Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ all: format $(addsuffix -el.elf,$(TARGETS)) $(addsuffix -eb.elf,$(TARGETS)) gene
ln -srf testdata/loader-$(CLANG)-eb.elf testdata/loader-eb.elf

generate:
go generate -run "internal/cmd/gentypes" ./...
go generate -skip "internal/cmd/gentypes" ./...
go generate -run "gentypes" ./...
go generate -skip "gentypes" ./...

testdata/loader-%-el.elf: testdata/loader.c
$* $(CFLAGS) -target bpfel -c $< -o $@
Expand Down
2 changes: 1 addition & 1 deletion asm/alu.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package asm

//go:generate go run golang.org/x/tools/cmd/stringer@latest -output alu_string.go -type=Source,Endianness,ALUOp
//go:generate go tool stringer -output alu_string.go -type=Source,Endianness,ALUOp

// Source of ALU / ALU64 / Branch operations
//
Expand Down
2 changes: 1 addition & 1 deletion asm/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package asm

import "github.com/cilium/ebpf/internal/platform"

//go:generate go run golang.org/x/tools/cmd/stringer@latest -output func_string.go -type=BuiltinFunc
//go:generate go tool stringer -output func_string.go -type=BuiltinFunc

// BuiltinFunc is a built-in eBPF function.
type BuiltinFunc uint32
Expand Down
2 changes: 1 addition & 1 deletion asm/jump.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package asm

//go:generate go run golang.org/x/tools/cmd/stringer@latest -output jump_string.go -type=JumpOp
//go:generate go tool stringer -output jump_string.go -type=JumpOp

// JumpOp affect control flow.
//
Expand Down
2 changes: 1 addition & 1 deletion asm/load_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package asm

import "fmt"

//go:generate go run golang.org/x/tools/cmd/stringer@latest -output load_store_string.go -type=Mode,Size
//go:generate go tool stringer -output load_store_string.go -type=Mode,Size

// Mode for load and store operations
//
Expand Down
2 changes: 1 addition & 1 deletion asm/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
)

//go:generate go run golang.org/x/tools/cmd/stringer@latest -output opcode_string.go -type=Class
//go:generate go tool stringer -output opcode_string.go -type=Class

// Class of operations
//
Expand Down
5 changes: 3 additions & 2 deletions asm/opcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion btf/btf_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"unsafe"
)

//go:generate go run golang.org/x/tools/cmd/stringer@latest -linecomment -output=btf_types_string.go -type=FuncLinkage,VarLinkage,btfKind
//go:generate go tool stringer -linecomment -output=btf_types_string.go -type=FuncLinkage,VarLinkage,btfKind

// btfKind describes a Type.
type btfKind uint8
Expand Down
15 changes: 9 additions & 6 deletions btf/btf_types_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions cmd/bpf2go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ eBPF from disk at runtime and to minimise the amount of manual
work required to interact with eBPF programs. It takes inspiration
from `bpftool gen skeleton`.

Invoke the program using go generate:
Add `bpf2go` as a tool dependency in your project's Go module:

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go foo path/to/src.c -- -I/path/to/include
go get -tool github.com/cilium/ebpf/cmd/bpf2go

Invoke the tool using go generate:

//go:generate go tool bpf2go foo path/to/src.c -- -I/path/to/include

This will emit `foo_bpfel.go` and `foo_bpfeb.go`, with types using `foo`
as a stem. The two files contain compiled BPF for little and big
Expand Down
2 changes: 1 addition & 1 deletion cmd/bpf2go/test/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
// specific API.
package test

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux test ../testdata/minimal.c
//go:generate go tool bpf2go -tags linux test ../testdata/minimal.c
14 changes: 6 additions & 8 deletions docs/ebpf/guides/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,12 @@ go: to add module requirements and sums:
% go mod tidy
```

We also need to manually add a dependency on `bpf2go` since it's not explicitly
imported by a `.go` source file:

```{ .shell-session data-copy="go get github.com/cilium/ebpf/cmd/bpf2go" }
% go get github.com/cilium/ebpf/cmd/bpf2go
go: added github.com/cilium/ebpf v0.11.0
go: added golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2
go: added golang.org/x/sys v0.6.0
First, add `bpf2go` as a tool dependency to your Go module. This ensures the
version of `bpf2go` used by the Go toolchain always matches your version of the
library.

