Skip to content

Commit

Permalink
link: add feature test for tcx
Browse files Browse the repository at this point in the history
As with many of the other existing APIs, this adds a feature test for
tcx to indicate to the user if the underlying kernel APIs are available
in the running kernel.

Signed-off-by: Robin Gögge <[email protected]>
  • Loading branch information
rgo3 authored and ti-mo committed Jan 5, 2024
1 parent 3aa8352 commit 431f71b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
21 changes: 21 additions & 0 deletions link/syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,24 @@ var haveProgQuery = internal.NewFeatureTest("BPF_PROG_QUERY", "4.15", func() err
}
return errors.New("syscall succeeded unexpectedly")
})

var haveTCX = internal.NewFeatureTest("tcx", "6.6", func() error {
attr := sys.LinkCreateTcxAttr{
// We rely on this being checked during the syscall.
// With an otherwise correct payload we expect EBADF here
// as an indication that the feature is present.
ProgFd: ^uint32(0),
TargetIfindex: ^uint32(0),
AttachType: sys.AttachType(ebpf.AttachTCXIngress),
}

_, err := sys.LinkCreateTcx(&attr)

if errors.Is(err, unix.EBADF) {
return nil
}
if err != nil {
return ErrNotSupported
}
return errors.New("syscall succeeded unexpectedly")
})
4 changes: 4 additions & 0 deletions link/syscalls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ func TestHaveBPFLink(t *testing.T) {
func TestHaveProgQuery(t *testing.T) {
testutils.CheckFeatureTest(t, haveProgQuery)
}

func TestHaveTCX(t *testing.T) {
testutils.CheckFeatureTest(t, haveTCX)
}
3 changes: 3 additions & 0 deletions link/tcx.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func AttachTCX(opts TCXOptions) (Link, error) {
runtime.KeepAlive(opts.Program)
runtime.KeepAlive(opts.Anchor)
if err != nil {
if haveFeatErr := haveTCX(); haveFeatErr != nil {
return nil, haveFeatErr
}
return nil, fmt.Errorf("attach tcx link: %w", err)
}

Expand Down

0 comments on commit 431f71b

Please sign in to comment.