Debian Codegraph @

features/prog_test.go

raw symbols SCIP
1 package features
2
3 import (
4 "errors"
5 "fmt"
6 "math"
7 "testing"
8
9 "github.com/cilium/ebpf"
10 "github.com/cilium/ebpf/asm"
11 "github.com/cilium/ebpf/internal"
12 "github.com/cilium/ebpf/internal/testutils"
13 "github.com/cilium/ebpf/internal/testutils/testmain"
14 )
15
16 func TestMain(m *testing.M) {
17 testmain.Run(m)
18 }
19
20 func TestHaveProgramType(t *testing.T) {
21 testutils.CheckFeatureMatrix(t, haveProgramTypeMatrix)
22 }
23
24 func TestHaveProgramTypeInvalid(t *testing.T) {
25 if err := HaveProgramType(ebpf.ProgramType(math.MaxUint32)); err == nil {
26 t.Fatal("Expected an error")
27 } else if errors.Is(err, internal.ErrNotSupported) {
28 t.Fatal("Got ErrNotSupported:", err)
29 }
30 }
31
32 func TestHaveProgramHelper(t *testing.T) {
33 type testCase struct {
34 prog ebpf.ProgramType
35 helper asm.BuiltinFunc
36 expected error
37 version string
38 }
39
40 // Referencing linux kernel commits to track the kernel version required to pass these test cases.
41 // These cases are derived from libbpf's selftests and helper/prog combinations that are
42 // probed for in cilium/cilium.
43 testCases := []testCase{
44 {ebpf.Kprobe, asm.FnMapLookupElem, nil, "3.19"}, // d0003ec01c66
45 {ebpf.SocketFilter, asm.FnKtimeGetCoarseNs, nil, "5.11"}, // d05512618056
46 {ebpf.SchedCLS, asm.FnSkbVlanPush, nil, "4.3"}, // 4e10df9a60d9
47 {ebpf.Kprobe, asm.FnSkbVlanPush, ebpf.ErrNotSupported, "4.3"}, // 4e10df9a60d9
48 {ebpf.Kprobe, asm.FnSysBpf, ebpf.ErrNotSupported, "5.14"}, // 79a7f8bdb159
49 {ebpf.Syscall, asm.FnSysBpf, nil, "5.14"}, // 79a7f8bdb159
50 {ebpf.XDP, asm.FnJiffies64, nil, "5.5"}, // 5576b991e9c1
51 {ebpf.XDP, asm.FnKtimeGetBootNs, nil, "5.7"}, // 71d19214776e
52 {ebpf.SchedCLS, asm.FnSkbChangeHead, nil, "5.8"}, // 6f3f65d80dac
53 {ebpf.SchedCLS, asm.FnRedirectNeigh, nil, "5.10"}, // b4ab31414970
54 {ebpf.SchedCLS, asm.FnSkbEcnSetCe, nil, "5.1"}, // f7c917ba11a6
55 {ebpf.SchedACT, asm.FnSkAssign, nil, "5.6"}, // cf7fbe660f2d
56 {ebpf.SchedACT, asm.FnFibLookup, nil, "4.18"}, // 87f5fc7e48dd
57 {ebpf.Kprobe, asm.FnFibLookup, ebpf.ErrNotSupported, "4.18"}, // 87f5fc7e48dd
58 {ebpf.CGroupSockAddr, asm.FnGetsockopt, nil, "5.8"}, // beecf11bc218
59 {ebpf.CGroupSockAddr, asm.FnSkLookupTcp, nil, "4.20"}, // 6acc9b432e67
60 {ebpf.CGroupSockAddr, asm.FnGetNetnsCookie, nil, "5.7"}, // f318903c0bf4
61 {ebpf.CGroupSock, asm.FnGetNetnsCookie, nil, "5.7"}, // f318903c0bf4
62 {ebpf.Kprobe, asm.FnKtimeGetCoarseNs, ebpf.ErrNotSupported, "5.16"}, // 5e0bc3082e2e
63 {ebpf.CGroupSockAddr, asm.FnGetCgroupClassid, nil, "5.7"}, // 5a52ae4e32a6
64 {ebpf.Kprobe, asm.FnGetBranchSnapshot, nil, "5.16"}, // 856c02dbce4f
65 {ebpf.SchedCLS, asm.FnSkbSetTstamp, nil, "5.18"}, // 9bb984f28d5b
66 {ebpf.CGroupSockopt, asm.FnSkStorageDelete, nil, "5.3"}, // 6ac99e8f23d4
67 {ebpf.SkLookup, asm.FnSkcToUdp6Sock, nil, "5.9"}, // 0d4fad3e57df
68 {ebpf.Syscall, asm.FnSysClose, nil, "5.14"}, // 3abea089246f
69 {ebpf.Netfilter, asm.FnCgrpStorageDelete, nil, "6.4"}, // c4bcfb38a95e
70 }
71
72 for _, tc := range testCases {
73 t.Run(fmt.Sprintf("%s/%s", tc.prog.String(), tc.helper.String()), func(t *testing.T) {
74 feature := fmt.Sprintf("helper %s for program type %s", tc.helper.String(), tc.prog.String())
75
76 testutils.SkipOnOldKernel(t, tc.version, feature)
77
78 err := HaveProgramHelper(tc.prog, tc.helper)
79 testutils.SkipIfNotSupportedOnOS(t, err)
80 if !errors.Is(err, tc.expected) {
81 t.Fatalf("%s/%s: %v", tc.prog.String(), tc.helper.String(), err)
82 }
83
84 })
85
86 }
87 }
88
89 func TestHelperProbeNotImplemented(t *testing.T) {
90 // Currently we don't support probing helpers for Tracing, Extension, LSM and StructOps programs.
91 // For each of those test the availability of the FnMapLookupElem helper and expect it to fail.
92 for _, pt := range []ebpf.ProgramType{ebpf.Tracing, ebpf.Extension, ebpf.LSM, ebpf.StructOps} {
93 t.Run(pt.String(), func(t *testing.T) {
94 if err := HaveProgramHelper(pt, asm.FnMapLookupElem); err == nil {
95 t.Fatal("Expected an error")
96 }
97 })
98 }
99 }