github.com/cilium/ebpf@v0.10.0/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  )
    14  
    15  func TestHaveProgramType(t *testing.T) {
    16  	testutils.CheckFeatureMatrix(t, haveProgramTypeMatrix)
    17  }
    18  
    19  func TestHaveProgramTypeInvalid(t *testing.T) {
    20  	if err := HaveProgramType(ebpf.ProgramType(math.MaxUint32)); err == nil {
    21  		t.Fatal("Expected an error")
    22  	} else if errors.Is(err, internal.ErrNotSupported) {
    23  		t.Fatal("Got ErrNotSupported:", err)
    24  	}
    25  }
    26  
    27  func TestHaveProgramHelper(t *testing.T) {
    28  	type testCase struct {
    29  		prog     ebpf.ProgramType
    30  		helper   asm.BuiltinFunc
    31  		expected error
    32  		version  string
    33  	}
    34  
    35  	// Referencing linux kernel commits to track the kernel version required to pass these test cases.
    36  	// They cases are derived from libbpf's selftests and helper/prog combinations that are
    37  	// probed for in cilium/cilium.
    38  	// Still missing since those helpers are not available in the lib yet, are:
    39  	// - Kprobe, GetBranchSnapshot
    40  	// - SchedCLS, SkbSetTstamp
    41  	// These two test cases depend on CI kernels supporting those:
    42  	// {ebpf.Kprobe, asm.FnKtimeGetCoarseNs, ebpf.ErrNotSupported, "5.16"}, // 5e0bc3082e2e
    43  	// {ebpf.CGroupSockAddr, asm.FnGetCgroupClassid, nil, "5.10"},    // b426ce83baa7
    44  	testCases := []testCase{
    45  		{ebpf.Kprobe, asm.FnMapLookupElem, nil, "3.19"},               // d0003ec01c66
    46  		{ebpf.SocketFilter, asm.FnKtimeGetCoarseNs, nil, "5.11"},      // d05512618056
    47  		{ebpf.SchedCLS, asm.FnSkbVlanPush, nil, "4.3"},                // 4e10df9a60d9
    48  		{ebpf.Kprobe, asm.FnSkbVlanPush, ebpf.ErrNotSupported, "4.3"}, // 4e10df9a60d9
    49  		{ebpf.Kprobe, asm.FnSysBpf, ebpf.ErrNotSupported, "5.14"},     // 79a7f8bdb159
    50  		{ebpf.Syscall, asm.FnSysBpf, nil, "5.14"},                     // 79a7f8bdb159
    51  		{ebpf.XDP, asm.FnJiffies64, nil, "5.5"},                       // 5576b991e9c1
    52  		{ebpf.XDP, asm.FnKtimeGetBootNs, nil, "5.7"},                  // 71d19214776e
    53  		{ebpf.SchedCLS, asm.FnSkbChangeHead, nil, "5.8"},              // 6f3f65d80dac
    54  		{ebpf.SchedCLS, asm.FnRedirectNeigh, nil, "5.10"},             // b4ab31414970
    55  		{ebpf.SchedCLS, asm.FnSkbEcnSetCe, nil, "5.1"},                // f7c917ba11a6
    56  		{ebpf.SchedACT, asm.FnSkAssign, nil, "5.6"},                   // cf7fbe660f2d
    57  		{ebpf.SchedACT, asm.FnFibLookup, nil, "4.18"},                 // 87f5fc7e48dd
    58  		{ebpf.Kprobe, asm.FnFibLookup, ebpf.ErrNotSupported, "4.18"},  // 87f5fc7e48dd
    59  		{ebpf.CGroupSockAddr, asm.FnGetsockopt, nil, "5.8"},           // beecf11bc218
    60  		{ebpf.CGroupSockAddr, asm.FnSkLookupTcp, nil, "4.20"},         // 6acc9b432e67
    61  		{ebpf.CGroupSockAddr, asm.FnGetNetnsCookie, nil, "5.7"},       // f318903c0bf4
    62  		{ebpf.CGroupSock, asm.FnGetNetnsCookie, nil, "5.7"},           // f318903c0bf4
    63  	}
    64  
    65  	for _, tc := range testCases {
    66  		t.Run(fmt.Sprintf("%s/%s", tc.prog.String(), tc.helper.String()), func(t *testing.T) {
    67  			feature := fmt.Sprintf("helper %s for program type %s", tc.helper.String(), tc.prog.String())
    68  
    69  			testutils.SkipOnOldKernel(t, tc.version, feature)
    70  
    71  			err := HaveProgramHelper(tc.prog, tc.helper)
    72  			if !errors.Is(err, tc.expected) {
    73  				t.Fatalf("%s/%s: %v", tc.prog.String(), tc.helper.String(), err)
    74  			}
    75  
    76  		})
    77  
    78  	}
    79  }