nat: use correct data types for memory sizes
[vpp.git] / test / vpp_pg_interface.py
old mode 100644 (file)
new mode 100755 (executable)
index 756f79b..32c0eae
@@ -1,8 +1,13 @@
 import os
-import time
 import socket
-from traceback import format_exc
+from socket import inet_pton, inet_ntop
+import struct
+import time
+from traceback import format_exc, format_stack
+
+import scapy.compat
 from scapy.utils import wrpcap, rdpcap, PcapReader
+from scapy.plist import PacketList
 from vpp_interface import VppInterface
 
 from scapy.layers.l2 import Ether, ARP
@@ -11,7 +16,11 @@ from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA,\
     IPv6ExtHdrHopByHop
 from util import ppp, ppc
 from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ismaddr
-from scapy.utils import inet_pton, inet_ntop
+
+
+class CaptureTimeoutError(Exception):
+    """ Exception raised if capture or packet doesn't appear within timeout """
+    pass
 
 
 def is_ipv6_misc(p):
@@ -36,6 +45,18 @@ class VppPGInterface(VppInterface):
         """packet-generator interface index assigned by VPP"""
         return self._pg_index
 
+    @property
+    def gso_enabled(self):
+        """gso enabled on packet-generator interface"""
+        if self._gso_enabled == 0:
+            return "gso-disabled"
+        return "gso-enabled"
+
+    @property
+    def gso_size(self):
+        """gso size on packet-generator interface"""
+        return self._gso_size
+
     @property
     def out_path(self):
         """pcap file path - captured packets"""
@@ -59,6 +80,10 @@ class VppPGInterface(VppInterface):
     @property
     def input_cli(self):
         """CLI string to load the injected packets"""
+        if self._nb_replays is not None:
+            return "%s limit %d" % (self._input_cli, self._nb_replays)
+        if self._worker is not None:
+            return "%s worker %d" % (self._input_cli, self._worker)
         return self._input_cli
 
     @property
@@ -75,67 +100,99 @@ class VppPGInterface(VppInterface):
         self._out_history_counter += 1
         return v
 
-    def __init__(self, test, pg_index):
+    def __init__(self, test, pg_index, gso, gso_size):
         """ Create VPP packet-generator interface """
-        r = test.vapi.pg_create_interface(pg_index)
-        self._sw_if_index = r.sw_if_index
-
         super(VppPGInterface, self).__init__(test)
 
+        r = test.vapi.pg_create_interface(pg_index, gso, gso_size)
+        self.set_sw_if_index(r.sw_if_index)
+
         self._in_history_counter = 0
         self._out_history_counter = 0
+        self._out_assert_counter = 0
         self._pg_index = pg_index
+        self._gso_enabled = gso
+        self._gso_size = gso_size
         self._out_file = "pg%u_out.pcap" % self.pg_index
         self._out_path = self.test.tempdir + "/" + self._out_file
         self._in_file = "pg%u_in.pcap" % self.pg_index
         self._in_path = self.test.tempdir + "/" + self._in_file
         self._capture_cli = "packet-generator capture pg%u pcap %s" % (
             self.pg_index, self.out_path)
-        self._cap_name = "pcap%u" % self.sw_if_index
+        self._cap_name = "pcap%u-sw_if_index-%s" % (
+            self.pg_index, self.sw_if_index)
         self._input_cli = \
             "packet-generator new pcap %s source pg%u name %s" % (
                 self.in_path, self.pg_index, self.cap_name)
+        self._nb_replays = None
 
-    def enable_capture(self):
-        """ Enable capture on this packet-generator interface"""
+    def _rename_previous_capture_file(self, path, counter, file):
+        # if a file from a previous capture exists, rename it.
         try:
-            if os.path.isfile(self.out_path):
-                os.rename(self.out_path,
-                          "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
-                          (self.test.tempdir,
-                           time.time(),
-                           self.name,
-                           self.out_history_counter,
-                           self._out_file))
-        except:
-            pass
+            if os.path.isfile(path):
+                name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % \
+                    (self.test.tempdir,
+                     time.time(),
+                     self.name,
+                     counter,
+                     file)
+                self.test.logger.debug("Renaming %s->%s" %
+                                       (path, name))
+                os.rename(path, name)
+        except OSError:
+            self.test.logger.debug("OSError: Could not rename %s %s" %
+                                   (path, file))
+
+    def enable_capture(self):
+        """ Enable capture on this packet-generator interface
+            of at most n packets.
+            If n < 0, this is no limit
+        """
+        # disable the capture to flush the capture
+        self.disable_capture()
+        self._rename_previous_capture_file(self.out_path,
+                                           self.out_history_counter,
+                                           self._out_file)
         # FIXME this should be an API, but no such exists atm
         self.test.vapi.cli(self.capture_cli)
         self._pcap_reader = None
 
-    def add_stream(self, pkts):
+    def disable_capture(self):
+        self.test.vapi.cli("%s disable" % self.capture_cli)
+
+    def add_stream(self, pkts, nb_replays=None, worker=None):
         """
         Add a stream of packets to this packet-generator
 
         :param pkts: iterable packets
 
         """
