OpenWRT router and switch with colored cables on a wooden table beside a tabby cat
eBPF in OpenWRT
William  

OpenWrt eBPF: Check Support Before You Build

Start with the kernel and build configuration: openwrt ebpf works only when the router’s kernel, build settings, and matching toolchain support the features your program needs. A compiled eBPF object may still fail to load, and a loaded program may still lack a usable network attachment point. Check those limits before building custom firmware or installing tools.

Last updated: 2026-11-11

What eBPF can and cannot do on OpenWrt

Use tcpdump when you need a standard packet capture. Use eBPF when you need kernel-side counters, filters, or custom events. For a packet capture on a small OpenWrt router, that split keeps the job honest: tcpdump gives Wireshark a standard capture, while eBPF trades full packet visibility for lower overhead and targeted telemetry.

I build and flash custom firmware to run eBPF packet capture on small routers without guessing. The work comes down to kernel configuration, compiling against the right target, and checking support with bpftool.

An eBPF program also has to pass the kernel verifier before it can run. Then it needs a supported attachment point, such as a traffic-control hook or XDP. Those are separate checks. A successful compile proves neither.

For a standard capture file, tcpdump can write packet data with its -w flag. Use eBPF for selected events or counters when saving every packet would waste router memory and storage.

Check kernel support before changing the build

Check the running kernel configuration first. If the router exposes it through /proc/config.gz, look for the options your program needs:

zcat /proc/config.gz | grep -E 'CONFIG_BPF|CONFIG_NET_CLS_BPF|CONFIG_NET_ACT_BPF'

If that file is missing, inspect the configuration used to build the firmware. A missing file does not prove that BPF support is disabled.

Install bpftool if the target package is available, then inspect the running system:

bpftool prog list
bpftool map list
uname -a
dmesg | tail -40

bpftool prog list shows loaded programs. Its output starts with each program’s ID and type, followed by attributes the kernel supports. An empty list only means no programs are loaded.

On Linux 5.1 and later, BPF program statistics can include total run time and the number of runs. On Linux 5.8 and later, bpftool can also show processes holding open file descriptors for BPF programs. Do not assume those details exist on your router; check the output and kernel version.

If loading fails, read the kernel log before changing configuration. The verifier’s message often names the rejected instruction or missing capability. The loader’s final EPERM alone does not tell you which one.

Enable CONFIG_BPF and the required kernel options

Enable CONFIG_BPF and CONFIG_BPF_SYSCALL for a user-space loader that loads programs and manages maps. CONFIG_BPF_SYSCALL enables the bpf() system call used to manipulate BPF programs and maps through file descriptors.

Add options for the attachment type you intend to use:

Kernel optionUse it for
CONFIG_BPFCore BPF support
CONFIG_BPF_SYSCALLLoading and managing BPF programs and maps from user space
CONFIG_NET_CLS_BPFBPF-based traffic classifier
CONFIG_NET_ACT_BPFBPF-based traffic-control action

Treat these as separate gates. CONFIG_NET_CLS_BPF and CONFIG_NET_ACT_BPF support the traffic-control path; they do not make XDP work on a driver that lacks XDP support.

Open the OpenWrt build configuration and enable only what the program needs. Then rebuild the kernel and matching packages. A generic config copied from another router is a poor shortcut: target kernels differ, and the missing option will still be missing.

For the details on the kernel checks and attachment paths, see this guide to eBPF on OpenWrt.

Build with the matching Clang, LLVM, and kernel headers

Build for the router’s architecture, not the machine doing the compilation. The target architecture, byte order, kernel headers, and C library must match the firmware. A host-built object can compile cleanly and still be useless on the router.

Use the OpenWrt SDK or build tree for the target. Its staging directory contains target headers and libraries. Host headers describe the host; mixing them into a cross-build creates errors that often look like unrelated compiler failures.

