Small network router with two antennas and four colorful Ethernet cables on a wooden table
eBPF in OpenWRT
William  

How to Make eBPF Work Reliably on OpenWrt

OpenWrt eBPF is practical for tracing, policy, and selected packet paths, but it is not a universal forwarding shortcut. Start by matching the router's kernel configuration, headers, LLVM target, architecture, and hook semantics. Then read the verifier output, kernel logs, and interface counters before changing configuration. A program that works on x86 can fail on OpenWrt because the target kernel lacks a helper, the MIPS build uses different byte order, or the driver cannot run the selected XDP mode.

Last updated: 2026-11-10

What is openwrt ebpf good for?

Use eBPF on OpenWrt when you need a narrow decision close to the packet path. Tracing, packet classification, filtering, counters, and selected redirects are sensible jobs.

The small-router limit arrives quickly. Vendor kernels may lack newer helpers, JIT support, map types, or tracing hooks. Flash and memory also matter. A program that loads on a server may be a poor fit for a router with a small package set and a slow CPU.

I split the work into four categories:

JobGood eBPF fitMain limit
ObservabilityTrace packets, sockets, interfaces, and selected kernel eventsTooling and trace support may be missing
PolicyDrop, mark, classify, or redirect trafficHook semantics decide what the program can see
Packet processingXDP or TC packet edits and forwardingDriver support and checksum work matter
RoutingConsult the kernel FIB or use a private mapeBPF does not maintain kernel routes

For tracing, eBPF can show what a packet or process did without adding another proxy to the path. For policy, it can reject traffic before it reaches a longer rule chain. For forwarding, it can select an interface directly or ask the kernel FIB for a route.

That does not make it a replacement for OpenWrt's normal firewall, routing daemon, or queueing system. Keep the ordinary path for work it already handles correctly.

If you need the first checks, start with this OpenWrt eBPF kernel guide. It covers the platform checks that people usually skip.

The OpenWrt kernel and headers must match

Two network router boards with Ethernet ports and cables on a wooden desk

The bpf-headers package supplies eBPF kernel headers for the OpenWrt build. Its package definition names it bpf-headers, marks it BUILDONLY:=1, and marks it HIDDEN:=1.

That tells you what the package is for. It supports the build process. It is not a normal runtime utility you install on the router and then expect to provide the complete eBPF toolchain.

The package's Makefile matters because kernel headers are not interchangeable decoration. BPF programs include definitions for helper prototypes, map types, context structures, and kernel-facing data. Those definitions must match the kernel you will load the program into.

Check the build tree and generated include paths instead of guessing:

find package/feeds -iname '*bpf*' -o -iname '*headers*'
grep -R "define Package/bpf-headers" package/feeds package
grep -R "bpf-headers" package/feeds package

The output tells you which feed owns the package and which Makefile defines it. If the package is missing, check feeds before editing source files. A stale feed index is not a kernel problem.

For a program compiled outside the OpenWrt build system, use the target kernel's generated headers where possible. The kernel's BPF header-generation tools can produce a smaller include set for a program, but the result still belongs to a particular kernel build.

A typical source-first build sequence looks like this:

make menuconfig
make package/bpf-headers/compile V=s
find staging_dir -path '*bpf*' -o -path '*include/uapi/linux/bpf.h*'

The exact staging path depends on the target and build tree. Check the output. Do not copy an include path from a different OpenWrt release and hope the compiler agrees.

Clang compiles BPF source for the BPF target. It does not compile a router program as MIPS machine code. The BPF instruction set is the program's load-time format; the kernel's verifier and optional JIT handle execution on the target.

A minimal compile command might look like this:

clang -O2 -g -target bpf -c trace.c -o trace.o

That command only proves that Clang can produce a BPF object. It does not prove that the OpenWrt kernel will accept it. The loader still has to resolve helper availability, map support, program type, license rules, and verifier constraints.

For a full build workflow, use the site's OpenWrt eBPF toolchain guide. The important part is still the match between the build inputs and the running kernel.

Kernel configuration decides what exists

