Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions link/link_other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/testutils"
)
Expand Down Expand Up @@ -112,6 +113,89 @@ func mustCgroupFixtures(t *testing.T) (*os.File, *ebpf.Program) {
return testutils.CreateCgroup(t), mustLoadProgram(t, ebpf.CGroupSKB, 0, "")
}

func mustStructOpsFixtures(t *testing.T) (*ebpf.Collection, error) {
t.Helper()

testutils.SkipIfNotSupported(t, haveBPFLink())

userData := []byte{
// test_1 func ptr (8B)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// test_2 func ptr (8B)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// data (4B) + padding (4B)
0xef, 0xbe, 0xad, 0xde, 0x00, 0x00, 0x00, 0x00,
}

spec := &ebpf.CollectionSpec{
Maps: map[string]*ebpf.MapSpec{
"testmod_ops": {
Name: "testmod_ops",
Type: ebpf.StructOpsMap,
MaxEntries: 1,
Flags: sys.BPF_F_LINK,
Key: &btf.Int{Size: 4},
KeySize: 4,
ValueSize: 24,
Value: &btf.Struct{
Name: "bpf_testmod_ops",
Size: 24,
Members: []btf.Member{
{
Name: "test_1",
Type: &btf.Pointer{
Target: &btf.FuncProto{
Params: []btf.FuncParam{},
Return: &btf.Int{Name: "int", Size: 4, Encoding: btf.Signed}}},
Offset: 0,
},
{
Name: "test_2",
Type: &btf.Pointer{
Target: &btf.FuncProto{
Params: []btf.FuncParam{
{Type: &btf.Int{Name: "int", Size: 4, Encoding: btf.Signed}},
{Type: &btf.Int{Name: "int", Size: 4, Encoding: btf.Signed}},
},
Return: (*btf.Void)(nil),
},
},
Offset: 64,
},
{
Name: "data",
Type: &btf.Int{Name: "int", Size: 4, Encoding: btf.Signed},
Offset: 128, // bits
},
},
},
Contents: []ebpf.MapKV{
{
Key: uint32(0),
Value: userData,
},
},
},
},
Programs: map[string]*ebpf.ProgramSpec{
"test_1": {
Name: "test_1",
Type: ebpf.StructOps,
AttachTo: "bpf_testmod_ops:test_1",
License: "GPL",
SectionName: "struct_ops/test_1",
Instructions: asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
},
},
Variables: map[string]*ebpf.VariableSpec{},
}

return ebpf.NewCollection(spec)
}

func mustLoadProgram(tb testing.TB, typ ebpf.ProgramType, attachType ebpf.AttachType, attachTo string) *ebpf.Program {
tb.Helper()

Expand Down
46 changes: 46 additions & 0 deletions link/struct_ops.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package link

import (
"fmt"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/internal/sys"
)

type structOpsLink struct {
*RawLink
}

// AttachStructOps attaches a struct_ops map (created from a ".struct_ops.link"
// section) to its kernel subsystem via a BPF link.
func AttachStructOps(m *ebpf.Map) (Link, error) {
if m == nil {
return nil, fmt.Errorf("map cannot be nil")
}

if t := m.Type(); t != ebpf.StructOpsMap {
return nil, fmt.Errorf("invalid map type %s, expected struct_ops", t)
}

mapFD := m.FD()
if mapFD <= 0 {
return nil, fmt.Errorf("invalid map: %s (was it created?)", sys.ErrClosedFd)
}

// ".struct_ops.link" requires the map to be created with BPF_F_LINK.
if (int(m.Flags()) & sys.BPF_F_LINK) != sys.BPF_F_LINK {
return nil, fmt.Errorf("map is missing BPF_F_LINK flag: %w", ErrNotSupported)
}

fd, err := sys.LinkCreate(&sys.LinkCreateAttr{
// struct_ops expects target_fd = map FD
ProgFd: uint32(mapFD),
AttachType: sys.AttachType(ebpf.AttachStructOps),
TargetFd: 0,
})
if err != nil {
return nil, fmt.Errorf("attach StructOps: create link: %w", err)
}

return &structOpsLink{&RawLink{fd: fd}}, nil
}
42 changes: 42 additions & 0 deletions link/struct_ops_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package link

import (
"errors"
"testing"

"github.com/cilium/ebpf/internal/testutils"
)

func TestStructOps(t *testing.T) {
testutils.SkipOnOldKernel(t, "6.12", "bpf_testmod_ops")

coll, err := mustStructOpsFixtures(t)

Check failure on line 13 in link/struct_ops_test.go

View workflow job for this annotation

GitHub Actions / Run tests (Windows) (main)

undefined: mustStructOpsFixtures
if errors.Is(err, ErrNotSupported) {
t.Skipf("bpf_testmod_ops: %s", err)
}
if err != nil {
t.Fatal(err)
}

m := coll.Maps["testmod_ops"]
if m == nil {
t.Fatal("map: testmod_ops not found")
}

p := coll.Programs["test_1"]
if m == nil {
t.Fatal("prog: test_1 not found")
}

l, err := AttachStructOps(m)
if err != nil {
t.Fatal(err)
}
defer l.Close()

// Close the program and map on test teardown.
t.Cleanup(func() {
m.Close()
p.Close()
})
}
Loading