-
Notifications
You must be signed in to change notification settings - Fork 707
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
bpf2go: generate assignment structs and Go types for Variables and VariableSpecs #1610
Conversation
d68d6f0
to
96cd686
Compare
93ccbd7
to
e3598eb
Compare
Had to repeat commit n.3, regenerating targets due to other changes. Now should be good to review. Actual line changes should not be that much, the others are all auto-generated stuff 🤞 |
Right now, maps key and value types are emitted. We should consider generating types exposed via variables by default. E.g.
Explicitly adding |
60a5ecc
to
e4bfeaf
Compare
For this, I just pushed 3 new commits for which I'd love to receive further feedback 🙏 The main doubt I had concerns a new function shouldEmit() I just introduced.
For this reason, I'd like to prevent this behavior from bpf2go perspective, as I think it is mainly related to a bpf2go behavior rather than the btf logic itself. |
While reviewing cilium#1610, I noticed we were generating VariableSpecs for variables named 'unused' in our examples. There had to be a better way to get their BTF info into the ELF. I thoroughly checked all map types present in Linux 6.12 for any validation code on attr->value_size, and only RingBuf and Arena maps require value_size to be zero at all times. Special-case PerfEventArray so type annotations aren't limited to 4 bytes. Signed-off-by: Timo Beckers <timo@isovalent.com>
While reviewing #1610, I noticed we were generating VariableSpecs for variables named 'unused' in our examples. There had to be a better way to get their BTF info into the ELF. I thoroughly checked all map types present in Linux 6.12 for any validation code on attr->value_size, and only RingBuf and Arena maps require value_size to be zero at all times. Special-case PerfEventArray so type annotations aren't limited to 4 bytes. Signed-off-by: Timo Beckers <timo@isovalent.com>
I've been polishing this a bit to get it into shape for the release. Please hold off on pushing any changes here. |
e4bfeaf
to
c1e865d
Compare
@smagnani96 I've done some polishing and squashed this down into a few commits. In the future, please regenerate testdata in the same commit as when codegen/testdata changes are made, otherwise commits can't be individually reverted and rebasing becomes painful. I've mostly rewritten the functionality related to CollectGlobalTypes and added some tests. I had to get hands-on to familiarize myself with the problem, only to realize it's all very finnicky and the existing code was sort of broken, so doing this through code review would've been too time-consuming. @mejedi PTAL so this can go into this week's release, see individual commits. Upon closer inspection, I found several flaws in |
Signed-off-by: Timo Beckers <timo@isovalent.com>
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 <simone.magnani@isovalent.com>
baeed3d
to
7b51a04
Compare
This commit generates Go type declarations for global variables declared in a C BPF program. Some improvements to CollectGlobalTypes were due: - types are now deduplicated by name - types are now selected based on allow-list instead of explicitly rejecting Datasec and Int - the output is sorted by type name - arrays' inner types are now emitted to interact with Variable{Spec} Also added a new btf.QualifiedType(), a subset of btf.UnderlyingType() to remove all qualifiers up to a Typedef, since Typedefs are named, and typically directly point to the anonymous type they alias. Typedefs should only be stripped for evaluating the concrete underlying type, and they should be included when yielding types to the Go renderer. Signed-off-by: Timo Beckers <timo@isovalent.com> Co-authored-by: Simone Magnani <simone.magnani@isovalent.com>
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 <simone.magnani@isovalent.com>
7b51a04
to
00fcefc
Compare
Congrats on the release! I love the changes in |
Commit messages should (hopefully) describe the introduced changes. In brief:
add bpf2go basic support to emit Variables and VariableSpecs
: as discussed in the RFC [RFC] - wrappers for type-safe ebpf.Variable #1543, this commit adds the minimal support to bpf2go for emittingVariables
andVariableSpecs
. Introduced in Collection: add Variables field to interact with global variables #1572,Variables
are always emitted for everyVariableSpec
in theCollectionSpec
, despite its usage being supported or not (kernel >=v5.5).fix typo in comment when bpf2go emits bpfProgramSpecs
: fixed a typo in a bpf2go-generated comment (bpfSpecs
rather thanbpfProgramSpecs
)regenerate all TARGETS in main
: plain execution ofmake
, to regenerate all the targets after (1).add bpf2go test emit-and-load specs+bpf objects
: extendedapi_test.go
to check also for Variables to be loaded, and all the{Map,Variable,Program}Specs
to be loaded. Introduced new test inoutput_test.go
to verify that the generated bpf2go output is as expected for every spec and bpf object.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.btf: handle btf.Var in writeTypeLit
: reuses the current logic to handle the btf.Var type while writing the output via GoFormatter.bpf2go: include btf.Var in CollectGlobalTypes
: include btf.Var from spec.Variables while calling the CollectGlobalTypes function.regenerate all TARGETS in main after btf.Var
: similarly as (2).RFC with discussion #1543.