bpf: Add BPF_MAP_TYPE_LRU_PERCPU_HASH
[linux-2.6-block.git] / include / uapi / linux / bpf.h
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>
11 #include <linux/bpf_common.h>
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 */
40 enum {
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
58 struct 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
66 /* BPF syscall commands, see bpf(2) man-page for details. */
67 enum bpf_cmd {
68         BPF_MAP_CREATE,
69         BPF_MAP_LOOKUP_ELEM,
70         BPF_MAP_UPDATE_ELEM,
71         BPF_MAP_DELETE_ELEM,
72         BPF_MAP_GET_NEXT_KEY,
73         BPF_PROG_LOAD,
74         BPF_OBJ_PIN,
75         BPF_OBJ_GET,
76 };
77
78 enum bpf_map_type {
79         BPF_MAP_TYPE_UNSPEC,
80         BPF_MAP_TYPE_HASH,
81         BPF_MAP_TYPE_ARRAY,
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,
90 };
91
92 enum bpf_prog_type {
93         BPF_PROG_TYPE_UNSPEC,
94         BPF_PROG_TYPE_SOCKET_FILTER,
95         BPF_PROG_TYPE_KPROBE,
96         BPF_PROG_TYPE_SCHED_CLS,
97         BPF_PROG_TYPE_SCHED_ACT,
98         BPF_PROG_TYPE_TRACEPOINT,
99         BPF_PROG_TYPE_XDP,
100         BPF_PROG_TYPE_PERF_EVENT,
101 };
102
103 #define BPF_PSEUDO_MAP_FD       1
104
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 */
109
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.
116  */
117 #define BPF_F_NO_COMMON_LRU     (1U << 1)
118
119 union bpf_attr {
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 */
126         };
127
128         struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
129                 __u32           map_fd;
130                 __aligned_u64   key;
131                 union {
132                         __aligned_u64 value;
133                         __aligned_u64 next_key;
134                 };
135                 __u64           flags;
136         };
137
138         struct { /* anonymous struct used by BPF_PROG_LOAD command */
139                 __u32           prog_type;      /* one of enum bpf_prog_type */
140                 __u32           insn_cnt;
141                 __aligned_u64   insns;
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 */
147         };
148
149         struct { /* anonymous struct used by BPF_OBJ_* commands */
150                 __aligned_u64   pathname;
151                 __u32           bpf_fd;
152         };
153 } __attribute__((aligned(8)));
154
155 /* BPF helper function descriptions:
156  *
157  * void *bpf_map_lookup_elem(&map, &key)
158  *     Return: Map value or NULL
159  *
160  * int bpf_map_update_elem(&map, &key, &value, flags)
161  *     Return: 0 on success or negative error
162  *
163  * int bpf_map_delete_elem(&map, &key)
164  *     Return: 0 on success or negative error
165  *
166  * int bpf_probe_read(void *dst, int size, void *src)
167  *     Return: 0 on success or negative error
168  *
169  * u64 bpf_ktime_get_ns(void)
170  *     Return: current ktime
171  *
172  * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
173  *     Return: length of buffer written or negative error
174  *
175  * u32 bpf_prandom_u32(void)
176  *     Return: random value
177  *
178  * u32 bpf_raw_smp_processor_id(void)
179  *     Return: SMP processor ID
180  *
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
190  *
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
200  *
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
211  *
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
218  *
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
226  *
227  * u64 bpf_get_current_pid_tgid(void)
228  *     Return: current->tgid << 32 | current->pid
229  *
230  * u64 bpf_get_current_uid_gid(void)
231  *     Return: current_gid << 32 | current_uid
232  *
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
236  *
237  * u32 bpf_get_cgroup_classid(skb)
238  *     retrieve a proc's classid
239  *     @skb: pointer to skb
240  *     Return: classid if != 0
241  *
242  * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
243  *     Return: 0 on success or negative error
244  *
245  * int bpf_skb_vlan_pop(skb)
246  *     Return: 0 on success or negative error
247  *
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
256  *
257  * u64 bpf_perf_event_read(&map, index)
258  *     Return: Number events read or error code
259  *
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
266  *
267  * u32 bpf_get_route_realm(skb)
268  *     retrieve a dst's tclassid
269  *     @skb: pointer to skb
270  *     Return: realm if != 0
271  *
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
280  *
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
289  *                      discard old
290  *             other bits - reserved
291  *     Return: >= 0 stackid on success or negative error
292  *
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
297  *     @to: raw to buffer
298  *     @to_size: length of to buffer
299  *     @seed: optional seed
300  *     Return: csum result or negative error code
301  *
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
308  *
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
315  *
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
323  *     @flags: reserved
324  *     Return: 0 on success or negative error
325  *
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
331  *
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
337  *     Return:
338  *       == 0 skb failed the cgroup2 descendant test
339  *       == 1 skb succeeded the cgroup2 descendant test
340  *        < 0 error
341  *
342  * u32 bpf_get_hash_recalc(skb)
343  *     Retrieve and possibly recalculate skb->hash.
344  *     @skb: pointer to skb
345  *     Return: hash
346  *
347  * u64 bpf_get_current_task(void)
348  *     Returns current task_struct
349  *     Return: current
350  *
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
357  *
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
362  *     Return:
363  *       == 0 current failed the cgroup2 descendant test
364  *       == 1 current succeeded the cgroup2 descendant test
365  *        < 0 error
366  *
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
372  *     @flags: reserved
373  *     Return: 0 on success or negative error
374  *
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
382  *
383  * s64 bpf_csum_update(skb, csum)
384  *     Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
385  *     @skb: pointer to skb
386  *     @csum: csum to add
387  *     Return: csum on success or negative error
388  *
389  * void bpf_set_hash_invalid(skb)
390  *     Invalidate current skb->hash.
391  *     @skb: pointer to skb
392  *
393  * int bpf_get_numa_node_id()
394  *     Return: Id of current NUMA node.
395  */
396 #define __BPF_FUNC_MAPPER(FN)           \
397         FN(unspec),                     \
398         FN(map_lookup_elem),            \
399         FN(map_update_elem),            \
400         FN(map_delete_elem),            \
401         FN(probe_read),                 \
402         FN(ktime_get_ns),               \
403         FN(trace_printk),               \
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),            \
409         FN(tail_call),                  \
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),         \
415         FN(skb_vlan_push),              \
416         FN(skb_vlan_pop),               \
417         FN(skb_get_tunnel_key),         \
418         FN(skb_set_tunnel_key),         \
419         FN(perf_event_read),            \
420         FN(redirect),                   \
421         FN(get_route_realm),            \
422         FN(perf_event_output),          \
423         FN(skb_load_bytes),             \
424         FN(get_stackid),                \
425         FN(csum_diff),                  \
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),            \
436         FN(skb_pull_data),              \
437         FN(csum_update),                \
438         FN(set_hash_invalid),           \
439         FN(get_numa_node_id),
440
441 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
442  * function eBPF program intends to call
443  */
444 #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
445 enum bpf_func_id {
446         __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
447         __BPF_FUNC_MAX_ID,
448 };
449 #undef __BPF_ENUM_FN
450
451 /* All flags used by eBPF helper functions, placed here. */
452
453 /* BPF_FUNC_skb_store_bytes flags. */
454 #define BPF_F_RECOMPUTE_CSUM            (1ULL << 0)
455 #define BPF_F_INVALIDATE_HASH           (1ULL << 1)
456
457 /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
458  * First 4 bits are for passing the header field size.
459  */
460 #define BPF_F_HDR_FIELD_MASK            0xfULL
461
462 /* BPF_FUNC_l4_csum_replace flags. */
463 #define BPF_F_PSEUDO_HDR                (1ULL << 4)
464 #define BPF_F_MARK_MANGLED_0            (1ULL << 5)
465
466 /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
467 #define BPF_F_INGRESS                   (1ULL << 0)
468
469 /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
470 #define BPF_F_TUNINFO_IPV6              (1ULL << 0)
471
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)
477
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)
481
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)
487
488 /* user accessible mirror of in-kernel sk_buff.
489  * new fields can only be added to the end of this structure
490  */
491 struct __sk_buff {
492         __u32 len;
493         __u32 pkt_type;
494         __u32 mark;
495         __u32 queue_mapping;
496         __u32 protocol;
497         __u32 vlan_present;
498         __u32 vlan_tci;
499         __u32 vlan_proto;
500         __u32 priority;
501         __u32 ingress_ifindex;
502         __u32 ifindex;
503         __u32 tc_index;
504         __u32 cb[5];
505         __u32 hash;
506         __u32 tc_classid;
507         __u32 data;
508         __u32 data_end;
509 };
510
511 struct bpf_tunnel_key {
512         __u32 tunnel_id;
513         union {
514                 __u32 remote_ipv4;
515                 __u32 remote_ipv6[4];
516         };
517         __u8 tunnel_tos;
518         __u8 tunnel_ttl;
519         __u16 tunnel_ext;
520         __u32 tunnel_label;
521 };
522
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
526  * in packet drop.
527  */
528 enum xdp_action {
529         XDP_ABORTED = 0,
530         XDP_DROP,
531         XDP_PASS,
532         XDP_TX,
533 };
534
535 /* user accessible metadata for XDP packet hook
536  * new fields must be added to the end of this structure
537  */
538 struct xdp_md {
539         __u32 data;
540         __u32 data_end;
541 };
542
543 #endif /* _UAPI__LINUX_BPF_H__ */