|
8 | 8 | "math"
|
9 | 9 | "path/filepath"
|
10 | 10 | "runtime"
|
| 11 | + "strings" |
11 | 12 | "time"
|
12 | 13 |
|
13 | 14 | "github.com/cilium/ebpf/asm"
|
@@ -381,38 +382,66 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, c *btf.Cache)
|
381 | 382 | attr.AttachBtfObjFd = uint32(spec.AttachTarget.FD())
|
382 | 383 | defer runtime.KeepAlive(spec.AttachTarget)
|
383 | 384 | } else if spec.AttachTo != "" {
|
384 |
| - module, targetID, err := findProgramTargetInKernel(spec.AttachTo, spec.Type, spec.AttachType) |
| 385 | + var targetMember string |
| 386 | + var ok bool |
| 387 | + attachTo := spec.AttachTo |
| 388 | + |
| 389 | + if spec.Type == StructOps { |
| 390 | + attachTo, targetMember, ok = strings.Cut(attachTo, ":") |
| 391 | + if !ok || attachTo == "" || targetMember == "" { |
| 392 | + return nil, fmt.Errorf("invalid AttachTo for StructOps (type:member): %q", spec.AttachTo) |
| 393 | + } |
| 394 | + } |
| 395 | + |
| 396 | + module, targetID, err := findProgramTargetInKernel(attachTo, spec.Type, spec.AttachType) |
385 | 397 | if err != nil && !errors.Is(err, errUnrecognizedAttachType) {
|
386 | 398 | // We ignore errUnrecognizedAttachType since AttachTo may be non-empty
|
387 | 399 | // for programs that don't attach anywhere.
|
388 | 400 | return nil, fmt.Errorf("attach %s/%s: %w", spec.Type, spec.AttachType, err)
|
389 | 401 | }
|
390 | 402 |
|
391 | 403 | attr.AttachBtfId = targetID
|
392 |
| - if module != nil { |
393 |
| - attr.AttachBtfObjFd = uint32(module.FD()) |
394 |
| - defer module.Close() |
395 |
| - } |
396 |
| - } |
| 404 | + if spec.Type == StructOps { |
| 405 | + // we need to resolve `target` here, load the kernel spec |
| 406 | + s, err := btf.LoadKernelSpec() |
| 407 | + if err != nil { |
| 408 | + return nil, fmt.Errorf("load vmlinux BTF: %w", err) |
| 409 | + } |
397 | 410 |
|
398 |
| - if spec.Type == StructOps { |
399 |
| - if meta, ok := spec.Instructions[0].Metadata.Get(structOpsProgMetaKey{}).(*structOpsProgMeta); ok { |
400 |
| - // set AttachBtfId / ExpectedAttachType from metadata |
401 |
| - attr.AttachBtfId = sys.TypeID(meta.attachBtfId) |
402 |
| - attr.ExpectedAttachType = meta.attachType |
| 411 | + st, spec2, modBtfObjID, err := doFindStructTypeByName(s, attachTo) |
| 412 | + fmt.Println("hogehogehogheoge", st, err) |
| 413 | + if err != nil { |
| 414 | + return nil, err |
| 415 | + } |
403 | 416 |
|
404 |
| - var modH *btf.Handle |
405 |
| - if meta.modBtfObjID != 0 { |
406 |
| - h, err := btf.NewHandleFromID(btf.ID(meta.modBtfObjID)) |
| 417 | + tid, err := spec2.TypeID(st) |
| 418 | + if err != nil { |
| 419 | + return nil, fmt.Errorf("type id for %s: %w", st.TypeName(), err) |
| 420 | + } |
| 421 | + attr.AttachBtfId = sys.TypeID(tid) |
| 422 | + |
| 423 | + idx := getStructMemberIndexByName(st, targetMember) |
| 424 | + fmt.Println(idx, "hogehogehoge") |
| 425 | + if idx < 0 { |
| 426 | + return nil, fmt.Errorf("member %q not found in %s", targetMember, st.Name) |
| 427 | + } |
| 428 | + |
| 429 | + attr.ExpectedAttachType = sys.AttachType(idx) |
| 430 | + |
| 431 | + if modBtfObjID != 0 { |
| 432 | + h, err := btf.NewHandleFromID(btf.ID(modBtfObjID)) |
407 | 433 | if err != nil {
|
408 |
| - return nil, fmt.Errorf("open module BTF handle (id=%d): %w", meta.modBtfObjID, err) |
| 434 | + return nil, fmt.Errorf("open module BTF handle (id=%d): %w", modBtfObjID, err) |
409 | 435 | }
|
410 |
| - modH = h |
411 |
| - // set AttachBtfObjFd if the type comes from a module |
| 436 | + defer h.Close() |
412 | 437 | attr.AttachBtfObjFd = uint32(h.FD())
|
413 |
| - defer modH.Close() |
414 | 438 | }
|
415 | 439 | }
|
| 440 | + |
| 441 | + if module != nil { |
| 442 | + attr.AttachBtfObjFd = uint32(module.FD()) |
| 443 | + defer module.Close() |
| 444 | + } |
416 | 445 | }
|
417 | 446 |
|
418 | 447 | if platform.IsWindows && opts.LogLevel != 0 {
|
@@ -1059,6 +1088,10 @@ func findProgramTargetInKernel(name string, progType ProgramType, attachType Att
|
1059 | 1088 | )
|
1060 | 1089 |
|
1061 | 1090 | switch (match{progType, attachType}) {
|
| 1091 | + case match{StructOps, AttachStructOps}: |
| 1092 | + typeName = name |
| 1093 | + featureName = "struct_ops " + name |
| 1094 | + target = (*btf.Struct)(nil) |
1062 | 1095 | case match{LSM, AttachLSMMac}:
|
1063 | 1096 | typeName = "bpf_lsm_" + name
|
1064 | 1097 | featureName = name + " LSM hook"
|
|
0 commit comments