xref: /linux/include/uapi/linux/bpf.h (revision 546ac1ffb70d25b56c1126940e5ec639c4dd7413)
1daedfb22SAlexei Starovoitov /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2daedfb22SAlexei Starovoitov  *
3daedfb22SAlexei Starovoitov  * This program is free software; you can redistribute it and/or
4daedfb22SAlexei Starovoitov  * modify it under the terms of version 2 of the GNU General Public
5daedfb22SAlexei Starovoitov  * License as published by the Free Software Foundation.
6daedfb22SAlexei Starovoitov  */
7daedfb22SAlexei Starovoitov #ifndef _UAPI__LINUX_BPF_H__
8daedfb22SAlexei Starovoitov #define _UAPI__LINUX_BPF_H__
9daedfb22SAlexei Starovoitov 
10daedfb22SAlexei Starovoitov #include <linux/types.h>
11c15952dcSAlexei Starovoitov #include <linux/bpf_common.h>
12daedfb22SAlexei Starovoitov 
13daedfb22SAlexei Starovoitov /* Extended instruction set based on top of classic BPF */
14daedfb22SAlexei Starovoitov 
15daedfb22SAlexei Starovoitov /* instruction classes */
16daedfb22SAlexei Starovoitov #define BPF_ALU64	0x07	/* alu mode in double word width */
17daedfb22SAlexei Starovoitov 
18daedfb22SAlexei Starovoitov /* ld/ldx fields */
19daedfb22SAlexei Starovoitov #define BPF_DW		0x18	/* double word */
20daedfb22SAlexei Starovoitov #define BPF_XADD	0xc0	/* exclusive add */
21daedfb22SAlexei Starovoitov 
22daedfb22SAlexei Starovoitov /* alu/jmp fields */
23daedfb22SAlexei Starovoitov #define BPF_MOV		0xb0	/* mov reg to reg */
24daedfb22SAlexei Starovoitov #define BPF_ARSH	0xc0	/* sign extending arithmetic shift right */
25daedfb22SAlexei Starovoitov 
26daedfb22SAlexei Starovoitov /* change endianness of a register */
27daedfb22SAlexei Starovoitov #define BPF_END		0xd0	/* flags for endianness conversion: */
28daedfb22SAlexei Starovoitov #define BPF_TO_LE	0x00	/* convert to little-endian */
29daedfb22SAlexei Starovoitov #define BPF_TO_BE	0x08	/* convert to big-endian */
30daedfb22SAlexei Starovoitov #define BPF_FROM_LE	BPF_TO_LE
31daedfb22SAlexei Starovoitov #define BPF_FROM_BE	BPF_TO_BE
32daedfb22SAlexei Starovoitov 
33daedfb22SAlexei Starovoitov #define BPF_JNE		0x50	/* jump != */
34daedfb22SAlexei Starovoitov #define BPF_JSGT	0x60	/* SGT is signed '>', GT in x86 */
35daedfb22SAlexei Starovoitov #define BPF_JSGE	0x70	/* SGE is signed '>=', GE in x86 */
36daedfb22SAlexei Starovoitov #define BPF_CALL	0x80	/* function call */
37daedfb22SAlexei Starovoitov #define BPF_EXIT	0x90	/* function return */
38daedfb22SAlexei Starovoitov 
39daedfb22SAlexei Starovoitov /* Register numbers */
40daedfb22SAlexei Starovoitov enum {
41daedfb22SAlexei Starovoitov 	BPF_REG_0 = 0,
42daedfb22SAlexei Starovoitov 	BPF_REG_1,
43daedfb22SAlexei Starovoitov 	BPF_REG_2,
44daedfb22SAlexei Starovoitov 	BPF_REG_3,
45daedfb22SAlexei Starovoitov 	BPF_REG_4,
46daedfb22SAlexei Starovoitov 	BPF_REG_5,
47daedfb22SAlexei Starovoitov 	BPF_REG_6,
48daedfb22SAlexei Starovoitov 	BPF_REG_7,
49daedfb22SAlexei Starovoitov 	BPF_REG_8,
50daedfb22SAlexei Starovoitov 	BPF_REG_9,
51daedfb22SAlexei Starovoitov 	BPF_REG_10,
52daedfb22SAlexei Starovoitov 	__MAX_BPF_REG,
53daedfb22SAlexei Starovoitov };
54daedfb22SAlexei Starovoitov 
55daedfb22SAlexei Starovoitov /* BPF has 10 general purpose 64-bit registers and stack frame. */
56daedfb22SAlexei Starovoitov #define MAX_BPF_REG	__MAX_BPF_REG
57daedfb22SAlexei Starovoitov 
58daedfb22SAlexei Starovoitov struct bpf_insn {
59daedfb22SAlexei Starovoitov 	__u8	code;		/* opcode */
60daedfb22SAlexei Starovoitov 	__u8	dst_reg:4;	/* dest register */
61daedfb22SAlexei Starovoitov 	__u8	src_reg:4;	/* source register */
62daedfb22SAlexei Starovoitov 	__s16	off;		/* signed offset */
63daedfb22SAlexei Starovoitov 	__s32	imm;		/* signed immediate constant */
64daedfb22SAlexei Starovoitov };
65daedfb22SAlexei Starovoitov 
66b95a5c4dSDaniel Mack /* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */
67b95a5c4dSDaniel Mack struct bpf_lpm_trie_key {
68b95a5c4dSDaniel Mack 	__u32	prefixlen;	/* up to 32 for AF_INET, 128 for AF_INET6 */
69b95a5c4dSDaniel Mack 	__u8	data[0];	/* Arbitrary size */
70b95a5c4dSDaniel Mack };
71b95a5c4dSDaniel Mack 
72b2197755SDaniel Borkmann /* BPF syscall commands, see bpf(2) man-page for details. */
7399c55f7dSAlexei Starovoitov enum bpf_cmd {
7499c55f7dSAlexei Starovoitov 	BPF_MAP_CREATE,
75db20fd2bSAlexei Starovoitov 	BPF_MAP_LOOKUP_ELEM,
76db20fd2bSAlexei Starovoitov 	BPF_MAP_UPDATE_ELEM,
77db20fd2bSAlexei Starovoitov 	BPF_MAP_DELETE_ELEM,
78db20fd2bSAlexei Starovoitov 	BPF_MAP_GET_NEXT_KEY,
7909756af4SAlexei Starovoitov 	BPF_PROG_LOAD,
80b2197755SDaniel Borkmann 	BPF_OBJ_PIN,
81b2197755SDaniel Borkmann 	BPF_OBJ_GET,
82f4324551SDaniel Mack 	BPF_PROG_ATTACH,
83f4324551SDaniel Mack 	BPF_PROG_DETACH,
841cf1cae9SAlexei Starovoitov 	BPF_PROG_TEST_RUN,
8534ad5580SMartin KaFai Lau 	BPF_PROG_GET_NEXT_ID,
8634ad5580SMartin KaFai Lau 	BPF_MAP_GET_NEXT_ID,
87b16d9aa4SMartin KaFai Lau 	BPF_PROG_GET_FD_BY_ID,
88bd5f5f4eSMartin KaFai Lau 	BPF_MAP_GET_FD_BY_ID,
891e270976SMartin KaFai Lau 	BPF_OBJ_GET_INFO_BY_FD,
9099c55f7dSAlexei Starovoitov };
9199c55f7dSAlexei Starovoitov 
9299c55f7dSAlexei Starovoitov enum bpf_map_type {
9399c55f7dSAlexei Starovoitov 	BPF_MAP_TYPE_UNSPEC,
940f8e4bd8SAlexei Starovoitov 	BPF_MAP_TYPE_HASH,
9528fbcfa0SAlexei Starovoitov 	BPF_MAP_TYPE_ARRAY,
9604fd61abSAlexei Starovoitov 	BPF_MAP_TYPE_PROG_ARRAY,
97ea317b26SKaixu Xia 	BPF_MAP_TYPE_PERF_EVENT_ARRAY,
98824bd0ceSAlexei Starovoitov 	BPF_MAP_TYPE_PERCPU_HASH,
99a10423b8SAlexei Starovoitov 	BPF_MAP_TYPE_PERCPU_ARRAY,
100d5a3b1f6SAlexei Starovoitov 	BPF_MAP_TYPE_STACK_TRACE,
1014ed8ec52SMartin KaFai Lau 	BPF_MAP_TYPE_CGROUP_ARRAY,
10229ba732aSMartin KaFai Lau 	BPF_MAP_TYPE_LRU_HASH,
1038f844938SMartin KaFai Lau 	BPF_MAP_TYPE_LRU_PERCPU_HASH,
104b95a5c4dSDaniel Mack 	BPF_MAP_TYPE_LPM_TRIE,
10556f668dfSMartin KaFai Lau 	BPF_MAP_TYPE_ARRAY_OF_MAPS,
106bcc6b1b7SMartin KaFai Lau 	BPF_MAP_TYPE_HASH_OF_MAPS,
107*546ac1ffSJohn Fastabend 	BPF_MAP_TYPE_DEVMAP,
10899c55f7dSAlexei Starovoitov };
10999c55f7dSAlexei Starovoitov 
11009756af4SAlexei Starovoitov enum bpf_prog_type {
11109756af4SAlexei Starovoitov 	BPF_PROG_TYPE_UNSPEC,
112ddd872bcSAlexei Starovoitov 	BPF_PROG_TYPE_SOCKET_FILTER,
1132541517cSAlexei Starovoitov 	BPF_PROG_TYPE_KPROBE,
11496be4325SDaniel Borkmann 	BPF_PROG_TYPE_SCHED_CLS,
11594caee8cSDaniel Borkmann 	BPF_PROG_TYPE_SCHED_ACT,
11698b5c2c6SAlexei Starovoitov 	BPF_PROG_TYPE_TRACEPOINT,
1176a773a15SBrenden Blanco 	BPF_PROG_TYPE_XDP,
1180515e599SAlexei Starovoitov 	BPF_PROG_TYPE_PERF_EVENT,
1190e33661dSDaniel Mack 	BPF_PROG_TYPE_CGROUP_SKB,
12061023658SDavid Ahern 	BPF_PROG_TYPE_CGROUP_SOCK,
1213a0af8fdSThomas Graf 	BPF_PROG_TYPE_LWT_IN,
1223a0af8fdSThomas Graf 	BPF_PROG_TYPE_LWT_OUT,
1233a0af8fdSThomas Graf 	BPF_PROG_TYPE_LWT_XMIT,
12440304b2aSLawrence Brakmo 	BPF_PROG_TYPE_SOCK_OPS,
12509756af4SAlexei Starovoitov };
12609756af4SAlexei Starovoitov 
1270e33661dSDaniel Mack enum bpf_attach_type {
1280e33661dSDaniel Mack 	BPF_CGROUP_INET_INGRESS,
1290e33661dSDaniel Mack 	BPF_CGROUP_INET_EGRESS,
13061023658SDavid Ahern 	BPF_CGROUP_INET_SOCK_CREATE,
13140304b2aSLawrence Brakmo 	BPF_CGROUP_SOCK_OPS,
1320e33661dSDaniel Mack 	__MAX_BPF_ATTACH_TYPE
1330e33661dSDaniel Mack };
1340e33661dSDaniel Mack 
1350e33661dSDaniel Mack #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
1360e33661dSDaniel Mack 
1377f677633SAlexei Starovoitov /* If BPF_F_ALLOW_OVERRIDE flag is used in BPF_PROG_ATTACH command
1387f677633SAlexei Starovoitov  * to the given target_fd cgroup the descendent cgroup will be able to
1397f677633SAlexei Starovoitov  * override effective bpf program that was inherited from this cgroup
1407f677633SAlexei Starovoitov  */
1417f677633SAlexei Starovoitov #define BPF_F_ALLOW_OVERRIDE	(1U << 0)
1427f677633SAlexei Starovoitov 
143e07b98d9SDavid S. Miller /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
144e07b98d9SDavid S. Miller  * verifier will perform strict alignment checking as if the kernel
145e07b98d9SDavid S. Miller  * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set,
146e07b98d9SDavid S. Miller  * and NET_IP_ALIGN defined to 2.
147e07b98d9SDavid S. Miller  */
148e07b98d9SDavid S. Miller #define BPF_F_STRICT_ALIGNMENT	(1U << 0)
149e07b98d9SDavid S. Miller 
150f1a66f85SDaniel Borkmann #define BPF_PSEUDO_MAP_FD	1
151f1a66f85SDaniel Borkmann 
1523274f520SAlexei Starovoitov /* flags for BPF_MAP_UPDATE_ELEM command */
1533274f520SAlexei Starovoitov #define BPF_ANY		0 /* create new element or update existing */
1543274f520SAlexei Starovoitov #define BPF_NOEXIST	1 /* create new element if it didn't exist */
1553274f520SAlexei Starovoitov #define BPF_EXIST	2 /* update existing element */
1563274f520SAlexei Starovoitov 
1576c905981SAlexei Starovoitov #define BPF_F_NO_PREALLOC	(1U << 0)
15829ba732aSMartin KaFai Lau /* Instead of having one common LRU list in the
1598f844938SMartin KaFai Lau  * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
16029ba732aSMartin KaFai Lau  * which can scale and perform better.
16129ba732aSMartin KaFai Lau  * Note, the LRU nodes (including free nodes) cannot be moved
16229ba732aSMartin KaFai Lau  * across different LRU lists.
16329ba732aSMartin KaFai Lau  */
16429ba732aSMartin KaFai Lau #define BPF_F_NO_COMMON_LRU	(1U << 1)
1656c905981SAlexei Starovoitov 
16699c55f7dSAlexei Starovoitov union bpf_attr {
16799c55f7dSAlexei Starovoitov 	struct { /* anonymous struct used by BPF_MAP_CREATE command */
16899c55f7dSAlexei Starovoitov 		__u32	map_type;	/* one of enum bpf_map_type */
16999c55f7dSAlexei Starovoitov 		__u32	key_size;	/* size of key in bytes */
17099c55f7dSAlexei Starovoitov 		__u32	value_size;	/* size of value in bytes */
17199c55f7dSAlexei Starovoitov 		__u32	max_entries;	/* max number of entries in a map */
1726c905981SAlexei Starovoitov 		__u32	map_flags;	/* prealloc or not */
17356f668dfSMartin KaFai Lau 		__u32	inner_map_fd;	/* fd pointing to the inner map */
17499c55f7dSAlexei Starovoitov 	};
175db20fd2bSAlexei Starovoitov 
176db20fd2bSAlexei Starovoitov 	struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
177db20fd2bSAlexei Starovoitov 		__u32		map_fd;
178db20fd2bSAlexei Starovoitov 		__aligned_u64	key;
179db20fd2bSAlexei Starovoitov 		union {
180db20fd2bSAlexei Starovoitov 			__aligned_u64 value;
181db20fd2bSAlexei Starovoitov 			__aligned_u64 next_key;
182db20fd2bSAlexei Starovoitov 		};
1833274f520SAlexei Starovoitov 		__u64		flags;
184db20fd2bSAlexei Starovoitov 	};
18509756af4SAlexei Starovoitov 
18609756af4SAlexei Starovoitov 	struct { /* anonymous struct used by BPF_PROG_LOAD command */
18709756af4SAlexei Starovoitov 		__u32		prog_type;	/* one of enum bpf_prog_type */
18809756af4SAlexei Starovoitov 		__u32		insn_cnt;
18909756af4SAlexei Starovoitov 		__aligned_u64	insns;
19009756af4SAlexei Starovoitov 		__aligned_u64	license;
191cbd35700SAlexei Starovoitov 		__u32		log_level;	/* verbosity level of verifier */
192cbd35700SAlexei Starovoitov 		__u32		log_size;	/* size of user buffer */
193cbd35700SAlexei Starovoitov 		__aligned_u64	log_buf;	/* user supplied buffer */
1942541517cSAlexei Starovoitov 		__u32		kern_version;	/* checked when prog_type=kprobe */
195e07b98d9SDavid S. Miller 		__u32		prog_flags;
19609756af4SAlexei Starovoitov 	};
197b2197755SDaniel Borkmann 
198b2197755SDaniel Borkmann 	struct { /* anonymous struct used by BPF_OBJ_* commands */
199b2197755SDaniel Borkmann 		__aligned_u64	pathname;
200b2197755SDaniel Borkmann 		__u32		bpf_fd;
201b2197755SDaniel Borkmann 	};
202f4324551SDaniel Mack 
203f4324551SDaniel Mack 	struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
204f4324551SDaniel Mack 		__u32		target_fd;	/* container object to attach to */
205f4324551SDaniel Mack 		__u32		attach_bpf_fd;	/* eBPF program to attach */
206f4324551SDaniel Mack 		__u32		attach_type;
2077f677633SAlexei Starovoitov 		__u32		attach_flags;
208f4324551SDaniel Mack 	};
2091cf1cae9SAlexei Starovoitov 
2101cf1cae9SAlexei Starovoitov 	struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
2111cf1cae9SAlexei Starovoitov 		__u32		prog_fd;
2121cf1cae9SAlexei Starovoitov 		__u32		retval;
2131cf1cae9SAlexei Starovoitov 		__u32		data_size_in;
2141cf1cae9SAlexei Starovoitov 		__u32		data_size_out;
2151cf1cae9SAlexei Starovoitov 		__aligned_u64	data_in;
2161cf1cae9SAlexei Starovoitov 		__aligned_u64	data_out;
2171cf1cae9SAlexei Starovoitov 		__u32		repeat;
2181cf1cae9SAlexei Starovoitov 		__u32		duration;
2191cf1cae9SAlexei Starovoitov 	} test;
22034ad5580SMartin KaFai Lau 
221b16d9aa4SMartin KaFai Lau 	struct { /* anonymous struct used by BPF_*_GET_*_ID */
222b16d9aa4SMartin KaFai Lau 		union {
22334ad5580SMartin KaFai Lau 			__u32		start_id;
224b16d9aa4SMartin KaFai Lau 			__u32		prog_id;
225bd5f5f4eSMartin KaFai Lau 			__u32		map_id;
226b16d9aa4SMartin KaFai Lau 		};
22734ad5580SMartin KaFai Lau 		__u32		next_id;
22834ad5580SMartin KaFai Lau 	};
2291e270976SMartin KaFai Lau 
2301e270976SMartin KaFai Lau 	struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
2311e270976SMartin KaFai Lau 		__u32		bpf_fd;
2321e270976SMartin KaFai Lau 		__u32		info_len;
2331e270976SMartin KaFai Lau 		__aligned_u64	info;
2341e270976SMartin KaFai Lau 	} info;
23599c55f7dSAlexei Starovoitov } __attribute__((aligned(8)));
23699c55f7dSAlexei Starovoitov 
237ebb676daSThomas Graf /* BPF helper function descriptions:
238ebb676daSThomas Graf  *
239ebb676daSThomas Graf  * void *bpf_map_lookup_elem(&map, &key)
240ebb676daSThomas Graf  *     Return: Map value or NULL
241ebb676daSThomas Graf  *
242ebb676daSThomas Graf  * int bpf_map_update_elem(&map, &key, &value, flags)
243ebb676daSThomas Graf  *     Return: 0 on success or negative error
244ebb676daSThomas Graf  *
245ebb676daSThomas Graf  * int bpf_map_delete_elem(&map, &key)
246ebb676daSThomas Graf  *     Return: 0 on success or negative error
247ebb676daSThomas Graf  *
248ebb676daSThomas Graf  * int bpf_probe_read(void *dst, int size, void *src)
249ebb676daSThomas Graf  *     Return: 0 on success or negative error
250ebb676daSThomas Graf  *
251ebb676daSThomas Graf  * u64 bpf_ktime_get_ns(void)
252ebb676daSThomas Graf  *     Return: current ktime
253ebb676daSThomas Graf  *
254ebb676daSThomas Graf  * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
255ebb676daSThomas Graf  *     Return: length of buffer written or negative error
256ebb676daSThomas Graf  *
257ebb676daSThomas Graf  * u32 bpf_prandom_u32(void)
258ebb676daSThomas Graf  *     Return: random value
259ebb676daSThomas Graf  *
260ebb676daSThomas Graf  * u32 bpf_raw_smp_processor_id(void)
261ebb676daSThomas Graf  *     Return: SMP processor ID
262ebb676daSThomas Graf  *
263ebb676daSThomas Graf  * int bpf_skb_store_bytes(skb, offset, from, len, flags)
264ebb676daSThomas Graf  *     store bytes into packet
26591bc4822SAlexei Starovoitov  *     @skb: pointer to skb
266a166151cSAlexei Starovoitov  *     @offset: offset within packet from skb->mac_header
26791bc4822SAlexei Starovoitov  *     @from: pointer where to copy bytes from
26891bc4822SAlexei Starovoitov  *     @len: number of bytes to store into packet
26991bc4822SAlexei Starovoitov  *     @flags: bit 0 - if true, recompute skb->csum
27091bc4822SAlexei Starovoitov  *             other bits - reserved
271ebb676daSThomas Graf  *     Return: 0 on success or negative error
272ebb676daSThomas Graf  *
273ebb676daSThomas Graf  * int bpf_l3_csum_replace(skb, offset, from, to, flags)
274ebb676daSThomas Graf  *     recompute IP checksum
27591bc4822SAlexei Starovoitov  *     @skb: pointer to skb
27691bc4822SAlexei Starovoitov  *     @offset: offset within packet where IP checksum is located
27791bc4822SAlexei Starovoitov  *     @from: old value of header field
27891bc4822SAlexei Starovoitov  *     @to: new value of header field
27991bc4822SAlexei Starovoitov  *     @flags: bits 0-3 - size of header field
28091bc4822SAlexei Starovoitov  *             other bits - reserved
281ebb676daSThomas Graf  *     Return: 0 on success or negative error
282ebb676daSThomas Graf  *
283ebb676daSThomas Graf  * int bpf_l4_csum_replace(skb, offset, from, to, flags)
284ebb676daSThomas Graf  *     recompute TCP/UDP checksum
28591bc4822SAlexei Starovoitov  *     @skb: pointer to skb
28691bc4822SAlexei Starovoitov  *     @offset: offset within packet where TCP/UDP checksum is located
28791bc4822SAlexei Starovoitov  *     @from: old value of header field
28891bc4822SAlexei Starovoitov  *     @to: new value of header field
28991bc4822SAlexei Starovoitov  *     @flags: bits 0-3 - size of header field
29091bc4822SAlexei Starovoitov  *             bit 4 - is pseudo header
29191bc4822SAlexei Starovoitov  *             other bits - reserved
292ebb676daSThomas Graf  *     Return: 0 on success or negative error
293ebb676daSThomas Graf  *
294ebb676daSThomas Graf  * int bpf_tail_call(ctx, prog_array_map, index)
295ebb676daSThomas Graf  *     jump into another BPF program
29604fd61abSAlexei Starovoitov  *     @ctx: context pointer passed to next program
29704fd61abSAlexei Starovoitov  *     @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
29804fd61abSAlexei Starovoitov  *     @index: index inside array that selects specific program to run
299ebb676daSThomas Graf  *     Return: 0 on success or negative error
300ebb676daSThomas Graf  *
301ebb676daSThomas Graf  * int bpf_clone_redirect(skb, ifindex, flags)
302ebb676daSThomas Graf  *     redirect to another netdev
3033896d655SAlexei Starovoitov  *     @skb: pointer to skb
3043896d655SAlexei Starovoitov  *     @ifindex: ifindex of the net device
3053896d655SAlexei Starovoitov  *     @flags: bit 0 - if set, redirect to ingress instead of egress
3063896d655SAlexei Starovoitov  *             other bits - reserved
307ebb676daSThomas Graf  *     Return: 0 on success or negative error
308ebb676daSThomas Graf  *
309ffeedafbSAlexei Starovoitov  * u64 bpf_get_current_pid_tgid(void)
310ffeedafbSAlexei Starovoitov  *     Return: current->tgid << 32 | current->pid
311ebb676daSThomas Graf  *
312ffeedafbSAlexei Starovoitov  * u64 bpf_get_current_uid_gid(void)
313ffeedafbSAlexei Starovoitov  *     Return: current_gid << 32 | current_uid
314ebb676daSThomas Graf  *
315ebb676daSThomas Graf  * int bpf_get_current_comm(char *buf, int size_of_buf)
316ffeedafbSAlexei Starovoitov  *     stores current->comm into buf
317ebb676daSThomas Graf  *     Return: 0 on success or negative error
318ebb676daSThomas Graf  *
319ebb676daSThomas Graf  * u32 bpf_get_cgroup_classid(skb)
320ebb676daSThomas Graf  *     retrieve a proc's classid
3218d20aabeSDaniel Borkmann  *     @skb: pointer to skb
3228d20aabeSDaniel Borkmann  *     Return: classid if != 0
323ebb676daSThomas Graf  *
324ebb676daSThomas Graf  * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
325ebb676daSThomas Graf  *     Return: 0 on success or negative error
326ebb676daSThomas Graf  *
327ebb676daSThomas Graf  * int bpf_skb_vlan_pop(skb)
328ebb676daSThomas Graf  *     Return: 0 on success or negative error
329ebb676daSThomas Graf  *
330ebb676daSThomas Graf  * int bpf_skb_get_tunnel_key(skb, key, size, flags)
331ebb676daSThomas Graf  * int bpf_skb_set_tunnel_key(skb, key, size, flags)
332d3aa45ceSAlexei Starovoitov  *     retrieve or populate tunnel metadata
333d3aa45ceSAlexei Starovoitov  *     @skb: pointer to skb
334d3aa45ceSAlexei Starovoitov  *     @key: pointer to 'struct bpf_tunnel_key'
335d3aa45ceSAlexei Starovoitov  *     @size: size of 'struct bpf_tunnel_key'
336d3aa45ceSAlexei Starovoitov  *     @flags: room for future extensions
337ebb676daSThomas Graf  *     Return: 0 on success or negative error
338ebb676daSThomas Graf  *
339b7d3ed5bSTeng Qin  * u64 bpf_perf_event_read(map, flags)
340b7d3ed5bSTeng Qin  *     read perf event counter value
341b7d3ed5bSTeng Qin  *     @map: pointer to perf_event_array map
342b7d3ed5bSTeng Qin  *     @flags: index of event in the map or bitmask flags
343b7d3ed5bSTeng Qin  *     Return: value of perf event counter read or error code
344ebb676daSThomas Graf  *
345ebb676daSThomas Graf  * int bpf_redirect(ifindex, flags)
346ebb676daSThomas Graf  *     redirect to another netdev
34727b29f63SAlexei Starovoitov  *     @ifindex: ifindex of the net device
34827b29f63SAlexei Starovoitov  *     @flags: bit 0 - if set, redirect to ingress instead of egress
34927b29f63SAlexei Starovoitov  *             other bits - reserved
35027b29f63SAlexei Starovoitov  *     Return: TC_ACT_REDIRECT
351ebb676daSThomas Graf  *
352ebb676daSThomas Graf  * u32 bpf_get_route_realm(skb)
353ebb676daSThomas Graf  *     retrieve a dst's tclassid
354c46646d0SDaniel Borkmann  *     @skb: pointer to skb
355c46646d0SDaniel Borkmann  *     Return: realm if != 0
356ebb676daSThomas Graf  *
357b7d3ed5bSTeng Qin  * int bpf_perf_event_output(ctx, map, flags, data, size)
358ebb676daSThomas Graf  *     output perf raw sample
359a43eec30SAlexei Starovoitov  *     @ctx: struct pt_regs*
360a43eec30SAlexei Starovoitov  *     @map: pointer to perf_event_array map
361b7d3ed5bSTeng Qin  *     @flags: index of event in the map or bitmask flags
362a43eec30SAlexei Starovoitov  *     @data: data on stack to be output as raw data
363a43eec30SAlexei Starovoitov  *     @size: size of data
364ebb676daSThomas Graf  *     Return: 0 on success or negative error
365ebb676daSThomas Graf  *
366ebb676daSThomas Graf  * int bpf_get_stackid(ctx, map, flags)
367ebb676daSThomas Graf  *     walk user or kernel stack and return id
368d5a3b1f6SAlexei Starovoitov  *     @ctx: struct pt_regs*
369d5a3b1f6SAlexei Starovoitov  *     @map: pointer to stack_trace map
370d5a3b1f6SAlexei Starovoitov  *     @flags: bits 0-7 - numer of stack frames to skip
371d5a3b1f6SAlexei Starovoitov  *             bit 8 - collect user stack instead of kernel
372d5a3b1f6SAlexei Starovoitov  *             bit 9 - compare stacks by hash only
373d5a3b1f6SAlexei Starovoitov  *             bit 10 - if two different stacks hash into the same stackid
374d5a3b1f6SAlexei Starovoitov  *                      discard old
375d5a3b1f6SAlexei Starovoitov  *             other bits - reserved
376d5a3b1f6SAlexei Starovoitov  *     Return: >= 0 stackid on success or negative error
377ebb676daSThomas Graf  *
378ebb676daSThomas Graf  * s64 bpf_csum_diff(from, from_size, to, to_size, seed)
379ebb676daSThomas Graf  *     calculate csum diff
3807d672345SDaniel Borkmann  *     @from: raw from buffer
3817d672345SDaniel Borkmann  *     @from_size: length of from buffer
3827d672345SDaniel Borkmann  *     @to: raw to buffer
3837d672345SDaniel Borkmann  *     @to_size: length of to buffer
3847d672345SDaniel Borkmann  *     @seed: optional seed
385ebb676daSThomas Graf  *     Return: csum result or negative error code
386ebb676daSThomas Graf  *
387ebb676daSThomas Graf  * int bpf_skb_get_tunnel_opt(skb, opt, size)
388ebb676daSThomas Graf  *     retrieve tunnel options metadata
38914ca0751SDaniel Borkmann  *     @skb: pointer to skb
39014ca0751SDaniel Borkmann  *     @opt: pointer to raw tunnel option data
39114ca0751SDaniel Borkmann  *     @size: size of @opt
392ebb676daSThomas Graf  *     Return: option size
393ebb676daSThomas Graf  *
394ebb676daSThomas Graf  * int bpf_skb_set_tunnel_opt(skb, opt, size)
395ebb676daSThomas Graf  *     populate tunnel options metadata
396ebb676daSThomas Graf  *     @skb: pointer to skb
397ebb676daSThomas Graf  *     @opt: pointer to raw tunnel option data
398ebb676daSThomas Graf  *     @size: size of @opt
399ebb676daSThomas Graf  *     Return: 0 on success or negative error
400ebb676daSThomas Graf  *
401ebb676daSThomas Graf  * int bpf_skb_change_proto(skb, proto, flags)
402ebb676daSThomas Graf  *     Change protocol of the skb. Currently supported is v4 -> v6,
403ebb676daSThomas Graf  *     v6 -> v4 transitions. The helper will also resize the skb. eBPF
404ebb676daSThomas Graf  *     program is expected to fill the new headers via skb_store_bytes
405ebb676daSThomas Graf  *     and lX_csum_replace.
4066578171aSDaniel Borkmann  *     @skb: pointer to skb
4076578171aSDaniel Borkmann  *     @proto: new skb->protocol type
4086578171aSDaniel Borkmann  *     @flags: reserved
4096578171aSDaniel Borkmann  *     Return: 0 on success or negative error
410ebb676daSThomas Graf  *
411ebb676daSThomas Graf  * int bpf_skb_change_type(skb, type)
412d2485c42SDaniel Borkmann  *     Change packet type of skb.
413d2485c42SDaniel Borkmann  *     @skb: pointer to skb
414d2485c42SDaniel Borkmann  *     @type: new skb->pkt_type type
415d2485c42SDaniel Borkmann  *     Return: 0 on success or negative error
416ebb676daSThomas Graf  *
417ebb676daSThomas Graf  * int bpf_skb_under_cgroup(skb, map, index)
418ebb676daSThomas Graf  *     Check cgroup2 membership of skb
4194a482f34SMartin KaFai Lau  *     @skb: pointer to skb
4204a482f34SMartin KaFai Lau  *     @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
4214a482f34SMartin KaFai Lau  *     @index: index of the cgroup in the bpf_map
4224a482f34SMartin KaFai Lau  *     Return:
4234a482f34SMartin KaFai Lau  *       == 0 skb failed the cgroup2 descendant test
4244a482f34SMartin KaFai Lau  *       == 1 skb succeeded the cgroup2 descendant test
4254a482f34SMartin KaFai Lau  *        < 0 error
426ebb676daSThomas Graf  *
427ebb676daSThomas Graf  * u32 bpf_get_hash_recalc(skb)
42813c5c240SDaniel Borkmann  *     Retrieve and possibly recalculate skb->hash.
42913c5c240SDaniel Borkmann  *     @skb: pointer to skb
43013c5c240SDaniel Borkmann  *     Return: hash
431ebb676daSThomas Graf  *
432606274c5SAlexei Starovoitov  * u64 bpf_get_current_task(void)
433606274c5SAlexei Starovoitov  *     Returns current task_struct
434606274c5SAlexei Starovoitov  *     Return: current
435ebb676daSThomas Graf  *
436ebb676daSThomas Graf  * int bpf_probe_write_user(void *dst, void *src, int len)
43796ae5227SSargun Dhillon  *     safely attempt to write to a location
43896ae5227SSargun Dhillon  *     @dst: destination address in userspace
43996ae5227SSargun Dhillon  *     @src: source address on stack
44096ae5227SSargun Dhillon  *     @len: number of bytes to copy
44196ae5227SSargun Dhillon  *     Return: 0 on success or negative error
442ebb676daSThomas Graf  *
443ebb676daSThomas Graf  * int bpf_current_task_under_cgroup(map, index)
444ebb676daSThomas Graf  *     Check cgroup2 membership of current task
44560d20f91SSargun Dhillon  *     @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
44660d20f91SSargun Dhillon  *     @index: index of the cgroup in the bpf_map
44760d20f91SSargun Dhillon  *     Return:
44860d20f91SSargun Dhillon  *       == 0 current failed the cgroup2 descendant test
44960d20f91SSargun Dhillon  *       == 1 current succeeded the cgroup2 descendant test
45060d20f91SSargun Dhillon  *        < 0 error
451ebb676daSThomas Graf  *
452ebb676daSThomas Graf  * int bpf_skb_change_tail(skb, len, flags)
453ebb676daSThomas Graf  *     The helper will resize the skb to the given new size, to be used f.e.
454ebb676daSThomas Graf  *     with control messages.
4555293efe6SDaniel Borkmann  *     @skb: pointer to skb
4565293efe6SDaniel Borkmann  *     @len: new skb length
4575293efe6SDaniel Borkmann  *     @flags: reserved
4585293efe6SDaniel Borkmann  *     Return: 0 on success or negative error
459ebb676daSThomas Graf  *
460ebb676daSThomas Graf  * int bpf_skb_pull_data(skb, len)
461ebb676daSThomas Graf  *     The helper will pull in non-linear data in case the skb is non-linear
462ebb676daSThomas Graf  *     and not all of len are part of the linear section. Only needed for
463ebb676daSThomas Graf  *     read/write with direct packet access.
46436bbef52SDaniel Borkmann  *     @skb: pointer to skb
46536bbef52SDaniel Borkmann  *     @len: len to make read/writeable
46636bbef52SDaniel Borkmann  *     Return: 0 on success or negative error
467ebb676daSThomas Graf  *
468ebb676daSThomas Graf  * s64 bpf_csum_update(skb, csum)
46936bbef52SDaniel Borkmann  *     Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
47036bbef52SDaniel Borkmann  *     @skb: pointer to skb
47136bbef52SDaniel Borkmann  *     @csum: csum to add
47236bbef52SDaniel Borkmann  *     Return: csum on success or negative error
473ebb676daSThomas Graf  *
474ebb676daSThomas Graf  * void bpf_set_hash_invalid(skb)
475ebb676daSThomas Graf  *     Invalidate current skb->hash.
4767a4b28c6SDaniel Borkmann  *     @skb: pointer to skb
477ebb676daSThomas Graf  *
478ebb676daSThomas Graf  * int bpf_get_numa_node_id()
479ebb676daSThomas Graf  *     Return: Id of current NUMA node.
4803a0af8fdSThomas Graf  *
4813a0af8fdSThomas Graf  * int bpf_skb_change_head()
4823a0af8fdSThomas Graf  *     Grows headroom of skb and adjusts MAC header offset accordingly.
4833a0af8fdSThomas Graf  *     Will extends/reallocae as required automatically.
4843a0af8fdSThomas Graf  *     May change skb data pointer and will thus invalidate any check
4853a0af8fdSThomas Graf  *     performed for direct packet access.
4863a0af8fdSThomas Graf  *     @skb: pointer to skb
4873a0af8fdSThomas Graf  *     @len: length of header to be pushed in front
4883a0af8fdSThomas Graf  *     @flags: Flags (unused for now)
4893a0af8fdSThomas Graf  *     Return: 0 on success or negative error
49017bedab2SMartin KaFai Lau  *
49117bedab2SMartin KaFai Lau  * int bpf_xdp_adjust_head(xdp_md, delta)
49217bedab2SMartin KaFai Lau  *     Adjust the xdp_md.data by delta
49317bedab2SMartin KaFai Lau  *     @xdp_md: pointer to xdp_md
49417bedab2SMartin KaFai Lau  *     @delta: An positive/negative integer to be added to xdp_md.data
49517bedab2SMartin KaFai Lau  *     Return: 0 on success or negative on error
496a5e8c070SGianluca Borello  *
497a5e8c070SGianluca Borello  * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
498a5e8c070SGianluca Borello  *     Copy a NUL terminated string from unsafe address. In case the string
499a5e8c070SGianluca Borello  *     length is smaller than size, the target is not padded with further NUL
500a5e8c070SGianluca Borello  *     bytes. In case the string length is larger than size, just count-1
501a5e8c070SGianluca Borello  *     bytes are copied and the last byte is set to NUL.
502a5e8c070SGianluca Borello  *     @dst: destination address
503a5e8c070SGianluca Borello  *     @size: maximum number of bytes to copy, including the trailing NUL
504a5e8c070SGianluca Borello  *     @unsafe_ptr: unsafe address
505a5e8c070SGianluca Borello  *     Return:
506a5e8c070SGianluca Borello  *       > 0 length of the string including the trailing NUL on success
507a5e8c070SGianluca Borello  *       < 0 error
50891b8270fSChenbo Feng  *
5093c60a531SAlexander Alemayhu  * u64 bpf_get_socket_cookie(skb)
51091b8270fSChenbo Feng  *     Get the cookie for the socket stored inside sk_buff.
51191b8270fSChenbo Feng  *     @skb: pointer to skb
51291b8270fSChenbo Feng  *     Return: 8 Bytes non-decreasing number on success or 0 if the socket
51391b8270fSChenbo Feng  *     field is missing inside sk_buff
5146acc5c29SChenbo Feng  *
5156acc5c29SChenbo Feng  * u32 bpf_get_socket_uid(skb)
5166acc5c29SChenbo Feng  *     Get the owner uid of the socket stored inside sk_buff.
5176acc5c29SChenbo Feng  *     @skb: pointer to skb
5185d4e3443SChenbo Feng  *     Return: uid of the socket owner on success or overflowuid if failed.
519ded092cdSDaniel Borkmann  *
520ded092cdSDaniel Borkmann  * u32 bpf_set_hash(skb, hash)
521ded092cdSDaniel Borkmann  *     Set full skb->hash.
522ded092cdSDaniel Borkmann  *     @skb: pointer to skb
523ded092cdSDaniel Borkmann  *     @hash: hash to set
5248c4b4c7eSLawrence Brakmo  *
5258c4b4c7eSLawrence Brakmo  * int bpf_setsockopt(bpf_socket, level, optname, optval, optlen)
5268c4b4c7eSLawrence Brakmo  *     Calls setsockopt. Not all opts are available, only those with
5278c4b4c7eSLawrence Brakmo  *     integer optvals plus TCP_CONGESTION.
5288c4b4c7eSLawrence Brakmo  *     Supported levels: SOL_SOCKET and IPROTO_TCP
5298c4b4c7eSLawrence Brakmo  *     @bpf_socket: pointer to bpf_socket
5308c4b4c7eSLawrence Brakmo  *     @level: SOL_SOCKET or IPROTO_TCP
5318c4b4c7eSLawrence Brakmo  *     @optname: option name
5328c4b4c7eSLawrence Brakmo  *     @optval: pointer to option value
5338c4b4c7eSLawrence Brakmo  *     @optlen: length of optval in byes
5348c4b4c7eSLawrence Brakmo  *     Return: 0 or negative error
5352be7e212SDaniel Borkmann  *
5362be7e212SDaniel Borkmann  * int bpf_skb_adjust_room(skb, len_diff, mode, flags)
5372be7e212SDaniel Borkmann  *     Grow or shrink room in sk_buff.
5382be7e212SDaniel Borkmann  *     @skb: pointer to skb
5392be7e212SDaniel Borkmann  *     @len_diff: (signed) amount of room to grow/shrink
5402be7e212SDaniel Borkmann  *     @mode: operation mode (enum bpf_adj_room_mode)
5412be7e212SDaniel Borkmann  *     @flags: reserved for future use
5422be7e212SDaniel Borkmann  *     Return: 0 on success or negative error code
5437a4b28c6SDaniel Borkmann  */
544ebb676daSThomas Graf #define __BPF_FUNC_MAPPER(FN)		\
545ebb676daSThomas Graf 	FN(unspec),			\
546ebb676daSThomas Graf 	FN(map_lookup_elem),		\
547ebb676daSThomas Graf 	FN(map_update_elem),		\
548ebb676daSThomas Graf 	FN(map_delete_elem),		\
549ebb676daSThomas Graf 	FN(probe_read),			\
550ebb676daSThomas Graf 	FN(ktime_get_ns),		\
551ebb676daSThomas Graf 	FN(trace_printk),		\
552ebb676daSThomas Graf 	FN(get_prandom_u32),		\
553ebb676daSThomas Graf 	FN(get_smp_processor_id),	\
554ebb676daSThomas Graf 	FN(skb_store_bytes),		\
555ebb676daSThomas Graf 	FN(l3_csum_replace),		\
556ebb676daSThomas Graf 	FN(l4_csum_replace),		\
557ebb676daSThomas Graf 	FN(tail_call),			\
558ebb676daSThomas Graf 	FN(clone_redirect),		\
559ebb676daSThomas Graf 	FN(get_current_pid_tgid),	\
560ebb676daSThomas Graf 	FN(get_current_uid_gid),	\
561ebb676daSThomas Graf 	FN(get_current_comm),		\
562ebb676daSThomas Graf 	FN(get_cgroup_classid),		\
563ebb676daSThomas Graf 	FN(skb_vlan_push),		\
564ebb676daSThomas Graf 	FN(skb_vlan_pop),		\
565ebb676daSThomas Graf 	FN(skb_get_tunnel_key),		\
566ebb676daSThomas Graf 	FN(skb_set_tunnel_key),		\
567ebb676daSThomas Graf 	FN(perf_event_read),		\
568ebb676daSThomas Graf 	FN(redirect),			\
569ebb676daSThomas Graf 	FN(get_route_realm),		\
570ebb676daSThomas Graf 	FN(perf_event_output),		\
571ebb676daSThomas Graf 	FN(skb_load_bytes),		\
572ebb676daSThomas Graf 	FN(get_stackid),		\
573ebb676daSThomas Graf 	FN(csum_diff),			\
574ebb676daSThomas Graf 	FN(skb_get_tunnel_opt),		\
575ebb676daSThomas Graf 	FN(skb_set_tunnel_opt),		\
576ebb676daSThomas Graf 	FN(skb_change_proto),		\
577ebb676daSThomas Graf 	FN(skb_change_type),		\
578ebb676daSThomas Graf 	FN(skb_under_cgroup),		\
579ebb676daSThomas Graf 	FN(get_hash_recalc),		\
580ebb676daSThomas Graf 	FN(get_current_task),		\
581ebb676daSThomas Graf 	FN(probe_write_user),		\
582ebb676daSThomas Graf 	FN(current_task_under_cgroup),	\
583ebb676daSThomas Graf 	FN(skb_change_tail),		\
584ebb676daSThomas Graf 	FN(skb_pull_data),		\
585ebb676daSThomas Graf 	FN(csum_update),		\
586ebb676daSThomas Graf 	FN(set_hash_invalid),		\
5873a0af8fdSThomas Graf 	FN(get_numa_node_id),		\
58817bedab2SMartin KaFai Lau 	FN(skb_change_head),		\
589a5e8c070SGianluca Borello 	FN(xdp_adjust_head),		\
59091b8270fSChenbo Feng 	FN(probe_read_str),		\
5916acc5c29SChenbo Feng 	FN(get_socket_cookie),		\
592ded092cdSDaniel Borkmann 	FN(get_socket_uid),		\
5938c4b4c7eSLawrence Brakmo 	FN(set_hash),			\
5942be7e212SDaniel Borkmann 	FN(setsockopt),			\
5952be7e212SDaniel Borkmann 	FN(skb_adjust_room),
5967a4b28c6SDaniel Borkmann 
597ebb676daSThomas Graf /* integer value in 'imm' field of BPF_CALL instruction selects which helper
598ebb676daSThomas Graf  * function eBPF program intends to call
5992d0e30c3SDaniel Borkmann  */
600ebb676daSThomas Graf #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
601ebb676daSThomas Graf enum bpf_func_id {
602ebb676daSThomas Graf 	__BPF_FUNC_MAPPER(__BPF_ENUM_FN)
60309756af4SAlexei Starovoitov 	__BPF_FUNC_MAX_ID,
60409756af4SAlexei Starovoitov };
605ebb676daSThomas Graf #undef __BPF_ENUM_FN
60609756af4SAlexei Starovoitov 
607781c53bcSDaniel Borkmann /* All flags used by eBPF helper functions, placed here. */
608781c53bcSDaniel Borkmann 
609781c53bcSDaniel Borkmann /* BPF_FUNC_skb_store_bytes flags. */
610781c53bcSDaniel Borkmann #define BPF_F_RECOMPUTE_CSUM		(1ULL << 0)
6118afd54c8SDaniel Borkmann #define BPF_F_INVALIDATE_HASH		(1ULL << 1)
612781c53bcSDaniel Borkmann 
613781c53bcSDaniel Borkmann /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
614781c53bcSDaniel Borkmann  * First 4 bits are for passing the header field size.
615781c53bcSDaniel Borkmann  */
616781c53bcSDaniel Borkmann #define BPF_F_HDR_FIELD_MASK		0xfULL
617781c53bcSDaniel Borkmann 
618781c53bcSDaniel Borkmann /* BPF_FUNC_l4_csum_replace flags. */
619781c53bcSDaniel Borkmann #define BPF_F_PSEUDO_HDR		(1ULL << 4)
6202f72959aSDaniel Borkmann #define BPF_F_MARK_MANGLED_0		(1ULL << 5)
621d1b662adSDaniel Borkmann #define BPF_F_MARK_ENFORCE		(1ULL << 6)
622781c53bcSDaniel Borkmann 
623781c53bcSDaniel Borkmann /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
624781c53bcSDaniel Borkmann #define BPF_F_INGRESS			(1ULL << 0)
625781c53bcSDaniel Borkmann 
626c6c33454SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
627c6c33454SDaniel Borkmann #define BPF_F_TUNINFO_IPV6		(1ULL << 0)
628c6c33454SDaniel Borkmann 
629d5a3b1f6SAlexei Starovoitov /* BPF_FUNC_get_stackid flags. */
630d5a3b1f6SAlexei Starovoitov #define BPF_F_SKIP_FIELD_MASK		0xffULL
631d5a3b1f6SAlexei Starovoitov #define BPF_F_USER_STACK		(1ULL << 8)
632d5a3b1f6SAlexei Starovoitov #define BPF_F_FAST_STACK_CMP		(1ULL << 9)
633d5a3b1f6SAlexei Starovoitov #define BPF_F_REUSE_STACKID		(1ULL << 10)
634d5a3b1f6SAlexei Starovoitov 
6352da897e5SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key flags. */
6362da897e5SDaniel Borkmann #define BPF_F_ZERO_CSUM_TX		(1ULL << 1)
63722080870SDaniel Borkmann #define BPF_F_DONT_FRAGMENT		(1ULL << 2)
6382da897e5SDaniel Borkmann 
6396816a7ffSDaniel Borkmann /* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */
6401e33759cSDaniel Borkmann #define BPF_F_INDEX_MASK		0xffffffffULL
6411e33759cSDaniel Borkmann #define BPF_F_CURRENT_CPU		BPF_F_INDEX_MASK
642555c8a86SDaniel Borkmann /* BPF_FUNC_perf_event_output for sk_buff input context. */
643555c8a86SDaniel Borkmann #define BPF_F_CTXLEN_MASK		(0xfffffULL << 32)
6441e33759cSDaniel Borkmann 
6452be7e212SDaniel Borkmann /* Mode for BPF_FUNC_skb_adjust_room helper. */
6462be7e212SDaniel Borkmann enum bpf_adj_room_mode {
6472be7e212SDaniel Borkmann 	BPF_ADJ_ROOM_NET,
6482be7e212SDaniel Borkmann };
6492be7e212SDaniel Borkmann 
6509bac3d6dSAlexei Starovoitov /* user accessible mirror of in-kernel sk_buff.
6519bac3d6dSAlexei Starovoitov  * new fields can only be added to the end of this structure
6529bac3d6dSAlexei Starovoitov  */
6539bac3d6dSAlexei Starovoitov struct __sk_buff {
6549bac3d6dSAlexei Starovoitov 	__u32 len;
6559bac3d6dSAlexei Starovoitov 	__u32 pkt_type;
6569bac3d6dSAlexei Starovoitov 	__u32 mark;
6579bac3d6dSAlexei Starovoitov 	__u32 queue_mapping;
658c2497395SAlexei Starovoitov 	__u32 protocol;
659c2497395SAlexei Starovoitov 	__u32 vlan_present;
660c2497395SAlexei Starovoitov 	__u32 vlan_tci;
66127cd5452SMichal Sekletar 	__u32 vlan_proto;
662bcad5718SDaniel Borkmann 	__u32 priority;
66337e82c2fSAlexei Starovoitov 	__u32 ingress_ifindex;
66437e82c2fSAlexei Starovoitov 	__u32 ifindex;
665d691f9e8SAlexei Starovoitov 	__u32 tc_index;
666d691f9e8SAlexei Starovoitov 	__u32 cb[5];
667ba7591d8SDaniel Borkmann 	__u32 hash;
668045efa82SDaniel Borkmann 	__u32 tc_classid;
669969bf05eSAlexei Starovoitov 	__u32 data;
670969bf05eSAlexei Starovoitov 	__u32 data_end;
671b1d9fc41SDaniel Borkmann 	__u32 napi_id;
6729bac3d6dSAlexei Starovoitov };
6739bac3d6dSAlexei Starovoitov 
674d3aa45ceSAlexei Starovoitov struct bpf_tunnel_key {
675d3aa45ceSAlexei Starovoitov 	__u32 tunnel_id;
676c6c33454SDaniel Borkmann 	union {
677d3aa45ceSAlexei Starovoitov 		__u32 remote_ipv4;
678c6c33454SDaniel Borkmann 		__u32 remote_ipv6[4];
679c6c33454SDaniel Borkmann 	};
680c6c33454SDaniel Borkmann 	__u8 tunnel_tos;
681c6c33454SDaniel Borkmann 	__u8 tunnel_ttl;
682c0e760c9SDaniel Borkmann 	__u16 tunnel_ext;
6834018ab18SDaniel Borkmann 	__u32 tunnel_label;
684d3aa45ceSAlexei Starovoitov };
685d3aa45ceSAlexei Starovoitov 
6863a0af8fdSThomas Graf /* Generic BPF return codes which all BPF program types may support.
6873a0af8fdSThomas Graf  * The values are binary compatible with their TC_ACT_* counter-part to
6883a0af8fdSThomas Graf  * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
6893a0af8fdSThomas Graf  * programs.
6903a0af8fdSThomas Graf  *
6913a0af8fdSThomas Graf  * XDP is handled seprately, see XDP_*.
6923a0af8fdSThomas Graf  */
6933a0af8fdSThomas Graf enum bpf_ret_code {
6943a0af8fdSThomas Graf 	BPF_OK = 0,
6953a0af8fdSThomas Graf 	/* 1 reserved */
6963a0af8fdSThomas Graf 	BPF_DROP = 2,
6973a0af8fdSThomas Graf 	/* 3-6 reserved */
6983a0af8fdSThomas Graf 	BPF_REDIRECT = 7,
6993a0af8fdSThomas Graf 	/* >127 are reserved for prog type specific return codes */
7003a0af8fdSThomas Graf };
7013a0af8fdSThomas Graf 
70261023658SDavid Ahern struct bpf_sock {
70361023658SDavid Ahern 	__u32 bound_dev_if;
704aa4c1037SDavid Ahern 	__u32 family;
705aa4c1037SDavid Ahern 	__u32 type;
706aa4c1037SDavid Ahern 	__u32 protocol;
70761023658SDavid Ahern };
70861023658SDavid Ahern 
70917bedab2SMartin KaFai Lau #define XDP_PACKET_HEADROOM 256
71017bedab2SMartin KaFai Lau 
7116a773a15SBrenden Blanco /* User return codes for XDP prog type.
7126a773a15SBrenden Blanco  * A valid XDP program must return one of these defined values. All other
7136a773a15SBrenden Blanco  * return codes are reserved for future use. Unknown return codes will result
7146a773a15SBrenden Blanco  * in packet drop.
7156a773a15SBrenden Blanco  */
7166a773a15SBrenden Blanco enum xdp_action {
7176a773a15SBrenden Blanco 	XDP_ABORTED = 0,
7186a773a15SBrenden Blanco 	XDP_DROP,
7196a773a15SBrenden Blanco 	XDP_PASS,
7206ce96ca3SBrenden Blanco 	XDP_TX,
721814abfabSJohn Fastabend 	XDP_REDIRECT,
7226a773a15SBrenden Blanco };
7236a773a15SBrenden Blanco 
7246a773a15SBrenden Blanco /* user accessible metadata for XDP packet hook
7256a773a15SBrenden Blanco  * new fields must be added to the end of this structure
7266a773a15SBrenden Blanco  */
7276a773a15SBrenden Blanco struct xdp_md {
7286a773a15SBrenden Blanco 	__u32 data;
7296a773a15SBrenden Blanco 	__u32 data_end;
7306a773a15SBrenden Blanco };
7316a773a15SBrenden Blanco 
7321e270976SMartin KaFai Lau #define BPF_TAG_SIZE	8
7331e270976SMartin KaFai Lau 
7341e270976SMartin KaFai Lau struct bpf_prog_info {
7351e270976SMartin KaFai Lau 	__u32 type;
7361e270976SMartin KaFai Lau 	__u32 id;
7371e270976SMartin KaFai Lau 	__u8  tag[BPF_TAG_SIZE];
7381e270976SMartin KaFai Lau 	__u32 jited_prog_len;
7391e270976SMartin KaFai Lau 	__u32 xlated_prog_len;
7401e270976SMartin KaFai Lau 	__aligned_u64 jited_prog_insns;
7411e270976SMartin KaFai Lau 	__aligned_u64 xlated_prog_insns;
7421e270976SMartin KaFai Lau } __attribute__((aligned(8)));
7431e270976SMartin KaFai Lau 
7441e270976SMartin KaFai Lau struct bpf_map_info {
7451e270976SMartin KaFai Lau 	__u32 type;
7461e270976SMartin KaFai Lau 	__u32 id;
7471e270976SMartin KaFai Lau 	__u32 key_size;
7481e270976SMartin KaFai Lau 	__u32 value_size;
7491e270976SMartin KaFai Lau 	__u32 max_entries;
7501e270976SMartin KaFai Lau 	__u32 map_flags;
7511e270976SMartin KaFai Lau } __attribute__((aligned(8)));
7521e270976SMartin KaFai Lau 
75340304b2aSLawrence Brakmo /* User bpf_sock_ops struct to access socket values and specify request ops
75440304b2aSLawrence Brakmo  * and their replies.
75540304b2aSLawrence Brakmo  * Some of this fields are in network (bigendian) byte order and may need
75640304b2aSLawrence Brakmo  * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h).
75740304b2aSLawrence Brakmo  * New fields can only be added at the end of this structure
75840304b2aSLawrence Brakmo  */
75940304b2aSLawrence Brakmo struct bpf_sock_ops {
76040304b2aSLawrence Brakmo 	__u32 op;
76140304b2aSLawrence Brakmo 	union {
76240304b2aSLawrence Brakmo 		__u32 reply;
76340304b2aSLawrence Brakmo 		__u32 replylong[4];
76440304b2aSLawrence Brakmo 	};
76540304b2aSLawrence Brakmo 	__u32 family;
76640304b2aSLawrence Brakmo 	__u32 remote_ip4;	/* Stored in network byte order */
76740304b2aSLawrence Brakmo 	__u32 local_ip4;	/* Stored in network byte order */
76840304b2aSLawrence Brakmo 	__u32 remote_ip6[4];	/* Stored in network byte order */
76940304b2aSLawrence Brakmo 	__u32 local_ip6[4];	/* Stored in network byte order */
77040304b2aSLawrence Brakmo 	__u32 remote_port;	/* Stored in network byte order */
77140304b2aSLawrence Brakmo 	__u32 local_port;	/* stored in host byte order */
77240304b2aSLawrence Brakmo };
77340304b2aSLawrence Brakmo 
77440304b2aSLawrence Brakmo /* List of known BPF sock_ops operators.
77540304b2aSLawrence Brakmo  * New entries can only be added at the end
77640304b2aSLawrence Brakmo  */
77740304b2aSLawrence Brakmo enum {
77840304b2aSLawrence Brakmo 	BPF_SOCK_OPS_VOID,
7798550f328SLawrence Brakmo 	BPF_SOCK_OPS_TIMEOUT_INIT,	/* Should return SYN-RTO value to use or
7808550f328SLawrence Brakmo 					 * -1 if default value should be used
7818550f328SLawrence Brakmo 					 */
78213d3b1ebSLawrence Brakmo 	BPF_SOCK_OPS_RWND_INIT,		/* Should return initial advertized
78313d3b1ebSLawrence Brakmo 					 * window (in packets) or -1 if default
78413d3b1ebSLawrence Brakmo 					 * value should be used
78513d3b1ebSLawrence Brakmo 					 */
7869872a4bdSLawrence Brakmo 	BPF_SOCK_OPS_TCP_CONNECT_CB,	/* Calls BPF program right before an
7879872a4bdSLawrence Brakmo 					 * active connection is initialized
7889872a4bdSLawrence Brakmo 					 */
7899872a4bdSLawrence Brakmo 	BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB,	/* Calls BPF program when an
7909872a4bdSLawrence Brakmo 						 * active connection is
7919872a4bdSLawrence Brakmo 						 * established
7929872a4bdSLawrence Brakmo 						 */
7939872a4bdSLawrence Brakmo 	BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB,	/* Calls BPF program when a
7949872a4bdSLawrence Brakmo 						 * passive connection is
7959872a4bdSLawrence Brakmo 						 * established
7969872a4bdSLawrence Brakmo 						 */
79791b5b21cSLawrence Brakmo 	BPF_SOCK_OPS_NEEDS_ECN,		/* If connection's congestion control
79891b5b21cSLawrence Brakmo 					 * needs ECN
79991b5b21cSLawrence Brakmo 					 */
80040304b2aSLawrence Brakmo };
80140304b2aSLawrence Brakmo 
802fc747810SLawrence Brakmo #define TCP_BPF_IW		1001	/* Set TCP initial congestion window */
80313bf9641SLawrence Brakmo #define TCP_BPF_SNDCWND_CLAMP	1002	/* Set sndcwnd_clamp */
804fc747810SLawrence Brakmo 
805daedfb22SAlexei Starovoitov #endif /* _UAPI__LINUX_BPF_H__ */
806