
gobpfgo
Load, attach and manage eBPF programs from Go, with no cgo and no unsafe. This manual starts with the concepts and a first tracepoint program, then adds one topic per chapter.
Why gobpfgo
Section titled “Why gobpfgo”Your BPF programs stay in C and compile with clang. gobpfgo is the user-space side: the loader that opens the compiled object, relocates it, creates its maps, loads its programs and attaches them.
- No cgo and no
unsafe.go buildis the whole build.CGO_ENABLED=0 GOARCH=arm64 go buildcross-compiles a static binary with no C toolchain, libbpf, libelf or zlib. - A 1:1 libbpf core. The
libbpfpackage ports libbpf function by function:bpf_object__open_fileisBpfObjectOpenFile, with the same section names, load steps, CO-RE relocations and errnos. - A thin object layer. The
bpfpackage wraps the core inObject,Program,MapandLinktypes with methods and explicit lifetimes. Each method forwards one core call. - Tested against libbpf. A C oracle linked against the pinned libbpf runs the same fixtures, and the root and unprivileged suites pass on six kernels from 5.10 to 7.4-rc4, on amd64 and arm64.
The shape of a program
Section titled “The shape of a program”The first program in this manual counts openat(2) calls. With the error
checks left out, its Go side reads:
obj, err := bpf.OpenFile("open_count.bpf.o", nil)err = obj.Load()link, err := obj.Program("handle_open").AttachTracepoint("syscalls", "sys_enter_openat")key, value := make([]byte, 4), make([]byte, 8)err = obj.Map("counts").Lookup(key, value, 0)link.Destroy(); obj.Close()The tutorial version checks every error and is built and run as part of the repository’s examples.
Where to go next
Section titled “Where to go next”Your first programCompile a tracepoint program, load it from Go and read its counter.
The guideThe two layers, then hooks, maps, helpers, CO-RE and buffers, one chapter each.
ReferenceThe package API, kernel support and the differences from libbpf.
