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 |
| 25 | pa_port_map = None |
| 26 | #@var pa_logger Local logger object |
| 27 | pa_logger = None |
| 28 | #@var pa_config Local copy of global configuration data |
| 29 | pa_config = None |
| 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 | |
| 38 | global pa_port_map |
| 39 | global pa_logger |
| 40 | global pa_config |
| 41 | |
| 42 | pa_logger = logging.getLogger("pkt_act") |
| 43 | pa_logger.info("Initializing test set") |
| 44 | pa_port_map = config["port_map"] |
| 45 | pa_config = config |
| 46 | |
| 47 | class FlowExpire(basic.SimpleDataPlane): |
| 48 | """ |
| 49 | Verify flow expire messages are properly generated. |
| 50 | |
| 51 | Generate a packet |
| 52 | Generate and install a matching flow with idle timeout = 1 sec |
| 53 | Verify the flow expiration message is received |
| 54 | """ |
| 55 | def runTest(self): |
| 56 | global pa_port_map |
| 57 | |
| 58 | of_ports = pa_port_map.keys() |
| 59 | of_ports.sort() |
| 60 | self.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 61 | |
| 62 | rc = delete_all_flows(self.controller, pa_logger) |
| 63 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 64 | |
| 65 | pkt = simple_tcp_packet() |
| 66 | match = parse.packet_to_flow_match(pkt) |
| 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 | |
| 72 | ingress_port = pa_config["base_of_port"] |
| 73 | egress_port = (pa_config["base_of_port"] + 1) % len(of_ports) |
| 74 | pa_logger.info("Ingress " + str(ingress_port) + |
| 75 | " to egress " + str(egress_port)) |
| 76 | |
| 77 | match.in_port = ingress_port |
| 78 | |
| 79 | request = message.flow_mod() |
| 80 | request.match = match |
| 81 | request.cookie = random.randint(0,9007199254740992) |
| 82 | request.buffer_id = 0xffffffff |
| 83 | request.idle_timeout = 1 |
| 84 | request.flags |= ofp.OFPFF_SEND_FLOW_REM |
| 85 | act.port = egress_port |
| 86 | self.assertTrue(request.actions.add(act), "Could not add action") |
| 87 | |
| 88 | pa_logger.info("Inserting flow") |
| 89 | rv = self.controller.message_send(request) |
| 90 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 91 | do_barrier(self.controller) |
| 92 | |
| 93 | (response, raw) = self.controller.poll(ofp.OFPT_FLOW_REMOVED, 2) |
| 94 | |
| 95 | self.assertTrue(response is not None, |
| 96 | 'Did not receive flow removed message ') |
| 97 | |
| 98 | self.assertEqual(request.cookie, response.cookie, |
| 99 | 'Cookies do not match') |
| 100 | |
| 101 | self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason, |
| 102 | 'Flow table entry removal reason is not idle_timeout') |
| 103 | |
| 104 | self.assertEqual(match, response.match, |
| 105 | 'Flow table entry does not match') |
| 106 | |