@@ -3,6 +3,7 @@ package ebpf
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "strings"
6
7
7
8
"github.com/cilium/ebpf/btf"
8
9
"github.com/cilium/ebpf/internal/sys"
@@ -19,8 +20,6 @@ type structOpsKernTypes struct {
19
20
typeID btf.TypeID
20
21
// value struct "bpf_struct_ops_<name>" that contains typ.
21
22
valueType * btf.Struct
22
- // The *btf.Member within valueType that embeds typ.
23
- dataMember * btf.Member
24
23
// The BTF object ID of the module where the type was found
25
24
// 0 if resolved in vmlinux.
26
25
modBtfObjId uint32
@@ -64,34 +63,6 @@ type structOpsSpec struct {
64
63
progAttachBtfID btf.TypeID
65
64
}
66
65
67
- // findByTypeFromStruct searches for the first member of a struct whose
68
- // resolved BTF type ID matches the given typ.
69
- //
70
- // It resolves the BTF type ID of typ and compares it against each
71
- // member’s TypeID in st.Members. If a match is found, the corresponding
72
- // *btf.Member is returned.
73
- //
74
- // Returns an error if typ cannot be resolved, if any member type
75
- // resolution fails, or if no member with the requested type exists.
76
- func findByTypeFromStruct (spec * btf.Spec , st * btf.Struct , typ btf.Type ) (* btf.Member , error ) {
77
- typeId , err := spec .TypeID (typ )
78
- if err != nil {
79
- return nil , fmt .Errorf ("failed to resolve typeId for %s: %w" , (typ ).TypeName (), err )
80
- }
81
-
82
- for _ , member := range st .Members {
83
- memberTypeId , err := spec .TypeID (member .Type )
84
- if err != nil {
85
- return nil , fmt .Errorf ("failed to resolve typeId for %s: %w" , member .Name , err )
86
- }
87
- if memberTypeId == typeId {
88
- return & member , nil
89
- }
90
- }
91
-
92
- return nil , fmt .Errorf ("member of type %s not found in %s" , typ .TypeName (), st .Name )
93
- }
94
-
95
66
// findStructTypeByName looks up a struct type with the exact name in the
96
67
// provided base BTF spec, and falls back to scanning all loaded module BTFs
97
68
// if it is not present in vmlinux.
@@ -168,32 +139,26 @@ func findStructTypeByNameFromModule(base *btf.Spec, name string) (*btf.Struct, *
168
139
// findStructOpsKernTypes discovers all kernel-side BTF artifacts that belong to
169
140
// a given struct_ops, identified by the user-visible base struct name
170
141
// (e.g., "tcp_congestion_ops").
171
- func findStructOpsKernTypes (kType * btf.Struct ) (* structOpsKernTypes , error ) {
142
+ func findStructOpsKernTypes (valueType * btf.Struct ) (* structOpsKernTypes , error ) {
172
143
spec , err := btf .LoadKernelSpec ()
173
144
if err != nil {
174
145
return nil , fmt .Errorf ("load vmlinux BTF: %w" , err )
175
146
}
176
147
177
148
// 1. kernel target struct (e.g. tcp_congestion_ops)
178
- kType , s , modID , err := findStructTypeByName (spec , kType .Name )
149
+ kTypeName := strings .TrimPrefix (valueType .Name , structOpsValuePrefix )
150
+ kType , s , modID , err := findStructTypeByName (spec , kTypeName )
179
151
if err != nil {
180
152
return nil , fmt .Errorf ("struct type: %s %w" , kType .TypeName (), err )
181
153
}
182
154
183
155
// 2. value struct (bpf_struct_ops_<name>)
184
- vTypeName := structOpsValuePrefix + kType .Name
185
- vType , _ , _ , err := findStructTypeByName (s , vTypeName )
156
+ vType , _ , _ , err := findStructTypeByName (s , valueType .Name )
186
157
if err != nil {
187
158
return nil , fmt .Errorf ("kern struct type for %s %w" , kType .TypeName (), err )
188
159
}
189
160
190
- // 3. member “data” that embeds the real ops
191
- dataMem , err := findByTypeFromStruct (s , vType , kType )
192
- if err != nil {
193
- return nil , err
194
- }
195
-
196
- // 4. type-ID of kernel target
161
+ // 3. type-ID of kernel target
197
162
kID , err := s .TypeID (kType )
198
163
if err != nil {
199
164
return nil , fmt .Errorf ("type ID of %s: %w" , kType .TypeName (), err )
@@ -204,7 +169,6 @@ func findStructOpsKernTypes(kType *btf.Struct) (*structOpsKernTypes, error) {
204
169
typ : kType ,
205
170
typeID : kID ,
206
171
valueType : vType ,
207
- dataMember : dataMem ,
208
172
modBtfObjId : uint32 (modID ),
209
173
}, nil
210
174
}
0 commit comments