Debian Codegraph @

link/link_other.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "fmt"
7
8 "github.com/cilium/ebpf/internal"
9 "github.com/cilium/ebpf/internal/platform"
10 "github.com/cilium/ebpf/internal/sys"
11 )
12
13 // Valid link types.
14 const (
15 UnspecifiedType = sys.BPF_LINK_TYPE_UNSPEC
16 RawTracepointType = sys.BPF_LINK_TYPE_RAW_TRACEPOINT
17 TracingType = sys.BPF_LINK_TYPE_TRACING
18 CgroupType = sys.BPF_LINK_TYPE_CGROUP
19 IterType = sys.BPF_LINK_TYPE_ITER
20 NetNsType = sys.BPF_LINK_TYPE_NETNS
21 XDPType = sys.BPF_LINK_TYPE_XDP
22 PerfEventType = sys.BPF_LINK_TYPE_PERF_EVENT
23 KprobeMultiType = sys.BPF_LINK_TYPE_KPROBE_MULTI
24 TCXType = sys.BPF_LINK_TYPE_TCX
25 UprobeMultiType = sys.BPF_LINK_TYPE_UPROBE_MULTI
26 NetfilterType = sys.BPF_LINK_TYPE_NETFILTER
27 NetkitType = sys.BPF_LINK_TYPE_NETKIT
28 StructOpsType = sys.BPF_LINK_TYPE_STRUCT_OPS
29 )
30
31 // AttachRawLink creates a raw link.
32 func AttachRawLink(opts RawLinkOptions) (*RawLink, error) {
33 if err := haveBPFLink(); err != nil {
34 return nil, err
35 }
36
37 if opts.Target < 0 {
38 return nil, fmt.Errorf("invalid target: %s", sys.ErrClosedFd)
39 }
40
41 progFd := opts.Program.FD()
42 if progFd < 0 {
43 return nil, fmt.Errorf("invalid program: %s", sys.ErrClosedFd)
44 }
45
46 p, attachType := platform.DecodeConstant(opts.Attach)
47 if p != platform.Linux {
48 return nil, fmt.Errorf("attach type %s: %w", opts.Attach, internal.ErrNotSupportedOnOS)
49 }
50
51 attr := sys.LinkCreateAttr{
52 TargetFd: uint32(opts.Target),
53 ProgFd: uint32(progFd),
54 AttachType: sys.AttachType(attachType),
55 TargetBtfId: opts.BTF,
56 Flags: opts.Flags,
57 }
58 fd, err := sys.LinkCreate(&attr)
59 if err != nil {
60 return nil, fmt.Errorf("create link: %w", err)
61 }
62
63 return &RawLink{fd, ""}, nil
64 }
65
66 // wrap a RawLink in a more specific type if possible.
67 //
68 // The function takes ownership of raw and closes it on error.
69 func wrapRawLink(raw *RawLink) (_ Link, err error) {
70 defer func() {
71 if err != nil {
72 raw.Close()
73 }
74 }()
75
76 info, err := raw.Info()
77 if err != nil {
78 return nil, err
79 }
80
81 switch info.Type {
82 case RawTracepointType:
83 return &rawTracepoint{*raw}, nil
84 case TracingType:
85 return &tracing{*raw}, nil
86 case CgroupType:
87 return &linkCgroup{*raw}, nil
88 case IterType:
89 return &Iter{*raw}, nil
90 case NetNsType:
91 return &NetNsLink{*raw}, nil
92 case KprobeMultiType:
93 return &kprobeMultiLink{*raw}, nil
94 case UprobeMultiType:
95 return &uprobeMultiLink{*raw}, nil
96 case PerfEventType:
97 return &perfEventLink{*raw, nil}, nil
98 case TCXType:
99 return &tcxLink{*raw}, nil
100 case NetfilterType:
101 return &netfilterLink{*raw}, nil
102 case NetkitType:
103 return &netkitLink{*raw}, nil
104 case XDPType:
105 return &xdpLink{*raw}, nil
106 case StructOpsType:
107 return &structOpsLink{*raw}, nil
108 default:
109 return raw, nil
110 }
111 }
112
113 type TracingInfo struct {
114 AttachType sys.AttachType
115 TargetObjectId uint32
116 TargetBtfId sys.TypeID
117 }
118
119 type CgroupInfo struct {
120 CgroupId uint64
121 AttachType sys.AttachType
122 _ [4]byte
123 }
124
125 type NetNsInfo struct {
126 NetnsInode uint32
127 AttachType sys.AttachType
128 }
129
130 type TCXInfo struct {
131 Ifindex uint32
132 AttachType sys.AttachType
133 }
134
135 type XDPInfo struct {
136 Ifindex uint32
137 }
138
139 type NetfilterInfo struct {
140 ProtocolFamily NetfilterProtocolFamily
141 Hook NetfilterInetHook
142 Priority int32
143 Flags uint32
144 }
145
146 type NetkitInfo struct {
147 Ifindex uint32
148 AttachType sys.AttachType
149 }
150
151 type RawTracepointInfo struct {
152 Name string
153 }
154
155 type KprobeMultiInfo struct {
156 // Count is the number of addresses hooked by the kprobe.
157 Count uint32
158 Flags uint32
159 Missed uint64
160 addrs []uint64
161 cookies []uint64
162 }
163
164 type KprobeMultiAddress struct {
165 Address uint64
166 Cookie uint64
167 }
168
169 // Addresses are the addresses hooked by the kprobe.
170 func (kpm *KprobeMultiInfo) Addresses() ([]KprobeMultiAddress, bool) {
171 if kpm.addrs == nil || len(kpm.addrs) != len(kpm.cookies) {
172 return nil, false
173 }
174 addrs := make([]KprobeMultiAddress, len(kpm.addrs))
175 for i := range kpm.addrs {
176 addrs[i] = KprobeMultiAddress{
177 Address: kpm.addrs[i],
178 Cookie: kpm.cookies[i],
179 }
180 }
181 return addrs, true
182 }
183
184 type UprobeMultiInfo struct {
185 Count uint32
186 Flags uint32
187 Missed uint64
188 offsets []uint64
189 cookies []uint64
190 refCtrOffsets []uint64
191 // File is the path that the file the uprobe was attached to
192 // had at creation time.
193 //
194 // However, due to various circumstances (differing mount namespaces,
195 // file replacement, ...), this path may not point to the same binary
196 // the uprobe was originally attached to.
197 File string
198 pid uint32
199 }
200
201 type UprobeMultiOffset struct {
202 Offset uint64
203 Cookie uint64
204 ReferenceCount uint64
205 }
206
207 // Offsets returns the offsets that the uprobe was attached to along with the related cookies and ref counters.
208 func (umi *UprobeMultiInfo) Offsets() ([]UprobeMultiOffset, bool) {
209 if umi.offsets == nil || len(umi.cookies) != len(umi.offsets) || len(umi.refCtrOffsets) != len(umi.offsets) {
210 return nil, false
211 }
212 var adresses = make([]UprobeMultiOffset, len(umi.offsets))
213 for i := range umi.offsets {
214 adresses[i] = UprobeMultiOffset{
215 Offset: umi.offsets[i],
216 Cookie: umi.cookies[i],
217 ReferenceCount: umi.refCtrOffsets[i],
218 }
219 }
220 return adresses, true
221 }
222
223 // Pid returns the process ID that this uprobe is attached to.
224 //
225 // If it does not exist, the uprobe will trigger for all processes.
226 func (umi *UprobeMultiInfo) Pid() (uint32, bool) {
227 return umi.pid, umi.pid > 0
228 }
229
230 const (
231 PerfEventUnspecified = sys.BPF_PERF_EVENT_UNSPEC
232 PerfEventUprobe = sys.BPF_PERF_EVENT_UPROBE
233 PerfEventUretprobe = sys.BPF_PERF_EVENT_URETPROBE
234 PerfEventKprobe = sys.BPF_PERF_EVENT_KPROBE
235 PerfEventKretprobe = sys.BPF_PERF_EVENT_KRETPROBE
236 PerfEventTracepoint = sys.BPF_PERF_EVENT_TRACEPOINT
237 PerfEventEvent = sys.BPF_PERF_EVENT_EVENT
238 )
239
240 type PerfEventInfo struct {
241 Type sys.PerfEventType
242 extra any
243 }
244
245 func (r *PerfEventInfo) Kprobe() *KprobeInfo {
246 e, _ := r.extra.(*KprobeInfo)
247 return e
248 }
249
250 func (r *PerfEventInfo) Uprobe() *UprobeInfo {
251 e, _ := r.extra.(*UprobeInfo)
252 return e
253 }
254
255 func (r *PerfEventInfo) Tracepoint() *TracepointInfo {
256 e, _ := r.extra.(*TracepointInfo)
257 return e
258 }
259
260 func (r *PerfEventInfo) Event() *EventInfo {
261 e, _ := r.extra.(*EventInfo)
262 return e
263 }
264
265 type KprobeInfo struct {
266 Address uint64
267 Missed uint64
268 Function string
269 Offset uint32
270 }
271
272 type UprobeInfo struct {
273 // File is the path that the file the uprobe was attached to
274 // had at creation time.
275 //
276 // However, due to various circumstances (differing mount namespaces,
277 // file replacement, ...), this path may not point to the same binary
278 // the uprobe was originally attached to.
279 File string
280 Offset uint32
281 Cookie uint64
282 OffsetReferenceCount uint64
283 }
284
285 type TracepointInfo struct {
286 Tracepoint string
287 Cookie uint64
288 }
289
290 type EventInfo struct {
291 Config uint64
292 Type uint32
293 Cookie uint64
294 }
295
296 // Tracing returns tracing type-specific link info.
297 //
298 // Returns nil if the type-specific link info isn't available.
299 func (r Info) Tracing() *TracingInfo {
300 e, _ := r.extra.(*TracingInfo)
301 return e
302 }
303
304 // Cgroup returns cgroup type-specific link info.
305 //
306 // Returns nil if the type-specific link info isn't available.
307 func (r Info) Cgroup() *CgroupInfo {
308 e, _ := r.extra.(*CgroupInfo)
309 return e
310 }
311
312 // NetNs returns netns type-specific link info.
313 //
314 // Returns nil if the type-specific link info isn't available.
315 func (r Info) NetNs() *NetNsInfo {
316 e, _ := r.extra.(*NetNsInfo)
317 return e
318 }
319
320 // XDP returns XDP type-specific link info.
321 //
322 // Returns nil if the type-specific link info isn't available.
323 func (r Info) XDP() *XDPInfo {
324 e, _ := r.extra.(*XDPInfo)
325 return e
326 }
327
328 // TCX returns TCX type-specific link info.
329 //
330 // Returns nil if the type-specific link info isn't available.
331 func (r Info) TCX() *TCXInfo {
332 e, _ := r.extra.(*TCXInfo)
333 return e
334 }
335
336 // Netfilter returns netfilter type-specific link info.
337 //
338 // Returns nil if the type-specific link info isn't available.
339 func (r Info) Netfilter() *NetfilterInfo {
340 e, _ := r.extra.(*NetfilterInfo)
341 return e
342 }
343
344 // Netkit returns netkit type-specific link info.
345 //
346 // Returns nil if the type-specific link info isn't available.
347 func (r Info) Netkit() *NetkitInfo {
348 e, _ := r.extra.(*NetkitInfo)
349 return e
350 }
351
352 // KprobeMulti returns kprobe-multi type-specific link info.
353 //
354 // Returns nil if the type-specific link info isn't available.
355 func (r Info) KprobeMulti() *KprobeMultiInfo {
356 e, _ := r.extra.(*KprobeMultiInfo)
357 return e
358 }
359
360 // UprobeMulti returns uprobe-multi type-specific link info.
361 //
362 // Returns nil if the type-specific link info isn't available.
363 func (r Info) UprobeMulti() *UprobeMultiInfo {
364 e, _ := r.extra.(*UprobeMultiInfo)
365 return e
366 }
367
368 // PerfEvent returns perf-event type-specific link info.
369 //
370 // Returns nil if the type-specific link info isn't available.
371 func (r Info) PerfEvent() *PerfEventInfo {
372 e, _ := r.extra.(*PerfEventInfo)
373 return e
374 }
375
376 // RawTracepoint returns raw-tracepoint type-specific link info.
377 //
378 // Returns nil if the type-specific link info isn't available.
379 func (r Info) RawTracepoint() *RawTracepointInfo {
380 e, _ := r.extra.(*RawTracepointInfo)
381 return e
382 }