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 | |
| 12 | import oftest.controller as controller |
| 13 | import oftest.cstruct as ofp |
| 14 | import oftest.message as message |
| 15 | import oftest.dataplane as dataplane |
| 16 | import oftest.action as action |
| 17 | import oftest.parse as parse |
| 18 | import basic |
| 19 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 20 | from oftest.testutils import * |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 21 | from time import sleep |
| 22 | |
| 23 | #@var port_map Local copy of the configuration map from OF port |
| 24 | # numbers to OS interfaces |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 25 | fe_port_map = None |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 26 | #@var fe_config Local copy of global configuration data |
| 27 | fe_config = None |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 28 | |
| 29 | def test_set_init(config): |
| 30 | """ |
| 31 | Set up function for packet action test classes |
| 32 | |
| 33 | @param config The configuration dictionary; see oft |
| 34 | """ |
| 35 | |
Ed Swierk | 6ccbb07 | 2012-03-19 14:48:40 -0700 | [diff] [blame] | 36 | basic.test_set_init(config) |
| 37 | |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 38 | global fe_port_map |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 39 | global fe_config |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 40 | |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 41 | fe_port_map = config["port_map"] |
| 42 | fe_config = config |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 43 | |
| 44 | class FlowExpire(basic.SimpleDataPlane): |
| 45 | """ |
| 46 | Verify flow expire messages are properly generated. |
| 47 | |
| 48 | Generate a packet |
| 49 | Generate and install a matching flow with idle timeout = 1 sec |
| 50 | Verify the flow expiration message is received |
| 51 | """ |
| 52 | def runTest(self): |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 53 | global fe_port_map |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 54 | |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 55 | # TODO: set from command-line parameter |
| 56 | test_timeout = 60 |
| 57 | |
| 58 | of_ports = fe_port_map.keys() |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 59 | of_ports.sort() |
| 60 | self.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 61 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 62 | rc = delete_all_flows(self.controller) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 63 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 64 | |
| 65 | pkt = simple_tcp_packet() |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 66 | match = packet_to_flow_match(self, pkt) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 67 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
| 68 | self.assertTrue(match is not None, |
| 69 | "Could not generate flow match from pkt") |
| 70 | act = action.action_output() |
| 71 | |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 72 | of_ports = fe_port_map.keys() |
| 73 | of_ports.sort() |
| 74 | self.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 75 | |
| 76 | ingress_port = of_ports[0] |
| 77 | egress_port = of_ports[1] |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 78 | logging.info("Ingress " + str(ingress_port) + |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 79 | " to egress " + str(egress_port)) |
| 80 | |
| 81 | match.in_port = ingress_port |
| 82 | |
| 83 | request = message.flow_mod() |
| 84 | request.match = match |
| 85 | request.cookie = random.randint(0,9007199254740992) |
| 86 | request.buffer_id = 0xffffffff |
| 87 | request.idle_timeout = 1 |
| 88 | request.flags |= ofp.OFPFF_SEND_FLOW_REM |
| 89 | act.port = egress_port |
| 90 | self.assertTrue(request.actions.add(act), "Could not add action") |
| 91 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 92 | logging.info("Inserting flow") |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 93 | rv = self.controller.message_send(request) |
| 94 | self.assertTrue(rv != -1, "Error installing flow mod") |
Dan Talayco | 0fc08bd | 2012-04-09 16:56:18 -0700 | [diff] [blame] | 95 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 96 | |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 97 | (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_FLOW_REMOVED, |
| 98 | timeout=test_timeout) |
| 99 | |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 100 | self.assertTrue(response is not None, |
| 101 | 'Did not receive flow removed message ') |
| 102 | |
| 103 | self.assertEqual(request.cookie, response.cookie, |
| 104 | 'Cookies do not match') |
| 105 | |
| 106 | self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason, |
| 107 | 'Flow table entry removal reason is not idle_timeout') |
| 108 | |
| 109 | self.assertEqual(match, response.match, |
| 110 | 'Flow table entry does not match') |
| 111 | |