Skip to content

What is eBPF

eBPF lets you run small programs inside the Linux kernel, at points you choose, without changing the kernel source or loading a kernel module. This page explains the pieces that every eBPF tool is made of and where gobpfgo fits among them. The next two pages install the tools and run a first program.

An eBPF program is a function written in a restricted subset of C. clang compiles it to BPF bytecode, the instruction set of a small virtual machine defined by the kernel, with eleven 64-bit registers and a 512-byte stack. The compiled program is stored in an ELF object file, by convention named *.bpf.o, together with the definitions of the maps it uses and a description of its types.

The program runs in the kernel. It does not run as a process: the kernel calls it when an event happens, on the CPU and in the context of that event, such as the thread making a system call or the CPU receiving a packet. The program must always finish, so every loop needs a bound the kernel can check before the program runs, and it reaches the rest of the kernel only through helpers, a fixed set of kernel functions that BPF programs may call, and kfuncs, kernel functions exported to BPF on newer kernels.

Each program has a program type, such as tracepoint, kprobe or XDP. The type decides which events the program can attach to, what its argument (the context) points to, and which helpers it may call. In C, you choose the type through the name of the ELF section that holds the function, set with the SEC() macro, for example SEC("tracepoint/syscalls/sys_enter_openat"). The loader maps the section name to a type, following libbpf’s conventions.

A user-space program, the loader, hands the bytecode to the kernel. Before the kernel accepts it, the verifier, part of the kernel, walks every path through the program. It checks that every memory access stays inside memory the program may touch, that a pointer that can be NULL is checked before it is used, that every loop ends, and that the program calls only helpers its type allows. If one check fails, the load fails, and the kernel returns the verifier’s log, which names the instruction and the reason.

When the verifier accepts the program, the kernel’s JIT compiler translates the bytecode to native machine code for the CPU, x86-64 or arm64, so the program runs at the speed of compiled kernel code. On a kernel built without the JIT, or with it switched off, an interpreter runs the bytecode instead.

All of this happens once, at load, before the program ever runs. The verifier refuses any program it cannot prove safe, even one that would behave correctly, so writing eBPF programs includes shaping the code until the verifier can follow it.

The Go process makes three bpf() calls: BPF_PROG_LOAD sends the program through the verifier and the JIT compiler, BPF_LINK_CREATE attaches it to a hook, and BPF_MAP_LOOKUP_ELEM reads the maps the program writes when the hook fires.

The Go process and the kernel. Loading passes the program through the verifier and the JIT; attaching connects it to a hook; when the hook fires, the program runs and writes to maps, which the Go process reads.

A hook is a point in the kernel, or in a user program, where an attached program runs. Attaching connects a loaded program to a hook. Until it is attached, a loaded program does nothing. Each hook decides when the program runs:

  • Tracepoints are instrumentation points that kernel developers placed in the kernel source. Each has a name and a fixed record format. For example, syscalls/sys_enter_openat fires at the start of every openat(2) system call. The list is under /sys/kernel/tracing/events.
  • kprobes attach at run time to the entry of almost any kernel function, and kretprobes to its return. They reach functions no tracepoint covers, but they depend on how the running kernel was built: a function that was renamed or inlined cannot be probed.
  • uprobes and uretprobes do the same for functions in user-space programs and libraries.
  • fentry and fexit run at the entry and return of a kernel function, like kprobes, but the kernel passes the function’s arguments with their types, so the program reads them directly. fexit also sees the return value. They need the kernel’s type information, described below under BTF.
  • LSM programs attach to Linux Security Module hooks, the points where the kernel asks its security modules whether an operation may go ahead, such as opening a file or executing a program. The program can refuse the operation by returning an error.
  • XDP programs run for each packet a network driver receives, before the rest of the network stack sees it. They can drop, pass, redirect or send back the packet.
  • tc programs, and tcx programs, their newer link-based form, run when a packet enters or leaves a network device, in the traffic control layer, with the kernel’s full socket buffer.
  • netfilter programs run at the netfilter hooks, such as pre-routing, where firewall rules also run.
  • cgroup programs run for the processes in a control group (cgroup) when they create, bind or connect a socket, send or receive traffic, read or write a sysctl, or access a device.
  • sockmap programs run on the data sent or received by the sockets in a sockmap or sockhash map, and can redirect it to another socket.
  • struct_ops programs implement the functions of a kernel operations table, such as a TCP congestion control algorithm, and the kernel calls them like a built-in implementation.
