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