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

Conversation

smagnani96
Copy link
Contributor

Commit messages should (hopefully) describe the introduced changes. In brief:

  1. add bpf2go basic support to emit Variables and VariableSpecs: as discussed in the reference PR*, this commit adds the minimal support to bpf2go for emitting Variables and VariableSpecs. Introduced in Collection: add Variables field to interact with global variables #1572, Variables are always emitted for every VariableSpec in the CollectionSpec, despite its usage being supported or not (kernel >=v5.5).
  2. fix typo in comment when bpf2go emits bpfProgramSpecs: fixed a typo in a bpf2go-generated comment (bpfSpecs rather than bpfProgramSpecs)
  3. regenerate all TARGETS in main: plain execution of make, to regenerate all the targets after (1).
  4. add bpf2go test emit-and-load specs+bpf objects: extended api_test.go to check also for Variables to be loaded, and all the {Map,Variable,Program}Specs to be loaded. Introduced new test in output_test.go to verify that the generated bpf2go output is as expected for every spec and bpf object.
  5. simplify and update tcx example to use Variable API: showcase the new Variable API in the TCX example. Back then when we introduced it, we wanted to use global variables, but there were no support for that, so we used bpf maps for counting packets.

*Reference PR with the discussion: #1543

This commit introduces the basic support in bpf2go to emit VariableSpecs
and Variables. Similarly as for maps and programs, the generated
`bpf_bpfe{b,l}.go` will contain `bpfVariableSpecs` in the `bpfSpecs` object,
and `bpfVariables` in the `bpfSpecs` object. The former represents variables
before being loaded in the kernel (in their respective data sections), while
the latter is used to interact with loaded variables. As for maps and
programs, these two sections are emitted (empty) even when no variables are
specified in the source bpf code.
Interactions with these objects, as introduced in previous commits, should
look similar to the following:

```go
spec, err := loadBpf()
// Error handling.

err := spec.Variables["pkt_count"].Set(uint64(2))
// Error handling, attach progra.

objs := bpfObjects{}
err := spec.LoadAndAssign(&objs, nil)
// Error handling, defer close.

var pktCount uint64
err := objs.PktCount.Get(&pktCount)
// Error handling.
```

Signed-off-by: Simone Magnani <[email protected]>
This commit fixes a typo in the comments of `output.tpl` that causes
bpf2go to emit a commit with `bpfSpecs` rather that `bpfProgramSpecs` when
referring to the `bpfProgramSpecs` struct.

Signed-off-by: Simone Magnani <[email protected]>
This commit simply runs `make` to regenerate all the bpf2go targets in the
cmd, docs, and examples directories. This is useful so that in the upcoming
commit we can add a simple test in `cmd/bpf2go/test/test_bpfe{b,l}.go` to
make sure that a Variable/VariableSpec is emitted and loaded.

Signed-off-by: Simone Magnani <[email protected]>
This commit extends `api_test.go` to check whether specs and bpf objects
are correctly loaded. It also accounts for the new Variable/VariableSpec
in the test.
In addition, this commit adds a bpf2go test in `output_test.go` to verify
that all the listed bpf objects (i.e., variables, maps and programs) are
emitted. The tests looks for the output program, stored in a buffer, which
should contain the expected declarations.

This commits introduces further checks in the `api_test.go`

Signed-off-by: Simone Magnani <[email protected]>
This commit updates the `example/tcx` program to use the new Variable API
rather than traditional bpf maps. The example already depends on `bpf_link`,
which requires kernel >= v6.6, therefore the Variable API is already
supported and working (since v5.5).

Signed-off-by: Simone Magnani <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant