blob: de7b52a868917d25d4755dc42e1836b40bc3c327 [file] [log] [blame]
Dan Talaycoc901f4d2010-03-07 21:55:45 -08001
Dan Talaycod2ca1032010-03-10 14:40:26 -08002import sys
Dan Talayco92c99122010-06-03 13:53:18 -07003import copy
Dan Talaycod2ca1032010-03-10 14:40:26 -08004
5try:
6 import scapy.all as scapy
7except:
8 try:
9 import scapy as scapy
10 except:
11 sys.exit("Need to install scapy for packet parsing")
Dan Talayco41eae8b2010-03-10 13:57:06 -080012
Rich Lane477f4812012-10-04 22:49:00 -070013from oftest import config
Dan Talaycoc901f4d2010-03-07 21:55:45 -080014import oftest.controller as controller
15import oftest.cstruct as ofp
16import oftest.message as message
17import oftest.dataplane as dataplane
18import oftest.action as action
Dan Talayco41eae8b2010-03-10 13:57:06 -080019import oftest.parse as parse
Dan Talaycoc901f4d2010-03-07 21:55:45 -080020import logging
Dan Talayco4b2bee62010-07-20 14:10:05 -070021import types
Dan Talayco73f84012012-10-02 09:23:18 -070022import time
Dan Talaycoc901f4d2010-03-07 21:55:45 -080023
Dan Talaycoba3745c2010-07-21 21:51:08 -070024global skipped_test_count
25skipped_test_count = 0
26
Dan Talayco551befa2010-07-15 17:05:32 -070027# Some useful defines
28IP_ETHERTYPE = 0x800
29TCP_PROTOCOL = 0x6
30UDP_PROTOCOL = 0x11
31
Jeffrey Townsend5cb7ed32012-08-17 18:11:01 +000032MINSIZE = 0
33
Rich Lane9a003812012-10-04 17:17:59 -070034def delete_all_flows(ctrl):
Dan Talayco41eae8b2010-03-10 13:57:06 -080035 """
36 Delete all flows on the switch
37 @param ctrl The controller object for the test
Dan Talayco41eae8b2010-03-10 13:57:06 -080038 """
39
Rich Lane9a003812012-10-04 17:17:59 -070040 logging.info("Deleting all flows")
Dan Talaycoc901f4d2010-03-07 21:55:45 -080041 msg = message.flow_mod()
42 msg.match.wildcards = ofp.OFPFW_ALL
Dan Talayco41eae8b2010-03-10 13:57:06 -080043 msg.out_port = ofp.OFPP_NONE
Dan Talaycoc901f4d2010-03-07 21:55:45 -080044 msg.command = ofp.OFPFC_DELETE
45 msg.buffer_id = 0xffffffff
Dan Talayco41eae8b2010-03-10 13:57:06 -080046 return ctrl.message_send(msg)
47
Ed Swierk99a74de2012-08-22 06:40:54 -070048def required_wildcards(parent):
Rich Lane2014f9b2012-10-05 15:29:40 -070049 w = test_param_get('required_wildcards', default='default')
Ed Swierk99a74de2012-08-22 06:40:54 -070050 if w == 'l3-l4':
51 return (ofp.OFPFW_NW_SRC_ALL | ofp.OFPFW_NW_DST_ALL | ofp.OFPFW_NW_TOS
52 | ofp.OFPFW_NW_PROTO | ofp.OFPFW_TP_SRC | ofp.OFPFW_TP_DST)
53 else:
54 return 0
55
Dan Talayco41eae8b2010-03-10 13:57:06 -080056def simple_tcp_packet(pktlen=100,
57 dl_dst='00:01:02:03:04:05',
58 dl_src='00:06:07:08:09:0a',
Tatsuya Yabe460321e2010-05-25 17:50:49 -070059 dl_vlan_enable=False,
60 dl_vlan=0,
61 dl_vlan_pcp=0,
Dan Talayco551befa2010-07-15 17:05:32 -070062 dl_vlan_cfi=0,
Dan Talayco41eae8b2010-03-10 13:57:06 -080063 ip_src='192.168.0.1',
64 ip_dst='192.168.0.2',
Tatsuya Yabe460321e2010-05-25 17:50:49 -070065 ip_tos=0,
Dan Talayco41eae8b2010-03-10 13:57:06 -080066 tcp_sport=1234,
Christian Dickmann8b59b4b2012-09-23 16:48:30 -070067 tcp_dport=80,
68 ip_ihl=None,
69 ip_options=False
Dan Talayco41eae8b2010-03-10 13:57:06 -080070 ):
71 """
72 Return a simple dataplane TCP packet
73
74 Supports a few parameters:
75 @param len Length of packet in bytes w/o CRC
76 @param dl_dst Destinatino MAC
77 @param dl_src Source MAC
Tatsuya Yabe460321e2010-05-25 17:50:49 -070078 @param dl_vlan_enable True if the packet is with vlan, False otherwise
79 @param dl_vlan VLAN ID
80 @param dl_vlan_pcp VLAN priority
Dan Talayco41eae8b2010-03-10 13:57:06 -080081 @param ip_src IP source
82 @param ip_dst IP destination
Tatsuya Yabe460321e2010-05-25 17:50:49 -070083 @param ip_tos IP ToS
Dan Talayco41eae8b2010-03-10 13:57:06 -080084 @param tcp_dport TCP destination port
85 @param ip_sport TCP source port
86
87 Generates a simple TCP request. Users
88 shouldn't assume anything about this packet other than that
89 it is a valid ethernet/IP/TCP frame.
90 """
Jeffrey Townsend5cb7ed32012-08-17 18:11:01 +000091
92 if MINSIZE > pktlen:
93 pktlen = MINSIZE
94
Dan Talayco551befa2010-07-15 17:05:32 -070095 # Note Dot1Q.id is really CFI
Shudong Zhou43ee54c2012-12-13 15:52:37 -080096 if (dl_vlan_enable):
Tatsuya Yabe460321e2010-05-25 17:50:49 -070097 pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \
Dan Talayco551befa2010-07-15 17:05:32 -070098 scapy.Dot1Q(prio=dl_vlan_pcp, id=dl_vlan_cfi, vlan=dl_vlan)/ \
Christian Dickmann8b59b4b2012-09-23 16:48:30 -070099 scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl)/ \
Tatsuya Yabe460321e2010-05-25 17:50:49 -0700100 scapy.TCP(sport=tcp_sport, dport=tcp_dport)
101 else:
Christian Dickmann8b59b4b2012-09-23 16:48:30 -0700102 if not ip_options:
103 pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \
104 scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl)/ \
105 scapy.TCP(sport=tcp_sport, dport=tcp_dport)
106 else:
107 pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \
108 scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl, options=ip_options)/ \
109 scapy.TCP(sport=tcp_sport, dport=tcp_dport)
Tatsuya Yabe460321e2010-05-25 17:50:49 -0700110
Dan Talayco41eae8b2010-03-10 13:57:06 -0800111 pkt = pkt/("D" * (pktlen - len(pkt)))
112
113 return pkt
114
Rich Lane6ee7bea2012-10-26 16:19:29 -0700115def simple_udp_packet(pktlen=100,
116 dl_dst='00:01:02:03:04:05',
117 dl_src='00:06:07:08:09:0a',
118 dl_vlan_enable=False,
119 dl_vlan=0,
120 dl_vlan_pcp=0,
121 dl_vlan_cfi=0,
122 ip_src='192.168.0.1',
123 ip_dst='192.168.0.2',
124 ip_tos=0,
125 udp_sport=1234,
126 udp_dport=80,
127 ip_ihl=None,
128 ip_options=False
129 ):
130 """
131 Return a simple dataplane UDP packet
132
133 Supports a few parameters:
134 @param len Length of packet in bytes w/o CRC
135 @param dl_dst Destination MAC
136 @param dl_src Source MAC
137 @param dl_vlan_enable True if the packet is with vlan, False otherwise
138 @param dl_vlan VLAN ID
139 @param dl_vlan_pcp VLAN priority
140 @param ip_src IP source
141 @param ip_dst IP destination
142 @param ip_tos IP ToS
143 @param udp_dport UDP destination port
144 @param udp_sport UDP source port
145
146 Generates a simple UDP packet. Users shouldn't assume anything about
147 this packet other than that it is a valid ethernet/IP/UDP frame.
148 """
149
150 if MINSIZE > pktlen:
151 pktlen = MINSIZE
152
153 # Note Dot1Q.id is really CFI
154 if (dl_vlan_enable):
155 pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \
156 scapy.Dot1Q(prio=dl_vlan_pcp, id=dl_vlan_cfi, vlan=dl_vlan)/ \
157 scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl)/ \
158 scapy.UDP(sport=udp_sport, dport=udp_dport)
159 else:
160 if not ip_options:
161 pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \
162 scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl)/ \
163 scapy.UDP(sport=udp_sport, dport=udp_dport)
164 else:
165 pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \
166 scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl, options=ip_options)/ \
167 scapy.UDP(sport=udp_sport, dport=udp_dport)
168
169 pkt = pkt/("D" * (pktlen - len(pkt)))
170
171 return pkt
172
Tatsuya Yabeb8fb3c32010-06-14 15:48:36 -0700173def simple_icmp_packet(pktlen=60,
174 dl_dst='00:01:02:03:04:05',
175 dl_src='00:06:07:08:09:0a',
176 dl_vlan_enable=False,
177 dl_vlan=0,
178 dl_vlan_pcp=0,
179 ip_src='192.168.0.1',
180 ip_dst='192.168.0.2',
181 ip_tos=0,
182 icmp_type=8,
183 icmp_code=0
184 ):
185 """
186 Return a simple ICMP packet
187
188 Supports a few parameters:
189 @param len Length of packet in bytes w/o CRC
190 @param dl_dst Destinatino MAC
191 @param dl_src Source MAC
192 @param dl_vlan_enable True if the packet is with vlan, False otherwise
193 @param dl_vlan VLAN ID
194 @param dl_vlan_pcp VLAN priority
195 @param ip_src IP source
196 @param ip_dst IP destination
197 @param ip_tos IP ToS
198 @param icmp_type ICMP type
199 @param icmp_code ICMP code
200
201 Generates a simple ICMP ECHO REQUEST. Users
202 shouldn't assume anything about this packet other than that
203 it is a valid ethernet/ICMP frame.
204 """
Jeffrey Townsend5cb7ed32012-08-17 18:11:01 +0000205
206 if MINSIZE > pktlen:
207 pktlen = MINSIZE
208
Tatsuya Yabeb8fb3c32010-06-14 15:48:36 -0700209 if (dl_vlan_enable):
210 pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \
211 scapy.Dot1Q(prio=dl_vlan_pcp, id=0, vlan=dl_vlan)/ \
212 scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos)/ \
213 scapy.ICMP(type=icmp_type, code=icmp_code)
214 else:
215 pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \
216 scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos)/ \
217 scapy.ICMP(type=icmp_type, code=icmp_code)
218
219 pkt = pkt/("0" * (pktlen - len(pkt)))
220
221 return pkt
222
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700223def simple_eth_packet(pktlen=60,
224 dl_dst='00:01:02:03:04:05',
225 dl_src='01:80:c2:00:00:00',
226 dl_type=0x88cc):
Jeffrey Townsend5cb7ed32012-08-17 18:11:01 +0000227
228 if MINSIZE > pktlen:
229 pktlen = MINSIZE
230
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700231 pkt = scapy.Ether(dst=dl_dst, src=dl_src, type=dl_type)
232
233 pkt = pkt/("0" * (pktlen - len(pkt)))
234
235 return pkt
236
Shudong Zhou43ee54c2012-12-13 15:52:37 -0800237def qinq_tcp_packet(pktlen=100,
238 dl_dst='00:01:02:03:04:05',
239 dl_src='00:06:07:08:09:0a',
240 dl_vlan_outer=20,
241 dl_vlan_pcp_outer=0,
242 dl_vlan_cfi_outer=0,
243 dl_vlan=10,
244 dl_vlan_pcp=0,
245 dl_vlan_cfi=0,
246 ip_src='192.168.0.1',
247 ip_dst='192.168.0.2',
248 ip_tos=0,
249 tcp_sport=1234,
250 tcp_dport=80,
251 ip_ihl=None,
252 ip_options=False
253 ):
254 """
255 Return a doubly tagged dataplane TCP packet
256
257 Supports a few parameters:
258 @param len Length of packet in bytes w/o CRC
259 @param dl_dst Destinatino MAC
260 @param dl_src Source MAC
261 @param dl_vlan_outer Outer VLAN ID
262 @param dl_vlan_pcp_outer Outer VLAN priority
263 @param dl_vlan_cfi_outer Outer VLAN cfi bit
264 @param dl_vlan Inner VLAN ID
265 @param dl_vlan_pcp VLAN priority
266 @param dl_vlan_cfi VLAN cfi bit
267 @param ip_src IP source
268 @param ip_dst IP destination
269 @param ip_tos IP ToS
270 @param tcp_dport TCP destination port
271 @param ip_sport TCP source port
272
273 Generates a TCP request. Users
274 shouldn't assume anything about this packet other than that
275 it is a valid ethernet/IP/TCP frame.
276 """
277
278 if MINSIZE > pktlen:
279 pktlen = MINSIZE
280
281 # Note Dot1Q.id is really CFI
282 pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \
283 scapy.Dot1Q(prio=dl_vlan_pcp_outer, id=dl_vlan_cfi_outer, vlan=dl_vlan_outer)/ \
284 scapy.Dot1Q(prio=dl_vlan_pcp, id=dl_vlan_cfi, vlan=dl_vlan)/ \
285 scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl)/ \
286 scapy.TCP(sport=tcp_sport, dport=tcp_dport)
287
288 pkt = pkt/("D" * (pktlen - len(pkt)))
289
290 return pkt
291
Shudong Zhoub7f12462012-11-20 13:01:12 -0800292def do_barrier(ctrl, timeout=-1):
Dan Talayco0fc08bd2012-04-09 16:56:18 -0700293 """
294 Do a barrier command
295 Return 0 on success, -1 on error
296 """
Dan Talayco41eae8b2010-03-10 13:57:06 -0800297 b = message.barrier_request()
Shudong Zhoub7f12462012-11-20 13:01:12 -0800298 (resp, pkt) = ctrl.transact(b, timeout=timeout)
Dan Talaycof6b94832012-04-12 21:50:57 -0700299 # We'll trust the transaction processing in the controller that xid matched
Dan Talayco0fc08bd2012-04-09 16:56:18 -0700300 if not resp:
301 return -1
302 return 0
Dan Talayco92c99122010-06-03 13:53:18 -0700303
Rich Lane9a003812012-10-04 17:17:59 -0700304def port_config_get(controller, port_no):
Dan Talayco92c99122010-06-03 13:53:18 -0700305 """
306 Get a port's configuration
307
308 Gets the switch feature configuration and grabs one port's
309 configuration
310
311 @returns (hwaddr, config, advert) The hwaddress, configuration and
312 advertised values
313 """
314 request = message.features_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700315 reply, pkt = controller.transact(request)
Rich Lane9a003812012-10-04 17:17:59 -0700316 logging.debug(reply.show())
Dan Talayco92c99122010-06-03 13:53:18 -0700317 if reply is None:
Rich Lane9a003812012-10-04 17:17:59 -0700318 logging.warn("Get feature request failed")
Dan Talayco92c99122010-06-03 13:53:18 -0700319 return None, None, None
320 for idx in range(len(reply.ports)):
321 if reply.ports[idx].port_no == port_no:
322 return (reply.ports[idx].hw_addr, reply.ports[idx].config,
323 reply.ports[idx].advertised)
324
Rich Lane9a003812012-10-04 17:17:59 -0700325 logging.warn("Did not find port number for port config")
Dan Talayco92c99122010-06-03 13:53:18 -0700326 return None, None, None
327
Rich Lane9a003812012-10-04 17:17:59 -0700328def port_config_set(controller, port_no, config, mask):
Dan Talayco92c99122010-06-03 13:53:18 -0700329 """
330 Set the port configuration according the given parameters
331
332 Gets the switch feature configuration and updates one port's
333 configuration value according to config and mask
334 """
Rich Lane9a003812012-10-04 17:17:59 -0700335 logging.info("Setting port " + str(port_no) + " to config " + str(config))
Dan Talayco92c99122010-06-03 13:53:18 -0700336 request = message.features_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700337 reply, pkt = controller.transact(request)
Dan Talayco92c99122010-06-03 13:53:18 -0700338 if reply is None:
339 return -1
Rich Lane9a003812012-10-04 17:17:59 -0700340 logging.debug(reply.show())
Dan Talayco92c99122010-06-03 13:53:18 -0700341 for idx in range(len(reply.ports)):
342 if reply.ports[idx].port_no == port_no:
343 break
344 if idx >= len(reply.ports):
345 return -1
346 mod = message.port_mod()
347 mod.port_no = port_no
348 mod.hw_addr = reply.ports[idx].hw_addr
349 mod.config = config
350 mod.mask = mask
351 mod.advertise = reply.ports[idx].advertised
352 rv = controller.message_send(mod)
353 return rv
354
Rich Lane2014f9b2012-10-05 15:29:40 -0700355def receive_pkt_check(dp, pkt, yes_ports, no_ports, assert_if):
Dan Talayco92c99122010-06-03 13:53:18 -0700356 """
357 Check for proper receive packets across all ports
Ken Chiang1bf01602012-04-04 10:48:23 -0700358 @param dp The dataplane object
Dan Talayco92c99122010-06-03 13:53:18 -0700359 @param pkt Expected packet; may be None if yes_ports is empty
360 @param yes_ports Set or list of ports that should recieve packet
361 @param no_ports Set or list of ports that should not receive packet
362 @param assert_if Object that implements assertXXX
363 """
Rich Lane91765672012-12-06 16:33:04 -0800364
365 # Wait this long for packets that we don't expect to receive.
366 # 100ms is (rarely) too short for positive tests on slow
367 # switches but is definitely not too short for a negative test.
368 negative_timeout = 0.1
369
Dan Talaycocf26b7a2011-08-05 10:15:35 -0700370 exp_pkt_arg = None
Rich Lane2014f9b2012-10-05 15:29:40 -0700371 if config["relax"]:
Dan Talaycocf26b7a2011-08-05 10:15:35 -0700372 exp_pkt_arg = pkt
373
Dan Talayco92c99122010-06-03 13:53:18 -0700374 for ofport in yes_ports:
Rich Lane9a003812012-10-04 17:17:59 -0700375 logging.debug("Checking for pkt on port " + str(ofport))
Ken Chiang1bf01602012-04-04 10:48:23 -0700376 (rcv_port, rcv_pkt, pkt_time) = dp.poll(
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700377 port_number=ofport, exp_pkt=exp_pkt_arg)
Dan Talayco92c99122010-06-03 13:53:18 -0700378 assert_if.assertTrue(rcv_pkt is not None,
379 "Did not receive pkt on " + str(ofport))
Ken Chiang1bf01602012-04-04 10:48:23 -0700380 if not dataplane.match_exp_pkt(pkt, rcv_pkt):
Rich Lane9a003812012-10-04 17:17:59 -0700381 logging.debug("Sent %s" % format_packet(pkt))
382 logging.debug("Resp %s" % format_packet(rcv_pkt))
Ken Chiang1bf01602012-04-04 10:48:23 -0700383 assert_if.assertTrue(dataplane.match_exp_pkt(pkt, rcv_pkt),
384 "Response packet does not match send packet " +
385 "on port " + str(ofport))
Dan Talayco73f84012012-10-02 09:23:18 -0700386 if len(no_ports) > 0:
Rich Lane91765672012-12-06 16:33:04 -0800387 time.sleep(negative_timeout)
Dan Talayco92c99122010-06-03 13:53:18 -0700388 for ofport in no_ports:
Rich Lane9a003812012-10-04 17:17:59 -0700389 logging.debug("Negative check for pkt on port " + str(ofport))
Ken Chiang1bf01602012-04-04 10:48:23 -0700390 (rcv_port, rcv_pkt, pkt_time) = dp.poll(
Rich Lane91765672012-12-06 16:33:04 -0800391 port_number=ofport, timeout=0, exp_pkt=exp_pkt_arg)
Dan Talayco92c99122010-06-03 13:53:18 -0700392 assert_if.assertTrue(rcv_pkt is None,
393 "Unexpected pkt on port " + str(ofport))
Dan Talayco551befa2010-07-15 17:05:32 -0700394
395
Dan Talaycoc948d0b2012-03-23 12:17:54 -0700396def receive_pkt_verify(parent, egr_ports, exp_pkt, ing_port):
Dan Talayco551befa2010-07-15 17:05:32 -0700397 """
398 Receive a packet and verify it matches an expected value
Dan Talaycof6e76c02012-03-23 10:56:12 -0700399 @param egr_port A single port or list of ports
Dan Talayco551befa2010-07-15 17:05:32 -0700400
401 parent must implement dataplane, assertTrue and assertEqual
402 """
Dan Talaycocf26b7a2011-08-05 10:15:35 -0700403 exp_pkt_arg = None
Rich Lane477f4812012-10-04 22:49:00 -0700404 if config["relax"]:
Dan Talaycocf26b7a2011-08-05 10:15:35 -0700405 exp_pkt_arg = exp_pkt
406
Dan Talaycof6e76c02012-03-23 10:56:12 -0700407 if type(egr_ports) == type([]):
408 egr_port_list = egr_ports
409 else:
410 egr_port_list = [egr_ports]
Dan Talaycocf26b7a2011-08-05 10:15:35 -0700411
Dan Talaycof6e76c02012-03-23 10:56:12 -0700412 # Expect a packet from each port on egr port list
413 for egr_port in egr_port_list:
Dan Talaycod8ae7582012-03-23 12:24:56 -0700414 check_port = egr_port
Dan Talaycoc948d0b2012-03-23 12:17:54 -0700415 if egr_port == ofp.OFPP_IN_PORT:
Dan Talaycod8ae7582012-03-23 12:24:56 -0700416 check_port = ing_port
Dan Talaycof6e76c02012-03-23 10:56:12 -0700417 (rcv_port, rcv_pkt, pkt_time) = parent.dataplane.poll(
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700418 port_number=check_port, exp_pkt=exp_pkt_arg)
Dan Talayco551befa2010-07-15 17:05:32 -0700419
Dan Talaycof6e76c02012-03-23 10:56:12 -0700420 if rcv_pkt is None:
Rich Lane9a003812012-10-04 17:17:59 -0700421 logging.error("ERROR: No packet received from " +
Dan Talaycod8ae7582012-03-23 12:24:56 -0700422 str(check_port))
Dan Talayco551befa2010-07-15 17:05:32 -0700423
Dan Talaycof6e76c02012-03-23 10:56:12 -0700424 parent.assertTrue(rcv_pkt is not None,
Dan Talaycod8ae7582012-03-23 12:24:56 -0700425 "Did not receive packet port " + str(check_port))
Rich Lane9a003812012-10-04 17:17:59 -0700426 logging.debug("Packet len " + str(len(rcv_pkt)) + " in on " +
Dan Talaycof6e76c02012-03-23 10:56:12 -0700427 str(rcv_port))
428
429 if str(exp_pkt) != str(rcv_pkt):
Rich Lane9a003812012-10-04 17:17:59 -0700430 logging.error("ERROR: Packet match failed.")
431 logging.debug("Expected len " + str(len(exp_pkt)) + ": "
Dan Talaycof6e76c02012-03-23 10:56:12 -0700432 + str(exp_pkt).encode('hex'))
Rich Lane9a003812012-10-04 17:17:59 -0700433 logging.debug("Received len " + str(len(rcv_pkt)) + ": "
Dan Talaycof6e76c02012-03-23 10:56:12 -0700434 + str(rcv_pkt).encode('hex'))
Rich Lane5d7e89a2012-10-26 16:43:13 -0700435 logging.debug("Expected packet: " + inspect_packet(scapy.Ether(str(exp_pkt))))
436 logging.debug("Received packet: " + inspect_packet(scapy.Ether(str(rcv_pkt))))
Dan Talaycof6e76c02012-03-23 10:56:12 -0700437 parent.assertEqual(str(exp_pkt), str(rcv_pkt),
Dan Talaycod8ae7582012-03-23 12:24:56 -0700438 "Packet match error on port " + str(check_port))
Dan Talaycof6e76c02012-03-23 10:56:12 -0700439
Dan Talayco551befa2010-07-15 17:05:32 -0700440def match_verify(parent, req_match, res_match):
441 """
442 Verify flow matches agree; if they disagree, report where
443
444 parent must implement assertEqual
445 Use str() to ensure content is compared and not pointers
446 """
447
448 parent.assertEqual(req_match.wildcards, res_match.wildcards,
449 'Match failed: wildcards: ' + hex(req_match.wildcards) +
450 " != " + hex(res_match.wildcards))
451 parent.assertEqual(req_match.in_port, res_match.in_port,
452 'Match failed: in_port: ' + str(req_match.in_port) +
453 " != " + str(res_match.in_port))
454 parent.assertEqual(str(req_match.dl_src), str(res_match.dl_src),
455 'Match failed: dl_src: ' + str(req_match.dl_src) +
456 " != " + str(res_match.dl_src))
457 parent.assertEqual(str(req_match.dl_dst), str(res_match.dl_dst),
458 'Match failed: dl_dst: ' + str(req_match.dl_dst) +
459 " != " + str(res_match.dl_dst))
460 parent.assertEqual(req_match.dl_vlan, res_match.dl_vlan,
461 'Match failed: dl_vlan: ' + str(req_match.dl_vlan) +
462 " != " + str(res_match.dl_vlan))
463 parent.assertEqual(req_match.dl_vlan_pcp, res_match.dl_vlan_pcp,
464 'Match failed: dl_vlan_pcp: ' +
465 str(req_match.dl_vlan_pcp) + " != " +
466 str(res_match.dl_vlan_pcp))
467 parent.assertEqual(req_match.dl_type, res_match.dl_type,
468 'Match failed: dl_type: ' + str(req_match.dl_type) +
469 " != " + str(res_match.dl_type))
470
471 if (not(req_match.wildcards & ofp.OFPFW_DL_TYPE)
472 and (req_match.dl_type == IP_ETHERTYPE)):
473 parent.assertEqual(req_match.nw_tos, res_match.nw_tos,
474 'Match failed: nw_tos: ' + str(req_match.nw_tos) +
475 " != " + str(res_match.nw_tos))
476 parent.assertEqual(req_match.nw_proto, res_match.nw_proto,
477 'Match failed: nw_proto: ' + str(req_match.nw_proto) +
478 " != " + str(res_match.nw_proto))
479 parent.assertEqual(req_match.nw_src, res_match.nw_src,
480 'Match failed: nw_src: ' + str(req_match.nw_src) +
481 " != " + str(res_match.nw_src))
482 parent.assertEqual(req_match.nw_dst, res_match.nw_dst,
483 'Match failed: nw_dst: ' + str(req_match.nw_dst) +
484 " != " + str(res_match.nw_dst))
485
486 if (not(req_match.wildcards & ofp.OFPFW_NW_PROTO)
487 and ((req_match.nw_proto == TCP_PROTOCOL)
488 or (req_match.nw_proto == UDP_PROTOCOL))):
489 parent.assertEqual(req_match.tp_src, res_match.tp_src,
490 'Match failed: tp_src: ' +
491 str(req_match.tp_src) +
492 " != " + str(res_match.tp_src))
493 parent.assertEqual(req_match.tp_dst, res_match.tp_dst,
494 'Match failed: tp_dst: ' +
495 str(req_match.tp_dst) +
496 " != " + str(res_match.tp_dst))
497
Ed Swierk99a74de2012-08-22 06:40:54 -0700498def packet_to_flow_match(parent, packet):
499 match = parse.packet_to_flow_match(packet)
500 match.wildcards |= required_wildcards(parent)
501 return match
502
503def flow_msg_create(parent, pkt, ing_port=None, action_list=None, wildcards=None,
Dan Talaycof6e76c02012-03-23 10:56:12 -0700504 egr_ports=None, egr_queue=None, check_expire=False, in_band=False):
Dan Talayco551befa2010-07-15 17:05:32 -0700505 """
506 Create a flow message
507
508 Match on packet with given wildcards.
509 See flow_match_test for other parameter descriptoins
510 @param egr_queue if not None, make the output an enqueue action
Dan Talayco677c0b72011-08-23 22:53:38 -0700511 @param in_band if True, do not wildcard ingress port
Dan Talaycof6e76c02012-03-23 10:56:12 -0700512 @param egr_ports None (drop), single port or list of ports
Dan Talayco551befa2010-07-15 17:05:32 -0700513 """
514 match = parse.packet_to_flow_match(pkt)
515 parent.assertTrue(match is not None, "Flow match from pkt failed")
Ed Swierk99a74de2012-08-22 06:40:54 -0700516 if wildcards is None:
517 wildcards = required_wildcards(parent)
Dan Talayco677c0b72011-08-23 22:53:38 -0700518 if in_band:
519 wildcards &= ~ofp.OFPFW_IN_PORT
Dan Talayco551befa2010-07-15 17:05:32 -0700520 match.wildcards = wildcards
521 match.in_port = ing_port
522
Dan Talaycof6e76c02012-03-23 10:56:12 -0700523 if type(egr_ports) == type([]):
524 egr_port_list = egr_ports
525 else:
526 egr_port_list = [egr_ports]
527
Dan Talayco551befa2010-07-15 17:05:32 -0700528 request = message.flow_mod()
529 request.match = match
530 request.buffer_id = 0xffffffff
531 if check_expire:
532 request.flags |= ofp.OFPFF_SEND_FLOW_REM
533 request.hard_timeout = 1
534
535 if action_list is not None:
536 for act in action_list:
Rich Lane9a003812012-10-04 17:17:59 -0700537 logging.debug("Adding action " + act.show())
Rich Lanee30455b2013-01-03 16:24:44 -0800538 request.actions.add(act)
Dan Talayco551befa2010-07-15 17:05:32 -0700539
540 # Set up output/enqueue action if directed
541 if egr_queue is not None:
Dan Talaycof6e76c02012-03-23 10:56:12 -0700542 parent.assertTrue(egr_ports is not None, "Egress port not set")
Dan Talayco551befa2010-07-15 17:05:32 -0700543 act = action.action_enqueue()
Dan Talaycof6e76c02012-03-23 10:56:12 -0700544 for egr_port in egr_port_list:
545 act.port = egr_port
546 act.queue_id = egr_queue
Rich Lanee30455b2013-01-03 16:24:44 -0800547 request.actions.add(act)
Dan Talaycof6e76c02012-03-23 10:56:12 -0700548 elif egr_ports is not None:
549 for egr_port in egr_port_list:
550 act = action.action_output()
551 act.port = egr_port
Rich Lanee30455b2013-01-03 16:24:44 -0800552 request.actions.add(act)
Dan Talayco551befa2010-07-15 17:05:32 -0700553
Rich Lane9a003812012-10-04 17:17:59 -0700554 logging.debug(request.show())
Dan Talayco551befa2010-07-15 17:05:32 -0700555
556 return request
557
Jeffrey Townsendf3eae9c2012-03-28 18:23:21 -0700558def flow_msg_install(parent, request, clear_table_override=None):
Dan Talayco551befa2010-07-15 17:05:32 -0700559 """
560 Install a flow mod message in the switch
561
562 @param parent Must implement controller, assertEqual, assertTrue
563 @param request The request, all set to go
564 @param clear_table If true, clear the flow table before installing
565 """
Dan Talayco8a64e332012-03-28 14:53:20 -0700566
Rich Lane2014f9b2012-10-05 15:29:40 -0700567 clear_table = test_param_get('clear_table', default=True)
Jeffrey Townsendf3eae9c2012-03-28 18:23:21 -0700568 if(clear_table_override != None):
569 clear_table = clear_table_override
570
571 if clear_table:
Rich Lane9a003812012-10-04 17:17:59 -0700572 logging.debug("Clear flow table")
573 rc = delete_all_flows(parent.controller)
Dan Talayco551befa2010-07-15 17:05:32 -0700574 parent.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talayco551befa2010-07-15 17:05:32 -0700575
Rich Lane9a003812012-10-04 17:17:59 -0700576 logging.debug("Insert flow")
Dan Talayco551befa2010-07-15 17:05:32 -0700577 rv = parent.controller.message_send(request)
578 parent.assertTrue(rv != -1, "Error installing flow mod")
Rich Lanee912d032012-12-22 14:28:33 -0800579
Dan Talayco0fc08bd2012-04-09 16:56:18 -0700580 parent.assertEqual(do_barrier(parent.controller), 0, "Barrier failed")
Dan Talayco551befa2010-07-15 17:05:32 -0700581
Ed Swierk99a74de2012-08-22 06:40:54 -0700582def flow_match_test_port_pair(parent, ing_port, egr_ports, wildcards=None,
Dan Talayco551befa2010-07-15 17:05:32 -0700583 dl_vlan=-1, pkt=None, exp_pkt=None,
Rich Lanee5779d32012-10-05 17:56:04 -0700584 action_list=None):
Dan Talayco551befa2010-07-15 17:05:32 -0700585 """
586 Flow match test on single TCP packet
Dan Talaycof6e76c02012-03-23 10:56:12 -0700587 @param egr_ports A single port or list of ports
Dan Talayco551befa2010-07-15 17:05:32 -0700588
589 Run test with packet through switch from ing_port to egr_port
590 See flow_match_test for parameter descriptions
591 """
592
Ed Swierk99a74de2012-08-22 06:40:54 -0700593 if wildcards is None:
594 wildcards = required_wildcards(parent)
Rich Lane9a003812012-10-04 17:17:59 -0700595 logging.info("Pkt match test: " + str(ing_port) + " to " +
Dan Talaycof6e76c02012-03-23 10:56:12 -0700596 str(egr_ports))
Rich Lanee5779d32012-10-05 17:56:04 -0700597 logging.debug(" WC: " + hex(wildcards) + " vlan: " + str(dl_vlan))
Dan Talayco551befa2010-07-15 17:05:32 -0700598 if pkt is None:
599 pkt = simple_tcp_packet(dl_vlan_enable=(dl_vlan >= 0), dl_vlan=dl_vlan)
600
601 request = flow_msg_create(parent, pkt, ing_port=ing_port,
Dan Talaycof6e76c02012-03-23 10:56:12 -0700602 wildcards=wildcards, egr_ports=egr_ports,
Dan Talayco551befa2010-07-15 17:05:32 -0700603 action_list=action_list)
604
605 flow_msg_install(parent, request)
606
Rich Lane9a003812012-10-04 17:17:59 -0700607 logging.debug("Send packet: " + str(ing_port) + " to " +
Dan Talaycof6e76c02012-03-23 10:56:12 -0700608 str(egr_ports))
Dan Talayco551befa2010-07-15 17:05:32 -0700609 parent.dataplane.send(ing_port, str(pkt))
610
611 if exp_pkt is None:
612 exp_pkt = pkt
Dan Talaycoc948d0b2012-03-23 12:17:54 -0700613 receive_pkt_verify(parent, egr_ports, exp_pkt, ing_port)
Dan Talayco551befa2010-07-15 17:05:32 -0700614
Rich Lane89725bb2012-12-03 16:23:27 -0800615def flow_match_test_pktout(parent, ing_port, egr_ports,
616 dl_vlan=-1, pkt=None, exp_pkt=None,
617 action_list=None):
618 """
619 Packet-out test on single TCP packet
620 @param egr_ports A single port or list of ports
621
622 Run test sending packet-out to egr_ports. The goal is to test the actions
623 taken on the packet, not the matching which is of course irrelevant.
624 See flow_match_test for parameter descriptions
625 """
626
627 if pkt is None:
628 pkt = simple_tcp_packet(dl_vlan_enable=(dl_vlan >= 0), dl_vlan=dl_vlan)
629
630 msg = message.packet_out()
631 msg.in_port = ing_port
632 msg.data = str(pkt)
633 if action_list is not None:
634 for act in action_list:
Rich Lanee30455b2013-01-03 16:24:44 -0800635 msg.actions.add(act)
Rich Lane89725bb2012-12-03 16:23:27 -0800636
637 # Set up output action
638 if egr_ports is not None:
639 for egr_port in egr_ports:
640 act = action.action_output()
641 act.port = egr_port
Rich Lanee30455b2013-01-03 16:24:44 -0800642 msg.actions.add(act)
Rich Lane89725bb2012-12-03 16:23:27 -0800643
644 logging.debug(msg.show())
645 rv = parent.controller.message_send(msg)
646 parent.assertTrue(rv == 0, "Error sending out message")
647
648 if exp_pkt is None:
649 exp_pkt = pkt
650 receive_pkt_verify(parent, egr_ports, exp_pkt, ing_port)
651
Dan Talaycof6e76c02012-03-23 10:56:12 -0700652def get_egr_list(parent, of_ports, how_many, exclude_list=[]):
653 """
654 Generate a list of ports avoiding those in the exclude list
Rich Lane9a003812012-10-04 17:17:59 -0700655 @param parent Supplies logging
Dan Talaycof6e76c02012-03-23 10:56:12 -0700656 @param of_ports List of OF port numbers
657 @param how_many Number of ports to be added to the list
658 @param exclude_list List of ports not to be used
659 @returns An empty list if unable to find enough ports
660 """
661
Dan Talaycoc948d0b2012-03-23 12:17:54 -0700662 if how_many == 0:
663 return []
664
Dan Talaycof6e76c02012-03-23 10:56:12 -0700665 count = 0
666 egr_ports = []
667 for egr_idx in range(len(of_ports)):
668 if of_ports[egr_idx] not in exclude_list:
669 egr_ports.append(of_ports[egr_idx])
670 count += 1
671 if count >= how_many:
672 return egr_ports
Rich Lane9a003812012-10-04 17:17:59 -0700673 logging.debug("Could not generate enough egress ports for test")
Dan Talaycof6e76c02012-03-23 10:56:12 -0700674 return []
675
Ed Swierk99a74de2012-08-22 06:40:54 -0700676def flow_match_test(parent, port_map, wildcards=None, dl_vlan=-1, pkt=None,
Rich Lanee5779d32012-10-05 17:56:04 -0700677 exp_pkt=None, action_list=None,
Dan Talaycoc948d0b2012-03-23 12:17:54 -0700678 max_test=0, egr_count=1, ing_port=False):
Dan Talayco551befa2010-07-15 17:05:32 -0700679 """
Rich Lane89725bb2012-12-03 16:23:27 -0800680 Run flow_match_test_port_pair on all port pairs and packet-out
Dan Talayco551befa2010-07-15 17:05:32 -0700681
682 @param max_test If > 0 no more than this number of tests are executed.
683 @param parent Must implement controller, dataplane, assertTrue, assertEqual
Rich Lane9a003812012-10-04 17:17:59 -0700684 and logging
Dan Talayco551befa2010-07-15 17:05:32 -0700685 @param pkt If not None, use this packet for ingress
686 @param wildcards For flow match entry
Dan Talayco79184222010-11-01 12:24:29 -0700687 @param dl_vlan If not -1, and pkt is None, create a pkt w/ VLAN tag
Dan Talayco551befa2010-07-15 17:05:32 -0700688 @param exp_pkt If not None, use this as the expected output pkt; els use pkt
689 @param action_list Additional actions to add to flow mod
Dan Talaycocfa172f2012-03-23 12:03:00 -0700690 @param egr_count Number of egress ports; -1 means get from config w/ dflt 2
Dan Talayco551befa2010-07-15 17:05:32 -0700691 """
Ed Swierk99a74de2012-08-22 06:40:54 -0700692 if wildcards is None:
693 wildcards = required_wildcards(parent)
Dan Talayco551befa2010-07-15 17:05:32 -0700694 of_ports = port_map.keys()
695 of_ports.sort()
696 parent.assertTrue(len(of_ports) > 1, "Not enough ports for test")
697 test_count = 0
698
Dan Talaycocfa172f2012-03-23 12:03:00 -0700699 if egr_count == -1:
Rich Lane2014f9b2012-10-05 15:29:40 -0700700 egr_count = test_param_get('egr_count', default=2)
Dan Talaycocfa172f2012-03-23 12:03:00 -0700701
Dan Talayco551befa2010-07-15 17:05:32 -0700702 for ing_idx in range(len(of_ports)):
703 ingress_port = of_ports[ing_idx]
Dan Talaycof6e76c02012-03-23 10:56:12 -0700704 egr_ports = get_egr_list(parent, of_ports, egr_count,
705 exclude_list=[ingress_port])
Dan Talaycoc948d0b2012-03-23 12:17:54 -0700706 if ing_port:
707 egr_ports.append(ofp.OFPP_IN_PORT)
Dan Talaycof6e76c02012-03-23 10:56:12 -0700708 if len(egr_ports) == 0:
709 parent.assertTrue(0, "Failed to generate egress port list")
710
711 flow_match_test_port_pair(parent, ingress_port, egr_ports,
712 wildcards=wildcards, dl_vlan=dl_vlan,
713 pkt=pkt, exp_pkt=exp_pkt,
Rich Lanee5779d32012-10-05 17:56:04 -0700714 action_list=action_list)
Dan Talaycof6e76c02012-03-23 10:56:12 -0700715 test_count += 1
716 if (max_test > 0) and (test_count > max_test):
Rich Lane9a003812012-10-04 17:17:59 -0700717 logging.info("Ran " + str(test_count) + " tests; exiting")
Rich Lane89725bb2012-12-03 16:23:27 -0800718 break
719
720
721 ingress_port = of_ports[0]
722 egr_ports = get_egr_list(parent, of_ports, egr_count,
723 exclude_list=[ingress_port])
724 if ing_port:
725 egr_ports.append(ofp.OFPP_IN_PORT)
726 flow_match_test_pktout(parent, ingress_port, egr_ports,
727 dl_vlan=dl_vlan,
728 pkt=pkt, exp_pkt=exp_pkt,
729 action_list=action_list)
Dan Talayco551befa2010-07-15 17:05:32 -0700730
Rich Lane2014f9b2012-10-05 15:29:40 -0700731def test_param_get(key, default=None):
Dan Talayco4b2bee62010-07-20 14:10:05 -0700732 """
733 Return value passed via test-params if present
734
Dan Talayco4b2bee62010-07-20 14:10:05 -0700735 @param key The lookup key
736 @param default Default value to use if not found
737
738 If the pair 'key=val' appeared in the string passed to --test-params
739 on the command line, return val (as interpreted by exec). Otherwise
740 return default value.
Dan Talaycof6e76c02012-03-23 10:56:12 -0700741
742 WARNING: TEST PARAMETERS MUST BE PYTHON IDENTIFIERS;
743 eg egr_count, not egr-count.
Dan Talayco4b2bee62010-07-20 14:10:05 -0700744 """
745 try:
746 exec config["test_params"]
747 except:
748 return default
749
750 s = "val = " + str(key)
751 try:
752 exec s
753 return val
754 except:
755 return default
756
757def action_generate(parent, field_to_mod, mod_field_vals):
758 """
759 Create an action to modify the field indicated in field_to_mod
760
761 @param parent Must implement, assertTrue
762 @param field_to_mod The field to modify as a string name
763 @param mod_field_vals Hash of values to use for modified values
764 """
765
766 act = None
767
768 if field_to_mod in ['pktlen']:
769 return None
770
771 if field_to_mod == 'dl_dst':
772 act = action.action_set_dl_dst()
773 act.dl_addr = parse.parse_mac(mod_field_vals['dl_dst'])
774 elif field_to_mod == 'dl_src':
775 act = action.action_set_dl_src()
776 act.dl_addr = parse.parse_mac(mod_field_vals['dl_src'])
777 elif field_to_mod == 'dl_vlan_enable':
778 if not mod_field_vals['dl_vlan_enable']: # Strip VLAN tag
779 act = action.action_strip_vlan()
780 # Add VLAN tag is handled by dl_vlan field
781 # Will return None in this case
782 elif field_to_mod == 'dl_vlan':
783 act = action.action_set_vlan_vid()
784 act.vlan_vid = mod_field_vals['dl_vlan']
785 elif field_to_mod == 'dl_vlan_pcp':
786 act = action.action_set_vlan_pcp()
787 act.vlan_pcp = mod_field_vals['dl_vlan_pcp']
788 elif field_to_mod == 'ip_src':
789 act = action.action_set_nw_src()
790 act.nw_addr = parse.parse_ip(mod_field_vals['ip_src'])
791 elif field_to_mod == 'ip_dst':
792 act = action.action_set_nw_dst()
793 act.nw_addr = parse.parse_ip(mod_field_vals['ip_dst'])
794 elif field_to_mod == 'ip_tos':
795 act = action.action_set_nw_tos()
796 act.nw_tos = mod_field_vals['ip_tos']
797 elif field_to_mod == 'tcp_sport':
798 act = action.action_set_tp_src()
799 act.tp_port = mod_field_vals['tcp_sport']
800 elif field_to_mod == 'tcp_dport':
801 act = action.action_set_tp_dst()
802 act.tp_port = mod_field_vals['tcp_dport']
Rich Lane110e0e32012-10-26 16:21:46 -0700803 elif field_to_mod == 'udp_sport':
804 act = action.action_set_tp_src()
805 act.tp_port = mod_field_vals['udp_sport']
806 elif field_to_mod == 'udp_dport':
807 act = action.action_set_tp_dst()
808 act.tp_port = mod_field_vals['udp_dport']
Dan Talayco4b2bee62010-07-20 14:10:05 -0700809 else:
810 parent.assertTrue(0, "Unknown field to modify: " + str(field_to_mod))
811
812 return act
813
814def pkt_action_setup(parent, start_field_vals={}, mod_field_vals={},
Rich Lane110e0e32012-10-26 16:21:46 -0700815 mod_fields=[], tp="tcp", check_test_params=False):
Dan Talayco4b2bee62010-07-20 14:10:05 -0700816 """
817 Set up the ingress and expected packet and action list for a test
818
Rich Lane2014f9b2012-10-05 15:29:40 -0700819 @param parent Must implement assertTrue
Dan Talayco4b2bee62010-07-20 14:10:05 -0700820 @param start_field_values Field values to use for ingress packet (optional)
821 @param mod_field_values Field values to use for modified packet (optional)
822 @param mod_fields The list of fields to be modified by the switch in the test.
823 @params check_test_params If True, will check the parameters vid, add_vlan
824 and strip_vlan from the command line.
825
826 Returns a triple: pkt-to-send, expected-pkt, action-list
827 """
828
829 new_actions = []
830
Dan Talayco4b2bee62010-07-20 14:10:05 -0700831 base_pkt_params = {}
832 base_pkt_params['pktlen'] = 100
833 base_pkt_params['dl_dst'] = '00:DE:F0:12:34:56'
834 base_pkt_params['dl_src'] = '00:23:45:67:89:AB'
835 base_pkt_params['dl_vlan_enable'] = False
836 base_pkt_params['dl_vlan'] = 2
837 base_pkt_params['dl_vlan_pcp'] = 0
838 base_pkt_params['ip_src'] = '192.168.0.1'
839 base_pkt_params['ip_dst'] = '192.168.0.2'
840 base_pkt_params['ip_tos'] = 0
Rich Lane110e0e32012-10-26 16:21:46 -0700841 if tp == "tcp":
842 base_pkt_params['tcp_sport'] = 1234
843 base_pkt_params['tcp_dport'] = 80
844 elif tp == "udp":
845 base_pkt_params['udp_sport'] = 1234
846 base_pkt_params['udp_dport'] = 80
Dan Talayco4b2bee62010-07-20 14:10:05 -0700847 for keyname in start_field_vals.keys():
848 base_pkt_params[keyname] = start_field_vals[keyname]
849
850 mod_pkt_params = {}
851 mod_pkt_params['pktlen'] = 100
852 mod_pkt_params['dl_dst'] = '00:21:0F:ED:CB:A9'
853 mod_pkt_params['dl_src'] = '00:ED:CB:A9:87:65'
854 mod_pkt_params['dl_vlan_enable'] = False
855 mod_pkt_params['dl_vlan'] = 3
856 mod_pkt_params['dl_vlan_pcp'] = 7
857 mod_pkt_params['ip_src'] = '10.20.30.40'
858 mod_pkt_params['ip_dst'] = '50.60.70.80'
859 mod_pkt_params['ip_tos'] = 0xf0
Rich Lane110e0e32012-10-26 16:21:46 -0700860 if tp == "tcp":
861 mod_pkt_params['tcp_sport'] = 4321
862 mod_pkt_params['tcp_dport'] = 8765
863 elif tp == "udp":
864 mod_pkt_params['udp_sport'] = 4321
865 mod_pkt_params['udp_dport'] = 8765
Dan Talayco4b2bee62010-07-20 14:10:05 -0700866 for keyname in mod_field_vals.keys():
867 mod_pkt_params[keyname] = mod_field_vals[keyname]
868
869 # Check for test param modifications
870 strip = False
871 if check_test_params:
Rich Lane2014f9b2012-10-05 15:29:40 -0700872 add_vlan = test_param_get('add_vlan')
873 strip_vlan = test_param_get('strip_vlan')
874 vid = test_param_get('vid')
Dan Talayco4b2bee62010-07-20 14:10:05 -0700875
876 if add_vlan and strip_vlan:
877 parent.assertTrue(0, "Add and strip VLAN both specified")
878
879 if vid:
880 base_pkt_params['dl_vlan_enable'] = True
881 base_pkt_params['dl_vlan'] = vid
882 if 'dl_vlan' in mod_fields:
883 mod_pkt_params['dl_vlan'] = vid + 1
884
885 if add_vlan:
886 base_pkt_params['dl_vlan_enable'] = False
887 mod_pkt_params['dl_vlan_enable'] = True
888 mod_pkt_params['pktlen'] = base_pkt_params['pktlen'] + 4
889 mod_fields.append('pktlen')
890 mod_fields.append('dl_vlan_enable')
891 if 'dl_vlan' not in mod_fields:
892 mod_fields.append('dl_vlan')
893 elif strip_vlan:
894 base_pkt_params['dl_vlan_enable'] = True
895 mod_pkt_params['dl_vlan_enable'] = False
896 mod_pkt_params['pktlen'] = base_pkt_params['pktlen'] - 4
897 mod_fields.append('dl_vlan_enable')
898 mod_fields.append('pktlen')
899
Rich Lane110e0e32012-10-26 16:21:46 -0700900 if tp == "tcp":
901 packet_builder = simple_tcp_packet
902 elif tp == "udp":
903 packet_builder = simple_udp_packet
904 else:
905 raise NotImplementedError("unknown transport protocol %s" % tp)
906
Dan Talayco4b2bee62010-07-20 14:10:05 -0700907 # Build the ingress packet
Rich Lane110e0e32012-10-26 16:21:46 -0700908 ingress_pkt = packet_builder(**base_pkt_params)
Dan Talayco4b2bee62010-07-20 14:10:05 -0700909
910 # Build the expected packet, modifying the indicated fields
911 for item in mod_fields:
912 base_pkt_params[item] = mod_pkt_params[item]
913 act = action_generate(parent, item, mod_pkt_params)
914 if act:
915 new_actions.append(act)
916
Rich Lane110e0e32012-10-26 16:21:46 -0700917 expected_pkt = packet_builder(**base_pkt_params)
Dan Talayco4b2bee62010-07-20 14:10:05 -0700918
919 return (ingress_pkt, expected_pkt, new_actions)
Dan Talayco677c0b72011-08-23 22:53:38 -0700920
921# Generate a simple "drop" flow mod
922# If in_band is true, then only drop from first test port
923def flow_mod_gen(port_map, in_band):
924 request = message.flow_mod()
925 request.match.wildcards = ofp.OFPFW_ALL
926 if in_band:
927 request.match.wildcards = ofp.OFPFW_ALL - ofp.OFPFW_IN_PORT
928 for of_port, ifname in port_map.items(): # Grab first port
929 break
930 request.match.in_port = of_port
931 request.buffer_id = 0xffffffff
932 return request
Dan Talaycoba3745c2010-07-21 21:51:08 -0700933
934def skip_message_emit(parent, s):
935 """
936 Print out a 'skipped' message to stderr
937
938 @param s The string to print out to the log file
Rich Lane9a003812012-10-04 17:17:59 -0700939 @param parent Must implement config object
Dan Talaycoba3745c2010-07-21 21:51:08 -0700940 """
941 global skipped_test_count
942
943 skipped_test_count += 1
Rich Lane9a003812012-10-04 17:17:59 -0700944 logging.info("Skipping: " + s)
Rich Lane477f4812012-10-04 22:49:00 -0700945 if config["dbg_level"] < logging.WARNING:
Dan Talaycoba3745c2010-07-21 21:51:08 -0700946 sys.stderr.write("(skipped) ")
947 else:
948 sys.stderr.write("(S)")
Dan Talayco677c0b72011-08-23 22:53:38 -0700949
Dan Talayco8a64e332012-03-28 14:53:20 -0700950
951def all_stats_get(parent):
952 """
953 Get the aggregate stats for all flows in the table
954 @param parent Test instance with controller connection and assert
955 @returns dict with keys flows, packets, bytes, active (flows),
956 lookups, matched
957 """
958 stat_req = message.aggregate_stats_request()
959 stat_req.match = ofp.ofp_match()
960 stat_req.match.wildcards = ofp.OFPFW_ALL
961 stat_req.table_id = 0xff
962 stat_req.out_port = ofp.OFPP_NONE
963
964 rv = {}
965
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700966 (reply, pkt) = parent.controller.transact(stat_req)
Dan Talayco8a64e332012-03-28 14:53:20 -0700967 parent.assertTrue(len(reply.stats) == 1, "Did not receive flow stats reply")
968
969 for obj in reply.stats:
970 (rv["flows"], rv["packets"], rv["bytes"]) = (obj.flow_count,
971 obj.packet_count, obj.byte_count)
972 break
973
974 request = message.table_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700975 (reply , pkt) = parent.controller.transact(request)
Dan Talayco8a64e332012-03-28 14:53:20 -0700976
977
978 (rv["active"], rv["lookups"], rv["matched"]) = (0,0,0)
979 for obj in reply.stats:
980 rv["active"] += obj.active_count
981 rv["lookups"] += obj.lookup_count
982 rv["matched"] += obj.matched_count
983
984 return rv
Dan Talayco2baf8b52012-03-30 09:55:42 -0700985
986FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.'
987 for x in range(256)])
988
989def hex_dump_buffer(src, length=16):
990 """
991 Convert src to a hex dump string and return the string
992 @param src The source buffer
993 @param length The number of bytes shown in each line
994 @returns A string showing the hex dump
995 """
Dan Talaycoc516fa02012-04-12 22:28:43 -0700996 result = ["\n"]
Dan Talayco2baf8b52012-03-30 09:55:42 -0700997 for i in xrange(0, len(src), length):
998 chars = src[i:i+length]
999 hex = ' '.join(["%02x" % ord(x) for x in chars])
1000 printable = ''.join(["%s" % ((ord(x) <= 127 and
1001 FILTER[ord(x)]) or '.') for x in chars])
1002 result.append("%04x %-*s %s\n" % (i, length*3, hex, printable))
1003 return ''.join(result)
1004
1005def format_packet(pkt):
1006 return "Packet length %d \n%s" % (len(str(pkt)),
1007 hex_dump_buffer(str(pkt)))
Rich Lane5d7e89a2012-10-26 16:43:13 -07001008
1009def inspect_packet(pkt):
1010 """
1011 Wrapper around scapy's show() method.
1012 @returns A string showing the dissected packet.
1013 """
1014 from cStringIO import StringIO
1015 out = None
1016 backup = sys.stdout
1017 try:
1018 sys.stdout = StringIO()
1019 pkt.show2()
1020 out = sys.stdout.getvalue()
1021 sys.stdout.close()
1022 finally:
1023 sys.stdout = backup
1024 return out