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

     1  package link
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"net"
     7  	"testing"
     8  
     9  	"github.com/go-quicktest/qt"
    10  
    11  	"github.com/cilium/ebpf"
    12  	"github.com/cilium/ebpf/internal/testutils"
    13  	"github.com/cilium/ebpf/internal/unix"
    14  )
    15  
    16  func TestAttachTCX(t *testing.T) {
    17  	testutils.SkipOnOldKernel(t, "6.6", "TCX link")
    18  
    19  	prog := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNone, "")
    20  	link, _ := mustAttachTCX(t, prog, ebpf.AttachTCXIngress)
    21  
    22  	testLink(t, link, prog)
    23  }
    24  
    25  func TestTCXAnchor(t *testing.T) {
    26  	testutils.SkipOnOldKernel(t, "6.6", "TCX link")
    27  
    28  	a := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNone, "")
    29  	b := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNone, "")
    30  
    31  	linkA, iface := mustAttachTCX(t, a, ebpf.AttachTCXEgress)
    32  
    33  	programInfo, err := a.Info()
    34  	qt.Assert(t, qt.IsNil(err))
    35  	programID, _ := programInfo.ID()
    36  
    37  	linkInfo, err := linkA.Info()
    38  	qt.Assert(t, qt.IsNil(err))
    39  	linkID := linkInfo.ID
    40  
    41  	for _, anchor := range []Anchor{
    42  		Head(),
    43  		Tail(),
    44  		BeforeProgram(a),
    45  		BeforeProgramByID(programID),
    46  		AfterLink(linkA),
    47  		AfterLinkByID(linkID),
    48  	} {
    49  		t.Run(fmt.Sprintf("%T", anchor), func(t *testing.T) {
    50  			linkB, err := AttachTCX(TCXOptions{
    51  				Program:   b,
    52  				Attach:    ebpf.AttachTCXEgress,
    53  				Interface: iface,
    54  				Anchor:    anchor,
    55  			})
    56  			qt.Assert(t, qt.IsNil(err))
    57  			qt.Assert(t, qt.IsNil(linkB.Close()))
    58  		})
    59  	}
    60  }
    61  
    62  func TestTCXExpectedRevision(t *testing.T) {
    63  	testutils.SkipOnOldKernel(t, "6.6", "TCX link")
    64  
    65  	iface, err := net.InterfaceByName("lo")
    66  	qt.Assert(t, qt.IsNil(err))
    67  
    68  	_, err = AttachTCX(TCXOptions{
    69  		Program:          mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNone, ""),
    70  		Attach:           ebpf.AttachTCXEgress,
    71  		Interface:        iface.Index,
    72  		ExpectedRevision: math.MaxUint64,
    73  	})
    74  	qt.Assert(t, qt.ErrorIs(err, unix.ESTALE))
    75  }
    76  
    77  func mustAttachTCX(tb testing.TB, prog *ebpf.Program, attachType ebpf.AttachType) (Link, int) {
    78  	iface, err := net.InterfaceByName("lo")
    79  	qt.Assert(tb, qt.IsNil(err))
    80  
    81  	link, err := AttachTCX(TCXOptions{
    82  		Program:   prog,
    83  		Attach:    attachType,
    84  		Interface: iface.Index,
    85  	})
    86  	qt.Assert(tb, qt.IsNil(err))
    87  	tb.Cleanup(func() { qt.Assert(tb, qt.IsNil(link.Close())) })
    88  
    89  	return link, iface.Index
    90  }