Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -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 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 12 | from oftest import config |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 13 | import oftest.controller as controller |
Rich Lane | d7b0ffa | 2013-03-08 15:53:42 -0800 | [diff] [blame] | 14 | import ofp |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 15 | import oftest.dataplane as dataplane |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 16 | import oftest.parse as parse |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 17 | import oftest.base_tests as base_tests |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 18 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 19 | from oftest.testutils import * |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 20 | from time import sleep |
| 21 | |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 22 | # TODO: ovs has problems with VLAN id? |
| 23 | WILDCARD_VALUES = [ofp.OFPFW_IN_PORT, |
| 24 | # (ofp.OFPFW_DL_VLAN | ofp.OFPFW_DL_VLAN_PCP), |
| 25 | ofp.OFPFW_DL_SRC, |
| 26 | ofp.OFPFW_DL_DST, |
| 27 | (ofp.OFPFW_DL_TYPE | ofp.OFPFW_NW_SRC_ALL | |
| 28 | ofp.OFPFW_NW_DST_ALL | ofp.OFPFW_NW_TOS | ofp.OFPFW_NW_PROTO | |
| 29 | ofp.OFPFW_TP_SRC | ofp.OFPFW_TP_DST), |
| 30 | (ofp.OFPFW_NW_PROTO | ofp.OFPFW_TP_SRC | ofp.OFPFW_TP_DST), |
| 31 | ofp.OFPFW_TP_SRC, |
| 32 | ofp.OFPFW_TP_DST, |
| 33 | ofp.OFPFW_NW_SRC_MASK, |
| 34 | ofp.OFPFW_NW_DST_MASK, |
| 35 | ofp.OFPFW_DL_VLAN_PCP, |
| 36 | ofp.OFPFW_NW_TOS] |
| 37 | |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 38 | def sendPacket(obj, pkt, ingress_port, egress_port, test_timeout): |
| 39 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 40 | logging.info("Sending packet to dp port " + str(ingress_port) + |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 41 | ", expecting output on " + str(egress_port)) |
| 42 | obj.dataplane.send(ingress_port, str(pkt)) |
| 43 | |
| 44 | exp_pkt_arg = None |
| 45 | exp_port = None |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 46 | if config["relax"]: |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 47 | exp_pkt_arg = pkt |
| 48 | exp_port = egress_port |
| 49 | |
| 50 | (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(port_number=exp_port, |
| 51 | exp_pkt=exp_pkt_arg) |
| 52 | obj.assertTrue(rcv_pkt is not None, |
| 53 | "Packet not received on port " + str(egress_port)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 54 | logging.debug("Packet len " + str(len(rcv_pkt)) + " in on " + |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 55 | str(rcv_port)) |
| 56 | obj.assertEqual(rcv_port, egress_port, |
| 57 | "Packet received on port " + str(rcv_port) + |
| 58 | ", expected port " + str(egress_port)) |
| 59 | obj.assertEqual(str(pkt), str(rcv_pkt), |
| 60 | 'Response packet does not match send packet') |
| 61 | |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 62 | def getStats(obj, port): |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame] | 63 | stat_req = ofp.message.port_stats_request() |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 64 | stat_req.port_no = port |
| 65 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 66 | logging.info("Sending stats request") |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 67 | response, pkt = obj.controller.transact(stat_req, timeout=2) |
| 68 | obj.assertTrue(response is not None, |
| 69 | "No response to stats request") |
| 70 | obj.assertTrue(len(response.stats) == 1, |
| 71 | "Did not receive port stats reply") |
| 72 | for item in response.stats: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 73 | logging.info("Sent " + str(item.tx_packets) + " packets") |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 74 | packet_sent = item.tx_packets |
| 75 | packet_recv = item.rx_packets |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 76 | logging.info("Port %d stats count: tx %d rx %d" % (port, packet_sent, packet_recv)) |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 77 | return packet_sent, packet_recv |
| 78 | |
Shudong Zhou | 1a5b082 | 2012-10-29 22:03:34 -0700 | [diff] [blame] | 79 | def getAllStats(obj): |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame] | 80 | stat_req = ofp.message.port_stats_request() |
Shudong Zhou | 1a5b082 | 2012-10-29 22:03:34 -0700 | [diff] [blame] | 81 | stat_req.port_no = ofp.OFPP_NONE |
| 82 | |
| 83 | logging.info("Sending all port stats request") |
| 84 | response, pkt = obj.controller.transact(stat_req, timeout=2) |
| 85 | obj.assertTrue(response is not None, |
| 86 | "No response to stats request") |
| 87 | obj.assertTrue(len(response.stats) >= 3, |
| 88 | "Did not receive all port stats reply") |
| 89 | stats = {} |
| 90 | for item in response.stats: |
| 91 | stats[ item.port_no ] = ( item.tx_packets, item.rx_packets ) |
| 92 | return stats |
| 93 | |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 94 | def verifyStats(obj, port, test_timeout, packet_sent, packet_recv): |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame] | 95 | stat_req = ofp.message.port_stats_request() |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 96 | stat_req.port_no = port |
| 97 | |
| 98 | all_packets_received = 0 |
| 99 | all_packets_sent = 0 |
| 100 | sent = recv = 0 |
| 101 | for i in range(0,test_timeout): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 102 | logging.info("Sending stats request") |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 103 | response, pkt = obj.controller.transact(stat_req, |
| 104 | timeout=test_timeout) |
| 105 | obj.assertTrue(response is not None, |
| 106 | "No response to stats request") |
| 107 | obj.assertTrue(len(response.stats) == 1, |
| 108 | "Did not receive port stats reply") |
| 109 | for item in response.stats: |
| 110 | sent = item.tx_packets |
| 111 | recv = item.rx_packets |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 112 | logging.info("Sent " + str(item.tx_packets) + " packets") |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 113 | if item.tx_packets == packet_sent: |
| 114 | all_packets_sent = 1 |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 115 | logging.info("Received " + str(item.rx_packets) + " packets") |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 116 | if item.rx_packets == packet_recv: |
| 117 | all_packets_received = 1 |
| 118 | |
| 119 | if all_packets_received and all_packets_sent: |
| 120 | break |
| 121 | sleep(1) |
| 122 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 123 | logging.info("Expected port %d stats count: tx %d rx %d" % (port, packet_sent, packet_recv)) |
| 124 | logging.info("Actual port %d stats count: tx %d rx %d" % (port, sent, recv)) |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 125 | obj.assertTrue(all_packets_sent, |
| 126 | "Packet sent does not match number sent") |
| 127 | obj.assertTrue(all_packets_received, |
| 128 | "Packet received does not match number sent") |
| 129 | |
Rich Lane | 97e9965 | 2013-01-02 17:23:20 -0800 | [diff] [blame] | 130 | @group('smoke') |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 131 | class SingleFlowStats(base_tests.SimpleDataPlane): |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 132 | """ |
| 133 | Verify flow stats are properly retrieved. |
| 134 | |
| 135 | Generate a packet |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 136 | Generate and install a flow from port 1 to 2 |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 137 | Send the packet |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 138 | Send port stats request to port 1 & 2 |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 139 | Verify that the packet counter has incremented |
| 140 | """ |
| 141 | |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 142 | def runTest(self): |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 143 | # TODO: set from command-line parameter |
| 144 | test_timeout = 60 |
| 145 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 146 | of_ports = config["port_map"].keys() |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 147 | of_ports.sort() |
| 148 | self.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 149 | |
Rich Lane | 32bf948 | 2013-01-03 17:26:30 -0800 | [diff] [blame] | 150 | delete_all_flows(self.controller) |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 151 | |
| 152 | # build packet |
| 153 | pkt = simple_tcp_packet() |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 154 | match = packet_to_flow_match(self, pkt) |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 155 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
| 156 | self.assertTrue(match is not None, |
| 157 | "Could not generate flow match from pkt") |
Rich Lane | 9d3cc6b | 2013-03-08 16:33:08 -0800 | [diff] [blame^] | 158 | act = ofp.action.output() |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 159 | |
| 160 | # build flow |
| 161 | ingress_port = of_ports[0]; |
| 162 | egress_port = of_ports[1]; |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 163 | logging.info("Ingress " + str(ingress_port) + |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 164 | " to egress " + str(egress_port)) |
| 165 | match.in_port = ingress_port |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame] | 166 | flow_mod_msg = ofp.message.flow_mod() |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 167 | flow_mod_msg.match = match |
| 168 | flow_mod_msg.cookie = random.randint(0,9007199254740992) |
| 169 | flow_mod_msg.buffer_id = 0xffffffff |
| 170 | flow_mod_msg.idle_timeout = 0 |
| 171 | flow_mod_msg.hard_timeout = 0 |
| 172 | act.port = egress_port |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame] | 173 | flow_mod_msg.actions.add(act) |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 174 | |
| 175 | # send flow |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 176 | logging.info("Inserting flow") |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 177 | self.controller.message_send(flow_mod_msg) |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 178 | do_barrier(self.controller) |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 179 | |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 180 | # get initial port stats count |
| 181 | initTxInPort, initRxInPort = getStats(self, ingress_port) |
| 182 | initTxOutPort, initRxOutPort = getStats(self, egress_port) |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 183 | |
| 184 | # send packet N times |
| 185 | num_sends = random.randint(10,20) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 186 | logging.info("Sending " + str(num_sends) + " test packets") |
Shudong Zhou | df510a8 | 2012-08-03 18:08:40 -0700 | [diff] [blame] | 187 | for i in range(0,num_sends): |
| 188 | sendPacket(self, pkt, ingress_port, egress_port, |
| 189 | test_timeout) |
| 190 | |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 191 | verifyStats(self, ingress_port, test_timeout, initTxInPort, initRxInPort + num_sends) |
| 192 | verifyStats(self, egress_port, test_timeout, initTxOutPort + num_sends, initRxOutPort) |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 193 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 194 | class MultiFlowStats(base_tests.SimpleDataPlane): |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 195 | """ |
| 196 | Verify flow stats are properly retrieved. |
| 197 | |
| 198 | Generate two packets and install two matching flows |
| 199 | Send some number of packets |
| 200 | Send a port stats request to get packet count |
| 201 | Verify that the packet counter has incremented |
| 202 | """ |
| 203 | |
| 204 | def buildFlowModMsg(self, pkt, ingress_port, egress_port): |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 205 | match = packet_to_flow_match(self, pkt) |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 206 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
| 207 | self.assertTrue(match is not None, |
| 208 | "Could not generate flow match from pkt") |
| 209 | match.in_port = ingress_port |
| 210 | |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame] | 211 | flow_mod_msg = ofp.message.flow_mod() |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 212 | flow_mod_msg.match = match |
| 213 | flow_mod_msg.cookie = random.randint(0,9007199254740992) |
| 214 | flow_mod_msg.buffer_id = 0xffffffff |
| 215 | flow_mod_msg.idle_timeout = 0 |
| 216 | flow_mod_msg.hard_timeout = 0 |
Rich Lane | 9d3cc6b | 2013-03-08 16:33:08 -0800 | [diff] [blame^] | 217 | act = ofp.action.output() |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 218 | act.port = egress_port |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame] | 219 | flow_mod_msg.actions.add(act) |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 220 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 221 | logging.info("Ingress " + str(ingress_port) + |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 222 | " to egress " + str(egress_port)) |
| 223 | |
| 224 | return flow_mod_msg |
| 225 | |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 226 | def runTest(self): |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 227 | # TODO: set from command-line parameter |
| 228 | test_timeout = 60 |
| 229 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 230 | of_ports = config["port_map"].keys() |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 231 | of_ports.sort() |
| 232 | self.assertTrue(len(of_ports) >= 3, "Not enough ports for test") |
| 233 | ingress_port = of_ports[0]; |
| 234 | egress_port1 = of_ports[1]; |
| 235 | egress_port2 = of_ports[2]; |
| 236 | |
Rich Lane | 32bf948 | 2013-01-03 17:26:30 -0800 | [diff] [blame] | 237 | delete_all_flows(self.controller) |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 238 | |
| 239 | pkt1 = simple_tcp_packet() |
| 240 | flow_mod_msg1 = self.buildFlowModMsg(pkt1, ingress_port, egress_port1) |
| 241 | |
| 242 | pkt2 = simple_tcp_packet(dl_src='0:7:7:7:7:7') |
| 243 | flow_mod_msg2 = self.buildFlowModMsg(pkt2, ingress_port, egress_port2) |
| 244 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 245 | logging.info("Inserting flow1") |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 246 | self.controller.message_send(flow_mod_msg1) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 247 | logging.info("Inserting flow2") |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 248 | self.controller.message_send(flow_mod_msg2) |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 249 | do_barrier(self.controller) |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 250 | |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 251 | # get initial port stats count |
| 252 | initTxInPort, initRxInPort = getStats(self, ingress_port) |
| 253 | initTxOutPort1, initRxOutPort1 = getStats(self, egress_port1) |
| 254 | initTxOutPort2, initRxOutPort2 = getStats(self, egress_port2) |
| 255 | |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 256 | num_pkt1s = random.randint(10,30) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 257 | logging.info("Sending " + str(num_pkt1s) + " pkt1s") |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 258 | num_pkt2s = random.randint(10,30) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 259 | logging.info("Sending " + str(num_pkt2s) + " pkt2s") |
Shudong Zhou | 1d42392 | 2012-08-04 16:45:02 -0700 | [diff] [blame] | 260 | for i in range(0,num_pkt1s): |
| 261 | sendPacket(self, pkt1, ingress_port, egress_port1, test_timeout) |
| 262 | for i in range(0,num_pkt2s): |
| 263 | sendPacket(self, pkt2, ingress_port, egress_port2, test_timeout) |
Shudong Zhou | 50051c7 | 2012-08-06 16:53:46 -0700 | [diff] [blame] | 264 | |
| 265 | verifyStats(self, ingress_port, test_timeout, |
| 266 | initTxInPort, initRxInPort + num_pkt1s + num_pkt2s) |
| 267 | verifyStats(self, egress_port1, test_timeout, |
| 268 | initTxOutPort1 + num_pkt1s, initRxOutPort1) |
| 269 | verifyStats(self, egress_port2, test_timeout, |
| 270 | initTxOutPort2 + num_pkt2s, initRxOutPort2) |
Shudong Zhou | 1a5b082 | 2012-10-29 22:03:34 -0700 | [diff] [blame] | 271 | |
| 272 | class AllPortStats(base_tests.SimpleDataPlane): |
| 273 | """ |
| 274 | Verify all port stats are properly retrieved. |
| 275 | |
| 276 | First, get stats from each port. Then get all port stats, verify |
| 277 | consistency with single port stats. |
| 278 | """ |
| 279 | |
| 280 | # TODO: This is copied from MultiFlowStats. Need to combine. |
| 281 | def buildFlowModMsg(self, pkt, ingress_port, egress_port): |
| 282 | match = packet_to_flow_match(self, pkt) |
| 283 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
| 284 | self.assertTrue(match is not None, |
| 285 | "Could not generate flow match from pkt") |
| 286 | match.in_port = ingress_port |
| 287 | |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame] | 288 | flow_mod_msg = ofp.message.flow_mod() |
Shudong Zhou | 1a5b082 | 2012-10-29 22:03:34 -0700 | [diff] [blame] | 289 | flow_mod_msg.match = match |
| 290 | flow_mod_msg.cookie = random.randint(0,9007199254740992) |
| 291 | flow_mod_msg.buffer_id = 0xffffffff |
| 292 | flow_mod_msg.idle_timeout = 0 |
| 293 | flow_mod_msg.hard_timeout = 0 |
Rich Lane | 9d3cc6b | 2013-03-08 16:33:08 -0800 | [diff] [blame^] | 294 | act = ofp.action.output() |
Shudong Zhou | 1a5b082 | 2012-10-29 22:03:34 -0700 | [diff] [blame] | 295 | act.port = egress_port |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame] | 296 | flow_mod_msg.actions.add(act) |
Shudong Zhou | 1a5b082 | 2012-10-29 22:03:34 -0700 | [diff] [blame] | 297 | |
| 298 | logging.info("Ingress " + str(ingress_port) + |
| 299 | " to egress " + str(egress_port)) |
| 300 | |
| 301 | return flow_mod_msg |
| 302 | |
| 303 | def runTest(self): |
| 304 | # TODO: set from command-line parameter |
| 305 | test_timeout = 60 |
| 306 | |
| 307 | of_ports = config["port_map"].keys() |
| 308 | of_ports.sort() |
| 309 | self.assertTrue(len(of_ports) >= 3, "Not enough ports for test") |
| 310 | port0 = of_ports[0]; |
| 311 | port1 = of_ports[1]; |
| 312 | port2 = of_ports[2]; |
| 313 | |
| 314 | # construct some packets and flows, send to switch |
| 315 | pkt1 = simple_tcp_packet() |
| 316 | flow_mod_msg1 = self.buildFlowModMsg(pkt1, port0, port1) |
| 317 | |
| 318 | pkt2 = simple_tcp_packet(dl_src='0:7:7:7:7:7') |
| 319 | flow_mod_msg2 = self.buildFlowModMsg(pkt2, port0, port2) |
| 320 | |
| 321 | logging.info("Inserting flow1") |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 322 | self.controller.message_send(flow_mod_msg1) |
Shudong Zhou | 1a5b082 | 2012-10-29 22:03:34 -0700 | [diff] [blame] | 323 | logging.info("Inserting flow2") |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 324 | self.controller.message_send(flow_mod_msg2) |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 325 | do_barrier(self.controller) |
Shudong Zhou | 1a5b082 | 2012-10-29 22:03:34 -0700 | [diff] [blame] | 326 | |
| 327 | num_pkt1s = random.randint(5,10) |
| 328 | logging.info("Sending " + str(num_pkt1s) + " pkt1s") |
| 329 | num_pkt2s = random.randint(10,15) |
| 330 | logging.info("Sending " + str(num_pkt2s) + " pkt2s") |
| 331 | for i in range(0,num_pkt1s): |
| 332 | sendPacket(self, pkt1, port0, port1, test_timeout) |
| 333 | for i in range(0,num_pkt2s): |
| 334 | sendPacket(self, pkt2, port0, port2, test_timeout) |
| 335 | |
| 336 | # get individual port stats count |
| 337 | port_stats = {} |
| 338 | port_stats[ port0 ] = getStats(self, port0) |
| 339 | port_stats[ port1 ] = getStats(self, port1) |
| 340 | port_stats[ port2 ] = getStats(self, port2) |
| 341 | |
| 342 | all_stats = getAllStats(self) |
Rich Lane | e53897c | 2012-10-30 09:40:13 -0700 | [diff] [blame] | 343 | self.assertEqual(port_stats[ port0 ], all_stats[ port0 ]) |
| 344 | self.assertEqual(port_stats[ port1 ], all_stats[ port1 ]) |
| 345 | self.assertEqual(port_stats[ port2 ], all_stats[ port2 ]) |