eBPF is the kernel-resident programmable engine that runs verified bytecode in response to kernel events; XDP (eXpress Data Path) is the hook point where eBPF programs see incoming packets at their earliest moment — right after the driver, before the kernel allocates an sk_buff. The combination gives you wire-rate packet processing without leaving the kernel and without writing a kernel module. This article covers where XDP sits in the receive path, the three modes (native, offloaded, generic), the program return codes, and the operational uses that show up in production: DDoS filtering, L4 load balancing (Katran-style), and per-packet observability.
How to verify
Inspect attached XDP programs and counters:
ip link show
bpftool prog list
bpftool prog show id <id>
bpftool map list
bpftool net show
ethtool -S eth0 | grep -i xdp
cat /sys/kernel/debug/tracing/trace_pipe
bpftool net show lists XDP attachments per interface and their mode. ethtool -S eth0 exposes per-NIC counters including rx_xdp_drop, rx_xdp_tx, rx_xdp_pass, rx_xdp_redirect — the four return codes a program emits.
What’s happening
The Linux receive path historically goes: NIC DMA → driver → sk_buff allocation → network stack → netfilter → routing → socket. Every step costs CPU; allocating the sk_buff is the most expensive single step. XDP runs before sk_buff allocation, on the raw packet buffer in driver memory.
An XDP program is an eBPF function that takes one argument — a struct xdp_md* describing the packet — and returns one of:
XDP_DROP— discard the packet immediately. CPU cost is a few hundred cycles. This is the DDoS-filter return code.XDP_PASS— let the kernel continue normal processing.XDP_TX— transmit the (possibly modified) packet back out the receiving interface.XDP_REDIRECT— send the packet to another interface or to a CPU map for further processing.XDP_ABORTED— error path, treated like DROP plus a trace event.
The XDP program can read and modify packet bytes within bounds the verifier proves safe. It cannot loop unbounded, dereference unchecked pointers, or call most kernel helpers — the eBPF verifier rejects programs that do.
Three modes:
- Native (driver) XDP — the driver calls into the XDP program directly, with the packet still in the NIC’s RX ring buffer. Maximum performance. Requires driver support: ixgbe, i40e, ice, mlx5, nfp, bnxt, virtio_net, veth all support it.
- Offloaded XDP — the program runs on the NIC itself, on Netronome SmartNICs. Truly zero CPU.
- Generic XDP — fallback for drivers without native support. Runs higher up the stack, after
sk_buffallocation. Useful for development; not for production performance.
Verify which mode you got with ip -d link show eth0 — output includes xdp prog id N mode (native|generic|driver-attached).
The procedure
-
Install bpftool and the eBPF toolchain.
apt install bpftool clang llvm libbpf-dev linux-tools-$(uname -r) -
Write a minimal “drop ICMPv4” XDP program. Save as
drop_icmp.c:#include <linux/bpf.h> #include <linux/if_ether.h> #include <linux/ip.h> #include <bpf/bpf_helpers.h> SEC("xdp") int drop_icmp(struct xdp_md *ctx) { void *data = (void *)(long)ctx->data; void *data_end = (void *)(long)ctx->data_end; struct ethhdr *eth = data; if ((void *)(eth + 1) > data_end) return XDP_PASS; if (eth->h_proto != __constant_htons(ETH_P_IP)) return XDP_PASS; struct iphdr *ip = (void *)(eth + 1); if ((void *)(ip + 1) > data_end) return XDP_PASS; if (ip->protocol == IPPROTO_ICMP) return XDP_DROP; return XDP_PASS; } char _license[] SEC("license") = "GPL"; -
Compile and load.
clang -O2 -g -target bpf -c drop_icmp.c -o drop_icmp.o ip link set dev eth0 xdpgeneric obj drop_icmp.o sec xdpUse
xdpdrvinstead ofxdpgenericfor native mode on a supporting NIC. -
Verify and observe.
ip -d link show eth0 | grep xdp bpftool prog show ping -c5 eth0 # from another host — should fail ethtool -S eth0 | grep xdp -
Detach the program.
ip link set dev eth0 xdpgeneric off -
For production use, drive XDP from a daemon that owns the BPF map. Cilium, Katran, Cloudflare’s L4Drop, and Facebook’s XDP-DDoS all follow this pattern: a userspace controller writes filter rules into a hash/array map, the XDP program reads from the map per-packet, and updating filters is a no-restart map update.
# Update a map entry from userspace bpftool map update id 42 key hex 0a 00 00 05 value hex 01That snippet sets
10.0.0.5to value1in the map, which the program could interpret as “block this source”.
Operational notes
- The verifier is strict. Every memory access must be provably in-bounds; loops must have provable bounds; pointer arithmetic must be aligned. Most “compile errors at load time” are verifier rejections — read the verifier output, not just the clang output.
- Generic XDP is significantly slower than native. On a 10G link with full PPS, generic might cap at 2-3 Mpps where native hits 10+ Mpps on the same hardware.
- Mode mixed with another XDP user breaks things. Only one XDP program can attach per netdev (the
xdphook). Tools like Cilium attach one program and dispatch to sub-programs viabpf_tail_call; if you load yours on top, the previous one is replaced and may break. - VLAN, tunneled, and encapsulated traffic complicates parsing. The XDP program sees the outer frame; if you need to filter inside a VXLAN, parse VXLAN explicitly. Be careful about packet bounds checks at every parse step.
- eBPF maps survive program reload when pinned to
/sys/fs/bpf/. State accumulated by a previous program version is available to the next without rebuilding — the pattern that makes hot updates possible.
In the engagements we run, XDP appears for two production cases: DDoS scrubbing at the edge (drop syn-flood / amplification before they hit the kernel) and L4 load balancing for ingress at scale. Both are deployed via Cilium or hand-rolled programs maintained in the same Ansible role library the managed-operations practice uses. For pure userspace alternatives (DPDK, VPP), see the DPDK overview; for kernel networking control inside containers, see the BPF + Cilium ecosystem.