1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 #ifndef _UAPI__LINUX_BPF_H__
8 #define _UAPI__LINUX_BPF_H__
10 #include <linux/types.h>
11 #include <linux/bpf_common.h>
13 /* Extended instruction set based on top of classic BPF */
15 /* instruction classes */
16 #define BPF_ALU64 0x07 /* alu mode in double word width */
19 #define BPF_DW 0x18 /* double word */
20 #define BPF_XADD 0xc0 /* exclusive add */
23 #define BPF_MOV 0xb0 /* mov reg to reg */
24 #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
26 /* change endianness of a register */
27 #define BPF_END 0xd0 /* flags for endianness conversion: */
28 #define BPF_TO_LE 0x00 /* convert to little-endian */
29 #define BPF_TO_BE 0x08 /* convert to big-endian */
30 #define BPF_FROM_LE BPF_TO_LE
31 #define BPF_FROM_BE BPF_TO_BE
33 #define BPF_JNE 0x50 /* jump != */
34 #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */
35 #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */
36 #define BPF_CALL 0x80 /* function call */
37 #define BPF_EXIT 0x90 /* function return */
39 /* Register numbers */
55 /* BPF has 10 general purpose 64-bit registers and stack frame. */
56 #define MAX_BPF_REG __MAX_BPF_REG
59 __u8 code; /* opcode */
60 __u8 dst_reg:4; /* dest register */
61 __u8 src_reg:4; /* source register */
62 __s16 off; /* signed offset */
63 __s32 imm; /* signed immediate constant */
66 /* BPF syscall commands, see bpf(2) man-page for details. */
82 BPF_MAP_TYPE_PROG_ARRAY,
83 BPF_MAP_TYPE_PERF_EVENT_ARRAY,
84 BPF_MAP_TYPE_PERCPU_HASH,
85 BPF_MAP_TYPE_PERCPU_ARRAY,
86 BPF_MAP_TYPE_STACK_TRACE,
87 BPF_MAP_TYPE_CGROUP_ARRAY,
88 BPF_MAP_TYPE_LRU_HASH,
89 BPF_MAP_TYPE_LRU_PERCPU_HASH,
94 BPF_PROG_TYPE_SOCKET_FILTER,
96 BPF_PROG_TYPE_SCHED_CLS,
97 BPF_PROG_TYPE_SCHED_ACT,
98 BPF_PROG_TYPE_TRACEPOINT,
100 BPF_PROG_TYPE_PERF_EVENT,
103 #define BPF_PSEUDO_MAP_FD 1
105 /* flags for BPF_MAP_UPDATE_ELEM command */
106 #define BPF_ANY 0 /* create new element or update existing */
107 #define BPF_NOEXIST 1 /* create new element if it didn't exist */
108 #define BPF_EXIST 2 /* update existing element */
110 #define BPF_F_NO_PREALLOC (1U << 0)
111 /* Instead of having one common LRU list in the
112 * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
113 * which can scale and perform better.
114 * Note, the LRU nodes (including free nodes) cannot be moved
115 * across different LRU lists.
117 #define BPF_F_NO_COMMON_LRU (1U << 1)
120 struct { /* anonymous struct used by BPF_MAP_CREATE command */
121 __u32 map_type; /* one of enum bpf_map_type */
122 __u32 key_size; /* size of key in bytes */
123 __u32 value_size; /* size of value in bytes */
124 __u32 max_entries; /* max number of entries in a map */
125 __u32 map_flags; /* prealloc or not */
128 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
133 __aligned_u64 next_key;
138 struct { /* anonymous struct used by BPF_PROG_LOAD command */
139 __u32 prog_type; /* one of enum bpf_prog_type */
142 __aligned_u64 license;
143 __u32 log_level; /* verbosity level of verifier */
144 __u32 log_size; /* size of user buffer */
145 __aligned_u64 log_buf; /* user supplied buffer */
146 __u32 kern_version; /* checked when prog_type=kprobe */
149 struct { /* anonymous struct used by BPF_OBJ_* commands */
150 __aligned_u64 pathname;
153 } __attribute__((aligned(8)));
155 /* BPF helper function descriptions:
157 * void *bpf_map_lookup_elem(&map, &key)
158 * Return: Map value or NULL
160 * int bpf_map_update_elem(&map, &key, &value, flags)
161 * Return: 0 on success or negative error
163 * int bpf_map_delete_elem(&map, &key)
164 * Return: 0 on success or negative error
166 * int bpf_probe_read(void *dst, int size, void *src)
167 * Return: 0 on success or negative error
169 * u64 bpf_ktime_get_ns(void)
170 * Return: current ktime
172 * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
173 * Return: length of buffer written or negative error
175 * u32 bpf_prandom_u32(void)
176 * Return: random value
178 * u32 bpf_raw_smp_processor_id(void)
179 * Return: SMP processor ID
181 * int bpf_skb_store_bytes(skb, offset, from, len, flags)
182 * store bytes into packet
183 * @skb: pointer to skb
184 * @offset: offset within packet from skb->mac_header
185 * @from: pointer where to copy bytes from
186 * @len: number of bytes to store into packet
187 * @flags: bit 0 - if true, recompute skb->csum
188 * other bits - reserved
189 * Return: 0 on success or negative error
191 * int bpf_l3_csum_replace(skb, offset, from, to, flags)
192 * recompute IP checksum
193 * @skb: pointer to skb
194 * @offset: offset within packet where IP checksum is located
195 * @from: old value of header field
196 * @to: new value of header field
197 * @flags: bits 0-3 - size of header field
198 * other bits - reserved
199 * Return: 0 on success or negative error
201 * int bpf_l4_csum_replace(skb, offset, from, to, flags)
202 * recompute TCP/UDP checksum
203 * @skb: pointer to skb
204 * @offset: offset within packet where TCP/UDP checksum is located
205 * @from: old value of header field
206 * @to: new value of header field
207 * @flags: bits 0-3 - size of header field
208 * bit 4 - is pseudo header
209 * other bits - reserved
210 * Return: 0 on success or negative error
212 * int bpf_tail_call(ctx, prog_array_map, index)
213 * jump into another BPF program
214 * @ctx: context pointer passed to next program
215 * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
216 * @index: index inside array that selects specific program to run
217 * Return: 0 on success or negative error
219 * int bpf_clone_redirect(skb, ifindex, flags)
220 * redirect to another netdev
221 * @skb: pointer to skb
222 * @ifindex: ifindex of the net device
223 * @flags: bit 0 - if set, redirect to ingress instead of egress
224 * other bits - reserved
225 * Return: 0 on success or negative error
227 * u64 bpf_get_current_pid_tgid(void)
228 * Return: current->tgid << 32 | current->pid
230 * u64 bpf_get_current_uid_gid(void)
231 * Return: current_gid << 32 | current_uid
233 * int bpf_get_current_comm(char *buf, int size_of_buf)
234 * stores current->comm into buf
235 * Return: 0 on success or negative error
237 * u32 bpf_get_cgroup_classid(skb)
238 * retrieve a proc's classid
239 * @skb: pointer to skb
240 * Return: classid if != 0
242 * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
243 * Return: 0 on success or negative error
245 * int bpf_skb_vlan_pop(skb)
246 * Return: 0 on success or negative error
248 * int bpf_skb_get_tunnel_key(skb, key, size, flags)
249 * int bpf_skb_set_tunnel_key(skb, key, size, flags)
250 * retrieve or populate tunnel metadata
251 * @skb: pointer to skb
252 * @key: pointer to 'struct bpf_tunnel_key'
253 * @size: size of 'struct bpf_tunnel_key'
254 * @flags: room for future extensions
255 * Return: 0 on success or negative error
257 * u64 bpf_perf_event_read(&map, index)
258 * Return: Number events read or error code
260 * int bpf_redirect(ifindex, flags)
261 * redirect to another netdev
262 * @ifindex: ifindex of the net device
263 * @flags: bit 0 - if set, redirect to ingress instead of egress
264 * other bits - reserved
265 * Return: TC_ACT_REDIRECT
267 * u32 bpf_get_route_realm(skb)
268 * retrieve a dst's tclassid
269 * @skb: pointer to skb
270 * Return: realm if != 0
272 * int bpf_perf_event_output(ctx, map, index, data, size)
273 * output perf raw sample
274 * @ctx: struct pt_regs*
275 * @map: pointer to perf_event_array map
276 * @index: index of event in the map
277 * @data: data on stack to be output as raw data
278 * @size: size of data
279 * Return: 0 on success or negative error
281 * int bpf_get_stackid(ctx, map, flags)
282 * walk user or kernel stack and return id
283 * @ctx: struct pt_regs*
284 * @map: pointer to stack_trace map
285 * @flags: bits 0-7 - numer of stack frames to skip
286 * bit 8 - collect user stack instead of kernel
287 * bit 9 - compare stacks by hash only
288 * bit 10 - if two different stacks hash into the same stackid
290 * other bits - reserved
291 * Return: >= 0 stackid on success or negative error
293 * s64 bpf_csum_diff(from, from_size, to, to_size, seed)
294 * calculate csum diff
295 * @from: raw from buffer
296 * @from_size: length of from buffer
298 * @to_size: length of to buffer
299 * @seed: optional seed
300 * Return: csum result or negative error code
302 * int bpf_skb_get_tunnel_opt(skb, opt, size)
303 * retrieve tunnel options metadata
304 * @skb: pointer to skb
305 * @opt: pointer to raw tunnel option data
306 * @size: size of @opt
307 * Return: option size
309 * int bpf_skb_set_tunnel_opt(skb, opt, size)
310 * populate tunnel options metadata
311 * @skb: pointer to skb
312 * @opt: pointer to raw tunnel option data
313 * @size: size of @opt
314 * Return: 0 on success or negative error
316 * int bpf_skb_change_proto(skb, proto, flags)
317 * Change protocol of the skb. Currently supported is v4 -> v6,
318 * v6 -> v4 transitions. The helper will also resize the skb. eBPF
319 * program is expected to fill the new headers via skb_store_bytes
320 * and lX_csum_replace.
321 * @skb: pointer to skb
322 * @proto: new skb->protocol type
324 * Return: 0 on success or negative error
326 * int bpf_skb_change_type(skb, type)
327 * Change packet type of skb.
328 * @skb: pointer to skb
329 * @type: new skb->pkt_type type
330 * Return: 0 on success or negative error
332 * int bpf_skb_under_cgroup(skb, map, index)
333 * Check cgroup2 membership of skb
334 * @skb: pointer to skb
335 * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
336 * @index: index of the cgroup in the bpf_map
338 * == 0 skb failed the cgroup2 descendant test
339 * == 1 skb succeeded the cgroup2 descendant test
342 * u32 bpf_get_hash_recalc(skb)
343 * Retrieve and possibly recalculate skb->hash.
344 * @skb: pointer to skb
347 * u64 bpf_get_current_task(void)
348 * Returns current task_struct
351 * int bpf_probe_write_user(void *dst, void *src, int len)
352 * safely attempt to write to a location
353 * @dst: destination address in userspace
354 * @src: source address on stack
355 * @len: number of bytes to copy
356 * Return: 0 on success or negative error
358 * int bpf_current_task_under_cgroup(map, index)
359 * Check cgroup2 membership of current task
360 * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
361 * @index: index of the cgroup in the bpf_map
363 * == 0 current failed the cgroup2 descendant test
364 * == 1 current succeeded the cgroup2 descendant test
367 * int bpf_skb_change_tail(skb, len, flags)
368 * The helper will resize the skb to the given new size, to be used f.e.
369 * with control messages.
370 * @skb: pointer to skb
371 * @len: new skb length
373 * Return: 0 on success or negative error
375 * int bpf_skb_pull_data(skb, len)
376 * The helper will pull in non-linear data in case the skb is non-linear
377 * and not all of len are part of the linear section. Only needed for
378 * read/write with direct packet access.
379 * @skb: pointer to skb
380 * @len: len to make read/writeable
381 * Return: 0 on success or negative error
383 * s64 bpf_csum_update(skb, csum)
384 * Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
385 * @skb: pointer to skb
387 * Return: csum on success or negative error
389 * void bpf_set_hash_invalid(skb)
390 * Invalidate current skb->hash.
391 * @skb: pointer to skb
393 * int bpf_get_numa_node_id()
394 * Return: Id of current NUMA node.
396 #define __BPF_FUNC_MAPPER(FN) \
398 FN(map_lookup_elem), \
399 FN(map_update_elem), \
400 FN(map_delete_elem), \
404 FN(get_prandom_u32), \
405 FN(get_smp_processor_id), \
406 FN(skb_store_bytes), \
407 FN(l3_csum_replace), \
408 FN(l4_csum_replace), \
410 FN(clone_redirect), \
411 FN(get_current_pid_tgid), \
412 FN(get_current_uid_gid), \
413 FN(get_current_comm), \
414 FN(get_cgroup_classid), \
417 FN(skb_get_tunnel_key), \
418 FN(skb_set_tunnel_key), \
419 FN(perf_event_read), \
421 FN(get_route_realm), \
422 FN(perf_event_output), \
423 FN(skb_load_bytes), \
426 FN(skb_get_tunnel_opt), \
427 FN(skb_set_tunnel_opt), \
428 FN(skb_change_proto), \
429 FN(skb_change_type), \
430 FN(skb_under_cgroup), \
431 FN(get_hash_recalc), \
432 FN(get_current_task), \
433 FN(probe_write_user), \
434 FN(current_task_under_cgroup), \
435 FN(skb_change_tail), \
438 FN(set_hash_invalid), \
439 FN(get_numa_node_id),
441 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
442 * function eBPF program intends to call
444 #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
446 __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
451 /* All flags used by eBPF helper functions, placed here. */
453 /* BPF_FUNC_skb_store_bytes flags. */
454 #define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
455 #define BPF_F_INVALIDATE_HASH (1ULL << 1)
457 /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
458 * First 4 bits are for passing the header field size.
460 #define BPF_F_HDR_FIELD_MASK 0xfULL
462 /* BPF_FUNC_l4_csum_replace flags. */
463 #define BPF_F_PSEUDO_HDR (1ULL << 4)
464 #define BPF_F_MARK_MANGLED_0 (1ULL << 5)
466 /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
467 #define BPF_F_INGRESS (1ULL << 0)
469 /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
470 #define BPF_F_TUNINFO_IPV6 (1ULL << 0)
472 /* BPF_FUNC_get_stackid flags. */
473 #define BPF_F_SKIP_FIELD_MASK 0xffULL
474 #define BPF_F_USER_STACK (1ULL << 8)
475 #define BPF_F_FAST_STACK_CMP (1ULL << 9)
476 #define BPF_F_REUSE_STACKID (1ULL << 10)
478 /* BPF_FUNC_skb_set_tunnel_key flags. */
479 #define BPF_F_ZERO_CSUM_TX (1ULL << 1)
480 #define BPF_F_DONT_FRAGMENT (1ULL << 2)
482 /* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */
483 #define BPF_F_INDEX_MASK 0xffffffffULL
484 #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
485 /* BPF_FUNC_perf_event_output for sk_buff input context. */
486 #define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
488 /* user accessible mirror of in-kernel sk_buff.
489 * new fields can only be added to the end of this structure
501 __u32 ingress_ifindex;
511 struct bpf_tunnel_key {
515 __u32 remote_ipv6[4];
523 /* User return codes for XDP prog type.
524 * A valid XDP program must return one of these defined values. All other
525 * return codes are reserved for future use. Unknown return codes will result
535 /* user accessible metadata for XDP packet hook
536 * new fields must be added to the end of this structure
543 #endif /* _UAPI__LINUX_BPF_H__ */