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