File: main.go

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (106 lines) | stat: -rw-r--r-- 2,547 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main

import (
	"fmt"

	"github.com/cilium/ebpf"
)

func main() {
	DocVariablesSetConst()
	DocVariablesSetGlobal()
}

// Full example written to be displayed in its entirety, so is commented generously.
func DocVariablesSetConst() {
	// Load the object file from disk using a bpf2go-generated scaffolding.
	spec, err := loadVariables()
	if err != nil {
		panicf("loading CollectionSpec: %s", err)
	}

	// Set the 'const_u32' variable to 42 in the CollectionSpec.
	want := uint32(42) // (1)!
	if err := spec.Variables["const_u32"].Set(want); err != nil {
		panicf("setting variable: %s", err)
	}

	// Load the CollectionSpec.
	//
	// Note: modifying spec.Variables after this point is ineffectual!
	// Modifying *Spec resources does not affect loaded/running BPF programs.
	var obj variablesPrograms
	if err := spec.LoadAndAssign(&obj, nil); err != nil {
		panicf("loading BPF program: %s", err)
	}

	fmt.Println("Running program with const_u32 set to", want)

	// Dry-run the BPF program with an empty context.
	ret, _, err := obj.ConstExample.Test(make([]byte, 15)) // (2)!
	if err != nil {
		panicf("running BPF program: %s", err)
	}

	if ret != want {
		panicf("unexpected return value %d", ret)
	}

	fmt.Println("BPF program returned", ret)

	// Output:
	// Running program with const_u32 set to 42
	// BPF program returned 42
}

func DocVariablesSetGlobal() {
	spec, err := loadVariables()
	if err != nil {
		panicf("loading CollectionSpec: %s", err)
	}

	// DocVariablesSetGlobalU16 {
	set := uint16(9000)
	if err := spec.Variables["global_u16"].Set(set); err != nil {
		panicf("setting variable: %s", err)
	}
	// }

	coll, err := ebpf.NewCollection(spec)
	if err != nil {
		panicf("loading BPF program: %s", err)
	}

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

	// DocVariablesSetGlobalRun {
	for range 3 {
		ret, _, err := coll.Programs["global_example"].Test(make([]byte, 15))
		if err != nil {
			panicf("running BPF program: %s", err)
		}
		fmt.Println("BPF program returned", ret)
	}

	// Output:
	// Running program with global_u16 set to 9000
	// BPF program returned 9000
	// BPF program returned 9001
	// BPF program returned 9002
	// }

	// DocVariablesGetGlobalU16 {
	var global_u16 uint16
	if err := coll.Variables["global_u16"].Get(&global_u16); err != nil {
		panicf("getting variable: %s", err)
	}
	fmt.Println("Variable global_u16 is now", global_u16)

	// Output:
	// Variable global_u16 is now 9003
	// }
}

func panicf(format string, args ...interface{}) {
	panic(fmt.Sprintf(format, args...))
}