blob: 6d2137b4cf380d084d31a6c026b7f5153f0cf975 [file] [log] [blame]
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -03001/* 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
Daniel Borkmann92b31a92017-08-10 01:39:55 +020033/* jmp encodings */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030034#define BPF_JNE 0x50 /* jump != */
Daniel Borkmann92b31a92017-08-10 01:39:55 +020035#define BPF_JLT 0xa0 /* LT is unsigned, '<' */
36#define BPF_JLE 0xb0 /* LE is unsigned, '<=' */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030037#define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */
38#define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */
Daniel Borkmann92b31a92017-08-10 01:39:55 +020039#define BPF_JSLT 0xc0 /* SLT is signed, '<' */
40#define BPF_JSLE 0xd0 /* SLE is signed, '<=' */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030041#define BPF_CALL 0x80 /* function call */
42#define BPF_EXIT 0x90 /* function return */
43
44/* Register numbers */
45enum {
46 BPF_REG_0 = 0,
47 BPF_REG_1,
48 BPF_REG_2,
49 BPF_REG_3,
50 BPF_REG_4,
51 BPF_REG_5,
52 BPF_REG_6,
53 BPF_REG_7,
54 BPF_REG_8,
55 BPF_REG_9,
56 BPF_REG_10,
57 __MAX_BPF_REG,
58};
59
60/* BPF has 10 general purpose 64-bit registers and stack frame. */
61#define MAX_BPF_REG __MAX_BPF_REG
62
63struct bpf_insn {
64 __u8 code; /* opcode */
65 __u8 dst_reg:4; /* dest register */
66 __u8 src_reg:4; /* source register */
67 __s16 off; /* signed offset */
68 __s32 imm; /* signed immediate constant */
69};
70
Mickaël Salaün9a738262017-02-10 00:21:35 +010071/* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */
72struct bpf_lpm_trie_key {
73 __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */
74 __u8 data[0]; /* Arbitrary size */
75};
76
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030077/* BPF syscall commands, see bpf(2) man-page for details. */
78enum bpf_cmd {
79 BPF_MAP_CREATE,
80 BPF_MAP_LOOKUP_ELEM,
81 BPF_MAP_UPDATE_ELEM,
82 BPF_MAP_DELETE_ELEM,
83 BPF_MAP_GET_NEXT_KEY,
84 BPF_PROG_LOAD,
85 BPF_OBJ_PIN,
86 BPF_OBJ_GET,
Joe Stringer0cb34dc2016-12-08 18:46:14 -080087 BPF_PROG_ATTACH,
88 BPF_PROG_DETACH,
Alexei Starovoitov30848872017-03-30 21:45:39 -070089 BPF_PROG_TEST_RUN,
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -070090 BPF_PROG_GET_NEXT_ID,
91 BPF_MAP_GET_NEXT_ID,
92 BPF_PROG_GET_FD_BY_ID,
93 BPF_MAP_GET_FD_BY_ID,
94 BPF_OBJ_GET_INFO_BY_FD,
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030095};
96
97enum bpf_map_type {
98 BPF_MAP_TYPE_UNSPEC,
99 BPF_MAP_TYPE_HASH,
100 BPF_MAP_TYPE_ARRAY,
101 BPF_MAP_TYPE_PROG_ARRAY,
102 BPF_MAP_TYPE_PERF_EVENT_ARRAY,
103 BPF_MAP_TYPE_PERCPU_HASH,
104 BPF_MAP_TYPE_PERCPU_ARRAY,
105 BPF_MAP_TYPE_STACK_TRACE,
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300106 BPF_MAP_TYPE_CGROUP_ARRAY,
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800107 BPF_MAP_TYPE_LRU_HASH,
108 BPF_MAP_TYPE_LRU_PERCPU_HASH,
Mickaël Salaün9a738262017-02-10 00:21:35 +0100109 BPF_MAP_TYPE_LPM_TRIE,
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700110 BPF_MAP_TYPE_ARRAY_OF_MAPS,
111 BPF_MAP_TYPE_HASH_OF_MAPS,
John Fastabend81f6bf82017-07-26 17:32:07 -0700112 BPF_MAP_TYPE_DEVMAP,
John Fastabend69e8cc12017-08-15 22:33:32 -0700113 BPF_MAP_TYPE_SOCKMAP,
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300114};
115
116enum bpf_prog_type {
117 BPF_PROG_TYPE_UNSPEC,
118 BPF_PROG_TYPE_SOCKET_FILTER,
119 BPF_PROG_TYPE_KPROBE,
120 BPF_PROG_TYPE_SCHED_CLS,
121 BPF_PROG_TYPE_SCHED_ACT,
122 BPF_PROG_TYPE_TRACEPOINT,
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300123 BPF_PROG_TYPE_XDP,
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800124 BPF_PROG_TYPE_PERF_EVENT,
125 BPF_PROG_TYPE_CGROUP_SKB,
126 BPF_PROG_TYPE_CGROUP_SOCK,
127 BPF_PROG_TYPE_LWT_IN,
128 BPF_PROG_TYPE_LWT_OUT,
129 BPF_PROG_TYPE_LWT_XMIT,
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700130 BPF_PROG_TYPE_SOCK_OPS,
John Fastabend69e8cc12017-08-15 22:33:32 -0700131 BPF_PROG_TYPE_SK_SKB,
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300132};
133
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800134enum bpf_attach_type {
135 BPF_CGROUP_INET_INGRESS,
136 BPF_CGROUP_INET_EGRESS,
137 BPF_CGROUP_INET_SOCK_CREATE,
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700138 BPF_CGROUP_SOCK_OPS,
John Fastabend464bc0f2017-08-28 07:10:04 -0700139 BPF_SK_SKB_STREAM_PARSER,
140 BPF_SK_SKB_STREAM_VERDICT,
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800141 __MAX_BPF_ATTACH_TYPE
142};
143
144#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
145
Stephen Rothwell5463b3d2017-02-14 08:22:20 +1100146/* If BPF_F_ALLOW_OVERRIDE flag is used in BPF_PROG_ATTACH command
147 * to the given target_fd cgroup the descendent cgroup will be able to
148 * override effective bpf program that was inherited from this cgroup
149 */
150#define BPF_F_ALLOW_OVERRIDE (1U << 0)
151
David S. Millere07b98d2017-05-10 11:38:07 -0700152/* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
153 * verifier will perform strict alignment checking as if the kernel
154 * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set,
155 * and NET_IP_ALIGN defined to 2.
156 */
157#define BPF_F_STRICT_ALIGNMENT (1U << 0)
158
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300159#define BPF_PSEUDO_MAP_FD 1
160
161/* flags for BPF_MAP_UPDATE_ELEM command */
162#define BPF_ANY 0 /* create new element or update existing */
163#define BPF_NOEXIST 1 /* create new element if it didn't exist */
164#define BPF_EXIST 2 /* update existing element */
165
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700166/* flags for BPF_MAP_CREATE command */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300167#define BPF_F_NO_PREALLOC (1U << 0)
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800168/* Instead of having one common LRU list in the
169 * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
170 * which can scale and perform better.
171 * Note, the LRU nodes (including free nodes) cannot be moved
172 * across different LRU lists.
173 */
174#define BPF_F_NO_COMMON_LRU (1U << 1)
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700175/* Specify numa node during map creation */
176#define BPF_F_NUMA_NODE (1U << 2)
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300177
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700178#define BPF_OBJ_NAME_LEN 16U
179
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300180union bpf_attr {
181 struct { /* anonymous struct used by BPF_MAP_CREATE command */
182 __u32 map_type; /* one of enum bpf_map_type */
183 __u32 key_size; /* size of key in bytes */
184 __u32 value_size; /* size of value in bytes */
185 __u32 max_entries; /* max number of entries in a map */
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700186 __u32 map_flags; /* BPF_MAP_CREATE related
187 * flags defined above.
188 */
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700189 __u32 inner_map_fd; /* fd pointing to the inner map */
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700190 __u32 numa_node; /* numa node (effective only if
191 * BPF_F_NUMA_NODE is set).
192 */
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700193 __u8 map_name[BPF_OBJ_NAME_LEN];
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300194 };
195
196 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
197 __u32 map_fd;
198 __aligned_u64 key;
199 union {
200 __aligned_u64 value;
201 __aligned_u64 next_key;
202 };
203 __u64 flags;
204 };
205
206 struct { /* anonymous struct used by BPF_PROG_LOAD command */
207 __u32 prog_type; /* one of enum bpf_prog_type */
208 __u32 insn_cnt;
209 __aligned_u64 insns;
210 __aligned_u64 license;
211 __u32 log_level; /* verbosity level of verifier */
212 __u32 log_size; /* size of user buffer */
213 __aligned_u64 log_buf; /* user supplied buffer */
214 __u32 kern_version; /* checked when prog_type=kprobe */
David S. Millere07b98d2017-05-10 11:38:07 -0700215 __u32 prog_flags;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700216 __u8 prog_name[BPF_OBJ_NAME_LEN];
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300217 };
218
219 struct { /* anonymous struct used by BPF_OBJ_* commands */
220 __aligned_u64 pathname;
221 __u32 bpf_fd;
222 };
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800223
224 struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
225 __u32 target_fd; /* container object to attach to */
226 __u32 attach_bpf_fd; /* eBPF program to attach */
227 __u32 attach_type;
Stephen Rothwell5463b3d2017-02-14 08:22:20 +1100228 __u32 attach_flags;
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800229 };
Alexei Starovoitov30848872017-03-30 21:45:39 -0700230
231 struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
232 __u32 prog_fd;
233 __u32 retval;
234 __u32 data_size_in;
235 __u32 data_size_out;
236 __aligned_u64 data_in;
237 __aligned_u64 data_out;
238 __u32 repeat;
239 __u32 duration;
240 } test;
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700241
242 struct { /* anonymous struct used by BPF_*_GET_*_ID */
243 union {
244 __u32 start_id;
245 __u32 prog_id;
246 __u32 map_id;
247 };
248 __u32 next_id;
249 };
250
251 struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
252 __u32 bpf_fd;
253 __u32 info_len;
254 __aligned_u64 info;
255 } info;
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300256} __attribute__((aligned(8)));
257
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800258/* BPF helper function descriptions:
259 *
260 * void *bpf_map_lookup_elem(&map, &key)
261 * Return: Map value or NULL
262 *
263 * int bpf_map_update_elem(&map, &key, &value, flags)
264 * Return: 0 on success or negative error
265 *
266 * int bpf_map_delete_elem(&map, &key)
267 * Return: 0 on success or negative error
268 *
269 * int bpf_probe_read(void *dst, int size, void *src)
270 * Return: 0 on success or negative error
271 *
272 * u64 bpf_ktime_get_ns(void)
273 * Return: current ktime
274 *
275 * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
276 * Return: length of buffer written or negative error
277 *
278 * u32 bpf_prandom_u32(void)
279 * Return: random value
280 *
281 * u32 bpf_raw_smp_processor_id(void)
282 * Return: SMP processor ID
283 *
284 * int bpf_skb_store_bytes(skb, offset, from, len, flags)
285 * store bytes into packet
286 * @skb: pointer to skb
287 * @offset: offset within packet from skb->mac_header
288 * @from: pointer where to copy bytes from
289 * @len: number of bytes to store into packet
290 * @flags: bit 0 - if true, recompute skb->csum
291 * other bits - reserved
292 * Return: 0 on success or negative error
293 *
294 * int bpf_l3_csum_replace(skb, offset, from, to, flags)
295 * recompute IP checksum
296 * @skb: pointer to skb
297 * @offset: offset within packet where IP checksum is located
298 * @from: old value of header field
299 * @to: new value of header field
300 * @flags: bits 0-3 - size of header field
301 * other bits - reserved
302 * Return: 0 on success or negative error
303 *
304 * int bpf_l4_csum_replace(skb, offset, from, to, flags)
305 * recompute TCP/UDP checksum
306 * @skb: pointer to skb
307 * @offset: offset within packet where TCP/UDP checksum is located
308 * @from: old value of header field
309 * @to: new value of header field
310 * @flags: bits 0-3 - size of header field
311 * bit 4 - is pseudo header
312 * other bits - reserved
313 * Return: 0 on success or negative error
314 *
315 * int bpf_tail_call(ctx, prog_array_map, index)
316 * jump into another BPF program
317 * @ctx: context pointer passed to next program
318 * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
319 * @index: index inside array that selects specific program to run
320 * Return: 0 on success or negative error
321 *
322 * int bpf_clone_redirect(skb, ifindex, flags)
323 * redirect to another netdev
324 * @skb: pointer to skb
325 * @ifindex: ifindex of the net device
326 * @flags: bit 0 - if set, redirect to ingress instead of egress
327 * other bits - reserved
328 * Return: 0 on success or negative error
329 *
330 * u64 bpf_get_current_pid_tgid(void)
331 * Return: current->tgid << 32 | current->pid
332 *
333 * u64 bpf_get_current_uid_gid(void)
334 * Return: current_gid << 32 | current_uid
335 *
336 * int bpf_get_current_comm(char *buf, int size_of_buf)
337 * stores current->comm into buf
338 * Return: 0 on success or negative error
339 *
340 * u32 bpf_get_cgroup_classid(skb)
341 * retrieve a proc's classid
342 * @skb: pointer to skb
343 * Return: classid if != 0
344 *
345 * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
346 * Return: 0 on success or negative error
347 *
348 * int bpf_skb_vlan_pop(skb)
349 * Return: 0 on success or negative error
350 *
351 * int bpf_skb_get_tunnel_key(skb, key, size, flags)
352 * int bpf_skb_set_tunnel_key(skb, key, size, flags)
353 * retrieve or populate tunnel metadata
354 * @skb: pointer to skb
355 * @key: pointer to 'struct bpf_tunnel_key'
356 * @size: size of 'struct bpf_tunnel_key'
357 * @flags: room for future extensions
358 * Return: 0 on success or negative error
359 *
Teng Qinb7d3ed52017-06-02 21:03:54 -0700360 * u64 bpf_perf_event_read(map, flags)
361 * read perf event counter value
362 * @map: pointer to perf_event_array map
363 * @flags: index of event in the map or bitmask flags
364 * Return: value of perf event counter read or error code
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800365 *
366 * int bpf_redirect(ifindex, flags)
367 * redirect to another netdev
368 * @ifindex: ifindex of the net device
Daniel Borkmannac299912017-09-25 02:25:52 +0200369 * @flags:
370 * cls_bpf:
371 * bit 0 - if set, redirect to ingress instead of egress
372 * other bits - reserved
373 * xdp_bpf:
374 * all bits - reserved
375 * Return: cls_bpf: TC_ACT_REDIRECT on success or TC_ACT_SHOT on error
376 * xdp_bfp: XDP_REDIRECT on success or XDP_ABORT on error
377 * int bpf_redirect_map(map, key, flags)
378 * redirect to endpoint in map
379 * @map: pointer to dev map
380 * @key: index in map to lookup
381 * @flags: --
382 * Return: XDP_REDIRECT on success or XDP_ABORT on error
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800383 *
384 * u32 bpf_get_route_realm(skb)
385 * retrieve a dst's tclassid
386 * @skb: pointer to skb
387 * Return: realm if != 0
388 *
Teng Qinb7d3ed52017-06-02 21:03:54 -0700389 * int bpf_perf_event_output(ctx, map, flags, data, size)
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800390 * output perf raw sample
391 * @ctx: struct pt_regs*
392 * @map: pointer to perf_event_array map
Teng Qinb7d3ed52017-06-02 21:03:54 -0700393 * @flags: index of event in the map or bitmask flags
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800394 * @data: data on stack to be output as raw data
395 * @size: size of data
396 * Return: 0 on success or negative error
397 *
398 * int bpf_get_stackid(ctx, map, flags)
399 * walk user or kernel stack and return id
400 * @ctx: struct pt_regs*
401 * @map: pointer to stack_trace map
402 * @flags: bits 0-7 - numer of stack frames to skip
403 * bit 8 - collect user stack instead of kernel
404 * bit 9 - compare stacks by hash only
405 * bit 10 - if two different stacks hash into the same stackid
406 * discard old
407 * other bits - reserved
408 * Return: >= 0 stackid on success or negative error
409 *
410 * s64 bpf_csum_diff(from, from_size, to, to_size, seed)
411 * calculate csum diff
412 * @from: raw from buffer
413 * @from_size: length of from buffer
414 * @to: raw to buffer
415 * @to_size: length of to buffer
416 * @seed: optional seed
417 * Return: csum result or negative error code
418 *
419 * int bpf_skb_get_tunnel_opt(skb, opt, size)
420 * retrieve tunnel options metadata
421 * @skb: pointer to skb
422 * @opt: pointer to raw tunnel option data
423 * @size: size of @opt
424 * Return: option size
425 *
426 * int bpf_skb_set_tunnel_opt(skb, opt, size)
427 * populate tunnel options metadata
428 * @skb: pointer to skb
429 * @opt: pointer to raw tunnel option data
430 * @size: size of @opt
431 * Return: 0 on success or negative error
432 *
433 * int bpf_skb_change_proto(skb, proto, flags)
434 * Change protocol of the skb. Currently supported is v4 -> v6,
435 * v6 -> v4 transitions. The helper will also resize the skb. eBPF
436 * program is expected to fill the new headers via skb_store_bytes
437 * and lX_csum_replace.
438 * @skb: pointer to skb
439 * @proto: new skb->protocol type
440 * @flags: reserved
441 * Return: 0 on success or negative error
442 *
443 * int bpf_skb_change_type(skb, type)
444 * Change packet type of skb.
445 * @skb: pointer to skb
446 * @type: new skb->pkt_type type
447 * Return: 0 on success or negative error
448 *
449 * int bpf_skb_under_cgroup(skb, map, index)
450 * Check cgroup2 membership of skb
451 * @skb: pointer to skb
452 * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
453 * @index: index of the cgroup in the bpf_map
454 * Return:
455 * == 0 skb failed the cgroup2 descendant test
456 * == 1 skb succeeded the cgroup2 descendant test
457 * < 0 error
458 *
459 * u32 bpf_get_hash_recalc(skb)
460 * Retrieve and possibly recalculate skb->hash.
461 * @skb: pointer to skb
462 * Return: hash
463 *
464 * u64 bpf_get_current_task(void)
465 * Returns current task_struct
466 * Return: current
467 *
468 * int bpf_probe_write_user(void *dst, void *src, int len)
469 * safely attempt to write to a location
470 * @dst: destination address in userspace
471 * @src: source address on stack
472 * @len: number of bytes to copy
473 * Return: 0 on success or negative error
474 *
475 * int bpf_current_task_under_cgroup(map, index)
476 * Check cgroup2 membership of current task
477 * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
478 * @index: index of the cgroup in the bpf_map
479 * Return:
480 * == 0 current failed the cgroup2 descendant test
481 * == 1 current succeeded the cgroup2 descendant test
482 * < 0 error
483 *
484 * int bpf_skb_change_tail(skb, len, flags)
485 * The helper will resize the skb to the given new size, to be used f.e.
486 * with control messages.
487 * @skb: pointer to skb
488 * @len: new skb length
489 * @flags: reserved
490 * Return: 0 on success or negative error
491 *
492 * int bpf_skb_pull_data(skb, len)
493 * The helper will pull in non-linear data in case the skb is non-linear
494 * and not all of len are part of the linear section. Only needed for
495 * read/write with direct packet access.
496 * @skb: pointer to skb
497 * @len: len to make read/writeable
498 * Return: 0 on success or negative error
499 *
500 * s64 bpf_csum_update(skb, csum)
501 * Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
502 * @skb: pointer to skb
503 * @csum: csum to add
504 * Return: csum on success or negative error
505 *
506 * void bpf_set_hash_invalid(skb)
507 * Invalidate current skb->hash.
508 * @skb: pointer to skb
509 *
510 * int bpf_get_numa_node_id()
511 * Return: Id of current NUMA node.
512 *
513 * int bpf_skb_change_head()
514 * Grows headroom of skb and adjusts MAC header offset accordingly.
515 * Will extends/reallocae as required automatically.
516 * May change skb data pointer and will thus invalidate any check
517 * performed for direct packet access.
518 * @skb: pointer to skb
519 * @len: length of header to be pushed in front
520 * @flags: Flags (unused for now)
521 * Return: 0 on success or negative error
522 *
523 * int bpf_xdp_adjust_head(xdp_md, delta)
524 * Adjust the xdp_md.data by delta
525 * @xdp_md: pointer to xdp_md
526 * @delta: An positive/negative integer to be added to xdp_md.data
527 * Return: 0 on success or negative on error
Mickaël Salaün9a738262017-02-10 00:21:35 +0100528 *
529 * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
530 * Copy a NUL terminated string from unsafe address. In case the string
531 * length is smaller than size, the target is not padded with further NUL
532 * bytes. In case the string length is larger than size, just count-1
533 * bytes are copied and the last byte is set to NUL.
534 * @dst: destination address
535 * @size: maximum number of bytes to copy, including the trailing NUL
536 * @unsafe_ptr: unsafe address
537 * Return:
538 * > 0 length of the string including the trailing NUL on success
539 * < 0 error
Alexei Starovoitov30848872017-03-30 21:45:39 -0700540 *
Alexander Alemayhu3c60a532017-04-08 22:08:10 +0200541 * u64 bpf_get_socket_cookie(skb)
Alexei Starovoitov30848872017-03-30 21:45:39 -0700542 * Get the cookie for the socket stored inside sk_buff.
543 * @skb: pointer to skb
544 * Return: 8 Bytes non-decreasing number on success or 0 if the socket
545 * field is missing inside sk_buff
546 *
547 * u32 bpf_get_socket_uid(skb)
548 * Get the owner uid of the socket stored inside sk_buff.
549 * @skb: pointer to skb
David S. Millere07b98d2017-05-10 11:38:07 -0700550 * Return: uid of the socket owner on success or overflowuid if failed.
Daniel Borkmannded092c2017-06-11 00:50:47 +0200551 *
552 * u32 bpf_set_hash(skb, hash)
553 * Set full skb->hash.
554 * @skb: pointer to skb
555 * @hash: hash to set
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700556 *
557 * int bpf_setsockopt(bpf_socket, level, optname, optval, optlen)
558 * Calls setsockopt. Not all opts are available, only those with
559 * integer optvals plus TCP_CONGESTION.
560 * Supported levels: SOL_SOCKET and IPROTO_TCP
561 * @bpf_socket: pointer to bpf_socket
562 * @level: SOL_SOCKET or IPROTO_TCP
563 * @optname: option name
564 * @optval: pointer to option value
565 * @optlen: length of optval in byes
566 * Return: 0 or negative error
Daniel Borkmann2be7e212017-07-02 02:13:26 +0200567 *
568 * int bpf_skb_adjust_room(skb, len_diff, mode, flags)
569 * Grow or shrink room in sk_buff.
570 * @skb: pointer to skb
571 * @len_diff: (signed) amount of room to grow/shrink
572 * @mode: operation mode (enum bpf_adj_room_mode)
573 * @flags: reserved for future use
574 * Return: 0 on success or negative error code
John Fastabend69e8cc12017-08-15 22:33:32 -0700575 *
576 * int bpf_sk_redirect_map(map, key, flags)
577 * Redirect skb to a sock in map using key as a lookup key for the
578 * sock in map.
579 * @map: pointer to sockmap
580 * @key: key to lookup sock in map
581 * @flags: reserved for future use
582 * Return: SK_REDIRECT
583 *
John Fastabend464bc0f2017-08-28 07:10:04 -0700584 * int bpf_sock_map_update(skops, map, key, flags)
John Fastabend69e8cc12017-08-15 22:33:32 -0700585 * @skops: pointer to bpf_sock_ops
586 * @map: pointer to sockmap to update
587 * @key: key to insert/update sock in map
588 * @flags: same flags as map update elem
Daniel Borkmannac299912017-09-25 02:25:52 +0200589 *
590 * int bpf_xdp_adjust_meta(xdp_md, delta)
591 * Adjust the xdp_md.data_meta by delta
592 * @xdp_md: pointer to xdp_md
593 * @delta: An positive/negative integer to be added to xdp_md.data_meta
594 * Return: 0 on success or negative on error
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800595 */
596#define __BPF_FUNC_MAPPER(FN) \
597 FN(unspec), \
598 FN(map_lookup_elem), \
599 FN(map_update_elem), \
600 FN(map_delete_elem), \
601 FN(probe_read), \
602 FN(ktime_get_ns), \
603 FN(trace_printk), \
604 FN(get_prandom_u32), \
605 FN(get_smp_processor_id), \
606 FN(skb_store_bytes), \
607 FN(l3_csum_replace), \
608 FN(l4_csum_replace), \
609 FN(tail_call), \
610 FN(clone_redirect), \
611 FN(get_current_pid_tgid), \
612 FN(get_current_uid_gid), \
613 FN(get_current_comm), \
614 FN(get_cgroup_classid), \
615 FN(skb_vlan_push), \
616 FN(skb_vlan_pop), \
617 FN(skb_get_tunnel_key), \
618 FN(skb_set_tunnel_key), \
619 FN(perf_event_read), \
620 FN(redirect), \
621 FN(get_route_realm), \
622 FN(perf_event_output), \
623 FN(skb_load_bytes), \
624 FN(get_stackid), \
625 FN(csum_diff), \
626 FN(skb_get_tunnel_opt), \
627 FN(skb_set_tunnel_opt), \
628 FN(skb_change_proto), \
629 FN(skb_change_type), \
630 FN(skb_under_cgroup), \
631 FN(get_hash_recalc), \
632 FN(get_current_task), \
633 FN(probe_write_user), \
634 FN(current_task_under_cgroup), \
635 FN(skb_change_tail), \
636 FN(skb_pull_data), \
637 FN(csum_update), \
638 FN(set_hash_invalid), \
639 FN(get_numa_node_id), \
640 FN(skb_change_head), \
Mickaël Salaün9a738262017-02-10 00:21:35 +0100641 FN(xdp_adjust_head), \
Chenbo Feng91b82702017-03-22 17:27:34 -0700642 FN(probe_read_str), \
Chenbo Feng6acc5c22017-03-22 17:27:35 -0700643 FN(get_socket_cookie), \
Daniel Borkmannded092c2017-06-11 00:50:47 +0200644 FN(get_socket_uid), \
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700645 FN(set_hash), \
Daniel Borkmann2be7e212017-07-02 02:13:26 +0200646 FN(setsockopt), \
William Tu996139e2017-08-07 13:14:42 -0700647 FN(skb_adjust_room), \
John Fastabend69e8cc12017-08-15 22:33:32 -0700648 FN(redirect_map), \
649 FN(sk_redirect_map), \
Daniel Borkmannac299912017-09-25 02:25:52 +0200650 FN(sock_map_update), \
651 FN(xdp_adjust_meta),
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800652
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300653/* integer value in 'imm' field of BPF_CALL instruction selects which helper
654 * function eBPF program intends to call
655 */
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800656#define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300657enum bpf_func_id {
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800658 __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300659 __BPF_FUNC_MAX_ID,
660};
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800661#undef __BPF_ENUM_FN
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300662
663/* All flags used by eBPF helper functions, placed here. */
664
665/* BPF_FUNC_skb_store_bytes flags. */
666#define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
667#define BPF_F_INVALIDATE_HASH (1ULL << 1)
668
669/* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
670 * First 4 bits are for passing the header field size.
671 */
672#define BPF_F_HDR_FIELD_MASK 0xfULL
673
674/* BPF_FUNC_l4_csum_replace flags. */
675#define BPF_F_PSEUDO_HDR (1ULL << 4)
676#define BPF_F_MARK_MANGLED_0 (1ULL << 5)
Mickaël Salaün9a738262017-02-10 00:21:35 +0100677#define BPF_F_MARK_ENFORCE (1ULL << 6)
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300678
679/* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
680#define BPF_F_INGRESS (1ULL << 0)
681
682/* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
683#define BPF_F_TUNINFO_IPV6 (1ULL << 0)
684
685/* BPF_FUNC_get_stackid flags. */
686#define BPF_F_SKIP_FIELD_MASK 0xffULL
687#define BPF_F_USER_STACK (1ULL << 8)
688#define BPF_F_FAST_STACK_CMP (1ULL << 9)
689#define BPF_F_REUSE_STACKID (1ULL << 10)
690
691/* BPF_FUNC_skb_set_tunnel_key flags. */
692#define BPF_F_ZERO_CSUM_TX (1ULL << 1)
693#define BPF_F_DONT_FRAGMENT (1ULL << 2)
694
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300695/* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300696#define BPF_F_INDEX_MASK 0xffffffffULL
697#define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300698/* BPF_FUNC_perf_event_output for sk_buff input context. */
699#define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300700
Daniel Borkmann2be7e212017-07-02 02:13:26 +0200701/* Mode for BPF_FUNC_skb_adjust_room helper. */
702enum bpf_adj_room_mode {
Arnaldo Carvalho de Melod62c1d72017-07-31 10:45:07 -0300703 BPF_ADJ_ROOM_NET,
Daniel Borkmann2be7e212017-07-02 02:13:26 +0200704};
705
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300706/* user accessible mirror of in-kernel sk_buff.
707 * new fields can only be added to the end of this structure
708 */
709struct __sk_buff {
710 __u32 len;
711 __u32 pkt_type;
712 __u32 mark;
713 __u32 queue_mapping;
714 __u32 protocol;
715 __u32 vlan_present;
716 __u32 vlan_tci;
717 __u32 vlan_proto;
718 __u32 priority;
719 __u32 ingress_ifindex;
720 __u32 ifindex;
721 __u32 tc_index;
722 __u32 cb[5];
723 __u32 hash;
724 __u32 tc_classid;
725 __u32 data;
726 __u32 data_end;
Daniel Borkmannb1d9fc42017-04-19 23:01:17 +0200727 __u32 napi_id;
John Fastabend69e8cc12017-08-15 22:33:32 -0700728
Daniel Borkmannac299912017-09-25 02:25:52 +0200729 /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */
John Fastabend69e8cc12017-08-15 22:33:32 -0700730 __u32 family;
731 __u32 remote_ip4; /* Stored in network byte order */
732 __u32 local_ip4; /* Stored in network byte order */
733 __u32 remote_ip6[4]; /* Stored in network byte order */
734 __u32 local_ip6[4]; /* Stored in network byte order */
735 __u32 remote_port; /* Stored in network byte order */
736 __u32 local_port; /* stored in host byte order */
Daniel Borkmannac299912017-09-25 02:25:52 +0200737 /* ... here. */
738
739 __u32 data_meta;
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300740};
741
742struct bpf_tunnel_key {
743 __u32 tunnel_id;
744 union {
745 __u32 remote_ipv4;
746 __u32 remote_ipv6[4];
747 };
748 __u8 tunnel_tos;
749 __u8 tunnel_ttl;
750 __u16 tunnel_ext;
751 __u32 tunnel_label;
752};
753
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800754/* Generic BPF return codes which all BPF program types may support.
755 * The values are binary compatible with their TC_ACT_* counter-part to
756 * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
757 * programs.
758 *
759 * XDP is handled seprately, see XDP_*.
760 */
761enum bpf_ret_code {
762 BPF_OK = 0,
763 /* 1 reserved */
764 BPF_DROP = 2,
765 /* 3-6 reserved */
766 BPF_REDIRECT = 7,
767 /* >127 are reserved for prog type specific return codes */
768};
769
770struct bpf_sock {
771 __u32 bound_dev_if;
772 __u32 family;
773 __u32 type;
774 __u32 protocol;
Daniel Borkmannac299912017-09-25 02:25:52 +0200775 __u32 mark;
776 __u32 priority;
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800777};
778
779#define XDP_PACKET_HEADROOM 256
780
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300781/* User return codes for XDP prog type.
782 * A valid XDP program must return one of these defined values. All other
Daniel Borkmannac299912017-09-25 02:25:52 +0200783 * return codes are reserved for future use. Unknown return codes will
784 * result in packet drops and a warning via bpf_warn_invalid_xdp_action().
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300785 */
786enum xdp_action {
787 XDP_ABORTED = 0,
788 XDP_DROP,
789 XDP_PASS,
790 XDP_TX,
Daniel Borkmannac299912017-09-25 02:25:52 +0200791 XDP_REDIRECT,
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300792};
793
794/* user accessible metadata for XDP packet hook
795 * new fields must be added to the end of this structure
796 */
797struct xdp_md {
798 __u32 data;
799 __u32 data_end;
Daniel Borkmannac299912017-09-25 02:25:52 +0200800 __u32 data_meta;
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300801};
802
John Fastabend69e8cc12017-08-15 22:33:32 -0700803enum sk_action {
804 SK_ABORTED = 0,
805 SK_DROP,
806 SK_REDIRECT,
807};
808
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700809#define BPF_TAG_SIZE 8
810
811struct bpf_prog_info {
812 __u32 type;
813 __u32 id;
814 __u8 tag[BPF_TAG_SIZE];
815 __u32 jited_prog_len;
816 __u32 xlated_prog_len;
817 __aligned_u64 jited_prog_insns;
818 __aligned_u64 xlated_prog_insns;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700819 __u64 load_time; /* ns since boottime */
820 __u32 created_by_uid;
821 __u32 nr_map_ids;
822 __aligned_u64 map_ids;
823 __u8 name[BPF_OBJ_NAME_LEN];
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700824} __attribute__((aligned(8)));
825
826struct bpf_map_info {
827 __u32 type;
828 __u32 id;
829 __u32 key_size;
830 __u32 value_size;
831 __u32 max_entries;
832 __u32 map_flags;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700833 __u8 name[BPF_OBJ_NAME_LEN];
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700834} __attribute__((aligned(8)));
835
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700836/* User bpf_sock_ops struct to access socket values and specify request ops
837 * and their replies.
Arnaldo Carvalho de Melof1d6cb22017-07-31 10:45:07 -0300838 * Some of this fields are in network (bigendian) byte order and may need
839 * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h).
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700840 * New fields can only be added at the end of this structure
841 */
842struct bpf_sock_ops {
843 __u32 op;
844 union {
845 __u32 reply;
846 __u32 replylong[4];
847 };
848 __u32 family;
Arnaldo Carvalho de Melof1d6cb22017-07-31 10:45:07 -0300849 __u32 remote_ip4; /* Stored in network byte order */
850 __u32 local_ip4; /* Stored in network byte order */
851 __u32 remote_ip6[4]; /* Stored in network byte order */
852 __u32 local_ip6[4]; /* Stored in network byte order */
853 __u32 remote_port; /* Stored in network byte order */
854 __u32 local_port; /* stored in host byte order */
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700855};
856
857/* List of known BPF sock_ops operators.
858 * New entries can only be added at the end
859 */
860enum {
861 BPF_SOCK_OPS_VOID,
862 BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or
863 * -1 if default value should be used
864 */
865 BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized
866 * window (in packets) or -1 if default
867 * value should be used
868 */
869 BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an
870 * active connection is initialized
871 */
872 BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an
873 * active connection is
874 * established
875 */
876 BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a
877 * passive connection is
878 * established
879 */
880 BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control
881 * needs ECN
882 */
883};
884
885#define TCP_BPF_IW 1001 /* Set TCP initial congestion window */
886#define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */
887
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300888#endif /* _UAPI__LINUX_BPF_H__ */