github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/features/prog_test.go (about)

     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/fdtrace"
    14  )
    15  
    16  func TestMain(m *testing.M) {
    17  	fdtrace.TestMain(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  	// They 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  	}
    67  
    68  	for _, tc := range testCases {
    69  		t.Run(fmt.Sprintf("%s/%s", tc.prog.String(), tc.helper.String()), func(t *testing.T) {
    70  			feature := fmt.Sprintf("helper %s for program type %s", tc.helper.String(), tc.prog.String())
    71  
    72  			testutils.SkipOnOldKernel(t, tc.version, feature)
    73  
    74  			err := HaveProgramHelper(tc.prog, tc.helper)
    75  			if !errors.Is(err, tc.expected) {
    76  				t.Fatalf("%s/%s: %v", tc.prog.String(), tc.helper.String(), err)
    77  			}
    78  
    79  		})
    80  
    81  	}
    82  }
    83  
    84  func TestHelperProbeNotImplemented(t *testing.T) {
    85  	// Currently we don't support probing helpers for Tracing, Extension, LSM and StructOps programs.
    86  	// For each of those test the availability of the FnMapLookupElem helper and expect it to fail.
    87  	for _, pt := range []ebpf.ProgramType{ebpf.Tracing, ebpf.Extension, ebpf.LSM, ebpf.StructOps} {
    88  		t.Run(pt.String(), func(t *testing.T) {
    89  			if err := HaveProgramHelper(pt, asm.FnMapLookupElem); err == nil {
    90  				t.Fatal("Expected an error")
    91  			}
    92  		})
    93  	}
    94  }