| 1 | bpf2go |
| 2 | === |
| 3 | |
| 4 | `bpf2go` compiles a C source file into eBPF bytecode and then emits a |
| 5 | Go file containing the eBPF. The goal is to avoid loading the |
| 6 | eBPF from disk at runtime and to minimise the amount of manual |
| 7 | work required to interact with eBPF programs. It takes inspiration |
| 8 | from `bpftool gen skeleton`. |
| 9 | |
| 10 | Add `bpf2go` as a tool dependency in your project's Go module: |
| 11 | |
| 12 | go get -tool github.com/cilium/ebpf/cmd/bpf2go |
| 13 | |
| 14 | Invoke the tool using go generate: |
| 15 | |
| 16 | //go:generate go tool bpf2go foo path/to/src.c -- -I/path/to/include |
| 17 | |
| 18 | This will emit `foo_bpfel.go` and `foo_bpfeb.go`, with types using `foo` |
| 19 | as a stem. The two files contain compiled BPF for little and big |
| 20 | endian systems, respectively. |
| 21 | |
| 22 | ## Environment Variables |
| 23 | |
| 24 | You can use environment variables to affect all bpf2go invocations |
| 25 | across a project, e.g. to set specific C flags: |
| 26 | |
| 27 | BPF2GO_CFLAGS="-O2 -g -Wall -Werror $(CFLAGS)" go generate ./... |
| 28 | |
| 29 | Alternatively, by exporting `$BPF2GO_CFLAGS` from your build system, you can |
| 30 | control all builds from a single location. |
| 31 | |
| 32 | Most bpf2go arguments can be controlled this way. See `bpf2go -h` for an |
| 33 | up-to-date list. |
| 34 | |
| 35 | ## Generated types |
| 36 | |
| 37 | `bpf2go` generates Go types for all map keys and values by default. You can |
| 38 | disable this behaviour using `-no-global-types`. You can add to the set of |
| 39 | types by specifying `-type foo` for each type you'd like to generate. |
| 40 | |
| 41 | ## Examples |
| 42 | |
| 43 | See [examples/kprobe](../../examples/kprobe/main.go) for a fully worked out example. |