import os import time import socket import struct from traceback import format_exc, format_stack from scapy.utils import wrpcap, rdpcap, PcapReader from scapy.plist import PacketList from vpp_interface import VppInterface from scapy.layers.l2 import Ether, ARP from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA,\ ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr, ICMPv6ND_RA, RouterAlert, \ 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): """ Is packet one of uninteresting IPv6 broadcasts? """ if p.haslayer(ICMPv6ND_RA): if in6_ismaddr(p[IPv6].dst): return True if p.haslayer(IPv6ExtHdrHopByHop): for o in p[IPv6ExtHdrHopByHop].options: if isinstance(o, RouterAlert): return True return False class VppPGInterface(VppInterface): """ VPP packet-generator interface """ @property def pg_index(self): """packet-generator interface index assigned by VPP""" return self._pg_index @property def out_path(self): """pcap file path - captured packets""" return self._out_path @property def in_path(self): """ pcap file path - injected packets""" return self._in_path @property def capture_cli(self): """CLI string to start capture on this interface""" return self._capture_cli @property def cap_name(self): """capture name for this interface""" return self._cap_name @property def input_cli(self): """CLI string to load the injected packets""" return self._input_cli @property def in_history_counter(self): """Self-incrementing counter used when renaming old pcap files""" v = self._in_history_counter self._in_history_counter += 1 return v @property def out_history_counter(self): """Self-incrementing counter used when renaming old pcap files""" v = self._out_history_counter self._out_history_counter += 1 return v def __init__(self, test, pg_index): """ 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) self._in_history_counter = 0 self._out_history_counter = 0 self._out_assert_counter = 0 self._pg_index = pg_index 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._input_cli = \ "packet-generator new pcap %s source pg%u name %s" % ( self.in_path, self.pg_index, self.cap_name) def enable_capture(self): """ Enable capture on this packet-generator interface""" try: if os.path.isfile(self.out_path): name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % \ (self.test.tempdir, time.time(), self.name, self.out_history_counter, self._out_file) self.test.logger.debug("Renaming %s->%s" % (self.out_path, name)) os.rename(self.out_path, name) except: pass # 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): """ Add a stream of packets to this packet-generator :param pkts: iterable packets """ try: if os.path.isfile(self.in_path): name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %\ (self.test.tempdir, time.time(), self.name, self.in_history_counter, self._in_file) self.test.logger.debug("Renaming %s->%s" % (self.in_path, name)) os.rename(self.in_path, name) except: pass 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: if not self.wait_for_capture_file(timeout): return None output = rdpcap(self.out_path) self.test.logger.debug("Capture has %s packets" % len(output.res)) except: self.test.logger.debug("Exception in scapy.rdpcap (%s): %s" % (self.out_path, format_exc())) return None before = len(output.res) if filter_out_fn: output.res = [p for p in output.res if not filter_out_fn(p)] removed = before - len(output.res) if removed: self.test.logger.debug( "Filtered out %s packets from capture (returning %s)" % (removed,
/*
Copyright (c) 2017 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.
*/
/*
* Note: to instantiate the template multiple times in a single file,
* #undef __included_cuckoo_template_h__...
*/
#ifndef __included_cuckoo_common_h__
#define __included_cuckoo_common_h__
#include <vppinfra/types.h>
#define CLIB_CUCKOO_OPTIMIZE_PREFETCH 1
#define CLIB_CUCKOO_OPTIMIZE_CMP_REDUCED_HASH 1
#define CLIB_CUCKOO_OPTIMIZE_UNROLL 1
#define CLIB_CUCKOO_OPTIMIZE_USE_COUNT_LIMITS_SEARCH 1
#define foreach_clib_cuckoo_error(F) \
F (CLIB_CUCKOO_ERROR_SUCCESS, 0, "success") \
F (CLIB_CUCKOO_ERROR_NOT_FOUND, -1, "object not found") \
F (CLIB_CUCKOO_ERROR_AGAIN, -2, "object busy")
typedef enum
{
#define F(n, v, s) n = v,
foreach_clib_cuckoo_error (F)
#undef F
} clib_cuckoo_error_e;
typedef struct
{
uword bucket1;
uword bucket2;
u8 reduced_hash;
} clib_cuckoo_lookup_info_t;
#endif /* __included_cuckoo_common_h__ */
/** @endcond */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/