Debian Codegraph @

link/syscalls.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "errors"
7
8 "github.com/cilium/ebpf"
9 "github.com/cilium/ebpf/asm"
10 "github.com/cilium/ebpf/internal"
11 "github.com/cilium/ebpf/internal/sys"
12 "github.com/cilium/ebpf/internal/unix"
13 )
14
15 var haveProgAttach = internal.NewFeatureTest("BPF_PROG_ATTACH", func() error {
16 prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
17 Type: ebpf.CGroupSKB,
18 License: "MIT",
19 Instructions: asm.Instructions{
20 asm.Mov.Imm(asm.R0, 0),
21 asm.Return(),
22 },
23 })
24 if err != nil {
25 return internal.ErrNotSupported
26 }
27
28 // BPF_PROG_ATTACH was introduced at the same time as CGgroupSKB,
29 // so being able to load the program is enough to infer that we
30 // have the syscall.
31 prog.Close()
32 return nil
33 }, "4.10")
34
35 var haveProgAttachReplace = internal.NewFeatureTest("BPF_PROG_ATTACH atomic replacement of MULTI progs", func() error {
36 if err := haveProgAttach(); err != nil {
37 return err
38 }
39
40 prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
41 Type: ebpf.CGroupSKB,
42 AttachType: ebpf.AttachCGroupInetIngress,
43 License: "MIT",
44 Instructions: asm.Instructions{
45 asm.Mov.Imm(asm.R0, 0),
46 asm.Return(),
47 },
48 })
49
50 if err != nil {
51 return internal.ErrNotSupported
52 }
53
54 defer prog.Close()
55
56 // We know that we have BPF_PROG_ATTACH since we can load CGroupSKB programs.
57 // If passing BPF_F_REPLACE gives us EINVAL we know that the feature isn't
58 // present.
59 attr := sys.ProgAttachAttr{
60 // We rely on this being checked after attachFlags.
61 TargetFdOrIfindex: ^uint32(0),
62 AttachBpfFd: uint32(prog.FD()),
63 AttachType: uint32(ebpf.AttachCGroupInetIngress),
64 AttachFlags: uint32(flagReplace),
65 }
66
67 err = sys.ProgAttach(&attr)
68 if errors.Is(err, unix.EINVAL) {
69 return internal.ErrNotSupported
70 }
71 if errors.Is(err, unix.EBADF) {
72 return nil
73 }
74 return err
75 }, "5.5")
76
77 var haveBPFLink = internal.NewFeatureTest("bpf_link", func() error {
78 attr := sys.LinkCreateAttr{
79 // This is a hopefully invalid file descriptor, which triggers EBADF.
80 TargetFd: ^uint32(0),
81 ProgFd: ^uint32(0),
82 AttachType: sys.AttachType(ebpf.AttachCGroupInetIngress),
83 }
84 _, err := sys.LinkCreate(&attr)
85 if errors.Is(err, unix.EINVAL) {
86 return internal.ErrNotSupported
87 }
88 if errors.Is(err, unix.EBADF) {
89 return nil
90 }
91 return err
92 }, "5.7")
93
94 var haveProgQuery = internal.NewFeatureTest("BPF_PROG_QUERY", func() error {
95 attr := sys.ProgQueryAttr{
96 // We rely on this being checked during the syscall.
97 // With an otherwise correct payload we expect EBADF here
98 // as an indication that the feature is present.
99 TargetFdOrIfindex: ^uint32(0),
100 AttachType: sys.AttachType(ebpf.AttachCGroupInetIngress),
101 }
102
103 err := sys.ProgQuery(&attr)
104
105 if errors.Is(err, unix.EBADF) {
106 return nil
107 }
108 if err != nil {
109 return ErrNotSupported
110 }
111 return errors.New("syscall succeeded unexpectedly")
112 }, "4.15")
113
114 var haveTCX = internal.NewFeatureTest("tcx", func() error {
115 prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
116 Type: ebpf.SchedCLS,
117 License: "MIT",
118 Instructions: asm.Instructions{
119 asm.Mov.Imm(asm.R0, 0),
120 asm.Return(),
121 },
122 })
123
124 if err != nil {
125 return internal.ErrNotSupported
126 }
127
128 defer prog.Close()
129 attr := sys.LinkCreateTcxAttr{
130 // We rely on this being checked during the syscall.
131 // With an otherwise correct payload we expect ENODEV here
132 // as an indication that the feature is present.
133 TargetIfindex: ^uint32(0),
134 ProgFd: uint32(prog.FD()),
135 AttachType: sys.AttachType(ebpf.AttachTCXIngress),
136 }
137
138 _, err = sys.LinkCreateTcx(&attr)
139
140 if errors.Is(err, unix.ENODEV) {
141 return nil
142 }
143 if err != nil {
144 return ErrNotSupported
145 }
146 return errors.New("syscall succeeded unexpectedly")
147 }, "6.6")
148
149 var haveNetkit = internal.NewFeatureTest("netkit", func() error {
150 prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
151 Type: ebpf.SchedCLS,
152 License: "MIT",
153 Instructions: asm.Instructions{
154 asm.Mov.Imm(asm.R0, 0),
155 asm.Return(),
156 },
157 })
158
159 if err != nil {
160 return internal.ErrNotSupported
161 }
162
163 defer prog.Close()
164 attr := sys.LinkCreateNetkitAttr{
165 // We rely on this being checked during the syscall.
166 // With an otherwise correct payload we expect ENODEV here
167 // as an indication that the feature is present.
168 TargetIfindex: ^uint32(0),
169 ProgFd: uint32(prog.FD()),
170 AttachType: sys.AttachType(ebpf.AttachNetkitPrimary),
171 }
172
173 _, err = sys.LinkCreateNetkit(&attr)
174
175 if errors.Is(err, unix.ENODEV) {
176 return nil
177 }
178 if err != nil {
179 return ErrNotSupported
180 }
181 return errors.New("syscall succeeded unexpectedly")
182 }, "6.7")