forked from aquasecurity/libbpfgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (72 loc) · 1.76 KB
/
main.go
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
package main
import "C"
import (
"encoding/binary"
"log"
"unsafe"
bpf "github.com/aquasecurity/libbpfgo"
)
func main() {
bpfModule, err := bpf.NewModuleFromFile("main.bpf.o")
if err != nil {
log.Fatal(err)
}
defer bpfModule.Close()
bpfModule.BPFLoadObject()
testerMap, err := bpfModule.GetMap("tester")
if err != nil {
log.Fatal(err)
}
// Get info about the "tester" map
infoTester, err := bpf.GetMapInfoByFD(testerMap.FileDescriptor())
if err != nil {
log.Fatal(err)
}
// Get a new BPFMapLow object pointing to the "tester" map
testerMapLow, err := bpf.GetMapByID(infoTester.ID)
if err != nil {
log.Fatal(err)
}
if testerMapLow.Name() != testerMap.Name() {
log.Fatal("Names do not match")
}
if testerMapLow.Type() != testerMap.Type() {
log.Fatal("Types do not match")
}
if testerMapLow.MaxEntries() != testerMap.MaxEntries() {
log.Fatal("Max entries do not match")
}
if testerMapLow.KeySize() != testerMap.KeySize() {
log.Fatal("Key sizes do not match")
}
if testerMapLow.ValueSize() != testerMap.ValueSize() {
log.Fatal("Value sizes do not match")
}
// Save a value in the "tester" map using the original BPFMap object
key1 := uint32(11)
value1 := uint32(1917)
key1Unsafe := unsafe.Pointer(&key1)
value1Unsafe := unsafe.Pointer(&value1)
err = testerMap.Update(key1Unsafe, value1Unsafe)
if err != nil {
log.Fatal(err)
}
// Get the value from the "tester" map using the new BPFMapLow object
v, err := testerMapLow.GetValue(key1Unsafe)
if err != nil {
log.Fatal(err)
}
if endian().Uint32(v) != value1 {
log.Fatal("Value mismatch")
}
}
func endian() binary.ByteOrder {
var i int32 = 0x01020304
u := unsafe.Pointer(&i)
pb := (*byte)(u)
b := *pb
if b == 0x04 {
return binary.LittleEndian
}
return binary.BigEndian
}