Debian Codegraph @

types.go

raw symbols SCIP
1 package ebpf
2
3 import (
4 "github.com/cilium/ebpf/internal/platform"
5 "github.com/cilium/ebpf/internal/sys"
6 )
7
8 //go:generate go tool stringer -output types_string.go -type=MapType,ProgramType,PinType
9
10 // MapType indicates the type map structure
11 // that will be initialized in the kernel.
12 type MapType uint32
13
14 // All the various map types that can be created
15 const (
16 UnspecifiedMap MapType = MapType(platform.LinuxTag | iota)
17 // Hash is a hash map
18 Hash
19 // Array is an array map
20 Array
21 // ProgramArray - A program array map is a special kind of array map whose map
22 // values contain only file descriptors referring to other eBPF
23 // programs. Thus, both the key_size and value_size must be
24 // exactly four bytes. This map is used in conjunction with the
25 // TailCall helper.
26 ProgramArray
27 // PerfEventArray - A perf event array is used in conjunction with PerfEventRead
28 // and PerfEventOutput calls, to read the raw bpf_perf_data from the registers.
29 PerfEventArray
30 // PerCPUHash - This data structure is useful for people who have high performance
31 // network needs and can reconcile adds at the end of some cycle, so that
32 // hashes can be lock free without the use of XAdd, which can be costly.
33 PerCPUHash
34 // PerCPUArray - This data structure is useful for people who have high performance
35 // network needs and can reconcile adds at the end of some cycle, so that
36 // hashes can be lock free without the use of XAdd, which can be costly.
37 // Each CPU gets a copy of this hash, the contents of all of which can be reconciled
38 // later.
39 PerCPUArray
40 // StackTrace - This holds whole user and kernel stack traces, it can be retrieved with
41 // GetStackID
42 StackTrace
43 // CGroupArray - This is a very niche structure used to help SKBInCGroup determine
44 // if an skb is from a socket belonging to a specific cgroup
45 CGroupArray
46 // LRUHash - This allows you to create a small hash structure that will purge the
47 // least recently used items rather than throw an error when you run out of memory
48 LRUHash
49 // LRUCPUHash - This is NOT like PerCPUHash, this structure is shared among the CPUs,
50 // it has more to do with including the CPU id with the LRU calculation so that if a
51 // particular CPU is using a value over-and-over again, then it will be saved, but if
52 // a value is being retrieved a lot but sparsely across CPUs it is not as important, basically
53 // giving weight to CPU locality over overall usage.
54 LRUCPUHash
55 // LPMTrie - This is an implementation of Longest-Prefix-Match Trie structure. It is useful,
56 // for storing things like IP addresses which can be bit masked allowing for keys of differing
57 // values to refer to the same reference based on their masks. See wikipedia for more details.
58 LPMTrie
59 // ArrayOfMaps - Each item in the array is another map. The inner map mustn't be a map of maps
60 // itself.
61 ArrayOfMaps
62 // HashOfMaps - Each item in the hash map is another map. The inner map mustn't be a map of maps
63 // itself.
64 HashOfMaps
65 // DevMap - Specialized map to store references to network devices.
66 DevMap
67 // SockMap - Specialized map to store references to sockets.
68 SockMap
69 // CPUMap - Specialized map to store references to CPUs.
70 CPUMap
71 // XSKMap - Specialized map for XDP programs to store references to open sockets.
72 XSKMap
73 // SockHash - Specialized hash to store references to sockets.
74 SockHash
75 // CGroupStorage - Special map for CGroups.
76 CGroupStorage
77 // ReusePortSockArray - Specialized map to store references to sockets that can be reused.
78 ReusePortSockArray
79 // PerCPUCGroupStorage - Special per CPU map for CGroups.
80 PerCPUCGroupStorage
81 // Queue - FIFO storage for BPF programs.
82 Queue
83 // Stack - LIFO storage for BPF programs.
84 Stack
85 // SkStorage - Specialized map for local storage at SK for BPF programs.
86 SkStorage
87 // DevMapHash - Hash-based indexing scheme for references to network devices.
88 DevMapHash
89 // StructOpsMap - This map holds a kernel struct with its function pointer implemented in a BPF
90 // program.
91 StructOpsMap
92 // RingBuf - Similar to PerfEventArray, but shared across all CPUs.
93 RingBuf
94 // InodeStorage - Specialized local storage map for inodes.
95 InodeStorage
96 // TaskStorage - Specialized local storage map for task_struct.
97 TaskStorage
98 // BloomFilter - Space-efficient data structure to quickly test whether an element exists in a set.
99 BloomFilter
100 // UserRingbuf - The reverse of RingBuf, used to send messages from user space to BPF programs.
101 UserRingbuf
102 // CgroupStorage - Store data keyed on a cgroup. If the cgroup disappears, the key is automatically removed.
103 CgroupStorage
104 // Arena - Sparse shared memory region between a BPF program and user space.
105 Arena
106 )
107
108 // Map types (Windows).
109 const (
110 WindowsHash MapType = MapType(platform.WindowsTag | iota + 1)
111 WindowsArray
112 WindowsProgramArray
113 WindowsPerCPUHash
114 WindowsPerCPUArray
115 WindowsHashOfMaps
116 WindowsArrayOfMaps
117 WindowsLRUHash
118 WindowsLPMTrie
119 WindowsQueue
120 WindowsLRUCPUHash
121 WindowsStack
122 WindowsRingBuf
123 )
124
125 // MapTypeForPlatform returns a platform specific map type.
126 //
127 // Use this if the library doesn't provide a constant yet.
128 func MapTypeForPlatform(plat string, typ uint32) (MapType, error) {
129 return platform.EncodeConstant[MapType](plat, typ)
130 }
131
132 // hasPerCPUValue returns true if the Map stores a value per CPU.
133 func (mt MapType) hasPerCPUValue() bool {
134 switch mt {
135 case PerCPUHash, PerCPUArray, LRUCPUHash, PerCPUCGroupStorage:
136 return true
137 case WindowsPerCPUHash, WindowsPerCPUArray, WindowsLRUCPUHash:
138 return true
139 default:
140 return false
141 }
142 }
143
144 // canStoreMapOrProgram returns true if the Map stores references to another Map
145 // or Program.
146 func (mt MapType) canStoreMapOrProgram() bool {
147 return mt.canStoreMap() || mt.canStoreProgram() || mt == StructOpsMap
148 }
149
150 // canStoreMap returns true if the map type accepts a map fd
151 // for update and returns a map id for lookup.
152 func (mt MapType) canStoreMap() bool {
153 return mt == ArrayOfMaps || mt == HashOfMaps || mt == WindowsArrayOfMaps || mt == WindowsHashOfMaps
154 }
155
156 // canStoreProgram returns true if the map type accepts a program fd
157 // for update and returns a program id for lookup.
158 func (mt MapType) canStoreProgram() bool {
159 return mt == ProgramArray || mt == WindowsProgramArray
160 }
161
162 // canHaveValueSize returns true if the map type supports setting a value size.
163 func (mt MapType) canHaveValueSize() bool {
164 switch mt {
165 case RingBuf, Arena:
166 return false
167
168 // Special-case perf events since they require a value size of either 0 or 4
169 // for historical reasons. Let the library fix this up later.
170 case PerfEventArray:
171 return false
172 }
173
174 return true
175 }
176
177 // mustHaveNoPrealloc returns true if the map type does not support
178 // preallocation and needs the BPF_F_NO_PREALLOC flag set to be created
179 // successfully.
180 func (mt MapType) mustHaveNoPrealloc() bool {
181 switch mt {
182 case CgroupStorage, InodeStorage, TaskStorage, SkStorage:
183 return true
184 case LPMTrie:
185 return true
186 }
187
188 return false
189 }
190
191 // mustHaveZeroMaxEntries returns true if the map type requires MaxEntries to be zero.
192 func (mt MapType) mustHaveZeroMaxEntries() bool {
193 switch mt {
194 case CgroupStorage, CGroupStorage, PerCPUCGroupStorage, InodeStorage, TaskStorage, SkStorage:
195 return true
196 }
197
198 return false
199 }
200
201 // ProgramType of the eBPF program
202 type ProgramType uint32
203
204 // eBPF program types (Linux).
205 const (
206 UnspecifiedProgram = ProgramType(sys.BPF_PROG_TYPE_UNSPEC)
207 SocketFilter = ProgramType(sys.BPF_PROG_TYPE_SOCKET_FILTER)
208 Kprobe = ProgramType(sys.BPF_PROG_TYPE_KPROBE)
209 SchedCLS = ProgramType(sys.BPF_PROG_TYPE_SCHED_CLS)
210 SchedACT = ProgramType(sys.BPF_PROG_TYPE_SCHED_ACT)
211 TracePoint = ProgramType(sys.BPF_PROG_TYPE_TRACEPOINT)
212 XDP = ProgramType(sys.BPF_PROG_TYPE_XDP)
213 PerfEvent = ProgramType(sys.BPF_PROG_TYPE_PERF_EVENT)
214 CGroupSKB = ProgramType(sys.BPF_PROG_TYPE_CGROUP_SKB)
215 CGroupSock = ProgramType(sys.BPF_PROG_TYPE_CGROUP_SOCK)
216 LWTIn = ProgramType(sys.BPF_PROG_TYPE_LWT_IN)
217 LWTOut = ProgramType(sys.BPF_PROG_TYPE_LWT_OUT)
218 LWTXmit = ProgramType(sys.BPF_PROG_TYPE_LWT_XMIT)
219 SockOps = ProgramType(sys.BPF_PROG_TYPE_SOCK_OPS)
220 SkSKB = ProgramType(sys.BPF_PROG_TYPE_SK_SKB)
221 CGroupDevice = ProgramType(sys.BPF_PROG_TYPE_CGROUP_DEVICE)
222 SkMsg = ProgramType(sys.BPF_PROG_TYPE_SK_MSG)
223 RawTracepoint = ProgramType(sys.BPF_PROG_TYPE_RAW_TRACEPOINT)
224 CGroupSockAddr = ProgramType(sys.BPF_PROG_TYPE_CGROUP_SOCK_ADDR)
225 LWTSeg6Local = ProgramType(sys.BPF_PROG_TYPE_LWT_SEG6LOCAL)
226 LircMode2 = ProgramType(sys.BPF_PROG_TYPE_LIRC_MODE2)
227 SkReuseport = ProgramType(sys.BPF_PROG_TYPE_SK_REUSEPORT)
228 FlowDissector = ProgramType(sys.BPF_PROG_TYPE_FLOW_DISSECTOR)
229 CGroupSysctl = ProgramType(sys.BPF_PROG_TYPE_CGROUP_SYSCTL)
230 RawTracepointWritable = ProgramType(sys.BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE)
231 CGroupSockopt = ProgramType(sys.BPF_PROG_TYPE_CGROUP_SOCKOPT)
232 Tracing = ProgramType(sys.BPF_PROG_TYPE_TRACING)
233 StructOps = ProgramType(sys.BPF_PROG_TYPE_STRUCT_OPS)
234 Extension = ProgramType(sys.BPF_PROG_TYPE_EXT)
235 LSM = ProgramType(sys.BPF_PROG_TYPE_LSM)
236 SkLookup = ProgramType(sys.BPF_PROG_TYPE_SK_LOOKUP)
237 Syscall = ProgramType(sys.BPF_PROG_TYPE_SYSCALL)
238 Netfilter = ProgramType(sys.BPF_PROG_TYPE_NETFILTER)
239 )
240
241 // eBPF program types (Windows).
242 //
243 // See https://github.com/microsoft/ebpf-for-windows/blob/main/include/ebpf_structs.h#L170
244 const (
245 WindowsXDP ProgramType = ProgramType(platform.WindowsTag) | (iota + 1)
246 WindowsBind
247 WindowsCGroupSockAddr
248 WindowsSockOps
249 WindowsXDPTest ProgramType = ProgramType(platform.WindowsTag) | 998
250 WindowsSample ProgramType = ProgramType(platform.WindowsTag) | 999
251 )
252
253 // ProgramTypeForPlatform returns a platform specific program type.
254 //
255 // Use this if the library doesn't provide a constant yet.
256 func ProgramTypeForPlatform(plat string, value uint32) (ProgramType, error) {
257 return platform.EncodeConstant[ProgramType](plat, value)
258 }
259
260 // AttachType of the eBPF program, needed to differentiate allowed context accesses in
261 // some newer program types like CGroupSockAddr. Should be set to AttachNone if not required.
262 // Will cause invalid argument (EINVAL) at program load time if set incorrectly.
263 type AttachType uint32
264
265 //go:generate go tool stringer -type AttachType -trimprefix Attach
266
267 // AttachNone is an alias for AttachCGroupInetIngress for readability reasons.
268 const AttachNone AttachType = 0
269
270 // Attach types (Linux).
271 const (
272 AttachCGroupInetIngress = AttachType(sys.BPF_CGROUP_INET_INGRESS)
273 AttachCGroupInetEgress = AttachType(sys.BPF_CGROUP_INET_EGRESS)
274 AttachCGroupInetSockCreate = AttachType(sys.BPF_CGROUP_INET_SOCK_CREATE)
275 AttachCGroupSockOps = AttachType(sys.BPF_CGROUP_SOCK_OPS)
276 AttachSkSKBStreamParser = AttachType(sys.BPF_SK_SKB_STREAM_PARSER)
277 AttachSkSKBStreamVerdict = AttachType(sys.BPF_SK_SKB_STREAM_VERDICT)
278 AttachCGroupDevice = AttachType(sys.BPF_CGROUP_DEVICE)
279 AttachSkMsgVerdict = AttachType(sys.BPF_SK_MSG_VERDICT)
280 AttachCGroupInet4Bind = AttachType(sys.BPF_CGROUP_INET4_BIND)
281 AttachCGroupInet6Bind = AttachType(sys.BPF_CGROUP_INET6_BIND)
282 AttachCGroupInet4Connect = AttachType(sys.BPF_CGROUP_INET4_CONNECT)
283 AttachCGroupInet6Connect = AttachType(sys.BPF_CGROUP_INET6_CONNECT)
284 AttachCGroupInet4PostBind = AttachType(sys.BPF_CGROUP_INET4_POST_BIND)
285 AttachCGroupInet6PostBind = AttachType(sys.BPF_CGROUP_INET6_POST_BIND)
286 AttachCGroupUDP4Sendmsg = AttachType(sys.BPF_CGROUP_UDP4_SENDMSG)
287 AttachCGroupUDP6Sendmsg = AttachType(sys.BPF_CGROUP_UDP6_SENDMSG)
288 AttachLircMode2 = AttachType(sys.BPF_LIRC_MODE2)
289 AttachFlowDissector = AttachType(sys.BPF_FLOW_DISSECTOR)
290 AttachCGroupSysctl = AttachType(sys.BPF_CGROUP_SYSCTL)
291 AttachCGroupUDP4Recvmsg = AttachType(sys.BPF_CGROUP_UDP4_RECVMSG)
292 AttachCGroupUDP6Recvmsg = AttachType(sys.BPF_CGROUP_UDP6_RECVMSG)
293 AttachCGroupGetsockopt = AttachType(sys.BPF_CGROUP_GETSOCKOPT)
294 AttachCGroupSetsockopt = AttachType(sys.BPF_CGROUP_SETSOCKOPT)
295 AttachTraceRawTp = AttachType(sys.BPF_TRACE_RAW_TP)
296 AttachTraceFEntry = AttachType(sys.BPF_TRACE_FENTRY)
297 AttachTraceFExit = AttachType(sys.BPF_TRACE_FEXIT)
298 AttachModifyReturn = AttachType(sys.BPF_MODIFY_RETURN)
299 AttachLSMMac = AttachType(sys.BPF_LSM_MAC)
300 AttachTraceIter = AttachType(sys.BPF_TRACE_ITER)
301 AttachCgroupInet4GetPeername = AttachType(sys.BPF_CGROUP_INET4_GETPEERNAME)
302 AttachCgroupInet6GetPeername = AttachType(sys.BPF_CGROUP_INET6_GETPEERNAME)
303 AttachCgroupInet4GetSockname = AttachType(sys.BPF_CGROUP_INET4_GETSOCKNAME)
304 AttachCgroupInet6GetSockname = AttachType(sys.BPF_CGROUP_INET6_GETSOCKNAME)
305 AttachXDPDevMap = AttachType(sys.BPF_XDP_DEVMAP)
306 AttachCgroupInetSockRelease = AttachType(sys.BPF_CGROUP_INET_SOCK_RELEASE)
307 AttachXDPCPUMap = AttachType(sys.BPF_XDP_CPUMAP)
308 AttachSkLookup = AttachType(sys.BPF_SK_LOOKUP)
309 AttachXDP = AttachType(sys.BPF_XDP)
310 AttachSkSKBVerdict = AttachType(sys.BPF_SK_SKB_VERDICT)
311 AttachSkReuseportSelect = AttachType(sys.BPF_SK_REUSEPORT_SELECT)
312 AttachSkReuseportSelectOrMigrate = AttachType(sys.BPF_SK_REUSEPORT_SELECT_OR_MIGRATE)
313 AttachPerfEvent = AttachType(sys.BPF_PERF_EVENT)
314 AttachTraceKprobeMulti = AttachType(sys.BPF_TRACE_KPROBE_MULTI)
315 AttachTraceKprobeSession = AttachType(sys.BPF_TRACE_KPROBE_SESSION)
316 AttachLSMCgroup = AttachType(sys.BPF_LSM_CGROUP)
317 AttachStructOps = AttachType(sys.BPF_STRUCT_OPS)
318 AttachNetfilter = AttachType(sys.BPF_NETFILTER)
319 AttachTCXIngress = AttachType(sys.BPF_TCX_INGRESS)
320 AttachTCXEgress = AttachType(sys.BPF_TCX_EGRESS)
321 AttachTraceUprobeMulti = AttachType(sys.BPF_TRACE_UPROBE_MULTI)
322 AttachCgroupUnixConnect = AttachType(sys.BPF_CGROUP_UNIX_CONNECT)
323 AttachCgroupUnixSendmsg = AttachType(sys.BPF_CGROUP_UNIX_SENDMSG)
324 AttachCgroupUnixRecvmsg = AttachType(sys.BPF_CGROUP_UNIX_RECVMSG)
325 AttachCgroupUnixGetpeername = AttachType(sys.BPF_CGROUP_UNIX_GETPEERNAME)
326 AttachCgroupUnixGetsockname = AttachType(sys.BPF_CGROUP_UNIX_GETSOCKNAME)
327 AttachNetkitPrimary = AttachType(sys.BPF_NETKIT_PRIMARY)
328 AttachNetkitPeer = AttachType(sys.BPF_NETKIT_PEER)
329 )
330
331 // Attach types (Windows).
332 //
333 // See https://github.com/microsoft/ebpf-for-windows/blob/main/include/ebpf_structs.h#L260
334 const (
335 AttachWindowsXDP = AttachType(platform.WindowsTag | iota + 1)
336 AttachWindowsBind
337 AttachWindowsCGroupInet4Connect
338 AttachWindowsCGroupInet6Connect
339 AttachWindowsCgroupInet4RecvAccept
340 AttachWindowsCgroupInet6RecvAccept
341 AttachWindowsCGroupSockOps
342 AttachWindowsSample
343 AttachWindowsXDPTest
344 )
345
346 // AttachTypeForPlatform returns a platform specific attach type.
347 //
348 // Use this if the library doesn't provide a constant yet.
349 func AttachTypeForPlatform(plat string, value uint32) (AttachType, error) {
350 return platform.EncodeConstant[AttachType](plat, value)
351 }
352
353 // AttachFlags of the eBPF program used in BPF_PROG_ATTACH command
354 type AttachFlags uint32
355
356 // PinType determines whether a map is pinned into a BPFFS.
357 type PinType uint32
358
359 // Valid pin types.
360 //
361 // Mirrors enum libbpf_pin_type.
362 const (
363 PinNone PinType = iota
364 // Pin an object by using its name as the filename.
365 PinByName
366 )
367
368 // LoadPinOptions control how a pinned object is loaded.
369 type LoadPinOptions struct {
370 // Request a read-only or write-only object. The default is a read-write
371 // object. Only one of the flags may be set.
372 ReadOnly bool
373 WriteOnly bool
374
375 // Raw flags for the syscall. Other fields of this struct take precedence.
376 Flags uint32
377 }
378
379 // Marshal returns a value suitable for BPF_OBJ_GET syscall file_flags parameter.
380 func (lpo *LoadPinOptions) Marshal() uint32 {
381 if lpo == nil {
382 return 0
383 }
384
385 flags := lpo.Flags
386 if lpo.ReadOnly {
387 flags |= sys.BPF_F_RDONLY
388 }
389 if lpo.WriteOnly {
390 flags |= sys.BPF_F_WRONLY
391 }
392 return flags
393 }
394
395 // BatchOptions batch map operations options
396 //
397 // Mirrors libbpf struct bpf_map_batch_opts
398 // Currently BPF_F_FLAG is the only supported
399 // flag (for ElemFlags).
400 type BatchOptions struct {
401 ElemFlags uint64
402 Flags uint64
403 }
404
405 // LogLevel controls the verbosity of the kernel's eBPF program verifier.
406 // These constants can be used for the ProgramOptions.LogLevel field.
407 type LogLevel = sys.LogLevel
408
409 const (
410 // Print verifier state at branch points.
411 LogLevelBranch = sys.BPF_LOG_LEVEL1
412
413 // Print verifier state for every instruction.
414 // Available since Linux v5.2.
415 LogLevelInstruction = sys.BPF_LOG_LEVEL2
416
417 // Print verifier errors and stats at the end of the verification process.
418 // Available since Linux v5.2.
419 LogLevelStats = sys.BPF_LOG_STATS
420 )