Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 1 | """ |
| 2 | Flow expire test case. |
| 3 | Similar to Flow expire test case in the perl test harness. |
| 4 | |
| 5 | """ |
| 6 | |
| 7 | import logging |
| 8 | |
| 9 | import unittest |
| 10 | import random |
| 11 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 12 | from oftest import config |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 13 | import oftest.controller as controller |
| 14 | import oftest.cstruct as ofp |
| 15 | import oftest.message as message |
| 16 | import oftest.dataplane as dataplane |
| 17 | import oftest.action as action |
| 18 | import oftest.parse as parse |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 19 | import oftest.base_tests as base_tests |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 20 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 21 | from oftest.testutils import * |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 22 | from time import sleep |
| 23 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 24 | class FlowExpire(base_tests.SimpleDataPlane): |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 25 | """ |
| 26 | Verify flow expire messages are properly generated. |
| 27 | |
| 28 | Generate a packet |
| 29 | Generate and install a matching flow with idle timeout = 1 sec |
| 30 | Verify the flow expiration message is received |
| 31 | """ |
| 32 | def runTest(self): |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 33 | # TODO: set from command-line parameter |
| 34 | test_timeout = 60 |
| 35 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 36 | of_ports = config["port_map"].keys() |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 37 | of_ports.sort() |
| 38 | self.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 39 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 40 | rc = delete_all_flows(self.controller) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 41 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 42 | |
| 43 | pkt = simple_tcp_packet() |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 44 | match = packet_to_flow_match(self, pkt) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 45 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
| 46 | self.assertTrue(match is not None, |
| 47 | "Could not generate flow match from pkt") |
| 48 | act = action.action_output() |
| 49 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 50 | of_ports = config["port_map"].keys() |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 51 | of_ports.sort() |
| 52 | self.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 53 | |
| 54 | ingress_port = of_ports[0] |
| 55 | egress_port = of_ports[1] |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 56 | logging.info("Ingress " + str(ingress_port) + |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 57 | " to egress " + str(egress_port)) |
| 58 | |
| 59 | match.in_port = ingress_port |
| 60 | |
| 61 | request = message.flow_mod() |
| 62 | request.match = match |
| 63 | request.cookie = random.randint(0,9007199254740992) |
| 64 | request.buffer_id = 0xffffffff |
| 65 | request.idle_timeout = 1 |
| 66 | request.flags |= ofp.OFPFF_SEND_FLOW_REM |
| 67 | act.port = egress_port |
| 68 | self.assertTrue(request.actions.add(act), "Could not add action") |
| 69 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 70 | logging.info("Inserting flow") |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 71 | rv = self.controller.message_send(request) |
| 72 | self.assertTrue(rv != -1, "Error installing flow mod") |
Dan Talayco | 0fc08bd | 2012-04-09 16:56:18 -0700 | [diff] [blame] | 73 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 74 | |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 75 | (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_FLOW_REMOVED, |
| 76 | timeout=test_timeout) |
| 77 | |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 78 | self.assertTrue(response is not None, |
| 79 | 'Did not receive flow removed message ') |
| 80 | |
| 81 | self.assertEqual(request.cookie, response.cookie, |
| 82 | 'Cookies do not match') |
| 83 | |
| 84 | self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason, |
| 85 | 'Flow table entry removal reason is not idle_timeout') |
| 86 | |
| 87 | self.assertEqual(match, response.match, |
| 88 | 'Flow table entry does not match') |
| 89 | |