forked from gojue/ebpfmanager
/
main.go
97 lines (81 loc) · 2.13 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
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"github.com/cilium/ebpf"
"github.com/sirupsen/logrus"
manager "github.com/adubovikov/ebpfmanager"
)
type TestData struct {
Input uint32
Output uint32
}
func (td TestData) String() string {
return fmt.Sprintf("{ Input:%v Output:%v }", td.Input, td.Output)
}
var testDataKey = uint32(1)
var testData = []TestData{
{2, 4},
{10, 20},
{42, 128},
{42, 84},
}
func main() {
// Initialize the manager
var m = &manager.Manager{}
if err := m.Init(recoverAssets()); err != nil {
logrus.Fatal(err)
}
// Get map used to send tests
testMap, found, err := m.GetMap("my_func_test_data")
if !found || err != nil {
logrus.Fatalf("couldn't retrieve my_func_test_data %v", err)
}
// Get xdp program used to trigger the tests
testProgs, found, err := m.GetProgram(
manager.ProbeIdentificationPair{
EbpfFuncName: "my_func_test",
},
)
if !found || err != nil {
logrus.Fatalf("couldn't retrieve my_func_test %v", err)
}
testProg := testProgs[0]
// Run test
runtTest(testMap, testProg)
// Run benchmark
runtBenchmark(testMap, testProg)
}
func runtTest(testMap *ebpf.Map, testProg *ebpf.Program) {
logrus.Println("Running tests ...")
for _, data := range testData {
// insert data
testMap.Put(testDataKey, data)
// Trigger test - (the 14 bytes is for the minimum packet size required to test an XDP program)
outLen, _, err := testProg.Test(make([]byte, 14))
if err != nil {
logrus.Fatal(err)
}
if outLen == 0 {
logrus.Printf("%v - PASS", data)
} else {
logrus.Printf("%v - FAIL (checkout /sys/kernel/debug/tracing/trace_pipe to see the logs)", data)
}
}
}
func runtBenchmark(testMap *ebpf.Map, testProg *ebpf.Program) {
logrus.Println("Running benchmark ...")
for _, data := range testData {
// insert data
testMap.Put(testDataKey, data)
// Trigger test
outLen, duration, err := testProg.Benchmark(make([]byte, 14), 1000, nil)
if err != nil {
logrus.Fatal(err)
}
if outLen == 0 {
logrus.Printf("%v - PASS (duration: %v)", data, duration)
} else {
logrus.Printf("%v - benchmark FAILED (checkout /sys/kernel/debug/tracing/trace_pipe to see the logs)", data)
}
}
}