What Is eBPF? Programs, Maps and the Verifier
eBPF runs small verified programs inside the Linux kernel. Learn the three parts that matter, programs, maps and the verifier, with a first complete example.

Most programs that watch or shape network traffic on Linux live outside the kernel and pay for it: every packet crosses into user space, gets looked at, and crosses back. eBPF turns that around by letting you load a small program into the kernel itself, where it runs at a chosen point in the packet path with no round trip. The kernel will only accept the program after it proves the program cannot crash the machine. This post explains the three ideas that make that work, programs, maps and the verifier, using Linux 6.12 and libbpf 1.5. It is the entry point to the cluster, so every term is defined before it is used, and a complete minimal program follows.
What eBPF is
eBPF stands for extended Berkeley Packet Filter, a name that is now mostly historical. The original BPF was a tiny virtual machine for filtering packets in tools like tcpdump. The extended version has more registers, 64-bit arithmetic, function calls, shared data structures and many more places to attach, and it is used for networking, tracing, security and scheduling. The kernel documentation calls it a general purpose execution engine, in its BPF documentation.
A useful way to think of it: eBPF is to the kernel what JavaScript is to a browser. It is a sandboxed way to run user-supplied logic inside a privileged host, with a checker that decides what the logic is allowed to do.
You write a program in a restricted dialect of C, compile it with clang to eBPF bytecode, and hand the bytecode to the kernel through the bpf(2) system call. The kernel verifies it, translates it into native machine code with a just-in-time compiler, and attaches it to a hook.
The three parts
Programs
A program is a function that the kernel calls when something happens at its hook. The hook is decided by the program type. Networking types include XDP, which sees packets as they come off the network driver, SCHED_CLS, which runs in traffic control, SOCKET_FILTER, which filters what a socket receives, and SOCK_OPS, which sees TCP events. Each type defines what context the program gets and what return values mean. An XDP program returns a verdict such as pass or drop; a tracing program returns nothing important.
Programs are small by design, and they cannot call arbitrary kernel functions. They can call helpers, a fixed list of functions the kernel exports for the program type, such as bpf_map_lookup_elem or bpf_ktime_get_ns. The bpf-helpers(7) man page lists them.
Maps
A program has no persistent memory of its own: its stack is 512 bytes and vanishes when it returns. State lives in maps, kernel-resident key-value stores that programs and user-space processes both access. Common types are:
BPF_MAP_TYPE_ARRAY: fixed-size array indexed by an integer.BPF_MAP_TYPE_HASH: a hash table with keys of any fixed size.BPF_MAP_TYPE_PERCPU_ARRAY: one copy per CPU, which avoids contention.BPF_MAP_TYPE_RINGBUF: a ring buffer for streaming events to user space.
A map is how the kernel side counts packets and how the user-space side reads the count. Maps outlive any single program run, and they can be shared between several programs.
The verifier
Before a program runs, the verifier walks every possible execution path and checks that it is safe. It rejects a program that might read outside a packet, dereference a null pointer, loop forever, use an uninitialized register or call a helper with the wrong argument types. The verifier is why eBPF can run inside the kernel without the usual risk of kernel modules: a module can corrupt memory, while a verified program, in principle, cannot.
The price is that the code must be written so the verifier can follow it. Loops need provable bounds (bounded loops have been accepted since Linux 5.3), and every pointer from a map lookup must be checked for null before use.
A first complete program
The program below attaches at the XDP hook, counts every packet in an array map, and lets traffic continue. It is the smallest example that contains all three parts.
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, __u64);
} pkt_count SEC(".maps");
SEC("xdp")
int count_all(struct xdp_md *ctx)
{
__u32 key = 0;
__u64 *value = bpf_map_lookup_elem(&pkt_count, &key);
if (value)
__sync_fetch_and_add(value, 1);
return XDP_PASS;
}
char LICENSE[] SEC("license") = "GPL";The map definition uses libbpf's BTF-style syntax, where the macros record the type, size and key and value types in the object file. SEC("xdp") places the function in an ELF section that tells the loader which program type it is. The if (value) check is the one the verifier insists on, since a lookup can return null. __sync_fetch_and_add is an atomic add, which matters because the same program can run on several CPUs at once. The LICENSE string is required to use GPL-only helpers.
Compile it and attach it to an interface with iproute2:
clang -O2 -g -target bpf -c count_all.bpf.c -o count_all.bpf.o
ip link set dev eth0 xdp obj count_all.bpf.o sec xdpRead the map from user space with bpftool, after some traffic has passed:
bpftool map dump name pkt_countkey: 00 00 00 00 value: 2a 00 00 00 00 00 00 00
Found 1 elementThe value bytes are little-endian, so 2a is 42 packets. The number will differ on your machine; it counts whatever arrives on the interface.
What the verifier does to a mistake
Remove the null check and the load fails. The program
__u64 *value = bpf_map_lookup_elem(&pkt_count, &key);
*value += 1;is rejected with a message that names the problem in the verifier's own vocabulary:
R0 invalid mem access 'map_value_or_null'R0 is the register that holds the return value of the helper. map_value_or_null is the verifier's type for "pointer to the map value, or null". The error is telling you that you used it without proving it is not null. The fix is the if (value) guard above.
How programs, maps and the verifier fit together
The flow from source to running code is short: clang compiles C to bytecode, a loader (here iproute2, in your own tools libbpf) calls bpf(2) to create the maps and load the program, the verifier checks it, the JIT compiles it, and the kernel attaches it. After that the program runs on every event at its hook, and the maps are the shared surface to the rest of the system.
When not to use eBPF
You need arbitrary kernel access. A verified program has a narrow set of helpers, and a kernel module is the tool if you need more, with the safety cost that implies.
The logic is complex and needs unbounded loops or large state. The 512-byte stack and the verifier's complexity limits push such logic to user space.
The target kernel is old. XDP needs 4.8, ring buffers need 5.8, and many helpers are newer still, so check the version your fleet runs.
You need portability to other operating systems. eBPF on Windows exists in a different form, and the programs are not interchangeable.
For per-packet filtering, counting, steering and tracing on Linux, it is usually the right tool because it keeps the work in the kernel without the maintenance burden of a module.

