Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 1 | """ |
| 2 | Flow stats test case. |
| 3 | Similar to Flow stats 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 FlowStats(basic.SimpleDataPlane): |
| 50 | """ |
| 51 | Verify flow stats are properly retrieved. |
| 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 | |
Dan Talayco | 39bf691 | 2010-07-08 14:17:52 -0700 | [diff] [blame] | 74 | ingress_port = of_ports[0]; |
| 75 | egress_port = of_ports[1]; |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 76 | pa_logger.info("Ingress " + str(ingress_port) + |
| 77 | " to egress " + str(egress_port)) |
| 78 | |
| 79 | match.in_port = ingress_port |
| 80 | |
| 81 | flow_mod_msg = message.flow_mod() |
| 82 | flow_mod_msg.match = match |
| 83 | flow_mod_msg.cookie = random.randint(0,9007199254740992) |
| 84 | flow_mod_msg.buffer_id = 0xffffffff |
| 85 | flow_mod_msg.idle_timeout = 1 |
| 86 | act.port = egress_port |
| 87 | self.assertTrue(flow_mod_msg.actions.add(act), "Could not add action") |
| 88 | |
| 89 | stat_req = message.flow_stats_request() |
| 90 | stat_req.match = match |
| 91 | stat_req.table_id = 0xff |
| 92 | stat_req.out_port = ofp.OFPP_NONE; |
| 93 | |
| 94 | do_barrier(self.controller) |
| 95 | pa_logger.info("Sending stats request") |
| 96 | rv = self.controller.message_send(stat_req) |
| 97 | self.assertTrue(rv != -1, "Error sending flow stat req") |
| 98 | do_barrier(self.controller) |
| 99 | |
| 100 | (response, raw) = self.controller.poll(ofp.OFPT_STATS_REPLY, 2) |
| 101 | |
| 102 | pa_logger.info("Inserting flow") |
| 103 | rv = self.controller.message_send(flow_mod_msg) |
| 104 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 105 | do_barrier(self.controller) |
| 106 | |
| 107 | pa_logger.info("Sending packet to dp port " + |
| 108 | str(ingress_port)) |
| 109 | self.dataplane.send(ingress_port, str(pkt)) |
| 110 | (rcv_port, rcv_pkt, pkt_time) = self.dataplane.poll(timeout=2) |
| 111 | self.assertTrue(rcv_pkt is not None, "Did not receive packet") |
| 112 | pa_logger.debug("Packet len " + str(len(pkt)) + " in on " + |
| 113 | str(rcv_port)) |
| 114 | self.assertEqual(rcv_port, egress_port, "Unexpected receive port") |
| 115 | self.assertEqual(str(pkt), str(rcv_pkt), |
| 116 | 'Response packet does not match send packet') |
| 117 | |
| 118 | pa_logger.info("Sending stats request") |
| 119 | rv = self.controller.message_send(stat_req) |
| 120 | self.assertTrue(rv != -1, "Error sending flow stat req") |
| 121 | do_barrier(self.controller) |
| 122 | |
| 123 | (response, raw) = self.controller.poll(ofp.OFPT_STATS_REPLY, 2) |
| 124 | #print "YYY: Stats reply is \n%s" % (response.show()) |
| 125 | self.assertTrue(len(response.stats) == 1, "Did not receive flow stats reply") |