Skip to content

Commit

Permalink
Add TCX tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelroquetto committed Jan 7, 2025
1 parent 6403683 commit f49b69b
Showing 1 changed file with 258 additions and 0 deletions.
258 changes: 258 additions & 0 deletions pkg/internal/ebpf/tcmanager/tcmanager_privileged_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
package tcmanager

import (
"bytes"
"context"
"errors"
"os"
"testing"
"time"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/stretchr/testify/assert"
)

const privilegedEnv = "PRIVILEGED_TESTS"

func TestTCXManager_tcxAttachType(t *testing.T) {
test := func(in AttachmentType, out ebpf.AttachType) {
r, err := tcxAttachType(in)
assert.NoError(t, err)
assert.Equal(t, out, r)
}

test(AttachmentEgress, ebpf.AttachTCXEgress)
test(AttachmentIngress, ebpf.AttachTCXIngress)
}

type Progs struct {
Ingress *ebpf.Program `ebpf:"beyla_ingress"`
Egress *ebpf.Program `ebpf:"beyla_egress"`
}

func isBpfProgLoaded(name string) (bool, error) {
id := ebpf.ProgramID(0)
var err error

for {
id, err = ebpf.ProgramGetNextID(id)

if err != nil {
if errors.Is(err, os.ErrNotExist) {
break
} else {

Check failure on line 44 in pkg/internal/ebpf/tcmanager/tcmanager_privileged_test.go

View workflow job for this annotation

GitHub Actions / test (1.23)

superfluous-else: if block ends with a break statement, so drop this else and outdent its block (revive)
return false, err
}
}

prog, err := ebpf.NewProgramFromID(id)
defer prog.Close()

Check failure on line 50 in pkg/internal/ebpf/tcmanager/tcmanager_privileged_test.go

View workflow job for this annotation

GitHub Actions / test (1.23)

SA5001: should check error returned from ebpf.NewProgramFromID() before deferring prog.Close() (staticcheck)

if err != nil {
if errors.Is(err, os.ErrNotExist) {
// the program has been closed in the meantime, continue
continue
} else {

Check failure on line 56 in pkg/internal/ebpf/tcmanager/tcmanager_privileged_test.go

View workflow job for this annotation

GitHub Actions / test (1.23)

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)
return false, err
}
}

info, err := prog.Info()

if err != nil {
return false, err
}

if info.Name == name {
return true, nil
}
}

return false, nil
}

func isBpfProgLinked(name string) (bool, error) {
it := new(link.Iterator)

for it.Next() {
link := it.Take()
defer link.Close()

info, err := link.Info()

if err != nil {
return false, err
}

prog, err := ebpf.NewProgramFromID(info.Program)
defer prog.Close()

Check failure on line 89 in pkg/internal/ebpf/tcmanager/tcmanager_privileged_test.go

View workflow job for this annotation

GitHub Actions / test (1.23)

SA5001: should check error returned from ebpf.NewProgramFromID() before deferring prog.Close() (staticcheck)

if err != nil {
return false, err
}

progInfo, err := prog.Info()

if err != nil {
return false, err
}

if progInfo.Name == name {
return true, nil
}
}

return false, nil
}

func TestTCXManager_addRemove(t *testing.T) {
if os.Getenv(privilegedEnv) == "" {
t.Skipf("Skipping this test because %v is not set", privilegedEnv)
}

reader := bytes.NewReader(tc_program)
assert.NotNil(t, reader)

spec, err := ebpf.LoadCollectionSpecFromReader(reader)
assert.NoError(t, err)

coll, err := ebpf.NewCollection(spec)
assert.NoError(t, err)
assert.NotNil(t, coll)

var progs Progs
err = coll.Assign(&progs)
assert.NoError(t, err)
assert.NotNil(t, progs.Ingress)
assert.NotNil(t, progs.Egress)

tcx := NewTCXManager()
assert.NotNil(t, tcx)

ctx := context.Background()
defer ctx.Done()

tcx.Start(ctx)

test := func(progName string, prog *ebpf.Program, attachType AttachmentType) {
tcx.AddProgram(progName, prog, attachType)

// wait for links to come up
time.Sleep(5 * time.Second)

linked, err := isBpfProgLinked(progName)
assert.NoError(t, err)
assert.True(t, linked)

tcx.RemoveProgram(progName)

prog.Close()

linked, err = isBpfProgLinked(progName)
assert.NoError(t, err)
assert.False(t, linked)

loaded, err := isBpfProgLoaded(progName)
assert.NoError(t, err)
assert.False(t, loaded)
}

test("beyla_ingress", progs.Ingress, AttachmentIngress)
test("beyla_egress", progs.Egress, AttachmentEgress)
}

/*
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
char __license[] SEC("license") = "Dual MIT/GPL";
SEC("tc_ingress")
int beyla_ingress(struct __sk_buff *skb) {
return 0;
}
SEC("tc_egress")
int beyla_egress(struct __sk_buff *skb) {
return 0;
}
clang -c -target bpf -O2 -o tc_program.o tc_program.c
xxd --include tc_program.o > tc_program.c
*/

// nolint: stylecheck,revive
var tc_program = []byte{
0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xf7, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x50, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
0x08, 0x00, 0x01, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x44, 0x75, 0x61, 0x6c, 0x20, 0x4d, 0x49, 0x54, 0x2f, 0x47, 0x50, 0x4c,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x00, 0x00, 0x04, 0x00, 0xf1, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x12, 0x00, 0x03, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x12, 0x00, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00, 0x11, 0x00, 0x05, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x04, 0x00, 0x2e, 0x74, 0x65, 0x78,
0x74, 0x00, 0x74, 0x63, 0x5f, 0x69, 0x6e, 0x67, 0x72, 0x65, 0x73, 0x73,
0x00, 0x62, 0x65, 0x79, 0x6c, 0x61, 0x5f, 0x69, 0x6e, 0x67, 0x72, 0x65,
0x73, 0x73, 0x00, 0x74, 0x63, 0x5f, 0x65, 0x67, 0x72, 0x65, 0x73, 0x73,
0x00, 0x62, 0x65, 0x79, 0x6c, 0x61, 0x5f, 0x65, 0x67, 0x72, 0x65, 0x73,
0x73, 0x00, 0x2e, 0x6c, 0x6c, 0x76, 0x6d, 0x5f, 0x61, 0x64, 0x64, 0x72,
0x73, 0x69, 0x67, 0x00, 0x5f, 0x5f, 0x6c, 0x69, 0x63, 0x65, 0x6e, 0x73,
0x65, 0x00, 0x66, 0x6f, 0x6f, 0x2e, 0x63, 0x00, 0x2e, 0x73, 0x74, 0x72,
0x74, 0x61, 0x62, 0x00, 0x2e, 0x73, 0x79, 0x6d, 0x74, 0x61, 0x62, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xeb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x37, 0x00, 0x00, 0x00, 0x03, 0x4c, 0xff, 0x6f, 0x00, 0x00, 0x00, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xe8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x5d, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}

0 comments on commit f49b69b

Please sign in to comment.