File: docs_test.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 (111 lines) | stat: -rw-r--r-- 2,497 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
107
108
109
110
111
//go:build linux

package examples

import (
	"fmt"

	"github.com/cilium/ebpf"
)

func DocLoadCollectionSpec() {
	// Parse an ELF into a CollectionSpec.
	// bpf_prog.o is the result of compiling BPF C code.
	spec, err := ebpf.LoadCollectionSpec("bpf_prog.o")
	if err != nil {
		panic(err)
	}

	// Look up the MapSpec and ProgramSpec in the CollectionSpec.
	m := spec.Maps["my_map"]
	p := spec.Programs["my_prog"]
	// Note: We've omitted nil checks for brevity, take a look at
	// LoadAndAssign for an automated way of checking for maps/programs.

	// Inspect the map and program type.
	fmt.Println(m.Type, p.Type)

	// Print the map's key and value BTF types.
	fmt.Println(m.Key, m.Value)

	// Print the program's instructions in a human-readable form,
	// similar to llvm-objdump -S.
	fmt.Println(p.Instructions)
}

func DocNewCollection() {
	spec, err := ebpf.LoadCollectionSpec("bpf_prog.o")
	if err != nil {
		panic(err)
	}

	// Instantiate a Collection from a CollectionSpec.
	coll, err := ebpf.NewCollection(spec)
	if err != nil {
		panic(err)
	}
	// Close the Collection before the enclosing function returns.
	defer coll.Close()

	// Obtain a reference to 'my_map'.
	m := coll.Maps["my_map"]

	// Set map key '1' to value '2'.
	if err := m.Put(uint32(1), uint64(2)); err != nil {
		panic(err)
	}
}

// DocLoadAndAssignObjs {
type myObjs struct {
	MyMap  *ebpf.Map     `ebpf:"my_map"`
	MyProg *ebpf.Program `ebpf:"my_prog"`
}

func (objs *myObjs) Close() error {
	if err := objs.MyMap.Close(); err != nil {
		return err
	}
	if err := objs.MyProg.Close(); err != nil {
		return err
	}
	return nil
}

// }

func DocLoadAndAssign() {
	spec, err := ebpf.LoadCollectionSpec("bpf_prog.o")
	if err != nil {
		panic(err)
	}

	// Insert only the resources specified in 'obj' into the kernel and assign
	// them to their respective fields. If any requested resources are not found
	// in the ELF, this will fail. Any errors encountered while loading Maps or
	// Programs will also be returned here.
	var objs myObjs
	if err := spec.LoadAndAssign(&objs, nil); err != nil {
		panic(err)
	}
	defer objs.Close()

	// Interact with MyMap through the custom struct.
	if err := objs.MyMap.Put(uint32(1), uint64(2)); err != nil {
		panic(err)
	}
}

func DocBTFTypeByName() {
	spec, err := ebpf.LoadCollectionSpec("bpf_prog.o")
	if err != nil {
		panic(err)
	}

	// Look up the __64 type declared in linux/bpf.h.
	t, err := spec.Types.AnyTypeByName("__u64")
	if err != nil {
		panic(err)
	}
	fmt.Println(t)
}