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