| 1 |
|
| 2 |
|
| 3 |
package link |
| 4 |
|
| 5 |
import ( |
| 6 |
"os" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/go-quicktest/qt" |
| 10 |
|
| 11 |
"github.com/cilium/ebpf" |
| 12 |
"github.com/cilium/ebpf/asm" |
| 13 |
"github.com/cilium/ebpf/internal/sys" |
| 14 |
"github.com/cilium/ebpf/internal/testutils" |
| 15 |
) |
| 16 |
|
| 17 |
func testLinkArch(t *testing.T, link Link) { |
| 18 |
t.Run("link/info", func(t *testing.T) { |
| 19 |
info, err := link.Info() |
| 20 |
testutils.SkipIfNotSupported(t, err) |
| 21 |
if err != nil { |
| 22 |
t.Fatal("Link info returns an error:", err) |
| 23 |
} |
| 24 |
|
| 25 |
if info.Type == 0 { |
| 26 |
t.Fatal("Failed to get link info type") |
| 27 |
} |
| 28 |
|
| 29 |
switch link.(type) { |
| 30 |
case *tracing: |
| 31 |
if info.Tracing() == nil { |
| 32 |
t.Fatalf("Failed to get link tracing extra info") |
| 33 |
} |
| 34 |
case *linkCgroup: |
| 35 |
cg := info.Cgroup() |
| 36 |
if cg.CgroupId == 0 { |
| 37 |
t.Fatalf("Failed to get link Cgroup extra info") |
| 38 |
} |
| 39 |
case *NetNsLink: |
| 40 |
netns := info.NetNs() |
| 41 |
if netns.AttachType == 0 { |
| 42 |
t.Fatalf("Failed to get link NetNs extra info") |
| 43 |
} |
| 44 |
case *xdpLink: |
| 45 |
xdp := info.XDP() |
| 46 |
if xdp.Ifindex == 0 { |
| 47 |
t.Fatalf("Failed to get link XDP extra info") |
| 48 |
} |
| 49 |
case *tcxLink: |
| 50 |
tcx := info.TCX() |
| 51 |
if tcx.Ifindex == 0 { |
| 52 |
t.Fatalf("Failed to get link TCX extra info") |
| 53 |
} |
| 54 |
case *netfilterLink: |
| 55 |
nf := info.Netfilter() |
| 56 |
if nf.Priority == 0 { |
| 57 |
t.Fatalf("Failed to get link Netfilter extra info") |
| 58 |
} |
| 59 |
case *kprobeMultiLink: |
| 60 |
|
| 61 |
kmulti := info.KprobeMulti() |
| 62 |
|
| 63 |
testutils.SkipOnOldKernel(t, "6.6", "bpf_kprobe_multi_link_fill_link_info") |
| 64 |
qt.Assert(t, qt.Not(qt.Equals(kmulti.Count, 0))) |
| 65 |
|
| 66 |
|
| 67 |
case *perfEventLink: |
| 68 |
|
| 69 |
pevent := info.PerfEvent() |
| 70 |
switch pevent.Type { |
| 71 |
case sys.BPF_PERF_EVENT_KPROBE, sys.BPF_PERF_EVENT_KRETPROBE: |
| 72 |
_ = pevent.Kprobe() |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
} |
| 77 |
} |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
func newRawLink(t *testing.T) (*RawLink, *ebpf.Program) { |
| 82 |
t.Helper() |
| 83 |
|
| 84 |
cgroup, prog := mustCgroupFixtures(t) |
| 85 |
link, err := AttachRawLink(RawLinkOptions{ |
| 86 |
Target: int(cgroup.Fd()), |
| 87 |
Program: prog, |
| 88 |
Attach: ebpf.AttachCGroupInetEgress, |
| 89 |
}) |
| 90 |
testutils.SkipIfNotSupported(t, err) |
| 91 |
if err != nil { |
| 92 |
t.Fatal("Can't create raw link:", err) |
| 93 |
} |
| 94 |
t.Cleanup(func() { link.Close() }) |
| 95 |
|
| 96 |
return link, prog |
| 97 |
} |
| 98 |
|
| 99 |
func mustCgroupFixtures(t *testing.T) (*os.File, *ebpf.Program) { |
| 100 |
t.Helper() |
| 101 |
|
| 102 |
testutils.SkipIfNotSupported(t, haveProgAttach()) |
| 103 |
|
| 104 |
return testutils.CreateCgroup(t), mustLoadProgram(t, ebpf.CGroupSKB, 0, "") |
| 105 |
} |
| 106 |
|
| 107 |
func mustLoadProgram(tb testing.TB, typ ebpf.ProgramType, attachType ebpf.AttachType, attachTo string) *ebpf.Program { |
| 108 |
tb.Helper() |
| 109 |
|
| 110 |
license := "MIT" |
| 111 |
switch typ { |
| 112 |
case ebpf.RawTracepoint, ebpf.LSM: |
| 113 |
license = "GPL" |
| 114 |
} |
| 115 |
|
| 116 |
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{ |
| 117 |
Type: typ, |
| 118 |
AttachType: attachType, |
| 119 |
AttachTo: attachTo, |
| 120 |
License: license, |
| 121 |
Instructions: asm.Instructions{ |
| 122 |
asm.Mov.Imm(asm.R0, 0), |
| 123 |
asm.Return(), |
| 124 |
}, |
| 125 |
}) |
| 126 |
if err != nil { |
| 127 |
tb.Fatal(err) |
| 128 |
} |
| 129 |
|
| 130 |
tb.Cleanup(func() { |
| 131 |
prog.Close() |
| 132 |
}) |
| 133 |
|
| 134 |
return prog |
| 135 |
} |
| 136 |
|
| 137 |
func TestDetachLinkFail(t *testing.T) { |
| 138 |
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "") |
| 139 |
defer prog.Close() |
| 140 |
|
| 141 |
uprobeLink, err := bashEx.Uprobe(bashSym, prog, nil) |
| 142 |
qt.Assert(t, qt.IsNil(err)) |
| 143 |
defer uprobeLink.Close() |
| 144 |
|
| 145 |
err = uprobeLink.Detach() |
| 146 |
qt.Assert(t, qt.ErrorIs(err, ErrNotSupported), qt.Commentf("got error: %s", err)) |
| 147 |
} |