Install
An eBPF tool built with gobpfgo has two halves: a BPF program in C, which clang compiles into an object file, and a Go program, which loads that object into the kernel. This page sets up both halves and checks that your kernel can run the tutorial.
Prerequisites
Section titled “Prerequisites”- Linux 5.10 or newer, on amd64 (x86-64) or arm64. gobpfgo is tested on six kernels from 5.10 to 7.4-rc4. Newer features need newer kernels; see the support table linked below.
- Root, or the
CAP_BPFandCAP_PERFMONcapabilities, to load and attach tracing programs. Networking programs needCAP_NET_ADMINinstead ofCAP_PERFMON. The tutorial runs its programs withsudo. - clang with the BPF target, and the LLVM tools that come with it, to
compile the C side.
clang -print-targetslistsbpfwhen the target is there. The examples are tested with clang 23. - The libbpf headers, for the C side only:
bpf_helpers.hand thelinux/bpf.hUAPI header. gobpfgo does not ship BPF-side headers. The examples build against a libbpf source tree. - Go 1.27 or newer, and
make.
The Go side needs no C toolchain and no libbpf library. clang and the headers are only needed where you compile the BPF objects.
Get the code
Section titled “Get the code”-
Clone gobpfgo and libbpf into the same directory. The examples’ Makefile looks for libbpf next to the gobpfgo checkout.
Terminal window git clone https://github.com/gobpfgo/gobpfgogit clone https://github.com/libbpf/libbpf -
Check the Go version.
Terminal window go versionIt must print
go1.27or newer. -
Build the examples’ Go code. The examples are their own Go module, which uses the gobpfgo checkout one directory up.
Terminal window cd gobpfgo/examplesgo build ./...The command prints nothing when it succeeds.
In your own project, add gobpfgo as a dependency instead:
go get github.com/gobpfgo/gobpfgoApplication code imports github.com/gobpfgo/gobpfgo/bpf; the libbpf core is
github.com/gobpfgo/gobpfgo/libbpf.
Compile a BPF object
Section titled “Compile a BPF object”From the examples directory, make compiles every tutorial/*/*.bpf.c
into a .bpf.o next to it:
makeOn an x86-64 host it runs:
clang -O2 -g -target bpf -D__TARGET_ARCH_x86 \ -I../../libbpf/include/uapi -I../../libbpf/src \ -c tutorial/step01/open_count.bpf.c -o tutorial/step01/open_count.bpf.oEach flag has a job:
-target bpfmakes clang emit BPF bytecode instead of code for the host CPU, in the host’s byte order.-O2optimizes the code. Build BPF objects with it: the examples are tested at-O2, and an unoptimized build of even the first program carries BTF that the kernel refuses.-gmakes clang write BTF, the type information described in What is eBPF, and the line information the verifier puts in its log.-D__TARGET_ARCH_x86tells libbpf’sbpf_tracing.hwhich CPU’s registers hold function arguments. The Makefile setsx86orarm64fromuname -m. The first program does not need it; the kprobe chapter does.-I../../libbpf/include/uapifindslinux/bpf.hin libbpf’s copy of the kernel UAPI headers, and-I../../libbpf/srcfindsbpf_helpers.h.
If libbpf is somewhere else, point the Makefile at it:
make LIBBPF=/path/to/libbpfTo use your distribution’s libbpf development package instead of a source
tree (libbpf-dev on Debian and Ubuntu, libbpf-devel on Fedora, libbpf on
Arch), add its header directory to the compiler command:
make CLANG="clang -I/usr/include/bpf"On Debian and Ubuntu, if clang stops with
fatal error: 'asm/types.h' file not found, the kernel headers keep asm/
in a per-architecture directory that clang does not search for the BPF
target. Add it the same way:
make CLANG="clang -I/usr/include/$(uname -m)-linux-gnu"make clean removes the built objects.
Check your kernel
Section titled “Check your kernel”The first program needs a tracepoint and an array map, which every kernel from 5.10 supports. Later chapters need more, so check these now:
uname -rls /sys/kernel/btf/vmlinuxsudo ls /sys/kernel/tracing/events/syscalls/sys_enter_openatuname -rprints the kernel release. It must be 5.10 or newer. Distribution kernels often backport BPF features, so an older-looking version can still support a feature; gobpfgo probes the kernel at load either way./sys/kernel/btf/vmlinuxexists when the kernel was built with BTF. See the second permission note below.- The last command lists the tracepoint the first program attaches to:
enable,filter,format,idand more. tracefs is mounted at/sys/kernel/tracingand is usually readable only by root.
If bpftool is installed, sudo bpftool feature probe kernel lists the
program types, map types and helpers the running kernel supports.
The minimum kernel for each feature, on amd64 and on arm64, the kernel configuration options and the kernels the test suites run on are listed in Kernel support.
Two permission notes
Section titled “Two permission notes”bpf() needs privilege. Creating maps, loading programs and attaching
them are privileged operations. Run your tool as root, or give it CAP_BPF
and CAP_PERFMON for tracing programs, or CAP_BPF and CAP_NET_ADMIN for
networking programs. Without them, the load fails with EPERM. In the
tutorial, the kernel first refuses the object’s BTF, which is optional, so
the load goes on, and then refuses to create the map:
libbpf: Error loading .BTF into kernel: -EPERM. BTF is optional, ignoring.step01: bpf_map_create: operation not permitted: countsOpening an object, linking objects and reading BTF need no privilege.
sudo resets PATH, so the tutorial runs sudo env PATH="$PATH" go run .
to let root find your go command.
BTF-aware hooks need a kernel built with BTF. fentry, fexit, LSM,
struct_ops, iterator and tracing multi programs read the kernel’s types from
/sys/kernel/btf/vmlinux, which exists only when the kernel was built with
CONFIG_DEBUG_INFO_BTF=y. CO-RE relocations need it too, unless the open
options point at a BTF file for the target kernel. Tracepoints, kprobes,
uprobes, XDP and tc programs without CO-RE work without kernel BTF, so the
first program runs either way. Most current distribution kernels enable BTF.
What is not supported
Section titled “What is not supported”gobpfgo does not provide skeleton generation, big-endian linker input,
split-BTF dedup, fixed-address arenas or perf_buffer__new_raw. The reasons,
and every intentional difference from libbpf, are in Differences from
libbpf.