Start with the kernel configuration. OpenWrt needs CONFIG_BPF, CONFIG_BPF_SYSCALL, CONFIG_NET_CLS_BPF, and CONFIG_NET_ACT_BPF enabled for common eBPF and TC workflows.

The CONFIG_BPF item is a Boolean option defined in kernel/bpf/Kconfig. It has no listed dependency in the kernel configuration metadata. That does not mean every eBPF feature appears automatically. Other options control syscalls, hooks, maps, tracing, and networking actions.

Check the configuration used by the running router:

zcat /proc/config.gz 2>/dev/null | grep -E 'CONFIG_(BPF|XDP|NET_CLS_BPF|NET_ACT_BPF|DEBUG_INFO_BTF)'
cat /boot/config-$(uname -r) 2>/dev/null | grep -E 'CONFIG_(BPF|XDP|NET_CLS_BPF|NET_ACT_BPF|DEBUG_INFO_BTF)'

If neither file exists, inspect the OpenWrt build output or rebuild with the needed configuration visible. A missing config file is not proof that the feature is absent.

The options that usually matter are:

  • CONFIG_BPF for the BPF subsystem.
  • CONFIG_BPF_SYSCALL for loading programs and managing maps from userspace.
  • CONFIG_NET_CLS_BPF for BPF traffic classification.
  • CONFIG_NET_ACT_BPF for BPF actions in the traffic-control path.
  • CONFIG_XDP_SOCKETS for AF_XDP socket support.
  • Verifier and map options required by the program type.
  • Tracing options required by the chosen tracepoint, kprobe, or raw tracepoint.
  • Networking options required by the selected XDP, TC, or socket path.

CONFIG_XDP_SOCKETS depends on CONFIG_BPF_SYSCALL. If the syscall option is absent, XDP sockets cannot be enabled as an isolated checkbox.

Do not treat a missing helper as a compiler error. The compiler can accept a helper declaration while the target kernel rejects the loaded program. libbpf, bpftool, or the loader will show the real failure during load.

The kernel's BPF configuration is the right place to check feature behavior. A random build recipe usually describes a different kernel.

What is openwrt ebpf forwarding actually doing?

Black network router on a wooden table with colorful Ethernet cables plugged in

OpenWrt eBPF forwarding is a hook-level operation. It does not create a second invisible kernel router with all the same behavior.

The path depends on where the program runs:

  • XDP runs at the receive side, before the normal socket buffer path in supported modes.
  • TC ingress runs after the packet has entered the kernel's packet representation.
  • TC egress runs as the packet leaves an interface.
  • Socket programs operate around socket delivery and do not replace interface forwarding.
  • Netfilter-adjacent programs interact with a path that still has different ordering and state from XDP.

An XDP program can return XDP_PASS, XDP_DROP, XDP_TX, or XDP_REDIRECT. XDP_PASS sends the frame into the normal receive path. A redirect sends it toward another target without asking the ordinary forwarding pipeline to make the same decision.

That distinction explains many bad tests. You run ip route get, see the expected route, and assume the redirected packet must follow it. It may never have reached the FIB lookup.

TC gives you more packet context, but the packet is further into the stack. That can make integration easier while reducing the benefit of running before socket buffer allocation. The right hook depends on the job, not on a claim that XDP is always faster.

Architecture, MIPS, and byte order are first-class constraints

Compile for BPF, then test on the actual router. Do not confuse the BPF target with the host architecture.

MIPS routers add several traps:

  • The kernel may be 32-bit.
  • The CPU may use a different byte order than your development host.
  • The BPF JIT may be missing or disabled.
  • The driver may support only a limited XDP mode.
  • Atomic operations and alignment assumptions can differ.
  • A helper or map type available on x86 may not be available in the target build.

Use Clang's BPF target for the program object:

clang -O2 -g -target bpf -c forward.c -o forward.o
file forward.o

Then inspect the target kernel instead of inferring it from the build machine:

uname -a
opkg list-installed | grep -E 'bpftool|libbpf|clang|llvm'
cat /proc/cpuinfo

