Debian Codegraph @

features/link.go

raw symbols SCIP
1 package features
2
3 import (
4 "errors"
5
6 "github.com/cilium/ebpf"
7 "github.com/cilium/ebpf/asm"
8 "github.com/cilium/ebpf/internal"
9 "github.com/cilium/ebpf/internal/sys"
10 "github.com/cilium/ebpf/internal/unix"
11 )
12
13 // HaveBPFLinkUprobeMulti probes the running kernel if uprobe_multi link is supported.
14 //
15 // See the package documentation for the meaning of the error return value.
16 func HaveBPFLinkUprobeMulti() error {
17 return haveBPFLinkUprobeMulti()
18 }
19
20 var haveBPFLinkUprobeMulti = internal.NewFeatureTest("bpf_link_uprobe_multi", func() error {
21 prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
22 Name: "probe_upm_link",
23 Type: ebpf.Kprobe,
24 Instructions: asm.Instructions{
25 asm.Mov.Imm(asm.R0, 0),
26 asm.Return(),
27 },
28 AttachType: ebpf.AttachTraceUprobeMulti,
29 License: "MIT",
30 })
31 if errors.Is(err, unix.E2BIG) {
32 // Kernel doesn't support AttachType field.
33 return ebpf.ErrNotSupported
34 }
35 if err != nil {
36 return err
37 }
38 defer prog.Close()
39
40 // We try to create uprobe multi link on '/' path which results in
41 // error with -EBADF in case uprobe multi link is supported.
42 fd, err := sys.LinkCreateUprobeMulti(&sys.LinkCreateUprobeMultiAttr{
43 ProgFd: uint32(prog.FD()),
44 AttachType: sys.BPF_TRACE_UPROBE_MULTI,
45 Path: sys.NewStringPointer("/"),
46 Offsets: sys.SlicePointer([]uint64{0}),
47 Count: 1,
48 })
49 switch {
50 case errors.Is(err, unix.EBADF):
51 return nil
52 case errors.Is(err, unix.EINVAL):
53 return ebpf.ErrNotSupported
54 case err != nil:
55 return err
56 }
57
58 // should not happen
59 fd.Close()
60 return errors.New("successfully attached uprobe_multi to /, kernel bug?")
61 }, "6.6")
62
63 // HaveBPFLinkKprobeMulti probes the running kernel if kprobe_multi link is supported.
64 //
65 // See the package documentation for the meaning of the error return value.
66 func HaveBPFLinkKprobeMulti() error {
67 return haveBPFLinkKprobeMulti()
68 }
69
70 var haveBPFLinkKprobeMulti = internal.NewFeatureTest("bpf_link_kprobe_multi", func() error {
71 prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
72 Name: "probe_kpm_link",
73 Type: ebpf.Kprobe,
74 Instructions: asm.Instructions{
75 asm.Mov.Imm(asm.R0, 0),
76 asm.Return(),
77 },
78 AttachType: ebpf.AttachTraceKprobeMulti,
79 License: "MIT",
80 })
81 if errors.Is(err, unix.E2BIG) {
82 // Kernel doesn't support AttachType field.
83 return ebpf.ErrNotSupported
84 }
85 if err != nil {
86 return err
87 }
88 defer prog.Close()
89
90 fd, err := sys.LinkCreateKprobeMulti(&sys.LinkCreateKprobeMultiAttr{
91 ProgFd: uint32(prog.FD()),
92 AttachType: sys.BPF_TRACE_KPROBE_MULTI,
93 Count: 1,
94 Syms: sys.NewStringSlicePointer([]string{"vprintk"}),
95 })
96 switch {
97 case errors.Is(err, unix.EINVAL):
98 return ebpf.ErrNotSupported
99 // If CONFIG_FPROBE isn't set.
100 case errors.Is(err, unix.EOPNOTSUPP):
101 return ebpf.ErrNotSupported
102 case err != nil:
103 return err
104 }
105
106 fd.Close()
107
108 return nil
109 }, "5.18")
110
111 // HaveBPFLinkKprobeSession probes the running kernel if kprobe_session link is supported.
112 //
113 // See the package documentation for the meaning of the error return value.
114 func HaveBPFLinkKprobeSession() error {
115 return haveBPFLinkKprobeSession()
116 }
117
118 var haveBPFLinkKprobeSession = internal.NewFeatureTest("bpf_link_kprobe_session", func() error {
119 prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
120 Name: "probe_kps_link",
121 Type: ebpf.Kprobe,
122 Instructions: asm.Instructions{
123 asm.Mov.Imm(asm.R0, 0),
124 asm.Return(),
125 },
126 AttachType: ebpf.AttachTraceKprobeSession,
127 License: "MIT",
128 })
129 if errors.Is(err, unix.E2BIG) {
130 // Kernel doesn't support AttachType field.
131 return ebpf.ErrNotSupported
132 }
133 if err != nil {
134 return err
135 }
136 defer prog.Close()
137
138 fd, err := sys.LinkCreateKprobeMulti(&sys.LinkCreateKprobeMultiAttr{
139 ProgFd: uint32(prog.FD()),
140 AttachType: sys.BPF_TRACE_KPROBE_SESSION,
141 Count: 1,
142 Syms: sys.NewStringSlicePointer([]string{"vprintk"}),
143 })
144 switch {
145 case errors.Is(err, unix.EINVAL):
146 return ebpf.ErrNotSupported
147 // If CONFIG_FPROBE isn't set.
148 case errors.Is(err, unix.EOPNOTSUPP):
149 return ebpf.ErrNotSupported
150 case err != nil:
151 return err
152 }
153
154 fd.Close()
155
156 return nil
157 }, "6.10")