Three columns of hooks. Static: tracepoints, fentry and fexit, LSM, struct_ops. Dynamic: kprobes, uprobes. Networking: XDP, tc and tcx, netfilter, cgroup, sockmap. Each card says what event fires it.

The hooks this manual covers, grouped as static, dynamic and networking hooks, with the event that runs the program.

The figure groups the hooks the way the guide does. Static hooks have arguments the kernel describes: a tracepoint’s record format, or the types of the function or operation the hook belongs to. Dynamic hooks are placed at attach time on a symbol or address, and the program reads the raw CPU registers to find the arguments.

A program cannot print or return data to user space. It writes to maps, key-value stores in kernel memory. The kernel creates each map at load, from the definitions in the object, and gives the loader a file descriptor for it. Programs read and write maps through helpers, and user space reads and writes them through the bpf() system call, using that descriptor. Maps are also how programs share state between runs and with each other: a counter a program increments stays in the map until the map is freed.

The common kinds are arrays, a fixed number of slots indexed by number; hash maps, keyed by any fixed-size value; their per-CPU forms, which keep one value per CPU so programs on different CPUs never contend; and the ring buffer and perf buffer, which stream records to user space instead of storing them. Global variables in BPF C are maps too, created from the object’s .data, .bss and .rodata sections.

Every step between user space and the kernel goes through one system call, bpf(2). Its first argument is a command, such as BPF_MAP_CREATE, BPF_PROG_LOAD, BPF_LINK_CREATE or BPF_MAP_LOOKUP_ELEM. The kernel refers to maps, programs and links by file descriptors, and keeps each one alive while something holds a reference to it: a descriptor in a process, a link, a pin in the BPF file system (bpffs), or another program.

The life of an eBPF tool follows the same steps every time. With gobpfgo, each step is one call:

  1. Compile. clang compiles the C source into an ELF object. This happens before your Go program runs.
  2. Open. The loader, gobpfgo in your Go process, reads the object: its sections, programs, map definitions and types. It makes no bpf() call. In Go, this is bpf.OpenFile.
  3. Load. The loader asks the kernel to create each map with BPF_MAP_CREATE, writes the map descriptors into the program’s instructions, and loads each program with BPF_PROG_LOAD. The kernel runs the verifier and the JIT and returns a program descriptor. In Go, this is Object.Load.
  4. Attach. The loader connects a program to its hook. For a tracepoint, it opens a perf event for the tracepoint and links the program to it with BPF_LINK_CREATE, or, on kernels older than 5.15, with an ioctl on the perf event. The result is a link, the kernel object that keeps the program attached. In Go, this is Program.AttachTracepoint.
  5. Events. The kernel runs the program each time the hook fires. The program updates its maps; the loader reads them with BPF_MAP_LOOKUP_ELEM, or reads a ring buffer through shared memory. In Go, this is Map.Lookup.
  6. Detach. Destroying the link disconnects the program from the hook. In Go, this is Link.Destroy.
  7. Close. Closing the object closes its map and program descriptors. The kernel frees each map and program once nothing references it any more. In Go, this is Object.Close.
Six steps in two rows: 1 Open with bpf.OpenFile, 2 Load with Object.Load, 3 Attach with Program.AttachTracepoint, 4 Events with Map.Lookup, repeated once per event, 5 Detach with Link.Destroy, 6 Close with Object.Close. Steps 3 and 4 are shaded: the program is attached and runs on every event.

