Packet capture framework enhancements
Subsystems (2)
cover Packet capture framework enhancements
This patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
And it keeps the old pdump command as is for those people who never want to change.
The one missing piece is that dumpcap utility does not yet have the necessary converter to take the classic BPF for pcap_compile and convert it to eBPF for DPDK. (It is not hard, just not working right yet.)
Stephen Hemminger (5):
librte_pcapng: add new library for writing pcapng files
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new pcapng and dumper
MAINTAINERS | 6 + app/dumpcap/main.c | 829 ++++++++++++++++++ app/dumpcap/meson.build | 11 + app/meson.build | 1 + doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + doc/guides/howto/packet_capture_framework.rst | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 80 ++ doc/guides/tools/index.rst | 1 + lib/meson.build | 5 +- lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++ lib/pcapng/rte_pcapng.c | 543 ++++++++++++ lib/pcapng/rte_pcapng.h | 175 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 386 +++++--- lib/pdump/rte_pdump.h | 117 ++- lib/pdump/version.map | 8 + 23 files changed, 2283 insertions(+), 162 deletions(-) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
Signature · 1 line
2.30.2
PATCH 1/5 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 543 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 175 ++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 868 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +543 -0
543 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +175 -0
175 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_init; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH 2/5 pdump: support pcapng and filtering
In reply to Stephen HemmingerThis enhances the DPDK pdump library to support new pcapng format and filtering via BPF.
The internal client/server protocol is changed to support two versions: the original pdump basic version and a new pcapng version.
The internal version number (not part of exposed API or ABI) is intentionally increased to cause any attempt to try mismatched primary/secondary process to fail.
Add new API to do allow filtering of captured packets with DPDK BPF (eBPF) filter program. It keeps statistics
on packets captured, filtered, and missed (because ring was full).
--- lib/meson.build | 4 +- lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 386 +++++++++++++++++++++++++++++------------- lib/pdump/rte_pdump.h | 117 ++++++++++++- lib/pdump/version.map | 8 + 5 files changed, 394 insertions(+), 123 deletions(-)
lib/meson.build +2 -2
@@ -26,6 +26,7 @@ libraries = [ 'timer', # eventdev depends on this 'acl', 'bbdev', + 'bpf', 'bitratestats', 'cfgfile', 'compressdev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'rib',
@@ -55,10 +55,10 @@ libraries = [ 'ipsec', # ipsec lib depends on net, crypto and security 'fib', #fib lib depends on rib 'port', # pkt framework libs which use other libs from above + 'pdump', # pdump lib depends on bpf pcapng 'table', 'pipeline', 'flow_classify', # flow_classify lib depends on pkt framework table lib - 'bpf', 'graph', 'node', ]
lib/pdump/meson.build +1 -1
@@ -3,4 +3,4 @@ sources = files('rte_pdump.c') headers = files('rte_pdump.h') -deps += ['ethdev'] +deps += ['ethdev', 'bpf', 'pcapng']
lib/pdump/rte_pdump.c +269 -117
586 lines not on this page · raw message
lib/pdump/rte_pdump.h +114 -3
159 lines not on this page · raw message
lib/pdump/version.map +8 -0
@@ -10,3 +10,11 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_pdump_enable_bpf; + rte_pdump_enable_bpf_by_deviceid; + rte_pdump_get_stats; +};
Signature · 1 line
2.30.2
PATCH 3/5 app/dumpcap: add new packet capture application
In reply to Stephen HemmingerThis is a new packet capture application to replace existing pdump. The new application works like Wireshark dumpcap program and supports the pdump API features.
It is not complete yet some features such as filtering are not implemented.
--- app/dumpcap/main.c | 829 ++++++++++++++++++++++++++++++++++++++++ app/dumpcap/meson.build | 11 + app/meson.build | 1 + 3 files changed, 841 insertions(+) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build
app/dumpcap/main.c new file +829 -0
@@ -0,0 +1,829 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019-2020 Microsoft Corporation +* +* DPDK application to dump network traffic +* This is designed to look and act like the Wireshark +* dumpcap program. +*/ + +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <inttypes.h> +#include <signal.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/queue.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/utsname.h> +#include <time.h> +#include <unistd.h> + +#include <rte_alarm.h> +#include <rte_config.h> +#include <rte_eal.h> +#include <rte_errno.h> +#include <rte_ethdev.h> +#include <rte_lcore.h> +#include <rte_malloc.h> +#include <rte_mbuf.h> +#include <rte_mempool.h> +#include <rte_pcapng.h> +#include <rte_pdump.h> +#include <rte_ring.h> +#include <rte_string_fns.h> +#include <rte_version.h> + +#include <pcap/pcap.h> + +#define RING_NAME "capture-ring" +#define MONITOR_INTERVAL (500 * 1000) +#define MBUF_POOL_CACHE_SIZE 32 +#define BURST_SIZE 32 +#define SLEEP_THRESHOLD 1000 + +static const char *progname; +static bool quit_signal; +static bool group_read; +static bool quiet; +static bool promiscuous_mode = true; +static bool use_pcapng = true; +static bool show_statistics; +static char *output_name; +static const char *filter_str; +static unsigned int ring_size =2048 ; +static uint64_t packets_received; +static size_t file_size; +static const char *capture_comment; +static uint32_t snaplen =65535 ; +static bool dump_bpf; +static struct timespec start_time; + +static struct { +double duration; +unsigned long packets; +size_t size; +} stop; + +struct interface { +TAILQ_ENTRY (interface) next; +uint64_t missed; +uint16_t port; +char name[RTE_ETH_NAME_MAX_LEN ]; + +struct rte_rxtx_callback *rx_cb[RTE_MAX_QUEUES_PER_PORT ]; +}; + +TAILQ_HEAD (interface_list, interface); +static struct interface_list interfaces =TAILQ_HEAD_INITIALIZER (interfaces); +static struct interface *port2intf[RTE_MAX_ETHPORTS ]; + +static void usage (void ) +{ +printf ("Usage: %s [options] ...\n\n" , progname); +printf ("Capture Interface:\n" +" -i <interface> name or port index of interface\n" +" -f <capture filter> packet filter in libpcap filter syntax\n" +" -s <snaplen>, --snapshot-length <snaplen>\n" +" packet snapshot length (def: appropriate maximum)\n" +" -p, --no-promiscuous-mode\n" +" don't capture in promiscuous mode\n" +" -D, --list-interfaces print list of interfaces and exit\n" +" -d print generated BPF code for capture filter\n" +" -S print statistics for each interface once per second\n" +"\n" +"Stop conditions:\n" +" -c <packet count> stop after n packets (def: infinite)\n" +" -a <autostop cond.> ..., --autostop <autostop cond.> ...\n" +" duration:NUM - stop after NUM seconds\n" +" filesize:NUM - stop this file after NUM kB\n" +" packets:NUM - stop after NUM packets\n" +"Output (files):\n" +" -w <filename> name of file to save (def: tempfile)\n" +" -g enable group read access on the output file(s)\n" +" -n use pcapng format instead of pcap (default)\n" +" -P use libpcap format instead of pcapng\n" +" --capture-comment <comment>\n" +" add a capture comment to the output file\n" +"\n" +"Miscellaneous:\n" +" -q don't report packet capture counts\n" +" -v, --version print version information and exit\n" +" -h, --help display this help and exit\n" +"\n" +"Use Ctrl-C to stop capturing at any time.\n" ); +} + +static const char *version (void ) +{ +static char str[128 ]; + +snprintf (str,sizeof (str), +"%s 1.0 (%s)\n" , progname,rte_version ()); +return str; +} + +/* Parse numeric argument from command line */ +static unsigned long get_uint (const char *arg,const char *name, +unsigned int limit) +{ +unsigned long u; +char *endp; + + u =strtoul (arg, &endp,0 ); +if (*arg =='\0' || *endp !='\0' ) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is not a valid number\n" , + name, arg); +if (limit && u > limit) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is too large (greater than %u)\n" , + name, arg, limit); + +return u; +} + +/* Set auto stop values */ +static void autostop (char *opt) +{ +char *value, *endp; + + value =strchr (opt,':' ); +if (value ==NULL ) +rte_exit (EXIT_FAILURE , +"Missing colon in auto stop parameter\n" ); + + *value++ ='\0' ; +if (strcmp (opt,"duration" ) ==0 ) { + stop.duration =strtod (value, &endp); +if (*value =='\0' || *endp !='\0' || stop.duration <0 ) +rte_exit (EXIT_FAILURE , +"Invalid duration \"%s\"\n" , value); + }else if (strcmp (opt,"filesize" ) ==0 ) { + stop.size =get_uint (value,"filesize" ,0 ) *1024 ; + }else if (strcmp (opt,"packets" ) ==0 ) { + stop.packets =get_uint (value,"packets" ,0 ); + }else { +rte_exit (EXIT_FAILURE , +"Unknown autostop parameter \"%s\"\n" , opt); + } +} + +/* Add interface to list of interfaces to capture */ +static void add_interface (uint16_t port,const char *name) +{ +struct interface *intf; + + intf =malloc (sizeof (*intf)); +if (!intf) +rte_exit (EXIT_FAILURE ,"no memory for interface\n" ); + +memset (intf,0 ,sizeof (*intf)); +strlcpy (intf->name, name,sizeof (intf->name)); + +printf ("Capturing on '%s'\n" , name); + + port2intf[port] = intf; +TAILQ_INSERT_TAIL (&interfaces, intf, next); +} + +/* Select all valid DPDK interfaces */ +static void select_all_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); + } +} + +/* +* Choose interface to capture if no -i option given. +* Select the first DPDK port, this matches what dumpcap does. +*/ +static void set_default_interface (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); +return ; + } +rte_exit (EXIT_FAILURE ,"No usable interfaces found\n" ); +} + +/* Lookup interface by name or port and add it to the list */ +static void select_interface (const char *arg) +{ +uint16_t port; + +if (strcmp (arg,"*" )) +select_all_interfaces (); +else if (rte_eth_dev_get_port_by_name (arg, &port) ==0 ) +add_interface (port, arg); +else { +char name[RTE_ETH_NAME_MAX_LEN ]; + + port =get_uint (arg,"port_number" ,UINT16_MAX ); +if (rte_eth_dev_get_name_by_port (port, name) <0 ) +rte_exit (EXIT_FAILURE ,"Invalid port number %u\n" , + port); +add_interface (port, name); + } +} + +/* Display list of possible interfaces that can be used. */ +static void show_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +printf ("%u. %s\n" , p, name); + } +} + +static struct rte_bpf *compile_filter (void ) +{ +struct bpf_program fcode; +pcap_t *pcap; + + pcap =pcap_open_dead (DLT_EN10MB , snaplen); +if (!pcap) +rte_exit (EXIT_FAILURE ,"can not open pcap\n" ); + +if (pcap_compile (pcap, &fcode, filter_str, +1 ,PCAP_NETMASK_UNKNOWN ) !=0 ) +rte_exit (EXIT_FAILURE ,"pcap filter string not valid (%s)\n" , +pcap_geterr (pcap)); + +rte_exit (EXIT_FAILURE ,"filter not implemented yet\n" ); + +/* +* Need to convert classic BPF to eBPF and put in shared memory +* be read by primary process. +*/ +pcap_freecode (&fcode); +pcap_close (pcap); + +rte_exit (EXIT_FAILURE ,"not implemented\n" ); +return NULL ; +} + +/* +* Parse command line options. +* These are chosen to be similar to dumpcap command. +*/ +static void parse_opts (int argc,char **argv) +{ +static const struct option long_options[] = { + {"autostop" , required_argument,NULL ,'a' }, + {"capture-comment" , required_argument,NULL ,0 }, + {"help" , no_argument,NULL ,'h' }, + {"interface" , required_argument,NULL ,'i' }, + {"list-interfaces" , no_argument,NULL ,'D' }, + {"no-promiscuous-mode" , no_argument,NULL ,'p' }, + {"output-file" , required_argument,NULL ,'w' }, + {"ring-buffer" , required_argument,NULL ,'b' }, + {"snapshot-length" , required_argument,NULL ,'s' }, + {"version" , no_argument,NULL ,'v' }, + {NULL }, + }; +int option_index, c; + +for (;;) { + c =getopt_long (argc, argv,"a:b:c:dDf:ghi:nN:pPqs:Svw:" , + long_options, &option_index); +if (c ==-1 ) +break ; + +switch (c) { +case 0 : +switch (option_index) { +case 0 : + capture_comment = optarg; +break ; +default : +usage (); +exit (1 ); + } +break ; +case 'a' : +autostop (optarg); +break ; +case 'b' : +rte_exit (EXIT_FAILURE , +"multiple files not implemented\n" ); +break ; +case 'c' : + stop.packets =get_uint (optarg,"packet_count" ,0 ); +break ; +case 'd' : + dump_bpf = true; +break ; +case 'D' : +show_interfaces (); +exit (0 ); +case 'f' : + filter_str = optarg; +break ; +case 'g' : + group_read = true; +break ; +case 'h' : +printf ("%s\n\n" ,version ()); +usage (); +exit (0 ); +case 'i' : +select_interface (optarg); +break ; +case 'n' : + use_pcapng = true; +break ; +case 'N' : + ring_size =get_uint (optarg,"packet_limit" ,0 ); +break ; +case 'p' : + promiscuous_mode = false; +break ; +case 'P' : + use_pcapng = false; +break ; +case 'q' : + quiet = true; +break ; +case 's' : + snaplen =get_uint (optarg,"snap_len" ,0 ); +break ; +case 'S' : + show_statistics = true; +break ; +case 'w' : + output_name = optarg; +break ; +case 'v' : +printf ("%s\n" ,version ()); +exit (0 ); +default : +fprintf (stderr,"Invalid option: %s\n" , + argv[optind -1 ]); +usage (); +exit (1 ); + } + } +} + +static void +signal_handler(int sig_num __rte_unused) +{ +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); +} + +static double elapsed (void ) +{ +struct timespec now; +double secs; + +clock_gettime (CLOCK_MONOTONIC_COARSE , &now); + secs = now.tv_sec - start_time.tv_sec; + secs += (now.tv_nsec - start_time.tv_nsec) /1.e9 ; +return secs; +} + +static void +cleanup_pdump_resources (void ) +{ +struct interface *intf; + + TAILQ_FOREACH(intf, &interfaces, next) { +rte_pdump_disable (intf->port, +RTE_PDUMP_ALL_QUEUES ,RTE_PDUMP_FLAG_RXTX ); +if (promiscuous_mode) +rte_eth_promiscuous_disable (intf->port); + } +} + +/* Alarm signal handler, used to check that primary process */ +static void +monitor_primary(void *arg __rte_unused) +{ +if (__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) +return ; + +if (rte_eal_primary_proc_alive (NULL )) { +rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); +return ; + } + +fprintf (stderr,"Primary process is no longer active, exiting...\n" ); +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); +} + +/* Setup handler to check when primary exits. */ +static void +enable_primary_monitor (void ) +{ +int ret; + +/* Once primary exits, so will pdump. */ + ret =rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to enable monitor:%d\n" , ret); +} + +static void +disable_primary_monitor (void ) +{ +int ret; + + ret =rte_eal_alarm_cancel (monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to disable monitor:%d\n" , ret); +} + +static void +report_packet_stats (rte_pcapng_t *out) +{ +struct interface *intf; +struct rte_pdump_stats rxstats, txstats; +struct rte_eth_stats stats; +uint64_t received, accepted, dropped, ifdrop; + +fputc ('\n' , stderr); + TAILQ_FOREACH(intf, &interfaces, next) { +if (rte_pdump_get_stats (intf->port,UINT16_MAX , + &rxstats, &txstats) <0 ) +continue ; + +if (rte_eth_stats_get (intf->port, &stats) <0 ) + ifdrop =0 ; +else + ifdrop = stats.imissed - intf->missed; + + received = rxstats.received + txstats.received; + accepted = rxstats.accepted + txstats.accepted; + dropped = rxstats.missed + txstats.missed; + +if (use_pcapng) +rte_pcapng_write_stats (out, intf->port, + received, ifdrop, accepted, dropped); +fprintf (stderr, +"Packets received/dropped on interface '%s': " +"%" PRIu64"/%" PRIu64" (%.1f)\n" , + intf->name, received, dropped, + received ?100. * received / (received + dropped) :0. ); + } +} + +/* +* Start DPDK EAL with arguments. +* Unlike most DPDK programs, this application does not use the +* typical EAL command line arguments. +* We don't want to expose all the DPDK internals to the user. +*/ +static void dpdk_init (void ) +{ +const char *args[] = { + progname,"--proc-type" ,"secondary" ,"--log-level" ,"error" + }; +const int eal_argc =RTE_DIM (args); +char **eal_argv; +unsigned int i; + +/* DPDK API requires mutable versions of command line arguments. */ + eal_argv =calloc (eal_argc +1 ,sizeof (char *)); +if (eal_argv ==NULL ) +rte_panic ("No memory\n" ); + +for (i =0 ; i <RTE_DIM (args); i++) + eal_argv[i] =strdup (args[i]); + +if (rte_eal_init (eal_argc, eal_argv) <0 ) +rte_panic ("EAL init failed\n" ); + +if (rte_eth_dev_count_avail () ==0 ) +rte_exit (EXIT_FAILURE ,"No Ethernet ports found\n" ); +} + +/* Create packet ring shared between callbacks and process */ +static struct rte_ring *create_ring (void ) +{ +struct rte_ring *pring; +size_t size, log2; + +/* Find next power of 2 >= size. */ + size = ring_size; + log2 =sizeof (size) *8 -__builtin_clzl (size -1 ); + size =1u << log2; + +if (size != ring_size) { +fprintf (stderr,"Ring size %u rounded up to %zu\n" , + ring_size, size); + ring_size = size; + } + + pring =rte_ring_lookup (RING_NAME ); +if (pring ==NULL ) { + pring =rte_ring_create (RING_NAME , ring_size, +rte_socket_id (),0 ); +if (pring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); + } +return pring; +} + +static struct rte_mempool *create_mempool (void ) +{ +static const char pool_name[] ="capture_mbufs" ; +size_t num_mbufs =2 * ring_size; +struct rte_mempool *mp; +uint16_t data_size =RTE_MBUF_DEFAULT_BUF_SIZE ; + + mp =rte_mempool_lookup (pool_name); +if (mp) +return mp; + +if (snaplen < data_size) + data_size = snaplen; + + mp =rte_pktmbuf_pool_create_by_ops (pool_name, num_mbufs, +MBUF_POOL_CACHE_SIZE ,0 , + data_size, +rte_socket_id (),"ring_mp_sc" ); +if (mp ==NULL ) +rte_exit (EXIT_FAILURE , +"Mempool (%s) creation failed: %s\n" , pool_name, +rte_strerror (rte_errno)); + +return mp; +} + +static void *create_output (void ) +{ +struct utsname uts; +char os[256 ]; +int fd; + +/* If no filename specified make a tempfile name */ +if (output_name ==NULL ) { +struct interface *intf; +struct tm *tm; +time_t now; +char ts[32 ]; + + intf =TAILQ_FIRST (&interfaces); + now =time (NULL ); + tm =localtime (&now); +if (!tm) +rte_panic ("localtime failed\n" ); + +strftime (ts,sizeof (ts),"%Y%m%d%H%M%S" , tm); +if (asprintf (&output_name,"/tmp/%s_%u_%s_%s.%s" , + progname, intf->port, intf->name, ts, + use_pcapng ?"pcapng" :"pcap" ) <0 ) +rte_panic ("asprintf failed\n" ); + } + +if (strcmp (output_name,"-" ) ==0 ) + fd =STDOUT_FILENO ; +else { +mode_t mode = group_read ?0640 :0600 ; + + fd =open (output_name,O_WRONLY |O_CREAT , mode); +if (fd <0 ) +rte_exit (EXIT_FAILURE ,"Can not open \"%s\": %s\n" , + output_name,strerror (errno)); + } + +if (use_pcapng) { +rte_pcapng_t *pcapng; + +if (uname (&uts) <0 ) +strcpy (os,"unknown" ); +else +snprintf (os,sizeof (os),"%s %s" , + uts.sysname, uts.release); + + pcapng =rte_pcapng_fdopen (fd, os,NULL ,version (), capture_comment); +if (pcapng ==NULL ) +rte_exit (EXIT_FAILURE ,"pcapng_fdopen failed: %s\n" , +strerror (rte_errno)); +return pcapng; + }else { +pcap_dumper_t *dumper; +pcap_t *pcap; + + pcap =pcap_open_dead_with_tstamp_precision (DLT_EN10MB , snaplen, +PCAP_TSTAMP_PRECISION_NANO ); +if (pcap ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_open_dead failed\n" ); + + dumper =pcap_dump_fopen (pcap,fdopen (fd,"w" )); +if (dumper ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_dump_fopen failed: %s\n" , +pcap_geterr (pcap)); +return dumper; + } +} + +static void enable_pdump (struct rte_ring *r,struct rte_mempool *mp, +struct rte_bpf *filter) +{ +struct rte_eth_stats stats; +struct interface *intf; +uint32_t flags; +int ret; + + flags =RTE_PDUMP_FLAG_RXTX ; +if (use_pcapng) + flags |=RTE_PDUMP_FLAG_PCAPNG ; + + TAILQ_FOREACH(intf, &interfaces, next) { +if (promiscuous_mode) +rte_eth_promiscuous_enable (intf->port); + +if (rte_eth_stats_get (intf->port, &stats) <0 ) + intf->missed =0 ; +else + intf->missed = stats.imissed; + + ret =rte_pdump_enable_bpf (intf->port,RTE_PDUMP_ALL_QUEUES , + flags, snaplen, + r, mp, filter); +if (ret <0 ) +rte_exit (EXIT_FAILURE , +"Packet dump enable failed: %s\n" , +rte_strerror (rte_errno)); + } +} + +/* +* Show current count of captured packets +* with backspaces to overwrite last value. +*/ +static void show_count (uint64_t count) +{ +unsigned int i; +static unsigned int bt; + +for (i =0 ; i < bt; i++) +fputc ('\b' , stderr); + + bt =fprintf (stderr,"%" PRIu64" " , count); +} + +/* Write multiple packets in older pcap format */ +static ssize_t +pcap_write_packets (pcap_dumper_t *dumper, +struct rte_mbuf *pkts[],uint16_t n) +{ +uint8_t temp_data[snaplen]; +struct pcap_pkthdr header; +uint16_t i; +size_t total =0 ; + +gettimeofday (&header.ts,NULL ); + +for (i =0 ; i < n; i++) { +struct rte_mbuf *m = pkts[i]; + + header.len =rte_pktmbuf_pkt_len (m); + header.caplen =RTE_MIN (header.len, snaplen); + +pcap_dump ((u_char *)dumper, &header, +rte_pktmbuf_read (m,0 , header.caplen, temp_data)); + + total +=sizeof (header) + header.len; + } + +return total; +} + +/* Process all packets in ring and dump to capture file */ +static int process_ring (void *out,struct rte_ring *r) +{ +struct rte_mbuf *pkts[BURST_SIZE ]; +unsigned int avail, n; +static unsigned int empty_count; +ssize_t written; + + n =rte_ring_sc_dequeue_burst (r, (void **) pkts,BURST_SIZE , + &avail); +if (n ==0 ) { +/* don't consume endless amounts of cpu if idle */ +if (empty_count <SLEEP_THRESHOLD ) + ++empty_count; +else +usleep (10 ); +return 0 ; + } + + empty_count = (avail ==0 ); + +if (use_pcapng) + written =rte_pcapng_write_packets (out, pkts, n); +else + written =pcap_write_packets (out, pkts, n); + +rte_pktmbuf_free_bulk (pkts, n); + +if (written <0 ) +return -1 ; + + file_size += written; + packets_received += n; +if (!quiet) +show_count (packets_received); + +return 0 ; +} + +int main (int argc,char **argv) +{ +struct rte_bpf *bpf_filter =NULL ; +struct rte_ring *r; +struct rte_mempool *mp; +void *out; + + progname =basename (argv[0 ]); + +dpdk_init (); +parse_opts (argc, argv); + +if (filter_str) + bpf_filter =compile_filter (); + +if (dump_bpf) +fprintf (stderr,"dump filter not implemented yet\n" ); + +if (TAILQ_EMPTY (&interfaces)) +set_default_interface (); + + r =create_ring (); + mp =create_mempool (); + out =create_output (); +if (out ==NULL ) +rte_exit (EXIT_FAILURE ,"can not open output file: %s\n" , +rte_strerror (rte_errno)); + +if (clock_gettime (CLOCK_MONOTONIC_COARSE , &start_time) <0 ) +rte_exit (EXIT_FAILURE ,"clock_gettime() failed: %s\n" , +strerror (errno)); + +enable_pdump (r, mp, bpf_filter); + +signal (SIGINT , signal_handler); +signal (SIGPIPE ,SIG_IGN ); + +enable_primary_monitor (); + +if (!quiet) { +fprintf (stderr,"Packets captured: " ); +show_count (0 ); + } + +while (!__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) { +if (process_ring (out, r) <0 ) { +fprintf (stderr,"pcapng file write failed; %s\n" , +strerror (errno)); +break ; + } + +if (stop.size && file_size >= stop.size) +break ; + +if (stop.packets && packets_received >= stop.packets) +break ; + +if (stop.duration !=0 &&elapsed () > stop.duration) +break ; + } + +disable_primary_monitor (); + +report_packet_stats (out); +if (use_pcapng) +rte_pcapng_close (out); +else +pcap_dump_close (out); + +cleanup_pdump_resources (); +rte_free (bpf_filter); +rte_ring_free (r); +rte_mempool_free (mp); + +return rte_eal_cleanup () ?EXIT_FAILURE :0 ; +}
app/dumpcap/meson.build new file +11 -0
@@ -0,0 +1,11 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +if is_windows + build = false + reason = 'not supported on Windows' + subdir_done() +endif + +sources = files('main.c') +deps += ['ethdev', 'pdump', 'pcapng']
app/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2017-2019 Intel Corporation apps = [ + 'dumpcap', 'pdump', 'proc-info', 'test-acl',
Signature · 1 line
2.30.2
Re: [dpdk-dev] [PATCH 3/5] app/dumpcap: add new packet capture application
Ken Thompson lives on...
WARNING:TYPO_SPELLING: 'CREAT' may be misspelled - perhaps 'CREATE'? #705: FILE: app/dumpcap/main.c:605:
+ fd = open(output_name, O_WRONLY | O_CREAT, mode);
PATCH 4/5 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + doc/guides/howto/packet_capture_framework.rst | 67 ++++++++-------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 ++++++ doc/guides/prog_guide/pdump_lib.rst | 28 +++++-- doc/guides/rel_notes/release_21_11.rst | 10 +++ doc/guides/tools/dumpcap.rst | 80 +++++++++++++++++++ doc/guides/tools/index.rst | 1 + 9 files changed, 174 insertions(+), 39 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -62,6 +62,16 @@ New Features * Added bus-level parsing of the devargs syntax. * Kept compatibility with the legacy syntax as parsing fallback. +* **Enhance Packet capture.** + + * New dpdk-dumpcap program that has most of the features of the + wireshark dumpcap utility including capture of multiple interfaces, + stopping after number of bytes, packets. + * New library for writing pcapng packet capture files. + * Enhancement to the pdump library to support: + * Packet filter with BPF. + * Pcapng format with timestamps and meta-data. + * Fixes packet capture with stripped VLAN tags. Removed Items -------------
doc/guides/tools/dumpcap.rst new file +80 -0
80 lines not on this page · raw message
doc/guides/tools/index.rst +1 -0
@@ -8,6 +8,7 @@ DPDK Tools User Guides :maxdepth: 2 :numbered: + dumpcap proc_info pdump pmdinfo
Signature · 1 line
2.30.2
PATCH 5/5 MAINTAINERS: add entry for new pcapng and dumper
In reply to Stephen HemmingerClaim responsibility for the new code.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -1429,6 +1429,12 @@ F: app/test/test_pdump.* F: app/pdump/ F: doc/guides/tools/pdump.rst +Packet dump +M: Stephen Hemminger <[email protected]> +F: lib/pcapng/ +F: doc/guides/prog_guide/pcapng_lib.rst +F: app/dumpcap/ +F: doc/guides/tools/dumpcap.rst Packet Framework ----------------
Signature · 1 line
2.30.2
v2 cover Packet capture framework enhancements
In reply to Stephen HemmingerThis patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
And it keeps the old pdump command as is for those people who never want to change.
The one missing piece is that dumpcap utility does not yet have the necessary converter to take the classic BPF for pcap_compile and convert it to eBPF for DPDK. (It is not hard, just not working right yet.)
v2
fix formatting of packet blocks
fix the new packet capture statistics
fix crash when primary process exits
record start/end time
various whitespace/checkpatch warnings
Stephen Hemminger (5):
librte_pcapng: add new library for writing pcapng files
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new pcapng and dumper
MAINTAINERS | 6 + app/dumpcap/main.c | 831 ++++++++++++++++++ app/dumpcap/meson.build | 18 + app/meson.build | 1 + doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +- doc/guides/howto/packet_capture_framework.rst | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 80 ++ doc/guides/tools/index.rst | 1 + lib/meson.build | 5 +- lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++ lib/pcapng/rte_pcapng.c | 577 ++++++++++++ lib/pcapng/rte_pcapng.h | 205 +++++ lib/pcapng/version.map | 13 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 419 ++++++--- lib/pdump/rte_pdump.h | 110 ++- lib/pdump/version.map | 8 + 24 files changed, 2426 insertions(+), 215 deletions(-) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
Signature · 1 line
2.30.2
PATCH v2 1/5 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 577 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 205 ++++++++++++++ lib/pcapng/version.map | 13 + 6 files changed, 933 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +577 -0
577 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +205 -0
205 lines not on this page · raw message
lib/pcapng/version.map new file +13 -0
@@ -0,0 +1,13 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_init; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH v2 2/5 pdump: support pcapng and filtering
In reply to Stephen HemmingerThis enhances the DPDK pdump library to support new pcapng format and filtering via BPF.
The internal client/server protocol is changed to support two versions: the original pdump basic version and a new pcapng version.
The internal version number (not part of exposed API or ABI) is intentionally increased to cause any attempt to try mismatched primary/secondary process to fail.
Add new API to do allow filtering of captured packets with DPDK BPF (eBPF) filter program. It keeps statistics
on packets captured, filtered, and missed (because ring was full).
--- lib/meson.build | 4 +- lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 419 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 110 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 415 insertions(+), 128 deletions(-)
lib/meson.build +2 -2
@@ -26,6 +26,7 @@ libraries = [ 'timer', # eventdev depends on this 'acl', 'bbdev', + 'bpf', 'bitratestats', 'cfgfile', 'compressdev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'rib',
@@ -55,10 +55,10 @@ libraries = [ 'ipsec', # ipsec lib depends on net, crypto and security 'fib', #fib lib depends on rib 'port', # pkt framework libs which use other libs from above + 'pdump', # pdump lib depends on bpf pcapng 'table', 'pipeline', 'flow_classify', # flow_classify lib depends on pkt framework table lib - 'bpf', 'graph', 'node', ]
lib/pdump/meson.build +1 -1
@@ -3,4 +3,4 @@ sources = files('rte_pdump.c') headers = files('rte_pdump.h') -deps += ['ethdev'] +deps += ['ethdev', 'bpf', 'pcapng']
lib/pdump/rte_pdump.c +297 -122
619 lines not on this page · raw message
lib/pdump/rte_pdump.h +107 -3
152 lines not on this page · raw message
lib/pdump/version.map +8 -0
@@ -10,3 +10,11 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_pdump_enable_bpf; + rte_pdump_enable_bpf_by_deviceid; + rte_pdump_stats; +};
Signature · 1 line
2.30.2
PATCH v2 3/5 app/dumpcap: add new packet capture application
In reply to Stephen HemmingerThis is a new packet capture application to replace existing pdump. The new application works like Wireshark dumpcap program and supports the pdump API features.
It is not complete yet some features such as filtering are not implemented.
--- app/dumpcap/main.c | 831 ++++++++++++++++++++++++++++++++++++++++ app/dumpcap/meson.build | 18 + app/meson.build | 1 + 3 files changed, 850 insertions(+) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build
app/dumpcap/main.c new file +831 -0
@@ -0,0 +1,831 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019-2020 Microsoft Corporation +* +* DPDK application to dump network traffic +* This is designed to look and act like the Wireshark +* dumpcap program. +*/ + +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <inttypes.h> +#include <signal.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/queue.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/utsname.h> +#include <time.h> +#include <unistd.h> + +#include <rte_alarm.h> +#include <rte_config.h> +#include <rte_eal.h> +#include <rte_errno.h> +#include <rte_ethdev.h> +#include <rte_lcore.h> +#include <rte_malloc.h> +#include <rte_mbuf.h> +#include <rte_mempool.h> +#include <rte_pcapng.h> +#include <rte_pdump.h> +#include <rte_ring.h> +#include <rte_string_fns.h> +#include <rte_time.h> +#include <rte_version.h> + +#include <pcap/pcap.h> + +#define RING_NAME "capture-ring" +#define MONITOR_INTERVAL (500 * 1000) +#define MBUF_POOL_CACHE_SIZE 32 +#define BURST_SIZE 32 +#define SLEEP_THRESHOLD 1000 + +/* command line flags */ +static const char *progname; +static bool quit_signal; +static bool group_read; +static bool quiet; +static bool promiscuous_mode = true; +static bool use_pcapng = true; +static bool show_statistics; +static char *output_name; +static const char *filter_str; +static unsigned int ring_size =2048 ; +static const char *capture_comment; +static uint32_t snaplen =RTE_MBUF_DEFAULT_BUF_SIZE ; +static bool dump_bpf; +static struct { +double duration; +unsigned long packets; +size_t size; +} stop; + +/* Running state */ +static uint64_t start_time, end_time; +static uint64_t packets_received; +static size_t file_size; + +struct interface { +TAILQ_ENTRY (interface) next; +uint16_t port; +char name[RTE_ETH_NAME_MAX_LEN ]; + +struct rte_rxtx_callback *rx_cb[RTE_MAX_QUEUES_PER_PORT ]; +}; + +TAILQ_HEAD (interface_list, interface); +static struct interface_list interfaces =TAILQ_HEAD_INITIALIZER (interfaces); +static struct interface *port2intf[RTE_MAX_ETHPORTS ]; + +static void usage (void ) +{ +printf ("Usage: %s [options] ...\n\n" , progname); +printf ("Capture Interface:\n" +" -i <interface> name or port index of interface\n" +" -f <capture filter> packet filter in libpcap filter syntax\n" ); +printf (" -s <snaplen>, --snapshot-length <snaplen>\n" +" packet snapshot length (def: %u)\n" , +RTE_MBUF_DEFAULT_BUF_SIZE ); +printf (" -p, --no-promiscuous-mode\n" +" don't capture in promiscuous mode\n" +" -D, --list-interfaces print list of interfaces and exit\n" +" -d print generated BPF code for capture filter\n" +" -S print statistics for each interface once per second\n" +"\n" +"Stop conditions:\n" +" -c <packet count> stop after n packets (def: infinite)\n" +" -a <autostop cond.> ..., --autostop <autostop cond.> ...\n" +" duration:NUM - stop after NUM seconds\n" +" filesize:NUM - stop this file after NUM kB\n" +" packets:NUM - stop after NUM packets\n" +"Output (files):\n" +" -w <filename> name of file to save (def: tempfile)\n" +" -g enable group read access on the output file(s)\n" +" -n use pcapng format instead of pcap (default)\n" +" -P use libpcap format instead of pcapng\n" +" --capture-comment <comment>\n" +" add a capture comment to the output file\n" +"\n" +"Miscellaneous:\n" +" -q don't report packet capture counts\n" +" -v, --version print version information and exit\n" +" -h, --help display this help and exit\n" +"\n" +"Use Ctrl-C to stop capturing at any time.\n" ); +} + +static const char *version (void ) +{ +static char str[128 ]; + +snprintf (str,sizeof (str), +"%s 1.0 (%s)\n" , progname,rte_version ()); +return str; +} + +/* Parse numeric argument from command line */ +static unsigned long get_uint (const char *arg,const char *name, +unsigned int limit) +{ +unsigned long u; +char *endp; + + u =strtoul (arg, &endp,0 ); +if (*arg =='\0' || *endp !='\0' ) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is not a valid number\n" , + name, arg); +if (limit && u > limit) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is too large (greater than %u)\n" , + name, arg, limit); + +return u; +} + +/* Set auto stop values */ +static void auto_stop (char *opt) +{ +char *value, *endp; + + value =strchr (opt,':' ); +if (value ==NULL ) +rte_exit (EXIT_FAILURE , +"Missing colon in auto stop parameter\n" ); + + *value++ ='\0' ; +if (strcmp (opt,"duration" ) ==0 ) { + stop.duration =strtod (value, &endp); +if (*value =='\0' || *endp !='\0' || stop.duration <0 ) +rte_exit (EXIT_FAILURE , +"Invalid duration \"%s\"\n" , value); + }else if (strcmp (opt,"filesize" ) ==0 ) { + stop.size =get_uint (value,"filesize" ,0 ) *1024 ; + }else if (strcmp (opt,"packets" ) ==0 ) { + stop.packets =get_uint (value,"packets" ,0 ); + }else { +rte_exit (EXIT_FAILURE , +"Unknown autostop parameter \"%s\"\n" , opt); + } +} + +/* Add interface to list of interfaces to capture */ +static void add_interface (uint16_t port,const char *name) +{ +struct interface *intf; + + intf =malloc (sizeof (*intf)); +if (!intf) +rte_exit (EXIT_FAILURE ,"no memory for interface\n" ); + +memset (intf,0 ,sizeof (*intf)); +strlcpy (intf->name, name,sizeof (intf->name)); + +printf ("Capturing on '%s'\n" , name); + + port2intf[port] = intf; +TAILQ_INSERT_TAIL (&interfaces, intf, next); +} + +/* Select all valid DPDK interfaces */ +static void select_all_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); + } +} + +/* +* Choose interface to capture if no -i option given. +* Select the first DPDK port, this matches what dumpcap does. +*/ +static void set_default_interface (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); +return ; + } +rte_exit (EXIT_FAILURE ,"No usable interfaces found\n" ); +} + +/* Lookup interface by name or port and add it to the list */ +static void select_interface (const char *arg) +{ +uint16_t port; + +if (strcmp (arg,"*" )) +select_all_interfaces (); +else if (rte_eth_dev_get_port_by_name (arg, &port) ==0 ) +add_interface (port, arg); +else { +char name[RTE_ETH_NAME_MAX_LEN ]; + + port =get_uint (arg,"port_number" ,UINT16_MAX ); +if (rte_eth_dev_get_name_by_port (port, name) <0 ) +rte_exit (EXIT_FAILURE ,"Invalid port number %u\n" , + port); +add_interface (port, name); + } +} + +/* Display list of possible interfaces that can be used. */ +static void show_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +printf ("%u. %s\n" , p, name); + } +} + +static struct rte_bpf *compile_filter (void ) +{ +struct bpf_program fcode; +pcap_t *pcap; + + pcap =pcap_open_dead (DLT_EN10MB , snaplen); +if (!pcap) +rte_exit (EXIT_FAILURE ,"can not open pcap\n" ); + +if (pcap_compile (pcap, &fcode, filter_str, +1 ,PCAP_NETMASK_UNKNOWN ) !=0 ) +rte_exit (EXIT_FAILURE ,"pcap filter string not valid (%s)\n" , +pcap_geterr (pcap)); + +rte_exit (EXIT_FAILURE ,"filter not implemented yet\n" ); + +/* +* Need to convert classic BPF to eBPF and put in shared memory +* be read by primary process. +*/ +pcap_freecode (&fcode); +pcap_close (pcap); + +rte_exit (EXIT_FAILURE ,"not implemented\n" ); +return NULL ; +} + +/* +* Parse command line options. +* These are chosen to be similar to dumpcap command. +*/ +static void parse_opts (int argc,char **argv) +{ +static const struct option long_options[] = { + {"autostop" , required_argument,NULL ,'a' }, + {"capture-comment" , required_argument,NULL ,0 }, + {"help" , no_argument,NULL ,'h' }, + {"interface" , required_argument,NULL ,'i' }, + {"list-interfaces" , no_argument,NULL ,'D' }, + {"no-promiscuous-mode" , no_argument,NULL ,'p' }, + {"output-file" , required_argument,NULL ,'w' }, + {"ring-buffer" , required_argument,NULL ,'b' }, + {"snapshot-length" , required_argument,NULL ,'s' }, + {"version" , no_argument,NULL ,'v' }, + {NULL }, + }; +int option_index, c; + +for (;;) { + c =getopt_long (argc, argv,"a:b:c:dDf:ghi:nN:pPqs:Svw:" , + long_options, &option_index); +if (c ==-1 ) +break ; + +switch (c) { +case 0 : +switch (option_index) { +case 0 : + capture_comment = optarg; +break ; +default : +usage (); +exit (1 ); + } +break ; +case 'a' : +auto_stop (optarg); +break ; +case 'b' : +rte_exit (EXIT_FAILURE , +"multiple files not implemented\n" ); +break ; +case 'c' : + stop.packets =get_uint (optarg,"packet_count" ,0 ); +break ; +case 'd' : + dump_bpf = true; +break ; +case 'D' : +show_interfaces (); +exit (0 ); +case 'f' : + filter_str = optarg; +break ; +case 'g' : + group_read = true; +break ; +case 'h' : +printf ("%s\n\n" ,version ()); +usage (); +exit (0 ); +case 'i' : +select_interface (optarg); +break ; +case 'n' : + use_pcapng = true; +break ; +case 'N' : + ring_size =get_uint (optarg,"packet_limit" ,0 ); +break ; +case 'p' : + promiscuous_mode = false; +break ; +case 'P' : + use_pcapng = false; +break ; +case 'q' : + quiet = true; +break ; +case 's' : + snaplen =get_uint (optarg,"snap_len" ,0 ); +break ; +case 'S' : + show_statistics = true; +break ; +case 'w' : + output_name = optarg; +break ; +case 'v' : +printf ("%s\n" ,version ()); +exit (0 ); +default : +fprintf (stderr,"Invalid option: %s\n" , + argv[optind -1 ]); +usage (); +exit (1 ); + } + } +} + +static void +signal_handler(int sig_num __rte_unused) +{ +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); +} + +/* Return the time since 1/1/1970 in nanoseconds */ +static uint64_t create_timestamp (void ) +{ +struct timespec now; + +clock_gettime (CLOCK_MONOTONIC , &now); +return rte_timespec_to_ns (&now); +} + +/* Return the seconds elapsed since start_time */ +static double elapsed (void ) +{ +struct timespec now; +int64_t delta; + +clock_gettime (CLOCK_MONOTONIC_COARSE , &now); + delta =rte_timespec_to_ns (&now) - start_time; +return delta /1.e9 ; +} + +static void +cleanup_pdump_resources (void ) +{ +struct interface *intf; + + TAILQ_FOREACH(intf, &interfaces, next) { +rte_pdump_disable (intf->port, +RTE_PDUMP_ALL_QUEUES ,RTE_PDUMP_FLAG_RXTX ); +if (promiscuous_mode) +rte_eth_promiscuous_disable (intf->port); + } +} + +/* Alarm signal handler, used to check that primary process */ +static void +monitor_primary(void *arg __rte_unused) +{ +if (__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) +return ; + +if (rte_eal_primary_proc_alive (NULL )) { +rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); + }else { +fprintf (stderr, +"Primary process is no longer active, exiting...\n" ); +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); + } +} + +/* Setup handler to check when primary exits. */ +static void +enable_primary_monitor (void ) +{ +int ret; + +/* Once primary exits, so will pdump. */ + ret =rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to enable monitor:%d\n" , ret); +} + +static void +disable_primary_monitor (void ) +{ +int ret; + + ret =rte_eal_alarm_cancel (monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to disable monitor:%d\n" , ret); +} + +static void +report_packet_stats (rte_pcapng_t *out) +{ +struct rte_pdump_stats pdump_stats; +struct interface *intf; +uint64_t ifrecv, ifdrop; +double percent; + +fputc ('\n' , stderr); + TAILQ_FOREACH(intf, &interfaces, next) { +if (rte_pdump_stats (intf->port, &pdump_stats) <0 ) +continue ; + +/* do what Wiretap does */ + ifrecv = pdump_stats.accepted + pdump_stats.filtered; + ifdrop = pdump_stats.nombuf + pdump_stats.ringfull; + +if (use_pcapng) +rte_pcapng_write_stats (out, intf->port,NULL , + start_time, end_time, + ifrecv, ifdrop); + +if (ifrecv ==0 ) + percent =0 ; +else + percent =100. * ifrecv / (ifrecv + ifdrop); + +fprintf (stderr, +"Packets received/dropped on interface '%s': " +"%" PRIu64"/%" PRIu64" (%.1f)\n" , + intf->name, ifrecv, ifdrop, percent); + } +} + +/* +* Start DPDK EAL with arguments. +* Unlike most DPDK programs, this application does not use the +* typical EAL command line arguments. +* We don't want to expose all the DPDK internals to the user. +*/ +static void dpdk_init (void ) +{ +const char *args[] = { + progname,"--proc-type" ,"secondary" ,"--log-level" ,"error" + }; +const int eal_argc =RTE_DIM (args); +char **eal_argv; +unsigned int i; + +/* DPDK API requires mutable versions of command line arguments. */ + eal_argv =calloc (eal_argc +1 ,sizeof (char *)); +if (eal_argv ==NULL ) +rte_panic ("No memory\n" ); + +for (i =0 ; i <RTE_DIM (args); i++) + eal_argv[i] =strdup (args[i]); + +if (rte_eal_init (eal_argc, eal_argv) <0 ) +rte_panic ("EAL init failed\n" ); + +if (rte_eth_dev_count_avail () ==0 ) +rte_exit (EXIT_FAILURE ,"No Ethernet ports found\n" ); +} + +/* Create packet ring shared between callbacks and process */ +static struct rte_ring *create_ring (void ) +{ +struct rte_ring *ring; +size_t size, log2; + +/* Find next power of 2 >= size. */ + size = ring_size; + log2 =sizeof (size) *8 -__builtin_clzl (size -1 ); + size =1u << log2; + +if (size != ring_size) { +fprintf (stderr,"Ring size %u rounded up to %zu\n" , + ring_size, size); + ring_size = size; + } + + ring =rte_ring_lookup (RING_NAME ); +if (ring ==NULL ) { + ring =rte_ring_create (RING_NAME , ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); + } +return ring; +} + +static struct rte_mempool *create_mempool (void ) +{ +static const char pool_name[] ="capture_mbufs" ; +size_t num_mbufs =2 * ring_size; +struct rte_mempool *mp; + + mp =rte_mempool_lookup (pool_name); +if (mp) +return mp; + + mp =rte_pktmbuf_pool_create_by_ops (pool_name, num_mbufs, +MBUF_POOL_CACHE_SIZE ,0 , +rte_pcapng_mbuf_size (snaplen), +rte_socket_id (),"ring_mp_sc" ); +if (mp ==NULL ) +rte_exit (EXIT_FAILURE , +"Mempool (%s) creation failed: %s\n" , pool_name, +rte_strerror (rte_errno)); + +return mp; +} + +static void *create_output (void ) +{ +struct utsname uts; +char os[256 ]; +int fd; + +/* If no filename specified make a tempfile name */ +if (output_name ==NULL ) { +struct interface *intf; +struct tm *tm; +time_t now; +char ts[32 ]; + + intf =TAILQ_FIRST (&interfaces); + now =time (NULL ); + tm =localtime (&now); +if (!tm) +rte_panic ("localtime failed\n" ); + +strftime (ts,sizeof (ts),"%Y%m%d%H%M%S" , tm); +if (asprintf (&output_name,"/tmp/%s_%u_%s_%s.%s" , + progname, intf->port, intf->name, ts, + use_pcapng ?"pcapng" :"pcap" ) <0 ) +rte_panic ("asprintf failed\n" ); + } + +if (strcmp (output_name,"-" ) ==0 ) + fd =STDOUT_FILENO ; +else { +mode_t mode = group_read ?0640 :0600 ; + + fd =open (output_name,O_WRONLY |O_CREAT , mode); +if (fd <0 ) +rte_exit (EXIT_FAILURE ,"Can not open \"%s\": %s\n" , + output_name,strerror (errno)); + } + +if (use_pcapng) { +rte_pcapng_t *pcapng; + +if (uname (&uts) <0 ) +strcpy (os,"unknown" ); +else +snprintf (os,sizeof (os),"%s %s" , + uts.sysname, uts.release); + + pcapng =rte_pcapng_fdopen (fd, os,NULL ,version (), capture_comment); +if (pcapng ==NULL ) +rte_exit (EXIT_FAILURE ,"pcapng_fdopen failed: %s\n" , +strerror (rte_errno)); +return pcapng; + }else { +pcap_dumper_t *dumper; +pcap_t *pcap; + + pcap =pcap_open_dead_with_tstamp_precision (DLT_EN10MB , snaplen, +PCAP_TSTAMP_PRECISION_NANO ); +if (pcap ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_open_dead failed\n" ); + + dumper =pcap_dump_fopen (pcap,fdopen (fd,"w" )); +if (dumper ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_dump_fopen failed: %s\n" , +pcap_geterr (pcap)); +return dumper; + } +} + +static void enable_pdump (struct rte_ring *r,struct rte_mempool *mp, +struct rte_bpf *filter) +{ +struct interface *intf; +uint32_t flags; +int ret; + + flags =RTE_PDUMP_FLAG_RXTX ; +if (use_pcapng) + flags |=RTE_PDUMP_FLAG_PCAPNG ; + + TAILQ_FOREACH(intf, &interfaces, next) { +if (promiscuous_mode) +rte_eth_promiscuous_enable (intf->port); + + ret =rte_pdump_enable_bpf (intf->port,RTE_PDUMP_ALL_QUEUES , + flags, snaplen, + r, mp, filter); +if (ret <0 ) +rte_exit (EXIT_FAILURE , +"Packet dump enable failed: %s\n" , +rte_strerror (rte_errno)); + } +} + +/* +* Show current count of captured packets +* with backspaces to overwrite last value. +*/ +static void show_count (uint64_t count) +{ +unsigned int i; +static unsigned int bt; + +for (i =0 ; i < bt; i++) +fputc ('\b' , stderr); + + bt =fprintf (stderr,"%" PRIu64" " , count); +} + +/* Write multiple packets in older pcap format */ +static ssize_t +pcap_write_packets (pcap_dumper_t *dumper, +struct rte_mbuf *pkts[],uint16_t n) +{ +uint8_t temp_data[snaplen]; +struct pcap_pkthdr header; +uint16_t i; +size_t total =0 ; + +gettimeofday (&header.ts,NULL ); + +for (i =0 ; i < n; i++) { +struct rte_mbuf *m = pkts[i]; + + header.len =rte_pktmbuf_pkt_len (m); + header.caplen =RTE_MIN (header.len, snaplen); + +pcap_dump ((u_char *)dumper, &header, +rte_pktmbuf_read (m,0 , header.caplen, temp_data)); + + total +=sizeof (header) + header.len; + } + +return total; +} + +/* Process all packets in ring and dump to capture file */ +static int process_ring (void *out,struct rte_ring *r) +{ +struct rte_mbuf *pkts[BURST_SIZE ]; +unsigned int avail, n; +static unsigned int empty_count; +ssize_t written; + + n =rte_ring_sc_dequeue_burst (r, (void **) pkts,BURST_SIZE , + &avail); +if (n ==0 ) { +/* don't consume endless amounts of cpu if idle */ +if (empty_count <SLEEP_THRESHOLD ) + ++empty_count; +else +usleep (10 ); +return 0 ; + } + + empty_count = (avail ==0 ); + +if (use_pcapng) + written =rte_pcapng_write_packets (out, pkts, n); +else + written =pcap_write_packets (out, pkts, n); + +rte_pktmbuf_free_bulk (pkts, n); + +if (written <0 ) +return -1 ; + + file_size += written; + packets_received += n; +if (!quiet) +show_count (packets_received); + +return 0 ; +} + +int main (int argc,char **argv) +{ +struct rte_bpf *bpf_filter =NULL ; +struct rte_ring *r; +struct rte_mempool *mp; +void *out; + + progname =basename (argv[0 ]); + +dpdk_init (); +parse_opts (argc, argv); + +if (filter_str) + bpf_filter =compile_filter (); + +if (dump_bpf) +fprintf (stderr,"dump filter not implemented yet\n" ); + +if (TAILQ_EMPTY (&interfaces)) +set_default_interface (); + + r =create_ring (); + mp =create_mempool (); + out =create_output (); +if (out ==NULL ) +rte_exit (EXIT_FAILURE ,"can not open output file: %s\n" , +rte_strerror (rte_errno)); + + start_time =create_timestamp (); +enable_pdump (r, mp, bpf_filter); + +signal (SIGINT , signal_handler); +signal (SIGPIPE ,SIG_IGN ); + +enable_primary_monitor (); + +if (!quiet) { +fprintf (stderr,"Packets captured: " ); +show_count (0 ); + } + +while (!__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) { +if (process_ring (out, r) <0 ) { +fprintf (stderr,"pcapng file write failed; %s\n" , +strerror (errno)); +break ; + } + +if (stop.size && file_size >= stop.size) +break ; + +if (stop.packets && packets_received >= stop.packets) +break ; + +if (stop.duration !=0 &&elapsed () > stop.duration) +break ; + } + + end_time =create_timestamp (); +disable_primary_monitor (); + +if (rte_eal_primary_proc_alive (NULL )) +report_packet_stats (out); + +if (use_pcapng) +rte_pcapng_close (out); +else +pcap_dump_close (out); + +cleanup_pdump_resources (); +rte_free (bpf_filter); +rte_ring_free (r); +rte_mempool_free (mp); + +return rte_eal_cleanup () ?EXIT_FAILURE :0 ; +}
app/dumpcap/meson.build new file +18 -0
@@ -0,0 +1,18 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +if not dpdk_conf.has('RTE_PORT_PCAP') + build = false + reason = 'missing dependency, "libpcap"' +endif + +if is_windows + build = false + reason = 'not supported on Windows' + subdir_done() +endif + +ext_deps += pcap_dep + +sources = files('main.c') +deps += ['ethdev', 'pdump', 'pcapng']
app/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2017-2019 Intel Corporation apps = [ + 'dumpcap', 'pdump', 'proc-info', 'test-acl',
Signature · 1 line
2.30.2
PATCH v2 4/5 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 80 ++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 222 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -62,6 +62,16 @@ New Features * Added bus-level parsing of the devargs syntax. * Kept compatibility with the legacy syntax as parsing fallback. +* **Enhance Packet capture.** + + * New dpdk-dumpcap program that has most of the features of the + wireshark dumpcap utility including capture of multiple interfaces, + stopping after number of bytes, packets. + * New library for writing pcapng packet capture files. + * Enhancement to the pdump library to support: + * Packet filter with BPF. + * Pcapng format with timestamps and meta-data. + * Fixes packet capture with stripped VLAN tags. Removed Items -------------
doc/guides/tools/dumpcap.rst new file +80 -0
80 lines not on this page · raw message
doc/guides/tools/index.rst +1 -0
@@ -8,6 +8,7 @@ DPDK Tools User Guides :maxdepth: 2 :numbered: + dumpcap proc_info pdump pmdinfo
Signature · 1 line
2.30.2
PATCH v2 5/5 MAINTAINERS: add entry for new pcapng and dumper
In reply to Stephen HemmingerClaim responsibility for the new code.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -1429,6 +1429,12 @@ F: app/test/test_pdump.* F: app/pdump/ F: doc/guides/tools/pdump.rst +Packet dump +M: Stephen Hemminger <[email protected]> +F: lib/pcapng/ +F: doc/guides/prog_guide/pcapng_lib.rst +F: app/dumpcap/ +F: doc/guides/tools/dumpcap.rst Packet Framework ----------------
Signature · 1 line
2.30.2
v3 cover Packet capture framework enhancements
In reply to Stephen HemmingerThis patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
It preserves the old pdump command as is for those people who never want to change.
v3 changes:
- introduce packet filters using classic BPF to eBPF converter required small fix to DPDK BPF interpreter
- introduce function to decode eBPF instructions
- add option to dumpcap to show both classic BPF and eBPF result
- drop some un-useful stubs
- minor checkpatch warning cleanup
Stephen Hemminger (8):
librte_pcapng: add new library for writing pcapng files
bpf: allow self-xor operation
bpf: add function to convert classic BPF to DPDK BPF
bpf: add function to dump eBPF instructions
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new pcapng and dumper
MAINTAINERS | 6 + app/dumpcap/main.c | 832 ++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +- doc/guides/howto/packet_capture_framework.rst | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 580 ++++++++++++ lib/bpf/bpf_dump.c | 118 +++ lib/bpf/bpf_validate.c | 8 +- lib/bpf/meson.build | 6 + lib/bpf/rte_bpf.h | 39 + lib/bpf/version.map | 7 + lib/meson.build | 5 +- lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 437 ++++++--- lib/pdump/rte_pdump.h | 110 ++- lib/pdump/version.map | 8 + 30 files changed, 3192 insertions(+), 215 deletions(-) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst create mode 100644 lib/bpf/bpf_convert.c create mode 100644 lib/bpf/bpf_dump.c create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
Signature · 1 line
2.30.2
PATCH v3 1/8 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 194 +++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 918 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +574 -0
574 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +194 -0
194 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH v3 2/8 bpf: allow self-xor operation
In reply to Stephen HemmingerWhen doing BPF filter program conversion, a common way to zero a register in single instruction is:
xor r7,r7
The BPF validator would not allow this because the value of r7 was undefined. But after this operation it always zero so allow it as a special case.
--- lib/bpf/bpf_validate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
lib/bpf/bpf_validate.c +6 -2
@@ -661,8 +661,12 @@ eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins) op =BPF_OP (ins->code); - err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , - (op !=BPF_NEG ) ? &rs :NULL ); +/* Allow self-xor as way to zero register */ +if (op ==BPF_XOR && ins->src_reg == ins->dst_reg) + err =NULL ; +else + err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , + (op !=BPF_NEG ) ? &rs :NULL );if (err !=NULL )return err;
Signature · 1 line
2.30.2
PATCH v3 3/8 bpf: add function to convert classic BPF to DPDK BPF
In reply to Stephen HemmingerThe pcap library emits classic BPF (32 bit) and is useful for creating filter programs. The DPDK BPF library only implements extended BPF (eBPF). Add an function to convert from old to new.
The rte_bpf_convert function uses rte_malloc to put the resulting program in hugepage shared memory so it can be passed from a secondary process to a primary process.
The code to convert was originally done as part of the Linux kernel implementation then converted to a userspace program. Both authors have agreed that it is allowable to license this as BSD licensed in DPDK.
--- lib/bpf/bpf_convert.c | 580 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 616 insertions(+) create mode 100644 lib/bpf/bpf_convert.c
lib/bpf/bpf_convert.c new file +580 -0
@@ -0,0 +1,580 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2021 Microsoft Corporation +* +* Based on bpf_convert_filter() in the Linux kernel sources +* and filter2xdp. +* +* Licensed as BSD with permission original authors. +* Copyright (C) 2017 Tobias Klauser +* Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com +*/ + +#include <assert.h> +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <rte_common.h> +#include <rte_bpf.h> +#include <rte_log.h> +#include <rte_malloc.h> +#include <rte_errno.h> + +/* Workaround name conflicts with libpcap */ +#define bpf_validate (f, len) bpf_validate_libpcap(f, len) +#include <pcap/pcap.h> +#include <pcap/bpf.h> +#undef bpf_validate + +#include "bpf_impl.h" +#include "bpf_def.h" + +#ifndef BPF_MAXINSNS +#define BPF_MAXINSNS 4096 +#endif + +/* +* Linux socket filter uses negative absolute offsets to +* reference ancillary data. +* +*/ +#define SKF_AD_OFF (-0x1000) +#define SKF_AD_PROTOCOL 0 +#define SKF_AD_PKTTYPE 4 +#define SKF_AD_IFINDEX 8 +#define SKF_AD_NLATTR 12 +#define SKF_AD_NLATTR_NEST 16 +#define SKF_AD_MARK 20 +#define SKF_AD_QUEUE 24 +#define SKF_AD_HATYPE 28 +#define SKF_AD_RXHASH 32 +#define SKF_AD_CPU 36 +#define SKF_AD_ALU_XOR_X 40 +#define SKF_AD_VLAN_TAG 44 +#define SKF_AD_VLAN_TAG_PRESENT 48 +#define SKF_AD_PAY_OFFSET 52 +#define SKF_AD_RANDOM 56 +#define SKF_AD_VLAN_TPID 60 +#define SKF_AD_MAX 64 + +/* ArgX, context and stack frame pointer register positions. Note, +* Arg1, Arg2, Arg3, etc are used as argument mappings of function +* calls in BPF_CALL instruction. +*/ +#define BPF_REG_ARG1 EBPF_REG_1 +#define BPF_REG_ARG2 EBPF_REG_2 +#define BPF_REG_ARG3 EBPF_REG_3 +#define BPF_REG_ARG4 EBPF_REG_4 +#define BPF_REG_ARG5 EBPF_REG_5 +#define BPF_REG_CTX EBPF_REG_6 +#define BPF_REG_FP EBPF_REG_10 + +/* Additional register mappings for converted user programs. */ +#define BPF_REG_A EBPF_REG_0 +#define BPF_REG_X EBPF_REG_7 +#define BPF_REG_TMP EBPF_REG_8 + +/* Helper macros for filter block array initializers. */ + +/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ + +#define EBPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_ALU32_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ + +#define BPF_ALU32_IMM (OP ,DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov, dst_reg = src_reg */ + +#define BPF_MOV64_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_MOV32_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* Short form of mov, dst_reg = imm32 */ + +#define BPF_MOV32_IMM (DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */ + +#define BPF_MOV32_RAW (TYPE ,DST ,SRC ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_SRC(TYPE), \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = IMM }) + +/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ + +#define BPF_LD_ABS (SIZE ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Memory load, dst_reg = *(uint *) (src_reg + off16) */ + +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Memory store, *(uint *) (dst_reg + off16) = src_reg */ + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ + +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +/* Raw code statement block */ + +#define BPF_RAW_INSN (CODE ,DST ,SRC ,OFF ,IMM ) \ + ((struct ebpf_insn) { \ + .code = CODE, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = IMM }) + +/* Program exit */ + +#define BPF_EXIT_INSN () \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | EBPF_EXIT, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = 0 }) + +/* +* Placeholder to convert BPF extensions like length and VLAN tag +* If and when DPDK BPF supports them. +*/ +static bool convert_bpf_load (const struct bpf_insn *fp, + struct ebpf_insn **new_insnp __rte_unused) +{ +switch (fp->k) { +case SKF_AD_OFF +SKF_AD_PROTOCOL : +case SKF_AD_OFF +SKF_AD_PKTTYPE : +case SKF_AD_OFF +SKF_AD_IFINDEX : +case SKF_AD_OFF +SKF_AD_HATYPE : +case SKF_AD_OFF +SKF_AD_MARK : +case SKF_AD_OFF +SKF_AD_RXHASH : +case SKF_AD_OFF +SKF_AD_QUEUE : +case SKF_AD_OFF +SKF_AD_VLAN_TAG : +case SKF_AD_OFF +SKF_AD_VLAN_TAG_PRESENT : +case SKF_AD_OFF +SKF_AD_VLAN_TPID : +case SKF_AD_OFF +SKF_AD_PAY_OFFSET : +case SKF_AD_OFF +SKF_AD_NLATTR : +case SKF_AD_OFF +SKF_AD_NLATTR_NEST : +case SKF_AD_OFF +SKF_AD_CPU : +case SKF_AD_OFF +SKF_AD_RANDOM : +case SKF_AD_OFF +SKF_AD_ALU_XOR_X : +/* TODO: Not implemented yet */ +RTE_BPF_LOG (ERR ,"BPF extension LOAD ABS %u not supported\n" , + fp->k); +return true; +default : +return false; + } +} + +static int bpf_convert_filter (const struct bpf_insn *prog,size_t len, +struct ebpf_insn *new_prog,uint32_t *new_len) +{ +unsigned int pass =0 ; +size_t new_flen =0 , target, i; +struct ebpf_insn *new_insn; +const struct bpf_insn *fp; +int *addrs =NULL ; +uint8_t bpf_src; + +if (len >BPF_MAXINSNS ) { +RTE_BPF_LOG (ERR ,"%s: cBPF program too long (%zu insns)\n" , + __func__, len); +return -EINVAL ; + } + +/* On second pass, allocate the new program */ +if (new_prog) { + addrs =calloc (len,sizeof (*addrs)); +if (addrs ==NULL ) +return -ENOMEM ; + } + +do_pass: + new_insn = new_prog; + fp = prog; + +/* Classic BPF related prologue emission. */ +if (new_insn) { +/* Classic BPF expects A and X to be reset first. These need +* to be guaranteed to be the first two instructions. +*/ + *new_insn++ =EBPF_ALU64_REG (BPF_XOR ,BPF_REG_A ,BPF_REG_A ); + *new_insn++ =EBPF_ALU64_REG (BPF_XOR ,BPF_REG_X ,BPF_REG_X ); + +/* All programs must keep CTX in callee saved BPF_REG_CTX. +* In eBPF case it's done by the compiler, here we need to +* do this ourself. Initial CTX is present in BPF_REG_ARG1. +*/ + *new_insn++ =BPF_MOV64_REG (BPF_REG_CTX ,BPF_REG_ARG1 ); + }else { + new_insn +=3 ; + } + +for (i =0 ; i < len; fp++, i++) { +struct ebpf_insn tmp_insns[6 ] = { }; +struct ebpf_insn *insn = tmp_insns; + +if (addrs) + addrs[i] = new_insn - new_prog; + +switch (fp->code) { +/* Absolute loads are how classic BPF accesses skb */ +case BPF_LD |BPF_ABS |BPF_W : +case BPF_LD |BPF_ABS |BPF_H : +case BPF_LD |BPF_ABS |BPF_B : +if (convert_bpf_load (fp, &insn)) + goto err; + + *insn =BPF_RAW_INSN (fp->code,0 ,0 ,0 , fp->k); +break ; + +case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_X : +/* For cBPF, don't cause floating point exception */ + *insn++ =BPF_MOV32_REG (BPF_REG_X ,BPF_REG_X ); + *insn++ =BPF_JMP_IMM (EBPF_JNE ,BPF_REG_X ,0 ,2 ); + *insn++ =BPF_ALU32_REG (BPF_XOR ,BPF_REG_A ,BPF_REG_A ); + *insn++ =BPF_EXIT_INSN (); +/* fallthrough */ +case BPF_ALU |BPF_ADD |BPF_X : +case BPF_ALU |BPF_ADD |BPF_K : +case BPF_ALU |BPF_SUB |BPF_X : +case BPF_ALU |BPF_SUB |BPF_K : +case BPF_ALU |BPF_AND |BPF_X : +case BPF_ALU |BPF_AND |BPF_K : +case BPF_ALU |BPF_OR |BPF_X : +case BPF_ALU |BPF_OR |BPF_K : +case BPF_ALU |BPF_LSH |BPF_X : +case BPF_ALU |BPF_LSH |BPF_K : +case BPF_ALU |BPF_RSH |BPF_X : +case BPF_ALU |BPF_RSH |BPF_K : +case BPF_ALU |BPF_XOR |BPF_X : +case BPF_ALU |BPF_XOR |BPF_K : +case BPF_ALU |BPF_MUL |BPF_X : +case BPF_ALU |BPF_MUL |BPF_K : +case BPF_ALU |BPF_DIV |BPF_K : +case BPF_ALU |BPF_MOD |BPF_K : +case BPF_ALU |BPF_NEG : +case BPF_LD |BPF_IND |BPF_W : +case BPF_LD |BPF_IND |BPF_H : +case BPF_LD |BPF_IND |BPF_B : +/* All arithmetic insns map as-is. */ + *insn =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); +break ; + +/* Jump transformation cannot use BPF block macros +* everywhere as offset calculation and target updates +* require a bit more work than the rest, i.e. jump +* opcodes map as-is, but offsets need adjustment. +*/ + +#define BPF_EMIT_JMP \ + do { \ + if (target >= len) \ + goto err; \ + insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ +/* Adjust pc relative offset for 2nd or 3rd insn. */ \ + insn->off -= insn - tmp_insns; \ + }while (0 ) + +case BPF_JMP |BPF_JA : + target = i + fp->k +1 ; + insn->code = fp->code; +BPF_EMIT_JMP ; +break ; + +case BPF_JMP |BPF_JEQ |BPF_K : +case BPF_JMP |BPF_JEQ |BPF_X : +case BPF_JMP |BPF_JSET |BPF_K : +case BPF_JMP |BPF_JSET |BPF_X : +case BPF_JMP |BPF_JGT |BPF_K : +case BPF_JMP |BPF_JGT |BPF_X : +case BPF_JMP |BPF_JGE |BPF_K : +case BPF_JMP |BPF_JGE |BPF_X : +if (BPF_SRC (fp->code) ==BPF_K && (int ) fp->k <0 ) { +/* BPF immediates are signed, zero extend +* immediate into tmp register and use it +* in compare insn. +*/ + *insn++ =BPF_MOV32_IMM (BPF_REG_TMP , fp->k); + + insn->dst_reg =BPF_REG_A ; + insn->src_reg =BPF_REG_TMP ; + bpf_src =BPF_X ; + }else { + insn->dst_reg =BPF_REG_A ; + insn->imm = fp->k; + bpf_src =BPF_SRC (fp->code); + insn->src_reg = bpf_src ==BPF_X ?BPF_REG_X :0 ; + } + +/* Common case where 'jump_false' is next insn. */ +if (fp->jf ==0 ) { + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; + target = i + fp->jt +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Convert JEQ into JNE when 'jump_true' is next insn. */ +if (fp->jt ==0 &&BPF_OP (fp->code) ==BPF_JEQ ) { + insn->code =BPF_JMP |EBPF_JNE | bpf_src; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Other jumps are mapped into two insns: Jxx and JA. */ + target = i + fp->jt +1 ; + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; +BPF_EMIT_JMP ; + insn++; + + insn->code =BPF_JMP |BPF_JA ; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + +/* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ +case BPF_LDX |BPF_MSH |BPF_B : +/* tmp = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_TMP ,BPF_REG_A ); +/* A = BPF_R0 = *(u8 *) (skb->data + K) */ + *insn++ =BPF_LD_ABS (BPF_B , fp->k); +/* A &= 0xf */ + *insn++ =BPF_ALU32_IMM (BPF_AND ,BPF_REG_A ,0xf ); +/* A <<= 2 */ + *insn++ =BPF_ALU32_IMM (BPF_LSH ,BPF_REG_A ,2 ); +/* X = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +/* A = tmp */ + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_TMP ); +break ; + +/* RET_K is remaped into 2 insns. RET_A case doesn't need an +* extra mov as EBPF_REG_0 is already mapped into BPF_REG_A. +*/ +case BPF_RET |BPF_A : +case BPF_RET |BPF_K : +if (BPF_RVAL (fp->code) ==BPF_K ) { + *insn++ =BPF_MOV32_RAW (BPF_K ,EBPF_REG_0 , +0 , fp->k); + } + *insn =BPF_EXIT_INSN (); +break ; + +/* Store to stack. */ +case BPF_ST : +case BPF_STX : + *insn =BPF_STX_MEM (BPF_W ,BPF_REG_FP ,BPF_CLASS (fp->code) == +BPF_ST ?BPF_REG_A :BPF_REG_X , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* Load from stack. */ +case BPF_LD |BPF_MEM : +case BPF_LDX |BPF_MEM : + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_FP , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* A = K or X = K */ +case BPF_LD |BPF_IMM : +case BPF_LDX |BPF_IMM : + *insn =BPF_MOV32_IMM (BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X , fp->k); +break ; + +/* X = A */ +case BPF_MISC |BPF_TAX : + *insn =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +break ; + +/* A = X */ +case BPF_MISC |BPF_TXA : + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_X ); +break ; + +/* A = skb->len or X = skb->len */ +case BPF_LD |BPF_W |BPF_LEN : +case BPF_LDX |BPF_W |BPF_LEN : +#if 0 + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_CTX , + offsetof(struct sk_buff , len)); +#endif +RTE_BPF_LOG (ERR ,"%s: length not implemented\n" , __func__); + goto err; + +/* Access seccomp_data fields. */ +case BPF_LDX |BPF_ABS |BPF_W : +#if 0 +/* A = *(u32 *) (ctx + K) */ + *insn =BPF_LDX_MEM (BPF_W ,BPF_REG_A ,BPF_REG_CTX , fp->k); +#endif +RTE_BPF_LOG (ERR ,"%s: data fields implemented\n" , __func__); + goto err; + +/* Unknown instruction. */ +default : +RTE_BPF_LOG (ERR ,"%s: Unknown instruction!: %#x\n" , + __func__, fp->code); + goto err; + } + + insn++; +if (new_prog) +memcpy (new_insn, tmp_insns, +sizeof (*insn) * (insn - tmp_insns)); + new_insn += insn - tmp_insns; + } + +if (!new_prog) { +/* Only calculating new length. */ + *new_len = new_insn - new_prog; +return 0 ; + } + + pass++; +if ((ptrdiff_t )new_flen != new_insn - new_prog) { + new_flen = new_insn - new_prog; +if (pass >2 ) + goto err; + goto do_pass; + } + +free (addrs); +assert (*new_len == new_flen); + +return 0 ; +err: +free (addrs); +return -1 ; +} + +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog) +{ +struct rte_bpf_prm *prm =NULL ; +struct ebpf_insn *ebpf =NULL ; +uint32_t ebpf_len =0 ; +int ret; + +if (prog ==NULL ) { +RTE_BPF_LOG (ERR ,"%s: NULL program\n" , __func__); + rte_errno =EINVAL ; +return NULL ; + } + +/* 1st pass: calculate the eBPF program length */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len,NULL , &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot get eBPF length\n" , __func__); + rte_errno = -ret; +return NULL ; + } + +RTE_BPF_LOG (DEBUG ,"%s: prog len cBPF=%u -> eBPF=%u\n" , + __func__, prog->bf_len, ebpf_len); + + prm =rte_zmalloc ("bpf_filter" , +sizeof (*prm) + ebpf_len *sizeof (*ebpf),0 ); +if (prm ==NULL ) { + rte_errno =ENOMEM ; +return NULL ; + } + +/* The EPBF instructions in this case are right after the header */ + ebpf = (void *)(prm +1 ); + +/* 2nd pass: remap cBPF to eBPF instructions */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot convert cBPF to eBPF\n" , __func__); +free (prm); + rte_errno = -ret; +return NULL ; + } + + prm->ins = ebpf; + prm->nb_ins = ebpf_len; + +/* Classic BPF programs use mbufs */ + prm->prog_arg.type =RTE_BPF_ARG_PTR_MBUF ; + prm->prog_arg.size =sizeof (struct rte_mbuf ); + +return prm; +}
lib/bpf/meson.build +5 -0
@@ -25,3 +25,8 @@ if dep.found() sources += files('bpf_load_elf.c') ext_deps += dep endif + +if dpdk_conf.has('RTE_PORT_PCAP') + sources += files('bpf_convert.c') + ext_deps += pcap_dep +endif
lib/bpf/rte_bpf.h +25 -0
@@ -198,6 +198,31 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +#ifdef RTE_PORT_PCAP + +struct bpf_program ; + +/** +* Convert a Classic BPF program from libpcap into a DPDK BPF code. +* +* @param prog +* Classic BPF program from pcap_compile(). +* @param prm +* Result Extended BPF program. +* @return +* Pointer to BPF program (allocated with *rte_malloc*) +* that is used in future BPF operations, +* or NULL on error, with error code set in rte_errno. +* Possible rte_errno errors include: +* - EINVAL - invalid parameter passed to function +* - ENOMEM - can't reserve enough memory +*/ +__rte_experimental +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog); + +#endif + #ifdef __cplusplus }#endif
lib/bpf/version.map +6 -0
@@ -14,3 +14,9 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_bpf_convert; +};
Signature · 1 line
2.30.2
PATCH v3 4/8 bpf: add function to dump eBPF instructions
In reply to Stephen HemmingerWhen debugging converted (and other) programs it is useful to see disassembled eBPF output.
--- lib/bpf/bpf_dump.c | 118 ++++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 ++++++ lib/bpf/version.map | 1 + 4 files changed, 134 insertions(+) create mode 100644 lib/bpf/bpf_dump.c
lib/bpf/bpf_dump.c new file +118 -0
@@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright (c) 2021 Stephen Hemminger +* Based on filter2xdp +* Copyright (C) 2017 Tobias Klauser +*/ + +#include <stdio.h> +#include <stdint.h> + +#include "rte_bpf.h" + +#define BPF_OP_INDEX (x) (BPF_OP(x) >> 4) +#define BPF_SIZE_INDEX (x) (BPF_SIZE(x) >> 3) + +static const char *const class_tbl[] = { + [BPF_LD ] ="ld" , [BPF_LDX ] ="ldx" , [BPF_ST ] ="st" , + [BPF_STX ] ="stx" , [BPF_ALU ] ="alu" , [BPF_JMP ] ="jmp" , + [BPF_RET ] ="ret" , [BPF_MISC ] ="alu64" , +}; + +static const char *const alu_op_tbl[16 ] = { + [BPF_ADD >>4 ] ="add" , [BPF_SUB >>4 ] ="sub" , + [BPF_MUL >>4 ] ="mul" , [BPF_DIV >>4 ] ="div" , + [BPF_OR >>4 ] ="or" , [BPF_AND >>4 ] ="and" , + [BPF_LSH >>4 ] ="lsh" , [BPF_RSH >>4 ] ="rsh" , + [BPF_NEG >>4 ] ="neg" , [BPF_MOD >>4 ] ="mod" , + [BPF_XOR >>4 ] ="xor" , [EBPF_MOV >>4 ] ="mov" , + [EBPF_ARSH >>4 ] ="arsh" , [EBPF_END >>4 ] ="endian" , +}; + +static const char *const size_tbl[] = { + [BPF_W >>3 ] ="w" , + [BPF_H >>3 ] ="h" , + [BPF_B >>3 ] ="b" , + [EBPF_DW >>3 ] ="dw" , +}; + +static const char *const jump_tbl[16 ] = { + [BPF_JA >>4 ] ="ja" , [BPF_JEQ >>4 ] ="jeq" , + [BPF_JGT >>4 ] ="jgt" , [BPF_JGE >>4 ] ="jge" , + [BPF_JSET >>4 ] ="jset" , [EBPF_JNE >>4 ] ="jne" , + [EBPF_JSGT >>4 ] ="jsgt" , [EBPF_JSGE >>4 ] ="jsge" , + [EBPF_CALL >>4 ] ="call" , [EBPF_EXIT >>4 ] ="exit" , +}; + +static void ebpf_dump (FILE *f,const struct ebpf_insn insn,size_t n) +{ +const char *op, *postfix ="" ; +uint8_t cls =BPF_CLASS (insn.code); + +fprintf (f," L%zu:\t" , n); + +switch (cls) { +default : +fprintf (f,"unimp 0x%x // class: %s\n" , insn.code, + class_tbl[cls]); +break ; +case BPF_ALU : + postfix ="32" ; +/* fall through */ +case EBPF_ALU64 : + op = alu_op_tbl[BPF_OP_INDEX (insn.code)]; +if (BPF_SRC (insn.code) ==BPF_X ) +fprintf (f,"%s%s r%u, r%u\n" , op, postfix, insn.dst_reg, + insn.src_reg); +else +fprintf (f,"%s%s r%u, #0x%x\n" , op, postfix, + insn.dst_reg, insn.imm); +break ; +case BPF_LD : + op ="ld" ; + postfix = size_tbl[BPF_SIZE_INDEX (insn.code)]; +if (BPF_MODE (insn.code) ==BPF_IMM ) +fprintf (f,"%s%s r%d, #0x%x\n" , op, postfix, + insn.dst_reg, insn.imm); +else if (BPF_MODE (insn.code) ==BPF_ABS ) +fprintf (f,"%s%s r%d, [%d]\n" , op, postfix, + insn.dst_reg, insn.imm); +else if (BPF_MODE (insn.code) ==BPF_IND ) +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, + insn.dst_reg, insn.src_reg, insn.imm); +else +fprintf (f,"// BUG: LD opcode 0x%02x in eBPF insns\n" , + insn.code); +break ; +case BPF_LDX : + op ="ldx" ; + postfix = size_tbl[BPF_SIZE_INDEX (insn.code)]; +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, insn.dst_reg, + insn.src_reg, insn.off); +break ; +#define L (pc, off) ((int)(pc) + 1 + (off)) +case BPF_JMP : + op = jump_tbl[BPF_OP_INDEX (insn.code)]; +if (op ==NULL ) +fprintf (f,"invalid jump opcode: %#x\n" , insn.code); +else if (BPF_OP (insn.code) ==BPF_JA ) +fprintf (f,"%s L%d\n" , op,L (n, insn.off)); +else if (BPF_OP (insn.code) ==EBPF_EXIT ) +fprintf (f,"%s\n" , op); +else +fprintf (f,"%s r%u, #0x%x, L%d\n" , op, insn.dst_reg, + insn.imm,L (n, insn.off)); +break ; +case BPF_RET : +fprintf (f,"// BUG: RET opcode 0x%02x in eBPF insns\n" , + insn.code); +break ; + } +} + +void rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len) +{ +uint32_t i; + +for (i =0 ; i < len; ++i) +ebpf_dump (f, buf[i], i); +}
lib/bpf/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2018 Intel Corporation sources = files('bpf.c', + 'bpf_dump.c', 'bpf_exec.c', 'bpf_load.c', 'bpf_pkt.c',
lib/bpf/rte_bpf.h +14 -0
@@ -198,6 +198,20 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +/** +* Dump epf instructions to a file. +* +* @param f +* A pointer to a file for output +* @param buf +* A pointer to BPF instructions +* @param len +* Number of BPF instructions to dump. +*/ +__rte_experimental +void +rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len); +#ifdef RTE_PORT_PCAP struct bpf_program ;
lib/bpf/version.map +1 -0
@@ -19,4 +19,5 @@ EXPERIMENTAL { global: rte_bpf_convert; + rte_bpf_dump; };
Signature · 1 line
2.30.2
PATCH v3 5/8 pdump: support pcapng and filtering
In reply to Stephen HemmingerThis enhances the DPDK pdump library to support new pcapng format and filtering via BPF.
The internal client/server protocol is changed to support two versions: the original pdump basic version and a new pcapng version.
The internal version number (not part of exposed API or ABI) is intentionally increased to cause any attempt to try mismatched primary/secondary process to fail.
Add new API to do allow filtering of captured packets with DPDK BPF (eBPF) filter program. It keeps statistics
on packets captured, filtered, and missed (because ring was full).
--- lib/meson.build | 4 +- lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 437 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 110 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 435 insertions(+), 126 deletions(-)
lib/meson.build +2 -2
@@ -26,6 +26,7 @@ libraries = [ 'timer', # eventdev depends on this 'acl', 'bbdev', + 'bpf', 'bitratestats', 'cfgfile', 'compressdev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'rib',
@@ -55,10 +55,10 @@ libraries = [ 'ipsec', # ipsec lib depends on net, crypto and security 'fib', #fib lib depends on rib 'port', # pkt framework libs which use other libs from above + 'pdump', # pdump lib depends on bpf pcapng 'table', 'pipeline', 'flow_classify', # flow_classify lib depends on pkt framework table lib - 'bpf', 'graph', 'node', ]
lib/pdump/meson.build +1 -1
@@ -3,4 +3,4 @@ sources = files('rte_pdump.c') headers = files('rte_pdump.h') -deps += ['ethdev'] +deps += ['ethdev', 'bpf', 'pcapng']
lib/pdump/rte_pdump.c +317 -120
644 lines not on this page · raw message
lib/pdump/rte_pdump.h +107 -3
152 lines not on this page · raw message
lib/pdump/version.map +8 -0
@@ -10,3 +10,11 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_pdump_enable_bpf; + rte_pdump_enable_bpf_by_deviceid; + rte_pdump_stats; +};
Signature · 1 line
2.30.2
PATCH v3 6/8 app/dumpcap: add new packet capture application
In reply to Stephen HemmingerThis is a new packet capture application to replace existing pdump. The new application works like Wireshark dumpcap program and supports the pdump API features.
It is not complete yet some features such as filtering are not implemented.
--- app/dumpcap/main.c | 832 ++++++++++++++++++++++++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + 3 files changed, 849 insertions(+) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build
app/dumpcap/main.c new file +832 -0
@@ -0,0 +1,832 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019-2020 Microsoft Corporation +* +* DPDK application to dump network traffic +* This is designed to look and act like the Wireshark +* dumpcap program. +*/ + +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <inttypes.h> +#include <signal.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/queue.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/utsname.h> +#include <time.h> +#include <unistd.h> + +#include <rte_alarm.h> +#include <rte_config.h> +#include <rte_eal.h> +#include <rte_errno.h> +#include <rte_ethdev.h> +#include <rte_lcore.h> +#include <rte_malloc.h> +#include <rte_mbuf.h> +#include <rte_mempool.h> +#include <rte_pcapng.h> +#include <rte_pdump.h> +#include <rte_ring.h> +#include <rte_string_fns.h> +#include <rte_time.h> +#include <rte_version.h> + +#include <pcap/pcap.h> + +#define RING_NAME "capture-ring" +#define MONITOR_INTERVAL (500 * 1000) +#define MBUF_POOL_CACHE_SIZE 32 +#define BURST_SIZE 32 +#define SLEEP_THRESHOLD 1000 + +/* command line flags */ +static const char *progname; +static bool quit_signal; +static bool group_read; +static bool quiet; +static bool promiscuous_mode = true; +static bool use_pcapng = true; +static char *output_name; +static const char *filter_str; +static unsigned int ring_size =2048 ; +static const char *capture_comment; +static uint32_t snaplen =RTE_MBUF_DEFAULT_BUF_SIZE ; +static unsigned dump_bpf; +static struct { +uint64_t duration;/* nanoseconds */ +unsigned long packets;/* number of packets in file */ +size_t size;/* file size (bytes) */ +} stop; + +/* Running state */ +static struct rte_bpf_prm *bpf_prm; +static uint64_t start_time, end_time; +static uint64_t packets_received; +static size_t file_size; + +struct interface { +TAILQ_ENTRY (interface) next; +uint16_t port; +char name[RTE_ETH_NAME_MAX_LEN ]; + +struct rte_rxtx_callback *rx_cb[RTE_MAX_QUEUES_PER_PORT ]; +}; + +TAILQ_HEAD (interface_list, interface); +static struct interface_list interfaces =TAILQ_HEAD_INITIALIZER (interfaces); +static struct interface *port2intf[RTE_MAX_ETHPORTS ]; + +static void usage (void ) +{ +printf ("Usage: %s [options] ...\n\n" , progname); +printf ("Capture Interface:\n" +" -i <interface> name or port index of interface\n" +" -f <capture filter> packet filter in libpcap filter syntax\n" ); +printf (" -s <snaplen>, --snapshot-length <snaplen>\n" +" packet snapshot length (def: %u)\n" , +RTE_MBUF_DEFAULT_BUF_SIZE ); +printf (" -p, --no-promiscuous-mode\n" +" don't capture in promiscuous mode\n" +" -D, --list-interfaces print list of interfaces and exit\n" +" -d print generated BPF code for capture filter\n" +"\n" +"Stop conditions:\n" +" -c <packet count> stop after n packets (def: infinite)\n" +" -a <autostop cond.> ..., --autostop <autostop cond.> ...\n" +" duration:NUM - stop after NUM seconds\n" +" filesize:NUM - stop this file after NUM kB\n" +" packets:NUM - stop after NUM packets\n" +"Output (files):\n" +" -w <filename> name of file to save (def: tempfile)\n" +" -g enable group read access on the output file(s)\n" +" -n use pcapng format instead of pcap (default)\n" +" -P use libpcap format instead of pcapng\n" +" --capture-comment <comment>\n" +" add a capture comment to the output file\n" +"\n" +"Miscellaneous:\n" +" -q don't report packet capture counts\n" +" -v, --version print version information and exit\n" +" -h, --help display this help and exit\n" +"\n" +"Use Ctrl-C to stop capturing at any time.\n" ); +} + +static const char *version (void ) +{ +static char str[128 ]; + +snprintf (str,sizeof (str), +"%s 1.0 (%s)\n" , progname,rte_version ()); +return str; +} + +/* Parse numeric argument from command line */ +static unsigned long get_uint (const char *arg,const char *name, +unsigned int limit) +{ +unsigned long u; +char *endp; + + u =strtoul (arg, &endp,0 ); +if (*arg =='\0' || *endp !='\0' ) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is not a valid number\n" , + name, arg); +if (limit && u > limit) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is too large (greater than %u)\n" , + name, arg, limit); + +return u; +} + +/* Set auto stop values */ +static void auto_stop (char *opt) +{ +char *value, *endp; + + value =strchr (opt,':' ); +if (value ==NULL ) +rte_exit (EXIT_FAILURE , +"Missing colon in auto stop parameter\n" ); + + *value++ ='\0' ; +if (strcmp (opt,"duration" ) ==0 ) { +double interval =strtod (value, &endp); + +if (*value =='\0' || *endp !='\0' || interval <=0 ) +rte_exit (EXIT_FAILURE , +"Invalid duration \"%s\"\n" , value); + stop.duration =NSEC_PER_SEC * interval; + }else if (strcmp (opt,"filesize" ) ==0 ) { + stop.size =get_uint (value,"filesize" ,0 ) *1024 ; + }else if (strcmp (opt,"packets" ) ==0 ) { + stop.packets =get_uint (value,"packets" ,0 ); + }else { +rte_exit (EXIT_FAILURE , +"Unknown autostop parameter \"%s\"\n" , opt); + } +} + +/* Add interface to list of interfaces to capture */ +static void add_interface (uint16_t port,const char *name) +{ +struct interface *intf; + + intf =malloc (sizeof (*intf)); +if (!intf) +rte_exit (EXIT_FAILURE ,"no memory for interface\n" ); + +memset (intf,0 ,sizeof (*intf)); +strlcpy (intf->name, name,sizeof (intf->name)); + +printf ("Capturing on '%s'\n" , name); + + port2intf[port] = intf; +TAILQ_INSERT_TAIL (&interfaces, intf, next); +} + +/* Select all valid DPDK interfaces */ +static void select_all_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); + } +} + +/* +* Choose interface to capture if no -i option given. +* Select the first DPDK port, this matches what dumpcap does. +*/ +static void set_default_interface (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); +return ; + } +rte_exit (EXIT_FAILURE ,"No usable interfaces found\n" ); +} + +/* Lookup interface by name or port and add it to the list */ +static void select_interface (const char *arg) +{ +uint16_t port; + +if (strcmp (arg,"*" )) +select_all_interfaces (); +else if (rte_eth_dev_get_port_by_name (arg, &port) ==0 ) +add_interface (port, arg); +else { +char name[RTE_ETH_NAME_MAX_LEN ]; + + port =get_uint (arg,"port_number" ,UINT16_MAX ); +if (rte_eth_dev_get_name_by_port (port, name) <0 ) +rte_exit (EXIT_FAILURE ,"Invalid port number %u\n" , + port); +add_interface (port, name); + } +} + +/* Display list of possible interfaces that can be used. */ +static void show_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +printf ("%u. %s\n" , p, name); + } +} + +static void compile_filter (void ) +{ +struct bpf_program fcode; +pcap_t *pcap; + + pcap =pcap_open_dead (DLT_EN10MB , snaplen); +if (!pcap) +rte_exit (EXIT_FAILURE ,"can not open pcap\n" ); + +if (pcap_compile (pcap, &fcode, filter_str, +1 ,PCAP_NETMASK_UNKNOWN ) !=0 ) +rte_exit (EXIT_FAILURE ,"pcap filter string not valid (%s)\n" , +pcap_geterr (pcap)); + + bpf_prm =rte_bpf_convert (&fcode); +if (bpf_prm ==NULL ) +rte_exit (EXIT_FAILURE , +"bpf convert failed\n" ); + +if (dump_bpf) { +printf ("BPF code\n" ); +bpf_dump (&fcode, dump_bpf); + +if (dump_bpf >1 ) { +unsigned int i; + +printf ("\nEBPF code\n" ); +for (i =0 ; i < bpf_prm->nb_ins; ++i) { +const struct ebpf_insn *ins = &bpf_prm->ins[i]; + +printf ("{ %#04x, %2u, %2u, %4u, %#10x },\n" , + ins->code, ins->dst_reg, ins->src_reg, + ins->off, ins->imm); + } + } +exit (0 ); + } + +/* Don't care about original program any more */ +pcap_freecode (&fcode); +pcap_close (pcap); +} + +/* +* Parse command line options. +* These are chosen to be similar to dumpcap command. +*/ +static void parse_opts (int argc,char **argv) +{ +static const struct option long_options[] = { + {"autostop" , required_argument,NULL ,'a' }, + {"capture-comment" , required_argument,NULL ,0 }, + {"help" , no_argument,NULL ,'h' }, + {"interface" , required_argument,NULL ,'i' }, + {"list-interfaces" , no_argument,NULL ,'D' }, + {"no-promiscuous-mode" , no_argument,NULL ,'p' }, + {"output-file" , required_argument,NULL ,'w' }, + {"ring-buffer" , required_argument,NULL ,'b' }, + {"snapshot-length" , required_argument,NULL ,'s' }, + {"version" , no_argument,NULL ,'v' }, + {NULL }, + }; +int option_index, c; + +for (;;) { + c =getopt_long (argc, argv,"a:b:c:dDf:ghi:nN:pPqs:vw:" , + long_options, &option_index); +if (c ==-1 ) +break ; + +switch (c) { +case 0 : +switch (option_index) { +case 0 : + capture_comment = optarg; +break ; +default : +usage (); +exit (1 ); + } +break ; +case 'a' : +auto_stop (optarg); +break ; +case 'b' : +rte_exit (EXIT_FAILURE , +"multiple files not implemented\n" ); +break ; +case 'c' : + stop.packets =get_uint (optarg,"packet_count" ,0 ); +break ; +case 'd' : + ++dump_bpf; +break ; +case 'D' : +show_interfaces (); +exit (0 ); +case 'f' : + filter_str = optarg; +break ; +case 'g' : + group_read = true; +break ; +case 'h' : +printf ("%s\n\n" ,version ()); +usage (); +exit (0 ); +case 'i' : +select_interface (optarg); +break ; +case 'n' : + use_pcapng = true; +break ; +case 'N' : + ring_size =get_uint (optarg,"packet_limit" ,0 ); +break ; +case 'p' : + promiscuous_mode = false; +break ; +case 'P' : + use_pcapng = false; +break ; +case 'q' : + quiet = true; +break ; +case 's' : + snaplen =get_uint (optarg,"snap_len" ,0 ); +break ; +case 'w' : + output_name = optarg; +break ; +case 'v' : +printf ("%s\n" ,version ()); +exit (0 ); +default : +fprintf (stderr,"Invalid option: %s\n" , + argv[optind -1 ]); +usage (); +exit (1 ); + } + } +} + +static void +signal_handler(int sig_num __rte_unused) +{ +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); +} + +/* Return the time since 1/1/1970 in nanoseconds */ +static uint64_t create_timestamp (void ) +{ +struct timespec now; + +clock_gettime (CLOCK_MONOTONIC , &now); +return rte_timespec_to_ns (&now); +} + +static void +cleanup_pdump_resources (void ) +{ +struct interface *intf; + + TAILQ_FOREACH(intf, &interfaces, next) { +rte_pdump_disable (intf->port, +RTE_PDUMP_ALL_QUEUES ,RTE_PDUMP_FLAG_RXTX ); +if (promiscuous_mode) +rte_eth_promiscuous_disable (intf->port); + } +} + +/* Alarm signal handler, used to check that primary process */ +static void +monitor_primary(void *arg __rte_unused) +{ +if (__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) +return ; + +if (rte_eal_primary_proc_alive (NULL )) { +rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); + }else { +fprintf (stderr, +"Primary process is no longer active, exiting...\n" ); +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); + } +} + +/* Setup handler to check when primary exits. */ +static void +enable_primary_monitor (void ) +{ +int ret; + +/* Once primary exits, so will pdump. */ + ret =rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to enable monitor:%d\n" , ret); +} + +static void +disable_primary_monitor (void ) +{ +int ret; + + ret =rte_eal_alarm_cancel (monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to disable monitor:%d\n" , ret); +} + +static void +report_packet_stats (rte_pcapng_t *out) +{ +struct rte_pdump_stats pdump_stats; +struct interface *intf; +uint64_t ifrecv, ifdrop; +double percent; + +fputc ('\n' , stderr); + TAILQ_FOREACH(intf, &interfaces, next) { +if (rte_pdump_stats (intf->port, &pdump_stats) <0 ) +continue ; + +/* do what Wiretap does */ + ifrecv = pdump_stats.accepted + pdump_stats.filtered; + ifdrop = pdump_stats.nombuf + pdump_stats.ringfull; + +if (use_pcapng) +rte_pcapng_write_stats (out, intf->port,NULL , + start_time, end_time, + ifrecv, ifdrop); + +if (ifrecv ==0 ) + percent =0 ; +else + percent =100. * ifrecv / (ifrecv + ifdrop); + +fprintf (stderr, +"Packets received/dropped on interface '%s': " +"%" PRIu64"/%" PRIu64" (%.1f)\n" , + intf->name, ifrecv, ifdrop, percent); + } +} + +/* +* Start DPDK EAL with arguments. +* Unlike most DPDK programs, this application does not use the +* typical EAL command line arguments. +* We don't want to expose all the DPDK internals to the user. +*/ +static void dpdk_init (void ) +{ +const char *args[] = { + progname,"--proc-type" ,"secondary" , +"--log-level" ,"bpf:debug" + + }; +const int eal_argc =RTE_DIM (args); +char **eal_argv; +unsigned int i; + +/* DPDK API requires mutable versions of command line arguments. */ + eal_argv =calloc (eal_argc +1 ,sizeof (char *)); +if (eal_argv ==NULL ) +rte_panic ("No memory\n" ); + +for (i =0 ; i <RTE_DIM (args); i++) + eal_argv[i] =strdup (args[i]); + +if (rte_eal_init (eal_argc, eal_argv) <0 ) +rte_exit (EXIT_FAILURE ,"EAL init failed: is primary process running?\n" ); + +if (rte_eth_dev_count_avail () ==0 ) +rte_exit (EXIT_FAILURE ,"No Ethernet ports found\n" ); +} + +/* Create packet ring shared between callbacks and process */ +static struct rte_ring *create_ring (void ) +{ +struct rte_ring *ring; +size_t size, log2; + +/* Find next power of 2 >= size. */ + size = ring_size; + log2 =sizeof (size) *8 -__builtin_clzl (size -1 ); + size =1u << log2; + +if (size != ring_size) { +fprintf (stderr,"Ring size %u rounded up to %zu\n" , + ring_size, size); + ring_size = size; + } + + ring =rte_ring_lookup (RING_NAME ); +if (ring ==NULL ) { + ring =rte_ring_create (RING_NAME , ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); + } +return ring; +} + +static struct rte_mempool *create_mempool (void ) +{ +static const char pool_name[] ="capture_mbufs" ; +size_t num_mbufs =2 * ring_size; +struct rte_mempool *mp; + + mp =rte_mempool_lookup (pool_name); +if (mp) +return mp; + + mp =rte_pktmbuf_pool_create_by_ops (pool_name, num_mbufs, +MBUF_POOL_CACHE_SIZE ,0 , +rte_pcapng_mbuf_size (snaplen), +rte_socket_id (),"ring_mp_sc" ); +if (mp ==NULL ) +rte_exit (EXIT_FAILURE , +"Mempool (%s) creation failed: %s\n" , pool_name, +rte_strerror (rte_errno)); + +return mp; +} + +static void *create_output (void ) +{ +struct utsname uts; +char os[256 ]; +int fd; + +/* If no filename specified make a tempfile name */ +if (output_name ==NULL ) { +struct interface *intf; +struct tm *tm; +time_t now; +char ts[32 ]; + + intf =TAILQ_FIRST (&interfaces); + now =time (NULL ); + tm =localtime (&now); +if (!tm) +rte_panic ("localtime failed\n" ); + +strftime (ts,sizeof (ts),"%Y%m%d%H%M%S" , tm); +if (asprintf (&output_name,"/tmp/%s_%u_%s_%s.%s" , + progname, intf->port, intf->name, ts, + use_pcapng ?"pcapng" :"pcap" ) <0 ) +rte_panic ("asprintf failed\n" ); + } + +if (strcmp (output_name,"-" ) ==0 ) + fd =STDOUT_FILENO ; +else { +mode_t mode = group_read ?0640 :0600 ; + + fd =open (output_name,O_WRONLY |O_CREAT , mode); +if (fd <0 ) +rte_exit (EXIT_FAILURE ,"Can not open \"%s\": %s\n" , + output_name,strerror (errno)); + } + +if (use_pcapng) { +rte_pcapng_t *pcapng; + +if (uname (&uts) <0 ) +strcpy (os,"unknown" ); +else +snprintf (os,sizeof (os),"%s %s" , + uts.sysname, uts.release); + + pcapng =rte_pcapng_fdopen (fd, os,NULL ,version (), capture_comment); +if (pcapng ==NULL ) +rte_exit (EXIT_FAILURE ,"pcapng_fdopen failed: %s\n" , +strerror (rte_errno)); +return pcapng; + }else { +pcap_dumper_t *dumper; +pcap_t *pcap; + + pcap =pcap_open_dead_with_tstamp_precision (DLT_EN10MB , snaplen, +PCAP_TSTAMP_PRECISION_NANO ); +if (pcap ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_open_dead failed\n" ); + + dumper =pcap_dump_fopen (pcap,fdopen (fd,"w" )); +if (dumper ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_dump_fopen failed: %s\n" , +pcap_geterr (pcap)); +return dumper; + } +} + +static void enable_pdump (struct rte_ring *r,struct rte_mempool *mp) +{ +struct interface *intf; +uint32_t flags; +int ret; + + flags =RTE_PDUMP_FLAG_RXTX ; +if (use_pcapng) + flags |=RTE_PDUMP_FLAG_PCAPNG ; + + TAILQ_FOREACH(intf, &interfaces, next) { +if (promiscuous_mode) +rte_eth_promiscuous_enable (intf->port); + + ret =rte_pdump_enable_bpf (intf->port,RTE_PDUMP_ALL_QUEUES , + flags, snaplen, + r, mp, bpf_prm); +if (ret <0 ) +rte_exit (EXIT_FAILURE , +"Packet dump enable failed: %s\n" , +rte_strerror (-ret)); + } +} + +/* +* Show current count of captured packets +* with backspaces to overwrite last value. +*/ +static void show_count (uint64_t count) +{ +unsigned int i; +static unsigned int bt; + +for (i =0 ; i < bt; i++) +fputc ('\b' , stderr); + + bt =fprintf (stderr,"%" PRIu64" " , count); +} + +/* Write multiple packets in older pcap format */ +static ssize_t +pcap_write_packets (pcap_dumper_t *dumper, +struct rte_mbuf *pkts[],uint16_t n) +{ +uint8_t temp_data[snaplen]; +struct pcap_pkthdr header; +uint16_t i; +size_t total =0 ; + +gettimeofday (&header.ts,NULL ); + +for (i =0 ; i < n; i++) { +struct rte_mbuf *m = pkts[i]; + + header.len =rte_pktmbuf_pkt_len (m); + header.caplen =RTE_MIN (header.len, snaplen); + +pcap_dump ((u_char *)dumper, &header, +rte_pktmbuf_read (m,0 , header.caplen, temp_data)); + + total +=sizeof (header) + header.len; + } + +return total; +} + +/* Process all packets in ring and dump to capture file */ +static int process_ring (void *out,struct rte_ring *r) +{ +struct rte_mbuf *pkts[BURST_SIZE ]; +unsigned int avail, n; +static unsigned int empty_count; +ssize_t written; + + n =rte_ring_sc_dequeue_burst (r, (void **) pkts,BURST_SIZE , + &avail); +if (n ==0 ) { +/* don't consume endless amounts of cpu if idle */ +if (empty_count <SLEEP_THRESHOLD ) + ++empty_count; +else +usleep (10 ); +return 0 ; + } + + empty_count = (avail ==0 ); + +if (use_pcapng) + written =rte_pcapng_write_packets (out, pkts, n); +else + written =pcap_write_packets (out, pkts, n); + +rte_pktmbuf_free_bulk (pkts, n); + +if (written <0 ) +return -1 ; + + file_size += written; + packets_received += n; +if (!quiet) +show_count (packets_received); + +return 0 ; +} + +int main (int argc,char **argv) +{ +struct rte_ring *r; +struct rte_mempool *mp; +void *out; + + progname =basename (argv[0 ]); + +dpdk_init (); +parse_opts (argc, argv); + +if (filter_str) +compile_filter (); + +if (TAILQ_EMPTY (&interfaces)) +set_default_interface (); + + r =create_ring (); + mp =create_mempool (); + out =create_output (); +if (out ==NULL ) +rte_exit (EXIT_FAILURE ,"can not open output file: %s\n" , +rte_strerror (rte_errno)); + + start_time =create_timestamp (); +enable_pdump (r, mp); + +signal (SIGINT , signal_handler); +signal (SIGPIPE ,SIG_IGN ); + +enable_primary_monitor (); + +if (!quiet) { +fprintf (stderr,"Packets captured: " ); +show_count (0 ); + } + +while (!__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) { +if (process_ring (out, r) <0 ) { +fprintf (stderr,"pcapng file write failed; %s\n" , +strerror (errno)); +break ; + } + +if (stop.size && file_size >= stop.size) +break ; + +if (stop.packets && packets_received >= stop.packets) +break ; + +if (stop.duration !=0 && +create_timestamp () - start_time > stop.duration) +break ; + } + + end_time =create_timestamp (); +disable_primary_monitor (); + +if (rte_eal_primary_proc_alive (NULL )) +report_packet_stats (out); + +if (use_pcapng) +rte_pcapng_close (out); +else +pcap_dump_close (out); + +cleanup_pdump_resources (); +rte_free (bpf_filter); +rte_ring_free (r); +rte_mempool_free (mp); + +return rte_eal_cleanup () ?EXIT_FAILURE :0 ; +}
app/dumpcap/meson.build new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +if not dpdk_conf.has('RTE_PORT_PCAP') + build = false + reason = 'missing dependency, "libpcap"' +endif + +if is_windows + build = false + reason = 'not supported on Windows' + subdir_done() +endif + +sources = files('main.c') +deps += ['ethdev', 'pdump', 'pcapng', 'bpf']
app/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2017-2019 Intel Corporation apps = [ + 'dumpcap', 'pdump', 'proc-info', 'test-acl',
Signature · 1 line
2.30.2
PATCH v3 7/8 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 86 +++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 228 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -62,6 +62,16 @@ New Features * Added bus-level parsing of the devargs syntax. * Kept compatibility with the legacy syntax as parsing fallback. +* **Enhance Packet capture.** + + * New dpdk-dumpcap program that has most of the features of the + wireshark dumpcap utility including capture of multiple interfaces, + stopping after number of bytes, packets. + * New library for writing pcapng packet capture files. + * Enhancement to the pdump library to support: + * Packet filter with BPF. + * Pcapng format with timestamps and meta-data. + * Fixes packet capture with stripped VLAN tags. Removed Items -------------
doc/guides/tools/dumpcap.rst new file +86 -0
86 lines not on this page · raw message
doc/guides/tools/index.rst +1 -0
@@ -8,6 +8,7 @@ DPDK Tools User Guides :maxdepth: 2 :numbered: + dumpcap proc_info pdump pmdinfo
Signature · 1 line
2.30.2
PATCH v3 8/8 MAINTAINERS: add entry for new pcapng and dumper
In reply to Stephen HemmingerClaim responsibility for the new code.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -1429,6 +1429,12 @@ F: app/test/test_pdump.* F: app/pdump/ F: doc/guides/tools/pdump.rst +Packet dump +M: Stephen Hemminger <[email protected]> +F: lib/pcapng/ +F: doc/guides/prog_guide/pcapng_lib.rst +F: app/dumpcap/ +F: doc/guides/tools/dumpcap.rst Packet Framework ----------------
Signature · 1 line
2.30.2
v4 cover Packet capture framework enhancements
In reply to Stephen HemmingerThis patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
It preserves the old pdump command as is for those people who never want to change.
v4 changes:
- minor checkpatch fixes.
Note: some of the checkpatch warnings are bogus and won't be fixed.
- fix build of dumpcap on FreeBSD
v3 changes:
- introduce packet filters using classic BPF to eBPF converter required small fix to DPDK BPF interpreter
- introduce function to decode eBPF instructions
- add option to dumpcap to show both classic BPF and eBPF result
- drop some un-useful stubs
- minor checkpatch warning cleanup
Stephen Hemminger (8):
librte_pcapng: add new library for writing pcapng files
bpf: allow self-xor operation
bpf: add function to convert classic BPF to DPDK BPF
bpf: add function to dump eBPF instructions
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new pcapng and dumper
MAINTAINERS | 6 + app/dumpcap/main.c | 827 ++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +- doc/guides/howto/packet_capture_framework.rst | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 570 ++++++++++++ lib/bpf/bpf_dump.c | 118 +++ lib/bpf/bpf_validate.c | 8 +- lib/bpf/meson.build | 6 + lib/bpf/rte_bpf.h | 39 + lib/bpf/version.map | 7 + lib/meson.build | 5 +- lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 437 ++++++--- lib/pdump/rte_pdump.h | 110 ++- lib/pdump/version.map | 8 + 30 files changed, 3177 insertions(+), 215 deletions(-) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst create mode 100644 lib/bpf/bpf_convert.c create mode 100644 lib/bpf/bpf_dump.c create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
Signature · 1 line
2.30.2
PATCH v4 1/8 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 194 +++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 918 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +574 -0
574 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +194 -0
194 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH v4 2/8 bpf: allow self-xor operation
In reply to Stephen HemmingerWhen doing BPF filter program conversion, a common way to zero a register in single instruction is:
xor r7,r7
The BPF validator would not allow this because the value of r7 was undefined. But after this operation it always zero.
--- lib/bpf/bpf_validate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
lib/bpf/bpf_validate.c +6 -2
@@ -661,8 +661,12 @@ eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins) op =BPF_OP (ins->code); - err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , - (op !=BPF_NEG ) ? &rs :NULL ); +/* Allow self-xor as way to zero register */ +if (op ==BPF_XOR && ins->src_reg == ins->dst_reg) + err =NULL ; +else + err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , + (op !=BPF_NEG ) ? &rs :NULL );if (err !=NULL )return err;
Signature · 1 line
2.30.2
PATCH v4 3/8 bpf: add function to convert classic BPF to DPDK BPF
In reply to Stephen HemmingerThe pcap library emits classic BPF (32 bit) and is useful for creating filter programs. The DPDK BPF library only implements extended BPF (eBPF). Add an function to convert from old to new.
The rte_bpf_convert function uses rte_malloc to put the resulting program in hugepage shared memory so it can be passed from a secondary process to a primary process.
The code to convert was originally done as part of the Linux kernel implementation then converted to a userspace program. Both authors have agreed that it is allowable to license this as BSD licensed in DPDK.
--- lib/bpf/bpf_convert.c | 570 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 606 insertions(+) create mode 100644 lib/bpf/bpf_convert.c
lib/bpf/bpf_convert.c new file +570 -0
@@ -0,0 +1,570 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2021 Microsoft Corporation +* +* Based on bpf_convert_filter() in the Linux kernel sources +* and filter2xdp. +* +* Licensed as BSD with permission original authors. +* Copyright (C) 2017 Tobias Klauser +* Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com +*/ + +#include <assert.h> +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <rte_common.h> +#include <rte_bpf.h> +#include <rte_log.h> +#include <rte_malloc.h> +#include <rte_errno.h> + +/* Workaround name conflicts with libpcap */ +#define bpf_validate (f, len) bpf_validate_libpcap(f, len) +#include <pcap/pcap.h> +#include <pcap/bpf.h> +#undef bpf_validate + +#include "bpf_impl.h" +#include "bpf_def.h" + +#ifndef BPF_MAXINSNS +#define BPF_MAXINSNS 4096 +#endif + +/* +* Linux socket filter uses negative absolute offsets to +* reference ancillary data. +* +*/ +#define SKF_AD_OFF (-0x1000) +#define SKF_AD_PROTOCOL 0 +#define SKF_AD_PKTTYPE 4 +#define SKF_AD_IFINDEX 8 +#define SKF_AD_NLATTR 12 +#define SKF_AD_NLATTR_NEST 16 +#define SKF_AD_MARK 20 +#define SKF_AD_QUEUE 24 +#define SKF_AD_HATYPE 28 +#define SKF_AD_RXHASH 32 +#define SKF_AD_CPU 36 +#define SKF_AD_ALU_XOR_X 40 +#define SKF_AD_VLAN_TAG 44 +#define SKF_AD_VLAN_TAG_PRESENT 48 +#define SKF_AD_PAY_OFFSET 52 +#define SKF_AD_RANDOM 56 +#define SKF_AD_VLAN_TPID 60 +#define SKF_AD_MAX 64 + +/* ArgX, context and stack frame pointer register positions. Note, +* Arg1, Arg2, Arg3, etc are used as argument mappings of function +* calls in BPF_CALL instruction. +*/ +#define BPF_REG_ARG1 EBPF_REG_1 +#define BPF_REG_ARG2 EBPF_REG_2 +#define BPF_REG_ARG3 EBPF_REG_3 +#define BPF_REG_ARG4 EBPF_REG_4 +#define BPF_REG_ARG5 EBPF_REG_5 +#define BPF_REG_CTX EBPF_REG_6 +#define BPF_REG_FP EBPF_REG_10 + +/* Additional register mappings for converted user programs. */ +#define BPF_REG_A EBPF_REG_0 +#define BPF_REG_X EBPF_REG_7 +#define BPF_REG_TMP EBPF_REG_8 + +/* Helper macros for filter block array initializers. */ + +/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ + +#define EBPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_ALU32_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ + +#define BPF_ALU32_IMM (OP ,DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov, dst_reg = src_reg */ + +#define BPF_MOV64_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_MOV32_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* Short form of mov, dst_reg = imm32 */ + +#define BPF_MOV32_IMM (DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */ + +#define BPF_MOV32_RAW (TYPE ,DST ,SRC ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_SRC(TYPE), \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = IMM }) + +/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ + +#define BPF_LD_ABS (SIZE ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Memory load, dst_reg = *(uint *) (src_reg + off16) */ + +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Memory store, *(uint *) (dst_reg + off16) = src_reg */ + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ + +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +/* Raw code statement block */ + +#define BPF_RAW_INSN (CODE ,DST ,SRC ,OFF ,IMM ) \ + ((struct ebpf_insn) { \ + .code = CODE, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = IMM }) + +/* Program exit */ + +#define BPF_EXIT_INSN () \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | EBPF_EXIT, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = 0 }) + +/* +* Placeholder to convert BPF extensions like length and VLAN tag +* If and when DPDK BPF supports them. +*/ +static bool convert_bpf_load (const struct bpf_insn *fp, + struct ebpf_insn **new_insnp __rte_unused) +{ +switch (fp->k) { +case SKF_AD_OFF +SKF_AD_PROTOCOL : +case SKF_AD_OFF +SKF_AD_PKTTYPE : +case SKF_AD_OFF +SKF_AD_IFINDEX : +case SKF_AD_OFF +SKF_AD_HATYPE : +case SKF_AD_OFF +SKF_AD_MARK : +case SKF_AD_OFF +SKF_AD_RXHASH : +case SKF_AD_OFF +SKF_AD_QUEUE : +case SKF_AD_OFF +SKF_AD_VLAN_TAG : +case SKF_AD_OFF +SKF_AD_VLAN_TAG_PRESENT : +case SKF_AD_OFF +SKF_AD_VLAN_TPID : +case SKF_AD_OFF +SKF_AD_PAY_OFFSET : +case SKF_AD_OFF +SKF_AD_NLATTR : +case SKF_AD_OFF +SKF_AD_NLATTR_NEST : +case SKF_AD_OFF +SKF_AD_CPU : +case SKF_AD_OFF +SKF_AD_RANDOM : +case SKF_AD_OFF +SKF_AD_ALU_XOR_X : +/* TODO: Not implemented yet */ +RTE_BPF_LOG (ERR ,"BPF extension LOAD ABS %u not supported\n" , + fp->k); +return true; +default : +return false; + } +} + +static int bpf_convert_filter (const struct bpf_insn *prog,size_t len, +struct ebpf_insn *new_prog,uint32_t *new_len) +{ +unsigned int pass =0 ; +size_t new_flen =0 , target, i; +struct ebpf_insn *new_insn; +const struct bpf_insn *fp; +int *addrs =NULL ; +uint8_t bpf_src; + +if (len >BPF_MAXINSNS ) { +RTE_BPF_LOG (ERR ,"%s: cBPF program too long (%zu insns)\n" , + __func__, len); +return -EINVAL ; + } + +/* On second pass, allocate the new program */ +if (new_prog) { + addrs =calloc (len,sizeof (*addrs)); +if (addrs ==NULL ) +return -ENOMEM ; + } + +do_pass: + new_insn = new_prog; + fp = prog; + +/* Classic BPF related prologue emission. */ +if (new_insn) { +/* Classic BPF expects A and X to be reset first. These need +* to be guaranteed to be the first two instructions. +*/ + *new_insn++ =EBPF_ALU64_REG (BPF_XOR ,BPF_REG_A ,BPF_REG_A ); + *new_insn++ =EBPF_ALU64_REG (BPF_XOR ,BPF_REG_X ,BPF_REG_X ); + +/* All programs must keep CTX in callee saved BPF_REG_CTX. +* In eBPF case it's done by the compiler, here we need to +* do this ourself. Initial CTX is present in BPF_REG_ARG1. +*/ + *new_insn++ =BPF_MOV64_REG (BPF_REG_CTX ,BPF_REG_ARG1 ); + }else { + new_insn +=3 ; + } + +for (i =0 ; i < len; fp++, i++) { +struct ebpf_insn tmp_insns[6 ] = { }; +struct ebpf_insn *insn = tmp_insns; + +if (addrs) + addrs[i] = new_insn - new_prog; + +switch (fp->code) { +/* Absolute loads are how classic BPF accesses skb */ +case BPF_LD |BPF_ABS |BPF_W : +case BPF_LD |BPF_ABS |BPF_H : +case BPF_LD |BPF_ABS |BPF_B : +if (convert_bpf_load (fp, &insn)) + goto err; + + *insn =BPF_RAW_INSN (fp->code,0 ,0 ,0 , fp->k); +break ; + +case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_X : +/* For cBPF, don't cause floating point exception */ + *insn++ =BPF_MOV32_REG (BPF_REG_X ,BPF_REG_X ); + *insn++ =BPF_JMP_IMM (EBPF_JNE ,BPF_REG_X ,0 ,2 ); + *insn++ =BPF_ALU32_REG (BPF_XOR ,BPF_REG_A ,BPF_REG_A ); + *insn++ =BPF_EXIT_INSN (); +/* fallthrough */ +case BPF_ALU |BPF_ADD |BPF_X : +case BPF_ALU |BPF_ADD |BPF_K : +case BPF_ALU |BPF_SUB |BPF_X : +case BPF_ALU |BPF_SUB |BPF_K : +case BPF_ALU |BPF_AND |BPF_X : +case BPF_ALU |BPF_AND |BPF_K : +case BPF_ALU |BPF_OR |BPF_X : +case BPF_ALU |BPF_OR |BPF_K : +case BPF_ALU |BPF_LSH |BPF_X : +case BPF_ALU |BPF_LSH |BPF_K : +case BPF_ALU |BPF_RSH |BPF_X : +case BPF_ALU |BPF_RSH |BPF_K : +case BPF_ALU |BPF_XOR |BPF_X : +case BPF_ALU |BPF_XOR |BPF_K : +case BPF_ALU |BPF_MUL |BPF_X : +case BPF_ALU |BPF_MUL |BPF_K : +case BPF_ALU |BPF_DIV |BPF_K : +case BPF_ALU |BPF_MOD |BPF_K : +case BPF_ALU |BPF_NEG : +case BPF_LD |BPF_IND |BPF_W : +case BPF_LD |BPF_IND |BPF_H : +case BPF_LD |BPF_IND |BPF_B : +/* All arithmetic insns map as-is. */ + *insn =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); +break ; + +/* Jump transformation cannot use BPF block macros +* everywhere as offset calculation and target updates +* require a bit more work than the rest, i.e. jump +* opcodes map as-is, but offsets need adjustment. +*/ + +#define BPF_EMIT_JMP \ + do { \ + if (target >= len) \ + goto err; \ + insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ +/* Adjust pc relative offset for 2nd or 3rd insn. */ \ + insn->off -= insn - tmp_insns; \ + }while (0 ) + +case BPF_JMP |BPF_JA : + target = i + fp->k +1 ; + insn->code = fp->code; +BPF_EMIT_JMP ; +break ; + +case BPF_JMP |BPF_JEQ |BPF_K : +case BPF_JMP |BPF_JEQ |BPF_X : +case BPF_JMP |BPF_JSET |BPF_K : +case BPF_JMP |BPF_JSET |BPF_X : +case BPF_JMP |BPF_JGT |BPF_K : +case BPF_JMP |BPF_JGT |BPF_X : +case BPF_JMP |BPF_JGE |BPF_K : +case BPF_JMP |BPF_JGE |BPF_X : +if (BPF_SRC (fp->code) ==BPF_K && (int ) fp->k <0 ) { +/* BPF immediates are signed, zero extend +* immediate into tmp register and use it +* in compare insn. +*/ + *insn++ =BPF_MOV32_IMM (BPF_REG_TMP , fp->k); + + insn->dst_reg =BPF_REG_A ; + insn->src_reg =BPF_REG_TMP ; + bpf_src =BPF_X ; + }else { + insn->dst_reg =BPF_REG_A ; + insn->imm = fp->k; + bpf_src =BPF_SRC (fp->code); + insn->src_reg = bpf_src ==BPF_X ?BPF_REG_X :0 ; + } + +/* Common case where 'jump_false' is next insn. */ +if (fp->jf ==0 ) { + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; + target = i + fp->jt +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Convert JEQ into JNE when 'jump_true' is next insn. */ +if (fp->jt ==0 &&BPF_OP (fp->code) ==BPF_JEQ ) { + insn->code =BPF_JMP |EBPF_JNE | bpf_src; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Other jumps are mapped into two insns: Jxx and JA. */ + target = i + fp->jt +1 ; + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; +BPF_EMIT_JMP ; + insn++; + + insn->code =BPF_JMP |BPF_JA ; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + +/* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ +case BPF_LDX |BPF_MSH |BPF_B : +/* tmp = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_TMP ,BPF_REG_A ); +/* A = BPF_R0 = *(u8 *) (skb->data + K) */ + *insn++ =BPF_LD_ABS (BPF_B , fp->k); +/* A &= 0xf */ + *insn++ =BPF_ALU32_IMM (BPF_AND ,BPF_REG_A ,0xf ); +/* A <<= 2 */ + *insn++ =BPF_ALU32_IMM (BPF_LSH ,BPF_REG_A ,2 ); +/* X = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +/* A = tmp */ + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_TMP ); +break ; + +/* RET_K is remaped into 2 insns. RET_A case doesn't need an +* extra mov as EBPF_REG_0 is already mapped into BPF_REG_A. +*/ +case BPF_RET |BPF_A : +case BPF_RET |BPF_K : +if (BPF_RVAL (fp->code) ==BPF_K ) { + *insn++ =BPF_MOV32_RAW (BPF_K ,EBPF_REG_0 , +0 , fp->k); + } + *insn =BPF_EXIT_INSN (); +break ; + +/* Store to stack. */ +case BPF_ST : +case BPF_STX : + *insn =BPF_STX_MEM (BPF_W ,BPF_REG_FP ,BPF_CLASS (fp->code) == +BPF_ST ?BPF_REG_A :BPF_REG_X , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* Load from stack. */ +case BPF_LD |BPF_MEM : +case BPF_LDX |BPF_MEM : + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_FP , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* A = K or X = K */ +case BPF_LD |BPF_IMM : +case BPF_LDX |BPF_IMM : + *insn =BPF_MOV32_IMM (BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X , fp->k); +break ; + +/* X = A */ +case BPF_MISC |BPF_TAX : + *insn =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +break ; + +/* A = X */ +case BPF_MISC |BPF_TXA : + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_X ); +break ; + +/* A = mbuf->len or X = mbuf->len */ +case BPF_LD |BPF_W |BPF_LEN : +case BPF_LDX |BPF_W |BPF_LEN : +/* BPF_ABS/BPF_IND implicitly expect mbuf ptr in R6 */ + + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_CTX , + offsetof(struct rte_mbuf , pkt_len)); +break ; + +/* Unknown instruction. */ +default : +RTE_BPF_LOG (ERR ,"%s: Unknown instruction!: %#x\n" , + __func__, fp->code); + goto err; + } + + insn++; +if (new_prog) +memcpy (new_insn, tmp_insns, +sizeof (*insn) * (insn - tmp_insns)); + new_insn += insn - tmp_insns; + } + +if (!new_prog) { +/* Only calculating new length. */ + *new_len = new_insn - new_prog; +return 0 ; + } + + pass++; +if ((ptrdiff_t )new_flen != new_insn - new_prog) { + new_flen = new_insn - new_prog; +if (pass >2 ) + goto err; + goto do_pass; + } + +free (addrs); +assert (*new_len == new_flen); + +return 0 ; +err: +free (addrs); +return -1 ; +} + +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog) +{ +struct rte_bpf_prm *prm =NULL ; +struct ebpf_insn *ebpf =NULL ; +uint32_t ebpf_len =0 ; +int ret; + +if (prog ==NULL ) { +RTE_BPF_LOG (ERR ,"%s: NULL program\n" , __func__); + rte_errno =EINVAL ; +return NULL ; + } + +/* 1st pass: calculate the eBPF program length */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len,NULL , &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot get eBPF length\n" , __func__); + rte_errno = -ret; +return NULL ; + } + +RTE_BPF_LOG (DEBUG ,"%s: prog len cBPF=%u -> eBPF=%u\n" , + __func__, prog->bf_len, ebpf_len); + + prm =rte_zmalloc ("bpf_filter" , +sizeof (*prm) + ebpf_len *sizeof (*ebpf),0 ); +if (prm ==NULL ) { + rte_errno =ENOMEM ; +return NULL ; + } + +/* The EPBF instructions in this case are right after the header */ + ebpf = (void *)(prm +1 ); + +/* 2nd pass: remap cBPF to eBPF instructions */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot convert cBPF to eBPF\n" , __func__); +free (prm); + rte_errno = -ret; +return NULL ; + } + + prm->ins = ebpf; + prm->nb_ins = ebpf_len; + +/* Classic BPF programs use mbufs */ + prm->prog_arg.type =RTE_BPF_ARG_PTR_MBUF ; + prm->prog_arg.size =sizeof (struct rte_mbuf ); + +return prm; +}
lib/bpf/meson.build +5 -0
@@ -25,3 +25,8 @@ if dep.found() sources += files('bpf_load_elf.c') ext_deps += dep endif + +if dpdk_conf.has('RTE_PORT_PCAP') + sources += files('bpf_convert.c') + ext_deps += pcap_dep +endif
lib/bpf/rte_bpf.h +25 -0
@@ -198,6 +198,31 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +#ifdef RTE_PORT_PCAP + +struct bpf_program ; + +/** +* Convert a Classic BPF program from libpcap into a DPDK BPF code. +* +* @param prog +* Classic BPF program from pcap_compile(). +* @param prm +* Result Extended BPF program. +* @return +* Pointer to BPF program (allocated with *rte_malloc*) +* that is used in future BPF operations, +* or NULL on error, with error code set in rte_errno. +* Possible rte_errno errors include: +* - EINVAL - invalid parameter passed to function +* - ENOMEM - can't reserve enough memory +*/ +__rte_experimental +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog); + +#endif + #ifdef __cplusplus }#endif
lib/bpf/version.map +6 -0
@@ -14,3 +14,9 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_bpf_convert; +};
Signature · 1 line
2.30.2
PATCH v4 4/8 bpf: add function to dump eBPF instructions
In reply to Stephen HemmingerWhen debugging converted (and other) programs it is useful to see disassembled eBPF output.
--- lib/bpf/bpf_dump.c | 118 ++++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 ++++++ lib/bpf/version.map | 1 + 4 files changed, 134 insertions(+) create mode 100644 lib/bpf/bpf_dump.c
lib/bpf/bpf_dump.c new file +118 -0
@@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright (c) 2021 Stephen Hemminger +* Based on filter2xdp +* Copyright (C) 2017 Tobias Klauser +*/ + +#include <stdio.h> +#include <stdint.h> + +#include "rte_bpf.h" + +#define BPF_OP_INDEX (x) (BPF_OP(x) >> 4) +#define BPF_SIZE_INDEX (x) (BPF_SIZE(x) >> 3) + +static const char *const class_tbl[] = { + [BPF_LD ] ="ld" , [BPF_LDX ] ="ldx" , [BPF_ST ] ="st" , + [BPF_STX ] ="stx" , [BPF_ALU ] ="alu" , [BPF_JMP ] ="jmp" , + [BPF_RET ] ="ret" , [BPF_MISC ] ="alu64" , +}; + +static const char *const alu_op_tbl[16 ] = { + [BPF_ADD >>4 ] ="add" , [BPF_SUB >>4 ] ="sub" , + [BPF_MUL >>4 ] ="mul" , [BPF_DIV >>4 ] ="div" , + [BPF_OR >>4 ] ="or" , [BPF_AND >>4 ] ="and" , + [BPF_LSH >>4 ] ="lsh" , [BPF_RSH >>4 ] ="rsh" , + [BPF_NEG >>4 ] ="neg" , [BPF_MOD >>4 ] ="mod" , + [BPF_XOR >>4 ] ="xor" , [EBPF_MOV >>4 ] ="mov" , + [EBPF_ARSH >>4 ] ="arsh" , [EBPF_END >>4 ] ="endian" , +}; + +static const char *const size_tbl[] = { + [BPF_W >>3 ] ="w" , + [BPF_H >>3 ] ="h" , + [BPF_B >>3 ] ="b" , + [EBPF_DW >>3 ] ="dw" , +}; + +static const char *const jump_tbl[16 ] = { + [BPF_JA >>4 ] ="ja" , [BPF_JEQ >>4 ] ="jeq" , + [BPF_JGT >>4 ] ="jgt" , [BPF_JGE >>4 ] ="jge" , + [BPF_JSET >>4 ] ="jset" , [EBPF_JNE >>4 ] ="jne" , + [EBPF_JSGT >>4 ] ="jsgt" , [EBPF_JSGE >>4 ] ="jsge" , + [EBPF_CALL >>4 ] ="call" , [EBPF_EXIT >>4 ] ="exit" , +}; + +static void ebpf_dump (FILE *f,const struct ebpf_insn insn,size_t n) +{ +const char *op, *postfix ="" ; +uint8_t cls =BPF_CLASS (insn.code); + +fprintf (f," L%zu:\t" , n); + +switch (cls) { +default : +fprintf (f,"unimp 0x%x // class: %s\n" , insn.code, + class_tbl[cls]); +break ; +case BPF_ALU : + postfix ="32" ; +/* fall through */ +case EBPF_ALU64 : + op = alu_op_tbl[BPF_OP_INDEX (insn.code)]; +if (BPF_SRC (insn.code) ==BPF_X ) +fprintf (f,"%s%s r%u, r%u\n" , op, postfix, insn.dst_reg, + insn.src_reg); +else +fprintf (f,"%s%s r%u, #0x%x\n" , op, postfix, + insn.dst_reg, insn.imm); +break ; +case BPF_LD : + op ="ld" ; + postfix = size_tbl[BPF_SIZE_INDEX (insn.code)]; +if (BPF_MODE (insn.code) ==BPF_IMM ) +fprintf (f,"%s%s r%d, #0x%x\n" , op, postfix, + insn.dst_reg, insn.imm); +else if (BPF_MODE (insn.code) ==BPF_ABS ) +fprintf (f,"%s%s r%d, [%d]\n" , op, postfix, + insn.dst_reg, insn.imm); +else if (BPF_MODE (insn.code) ==BPF_IND ) +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, + insn.dst_reg, insn.src_reg, insn.imm); +else +fprintf (f,"// BUG: LD opcode 0x%02x in eBPF insns\n" , + insn.code); +break ; +case BPF_LDX : + op ="ldx" ; + postfix = size_tbl[BPF_SIZE_INDEX (insn.code)]; +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, insn.dst_reg, + insn.src_reg, insn.off); +break ; +#define L (pc, off) ((int)(pc) + 1 + (off)) +case BPF_JMP : + op = jump_tbl[BPF_OP_INDEX (insn.code)]; +if (op ==NULL ) +fprintf (f,"invalid jump opcode: %#x\n" , insn.code); +else if (BPF_OP (insn.code) ==BPF_JA ) +fprintf (f,"%s L%d\n" , op,L (n, insn.off)); +else if (BPF_OP (insn.code) ==EBPF_EXIT ) +fprintf (f,"%s\n" , op); +else +fprintf (f,"%s r%u, #0x%x, L%d\n" , op, insn.dst_reg, + insn.imm,L (n, insn.off)); +break ; +case BPF_RET : +fprintf (f,"// BUG: RET opcode 0x%02x in eBPF insns\n" , + insn.code); +break ; + } +} + +void rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len) +{ +uint32_t i; + +for (i =0 ; i < len; ++i) +ebpf_dump (f, buf[i], i); +}
lib/bpf/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2018 Intel Corporation sources = files('bpf.c', + 'bpf_dump.c', 'bpf_exec.c', 'bpf_load.c', 'bpf_pkt.c',
lib/bpf/rte_bpf.h +14 -0
@@ -198,6 +198,20 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +/** +* Dump epf instructions to a file. +* +* @param f +* A pointer to a file for output +* @param buf +* A pointer to BPF instructions +* @param len +* Number of BPF instructions to dump. +*/ +__rte_experimental +void +rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len); +#ifdef RTE_PORT_PCAP struct bpf_program ;
lib/bpf/version.map +1 -0
@@ -19,4 +19,5 @@ EXPERIMENTAL { global: rte_bpf_convert; + rte_bpf_dump; };
Signature · 1 line
2.30.2
PATCH v4 5/8 pdump: support pcapng and filtering
In reply to Stephen HemmingerThis enhances the DPDK pdump library to support new pcapng format and filtering via BPF.
The internal client/server protocol is changed to support two versions: the original pdump basic version and a new pcapng version.
The internal version number (not part of exposed API or ABI) is intentionally increased to cause any attempt to try mismatched primary/secondary process to fail.
Add new API to do allow filtering of captured packets with DPDK BPF (eBPF) filter program. It keeps statistics
on packets captured, filtered, and missed (because ring was full).
--- lib/meson.build | 4 +- lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 437 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 110 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 435 insertions(+), 126 deletions(-)
lib/meson.build +2 -2
@@ -26,6 +26,7 @@ libraries = [ 'timer', # eventdev depends on this 'acl', 'bbdev', + 'bpf', 'bitratestats', 'cfgfile', 'compressdev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'rib',
@@ -55,10 +55,10 @@ libraries = [ 'ipsec', # ipsec lib depends on net, crypto and security 'fib', #fib lib depends on rib 'port', # pkt framework libs which use other libs from above + 'pdump', # pdump lib depends on bpf pcapng 'table', 'pipeline', 'flow_classify', # flow_classify lib depends on pkt framework table lib - 'bpf', 'graph', 'node', ]
lib/pdump/meson.build +1 -1
@@ -3,4 +3,4 @@ sources = files('rte_pdump.c') headers = files('rte_pdump.h') -deps += ['ethdev'] +deps += ['ethdev', 'bpf', 'pcapng']
lib/pdump/rte_pdump.c +317 -120
644 lines not on this page · raw message
lib/pdump/rte_pdump.h +107 -3
152 lines not on this page · raw message
lib/pdump/version.map +8 -0
@@ -10,3 +10,11 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_pdump_enable_bpf; + rte_pdump_enable_bpf_by_deviceid; + rte_pdump_stats; +};
Signature · 1 line
2.30.2
PATCH v4 6/8 app/dumpcap: add new packet capture application
In reply to Stephen HemmingerThis is a new packet capture application to replace existing pdump. The new application works like Wireshark dumpcap program and supports the pdump API features.
It is not complete yet some features such as filtering are not implemented.
--- app/dumpcap/main.c | 827 ++++++++++++++++++++++++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + 3 files changed, 844 insertions(+) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build
app/dumpcap/main.c new file +827 -0
@@ -0,0 +1,827 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019-2020 Microsoft Corporation +* +* DPDK application to dump network traffic +* This is designed to look and act like the Wireshark +* dumpcap program. +*/ + +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <inttypes.h> +#include <limits.h> +#include <signal.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/queue.h> +#include <sys/types.h> +#include <sys/utsname.h> +#include <time.h> +#include <unistd.h> + +#include <rte_alarm.h> +#include <rte_bpf.h> +#include <rte_config.h> +#include <rte_debug.h> +#include <rte_eal.h> +#include <rte_errno.h> +#include <rte_ethdev.h> +#include <rte_lcore.h> +#include <rte_malloc.h> +#include <rte_mbuf.h> +#include <rte_mempool.h> +#include <rte_pcapng.h> +#include <rte_pdump.h> +#include <rte_ring.h> +#include <rte_string_fns.h> +#include <rte_time.h> +#include <rte_version.h> + +#include <pcap/pcap.h> +#include <pcap/bpf.h> + +#define RING_NAME "capture-ring" +#define MONITOR_INTERVAL (500 * 1000) +#define MBUF_POOL_CACHE_SIZE 32 +#define BURST_SIZE 32 +#define SLEEP_THRESHOLD 1000 + +/* command line flags */ +static const char *progname; +static bool quit_signal; +static bool group_read; +static bool quiet; +static bool promiscuous_mode = true; +static bool use_pcapng = true; +static char *output_name; +static const char *filter_str; +static unsigned int ring_size =2048 ; +static const char *capture_comment; +static uint32_t snaplen =RTE_MBUF_DEFAULT_BUF_SIZE ; +static bool dump_bpf; +static struct { +uint64_t duration;/* nanoseconds */ +unsigned long packets;/* number of packets in file */ +size_t size;/* file size (bytes) */ +} stop; + +/* Running state */ +static struct rte_bpf_prm *bpf_prm; +static uint64_t start_time, end_time; +static uint64_t packets_received; +static size_t file_size; + +struct interface { +TAILQ_ENTRY (interface) next; +uint16_t port; +char name[RTE_ETH_NAME_MAX_LEN ]; + +struct rte_rxtx_callback *rx_cb[RTE_MAX_QUEUES_PER_PORT ]; +}; + +TAILQ_HEAD (interface_list, interface); +static struct interface_list interfaces =TAILQ_HEAD_INITIALIZER (interfaces); +static struct interface *port2intf[RTE_MAX_ETHPORTS ]; + +static void usage (void ) +{ +printf ("Usage: %s [options] ...\n\n" , progname); +printf ("Capture Interface:\n" +" -i <interface> name or port index of interface\n" +" -f <capture filter> packet filter in libpcap filter syntax\n" ); +printf (" -s <snaplen>, --snapshot-length <snaplen>\n" +" packet snapshot length (def: %u)\n" , +RTE_MBUF_DEFAULT_BUF_SIZE ); +printf (" -p, --no-promiscuous-mode\n" +" don't capture in promiscuous mode\n" +" -D, --list-interfaces print list of interfaces and exit\n" +" -d print generated BPF code for capture filter\n" +"\n" +"Stop conditions:\n" +" -c <packet count> stop after n packets (def: infinite)\n" +" -a <autostop cond.> ..., --autostop <autostop cond.> ...\n" +" duration:NUM - stop after NUM seconds\n" +" filesize:NUM - stop this file after NUM kB\n" +" packets:NUM - stop after NUM packets\n" +"Output (files):\n" +" -w <filename> name of file to save (def: tempfile)\n" +" -g enable group read access on the output file(s)\n" +" -n use pcapng format instead of pcap (default)\n" +" -P use libpcap format instead of pcapng\n" +" --capture-comment <comment>\n" +" add a capture comment to the output file\n" +"\n" +"Miscellaneous:\n" +" -q don't report packet capture counts\n" +" -v, --version print version information and exit\n" +" -h, --help display this help and exit\n" +"\n" +"Use Ctrl-C to stop capturing at any time.\n" ); +} + +static const char *version (void ) +{ +static char str[128 ]; + +snprintf (str,sizeof (str), +"%s 1.0 (%s)\n" , progname,rte_version ()); +return str; +} + +/* Parse numeric argument from command line */ +static unsigned long get_uint (const char *arg,const char *name, +unsigned int limit) +{ +unsigned long u; +char *endp; + + u =strtoul (arg, &endp,0 ); +if (*arg =='\0' || *endp !='\0' ) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is not a valid number\n" , + name, arg); +if (limit && u > limit) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is too large (greater than %u)\n" , + name, arg, limit); + +return u; +} + +/* Set auto stop values */ +static void auto_stop (char *opt) +{ +char *value, *endp; + + value =strchr (opt,':' ); +if (value ==NULL ) +rte_exit (EXIT_FAILURE , +"Missing colon in auto stop parameter\n" ); + + *value++ ='\0' ; +if (strcmp (opt,"duration" ) ==0 ) { +double interval =strtod (value, &endp); + +if (*value =='\0' || *endp !='\0' || interval <=0 ) +rte_exit (EXIT_FAILURE , +"Invalid duration \"%s\"\n" , value); + stop.duration =NSEC_PER_SEC * interval; + }else if (strcmp (opt,"filesize" ) ==0 ) { + stop.size =get_uint (value,"filesize" ,0 ) *1024 ; + }else if (strcmp (opt,"packets" ) ==0 ) { + stop.packets =get_uint (value,"packets" ,0 ); + }else { +rte_exit (EXIT_FAILURE , +"Unknown autostop parameter \"%s\"\n" , opt); + } +} + +/* Add interface to list of interfaces to capture */ +static void add_interface (uint16_t port,const char *name) +{ +struct interface *intf; + + intf =malloc (sizeof (*intf)); +if (!intf) +rte_exit (EXIT_FAILURE ,"no memory for interface\n" ); + +memset (intf,0 ,sizeof (*intf)); +strlcpy (intf->name, name,sizeof (intf->name)); + +printf ("Capturing on '%s'\n" , name); + + port2intf[port] = intf; +TAILQ_INSERT_TAIL (&interfaces, intf, next); +} + +/* Select all valid DPDK interfaces */ +static void select_all_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); + } +} + +/* +* Choose interface to capture if no -i option given. +* Select the first DPDK port, this matches what dumpcap does. +*/ +static void set_default_interface (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); +return ; + } +rte_exit (EXIT_FAILURE ,"No usable interfaces found\n" ); +} + +/* Lookup interface by name or port and add it to the list */ +static void select_interface (const char *arg) +{ +uint16_t port; + +if (strcmp (arg,"*" )) +select_all_interfaces (); +else if (rte_eth_dev_get_port_by_name (arg, &port) ==0 ) +add_interface (port, arg); +else { +char name[RTE_ETH_NAME_MAX_LEN ]; + + port =get_uint (arg,"port_number" ,UINT16_MAX ); +if (rte_eth_dev_get_name_by_port (port, name) <0 ) +rte_exit (EXIT_FAILURE ,"Invalid port number %u\n" , + port); +add_interface (port, name); + } +} + +/* Display list of possible interfaces that can be used. */ +static void show_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +printf ("%u. %s\n" , p, name); + } +} + +static void compile_filter (void ) +{ +struct bpf_program bf; +pcap_t *pcap; + + pcap =pcap_open_dead (DLT_EN10MB , snaplen); +if (!pcap) +rte_exit (EXIT_FAILURE ,"can not open pcap\n" ); + +if (pcap_compile (pcap, &bf, filter_str, +1 ,PCAP_NETMASK_UNKNOWN ) !=0 ) +rte_exit (EXIT_FAILURE ,"pcap filter string not valid (%s)\n" , +pcap_geterr (pcap)); + + bpf_prm =rte_bpf_convert (&bf); +if (bpf_prm ==NULL ) +rte_exit (EXIT_FAILURE , +"bpf convert failed\n" ); + +if (dump_bpf) { +printf ("cBPF program (%u insns)\n" , bf.bf_len); +bpf_dump (&bf,1 ); +printf ("\neBPF program (%u insns)\n" , bpf_prm->nb_ins); +rte_bpf_dump (stdout, bpf_prm->ins, bpf_prm->nb_ins); +exit (0 ); + } + +/* Don't care about original program any more */ +pcap_freecode (&bf); +pcap_close (pcap); +} + +/* +* Parse command line options. +* These are chosen to be similar to dumpcap command. +*/ +static void parse_opts (int argc,char **argv) +{ +static const struct option long_options[] = { + {"autostop" , required_argument,NULL ,'a' }, + {"capture-comment" , required_argument,NULL ,0 }, + {"help" , no_argument,NULL ,'h' }, + {"interface" , required_argument,NULL ,'i' }, + {"list-interfaces" , no_argument,NULL ,'D' }, + {"no-promiscuous-mode" , no_argument,NULL ,'p' }, + {"output-file" , required_argument,NULL ,'w' }, + {"ring-buffer" , required_argument,NULL ,'b' }, + {"snapshot-length" , required_argument,NULL ,'s' }, + {"version" , no_argument,NULL ,'v' }, + {NULL }, + }; +int option_index, c; + +for (;;) { + c =getopt_long (argc, argv,"a:b:c:dDf:ghi:nN:pPqs:vw:" , + long_options, &option_index); +if (c ==-1 ) +break ; + +switch (c) { +case 0 : +switch (option_index) { +case 0 : + capture_comment = optarg; +break ; +default : +usage (); +exit (1 ); + } +break ; +case 'a' : +auto_stop (optarg); +break ; +case 'b' : +rte_exit (EXIT_FAILURE , +"multiple files not implemented\n" ); +break ; +case 'c' : + stop.packets =get_uint (optarg,"packet_count" ,0 ); +break ; +case 'd' : + dump_bpf = true; +break ; +case 'D' : +show_interfaces (); +exit (0 ); +case 'f' : + filter_str = optarg; +break ; +case 'g' : + group_read = true; +break ; +case 'h' : +printf ("%s\n\n" ,version ()); +usage (); +exit (0 ); +case 'i' : +select_interface (optarg); +break ; +case 'n' : + use_pcapng = true; +break ; +case 'N' : + ring_size =get_uint (optarg,"packet_limit" ,0 ); +break ; +case 'p' : + promiscuous_mode = false; +break ; +case 'P' : + use_pcapng = false; +break ; +case 'q' : + quiet = true; +break ; +case 's' : + snaplen =get_uint (optarg,"snap_len" ,0 ); +break ; +case 'w' : + output_name = optarg; +break ; +case 'v' : +printf ("%s\n" ,version ()); +exit (0 ); +default : +fprintf (stderr,"Invalid option: %s\n" , + argv[optind -1 ]); +usage (); +exit (1 ); + } + } +} + +static void +signal_handler(int sig_num __rte_unused) +{ +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); +} + +/* Return the time since 1/1/1970 in nanoseconds */ +static uint64_t create_timestamp (void ) +{ +struct timespec now; + +clock_gettime (CLOCK_MONOTONIC , &now); +return rte_timespec_to_ns (&now); +} + +static void +cleanup_pdump_resources (void ) +{ +struct interface *intf; + + TAILQ_FOREACH(intf, &interfaces, next) { +rte_pdump_disable (intf->port, +RTE_PDUMP_ALL_QUEUES ,RTE_PDUMP_FLAG_RXTX ); +if (promiscuous_mode) +rte_eth_promiscuous_disable (intf->port); + } +} + +/* Alarm signal handler, used to check that primary process */ +static void +monitor_primary(void *arg __rte_unused) +{ +if (__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) +return ; + +if (rte_eal_primary_proc_alive (NULL )) { +rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); + }else { +fprintf (stderr, +"Primary process is no longer active, exiting...\n" ); +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); + } +} + +/* Setup handler to check when primary exits. */ +static void +enable_primary_monitor (void ) +{ +int ret; + +/* Once primary exits, so will pdump. */ + ret =rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to enable monitor:%d\n" , ret); +} + +static void +disable_primary_monitor (void ) +{ +int ret; + + ret =rte_eal_alarm_cancel (monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to disable monitor:%d\n" , ret); +} + +static void +report_packet_stats (rte_pcapng_t *out) +{ +struct rte_pdump_stats pdump_stats; +struct interface *intf; +uint64_t ifrecv, ifdrop; +double percent; + +fputc ('\n' , stderr); + TAILQ_FOREACH(intf, &interfaces, next) { +if (rte_pdump_stats (intf->port, &pdump_stats) <0 ) +continue ; + +/* do what Wiretap does */ + ifrecv = pdump_stats.accepted + pdump_stats.filtered; + ifdrop = pdump_stats.nombuf + pdump_stats.ringfull; + +if (use_pcapng) +rte_pcapng_write_stats (out, intf->port,NULL , + start_time, end_time, + ifrecv, ifdrop); + +if (ifrecv ==0 ) + percent =0 ; +else + percent =100. * ifrecv / (ifrecv + ifdrop); + +fprintf (stderr, +"Packets received/dropped on interface '%s': " +"%" PRIu64"/%" PRIu64" (%.1f)\n" , + intf->name, ifrecv, ifdrop, percent); + } +} + +/* +* Start DPDK EAL with arguments. +* Unlike most DPDK programs, this application does not use the +* typical EAL command line arguments. +* We don't want to expose all the DPDK internals to the user. +*/ +static void dpdk_init (void ) +{ +const char *args[] = { + progname,"--proc-type" ,"secondary" , +"--log-level" ,"bpf:debug" + + }; +const int eal_argc =RTE_DIM (args); +char **eal_argv; +unsigned int i; + +/* DPDK API requires mutable versions of command line arguments. */ + eal_argv =calloc (eal_argc +1 ,sizeof (char *)); +if (eal_argv ==NULL ) +rte_panic ("No memory\n" ); + +for (i =0 ; i <RTE_DIM (args); i++) + eal_argv[i] =strdup (args[i]); + +if (rte_eal_init (eal_argc, eal_argv) <0 ) +rte_exit (EXIT_FAILURE ,"EAL init failed: is primary process running?\n" ); + +if (rte_eth_dev_count_avail () ==0 ) +rte_exit (EXIT_FAILURE ,"No Ethernet ports found\n" ); +} + +/* Create packet ring shared between callbacks and process */ +static struct rte_ring *create_ring (void ) +{ +struct rte_ring *ring; +size_t size, log2; + +/* Find next power of 2 >= size. */ + size = ring_size; + log2 =sizeof (size) *8 -__builtin_clzl (size -1 ); + size =1u << log2; + +if (size != ring_size) { +fprintf (stderr,"Ring size %u rounded up to %zu\n" , + ring_size, size); + ring_size = size; + } + + ring =rte_ring_lookup (RING_NAME ); +if (ring ==NULL ) { + ring =rte_ring_create (RING_NAME , ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); + } +return ring; +} + +static struct rte_mempool *create_mempool (void ) +{ +static const char pool_name[] ="capture_mbufs" ; +size_t num_mbufs =2 * ring_size; +struct rte_mempool *mp; + + mp =rte_mempool_lookup (pool_name); +if (mp) +return mp; + + mp =rte_pktmbuf_pool_create_by_ops (pool_name, num_mbufs, +MBUF_POOL_CACHE_SIZE ,0 , +rte_pcapng_mbuf_size (snaplen), +rte_socket_id (),"ring_mp_sc" ); +if (mp ==NULL ) +rte_exit (EXIT_FAILURE , +"Mempool (%s) creation failed: %s\n" , pool_name, +rte_strerror (rte_errno)); + +return mp; +} + +static void *create_output (void ) +{ +static char tmp_path[PATH_MAX ]; +struct utsname uts; +char os[256 ]; +int fd; + +/* If no filename specified make a tempfile name */ +if (output_name ==NULL ) { +struct interface *intf; +struct tm *tm; +time_t now; +char ts[32 ]; + + intf =TAILQ_FIRST (&interfaces); + now =time (NULL ); + tm =localtime (&now); +if (!tm) +rte_panic ("localtime failed\n" ); + +strftime (ts,sizeof (ts),"%Y%m%d%H%M%S" , tm); + +snprintf (tmp_path,sizeof (tmp_path), +"/tmp/%s_%u_%s_%s.%s" , + progname, intf->port, intf->name, ts, + use_pcapng ?"pcapng" :"pcap" ); + output_name = tmp_path; + } + +if (strcmp (output_name,"-" ) ==0 ) + fd =STDOUT_FILENO ; +else { +mode_t mode = group_read ?0640 :0600 ; + + fd =open (output_name,O_WRONLY |O_CREAT , mode); +if (fd <0 ) +rte_exit (EXIT_FAILURE ,"Can not open \"%s\": %s\n" , + output_name,strerror (errno)); + } + +if (use_pcapng) { +rte_pcapng_t *pcapng; + +if (uname (&uts) <0 ) +strcpy (os,"unknown" ); +else +snprintf (os,sizeof (os),"%s %s" , + uts.sysname, uts.release); + + pcapng =rte_pcapng_fdopen (fd, os,NULL ,version (), capture_comment); +if (pcapng ==NULL ) +rte_exit (EXIT_FAILURE ,"pcapng_fdopen failed: %s\n" , +strerror (rte_errno)); +return pcapng; + }else { +pcap_dumper_t *dumper; +pcap_t *pcap; + + pcap =pcap_open_dead_with_tstamp_precision (DLT_EN10MB , snaplen, +PCAP_TSTAMP_PRECISION_NANO ); +if (pcap ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_open_dead failed\n" ); + + dumper =pcap_dump_fopen (pcap,fdopen (fd,"w" )); +if (dumper ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_dump_fopen failed: %s\n" , +pcap_geterr (pcap)); +return dumper; + } +} + +static void enable_pdump (struct rte_ring *r,struct rte_mempool *mp) +{ +struct interface *intf; +uint32_t flags; +int ret; + + flags =RTE_PDUMP_FLAG_RXTX ; +if (use_pcapng) + flags |=RTE_PDUMP_FLAG_PCAPNG ; + + TAILQ_FOREACH(intf, &interfaces, next) { +if (promiscuous_mode) +rte_eth_promiscuous_enable (intf->port); + + ret =rte_pdump_enable_bpf (intf->port,RTE_PDUMP_ALL_QUEUES , + flags, snaplen, + r, mp, bpf_prm); +if (ret <0 ) +rte_exit (EXIT_FAILURE , +"Packet dump enable failed: %s\n" , +rte_strerror (-ret)); + } +} + +/* +* Show current count of captured packets +* with backspaces to overwrite last value. +*/ +static void show_count (uint64_t count) +{ +unsigned int i; +static unsigned int bt; + +for (i =0 ; i < bt; i++) +fputc ('\b' , stderr); + + bt =fprintf (stderr,"%" PRIu64" " , count); +} + +/* Write multiple packets in older pcap format */ +static ssize_t +pcap_write_packets (pcap_dumper_t *dumper, +struct rte_mbuf *pkts[],uint16_t n) +{ +uint8_t temp_data[snaplen]; +struct pcap_pkthdr header; +uint16_t i; +size_t total =0 ; + +gettimeofday (&header.ts,NULL ); + +for (i =0 ; i < n; i++) { +struct rte_mbuf *m = pkts[i]; + + header.len =rte_pktmbuf_pkt_len (m); + header.caplen =RTE_MIN (header.len, snaplen); + +pcap_dump ((u_char *)dumper, &header, +rte_pktmbuf_read (m,0 , header.caplen, temp_data)); + + total +=sizeof (header) + header.len; + } + +return total; +} + +/* Process all packets in ring and dump to capture file */ +static int process_ring (void *out,struct rte_ring *r) +{ +struct rte_mbuf *pkts[BURST_SIZE ]; +unsigned int avail, n; +static unsigned int empty_count; +ssize_t written; + + n =rte_ring_sc_dequeue_burst (r, (void **) pkts,BURST_SIZE , + &avail); +if (n ==0 ) { +/* don't consume endless amounts of cpu if idle */ +if (empty_count <SLEEP_THRESHOLD ) + ++empty_count; +else +usleep (10 ); +return 0 ; + } + + empty_count = (avail ==0 ); + +if (use_pcapng) + written =rte_pcapng_write_packets (out, pkts, n); +else + written =pcap_write_packets (out, pkts, n); + +rte_pktmbuf_free_bulk (pkts, n); + +if (written <0 ) +return -1 ; + + file_size += written; + packets_received += n; +if (!quiet) +show_count (packets_received); + +return 0 ; +} + +int main (int argc,char **argv) +{ +struct rte_ring *r; +struct rte_mempool *mp; +void *out; + + progname = argv[0 ]; + +dpdk_init (); +parse_opts (argc, argv); + +if (filter_str) +compile_filter (); + +if (TAILQ_EMPTY (&interfaces)) +set_default_interface (); + + r =create_ring (); + mp =create_mempool (); + out =create_output (); +if (out ==NULL ) +rte_exit (EXIT_FAILURE ,"can not open output file: %s\n" , +rte_strerror (rte_errno)); + + start_time =create_timestamp (); +enable_pdump (r, mp); + +signal (SIGINT , signal_handler); +signal (SIGPIPE ,SIG_IGN ); + +enable_primary_monitor (); + +if (!quiet) { +fprintf (stderr,"Packets captured: " ); +show_count (0 ); + } + +while (!__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) { +if (process_ring (out, r) <0 ) { +fprintf (stderr,"pcapng file write failed; %s\n" , +strerror (errno)); +break ; + } + +if (stop.size && file_size >= stop.size) +break ; + +if (stop.packets && packets_received >= stop.packets) +break ; + +if (stop.duration !=0 && +create_timestamp () - start_time > stop.duration) +break ; + } + + end_time =create_timestamp (); +disable_primary_monitor (); + +if (rte_eal_primary_proc_alive (NULL )) +report_packet_stats (out); + +if (use_pcapng) +rte_pcapng_close (out); +else +pcap_dump_close (out); + +cleanup_pdump_resources (); +rte_free (bpf_filter); +rte_ring_free (r); +rte_mempool_free (mp); + +return rte_eal_cleanup () ?EXIT_FAILURE :0 ; +}
app/dumpcap/meson.build new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +if not dpdk_conf.has('RTE_PORT_PCAP') + build = false + reason = 'missing dependency, "libpcap"' +endif + +if is_windows + build = false + reason = 'not supported on Windows' + subdir_done() +endif + +sources = files('main.c') +deps += ['ethdev', 'pdump', 'pcapng', 'bpf']
app/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2017-2019 Intel Corporation apps = [ + 'dumpcap', 'pdump', 'proc-info', 'test-acl',
Signature · 1 line
2.30.2
PATCH v4 7/8 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 86 +++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 228 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -62,6 +62,16 @@ New Features * Added bus-level parsing of the devargs syntax. * Kept compatibility with the legacy syntax as parsing fallback. +* **Enhance Packet capture.** + + * New dpdk-dumpcap program that has most of the features of the + wireshark dumpcap utility including capture of multiple interfaces, + stopping after number of bytes, packets. + * New library for writing pcapng packet capture files. + * Enhancement to the pdump library to support: + * Packet filter with BPF. + * Pcapng format with timestamps and meta-data. + * Fixes packet capture with stripped VLAN tags. Removed Items -------------
doc/guides/tools/dumpcap.rst new file +86 -0
86 lines not on this page · raw message
doc/guides/tools/index.rst +1 -0
@@ -8,6 +8,7 @@ DPDK Tools User Guides :maxdepth: 2 :numbered: + dumpcap proc_info pdump pmdinfo
Signature · 1 line
2.30.2
PATCH v4 8/8 MAINTAINERS: add entry for new pcapng and dumper
In reply to Stephen HemmingerClaim responsibility for the new code.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -1429,6 +1429,12 @@ F: app/test/test_pdump.* F: app/pdump/ F: doc/guides/tools/pdump.rst +Packet dump +M: Stephen Hemminger <[email protected]> +F: lib/pcapng/ +F: doc/guides/prog_guide/pcapng_lib.rst +F: app/dumpcap/ +F: doc/guides/tools/dumpcap.rst Packet Framework ----------------
Signature · 1 line
2.30.2
v5 cover Packet capture framework enhancements
In reply to Stephen HemmingerThis patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
It preserves the old pdump command as is for those people who never want to change.
v5 changes:
- minor build and checkpatch fixes for RHEL/FreeBSD
- disable lib/pdump on Windows. It was not useful before and now pdump depends on bpf.
v4 changes:
- minor checkpatch fixes.
Note: some of the checkpatch warnings are bogus and won't be fixed.
- fix build of dumpcap on FreeBSD
v3 changes:
- introduce packet filters using classic BPF to eBPF converter required small fix to DPDK BPF interpreter
- introduce function to decode eBPF instructions
- add option to dumpcap to show both classic BPF and eBPF result
- drop some un-useful stubs
- minor checkpatch warning cleanup
Stephen Hemminger (9):
librte_pcapng: add new library for writing pcapng files
bpf: allow self-xor operation
bpf: add function to convert classic BPF to DPDK BPF
bpf: add function to dump eBPF instructions
lib: pdump is not supported on Windows
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new pcapng and dumper
MAINTAINERS | 6 + app/dumpcap/main.c | 827 ++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +- doc/guides/howto/packet_capture_framework.rst | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 570 ++++++++++++ lib/bpf/bpf_dump.c | 118 +++ lib/bpf/bpf_validate.c | 8 +- lib/bpf/meson.build | 6 + lib/bpf/rte_bpf.h | 39 + lib/bpf/version.map | 7 + lib/meson.build | 6 +- lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 437 ++++++--- lib/pdump/rte_pdump.h | 110 ++- lib/pdump/version.map | 8 + 30 files changed, 3177 insertions(+), 216 deletions(-) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst create mode 100644 lib/bpf/bpf_convert.c create mode 100644 lib/bpf/bpf_dump.c create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
Signature · 1 line
2.30.2
PATCH v5 1/9 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 194 +++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 918 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +574 -0
574 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +194 -0
194 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH v5 2/9 bpf: allow self-xor operation
In reply to Stephen HemmingerWhen doing BPF filter program conversion, a common way to zero a register in single instruction is:
xor r7,r7
The BPF validator would not allow this because the value of r7 was undefined. But after this operation it always zero.
--- lib/bpf/bpf_validate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
lib/bpf/bpf_validate.c +6 -2
@@ -661,8 +661,12 @@ eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins) op =BPF_OP (ins->code); - err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , - (op !=BPF_NEG ) ? &rs :NULL ); +/* Allow self-xor as way to zero register */ +if (op ==BPF_XOR && ins->src_reg == ins->dst_reg) + err =NULL ; +else + err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , + (op !=BPF_NEG ) ? &rs :NULL );if (err !=NULL )return err;
Signature · 1 line
2.30.2
PATCH v5 3/9 bpf: add function to convert classic BPF to DPDK BPF
In reply to Stephen HemmingerThe pcap library emits classic BPF (32 bit) and is useful for creating filter programs. The DPDK BPF library only implements extended BPF (eBPF). Add an function to convert from old to new.
The rte_bpf_convert function uses rte_malloc to put the resulting program in hugepage shared memory so it can be passed from a secondary process to a primary process.
The code to convert was originally done as part of the Linux kernel implementation then converted to a userspace program. Both authors have agreed that it is allowable to license this as BSD licensed in DPDK.
--- lib/bpf/bpf_convert.c | 570 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 606 insertions(+) create mode 100644 lib/bpf/bpf_convert.c
lib/bpf/bpf_convert.c new file +570 -0
@@ -0,0 +1,570 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2021 Microsoft Corporation +* +* Based on bpf_convert_filter() in the Linux kernel sources +* and filter2xdp. +* +* Licensed as BSD with permission original authors. +* Copyright (C) 2017 Tobias Klauser +* Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com +*/ + +#include <assert.h> +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <rte_common.h> +#include <rte_bpf.h> +#include <rte_log.h> +#include <rte_malloc.h> +#include <rte_errno.h> + +/* Workaround name conflicts with libpcap */ +#define bpf_validate (f, len) bpf_validate_libpcap(f, len) +#include <pcap/pcap.h> +#include <pcap/bpf.h> +#undef bpf_validate + +#include "bpf_impl.h" +#include "bpf_def.h" + +#ifndef BPF_MAXINSNS +#define BPF_MAXINSNS 4096 +#endif + +/* +* Linux socket filter uses negative absolute offsets to +* reference ancillary data. +* +*/ +#define SKF_AD_OFF (-0x1000) +#define SKF_AD_PROTOCOL 0 +#define SKF_AD_PKTTYPE 4 +#define SKF_AD_IFINDEX 8 +#define SKF_AD_NLATTR 12 +#define SKF_AD_NLATTR_NEST 16 +#define SKF_AD_MARK 20 +#define SKF_AD_QUEUE 24 +#define SKF_AD_HATYPE 28 +#define SKF_AD_RXHASH 32 +#define SKF_AD_CPU 36 +#define SKF_AD_ALU_XOR_X 40 +#define SKF_AD_VLAN_TAG 44 +#define SKF_AD_VLAN_TAG_PRESENT 48 +#define SKF_AD_PAY_OFFSET 52 +#define SKF_AD_RANDOM 56 +#define SKF_AD_VLAN_TPID 60 +#define SKF_AD_MAX 64 + +/* ArgX, context and stack frame pointer register positions. Note, +* Arg1, Arg2, Arg3, etc are used as argument mappings of function +* calls in BPF_CALL instruction. +*/ +#define BPF_REG_ARG1 EBPF_REG_1 +#define BPF_REG_ARG2 EBPF_REG_2 +#define BPF_REG_ARG3 EBPF_REG_3 +#define BPF_REG_ARG4 EBPF_REG_4 +#define BPF_REG_ARG5 EBPF_REG_5 +#define BPF_REG_CTX EBPF_REG_6 +#define BPF_REG_FP EBPF_REG_10 + +/* Additional register mappings for converted user programs. */ +#define BPF_REG_A EBPF_REG_0 +#define BPF_REG_X EBPF_REG_7 +#define BPF_REG_TMP EBPF_REG_8 + +/* Helper macros for filter block array initializers. */ + +/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ + +#define EBPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_ALU32_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ + +#define BPF_ALU32_IMM (OP ,DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov, dst_reg = src_reg */ + +#define BPF_MOV64_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_MOV32_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* Short form of mov, dst_reg = imm32 */ + +#define BPF_MOV32_IMM (DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */ + +#define BPF_MOV32_RAW (TYPE ,DST ,SRC ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_SRC(TYPE), \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = IMM }) + +/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ + +#define BPF_LD_ABS (SIZE ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Memory load, dst_reg = *(uint *) (src_reg + off16) */ + +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Memory store, *(uint *) (dst_reg + off16) = src_reg */ + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ + +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +/* Raw code statement block */ + +#define BPF_RAW_INSN (CODE ,DST ,SRC ,OFF ,IMM ) \ + ((struct ebpf_insn) { \ + .code = CODE, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = IMM }) + +/* Program exit */ + +#define BPF_EXIT_INSN () \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | EBPF_EXIT, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = 0 }) + +/* +* Placeholder to convert BPF extensions like length and VLAN tag +* If and when DPDK BPF supports them. +*/ +static bool convert_bpf_load (const struct bpf_insn *fp, + struct ebpf_insn **new_insnp __rte_unused) +{ +switch (fp->k) { +case SKF_AD_OFF +SKF_AD_PROTOCOL : +case SKF_AD_OFF +SKF_AD_PKTTYPE : +case SKF_AD_OFF +SKF_AD_IFINDEX : +case SKF_AD_OFF +SKF_AD_HATYPE : +case SKF_AD_OFF +SKF_AD_MARK : +case SKF_AD_OFF +SKF_AD_RXHASH : +case SKF_AD_OFF +SKF_AD_QUEUE : +case SKF_AD_OFF +SKF_AD_VLAN_TAG : +case SKF_AD_OFF +SKF_AD_VLAN_TAG_PRESENT : +case SKF_AD_OFF +SKF_AD_VLAN_TPID : +case SKF_AD_OFF +SKF_AD_PAY_OFFSET : +case SKF_AD_OFF +SKF_AD_NLATTR : +case SKF_AD_OFF +SKF_AD_NLATTR_NEST : +case SKF_AD_OFF +SKF_AD_CPU : +case SKF_AD_OFF +SKF_AD_RANDOM : +case SKF_AD_OFF +SKF_AD_ALU_XOR_X : +/* TODO: Not implemented yet */ +RTE_BPF_LOG (ERR ,"BPF extension LOAD ABS %u not supported\n" , + fp->k); +return true; +default : +return false; + } +} + +static int bpf_convert_filter (const struct bpf_insn *prog,size_t len, +struct ebpf_insn *new_prog,uint32_t *new_len) +{ +unsigned int pass =0 ; +size_t new_flen =0 , target, i; +struct ebpf_insn *new_insn; +const struct bpf_insn *fp; +int *addrs =NULL ; +uint8_t bpf_src; + +if (len >BPF_MAXINSNS ) { +RTE_BPF_LOG (ERR ,"%s: cBPF program too long (%zu insns)\n" , + __func__, len); +return -EINVAL ; + } + +/* On second pass, allocate the new program */ +if (new_prog) { + addrs =calloc (len,sizeof (*addrs)); +if (addrs ==NULL ) +return -ENOMEM ; + } + +do_pass: + new_insn = new_prog; + fp = prog; + +/* Classic BPF related prologue emission. */ +if (new_insn) { +/* Classic BPF expects A and X to be reset first. These need +* to be guaranteed to be the first two instructions. +*/ + *new_insn++ =EBPF_ALU64_REG (BPF_XOR ,BPF_REG_A ,BPF_REG_A ); + *new_insn++ =EBPF_ALU64_REG (BPF_XOR ,BPF_REG_X ,BPF_REG_X ); + +/* All programs must keep CTX in callee saved BPF_REG_CTX. +* In eBPF case it's done by the compiler, here we need to +* do this ourself. Initial CTX is present in BPF_REG_ARG1. +*/ + *new_insn++ =BPF_MOV64_REG (BPF_REG_CTX ,BPF_REG_ARG1 ); + }else { + new_insn +=3 ; + } + +for (i =0 ; i < len; fp++, i++) { +struct ebpf_insn tmp_insns[6 ] = { }; +struct ebpf_insn *insn = tmp_insns; + +if (addrs) + addrs[i] = new_insn - new_prog; + +switch (fp->code) { +/* Absolute loads are how classic BPF accesses skb */ +case BPF_LD |BPF_ABS |BPF_W : +case BPF_LD |BPF_ABS |BPF_H : +case BPF_LD |BPF_ABS |BPF_B : +if (convert_bpf_load (fp, &insn)) + goto err; + + *insn =BPF_RAW_INSN (fp->code,0 ,0 ,0 , fp->k); +break ; + +case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_X : +/* For cBPF, don't cause floating point exception */ + *insn++ =BPF_MOV32_REG (BPF_REG_X ,BPF_REG_X ); + *insn++ =BPF_JMP_IMM (EBPF_JNE ,BPF_REG_X ,0 ,2 ); + *insn++ =BPF_ALU32_REG (BPF_XOR ,BPF_REG_A ,BPF_REG_A ); + *insn++ =BPF_EXIT_INSN (); +/* fallthrough */ +case BPF_ALU |BPF_ADD |BPF_X : +case BPF_ALU |BPF_ADD |BPF_K : +case BPF_ALU |BPF_SUB |BPF_X : +case BPF_ALU |BPF_SUB |BPF_K : +case BPF_ALU |BPF_AND |BPF_X : +case BPF_ALU |BPF_AND |BPF_K : +case BPF_ALU |BPF_OR |BPF_X : +case BPF_ALU |BPF_OR |BPF_K : +case BPF_ALU |BPF_LSH |BPF_X : +case BPF_ALU |BPF_LSH |BPF_K : +case BPF_ALU |BPF_RSH |BPF_X : +case BPF_ALU |BPF_RSH |BPF_K : +case BPF_ALU |BPF_XOR |BPF_X : +case BPF_ALU |BPF_XOR |BPF_K : +case BPF_ALU |BPF_MUL |BPF_X : +case BPF_ALU |BPF_MUL |BPF_K : +case BPF_ALU |BPF_DIV |BPF_K : +case BPF_ALU |BPF_MOD |BPF_K : +case BPF_ALU |BPF_NEG : +case BPF_LD |BPF_IND |BPF_W : +case BPF_LD |BPF_IND |BPF_H : +case BPF_LD |BPF_IND |BPF_B : +/* All arithmetic insns map as-is. */ + *insn =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); +break ; + +/* Jump transformation cannot use BPF block macros +* everywhere as offset calculation and target updates +* require a bit more work than the rest, i.e. jump +* opcodes map as-is, but offsets need adjustment. +*/ + +#define BPF_EMIT_JMP \ + do { \ + if (target >= len) \ + goto err; \ + insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ +/* Adjust pc relative offset for 2nd or 3rd insn. */ \ + insn->off -= insn - tmp_insns; \ + }while (0 ) + +case BPF_JMP |BPF_JA : + target = i + fp->k +1 ; + insn->code = fp->code; +BPF_EMIT_JMP ; +break ; + +case BPF_JMP |BPF_JEQ |BPF_K : +case BPF_JMP |BPF_JEQ |BPF_X : +case BPF_JMP |BPF_JSET |BPF_K : +case BPF_JMP |BPF_JSET |BPF_X : +case BPF_JMP |BPF_JGT |BPF_K : +case BPF_JMP |BPF_JGT |BPF_X : +case BPF_JMP |BPF_JGE |BPF_K : +case BPF_JMP |BPF_JGE |BPF_X : +if (BPF_SRC (fp->code) ==BPF_K && (int ) fp->k <0 ) { +/* BPF immediates are signed, zero extend +* immediate into tmp register and use it +* in compare insn. +*/ + *insn++ =BPF_MOV32_IMM (BPF_REG_TMP , fp->k); + + insn->dst_reg =BPF_REG_A ; + insn->src_reg =BPF_REG_TMP ; + bpf_src =BPF_X ; + }else { + insn->dst_reg =BPF_REG_A ; + insn->imm = fp->k; + bpf_src =BPF_SRC (fp->code); + insn->src_reg = bpf_src ==BPF_X ?BPF_REG_X :0 ; + } + +/* Common case where 'jump_false' is next insn. */ +if (fp->jf ==0 ) { + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; + target = i + fp->jt +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Convert JEQ into JNE when 'jump_true' is next insn. */ +if (fp->jt ==0 &&BPF_OP (fp->code) ==BPF_JEQ ) { + insn->code =BPF_JMP |EBPF_JNE | bpf_src; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Other jumps are mapped into two insns: Jxx and JA. */ + target = i + fp->jt +1 ; + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; +BPF_EMIT_JMP ; + insn++; + + insn->code =BPF_JMP |BPF_JA ; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + +/* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ +case BPF_LDX |BPF_MSH |BPF_B : +/* tmp = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_TMP ,BPF_REG_A ); +/* A = BPF_R0 = *(u8 *) (skb->data + K) */ + *insn++ =BPF_LD_ABS (BPF_B , fp->k); +/* A &= 0xf */ + *insn++ =BPF_ALU32_IMM (BPF_AND ,BPF_REG_A ,0xf ); +/* A <<= 2 */ + *insn++ =BPF_ALU32_IMM (BPF_LSH ,BPF_REG_A ,2 ); +/* X = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +/* A = tmp */ + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_TMP ); +break ; + +/* RET_K is remaped into 2 insns. RET_A case doesn't need an +* extra mov as EBPF_REG_0 is already mapped into BPF_REG_A. +*/ +case BPF_RET |BPF_A : +case BPF_RET |BPF_K : +if (BPF_RVAL (fp->code) ==BPF_K ) { + *insn++ =BPF_MOV32_RAW (BPF_K ,EBPF_REG_0 , +0 , fp->k); + } + *insn =BPF_EXIT_INSN (); +break ; + +/* Store to stack. */ +case BPF_ST : +case BPF_STX : + *insn =BPF_STX_MEM (BPF_W ,BPF_REG_FP ,BPF_CLASS (fp->code) == +BPF_ST ?BPF_REG_A :BPF_REG_X , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* Load from stack. */ +case BPF_LD |BPF_MEM : +case BPF_LDX |BPF_MEM : + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_FP , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* A = K or X = K */ +case BPF_LD |BPF_IMM : +case BPF_LDX |BPF_IMM : + *insn =BPF_MOV32_IMM (BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X , fp->k); +break ; + +/* X = A */ +case BPF_MISC |BPF_TAX : + *insn =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +break ; + +/* A = X */ +case BPF_MISC |BPF_TXA : + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_X ); +break ; + +/* A = mbuf->len or X = mbuf->len */ +case BPF_LD |BPF_W |BPF_LEN : +case BPF_LDX |BPF_W |BPF_LEN : +/* BPF_ABS/BPF_IND implicitly expect mbuf ptr in R6 */ + + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_CTX , + offsetof(struct rte_mbuf , pkt_len)); +break ; + +/* Unknown instruction. */ +default : +RTE_BPF_LOG (ERR ,"%s: Unknown instruction!: %#x\n" , + __func__, fp->code); + goto err; + } + + insn++; +if (new_prog) +memcpy (new_insn, tmp_insns, +sizeof (*insn) * (insn - tmp_insns)); + new_insn += insn - tmp_insns; + } + +if (!new_prog) { +/* Only calculating new length. */ + *new_len = new_insn - new_prog; +return 0 ; + } + + pass++; +if ((ptrdiff_t )new_flen != new_insn - new_prog) { + new_flen = new_insn - new_prog; +if (pass >2 ) + goto err; + goto do_pass; + } + +free (addrs); +assert (*new_len == new_flen); + +return 0 ; +err: +free (addrs); +return -1 ; +} + +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog) +{ +struct rte_bpf_prm *prm =NULL ; +struct ebpf_insn *ebpf =NULL ; +uint32_t ebpf_len =0 ; +int ret; + +if (prog ==NULL ) { +RTE_BPF_LOG (ERR ,"%s: NULL program\n" , __func__); + rte_errno =EINVAL ; +return NULL ; + } + +/* 1st pass: calculate the eBPF program length */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len,NULL , &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot get eBPF length\n" , __func__); + rte_errno = -ret; +return NULL ; + } + +RTE_BPF_LOG (DEBUG ,"%s: prog len cBPF=%u -> eBPF=%u\n" , + __func__, prog->bf_len, ebpf_len); + + prm =rte_zmalloc ("bpf_filter" , +sizeof (*prm) + ebpf_len *sizeof (*ebpf),0 ); +if (prm ==NULL ) { + rte_errno =ENOMEM ; +return NULL ; + } + +/* The EPBF instructions in this case are right after the header */ + ebpf = (void *)(prm +1 ); + +/* 2nd pass: remap cBPF to eBPF instructions */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot convert cBPF to eBPF\n" , __func__); +free (prm); + rte_errno = -ret; +return NULL ; + } + + prm->ins = ebpf; + prm->nb_ins = ebpf_len; + +/* Classic BPF programs use mbufs */ + prm->prog_arg.type =RTE_BPF_ARG_PTR_MBUF ; + prm->prog_arg.size =sizeof (struct rte_mbuf ); + +return prm; +}
lib/bpf/meson.build +5 -0
@@ -25,3 +25,8 @@ if dep.found() sources += files('bpf_load_elf.c') ext_deps += dep endif + +if dpdk_conf.has('RTE_PORT_PCAP') + sources += files('bpf_convert.c') + ext_deps += pcap_dep +endif
lib/bpf/rte_bpf.h +25 -0
@@ -198,6 +198,31 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +#ifdef RTE_PORT_PCAP + +struct bpf_program ; + +/** +* Convert a Classic BPF program from libpcap into a DPDK BPF code. +* +* @param prog +* Classic BPF program from pcap_compile(). +* @param prm +* Result Extended BPF program. +* @return +* Pointer to BPF program (allocated with *rte_malloc*) +* that is used in future BPF operations, +* or NULL on error, with error code set in rte_errno. +* Possible rte_errno errors include: +* - EINVAL - invalid parameter passed to function +* - ENOMEM - can't reserve enough memory +*/ +__rte_experimental +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog); + +#endif + #ifdef __cplusplus }#endif
lib/bpf/version.map +6 -0
@@ -14,3 +14,9 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_bpf_convert; +};
Signature · 1 line
2.30.2
PATCH v5 4/9 bpf: add function to dump eBPF instructions
In reply to Stephen HemmingerWhen debugging converted (and other) programs it is useful to see disassembled eBPF output.
--- lib/bpf/bpf_dump.c | 118 ++++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 ++++++ lib/bpf/version.map | 1 + 4 files changed, 134 insertions(+) create mode 100644 lib/bpf/bpf_dump.c
lib/bpf/bpf_dump.c new file +118 -0
@@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright (c) 2021 Stephen Hemminger +* Based on filter2xdp +* Copyright (C) 2017 Tobias Klauser +*/ + +#include <stdio.h> +#include <stdint.h> + +#include "rte_bpf.h" + +#define BPF_OP_INDEX (x) (BPF_OP(x) >> 4) +#define BPF_SIZE_INDEX (x) (BPF_SIZE(x) >> 3) + +static const char *const class_tbl[] = { + [BPF_LD ] ="ld" , [BPF_LDX ] ="ldx" , [BPF_ST ] ="st" , + [BPF_STX ] ="stx" , [BPF_ALU ] ="alu" , [BPF_JMP ] ="jmp" , + [BPF_RET ] ="ret" , [BPF_MISC ] ="alu64" , +}; + +static const char *const alu_op_tbl[16 ] = { + [BPF_ADD >>4 ] ="add" , [BPF_SUB >>4 ] ="sub" , + [BPF_MUL >>4 ] ="mul" , [BPF_DIV >>4 ] ="div" , + [BPF_OR >>4 ] ="or" , [BPF_AND >>4 ] ="and" , + [BPF_LSH >>4 ] ="lsh" , [BPF_RSH >>4 ] ="rsh" , + [BPF_NEG >>4 ] ="neg" , [BPF_MOD >>4 ] ="mod" , + [BPF_XOR >>4 ] ="xor" , [EBPF_MOV >>4 ] ="mov" , + [EBPF_ARSH >>4 ] ="arsh" , [EBPF_END >>4 ] ="endian" , +}; + +static const char *const size_tbl[] = { + [BPF_W >>3 ] ="w" , + [BPF_H >>3 ] ="h" , + [BPF_B >>3 ] ="b" , + [EBPF_DW >>3 ] ="dw" , +}; + +static const char *const jump_tbl[16 ] = { + [BPF_JA >>4 ] ="ja" , [BPF_JEQ >>4 ] ="jeq" , + [BPF_JGT >>4 ] ="jgt" , [BPF_JGE >>4 ] ="jge" , + [BPF_JSET >>4 ] ="jset" , [EBPF_JNE >>4 ] ="jne" , + [EBPF_JSGT >>4 ] ="jsgt" , [EBPF_JSGE >>4 ] ="jsge" , + [EBPF_CALL >>4 ] ="call" , [EBPF_EXIT >>4 ] ="exit" , +}; + +static void ebpf_dump (FILE *f,const struct ebpf_insn insn,size_t n) +{ +const char *op, *postfix ="" ; +uint8_t cls =BPF_CLASS (insn.code); + +fprintf (f," L%zu:\t" , n); + +switch (cls) { +default : +fprintf (f,"unimp 0x%x // class: %s\n" , insn.code, + class_tbl[cls]); +break ; +case BPF_ALU : + postfix ="32" ; +/* fall through */ +case EBPF_ALU64 : + op = alu_op_tbl[BPF_OP_INDEX (insn.code)]; +if (BPF_SRC (insn.code) ==BPF_X ) +fprintf (f,"%s%s r%u, r%u\n" , op, postfix, insn.dst_reg, + insn.src_reg); +else +fprintf (f,"%s%s r%u, #0x%x\n" , op, postfix, + insn.dst_reg, insn.imm); +break ; +case BPF_LD : + op ="ld" ; + postfix = size_tbl[BPF_SIZE_INDEX (insn.code)]; +if (BPF_MODE (insn.code) ==BPF_IMM ) +fprintf (f,"%s%s r%d, #0x%x\n" , op, postfix, + insn.dst_reg, insn.imm); +else if (BPF_MODE (insn.code) ==BPF_ABS ) +fprintf (f,"%s%s r%d, [%d]\n" , op, postfix, + insn.dst_reg, insn.imm); +else if (BPF_MODE (insn.code) ==BPF_IND ) +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, + insn.dst_reg, insn.src_reg, insn.imm); +else +fprintf (f,"// BUG: LD opcode 0x%02x in eBPF insns\n" , + insn.code); +break ; +case BPF_LDX : + op ="ldx" ; + postfix = size_tbl[BPF_SIZE_INDEX (insn.code)]; +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, insn.dst_reg, + insn.src_reg, insn.off); +break ; +#define L (pc, off) ((int)(pc) + 1 + (off)) +case BPF_JMP : + op = jump_tbl[BPF_OP_INDEX (insn.code)]; +if (op ==NULL ) +fprintf (f,"invalid jump opcode: %#x\n" , insn.code); +else if (BPF_OP (insn.code) ==BPF_JA ) +fprintf (f,"%s L%d\n" , op,L (n, insn.off)); +else if (BPF_OP (insn.code) ==EBPF_EXIT ) +fprintf (f,"%s\n" , op); +else +fprintf (f,"%s r%u, #0x%x, L%d\n" , op, insn.dst_reg, + insn.imm,L (n, insn.off)); +break ; +case BPF_RET : +fprintf (f,"// BUG: RET opcode 0x%02x in eBPF insns\n" , + insn.code); +break ; + } +} + +void rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len) +{ +uint32_t i; + +for (i =0 ; i < len; ++i) +ebpf_dump (f, buf[i], i); +}
lib/bpf/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2018 Intel Corporation sources = files('bpf.c', + 'bpf_dump.c', 'bpf_exec.c', 'bpf_load.c', 'bpf_pkt.c',
lib/bpf/rte_bpf.h +14 -0
@@ -198,6 +198,20 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +/** +* Dump epf instructions to a file. +* +* @param f +* A pointer to a file for output +* @param buf +* A pointer to BPF instructions +* @param len +* Number of BPF instructions to dump. +*/ +__rte_experimental +void +rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len); +#ifdef RTE_PORT_PCAP struct bpf_program ;
lib/bpf/version.map +1 -0
@@ -19,4 +19,5 @@ EXPERIMENTAL { global: rte_bpf_convert; + rte_bpf_dump; };
Signature · 1 line
2.30.2
PATCH v5 5/9 lib: pdump is not supported on Windows
In reply to Stephen HemmingerThe original version of the pdump library was building on Windows, but it was useless since the pdump utility was not being built.
The new version of pdump with filtering now has dependency on bpf. But bpf library is not available on Windows.
For now, just stop trying to build pdump on Windows. Eventually, bpf library, pdump library, dumpcap tool, and pdump tool can be converted to work on Windows.
--- lib/meson.build | 1 - 1 file changed, 1 deletion(-)
lib/meson.build +0 -1
@@ -85,7 +85,6 @@ if is_windows 'gro', 'gso', 'latencystats', - 'pdump', ] # only supported libraries for windows endif
Signature · 1 line
2.30.2
PATCH v5 6/9 pdump: support pcapng and filtering
In reply to Stephen HemmingerThis enhances the DPDK pdump library to support new pcapng format and filtering via BPF.
The internal client/server protocol is changed to support two versions: the original pdump basic version and a new pcapng version.
The internal version number (not part of exposed API or ABI) is intentionally increased to cause any attempt to try mismatched primary/secondary process to fail.
Add new API to do allow filtering of captured packets with DPDK BPF (eBPF) filter program. It keeps statistics
on packets captured, filtered, and missed (because ring was full).
--- lib/meson.build | 4 +- lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 437 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 110 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 435 insertions(+), 126 deletions(-)
lib/meson.build +2 -2
@@ -26,6 +26,7 @@ libraries = [ 'timer', # eventdev depends on this 'acl', 'bbdev', + 'bpf', 'bitratestats', 'cfgfile', 'compressdev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'rib',
@@ -55,10 +55,10 @@ libraries = [ 'ipsec', # ipsec lib depends on net, crypto and security 'fib', #fib lib depends on rib 'port', # pkt framework libs which use other libs from above + 'pdump', # pdump lib depends on bpf pcapng 'table', 'pipeline', 'flow_classify', # flow_classify lib depends on pkt framework table lib - 'bpf', 'graph', 'node', ]
lib/pdump/meson.build +1 -1
@@ -3,4 +3,4 @@ sources = files('rte_pdump.c') headers = files('rte_pdump.h') -deps += ['ethdev'] +deps += ['ethdev', 'bpf', 'pcapng']
lib/pdump/rte_pdump.c +317 -120
644 lines not on this page · raw message
lib/pdump/rte_pdump.h +107 -3
152 lines not on this page · raw message
lib/pdump/version.map +8 -0
@@ -10,3 +10,11 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_pdump_enable_bpf; + rte_pdump_enable_bpf_by_deviceid; + rte_pdump_stats; +};
Signature · 1 line
2.30.2
PATCH v5 7/9 app/dumpcap: add new packet capture application
In reply to Stephen HemmingerThis is a new packet capture application to replace existing pdump. The new application works like Wireshark dumpcap program and supports the pdump API features.
It is not complete yet some features such as filtering are not implemented.
--- app/dumpcap/main.c | 827 ++++++++++++++++++++++++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + 3 files changed, 844 insertions(+) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build
app/dumpcap/main.c new file +827 -0
@@ -0,0 +1,827 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019-2020 Microsoft Corporation +* +* DPDK application to dump network traffic +* This is designed to look and act like the Wireshark +* dumpcap program. +*/ + +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <inttypes.h> +#include <limits.h> +#include <signal.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/queue.h> +#include <sys/types.h> +#include <sys/utsname.h> +#include <time.h> +#include <unistd.h> + +#include <rte_alarm.h> +#include <rte_bpf.h> +#include <rte_config.h> +#include <rte_debug.h> +#include <rte_eal.h> +#include <rte_errno.h> +#include <rte_ethdev.h> +#include <rte_lcore.h> +#include <rte_malloc.h> +#include <rte_mbuf.h> +#include <rte_mempool.h> +#include <rte_pcapng.h> +#include <rte_pdump.h> +#include <rte_ring.h> +#include <rte_string_fns.h> +#include <rte_time.h> +#include <rte_version.h> + +#include <pcap/pcap.h> +#include <pcap/bpf.h> + +#define RING_NAME "capture-ring" +#define MONITOR_INTERVAL (500 * 1000) +#define MBUF_POOL_CACHE_SIZE 32 +#define BURST_SIZE 32 +#define SLEEP_THRESHOLD 1000 + +/* command line flags */ +static const char *progname; +static bool quit_signal; +static bool group_read; +static bool quiet; +static bool promiscuous_mode = true; +static bool use_pcapng = true; +static char *output_name; +static const char *filter_str; +static unsigned int ring_size =2048 ; +static const char *capture_comment; +static uint32_t snaplen =RTE_MBUF_DEFAULT_BUF_SIZE ; +static bool dump_bpf; +static struct { +uint64_t duration;/* nanoseconds */ +unsigned long packets;/* number of packets in file */ +size_t size;/* file size (bytes) */ +} stop; + +/* Running state */ +static struct rte_bpf_prm *bpf_prm; +static uint64_t start_time, end_time; +static uint64_t packets_received; +static size_t file_size; + +struct interface { +TAILQ_ENTRY (interface) next; +uint16_t port; +char name[RTE_ETH_NAME_MAX_LEN ]; + +struct rte_rxtx_callback *rx_cb[RTE_MAX_QUEUES_PER_PORT ]; +}; + +TAILQ_HEAD (interface_list, interface); +static struct interface_list interfaces =TAILQ_HEAD_INITIALIZER (interfaces); +static struct interface *port2intf[RTE_MAX_ETHPORTS ]; + +static void usage (void ) +{ +printf ("Usage: %s [options] ...\n\n" , progname); +printf ("Capture Interface:\n" +" -i <interface> name or port index of interface\n" +" -f <capture filter> packet filter in libpcap filter syntax\n" ); +printf (" -s <snaplen>, --snapshot-length <snaplen>\n" +" packet snapshot length (def: %u)\n" , +RTE_MBUF_DEFAULT_BUF_SIZE ); +printf (" -p, --no-promiscuous-mode\n" +" don't capture in promiscuous mode\n" +" -D, --list-interfaces print list of interfaces and exit\n" +" -d print generated BPF code for capture filter\n" +"\n" +"Stop conditions:\n" +" -c <packet count> stop after n packets (def: infinite)\n" +" -a <autostop cond.> ..., --autostop <autostop cond.> ...\n" +" duration:NUM - stop after NUM seconds\n" +" filesize:NUM - stop this file after NUM kB\n" +" packets:NUM - stop after NUM packets\n" +"Output (files):\n" +" -w <filename> name of file to save (def: tempfile)\n" +" -g enable group read access on the output file(s)\n" +" -n use pcapng format instead of pcap (default)\n" +" -P use libpcap format instead of pcapng\n" +" --capture-comment <comment>\n" +" add a capture comment to the output file\n" +"\n" +"Miscellaneous:\n" +" -q don't report packet capture counts\n" +" -v, --version print version information and exit\n" +" -h, --help display this help and exit\n" +"\n" +"Use Ctrl-C to stop capturing at any time.\n" ); +} + +static const char *version (void ) +{ +static char str[128 ]; + +snprintf (str,sizeof (str), +"%s 1.0 (%s)\n" , progname,rte_version ()); +return str; +} + +/* Parse numeric argument from command line */ +static unsigned long get_uint (const char *arg,const char *name, +unsigned int limit) +{ +unsigned long u; +char *endp; + + u =strtoul (arg, &endp,0 ); +if (*arg =='\0' || *endp !='\0' ) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is not a valid number\n" , + name, arg); +if (limit && u > limit) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is too large (greater than %u)\n" , + name, arg, limit); + +return u; +} + +/* Set auto stop values */ +static void auto_stop (char *opt) +{ +char *value, *endp; + + value =strchr (opt,':' ); +if (value ==NULL ) +rte_exit (EXIT_FAILURE , +"Missing colon in auto stop parameter\n" ); + + *value++ ='\0' ; +if (strcmp (opt,"duration" ) ==0 ) { +double interval =strtod (value, &endp); + +if (*value =='\0' || *endp !='\0' || interval <=0 ) +rte_exit (EXIT_FAILURE , +"Invalid duration \"%s\"\n" , value); + stop.duration =NSEC_PER_SEC * interval; + }else if (strcmp (opt,"filesize" ) ==0 ) { + stop.size =get_uint (value,"filesize" ,0 ) *1024 ; + }else if (strcmp (opt,"packets" ) ==0 ) { + stop.packets =get_uint (value,"packets" ,0 ); + }else { +rte_exit (EXIT_FAILURE , +"Unknown autostop parameter \"%s\"\n" , opt); + } +} + +/* Add interface to list of interfaces to capture */ +static void add_interface (uint16_t port,const char *name) +{ +struct interface *intf; + + intf =malloc (sizeof (*intf)); +if (!intf) +rte_exit (EXIT_FAILURE ,"no memory for interface\n" ); + +memset (intf,0 ,sizeof (*intf)); +strlcpy (intf->name, name,sizeof (intf->name)); + +printf ("Capturing on '%s'\n" , name); + + port2intf[port] = intf; +TAILQ_INSERT_TAIL (&interfaces, intf, next); +} + +/* Select all valid DPDK interfaces */ +static void select_all_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); + } +} + +/* +* Choose interface to capture if no -i option given. +* Select the first DPDK port, this matches what dumpcap does. +*/ +static void set_default_interface (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); +return ; + } +rte_exit (EXIT_FAILURE ,"No usable interfaces found\n" ); +} + +/* Lookup interface by name or port and add it to the list */ +static void select_interface (const char *arg) +{ +uint16_t port; + +if (strcmp (arg,"*" )) +select_all_interfaces (); +else if (rte_eth_dev_get_port_by_name (arg, &port) ==0 ) +add_interface (port, arg); +else { +char name[RTE_ETH_NAME_MAX_LEN ]; + + port =get_uint (arg,"port_number" ,UINT16_MAX ); +if (rte_eth_dev_get_name_by_port (port, name) <0 ) +rte_exit (EXIT_FAILURE ,"Invalid port number %u\n" , + port); +add_interface (port, name); + } +} + +/* Display list of possible interfaces that can be used. */ +static void show_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +printf ("%u. %s\n" , p, name); + } +} + +static void compile_filter (void ) +{ +struct bpf_program bf; +pcap_t *pcap; + + pcap =pcap_open_dead (DLT_EN10MB , snaplen); +if (!pcap) +rte_exit (EXIT_FAILURE ,"can not open pcap\n" ); + +if (pcap_compile (pcap, &bf, filter_str, +1 ,PCAP_NETMASK_UNKNOWN ) !=0 ) +rte_exit (EXIT_FAILURE ,"pcap filter string not valid (%s)\n" , +pcap_geterr (pcap)); + + bpf_prm =rte_bpf_convert (&bf); +if (bpf_prm ==NULL ) +rte_exit (EXIT_FAILURE , +"bpf convert failed\n" ); + +if (dump_bpf) { +printf ("cBPF program (%u insns)\n" , bf.bf_len); +bpf_dump (&bf,1 ); +printf ("\neBPF program (%u insns)\n" , bpf_prm->nb_ins); +rte_bpf_dump (stdout, bpf_prm->ins, bpf_prm->nb_ins); +exit (0 ); + } + +/* Don't care about original program any more */ +pcap_freecode (&bf); +pcap_close (pcap); +} + +/* +* Parse command line options. +* These are chosen to be similar to dumpcap command. +*/ +static void parse_opts (int argc,char **argv) +{ +static const struct option long_options[] = { + {"autostop" , required_argument,NULL ,'a' }, + {"capture-comment" , required_argument,NULL ,0 }, + {"help" , no_argument,NULL ,'h' }, + {"interface" , required_argument,NULL ,'i' }, + {"list-interfaces" , no_argument,NULL ,'D' }, + {"no-promiscuous-mode" , no_argument,NULL ,'p' }, + {"output-file" , required_argument,NULL ,'w' }, + {"ring-buffer" , required_argument,NULL ,'b' }, + {"snapshot-length" , required_argument,NULL ,'s' }, + {"version" , no_argument,NULL ,'v' }, + {NULL }, + }; +int option_index, c; + +for (;;) { + c =getopt_long (argc, argv,"a:b:c:dDf:ghi:nN:pPqs:vw:" , + long_options, &option_index); +if (c ==-1 ) +break ; + +switch (c) { +case 0 : +switch (option_index) { +case 0 : + capture_comment = optarg; +break ; +default : +usage (); +exit (1 ); + } +break ; +case 'a' : +auto_stop (optarg); +break ; +case 'b' : +rte_exit (EXIT_FAILURE , +"multiple files not implemented\n" ); +break ; +case 'c' : + stop.packets =get_uint (optarg,"packet_count" ,0 ); +break ; +case 'd' : + dump_bpf = true; +break ; +case 'D' : +show_interfaces (); +exit (0 ); +case 'f' : + filter_str = optarg; +break ; +case 'g' : + group_read = true; +break ; +case 'h' : +printf ("%s\n\n" ,version ()); +usage (); +exit (0 ); +case 'i' : +select_interface (optarg); +break ; +case 'n' : + use_pcapng = true; +break ; +case 'N' : + ring_size =get_uint (optarg,"packet_limit" ,0 ); +break ; +case 'p' : + promiscuous_mode = false; +break ; +case 'P' : + use_pcapng = false; +break ; +case 'q' : + quiet = true; +break ; +case 's' : + snaplen =get_uint (optarg,"snap_len" ,0 ); +break ; +case 'w' : + output_name = optarg; +break ; +case 'v' : +printf ("%s\n" ,version ()); +exit (0 ); +default : +fprintf (stderr,"Invalid option: %s\n" , + argv[optind -1 ]); +usage (); +exit (1 ); + } + } +} + +static void +signal_handler(int sig_num __rte_unused) +{ +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); +} + +/* Return the time since 1/1/1970 in nanoseconds */ +static uint64_t create_timestamp (void ) +{ +struct timespec now; + +clock_gettime (CLOCK_MONOTONIC , &now); +return rte_timespec_to_ns (&now); +} + +static void +cleanup_pdump_resources (void ) +{ +struct interface *intf; + + TAILQ_FOREACH(intf, &interfaces, next) { +rte_pdump_disable (intf->port, +RTE_PDUMP_ALL_QUEUES ,RTE_PDUMP_FLAG_RXTX ); +if (promiscuous_mode) +rte_eth_promiscuous_disable (intf->port); + } +} + +/* Alarm signal handler, used to check that primary process */ +static void +monitor_primary(void *arg __rte_unused) +{ +if (__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) +return ; + +if (rte_eal_primary_proc_alive (NULL )) { +rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); + }else { +fprintf (stderr, +"Primary process is no longer active, exiting...\n" ); +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); + } +} + +/* Setup handler to check when primary exits. */ +static void +enable_primary_monitor (void ) +{ +int ret; + +/* Once primary exits, so will pdump. */ + ret =rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to enable monitor:%d\n" , ret); +} + +static void +disable_primary_monitor (void ) +{ +int ret; + + ret =rte_eal_alarm_cancel (monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to disable monitor:%d\n" , ret); +} + +static void +report_packet_stats (rte_pcapng_t *out) +{ +struct rte_pdump_stats pdump_stats; +struct interface *intf; +uint64_t ifrecv, ifdrop; +double percent; + +fputc ('\n' , stderr); + TAILQ_FOREACH(intf, &interfaces, next) { +if (rte_pdump_stats (intf->port, &pdump_stats) <0 ) +continue ; + +/* do what Wiretap does */ + ifrecv = pdump_stats.accepted + pdump_stats.filtered; + ifdrop = pdump_stats.nombuf + pdump_stats.ringfull; + +if (use_pcapng) +rte_pcapng_write_stats (out, intf->port,NULL , + start_time, end_time, + ifrecv, ifdrop); + +if (ifrecv ==0 ) + percent =0 ; +else + percent =100. * ifrecv / (ifrecv + ifdrop); + +fprintf (stderr, +"Packets received/dropped on interface '%s': " +"%" PRIu64"/%" PRIu64" (%.1f)\n" , + intf->name, ifrecv, ifdrop, percent); + } +} + +/* +* Start DPDK EAL with arguments. +* Unlike most DPDK programs, this application does not use the +* typical EAL command line arguments. +* We don't want to expose all the DPDK internals to the user. +*/ +static void dpdk_init (void ) +{ +const char *args[] = { + progname,"--proc-type" ,"secondary" , +"--log-level" ,"bpf:debug" + + }; +const int eal_argc =RTE_DIM (args); +char **eal_argv; +unsigned int i; + +/* DPDK API requires mutable versions of command line arguments. */ + eal_argv =calloc (eal_argc +1 ,sizeof (char *)); +if (eal_argv ==NULL ) +rte_panic ("No memory\n" ); + +for (i =0 ; i <RTE_DIM (args); i++) + eal_argv[i] =strdup (args[i]); + +if (rte_eal_init (eal_argc, eal_argv) <0 ) +rte_exit (EXIT_FAILURE ,"EAL init failed: is primary process running?\n" ); + +if (rte_eth_dev_count_avail () ==0 ) +rte_exit (EXIT_FAILURE ,"No Ethernet ports found\n" ); +} + +/* Create packet ring shared between callbacks and process */ +static struct rte_ring *create_ring (void ) +{ +struct rte_ring *ring; +size_t size, log2; + +/* Find next power of 2 >= size. */ + size = ring_size; + log2 =sizeof (size) *8 -__builtin_clzl (size -1 ); + size =1u << log2; + +if (size != ring_size) { +fprintf (stderr,"Ring size %u rounded up to %zu\n" , + ring_size, size); + ring_size = size; + } + + ring =rte_ring_lookup (RING_NAME ); +if (ring ==NULL ) { + ring =rte_ring_create (RING_NAME , ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); + } +return ring; +} + +static struct rte_mempool *create_mempool (void ) +{ +static const char pool_name[] ="capture_mbufs" ; +size_t num_mbufs =2 * ring_size; +struct rte_mempool *mp; + + mp =rte_mempool_lookup (pool_name); +if (mp) +return mp; + + mp =rte_pktmbuf_pool_create_by_ops (pool_name, num_mbufs, +MBUF_POOL_CACHE_SIZE ,0 , +rte_pcapng_mbuf_size (snaplen), +rte_socket_id (),"ring_mp_sc" ); +if (mp ==NULL ) +rte_exit (EXIT_FAILURE , +"Mempool (%s) creation failed: %s\n" , pool_name, +rte_strerror (rte_errno)); + +return mp; +} + +static void *create_output (void ) +{ +static char tmp_path[PATH_MAX ]; +struct utsname uts; +char os[256 ]; +int fd; + +/* If no filename specified make a tempfile name */ +if (output_name ==NULL ) { +struct interface *intf; +struct tm *tm; +time_t now; +char ts[32 ]; + + intf =TAILQ_FIRST (&interfaces); + now =time (NULL ); + tm =localtime (&now); +if (!tm) +rte_panic ("localtime failed\n" ); + +strftime (ts,sizeof (ts),"%Y%m%d%H%M%S" , tm); + +snprintf (tmp_path,sizeof (tmp_path), +"/tmp/%s_%u_%s_%s.%s" , + progname, intf->port, intf->name, ts, + use_pcapng ?"pcapng" :"pcap" ); + output_name = tmp_path; + } + +if (strcmp (output_name,"-" ) ==0 ) + fd =STDOUT_FILENO ; +else { +mode_t mode = group_read ?0640 :0600 ; + + fd =open (output_name,O_WRONLY |O_CREAT , mode); +if (fd <0 ) +rte_exit (EXIT_FAILURE ,"Can not open \"%s\": %s\n" , + output_name,strerror (errno)); + } + +if (use_pcapng) { +rte_pcapng_t *pcapng; + +if (uname (&uts) <0 ) +strcpy (os,"unknown" ); +else +snprintf (os,sizeof (os),"%s %s" , + uts.sysname, uts.release); + + pcapng =rte_pcapng_fdopen (fd, os,NULL ,version (), capture_comment); +if (pcapng ==NULL ) +rte_exit (EXIT_FAILURE ,"pcapng_fdopen failed: %s\n" , +strerror (rte_errno)); +return pcapng; + }else { +pcap_dumper_t *dumper; +pcap_t *pcap; + + pcap =pcap_open_dead_with_tstamp_precision (DLT_EN10MB , snaplen, +PCAP_TSTAMP_PRECISION_NANO ); +if (pcap ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_open_dead failed\n" ); + + dumper =pcap_dump_fopen (pcap,fdopen (fd,"w" )); +if (dumper ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_dump_fopen failed: %s\n" , +pcap_geterr (pcap)); +return dumper; + } +} + +static void enable_pdump (struct rte_ring *r,struct rte_mempool *mp) +{ +struct interface *intf; +uint32_t flags; +int ret; + + flags =RTE_PDUMP_FLAG_RXTX ; +if (use_pcapng) + flags |=RTE_PDUMP_FLAG_PCAPNG ; + + TAILQ_FOREACH(intf, &interfaces, next) { +if (promiscuous_mode) +rte_eth_promiscuous_enable (intf->port); + + ret =rte_pdump_enable_bpf (intf->port,RTE_PDUMP_ALL_QUEUES , + flags, snaplen, + r, mp, bpf_prm); +if (ret <0 ) +rte_exit (EXIT_FAILURE , +"Packet dump enable failed: %s\n" , +rte_strerror (-ret)); + } +} + +/* +* Show current count of captured packets +* with backspaces to overwrite last value. +*/ +static void show_count (uint64_t count) +{ +unsigned int i; +static unsigned int bt; + +for (i =0 ; i < bt; i++) +fputc ('\b' , stderr); + + bt =fprintf (stderr,"%" PRIu64" " , count); +} + +/* Write multiple packets in older pcap format */ +static ssize_t +pcap_write_packets (pcap_dumper_t *dumper, +struct rte_mbuf *pkts[],uint16_t n) +{ +uint8_t temp_data[snaplen]; +struct pcap_pkthdr header; +uint16_t i; +size_t total =0 ; + +gettimeofday (&header.ts,NULL ); + +for (i =0 ; i < n; i++) { +struct rte_mbuf *m = pkts[i]; + + header.len =rte_pktmbuf_pkt_len (m); + header.caplen =RTE_MIN (header.len, snaplen); + +pcap_dump ((u_char *)dumper, &header, +rte_pktmbuf_read (m,0 , header.caplen, temp_data)); + + total +=sizeof (header) + header.len; + } + +return total; +} + +/* Process all packets in ring and dump to capture file */ +static int process_ring (void *out,struct rte_ring *r) +{ +struct rte_mbuf *pkts[BURST_SIZE ]; +unsigned int avail, n; +static unsigned int empty_count; +ssize_t written; + + n =rte_ring_sc_dequeue_burst (r, (void **) pkts,BURST_SIZE , + &avail); +if (n ==0 ) { +/* don't consume endless amounts of cpu if idle */ +if (empty_count <SLEEP_THRESHOLD ) + ++empty_count; +else +usleep (10 ); +return 0 ; + } + + empty_count = (avail ==0 ); + +if (use_pcapng) + written =rte_pcapng_write_packets (out, pkts, n); +else + written =pcap_write_packets (out, pkts, n); + +rte_pktmbuf_free_bulk (pkts, n); + +if (written <0 ) +return -1 ; + + file_size += written; + packets_received += n; +if (!quiet) +show_count (packets_received); + +return 0 ; +} + +int main (int argc,char **argv) +{ +struct rte_ring *r; +struct rte_mempool *mp; +void *out; + + progname = argv[0 ]; + +dpdk_init (); +parse_opts (argc, argv); + +if (filter_str) +compile_filter (); + +if (TAILQ_EMPTY (&interfaces)) +set_default_interface (); + + r =create_ring (); + mp =create_mempool (); + out =create_output (); +if (out ==NULL ) +rte_exit (EXIT_FAILURE ,"can not open output file: %s\n" , +rte_strerror (rte_errno)); + + start_time =create_timestamp (); +enable_pdump (r, mp); + +signal (SIGINT , signal_handler); +signal (SIGPIPE ,SIG_IGN ); + +enable_primary_monitor (); + +if (!quiet) { +fprintf (stderr,"Packets captured: " ); +show_count (0 ); + } + +while (!__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) { +if (process_ring (out, r) <0 ) { +fprintf (stderr,"pcapng file write failed; %s\n" , +strerror (errno)); +break ; + } + +if (stop.size && file_size >= stop.size) +break ; + +if (stop.packets && packets_received >= stop.packets) +break ; + +if (stop.duration !=0 && +create_timestamp () - start_time > stop.duration) +break ; + } + + end_time =create_timestamp (); +disable_primary_monitor (); + +if (rte_eal_primary_proc_alive (NULL )) +report_packet_stats (out); + +if (use_pcapng) +rte_pcapng_close (out); +else +pcap_dump_close (out); + +cleanup_pdump_resources (); +rte_free (bpf_filter); +rte_ring_free (r); +rte_mempool_free (mp); + +return rte_eal_cleanup () ?EXIT_FAILURE :0 ; +}
app/dumpcap/meson.build new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +if not dpdk_conf.has('RTE_PORT_PCAP') + build = false + reason = 'missing dependency, "libpcap"' +endif + +if is_windows + build = false + reason = 'not supported on Windows' + subdir_done() +endif + +sources = files('main.c') +deps += ['ethdev', 'pdump', 'pcapng', 'bpf']
app/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2017-2019 Intel Corporation apps = [ + 'dumpcap', 'pdump', 'proc-info', 'test-acl',
Signature · 1 line
2.30.2
PATCH v5 8/9 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 86 +++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 228 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -62,6 +62,16 @@ New Features * Added bus-level parsing of the devargs syntax. * Kept compatibility with the legacy syntax as parsing fallback. +* **Enhance Packet capture.** + + * New dpdk-dumpcap program that has most of the features of the + wireshark dumpcap utility including capture of multiple interfaces, + stopping after number of bytes, packets. + * New library for writing pcapng packet capture files. + * Enhancement to the pdump library to support: + * Packet filter with BPF. + * Pcapng format with timestamps and meta-data. + * Fixes packet capture with stripped VLAN tags. Removed Items -------------
doc/guides/tools/dumpcap.rst new file +86 -0
86 lines not on this page · raw message
doc/guides/tools/index.rst +1 -0
@@ -8,6 +8,7 @@ DPDK Tools User Guides :maxdepth: 2 :numbered: + dumpcap proc_info pdump pmdinfo
Signature · 1 line
2.30.2
PATCH v5 9/9 MAINTAINERS: add entry for new pcapng and dumper
In reply to Stephen HemmingerClaim responsibility for the new code.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -1429,6 +1429,12 @@ F: app/test/test_pdump.* F: app/pdump/ F: doc/guides/tools/pdump.rst +Packet dump +M: Stephen Hemminger <[email protected]> +F: lib/pcapng/ +F: doc/guides/prog_guide/pcapng_lib.rst +F: app/dumpcap/ +F: doc/guides/tools/dumpcap.rst Packet Framework ----------------
Signature · 1 line
2.30.2
v6 cover Packet capture framework enhancements
In reply to Stephen HemmingerThis patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
It preserves the old pdump command as is for those people who never want to change.
v6 changes:
- add a test for bpf converter (and fix one bug it found).
v5 changes:
- minor build and checkpatch fixes for RHEL/FreeBSD
- disable lib/pdump on Windows. It was not useful before and now pdump depends on bpf.
v4 changes:
- minor checkpatch fixes.
Note: some of the checkpatch warnings are bogus and won't be fixed.
- fix build of dumpcap on FreeBSD
v3 changes:
- introduce packet filters using classic BPF to eBPF converter required small fix to DPDK BPF interpreter
- introduce function to decode eBPF instructions
- add option to dumpcap to show both classic BPF and eBPF result
- drop some un-useful stubs
- minor checkpatch warning cleanup
Stephen Hemminger (10):
librte_pcapng: add new library for writing pcapng files
bpf: allow self-xor operation
bpf: add function to convert classic BPF to DPDK BPF
bpf: add function to dump eBPF instructions
test: add test for bpf_convert
lib: pdump is not supported on Windows
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new pcapng and dumper
MAINTAINERS | 6 + app/dumpcap/main.c | 827 ++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + app/test/test_bpf.c | 148 ++++ doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +- doc/guides/howto/packet_capture_framework.rst | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 575 ++++++++++++ lib/bpf/bpf_dump.c | 118 +++ lib/bpf/bpf_validate.c | 8 +- lib/bpf/meson.build | 6 + lib/bpf/rte_bpf.h | 39 + lib/bpf/version.map | 7 + lib/meson.build | 6 +- lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 437 ++++++--- lib/pdump/rte_pdump.h | 110 ++- lib/pdump/version.map | 8 + 31 files changed, 3330 insertions(+), 216 deletions(-) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst create mode 100644 lib/bpf/bpf_convert.c create mode 100644 lib/bpf/bpf_dump.c create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
Signature · 1 line
2.30.2
PATCH v6 1/10 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 194 +++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 918 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +574 -0
574 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +194 -0
194 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH v6 2/10 bpf: allow self-xor operation
In reply to Stephen HemmingerWhen doing BPF filter program conversion, a common way to zero a register in single instruction is:
xor r7,r7
The BPF validator would not allow this because the value of r7 was undefined. But after this operation it always zero.
--- lib/bpf/bpf_validate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
lib/bpf/bpf_validate.c +6 -2
@@ -661,8 +661,12 @@ eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins) op =BPF_OP (ins->code); - err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , - (op !=BPF_NEG ) ? &rs :NULL ); +/* Allow self-xor as way to zero register */ +if (op ==BPF_XOR && ins->src_reg == ins->dst_reg) + err =NULL ; +else + err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , + (op !=BPF_NEG ) ? &rs :NULL );if (err !=NULL )return err;
Signature · 1 line
2.30.2
PATCH v6 3/10 bpf: add function to convert classic BPF to DPDK BPF
In reply to Stephen HemmingerThe pcap library emits classic BPF (32 bit) and is useful for creating filter programs. The DPDK BPF library only implements extended BPF (eBPF). Add an function to convert from old to new.
The rte_bpf_convert function uses rte_malloc to put the resulting program in hugepage shared memory so it can be passed from a secondary process to a primary process.
The code to convert was originally done as part of the Linux kernel implementation then converted to a userspace program. Both authors have agreed that it is allowable to license this as BSD licensed in DPDK.
--- lib/bpf/bpf_convert.c | 570 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 606 insertions(+) create mode 100644 lib/bpf/bpf_convert.c
lib/bpf/bpf_convert.c new file +570 -0
@@ -0,0 +1,570 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2021 Microsoft Corporation +* +* Based on bpf_convert_filter() in the Linux kernel sources +* and filter2xdp. +* +* Licensed as BSD with permission original authors. +* Copyright (C) 2017 Tobias Klauser +* Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com +*/ + +#include <assert.h> +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <rte_common.h> +#include <rte_bpf.h> +#include <rte_log.h> +#include <rte_malloc.h> +#include <rte_errno.h> + +/* Workaround name conflicts with libpcap */ +#define bpf_validate (f, len) bpf_validate_libpcap(f, len) +#include <pcap/pcap.h> +#include <pcap/bpf.h> +#undef bpf_validate + +#include "bpf_impl.h" +#include "bpf_def.h" + +#ifndef BPF_MAXINSNS +#define BPF_MAXINSNS 4096 +#endif + +/* +* Linux socket filter uses negative absolute offsets to +* reference ancillary data. +* +*/ +#define SKF_AD_OFF (-0x1000) +#define SKF_AD_PROTOCOL 0 +#define SKF_AD_PKTTYPE 4 +#define SKF_AD_IFINDEX 8 +#define SKF_AD_NLATTR 12 +#define SKF_AD_NLATTR_NEST 16 +#define SKF_AD_MARK 20 +#define SKF_AD_QUEUE 24 +#define SKF_AD_HATYPE 28 +#define SKF_AD_RXHASH 32 +#define SKF_AD_CPU 36 +#define SKF_AD_ALU_XOR_X 40 +#define SKF_AD_VLAN_TAG 44 +#define SKF_AD_VLAN_TAG_PRESENT 48 +#define SKF_AD_PAY_OFFSET 52 +#define SKF_AD_RANDOM 56 +#define SKF_AD_VLAN_TPID 60 +#define SKF_AD_MAX 64 + +/* ArgX, context and stack frame pointer register positions. Note, +* Arg1, Arg2, Arg3, etc are used as argument mappings of function +* calls in BPF_CALL instruction. +*/ +#define BPF_REG_ARG1 EBPF_REG_1 +#define BPF_REG_ARG2 EBPF_REG_2 +#define BPF_REG_ARG3 EBPF_REG_3 +#define BPF_REG_ARG4 EBPF_REG_4 +#define BPF_REG_ARG5 EBPF_REG_5 +#define BPF_REG_CTX EBPF_REG_6 +#define BPF_REG_FP EBPF_REG_10 + +/* Additional register mappings for converted user programs. */ +#define BPF_REG_A EBPF_REG_0 +#define BPF_REG_X EBPF_REG_7 +#define BPF_REG_TMP EBPF_REG_8 + +/* Helper macros for filter block array initializers. */ + +/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ + +#define EBPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_ALU32_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ + +#define BPF_ALU32_IMM (OP ,DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov, dst_reg = src_reg */ + +#define BPF_MOV64_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_MOV32_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* Short form of mov, dst_reg = imm32 */ + +#define BPF_MOV32_IMM (DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */ + +#define BPF_MOV32_RAW (TYPE ,DST ,SRC ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_SRC(TYPE), \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = IMM }) + +/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ + +#define BPF_LD_ABS (SIZE ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Memory load, dst_reg = *(uint *) (src_reg + off16) */ + +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Memory store, *(uint *) (dst_reg + off16) = src_reg */ + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ + +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +/* Raw code statement block */ + +#define BPF_RAW_INSN (CODE ,DST ,SRC ,OFF ,IMM ) \ + ((struct ebpf_insn) { \ + .code = CODE, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = IMM }) + +/* Program exit */ + +#define BPF_EXIT_INSN () \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | EBPF_EXIT, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = 0 }) + +/* +* Placeholder to convert BPF extensions like length and VLAN tag +* If and when DPDK BPF supports them. +*/ +static bool convert_bpf_load (const struct bpf_insn *fp, + struct ebpf_insn **new_insnp __rte_unused) +{ +switch (fp->k) { +case SKF_AD_OFF +SKF_AD_PROTOCOL : +case SKF_AD_OFF +SKF_AD_PKTTYPE : +case SKF_AD_OFF +SKF_AD_IFINDEX : +case SKF_AD_OFF +SKF_AD_HATYPE : +case SKF_AD_OFF +SKF_AD_MARK : +case SKF_AD_OFF +SKF_AD_RXHASH : +case SKF_AD_OFF +SKF_AD_QUEUE : +case SKF_AD_OFF +SKF_AD_VLAN_TAG : +case SKF_AD_OFF +SKF_AD_VLAN_TAG_PRESENT : +case SKF_AD_OFF +SKF_AD_VLAN_TPID : +case SKF_AD_OFF +SKF_AD_PAY_OFFSET : +case SKF_AD_OFF +SKF_AD_NLATTR : +case SKF_AD_OFF +SKF_AD_NLATTR_NEST : +case SKF_AD_OFF +SKF_AD_CPU : +case SKF_AD_OFF +SKF_AD_RANDOM : +case SKF_AD_OFF +SKF_AD_ALU_XOR_X : +/* TODO: Not implemented yet */ +RTE_BPF_LOG (ERR ,"BPF extension LOAD ABS %u not supported\n" , + fp->k); +return true; +default : +return false; + } +} + +static int bpf_convert_filter (const struct bpf_insn *prog,size_t len, +struct ebpf_insn *new_prog,uint32_t *new_len) +{ +unsigned int pass =0 ; +size_t new_flen =0 , target, i; +struct ebpf_insn *new_insn; +const struct bpf_insn *fp; +int *addrs =NULL ; +uint8_t bpf_src; + +if (len >BPF_MAXINSNS ) { +RTE_BPF_LOG (ERR ,"%s: cBPF program too long (%zu insns)\n" , + __func__, len); +return -EINVAL ; + } + +/* On second pass, allocate the new program */ +if (new_prog) { + addrs =calloc (len,sizeof (*addrs)); +if (addrs ==NULL ) +return -ENOMEM ; + } + +do_pass: + new_insn = new_prog; + fp = prog; + +/* Classic BPF related prologue emission. */ +if (new_insn) { +/* Classic BPF expects A and X to be reset first. These need +* to be guaranteed to be the first two instructions. +*/ + *new_insn++ =EBPF_ALU64_REG (BPF_XOR ,BPF_REG_A ,BPF_REG_A ); + *new_insn++ =EBPF_ALU64_REG (BPF_XOR ,BPF_REG_X ,BPF_REG_X ); + +/* All programs must keep CTX in callee saved BPF_REG_CTX. +* In eBPF case it's done by the compiler, here we need to +* do this ourself. Initial CTX is present in BPF_REG_ARG1. +*/ + *new_insn++ =BPF_MOV64_REG (BPF_REG_CTX ,BPF_REG_ARG1 ); + }else { + new_insn +=3 ; + } + +for (i =0 ; i < len; fp++, i++) { +struct ebpf_insn tmp_insns[6 ] = { }; +struct ebpf_insn *insn = tmp_insns; + +if (addrs) + addrs[i] = new_insn - new_prog; + +switch (fp->code) { +/* Absolute loads are how classic BPF accesses skb */ +case BPF_LD |BPF_ABS |BPF_W : +case BPF_LD |BPF_ABS |BPF_H : +case BPF_LD |BPF_ABS |BPF_B : +if (convert_bpf_load (fp, &insn)) + goto err; + + *insn =BPF_RAW_INSN (fp->code,0 ,0 ,0 , fp->k); +break ; + +case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_X : +/* For cBPF, don't cause floating point exception */ + *insn++ =BPF_MOV32_REG (BPF_REG_X ,BPF_REG_X ); + *insn++ =BPF_JMP_IMM (EBPF_JNE ,BPF_REG_X ,0 ,2 ); + *insn++ =BPF_ALU32_REG (BPF_XOR ,BPF_REG_A ,BPF_REG_A ); + *insn++ =BPF_EXIT_INSN (); +/* fallthrough */ +case BPF_ALU |BPF_ADD |BPF_X : +case BPF_ALU |BPF_ADD |BPF_K : +case BPF_ALU |BPF_SUB |BPF_X : +case BPF_ALU |BPF_SUB |BPF_K : +case BPF_ALU |BPF_AND |BPF_X : +case BPF_ALU |BPF_AND |BPF_K : +case BPF_ALU |BPF_OR |BPF_X : +case BPF_ALU |BPF_OR |BPF_K : +case BPF_ALU |BPF_LSH |BPF_X : +case BPF_ALU |BPF_LSH |BPF_K : +case BPF_ALU |BPF_RSH |BPF_X : +case BPF_ALU |BPF_RSH |BPF_K : +case BPF_ALU |BPF_XOR |BPF_X : +case BPF_ALU |BPF_XOR |BPF_K : +case BPF_ALU |BPF_MUL |BPF_X : +case BPF_ALU |BPF_MUL |BPF_K : +case BPF_ALU |BPF_DIV |BPF_K : +case BPF_ALU |BPF_MOD |BPF_K : +case BPF_ALU |BPF_NEG : +case BPF_LD |BPF_IND |BPF_W : +case BPF_LD |BPF_IND |BPF_H : +case BPF_LD |BPF_IND |BPF_B : +/* All arithmetic insns map as-is. */ + *insn =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); +break ; + +/* Jump transformation cannot use BPF block macros +* everywhere as offset calculation and target updates +* require a bit more work than the rest, i.e. jump +* opcodes map as-is, but offsets need adjustment. +*/ + +#define BPF_EMIT_JMP \ + do { \ + if (target >= len) \ + goto err; \ + insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ +/* Adjust pc relative offset for 2nd or 3rd insn. */ \ + insn->off -= insn - tmp_insns; \ + }while (0 ) + +case BPF_JMP |BPF_JA : + target = i + fp->k +1 ; + insn->code = fp->code; +BPF_EMIT_JMP ; +break ; + +case BPF_JMP |BPF_JEQ |BPF_K : +case BPF_JMP |BPF_JEQ |BPF_X : +case BPF_JMP |BPF_JSET |BPF_K : +case BPF_JMP |BPF_JSET |BPF_X : +case BPF_JMP |BPF_JGT |BPF_K : +case BPF_JMP |BPF_JGT |BPF_X : +case BPF_JMP |BPF_JGE |BPF_K : +case BPF_JMP |BPF_JGE |BPF_X : +if (BPF_SRC (fp->code) ==BPF_K && (int ) fp->k <0 ) { +/* BPF immediates are signed, zero extend +* immediate into tmp register and use it +* in compare insn. +*/ + *insn++ =BPF_MOV32_IMM (BPF_REG_TMP , fp->k); + + insn->dst_reg =BPF_REG_A ; + insn->src_reg =BPF_REG_TMP ; + bpf_src =BPF_X ; + }else { + insn->dst_reg =BPF_REG_A ; + insn->imm = fp->k; + bpf_src =BPF_SRC (fp->code); + insn->src_reg = bpf_src ==BPF_X ?BPF_REG_X :0 ; + } + +/* Common case where 'jump_false' is next insn. */ +if (fp->jf ==0 ) { + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; + target = i + fp->jt +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Convert JEQ into JNE when 'jump_true' is next insn. */ +if (fp->jt ==0 &&BPF_OP (fp->code) ==BPF_JEQ ) { + insn->code =BPF_JMP |EBPF_JNE | bpf_src; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Other jumps are mapped into two insns: Jxx and JA. */ + target = i + fp->jt +1 ; + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; +BPF_EMIT_JMP ; + insn++; + + insn->code =BPF_JMP |BPF_JA ; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + +/* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ +case BPF_LDX |BPF_MSH |BPF_B : +/* tmp = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_TMP ,BPF_REG_A ); +/* A = BPF_R0 = *(u8 *) (skb->data + K) */ + *insn++ =BPF_LD_ABS (BPF_B , fp->k); +/* A &= 0xf */ + *insn++ =BPF_ALU32_IMM (BPF_AND ,BPF_REG_A ,0xf ); +/* A <<= 2 */ + *insn++ =BPF_ALU32_IMM (BPF_LSH ,BPF_REG_A ,2 ); +/* X = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +/* A = tmp */ + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_TMP ); +break ; + +/* RET_K is remaped into 2 insns. RET_A case doesn't need an +* extra mov as EBPF_REG_0 is already mapped into BPF_REG_A. +*/ +case BPF_RET |BPF_A : +case BPF_RET |BPF_K : +if (BPF_RVAL (fp->code) ==BPF_K ) { + *insn++ =BPF_MOV32_RAW (BPF_K ,EBPF_REG_0 , +0 , fp->k); + } + *insn =BPF_EXIT_INSN (); +break ; + +/* Store to stack. */ +case BPF_ST : +case BPF_STX : + *insn =BPF_STX_MEM (BPF_W ,BPF_REG_FP ,BPF_CLASS (fp->code) == +BPF_ST ?BPF_REG_A :BPF_REG_X , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* Load from stack. */ +case BPF_LD |BPF_MEM : +case BPF_LDX |BPF_MEM : + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_FP , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* A = K or X = K */ +case BPF_LD |BPF_IMM : +case BPF_LDX |BPF_IMM : + *insn =BPF_MOV32_IMM (BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X , fp->k); +break ; + +/* X = A */ +case BPF_MISC |BPF_TAX : + *insn =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +break ; + +/* A = X */ +case BPF_MISC |BPF_TXA : + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_X ); +break ; + +/* A = mbuf->len or X = mbuf->len */ +case BPF_LD |BPF_W |BPF_LEN : +case BPF_LDX |BPF_W |BPF_LEN : +/* BPF_ABS/BPF_IND implicitly expect mbuf ptr in R6 */ + + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_CTX , + offsetof(struct rte_mbuf , pkt_len)); +break ; + +/* Unknown instruction. */ +default : +RTE_BPF_LOG (ERR ,"%s: Unknown instruction!: %#x\n" , + __func__, fp->code); + goto err; + } + + insn++; +if (new_prog) +memcpy (new_insn, tmp_insns, +sizeof (*insn) * (insn - tmp_insns)); + new_insn += insn - tmp_insns; + } + +if (!new_prog) { +/* Only calculating new length. */ + *new_len = new_insn - new_prog; +return 0 ; + } + + pass++; +if ((ptrdiff_t )new_flen != new_insn - new_prog) { + new_flen = new_insn - new_prog; +if (pass >2 ) + goto err; + goto do_pass; + } + +free (addrs); +assert (*new_len == new_flen); + +return 0 ; +err: +free (addrs); +return -1 ; +} + +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog) +{ +struct rte_bpf_prm *prm =NULL ; +struct ebpf_insn *ebpf =NULL ; +uint32_t ebpf_len =0 ; +int ret; + +if (prog ==NULL ) { +RTE_BPF_LOG (ERR ,"%s: NULL program\n" , __func__); + rte_errno =EINVAL ; +return NULL ; + } + +/* 1st pass: calculate the eBPF program length */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len,NULL , &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot get eBPF length\n" , __func__); + rte_errno = -ret; +return NULL ; + } + +RTE_BPF_LOG (DEBUG ,"%s: prog len cBPF=%u -> eBPF=%u\n" , + __func__, prog->bf_len, ebpf_len); + + prm =rte_zmalloc ("bpf_filter" , +sizeof (*prm) + ebpf_len *sizeof (*ebpf),0 ); +if (prm ==NULL ) { + rte_errno =ENOMEM ; +return NULL ; + } + +/* The EPBF instructions in this case are right after the header */ + ebpf = (void *)(prm +1 ); + +/* 2nd pass: remap cBPF to eBPF instructions */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot convert cBPF to eBPF\n" , __func__); +free (prm); + rte_errno = -ret; +return NULL ; + } + + prm->ins = ebpf; + prm->nb_ins = ebpf_len; + +/* Classic BPF programs use mbufs */ + prm->prog_arg.type =RTE_BPF_ARG_PTR_MBUF ; + prm->prog_arg.size =sizeof (struct rte_mbuf ); + +return prm; +}
lib/bpf/meson.build +5 -0
@@ -25,3 +25,8 @@ if dep.found() sources += files('bpf_load_elf.c') ext_deps += dep endif + +if dpdk_conf.has('RTE_PORT_PCAP') + sources += files('bpf_convert.c') + ext_deps += pcap_dep +endif
lib/bpf/rte_bpf.h +25 -0
@@ -198,6 +198,31 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +#ifdef RTE_PORT_PCAP + +struct bpf_program ; + +/** +* Convert a Classic BPF program from libpcap into a DPDK BPF code. +* +* @param prog +* Classic BPF program from pcap_compile(). +* @param prm +* Result Extended BPF program. +* @return +* Pointer to BPF program (allocated with *rte_malloc*) +* that is used in future BPF operations, +* or NULL on error, with error code set in rte_errno. +* Possible rte_errno errors include: +* - EINVAL - invalid parameter passed to function +* - ENOMEM - can't reserve enough memory +*/ +__rte_experimental +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog); + +#endif + #ifdef __cplusplus }#endif
lib/bpf/version.map +6 -0
@@ -14,3 +14,9 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_bpf_convert; +};
Signature · 1 line
2.30.2
Re: [dpdk-dev] [PATCH v6 03/10] bpf: add function to convert classic BPF to DPDK BPF
2021-09-09 16:33 (UTC-0700), Stephen Hemminger:
[...]
Quoted 5 lines
lib/bpf/bpf_convert.c new file +5
-0+ prm =rte_zmalloc ("bpf_filter" , +sizeof (*prm) + ebpf_len *sizeof (*ebpf),0 ); +if (prm ==NULL ) { + rte_errno =ENOMEM ; +return NULL ;
lib/bpf/bpf_convert.c line 557
Quoted 10 lines
lib/bpf/bpf_convert.c new file +10
-0+ } + +/* The EPBF instructions in this case are right after the header */ + ebpf = (void *)(prm +1 ); + +/* 2nd pass: remap cBPF to eBPF instructions */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot convert cBPF to eBPF\n" , __func__); +free (prm);
free -> rte_free
Quoted 3 lines
lib/bpf/bpf_convert.c new file +3
-0+ rte_errno = -ret; +return NULL ; + }
[...]
lib/bpf/rte_bpf.h rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[], · line 201
Quoted 9 lines
lib/bpf/rte_bpf.h +1
-0@@ -198,6 +198,31 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +#ifdef RTE_PORT_PCAP
In libre_bpf the function for ELF loading is always declared, and defined as a stub when libelf is unavailable. The app using it can link to DPDK with or without ELF support. No strong opinion here, but using different approaches is a bit messy.
Quoted 27 lines
lib/bpf/rte_bpf.h +24
-0+ +struct bpf_program ; + +/** +* Convert a Classic BPF program from libpcap into a DPDK BPF code. +* +* @param prog +* Classic BPF program from pcap_compile(). +* @param prm +* Result Extended BPF program. +* @return +* Pointer to BPF program (allocated with *rte_malloc*) +* that is used in future BPF operations, +* or NULL on error, with error code set in rte_errno. +* Possible rte_errno errors include: +* - EINVAL - invalid parameter passed to function +* - ENOMEM - can't reserve enough memory +*/ +__rte_experimental +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog); + +#endif + #ifdef __cplusplus }#endif
PATCH v6 4/10 bpf: add function to dump eBPF instructions
In reply to Stephen HemmingerWhen debugging converted (and other) programs it is useful to see disassembled eBPF output.
--- lib/bpf/bpf_convert.c | 7 ++- lib/bpf/bpf_dump.c | 118 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 +++++ lib/bpf/version.map | 1 + 5 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 lib/bpf/bpf_dump.c
lib/bpf/bpf_convert.c +6 -1
@@ -331,7 +331,12 @@ static int bpf_convert_filter(const struct bpf_insn *prog, size_t len,case BPF_LD |BPF_IND |BPF_H :case BPF_LD |BPF_IND |BPF_B :/* All arithmetic insns map as-is. */ - *insn =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); + insn->code = fp->code; + insn->dst_reg =BPF_REG_A ; + bpf_src =BPF_SRC (fp->code); + insn->src_reg = bpf_src ==BPF_X ?BPF_REG_X :0 ; + insn->off =0 ; + insn->imm = fp->k;break ;/* Jump transformation cannot use BPF block macros
lib/bpf/bpf_dump.c new file +118 -0
118 lines not on this page · raw message
lib/bpf/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2018 Intel Corporation sources = files('bpf.c', + 'bpf_dump.c', 'bpf_exec.c', 'bpf_load.c', 'bpf_pkt.c',
lib/bpf/rte_bpf.h +14 -0
@@ -198,6 +198,20 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +/** +* Dump epf instructions to a file. +* +* @param f +* A pointer to a file for output +* @param buf +* A pointer to BPF instructions +* @param len +* Number of BPF instructions to dump. +*/ +__rte_experimental +void +rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len); +#ifdef RTE_PORT_PCAP struct bpf_program ;
lib/bpf/version.map +1 -0
@@ -19,4 +19,5 @@ EXPERIMENTAL { global: rte_bpf_convert; + rte_bpf_dump; };
Signature · 1 line
2.30.2
PATCH v6 5/10 test: add test for bpf_convert
In reply to Stephen HemmingerAdd some functional tests for the Classic BPF to DPDK BPF converter.
--- app/test/test_bpf.c | 148 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+)
app/test/test_bpf.c +148 -0
@@ -10,6 +10,7 @@#include <rte_memory.h> #include <rte_debug.h> #include <rte_hexdump.h> +#include <rte_malloc.h> #include <rte_random.h> #include <rte_byteorder.h> #include <rte_errno.h>
@@ -3233,3 +3234,150 @@ test_bpf(void) }REGISTER_TEST_COMMAND (bpf_autotest, test_bpf); + +#ifdef RTE_PORT_PCAP +#include <pcap/pcap.h> + +static void +test_bpf_dump (struct bpf_program *cbf,const struct rte_bpf_prm *prm) +{ +printf ("cBPF program (%u insns)\n" , cbf->bf_len); +bpf_dump (cbf,1 ); + +printf ("\neBPF program (%u insns)\n" , prm->nb_ins); +rte_bpf_dump (stdout, prm->ins, prm->nb_ins); +} + +static int +test_bpf_match (pcap_t *pcap,const char *str,bool expected) +{ +struct bpf_program fcode; +struct rte_bpf_prm *prm =NULL ; +struct rte_bpf *bpf =NULL ; +uint8_t tbuf[sizeof (struct dummy_mbuf )]; +int ret =-1 ; +uint64_t rc; + +if (pcap_compile (pcap, &fcode, str,1 ,PCAP_NETMASK_UNKNOWN )) { +printf ("%s@%d: pcap_compile(\"%s\") failed: %s;\n" , + __func__, __LINE__, str,pcap_geterr (pcap)); +return -1 ; + } + + prm =rte_bpf_convert (&fcode); +if (prm ==NULL ) { +printf ("%s@%d: bpf_convert('%s') failed,, error=%d(%s);\n" , + __func__, __LINE__, str, rte_errno,strerror (rte_errno)); + goto error; + } + + bpf =rte_bpf_load (prm); +if (bpf ==NULL ) { +printf ("%s@%d: failed to load bpf code, error=%d(%s);\n" , + __func__, __LINE__, rte_errno,strerror (rte_errno)); + goto error; + } + +test_ld_mbuf1_prepare (tbuf); + rc =rte_bpf_exec (bpf, tbuf); +if ((rc ==0 ) == expected) + ret =0 ; +else +printf ("%s@%d: failed match: expect %s 0 got %" PRIu64"\n" , + __func__, __LINE__, expected ?"==" :"<>" , rc); +error: +if (bpf) +rte_bpf_destroy (bpf); +rte_free (prm); +pcap_freecode (&fcode); +return ret; +} + +/* Basic sanity test can we match a IP packet */ +static int +test_bpf_filter_sanity (pcap_t *pcap) +{ +int ret; + + ret =test_bpf_match (pcap,"ip" , true); + ret |=test_bpf_match (pcap,"not ip" , false); + +return ret; +} + +/* Some sample pcap filter strings from tcpdump man page */ +static const char *sample_filters[] = { +"host 192.168.1.100" , +"src net 10" , +"not stp" , +"len = 128" , +"ip host 1.1.1.1 and not 1.1.1.2" , +"ip and not net 127.0.0.1" , +"tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net 127.0.0.1" , +"ether[0] & 1 = 0 and ip[16] >= 224" , +"icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply" , +}; + +static int +test_bpf_filter (pcap_t *pcap,const char *s) +{ +struct bpf_program fcode; +struct rte_bpf_prm *prm =NULL ; +struct rte_bpf *bpf =NULL ; + +if (pcap_compile (pcap, &fcode, s,1 ,PCAP_NETMASK_UNKNOWN )) { +printf ("%s@%d: pcap_compile('%s') failed: %s;\n" , + __func__, __LINE__, s,pcap_geterr (pcap)); +return -1 ; + } + + prm =rte_bpf_convert (&fcode); +if (prm ==NULL ) { +printf ("%s@%d: bpf_convert('%s') failed,, error=%d(%s);\n" , + __func__, __LINE__, s, rte_errno,strerror (rte_errno)); + goto error; + } + + bpf =rte_bpf_load (prm); +if (bpf ==NULL ) { +printf ("%s@%d: failed to load bpf code, error=%d(%s);\n" , + __func__, __LINE__, rte_errno,strerror (rte_errno)); + goto error; + } + +error: +if (bpf) +rte_bpf_destroy (bpf); +else { +printf ("%s \"%s\"\n" , __func__, s); +test_bpf_dump (&fcode, prm); + } + +rte_free (prm); +pcap_freecode (&fcode); +return (bpf ==NULL ) ?-1 :0 ; +} + +static int +test_bpf_convert (void ) +{ +unsigned int i; +pcap_t *pcap; +int rc; + + pcap =pcap_open_dead (DLT_EN10MB ,262144 ); +if (!pcap) { +printf ("pcap_open_dead failed\n" ); +return -1 ; + } + + rc =test_bpf_filter_sanity (pcap); +for (i =0 ; i <RTE_DIM (sample_filters); i++) { + rc |=test_bpf_filter (pcap,sample_filters[i]); + } +pcap_close (pcap); +return rc; +} + +REGISTER_TEST_COMMAND (bpf_convert_autotest, test_bpf_convert); +#endif /* RTE_PORT_PCAP */
Signature · 1 line
2.30.2
PATCH v6 6/10 lib: pdump is not supported on Windows
In reply to Stephen HemmingerThe original version of the pdump library was building on Windows, but it was useless since the pdump utility was not being built.
The new version of pdump with filtering now has dependency on bpf. But bpf library is not available on Windows.
For now, just stop trying to build pdump on Windows. Eventually, bpf library, pdump library, dumpcap tool, and pdump tool can be converted to work on Windows.
--- lib/meson.build | 1 - 1 file changed, 1 deletion(-)
lib/meson.build +0 -1
@@ -85,7 +85,6 @@ if is_windows 'gro', 'gso', 'latencystats', - 'pdump', ] # only supported libraries for windows endif
Signature · 1 line
2.30.2
Re: [dpdk-dev] [PATCH v6 06/10] lib: pdump is not supported on Windows
2021-09-09 16:33 (UTC-0700), Stephen Hemminger:
Quoted 26 lines
The original version of the pdump library was building on Windows, but it was useless since the pdump utility was not being built.
The new version of pdump with filtering now has dependency on bpf. But bpf library is not available on Windows.
For now, just stop trying to build pdump on Windows. Eventually, bpf library, pdump library, dumpcap tool, and pdump tool can be converted to work on Windows.
Signed-off-by Stephen Hemminger <[email protected]>Cc Dmitry Kozlyuk <[email protected]>Cc Narcisa Ana Maria Vasile <[email protected]>Cc Dmitry Malloy <[email protected]>Cc Pallavi Kadam <[email protected]>--- lib/meson.build | 1 - 1 file changed, 1 deletion(-)lib/meson.build +0
-0
lib/meson.build if is_windows · line 90
Quoted 7 lines
lib/meson.build +0
-1@@ -85,7 +85,6 @@ if is_windows 'gro', 'gso', 'latencystats', - 'pdump', ] # only supported libraries for windows endif
Anyway pdump relies on multiprocess not supported on Windows.
PATCH v6 7/10 pdump: support pcapng and filtering
In reply to Stephen HemmingerThis enhances the DPDK pdump library to support new pcapng format and filtering via BPF.
The internal client/server protocol is changed to support two versions: the original pdump basic version and a new pcapng version.
The internal version number (not part of exposed API or ABI) is intentionally increased to cause any attempt to try mismatched primary/secondary process to fail.
Add new API to do allow filtering of captured packets with DPDK BPF (eBPF) filter program. It keeps statistics
on packets captured, filtered, and missed (because ring was full).
--- lib/meson.build | 4 +- lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 437 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 110 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 435 insertions(+), 126 deletions(-)
lib/meson.build +2 -2
@@ -26,6 +26,7 @@ libraries = [ 'timer', # eventdev depends on this 'acl', 'bbdev', + 'bpf', 'bitratestats', 'cfgfile', 'compressdev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'rib',
@@ -55,10 +55,10 @@ libraries = [ 'ipsec', # ipsec lib depends on net, crypto and security 'fib', #fib lib depends on rib 'port', # pkt framework libs which use other libs from above + 'pdump', # pdump lib depends on bpf pcapng 'table', 'pipeline', 'flow_classify', # flow_classify lib depends on pkt framework table lib - 'bpf', 'graph', 'node', ]
lib/pdump/meson.build +1 -1
@@ -3,4 +3,4 @@ sources = files('rte_pdump.c') headers = files('rte_pdump.h') -deps += ['ethdev'] +deps += ['ethdev', 'bpf', 'pcapng']
lib/pdump/rte_pdump.c +317 -120
644 lines not on this page · raw message
lib/pdump/rte_pdump.h +107 -3
152 lines not on this page · raw message
lib/pdump/version.map +8 -0
@@ -10,3 +10,11 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_pdump_enable_bpf; + rte_pdump_enable_bpf_by_deviceid; + rte_pdump_stats; +};
Signature · 1 line
2.30.2
PATCH v6 8/10 app/dumpcap: add new packet capture application
In reply to Stephen HemmingerThis is a new packet capture application to replace existing pdump. The new application works like Wireshark dumpcap program and supports the pdump API features.
It is not complete yet some features such as filtering are not implemented.
--- app/dumpcap/main.c | 827 ++++++++++++++++++++++++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + 3 files changed, 844 insertions(+) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build
app/dumpcap/main.c new file +827 -0
@@ -0,0 +1,827 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019-2020 Microsoft Corporation +* +* DPDK application to dump network traffic +* This is designed to look and act like the Wireshark +* dumpcap program. +*/ + +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <inttypes.h> +#include <limits.h> +#include <signal.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/queue.h> +#include <sys/types.h> +#include <sys/utsname.h> +#include <time.h> +#include <unistd.h> + +#include <rte_alarm.h> +#include <rte_bpf.h> +#include <rte_config.h> +#include <rte_debug.h> +#include <rte_eal.h> +#include <rte_errno.h> +#include <rte_ethdev.h> +#include <rte_lcore.h> +#include <rte_malloc.h> +#include <rte_mbuf.h> +#include <rte_mempool.h> +#include <rte_pcapng.h> +#include <rte_pdump.h> +#include <rte_ring.h> +#include <rte_string_fns.h> +#include <rte_time.h> +#include <rte_version.h> + +#include <pcap/pcap.h> +#include <pcap/bpf.h> + +#define RING_NAME "capture-ring" +#define MONITOR_INTERVAL (500 * 1000) +#define MBUF_POOL_CACHE_SIZE 32 +#define BURST_SIZE 32 +#define SLEEP_THRESHOLD 1000 + +/* command line flags */ +static const char *progname; +static bool quit_signal; +static bool group_read; +static bool quiet; +static bool promiscuous_mode = true; +static bool use_pcapng = true; +static char *output_name; +static const char *filter_str; +static unsigned int ring_size =2048 ; +static const char *capture_comment; +static uint32_t snaplen =RTE_MBUF_DEFAULT_BUF_SIZE ; +static bool dump_bpf; +static struct { +uint64_t duration;/* nanoseconds */ +unsigned long packets;/* number of packets in file */ +size_t size;/* file size (bytes) */ +} stop; + +/* Running state */ +static struct rte_bpf_prm *bpf_prm; +static uint64_t start_time, end_time; +static uint64_t packets_received; +static size_t file_size; + +struct interface { +TAILQ_ENTRY (interface) next; +uint16_t port; +char name[RTE_ETH_NAME_MAX_LEN ]; + +struct rte_rxtx_callback *rx_cb[RTE_MAX_QUEUES_PER_PORT ]; +}; + +TAILQ_HEAD (interface_list, interface); +static struct interface_list interfaces =TAILQ_HEAD_INITIALIZER (interfaces); +static struct interface *port2intf[RTE_MAX_ETHPORTS ]; + +static void usage (void ) +{ +printf ("Usage: %s [options] ...\n\n" , progname); +printf ("Capture Interface:\n" +" -i <interface> name or port index of interface\n" +" -f <capture filter> packet filter in libpcap filter syntax\n" ); +printf (" -s <snaplen>, --snapshot-length <snaplen>\n" +" packet snapshot length (def: %u)\n" , +RTE_MBUF_DEFAULT_BUF_SIZE ); +printf (" -p, --no-promiscuous-mode\n" +" don't capture in promiscuous mode\n" +" -D, --list-interfaces print list of interfaces and exit\n" +" -d print generated BPF code for capture filter\n" +"\n" +"Stop conditions:\n" +" -c <packet count> stop after n packets (def: infinite)\n" +" -a <autostop cond.> ..., --autostop <autostop cond.> ...\n" +" duration:NUM - stop after NUM seconds\n" +" filesize:NUM - stop this file after NUM kB\n" +" packets:NUM - stop after NUM packets\n" +"Output (files):\n" +" -w <filename> name of file to save (def: tempfile)\n" +" -g enable group read access on the output file(s)\n" +" -n use pcapng format instead of pcap (default)\n" +" -P use libpcap format instead of pcapng\n" +" --capture-comment <comment>\n" +" add a capture comment to the output file\n" +"\n" +"Miscellaneous:\n" +" -q don't report packet capture counts\n" +" -v, --version print version information and exit\n" +" -h, --help display this help and exit\n" +"\n" +"Use Ctrl-C to stop capturing at any time.\n" ); +} + +static const char *version (void ) +{ +static char str[128 ]; + +snprintf (str,sizeof (str), +"%s 1.0 (%s)\n" , progname,rte_version ()); +return str; +} + +/* Parse numeric argument from command line */ +static unsigned long get_uint (const char *arg,const char *name, +unsigned int limit) +{ +unsigned long u; +char *endp; + + u =strtoul (arg, &endp,0 ); +if (*arg =='\0' || *endp !='\0' ) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is not a valid number\n" , + name, arg); +if (limit && u > limit) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is too large (greater than %u)\n" , + name, arg, limit); + +return u; +} + +/* Set auto stop values */ +static void auto_stop (char *opt) +{ +char *value, *endp; + + value =strchr (opt,':' ); +if (value ==NULL ) +rte_exit (EXIT_FAILURE , +"Missing colon in auto stop parameter\n" ); + + *value++ ='\0' ; +if (strcmp (opt,"duration" ) ==0 ) { +double interval =strtod (value, &endp); + +if (*value =='\0' || *endp !='\0' || interval <=0 ) +rte_exit (EXIT_FAILURE , +"Invalid duration \"%s\"\n" , value); + stop.duration =NSEC_PER_SEC * interval; + }else if (strcmp (opt,"filesize" ) ==0 ) { + stop.size =get_uint (value,"filesize" ,0 ) *1024 ; + }else if (strcmp (opt,"packets" ) ==0 ) { + stop.packets =get_uint (value,"packets" ,0 ); + }else { +rte_exit (EXIT_FAILURE , +"Unknown autostop parameter \"%s\"\n" , opt); + } +} + +/* Add interface to list of interfaces to capture */ +static void add_interface (uint16_t port,const char *name) +{ +struct interface *intf; + + intf =malloc (sizeof (*intf)); +if (!intf) +rte_exit (EXIT_FAILURE ,"no memory for interface\n" ); + +memset (intf,0 ,sizeof (*intf)); +strlcpy (intf->name, name,sizeof (intf->name)); + +printf ("Capturing on '%s'\n" , name); + + port2intf[port] = intf; +TAILQ_INSERT_TAIL (&interfaces, intf, next); +} + +/* Select all valid DPDK interfaces */ +static void select_all_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); + } +} + +/* +* Choose interface to capture if no -i option given. +* Select the first DPDK port, this matches what dumpcap does. +*/ +static void set_default_interface (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); +return ; + } +rte_exit (EXIT_FAILURE ,"No usable interfaces found\n" ); +} + +/* Lookup interface by name or port and add it to the list */ +static void select_interface (const char *arg) +{ +uint16_t port; + +if (strcmp (arg,"*" )) +select_all_interfaces (); +else if (rte_eth_dev_get_port_by_name (arg, &port) ==0 ) +add_interface (port, arg); +else { +char name[RTE_ETH_NAME_MAX_LEN ]; + + port =get_uint (arg,"port_number" ,UINT16_MAX ); +if (rte_eth_dev_get_name_by_port (port, name) <0 ) +rte_exit (EXIT_FAILURE ,"Invalid port number %u\n" , + port); +add_interface (port, name); + } +} + +/* Display list of possible interfaces that can be used. */ +static void show_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +printf ("%u. %s\n" , p, name); + } +} + +static void compile_filter (void ) +{ +struct bpf_program bf; +pcap_t *pcap; + + pcap =pcap_open_dead (DLT_EN10MB , snaplen); +if (!pcap) +rte_exit (EXIT_FAILURE ,"can not open pcap\n" ); + +if (pcap_compile (pcap, &bf, filter_str, +1 ,PCAP_NETMASK_UNKNOWN ) !=0 ) +rte_exit (EXIT_FAILURE ,"pcap filter string not valid (%s)\n" , +pcap_geterr (pcap)); + + bpf_prm =rte_bpf_convert (&bf); +if (bpf_prm ==NULL ) +rte_exit (EXIT_FAILURE , +"bpf convert failed\n" ); + +if (dump_bpf) { +printf ("cBPF program (%u insns)\n" , bf.bf_len); +bpf_dump (&bf,1 ); +printf ("\neBPF program (%u insns)\n" , bpf_prm->nb_ins); +rte_bpf_dump (stdout, bpf_prm->ins, bpf_prm->nb_ins); +exit (0 ); + } + +/* Don't care about original program any more */ +pcap_freecode (&bf); +pcap_close (pcap); +} + +/* +* Parse command line options. +* These are chosen to be similar to dumpcap command. +*/ +static void parse_opts (int argc,char **argv) +{ +static const struct option long_options[] = { + {"autostop" , required_argument,NULL ,'a' }, + {"capture-comment" , required_argument,NULL ,0 }, + {"help" , no_argument,NULL ,'h' }, + {"interface" , required_argument,NULL ,'i' }, + {"list-interfaces" , no_argument,NULL ,'D' }, + {"no-promiscuous-mode" , no_argument,NULL ,'p' }, + {"output-file" , required_argument,NULL ,'w' }, + {"ring-buffer" , required_argument,NULL ,'b' }, + {"snapshot-length" , required_argument,NULL ,'s' }, + {"version" , no_argument,NULL ,'v' }, + {NULL }, + }; +int option_index, c; + +for (;;) { + c =getopt_long (argc, argv,"a:b:c:dDf:ghi:nN:pPqs:vw:" , + long_options, &option_index); +if (c ==-1 ) +break ; + +switch (c) { +case 0 : +switch (option_index) { +case 0 : + capture_comment = optarg; +break ; +default : +usage (); +exit (1 ); + } +break ; +case 'a' : +auto_stop (optarg); +break ; +case 'b' : +rte_exit (EXIT_FAILURE , +"multiple files not implemented\n" ); +break ; +case 'c' : + stop.packets =get_uint (optarg,"packet_count" ,0 ); +break ; +case 'd' : + dump_bpf = true; +break ; +case 'D' : +show_interfaces (); +exit (0 ); +case 'f' : + filter_str = optarg; +break ; +case 'g' : + group_read = true; +break ; +case 'h' : +printf ("%s\n\n" ,version ()); +usage (); +exit (0 ); +case 'i' : +select_interface (optarg); +break ; +case 'n' : + use_pcapng = true; +break ; +case 'N' : + ring_size =get_uint (optarg,"packet_limit" ,0 ); +break ; +case 'p' : + promiscuous_mode = false; +break ; +case 'P' : + use_pcapng = false; +break ; +case 'q' : + quiet = true; +break ; +case 's' : + snaplen =get_uint (optarg,"snap_len" ,0 ); +break ; +case 'w' : + output_name = optarg; +break ; +case 'v' : +printf ("%s\n" ,version ()); +exit (0 ); +default : +fprintf (stderr,"Invalid option: %s\n" , + argv[optind -1 ]); +usage (); +exit (1 ); + } + } +} + +static void +signal_handler(int sig_num __rte_unused) +{ +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); +} + +/* Return the time since 1/1/1970 in nanoseconds */ +static uint64_t create_timestamp (void ) +{ +struct timespec now; + +clock_gettime (CLOCK_MONOTONIC , &now); +return rte_timespec_to_ns (&now); +} + +static void +cleanup_pdump_resources (void ) +{ +struct interface *intf; + + TAILQ_FOREACH(intf, &interfaces, next) { +rte_pdump_disable (intf->port, +RTE_PDUMP_ALL_QUEUES ,RTE_PDUMP_FLAG_RXTX ); +if (promiscuous_mode) +rte_eth_promiscuous_disable (intf->port); + } +} + +/* Alarm signal handler, used to check that primary process */ +static void +monitor_primary(void *arg __rte_unused) +{ +if (__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) +return ; + +if (rte_eal_primary_proc_alive (NULL )) { +rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); + }else { +fprintf (stderr, +"Primary process is no longer active, exiting...\n" ); +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); + } +} + +/* Setup handler to check when primary exits. */ +static void +enable_primary_monitor (void ) +{ +int ret; + +/* Once primary exits, so will pdump. */ + ret =rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to enable monitor:%d\n" , ret); +} + +static void +disable_primary_monitor (void ) +{ +int ret; + + ret =rte_eal_alarm_cancel (monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to disable monitor:%d\n" , ret); +} + +static void +report_packet_stats (rte_pcapng_t *out) +{ +struct rte_pdump_stats pdump_stats; +struct interface *intf; +uint64_t ifrecv, ifdrop; +double percent; + +fputc ('\n' , stderr); + TAILQ_FOREACH(intf, &interfaces, next) { +if (rte_pdump_stats (intf->port, &pdump_stats) <0 ) +continue ; + +/* do what Wiretap does */ + ifrecv = pdump_stats.accepted + pdump_stats.filtered; + ifdrop = pdump_stats.nombuf + pdump_stats.ringfull; + +if (use_pcapng) +rte_pcapng_write_stats (out, intf->port,NULL , + start_time, end_time, + ifrecv, ifdrop); + +if (ifrecv ==0 ) + percent =0 ; +else + percent =100. * ifrecv / (ifrecv + ifdrop); + +fprintf (stderr, +"Packets received/dropped on interface '%s': " +"%" PRIu64"/%" PRIu64" (%.1f)\n" , + intf->name, ifrecv, ifdrop, percent); + } +} + +/* +* Start DPDK EAL with arguments. +* Unlike most DPDK programs, this application does not use the +* typical EAL command line arguments. +* We don't want to expose all the DPDK internals to the user. +*/ +static void dpdk_init (void ) +{ +const char *args[] = { + progname,"--proc-type" ,"secondary" , +"--log-level" ,"bpf:debug" + + }; +const int eal_argc =RTE_DIM (args); +char **eal_argv; +unsigned int i; + +/* DPDK API requires mutable versions of command line arguments. */ + eal_argv =calloc (eal_argc +1 ,sizeof (char *)); +if (eal_argv ==NULL ) +rte_panic ("No memory\n" ); + +for (i =0 ; i <RTE_DIM (args); i++) + eal_argv[i] =strdup (args[i]); + +if (rte_eal_init (eal_argc, eal_argv) <0 ) +rte_exit (EXIT_FAILURE ,"EAL init failed: is primary process running?\n" ); + +if (rte_eth_dev_count_avail () ==0 ) +rte_exit (EXIT_FAILURE ,"No Ethernet ports found\n" ); +} + +/* Create packet ring shared between callbacks and process */ +static struct rte_ring *create_ring (void ) +{ +struct rte_ring *ring; +size_t size, log2; + +/* Find next power of 2 >= size. */ + size = ring_size; + log2 =sizeof (size) *8 -__builtin_clzl (size -1 ); + size =1u << log2; + +if (size != ring_size) { +fprintf (stderr,"Ring size %u rounded up to %zu\n" , + ring_size, size); + ring_size = size; + } + + ring =rte_ring_lookup (RING_NAME ); +if (ring ==NULL ) { + ring =rte_ring_create (RING_NAME , ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); + } +return ring; +} + +static struct rte_mempool *create_mempool (void ) +{ +static const char pool_name[] ="capture_mbufs" ; +size_t num_mbufs =2 * ring_size; +struct rte_mempool *mp; + + mp =rte_mempool_lookup (pool_name); +if (mp) +return mp; + + mp =rte_pktmbuf_pool_create_by_ops (pool_name, num_mbufs, +MBUF_POOL_CACHE_SIZE ,0 , +rte_pcapng_mbuf_size (snaplen), +rte_socket_id (),"ring_mp_sc" ); +if (mp ==NULL ) +rte_exit (EXIT_FAILURE , +"Mempool (%s) creation failed: %s\n" , pool_name, +rte_strerror (rte_errno)); + +return mp; +} + +static void *create_output (void ) +{ +static char tmp_path[PATH_MAX ]; +struct utsname uts; +char os[256 ]; +int fd; + +/* If no filename specified make a tempfile name */ +if (output_name ==NULL ) { +struct interface *intf; +struct tm *tm; +time_t now; +char ts[32 ]; + + intf =TAILQ_FIRST (&interfaces); + now =time (NULL ); + tm =localtime (&now); +if (!tm) +rte_panic ("localtime failed\n" ); + +strftime (ts,sizeof (ts),"%Y%m%d%H%M%S" , tm); + +snprintf (tmp_path,sizeof (tmp_path), +"/tmp/%s_%u_%s_%s.%s" , + progname, intf->port, intf->name, ts, + use_pcapng ?"pcapng" :"pcap" ); + output_name = tmp_path; + } + +if (strcmp (output_name,"-" ) ==0 ) + fd =STDOUT_FILENO ; +else { +mode_t mode = group_read ?0640 :0600 ; + + fd =open (output_name,O_WRONLY |O_CREAT , mode); +if (fd <0 ) +rte_exit (EXIT_FAILURE ,"Can not open \"%s\": %s\n" , + output_name,strerror (errno)); + } + +if (use_pcapng) { +rte_pcapng_t *pcapng; + +if (uname (&uts) <0 ) +strcpy (os,"unknown" ); +else +snprintf (os,sizeof (os),"%s %s" , + uts.sysname, uts.release); + + pcapng =rte_pcapng_fdopen (fd, os,NULL ,version (), capture_comment); +if (pcapng ==NULL ) +rte_exit (EXIT_FAILURE ,"pcapng_fdopen failed: %s\n" , +strerror (rte_errno)); +return pcapng; + }else { +pcap_dumper_t *dumper; +pcap_t *pcap; + + pcap =pcap_open_dead_with_tstamp_precision (DLT_EN10MB , snaplen, +PCAP_TSTAMP_PRECISION_NANO ); +if (pcap ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_open_dead failed\n" ); + + dumper =pcap_dump_fopen (pcap,fdopen (fd,"w" )); +if (dumper ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_dump_fopen failed: %s\n" , +pcap_geterr (pcap)); +return dumper; + } +} + +static void enable_pdump (struct rte_ring *r,struct rte_mempool *mp) +{ +struct interface *intf; +uint32_t flags; +int ret; + + flags =RTE_PDUMP_FLAG_RXTX ; +if (use_pcapng) + flags |=RTE_PDUMP_FLAG_PCAPNG ; + + TAILQ_FOREACH(intf, &interfaces, next) { +if (promiscuous_mode) +rte_eth_promiscuous_enable (intf->port); + + ret =rte_pdump_enable_bpf (intf->port,RTE_PDUMP_ALL_QUEUES , + flags, snaplen, + r, mp, bpf_prm); +if (ret <0 ) +rte_exit (EXIT_FAILURE , +"Packet dump enable failed: %s\n" , +rte_strerror (-ret)); + } +} + +/* +* Show current count of captured packets +* with backspaces to overwrite last value. +*/ +static void show_count (uint64_t count) +{ +unsigned int i; +static unsigned int bt; + +for (i =0 ; i < bt; i++) +fputc ('\b' , stderr); + + bt =fprintf (stderr,"%" PRIu64" " , count); +} + +/* Write multiple packets in older pcap format */ +static ssize_t +pcap_write_packets (pcap_dumper_t *dumper, +struct rte_mbuf *pkts[],uint16_t n) +{ +uint8_t temp_data[snaplen]; +struct pcap_pkthdr header; +uint16_t i; +size_t total =0 ; + +gettimeofday (&header.ts,NULL ); + +for (i =0 ; i < n; i++) { +struct rte_mbuf *m = pkts[i]; + + header.len =rte_pktmbuf_pkt_len (m); + header.caplen =RTE_MIN (header.len, snaplen); + +pcap_dump ((u_char *)dumper, &header, +rte_pktmbuf_read (m,0 , header.caplen, temp_data)); + + total +=sizeof (header) + header.len; + } + +return total; +} + +/* Process all packets in ring and dump to capture file */ +static int process_ring (void *out,struct rte_ring *r) +{ +struct rte_mbuf *pkts[BURST_SIZE ]; +unsigned int avail, n; +static unsigned int empty_count; +ssize_t written; + + n =rte_ring_sc_dequeue_burst (r, (void **) pkts,BURST_SIZE , + &avail); +if (n ==0 ) { +/* don't consume endless amounts of cpu if idle */ +if (empty_count <SLEEP_THRESHOLD ) + ++empty_count; +else +usleep (10 ); +return 0 ; + } + + empty_count = (avail ==0 ); + +if (use_pcapng) + written =rte_pcapng_write_packets (out, pkts, n); +else + written =pcap_write_packets (out, pkts, n); + +rte_pktmbuf_free_bulk (pkts, n); + +if (written <0 ) +return -1 ; + + file_size += written; + packets_received += n; +if (!quiet) +show_count (packets_received); + +return 0 ; +} + +int main (int argc,char **argv) +{ +struct rte_ring *r; +struct rte_mempool *mp; +void *out; + + progname = argv[0 ]; + +dpdk_init (); +parse_opts (argc, argv); + +if (filter_str) +compile_filter (); + +if (TAILQ_EMPTY (&interfaces)) +set_default_interface (); + + r =create_ring (); + mp =create_mempool (); + out =create_output (); +if (out ==NULL ) +rte_exit (EXIT_FAILURE ,"can not open output file: %s\n" , +rte_strerror (rte_errno)); + + start_time =create_timestamp (); +enable_pdump (r, mp); + +signal (SIGINT , signal_handler); +signal (SIGPIPE ,SIG_IGN ); + +enable_primary_monitor (); + +if (!quiet) { +fprintf (stderr,"Packets captured: " ); +show_count (0 ); + } + +while (!__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) { +if (process_ring (out, r) <0 ) { +fprintf (stderr,"pcapng file write failed; %s\n" , +strerror (errno)); +break ; + } + +if (stop.size && file_size >= stop.size) +break ; + +if (stop.packets && packets_received >= stop.packets) +break ; + +if (stop.duration !=0 && +create_timestamp () - start_time > stop.duration) +break ; + } + + end_time =create_timestamp (); +disable_primary_monitor (); + +if (rte_eal_primary_proc_alive (NULL )) +report_packet_stats (out); + +if (use_pcapng) +rte_pcapng_close (out); +else +pcap_dump_close (out); + +cleanup_pdump_resources (); +rte_free (bpf_filter); +rte_ring_free (r); +rte_mempool_free (mp); + +return rte_eal_cleanup () ?EXIT_FAILURE :0 ; +}
app/dumpcap/meson.build new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +if not dpdk_conf.has('RTE_PORT_PCAP') + build = false + reason = 'missing dependency, "libpcap"' +endif + +if is_windows + build = false + reason = 'not supported on Windows' + subdir_done() +endif + +sources = files('main.c') +deps += ['ethdev', 'pdump', 'pcapng', 'bpf']
app/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2017-2019 Intel Corporation apps = [ + 'dumpcap', 'pdump', 'proc-info', 'test-acl',
Signature · 1 line
2.30.2
PATCH v6 9/10 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 86 +++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 228 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -62,6 +62,16 @@ New Features * Added bus-level parsing of the devargs syntax. * Kept compatibility with the legacy syntax as parsing fallback. +* **Enhance Packet capture.** + + * New dpdk-dumpcap program that has most of the features of the + wireshark dumpcap utility including capture of multiple interfaces, + stopping after number of bytes, packets. + * New library for writing pcapng packet capture files. + * Enhancement to the pdump library to support: + * Packet filter with BPF. + * Pcapng format with timestamps and meta-data. + * Fixes packet capture with stripped VLAN tags. Removed Items -------------
doc/guides/tools/dumpcap.rst new file +86 -0
86 lines not on this page · raw message
doc/guides/tools/index.rst +1 -0
@@ -8,6 +8,7 @@ DPDK Tools User Guides :maxdepth: 2 :numbered: + dumpcap proc_info pdump pmdinfo
Signature · 1 line
2.30.2
PATCH v6 10/10 MAINTAINERS: add entry for new pcapng and dumper
In reply to Stephen HemmingerClaim responsibility for the new code.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -1429,6 +1429,12 @@ F: app/test/test_pdump.* F: app/pdump/ F: doc/guides/tools/pdump.rst +Packet dump +M: Stephen Hemminger <[email protected]> +F: lib/pcapng/ +F: doc/guides/prog_guide/pcapng_lib.rst +F: app/dumpcap/ +F: doc/guides/tools/dumpcap.rst Packet Framework ----------------
Signature · 1 line
2.30.2
v7 cover Packet capture framework enhancements
In reply to Stephen HemmingerThis patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
v7 changes:
- add functional tests for pcapng lib
- bug fix for error returns in pcapng lib
- handle long osname on FreeBSD
- resolve almost all checkpatch issues
Stephen Hemminger (11):
librte_pcapng: add new library for writing pcapng files
lib: pdump is not supported on Windows
bpf: allow self-xor operation
bpf: add function to convert classic BPF to DPDK BPF
bpf: add function to dump eBPF instructions
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
test: add test for bpf_convert
test: add a test for pcapng library
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new pcapng and dumper
MAINTAINERS | 6 + app/dumpcap/main.c | 844 ++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + app/test/meson.build | 1 + app/test/test_bpf.c | 148 +++ app/test/test_pcapng.c | 190 ++++ doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +- doc/guides/howto/packet_capture_framework.rst | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 575 ++++++++++++ lib/bpf/bpf_dump.c | 118 +++ lib/bpf/bpf_validate.c | 8 +- lib/bpf/meson.build | 6 + lib/bpf/rte_bpf.h | 39 + lib/bpf/version.map | 7 + lib/meson.build | 6 +- lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 437 ++++++--- lib/pdump/rte_pdump.h | 110 ++- lib/pdump/version.map | 8 + 33 files changed, 3538 insertions(+), 216 deletions(-) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build create mode 100644 app/test/test_pcapng.c create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst create mode 100644 lib/bpf/bpf_convert.c create mode 100644 lib/bpf/bpf_dump.c create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
Signature · 1 line
2.30.2
PATCH v7 1/11 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 194 +++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 918 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +574 -0
574 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +194 -0
194 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH v7 2/11 lib: pdump is not supported on Windows
In reply to Stephen HemmingerThe current version of the pdump library was building on Windows, but it was useless since the pdump utility was not being built and Windows does not have multi-process support.
The new version of pdump with filtering now has dependency on bpf. But bpf library is not available on Windows.
--- lib/meson.build | 1 - 1 file changed, 1 deletion(-)
lib/meson.build +0 -1
@@ -85,7 +85,6 @@ if is_windows 'gro', 'gso', 'latencystats', - 'pdump', ] # only supported libraries for windows endif
Signature · 1 line
2.30.2
PATCH v7 3/11 bpf: allow self-xor operation
In reply to Stephen HemmingerWhen doing BPF filter program conversion, a common way to zero a register in single instruction is:
xor r7,r7
The BPF validator would not allow this because the value of r7 was undefined. But after this operation it always zero.
--- lib/bpf/bpf_validate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
lib/bpf/bpf_validate.c +6 -2
@@ -661,8 +661,12 @@ eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins) op =BPF_OP (ins->code); - err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , - (op !=BPF_NEG ) ? &rs :NULL ); +/* Allow self-xor as way to zero register */ +if (op ==BPF_XOR && ins->src_reg == ins->dst_reg) + err =NULL ; +else + err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , + (op !=BPF_NEG ) ? &rs :NULL );if (err !=NULL )return err;
Signature · 1 line
2.30.2
PATCH v7 4/11 bpf: add function to convert classic BPF to DPDK BPF
In reply to Stephen HemmingerThe pcap library emits classic BPF (32 bit) and is useful for creating filter programs. The DPDK BPF library only implements extended BPF (eBPF). Add an function to convert from old to new.
The rte_bpf_convert function uses rte_malloc to put the resulting program in hugepage shared memory so it can be passed from a secondary process to a primary process.
The code to convert was originally done as part of the Linux kernel implementation then converted to a userspace program. Both authors have agreed that it is allowable to license this as BSD licensed in DPDK.
--- lib/bpf/bpf_convert.c | 570 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 606 insertions(+) create mode 100644 lib/bpf/bpf_convert.c
lib/bpf/bpf_convert.c new file +570 -0
@@ -0,0 +1,570 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2021 Microsoft Corporation +* +* Based on bpf_convert_filter() in the Linux kernel sources +* and filter2xdp. +* +* Licensed as BSD with permission original authors. +* Copyright (C) 2017 Tobias Klauser +* Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com +*/ + +#include <assert.h> +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <rte_common.h> +#include <rte_bpf.h> +#include <rte_log.h> +#include <rte_malloc.h> +#include <rte_errno.h> + +/* Workaround name conflicts with libpcap */ +#define bpf_validate (f, len) bpf_validate_libpcap(f, len) +#include <pcap/pcap.h> +#include <pcap/bpf.h> +#undef bpf_validate + +#include "bpf_impl.h" +#include "bpf_def.h" + +#ifndef BPF_MAXINSNS +#define BPF_MAXINSNS 4096 +#endif + +/* +* Linux socket filter uses negative absolute offsets to +* reference ancillary data. +* +*/ +#define SKF_AD_OFF (-0x1000) +#define SKF_AD_PROTOCOL 0 +#define SKF_AD_PKTTYPE 4 +#define SKF_AD_IFINDEX 8 +#define SKF_AD_NLATTR 12 +#define SKF_AD_NLATTR_NEST 16 +#define SKF_AD_MARK 20 +#define SKF_AD_QUEUE 24 +#define SKF_AD_HATYPE 28 +#define SKF_AD_RXHASH 32 +#define SKF_AD_CPU 36 +#define SKF_AD_ALU_XOR_X 40 +#define SKF_AD_VLAN_TAG 44 +#define SKF_AD_VLAN_TAG_PRESENT 48 +#define SKF_AD_PAY_OFFSET 52 +#define SKF_AD_RANDOM 56 +#define SKF_AD_VLAN_TPID 60 +#define SKF_AD_MAX 64 + +/* ArgX, context and stack frame pointer register positions. Note, +* Arg1, Arg2, Arg3, etc are used as argument mappings of function +* calls in BPF_CALL instruction. +*/ +#define BPF_REG_ARG1 EBPF_REG_1 +#define BPF_REG_ARG2 EBPF_REG_2 +#define BPF_REG_ARG3 EBPF_REG_3 +#define BPF_REG_ARG4 EBPF_REG_4 +#define BPF_REG_ARG5 EBPF_REG_5 +#define BPF_REG_CTX EBPF_REG_6 +#define BPF_REG_FP EBPF_REG_10 + +/* Additional register mappings for converted user programs. */ +#define BPF_REG_A EBPF_REG_0 +#define BPF_REG_X EBPF_REG_7 +#define BPF_REG_TMP EBPF_REG_8 + +/* Helper macros for filter block array initializers. */ + +/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ + +#define EBPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_ALU32_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ + +#define BPF_ALU32_IMM (OP ,DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov, dst_reg = src_reg */ + +#define BPF_MOV64_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_MOV32_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* Short form of mov, dst_reg = imm32 */ + +#define BPF_MOV32_IMM (DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */ + +#define BPF_MOV32_RAW (TYPE ,DST ,SRC ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_SRC(TYPE), \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = IMM }) + +/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ + +#define BPF_LD_ABS (SIZE ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Memory load, dst_reg = *(uint *) (src_reg + off16) */ + +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Memory store, *(uint *) (dst_reg + off16) = src_reg */ + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ + +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +/* Raw code statement block */ + +#define BPF_RAW_INSN (CODE ,DST ,SRC ,OFF ,IMM ) \ + ((struct ebpf_insn) { \ + .code = CODE, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = IMM }) + +/* Program exit */ + +#define BPF_EXIT_INSN () \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | EBPF_EXIT, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = 0 }) + +/* +* Placeholder to convert BPF extensions like length and VLAN tag +* If and when DPDK BPF supports them. +*/ +static bool convert_bpf_load (const struct bpf_insn *fp, + struct ebpf_insn **new_insnp __rte_unused) +{ +switch (fp->k) { +case SKF_AD_OFF +SKF_AD_PROTOCOL : +case SKF_AD_OFF +SKF_AD_PKTTYPE : +case SKF_AD_OFF +SKF_AD_IFINDEX : +case SKF_AD_OFF +SKF_AD_HATYPE : +case SKF_AD_OFF +SKF_AD_MARK : +case SKF_AD_OFF +SKF_AD_RXHASH : +case SKF_AD_OFF +SKF_AD_QUEUE : +case SKF_AD_OFF +SKF_AD_VLAN_TAG : +case SKF_AD_OFF +SKF_AD_VLAN_TAG_PRESENT : +case SKF_AD_OFF +SKF_AD_VLAN_TPID : +case SKF_AD_OFF +SKF_AD_PAY_OFFSET : +case SKF_AD_OFF +SKF_AD_NLATTR : +case SKF_AD_OFF +SKF_AD_NLATTR_NEST : +case SKF_AD_OFF +SKF_AD_CPU : +case SKF_AD_OFF +SKF_AD_RANDOM : +case SKF_AD_OFF +SKF_AD_ALU_XOR_X : +/* TODO: Not implemented yet */ +RTE_BPF_LOG (ERR ,"BPF extension LOAD ABS %u not supported\n" , + fp->k); +return true; +default : +return false; + } +} + +static int bpf_convert_filter (const struct bpf_insn *prog,size_t len, +struct ebpf_insn *new_prog,uint32_t *new_len) +{ +unsigned int pass =0 ; +size_t new_flen =0 , target, i; +struct ebpf_insn *new_insn; +const struct bpf_insn *fp; +int *addrs =NULL ; +uint8_t bpf_src; + +if (len >BPF_MAXINSNS ) { +RTE_BPF_LOG (ERR ,"%s: cBPF program too long (%zu insns)\n" , + __func__, len); +return -EINVAL ; + } + +/* On second pass, allocate the new program */ +if (new_prog) { + addrs =calloc (len,sizeof (*addrs)); +if (addrs ==NULL ) +return -ENOMEM ; + } + +do_pass: + new_insn = new_prog; + fp = prog; + +/* Classic BPF related prologue emission. */ +if (new_insn) { +/* Classic BPF expects A and X to be reset first. These need +* to be guaranteed to be the first two instructions. +*/ + *new_insn++ =EBPF_ALU64_REG (BPF_XOR ,BPF_REG_A ,BPF_REG_A ); + *new_insn++ =EBPF_ALU64_REG (BPF_XOR ,BPF_REG_X ,BPF_REG_X ); + +/* All programs must keep CTX in callee saved BPF_REG_CTX. +* In eBPF case it's done by the compiler, here we need to +* do this ourself. Initial CTX is present in BPF_REG_ARG1. +*/ + *new_insn++ =BPF_MOV64_REG (BPF_REG_CTX ,BPF_REG_ARG1 ); + }else { + new_insn +=3 ; + } + +for (i =0 ; i < len; fp++, i++) { +struct ebpf_insn tmp_insns[6 ] = { }; +struct ebpf_insn *insn = tmp_insns; + +if (addrs) + addrs[i] = new_insn - new_prog; + +switch (fp->code) { +/* Absolute loads are how classic BPF accesses skb */ +case BPF_LD |BPF_ABS |BPF_W : +case BPF_LD |BPF_ABS |BPF_H : +case BPF_LD |BPF_ABS |BPF_B : +if (convert_bpf_load (fp, &insn)) + goto err; + + *insn =BPF_RAW_INSN (fp->code,0 ,0 ,0 , fp->k); +break ; + +case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_X : +/* For cBPF, don't cause floating point exception */ + *insn++ =BPF_MOV32_REG (BPF_REG_X ,BPF_REG_X ); + *insn++ =BPF_JMP_IMM (EBPF_JNE ,BPF_REG_X ,0 ,2 ); + *insn++ =BPF_ALU32_REG (BPF_XOR ,BPF_REG_A ,BPF_REG_A ); + *insn++ =BPF_EXIT_INSN (); +/* fallthrough */ +case BPF_ALU |BPF_ADD |BPF_X : +case BPF_ALU |BPF_ADD |BPF_K : +case BPF_ALU |BPF_SUB |BPF_X : +case BPF_ALU |BPF_SUB |BPF_K : +case BPF_ALU |BPF_AND |BPF_X : +case BPF_ALU |BPF_AND |BPF_K : +case BPF_ALU |BPF_OR |BPF_X : +case BPF_ALU |BPF_OR |BPF_K : +case BPF_ALU |BPF_LSH |BPF_X : +case BPF_ALU |BPF_LSH |BPF_K : +case BPF_ALU |BPF_RSH |BPF_X : +case BPF_ALU |BPF_RSH |BPF_K : +case BPF_ALU |BPF_XOR |BPF_X : +case BPF_ALU |BPF_XOR |BPF_K : +case BPF_ALU |BPF_MUL |BPF_X : +case BPF_ALU |BPF_MUL |BPF_K : +case BPF_ALU |BPF_DIV |BPF_K : +case BPF_ALU |BPF_MOD |BPF_K : +case BPF_ALU |BPF_NEG : +case BPF_LD |BPF_IND |BPF_W : +case BPF_LD |BPF_IND |BPF_H : +case BPF_LD |BPF_IND |BPF_B : +/* All arithmetic insns map as-is. */ + *insn =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); +break ; + +/* Jump transformation cannot use BPF block macros +* everywhere as offset calculation and target updates +* require a bit more work than the rest, i.e. jump +* opcodes map as-is, but offsets need adjustment. +*/ + +#define BPF_EMIT_JMP \ + do { \ + if (target >= len) \ + goto err; \ + insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ +/* Adjust pc relative offset for 2nd or 3rd insn. */ \ + insn->off -= insn - tmp_insns; \ + }while (0 ) + +case BPF_JMP |BPF_JA : + target = i + fp->k +1 ; + insn->code = fp->code; +BPF_EMIT_JMP ; +break ; + +case BPF_JMP |BPF_JEQ |BPF_K : +case BPF_JMP |BPF_JEQ |BPF_X : +case BPF_JMP |BPF_JSET |BPF_K : +case BPF_JMP |BPF_JSET |BPF_X : +case BPF_JMP |BPF_JGT |BPF_K : +case BPF_JMP |BPF_JGT |BPF_X : +case BPF_JMP |BPF_JGE |BPF_K : +case BPF_JMP |BPF_JGE |BPF_X : +if (BPF_SRC (fp->code) ==BPF_K && (int ) fp->k <0 ) { +/* BPF immediates are signed, zero extend +* immediate into tmp register and use it +* in compare insn. +*/ + *insn++ =BPF_MOV32_IMM (BPF_REG_TMP , fp->k); + + insn->dst_reg =BPF_REG_A ; + insn->src_reg =BPF_REG_TMP ; + bpf_src =BPF_X ; + }else { + insn->dst_reg =BPF_REG_A ; + insn->imm = fp->k; + bpf_src =BPF_SRC (fp->code); + insn->src_reg = bpf_src ==BPF_X ?BPF_REG_X :0 ; + } + +/* Common case where 'jump_false' is next insn. */ +if (fp->jf ==0 ) { + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; + target = i + fp->jt +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Convert JEQ into JNE when 'jump_true' is next insn. */ +if (fp->jt ==0 &&BPF_OP (fp->code) ==BPF_JEQ ) { + insn->code =BPF_JMP |EBPF_JNE | bpf_src; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Other jumps are mapped into two insns: Jxx and JA. */ + target = i + fp->jt +1 ; + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; +BPF_EMIT_JMP ; + insn++; + + insn->code =BPF_JMP |BPF_JA ; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + +/* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ +case BPF_LDX |BPF_MSH |BPF_B : +/* tmp = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_TMP ,BPF_REG_A ); +/* A = BPF_R0 = *(u8 *) (skb->data + K) */ + *insn++ =BPF_LD_ABS (BPF_B , fp->k); +/* A &= 0xf */ + *insn++ =BPF_ALU32_IMM (BPF_AND ,BPF_REG_A ,0xf ); +/* A <<= 2 */ + *insn++ =BPF_ALU32_IMM (BPF_LSH ,BPF_REG_A ,2 ); +/* X = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +/* A = tmp */ + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_TMP ); +break ; + +/* RET_K is remaped into 2 insns. RET_A case doesn't need an +* extra mov as EBPF_REG_0 is already mapped into BPF_REG_A. +*/ +case BPF_RET |BPF_A : +case BPF_RET |BPF_K : +if (BPF_RVAL (fp->code) ==BPF_K ) { + *insn++ =BPF_MOV32_RAW (BPF_K ,EBPF_REG_0 , +0 , fp->k); + } + *insn =BPF_EXIT_INSN (); +break ; + +/* Store to stack. */ +case BPF_ST : +case BPF_STX : + *insn =BPF_STX_MEM (BPF_W ,BPF_REG_FP ,BPF_CLASS (fp->code) == +BPF_ST ?BPF_REG_A :BPF_REG_X , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* Load from stack. */ +case BPF_LD |BPF_MEM : +case BPF_LDX |BPF_MEM : + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_FP , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* A = K or X = K */ +case BPF_LD |BPF_IMM : +case BPF_LDX |BPF_IMM : + *insn =BPF_MOV32_IMM (BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X , fp->k); +break ; + +/* X = A */ +case BPF_MISC |BPF_TAX : + *insn =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +break ; + +/* A = X */ +case BPF_MISC |BPF_TXA : + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_X ); +break ; + +/* A = mbuf->len or X = mbuf->len */ +case BPF_LD |BPF_W |BPF_LEN : +case BPF_LDX |BPF_W |BPF_LEN : +/* BPF_ABS/BPF_IND implicitly expect mbuf ptr in R6 */ + + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_CTX , + offsetof(struct rte_mbuf , pkt_len)); +break ; + +/* Unknown instruction. */ +default : +RTE_BPF_LOG (ERR ,"%s: Unknown instruction!: %#x\n" , + __func__, fp->code); + goto err; + } + + insn++; +if (new_prog) +memcpy (new_insn, tmp_insns, +sizeof (*insn) * (insn - tmp_insns)); + new_insn += insn - tmp_insns; + } + +if (!new_prog) { +/* Only calculating new length. */ + *new_len = new_insn - new_prog; +return 0 ; + } + + pass++; +if ((ptrdiff_t )new_flen != new_insn - new_prog) { + new_flen = new_insn - new_prog; +if (pass >2 ) + goto err; + goto do_pass; + } + +free (addrs); +assert (*new_len == new_flen); + +return 0 ; +err: +free (addrs); +return -1 ; +} + +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog) +{ +struct rte_bpf_prm *prm =NULL ; +struct ebpf_insn *ebpf =NULL ; +uint32_t ebpf_len =0 ; +int ret; + +if (prog ==NULL ) { +RTE_BPF_LOG (ERR ,"%s: NULL program\n" , __func__); + rte_errno =EINVAL ; +return NULL ; + } + +/* 1st pass: calculate the eBPF program length */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len,NULL , &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot get eBPF length\n" , __func__); + rte_errno = -ret; +return NULL ; + } + +RTE_BPF_LOG (DEBUG ,"%s: prog len cBPF=%u -> eBPF=%u\n" , + __func__, prog->bf_len, ebpf_len); + + prm =rte_zmalloc ("bpf_filter" , +sizeof (*prm) + ebpf_len *sizeof (*ebpf),0 ); +if (prm ==NULL ) { + rte_errno =ENOMEM ; +return NULL ; + } + +/* The EPBF instructions in this case are right after the header */ + ebpf = (void *)(prm +1 ); + +/* 2nd pass: remap cBPF to eBPF instructions */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot convert cBPF to eBPF\n" , __func__); +free (prm); + rte_errno = -ret; +return NULL ; + } + + prm->ins = ebpf; + prm->nb_ins = ebpf_len; + +/* Classic BPF programs use mbufs */ + prm->prog_arg.type =RTE_BPF_ARG_PTR_MBUF ; + prm->prog_arg.size =sizeof (struct rte_mbuf ); + +return prm; +}
lib/bpf/meson.build +5 -0
@@ -25,3 +25,8 @@ if dep.found() sources += files('bpf_load_elf.c') ext_deps += dep endif + +if dpdk_conf.has('RTE_PORT_PCAP') + sources += files('bpf_convert.c') + ext_deps += pcap_dep +endif
lib/bpf/rte_bpf.h +25 -0
@@ -198,6 +198,31 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +#ifdef RTE_PORT_PCAP + +struct bpf_program ; + +/** +* Convert a Classic BPF program from libpcap into a DPDK BPF code. +* +* @param prog +* Classic BPF program from pcap_compile(). +* @param prm +* Result Extended BPF program. +* @return +* Pointer to BPF program (allocated with *rte_malloc*) +* that is used in future BPF operations, +* or NULL on error, with error code set in rte_errno. +* Possible rte_errno errors include: +* - EINVAL - invalid parameter passed to function +* - ENOMEM - can't reserve enough memory +*/ +__rte_experimental +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog); + +#endif + #ifdef __cplusplus }#endif
lib/bpf/version.map +6 -0
@@ -14,3 +14,9 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_bpf_convert; +};
Signature · 1 line
2.30.2
PATCH v7 5/11 bpf: add function to dump eBPF instructions
In reply to Stephen HemmingerWhen debugging converted (and other) programs it is useful to see disassembled eBPF output.
--- lib/bpf/bpf_convert.c | 7 ++- lib/bpf/bpf_dump.c | 118 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 +++++ lib/bpf/version.map | 1 + 5 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 lib/bpf/bpf_dump.c
lib/bpf/bpf_convert.c +6 -1
@@ -331,7 +331,12 @@ static int bpf_convert_filter(const struct bpf_insn *prog, size_t len,case BPF_LD |BPF_IND |BPF_H :case BPF_LD |BPF_IND |BPF_B :/* All arithmetic insns map as-is. */ - *insn =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); + insn->code = fp->code; + insn->dst_reg =BPF_REG_A ; + bpf_src =BPF_SRC (fp->code); + insn->src_reg = bpf_src ==BPF_X ?BPF_REG_X :0 ; + insn->off =0 ; + insn->imm = fp->k;break ;/* Jump transformation cannot use BPF block macros
lib/bpf/bpf_dump.c new file +118 -0
118 lines not on this page · raw message
lib/bpf/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2018 Intel Corporation sources = files('bpf.c', + 'bpf_dump.c', 'bpf_exec.c', 'bpf_load.c', 'bpf_pkt.c',
lib/bpf/rte_bpf.h +14 -0
@@ -198,6 +198,20 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +/** +* Dump epf instructions to a file. +* +* @param f +* A pointer to a file for output +* @param buf +* A pointer to BPF instructions +* @param len +* Number of BPF instructions to dump. +*/ +__rte_experimental +void +rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len); +#ifdef RTE_PORT_PCAP struct bpf_program ;
lib/bpf/version.map +1 -0
@@ -19,4 +19,5 @@ EXPERIMENTAL { global: rte_bpf_convert; + rte_bpf_dump; };
Signature · 1 line
2.30.2
PATCH v7 6/11 pdump: support pcapng and filtering
In reply to Stephen HemmingerThis enhances the DPDK pdump library to support new pcapng format and filtering via BPF.
The internal client/server protocol is changed to support two versions: the original pdump basic version and a new pcapng version.
The internal version number (not part of exposed API or ABI) is intentionally increased to cause any attempt to try mismatched primary/secondary process to fail.
Add new API to do allow filtering of captured packets with DPDK BPF (eBPF) filter program. It keeps statistics
on packets captured, filtered, and missed (because ring was full).
--- lib/meson.build | 4 +- lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 437 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 110 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 435 insertions(+), 126 deletions(-)
lib/meson.build +2 -2
@@ -26,6 +26,7 @@ libraries = [ 'timer', # eventdev depends on this 'acl', 'bbdev', + 'bpf', 'bitratestats', 'cfgfile', 'compressdev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'rib',
@@ -55,10 +55,10 @@ libraries = [ 'ipsec', # ipsec lib depends on net, crypto and security 'fib', #fib lib depends on rib 'port', # pkt framework libs which use other libs from above + 'pdump', # pdump lib depends on bpf pcapng 'table', 'pipeline', 'flow_classify', # flow_classify lib depends on pkt framework table lib - 'bpf', 'graph', 'node', ]
lib/pdump/meson.build +1 -1
@@ -3,4 +3,4 @@ sources = files('rte_pdump.c') headers = files('rte_pdump.h') -deps += ['ethdev'] +deps += ['ethdev', 'bpf', 'pcapng']
lib/pdump/rte_pdump.c +317 -120
644 lines not on this page · raw message
lib/pdump/rte_pdump.h +107 -3
152 lines not on this page · raw message
lib/pdump/version.map +8 -0
@@ -10,3 +10,11 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_pdump_enable_bpf; + rte_pdump_enable_bpf_by_deviceid; + rte_pdump_stats; +};
Signature · 1 line
2.30.2
PATCH v7 7/11 app/dumpcap: add new packet capture application
In reply to Stephen HemmingerThis is a new packet capture application to replace existing pdump. The new application works like Wireshark dumpcap program and supports the pdump API features.
It is not complete yet some features such as filtering are not implemented.
--- app/dumpcap/main.c | 844 ++++++++++++++++++++++++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + 3 files changed, 861 insertions(+) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build
app/dumpcap/main.c new file +844 -0
@@ -0,0 +1,844 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019-2020 Microsoft Corporation +* +* DPDK application to dump network traffic +* This is designed to look and act like the Wireshark +* dumpcap program. +*/ + +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <inttypes.h> +#include <limits.h> +#include <signal.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/queue.h> +#include <sys/types.h> +#include <sys/utsname.h> +#include <time.h> +#include <unistd.h> + +#include <rte_alarm.h> +#include <rte_bpf.h> +#include <rte_config.h> +#include <rte_debug.h> +#include <rte_eal.h> +#include <rte_errno.h> +#include <rte_ethdev.h> +#include <rte_lcore.h> +#include <rte_malloc.h> +#include <rte_mbuf.h> +#include <rte_mempool.h> +#include <rte_pcapng.h> +#include <rte_pdump.h> +#include <rte_ring.h> +#include <rte_string_fns.h> +#include <rte_time.h> +#include <rte_version.h> + +#include <pcap/pcap.h> +#include <pcap/bpf.h> + +#define RING_NAME "capture-ring" +#define MONITOR_INTERVAL (500 * 1000) +#define MBUF_POOL_CACHE_SIZE 32 +#define BURST_SIZE 32 +#define SLEEP_THRESHOLD 1000 + +/* command line flags */ +static const char *progname; +static bool quit_signal; +static bool group_read; +static bool quiet; +static bool promiscuous_mode = true; +static bool use_pcapng = true; +static char *output_name; +static const char *filter_str; +static unsigned int ring_size =2048 ; +static const char *capture_comment; +static uint32_t snaplen =RTE_MBUF_DEFAULT_BUF_SIZE ; +static bool dump_bpf; +static struct { +uint64_t duration;/* nanoseconds */ +unsigned long packets;/* number of packets in file */ +size_t size;/* file size (bytes) */ +} stop; + +/* Running state */ +static struct rte_bpf_prm *bpf_prm; +static uint64_t start_time, end_time; +static uint64_t packets_received; +static size_t file_size; + +struct interface { +TAILQ_ENTRY (interface) next; +uint16_t port; +char name[RTE_ETH_NAME_MAX_LEN ]; + +struct rte_rxtx_callback *rx_cb[RTE_MAX_QUEUES_PER_PORT ]; +}; + +TAILQ_HEAD (interface_list, interface); +static struct interface_list interfaces =TAILQ_HEAD_INITIALIZER (interfaces); +static struct interface *port2intf[RTE_MAX_ETHPORTS ]; + +/* Can do either pcap or pcapng format output */ +typedef union { +rte_pcapng_t *pcapng; +pcap_dumper_t *dumper; +}dumpcap_out_t ; + +static void usage (void ) +{ +printf ("Usage: %s [options] ...\n\n" , progname); +printf ("Capture Interface:\n" +" -i <interface> name or port index of interface\n" +" -f <capture filter> packet filter in libpcap filter syntax\n" ); +printf (" -s <snaplen>, --snapshot-length <snaplen>\n" +" packet snapshot length (def: %u)\n" , +RTE_MBUF_DEFAULT_BUF_SIZE ); +printf (" -p, --no-promiscuous-mode\n" +" don't capture in promiscuous mode\n" +" -D, --list-interfaces print list of interfaces and exit\n" +" -d print generated BPF code for capture filter\n" +"\n" +"Stop conditions:\n" +" -c <packet count> stop after n packets (def: infinite)\n" +" -a <autostop cond.> ..., --autostop <autostop cond.> ...\n" +" duration:NUM - stop after NUM seconds\n" +" filesize:NUM - stop this file after NUM kB\n" +" packets:NUM - stop after NUM packets\n" +"Output (files):\n" +" -w <filename> name of file to save (def: tempfile)\n" +" -g enable group read access on the output file(s)\n" +" -n use pcapng format instead of pcap (default)\n" +" -P use libpcap format instead of pcapng\n" +" --capture-comment <comment>\n" +" add a capture comment to the output file\n" +"\n" +"Miscellaneous:\n" +" -q don't report packet capture counts\n" +" -v, --version print version information and exit\n" +" -h, --help display this help and exit\n" +"\n" +"Use Ctrl-C to stop capturing at any time.\n" ); +} + +static const char *version (void ) +{ +static char str[128 ]; + +snprintf (str,sizeof (str), +"%s 1.0 (%s)\n" , progname,rte_version ()); +return str; +} + +/* Parse numeric argument from command line */ +static unsigned long get_uint (const char *arg,const char *name, +unsigned int limit) +{ +unsigned long u; +char *endp; + + u =strtoul (arg, &endp,0 ); +if (*arg =='\0' || *endp !='\0' ) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is not a valid number\n" , + name, arg); +if (limit && u > limit) +rte_exit (EXIT_FAILURE , +"Specified %s \"%s\" is too large (greater than %u)\n" , + name, arg, limit); + +return u; +} + +/* Set auto stop values */ +static void auto_stop (char *opt) +{ +char *value, *endp; + + value =strchr (opt,':' ); +if (value ==NULL ) +rte_exit (EXIT_FAILURE , +"Missing colon in auto stop parameter\n" ); + + *value++ ='\0' ; +if (strcmp (opt,"duration" ) ==0 ) { +double interval =strtod (value, &endp); + +if (*value =='\0' || *endp !='\0' || interval <=0 ) +rte_exit (EXIT_FAILURE , +"Invalid duration \"%s\"\n" , value); + stop.duration =NSEC_PER_SEC * interval; + }else if (strcmp (opt,"filesize" ) ==0 ) { + stop.size =get_uint (value,"filesize" ,0 ) *1024 ; + }else if (strcmp (opt,"packets" ) ==0 ) { + stop.packets =get_uint (value,"packets" ,0 ); + }else { +rte_exit (EXIT_FAILURE , +"Unknown autostop parameter \"%s\"\n" , opt); + } +} + +/* Add interface to list of interfaces to capture */ +static void add_interface (uint16_t port,const char *name) +{ +struct interface *intf; + + intf =malloc (sizeof (*intf)); +if (!intf) +rte_exit (EXIT_FAILURE ,"no memory for interface\n" ); + +memset (intf,0 ,sizeof (*intf)); +strlcpy (intf->name, name,sizeof (intf->name)); + +printf ("Capturing on '%s'\n" , name); + + port2intf[port] = intf; +TAILQ_INSERT_TAIL (&interfaces, intf, next); +} + +/* Select all valid DPDK interfaces */ +static void select_all_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); + } +} + +/* +* Choose interface to capture if no -i option given. +* Select the first DPDK port, this matches what dumpcap does. +*/ +static void set_default_interface (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +add_interface (p, name); +return ; + } +rte_exit (EXIT_FAILURE ,"No usable interfaces found\n" ); +} + +/* Lookup interface by name or port and add it to the list */ +static void select_interface (const char *arg) +{ +uint16_t port; + +if (strcmp (arg,"*" )) +select_all_interfaces (); +else if (rte_eth_dev_get_port_by_name (arg, &port) ==0 ) +add_interface (port, arg); +else { +char name[RTE_ETH_NAME_MAX_LEN ]; + + port =get_uint (arg,"port_number" ,UINT16_MAX ); +if (rte_eth_dev_get_name_by_port (port, name) <0 ) +rte_exit (EXIT_FAILURE ,"Invalid port number %u\n" , + port); +add_interface (port, name); + } +} + +/* Display list of possible interfaces that can be used. */ +static void show_interfaces (void ) +{ +char name[RTE_ETH_NAME_MAX_LEN ]; +uint16_t p; + +RTE_ETH_FOREACH_DEV (p) { +if (rte_eth_dev_get_name_by_port (p, name) <0 ) +continue ; +printf ("%u. %s\n" , p, name); + } +} + +static void compile_filter (void ) +{ +struct bpf_program bf; +pcap_t *pcap; + + pcap =pcap_open_dead (DLT_EN10MB , snaplen); +if (!pcap) +rte_exit (EXIT_FAILURE ,"can not open pcap\n" ); + +if (pcap_compile (pcap, &bf, filter_str, +1 ,PCAP_NETMASK_UNKNOWN ) !=0 ) +rte_exit (EXIT_FAILURE ,"pcap filter string not valid (%s)\n" , +pcap_geterr (pcap)); + + bpf_prm =rte_bpf_convert (&bf); +if (bpf_prm ==NULL ) +rte_exit (EXIT_FAILURE , +"bpf convert failed\n" ); + +if (dump_bpf) { +printf ("cBPF program (%u insns)\n" , bf.bf_len); +bpf_dump (&bf,1 ); +printf ("\neBPF program (%u insns)\n" , bpf_prm->nb_ins); +rte_bpf_dump (stdout, bpf_prm->ins, bpf_prm->nb_ins); +exit (0 ); + } + +/* Don't care about original program any more */ +pcap_freecode (&bf); +pcap_close (pcap); +} + +/* +* Parse command line options. +* These are chosen to be similar to dumpcap command. +*/ +static void parse_opts (int argc,char **argv) +{ +static const struct option long_options[] = { + {"autostop" , required_argument,NULL ,'a' }, + {"capture-comment" , required_argument,NULL ,0 }, + {"help" , no_argument,NULL ,'h' }, + {"interface" , required_argument,NULL ,'i' }, + {"list-interfaces" , no_argument,NULL ,'D' }, + {"no-promiscuous-mode" , no_argument,NULL ,'p' }, + {"output-file" , required_argument,NULL ,'w' }, + {"ring-buffer" , required_argument,NULL ,'b' }, + {"snapshot-length" , required_argument,NULL ,'s' }, + {"version" , no_argument,NULL ,'v' }, + {NULL }, + }; +int option_index, c; + +for (;;) { + c =getopt_long (argc, argv,"a:b:c:dDf:ghi:nN:pPqs:vw:" , + long_options, &option_index); +if (c ==-1 ) +break ; + +switch (c) { +case 0 : +switch (option_index) { +case 0 : + capture_comment = optarg; +break ; +default : +usage (); +exit (1 ); + } +break ; +case 'a' : +auto_stop (optarg); +break ; +case 'b' : +rte_exit (EXIT_FAILURE , +"multiple files not implemented\n" ); +break ; +case 'c' : + stop.packets =get_uint (optarg,"packet_count" ,0 ); +break ; +case 'd' : + dump_bpf = true; +break ; +case 'D' : +show_interfaces (); +exit (0 ); +case 'f' : + filter_str = optarg; +break ; +case 'g' : + group_read = true; +break ; +case 'h' : +printf ("%s\n\n" ,version ()); +usage (); +exit (0 ); +case 'i' : +select_interface (optarg); +break ; +case 'n' : + use_pcapng = true; +break ; +case 'N' : + ring_size =get_uint (optarg,"packet_limit" ,0 ); +break ; +case 'p' : + promiscuous_mode = false; +break ; +case 'P' : + use_pcapng = false; +break ; +case 'q' : + quiet = true; +break ; +case 's' : + snaplen =get_uint (optarg,"snap_len" ,0 ); +break ; +case 'w' : + output_name = optarg; +break ; +case 'v' : +printf ("%s\n" ,version ()); +exit (0 ); +default : +fprintf (stderr,"Invalid option: %s\n" , + argv[optind -1 ]); +usage (); +exit (1 ); + } + } +} + +static void +signal_handler(int sig_num __rte_unused) +{ +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); +} + +/* Return the time since 1/1/1970 in nanoseconds */ +static uint64_t create_timestamp (void ) +{ +struct timespec now; + +clock_gettime (CLOCK_MONOTONIC , &now); +return rte_timespec_to_ns (&now); +} + +static void +cleanup_pdump_resources (void ) +{ +struct interface *intf; + + TAILQ_FOREACH(intf, &interfaces, next) { +rte_pdump_disable (intf->port, +RTE_PDUMP_ALL_QUEUES ,RTE_PDUMP_FLAG_RXTX ); +if (promiscuous_mode) +rte_eth_promiscuous_disable (intf->port); + } +} + +/* Alarm signal handler, used to check that primary process */ +static void +monitor_primary(void *arg __rte_unused) +{ +if (__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) +return ; + +if (rte_eal_primary_proc_alive (NULL )) { +rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); + }else { +fprintf (stderr, +"Primary process is no longer active, exiting...\n" ); +__atomic_store_n (&quit_signal, true, __ATOMIC_RELAXED); + } +} + +/* Setup handler to check when primary exits. */ +static void +enable_primary_monitor (void ) +{ +int ret; + +/* Once primary exits, so will pdump. */ + ret =rte_eal_alarm_set (MONITOR_INTERVAL , monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to enable monitor:%d\n" , ret); +} + +static void +disable_primary_monitor (void ) +{ +int ret; + + ret =rte_eal_alarm_cancel (monitor_primary,NULL ); +if (ret <0 ) +fprintf (stderr,"Fail to disable monitor:%d\n" , ret); +} + +static void +report_packet_stats (dumpcap_out_t out) +{ +struct rte_pdump_stats pdump_stats; +struct interface *intf; +uint64_t ifrecv, ifdrop; +double percent; + +fputc ('\n' , stderr); + TAILQ_FOREACH(intf, &interfaces, next) { +if (rte_pdump_stats (intf->port, &pdump_stats) <0 ) +continue ; + +/* do what Wiretap does */ + ifrecv = pdump_stats.accepted + pdump_stats.filtered; + ifdrop = pdump_stats.nombuf + pdump_stats.ringfull; + +if (use_pcapng) +rte_pcapng_write_stats (out.pcapng, intf->port,NULL , + start_time, end_time, + ifrecv, ifdrop); + +if (ifrecv ==0 ) + percent =0 ; +else + percent =100. * ifrecv / (ifrecv + ifdrop); + +fprintf (stderr, +"Packets received/dropped on interface '%s': " +"%" PRIu64"/%" PRIu64" (%.1f)\n" , + intf->name, ifrecv, ifdrop, percent); + } +} + +/* +* Start DPDK EAL with arguments. +* Unlike most DPDK programs, this application does not use the +* typical EAL command line arguments. +* We don't want to expose all the DPDK internals to the user. +*/ +static void dpdk_init (void ) +{ +static const char *const args[] = { +"dumpcap" ,"--proc-type" ,"secondary" , +"--log-level" ,"notice" + + }; +const int eal_argc =RTE_DIM (args); +char **eal_argv; +unsigned int i; + +/* DPDK API requires mutable versions of command line arguments. */ + eal_argv =calloc (eal_argc +1 ,sizeof (char *)); +if (eal_argv ==NULL ) +rte_panic ("No memory\n" ); + + eal_argv[0 ] =strdup (progname); +for (i =1 ; i <RTE_DIM (args); i++) + eal_argv[i] =strdup (args[i]); + +if (rte_eal_init (eal_argc, eal_argv) <0 ) +rte_exit (EXIT_FAILURE ,"EAL init failed: is primary process running?\n" ); + +if (rte_eth_dev_count_avail () ==0 ) +rte_exit (EXIT_FAILURE ,"No Ethernet ports found\n" ); +} + +/* Create packet ring shared between callbacks and process */ +static struct rte_ring *create_ring (void ) +{ +struct rte_ring *ring; +size_t size, log2; + +/* Find next power of 2 >= size. */ + size = ring_size; + log2 =sizeof (size) *8 -__builtin_clzl (size -1 ); + size =1u << log2; + +if (size != ring_size) { +fprintf (stderr,"Ring size %u rounded up to %zu\n" , + ring_size, size); + ring_size = size; + } + + ring =rte_ring_lookup (RING_NAME ); +if (ring ==NULL ) { + ring =rte_ring_create (RING_NAME , ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); + } +return ring; +} + +static struct rte_mempool *create_mempool (void ) +{ +static const char pool_name[] ="capture_mbufs" ; +size_t num_mbufs =2 * ring_size; +struct rte_mempool *mp; + + mp =rte_mempool_lookup (pool_name); +if (mp) +return mp; + + mp =rte_pktmbuf_pool_create_by_ops (pool_name, num_mbufs, +MBUF_POOL_CACHE_SIZE ,0 , +rte_pcapng_mbuf_size (snaplen), +rte_socket_id (),"ring_mp_sc" ); +if (mp ==NULL ) +rte_exit (EXIT_FAILURE , +"Mempool (%s) creation failed: %s\n" , pool_name, +rte_strerror (rte_errno)); + +return mp; +} + +/* +* Get Operating System information. +* Returns an string allocated via malloc(). +*/ +static char *get_os_info (void ) +{ +struct utsname uts; +char *osname =NULL ; + +if (uname (&uts) <0 ) +return NULL ; + +if (asprintf (&osname,"%s %s" , + uts.sysname, uts.release) ==-1 ) +return NULL ; + +return osname; +} + +static dumpcap_out_t create_output (void ) +{ +dumpcap_out_t ret; +static char tmp_path[PATH_MAX ]; +int fd; + +/* If no filename specified make a tempfile name */ +if (output_name ==NULL ) { +struct interface *intf; +struct tm *tm; +time_t now; +char ts[32 ]; + + intf =TAILQ_FIRST (&interfaces); + now =time (NULL ); + tm =localtime (&now); +if (!tm) +rte_panic ("localtime failed\n" ); + +strftime (ts,sizeof (ts),"%Y%m%d%H%M%S" , tm); + +snprintf (tmp_path,sizeof (tmp_path), +"/tmp/%s_%u_%s_%s.%s" , + progname, intf->port, intf->name, ts, + use_pcapng ?"pcapng" :"pcap" ); + output_name = tmp_path; + } + +if (strcmp (output_name,"-" ) ==0 ) + fd =STDOUT_FILENO ; +else { +mode_t mode = group_read ?0640 :0600 ; + + fd =open (output_name,O_WRONLY |O_CREAT , mode); +if (fd <0 ) +rte_exit (EXIT_FAILURE ,"Can not open \"%s\": %s\n" , + output_name,strerror (errno)); + } + +if (use_pcapng) { +char *os =get_os_info (); + + ret.pcapng =rte_pcapng_fdopen (fd, os,NULL , +version (), capture_comment); +if (ret.pcapng ==NULL ) +rte_exit (EXIT_FAILURE ,"pcapng_fdopen failed: %s\n" , +strerror (rte_errno)); +free (os); + }else { +pcap_t *pcap; + + pcap =pcap_open_dead_with_tstamp_precision (DLT_EN10MB , snaplen, +PCAP_TSTAMP_PRECISION_NANO ); +if (pcap ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_open_dead failed\n" ); + + ret.dumper =pcap_dump_fopen (pcap,fdopen (fd,"w" )); +if (ret.dumper ==NULL ) +rte_exit (EXIT_FAILURE ,"pcap_dump_fopen failed: %s\n" , +pcap_geterr (pcap)); + } + +return ret; +} + +static void enable_pdump (struct rte_ring *r,struct rte_mempool *mp) +{ +struct interface *intf; +uint32_t flags; +int ret; + + flags =RTE_PDUMP_FLAG_RXTX ; +if (use_pcapng) + flags |=RTE_PDUMP_FLAG_PCAPNG ; + + TAILQ_FOREACH(intf, &interfaces, next) { +if (promiscuous_mode) +rte_eth_promiscuous_enable (intf->port); + + ret =rte_pdump_enable_bpf (intf->port,RTE_PDUMP_ALL_QUEUES , + flags, snaplen, + r, mp, bpf_prm); +if (ret <0 ) +rte_exit (EXIT_FAILURE , +"Packet dump enable failed: %s\n" , +rte_strerror (-ret)); + } +} + +/* +* Show current count of captured packets +* with backspaces to overwrite last value. +*/ +static void show_count (uint64_t count) +{ +unsigned int i; +static unsigned int bt; + +for (i =0 ; i < bt; i++) +fputc ('\b' , stderr); + + bt =fprintf (stderr,"%" PRIu64" " , count); +} + +/* Write multiple packets in older pcap format */ +static ssize_t +pcap_write_packets (pcap_dumper_t *dumper, +struct rte_mbuf *pkts[],uint16_t n) +{ +uint8_t temp_data[snaplen]; +struct pcap_pkthdr header; +uint16_t i; +size_t total =0 ; + +gettimeofday (&header.ts,NULL ); + +for (i =0 ; i < n; i++) { +struct rte_mbuf *m = pkts[i]; + + header.len =rte_pktmbuf_pkt_len (m); + header.caplen =RTE_MIN (header.len, snaplen); + +pcap_dump ((u_char *)dumper, &header, +rte_pktmbuf_read (m,0 , header.caplen, temp_data)); + + total +=sizeof (header) + header.len; + } + +return total; +} + +/* Process all packets in ring and dump to capture file */ +static int process_ring (dumpcap_out_t out,struct rte_ring *r) +{ +struct rte_mbuf *pkts[BURST_SIZE ]; +unsigned int avail, n; +static unsigned int empty_count; +ssize_t written; + + n =rte_ring_sc_dequeue_burst (r, (void **) pkts,BURST_SIZE , + &avail); +if (n ==0 ) { +/* don't consume endless amounts of cpu if idle */ +if (empty_count <SLEEP_THRESHOLD ) + ++empty_count; +else +usleep (10 ); +return 0 ; + } + + empty_count = (avail ==0 ); + +if (use_pcapng) + written =rte_pcapng_write_packets (out.pcapng, pkts, n); +else + written =pcap_write_packets (out.dumper, pkts, n); + +rte_pktmbuf_free_bulk (pkts, n); + +if (written <0 ) +return -1 ; + + file_size += written; + packets_received += n; +if (!quiet) +show_count (packets_received); + +return 0 ; +} + +int main (int argc,char **argv) +{ +struct rte_ring *r; +struct rte_mempool *mp; +dumpcap_out_t out; + + progname = argv[0 ]; + +dpdk_init (); +parse_opts (argc, argv); + +if (filter_str) +compile_filter (); + +if (TAILQ_EMPTY (&interfaces)) +set_default_interface (); + + r =create_ring (); + mp =create_mempool (); + out =create_output (); + + start_time =create_timestamp (); +enable_pdump (r, mp); + +signal (SIGINT , signal_handler); +signal (SIGPIPE ,SIG_IGN ); + +enable_primary_monitor (); + +if (!quiet) { +fprintf (stderr,"Packets captured: " ); +show_count (0 ); + } + +while (!__atomic_load_n (&quit_signal, __ATOMIC_RELAXED)) { +if (process_ring (out, r) <0 ) { +fprintf (stderr,"pcapng file write failed; %s\n" , +strerror (errno)); +break ; + } + +if (stop.size && file_size >= stop.size) +break ; + +if (stop.packets && packets_received >= stop.packets) +break ; + +if (stop.duration !=0 && +create_timestamp () - start_time > stop.duration) +break ; + } + + end_time =create_timestamp (); +disable_primary_monitor (); + +if (rte_eal_primary_proc_alive (NULL )) +report_packet_stats (out); + +if (use_pcapng) +rte_pcapng_close (out.pcapng); +else +pcap_dump_close (out.dumper); + +cleanup_pdump_resources (); +rte_free (bpf_filter); +rte_ring_free (r); +rte_mempool_free (mp); + +return rte_eal_cleanup () ?EXIT_FAILURE :0 ; +}
app/dumpcap/meson.build new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +if not dpdk_conf.has('RTE_PORT_PCAP') + build = false + reason = 'missing dependency, "libpcap"' +endif + +if is_windows + build = false + reason = 'not supported on Windows' + subdir_done() +endif + +sources = files('main.c') +deps += ['ethdev', 'pdump', 'pcapng', 'bpf']
app/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2017-2019 Intel Corporation apps = [ + 'dumpcap', 'pdump', 'proc-info', 'test-acl',
Signature · 1 line
2.30.2
PATCH v7 8/11 test: add test for bpf_convert
In reply to Stephen HemmingerAdd some functional tests for the Classic BPF to DPDK BPF converter.
--- app/test/test_bpf.c | 148 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+)
app/test/test_bpf.c +148 -0
@@ -10,6 +10,7 @@#include <rte_memory.h> #include <rte_debug.h> #include <rte_hexdump.h> +#include <rte_malloc.h> #include <rte_random.h> #include <rte_byteorder.h> #include <rte_errno.h>
@@ -3233,3 +3234,150 @@ test_bpf(void) }REGISTER_TEST_COMMAND (bpf_autotest, test_bpf); + +#ifdef RTE_PORT_PCAP +#include <pcap/pcap.h> + +static void +test_bpf_dump (struct bpf_program *cbf,const struct rte_bpf_prm *prm) +{ +printf ("cBPF program (%u insns)\n" , cbf->bf_len); +bpf_dump (cbf,1 ); + +printf ("\neBPF program (%u insns)\n" , prm->nb_ins); +rte_bpf_dump (stdout, prm->ins, prm->nb_ins); +} + +static int +test_bpf_match (pcap_t *pcap,const char *str,bool expected) +{ +struct bpf_program fcode; +struct rte_bpf_prm *prm =NULL ; +struct rte_bpf *bpf =NULL ; +uint8_t tbuf[sizeof (struct dummy_mbuf )]; +int ret =-1 ; +uint64_t rc; + +if (pcap_compile (pcap, &fcode, str,1 ,PCAP_NETMASK_UNKNOWN )) { +printf ("%s@%d: pcap_compile(\"%s\") failed: %s;\n" , + __func__, __LINE__, str,pcap_geterr (pcap)); +return -1 ; + } + + prm =rte_bpf_convert (&fcode); +if (prm ==NULL ) { +printf ("%s@%d: bpf_convert('%s') failed,, error=%d(%s);\n" , + __func__, __LINE__, str, rte_errno,strerror (rte_errno)); + goto error; + } + + bpf =rte_bpf_load (prm); +if (bpf ==NULL ) { +printf ("%s@%d: failed to load bpf code, error=%d(%s);\n" , + __func__, __LINE__, rte_errno,strerror (rte_errno)); + goto error; + } + +test_ld_mbuf1_prepare (tbuf); + rc =rte_bpf_exec (bpf, tbuf); +if ((rc ==0 ) == expected) + ret =0 ; +else +printf ("%s@%d: failed match: expect %s 0 got %" PRIu64"\n" , + __func__, __LINE__, expected ?"==" :"<>" , rc); +error: +if (bpf) +rte_bpf_destroy (bpf); +rte_free (prm); +pcap_freecode (&fcode); +return ret; +} + +/* Basic sanity test can we match a IP packet */ +static int +test_bpf_filter_sanity (pcap_t *pcap) +{ +int ret; + + ret =test_bpf_match (pcap,"ip" , true); + ret |=test_bpf_match (pcap,"not ip" , false); + +return ret; +} + +/* Some sample pcap filter strings from tcpdump man page */ +static const char *const sample_filters[] = { +"host 192.168.1.100" , +"src net 10" , +"not stp" , +"len = 128" , +"ip host 1.1.1.1 and not 1.1.1.2" , +"ip and not net 127.0.0.1" , +"tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net 127.0.0.1" , +"ether[0] & 1 = 0 and ip[16] >= 224" , +"icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply" , +}; + +static int +test_bpf_filter (pcap_t *pcap,const char *s) +{ +struct bpf_program fcode; +struct rte_bpf_prm *prm =NULL ; +struct rte_bpf *bpf =NULL ; + +if (pcap_compile (pcap, &fcode, s,1 ,PCAP_NETMASK_UNKNOWN )) { +printf ("%s@%d: pcap_compile('%s') failed: %s;\n" , + __func__, __LINE__, s,pcap_geterr (pcap)); +return -1 ; + } + + prm =rte_bpf_convert (&fcode); +if (prm ==NULL ) { +printf ("%s@%d: bpf_convert('%s') failed,, error=%d(%s);\n" , + __func__, __LINE__, s, rte_errno,strerror (rte_errno)); + goto error; + } + + bpf =rte_bpf_load (prm); +if (bpf ==NULL ) { +printf ("%s@%d: failed to load bpf code, error=%d(%s);\n" , + __func__, __LINE__, rte_errno,strerror (rte_errno)); + goto error; + } + +error: +if (bpf) +rte_bpf_destroy (bpf); +else { +printf ("%s \"%s\"\n" , __func__, s); +test_bpf_dump (&fcode, prm); + } + +rte_free (prm); +pcap_freecode (&fcode); +return (bpf ==NULL ) ?-1 :0 ; +} + +static int +test_bpf_convert (void ) +{ +unsigned int i; +pcap_t *pcap; +int rc; + + pcap =pcap_open_dead (DLT_EN10MB ,262144 ); +if (!pcap) { +printf ("pcap_open_dead failed\n" ); +return -1 ; + } + + rc =test_bpf_filter_sanity (pcap); +for (i =0 ; i <RTE_DIM (sample_filters); i++) + rc |=test_bpf_filter (pcap, sample_filters[i]); + +pcap_close (pcap); +return rc; +} + +REGISTER_TEST_COMMAND (bpf_convert_autotest, test_bpf_convert); +#endif /* RTE_PORT_PCAP */
Signature · 1 line
2.30.2
PATCH v7 9/11 test: add a test for pcapng library
In reply to Stephen HemmingerSimple unit test that created pcapng file using API.
To run this test you need to have at least one device. For example:
DPDK_TEST=pcapng_autotest ./build/app/test/dpdk-test -l 0-15 \
--no-huge -m 2048 --vdev=net_tap,iface=dummy--- app/test/meson.build | 1 + app/test/test_pcapng.c | 190 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 app/test/test_pcapng.c
app/test/meson.build +1 -0
@@ -97,6 +97,7 @@ test_sources = files( 'test_metrics.c', 'test_mcslock.c', 'test_mp_secondary.c', + 'test_pcapng.c', 'test_per_lcore.c', 'test_pflock.c', 'test_pmd_perf.c',
app/test/test_pcapng.c new file +190 -0
190 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v7 10/11 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 86 +++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 228 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -62,6 +62,16 @@ New Features * Added bus-level parsing of the devargs syntax. * Kept compatibility with the legacy syntax as parsing fallback. +* **Enhance Packet capture.** + + * New dpdk-dumpcap program that has most of the features of the + wireshark dumpcap utility including capture of multiple interfaces, + stopping after number of bytes, packets. + * New library for writing pcapng packet capture files. + * Enhancement to the pdump library to support: + * Packet filter with BPF. + * Pcapng format with timestamps and meta-data. + * Fixes packet capture with stripped VLAN tags. Removed Items -------------
doc/guides/tools/dumpcap.rst new file +86 -0
86 lines not on this page · raw message
doc/guides/tools/index.rst +1 -0
@@ -8,6 +8,7 @@ DPDK Tools User Guides :maxdepth: 2 :numbered: + dumpcap proc_info pdump pmdinfo
Signature · 1 line
2.30.2
PATCH v7 11/11 MAINTAINERS: add entry for new pcapng and dumper
In reply to Stephen HemmingerClaim responsibility for the new code.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -1429,6 +1429,12 @@ F: app/test/test_pdump.* F: app/pdump/ F: doc/guides/tools/pdump.rst +Packet dump +M: Stephen Hemminger <[email protected]> +F: lib/pcapng/ +F: doc/guides/prog_guide/pcapng_lib.rst +F: app/dumpcap/ +F: doc/guides/tools/dumpcap.rst Packet Framework ----------------
Signature · 1 line
2.30.2
v8 cover Packet capture framework enhancements
In reply to Stephen HemmingerThis patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
v8 changes:
- enable BPF tests in autotest
- add more BPF test strings
- use rte_strscpy to satisfy checkpatch
- merge MAINTAINERS (put this in with existing pdump)
v7 changes:
- add functional tests for pcapng lib
- bug fix for error returns in pcapng lib
- handle long osname on FreeBSD
Stephen Hemminger (12):
librte_pcapng: add new library for writing pcapng files
lib: pdump is not supported on Windows
bpf: allow self-xor operation
bpf: add function to convert classic BPF to DPDK BPF
bpf: add function to dump eBPF instructions
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
test: add test for bpf_convert
test: add a test for pcapng library
test: enable bpf autotest
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new packet capture features
MAINTAINERS | 11 +- app/dumpcap/main.c | 844 ++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + app/test/meson.build | 3 + app/test/test_bpf.c | 173 ++++ app/test/test_pcapng.c | 190 ++++ doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +- doc/guides/howto/packet_capture_framework.rst | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 575 ++++++++++++ lib/bpf/bpf_dump.c | 118 +++ lib/bpf/bpf_validate.c | 8 +- lib/bpf/meson.build | 6 + lib/bpf/rte_bpf.h | 39 + lib/bpf/version.map | 7 + lib/meson.build | 6 +- lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 441 ++++++--- lib/pdump/rte_pdump.h | 110 ++- lib/pdump/version.map | 8 + 33 files changed, 3569 insertions(+), 221 deletions(-) create mode 100644 app/dumpcap/main.c create mode 100644 app/dumpcap/meson.build create mode 100644 app/test/test_pcapng.c create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst create mode 100644 lib/bpf/bpf_convert.c create mode 100644 lib/bpf/bpf_dump.c create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
Signature · 1 line
2.30.2
PATCH v8 1/12 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 194 +++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 918 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +574 -0
574 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +194 -0
194 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH v8 2/12 lib: pdump is not supported on Windows
In reply to Stephen HemmingerThe current version of the pdump library was building on Windows, but it was useless since the pdump utility was not being built and Windows does not have multi-process support.
The new version of pdump with filtering now has dependency on bpf. But bpf library is not available on Windows.
--- lib/meson.build | 1 - 1 file changed, 1 deletion(-)
lib/meson.build +0 -1
@@ -85,7 +85,6 @@ if is_windows 'gro', 'gso', 'latencystats', - 'pdump', ] # only supported libraries for windows endif
Signature · 1 line
2.30.2
PATCH v8 3/12 bpf: allow self-xor operation
In reply to Stephen HemmingerWhen doing BPF filter program conversion, a common way to zero a register in single instruction is:
xor r7,r7
The BPF validator would not allow this because the value of r7 was undefined. But after this operation it always zero.
--- lib/bpf/bpf_validate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
lib/bpf/bpf_validate.c +6 -2
@@ -661,8 +661,12 @@ eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins) op =BPF_OP (ins->code); - err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , - (op !=BPF_NEG ) ? &rs :NULL ); +/* Allow self-xor as way to zero register */ +if (op ==BPF_XOR && ins->src_reg == ins->dst_reg) + err =NULL ; +else + err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , + (op !=BPF_NEG ) ? &rs :NULL );if (err !=NULL )return err;
Signature · 1 line
2.30.2