Debian Codegraph @

features/misc.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 )
11
12 // HaveLargeInstructions probes the running kernel if more than 4096 instructions
13 // per program are supported.
14 //
15 // Upstream commit c04c0d2b968a ("bpf: increase complexity limit and maximum program size").
16 //
17 // See the package documentation for the meaning of the error return value.
18 func HaveLargeInstructions() error {
19 return haveLargeInstructions()
20 }
21
22 var haveLargeInstructions = internal.NewFeatureTest(">4096 instructions", func() error {
23 const maxInsns = 4096
24
25 insns := make(asm.Instructions, maxInsns, maxInsns+1)
26 for i := range insns {
27 insns[i] = asm.Mov.Imm(asm.R0, 1)
28 }
29 insns = append(insns, asm.Return())
30
31 return probeProgram(&ebpf.ProgramSpec{
32 Type: ebpf.SocketFilter,
33 Instructions: insns,
34 })
35 }, "5.2")
36
37 // HaveBoundedLoops probes the running kernel if bounded loops are supported.
38 //
39 // Upstream commit 2589726d12a1 ("bpf: introduce bounded loops").
40 //
41 // See the package documentation for the meaning of the error return value.
42 func HaveBoundedLoops() error {
43 return haveBoundedLoops()
44 }
45
46 var haveBoundedLoops = internal.NewFeatureTest("bounded loops", func() error {
47 return probeProgram(&ebpf.ProgramSpec{
48 Type: ebpf.SocketFilter,
49 Instructions: asm.Instructions{
50 asm.Mov.Imm(asm.R0, 10),
51 asm.Sub.Imm(asm.R0, 1).WithSymbol("loop"),
52 asm.JNE.Imm(asm.R0, 0, "loop"),
53 asm.Return(),
54 },
55 })
56 }, "5.3")
57
58 // HaveV2ISA probes the running kernel if instructions of the v2 ISA are supported.
59 //
60 // Upstream commit 92b31a9af73b ("bpf: add BPF_J{LT,LE,SLT,SLE} instructions").
61 //
62 // See the package documentation for the meaning of the error return value.
63 func HaveV2ISA() error {
64 return haveV2ISA()
65 }
66
67 var haveV2ISA = internal.NewFeatureTest("v2 ISA", func() error {
68 err := probeProgram(&ebpf.ProgramSpec{
69 Type: ebpf.SocketFilter,
70 Instructions: asm.Instructions{
71 asm.Mov.Imm(asm.R0, 0),
72 asm.JLT.Imm(asm.R0, 0, "exit"),
73 asm.Mov.Imm(asm.R0, 1),
74 asm.Return().WithSymbol("exit"),
75 },
76 })
77 // This sometimes bubbles up from the JIT on aarch64.
78 if errors.Is(err, sys.ENOTSUPP) {
79 return ebpf.ErrNotSupported
80 }
81 return err
82 }, "4.14")
83
84 // HaveV3ISA probes the running kernel if instructions of the v3 ISA are supported.
85 //
86 // Upstream commit 092ed0968bb6 ("bpf: verifier support JMP32").
87 //
88 // See the package documentation for the meaning of the error return value.
89 func HaveV3ISA() error {
90 return haveV3ISA()
91 }
92
93 var haveV3ISA = internal.NewFeatureTest("v3 ISA", func() error {
94 err := probeProgram(&ebpf.ProgramSpec{
95 Type: ebpf.SocketFilter,
96 Instructions: asm.Instructions{
97 asm.Mov.Imm(asm.R0, 0),
98 asm.JLT.Imm32(asm.R0, 0, "exit"),
99 asm.Mov.Imm(asm.R0, 1),
100 asm.Return().WithSymbol("exit"),
101 },
102 })
103 // This sometimes bubbles up from the JIT on aarch64.
104 if errors.Is(err, sys.ENOTSUPP) {
105 return ebpf.ErrNotSupported
106 }
107 return err
108 }, "5.1")
109
110 // HaveV4ISA probes the running kernel if instructions of the v4 ISA are supported.
111 //
112 // Upstream commit 1f9a1ea821ff ("bpf: Support new sign-extension load insns").
113 //
114 // See the package documentation for the meaning of the error return value.
115 func HaveV4ISA() error {
116 return haveV4ISA()
117 }
118
119 var haveV4ISA = internal.NewFeatureTest("v4 ISA", func() error {
120 err := probeProgram(&ebpf.ProgramSpec{
121 Type: ebpf.SocketFilter,
122 Instructions: asm.Instructions{
123 asm.Mov.Imm(asm.R0, 0),
124 asm.JEq.Imm(asm.R0, 1, "error"),
125 asm.LongJump("exit"),
126 asm.Mov.Imm(asm.R0, 1).WithSymbol("error"),
127 asm.Return().WithSymbol("exit"),
128 },
129 })
130 // This sometimes bubbles up from the JIT on aarch64.
131 if errors.Is(err, sys.ENOTSUPP) {
132 return ebpf.ErrNotSupported
133 }
134 return err
135 }, "6.6")