The lifecycle of an object, with the gobpfgo call for each step. Between attach and detach, the program runs on every event.

Loading and attaching are privileged. The kernel requires root, or the CAP_BPF capability together with CAP_PERFMON for tracing programs or CAP_NET_ADMIN for networking programs. The install page covers the details.

BTF, the BPF Type Format, is a compact description of C types: structs and their fields, with sizes and offsets, enums, and function signatures. clang writes BTF for your program into the object’s .BTF and .BTF.ext sections when it compiles with -g. A kernel built with CONFIG_DEBUG_INFO_BTF=y publishes BTF for all of its own types at /sys/kernel/btf/vmlinux.

The kernel and the loader use BTF in several ways. Maps declared in the .maps section describe their key and value types in BTF. The verifier uses the program’s BTF to check functions and to put source lines in its log. fentry, fexit, LSM and struct_ops programs need the kernel’s BTF to find the function or table they attach to and its argument types.

CO-RE, short for Compile Once, Run Everywhere, solves a portability problem. The layout of kernel structs changes between kernel versions and configurations, so a program compiled against one kernel’s headers can read the wrong offset on another. With CO-RE, clang records each access to a kernel struct field as a relocation that names the type and the field instead of fixing the offset. At load, the loader looks the field up in the running kernel’s BTF and writes the real offset into the instruction. One object then runs on many kernels. gobpfgo applies these relocations as libbpf does. The first program reads no kernel structs, so it needs neither CO-RE nor kernel BTF.

gobpfgo is the loader. Your Go program uses it to open objects that clang compiled, apply their relocations, create their maps, load and attach their programs, and read their maps and buffers. Before loading, it probes the running kernel, as libbpf does, and adapts the object to what the kernel supports, for example by rewriting BTF kinds an older kernel does not know.

It is written in Go only: no cgo, no unsafe, and no libbpf shared library, so CGO_ENABLED=0 go build produces a static binary for amd64 or arm64. It has two packages, a 1:1 port of libbpf and a thin layer of Go types over it:

Three stacked levels. The bpf package calls bpf.OpenFile, Object.Load, Program.AttachTracepoint, Map.Lookup and Object.Close. Each forwards to the libbpf package: BpfObjectOpenFile, BpfObjectLoad, BpfProgramAttachTracepoint, BpfMapLookupElem and BpfObjectClose. These reach the kernel as no bpf() call for the open, BPF_BTF_LOAD, BPF_MAP_CREATE and BPF_PROG_LOAD for the load, perf_event_open and BPF_LINK_CREATE for the attach, BPF_MAP_LOOKUP_ELEM for the lookup, and closing each descriptor for the close.

The two packages and the kernel interface, with the call each level makes for open, load, attach, map lookup and close.

Each layer call forwards to one core call, and the core makes the system calls. Object.Load calls BpfObjectLoad, which loads the object’s BTF with BPF_BTF_LOAD, creates its maps with BPF_MAP_CREATE and loads its programs with BPF_PROG_LOAD. Program.AttachTracepoint calls BpfProgramAttachTracepoint, which opens a perf event and creates the link. Map.Lookup calls BpfMapLookupElem, which sends BPF_MAP_LOOKUP_ELEM. bpf.OpenFile and BpfObjectOpenFile make no bpf() call, and Object.Close and BpfObjectClose close the descriptors.

The bpf package is the layer you use in application code. The libbpf package, the core, keeps libbpf’s names and behavior. The two layers compares them.

  • It does not compile BPF programs. Programs stay in C and clang compiles them. There is no BPF assembler and no Go-to-BPF compiler.
  • It ships no BPF-side headers. The C side includes libbpf’s headers, such as bpf_helpers.h, from a libbpf source tree or from your distribution’s libbpf development package.
  • It does not generate skeletons. There is no equivalent of bpftool gen skeleton: you open an object by path or from bytes and look up its programs and maps by name.

The full list of unsupported features and of intentional differences from libbpf is in Differences from libbpf.