Skip to content

Commit ba2282a

Browse files
committed
docs: briefly document Variable and Collection.Variables
Add a little blurb to distinguish VariableSpec and Variable and clarify the difference between accessing globals before and after loading. Signed-off-by: Timo Beckers <[email protected]>
1 parent b39270a commit ba2282a

3 files changed

Lines changed: 48 additions & 7 deletions

File tree

docs/ebpf/concepts/global-variables.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,40 @@ as the user space application. More on that in a future section.
9292
its value is actually provided by user space. The `volatile` qualifier
9393
doesn't change the variable's semantics.
9494

95-
:simple-go: Next, in user space, initialize `global_u16` to 9000:
95+
### Before Loading: Using VariableSpec
96+
97+
For interacting with global variables before loading the BPF program into the
98+
kernel, use the methods on its {{ godoc('VariableSpec') }} found in {{
99+
godoc('CollectionSpec.Variables') }} or injected using {{ godoc('LoadAndAssign')
100+
}}. This ensures the variable is populated before the BPF program has a chance
101+
to execute.
102+
103+
:simple-go: In user space, initialize `global_u16` to 9000:
96104

97105
{{ go_example('DocVariablesSetGlobalU16') }}
98106

99-
Dry-running `global_example()` a few times results in our value increasing on
107+
Dry-running `global_example()` a few times results in the value increasing on
100108
every invocation:
101109

102110
{{ go_example('DocVariablesSetGlobalRun') }}
103111

112+
Once a CollectionSpec has been loaded into the kernel, further modifications
113+
to a VariableSpec are ineffectual.
114+
115+
### After Loading: Using Variable
116+
117+
After loading the BPF program into the kernel, accessing global variables from
118+
user space can be done through the {{ godoc('Variable') }} abstraction. These
119+
can be injected into an object using {{ godoc('LoadAndAssign') }}, or found in
120+
the {{ godoc('Collection.Variables') }} field.
121+
122+
:simple-go: Building on the previous example, read the incremented variable
123+
using {{ godoc('Variable.Get') }}:
124+
125+
{{ go_example('DocVariablesGetGlobalU16') }}
126+
127+
Modify the Variable at runtime using {{ godoc('Variable.Set') }}.
128+
104129
## Internal/Hidden Global Variables
105130

106131
By default, all global variables described in an ELF's data sections are exposed

docs/examples/variables/main.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
6+
"github.com/cilium/ebpf"
7+
)
48

59
func main() {
610
DocVariablesSetConst()
@@ -62,16 +66,16 @@ func DocVariablesSetGlobal() {
6266
}
6367
// }
6468

65-
var obj variablesPrograms
66-
if err := spec.LoadAndAssign(&obj, nil); err != nil {
69+
coll, err := ebpf.NewCollection(spec)
70+
if err != nil {
6771
panicf("loading BPF program: %s", err)
6872
}
6973

7074
fmt.Println("Running program with global_u16 set to", set)
7175

7276
// DocVariablesSetGlobalRun {
7377
for range 3 {
74-
ret, _, err := obj.GlobalExample.Test(make([]byte, 15))
78+
ret, _, err := coll.Programs["global_example"].Test(make([]byte, 15))
7579
if err != nil {
7680
panicf("running BPF program: %s", err)
7781
}
@@ -84,6 +88,17 @@ func DocVariablesSetGlobal() {
8488
// BPF program returned 9001
8589
// BPF program returned 9002
8690
// }
91+
92+
// DocVariablesGetGlobalU16 {
93+
var global_u16 uint16
94+
if err := coll.Variables["global_u16"].Get(&global_u16); err != nil {
95+
panicf("getting variable: %s", err)
96+
}
97+
fmt.Println("Variable global_u16 is now", global_u16)
98+
99+
// Output:
100+
// Variable global_u16 is now 9003
101+
// }
87102
}
88103

89104
func panicf(format string, args ...interface{}) {

docs/includes/glossary.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
*[ELF]: Executable and Linkable Format, a container format used for compiled eBPF programs.
77
*[Spec]: Unrealized blueprint of an eBPF resource, e.g. MapSpec, ProgramSpec, btf.Spec.
88
*[CollectionSpec]: Bundle of ProgramSpecs, MapSpecs and a btf.Spec. Direct result of loading an eBPF ELF.
9-
*[VariableSpec]: Setter/getter for a global variable declared in an eBPF program.
9+
*[VariableSpec]: Accessor for a global variable declared in an eBPF program.
1010
*[Collection]: Bundle of Maps and Programs that were loaded into the kernel. Direct result of instantiating (loading into the kernel) a CollectionSpec.
11+
*[Variable]: Accessor for a global variable declared in an eBPF program, used after loading.
1112
*[bpffs]: Birtual filesystem for 'pinning' references to eBPF resources in an familiar file hierarchy. Usually mounted at /sys/fs/bpf, but many individual instances can be mounted.
1213
*[helper]: A piece of logic provided by the kernel. Read a map value, redirect a packet, etc.
1314
*[kfunc]: An extensible evolution of the BPF helper mechanism. Can be dynamically provided by kernel modules. Not specified in UAPI.

0 commit comments

Comments
 (0)