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