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