Skip to content

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.

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 build is the whole build. CGO_ENABLED=0 GOARCH=arm64 go build cross-compiles a static binary with no C toolchain, libbpf, libelf or zlib.
  • A 1:1 libbpf core. The libbpf package ports libbpf function by function: bpf_object__open_file is BpfObjectOpenFile, with the same section names, load steps, CO-RE relocations and errnos.
  • A thin object layer. The bpf package wraps the core in Object, Program, Map and Link types 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 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.