Debian Codegraph @

syscalls.go

raw symbols SCIP
1 package ebpf
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "math"
8 "os"
9 "runtime"
10 "strings"
11
12 "github.com/cilium/ebpf/asm"
13 "github.com/cilium/ebpf/internal"
14 "github.com/cilium/ebpf/internal/linux"
15 "github.com/cilium/ebpf/internal/platform"
16 "github.com/cilium/ebpf/internal/sys"
17 "github.com/cilium/ebpf/internal/tracefs"
18 "github.com/cilium/ebpf/internal/unix"
19 )
20
21 var (
22 // pre-allocating these here since they may
23 // get called in hot code paths and cause
24 // unnecessary memory allocations
25 sysErrKeyNotExist = sys.Error(ErrKeyNotExist, unix.ENOENT)
26 sysErrKeyExist = sys.Error(ErrKeyExist, unix.EEXIST)
27 sysErrNotSupported = sys.Error(ErrNotSupported, sys.ENOTSUPP)
28 )
29
30 // sanitizeName replaces all invalid characters in name with replacement.
31 // Passing a negative value for replacement will delete characters instead
32 // of replacing them.
33 //
34 // The set of allowed characters may change over time.
35 func sanitizeName(name string, replacement rune) string {
36 return strings.Map(func(char rune) rune {
37 switch {
38 case char >= 'A' && char <= 'Z':
39 return char
40 case char >= 'a' && char <= 'z':
41 return char
42 case char >= '0' && char <= '9':
43 return char
44 case char == '.':
45 return char
46 case char == '_':
47 return char
48 default:
49 return replacement
50 }
51 }, name)
52 }
53
54 func maybeFillObjName(name string) sys.ObjName {
55 if errors.Is(haveObjName(), ErrNotSupported) {
56 return sys.ObjName{}
57 }
58
59 name = sanitizeName(name, -1)
60 if errors.Is(objNameAllowsDot(), ErrNotSupported) {
61 name = strings.ReplaceAll(name, ".", "")
62 }
63
64 return sys.NewObjName(name)
65 }
66
67 func progLoad(insns asm.Instructions, typ ProgramType, license string) (*sys.FD, error) {
68 buf := bytes.NewBuffer(make([]byte, 0, insns.Size()))
69 if err := insns.Marshal(buf, internal.NativeEndian); err != nil {
70 return nil, err
71 }
72 bytecode := buf.Bytes()
73
74 return sys.ProgLoad(&sys.ProgLoadAttr{
75 ProgType: sys.ProgType(typ),
76 License: sys.NewStringPointer(license),
77 Insns: sys.SlicePointer(bytecode),
78 InsnCnt: uint32(len(bytecode) / asm.InstructionSize),
79 })
80 }
81
82 var haveNestedMaps = internal.NewFeatureTest("nested maps", func() error {
83 if platform.IsWindows {
84 // We only support efW versions which have this feature, no need to probe.
85 return nil
86 }
87
88 _, err := sys.MapCreate(&sys.MapCreateAttr{
89 MapType: sys.MapType(ArrayOfMaps),
90 KeySize: 4,
91 ValueSize: 4,
92 MaxEntries: 1,
93 // Invalid file descriptor.
94 InnerMapFd: ^uint32(0),
95 })
96 if errors.Is(err, unix.EINVAL) {
97 return internal.ErrNotSupported
98 }
99 if errors.Is(err, unix.EBADF) {
100 return nil
101 }
102 return err
103 }, "4.12", "windows:0.21.0")
104
105 var haveMapMutabilityModifiers = internal.NewFeatureTest("read- and write-only maps", func() error {
106 // This checks BPF_F_RDONLY_PROG and BPF_F_WRONLY_PROG. Since
107 // BPF_MAP_FREEZE appeared in 5.2 as well we don't do a separate check.
108 m, err := sys.MapCreate(&sys.MapCreateAttr{
109 MapType: sys.MapType(Array),
110 KeySize: 4,
111 ValueSize: 4,
112 MaxEntries: 1,
113 MapFlags: sys.BPF_F_RDONLY_PROG,
114 })
115 if err != nil {
116 return internal.ErrNotSupported
117 }
118 _ = m.Close()
119 return nil
120 }, "5.2")
121
122 var haveMmapableMaps = internal.NewFeatureTest("mmapable maps", func() error {
123 // This checks BPF_F_MMAPABLE, which appeared in 5.5 for array maps.
124 m, err := sys.MapCreate(&sys.MapCreateAttr{
125 MapType: sys.MapType(Array),
126 KeySize: 4,
127 ValueSize: 4,
128 MaxEntries: 1,
129 MapFlags: sys.BPF_F_MMAPABLE,
130 })
131 if err != nil {
132 return internal.ErrNotSupported
133 }
134 _ = m.Close()
135 return nil
136 }, "5.5")
137
138 var haveInnerMaps = internal.NewFeatureTest("inner maps", func() error {
139 // This checks BPF_F_INNER_MAP, which appeared in 5.10.
140 m, err := sys.MapCreate(&sys.MapCreateAttr{
141 MapType: sys.MapType(Array),
142 KeySize: 4,
143 ValueSize: 4,
144 MaxEntries: 1,
145 MapFlags: sys.BPF_F_INNER_MAP,
146 })
147
148 if err != nil {
149 return internal.ErrNotSupported
150 }
151 _ = m.Close()
152 return nil
153 }, "5.10")
154
155 var haveNoPreallocMaps = internal.NewFeatureTest("prealloc maps", func() error {
156 // This checks BPF_F_NO_PREALLOC, which appeared in 4.6.
157 m, err := sys.MapCreate(&sys.MapCreateAttr{
158 MapType: sys.MapType(Hash),
159 KeySize: 4,
160 ValueSize: 4,
161 MaxEntries: 1,
162 MapFlags: sys.BPF_F_NO_PREALLOC,
163 })
164
165 if err != nil {
166 return internal.ErrNotSupported
167 }
168 _ = m.Close()
169 return nil
170 }, "4.6")
171
172 func wrapMapError(err error) error {
173 if err == nil {
174 return nil
175 }
176
177 if errors.Is(err, unix.ENOENT) {
178 return sysErrKeyNotExist
179 }
180
181 if errors.Is(err, unix.EEXIST) {
182 return sysErrKeyExist
183 }
184
185 if errors.Is(err, sys.ENOTSUPP) {
186 return sysErrNotSupported
187 }
188
189 if errors.Is(err, unix.E2BIG) {
190 return fmt.Errorf("key too big for map: %w", err)
191 }
192
193 return err
194 }
195
196 var haveObjName = internal.NewFeatureTest("object names", func() error {
197 if platform.IsWindows {
198 // We only support efW versions which have this feature, no need to probe.
199 return nil
200 }
201
202 attr := sys.MapCreateAttr{
203 MapType: sys.MapType(Array),
204 KeySize: 4,
205 ValueSize: 4,
206 MaxEntries: 1,
207 MapName: sys.NewObjName("feature_test"),
208 }
209
210 fd, err := sys.MapCreate(&attr)
211 if err != nil {
212 return internal.ErrNotSupported
213 }
214
215 _ = fd.Close()
216 return nil
217 }, "4.15", "windows:0.21.0")
218
219 var objNameAllowsDot = internal.NewFeatureTest("dot in object names", func() error {
220 if platform.IsWindows {
221 // We only support efW versions which have this feature, no need to probe.
222 return nil
223 }
224
225 if err := haveObjName(); err != nil {
226 return err
227 }
228
229 attr := sys.MapCreateAttr{
230 MapType: sys.MapType(Array),
231 KeySize: 4,
232 ValueSize: 4,
233 MaxEntries: 1,
234 MapName: sys.NewObjName(".test"),
235 }
236
237 fd, err := sys.MapCreate(&attr)
238 if err != nil {
239 return internal.ErrNotSupported
240 }
241
242 _ = fd.Close()
243 return nil
244 }, "5.2", "windows:0.21.0")
245
246 var haveBatchAPI = internal.NewFeatureTest("map batch api", func() error {
247 var maxEntries uint32 = 2
248 attr := sys.MapCreateAttr{
249 MapType: sys.MapType(Hash),
250 KeySize: 4,
251 ValueSize: 4,
252 MaxEntries: maxEntries,
253 }
254
255 fd, err := sys.MapCreate(&attr)
256 if err != nil {
257 return internal.ErrNotSupported
258 }
259 defer fd.Close()
260
261 keys := []uint32{1, 2}
262 values := []uint32{3, 4}
263 kp, _ := marshalMapSyscallInput(keys, 8)
264 vp, _ := marshalMapSyscallInput(values, 8)
265
266 err = sys.MapUpdateBatch(&sys.MapUpdateBatchAttr{
267 MapFd: fd.Uint(),
268 Keys: kp,
269 Values: vp,
270 Count: maxEntries,
271 })
272 if err != nil {
273 return internal.ErrNotSupported
274 }
275 return nil
276 }, "5.6")
277
278 var haveProbeReadKernel = internal.NewFeatureTest("bpf_probe_read_kernel", func() error {
279 insns := asm.Instructions{
280 asm.Mov.Reg(asm.R1, asm.R10),
281 asm.Add.Imm(asm.R1, -8),
282 asm.Mov.Imm(asm.R2, 8),
283 asm.Mov.Imm(asm.R3, 0),
284 asm.FnProbeReadKernel.Call(),
285 asm.Return(),
286 }
287
288 fd, err := progLoad(insns, Kprobe, "GPL")
289 if err != nil {
290 return internal.ErrNotSupported
291 }
292 _ = fd.Close()
293 return nil
294 }, "5.5")
295
296 var haveBPFToBPFCalls = internal.NewFeatureTest("bpf2bpf calls", func() error {
297 insns := asm.Instructions{
298 asm.Call.Label("prog2").WithSymbol("prog1"),
299 asm.Return(),
300 asm.Mov.Imm(asm.R0, 0).WithSymbol("prog2"),
301 asm.Return(),
302 }
303
304 fd, err := progLoad(insns, SocketFilter, "MIT")
305 if err != nil {
306 return internal.ErrNotSupported
307 }
308 _ = fd.Close()
309 return nil
310 }, "4.16")
311
312 var haveSyscallWrapper = internal.NewFeatureTest("syscall wrapper", func() error {
313 prefix := linux.PlatformPrefix()
314 if prefix == "" {
315 return fmt.Errorf("unable to find the platform prefix for (%s)", runtime.GOARCH)
316 }
317
318 args := tracefs.ProbeArgs{
319 Type: tracefs.Kprobe,
320 Symbol: prefix + "sys_bpf",
321 Pid: -1,
322 }
323
324 var err error
325 args.Group, err = tracefs.RandomGroup("ebpf_probe")
326 if err != nil {
327 return err
328 }
329
330 evt, err := tracefs.NewEvent(args)
331 if errors.Is(err, os.ErrNotExist) {
332 return internal.ErrNotSupported
333 }
334 if err != nil {
335 return err
336 }
337
338 return evt.Close()
339 }, "4.17")
340
341 var haveProgramExtInfos = internal.NewFeatureTest("program ext_infos", func() error {
342 insns := asm.Instructions{
343 asm.Mov.Imm(asm.R0, 0),
344 asm.Return(),
345 }
346
347 buf := bytes.NewBuffer(make([]byte, 0, insns.Size()))
348 if err := insns.Marshal(buf, internal.NativeEndian); err != nil {
349 return err
350 }
351 bytecode := buf.Bytes()
352
353 _, err := sys.ProgLoad(&sys.ProgLoadAttr{
354 ProgType: sys.ProgType(SocketFilter),
355 License: sys.NewStringPointer("MIT"),
356 Insns: sys.SlicePointer(bytecode),
357 InsnCnt: uint32(len(bytecode) / asm.InstructionSize),
358 FuncInfoCnt: 1,
359 ProgBtfFd: math.MaxUint32,
360 })
361
362 if errors.Is(err, unix.EBADF) {
363 return nil
364 }
365
366 if errors.Is(err, unix.E2BIG) {
367 return ErrNotSupported
368 }
369
370 return err
371 }, "5.0")