Packet capture framework enhancements
Subsystems (2)
Re: [dpdk-dev] [PATCH v8 03/12] bpf: allow self-xor operation
In reply to Stephen HemmingerQuoted 19 lines
When doing BPF filter program conversion, a common way to zero a register in single instruction is:
xor r7,r7The BPF validator would not allow this because the value of r7 was undefined. But after this operation it always zero.
Signed-off-by Stephen Hemminger <[email protected]>--- lib/bpf/bpf_validate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)lib/bpf/bpf_validate.c +0
-0@@ -661,8 +661,12 @@ eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins)
lib/bpf/bpf_validate.c eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins) · line 669
Quoted 10 lines
lib/bpf/bpf_validate.c +6
-2op =BPF_OP (ins->code); - err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , - (op !=BPF_NEG ) ? &rs :NULL ); +/* Allow self-xor as way to zero register */ +if (op ==BPF_XOR && ins->src_reg == ins->dst_reg) + err =NULL ; +else + err =eval_defined ((op !=EBPF_MOV ) ? rd :NULL , + (op !=BPF_NEG ) ? &rs :NULL );
Two things:
- We probably need to check that this is instruction with source register (not imm value).
- rd value is not evaluated to zero, while it probably should
(will help evaluator to better predict further values)
So might be better to do something like:
/* 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);
if (err != NULL)
return err;...
Another thing - shouldn't that patch be treated like a fix (cc to stable, etc.)?
Quoted 5 lines
lib/bpf/bpf_validate.c +0
-1if (err !=NULL )return err; --2.30.2
PATCH v8 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. Both authors have agreed that it is allowable to license this as BSD licensed in DPDK.
--- lib/bpf/bpf_convert.c | 570 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 606 insertions(+) create mode 100644 lib/bpf/bpf_convert.c
lib/bpf/bpf_convert.c new file +570 -0
@@ -0,0 +1,570 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2021 Microsoft Corporation +* +* Based on bpf_convert_filter() in the Linux kernel sources +* and filter2xdp. +* +* Licensed as BSD with permission original authors. +* Copyright (C) 2017 Tobias Klauser +* Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com +*/ + +#include <assert.h> +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <rte_common.h> +#include <rte_bpf.h> +#include <rte_log.h> +#include <rte_malloc.h> +#include <rte_errno.h> + +/* Workaround name conflicts with libpcap */ +#define bpf_validate (f, len) bpf_validate_libpcap(f, len) +#include <pcap/pcap.h> +#include <pcap/bpf.h> +#undef bpf_validate + +#include "bpf_impl.h" +#include "bpf_def.h" + +#ifndef BPF_MAXINSNS +#define BPF_MAXINSNS 4096 +#endif + +/* +* Linux socket filter uses negative absolute offsets to +* reference ancillary data. +*/ +#define SKF_AD_OFF (-0x1000) +#define SKF_AD_PROTOCOL 0 +#define SKF_AD_PKTTYPE 4 +#define SKF_AD_IFINDEX 8 +#define SKF_AD_NLATTR 12 +#define SKF_AD_NLATTR_NEST 16 +#define SKF_AD_MARK 20 +#define SKF_AD_QUEUE 24 +#define SKF_AD_HATYPE 28 +#define SKF_AD_RXHASH 32 +#define SKF_AD_CPU 36 +#define SKF_AD_ALU_XOR_X 40 +#define SKF_AD_VLAN_TAG 44 +#define SKF_AD_VLAN_TAG_PRESENT 48 +#define SKF_AD_PAY_OFFSET 52 +#define SKF_AD_RANDOM 56 +#define SKF_AD_VLAN_TPID 60 +#define SKF_AD_MAX 64 + +/* ArgX, context and stack frame pointer register positions. Note, +* Arg1, Arg2, Arg3, etc are used as argument mappings of function +* calls in BPF_CALL instruction. +*/ +#define BPF_REG_ARG1 EBPF_REG_1 +#define BPF_REG_ARG2 EBPF_REG_2 +#define BPF_REG_ARG3 EBPF_REG_3 +#define BPF_REG_ARG4 EBPF_REG_4 +#define BPF_REG_ARG5 EBPF_REG_5 +#define BPF_REG_CTX EBPF_REG_6 +#define BPF_REG_FP EBPF_REG_10 + +/* Additional register mappings for converted user programs. */ +#define BPF_REG_A EBPF_REG_0 +#define BPF_REG_X EBPF_REG_7 +#define BPF_REG_TMP EBPF_REG_8 + +/* Helper macros for filter block array initializers. */ + +/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ + +#define EBPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_ALU32_REG (OP ,DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ + +#define BPF_ALU32_IMM (OP ,DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov, dst_reg = src_reg */ + +#define BPF_MOV64_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = EBPF_ALU64 | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_MOV32_REG (DST ,SRC ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* Short form of mov, dst_reg = imm32 */ + +#define BPF_MOV32_IMM (DST ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */ + +#define BPF_MOV32_RAW (TYPE ,DST ,SRC ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_ALU | EBPF_MOV | BPF_SRC(TYPE), \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = IMM }) + +/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ + +#define BPF_LD_ABS (SIZE ,IMM ) \ + ((struct ebpf_insn) { \ + .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Memory load, dst_reg = *(uint *) (src_reg + off16) */ + +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Memory store, *(uint *) (dst_reg + off16) = src_reg */ + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ + +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +/* Raw code statement block */ + +#define BPF_RAW_INSN (CODE ,DST ,SRC ,OFF ,IMM ) \ + ((struct ebpf_insn) { \ + .code = CODE, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = IMM }) + +/* Program exit */ + +#define BPF_EXIT_INSN () \ + ((struct ebpf_insn) { \ + .code = BPF_JMP | EBPF_EXIT, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = 0 }) + +/* +* Placeholder to convert BPF extensions like length and VLAN tag +* If and when DPDK BPF supports them. +*/ +static bool convert_bpf_load (const struct bpf_insn *fp, + struct ebpf_insn **new_insnp __rte_unused) +{ +switch (fp->k) { +case SKF_AD_OFF +SKF_AD_PROTOCOL : +case SKF_AD_OFF +SKF_AD_PKTTYPE : +case SKF_AD_OFF +SKF_AD_IFINDEX : +case SKF_AD_OFF +SKF_AD_HATYPE : +case SKF_AD_OFF +SKF_AD_MARK : +case SKF_AD_OFF +SKF_AD_RXHASH : +case SKF_AD_OFF +SKF_AD_QUEUE : +case SKF_AD_OFF +SKF_AD_VLAN_TAG : +case SKF_AD_OFF +SKF_AD_VLAN_TAG_PRESENT : +case SKF_AD_OFF +SKF_AD_VLAN_TPID : +case SKF_AD_OFF +SKF_AD_PAY_OFFSET : +case SKF_AD_OFF +SKF_AD_NLATTR : +case SKF_AD_OFF +SKF_AD_NLATTR_NEST : +case SKF_AD_OFF +SKF_AD_CPU : +case SKF_AD_OFF +SKF_AD_RANDOM : +case SKF_AD_OFF +SKF_AD_ALU_XOR_X : +/* 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 =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); +break ; + +/* Jump transformation cannot use BPF block macros +* everywhere as offset calculation and target updates +* require a bit more work than the rest, i.e. jump +* opcodes map as-is, but offsets need adjustment. +*/ + +#define BPF_EMIT_JMP \ + do { \ + if (target >= len) \ + goto err; \ + insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ +/* Adjust pc relative offset for 2nd or 3rd insn. */ \ + insn->off -= insn - tmp_insns; \ + }while (0 ) + +case BPF_JMP |BPF_JA : + target = i + fp->k +1 ; + insn->code = fp->code; +BPF_EMIT_JMP ; +break ; + +case BPF_JMP |BPF_JEQ |BPF_K : +case BPF_JMP |BPF_JEQ |BPF_X : +case BPF_JMP |BPF_JSET |BPF_K : +case BPF_JMP |BPF_JSET |BPF_X : +case BPF_JMP |BPF_JGT |BPF_K : +case BPF_JMP |BPF_JGT |BPF_X : +case BPF_JMP |BPF_JGE |BPF_K : +case BPF_JMP |BPF_JGE |BPF_X : +if (BPF_SRC (fp->code) ==BPF_K && (int ) fp->k <0 ) { +/* BPF immediates are signed, zero extend +* immediate into tmp register and use it +* in compare insn. +*/ + *insn++ =BPF_MOV32_IMM (BPF_REG_TMP , fp->k); + + insn->dst_reg =BPF_REG_A ; + insn->src_reg =BPF_REG_TMP ; + bpf_src =BPF_X ; + }else { + insn->dst_reg =BPF_REG_A ; + insn->imm = fp->k; + bpf_src =BPF_SRC (fp->code); + insn->src_reg = bpf_src ==BPF_X ?BPF_REG_X :0 ; + } + +/* Common case where 'jump_false' is next insn. */ +if (fp->jf ==0 ) { + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; + target = i + fp->jt +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Convert JEQ into JNE when 'jump_true' is next insn. */ +if (fp->jt ==0 &&BPF_OP (fp->code) ==BPF_JEQ ) { + insn->code =BPF_JMP |EBPF_JNE | bpf_src; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + } + +/* Other jumps are mapped into two insns: Jxx and JA. */ + target = i + fp->jt +1 ; + insn->code =BPF_JMP |BPF_OP (fp->code) | bpf_src; +BPF_EMIT_JMP ; + insn++; + + insn->code =BPF_JMP |BPF_JA ; + target = i + fp->jf +1 ; +BPF_EMIT_JMP ; +break ; + +/* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ +case BPF_LDX |BPF_MSH |BPF_B : +/* tmp = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_TMP ,BPF_REG_A ); +/* A = BPF_R0 = *(u8 *) (skb->data + K) */ + *insn++ =BPF_LD_ABS (BPF_B , fp->k); +/* A &= 0xf */ + *insn++ =BPF_ALU32_IMM (BPF_AND ,BPF_REG_A ,0xf ); +/* A <<= 2 */ + *insn++ =BPF_ALU32_IMM (BPF_LSH ,BPF_REG_A ,2 ); +/* X = A */ + *insn++ =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +/* A = tmp */ + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_TMP ); +break ; + +/* RET_K is remaped into 2 insns. RET_A case doesn't need an +* extra mov as EBPF_REG_0 is already mapped into BPF_REG_A. +*/ +case BPF_RET |BPF_A : +case BPF_RET |BPF_K : +if (BPF_RVAL (fp->code) ==BPF_K ) { + *insn++ =BPF_MOV32_RAW (BPF_K ,EBPF_REG_0 , +0 , fp->k); + } + *insn =BPF_EXIT_INSN (); +break ; + +/* Store to stack. */ +case BPF_ST : +case BPF_STX : + *insn =BPF_STX_MEM (BPF_W ,BPF_REG_FP ,BPF_CLASS (fp->code) == +BPF_ST ?BPF_REG_A :BPF_REG_X , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* Load from stack. */ +case BPF_LD |BPF_MEM : +case BPF_LDX |BPF_MEM : + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_FP , + -(BPF_MEMWORDS - fp->k) *4 ); +break ; + +/* A = K or X = K */ +case BPF_LD |BPF_IMM : +case BPF_LDX |BPF_IMM : + *insn =BPF_MOV32_IMM (BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X , fp->k); +break ; + +/* X = A */ +case BPF_MISC |BPF_TAX : + *insn =BPF_MOV64_REG (BPF_REG_X ,BPF_REG_A ); +break ; + +/* A = X */ +case BPF_MISC |BPF_TXA : + *insn =BPF_MOV64_REG (BPF_REG_A ,BPF_REG_X ); +break ; + +/* A = mbuf->len or X = mbuf->len */ +case BPF_LD |BPF_W |BPF_LEN : +case BPF_LDX |BPF_W |BPF_LEN : +/* BPF_ABS/BPF_IND implicitly expect mbuf ptr in R6 */ + + *insn =BPF_LDX_MEM (BPF_W ,BPF_CLASS (fp->code) ==BPF_LD ? +BPF_REG_A :BPF_REG_X ,BPF_REG_CTX , + offsetof(struct rte_mbuf , pkt_len)); +break ; + +/* Unknown instruction. */ +default : +RTE_BPF_LOG (ERR ,"%s: Unknown instruction!: %#x\n" , + __func__, fp->code); + goto err; + } + + insn++; +if (new_prog) +memcpy (new_insn, tmp_insns, +sizeof (*insn) * (insn - tmp_insns)); + new_insn += insn - tmp_insns; + } + +if (!new_prog) { +/* Only calculating new length. */ + *new_len = new_insn - new_prog; +return 0 ; + } + + pass++; +if ((ptrdiff_t )new_flen != new_insn - new_prog) { + new_flen = new_insn - new_prog; +if (pass >2 ) + goto err; + goto do_pass; + } + +free (addrs); +assert (*new_len == new_flen); + +return 0 ; +err: +free (addrs); +return -1 ; +} + +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog) +{ +struct rte_bpf_prm *prm =NULL ; +struct ebpf_insn *ebpf =NULL ; +uint32_t ebpf_len =0 ; +int ret; + +if (prog ==NULL ) { +RTE_BPF_LOG (ERR ,"%s: NULL program\n" , __func__); + rte_errno =EINVAL ; +return NULL ; + } + +/* 1st pass: calculate the eBPF program length */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len,NULL , &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot get eBPF length\n" , __func__); + rte_errno = -ret; +return NULL ; + } + +RTE_BPF_LOG (DEBUG ,"%s: prog len cBPF=%u -> eBPF=%u\n" , + __func__, prog->bf_len, ebpf_len); + + prm =rte_zmalloc ("bpf_filter" , +sizeof (*prm) + ebpf_len *sizeof (*ebpf),0 ); +if (prm ==NULL ) { + rte_errno =ENOMEM ; +return NULL ; + } + +/* The EPBF instructions in this case are right after the header */ + ebpf = (void *)(prm +1 ); + +/* 2nd pass: remap cBPF to eBPF instructions */ + ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len); +if (ret <0 ) { +RTE_BPF_LOG (ERR ,"%s: cannot convert cBPF to eBPF\n" , __func__); +free (prm); + rte_errno = -ret; +return NULL ; + } + + prm->ins = ebpf; + prm->nb_ins = ebpf_len; + +/* Classic BPF programs use mbufs */ + prm->prog_arg.type =RTE_BPF_ARG_PTR_MBUF ; + prm->prog_arg.size =sizeof (struct rte_mbuf ); + +return prm; +}
lib/bpf/meson.build +5 -0
@@ -25,3 +25,8 @@ if dep.found() sources += files('bpf_load_elf.c') ext_deps += dep endif + +if dpdk_conf.has('RTE_PORT_PCAP') + sources += files('bpf_convert.c') + ext_deps += pcap_dep +endif
lib/bpf/rte_bpf.h +25 -0
@@ -198,6 +198,31 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +#ifdef RTE_PORT_PCAP + +struct bpf_program ; + +/** +* Convert a Classic BPF program from libpcap into a DPDK BPF code. +* +* @param prog +* Classic BPF program from pcap_compile(). +* @param prm +* Result Extended BPF program. +* @return +* Pointer to BPF program (allocated with *rte_malloc*) +* that is used in future BPF operations, +* or NULL on error, with error code set in rte_errno. +* Possible rte_errno errors include: +* - EINVAL - invalid parameter passed to function +* - ENOMEM - can't reserve enough memory +*/ +__rte_experimental +struct rte_bpf_prm * +rte_bpf_convert (const struct bpf_program *prog); + +#endif + #ifdef __cplusplus }#endif
lib/bpf/version.map +6 -0
@@ -14,3 +14,9 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_bpf_convert; +};
Signature · 1 line
2.30.2
Re: [dpdk-dev] [PATCH v8 04/12] bpf: add function to convert classic BPF to DPDK BPF
Quoted 31 lines
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. Both authors have agreed that it is allowable to license this as BSD licensed in DPDK.
Signed-off-by Stephen Hemminger <[email protected]>--- lib/bpf/bpf_convert.c | 570 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 5 + lib/bpf/rte_bpf.h | 25 ++ lib/bpf/version.map | 6 + 4 files changed, 606 insertions(+) create mode 100644 lib/bpf/bpf_convert.clib/bpf/meson.build +0
-0
lib/bpf/meson.build if dep.found() · line 29
Quoted 5 lines
lib/bpf/meson.build +2
-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')
Do we really need that 'if' above?
Why not to always have it enabled?
Quoted 44 lines
lib/bpf/meson.build +3
-0+ sources += files('bpf_convert.c') + ext_deps += pcap_dep +endiflib/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 +0
-0
lib/bpf/version.map DPDK_22 {
Quoted 10 lines
lib/bpf/version.map +6
-1@@ -14,3 +14,9 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_bpf_convert; +}; --
Cool feature, thanks for contributing.
Quoted 1 line
2.30.2
Re: [dpdk-dev] [PATCH v8 04/12] bpf: add function to convert classic BPF to DPDK BPF
Quoted 4 lines · Konstantin
Quoted 1 line
+if
dpdk_conf.has('RTE_PORT_PCAP')Do we really need that 'if' above?
Why not to always have it enabled?
The converter code needs libpcap for the pcap header files that define the encoding of classic BPF (struct bpf_insn).
PATCH v8 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_convert.c | 7 ++- lib/bpf/bpf_dump.c | 118 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 +++++ lib/bpf/version.map | 1 + 5 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 lib/bpf/bpf_dump.c
lib/bpf/bpf_convert.c +6 -1
@@ -331,7 +331,12 @@ static int bpf_convert_filter(const struct bpf_insn *prog, size_t len,case BPF_LD |BPF_IND |BPF_H :case BPF_LD |BPF_IND |BPF_B :/* All arithmetic insns map as-is. */ - *insn =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); + insn->code = fp->code; + insn->dst_reg =BPF_REG_A ; + bpf_src =BPF_SRC (fp->code); + insn->src_reg = bpf_src ==BPF_X ?BPF_REG_X :0 ; + insn->off =0 ; + insn->imm = fp->k;break ;/* Jump transformation cannot use BPF block macros
lib/bpf/bpf_dump.c new file +118 -0
118 lines not on this page · raw message
lib/bpf/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2018 Intel Corporation sources = files('bpf.c', + 'bpf_dump.c', 'bpf_exec.c', 'bpf_load.c', 'bpf_pkt.c',
lib/bpf/rte_bpf.h +14 -0
@@ -198,6 +198,20 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +/** +* Dump epf instructions to a file. +* +* @param f +* A pointer to a file for output +* @param buf +* A pointer to BPF instructions +* @param len +* Number of BPF instructions to dump. +*/ +__rte_experimental +void +rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len); +#ifdef RTE_PORT_PCAP struct bpf_program ;
lib/bpf/version.map +1 -0
@@ -19,4 +19,5 @@ EXPERIMENTAL { global: rte_bpf_convert; + rte_bpf_dump; };
Signature · 1 line
2.30.2
Re: [dpdk-dev] [PATCH v8 05/12] bpf: add function to dump eBPF instructions
Quoted 21 lines
When debugging converted (and other) programs it is useful to see disassembled eBPF output.
Signed-off-by Stephen Hemminger <[email protected]>--- lib/bpf/bpf_convert.c | 7 ++- lib/bpf/bpf_dump.c | 118 ++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 +++++ lib/bpf/version.map | 1 + 5 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 lib/bpf/bpf_dump.clib/bpf/bpf_convert.c +0
-0@@ -331,7 +331,12 @@ static int bpf_convert_filter(const struct bpf_insn *prog, size_t len,case BPF_LD |BPF_IND |BPF_H :
lib/bpf/bpf_convert.c static int bpf_convert_filter(const struct bpf_insn *prog, size_t len, · line 340
Quoted 10 lines
lib/bpf/bpf_convert.c +6
-1case BPF_LD |BPF_IND |BPF_B :/* All arithmetic insns map as-is. */ - *insn =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); + insn->code = fp->code; + insn->dst_reg =BPF_REG_A ; + bpf_src =BPF_SRC (fp->code); + insn->src_reg = bpf_src ==BPF_X ?BPF_REG_X :0 ; + insn->off =0 ; + insn->imm = fp->k;break ;
Should it be part of that patch?
Looks like belongs to previous one, no?
Quoted 168 lines
lib/bpf/bpf_convert.c +0
-0/* Jump transformation cannot use BPF block macros lib/bpf/bpf_dump.c new file +118
-0@@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright (c) 2021 Stephen Hemminger +* Based on filter2xdp +* Copyright (C) 2017 Tobias Klauser +*/ + +#include <stdio.h> +#include <stdint.h> + +#include "rte_bpf.h" + +#define BPF_OP_INDEX (x) (BPF_OP(x) >> 4) +#define BPF_SIZE_INDEX (x) (BPF_SIZE(x) >> 3) + +static const char *const class_tbl[] = { + [BPF_LD ] ="ld" , [BPF_LDX ] ="ldx" , [BPF_ST ] ="st" , + [BPF_STX ] ="stx" , [BPF_ALU ] ="alu" , [BPF_JMP ] ="jmp" , + [BPF_RET ] ="ret" , [BPF_MISC ] ="alu64" , +}; + +static const char *const alu_op_tbl[16 ] = { + [BPF_ADD >>4 ] ="add" , [BPF_SUB >>4 ] ="sub" , + [BPF_MUL >>4 ] ="mul" , [BPF_DIV >>4 ] ="div" , + [BPF_OR >>4 ] ="or" , [BPF_AND >>4 ] ="and" , + [BPF_LSH >>4 ] ="lsh" , [BPF_RSH >>4 ] ="rsh" , + [BPF_NEG >>4 ] ="neg" , [BPF_MOD >>4 ] ="mod" , + [BPF_XOR >>4 ] ="xor" , [EBPF_MOV >>4 ] ="mov" , + [EBPF_ARSH >>4 ] ="arsh" , [EBPF_END >>4 ] ="endian" , +}; + +static const char *const size_tbl[] = { + [BPF_W >>3 ] ="w" , + [BPF_H >>3 ] ="h" , + [BPF_B >>3 ] ="b" , + [EBPF_DW >>3 ] ="dw" , +}; + +static const char *const jump_tbl[16 ] = { + [BPF_JA >>4 ] ="ja" , [BPF_JEQ >>4 ] ="jeq" , + [BPF_JGT >>4 ] ="jgt" , [BPF_JGE >>4 ] ="jge" , + [BPF_JSET >>4 ] ="jset" , [EBPF_JNE >>4 ] ="jne" , + [EBPF_JSGT >>4 ] ="jsgt" , [EBPF_JSGE >>4 ] ="jsge" , + [EBPF_CALL >>4 ] ="call" , [EBPF_EXIT >>4 ] ="exit" , +}; + +static void ebpf_dump (FILE *f,const struct ebpf_insn insn,size_t n) +{ +const char *op, *postfix ="" ; +uint8_t cls =BPF_CLASS (insn.code); + +fprintf (f," L%zu:\t" , n); + +switch (cls) { +default : +fprintf (f,"unimp 0x%x // class: %s\n" , insn.code, + class_tbl[cls]); +break ; +case BPF_ALU : + postfix ="32" ; +/* fall through */ +case EBPF_ALU64 : + op = alu_op_tbl[BPF_OP_INDEX (insn.code)]; +if (BPF_SRC (insn.code) ==BPF_X ) +fprintf (f,"%s%s r%u, r%u\n" , op, postfix, insn.dst_reg, + insn.src_reg); +else +fprintf (f,"%s%s r%u, #0x%x\n" , op, postfix, + insn.dst_reg, insn.imm); +break ; +case BPF_LD : + op ="ld" ; + postfix = size_tbl[BPF_SIZE_INDEX (insn.code)]; +if (BPF_MODE (insn.code) ==BPF_IMM ) +fprintf (f,"%s%s r%d, #0x%x\n" , op, postfix, + insn.dst_reg, insn.imm); +else if (BPF_MODE (insn.code) ==BPF_ABS ) +fprintf (f,"%s%s r%d, [%d]\n" , op, postfix, + insn.dst_reg, insn.imm); +else if (BPF_MODE (insn.code) ==BPF_IND ) +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, + insn.dst_reg, insn.src_reg, insn.imm); +else +fprintf (f,"// BUG: LD opcode 0x%02x in eBPF insns\n" , + insn.code); +break ; +case BPF_LDX : + op ="ldx" ; + postfix = size_tbl[BPF_SIZE_INDEX (insn.code)]; +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, insn.dst_reg, + insn.src_reg, insn.off); +break ; +#define L (pc, off) ((int)(pc) + 1 + (off)) +case BPF_JMP : + op = jump_tbl[BPF_OP_INDEX (insn.code)]; +if (op ==NULL ) +fprintf (f,"invalid jump opcode: %#x\n" , insn.code); +else if (BPF_OP (insn.code) ==BPF_JA ) +fprintf (f,"%s L%d\n" , op,L (n, insn.off)); +else if (BPF_OP (insn.code) ==EBPF_EXIT ) +fprintf (f,"%s\n" , op); +else +fprintf (f,"%s r%u, #0x%x, L%d\n" , op, insn.dst_reg, + insn.imm,L (n, insn.off)); +break ; +case BPF_RET : +fprintf (f,"// BUG: RET opcode 0x%02x in eBPF insns\n" , + insn.code); +break ; + } +} + +void rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len) +{ +uint32_t i; + +for (i =0 ; i < len; ++i) +ebpf_dump (f, buf[i], i); +}lib/bpf/meson.build +1
-0@@ -2,6 +2,7 @@ # Copyright(c) 2018 Intel Corporation sources = files('bpf.c', + 'bpf_dump.c', 'bpf_exec.c', 'bpf_load.c', 'bpf_pkt.c',lib/bpf/rte_bpf.h +14
-0@@ -198,6 +198,20 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +/** +* Dump epf instructions to a file. +* +* @param f +* A pointer to a file for output +* @param buf +* A pointer to BPF instructions +* @param len +* Number of BPF instructions to dump. +*/ +__rte_experimental +void +rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len); +#ifdef RTE_PORT_PCAP struct bpf_program ;lib/bpf/version.map +0
-0
lib/bpf/version.map EXPERIMENTAL {
Quoted 6 lines
lib/bpf/version.map +1
-1@@ -19,4 +19,5 @@ EXPERIMENTAL { global: rte_bpf_convert; + rte_bpf_dump; }; --
Quoted 1 line
2.30.2
Re: [dpdk-dev] [PATCH v8 05/12] bpf: add function to dump eBPF instructions
Quoted 11 lines · Konstantin
Quoted 11 lines
lib/bpf/bpf_convert.c +2
-1@@ -331,7 +331,12 @@ static int bpf_convert_filter(const struct bpf_insn *prog, size_t len,case BPF_LD |BPF_IND |BPF_H :case BPF_LD |BPF_IND |BPF_B :/* All arithmetic insns map as-is. */ - *insn =BPF_RAW_INSN (fp->code,BPF_REG_A ,BPF_REG_X ,0 , fp->k); + insn->code = fp->code; + insn->dst_reg =BPF_REG_A ;
Quoted 8 lines
lib/bpf/bpf_convert.c static int bpf_convert_filter(const struct bpf_insn *prog, size_t len, · line 340
Quoted 5 lines
lib/bpf/bpf_convert.c +4
-0+ 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 ;Should it be part of that patch?
Looks like belongs to previous one, no?
Yes, moved it in next bundle.
PATCH v8 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 | 441 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 110 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 437 insertions(+), 128 deletions(-)
lib/meson.build +2 -2
@@ -26,6 +26,7 @@ libraries = [ 'timer', # eventdev depends on this 'acl', 'bbdev', + 'bpf', 'bitratestats', 'cfgfile', 'compressdev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'rib',
@@ -55,10 +55,10 @@ libraries = [ 'ipsec', # ipsec lib depends on net, crypto and security 'fib', #fib lib depends on rib 'port', # pkt framework libs which use other libs from above + 'pdump', # pdump lib depends on bpf pcapng 'table', 'pipeline', 'flow_classify', # flow_classify lib depends on pkt framework table lib - 'bpf', 'graph', 'node', ]
lib/pdump/meson.build +1 -1
@@ -3,4 +3,4 @@ sources = files('rte_pdump.c') headers = files('rte_pdump.h') -deps += ['ethdev'] +deps += ['ethdev', 'bpf', 'pcapng']
lib/pdump/rte_pdump.c +319 -122
656 lines not on this page · raw message
lib/pdump/rte_pdump.h +107 -3
152 lines not on this page · raw message
lib/pdump/version.map +8 -0
@@ -10,3 +10,11 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_pdump_enable_bpf; + rte_pdump_enable_bpf_by_deviceid; + rte_pdump_stats; +};
Signature · 1 line
2.30.2
PATCH v8 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 v8 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 | 173 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+)
app/test/test_bpf.c +173 -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,175 @@ test_bpf(void) }REGISTER_TEST_COMMAND (bpf_autotest, test_bpf); + +#ifdef RTE_PORT_PCAP +#include <pcap/pcap.h> + +static void +test_bpf_dump (struct bpf_program *cbf,const struct rte_bpf_prm *prm) +{ +printf ("cBPF program (%u insns)\n" , cbf->bf_len); +bpf_dump (cbf,1 ); + +printf ("\neBPF program (%u insns)\n" , prm->nb_ins); +rte_bpf_dump (stdout, prm->ins, prm->nb_ins); +} + +static int +test_bpf_match (pcap_t *pcap,const char *str,bool expected) +{ +struct bpf_program fcode; +struct rte_bpf_prm *prm =NULL ; +struct rte_bpf *bpf =NULL ; +uint8_t tbuf[sizeof (struct dummy_mbuf )]; +int ret =-1 ; +uint64_t rc; + +if (pcap_compile (pcap, &fcode, str,1 ,PCAP_NETMASK_UNKNOWN )) { +printf ("%s@%d: pcap_compile(\"%s\") failed: %s;\n" , + __func__, __LINE__, str,pcap_geterr (pcap)); +return -1 ; + } + + prm =rte_bpf_convert (&fcode); +if (prm ==NULL ) { +printf ("%s@%d: bpf_convert('%s') failed,, error=%d(%s);\n" , + __func__, __LINE__, str, rte_errno,strerror (rte_errno)); + goto error; + } + + bpf =rte_bpf_load (prm); +if (bpf ==NULL ) { +printf ("%s@%d: failed to load bpf code, error=%d(%s);\n" , + __func__, __LINE__, rte_errno,strerror (rte_errno)); + goto error; + } + +test_ld_mbuf1_prepare (tbuf); + rc =rte_bpf_exec (bpf, tbuf); +if ((rc ==0 ) == expected) + ret =0 ; +else +printf ("%s@%d: failed match: expect %s 0 got %" PRIu64"\n" , + __func__, __LINE__, expected ?"==" :"<>" , rc); +error: +if (bpf) +rte_bpf_destroy (bpf); +rte_free (prm); +pcap_freecode (&fcode); +return ret; +} + +/* Basic sanity test can we match a IP packet */ +static int +test_bpf_filter_sanity (pcap_t *pcap) +{ +int ret; + + ret =test_bpf_match (pcap,"ip" , true); + ret |=test_bpf_match (pcap,"not ip" , false); + +return ret; +} + +/* +* Some sample pcap filter strings from +* 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 www.example.com and not (port 80 or port 25)" , +"host www.example.com 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
Re: [dpdk-dev] [PATCH v8 08/12] test: add test for bpf_convert
Quoted 189 lines
Add some functional tests for the Classic BPF to DPDK BPF converter.
Signed-off-by Stephen Hemminger <[email protected]>--- app/test/test_bpf.c | 173 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+)app/test/test_bpf.c +164
-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,175 @@ test_bpf(void) }REGISTER_TEST_COMMAND (bpf_autotest, test_bpf); + +#ifdef RTE_PORT_PCAP +#include <pcap/pcap.h> + +static void +test_bpf_dump (struct bpf_program *cbf,const struct rte_bpf_prm *prm) +{ +printf ("cBPF program (%u insns)\n" , cbf->bf_len); +bpf_dump (cbf,1 ); + +printf ("\neBPF program (%u insns)\n" , prm->nb_ins); +rte_bpf_dump (stdout, prm->ins, prm->nb_ins); +} + +static int +test_bpf_match (pcap_t *pcap,const char *str,bool expected) +{ +struct bpf_program fcode; +struct rte_bpf_prm *prm =NULL ; +struct rte_bpf *bpf =NULL ; +uint8_t tbuf[sizeof (struct dummy_mbuf )]; +int ret =-1 ; +uint64_t rc; + +if (pcap_compile (pcap, &fcode, str,1 ,PCAP_NETMASK_UNKNOWN )) { +printf ("%s@%d: pcap_compile(\"%s\") failed: %s;\n" , + __func__, __LINE__, str,pcap_geterr (pcap)); +return -1 ; + } + + prm =rte_bpf_convert (&fcode); +if (prm ==NULL ) { +printf ("%s@%d: bpf_convert('%s') failed,, error=%d(%s);\n" , + __func__, __LINE__, str, rte_errno,strerror (rte_errno)); + goto error; + } + + bpf =rte_bpf_load (prm); +if (bpf ==NULL ) { +printf ("%s@%d: failed to load bpf code, error=%d(%s);\n" , + __func__, __LINE__, rte_errno,strerror (rte_errno)); + goto error; + } + +test_ld_mbuf1_prepare (tbuf); + rc =rte_bpf_exec (bpf, tbuf); +if ((rc ==0 ) == expected) + ret =0 ; +else +printf ("%s@%d: failed match: expect %s 0 got %" PRIu64"\n" , + __func__, __LINE__, expected ?"==" :"<>" , rc); +error: +if (bpf) +rte_bpf_destroy (bpf); +rte_free (prm); +pcap_freecode (&fcode); +return ret; +} + +/* Basic sanity test can we match a IP packet */ +static int +test_bpf_filter_sanity (pcap_t *pcap) +{ +int ret; + + ret =test_bpf_match (pcap,"ip" , true); + ret |=test_bpf_match (pcap,"not ip" , false); + +return ret; +} + +/* +* Some sample pcap filter strings from +* 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 www.example.com and not (port 80 or port 25)" , +"host www.example.com 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);
app/test/test_bpf.c test_bpf(void)
Quoted 10 lines
app/test/test_bpf.c +9
-1+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 */ --
Quoted 1 line
2.30.2
PATCH v8 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 | 1 + app/test/test_pcapng.c | 190 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 app/test/test_pcapng.c
app/test/meson.build +1 -0
@@ -97,6 +97,7 @@ test_sources = files( 'test_metrics.c', 'test_mcslock.c', 'test_mp_secondary.c', + 'test_pcapng.c', 'test_per_lcore.c', 'test_pflock.c', 'test_pmd_perf.c',
app/test/test_pcapng.c new file +190 -0
190 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v8 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
Re: [dpdk-dev] [PATCH v8 10/12] test: enable bpf autotest
Quoted 15 lines
The BPF autotest is defined but not run automatically. Since it is short, it should be added to the autotest suite.
Signed-off-by Stephen Hemminger <[email protected]>--- app/test/meson.build | 2 ++ 1 file changed, 2 insertions(+)app/test/meson.build +0
-0
app/test/meson.build test_deps = [
Quoted 9 lines
app/test/meson.build +2
-1@@ -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], --
Quoted 1 line
2.30.2
Re: [dpdk-dev] [PATCH v8 10/12] test: enable bpf autotest
Quoted 8 lines · Konstantin
Quoted 8 lines
The BPF autotest is defined but not run automatically. Since it is short, it should be added to the autotest suite.
Signed-off-by Stephen Hemminger <[email protected]>--- app/test/meson.build | 2 ++ 1 file changed, 2 insertions(+)
Quoted 19 lines
Quoted 6 lines
app/test/meson.build +0
-0app/test/meson.build test_deps = [ · line 201
Quoted 8 lines
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],Quoted 1 line
2.30.2
One oddity of original BPF test is that it constructs an mbuf where there is no Ethernet header. Didn't want to change that since not only would the constructor have to be changed but also the hand written BPF programs as well.
Not necessarily a bug, but the test is not doing what any application using BPF on incoming mbuf's would expect to do.
Re: [dpdk-dev] [PATCH v8 10/12] test: enable bpf autotest
In reply to Stephen HemmingerQuoted 13 lines · On Mon, 13 Sep 2021 11:15:08 -0700 Stephen Hemminger…
The BPF autotest is defined but not run automatically. Since it is short, it should be added to the autotest suite.
Signed-off-by Stephen Hemminger <[email protected]>--- app/test/meson.build | 2 ++ 1 file changed, 2 insertions(+)app/test/meson.build +0
-0
app/test/meson.build test_deps = [ · line 201
Quoted 8 lines
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],
Note: this patch exposes a pre-existing bug in DPDK:
https://bugs.dpdk.org/show_bug.cgi?id=811
The BPF code does not work if built with Clang.
The test was just being ignored by the CI before!
PATCH v8 11/12 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 86 +++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 228 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -82,6 +82,16 @@ New Features * Added PDCP short MAC-I support. +* **Enhanced Packet capture.** + + * 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 v8 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
@@ -1423,12 +1423,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
v9 cover Packet capture framework enhancements
In reply to Stephen HemmingerThis patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
v9 changes:
- incorporate suggested change to BPF XOR
- make autotest for pcapng more complete by reading the resulting file with libpcap
v8 changes:
- enable BPF tests in autotest
- add more BPF test strings
- use rte_strscpy to satisfy checkpatch
- merge MAINTAINERS (put this in with existing pdump)
Stephen Hemminger (12):
librte_pcapng: add new library for writing pcapng files
lib: pdump is not supported on Windows
bpf: allow self-xor operation
bpf: add function to convert classic BPF to DPDK BPF
bpf: add function to dump eBPF instructions
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
test: add test for bpf_convert
test: add a test for pcapng library
test: enable bpf autotest
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new packet capture features
MAINTAINERS | 11 +- app/dumpcap/main.c | 844 ++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + app/test/meson.build | 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 | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 575 ++++++++++++ lib/bpf/bpf_dump.c | 118 +++ lib/bpf/bpf_validate.c | 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 | 574 ++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 441 ++++++--- lib/pdump/rte_pdump.h | 110 ++- lib/pdump/version.map | 8 + 33 files changed, 3683 insertions(+), 220 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 v9 1/12 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 194 +++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 918 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +574 -0
574 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +194 -0
194 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH v9 2/12 lib: pdump is not supported on Windows
In reply to Stephen HemmingerThe current version of the pdump library was building on Windows, but it was useless since the pdump utility was not being built and Windows does not have multi-process support.
The new version of pdump with filtering now has dependency on bpf. But bpf library is not available on Windows.
--- lib/meson.build | 1 - 1 file changed, 1 deletion(-)
lib/meson.build +0 -1
@@ -85,7 +85,6 @@ if is_windows 'gro', 'gso', 'latencystats', - 'pdump', ] # only supported libraries for windows endif
Signature · 1 line
2.30.2
PATCH v9 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
Re: [dpdk-dev] [PATCH v9 03/12] bpf: allow self-xor operation
Quoted 28 lines
Some 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.
Fixes 8021917293d0 ("bpf: add extra validation for input BPF program")Signed-off-by Stephen Hemminger <[email protected]>--- lib/bpf/bpf_validate.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)lib/bpf/bpf_validate.c +4
-0@@ -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 );
lib/bpf/bpf_validate.c eval_alu(struct bpf_verifier *bvf, const struct ebpf_insn *ins)
Quoted 10 lines
lib/bpf/bpf_validate.c +4
-2+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; --
Quoted 1 line
2.30.2
PATCH v9 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 v9 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 | 118 ++++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 ++++++ lib/bpf/version.map | 1 + 4 files changed, 134 insertions(+) create mode 100644 lib/bpf/bpf_dump.c
lib/bpf/bpf_dump.c new file +118 -0
@@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright (c) 2021 Stephen Hemminger +* Based on filter2xdp +* Copyright (C) 2017 Tobias Klauser +*/ + +#include <stdio.h> +#include <stdint.h> + +#include "rte_bpf.h" + +#define BPF_OP_INDEX (x) (BPF_OP(x) >> 4) +#define BPF_SIZE_INDEX (x) (BPF_SIZE(x) >> 3) + +static const char *const class_tbl[] = { + [BPF_LD ] ="ld" , [BPF_LDX ] ="ldx" , [BPF_ST ] ="st" , + [BPF_STX ] ="stx" , [BPF_ALU ] ="alu" , [BPF_JMP ] ="jmp" , + [BPF_RET ] ="ret" , [BPF_MISC ] ="alu64" , +}; + +static const char *const alu_op_tbl[16 ] = { + [BPF_ADD >>4 ] ="add" , [BPF_SUB >>4 ] ="sub" , + [BPF_MUL >>4 ] ="mul" , [BPF_DIV >>4 ] ="div" , + [BPF_OR >>4 ] ="or" , [BPF_AND >>4 ] ="and" , + [BPF_LSH >>4 ] ="lsh" , [BPF_RSH >>4 ] ="rsh" , + [BPF_NEG >>4 ] ="neg" , [BPF_MOD >>4 ] ="mod" , + [BPF_XOR >>4 ] ="xor" , [EBPF_MOV >>4 ] ="mov" , + [EBPF_ARSH >>4 ] ="arsh" , [EBPF_END >>4 ] ="endian" , +}; + +static const char *const size_tbl[] = { + [BPF_W >>3 ] ="w" , + [BPF_H >>3 ] ="h" , + [BPF_B >>3 ] ="b" , + [EBPF_DW >>3 ] ="dw" , +}; + +static const char *const jump_tbl[16 ] = { + [BPF_JA >>4 ] ="ja" , [BPF_JEQ >>4 ] ="jeq" , + [BPF_JGT >>4 ] ="jgt" , [BPF_JGE >>4 ] ="jge" , + [BPF_JSET >>4 ] ="jset" , [EBPF_JNE >>4 ] ="jne" , + [EBPF_JSGT >>4 ] ="jsgt" , [EBPF_JSGE >>4 ] ="jsge" , + [EBPF_CALL >>4 ] ="call" , [EBPF_EXIT >>4 ] ="exit" , +}; + +static void ebpf_dump (FILE *f,const struct ebpf_insn insn,size_t n) +{ +const char *op, *postfix ="" ; +uint8_t cls =BPF_CLASS (insn.code); + +fprintf (f," L%zu:\t" , n); + +switch (cls) { +default : +fprintf (f,"unimp 0x%x // class: %s\n" , insn.code, + class_tbl[cls]); +break ; +case BPF_ALU : + postfix ="32" ; +/* fall through */ +case EBPF_ALU64 : + op = alu_op_tbl[BPF_OP_INDEX (insn.code)]; +if (BPF_SRC (insn.code) ==BPF_X ) +fprintf (f,"%s%s r%u, r%u\n" , op, postfix, insn.dst_reg, + insn.src_reg); +else +fprintf (f,"%s%s r%u, #0x%x\n" , op, postfix, + insn.dst_reg, insn.imm); +break ; +case BPF_LD : + op ="ld" ; + postfix = size_tbl[BPF_SIZE_INDEX (insn.code)]; +if (BPF_MODE (insn.code) ==BPF_IMM ) +fprintf (f,"%s%s r%d, #0x%x\n" , op, postfix, + insn.dst_reg, insn.imm); +else if (BPF_MODE (insn.code) ==BPF_ABS ) +fprintf (f,"%s%s r%d, [%d]\n" , op, postfix, + insn.dst_reg, insn.imm); +else if (BPF_MODE (insn.code) ==BPF_IND ) +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, + insn.dst_reg, insn.src_reg, insn.imm); +else +fprintf (f,"// BUG: LD opcode 0x%02x in eBPF insns\n" , + insn.code); +break ; +case BPF_LDX : + op ="ldx" ; + postfix = size_tbl[BPF_SIZE_INDEX (insn.code)]; +fprintf (f,"%s%s r%d, [r%u + %d]\n" , op, postfix, insn.dst_reg, + insn.src_reg, insn.off); +break ; +#define L (pc, off) ((int)(pc) + 1 + (off)) +case BPF_JMP : + op = jump_tbl[BPF_OP_INDEX (insn.code)]; +if (op ==NULL ) +fprintf (f,"invalid jump opcode: %#x\n" , insn.code); +else if (BPF_OP (insn.code) ==BPF_JA ) +fprintf (f,"%s L%d\n" , op,L (n, insn.off)); +else if (BPF_OP (insn.code) ==EBPF_EXIT ) +fprintf (f,"%s\n" , op); +else +fprintf (f,"%s r%u, #0x%x, L%d\n" , op, insn.dst_reg, + insn.imm,L (n, insn.off)); +break ; +case BPF_RET : +fprintf (f,"// BUG: RET opcode 0x%02x in eBPF insns\n" , + insn.code); +break ; + } +} + +void rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len) +{ +uint32_t i; + +for (i =0 ; i < len; ++i) +ebpf_dump (f, buf[i], i); +}
lib/bpf/meson.build +1 -0
@@ -2,6 +2,7 @@ # Copyright(c) 2018 Intel Corporation sources = files('bpf.c', + 'bpf_dump.c', 'bpf_exec.c', 'bpf_load.c', 'bpf_pkt.c',
lib/bpf/rte_bpf.h +14 -0
@@ -198,6 +198,20 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],int rte_bpf_get_jit (const struct rte_bpf *bpf,struct rte_bpf_jit *jit); +/** +* Dump epf instructions to a file. +* +* @param f +* A pointer to a file for output +* @param buf +* A pointer to BPF instructions +* @param len +* Number of BPF instructions to dump. +*/ +__rte_experimental +void +rte_bpf_dump (FILE *f,const struct ebpf_insn *buf,uint32_t len); +#ifdef RTE_PORT_PCAP struct bpf_program ;
lib/bpf/version.map +1 -0
@@ -19,4 +19,5 @@ EXPERIMENTAL { global: rte_bpf_convert; + rte_bpf_dump; };
Signature · 1 line
2.30.2
PATCH v9 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 | 441 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 110 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 437 insertions(+), 128 deletions(-)
lib/meson.build +2 -2
@@ -26,6 +26,7 @@ libraries = [ 'timer', # eventdev depends on this 'acl', 'bbdev', + 'bpf', 'bitratestats', 'cfgfile', 'compressdev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'rib',
@@ -55,10 +55,10 @@ libraries = [ 'ipsec', # ipsec lib depends on net, crypto and security 'fib', #fib lib depends on rib 'port', # pkt framework libs which use other libs from above + 'pdump', # pdump lib depends on bpf pcapng 'table', 'pipeline', 'flow_classify', # flow_classify lib depends on pkt framework table lib - 'bpf', 'graph', 'node', ]
lib/pdump/meson.build +1 -1
@@ -3,4 +3,4 @@ sources = files('rte_pdump.c') headers = files('rte_pdump.h') -deps += ['ethdev'] +deps += ['ethdev', 'bpf', 'pcapng']
lib/pdump/rte_pdump.c +319 -122
656 lines not on this page · raw message
lib/pdump/rte_pdump.h +107 -3
152 lines not on this page · raw message
lib/pdump/version.map +8 -0
@@ -10,3 +10,11 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_pdump_enable_bpf; + rte_pdump_enable_bpf_by_deviceid; + rte_pdump_stats; +};
Signature · 1 line
2.30.2
PATCH v9 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 v9 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 ) { + .d_addr.addr_bytes = {0xff ,0xff ,0xff ,0xff ,0xff ,0xff }, + .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 v9 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
@@ -395,6 +395,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 v9 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
@@ -193,6 +193,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 v9 11/12 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 86 +++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 228 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -87,6 +87,16 @@ New Features Added command-line options to specify total number of processes and current process ID. Each process owns subset of Rx and Tx queues. +* **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 v9 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
@@ -1422,12 +1422,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
v10 cover Packet capture framework enhancements
In reply to Stephen HemmingerThis patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
There are no blocker items. The following are worth noting:
* some of the patches get bogus checkpatch warnings
* enabling BPF tests causes CI to see a pre-existing bug
* filtering for stripped VLAN tags requires changes to libpcap (to be addressed in future)
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):
librte_pcapng: add new library for writing pcapng files
lib: pdump is not supported on Windows
bpf: allow self-xor operation
bpf: add function to convert classic BPF to DPDK BPF
bpf: add function to dump eBPF instructions
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
test: add test for bpf_convert
test: add a test for pcapng library
test: enable bpf autotest
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new packet capture features
MAINTAINERS | 11 +- app/dumpcap/main.c | 844 ++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + app/test/meson.build | 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 | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 575 ++++++++++++ lib/bpf/bpf_dump.c | 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 | 574 ++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 441 ++++++--- lib/pdump/rte_pdump.h | 110 ++- lib/pdump/version.map | 8 + 33 files changed, 3704 insertions(+), 220 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 v10 1/12 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 194 +++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 918 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +574 -0
574 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +194 -0
194 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH v10 2/12 lib: pdump is not supported on Windows
In reply to Stephen HemmingerThe current version of the pdump library was building on Windows, but it was useless since the pdump utility was not being built and Windows does not have multi-process support.
The new version of pdump with filtering now has dependency on bpf. But bpf library is not available on Windows.
--- lib/meson.build | 1 - 1 file changed, 1 deletion(-)
lib/meson.build +0 -1
@@ -85,7 +85,6 @@ if is_windows 'gro', 'gso', 'latencystats', - 'pdump', ] # only supported libraries for windows endif
Signature · 1 line
2.30.2
PATCH v10 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 v10 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 v10 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 v10 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 | 441 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 110 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 437 insertions(+), 128 deletions(-)
lib/meson.build +2 -2
@@ -26,6 +26,7 @@ libraries = [ 'timer', # eventdev depends on this 'acl', 'bbdev', + 'bpf', 'bitratestats', 'cfgfile', 'compressdev',
@@ -43,7 +44,6 @@ libraries = [ 'member', 'pcapng', 'power', - 'pdump', 'rawdev', 'regexdev', 'rib',
@@ -55,10 +55,10 @@ libraries = [ 'ipsec', # ipsec lib depends on net, crypto and security 'fib', #fib lib depends on rib 'port', # pkt framework libs which use other libs from above + 'pdump', # pdump lib depends on bpf pcapng 'table', 'pipeline', 'flow_classify', # flow_classify lib depends on pkt framework table lib - 'bpf', 'graph', 'node', ]
lib/pdump/meson.build +1 -1
@@ -3,4 +3,4 @@ sources = files('rte_pdump.c') headers = files('rte_pdump.h') -deps += ['ethdev'] +deps += ['ethdev', 'bpf', 'pcapng']
lib/pdump/rte_pdump.c +319 -122
656 lines not on this page · raw message
lib/pdump/rte_pdump.h +107 -3
152 lines not on this page · raw message
lib/pdump/version.map +8 -0
@@ -10,3 +10,11 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_pdump_enable_bpf; + rte_pdump_enable_bpf_by_deviceid; + rte_pdump_stats; +};
Signature · 1 line
2.30.2
Re: [dpdk-dev] [PATCH v10 06/12] pdump: support pcapng and filtering
Quoted 8 lines
-----Original Message-----
From: dev <[email protected]> On Behalf Of Stephen Hemminger+++ b/lib/meson.build
+ 'bpf',
'bitratestats',
If alphabetical order, should bpf come after bitratestats?
lib/pdump/rte_pdump.c enum pdump_operation { · line 39
Quoted 9 lines
lib/pdump/rte_pdump.c +7
-1+/* +* Note: version numbers intentionally start at 3 +* in order to catch any application built with older out +* version of DPDK using incompatible client request format. +*/ enum pdump_version { -V1 =1 +PDUMP_CLIENT_LEGACY =3 , +PDUMP_CLIENT_PCAPNG =4 ,
The version numbering was internal to library, applications do not have control over it, can't we start enumeration from 1?
lib/pdump/rte_pdump.c enum pdump_operation { · line 45
Quoted 2 lines
struct
pdump_request{lib/pdump/rte_pdump.c +1
-0+uint16_t flags;
Why is the flags type changed from unit32_t unint16_t?
lib/pdump/rte_pdump.c static struct pdump_rxtx_cbs { · line 112
Quoted 5 lines
lib/pdump/rte_pdump.c +4
-0+* Similar behavior to rte_bpf_eth callback. +* if BPF program returns zero value for a given packet, +* then it will be ignored. +*/
Looks like wrong callback name referred in the comment, should be corrected?
lib/pdump/rte_pdump.c static struct pdump_rxtx_cbs { · line 113
Quoted 1 line
lib/pdump/rte_pdump.c +1
-0+if (cbs->filter && rcs[i] ==0 ) {
Why do we need to do this again if some packets already filtered.
lib/pdump/rte_pdump.c set_pdump_rxtx_cbs(const struct pdump_request *p) · line 304
Quoted 6 lines
lib/pdump/rte_pdump.c +6
-0+if (!(p->ver ==PDUMP_CLIENT_LEGACY || + p->ver ==PDUMP_CLIENT_PCAPNG )) { +PDUMP_LOG (ERR , +"incorrect client version %u\n" , p->ver); +return -EINVAL ; + }
This check is not useful here I guess, as we are setting the version in the library itself below.
Review
Quoted 3 lines
(quoted diff) +2
-0+pdump_prepare_client_request(const char *device, uint16_t queue, + req->queue = queue;
This assignment is done below as well, so here it is redundant I guess?
lib/pdump/rte_pdump.c rte_pdump_disable_by_deviceid(char *device_id, uint16_t queue, · line 740
Quoted 10 lines
lib/pdump/rte_pdump.c +5
-1- }else { + req->queue = queue; } +if (pdump_stats ==NULL ) { +if (rte_eal_process_type () ==RTE_PROC_PRIMARY ) { +PDUMP_LOG (ERR , +"pdump not initialized\n" );
Might be god to say "pdump stats" not initialized instead of just saying "pdump"?
Quoted 11 lines
+/**
+ * Retrieve the packet capture statistics for a queue.
+ *
+ * @paramport_id
+ * The port identifier of the Ethernet device.
+ * @param stats
+ * A pointer to structure of type *rte_pdump_stats* to be filled in.
+ * @return
+ * Zero if successful. -1 on error andrte_errnois set.
+ */
Missing below experimental warning in the above comments .
Quoted 3 lines
+ * @warning
+ * @b EXPERIMENTAL: this API may change, or be removed, without prior +notice
Re: [dpdk-dev] [PATCH v10 06/12] pdump: support pcapng and filtering
Quoted 10 lines · Reshma
lib/pdump/rte_pdump.c enum pdump_operation { · line 39
Quoted 9 lines
lib/pdump/rte_pdump.c +7
-1+/* +* Note: version numbers intentionally start at 3 +* in order to catch any application built with older out +* version of DPDK using incompatible client request format. +*/ enum pdump_version { -V1 =1 +PDUMP_CLIENT_LEGACY =3 , +PDUMP_CLIENT_PCAPNG =4 ,The version numbering was internal to library, applications do not have control over it, can't we start enumeration from 1?
Although, DPDK does not support mixing versions between primary/secondary process. Someone is sure to try.
I wanted to make sure that if user did something invalid like using old pdump (built with DPDK 20.11) and new application that it would fail in a direct manner.
Re: [dpdk-dev] [PATCH v10 06/12] pdump: support pcapng and filtering
In reply to Pattan, ReshmaQuoted 3 lines · Reshma
lib/pdump/rte_pdump.c enum pdump_operation { · line 45
Quoted 2 lines
struct
pdump_request{lib/pdump/rte_pdump.c +1
-0+uint16_t flags;Why is the flags type changed from
unit32_tunint16_t?
Only to pack the structure. They were unused.
Re: [dpdk-dev] [PATCH v10 06/12] pdump: support pcapng and filtering
In reply to Pattan, ReshmaQuoted 6 lines · Reshma
lib/pdump/rte_pdump.c static struct pdump_rxtx_cbs { · line 112
Quoted 5 lines
lib/pdump/rte_pdump.c +4
-0+* Similar behavior to rte_bpf_eth callback. +* if BPF program returns zero value for a given packet, +* then it will be ignored. +*/ Looks like wrong callback name referred in the comment, should be corrected?
It really is pcap_offline_filter() and Linux kernel socket filter.
Quoted 2 lines
lib/pdump/rte_pdump.c static struct pdump_rxtx_cbs { · line 113
Quoted 1 line
lib/pdump/rte_pdump.c +1
-0+if (cbs->filter && rcs[i] ==0 ) {Why do we need to do this again if some packets already filtered.
The earlier call (rte_bpf_exec_burst) returns the number of packets that were processed. Actually, the return value there is always equal n.
So this code is the filtering, there was an issue with checking return value of exec_burst.
Re: [dpdk-dev] [PATCH v10 06/12] pdump: support pcapng and filtering
Quoted 4 lines
-----Original Message-----
Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
lib/pdump/rte_pdump.c static struct pdump_rxtx_cbs { · line 112
Quoted 4 lines
lib/pdump/rte_pdump.c +4
-0+* Similar behavior to rte_bpf_eth callback. +* if BPF program returns zero value for a given packet, +* then it will be ignored. +*/ Looks like wrong callback name referred in the comment, should be
corrected?
It really is
pcap_offline_filter()and Linux kernel socket filter.
Oh ok, rcs[i] is basically "return value of the filter program. This will be zero if the packet doesn't match the filter and non-zero if the packet matches the filter." got it now on why is the below if check.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
lib/pdump/rte_pdump.c +1
-0+if (cbs->filter && rcs[i] ==0 ) {
PATCH v10 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 v10 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 ) { + .d_addr.addr_bytes = {0xff ,0xff ,0xff ,0xff ,0xff ,0xff }, + .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 v10 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
@@ -395,6 +395,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 v10 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
@@ -193,6 +193,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 v10 11/12 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 86 +++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 228 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -87,6 +87,16 @@ New Features Added command-line options to specify total number of processes and current process ID. Each process owns subset of Rx and Tx queues. +* **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 v10 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
@@ -1424,12 +1424,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
v11 cover Packet capture framework enhancements
In reply to Stephen HemmingerThis patch set is a more complete version of the the enhanced packet capture support described last year.
The new capture library and utility are:
- faster avoids lots of extra I/O, does bursting, etc.
- gives more information (multiple ports, queues, etc)
- has a better user interface (same as Wireshark dumpcap)
- fixes structural problems with VLAN's and timestamps
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
* filtering for stripped VLAN tags requires changes to libpcap (to be addressed in future)
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):
librte_pcapng: add new library for writing pcapng files
lib: pdump is not supported on Windows
bpf: allow self-xor operation
bpf: add function to convert classic BPF to DPDK BPF
bpf: add function to dump eBPF instructions
pdump: support pcapng and filtering app/dumpcap: add new packet capture application
test: add test for bpf_convert
test: add a test for pcapng library
test: enable bpf autotest
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new packet capture features
MAINTAINERS | 11 +- app/dumpcap/main.c | 844 ++++++++++++++++++ app/dumpcap/meson.build | 16 + app/meson.build | 1 + app/test/meson.build | 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 | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 575 ++++++++++++ lib/bpf/bpf_dump.c | 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 | 574 ++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 427 ++++++--- lib/pdump/rte_pdump.h | 113 ++- lib/pdump/version.map | 8 + 33 files changed, 3694 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 v11 1/12 librte_pcapng: add new library for writing pcapng files
This is utility library for writing pcapng format files used by Wireshark family of utilities. Older tcpdump also knows how to read (but not write) this format.
See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 +++++++++ lib/pcapng/rte_pcapng.c | 574 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 194 +++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 918 insertions(+) create mode 100644 lib/pcapng/meson.build create mode 100644 lib/pcapng/pcapng_proto.h create mode 100644 lib/pcapng/rte_pcapng.c create mode 100644 lib/pcapng/rte_pcapng.h create mode 100644 lib/pcapng/version.map
lib/meson.build +1 -0
@@ -41,6 +41,7 @@ libraries = [ 'latencystats', 'lpm', 'member', + 'pcapng', 'power', 'pdump', 'rawdev',
lib/pcapng/meson.build new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2019 Microsoft Corporation + +version = 1 +sources = files('rte_pcapng.c') +headers = files('rte_pcapng.h') + +deps += ['ethdev']
lib/pcapng/pcapng_proto.h new file +129 -0
129 lines not on this page · raw message
lib/pcapng/rte_pcapng.c new file +574 -0
574 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +194 -0
194 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
PATCH v11 2/12 lib: pdump is not supported on Windows
In reply to Stephen HemmingerThe current version of the pdump library was building on Windows, but it was useless since the pdump utility was not being built and Windows does not have multi-process support.
The new version of pdump with filtering now has dependency on bpf. But bpf library is not available on Windows.
--- lib/meson.build | 1 - 1 file changed, 1 deletion(-)
lib/meson.build +0 -1
@@ -85,7 +85,6 @@ if is_windows 'gro', 'gso', 'latencystats', - 'pdump', ] # only supported libraries for windows endif
Signature · 1 line
2.30.2
PATCH v11 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 v11 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 v11 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 v11 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 | 427 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 113 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 427 insertions(+), 127 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 +306 -121
643 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 v11 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 v11 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 ) { + .d_addr.addr_bytes = {0xff ,0xff ,0xff ,0xff ,0xff ,0xff }, + .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 v11 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
@@ -395,6 +395,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 v11 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
@@ -193,6 +193,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 v11 11/12 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 86 +++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 228 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -91,6 +91,16 @@ New Features Added command-line options to specify total number of processes and current process ID. Each process owns subset of Rx and Tx queues. +* **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 v11 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
v12 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().
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
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 | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 575 ++++++++++++ lib/bpf/bpf_dump.c | 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 | 613 +++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 427 ++++++--- lib/pdump/rte_pdump.h | 113 ++- lib/pdump/version.map | 8 + 33 files changed, 3733 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 v12 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
@@ -84,7 +84,6 @@ if is_windows 'gro', 'gso', 'latencystats', - 'pdump', 'stack', ] # only supported libraries for windows endif
Signature · 1 line
2.30.2
PATCH v12 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 draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
and
https://github.com/pcapng/pcapng/
--- lib/meson.build | 1 + lib/pcapng/meson.build | 8 + lib/pcapng/pcapng_proto.h | 129 ++++++++ lib/pcapng/rte_pcapng.c | 613 ++++++++++++++++++++++++++++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++++++++++ lib/pcapng/version.map | 12 + 6 files changed, 957 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 +613 -0
613 lines not on this page · raw message
lib/pcapng/rte_pcapng.h new file +194 -0
194 lines not on this page · raw message
lib/pcapng/version.map new file +12 -0
@@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_pcapng_close; + rte_pcapng_copy; + rte_pcapng_fdopen; + rte_pcapng_mbuf_size; + rte_pcapng_write_packets; + rte_pcapng_write_stats; + + local: *; +};
Signature · 1 line
2.30.2
Re: [dpdk-dev] [PATCH v12 02/12] librte_pcapng: add new library for writing pcapng files
Quoted 6 lines
-----Original Message-----
From: dev <[email protected]> On Behalf Of Stephen Hemminger See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
The page is not found. Might need to add new link I guess
lib/pcapng/pcapng_proto.h line 60
Quoted 3 lines
lib/pcapng/pcapng_proto.h new file +3
-0+enum pcapng_interface_options { +PCAPNG_IFB_NAME =2 , +PCAPNG_IFB_DESCRIPTION ,
Can IFB(interface block) be replaced with IF(interface) only? But that's ok, upto u.
lib/pcapng/rte_pcapng.c line 109
Quoted 3 lines
lib/pcapng/rte_pcapng.c new file +3
-0+ buf =calloc (1 , len); +if (!buf) +return -1 ;
How about returning -ENOMEM
lib/pcapng/rte_pcapng.c line 120
Quoted 11 lines
lib/pcapng/rte_pcapng.c new file +11
-0+ + hdr = (struct pcapng_section_header *)buf; + *hdr = (struct pcapng_section_header ) { + .block_type =PCAPNG_SECTION_BLOCK , + .block_length = len, + .byte_order_magic =PCAPNG_BYTE_ORDER_MAGIC , + .major_version =PCAPNG_MAJOR_VERS , + .minor_version =PCAPNG_MINOR_VERS , + .section_length =UINT64_MAX , + }; + hdr->block_length = len;
Why to assign block_len with len again? as it is already done few lines above.
Review
Quoted 1 line
(quoted diff) +1
-0+ opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0);
Some comments around this code, about adding end of options at the end of options list would be helpful.
Review
Quoted 2 lines
(quoted diff) +2
-0+ +/* Write the PCAPNG section header at start of file */ static ssize_t
:s/section header/ interface header?
lib/pcapng/rte_pcapng.c line 163
Quoted 3 lines
lib/pcapng/rte_pcapng.c new file +3
-0+pcapng_interface_block(rte_pcapng_t *self, const char *if_name, + if (mac_addr) + len += pcapng_optlen(6);
How about using RTE_ETHER_ADDR_LEN instead of 6
Quoted 1 line
+struct
rte_mbuf* rte_pcapng_copy(uint16_tport_id,uint32_tqueue,
<snip>
lib/pcapng/rte_pcapng.c line 520
Quoted 2 lines
lib/pcapng/rte_pcapng.c new file +2
-0+fail: +rte_pktmbuf_free (mc);
Freeing mc , would that take care of freeing up the additional byte prepended after mc creation?
lib/pcapng/rte_pcapng.c line 495
Quoted 2 lines
lib/pcapng/rte_pcapng.c new file +2
-0+ opt =pcapng_add_option (opt,PCAPNG_EPB_QUEUE , + &queue,sizeof (queue));
Don't we need to add end of options to the end of option list, like did in Interface block and section header block?
lib/pcapng/rte_pcapng.h new file mode
Quoted 3 lines
lib/pcapng/rte_pcapng.h new file mode renamed from lib/pcapng/rte_pcapng.h +2
-0+ * + * Packets to be captured are copied by rte_pcapng_mbuf()
Do you mean by rte_pcapng_copy()?
Re: [dpdk-dev] [PATCH v12 02/12] librte_pcapng: add new library for writing pcapng files
Quoted 5 lines · Reshma
lib/pcapng/rte_pcapng.c line 109
Quoted 3 lines
lib/pcapng/rte_pcapng.c new file +3
-0+ buf =calloc (1 , len); +if (!buf) +return -1 ;How about returning
-ENOMEM
It could but not necessary.
The other code is returning result of write and therefore would be -1 on write error.
This is in internal local function and the only caller is just checking for < 0.
PS: In reality, malloc can't fail on Linux. Process gets oom killed instead (unless someone has gone and tweaked the memory allocator to not overcommit).
Re: [dpdk-dev] [PATCH v12 02/12] librte_pcapng: add new library for writing pcapng files
In reply to Pattan, ReshmaQuoted 34 lines · Reshma
Quoted 4 lines
-----Original Message-----
From: dev <[email protected]> On Behalf Of Stephen Hemminger See draft RFC
https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.htmlThe page is not found. Might need to add new link I guess
lib/pcapng/pcapng_proto.h line 60
Quoted 3 lines
lib/pcapng/pcapng_proto.h new file +3
-0+enum pcapng_interface_options { +PCAPNG_IFB_NAME =2 , +PCAPNG_IFB_DESCRIPTION ,Can IFB(interface block) be replaced with IF(interface) only? But that's ok, upto u.
lib/pcapng/rte_pcapng.c line 109
Quoted 3 lines
lib/pcapng/rte_pcapng.c new file +3
-0+ buf =calloc (1 , len); +if (!buf) +return -1 ;How about returning
-ENOMEMlib/pcapng/rte_pcapng.c line 120
Quoted 11 lines
lib/pcapng/rte_pcapng.c new file +11
-0+ + hdr = (struct pcapng_section_header *)buf; + *hdr = (struct pcapng_section_header ) { + .block_type =PCAPNG_SECTION_BLOCK , + .block_length = len, + .byte_order_magic =PCAPNG_BYTE_ORDER_MAGIC , + .major_version =PCAPNG_MAJOR_VERS , + .minor_version =PCAPNG_MINOR_VERS , + .section_length =UINT64_MAX , + }; + hdr->block_length = len;Why to assign
block_lenwith len again? as it is already done few lines above.
Quoted 3 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0);Some comments around this code, about adding end of options at the end of options list would be helpful.
Ok, but someone looking at this code should really look at the standard to see what the data format is.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +/* Write the PCAPNG section header at start of file */ static ssize_t:s/section header/ interface header?
Good catch, copy/paste of comment.
Quoted 6 lines
lib/pcapng/rte_pcapng.c line 163
Quoted 4 lines
lib/pcapng/rte_pcapng.c new file +3
-0+pcapng_interface_block(rte_pcapng_t *self, const char *if_name, + if (mac_addr) + len += pcapng_optlen(6);How about using
RTE_ETHER_ADDR_LENinstead of 6
Fixing now, also merging pcapng_interface_block since only called one place.
Quoted 8 lines
Quoted 2 lines
+struct
rte_mbuf* rte_pcapng_copy(uint16_tport_id,uint32_tqueue,<snip>
lib/pcapng/rte_pcapng.c line 520
Quoted 2 lines
lib/pcapng/rte_pcapng.c new file +2
-0+fail: +rte_pktmbuf_free (mc);Freeing mc , would that take care of freeing up the additional byte prepended after mc creation?
Mbuf are allocation unit, so the whole buffer goes.
Quoted 5 lines
lib/pcapng/rte_pcapng.c line 495
Quoted 3 lines
lib/pcapng/rte_pcapng.c new file +2
-0+ opt =pcapng_add_option (opt,PCAPNG_EPB_QUEUE , + &queue,sizeof (queue));Don't we need to add end of options to the end of option list, like did in Interface block and section header block?
It turns out that the reference (wireshark) does not. So did not do that to save space on the output file.
Quoted 6 lines
lib/pcapng/rte_pcapng.h new file mode
Quoted 4 lines
lib/pcapng/rte_pcapng.h new file mode renamed from lib/pcapng/rte_pcapng.h +2
-0+ * + * Packets to be captured are copied by rte_pcapng_mbuf()Do you mean by
rte_pcapng_copy()?
Good catch, function got renamed and comment not updated.
PATCH v12 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 v12 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 v12 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 v12 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 | 427 ++++++++++++++++++++++++++++++------------ lib/pdump/rte_pdump.h | 113 ++++++++++- lib/pdump/version.map | 8 + 5 files changed, 427 insertions(+), 127 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 +306 -121
643 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 v12 06/12] pdump: support pcapng and filtering
Quoted 4 lines
-----Original Message-----
From: dev <[email protected]> On Behalf Of Stephen Hemminger
I see all comments from v10 been addressed. A small Nitpick,
lib/pdump/rte_pdump.c rte_pdump_disable_by_deviceid(char *device_id, uint16_t queue, · line 728
Quoted 3 lines
lib/pdump/rte_pdump.c +3
-0+if (pdump_stats ==NULL ) { +if (rte_eal_process_type () ==RTE_PROC_PRIMARY ) { +PDUMP_LOG (ERR ,"pdump stats initialized\n" );
:s/initialized/not initialized ?
PATCH v12 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 v12 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 ) { + .d_addr.addr_bytes = {0xff ,0xff ,0xff ,0xff ,0xff ,0xff }, + .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 v12 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 v12 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 v12 11/12 doc: changes for new pcapng and dumpcap
In reply to Stephen HemmingerDescribe the new packet capture library and utilities
--- doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + .../howto/img/packet_capture_framework.svg | 96 +++++++++---------- doc/guides/howto/packet_capture_framework.rst | 67 ++++++------- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 +++++ doc/guides/prog_guide/pdump_lib.rst | 28 ++++-- doc/guides/rel_notes/release_21_11.rst | 10 ++ doc/guides/tools/dumpcap.rst | 86 +++++++++++++++++ doc/guides/tools/index.rst | 1 + 10 files changed, 228 insertions(+), 87 deletions(-) create mode 100644 doc/guides/prog_guide/pcapng_lib.rst create mode 100644 doc/guides/tools/dumpcap.rst
doc/api/doxy-api-index.md +1 -0
@@ -223,3 +223,4 @@ The public API headers are grouped by topics: [experimental APIs] (@ref rte_compat.h), [ABI versioning] (@ref rte_function_versioning.h), [version] (@ref rte_version.h) + [pcapng] (@ref rte_pcapng.h)
doc/api/doxy-api.conf.in +1 -0
@@ -58,6 +58,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/metrics \ @TOPDIR@/lib/node \ @TOPDIR@/lib/net \ + @TOPDIR@/lib/pcapng \ @TOPDIR@/lib/pci \ @TOPDIR@/lib/pdump \ @TOPDIR@/lib/pipeline \
doc/guides/howto/img/packet_capture_framework.svg +48 -48
205 lines not on this page · raw message
doc/guides/howto/packet_capture_framework.rst +35 -32
125 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -43,6 +43,7 @@ Programmer's Guide ip_fragment_reassembly_lib generic_receive_offload_lib generic_segmentation_offload_lib + pcapng_lib pdump_lib multi_proc_support kernel_nic_interface
doc/guides/prog_guide/pcapng_lib.rst new file +24 -0
@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation. + +.. _pcapng_library: + +Packet Capture File Writer +========================== + +Pcapng is a library for creating files in Pcapng file format. +The Pcapng file format is the default capture file format for modern +network capture processing tools. It can be read by wireshark and tcpdump. + +Usage +----- + +Before the library can be used the function ``rte_pcapng_init`` +should be called once to initialize timestamp computation. + + +References +---------- +* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html + +* Project repository https://github.com/pcapng/pcapng/
doc/guides/prog_guide/pdump_lib.rst +21 -7
@@ -3,10 +3,10 @@ .. _pdump_library: -The librte_pdump Library -======================== +The Packet Capture Library +========================== -The ``librte_pdump`` library provides a framework for packet capturing in DPDK. +The DPDK ``pdump`` library provides a framework for packet capturing in DPDK. The library does the complete copy of the Rx and Tx mbufs to a new mempool and hence it slows down the performance of the applications, so it is recommended to use this library for debugging purposes.
@@ -23,11 +23,19 @@ or disable the packet capture, and to uninitialize it. * ``rte_pdump_enable()``: This API enables the packet capture on a given port and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf()`` + This API enables the packet capture on a given port and queue. + It also allows setting an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_enable_by_deviceid()``: This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. - Note: The filter option in the API is a place holder for future enhancements. + +* ``rte_pdump_enable_bpf_by_deviceid()`` + This API enables the packet capture on a given device id (``vdev name or pci address``) and queue. + It also allows seating an optional filter using DPDK BPF interpreter and + setting the captured packet length. * ``rte_pdump_disable()``: This API disables the packet capture on a given port and queue.
@@ -61,6 +69,12 @@ and enables the packet capture by registering the Ethernet RX and TX callbacks f and queue combinations. Then the primary process will mirror the packets to the new mempool and enqueue them to the rte_ring that secondary process have passed to these APIs. +The packet ring supports one of two formats. The default format enqueues copies of the original packets +into the rte_ring. If the ``RTE_PDUMP_FLAG_PCAPNG`` is set the mbuf data is extended with header and trailer +to match the format of Pcapng enhanced packet block. The enhanced packet block has meta-data such as the +timestamp, port and queue the packet was captured on. It is up to the application consuming the +packets from the ring to select the format desired. + The library APIs ``rte_pdump_disable()`` and ``rte_pdump_disable_by_deviceid()`` disables the packet capture. For the calls to these APIs from secondary process, the library creates the "pdump disable" request and sends the request to the primary process over the multi process channel. The primary process takes this request and
@@ -74,5 +88,5 @@ function. Use Case: Packet Capturing -------------------------- -The DPDK ``app/pdump`` tool is developed based on this library to capture packets in DPDK. -Users can use this as an example to develop their own packet capturing tools. +The DPDK ``app/dpdk-dumpcap`` utility uses this library +to capture packets in DPDK.
doc/guides/rel_notes/release_21_11.rst +10 -0
@@ -125,6 +125,16 @@ New Features * Added tests to validate packets hard expiry. * Added tests to verify tunnel header verification in IPsec inbound. +* **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 v12 11/12] doc: changes for new pcapng and dumpcap
Review
Quoted 7 lines
-----Original Message-----
From: dev <[email protected]> On Behalf Of Stephen Hemminger(quoted diff) +2
-0+The DPDK packet capture framework was introduced in DPDK v16.07 and +enhanced in 21.1.
need to edit the version
Quoted 1 line
+#. Launch the dpdk-dump as follows::
:s/dpdk-dump/dpdk-dumpcap
Quoted 1 line
+ Inspect packets captured in the file capture.pcap using a tool such as
:s/ capture.pcap/ capture.pcapng
doc/guides/prog_guide/pcapng_lib.rst line 2
Quoted 3 lines
doc/guides/prog_guide/pcapng_lib.rst new file +2
-0@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation.
need to edit the licence .
Review line 3
Quoted 1 line
(quoted diff) +1
-0+ It also allows seating an optional filter using DPDK BPF interpreter
:s/seating/setting
doc/guides/prog_guide/pcapng_lib.rst line 22
Quoted 1 line
doc/guides/prog_guide/pcapng_lib.rst new file +1
-0+* Draft RFC https://www.ietf.org/id/draft-tuexen-opsawg-pcapng-03.html
Need the link update
Re: [dpdk-dev] [PATCH v12 11/12] doc: changes for new pcapng and dumpcap
Quoted 5 lines · Reshma
doc/guides/prog_guide/pcapng_lib.rst line 2
Quoted 3 lines
doc/guides/prog_guide/pcapng_lib.rst new file +2
-0@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation.need to edit the licence
Do you want me to change date on the existing doc as well.
Re: [dpdk-dev] [PATCH v12 11/12] doc: changes for new pcapng and dumpcap
Quoted 7 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]> On Fri, 15 Oct 2021 16:42:19 +0000
"Pattan, Reshma" <[email protected]> wrote:
Quoted 7 lines
Quoted 5 lines
doc/guides/prog_guide/pcapng_lib.rst line 2
Quoted 3 lines
doc/guides/prog_guide/pcapng_lib.rst new file +2
-0@@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2016 Intel Corporation.need to edit the licence
Do you want me to change date on the existing doc as well.
Yes, looks good as in v14.
PATCH v12 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 v12 00/12] Packet capture framework update
In reply to Stephen HemmingerQuoted 131 lines · On Fri, 1 Oct 2021 09:26:53 -0700 Stephen Hemminger…
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 timestampsThere are no blocker items.
The following are worth noting:
* bogus checkpatch warnings
- the correct flag to open isO_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 bytmpfile()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().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 torte_bpf_dumpto handle more instructions make sure allbpf_testcases are decodedv9:
- incorporate suggested change to BPF XOR
- make autotest for pcapng more complete by reading the resulting file with libpcapv8:
- enable BPF tests in autotest
- add more BPF test strings
- userte_strscpyto satisfy checkpatch
- mergeMAINTAINERS(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 issuesv5:
- 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 FreeBSDv3:
- 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 cleanupv2:
fix formatting of packet blocks
fix the new packet capture statistics
fix crash when primary process exits
record start/end time
various whitespace/checkpatch warningsStephen 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 forbpf_convert
test: add a test for pcapng library
test: enable bpf autotest
doc: changes for new pcapng and dumpcap
MAINTAINERS: add entry for new packet capture featuresMAINTAINERS | 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 | 67 +- doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/pcapng_lib.rst | 24 + doc/guides/prog_guide/pdump_lib.rst | 28 +- doc/guides/rel_notes/release_21_11.rst | 10 + doc/guides/tools/dumpcap.rst | 86 ++ doc/guides/tools/index.rst | 1 + lib/bpf/bpf_convert.c | 575 ++++++++++++ lib/bpf/bpf_dump.c | 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 | 613 +++++++++++++ lib/pcapng/rte_pcapng.h | 194 ++++ lib/pcapng/version.map | 12 + lib/pdump/meson.build | 2 +- lib/pdump/rte_pdump.c | 427 ++++++--- lib/pdump/rte_pdump.h | 113 ++- lib/pdump/version.map | 8 + 33 files changed, 3733 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
Quoted 9 lines
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
Anything outstanding on this patchset? Would like this to make 20.11
The only failures in CI are false positives: ie bogus checkpatch, and ice RHEL7 build
Re: [dpdk-dev] [PATCH v12 00/12] Packet capture framework update
12/10/2021 04:31, Stephen Hemminger:
Quoted 7 lines
Quoted 2 lines · On Fri, 1 Oct 2021 09:26:53 -0700 Stephen Hemminger…
This patch set is a more complete version of the the enhanced packet capture support described last year.
Anything outstanding on this patchset? Would like this to make 20.11
Too late for 20.11, but we can try to merge it in 21.11 timeframe ;)
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?
Re: [dpdk-dev] [PATCH v12 00/12] Packet capture framework update
Quoted 8 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.