The output tells you what is running, which packages are installed, and what architecture the kernel reports. It does not prove that a network driver supports native XDP.

Byte order deserves its own check. Packet fields are in network byte order. BPF map keys and values are memory layouts chosen by your program. If you compare a field without converting it consistently, the program can compile, load, and make the wrong decision.

A program that reads IPv4 addresses from an LPM trie also needs a correct key layout. BPF_MAP_TYPE_LPM_TRIE provides longest-prefix matching for stored prefixes, and IPv4 address data has a length of 4 bytes. The prefix length and address bytes must be populated in the format the map expects.

Routing table lookup with bpf_fib_lookup

Black network router with connected Ethernet cables on a wooden desk beside a potted plant

Use bpf_fib_lookup when you want the kernel's forwarding information without sending the packet through a later slow-path decision.

The program fills a struct bpf_fib_lookup with the packet's address family and relevant addresses. The helper performs a FIB lookup and returns a result code. On a successful lookup, the structure can provide the output interface and next-hop information needed for a later redirect or packet rewrite.

That is useful, but it is not a complete router. The helper does not replace:

  • Route policy rules.
  • Userspace route management.
  • Neighbor discovery and address resolution.
  • Interface state.
  • VLAN or bridge configuration.
  • Source address selection.
  • NAT and connection tracking.
  • The return path.

Treat the return code as a decision, not as a Boolean. A miss, blackhole, unreachable route, unsupported lookup, or link problem needs a different response. Log the code during development and compare it with the kernel's route state.

A simple diagnostic pattern is:

ip route get 192.0.2.1
ip rule show
ip neigh show
ip -br link

Those commands describe the kernel's state from userspace. They do not prove that your eBPF program called the helper, used the same namespace, or accepted the result.

If the program uses a private map instead, you own synchronization. BPF_MAP_TYPE_LPM_TRIE can match prefixes, but it does not update itself when ip route changes. A route daemon or controller must listen for route and link updates and refresh the map.

A private forwarding table is justified when the kernel FIB cannot express the decision. Otherwise, it creates a stale second routing table for no good reason.

Redirects follow hook-specific rules

bpf_redirect selects a target interface or redirect action. bpf_redirect_map obtains the target from a map. The program still has to return the correct action for the hook where it runs.

For XDP, XDP_REDIRECT is the action used to redirect ingress frames to other XDP-enabled network devices. The redirect helpers store the target and related metadata in a per-CPU struct bpf_redirect_info.

When the program returns the XDP_REDIRECT code, the driver calls xdp_do_redirect(). That function places the frame into a map-specific bulk queue. The driver then completes the redirect path.

For BPF_MAP_TYPE_DEVMAP and BPF_MAP_TYPE_DEVMAP_HASH, the map contains references to network devices for forwarding packets through other ports. This is useful for interface selection, but the target still needs compatible XDP support.

bpf_redirect_map() returns XDP_REDIRECT on success. If the map lookup fails, it returns the value of the two lower bits of the flags argument. Check the map entry before blaming the interface.

The direct helper has different timing. bpf_redirect() stores the target and uses an action code, so the redirection happens after the eBPF program returns. That can be more efficient, but it still depends on the hook and driver completing the operation.

Debug a redirect in this order:

bpftool net show
ip -br link
ip -s link show dev lan0
ip -s link show dev lan1
tcpdump -ni lan0
tcpdump -ni lan1

Replace the interface names with the real ones. bpftool net show lists BPF program attachments in the kernel networking subsystem. Interface counters tell you whether packets reached the device. Packet capture tells you whether they carried the expected headers.

If the program returns a redirect action but the target counter stays still, inspect the map, target interface, driver support, and namespace. Do not add more route entries. The packet may never have reached the routing table.

OpenWrt eBPF needs measurable proof

A man plugs a green Ethernet cable into a black router on a shelf

Start with the load operation and save the complete error. Verifier output is not noise. It tells you which instruction, pointer type, bounds check, helper, or map operation failed.

Use bpftool where the target package provides it:

