]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/uapi/linux/bpf.h
cgroup: add support for eBPF programs
[mirror_ubuntu-artful-kernel.git] / include / uapi / linux / bpf.h
CommitLineData
daedfb22
AS
1/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
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.
6 */
7#ifndef _UAPI__LINUX_BPF_H__
8#define _UAPI__LINUX_BPF_H__
9
10#include <linux/types.h>
c15952dc 11#include <linux/bpf_common.h>
daedfb22
AS
12
13/* Extended instruction set based on top of classic BPF */
14
15/* instruction classes */
16#define BPF_ALU64 0x07 /* alu mode in double word width */
17
18/* ld/ldx fields */
19#define BPF_DW 0x18 /* double word */
20#define BPF_XADD 0xc0 /* exclusive add */
21
22/* alu/jmp fields */
23#define BPF_MOV 0xb0 /* mov reg to reg */
24#define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
25
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
32
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 */
38
39/* Register numbers */
40enum {
41 BPF_REG_0 = 0,
42 BPF_REG_1,
43 BPF_REG_2,
44 BPF_REG_3,
45 BPF_REG_4,
46 BPF_REG_5,
47 BPF_REG_6,
48 BPF_REG_7,
49 BPF_REG_8,
50 BPF_REG_9,
51 BPF_REG_10,
52 __MAX_BPF_REG,
53};
54
55/* BPF has 10 general purpose 64-bit registers and stack frame. */
56#define MAX_BPF_REG __MAX_BPF_REG
57
58struct bpf_insn {
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 */
64};
65
b2197755 66/* BPF syscall commands, see bpf(2) man-page for details. */
99c55f7d 67enum bpf_cmd {
99c55f7d 68 BPF_MAP_CREATE,
db20fd2b 69 BPF_MAP_LOOKUP_ELEM,
db20fd2b 70 BPF_MAP_UPDATE_ELEM,
db20fd2b 71 BPF_MAP_DELETE_ELEM,
db20fd2b 72 BPF_MAP_GET_NEXT_KEY,
09756af4 73 BPF_PROG_LOAD,
b2197755
DB
74 BPF_OBJ_PIN,
75 BPF_OBJ_GET,
99c55f7d
AS
76};
77
78enum bpf_map_type {
79 BPF_MAP_TYPE_UNSPEC,
0f8e4bd8 80 BPF_MAP_TYPE_HASH,
28fbcfa0 81 BPF_MAP_TYPE_ARRAY,
04fd61ab 82 BPF_MAP_TYPE_PROG_ARRAY,
ea317b26 83 BPF_MAP_TYPE_PERF_EVENT_ARRAY,
824bd0ce 84 BPF_MAP_TYPE_PERCPU_HASH,
a10423b8 85 BPF_MAP_TYPE_PERCPU_ARRAY,
d5a3b1f6 86 BPF_MAP_TYPE_STACK_TRACE,
4ed8ec52 87 BPF_MAP_TYPE_CGROUP_ARRAY,
29ba732a 88 BPF_MAP_TYPE_LRU_HASH,
8f844938 89 BPF_MAP_TYPE_LRU_PERCPU_HASH,
99c55f7d
AS
90};
91
09756af4
AS
92enum bpf_prog_type {
93 BPF_PROG_TYPE_UNSPEC,
ddd872bc 94 BPF_PROG_TYPE_SOCKET_FILTER,
2541517c 95 BPF_PROG_TYPE_KPROBE,
96be4325 96 BPF_PROG_TYPE_SCHED_CLS,
94caee8c 97 BPF_PROG_TYPE_SCHED_ACT,
98b5c2c6 98 BPF_PROG_TYPE_TRACEPOINT,
6a773a15 99 BPF_PROG_TYPE_XDP,
0515e599 100 BPF_PROG_TYPE_PERF_EVENT,
0e33661d 101 BPF_PROG_TYPE_CGROUP_SKB,
09756af4
AS
102};
103
0e33661d
DM
104enum bpf_attach_type {
105 BPF_CGROUP_INET_INGRESS,
106 BPF_CGROUP_INET_EGRESS,
107 __MAX_BPF_ATTACH_TYPE
108};
109
110#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
111
f1a66f85
DB
112#define BPF_PSEUDO_MAP_FD 1
113
3274f520
AS
114/* flags for BPF_MAP_UPDATE_ELEM command */
115#define BPF_ANY 0 /* create new element or update existing */
116#define BPF_NOEXIST 1 /* create new element if it didn't exist */
117#define BPF_EXIST 2 /* update existing element */
118
6c905981 119#define BPF_F_NO_PREALLOC (1U << 0)
29ba732a 120/* Instead of having one common LRU list in the
8f844938 121 * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
29ba732a
MKL
122 * which can scale and perform better.
123 * Note, the LRU nodes (including free nodes) cannot be moved
124 * across different LRU lists.
125 */
126#define BPF_F_NO_COMMON_LRU (1U << 1)
6c905981 127
99c55f7d
AS
128union bpf_attr {
129 struct { /* anonymous struct used by BPF_MAP_CREATE command */
130 __u32 map_type; /* one of enum bpf_map_type */
131 __u32 key_size; /* size of key in bytes */
132 __u32 value_size; /* size of value in bytes */
133 __u32 max_entries; /* max number of entries in a map */
6c905981 134 __u32 map_flags; /* prealloc or not */
99c55f7d 135 };
db20fd2b
AS
136
137 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
138 __u32 map_fd;
139 __aligned_u64 key;
140 union {
141 __aligned_u64 value;
142 __aligned_u64 next_key;
143 };
3274f520 144 __u64 flags;
db20fd2b 145 };
09756af4
AS
146
147 struct { /* anonymous struct used by BPF_PROG_LOAD command */
148 __u32 prog_type; /* one of enum bpf_prog_type */
149 __u32 insn_cnt;
150 __aligned_u64 insns;
151 __aligned_u64 license;
cbd35700
AS
152 __u32 log_level; /* verbosity level of verifier */
153 __u32 log_size; /* size of user buffer */
154 __aligned_u64 log_buf; /* user supplied buffer */
2541517c 155 __u32 kern_version; /* checked when prog_type=kprobe */
09756af4 156 };
b2197755
DB
157
158 struct { /* anonymous struct used by BPF_OBJ_* commands */
159 __aligned_u64 pathname;
160 __u32 bpf_fd;
161 };
99c55f7d
AS
162} __attribute__((aligned(8)));
163
ebb676da
TG
164/* BPF helper function descriptions:
165 *
166 * void *bpf_map_lookup_elem(&map, &key)
167 * Return: Map value or NULL
168 *
169 * int bpf_map_update_elem(&map, &key, &value, flags)
170 * Return: 0 on success or negative error
171 *
172 * int bpf_map_delete_elem(&map, &key)
173 * Return: 0 on success or negative error
174 *
175 * int bpf_probe_read(void *dst, int size, void *src)
176 * Return: 0 on success or negative error
177 *
178 * u64 bpf_ktime_get_ns(void)
179 * Return: current ktime
180 *
181 * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
182 * Return: length of buffer written or negative error
183 *
184 * u32 bpf_prandom_u32(void)
185 * Return: random value
186 *
187 * u32 bpf_raw_smp_processor_id(void)
188 * Return: SMP processor ID
189 *
190 * int bpf_skb_store_bytes(skb, offset, from, len, flags)
191 * store bytes into packet
192 * @skb: pointer to skb
193 * @offset: offset within packet from skb->mac_header
194 * @from: pointer where to copy bytes from
195 * @len: number of bytes to store into packet
196 * @flags: bit 0 - if true, recompute skb->csum
197 * other bits - reserved
198 * Return: 0 on success or negative error
199 *
200 * int bpf_l3_csum_replace(skb, offset, from, to, flags)
201 * recompute IP checksum
202 * @skb: pointer to skb
203 * @offset: offset within packet where IP checksum is located
204 * @from: old value of header field
205 * @to: new value of header field
206 * @flags: bits 0-3 - size of header field
207 * other bits - reserved
208 * Return: 0 on success or negative error
209 *
210 * int bpf_l4_csum_replace(skb, offset, from, to, flags)
211 * recompute TCP/UDP checksum
212 * @skb: pointer to skb
213 * @offset: offset within packet where TCP/UDP checksum is located
214 * @from: old value of header field
215 * @to: new value of header field
216 * @flags: bits 0-3 - size of header field
217 * bit 4 - is pseudo header
218 * other bits - reserved
219 * Return: 0 on success or negative error
220 *
221 * int bpf_tail_call(ctx, prog_array_map, index)
222 * jump into another BPF program
223 * @ctx: context pointer passed to next program
224 * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
225 * @index: index inside array that selects specific program to run
226 * Return: 0 on success or negative error
227 *
228 * int bpf_clone_redirect(skb, ifindex, flags)
229 * redirect to another netdev
230 * @skb: pointer to skb
231 * @ifindex: ifindex of the net device
232 * @flags: bit 0 - if set, redirect to ingress instead of egress
233 * other bits - reserved
234 * Return: 0 on success or negative error
235 *
236 * u64 bpf_get_current_pid_tgid(void)
237 * Return: current->tgid << 32 | current->pid
238 *
239 * u64 bpf_get_current_uid_gid(void)
240 * Return: current_gid << 32 | current_uid
241 *
242 * int bpf_get_current_comm(char *buf, int size_of_buf)
243 * stores current->comm into buf
244 * Return: 0 on success or negative error
245 *
246 * u32 bpf_get_cgroup_classid(skb)
247 * retrieve a proc's classid
248 * @skb: pointer to skb
249 * Return: classid if != 0
250 *
251 * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
252 * Return: 0 on success or negative error
253 *
254 * int bpf_skb_vlan_pop(skb)
255 * Return: 0 on success or negative error
256 *
257 * int bpf_skb_get_tunnel_key(skb, key, size, flags)
258 * int bpf_skb_set_tunnel_key(skb, key, size, flags)
259 * retrieve or populate tunnel metadata
260 * @skb: pointer to skb
261 * @key: pointer to 'struct bpf_tunnel_key'
262 * @size: size of 'struct bpf_tunnel_key'
263 * @flags: room for future extensions
264 * Return: 0 on success or negative error
265 *
266 * u64 bpf_perf_event_read(&map, index)
267 * Return: Number events read or error code
268 *
269 * int bpf_redirect(ifindex, flags)
270 * redirect to another netdev
271 * @ifindex: ifindex of the net device
272 * @flags: bit 0 - if set, redirect to ingress instead of egress
273 * other bits - reserved
274 * Return: TC_ACT_REDIRECT
275 *
276 * u32 bpf_get_route_realm(skb)
277 * retrieve a dst's tclassid
278 * @skb: pointer to skb
279 * Return: realm if != 0
280 *
281 * int bpf_perf_event_output(ctx, map, index, data, size)
282 * output perf raw sample
283 * @ctx: struct pt_regs*
284 * @map: pointer to perf_event_array map
285 * @index: index of event in the map
286 * @data: data on stack to be output as raw data
287 * @size: size of data
288 * Return: 0 on success or negative error
289 *
290 * int bpf_get_stackid(ctx, map, flags)
291 * walk user or kernel stack and return id
292 * @ctx: struct pt_regs*
293 * @map: pointer to stack_trace map
294 * @flags: bits 0-7 - numer of stack frames to skip
295 * bit 8 - collect user stack instead of kernel
296 * bit 9 - compare stacks by hash only
297 * bit 10 - if two different stacks hash into the same stackid
298 * discard old
299 * other bits - reserved
300 * Return: >= 0 stackid on success or negative error
301 *
302 * s64 bpf_csum_diff(from, from_size, to, to_size, seed)
303 * calculate csum diff
304 * @from: raw from buffer
305 * @from_size: length of from buffer
306 * @to: raw to buffer
307 * @to_size: length of to buffer
308 * @seed: optional seed
309 * Return: csum result or negative error code
310 *
311 * int bpf_skb_get_tunnel_opt(skb, opt, size)
312 * retrieve tunnel options metadata
313 * @skb: pointer to skb
314 * @opt: pointer to raw tunnel option data
315 * @size: size of @opt
316 * Return: option size
317 *
318 * int bpf_skb_set_tunnel_opt(skb, opt, size)
319 * populate tunnel options metadata
320 * @skb: pointer to skb
321 * @opt: pointer to raw tunnel option data
322 * @size: size of @opt
323 * Return: 0 on success or negative error
324 *
325 * int bpf_skb_change_proto(skb, proto, flags)
326 * Change protocol of the skb. Currently supported is v4 -> v6,
327 * v6 -> v4 transitions. The helper will also resize the skb. eBPF
328 * program is expected to fill the new headers via skb_store_bytes
329 * and lX_csum_replace.
330 * @skb: pointer to skb
331 * @proto: new skb->protocol type
332 * @flags: reserved
333 * Return: 0 on success or negative error
334 *
335 * int bpf_skb_change_type(skb, type)
336 * Change packet type of skb.
337 * @skb: pointer to skb
338 * @type: new skb->pkt_type type
339 * Return: 0 on success or negative error
340 *
341 * int bpf_skb_under_cgroup(skb, map, index)
342 * Check cgroup2 membership of skb
343 * @skb: pointer to skb
344 * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
345 * @index: index of the cgroup in the bpf_map
346 * Return:
347 * == 0 skb failed the cgroup2 descendant test
348 * == 1 skb succeeded the cgroup2 descendant test
349 * < 0 error
350 *
351 * u32 bpf_get_hash_recalc(skb)
352 * Retrieve and possibly recalculate skb->hash.
353 * @skb: pointer to skb
354 * Return: hash
355 *
356 * u64 bpf_get_current_task(void)
357 * Returns current task_struct
358 * Return: current
359 *
360 * int bpf_probe_write_user(void *dst, void *src, int len)
361 * safely attempt to write to a location
362 * @dst: destination address in userspace
363 * @src: source address on stack
364 * @len: number of bytes to copy
365 * Return: 0 on success or negative error
366 *
367 * int bpf_current_task_under_cgroup(map, index)
368 * Check cgroup2 membership of current task
369 * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
370 * @index: index of the cgroup in the bpf_map
371 * Return:
372 * == 0 current failed the cgroup2 descendant test
373 * == 1 current succeeded the cgroup2 descendant test
374 * < 0 error
375 *
376 * int bpf_skb_change_tail(skb, len, flags)
377 * The helper will resize the skb to the given new size, to be used f.e.
378 * with control messages.
379 * @skb: pointer to skb
380 * @len: new skb length
381 * @flags: reserved
382 * Return: 0 on success or negative error
383 *
384 * int bpf_skb_pull_data(skb, len)
385 * The helper will pull in non-linear data in case the skb is non-linear
386 * and not all of len are part of the linear section. Only needed for
387 * read/write with direct packet access.
388 * @skb: pointer to skb
389 * @len: len to make read/writeable
390 * Return: 0 on success or negative error
391 *
392 * s64 bpf_csum_update(skb, csum)
393 * Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
394 * @skb: pointer to skb
395 * @csum: csum to add
396 * Return: csum on success or negative error
397 *
398 * void bpf_set_hash_invalid(skb)
399 * Invalidate current skb->hash.
400 * @skb: pointer to skb
401 *
402 * int bpf_get_numa_node_id()
403 * Return: Id of current NUMA node.
404 */
405#define __BPF_FUNC_MAPPER(FN) \
406 FN(unspec), \
407 FN(map_lookup_elem), \
408 FN(map_update_elem), \
409 FN(map_delete_elem), \
410 FN(probe_read), \
411 FN(ktime_get_ns), \
412 FN(trace_printk), \
413 FN(get_prandom_u32), \
414 FN(get_smp_processor_id), \
415 FN(skb_store_bytes), \
416 FN(l3_csum_replace), \
417 FN(l4_csum_replace), \
418 FN(tail_call), \
419 FN(clone_redirect), \
420 FN(get_current_pid_tgid), \
421 FN(get_current_uid_gid), \
422 FN(get_current_comm), \
423 FN(get_cgroup_classid), \
424 FN(skb_vlan_push), \
425 FN(skb_vlan_pop), \
426 FN(skb_get_tunnel_key), \
427 FN(skb_set_tunnel_key), \
428 FN(perf_event_read), \
429 FN(redirect), \
430 FN(get_route_realm), \
431 FN(perf_event_output), \
432 FN(skb_load_bytes), \
433 FN(get_stackid), \
434 FN(csum_diff), \
435 FN(skb_get_tunnel_opt), \
436 FN(skb_set_tunnel_opt), \
437 FN(skb_change_proto), \
438 FN(skb_change_type), \
439 FN(skb_under_cgroup), \
440 FN(get_hash_recalc), \
441 FN(get_current_task), \
442 FN(probe_write_user), \
443 FN(current_task_under_cgroup), \
444 FN(skb_change_tail), \
445 FN(skb_pull_data), \
446 FN(csum_update), \
447 FN(set_hash_invalid), \
448 FN(get_numa_node_id),
449
09756af4
AS
450/* integer value in 'imm' field of BPF_CALL instruction selects which helper
451 * function eBPF program intends to call
452 */
ebb676da 453#define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
09756af4 454enum bpf_func_id {
ebb676da 455 __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
09756af4
AS
456 __BPF_FUNC_MAX_ID,
457};
ebb676da 458#undef __BPF_ENUM_FN
09756af4 459
781c53bc
DB
460/* All flags used by eBPF helper functions, placed here. */
461
462/* BPF_FUNC_skb_store_bytes flags. */
463#define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
8afd54c8 464#define BPF_F_INVALIDATE_HASH (1ULL << 1)
781c53bc
DB
465
466/* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
467 * First 4 bits are for passing the header field size.
468 */
469#define BPF_F_HDR_FIELD_MASK 0xfULL
470
471/* BPF_FUNC_l4_csum_replace flags. */
472#define BPF_F_PSEUDO_HDR (1ULL << 4)
2f72959a 473#define BPF_F_MARK_MANGLED_0 (1ULL << 5)
781c53bc
DB
474
475/* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
476#define BPF_F_INGRESS (1ULL << 0)
477
c6c33454
DB
478/* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
479#define BPF_F_TUNINFO_IPV6 (1ULL << 0)
480
d5a3b1f6
AS
481/* BPF_FUNC_get_stackid flags. */
482#define BPF_F_SKIP_FIELD_MASK 0xffULL
483#define BPF_F_USER_STACK (1ULL << 8)
484#define BPF_F_FAST_STACK_CMP (1ULL << 9)
485#define BPF_F_REUSE_STACKID (1ULL << 10)
486
2da897e5
DB
487/* BPF_FUNC_skb_set_tunnel_key flags. */
488#define BPF_F_ZERO_CSUM_TX (1ULL << 1)
22080870 489#define BPF_F_DONT_FRAGMENT (1ULL << 2)
2da897e5 490
6816a7ff 491/* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */
1e33759c
DB
492#define BPF_F_INDEX_MASK 0xffffffffULL
493#define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
555c8a86
DB
494/* BPF_FUNC_perf_event_output for sk_buff input context. */
495#define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
1e33759c 496
9bac3d6d
AS
497/* user accessible mirror of in-kernel sk_buff.
498 * new fields can only be added to the end of this structure
499 */
500struct __sk_buff {
501 __u32 len;
502 __u32 pkt_type;
503 __u32 mark;
504 __u32 queue_mapping;
c2497395
AS
505 __u32 protocol;
506 __u32 vlan_present;
507 __u32 vlan_tci;
27cd5452 508 __u32 vlan_proto;
bcad5718 509 __u32 priority;
37e82c2f
AS
510 __u32 ingress_ifindex;
511 __u32 ifindex;
d691f9e8
AS
512 __u32 tc_index;
513 __u32 cb[5];
ba7591d8 514 __u32 hash;
045efa82 515 __u32 tc_classid;
969bf05e
AS
516 __u32 data;
517 __u32 data_end;
9bac3d6d
AS
518};
519
d3aa45ce
AS
520struct bpf_tunnel_key {
521 __u32 tunnel_id;
c6c33454
DB
522 union {
523 __u32 remote_ipv4;
524 __u32 remote_ipv6[4];
525 };
526 __u8 tunnel_tos;
527 __u8 tunnel_ttl;
c0e760c9 528 __u16 tunnel_ext;
4018ab18 529 __u32 tunnel_label;
d3aa45ce
AS
530};
531
6a773a15
BB
532/* User return codes for XDP prog type.
533 * A valid XDP program must return one of these defined values. All other
534 * return codes are reserved for future use. Unknown return codes will result
535 * in packet drop.
536 */
537enum xdp_action {
538 XDP_ABORTED = 0,
539 XDP_DROP,
540 XDP_PASS,
6ce96ca3 541 XDP_TX,
6a773a15
BB
542};
543
544/* user accessible metadata for XDP packet hook
545 * new fields must be added to the end of this structure
546 */
547struct xdp_md {
548 __u32 data;
549 __u32 data_end;
550};
551
daedfb22 552#endif /* _UAPI__LINUX_BPF_H__ */