1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
package link
import (
"errors"
"math"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/testutils/fdtrace"
"github.com/cilium/ebpf/internal/unix"
"github.com/go-quicktest/qt"
)
func TestMain(m *testing.M) {
fdtrace.TestMain(m)
}
func TestRawLink(t *testing.T) {
cgroup, prog := mustCgroupFixtures(t)
link, err := AttachRawLink(RawLinkOptions{
Target: int(cgroup.Fd()),
Program: prog,
Attach: ebpf.AttachCGroupInetEgress,
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't create raw link:", err)
}
info, err := link.Info()
if err != nil {
t.Fatal("Can't get link info:", err)
}
pi, err := prog.Info()
if err != nil {
t.Fatal("Can't get program info:", err)
}
progID, ok := pi.ID()
if !ok {
t.Fatal("Program ID not available in program info")
}
if info.Program != progID {
t.Error("Link program ID doesn't match program ID")
}
testLink(t, link, prog)
}
func TestUnpinRawLink(t *testing.T) {
cgroup, prog := mustCgroupFixtures(t)
link, _ := newPinnedRawLink(t, cgroup, prog)
defer link.Close()
qt.Assert(t, qt.IsTrue(link.IsPinned()))
if err := link.Unpin(); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.IsFalse(link.IsPinned()))
}
func TestRawLinkLoadPinnedWithOptions(t *testing.T) {
cgroup, prog := mustCgroupFixtures(t)
link, path := newPinnedRawLink(t, cgroup, prog)
defer link.Close()
qt.Assert(t, qt.IsTrue(link.IsPinned()))
// It seems like the kernel ignores BPF_F_RDONLY when updating a link,
// so we can't test this.
_, err := loadPinnedRawLink(path, &ebpf.LoadPinOptions{
Flags: math.MaxUint32,
})
if !errors.Is(err, unix.EINVAL) {
t.Fatal("Invalid flags don't trigger an error:", err)
}
}
func TestIterator(t *testing.T) {
cgroup, prog := mustCgroupFixtures(t)
tLink, err := AttachRawLink(RawLinkOptions{
Target: int(cgroup.Fd()),
Program: prog,
Attach: ebpf.AttachCGroupInetEgress,
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't create original raw link:", err)
}
defer tLink.Close()
tLinkInfo, err := tLink.Info()
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't get original link info:", err)
}
it := new(Iterator)
defer it.Close()
prev := it.ID
var foundLink Link
for it.Next() {
// Iterate all loaded links.
if it.Link == nil {
t.Fatal("Next doesn't assign link")
}
if it.ID == prev {
t.Fatal("Iterator doesn't advance ID")
}
prev = it.ID
if it.ID == tLinkInfo.ID {
foundLink = it.Take()
}
}
if err := it.Err(); err != nil {
t.Fatal("Iteration returned an error:", err)
}
if it.Link != nil {
t.Fatal("Next doesn't clean up link on last iteration")
}
if prev != it.ID {
t.Fatal("Next changes ID on last iteration")
}
if foundLink == nil {
t.Fatal("Original link not found")
}
defer foundLink.Close()
// Confirm that we found the original link.
info, err := foundLink.Info()
if err != nil {
t.Fatal("Can't get link info:", err)
}
if info.ID != tLinkInfo.ID {
t.Fatal("Found link has wrong ID")
}
}
func newPinnedRawLink(t *testing.T, cgroup *os.File, prog *ebpf.Program) (*RawLink, string) {
t.Helper()
link, err := AttachRawLink(RawLinkOptions{
Target: int(cgroup.Fd()),
Program: prog,
Attach: ebpf.AttachCGroupInetEgress,
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't create raw link:", err)
}
path := filepath.Join(testutils.TempBPFFS(t), "link")
err = link.Pin(path)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
}
return link, path
}
func mustCgroupFixtures(t *testing.T) (*os.File, *ebpf.Program) {
t.Helper()
testutils.SkipIfNotSupported(t, haveProgAttach())
return testutils.CreateCgroup(t), mustLoadProgram(t, ebpf.CGroupSKB, 0, "")
}
func testLink(t *testing.T, link Link, prog *ebpf.Program) {
t.Helper()
tmp, err := os.MkdirTemp("/sys/fs/bpf", "ebpf-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
_, isRawLink := link.(*RawLink)
t.Run("link/pinning", func(t *testing.T) {
path := filepath.Join(tmp, "link")
err = link.Pin(path)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatalf("Can't pin %T: %s", link, err)
}
link2, err := LoadPinnedLink(path, nil)
if err != nil {
t.Fatalf("Can't load pinned %T: %s", link, err)
}
link2.Close()
if !isRawLink && reflect.TypeOf(link) != reflect.TypeOf(link2) {
t.Errorf("Loading a pinned %T returns a %T", link, link2)
}
_, err = LoadPinnedLink(path, &ebpf.LoadPinOptions{
Flags: math.MaxUint32,
})
if !errors.Is(err, unix.EINVAL) {
t.Errorf("Loading a pinned %T doesn't respect flags", link)
}
})
t.Run("link/update", func(t *testing.T) {
err := link.Update(prog)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Update returns an error:", err)
}
func() {
// Panicking is OK
defer func() {
_ = recover()
}()
if err := link.Update(nil); err == nil {
t.Fatalf("%T.Update accepts nil program", link)
}
}()
})
t.Run("link/info", func(t *testing.T) {
info, err := link.Info()
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Link info returns an error:", err)
}
if info.Type == 0 {
t.Fatal("Failed to get link info type")
}
switch link.(type) {
case *tracing:
if info.Tracing() == nil {
t.Fatalf("Failed to get link tracing extra info")
}
case *linkCgroup:
cg := info.Cgroup()
if cg.CgroupId == 0 {
t.Fatalf("Failed to get link Cgroup extra info")
}
case *NetNsLink:
netns := info.NetNs()
if netns.AttachType == 0 {
t.Fatalf("Failed to get link NetNs extra info")
}
case *xdpLink:
xdp := info.XDP()
if xdp.Ifindex == 0 {
t.Fatalf("Failed to get link XDP extra info")
}
case *tcxLink:
tcx := info.TCX()
if tcx.Ifindex == 0 {
t.Fatalf("Failed to get link TCX extra info")
}
case *netfilterLink:
nf := info.Netfilter()
if nf.Priority == 0 {
t.Fatalf("Failed to get link Netfilter extra info")
}
case *kprobeMultiLink:
// test default Info data
kmulti := info.KprobeMulti()
if count, ok := kmulti.AddressCount(); ok {
qt.Assert(t, qt.Not(qt.Equals(count, 0)))
_, ok = kmulti.Missed()
qt.Assert(t, qt.IsTrue(ok))
// NB: We don't check that missed is actually correct
// since it's not easy to trigger from tests.
}
case *perfEventLink:
// test default Info data
pevent := info.PerfEvent()
switch pevent.Type {
case sys.BPF_PERF_EVENT_KPROBE, sys.BPF_PERF_EVENT_KRETPROBE:
kp := pevent.Kprobe()
if addr, ok := kp.Address(); ok {
qt.Assert(t, qt.Not(qt.Equals(addr, 0)))
_, ok := kp.Missed()
qt.Assert(t, qt.IsTrue(ok))
// NB: We don't check that missed is actually correct
// since it's not easy to trigger from tests.
}
}
}
})
type FDer interface {
FD() int
}
t.Run("from fd", func(t *testing.T) {
fder, ok := link.(FDer)
if !ok {
t.Skip("Link doesn't allow retrieving FD")
}
// We need to dup the FD since NewLinkFromFD takes
// ownership.
dupFD, err := unix.FcntlInt(uintptr(fder.FD()), unix.F_DUPFD_CLOEXEC, 1)
if err != nil {
t.Fatal("Can't dup link FD:", err)
}
defer unix.Close(dupFD)
newLink, err := NewFromFD(dupFD)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't create new link from dup link FD:", err)
}
defer newLink.Close()
if !isRawLink && reflect.TypeOf(newLink) != reflect.TypeOf(link) {
t.Fatalf("Expected type %T, got %T", link, newLink)
}
})
if err := link.Close(); err != nil {
t.Fatalf("%T.Close returns an error: %s", link, err)
}
}
func mustLoadProgram(tb testing.TB, typ ebpf.ProgramType, attachType ebpf.AttachType, attachTo string) *ebpf.Program {
tb.Helper()
license := "MIT"
switch typ {
case ebpf.RawTracepoint, ebpf.LSM:
license = "GPL"
}
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: typ,
AttachType: attachType,
AttachTo: attachTo,
License: license,
Instructions: asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
})
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() {
prog.Close()
})
return prog
}
func TestLoadWrongPin(t *testing.T) {
cg, p := mustCgroupFixtures(t)
l, err := AttachRawLink(RawLinkOptions{
Target: int(cg.Fd()),
Program: p,
Attach: ebpf.AttachCGroupInetEgress,
})
testutils.SkipIfNotSupported(t, err)
t.Cleanup(func() { l.Close() })
tmp := testutils.TempBPFFS(t)
ppath := filepath.Join(tmp, "prog")
lpath := filepath.Join(tmp, "link")
qt.Assert(t, qt.IsNil(p.Pin(ppath)))
qt.Assert(t, qt.IsNil(l.Pin(lpath)))
_, err = LoadPinnedLink(ppath, nil)
qt.Assert(t, qt.IsNotNil(err))
ll, err := LoadPinnedLink(lpath, nil)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.IsNil(ll.Close()))
}
|