Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

land bpf2go basic support for Variable and VariableSpec #1610

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions cmd/bpf2go/gen/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (n templateName) MapSpecs() string {
return string(n) + "MapSpecs"
}

func (n templateName) VariableSpecs() string {
return string(n) + "VariableSpecs"
}

func (n templateName) Load() string {
return n.maybeExport("load" + toUpperFirst(string(n)))
}
Expand All @@ -65,6 +69,10 @@ func (n templateName) Maps() string {
return string(n) + "Maps"
}

func (n templateName) Variables() string {
return string(n) + "Variables"
}

func (n templateName) Programs() string {
return string(n) + "Programs"
}
Expand All @@ -82,6 +90,8 @@ type GenerateArgs struct {
Constraints constraint.Expr
// Maps to be emitted.
Maps []string
// Variables to be emitted.
Variables []string
// Programs to be emitted.
Programs []string
// Types to be emitted.
Expand Down Expand Up @@ -121,6 +131,11 @@ func Generate(args GenerateArgs) error {
maps[name] = args.Identifier(name)
}

variables := make(map[string]string)
for _, name := range args.Variables {
variables[name] = args.Identifier(name)
}

programs := make(map[string]string)
for _, name := range args.Programs {
programs[name] = args.Identifier(name)
Expand Down Expand Up @@ -151,6 +166,7 @@ func Generate(args GenerateArgs) error {
Constraints constraint.Expr
Name templateName
Maps map[string]string
Variables map[string]string
Programs map[string]string
Types []btf.Type
TypeNames map[btf.Type]string
Expand All @@ -162,6 +178,7 @@ func Generate(args GenerateArgs) error {
args.Constraints,
templateName(args.Stem),
maps,
variables,
programs,
types,
typeNames,
Expand Down
22 changes: 21 additions & 1 deletion cmd/bpf2go/gen/output.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ func {{ .Name.LoadObjects }}(obj interface{}, opts *ebpf.CollectionOptions) (err
type {{ .Name.Specs }} struct {
{{ .Name.ProgramSpecs }}
{{ .Name.MapSpecs }}
{{ .Name.VariableSpecs }}
}

// {{ .Name.Specs }} contains programs before they are loaded into the kernel.
// {{ .Name.ProgramSpecs }} contains programs before they are loaded into the kernel.
//
// It can be passed ebpf.CollectionSpec.Assign.
type {{ .Name.ProgramSpecs }} struct {
Expand All @@ -74,12 +75,22 @@ type {{ .Name.MapSpecs }} struct {
{{- end }}
}

// {{ .Name.VariableSpecs }} contains variables before they are loaded into the kernel.
//
// It can be passed ebpf.CollectionSpec.Assign.
type {{ .Name.VariableSpecs }} struct {
{{- range $name, $id := .Variables }}
{{ $id }} *ebpf.VariableSpec `ebpf:"{{ $name }}"`
{{- end }}
}

// {{ .Name.Objects }} contains all objects after they have been loaded into the kernel.
//
// It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign.
type {{ .Name.Objects }} struct {
{{ .Name.Programs }}
{{ .Name.Maps }}
{{ .Name.Variables }}
}

func (o *{{ .Name.Objects }}) Close() error {
Expand All @@ -106,6 +117,15 @@ func (m *{{ .Name.Maps }}) Close() error {
)
}

// {{ .Name.Variables }} contains all variables after they have been loaded into the kernel.
//
// It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign.
type {{ .Name.Variables }} struct {
{{- range $name, $id := .Variables }}
{{ $id }} *ebpf.Variable `ebpf:"{{ $name }}"`
{{- end }}
}

// {{ .Name.Programs }} contains all programs after they have been loaded into the kernel.
//
// It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign.
Expand Down
22 changes: 22 additions & 0 deletions cmd/bpf2go/gen/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,25 @@ func TestCustomIdentifier(t *testing.T) {
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.StringContains(buf.String(), "DO_THING"))
}

func TestBpfObjects(t *testing.T) {
var buf bytes.Buffer
args := GenerateArgs{
Package: "foo",
Stem: "bar",
Maps: []string{"map1"},
Variables: []string{"var_1"},
Programs: []string{"prog_foo_1"},
Output: &buf,
}
err := Generate(args)
qt.Assert(t, qt.IsNil(err))

qt.Assert(t, qt.StringContains(buf.String(), "Map1 *ebpf.MapSpec `ebpf:\"map1\"`"))
qt.Assert(t, qt.StringContains(buf.String(), "Var1 *ebpf.VariableSpec `ebpf:\"var_1\"`"))
qt.Assert(t, qt.StringContains(buf.String(), "ProgFoo1 *ebpf.ProgramSpec `ebpf:\"prog_foo_1\"`"))

qt.Assert(t, qt.StringContains(buf.String(), "Map1 *ebpf.Map `ebpf:\"map1\"`"))
qt.Assert(t, qt.StringContains(buf.String(), "Var1 *ebpf.Variable `ebpf:\"var_1\"`"))
qt.Assert(t, qt.StringContains(buf.String(), "ProgFoo1 *ebpf.Program `ebpf:\"prog_foo_1\"`"))
}
6 changes: 6 additions & 0 deletions cmd/bpf2go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ func (b2g *bpf2go) convert(tgt gen.Target, goarches gen.GoArches) (err error) {
}
}

var variables []string
for name := range spec.Variables {
variables = append(variables, name)
}

var programs []string
for name := range spec.Programs {
programs = append(programs, name)
Expand Down Expand Up @@ -397,6 +402,7 @@ func (b2g *bpf2go) convert(tgt gen.Target, goarches gen.GoArches) (err error) {
Stem: b2g.identStem,
Constraints: constraints,
Maps: maps,
Variables: variables,
Programs: programs,
Types: types,
ObjectFile: filepath.Base(objFileName),
Expand Down
16 changes: 16 additions & 0 deletions cmd/bpf2go/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ func TestLoadingSpec(t *testing.T) {
if spec == nil {
t.Fatal("Got a nil spec")
}

if spec.Programs == nil {
t.Error("Got a nil spec for programs")
}

if spec.Maps == nil {
t.Error("Got a nil spec for maps")
}

if spec.Variables == nil {
t.Error("Got a nil spec for variables")
}
}

func TestLoadingObjects(t *testing.T) {
Expand All @@ -36,6 +48,10 @@ func TestLoadingObjects(t *testing.T) {
if objs.Map1 == nil {
t.Error("Loading returns an object with nil maps")
}

if objs.MyConstant == nil || objs.StructConst == nil {
t.Error("Loading returns an object with nil variables")
}
}

func TestTypes(t *testing.T) {
Expand Down
20 changes: 19 additions & 1 deletion cmd/bpf2go/test/test_bpfeb.go

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

20 changes: 19 additions & 1 deletion cmd/bpf2go/test/test_bpfel.go

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

16 changes: 15 additions & 1 deletion docs/examples/getting_started/counter_bpfeb.go

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

16 changes: 15 additions & 1 deletion docs/examples/getting_started/counter_bpfel.go

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

Loading