Packet capture framework enhancements
Subsystems (2)
Re: [dpdk-dev] [PATCH v12 00/12] Packet capture framework update
In reply to Pattan, ReshmaQuoted 10 lines · Reshma
Quoted 6 lines
-----Original Message-----
From: Thomas Monjalon <[email protected]>I was hoping to see a feedback from the current maintainer, but it seems you didn't Cc her... Reshma, are you aware of these patches?
I was aware of v10 where I had comments, I will take a look at V12.
Yes, please add me to CC for future patch sets, that would help me to not miss them.
This means we have a flawed process if patches can't get reviewed that have been submitted a month ahead of release.
Re: [dpdk-dev] [PATCH v12 00/12] Packet capture framework update
12/10/2021 17:44, Stephen Hemminger:
Quoted 4 lines
Quoted 2 lines · Reshma
From: Thomas Monjalon <[email protected]>
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I was hoping to see a feedback from the current maintainer, but it seems you didn't Cc her... Reshma, are you aware of these patches?
I was aware of v10 where I had comments, I will take a look at V12.
Yes, please add me to CC for future patch sets, that would help me to not miss them.This means we have a flawed process if patches can't get reviewed that have been submitted a month ahead of release.
Part of the process, you are supposed to use "--cc-cmd devtools/get-maintainer.sh" so maintainers are Cc'ed.
Re: [dpdk-dev] [PATCH v12 00/12] Packet capture framework update
Quoted 8 lines · On Tue, 12 Oct 2021 17:48:47 +0200 Thomas Monjalon <[email protected]> wrote:
12/10/2021 17:44, Stephen Hemminger:
Quoted 7 lines
Quoted 5 lines · Reshma
From: Thomas Monjalon <[email protected]>
Quoted 2 lines
I was hoping to see a feedback from the current maintainer, but it seems you didn't Cc her... Reshma, are you aware of these patches?
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I was aware of v10 where I had comments, I will take a look at V12.
Yes, please add me to CC for future patch sets, that would help me to not miss them.This means we have a flawed process if patches can't get reviewed that have been submitted a month ahead of release.
Part of the process, you are supposed to use "--cc-cmd
devtools/get-maintainer.sh" so maintainers are Cc'ed.
Thought they were, look like Reshma got missed.
No worries about doing in 22.02 since there is no API/ABI breakage in the patchset. No pre-release note needed either.
Re: [dpdk-dev] [PATCH v12 00/12] Packet capture framework update
12/10/2021 20:00, Stephen Hemminger:
Quoted 16 lines
Quoted 14 lines · On Tue, 12 Oct 2021 17:48:47 +0200 Thomas Monjalon <[email protected]> wrote:
12/10/2021 17:44, Stephen Hemminger:
Quoted 12 lines
Quoted 7 lines · Reshma
From: Thomas Monjalon <[email protected]>
Quoted 2 lines
I was hoping to see a feedback from the current maintainer, but it seems you didn't Cc her... Reshma, are you aware of these patches?
I was aware of v10 where I had comments, I will take a look at V12.
Yes, please add me to CC for future patch sets, that would help me to not miss them.This means we have a flawed process if patches can't get reviewed that have been submitted a month ahead of release.
Quoted 7 lines
Quoted 2 lines
Part of the process, you are supposed to use "--cc-cmd
devtools/get-maintainer.sh" so maintainers are Cc'ed.Thought they were, look like Reshma got missed.
No worries about doing in 22.02 since there is no API/ABI breakage in the patchset. No pre-release note needed either.
We can still merge it for 21.11 if Reshma is OK with the v12.
Re: [dpdk-dev] [PATCH v12 00/12] Packet capture framework update
Quoted 11 lines
-----Original Message-----
From: Thomas Monjalon <[email protected]>Quoted 4 lines
Thought they were, look like Reshma got missed.
No worries about doing in 22.02 since there is no API/ABI breakage in the patchset. No pre-release note needed either.
We can still merge it for 21.11 if Reshma is OK with the v12.
FYI, I have reviewed and acked the below pdump library patch. [v12,06/12] pdump: support pcapng and filtering
There are some other patches , mainly new library librte_pcapng which needs a review and an Ack, can someone join to review and provide an Ack? I will also take a look.
Thanks,
Reshma
v13 cover Packet capture framework update
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
There are no blocker items.
The following are worth noting:
* bogus checkpatch warnings
- the correct flag to open is O_CREAT
- intentionally keeping macro with goto since that was in original code and is clearer
- the tempfile name can not be const since it is overwritten by tmpfile() call
* enabling BPF tests causes CI to see a pre-existing bug
https://bugs.dpdk.org/show_bug.cgi?id=811
* future filtering for stripped VLAN tags needs collabration with libpcap project to fix pcap_compile_filter().
v13
- integrate feedback in documentation and pcapng library
v12
- fixes for capture offloaded VLAN tags. look at direction flag and handle QinQ offload.
v11
- address review comments for pdump (patch 6)
v10:
- fix to rte_bpf_dump to handle more instructions make sure all bpf_test cases are decoded
v9:
- incorporate suggested change to BPF XOR
- make autotest for pcapng more complete by reading the resulting file with libpcap
v8:
- 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:
- add functional tests for pcapng lib
- bug fix for error returns in pcapng lib
- handle long osname on FreeBSD
- resolve almost all checkpatch issues
v5:
- 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:
- minor checkpatch fixes.
Note: some of the checkpatch warnings are bogus and won't be fixed.
- fix build of dumpcap on FreeBSD
v3:
- 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
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 (12):
lib: pdump is not supported on Windows
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
test: add test for bpf_convert
test: add a test for pcapng library
test: enable bpf autotest
doc: changes for new pcapng and dumpcap utility
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 | 6 + app/test/test_bpf.c | 200 +++++ app/test/test_pcapng.c | 272 ++++++ 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 | 69 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 25 + 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 | 139 +++ lib/bpf/bpf_validate.c | 9 +- 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 | 607 +++++++++++++ lib/pcapng/rte_pcapng.h | 196 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 432 ++++++--- lib/pdump/rte_pdump.h | 113 ++- lib/pdump/version.map | 8 + 33 files changed, 3737 insertions(+), 219 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 v13 1/12 lib: pdump is not supported on Windows
The 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', 'stack', 'security', ] # only supported libraries for windows
Signature · 1 line
2.30.2
PATCH v13 2/12 librte_pcapng: add new library for writing pcapng files
In reply to Stephen HemmingerThis 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
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 | 607 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 196 ++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 953 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 +607 -0
607 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +196 -0
196 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 v13 3/12 bpf: allow self-xor operation
In reply to Stephen HemmingerSome BPF programs may use XOR of a register with itself as a way to zero register in one instruction. The BPF filter converter generates this in the prolog to the generated code.
The BPF validator would not allow this because the value of register was undefined. But after this operation it always zero.
--- lib/bpf/bpf_validate.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
lib/bpf/bpf_validate.c +8 -1
@@ -661,8 +661,15 @@ eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins) op =BPF_OP (ins->code); +/* Allow self-xor as way to zero register */ +if (op ==BPF_XOR &&BPF_SRC (ins->code) ==BPF_X && + ins->src_reg == ins->dst_reg) { +eval_fill_imm (&rs,UINT64_MAX ,0 ); +eval_fill_imm (rd,UINT64_MAX ,0 ); + } + err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , - (op !=BPF_NEG ) ? &rs :NULL ); + (op !=BPF_NEG ) ? &rs :NULL );if (err !=NULL )return err;
Signature · 1 line
2.30.2
PATCH v13 4/12 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. See https://github.com/tklauser/filter2xdp
Both authors have agreed that it is allowable to create a modified version of this code and license it with BSD license used by DPDK.
--- lib/bpf/bpf_convert.c | 575 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 611 insertions(+) create mode 100644 lib/bpf/bpf_convert.c
lib/bpf/bpf_convert.c new file +575 -0
@@ -0,0 +1,575 @@ +/* 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 : +/* Linux has special negative offsets to access meta-data. */ +RTE_BPF_LOG (ERR , +"rte_bpf_convert: socket offset %d not supported\n" , + fp->k -SKF_AD_OFF ); +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->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 +* 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 v13 5/12 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 | 139 ++++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 +++++ lib/bpf/version.map | 1 + 4 files changed, 155 insertions(+) create mode 100644 lib/bpf/bpf_dump.c
lib/bpf/bpf_dump.c new file +139 -0
@@ -0,0 +1,139 @@ +/* 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" , +}; + +void rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len) +{ +uint32_t i; + +for (i =0 ; i < len; ++i) { +const struct ebpf_insn *ins = buf + i; +uint8_t cls =BPF_CLASS (ins->code); +const char *op, *postfix ="" ; + +fprintf (f," L%u:\t" , i); + +switch (cls) { +default : +fprintf (f,"unimp 0x%x // class: %s\n" , + ins->code, class_tbl[cls]); +break ; +case BPF_ALU : + postfix ="32" ; +/* fall through */ +case EBPF_ALU64 : + op = alu_op_tbl[BPF_OP_INDEX (ins->code)]; +if (BPF_SRC (ins->code) ==BPF_X ) +fprintf (f,"%s%s r%u, r%u\n" , op, postfix, ins->dst_reg, + ins->src_reg); +else +fprintf (f,"%s%s r%u, #0x%x\n" , op, postfix, + ins->dst_reg, ins->imm); +break ; +case BPF_LD : + op ="ld" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +if (ins->code == (BPF_LD |BPF_IMM |EBPF_DW )) { +uint64_t val; + + val = (uint32_t )ins[0 ].imm | + (uint64_t )(uint32_t )ins[1 ].imm <<32 ; +fprintf (f,"%s%s r%d, #0x%" PRIx64"\n" , + op, postfix, ins->dst_reg, val); + i++; + }else if (BPF_MODE (ins->code) ==BPF_IMM ) +fprintf (f,"%s%s r%d, #0x%x\n" , op, postfix, + ins->dst_reg, ins->imm); +else if (BPF_MODE (ins->code) ==BPF_ABS ) +fprintf (f,"%s%s r%d, [%d]\n" , op, postfix, + ins->dst_reg, ins->imm); +else if (BPF_MODE (ins->code) ==BPF_IND ) +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, + ins->dst_reg, ins->src_reg, ins->imm); +else +fprintf (f,"// BUG: LD opcode 0x%02x in eBPF insns\n" , + ins->code); +break ; +case BPF_LDX : + op ="ldx" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, ins->dst_reg, + ins->src_reg, ins->off); +break ; +case BPF_ST : + op ="st" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +if (BPF_MODE (ins->code) ==BPF_MEM ) +fprintf (f,"%s%s [r%d + %d], #0x%x\n" , op, postfix, + ins->dst_reg, ins->off, ins->imm); +else +fprintf (f,"// BUG: ST opcode 0x%02x in eBPF insns\n" , + ins->code); +break ; +case BPF_STX : + op ="stx" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +fprintf (f,"%s%s [r%d + %d], r%u\n" , op, postfix, + ins->dst_reg, ins->off, ins->src_reg); +break ; +#define L (pc, off) ((int)(pc) + 1 + (off)) +case BPF_JMP : + op = jump_tbl[BPF_OP_INDEX (ins->code)]; +if (op ==NULL ) +fprintf (f,"invalid jump opcode: %#x\n" , ins->code); +else if (BPF_OP (ins->code) ==BPF_JA ) +fprintf (f,"%s L%d\n" , op,L (i, ins->off)); +else if (BPF_OP (ins->code) ==EBPF_EXIT ) +fprintf (f,"%s\n" , op); +else +fprintf (f,"%s r%u, #0x%x, L%d\n" , op, ins->dst_reg, + ins->imm,L (i, ins->off)); +break ; +case BPF_RET : +fprintf (f,"// BUG: RET opcode 0x%02x in eBPF insns\n" , + ins->code); +break ; + } + } +}
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 v13 6/12 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 | 432 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 113 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 433 insertions(+), 126 deletions(-)
lib/meson.build +2 -2
@@ -27,6 +27,7 @@ libraries = [ 'acl', 'bbdev', 'bitratestats', + 'bpf', 'cfgfile', 'compressdev', 'cryptodev',
@@ -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 '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 +312 -120
649 lines not on this page · raw message
lib/pdump/rte_pdump.h +110 -3
155 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 v13 7/12 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)); +rte_strscpy (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 v13 8/12 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 | 200 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+)
app/test/test_bpf.c +200 -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,202 @@ 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, +struct rte_mbuf *mb) +{ +struct bpf_program fcode; +struct rte_bpf_prm *prm =NULL ; +struct rte_bpf *bpf =NULL ; +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; + } + + rc =rte_bpf_exec (bpf, mb); +/* The return code from bpf capture filter is non-zero if matched */ + ret = (rc ==0 ); +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) +{ +const uint32_t plen =100 ; +struct rte_mbuf mb, *m; +uint8_t tbuf[RTE_MBUF_DEFAULT_BUF_SIZE ]; +struct { +struct rte_ether_hdr eth_hdr; +struct rte_ipv4_hdr ip_hdr; + } *hdr; + +dummy_mbuf_prep (&mb, tbuf,sizeof (tbuf), plen); + m = &mb; + + hdr =rte_pktmbuf_mtod (m,typeof (hdr)); + hdr->eth_hdr = (struct rte_ether_hdr ) { + .dst_addr.addr_bytes ="\xff\xff\xff\xff\xff\xff" , + .ether_type =rte_cpu_to_be_16 (RTE_ETHER_TYPE_IPV4 ), + }; + hdr->ip_hdr = (struct rte_ipv4_hdr ) { + .version_ihl =RTE_IPV4_VHL_DEF , + .total_length =rte_cpu_to_be_16 (plen), + .time_to_live =IPDEFTTL , + .next_proto_id =IPPROTO_RAW , + .src_addr =rte_cpu_to_be_32 (RTE_IPV4_LOOPBACK ), + .dst_addr =rte_cpu_to_be_32 (RTE_IPV4_BROADCAST ), + }; + +if (test_bpf_match (pcap,"ip" , m) !=0 ) { +printf ("%s@%d: filter \"ip\" doesn't match test data\n" , + __func__, __LINE__); +return -1 ; + } +if (test_bpf_match (pcap,"not ip" , m) ==0 ) { +printf ("%s@%d: filter \"not ip\" does match test data\n" , + __func__, __LINE__); +return -1 ; + } + +return 0 ; +} + +/* +* Some sample pcap filter strings from +* https://wiki.wireshark.org/CaptureFilters +*/ +static const char *const sample_filters[] = { +"host 172.18.5.4" , +"net 192.168.0.0/24" , +"src net 192.168.0.0/24" , +"src net 192.168.0.0 mask 255.255.255.0" , +"dst net 192.168.0.0/24" , +"dst net 192.168.0.0 mask 255.255.255.0" , +"port 53" , +"host dpdk.org and not (port 80 or port 25)" , +"host dpdk.org and not port 80 and not port 25" , +"port not 53 and not arp" , +"(tcp[0:2] > 1500 and tcp[0:2] < 1550) or (tcp[2:2] > 1500 and tcp[2:2] < 1550)" , +"ether proto 0x888e" , +"ether[0] & 1 = 0 and ip[16] >= 224" , +"icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply" , +"tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net 127.0.0.1" , +"not ether dst 01:80:c2:00:00:0e" , +"not broadcast and not multicast" , +"dst host ff02::1" , +"port 80 and tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420" , +/* Worms */ +"dst port 135 and tcp port 135 and ip[2:2]==48" , +"icmp[icmptype]==icmp-echo and ip[2:2]==92 and icmp[8:4]==0xAAAAAAAA" , +"dst port 135 or dst port 445 or dst port 1433" +" and tcp[tcpflags] & (tcp-syn) != 0" +" and tcp[tcpflags] & (tcp-ack) = 0 and src net 192.168.0.0/24" , +"tcp src port 443 and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4] = 0x18)" +" and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4 + 1] = 0x03)" +" and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4 + 2] < 0x04)" +" and ((ip[2:2] - 4 * (ip[0] & 0x0F) - 4 * ((tcp[12] & 0xF0) >> 4) > 69))" , +/* Other */ +"len = 128" , +}; + +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 v13 9/12 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 | 4 + app/test/test_pcapng.c | 272 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 276 insertions(+) create mode 100644 app/test/test_pcapng.c
app/test/meson.build +4 -0
@@ -396,6 +396,10 @@ if dpdk_conf.has('RTE_NET_RING') fast_tests += [['pdump_autotest', true]] endif +if dpdk_conf.has('RTE_PORT_PCAP') + test_sources += 'test_pcapng.c' +endif + if dpdk_conf.has('RTE_LIB_POWER') test_deps += 'power' endif
app/test/test_pcapng.c new file +272 -0
272 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v13 10/12 test: enable bpf autotest
In reply to Stephen HemmingerThe BPF autotest is defined but not run automatically. Since it is short, it should be added to the autotest suite.
--- app/test/meson.build | 2 ++ 1 file changed, 2 insertions(+)
app/test/meson.build +2 -0
@@ -194,6 +194,8 @@ test_deps = [ fast_tests = [ ['acl_autotest', true], ['atomic_autotest', false], + ['bpf_autotest', true], + ['bpf_convert_autotest', true], ['bitops_autotest', true], ['byteorder_autotest', true], ['cksum_autotest', true],
Signature · 1 line
2.30.2
PATCH v13 11/12 doc: changes for new pcapng and dumpcap utility
In reply to Stephen HemmingerDescribe the new packet capture library and utility.
--- 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 | 69 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 25 +++++ 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, 230 insertions(+), 88 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 +36 -33
126 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 +25 -0
@@ -0,0 +1,25 @@ +.. 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 +---------- +* Project repository https://github.com/pcapng/pcapng/ + +* PCAP Next Generation (pcapng) Capture File Format +https://pcapng.github.io/pcapng/draft-tuexen-opsawg-pcapng.html
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 setting 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
@@ -159,6 +159,16 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Revised packet capture framework.** + + * New dpdk-dumpcap program that has most of the features of the + wireshark dumpcap utility including: capture of multiple interfaces, + filtering, and stopping after number of bytes, packets. + * New library for writing pcapng packet capture files. + * Enhancements 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 v13 12/12 MAINTAINERS: add entry for new packet capture features
In reply to Stephen HemmingerSince the packet capture is just extension of existing pdump; add myself as maintainer of that.
--- MAINTAINERS | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
MAINTAINERS +8 -3
@@ -1425,12 +1425,17 @@ F: doc/guides/sample_app_ug/qos_scheduler.rst Packet capture M: Reshma Pattan <[email protected]> +M: Stephen Hemminger <[email protected]> F: lib/pdump/ +F: lib/pcapng/ F: doc/guides/prog_guide/pdump_lib.rst -F: app/test/test_pdump.* -F: app/pdump/ +F: doc/guides/prog_guide/pcapng_lib.rst +F: doc/guides/tools/dumpcap.rst F: doc/guides/tools/pdump.rst - +F: app/test/test_pdump.c +F: app/test/test_pcapng.c +F: app/pdump/ +F: app/dumpcap/ Packet Framework ----------------
Signature · 1 line
2.30.2
v14 cover Packet capture framework update
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
There are no blocker items.
The following are worth noting:
* bogus checkpatch warnings
- the correct flag to open is O_CREAT
- intentionally keeping macro with goto since that was in original code and is clearer
- the tempfile name can not be const since it is overwritten by tmpfile() call
* enabling BPF tests causes CI to see a pre-existing bug
https://bugs.dpdk.org/show_bug.cgi?id=811
* future filtering for stripped VLAN tags needs collabration with libpcap project to fix pcap_compile_filter().
v14
- fix checkpatch whitespace warning
- enhance the pcapng prog guide documentation
v13
- integrate feedback in documentation and pcapng library
- rebase to align with rte_ether_addr changes
v12
- fixes for capture offloaded VLAN tags. look at direction flag and handle QinQ offload.
v11
- address review comments for pdump (patch 6)
v10:
- fix to rte_bpf_dump to handle more instructions make sure all bpf_test cases are decoded
v9:
- incorporate suggested change to BPF XOR
- make autotest for pcapng more complete by reading the resulting file with libpcap
v8:
- 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:
- add functional tests for pcapng lib
- bug fix for error returns in pcapng lib
- handle long osname on FreeBSD
- resolve almost all checkpatch issues
v5:
- 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:
- minor checkpatch fixes.
Note: some of the checkpatch warnings are bogus and won't be fixed.
- fix build of dumpcap on FreeBSD
v3:
- 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
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 (12):
lib: pdump is not supported on Windows
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
test: add test for bpf_convert
test: add a test for pcapng library
test: enable bpf autotest
doc: changes for new pcapng and dumpcap utility
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 | 6 + app/test/test_bpf.c | 200 +++++ app/test/test_pcapng.c | 272 ++++++ 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 | 69 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 46 + 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 | 139 +++ lib/bpf/bpf_validate.c | 9 +- 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 | 607 +++++++++++++ lib/pcapng/rte_pcapng.h | 195 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 432 ++++++--- lib/pdump/rte_pdump.h | 113 ++- lib/pdump/version.map | 8 + 33 files changed, 3757 insertions(+), 219 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 v14 1/12 lib: pdump is not supported on Windows
The 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', 'stack', 'security', ] # only supported libraries for windows
Signature · 1 line
2.30.2
PATCH v14 2/12 librte_pcapng: add new library for writing pcapng files
In reply to Stephen HemmingerThis 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
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 | 607 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 195 ++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 952 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 +607 -0
607 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +195 -0
195 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
Re: [dpdk-dev] [PATCH v14 02/12] librte_pcapng: add new library for writing pcapng files
Quoted 7 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Subject: [PATCH v14 02/12]librte_pcapng: add new library for writing pcapng files
Quoted 7 lines
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
https://github.com/pcapng/pcapng/Signed-off-by Stephen Hemminger <[email protected]>
PATCH v14 3/12 bpf: allow self-xor operation
In reply to Stephen HemmingerSome BPF programs may use XOR of a register with itself as a way to zero register in one instruction. The BPF filter converter generates this in the prolog to the generated code.
The BPF validator would not allow this because the value of register was undefined. But after this operation it always zero.
--- lib/bpf/bpf_validate.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
lib/bpf/bpf_validate.c +8 -1
@@ -661,8 +661,15 @@ eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins) op =BPF_OP (ins->code); +/* Allow self-xor as way to zero register */ +if (op ==BPF_XOR &&BPF_SRC (ins->code) ==BPF_X && + ins->src_reg == ins->dst_reg) { +eval_fill_imm (&rs,UINT64_MAX ,0 ); +eval_fill_imm (rd,UINT64_MAX ,0 ); + } + err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , - (op !=BPF_NEG ) ? &rs :NULL ); + (op !=BPF_NEG ) ? &rs :NULL );if (err !=NULL )return err;
Signature · 1 line
2.30.2
PATCH v14 4/12 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. See https://github.com/tklauser/filter2xdp
Both authors have agreed that it is allowable to create a modified version of this code and license it with BSD license used by DPDK.
--- lib/bpf/bpf_convert.c | 575 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 611 insertions(+) create mode 100644 lib/bpf/bpf_convert.c
lib/bpf/bpf_convert.c new file +575 -0
@@ -0,0 +1,575 @@ +/* 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 : +/* Linux has special negative offsets to access meta-data. */ +RTE_BPF_LOG (ERR , +"rte_bpf_convert: socket offset %d not supported\n" , + fp->k -SKF_AD_OFF ); +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->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 +* 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 v14 5/12 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 | 139 ++++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 +++++ lib/bpf/version.map | 1 + 4 files changed, 155 insertions(+) create mode 100644 lib/bpf/bpf_dump.c
lib/bpf/bpf_dump.c new file +139 -0
@@ -0,0 +1,139 @@ +/* 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" , +}; + +void rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len) +{ +uint32_t i; + +for (i =0 ; i < len; ++i) { +const struct ebpf_insn *ins = buf + i; +uint8_t cls =BPF_CLASS (ins->code); +const char *op, *postfix ="" ; + +fprintf (f," L%u:\t" , i); + +switch (cls) { +default : +fprintf (f,"unimp 0x%x // class: %s\n" , + ins->code, class_tbl[cls]); +break ; +case BPF_ALU : + postfix ="32" ; +/* fall through */ +case EBPF_ALU64 : + op = alu_op_tbl[BPF_OP_INDEX (ins->code)]; +if (BPF_SRC (ins->code) ==BPF_X ) +fprintf (f,"%s%s r%u, r%u\n" , op, postfix, ins->dst_reg, + ins->src_reg); +else +fprintf (f,"%s%s r%u, #0x%x\n" , op, postfix, + ins->dst_reg, ins->imm); +break ; +case BPF_LD : + op ="ld" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +if (ins->code == (BPF_LD |BPF_IMM |EBPF_DW )) { +uint64_t val; + + val = (uint32_t )ins[0 ].imm | + (uint64_t )(uint32_t )ins[1 ].imm <<32 ; +fprintf (f,"%s%s r%d, #0x%" PRIx64"\n" , + op, postfix, ins->dst_reg, val); + i++; + }else if (BPF_MODE (ins->code) ==BPF_IMM ) +fprintf (f,"%s%s r%d, #0x%x\n" , op, postfix, + ins->dst_reg, ins->imm); +else if (BPF_MODE (ins->code) ==BPF_ABS ) +fprintf (f,"%s%s r%d, [%d]\n" , op, postfix, + ins->dst_reg, ins->imm); +else if (BPF_MODE (ins->code) ==BPF_IND ) +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, + ins->dst_reg, ins->src_reg, ins->imm); +else +fprintf (f,"// BUG: LD opcode 0x%02x in eBPF insns\n" , + ins->code); +break ; +case BPF_LDX : + op ="ldx" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, ins->dst_reg, + ins->src_reg, ins->off); +break ; +case BPF_ST : + op ="st" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +if (BPF_MODE (ins->code) ==BPF_MEM ) +fprintf (f,"%s%s [r%d + %d], #0x%x\n" , op, postfix, + ins->dst_reg, ins->off, ins->imm); +else +fprintf (f,"// BUG: ST opcode 0x%02x in eBPF insns\n" , + ins->code); +break ; +case BPF_STX : + op ="stx" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +fprintf (f,"%s%s [r%d + %d], r%u\n" , op, postfix, + ins->dst_reg, ins->off, ins->src_reg); +break ; +#define L (pc, off) ((int)(pc) + 1 + (off)) +case BPF_JMP : + op = jump_tbl[BPF_OP_INDEX (ins->code)]; +if (op ==NULL ) +fprintf (f,"invalid jump opcode: %#x\n" , ins->code); +else if (BPF_OP (ins->code) ==BPF_JA ) +fprintf (f,"%s L%d\n" , op,L (i, ins->off)); +else if (BPF_OP (ins->code) ==EBPF_EXIT ) +fprintf (f,"%s\n" , op); +else +fprintf (f,"%s r%u, #0x%x, L%d\n" , op, ins->dst_reg, + ins->imm,L (i, ins->off)); +break ; +case BPF_RET : +fprintf (f,"// BUG: RET opcode 0x%02x in eBPF insns\n" , + ins->code); +break ; + } + } +}
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 v14 6/12 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 | 432 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 113 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 433 insertions(+), 126 deletions(-)
lib/meson.build +2 -2
@@ -27,6 +27,7 @@ libraries = [ 'acl', 'bbdev', 'bitratestats', + 'bpf', 'cfgfile', 'compressdev', 'cryptodev',
@@ -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 '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 +312 -120
649 lines not on this page · raw message
lib/pdump/rte_pdump.h +110 -3
155 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 v14 7/12 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)); +rte_strscpy (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 v14 8/12 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 | 200 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+)
app/test/test_bpf.c +200 -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,202 @@ 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, +struct rte_mbuf *mb) +{ +struct bpf_program fcode; +struct rte_bpf_prm *prm =NULL ; +struct rte_bpf *bpf =NULL ; +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; + } + + rc =rte_bpf_exec (bpf, mb); +/* The return code from bpf capture filter is non-zero if matched */ + ret = (rc ==0 ); +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) +{ +const uint32_t plen =100 ; +struct rte_mbuf mb, *m; +uint8_t tbuf[RTE_MBUF_DEFAULT_BUF_SIZE ]; +struct { +struct rte_ether_hdr eth_hdr; +struct rte_ipv4_hdr ip_hdr; + } *hdr; + +dummy_mbuf_prep (&mb, tbuf,sizeof (tbuf), plen); + m = &mb; + + hdr =rte_pktmbuf_mtod (m,typeof (hdr)); + hdr->eth_hdr = (struct rte_ether_hdr ) { + .dst_addr.addr_bytes ="\xff\xff\xff\xff\xff\xff" , + .ether_type =rte_cpu_to_be_16 (RTE_ETHER_TYPE_IPV4 ), + }; + hdr->ip_hdr = (struct rte_ipv4_hdr ) { + .version_ihl =RTE_IPV4_VHL_DEF , + .total_length =rte_cpu_to_be_16 (plen), + .time_to_live =IPDEFTTL , + .next_proto_id =IPPROTO_RAW , + .src_addr =rte_cpu_to_be_32 (RTE_IPV4_LOOPBACK ), + .dst_addr =rte_cpu_to_be_32 (RTE_IPV4_BROADCAST ), + }; + +if (test_bpf_match (pcap,"ip" , m) !=0 ) { +printf ("%s@%d: filter \"ip\" doesn't match test data\n" , + __func__, __LINE__); +return -1 ; + } +if (test_bpf_match (pcap,"not ip" , m) ==0 ) { +printf ("%s@%d: filter \"not ip\" does match test data\n" , + __func__, __LINE__); +return -1 ; + } + +return 0 ; +} + +/* +* Some sample pcap filter strings from +* https://wiki.wireshark.org/CaptureFilters +*/ +static const char *const sample_filters[] = { +"host 172.18.5.4" , +"net 192.168.0.0/24" , +"src net 192.168.0.0/24" , +"src net 192.168.0.0 mask 255.255.255.0" , +"dst net 192.168.0.0/24" , +"dst net 192.168.0.0 mask 255.255.255.0" , +"port 53" , +"host dpdk.org and not (port 80 or port 25)" , +"host dpdk.org and not port 80 and not port 25" , +"port not 53 and not arp" , +"(tcp[0:2] > 1500 and tcp[0:2] < 1550) or (tcp[2:2] > 1500 and tcp[2:2] < 1550)" , +"ether proto 0x888e" , +"ether[0] & 1 = 0 and ip[16] >= 224" , +"icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply" , +"tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net 127.0.0.1" , +"not ether dst 01:80:c2:00:00:0e" , +"not broadcast and not multicast" , +"dst host ff02::1" , +"port 80 and tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420" , +/* Worms */ +"dst port 135 and tcp port 135 and ip[2:2]==48" , +"icmp[icmptype]==icmp-echo and ip[2:2]==92 and icmp[8:4]==0xAAAAAAAA" , +"dst port 135 or dst port 445 or dst port 1433" +" and tcp[tcpflags] & (tcp-syn) != 0" +" and tcp[tcpflags] & (tcp-ack) = 0 and src net 192.168.0.0/24" , +"tcp src port 443 and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4] = 0x18)" +" and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4 + 1] = 0x03)" +" and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4 + 2] < 0x04)" +" and ((ip[2:2] - 4 * (ip[0] & 0x0F) - 4 * ((tcp[12] & 0xF0) >> 4) > 69))" , +/* Other */ +"len = 128" , +}; + +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 v14 9/12 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 | 4 + app/test/test_pcapng.c | 272 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 276 insertions(+) create mode 100644 app/test/test_pcapng.c
app/test/meson.build +4 -0
@@ -396,6 +396,10 @@ if dpdk_conf.has('RTE_NET_RING') fast_tests += [['pdump_autotest', true]] endif +if dpdk_conf.has('RTE_PORT_PCAP') + test_sources += 'test_pcapng.c' +endif + if dpdk_conf.has('RTE_LIB_POWER') test_deps += 'power' endif
app/test/test_pcapng.c new file +272 -0
272 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v14 10/12 test: enable bpf autotest
In reply to Stephen HemmingerThe BPF autotest is defined but not run automatically. Since it is short, it should be added to the autotest suite.
--- app/test/meson.build | 2 ++ 1 file changed, 2 insertions(+)
app/test/meson.build +2 -0
@@ -194,6 +194,8 @@ test_deps = [ fast_tests = [ ['acl_autotest', true], ['atomic_autotest', false], + ['bpf_autotest', true], + ['bpf_convert_autotest', true], ['bitops_autotest', true], ['byteorder_autotest', true], ['cksum_autotest', true],
Signature · 1 line
2.30.2
PATCH v14 11/12 doc: changes for new pcapng and dumpcap utility
In reply to Stephen HemmingerDescribe the new packet capture library and utility. Fix the title line on the pdump documentation.
--- 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 | 69 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 46 +++++++++ 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, 251 insertions(+), 88 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 +36 -33
126 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 +46 -0
46 lines not on this page · raw message
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +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 setting 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
@@ -159,6 +159,16 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Revised packet capture framework.** + + * New dpdk-dumpcap program that has most of the features of the + wireshark dumpcap utility including: capture of multiple interfaces, + filtering, and stopping after number of bytes, packets. + * New library for writing pcapng packet capture files. + * Enhancements 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
Re: [dpdk-dev] [PATCH v14 11/12] doc: changes for new pcapng and dumpcap utility
Quoted 10 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Subject: [PATCH v14 11/12] doc: changes for new pcapng and dumpcap utilityDescribe the new packet capture library and utility. Fix the title line on the pdump documentation.
Signed-off-by Stephen Hemminger <[email protected]>
PATCH v14 12/12 MAINTAINERS: add entry for new packet capture features
In reply to Stephen HemmingerSince the packet capture is just extension of existing pdump; add myself as maintainer of that.
--- MAINTAINERS | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
MAINTAINERS +8 -3
@@ -1425,12 +1425,17 @@ F: doc/guides/sample_app_ug/qos_scheduler.rst Packet capture M: Reshma Pattan <[email protected]> +M: Stephen Hemminger <[email protected]> F: lib/pdump/ +F: lib/pcapng/ F: doc/guides/prog_guide/pdump_lib.rst -F: app/test/test_pdump.* -F: app/pdump/ +F: doc/guides/prog_guide/pcapng_lib.rst +F: doc/guides/tools/dumpcap.rst F: doc/guides/tools/pdump.rst - +F: app/test/test_pdump.c +F: app/test/test_pcapng.c +F: app/pdump/ +F: app/dumpcap/ Packet Framework ----------------
Signature · 1 line
2.30.2
Re: [dpdk-dev] [PATCH v14 12/12] MAINTAINERS: add entry for new packet capture features
Quoted 6 lines
-----Original Message-----
From: dev <[email protected]> On Behalf Of Stephen Hemminger
Sent: Friday, October 15, 2021 9:11 PMSigned-off-by Stephen Hemminger <[email protected]>
v15 cover Packet capture framework update
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
There are no blocker items.
The following are worth noting:
* bogus checkpatch warnings
- the correct flag to open is O_CREAT
- intentionally keeping macro with goto since that was in original code and is clearer
- the tempfile name can not be const since it is overwritten by tmpfile() call
v15
- fix minor spelling in doc
make sure CI tests are rerun after test/bpf fix
v14
- fix checkpatch whitespace warning
- enhance the pcapng prog guide documentation
v13
- integrate feedback in documentation and pcapng library
- rebase to align with rte_ether_addr changes
v12
- fixes for capture offloaded VLAN tags. look at direction flag and handle QinQ offload.
v11
- address review comments for pdump (patch 6)
v10:
- fix to rte_bpf_dump to handle more instructions make sure all bpf_test cases are decoded
v9:
- incorporate suggested change to BPF XOR
- make autotest for pcapng more complete by reading the resulting file with libpcap
v8:
- 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:
- add functional tests for pcapng lib
- bug fix for error returns in pcapng lib
- handle long osname on FreeBSD
- resolve almost all checkpatch issues
v5:
- 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:
- minor checkpatch fixes.
Note: some of the checkpatch warnings are bogus and won't be fixed.
- fix build of dumpcap on FreeBSD
v3:
- 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
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 (12):
lib: pdump is not supported on Windows
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
test: add test for bpf_convert
test: add a test for pcapng library
test: enable bpf autotest
doc: changes for new pcapng and dumpcap utility
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 | 6 + app/test/test_bpf.c | 200 +++++ app/test/test_pcapng.c | 272 ++++++ 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 | 69 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 46 + 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 | 139 +++ lib/bpf/bpf_validate.c | 9 +- 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 | 607 +++++++++++++ lib/pcapng/rte_pcapng.h | 195 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 432 ++++++--- lib/pdump/rte_pdump.h | 113 ++- lib/pdump/version.map | 8 + 33 files changed, 3757 insertions(+), 219 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 v15 1/12 lib: pdump is not supported on Windows
The 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
@@ -86,7 +86,6 @@ if is_windows 'gro', 'gso', 'latencystats', - 'pdump', 'stack', 'security', ] # only supported libraries for windows
Signature · 1 line
2.30.2
PATCH v15 2/12 librte_pcapng: add new library for writing pcapng files
In reply to Stephen HemmingerThis 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
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 | 607 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 195 ++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 952 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 +607 -0
607 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +195 -0
195 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
Re: [dpdk-dev] [PATCH v15 02/12] librte_pcapng: add new library for writing pcapng files
Quoted 17 lines · Stephen Hemminger
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
https://github.com/pcapng/pcapng/Signed-off-by Stephen Hemminger <[email protected]>Acked-by Reshma Pattan <[email protected]>--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 ++++++++ lib/pcapng/rte_pcapng.c | 607 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 195 ++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 952 insertions(+)
Quoted 6 lines
create mode 100644
lib/pcapng/meson.build
create mode 100644lib/pcapng/pcapng_proto.h
create mode 100644lib/pcapng/rte_pcapng.c
create mode 100644lib/pcapng/rte_pcapng.h
create mode 100644lib/pcapng/version.map
Minor niggle, does this need a MAINTAINERS entry?
Re: [dpdk-dev] [PATCH v15 02/12] librte_pcapng: add new library for writing pcapng files
Quoted 25 lines · Ray
Quoted 17 lines · Stephen Hemminger
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
https://github.com/pcapng/pcapng/Signed-off-by Stephen Hemminger <[email protected]>Acked-by Reshma Pattan <[email protected]>--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 ++++++++ lib/pcapng/rte_pcapng.c | 607 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 195 ++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 952 insertions(+)Quoted 6 lines
create mode 100644
lib/pcapng/meson.build
create mode 100644lib/pcapng/pcapng_proto.h
create mode 100644lib/pcapng/rte_pcapng.c
create mode 100644lib/pcapng/rte_pcapng.h
create mode 100644lib/pcapng/version.map
Quoted 3 lines
Minor niggle, does this need a
MAINTAINERSentry?Acked-by Ray Kinsella <[email protected]>
It doesn't need its own entry in MAINTAINERS.
If you look at the last patch, added that directory under the Packet Capture section.
Re: [dpdk-dev] [PATCH v15 02/12] librte_pcapng: add new library for writing pcapng files
21/10/2021 17:29, Stephen Hemminger:
Quoted 14 lines
Quoted 11 lines · Ray
Quoted 10 lines · Stephen Hemminger
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
https://github.com/pcapng/pcapng/Signed-off-by Stephen Hemminger <[email protected]>Acked-by Reshma Pattan <[email protected]>---
Quoted 6 lines
Quoted 3 lines
Minor niggle, does this need a
MAINTAINERSentry?Acked-by Ray Kinsella <[email protected]>It doesn't need its own entry in
MAINTAINERS.
If you look at the last patch, added that directory under the Packet Capture section.
It should not be the last patch.
I will try to squash where appropriate while merging.
Re: [dpdk-dev] [PATCH v15 02/12] librte_pcapng: add new library for writing pcapng files
In reply to Stephen Hemminger20/10/2021 23:42, Stephen Hemminger:
Quoted 2 lines
+++ b/lib/pcapng/meson.build
+version = 1
What is the meaning of this version in meson? Is it used somewhere?
Re: [dpdk-dev] [PATCH v15 02/12] librte_pcapng: add new library for writing pcapng files
Quoted 8 lines · On Fri, 22 Oct 2021 15:43:58 +0200 Thomas Monjalon <[email protected]> wrote:
20/10/2021 23:42, Stephen Hemminger:
Quoted 2 lines
+++ b/lib/pcapng/meson.build
+version = 1What is the meaning of this version in meson? Is it used somewhere?
No just copy/paste from some other meson file.
Re: [dpdk-dev] [PATCH v15 02/12] librte_pcapng: add new library for writing pcapng files
22/10/2021 17:07, Stephen Hemminger:
Quoted 10 lines
Quoted 5 lines · On Fri, 22 Oct 2021 15:43:58 +0200 Thomas Monjalon <[email protected]> wrote:
20/10/2021 23:42, Stephen Hemminger:
Quoted 2 lines
+++ b/lib/pcapng/meson.build
+version = 1What is the meaning of this version in meson? Is it used somewhere?
No just copy/paste from some other meson file.
OK, removed then.
For info, I've found multiple garbages in your patches, like repeated words, misplaced line in doc, etc.
Re: [dpdk-dev] [PATCH v15 02/12] librte_pcapng: add new library for writing pcapng files
In reply to Stephen Hemmingerlib/pcapng/rte_pcapng.c line 181
Quoted 3 lines · Stephen Hemminger
lib/pcapng/rte_pcapng.c new file +3
-0+/* DPDK reports in units of Mbps */ +rte_eth_link_get (port, &link); +if (link.link_status ==ETH_LINK_UP )
Should use renamed 'RTE_ETH_LINK_UP' macro with RTE_ prefix.
Re: [dpdk-dev] [PATCH v15 02/12] librte_pcapng: add new library for writing pcapng files
Quoted 6 lines · On Fri, 29 Oct 2021 18:50:57 +0100 Ferruh Yigit <[email protected]> wrote:
lib/pcapng/rte_pcapng.c line 181
Quoted 3 lines · Stephen Hemminger
lib/pcapng/rte_pcapng.c new file +3
-0+/* DPDK reports in units of Mbps */ +rte_eth_link_get (port, &link); +if (link.link_status ==ETH_LINK_UP )Should use renamed '
RTE_ETH_LINK_UP' macro withRTE_prefix.
Sure, that wasn't there when this was first written. Should we deprecate old one?
PATCH 1/1 pcapng: do not use deprecated ETH_LINK_UP
In reply to Ferruh YigitRTE_ prefix was added by:
commit 295968d17407 ("ethdev: add namespace")
--- lib/pcapng/rte_pcapng.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/pcapng/rte_pcapng.c +1 -1
@@ -178,7 +178,7 @@ pcapng_add_interface(rte_pcapng_t *self, uint16_t port)/* DPDK reports in units of Mbps */ rte_eth_link_get (port, &link); -if (link.link_status ==ETH_LINK_UP ) +if (link.link_status ==RTE_ETH_LINK_UP ) speed = link.link_speed *PCAPNG_MBPS_SPEED ;if (rte_eth_macaddr_get (port, &macaddr) <0 )
Signature · 1 line
2.30.2
Thomas Monjalon
AppliedRe: [dpdk-dev] [PATCH] pcapng: do not use deprecated ETH_LINK_UP
29/10/2021 23:50, Stephen Hemminger:
Quoted 5 lines
RTE_prefix was added by:
commit295968d17407("ethdev: add namespace")Fixes 8d23ce8f5ee9 ("pcapng: add new library for writing pcapng files")Signed-off-by Stephen Hemminger <[email protected]>
Applied, thanks.
Note: I missed it when merging patches.
PATCH v15 3/12 bpf: allow self-xor operation
In reply to Stephen HemmingerSome BPF programs may use XOR of a register with itself as a way to zero register in one instruction. The BPF filter converter generates this in the prolog to the generated code.
The BPF validator would not allow this because the value of register was undefined. But after this operation it always zero.
--- lib/bpf/bpf_validate.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
lib/bpf/bpf_validate.c +8 -1
@@ -661,8 +661,15 @@ eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins) op =BPF_OP (ins->code); +/* Allow self-xor as way to zero register */ +if (op ==BPF_XOR &&BPF_SRC (ins->code) ==BPF_X && + ins->src_reg == ins->dst_reg) { +eval_fill_imm (&rs,UINT64_MAX ,0 ); +eval_fill_imm (rd,UINT64_MAX ,0 ); + } + err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , - (op !=BPF_NEG ) ? &rs :NULL ); + (op !=BPF_NEG ) ? &rs :NULL );if (err !=NULL )return err;
Signature · 1 line
2.30.2
PATCH v15 4/12 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. See https://github.com/tklauser/filter2xdp
Both authors have agreed that it is allowable to create a modified version of this code and license it with BSD license used by DPDK.
--- lib/bpf/bpf_convert.c | 575 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 611 insertions(+) create mode 100644 lib/bpf/bpf_convert.c
lib/bpf/bpf_convert.c new file +575 -0
@@ -0,0 +1,575 @@ +/* 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 : +/* Linux has special negative offsets to access meta-data. */ +RTE_BPF_LOG (ERR , +"rte_bpf_convert: socket offset %d not supported\n" , + fp->k -SKF_AD_OFF ); +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->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 +* 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 v15 04/12] bpf: add function to convert classic BPF to DPDK BPF
Quoted 18 lines · Stephen Hemminger
The 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_convertfunction usesrte_mallocto 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. See https://github.com/tklauser/filter2xdp
Both authors have agreed that it is allowable to create a modified version of this code and license it with BSD license used by DPDK.
Signed-off-by Stephen Hemminger <[email protected]>
Quoted 8 lines
--- lib/bpf/bpf_convert.c | 575 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 611 insertions(+)create mode 100644
lib/bpf/bpf_convert.c
PATCH v15 5/12 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 | 139 ++++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 +++++ lib/bpf/version.map | 1 + 4 files changed, 155 insertions(+) create mode 100644 lib/bpf/bpf_dump.c
lib/bpf/bpf_dump.c new file +139 -0
@@ -0,0 +1,139 @@ +/* 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" , +}; + +void rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len) +{ +uint32_t i; + +for (i =0 ; i < len; ++i) { +const struct ebpf_insn *ins = buf + i; +uint8_t cls =BPF_CLASS (ins->code); +const char *op, *postfix ="" ; + +fprintf (f," L%u:\t" , i); + +switch (cls) { +default : +fprintf (f,"unimp 0x%x // class: %s\n" , + ins->code, class_tbl[cls]); +break ; +case BPF_ALU : + postfix ="32" ; +/* fall through */ +case EBPF_ALU64 : + op = alu_op_tbl[BPF_OP_INDEX (ins->code)]; +if (BPF_SRC (ins->code) ==BPF_X ) +fprintf (f,"%s%s r%u, r%u\n" , op, postfix, ins->dst_reg, + ins->src_reg); +else +fprintf (f,"%s%s r%u, #0x%x\n" , op, postfix, + ins->dst_reg, ins->imm); +break ; +case BPF_LD : + op ="ld" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +if (ins->code == (BPF_LD |BPF_IMM |EBPF_DW )) { +uint64_t val; + + val = (uint32_t )ins[0 ].imm | + (uint64_t )(uint32_t )ins[1 ].imm <<32 ; +fprintf (f,"%s%s r%d, #0x%" PRIx64"\n" , + op, postfix, ins->dst_reg, val); + i++; + }else if (BPF_MODE (ins->code) ==BPF_IMM ) +fprintf (f,"%s%s r%d, #0x%x\n" , op, postfix, + ins->dst_reg, ins->imm); +else if (BPF_MODE (ins->code) ==BPF_ABS ) +fprintf (f,"%s%s r%d, [%d]\n" , op, postfix, + ins->dst_reg, ins->imm); +else if (BPF_MODE (ins->code) ==BPF_IND ) +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, + ins->dst_reg, ins->src_reg, ins->imm); +else +fprintf (f,"// BUG: LD opcode 0x%02x in eBPF insns\n" , + ins->code); +break ; +case BPF_LDX : + op ="ldx" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, ins->dst_reg, + ins->src_reg, ins->off); +break ; +case BPF_ST : + op ="st" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +if (BPF_MODE (ins->code) ==BPF_MEM ) +fprintf (f,"%s%s [r%d + %d], #0x%x\n" , op, postfix, + ins->dst_reg, ins->off, ins->imm); +else +fprintf (f,"// BUG: ST opcode 0x%02x in eBPF insns\n" , + ins->code); +break ; +case BPF_STX : + op ="stx" ; + postfix = size_tbl[BPF_SIZE_INDEX (ins->code)]; +fprintf (f,"%s%s [r%d + %d], r%u\n" , op, postfix, + ins->dst_reg, ins->off, ins->src_reg); +break ; +#define L (pc, off) ((int)(pc) + 1 + (off)) +case BPF_JMP : + op = jump_tbl[BPF_OP_INDEX (ins->code)]; +if (op ==NULL ) +fprintf (f,"invalid jump opcode: %#x\n" , ins->code); +else if (BPF_OP (ins->code) ==BPF_JA ) +fprintf (f,"%s L%d\n" , op,L (i, ins->off)); +else if (BPF_OP (ins->code) ==EBPF_EXIT ) +fprintf (f,"%s\n" , op); +else +fprintf (f,"%s r%u, #0x%x, L%d\n" , op, ins->dst_reg, + ins->imm,L (i, ins->off)); +break ; +case BPF_RET : +fprintf (f,"// BUG: RET opcode 0x%02x in eBPF insns\n" , + ins->code); +break ; + } + } +}
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
Re: [dpdk-dev] [PATCH v15 05/12] bpf: add function to dump eBPF instructions
Quoted 5 lines · Stephen Hemminger
When debugging converted (and other) programs it is useful to see disassembled eBPF output.
Signed-off-by Stephen Hemminger <[email protected]>
Quoted 8 lines
--- lib/bpf/bpf_dump.c | 139 ++++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 +++++ lib/bpf/version.map | 1 + 4 files changed, 155 insertions(+)create mode 100644
lib/bpf/bpf_dump.c
PATCH v15 6/12 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 | 432 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 113 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 433 insertions(+), 126 deletions(-)
lib/meson.build +2 -2
@@ -27,6 +27,7 @@ libraries = [ 'acl', 'bbdev', 'bitratestats', + 'bpf', 'cfgfile', 'compressdev', 'cryptodev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'dmadev',
@@ -56,10 +56,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 '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 +312 -120
649 lines not on this page · raw message
lib/pdump/rte_pdump.h +110 -3
155 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
Re: [dpdk-dev] [PATCH v15 06/12] pdump: support pcapng and filtering
Quoted 17 lines · Stephen Hemminger
This 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).Signed-off-by Stephen Hemminger <[email protected]>Acked-by Reshma Pattan <[email protected]>
Quoted 8 lines
--- lib/meson.build | 4 +- lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 432 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 113 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 433 insertions(+), 126 deletions(-)
Re: [dpdk-dev] [PATCH v15 06/12] pdump: support pcapng and filtering
In reply to Stephen HemmingerHi Hemminger,
I meet an issue when using dpdk-pdump with your patch ,we try to capture pkts from virtio port, all packets captured shows malformed packets , and no issue if remove your patch. Bug link:https://bugs.dpdk.org/show_bug.cgi?id=840
Could you help to take a look at this issue?
BR,
Yinan
Quoted 26 lines
-----Original Message-----
From: dev <[email protected]> On Behalf Of Stephen Hemminger
Sent: 2021?10?21? 5:43
To: [email protected]
Cc: Stephen Hemminger <[email protected]>; Pattan, Reshma <[email protected]>; Ray Kinsella <[email protected]>; Burakov, Anatoly <[email protected]>
Subject: [dpdk-dev] [PATCH v15 06/12] pdump: support pcapng and filteringThis 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).Signed-off-by Stephen Hemminger <[email protected]>Acked-by Reshma Pattan <[email protected]>
Re: [dpdk-dev] [PATCH v15 06/12] pdump: support pcapng and filtering
Quoted 7 lines · Yinan
Hi Hemminger,
I meet an issue when using dpdk-pdump with your patch ,we try to capture pkts from virtio port, all packets captured shows malformed packets , and no issue if remove your patch. Bug link:https://bugs.dpdk.org/show_bug.cgi?id=840
Could you help to take a look at this issue?BR,
Yinan
Thanks looking into it today.
PATCH v15 7/12 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)); +rte_strscpy (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 v15 8/12 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 | 200 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+)
app/test/test_bpf.c +200 -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>
@@ -3248,3 +3249,202 @@ 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, +struct rte_mbuf *mb) +{ +struct bpf_program fcode; +struct rte_bpf_prm *prm =NULL ; +struct rte_bpf *bpf =NULL ; +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; + } + + rc =rte_bpf_exec (bpf, mb); +/* The return code from bpf capture filter is non-zero if matched */ + ret = (rc ==0 ); +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) +{ +const uint32_t plen =100 ; +struct rte_mbuf mb, *m; +uint8_t tbuf[RTE_MBUF_DEFAULT_BUF_SIZE ]; +struct { +struct rte_ether_hdr eth_hdr; +struct rte_ipv4_hdr ip_hdr; + } *hdr; + +dummy_mbuf_prep (&mb, tbuf,sizeof (tbuf), plen); + m = &mb; + + hdr =rte_pktmbuf_mtod (m,typeof (hdr)); + hdr->eth_hdr = (struct rte_ether_hdr ) { + .dst_addr.addr_bytes ="\xff\xff\xff\xff\xff\xff" , + .ether_type =rte_cpu_to_be_16 (RTE_ETHER_TYPE_IPV4 ), + }; + hdr->ip_hdr = (struct rte_ipv4_hdr ) { + .version_ihl =RTE_IPV4_VHL_DEF , + .total_length =rte_cpu_to_be_16 (plen), + .time_to_live =IPDEFTTL , + .next_proto_id =IPPROTO_RAW , + .src_addr =rte_cpu_to_be_32 (RTE_IPV4_LOOPBACK ), + .dst_addr =rte_cpu_to_be_32 (RTE_IPV4_BROADCAST ), + }; + +if (test_bpf_match (pcap,"ip" , m) !=0 ) { +printf ("%s@%d: filter \"ip\" doesn't match test data\n" , + __func__, __LINE__); +return -1 ; + } +if (test_bpf_match (pcap,"not ip" , m) ==0 ) { +printf ("%s@%d: filter \"not ip\" does match test data\n" , + __func__, __LINE__); +return -1 ; + } + +return 0 ; +} + +/* +* Some sample pcap filter strings from +* https://wiki.wireshark.org/CaptureFilters +*/ +static const char *const sample_filters[] = { +"host 172.18.5.4" , +"net 192.168.0.0/24" , +"src net 192.168.0.0/24" , +"src net 192.168.0.0 mask 255.255.255.0" , +"dst net 192.168.0.0/24" , +"dst net 192.168.0.0 mask 255.255.255.0" , +"port 53" , +"host dpdk.org and not (port 80 or port 25)" , +"host dpdk.org and not port 80 and not port 25" , +"port not 53 and not arp" , +"(tcp[0:2] > 1500 and tcp[0:2] < 1550) or (tcp[2:2] > 1500 and tcp[2:2] < 1550)" , +"ether proto 0x888e" , +"ether[0] & 1 = 0 and ip[16] >= 224" , +"icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply" , +"tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net 127.0.0.1" , +"not ether dst 01:80:c2:00:00:0e" , +"not broadcast and not multicast" , +"dst host ff02::1" , +"port 80 and tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420" , +/* Worms */ +"dst port 135 and tcp port 135 and ip[2:2]==48" , +"icmp[icmptype]==icmp-echo and ip[2:2]==92 and icmp[8:4]==0xAAAAAAAA" , +"dst port 135 or dst port 445 or dst port 1433" +" and tcp[tcpflags] & (tcp-syn) != 0" +" and tcp[tcpflags] & (tcp-ack) = 0 and src net 192.168.0.0/24" , +"tcp src port 443 and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4] = 0x18)" +" and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4 + 1] = 0x03)" +" and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4 + 2] < 0x04)" +" and ((ip[2:2] - 4 * (ip[0] & 0x0F) - 4 * ((tcp[12] & 0xF0) >> 4) > 69))" , +/* Other */ +"len = 128" , +}; + +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 v15 9/12 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 | 4 + app/test/test_pcapng.c | 272 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 276 insertions(+) create mode 100644 app/test/test_pcapng.c
app/test/meson.build +4 -0
@@ -400,6 +400,10 @@ if dpdk_conf.has('RTE_NET_RING') fast_tests += [['pdump_autotest', true]] endif +if dpdk_conf.has('RTE_PORT_PCAP') + test_sources += 'test_pcapng.c' +endif + if dpdk_conf.has('RTE_LIB_POWER') test_deps += 'power' endif
app/test/test_pcapng.c new file +272 -0
272 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v15 10/12 test: enable bpf autotest
In reply to Stephen HemmingerThe BPF autotest is defined but not run automatically. Since it is short, it should be added to the autotest suite.
--- app/test/meson.build | 2 ++ 1 file changed, 2 insertions(+)
app/test/meson.build +2 -0
@@ -197,6 +197,8 @@ test_deps = [ fast_tests = [ ['acl_autotest', true], ['atomic_autotest', false], + ['bpf_autotest', true], + ['bpf_convert_autotest', true], ['bitops_autotest', true], ['byteorder_autotest', true], ['cksum_autotest', true],
Signature · 1 line
2.30.2
PATCH v15 11/12 doc: changes for new pcapng and dumpcap utility
In reply to Stephen HemmingerDescribe the new packet capture library and utility. Fix the title line on the pdump documentation.
--- 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 | 69 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 46 +++++++++ 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, 251 insertions(+), 88 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
@@ -224,3 +224,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
@@ -59,6 +59,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 +36 -33
126 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -44,6 +44,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 +46 -0
46 lines not on this page · raw message
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +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 setting 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
@@ -189,6 +189,16 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Revised packet capture framework.** + + * New dpdk-dumpcap program that has most of the features of the + wireshark dumpcap utility including: capture of multiple interfaces, + filtering, and stopping after number of bytes, packets. + * New library for writing pcapng packet capture files. + * Enhancements 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 v15 12/12 MAINTAINERS: add entry for new packet capture features
In reply to Stephen HemmingerSince the packet capture is just extension of existing pdump; add myself as maintainer of that.
--- MAINTAINERS | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
MAINTAINERS +8 -3
@@ -1430,12 +1430,17 @@ F: doc/guides/sample_app_ug/qos_scheduler.rst Packet capture M: Reshma Pattan <[email protected]> +M: Stephen Hemminger <[email protected]> F: lib/pdump/ +F: lib/pcapng/ F: doc/guides/prog_guide/pdump_lib.rst -F: app/test/test_pdump.* -F: app/pdump/ +F: doc/guides/prog_guide/pcapng_lib.rst +F: doc/guides/tools/dumpcap.rst F: doc/guides/tools/pdump.rst - +F: app/test/test_pdump.c +F: app/test/test_pcapng.c +F: app/pdump/ +F: app/dumpcap/ Packet Framework ----------------
Signature · 1 line
2.30.2
Re: [dpdk-dev] [PATCH v15 12/12] MAINTAINERS: add entry for new packet capture features
Quoted 5 lines · On Wed, 20 Oct 2021 14:42:36 -0700 Stephen Hemminger…
Since the packet capture is just extension of existing pdump; add myself as maintainer of that.
Signed-off-by Stephen Hemminger <[email protected]>---
FYI the CI infrastructure is reporting false positives for spelling errors on this.
"de facto" is correct spelling according to standard dictionaries. It sees "fdopen" in the doc around pcapng and thinks the API call is a word that has to be in dictionary.
Re: [dpdk-dev] [PATCH v15 00/12] Packet capture framework update
In reply to Stephen Hemminger20/10/2021 23:42, Stephen Hemminger:
Quoted 8 lines
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
I have re-organized the commits and fixed various stuff in the doc. Doc and maintainers update are part of relevant commits. Applied, thanks.