@@ -3,66 +3,12 @@ package ebpf
3
3
import (
4
4
"errors"
5
5
"fmt"
6
- "strings"
7
6
8
7
"github.com/cilium/ebpf/btf"
9
- "github.com/cilium/ebpf/internal/sys"
10
8
)
11
9
12
10
const structOpsValuePrefix = "bpf_struct_ops_"
13
11
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
-
66
12
// findStructTypeByName looks up a struct type with the exact name in the
67
13
// provided base BTF spec, and falls back to scanning all loaded module BTFs
68
14
// if it is not present in vmlinux.
@@ -136,43 +82,6 @@ func findStructTypeByNameFromModule(base *btf.Spec, name string) (*btf.Struct, *
136
82
return nil , nil , 0 , btf .ErrNotFound
137
83
}
138
84
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
-
176
85
// getStructMemberIndexByName returns the index of `member` within struct `s` by
177
86
// comparing the member name.
178
87
func getStructMemberIndexByName (s * btf.Struct , name string ) int {
0 commit comments