diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile | 3 | ||||
-rw-r--r-- | test/framework.py | 22 | ||||
-rw-r--r-- | test/hook.py | 14 | ||||
-rw-r--r-- | test/patches/scapy-2.3.3/inet6.py.patch | 25 | ||||
-rwxr-xr-x | test/sys_req/set_system_parameters.sh | 57 | ||||
-rw-r--r-- | test/sys_req/system_parameters | 18 | ||||
-rw-r--r-- | test/test_reassembly.py | 966 | ||||
-rw-r--r-- | test/util.py | 195 | ||||
-rw-r--r-- | test/vpp_gre_interface.py | 28 | ||||
-rw-r--r-- | test/vpp_interface.py | 12 | ||||
-rw-r--r-- | test/vpp_lo_interface.py | 12 | ||||
-rw-r--r-- | test/vpp_papi_provider.py | 23 | ||||
-rw-r--r-- | test/vpp_punt_socket.py | 75 |
13 files changed, 1392 insertions, 58 deletions
diff --git a/test/Makefile b/test/Makefile index f3b7f5c6d51..155325a9f17 100644 --- a/test/Makefile +++ b/test/Makefile @@ -88,7 +88,7 @@ $(PAPI_INSTALL_DONE): $(PIP_PATCH_DONE) @touch $@ define retest-func - @env VPP_TEST_FAILED_DIR=$(VPP_TEST_FAILED_DIR) scripts/setsid_wrapper.sh $(FORCE_FOREGROUND) $(PYTHON_VENV_PATH)/bin/activate python run_tests.py -d $(TEST_DIR) $(UNITTEST_EXTRA_OPTS) || env VPP_TEST_FAILED_DIR=$(VPP_TEST_FAILED_DIR) COMPRESS_FAILED_TEST_LOGS=$(COMPRESS_FAILED_TEST_LOGS) scripts/compress_failed.sh +@env VPP_TEST_FAILED_DIR=$(VPP_TEST_FAILED_DIR) scripts/setsid_wrapper.sh $(FORCE_FOREGROUND) $(PYTHON_VENV_PATH)/bin/activate python run_tests.py -d $(TEST_DIR) $(UNITTEST_EXTRA_OPTS) || env VPP_TEST_FAILED_DIR=$(VPP_TEST_FAILED_DIR) COMPRESS_FAILED_TEST_LOGS=$(COMPRESS_FAILED_TEST_LOGS) scripts/compress_failed.sh endef .PHONY: sanity @@ -103,6 +103,7 @@ endif sanity: verify-no-running-vpp @sys_req/dev_shm_size.sh + @sys_req/set_system_parameters.sh sys_req/system_parameters @bash -c "$(SANITY_IMPORT_VPP_PAPI_CMD) ||\ (echo \"*******************************************************************\" &&\ echo \"* Sanity check failed, cannot import vpp_papi\" &&\ diff --git a/test/framework.py b/test/framework.py index 2e3f9781e5d..fc6f550184f 100644 --- a/test/framework.py +++ b/test/framework.py @@ -24,6 +24,7 @@ from vpp_lo_interface import VppLoInterface from vpp_papi_provider import VppPapiProvider from log import * from vpp_object import VppObjectRegistry +from vpp_punt_socket import vpp_uds_socket_name if os.name == 'posix' and sys.version_info[0] < 3: # using subprocess32 is recommended by python official documentation # @ https://docs.python.org/2/library/subprocess.html @@ -255,7 +256,8 @@ class VppTestCase(unittest.TestCase): coredump_size, "}", "api-trace", "{", "on", "}", "api-segment", "{", "prefix", cls.shm_prefix, "}", "plugins", "{", "plugin", "dpdk_plugin.so", "{", - "disable", "}", "}"] + "disable", "}", "}", + "punt", "{", "socket", cls.punt_socket_path, "}"] if plugin_path is not None: cls.vpp_cmdline.extend(["plugin_path", plugin_path]) cls.logger.info("vpp_cmdline: %s" % cls.vpp_cmdline) @@ -317,7 +319,7 @@ class VppTestCase(unittest.TestCase): Remove shared memory files, start vpp and connect the vpp-api """ gc.collect() # run garbage collection first - random.seed() + random.seed(1) cls.logger = getLogger(cls.__name__) cls.tempdir = tempfile.mkdtemp( prefix='vpp-unittest-%s-' % cls.__name__) @@ -328,6 +330,7 @@ class VppTestCase(unittest.TestCase): cls.file_handler.setLevel(DEBUG) cls.logger.addHandler(cls.file_handler) cls.shm_prefix = cls.tempdir.split("/")[-1] + cls.punt_socket_path = '%s/%s' % (cls.tempdir, vpp_uds_socket_name) os.chdir(cls.tempdir) cls.logger.info("Temporary dir is %s, shm prefix is %s", cls.tempdir, cls.shm_prefix) @@ -499,13 +502,16 @@ class VppTestCase(unittest.TestCase): type(self).test_instance = self @classmethod - def pg_enable_capture(cls, interfaces): + def pg_enable_capture(cls, interfaces=None): """ Enable capture on packet-generator interfaces - :param interfaces: iterable interface indexes + :param interfaces: iterable interface indexes (if None, + use self.pg_interfaces) """ + if interfaces is None: + interfaces = cls.pg_interfaces for i in interfaces: i.enable_capture() @@ -573,19 +579,21 @@ class VppTestCase(unittest.TestCase): return result @staticmethod - def extend_packet(packet, size): + def extend_packet(packet, size, padding=' '): """ - Extend packet to given size by padding with spaces + Extend packet to given size by padding with spaces or custom padding NOTE: Currently works only when Raw layer is present. :param packet: packet :param size: target size + :param padding: padding used to extend the payload """ packet_len = len(packet) + 4 extend = size - packet_len if extend > 0: - packet[Raw].load += ' ' * extend + num = (extend / len(padding)) + 1 + packet[Raw].load += (padding * num)[:extend] @classmethod def reset_packet_infos(cls): diff --git a/test/hook.py b/test/hook.py index 77a2fc5bb23..0ef1377ba99 100644 --- a/test/hook.py +++ b/test/hook.py @@ -2,7 +2,7 @@ import signal import os import traceback from log import RED, single_line_delim, double_line_delim -from debug import spawn_gdb, gdb_path +from debug import spawn_gdb class Hook(object): @@ -62,14 +62,10 @@ class PollHook(Hook): def on_crash(self, core_path): if self.testcase.debug_core: - if not spawn_gdb(self.testcase.vpp_bin, core_path, self.logger): - self.logger.error( - "Debugger '%s' does not exist or is not an executable.." % - gdb_path) - else: - return - self.logger.critical("Core file present, debug with: gdb %s %s" % - (self.testcase.vpp_bin, core_path)) + spawn_gdb(self.testcase.vpp_bin, core_path, self.logger) + else: + self.logger.critical("Core file present, debug with: gdb %s %s" % + (self.testcase.vpp_bin, core_path)) def poll_vpp(self): """ diff --git a/test/patches/scapy-2.3.3/inet6.py.patch b/test/patches/scapy-2.3.3/inet6.py.patch index cf3217eb672..706e19738c4 100644 --- a/test/patches/scapy-2.3.3/inet6.py.patch +++ b/test/patches/scapy-2.3.3/inet6.py.patch @@ -1,7 +1,8 @@ diff --git a/scapy/layers/inet6.py b/scapy/layers/inet6.py ---- a/scapy/layers/inet6.py 2017-06-01 14:04:18.160881034 +0200 -+++ b/scapy/layers/inet6.py 2017-06-02 09:08:40.133800208 +0200 -@@ -369,6 +369,8 @@ +index 03b80ec..06ef27f 100644 +--- a/scapy/layers/inet6.py ++++ b/scapy/layers/inet6.py +@@ -369,6 +369,8 @@ class _IPv6GuessPayload: return Raw elif self.nh == 135 and len(p) > 3: # Mobile IPv6 return _mip6_mhtype2cls.get(ord(p[2]), MIP6MH_Generic) @@ -9,11 +10,11 @@ diff --git a/scapy/layers/inet6.py b/scapy/layers/inet6.py + return IPv6ExtHdrSegmentRouting else: return get_cls(ipv6nhcls.get(self.nh,"Raw"), "Raw") - -@@ -430,6 +432,14 @@ + +@@ -430,6 +432,14 @@ class IPv6(_IPv6GuessPayload, Packet, IPTools): sd = strxor(sd, a) sd = inet_ntop(socket.AF_INET6, sd) - + + if self.nh == 43 and isinstance(self.payload, IPv6ExtHdrSegmentRouting): + # With segment routing header (rh == 4), the destination is + # the first address of the IPv6 addresses list @@ -23,10 +24,10 @@ diff --git a/scapy/layers/inet6.py b/scapy/layers/inet6.py + sd = self.dst + if self.nh == 44 and isinstance(self.payload, IPv6ExtHdrFragment): - nh = self.payload.nh - -@@ -489,6 +499,8 @@ - return self.payload.answers(other.payload.payload) + nh = self.payload.nh + +@@ -489,6 +499,8 @@ class IPv6(_IPv6GuessPayload, Packet, IPTools): + return self.payload.answers(other.payload.payload) elif other.nh == 43 and isinstance(other.payload, IPv6ExtHdrRouting): return self.payload.answers(other.payload.payload) # Buggy if self.payload is a IPv6ExtHdrRouting + elif other.nh == 43 and isinstance(other.payload, IPv6ExtHdrSegmentRouting): @@ -34,10 +35,10 @@ diff --git a/scapy/layers/inet6.py b/scapy/layers/inet6.py elif other.nh == 60 and isinstance(other.payload, IPv6ExtHdrDestOpt): return self.payload.payload.answers(other.payload.payload) elif self.nh == 60 and isinstance(self.payload, IPv6ExtHdrDestOpt): # BU in reply to BRR, for instance -@@ -919,6 +931,148 @@ +@@ -919,6 +931,148 @@ class IPv6ExtHdrRouting(_IPv6ExtHdr): pkt = pkt[:3]+struct.pack("B", len(self.addresses))+pkt[4:] return _IPv6ExtHdr.post_build(self, pkt, pay) - + +######################### Segment Routing Header ############################ + +# This implementation is based on draft 06, available at: diff --git a/test/sys_req/set_system_parameters.sh b/test/sys_req/set_system_parameters.sh new file mode 100755 index 00000000000..533a02edbaa --- /dev/null +++ b/test/sys_req/set_system_parameters.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +file="$1" + +usage(){ + echo "Usage: $0 <requirements file>" +} + +if [ "$file" == "" ] +then + echo "Invalid parameters specified." + usage + exit 1 +fi + +if [ ! -f $file ] +then + echo "File '$file' does not exist." + usage + exit 1 +fi + +cat $file | grep -v -e '^#.*$' | grep -v -e '^ *$' | while read line +do + value_file=`echo $line | awk '{print $1}'` + operator=`echo $line | awk '{print $2}'` + value=`echo $line | awk '{print $3}'` + set_value=`echo $line | awk '{print $4}'` + if [[ "$value_file" == "" || "$operator" == "" || "$value" == "" || "$set_value" == "" ]] + then + echo "Syntax error in requirements file." + exit 1 + fi + current_value=`cat $value_file` + if test "$current_value" $operator "$value" + then + if test "$V" = "2" + then + echo "Requirement '$value_file $operator $value' satisfied." + fi + else + echo "Requirement '$value_file $operator $value' not satisfied." + echo "Writing '$set_value' to '$value_file'." + echo "$set_value" | tee "$value_file" > /dev/null + if ! test "`cat $value_file`" = "$set_value" + then + echo "Repeating the write using sudo..." + echo "$set_value" | sudo -n tee "$value_file" > /dev/null + if ! test "`cat $value_file`" = "$set_value" + then + echo "Couldn't set the required value. Is that value allowed? Is sudo working?" + exit 1 + fi + fi + echo "Succesfully wrote '$set_value' to '$value_file'." + fi +done diff --git a/test/sys_req/system_parameters b/test/sys_req/system_parameters new file mode 100644 index 00000000000..6373774dd94 --- /dev/null +++ b/test/sys_req/system_parameters @@ -0,0 +1,18 @@ +# test framework system requirements +# format of this file is +# <path> <operator> <comparison-value> <set-value> +# +# path - path to value e.g. in /proc which needs to be checked +# operator - test operator (e.g. -gt) +# comparison-value - value, against which the value read from <path> is compared +# set-value - value, to which the path is set if the test fails +# +# the comparison is done using `test' command + + +# test_reassembly.py +# needed by test_reassembly which uses udp punt via unix domain sockets +# to ensure that all data which vpp might produce in a burst fits into +# the socket send buffer +/proc/sys/net/core/wmem_max -ge 4636252 4636252 +/proc/sys/net/core/wmem_default -ge 4636252 4636252 diff --git a/test/test_reassembly.py b/test/test_reassembly.py new file mode 100644 index 00000000000..a2d77d493de --- /dev/null +++ b/test/test_reassembly.py @@ -0,0 +1,966 @@ +#!/usr/bin/env python +import unittest +from random import shuffle + +from framework import VppTestCase, VppTestRunner + +from scapy.packet import Raw +from scapy.layers.l2 import Ether, GRE +from scapy.layers.inet import IP, UDP +from util import ppp, fragment_rfc791, fragment_rfc8200 +from vpp_punt_socket import VppUDSPuntSocket +from scapy.layers.inet6 import IPv6, IPv6ExtHdrFragment, ICMPv6ParamProblem,\ + ICMPv6TimeExceeded +from vpp_gre_interface import VppGreInterface, VppGre6Interface +from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto + +test_packet_count = 257 + + +class TestIPv4Reassembly(VppTestCase): + """ IPv4 Reassembly """ + + @classmethod + def setUpClass(cls): + super(TestIPv4Reassembly, cls).setUpClass() + + cls.create_pg_interfaces([0]) + cls.pg_if = cls.pg0 + + # setup all interfaces + for i in cls.pg_interfaces: + i.admin_up() + i.config_ip4() + i.resolve_arp() + + cls.punt_port = 9999 + cls.punt_socket = VppUDSPuntSocket(cls, cls.punt_port) + + # packet sizes + cls.packet_sizes = [64, 512, 1518, 9018] + cls.padding = " abcdefghijklmn" + cls.create_stream(cls.packet_sizes) + cls.create_fragments() + + def setUp(self): + """ Test setup - force timeout on existing reassemblies """ + super(TestIPv4Reassembly, self).setUp() + self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000, + expire_walk_interval_ms=10) + self.sleep(.25) + self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000, + expire_walk_interval_ms=10000) + + def tearDown(self): + super(TestIPv4Reassembly, self).tearDown() + self.logger.debug(self.vapi.ppcli("show ip4-reassembly details")) + + @classmethod + def create_stream(cls, packet_sizes, packet_count=test_packet_count): + """Create input packet stream for defined interface. + + :param list packet_sizes: Required packet sizes. + """ + for i in range(0, packet_count): + info = cls.create_packet_info(cls.pg_if, cls.pg_if) + payload = cls.info_to_payload(info) + p = (Ether(dst=cls.pg_if.local_mac, src=cls.pg_if.remote_mac) / + IP(id=info.index, src=cls.pg_if.remote_ip4, + dst=cls.pg_if.local_ip4) / + UDP(sport=1234, dport=cls.punt_port) / + Raw(payload)) + size = packet_sizes[(i // 2) % len(packet_sizes)] + cls.extend_packet(p, size, cls.padding) + info.data = p + + @classmethod + def create_fragments(cls): + infos = cls._packet_infos + cls.pkt_infos = [] + for index, info in infos.iteritems(): + p = info.data + # self.logger.debug(ppp("Packet:", p.__class__(str(p)))) + fragments_400 = fragment_rfc791(p, 400) + fragments_300 = fragment_rfc791(p, 300) + fragments_200 = [ + x for f in fragments_400 for x in fragment_rfc791(f, 200)] + cls.pkt_infos.append( + (index, fragments_400, fragments_300, fragments_200)) + cls.fragments_400 = [ + x for (_, frags, _, _) in cls.pkt_infos for x in frags] + cls.fragments_300 = [ + x for (_, _, frags, _) in cls.pkt_infos for x in frags] + cls.fragments_200 = [ + x for (_, _, _, frags) in cls.pkt_infos for x in frags] + cls.logger.debug("Fragmented %s packets into %s 400-byte fragments, " + "%s 300-byte fragments and %s 200-byte fragments" % + (len(infos), len(cls.fragments_400), + len(cls.fragments_300), len(cls.fragments_200))) + + def verify_capture(self, capture, dropped_packet_indexes=[]): + """Verify captured packet stream. + + :param list capture: Captured packet stream. + """ + info = None + seen = set() + for packet in capture: + try: + sw_if_index = packet['sw_if_index'] + punt_action = packet['punt_action'] + packet = Ether(packet['packet']) + self.logger.debug(ppp("Got packet from %s, action %s" % + (sw_if_index, punt_action), packet)) + ip = packet[IP] + udp = packet[UDP] + payload_info = self.payload_to_info(str(packet[Raw])) + packet_index = payload_info.index + self.assertTrue( + packet_index not in dropped_packet_indexes, + ppp("Packet received, but should be dropped:", packet)) + if packet_index in seen: + raise Exception(ppp("Duplicate packet received", packet)) + seen.add(packet_index) + self.assertEqual(payload_info.dst, self.pg_if.sw_if_index) + info = self._packet_infos[packet_index] + self.assertTrue(info is not None) + self.assertEqual(packet_index, info.index) + saved_packet = info.data + self.assertEqual(ip.src, saved_packet[IP].src) + self.assertEqual(ip.dst, saved_packet[IP].dst) + self.assertEqual(udp.payload, saved_packet[UDP].payload) + except: + self.logger.error(ppp("Unexpected or invalid packet:", packet)) + raise + for index in self._packet_infos: + self.assertTrue(index in seen or index in dropped_packet_indexes, + "Packet with packet_index %d not received" % index) + + def test_reassembly(self): + """ basic reassembly """ + + self.pg_enable_capture() + self.pg_if.add_stream(self.fragments_200) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + # run it all again to verify correctness + self.pg_enable_capture() + self.pg_if.add_stream(self.fragments_200) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + def test_reversed(self): + """ reverse order reassembly """ + + fragments = list(self.fragments_200) + fragments.reverse() + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.packet_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + # run it all again to verify correctness + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.packet_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + def test_random(self): + """ random order reassembly """ + + fragments = list(self.fragments_200) + shuffle(fragments) + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.packet_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + # run it all again to verify correctness + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.packet_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + def test_duplicates(self): + """ duplicate fragments """ + + fragments = [ + x for (_, frags, _, _) in self.pkt_infos + for x in frags + for _ in range(0, min(2, len(frags))) + ] + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + def test_overlap1(self): + """ overlapping fragments case #1 """ + + fragments = [] + for _, _, frags_300, frags_200 in self.pkt_infos: + if len(frags_300) == 1: + fragments.extend(frags_300) + else: + for i, j in zip(frags_200, frags_300): + fragments.extend(i) + fragments.extend(j) + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + # run it all to verify correctness + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + def test_overlap2(self): + """ overlapping fragments case #2 """ + + fragments = [] + for _, _, frags_300, frags_200 in self.pkt_infos: + if len(frags_300) == 1: + fragments.extend(frags_300) + else: + # care must be taken here so that there are no fragments + # received by vpp after reassembly is finished, otherwise + # new reassemblies will be started and packet generator will + # freak out when it detects unfreed buffers + zipped = zip(frags_300, frags_200) + for i, j in zipped[:-1]: + fragments.extend(i) + fragments.extend(j) + fragments.append(zipped[-1][0]) + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + # run it all to verify correctness + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + def test_timeout_inline(self): + """ timeout (inline) """ + + dropped_packet_indexes = set( + index for (index, frags, _, _) in self.pkt_infos if len(frags) > 1 + ) + + self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000, + expire_walk_interval_ms=10000) + + self.pg_enable_capture() + self.pg_if.add_stream(self.fragments_400) + self.pg_start() + + packets = self.punt_socket.wait_for_packets( + len(self.pkt_infos) - len(dropped_packet_indexes)) + self.verify_capture(packets, dropped_packet_indexes) + self.pg_if.assert_nothing_captured() + + def test_timeout_cleanup(self): + """ timeout (cleanup) """ + + # whole packets + fragmented packets sans last fragment + fragments = [ + x for (_, frags_400, _, _) in self.pkt_infos + for x in frags_400[:-1 if len(frags_400) > 1 else None] + ] + + # last fragments for fragmented packets + fragments2 = [frags_400[-1] + for (_, frags_400, _, _) in self.pkt_infos + if len(frags_400) > 1] + + dropped_packet_indexes = set( + index for (index, frags_400, _, _) in self.pkt_infos + if len(frags_400) > 1) + + self.vapi.ip_reassembly_set(timeout_ms=100, max_reassemblies=1000, + expire_walk_interval_ms=50) + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + self.sleep(.25, "wait before sending rest of fragments") + + self.pg_if.add_stream(fragments2) + self.pg_start() + self.sleep(.25, "wait for vpp to process packets") + + packets = self.punt_socket.wait_for_packets( + len(self.pkt_infos) - len(dropped_packet_indexes)) + self.verify_capture(packets, dropped_packet_indexes) + self.pg_if.assert_nothing_captured() + + def test_disabled(self): + """ reassembly disabled """ + + dropped_packet_indexes = set( + index for (index, frags_400, _, _) in self.pkt_infos + if len(frags_400) > 1) + + self.vapi.ip_reassembly_set(timeout_ms=1000, max_reassemblies=0, + expire_walk_interval_ms=10000) + + self.pg_enable_capture() + self.pg_if.add_stream(self.fragments_400) + self.pg_start() + + packets = self.punt_socket.wait_for_packets( + len(self.pkt_infos) - len(dropped_packet_indexes)) + self.verify_capture(packets, dropped_packet_indexes) + self.pg_if.assert_nothing_captured() + + +class TestIPv6Reassembly(VppTestCase): + """ IPv6 Reassembly """ + + @classmethod + def setUpClass(cls): + super(TestIPv6Reassembly, cls).setUpClass() + + cls.create_pg_interfaces([0]) + cls.pg_if = cls.pg0 + + # setup all interfaces + for i in cls.pg_interfaces: + i.admin_up() + i.config_ip6() + i.resolve_ndp() + + cls.punt_port = 9999 + cls.punt_socket = VppUDSPuntSocket(cls, cls.punt_port, is_ip4=0) + + # packet sizes + cls.packet_sizes = [64, 512, 1518, 9018] + cls.padding = " abcdefghijklmn" + cls.create_stream(cls.packet_sizes) + cls.create_fragments() + + def setUp(self): + """ Test setup - force timeout on existing reassemblies """ + super(TestIPv6Reassembly, self).setUp() + self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000, + expire_walk_interval_ms=10, is_ip6=1) + self.sleep(.25) + self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000, + expire_walk_interval_ms=10000, is_ip6=1) + + def tearDown(self): + super(TestIPv6Reassembly, self).tearDown() + self.logger.debug(self.vapi.ppcli("show ip6-reassembly details")) + + @classmethod + def create_stream(cls, packet_sizes, packet_count=test_packet_count): + """Create input packet stream for defined interface. + + :param list packet_sizes: Required packet sizes. + """ + for i in range(0, packet_count): + info = cls.create_packet_info(cls.pg_if, cls.pg_if) + payload = cls.info_to_payload(info) + p = (Ether(dst=cls.pg_if.local_mac, src=cls.pg_if.remote_mac) / + IPv6(src=cls.pg_if.remote_ip6, + dst=cls.pg_if.local_ip6) / + UDP(sport=1234, dport=cls.punt_port) / + Raw(payload)) + size = packet_sizes[(i // 2) % len(packet_sizes)] + cls.extend_packet(p, size, cls.padding) + info.data = p + + @classmethod + def create_fragments(cls): + infos = cls._packet_infos + cls.pkt_infos = [] + for index, info in infos.iteritems(): + p = info.data + # self.logger.debug(ppp("Packet:", p.__class__(str(p)))) + fragments_400 = fragment_rfc8200(p, info.index, 400) + fragments_300 = fragment_rfc8200(p, info.index, 300) + cls.pkt_infos.append((index, fragments_400, fragments_300)) + cls.fragments_400 = [ + x for _, frags, _ in cls.pkt_infos for x in frags] + cls.fragments_300 = [ + x for _, _, frags in cls.pkt_infos for x in frags] + cls.logger.debug("Fragmented %s packets into %s 400-byte fragments, " + "and %s 300-byte fragments" % + (len(infos), len(cls.fragments_400), + len(cls.fragments_300))) + + def verify_capture(self, capture, dropped_packet_indexes=[]): + """Verify captured packet strea . + + :param list capture: Captured packet stream. + """ + info = None + seen = set() + for packet in capture: + try: + sw_if_index = packet['sw_if_index'] + punt_action = packet['punt_action'] + packet = Ether(packet['packet']) + self.logger.debug(ppp("Got packet from %s, action %s" % + (sw_if_index, punt_action), packet)) + ip = packet[IPv6] + udp = packet[UDP] + payload_info = self.payload_to_info(str(packet[Raw])) + packet_index = payload_info.index + self.assertTrue( + packet_index not in dropped_packet_indexes, + ppp("Packet received, but should be dropped:", packet)) + if packet_index in seen: + raise Exception(ppp("Duplicate packet received", packet)) + seen.add(packet_index) + self.assertEqual(payload_info.dst, self.pg_if.sw_if_index) + info = self._packet_infos[packet_index] + self.assertTrue(info is not None) + self.assertEqual(packet_index, info.index) + saved_packet = info.data + self.assertEqual(ip.src, saved_packet[IPv6].src) + self.assertEqual(ip.dst, saved_packet[IPv6].dst) + self.assertEqual(udp.payload, saved_packet[UDP].payload) + except: + self.logger.error(ppp("Unexpected or invalid packet:", packet)) + raise + for index in self._packet_infos: + self.assertTrue(index in seen or index in dropped_packet_indexes, + "Packet with packet_index %d not received" % index) + + def test_reassembly(self): + """ basic reassembly """ + + self.pg_enable_capture() + self.pg_if.add_stream(self.fragments_400) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + # run it all again to verify correctness + self.pg_enable_capture() + self.pg_if.add_stream(self.fragments_400) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + def test_reversed(self): + """ reverse order reassembly """ + + fragments = list(self.fragments_400) + fragments.reverse() + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + # run it all again to verify correctness + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + def test_random(self): + """ random order reassembly """ + + fragments = list(self.fragments_400) + shuffle(fragments) + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + # run it all again to verify correctness + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + def test_duplicates(self): + """ duplicate fragments """ + + fragments = [ + x for (_, frags, _) in self.pkt_infos + for x in frags + for _ in range(0, min(2, len(frags))) + ] + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + packets = self.punt_socket.wait_for_packets(len(self.pkt_infos)) + self.verify_capture(packets) + self.pg_if.assert_nothing_captured() + + def test_overlap1(self): + """ overlapping fragments case #1 """ + + fragments = [] + for _, frags_400, frags_300 in self.pkt_infos: + if len(frags_300) == 1: + fragments.extend(frags_400) + else: + for i, j in zip(frags_300, frags_400): + fragments.extend(i) + fragments.extend(j) + + dropped_packet_indexes = set( + index for (index, _, frags) in self.pkt_infos if len(frags) > 1 + ) + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + self.sleep(.1, "wait for vpp to process packets") + packets = self.punt_socket.wait_for_packets( + len(self.pkt_infos) - len(dropped_packet_indexes)) + self.verify_capture(packets, dropped_packet_indexes) + self.pg_if.assert_nothing_captured() + + def test_overlap2(self): + """ overlapping fragments case #2 """ + + fragments = [] + for _, frags_400, frags_30 in self.pkt_infos: + if len(frags_400) == 1: + fragments.extend(frags_400) + else: + # care must be taken here so that there are no fragments + # received by vpp after reassembly is finished, otherwise + # new reassemblies will be started and packet generator will + # freak out when it detects unfreed buffers + zipped = zip(frags_400, frags_30) + for i, j in zipped[:-1]: + fragments.extend(i) + fragments.extend(j) + fragments.append(zipped[-1][0]) + + dropped_packet_indexes = set( + index for (index, _, frags) in self.pkt_infos if len(frags) > 1 + ) + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + self.sleep(.1, "wait for vpp to process packets") + packets = self.punt_socket.wait_for_packets( + len(self.pkt_infos) - len(dropped_packet_indexes)) + self.verify_capture(packets, dropped_packet_indexes) + self.pg_if.assert_nothing_captured() + + def test_timeout_inline(self): + """ timeout (inline) """ + + dropped_packet_indexes = set( + index for (index, frags, _) in self.pkt_infos if len(frags) > 1 + ) + + self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000, + expire_walk_interval_ms=10000, is_ip6=1) + + self.pg_enable_capture() + self.pg_if.add_stream(self.fragments_400) + self.pg_start() + + packets = self.punt_socket.wait_for_packets( + len(self.pkt_infos) - len(dropped_packet_indexes)) + self.verify_capture(packets, dropped_packet_indexes) + pkts = self.pg_if.get_capture( + expected_count=len(dropped_packet_indexes)) + for icmp in pkts: + self.assertIn(ICMPv6TimeExceeded, icmp) + self.assertIn(IPv6ExtHdrFragment, icmp) + self.assertIn(icmp[IPv6ExtHdrFragment].id, dropped_packet_indexes) + dropped_packet_indexes.remove(icmp[IPv6ExtHdrFragment].id) + + def test_timeout_cleanup(self): + """ timeout (cleanup) """ + + # whole packets + fragmented packets sans last fragment + fragments = [ + x for (_, frags_400, _) in self.pkt_infos + for x in frags_400[:-1 if len(frags_400) > 1 else None] + ] + + # last fragments for fragmented packets + fragments2 = [frags_400[-1] + for (_, frags_400, _) in self.pkt_infos + if len(frags_400) > 1] + + dropped_packet_indexes = set( + index for (index, frags_400, _) in self.pkt_infos + if len(frags_400) > 1) + + self.vapi.ip_reassembly_set(timeout_ms=100, max_reassemblies=1000, + expire_walk_interval_ms=50) + + self.vapi.ip_reassembly_set(timeout_ms=100, max_reassemblies=1000, + expire_walk_interval_ms=50, is_ip6=1) + + self.pg_enable_capture() + self.pg_if.add_stream(fragments) + self.pg_start() + + self.sleep(.25, "wait before sending rest of fragments") + + self.pg_if.add_stream(fragments2) + self.pg_start() + self.sleep(.25, "wait for vpp to process packets") + + packets = self.punt_socket.wait_for_packets( + len(self.pkt_infos) - len(dropped_packet_indexes)) + self.verify_capture(packets, dropped_packet_indexes) + pkts = self.pg_if.get_capture( + expected_count=len(dropped_packet_indexes)) + for icmp in pkts: + self.assertIn(ICMPv6TimeExceeded, icmp) + self.assertIn(IPv6ExtHdrFragment, icmp) + self.assertIn(icmp[IPv6ExtHdrFragment].id, dropped_packet_indexes) + dropped_packet_indexes.remove(icmp[IPv6ExtHdrFragment].id) + + def test_disabled(self): + """ reassembly disabled """ + + dropped_packet_indexes = set( + index for (index, frags_400, _) in self.pkt_infos + if len(frags_400) > 1) + + self.vapi.ip_reassembly_set(timeout_ms=1000, max_reassemblies=0, + expire_walk_interval_ms=10000, is_ip6=1) + + self.pg_enable_capture() + self.pg_if.add_stream(self.fragments_400) + self.pg_start() + + packets = self.punt_socket.wait_for_packets( + len(self.pkt_infos) - len(dropped_packet_indexes)) + self.verify_capture(packets, dropped_packet_indexes) + self.pg_if.assert_nothing_captured() + + def test_missing_upper(self): + """ missing upper layer """ + p = (Ether(dst=self.pg_if.local_mac, src=self.pg_if.remote_mac) / + IPv6(src=self.pg_if.remote_ip6, + dst=self.pg_if.local_ip6) / + UDP(sport=1234, dport=self.punt_port) / + Raw()) + self.extend_packet(p, 1000, self.padding) + fragments = fragment_rfc8200(p, 1, 500) + bad_fragment = p.__class__(str(fragments[1])) + bad_fragment[IPv6ExtHdrFragment].nh = 59 + bad_fragment[IPv6ExtHdrFragment].offset = 0 + self.pg_enable_capture() + self.pg_if.add_stream([bad_fragment]) + self.pg_start() + pkts = self.pg_if.get_capture(expected_count=1) + icmp = pkts[0] + self.assertIn(ICMPv6ParamProblem, icmp) + self.assert_equal(icmp[ICMPv6ParamProblem].code, 3, "ICMP code") + + def test_invalid_frag_size(self): + """ fragment size not a multiple of 8 """ + p = (Ether(dst=self.pg_if.local_mac, src=self.pg_if.remote_mac) / + IPv6(src=self.pg_if.remote_ip6, + dst=self.pg_if.local_ip6) / + UDP(sport=1234, dport=self.punt_port) / + Raw()) + self.extend_packet(p, 1000, self.padding) + fragments = fragment_rfc8200(p, 1, 500) + bad_fragment = fragments[0] + self.extend_packet(bad_fragment, len(bad_fragment) + 5) + self.pg_enable_capture() + self.pg_if.add_stream([bad_fragment]) + self.pg_start() + pkts = self.pg_if.get_capture(expected_count=1) + icmp = pkts[0] + self.assertIn(ICMPv6ParamProblem, icmp) + self.assert_equal(icmp[ICMPv6ParamProblem].code, 0, "ICMP code") + + def test_invalid_packet_size(self): + """ total packet size > 65535 """ + p = (Ether(dst=self.pg_if.local_mac, src=self.pg_if.remote_mac) / + IPv6(src=self.pg_if.remote_ip6, + dst=self.pg_if.local_ip6) / + UDP(sport=1234, dport=self.punt_port) / + Raw()) + self.extend_packet(p, 1000, self.padding) + fragments = fragment_rfc8200(p, 1, 500) + bad_fragment = fragments[1] + bad_fragment[IPv6ExtHdrFragment].offset = 65500 + self.pg_enable_capture() + self.pg_if.add_stream([bad_fragment]) + self.pg_start() + pkts = self.pg_if.get_capture(expected_count=1) + icmp = pkts[0] + self.assertIn(ICMPv6ParamProblem, icmp) + self.assert_equal(icmp[ICMPv6ParamProblem].code, 0, "ICMP code") + + +class TestFIFReassembly(VppTestCase): + """ Fragments in fragments reassembly """ + + @classmethod + def setUpClass(cls): + super(TestFIFReassembly, cls).setUpClass() + + cls.create_pg_interfaces([0]) + cls.pg_if = cls.pg0 + cls.pg_if.admin_up() + cls.pg_if.config_ip4() + cls.pg_if.resolve_arp() + cls.pg_if.config_ip6() + cls.pg_if.resolve_ndp() + + cls.punt_port = 9999 + cls.punt4_socket = VppUDSPuntSocket(cls, cls.punt_port) + cls.punt6_socket = VppUDSPuntSocket(cls, cls.punt_port, is_ip4=0) + cls.packet_sizes = [64, 512, 1518, 9018] + cls.padding = " abcdefghijklmn" + + def setUp(self): + """ Test setup - force timeout on existing reassemblies """ + super(TestFIFReassembly, self).setUp() + self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000, + expire_walk_interval_ms=10) + self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000, + expire_walk_interval_ms=10, is_ip6=1) + self.sleep(.25) + self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000, + expire_walk_interval_ms=10000) + self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000, + expire_walk_interval_ms=10000, is_ip6=1) + + def tearDown(self): + self.logger.debug(self.vapi.ppcli("show ip4-reassembly details")) + self.logger.debug(self.vapi.ppcli("show ip6-reassembly details")) + super(TestFIFReassembly, self).tearDown() + + def verify_capture(self, capture, ip_class, dropped_packet_indexes=[]): + """Verify captured packet stream. + + :param list capture: Captured packet stream. + """ + info = None + seen = set() + for packet in capture: + try: + sw_if_index = packet['sw_if_index'] + punt_action = packet['punt_action'] + packet = Ether(packet['packet']) + self.logger.debug(ppp("Got packet from %s, action %s" % + (sw_if_index, punt_action), packet)) + ip = packet[ip_class] + udp = packet[UDP] + payload_info = self.payload_to_info(str(packet[Raw])) + packet_index = payload_info.index + self.assertTrue( + packet_index not in dropped_packet_indexes, + ppp("Packet received, but should be dropped:", packet)) + if packet_index in seen: + raise Exception(ppp("Duplicate packet received", packet)) + seen.add(packet_index) + self.assertEqual(payload_info.dst, self.pg_if.sw_if_index) + info = self._packet_infos[packet_index] + self.assertTrue(info is not None) + self.assertEqual(packet_index, info.index) + saved_packet = info.data + self.assertEqual(ip.src, saved_packet[ip_class].src) + self.assertEqual(ip.dst, saved_packet[ip_class].dst) + self.assertEqual(udp.payload, saved_packet[UDP].payload) + except: + self.logger.error(ppp("Unexpected or invalid packet:", packet)) + raise + for index in self._packet_infos: + self.assertTrue(index in seen or index in dropped_packet_indexes, + "Packet with packet_index %d not received" % index) + + def test_fif4(self): + """ Fragments in fragments (4o4) """ + + # TODO this should be ideally in setUpClass, but then we hit a bug + # with VppIpRoute incorrectly reporting it's present when it's not + # so we need to manually remove the vpp config, thus we cannot have + # it shared for multiple test cases + self.tun_ip4 = "1.1.1.2" + + self.gre4 = VppGreInterface(self, self.pg0.local_ip4, self.tun_ip4) + self.gre4.add_vpp_config() + self.gre4.admin_up() + self.gre4.config_ip4() + + self.route4 = VppIpRoute(self, self.tun_ip4, 32, + [VppRoutePath(self.pg0.remote_ip4, + self.pg0.sw_if_index)]) + self.route4.add_vpp_config() + + self.reset_packet_infos() + for i in range(test_packet_count): + info = self.create_packet_info(self.pg0, self.pg0) + payload = self.info_to_payload(info) + p = (IP(id=i, src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / + UDP(sport=1234, dport=self.punt_port) / + Raw(payload)) + size = self.packet_sizes[(i // 2) % len(self.packet_sizes)] + self.extend_packet(p, size, self.padding) + info.data = p + + fragments = [x for _, p in self._packet_infos.iteritems() + for x in fragment_rfc791(p.data, 400)] + + encapped_fragments = \ + [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / + IP(src=self.tun_ip4, dst=self.pg0.local_ip4) / + GRE() / + p + for p in fragments] + + fragmented_encapped_fragments = \ + [x for p in encapped_fragments + for x in fragment_rfc791(p, 200)] + + self.pg0.add_stream(fragmented_encapped_fragments) + + self.pg_enable_capture(self.pg_interfaces) + self.pg_start() + + self.pg0.assert_nothing_captured() + packets = self.punt4_socket.wait_for_packets(len(self._packet_infos)) + self.verify_capture(packets, IP) + + # TODO remove gre vpp config by hand until VppIpRoute gets fixed + # so that it's query_vpp_config() works as it should + self.gre4.remove_vpp_config() + + def test_fif6(self): + """ Fragments in fragments (6o6) """ + # TODO this should be ideally in setUpClass, but then we hit a bug + # with VppIpRoute incorrectly reporting it's present when it's not + # so we need to manually remove the vpp config, thus we cannot have + # it shared for multiple test cases + self.tun_ip6 = "1002::1" + + self.gre6 = VppGre6Interface(self, self.pg0.local_ip6, self.tun_ip6) + self.gre6.add_vpp_config() + self.gre6.admin_up() + self.gre6.config_ip6() + + self.route6 = VppIpRoute(self, self.tun_ip6, 128, + [VppRoutePath(self.pg0.remote_ip6, + self.pg0.sw_if_index, + proto=DpoProto.DPO_PROTO_IP6)], + is_ip6=1) + self.route6.add_vpp_config() + + self.reset_packet_infos() + for i in range(test_packet_count): + info = self.create_packet_info(self.pg0, self.pg0) + payload = self.info_to_payload(info) + p = (IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) / + UDP(sport=1234, dport=self.punt_port) / + Raw(payload)) + size = self.packet_sizes[(i // 2) % len(self.packet_sizes)] + self.extend_packet(p, size, self.padding) + info.data = p + + fragments = [x for _, i in self._packet_infos.iteritems() + for x in fragment_rfc8200( + i.data, i.index, 400)] + + encapped_fragments = \ + [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / + IPv6(src=self.tun_ip6, dst=self.pg0.local_ip6) / + GRE() / + p + for p in fragments] + + fragmented_encapped_fragments = \ + [x for p in encapped_fragments for x in ( + fragment_rfc8200( + p, + 2 * len(self._packet_infos) + p[IPv6ExtHdrFragment].id, + 200) + if IPv6ExtHdrFragment in p else [p] + ) + ] + + self.pg0.add_stream(fragmented_encapped_fragments) + + self.pg_enable_capture(self.pg_interfaces) + self.pg_start() + + self.pg0.assert_nothing_captured() + packets = self.punt6_socket.wait_for_packets(len(self._packet_infos)) + self.verify_capture(packets, IPv6) + + # TODO remove gre vpp config by hand until VppIpRoute gets fixed + # so that it's query_vpp_config() works as it should + self.gre6.remove_vpp_config() + + +if __name__ == '__main__': + unittest.main(testRunner=VppTestRunner) diff --git a/test/util.py b/test/util.py index 3e0267a3f4d..512bf9e3b7e 100644 --- a/test/util.py +++ b/test/util.py @@ -8,10 +8,10 @@ from scapy.layers.inet6 import in6_mactoifaceid from scapy.layers.l2 import Ether from scapy.packet import Raw -from scapy.layers.inet import IP, UDP, TCP -from scapy.layers.inet6 import IPv6, ICMPv6Unknown, ICMPv6EchoRequest -from scapy.packet import Packet -from socket import inet_pton, AF_INET, AF_INET6 +from scapy.layers.inet import IP +from scapy.layers.inet6 import IPv6, IPv6ExtHdrFragment, IPv6ExtHdrRouting,\ + IPv6ExtHdrHopByHop +from socket import AF_INET6 def ppp(headline, packet): @@ -37,8 +37,7 @@ def ppc(headline, capture, limit=10): tail = "" if limit < len(capture): tail = "\nPrint limit reached, %s out of %s packets printed" % ( - len(capture), limit) - limit = len(capture) + limit, len(capture)) body = "".join([ppp("Packet #%s:" % count, p) for count, p in zip(range(0, limit), capture)]) return "%s\n%s%s" % (headline, body, tail) @@ -174,6 +173,7 @@ class ForeignAddressFactory(object): class L4_Conn(): """ L4 'connection' tied to two VPP interfaces """ + def __init__(self, testcase, if1, if2, af, l4proto, port1, port2): self.testcase = testcase self.ifs = [None, None] @@ -189,7 +189,7 @@ class L4_Conn(): def pkt(self, side, l4args={}, payload="x"): is_ip6 = 1 if self.address_family == AF_INET6 else 0 s0 = side - s1 = 1-side + s1 = 1 - side src_if = self.ifs[s0] dst_if = self.ifs[s1] layer_3 = [IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4), @@ -208,7 +208,7 @@ class L4_Conn(): l4args['flags'] = flags self.ifs[side].add_stream(self.pkt(side, l4args=l4args, payload=payload)) - self.ifs[1-side].enable_capture() + self.ifs[1 - side].enable_capture() self.testcase.pg_start() def recv(self, side): @@ -217,15 +217,190 @@ class L4_Conn(): def send_through(self, side, flags=None, payload=""): self.send(side, flags, payload) - p = self.recv(1-side) + p = self.recv(1 - side) return p def send_pingpong(self, side, flags1=None, flags2=None): p1 = self.send_through(side, flags1) - p2 = self.send_through(1-side, flags2) + p2 = self.send_through(1 - side, flags2) return [p1, p2] class L4_CONN_SIDE: L4_CONN_SIDE_ZERO = 0 L4_CONN_SIDE_ONE = 1 + + +class LoggerWrapper(object): + def __init__(self, logger=None): + self._logger = logger + + def debug(self, *args, **kwargs): + if self._logger: + self._logger.debug(*args, **kwargs) + + def error(self, *args, **kwargs): + if self._logger: + self._logger.error(*args, **kwargs) + + +def fragment_rfc791(packet, fragsize, _logger=None): + """ + Fragment an IPv4 packet per RFC 791 + :param packet: packet to fragment + :param fragsize: size at which to fragment + :note: IP options are not supported + :returns: list of fragments + """ + logger = LoggerWrapper(_logger) + logger.debug(ppp("Fragmenting packet:", packet)) + packet = packet.__class__(str(packet)) # recalculate all values + if len(packet[IP].options) > 0: + raise Exception("Not implemented") + if len(packet) <= fragsize: + return [packet] + + pre_ip_len = len(packet) - len(packet[IP]) + ip_header_len = packet[IP].ihl * 4 + hex_packet = str(packet) + hex_headers = hex_packet[:(pre_ip_len + ip_header_len)] + hex_payload = hex_packet[(pre_ip_len + ip_header_len):] + + pkts = [] + ihl = packet[IP].ihl + otl = len(packet[IP]) + nfb = (fragsize - pre_ip_len - ihl * 4) / 8 + fo = packet[IP].frag + + p = packet.__class__(hex_headers + hex_payload[:nfb * 8]) + p[IP].flags = "MF" + p[IP].frag = fo + p[IP].len = ihl * 4 + nfb * 8 + del p[IP].chksum + pkts.append(p) + + p = packet.__class__(hex_headers + hex_payload[nfb * 8:]) + p[IP].len = otl - nfb * 8 + p[IP].frag = fo + nfb + del p[IP].chksum + + more_fragments = fragment_rfc791(p, fragsize, _logger) + pkts.extend(more_fragments) + + return pkts + + +def fragment_rfc8200(packet, identification, fragsize, _logger=None): + """ + Fragment an IPv6 packet per RFC 8200 + :param packet: packet to fragment + :param fragsize: size at which to fragment + :note: IP options are not supported + :returns: list of fragments + """ + logger = LoggerWrapper(_logger) + packet = packet.__class__(str(packet)) # recalculate all values + if len(packet) <= fragsize: + return [packet] + logger.debug(ppp("Fragmenting packet:", packet)) + pkts = [] + counter = 0 + routing_hdr = None + hop_by_hop_hdr = None + upper_layer = None + seen_ipv6 = False + ipv6_nr = -1 + l = packet.getlayer(counter) + while l is not None: + if l.__class__ is IPv6: + if seen_ipv6: + # ignore 2nd IPv6 header and everything below.. + break + ipv6_nr = counter + seen_ipv6 = True + elif l.__class__ is IPv6ExtHdrFragment: + raise Exception("Already fragmented") + elif l.__class__ is IPv6ExtHdrRouting: + routing_hdr = counter + elif l.__class__ is IPv6ExtHdrHopByHop: + hop_by_hop_hdr = counter + elif seen_ipv6 and not upper_layer and \ + not l.__class__.__name__.startswith('IPv6ExtHdr'): + upper_layer = counter + counter = counter + 1 + l = packet.getlayer(counter) + + logger.debug( + "Layers seen: IPv6(#%s), Routing(#%s), HopByHop(#%s), upper(#%s)" % + (ipv6_nr, routing_hdr, hop_by_hop_hdr, upper_layer)) + + if upper_layer is None: + raise Exception("Upper layer header not found in IPv6 packet") + + last_per_fragment_hdr = ipv6_nr + if routing_hdr is None: + if hop_by_hop_hdr is not None: + last_per_fragment_hdr = hop_by_hop_hdr + else: + last_per_fragment_hdr = routing_hdr + logger.debug("Last per-fragment hdr is #%s" % (last_per_fragment_hdr)) + + per_fragment_headers = packet.copy() + per_fragment_headers[last_per_fragment_hdr].remove_payload() + logger.debug(ppp("Per-fragment headers:", per_fragment_headers)) + + ext_and_upper_layer = packet.getlayer(last_per_fragment_hdr)[1] + hex_payload = str(ext_and_upper_layer) + logger.debug("Payload length is %s" % len(hex_payload)) + logger.debug(ppp("Ext and upper layer:", ext_and_upper_layer)) + + fragment_ext_hdr = IPv6ExtHdrFragment() + logger.debug(ppp("Fragment header:", fragment_ext_hdr)) + + if len(per_fragment_headers) + len(fragment_ext_hdr) +\ + len(ext_and_upper_layer) - len(ext_and_upper_layer.payload)\ + > fragsize: + raise Exception("Cannot fragment this packet - MTU too small " + "(%s, %s, %s, %s, %s)" % ( + len(per_fragment_headers), len(fragment_ext_hdr), + len(ext_and_upper_layer), + len(ext_and_upper_layer.payload), fragsize)) + + orig_nh = packet[IPv6].nh + p = per_fragment_headers + del p[IPv6].plen + del p[IPv6].nh + p = p / fragment_ext_hdr + del p[IPv6ExtHdrFragment].nh + first_payload_len_nfb = (fragsize - len(p)) / 8 + p = p / Raw(hex_payload[:first_payload_len_nfb * 8]) + del p[IPv6].plen + p[IPv6ExtHdrFragment].nh = orig_nh + p[IPv6ExtHdrFragment].id = identification + p[IPv6ExtHdrFragment].offset = 0 + p[IPv6ExtHdrFragment].m = 1 + p = p.__class__(str(p)) + logger.debug(ppp("Fragment %s:" % len(pkts), p)) + pkts.append(p) + offset = first_payload_len_nfb * 8 + logger.debug("Offset after first fragment: %s" % offset) + while len(hex_payload) > offset: + p = per_fragment_headers + del p[IPv6].plen + del p[IPv6].nh + p = p / fragment_ext_hdr + del p[IPv6ExtHdrFragment].nh + l_nfb = (fragsize - len(p)) / 8 + p = p / Raw(hex_payload[offset:offset + l_nfb * 8]) + p[IPv6ExtHdrFragment].nh = orig_nh + p[IPv6ExtHdrFragment].id = identification + p[IPv6ExtHdrFragment].offset = offset / 8 + p[IPv6ExtHdrFragment].m = 1 + p = p.__class__(str(p)) + logger.debug(ppp("Fragment %s:" % len(pkts), p)) + pkts.append(p) + offset = offset + l_nfb * 8 + + pkts[-1][IPv6ExtHdrFragment].m = 0 # reset more-flags in last fragment + + return pkts diff --git a/test/vpp_gre_interface.py b/test/vpp_gre_interface.py index acfd348d64f..998f6eab32b 100644 --- a/test/vpp_gre_interface.py +++ b/test/vpp_gre_interface.py @@ -26,14 +26,21 @@ class VppGreInterface(VppInterface): is_teb=self.t_is_teb) self._sw_if_index = r.sw_if_index self.generate_remote_hosts() + self._test.registry.register(self, self._test.logger) def remove_vpp_config(self): s = socket.inet_pton(socket.AF_INET, self.t_src) d = socket.inet_pton(socket.AF_INET, self.t_dst) self.unconfig() - r = self.test.vapi.gre_tunnel_add_del(s, d, - outer_fib_id=self.t_outer_fib, - is_add=0) + self.test.vapi.gre_tunnel_add_del(s, d, + outer_fib_id=self.t_outer_fib, + is_add=0) + + def __str__(self): + return self.object_id() + + def object_id(self): + return "gre-%d" % self._sw_if_index class VppGre6Interface(VppInterface): @@ -60,12 +67,19 @@ class VppGre6Interface(VppInterface): is_ip6=1) self._sw_if_index = r.sw_if_index self.generate_remote_hosts() + self._test.registry.register(self, self._test.logger) def remove_vpp_config(self): s = socket.inet_pton(socket.AF_INET6, self.t_src) d = socket.inet_pton(socket.AF_INET6, self.t_dst) self.unconfig() - r = self.test.vapi.gre_tunnel_add_del(s, d, - outer_fib_id=self.t_outer_fib, - is_add=0, - is_ip6=1) + self.test.vapi.gre_tunnel_add_del(s, d, + outer_fib_id=self.t_outer_fib, + is_add=0, + is_ip6=1) + + def __str__(self): + return self.object_id() + + def object_id(self): + return "gre-%d" % self._sw_if_index diff --git a/test/vpp_interface.py b/test/vpp_interface.py index b8505ce39d7..8891dde6afe 100644 --- a/test/vpp_interface.py +++ b/test/vpp_interface.py @@ -372,3 +372,15 @@ class VppInterface(object): self.test.vapi.proxy_arp_intfc_enable_disable( self.sw_if_index, enable) + + def query_vpp_config(self): + dump = self.test.vapi.sw_interface_dump() + return self.is_interface_config_in_dump(dump) + + def is_interface_config_in_dump(self, dump): + for i in dump: + if i.interface_name.rstrip(' \t\r\n\0') == self.name and \ + i.sw_if_index == self.sw_if_index: + return True + else: + return False diff --git a/test/vpp_lo_interface.py b/test/vpp_lo_interface.py index 963123f6404..ae4111bec82 100644 --- a/test/vpp_lo_interface.py +++ b/test/vpp_lo_interface.py @@ -19,17 +19,5 @@ class VppLoInterface(VppInterface, VppObject): def remove_vpp_config(self): self.test.vapi.delete_loopback(self.sw_if_index) - def query_vpp_config(self): - dump = self.test.vapi.sw_interface_dump() - return self.is_interface_config_in_dump(dump) - - def is_interface_config_in_dump(self, dump): - for i in dump: - if i.interface_name.rstrip(' \t\r\n\0') == self.name and \ - i.sw_if_index == self.sw_if_index: - return True - else: - return False - def object_id(self): return "loopback-%d" % self._sw_if_index diff --git a/test/vpp_papi_provider.py b/test/vpp_papi_provider.py index 0269736cd78..087a14b7fad 100644 --- a/test/vpp_papi_provider.py +++ b/test/vpp_papi_provider.py @@ -3102,3 +3102,26 @@ class VppPapiProvider(object): 'ip6_fib_id': ip6_fib_id, 'namespace_id': namespace_id, 'namespace_id_len': len(namespace_id)}) + + def punt_socket_register(self, l4_port, pathname, header_version=1, + is_ip4=1, l4_protocol=0x11): + """ Punt to socket """ + return self.api(self.papi.punt_socket_register, + {'is_ip4': is_ip4, + 'l4_protocol': l4_protocol, + 'l4_port': l4_port, + 'pathname': pathname, + 'header_version': header_version}) + + def ip_reassembly_set(self, timeout_ms, max_reassemblies, + expire_walk_interval_ms, is_ip6=0): + """ Set IP reassembly parameters """ + return self.api(self.papi.ip_reassembly_set, + {'is_ip6': is_ip6, + 'timeout_ms': timeout_ms, + 'expire_walk_interval_ms': expire_walk_interval_ms, + 'max_reassemblies': max_reassemblies}) + + def ip_reassembly_get(self, is_ip6=0): + """ Get IP reassembly parameters """ + return self.api(self.papi.ip_reassembly_get, {'is_ip6': is_ip6}) diff --git a/test/vpp_punt_socket.py b/test/vpp_punt_socket.py new file mode 100644 index 00000000000..5004b23396d --- /dev/null +++ b/test/vpp_punt_socket.py @@ -0,0 +1,75 @@ +from socket import socket, AF_UNIX, SOCK_DGRAM +from select import select +from time import time +from struct import unpack, calcsize +from util import ppc +from scapy.layers.l2 import Ether + +client_uds_socket_name = "client-uds-socket" +vpp_uds_socket_name = "vpp-uds-socket" + +VPP_PUNT_HEADER_FMT = '=Ii' +VPP_PUNT_HEADER_SIZE = calcsize(VPP_PUNT_HEADER_FMT) + + +class VppPuntAction: + PUNT_L2 = 0 + PUNT_IP4_ROUTED = 1 + PUNT_IP6_ROUTED = 2 + + +class VppUDSPuntSocket(object): + def __init__(self, testcase, port, is_ip4=1, l4_protocol=0x11): + client_path = '%s/%s-%s-%s' % (testcase.tempdir, + client_uds_socket_name, + "4" if is_ip4 else "6", port) + testcase.vapi.punt_socket_register( + port, client_path, is_ip4=is_ip4, l4_protocol=l4_protocol) + self.testcase = testcase + self.uds = socket(AF_UNIX, SOCK_DGRAM) + self.uds.bind(client_path) + self.uds.connect(testcase.punt_socket_path) + + def wait_for_packets(self, count, timeout=1): + packets = [] + now = time() + deadline = now + timeout + while len(packets) < count and now < deadline: + r, w, e = select([self.uds], [], [self.uds], deadline - now) + if self.uds in r: + x = self.uds.recv(1024 * 1024) + sw_if_index, punt_action = unpack( + VPP_PUNT_HEADER_FMT, x[:VPP_PUNT_HEADER_SIZE]) + packets.append({'sw_if_index': sw_if_index, + 'punt_action': punt_action, + 'packet': x[VPP_PUNT_HEADER_SIZE:]}) + + if self.uds in e: + raise Exception("select() indicates error on UDS socket") + now = time() + + if len(packets) != count: + raise Exception("Unexpected packet count received, got %s packets," + " expected %s packets" % (len(packets), count)) + self.testcase.logger.debug( + "Got %s packets via punt socket" % len(packets)) + return packets + + def assert_nothing_captured(self, timeout=.25): + packets = [] + now = time() + deadline = now + timeout + while now < deadline: + r, w, e = select([self.uds], [], [self.uds], deadline - now) + if self.uds in r: + x = self.uds.recv(1024 * 1024) + packets.append(Ether(x[VPP_PUNT_HEADER_SIZE:])) + if self.uds in e: + raise Exception("select() indicates error on UDS socket") + now = time() + + if len(packets) > 0: + self.testcase.logger.error( + ppc("Unexpected packets captured:", packets)) + raise Exception("Unexpected packet count received, got %s packets," + " expected no packets" % len(packets)) |