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