Skip to content

Commit 65da3b9

Browse files
author
Dongsu Park
committed
bpf_test: add tests for loading modules with pinning config
To cover the test case of loading modules with pinning config, add more tests for loading module with specific parameters, and checking for actual pinned object under `/sys/fs/bpf`.
1 parent e6a185d commit 65da3b9

File tree

8 files changed

+63
-4
lines changed

8 files changed

+63
-4
lines changed

bpf_test.go

+44-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
package bpf
1818

1919
import (
20+
"os"
21+
"path/filepath"
2022
"testing"
2123

2224
"github.com/iovisor/gobpf/bcc"
2325
"github.com/iovisor/gobpf/elf"
26+
"github.com/iovisor/gobpf/pkg/bpffs"
2427
)
2528

2629
var simple1 string = `
@@ -109,6 +112,7 @@ func checkMaps(t *testing.T, b *elf.Module) {
109112
"dummy_array",
110113
"dummy_prog_array",
111114
"dummy_perf",
115+
"dummy_array_custom",
112116
}
113117

114118
if kernelVersion >= kernelVersion46 {
@@ -233,6 +237,22 @@ func checkSocketFilters(t *testing.T, b *elf.Module) {
233237
}
234238
}
235239

240+
func checkPinConfig(t *testing.T, expectedPaths []string) {
241+
for _, p := range expectedPaths {
242+
if fi, err := os.Stat(p); os.IsNotExist(err) || !fi.Mode().IsRegular() {
243+
t.Fatalf("pinned object %q not found", p)
244+
}
245+
}
246+
}
247+
248+
func checkPinConfigCleanup(t *testing.T, expectedPaths []string) {
249+
for _, p := range expectedPaths {
250+
if _, err := os.Stat(p); !os.IsNotExist(err) {
251+
t.Fatalf("pinned object %q is not cleaned up", p)
252+
}
253+
}
254+
}
255+
236256
func TestModuleLoadELF(t *testing.T) {
237257
var err error
238258
kernelVersion, err = elf.CurrentKernelVersion()
@@ -249,18 +269,40 @@ func TestModuleLoadELF(t *testing.T) {
249269
dummyELF = "./tests/dummy-46.o"
250270
}
251271

272+
var secParams = map[string]elf.SectionParams{
273+
"maps/dummy_array_custom": elf.SectionParams{
274+
PinPath: filepath.Join("gobpf-test", "testgroup1"),
275+
},
276+
}
277+
var closeOptions = map[string]elf.CloseOptions{
278+
"maps/dummy_array_custom": elf.CloseOptions{
279+
Unpin: true,
280+
PinPath: filepath.Join("gobpf-test", "testgroup1"),
281+
},
282+
}
283+
284+
if err := bpffs.Mount(); err != nil {
285+
t.Skipf("error mounting bpf fs, skipping test: %v", err)
286+
}
287+
252288
b := elf.NewModule(dummyELF)
253289
if b == nil {
254290
t.Fatal("prog is nil")
255291
}
256-
if err := b.Load(nil); err != nil {
292+
if err := b.Load(secParams); err != nil {
257293
t.Fatal(err)
258294
}
259-
defer b.Close()
295+
defer func() {
296+
if err := b.CloseExt(closeOptions); err != nil {
297+
t.Fatal(err)
298+
}
299+
checkPinConfigCleanup(t, []string{"/sys/fs/bpf/gobpf-test/testgroup1"})
300+
}()
260301

261302
checkMaps(t, b)
262303
checkProbes(t, b)
263304
checkCgroupProgs(t, b)
264305
checkSocketFilters(t, b)
265306
checkTracepointProgs(t, b)
307+
checkPinConfig(t, []string{"/sys/fs/bpf/gobpf-test/testgroup1"})
266308
}

elf/include/bpf.h

+7
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,11 @@ typedef struct bpf_map_def {
617617
char namespace[BUF_SIZE_MAP_NS];
618618
} bpf_map_def;
619619

620+
enum bpf_pin_type {
621+
PIN_NONE = 0,
622+
PIN_OBJECT_NS,
623+
PIN_GLOBAL_NS,
624+
PIN_CUSTOM_NS,
625+
};
626+
620627
#endif /* _UAPI__LINUX_BPF_H__ */

tests/build

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
set -euo pipefail
44

5-
clang -O2 -emit-llvm -c dummy.c -o - | llc -march=bpf -filetype=obj -o dummy.o
5+
DUMMY_SRC=dummy.c
6+
DUMMY_OBJ=dummy.o
7+
8+
clang -O2 -emit-llvm -c ${DUMMY_SRC} -o - | llc -march=bpf -filetype=obj -o ${DUMMY_OBJ}
69
for kernel in 46 48 410; do
7-
clang -DKERNEL_VERSION="${kernel}" -O2 -emit-llvm -c dummy.c -o - | llc -march=bpf -filetype=obj -o "dummy-${kernel}.o"
10+
clang -DKERNEL_VERSION="${kernel}" -O2 -emit-llvm -c ${DUMMY_SRC} -o - | llc -march=bpf -filetype=obj -o "dummy-${kernel}.o"
811
done

tests/dummy-410.o

392 Bytes
Binary file not shown.

tests/dummy-46.o

392 Bytes
Binary file not shown.

tests/dummy-48.o

392 Bytes
Binary file not shown.

tests/dummy.c

+7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ struct bpf_map_def SEC("maps/dummy_cgroup_array") dummy_cgroup_array = {
7272
};
7373
#endif
7474

75+
struct bpf_map_def SEC("maps/dummy_array_custom") dummy_array_custom = {
76+
.type = BPF_MAP_TYPE_ARRAY,
77+
.key_size = sizeof(int),
78+
.value_size = sizeof(unsigned int),
79+
.max_entries = 128,
80+
.pinning = PIN_CUSTOM_NS,
81+
};
7582

7683
SEC("kprobe/dummy")
7784
int kprobe__dummy(struct pt_regs *ctx)

tests/dummy.o

392 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)