Merge pull request #28 from eswierk/master

Various fixes and improvements
diff --git a/profiles/noing.py b/profiles/noing.py
index bfd1c70..4389dd4 100644
--- a/profiles/noing.py
+++ b/profiles/noing.py
@@ -18,3 +18,7 @@
     "FloodPlusIngress",
     "ModifyVIDToIngress",
 ]
+
+#@var run_test_list List of tests to run which would normally be skipped
+run_test_list = [
+]
diff --git a/src/python/oftest/dataplane.py b/src/python/oftest/dataplane.py
index 717a85f..76aff75 100644
--- a/src/python/oftest/dataplane.py
+++ b/src/python/oftest/dataplane.py
@@ -30,7 +30,9 @@
 have_pypcap = False
 try:
     import pcap
-    have_pypcap = True
+    if hasattr(pcap, "pcap"):
+        # the incompatible pylibpcap library masquerades as pcap
+        have_pypcap = True
 except:
     pass
 
diff --git a/tests/bsn_mirror.py b/tests/bsn_mirror.py
new file mode 100644
index 0000000..c0ecc07
--- /dev/null
+++ b/tests/bsn_mirror.py
@@ -0,0 +1,168 @@
+"""
+"""
+import struct
+
+import logging
+
+from oftest import config
+import oftest.controller as controller
+import oftest.cstruct as ofp
+import oftest.message as message
+import oftest.action as action
+import oftest.action_list as action_list
+import oftest.base_tests as base_tests
+
+from oftest.testutils import *
+
+class bsn_action_mirror(action.action_vendor):
+    def __init__(self):
+        self.type = ofp.OFPAT_VENDOR
+        self.len = 24
+        self.vendor = 0x005c16c7
+        self.subtype = 1
+        self.dest_port = 0
+        self.vlan_tag = 0
+        self.copy_stage = 0
+
+    def __assert(self):
+        return (True, None)
+
+    def pack(self, assertstruct=True):
+        return struct.pack("!HHLLLLBBBB", self.type, self.len, self.vendor,
+                           self.subtype, self.dest_port, self.vlan_tag,
+                           self.copy_stage, 0, 0, 0)
+
+    def unpack(self, binaryString):
+        if len(binaryString) < self.len:
+            raise Exception("too short")
+        x = struct.unpack("!HHLLLLBBBB", binaryString[:self.len])
+        if x[0] != self.type:
+            raise Exception("wrong type")
+        if x[1] != self.len:
+            raise Exception("wrong length")
+        if x[2] != self.vendor:
+            raise Exception("wrong vendor")
+        if x[3] != self.subtype:
+            raise Exception("wrong subtype")
+        self.dest_port = x[4]
+        self.vlan_tag = x[5]
+        self.copy_stage = x[6]
+        return binaryString[self.len:]
+
+    def __len__(self):
+        return self.len
+
+    def __eq__(self, other):
+        if type(self) != type(other): return False
+        if self.type != other.type: return False
+        if self.len != other.len: return False
+        if self.vendor != other.vendor: return False
+        if self.subtype != other.subtype: return False
+        if self.dest_port != other.dest_port: return False
+        if self.vlan_tag != other.vlan_tag: return False
+        if self.copy_stage != other.copy_stage: return False
+        return True
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
+    def show(self, prefix=""):
+        outstr = prefix + "action_vendor\n"
+        for f in ["type", "len", "vendor", "subtype", "dest_port", "vlan_tag",
+                  "copy_stage"]:
+            outstr += prefix + ("%s: %s\n" % (f, getattr(self, f)))
+        return outstr
+
+action_list.action_object_map[ofp.OFPAT_VENDOR] = bsn_action_mirror
+
+class BSNMirrorAction(base_tests.SimpleDataPlane):
+    """
+    Exercise BSN vendor extension for copying packets to a mirror destination
+    port
+    """
+
+    priority = -1
+
+    def bsn_set_mirroring(self, enabled):
+        """
+        Use the BSN_SET_MIRRORING vendor command to enable/disable
+        mirror action support
+        """
+        m = message.vendor()
+        m.vendor = 0x005c16c7
+        m.data = struct.pack("!LBBBB", 3, enabled, 0, 0, 0)
+        rc = self.controller.message_send(m)
+        self.assertNotEqual(rc, -1, "Error sending set mirroring command")
+
+    def bsn_get_mirroring(self):
+        """
+        Use the BSN_GET_MIRRORING_REQUEST vendor command to get the
+        enabled/disabled state of mirror action support
+        """
+        m = message.vendor()
+        m.vendor = 0x005c16c7
+        m.data = struct.pack("!LBBBB", 4, 0, 0, 0, 0)
+        rc = self.controller.message_send(m)
+        self.assertNotEqual(rc, -1, "Error sending get mirroring command")
+        m, r = self.controller.poll(ofp.OFPT_VENDOR, 2)
+        self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID")
+        x = struct.unpack("!LBBBB", m.data)
+        self.assertEqual(x[0], 5, "Wrong subtype")
+        return x[1]
+
+    def runTest(self):
+        mirror_ports = test_param_get("mirror_ports")
+        ports = [p for p in config["port_map"].keys() if p not in mirror_ports]
+        pkt = simple_tcp_packet()
+        match = packet_to_flow_match(self, pkt)
+        match.in_port = ports[0]
+        match.wildcards &= ~ofp.OFPFW_IN_PORT
+
+        logging.info("Checking that mirror ports are not reported")
+        self.assertEqual(bool(self.bsn_get_mirroring()), False)
+        m, r = self.controller.transact(message.features_request(), 2)
+        p = dict([(pt.port_no, pt) for pt in m.ports])
+        self.assertFalse(mirror_ports[0] in p or mirror_ports[1] in p,
+                         "Mirror port in features reply")
+
+        logging.info("Enabling mirror port reporting")
+        self.bsn_set_mirroring(True)
+
+        logging.info("Checking that mirror ports are reported")
+        self.assertEqual(bool(self.bsn_get_mirroring()), True)
+        m, r = self.controller.transact(message.features_request(), 2)
+        p = dict([(pt.port_no, pt) for pt in m.ports])
+        self.assertTrue(mirror_ports[0] in p and mirror_ports[1] in p,
+                        "Mirror port not in features reply")
+        self.assertTrue(p[mirror_ports[0]].config & (1 << 31),
+                        "Mirror port config flag not set in features reply")
+        self.assertTrue(p[mirror_ports[1]].config & (1 << 31),
+                        "Mirror port config flag not set in features reply")
+
+        act1 = bsn_action_mirror()
+        act1.dest_port = mirror_ports[0]
+        act1.copy_stage = 0
+        act2 = bsn_action_mirror()
+        act2.dest_port = mirror_ports[1]
+        act2.copy_stage = 0
+        act3 = action.action_output()
+        act3.port = ports[1]
+        flow_mod = message.flow_mod()
+        flow_mod.match = match
+        self.assertTrue(flow_mod.actions.add(act1), "Could not add mirror action")
+        self.assertTrue(flow_mod.actions.add(act2), "Could not add mirror action")
+        self.assertTrue(flow_mod.actions.add(act3), "Could not add output action")
+        self.assertEqual(delete_all_flows(self.controller), 0,
+                         "Failed to delete all flows")
+        self.assertNotEqual(self.controller.message_send(flow_mod), -1,
+                            "Error installing flow mod")
+        self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
+        
+        logging.info("Sending packet to port %s" % ports[0])
+        self.dataplane.send(ports[0], str(pkt))
+        logging.info("Checking that packet was received from output port %s, "
+                     "mirror ports %s and %s" % (
+              ports[1], mirror_ports[0], mirror_ports[1]))
+        receive_pkt_check(self.dataplane, pkt,
+                          [ports[1], mirror_ports[0], mirror_ports[1]], [],
+                          self)
diff --git a/tests/load.py b/tests/load.py
index dcd873b..c2f9013 100644
--- a/tests/load.py
+++ b/tests/load.py
@@ -200,3 +200,46 @@
                 break
             in_count += 1
         logging.info("PacketOutLoad Sent %d. Got %d." % (out_count, in_count))
