Dan Talayco | 5eba844 | 2010-03-10 13:58:43 -0800 | [diff] [blame] | 1 | """ |
| 2 | Test cases for testing actions taken on packets |
| 3 | |
| 4 | See basic.py for other info. |
| 5 | |
| 6 | It is recommended that these definitions be kept in their own |
| 7 | namespace as different groups of tests will likely define |
| 8 | similar identifiers. |
| 9 | |
| 10 | The function test_set_init is called with a complete configuration |
| 11 | dictionary prior to the invocation of any tests from this file. |
| 12 | |
| 13 | The switch is actively attempting to contact the controller at the address |
| 14 | indicated oin oft_config |
| 15 | |
| 16 | """ |
| 17 | |
| 18 | import logging |
| 19 | |
| 20 | import unittest |
| 21 | |
| 22 | import oftest.controller as controller |
| 23 | import oftest.cstruct as ofp |
| 24 | import oftest.message as message |
| 25 | import oftest.dataplane as dataplane |
| 26 | import oftest.action as action |
| 27 | import oftest.parse as parse |
| 28 | import basic |
| 29 | |
| 30 | from testutils import * |
| 31 | |
| 32 | #@var port_map Local copy of the configuration map from OF port |
| 33 | # numbers to OS interfaces |
| 34 | pa_port_map = None |
| 35 | #@var pa_logger Local logger object |
| 36 | pa_logger = None |
| 37 | #@var pa_config Local copy of global configuration data |
| 38 | pa_config = None |
| 39 | |
| 40 | def test_set_init(config): |
| 41 | """ |
| 42 | Set up function for packet action test classes |
| 43 | |
| 44 | @param config The configuration dictionary; see oft |
| 45 | """ |
| 46 | |
| 47 | global pa_port_map |
| 48 | global pa_logger |
| 49 | global pa_config |
| 50 | |
| 51 | pa_logger = logging.getLogger("pkt_act") |
| 52 | pa_logger.info("Initializing test set") |
| 53 | pa_port_map = config["port_map"] |
| 54 | pa_config = config |
| 55 | |
| 56 | class DirectPacket(basic.SimpleDataPlane): |
| 57 | """ |
| 58 | Test case that checks single port direction action |
| 59 | |
| 60 | Generate a packet |
| 61 | Generate and install a matching flow |
| 62 | Add action to direct the packet to an egress port |
| 63 | Send the packet to ingress dataplane port |
| 64 | Verify the packet is received at the egress port only |
| 65 | """ |
| 66 | def runTest(self): |
| 67 | global pa_port_map |
| 68 | of_ports = pa_port_map.keys() |
| 69 | of_ports.sort() |
| 70 | self.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 71 | |
| 72 | rc = delete_all_flows(self.controller, pa_logger) |
| 73 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 74 | |
| 75 | pkt = simple_tcp_packet() |
| 76 | match = parse.packet_to_flow_match(pkt) |
Dan Talayco | 7dd6cd6 | 2010-03-16 15:02:35 -0700 | [diff] [blame] | 77 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
Dan Talayco | 5eba844 | 2010-03-10 13:58:43 -0800 | [diff] [blame] | 78 | self.assertTrue(match is not None, |
| 79 | "Could not generate flow match from pkt") |
| 80 | act = action.action_output() |
| 81 | |
| 82 | for idx in range(len(of_ports)): |
| 83 | ingress_port = of_ports[idx] |
| 84 | egress_port = of_ports[(idx + 1) % len(of_ports)] |
| 85 | pa_logger.info("Ingress " + str(ingress_port) + |
| 86 | " to egress " + str(egress_port)) |
| 87 | |
| 88 | match.in_port = ingress_port |
| 89 | |
| 90 | request = message.flow_mod() |
| 91 | request.match = match |
| 92 | request.buffer_id = 0xffffffff |
| 93 | act.port = egress_port |
| 94 | self.assertTrue(request.actions.add(act), "Could not add action") |
| 95 | |
| 96 | pa_logger.info("Inserting flow") |
| 97 | rv = self.controller.message_send(request) |
| 98 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 99 | do_barrier(self.controller) |
| 100 | |
| 101 | pa_logger.info("Sending packet to dp port " + |
| 102 | str(ingress_port)) |
| 103 | self.dataplane.send(ingress_port, str(pkt)) |
| 104 | (rcv_port, rcv_pkt, pkt_time) = self.dataplane.poll(timeout=1) |
| 105 | self.assertTrue(rcv_pkt is not None, "Did not receive packet") |
| 106 | pa_logger.debug("Packet len " + str(len(pkt)) + " in on " + |
| 107 | str(rcv_port)) |
| 108 | self.assertEqual(rcv_port, egress_port, "Unexpected receive port") |
| 109 | self.assertEqual(str(pkt), str(rcv_pkt), |
| 110 | 'Response packet does not match send packet') |
| 111 | |
| 112 | |