-
Notifications
You must be signed in to change notification settings - Fork 697
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; 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 VarMsgType = [16]int8 ``` 2. A new type definition (non-alias, need new methods) for the variables is performed: ```go type PktCount ebpf.Variable type VarMsg ebpf.Variable ``` 3. The `.Get` and `.Set` methods for these types are generated: ```go func (v *PktCount) Get() (PktCountType, error) { var ret PktCountType return ret, (*ebpf.Variable)(v).Get(&ret) } func (v *PktCount) Set(val PktCountType) error { return (*ebpf.Variable)(v).Set(val) } func (v *VarMsg) Get() (VarMsgType, error) { var ret VarMsgType return ret, (*ebpf.Variable)(v).Get(&ret) } func (v *VarMsg) Set(val VarMsgType) error { return (*ebpf.Variable)(v).Set(val) } ``` 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 to get a reference to the underlying value while also performing atomic operations. ```go func (v *PktCount) AtomicRef() (*ebpf.Uint64, error) { return (*ebpf.Variable)(v).AtomicUint64() } ``` 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, err := objs.PktCount.AtomicRef() err := aPktCount.Load() aPktCount.Store(rand.Uint64()) ... varMsg, _ := objs.VarMsg.Get() varMsg[0] = 'B' err := objs.VarMsg.Set(varMsg) ``` Signed-off-by: Simone Magnani <[email protected]>
- Loading branch information
1 parent
99358ae
commit 0f69c00
Showing
7 changed files
with
166 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