-        try:
-            if os.path.isfile(self.in_path):
-                os.rename(self.in_path,
-                          "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
-                          (self.test.tempdir,
-                           time.time(),
-                           self.name,
-                           self.in_history_counter,
-                           self._in_file))
-        except:
-            pass
+        self._worker = worker
+        self._nb_replays = nb_replays
+        self._rename_previous_capture_file(self.in_path,
+                                           self.in_history_counter,
+                                           self._in_file)
         wrpcap(self.in_path, pkts)
         self.test.register_capture(self.cap_name)
         # FIXME this should be an API, but no such exists atm
         self.test.vapi.cli(self.input_cli)
 
+    def generate_debug_aid(self, kind):
+        """ Create a hardlink to the out file with a counter and a file
+        containing stack trace to ease debugging in case of multiple capture
+        files present. """
+        self.test.logger.debug("Generating debug aid for %s on %s" %
+                               (kind, self._name))
+        link_path, stack_path = ["%s/debug_%s_%s_%s.%s" %
+                                 (self.test.tempdir, self._name,
+                                  self._out_assert_counter, kind, suffix)
+                                 for suffix in ["pcap", "stack"]
+                                 ]
+        os.link(self.out_path, link_path)
+        with open(stack_path, "w") as f:
+            f.writelines(format_stack())
+        self._out_assert_counter += 1
+
     def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc):
         """ Helper method to get capture and filter it """
         try:
@@ -150,7 +207,7 @@ class VppPGInterface(VppInterface):
         before = len(output.res)
         if filter_out_fn:
             output.res = [p for p in output.res if not filter_out_fn(p)]
-        removed = len(output.res) - before
+        removed = before - len(output.res)
         if removed:
             self.test.logger.debug(
                 "Filtered out %s packets from capture (returning %s)" %
@@ -184,9 +241,6 @@ class VppPGInterface(VppInterface):
                     name)
         self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % (
             expected_count, based_on, name))
-        if expected_count == 0:
-            raise Exception(
-                "Internal error, expected packet count for %s is 0!" % name)
         while remaining_time > 0:
             before = time.time()
             capture = self._get_capture(remaining_time, filter_out_fn)
@@ -195,8 +249,21 @@ class VppPGInterface(VppInterface):
                 if len(capture.res) == expected_count:
                     # bingo, got the packets we expected
                     return capture
+                elif len(capture.res) > expected_count:
+                    self.test.logger.error(
+                        ppc("Unexpected packets captured:", capture))
+                    break
+                else:
+                    self.test.logger.debug("Partial capture containing %s "
+                                           "packets doesn't match expected "
+                                           "count %s (yet?)" %
+                                           (len(capture.res), expected_count))
+            elif expected_count == 0:
+                # bingo, got None as we expected - return empty capture
+                return PacketList()
             remaining_time -= elapsed_time
         if capture:
+            self.generate_debug_aid("count-mismatch")
             raise Exception("Captured packets mismatch, captured %s packets, "
                             "expected %s packets on %s" %
                             (len(capture.res), expected_count, name))
@@ -214,13 +281,12 @@ class VppPGInterface(VppInterface):
             try:
                 capture = self.get_capture(
                     0, remark=remark, filter_out_fn=filter_out_fn)
-                if not capture:
+                if not capture or len(capture.res) == 0:
                     # junk filtered out, we're good
                     return
-                self.test.logger.error(
-                    ppc("Unexpected packets captured:", capture))
             except:
                 pass
+            self.generate_debug_aid("empty-assert")
             if remark:
                 raise AssertionError(
                     "Non-empty capture file present for interface %s (%s)" %
@@ -229,6 +295,20 @@ class VppPGInterface(VppInterface):
                 raise AssertionError("Capture file present for interface %s" %
                                      self.name)
 
+    def wait_for_pg_stop(self):
+        # wait till packet-generator is stopped
+        # "show packet-generator" while it is still running gives this:
+        # Name               Enabled        Count     Parameters
+        # pcap0-sw_if_inde     Yes           64       limit 64, ...
+        #
+        # also have a 5-minute timeout just in case things go terribly wrong...
+        deadline = time.time() + 300
+        while self.test.vapi.cli('show packet-generator').find("Yes") != -1:
+            self._test.sleep(0.01)  # yield
+            if time.time() > deadline:
+                self.test.logger.debug("Timeout waiting for pg to stop")
+                break
+
     def wait_for_capture_file(self, timeout=1):
         """
         Wait until pcap capture file appears
@@ -237,27 +317,52 @@ class VppPGInterface(VppInterface):
 
         :returns: True/False if the file is present or appears within timeout
         """
-        limit = time.time() + timeout
+        self.wait_for_pg_stop()
+        deadline = time.time() + timeout
         if not os.path.isfile(self.out_path):
             self.test.logger.debug("Waiting for capture file %s to appear, "
                                    "timeout is %ss" % (self.out_path, timeout))
         else:
-            self.test.logger.debug(
-                "Capture file %s already exists" %
-                self.out_path)
+            self.test.logger.debug("Capture file %s already exists" %
+                                   self.out_path)
             return True
-        while time.time() < limit:
+        while time.time() < deadline:
             if os.path.isfile(self.out_path):
                 break
-            time.sleep(0)  # yield
+            self._test.sleep(0)  # yield
         if os.path.isfile(self.out_path):
             self.test.logger.debug("Capture file appeared after %fs" %
-                                   (time.time() - (limit - timeout)))
+                                   (time.time() - (deadline - timeout)))
         else:
             self.test.logger.debug("Timeout - capture file still nowhere")
             return False
         return True
 
