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