Check the actual compiler command before changing flags. It should use the intended BPF target, architecture, include paths, and OpenWrt staging directory. Do not copy an include path from another target and hope the compiler sorts it out.

For a full OpenWrt build, allow about 3-4 GB of available disk space on ESPRESSObin. The build needs room for source, toolchains, staging files, and image output. Keep the SDK and its staging directory together so the compiler does not quietly pull in headers from somewhere else.

Use libbpf for a deployed loader when you need direct control over maps and events. BCC can be useful for experiments, but its runtime and language dependencies are a poor fit for many small routers.

Load and debug an eBPF program on the router

Start with a small program and watch what the loader actually reports. Then inspect loaded programs and kernel messages:

bpftool prog list
dmesg | tail -40
strace -f -e bpf,prctl,capset,execve ./loader

strace shows the system calls around loading. A failed bpf() call points to the load path; prctl and capset calls can expose sandbox or capability restrictions. The verifier message in the kernel log helps separate bad program logic from missing kernel support.

For temporary tracing with bpf_trace_printk, read from /sys/kernel/tracing/trace_pipe. It streams trace output and blocks until new data arrives. That is useful while debugging, but it is not a packet-capture format or a logging system.

Avoid sending full payloads through maps or perf buffers unless you have a clear need. Payloads use memory and require user-space processing. Keep events short, count what matters in the kernel, and send only the details your analysis needs.

Use XDP tools only when the kernel and device support XDP

The OpenWrt xdp-tools package does not add XDP support to a kernel or driver that lacks it. Check the running kernel, package availability, and interface support before building or installing the tools.

XDP can run in a driver’s native mode or in generic mode, depending on kernel and driver support. Native attachment can process packets earlier; generic mode works through a later software path and costs more. Check the attachment result instead of assuming the requested mode took effect.

The package is useful when you need XDP-specific loading or inspection tools. For a simple traffic-control program, it may add nothing you need. Start with the attachment type your task requires, then install the tool that manages it.

Capture packets with eBPF and libpcap

A couple plugs blue and yellow cables into a white wireless router on a table

Use tcpdump if the job is to produce a pcap file for Wireshark. Its -w flag saves packet data for later analysis. For a practical tcpdump capture workflow, keep the capture on attached storage or send it to another machine; small routers run out of room quickly.

Use eBPF when the question is narrower: count matching traffic, record selected events, or track socket activity. A BPF filter can narrow what a capture tool sees. An XDP program can inspect or act on packets earlier, if the driver and kernel support the required mode.

“pcap eBPF” often mixes two different jobs. libpcap and tcpdump capture packets into a familiar format; eBPF runs selected logic in the kernel. Choose based on whether you need packet contents for later analysis or a small stream of targeted data.

Use qosify for OpenWrt traffic shaping

Use qosify when the goal is traffic shaping on OpenWrt. It is the practical path for classifying and prioritizing traffic without writing a custom eBPF program and maintaining its loader.

eBPF makes sense when you need behavior that the available QoS rules cannot express, or when you need custom kernel-side telemetry. Otherwise, custom code adds build, verifier, and deployment work without improving the result. Keep traffic shaping on the supported OpenWrt path until you can name the missing behavior.

FAQ

Can eBPF replace tcpdump on OpenWrt?

No, not when you need a full packet capture for later inspection. Run tcpdump with -w to preserve packet data for Wireshark. Use eBPF when counters, filters, or selected events answer the question.

Can an XDP program see traffic on every OpenWrt interface?

No. The interface driver decides much of this, while the kernel and attachment mode decide the rest. Check the interface support and verify the attachment on the running router before treating XDP as available.

Why do eBPF counters disagree with tcpdump?

They may count at different points in the packet path. Check the interface, capture filter, and eBPF attachment point together; one mismatch is enough to produce different totals.

Should I store captured payloads in a BPF map?

Only if the program needs payload data later. Maps consume limited router memory, and copying full packets increases user-space work. Store short events or counters when those answer the question.

Related on this blog