| 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 |
|
| 41 |
|
| 42 |
|
| 43 |
testCases := []testCase{ |
| 44 |
{ebpf.Kprobe, asm.FnMapLookupElem, nil, "3.19"}, |
| 45 |
{ebpf.SocketFilter, asm.FnKtimeGetCoarseNs, nil, "5.11"}, |
| 46 |
{ebpf.SchedCLS, asm.FnSkbVlanPush, nil, "4.3"}, |
| 47 |
{ebpf.Kprobe, asm.FnSkbVlanPush, ebpf.ErrNotSupported, "4.3"}, |
| 48 |
{ebpf.Kprobe, asm.FnSysBpf, ebpf.ErrNotSupported, "5.14"}, |
| 49 |
{ebpf.Syscall, asm.FnSysBpf, nil, "5.14"}, |
| 50 |
{ebpf.XDP, asm.FnJiffies64, nil, "5.5"}, |
| 51 |
{ebpf.XDP, asm.FnKtimeGetBootNs, nil, "5.7"}, |
| 52 |
{ebpf.SchedCLS, asm.FnSkbChangeHead, nil, "5.8"}, |
| 53 |
{ebpf.SchedCLS, asm.FnRedirectNeigh, nil, "5.10"}, |
| 54 |
{ebpf.SchedCLS, asm.FnSkbEcnSetCe, nil, "5.1"}, |
| 55 |
{ebpf.SchedACT, asm.FnSkAssign, nil, "5.6"}, |
| 56 |
{ebpf.SchedACT, asm.FnFibLookup, nil, "4.18"}, |
| 57 |
{ebpf.Kprobe, asm.FnFibLookup, ebpf.ErrNotSupported, "4.18"}, |
| 58 |
{ebpf.CGroupSockAddr, asm.FnGetsockopt, nil, "5.8"}, |
| 59 |
{ebpf.CGroupSockAddr, asm.FnSkLookupTcp, nil, "4.20"}, |
| 60 |
{ebpf.CGroupSockAddr, asm.FnGetNetnsCookie, nil, "5.7"}, |
| 61 |
{ebpf.CGroupSock, asm.FnGetNetnsCookie, nil, "5.7"}, |
| 62 |
{ebpf.Kprobe, asm.FnKtimeGetCoarseNs, ebpf.ErrNotSupported, "5.16"}, |
| 63 |
{ebpf.CGroupSockAddr, asm.FnGetCgroupClassid, nil, "5.7"}, |
| 64 |
{ebpf.Kprobe, asm.FnGetBranchSnapshot, nil, "5.16"}, |
| 65 |
{ebpf.SchedCLS, asm.FnSkbSetTstamp, nil, "5.18"}, |
| 66 |
{ebpf.CGroupSockopt, asm.FnSkStorageDelete, nil, "5.3"}, |
| 67 |
{ebpf.SkLookup, asm.FnSkcToUdp6Sock, nil, "5.9"}, |
| 68 |
{ebpf.Syscall, asm.FnSysClose, nil, "5.14"}, |
| 69 |
{ebpf.Netfilter, asm.FnCgrpStorageDelete, nil, "6.4"}, |
| 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 |
|
| 91 |
|
| 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 |
} |