blob: 13c82c31fb7bd92792be2f847a5f2245b0addf53 [file] [log] [blame]
Howard Pershc7963582012-03-29 10:02:59 -07001"""
2Flow query test case.
3
Howard Persh3340d452012-04-06 16:45:21 -07004Attempts to fill switch to capacity with randomized flows, and ensure that
5they all are read back correctly.
Howard Pershc7963582012-03-29 10:02:59 -07006"""
Howard Persh07d99e62012-04-09 15:26:57 -07007
8# COMMON TEST PARAMETERS
9#
10# Name: wildcards
11# Type: number
12# Description:
13# Overrides bitmap of supported wildcards reported by switch
14# Default: none
15#
Howard Pershc1199d52012-04-11 14:21:32 -070016# Name: wildcards_force
17# Type: number
18# Description:
19# Bitmap of wildcards to always be set
20# Default: none
21#
Howard Persh07d99e62012-04-09 15:26:57 -070022# Name: actions
23# Type: number
24# Description:
25# Overrides bitmap of supported actions reported by switch
26# Default: none
27#
Howard Pershc1199d52012-04-11 14:21:32 -070028# Name: actions_force
29# Type: number
30# Description:
31# Bitmap of actions to always be used
32# Default: none
33#
34# Name: ports
35# Type: list of OF port numbers
36# Description:
37# Override list of OF port numbers reported by switch
38# Default: none
39#
Howard Persh8d21c1f2012-04-20 15:57:29 -070040# Name: queues
41# Type: list of OF (port-number, queue-id) pairs
42# Description:
43# Override list of OF (port-number, queue-id) pairs returned by switch
44# Default: none
45#
Howard Pershb10a47a2012-08-21 13:54:47 -070046# Name: vlans
47# Type: list of VLAN ids
48# Description:
49# Override VLAN ids used in tests to given list
50# Default: []
51#
Howard Persh07d99e62012-04-09 15:26:57 -070052# Name: conservative_ordered_actions
53# Type: boolean (True or False)
54# Description:
55# Compare flow actions lists as unordered
56# Default: True
57
58
Howard Persh680b92a2012-03-31 13:34:35 -070059import math
Howard Pershc7963582012-03-29 10:02:59 -070060
61import logging
62
63import unittest
64import random
Howard Persh9cab4822012-09-11 17:08:40 -070065import time
Howard Pershc7963582012-03-29 10:02:59 -070066
Rich Lane477f4812012-10-04 22:49:00 -070067from oftest import config
Howard Pershc7963582012-03-29 10:02:59 -070068import oftest.controller as controller
69import oftest.cstruct as ofp
70import oftest.message as message
71import oftest.dataplane as dataplane
72import oftest.action as action
73import oftest.action_list as action_list
74import oftest.parse as parse
75import pktact
Rich Laneb90a1c42012-10-05 09:16:05 -070076import oftest.base_tests as base_tests
Howard Pershc7963582012-03-29 10:02:59 -070077
Rich Laneda3b5ad2012-10-03 09:05:32 -070078from oftest.testutils import *
Howard Pershc7963582012-03-29 10:02:59 -070079from time import sleep
80
Howard Pershc7963582012-03-29 10:02:59 -070081
rootf6af1672012-04-06 09:46:29 -070082def flip_coin():
83 return random.randint(1, 100) <= 50
84
85
Howard Pershc7963582012-03-29 10:02:59 -070086def shuffle(list):
87 n = len(list)
88 lim = n * n
89 i = 0
90 while i < lim:
91 a = random.randint(0, n - 1)
92 b = random.randint(0, n - 1)
93 temp = list[a]
94 list[a] = list[b]
95 list[b] = temp
96 i = i + 1
97 return list
98
99
Howard Persh680b92a2012-03-31 13:34:35 -0700100def rand_pick(list):
101 return list[random.randint(0, len(list) - 1)]
Howard Pershc7963582012-03-29 10:02:59 -0700102
Howard Persh680b92a2012-03-31 13:34:35 -0700103def rand_dl_addr():
104 return [random.randint(0, 255) & ~1,
105 random.randint(0, 255),
106 random.randint(0, 255),
107 random.randint(0, 255),
108 random.randint(0, 255),
109 random.randint(0, 255)
110 ]
Howard Pershc7963582012-03-29 10:02:59 -0700111
112def rand_nw_addr():
113 return random.randint(0, (1 << 32) - 1)
114
115
rootf6af1672012-04-06 09:46:29 -0700116class Flow_Info:
Howard Persh680b92a2012-03-31 13:34:35 -0700117 # Members:
118 # priorities - list of flow priorities
119 # dl_addrs - list of MAC addresses
120 # vlans - list of VLAN ids
121 # ethertypes - list of Ethertypes
122 # ip_addrs - list of IP addresses
123 # ip_tos - list of IP TOS values
124 # ip_protos - list of IP protocols
125 # l4_ports - list of L4 ports
126
127 def __init__(self):
128 priorities = []
129 dl_addrs = []
130 vlans = []
131 ethertypes = []
132 ip_addrs = []
133 ip_tos = []
134 ip_protos = []
135 l4_ports = []
136
137 def rand(self, n):
138 self.priorities = []
139 i = 0
140 while i < n:
141 self.priorities.append(random.randint(1, 65534))
142 i = i + 1
143
144 self.dl_addrs = []
145 i = 0
146 while i < n:
147 self.dl_addrs.append(rand_dl_addr())
148 i = i + 1
149
Rich Lane2014f9b2012-10-05 15:29:40 -0700150 if test_param_get("vlans", []) != []:
151 self.vlans = test_param_get("vlans", [])
Howard Pershb10a47a2012-08-21 13:54:47 -0700152
Rich Lane9a003812012-10-04 17:17:59 -0700153 logging.info("Overriding VLAN ids to:")
154 logging.info(self.vlans)
Howard Pershb10a47a2012-08-21 13:54:47 -0700155 else:
156 self.vlans = []
157 i = 0
158 while i < n:
159 self.vlans.append(random.randint(1, 4094))
160 i = i + 1
Howard Persh680b92a2012-03-31 13:34:35 -0700161
rootf6af1672012-04-06 09:46:29 -0700162 self.ethertypes = [0x0800, 0x0806]
Howard Persh680b92a2012-03-31 13:34:35 -0700163 i = 0
164 while i < n:
165 self.ethertypes.append(random.randint(0, (1 << 16) - 1))
166 i = i + 1
rootf6af1672012-04-06 09:46:29 -0700167 self.ethertypes = shuffle(self.ethertypes)[0 : n]
Howard Persh680b92a2012-03-31 13:34:35 -0700168
169 self.ip_addrs = []
170 i = 0
171 while i < n:
172 self.ip_addrs.append(rand_nw_addr())
173 i = i + 1
174
175 self.ip_tos = []
176 i = 0
177 while i < n:
178 self.ip_tos.append(random.randint(0, (1 << 8) - 1) & ~3)
179 i = i + 1
180
rootf6af1672012-04-06 09:46:29 -0700181 self.ip_protos = [1, 6, 17]
Howard Persh680b92a2012-03-31 13:34:35 -0700182 i = 0
183 while i < n:
184 self.ip_protos.append(random.randint(0, (1 << 8) - 1))
185 i = i + 1
rootf6af1672012-04-06 09:46:29 -0700186 self.ip_protos = shuffle(self.ip_protos)[0 : n]
Howard Persh680b92a2012-03-31 13:34:35 -0700187
188 self.l4_ports = []
189 i = 0
190 while i < n:
191 self.l4_ports.append(random.randint(0, (1 << 16) - 1))
192 i = i + 1
193
194 def rand_priority(self):
195 return rand_pick(self.priorities)
196
197 def rand_dl_addr(self):
198 return rand_pick(self.dl_addrs)
199
200 def rand_vlan(self):
201 return rand_pick(self.vlans)
202
203 def rand_ethertype(self):
204 return rand_pick(self.ethertypes)
205
206 def rand_ip_addr(self):
207 return rand_pick(self.ip_addrs)
208
209 def rand_ip_tos(self):
210 return rand_pick(self.ip_tos)
211
212 def rand_ip_proto(self):
213 return rand_pick(self.ip_protos)
214
215 def rand_l4_port(self):
216 return rand_pick(self.l4_ports)
217
218
Howard Pershc7963582012-03-29 10:02:59 -0700219# TBD - These don't belong here
220
Howard Persh680b92a2012-03-31 13:34:35 -0700221all_wildcards_list = [ofp.OFPFW_IN_PORT,
Howard Persh680b92a2012-03-31 13:34:35 -0700222 ofp.OFPFW_DL_DST,
rootf6af1672012-04-06 09:46:29 -0700223 ofp.OFPFW_DL_SRC,
224 ofp.OFPFW_DL_VLAN,
225 ofp.OFPFW_DL_VLAN_PCP,
Howard Persh680b92a2012-03-31 13:34:35 -0700226 ofp.OFPFW_DL_TYPE,
rootf6af1672012-04-06 09:46:29 -0700227 ofp.OFPFW_NW_TOS,
Howard Persh680b92a2012-03-31 13:34:35 -0700228 ofp.OFPFW_NW_PROTO,
Howard Persh680b92a2012-03-31 13:34:35 -0700229 ofp.OFPFW_NW_SRC_MASK,
230 ofp.OFPFW_NW_DST_MASK,
rootf6af1672012-04-06 09:46:29 -0700231 ofp.OFPFW_TP_SRC,
232 ofp.OFPFW_TP_DST
Howard Persh680b92a2012-03-31 13:34:35 -0700233 ]
Howard Pershc7963582012-03-29 10:02:59 -0700234
Howard Persh3340d452012-04-06 16:45:21 -0700235# TBD - Need this because there are duplicates in ofp.ofp_flow_wildcards_map
236# -- FIX
rootf6af1672012-04-06 09:46:29 -0700237all_wildcard_names = {
238 1 : 'OFPFW_IN_PORT',
239 2 : 'OFPFW_DL_VLAN',
240 4 : 'OFPFW_DL_SRC',
241 8 : 'OFPFW_DL_DST',
242 16 : 'OFPFW_DL_TYPE',
243 32 : 'OFPFW_NW_PROTO',
244 64 : 'OFPFW_TP_SRC',
245 128 : 'OFPFW_TP_DST',
246 1048576 : 'OFPFW_DL_VLAN_PCP',
247 2097152 : 'OFPFW_NW_TOS'
248}
249
rootf6af1672012-04-06 09:46:29 -0700250def wildcard_set(x, w, val):
251 result = x
252 if w == ofp.OFPFW_NW_SRC_MASK:
Howard Persh3340d452012-04-06 16:45:21 -0700253 result = (result & ~ofp.OFPFW_NW_SRC_MASK) \
254 | (val << ofp.OFPFW_NW_SRC_SHIFT)
rootf6af1672012-04-06 09:46:29 -0700255 elif w == ofp.OFPFW_NW_DST_MASK:
Howard Persh3340d452012-04-06 16:45:21 -0700256 result = (result & ~ofp.OFPFW_NW_DST_MASK) \
257 | (val << ofp.OFPFW_NW_DST_SHIFT)
rootf6af1672012-04-06 09:46:29 -0700258 elif val == 0:
259 result = result & ~w
260 else:
261 result = result | w
262 return result
263
264def wildcard_get(x, w):
265 if w == ofp.OFPFW_NW_SRC_MASK:
266 return (x & ofp.OFPFW_NW_SRC_MASK) >> ofp.OFPFW_NW_SRC_SHIFT
267 if w == ofp.OFPFW_NW_DST_MASK:
268 return (x & ofp.OFPFW_NW_DST_MASK) >> ofp.OFPFW_NW_DST_SHIFT
269 return 1 if (x & w) != 0 else 0
270
Howard Persh8d21c1f2012-04-20 15:57:29 -0700271def wildcards_to_str(wildcards):
272 result = "{"
273 sep = ""
274 for w in all_wildcards_list:
275 if (wildcards & w) == 0:
276 continue
277 if w == ofp.OFPFW_NW_SRC_MASK:
278 n = wildcard_get(wildcards, w)
279 if n > 0:
280 result = result + sep + ("OFPFW_NW_SRC(%d)" % (n))
281 elif w == ofp.OFPFW_NW_DST_MASK:
282 n = wildcard_get(wildcards, w)
283 if n > 0:
284 result = result + sep + ("OFPFW_NW_DST(%d)" % (n))
285 else:
286 result = result + sep + all_wildcard_names[w]
287 sep = ", "
288 result = result +"}"
289 return result
Howard Pershc7963582012-03-29 10:02:59 -0700290
Howard Persh680b92a2012-03-31 13:34:35 -0700291all_actions_list = [ofp.OFPAT_OUTPUT,
292 ofp.OFPAT_SET_VLAN_VID,
293 ofp.OFPAT_SET_VLAN_PCP,
294 ofp.OFPAT_STRIP_VLAN,
295 ofp.OFPAT_SET_DL_SRC,
296 ofp.OFPAT_SET_DL_DST,
297 ofp.OFPAT_SET_NW_SRC,
298 ofp.OFPAT_SET_NW_DST,
299 ofp.OFPAT_SET_NW_TOS,
300 ofp.OFPAT_SET_TP_SRC,
301 ofp.OFPAT_SET_TP_DST,
302 ofp.OFPAT_ENQUEUE
303 ]
304
Howard Persh8d21c1f2012-04-20 15:57:29 -0700305def actions_bmap_to_str(bm):
306 result = "{"
307 sep = ""
308 for a in all_actions_list:
309 if ((1 << a) & bm) != 0:
310 result = result + sep + ofp.ofp_action_type_map[a]
311 sep = ", "
312 result = result + "}"
313 return result
314
Howard Persh680b92a2012-03-31 13:34:35 -0700315def dl_addr_to_str(a):
316 return "%x:%x:%x:%x:%x:%x" % tuple(a)
317
318def ip_addr_to_str(a, n):
rootf6af1672012-04-06 09:46:29 -0700319 if n is not None:
320 a = a & ~((1 << (32 - n)) - 1)
Howard Persh680b92a2012-03-31 13:34:35 -0700321 result = "%d.%d.%d.%d" % (a >> 24, \
322 (a >> 16) & 0xff, \
323 (a >> 8) & 0xff, \
324 a & 0xff \
325 )
326 if n is not None:
327 result = result + ("/%d" % (n))
328 return result
329
Howard Pershc7963582012-03-29 10:02:59 -0700330
rootf6af1672012-04-06 09:46:29 -0700331class Flow_Cfg:
Howard Pershc7963582012-03-29 10:02:59 -0700332 # Members:
333 # - match
334 # - idle_timeout
335 # - hard_timeout
336 # - priority
337 # - action_list
338
339 def __init__(self):
Howard Persh680b92a2012-03-31 13:34:35 -0700340 self.priority = 0
Howard Pershc7963582012-03-29 10:02:59 -0700341 self.match = parse.ofp_match()
342 self.match.wildcards = ofp.OFPFW_ALL
343 self.idle_timeout = 0
344 self.hard_timeout = 0
Howard Pershc7963582012-03-29 10:02:59 -0700345 self.actions = action_list.action_list()
346
rootf6af1672012-04-06 09:46:29 -0700347 # {pri, match} is considered a flow key
348 def key_equal(self, x):
Howard Persh680b92a2012-03-31 13:34:35 -0700349 if self.priority != x.priority:
350 return False
351 # TBD - Should this logic be moved to ofp_match.__eq__()?
352 if self.match.wildcards != x.match.wildcards:
353 return False
rootf6af1672012-04-06 09:46:29 -0700354 if wildcard_get(self.match.wildcards, ofp.OFPFW_IN_PORT) == 0 \
Howard Persh680b92a2012-03-31 13:34:35 -0700355 and self.match.in_port != x.match.in_port:
356 return False
rootf6af1672012-04-06 09:46:29 -0700357 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_DST) == 0 \
Howard Persh680b92a2012-03-31 13:34:35 -0700358 and self.match.dl_dst != x.match.dl_dst:
359 return False
rootf6af1672012-04-06 09:46:29 -0700360 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_SRC) == 0 \
361 and self.match.dl_src != x.match.dl_src:
362 return False
363 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_VLAN) == 0 \
Howard Persh680b92a2012-03-31 13:34:35 -0700364 and self.match.dl_vlan != x.match.dl_vlan:
365 return False
rootf6af1672012-04-06 09:46:29 -0700366 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_VLAN_PCP) == 0 \
Howard Persh680b92a2012-03-31 13:34:35 -0700367 and self.match.dl_vlan_pcp != x.match.dl_vlan_pcp:
368 return False
rootf6af1672012-04-06 09:46:29 -0700369 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_TYPE) == 0 \
Howard Persh680b92a2012-03-31 13:34:35 -0700370 and self.match.dl_type != x.match.dl_type:
371 return False
rootf6af1672012-04-06 09:46:29 -0700372 if wildcard_get(self.match.wildcards, ofp.OFPFW_NW_TOS) == 0 \
Howard Persh680b92a2012-03-31 13:34:35 -0700373 and self.match.nw_tos != x.match.nw_tos:
374 return False
rootf6af1672012-04-06 09:46:29 -0700375 if wildcard_get(self.match.wildcards, ofp.OFPFW_NW_PROTO) == 0 \
Howard Persh680b92a2012-03-31 13:34:35 -0700376 and self.match.nw_proto != x.match.nw_proto:
377 return False
rootf6af1672012-04-06 09:46:29 -0700378 n = wildcard_get(self.match.wildcards, ofp.OFPFW_NW_SRC_MASK)
379 if n < 32:
380 m = ~((1 << n) - 1)
Howard Persh680b92a2012-03-31 13:34:35 -0700381 if (self.match.nw_src & m) != (x.match.nw_src & m):
382 return False
rootf6af1672012-04-06 09:46:29 -0700383 n = wildcard_get(self.match.wildcards, ofp.OFPFW_NW_DST_MASK)
384 if n < 32:
385 m = ~((1 << n) - 1)
Howard Persh680b92a2012-03-31 13:34:35 -0700386 if (self.match.nw_dst & m) != (x.match.nw_dst & m):
387 return False
rootf6af1672012-04-06 09:46:29 -0700388 if wildcard_get(self.match.wildcards, ofp.OFPFW_TP_SRC) == 0 \
389 and self.match.tp_src != x.match.tp_src:
Howard Persh680b92a2012-03-31 13:34:35 -0700390 return False
rootf6af1672012-04-06 09:46:29 -0700391 if wildcard_get(self.match.wildcards, ofp.OFPFW_TP_DST) == 0 \
392 and self.match.tp_dst != x.match.tp_dst:
393 return False
394 return True
395
Howard Persh5f3c83f2012-04-13 09:57:10 -0700396 def actions_equal(self, x):
Rich Lane2014f9b2012-10-05 15:29:40 -0700397 if test_param_get("conservative_ordered_actions", True):
Howard Persh5f3c83f2012-04-13 09:57:10 -0700398 # Compare actions lists as unordered
399
root2843d2b2012-04-06 10:27:46 -0700400 aa = copy.deepcopy(x.actions.actions)
401 for a in self.actions.actions:
402 i = 0
403 while i < len(aa):
404 if a == aa[i]:
405 break
406 i = i + 1
407 if i < len(aa):
408 aa.pop(i)
409 else:
410 return False
411 return aa == []
412 else:
413 return self.actions == x.actions
Howard Persh5f3c83f2012-04-13 09:57:10 -0700414
415 def non_key_equal(self, x):
416 if self.cookie != x.cookie:
417 return False
418 if self.idle_timeout != x.idle_timeout:
419 return False
420 if self.hard_timeout != x.hard_timeout:
421 return False
422 return self.actions_equal(x)
rootf6af1672012-04-06 09:46:29 -0700423
root2843d2b2012-04-06 10:27:46 -0700424 def key_str(self):
Howard Persh680b92a2012-03-31 13:34:35 -0700425 result = "priority=%d" % self.priority
426 # TBD - Would be nice if ofp_match.show() was better behaved
427 # (no newlines), and more intuitive (things in hex where approprate), etc.
Howard Persh8d21c1f2012-04-20 15:57:29 -0700428 result = result + (", wildcards=0x%x=%s" \
429 % (self.match.wildcards, \
430 wildcards_to_str(self.match.wildcards) \
431 )
432 )
rootf6af1672012-04-06 09:46:29 -0700433 if wildcard_get(self.match.wildcards, ofp.OFPFW_IN_PORT) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -0700434 result = result + (", in_port=%d" % (self.match.in_port))
rootf6af1672012-04-06 09:46:29 -0700435 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_DST) == 0:
Howard Persh3340d452012-04-06 16:45:21 -0700436 result = result + (", dl_dst=%s" \
437 % (dl_addr_to_str(self.match.dl_dst)) \
438 )
rootf6af1672012-04-06 09:46:29 -0700439 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_SRC) == 0:
Howard Persh3340d452012-04-06 16:45:21 -0700440 result = result + (", dl_src=%s" \
441 % (dl_addr_to_str(self.match.dl_src)) \
442 )
rootf6af1672012-04-06 09:46:29 -0700443 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_VLAN) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -0700444 result = result + (", dl_vlan=%d" % (self.match.dl_vlan))
rootf6af1672012-04-06 09:46:29 -0700445 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_VLAN_PCP) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -0700446 result = result + (", dl_vlan_pcp=%d" % (self.match.dl_vlan_pcp))
rootf6af1672012-04-06 09:46:29 -0700447 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_TYPE) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -0700448 result = result + (", dl_type=0x%x" % (self.match.dl_type))
rootf6af1672012-04-06 09:46:29 -0700449 if wildcard_get(self.match.wildcards, ofp.OFPFW_NW_TOS) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -0700450 result = result + (", nw_tos=0x%x" % (self.match.nw_tos))
rootf6af1672012-04-06 09:46:29 -0700451 if wildcard_get(self.match.wildcards, ofp.OFPFW_NW_PROTO) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -0700452 result = result + (", nw_proto=%d" % (self.match.nw_proto))
rootf6af1672012-04-06 09:46:29 -0700453 n = wildcard_get(self.match.wildcards, ofp.OFPFW_NW_SRC_MASK)
Howard Persh680b92a2012-03-31 13:34:35 -0700454 if n < 32:
Howard Persh3340d452012-04-06 16:45:21 -0700455 result = result + (", nw_src=%s" % \
456 (ip_addr_to_str(self.match.nw_src, 32 - n)) \
457 )
rootf6af1672012-04-06 09:46:29 -0700458 n = wildcard_get(self.match.wildcards, ofp.OFPFW_NW_DST_MASK)
Howard Persh680b92a2012-03-31 13:34:35 -0700459 if n < 32:
Howard Persh3340d452012-04-06 16:45:21 -0700460 result = result + (", nw_dst=%s" % \
461 (ip_addr_to_str(self.match.nw_dst, 32 - n)) \
462 )
rootf6af1672012-04-06 09:46:29 -0700463 if wildcard_get(self.match.wildcards, ofp.OFPFW_TP_SRC) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -0700464 result = result + (", tp_src=%d" % self.match.tp_src)
rootf6af1672012-04-06 09:46:29 -0700465 if wildcard_get(self.match.wildcards, ofp.OFPFW_TP_DST) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -0700466 result = result + (", tp_dst=%d" % self.match.tp_dst)
rootf6af1672012-04-06 09:46:29 -0700467 return result
468
469 def __eq__(self, x):
470 return (self.key_equal(x) and self.non_key_equal(x))
471
472 def __str__(self):
root2843d2b2012-04-06 10:27:46 -0700473 result = self.key_str()
474 result = result + (", cookie=%d" % self.cookie)
Howard Persh680b92a2012-03-31 13:34:35 -0700475 result = result + (", idle_timeout=%d" % self.idle_timeout)
476 result = result + (", hard_timeout=%d" % self.hard_timeout)
Howard Persh680b92a2012-03-31 13:34:35 -0700477 for a in self.actions.actions:
478 result = result + (", action=%s" % ofp.ofp_action_type_map[a.type])
479 if a.type == ofp.OFPAT_OUTPUT:
480 result = result + ("(%d)" % (a.port))
481 elif a.type == ofp.OFPAT_SET_VLAN_VID:
482 result = result + ("(%d)" % (a.vlan_vid))
483 elif a.type == ofp.OFPAT_SET_VLAN_PCP:
484 result = result + ("(%d)" % (a.vlan_pcp))
485 elif a.type == ofp.OFPAT_SET_DL_SRC or a.type == ofp.OFPAT_SET_DL_DST:
486 result = result + ("(%s)" % (dl_addr_to_str(a.dl_addr)))
487 elif a.type == ofp.OFPAT_SET_NW_SRC or a.type == ofp.OFPAT_SET_NW_DST:
488 result = result + ("(%s)" % (ip_addr_to_str(a.nw_addr, None)))
489 elif a.type == ofp.OFPAT_SET_NW_TOS:
490 result = result + ("(0x%x)" % (a.nw_tos))
491 elif a.type == ofp.OFPAT_SET_TP_SRC or a.type == ofp.OFPAT_SET_TP_DST:
492 result = result + ("(%d)" % (a.tp_port))
493 elif a.type == ofp.OFPAT_ENQUEUE:
494 result = result + ("(port=%d,queue=%d)" % (a.port, a.queue_id))
495 return result
Howard Pershc7963582012-03-29 10:02:59 -0700496
Howard Persh8d21c1f2012-04-20 15:57:29 -0700497 def rand_actions_ordered(self, fi, valid_actions, valid_ports, valid_queues):
Howard Persh3340d452012-04-06 16:45:21 -0700498 # Action lists are ordered, so pick an ordered random subset of
499 # supported actions
Howard Pershc1199d52012-04-11 14:21:32 -0700500
Rich Lane2014f9b2012-10-05 15:29:40 -0700501 actions_force = test_param_get("actions_force", 0)
Howard Persh8d21c1f2012-04-20 15:57:29 -0700502 if actions_force != 0:
Rich Lane9a003812012-10-04 17:17:59 -0700503 logging.info("Forced actions:")
504 logging.info(actions_bmap_to_str(actions_force))
Howard Pershc1199d52012-04-11 14:21:32 -0700505
Dan Talayco910a8282012-04-07 00:05:20 -0700506 ACTION_MAX_LEN = 65535 # @fixme Should be test param?
Howard Persh3340d452012-04-06 16:45:21 -0700507 supported_actions = []
508 for a in all_actions_list:
509 if ((1 << a) & valid_actions) != 0:
510 supported_actions.append(a)
511
Howard Pershc1199d52012-04-11 14:21:32 -0700512 actions \
513 = shuffle(supported_actions)[0 : random.randint(1, len(supported_actions))]
514
515 for a in all_actions_list:
516 if ((1 << a) & actions_force) != 0:
517 actions.append(a)
518
519 actions = shuffle(actions)
Howard Persh3340d452012-04-06 16:45:21 -0700520
Howard Persh6a3698d2012-08-21 14:26:39 -0700521 set_vlanf = False
522 strip_vlanf = False
Howard Persh3340d452012-04-06 16:45:21 -0700523 self.actions = action_list.action_list()
Howard Pershc1199d52012-04-11 14:21:32 -0700524 for a in actions:
Dan Talayco910a8282012-04-07 00:05:20 -0700525 act = None
Howard Persh3340d452012-04-06 16:45:21 -0700526 if a == ofp.OFPAT_OUTPUT:
527 pass # OUTPUT actions must come last
528 elif a == ofp.OFPAT_SET_VLAN_VID:
Howard Persh6a3698d2012-08-21 14:26:39 -0700529 if not strip_vlanf:
530 act = action.action_set_vlan_vid()
531 act.vlan_vid = fi.rand_vlan()
532 set_vlanf = True
Howard Persh3340d452012-04-06 16:45:21 -0700533 elif a == ofp.OFPAT_SET_VLAN_PCP:
Howard Persh6a3698d2012-08-21 14:26:39 -0700534 if not strip_vlanf:
535 act = action.action_set_vlan_pcp()
536 act.vlan_pcp = random.randint(0, (1 << 3) - 1)
537 set_vlanf = True
Howard Persh3340d452012-04-06 16:45:21 -0700538 elif a == ofp.OFPAT_STRIP_VLAN:
Howard Persh6a3698d2012-08-21 14:26:39 -0700539 if not set_vlanf:
540 act = action.action_strip_vlan()
541 strip_vlanf = True
Howard Persh3340d452012-04-06 16:45:21 -0700542 elif a == ofp.OFPAT_SET_DL_SRC:
543 act = action.action_set_dl_src()
544 act.dl_addr = fi.rand_dl_addr()
Howard Persh3340d452012-04-06 16:45:21 -0700545 elif a == ofp.OFPAT_SET_DL_DST:
546 act = action.action_set_dl_dst()
547 act.dl_addr = fi.rand_dl_addr()
Howard Persh3340d452012-04-06 16:45:21 -0700548 elif a == ofp.OFPAT_SET_NW_SRC:
549 act = action.action_set_nw_src()
550 act.nw_addr = fi.rand_ip_addr()
Howard Persh3340d452012-04-06 16:45:21 -0700551 elif a == ofp.OFPAT_SET_NW_DST:
552 act = action.action_set_nw_dst()
553 act.nw_addr = fi.rand_ip_addr()
Howard Persh3340d452012-04-06 16:45:21 -0700554 elif a == ofp.OFPAT_SET_NW_TOS:
555 act = action.action_set_nw_tos()
556 act.nw_tos = fi.rand_ip_tos()
Howard Persh3340d452012-04-06 16:45:21 -0700557 elif a == ofp.OFPAT_SET_TP_SRC:
558 act = action.action_set_tp_src()
559 act.tp_port = fi.rand_l4_port()
Howard Persh3340d452012-04-06 16:45:21 -0700560 elif a == ofp.OFPAT_SET_TP_DST:
561 act = action.action_set_tp_dst()
562 act.tp_port = fi.rand_l4_port()
Howard Persh3340d452012-04-06 16:45:21 -0700563 elif a == ofp.OFPAT_ENQUEUE:
564 pass # Enqueue actions must come last
Dan Talayco910a8282012-04-07 00:05:20 -0700565 if act:
Dan Talayco910a8282012-04-07 00:05:20 -0700566 self.actions.add(act)
567
Howard Persh3340d452012-04-06 16:45:21 -0700568 p = random.randint(1, 100)
Howard Pershc1199d52012-04-11 14:21:32 -0700569 if (((1 << ofp.OFPAT_ENQUEUE) & actions_force) != 0 or p <= 33) \
Howard Persh8d21c1f2012-04-20 15:57:29 -0700570 and len(valid_queues) > 0 \
571 and ofp.OFPAT_ENQUEUE in actions:
Howard Pershc1199d52012-04-11 14:21:32 -0700572 # In not forecd, one third of the time, include ENQUEUE actions
573 # at end of list
Howard Persh3340d452012-04-06 16:45:21 -0700574 # At most 1 ENQUEUE action
575 act = action.action_enqueue()
Howard Persh8d21c1f2012-04-20 15:57:29 -0700576 (act.port, act.queue_id) = rand_pick(valid_queues)
Howard Persh3340d452012-04-06 16:45:21 -0700577 self.actions.add(act)
Howard Pershc1199d52012-04-11 14:21:32 -0700578 if (((1 << ofp.OFPAT_OUTPUT) & actions_force) != 0 \
579 or (p > 33 and p <= 66) \
580 ) \
Howard Persh8d21c1f2012-04-20 15:57:29 -0700581 and len(valid_ports) > 0 \
Howard Pershc1199d52012-04-11 14:21:32 -0700582 and ofp.OFPAT_OUTPUT in actions:
Howard Persh3340d452012-04-06 16:45:21 -0700583 # One third of the time, include OUTPUT actions at end of list
584 port_idxs = shuffle(range(len(valid_ports)))
585 # Only 1 output action allowed if IN_PORT wildcarded
586 n = 1 if wildcard_get(self.match.wildcards, ofp.OFPFW_IN_PORT) != 0 \
587 else random.randint(1, len(valid_ports))
588 port_idxs = port_idxs[0 : n]
589 for pi in port_idxs:
590 act = action.action_output()
591 act.port = valid_ports[pi]
Howard Persh3340d452012-04-06 16:45:21 -0700592 if act.port != ofp.OFPP_IN_PORT \
593 or wildcard_get(self.match.wildcards, ofp.OFPFW_IN_PORT) == 0:
594 # OUTPUT(IN_PORT) only valid if OFPFW_IN_PORT not wildcarded
595 self.actions.add(act)
596 else:
597 # One third of the time, include neither
598 pass
599
600
601 # Randomize flow data for flow modifies (i.e. cookie and actions)
Howard Persh8d21c1f2012-04-20 15:57:29 -0700602 def rand_mod(self, fi, valid_actions, valid_ports, valid_queues):
rootf6af1672012-04-06 09:46:29 -0700603 self.cookie = random.randint(0, (1 << 53) - 1)
Howard Pershc7963582012-03-29 10:02:59 -0700604
Dan Talayco910a8282012-04-07 00:05:20 -0700605 # By default, test with conservative ordering conventions
606 # This should probably be indicated in a profile
Rich Lane2014f9b2012-10-05 15:29:40 -0700607 if test_param_get("conservative_ordered_actions", True):
Howard Persh8d21c1f2012-04-20 15:57:29 -0700608 self.rand_actions_ordered(fi, valid_actions, valid_ports, valid_queues)
Howard Persh3340d452012-04-06 16:45:21 -0700609 return self
610
Rich Lane2014f9b2012-10-05 15:29:40 -0700611 actions_force = test_param_get("actions_force", 0)
Howard Persh8d21c1f2012-04-20 15:57:29 -0700612 if actions_force != 0:
Rich Lane9a003812012-10-04 17:17:59 -0700613 logging.info("Forced actions:")
614 logging.info(actions_bmap_to_str(actions_force))
Howard Pershc1199d52012-04-11 14:21:32 -0700615
616 ACTION_MAX_LEN = 65535 # @fixme Should be test param?
Howard Persh680b92a2012-03-31 13:34:35 -0700617 supported_actions = []
618 for a in all_actions_list:
619 if ((1 << a) & valid_actions) != 0:
620 supported_actions.append(a)
621
Howard Pershc1199d52012-04-11 14:21:32 -0700622 actions \
623 = shuffle(supported_actions)[0 : random.randint(1, len(supported_actions))]
624
625 for a in all_actions_list:
626 if ((1 << a) & actions_force) != 0:
627 actions.append(a)
628
629 actions = shuffle(actions)
Howard Pershc7963582012-03-29 10:02:59 -0700630
631 self.actions = action_list.action_list()
Howard Pershc1199d52012-04-11 14:21:32 -0700632 for a in actions:
Howard Pershc7963582012-03-29 10:02:59 -0700633 if a == ofp.OFPAT_OUTPUT:
634 # TBD - Output actions are clustered in list, spread them out?
Howard Persh8d21c1f2012-04-20 15:57:29 -0700635 if len(valid_ports) == 0:
636 continue
Howard Pershc7963582012-03-29 10:02:59 -0700637 port_idxs = shuffle(range(len(valid_ports)))
Howard Persh680b92a2012-03-31 13:34:35 -0700638 port_idxs = port_idxs[0 : random.randint(1, len(valid_ports))]
Howard Pershc7963582012-03-29 10:02:59 -0700639 for pi in port_idxs:
640 act = action.action_output()
Howard Persh680b92a2012-03-31 13:34:35 -0700641 act.port = valid_ports[pi]
Howard Pershc7963582012-03-29 10:02:59 -0700642 self.actions.add(act)
643 elif a == ofp.OFPAT_SET_VLAN_VID:
644 act = action.action_set_vlan_vid()
Howard Persh680b92a2012-03-31 13:34:35 -0700645 act.vlan_vid = fi.rand_vlan()
Howard Pershc7963582012-03-29 10:02:59 -0700646 self.actions.add(act)
647 elif a == ofp.OFPAT_SET_VLAN_PCP:
Dan Talayco910a8282012-04-07 00:05:20 -0700648 act = action.action_set_vlan_pcp()
649 act.vlan_pcp = random.randint(0, (1 << 3) - 1)
Howard Pershc7963582012-03-29 10:02:59 -0700650 elif a == ofp.OFPAT_STRIP_VLAN:
651 act = action.action_strip_vlan()
652 self.actions.add(act)
653 elif a == ofp.OFPAT_SET_DL_SRC:
654 act = action.action_set_dl_src()
Howard Persh680b92a2012-03-31 13:34:35 -0700655 act.dl_addr = fi.rand_dl_addr()
Howard Pershc7963582012-03-29 10:02:59 -0700656 self.actions.add(act)
657 elif a == ofp.OFPAT_SET_DL_DST:
658 act = action.action_set_dl_dst()
Howard Persh680b92a2012-03-31 13:34:35 -0700659 act.dl_addr = fi.rand_dl_addr()
Howard Pershc7963582012-03-29 10:02:59 -0700660 self.actions.add(act)
661 elif a == ofp.OFPAT_SET_NW_SRC:
662 act = action.action_set_nw_src()
Howard Persh680b92a2012-03-31 13:34:35 -0700663 act.nw_addr = fi.rand_ip_addr()
Howard Pershc7963582012-03-29 10:02:59 -0700664 self.actions.add(act)
665 elif a == ofp.OFPAT_SET_NW_DST:
666 act = action.action_set_nw_dst()
Howard Persh680b92a2012-03-31 13:34:35 -0700667 act.nw_addr = fi.rand_ip_addr()
Howard Pershc7963582012-03-29 10:02:59 -0700668 self.actions.add(act)
669 elif a == ofp.OFPAT_SET_NW_TOS:
670 act = action.action_set_nw_tos()
Howard Persh680b92a2012-03-31 13:34:35 -0700671 act.nw_tos = fi.rand_ip_tos()
Howard Pershc7963582012-03-29 10:02:59 -0700672 self.actions.add(act)
673 elif a == ofp.OFPAT_SET_TP_SRC:
674 act = action.action_set_tp_src()
Howard Persh680b92a2012-03-31 13:34:35 -0700675 act.tp_port = fi.rand_l4_port()
Howard Pershc7963582012-03-29 10:02:59 -0700676 self.actions.add(act)
677 elif a == ofp.OFPAT_SET_TP_DST:
678 act = action.action_set_tp_dst()
Howard Persh680b92a2012-03-31 13:34:35 -0700679 act.tp_port = fi.rand_l4_port()
Howard Pershc7963582012-03-29 10:02:59 -0700680 self.actions.add(act)
681 elif a == ofp.OFPAT_ENQUEUE:
682 # TBD - Enqueue actions are clustered in list, spread them out?
Howard Persh8d21c1f2012-04-20 15:57:29 -0700683 if len(valid_queues) == 0:
684 continue
685 qidxs = shuffle(range(len(valid_queues)))
686 qidxs = qidxs[0 : random.randint(1, len(valid_queues))]
687 for qi in qidxs:
Howard Pershc7963582012-03-29 10:02:59 -0700688 act = action.action_enqueue()
Howard Persh8d21c1f2012-04-20 15:57:29 -0700689 (act.port, act.queue_id) = valid_queues[qi]
Howard Pershc7963582012-03-29 10:02:59 -0700690 self.actions.add(act)
691
692 return self
693
rootf6af1672012-04-06 09:46:29 -0700694 # Randomize flow cfg
Ed Swierk99a74de2012-08-22 06:40:54 -0700695 def rand(self, fi, wildcards_force, valid_wildcards, valid_actions, valid_ports,
696 valid_queues):
Howard Persh8d21c1f2012-04-20 15:57:29 -0700697 if wildcards_force != 0:
Rich Lane9a003812012-10-04 17:17:59 -0700698 logging.info("Wildcards forced:")
699 logging.info(wildcards_to_str(wildcards_force))
Howard Pershc1199d52012-04-11 14:21:32 -0700700
rootf6af1672012-04-06 09:46:29 -0700701 # Start with no wildcards, i.e. everything specified
702 self.match.wildcards = 0
Howard Pershc1199d52012-04-11 14:21:32 -0700703
704 if wildcards_force != 0:
705 exact = False
706 else:
707 # Make approx. 5% of flows exact
708 exact = (random.randint(1, 100) <= 5)
rootf6af1672012-04-06 09:46:29 -0700709
710 # For each qualifier Q,
711 # if (wildcarding is not supported for Q,
712 # or an exact flow is specified
713 # or a coin toss comes up heads),
714 # specify Q
715 # else
716 # wildcard Q
717
Howard Pershc1199d52012-04-11 14:21:32 -0700718 if wildcard_get(wildcards_force, ofp.OFPFW_IN_PORT) == 0 \
719 and (wildcard_get(valid_wildcards, ofp.OFPFW_IN_PORT) == 0 \
720 or exact \
721 or flip_coin() \
722 ):
rootf6af1672012-04-06 09:46:29 -0700723 self.match.in_port = rand_pick(valid_ports)
724 else:
Howard Persh3340d452012-04-06 16:45:21 -0700725 self.match.wildcards = wildcard_set(self.match.wildcards, \
726 ofp.OFPFW_IN_PORT, \
727 1 \
728 )
rootf6af1672012-04-06 09:46:29 -0700729
Howard Pershc1199d52012-04-11 14:21:32 -0700730 if wildcard_get(wildcards_force, ofp.OFPFW_DL_DST) == 0 \
731 and (wildcard_get(valid_wildcards, ofp.OFPFW_DL_DST) == 0 \
732 or exact \
733 or flip_coin() \
734 ):
rootf6af1672012-04-06 09:46:29 -0700735 self.match.dl_dst = fi.rand_dl_addr()
736 else:
Howard Persh3340d452012-04-06 16:45:21 -0700737 self.match.wildcards = wildcard_set(self.match.wildcards, \
738 ofp.OFPFW_DL_DST, \
739 1 \
740 )
rootf6af1672012-04-06 09:46:29 -0700741
Howard Pershc1199d52012-04-11 14:21:32 -0700742 if wildcard_get(wildcards_force, ofp.OFPFW_DL_SRC) == 0 \
743 and (wildcard_get(valid_wildcards, ofp.OFPFW_DL_SRC) == 0 \
744 or exact \
745 or flip_coin() \
746 ):
rootf6af1672012-04-06 09:46:29 -0700747 self.match.dl_src = fi.rand_dl_addr()
748 else:
Howard Persh3340d452012-04-06 16:45:21 -0700749 self.match.wildcards = wildcard_set(self.match.wildcards, \
750 ofp.OFPFW_DL_SRC, \
751 1 \
752 )
rootf6af1672012-04-06 09:46:29 -0700753
Howard Pershc1199d52012-04-11 14:21:32 -0700754 if wildcard_get(wildcards_force, ofp.OFPFW_DL_VLAN) == 0 \
755 and (wildcard_get(valid_wildcards, ofp.OFPFW_DL_VLAN) == 0 \
756 or exact \
757 or flip_coin() \
758 ):
rootf6af1672012-04-06 09:46:29 -0700759 self.match.dl_vlan = fi.rand_vlan()
760 else:
Howard Persh3340d452012-04-06 16:45:21 -0700761 self.match.wildcards = wildcard_set(self.match.wildcards, \
762 ofp.OFPFW_DL_VLAN, \
763 1 \
764 )
rootf6af1672012-04-06 09:46:29 -0700765
Howard Pershc1199d52012-04-11 14:21:32 -0700766 if wildcard_get(wildcards_force, ofp.OFPFW_DL_VLAN_PCP) == 0 \
767 and (wildcard_get(valid_wildcards, ofp.OFPFW_DL_VLAN_PCP) == 0 \
768 or exact \
769 or flip_coin() \
770 ):
771 self.match.dl_vlan_pcp = random.randint(0, (1 << 3) - 1)
772 else:
773 self.match.wildcards = wildcard_set(self.match.wildcards, \
774 ofp.OFPFW_DL_VLAN_PCP, \
775 1 \
776 )
777
778 if wildcard_get(wildcards_force, ofp.OFPFW_DL_TYPE) == 0 \
779 and (wildcard_get(valid_wildcards, ofp.OFPFW_DL_TYPE) == 0 \
780 or exact \
781 or flip_coin() \
782 ):
rootf6af1672012-04-06 09:46:29 -0700783 self.match.dl_type = fi.rand_ethertype()
784 else:
Howard Persh3340d452012-04-06 16:45:21 -0700785 self.match.wildcards = wildcard_set(self.match.wildcards, \
786 ofp.OFPFW_DL_TYPE, \
787 1 \
788 )
rootf6af1672012-04-06 09:46:29 -0700789
Howard Pershc1199d52012-04-11 14:21:32 -0700790 n = wildcard_get(wildcards_force, ofp.OFPFW_NW_SRC_MASK)
791 if n == 0:
792 if exact or flip_coin():
793 n = 0
794 else:
795 n = wildcard_get(valid_wildcards, ofp.OFPFW_NW_SRC_MASK)
796 if n > 32:
797 n = 32
798 n = random.randint(0, n)
Howard Persh3340d452012-04-06 16:45:21 -0700799 self.match.wildcards = wildcard_set(self.match.wildcards, \
800 ofp.OFPFW_NW_SRC_MASK, \
801 n \
802 )
rootf6af1672012-04-06 09:46:29 -0700803 if n < 32:
804 self.match.nw_src = fi.rand_ip_addr() & ~((1 << n) - 1)
805 # Specifying any IP address match other than all bits
806 # don't care requires that Ethertype is one of {IP, ARP}
807 if flip_coin():
808 self.match.dl_type = rand_pick([0x0800, 0x0806])
Howard Persh3340d452012-04-06 16:45:21 -0700809 self.match.wildcards = wildcard_set(self.match.wildcards, \
810 ofp.OFPFW_DL_TYPE, \
811 0 \
812 )
rootf6af1672012-04-06 09:46:29 -0700813
Howard Pershc1199d52012-04-11 14:21:32 -0700814 n = wildcard_get(wildcards_force, ofp.OFPFW_NW_DST_MASK)
815 if n == 0:
816 if exact or flip_coin():
817 n = 0
818 else:
819 n = wildcard_get(valid_wildcards, ofp.OFPFW_NW_DST_MASK)
820 if n > 32:
821 n = 32
822 n = random.randint(0, n)
Howard Persh3340d452012-04-06 16:45:21 -0700823 self.match.wildcards = wildcard_set(self.match.wildcards, \
824 ofp.OFPFW_NW_DST_MASK, \
825 n \
826 )
rootf6af1672012-04-06 09:46:29 -0700827 if n < 32:
828 self.match.nw_dst = fi.rand_ip_addr() & ~((1 << n) - 1)
829 # Specifying any IP address match other than all bits
830 # don't care requires that Ethertype is one of {IP, ARP}
831 if flip_coin():
832 self.match.dl_type = rand_pick([0x0800, 0x0806])
Howard Persh3340d452012-04-06 16:45:21 -0700833 self.match.wildcards = wildcard_set(self.match.wildcards, \
834 ofp.OFPFW_DL_TYPE, \
835 0 \
836 )
rootf6af1672012-04-06 09:46:29 -0700837
Howard Pershc1199d52012-04-11 14:21:32 -0700838 if wildcard_get(wildcards_force, ofp.OFPFW_NW_TOS) == 0 \
839 and (wildcard_get(valid_wildcards, ofp.OFPFW_NW_TOS) == 0 \
840 or exact \
841 or flip_coin() \
842 ):
rootf6af1672012-04-06 09:46:29 -0700843 self.match.nw_tos = fi.rand_ip_tos()
844 # Specifying a TOS value requires that Ethertype is IP
845 if flip_coin():
846 self.match.dl_type = 0x0800
Howard Persh3340d452012-04-06 16:45:21 -0700847 self.match.wildcards = wildcard_set(self.match.wildcards, \
848 ofp.OFPFW_DL_TYPE, \
849 0 \
850 )
rootf6af1672012-04-06 09:46:29 -0700851 else:
Howard Persh3340d452012-04-06 16:45:21 -0700852 self.match.wildcards = wildcard_set(self.match.wildcards, \
853 ofp.OFPFW_NW_TOS, \
854 1 \
855 )
rootf6af1672012-04-06 09:46:29 -0700856
Dan Talayco910a8282012-04-07 00:05:20 -0700857 # Known issue on OVS with specifying nw_proto w/o dl_type as IP
Howard Pershc1199d52012-04-11 14:21:32 -0700858 if wildcard_get(wildcards_force, ofp.OFPFW_NW_PROTO) == 0 \
859 and (wildcard_get(valid_wildcards, ofp.OFPFW_NW_PROTO) == 0 \
860 or exact \
861 or flip_coin() \
862 ):
Dan Talayco910a8282012-04-07 00:05:20 -0700863 self.match.nw_proto = fi.rand_ip_proto()
864 # Specifying an IP protocol requires that Ethertype is IP
865 if flip_coin():
866 self.match.dl_type = 0x0800
867 self.match.wildcards = wildcard_set(self.match.wildcards, \
868 ofp.OFPFW_DL_TYPE, \
869 0 \
870 )
871 else:
Howard Persh3340d452012-04-06 16:45:21 -0700872 self.match.wildcards = wildcard_set(self.match.wildcards, \
873 ofp.OFPFW_NW_PROTO, \
874 1 \
875 )
Dan Talayco910a8282012-04-07 00:05:20 -0700876
Howard Pershc1199d52012-04-11 14:21:32 -0700877 if wildcard_get(wildcards_force, ofp.OFPFW_TP_SRC) == 0 \
878 and (wildcard_get(valid_wildcards, ofp.OFPFW_TP_SRC) == 0 \
879 or exact\
880 or flip_coin() \
881 ):
rootf6af1672012-04-06 09:46:29 -0700882 self.match.tp_src = fi.rand_l4_port()
883 # Specifying a L4 port requires that IP protcol is
884 # one of {ICMP, TCP, UDP}
885 if flip_coin():
886 self.match.nw_proto = rand_pick([1, 6, 17])
Howard Persh3340d452012-04-06 16:45:21 -0700887 self.match.wildcards = wildcard_set(self.match.wildcards, \
888 ofp.OFPFW_NW_PROTO, \
889 0 \
890 )
rootf6af1672012-04-06 09:46:29 -0700891 # Specifying a L4 port requirues that Ethertype is IP
892 self.match.dl_type = 0x0800
Howard Persh3340d452012-04-06 16:45:21 -0700893 self.match.wildcards = wildcard_set(self.match.wildcards, \
894 ofp.OFPFW_DL_TYPE, \
895 0 \
896 )
rootf6af1672012-04-06 09:46:29 -0700897 if self.match.nw_proto == 1:
898 self.match.tp_src = self.match.tp_src & 0xff
899 else:
Howard Persh3340d452012-04-06 16:45:21 -0700900 self.match.wildcards = wildcard_set(self.match.wildcards, \
901 ofp.OFPFW_TP_SRC, \
902 1 \
903 )
rootf6af1672012-04-06 09:46:29 -0700904
Howard Pershc1199d52012-04-11 14:21:32 -0700905 if wildcard_get(wildcards_force, ofp.OFPFW_TP_DST) == 0 \
906 and (wildcard_get(valid_wildcards, ofp.OFPFW_TP_DST) == 0 \
907 or exact \
908 or flip_coin() \
909 ):
rootf6af1672012-04-06 09:46:29 -0700910 self.match.tp_dst = fi.rand_l4_port()
911 # Specifying a L4 port requires that IP protcol is
912 # one of {ICMP, TCP, UDP}
913 if flip_coin():
914 self.match.nw_proto = rand_pick([1, 6, 17])
Howard Persh3340d452012-04-06 16:45:21 -0700915 self.match.wildcards = wildcard_set(self.match.wildcards, \
916 ofp.OFPFW_NW_PROTO, \
917 0 \
918 )
rootf6af1672012-04-06 09:46:29 -0700919 # Specifying a L4 port requirues that Ethertype is IP
920 self.match.dl_type = 0x0800
Howard Persh3340d452012-04-06 16:45:21 -0700921 self.match.wildcards = wildcard_set(self.match.wildcards, \
922 ofp.OFPFW_DL_TYPE, \
923 0 \
924 )
rootf6af1672012-04-06 09:46:29 -0700925 if self.match.nw_proto == 1:
926 self.match.tp_dst = self.match.tp_dst & 0xff
927 else:
Howard Persh3340d452012-04-06 16:45:21 -0700928 self.match.wildcards = wildcard_set(self.match.wildcards, \
929 ofp.OFPFW_TP_DST, \
930 1 \
931 )
rootf6af1672012-04-06 09:46:29 -0700932
933 # If nothing is wildcarded, it is an exact flow spec -- some switches
Howard Persh3340d452012-04-06 16:45:21 -0700934 # (Open vSwitch, for one) *require* that exact flow specs
935 # have priority 65535.
936 self.priority = 65535 if self.match.wildcards == 0 \
937 else fi.rand_priority()
rootf6af1672012-04-06 09:46:29 -0700938
939 # N.B. Don't make the timeout too short, else the flow might
940 # disappear before we get a chance to check for it.
941 t = random.randint(0, 65535)
942 self.idle_timeout = 0 if t < 60 else t
943 t = random.randint(0, 65535)
944 self.hard_timeout = 0 if t < 60 else t
945
Howard Persh8d21c1f2012-04-20 15:57:29 -0700946 self.rand_mod(fi, valid_actions, valid_ports, valid_queues)
rootf6af1672012-04-06 09:46:29 -0700947
948 return self
949
950 # Return flow cfg in canonical form
Howard Persh3340d452012-04-06 16:45:21 -0700951 # - There are dependencies between flow qualifiers, e.g. it only makes
952 # sense to qualify nw_proto if dl_type is qualified to be 0x0800 (IP).
953 # The canonical form of flow match criteria will "wildcard out"
954 # all such cases.
rootf6af1672012-04-06 09:46:29 -0700955 def canonical(self):
956 result = copy.deepcopy(self)
Howard Persh07d99e62012-04-09 15:26:57 -0700957
958 if wildcard_get(result.match.wildcards, ofp.OFPFW_DL_VLAN) != 0:
959 result.match.wildcards = wildcard_set(result.match.wildcards, \
960 ofp.OFPFW_DL_VLAN_PCP, \
961 1 \
962 )
963
rootf6af1672012-04-06 09:46:29 -0700964 if wildcard_get(result.match.wildcards, ofp.OFPFW_DL_TYPE) != 0 \
965 or result.match.dl_type not in [0x0800, 0x0806]:
Howard Persh3340d452012-04-06 16:45:21 -0700966 # dl_tyoe is wildcarded, or specified as something other
967 # than IP or ARP
Howard Persh07d99e62012-04-09 15:26:57 -0700968 # => nw_src, nw_dst, nw_proto cannot be specified,
969 # must be wildcarded
Howard Persh3340d452012-04-06 16:45:21 -0700970 result.match.wildcards = wildcard_set(result.match.wildcards, \
971 ofp.OFPFW_NW_SRC_MASK, \
972 32 \
973 )
974 result.match.wildcards = wildcard_set(result.match.wildcards, \
975 ofp.OFPFW_NW_DST_MASK, \
976 32 \
977 )
Howard Persh3340d452012-04-06 16:45:21 -0700978 result.match.wildcards = wildcard_set(result.match.wildcards, \
979 ofp.OFPFW_NW_PROTO, \
980 1 \
981 )
Howard Persh07d99e62012-04-09 15:26:57 -0700982
983 if wildcard_get(result.match.wildcards, ofp.OFPFW_DL_TYPE) != 0 \
984 or result.match.dl_type != 0x0800:
985 # dl_type is wildcarded, or specified as something other than IP
986 # => nw_tos, tp_src and tp_dst cannot be specified,
987 # must be wildcarded
Howard Persh3340d452012-04-06 16:45:21 -0700988 result.match.wildcards = wildcard_set(result.match.wildcards, \
989 ofp.OFPFW_NW_TOS, \
990 1 \
991 )
992 result.match.wildcards = wildcard_set(result.match.wildcards, \
993 ofp.OFPFW_TP_SRC, \
994 1 \
995 )
996 result.match.wildcards = wildcard_set(result.match.wildcards, \
997 ofp.OFPFW_TP_DST, \
998 1 \
999 )
Howard Persh07d99e62012-04-09 15:26:57 -07001000 result.match.wildcards = wildcard_set(result.match.wildcards, \
1001 ofp.OFPFW_NW_SRC_MASK, \
1002 32 \
1003 )
1004 result.match.wildcards = wildcard_set(result.match.wildcards, \
1005 ofp.OFPFW_NW_DST_MASK, \
1006 32 \
1007 )
1008 result.match.wildcards = wildcard_set(result.match.wildcards, \
1009 ofp.OFPFW_NW_PROTO, \
1010 1 \
1011 )
1012
rootf6af1672012-04-06 09:46:29 -07001013 if wildcard_get(result.match.wildcards, ofp.OFPFW_NW_PROTO) != 0 \
1014 or result.match.nw_proto not in [1, 6, 17]:
Howard Persh3340d452012-04-06 16:45:21 -07001015 # nw_proto is wildcarded, or specified as something other than ICMP,
1016 # TCP or UDP
rootf6af1672012-04-06 09:46:29 -07001017 # => tp_src and tp_dst cannot be specified, must be wildcarded
Howard Persh3340d452012-04-06 16:45:21 -07001018 result.match.wildcards = wildcard_set(result.match.wildcards, \
1019 ofp.OFPFW_TP_SRC, \
1020 1 \
1021 )
1022 result.match.wildcards = wildcard_set(result.match.wildcards, \
1023 ofp.OFPFW_TP_DST, \
1024 1 \
1025 )
rootf6af1672012-04-06 09:46:29 -07001026 return result
1027
Howard Persh680b92a2012-03-31 13:34:35 -07001028 # Overlap check
1029 # delf == True <=> Check for delete overlap, else add overlap
1030 # "Add overlap" is defined as there exists a packet that could match both the
1031 # receiver and argument flowspecs
1032 # "Delete overlap" is defined as the specificity of the argument flowspec
1033 # is greater than or equal to the specificity of the receiver flowspec
1034 def overlaps(self, x, delf):
rootf6af1672012-04-06 09:46:29 -07001035 if wildcard_get(self.match.wildcards, ofp.OFPFW_IN_PORT) == 0:
1036 if wildcard_get(x.match.wildcards, ofp.OFPFW_IN_PORT) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -07001037 if self.match.in_port != x.match.in_port:
1038 return False # Both specified, and not equal
1039 elif delf:
1040 return False # Receiver more specific
rootf6af1672012-04-06 09:46:29 -07001041 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_VLAN) == 0:
1042 if wildcard_get(x.match.wildcards, ofp.OFPFW_DL_VLAN) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -07001043 if self.match.dl_vlan != x.match.dl_vlan:
1044 return False # Both specified, and not equal
1045 elif delf:
1046 return False # Receiver more specific
rootf6af1672012-04-06 09:46:29 -07001047 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_SRC) == 0:
1048 if wildcard_get(x.match.wildcards, ofp.OFPFW_DL_SRC) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -07001049 if self.match.dl_src != x.match.dl_src:
1050 return False # Both specified, and not equal
1051 elif delf:
1052 return False # Receiver more specific
rootf6af1672012-04-06 09:46:29 -07001053 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_DST) == 0:
1054 if wildcard_get(x.match.wildcards, ofp.OFPFW_DL_DST) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -07001055 if self.match.dl_dst != x.match.dl_dst:
1056 return False # Both specified, and not equal
1057 elif delf:
1058 return False # Receiver more specific
rootf6af1672012-04-06 09:46:29 -07001059 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_TYPE) == 0:
1060 if wildcard_get(x.match.wildcards, ofp.OFPFW_DL_TYPE) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -07001061 if self.match.dl_type != x.match.dl_type:
1062 return False # Both specified, and not equal
1063 elif delf:
1064 return False # Recevier more specific
rootf6af1672012-04-06 09:46:29 -07001065 if wildcard_get(self.match.wildcards, ofp.OFPFW_NW_PROTO) == 0:
1066 if wildcard_get(x.match.wildcards, ofp.OFPFW_NW_PROTO) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -07001067 if self.match.nw_proto != x.match.nw_proto:
1068 return False # Both specified, and not equal
1069 elif delf:
1070 return False # Receiver more specific
rootf6af1672012-04-06 09:46:29 -07001071 if wildcard_get(self.match.wildcards, ofp.OFPFW_TP_SRC) == 0:
1072 if wildcard_get(x.match.wildcards, ofp.OFPFW_TP_SRC) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -07001073 if self.match.tp_src != x.match.tp_src:
1074 return False # Both specified, and not equal
1075 elif delf:
1076 return False # Receiver more specific
rootf6af1672012-04-06 09:46:29 -07001077 if wildcard_get(self.match.wildcards, ofp.OFPFW_TP_DST) == 0:
1078 if wildcard_get(x.match.wildcards, ofp.OFPFW_TP_DST) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -07001079 if self.match.tp_dst != x.match.tp_dst:
1080 return False # Both specified, and not equal
1081 elif delf:
1082 return False # Receiver more specific
rootf6af1672012-04-06 09:46:29 -07001083 na = wildcard_get(self.match.wildcards, ofp.OFPFW_NW_SRC_MASK)
1084 nb = wildcard_get(x.match.wildcards, ofp.OFPFW_NW_SRC_MASK)
Howard Persh680b92a2012-03-31 13:34:35 -07001085 if delf and na < nb:
1086 return False # Receiver more specific
Howard Pershc7963582012-03-29 10:02:59 -07001087 if (na < 32 and nb < 32):
1088 m = ~((1 << na) - 1) & ~((1 << nb) - 1)
1089 if (self.match.nw_src & m) != (x.match.nw_src & m):
Howard Persh680b92a2012-03-31 13:34:35 -07001090 return False # Overlapping bits not equal
rootf6af1672012-04-06 09:46:29 -07001091 na = wildcard_get(self.match.wildcards, ofp.OFPFW_NW_DST_MASK)
1092 nb = wildcard_get(x.match.wildcards, ofp.OFPFW_NW_DST_MASK)
Howard Persh680b92a2012-03-31 13:34:35 -07001093 if delf and na < nb:
1094 return False # Receiver more specific
Howard Pershc7963582012-03-29 10:02:59 -07001095 if (na < 32 and nb < 32):
1096 m = ~((1 << na) - 1) & ~((1 << nb) - 1)
1097 if (self.match.nw_dst & m) != (x.match.nw_dst & m):
rootf6af1672012-04-06 09:46:29 -07001098 return False # Overlapping bits not equal
1099 if wildcard_get(self.match.wildcards, ofp.OFPFW_DL_VLAN_PCP) == 0:
1100 if wildcard_get(x.match.wildcards, ofp.OFPFW_DL_VLAN_PCP) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -07001101 if self.match.dl_vlan_pcp != x.match.dl_vlan_pcp:
1102 return False # Both specified, and not equal
1103 elif delf:
1104 return False # Receiver more specific
rootf6af1672012-04-06 09:46:29 -07001105 if wildcard_get(self.match.wildcards, ofp.OFPFW_NW_TOS) == 0:
1106 if wildcard_get(x.match.wildcards, ofp.OFPFW_NW_TOS) == 0:
Howard Persh680b92a2012-03-31 13:34:35 -07001107 if self.match.nw_tos != x.match.nw_tos:
1108 return False # Both specified, and not equal
1109 elif delf:
1110 return False # Receiver more specific
1111 return True # Flows overlap
Howard Pershc7963582012-03-29 10:02:59 -07001112
1113 def to_flow_mod_msg(self, msg):
1114 msg.match = self.match
rootf6af1672012-04-06 09:46:29 -07001115 msg.cookie = self.cookie
Howard Pershc7963582012-03-29 10:02:59 -07001116 msg.idle_timeout = self.idle_timeout
1117 msg.hard_timeout = self.hard_timeout
1118 msg.priority = self.priority
1119 msg.actions = self.actions
1120 return msg
1121
1122 def from_flow_stat(self, msg):
1123 self.match = msg.match
rootf6af1672012-04-06 09:46:29 -07001124 self.cookie = msg.cookie
Howard Pershc7963582012-03-29 10:02:59 -07001125 self.idle_timeout = msg.idle_timeout
1126 self.hard_timeout = msg.hard_timeout
1127 self.priority = msg.priority
1128 self.actions = msg.actions
1129
rootf6af1672012-04-06 09:46:29 -07001130 def from_flow_rem(self, msg):
1131 self.match = msg.match
1132 self.idle_timeout = msg.idle_timeout
1133 self.priority = msg.priority
Howard Pershc7963582012-03-29 10:02:59 -07001134
Howard Pershc7963582012-03-29 10:02:59 -07001135
rootf6af1672012-04-06 09:46:29 -07001136class Flow_Tbl:
1137 def clear(self):
1138 self.dict = {}
Howard Persh680b92a2012-03-31 13:34:35 -07001139
rootf6af1672012-04-06 09:46:29 -07001140 def __init__(self):
1141 self.clear()
Howard Persh680b92a2012-03-31 13:34:35 -07001142
rootf6af1672012-04-06 09:46:29 -07001143 def find(self, f):
root2843d2b2012-04-06 10:27:46 -07001144 return self.dict.get(f.key_str(), None)
rootf6af1672012-04-06 09:46:29 -07001145
1146 def insert(self, f):
root2843d2b2012-04-06 10:27:46 -07001147 self.dict[f.key_str()] = f
rootf6af1672012-04-06 09:46:29 -07001148
1149 def delete(self, f):
root2843d2b2012-04-06 10:27:46 -07001150 del self.dict[f.key_str()]
rootf6af1672012-04-06 09:46:29 -07001151
1152 def values(self):
1153 return self.dict.values()
1154
1155 def count(self):
1156 return len(self.dict)
1157
Ed Swierk99a74de2012-08-22 06:40:54 -07001158 def rand(self, wildcards_force, sw, fi, num_flows):
rootf6af1672012-04-06 09:46:29 -07001159 self.clear()
1160 i = 0
1161 tbl = 0
1162 j = 0
1163 while i < num_flows:
1164 fc = Flow_Cfg()
1165 fc.rand(fi, \
Ed Swierk99a74de2012-08-22 06:40:54 -07001166 wildcards_force, \
rootf6af1672012-04-06 09:46:29 -07001167 sw.tbl_stats.stats[tbl].wildcards, \
1168 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07001169 sw.valid_ports, \
1170 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07001171 )
1172 fc = fc.canonical()
1173 if self.find(fc):
1174 continue
1175 fc.send_rem = False
1176 self.insert(fc)
1177 i = i + 1
1178 j = j + 1
1179 if j >= sw.tbl_stats.stats[tbl].max_entries:
1180 tbl = tbl + 1
1181 j = 0
1182
1183
1184class Switch:
1185 # Members:
Howard Persh8d21c1f2012-04-20 15:57:29 -07001186 # controller - switch's test controller
1187 # sw_features - switch's OFPT_FEATURES_REPLY message
1188 # valid_ports - list of valid port numbers
1189 # valid_queues - list of valid [port, queue] pairs
1190 # tbl_stats - switch's OFPT_STATS_REPLY message, for table stats request
1191 # queue_stats - switch's OFPT_STATS_REPLY message, for queue stats request
1192 # flow_stats - switch's OFPT_STATS_REPLY message, for flow stats request
1193 # flow_tbl - (test's idea of) switch's flow table
rootf6af1672012-04-06 09:46:29 -07001194
1195 def __init__(self):
Howard Persh8d21c1f2012-04-20 15:57:29 -07001196 self.controller = None
1197 self.sw_features = None
1198 self.valid_ports = []
1199 self.valid_queues = []
1200 self.tbl_stats = None
1201 self.flow_stats = None
1202 self.flow_tbl = Flow_Tbl()
Ed Swierk99a74de2012-08-22 06:40:54 -07001203 self.error_msgs = []
1204 self.removed_msgs = []
1205
1206 def error_handler(self, controller, msg, rawmsg):
Rich Lane9a003812012-10-04 17:17:59 -07001207 logging.info("Got an ERROR message, type=%d, code=%d" \
Ed Swierk99a74de2012-08-22 06:40:54 -07001208 % (msg.type, msg.code) \
1209 )
Rich Lane9a003812012-10-04 17:17:59 -07001210 logging.info("Message header:")
1211 logging.info(msg.header.show())
Ed Swierk99a74de2012-08-22 06:40:54 -07001212 self.error_msgs.append(msg)
1213
1214 def removed_handler(self, controller, msg, rawmsg):
Rich Lane9a003812012-10-04 17:17:59 -07001215 logging.info("Got a REMOVED message")
1216 logging.info("Message header:")
1217 logging.info(msg.header.show())
Ed Swierk99a74de2012-08-22 06:40:54 -07001218 self.removed_msgs.append(msg)
rootf6af1672012-04-06 09:46:29 -07001219
Howard Persh3340d452012-04-06 16:45:21 -07001220 def controller_set(self, controller):
1221 self.controller = controller
1222 # Register error message handler
Ed Swierk99a74de2012-08-22 06:40:54 -07001223 self.error_msgs = []
1224 self.removed_msgs = []
1225 controller.register(ofp.OFPT_ERROR, self.error_handler)
1226 controller.register(ofp.OFPT_FLOW_REMOVED, self.removed_handler)
Howard Persh3340d452012-04-06 16:45:21 -07001227
rootf6af1672012-04-06 09:46:29 -07001228 def features_get(self):
1229 # Get switch features
1230 request = message.features_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -07001231 (self.sw_features, pkt) = self.controller.transact(request)
rootf6af1672012-04-06 09:46:29 -07001232 if self.sw_features is None:
Rich Lane9a003812012-10-04 17:17:59 -07001233 logging.error("Get switch features failed")
rootf6af1672012-04-06 09:46:29 -07001234 return False
1235 self.valid_ports = map(lambda x: x.port_no, self.sw_features.ports)
Rich Lane9a003812012-10-04 17:17:59 -07001236 logging.info("Ports reported by switch:")
1237 logging.info(self.valid_ports)
Rich Lane2014f9b2012-10-05 15:29:40 -07001238 ports_override = test_param_get("ports", [])
Howard Persh8d21c1f2012-04-20 15:57:29 -07001239 if ports_override != []:
Rich Lane9a003812012-10-04 17:17:59 -07001240 logging.info("Overriding ports to:")
1241 logging.info(ports_override)
Howard Persh8d21c1f2012-04-20 15:57:29 -07001242 self.valid_ports = ports_override
1243
Howard Persh3340d452012-04-06 16:45:21 -07001244 # TBD - OFPP_LOCAL is returned by OVS is switch features --
1245 # is that universal?
1246
1247 # TBD - There seems to be variability in which switches support which
1248 # ports; need to sort that out
1249 # TBD - Is it legal to enqueue to a special port? Depends on switch?
1250# self.valid_ports.extend([ofp.OFPP_IN_PORT, \
1251# ofp.OFPP_NORMAL, \
1252# ofp.OFPP_FLOOD, \
1253# ofp.OFPP_ALL, \
1254# ofp.OFPP_CONTROLLER \
1255# ] \
1256# )
Rich Lane9a003812012-10-04 17:17:59 -07001257 logging.info("Supported actions reported by switch:")
1258 logging.info("0x%x=%s" \
Howard Persh8d21c1f2012-04-20 15:57:29 -07001259 % (self.sw_features.actions, \
1260 actions_bmap_to_str(self.sw_features.actions) \
1261 ) \
1262 )
Rich Lane2014f9b2012-10-05 15:29:40 -07001263 actions_override = test_param_get("actions", -1)
Howard Persh07d99e62012-04-09 15:26:57 -07001264 if actions_override != -1:
Rich Lane9a003812012-10-04 17:17:59 -07001265 logging.info("Overriding supported actions to:")
1266 logging.info(actions_bmap_to_str(actions_override))
Howard Persh07d99e62012-04-09 15:26:57 -07001267 self.sw_features.actions = actions_override
rootf6af1672012-04-06 09:46:29 -07001268 return True
1269
1270 def tbl_stats_get(self):
1271 # Get table stats
Howard Persh680b92a2012-03-31 13:34:35 -07001272 request = message.table_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -07001273 (self.tbl_stats, pkt) = self.controller.transact(request)
Howard Persh07d99e62012-04-09 15:26:57 -07001274 if self.tbl_stats is None:
Rich Lane9a003812012-10-04 17:17:59 -07001275 logging.error("Get table stats failed")
Howard Persh07d99e62012-04-09 15:26:57 -07001276 return False
Howard Persh8d21c1f2012-04-20 15:57:29 -07001277 i = 0
Howard Persh07d99e62012-04-09 15:26:57 -07001278 for ts in self.tbl_stats.stats:
Rich Lane9a003812012-10-04 17:17:59 -07001279 logging.info("Supported wildcards for table %d reported by switch:"
Howard Persh8d21c1f2012-04-20 15:57:29 -07001280 % (i)
1281 )
Rich Lane9a003812012-10-04 17:17:59 -07001282 logging.info("0x%x=%s" \
Howard Persh8d21c1f2012-04-20 15:57:29 -07001283 % (ts.wildcards, \
1284 wildcards_to_str(ts.wildcards) \
1285 ) \
1286 )
Rich Lane2014f9b2012-10-05 15:29:40 -07001287 wildcards_override = test_param_get("wildcards", -1)
Howard Persh07d99e62012-04-09 15:26:57 -07001288 if wildcards_override != -1:
Rich Lane9a003812012-10-04 17:17:59 -07001289 logging.info("Overriding supported wildcards for table %d to:"
Howard Persh8d21c1f2012-04-20 15:57:29 -07001290 % (i)
1291 )
Rich Lane9a003812012-10-04 17:17:59 -07001292 logging.info(wildcards_to_str(wildcards_override))
Howard Persh07d99e62012-04-09 15:26:57 -07001293 ts.wildcards = wildcards_override
Howard Persh8d21c1f2012-04-20 15:57:29 -07001294 i = i + 1
Howard Persh07d99e62012-04-09 15:26:57 -07001295 return True
Howard Persh680b92a2012-03-31 13:34:35 -07001296
Howard Persh8d21c1f2012-04-20 15:57:29 -07001297 def queue_stats_get(self):
1298 # Get queue stats
1299 request = message.queue_stats_request()
1300 request.port_no = ofp.OFPP_ALL
1301 request.queue_id = ofp.OFPQ_ALL
Rich Lanec8aaa3e2012-07-26 19:28:02 -07001302 (self.queue_stats, pkt) = self.controller.transact(request)
Howard Persh8d21c1f2012-04-20 15:57:29 -07001303 if self.queue_stats is None:
Rich Lane9a003812012-10-04 17:17:59 -07001304 logging.error("Get queue stats failed")
Howard Persh8d21c1f2012-04-20 15:57:29 -07001305 return False
1306 self.valid_queues = map(lambda x: (x.port_no, x.queue_id), \
1307 self.queue_stats.stats \
1308 )
Rich Lane9a003812012-10-04 17:17:59 -07001309 logging.info("(Port, queue) pairs reported by switch:")
1310 logging.info(self.valid_queues)
Rich Lane2014f9b2012-10-05 15:29:40 -07001311 queues_override = test_param_get("queues", [])
Howard Persh8d21c1f2012-04-20 15:57:29 -07001312 if queues_override != []:
Rich Lane9a003812012-10-04 17:17:59 -07001313 logging.info("Overriding (port, queue) pairs to:")
1314 logging.info(queues_override)
Howard Persh8d21c1f2012-04-20 15:57:29 -07001315 self.valid_queues = queues_override
1316 return True
1317
1318 def connect(self, controller):
1319 # Connect to controller, and get all switch capabilities
1320 self.controller_set(controller)
1321 return (self.features_get() \
1322 and self.tbl_stats_get() \
1323 and self.queue_stats_get() \
1324 )
1325
Howard Pershc1199d52012-04-11 14:21:32 -07001326 def flow_stats_get(self, limit = 10000):
rootf6af1672012-04-06 09:46:29 -07001327 request = message.flow_stats_request()
Howard Persh680b92a2012-03-31 13:34:35 -07001328 query_match = ofp.ofp_match()
1329 query_match.wildcards = ofp.OFPFW_ALL
rootf6af1672012-04-06 09:46:29 -07001330 request.match = query_match
1331 request.table_id = 0xff
1332 request.out_port = ofp.OFPP_NONE;
Howard Persh3340d452012-04-06 16:45:21 -07001333 if self.controller.message_send(request) == -1:
1334 return False
1335 # <TBD>
1336 # Glue together successive reponse messages for stats reply.
1337 # Looking at the "more" flag and performing re-assembly
1338 # should be a part of the infrastructure.
1339 # </TBD>
1340 n = 0
1341 while True:
Dan Talaycoc689a792012-09-28 14:22:53 -07001342 (resp, pkt) = self.controller.poll(ofp.OFPT_STATS_REPLY)
Howard Persh3340d452012-04-06 16:45:21 -07001343 if resp is None:
Howard Pershc1199d52012-04-11 14:21:32 -07001344 return False # Did not get expected response
Howard Persh3340d452012-04-06 16:45:21 -07001345 if n == 0:
1346 self.flow_stats = resp
1347 else:
1348 self.flow_stats.stats.extend(resp.stats)
1349 n = n + 1
Howard Pershc1199d52012-04-11 14:21:32 -07001350 if len(self.flow_stats.stats) > limit:
Rich Lane9a003812012-10-04 17:17:59 -07001351 logging.error("Too many flows returned")
Howard Pershc1199d52012-04-11 14:21:32 -07001352 return False
1353 if (resp.flags & 1) == 0:
1354 break # No more responses expected
Howard Persh3340d452012-04-06 16:45:21 -07001355 return (n > 0)
Howard Persh680b92a2012-03-31 13:34:35 -07001356
rootf6af1672012-04-06 09:46:29 -07001357 def flow_add(self, flow_cfg, overlapf = False):
Howard Persh680b92a2012-03-31 13:34:35 -07001358 flow_mod_msg = message.flow_mod()
1359 flow_mod_msg.command = ofp.OFPFC_ADD
1360 flow_mod_msg.buffer_id = 0xffffffff
rootf6af1672012-04-06 09:46:29 -07001361 flow_cfg.to_flow_mod_msg(flow_mod_msg)
Howard Persh680b92a2012-03-31 13:34:35 -07001362 if overlapf:
1363 flow_mod_msg.flags = flow_mod_msg.flags | ofp.OFPFF_CHECK_OVERLAP
rootf6af1672012-04-06 09:46:29 -07001364 if flow_cfg.send_rem:
Howard Persh3340d452012-04-06 16:45:21 -07001365 flow_mod_msg.flags = flow_mod_msg.flags | ofp.OFPFF_SEND_FLOW_REM
Howard Persh07d99e62012-04-09 15:26:57 -07001366 flow_mod_msg.header.xid = random.randrange(1,0xffffffff)
Rich Lane9a003812012-10-04 17:17:59 -07001367 logging.info("Sending flow_mod(add), xid=%d"
Howard Pershc1199d52012-04-11 14:21:32 -07001368 % (flow_mod_msg.header.xid)
1369 )
rootf6af1672012-04-06 09:46:29 -07001370 return (self.controller.message_send(flow_mod_msg) != -1)
Howard Persh680b92a2012-03-31 13:34:35 -07001371
rootf6af1672012-04-06 09:46:29 -07001372 def flow_mod(self, flow_cfg, strictf):
Howard Persh680b92a2012-03-31 13:34:35 -07001373 flow_mod_msg = message.flow_mod()
Howard Persh3340d452012-04-06 16:45:21 -07001374 flow_mod_msg.command = ofp.OFPFC_MODIFY_STRICT if strictf \
1375 else ofp.OFPFC_MODIFY
Howard Persh680b92a2012-03-31 13:34:35 -07001376 flow_mod_msg.buffer_id = 0xffffffff
rootf6af1672012-04-06 09:46:29 -07001377 flow_cfg.to_flow_mod_msg(flow_mod_msg)
Howard Persh07d99e62012-04-09 15:26:57 -07001378 flow_mod_msg.header.xid = random.randrange(1,0xffffffff)
Rich Lane9a003812012-10-04 17:17:59 -07001379 logging.info("Sending flow_mod(mod), xid=%d"
Howard Pershc1199d52012-04-11 14:21:32 -07001380 % (flow_mod_msg.header.xid)
1381 )
rootf6af1672012-04-06 09:46:29 -07001382 return (self.controller.message_send(flow_mod_msg) != -1)
1383
1384 def flow_del(self, flow_cfg, strictf):
1385 flow_mod_msg = message.flow_mod()
Howard Persh3340d452012-04-06 16:45:21 -07001386 flow_mod_msg.command = ofp.OFPFC_DELETE_STRICT if strictf \
1387 else ofp.OFPFC_DELETE
rootf6af1672012-04-06 09:46:29 -07001388 flow_mod_msg.buffer_id = 0xffffffff
1389 # TBD - "out_port" filtering of deletes needs to be tested
Howard Persh680b92a2012-03-31 13:34:35 -07001390 flow_mod_msg.out_port = ofp.OFPP_NONE
rootf6af1672012-04-06 09:46:29 -07001391 flow_cfg.to_flow_mod_msg(flow_mod_msg)
Howard Persh07d99e62012-04-09 15:26:57 -07001392 flow_mod_msg.header.xid = random.randrange(1,0xffffffff)
Rich Lane9a003812012-10-04 17:17:59 -07001393 logging.info("Sending flow_mod(del), xid=%d"
Howard Pershc1199d52012-04-11 14:21:32 -07001394 % (flow_mod_msg.header.xid)
1395 )
rootf6af1672012-04-06 09:46:29 -07001396 return (self.controller.message_send(flow_mod_msg) != -1)
1397
1398 def barrier(self):
1399 barrier = message.barrier_request()
Dan Talaycoc689a792012-09-28 14:22:53 -07001400 (resp, pkt) = self.controller.transact(barrier, 30)
rootf6af1672012-04-06 09:46:29 -07001401 return (resp is not None)
1402
Howard Persh3340d452012-04-06 16:45:21 -07001403 def errors_verify(self, num_exp, type = 0, code = 0):
rootf6af1672012-04-06 09:46:29 -07001404 result = True
Rich Lane9a003812012-10-04 17:17:59 -07001405 logging.info("Expecting %d error messages" % (num_exp))
Ed Swierk99a74de2012-08-22 06:40:54 -07001406 num_got = len(self.error_msgs)
Rich Lane9a003812012-10-04 17:17:59 -07001407 logging.info("Got %d error messages" % (num_got))
Howard Persh3340d452012-04-06 16:45:21 -07001408 if num_got != num_exp:
Rich Lane9a003812012-10-04 17:17:59 -07001409 logging.error("Incorrect number of error messages received")
Howard Persh3340d452012-04-06 16:45:21 -07001410 result = False
1411 if num_exp == 0:
1412 return result
1413 elif num_exp == 1:
Rich Lane9a003812012-10-04 17:17:59 -07001414 logging.info("Expecting error message, type=%d, code=%d" \
Howard Persh3340d452012-04-06 16:45:21 -07001415 % (type, code) \
1416 )
1417 f = False
Ed Swierk99a74de2012-08-22 06:40:54 -07001418 for e in self.error_msgs:
Howard Persh3340d452012-04-06 16:45:21 -07001419 if e.type == type and e.code == code:
Rich Lane9a003812012-10-04 17:17:59 -07001420 logging.info("Got it")
Howard Persh3340d452012-04-06 16:45:21 -07001421 f = True
1422 if not f:
Rich Lane9a003812012-10-04 17:17:59 -07001423 logging.error("Did not get it")
rootf6af1672012-04-06 09:46:29 -07001424 result = False
Howard Persh3340d452012-04-06 16:45:21 -07001425 else:
Rich Lane9a003812012-10-04 17:17:59 -07001426 logging.error("Can't expect more than 1 error message type")
rootf6af1672012-04-06 09:46:29 -07001427 result = False
1428 return result
1429
Howard Persh3340d452012-04-06 16:45:21 -07001430 def removed_verify(self, num_exp):
1431 result = True
Rich Lane9a003812012-10-04 17:17:59 -07001432 logging.info("Expecting %d removed messages" % (num_exp))
Ed Swierk99a74de2012-08-22 06:40:54 -07001433 num_got = len(self.removed_msgs)
Rich Lane9a003812012-10-04 17:17:59 -07001434 logging.info("Got %d removed messages" % (num_got))
Howard Persh3340d452012-04-06 16:45:21 -07001435 if num_got != num_exp:
Rich Lane9a003812012-10-04 17:17:59 -07001436 logging.error("Incorrect number of removed messages received")
Howard Persh3340d452012-04-06 16:45:21 -07001437 result = False
1438 if num_exp < 2:
1439 return result
Rich Lane9a003812012-10-04 17:17:59 -07001440 logging.error("Can't expect more than 1 error message type")
Howard Persh3340d452012-04-06 16:45:21 -07001441 return False
1442
Howard Persh5f3c83f2012-04-13 09:57:10 -07001443 # modf == True <=> Verify for flow modify, else for add/delete
1444 def flow_tbl_verify(self, modf = False):
rootf6af1672012-04-06 09:46:29 -07001445 result = True
1446
1447 # Verify flow count in switch
Rich Lane9a003812012-10-04 17:17:59 -07001448 logging.info("Reading table stats")
1449 logging.info("Expecting %d flows" % (self.flow_tbl.count()))
rootf6af1672012-04-06 09:46:29 -07001450 if not self.tbl_stats_get():
Rich Lane9a003812012-10-04 17:17:59 -07001451 logging.error("Get table stats failed")
rootf6af1672012-04-06 09:46:29 -07001452 return False
1453 n = 0
1454 for ts in self.tbl_stats.stats:
1455 n = n + ts.active_count
Rich Lane9a003812012-10-04 17:17:59 -07001456 logging.info("Table stats reported %d active flows" \
rootf6af1672012-04-06 09:46:29 -07001457 % (n) \
1458 )
1459 if n != self.flow_tbl.count():
Rich Lane9a003812012-10-04 17:17:59 -07001460 logging.error("Incorrect number of active flows reported")
rootf6af1672012-04-06 09:46:29 -07001461 result = False
1462
1463 # Read flows from switch
Rich Lane9a003812012-10-04 17:17:59 -07001464 logging.info("Retrieving flows from switch")
1465 logging.info("Expecting %d flows" % (self.flow_tbl.count()))
rootf6af1672012-04-06 09:46:29 -07001466 if not self.flow_stats_get():
Rich Lane9a003812012-10-04 17:17:59 -07001467 logging.error("Get flow stats failed")
rootf6af1672012-04-06 09:46:29 -07001468 return False
Rich Lane9a003812012-10-04 17:17:59 -07001469 logging.info("Retrieved %d flows" % (len(self.flow_stats.stats)))
rootf6af1672012-04-06 09:46:29 -07001470
1471 # Verify flows returned by switch
1472
1473 if len(self.flow_stats.stats) != self.flow_tbl.count():
Rich Lane9a003812012-10-04 17:17:59 -07001474 logging.error("Switch reported incorrect number of flows")
rootf6af1672012-04-06 09:46:29 -07001475 result = False
1476
Rich Lane9a003812012-10-04 17:17:59 -07001477 logging.info("Verifying received flows")
rootf6af1672012-04-06 09:46:29 -07001478 for fc in self.flow_tbl.values():
1479 fc.matched = False
1480 for fs in self.flow_stats.stats:
1481 flow_in = Flow_Cfg()
1482 flow_in.from_flow_stat(fs)
Rich Lane9a003812012-10-04 17:17:59 -07001483 logging.info("Received flow:")
1484 logging.info(str(flow_in))
rootf6af1672012-04-06 09:46:29 -07001485 fc = self.flow_tbl.find(flow_in)
1486 if fc is None:
Rich Lane9a003812012-10-04 17:17:59 -07001487 logging.error("Received flow:")
1488 logging.error(str(flow_in))
1489 logging.error("does not match any defined flow")
rootf6af1672012-04-06 09:46:29 -07001490 result = False
1491 elif fc.matched:
Rich Lane9a003812012-10-04 17:17:59 -07001492 logging.error("Received flow:")
1493 logging.error(str(flow_in))
1494 logging.error("re-matches defined flow:")
1495 logging.info(str(fc))
rootf6af1672012-04-06 09:46:29 -07001496 result = False
1497 else:
Rich Lane9a003812012-10-04 17:17:59 -07001498 logging.info("matched")
Howard Persh5f3c83f2012-04-13 09:57:10 -07001499 if modf:
1500 # Check for modify
1501
1502 if flow_in.cookie != fc.cookie:
Rich Lane9a003812012-10-04 17:17:59 -07001503 logging.warning("Defined flow:")
1504 logging.warning(str(fc))
1505 logging.warning("Received flow:")
1506 logging.warning(str(flow_in))
1507 logging.warning("cookies do not match")
Howard Persh5f3c83f2012-04-13 09:57:10 -07001508 if not flow_in.actions_equal(fc):
Rich Lane9a003812012-10-04 17:17:59 -07001509 logging.error("Defined flow:")
1510 logging.error(str(fc))
1511 logging.error("Received flow:")
1512 logging.error(str(flow_in))
1513 logging.error("actions do not match")
Howard Persh5f3c83f2012-04-13 09:57:10 -07001514 else:
1515 # Check for add/delete
1516
1517 if not flow_in == fc:
Rich Lane9a003812012-10-04 17:17:59 -07001518 logging.error("Defined flow:")
1519 logging.error(str(fc))
1520 logging.error("Received flow:")
1521 logging.error(str(flow_in))
1522 logging.error("non-key portions of flow do not match")
Howard Persh5f3c83f2012-04-13 09:57:10 -07001523 result = False
rootf6af1672012-04-06 09:46:29 -07001524 fc.matched = True
1525 for fc in self.flow_tbl.values():
1526 if not fc.matched:
Rich Lane9a003812012-10-04 17:17:59 -07001527 logging.error("Defined flow:")
1528 logging.error(str(fc))
1529 logging.error("was not returned by switch")
rootf6af1672012-04-06 09:46:29 -07001530 result = False
1531
1532 return result
Howard Persh680b92a2012-03-31 13:34:35 -07001533
Howard Persh9cab4822012-09-11 17:08:40 -07001534 def settle(self):
1535 time.sleep(2)
1536
Howard Persh07d99e62012-04-09 15:26:57 -07001537# FLOW ADD 5
1538#
1539# OVERVIEW
1540# Add flows to switch, read back and verify flow configurations
1541#
1542# PURPOSE
1543# - Test acceptance of flow adds
1544# - Test ability of switch to process additions to flow table in random
1545# priority order
1546# - Test correctness of flow configuration responses
1547#
1548# PARAMETERS
1549#
1550# Name: num_flows
1551# Type: number
1552# Description:
1553# Number of flows to define; 0 => maximum number of flows, as determined
1554# from switch capabilities
1555# Default: 100
1556#
1557# PROCESS
1558# 1. Delete all flows from switch
1559# 2. Generate <num_flows> distinct flow configurations
1560# 3. Send <num_flows> flow adds to switch, for flows generated in step 2 above
1561# 4. Verify that no OFPT_ERROR responses were generated by switch
1562# 5. Retrieve flow stats from switch
1563# 6. Compare flow configurations returned by switch
1564# 7. Test PASSED iff all flows sent to switch in step 3 above are returned
1565# in step 5 above; else test FAILED
Howard Persh680b92a2012-03-31 13:34:35 -07001566
Rich Laneb90a1c42012-10-05 09:16:05 -07001567class Flow_Add_5(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07001568 """
1569 Test FLOW_ADD_5 from draft top-half test plan
1570
1571 INPUTS
1572 num_flows - Number of flows to generate
1573 """
Howard Persh680b92a2012-03-31 13:34:35 -07001574
rootf6af1672012-04-06 09:46:29 -07001575 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07001576 logging.info("Flow_Add_5 TEST BEGIN")
Howard Pershc7963582012-03-29 10:02:59 -07001577
Rich Lane2014f9b2012-10-05 15:29:40 -07001578 num_flows = test_param_get("num_flows", 100)
Howard Persh3340d452012-04-06 16:45:21 -07001579
Howard Pershc7963582012-03-29 10:02:59 -07001580 # Clear all flows from switch
rootf6af1672012-04-06 09:46:29 -07001581
Rich Lane9a003812012-10-04 17:17:59 -07001582 logging.info("Deleting all flows from switch")
1583 rc = delete_all_flows(self.controller)
Howard Pershc7963582012-03-29 10:02:59 -07001584 self.assertEqual(rc, 0, "Failed to delete all flows")
1585
rootf6af1672012-04-06 09:46:29 -07001586 # Get switch capabilites
Howard Pershc7963582012-03-29 10:02:59 -07001587
rootf6af1672012-04-06 09:46:29 -07001588 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07001589 self.assertTrue(sw.connect(self.controller), \
1590 "Failed to connect to switch" \
1591 )
Howard Pershc7963582012-03-29 10:02:59 -07001592
rootf6af1672012-04-06 09:46:29 -07001593 if num_flows == 0:
1594 # Number of flows requested was 0
1595 # => Generate max number of flows
Howard Pershc7963582012-03-29 10:02:59 -07001596
rootf6af1672012-04-06 09:46:29 -07001597 for ts in sw.tbl_stats.stats:
1598 num_flows = num_flows + ts.max_entries
Howard Pershc7963582012-03-29 10:02:59 -07001599
Rich Lane9a003812012-10-04 17:17:59 -07001600 logging.info("Generating %d flows" % (num_flows))
Howard Persh680b92a2012-03-31 13:34:35 -07001601
1602 # Dream up some flow information, i.e. space to chose from for
1603 # random flow parameter generation
Howard Pershc7963582012-03-29 10:02:59 -07001604
rootf6af1672012-04-06 09:46:29 -07001605 fi = Flow_Info()
Howard Persh07d99e62012-04-09 15:26:57 -07001606 fi.rand(max(2 * int(math.log(num_flows)), 1))
Howard Pershc7963582012-03-29 10:02:59 -07001607
rootf6af1672012-04-06 09:46:29 -07001608 # Create a flow table
Howard Persh680b92a2012-03-31 13:34:35 -07001609
rootf6af1672012-04-06 09:46:29 -07001610 ft = Flow_Tbl()
Ed Swierk99a74de2012-08-22 06:40:54 -07001611 ft.rand(required_wildcards(self), sw, fi, num_flows)
Howard Persh680b92a2012-03-31 13:34:35 -07001612
rootf6af1672012-04-06 09:46:29 -07001613 # Send flow table to switch
Howard Persh680b92a2012-03-31 13:34:35 -07001614
Rich Lane9a003812012-10-04 17:17:59 -07001615 logging.info("Sending flow adds to switch")
rootf6af1672012-04-06 09:46:29 -07001616 for fc in ft.values(): # Randomizes order of sending
Rich Lane9a003812012-10-04 17:17:59 -07001617 logging.info("Adding flow:")
1618 logging.info(str(fc));
rootf6af1672012-04-06 09:46:29 -07001619 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
Howard Persh680b92a2012-03-31 13:34:35 -07001620
rootf6af1672012-04-06 09:46:29 -07001621 # Do barrier, to make sure all flows are in
Howard Persh680b92a2012-03-31 13:34:35 -07001622
rootf6af1672012-04-06 09:46:29 -07001623 self.assertTrue(sw.barrier(), "Barrier failed")
Howard Pershc7963582012-03-29 10:02:59 -07001624
rootf6af1672012-04-06 09:46:29 -07001625 result = True
Howard Pershc7963582012-03-29 10:02:59 -07001626
Howard Persh9cab4822012-09-11 17:08:40 -07001627 sw.settle() # Allow switch to settle and generate any notifications
1628
rootf6af1672012-04-06 09:46:29 -07001629 # Check for any error messages
Howard Pershc7963582012-03-29 10:02:59 -07001630
rootf6af1672012-04-06 09:46:29 -07001631 if not sw.errors_verify(0):
1632 result = False
Howard Pershc7963582012-03-29 10:02:59 -07001633
rootf6af1672012-04-06 09:46:29 -07001634 # Verify flow table
Howard Pershc7963582012-03-29 10:02:59 -07001635
rootf6af1672012-04-06 09:46:29 -07001636 sw.flow_tbl = ft
1637 if not sw.flow_tbl_verify():
1638 result = False
Howard Pershc7963582012-03-29 10:02:59 -07001639
rootf6af1672012-04-06 09:46:29 -07001640 self.assertTrue(result, "Flow_Add_5 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07001641 logging.info("Flow_Add_5 TEST PASSED")
Howard Pershc7963582012-03-29 10:02:59 -07001642
Howard Pershc7963582012-03-29 10:02:59 -07001643
Howard Persh07d99e62012-04-09 15:26:57 -07001644# FLOW ADD 5_1
1645#
1646# OVERVIEW
1647# Verify handling of non-canonical flows
1648#
1649# PURPOSE
1650# - Test that switch detects and correctly responds to a non-canonical flow
1651# definition. A canonical flow is one that satisfies all match qualifier
1652# dependencies; a non-canonical flow is one that does not.
1653#
1654# PARAMETERS
1655# - None
1656#
1657# PROCESS
1658# 1. Delete all flows from switch
1659# 2. Generate 1 flow definition, which is different from its canonicalization
1660# 3. Send flow to switch
1661# 4. Retrieve flow from switch
1662# 5. Compare returned flow to canonical form of defined flow
1663# 7. Test PASSED iff flow received in step 4 above is identical to canonical form of flow defined in step 3 above
1664
1665# Disabled.
1666# Should be DUT dependent.
Howard Persh07d99e62012-04-09 15:26:57 -07001667
Rich Laneb90a1c42012-10-05 09:16:05 -07001668class Flow_Add_5_1(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07001669 """
1670 Test FLOW_ADD_5.1 from draft top-half test plan
1671
1672 INPUTS
1673 None
1674 """
Rich Laned1d9c282012-10-04 22:07:10 -07001675
1676 priority = -1
rootf6af1672012-04-06 09:46:29 -07001677
1678 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07001679 logging.info("Flow_Add_5_1 TEST BEGIN")
rootf6af1672012-04-06 09:46:29 -07001680
Rich Lane2014f9b2012-10-05 15:29:40 -07001681 num_flows = test_param_get("num_flows", 100)
rootf6af1672012-04-06 09:46:29 -07001682
1683 # Clear all flows from switch
1684
Rich Lane9a003812012-10-04 17:17:59 -07001685 logging.info("Deleting all flows from switch")
1686 rc = delete_all_flows(self.controller)
rootf6af1672012-04-06 09:46:29 -07001687 self.assertEqual(rc, 0, "Failed to delete all flows")
1688
1689 # Get switch capabilites
1690
rootf6af1672012-04-06 09:46:29 -07001691 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07001692 self.assertTrue(sw.connect(self.controller), \
1693 "Failed to connect to switch" \
1694 )
rootf6af1672012-04-06 09:46:29 -07001695
1696 # Dream up some flow information, i.e. space to chose from for
1697 # random flow parameter generation
1698
1699 fi = Flow_Info()
1700 fi.rand(10)
1701
1702 # Dream up a flow config that will be canonicalized by the switch
1703
1704 while True:
1705 fc = Flow_Cfg()
1706 fc.rand(fi, \
Ed Swierk99a74de2012-08-22 06:40:54 -07001707 required_wildcards(self), \
rootf6af1672012-04-06 09:46:29 -07001708 sw.tbl_stats.stats[0].wildcards, \
1709 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07001710 sw.valid_ports, \
1711 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07001712 )
1713 fcc = fc.canonical()
Howard Persh07d99e62012-04-09 15:26:57 -07001714 if fcc.match != fc.match:
rootf6af1672012-04-06 09:46:29 -07001715 break
1716
1717 ft = Flow_Tbl()
1718 ft.insert(fcc)
1719
1720 # Send it to the switch
1721
Rich Lane9a003812012-10-04 17:17:59 -07001722 logging.info("Sending flow add to switch:")
1723 logging.info(str(fc))
1724 logging.info("should be canonicalized as:")
1725 logging.info(str(fcc))
rootf6af1672012-04-06 09:46:29 -07001726 fc.send_rem = False
1727 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
1728
1729 # Do barrier, to make sure all flows are in
1730
1731 self.assertTrue(sw.barrier(), "Barrier failed")
1732
1733 result = True
1734
Howard Persh9cab4822012-09-11 17:08:40 -07001735 sw.settle() # Allow switch to settle and generate any notifications
1736
rootf6af1672012-04-06 09:46:29 -07001737 # Check for any error messages
1738
1739 if not sw.errors_verify(0):
1740 result = False
1741
1742 # Verify flow table
1743
1744 sw.flow_tbl = ft
1745 if not sw.flow_tbl_verify():
1746 result = False
1747
1748 self.assertTrue(result, "Flow_Add_5_1 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07001749 logging.info("Flow_Add_5_1 TEST PASSED")
rootf6af1672012-04-06 09:46:29 -07001750
1751
Howard Persh07d99e62012-04-09 15:26:57 -07001752# FLOW ADD 6
1753#
1754# OVERVIEW
1755# Test flow table capacity
1756#
1757# PURPOSE
1758# - Test switch can accept as many flow definitions as it claims
1759# - Test generation of OFPET_FLOW_MOD_FAILED/OFPFMFC_ALL_TABLES_FULL
1760# - Test that attempting to create flows beyond capacity does not corrupt
1761# flow table
1762#
1763# PARAMETERS
1764# None
1765#
1766# PROCESS
1767# 1. Delete all flows from switch
1768# 2. Send OFPT_FEATURES_REQUEST and OFPT_STATS_REQUEST/OFPST_TABLE enquiries
1769# to determine flow table size, N
1770# 3. Generate (N + 1) distinct flow configurations
1771# 4. Send N flow adds to switch, for flows generated in step 3 above
1772# 5. Verify flow table in switch
1773# 6. Send one more flow add to switch
1774# 7. Verify that OFPET_FLOW_MOD_FAILED/OFPFMFC_ALL_TABLES_FULL error
1775# response was generated by switch, for last flow mod sent
1776# 7. Retrieve flow stats from switch
1777# 8. Verify flow table in switch
1778# 9. Test PASSED iff:
1779# - error message received, for correct flow
1780# - last flow definition sent to switch is not in flow table
1781# else test FAILED
1782
Howard Persh3340d452012-04-06 16:45:21 -07001783# Disabled because of bogus capacity reported by OVS.
1784# Should be DUT dependent.
Howard Persh3340d452012-04-06 16:45:21 -07001785
Rich Laneb90a1c42012-10-05 09:16:05 -07001786class Flow_Add_6(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07001787 """
1788 Test FLOW_ADD_6 from draft top-half test plan
1789
1790 INPUTS
1791 num_flows - Number of flows to generate
1792 """
Howard Pershc7963582012-03-29 10:02:59 -07001793
Rich Laned1d9c282012-10-04 22:07:10 -07001794 priority = -1
1795
Howard Pershc7963582012-03-29 10:02:59 -07001796 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07001797 logging.info("Flow_Add_6 TEST BEGIN")
Howard Pershc7963582012-03-29 10:02:59 -07001798
rootf6af1672012-04-06 09:46:29 -07001799 # Clear all flows from switch
Howard Pershc7963582012-03-29 10:02:59 -07001800
Rich Lane9a003812012-10-04 17:17:59 -07001801 logging.info("Deleting all flows from switch")
1802 rc = delete_all_flows(self.controller)
rootf6af1672012-04-06 09:46:29 -07001803 self.assertEqual(rc, 0, "Failed to delete all flows")
1804
1805 # Get switch capabilites
1806
rootf6af1672012-04-06 09:46:29 -07001807 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07001808 self.assertTrue(sw.connect(self.controller), \
1809 "Failed to connect to switch" \
1810 )
rootf6af1672012-04-06 09:46:29 -07001811
root2843d2b2012-04-06 10:27:46 -07001812 num_flows = 0
rootf6af1672012-04-06 09:46:29 -07001813 for ts in sw.tbl_stats.stats:
1814 num_flows = num_flows + ts.max_entries
1815
Rich Lane9a003812012-10-04 17:17:59 -07001816 logging.info("Switch capacity is %d flows" % (num_flows))
1817 logging.info("Generating %d flows" % (num_flows))
rootf6af1672012-04-06 09:46:29 -07001818
1819 # Dream up some flow information, i.e. space to chose from for
1820 # random flow parameter generation
1821
1822 fi = Flow_Info()
Howard Persh07d99e62012-04-09 15:26:57 -07001823 fi.rand(max(2 * int(math.log(num_flows)), 1))
rootf6af1672012-04-06 09:46:29 -07001824
1825 # Create a flow table, to switch's capacity
1826
1827 ft = Flow_Tbl()
Ed Swierk99a74de2012-08-22 06:40:54 -07001828 ft.rand(required_wildcards(self), sw, fi, num_flows)
rootf6af1672012-04-06 09:46:29 -07001829
1830 # Send flow table to switch
1831
Rich Lane9a003812012-10-04 17:17:59 -07001832 logging.info("Sending flow adds to switch")
rootf6af1672012-04-06 09:46:29 -07001833 for fc in ft.values(): # Randomizes order of sending
Rich Lane9a003812012-10-04 17:17:59 -07001834 logging.info("Adding flow:")
1835 logging.info(str(fc));
rootf6af1672012-04-06 09:46:29 -07001836 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
1837
1838 # Do barrier, to make sure all flows are in
1839
1840 self.assertTrue(sw.barrier(), "Barrier failed")
1841
1842 result = True
1843
Howard Persh9cab4822012-09-11 17:08:40 -07001844 sw.settle() # Allow switch to settle and generate any notifications
1845
rootf6af1672012-04-06 09:46:29 -07001846 # Check for any error messages
1847
1848 if not sw.errors_verify(0):
1849 result = False
1850
1851 # Dream up one more flow
1852
Rich Lane9a003812012-10-04 17:17:59 -07001853 logging.info("Creating one more flow")
rootf6af1672012-04-06 09:46:29 -07001854 while True:
1855 fc = Flow_Cfg()
1856 fc.rand(fi, \
Ed Swierk99a74de2012-08-22 06:40:54 -07001857 required_wildcards(self), \
Howard Persh07d99e62012-04-09 15:26:57 -07001858 sw.tbl_stats.stats[0].wildcards, \
rootf6af1672012-04-06 09:46:29 -07001859 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07001860 sw.valid_ports, \
1861 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07001862 )
1863 fc = fc.canonical()
Howard Persh07d99e62012-04-09 15:26:57 -07001864 if not ft.find(fc):
1865 break
rootf6af1672012-04-06 09:46:29 -07001866
1867 # Send one-more flow
1868
1869 fc.send_rem = False
Rich Lane9a003812012-10-04 17:17:59 -07001870 logging.info("Sending flow add switch")
1871 logging.info(str(fc));
rootf6af1672012-04-06 09:46:29 -07001872 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
1873
1874 # Do barrier, to make sure all flows are in
1875
1876 self.assertTrue(sw.barrier(), "Barrier failed")
1877
Howard Persh9cab4822012-09-11 17:08:40 -07001878 sw.settle() # Allow switch to settle and generate any notifications
1879
rootf6af1672012-04-06 09:46:29 -07001880 # Check for expected error message
1881
1882 if not sw.errors_verify(1, \
1883 ofp.OFPET_FLOW_MOD_FAILED, \
1884 ofp.OFPFMFC_ALL_TABLES_FULL \
1885 ):
1886 result = False
1887
1888 # Verify flow table
1889
1890 sw.flow_tbl = ft
1891 if not sw.flow_tbl_verify():
1892 result = False
1893
1894 self.assertTrue(result, "Flow_add_6 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07001895 logging.info("Flow_add_6 TEST PASSED")
rootf6af1672012-04-06 09:46:29 -07001896
1897
Howard Persh07d99e62012-04-09 15:26:57 -07001898# FLOW ADD 7
1899#
1900# OVERVIEW
1901# Test flow redefinition
1902#
1903# PURPOSE
1904# Verify that successive flow adds with same priority and match criteria
1905# overwrite in flow table
1906#
1907# PARAMETERS
1908# None
1909#
1910# PROCESS
1911# 1. Delete all flows from switch
1912# 2. Generate flow definition F1
1913# 3. Generate flow definition F2, with same key (priority and match) as F1,
1914# but with different actions
1915# 4. Send flow adds for F1 and F2 to switch
1916# 5. Verify flow definitions in switch
1917# 6. Test PASSED iff 1 flow returned by switch, matching configuration of F2;
1918# else test FAILED
1919
Rich Laneb90a1c42012-10-05 09:16:05 -07001920class Flow_Add_7(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07001921 """
1922 Test FLOW_ADD_7 from draft top-half test plan
1923
1924 INPUTS
1925 None
1926 """
1927
1928 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07001929 logging.info("Flow_Add_7 TEST BEGIN")
rootf6af1672012-04-06 09:46:29 -07001930
1931 # Clear all flows from switch
1932
Rich Lane9a003812012-10-04 17:17:59 -07001933 logging.info("Deleting all flows from switch")
1934 rc = delete_all_flows(self.controller)
rootf6af1672012-04-06 09:46:29 -07001935 self.assertEqual(rc, 0, "Failed to delete all flows")
1936
1937 # Get switch capabilites
1938
rootf6af1672012-04-06 09:46:29 -07001939 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07001940 self.assertTrue(sw.connect(self.controller), \
1941 "Failed to connect to switch" \
1942 )
rootf6af1672012-04-06 09:46:29 -07001943
1944 # Dream up some flow information, i.e. space to chose from for
1945 # random flow parameter generation
1946
1947 fi = Flow_Info()
1948 fi.rand(10)
1949
1950 # Dream up a flow config
1951
1952 fc = Flow_Cfg()
1953 fc.rand(fi, \
Ed Swierk99a74de2012-08-22 06:40:54 -07001954 required_wildcards(self), \
rootf6af1672012-04-06 09:46:29 -07001955 sw.tbl_stats.stats[0].wildcards, \
1956 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07001957 sw.valid_ports, \
1958 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07001959 )
1960 fc = fc.canonical()
1961
1962 # Send it to the switch
1963
Rich Lane9a003812012-10-04 17:17:59 -07001964 logging.info("Sending flow add to switch:")
1965 logging.info(str(fc))
rootf6af1672012-04-06 09:46:29 -07001966 ft = Flow_Tbl()
1967 fc.send_rem = False
1968 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
1969 ft.insert(fc)
1970
1971 # Dream up some different actions, with the same flow key
1972
1973 fc2 = copy.deepcopy(fc)
1974 while True:
1975 fc2.rand_mod(fi, \
1976 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07001977 sw.valid_ports, \
1978 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07001979 )
1980 if fc2 != fc:
1981 break
1982
1983 # Send that to the switch
1984
Rich Lane9a003812012-10-04 17:17:59 -07001985 logging.info("Sending flow add to switch:")
1986 logging.info(str(fc2))
rootf6af1672012-04-06 09:46:29 -07001987 fc2.send_rem = False
1988 self.assertTrue(sw.flow_add(fc2), "Failed to add flow")
1989 ft.insert(fc2)
1990
1991 # Do barrier, to make sure all flows are in
1992
1993 self.assertTrue(sw.barrier(), "Barrier failed")
1994
1995 result = True
1996
Howard Persh9cab4822012-09-11 17:08:40 -07001997 sw.settle() # Allow switch to settle and generate any notifications
1998
rootf6af1672012-04-06 09:46:29 -07001999 # Check for any error messages
2000
2001 if not sw.errors_verify(0):
2002 result = False
2003
2004 # Verify flow table
2005
2006 sw.flow_tbl = ft
2007 if not sw.flow_tbl_verify():
2008 result = False
2009
2010 self.assertTrue(result, "Flow_Add_7 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07002011 logging.info("Flow_Add_7 TEST PASSED")
rootf6af1672012-04-06 09:46:29 -07002012
2013
Howard Persh07d99e62012-04-09 15:26:57 -07002014# FLOW ADD 8
2015#
2016# OVERVIEW
2017# Add overlapping flows to switch, verify that overlapping flows are rejected
2018#
2019# PURPOSE
2020# - Test detection of overlapping flows by switch
2021# - Test generation of OFPET_FLOW_MOD_FAILED/OFPFMFC_OVERLAP messages
2022# - Test rejection of overlapping flows
2023# - Test defining overlapping flows does not corrupt flow table
2024#
2025# PARAMETERS
2026# None
2027#
2028# PROCESS
2029# 1. Delete all flows from switch
2030# 2. Generate flow definition F1
2031# 3. Generate flow definition F2, with key overlapping F1
2032# 4. Send flow add to switch, for F1
2033# 5. Send flow add to switch, for F2, with OFPFF_CHECK_OVERLAP set
2034# 6. Verify that OFPET_FLOW_MOD_FAILED/OFPFMFC_OVERLAP error response
2035# was generated by switch
2036# 7. Verifiy flows configured in swtich
2037# 8. Test PASSED iff:
2038# - error message received, for overlapping flow
2039# - overlapping flow is not in flow table
2040# else test FAILED
2041
Rich Laneb90a1c42012-10-05 09:16:05 -07002042class Flow_Add_8(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07002043 """
2044 Test FLOW_ADD_8 from draft top-half test plan
2045
2046 INPUTS
2047 None
2048 """
2049
2050 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07002051 logging.info("Flow_Add_8 TEST BEGIN")
rootf6af1672012-04-06 09:46:29 -07002052
2053 # Clear all flows from switch
2054
Rich Lane9a003812012-10-04 17:17:59 -07002055 logging.info("Deleting all flows from switch")
2056 rc = delete_all_flows(self.controller)
rootf6af1672012-04-06 09:46:29 -07002057 self.assertEqual(rc, 0, "Failed to delete all flows")
2058
2059 # Get switch capabilites
2060
rootf6af1672012-04-06 09:46:29 -07002061 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07002062 self.assertTrue(sw.connect(self.controller), \
2063 "Failed to connect to switch" \
2064 )
rootf6af1672012-04-06 09:46:29 -07002065
2066 # Dream up some flow information, i.e. space to chose from for
2067 # random flow parameter generation
2068
2069 fi = Flow_Info()
2070 fi.rand(10)
2071
2072 # Dream up a flow config, with at least 1 qualifier specified
2073
2074 fc = Flow_Cfg()
2075 while True:
2076 fc.rand(fi, \
Ed Swierk99a74de2012-08-22 06:40:54 -07002077 required_wildcards(self), \
rootf6af1672012-04-06 09:46:29 -07002078 sw.tbl_stats.stats[0].wildcards, \
2079 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07002080 sw.valid_ports, \
2081 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07002082 )
2083 fc = fc.canonical()
2084 if fc.match.wildcards != ofp.OFPFW_ALL:
2085 break
2086
2087 # Send it to the switch
2088
Rich Lane9a003812012-10-04 17:17:59 -07002089 logging.info("Sending flow add to switch:")
2090 logging.info(str(fc))
rootf6af1672012-04-06 09:46:29 -07002091 ft = Flow_Tbl()
2092 fc.send_rem = False
2093 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
2094 ft.insert(fc)
2095
2096 # Wildcard out one qualifier that was specified, to create an
2097 # overlapping flow
2098
2099 fc2 = copy.deepcopy(fc)
2100 for wi in shuffle(range(len(all_wildcards_list))):
2101 w = all_wildcards_list[wi]
2102 if (fc2.match.wildcards & w) == 0:
2103 break
2104 if w == ofp.OFPFW_NW_SRC_MASK:
2105 w = ofp.OFPFW_NW_SRC_ALL
2106 wn = "OFPFW_NW_SRC"
2107 elif w == ofp.OFPFW_NW_DST_MASK:
2108 w = ofp.OFPFW_NW_DST_ALL
2109 wn = "OFPFW_NW_DST"
2110 else:
2111 wn = all_wildcard_names[w]
Rich Lane9a003812012-10-04 17:17:59 -07002112 logging.info("Wildcarding out %s" % (wn))
rootf6af1672012-04-06 09:46:29 -07002113 fc2.match.wildcards = fc2.match.wildcards | w
Howard Persh07d99e62012-04-09 15:26:57 -07002114 fc2 = fc2.canonical()
rootf6af1672012-04-06 09:46:29 -07002115
2116 # Send that to the switch, with overlap checking
2117
Rich Lane9a003812012-10-04 17:17:59 -07002118 logging.info("Sending flow add to switch:")
2119 logging.info(str(fc2))
rootf6af1672012-04-06 09:46:29 -07002120 fc2.send_rem = False
2121 self.assertTrue(sw.flow_add(fc2, True), "Failed to add flow")
2122
2123 # Do barrier, to make sure all flows are in
2124 self.assertTrue(sw.barrier(), "Barrier failed")
2125
2126 result = True
2127
Howard Persh9cab4822012-09-11 17:08:40 -07002128 sw.settle() # Allow switch to settle and generate any notifications
2129
rootf6af1672012-04-06 09:46:29 -07002130 # Check for expected error message
2131
2132 if not sw.errors_verify(1, \
2133 ofp.OFPET_FLOW_MOD_FAILED, \
2134 ofp.OFPFMFC_OVERLAP \
2135 ):
2136 result = False
2137
2138 # Verify flow table
2139
2140 sw.flow_tbl = ft
2141 if not sw.flow_tbl_verify():
2142 result = False
2143
2144 self.assertTrue(result, "Flow_Add_8 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07002145 logging.info("Flow_Add_8 TEST PASSED")
rootf6af1672012-04-06 09:46:29 -07002146
2147
Howard Persh07d99e62012-04-09 15:26:57 -07002148# FLOW MODIFY 1
2149#
2150# OVERVIEW
2151# Strict modify of single existing flow
2152#
2153# PURPOSE
2154# - Verify that strict flow modify operates only on specified flow
2155# - Verify that flow is correctly modified
2156#
2157# PARAMETERS
2158# None
2159#
2160# PROCESS
2161# 1. Delete all flows from switch
2162# 2. Generate 1 flow F
2163# 3. Send flow add to switch, for flow F
2164# 4. Generate new action list for flow F, yielding F'
2165# 5. Send strict flow modify to switch, for flow F'
2166# 6. Verify flow table in switch
2167# 7. Test PASSED iff flow returned by switch is F'; else FAILED
2168
Rich Laneb90a1c42012-10-05 09:16:05 -07002169class Flow_Mod_1(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07002170 """
2171 Test FLOW_MOD_1 from draft top-half test plan
2172
2173 INPUTS
2174 None
2175 """
2176
2177 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07002178 logging.info("Flow_Mod_1 TEST BEGIN")
rootf6af1672012-04-06 09:46:29 -07002179
2180 # Clear all flows from switch
2181
Rich Lane9a003812012-10-04 17:17:59 -07002182 logging.info("Deleting all flows from switch")
2183 rc = delete_all_flows(self.controller)
rootf6af1672012-04-06 09:46:29 -07002184 self.assertEqual(rc, 0, "Failed to delete all flows")
2185
2186 # Get switch capabilites
2187
rootf6af1672012-04-06 09:46:29 -07002188 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07002189 self.assertTrue(sw.connect(self.controller), \
2190 "Failed to connect to switch" \
2191 )
rootf6af1672012-04-06 09:46:29 -07002192
2193 # Dream up some flow information, i.e. space to chose from for
2194 # random flow parameter generation
2195
2196 fi = Flow_Info()
2197 fi.rand(10)
2198
2199 # Dream up a flow config
2200
2201 fc = Flow_Cfg()
2202 fc.rand(fi, \
Ed Swierk99a74de2012-08-22 06:40:54 -07002203 required_wildcards(self), \
rootf6af1672012-04-06 09:46:29 -07002204 sw.tbl_stats.stats[0].wildcards, \
2205 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07002206 sw.valid_ports, \
2207 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07002208 )
2209 fc = fc.canonical()
2210
2211 # Send it to the switch
2212
Rich Lane9a003812012-10-04 17:17:59 -07002213 logging.info("Sending flow add to switch:")
2214 logging.info(str(fc))
rootf6af1672012-04-06 09:46:29 -07002215 ft = Flow_Tbl()
2216 fc.send_rem = False
2217 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
2218 ft.insert(fc)
2219
2220 # Dream up some different actions, with the same flow key
2221
2222 fc2 = copy.deepcopy(fc)
2223 while True:
2224 fc2.rand_mod(fi, \
2225 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07002226 sw.valid_ports, \
2227 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07002228 )
2229 if fc2 != fc:
2230 break
2231
2232 # Send that to the switch
2233
Rich Lane9a003812012-10-04 17:17:59 -07002234 logging.info("Sending strict flow mod to switch:")
2235 logging.info(str(fc2))
rootf6af1672012-04-06 09:46:29 -07002236 fc2.send_rem = False
2237 self.assertTrue(sw.flow_mod(fc2, True), "Failed to modify flow")
2238 ft.insert(fc2)
2239
2240 # Do barrier, to make sure all flows are in
2241
2242 self.assertTrue(sw.barrier(), "Barrier failed")
2243
2244 result = True
2245
Howard Persh9cab4822012-09-11 17:08:40 -07002246 sw.settle() # Allow switch to settle and generate any notifications
2247
rootf6af1672012-04-06 09:46:29 -07002248 # Check for any error messages
2249
2250 if not sw.errors_verify(0):
2251 result = False
2252
2253 # Verify flow table
2254
2255 sw.flow_tbl = ft
Howard Persh5f3c83f2012-04-13 09:57:10 -07002256 if not sw.flow_tbl_verify(True):
rootf6af1672012-04-06 09:46:29 -07002257 result = False
2258
2259 self.assertTrue(result, "Flow_Mod_1 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07002260 logging.info("Flow_Mod_1 TEST PASSED")
rootf6af1672012-04-06 09:46:29 -07002261
Howard Persh07d99e62012-04-09 15:26:57 -07002262
2263# FLOW MODIFY 2
2264#
2265# OVERVIEW
2266# Loose modify of mutiple flows
2267#
2268# PURPOSE
2269# - Verify that loose flow modify operates only on matching flows
2270# - Verify that matching flows are correctly modified
2271#
2272# PARAMETERS
2273# Name: num_flows
2274# Type: number
2275# Description:
2276# Number of flows to define
2277# Default: 100
2278#
2279# PROCESS
2280# 1. Delete all flows from switch
2281# 2. Generate <num_flows> distinct flow configurations
2282# 3. Send <num_flows> flow adds to switch
2283# 4. Pick 1 defined flow F at random
2284# 5. Create overlapping loose flow mod match criteria by repeatedly
2285# wildcarding out qualifiers in match of F => F',
2286# and create new actions list A' for F'
2287# 6. Send loose flow modify for F' to switch
2288# 7. Verify flow table in swtich
2289# 8. Test PASSED iff all flows sent to switch in steps 3 and 6 above,
2290# are returned in step 7 above, each with correct (original or modified)
2291# action list;
2292# else test FAILED
rootf6af1672012-04-06 09:46:29 -07002293
Rich Laneb90a1c42012-10-05 09:16:05 -07002294class Flow_Mod_2(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07002295 """
2296 Test FLOW_MOD_2 from draft top-half test plan
2297
2298 INPUTS
2299 None
2300 """
2301
2302 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07002303 logging.info("Flow_Mod_2 TEST BEGIN")
rootf6af1672012-04-06 09:46:29 -07002304
Rich Lane2014f9b2012-10-05 15:29:40 -07002305 num_flows = test_param_get("num_flows", 100)
rootf6af1672012-04-06 09:46:29 -07002306
2307 # Clear all flows from switch
2308
Rich Lane9a003812012-10-04 17:17:59 -07002309 logging.info("Deleting all flows from switch")
2310 rc = delete_all_flows(self.controller)
rootf6af1672012-04-06 09:46:29 -07002311 self.assertEqual(rc, 0, "Failed to delete all flows")
2312
2313 # Get switch capabilites
2314
rootf6af1672012-04-06 09:46:29 -07002315 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07002316 self.assertTrue(sw.connect(self.controller), \
2317 "Failed to connect to switch" \
2318 )
rootf6af1672012-04-06 09:46:29 -07002319
2320 # Dream up some flow information, i.e. space to chose from for
2321 # random flow parameter generation
2322
2323 fi = Flow_Info()
Howard Persh3340d452012-04-06 16:45:21 -07002324 # Shrunk, to increase chance of meta-matches
Howard Persh07d99e62012-04-09 15:26:57 -07002325 fi.rand(max(int(math.log(num_flows)) / 2, 1))
rootf6af1672012-04-06 09:46:29 -07002326
2327 # Dream up some flows
2328
2329 ft = Flow_Tbl()
Ed Swierk99a74de2012-08-22 06:40:54 -07002330 ft.rand(required_wildcards(self), sw, fi, num_flows)
rootf6af1672012-04-06 09:46:29 -07002331
2332 # Send flow table to switch
2333
Rich Lane9a003812012-10-04 17:17:59 -07002334 logging.info("Sending flow adds to switch")
rootf6af1672012-04-06 09:46:29 -07002335 for fc in ft.values(): # Randomizes order of sending
Rich Lane9a003812012-10-04 17:17:59 -07002336 logging.info("Adding flow:")
2337 logging.info(str(fc));
rootf6af1672012-04-06 09:46:29 -07002338 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
2339
2340 # Do barrier, to make sure all flows are in
2341
2342 self.assertTrue(sw.barrier(), "Barrier failed")
2343
2344 result = True
2345
Howard Persh9cab4822012-09-11 17:08:40 -07002346 sw.settle() # Allow switch to settle and generate any notifications
2347
rootf6af1672012-04-06 09:46:29 -07002348 # Check for any error messages
2349
2350 if not sw.errors_verify(0):
2351 result = False
2352
2353 # Verify flow table
2354
2355 sw.flow_tbl = ft
2356 if not sw.flow_tbl_verify():
2357 result = False
2358
2359 # Pick a random flow as a basis
Howard Persh5f3c83f2012-04-13 09:57:10 -07002360
2361 mfc = copy.deepcopy((ft.values())[0])
Howard Persh8d21c1f2012-04-20 15:57:29 -07002362 mfc.rand_mod(fi, \
2363 sw.sw_features.actions, \
2364 sw.valid_ports, \
2365 sw.valid_queues \
2366 )
rootf6af1672012-04-06 09:46:29 -07002367
2368 # Repeatedly wildcard qualifiers
2369
2370 for wi in shuffle(range(len(all_wildcards_list))):
2371 w = all_wildcards_list[wi]
2372 if w == ofp.OFPFW_NW_SRC_MASK or w == ofp.OFPFW_NW_DST_MASK:
2373 n = wildcard_get(mfc.match.wildcards, w)
2374 if n < 32:
Howard Persh3340d452012-04-06 16:45:21 -07002375 mfc.match.wildcards = wildcard_set(mfc.match.wildcards, \
2376 w, \
2377 random.randint(n + 1, 32) \
2378 )
rootf6af1672012-04-06 09:46:29 -07002379 else:
2380 continue
2381 else:
2382 if wildcard_get(mfc.match.wildcards, w) == 0:
2383 mfc.match.wildcards = wildcard_set(mfc.match.wildcards, w, 1)
2384 else:
2385 continue
2386 mfc = mfc.canonical()
2387
2388 # Count the number of flows that would be modified
2389
2390 n = 0
2391 for fc in ft.values():
2392 if mfc.overlaps(fc, True) and not mfc.non_key_equal(fc):
2393 n = n + 1
2394
2395 # If more than 1, we found our loose delete flow spec
2396 if n > 1:
2397 break
2398
Rich Lane9a003812012-10-04 17:17:59 -07002399 logging.info("Modifying %d flows" % (n))
2400 logging.info("Sending flow mod to switch:")
2401 logging.info(str(mfc))
rootf6af1672012-04-06 09:46:29 -07002402 self.assertTrue(sw.flow_mod(mfc, False), "Failed to modify flow")
2403
2404 # Do barrier, to make sure all flows are in
2405 self.assertTrue(sw.barrier(), "Barrier failed")
2406
Howard Persh9cab4822012-09-11 17:08:40 -07002407 sw.settle() # Allow switch to settle and generate any notifications
2408
rootf6af1672012-04-06 09:46:29 -07002409 # Check for error message
2410
2411 if not sw.errors_verify(0):
2412 result = False
2413
2414 # Apply flow mod to local flow table
2415
2416 for fc in ft.values():
2417 if mfc.overlaps(fc, True):
root2843d2b2012-04-06 10:27:46 -07002418 fc.actions = mfc.actions
rootf6af1672012-04-06 09:46:29 -07002419
2420 # Verify flow table
2421
2422 sw.flow_tbl = ft
Howard Persh5f3c83f2012-04-13 09:57:10 -07002423 if not sw.flow_tbl_verify(True):
rootf6af1672012-04-06 09:46:29 -07002424 result = False
2425
2426 self.assertTrue(result, "Flow_Mod_2 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07002427 logging.info("Flow_Mod_2 TEST PASSED")
rootf6af1672012-04-06 09:46:29 -07002428
2429
Howard Persh07d99e62012-04-09 15:26:57 -07002430# FLOW MODIFY 3
2431
2432# OVERVIEW
2433# Strict modify of non-existent flow
2434#
2435# PURPOSE
2436# Verify that strict modify of a non-existent flow is equivalent to a flow add
2437#
2438# PARAMETERS
2439# None
2440#
2441# PROCESS
2442# 1. Delete all flows from switch
2443# 2. Send single flow mod, as strict modify, to switch
2444# 3. Verify flow table in switch
2445# 4. Test PASSED iff flow defined in step 2 above verified; else FAILED
2446
Rich Laneb90a1c42012-10-05 09:16:05 -07002447class Flow_Mod_3(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07002448 """
2449 Test FLOW_MOD_3 from draft top-half test plan
2450
2451 INPUTS
2452 None
2453 """
2454
2455 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07002456 logging.info("Flow_Mod_3 TEST BEGIN")
rootf6af1672012-04-06 09:46:29 -07002457
2458 # Clear all flows from switch
2459
Rich Lane9a003812012-10-04 17:17:59 -07002460 logging.info("Deleting all flows from switch")
2461 rc = delete_all_flows(self.controller)
rootf6af1672012-04-06 09:46:29 -07002462 self.assertEqual(rc, 0, "Failed to delete all flows")
2463
2464 # Get switch capabilites
2465
rootf6af1672012-04-06 09:46:29 -07002466 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07002467 self.assertTrue(sw.connect(self.controller), \
2468 "Failed to connect to switch" \
2469 )
rootf6af1672012-04-06 09:46:29 -07002470
2471 # Dream up some flow information, i.e. space to chose from for
2472 # random flow parameter generation
2473
2474 fi = Flow_Info()
2475 fi.rand(10)
2476
2477 # Dream up a flow config
2478
2479 fc = Flow_Cfg()
2480 fc.rand(fi, \
Ed Swierk99a74de2012-08-22 06:40:54 -07002481 required_wildcards(self), \
rootf6af1672012-04-06 09:46:29 -07002482 sw.tbl_stats.stats[0].wildcards, \
2483 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07002484 sw.valid_ports, \
2485 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07002486 )
2487 fc = fc.canonical()
2488
2489 # Send it to the switch
2490
Rich Lane9a003812012-10-04 17:17:59 -07002491 logging.info("Sending flow mod to switch:")
2492 logging.info(str(fc))
rootf6af1672012-04-06 09:46:29 -07002493 ft = Flow_Tbl()
2494 fc.send_rem = False
2495 self.assertTrue(sw.flow_mod(fc, True), "Failed to modify flows")
2496 ft.insert(fc)
2497
2498 # Do barrier, to make sure all flows are in
2499
2500 self.assertTrue(sw.barrier(), "Barrier failed")
2501
2502 result = True
2503
Howard Persh9cab4822012-09-11 17:08:40 -07002504 sw.settle() # Allow switch to settle and generate any notifications
2505
rootf6af1672012-04-06 09:46:29 -07002506 # Check for any error messages
2507
2508 if not sw.errors_verify(0):
2509 result = False
2510
2511 # Verify flow table
2512
2513 sw.flow_tbl = ft
2514 if not sw.flow_tbl_verify():
2515 result = False
2516
2517 self.assertTrue(result, "Flow_Mod_3 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07002518 logging.info("Flow_Mod_3 TEST PASSED")
rootf6af1672012-04-06 09:46:29 -07002519
2520
Howard Persh8d21c1f2012-04-20 15:57:29 -07002521# FLOW MODIFY 3_1
2522
2523# OVERVIEW
2524# No-op modify
2525#
2526# PURPOSE
2527# Verify that modify of a flow with new actions same as old ones operates correctly
2528#
2529# PARAMETERS
2530# None
2531#
2532# PROCESS
2533# 1. Delete all flows from switch
2534# 2. Send single flow mod, as strict modify, to switch
2535# 3. Verify flow table in switch
2536# 4. Send same flow mod, as strict modify, to switch
2537# 5. Verify flow table in switch
2538# 6. Test PASSED iff flow defined in step 2 and 4 above verified; else FAILED
2539
Rich Laneb90a1c42012-10-05 09:16:05 -07002540class Flow_Mod_3_1(base_tests.SimpleProtocol):
Howard Persh8d21c1f2012-04-20 15:57:29 -07002541 """
2542 Test FLOW_MOD_3_1 from draft top-half test plan
2543
2544 INPUTS
2545 None
2546 """
2547
2548 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07002549 logging.info("Flow_Mod_3_1 TEST BEGIN")
Howard Persh8d21c1f2012-04-20 15:57:29 -07002550
2551 # Clear all flows from switch
2552
Rich Lane9a003812012-10-04 17:17:59 -07002553 logging.info("Deleting all flows from switch")
2554 rc = delete_all_flows(self.controller)
Howard Persh8d21c1f2012-04-20 15:57:29 -07002555 self.assertEqual(rc, 0, "Failed to delete all flows")
2556
2557 # Get switch capabilites
2558
2559 sw = Switch()
2560 self.assertTrue(sw.connect(self.controller), \
2561 "Failed to connect to switch" \
2562 )
2563
2564 # Dream up some flow information, i.e. space to chose from for
2565 # random flow parameter generation
2566
2567 fi = Flow_Info()
2568 fi.rand(10)
2569
2570 # Dream up a flow config
2571
2572 fc = Flow_Cfg()
2573 fc.rand(fi, \
Ed Swierk99a74de2012-08-22 06:40:54 -07002574 required_wildcards(self), \
Howard Persh8d21c1f2012-04-20 15:57:29 -07002575 sw.tbl_stats.stats[0].wildcards, \
2576 sw.sw_features.actions, \
2577 sw.valid_ports, \
2578 sw.valid_queues \
2579 )
2580 fc = fc.canonical()
2581
2582 # Send it to the switch
2583
Rich Lane9a003812012-10-04 17:17:59 -07002584 logging.info("Sending flow mod to switch:")
2585 logging.info(str(fc))
Howard Persh8d21c1f2012-04-20 15:57:29 -07002586 ft = Flow_Tbl()
2587 fc.send_rem = False
2588 self.assertTrue(sw.flow_mod(fc, True), "Failed to modify flows")
2589 ft.insert(fc)
2590
2591 # Do barrier, to make sure all flows are in
2592
2593 self.assertTrue(sw.barrier(), "Barrier failed")
2594
2595 result = True
2596
Howard Persh9cab4822012-09-11 17:08:40 -07002597 sw.settle() # Allow switch to settle and generate any notifications
2598
Howard Persh8d21c1f2012-04-20 15:57:29 -07002599 # Check for any error messages
2600
2601 if not sw.errors_verify(0):
2602 result = False
2603
2604 # Verify flow table
2605
2606 sw.flow_tbl = ft
2607 if not sw.flow_tbl_verify():
2608 result = False
2609
2610 # Send same flow to the switch again
2611
Rich Lane9a003812012-10-04 17:17:59 -07002612 logging.info("Sending flow mod to switch:")
2613 logging.info(str(fc))
Howard Persh8d21c1f2012-04-20 15:57:29 -07002614 self.assertTrue(sw.flow_mod(fc, True), "Failed to modify flows")
2615
2616 # Do barrier, to make sure all flows are in
2617
2618 self.assertTrue(sw.barrier(), "Barrier failed")
2619
Howard Persh9cab4822012-09-11 17:08:40 -07002620 sw.settle() # Allow switch to settle and generate any notifications
2621
Howard Persh8d21c1f2012-04-20 15:57:29 -07002622 # Check for any error messages
2623
2624 if not sw.errors_verify(0):
2625 result = False
2626
2627 # Verify flow table
2628
2629 if not sw.flow_tbl_verify():
2630 result = False
2631
2632 self.assertTrue(result, "Flow_Mod_3_1 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07002633 logging.info("Flow_Mod_3_1 TEST PASSED")
Howard Persh8d21c1f2012-04-20 15:57:29 -07002634
2635
Howard Persh07d99e62012-04-09 15:26:57 -07002636# FLOW DELETE 1
2637#
2638# OVERVIEW
2639# Strict delete of single flow
2640#
2641# PURPOSE
2642# Verify correct operation of strict delete of single defined flow
2643#
2644# PARAMETERS
2645# None
2646#
2647# PROCESS
2648# 1. Delete all flows from switch
2649# 2. Send flow F to switch
2650# 3. Send strict flow delete for F to switch
2651# 4. Verify flow table in swtich
2652# 6. Test PASSED iff all flows sent to switch in step 2 above,
2653# less flow removed in step 3 above, are returned in step 4 above;
2654# else test FAILED
2655
Rich Laneb90a1c42012-10-05 09:16:05 -07002656class Flow_Del_1(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07002657 """
2658 Test FLOW_DEL_1 from draft top-half test plan
2659
2660 INPUTS
2661 None
2662 """
2663
2664 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07002665 logging.info("Flow_Del_1 TEST BEGIN")
rootf6af1672012-04-06 09:46:29 -07002666
2667 # Clear all flows from switch
2668
Rich Lane9a003812012-10-04 17:17:59 -07002669 logging.info("Deleting all flows from switch")
2670 rc = delete_all_flows(self.controller)
rootf6af1672012-04-06 09:46:29 -07002671 self.assertEqual(rc, 0, "Failed to delete all flows")
2672
2673 # Get switch capabilites
2674
rootf6af1672012-04-06 09:46:29 -07002675 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07002676 self.assertTrue(sw.connect(self.controller), \
2677 "Failed to connect to switch" \
2678 )
rootf6af1672012-04-06 09:46:29 -07002679
2680 # Dream up some flow information, i.e. space to chose from for
2681 # random flow parameter generation
2682
2683 fi = Flow_Info()
2684 fi.rand(10)
2685
2686 # Dream up a flow config
2687
2688 fc = Flow_Cfg()
2689 fc.rand(fi, \
Ed Swierk99a74de2012-08-22 06:40:54 -07002690 required_wildcards(self), \
rootf6af1672012-04-06 09:46:29 -07002691 sw.tbl_stats.stats[0].wildcards, \
2692 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07002693 sw.valid_ports, \
2694 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07002695 )
2696 fc = fc.canonical()
2697
2698 # Send it to the switch
2699
Rich Lane9a003812012-10-04 17:17:59 -07002700 logging.info("Sending flow add to switch:")
2701 logging.info(str(fc))
rootf6af1672012-04-06 09:46:29 -07002702 ft = Flow_Tbl()
2703 fc.send_rem = False
2704 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
2705 ft.insert(fc)
2706
2707 # Dream up some different actions, with the same flow key
2708
2709 fc2 = copy.deepcopy(fc)
2710 while True:
2711 fc2.rand_mod(fi, \
2712 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07002713 sw.valid_ports, \
2714 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07002715 )
2716 if fc2 != fc:
2717 break
2718
2719 # Delete strictly
2720
Rich Lane9a003812012-10-04 17:17:59 -07002721 logging.info("Sending strict flow del to switch:")
2722 logging.info(str(fc2))
rootf6af1672012-04-06 09:46:29 -07002723 self.assertTrue(sw.flow_del(fc2, True), "Failed to delete flow")
2724 ft.delete(fc)
2725
2726 # Do barrier, to make sure all flows are in
2727
2728 self.assertTrue(sw.barrier(), "Barrier failed")
2729
2730 result = True
2731
Howard Persh9cab4822012-09-11 17:08:40 -07002732 sw.settle() # Allow switch to settle and generate any notifications
2733
rootf6af1672012-04-06 09:46:29 -07002734 # Check for any error messages
2735
2736 if not sw.errors_verify(0):
2737 result = False
2738
2739 # Verify flow table
2740
2741 sw.flow_tbl = ft
2742 if not sw.flow_tbl_verify():
2743 result = False
2744
2745 self.assertTrue(result, "Flow_Del_1 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07002746 logging.info("Flow_Del_1 TEST PASSED")
rootf6af1672012-04-06 09:46:29 -07002747
2748
Howard Persh07d99e62012-04-09 15:26:57 -07002749# FLOW DELETE 2
2750#
2751# OVERVIEW
2752# Loose delete of multiple flows
2753#
2754# PURPOSE
2755# - Verify correct operation of loose delete of multiple flows
2756#
2757# PARAMETERS
2758# Name: num_flows
2759# Type: number
2760# Description:
2761# Number of flows to define
2762# Default: 100
2763#
2764# PROCESS
2765# 1. Delete all flows from switch
2766# 2. Generate <num_flows> distinct flow configurations
2767# 3. Send <num_flows> flow adds to switch, for flows generated in step 2 above
2768# 4. Pick 1 defined flow F at random
2769# 5. Repeatedly wildcard out qualifiers in match of F, creating F', such that
2770# F' will match more than 1 existing flow key
2771# 6. Send loose flow delete for F' to switch
2772# 7. Verify flow table in switch
2773# 8. Test PASSED iff all flows sent to switch in step 3 above, less flows
2774# removed in step 6 above (i.e. those that match F'), are returned
2775# in step 7 above;
2776# else test FAILED
2777
Rich Laneb90a1c42012-10-05 09:16:05 -07002778class Flow_Del_2(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07002779 """
2780 Test FLOW_DEL_2 from draft top-half test plan
2781
2782 INPUTS
2783 None
2784 """
2785
2786 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07002787 logging.info("Flow_Del_2 TEST BEGIN")
rootf6af1672012-04-06 09:46:29 -07002788
Rich Lane2014f9b2012-10-05 15:29:40 -07002789 num_flows = test_param_get("num_flows", 100)
rootf6af1672012-04-06 09:46:29 -07002790
2791 # Clear all flows from switch
2792
Rich Lane9a003812012-10-04 17:17:59 -07002793 logging.info("Deleting all flows from switch")
2794 rc = delete_all_flows(self.controller)
rootf6af1672012-04-06 09:46:29 -07002795 self.assertEqual(rc, 0, "Failed to delete all flows")
2796
2797 # Get switch capabilites
2798
rootf6af1672012-04-06 09:46:29 -07002799 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07002800 self.assertTrue(sw.connect(self.controller), \
2801 "Failed to connect to switch" \
2802 )
rootf6af1672012-04-06 09:46:29 -07002803
2804 # Dream up some flow information, i.e. space to chose from for
2805 # random flow parameter generation
2806
2807 fi = Flow_Info()
Howard Persh3340d452012-04-06 16:45:21 -07002808 # Shrunk, to increase chance of meta-matches
Howard Persh07d99e62012-04-09 15:26:57 -07002809 fi.rand(max(int(math.log(num_flows)) / 2, 1))
rootf6af1672012-04-06 09:46:29 -07002810
2811 # Dream up some flows
2812
2813 ft = Flow_Tbl()
Ed Swierk99a74de2012-08-22 06:40:54 -07002814 ft.rand(required_wildcards(self), sw, fi, num_flows)
rootf6af1672012-04-06 09:46:29 -07002815
2816 # Send flow table to switch
2817
Rich Lane9a003812012-10-04 17:17:59 -07002818 logging.info("Sending flow adds to switch")
rootf6af1672012-04-06 09:46:29 -07002819 for fc in ft.values(): # Randomizes order of sending
Rich Lane9a003812012-10-04 17:17:59 -07002820 logging.info("Adding flow:")
2821 logging.info(str(fc));
rootf6af1672012-04-06 09:46:29 -07002822 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
2823
2824 # Do barrier, to make sure all flows are in
2825
2826 self.assertTrue(sw.barrier(), "Barrier failed")
2827
2828 result = True
2829
Howard Persh9cab4822012-09-11 17:08:40 -07002830 sw.settle() # Allow switch to settle and generate any notifications
2831
rootf6af1672012-04-06 09:46:29 -07002832 # Check for any error messages
2833
2834 if not sw.errors_verify(0):
2835 result = False
2836
2837 # Verify flow table
2838
2839 sw.flow_tbl = ft
2840 if not sw.flow_tbl_verify():
2841 result = False
2842
2843 # Pick a random flow as a basis
2844
2845 dfc = copy.deepcopy(ft.values()[0])
Howard Persh8d21c1f2012-04-20 15:57:29 -07002846 dfc.rand_mod(fi, \
2847 sw.sw_features.actions, \
2848 sw.valid_ports, \
2849 sw.valid_queues \
2850 )
rootf6af1672012-04-06 09:46:29 -07002851
2852 # Repeatedly wildcard qualifiers
2853
2854 for wi in shuffle(range(len(all_wildcards_list))):
2855 w = all_wildcards_list[wi]
2856 if w == ofp.OFPFW_NW_SRC_MASK or w == ofp.OFPFW_NW_DST_MASK:
2857 n = wildcard_get(dfc.match.wildcards, w)
2858 if n < 32:
Howard Persh3340d452012-04-06 16:45:21 -07002859 dfc.match.wildcards = wildcard_set(dfc.match.wildcards, \
2860 w, \
2861 random.randint(n + 1, 32) \
2862 )
rootf6af1672012-04-06 09:46:29 -07002863 else:
2864 continue
2865 else:
2866 if wildcard_get(dfc.match.wildcards, w) == 0:
2867 dfc.match.wildcards = wildcard_set(dfc.match.wildcards, w, 1)
2868 else:
2869 continue
2870 dfc = dfc.canonical()
2871
2872 # Count the number of flows that would be deleted
2873
2874 n = 0
2875 for fc in ft.values():
2876 if dfc.overlaps(fc, True):
2877 n = n + 1
2878
2879 # If more than 1, we found our loose delete flow spec
2880 if n > 1:
2881 break
2882
Rich Lane9a003812012-10-04 17:17:59 -07002883 logging.info("Deleting %d flows" % (n))
2884 logging.info("Sending flow del to switch:")
2885 logging.info(str(dfc))
rootf6af1672012-04-06 09:46:29 -07002886 self.assertTrue(sw.flow_del(dfc, False), "Failed to delete flows")
2887
2888 # Do barrier, to make sure all flows are in
2889 self.assertTrue(sw.barrier(), "Barrier failed")
2890
Howard Persh9cab4822012-09-11 17:08:40 -07002891 sw.settle() # Allow switch to settle and generate any notifications
2892
rootf6af1672012-04-06 09:46:29 -07002893 # Check for error message
2894
2895 if not sw.errors_verify(0):
2896 result = False
2897
2898 # Apply flow mod to local flow table
2899
2900 for fc in ft.values():
2901 if dfc.overlaps(fc, True):
2902 ft.delete(fc)
2903
2904 # Verify flow table
2905
2906 sw.flow_tbl = ft
2907 if not sw.flow_tbl_verify():
2908 result = False
2909
2910 self.assertTrue(result, "Flow_Del_2 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07002911 logging.info("Flow_Del_2 TEST PASSED")
rootf6af1672012-04-06 09:46:29 -07002912
2913
Howard Persh07d99e62012-04-09 15:26:57 -07002914# FLOW DELETE 4
2915#
2916# OVERVIEW
2917# Flow removed messages
2918#
2919# PURPOSE
2920# Verify that switch generates OFPT_FLOW_REMOVED/OFPRR_DELETE response
2921# messages when deleting flows that were added with OFPFF_SEND_FLOW_REM flag
2922#
2923# PARAMETERS
2924# None
2925#
2926# PROCESS
2927# 1. Delete all flows from switch
2928# 2. Send flow add to switch, with OFPFF_SEND_FLOW_REM set
2929# 3. Send strict flow delete of flow to switch
2930# 4. Verify that OFPT_FLOW_REMOVED/OFPRR_DELETE message is received from switch
2931# 5. Verify flow table in switch
2932# 6. Test PASSED iff all flows sent to switch in step 2 above, less flow
2933# removed in step 3 above, are returned in step 5 above, and that
2934# asynch message was received; else test FAILED
2935
2936
Rich Laneb90a1c42012-10-05 09:16:05 -07002937class Flow_Del_4(base_tests.SimpleProtocol):
rootf6af1672012-04-06 09:46:29 -07002938 """
2939 Test FLOW_DEL_4 from draft top-half test plan
2940
2941 INPUTS
2942 None
2943 """
2944
2945 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -07002946 logging.info("Flow_Del_4 TEST BEGIN")
rootf6af1672012-04-06 09:46:29 -07002947
2948 # Clear all flows from switch
2949
Rich Lane9a003812012-10-04 17:17:59 -07002950 logging.info("Deleting all flows from switch")
2951 rc = delete_all_flows(self.controller)
rootf6af1672012-04-06 09:46:29 -07002952 self.assertEqual(rc, 0, "Failed to delete all flows")
2953
2954 # Get switch capabilites
2955
rootf6af1672012-04-06 09:46:29 -07002956 sw = Switch()
Howard Persh8d21c1f2012-04-20 15:57:29 -07002957 self.assertTrue(sw.connect(self.controller), \
2958 "Failed to connect to switch" \
2959 )
rootf6af1672012-04-06 09:46:29 -07002960
2961 # Dream up some flow information, i.e. space to chose from for
2962 # random flow parameter generation
2963
2964 fi = Flow_Info()
2965 fi.rand(10)
2966
2967 # Dream up a flow config
2968
2969 fc = Flow_Cfg()
2970 fc.rand(fi, \
Ed Swierk99a74de2012-08-22 06:40:54 -07002971 required_wildcards(self), \
rootf6af1672012-04-06 09:46:29 -07002972 sw.tbl_stats.stats[0].wildcards, \
2973 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07002974 sw.valid_ports, \
2975 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07002976 )
2977 fc = fc.canonical()
2978
2979 # Send it to the switch. with "notify on removed"
2980
Rich Lane9a003812012-10-04 17:17:59 -07002981 logging.info("Sending flow add to switch:")
2982 logging.info(str(fc))
rootf6af1672012-04-06 09:46:29 -07002983 ft = Flow_Tbl()
2984 fc.send_rem = True
2985 self.assertTrue(sw.flow_add(fc), "Failed to add flow")
2986 ft.insert(fc)
2987
2988 # Dream up some different actions, with the same flow key
2989
2990 fc2 = copy.deepcopy(fc)
2991 while True:
2992 fc2.rand_mod(fi, \
2993 sw.sw_features.actions, \
Howard Persh8d21c1f2012-04-20 15:57:29 -07002994 sw.valid_ports, \
2995 sw.valid_queues \
rootf6af1672012-04-06 09:46:29 -07002996 )
2997 if fc2 != fc:
2998 break
2999
3000 # Delete strictly
3001
Rich Lane9a003812012-10-04 17:17:59 -07003002 logging.info("Sending strict flow del to switch:")
3003 logging.info(str(fc2))
rootf6af1672012-04-06 09:46:29 -07003004 self.assertTrue(sw.flow_del(fc2, True), "Failed to delete flow")
3005 ft.delete(fc)
3006
3007 # Do barrier, to make sure all flows are in
3008
3009 self.assertTrue(sw.barrier(), "Barrier failed")
3010
3011 result = True
3012
Howard Persh9cab4822012-09-11 17:08:40 -07003013 sw.settle() # Allow switch to settle and generate any notifications
3014
rootf6af1672012-04-06 09:46:29 -07003015 # Check for expected "removed" message
3016
Howard Persh3340d452012-04-06 16:45:21 -07003017 if not sw.errors_verify(0):
3018 result = False
3019
3020 if not sw.removed_verify(1):
rootf6af1672012-04-06 09:46:29 -07003021 result = False
3022
3023 # Verify flow table
3024
3025 sw.flow_tbl = ft
3026 if not sw.flow_tbl_verify():
3027 result = False
3028
3029 self.assertTrue(result, "Flow_Del_4 TEST FAILED")
Rich Lane9a003812012-10-04 17:17:59 -07003030 logging.info("Flow_Del_4 TEST PASSED")
rootf6af1672012-04-06 09:46:29 -07003031