| 1 |
package ebpf |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"reflect" |
| 10 |
"slices" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/go-quicktest/qt" |
| 14 |
|
| 15 |
"github.com/cilium/ebpf/asm" |
| 16 |
"github.com/cilium/ebpf/btf" |
| 17 |
"github.com/cilium/ebpf/internal/sys" |
| 18 |
"github.com/cilium/ebpf/internal/testutils" |
| 19 |
"github.com/cilium/ebpf/internal/testutils/testmain" |
| 20 |
) |
| 21 |
|
| 22 |
func TestMain(m *testing.M) { |
| 23 |
testmain.Run(m) |
| 24 |
} |
| 25 |
|
| 26 |
func TestCollectionSpecNotModified(t *testing.T) { |
| 27 |
spec := &CollectionSpec{ |
| 28 |
Maps: map[string]*MapSpec{ |
| 29 |
"my-map": { |
| 30 |
Type: Array, |
| 31 |
KeySize: 4, |
| 32 |
ValueSize: 4, |
| 33 |
MaxEntries: 1, |
| 34 |
}, |
| 35 |
".rodata": { |
| 36 |
Type: Array, |
| 37 |
KeySize: 4, |
| 38 |
ValueSize: 4, |
| 39 |
MaxEntries: 1, |
| 40 |
Flags: 0, |
| 41 |
Contents: []MapKV{{uint32(0), uint32(1)}}, |
| 42 |
}, |
| 43 |
}, |
| 44 |
Programs: map[string]*ProgramSpec{ |
| 45 |
"test": { |
| 46 |
Type: SocketFilter, |
| 47 |
Instructions: asm.Instructions{ |
| 48 |
asm.LoadImm(asm.R1, 0, asm.DWord).WithReference(".rodata"), |
| 49 |
asm.LoadImm(asm.R1, 0, asm.DWord).WithReference("my-map"), |
| 50 |
asm.LoadImm(asm.R0, 0, asm.DWord), |
| 51 |
asm.Return(), |
| 52 |
}, |
| 53 |
License: "MIT", |
| 54 |
}, |
| 55 |
}, |
| 56 |
} |
| 57 |
|
| 58 |
orig := spec.Copy() |
| 59 |
coll := mustNewCollection(t, spec, nil) |
| 60 |
qt.Assert(t, qt.CmpEquals(orig, spec, csCmpOpts)) |
| 61 |
|
| 62 |
for name := range spec.Maps { |
| 63 |
qt.Assert(t, qt.IsNotNil(coll.Maps[name])) |
| 64 |
} |
| 65 |
|
| 66 |
for name := range spec.Programs { |
| 67 |
qt.Assert(t, qt.IsNotNil(coll.Programs[name])) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func TestCollectionSpecCopy(t *testing.T) { |
| 72 |
ms := &MapSpec{ |
| 73 |
Type: Array, |
| 74 |
KeySize: 4, |
| 75 |
ValueSize: 4, |
| 76 |
MaxEntries: 1, |
| 77 |
} |
| 78 |
|
| 79 |
cs := &CollectionSpec{ |
| 80 |
map[string]*MapSpec{"my-map": ms}, |
| 81 |
map[string]*ProgramSpec{ |
| 82 |
"test": { |
| 83 |
Type: SocketFilter, |
| 84 |
Instructions: asm.Instructions{ |
| 85 |
asm.LoadMapPtr(asm.R1, 0), |
| 86 |
asm.LoadImm(asm.R0, 0, asm.DWord), |
| 87 |
asm.Return(), |
| 88 |
}, |
| 89 |
License: "MIT", |
| 90 |
}, |
| 91 |
}, |
| 92 |
map[string]*VariableSpec{ |
| 93 |
"my-var": { |
| 94 |
Name: "my-var", |
| 95 |
SectionName: "my-map", |
| 96 |
Offset: 0, |
| 97 |
}, |
| 98 |
}, |
| 99 |
&btf.Spec{}, |
| 100 |
binary.LittleEndian, |
| 101 |
} |
| 102 |
|
| 103 |
qt.Check(t, qt.IsNil((*CollectionSpec)(nil).Copy())) |
| 104 |
qt.Assert(t, testutils.IsDeepCopy(cs.Copy(), cs)) |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
var loadKeyFromMapProgramSpec = &ProgramSpec{ |
| 109 |
Type: SocketFilter, |
| 110 |
Instructions: asm.Instructions{ |
| 111 |
|
| 112 |
asm.LoadMapPtr(asm.R1, 0).WithReference("test-map"), |
| 113 |
|
| 114 |
asm.Mov.Reg(asm.R2, asm.R10), |
| 115 |
asm.Add.Imm(asm.R2, -4), |
| 116 |
asm.StoreImm(asm.R2, 0, 0, asm.Word), |
| 117 |
|
| 118 |
asm.FnMapLookupElem.Call(), |
| 119 |
asm.JEq.Imm(asm.R0, 0, "error"), |
| 120 |
asm.LoadMem(asm.R0, asm.R0, 0, asm.Word), |
| 121 |
asm.Ja.Label("ret"), |
| 122 |
|
| 123 |
asm.Mov.Imm(asm.R0, 0).WithSymbol("error"), |
| 124 |
asm.Return().WithSymbol("ret"), |
| 125 |
}, |
| 126 |
} |
| 127 |
|
| 128 |
func TestCollectionSpecMapReplacements(t *testing.T) { |
| 129 |
cs := &CollectionSpec{ |
| 130 |
Maps: map[string]*MapSpec{ |
| 131 |
"test-map": { |
| 132 |
Type: Array, |
| 133 |
KeySize: 4, |
| 134 |
ValueSize: 4, |
| 135 |
MaxEntries: 1, |
| 136 |
}, |
| 137 |
}, |
| 138 |
Programs: map[string]*ProgramSpec{ |
| 139 |
"test-prog": loadKeyFromMapProgramSpec.Copy(), |
| 140 |
}, |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
newMap := mustNewMap(t, cs.Maps["test-map"], nil) |
| 145 |
|
| 146 |
err := newMap.Put(uint32(0), uint32(2)) |
| 147 |
if err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} |
| 150 |
|
| 151 |
coll := mustNewCollection(t, cs, &CollectionOptions{ |
| 152 |
MapReplacements: map[string]*Map{ |
| 153 |
"test-map": newMap, |
| 154 |
}, |
| 155 |
}) |
| 156 |
|
| 157 |
ret := mustRun(t, coll.Programs["test-prog"], nil) |
| 158 |
|
| 159 |
if ret != 2 { |
| 160 |
t.Fatal("new / override map not used") |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
coll.Close() |
| 165 |
err = newMap.Put(uint32(0), uint32(3)) |
| 166 |
if err != nil { |
| 167 |
t.Fatalf("failed to update replaced map: %s", err) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func TestCollectionSpecMapReplacements_NonExistingMap(t *testing.T) { |
| 172 |
cs := &CollectionSpec{ |
| 173 |
Maps: map[string]*MapSpec{ |
| 174 |
"test-map": { |
| 175 |
Type: Array, |
| 176 |
KeySize: 4, |
| 177 |
ValueSize: 4, |
| 178 |
MaxEntries: 1, |
| 179 |
}, |
| 180 |
}, |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
newMap := mustNewMap(t, cs.Maps["test-map"], nil) |
| 185 |
|
| 186 |
coll, err := newCollection(t, cs, &CollectionOptions{ |
| 187 |
MapReplacements: map[string]*Map{ |
| 188 |
"non-existing-map": newMap, |
| 189 |
}, |
| 190 |
}) |
| 191 |
if err == nil { |
| 192 |
coll.Close() |
| 193 |
t.Fatal("Overriding a non existing map did not fail") |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func TestCollectionSpecMapReplacements_SpecMismatch(t *testing.T) { |
| 198 |
cs := &CollectionSpec{ |
| 199 |
Maps: map[string]*MapSpec{ |
| 200 |
"test-map": { |
| 201 |
Type: Array, |
| 202 |
KeySize: 4, |
| 203 |
ValueSize: 4, |
| 204 |
MaxEntries: 1, |
| 205 |
}, |
| 206 |
}, |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
newMap := mustNewMap(t, &MapSpec{ |
| 211 |
Type: Array, |
| 212 |
KeySize: 4, |
| 213 |
ValueSize: 8, |
| 214 |
MaxEntries: 1, |
| 215 |
}, nil) |
| 216 |
|
| 217 |
coll, err := newCollection(t, cs, &CollectionOptions{ |
| 218 |
MapReplacements: map[string]*Map{ |
| 219 |
"test-map": newMap, |
| 220 |
}, |
| 221 |
}) |
| 222 |
if err == nil { |
| 223 |
coll.Close() |
| 224 |
t.Fatal("Overriding a map with a mismatching spec did not fail") |
| 225 |
} |
| 226 |
if !errors.Is(err, ErrMapIncompatible) { |
| 227 |
t.Fatalf("Overriding a map with a mismatching spec failed with the wrong error") |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
func TestMapReplacementsDataSections(t *testing.T) { |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
file := testutils.NativeFile(t, "testdata/loader-%s.elf") |
| 236 |
spec, err := LoadCollectionSpec(file) |
| 237 |
if err != nil { |
| 238 |
t.Fatal(err) |
| 239 |
} |
| 240 |
|
| 241 |
var objs struct { |
| 242 |
Data *Map `ebpf:".data"` |
| 243 |
ROData *Map `ebpf:".rodata"` |
| 244 |
} |
| 245 |
|
| 246 |
mustLoadAndAssign(t, spec, &objs, nil) |
| 247 |
defer objs.Data.Close() |
| 248 |
defer objs.ROData.Close() |
| 249 |
|
| 250 |
mustLoadAndAssign(t, spec, &objs, &CollectionOptions{ |
| 251 |
MapReplacements: map[string]*Map{ |
| 252 |
".data": objs.Data, |
| 253 |
".rodata": objs.ROData, |
| 254 |
}, |
| 255 |
}) |
| 256 |
qt.Assert(t, qt.IsNil(objs.Data.Close())) |
| 257 |
qt.Assert(t, qt.IsNil(objs.ROData.Close())) |
| 258 |
} |
| 259 |
|
| 260 |
func TestCollectionSpec_LoadAndAssign_LazyLoading(t *testing.T) { |
| 261 |
spec := &CollectionSpec{ |
| 262 |
Maps: map[string]*MapSpec{ |
| 263 |
"valid": { |
| 264 |
Type: Array, |
| 265 |
KeySize: 4, |
| 266 |
ValueSize: 4, |
| 267 |
MaxEntries: 1, |
| 268 |
}, |
| 269 |
"bogus": { |
| 270 |
Type: Array, |
| 271 |
MaxEntries: 0, |
| 272 |
}, |
| 273 |
}, |
| 274 |
Programs: map[string]*ProgramSpec{ |
| 275 |
"valid": { |
| 276 |
Type: SocketFilter, |
| 277 |
Instructions: asm.Instructions{ |
| 278 |
asm.LoadImm(asm.R0, 0, asm.DWord), |
| 279 |
asm.Return(), |
| 280 |
}, |
| 281 |
License: "MIT", |
| 282 |
}, |
| 283 |
"bogus": { |
| 284 |
Type: SocketFilter, |
| 285 |
Instructions: asm.Instructions{ |
| 286 |
|
| 287 |
asm.Return(), |
| 288 |
}, |
| 289 |
License: "MIT", |
| 290 |
}, |
| 291 |
}, |
| 292 |
} |
| 293 |
|
| 294 |
var objs struct { |
| 295 |
Prog *Program `ebpf:"valid"` |
| 296 |
Map *Map `ebpf:"valid"` |
| 297 |
} |
| 298 |
|
| 299 |
mustLoadAndAssign(t, spec, &objs, nil) |
| 300 |
defer objs.Prog.Close() |
| 301 |
defer objs.Map.Close() |
| 302 |
|
| 303 |
if objs.Prog == nil { |
| 304 |
t.Error("Program is nil") |
| 305 |
} |
| 306 |
|
| 307 |
if objs.Map == nil { |
| 308 |
t.Error("Map is nil") |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
func TestCollectionSpecAssign(t *testing.T) { |
| 313 |
var specs struct { |
| 314 |
Program *ProgramSpec `ebpf:"prog1"` |
| 315 |
Map *MapSpec `ebpf:"map1"` |
| 316 |
Variable *VariableSpec `ebpf:"var1"` |
| 317 |
} |
| 318 |
|
| 319 |
mapSpec := &MapSpec{ |
| 320 |
Type: Array, |
| 321 |
KeySize: 4, |
| 322 |
ValueSize: 4, |
| 323 |
MaxEntries: 1, |
| 324 |
} |
| 325 |
progSpec := &ProgramSpec{ |
| 326 |
Type: SocketFilter, |
| 327 |
Instructions: asm.Instructions{ |
| 328 |
asm.LoadImm(asm.R0, 0, asm.DWord), |
| 329 |
asm.Return(), |
| 330 |
}, |
| 331 |
License: "MIT", |
| 332 |
} |
| 333 |
|
| 334 |
cs := &CollectionSpec{ |
| 335 |
Maps: map[string]*MapSpec{ |
| 336 |
"map1": mapSpec, |
| 337 |
}, |
| 338 |
Programs: map[string]*ProgramSpec{ |
| 339 |
"prog1": progSpec, |
| 340 |
}, |
| 341 |
Variables: map[string]*VariableSpec{ |
| 342 |
"var1": {}, |
| 343 |
}, |
| 344 |
} |
| 345 |
|
| 346 |
qt.Assert(t, qt.IsNil(cs.Assign(&specs))) |
| 347 |
qt.Assert(t, qt.Equals(specs.Program, progSpec)) |
| 348 |
qt.Assert(t, qt.Equals(specs.Map, mapSpec)) |
| 349 |
|
| 350 |
|
| 351 |
qt.Assert(t, qt.IsNotNil(cs.Assign(nil))) |
| 352 |
qt.Assert(t, qt.IsNotNil(cs.Assign((*int)(nil)))) |
| 353 |
qt.Assert(t, qt.IsNotNil(cs.Assign(0))) |
| 354 |
|
| 355 |
|
| 356 |
qt.Assert(t, qt.IsNil(cs.Assign(new(struct{ Foo int })))) |
| 357 |
|
| 358 |
|
| 359 |
var u struct { |
| 360 |
foo *MapSpec `ebpf:"map1"` |
| 361 |
} |
| 362 |
qt.Assert(t, qt.IsNotNil(cs.Assign(&u))) |
| 363 |
} |
| 364 |
|
| 365 |
func TestNewCollectionFdLeak(t *testing.T) { |
| 366 |
spec := &CollectionSpec{ |
| 367 |
Maps: map[string]*MapSpec{ |
| 368 |
"map1": { |
| 369 |
Type: Array, KeySize: 4, ValueSize: 4, MaxEntries: 1, |
| 370 |
|
| 371 |
Contents: []MapKV{{uint32(0), uint64(0)}}, |
| 372 |
}, |
| 373 |
}, |
| 374 |
} |
| 375 |
|
| 376 |
_, err := newCollection(t, spec, nil) |
| 377 |
qt.Assert(t, qt.IsNotNil(err)) |
| 378 |
} |
| 379 |
|
| 380 |
func TestAssignValues(t *testing.T) { |
| 381 |
zero := func(t reflect.Type, name string) (any, error) { |
| 382 |
return reflect.Zero(t).Interface(), nil |
| 383 |
} |
| 384 |
|
| 385 |
type t1 struct { |
| 386 |
Bar int `ebpf:"bar"` |
| 387 |
} |
| 388 |
|
| 389 |
type t2 struct { |
| 390 |
t1 |
| 391 |
Foo int `ebpf:"foo"` |
| 392 |
} |
| 393 |
|
| 394 |
type t2ptr struct { |
| 395 |
*t1 |
| 396 |
Foo int `ebpf:"foo"` |
| 397 |
} |
| 398 |
|
| 399 |
invalid := []struct { |
| 400 |
name string |
| 401 |
to any |
| 402 |
}{ |
| 403 |
{"non-struct", 1}, |
| 404 |
{"non-pointer struct", t1{}}, |
| 405 |
{"pointer to non-struct", new(int)}, |
| 406 |
{"embedded nil pointer", &t2ptr{}}, |
| 407 |
{"unexported field", new(struct { |
| 408 |
foo int `ebpf:"foo"` |
| 409 |
})}, |
| 410 |
{"identical tag", new(struct { |
| 411 |
Foo1 int `ebpf:"foo"` |
| 412 |
Foo2 int `ebpf:"foo"` |
| 413 |
})}, |
| 414 |
} |
| 415 |
|
| 416 |
for _, testcase := range invalid { |
| 417 |
t.Run(testcase.name, func(t *testing.T) { |
| 418 |
if err := assignValues(testcase.to, zero); err == nil { |
| 419 |
t.Fatal("assignValues didn't return an error") |
| 420 |
} else { |
| 421 |
t.Log(err) |
| 422 |
} |
| 423 |
}) |
| 424 |
} |
| 425 |
|
| 426 |
valid := []struct { |
| 427 |
name string |
| 428 |
to any |
| 429 |
}{ |
| 430 |
{"pointer to struct", new(t1)}, |
| 431 |
{"embedded struct", new(t2)}, |
| 432 |
{"embedded struct pointer", &t2ptr{t1: new(t1)}}, |
| 433 |
{"untagged field", new(struct{ Foo int })}, |
| 434 |
} |
| 435 |
|
| 436 |
for _, testcase := range valid { |
| 437 |
t.Run(testcase.name, func(t *testing.T) { |
| 438 |
if err := assignValues(testcase.to, zero); err != nil { |
| 439 |
t.Fatal("assignValues returned", err) |
| 440 |
} |
| 441 |
}) |
| 442 |
} |
| 443 |
|
| 444 |
} |
| 445 |
|
| 446 |
func TestCollectionAssign(t *testing.T) { |
| 447 |
var objs struct { |
| 448 |
Program *Program `ebpf:"prog1"` |
| 449 |
Map *Map `ebpf:"map1"` |
| 450 |
} |
| 451 |
|
| 452 |
cs := &CollectionSpec{ |
| 453 |
Maps: map[string]*MapSpec{ |
| 454 |
"map1": { |
| 455 |
Type: Array, |
| 456 |
KeySize: 4, |
| 457 |
ValueSize: 4, |
| 458 |
MaxEntries: 1, |
| 459 |
}, |
| 460 |
}, |
| 461 |
Programs: map[string]*ProgramSpec{ |
| 462 |
"prog1": { |
| 463 |
Type: SocketFilter, |
| 464 |
Instructions: asm.Instructions{ |
| 465 |
asm.LoadImm(asm.R0, 0, asm.DWord), |
| 466 |
asm.Return(), |
| 467 |
}, |
| 468 |
License: "MIT", |
| 469 |
}, |
| 470 |
}, |
| 471 |
} |
| 472 |
|
| 473 |
coll := mustNewCollection(t, cs, nil) |
| 474 |
|
| 475 |
qt.Assert(t, qt.IsNil(coll.Assign(&objs))) |
| 476 |
defer objs.Program.Close() |
| 477 |
defer objs.Map.Close() |
| 478 |
|
| 479 |
|
| 480 |
qt.Assert(t, qt.IsTrue(objs.Program.FD() >= 0)) |
| 481 |
qt.Assert(t, qt.IsTrue(objs.Map.FD() >= 0)) |
| 482 |
|
| 483 |
|
| 484 |
qt.Assert(t, qt.IsNil(coll.Programs["prog1"])) |
| 485 |
qt.Assert(t, qt.IsNil(coll.Maps["map1"])) |
| 486 |
} |
| 487 |
|
| 488 |
func TestCollectionAssignFail(t *testing.T) { |
| 489 |
|
| 490 |
var objs struct { |
| 491 |
Program *Program `ebpf:"prog1"` |
| 492 |
Map *Map `ebpf:"map2"` |
| 493 |
} |
| 494 |
|
| 495 |
cs := &CollectionSpec{ |
| 496 |
Maps: map[string]*MapSpec{ |
| 497 |
"map1": { |
| 498 |
Type: Array, |
| 499 |
KeySize: 4, |
| 500 |
ValueSize: 4, |
| 501 |
MaxEntries: 1, |
| 502 |
}, |
| 503 |
}, |
| 504 |
Programs: map[string]*ProgramSpec{ |
| 505 |
"prog1": { |
| 506 |
Type: SocketFilter, |
| 507 |
Instructions: asm.Instructions{ |
| 508 |
asm.LoadImm(asm.R0, 0, asm.DWord), |
| 509 |
asm.Return(), |
| 510 |
}, |
| 511 |
License: "MIT", |
| 512 |
}, |
| 513 |
}, |
| 514 |
} |
| 515 |
|
| 516 |
coll := mustNewCollection(t, cs, nil) |
| 517 |
|
| 518 |
qt.Assert(t, qt.IsNotNil(coll.Assign(&objs))) |
| 519 |
|
| 520 |
|
| 521 |
qt.Assert(t, qt.IsNotNil(coll.Programs["prog1"])) |
| 522 |
qt.Assert(t, qt.IsNotNil(coll.Maps["map1"])) |
| 523 |
} |
| 524 |
|
| 525 |
func TestIncompleteLoadAndAssign(t *testing.T) { |
| 526 |
spec := &CollectionSpec{ |
| 527 |
Programs: map[string]*ProgramSpec{ |
| 528 |
"valid": { |
| 529 |
Type: SocketFilter, |
| 530 |
Instructions: asm.Instructions{ |
| 531 |
asm.LoadImm(asm.R0, 0, asm.DWord), |
| 532 |
asm.Return(), |
| 533 |
}, |
| 534 |
License: "MIT", |
| 535 |
}, |
| 536 |
"invalid": { |
| 537 |
Type: SocketFilter, |
| 538 |
Instructions: asm.Instructions{ |
| 539 |
asm.Return(), |
| 540 |
}, |
| 541 |
License: "MIT", |
| 542 |
}, |
| 543 |
}, |
| 544 |
} |
| 545 |
|
| 546 |
s := struct { |
| 547 |
|
| 548 |
Valid *Program `ebpf:"valid"` |
| 549 |
|
| 550 |
Invalid *Program `ebpf:"invalid"` |
| 551 |
}{} |
| 552 |
|
| 553 |
if err := loadAndAssign(t, spec, &s, nil); err == nil { |
| 554 |
t.Fatal("expected error loading invalid ProgramSpec") |
| 555 |
} |
| 556 |
|
| 557 |
if s.Valid == nil { |
| 558 |
t.Fatal("expected valid prog to be non-nil") |
| 559 |
} |
| 560 |
|
| 561 |
if fd := s.Valid.FD(); fd != -1 { |
| 562 |
t.Fatal("expected valid prog to have closed fd -1, got:", fd) |
| 563 |
} |
| 564 |
|
| 565 |
if s.Invalid != nil { |
| 566 |
t.Fatal("expected invalid prog to be nil due to never being assigned") |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
func BenchmarkNewCollection(b *testing.B) { |
| 571 |
file := testutils.NativeFile(b, "testdata/loader-%s.elf") |
| 572 |
spec, err := LoadCollectionSpec(file) |
| 573 |
if err != nil { |
| 574 |
b.Fatal(err) |
| 575 |
} |
| 576 |
|
| 577 |
for _, m := range spec.Maps { |
| 578 |
m.Pinning = PinNone |
| 579 |
} |
| 580 |
|
| 581 |
spec = fixupCollectionSpec(spec) |
| 582 |
|
| 583 |
b.ReportAllocs() |
| 584 |
|
| 585 |
for b.Loop() { |
| 586 |
coll, err := NewCollection(spec) |
| 587 |
if err != nil { |
| 588 |
b.Fatal(err) |
| 589 |
} |
| 590 |
coll.Close() |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
func BenchmarkNewCollectionManyProgs(b *testing.B) { |
| 595 |
file := testutils.NativeFile(b, "testdata/manyprogs-%s.elf") |
| 596 |
spec, err := LoadCollectionSpec(file) |
| 597 |
if err != nil { |
| 598 |
b.Fatal(err) |
| 599 |
} |
| 600 |
|
| 601 |
spec = fixupCollectionSpec(spec) |
| 602 |
|
| 603 |
b.ReportAllocs() |
| 604 |
|
| 605 |
for b.Loop() { |
| 606 |
coll, err := NewCollection(spec) |
| 607 |
if err != nil { |
| 608 |
b.Fatal(err) |
| 609 |
} |
| 610 |
coll.Close() |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
func BenchmarkLoadCollectionManyProgs(b *testing.B) { |
| 615 |
file, err := os.Open(testutils.NativeFile(b, "testdata/manyprogs-%s.elf")) |
| 616 |
qt.Assert(b, qt.IsNil(err)) |
| 617 |
defer file.Close() |
| 618 |
|
| 619 |
b.ReportAllocs() |
| 620 |
|
| 621 |
for b.Loop() { |
| 622 |
_, err := file.Seek(0, io.SeekStart) |
| 623 |
if err != nil { |
| 624 |
b.Fatal(err) |
| 625 |
} |
| 626 |
|
| 627 |
_, err = LoadCollectionSpecFromReader(file) |
| 628 |
if err != nil { |
| 629 |
b.Fatal(err) |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
func ExampleCollectionSpec_Assign() { |
| 635 |
spec := &CollectionSpec{ |
| 636 |
Maps: map[string]*MapSpec{ |
| 637 |
"map1": { |
| 638 |
Type: Array, |
| 639 |
KeySize: 4, |
| 640 |
ValueSize: 4, |
| 641 |
MaxEntries: 1, |
| 642 |
}, |
| 643 |
}, |
| 644 |
Programs: map[string]*ProgramSpec{ |
| 645 |
"prog1": { |
| 646 |
Type: SocketFilter, |
| 647 |
Instructions: asm.Instructions{ |
| 648 |
asm.LoadImm(asm.R0, 0, asm.DWord), |
| 649 |
asm.Return(), |
| 650 |
}, |
| 651 |
License: "MIT", |
| 652 |
}, |
| 653 |
}, |
| 654 |
} |
| 655 |
|
| 656 |
type maps struct { |
| 657 |
Map *MapSpec `ebpf:"map1"` |
| 658 |
} |
| 659 |
|
| 660 |
var specs struct { |
| 661 |
maps |
| 662 |
Program *ProgramSpec `ebpf:"prog1"` |
| 663 |
} |
| 664 |
|
| 665 |
if err := spec.Assign(&specs); err != nil { |
| 666 |
panic(err) |
| 667 |
} |
| 668 |
|
| 669 |
fmt.Println(specs.Program.Type) |
| 670 |
fmt.Println(specs.Map.Type) |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
} |
| 675 |
|
| 676 |
func ExampleCollectionSpec_LoadAndAssign() { |
| 677 |
spec := &CollectionSpec{ |
| 678 |
Maps: map[string]*MapSpec{ |
| 679 |
"map1": { |
| 680 |
Type: Array, |
| 681 |
KeySize: 4, |
| 682 |
ValueSize: 4, |
| 683 |
MaxEntries: 1, |
| 684 |
}, |
| 685 |
}, |
| 686 |
Programs: map[string]*ProgramSpec{ |
| 687 |
"prog1": { |
| 688 |
Type: SocketFilter, |
| 689 |
Instructions: asm.Instructions{ |
| 690 |
asm.LoadImm(asm.R0, 0, asm.DWord), |
| 691 |
asm.Return(), |
| 692 |
}, |
| 693 |
License: "MIT", |
| 694 |
}, |
| 695 |
}, |
| 696 |
} |
| 697 |
|
| 698 |
var objs struct { |
| 699 |
Program *Program `ebpf:"prog1"` |
| 700 |
Map *Map `ebpf:"map1"` |
| 701 |
} |
| 702 |
|
| 703 |
if err := spec.LoadAndAssign(&objs, nil); err != nil { |
| 704 |
panic(err) |
| 705 |
} |
| 706 |
defer objs.Program.Close() |
| 707 |
defer objs.Map.Close() |
| 708 |
} |
| 709 |
|
| 710 |
func TestStructOpsMapSpecSimpleLoadAndAssign(t *testing.T) { |
| 711 |
requireTestmodOps(t) |
| 712 |
|
| 713 |
makeProg := func(attachTo string) map[string]*ProgramSpec { |
| 714 |
return map[string]*ProgramSpec{ |
| 715 |
"test_1": { |
| 716 |
Name: "test_1", |
| 717 |
Type: StructOps, |
| 718 |
AttachTo: attachTo, |
| 719 |
License: "GPL", |
| 720 |
Instructions: asm.Instructions{ |
| 721 |
asm.Mov.Imm(asm.R0, 0), |
| 722 |
asm.Return(), |
| 723 |
}, |
| 724 |
}, |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
funcPtr := &btf.Pointer{ |
| 729 |
Target: &btf.FuncProto{ |
| 730 |
Return: &btf.Int{Name: "int", Size: 4, Encoding: btf.Signed}, |
| 731 |
}, |
| 732 |
} |
| 733 |
|
| 734 |
type testCase struct { |
| 735 |
name string |
| 736 |
withProg bool |
| 737 |
attachTo string |
| 738 |
valueType *btf.Struct |
| 739 |
valueBytes []byte |
| 740 |
} |
| 741 |
|
| 742 |
cases := []testCase{ |
| 743 |
{ |
| 744 |
name: "ops_with_data", |
| 745 |
withProg: true, |
| 746 |
attachTo: "bpf_testmod_ops:test_1", |
| 747 |
valueType: &btf.Struct{ |
| 748 |
Name: "bpf_testmod_ops", |
| 749 |
Size: 16, |
| 750 |
Members: []btf.Member{ |
| 751 |
{ |
| 752 |
Name: "test_1", |
| 753 |
Type: funcPtr, |
| 754 |
Offset: 0, |
| 755 |
}, |
| 756 |
{ |
| 757 |
Name: "data", |
| 758 |
Type: &btf.Int{Name: "int", Size: 4}, |
| 759 |
Offset: 64, |
| 760 |
}, |
| 761 |
}, |
| 762 |
}, |
| 763 |
valueBytes: []byte{ |
| 764 |
|
| 765 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 766 |
|
| 767 |
0xde, 0xed, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, |
| 768 |
}, |
| 769 |
}, |
| 770 |
{ |
| 771 |
name: "ops_only_func", |
| 772 |
withProg: true, |
| 773 |
attachTo: "bpf_testmod_ops2:test_1", |
| 774 |
valueType: &btf.Struct{ |
| 775 |
Name: "bpf_testmod_ops2", |
| 776 |
Size: 8, |
| 777 |
Members: []btf.Member{ |
| 778 |
{ |
| 779 |
Name: "test_1", |
| 780 |
Type: funcPtr, |
| 781 |
Offset: 0, |
| 782 |
}, |
| 783 |
}, |
| 784 |
}, |
| 785 |
valueBytes: []byte{ |
| 786 |
|
| 787 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 788 |
}, |
| 789 |
}, |
| 790 |
{ |
| 791 |
name: "ops_empty_value", |
| 792 |
withProg: false, |
| 793 |
valueType: &btf.Struct{ |
| 794 |
Name: "bpf_testmod_ops2", |
| 795 |
Size: 0, |
| 796 |
Members: []btf.Member{}, |
| 797 |
}, |
| 798 |
valueBytes: []byte{}, |
| 799 |
}, |
| 800 |
} |
| 801 |
|
| 802 |
for _, c := range cases { |
| 803 |
t.Run(c.name, func(t *testing.T) { |
| 804 |
spec := &CollectionSpec{ |
| 805 |
Programs: map[string]*ProgramSpec{}, |
| 806 |
Maps: map[string]*MapSpec{ |
| 807 |
"testmod_ops": { |
| 808 |
Name: "testmod_ops", |
| 809 |
Type: StructOpsMap, |
| 810 |
Flags: sys.BPF_F_LINK, |
| 811 |
Key: &btf.Int{Size: 4}, |
| 812 |
KeySize: 4, |
| 813 |
Value: c.valueType, |
| 814 |
MaxEntries: 1, |
| 815 |
Contents: []MapKV{ |
| 816 |
{ |
| 817 |
Key: uint32(0), |
| 818 |
Value: slices.Clone(c.valueBytes), |
| 819 |
}, |
| 820 |
}, |
| 821 |
}, |
| 822 |
}, |
| 823 |
} |
| 824 |
if c.withProg { |
| 825 |
spec.Programs = makeProg(c.attachTo) |
| 826 |
} |
| 827 |
|
| 828 |
coll := mustNewCollection(t, spec, nil) |
| 829 |
|
| 830 |
for name := range spec.Maps { |
| 831 |
qt.Assert(t, qt.IsNotNil(coll.Maps[name])) |
| 832 |
} |
| 833 |
for name := range spec.Programs { |
| 834 |
qt.Assert(t, qt.IsNotNil(coll.Programs[name])) |
| 835 |
} |
| 836 |
}) |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
func TestLinkedELF(t *testing.T) { |
| 841 |
spec, err := LoadCollectionSpec("testdata/linked-el.elf") |
| 842 |
qt.Assert(t, qt.IsNil(err)) |
| 843 |
|
| 844 |
|
| 845 |
for name, m := range spec.Maps { |
| 846 |
qt.Assert(t, qt.Equals(m.MaxEntries, 1), qt.Commentf(name)) |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
coll := mustNewCollection(t, spec, nil) |
| 853 |
for name, prog := range coll.Programs { |
| 854 |
res := mustRun(t, prog, nil) |
| 855 |
qt.Assert(t, qt.Equals(res, 0), qt.Commentf(name)) |
| 856 |
} |
| 857 |
} |