BPF Trace


BPF Trace

High level tracing language

eBPF based

static and dynamic tracing

kernel and user namespace

Tracing

- Ability to interrupt flow of running program. at configurable location. 

- execution of code to obtain some data

- For visibility 

It affects performance. 

Perf sample all data and then you filter. it is different. 

Static Tracing

hard coded instrumentation points in code

Dynamic Tracing

Can instrument any software event. E.g. function call, return

live modification of instruction set

it needs symbol table

E.g. gdb

Probe Providers

They provides probe point. At probe point we can execute some code. 

1. BEGIN/END : built-in events. BEGIN to initialize data structure END to aggregate counters. 

2. hardware: processor level events

3. interval: built-in timers

4. kprobe / kretprobe : Kernel function tracing

5. profile: timed sampling

6. rawtracepoint: Kernel static trace point with raw argument

7. software: kernel software event

8. tracepoint: kernel static tracepoint 

9. uprobe / uretprobe : user-level function start / return

sudo bpftrace -l | wc -l

153620

Most of them are kernel level. static trace points. 

e.g. 1 openat syscall using tracepoint:syscall:sys_enter_openat

Here 

(1) "comm" is built-in variable having process name

(2) "args" is struct containing all tracepoint arguments

for more details about "args" struct, use below command

bpftrace -vl tracepoint:syscall:sys_enter_openat

To execute

sudo bpftrace -e 'tracepoint:syscall:sys_enter_openat {printf();}

sudo bpftrace <file name> 

It has buil-in histagram

in the command we can specify filter

/pid == 32184/

/comm == "<name of process>"/


Reference

github.com/bpftrace


 https://media.ccc.de/v/osmodevcon2024-204-introduction-to-xdp-ebpf-and-afxdp

eBPF, XDP and AF_XDP


1. NIC -> Kernel
2. Kernel process it layer by layer
3. Sub-systems tunnel, traffic shaping, IPSec, NAT, Packet filter
4. enqueued to socket receive queue. 
5. wake up user space process
6. copy data to user space process

eBPF, XDP and AF_XDP are Kernel Feature for high speed packet processing


 * [eBPF](https://ebpf.io/what-is-ebpf/) (extended Berkeley Packet Filter) is a kind of virtual machine that runs sandboxed programs inside the Linux kernel.

- TCP Dump uses BPF. 

- Not tied specifically to networking

- in kernel verifiers. No endless loop. no access to invalid memory. 

Instructions

- RFC by IETF defines instruction set

- VM + limited instruction set

- JIT

Program Types

1. XDP

2. socket filtering, kprobe, tc schedular (action, classifiers), performance events

3. tracing: function entry, function exit, modify return value

4. cgroups, light weight tunnels

5. TCP stream parsing ( kernel assisted TLS and KCM = kernel connection machine. IP multiplex)

$ bpftool prog

$ clang target bpf

eBPF maps

1. K-v store

2. hash, array, radix-tree, bloom filter

3. _per_cpu variants for multi-processor efficiency

Use cases

1. device driver for DPDK, VPP and related user plane frameworks

2. load balancer (e.g. dnsdist)

3. software switch (e.g. vSwitch)

4. DoS protection filter

5. Intrusion Detection Systems (e.g. Suricata)

6. eupf = eBPF + AF_XDP for accelerated 5G UPF

* [XDP](https://docs.cilium.io/en/latest/bpf/progtypes/#xdp) (eXpress Data Path) is a framework for eBPF that enables high-performance programmable packet processing in the Linux kernel

Kernel 4.18
XDP_DROP : DoS. Drop at line rate. similar to disconnect cable. 
XDP_PASS : Slow Path OR other NIC. use case like netfilter. 
XDP_TX : 
XDP_REDIRECT

Register eBPF program to XDP hook

* [AF_XDP](https://www.kernel.org/doc/html/next/networking/af_xdp.html) is an *address family* that is optimized for high-performance packet processing. It allows in-kernel XDP eBPF programs to efficiently pass packets to userspace via memory-mapped ring buffers.

XDP Socket
Operates on raw packet
Memory mapped Rx/Tx Ring. so zero copy. It is at network interface card interrupt receive level. receive queue of network card. Morden NIC has many receive queues. one for each CPU. based on connection.  it can edit the packet.no read/write. as no copy between user space and kernel space. 
Single socket for RX+Tx. Not possible
descriptor points to memory 

eBPF program will decide go to AF_XDP or not. 

AF_XDP can also generate pcap. 

Ref:
github.com/xdp-project/xdp-tools
github.com/xdp-project/xdp-tutorial
qmonnet.github.io/whirl-offload/2021/09/23/bpftool-features-thread
https://media.ccc.de/v/osmodevcon2024-204-introduction-to-xdp-ebpf-and-afxdp

XDP is close to DPDK. But DPDK is faster.