```{ .shell-session data-copy="go get -tool github.com/cilium/ebpf/cmd/bpf2go" }
% go get -tool github.com/cilium/ebpf/cmd/bpf2go
```

Now we're ready to run `go generate`:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/getting_started/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
// getting_started_gen {
package main

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux counter counter.c
//go:generate go tool bpf2go -tags linux counter counter.c

// }
2 changes: 1 addition & 1 deletion docs/examples/variables/gen.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go variables variables.c
//go:generate go tool bpf2go variables variables.c
2 changes: 1 addition & 1 deletion examples/cgroup_skb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux bpf cgroup_skb.c -- -I../headers
//go:generate go tool bpf2go -tags linux bpf cgroup_skb.c -- -I../headers

func main() {
// Allow the current process to lock memory for eBPF resources.
Expand Down
2 changes: 1 addition & 1 deletion examples/fentry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux bpf fentry.c -- -I../headers
//go:generate go tool bpf2go -tags linux bpf fentry.c -- -I../headers

func main() {
stopper := make(chan os.Signal, 1)
Expand Down
2 changes: 1 addition & 1 deletion examples/kprobe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux bpf kprobe.c -- -I../headers
//go:generate go tool bpf2go -tags linux bpf kprobe.c -- -I../headers

const mapKey uint32 = 0

Expand Down
2 changes: 1 addition & 1 deletion examples/kprobe_percpu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux bpf kprobe_percpu.c -- -I../headers
//go:generate go tool bpf2go -tags linux bpf kprobe_percpu.c -- -I../headers

const mapKey uint32 = 0

Expand Down
2 changes: 1 addition & 1 deletion examples/kprobepin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux bpf kprobe_pin.c -- -I../headers
//go:generate go tool bpf2go -tags linux bpf kprobe_pin.c -- -I../headers

const (
mapKey uint32 = 0
Expand Down
2 changes: 1 addition & 1 deletion examples/ringbuffer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux bpf ringbuffer.c -- -I../headers
//go:generate go tool bpf2go -tags linux bpf ringbuffer.c -- -I../headers

func main() {
// Name of the kernel function to trace.
Expand Down
2 changes: 1 addition & 1 deletion examples/tcprtt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux bpf tcprtt.c -- -I../headers
//go:generate go tool bpf2go -tags linux bpf tcprtt.c -- -I../headers

func main() {
stopper := make(chan os.Signal, 1)
Expand Down
2 changes: 1 addition & 1 deletion examples/tcprtt_sockops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"golang.org/x/sys/unix"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux -tags "linux" bpf tcprtt_sockops.c -- -I../headers
//go:generate go tool bpf2go -tags linux -tags "linux" bpf tcprtt_sockops.c -- -I../headers

func main() {
stopper := make(chan os.Signal, 1)
Expand Down
2 changes: 1 addition & 1 deletion examples/tcx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/cilium/ebpf/link"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux bpf tcx.c -- -I../headers
//go:generate go tool bpf2go -tags linux bpf tcx.c -- -I../headers
func main() {
if len(os.Args) < 2 {
log.Fatalf("Please specify a network interface")
Expand Down
2 changes: 1 addition & 1 deletion examples/tracepoint_in_c/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux bpf tracepoint.c -- -I../headers
//go:generate go tool bpf2go -tags linux bpf tracepoint.c -- -I../headers

const mapKey uint32 = 0

Expand Down
2 changes: 1 addition & 1 deletion examples/uretprobe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux -target amd64 bpf uretprobe.c -- -I../headers
//go:generate go tool bpf2go -tags linux -target amd64 bpf uretprobe.c -- -I../headers

const (
// The path to the ELF binary containing the function to trace.
Expand Down
2 changes: 1 addition & 1 deletion examples/xdp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/cilium/ebpf/link"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags linux bpf xdp.c -- -I../headers
//go:generate go tool bpf2go -tags linux bpf xdp.c -- -I../headers

func main() {
if len(os.Args) < 2 {
Expand Down
16 changes: 12 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module github.com/cilium/ebpf

go 1.24
go 1.24.0

require (
github.com/go-quicktest/qt v1.101.1-0.20240301121107-c6c8733fa1e6
github.com/google/go-cmp v0.6.0
github.com/jsimonetti/rtnetlink/v2 v2.0.1
golang.org/x/sys v0.31.0
golang.org/x/sys v0.37.0
)

require (
Expand All @@ -16,6 +16,14 @@ require (
github.com/mdlayher/netlink v1.7.2 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/mod v0.29.0 // indirect
golang.org/x/net v0.46.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/tools v0.38.0 // indirect
)

tool (
github.com/cilium/ebpf/cmd/bpf2go
github.com/cilium/ebpf/internal/cmd/gentypes
golang.org/x/tools/cmd/stringer
)
16 changes: 10 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
2 changes: 1 addition & 1 deletion internal/efw/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package efw
// See https://github.com/microsoft/ebpf-for-windows/blob/main/include/ebpf_result.h
type Result int32

//go:generate go run golang.org/x/tools/cmd/stringer@latest -tags windows -output result_string_windows.go -type=Result
//go:generate go tool stringer -tags windows -output result_string_windows.go -type=Result

const (
EBPF_SUCCESS Result = iota
Expand Down
2 changes: 1 addition & 1 deletion internal/sys/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package sys

// Regenerate types.go by invoking go generate in the current directory.

//go:generate go run github.com/cilium/ebpf/internal/cmd/gentypes ../../btf/testdata/vmlinux.btf.gz
//go:generate go tool gentypes ../../btf/testdata/vmlinux.btf.gz
2 changes: 1 addition & 1 deletion internal/tracefs/kprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
ErrInvalidMaxActive = errors.New("can only set maxactive on kretprobes")
)

//go:generate go run golang.org/x/tools/cmd/stringer@latest -type=ProbeType -linecomment
//go:generate go tool stringer -type=ProbeType -linecomment

type ProbeType uint8

Expand Down
5 changes: 3 additions & 2 deletions internal/tracefs/probetype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/unix/errno_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"syscall"
)

//go:generate go run golang.org/x/tools/cmd/stringer@latest -type=Errno -tags=windows -output=errno_string_windows.go
//go:generate go tool stringer -type=Errno -tags=windows -output=errno_string_windows.go

// Windows specific constants for Unix errnos.
//
Expand Down
4 changes: 2 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/cilium/ebpf/internal/sys"
)

//go:generate go run golang.org/x/tools/cmd/stringer@latest -output types_string.go -type=MapType,ProgramType,PinType
//go:generate go tool stringer -output types_string.go -type=MapType,ProgramType,PinType

// MapType indicates the type map structure
// that will be initialized in the kernel.
Expand Down Expand Up @@ -252,7 +252,7 @@ func ProgramTypeForPlatform(plat string, value uint32) (ProgramType, error) {
// Will cause invalid argument (EINVAL) at program load time if set incorrectly.
type AttachType uint32

//go:generate go run golang.org/x/tools/cmd/stringer@latest -type AttachType -trimprefix Attach
//go:generate go tool stringer -type AttachType -trimprefix Attach

// AttachNone is an alias for AttachCGroupInetIngress for readability reasons.
const AttachNone AttachType = 0
Expand Down
Loading
Loading