+    def verify_enough_packet_data_in_pcap(self):
+        """
+        Check if enough data is available in file handled by internal pcap
+        reader so that a whole packet can be read.
+
+        :returns: True if enough data present, else False
+        """
+        orig_pos = self._pcap_reader.f.tell()  # save file position
+        enough_data = False
+        # read packet header from pcap
+        packet_header_size = 16
+        caplen = None
+        end_pos = None
+        hdr = self._pcap_reader.f.read(packet_header_size)
+        if len(hdr) == packet_header_size:
+            # parse the capture length - caplen
+            sec, usec, caplen, wirelen = struct.unpack(
+                self._pcap_reader.endian + "IIII", hdr)
+            self._pcap_reader.f.seek(0, 2)  # seek to end of file
+            end_pos = self._pcap_reader.f.tell()  # get position at end
+            if end_pos >= orig_pos + len(hdr) + caplen:
+                enough_data = True  # yay, we have enough data
+        self._pcap_reader.f.seek(orig_pos, 0)  # restore original position
+        return enough_data
+
     def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc):
         """
         Wait for next packet captured with a timeout
@@ -270,23 +375,31 @@ class VppPGInterface(VppInterface):
         deadline = time.time() + timeout
         if self._pcap_reader is None:
             if not self.wait_for_capture_file(timeout):
-                raise Exception("Capture file %s did not appear within "
-                                "timeout" % self.out_path)
+                raise CaptureTimeoutError("Capture file %s did not appear "
+                                          "within timeout" % self.out_path)
             while time.time() < deadline:
                 try:
                     self._pcap_reader = PcapReader(self.out_path)
                     break
                 except:
                     self.test.logger.debug(
-                        "Exception in scapy.PcapReader(%s): "
-                        "%s" %
+                        "Exception in scapy.PcapReader(%s): %s" %
                         (self.out_path, format_exc()))
         if not self._pcap_reader:
-            raise Exception("Capture file %s did not appear within "
-                            "timeout" % self.out_path)
+            raise CaptureTimeoutError("Capture file %s did not appear within "
+                                      "timeout" % self.out_path)
 
-        self.test.logger.debug("Waiting for packet")
-        while time.time() < deadline:
+        poll = False
+        if timeout > 0:
+            self.test.logger.debug("Waiting for packet")
+        else:
+            poll = True
+            self.test.logger.debug("Polling for packet")
+        while time.time() < deadline or poll:
+            if not self.verify_enough_packet_data_in_pcap():
+                self._test.sleep(0)  # yield
+                poll = False
+                continue
             p = self._pcap_reader.recv()
             if p is not None:
                 if filter_out_fn is not None and filter_out_fn(p):
@@ -298,9 +411,10 @@ class VppPGInterface(VppInterface):
                         "Packet received after %fs" %
                         (time.time() - (deadline - timeout)))
                     return p
-            time.sleep(0)  # yield
+            self._test.sleep(0)  # yield
+            poll = False
         self.test.logger.debug("Timeout - no packets received")
-        raise Exception("Packet didn't arrive within timeout")
+        raise CaptureTimeoutError("Packet didn't arrive within timeout")
 
     def create_arp_req(self):
         """Create ARP request applicable for this interface"""
@@ -341,10 +455,6 @@ class VppPGInterface(VppInterface):
                                   pg_interface.name)
             return
         arp_reply = captured_packet.copy()  # keep original for exception
-        # Make Dot1AD packet content recognizable to scapy
-        if arp_reply.type == 0x88a8:
-            arp_reply.type = 0x8100
-            arp_reply = Ether(str(arp_reply))
         try:
             if arp_reply[ARP].op == ARP.is_at:
                 self.test.logger.info("VPP %s MAC address is %s " %
@@ -388,10 +498,6 @@ class VppPGInterface(VppInterface):
                     "Timeout while waiting for NDP response")
                 raise
             ndp_reply = captured_packet.copy()  # keep original for exception
-            # Make Dot1AD packet content recognizable to scapy
-            if ndp_reply.type == 0x88a8:
-                ndp_reply.type = 0x8100
-                ndp_reply = Ether(str(ndp_reply))
             try:
                 ndp_na = ndp_reply[ICMPv6ND_NA]
                 opt = ndp_na[ICMPv6NDOptDstLLAddr]