make test: filter IPv6 RAs out by default

Most of the test cases are not interested in IPv6 Router Alerts,
so change the default behaviour of get_capture to filter out
these packets with the possibility of turning the filtering off,
for test cases which are interested in the RAs.

Change-Id: I0b5ee685f82c49cd32c6d6a4638eb3493d2988fc
Signed-off-by: Klement Sekera <ksekera@cisco.com>
diff --git a/test/test_gre.py b/test/test_gre.py
index 59d03e9..f00e446 100644
--- a/test/test_gre.py
+++ b/test/test_gre.py
@@ -12,7 +12,7 @@
 from scapy.packet import Raw
 from scapy.layers.l2 import Ether, Dot1Q, GRE
 from scapy.layers.inet import IP, UDP
-from scapy.layers.inet6 import ICMPv6ND_RA, IPv6
+from scapy.layers.inet6 import IPv6
 from scapy.volatile import RandMAC, RandIP
 
 from util import ppp, ppc
@@ -129,18 +129,9 @@
             pkts.append(p)
         return pkts
 
-    def verify_filter(self, capture, sent):
-        if not len(capture) == len(sent):
-            # filter out any IPv6 RAs from the capture
-            for p in capture:
-                if (p.haslayer(ICMPv6ND_RA)):
-                    capture.remove(p)
-        return capture
-
     def verify_tunneled_4o4(self, src_if, capture, sent,
                             tunnel_src, tunnel_dst):
 
-        capture = self.verify_filter(capture, sent)
         self.assertEqual(len(capture), len(sent))
 
         for i in range(len(capture)):
@@ -169,7 +160,6 @@
 
     def verify_tunneled_l2o4(self, src_if, capture, sent,
                              tunnel_src, tunnel_dst):
-        capture = self.verify_filter(capture, sent)
         self.assertEqual(len(capture), len(sent))
 
         for i in range(len(capture)):
@@ -203,7 +193,6 @@
     def verify_tunneled_vlano4(self, src_if, capture, sent,
                                tunnel_src, tunnel_dst, vlan):
         try:
-            capture = self.verify_filter(capture, sent)
             self.assertEqual(len(capture), len(sent))
         except:
             ppc("Unexpected packets captured:", capture)
@@ -242,7 +231,6 @@
                 raise
 
     def verify_decapped_4o4(self, src_if, capture, sent):
-        capture = self.verify_filter(capture, sent)
         self.assertEqual(len(capture), len(sent))
 
         for i in range(len(capture)):
@@ -266,7 +254,6 @@
                 raise
 
     def verify_decapped_6o4(self, src_if, capture, sent):
-        capture = self.verify_filter(capture, sent)
         self.assertEqual(len(capture), len(sent))
 
         for i in range(len(capture)):
diff --git a/test/test_lb.py b/test/test_lb.py
index 7037d80..9b5baae 100644
--- a/test/test_lb.py
+++ b/test/test_lb.py
@@ -1,7 +1,7 @@
 import socket
 
 from scapy.layers.inet import IP, UDP
-from scapy.layers.inet6 import ICMPv6ND_RA, IPv6
+from scapy.layers.inet6 import IPv6
 from scapy.layers.l2 import Ether, GRE
 from scapy.packet import Raw
 
@@ -95,16 +95,7 @@
         self.assertEqual(str(inner), str(self.info.data[IPver]))
 
     def checkCapture(self, gre4, isv4):
-        # RA might appear in capture
-        try:
-            out = self.pg0.get_capture()
-            # filter out any IPv6 RAs from the capture
-            for p in out:
-                if (p.haslayer(ICMPv6ND_RA)):
-                    out.remove(p)
-            self.assertEqual(len(out), 0)
-        except:
-            pass
+        self.pg0.assert_nothing_captured()
         out = self.pg1.get_capture()
         self.assertEqual(len(out), len(self.packets))
 
diff --git a/test/vpp_pg_interface.py b/test/vpp_pg_interface.py
index 44bd1a2..634d7d3 100644
--- a/test/vpp_pg_interface.py
+++ b/test/vpp_pg_interface.py
@@ -5,10 +5,22 @@
 
 from scapy.layers.l2 import Ether, ARP
 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA,\
-    ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr
+    ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr, ICMPv6ND_RA, RouterAlert, \
+    IPv6ExtHdrHopByHop
 from util import ppp, ppc
 
 
+def is_ipv6_misc(p):
+    """ Is packet one of uninteresting IPv6 broadcasts? """
+    if p.haslayer(ICMPv6ND_RA):
+        return True
+    if p.haslayer(IPv6ExtHdrHopByHop):
+        for o in p[IPv6ExtHdrHopByHop].options:
+            if isinstance(o, RouterAlert):
+                return True
+    return False
+
+
 class VppPGInterface(VppInterface):
     """
     VPP packet-generator interface
@@ -118,15 +130,19 @@
         # FIXME this should be an API, but no such exists atm
         self.test.vapi.cli(self.input_cli)
 
-    def get_capture(self, remark=None):
+    def get_capture(self, remark=None, filter_fn=is_ipv6_misc):
         """
         Get captured packets
 
+        :param remark: remark printed into debug logs
+        :param filter_fn: filter applied to each packet, packets for which
+                          the filter returns True are removed from capture
         :returns: iterable packets
         """
         try:
             self.wait_for_capture_file()
             output = rdpcap(self.out_path)
+            self.test.logger.debug("Capture has %s packets" % len(output.res))
         except IOError:  # TODO
             self.test.logger.debug("File %s does not exist, probably because no"
                                    " packets arrived" % self.out_path)
@@ -135,12 +151,20 @@
                                 (self.name, remark))
             else:
                 raise Exception("No packets captured on %s" % self.name)
+        before = len(output.res)
+        if filter_fn:
+            output.res = [p for p in output.res if not filter_fn(p)]
+        removed = len(output.res) - before
+        if removed:
+            self.test.logger.debug(
+                "Filtered out %s packets from capture (returning %s)" %
+                (removed, len(output.res)))
         return output
 
     def assert_nothing_captured(self, remark=None):
         if os.path.isfile(self.out_path):
             try:
-                capture = self.get_capture()
+                capture = self.get_capture(remark=remark)
                 self.test.logger.error(
                     ppc("Unexpected packets captured:", capture))
             except:
@@ -234,7 +258,7 @@
         self.test.pg_start()
         self.test.logger.info(self.test.vapi.cli("show trace"))
         try:
-            arp_reply = pg_interface.get_capture()
+            arp_reply = pg_interface.get_capture(filter_fn=None)
         except:
             self.test.logger.info("No ARP received on port %s" %
                                   pg_interface.name)
@@ -274,7 +298,7 @@
         pg_interface.enable_capture()
         self.test.pg_start()
         self.test.logger.info(self.test.vapi.cli("show trace"))
-        replies = pg_interface.get_capture()
+        replies = pg_interface.get_capture(filter_fn=None)
         if replies is None or len(replies) == 0:
             self.test.logger.info(
                 "No NDP received on port %s" %