Skip to content

Commit fabe147

Browse files
smagnani96ti-mo
authored andcommitted
bpf2go: generate assignment structs for Variables and VariableSpecs
This commit teaches bpf2go to emit assignment structs for VariableSpecs and Variables. Similar to maps and programs, the generated bpf_bpfe{b,l}.go have bpfVariableSpecs embedded in bpfSpecs and bpfVariables in bpfSpecs. Also fixed a typo in the bpfProgramSpecs comments in output.tpl that emitted bpfSpecs rather than bpfProgramSpecs. Signed-off-by: Simone Magnani <[email protected]>
1 parent 7673737 commit fabe147

34 files changed

+530
-43
lines changed

cmd/bpf2go/gen/output.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func (n templateName) MapSpecs() string {
4949
return string(n) + "MapSpecs"
5050
}
5151

52+
func (n templateName) VariableSpecs() string {
53+
return string(n) + "VariableSpecs"
54+
}
55+
5256
func (n templateName) Load() string {
5357
return n.maybeExport("load" + toUpperFirst(string(n)))
5458
}
@@ -65,6 +69,10 @@ func (n templateName) Maps() string {
6569
return string(n) + "Maps"
6670
}
6771

72+
func (n templateName) Variables() string {
73+
return string(n) + "Variables"
74+
}
75+
6876
func (n templateName) Programs() string {
6977
return string(n) + "Programs"
7078
}
@@ -82,6 +90,8 @@ type GenerateArgs struct {
8290
Constraints constraint.Expr
8391
// Maps to be emitted.
8492
Maps []string
93+
// Variables to be emitted.
94+
Variables []string
8595
// Programs to be emitted.
8696
Programs []string
8797
// Types to be emitted.
@@ -121,6 +131,11 @@ func Generate(args GenerateArgs) error {
121131
maps[name] = args.Identifier(name)
122132
}
123133

134+
variables := make(map[string]string)
135+
for _, name := range args.Variables {
136+
variables[name] = args.Identifier(name)
137+
}
138+
124139
programs := make(map[string]string)
125140
for _, name := range args.Programs {
126141
programs[name] = args.Identifier(name)
@@ -151,6 +166,7 @@ func Generate(args GenerateArgs) error {
151166
Constraints constraint.Expr
152167
Name templateName
153168
Maps map[string]string
169+
Variables map[string]string
154170
Programs map[string]string
155171
Types []btf.Type
156172
TypeNames map[btf.Type]string
@@ -162,6 +178,7 @@ func Generate(args GenerateArgs) error {
162178
args.Constraints,
163179
templateName(args.Stem),
164180
maps,
181+
variables,
165182
programs,
166183
types,
167184
typeNames,

cmd/bpf2go/gen/output.tpl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ func {{ .Name.LoadObjects }}(obj interface{}, opts *ebpf.CollectionOptions) (err
5454
type {{ .Name.Specs }} struct {
5555
{{ .Name.ProgramSpecs }}
5656
{{ .Name.MapSpecs }}
57+
{{ .Name.VariableSpecs }}
5758
}
5859

59-
// {{ .Name.Specs }} contains programs before they are loaded into the kernel.
60+
// {{ .Name.ProgramSpecs }} contains programs before they are loaded into the kernel.
6061
//
6162
// It can be passed ebpf.CollectionSpec.Assign.
6263
type {{ .Name.ProgramSpecs }} struct {
@@ -74,12 +75,22 @@ type {{ .Name.MapSpecs }} struct {
7475
{{- end }}
7576
}
7677

78+
// {{ .Name.VariableSpecs }} contains global variables before they are loaded into the kernel.
79+
//
80+
// It can be passed ebpf.CollectionSpec.Assign.
81+
type {{ .Name.VariableSpecs }} struct {
82+
{{- range $name, $id := .Variables }}
83+
{{ $id }} *ebpf.VariableSpec `ebpf:"{{ $name }}"`
84+
{{- end }}
85+
}
86+
7787
// {{ .Name.Objects }} contains all objects after they have been loaded into the kernel.
7888
//
7989
// It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign.
8090
type {{ .Name.Objects }} struct {
8191
{{ .Name.Programs }}
8292
{{ .Name.Maps }}
93+
{{ .Name.Variables }}
8394
}
8495

8596
func (o *{{ .Name.Objects }}) Close() error {
@@ -106,6 +117,15 @@ func (m *{{ .Name.Maps }}) Close() error {
106117
)
107118
}
108119

120+
// {{ .Name.Variables }} contains all global variables after they have been loaded into the kernel.
121+
//
122+
// It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign.
123+
type {{ .Name.Variables }} struct {
124+
{{- range $name, $id := .Variables }}
125+
{{ $id }} *ebpf.Variable `ebpf:"{{ $name }}"`
126+
{{- end }}
127+
}
128+
109129
// {{ .Name.Programs }} contains all programs after they have been loaded into the kernel.
110130
//
111131
// It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign.

cmd/bpf2go/gen/output_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,27 @@ func TestCustomIdentifier(t *testing.T) {
9292
qt.Assert(t, qt.IsNil(err))
9393
qt.Assert(t, qt.StringContains(buf.String(), "DO_THING"))
9494
}
95+
96+
func TestObjects(t *testing.T) {
97+
var buf bytes.Buffer
98+
args := GenerateArgs{
99+
Package: "foo",
100+
Stem: "bar",
101+
Maps: []string{"map1"},
102+
Variables: []string{"var_1"},
103+
Programs: []string{"prog_foo_1"},
104+
Output: &buf,
105+
}
106+
err := Generate(args)
107+
qt.Assert(t, qt.IsNil(err))
108+
109+
str := buf.String()
110+
111+
qt.Assert(t, qt.StringContains(str, "Map1 *ebpf.MapSpec `ebpf:\"map1\"`"))
112+
qt.Assert(t, qt.StringContains(str, "Var1 *ebpf.VariableSpec `ebpf:\"var_1\"`"))
113+
qt.Assert(t, qt.StringContains(str, "ProgFoo1 *ebpf.ProgramSpec `ebpf:\"prog_foo_1\"`"))
114+
115+
qt.Assert(t, qt.StringContains(str, "Map1 *ebpf.Map `ebpf:\"map1\"`"))
116+
qt.Assert(t, qt.StringContains(str, "Var1 *ebpf.Variable `ebpf:\"var_1\"`"))
117+
qt.Assert(t, qt.StringContains(str, "ProgFoo1 *ebpf.Program `ebpf:\"prog_foo_1\"`"))
118+
}

cmd/bpf2go/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,11 @@ func (b2g *bpf2go) convert(tgt gen.Target, goarches gen.GoArches) (err error) {
370370
}
371371
}
372372

373+
var variables []string
374+
for name := range spec.Variables {
375+
variables = append(variables, name)
376+
}
377+
373378
var programs []string
374379
for name := range spec.Programs {
375380
programs = append(programs, name)
@@ -397,6 +402,7 @@ func (b2g *bpf2go) convert(tgt gen.Target, goarches gen.GoArches) (err error) {
397402
Stem: b2g.identStem,
398403
Constraints: constraints,
399404
Maps: maps,
405+
Variables: variables,
400406
Programs: programs,
401407
Types: types,
402408
ObjectFile: filepath.Base(objFileName),

cmd/bpf2go/test/api_test.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ import (
55
"testing"
66
"unsafe"
77

8+
"github.com/go-quicktest/qt"
9+
810
"github.com/cilium/ebpf/internal/testutils"
911
)
1012

1113
func TestLoadingSpec(t *testing.T) {
1214
spec, err := loadTest()
1315
testutils.SkipIfNotSupported(t, err)
14-
if err != nil {
15-
t.Fatal("Can't load spec:", err)
16-
}
16+
qt.Assert(t, qt.IsNil(err))
1717

18-
if spec == nil {
19-
t.Fatal("Got a nil spec")
20-
}
18+
qt.Assert(t, qt.Not(qt.IsNil(spec)))
19+
qt.Assert(t, qt.Not(qt.IsNil(spec.Programs)))
20+
qt.Assert(t, qt.Not(qt.IsNil(spec.Maps)))
21+
qt.Assert(t, qt.Not(qt.IsNil(spec.Variables)))
2122
}
2223

2324
func TestLoadingObjects(t *testing.T) {
@@ -29,13 +30,10 @@ func TestLoadingObjects(t *testing.T) {
2930
}
3031
defer objs.Close()
3132

32-
if objs.Filter == nil {
33-
t.Error("Loading returns an object with nil programs")
34-
}
35-
36-
if objs.Map1 == nil {
37-
t.Error("Loading returns an object with nil maps")
38-
}
33+
qt.Assert(t, qt.Not(qt.IsNil(objs.Filter)))
34+
qt.Assert(t, qt.Not(qt.IsNil(objs.Map1)))
35+
qt.Assert(t, qt.Not(qt.IsNil(objs.MyConstant)))
36+
qt.Assert(t, qt.Not(qt.IsNil(objs.StructConst)))
3937
}
4038

4139
func TestTypes(t *testing.T) {

cmd/bpf2go/test/test_bpfeb.go

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/bpf2go/test/test_bpfel.go

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/examples/getting_started/counter_bpfeb.go

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/examples/getting_started/counter_bpfel.go

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)