blob: 70082cb626b43283dab7ec28c5f495c3c9cf36eb [file] [log] [blame]
Greg Kroah-Hartmane2be04c2017-11-01 15:09:13 +01001/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
Alexei Starovoitovdaedfb22014-09-04 22:17:18 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 */
8#ifndef _UAPI__LINUX_BPF_H__
9#define _UAPI__LINUX_BPF_H__
10
11#include <linux/types.h>
Alexei Starovoitovc15952d2014-10-14 02:08:54 -070012#include <linux/bpf_common.h>
Alexei Starovoitovdaedfb22014-09-04 22:17:18 -070013
14/* Extended instruction set based on top of classic BPF */
15
16/* instruction classes */
17#define BPF_ALU64 0x07 /* alu mode in double word width */
18
19/* ld/ldx fields */
Jesper Dangaard Brouercb5f7332018-01-17 12:05:36 +010020#define BPF_DW 0x18 /* double word (64-bit) */
Alexei Starovoitovdaedfb22014-09-04 22:17:18 -070021#define BPF_XADD 0xc0 /* exclusive add */
22
23/* alu/jmp fields */
24#define BPF_MOV 0xb0 /* mov reg to reg */
25#define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
26
27/* change endianness of a register */
28#define BPF_END 0xd0 /* flags for endianness conversion: */
29#define BPF_TO_LE 0x00 /* convert to little-endian */
30#define BPF_TO_BE 0x08 /* convert to big-endian */
31#define BPF_FROM_LE BPF_TO_LE
32#define BPF_FROM_BE BPF_TO_BE
33
Daniel Borkmann92b31a92017-08-10 01:39:55 +020034/* jmp encodings */
Alexei Starovoitovdaedfb22014-09-04 22:17:18 -070035#define BPF_JNE 0x50 /* jump != */
Daniel Borkmann92b31a92017-08-10 01:39:55 +020036#define BPF_JLT 0xa0 /* LT is unsigned, '<' */
37#define BPF_JLE 0xb0 /* LE is unsigned, '<=' */
Alexei Starovoitovdaedfb22014-09-04 22:17:18 -070038#define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */
39#define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */
Daniel Borkmann92b31a92017-08-10 01:39:55 +020040#define BPF_JSLT 0xc0 /* SLT is signed, '<' */
41#define BPF_JSLE 0xd0 /* SLE is signed, '<=' */
Alexei Starovoitovdaedfb22014-09-04 22:17:18 -070042#define BPF_CALL 0x80 /* function call */
43#define BPF_EXIT 0x90 /* function return */
44
45/* Register numbers */
46enum {
47 BPF_REG_0 = 0,
48 BPF_REG_1,
49 BPF_REG_2,
50 BPF_REG_3,
51 BPF_REG_4,
52 BPF_REG_5,
53 BPF_REG_6,
54 BPF_REG_7,
55 BPF_REG_8,
56 BPF_REG_9,
57 BPF_REG_10,
58 __MAX_BPF_REG,
59};
60
61/* BPF has 10 general purpose 64-bit registers and stack frame. */
62#define MAX_BPF_REG __MAX_BPF_REG
63
64struct bpf_insn {
65 __u8 code; /* opcode */
66 __u8 dst_reg:4; /* dest register */
67 __u8 src_reg:4; /* source register */
68 __s16 off; /* signed offset */
69 __s32 imm; /* signed immediate constant */
70};
71
Daniel Mackb95a5c42017-01-21 17:26:11 +010072/* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */
73struct bpf_lpm_trie_key {
74 __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */
75 __u8 data[0]; /* Arbitrary size */
76};
77
Roman Gushchinde9cbba2018-08-02 14:27:18 -070078struct bpf_cgroup_storage_key {
79 __u64 cgroup_inode_id; /* cgroup inode id */
80 __u32 attach_type; /* program attach type */
81};
82
Daniel Borkmannb2197752015-10-29 14:58:09 +010083/* BPF syscall commands, see bpf(2) man-page for details. */
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070084enum bpf_cmd {
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070085 BPF_MAP_CREATE,
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070086 BPF_MAP_LOOKUP_ELEM,
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070087 BPF_MAP_UPDATE_ELEM,
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070088 BPF_MAP_DELETE_ELEM,
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070089 BPF_MAP_GET_NEXT_KEY,
Alexei Starovoitov09756af2014-09-26 00:17:00 -070090 BPF_PROG_LOAD,
Daniel Borkmannb2197752015-10-29 14:58:09 +010091 BPF_OBJ_PIN,
92 BPF_OBJ_GET,
Daniel Mackf4324552016-11-23 16:52:27 +010093 BPF_PROG_ATTACH,
94 BPF_PROG_DETACH,
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070095 BPF_PROG_TEST_RUN,
Martin KaFai Lau34ad5582017-06-05 12:15:48 -070096 BPF_PROG_GET_NEXT_ID,
97 BPF_MAP_GET_NEXT_ID,
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -070098 BPF_PROG_GET_FD_BY_ID,
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -070099 BPF_MAP_GET_FD_BY_ID,
Martin KaFai Lau1e270972017-06-05 12:15:52 -0700100 BPF_OBJ_GET_INFO_BY_FD,
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700101 BPF_PROG_QUERY,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700102 BPF_RAW_TRACEPOINT_OPEN,
Martin KaFai Lauf56a6532018-04-18 15:56:01 -0700103 BPF_BTF_LOAD,
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700104 BPF_BTF_GET_FD_BY_ID,
Yonghong Song41bdc4b2018-05-24 11:21:09 -0700105 BPF_TASK_FD_QUERY,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700106};
107
108enum bpf_map_type {
109 BPF_MAP_TYPE_UNSPEC,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800110 BPF_MAP_TYPE_HASH,
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800111 BPF_MAP_TYPE_ARRAY,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700112 BPF_MAP_TYPE_PROG_ARRAY,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000113 BPF_MAP_TYPE_PERF_EVENT_ARRAY,
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800114 BPF_MAP_TYPE_PERCPU_HASH,
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800115 BPF_MAP_TYPE_PERCPU_ARRAY,
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800116 BPF_MAP_TYPE_STACK_TRACE,
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700117 BPF_MAP_TYPE_CGROUP_ARRAY,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800118 BPF_MAP_TYPE_LRU_HASH,
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800119 BPF_MAP_TYPE_LRU_PERCPU_HASH,
Daniel Mackb95a5c42017-01-21 17:26:11 +0100120 BPF_MAP_TYPE_LPM_TRIE,
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700121 BPF_MAP_TYPE_ARRAY_OF_MAPS,
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700122 BPF_MAP_TYPE_HASH_OF_MAPS,
John Fastabend546ac1f2017-07-17 09:28:56 -0700123 BPF_MAP_TYPE_DEVMAP,
John Fastabend174a79f2017-08-15 22:32:47 -0700124 BPF_MAP_TYPE_SOCKMAP,
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200125 BPF_MAP_TYPE_CPUMAP,
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200126 BPF_MAP_TYPE_XSKMAP,
John Fastabend81110382018-05-14 10:00:17 -0700127 BPF_MAP_TYPE_SOCKHASH,
Roman Gushchinde9cbba2018-08-02 14:27:18 -0700128 BPF_MAP_TYPE_CGROUP_STORAGE,
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700129 BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
Roman Gushchinb741f162018-09-28 14:45:43 +0000130 BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE,
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200131 BPF_MAP_TYPE_QUEUE,
132 BPF_MAP_TYPE_STACK,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700133};
134
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700135enum bpf_prog_type {
136 BPF_PROG_TYPE_UNSPEC,
Alexei Starovoitovddd872b2014-12-01 15:06:34 -0800137 BPF_PROG_TYPE_SOCKET_FILTER,
Alexei Starovoitov25415172015-03-25 12:49:20 -0700138 BPF_PROG_TYPE_KPROBE,
Daniel Borkmann96be4322015-03-01 12:31:46 +0100139 BPF_PROG_TYPE_SCHED_CLS,
Daniel Borkmann94caee8c2015-03-20 15:11:11 +0100140 BPF_PROG_TYPE_SCHED_ACT,
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -0700141 BPF_PROG_TYPE_TRACEPOINT,
Brenden Blanco6a773a12016-07-19 12:16:47 -0700142 BPF_PROG_TYPE_XDP,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700143 BPF_PROG_TYPE_PERF_EVENT,
Daniel Mack0e336612016-11-23 16:52:25 +0100144 BPF_PROG_TYPE_CGROUP_SKB,
David Ahern610236582016-12-01 08:48:04 -0800145 BPF_PROG_TYPE_CGROUP_SOCK,
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100146 BPF_PROG_TYPE_LWT_IN,
147 BPF_PROG_TYPE_LWT_OUT,
148 BPF_PROG_TYPE_LWT_XMIT,
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700149 BPF_PROG_TYPE_SOCK_OPS,
John Fastabendb005fd12017-08-15 22:31:58 -0700150 BPF_PROG_TYPE_SK_SKB,
Roman Gushchinebc614f2017-11-05 08:15:32 -0500151 BPF_PROG_TYPE_CGROUP_DEVICE,
John Fastabend4f738ad2018-03-18 12:57:10 -0700152 BPF_PROG_TYPE_SK_MSG,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700153 BPF_PROG_TYPE_RAW_TRACEPOINT,
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700154 BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
Mathieu Xhonneux004d4b22018-05-20 14:58:16 +0100155 BPF_PROG_TYPE_LWT_SEG6LOCAL,
Sean Youngf4364dc2018-05-27 12:24:09 +0100156 BPF_PROG_TYPE_LIRC_MODE2,
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -0700157 BPF_PROG_TYPE_SK_REUSEPORT,
Petar Penkovd58e4682018-09-14 07:46:18 -0700158 BPF_PROG_TYPE_FLOW_DISSECTOR,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700159};
160
Daniel Mack0e336612016-11-23 16:52:25 +0100161enum bpf_attach_type {
162 BPF_CGROUP_INET_INGRESS,
163 BPF_CGROUP_INET_EGRESS,
David Ahern610236582016-12-01 08:48:04 -0800164 BPF_CGROUP_INET_SOCK_CREATE,
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700165 BPF_CGROUP_SOCK_OPS,
John Fastabend464bc0f2017-08-28 07:10:04 -0700166 BPF_SK_SKB_STREAM_PARSER,
167 BPF_SK_SKB_STREAM_VERDICT,
Roman Gushchinebc614f2017-11-05 08:15:32 -0500168 BPF_CGROUP_DEVICE,
John Fastabend4f738ad2018-03-18 12:57:10 -0700169 BPF_SK_MSG_VERDICT,
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700170 BPF_CGROUP_INET4_BIND,
171 BPF_CGROUP_INET6_BIND,
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700172 BPF_CGROUP_INET4_CONNECT,
173 BPF_CGROUP_INET6_CONNECT,
Andrey Ignatovaac3fc32018-03-30 15:08:07 -0700174 BPF_CGROUP_INET4_POST_BIND,
175 BPF_CGROUP_INET6_POST_BIND,
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700176 BPF_CGROUP_UDP4_SENDMSG,
177 BPF_CGROUP_UDP6_SENDMSG,
Sean Youngf4364dc2018-05-27 12:24:09 +0100178 BPF_LIRC_MODE2,
Petar Penkovd58e4682018-09-14 07:46:18 -0700179 BPF_FLOW_DISSECTOR,
Daniel Mack0e336612016-11-23 16:52:25 +0100180 __MAX_BPF_ATTACH_TYPE
181};
182
183#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
184
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700185/* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
186 *
187 * NONE(default): No further bpf programs allowed in the subtree.
188 *
189 * BPF_F_ALLOW_OVERRIDE: If a sub-cgroup installs some bpf program,
190 * the program in this cgroup yields to sub-cgroup program.
191 *
192 * BPF_F_ALLOW_MULTI: If a sub-cgroup installs some bpf program,
193 * that cgroup program gets run in addition to the program in this cgroup.
194 *
195 * Only one program is allowed to be attached to a cgroup with
196 * NONE or BPF_F_ALLOW_OVERRIDE flag.
197 * Attaching another program on top of NONE or BPF_F_ALLOW_OVERRIDE will
198 * release old program and attach the new one. Attach flags has to match.
199 *
200 * Multiple programs are allowed to be attached to a cgroup with
201 * BPF_F_ALLOW_MULTI flag. They are executed in FIFO order
202 * (those that were attached first, run first)
203 * The programs of sub-cgroup are executed first, then programs of
204 * this cgroup and then programs of parent cgroup.
205 * When children program makes decision (like picking TCP CA or sock bind)
206 * parent program has a chance to override it.
207 *
208 * A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups.
209 * A cgroup with NONE doesn't allow any programs in sub-cgroups.
210 * Ex1:
211 * cgrp1 (MULTI progs A, B) ->
212 * cgrp2 (OVERRIDE prog C) ->
213 * cgrp3 (MULTI prog D) ->
214 * cgrp4 (OVERRIDE prog E) ->
215 * cgrp5 (NONE prog F)
216 * the event in cgrp5 triggers execution of F,D,A,B in that order.
217 * if prog F is detached, the execution is E,D,A,B
218 * if prog F and D are detached, the execution is E,A,B
219 * if prog F, E and D are detached, the execution is C,A,B
220 *
221 * All eligible programs are executed regardless of return code from
222 * earlier programs.
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800223 */
224#define BPF_F_ALLOW_OVERRIDE (1U << 0)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700225#define BPF_F_ALLOW_MULTI (1U << 1)
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800226
David S. Millere07b98d2017-05-10 11:38:07 -0700227/* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
228 * verifier will perform strict alignment checking as if the kernel
229 * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set,
230 * and NET_IP_ALIGN defined to 2.
231 */
232#define BPF_F_STRICT_ALIGNMENT (1U << 0)
233
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800234/* when bpf_ldimm64->src_reg == BPF_PSEUDO_MAP_FD, bpf_ldimm64->imm == fd */
Daniel Borkmannf1a66f82015-03-01 12:31:43 +0100235#define BPF_PSEUDO_MAP_FD 1
236
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800237/* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative
238 * offset to another bpf function
239 */
240#define BPF_PSEUDO_CALL 1
241
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800242/* flags for BPF_MAP_UPDATE_ELEM command */
243#define BPF_ANY 0 /* create new element or update existing */
244#define BPF_NOEXIST 1 /* create new element if it didn't exist */
245#define BPF_EXIST 2 /* update existing element */
246
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700247/* flags for BPF_MAP_CREATE command */
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800248#define BPF_F_NO_PREALLOC (1U << 0)
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800249/* Instead of having one common LRU list in the
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800250 * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800251 * which can scale and perform better.
252 * Note, the LRU nodes (including free nodes) cannot be moved
253 * across different LRU lists.
254 */
255#define BPF_F_NO_COMMON_LRU (1U << 1)
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700256/* Specify numa node during map creation */
257#define BPF_F_NUMA_NODE (1U << 2)
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800258
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700259/* flags for BPF_PROG_QUERY */
260#define BPF_F_QUERY_EFFECTIVE (1U << 0)
261
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700262#define BPF_OBJ_NAME_LEN 16U
263
Chenbo Feng6e71b042017-10-18 13:00:22 -0700264/* Flags for accessing BPF object */
265#define BPF_F_RDONLY (1U << 3)
266#define BPF_F_WRONLY (1U << 4)
267
Song Liu615755a2018-03-14 10:23:21 -0700268/* Flag for stack_map, store build_id+offset instead of pointer */
269#define BPF_F_STACK_BUILD_ID (1U << 5)
270
271enum bpf_stack_build_id_status {
272 /* user space need an empty entry to identify end of a trace */
273 BPF_STACK_BUILD_ID_EMPTY = 0,
274 /* with valid build_id and offset */
275 BPF_STACK_BUILD_ID_VALID = 1,
276 /* couldn't get build_id, fallback to ip */
277 BPF_STACK_BUILD_ID_IP = 2,
278};
279
280#define BPF_BUILD_ID_SIZE 20
281struct bpf_stack_build_id {
282 __s32 status;
283 unsigned char build_id[BPF_BUILD_ID_SIZE];
284 union {
285 __u64 offset;
286 __u64 ip;
287 };
288};
289
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700290union bpf_attr {
291 struct { /* anonymous struct used by BPF_MAP_CREATE command */
292 __u32 map_type; /* one of enum bpf_map_type */
293 __u32 key_size; /* size of key in bytes */
294 __u32 value_size; /* size of value in bytes */
295 __u32 max_entries; /* max number of entries in a map */
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700296 __u32 map_flags; /* BPF_MAP_CREATE related
297 * flags defined above.
298 */
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700299 __u32 inner_map_fd; /* fd pointing to the inner map */
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700300 __u32 numa_node; /* numa node (effective only if
301 * BPF_F_NUMA_NODE is set).
302 */
Martin KaFai Lau067cae42017-10-05 21:52:12 -0700303 char map_name[BPF_OBJ_NAME_LEN];
Jakub Kicinskia3884572018-01-11 20:29:09 -0800304 __u32 map_ifindex; /* ifindex of netdev to create on */
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700305 __u32 btf_fd; /* fd pointing to a BTF type data */
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700306 __u32 btf_key_type_id; /* BTF type_id of the key */
307 __u32 btf_value_type_id; /* BTF type_id of the value */
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700308 };
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700309
310 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
311 __u32 map_fd;
312 __aligned_u64 key;
313 union {
314 __aligned_u64 value;
315 __aligned_u64 next_key;
316 };
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800317 __u64 flags;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700318 };
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700319
320 struct { /* anonymous struct used by BPF_PROG_LOAD command */
321 __u32 prog_type; /* one of enum bpf_prog_type */
322 __u32 insn_cnt;
323 __aligned_u64 insns;
324 __aligned_u64 license;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700325 __u32 log_level; /* verbosity level of verifier */
326 __u32 log_size; /* size of user buffer */
327 __aligned_u64 log_buf; /* user supplied buffer */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700328 __u32 kern_version; /* checked when prog_type=kprobe */
David S. Millere07b98d2017-05-10 11:38:07 -0700329 __u32 prog_flags;
Martin KaFai Lau067cae42017-10-05 21:52:12 -0700330 char prog_name[BPF_OBJ_NAME_LEN];
Jakub Kicinski1f6f4cb2017-11-20 15:21:53 -0800331 __u32 prog_ifindex; /* ifindex of netdev to prep for */
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700332 /* For some prog types expected attach type must be known at
333 * load time to verify attach type specific parts of prog
334 * (context accesses, allowed helpers, etc).
335 */
336 __u32 expected_attach_type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700337 };
Daniel Borkmannb2197752015-10-29 14:58:09 +0100338
339 struct { /* anonymous struct used by BPF_OBJ_* commands */
340 __aligned_u64 pathname;
341 __u32 bpf_fd;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700342 __u32 file_flags;
Daniel Borkmannb2197752015-10-29 14:58:09 +0100343 };
Daniel Mackf4324552016-11-23 16:52:27 +0100344
345 struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
346 __u32 target_fd; /* container object to attach to */
347 __u32 attach_bpf_fd; /* eBPF program to attach */
348 __u32 attach_type;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800349 __u32 attach_flags;
Daniel Mackf4324552016-11-23 16:52:27 +0100350 };
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700351
352 struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
353 __u32 prog_fd;
354 __u32 retval;
355 __u32 data_size_in;
356 __u32 data_size_out;
357 __aligned_u64 data_in;
358 __aligned_u64 data_out;
359 __u32 repeat;
360 __u32 duration;
361 } test;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700362
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700363 struct { /* anonymous struct used by BPF_*_GET_*_ID */
364 union {
365 __u32 start_id;
366 __u32 prog_id;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700367 __u32 map_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700368 __u32 btf_id;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700369 };
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700370 __u32 next_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700371 __u32 open_flags;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700372 };
Martin KaFai Lau1e270972017-06-05 12:15:52 -0700373
374 struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
375 __u32 bpf_fd;
376 __u32 info_len;
377 __aligned_u64 info;
378 } info;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700379
380 struct { /* anonymous struct used by BPF_PROG_QUERY command */
381 __u32 target_fd; /* container object to query */
382 __u32 attach_type;
383 __u32 query_flags;
384 __u32 attach_flags;
385 __aligned_u64 prog_ids;
386 __u32 prog_cnt;
387 } query;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700388
389 struct {
390 __u64 name;
391 __u32 prog_fd;
392 } raw_tracepoint;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -0700393
394 struct { /* anonymous struct for BPF_BTF_LOAD */
395 __aligned_u64 btf;
396 __aligned_u64 btf_log_buf;
397 __u32 btf_size;
398 __u32 btf_log_size;
399 __u32 btf_log_level;
400 };
Yonghong Song41bdc4b2018-05-24 11:21:09 -0700401
402 struct {
403 __u32 pid; /* input: pid */
404 __u32 fd; /* input: fd */
405 __u32 flags; /* input: flags */
406 __u32 buf_len; /* input/output: buf len */
407 __aligned_u64 buf; /* input/output:
408 * tp_name for tracepoint
409 * symbol for kprobe
410 * filename for uprobe
411 */
412 __u32 prog_id; /* output: prod_id */
413 __u32 fd_type; /* output: BPF_FD_TYPE_* */
414 __u64 probe_offset; /* output: probe_offset */
415 __u64 probe_addr; /* output: probe_addr */
416 } task_fd_query;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700417} __attribute__((aligned(8)));
418
Quentin Monnet56a092c2018-04-25 18:16:52 +0100419/* The description below is an attempt at providing documentation to eBPF
420 * developers about the multiple available eBPF helper functions. It can be
421 * parsed and used to produce a manual page. The workflow is the following,
422 * and requires the rst2man utility:
Thomas Grafebb676d2016-10-27 11:23:51 +0200423 *
Quentin Monnet56a092c2018-04-25 18:16:52 +0100424 * $ ./scripts/bpf_helpers_doc.py \
425 * --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst
426 * $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7
427 * $ man /tmp/bpf-helpers.7
Thomas Grafebb676d2016-10-27 11:23:51 +0200428 *
Quentin Monnet56a092c2018-04-25 18:16:52 +0100429 * Note that in order to produce this external documentation, some RST
430 * formatting is used in the descriptions to get "bold" and "italics" in
431 * manual pages. Also note that the few trailing white spaces are
432 * intentional, removing them would break paragraphs for rst2man.
Thomas Grafebb676d2016-10-27 11:23:51 +0200433 *
Quentin Monnet56a092c2018-04-25 18:16:52 +0100434 * Start of BPF helper function descriptions:
Quentin Monnetad4a5222018-04-25 18:16:53 +0100435 *
436 * void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)
437 * Description
438 * Perform a lookup in *map* for an entry associated to *key*.
439 * Return
440 * Map value associated to *key*, or **NULL** if no entry was
441 * found.
442 *
443 * int bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags)
444 * Description
445 * Add or update the value of the entry associated to *key* in
446 * *map* with *value*. *flags* is one of:
447 *
448 * **BPF_NOEXIST**
449 * The entry for *key* must not exist in the map.
450 * **BPF_EXIST**
451 * The entry for *key* must already exist in the map.
452 * **BPF_ANY**
453 * No condition on the existence of the entry for *key*.
454 *
455 * Flag value **BPF_NOEXIST** cannot be used for maps of types
456 * **BPF_MAP_TYPE_ARRAY** or **BPF_MAP_TYPE_PERCPU_ARRAY** (all
457 * elements always exist), the helper would return an error.
458 * Return
459 * 0 on success, or a negative error in case of failure.
460 *
461 * int bpf_map_delete_elem(struct bpf_map *map, const void *key)
462 * Description
463 * Delete entry with *key* from *map*.
464 * Return
465 * 0 on success, or a negative error in case of failure.
466 *
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200467 * int bpf_map_push_elem(struct bpf_map *map, const void *value, u64 flags)
468 * Description
469 * Push an element *value* in *map*. *flags* is one of:
470 *
471 * **BPF_EXIST**
472 * If the queue/stack is full, the oldest element is removed to
473 * make room for this.
474 * Return
475 * 0 on success, or a negative error in case of failure.
476 *
477 * int bpf_map_pop_elem(struct bpf_map *map, void *value)
478 * Description
479 * Pop an element from *map*.
480 * Return
481 * 0 on success, or a negative error in case of failure.
482 *
483 * int bpf_map_peek_elem(struct bpf_map *map, void *value)
484 * Description
485 * Get an element from *map* without removing it.
486 * Return
487 * 0 on success, or a negative error in case of failure.
488 *
Quentin Monnetad4a5222018-04-25 18:16:53 +0100489 * int bpf_probe_read(void *dst, u32 size, const void *src)
490 * Description
491 * For tracing programs, safely attempt to read *size* bytes from
492 * address *src* and store the data in *dst*.
493 * Return
494 * 0 on success, or a negative error in case of failure.
495 *
496 * u64 bpf_ktime_get_ns(void)
497 * Description
498 * Return the time elapsed since system boot, in nanoseconds.
499 * Return
500 * Current *ktime*.
501 *
502 * int bpf_trace_printk(const char *fmt, u32 fmt_size, ...)
503 * Description
504 * This helper is a "printk()-like" facility for debugging. It
505 * prints a message defined by format *fmt* (of size *fmt_size*)
506 * to file *\/sys/kernel/debug/tracing/trace* from DebugFS, if
507 * available. It can take up to three additional **u64**
508 * arguments (as an eBPF helpers, the total number of arguments is
509 * limited to five).
510 *
511 * Each time the helper is called, it appends a line to the trace.
512 * The format of the trace is customizable, and the exact output
513 * one will get depends on the options set in
514 * *\/sys/kernel/debug/tracing/trace_options* (see also the
515 * *README* file under the same directory). However, it usually
516 * defaults to something like:
517 *
518 * ::
519 *
520 * telnet-470 [001] .N.. 419421.045894: 0x00000001: <formatted msg>
521 *
522 * In the above:
523 *
524 * * ``telnet`` is the name of the current task.
525 * * ``470`` is the PID of the current task.
526 * * ``001`` is the CPU number on which the task is
527 * running.
528 * * In ``.N..``, each character refers to a set of
529 * options (whether irqs are enabled, scheduling
530 * options, whether hard/softirqs are running, level of
531 * preempt_disabled respectively). **N** means that
532 * **TIF_NEED_RESCHED** and **PREEMPT_NEED_RESCHED**
533 * are set.
534 * * ``419421.045894`` is a timestamp.
535 * * ``0x00000001`` is a fake value used by BPF for the
536 * instruction pointer register.
537 * * ``<formatted msg>`` is the message formatted with
538 * *fmt*.
539 *
540 * The conversion specifiers supported by *fmt* are similar, but
541 * more limited than for printk(). They are **%d**, **%i**,
542 * **%u**, **%x**, **%ld**, **%li**, **%lu**, **%lx**, **%lld**,
543 * **%lli**, **%llu**, **%llx**, **%p**, **%s**. No modifier (size
544 * of field, padding with zeroes, etc.) is available, and the
545 * helper will return **-EINVAL** (but print nothing) if it
546 * encounters an unknown specifier.
547 *
548 * Also, note that **bpf_trace_printk**\ () is slow, and should
549 * only be used for debugging purposes. For this reason, a notice
550 * bloc (spanning several lines) is printed to kernel logs and
551 * states that the helper should not be used "for production use"
552 * the first time this helper is used (or more precisely, when
553 * **trace_printk**\ () buffers are allocated). For passing values
554 * to user space, perf events should be preferred.
555 * Return
556 * The number of bytes written to the buffer, or a negative error
557 * in case of failure.
558 *
Quentin Monnet1fdd08b2018-04-25 18:16:55 +0100559 * u32 bpf_get_prandom_u32(void)
560 * Description
561 * Get a pseudo-random number.
562 *
563 * From a security point of view, this helper uses its own
564 * pseudo-random internal state, and cannot be used to infer the
565 * seed of other random functions in the kernel. However, it is
566 * essential to note that the generator used by the helper is not
567 * cryptographically secure.
568 * Return
569 * A random 32-bit unsigned value.
570 *
571 * u32 bpf_get_smp_processor_id(void)
572 * Description
573 * Get the SMP (symmetric multiprocessing) processor id. Note that
574 * all programs run with preemption disabled, which means that the
575 * SMP processor id is stable during all the execution of the
576 * program.
577 * Return
578 * The SMP id of the processor running the program.
579 *
Quentin Monnetad4a5222018-04-25 18:16:53 +0100580 * int bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags)
581 * Description
582 * Store *len* bytes from address *from* into the packet
583 * associated to *skb*, at *offset*. *flags* are a combination of
584 * **BPF_F_RECOMPUTE_CSUM** (automatically recompute the
585 * checksum for the packet after storing the bytes) and
586 * **BPF_F_INVALIDATE_HASH** (set *skb*\ **->hash**, *skb*\
587 * **->swhash** and *skb*\ **->l4hash** to 0).
588 *
589 * A call to this helper is susceptible to change the underlaying
590 * packet buffer. Therefore, at load time, all checks on pointers
591 * previously done by the verifier are invalidated and must be
592 * performed again, if the helper is used in combination with
593 * direct packet access.
594 * Return
595 * 0 on success, or a negative error in case of failure.
596 *
597 * int bpf_l3_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 size)
598 * Description
599 * Recompute the layer 3 (e.g. IP) checksum for the packet
600 * associated to *skb*. Computation is incremental, so the helper
601 * must know the former value of the header field that was
602 * modified (*from*), the new value of this field (*to*), and the
603 * number of bytes (2 or 4) for this field, stored in *size*.
604 * Alternatively, it is possible to store the difference between
605 * the previous and the new values of the header field in *to*, by
606 * setting *from* and *size* to 0. For both methods, *offset*
607 * indicates the location of the IP checksum within the packet.
608 *
609 * This helper works in combination with **bpf_csum_diff**\ (),
610 * which does not update the checksum in-place, but offers more
611 * flexibility and can handle sizes larger than 2 or 4 for the
612 * checksum to update.
613 *
614 * A call to this helper is susceptible to change the underlaying
615 * packet buffer. Therefore, at load time, all checks on pointers
616 * previously done by the verifier are invalidated and must be
617 * performed again, if the helper is used in combination with
618 * direct packet access.
619 * Return
620 * 0 on success, or a negative error in case of failure.
621 *
622 * int bpf_l4_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 flags)
623 * Description
624 * Recompute the layer 4 (e.g. TCP, UDP or ICMP) checksum for the
625 * packet associated to *skb*. Computation is incremental, so the
626 * helper must know the former value of the header field that was
627 * modified (*from*), the new value of this field (*to*), and the
628 * number of bytes (2 or 4) for this field, stored on the lowest
629 * four bits of *flags*. Alternatively, it is possible to store
630 * the difference between the previous and the new values of the
631 * header field in *to*, by setting *from* and the four lowest
632 * bits of *flags* to 0. For both methods, *offset* indicates the
633 * location of the IP checksum within the packet. In addition to
634 * the size of the field, *flags* can be added (bitwise OR) actual
635 * flags. With **BPF_F_MARK_MANGLED_0**, a null checksum is left
636 * untouched (unless **BPF_F_MARK_ENFORCE** is added as well), and
637 * for updates resulting in a null checksum the value is set to
638 * **CSUM_MANGLED_0** instead. Flag **BPF_F_PSEUDO_HDR** indicates
639 * the checksum is to be computed against a pseudo-header.
640 *
641 * This helper works in combination with **bpf_csum_diff**\ (),
642 * which does not update the checksum in-place, but offers more
643 * flexibility and can handle sizes larger than 2 or 4 for the
644 * checksum to update.
645 *
646 * A call to this helper is susceptible to change the underlaying
647 * packet buffer. Therefore, at load time, all checks on pointers
648 * previously done by the verifier are invalidated and must be
649 * performed again, if the helper is used in combination with
650 * direct packet access.
651 * Return
652 * 0 on success, or a negative error in case of failure.
653 *
654 * int bpf_tail_call(void *ctx, struct bpf_map *prog_array_map, u32 index)
655 * Description
656 * This special helper is used to trigger a "tail call", or in
657 * other words, to jump into another eBPF program. The same stack
658 * frame is used (but values on stack and in registers for the
659 * caller are not accessible to the callee). This mechanism allows
660 * for program chaining, either for raising the maximum number of
661 * available eBPF instructions, or to execute given programs in
662 * conditional blocks. For security reasons, there is an upper
663 * limit to the number of successive tail calls that can be
664 * performed.
665 *
666 * Upon call of this helper, the program attempts to jump into a
667 * program referenced at index *index* in *prog_array_map*, a
668 * special map of type **BPF_MAP_TYPE_PROG_ARRAY**, and passes
669 * *ctx*, a pointer to the context.
670 *
671 * If the call succeeds, the kernel immediately runs the first
672 * instruction of the new program. This is not a function call,
673 * and it never returns to the previous program. If the call
674 * fails, then the helper has no effect, and the caller continues
675 * to run its subsequent instructions. A call can fail if the
676 * destination program for the jump does not exist (i.e. *index*
677 * is superior to the number of entries in *prog_array_map*), or
678 * if the maximum number of tail calls has been reached for this
679 * chain of programs. This limit is defined in the kernel by the
680 * macro **MAX_TAIL_CALL_CNT** (not accessible to user space),
681 * which is currently set to 32.
682 * Return
683 * 0 on success, or a negative error in case of failure.
684 *
685 * int bpf_clone_redirect(struct sk_buff *skb, u32 ifindex, u64 flags)
686 * Description
687 * Clone and redirect the packet associated to *skb* to another
688 * net device of index *ifindex*. Both ingress and egress
689 * interfaces can be used for redirection. The **BPF_F_INGRESS**
690 * value in *flags* is used to make the distinction (ingress path
691 * is selected if the flag is present, egress path otherwise).
692 * This is the only flag supported for now.
693 *
694 * In comparison with **bpf_redirect**\ () helper,
695 * **bpf_clone_redirect**\ () has the associated cost of
696 * duplicating the packet buffer, but this can be executed out of
697 * the eBPF program. Conversely, **bpf_redirect**\ () is more
698 * efficient, but it is handled through an action code where the
699 * redirection happens only after the eBPF program has returned.
700 *
701 * A call to this helper is susceptible to change the underlaying
702 * packet buffer. Therefore, at load time, all checks on pointers
703 * previously done by the verifier are invalidated and must be
704 * performed again, if the helper is used in combination with
705 * direct packet access.
706 * Return
707 * 0 on success, or a negative error in case of failure.
Quentin Monnetc456dec2018-04-25 18:16:54 +0100708 *
709 * u64 bpf_get_current_pid_tgid(void)
710 * Return
711 * A 64-bit integer containing the current tgid and pid, and
712 * created as such:
713 * *current_task*\ **->tgid << 32 \|**
714 * *current_task*\ **->pid**.
715 *
716 * u64 bpf_get_current_uid_gid(void)
717 * Return
718 * A 64-bit integer containing the current GID and UID, and
719 * created as such: *current_gid* **<< 32 \|** *current_uid*.
720 *
721 * int bpf_get_current_comm(char *buf, u32 size_of_buf)
722 * Description
723 * Copy the **comm** attribute of the current task into *buf* of
724 * *size_of_buf*. The **comm** attribute contains the name of
725 * the executable (excluding the path) for the current task. The
726 * *size_of_buf* must be strictly positive. On success, the
727 * helper makes sure that the *buf* is NUL-terminated. On failure,
728 * it is filled with zeroes.
729 * Return
730 * 0 on success, or a negative error in case of failure.
731 *
Quentin Monnet1fdd08b2018-04-25 18:16:55 +0100732 * u32 bpf_get_cgroup_classid(struct sk_buff *skb)
733 * Description
734 * Retrieve the classid for the current task, i.e. for the net_cls
735 * cgroup to which *skb* belongs.
736 *
737 * This helper can be used on TC egress path, but not on ingress.
738 *
739 * The net_cls cgroup provides an interface to tag network packets
740 * based on a user-provided identifier for all traffic coming from
741 * the tasks belonging to the related cgroup. See also the related
742 * kernel documentation, available from the Linux sources in file
743 * *Documentation/cgroup-v1/net_cls.txt*.
744 *
745 * The Linux kernel has two versions for cgroups: there are
746 * cgroups v1 and cgroups v2. Both are available to users, who can
747 * use a mixture of them, but note that the net_cls cgroup is for
748 * cgroup v1 only. This makes it incompatible with BPF programs
749 * run on cgroups, which is a cgroup-v2-only feature (a socket can
750 * only hold data for one version of cgroups at a time).
751 *
752 * This helper is only available is the kernel was compiled with
753 * the **CONFIG_CGROUP_NET_CLASSID** configuration option set to
754 * "**y**" or to "**m**".
755 * Return
756 * The classid, or 0 for the default unconfigured classid.
757 *
Quentin Monnetc456dec2018-04-25 18:16:54 +0100758 * int bpf_skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
759 * Description
760 * Push a *vlan_tci* (VLAN tag control information) of protocol
761 * *vlan_proto* to the packet associated to *skb*, then update
762 * the checksum. Note that if *vlan_proto* is different from
763 * **ETH_P_8021Q** and **ETH_P_8021AD**, it is considered to
764 * be **ETH_P_8021Q**.
765 *
766 * A call to this helper is susceptible to change the underlaying
767 * packet buffer. Therefore, at load time, all checks on pointers
768 * previously done by the verifier are invalidated and must be
769 * performed again, if the helper is used in combination with
770 * direct packet access.
771 * Return
772 * 0 on success, or a negative error in case of failure.
773 *
774 * int bpf_skb_vlan_pop(struct sk_buff *skb)
775 * Description
776 * Pop a VLAN header from the packet associated to *skb*.
777 *
778 * A call to this helper is susceptible to change the underlaying
779 * packet buffer. Therefore, at load time, all checks on pointers
780 * previously done by the verifier are invalidated and must be
781 * performed again, if the helper is used in combination with
782 * direct packet access.
783 * Return
784 * 0 on success, or a negative error in case of failure.
785 *
786 * int bpf_skb_get_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags)
787 * Description
788 * Get tunnel metadata. This helper takes a pointer *key* to an
789 * empty **struct bpf_tunnel_key** of **size**, that will be
790 * filled with tunnel metadata for the packet associated to *skb*.
791 * The *flags* can be set to **BPF_F_TUNINFO_IPV6**, which
792 * indicates that the tunnel is based on IPv6 protocol instead of
793 * IPv4.
794 *
795 * The **struct bpf_tunnel_key** is an object that generalizes the
796 * principal parameters used by various tunneling protocols into a
797 * single struct. This way, it can be used to easily make a
798 * decision based on the contents of the encapsulation header,
799 * "summarized" in this struct. In particular, it holds the IP
800 * address of the remote end (IPv4 or IPv6, depending on the case)
801 * in *key*\ **->remote_ipv4** or *key*\ **->remote_ipv6**. Also,
802 * this struct exposes the *key*\ **->tunnel_id**, which is
803 * generally mapped to a VNI (Virtual Network Identifier), making
804 * it programmable together with the **bpf_skb_set_tunnel_key**\
805 * () helper.
806 *
807 * Let's imagine that the following code is part of a program
808 * attached to the TC ingress interface, on one end of a GRE
809 * tunnel, and is supposed to filter out all messages coming from
810 * remote ends with IPv4 address other than 10.0.0.1:
811 *
812 * ::
813 *
814 * int ret;
815 * struct bpf_tunnel_key key = {};
816 *
817 * ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
818 * if (ret < 0)
819 * return TC_ACT_SHOT; // drop packet
820 *
821 * if (key.remote_ipv4 != 0x0a000001)
822 * return TC_ACT_SHOT; // drop packet
823 *
824 * return TC_ACT_OK; // accept packet
825 *
826 * This interface can also be used with all encapsulation devices
827 * that can operate in "collect metadata" mode: instead of having
828 * one network device per specific configuration, the "collect
829 * metadata" mode only requires a single device where the
830 * configuration can be extracted from this helper.
831 *
832 * This can be used together with various tunnels such as VXLan,
833 * Geneve, GRE or IP in IP (IPIP).
834 * Return
835 * 0 on success, or a negative error in case of failure.
836 *
837 * int bpf_skb_set_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags)
838 * Description
839 * Populate tunnel metadata for packet associated to *skb.* The
840 * tunnel metadata is set to the contents of *key*, of *size*. The
841 * *flags* can be set to a combination of the following values:
842 *
843 * **BPF_F_TUNINFO_IPV6**
844 * Indicate that the tunnel is based on IPv6 protocol
845 * instead of IPv4.
846 * **BPF_F_ZERO_CSUM_TX**
847 * For IPv4 packets, add a flag to tunnel metadata
848 * indicating that checksum computation should be skipped
849 * and checksum set to zeroes.
850 * **BPF_F_DONT_FRAGMENT**
851 * Add a flag to tunnel metadata indicating that the
852 * packet should not be fragmented.
853 * **BPF_F_SEQ_NUMBER**
854 * Add a flag to tunnel metadata indicating that a
855 * sequence number should be added to tunnel header before
856 * sending the packet. This flag was added for GRE
857 * encapsulation, but might be used with other protocols
858 * as well in the future.
859 *
860 * Here is a typical usage on the transmit path:
861 *
862 * ::
863 *
864 * struct bpf_tunnel_key key;
865 * populate key ...
866 * bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0);
867 * bpf_clone_redirect(skb, vxlan_dev_ifindex, 0);
868 *
869 * See also the description of the **bpf_skb_get_tunnel_key**\ ()
870 * helper for additional information.
871 * Return
872 * 0 on success, or a negative error in case of failure.
873 *
Quentin Monnetc6b5fb82018-04-25 18:16:57 +0100874 * u64 bpf_perf_event_read(struct bpf_map *map, u64 flags)
875 * Description
876 * Read the value of a perf event counter. This helper relies on a
877 * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of
878 * the perf event counter is selected when *map* is updated with
879 * perf event file descriptors. The *map* is an array whose size
880 * is the number of available CPUs, and each cell contains a value
881 * relative to one CPU. The value to retrieve is indicated by
882 * *flags*, that contains the index of the CPU to look up, masked
883 * with **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to
884 * **BPF_F_CURRENT_CPU** to indicate that the value for the
885 * current CPU should be retrieved.
886 *
887 * Note that before Linux 4.13, only hardware perf event can be
888 * retrieved.
889 *
890 * Also, be aware that the newer helper
891 * **bpf_perf_event_read_value**\ () is recommended over
Quentin Monnet3bd5a092018-04-30 11:39:03 +0100892 * **bpf_perf_event_read**\ () in general. The latter has some ABI
Quentin Monnetc6b5fb82018-04-25 18:16:57 +0100893 * quirks where error and counter value are used as a return code
894 * (which is wrong to do since ranges may overlap). This issue is
Quentin Monnet3bd5a092018-04-30 11:39:03 +0100895 * fixed with **bpf_perf_event_read_value**\ (), which at the same
896 * time provides more features over the **bpf_perf_event_read**\
897 * () interface. Please refer to the description of
Quentin Monnetc6b5fb82018-04-25 18:16:57 +0100898 * **bpf_perf_event_read_value**\ () for details.
899 * Return
900 * The value of the perf event counter read from the map, or a
901 * negative error code in case of failure.
902 *
Quentin Monnetc456dec2018-04-25 18:16:54 +0100903 * int bpf_redirect(u32 ifindex, u64 flags)
904 * Description
905 * Redirect the packet to another net device of index *ifindex*.
906 * This helper is somewhat similar to **bpf_clone_redirect**\
907 * (), except that the packet is not cloned, which provides
908 * increased performance.
909 *
910 * Except for XDP, both ingress and egress interfaces can be used
911 * for redirection. The **BPF_F_INGRESS** value in *flags* is used
912 * to make the distinction (ingress path is selected if the flag
913 * is present, egress path otherwise). Currently, XDP only
914 * supports redirection to the egress interface, and accepts no
915 * flag at all.
916 *
917 * The same effect can be attained with the more generic
918 * **bpf_redirect_map**\ (), which requires specific maps to be
919 * used but offers better performance.
920 * Return
921 * For XDP, the helper returns **XDP_REDIRECT** on success or
922 * **XDP_ABORTED** on error. For other program types, the values
923 * are **TC_ACT_REDIRECT** on success or **TC_ACT_SHOT** on
924 * error.
925 *
Quentin Monnet1fdd08b2018-04-25 18:16:55 +0100926 * u32 bpf_get_route_realm(struct sk_buff *skb)
927 * Description
928 * Retrieve the realm or the route, that is to say the
929 * **tclassid** field of the destination for the *skb*. The
930 * indentifier retrieved is a user-provided tag, similar to the
931 * one used with the net_cls cgroup (see description for
932 * **bpf_get_cgroup_classid**\ () helper), but here this tag is
933 * held by a route (a destination entry), not by a task.
934 *
935 * Retrieving this identifier works with the clsact TC egress hook
936 * (see also **tc-bpf(8)**), or alternatively on conventional
937 * classful egress qdiscs, but not on TC ingress path. In case of
938 * clsact TC egress hook, this has the advantage that, internally,
939 * the destination entry has not been dropped yet in the transmit
940 * path. Therefore, the destination entry does not need to be
941 * artificially held via **netif_keep_dst**\ () for a classful
942 * qdisc until the *skb* is freed.
943 *
944 * This helper is available only if the kernel was compiled with
945 * **CONFIG_IP_ROUTE_CLASSID** configuration option.
946 * Return
947 * The realm of the route for the packet associated to *skb*, or 0
948 * if none was found.
949 *
Quentin Monnetc456dec2018-04-25 18:16:54 +0100950 * int bpf_perf_event_output(struct pt_reg *ctx, struct bpf_map *map, u64 flags, void *data, u64 size)
951 * Description
952 * Write raw *data* blob into a special BPF perf event held by
953 * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf
954 * event must have the following attributes: **PERF_SAMPLE_RAW**
955 * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and
956 * **PERF_COUNT_SW_BPF_OUTPUT** as **config**.
957 *
958 * The *flags* are used to indicate the index in *map* for which
959 * the value must be put, masked with **BPF_F_INDEX_MASK**.
960 * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU**
961 * to indicate that the index of the current CPU core should be
962 * used.
963 *
964 * The value to write, of *size*, is passed through eBPF stack and
965 * pointed by *data*.
966 *
967 * The context of the program *ctx* needs also be passed to the
968 * helper.
969 *
970 * On user space, a program willing to read the values needs to
971 * call **perf_event_open**\ () on the perf event (either for
972 * one or for all CPUs) and to store the file descriptor into the
973 * *map*. This must be done before the eBPF program can send data
974 * into it. An example is available in file
975 * *samples/bpf/trace_output_user.c* in the Linux kernel source
976 * tree (the eBPF program counterpart is in
977 * *samples/bpf/trace_output_kern.c*).
978 *
979 * **bpf_perf_event_output**\ () achieves better performance
980 * than **bpf_trace_printk**\ () for sharing data with user
981 * space, and is much better suitable for streaming data from eBPF
982 * programs.
983 *
984 * Note that this helper is not restricted to tracing use cases
985 * and can be used with programs attached to TC or XDP as well,
986 * where it allows for passing data to user space listeners. Data
987 * can be:
988 *
989 * * Only custom structs,
990 * * Only the packet payload, or
991 * * A combination of both.
992 * Return
993 * 0 on success, or a negative error in case of failure.
994 *
Quentin Monnet1fdd08b2018-04-25 18:16:55 +0100995 * int bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len)
996 * Description
997 * This helper was provided as an easy way to load data from a
998 * packet. It can be used to load *len* bytes from *offset* from
999 * the packet associated to *skb*, into the buffer pointed by
1000 * *to*.
1001 *
1002 * Since Linux 4.7, usage of this helper has mostly been replaced
1003 * by "direct packet access", enabling packet data to be
1004 * manipulated with *skb*\ **->data** and *skb*\ **->data_end**
1005 * pointing respectively to the first byte of packet data and to
1006 * the byte after the last byte of packet data. However, it
1007 * remains useful if one wishes to read large quantities of data
1008 * at once from a packet into the eBPF stack.
1009 * Return
1010 * 0 on success, or a negative error in case of failure.
1011 *
Quentin Monnetc456dec2018-04-25 18:16:54 +01001012 * int bpf_get_stackid(struct pt_reg *ctx, struct bpf_map *map, u64 flags)
1013 * Description
1014 * Walk a user or a kernel stack and return its id. To achieve
1015 * this, the helper needs *ctx*, which is a pointer to the context
1016 * on which the tracing program is executed, and a pointer to a
1017 * *map* of type **BPF_MAP_TYPE_STACK_TRACE**.
1018 *
1019 * The last argument, *flags*, holds the number of stack frames to
1020 * skip (from 0 to 255), masked with
1021 * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set
1022 * a combination of the following flags:
1023 *
1024 * **BPF_F_USER_STACK**
1025 * Collect a user space stack instead of a kernel stack.
1026 * **BPF_F_FAST_STACK_CMP**
1027 * Compare stacks by hash only.
1028 * **BPF_F_REUSE_STACKID**
1029 * If two different stacks hash into the same *stackid*,
1030 * discard the old one.
1031 *
1032 * The stack id retrieved is a 32 bit long integer handle which
1033 * can be further combined with other data (including other stack
1034 * ids) and used as a key into maps. This can be useful for
1035 * generating a variety of graphs (such as flame graphs or off-cpu
1036 * graphs).
1037 *
1038 * For walking a stack, this helper is an improvement over
1039 * **bpf_probe_read**\ (), which can be used with unrolled loops
1040 * but is not efficient and consumes a lot of eBPF instructions.
1041 * Instead, **bpf_get_stackid**\ () can collect up to
1042 * **PERF_MAX_STACK_DEPTH** both kernel and user frames. Note that
1043 * this limit can be controlled with the **sysctl** program, and
1044 * that it should be manually increased in order to profile long
1045 * user stacks (such as stacks for Java programs). To do so, use:
1046 *
1047 * ::
1048 *
1049 * # sysctl kernel.perf_event_max_stack=<new value>
Quentin Monnetc456dec2018-04-25 18:16:54 +01001050 * Return
1051 * The positive or null stack id on success, or a negative error
1052 * in case of failure.
1053 *
Quentin Monnet1fdd08b2018-04-25 18:16:55 +01001054 * s64 bpf_csum_diff(__be32 *from, u32 from_size, __be32 *to, u32 to_size, __wsum seed)
1055 * Description
1056 * Compute a checksum difference, from the raw buffer pointed by
1057 * *from*, of length *from_size* (that must be a multiple of 4),
1058 * towards the raw buffer pointed by *to*, of size *to_size*
1059 * (same remark). An optional *seed* can be added to the value
1060 * (this can be cascaded, the seed may come from a previous call
1061 * to the helper).
1062 *
1063 * This is flexible enough to be used in several ways:
1064 *
1065 * * With *from_size* == 0, *to_size* > 0 and *seed* set to
1066 * checksum, it can be used when pushing new data.
1067 * * With *from_size* > 0, *to_size* == 0 and *seed* set to
1068 * checksum, it can be used when removing data from a packet.
1069 * * With *from_size* > 0, *to_size* > 0 and *seed* set to 0, it
1070 * can be used to compute a diff. Note that *from_size* and
1071 * *to_size* do not need to be equal.
1072 *
1073 * This helper can be used in combination with
1074 * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ (), to
1075 * which one can feed in the difference computed with
1076 * **bpf_csum_diff**\ ().
1077 * Return
1078 * The checksum result, or a negative error code in case of
1079 * failure.
1080 *
1081 * int bpf_skb_get_tunnel_opt(struct sk_buff *skb, u8 *opt, u32 size)
1082 * Description
1083 * Retrieve tunnel options metadata for the packet associated to
1084 * *skb*, and store the raw tunnel option data to the buffer *opt*
1085 * of *size*.
1086 *
1087 * This helper can be used with encapsulation devices that can
1088 * operate in "collect metadata" mode (please refer to the related
1089 * note in the description of **bpf_skb_get_tunnel_key**\ () for
1090 * more details). A particular example where this can be used is
1091 * in combination with the Geneve encapsulation protocol, where it
1092 * allows for pushing (with **bpf_skb_get_tunnel_opt**\ () helper)
1093 * and retrieving arbitrary TLVs (Type-Length-Value headers) from
1094 * the eBPF program. This allows for full customization of these
1095 * headers.
1096 * Return
1097 * The size of the option data retrieved.
1098 *
1099 * int bpf_skb_set_tunnel_opt(struct sk_buff *skb, u8 *opt, u32 size)
1100 * Description
1101 * Set tunnel options metadata for the packet associated to *skb*
1102 * to the option data contained in the raw buffer *opt* of *size*.
1103 *
1104 * See also the description of the **bpf_skb_get_tunnel_opt**\ ()
1105 * helper for additional information.
1106 * Return
1107 * 0 on success, or a negative error in case of failure.
1108 *
1109 * int bpf_skb_change_proto(struct sk_buff *skb, __be16 proto, u64 flags)
1110 * Description
1111 * Change the protocol of the *skb* to *proto*. Currently
1112 * supported are transition from IPv4 to IPv6, and from IPv6 to
1113 * IPv4. The helper takes care of the groundwork for the
1114 * transition, including resizing the socket buffer. The eBPF
1115 * program is expected to fill the new headers, if any, via
1116 * **skb_store_bytes**\ () and to recompute the checksums with
1117 * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\
1118 * (). The main case for this helper is to perform NAT64
1119 * operations out of an eBPF program.
1120 *
1121 * Internally, the GSO type is marked as dodgy so that headers are
1122 * checked and segments are recalculated by the GSO/GRO engine.
1123 * The size for GSO target is adapted as well.
1124 *
1125 * All values for *flags* are reserved for future usage, and must
1126 * be left at zero.
1127 *
1128 * A call to this helper is susceptible to change the underlaying
1129 * packet buffer. Therefore, at load time, all checks on pointers
1130 * previously done by the verifier are invalidated and must be
1131 * performed again, if the helper is used in combination with
1132 * direct packet access.
1133 * Return
1134 * 0 on success, or a negative error in case of failure.
1135 *
1136 * int bpf_skb_change_type(struct sk_buff *skb, u32 type)
1137 * Description
1138 * Change the packet type for the packet associated to *skb*. This
1139 * comes down to setting *skb*\ **->pkt_type** to *type*, except
1140 * the eBPF program does not have a write access to *skb*\
1141 * **->pkt_type** beside this helper. Using a helper here allows
1142 * for graceful handling of errors.
1143 *
1144 * The major use case is to change incoming *skb*s to
1145 * **PACKET_HOST** in a programmatic way instead of having to
1146 * recirculate via **redirect**\ (..., **BPF_F_INGRESS**), for
1147 * example.
1148 *
1149 * Note that *type* only allows certain values. At this time, they
1150 * are:
1151 *
1152 * **PACKET_HOST**
1153 * Packet is for us.
1154 * **PACKET_BROADCAST**
1155 * Send packet to all.
1156 * **PACKET_MULTICAST**
1157 * Send packet to group.
1158 * **PACKET_OTHERHOST**
1159 * Send packet to someone else.
1160 * Return
1161 * 0 on success, or a negative error in case of failure.
1162 *
Quentin Monnetc6b5fb82018-04-25 18:16:57 +01001163 * int bpf_skb_under_cgroup(struct sk_buff *skb, struct bpf_map *map, u32 index)
1164 * Description
1165 * Check whether *skb* is a descendant of the cgroup2 held by
1166 * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*.
1167 * Return
1168 * The return value depends on the result of the test, and can be:
1169 *
1170 * * 0, if the *skb* failed the cgroup2 descendant test.
1171 * * 1, if the *skb* succeeded the cgroup2 descendant test.
1172 * * A negative error code, if an error occurred.
1173 *
Quentin Monnetfa156012018-04-25 18:16:56 +01001174 * u32 bpf_get_hash_recalc(struct sk_buff *skb)
1175 * Description
1176 * Retrieve the hash of the packet, *skb*\ **->hash**. If it is
1177 * not set, in particular if the hash was cleared due to mangling,
1178 * recompute this hash. Later accesses to the hash can be done
1179 * directly with *skb*\ **->hash**.
1180 *
1181 * Calling **bpf_set_hash_invalid**\ (), changing a packet
1182 * prototype with **bpf_skb_change_proto**\ (), or calling
1183 * **bpf_skb_store_bytes**\ () with the
1184 * **BPF_F_INVALIDATE_HASH** are actions susceptible to clear
1185 * the hash and to trigger a new computation for the next call to
1186 * **bpf_get_hash_recalc**\ ().
1187 * Return
1188 * The 32-bit hash.
1189 *
Quentin Monnetc456dec2018-04-25 18:16:54 +01001190 * u64 bpf_get_current_task(void)
1191 * Return
1192 * A pointer to the current task struct.
Quentin Monnetfa156012018-04-25 18:16:56 +01001193 *
Quentin Monnetc6b5fb82018-04-25 18:16:57 +01001194 * int bpf_probe_write_user(void *dst, const void *src, u32 len)
1195 * Description
1196 * Attempt in a safe way to write *len* bytes from the buffer
1197 * *src* to *dst* in memory. It only works for threads that are in
1198 * user context, and *dst* must be a valid user space address.
1199 *
1200 * This helper should not be used to implement any kind of
1201 * security mechanism because of TOC-TOU attacks, but rather to
1202 * debug, divert, and manipulate execution of semi-cooperative
1203 * processes.
1204 *
1205 * Keep in mind that this feature is meant for experiments, and it
1206 * has a risk of crashing the system and running programs.
1207 * Therefore, when an eBPF program using this helper is attached,
1208 * a warning including PID and process name is printed to kernel
1209 * logs.
1210 * Return
1211 * 0 on success, or a negative error in case of failure.
1212 *
1213 * int bpf_current_task_under_cgroup(struct bpf_map *map, u32 index)
1214 * Description
1215 * Check whether the probe is being run is the context of a given
1216 * subset of the cgroup2 hierarchy. The cgroup2 to test is held by
1217 * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*.
1218 * Return
1219 * The return value depends on the result of the test, and can be:
1220 *
1221 * * 0, if the *skb* task belongs to the cgroup2.
1222 * * 1, if the *skb* task does not belong to the cgroup2.
1223 * * A negative error code, if an error occurred.
1224 *
Quentin Monnetfa156012018-04-25 18:16:56 +01001225 * int bpf_skb_change_tail(struct sk_buff *skb, u32 len, u64 flags)
1226 * Description
1227 * Resize (trim or grow) the packet associated to *skb* to the
1228 * new *len*. The *flags* are reserved for future usage, and must
1229 * be left at zero.
1230 *
1231 * The basic idea is that the helper performs the needed work to
1232 * change the size of the packet, then the eBPF program rewrites
1233 * the rest via helpers like **bpf_skb_store_bytes**\ (),
1234 * **bpf_l3_csum_replace**\ (), **bpf_l3_csum_replace**\ ()
1235 * and others. This helper is a slow path utility intended for
1236 * replies with control messages. And because it is targeted for
1237 * slow path, the helper itself can afford to be slow: it
1238 * implicitly linearizes, unclones and drops offloads from the
1239 * *skb*.
1240 *
1241 * A call to this helper is susceptible to change the underlaying
1242 * packet buffer. Therefore, at load time, all checks on pointers
1243 * previously done by the verifier are invalidated and must be
1244 * performed again, if the helper is used in combination with
1245 * direct packet access.
1246 * Return
1247 * 0 on success, or a negative error in case of failure.
1248 *
1249 * int bpf_skb_pull_data(struct sk_buff *skb, u32 len)
1250 * Description
1251 * Pull in non-linear data in case the *skb* is non-linear and not
1252 * all of *len* are part of the linear section. Make *len* bytes
1253 * from *skb* readable and writable. If a zero value is passed for
1254 * *len*, then the whole length of the *skb* is pulled.
1255 *
1256 * This helper is only needed for reading and writing with direct
1257 * packet access.
1258 *
1259 * For direct packet access, testing that offsets to access
1260 * are within packet boundaries (test on *skb*\ **->data_end**) is
1261 * susceptible to fail if offsets are invalid, or if the requested
1262 * data is in non-linear parts of the *skb*. On failure the
1263 * program can just bail out, or in the case of a non-linear
1264 * buffer, use a helper to make the data available. The
1265 * **bpf_skb_load_bytes**\ () helper is a first solution to access
1266 * the data. Another one consists in using **bpf_skb_pull_data**
1267 * to pull in once the non-linear parts, then retesting and
1268 * eventually access the data.
1269 *
1270 * At the same time, this also makes sure the *skb* is uncloned,
1271 * which is a necessary condition for direct write. As this needs
1272 * to be an invariant for the write part only, the verifier
1273 * detects writes and adds a prologue that is calling
1274 * **bpf_skb_pull_data()** to effectively unclone the *skb* from
1275 * the very beginning in case it is indeed cloned.
1276 *
1277 * A call to this helper is susceptible to change the underlaying
1278 * packet buffer. Therefore, at load time, all checks on pointers
1279 * previously done by the verifier are invalidated and must be
1280 * performed again, if the helper is used in combination with
1281 * direct packet access.
1282 * Return
1283 * 0 on success, or a negative error in case of failure.
1284 *
1285 * s64 bpf_csum_update(struct sk_buff *skb, __wsum csum)
1286 * Description
1287 * Add the checksum *csum* into *skb*\ **->csum** in case the
1288 * driver has supplied a checksum for the entire packet into that
1289 * field. Return an error otherwise. This helper is intended to be
1290 * used in combination with **bpf_csum_diff**\ (), in particular
1291 * when the checksum needs to be updated after data has been
1292 * written into the packet through direct packet access.
1293 * Return
1294 * The checksum on success, or a negative error code in case of
1295 * failure.
1296 *
1297 * void bpf_set_hash_invalid(struct sk_buff *skb)
1298 * Description
1299 * Invalidate the current *skb*\ **->hash**. It can be used after
1300 * mangling on headers through direct packet access, in order to
1301 * indicate that the hash is outdated and to trigger a
1302 * recalculation the next time the kernel tries to access this
1303 * hash or when the **bpf_get_hash_recalc**\ () helper is called.
1304 *
1305 * int bpf_get_numa_node_id(void)
1306 * Description
1307 * Return the id of the current NUMA node. The primary use case
1308 * for this helper is the selection of sockets for the local NUMA
1309 * node, when the program is attached to sockets using the
1310 * **SO_ATTACH_REUSEPORT_EBPF** option (see also **socket(7)**),
1311 * but the helper is also available to other eBPF program types,
1312 * similarly to **bpf_get_smp_processor_id**\ ().
1313 * Return
1314 * The id of current NUMA node.
1315 *
Quentin Monnetc6b5fb82018-04-25 18:16:57 +01001316 * int bpf_skb_change_head(struct sk_buff *skb, u32 len, u64 flags)
1317 * Description
1318 * Grows headroom of packet associated to *skb* and adjusts the
1319 * offset of the MAC header accordingly, adding *len* bytes of
1320 * space. It automatically extends and reallocates memory as
1321 * required.
1322 *
1323 * This helper can be used on a layer 3 *skb* to push a MAC header
1324 * for redirection into a layer 2 device.
1325 *
1326 * All values for *flags* are reserved for future usage, and must
1327 * be left at zero.
1328 *
1329 * A call to this helper is susceptible to change the underlaying
1330 * packet buffer. Therefore, at load time, all checks on pointers
1331 * previously done by the verifier are invalidated and must be
1332 * performed again, if the helper is used in combination with
1333 * direct packet access.
1334 * Return
1335 * 0 on success, or a negative error in case of failure.
1336 *
1337 * int bpf_xdp_adjust_head(struct xdp_buff *xdp_md, int delta)
1338 * Description
1339 * Adjust (move) *xdp_md*\ **->data** by *delta* bytes. Note that
1340 * it is possible to use a negative value for *delta*. This helper
1341 * can be used to prepare the packet for pushing or popping
1342 * headers.
1343 *
1344 * A call to this helper is susceptible to change the underlaying
1345 * packet buffer. Therefore, at load time, all checks on pointers
1346 * previously done by the verifier are invalidated and must be
1347 * performed again, if the helper is used in combination with
1348 * direct packet access.
1349 * Return
1350 * 0 on success, or a negative error in case of failure.
1351 *
1352 * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
1353 * Description
1354 * Copy a NUL terminated string from an unsafe address
1355 * *unsafe_ptr* to *dst*. The *size* should include the
1356 * terminating NUL byte. In case the string length is smaller than
1357 * *size*, the target is not padded with further NUL bytes. If the
1358 * string length is larger than *size*, just *size*-1 bytes are
1359 * copied and the last byte is set to NUL.
1360 *
1361 * On success, the length of the copied string is returned. This
1362 * makes this helper useful in tracing programs for reading
1363 * strings, and more importantly to get its length at runtime. See
1364 * the following snippet:
1365 *
1366 * ::
1367 *
1368 * SEC("kprobe/sys_open")
1369 * void bpf_sys_open(struct pt_regs *ctx)
1370 * {
1371 * char buf[PATHLEN]; // PATHLEN is defined to 256
1372 * int res = bpf_probe_read_str(buf, sizeof(buf),
1373 * ctx->di);
1374 *
1375 * // Consume buf, for example push it to
1376 * // userspace via bpf_perf_event_output(); we
1377 * // can use res (the string length) as event
1378 * // size, after checking its boundaries.
1379 * }
1380 *
1381 * In comparison, using **bpf_probe_read()** helper here instead
1382 * to read the string would require to estimate the length at
1383 * compile time, and would often result in copying more memory
1384 * than necessary.
1385 *
1386 * Another useful use case is when parsing individual process
1387 * arguments or individual environment variables navigating
1388 * *current*\ **->mm->arg_start** and *current*\
1389 * **->mm->env_start**: using this helper and the return value,
1390 * one can quickly iterate at the right offset of the memory area.
1391 * Return
1392 * On success, the strictly positive length of the string,
1393 * including the trailing NUL character. On error, a negative
1394 * value.
1395 *
1396 * u64 bpf_get_socket_cookie(struct sk_buff *skb)
1397 * Description
1398 * If the **struct sk_buff** pointed by *skb* has a known socket,
1399 * retrieve the cookie (generated by the kernel) of this socket.
1400 * If no cookie has been set yet, generate a new cookie. Once
1401 * generated, the socket cookie remains stable for the life of the
1402 * socket. This helper can be useful for monitoring per socket
1403 * networking traffic statistics as it provides a unique socket
1404 * identifier per namespace.
1405 * Return
1406 * A 8-byte long non-decreasing number on success, or 0 if the
1407 * socket field is missing inside *skb*.
1408 *
Andrey Ignatovd692f112018-07-30 17:42:28 -07001409 * u64 bpf_get_socket_cookie(struct bpf_sock_addr *ctx)
1410 * Description
1411 * Equivalent to bpf_get_socket_cookie() helper that accepts
1412 * *skb*, but gets socket from **struct bpf_sock_addr** contex.
1413 * Return
1414 * A 8-byte long non-decreasing number.
1415 *
1416 * u64 bpf_get_socket_cookie(struct bpf_sock_ops *ctx)
1417 * Description
1418 * Equivalent to bpf_get_socket_cookie() helper that accepts
1419 * *skb*, but gets socket from **struct bpf_sock_ops** contex.
1420 * Return
1421 * A 8-byte long non-decreasing number.
1422 *
Quentin Monnetc6b5fb82018-04-25 18:16:57 +01001423 * u32 bpf_get_socket_uid(struct sk_buff *skb)
1424 * Return
1425 * The owner UID of the socket associated to *skb*. If the socket
1426 * is **NULL**, or if it is not a full socket (i.e. if it is a
1427 * time-wait or a request socket instead), **overflowuid** value
1428 * is returned (note that **overflowuid** might also be the actual
1429 * UID value for the socket).
1430 *
Quentin Monnetfa156012018-04-25 18:16:56 +01001431 * u32 bpf_set_hash(struct sk_buff *skb, u32 hash)
1432 * Description
1433 * Set the full hash for *skb* (set the field *skb*\ **->hash**)
1434 * to value *hash*.
1435 * Return
1436 * 0
1437 *
Andrey Ignatova3ef8e92018-04-28 16:06:19 -07001438 * int bpf_setsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, char *optval, int optlen)
Quentin Monnet7aa79a82018-04-25 18:16:58 +01001439 * Description
1440 * Emulate a call to **setsockopt()** on the socket associated to
1441 * *bpf_socket*, which must be a full socket. The *level* at
1442 * which the option resides and the name *optname* of the option
1443 * must be specified, see **setsockopt(2)** for more information.
1444 * The option value of length *optlen* is pointed by *optval*.
1445 *
1446 * This helper actually implements a subset of **setsockopt()**.
1447 * It supports the following *level*\ s:
1448 *
1449 * * **SOL_SOCKET**, which supports the following *optname*\ s:
1450 * **SO_RCVBUF**, **SO_SNDBUF**, **SO_MAX_PACING_RATE**,
1451 * **SO_PRIORITY**, **SO_RCVLOWAT**, **SO_MARK**.
1452 * * **IPPROTO_TCP**, which supports the following *optname*\ s:
1453 * **TCP_CONGESTION**, **TCP_BPF_IW**,
1454 * **TCP_BPF_SNDCWND_CLAMP**.
1455 * * **IPPROTO_IP**, which supports *optname* **IP_TOS**.
1456 * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**.
1457 * Return
1458 * 0 on success, or a negative error in case of failure.
1459 *
Nicolas Dichtelb55cbc82018-10-17 16:24:48 +02001460 * int bpf_skb_adjust_room(struct sk_buff *skb, s32 len_diff, u32 mode, u64 flags)
Quentin Monnetfa156012018-04-25 18:16:56 +01001461 * Description
1462 * Grow or shrink the room for data in the packet associated to
1463 * *skb* by *len_diff*, and according to the selected *mode*.
1464 *
1465 * There is a single supported mode at this time:
1466 *
1467 * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer
1468 * (room space is added or removed below the layer 3 header).
1469 *
1470 * All values for *flags* are reserved for future usage, and must
1471 * be left at zero.
1472 *
1473 * A call to this helper is susceptible to change the underlaying
1474 * packet buffer. Therefore, at load time, all checks on pointers
1475 * previously done by the verifier are invalidated and must be
1476 * performed again, if the helper is used in combination with
1477 * direct packet access.
1478 * Return
1479 * 0 on success, or a negative error in case of failure.
1480 *
Quentin Monnetab127042018-04-25 18:16:59 +01001481 * int bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags)
1482 * Description
1483 * Redirect the packet to the endpoint referenced by *map* at
1484 * index *key*. Depending on its type, this *map* can contain
1485 * references to net devices (for forwarding packets through other
1486 * ports), or to CPUs (for redirecting XDP frames to another CPU;
1487 * but this is only implemented for native XDP (with driver
1488 * support) as of this writing).
1489 *
1490 * All values for *flags* are reserved for future usage, and must
1491 * be left at zero.
1492 *
1493 * When used to redirect packets to net devices, this helper
1494 * provides a high performance increase over **bpf_redirect**\ ().
1495 * This is due to various implementation details of the underlying
1496 * mechanisms, one of which is the fact that **bpf_redirect_map**\
1497 * () tries to send packet as a "bulk" to the device.
1498 * Return
1499 * **XDP_REDIRECT** on success, or **XDP_ABORTED** on error.
1500 *
1501 * int bpf_sk_redirect_map(struct bpf_map *map, u32 key, u64 flags)
1502 * Description
1503 * Redirect the packet to the socket referenced by *map* (of type
1504 * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and
1505 * egress interfaces can be used for redirection. The
1506 * **BPF_F_INGRESS** value in *flags* is used to make the
1507 * distinction (ingress path is selected if the flag is present,
1508 * egress path otherwise). This is the only flag supported for now.
1509 * Return
1510 * **SK_PASS** on success, or **SK_DROP** on error.
1511 *
Andrey Ignatova3ef8e92018-04-28 16:06:19 -07001512 * int bpf_sock_map_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags)
Quentin Monnetab127042018-04-25 18:16:59 +01001513 * Description
1514 * Add an entry to, or update a *map* referencing sockets. The
1515 * *skops* is used as a new value for the entry associated to
1516 * *key*. *flags* is one of:
1517 *
1518 * **BPF_NOEXIST**
1519 * The entry for *key* must not exist in the map.
1520 * **BPF_EXIST**
1521 * The entry for *key* must already exist in the map.
1522 * **BPF_ANY**
1523 * No condition on the existence of the entry for *key*.
1524 *
1525 * If the *map* has eBPF programs (parser and verdict), those will
1526 * be inherited by the socket being added. If the socket is
1527 * already attached to eBPF programs, this results in an error.
1528 * Return
1529 * 0 on success, or a negative error in case of failure.
1530 *
Quentin Monnetfa156012018-04-25 18:16:56 +01001531 * int bpf_xdp_adjust_meta(struct xdp_buff *xdp_md, int delta)
1532 * Description
1533 * Adjust the address pointed by *xdp_md*\ **->data_meta** by
1534 * *delta* (which can be positive or negative). Note that this
1535 * operation modifies the address stored in *xdp_md*\ **->data**,
1536 * so the latter must be loaded only after the helper has been
1537 * called.
1538 *
1539 * The use of *xdp_md*\ **->data_meta** is optional and programs
1540 * are not required to use it. The rationale is that when the
1541 * packet is processed with XDP (e.g. as DoS filter), it is
1542 * possible to push further meta data along with it before passing
1543 * to the stack, and to give the guarantee that an ingress eBPF
1544 * program attached as a TC classifier on the same device can pick
1545 * this up for further post-processing. Since TC works with socket
1546 * buffers, it remains possible to set from XDP the **mark** or
1547 * **priority** pointers, or other pointers for the socket buffer.
1548 * Having this scratch space generic and programmable allows for
1549 * more flexibility as the user is free to store whatever meta
1550 * data they need.
1551 *
1552 * A call to this helper is susceptible to change the underlaying
1553 * packet buffer. Therefore, at load time, all checks on pointers
1554 * previously done by the verifier are invalidated and must be
1555 * performed again, if the helper is used in combination with
1556 * direct packet access.
1557 * Return
1558 * 0 on success, or a negative error in case of failure.
Quentin Monnet7aa79a82018-04-25 18:16:58 +01001559 *
1560 * int bpf_perf_event_read_value(struct bpf_map *map, u64 flags, struct bpf_perf_event_value *buf, u32 buf_size)
1561 * Description
1562 * Read the value of a perf event counter, and store it into *buf*
1563 * of size *buf_size*. This helper relies on a *map* of type
1564 * **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of the perf event
1565 * counter is selected when *map* is updated with perf event file
1566 * descriptors. The *map* is an array whose size is the number of
1567 * available CPUs, and each cell contains a value relative to one
1568 * CPU. The value to retrieve is indicated by *flags*, that
1569 * contains the index of the CPU to look up, masked with
1570 * **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to
1571 * **BPF_F_CURRENT_CPU** to indicate that the value for the
1572 * current CPU should be retrieved.
1573 *
1574 * This helper behaves in a way close to
1575 * **bpf_perf_event_read**\ () helper, save that instead of
1576 * just returning the value observed, it fills the *buf*
1577 * structure. This allows for additional data to be retrieved: in
1578 * particular, the enabled and running times (in *buf*\
1579 * **->enabled** and *buf*\ **->running**, respectively) are
1580 * copied. In general, **bpf_perf_event_read_value**\ () is
1581 * recommended over **bpf_perf_event_read**\ (), which has some
1582 * ABI issues and provides fewer functionalities.
1583 *
1584 * These values are interesting, because hardware PMU (Performance
1585 * Monitoring Unit) counters are limited resources. When there are
1586 * more PMU based perf events opened than available counters,
1587 * kernel will multiplex these events so each event gets certain
1588 * percentage (but not all) of the PMU time. In case that
1589 * multiplexing happens, the number of samples or counter value
1590 * will not reflect the case compared to when no multiplexing
1591 * occurs. This makes comparison between different runs difficult.
1592 * Typically, the counter value should be normalized before
1593 * comparing to other experiments. The usual normalization is done
1594 * as follows.
1595 *
1596 * ::
1597 *
1598 * normalized_counter = counter * t_enabled / t_running
1599 *
1600 * Where t_enabled is the time enabled for event and t_running is
1601 * the time running for event since last normalization. The
1602 * enabled and running times are accumulated since the perf event
1603 * open. To achieve scaling factor between two invocations of an
1604 * eBPF program, users can can use CPU id as the key (which is
1605 * typical for perf array usage model) to remember the previous
1606 * value and do the calculation inside the eBPF program.
1607 * Return
1608 * 0 on success, or a negative error in case of failure.
1609 *
Andrey Ignatova3ef8e92018-04-28 16:06:19 -07001610 * int bpf_perf_prog_read_value(struct bpf_perf_event_data *ctx, struct bpf_perf_event_value *buf, u32 buf_size)
Quentin Monnet7aa79a82018-04-25 18:16:58 +01001611 * Description
1612 * For en eBPF program attached to a perf event, retrieve the
1613 * value of the event counter associated to *ctx* and store it in
1614 * the structure pointed by *buf* and of size *buf_size*. Enabled
1615 * and running times are also stored in the structure (see
1616 * description of helper **bpf_perf_event_read_value**\ () for
1617 * more details).
1618 * Return
1619 * 0 on success, or a negative error in case of failure.
1620 *
Andrey Ignatova3ef8e92018-04-28 16:06:19 -07001621 * int bpf_getsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, char *optval, int optlen)
Quentin Monnet7aa79a82018-04-25 18:16:58 +01001622 * Description
1623 * Emulate a call to **getsockopt()** on the socket associated to
1624 * *bpf_socket*, which must be a full socket. The *level* at
1625 * which the option resides and the name *optname* of the option
1626 * must be specified, see **getsockopt(2)** for more information.
1627 * The retrieved value is stored in the structure pointed by
1628 * *opval* and of length *optlen*.
1629 *
1630 * This helper actually implements a subset of **getsockopt()**.
1631 * It supports the following *level*\ s:
1632 *
1633 * * **IPPROTO_TCP**, which supports *optname*
1634 * **TCP_CONGESTION**.
1635 * * **IPPROTO_IP**, which supports *optname* **IP_TOS**.
1636 * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**.
1637 * Return
1638 * 0 on success, or a negative error in case of failure.
1639 *
1640 * int bpf_override_return(struct pt_reg *regs, u64 rc)
1641 * Description
1642 * Used for error injection, this helper uses kprobes to override
1643 * the return value of the probed function, and to set it to *rc*.
1644 * The first argument is the context *regs* on which the kprobe
1645 * works.
1646 *
1647 * This helper works by setting setting the PC (program counter)
1648 * to an override function which is run in place of the original
1649 * probed function. This means the probed function is not run at
1650 * all. The replacement function just returns with the required
1651 * value.
1652 *
1653 * This helper has security implications, and thus is subject to
1654 * restrictions. It is only available if the kernel was compiled
1655 * with the **CONFIG_BPF_KPROBE_OVERRIDE** configuration
1656 * option, and in this case it only works on functions tagged with
1657 * **ALLOW_ERROR_INJECTION** in the kernel code.
1658 *
1659 * Also, the helper is only available for the architectures having
1660 * the CONFIG_FUNCTION_ERROR_INJECTION option. As of this writing,
1661 * x86 architecture is the only one to support this feature.
1662 * Return
1663 * 0
1664 *
Andrey Ignatova3ef8e92018-04-28 16:06:19 -07001665 * int bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *bpf_sock, int argval)
Quentin Monnet7aa79a82018-04-25 18:16:58 +01001666 * Description
1667 * Attempt to set the value of the **bpf_sock_ops_cb_flags** field
1668 * for the full TCP socket associated to *bpf_sock_ops* to
1669 * *argval*.
1670 *
1671 * The primary use of this field is to determine if there should
1672 * be calls to eBPF programs of type
1673 * **BPF_PROG_TYPE_SOCK_OPS** at various points in the TCP
1674 * code. A program of the same type can change its value, per
1675 * connection and as necessary, when the connection is
1676 * established. This field is directly accessible for reading, but
1677 * this helper must be used for updates in order to return an
1678 * error if an eBPF program tries to set a callback that is not
1679 * supported in the current kernel.
1680 *
1681 * The supported callback values that *argval* can combine are:
1682 *
1683 * * **BPF_SOCK_OPS_RTO_CB_FLAG** (retransmission time out)
1684 * * **BPF_SOCK_OPS_RETRANS_CB_FLAG** (retransmission)
1685 * * **BPF_SOCK_OPS_STATE_CB_FLAG** (TCP state change)
1686 *
1687 * Here are some examples of where one could call such eBPF
1688 * program:
1689 *
1690 * * When RTO fires.
1691 * * When a packet is retransmitted.
1692 * * When the connection terminates.
1693 * * When a packet is sent.
1694 * * When a packet is received.
1695 * Return
1696 * Code **-EINVAL** if the socket is not a full TCP socket;
1697 * otherwise, a positive number containing the bits that could not
1698 * be set is returned (which comes down to 0 if all bits were set
1699 * as required).
1700 *
Quentin Monnetab127042018-04-25 18:16:59 +01001701 * int bpf_msg_redirect_map(struct sk_msg_buff *msg, struct bpf_map *map, u32 key, u64 flags)
1702 * Description
1703 * This helper is used in programs implementing policies at the
1704 * socket level. If the message *msg* is allowed to pass (i.e. if
1705 * the verdict eBPF program returns **SK_PASS**), redirect it to
1706 * the socket referenced by *map* (of type
1707 * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and
1708 * egress interfaces can be used for redirection. The
1709 * **BPF_F_INGRESS** value in *flags* is used to make the
1710 * distinction (ingress path is selected if the flag is present,
1711 * egress path otherwise). This is the only flag supported for now.
1712 * Return
1713 * **SK_PASS** on success, or **SK_DROP** on error.
1714 *
1715 * int bpf_msg_apply_bytes(struct sk_msg_buff *msg, u32 bytes)
1716 * Description
1717 * For socket policies, apply the verdict of the eBPF program to
1718 * the next *bytes* (number of bytes) of message *msg*.
1719 *
1720 * For example, this helper can be used in the following cases:
1721 *
1722 * * A single **sendmsg**\ () or **sendfile**\ () system call
1723 * contains multiple logical messages that the eBPF program is
1724 * supposed to read and for which it should apply a verdict.
1725 * * An eBPF program only cares to read the first *bytes* of a
1726 * *msg*. If the message has a large payload, then setting up
1727 * and calling the eBPF program repeatedly for all bytes, even
1728 * though the verdict is already known, would create unnecessary
1729 * overhead.
1730 *
1731 * When called from within an eBPF program, the helper sets a
1732 * counter internal to the BPF infrastructure, that is used to
1733 * apply the last verdict to the next *bytes*. If *bytes* is
1734 * smaller than the current data being processed from a
1735 * **sendmsg**\ () or **sendfile**\ () system call, the first
1736 * *bytes* will be sent and the eBPF program will be re-run with
1737 * the pointer for start of data pointing to byte number *bytes*
1738 * **+ 1**. If *bytes* is larger than the current data being
1739 * processed, then the eBPF verdict will be applied to multiple
1740 * **sendmsg**\ () or **sendfile**\ () calls until *bytes* are
1741 * consumed.
1742 *
1743 * Note that if a socket closes with the internal counter holding
1744 * a non-zero value, this is not a problem because data is not
1745 * being buffered for *bytes* and is sent as it is received.
1746 * Return
1747 * 0
1748 *
1749 * int bpf_msg_cork_bytes(struct sk_msg_buff *msg, u32 bytes)
1750 * Description
1751 * For socket policies, prevent the execution of the verdict eBPF
1752 * program for message *msg* until *bytes* (byte number) have been
1753 * accumulated.
1754 *
1755 * This can be used when one needs a specific number of bytes
1756 * before a verdict can be assigned, even if the data spans
1757 * multiple **sendmsg**\ () or **sendfile**\ () calls. The extreme
1758 * case would be a user calling **sendmsg**\ () repeatedly with
1759 * 1-byte long message segments. Obviously, this is bad for
1760 * performance, but it is still valid. If the eBPF program needs
1761 * *bytes* bytes to validate a header, this helper can be used to
1762 * prevent the eBPF program to be called again until *bytes* have
1763 * been accumulated.
1764 * Return
1765 * 0
1766 *
1767 * int bpf_msg_pull_data(struct sk_msg_buff *msg, u32 start, u32 end, u64 flags)
1768 * Description
1769 * For socket policies, pull in non-linear data from user space
1770 * for *msg* and set pointers *msg*\ **->data** and *msg*\
1771 * **->data_end** to *start* and *end* bytes offsets into *msg*,
1772 * respectively.
1773 *
1774 * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a
1775 * *msg* it can only parse data that the (**data**, **data_end**)
1776 * pointers have already consumed. For **sendmsg**\ () hooks this
1777 * is likely the first scatterlist element. But for calls relying
1778 * on the **sendpage** handler (e.g. **sendfile**\ ()) this will
1779 * be the range (**0**, **0**) because the data is shared with
1780 * user space and by default the objective is to avoid allowing
1781 * user space to modify data while (or after) eBPF verdict is
1782 * being decided. This helper can be used to pull in data and to
1783 * set the start and end pointer to given values. Data will be
1784 * copied if necessary (i.e. if data was not linear and if start
1785 * and end pointers do not point to the same chunk).
1786 *
1787 * A call to this helper is susceptible to change the underlaying
1788 * packet buffer. Therefore, at load time, all checks on pointers
1789 * previously done by the verifier are invalidated and must be
1790 * performed again, if the helper is used in combination with
1791 * direct packet access.
1792 *
1793 * All values for *flags* are reserved for future usage, and must
1794 * be left at zero.
1795 * Return
1796 * 0 on success, or a negative error in case of failure.
1797 *
Andrey Ignatova3ef8e92018-04-28 16:06:19 -07001798 * int bpf_bind(struct bpf_sock_addr *ctx, struct sockaddr *addr, int addr_len)
Quentin Monnet7aa79a82018-04-25 18:16:58 +01001799 * Description
1800 * Bind the socket associated to *ctx* to the address pointed by
1801 * *addr*, of length *addr_len*. This allows for making outgoing
1802 * connection from the desired IP address, which can be useful for
1803 * example when all processes inside a cgroup should use one
1804 * single IP address on a host that has multiple IP configured.
1805 *
1806 * This helper works for IPv4 and IPv6, TCP and UDP sockets. The
1807 * domain (*addr*\ **->sa_family**) must be **AF_INET** (or
1808 * **AF_INET6**). Looking for a free port to bind to can be
1809 * expensive, therefore binding to port is not permitted by the
1810 * helper: *addr*\ **->sin_port** (or **sin6_port**, respectively)
1811 * must be set to zero.
1812 * Return
1813 * 0 on success, or a negative error in case of failure.
Quentin Monnet2d020dd2018-04-25 18:17:00 +01001814 *
1815 * int bpf_xdp_adjust_tail(struct xdp_buff *xdp_md, int delta)
1816 * Description
1817 * Adjust (move) *xdp_md*\ **->data_end** by *delta* bytes. It is
1818 * only possible to shrink the packet as of this writing,
1819 * therefore *delta* must be a negative integer.
1820 *
1821 * A call to this helper is susceptible to change the underlaying
1822 * packet buffer. Therefore, at load time, all checks on pointers
1823 * previously done by the verifier are invalidated and must be
1824 * performed again, if the helper is used in combination with
1825 * direct packet access.
1826 * Return
1827 * 0 on success, or a negative error in case of failure.
1828 *
1829 * int bpf_skb_get_xfrm_state(struct sk_buff *skb, u32 index, struct bpf_xfrm_state *xfrm_state, u32 size, u64 flags)
1830 * Description
1831 * Retrieve the XFRM state (IP transform framework, see also
1832 * **ip-xfrm(8)**) at *index* in XFRM "security path" for *skb*.
1833 *
1834 * The retrieved value is stored in the **struct bpf_xfrm_state**
1835 * pointed by *xfrm_state* and of length *size*.
1836 *
1837 * All values for *flags* are reserved for future usage, and must
1838 * be left at zero.
1839 *
1840 * This helper is available only if the kernel was compiled with
1841 * **CONFIG_XFRM** configuration option.
1842 * Return
1843 * 0 on success, or a negative error in case of failure.
Yonghong Songc195651e2018-04-28 22:28:08 -07001844 *
1845 * int bpf_get_stack(struct pt_regs *regs, void *buf, u32 size, u64 flags)
1846 * Description
Quentin Monnet79552fb2018-04-30 11:39:04 +01001847 * Return a user or a kernel stack in bpf program provided buffer.
1848 * To achieve this, the helper needs *ctx*, which is a pointer
1849 * to the context on which the tracing program is executed.
1850 * To store the stacktrace, the bpf program provides *buf* with
1851 * a nonnegative *size*.
Yonghong Songc195651e2018-04-28 22:28:08 -07001852 *
Quentin Monnet79552fb2018-04-30 11:39:04 +01001853 * The last argument, *flags*, holds the number of stack frames to
1854 * skip (from 0 to 255), masked with
1855 * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set
1856 * the following flags:
Yonghong Songc195651e2018-04-28 22:28:08 -07001857 *
Quentin Monnet79552fb2018-04-30 11:39:04 +01001858 * **BPF_F_USER_STACK**
1859 * Collect a user space stack instead of a kernel stack.
1860 * **BPF_F_USER_BUILD_ID**
1861 * Collect buildid+offset instead of ips for user stack,
1862 * only valid if **BPF_F_USER_STACK** is also specified.
Yonghong Songc195651e2018-04-28 22:28:08 -07001863 *
Quentin Monnet79552fb2018-04-30 11:39:04 +01001864 * **bpf_get_stack**\ () can collect up to
1865 * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject
1866 * to sufficient large buffer size. Note that
1867 * this limit can be controlled with the **sysctl** program, and
1868 * that it should be manually increased in order to profile long
1869 * user stacks (such as stacks for Java programs). To do so, use:
Yonghong Songc195651e2018-04-28 22:28:08 -07001870 *
Quentin Monnet79552fb2018-04-30 11:39:04 +01001871 * ::
Yonghong Songc195651e2018-04-28 22:28:08 -07001872 *
Quentin Monnet79552fb2018-04-30 11:39:04 +01001873 * # sysctl kernel.perf_event_max_stack=<new value>
Yonghong Songc195651e2018-04-28 22:28:08 -07001874 * Return
Quentin Monnet7a279e92018-05-29 12:27:44 +01001875 * A non-negative value equal to or less than *size* on success,
1876 * or a negative error in case of failure.
Daniel Borkmann4e1ec562018-05-04 01:08:15 +02001877 *
Quentin Monnet2bae79d2018-07-12 12:52:22 +01001878 * int bpf_skb_load_bytes_relative(const struct sk_buff *skb, u32 offset, void *to, u32 len, u32 start_header)
Daniel Borkmann4e1ec562018-05-04 01:08:15 +02001879 * Description
1880 * This helper is similar to **bpf_skb_load_bytes**\ () in that
1881 * it provides an easy way to load *len* bytes from *offset*
1882 * from the packet associated to *skb*, into the buffer pointed
1883 * by *to*. The difference to **bpf_skb_load_bytes**\ () is that
1884 * a fifth argument *start_header* exists in order to select a
1885 * base offset to start from. *start_header* can be one of:
1886 *
1887 * **BPF_HDR_START_MAC**
1888 * Base offset to load data from is *skb*'s mac header.
1889 * **BPF_HDR_START_NET**
1890 * Base offset to load data from is *skb*'s network header.
1891 *
1892 * In general, "direct packet access" is the preferred method to
1893 * access packet data, however, this helper is in particular useful
1894 * in socket filters where *skb*\ **->data** does not always point
1895 * to the start of the mac header and where "direct packet access"
1896 * is not available.
Daniel Borkmann4e1ec562018-05-04 01:08:15 +02001897 * Return
1898 * 0 on success, or a negative error in case of failure.
1899 *
David Ahern87f5fc72018-05-09 20:34:26 -07001900 * int bpf_fib_lookup(void *ctx, struct bpf_fib_lookup *params, int plen, u32 flags)
1901 * Description
1902 * Do FIB lookup in kernel tables using parameters in *params*.
1903 * If lookup is successful and result shows packet is to be
1904 * forwarded, the neighbor tables are searched for the nexthop.
1905 * If successful (ie., FIB lookup shows forwarding and nexthop
David Ahernfa898d72018-05-29 10:58:07 -07001906 * is resolved), the nexthop address is returned in ipv4_dst
1907 * or ipv6_dst based on family, smac is set to mac address of
1908 * egress device, dmac is set to nexthop mac address, rt_metric
David Ahern4c795792018-06-26 16:21:18 -07001909 * is set to metric from route (IPv4/IPv6 only), and ifindex
1910 * is set to the device index of the nexthop from the FIB lookup.
David Ahern87f5fc72018-05-09 20:34:26 -07001911 *
1912 * *plen* argument is the size of the passed in struct.
Quentin Monnet7a279e92018-05-29 12:27:44 +01001913 * *flags* argument can be a combination of one or more of the
1914 * following values:
David Ahern87f5fc72018-05-09 20:34:26 -07001915 *
Quentin Monnet7a279e92018-05-29 12:27:44 +01001916 * **BPF_FIB_LOOKUP_DIRECT**
1917 * Do a direct table lookup vs full lookup using FIB
1918 * rules.
1919 * **BPF_FIB_LOOKUP_OUTPUT**
1920 * Perform lookup from an egress perspective (default is
1921 * ingress).
David Ahern87f5fc72018-05-09 20:34:26 -07001922 *
1923 * *ctx* is either **struct xdp_md** for XDP programs or
1924 * **struct sk_buff** tc cls_act programs.
David Ahern87f5fc72018-05-09 20:34:26 -07001925 * Return
David Ahern4c795792018-06-26 16:21:18 -07001926 * * < 0 if any input argument is invalid
1927 * * 0 on success (packet is forwarded, nexthop neighbor exists)
1928 * * > 0 one of **BPF_FIB_LKUP_RET_** codes explaining why the
Quentin Monnet2bae79d2018-07-12 12:52:22 +01001929 * packet is not forwarded or needs assist from full stack
John Fastabend81110382018-05-14 10:00:17 -07001930 *
1931 * int bpf_sock_hash_update(struct bpf_sock_ops_kern *skops, struct bpf_map *map, void *key, u64 flags)
1932 * Description
1933 * Add an entry to, or update a sockhash *map* referencing sockets.
1934 * The *skops* is used as a new value for the entry associated to
1935 * *key*. *flags* is one of:
1936 *
1937 * **BPF_NOEXIST**
1938 * The entry for *key* must not exist in the map.
1939 * **BPF_EXIST**
1940 * The entry for *key* must already exist in the map.
1941 * **BPF_ANY**
1942 * No condition on the existence of the entry for *key*.
1943 *
1944 * If the *map* has eBPF programs (parser and verdict), those will
1945 * be inherited by the socket being added. If the socket is
1946 * already attached to eBPF programs, this results in an error.
1947 * Return
1948 * 0 on success, or a negative error in case of failure.
1949 *
1950 * int bpf_msg_redirect_hash(struct sk_msg_buff *msg, struct bpf_map *map, void *key, u64 flags)
1951 * Description
1952 * This helper is used in programs implementing policies at the
1953 * socket level. If the message *msg* is allowed to pass (i.e. if
1954 * the verdict eBPF program returns **SK_PASS**), redirect it to
1955 * the socket referenced by *map* (of type
1956 * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and
1957 * egress interfaces can be used for redirection. The
1958 * **BPF_F_INGRESS** value in *flags* is used to make the
1959 * distinction (ingress path is selected if the flag is present,
1960 * egress path otherwise). This is the only flag supported for now.
1961 * Return
1962 * **SK_PASS** on success, or **SK_DROP** on error.
1963 *
1964 * int bpf_sk_redirect_hash(struct sk_buff *skb, struct bpf_map *map, void *key, u64 flags)
1965 * Description
1966 * This helper is used in programs implementing policies at the
1967 * skb socket level. If the sk_buff *skb* is allowed to pass (i.e.
1968 * if the verdeict eBPF program returns **SK_PASS**), redirect it
1969 * to the socket referenced by *map* (of type
1970 * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and
1971 * egress interfaces can be used for redirection. The
1972 * **BPF_F_INGRESS** value in *flags* is used to make the
1973 * distinction (ingress path is selected if the flag is present,
1974 * egress otherwise). This is the only flag supported for now.
1975 * Return
1976 * **SK_PASS** on success, or **SK_DROP** on error.
Mathieu Xhonneuxfe94cc22018-05-20 14:58:14 +01001977 *
1978 * int bpf_lwt_push_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
1979 * Description
1980 * Encapsulate the packet associated to *skb* within a Layer 3
1981 * protocol header. This header is provided in the buffer at
1982 * address *hdr*, with *len* its size in bytes. *type* indicates
1983 * the protocol of the header and can be one of:
1984 *
1985 * **BPF_LWT_ENCAP_SEG6**
1986 * IPv6 encapsulation with Segment Routing Header
1987 * (**struct ipv6_sr_hdr**). *hdr* only contains the SRH,
1988 * the IPv6 header is computed by the kernel.
1989 * **BPF_LWT_ENCAP_SEG6_INLINE**
1990 * Only works if *skb* contains an IPv6 packet. Insert a
1991 * Segment Routing Header (**struct ipv6_sr_hdr**) inside
1992 * the IPv6 header.
1993 *
1994 * A call to this helper is susceptible to change the underlaying
1995 * packet buffer. Therefore, at load time, all checks on pointers
1996 * previously done by the verifier are invalidated and must be
1997 * performed again, if the helper is used in combination with
1998 * direct packet access.
1999 * Return
2000 * 0 on success, or a negative error in case of failure.
2001 *
2002 * int bpf_lwt_seg6_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len)
2003 * Description
2004 * Store *len* bytes from address *from* into the packet
2005 * associated to *skb*, at *offset*. Only the flags, tag and TLVs
2006 * inside the outermost IPv6 Segment Routing Header can be
2007 * modified through this helper.
2008 *
2009 * A call to this helper is susceptible to change the underlaying
2010 * packet buffer. Therefore, at load time, all checks on pointers
2011 * previously done by the verifier are invalidated and must be
2012 * performed again, if the helper is used in combination with
2013 * direct packet access.
2014 * Return
2015 * 0 on success, or a negative error in case of failure.
2016 *
2017 * int bpf_lwt_seg6_adjust_srh(struct sk_buff *skb, u32 offset, s32 delta)
2018 * Description
2019 * Adjust the size allocated to TLVs in the outermost IPv6
2020 * Segment Routing Header contained in the packet associated to
2021 * *skb*, at position *offset* by *delta* bytes. Only offsets
2022 * after the segments are accepted. *delta* can be as well
2023 * positive (growing) as negative (shrinking).
2024 *
2025 * A call to this helper is susceptible to change the underlaying
2026 * packet buffer. Therefore, at load time, all checks on pointers
2027 * previously done by the verifier are invalidated and must be
2028 * performed again, if the helper is used in combination with
2029 * direct packet access.
2030 * Return
2031 * 0 on success, or a negative error in case of failure.
2032 *
2033 * int bpf_lwt_seg6_action(struct sk_buff *skb, u32 action, void *param, u32 param_len)
2034 * Description
2035 * Apply an IPv6 Segment Routing action of type *action* to the
2036 * packet associated to *skb*. Each action takes a parameter
2037 * contained at address *param*, and of length *param_len* bytes.
2038 * *action* can be one of:
2039 *
2040 * **SEG6_LOCAL_ACTION_END_X**
2041 * End.X action: Endpoint with Layer-3 cross-connect.
2042 * Type of *param*: **struct in6_addr**.
2043 * **SEG6_LOCAL_ACTION_END_T**
2044 * End.T action: Endpoint with specific IPv6 table lookup.
2045 * Type of *param*: **int**.
2046 * **SEG6_LOCAL_ACTION_END_B6**
2047 * End.B6 action: Endpoint bound to an SRv6 policy.
2048 * Type of param: **struct ipv6_sr_hdr**.
2049 * **SEG6_LOCAL_ACTION_END_B6_ENCAP**
2050 * End.B6.Encap action: Endpoint bound to an SRv6
2051 * encapsulation policy.
2052 * Type of param: **struct ipv6_sr_hdr**.
2053 *
2054 * A call to this helper is susceptible to change the underlaying
2055 * packet buffer. Therefore, at load time, all checks on pointers
2056 * previously done by the verifier are invalidated and must be
2057 * performed again, if the helper is used in combination with
2058 * direct packet access.
2059 * Return
2060 * 0 on success, or a negative error in case of failure.
Sean Youngf4364dc2018-05-27 12:24:09 +01002061 *
2062 * int bpf_rc_keydown(void *ctx, u32 protocol, u64 scancode, u32 toggle)
2063 * Description
2064 * This helper is used in programs implementing IR decoding, to
2065 * report a successfully decoded key press with *scancode*,
2066 * *toggle* value in the given *protocol*. The scancode will be
2067 * translated to a keycode using the rc keymap, and reported as
2068 * an input key down event. After a period a key up event is
2069 * generated. This period can be extended by calling either
2070 * **bpf_rc_keydown** () again with the same values, or calling
2071 * **bpf_rc_repeat** ().
2072 *
2073 * Some protocols include a toggle bit, in case the button was
2074 * released and pressed again between consecutive scancodes.
2075 *
2076 * The *ctx* should point to the lirc sample as passed into
2077 * the program.
2078 *
2079 * The *protocol* is the decoded protocol number (see
2080 * **enum rc_proto** for some predefined values).
2081 *
2082 * This helper is only available is the kernel was compiled with
2083 * the **CONFIG_BPF_LIRC_MODE2** configuration option set to
2084 * "**y**".
Sean Youngf4364dc2018-05-27 12:24:09 +01002085 * Return
2086 * 0
2087 *
2088 * int bpf_rc_repeat(void *ctx)
2089 * Description
2090 * This helper is used in programs implementing IR decoding, to
2091 * report a successfully decoded repeat key message. This delays
2092 * the generation of a key up event for previously generated
2093 * key down event.
2094 *
2095 * Some IR protocols like NEC have a special IR message for
2096 * repeating last button, for when a button is held down.
2097 *
2098 * The *ctx* should point to the lirc sample as passed into
2099 * the program.
2100 *
2101 * This helper is only available is the kernel was compiled with
2102 * the **CONFIG_BPF_LIRC_MODE2** configuration option set to
2103 * "**y**".
Sean Youngf4364dc2018-05-27 12:24:09 +01002104 * Return
2105 * 0
Daniel Borkmanncb20b082018-06-02 23:06:36 +02002106 *
2107 * uint64_t bpf_skb_cgroup_id(struct sk_buff *skb)
2108 * Description
2109 * Return the cgroup v2 id of the socket associated with the *skb*.
2110 * This is roughly similar to the **bpf_get_cgroup_classid**\ ()
2111 * helper for cgroup v1 by providing a tag resp. identifier that
2112 * can be matched on or used for map lookups e.g. to implement
2113 * policy. The cgroup v2 id of a given path in the hierarchy is
2114 * exposed in user space through the f_handle API in order to get
2115 * to the same 64-bit id.
2116 *
2117 * This helper can be used on TC egress path, but not on ingress,
2118 * and is available only if the kernel was compiled with the
2119 * **CONFIG_SOCK_CGROUP_DATA** configuration option.
2120 * Return
2121 * The id is returned or 0 in case the id could not be retrieved.
Yonghong Songbf6fa2c2018-06-03 15:59:41 -07002122 *
Andrey Ignatov77236282018-08-12 10:49:27 -07002123 * u64 bpf_skb_ancestor_cgroup_id(struct sk_buff *skb, int ancestor_level)
2124 * Description
2125 * Return id of cgroup v2 that is ancestor of cgroup associated
2126 * with the *skb* at the *ancestor_level*. The root cgroup is at
2127 * *ancestor_level* zero and each step down the hierarchy
2128 * increments the level. If *ancestor_level* == level of cgroup
2129 * associated with *skb*, then return value will be same as that
2130 * of **bpf_skb_cgroup_id**\ ().
2131 *
2132 * The helper is useful to implement policies based on cgroups
2133 * that are upper in hierarchy than immediate cgroup associated
2134 * with *skb*.
2135 *
2136 * The format of returned id and helper limitations are same as in
2137 * **bpf_skb_cgroup_id**\ ().
2138 * Return
2139 * The id is returned or 0 in case the id could not be retrieved.
2140 *
Yonghong Songbf6fa2c2018-06-03 15:59:41 -07002141 * u64 bpf_get_current_cgroup_id(void)
2142 * Return
2143 * A 64-bit integer containing the current cgroup id based
2144 * on the cgroup within which the current task is running.
Roman Gushchincd339432018-08-02 14:27:24 -07002145 *
2146 * void* get_local_storage(void *map, u64 flags)
2147 * Description
2148 * Get the pointer to the local storage area.
2149 * The type and the size of the local storage is defined
2150 * by the *map* argument.
2151 * The *flags* meaning is specific for each map type,
2152 * and has to be 0 for cgroup local storage.
2153 *
2154 * Depending on the bpf program type, a local storage area
2155 * can be shared between multiple instances of the bpf program,
2156 * running simultaneously.
2157 *
2158 * A user should care about the synchronization by himself.
2159 * For example, by using the BPF_STX_XADD instruction to alter
2160 * the shared data.
2161 * Return
2162 * Pointer to the local storage area.
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002163 *
2164 * int bpf_sk_select_reuseport(struct sk_reuseport_md *reuse, struct bpf_map *map, void *key, u64 flags)
2165 * Description
2166 * Select a SO_REUSEPORT sk from a BPF_MAP_TYPE_REUSEPORT_ARRAY map
2167 * It checks the selected sk is matching the incoming
2168 * request in the skb.
2169 * Return
2170 * 0 on success, or a negative error in case of failure.
Joe Stringer6acc9b42018-10-02 13:35:36 -07002171 *
2172 * struct bpf_sock *bpf_sk_lookup_tcp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u32 netns, u64 flags)
2173 * Description
2174 * Look for TCP socket matching *tuple*, optionally in a child
2175 * network namespace *netns*. The return value must be checked,
2176 * and if non-NULL, released via **bpf_sk_release**\ ().
2177 *
2178 * The *ctx* should point to the context of the program, such as
2179 * the skb or socket (depending on the hook in use). This is used
2180 * to determine the base network namespace for the lookup.
2181 *
2182 * *tuple_size* must be one of:
2183 *
2184 * **sizeof**\ (*tuple*\ **->ipv4**)
2185 * Look for an IPv4 socket.
2186 * **sizeof**\ (*tuple*\ **->ipv6**)
2187 * Look for an IPv6 socket.
2188 *
2189 * If the *netns* is zero, then the socket lookup table in the
2190 * netns associated with the *ctx* will be used. For the TC hooks,
2191 * this in the netns of the device in the skb. For socket hooks,
2192 * this in the netns of the socket. If *netns* is non-zero, then
2193 * it specifies the ID of the netns relative to the netns
2194 * associated with the *ctx*.
2195 *
2196 * All values for *flags* are reserved for future usage, and must
2197 * be left at zero.
2198 *
2199 * This helper is available only if the kernel was compiled with
2200 * **CONFIG_NET** configuration option.
2201 * Return
2202 * Pointer to *struct bpf_sock*, or NULL in case of failure.
2203 *
2204 * struct bpf_sock *bpf_sk_lookup_udp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u32 netns, u64 flags)
2205 * Description
2206 * Look for UDP socket matching *tuple*, optionally in a child
2207 * network namespace *netns*. The return value must be checked,
2208 * and if non-NULL, released via **bpf_sk_release**\ ().
2209 *
2210 * The *ctx* should point to the context of the program, such as
2211 * the skb or socket (depending on the hook in use). This is used
2212 * to determine the base network namespace for the lookup.
2213 *
2214 * *tuple_size* must be one of:
2215 *
2216 * **sizeof**\ (*tuple*\ **->ipv4**)
2217 * Look for an IPv4 socket.
2218 * **sizeof**\ (*tuple*\ **->ipv6**)
2219 * Look for an IPv6 socket.
2220 *
2221 * If the *netns* is zero, then the socket lookup table in the
2222 * netns associated with the *ctx* will be used. For the TC hooks,
2223 * this in the netns of the device in the skb. For socket hooks,
2224 * this in the netns of the socket. If *netns* is non-zero, then
2225 * it specifies the ID of the netns relative to the netns
2226 * associated with the *ctx*.
2227 *
2228 * All values for *flags* are reserved for future usage, and must
2229 * be left at zero.
2230 *
2231 * This helper is available only if the kernel was compiled with
2232 * **CONFIG_NET** configuration option.
2233 * Return
2234 * Pointer to *struct bpf_sock*, or NULL in case of failure.
2235 *
2236 * int bpf_sk_release(struct bpf_sock *sk)
2237 * Description
2238 * Release the reference held by *sock*. *sock* must be a non-NULL
2239 * pointer that was returned from bpf_sk_lookup_xxx\ ().
2240 * Return
2241 * 0 on success, or a negative error in case of failure.
Thomas Grafebb676d2016-10-27 11:23:51 +02002242 */
2243#define __BPF_FUNC_MAPPER(FN) \
2244 FN(unspec), \
2245 FN(map_lookup_elem), \
2246 FN(map_update_elem), \
2247 FN(map_delete_elem), \
2248 FN(probe_read), \
2249 FN(ktime_get_ns), \
2250 FN(trace_printk), \
2251 FN(get_prandom_u32), \
2252 FN(get_smp_processor_id), \
2253 FN(skb_store_bytes), \
2254 FN(l3_csum_replace), \
2255 FN(l4_csum_replace), \
2256 FN(tail_call), \
2257 FN(clone_redirect), \
2258 FN(get_current_pid_tgid), \
2259 FN(get_current_uid_gid), \
2260 FN(get_current_comm), \
2261 FN(get_cgroup_classid), \
2262 FN(skb_vlan_push), \
2263 FN(skb_vlan_pop), \
2264 FN(skb_get_tunnel_key), \
2265 FN(skb_set_tunnel_key), \
2266 FN(perf_event_read), \
2267 FN(redirect), \
2268 FN(get_route_realm), \
2269 FN(perf_event_output), \
2270 FN(skb_load_bytes), \
2271 FN(get_stackid), \
2272 FN(csum_diff), \
2273 FN(skb_get_tunnel_opt), \
2274 FN(skb_set_tunnel_opt), \
2275 FN(skb_change_proto), \
2276 FN(skb_change_type), \
2277 FN(skb_under_cgroup), \
2278 FN(get_hash_recalc), \
2279 FN(get_current_task), \
2280 FN(probe_write_user), \
2281 FN(current_task_under_cgroup), \
2282 FN(skb_change_tail), \
2283 FN(skb_pull_data), \
2284 FN(csum_update), \
2285 FN(set_hash_invalid), \
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002286 FN(get_numa_node_id), \
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08002287 FN(skb_change_head), \
Gianluca Borelloa5e8c072017-01-18 17:55:49 +00002288 FN(xdp_adjust_head), \
Chenbo Feng91b82702017-03-22 17:27:34 -07002289 FN(probe_read_str), \
Chenbo Feng6acc5c22017-03-22 17:27:35 -07002290 FN(get_socket_cookie), \
Daniel Borkmannded092c2017-06-11 00:50:47 +02002291 FN(get_socket_uid), \
Lawrence Brakmo8c4b4c72017-06-30 20:02:46 -07002292 FN(set_hash), \
Daniel Borkmann2be7e212017-07-02 02:13:26 +02002293 FN(setsockopt), \
John Fastabend97f91a72017-07-17 09:29:18 -07002294 FN(skb_adjust_room), \
John Fastabend174a79f2017-08-15 22:32:47 -07002295 FN(redirect_map), \
2296 FN(sk_redirect_map), \
2297 FN(sock_map_update), \
Yonghong Song908432c2017-10-05 09:19:20 -07002298 FN(xdp_adjust_meta), \
Yonghong Song4bebdc72017-10-05 09:19:22 -07002299 FN(perf_event_read_value), \
Lawrence Brakmocd86d1f2017-10-20 11:05:40 -07002300 FN(perf_prog_read_value), \
Josef Bacik9802d862017-12-11 11:36:48 -05002301 FN(getsockopt), \
Lawrence Brakmob13d8802018-01-25 16:14:10 -08002302 FN(override_return), \
John Fastabend4f738ad2018-03-18 12:57:10 -07002303 FN(sock_ops_cb_flags_set), \
John Fastabend2a100312018-03-18 12:57:15 -07002304 FN(msg_redirect_map), \
John Fastabend91843d52018-03-18 12:57:20 -07002305 FN(msg_apply_bytes), \
John Fastabend015632b2018-03-18 12:57:25 -07002306 FN(msg_cork_bytes), \
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002307 FN(msg_pull_data), \
Nikita V. Shirokovb32cc5b2018-04-17 21:42:13 -07002308 FN(bind), \
Eyal Birger12bed762018-04-24 17:50:29 +03002309 FN(xdp_adjust_tail), \
Yonghong Songc195651e2018-04-28 22:28:08 -07002310 FN(skb_get_xfrm_state), \
Daniel Borkmann4e1ec562018-05-04 01:08:15 +02002311 FN(get_stack), \
David Ahern87f5fc72018-05-09 20:34:26 -07002312 FN(skb_load_bytes_relative), \
John Fastabend81110382018-05-14 10:00:17 -07002313 FN(fib_lookup), \
2314 FN(sock_hash_update), \
2315 FN(msg_redirect_hash), \
Mathieu Xhonneuxfe94cc22018-05-20 14:58:14 +01002316 FN(sk_redirect_hash), \
2317 FN(lwt_push_encap), \
2318 FN(lwt_seg6_store_bytes), \
2319 FN(lwt_seg6_adjust_srh), \
Sean Youngf4364dc2018-05-27 12:24:09 +01002320 FN(lwt_seg6_action), \
2321 FN(rc_repeat), \
Daniel Borkmanncb20b082018-06-02 23:06:36 +02002322 FN(rc_keydown), \
Yonghong Songbf6fa2c2018-06-03 15:59:41 -07002323 FN(skb_cgroup_id), \
Roman Gushchincd339432018-08-02 14:27:24 -07002324 FN(get_current_cgroup_id), \
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002325 FN(get_local_storage), \
Andrey Ignatov77236282018-08-12 10:49:27 -07002326 FN(sk_select_reuseport), \
Joe Stringer6acc9b42018-10-02 13:35:36 -07002327 FN(skb_ancestor_cgroup_id), \
2328 FN(sk_lookup_tcp), \
2329 FN(sk_lookup_udp), \
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002330 FN(sk_release), \
2331 FN(map_push_elem), \
2332 FN(map_pop_elem), \
2333 FN(map_peek_elem),
Thomas Grafebb676d2016-10-27 11:23:51 +02002334
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002335/* integer value in 'imm' field of BPF_CALL instruction selects which helper
2336 * function eBPF program intends to call
2337 */
Thomas Grafebb676d2016-10-27 11:23:51 +02002338#define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002339enum bpf_func_id {
Thomas Grafebb676d2016-10-27 11:23:51 +02002340 __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002341 __BPF_FUNC_MAX_ID,
2342};
Thomas Grafebb676d2016-10-27 11:23:51 +02002343#undef __BPF_ENUM_FN
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002344
Daniel Borkmann781c53b2016-01-11 01:16:38 +01002345/* All flags used by eBPF helper functions, placed here. */
2346
2347/* BPF_FUNC_skb_store_bytes flags. */
2348#define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01002349#define BPF_F_INVALIDATE_HASH (1ULL << 1)
Daniel Borkmann781c53b2016-01-11 01:16:38 +01002350
2351/* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
2352 * First 4 bits are for passing the header field size.
2353 */
2354#define BPF_F_HDR_FIELD_MASK 0xfULL
2355
2356/* BPF_FUNC_l4_csum_replace flags. */
2357#define BPF_F_PSEUDO_HDR (1ULL << 4)
Daniel Borkmann2f729592016-02-19 23:05:26 +01002358#define BPF_F_MARK_MANGLED_0 (1ULL << 5)
Daniel Borkmannd1b662a2017-01-24 01:06:28 +01002359#define BPF_F_MARK_ENFORCE (1ULL << 6)
Daniel Borkmann781c53b2016-01-11 01:16:38 +01002360
2361/* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
2362#define BPF_F_INGRESS (1ULL << 0)
2363
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002364/* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
2365#define BPF_F_TUNINFO_IPV6 (1ULL << 0)
2366
Yonghong Songc195651e2018-04-28 22:28:08 -07002367/* flags for both BPF_FUNC_get_stackid and BPF_FUNC_get_stack. */
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -08002368#define BPF_F_SKIP_FIELD_MASK 0xffULL
2369#define BPF_F_USER_STACK (1ULL << 8)
Yonghong Songc195651e2018-04-28 22:28:08 -07002370/* flags used by BPF_FUNC_get_stackid only. */
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -08002371#define BPF_F_FAST_STACK_CMP (1ULL << 9)
2372#define BPF_F_REUSE_STACKID (1ULL << 10)
Yonghong Songc195651e2018-04-28 22:28:08 -07002373/* flags used by BPF_FUNC_get_stack only. */
2374#define BPF_F_USER_BUILD_ID (1ULL << 11)
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -08002375
Daniel Borkmann2da897e2016-02-23 02:05:26 +01002376/* BPF_FUNC_skb_set_tunnel_key flags. */
2377#define BPF_F_ZERO_CSUM_TX (1ULL << 1)
Daniel Borkmann22080872016-03-04 15:15:05 +01002378#define BPF_F_DONT_FRAGMENT (1ULL << 2)
William Tu77a51962018-03-01 13:49:57 -08002379#define BPF_F_SEQ_NUMBER (1ULL << 3)
Daniel Borkmann2da897e2016-02-23 02:05:26 +01002380
Yonghong Song908432c2017-10-05 09:19:20 -07002381/* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and
2382 * BPF_FUNC_perf_event_read_value flags.
2383 */
Daniel Borkmann1e337592016-04-18 21:01:23 +02002384#define BPF_F_INDEX_MASK 0xffffffffULL
2385#define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002386/* BPF_FUNC_perf_event_output for sk_buff input context. */
2387#define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
Daniel Borkmann1e337592016-04-18 21:01:23 +02002388
Daniel Borkmann2be7e212017-07-02 02:13:26 +02002389/* Mode for BPF_FUNC_skb_adjust_room helper. */
2390enum bpf_adj_room_mode {
2391 BPF_ADJ_ROOM_NET,
2392};
2393
Daniel Borkmann4e1ec562018-05-04 01:08:15 +02002394/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */
2395enum bpf_hdr_start_off {
2396 BPF_HDR_START_MAC,
2397 BPF_HDR_START_NET,
2398};
2399
Mathieu Xhonneuxfe94cc22018-05-20 14:58:14 +01002400/* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */
2401enum bpf_lwt_encap_mode {
2402 BPF_LWT_ENCAP_SEG6,
2403 BPF_LWT_ENCAP_SEG6_INLINE
2404};
2405
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002406/* user accessible mirror of in-kernel sk_buff.
2407 * new fields can only be added to the end of this structure
2408 */
2409struct __sk_buff {
2410 __u32 len;
2411 __u32 pkt_type;
2412 __u32 mark;
2413 __u32 queue_mapping;
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002414 __u32 protocol;
2415 __u32 vlan_present;
2416 __u32 vlan_tci;
Michal Sekletar27cd5452015-03-24 14:48:41 +01002417 __u32 vlan_proto;
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002418 __u32 priority;
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002419 __u32 ingress_ifindex;
2420 __u32 ifindex;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002421 __u32 tc_index;
2422 __u32 cb[5];
Daniel Borkmannba7591d2015-08-01 00:46:29 +02002423 __u32 hash;
Daniel Borkmann045efa82015-09-15 23:05:42 -07002424 __u32 tc_classid;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002425 __u32 data;
2426 __u32 data_end;
Daniel Borkmannb1d9fc42017-04-19 23:01:17 +02002427 __u32 napi_id;
John Fastabend8a31db52017-08-15 22:33:09 -07002428
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002429 /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */
John Fastabend8a31db52017-08-15 22:33:09 -07002430 __u32 family;
2431 __u32 remote_ip4; /* Stored in network byte order */
2432 __u32 local_ip4; /* Stored in network byte order */
2433 __u32 remote_ip6[4]; /* Stored in network byte order */
2434 __u32 local_ip6[4]; /* Stored in network byte order */
2435 __u32 remote_port; /* Stored in network byte order */
2436 __u32 local_port; /* stored in host byte order */
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002437 /* ... here. */
2438
2439 __u32 data_meta;
Petar Penkovd58e4682018-09-14 07:46:18 -07002440 struct bpf_flow_keys *flow_keys;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002441};
2442
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002443struct bpf_tunnel_key {
2444 __u32 tunnel_id;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002445 union {
2446 __u32 remote_ipv4;
2447 __u32 remote_ipv6[4];
2448 };
2449 __u8 tunnel_tos;
2450 __u8 tunnel_ttl;
Daniel Borkmann1fbc2e02018-06-02 23:06:37 +02002451 __u16 tunnel_ext; /* Padding, future use. */
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002452 __u32 tunnel_label;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002453};
2454
Eyal Birger12bed762018-04-24 17:50:29 +03002455/* user accessible mirror of in-kernel xfrm_state.
2456 * new fields can only be added to the end of this structure
2457 */
2458struct bpf_xfrm_state {
2459 __u32 reqid;
2460 __u32 spi; /* Stored in network byte order */
2461 __u16 family;
Daniel Borkmann1fbc2e02018-06-02 23:06:37 +02002462 __u16 ext; /* Padding, future use. */
Eyal Birger12bed762018-04-24 17:50:29 +03002463 union {
2464 __u32 remote_ipv4; /* Stored in network byte order */
2465 __u32 remote_ipv6[4]; /* Stored in network byte order */
2466 };
2467};
2468
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002469/* Generic BPF return codes which all BPF program types may support.
2470 * The values are binary compatible with their TC_ACT_* counter-part to
2471 * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
2472 * programs.
2473 *
2474 * XDP is handled seprately, see XDP_*.
2475 */
2476enum bpf_ret_code {
2477 BPF_OK = 0,
2478 /* 1 reserved */
2479 BPF_DROP = 2,
2480 /* 3-6 reserved */
2481 BPF_REDIRECT = 7,
2482 /* >127 are reserved for prog type specific return codes */
2483};
2484
David Ahern610236582016-12-01 08:48:04 -08002485struct bpf_sock {
2486 __u32 bound_dev_if;
David Ahernaa4c1032016-12-01 08:48:06 -08002487 __u32 family;
2488 __u32 type;
2489 __u32 protocol;
David Ahern482dca92017-08-31 15:05:44 -07002490 __u32 mark;
2491 __u32 priority;
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002492 __u32 src_ip4; /* Allows 1,2,4-byte read.
2493 * Stored in network byte order.
2494 */
2495 __u32 src_ip6[4]; /* Allows 1,2,4-byte read.
2496 * Stored in network byte order.
2497 */
2498 __u32 src_port; /* Allows 4-byte read.
2499 * Stored in host byte order
2500 */
David Ahern610236582016-12-01 08:48:04 -08002501};
2502
Joe Stringer6acc9b42018-10-02 13:35:36 -07002503struct bpf_sock_tuple {
2504 union {
2505 struct {
2506 __be32 saddr;
2507 __be32 daddr;
2508 __be16 sport;
2509 __be16 dport;
2510 } ipv4;
2511 struct {
2512 __be32 saddr[4];
2513 __be32 daddr[4];
2514 __be16 sport;
2515 __be16 dport;
2516 } ipv6;
2517 };
2518};
2519
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08002520#define XDP_PACKET_HEADROOM 256
2521
Brenden Blanco6a773a12016-07-19 12:16:47 -07002522/* User return codes for XDP prog type.
2523 * A valid XDP program must return one of these defined values. All other
Daniel Borkmann9beb8be2017-09-09 01:40:35 +02002524 * return codes are reserved for future use. Unknown return codes will
2525 * result in packet drops and a warning via bpf_warn_invalid_xdp_action().
Brenden Blanco6a773a12016-07-19 12:16:47 -07002526 */
2527enum xdp_action {
2528 XDP_ABORTED = 0,
2529 XDP_DROP,
2530 XDP_PASS,
Brenden Blanco6ce96ca2016-07-19 12:16:53 -07002531 XDP_TX,
John Fastabend814abfa2017-07-17 09:27:07 -07002532 XDP_REDIRECT,
Brenden Blanco6a773a12016-07-19 12:16:47 -07002533};
2534
2535/* user accessible metadata for XDP packet hook
2536 * new fields must be added to the end of this structure
2537 */
2538struct xdp_md {
2539 __u32 data;
2540 __u32 data_end;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002541 __u32 data_meta;
Jesper Dangaard Brouerdaaf24c2018-01-11 17:39:09 +01002542 /* Below access go through struct xdp_rxq_info */
Jesper Dangaard Brouer02dd3292018-01-03 11:26:14 +01002543 __u32 ingress_ifindex; /* rxq->dev->ifindex */
2544 __u32 rx_queue_index; /* rxq->queue_index */
Brenden Blanco6a773a12016-07-19 12:16:47 -07002545};
2546
John Fastabend174a79f2017-08-15 22:32:47 -07002547enum sk_action {
John Fastabendbfa640752017-10-27 09:45:53 -07002548 SK_DROP = 0,
2549 SK_PASS,
John Fastabend174a79f2017-08-15 22:32:47 -07002550};
2551
John Fastabend4f738ad2018-03-18 12:57:10 -07002552/* user accessible metadata for SK_MSG packet hook, new fields must
2553 * be added to the end of this structure
2554 */
2555struct sk_msg_md {
2556 void *data;
2557 void *data_end;
John Fastabend303def32018-05-17 14:16:58 -07002558
2559 __u32 family;
2560 __u32 remote_ip4; /* Stored in network byte order */
2561 __u32 local_ip4; /* Stored in network byte order */
2562 __u32 remote_ip6[4]; /* Stored in network byte order */
2563 __u32 local_ip6[4]; /* Stored in network byte order */
2564 __u32 remote_port; /* Stored in network byte order */
2565 __u32 local_port; /* stored in host byte order */
John Fastabend4f738ad2018-03-18 12:57:10 -07002566};
2567
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002568struct sk_reuseport_md {
2569 /*
2570 * Start of directly accessible data. It begins from
2571 * the tcp/udp header.
2572 */
2573 void *data;
2574 void *data_end; /* End of directly accessible data */
2575 /*
2576 * Total length of packet (starting from the tcp/udp header).
2577 * Note that the directly accessible bytes (data_end - data)
2578 * could be less than this "len". Those bytes could be
2579 * indirectly read by a helper "bpf_skb_load_bytes()".
2580 */
2581 __u32 len;
2582 /*
2583 * Eth protocol in the mac header (network byte order). e.g.
2584 * ETH_P_IP(0x0800) and ETH_P_IPV6(0x86DD)
2585 */
2586 __u32 eth_protocol;
2587 __u32 ip_protocol; /* IP protocol. e.g. IPPROTO_TCP, IPPROTO_UDP */
2588 __u32 bind_inany; /* Is sock bound to an INANY address? */
2589 __u32 hash; /* A hash of the packet 4 tuples */
2590};
2591
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002592#define BPF_TAG_SIZE 8
2593
2594struct bpf_prog_info {
2595 __u32 type;
2596 __u32 id;
2597 __u8 tag[BPF_TAG_SIZE];
2598 __u32 jited_prog_len;
2599 __u32 xlated_prog_len;
2600 __aligned_u64 jited_prog_insns;
2601 __aligned_u64 xlated_prog_insns;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002602 __u64 load_time; /* ns since boottime */
2603 __u32 created_by_uid;
2604 __u32 nr_map_ids;
2605 __aligned_u64 map_ids;
Martin KaFai Lau067cae42017-10-05 21:52:12 -07002606 char name[BPF_OBJ_NAME_LEN];
Jakub Kicinski675fc272017-12-27 18:39:09 -08002607 __u32 ifindex;
Jiri Olsab85fab02018-04-25 19:41:06 +02002608 __u32 gpl_compatible:1;
Jakub Kicinski675fc272017-12-27 18:39:09 -08002609 __u64 netns_dev;
2610 __u64 netns_ino;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302611 __u32 nr_jited_ksyms;
Sandipan Das815581c2018-05-24 12:26:52 +05302612 __u32 nr_jited_func_lens;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302613 __aligned_u64 jited_ksyms;
Sandipan Das815581c2018-05-24 12:26:52 +05302614 __aligned_u64 jited_func_lens;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002615} __attribute__((aligned(8)));
2616
2617struct bpf_map_info {
2618 __u32 type;
2619 __u32 id;
2620 __u32 key_size;
2621 __u32 value_size;
2622 __u32 max_entries;
2623 __u32 map_flags;
Martin KaFai Lau067cae42017-10-05 21:52:12 -07002624 char name[BPF_OBJ_NAME_LEN];
Jakub Kicinski52775b32018-01-17 19:13:28 -08002625 __u32 ifindex;
Daniel Borkmann36f98142018-06-02 05:21:59 +02002626 __u32 :32;
Jakub Kicinski52775b32018-01-17 19:13:28 -08002627 __u64 netns_dev;
2628 __u64 netns_ino;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002629 __u32 btf_id;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002630 __u32 btf_key_type_id;
2631 __u32 btf_value_type_id;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002632} __attribute__((aligned(8)));
2633
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002634struct bpf_btf_info {
2635 __aligned_u64 btf;
2636 __u32 btf_size;
2637 __u32 id;
2638} __attribute__((aligned(8)));
2639
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002640/* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
2641 * by user and intended to be used by socket (e.g. to bind to, depends on
2642 * attach attach type).
2643 */
2644struct bpf_sock_addr {
2645 __u32 user_family; /* Allows 4-byte read, but no write. */
2646 __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write.
2647 * Stored in network byte order.
2648 */
2649 __u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
2650 * Stored in network byte order.
2651 */
2652 __u32 user_port; /* Allows 4-byte read and write.
2653 * Stored in network byte order
2654 */
2655 __u32 family; /* Allows 4-byte read, but no write */
2656 __u32 type; /* Allows 4-byte read, but no write */
2657 __u32 protocol; /* Allows 4-byte read, but no write */
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002658 __u32 msg_src_ip4; /* Allows 1,2,4-byte read an 4-byte write.
2659 * Stored in network byte order.
2660 */
2661 __u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
2662 * Stored in network byte order.
2663 */
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002664};
2665
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002666/* User bpf_sock_ops struct to access socket values and specify request ops
2667 * and their replies.
2668 * Some of this fields are in network (bigendian) byte order and may need
2669 * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h).
2670 * New fields can only be added at the end of this structure
2671 */
2672struct bpf_sock_ops {
2673 __u32 op;
2674 union {
Lawrence Brakmode525be2018-01-25 16:14:09 -08002675 __u32 args[4]; /* Optionally passed to bpf program */
2676 __u32 reply; /* Returned by bpf program */
2677 __u32 replylong[4]; /* Optionally returned by bpf prog */
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002678 };
2679 __u32 family;
2680 __u32 remote_ip4; /* Stored in network byte order */
2681 __u32 local_ip4; /* Stored in network byte order */
2682 __u32 remote_ip6[4]; /* Stored in network byte order */
2683 __u32 local_ip6[4]; /* Stored in network byte order */
2684 __u32 remote_port; /* Stored in network byte order */
2685 __u32 local_port; /* stored in host byte order */
Lawrence Brakmof19397a2017-12-01 10:15:04 -08002686 __u32 is_fullsock; /* Some TCP fields are only valid if
2687 * there is a full socket. If not, the
2688 * fields read as zero.
2689 */
2690 __u32 snd_cwnd;
2691 __u32 srtt_us; /* Averaged RTT << 3 in usecs */
Lawrence Brakmob13d8802018-01-25 16:14:10 -08002692 __u32 bpf_sock_ops_cb_flags; /* flags defined in uapi/linux/tcp.h */
Lawrence Brakmo44f0e432018-01-25 16:14:12 -08002693 __u32 state;
2694 __u32 rtt_min;
2695 __u32 snd_ssthresh;
2696 __u32 rcv_nxt;
2697 __u32 snd_nxt;
2698 __u32 snd_una;
2699 __u32 mss_cache;
2700 __u32 ecn_flags;
2701 __u32 rate_delivered;
2702 __u32 rate_interval_us;
2703 __u32 packets_out;
2704 __u32 retrans_out;
2705 __u32 total_retrans;
2706 __u32 segs_in;
2707 __u32 data_segs_in;
2708 __u32 segs_out;
2709 __u32 data_segs_out;
2710 __u32 lost_out;
2711 __u32 sacked_out;
2712 __u32 sk_txhash;
2713 __u64 bytes_received;
2714 __u64 bytes_acked;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002715};
2716
Lawrence Brakmob13d8802018-01-25 16:14:10 -08002717/* Definitions for bpf_sock_ops_cb_flags */
Lawrence Brakmof89013f2018-01-25 16:14:11 -08002718#define BPF_SOCK_OPS_RTO_CB_FLAG (1<<0)
Lawrence Brakmoa31ad292018-01-25 16:14:14 -08002719#define BPF_SOCK_OPS_RETRANS_CB_FLAG (1<<1)
Lawrence Brakmod4487492018-01-25 16:14:15 -08002720#define BPF_SOCK_OPS_STATE_CB_FLAG (1<<2)
2721#define BPF_SOCK_OPS_ALL_CB_FLAGS 0x7 /* Mask of all currently
Lawrence Brakmob13d8802018-01-25 16:14:10 -08002722 * supported cb flags
2723 */
2724
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002725/* List of known BPF sock_ops operators.
2726 * New entries can only be added at the end
2727 */
2728enum {
2729 BPF_SOCK_OPS_VOID,
Lawrence Brakmo8550f322017-06-30 20:02:42 -07002730 BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or
2731 * -1 if default value should be used
2732 */
Lawrence Brakmo13d3b1e2017-06-30 20:02:44 -07002733 BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized
2734 * window (in packets) or -1 if default
2735 * value should be used
2736 */
Lawrence Brakmo9872a4b2017-06-30 20:02:47 -07002737 BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an
2738 * active connection is initialized
2739 */
2740 BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an
2741 * active connection is
2742 * established
2743 */
2744 BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a
2745 * passive connection is
2746 * established
2747 */
Lawrence Brakmo91b5b212017-06-30 20:02:49 -07002748 BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control
2749 * needs ECN
2750 */
Lawrence Brakmoe6546ef2017-10-20 11:05:39 -07002751 BPF_SOCK_OPS_BASE_RTT, /* Get base RTT. The correct value is
2752 * based on the path and may be
2753 * dependent on the congestion control
2754 * algorithm. In general it indicates
2755 * a congestion threshold. RTTs above
2756 * this indicate congestion
2757 */
Lawrence Brakmof89013f2018-01-25 16:14:11 -08002758 BPF_SOCK_OPS_RTO_CB, /* Called when an RTO has triggered.
2759 * Arg1: value of icsk_retransmits
2760 * Arg2: value of icsk_rto
2761 * Arg3: whether RTO has expired
2762 */
Lawrence Brakmoa31ad292018-01-25 16:14:14 -08002763 BPF_SOCK_OPS_RETRANS_CB, /* Called when skb is retransmitted.
2764 * Arg1: sequence number of 1st byte
2765 * Arg2: # segments
2766 * Arg3: return value of
2767 * tcp_transmit_skb (0 => success)
2768 */
Lawrence Brakmod4487492018-01-25 16:14:15 -08002769 BPF_SOCK_OPS_STATE_CB, /* Called when TCP changes state.
2770 * Arg1: old_state
2771 * Arg2: new_state
2772 */
Andrey Ignatovf333ee02018-07-11 17:33:32 -07002773 BPF_SOCK_OPS_TCP_LISTEN_CB, /* Called on listen(2), right after
2774 * socket transition to LISTEN state.
2775 */
Lawrence Brakmod4487492018-01-25 16:14:15 -08002776};
2777
2778/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
2779 * changes between the TCP and BPF versions. Ideally this should never happen.
2780 * If it does, we need to add code to convert them before calling
2781 * the BPF sock_ops function.
2782 */
2783enum {
2784 BPF_TCP_ESTABLISHED = 1,
2785 BPF_TCP_SYN_SENT,
2786 BPF_TCP_SYN_RECV,
2787 BPF_TCP_FIN_WAIT1,
2788 BPF_TCP_FIN_WAIT2,
2789 BPF_TCP_TIME_WAIT,
2790 BPF_TCP_CLOSE,
2791 BPF_TCP_CLOSE_WAIT,
2792 BPF_TCP_LAST_ACK,
2793 BPF_TCP_LISTEN,
2794 BPF_TCP_CLOSING, /* Now a valid state */
2795 BPF_TCP_NEW_SYN_RECV,
2796
2797 BPF_TCP_MAX_STATES /* Leave at the end! */
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002798};
2799
Lawrence Brakmofc747812017-06-30 20:02:51 -07002800#define TCP_BPF_IW 1001 /* Set TCP initial congestion window */
Lawrence Brakmo13bf9642017-06-30 20:02:53 -07002801#define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */
Lawrence Brakmofc747812017-06-30 20:02:51 -07002802
Yonghong Song908432c2017-10-05 09:19:20 -07002803struct bpf_perf_event_value {
2804 __u64 counter;
2805 __u64 enabled;
2806 __u64 running;
2807};
2808
Roman Gushchinebc614f2017-11-05 08:15:32 -05002809#define BPF_DEVCG_ACC_MKNOD (1ULL << 0)
2810#define BPF_DEVCG_ACC_READ (1ULL << 1)
2811#define BPF_DEVCG_ACC_WRITE (1ULL << 2)
2812
2813#define BPF_DEVCG_DEV_BLOCK (1ULL << 0)
2814#define BPF_DEVCG_DEV_CHAR (1ULL << 1)
2815
2816struct bpf_cgroup_dev_ctx {
Yonghong Song06ef0cc2017-12-18 10:13:44 -08002817 /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */
2818 __u32 access_type;
Roman Gushchinebc614f2017-11-05 08:15:32 -05002819 __u32 major;
2820 __u32 minor;
2821};
2822
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002823struct bpf_raw_tracepoint_args {
2824 __u64 args[0];
2825};
2826
David Ahern87f5fc72018-05-09 20:34:26 -07002827/* DIRECT: Skip the FIB rules and go to FIB table associated with device
2828 * OUTPUT: Do lookup from egress perspective; default is ingress
2829 */
2830#define BPF_FIB_LOOKUP_DIRECT BIT(0)
2831#define BPF_FIB_LOOKUP_OUTPUT BIT(1)
2832
David Ahern4c795792018-06-26 16:21:18 -07002833enum {
2834 BPF_FIB_LKUP_RET_SUCCESS, /* lookup successful */
2835 BPF_FIB_LKUP_RET_BLACKHOLE, /* dest is blackholed; can be dropped */
2836 BPF_FIB_LKUP_RET_UNREACHABLE, /* dest is unreachable; can be dropped */
2837 BPF_FIB_LKUP_RET_PROHIBIT, /* dest not allowed; can be dropped */
2838 BPF_FIB_LKUP_RET_NOT_FWDED, /* packet is not forwarded */
2839 BPF_FIB_LKUP_RET_FWD_DISABLED, /* fwding is not enabled on ingress */
2840 BPF_FIB_LKUP_RET_UNSUPP_LWT, /* fwd requires encapsulation */
2841 BPF_FIB_LKUP_RET_NO_NEIGH, /* no neighbor entry for nh */
2842 BPF_FIB_LKUP_RET_FRAG_NEEDED, /* fragmentation required to fwd */
2843};
2844
David Ahern87f5fc72018-05-09 20:34:26 -07002845struct bpf_fib_lookup {
David Ahernfa898d72018-05-29 10:58:07 -07002846 /* input: network family for lookup (AF_INET, AF_INET6)
2847 * output: network family of egress nexthop
2848 */
2849 __u8 family;
David Ahern87f5fc72018-05-09 20:34:26 -07002850
2851 /* set if lookup is to consider L4 data - e.g., FIB rules */
2852 __u8 l4_protocol;
2853 __be16 sport;
2854 __be16 dport;
2855
2856 /* total length of packet from network header - used for MTU check */
2857 __u16 tot_len;
David Ahern4c795792018-06-26 16:21:18 -07002858
2859 /* input: L3 device index for lookup
2860 * output: device index from FIB lookup
2861 */
2862 __u32 ifindex;
David Ahern87f5fc72018-05-09 20:34:26 -07002863
2864 union {
2865 /* inputs to lookup */
2866 __u8 tos; /* AF_INET */
David Ahernbd3a08a2018-06-03 08:15:19 -07002867 __be32 flowinfo; /* AF_INET6, flow_label + priority */
David Ahern87f5fc72018-05-09 20:34:26 -07002868
David Ahernfa898d72018-05-29 10:58:07 -07002869 /* output: metric of fib result (IPv4/IPv6 only) */
2870 __u32 rt_metric;
David Ahern87f5fc72018-05-09 20:34:26 -07002871 };
2872
2873 union {
David Ahern87f5fc72018-05-09 20:34:26 -07002874 __be32 ipv4_src;
2875 __u32 ipv6_src[4]; /* in6_addr; network order */
2876 };
2877
David Ahernfa898d72018-05-29 10:58:07 -07002878 /* input to bpf_fib_lookup, ipv{4,6}_dst is destination address in
2879 * network header. output: bpf_fib_lookup sets to gateway address
2880 * if FIB lookup returns gateway route
David Ahern87f5fc72018-05-09 20:34:26 -07002881 */
2882 union {
David Ahern87f5fc72018-05-09 20:34:26 -07002883 __be32 ipv4_dst;
2884 __u32 ipv6_dst[4]; /* in6_addr; network order */
2885 };
2886
2887 /* output */
2888 __be16 h_vlan_proto;
2889 __be16 h_vlan_TCI;
2890 __u8 smac[6]; /* ETH_ALEN */
2891 __u8 dmac[6]; /* ETH_ALEN */
2892};
2893
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002894enum bpf_task_fd_type {
2895 BPF_FD_TYPE_RAW_TRACEPOINT, /* tp name */
2896 BPF_FD_TYPE_TRACEPOINT, /* tp name */
2897 BPF_FD_TYPE_KPROBE, /* (symbol + offset) or addr */
2898 BPF_FD_TYPE_KRETPROBE, /* (symbol + offset) or addr */
2899 BPF_FD_TYPE_UPROBE, /* filename + offset */
2900 BPF_FD_TYPE_URETPROBE, /* filename + offset */
2901};
2902
Petar Penkovd58e4682018-09-14 07:46:18 -07002903struct bpf_flow_keys {
2904 __u16 nhoff;
2905 __u16 thoff;
2906 __u16 addr_proto; /* ETH_P_* of valid addrs */
2907 __u8 is_frag;
2908 __u8 is_first_frag;
2909 __u8 is_encap;
2910 __u8 ip_proto;
2911 __be16 n_proto;
2912 __be16 sport;
2913 __be16 dport;
2914 union {
2915 struct {
2916 __be32 ipv4_src;
2917 __be32 ipv4_dst;
2918 };
2919 struct {
2920 __u32 ipv6_src[4]; /* in6_addr; network order */
2921 __u32 ipv6_dst[4]; /* in6_addr; network order */
2922 };
2923 };
2924};
2925
Alexei Starovoitovdaedfb22014-09-04 22:17:18 -07002926#endif /* _UAPI__LINUX_BPF_H__ */