-
Notifications
You must be signed in to change notification settings - Fork 682
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
add bpf2go support and wrappers for ebpf.Variable generation #1543
base: main
Are you sure you want to change the base?
Conversation
bbe1aba
to
799fef7
Compare
799fef7
to
d978138
Compare
fa76e79
to
71e3029
Compare
71e3029
to
5137cbe
Compare
2cf61df
to
9cf6f9f
Compare
This PR has been rebased on https://github.com/ti-mo/ebpf/tree/tb/ebpf-variables. |
9cf6f9f
to
0f69c00
Compare
…ndler Signed-off-by: Timo Beckers <timo@isovalent.com>
Signed-off-by: Timo Beckers <timo@isovalent.com>
…iables Signed-off-by: Timo Beckers <timo@isovalent.com>
Signed-off-by: Timo Beckers <timo@isovalent.com>
Signed-off-by: Timo Beckers <timo@isovalent.com>
0f69c00
to
cf717a0
Compare
Signed-off-by: Simone Magnani <simone.magnani@isovalent.com>
8486b44
to
f6a94c5
Compare
This commit introduces support and wrappers around ebpf.Variable. An ebpf.Variable does not define per-se wrappers method to automatically infer the underlying data type and handle typed data in Go. In fact, it relies on parameters passed to the `Variable.Get` and `Variable.Set` APIs. This commit allows `bpf2go` to emit `VariableSpec` and `Variable` in the generated code. In addition, a new set of wrapper methods are created to provide support for typed method around variables. To do so, this commit enables emitting `Dataspec`, which so far was not emitted. However, it modifies the current implementation so that for each variable in `Dataspec` the needed support is provided. Supposing to have an ebpf program that defines the following two global variables: ```c __u64 pkt_count = 0; const __u32 ro_variable = 1; char var_msg[] = "Hello World!"; ``` The steps performed during the Go code generation are as follows: 1. An alias with the variable name is generated in the Go file: ```go type PktCountType = uint64 type RoVariable = uint32 type VarMsgType = [16]int8 ``` 2. A new type definition (non-alias, need new methods) for the variables is performed: ```go type PktCount ebpf.Variable type RoVariable ebpf.Variable type VarMsg ebpf.Variable ``` 3. The `.Get` methods for all types are generated: ```go func (v *PktCount) Get() (PktCountType, error) { var ret PktCountType return ret, (*ebpf.Variable)(v).Get(&ret) } func (v *RoVariable) Get() (RoVariableType, error) { var ret RoVariableType return ret, (*ebpf.Variable)(v).Get(&ret) } func (v *VarMsg) Get() (VarMsgType, error) { var ret VarMsgType return ret, (*ebpf.Variable)(v).Get(&ret) } ``` 4. The `.Set` methods for the non read-only variables are generated: ```go func (v *PktCount) Set(val PktCountType) error { return (*ebpf.Variable)(v).Set(val) } func (v *VarMsg) set(val VarMsgType) error { return (*ebpf.Variable)(v).Set(ret) } ``` 4. In case the type alias is supported by the atomic package, and then also supported by `ebpf.Variable` (int32, uint32, int64, uint64), then an additional `AtomicRef` method is created for the non read-only variables to get an reference to the underlying data. ```go func (v *PktCount) AtomicRef() (*ebpf.Uint64, error) { ret, _ := (*ebpf.Variable)(v).AtomicUint64() return ret } ``` From the user perspective, ignoring the error catching, the following operations can be performed: ```go ... objs := bpfObjects{} err := loadBpfObjects(&objs, nil) ... // ref kept and can be used multiple times aPktCount := objs.PktCount.AtomicRef() err := aPktCount.Load() aPktCount.Store(rand.Uint64()) ... vRoVariable, _ := objs.RoVariable.Get() ... varMsg, _ := objs.VarMsg.Get() varMsg[0] = 'B' err := objs.VarMsg.Set(varMsg) ``` Signed-off-by: Simone Magnani <simone.magnani@isovalent.com>
f6a94c5
to
fb547c3
Compare
It probably makes sense to land basic
I see huge value in a type-safe Is it possible to devise an approach that works with |
Do you suggest to change this PR to provide basic VariableSpecs support in bpf2go or should I open a new one? Concerning the type-safe API, I can keep exploring solutions. I tried with generics with no luck, but maybe there's still a way...AFAIU CollectionSpec would also need to be a generic holding maps and variables each of different generic types. |
This commit introduces support and wrappers around ebpf.Variable.
An ebpf.Variable does not define per-se wrappers method to automatically
infer the underlying data type and handle typed data in Go. In fact, it
relies on parameters passed to the
Variable.Get
andVariable.Set
APIs.This commit allows
bpf2go
to emitVariableSpec
andVariable
in thegenerated code. In addition, a new set of wrapper methods are created
to provide support for typed method around variables. To do so, this
commit enables emitting
Dataspec
, which so far was not emitted. However,it modifies the current implementation so that for each variable in
Dataspec
the needed support is provided.
Supposing to have an ebpf program that defines the following two global variables:
The steps performed during the Go code generation are as follows:
.Get
methods for all types are generated:.Set
methods for the non read-only variables are generated:also supported by
ebpf.Variable
(int32, uint32, int64, uint64), thenan additional
AtomicRef
method is created for the non read-onlyvariables to get an reference to the underlying data.
From the user perspective, ignoring the error catching, the following
operations can be performed:
Suggestions/Comments are very welcome. For general discussion, I've opened the issue (:arrow_down:).
This PR relies and is rebased on https://github.com/ti-mo/ebpf/tree/tb/ebpf-variables, not yet merged but planned. Additionally, I'd need the support introduced in my 1st commit 59808a1.
The only new commit that should be referred in this PR is the latest f6a94c5.
My old Variable API proposal prior to https://github.com/ti-mo/ebpf/tree/tb/ebpf-variables are kept here.
Fixes: #1542