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