+
+class FlowModLoad(base_tests.SimpleProtocol):
+
+    def checkBarrier(self):
+        msg, pkt = self.controller.transact(message.barrier_request(), timeout=60)
+        self.assertNotEqual(msg, None, "Barrier failed")
+        while self.controller.packets:
+           msg = self.controller.packets.pop(0)[0]
+           self.assertNotEqual(msg.header.type, message.OFPT_ERROR,
+                               "Error received")
+
+    def runTest(self):
+        msg, pkt = self.controller.transact(message.table_stats_request())
+        num_flows = msg.stats[0].max_entries
+
+        requests = []
+        for i in range(num_flows):
+            match = ofp.ofp_match()
+            match.wildcards = ofp.OFPFW_ALL & ~ofp.OFPFW_DL_VLAN & ~ofp.OFPFW_DL_DST
+            match.dl_vlan = ofp.OFP_VLAN_NONE
+            match.dl_dst = [0, 1, 2, 3, i / 256, i % 256]
+            act = action.action_output()
+            act.port = ofp.OFPP_CONTROLLER
+            request = message.flow_mod()
+            request.command = ofp.OFPFC_ADD
+            request.buffer_id = 0xffffffff
+            request.priority = num_flows - i
+            request.out_port = ofp.OFPP_NONE
+            request.match = match
+            request.actions.add(act)
+            requests.append(request)
+
+        for i in range(5):
+            logging.info("Iteration %d: delete all flows" % i)
+            self.assertEqual(delete_all_flows(self.controller), 0,
+                             "Failed to delete all flows")
+            self.checkBarrier()
+
+            logging.info("Iteration %d: add %s flows" % (i, num_flows))
+            for request in requests:
+               self.assertNotEqual(self.controller.message_send(request), -1,
+                               "Error installing flow mod")
+            self.checkBarrier()
diff --git a/tests/pktact.py b/tests/pktact.py
index ff6927f..d543bd7 100644
--- a/tests/pktact.py
+++ b/tests/pktact.py
@@ -1953,7 +1953,7 @@
 
         delete_all_flows(self.controller)
 
-        pkt = simple_tcp_packet()
+        pkt = simple_tcp_packet(dl_vlan_enable=True, dl_vlan=2)
         ingress_port = of_ports[0]
         egress_port = of_ports[1]