| 1 |
|
| 2 |
|
| 3 |
package link |
| 4 |
|
| 5 |
import ( |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/go-quicktest/qt" |
| 9 |
|
| 10 |
"github.com/cilium/ebpf" |
| 11 |
"github.com/cilium/ebpf/asm" |
| 12 |
"github.com/cilium/ebpf/btf" |
| 13 |
"github.com/cilium/ebpf/internal/sys" |
| 14 |
"github.com/cilium/ebpf/internal/testutils" |
| 15 |
) |
| 16 |
|
| 17 |
func TestStructOps(t *testing.T) { |
| 18 |
testutils.SkipOnOldKernel(t, "6.12", "bpf_testmod_ops") |
| 19 |
|
| 20 |
m := mustStructOpsFixtures(t) |
| 21 |
l, err := AttachStructOps(StructOpsOptions{Map: m}) |
| 22 |
qt.Assert(t, qt.IsNil(err)) |
| 23 |
|
| 24 |
testLink(t, l, nil) |
| 25 |
} |
| 26 |
|
| 27 |
func mustStructOpsFixtures(tb testing.TB) *ebpf.Map { |
| 28 |
tb.Helper() |
| 29 |
|
| 30 |
testutils.SkipIfNotSupported(tb, haveBPFLink()) |
| 31 |
|
| 32 |
userData := []byte{ |
| 33 |
|
| 34 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 35 |
|
| 36 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 37 |
|
| 38 |
0xef, 0xbe, 0xad, 0xde, 0x00, 0x00, 0x00, 0x00, |
| 39 |
} |
| 40 |
|
| 41 |
spec := &ebpf.CollectionSpec{ |
| 42 |
Maps: map[string]*ebpf.MapSpec{ |
| 43 |
"testmod_ops": { |
| 44 |
Name: "testmod_ops", |
| 45 |
Type: ebpf.StructOpsMap, |
| 46 |
MaxEntries: 1, |
| 47 |
Flags: sys.BPF_F_LINK, |
| 48 |
Key: &btf.Int{Size: 4}, |
| 49 |
KeySize: 4, |
| 50 |
ValueSize: 24, |
| 51 |
Value: &btf.Struct{ |
| 52 |
Name: "bpf_testmod_ops", |
| 53 |
Size: 24, |
| 54 |
Members: []btf.Member{ |
| 55 |
{ |
| 56 |
Name: "test_1", |
| 57 |
Type: &btf.Pointer{ |
| 58 |
Target: &btf.FuncProto{ |
| 59 |
Params: []btf.FuncParam{}, |
| 60 |
Return: &btf.Int{Name: "int", Size: 4, Encoding: btf.Signed}}}, |
| 61 |
Offset: 0, |
| 62 |
}, |
| 63 |
{ |
| 64 |
Name: "test_2", |
| 65 |
Type: &btf.Pointer{ |
| 66 |
Target: &btf.FuncProto{ |
| 67 |
Params: []btf.FuncParam{ |
| 68 |
{Type: &btf.Int{Name: "int", Size: 4, Encoding: btf.Signed}}, |
| 69 |
{Type: &btf.Int{Name: "int", Size: 4, Encoding: btf.Signed}}, |
| 70 |
}, |
| 71 |
Return: (*btf.Void)(nil), |
| 72 |
}, |
| 73 |
}, |
| 74 |
Offset: 64, |
| 75 |
}, |
| 76 |
{ |
| 77 |
Name: "data", |
| 78 |
Type: &btf.Int{Name: "int", Size: 4, Encoding: btf.Signed}, |
| 79 |
Offset: 128, |
| 80 |
}, |
| 81 |
}, |
| 82 |
}, |
| 83 |
Contents: []ebpf.MapKV{ |
| 84 |
{ |
| 85 |
Key: uint32(0), |
| 86 |
Value: userData, |
| 87 |
}, |
| 88 |
}, |
| 89 |
}, |
| 90 |
}, |
| 91 |
Programs: map[string]*ebpf.ProgramSpec{ |
| 92 |
"test_1": { |
| 93 |
Name: "test_1", |
| 94 |
Type: ebpf.StructOps, |
| 95 |
AttachTo: "bpf_testmod_ops:test_1", |
| 96 |
License: "GPL", |
| 97 |
SectionName: "struct_ops/test_1", |
| 98 |
Instructions: asm.Instructions{ |
| 99 |
asm.Mov.Imm(asm.R0, 0), |
| 100 |
asm.Return(), |
| 101 |
}, |
| 102 |
}, |
| 103 |
}, |
| 104 |
Variables: map[string]*ebpf.VariableSpec{}, |
| 105 |
} |
| 106 |
|
| 107 |
coll, err := ebpf.NewCollection(spec) |
| 108 |
testutils.SkipIfNotSupported(tb, err) |
| 109 |
qt.Assert(tb, qt.IsNil(err)) |
| 110 |
tb.Cleanup(func() { |
| 111 |
coll.Close() |
| 112 |
}) |
| 113 |
|
| 114 |
m := coll.Maps["testmod_ops"] |
| 115 |
qt.Assert(tb, qt.IsNotNil(m)) |
| 116 |
|
| 117 |
return m |
| 118 |
} |