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