#!/usr/bin/env python from __future__ import print_function import gc import sys import os import select import unittest import tempfile import time import faulthandler import random import copy import psutil import platform from collections import deque from threading import Thread, Event from inspect import getdoc, isclass from traceback import format_exception from logging import FileHandler, DEBUG, Formatter from scapy.packet import Raw from hook import StepHook, PollHook, VppDiedError from vpp_pg_interface import VppPGInterface from vpp_sub_interface import VppSubInterface from vpp_lo_interface import VppLoInterface from vpp_papi_provider import VppPapiProvider from vpp_papi.vpp_stats import VPPStats from log import RED, GREEN, YELLOW, double_line_delim, single_line_delim, \ get_logger, colorize from vpp_object import VppObjectRegistry from util import ppp, is_core_present from scapy.layers.inet import IPerror, TCPerror, UDPerror, ICMPerror from scapy.layers.inet6 import ICMPv6DestUnreach, ICMPv6EchoRequest from scapy.layers.inet6 import ICMPv6EchoReply 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 import subprocess32 as subprocess else: import subprocess # Python2/3 compatible try: input = raw_input except NameError: pass PASS = 0 FAIL = 1 ERROR = 2 SKIP = 3 TEST_RUN = 4 debug_framework = False if os.getenv('TEST_DEBUG', "0") == "1": debug_framework = True import debug_internal """ Test framework module. The module provides a set of tools for constructing and running tests and representing the results. """ class _PacketInfo(object): """Private class to create packet info object. Help process information about the next packet. Set variables to default values. """ #: Store the index of the packet. index = -1 #: Store the index of the source packet generator interface of the packet. src = -1 #: Store the index of the destination packet generator interface #: of the packet. dst = -1 #: Store expected ip version ip = -1 #: Store expected upper protocol proto = -1 #: Store the copy of the former packet. data = None def __eq__(self, other): index = self.index == other.index src = self.src == other.src dst = self.dst == other.dst data = self.data == other.data return index and src and dst and data def pump_output(testclass): """ pump output from vpp stdout/stderr to proper queues """ stdout_fragment = "" stderr_fragment = "" while not testclass.pump_thread_stop_flag.is_set(): readable = select.select([testclass.vpp.stdout.fileno(), testclass.vpp.stderr.fileno(), testclass.pump_thread_wakeup_pipe[0]], [], [])[0] if testclass.vpp.stdout.fileno() in readable: read = os.read(testclass.vpp.stdout.fileno(), 102400) if len(read) > 0: split = read.splitlines(True) if len(stdout_fragment) > 0: split[0] = "%s%s" % (stdout_fragment, split[0]) if len(split) > 0 and split[-1].endswith("\n"): limit = None else: limit = -1 stdout_fragment = split[-1] testclass.vpp_stdout_deque.extend(split[:limit]) if not testclass.cache_vpp_output: for line in split[:limit]: testclass.logger.debug( "VPP STDOUT: %s" % line.rstrip("\n")) if testclass.vpp.stderr.fileno() in readable: read = os.read(testclass.vpp.stderr.fileno(), 102400) if len(read) > 0: split = read.splitlines(True) if len(stderr_fragment) > 0: split[0] = "%s%s" % (stderr_fragment, split[0]) if len(split) > 0 and split[-1].endswith(b"\n"): limit = None else: limit = -1 stderr_fragment = split[-1] testclass.vpp_stderr_deque.extend(split[:limit]) if not testclass.cache_vpp_output: for line in split[:limit]: testclass.logger.debug( "VPP STDERR: %s" % line.rstrip("\n")) # ignoring the dummy pipe here intentionally - the # flag will take care of properly terminating the loop def _is_skip_aarch64_set(): return os.getenv('SKIP_AARCH64', 'n').lower() in ('yes', 'y', '1') is_skip_aarch64_set = _is_skip_aarch64_set() def _is_platform_aarch64(): return platform.machine() == 'aarch64' is_platform_aarch64 = _is_platform_aarch64() def _running_extended_tests(): s = os.getenv("EXTENDED_TESTS", "n") return True if s.lower() in ("y", "yes", "1") else False running_extended_tests = _running_extended_tests() def _running_on_centos(): os_id = os.getenv("OS_ID", "") return True if "centos" in os_id.lower() else False running_on_centos = _running_on_centos class KeepAliveReporter(object): """ Singleton object which reports test start to parent process """ _shared_state = {} def __init__(self): self.__dict__ = self._shared_state self._pipe = None @property def pipe(self): return self._pipe @pipe.setter def pipe(self, pipe): if self._pipe is not None: raise Exception("Internal error - pipe should only be set once.") self._pipe = pipe def send_keep_alive(self, test, desc=None): """ Write current test tmpdir & desc to keep-alive pipe to signal liveness """ if self.pipe is None: # if not running forked.. return if isclass(test): desc = '%s (%s)' % (desc, unittest.util.strclass(test)) else: desc = test.id() self.pipe.send((desc, test.vpp_bin, test.tempdir, test.vpp.pid)) class VppTestCase(unittest.TestCase): """This subclass is a base class for VPP test cases that are implemented as classes. It provides methods to create and run test case. """ extra_vpp_punt_config = [] extra_vpp_plugin_config = [] @property def packet_infos(self): """List of packet infos""" return self._packet_infos @classmethod def get_packet_count_for_if_idx(cls, dst_if_index): """Get the number of packet info for specified destination if index""" if dst_if_index in cls._packet_count_for_dst_if_idx: return cls._packet_count_for_dst_if_idx
/*
* Copyright (c) 2015 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __included_vxlan_packet_h__
#define __included_vxlan_packet_h__ 1
/*
* From RFC-7384
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |R|R|R|R|I|R|R|R| Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | VXLAN Network Identifier (VNI) | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* VXLAN Header: This is an 8-byte field that has:
*
* - Flags (8 bits): where the I flag MUST be set to 1 for a valid
* VXLAN Network ID (VNI). The other 7 bits (designated "R") are
* reserved fields and MUST be set to zero on transmission and
* ignored on receipt.
*
* - VXLAN Segment ID/VXLAN Network Identifier (VNI): this is a
* 24-bit value used to designate the individual VXLAN overlay
* network on which the communicating VMs are situated. VMs in
* different VXLAN overlay networks cannot communicate with each
* other.
*
* - Reserved fields (24 bits and 8 bits): MUST be set to zero on
* transmission and ignored on receipt.
*
*/
typedef struct {
u8 flags;
u8 res1;
u8 res2;
u8 res3;
u32 vni_reserved;
} vxlan_header_t;
#define VXLAN_FLAGS_I 0x08
static inline u32 vnet_get_vni (vxlan_header_t * h)
{
u32 vni_reserved_host_byte_order;
vni_reserved_host_byte_order = clib_net_to_host_u32 (h->vni_reserved);
return vni_reserved_host_byte_order >> 8;
}
static inline void vnet_set_vni_and_flags (vxlan_header_t * h, u32 vni)
{
h->vni_reserved = clib_host_to_net_u32 (vni<<8);
* (u32 *) h = 0;
h->flags = VXLAN_FLAGS_I;
}
#endif /* __included_vxlan_packet_h__ */