bpftool prog show
bpftool map show
bpftool net show
bpftool feature probe

The commands reveal loaded programs, maps, network attachments, and supported features. If bpftool feature probe is unavailable, the package may be too old, incomplete, or built without the expected support.

Then read the kernel log:

dmesg -T | tail -50
logread | tail -50

OpenWrt commonly exposes messages through logread, while the kernel ring buffer remains useful for verifier and driver messages. Check both. One log path is not a universal law.

If a loader fails before the program reaches the kernel, use strace on the loader:

strace -f -o /tmp/ebpf-load.strace your-loader arguments
grep -E 'ENOENT|EPERM|EINVAL|bpf|/sys/fs/bpf' /tmp/ebpf-load.strace

ENOENT often points to a missing file, mount, or package. EPERM points toward permissions, capabilities, or a restricted environment. EINVAL usually means the kernel rejected the requested object or attribute. Read the surrounding calls before choosing a fix.

Check the BPF filesystem if the loader expects pinned objects:

mount | grep bpf
ls -la /sys/fs/bpf

A missing mount namespace or pinned path can look like a program failure. The object may have loaded and then become unreachable from the process that expected to find it.

Finally, compare packet counters and captures. A program counter increasing proves execution. It does not prove forwarding. The receiving interface counter and a capture on that interface are the proof.

einat-ebpf shows the cost of forwarding projects

einat-ebpf is a useful case study in eBPF-based translation and forwarding. It also shows why project documentation cannot replace a target compatibility check.

The project has assumptions about kernel support, hook type, helpers, map behavior, interface setup, and userspace control. The OpenWrt guide for the project calls out pitfalls on OpenWrt. Read those constraints before trying to package it for a router.

Check the target in layers:

  1. Confirm the kernel configuration.
  2. Confirm the required helpers and map types.
  3. Confirm the program's hook can run on the target driver.
  4. Confirm the userspace controller can open the required BPF objects.
  5. Confirm the return path, checksums, and neighbor behavior.
  6. Test rollback before attaching it to the main gateway.

Do not start with the forwarding daemon. Load the smallest program, inspect its attachment, and pass one controlled flow. Then add translation and return-path handling.

Translation is where toy forwarding code becomes a system. You need address and port rewrites, checksum updates, flow state, timeouts, and reverse lookup. If the project expects conntrack behavior that your hook bypasses, the first packet may work while the session fails later.

Keep einat-ebpf on a lab router until the exact kernel and driver combination is reproducible. A main gateway is a poor place to discover that one helper was disabled.

qosify, eBPF, and CAKE have separate jobs

qosify uses eBPF-related classification to identify or mark traffic, while CAKE handles queue management. Those are different jobs.

Classification answers: “What should this packet be called or marked?”

CAKE answers: “How should packets in each class be queued, shaped, scheduled, and dropped?”

If classification fails, inspect the eBPF program, attachment, map contents, marks, and packet direction. If classification works but latency or fairness remains poor, inspect CAKE configuration, interface placement, shaping rate, and the actual bottleneck.

Do not blame CAKE for a missing mark. Do not blame eBPF for a queue that is configured on the wrong interface. Check the output at the boundary between the components.

A useful workflow is:

tc -s filter show dev wan ingress
tc -s qdisc show dev wan
ip -s link show dev wan
tcpdump -ni wan -vv

The traffic-control counters show whether the classifier runs. The qdisc counters show whether CAKE receives and processes traffic. The interface counters show whether the test is crossing the interface you think it is.

The same split applies to OpenWrt firewall integration. An eBPF classifier can mark traffic, but it does not automatically create the firewall rule, route, NAT state, or queue policy that consumes the mark.

Fix package dependency failures before touching eBPF

The error required dependency package uci-firewall is not available in any repository is a package feed or package metadata problem. It is not proof that eBPF failed.

Check the feeds and package indexes first:

opkg update
opkg list | grep -i uci-firewall
grep -R "uci-firewall" /etc/opkg /etc/opkg/distfeeds.conf

