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 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 23 | #@var fs_port_map Local copy of the configuration map from OF port |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 24 | # numbers to OS interfaces |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 25 | fs_port_map = None |
| 26 | #@var fs_logger Local logger object |
| 27 | fs_logger = None |
| 28 | #@var fs_config Local copy of global configuration data |
| 29 | fs_config = None |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 30 | |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 31 | # TODO: ovs has problems with VLAN id? |
| 32 | WILDCARD_VALUES = [ofp.OFPFW_IN_PORT, |
| 33 | # ofp.OFPFW_DL_VLAN, |
| 34 | ofp.OFPFW_DL_SRC, |
| 35 | ofp.OFPFW_DL_DST, |
| 36 | ofp.OFPFW_DL_TYPE, |
| 37 | ofp.OFPFW_NW_PROTO, |
| 38 | ofp.OFPFW_TP_SRC, |
| 39 | ofp.OFPFW_TP_DST, |
| 40 | 0x3F << ofp.OFPFW_NW_SRC_SHIFT, |
| 41 | 0x3F << ofp.OFPFW_NW_DST_SHIFT, |
| 42 | ofp.OFPFW_DL_VLAN_PCP, |
| 43 | ofp.OFPFW_NW_TOS] |
| 44 | |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 45 | def test_set_init(config): |
| 46 | """ |
| 47 | Set up function for packet action test classes |
| 48 | |
| 49 | @param config The configuration dictionary; see oft |
| 50 | """ |
| 51 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 52 | global fs_port_map |
| 53 | global fs_logger |
| 54 | global fs_config |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 55 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 56 | fs_logger = logging.getLogger("flow_stats") |
| 57 | fs_logger.info("Initializing test set") |
| 58 | fs_port_map = config["port_map"] |
| 59 | fs_config = config |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 60 | |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 61 | class SingleFlowStats(basic.SimpleDataPlane): |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 62 | """ |
| 63 | Verify flow stats are properly retrieved. |
| 64 | |
| 65 | Generate a packet |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 66 | Generate and install a matching flow |
| 67 | Send the packet |
| 68 | Send a flow stats request to match the flow and retrieve stats |
| 69 | Verify that the packet counter has incremented |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 70 | """ |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 71 | |
| 72 | def verifyStats(self, match, out_port, test_timeout, packet_count): |
| 73 | stat_req = message.flow_stats_request() |
| 74 | stat_req.match = match |
| 75 | stat_req.table_id = 0xff |
| 76 | stat_req.out_port = out_port |
| 77 | |
| 78 | all_packets_received = 0 |
| 79 | for i in range(0,test_timeout): |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 80 | fs_logger.info("Sending stats request") |
| 81 | response, pkt = self.controller.transact(stat_req, |
| 82 | timeout=test_timeout) |
| 83 | self.assertTrue(response is not None, |
| 84 | "No response to stats request") |
| 85 | self.assertTrue(len(response.stats) == 1, |
| 86 | "Did not receive flow stats reply") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 87 | for obj in response.stats: |
| 88 | # TODO: pad1 and pad2 fields may be nonzero, is this a bug? |
| 89 | # for now, just clear them so the assert is simpler |
| 90 | #obj.match.pad1 = 0 |
| 91 | #obj.match.pad2 = [0, 0] |
| 92 | #self.assertEqual(match, obj.match, |
| 93 | # "Matches do not match") |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 94 | fs_logger.info("Received " + str(obj.packet_count) + " packets") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 95 | if obj.packet_count == packet_count: |
| 96 | all_packets_received = 1 |
| 97 | |
| 98 | if all_packets_received: |
| 99 | break |
| 100 | sleep(1) |
| 101 | |
| 102 | self.assertTrue(all_packets_received, |
| 103 | "Packet count does not match number sent") |
| 104 | |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 105 | def runTest(self): |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 106 | global fs_port_map |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 107 | |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 108 | # TODO: set from command-line parameter |
| 109 | test_timeout = 60 |
| 110 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 111 | of_ports = fs_port_map.keys() |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 112 | of_ports.sort() |
| 113 | self.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 114 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 115 | rc = delete_all_flows(self.controller, fs_logger) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 116 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 117 | |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 118 | # build packet |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 119 | pkt = simple_tcp_packet() |
| 120 | match = parse.packet_to_flow_match(pkt) |
| 121 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
| 122 | self.assertTrue(match is not None, |
| 123 | "Could not generate flow match from pkt") |
| 124 | act = action.action_output() |
| 125 | |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 126 | # build flow |
Dan Talayco | 39bf691 | 2010-07-08 14:17:52 -0700 | [diff] [blame] | 127 | ingress_port = of_ports[0]; |
| 128 | egress_port = of_ports[1]; |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 129 | fs_logger.info("Ingress " + str(ingress_port) + |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 130 | " to egress " + str(egress_port)) |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 131 | match.in_port = ingress_port |
| 132 | flow_mod_msg = message.flow_mod() |
| 133 | flow_mod_msg.match = match |
| 134 | flow_mod_msg.cookie = random.randint(0,9007199254740992) |
| 135 | flow_mod_msg.buffer_id = 0xffffffff |
| 136 | flow_mod_msg.idle_timeout = 0 |
| 137 | flow_mod_msg.hard_timeout = 0 |
| 138 | act.port = egress_port |
| 139 | self.assertTrue(flow_mod_msg.actions.add(act), "Could not add action") |
| 140 | |
| 141 | # send flow |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 142 | fs_logger.info("Inserting flow") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 143 | rv = self.controller.message_send(flow_mod_msg) |
| 144 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 145 | do_barrier(self.controller) |
| 146 | |
| 147 | # no packets sent, so zero packet count |
| 148 | self.verifyStats(match, ofp.OFPP_NONE, test_timeout, 0) |
| 149 | |
| 150 | # send packet N times |
| 151 | num_sends = random.randint(10,20) |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 152 | fs_logger.info("Sending " + str(num_sends) + " test packets") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 153 | for i in range(0,num_sends): |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 154 | fs_logger.info("Sending packet to dp port " + |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 155 | str(ingress_port)) |
| 156 | self.dataplane.send(ingress_port, str(pkt)) |
Dan Talayco | aff26c8 | 2012-03-25 15:06:26 -0700 | [diff] [blame] | 157 | (rcv_port, rcv_pkt, pkt_time) = self.dataplane.poll(timeout= |
| 158 | test_timeout) |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 159 | self.assertTrue(rcv_pkt is not None, "Did not receive packet") |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 160 | fs_logger.debug("Packet len " + str(len(pkt)) + " in on " + |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 161 | str(rcv_port)) |
| 162 | self.assertEqual(rcv_port, egress_port, "Unexpected receive port") |
| 163 | for j in range(0,test_timeout): |
| 164 | if str(pkt) == str(rcv_pkt): |
| 165 | break |
| 166 | sleep(1) |
| 167 | self.assertTrue(j < test_timeout, |
| 168 | 'Timeout sending packets for flow stats test') |
| 169 | |
| 170 | self.verifyStats(match, ofp.OFPP_NONE, test_timeout, num_sends) |
| 171 | self.verifyStats(match, egress_port, test_timeout, num_sends) |
| 172 | for wc in WILDCARD_VALUES: |
| 173 | match.wildcards = wc |
| 174 | self.verifyStats(match, egress_port, test_timeout, num_sends) |
| 175 | |
| 176 | |
| 177 | class TwoFlowStats(basic.SimpleDataPlane): |
| 178 | """ |
| 179 | Verify flow stats are properly retrieved. |
| 180 | |
| 181 | Generate two packets and install two matching flows |
| 182 | Send some number of packets |
| 183 | Send a flow stats request to match the flows and retrieve stats |
| 184 | Verify that the packet counter has incremented |
| 185 | |
| 186 | TODO: add a third flow, and then configure the match to exclude |
| 187 | that flow? |
| 188 | """ |
| 189 | |
| 190 | def buildFlowModMsg(self, pkt, ingress_port, egress_port): |
| 191 | match = parse.packet_to_flow_match(pkt) |
| 192 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
| 193 | self.assertTrue(match is not None, |
| 194 | "Could not generate flow match from pkt") |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 195 | match.in_port = ingress_port |
| 196 | |
| 197 | flow_mod_msg = message.flow_mod() |
| 198 | flow_mod_msg.match = match |
| 199 | flow_mod_msg.cookie = random.randint(0,9007199254740992) |
| 200 | flow_mod_msg.buffer_id = 0xffffffff |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 201 | flow_mod_msg.idle_timeout = 0 |
| 202 | flow_mod_msg.hard_timeout = 0 |
| 203 | act = action.action_output() |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 204 | act.port = egress_port |
| 205 | self.assertTrue(flow_mod_msg.actions.add(act), "Could not add action") |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 206 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 207 | fs_logger.info("Ingress " + str(ingress_port) + |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 208 | " to egress " + str(egress_port)) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 209 | |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 210 | return flow_mod_msg |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 211 | |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 212 | def sendPacket(self, pkt, ingress_port, egress_port, test_timeout): |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 213 | fs_logger.info("Sending packet to dp port " + |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 214 | str(ingress_port)) |
| 215 | self.dataplane.send(ingress_port, str(pkt)) |
Dan Talayco | aff26c8 | 2012-03-25 15:06:26 -0700 | [diff] [blame] | 216 | (rcv_port, rcv_pkt, pkt_time) = self.dataplane.poll(timeout= |
| 217 | test_timeout) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 218 | self.assertTrue(rcv_pkt is not None, "Did not receive packet") |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 219 | fs_logger.debug("Packet len " + str(len(pkt)) + " in on " + |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 220 | str(rcv_port)) |
| 221 | self.assertEqual(rcv_port, egress_port, "Unexpected receive port") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 222 | for j in range(0,test_timeout): |
| 223 | if str(pkt) == str(rcv_pkt): |
| 224 | break |
| 225 | sleep(1) |
| 226 | self.assertTrue(j < test_timeout, |
| 227 | 'Timeout sending packets for flow stats test') |
| 228 | |
| 229 | def verifyStats(self, match, out_port, test_timeout, packet_count): |
| 230 | stat_req = message.flow_stats_request() |
| 231 | stat_req.match = match |
| 232 | stat_req.table_id = 0xff |
| 233 | stat_req.out_port = out_port |
| 234 | |
| 235 | all_packets_received = 0 |
| 236 | for i in range(0,test_timeout): |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 237 | fs_logger.info("Sending stats request") |
| 238 | response, pkt = self.controller.transact(stat_req, |
| 239 | timeout=test_timeout) |
| 240 | self.assertTrue(response is not None, |
| 241 | "No response to stats request") |
Dan Talayco | aff26c8 | 2012-03-25 15:06:26 -0700 | [diff] [blame] | 242 | self.assertTrue(len(response.stats) >= 1, |
| 243 | "Did not receive flow stats reply") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 244 | total_packets = 0 |
| 245 | for obj in response.stats: |
| 246 | # TODO: pad1 and pad2 fields may be nonzero, is this a bug? |
| 247 | # for now, just clear them so the assert is simpler |
| 248 | #obj.match.pad1 = 0 |
| 249 | #obj.match.pad2 = [0, 0] |
| 250 | #self.assertEqual(match, obj.match, |
| 251 | # "Matches do not match") |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 252 | fs_logger.info("Received " + str(obj.packet_count) + " packets") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 253 | total_packets += obj.packet_count |
| 254 | if total_packets == packet_count: |
| 255 | all_packets_received = 1 |
| 256 | break |
| 257 | sleep(1) |
| 258 | |
| 259 | self.assertTrue(all_packets_received, |
| 260 | "Packet count does not match number sent") |
| 261 | |
| 262 | def runTest(self): |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 263 | global fs_port_map |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 264 | |
| 265 | # TODO: set from command-line parameter |
| 266 | test_timeout = 60 |
| 267 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 268 | of_ports = fs_port_map.keys() |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 269 | of_ports.sort() |
| 270 | self.assertTrue(len(of_ports) >= 3, "Not enough ports for test") |
| 271 | ingress_port = of_ports[0]; |
| 272 | egress_port1 = of_ports[1]; |
| 273 | egress_port2 = of_ports[2]; |
| 274 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 275 | rc = delete_all_flows(self.controller, fs_logger) |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 276 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 277 | |
| 278 | pkt1 = simple_tcp_packet() |
| 279 | flow_mod_msg1 = self.buildFlowModMsg(pkt1, ingress_port, egress_port1) |
| 280 | |
| 281 | pkt2 = simple_tcp_packet(dl_src='0:7:7:7:7:7') |
| 282 | flow_mod_msg2 = self.buildFlowModMsg(pkt2, ingress_port, egress_port2) |
| 283 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 284 | fs_logger.info("Inserting flow1") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 285 | rv = self.controller.message_send(flow_mod_msg1) |
| 286 | self.assertTrue(rv != -1, "Error installing flow mod") |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 287 | fs_logger.info("Inserting flow2") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 288 | rv = self.controller.message_send(flow_mod_msg2) |
| 289 | self.assertTrue(rv != -1, "Error installing flow mod") |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 290 | do_barrier(self.controller) |
| 291 | |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 292 | num_pkt1s = random.randint(10,30) |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 293 | fs_logger.info("Sending " + str(num_pkt1s) + " pkt1s") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 294 | num_pkt2s = random.randint(10,30) |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 295 | fs_logger.info("Sending " + str(num_pkt2s) + " pkt2s") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 296 | for i in range(0,num_pkt1s): |
| 297 | self.sendPacket(pkt1, ingress_port, egress_port1, test_timeout) |
| 298 | for i in range(0,num_pkt2s): |
| 299 | self.sendPacket(pkt2, ingress_port, egress_port2, test_timeout) |
| 300 | |
| 301 | match1 = parse.packet_to_flow_match(pkt1) |
| 302 | self.verifyStats(match1, ofp.OFPP_NONE, test_timeout, num_pkt1s) |
| 303 | match2 = parse.packet_to_flow_match(pkt2) |
| 304 | self.verifyStats(match2, ofp.OFPP_NONE, test_timeout, num_pkt2s) |
| 305 | match1.wildcards |= ofp.OFPFW_DL_SRC |
| 306 | self.verifyStats(match1, ofp.OFPP_NONE, test_timeout, |
| 307 | num_pkt1s+num_pkt2s) |
| 308 | # TODO: sweep through the wildcards to verify matching? |
| 309 | |
| 310 | |
| 311 | class AggregateStats(basic.SimpleDataPlane): |
| 312 | """ |
| 313 | Verify aggregate flow stats are properly retrieved. |
| 314 | |
| 315 | Generate two packets |
| 316 | Generate and install two matching flows |
| 317 | Send an aggregate stats request |
| 318 | Verify that aggregate stats are correct |
| 319 | Also verify out_port filtering |
| 320 | """ |
| 321 | |
| 322 | def buildFlowModMsg(self, pkt, ingress_port, egress_port): |
| 323 | match = parse.packet_to_flow_match(pkt) |
| 324 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
| 325 | self.assertTrue(match is not None, |
| 326 | "Could not generate flow match from pkt") |
| 327 | match.in_port = ingress_port |
| 328 | |
| 329 | flow_mod_msg = message.flow_mod() |
| 330 | flow_mod_msg.match = match |
| 331 | flow_mod_msg.cookie = random.randint(0,9007199254740992) |
| 332 | flow_mod_msg.buffer_id = 0xffffffff |
| 333 | flow_mod_msg.idle_timeout = 0 |
| 334 | flow_mod_msg.hard_timeout = 0 |
| 335 | act = action.action_output() |
| 336 | act.port = egress_port |
| 337 | self.assertTrue(flow_mod_msg.actions.add(act), "Could not add action") |
| 338 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 339 | fs_logger.info("Ingress " + str(ingress_port) + |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 340 | " to egress " + str(egress_port)) |
| 341 | |
| 342 | return flow_mod_msg |
| 343 | |
| 344 | def sendPacket(self, pkt, ingress_port, egress_port, test_timeout): |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 345 | fs_logger.info("Sending packet to dp port " + |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 346 | str(ingress_port)) |
| 347 | self.dataplane.send(ingress_port, str(pkt)) |
Dan Talayco | aff26c8 | 2012-03-25 15:06:26 -0700 | [diff] [blame] | 348 | (rcv_port, rcv_pkt, pkt_time) = self.dataplane.poll(timeout= |
| 349 | test_timeout) |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 350 | self.assertTrue(rcv_pkt is not None, "Did not receive packet") |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 351 | fs_logger.debug("Packet len " + str(len(pkt)) + " in on " + |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 352 | str(rcv_port)) |
| 353 | self.assertEqual(rcv_port, egress_port, "Unexpected receive port") |
| 354 | for j in range(0,test_timeout): |
| 355 | if str(pkt) == str(rcv_pkt): |
| 356 | break |
| 357 | sleep(1) |
| 358 | self.assertTrue(j < test_timeout, |
| 359 | 'Timeout sending packets for flow stats test') |
| 360 | |
| 361 | def verifyAggFlowStats(self, match, out_port, test_timeout, |
| 362 | flow_count, packet_count): |
| 363 | stat_req = message.aggregate_stats_request() |
| 364 | stat_req.match = match |
| 365 | stat_req.table_id = 0xff |
| 366 | stat_req.out_port = out_port |
| 367 | |
| 368 | all_packets_received = 0 |
| 369 | for i in range(0,test_timeout): |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 370 | fs_logger.info("Sending stats request") |
| 371 | response, pkt = self.controller.transact(stat_req, |
| 372 | timeout=test_timeout) |
| 373 | self.assertTrue(response is not None, |
| 374 | "No response to stats request") |
Dan Talayco | aff26c8 | 2012-03-25 15:06:26 -0700 | [diff] [blame] | 375 | self.assertTrue(len(response.stats) == 1, |
| 376 | "Did not receive flow stats reply") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 377 | for obj in response.stats: |
| 378 | self.assertTrue(obj.flow_count == flow_count, |
| 379 | "Flow count " + str(obj.flow_count) + |
| 380 | " does not match expected " + str(flow_count)) |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 381 | fs_logger.info("Received " + str(obj.packet_count) + " packets") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 382 | if obj.packet_count == packet_count: |
| 383 | all_packets_received = 1 |
| 384 | |
| 385 | if all_packets_received: |
| 386 | break |
| 387 | sleep(1) |
| 388 | |
| 389 | self.assertTrue(all_packets_received, |
| 390 | "Packet count does not match number sent") |
| 391 | |
| 392 | def runTest(self): |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 393 | global fs_port_map |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 394 | |
| 395 | # TODO: set from command-line parameter |
| 396 | test_timeout = 60 |
| 397 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 398 | of_ports = fs_port_map.keys() |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 399 | of_ports.sort() |
| 400 | self.assertTrue(len(of_ports) >= 3, "Not enough ports for test") |
| 401 | ingress_port = of_ports[0]; |
| 402 | egress_port1 = of_ports[1]; |
| 403 | egress_port2 = of_ports[2]; |
| 404 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 405 | rc = delete_all_flows(self.controller, fs_logger) |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 406 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 407 | |
| 408 | pkt1 = simple_tcp_packet() |
| 409 | flow_mod_msg1 = self.buildFlowModMsg(pkt1, ingress_port, egress_port1) |
| 410 | |
| 411 | pkt2 = simple_tcp_packet(dl_src='0:7:7:7:7:7') |
| 412 | flow_mod_msg2 = self.buildFlowModMsg(pkt2, ingress_port, egress_port2) |
| 413 | |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 414 | fs_logger.info("Inserting flow1") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 415 | rv = self.controller.message_send(flow_mod_msg1) |
| 416 | self.assertTrue(rv != -1, "Error installing flow mod") |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 417 | fs_logger.info("Inserting flow2") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 418 | rv = self.controller.message_send(flow_mod_msg2) |
| 419 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 420 | do_barrier(self.controller) |
| 421 | |
| 422 | num_pkt1s = random.randint(10,30) |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 423 | fs_logger.info("Sending " + str(num_pkt1s) + " pkt1s") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 424 | num_pkt2s = random.randint(10,30) |
Ken Chiang | fb593e7 | 2012-03-28 17:19:13 -0700 | [diff] [blame] | 425 | fs_logger.info("Sending " + str(num_pkt2s) + " pkt2s") |
Ken Chiang | 620bdcc | 2012-03-23 12:52:07 -0700 | [diff] [blame] | 426 | for i in range(0,num_pkt1s): |
| 427 | self.sendPacket(pkt1, ingress_port, egress_port1, test_timeout) |
| 428 | for i in range(0,num_pkt2s): |
| 429 | self.sendPacket(pkt2, ingress_port, egress_port2, test_timeout) |
| 430 | |
| 431 | # loop on flow stats request until timeout |
| 432 | match = parse.packet_to_flow_match(pkt1) |
| 433 | match.wildcards |= ofp.OFPFW_DL_SRC |
| 434 | self.verifyAggFlowStats(match, ofp.OFPP_NONE, test_timeout, |
| 435 | 2, num_pkt1s+num_pkt2s) |
| 436 | |
| 437 | # out_port filter for egress_port1 |
| 438 | self.verifyAggFlowStats(match, egress_port1, test_timeout, |
| 439 | 1, num_pkt1s) |
| 440 | |
| 441 | # out_port filter for egress_port1 |
| 442 | self.verifyAggFlowStats(match, egress_port2, test_timeout, |
| 443 | 1, num_pkt2s) |