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 | |
Ed Swierk | 6ccbb07 | 2012-03-19 14:48:40 -0700 | [diff] [blame] | 38 | basic.test_set_init(config) |
| 39 | |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 40 | global pa_port_map |
| 41 | global pa_logger |
| 42 | global pa_config |
| 43 | |
| 44 | pa_logger = logging.getLogger("pkt_act") |
| 45 | pa_logger.info("Initializing test set") |
| 46 | pa_port_map = config["port_map"] |
| 47 | pa_config = config |
| 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): |
| 58 | global pa_port_map |
| 59 | |
| 60 | of_ports = pa_port_map.keys() |
| 61 | of_ports.sort() |
| 62 | self.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 63 | |
| 64 | rc = delete_all_flows(self.controller, pa_logger) |
| 65 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 66 | |
| 67 | pkt = simple_tcp_packet() |
| 68 | match = parse.packet_to_flow_match(pkt) |
| 69 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
| 70 | self.assertTrue(match is not None, |
| 71 | "Could not generate flow match from pkt") |
| 72 | act = action.action_output() |
| 73 | |
| 74 | ingress_port = pa_config["base_of_port"] |
| 75 | egress_port = (pa_config["base_of_port"] + 1) % len(of_ports) |
| 76 | pa_logger.info("Ingress " + str(ingress_port) + |
| 77 | " to egress " + str(egress_port)) |
| 78 | |
| 79 | match.in_port = ingress_port |
| 80 | |
| 81 | request = message.flow_mod() |
| 82 | request.match = match |
| 83 | request.cookie = random.randint(0,9007199254740992) |
| 84 | request.buffer_id = 0xffffffff |
| 85 | request.idle_timeout = 1 |
| 86 | request.flags |= ofp.OFPFF_SEND_FLOW_REM |
| 87 | act.port = egress_port |
| 88 | self.assertTrue(request.actions.add(act), "Could not add action") |
| 89 | |
| 90 | pa_logger.info("Inserting flow") |
| 91 | rv = self.controller.message_send(request) |
| 92 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 93 | do_barrier(self.controller) |
| 94 | |
| 95 | (response, raw) = self.controller.poll(ofp.OFPT_FLOW_REMOVED, 2) |
| 96 | |
| 97 | self.assertTrue(response is not None, |
| 98 | 'Did not receive flow removed message ') |
| 99 | |
| 100 | self.assertEqual(request.cookie, response.cookie, |
| 101 | 'Cookies do not match') |
| 102 | |
| 103 | self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason, |
| 104 | 'Flow table entry removal reason is not idle_timeout') |
| 105 | |
| 106 | self.assertEqual(match, response.match, |
| 107 | 'Flow table entry does not match') |
| 108 | |