Updated version of flow_query:
- loads more debug output
- does flow deletes
- etc. etc.
N.B.
- only lightly tested
- loose (non-strict) deletes do not work
diff --git a/tests/flow_query2.py b/tests/flow_query2.py
new file mode 100644
index 0000000..a09b886
--- /dev/null
+++ b/tests/flow_query2.py
@@ -0,0 +1,1210 @@
+"""
+Flow query test case.
+
+Attempts to fill switch to capacity with randomized flows, and ensure that they all are read back correctly.
+"""
+import math
+
+import logging
+
+import unittest
+import random
+
+import oftest.controller as controller
+import oftest.cstruct as ofp
+import oftest.message as message
+import oftest.dataplane as dataplane
+import oftest.action as action
+import oftest.action_list as action_list
+import oftest.parse as parse
+import pktact
+import basic
+
+from testutils import *
+from time import sleep
+
+#@var port_map Local copy of the configuration map from OF port
+# numbers to OS interfaces
+pa_port_map = None
+#@var pa_logger Local logger object
+pa_logger = None
+#@var pa_config Local copy of global configuration data
+pa_config = None
+
+def test_set_init(config):
+ """
+ Set up function for packet action test classes
+
+ @param config The configuration dictionary; see oft
+ """
+
+ basic.test_set_init(config)
+
+ global pa_port_map
+ global pa_logger
+ global pa_config
+
+ pa_logger = logging.getLogger("pkt_act")
+ pa_logger.info("Initializing test set")
+ pa_port_map = config["port_map"]
+ pa_config = config
+
+
+def shuffle(list):
+ n = len(list)
+ lim = n * n
+ i = 0
+ while i < lim:
+ a = random.randint(0, n - 1)
+ b = random.randint(0, n - 1)
+ temp = list[a]
+ list[a] = list[b]
+ list[b] = temp
+ i = i + 1
+ return list
+
+
+def rand_pick(list):
+ return list[random.randint(0, len(list) - 1)]
+
+def rand_dl_addr():
+ return [random.randint(0, 255) & ~1,
+ random.randint(0, 255),
+ random.randint(0, 255),
+ random.randint(0, 255),
+ random.randint(0, 255),
+ random.randint(0, 255)
+ ]
+
+def rand_nw_addr():
+ return random.randint(0, (1 << 32) - 1)
+
+
+class flow_info:
+ # Members:
+ # priorities - list of flow priorities
+ # dl_addrs - list of MAC addresses
+ # vlans - list of VLAN ids
+ # ethertypes - list of Ethertypes
+ # ip_addrs - list of IP addresses
+ # ip_tos - list of IP TOS values
+ # ip_protos - list of IP protocols
+ # l4_ports - list of L4 ports
+
+ def __init__(self):
+ priorities = []
+ dl_addrs = []
+ vlans = []
+ ethertypes = []
+ ip_addrs = []
+ ip_tos = []
+ ip_protos = []
+ l4_ports = []
+
+ def rand(self, n):
+ self.priorities = []
+ i = 0
+ while i < n:
+ self.priorities.append(random.randint(1, 65534))
+ i = i + 1
+
+ self.dl_addrs = []
+ i = 0
+ while i < n:
+ self.dl_addrs.append(rand_dl_addr())
+ i = i + 1
+
+ self.vlans = []
+ i = 0
+ while i < n:
+ self.vlans.append(random.randint(1, 4094))
+ i = i + 1
+
+ self.ethertypes = []
+ i = 0
+ while i < n:
+ self.ethertypes.append(random.randint(0, (1 << 16) - 1))
+ i = i + 1
+
+ self.ip_addrs = []
+ i = 0
+ while i < n:
+ self.ip_addrs.append(rand_nw_addr())
+ i = i + 1
+
+ self.ip_tos = []
+ i = 0
+ while i < n:
+ self.ip_tos.append(random.randint(0, (1 << 8) - 1) & ~3)
+ i = i + 1
+
+ self.ip_protos = []
+ i = 0
+ while i < n:
+ self.ip_protos.append(random.randint(0, (1 << 8) - 1))
+ i = i + 1
+
+ self.l4_ports = []
+ i = 0
+ while i < n:
+ self.l4_ports.append(random.randint(0, (1 << 16) - 1))
+ i = i + 1
+
+ def rand_priority(self):
+ return rand_pick(self.priorities)
+
+ def rand_dl_addr(self):
+ return rand_pick(self.dl_addrs)
+
+ def rand_vlan(self):
+ return rand_pick(self.vlans)
+
+ def rand_ethertype(self):
+ return rand_pick(self.ethertypes)
+
+ def rand_ip_addr(self):
+ return rand_pick(self.ip_addrs)
+
+ def rand_ip_tos(self):
+ return rand_pick(self.ip_tos)
+
+ def rand_ip_proto(self):
+ return rand_pick(self.ip_protos)
+
+ def rand_l4_port(self):
+ return rand_pick(self.l4_ports)
+
+
+# TBD - These don't belong here
+
+all_wildcards_list = [ofp.OFPFW_IN_PORT,
+ ofp.OFPFW_DL_VLAN,
+ ofp.OFPFW_DL_SRC,
+ ofp.OFPFW_DL_DST,
+ ofp.OFPFW_DL_TYPE,
+ ofp.OFPFW_NW_PROTO,
+ ofp.OFPFW_TP_SRC,
+ ofp.OFPFW_TP_DST,
+ ofp.OFPFW_NW_SRC_MASK,
+ ofp.OFPFW_NW_DST_MASK,
+ ofp.OFPFW_DL_VLAN_PCP,
+ ofp.OFPFW_NW_TOS
+ ]
+
+
+all_actions_list = [ofp.OFPAT_OUTPUT,
+ ofp.OFPAT_SET_VLAN_VID,
+ ofp.OFPAT_SET_VLAN_PCP,
+ ofp.OFPAT_STRIP_VLAN,
+ ofp.OFPAT_SET_DL_SRC,
+ ofp.OFPAT_SET_DL_DST,
+ ofp.OFPAT_SET_NW_SRC,
+ ofp.OFPAT_SET_NW_DST,
+ ofp.OFPAT_SET_NW_TOS,
+ ofp.OFPAT_SET_TP_SRC,
+ ofp.OFPAT_SET_TP_DST,
+ ofp.OFPAT_ENQUEUE
+ ]
+
+def dl_addr_to_str(a):
+ return "%x:%x:%x:%x:%x:%x" % tuple(a)
+
+def ip_addr_to_str(a, n):
+ result = "%d.%d.%d.%d" % (a >> 24, \
+ (a >> 16) & 0xff, \
+ (a >> 8) & 0xff, \
+ a & 0xff \
+ )
+ if n is not None:
+ result = result + ("/%d" % (n))
+ return result
+
+
+class flow_cfg:
+ # Members:
+ # - match
+ # - idle_timeout
+ # - hard_timeout
+ # - priority
+ # - action_list
+
+ def __init__(self):
+ self.priority = 0
+ self.match = parse.ofp_match()
+ self.match.wildcards = ofp.OFPFW_ALL
+ self.idle_timeout = 0
+ self.hard_timeout = 0
+ self.actions = action_list.action_list()
+
+ def __eq__(self, x):
+ if self.priority != x.priority:
+ return False
+ # TBD - Should this logic be moved to ofp_match.__eq__()?
+ if self.match.wildcards != x.match.wildcards:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_IN_PORT) == 0 \
+ and self.match.in_port != x.match.in_port:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_DL_SRC) == 0 \
+ and self.match.dl_src != x.match.dl_src:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_DL_DST) == 0 \
+ and self.match.dl_dst != x.match.dl_dst:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_DL_VLAN) == 0 \
+ and self.match.dl_vlan != x.match.dl_vlan:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_DL_VLAN_PCP) == 0 \
+ and self.match.dl_vlan_pcp != x.match.dl_vlan_pcp:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_DL_TYPE) == 0 \
+ and self.match.dl_type != x.match.dl_type:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_NW_TOS) == 0 \
+ and self.match.nw_tos != x.match.nw_tos:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_NW_PROTO) == 0 \
+ and self.match.nw_proto != x.match.nw_proto:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_NW_SRC_MASK) \
+ < ofp.OFPFW_NW_SRC_ALL:
+ m = ~((1 << ((self.match.wildcards & ofp.OFPFW_NW_SRC_MASK) \
+ >> ofp.OFPFW_NW_SRC_SHIFT)) - 1)
+ if (self.match.nw_src & m) != (x.match.nw_src & m):
+ return False
+ if (self.match.wildcards & ofp.OFPFW_NW_DST_MASK) \
+ < ofp.OFPFW_NW_DST_ALL:
+ m = ~((1 << ((self.match.wildcards & ofp.OFPFW_NW_DST_MASK) \
+ >> ofp.OFPFW_NW_DST_SHIFT)) - 1)
+ if (self.match.nw_dst & m) != (x.match.nw_dst & m):
+ return False
+ if (self.match.wildcards & ofp.OFPFW_TP_SRC) == 0 \
+ and self.match.tp_src != x.match.tp_src:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_TP_DST) == 0 \
+ and self.match.tp_dst != x.match.tp_dst:
+ return False
+ if self.idle_timeout != x.idle_timeout:
+ return False
+ if self.hard_timeout != x.hard_timeout:
+ return False
+ return self.actions == x.actions # N.B. Action lists are ordered
+
+ def __str__(self):
+ result = "priority=%d" % self.priority
+ # TBD - Would be nice if ofp_match.show() was better behaved
+ # (no newlines), and more intuitive (things in hex where approprate), etc.
+ result = result + ", wildcards={"
+ sep = ""
+ for w in ofp.ofp_flow_wildcards_map:
+ if w == ofp.OFPFW_NW_SRC_SHIFT \
+ or w == ofp.OFPFW_NW_SRC_BITS \
+ or w == ofp.OFPFW_NW_SRC_ALL \
+ or w == ofp.OFPFW_NW_DST_SHIFT \
+ or w == ofp.OFPFW_NW_DST_BITS \
+ or w == ofp.OFPFW_NW_DST_ALL \
+ or w == ofp.OFPFW_ALL \
+ or self.match.wildcards & w == 0:
+ continue
+ if w == ofp.OFPFW_NW_SRC_MASK:
+ result = result + sep + "OFPFW_NW_SRC"
+ elif w == ofp.OFPFW_NW_DST_MASK:
+ result = result + sep + "OFPFW_NW_DST"
+ else:
+ result = result + sep + ofp.ofp_flow_wildcards_map[w]
+ sep = ", "
+ result = result +"}"
+ if (self.match.wildcards & ofp.OFPFW_IN_PORT) == 0:
+ result = result + (", in_port=%d" % (self.match.in_port))
+ if (self.match.wildcards & ofp.OFPFW_DL_SRC) == 0:
+ result = result + (", dl_src=%s" % (dl_addr_to_str(self.match.dl_src)))
+ if (self.match.wildcards & ofp.OFPFW_DL_DST) == 0:
+ result = result + (", dl_dst=%s" % (dl_addr_to_str(self.match.dl_dst)))
+ if (self.match.wildcards & ofp.OFPFW_DL_VLAN) == 0:
+ result = result + (", dl_vlan=%d" % (self.match.dl_vlan))
+ if (self.match.wildcards & ofp.OFPFW_DL_VLAN_PCP) == 0:
+ result = result + (", dl_vlan_pcp=%d" % (self.match.dl_vlan_pcp))
+ if (self.match.wildcards & ofp.OFPFW_DL_TYPE) == 0:
+ result = result + (", dl_type=0x%x" % (self.match.dl_type))
+ if (self.match.wildcards & ofp.OFPFW_NW_TOS) == 0:
+ result = result + (", nw_tos=0x%x" % (self.match.nw_tos))
+ if (self.match.wildcards & ofp.OFPFW_NW_PROTO) == 0:
+ result = result + (", nw_proto=%d" % (self.match.nw_proto))
+ n = (self.match.wildcards & ofp.OFPFW_NW_SRC_MASK) >> ofp.OFPFW_NW_SRC_SHIFT
+ if n < 32:
+ result = result + (", nw_src=%s" % (ip_addr_to_str(self.match.nw_src, n)))
+ n = (self.match.wildcards & ofp.OFPFW_NW_DST_MASK) >> ofp.OFPFW_NW_DST_SHIFT
+ if n < 32:
+ result = result + (", nw_dst=%s" % (ip_addr_to_str(self.match.nw_dst, n)))
+ if (self.match.wildcards & ofp.OFPFW_TP_SRC) == 0:
+ result = result + (", tp_src=%d" % self.match.tp_src)
+ if (self.match.wildcards & ofp.OFPFW_TP_DST) == 0:
+ result = result + (", tp_dst=%d" % self.match.tp_dst)
+ result = result + (", idle_timeout=%d" % self.idle_timeout)
+ result = result + (", hard_timeout=%d" % self.hard_timeout)
+ result = result + (", hard_timeout=%d" % self.hard_timeout)
+ for a in self.actions.actions:
+ result = result + (", action=%s" % ofp.ofp_action_type_map[a.type])
+ if a.type == ofp.OFPAT_OUTPUT:
+ result = result + ("(%d)" % (a.port))
+ elif a.type == ofp.OFPAT_SET_VLAN_VID:
+ result = result + ("(%d)" % (a.vlan_vid))
+ elif a.type == ofp.OFPAT_SET_VLAN_PCP:
+ result = result + ("(%d)" % (a.vlan_pcp))
+ elif a.type == ofp.OFPAT_SET_DL_SRC or a.type == ofp.OFPAT_SET_DL_DST:
+ result = result + ("(%s)" % (dl_addr_to_str(a.dl_addr)))
+ elif a.type == ofp.OFPAT_SET_NW_SRC or a.type == ofp.OFPAT_SET_NW_DST:
+ result = result + ("(%s)" % (ip_addr_to_str(a.nw_addr, None)))
+ elif a.type == ofp.OFPAT_SET_NW_TOS:
+ result = result + ("(0x%x)" % (a.nw_tos))
+ elif a.type == ofp.OFPAT_SET_TP_SRC or a.type == ofp.OFPAT_SET_TP_DST:
+ result = result + ("(%d)" % (a.tp_port))
+ elif a.type == ofp.OFPAT_ENQUEUE:
+ result = result + ("(port=%d,queue=%d)" % (a.port, a.queue_id))
+ return result
+
+ def rand(self, fi, valid_wildcards, valid_actions, valid_ports):
+ # Start with no wildcards, i.e. everything specified
+ self.match.wildcards = 0
+
+ # Make approx. 1% of flows exact
+ exact = True if random.randint(1, 100) == 1 else False
+
+ # For each qualifier Q,
+ # if (wildcarding is not supported for Q,
+ # or an exact flow is specified
+ # or a coin toss comes up heads),
+ # specify Q
+ # else
+ # wildcard Q
+
+ if (ofp.OFPFW_IN_PORT & valid_wildcards) == 0 \
+ or exact \
+ or random.randint(1, 100) <= 50:
+ self.match.in_port = rand_pick(valid_ports)
+ else:
+ self.match.wildcards = self.match.wildcards | ofp.OFPFW_IN_PORT
+
+ if (ofp.OFPFW_DL_DST & valid_wildcards) == 0 \
+ or exact \
+ or random.randint(1, 100) <= 50:
+ self.match.dl_dst = fi.rand_dl_addr()
+ else:
+ self.match.wildcards = self.match.wildcards | ofp.OFPFW_DL_DST
+
+ if (ofp.OFPFW_DL_SRC & valid_wildcards) == 0 \
+ or exact \
+ or random.randint(1, 100) <= 50:
+ self.match.dl_src = fi.rand_dl_addr()
+ else:
+ self.match.wildcards = self.match.wildcards | ofp.OFPFW_DL_SRC
+
+ if (ofp.OFPFW_DL_VLAN_PCP & valid_wildcards) == 0 \
+ or exact \
+ or random.randint(1, 100) <= 50:
+ self.match.dl_vlan_pcp = random.randint(0, (1 << 3) - 1)
+ else:
+ self.match.wildcards = self.match.wildcards | ofp.OFPFW_DL_VLAN_PCP
+
+ if (ofp.OFPFW_DL_VLAN & valid_wildcards) == 0 \
+ or exact \
+ or random.randint(1, 100) <= 50:
+ self.match.dl_vlan = fi.rand_vlan()
+ else:
+ self.match.wildcards = self.match.wildcards | ofp.OFPFW_DL_VLAN
+
+ if (ofp.OFPFW_DL_TYPE & valid_wildcards) == 0 \
+ or exact \
+ or random.randint(1, 100) <= 50:
+ self.match.dl_type = fi.rand_ethertype()
+ else:
+ self.match.wildcards = self.match.wildcards | ofp.OFPFW_DL_TYPE
+
+ if exact:
+ n = 0
+ else:
+ n = (valid_wildcards & ofp.OFPFW_NW_SRC_MASK) \
+ >> ofp.OFPFW_NW_SRC_SHIFT
+ if n > 32:
+ n = 32
+ n = random.randint(0, n)
+ self.match.wildcards = self.match.wildcards \
+ | (n << ofp.OFPFW_NW_SRC_SHIFT)
+ if n < 32:
+ self.match.nw_src = fi.rand_ip_addr() & ~((1 << n) - 1)
+ # Specifying any IP address match other than all bits
+ # don't care requires that Ethertype is one of {IP, ARP}
+ self.match.dl_type = rand_pick([0x0800, 0x0806])
+ self.match.wildcards = self.match.wildcards & ~ofp.OFPFW_DL_TYPE
+
+ if exact:
+ n = 0
+ else:
+ n = (valid_wildcards & ofp.OFPFW_NW_DST_MASK) \
+ >> ofp.OFPFW_NW_DST_SHIFT
+ if n > 32:
+ n = 32
+ n = random.randint(0, n)
+ self.match.wildcards = self.match.wildcards \
+ | (n << ofp.OFPFW_NW_DST_SHIFT)
+ if n < 32:
+ self.match.nw_dst = fi.rand_ip_addr() & ~((1 << n) - 1)
+ # Specifying any IP address match other than all bits
+ # don't care requires that Ethertype is one of {IP, ARP}
+ self.match.dl_type = rand_pick([0x0800, 0x0806])
+ self.match.wildcards = self.match.wildcards & ~ofp.OFPFW_DL_TYPE
+
+ if (ofp.OFPFW_NW_TOS & valid_wildcards) == 0 \
+ or exact \
+ or random.randint(1, 100) <= 50:
+ self.match.nw_tos = fi.rand_ip_tos()
+ # Specifying a TOS value requires that Ethertype is IP
+ self.match.dl_type = 0x0800
+ self.match.wildcards = self.match.wildcards & ~ofp.OFPFW_DL_TYPE
+ else:
+ self.match.wildcards = self.match.wildcards | ofp.OFPFW_NW_TOS
+
+ if (ofp.OFPFW_NW_PROTO & valid_wildcards) == 0 \
+ or exact \
+ or random.randint(1, 100) <= 50:
+ self.match.nw_proto = fi.rand_ip_proto()
+ # Specifying an IP protocol requires that Ethertype is IP
+ self.match.dl_type = 0x0800
+ self.match.wildcards = self.match.wildcards & ~ofp.OFPFW_DL_TYPE
+ else:
+ self.match.wildcards = self.match.wildcards | ofp.OFPFW_NW_PROTO
+
+ if (ofp.OFPFW_TP_SRC & valid_wildcards) == 0 \
+ or exact\
+ or random.randint(1, 100) <= 50:
+ self.match.tp_src = fi.rand_l4_port()
+ # Specifying a L4 port requires that IP protcol is
+ # one of {ICMP, TCP, UDP}
+ self.match.nw_proto = rand_pick([1, 6, 17])
+ self.match.wildcards = self.match.wildcards & ~ofp.OFPFW_NW_PROTO
+ # Specifying a L4 port requirues that Ethertype is IP
+ self.match.dl_type = 0x0800
+ self.match.wildcards = self.match.wildcards & ~ofp.OFPFW_DL_TYPE
+ else:
+ self.match.wildcards = self.match.wildcards | ofp.OFPFW_TP_SRC
+
+ if (ofp.OFPFW_TP_DST & valid_wildcards) == 0 \
+ or exact \
+ or random.randint(1, 100) <= 50:
+ self.match.tp_dst = fi.rand_l4_port()
+ # Specifying a L4 port requires that IP protcol is
+ # one of {ICMP, TCP, UDP}
+ self.match.nw_proto = rand_pick([1, 6, 17])
+ self.match.wildcards = self.match.wildcards & ~ofp.OFPFW_NW_PROTO
+ # Specifying a L4 port requirues that Ethertype is IP
+ self.match.dl_type = 0x0800
+ self.match.wildcards = self.match.wildcards & ~ofp.OFPFW_DL_TYPE
+ else:
+ self.match.wildcards = self.match.wildcards | ofp.OFPFW_TP_DST
+
+ # N.B. Don't make the timeout too short, else the flow might
+ # disappear before we get a chance to check for it.
+ t = random.randint(0, 65535)
+ self.idle_timeout = 0 if t < 60 else t
+ t = random.randint(0, 65535)
+ self.hard_timeout = 0 if t < 60 else t
+
+ # If nothing is wildcarded, it is an exact flow spec -- some switches
+ # (Open vSwitch, for one) *require* that exact flow specs have priority 65535.
+ self.priority = 65535 if self.match.wildcards == 0 else fi.rand_priority()
+
+ # Action lists are ordered, so pick an ordered random subset of
+ # supported actions
+ supported_actions = []
+ for a in all_actions_list:
+ if ((1 << a) & valid_actions) != 0:
+ supported_actions.append(a)
+
+ supported_actions = shuffle(supported_actions)
+ supported_actions \
+ = supported_actions[0 : random.randint(1, len(supported_actions))]
+
+ self.actions = action_list.action_list()
+ for a in supported_actions:
+ if a == ofp.OFPAT_OUTPUT:
+ # TBD - Output actions are clustered in list, spread them out?
+ port_idxs = shuffle(range(len(valid_ports)))
+ port_idxs = port_idxs[0 : random.randint(1, len(valid_ports))]
+ for pi in port_idxs:
+ act = action.action_output()
+ act.port = valid_ports[pi]
+ self.actions.add(act)
+ elif a == ofp.OFPAT_SET_VLAN_VID:
+ act = action.action_set_vlan_vid()
+ act.vlan_vid = fi.rand_vlan()
+ self.actions.add(act)
+ elif a == ofp.OFPAT_SET_VLAN_PCP:
+ # TBD - Temporaily removed, broken in Indigo
+ #act = action.action_set_vlan_pcp()
+ #act.vlan_pcp = random.randint(0, (1 << 3) - 1)
+ pass
+ elif a == ofp.OFPAT_STRIP_VLAN:
+ act = action.action_strip_vlan()
+ self.actions.add(act)
+ elif a == ofp.OFPAT_SET_DL_SRC:
+ act = action.action_set_dl_src()
+ act.dl_addr = fi.rand_dl_addr()
+ self.actions.add(act)
+ elif a == ofp.OFPAT_SET_DL_DST:
+ act = action.action_set_dl_dst()
+ act.dl_addr = fi.rand_dl_addr()
+ self.actions.add(act)
+ elif a == ofp.OFPAT_SET_NW_SRC:
+ act = action.action_set_nw_src()
+ act.nw_addr = fi.rand_ip_addr()
+ self.actions.add(act)
+ elif a == ofp.OFPAT_SET_NW_DST:
+ act = action.action_set_nw_dst()
+ act.nw_addr = fi.rand_ip_addr()
+ self.actions.add(act)
+ elif a == ofp.OFPAT_SET_NW_TOS:
+ act = action.action_set_nw_tos()
+ act.nw_tos = fi.rand_ip_tos()
+ self.actions.add(act)
+ elif a == ofp.OFPAT_SET_TP_SRC:
+ act = action.action_set_tp_src()
+ act.tp_port = fi.rand_l4_port()
+ self.actions.add(act)
+ elif a == ofp.OFPAT_SET_TP_DST:
+ act = action.action_set_tp_dst()
+ act.tp_port = fi.rand_l4_port()
+ self.actions.add(act)
+ elif a == ofp.OFPAT_ENQUEUE:
+ # TBD - Enqueue actions are clustered in list, spread them out?
+ port_idxs = shuffle(range(len(valid_ports)))
+ port_idxs = port_idxs[0 : random.randint(1, len(valid_ports))]
+ for pi in port_idxs:
+ act = action.action_enqueue()
+ act.port = valid_ports[pi]
+ # TBD - Limits for queue number?
+ act.queue_id = random.randint(0, 7)
+ self.actions.add(act)
+
+ return self
+
+ # Overlap check
+ # delf == True <=> Check for delete overlap, else add overlap
+ # "Add overlap" is defined as there exists a packet that could match both the
+ # receiver and argument flowspecs
+ # "Delete overlap" is defined as the specificity of the argument flowspec
+ # is greater than or equal to the specificity of the receiver flowspec
+ def overlaps(self, x, delf):
+ if self.priority != x.priority:
+ return False
+ if (self.match.wildcards & ofp.OFPFW_IN_PORT) == 0:
+ if (x.match.wildcards & ofp.OFPFW_IN_PORT) == 0:
+ if self.match.in_port != x.match.in_port:
+ return False # Both specified, and not equal
+ elif delf:
+ return False # Receiver more specific
+ if (self.match.wildcards & ofp.OFPFW_DL_VLAN) == 0:
+ if (x.match.wildcards & ofp.OFPFW_DL_VLAN) == 0:
+ if self.match.dl_vlan != x.match.dl_vlan:
+ return False # Both specified, and not equal
+ elif delf:
+ return False # Receiver more specific
+ if (self.match.wildcards & ofp.OFPFW_DL_SRC) == 0:
+ if (x.match.wildcards & ofp.OFPFW_DL_SRC) == 0:
+ if self.match.dl_src != x.match.dl_src:
+ return False # Both specified, and not equal
+ elif delf:
+ return False # Receiver more specific
+ if (self.match.wildcards & ofp.OFPFW_DL_DST) == 0:
+ if (x.match.wildcards & ofp.OFPFW_DL_DST) == 0:
+ if self.match.dl_dst != x.match.dl_dst:
+ return False # Both specified, and not equal
+ elif delf:
+ return False # Receiver more specific
+ if (self.match.wildcards & ofp.OFPFW_DL_TYPE) == 0:
+ if (x.match.wildcards & ofp.OFPFW_DL_TYPE) == 0:
+ if self.match.dl_type != x.match.dl_type:
+ return False # Both specified, and not equal
+ elif delf:
+ return False # Recevier more specific
+ if (self.match.wildcards & ofp.OFPFW_NW_PROTO) == 0:
+ if (x.match.wildcards & ofp.OFPFW_NW_PROTO) == 0:
+ if self.match.nw_proto != x.match.nw_proto:
+ return False # Both specified, and not equal
+ elif delf:
+ return False # Receiver more specific
+ if (self.match.wildcards & ofp.OFPFW_TP_SRC) == 0:
+ if (x.match.wildcards & ofp.OFPFW_TP_SRC) == 0:
+ if self.match.tp_src != x.match.tp_src:
+ return False # Both specified, and not equal
+ elif delf:
+ return False # Receiver more specific
+ if (self.match.wildcards & ofp.OFPFW_TP_DST) == 0:
+ if (x.match.wildcards & ofp.OFPFW_TP_DST) == 0:
+ if self.match.tp_dst != x.match.tp_dst:
+ return False # Both specified, and not equal
+ elif delf:
+ return False # Receiver more specific
+ na = (self.match.wildcards & ofp.OFPFW_NW_SRC_MASK) \
+ >> ofp.OFPFW_NW_SRC_SHIFT
+ nb = (x.match.wildcards & ofp.OFPFW_NW_SRC_MASK) \
+ >> ofp.OFPFW_NW_SRC_SHIFT
+ if delf and na < nb:
+ return False # Receiver more specific
+ if (na < 32 and nb < 32):
+ m = ~((1 << na) - 1) & ~((1 << nb) - 1)
+ if (self.match.nw_src & m) != (x.match.nw_src & m):
+ return False # Overlapping bits not equal
+ na = (self.match.wildcards & ofp.OFPFW_NW_DST_MASK) \
+ >> ofp.OFPFW_NW_DST_SHIFT
+ nb = (x.match.wildcards & ofp.OFPFW_NW_DST_MASK) \
+ >> ofp.OFPFW_NW_DST_SHIFT
+ if delf and na < nb:
+ return False # Receiver more specific
+ if (na < 32 and nb < 32):
+ m = ~((1 << na) - 1) & ~((1 << nb) - 1)
+ if (self.match.nw_dst & m) != (x.match.nw_dst & m):
+ return False # Overlapping bit not equal
+ if (self.match.wildcards & ofp.OFPFW_DL_VLAN_PCP) == 0:
+ if (x.match.wildcards & ofp.OFPFW_DL_VLAN_PCP) == 0:
+ if self.match.dl_vlan_pcp != x.match.dl_vlan_pcp:
+ return False # Both specified, and not equal
+ elif delf:
+ return False # Receiver more specific
+ if (self.match.wildcards & ofp.OFPFW_NW_TOS) == 0:
+ if (x.match.wildcards & ofp.OFPFW_NW_TOS) == 0:
+ if self.match.nw_tos != x.match.nw_tos:
+ return False # Both specified, and not equal
+ elif delf:
+ return False # Receiver more specific
+ return True # Flows overlap
+
+ def to_flow_mod_msg(self, msg):
+ msg.match = self.match
+ msg.idle_timeout = self.idle_timeout
+ msg.hard_timeout = self.hard_timeout
+ msg.priority = self.priority
+ msg.actions = self.actions
+ return msg
+
+ def from_flow_stat(self, msg):
+ self.match = msg.match
+ self.idle_timeout = msg.idle_timeout
+ self.hard_timeout = msg.hard_timeout
+ self.priority = msg.priority
+ self.actions = msg.actions
+
+
+class FlowQuery(basic.SimpleProtocol):
+ """
+ """
+
+ def do_barrier(self):
+ barrier = message.barrier_request()
+ (resp, pkt) = self.controller.transact(barrier, 5)
+ self.assertTrue(resp is not None,
+ "Did not receive response to barrier request"
+ )
+
+
+ def verify_flows(self,
+ sw_features,
+ tbl_flows,
+ num_flows,
+ overlapf,
+ num_overlaps
+ ):
+ result = True
+
+ # Check number of flows reported in table stats
+
+ self.logger.debug("Verifying table stats reports correct number of")
+ self.logger.debug(" active flows")
+ request = message.table_stats_request()
+ (tbl_stats_after, pkt) = self.controller.transact(request, timeout=2)
+ self.assertTrue(tbl_stats_after is not None,
+ "No reply to table_stats_request"
+ )
+
+ num_flows_reported = 0
+ for ts in tbl_stats_after.stats:
+ num_flows_reported = num_flows_reported + ts.active_count
+
+ num_flows_expected = num_flows
+ if overlapf:
+ num_flows_expected = num_flows_expected - num_overlaps
+
+ self.logger.debug("Number of flows reported = "
+ + str(num_flows_reported)
+ )
+ self.logger.debug("Numer of flows expected = "
+ + str(num_flows_expected)
+ )
+ if num_flows_reported != num_flows_expected:
+ self.logger.error("Incorrect number of flows returned by table stats")
+ result = False
+
+ # Retrieve all flows from switch
+
+ self.logger.debug("Retrieving all flows from switch")
+ stat_req = message.flow_stats_request()
+ query_match = ofp.ofp_match()
+ query_match.wildcards = ofp.OFPFW_ALL
+ stat_req.match = query_match
+ stat_req.table_id = 0xff
+ stat_req.out_port = ofp.OFPP_NONE;
+ flow_stats, pkt = self.controller.transact(stat_req, timeout=2)
+ self.assertTrue(flow_stats is not None, "Get all flow stats failed")
+
+ # Verify retrieved flows
+
+ self.logger.debug("Verifying retrieved flows")
+
+ self.assertEqual(flow_stats.type,
+ ofp.OFPST_FLOW,
+ "Unexpected type of response message"
+ )
+
+ num_flows_reported = len(flow_stats.stats)
+
+ self.logger.debug("Number of flows reported = "
+ + str(num_flows_reported)
+ )
+ self.logger.debug("Numer of flows expected = "
+ + str(num_flows_expected)
+ )
+ if num_flows_reported != num_flows_expected:
+ self.logger.error("Incorrect number of flows returned by table stats")
+ result = False
+
+ for f in tbl_flows:
+ f.resp_matched = False
+
+ num_resp_flows_matched = 0
+ for flow_stat in flow_stats.stats:
+ flow_in = flow_cfg()
+ flow_in.from_flow_stat(flow_stat)
+
+ matched = False
+ for f in tbl_flows:
+ if f.deleted:
+ continue
+ if not f.resp_matched \
+ and (not overlapf or not f.overlap) \
+ and f == flow_in:
+ f.resp_matched = True
+ num_resp_flows_matched = num_resp_flows_matched + 1
+ matched = True
+ break
+ if not matched:
+ self.logger.error("Response flow")
+ self.logger.error(str(flow_in))
+ self.logger.error("does not match any configured flow")
+ result = False
+
+ self.logger.debug("Number of flows matched in response = "
+ + str(num_resp_flows_matched)
+ )
+ self.logger.debug("Number of flows expected = "
+ + str(num_flows_expected)
+ )
+ if num_resp_flows_matched != num_flows_expected:
+ for f in tbl_flows:
+ if not f.resp_matched:
+ self.logger.error("Configured flow")
+ self.logger.error("tbl_idx=%d, flow_idx=%d, %s" % (f.tbl_idx, f.flow_idx, str(f)))
+ self.logger.error("missing in flow response")
+ result = False
+
+ self.assertTrue(result, "Flow verification failed")
+
+ def flow_add(self, flow, overlapf):
+ flow_mod_msg = message.flow_mod()
+ flow_mod_msg.command = ofp.OFPFC_ADD
+ flow_mod_msg.buffer_id = 0xffffffff
+ flow_mod_msg.cookie = random.randint(0, (1 << 53) - 1)
+ flow.to_flow_mod_msg(flow_mod_msg)
+ if overlapf:
+ flow_mod_msg.flags = flow_mod_msg.flags | ofp.OFPFF_CHECK_OVERLAP
+ self.logger.debug("Sending flow_mod(add) request to switch")
+ rv = self.controller.message_send(flow_mod_msg)
+ self.assertTrue(rv != -1, "Error installing flow mod")
+
+ # TBD - Don't poll for each error message
+ if flow.overlap:
+ self.logger.debug("Flow overlaps with tbl_idx=%d flow_idx=%d"
+ % (flow.overlaps_with[0], flow.overlaps_with[1])
+ )
+ else:
+ self.logger.debug("Flow does not overlap")
+ self.logger.debug("Checking for error response from switch")
+ (errmsg, pkt) = self.controller.poll(ofp.OFPT_ERROR, 1)
+ if errmsg is not None:
+ # Got ERROR message
+ self.logger.debug("Got ERROR message, type = "
+ + str(errmsg.type)
+ + ", code = "
+ + str(errmsg.code)
+ )
+
+ if errmsg.type == ofp.OFPET_FLOW_MOD_FAILED \
+ and errmsg.code == ofp.OFPFMFC_OVERLAP:
+ # Got "overlap" ERROR message
+ self.logger.debug("ERROR is overlap")
+
+ self.assertTrue(overlapf and flow.overlap,
+ "Overlap not expected"
+ )
+ else:
+ self.logger.debug("ERROR is not overlap")
+ self.assertTrue(False,
+ "Unexpected error message")
+
+ else:
+ # Did not get ERROR message
+ self.logger.debug("No ERROR message received")
+ self.assertTrue(not (overlapf and flow.overlap),
+ "Did not get expected OVERLAP"
+ )
+
+
+ def flow_del(self, flow, strictf):
+ flow_mod_msg = message.flow_mod()
+ flow_mod_msg.command = ofp.OFPFC_DELETE_STRICT \
+ if strictf else ofp.OFPFC_DELETE
+ flow_mod_msg.buffer_id = 0xffffffff
+ flow_mod_msg.cookie = random.randint(0, (1 << 53) - 1)
+ # TBD - Needs to be a test variable
+ flow_mod_msg.out_port = ofp.OFPP_NONE
+ flow.to_flow_mod_msg(flow_mod_msg)
+ rv = self.controller.message_send(flow_mod_msg)
+ self.assertTrue(rv != -1, "Error installing flow mod")
+ # TBD - Don't poll for each error message
+ (errmsg, pkt) = self.controller.poll(ofp.OFPT_ERROR, 1)
+ if errmsg is not None:
+ # Got ERROR message
+ self.logger.debug("Got ERROR message, type = "
+ + str(errmsg.type)
+ + ", code = "
+ + str(errmsg.code)
+ )
+ self.assertTrue(False,
+ "Unexpected error message"
+ )
+
+
+ # Add flows to capacity, make sure they can be read back, and delete them
+
+ def test1(self,
+ overlapf, # True <=> When sending flow adds to
+ # switch, include the "check for
+ # overlap" flag, and verify that an
+ # error message is received
+ # if an overlapping flow is defined
+ strictf # True <=> When deleting flows, delete
+ # them strictly
+ ):
+ """
+ """
+
+ # Clear all flows from switch
+ self.logger.debug("Deleting all flows from switch")
+ rc = delete_all_flows(self.controller, pa_logger)
+ self.assertEqual(rc, 0, "Failed to delete all flows")
+
+ # Get valid port numbers
+ # Get number of tables supported
+ # Get actions supported by switch
+
+ self.logger.debug("Retrieving features from switch")
+ request = message.features_request()
+ (sw_features, pkt) = self.controller.transact(request, timeout=2)
+ self.assertTrue(sw_features is not None, "No reply to features_request")
+ self.logger.debug("Switch features -")
+ self.logger.debug("Number of tables: " + str(sw_features.n_tables))
+ self.logger.debug("Supported actions: " + hex(sw_features.actions))
+ self.logger.debug("Ports: "
+ + str(map(lambda x: x.port_no, sw_features.ports))
+ )
+
+ # For each table, get wildcards supported maximum number of flows
+
+ self.logger.debug("Retrieving table stats from switch")
+ request = message.table_stats_request()
+ (tbl_stats, pkt) = self.controller.transact(request, timeout=2)
+ self.assertTrue(tbl_stats is not None,
+ "No reply to table_stats_request"
+ )
+ active_count = 0
+ max_entries = 0
+ tbl_idx = 0
+ while tbl_idx < sw_features.n_tables:
+ self.logger.debug("Table " + str(tbl_idx) + " - ")
+ self.logger.debug("Supported wildcards: "
+ + hex(tbl_stats.stats[tbl_idx].wildcards)
+ )
+ self.logger.debug("Max entries: "
+ + str(tbl_stats.stats[tbl_idx].max_entries)
+ )
+ self.logger.debug("Active count: "
+ + str(tbl_stats.stats[tbl_idx].active_count)
+ )
+ max_entries = max_entries + tbl_stats.stats[tbl_idx].max_entries
+ active_count = active_count + tbl_stats.stats[tbl_idx].active_count
+ tbl_idx = tbl_idx + 1
+
+ self.logger.debug("Total active entries = "
+ + str(active_count)
+ )
+ self.assertEqual(active_count,
+ 0,
+ "Delete all flows failed"
+ )
+
+ # TBD - For testing only, since Open vSWitch reports
+ # ridiculously large capacity; remove
+ sw_features.n_tables = 1
+ tbl_stats.stats[0].max_entries = 10
+ max_entries = tbl_stats.stats[0].max_entries
+
+ # Dream up some flow information, i.e. space to chose from for
+ # random flow parameter generation
+ fi = flow_info()
+ n = int(math.log(max_entries))
+ if not overlapf:
+ # Generated space smaller when testing overlaps,
+ # to increase likelihood of some
+ n = 2 * n
+ fi.rand(n)
+
+ # For each table, think up flows to fill it
+
+ self.logger.debug("Creating flows")
+ num_flows = 0
+ num_overlaps = 0
+ tbl_flows = []
+ tbl_idx = 0
+
+ while tbl_idx < sw_features.n_tables:
+ flow_idx = 0
+ while flow_idx < tbl_stats.stats[tbl_idx].max_entries:
+ flow_out = flow_cfg()
+ if overlapf and num_flows == 1:
+ # Make 2nd flow a copy of the first,
+ # to guarantee at least 1 overlap
+ flow_out = copy.deepcopy(tbl_flows[0])
+ flow_out.overlap = True
+ flow_out.overlaps_with = [0, 0]
+ num_overlaps = num_overlaps + 1
+ else:
+ flow_out.rand(fi,
+ tbl_stats.stats[tbl_idx].wildcards,
+ sw_features.actions,
+ map(lambda x: x.port_no, sw_features.ports)
+ )
+ flow_out.overlap = False
+
+ for f in tbl_flows:
+ if (not overlapf or not f.overlap) \
+ and flow_out.overlaps(f, False):
+ flow_out.overlap = True
+ flow_out.overlaps_with = [f.tbl_idx, f.flow_idx]
+ num_overlaps = num_overlaps + 1
+
+ flow_out.tbl_idx = tbl_idx
+ flow_out.flow_idx = flow_idx
+
+ self.logger.debug("tbl_idx=%d, flow_idx=%d, %s" % (tbl_idx, flow_idx, str(flow_out)))
+
+ tbl_flows.append(flow_out)
+
+ num_flows = num_flows + 1
+ flow_idx = flow_idx + 1
+ tbl_idx = tbl_idx + 1
+
+ self.logger.debug("Created " + str(num_flows)
+ + " flows, with " + str(num_overlaps)
+ + " overlaps"
+ )
+
+ # Send all flows to switch
+
+ self.logger.debug("Sending flows to switch")
+ for f in tbl_flows:
+ self.flow_add(f, overlapf)
+ f.deleted = False
+
+ # Send barrier, to make sure all flows are in
+ self.do_barrier()
+
+ # Red back all flows from switch, and verify
+
+ self.verify_flows(sw_features,
+ tbl_flows,
+ num_flows,
+ overlapf,
+ num_overlaps
+ )
+
+ # Delete a flows from switch
+
+ if strictf:
+ # Strict delete
+
+ # Delete a few flows, in random order, individually (i.e. strictly)
+
+ del_flow_idxs = shuffle(range(len(tbl_flows)))
+ # TBD - Limited, for testing only; remove
+ del_flow_idxs = del_flow_idxs[0 : random.randint(3, 3)]
+ for di in del_flow_idxs:
+ f = tbl_flows[di]
+ tbl_idx = f.tbl_idx
+ flow_idx = f.flow_idx
+ if (overlapf and f.overlap):
+ self.logger.debug("Flow tbl_idx = " + str(tbl_idx)
+ + ", flow_idx = " + str(flow_idx)
+ + " was an overlap, skipping delete"
+ )
+ else:
+ self.logger.debug("Deleting flow, tbl_idx = "
+ + str(tbl_idx) + ", flow_idx = "
+ + str(flow_idx)
+ )
+ self.flow_del(f, True)
+ f.deleted = True
+ num_flows = num_flows - 1
+
+ # Send barrier, to make sure all flows are deleted
+ self.do_barrier();
+
+ # Red back all flows from switch, and verify
+
+ self.verify_flows(sw_features,
+ tbl_flows,
+ num_flows,
+ overlapf,
+ num_overlaps
+ )
+
+ # Delete all remaining flows, in random order,
+ # individually (i.e. strictly)
+
+ del_flow_idxs = shuffle(range(len(tbl_flows)))
+ for di in del_flow_idxs:
+ f = tbl_flows[di]
+ if f.deleted:
+ continue
+ tbl_idx = f.tbl_idx
+ flow_idx = f.flow_idx
+ if (overlapf and f.overlap):
+ self.logger.debug("Flow tbl_idx = "
+ + str(tbl_idx)
+ + ", flow_idx = "
+ + str(flow_idx)
+ + " was an overlap, skipping delete"
+ )
+ else:
+ self.logger.debug("Deleting flow, tbl_idx = "
+ + str(tbl_idx)
+ + ", flow_idx = "
+ + str(flow_idx)
+ )
+ self.flow_del(f, True)
+ f.deleted = True
+ num_flows = num_flows - 1
+
+ # Send barrier, to make sure all flows are deleted
+ self.do_barrier()
+
+ # Red back all flows from switch (i.e. none), and verify
+
+ self.verify_flows(sw_features,
+ tbl_flows,
+ num_flows,
+ overlapf,
+ num_overlaps
+ )
+
+ else:
+ # Non-strict delete
+
+ # Pick a flow at random that had at least 1 qualifier specified,
+ # wildcard a qualifier that was specified,
+ # and do a non-strict delete
+ # Keep wildcarding specified qualifiers, one by one, and deleteing,
+ # until everything is wildcarded,
+ # and hence all flows should be deleted
+
+ while True:
+ f = tbl_flows[random.randint(0, len(tbl_flows) - 1)]
+ if f.match.wildcards != tbl_stats.stats[f.tbl_idx].wildcards:
+ self.logger.debug("Choosing flow for basis of non-strict delete")
+ self.logger.debug(" tbl_idx=%d flow_idx=%d" % (f.tbl_idx, f.flow_idx))
+ self.logger.debug(" " + str(f))
+ break
+
+ # For each qualifier, in random order, if it was specified,
+ # wildcard it, do a delete, and check the results
+
+ wildcard_idxs = shuffle(range(len(all_wildcards_list)))
+ for wi in wildcard_idxs:
+ w = all_wildcards_list[wi]
+ if (f.match.wildcards & w) != 0:
+ continue
+
+ if w == ofp.OFPFW_NW_SRC_MASK:
+ f.match.wildcards = (f.match.wildcards
+ & ~ofp.OFPFW_NW_SRC_MASK
+ ) \
+ | ofp.OFPFW_NW_SRC_ALL
+ wn = "OFPFW_NW_SRC"
+ elif w == ofp.OFPFW_NW_DST_MASK:
+ f.match.wildcards = (f.match.wildcards
+ & ~ofp.OFPFW_NW_DST_MASK
+ ) \
+ | ofp.OFPFW_NW_DST_ALL
+ wn = "OFPFW_NW_DST"
+ else:
+ f.match.wildcards = f.match.wildcards | w
+ wn = ofp.ofp_flow_wildcards_map[w]
+
+ self.logger.debug("Adding wildcard %s" % (wn))
+ self.logger.debug(str(f))
+
+ # Mark all flows which would be deleted by this
+ # non-strict delete
+
+ for ff in tbl_flows:
+ if not ff.deleted and f.overlaps(ff, True):
+ self.logger.debug("Deleting flow, tbl_idx = "
+ + str(ff.tbl_idx) + ", flow_idx = "
+ + str(ff.flow_idx)
+ )
+ ff.deleted = True
+ num_flows = num_flows - 1
+
+ self.flow_del(f, False)
+
+ # Send barrier, to make sure all flows are deleted
+ self.do_barrier()
+
+ # Red back all flows from switch, and verify
+
+ self.verify_flows(sw_features,
+ tbl_flows,
+ num_flows,
+ overlapf,
+ num_overlaps
+ )
+
+
+
+ def runTest(self):
+ """
+ Run all tests
+ """
+
+ self.test1(False, True) # Test with no overlaps, strict delete
+ self.test1(True, True) # Test with overlaps, strict delete
+# self.test1(False, False) # Test with no overlaps, non-strict delete
+# self.test1(True, False) # Test with overlaps, non-strict delete
+