-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add bpf2go support and wrappers for ebpf.Variable generation
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 <[email protected]>
- Loading branch information
1 parent
59808a1
commit f6a94c5
Showing
7 changed files
with
164 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters