Skip to content

Commit 001149b

Browse files
committed
struct_ops: remove necessary function and struct type
Signed-off-by: shun159 <[email protected]>
1 parent 5b12ed2 commit 001149b

File tree

2 files changed

+9
-128
lines changed

2 files changed

+9
-128
lines changed

collection.go

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,6 @@ type collectionLoader struct {
418418
programs map[string]*Program
419419
vars map[string]*Variable
420420
types *btf.Cache
421-
// map name -> structOpsSpec
422-
stOpsSpecs map[string]*structOpsSpec
423421
// structOps program name -> structOpsMap name
424422
stOpsProgsToMap map[string]string
425423
}
@@ -447,7 +445,6 @@ func newCollectionLoader(coll *CollectionSpec, opts *CollectionOptions) (*collec
447445
make(map[string]*Program),
448446
make(map[string]*Variable),
449447
newBTFCache(&opts.Programs),
450-
make(map[string]*structOpsSpec),
451448
make(map[string]string),
452449
}, nil
453450
}
@@ -593,25 +590,6 @@ func (cl *collectionLoader) loadProgram(progName string) (*Program, error) {
593590

594591
cl.programs[progName] = prog
595592

596-
if prog.Type() == StructOps {
597-
mapName, ok := cl.stOpsProgsToMap[prog.name]
598-
if !ok {
599-
return nil, fmt.Errorf("program %s should be linked to a StructOpsMap", prog.name)
600-
}
601-
602-
structOps, ok := cl.stOpsSpecs[mapName]
603-
if !ok {
604-
return nil, fmt.Errorf("program %s has been loaded but not associated", prog.name)
605-
}
606-
607-
attachType := structOps.progAttachType[prog.name]
608-
if int(attachType) > len(structOps.kernTypes.typ.Members) {
609-
return nil, fmt.Errorf("program %s: unexpected attach type %d",
610-
prog.name, attachType)
611-
}
612-
structOps.programName[attachType] = progSpec.Name
613-
}
614-
615593
return prog, nil
616594
}
617595

@@ -862,6 +840,11 @@ func populateFuncPtr(vType *btf.Struct, data []byte, programs map[string]*Progra
862840
// initKernStructOps indexes struct_ops maps: resolve kernel types, allocate per-map state,
863841
// and stage copy/attach metadata. No kernel objects are created here.
864842
func (cl *collectionLoader) initKernStructOps() error {
843+
s, err := btf.LoadKernelSpec()
844+
if err != nil {
845+
return fmt.Errorf("load vmlinux BTF: %w", err)
846+
}
847+
865848
for _, ms := range cl.coll.Maps {
866849
if ms.Type != StructOpsMap {
867850
continue
@@ -877,24 +860,13 @@ func (cl *collectionLoader) initKernStructOps() error {
877860
return fmt.Errorf("user struct type should be a Struct")
878861
}
879862

880-
// resolve kernel-side types (target + value type)
881-
kernTypes, err := findStructOpsKernTypes(vType)
863+
kTypeName := strings.TrimPrefix(vType.Name, structOpsValuePrefix)
864+
kType, _, _, err := findStructTypeByName(s, kTypeName)
882865
if err != nil {
883-
return fmt.Errorf("find kern_type: %w", err)
884-
}
885-
886-
// allocate per-map state (names, offsets, kernel buffer, attach types, typeID)
887-
structOps := &structOpsSpec{
888-
make([]string, len(kernTypes.typ.Members)),
889-
make([]int, len(kernTypes.typ.Members)),
890-
make([]byte, kernTypes.valueType.Size),
891-
kernTypes,
892-
make(map[string]sys.AttachType),
893-
kernTypes.typeID,
866+
return fmt.Errorf("struct type: %s %w", kTypeName, err)
894867
}
895868

896-
cl.stOpsSpecs[ms.Name] = structOps
897-
cl.setStructOpsProgAttachTo(kernTypes.typ, ms.Name)
869+
cl.setStructOpsProgAttachTo(kType, ms.Name)
898870
}
899871

900872
return nil

struct_ops.go

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,12 @@ package ebpf
33
import (
44
"errors"
55
"fmt"
6-
"strings"
76

87
"github.com/cilium/ebpf/btf"
9-
"github.com/cilium/ebpf/internal/sys"
108
)
119

1210
const structOpsValuePrefix = "bpf_struct_ops_"
1311

14-
// structOpsKernTypes groups all kernel-side BTF artefacts that belong to a
15-
// resolved struct_ops.
16-
type structOpsKernTypes struct {
17-
spec *btf.Spec
18-
// target kernel struct type (e.g. tcp_congestion_ops).
19-
typ *btf.Struct
20-
typeID btf.TypeID
21-
// value struct "bpf_struct_ops_<name>" that contains typ.
22-
valueType *btf.Struct
23-
// The BTF object ID of the module where the type was found
24-
// 0 if resolved in vmlinux.
25-
modBtfObjId uint32
26-
}
27-
28-
// used to holds "environment specific" data
29-
type structOpsSpec struct {
30-
// programName keeps track of program symbols by attach order.
31-
programName []string
32-
33-
// kernFuncOff contains the byte offsets into kernVData where
34-
// program FDs must be written for function pointer members.
35-
kernFuncOff []int
36-
37-
/*
38-
* kernVData mirrors the kernel-side representation of the
39-
* struct_ops type, including its nested data. For example:
40-
*
41-
* struct bpf_struct_ops_tcp_congestion_ops {
42-
* [... kernel internal fields ...]
43-
* struct tcp_congestion_ops data;
44-
* }
45-
*
46-
* In this case, len(kernVData) == sizeof(struct bpf_struct_ops_tcp_congestion_ops).
47-
* copyDataMember() will copy user-supplied data
48-
* into kernVData, which is then pushed into the map.
49-
*/
50-
kernVData []byte
51-
52-
// kernTypes describes the BTF types of the target struct_ops
53-
// object and its nested members, used for resolving offsets
54-
// and function pointer
55-
kernTypes *structOpsKernTypes
56-
57-
// progAttachType maps program names to the sys.AttachType
58-
// expected by the kernel when attaching each function pointer.
59-
progAttachType map[string]sys.AttachType
60-
61-
// progAttachBtfID holds the BTF type ID of the struct_ops
62-
// target in vmlinux
63-
progAttachBtfID btf.TypeID
64-
}
65-
6612
// findStructTypeByName looks up a struct type with the exact name in the
6713
// provided base BTF spec, and falls back to scanning all loaded module BTFs
6814
// if it is not present in vmlinux.
@@ -136,43 +82,6 @@ func findStructTypeByNameFromModule(base *btf.Spec, name string) (*btf.Struct, *
13682
return nil, nil, 0, btf.ErrNotFound
13783
}
13884

139-
// findStructOpsKernTypes discovers all kernel-side BTF artifacts that belong to
140-
// a given struct_ops, identified by the user-visible base struct name
141-
// (e.g., "tcp_congestion_ops").
142-
func findStructOpsKernTypes(valueType *btf.Struct) (*structOpsKernTypes, error) {
143-
spec, err := btf.LoadKernelSpec()
144-
if err != nil {
145-
return nil, fmt.Errorf("load vmlinux BTF: %w", err)
146-
}
147-
148-
// 1. kernel target struct (e.g. tcp_congestion_ops)
149-
kTypeName := strings.TrimPrefix(valueType.Name, structOpsValuePrefix)
150-
kType, s, modID, err := findStructTypeByName(spec, kTypeName)
151-
if err != nil {
152-
return nil, fmt.Errorf("struct type: %s %w", kType.TypeName(), err)
153-
}
154-
155-
// 2. value struct (bpf_struct_ops_<name>)
156-
vType, _, _, err := findStructTypeByName(s, valueType.Name)
157-
if err != nil {
158-
return nil, fmt.Errorf("kern struct type for %s %w", kType.TypeName(), err)
159-
}
160-
161-
// 3. type-ID of kernel target
162-
kID, err := s.TypeID(kType)
163-
if err != nil {
164-
return nil, fmt.Errorf("type ID of %s: %w", kType.TypeName(), err)
165-
}
166-
167-
return &structOpsKernTypes{
168-
spec: s,
169-
typ: kType,
170-
typeID: kID,
171-
valueType: vType,
172-
modBtfObjId: uint32(modID),
173-
}, nil
174-
}
175-
17685
// getStructMemberIndexByName returns the index of `member` within struct `s` by
17786
// comparing the member name.
17887
func getStructMemberIndexByName(s *btf.Struct, name string) int {

0 commit comments

Comments
 (0)