#!/usr/bin/env python from __future__ import print_function import gc import sys import os import select import unittest import tempfile import time import resource import faulthandler from collections import deque from threading import Thread, Event from inspect import getdoc from traceback import format_exception from logging import FileHandler, DEBUG, Formatter from scapy.packet import Raw from hook import StepHook, PollHook 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 log import * from vpp_object import VppObjectRegistry 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 """ 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 """ while not testclass.pump_thread_stop_flag.wait(0): 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(), 1024) testclass.vpp_stdout_deque.append(read) if testclass.vpp.stderr.fileno() in readable: read = os.read(testclass.vpp.stderr.fileno(), 1024) testclass.vpp_stderr_deque.append(read) # ignoring the dummy pipe here intentionally - the flag will take care # of properly terminating the loop def running_extended_tests(): try: s = os.getenv("EXTENDED_TESTS") return True if s.lower() in ("y", "yes", "1") else False except: return False return False 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. """ @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[dst_if_index] else: return 0 @classmethod def instance(cls): """Return the instance of this testcase""" return cls.test_instance @classmethod def set_debug_flags(cls, d): cls.debug_core = False cls.debug_gdb = False cls.debug_gdbserver = False if d is None: return dl = d.lower() if dl == "core": cls.debug_core = True elif dl == "gdb": cls.debug_gdb = True elif dl == "gdbserver": cls.debug_gdbserver = True else: raise Exception("Unrecognized DEBUG option: '%s'" % d) @classmethod def setUpConstants(cls): """ Set-up the test case class based on environment variables """ try: s = os.getenv("STEP") cls.step = True if s.lower() in ("y", "yes", "1") else False except: cls.step = False try: d = os.getenv("DEBUG") except: d = None cls.set_debug_flags(d) cls.vpp_bin = os.getenv('VPP_TEST_BIN', "vpp") cls.plugin_path = os.getenv('VPP_TEST_PLUGIN_PATH') cls.extern_plugin_path = os.getenv('EXTERN_PLUGINS') plugin_path = None if cls.plugin_path is not None: if cls.extern_plugin_path is not None: plugin_path = "%s:%s" % ( cls.plugin_path, cls.extern_plugin_path) else: plugin_path = cls.plugin_path elif cls.extern_plugin_path is not None: plugin_path = cls.extern_plugin_path debug_cli = "" if cls.step or cls.debug_gdb or cls.debug_gdbserver: debug_cli = "cli-listen localhost:5002" coredump_size = None try: size = os.getenv("COREDUMP_SIZE") if size is not None: coredump_size = "coredump-size %s" % size except: pass if coredump_size is None: coredump_size = "coredump-size unlimited" cls.vpp_cmdline = [cls.vpp_bin, "unix", "{", "nodaemon", debug_cli, coredump_size, "}", "api-trace", "{", "on", "}", "api-segment", "{", "prefix", cls.shm_prefix, "}", "plugins", "{", "plugin", "dpdk_plugin.so", "{", "disable", "}", "}"] if plugin_path is not None: cls.vpp_cmdline.extend(["plugin_path", plugin_path]) cls.logger.info("vpp_cmdline: %s" % cls.vpp_cmdline) @classmethod def wait_for_enter(cls): if cls.debug_gdbserver: print(double_line_delim) print("Spawned GDB server with PID: %d" % cls.vpp.pid) elif cls.debug_gdb: print(double_line_delim) print("Spawned VPP with PID: %d" % cls.vpp.pid) else: cls.logger.debug("Spawned VPP with PID: %d" % cls.vpp.pid) return print(single_line_delim) print("You can debug the VPP using e.g.:") if cls.debug_gdbserver: print("gdb " + cls.vpp_bin + " -ex 'target remote localhost:7777'") print("Now is the time to attach a gdb by running the above " "command, set up breakpoints etc. and then resume VPP from " "within gdb by issuing the 'continue' command") elif cls.debug_gdb: print("gdb " + cls.vpp_bin + " -ex 'attach %s'" % cls.vpp.pid) print("Now is the time to attach a gdb by running the above " "command and set up breakpoints etc.") print(single_line_delim) raw_input("Press ENTER to continue running the testcase...") @classmethod def run_vpp(cls): cmdline = cls.vpp_cmdline if cls.debug_gdbserver: gdbserver = '/usr/bin/gdbserver' if not os.path.isfile(gdbserver) or \ not os.access(gdbserver, os.X_OK): raise Exception("gdbserver binary '%s' does not exist or is " "not executable" % gdbserver) cmdline = [gdbserver, 'localhost:7777'] + cls.vpp_cmdline cls.logger.info("Gdbserver cmdline is %s", " ".join(cmdline)) try: cls.vpp = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1) except Exception as e: cls.logger.critical("Couldn't start vpp: %s" % e) raise cls.wait_for_enter() @classmethod def setUpClass(cls): """ Perform class setup before running the testcase Remove shared memory files, start vpp and connect the vpp-api """ gc.collect() # run garbage collection first cls.logger = getLogger(cls.__name__) cls.tempdir = tempfile.mkdtemp( prefix='vpp-unittest-' + cls.__name__ + '-') cls.file_handler = FileHandler("%s/log.txt" % cls.tempdir) cls.file_handler.setFormatter( Formatter(fmt='%(asctime)s,%(msecs)03d %(message)s', datefmt="%H:%M:%S")) cls.file_handler.setLevel(DEBUG) cls.logger.addHandler(cls.file_handler) cls.shm_prefix = cls.tempdir.split("/")[-1] os.chdir(cls.tempdir) cls.logger.info("Temporary dir is %s, shm prefix is %s", cls.tempdir, cls.shm_prefix) cls.setUpConstants() cls.reset_packet_infos() cls._captures = [] cls._zombie_captures = [] cls.verbose = 0 cls.vpp_dead = False cls.registry = VppObjectRegistry() cls.vpp_startup_failed = False # need to catch exceptions here because if we raise, then the cleanup # doesn't get called and we might end with a zombie vpp try: cls.run_vpp() cls.vpp_stdout_deque = deque() cls.vpp_stderr_deque = deque() cls.pump_thread_stop_flag = Event() cls.pump_thread_wakeup_pipe = os.pipe() cls.pump_thread = Thread(target=pump_output, args=(cls,)) cls.pump_thread.daemon = True cls.pump_thread.start() cls.vapi = VppPapiProvider(cls.shm_prefix, cls.shm_prefix, cls) if cls.step: hook = StepHook(cls) else: hook = PollHook(cls) cls.vapi.register_hook(hook) cls.sleep(0.1, "after vpp startup, before initial poll") try: hook.poll_vpp() except: cls.vpp_startup_failed = True cls.logger.critical( "VPP died shortly after startup, check the" " output to standard error for possible cause") raise try: cls.vapi.connect() except: if cls.debug_gdbserver: print(colorize("You're running VPP inside gdbserver but " "VPP-API connection failed, did you forget " "to 'continue' VPP from within gdb?", RED)) raise except: t, v, tb = sys.exc_info() try: cls.quit() except: pass raise t, v, tb @classmethod def quit(cls): """ Disconnect vpp-api, kill vpp and cleanup shared memory files """ if (cls.debug_gdbserver or cls.debug_gdb) and hasattr(cls, 'vpp'): cls.vpp.poll() if cls.vpp.returncode is None: print(double_line_delim) print("VPP or GDB server is still running") print(single_line_delim) raw_input("When done debugging, press ENTER to kill the " "process and finish running the testcase...") os.write(cls.pump_thread_wakeup_pipe[1], 'ding dong wake up') cls.pump_thread_stop_flag.set() if hasattr(cls, 'pump_thread'): cls.logger.debug("Waiting for pump thread to stop") cls.pump_thread.join() if hasattr(cls, 'vpp_stderr_reader_thread'): cls.logger.debug("Waiting for stdderr pump to stop") cls.vpp_stderr_reader_thread.join() if hasattr(cls, 'vpp'): if hasattr(cls, 'vapi'): cls.vapi.disconnect() del cls.vapi cls.vpp.poll() if cls.vpp.returncode is None: cls.logger.debug("Sending TERM to vpp") cls.vpp.terminate() cls.logger.debug("Waiting for vpp to die") cls.vpp.communicate() del cls.vpp if cls.vpp_startup_failed: stdout_log = cls.logger.info stderr_log = cls.logger.critical else: stdout_log = cls.logger.info stderr_log = cls.logger.info if hasattr(cls, 'vpp_stdout_deque'): stdout_log(single_line_delim) stdout_log('VPP output to stdout while running %s:', cls.__name__) stdout_log(single_line_delim) vpp_output = "".join(cls.vpp_stdout_deque) with open(cls.tempdir + '/vpp_stdout.txt', 'w') as f: f.write(vpp_output) stdout_log('\n%s', vpp_output) stdout_log(single_line_delim) if hasattr(cls, 'vpp_stderr_deque'): stderr_log(single_line_delim) stderr_log('VPP output to stderr while running %s:', cls.__name__) stderr_log(single_line_delim) vpp_output = "".join(cls.vpp_stderr_deque) with open(cls.tempdir + '/vpp_stderr.txt', 'w') as f: f.write(vpp_output) stderr_log('\n%s', vpp_output) stderr_log(single_line_delim) @classmethod def tearDownClass(cls): """ Perform final cleanup after running all tests in this test-case """ cls.quit() cls.file_handler.close() def tearDown(self): """ Show various debug prints after each test """ self.logger.debug("--- tearDown() for %s.%s(%s) called ---" % (self.__class__.__name__, self._testMethodName, self._testMethodDoc)) if not self.vpp_dead: self.logger.debug(self.vapi.cli("show trace")) self.logger.info(self.vapi.ppcli("show interface")) self.logger.info(self.vapi.ppcli("show hardware")) self.logger.info(self.vapi.ppcli("show error")) self.logger.info(self.vapi.ppcli("show run")) self.registry.remove_vpp_config(self.logger) # Save/Dump VPP api trace log api_trace = "vpp_api_trace.%s.log" % self._testMethodName tmp_api_trace = "/tmp/%s" % api_trace vpp_api_trace_log = "%s/%s" % (self.tempdir, api_trace) self.logger.info(self.vapi.ppcli("api trace save %s" % api_trace)) self.logger.info("Moving %s to %s\n" % (tmp_api_trace, vpp_api_trace_log)) os.rename(tmp_api_trace, vpp_api_trace_log) self.logger.info(self.vapi.ppcli("api trace dump %s" % vpp_api_trace_log)) else: self.registry.unregister_all(self.logger) def setUp(self): """ Clear trace before running each test""" self.logger.debug("--- setUp() for %s.%s(%s) called ---" % (self.__class__.__name__, self._testMethodName, self._testMethodDoc)) if self.vpp_dead: raise Exception("VPP is dead when setting up the test") self.sleep(.1, "during setUp") self.vpp_stdout_deque.append( "--- test setUp() for %s.%s(%s) starts here ---\n" % (self.__class__.__name__, self._testMethodName, self._testMethodDoc)) self.vpp_stderr_deque.append( "--- test setUp() for %s.%s(%s) starts here ---\n" % (self.__class__.__name__, self._testMethodName, self._testMethodDoc)) self.vapi.cli("clear trace") # store the test instance inside the test class - so that objects # holding the class can access instance methods (like assertEqual) type(self).test_instance = self @classmethod def pg_enable_capture(cls, interfaces): """ Enable capture on packet-generator interfaces :param interfaces: iterable interface indexes """ for i in interfaces: i.enable_capture()
/*
Copyright (c) 2014 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.
*/
/** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
/*
* Note: to instantiate the template multiple times in a single file,
* #undef __included_bihash_template_h__...
*/
#ifndef __included_bihash_template_h__
#define __included_bihash_template_h__
#include <vppinfra/heap.h>
#include <vppinfra/format.h>
#include <vppinfra/pool.h>
#include <vppinfra/cache.h>
#include <vppinfra/lock.h>
#ifndef BIHASH_TYPE
#error BIHASH_TYPE not defined
#endif
#define _bv(a,b) a##b
#define __bv(a,b) _bv(a,b)
#define BV(a) __bv(a,BIHASH_TYPE)
#define _bvt(a,b) a##b##_t
#define __bvt(a,b) _bvt(a,b)
#define BVT(a) __bvt(a,BIHASH_TYPE)
typedef struct BV (clib_bihash_value)
{
union
{
BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
struct BV (clib_bihash_value) * next_free;
};
} BVT (clib_bihash_value);
#define BIHASH_BUCKET_OFFSET_BITS 36
typedef struct
{
union
{
struct
{
u64 offset:BIHASH_BUCKET_OFFSET_BITS;
u64 lock:1;
u64 linear_search:1;
u64 log2_pages:8;
i64 refcnt:16;
};
u64 as_u64;
};
} BVT (clib_bihash_bucket);
STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
typedef struct
{
BVT (clib_bihash_value) * values;
BVT (clib_bihash_bucket) * buckets;
volatile u32 *alloc_lock;
BVT (clib_bihash_value) ** working_copies;
int *working_copy_lengths;
BVT (clib_bihash_bucket) saved_bucket;
u32 nbuckets;
u32 log2_nbuckets;
u8 *name;
u64 cache_hits;
u64 cache_misses;
BVT (clib_bihash_value) ** freelists;
/*
* Backing store allocation. Since bihash manages its own
* freelists, we simple dole out memory at alloc_arena_next.
*/
uword alloc_arena;
uword alloc_arena_next;
uword alloc_arena_size;
/**
* A custom format function to print the Key and Value of bihash_key instead of default hexdump
*/
format_function_t *fmt_fn;
} BVT (clib_bihash);
static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
{
while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
CLIB_PAUSE ();
}
static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
{
__atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
}
static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
{
BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
do
{
locked_bucket.as_u64 = unlocked_bucket.as_u64 = b