If the package is absent, compare the package's OpenWrt release, target, and feed configuration. A package built for another release can name dependencies that your configured repositories do not provide.

The fix is usually one of these:

  • Use the package feed matching the running release.
  • Rebuild the package against the same OpenWrt tree.
  • Remove a stale dependency from a local package Makefile.
  • Install the package that replaced the old dependency in that release.
  • Stop using the package if its dependency model assumes a different firewall stack.

Do not force-install a package with --force-depends on the gateway. That turns a clear dependency error into an untracked runtime failure.

A reproducible build and load checklist

Use this order. It keeps toolchain errors separate from hook errors.

  1. Identify the target.
ubus call system board
uname -a
cat /proc/cpuinfo

Record the target architecture, kernel release, and board profile.

  1. Check kernel configuration.
zcat /proc/config.gz 2>/dev/null | grep -E 'CONFIG_(BPF|BPF_SYSCALL|NET_CLS_BPF|NET_ACT_BPF|XDP_SOCKETS)'

If the options are absent, rebuild the kernel. A userspace package cannot add a disabled kernel subsystem.

  1. Build matching headers.
make package/bpf-headers/compile V=s

Use the headers from the same OpenWrt build inputs as the target kernel.

  1. Compile for BPF.
clang -O2 -g -target bpf -c program.c -o program.o

Check the object and compiler output. Do not use a host-native target by accident.

  1. Load the smallest program first.

Load a trace or pass-through program before adding redirects, maps, or packet rewrites. Save the verifier output.

  1. Inspect the attachment.
bpftool prog show
bpftool net show
bpftool map show

Confirm the program type, interface, hook, and map IDs.

  1. Test one controlled packet flow.

Use one source, one destination, and one interface pair. Capture both sides and inspect counters.

  1. Add forwarding logic.

Decide whether the program uses bpf_fib_lookup, bpf_redirect, or a private map. Do not mix them until each path works alone.

  1. Test failure and rollback.

Keep a shell open on the router. Remove the program, unpin maps, and restore ordinary routing before testing the next change.

  1. Record the working build.

Save the kernel config, target profile, compiler version, source revision, map layout, and attach command. “It worked once” is not a deployment method.

The OpenWrt eBPF capture guide is useful when the program loads but the packet path remains unclear.

Who should use eBPF on OpenWrt?

Use it if you need targeted tracing, custom classification, or a controlled packet path that the normal firewall and routing tools cannot express cleanly.

Skip it for ordinary home routing. OpenWrt's normal firewall, routes, conntrack, and CAKE setup are easier to inspect and maintain. Adding eBPF because it sounds faster creates a second control plane before it solves a real problem.

Use a dedicated lab router for development. The main gateway should carry the stable path while you load, break, and reload programs elsewhere. That split is less exciting than a live outage and much easier to support.

FAQ

Does eBPF replace the OpenWrt routing table?

No. An eBPF program can pass a packet to the normal stack, consult the FIB with bpf_fib_lookup, or redirect the packet itself. It does not add or remove kernel routes. Route management remains a userspace or routing-daemon job.

Why does a program compile but fail to load?

Compilation checks BPF syntax and object generation. Loading checks the target kernel's verifier, helpers, map types, program type, permissions, and configuration. Read the complete verifier or libbpf error, then check dmesg and the target kernel config.

Can XDP forward packets on every OpenWrt interface?

No. XDP behavior depends on the network driver and the mode available on that interface. A target may support generic XDP while lacking native support, or it may not support the required redirect path at all. Check the attachment result and interface counters.

What does a stale private routing map look like?

The kernel route output looks correct while the eBPF map still contains an old prefix or next hop. Dump the map, compare it with ip route and ip rule, and check whether the controller receives route and link updates. A private map does not follow kernel routing changes by itself.

Should qosify or CAKE be blamed first when traffic is unclassified?

Check the classifier counters and packet marks first. If the classifier never runs, fix the eBPF attachment or map logic. If marks are present but queue behavior is wrong, inspect the CAKE qdisc and the interface where it is attached.

Related: How eBPF Steers Traffic Without Proxy Hops