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