1e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2daedfb22SAlexei Starovoitov /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com 3daedfb22SAlexei Starovoitov * 4daedfb22SAlexei Starovoitov * This program is free software; you can redistribute it and/or 5daedfb22SAlexei Starovoitov * modify it under the terms of version 2 of the GNU General Public 6daedfb22SAlexei Starovoitov * License as published by the Free Software Foundation. 7daedfb22SAlexei Starovoitov */ 8daedfb22SAlexei Starovoitov #ifndef _UAPI__LINUX_BPF_H__ 9daedfb22SAlexei Starovoitov #define _UAPI__LINUX_BPF_H__ 10daedfb22SAlexei Starovoitov 11daedfb22SAlexei Starovoitov #include <linux/types.h> 12c15952dcSAlexei Starovoitov #include <linux/bpf_common.h> 13daedfb22SAlexei Starovoitov 14daedfb22SAlexei Starovoitov /* Extended instruction set based on top of classic BPF */ 15daedfb22SAlexei Starovoitov 16daedfb22SAlexei Starovoitov /* instruction classes */ 17d405c740SJiong Wang #define BPF_JMP32 0x06 /* jmp mode in word width */ 18daedfb22SAlexei Starovoitov #define BPF_ALU64 0x07 /* alu mode in double word width */ 19daedfb22SAlexei Starovoitov 20daedfb22SAlexei Starovoitov /* ld/ldx fields */ 21cb5f7334SJesper Dangaard Brouer #define BPF_DW 0x18 /* double word (64-bit) */ 221f9a1ea8SYonghong Song #define BPF_MEMSX 0x80 /* load with sign extension */ 2391c960b0SBrendan Jackman #define BPF_ATOMIC 0xc0 /* atomic memory ops - op type in immediate */ 2491c960b0SBrendan Jackman #define BPF_XADD 0xc0 /* exclusive add - legacy name */ 25daedfb22SAlexei Starovoitov 26daedfb22SAlexei Starovoitov /* alu/jmp fields */ 27daedfb22SAlexei Starovoitov #define BPF_MOV 0xb0 /* mov reg to reg */ 28daedfb22SAlexei Starovoitov #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */ 29daedfb22SAlexei Starovoitov 30daedfb22SAlexei Starovoitov /* change endianness of a register */ 31daedfb22SAlexei Starovoitov #define BPF_END 0xd0 /* flags for endianness conversion: */ 32daedfb22SAlexei Starovoitov #define BPF_TO_LE 0x00 /* convert to little-endian */ 33daedfb22SAlexei Starovoitov #define BPF_TO_BE 0x08 /* convert to big-endian */ 34daedfb22SAlexei Starovoitov #define BPF_FROM_LE BPF_TO_LE 35daedfb22SAlexei Starovoitov #define BPF_FROM_BE BPF_TO_BE 36daedfb22SAlexei Starovoitov 3792b31a9aSDaniel Borkmann /* jmp encodings */ 38daedfb22SAlexei Starovoitov #define BPF_JNE 0x50 /* jump != */ 3992b31a9aSDaniel Borkmann #define BPF_JLT 0xa0 /* LT is unsigned, '<' */ 4092b31a9aSDaniel Borkmann #define BPF_JLE 0xb0 /* LE is unsigned, '<=' */ 41daedfb22SAlexei Starovoitov #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */ 42daedfb22SAlexei Starovoitov #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */ 4392b31a9aSDaniel Borkmann #define BPF_JSLT 0xc0 /* SLT is signed, '<' */ 4492b31a9aSDaniel Borkmann #define BPF_JSLE 0xd0 /* SLE is signed, '<=' */ 45011832b9SAlexei Starovoitov #define BPF_JCOND 0xe0 /* conditional pseudo jumps: may_goto, goto_or_nop */ 46daedfb22SAlexei Starovoitov #define BPF_CALL 0x80 /* function call */ 47daedfb22SAlexei Starovoitov #define BPF_EXIT 0x90 /* function return */ 48daedfb22SAlexei Starovoitov 495ca419f2SBrendan Jackman /* atomic op type fields (stored in immediate) */ 505ffa2550SBrendan Jackman #define BPF_FETCH 0x01 /* not an opcode on its own, used to build others */ 515ffa2550SBrendan Jackman #define BPF_XCHG (0xe0 | BPF_FETCH) /* atomic exchange */ 525ffa2550SBrendan Jackman #define BPF_CMPXCHG (0xf0 | BPF_FETCH) /* atomic compare-and-write */ 535ca419f2SBrendan Jackman 54011832b9SAlexei Starovoitov enum bpf_cond_pseudo_jmp { 55011832b9SAlexei Starovoitov BPF_MAY_GOTO = 0, 56011832b9SAlexei Starovoitov }; 57011832b9SAlexei Starovoitov 58daedfb22SAlexei Starovoitov /* Register numbers */ 59daedfb22SAlexei Starovoitov enum { 60daedfb22SAlexei Starovoitov BPF_REG_0 = 0, 61daedfb22SAlexei Starovoitov BPF_REG_1, 62daedfb22SAlexei Starovoitov BPF_REG_2, 63daedfb22SAlexei Starovoitov BPF_REG_3, 64daedfb22SAlexei Starovoitov BPF_REG_4, 65daedfb22SAlexei Starovoitov BPF_REG_5, 66daedfb22SAlexei Starovoitov BPF_REG_6, 67daedfb22SAlexei Starovoitov BPF_REG_7, 68daedfb22SAlexei Starovoitov BPF_REG_8, 69daedfb22SAlexei Starovoitov BPF_REG_9, 70daedfb22SAlexei Starovoitov BPF_REG_10, 71daedfb22SAlexei Starovoitov __MAX_BPF_REG, 72daedfb22SAlexei Starovoitov }; 73daedfb22SAlexei Starovoitov 74daedfb22SAlexei Starovoitov /* BPF has 10 general purpose 64-bit registers and stack frame. */ 75daedfb22SAlexei Starovoitov #define MAX_BPF_REG __MAX_BPF_REG 76daedfb22SAlexei Starovoitov 77daedfb22SAlexei Starovoitov struct bpf_insn { 78daedfb22SAlexei Starovoitov __u8 code; /* opcode */ 79daedfb22SAlexei Starovoitov __u8 dst_reg:4; /* dest register */ 80daedfb22SAlexei Starovoitov __u8 src_reg:4; /* source register */ 81daedfb22SAlexei Starovoitov __s16 off; /* signed offset */ 82daedfb22SAlexei Starovoitov __s32 imm; /* signed immediate constant */ 83daedfb22SAlexei Starovoitov }; 84daedfb22SAlexei Starovoitov 85896880ffSKees Cook /* Deprecated: use struct bpf_lpm_trie_key_u8 (when the "data" member is needed for 86896880ffSKees Cook * byte access) or struct bpf_lpm_trie_key_hdr (when using an alternative type for 87896880ffSKees Cook * the trailing flexible array member) instead. 88896880ffSKees Cook */ 89b95a5c4dSDaniel Mack struct bpf_lpm_trie_key { 90b95a5c4dSDaniel Mack __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */ 913024d95aSDaniel Borkmann __u8 data[0]; /* Arbitrary size */ 92b95a5c4dSDaniel Mack }; 93b95a5c4dSDaniel Mack 94896880ffSKees Cook /* Header for bpf_lpm_trie_key structs */ 95896880ffSKees Cook struct bpf_lpm_trie_key_hdr { 96896880ffSKees Cook __u32 prefixlen; 97896880ffSKees Cook }; 98896880ffSKees Cook 99896880ffSKees Cook /* Key of an a BPF_MAP_TYPE_LPM_TRIE entry, with trailing byte array. */ 100896880ffSKees Cook struct bpf_lpm_trie_key_u8 { 101896880ffSKees Cook union { 102896880ffSKees Cook struct bpf_lpm_trie_key_hdr hdr; 103896880ffSKees Cook __u32 prefixlen; 104896880ffSKees Cook }; 105896880ffSKees Cook __u8 data[]; /* Arbitrary size */ 106896880ffSKees Cook }; 107896880ffSKees Cook 108de9cbbaaSRoman Gushchin struct bpf_cgroup_storage_key { 109de9cbbaaSRoman Gushchin __u64 cgroup_inode_id; /* cgroup inode id */ 1106fc88c35SDave Marchevsky __u32 attach_type; /* program attach type (enum bpf_attach_type) */ 111de9cbbaaSRoman Gushchin }; 112de9cbbaaSRoman Gushchin 113d4ccaf58SHao Luo enum bpf_cgroup_iter_order { 114d4ffb6f3SHao Luo BPF_CGROUP_ITER_ORDER_UNSPEC = 0, 115d4ffb6f3SHao Luo BPF_CGROUP_ITER_SELF_ONLY, /* process only a single object. */ 116d4ffb6f3SHao Luo BPF_CGROUP_ITER_DESCENDANTS_PRE, /* walk descendants in pre-order. */ 117d4ffb6f3SHao Luo BPF_CGROUP_ITER_DESCENDANTS_POST, /* walk descendants in post-order. */ 118d4ffb6f3SHao Luo BPF_CGROUP_ITER_ANCESTORS_UP, /* walk ancestors upward. */ 119d4ccaf58SHao Luo }; 120d4ccaf58SHao Luo 1215e7b3020SYonghong Song union bpf_iter_link_info { 1225e7b3020SYonghong Song struct { 1235e7b3020SYonghong Song __u32 map_fd; 1245e7b3020SYonghong Song } map; 125d4ccaf58SHao Luo struct { 126d4ccaf58SHao Luo enum bpf_cgroup_iter_order order; 127d4ccaf58SHao Luo 128d4ccaf58SHao Luo /* At most one of cgroup_fd and cgroup_id can be non-zero. If 129d4ccaf58SHao Luo * both are zero, the walk starts from the default cgroup v2 130d4ccaf58SHao Luo * root. For walking v1 hierarchy, one should always explicitly 131d4ccaf58SHao Luo * specify cgroup_fd. 132d4ccaf58SHao Luo */ 133d4ccaf58SHao Luo __u32 cgroup_fd; 134d4ccaf58SHao Luo __u64 cgroup_id; 135d4ccaf58SHao Luo } cgroup; 136f0d74c4dSKui-Feng Lee /* Parameters of task iterators. */ 137f0d74c4dSKui-Feng Lee struct { 138f0d74c4dSKui-Feng Lee __u32 tid; 139f0d74c4dSKui-Feng Lee __u32 pid; 140f0d74c4dSKui-Feng Lee __u32 pid_fd; 141f0d74c4dSKui-Feng Lee } task; 1425e7b3020SYonghong Song }; 1435e7b3020SYonghong Song 1447799e4d9SJoe Stringer /* BPF syscall commands, see bpf(2) man-page for more details. */ 1457799e4d9SJoe Stringer /** 1467799e4d9SJoe Stringer * DOC: eBPF Syscall Preamble 1477799e4d9SJoe Stringer * 1487799e4d9SJoe Stringer * The operation to be performed by the **bpf**\ () system call is determined 1497799e4d9SJoe Stringer * by the *cmd* argument. Each operation takes an accompanying argument, 1507799e4d9SJoe Stringer * provided via *attr*, which is a pointer to a union of type *bpf_attr* (see 1517799e4d9SJoe Stringer * below). The size argument is the size of the union pointed to by *attr*. 1527799e4d9SJoe Stringer */ 1537799e4d9SJoe Stringer /** 1547799e4d9SJoe Stringer * DOC: eBPF Syscall Commands 1557799e4d9SJoe Stringer * 1567799e4d9SJoe Stringer * BPF_MAP_CREATE 1577799e4d9SJoe Stringer * Description 1587799e4d9SJoe Stringer * Create a map and return a file descriptor that refers to the 1597799e4d9SJoe Stringer * map. The close-on-exec file descriptor flag (see **fcntl**\ (2)) 1607799e4d9SJoe Stringer * is automatically enabled for the new file descriptor. 1617799e4d9SJoe Stringer * 1627799e4d9SJoe Stringer * Applying **close**\ (2) to the file descriptor returned by 1637799e4d9SJoe Stringer * **BPF_MAP_CREATE** will delete the map (but see NOTES). 1647799e4d9SJoe Stringer * 1657799e4d9SJoe Stringer * Return 1667799e4d9SJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 1677799e4d9SJoe Stringer * error occurred (in which case, *errno* is set appropriately). 1687799e4d9SJoe Stringer * 1697799e4d9SJoe Stringer * BPF_MAP_LOOKUP_ELEM 1707799e4d9SJoe Stringer * Description 1717799e4d9SJoe Stringer * Look up an element with a given *key* in the map referred to 1727799e4d9SJoe Stringer * by the file descriptor *map_fd*. 1737799e4d9SJoe Stringer * 1746690523bSJoe Stringer * The *flags* argument may be specified as one of the 1756690523bSJoe Stringer * following: 1766690523bSJoe Stringer * 1776690523bSJoe Stringer * **BPF_F_LOCK** 1786690523bSJoe Stringer * Look up the value of a spin-locked map without 1796690523bSJoe Stringer * returning the lock. This must be specified if the 1806690523bSJoe Stringer * elements contain a spinlock. 1816690523bSJoe Stringer * 1827799e4d9SJoe Stringer * Return 1837799e4d9SJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 1847799e4d9SJoe Stringer * is set appropriately. 1857799e4d9SJoe Stringer * 1867799e4d9SJoe Stringer * BPF_MAP_UPDATE_ELEM 1877799e4d9SJoe Stringer * Description 1887799e4d9SJoe Stringer * Create or update an element (key/value pair) in a specified map. 1897799e4d9SJoe Stringer * 1907799e4d9SJoe Stringer * The *flags* argument should be specified as one of the 1917799e4d9SJoe Stringer * following: 1927799e4d9SJoe Stringer * 1937799e4d9SJoe Stringer * **BPF_ANY** 1947799e4d9SJoe Stringer * Create a new element or update an existing element. 1957799e4d9SJoe Stringer * **BPF_NOEXIST** 1967799e4d9SJoe Stringer * Create a new element only if it did not exist. 1977799e4d9SJoe Stringer * **BPF_EXIST** 1987799e4d9SJoe Stringer * Update an existing element. 1996690523bSJoe Stringer * **BPF_F_LOCK** 2006690523bSJoe Stringer * Update a spin_lock-ed map element. 2017799e4d9SJoe Stringer * 2027799e4d9SJoe Stringer * Return 2037799e4d9SJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 2047799e4d9SJoe Stringer * is set appropriately. 2057799e4d9SJoe Stringer * 2067799e4d9SJoe Stringer * May set *errno* to **EINVAL**, **EPERM**, **ENOMEM**, 2077799e4d9SJoe Stringer * **E2BIG**, **EEXIST**, or **ENOENT**. 2087799e4d9SJoe Stringer * 2097799e4d9SJoe Stringer * **E2BIG** 2107799e4d9SJoe Stringer * The number of elements in the map reached the 2117799e4d9SJoe Stringer * *max_entries* limit specified at map creation time. 2127799e4d9SJoe Stringer * **EEXIST** 2137799e4d9SJoe Stringer * If *flags* specifies **BPF_NOEXIST** and the element 2147799e4d9SJoe Stringer * with *key* already exists in the map. 2157799e4d9SJoe Stringer * **ENOENT** 2167799e4d9SJoe Stringer * If *flags* specifies **BPF_EXIST** and the element with 2177799e4d9SJoe Stringer * *key* does not exist in the map. 2187799e4d9SJoe Stringer * 2197799e4d9SJoe Stringer * BPF_MAP_DELETE_ELEM 2207799e4d9SJoe Stringer * Description 2217799e4d9SJoe Stringer * Look up and delete an element by key in a specified map. 2227799e4d9SJoe Stringer * 2237799e4d9SJoe Stringer * Return 2247799e4d9SJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 2257799e4d9SJoe Stringer * is set appropriately. 2267799e4d9SJoe Stringer * 2277799e4d9SJoe Stringer * BPF_MAP_GET_NEXT_KEY 2287799e4d9SJoe Stringer * Description 2297799e4d9SJoe Stringer * Look up an element by key in a specified map and return the key 2307799e4d9SJoe Stringer * of the next element. Can be used to iterate over all elements 2317799e4d9SJoe Stringer * in the map. 2327799e4d9SJoe Stringer * 2337799e4d9SJoe Stringer * Return 2347799e4d9SJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 2357799e4d9SJoe Stringer * is set appropriately. 2367799e4d9SJoe Stringer * 2377799e4d9SJoe Stringer * The following cases can be used to iterate over all elements of 2387799e4d9SJoe Stringer * the map: 2397799e4d9SJoe Stringer * 2407799e4d9SJoe Stringer * * If *key* is not found, the operation returns zero and sets 2417799e4d9SJoe Stringer * the *next_key* pointer to the key of the first element. 2427799e4d9SJoe Stringer * * If *key* is found, the operation returns zero and sets the 2437799e4d9SJoe Stringer * *next_key* pointer to the key of the next element. 2447799e4d9SJoe Stringer * * If *key* is the last element, returns -1 and *errno* is set 2457799e4d9SJoe Stringer * to **ENOENT**. 2467799e4d9SJoe Stringer * 2477799e4d9SJoe Stringer * May set *errno* to **ENOMEM**, **EFAULT**, **EPERM**, or 2487799e4d9SJoe Stringer * **EINVAL** on error. 2497799e4d9SJoe Stringer * 2507799e4d9SJoe Stringer * BPF_PROG_LOAD 2517799e4d9SJoe Stringer * Description 2527799e4d9SJoe Stringer * Verify and load an eBPF program, returning a new file 2537799e4d9SJoe Stringer * descriptor associated with the program. 2547799e4d9SJoe Stringer * 2557799e4d9SJoe Stringer * Applying **close**\ (2) to the file descriptor returned by 2567799e4d9SJoe Stringer * **BPF_PROG_LOAD** will unload the eBPF program (but see NOTES). 2577799e4d9SJoe Stringer * 2587799e4d9SJoe Stringer * The close-on-exec file descriptor flag (see **fcntl**\ (2)) is 2597799e4d9SJoe Stringer * automatically enabled for the new file descriptor. 2607799e4d9SJoe Stringer * 2617799e4d9SJoe Stringer * Return 2627799e4d9SJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 2637799e4d9SJoe Stringer * error occurred (in which case, *errno* is set appropriately). 2647799e4d9SJoe Stringer * 265f67c9cbfSJoe Stringer * BPF_OBJ_PIN 266f67c9cbfSJoe Stringer * Description 267f67c9cbfSJoe Stringer * Pin an eBPF program or map referred by the specified *bpf_fd* 268f67c9cbfSJoe Stringer * to the provided *pathname* on the filesystem. 269f67c9cbfSJoe Stringer * 2708aacb3c8SJoe Stringer * The *pathname* argument must not contain a dot ("."). 2718aacb3c8SJoe Stringer * 2728aacb3c8SJoe Stringer * On success, *pathname* retains a reference to the eBPF object, 2738aacb3c8SJoe Stringer * preventing deallocation of the object when the original 2748aacb3c8SJoe Stringer * *bpf_fd* is closed. This allow the eBPF object to live beyond 2758aacb3c8SJoe Stringer * **close**\ (\ *bpf_fd*\ ), and hence the lifetime of the parent 2768aacb3c8SJoe Stringer * process. 2778aacb3c8SJoe Stringer * 2788aacb3c8SJoe Stringer * Applying **unlink**\ (2) or similar calls to the *pathname* 2798aacb3c8SJoe Stringer * unpins the object from the filesystem, removing the reference. 2808aacb3c8SJoe Stringer * If no other file descriptors or filesystem nodes refer to the 2818aacb3c8SJoe Stringer * same object, it will be deallocated (see NOTES). 2828aacb3c8SJoe Stringer * 2838aacb3c8SJoe Stringer * The filesystem type for the parent directory of *pathname* must 2848aacb3c8SJoe Stringer * be **BPF_FS_MAGIC**. 2858aacb3c8SJoe Stringer * 286f67c9cbfSJoe Stringer * Return 287f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 288f67c9cbfSJoe Stringer * is set appropriately. 289f67c9cbfSJoe Stringer * 290f67c9cbfSJoe Stringer * BPF_OBJ_GET 291f67c9cbfSJoe Stringer * Description 292f67c9cbfSJoe Stringer * Open a file descriptor for the eBPF object pinned to the 293f67c9cbfSJoe Stringer * specified *pathname*. 294f67c9cbfSJoe Stringer * 295f67c9cbfSJoe Stringer * Return 296f67c9cbfSJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 297f67c9cbfSJoe Stringer * error occurred (in which case, *errno* is set appropriately). 298f67c9cbfSJoe Stringer * 299f67c9cbfSJoe Stringer * BPF_PROG_ATTACH 300f67c9cbfSJoe Stringer * Description 301f67c9cbfSJoe Stringer * Attach an eBPF program to a *target_fd* at the specified 302f67c9cbfSJoe Stringer * *attach_type* hook. 303f67c9cbfSJoe Stringer * 30432e76b18SJoe Stringer * The *attach_type* specifies the eBPF attachment point to 30532e76b18SJoe Stringer * attach the program to, and must be one of *bpf_attach_type* 30632e76b18SJoe Stringer * (see below). 30732e76b18SJoe Stringer * 30832e76b18SJoe Stringer * The *attach_bpf_fd* must be a valid file descriptor for a 30932e76b18SJoe Stringer * loaded eBPF program of a cgroup, flow dissector, LIRC, sockmap 31032e76b18SJoe Stringer * or sock_ops type corresponding to the specified *attach_type*. 31132e76b18SJoe Stringer * 31232e76b18SJoe Stringer * The *target_fd* must be a valid file descriptor for a kernel 31332e76b18SJoe Stringer * object which depends on the attach type of *attach_bpf_fd*: 31432e76b18SJoe Stringer * 31532e76b18SJoe Stringer * **BPF_PROG_TYPE_CGROUP_DEVICE**, 31632e76b18SJoe Stringer * **BPF_PROG_TYPE_CGROUP_SKB**, 31732e76b18SJoe Stringer * **BPF_PROG_TYPE_CGROUP_SOCK**, 31832e76b18SJoe Stringer * **BPF_PROG_TYPE_CGROUP_SOCK_ADDR**, 31932e76b18SJoe Stringer * **BPF_PROG_TYPE_CGROUP_SOCKOPT**, 32032e76b18SJoe Stringer * **BPF_PROG_TYPE_CGROUP_SYSCTL**, 32132e76b18SJoe Stringer * **BPF_PROG_TYPE_SOCK_OPS** 32232e76b18SJoe Stringer * 32332e76b18SJoe Stringer * Control Group v2 hierarchy with the eBPF controller 32432e76b18SJoe Stringer * enabled. Requires the kernel to be compiled with 32532e76b18SJoe Stringer * **CONFIG_CGROUP_BPF**. 32632e76b18SJoe Stringer * 32732e76b18SJoe Stringer * **BPF_PROG_TYPE_FLOW_DISSECTOR** 32832e76b18SJoe Stringer * 32932e76b18SJoe Stringer * Network namespace (eg /proc/self/ns/net). 33032e76b18SJoe Stringer * 33132e76b18SJoe Stringer * **BPF_PROG_TYPE_LIRC_MODE2** 33232e76b18SJoe Stringer * 33332e76b18SJoe Stringer * LIRC device path (eg /dev/lircN). Requires the kernel 33432e76b18SJoe Stringer * to be compiled with **CONFIG_BPF_LIRC_MODE2**. 33532e76b18SJoe Stringer * 33632e76b18SJoe Stringer * **BPF_PROG_TYPE_SK_SKB**, 33732e76b18SJoe Stringer * **BPF_PROG_TYPE_SK_MSG** 33832e76b18SJoe Stringer * 33932e76b18SJoe Stringer * eBPF map of socket type (eg **BPF_MAP_TYPE_SOCKHASH**). 34032e76b18SJoe Stringer * 341f67c9cbfSJoe Stringer * Return 342f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 343f67c9cbfSJoe Stringer * is set appropriately. 344f67c9cbfSJoe Stringer * 345f67c9cbfSJoe Stringer * BPF_PROG_DETACH 346f67c9cbfSJoe Stringer * Description 347f67c9cbfSJoe Stringer * Detach the eBPF program associated with the *target_fd* at the 348f67c9cbfSJoe Stringer * hook specified by *attach_type*. The program must have been 349f67c9cbfSJoe Stringer * previously attached using **BPF_PROG_ATTACH**. 350f67c9cbfSJoe Stringer * 351f67c9cbfSJoe Stringer * Return 352f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 353f67c9cbfSJoe Stringer * is set appropriately. 354f67c9cbfSJoe Stringer * 355f67c9cbfSJoe Stringer * BPF_PROG_TEST_RUN 356f67c9cbfSJoe Stringer * Description 3572a3fdca4SJoe Stringer * Run the eBPF program associated with the *prog_fd* a *repeat* 3582a3fdca4SJoe Stringer * number of times against a provided program context *ctx_in* and 3592a3fdca4SJoe Stringer * data *data_in*, and return the modified program context 3602a3fdca4SJoe Stringer * *ctx_out*, *data_out* (for example, packet data), result of the 3612a3fdca4SJoe Stringer * execution *retval*, and *duration* of the test run. 362f67c9cbfSJoe Stringer * 363f3c45326SJoe Stringer * The sizes of the buffers provided as input and output 364f3c45326SJoe Stringer * parameters *ctx_in*, *ctx_out*, *data_in*, and *data_out* must 365f3c45326SJoe Stringer * be provided in the corresponding variables *ctx_size_in*, 366f3c45326SJoe Stringer * *ctx_size_out*, *data_size_in*, and/or *data_size_out*. If any 367f3c45326SJoe Stringer * of these parameters are not provided (ie set to NULL), the 368f3c45326SJoe Stringer * corresponding size field must be zero. 369f3c45326SJoe Stringer * 370f3c45326SJoe Stringer * Some program types have particular requirements: 371f3c45326SJoe Stringer * 372f3c45326SJoe Stringer * **BPF_PROG_TYPE_SK_LOOKUP** 373f3c45326SJoe Stringer * *data_in* and *data_out* must be NULL. 374f3c45326SJoe Stringer * 375f3c45326SJoe Stringer * **BPF_PROG_TYPE_RAW_TRACEPOINT**, 376f3c45326SJoe Stringer * **BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE** 377f3c45326SJoe Stringer * 378f3c45326SJoe Stringer * *ctx_out*, *data_in* and *data_out* must be NULL. 379f3c45326SJoe Stringer * *repeat* must be zero. 380f3c45326SJoe Stringer * 381e40fbbf0SUsama Arif * BPF_PROG_RUN is an alias for BPF_PROG_TEST_RUN. 382e40fbbf0SUsama Arif * 383f67c9cbfSJoe Stringer * Return 384f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 385f67c9cbfSJoe Stringer * is set appropriately. 386f67c9cbfSJoe Stringer * 3872a3fdca4SJoe Stringer * **ENOSPC** 3882a3fdca4SJoe Stringer * Either *data_size_out* or *ctx_size_out* is too small. 3892a3fdca4SJoe Stringer * **ENOTSUPP** 3902a3fdca4SJoe Stringer * This command is not supported by the program type of 3912a3fdca4SJoe Stringer * the program referred to by *prog_fd*. 3922a3fdca4SJoe Stringer * 393f67c9cbfSJoe Stringer * BPF_PROG_GET_NEXT_ID 394f67c9cbfSJoe Stringer * Description 395f67c9cbfSJoe Stringer * Fetch the next eBPF program currently loaded into the kernel. 396f67c9cbfSJoe Stringer * 397f67c9cbfSJoe Stringer * Looks for the eBPF program with an id greater than *start_id* 398f67c9cbfSJoe Stringer * and updates *next_id* on success. If no other eBPF programs 399f67c9cbfSJoe Stringer * remain with ids higher than *start_id*, returns -1 and sets 400f67c9cbfSJoe Stringer * *errno* to **ENOENT**. 401f67c9cbfSJoe Stringer * 402f67c9cbfSJoe Stringer * Return 403f67c9cbfSJoe Stringer * Returns zero on success. On error, or when no id remains, -1 404f67c9cbfSJoe Stringer * is returned and *errno* is set appropriately. 405f67c9cbfSJoe Stringer * 406f67c9cbfSJoe Stringer * BPF_MAP_GET_NEXT_ID 407f67c9cbfSJoe Stringer * Description 408f67c9cbfSJoe Stringer * Fetch the next eBPF map currently loaded into the kernel. 409f67c9cbfSJoe Stringer * 410f67c9cbfSJoe Stringer * Looks for the eBPF map with an id greater than *start_id* 411f67c9cbfSJoe Stringer * and updates *next_id* on success. If no other eBPF maps 412f67c9cbfSJoe Stringer * remain with ids higher than *start_id*, returns -1 and sets 413f67c9cbfSJoe Stringer * *errno* to **ENOENT**. 414f67c9cbfSJoe Stringer * 415f67c9cbfSJoe Stringer * Return 416f67c9cbfSJoe Stringer * Returns zero on success. On error, or when no id remains, -1 417f67c9cbfSJoe Stringer * is returned and *errno* is set appropriately. 418f67c9cbfSJoe Stringer * 419f67c9cbfSJoe Stringer * BPF_PROG_GET_FD_BY_ID 420f67c9cbfSJoe Stringer * Description 421f67c9cbfSJoe Stringer * Open a file descriptor for the eBPF program corresponding to 422f67c9cbfSJoe Stringer * *prog_id*. 423f67c9cbfSJoe Stringer * 424f67c9cbfSJoe Stringer * Return 425f67c9cbfSJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 426f67c9cbfSJoe Stringer * error occurred (in which case, *errno* is set appropriately). 427f67c9cbfSJoe Stringer * 428f67c9cbfSJoe Stringer * BPF_MAP_GET_FD_BY_ID 429f67c9cbfSJoe Stringer * Description 430f67c9cbfSJoe Stringer * Open a file descriptor for the eBPF map corresponding to 431f67c9cbfSJoe Stringer * *map_id*. 432f67c9cbfSJoe Stringer * 433f67c9cbfSJoe Stringer * Return 434f67c9cbfSJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 435f67c9cbfSJoe Stringer * error occurred (in which case, *errno* is set appropriately). 436f67c9cbfSJoe Stringer * 437f67c9cbfSJoe Stringer * BPF_OBJ_GET_INFO_BY_FD 438f67c9cbfSJoe Stringer * Description 439f67c9cbfSJoe Stringer * Obtain information about the eBPF object corresponding to 440f67c9cbfSJoe Stringer * *bpf_fd*. 441f67c9cbfSJoe Stringer * 442f67c9cbfSJoe Stringer * Populates up to *info_len* bytes of *info*, which will be in 443f67c9cbfSJoe Stringer * one of the following formats depending on the eBPF object type 444f67c9cbfSJoe Stringer * of *bpf_fd*: 445f67c9cbfSJoe Stringer * 446f67c9cbfSJoe Stringer * * **struct bpf_prog_info** 447f67c9cbfSJoe Stringer * * **struct bpf_map_info** 448f67c9cbfSJoe Stringer * * **struct bpf_btf_info** 449f67c9cbfSJoe Stringer * * **struct bpf_link_info** 450f67c9cbfSJoe Stringer * 451f67c9cbfSJoe Stringer * Return 452f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 453f67c9cbfSJoe Stringer * is set appropriately. 454f67c9cbfSJoe Stringer * 455f67c9cbfSJoe Stringer * BPF_PROG_QUERY 456f67c9cbfSJoe Stringer * Description 457f67c9cbfSJoe Stringer * Obtain information about eBPF programs associated with the 458f67c9cbfSJoe Stringer * specified *attach_type* hook. 459f67c9cbfSJoe Stringer * 4605d999994SJoe Stringer * The *target_fd* must be a valid file descriptor for a kernel 4615d999994SJoe Stringer * object which depends on the attach type of *attach_bpf_fd*: 4625d999994SJoe Stringer * 4635d999994SJoe Stringer * **BPF_PROG_TYPE_CGROUP_DEVICE**, 4645d999994SJoe Stringer * **BPF_PROG_TYPE_CGROUP_SKB**, 4655d999994SJoe Stringer * **BPF_PROG_TYPE_CGROUP_SOCK**, 4665d999994SJoe Stringer * **BPF_PROG_TYPE_CGROUP_SOCK_ADDR**, 4675d999994SJoe Stringer * **BPF_PROG_TYPE_CGROUP_SOCKOPT**, 4685d999994SJoe Stringer * **BPF_PROG_TYPE_CGROUP_SYSCTL**, 4695d999994SJoe Stringer * **BPF_PROG_TYPE_SOCK_OPS** 4705d999994SJoe Stringer * 4715d999994SJoe Stringer * Control Group v2 hierarchy with the eBPF controller 4725d999994SJoe Stringer * enabled. Requires the kernel to be compiled with 4735d999994SJoe Stringer * **CONFIG_CGROUP_BPF**. 4745d999994SJoe Stringer * 4755d999994SJoe Stringer * **BPF_PROG_TYPE_FLOW_DISSECTOR** 4765d999994SJoe Stringer * 4775d999994SJoe Stringer * Network namespace (eg /proc/self/ns/net). 4785d999994SJoe Stringer * 4795d999994SJoe Stringer * **BPF_PROG_TYPE_LIRC_MODE2** 4805d999994SJoe Stringer * 4815d999994SJoe Stringer * LIRC device path (eg /dev/lircN). Requires the kernel 4825d999994SJoe Stringer * to be compiled with **CONFIG_BPF_LIRC_MODE2**. 4835d999994SJoe Stringer * 4845d999994SJoe Stringer * **BPF_PROG_QUERY** always fetches the number of programs 4855d999994SJoe Stringer * attached and the *attach_flags* which were used to attach those 4865d999994SJoe Stringer * programs. Additionally, if *prog_ids* is nonzero and the number 4875d999994SJoe Stringer * of attached programs is less than *prog_cnt*, populates 4885d999994SJoe Stringer * *prog_ids* with the eBPF program ids of the programs attached 4895d999994SJoe Stringer * at *target_fd*. 4905d999994SJoe Stringer * 4915d999994SJoe Stringer * The following flags may alter the result: 4925d999994SJoe Stringer * 4935d999994SJoe Stringer * **BPF_F_QUERY_EFFECTIVE** 4945d999994SJoe Stringer * Only return information regarding programs which are 4955d999994SJoe Stringer * currently effective at the specified *target_fd*. 4965d999994SJoe Stringer * 497f67c9cbfSJoe Stringer * Return 498f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 499f67c9cbfSJoe Stringer * is set appropriately. 500f67c9cbfSJoe Stringer * 501f67c9cbfSJoe Stringer * BPF_RAW_TRACEPOINT_OPEN 502f67c9cbfSJoe Stringer * Description 503f67c9cbfSJoe Stringer * Attach an eBPF program to a tracepoint *name* to access kernel 504f67c9cbfSJoe Stringer * internal arguments of the tracepoint in their raw form. 505f67c9cbfSJoe Stringer * 506f67c9cbfSJoe Stringer * The *prog_fd* must be a valid file descriptor associated with 507f67c9cbfSJoe Stringer * a loaded eBPF program of type **BPF_PROG_TYPE_RAW_TRACEPOINT**. 508f67c9cbfSJoe Stringer * 509f67c9cbfSJoe Stringer * No ABI guarantees are made about the content of tracepoint 510f67c9cbfSJoe Stringer * arguments exposed to the corresponding eBPF program. 511f67c9cbfSJoe Stringer * 512f67c9cbfSJoe Stringer * Applying **close**\ (2) to the file descriptor returned by 513f67c9cbfSJoe Stringer * **BPF_RAW_TRACEPOINT_OPEN** will delete the map (but see NOTES). 514f67c9cbfSJoe Stringer * 515f67c9cbfSJoe Stringer * Return 516f67c9cbfSJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 517f67c9cbfSJoe Stringer * error occurred (in which case, *errno* is set appropriately). 518f67c9cbfSJoe Stringer * 519f67c9cbfSJoe Stringer * BPF_BTF_LOAD 520f67c9cbfSJoe Stringer * Description 521f67c9cbfSJoe Stringer * Verify and load BPF Type Format (BTF) metadata into the kernel, 522f67c9cbfSJoe Stringer * returning a new file descriptor associated with the metadata. 523f67c9cbfSJoe Stringer * BTF is described in more detail at 524f67c9cbfSJoe Stringer * https://www.kernel.org/doc/html/latest/bpf/btf.html. 525f67c9cbfSJoe Stringer * 526f67c9cbfSJoe Stringer * The *btf* parameter must point to valid memory providing 527f67c9cbfSJoe Stringer * *btf_size* bytes of BTF binary metadata. 528f67c9cbfSJoe Stringer * 529f67c9cbfSJoe Stringer * The returned file descriptor can be passed to other **bpf**\ () 530f67c9cbfSJoe Stringer * subcommands such as **BPF_PROG_LOAD** or **BPF_MAP_CREATE** to 531f67c9cbfSJoe Stringer * associate the BTF with those objects. 532f67c9cbfSJoe Stringer * 533f67c9cbfSJoe Stringer * Similar to **BPF_PROG_LOAD**, **BPF_BTF_LOAD** has optional 534f67c9cbfSJoe Stringer * parameters to specify a *btf_log_buf*, *btf_log_size* and 535f67c9cbfSJoe Stringer * *btf_log_level* which allow the kernel to return freeform log 536f67c9cbfSJoe Stringer * output regarding the BTF verification process. 537f67c9cbfSJoe Stringer * 538f67c9cbfSJoe Stringer * Return 539f67c9cbfSJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 540f67c9cbfSJoe Stringer * error occurred (in which case, *errno* is set appropriately). 541f67c9cbfSJoe Stringer * 542f67c9cbfSJoe Stringer * BPF_BTF_GET_FD_BY_ID 543f67c9cbfSJoe Stringer * Description 544f67c9cbfSJoe Stringer * Open a file descriptor for the BPF Type Format (BTF) 545f67c9cbfSJoe Stringer * corresponding to *btf_id*. 546f67c9cbfSJoe Stringer * 547f67c9cbfSJoe Stringer * Return 548f67c9cbfSJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 549f67c9cbfSJoe Stringer * error occurred (in which case, *errno* is set appropriately). 550f67c9cbfSJoe Stringer * 551f67c9cbfSJoe Stringer * BPF_TASK_FD_QUERY 552f67c9cbfSJoe Stringer * Description 553f67c9cbfSJoe Stringer * Obtain information about eBPF programs associated with the 554f67c9cbfSJoe Stringer * target process identified by *pid* and *fd*. 555f67c9cbfSJoe Stringer * 556f67c9cbfSJoe Stringer * If the *pid* and *fd* are associated with a tracepoint, kprobe 557f67c9cbfSJoe Stringer * or uprobe perf event, then the *prog_id* and *fd_type* will 558f67c9cbfSJoe Stringer * be populated with the eBPF program id and file descriptor type 559f67c9cbfSJoe Stringer * of type **bpf_task_fd_type**. If associated with a kprobe or 560f67c9cbfSJoe Stringer * uprobe, the *probe_offset* and *probe_addr* will also be 561f67c9cbfSJoe Stringer * populated. Optionally, if *buf* is provided, then up to 562f67c9cbfSJoe Stringer * *buf_len* bytes of *buf* will be populated with the name of 563f67c9cbfSJoe Stringer * the tracepoint, kprobe or uprobe. 564f67c9cbfSJoe Stringer * 565f67c9cbfSJoe Stringer * The resulting *prog_id* may be introspected in deeper detail 566f67c9cbfSJoe Stringer * using **BPF_PROG_GET_FD_BY_ID** and **BPF_OBJ_GET_INFO_BY_FD**. 567f67c9cbfSJoe Stringer * 568f67c9cbfSJoe Stringer * Return 569f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 570f67c9cbfSJoe Stringer * is set appropriately. 571f67c9cbfSJoe Stringer * 572f67c9cbfSJoe Stringer * BPF_MAP_LOOKUP_AND_DELETE_ELEM 573f67c9cbfSJoe Stringer * Description 574f67c9cbfSJoe Stringer * Look up an element with the given *key* in the map referred to 575f67c9cbfSJoe Stringer * by the file descriptor *fd*, and if found, delete the element. 576f67c9cbfSJoe Stringer * 5773e87f192SDenis Salopek * For **BPF_MAP_TYPE_QUEUE** and **BPF_MAP_TYPE_STACK** map 5783e87f192SDenis Salopek * types, the *flags* argument needs to be set to 0, but for other 5793e87f192SDenis Salopek * map types, it may be specified as: 5803e87f192SDenis Salopek * 5813e87f192SDenis Salopek * **BPF_F_LOCK** 5823e87f192SDenis Salopek * Look up and delete the value of a spin-locked map 5833e87f192SDenis Salopek * without returning the lock. This must be specified if 5843e87f192SDenis Salopek * the elements contain a spinlock. 5853e87f192SDenis Salopek * 586f67c9cbfSJoe Stringer * The **BPF_MAP_TYPE_QUEUE** and **BPF_MAP_TYPE_STACK** map types 587f67c9cbfSJoe Stringer * implement this command as a "pop" operation, deleting the top 588f67c9cbfSJoe Stringer * element rather than one corresponding to *key*. 589f67c9cbfSJoe Stringer * The *key* and *key_len* parameters should be zeroed when 590f67c9cbfSJoe Stringer * issuing this operation for these map types. 591f67c9cbfSJoe Stringer * 592f67c9cbfSJoe Stringer * This command is only valid for the following map types: 593f67c9cbfSJoe Stringer * * **BPF_MAP_TYPE_QUEUE** 594f67c9cbfSJoe Stringer * * **BPF_MAP_TYPE_STACK** 5953e87f192SDenis Salopek * * **BPF_MAP_TYPE_HASH** 5963e87f192SDenis Salopek * * **BPF_MAP_TYPE_PERCPU_HASH** 5973e87f192SDenis Salopek * * **BPF_MAP_TYPE_LRU_HASH** 5983e87f192SDenis Salopek * * **BPF_MAP_TYPE_LRU_PERCPU_HASH** 599f67c9cbfSJoe Stringer * 600f67c9cbfSJoe Stringer * Return 601f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 602f67c9cbfSJoe Stringer * is set appropriately. 603f67c9cbfSJoe Stringer * 604f67c9cbfSJoe Stringer * BPF_MAP_FREEZE 605f67c9cbfSJoe Stringer * Description 606f67c9cbfSJoe Stringer * Freeze the permissions of the specified map. 607f67c9cbfSJoe Stringer * 608f67c9cbfSJoe Stringer * Write permissions may be frozen by passing zero *flags*. 609f67c9cbfSJoe Stringer * Upon success, no future syscall invocations may alter the 610f67c9cbfSJoe Stringer * map state of *map_fd*. Write operations from eBPF programs 611f67c9cbfSJoe Stringer * are still possible for a frozen map. 612f67c9cbfSJoe Stringer * 613f67c9cbfSJoe Stringer * Not supported for maps of type **BPF_MAP_TYPE_STRUCT_OPS**. 614f67c9cbfSJoe Stringer * 615f67c9cbfSJoe Stringer * Return 616f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 617f67c9cbfSJoe Stringer * is set appropriately. 618f67c9cbfSJoe Stringer * 619f67c9cbfSJoe Stringer * BPF_BTF_GET_NEXT_ID 620f67c9cbfSJoe Stringer * Description 621f67c9cbfSJoe Stringer * Fetch the next BPF Type Format (BTF) object currently loaded 622f67c9cbfSJoe Stringer * into the kernel. 623f67c9cbfSJoe Stringer * 624f67c9cbfSJoe Stringer * Looks for the BTF object with an id greater than *start_id* 625f67c9cbfSJoe Stringer * and updates *next_id* on success. If no other BTF objects 626f67c9cbfSJoe Stringer * remain with ids higher than *start_id*, returns -1 and sets 627f67c9cbfSJoe Stringer * *errno* to **ENOENT**. 628f67c9cbfSJoe Stringer * 629f67c9cbfSJoe Stringer * Return 630f67c9cbfSJoe Stringer * Returns zero on success. On error, or when no id remains, -1 631f67c9cbfSJoe Stringer * is returned and *errno* is set appropriately. 632f67c9cbfSJoe Stringer * 633f67c9cbfSJoe Stringer * BPF_MAP_LOOKUP_BATCH 634f67c9cbfSJoe Stringer * Description 635f67c9cbfSJoe Stringer * Iterate and fetch multiple elements in a map. 636f67c9cbfSJoe Stringer * 6370cb80454SJoe Stringer * Two opaque values are used to manage batch operations, 6380cb80454SJoe Stringer * *in_batch* and *out_batch*. Initially, *in_batch* must be set 6390cb80454SJoe Stringer * to NULL to begin the batched operation. After each subsequent 6400cb80454SJoe Stringer * **BPF_MAP_LOOKUP_BATCH**, the caller should pass the resultant 6410cb80454SJoe Stringer * *out_batch* as the *in_batch* for the next operation to 64258fd62e0SMartin Kelly * continue iteration from the current point. Both *in_batch* and 64358fd62e0SMartin Kelly * *out_batch* must point to memory large enough to hold a key, 64458fd62e0SMartin Kelly * except for maps of type **BPF_MAP_TYPE_{HASH, PERCPU_HASH, 64558fd62e0SMartin Kelly * LRU_HASH, LRU_PERCPU_HASH}**, for which batch parameters 64658fd62e0SMartin Kelly * must be at least 4 bytes wide regardless of key size. 6470cb80454SJoe Stringer * 6480cb80454SJoe Stringer * The *keys* and *values* are output parameters which must point 6490cb80454SJoe Stringer * to memory large enough to hold *count* items based on the key 6500cb80454SJoe Stringer * and value size of the map *map_fd*. The *keys* buffer must be 6510cb80454SJoe Stringer * of *key_size* * *count*. The *values* buffer must be of 6520cb80454SJoe Stringer * *value_size* * *count*. 6530cb80454SJoe Stringer * 6540cb80454SJoe Stringer * The *elem_flags* argument may be specified as one of the 6550cb80454SJoe Stringer * following: 6560cb80454SJoe Stringer * 6570cb80454SJoe Stringer * **BPF_F_LOCK** 6580cb80454SJoe Stringer * Look up the value of a spin-locked map without 6590cb80454SJoe Stringer * returning the lock. This must be specified if the 6600cb80454SJoe Stringer * elements contain a spinlock. 6610cb80454SJoe Stringer * 6620cb80454SJoe Stringer * On success, *count* elements from the map are copied into the 6630cb80454SJoe Stringer * user buffer, with the keys copied into *keys* and the values 6640cb80454SJoe Stringer * copied into the corresponding indices in *values*. 6650cb80454SJoe Stringer * 6660cb80454SJoe Stringer * If an error is returned and *errno* is not **EFAULT**, *count* 6670cb80454SJoe Stringer * is set to the number of successfully processed elements. 6680cb80454SJoe Stringer * 669f67c9cbfSJoe Stringer * Return 670f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 671f67c9cbfSJoe Stringer * is set appropriately. 672f67c9cbfSJoe Stringer * 6730cb80454SJoe Stringer * May set *errno* to **ENOSPC** to indicate that *keys* or 6740cb80454SJoe Stringer * *values* is too small to dump an entire bucket during 6750cb80454SJoe Stringer * iteration of a hash-based map type. 6760cb80454SJoe Stringer * 677f67c9cbfSJoe Stringer * BPF_MAP_LOOKUP_AND_DELETE_BATCH 678f67c9cbfSJoe Stringer * Description 6790cb80454SJoe Stringer * Iterate and delete all elements in a map. 6800cb80454SJoe Stringer * 6810cb80454SJoe Stringer * This operation has the same behavior as 6820cb80454SJoe Stringer * **BPF_MAP_LOOKUP_BATCH** with two exceptions: 6830cb80454SJoe Stringer * 6840cb80454SJoe Stringer * * Every element that is successfully returned is also deleted 6850cb80454SJoe Stringer * from the map. This is at least *count* elements. Note that 6860cb80454SJoe Stringer * *count* is both an input and an output parameter. 6870cb80454SJoe Stringer * * Upon returning with *errno* set to **EFAULT**, up to 6880cb80454SJoe Stringer * *count* elements may be deleted without returning the keys 6890cb80454SJoe Stringer * and values of the deleted elements. 690f67c9cbfSJoe Stringer * 691f67c9cbfSJoe Stringer * Return 692f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 693f67c9cbfSJoe Stringer * is set appropriately. 694f67c9cbfSJoe Stringer * 695f67c9cbfSJoe Stringer * BPF_MAP_UPDATE_BATCH 696f67c9cbfSJoe Stringer * Description 6970cb80454SJoe Stringer * Update multiple elements in a map by *key*. 6980cb80454SJoe Stringer * 6990cb80454SJoe Stringer * The *keys* and *values* are input parameters which must point 7000cb80454SJoe Stringer * to memory large enough to hold *count* items based on the key 7010cb80454SJoe Stringer * and value size of the map *map_fd*. The *keys* buffer must be 7020cb80454SJoe Stringer * of *key_size* * *count*. The *values* buffer must be of 7030cb80454SJoe Stringer * *value_size* * *count*. 7040cb80454SJoe Stringer * 7050cb80454SJoe Stringer * Each element specified in *keys* is sequentially updated to the 7060cb80454SJoe Stringer * value in the corresponding index in *values*. The *in_batch* 7070cb80454SJoe Stringer * and *out_batch* parameters are ignored and should be zeroed. 7080cb80454SJoe Stringer * 7090cb80454SJoe Stringer * The *elem_flags* argument should be specified as one of the 7100cb80454SJoe Stringer * following: 7110cb80454SJoe Stringer * 7120cb80454SJoe Stringer * **BPF_ANY** 7130cb80454SJoe Stringer * Create new elements or update a existing elements. 7140cb80454SJoe Stringer * **BPF_NOEXIST** 7150cb80454SJoe Stringer * Create new elements only if they do not exist. 7160cb80454SJoe Stringer * **BPF_EXIST** 7170cb80454SJoe Stringer * Update existing elements. 7180cb80454SJoe Stringer * **BPF_F_LOCK** 7190cb80454SJoe Stringer * Update spin_lock-ed map elements. This must be 7200cb80454SJoe Stringer * specified if the map value contains a spinlock. 7210cb80454SJoe Stringer * 7220cb80454SJoe Stringer * On success, *count* elements from the map are updated. 7230cb80454SJoe Stringer * 7240cb80454SJoe Stringer * If an error is returned and *errno* is not **EFAULT**, *count* 7250cb80454SJoe Stringer * is set to the number of successfully processed elements. 726f67c9cbfSJoe Stringer * 727f67c9cbfSJoe Stringer * Return 728f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 729f67c9cbfSJoe Stringer * is set appropriately. 730f67c9cbfSJoe Stringer * 7310cb80454SJoe Stringer * May set *errno* to **EINVAL**, **EPERM**, **ENOMEM**, or 7320cb80454SJoe Stringer * **E2BIG**. **E2BIG** indicates that the number of elements in 7330cb80454SJoe Stringer * the map reached the *max_entries* limit specified at map 7340cb80454SJoe Stringer * creation time. 7350cb80454SJoe Stringer * 7360cb80454SJoe Stringer * May set *errno* to one of the following error codes under 7370cb80454SJoe Stringer * specific circumstances: 7380cb80454SJoe Stringer * 7390cb80454SJoe Stringer * **EEXIST** 7400cb80454SJoe Stringer * If *flags* specifies **BPF_NOEXIST** and the element 7410cb80454SJoe Stringer * with *key* already exists in the map. 7420cb80454SJoe Stringer * **ENOENT** 7430cb80454SJoe Stringer * If *flags* specifies **BPF_EXIST** and the element with 7440cb80454SJoe Stringer * *key* does not exist in the map. 7450cb80454SJoe Stringer * 746f67c9cbfSJoe Stringer * BPF_MAP_DELETE_BATCH 747f67c9cbfSJoe Stringer * Description 7480cb80454SJoe Stringer * Delete multiple elements in a map by *key*. 7490cb80454SJoe Stringer * 7500cb80454SJoe Stringer * The *keys* parameter is an input parameter which must point 7510cb80454SJoe Stringer * to memory large enough to hold *count* items based on the key 7520cb80454SJoe Stringer * size of the map *map_fd*, that is, *key_size* * *count*. 7530cb80454SJoe Stringer * 7540cb80454SJoe Stringer * Each element specified in *keys* is sequentially deleted. The 7550cb80454SJoe Stringer * *in_batch*, *out_batch*, and *values* parameters are ignored 7560cb80454SJoe Stringer * and should be zeroed. 7570cb80454SJoe Stringer * 7580cb80454SJoe Stringer * The *elem_flags* argument may be specified as one of the 7590cb80454SJoe Stringer * following: 7600cb80454SJoe Stringer * 7610cb80454SJoe Stringer * **BPF_F_LOCK** 7620cb80454SJoe Stringer * Look up the value of a spin-locked map without 7630cb80454SJoe Stringer * returning the lock. This must be specified if the 7640cb80454SJoe Stringer * elements contain a spinlock. 7650cb80454SJoe Stringer * 7660cb80454SJoe Stringer * On success, *count* elements from the map are updated. 7670cb80454SJoe Stringer * 7680cb80454SJoe Stringer * If an error is returned and *errno* is not **EFAULT**, *count* 7690cb80454SJoe Stringer * is set to the number of successfully processed elements. If 7700cb80454SJoe Stringer * *errno* is **EFAULT**, up to *count* elements may be been 7710cb80454SJoe Stringer * deleted. 772f67c9cbfSJoe Stringer * 773f67c9cbfSJoe Stringer * Return 774f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 775f67c9cbfSJoe Stringer * is set appropriately. 776f67c9cbfSJoe Stringer * 777f67c9cbfSJoe Stringer * BPF_LINK_CREATE 778f67c9cbfSJoe Stringer * Description 779f67c9cbfSJoe Stringer * Attach an eBPF program to a *target_fd* at the specified 780f67c9cbfSJoe Stringer * *attach_type* hook and return a file descriptor handle for 781f67c9cbfSJoe Stringer * managing the link. 782f67c9cbfSJoe Stringer * 783f67c9cbfSJoe Stringer * Return 784f67c9cbfSJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 785f67c9cbfSJoe Stringer * error occurred (in which case, *errno* is set appropriately). 786f67c9cbfSJoe Stringer * 787f67c9cbfSJoe Stringer * BPF_LINK_UPDATE 788f67c9cbfSJoe Stringer * Description 789f67c9cbfSJoe Stringer * Update the eBPF program in the specified *link_fd* to 790f67c9cbfSJoe Stringer * *new_prog_fd*. 791f67c9cbfSJoe Stringer * 792f67c9cbfSJoe Stringer * Return 793f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 794f67c9cbfSJoe Stringer * is set appropriately. 795f67c9cbfSJoe Stringer * 796f67c9cbfSJoe Stringer * BPF_LINK_GET_FD_BY_ID 797f67c9cbfSJoe Stringer * Description 798f67c9cbfSJoe Stringer * Open a file descriptor for the eBPF Link corresponding to 799f67c9cbfSJoe Stringer * *link_id*. 800f67c9cbfSJoe Stringer * 801f67c9cbfSJoe Stringer * Return 802f67c9cbfSJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 803f67c9cbfSJoe Stringer * error occurred (in which case, *errno* is set appropriately). 804f67c9cbfSJoe Stringer * 805f67c9cbfSJoe Stringer * BPF_LINK_GET_NEXT_ID 806f67c9cbfSJoe Stringer * Description 807f67c9cbfSJoe Stringer * Fetch the next eBPF link currently loaded into the kernel. 808f67c9cbfSJoe Stringer * 809f67c9cbfSJoe Stringer * Looks for the eBPF link with an id greater than *start_id* 810f67c9cbfSJoe Stringer * and updates *next_id* on success. If no other eBPF links 811f67c9cbfSJoe Stringer * remain with ids higher than *start_id*, returns -1 and sets 812f67c9cbfSJoe Stringer * *errno* to **ENOENT**. 813f67c9cbfSJoe Stringer * 814f67c9cbfSJoe Stringer * Return 815f67c9cbfSJoe Stringer * Returns zero on success. On error, or when no id remains, -1 816f67c9cbfSJoe Stringer * is returned and *errno* is set appropriately. 817f67c9cbfSJoe Stringer * 818f67c9cbfSJoe Stringer * BPF_ENABLE_STATS 819f67c9cbfSJoe Stringer * Description 820f67c9cbfSJoe Stringer * Enable eBPF runtime statistics gathering. 821f67c9cbfSJoe Stringer * 822f67c9cbfSJoe Stringer * Runtime statistics gathering for the eBPF runtime is disabled 823f67c9cbfSJoe Stringer * by default to minimize the corresponding performance overhead. 824f67c9cbfSJoe Stringer * This command enables statistics globally. 825f67c9cbfSJoe Stringer * 826f67c9cbfSJoe Stringer * Multiple programs may independently enable statistics. 827f67c9cbfSJoe Stringer * After gathering the desired statistics, eBPF runtime statistics 828f67c9cbfSJoe Stringer * may be disabled again by calling **close**\ (2) for the file 829f67c9cbfSJoe Stringer * descriptor returned by this function. Statistics will only be 830f67c9cbfSJoe Stringer * disabled system-wide when all outstanding file descriptors 831f67c9cbfSJoe Stringer * returned by prior calls for this subcommand are closed. 832f67c9cbfSJoe Stringer * 833f67c9cbfSJoe Stringer * Return 834f67c9cbfSJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 835f67c9cbfSJoe Stringer * error occurred (in which case, *errno* is set appropriately). 836f67c9cbfSJoe Stringer * 837f67c9cbfSJoe Stringer * BPF_ITER_CREATE 838f67c9cbfSJoe Stringer * Description 839f67c9cbfSJoe Stringer * Create an iterator on top of the specified *link_fd* (as 840f67c9cbfSJoe Stringer * previously created using **BPF_LINK_CREATE**) and return a 841f67c9cbfSJoe Stringer * file descriptor that can be used to trigger the iteration. 842f67c9cbfSJoe Stringer * 843f67c9cbfSJoe Stringer * If the resulting file descriptor is pinned to the filesystem 844f67c9cbfSJoe Stringer * using **BPF_OBJ_PIN**, then subsequent **read**\ (2) syscalls 845f67c9cbfSJoe Stringer * for that path will trigger the iterator to read kernel state 846f67c9cbfSJoe Stringer * using the eBPF program attached to *link_fd*. 847f67c9cbfSJoe Stringer * 848f67c9cbfSJoe Stringer * Return 849f67c9cbfSJoe Stringer * A new file descriptor (a nonnegative integer), or -1 if an 850f67c9cbfSJoe Stringer * error occurred (in which case, *errno* is set appropriately). 851f67c9cbfSJoe Stringer * 852f67c9cbfSJoe Stringer * BPF_LINK_DETACH 853f67c9cbfSJoe Stringer * Description 854f67c9cbfSJoe Stringer * Forcefully detach the specified *link_fd* from its 855f67c9cbfSJoe Stringer * corresponding attachment point. 856f67c9cbfSJoe Stringer * 857f67c9cbfSJoe Stringer * Return 858f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 859f67c9cbfSJoe Stringer * is set appropriately. 860f67c9cbfSJoe Stringer * 861f67c9cbfSJoe Stringer * BPF_PROG_BIND_MAP 862f67c9cbfSJoe Stringer * Description 863f67c9cbfSJoe Stringer * Bind a map to the lifetime of an eBPF program. 864f67c9cbfSJoe Stringer * 865f67c9cbfSJoe Stringer * The map identified by *map_fd* is bound to the program 866f67c9cbfSJoe Stringer * identified by *prog_fd* and only released when *prog_fd* is 867f67c9cbfSJoe Stringer * released. This may be used in cases where metadata should be 868f67c9cbfSJoe Stringer * associated with a program which otherwise does not contain any 869f67c9cbfSJoe Stringer * references to the map (for example, embedded in the eBPF 870f67c9cbfSJoe Stringer * program instructions). 871f67c9cbfSJoe Stringer * 872f67c9cbfSJoe Stringer * Return 873f67c9cbfSJoe Stringer * Returns zero on success. On error, -1 is returned and *errno* 874f67c9cbfSJoe Stringer * is set appropriately. 875f67c9cbfSJoe Stringer * 87635f96de0SAndrii Nakryiko * BPF_TOKEN_CREATE 87735f96de0SAndrii Nakryiko * Description 87835f96de0SAndrii Nakryiko * Create BPF token with embedded information about what 87935f96de0SAndrii Nakryiko * BPF-related functionality it allows: 88035f96de0SAndrii Nakryiko * - a set of allowed bpf() syscall commands; 88135f96de0SAndrii Nakryiko * - a set of allowed BPF map types to be created with 88235f96de0SAndrii Nakryiko * BPF_MAP_CREATE command, if BPF_MAP_CREATE itself is allowed; 88335f96de0SAndrii Nakryiko * - a set of allowed BPF program types and BPF program attach 88435f96de0SAndrii Nakryiko * types to be loaded with BPF_PROG_LOAD command, if 88535f96de0SAndrii Nakryiko * BPF_PROG_LOAD itself is allowed. 88635f96de0SAndrii Nakryiko * 88735f96de0SAndrii Nakryiko * BPF token is created (derived) from an instance of BPF FS, 88835f96de0SAndrii Nakryiko * assuming it has necessary delegation mount options specified. 88935f96de0SAndrii Nakryiko * This BPF token can be passed as an extra parameter to various 89035f96de0SAndrii Nakryiko * bpf() syscall commands to grant BPF subsystem functionality to 89135f96de0SAndrii Nakryiko * unprivileged processes. 89235f96de0SAndrii Nakryiko * 89335f96de0SAndrii Nakryiko * When created, BPF token is "associated" with the owning 89435f96de0SAndrii Nakryiko * user namespace of BPF FS instance (super block) that it was 89535f96de0SAndrii Nakryiko * derived from, and subsequent BPF operations performed with 89635f96de0SAndrii Nakryiko * BPF token would be performing capabilities checks (i.e., 89735f96de0SAndrii Nakryiko * CAP_BPF, CAP_PERFMON, CAP_NET_ADMIN, CAP_SYS_ADMIN) within 89835f96de0SAndrii Nakryiko * that user namespace. Without BPF token, such capabilities 89935f96de0SAndrii Nakryiko * have to be granted in init user namespace, making bpf() 90035f96de0SAndrii Nakryiko * syscall incompatible with user namespace, for the most part. 90135f96de0SAndrii Nakryiko * 90235f96de0SAndrii Nakryiko * Return 90335f96de0SAndrii Nakryiko * A new file descriptor (a nonnegative integer), or -1 if an 90435f96de0SAndrii Nakryiko * error occurred (in which case, *errno* is set appropriately). 90535f96de0SAndrii Nakryiko * 9067799e4d9SJoe Stringer * NOTES 9077799e4d9SJoe Stringer * eBPF objects (maps and programs) can be shared between processes. 9088aacb3c8SJoe Stringer * 9098aacb3c8SJoe Stringer * * After **fork**\ (2), the child inherits file descriptors 9108aacb3c8SJoe Stringer * referring to the same eBPF objects. 9118aacb3c8SJoe Stringer * * File descriptors referring to eBPF objects can be transferred over 9128aacb3c8SJoe Stringer * **unix**\ (7) domain sockets. 9138aacb3c8SJoe Stringer * * File descriptors referring to eBPF objects can be duplicated in the 9148aacb3c8SJoe Stringer * usual way, using **dup**\ (2) and similar calls. 9158aacb3c8SJoe Stringer * * File descriptors referring to eBPF objects can be pinned to the 9168aacb3c8SJoe Stringer * filesystem using the **BPF_OBJ_PIN** command of **bpf**\ (2). 9178aacb3c8SJoe Stringer * 9188aacb3c8SJoe Stringer * An eBPF object is deallocated only after all file descriptors referring 9198aacb3c8SJoe Stringer * to the object have been closed and no references remain pinned to the 9208aacb3c8SJoe Stringer * filesystem or attached (for example, bound to a program or device). 9217799e4d9SJoe Stringer */ 92299c55f7dSAlexei Starovoitov enum bpf_cmd { 92399c55f7dSAlexei Starovoitov BPF_MAP_CREATE, 924db20fd2bSAlexei Starovoitov BPF_MAP_LOOKUP_ELEM, 925db20fd2bSAlexei Starovoitov BPF_MAP_UPDATE_ELEM, 926db20fd2bSAlexei Starovoitov BPF_MAP_DELETE_ELEM, 927db20fd2bSAlexei Starovoitov BPF_MAP_GET_NEXT_KEY, 92809756af4SAlexei Starovoitov BPF_PROG_LOAD, 929b2197755SDaniel Borkmann BPF_OBJ_PIN, 930b2197755SDaniel Borkmann BPF_OBJ_GET, 931f4324551SDaniel Mack BPF_PROG_ATTACH, 932f4324551SDaniel Mack BPF_PROG_DETACH, 9331cf1cae9SAlexei Starovoitov BPF_PROG_TEST_RUN, 9345d67f349SAlexei Starovoitov BPF_PROG_RUN = BPF_PROG_TEST_RUN, 93534ad5580SMartin KaFai Lau BPF_PROG_GET_NEXT_ID, 93634ad5580SMartin KaFai Lau BPF_MAP_GET_NEXT_ID, 937b16d9aa4SMartin KaFai Lau BPF_PROG_GET_FD_BY_ID, 938bd5f5f4eSMartin KaFai Lau BPF_MAP_GET_FD_BY_ID, 9391e270976SMartin KaFai Lau BPF_OBJ_GET_INFO_BY_FD, 940468e2f64SAlexei Starovoitov BPF_PROG_QUERY, 941c4f6699dSAlexei Starovoitov BPF_RAW_TRACEPOINT_OPEN, 942f56a653cSMartin KaFai Lau BPF_BTF_LOAD, 94378958fcaSMartin KaFai Lau BPF_BTF_GET_FD_BY_ID, 94441bdc4b4SYonghong Song BPF_TASK_FD_QUERY, 945bd513cd0SMauricio Vasquez B BPF_MAP_LOOKUP_AND_DELETE_ELEM, 94687df15deSDaniel Borkmann BPF_MAP_FREEZE, 9471b9ed84eSQuentin Monnet BPF_BTF_GET_NEXT_ID, 948cb4d03abSBrian Vazquez BPF_MAP_LOOKUP_BATCH, 94905799638SYonghong Song BPF_MAP_LOOKUP_AND_DELETE_BATCH, 950aa2e93b8SBrian Vazquez BPF_MAP_UPDATE_BATCH, 951aa2e93b8SBrian Vazquez BPF_MAP_DELETE_BATCH, 952af6eea57SAndrii Nakryiko BPF_LINK_CREATE, 9530c991ebcSAndrii Nakryiko BPF_LINK_UPDATE, 9542d602c8cSAndrii Nakryiko BPF_LINK_GET_FD_BY_ID, 9552d602c8cSAndrii Nakryiko BPF_LINK_GET_NEXT_ID, 956d46edd67SSong Liu BPF_ENABLE_STATS, 957ac51d99bSYonghong Song BPF_ITER_CREATE, 95873b11c2aSAndrii Nakryiko BPF_LINK_DETACH, 959ef15314aSYiFei Zhu BPF_PROG_BIND_MAP, 96035f96de0SAndrii Nakryiko BPF_TOKEN_CREATE, 96135f96de0SAndrii Nakryiko __MAX_BPF_CMD, 96299c55f7dSAlexei Starovoitov }; 96399c55f7dSAlexei Starovoitov 96499c55f7dSAlexei Starovoitov enum bpf_map_type { 96599c55f7dSAlexei Starovoitov BPF_MAP_TYPE_UNSPEC, 9660f8e4bd8SAlexei Starovoitov BPF_MAP_TYPE_HASH, 96728fbcfa0SAlexei Starovoitov BPF_MAP_TYPE_ARRAY, 96804fd61abSAlexei Starovoitov BPF_MAP_TYPE_PROG_ARRAY, 969ea317b26SKaixu Xia BPF_MAP_TYPE_PERF_EVENT_ARRAY, 970824bd0ceSAlexei Starovoitov BPF_MAP_TYPE_PERCPU_HASH, 971a10423b8SAlexei Starovoitov BPF_MAP_TYPE_PERCPU_ARRAY, 972d5a3b1f6SAlexei Starovoitov BPF_MAP_TYPE_STACK_TRACE, 9734ed8ec52SMartin KaFai Lau BPF_MAP_TYPE_CGROUP_ARRAY, 97429ba732aSMartin KaFai Lau BPF_MAP_TYPE_LRU_HASH, 9758f844938SMartin KaFai Lau BPF_MAP_TYPE_LRU_PERCPU_HASH, 976b95a5c4dSDaniel Mack BPF_MAP_TYPE_LPM_TRIE, 97756f668dfSMartin KaFai Lau BPF_MAP_TYPE_ARRAY_OF_MAPS, 978bcc6b1b7SMartin KaFai Lau BPF_MAP_TYPE_HASH_OF_MAPS, 979546ac1ffSJohn Fastabend BPF_MAP_TYPE_DEVMAP, 980174a79ffSJohn Fastabend BPF_MAP_TYPE_SOCKMAP, 9816710e112SJesper Dangaard Brouer BPF_MAP_TYPE_CPUMAP, 982fbfc504aSBjörn Töpel BPF_MAP_TYPE_XSKMAP, 98381110384SJohn Fastabend BPF_MAP_TYPE_SOCKHASH, 984c4bcfb38SYonghong Song BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED, 985c4bcfb38SYonghong Song /* BPF_MAP_TYPE_CGROUP_STORAGE is available to bpf programs attaching 986c4bcfb38SYonghong Song * to a cgroup. The newer BPF_MAP_TYPE_CGRP_STORAGE is available to 987c4bcfb38SYonghong Song * both cgroup-attached and other progs and supports all functionality 988c4bcfb38SYonghong Song * provided by BPF_MAP_TYPE_CGROUP_STORAGE. So mark 989c4bcfb38SYonghong Song * BPF_MAP_TYPE_CGROUP_STORAGE deprecated. 990c4bcfb38SYonghong Song */ 991c4bcfb38SYonghong Song BPF_MAP_TYPE_CGROUP_STORAGE = BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED, 9925dc4c4b7SMartin KaFai Lau BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, 9939bc95a95SYonghong Song BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE_DEPRECATED, 9949bc95a95SYonghong Song /* BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE is available to bpf programs 9959bc95a95SYonghong Song * attaching to a cgroup. The new mechanism (BPF_MAP_TYPE_CGRP_STORAGE + 9969bc95a95SYonghong Song * local percpu kptr) supports all BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE 9979bc95a95SYonghong Song * functionality and more. So mark * BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE 9989bc95a95SYonghong Song * deprecated. 9999bc95a95SYonghong Song */ 10009bc95a95SYonghong Song BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE = BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE_DEPRECATED, 1001f1a2e44aSMauricio Vasquez B BPF_MAP_TYPE_QUEUE, 1002f1a2e44aSMauricio Vasquez B BPF_MAP_TYPE_STACK, 10036ac99e8fSMartin KaFai Lau BPF_MAP_TYPE_SK_STORAGE, 10046f9d451aSToke Høiland-Jørgensen BPF_MAP_TYPE_DEVMAP_HASH, 100585d33df3SMartin KaFai Lau BPF_MAP_TYPE_STRUCT_OPS, 1006457f4436SAndrii Nakryiko BPF_MAP_TYPE_RINGBUF, 10078ea63684SKP Singh BPF_MAP_TYPE_INODE_STORAGE, 10084cf1bc1fSKP Singh BPF_MAP_TYPE_TASK_STORAGE, 10099330986cSJoanne Koong BPF_MAP_TYPE_BLOOM_FILTER, 1010583c1f42SDavid Vernet BPF_MAP_TYPE_USER_RINGBUF, 1011c4bcfb38SYonghong Song BPF_MAP_TYPE_CGRP_STORAGE, 101231746031SAlexei Starovoitov BPF_MAP_TYPE_ARENA, 1013a177fc2bSAndrii Nakryiko __MAX_BPF_MAP_TYPE 101499c55f7dSAlexei Starovoitov }; 101599c55f7dSAlexei Starovoitov 10166c4fc209SDaniel Borkmann /* Note that tracing related programs such as 10176c4fc209SDaniel Borkmann * BPF_PROG_TYPE_{KPROBE,TRACEPOINT,PERF_EVENT,RAW_TRACEPOINT} 10186c4fc209SDaniel Borkmann * are not subject to a stable API since kernel internal data 10196c4fc209SDaniel Borkmann * structures can change from release to release and may 10206c4fc209SDaniel Borkmann * therefore break existing tracing BPF programs. Tracing BPF 10216c4fc209SDaniel Borkmann * programs correspond to /a/ specific kernel which is to be 10226c4fc209SDaniel Borkmann * analyzed, and not /a/ specific kernel /and/ all future ones. 10236c4fc209SDaniel Borkmann */ 102409756af4SAlexei Starovoitov enum bpf_prog_type { 102509756af4SAlexei Starovoitov BPF_PROG_TYPE_UNSPEC, 1026ddd872bcSAlexei Starovoitov BPF_PROG_TYPE_SOCKET_FILTER, 10272541517cSAlexei Starovoitov BPF_PROG_TYPE_KPROBE, 102896be4325SDaniel Borkmann BPF_PROG_TYPE_SCHED_CLS, 102994caee8cSDaniel Borkmann BPF_PROG_TYPE_SCHED_ACT, 103098b5c2c6SAlexei Starovoitov BPF_PROG_TYPE_TRACEPOINT, 10316a773a15SBrenden Blanco BPF_PROG_TYPE_XDP, 10320515e599SAlexei Starovoitov BPF_PROG_TYPE_PERF_EVENT, 10330e33661dSDaniel Mack BPF_PROG_TYPE_CGROUP_SKB, 103461023658SDavid Ahern BPF_PROG_TYPE_CGROUP_SOCK, 10353a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_IN, 10363a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_OUT, 10373a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_XMIT, 103840304b2aSLawrence Brakmo BPF_PROG_TYPE_SOCK_OPS, 1039b005fd18SJohn Fastabend BPF_PROG_TYPE_SK_SKB, 1040ebc614f6SRoman Gushchin BPF_PROG_TYPE_CGROUP_DEVICE, 10414f738adbSJohn Fastabend BPF_PROG_TYPE_SK_MSG, 1042c4f6699dSAlexei Starovoitov BPF_PROG_TYPE_RAW_TRACEPOINT, 10434fbac77dSAndrey Ignatov BPF_PROG_TYPE_CGROUP_SOCK_ADDR, 1044004d4b27SMathieu Xhonneux BPF_PROG_TYPE_LWT_SEG6LOCAL, 1045f4364dcfSSean Young BPF_PROG_TYPE_LIRC_MODE2, 10462dbb9b9eSMartin KaFai Lau BPF_PROG_TYPE_SK_REUSEPORT, 1047d58e468bSPetar Penkov BPF_PROG_TYPE_FLOW_DISSECTOR, 10487b146cebSAndrey Ignatov BPF_PROG_TYPE_CGROUP_SYSCTL, 10499df1c28bSMatt Mullins BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, 10500d01da6aSStanislav Fomichev BPF_PROG_TYPE_CGROUP_SOCKOPT, 1051f1b9509cSAlexei Starovoitov BPF_PROG_TYPE_TRACING, 105227ae7997SMartin KaFai Lau BPF_PROG_TYPE_STRUCT_OPS, 1053be8704ffSAlexei Starovoitov BPF_PROG_TYPE_EXT, 1054fc611f47SKP Singh BPF_PROG_TYPE_LSM, 1055e9ddbb77SJakub Sitnicki BPF_PROG_TYPE_SK_LOOKUP, 105679a7f8bdSAlexei Starovoitov BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */ 105784601d6eSFlorian Westphal BPF_PROG_TYPE_NETFILTER, 1058caf8f28eSAndrii Nakryiko __MAX_BPF_PROG_TYPE 105909756af4SAlexei Starovoitov }; 106009756af4SAlexei Starovoitov 10610e33661dSDaniel Mack enum bpf_attach_type { 10620e33661dSDaniel Mack BPF_CGROUP_INET_INGRESS, 10630e33661dSDaniel Mack BPF_CGROUP_INET_EGRESS, 106461023658SDavid Ahern BPF_CGROUP_INET_SOCK_CREATE, 106540304b2aSLawrence Brakmo BPF_CGROUP_SOCK_OPS, 1066464bc0fdSJohn Fastabend BPF_SK_SKB_STREAM_PARSER, 1067464bc0fdSJohn Fastabend BPF_SK_SKB_STREAM_VERDICT, 1068ebc614f6SRoman Gushchin BPF_CGROUP_DEVICE, 10694f738adbSJohn Fastabend BPF_SK_MSG_VERDICT, 10704fbac77dSAndrey Ignatov BPF_CGROUP_INET4_BIND, 10714fbac77dSAndrey Ignatov BPF_CGROUP_INET6_BIND, 1072d74bad4eSAndrey Ignatov BPF_CGROUP_INET4_CONNECT, 1073d74bad4eSAndrey Ignatov BPF_CGROUP_INET6_CONNECT, 1074aac3fc32SAndrey Ignatov BPF_CGROUP_INET4_POST_BIND, 1075aac3fc32SAndrey Ignatov BPF_CGROUP_INET6_POST_BIND, 10761cedee13SAndrey Ignatov BPF_CGROUP_UDP4_SENDMSG, 10771cedee13SAndrey Ignatov BPF_CGROUP_UDP6_SENDMSG, 1078f4364dcfSSean Young BPF_LIRC_MODE2, 1079d58e468bSPetar Penkov BPF_FLOW_DISSECTOR, 10807b146cebSAndrey Ignatov BPF_CGROUP_SYSCTL, 1081983695faSDaniel Borkmann BPF_CGROUP_UDP4_RECVMSG, 1082983695faSDaniel Borkmann BPF_CGROUP_UDP6_RECVMSG, 10830d01da6aSStanislav Fomichev BPF_CGROUP_GETSOCKOPT, 10840d01da6aSStanislav Fomichev BPF_CGROUP_SETSOCKOPT, 1085f1b9509cSAlexei Starovoitov BPF_TRACE_RAW_TP, 1086fec56f58SAlexei Starovoitov BPF_TRACE_FENTRY, 1087fec56f58SAlexei Starovoitov BPF_TRACE_FEXIT, 1088ae240823SKP Singh BPF_MODIFY_RETURN, 1089fc611f47SKP Singh BPF_LSM_MAC, 109015d83c4dSYonghong Song BPF_TRACE_ITER, 10911b66d253SDaniel Borkmann BPF_CGROUP_INET4_GETPEERNAME, 10921b66d253SDaniel Borkmann BPF_CGROUP_INET6_GETPEERNAME, 10931b66d253SDaniel Borkmann BPF_CGROUP_INET4_GETSOCKNAME, 10941b66d253SDaniel Borkmann BPF_CGROUP_INET6_GETSOCKNAME, 1095fbee97feSDavid Ahern BPF_XDP_DEVMAP, 1096f5836749SStanislav Fomichev BPF_CGROUP_INET_SOCK_RELEASE, 109792164774SLorenzo Bianconi BPF_XDP_CPUMAP, 1098e9ddbb77SJakub Sitnicki BPF_SK_LOOKUP, 1099aa8d3a71SAndrii Nakryiko BPF_XDP, 1100a7ba4558SCong Wang BPF_SK_SKB_VERDICT, 1101d5e4ddaeSKuniyuki Iwashima BPF_SK_REUSEPORT_SELECT, 1102d5e4ddaeSKuniyuki Iwashima BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, 1103b89fbfbbSAndrii Nakryiko BPF_PERF_EVENT, 11040dcac272SJiri Olsa BPF_TRACE_KPROBE_MULTI, 110569fd337aSStanislav Fomichev BPF_LSM_CGROUP, 110668b04864SKui-Feng Lee BPF_STRUCT_OPS, 1107132328e8SFlorian Westphal BPF_NETFILTER, 1108e420bed0SDaniel Borkmann BPF_TCX_INGRESS, 1109e420bed0SDaniel Borkmann BPF_TCX_EGRESS, 111089ae89f5SJiri Olsa BPF_TRACE_UPROBE_MULTI, 1111859051ddSDaan De Meyer BPF_CGROUP_UNIX_CONNECT, 1112859051ddSDaan De Meyer BPF_CGROUP_UNIX_SENDMSG, 1113859051ddSDaan De Meyer BPF_CGROUP_UNIX_RECVMSG, 1114859051ddSDaan De Meyer BPF_CGROUP_UNIX_GETPEERNAME, 1115859051ddSDaan De Meyer BPF_CGROUP_UNIX_GETSOCKNAME, 111635dfaad7SDaniel Borkmann BPF_NETKIT_PRIMARY, 111735dfaad7SDaniel Borkmann BPF_NETKIT_PEER, 11180e33661dSDaniel Mack __MAX_BPF_ATTACH_TYPE 11190e33661dSDaniel Mack }; 11200e33661dSDaniel Mack 11210e33661dSDaniel Mack #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE 11220e33661dSDaniel Mack 1123f2e10bffSAndrii Nakryiko enum bpf_link_type { 1124f2e10bffSAndrii Nakryiko BPF_LINK_TYPE_UNSPEC = 0, 1125f2e10bffSAndrii Nakryiko BPF_LINK_TYPE_RAW_TRACEPOINT = 1, 1126f2e10bffSAndrii Nakryiko BPF_LINK_TYPE_TRACING = 2, 1127f2e10bffSAndrii Nakryiko BPF_LINK_TYPE_CGROUP = 3, 1128de4e05caSYonghong Song BPF_LINK_TYPE_ITER = 4, 11297f045a49SJakub Sitnicki BPF_LINK_TYPE_NETNS = 5, 1130aa8d3a71SAndrii Nakryiko BPF_LINK_TYPE_XDP = 6, 1131b89fbfbbSAndrii Nakryiko BPF_LINK_TYPE_PERF_EVENT = 7, 11320dcac272SJiri Olsa BPF_LINK_TYPE_KPROBE_MULTI = 8, 1133f7e0beafSKui-Feng Lee BPF_LINK_TYPE_STRUCT_OPS = 9, 113484601d6eSFlorian Westphal BPF_LINK_TYPE_NETFILTER = 10, 1135e420bed0SDaniel Borkmann BPF_LINK_TYPE_TCX = 11, 113689ae89f5SJiri Olsa BPF_LINK_TYPE_UPROBE_MULTI = 12, 113735dfaad7SDaniel Borkmann BPF_LINK_TYPE_NETKIT = 13, 11387065eefbSAndrii Nakryiko __MAX_BPF_LINK_TYPE, 1139f2e10bffSAndrii Nakryiko }; 1140f2e10bffSAndrii Nakryiko 11417065eefbSAndrii Nakryiko #define MAX_BPF_LINK_TYPE __MAX_BPF_LINK_TYPE 11427065eefbSAndrii Nakryiko 11431b715e1bSYafang Shao enum bpf_perf_event_type { 11441b715e1bSYafang Shao BPF_PERF_EVENT_UNSPEC = 0, 11451b715e1bSYafang Shao BPF_PERF_EVENT_UPROBE = 1, 11461b715e1bSYafang Shao BPF_PERF_EVENT_URETPROBE = 2, 11471b715e1bSYafang Shao BPF_PERF_EVENT_KPROBE = 3, 11481b715e1bSYafang Shao BPF_PERF_EVENT_KRETPROBE = 4, 11491b715e1bSYafang Shao BPF_PERF_EVENT_TRACEPOINT = 5, 11501b715e1bSYafang Shao BPF_PERF_EVENT_EVENT = 6, 11511b715e1bSYafang Shao }; 11521b715e1bSYafang Shao 1153324bda9eSAlexei Starovoitov /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command 1154324bda9eSAlexei Starovoitov * 1155324bda9eSAlexei Starovoitov * NONE(default): No further bpf programs allowed in the subtree. 1156324bda9eSAlexei Starovoitov * 1157324bda9eSAlexei Starovoitov * BPF_F_ALLOW_OVERRIDE: If a sub-cgroup installs some bpf program, 1158324bda9eSAlexei Starovoitov * the program in this cgroup yields to sub-cgroup program. 1159324bda9eSAlexei Starovoitov * 1160324bda9eSAlexei Starovoitov * BPF_F_ALLOW_MULTI: If a sub-cgroup installs some bpf program, 1161324bda9eSAlexei Starovoitov * that cgroup program gets run in addition to the program in this cgroup. 1162324bda9eSAlexei Starovoitov * 1163324bda9eSAlexei Starovoitov * Only one program is allowed to be attached to a cgroup with 1164324bda9eSAlexei Starovoitov * NONE or BPF_F_ALLOW_OVERRIDE flag. 1165324bda9eSAlexei Starovoitov * Attaching another program on top of NONE or BPF_F_ALLOW_OVERRIDE will 1166324bda9eSAlexei Starovoitov * release old program and attach the new one. Attach flags has to match. 1167324bda9eSAlexei Starovoitov * 1168324bda9eSAlexei Starovoitov * Multiple programs are allowed to be attached to a cgroup with 1169324bda9eSAlexei Starovoitov * BPF_F_ALLOW_MULTI flag. They are executed in FIFO order 1170324bda9eSAlexei Starovoitov * (those that were attached first, run first) 1171324bda9eSAlexei Starovoitov * The programs of sub-cgroup are executed first, then programs of 1172324bda9eSAlexei Starovoitov * this cgroup and then programs of parent cgroup. 1173324bda9eSAlexei Starovoitov * When children program makes decision (like picking TCP CA or sock bind) 1174324bda9eSAlexei Starovoitov * parent program has a chance to override it. 1175324bda9eSAlexei Starovoitov * 11767dd68b32SAndrey Ignatov * With BPF_F_ALLOW_MULTI a new program is added to the end of the list of 11777dd68b32SAndrey Ignatov * programs for a cgroup. Though it's possible to replace an old program at 11787dd68b32SAndrey Ignatov * any position by also specifying BPF_F_REPLACE flag and position itself in 11797dd68b32SAndrey Ignatov * replace_bpf_fd attribute. Old program at this position will be released. 11807dd68b32SAndrey Ignatov * 1181324bda9eSAlexei Starovoitov * A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups. 1182324bda9eSAlexei Starovoitov * A cgroup with NONE doesn't allow any programs in sub-cgroups. 1183324bda9eSAlexei Starovoitov * Ex1: 1184324bda9eSAlexei Starovoitov * cgrp1 (MULTI progs A, B) -> 1185324bda9eSAlexei Starovoitov * cgrp2 (OVERRIDE prog C) -> 1186324bda9eSAlexei Starovoitov * cgrp3 (MULTI prog D) -> 1187324bda9eSAlexei Starovoitov * cgrp4 (OVERRIDE prog E) -> 1188324bda9eSAlexei Starovoitov * cgrp5 (NONE prog F) 1189324bda9eSAlexei Starovoitov * the event in cgrp5 triggers execution of F,D,A,B in that order. 1190324bda9eSAlexei Starovoitov * if prog F is detached, the execution is E,D,A,B 1191324bda9eSAlexei Starovoitov * if prog F and D are detached, the execution is E,A,B 1192324bda9eSAlexei Starovoitov * if prog F, E and D are detached, the execution is C,A,B 1193324bda9eSAlexei Starovoitov * 1194324bda9eSAlexei Starovoitov * All eligible programs are executed regardless of return code from 1195324bda9eSAlexei Starovoitov * earlier programs. 11967f677633SAlexei Starovoitov */ 11977f677633SAlexei Starovoitov #define BPF_F_ALLOW_OVERRIDE (1U << 0) 1198324bda9eSAlexei Starovoitov #define BPF_F_ALLOW_MULTI (1U << 1) 1199053c8e1fSDaniel Borkmann /* Generic attachment flags. */ 12007dd68b32SAndrey Ignatov #define BPF_F_REPLACE (1U << 2) 1201053c8e1fSDaniel Borkmann #define BPF_F_BEFORE (1U << 3) 1202053c8e1fSDaniel Borkmann #define BPF_F_AFTER (1U << 4) 1203053c8e1fSDaniel Borkmann #define BPF_F_ID (1U << 5) 1204053c8e1fSDaniel Borkmann #define BPF_F_LINK BPF_F_LINK /* 1 << 13 */ 12057f677633SAlexei Starovoitov 1206e07b98d9SDavid S. Miller /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the 1207e07b98d9SDavid S. Miller * verifier will perform strict alignment checking as if the kernel 1208e07b98d9SDavid S. Miller * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set, 1209e07b98d9SDavid S. Miller * and NET_IP_ALIGN defined to 2. 1210e07b98d9SDavid S. Miller */ 1211e07b98d9SDavid S. Miller #define BPF_F_STRICT_ALIGNMENT (1U << 0) 1212e07b98d9SDavid S. Miller 12135a70f4a6SMichael Weiß /* If BPF_F_ANY_ALIGNMENT is used in BPF_PROG_LOAD command, the 1214e9ee9efcSDavid Miller * verifier will allow any alignment whatsoever. On platforms 1215e9ee9efcSDavid Miller * with strict alignment requirements for loads ands stores (such 1216e9ee9efcSDavid Miller * as sparc and mips) the verifier validates that all loads and 1217e9ee9efcSDavid Miller * stores provably follow this requirement. This flag turns that 1218e9ee9efcSDavid Miller * checking and enforcement off. 1219e9ee9efcSDavid Miller * 1220e9ee9efcSDavid Miller * It is mostly used for testing when we want to validate the 1221e9ee9efcSDavid Miller * context and memory access aspects of the verifier, but because 1222e9ee9efcSDavid Miller * of an unaligned access the alignment check would trigger before 1223e9ee9efcSDavid Miller * the one we are interested in. 1224e9ee9efcSDavid Miller */ 1225e9ee9efcSDavid Miller #define BPF_F_ANY_ALIGNMENT (1U << 1) 1226e9ee9efcSDavid Miller 1227c240eff6SJiong Wang /* BPF_F_TEST_RND_HI32 is used in BPF_PROG_LOAD command for testing purpose. 1228c240eff6SJiong Wang * Verifier does sub-register def/use analysis and identifies instructions whose 1229c240eff6SJiong Wang * def only matters for low 32-bit, high 32-bit is never referenced later 1230c240eff6SJiong Wang * through implicit zero extension. Therefore verifier notifies JIT back-ends 1231c240eff6SJiong Wang * that it is safe to ignore clearing high 32-bit for these instructions. This 1232c240eff6SJiong Wang * saves some back-ends a lot of code-gen. However such optimization is not 1233c240eff6SJiong Wang * necessary on some arches, for example x86_64, arm64 etc, whose JIT back-ends 1234c240eff6SJiong Wang * hence hasn't used verifier's analysis result. But, we really want to have a 1235c240eff6SJiong Wang * way to be able to verify the correctness of the described optimization on 1236c240eff6SJiong Wang * x86_64 on which testsuites are frequently exercised. 1237c240eff6SJiong Wang * 1238c240eff6SJiong Wang * So, this flag is introduced. Once it is set, verifier will randomize high 1239c240eff6SJiong Wang * 32-bit for those instructions who has been identified as safe to ignore them. 1240c240eff6SJiong Wang * Then, if verifier is not doing correct analysis, such randomization will 1241c240eff6SJiong Wang * regress tests to expose bugs. 1242c240eff6SJiong Wang */ 1243c240eff6SJiong Wang #define BPF_F_TEST_RND_HI32 (1U << 2) 1244c240eff6SJiong Wang 124510d274e8SAlexei Starovoitov /* The verifier internal test flag. Behavior is undefined */ 124610d274e8SAlexei Starovoitov #define BPF_F_TEST_STATE_FREQ (1U << 3) 124710d274e8SAlexei Starovoitov 12481e6c62a8SAlexei Starovoitov /* If BPF_F_SLEEPABLE is used in BPF_PROG_LOAD command, the verifier will 12491e6c62a8SAlexei Starovoitov * restrict map and helper usage for such programs. Sleepable BPF programs can 12501e6c62a8SAlexei Starovoitov * only be attached to hooks where kernel execution context allows sleeping. 12511e6c62a8SAlexei Starovoitov * Such programs are allowed to use helpers that may sleep like 12521e6c62a8SAlexei Starovoitov * bpf_copy_from_user(). 12531e6c62a8SAlexei Starovoitov */ 12541e6c62a8SAlexei Starovoitov #define BPF_F_SLEEPABLE (1U << 4) 12551e6c62a8SAlexei Starovoitov 1256c2f2cdbeSLorenzo Bianconi /* If BPF_F_XDP_HAS_FRAGS is used in BPF_PROG_LOAD command, the loaded program 1257c2f2cdbeSLorenzo Bianconi * fully support xdp frags. 1258c2f2cdbeSLorenzo Bianconi */ 1259c2f2cdbeSLorenzo Bianconi #define BPF_F_XDP_HAS_FRAGS (1U << 5) 1260c2f2cdbeSLorenzo Bianconi 12612b3486bcSStanislav Fomichev /* If BPF_F_XDP_DEV_BOUND_ONLY is used in BPF_PROG_LOAD command, the loaded 12622b3486bcSStanislav Fomichev * program becomes device-bound but can access XDP metadata. 12632b3486bcSStanislav Fomichev */ 12642b3486bcSStanislav Fomichev #define BPF_F_XDP_DEV_BOUND_ONLY (1U << 6) 12652b3486bcSStanislav Fomichev 12665f99f312SAndrii Nakryiko /* The verifier internal test flag. Behavior is undefined */ 1267ff8867afSAndrii Nakryiko #define BPF_F_TEST_REG_INVARIANTS (1U << 7) 12685f99f312SAndrii Nakryiko 12690dcac272SJiri Olsa /* link_create.kprobe_multi.flags used in LINK_CREATE command for 12700dcac272SJiri Olsa * BPF_TRACE_KPROBE_MULTI attach type to create return probe. 12710dcac272SJiri Olsa */ 1272c5487f8dSJiri Olsa enum { 1273c5487f8dSJiri Olsa BPF_F_KPROBE_MULTI_RETURN = (1U << 0) 1274c5487f8dSJiri Olsa }; 12750dcac272SJiri Olsa 127689ae89f5SJiri Olsa /* link_create.uprobe_multi.flags used in LINK_CREATE command for 127789ae89f5SJiri Olsa * BPF_TRACE_UPROBE_MULTI attach type to create return probe. 127889ae89f5SJiri Olsa */ 127989ae89f5SJiri Olsa enum { 128089ae89f5SJiri Olsa BPF_F_UPROBE_MULTI_RETURN = (1U << 0) 128189ae89f5SJiri Olsa }; 128289ae89f5SJiri Olsa 128391721c2dSDaniel Xu /* link_create.netfilter.flags used in LINK_CREATE command for 128491721c2dSDaniel Xu * BPF_PROG_TYPE_NETFILTER to enable IP packet defragmentation. 128591721c2dSDaniel Xu */ 128691721c2dSDaniel Xu #define BPF_F_NETFILTER_IP_DEFRAG (1U << 0) 128791721c2dSDaniel Xu 1288d8eca5bbSDaniel Borkmann /* When BPF ldimm64's insn[0].src_reg != 0 then this can have 12894976b718SHao Luo * the following extensions: 1290d8eca5bbSDaniel Borkmann * 1291387544bfSAlexei Starovoitov * insn[0].src_reg: BPF_PSEUDO_MAP_[FD|IDX] 1292387544bfSAlexei Starovoitov * insn[0].imm: map fd or fd_idx 12934976b718SHao Luo * insn[1].imm: 0 12944976b718SHao Luo * insn[0].off: 0 12954976b718SHao Luo * insn[1].off: 0 12964976b718SHao Luo * ldimm64 rewrite: address of map 12974976b718SHao Luo * verifier type: CONST_PTR_TO_MAP 1298d8eca5bbSDaniel Borkmann */ 1299f1a66f85SDaniel Borkmann #define BPF_PSEUDO_MAP_FD 1 1300387544bfSAlexei Starovoitov #define BPF_PSEUDO_MAP_IDX 5 1301387544bfSAlexei Starovoitov 1302387544bfSAlexei Starovoitov /* insn[0].src_reg: BPF_PSEUDO_MAP_[IDX_]VALUE 1303387544bfSAlexei Starovoitov * insn[0].imm: map fd or fd_idx 13044976b718SHao Luo * insn[1].imm: offset into value 13054976b718SHao Luo * insn[0].off: 0 13064976b718SHao Luo * insn[1].off: 0 13074976b718SHao Luo * ldimm64 rewrite: address of map[0]+offset 13084976b718SHao Luo * verifier type: PTR_TO_MAP_VALUE 13094976b718SHao Luo */ 1310d8eca5bbSDaniel Borkmann #define BPF_PSEUDO_MAP_VALUE 2 1311387544bfSAlexei Starovoitov #define BPF_PSEUDO_MAP_IDX_VALUE 6 1312387544bfSAlexei Starovoitov 13134976b718SHao Luo /* insn[0].src_reg: BPF_PSEUDO_BTF_ID 13144976b718SHao Luo * insn[0].imm: kernel btd id of VAR 13154976b718SHao Luo * insn[1].imm: 0 13164976b718SHao Luo * insn[0].off: 0 13174976b718SHao Luo * insn[1].off: 0 13184976b718SHao Luo * ldimm64 rewrite: address of the kernel variable 13194976b718SHao Luo * verifier type: PTR_TO_BTF_ID or PTR_TO_MEM, depending on whether the var 13204976b718SHao Luo * is struct/union. 13214976b718SHao Luo */ 13224976b718SHao Luo #define BPF_PSEUDO_BTF_ID 3 132369c087baSYonghong Song /* insn[0].src_reg: BPF_PSEUDO_FUNC 132469c087baSYonghong Song * insn[0].imm: insn offset to the func 132569c087baSYonghong Song * insn[1].imm: 0 132669c087baSYonghong Song * insn[0].off: 0 132769c087baSYonghong Song * insn[1].off: 0 132869c087baSYonghong Song * ldimm64 rewrite: address of the function 132969c087baSYonghong Song * verifier type: PTR_TO_FUNC. 133069c087baSYonghong Song */ 133169c087baSYonghong Song #define BPF_PSEUDO_FUNC 4 1332f1a66f85SDaniel Borkmann 1333cc8b0b92SAlexei Starovoitov /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative 1334cc8b0b92SAlexei Starovoitov * offset to another bpf function 1335cc8b0b92SAlexei Starovoitov */ 1336cc8b0b92SAlexei Starovoitov #define BPF_PSEUDO_CALL 1 1337e6ac2450SMartin KaFai Lau /* when bpf_call->src_reg == BPF_PSEUDO_KFUNC_CALL, 1338e6ac2450SMartin KaFai Lau * bpf_call->imm == btf_id of a BTF_KIND_FUNC in the running kernel 1339e6ac2450SMartin KaFai Lau */ 1340e6ac2450SMartin KaFai Lau #define BPF_PSEUDO_KFUNC_CALL 2 1341cc8b0b92SAlexei Starovoitov 1342*667a86adSAlexei Starovoitov enum bpf_addr_space_cast { 1343*667a86adSAlexei Starovoitov BPF_ADDR_SPACE_CAST = 1, 1344*667a86adSAlexei Starovoitov }; 1345*667a86adSAlexei Starovoitov 13463274f520SAlexei Starovoitov /* flags for BPF_MAP_UPDATE_ELEM command */ 13471aae4bddSAndrii Nakryiko enum { 13481aae4bddSAndrii Nakryiko BPF_ANY = 0, /* create new element or update existing */ 13491aae4bddSAndrii Nakryiko BPF_NOEXIST = 1, /* create new element if it didn't exist */ 13501aae4bddSAndrii Nakryiko BPF_EXIST = 2, /* update existing element */ 13511aae4bddSAndrii Nakryiko BPF_F_LOCK = 4, /* spin_lock-ed map_lookup/map_update */ 13521aae4bddSAndrii Nakryiko }; 13533274f520SAlexei Starovoitov 135496eabe7aSMartin KaFai Lau /* flags for BPF_MAP_CREATE command */ 13551aae4bddSAndrii Nakryiko enum { 13561aae4bddSAndrii Nakryiko BPF_F_NO_PREALLOC = (1U << 0), 135729ba732aSMartin KaFai Lau /* Instead of having one common LRU list in the 13588f844938SMartin KaFai Lau * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list 135929ba732aSMartin KaFai Lau * which can scale and perform better. 136029ba732aSMartin KaFai Lau * Note, the LRU nodes (including free nodes) cannot be moved 136129ba732aSMartin KaFai Lau * across different LRU lists. 136229ba732aSMartin KaFai Lau */ 13631aae4bddSAndrii Nakryiko BPF_F_NO_COMMON_LRU = (1U << 1), 136496eabe7aSMartin KaFai Lau /* Specify numa node during map creation */ 13651aae4bddSAndrii Nakryiko BPF_F_NUMA_NODE = (1U << 2), 1366cb4d2b3fSMartin KaFai Lau 1367591fe988SDaniel Borkmann /* Flags for accessing BPF object from syscall side. */ 13681aae4bddSAndrii Nakryiko BPF_F_RDONLY = (1U << 3), 13691aae4bddSAndrii Nakryiko BPF_F_WRONLY = (1U << 4), 13706e71b04aSChenbo Feng 1371615755a7SSong Liu /* Flag for stack_map, store build_id+offset instead of pointer */ 13721aae4bddSAndrii Nakryiko BPF_F_STACK_BUILD_ID = (1U << 5), 1373615755a7SSong Liu 137496b3b6c9SLorenz Bauer /* Zero-initialize hash function seed. This should only be used for testing. */ 13751aae4bddSAndrii Nakryiko BPF_F_ZERO_SEED = (1U << 6), 137696b3b6c9SLorenz Bauer 1377591fe988SDaniel Borkmann /* Flags for accessing BPF object from program side. */ 13781aae4bddSAndrii Nakryiko BPF_F_RDONLY_PROG = (1U << 7), 13791aae4bddSAndrii Nakryiko BPF_F_WRONLY_PROG = (1U << 8), 1380591fe988SDaniel Borkmann 13818f51dfc7SStanislav Fomichev /* Clone map from listener for newly accepted socket */ 13821aae4bddSAndrii Nakryiko BPF_F_CLONE = (1U << 9), 13838f51dfc7SStanislav Fomichev 1384fc970227SAndrii Nakryiko /* Enable memory-mapping BPF map */ 13851aae4bddSAndrii Nakryiko BPF_F_MMAPABLE = (1U << 10), 1386792cacccSSong Liu 1387792cacccSSong Liu /* Share perf_event among processes */ 1388792cacccSSong Liu BPF_F_PRESERVE_ELEMS = (1U << 11), 13894a8f87e6SDaniel Borkmann 13904a8f87e6SDaniel Borkmann /* Create a map that is suitable to be an inner map with dynamic max entries */ 13914a8f87e6SDaniel Borkmann BPF_F_INNER_MAP = (1U << 12), 139268b04864SKui-Feng Lee 139368b04864SKui-Feng Lee /* Create a map that will be registered/unregesitered by the backed bpf_link */ 139468b04864SKui-Feng Lee BPF_F_LINK = (1U << 13), 1395cb8edce2SAndrii Nakryiko 1396cb8edce2SAndrii Nakryiko /* Get path from provided FD in BPF_OBJ_PIN/BPF_OBJ_GET commands */ 1397cb8edce2SAndrii Nakryiko BPF_F_PATH_FD = (1U << 14), 1398fcc2c1fbSKui-Feng Lee 1399fcc2c1fbSKui-Feng Lee /* Flag for value_type_btf_obj_fd, the fd is available */ 1400fcc2c1fbSKui-Feng Lee BPF_F_VTYPE_BTF_OBJ_FD = (1U << 15), 1401a177fc2bSAndrii Nakryiko 1402a177fc2bSAndrii Nakryiko /* BPF token FD is passed in a corresponding command's token_fd field */ 1403a177fc2bSAndrii Nakryiko BPF_F_TOKEN_FD = (1U << 16), 140431746031SAlexei Starovoitov 140531746031SAlexei Starovoitov /* When user space page faults in bpf_arena send SIGSEGV instead of inserting new page */ 140631746031SAlexei Starovoitov BPF_F_SEGV_ON_FAULT = (1U << 17), 140731746031SAlexei Starovoitov 140831746031SAlexei Starovoitov /* Do not translate kernel bpf_arena pointers to user pointers */ 140931746031SAlexei Starovoitov BPF_F_NO_USER_CONV = (1U << 18), 14101aae4bddSAndrii Nakryiko }; 1411fc970227SAndrii Nakryiko 1412f5bfcd95SAndrey Ignatov /* Flags for BPF_PROG_QUERY. */ 1413f5bfcd95SAndrey Ignatov 1414f5bfcd95SAndrey Ignatov /* Query effective (directly attached + inherited from ancestor cgroups) 1415f5bfcd95SAndrey Ignatov * programs that will be executed for events within a cgroup. 14160e426a3aSPu Lehui * attach_flags with this flag are always returned 0. 1417f5bfcd95SAndrey Ignatov */ 14182f183360SLorenz Bauer #define BPF_F_QUERY_EFFECTIVE (1U << 0) 14192f183360SLorenz Bauer 14201b4d60ecSSong Liu /* Flags for BPF_PROG_TEST_RUN */ 14211b4d60ecSSong Liu 14221b4d60ecSSong Liu /* If set, run the test on the cpu specified by bpf_attr.test.cpu */ 14231b4d60ecSSong Liu #define BPF_F_TEST_RUN_ON_CPU (1U << 0) 1424b530e9e1SToke Høiland-Jørgensen /* If set, XDP frames will be transmitted after processing */ 1425b530e9e1SToke Høiland-Jørgensen #define BPF_F_TEST_XDP_LIVE_FRAMES (1U << 1) 14261b4d60ecSSong Liu 1427d46edd67SSong Liu /* type for BPF_ENABLE_STATS */ 1428d46edd67SSong Liu enum bpf_stats_type { 1429d46edd67SSong Liu /* enabled run_time_ns and run_cnt */ 1430d46edd67SSong Liu BPF_STATS_RUN_TIME = 0, 1431d46edd67SSong Liu }; 1432d46edd67SSong Liu 1433615755a7SSong Liu enum bpf_stack_build_id_status { 1434615755a7SSong Liu /* user space need an empty entry to identify end of a trace */ 1435615755a7SSong Liu BPF_STACK_BUILD_ID_EMPTY = 0, 1436615755a7SSong Liu /* with valid build_id and offset */ 1437615755a7SSong Liu BPF_STACK_BUILD_ID_VALID = 1, 1438615755a7SSong Liu /* couldn't get build_id, fallback to ip */ 1439615755a7SSong Liu BPF_STACK_BUILD_ID_IP = 2, 1440615755a7SSong Liu }; 1441615755a7SSong Liu 1442615755a7SSong Liu #define BPF_BUILD_ID_SIZE 20 1443615755a7SSong Liu struct bpf_stack_build_id { 1444615755a7SSong Liu __s32 status; 1445615755a7SSong Liu unsigned char build_id[BPF_BUILD_ID_SIZE]; 1446615755a7SSong Liu union { 1447615755a7SSong Liu __u64 offset; 1448615755a7SSong Liu __u64 ip; 1449615755a7SSong Liu }; 1450615755a7SSong Liu }; 1451615755a7SSong Liu 14521aae4bddSAndrii Nakryiko #define BPF_OBJ_NAME_LEN 16U 14531aae4bddSAndrii Nakryiko 145499c55f7dSAlexei Starovoitov union bpf_attr { 145599c55f7dSAlexei Starovoitov struct { /* anonymous struct used by BPF_MAP_CREATE command */ 145699c55f7dSAlexei Starovoitov __u32 map_type; /* one of enum bpf_map_type */ 145799c55f7dSAlexei Starovoitov __u32 key_size; /* size of key in bytes */ 145899c55f7dSAlexei Starovoitov __u32 value_size; /* size of value in bytes */ 145999c55f7dSAlexei Starovoitov __u32 max_entries; /* max number of entries in a map */ 146096eabe7aSMartin KaFai Lau __u32 map_flags; /* BPF_MAP_CREATE related 146196eabe7aSMartin KaFai Lau * flags defined above. 146296eabe7aSMartin KaFai Lau */ 146356f668dfSMartin KaFai Lau __u32 inner_map_fd; /* fd pointing to the inner map */ 146496eabe7aSMartin KaFai Lau __u32 numa_node; /* numa node (effective only if 146596eabe7aSMartin KaFai Lau * BPF_F_NUMA_NODE is set). 146696eabe7aSMartin KaFai Lau */ 1467067cae47SMartin KaFai Lau char map_name[BPF_OBJ_NAME_LEN]; 1468a3884572SJakub Kicinski __u32 map_ifindex; /* ifindex of netdev to create on */ 1469a26ca7c9SMartin KaFai Lau __u32 btf_fd; /* fd pointing to a BTF type data */ 14709b2cf328SMartin KaFai Lau __u32 btf_key_type_id; /* BTF type_id of the key */ 14719b2cf328SMartin KaFai Lau __u32 btf_value_type_id; /* BTF type_id of the value */ 147285d33df3SMartin KaFai Lau __u32 btf_vmlinux_value_type_id;/* BTF type_id of a kernel- 147385d33df3SMartin KaFai Lau * struct stored as the 147485d33df3SMartin KaFai Lau * map value 147585d33df3SMartin KaFai Lau */ 14769330986cSJoanne Koong /* Any per-map-type extra fields 14779330986cSJoanne Koong * 14789330986cSJoanne Koong * BPF_MAP_TYPE_BLOOM_FILTER - the lowest 4 bits indicate the 14799330986cSJoanne Koong * number of hash functions (if 0, the bloom filter will default 14809330986cSJoanne Koong * to using 5 hash functions). 148131746031SAlexei Starovoitov * 148231746031SAlexei Starovoitov * BPF_MAP_TYPE_ARENA - contains the address where user space 148331746031SAlexei Starovoitov * is going to mmap() the arena. It has to be page aligned. 14849330986cSJoanne Koong */ 14859330986cSJoanne Koong __u64 map_extra; 1486fcc2c1fbSKui-Feng Lee 1487fcc2c1fbSKui-Feng Lee __s32 value_type_btf_obj_fd; /* fd pointing to a BTF 1488fcc2c1fbSKui-Feng Lee * type data for 1489fcc2c1fbSKui-Feng Lee * btf_vmlinux_value_type_id. 1490fcc2c1fbSKui-Feng Lee */ 1491a177fc2bSAndrii Nakryiko /* BPF token FD to use with BPF_MAP_CREATE operation. 1492a177fc2bSAndrii Nakryiko * If provided, map_flags should have BPF_F_TOKEN_FD flag set. 1493a177fc2bSAndrii Nakryiko */ 1494a177fc2bSAndrii Nakryiko __s32 map_token_fd; 149599c55f7dSAlexei Starovoitov }; 1496db20fd2bSAlexei Starovoitov 1497db20fd2bSAlexei Starovoitov struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ 1498db20fd2bSAlexei Starovoitov __u32 map_fd; 1499db20fd2bSAlexei Starovoitov __aligned_u64 key; 1500db20fd2bSAlexei Starovoitov union { 1501db20fd2bSAlexei Starovoitov __aligned_u64 value; 1502db20fd2bSAlexei Starovoitov __aligned_u64 next_key; 1503db20fd2bSAlexei Starovoitov }; 15043274f520SAlexei Starovoitov __u64 flags; 1505db20fd2bSAlexei Starovoitov }; 150609756af4SAlexei Starovoitov 1507cb4d03abSBrian Vazquez struct { /* struct used by BPF_MAP_*_BATCH commands */ 1508cb4d03abSBrian Vazquez __aligned_u64 in_batch; /* start batch, 1509cb4d03abSBrian Vazquez * NULL to start from beginning 1510cb4d03abSBrian Vazquez */ 1511cb4d03abSBrian Vazquez __aligned_u64 out_batch; /* output: next start batch */ 1512cb4d03abSBrian Vazquez __aligned_u64 keys; 1513cb4d03abSBrian Vazquez __aligned_u64 values; 1514cb4d03abSBrian Vazquez __u32 count; /* input/output: 1515cb4d03abSBrian Vazquez * input: # of key/value 1516cb4d03abSBrian Vazquez * elements 1517cb4d03abSBrian Vazquez * output: # of filled elements 1518cb4d03abSBrian Vazquez */ 1519cb4d03abSBrian Vazquez __u32 map_fd; 1520cb4d03abSBrian Vazquez __u64 elem_flags; 1521cb4d03abSBrian Vazquez __u64 flags; 1522cb4d03abSBrian Vazquez } batch; 1523cb4d03abSBrian Vazquez 152409756af4SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_LOAD command */ 152509756af4SAlexei Starovoitov __u32 prog_type; /* one of enum bpf_prog_type */ 152609756af4SAlexei Starovoitov __u32 insn_cnt; 152709756af4SAlexei Starovoitov __aligned_u64 insns; 152809756af4SAlexei Starovoitov __aligned_u64 license; 1529cbd35700SAlexei Starovoitov __u32 log_level; /* verbosity level of verifier */ 1530cbd35700SAlexei Starovoitov __u32 log_size; /* size of user buffer */ 1531cbd35700SAlexei Starovoitov __aligned_u64 log_buf; /* user supplied buffer */ 15326c4fc209SDaniel Borkmann __u32 kern_version; /* not used */ 1533e07b98d9SDavid S. Miller __u32 prog_flags; 1534067cae47SMartin KaFai Lau char prog_name[BPF_OBJ_NAME_LEN]; 15351f6f4cb7SJakub Kicinski __u32 prog_ifindex; /* ifindex of netdev to prep for */ 15365e43f899SAndrey Ignatov /* For some prog types expected attach type must be known at 15375e43f899SAndrey Ignatov * load time to verify attach type specific parts of prog 15385e43f899SAndrey Ignatov * (context accesses, allowed helpers, etc). 15395e43f899SAndrey Ignatov */ 15405e43f899SAndrey Ignatov __u32 expected_attach_type; 1541838e9690SYonghong Song __u32 prog_btf_fd; /* fd pointing to BTF type data */ 1542838e9690SYonghong Song __u32 func_info_rec_size; /* userspace bpf_func_info size */ 1543838e9690SYonghong Song __aligned_u64 func_info; /* func info */ 1544838e9690SYonghong Song __u32 func_info_cnt; /* number of bpf_func_info records */ 1545c454a46bSMartin KaFai Lau __u32 line_info_rec_size; /* userspace bpf_line_info size */ 1546c454a46bSMartin KaFai Lau __aligned_u64 line_info; /* line info */ 1547c454a46bSMartin KaFai Lau __u32 line_info_cnt; /* number of bpf_line_info records */ 1548ccfe29ebSAlexei Starovoitov __u32 attach_btf_id; /* in-kernel BTF type id to attach to */ 1549290248a5SAndrii Nakryiko union { 1550290248a5SAndrii Nakryiko /* valid prog_fd to attach to bpf prog */ 1551290248a5SAndrii Nakryiko __u32 attach_prog_fd; 1552290248a5SAndrii Nakryiko /* or valid module BTF object fd or 0 to attach to vmlinux */ 1553290248a5SAndrii Nakryiko __u32 attach_btf_obj_fd; 1554290248a5SAndrii Nakryiko }; 1555fbd94c7aSAlexei Starovoitov __u32 core_relo_cnt; /* number of bpf_core_relo */ 1556387544bfSAlexei Starovoitov __aligned_u64 fd_array; /* array of FDs */ 1557fbd94c7aSAlexei Starovoitov __aligned_u64 core_relos; 1558fbd94c7aSAlexei Starovoitov __u32 core_relo_rec_size; /* sizeof(struct bpf_core_relo) */ 155947a71c1fSAndrii Nakryiko /* output: actual total log contents size (including termintaing zero). 156047a71c1fSAndrii Nakryiko * It could be both larger than original log_size (if log was 156147a71c1fSAndrii Nakryiko * truncated), or smaller (if log buffer wasn't filled completely). 156247a71c1fSAndrii Nakryiko */ 156347a71c1fSAndrii Nakryiko __u32 log_true_size; 1564caf8f28eSAndrii Nakryiko /* BPF token FD to use with BPF_PROG_LOAD operation. 1565caf8f28eSAndrii Nakryiko * If provided, prog_flags should have BPF_F_TOKEN_FD flag set. 1566caf8f28eSAndrii Nakryiko */ 1567caf8f28eSAndrii Nakryiko __s32 prog_token_fd; 156809756af4SAlexei Starovoitov }; 1569b2197755SDaniel Borkmann 1570b2197755SDaniel Borkmann struct { /* anonymous struct used by BPF_OBJ_* commands */ 1571b2197755SDaniel Borkmann __aligned_u64 pathname; 1572b2197755SDaniel Borkmann __u32 bpf_fd; 15736e71b04aSChenbo Feng __u32 file_flags; 1574cb8edce2SAndrii Nakryiko /* Same as dirfd in openat() syscall; see openat(2) 1575cb8edce2SAndrii Nakryiko * manpage for details of path FD and pathname semantics; 1576cb8edce2SAndrii Nakryiko * path_fd should accompanied by BPF_F_PATH_FD flag set in 1577cb8edce2SAndrii Nakryiko * file_flags field, otherwise it should be set to zero; 1578cb8edce2SAndrii Nakryiko * if BPF_F_PATH_FD flag is not set, AT_FDCWD is assumed. 1579cb8edce2SAndrii Nakryiko */ 1580cb8edce2SAndrii Nakryiko __s32 path_fd; 1581b2197755SDaniel Borkmann }; 1582f4324551SDaniel Mack 1583f4324551SDaniel Mack struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ 1584053c8e1fSDaniel Borkmann union { 1585053c8e1fSDaniel Borkmann __u32 target_fd; /* target object to attach to or ... */ 1586053c8e1fSDaniel Borkmann __u32 target_ifindex; /* target ifindex */ 1587053c8e1fSDaniel Borkmann }; 1588053c8e1fSDaniel Borkmann __u32 attach_bpf_fd; 1589f4324551SDaniel Mack __u32 attach_type; 15907f677633SAlexei Starovoitov __u32 attach_flags; 1591053c8e1fSDaniel Borkmann __u32 replace_bpf_fd; 1592053c8e1fSDaniel Borkmann union { 1593053c8e1fSDaniel Borkmann __u32 relative_fd; 1594053c8e1fSDaniel Borkmann __u32 relative_id; 1595053c8e1fSDaniel Borkmann }; 1596053c8e1fSDaniel Borkmann __u64 expected_revision; 1597f4324551SDaniel Mack }; 15981cf1cae9SAlexei Starovoitov 15991cf1cae9SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */ 16001cf1cae9SAlexei Starovoitov __u32 prog_fd; 16011cf1cae9SAlexei Starovoitov __u32 retval; 1602b5a36b1eSLorenz Bauer __u32 data_size_in; /* input: len of data_in */ 1603b5a36b1eSLorenz Bauer __u32 data_size_out; /* input/output: len of data_out 1604b5a36b1eSLorenz Bauer * returns ENOSPC if data_out 1605b5a36b1eSLorenz Bauer * is too small. 1606b5a36b1eSLorenz Bauer */ 16071cf1cae9SAlexei Starovoitov __aligned_u64 data_in; 16081cf1cae9SAlexei Starovoitov __aligned_u64 data_out; 16091cf1cae9SAlexei Starovoitov __u32 repeat; 16101cf1cae9SAlexei Starovoitov __u32 duration; 1611b0b9395dSStanislav Fomichev __u32 ctx_size_in; /* input: len of ctx_in */ 1612b0b9395dSStanislav Fomichev __u32 ctx_size_out; /* input/output: len of ctx_out 1613b0b9395dSStanislav Fomichev * returns ENOSPC if ctx_out 1614b0b9395dSStanislav Fomichev * is too small. 1615b0b9395dSStanislav Fomichev */ 1616b0b9395dSStanislav Fomichev __aligned_u64 ctx_in; 1617b0b9395dSStanislav Fomichev __aligned_u64 ctx_out; 16181b4d60ecSSong Liu __u32 flags; 16191b4d60ecSSong Liu __u32 cpu; 1620b530e9e1SToke Høiland-Jørgensen __u32 batch_size; 16211cf1cae9SAlexei Starovoitov } test; 162234ad5580SMartin KaFai Lau 1623b16d9aa4SMartin KaFai Lau struct { /* anonymous struct used by BPF_*_GET_*_ID */ 1624b16d9aa4SMartin KaFai Lau union { 162534ad5580SMartin KaFai Lau __u32 start_id; 1626b16d9aa4SMartin KaFai Lau __u32 prog_id; 1627bd5f5f4eSMartin KaFai Lau __u32 map_id; 162878958fcaSMartin KaFai Lau __u32 btf_id; 1629a3b80e10SAndrii Nakryiko __u32 link_id; 1630b16d9aa4SMartin KaFai Lau }; 163134ad5580SMartin KaFai Lau __u32 next_id; 16326e71b04aSChenbo Feng __u32 open_flags; 163334ad5580SMartin KaFai Lau }; 16341e270976SMartin KaFai Lau 16351e270976SMartin KaFai Lau struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ 16361e270976SMartin KaFai Lau __u32 bpf_fd; 16371e270976SMartin KaFai Lau __u32 info_len; 16381e270976SMartin KaFai Lau __aligned_u64 info; 16391e270976SMartin KaFai Lau } info; 1640468e2f64SAlexei Starovoitov 1641468e2f64SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_QUERY command */ 1642053c8e1fSDaniel Borkmann union { 1643053c8e1fSDaniel Borkmann __u32 target_fd; /* target object to query or ... */ 1644053c8e1fSDaniel Borkmann __u32 target_ifindex; /* target ifindex */ 1645053c8e1fSDaniel Borkmann }; 1646468e2f64SAlexei Starovoitov __u32 attach_type; 1647468e2f64SAlexei Starovoitov __u32 query_flags; 1648468e2f64SAlexei Starovoitov __u32 attach_flags; 1649468e2f64SAlexei Starovoitov __aligned_u64 prog_ids; 1650053c8e1fSDaniel Borkmann union { 1651468e2f64SAlexei Starovoitov __u32 prog_cnt; 1652053c8e1fSDaniel Borkmann __u32 count; 1653053c8e1fSDaniel Borkmann }; 1654053c8e1fSDaniel Borkmann __u32 :32; 16550e426a3aSPu Lehui /* output: per-program attach_flags. 16560e426a3aSPu Lehui * not allowed to be set during effective query. 16570e426a3aSPu Lehui */ 16580e426a3aSPu Lehui __aligned_u64 prog_attach_flags; 1659053c8e1fSDaniel Borkmann __aligned_u64 link_ids; 1660053c8e1fSDaniel Borkmann __aligned_u64 link_attach_flags; 1661053c8e1fSDaniel Borkmann __u64 revision; 1662468e2f64SAlexei Starovoitov } query; 1663c4f6699dSAlexei Starovoitov 1664af6eea57SAndrii Nakryiko struct { /* anonymous struct used by BPF_RAW_TRACEPOINT_OPEN command */ 1665c4f6699dSAlexei Starovoitov __u64 name; 1666c4f6699dSAlexei Starovoitov __u32 prog_fd; 1667c4f6699dSAlexei Starovoitov } raw_tracepoint; 1668f56a653cSMartin KaFai Lau 1669f56a653cSMartin KaFai Lau struct { /* anonymous struct for BPF_BTF_LOAD */ 1670f56a653cSMartin KaFai Lau __aligned_u64 btf; 1671f56a653cSMartin KaFai Lau __aligned_u64 btf_log_buf; 1672f56a653cSMartin KaFai Lau __u32 btf_size; 1673f56a653cSMartin KaFai Lau __u32 btf_log_size; 1674f56a653cSMartin KaFai Lau __u32 btf_log_level; 167547a71c1fSAndrii Nakryiko /* output: actual total log contents size (including termintaing zero). 167647a71c1fSAndrii Nakryiko * It could be both larger than original log_size (if log was 167747a71c1fSAndrii Nakryiko * truncated), or smaller (if log buffer wasn't filled completely). 167847a71c1fSAndrii Nakryiko */ 167947a71c1fSAndrii Nakryiko __u32 btf_log_true_size; 16809ea7c4bfSAndrii Nakryiko __u32 btf_flags; 16819ea7c4bfSAndrii Nakryiko /* BPF token FD to use with BPF_BTF_LOAD operation. 16829ea7c4bfSAndrii Nakryiko * If provided, btf_flags should have BPF_F_TOKEN_FD flag set. 16839ea7c4bfSAndrii Nakryiko */ 16849ea7c4bfSAndrii Nakryiko __s32 btf_token_fd; 1685f56a653cSMartin KaFai Lau }; 168641bdc4b4SYonghong Song 168741bdc4b4SYonghong Song struct { 168841bdc4b4SYonghong Song __u32 pid; /* input: pid */ 168941bdc4b4SYonghong Song __u32 fd; /* input: fd */ 169041bdc4b4SYonghong Song __u32 flags; /* input: flags */ 169141bdc4b4SYonghong Song __u32 buf_len; /* input/output: buf len */ 169241bdc4b4SYonghong Song __aligned_u64 buf; /* input/output: 169341bdc4b4SYonghong Song * tp_name for tracepoint 169441bdc4b4SYonghong Song * symbol for kprobe 169541bdc4b4SYonghong Song * filename for uprobe 169641bdc4b4SYonghong Song */ 169741bdc4b4SYonghong Song __u32 prog_id; /* output: prod_id */ 169841bdc4b4SYonghong Song __u32 fd_type; /* output: BPF_FD_TYPE_* */ 169941bdc4b4SYonghong Song __u64 probe_offset; /* output: probe_offset */ 170041bdc4b4SYonghong Song __u64 probe_addr; /* output: probe_addr */ 170141bdc4b4SYonghong Song } task_fd_query; 1702af6eea57SAndrii Nakryiko 1703af6eea57SAndrii Nakryiko struct { /* struct used by BPF_LINK_CREATE command */ 170468b04864SKui-Feng Lee union { 1705af6eea57SAndrii Nakryiko __u32 prog_fd; /* eBPF program to attach */ 170668b04864SKui-Feng Lee __u32 map_fd; /* struct_ops to attach */ 170768b04864SKui-Feng Lee }; 1708aa8d3a71SAndrii Nakryiko union { 1709e420bed0SDaniel Borkmann __u32 target_fd; /* target object to attach to or ... */ 1710aa8d3a71SAndrii Nakryiko __u32 target_ifindex; /* target ifindex */ 1711aa8d3a71SAndrii Nakryiko }; 1712af6eea57SAndrii Nakryiko __u32 attach_type; /* attach type */ 1713af6eea57SAndrii Nakryiko __u32 flags; /* extra flags */ 17144a1e7c0cSToke Høiland-Jørgensen union { 17154a1e7c0cSToke Høiland-Jørgensen __u32 target_btf_id; /* btf_id of target to attach to */ 17164a1e7c0cSToke Høiland-Jørgensen struct { 17175e7b3020SYonghong Song __aligned_u64 iter_info; /* extra bpf_iter_link_info */ 17185e7b3020SYonghong Song __u32 iter_info_len; /* iter_info length */ 17194a1e7c0cSToke Høiland-Jørgensen }; 172082e6b1eeSAndrii Nakryiko struct { 172182e6b1eeSAndrii Nakryiko /* black box user-provided value passed through 172282e6b1eeSAndrii Nakryiko * to BPF program at the execution time and 172382e6b1eeSAndrii Nakryiko * accessible through bpf_get_attach_cookie() BPF helper 172482e6b1eeSAndrii Nakryiko */ 172582e6b1eeSAndrii Nakryiko __u64 bpf_cookie; 172682e6b1eeSAndrii Nakryiko } perf_event; 17270dcac272SJiri Olsa struct { 17280dcac272SJiri Olsa __u32 flags; 17290dcac272SJiri Olsa __u32 cnt; 17300dcac272SJiri Olsa __aligned_u64 syms; 17310dcac272SJiri Olsa __aligned_u64 addrs; 1732ca74823cSJiri Olsa __aligned_u64 cookies; 17330dcac272SJiri Olsa } kprobe_multi; 17342fcc8241SKui-Feng Lee struct { 17352fcc8241SKui-Feng Lee /* this is overlaid with the target_btf_id above. */ 17362fcc8241SKui-Feng Lee __u32 target_btf_id; 17372fcc8241SKui-Feng Lee /* black box user-provided value passed through 17382fcc8241SKui-Feng Lee * to BPF program at the execution time and 17392fcc8241SKui-Feng Lee * accessible through bpf_get_attach_cookie() BPF helper 17402fcc8241SKui-Feng Lee */ 17412fcc8241SKui-Feng Lee __u64 cookie; 17422fcc8241SKui-Feng Lee } tracing; 174384601d6eSFlorian Westphal struct { 174484601d6eSFlorian Westphal __u32 pf; 174584601d6eSFlorian Westphal __u32 hooknum; 174684601d6eSFlorian Westphal __s32 priority; 174784601d6eSFlorian Westphal __u32 flags; 174884601d6eSFlorian Westphal } netfilter; 1749e420bed0SDaniel Borkmann struct { 1750e420bed0SDaniel Borkmann union { 1751e420bed0SDaniel Borkmann __u32 relative_fd; 1752e420bed0SDaniel Borkmann __u32 relative_id; 1753e420bed0SDaniel Borkmann }; 1754e420bed0SDaniel Borkmann __u64 expected_revision; 1755e420bed0SDaniel Borkmann } tcx; 175689ae89f5SJiri Olsa struct { 175789ae89f5SJiri Olsa __aligned_u64 path; 175889ae89f5SJiri Olsa __aligned_u64 offsets; 175989ae89f5SJiri Olsa __aligned_u64 ref_ctr_offsets; 17600b779b61SJiri Olsa __aligned_u64 cookies; 176189ae89f5SJiri Olsa __u32 cnt; 176289ae89f5SJiri Olsa __u32 flags; 1763b733eeadSJiri Olsa __u32 pid; 176489ae89f5SJiri Olsa } uprobe_multi; 176535dfaad7SDaniel Borkmann struct { 176635dfaad7SDaniel Borkmann union { 176735dfaad7SDaniel Borkmann __u32 relative_fd; 176835dfaad7SDaniel Borkmann __u32 relative_id; 176935dfaad7SDaniel Borkmann }; 177035dfaad7SDaniel Borkmann __u64 expected_revision; 177135dfaad7SDaniel Borkmann } netkit; 17724a1e7c0cSToke Høiland-Jørgensen }; 1773af6eea57SAndrii Nakryiko } link_create; 17740c991ebcSAndrii Nakryiko 17750c991ebcSAndrii Nakryiko struct { /* struct used by BPF_LINK_UPDATE command */ 17760c991ebcSAndrii Nakryiko __u32 link_fd; /* link fd */ 1777aef56f2eSKui-Feng Lee union { 17780c991ebcSAndrii Nakryiko /* new program fd to update link with */ 17790c991ebcSAndrii Nakryiko __u32 new_prog_fd; 1780aef56f2eSKui-Feng Lee /* new struct_ops map fd to update link with */ 1781aef56f2eSKui-Feng Lee __u32 new_map_fd; 1782aef56f2eSKui-Feng Lee }; 17830c991ebcSAndrii Nakryiko __u32 flags; /* extra flags */ 1784aef56f2eSKui-Feng Lee union { 17850c991ebcSAndrii Nakryiko /* expected link's program fd; is specified only if 1786aef56f2eSKui-Feng Lee * BPF_F_REPLACE flag is set in flags. 1787aef56f2eSKui-Feng Lee */ 17880c991ebcSAndrii Nakryiko __u32 old_prog_fd; 1789aef56f2eSKui-Feng Lee /* expected link's map fd; is specified only 1790aef56f2eSKui-Feng Lee * if BPF_F_REPLACE flag is set. 1791aef56f2eSKui-Feng Lee */ 1792aef56f2eSKui-Feng Lee __u32 old_map_fd; 1793aef56f2eSKui-Feng Lee }; 17940c991ebcSAndrii Nakryiko } link_update; 17950c991ebcSAndrii Nakryiko 179673b11c2aSAndrii Nakryiko struct { 179773b11c2aSAndrii Nakryiko __u32 link_fd; 179873b11c2aSAndrii Nakryiko } link_detach; 179973b11c2aSAndrii Nakryiko 1800d46edd67SSong Liu struct { /* struct used by BPF_ENABLE_STATS command */ 1801d46edd67SSong Liu __u32 type; 1802d46edd67SSong Liu } enable_stats; 1803d46edd67SSong Liu 1804ac51d99bSYonghong Song struct { /* struct used by BPF_ITER_CREATE command */ 1805ac51d99bSYonghong Song __u32 link_fd; 1806ac51d99bSYonghong Song __u32 flags; 1807ac51d99bSYonghong Song } iter_create; 1808ac51d99bSYonghong Song 1809ef15314aSYiFei Zhu struct { /* struct used by BPF_PROG_BIND_MAP command */ 1810ef15314aSYiFei Zhu __u32 prog_fd; 1811ef15314aSYiFei Zhu __u32 map_fd; 1812ef15314aSYiFei Zhu __u32 flags; /* extra flags */ 1813ef15314aSYiFei Zhu } prog_bind_map; 1814ef15314aSYiFei Zhu 181535f96de0SAndrii Nakryiko struct { /* struct used by BPF_TOKEN_CREATE command */ 181635f96de0SAndrii Nakryiko __u32 flags; 181735f96de0SAndrii Nakryiko __u32 bpffs_fd; 181835f96de0SAndrii Nakryiko } token_create; 181935f96de0SAndrii Nakryiko 182099c55f7dSAlexei Starovoitov } __attribute__((aligned(8))); 182199c55f7dSAlexei Starovoitov 182256a092c8SQuentin Monnet /* The description below is an attempt at providing documentation to eBPF 182356a092c8SQuentin Monnet * developers about the multiple available eBPF helper functions. It can be 182456a092c8SQuentin Monnet * parsed and used to produce a manual page. The workflow is the following, 182556a092c8SQuentin Monnet * and requires the rst2man utility: 1826ebb676daSThomas Graf * 1827923a932cSJoe Stringer * $ ./scripts/bpf_doc.py \ 182856a092c8SQuentin Monnet * --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst 182956a092c8SQuentin Monnet * $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7 183056a092c8SQuentin Monnet * $ man /tmp/bpf-helpers.7 1831ebb676daSThomas Graf * 183256a092c8SQuentin Monnet * Note that in order to produce this external documentation, some RST 183356a092c8SQuentin Monnet * formatting is used in the descriptions to get "bold" and "italics" in 183456a092c8SQuentin Monnet * manual pages. Also note that the few trailing white spaces are 183556a092c8SQuentin Monnet * intentional, removing them would break paragraphs for rst2man. 1836ebb676daSThomas Graf * 183756a092c8SQuentin Monnet * Start of BPF helper function descriptions: 1838ad4a5223SQuentin Monnet * 1839ad4a5223SQuentin Monnet * void *bpf_map_lookup_elem(struct bpf_map *map, const void *key) 1840ad4a5223SQuentin Monnet * Description 1841ad4a5223SQuentin Monnet * Perform a lookup in *map* for an entry associated to *key*. 1842ad4a5223SQuentin Monnet * Return 1843ad4a5223SQuentin Monnet * Map value associated to *key*, or **NULL** if no entry was 1844ad4a5223SQuentin Monnet * found. 1845ad4a5223SQuentin Monnet * 1846bdb7b79bSAndrii Nakryiko * long bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags) 1847ad4a5223SQuentin Monnet * Description 1848ad4a5223SQuentin Monnet * Add or update the value of the entry associated to *key* in 1849ad4a5223SQuentin Monnet * *map* with *value*. *flags* is one of: 1850ad4a5223SQuentin Monnet * 1851ad4a5223SQuentin Monnet * **BPF_NOEXIST** 1852ad4a5223SQuentin Monnet * The entry for *key* must not exist in the map. 1853ad4a5223SQuentin Monnet * **BPF_EXIST** 1854ad4a5223SQuentin Monnet * The entry for *key* must already exist in the map. 1855ad4a5223SQuentin Monnet * **BPF_ANY** 1856ad4a5223SQuentin Monnet * No condition on the existence of the entry for *key*. 1857ad4a5223SQuentin Monnet * 1858ad4a5223SQuentin Monnet * Flag value **BPF_NOEXIST** cannot be used for maps of types 1859ad4a5223SQuentin Monnet * **BPF_MAP_TYPE_ARRAY** or **BPF_MAP_TYPE_PERCPU_ARRAY** (all 1860ad4a5223SQuentin Monnet * elements always exist), the helper would return an error. 1861ad4a5223SQuentin Monnet * Return 1862ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 1863ad4a5223SQuentin Monnet * 1864bdb7b79bSAndrii Nakryiko * long bpf_map_delete_elem(struct bpf_map *map, const void *key) 1865ad4a5223SQuentin Monnet * Description 1866ad4a5223SQuentin Monnet * Delete entry with *key* from *map*. 1867ad4a5223SQuentin Monnet * Return 1868ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 1869ad4a5223SQuentin Monnet * 1870bdb7b79bSAndrii Nakryiko * long bpf_probe_read(void *dst, u32 size, const void *unsafe_ptr) 1871ad4a5223SQuentin Monnet * Description 1872ad4a5223SQuentin Monnet * For tracing programs, safely attempt to read *size* bytes from 18736ae08ae3SDaniel Borkmann * kernel space address *unsafe_ptr* and store the data in *dst*. 18746ae08ae3SDaniel Borkmann * 1875ab8d7809SQuentin Monnet * Generally, use **bpf_probe_read_user**\ () or 1876ab8d7809SQuentin Monnet * **bpf_probe_read_kernel**\ () instead. 1877ad4a5223SQuentin Monnet * Return 1878ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 1879ad4a5223SQuentin Monnet * 1880ad4a5223SQuentin Monnet * u64 bpf_ktime_get_ns(void) 1881ad4a5223SQuentin Monnet * Description 1882ad4a5223SQuentin Monnet * Return the time elapsed since system boot, in nanoseconds. 188371d19214SMaciej Żenczykowski * Does not include time the system was suspended. 1884ab8d7809SQuentin Monnet * See: **clock_gettime**\ (**CLOCK_MONOTONIC**) 1885ad4a5223SQuentin Monnet * Return 1886ad4a5223SQuentin Monnet * Current *ktime*. 1887ad4a5223SQuentin Monnet * 1888bdb7b79bSAndrii Nakryiko * long bpf_trace_printk(const char *fmt, u32 fmt_size, ...) 1889ad4a5223SQuentin Monnet * Description 1890ad4a5223SQuentin Monnet * This helper is a "printk()-like" facility for debugging. It 1891ad4a5223SQuentin Monnet * prints a message defined by format *fmt* (of size *fmt_size*) 189227d7fdf0SRoss Zwisler * to file *\/sys/kernel/tracing/trace* from TraceFS, if 1893ad4a5223SQuentin Monnet * available. It can take up to three additional **u64** 1894ad4a5223SQuentin Monnet * arguments (as an eBPF helpers, the total number of arguments is 1895ad4a5223SQuentin Monnet * limited to five). 1896ad4a5223SQuentin Monnet * 1897ad4a5223SQuentin Monnet * Each time the helper is called, it appends a line to the trace. 189827d7fdf0SRoss Zwisler * Lines are discarded while *\/sys/kernel/tracing/trace* is 189927d7fdf0SRoss Zwisler * open, use *\/sys/kernel/tracing/trace_pipe* to avoid this. 1900ad4a5223SQuentin Monnet * The format of the trace is customizable, and the exact output 1901ad4a5223SQuentin Monnet * one will get depends on the options set in 190227d7fdf0SRoss Zwisler * *\/sys/kernel/tracing/trace_options* (see also the 1903ad4a5223SQuentin Monnet * *README* file under the same directory). However, it usually 1904ad4a5223SQuentin Monnet * defaults to something like: 1905ad4a5223SQuentin Monnet * 1906ad4a5223SQuentin Monnet * :: 1907ad4a5223SQuentin Monnet * 1908ad4a5223SQuentin Monnet * telnet-470 [001] .N.. 419421.045894: 0x00000001: <formatted msg> 1909ad4a5223SQuentin Monnet * 1910ad4a5223SQuentin Monnet * In the above: 1911ad4a5223SQuentin Monnet * 1912ad4a5223SQuentin Monnet * * ``telnet`` is the name of the current task. 1913ad4a5223SQuentin Monnet * * ``470`` is the PID of the current task. 1914ad4a5223SQuentin Monnet * * ``001`` is the CPU number on which the task is 1915ad4a5223SQuentin Monnet * running. 1916ad4a5223SQuentin Monnet * * In ``.N..``, each character refers to a set of 1917ad4a5223SQuentin Monnet * options (whether irqs are enabled, scheduling 1918ad4a5223SQuentin Monnet * options, whether hard/softirqs are running, level of 1919ad4a5223SQuentin Monnet * preempt_disabled respectively). **N** means that 1920ad4a5223SQuentin Monnet * **TIF_NEED_RESCHED** and **PREEMPT_NEED_RESCHED** 1921ad4a5223SQuentin Monnet * are set. 1922ad4a5223SQuentin Monnet * * ``419421.045894`` is a timestamp. 1923ad4a5223SQuentin Monnet * * ``0x00000001`` is a fake value used by BPF for the 1924ad4a5223SQuentin Monnet * instruction pointer register. 1925ad4a5223SQuentin Monnet * * ``<formatted msg>`` is the message formatted with 1926ad4a5223SQuentin Monnet * *fmt*. 1927ad4a5223SQuentin Monnet * 1928ad4a5223SQuentin Monnet * The conversion specifiers supported by *fmt* are similar, but 1929ad4a5223SQuentin Monnet * more limited than for printk(). They are **%d**, **%i**, 1930ad4a5223SQuentin Monnet * **%u**, **%x**, **%ld**, **%li**, **%lu**, **%lx**, **%lld**, 1931ad4a5223SQuentin Monnet * **%lli**, **%llu**, **%llx**, **%p**, **%s**. No modifier (size 1932ad4a5223SQuentin Monnet * of field, padding with zeroes, etc.) is available, and the 1933ad4a5223SQuentin Monnet * helper will return **-EINVAL** (but print nothing) if it 1934ad4a5223SQuentin Monnet * encounters an unknown specifier. 1935ad4a5223SQuentin Monnet * 1936ad4a5223SQuentin Monnet * Also, note that **bpf_trace_printk**\ () is slow, and should 1937ad4a5223SQuentin Monnet * only be used for debugging purposes. For this reason, a notice 1938b16fc097STobias Klauser * block (spanning several lines) is printed to kernel logs and 1939ad4a5223SQuentin Monnet * states that the helper should not be used "for production use" 1940ad4a5223SQuentin Monnet * the first time this helper is used (or more precisely, when 1941ad4a5223SQuentin Monnet * **trace_printk**\ () buffers are allocated). For passing values 1942ad4a5223SQuentin Monnet * to user space, perf events should be preferred. 1943ad4a5223SQuentin Monnet * Return 1944ad4a5223SQuentin Monnet * The number of bytes written to the buffer, or a negative error 1945ad4a5223SQuentin Monnet * in case of failure. 1946ad4a5223SQuentin Monnet * 19471fdd08beSQuentin Monnet * u32 bpf_get_prandom_u32(void) 19481fdd08beSQuentin Monnet * Description 19491fdd08beSQuentin Monnet * Get a pseudo-random number. 19501fdd08beSQuentin Monnet * 19511fdd08beSQuentin Monnet * From a security point of view, this helper uses its own 19521fdd08beSQuentin Monnet * pseudo-random internal state, and cannot be used to infer the 19531fdd08beSQuentin Monnet * seed of other random functions in the kernel. However, it is 19541fdd08beSQuentin Monnet * essential to note that the generator used by the helper is not 19551fdd08beSQuentin Monnet * cryptographically secure. 19561fdd08beSQuentin Monnet * Return 19571fdd08beSQuentin Monnet * A random 32-bit unsigned value. 19581fdd08beSQuentin Monnet * 19591fdd08beSQuentin Monnet * u32 bpf_get_smp_processor_id(void) 19601fdd08beSQuentin Monnet * Description 19611fdd08beSQuentin Monnet * Get the SMP (symmetric multiprocessing) processor id. Note that 196233656275SMatteo Croce * all programs run with migration disabled, which means that the 19631fdd08beSQuentin Monnet * SMP processor id is stable during all the execution of the 19641fdd08beSQuentin Monnet * program. 19651fdd08beSQuentin Monnet * Return 19661fdd08beSQuentin Monnet * The SMP id of the processor running the program. 19671fdd08beSQuentin Monnet * 1968bdb7b79bSAndrii Nakryiko * long bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags) 1969ad4a5223SQuentin Monnet * Description 1970ad4a5223SQuentin Monnet * Store *len* bytes from address *from* into the packet 1971ad4a5223SQuentin Monnet * associated to *skb*, at *offset*. *flags* are a combination of 1972ad4a5223SQuentin Monnet * **BPF_F_RECOMPUTE_CSUM** (automatically recompute the 1973ad4a5223SQuentin Monnet * checksum for the packet after storing the bytes) and 1974ad4a5223SQuentin Monnet * **BPF_F_INVALIDATE_HASH** (set *skb*\ **->hash**, *skb*\ 1975ad4a5223SQuentin Monnet * **->swhash** and *skb*\ **->l4hash** to 0). 1976ad4a5223SQuentin Monnet * 197732e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1978ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1979ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 1980ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 1981ad4a5223SQuentin Monnet * direct packet access. 1982ad4a5223SQuentin Monnet * Return 1983ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 1984ad4a5223SQuentin Monnet * 1985bdb7b79bSAndrii Nakryiko * long bpf_l3_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 size) 1986ad4a5223SQuentin Monnet * Description 1987ad4a5223SQuentin Monnet * Recompute the layer 3 (e.g. IP) checksum for the packet 1988ad4a5223SQuentin Monnet * associated to *skb*. Computation is incremental, so the helper 1989ad4a5223SQuentin Monnet * must know the former value of the header field that was 1990ad4a5223SQuentin Monnet * modified (*from*), the new value of this field (*to*), and the 1991ad4a5223SQuentin Monnet * number of bytes (2 or 4) for this field, stored in *size*. 1992ad4a5223SQuentin Monnet * Alternatively, it is possible to store the difference between 1993ad4a5223SQuentin Monnet * the previous and the new values of the header field in *to*, by 1994ad4a5223SQuentin Monnet * setting *from* and *size* to 0. For both methods, *offset* 1995ad4a5223SQuentin Monnet * indicates the location of the IP checksum within the packet. 1996ad4a5223SQuentin Monnet * 1997ad4a5223SQuentin Monnet * This helper works in combination with **bpf_csum_diff**\ (), 1998ad4a5223SQuentin Monnet * which does not update the checksum in-place, but offers more 1999ad4a5223SQuentin Monnet * flexibility and can handle sizes larger than 2 or 4 for the 2000ad4a5223SQuentin Monnet * checksum to update. 2001ad4a5223SQuentin Monnet * 200232e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2003ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2004ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 2005ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 2006ad4a5223SQuentin Monnet * direct packet access. 2007ad4a5223SQuentin Monnet * Return 2008ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 2009ad4a5223SQuentin Monnet * 2010bdb7b79bSAndrii Nakryiko * long bpf_l4_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 flags) 2011ad4a5223SQuentin Monnet * Description 2012ad4a5223SQuentin Monnet * Recompute the layer 4 (e.g. TCP, UDP or ICMP) checksum for the 2013ad4a5223SQuentin Monnet * packet associated to *skb*. Computation is incremental, so the 2014ad4a5223SQuentin Monnet * helper must know the former value of the header field that was 2015ad4a5223SQuentin Monnet * modified (*from*), the new value of this field (*to*), and the 2016ad4a5223SQuentin Monnet * number of bytes (2 or 4) for this field, stored on the lowest 2017ad4a5223SQuentin Monnet * four bits of *flags*. Alternatively, it is possible to store 2018ad4a5223SQuentin Monnet * the difference between the previous and the new values of the 2019ad4a5223SQuentin Monnet * header field in *to*, by setting *from* and the four lowest 2020ad4a5223SQuentin Monnet * bits of *flags* to 0. For both methods, *offset* indicates the 2021ad4a5223SQuentin Monnet * location of the IP checksum within the packet. In addition to 2022ad4a5223SQuentin Monnet * the size of the field, *flags* can be added (bitwise OR) actual 2023ad4a5223SQuentin Monnet * flags. With **BPF_F_MARK_MANGLED_0**, a null checksum is left 2024ad4a5223SQuentin Monnet * untouched (unless **BPF_F_MARK_ENFORCE** is added as well), and 2025ad4a5223SQuentin Monnet * for updates resulting in a null checksum the value is set to 2026ad4a5223SQuentin Monnet * **CSUM_MANGLED_0** instead. Flag **BPF_F_PSEUDO_HDR** indicates 2027ad4a5223SQuentin Monnet * the checksum is to be computed against a pseudo-header. 2028ad4a5223SQuentin Monnet * 2029ad4a5223SQuentin Monnet * This helper works in combination with **bpf_csum_diff**\ (), 2030ad4a5223SQuentin Monnet * which does not update the checksum in-place, but offers more 2031ad4a5223SQuentin Monnet * flexibility and can handle sizes larger than 2 or 4 for the 2032ad4a5223SQuentin Monnet * checksum to update. 2033ad4a5223SQuentin Monnet * 203432e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2035ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2036ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 2037ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 2038ad4a5223SQuentin Monnet * direct packet access. 2039ad4a5223SQuentin Monnet * Return 2040ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 2041ad4a5223SQuentin Monnet * 2042bdb7b79bSAndrii Nakryiko * long bpf_tail_call(void *ctx, struct bpf_map *prog_array_map, u32 index) 2043ad4a5223SQuentin Monnet * Description 2044ad4a5223SQuentin Monnet * This special helper is used to trigger a "tail call", or in 2045ad4a5223SQuentin Monnet * other words, to jump into another eBPF program. The same stack 2046ad4a5223SQuentin Monnet * frame is used (but values on stack and in registers for the 2047ad4a5223SQuentin Monnet * caller are not accessible to the callee). This mechanism allows 2048ad4a5223SQuentin Monnet * for program chaining, either for raising the maximum number of 2049ad4a5223SQuentin Monnet * available eBPF instructions, or to execute given programs in 2050ad4a5223SQuentin Monnet * conditional blocks. For security reasons, there is an upper 2051ad4a5223SQuentin Monnet * limit to the number of successive tail calls that can be 2052ad4a5223SQuentin Monnet * performed. 2053ad4a5223SQuentin Monnet * 2054ad4a5223SQuentin Monnet * Upon call of this helper, the program attempts to jump into a 2055ad4a5223SQuentin Monnet * program referenced at index *index* in *prog_array_map*, a 2056ad4a5223SQuentin Monnet * special map of type **BPF_MAP_TYPE_PROG_ARRAY**, and passes 2057ad4a5223SQuentin Monnet * *ctx*, a pointer to the context. 2058ad4a5223SQuentin Monnet * 2059ad4a5223SQuentin Monnet * If the call succeeds, the kernel immediately runs the first 2060ad4a5223SQuentin Monnet * instruction of the new program. This is not a function call, 2061ad4a5223SQuentin Monnet * and it never returns to the previous program. If the call 2062ad4a5223SQuentin Monnet * fails, then the helper has no effect, and the caller continues 2063ad4a5223SQuentin Monnet * to run its subsequent instructions. A call can fail if the 2064ad4a5223SQuentin Monnet * destination program for the jump does not exist (i.e. *index* 2065ad4a5223SQuentin Monnet * is superior to the number of entries in *prog_array_map*), or 2066ad4a5223SQuentin Monnet * if the maximum number of tail calls has been reached for this 2067ad4a5223SQuentin Monnet * chain of programs. This limit is defined in the kernel by the 2068ad4a5223SQuentin Monnet * macro **MAX_TAIL_CALL_CNT** (not accessible to user space), 2069ebf7f6f0STiezhu Yang * which is currently set to 33. 2070ad4a5223SQuentin Monnet * Return 2071ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 2072ad4a5223SQuentin Monnet * 2073bdb7b79bSAndrii Nakryiko * long bpf_clone_redirect(struct sk_buff *skb, u32 ifindex, u64 flags) 2074ad4a5223SQuentin Monnet * Description 2075ad4a5223SQuentin Monnet * Clone and redirect the packet associated to *skb* to another 2076ad4a5223SQuentin Monnet * net device of index *ifindex*. Both ingress and egress 2077ad4a5223SQuentin Monnet * interfaces can be used for redirection. The **BPF_F_INGRESS** 2078ad4a5223SQuentin Monnet * value in *flags* is used to make the distinction (ingress path 2079ad4a5223SQuentin Monnet * is selected if the flag is present, egress path otherwise). 2080ad4a5223SQuentin Monnet * This is the only flag supported for now. 2081ad4a5223SQuentin Monnet * 2082ad4a5223SQuentin Monnet * In comparison with **bpf_redirect**\ () helper, 2083ad4a5223SQuentin Monnet * **bpf_clone_redirect**\ () has the associated cost of 2084ad4a5223SQuentin Monnet * duplicating the packet buffer, but this can be executed out of 2085ad4a5223SQuentin Monnet * the eBPF program. Conversely, **bpf_redirect**\ () is more 2086ad4a5223SQuentin Monnet * efficient, but it is handled through an action code where the 2087ad4a5223SQuentin Monnet * redirection happens only after the eBPF program has returned. 2088ad4a5223SQuentin Monnet * 208932e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2090ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2091ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 2092ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 2093ad4a5223SQuentin Monnet * direct packet access. 2094ad4a5223SQuentin Monnet * Return 20957cb779a6SStanislav Fomichev * 0 on success, or a negative error in case of failure. Positive 20967cb779a6SStanislav Fomichev * error indicates a potential drop or congestion in the target 20977cb779a6SStanislav Fomichev * device. The particular positive error codes are not defined. 2098c456dec4SQuentin Monnet * 2099c456dec4SQuentin Monnet * u64 bpf_get_current_pid_tgid(void) 2100e40fbbf0SUsama Arif * Description 2101e40fbbf0SUsama Arif * Get the current pid and tgid. 2102c456dec4SQuentin Monnet * Return 2103c456dec4SQuentin Monnet * A 64-bit integer containing the current tgid and pid, and 2104c456dec4SQuentin Monnet * created as such: 2105c456dec4SQuentin Monnet * *current_task*\ **->tgid << 32 \|** 2106c456dec4SQuentin Monnet * *current_task*\ **->pid**. 2107c456dec4SQuentin Monnet * 2108c456dec4SQuentin Monnet * u64 bpf_get_current_uid_gid(void) 2109e40fbbf0SUsama Arif * Description 2110e40fbbf0SUsama Arif * Get the current uid and gid. 2111c456dec4SQuentin Monnet * Return 2112c456dec4SQuentin Monnet * A 64-bit integer containing the current GID and UID, and 2113c456dec4SQuentin Monnet * created as such: *current_gid* **<< 32 \|** *current_uid*. 2114c456dec4SQuentin Monnet * 2115bdb7b79bSAndrii Nakryiko * long bpf_get_current_comm(void *buf, u32 size_of_buf) 2116c456dec4SQuentin Monnet * Description 2117c456dec4SQuentin Monnet * Copy the **comm** attribute of the current task into *buf* of 2118c456dec4SQuentin Monnet * *size_of_buf*. The **comm** attribute contains the name of 2119c456dec4SQuentin Monnet * the executable (excluding the path) for the current task. The 2120c456dec4SQuentin Monnet * *size_of_buf* must be strictly positive. On success, the 2121c456dec4SQuentin Monnet * helper makes sure that the *buf* is NUL-terminated. On failure, 2122c456dec4SQuentin Monnet * it is filled with zeroes. 2123c456dec4SQuentin Monnet * Return 2124c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 2125c456dec4SQuentin Monnet * 21261fdd08beSQuentin Monnet * u32 bpf_get_cgroup_classid(struct sk_buff *skb) 21271fdd08beSQuentin Monnet * Description 21281fdd08beSQuentin Monnet * Retrieve the classid for the current task, i.e. for the net_cls 21291fdd08beSQuentin Monnet * cgroup to which *skb* belongs. 21301fdd08beSQuentin Monnet * 21311fdd08beSQuentin Monnet * This helper can be used on TC egress path, but not on ingress. 21321fdd08beSQuentin Monnet * 21331fdd08beSQuentin Monnet * The net_cls cgroup provides an interface to tag network packets 21341fdd08beSQuentin Monnet * based on a user-provided identifier for all traffic coming from 21351fdd08beSQuentin Monnet * the tasks belonging to the related cgroup. See also the related 21361fdd08beSQuentin Monnet * kernel documentation, available from the Linux sources in file 2137da82c92fSMauro Carvalho Chehab * *Documentation/admin-guide/cgroup-v1/net_cls.rst*. 21381fdd08beSQuentin Monnet * 21391fdd08beSQuentin Monnet * The Linux kernel has two versions for cgroups: there are 21401fdd08beSQuentin Monnet * cgroups v1 and cgroups v2. Both are available to users, who can 21411fdd08beSQuentin Monnet * use a mixture of them, but note that the net_cls cgroup is for 21421fdd08beSQuentin Monnet * cgroup v1 only. This makes it incompatible with BPF programs 21431fdd08beSQuentin Monnet * run on cgroups, which is a cgroup-v2-only feature (a socket can 21441fdd08beSQuentin Monnet * only hold data for one version of cgroups at a time). 21451fdd08beSQuentin Monnet * 21461fdd08beSQuentin Monnet * This helper is only available is the kernel was compiled with 21471fdd08beSQuentin Monnet * the **CONFIG_CGROUP_NET_CLASSID** configuration option set to 21481fdd08beSQuentin Monnet * "**y**" or to "**m**". 21491fdd08beSQuentin Monnet * Return 21501fdd08beSQuentin Monnet * The classid, or 0 for the default unconfigured classid. 21511fdd08beSQuentin Monnet * 2152bdb7b79bSAndrii Nakryiko * long bpf_skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 2153c456dec4SQuentin Monnet * Description 2154c456dec4SQuentin Monnet * Push a *vlan_tci* (VLAN tag control information) of protocol 2155c456dec4SQuentin Monnet * *vlan_proto* to the packet associated to *skb*, then update 2156c456dec4SQuentin Monnet * the checksum. Note that if *vlan_proto* is different from 2157c456dec4SQuentin Monnet * **ETH_P_8021Q** and **ETH_P_8021AD**, it is considered to 2158c456dec4SQuentin Monnet * be **ETH_P_8021Q**. 2159c456dec4SQuentin Monnet * 216032e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2161c456dec4SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2162c456dec4SQuentin Monnet * previously done by the verifier are invalidated and must be 2163c456dec4SQuentin Monnet * performed again, if the helper is used in combination with 2164c456dec4SQuentin Monnet * direct packet access. 2165c456dec4SQuentin Monnet * Return 2166c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 2167c456dec4SQuentin Monnet * 2168bdb7b79bSAndrii Nakryiko * long bpf_skb_vlan_pop(struct sk_buff *skb) 2169c456dec4SQuentin Monnet * Description 2170c456dec4SQuentin Monnet * Pop a VLAN header from the packet associated to *skb*. 2171c456dec4SQuentin Monnet * 217232e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2173c456dec4SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2174c456dec4SQuentin Monnet * previously done by the verifier are invalidated and must be 2175c456dec4SQuentin Monnet * performed again, if the helper is used in combination with 2176c456dec4SQuentin Monnet * direct packet access. 2177c456dec4SQuentin Monnet * Return 2178c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 2179c456dec4SQuentin Monnet * 2180bdb7b79bSAndrii Nakryiko * long bpf_skb_get_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags) 2181c456dec4SQuentin Monnet * Description 2182c456dec4SQuentin Monnet * Get tunnel metadata. This helper takes a pointer *key* to an 2183c456dec4SQuentin Monnet * empty **struct bpf_tunnel_key** of **size**, that will be 2184c456dec4SQuentin Monnet * filled with tunnel metadata for the packet associated to *skb*. 2185c456dec4SQuentin Monnet * The *flags* can be set to **BPF_F_TUNINFO_IPV6**, which 2186c456dec4SQuentin Monnet * indicates that the tunnel is based on IPv6 protocol instead of 2187c456dec4SQuentin Monnet * IPv4. 2188c456dec4SQuentin Monnet * 2189c456dec4SQuentin Monnet * The **struct bpf_tunnel_key** is an object that generalizes the 2190c456dec4SQuentin Monnet * principal parameters used by various tunneling protocols into a 2191c456dec4SQuentin Monnet * single struct. This way, it can be used to easily make a 2192c456dec4SQuentin Monnet * decision based on the contents of the encapsulation header, 2193c456dec4SQuentin Monnet * "summarized" in this struct. In particular, it holds the IP 2194c456dec4SQuentin Monnet * address of the remote end (IPv4 or IPv6, depending on the case) 2195c456dec4SQuentin Monnet * in *key*\ **->remote_ipv4** or *key*\ **->remote_ipv6**. Also, 2196c456dec4SQuentin Monnet * this struct exposes the *key*\ **->tunnel_id**, which is 2197c456dec4SQuentin Monnet * generally mapped to a VNI (Virtual Network Identifier), making 2198c456dec4SQuentin Monnet * it programmable together with the **bpf_skb_set_tunnel_key**\ 2199c456dec4SQuentin Monnet * () helper. 2200c456dec4SQuentin Monnet * 2201c456dec4SQuentin Monnet * Let's imagine that the following code is part of a program 2202c456dec4SQuentin Monnet * attached to the TC ingress interface, on one end of a GRE 2203c456dec4SQuentin Monnet * tunnel, and is supposed to filter out all messages coming from 2204c456dec4SQuentin Monnet * remote ends with IPv4 address other than 10.0.0.1: 2205c456dec4SQuentin Monnet * 2206c456dec4SQuentin Monnet * :: 2207c456dec4SQuentin Monnet * 2208c456dec4SQuentin Monnet * int ret; 2209c456dec4SQuentin Monnet * struct bpf_tunnel_key key = {}; 2210c456dec4SQuentin Monnet * 2211c456dec4SQuentin Monnet * ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0); 2212c456dec4SQuentin Monnet * if (ret < 0) 2213c456dec4SQuentin Monnet * return TC_ACT_SHOT; // drop packet 2214c456dec4SQuentin Monnet * 2215c456dec4SQuentin Monnet * if (key.remote_ipv4 != 0x0a000001) 2216c456dec4SQuentin Monnet * return TC_ACT_SHOT; // drop packet 2217c456dec4SQuentin Monnet * 2218c456dec4SQuentin Monnet * return TC_ACT_OK; // accept packet 2219c456dec4SQuentin Monnet * 2220c456dec4SQuentin Monnet * This interface can also be used with all encapsulation devices 2221c456dec4SQuentin Monnet * that can operate in "collect metadata" mode: instead of having 2222c456dec4SQuentin Monnet * one network device per specific configuration, the "collect 2223c456dec4SQuentin Monnet * metadata" mode only requires a single device where the 2224c456dec4SQuentin Monnet * configuration can be extracted from this helper. 2225c456dec4SQuentin Monnet * 2226c456dec4SQuentin Monnet * This can be used together with various tunnels such as VXLan, 2227c456dec4SQuentin Monnet * Geneve, GRE or IP in IP (IPIP). 2228c456dec4SQuentin Monnet * Return 2229c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 2230c456dec4SQuentin Monnet * 2231bdb7b79bSAndrii Nakryiko * long bpf_skb_set_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags) 2232c456dec4SQuentin Monnet * Description 2233c456dec4SQuentin Monnet * Populate tunnel metadata for packet associated to *skb.* The 2234c456dec4SQuentin Monnet * tunnel metadata is set to the contents of *key*, of *size*. The 2235c456dec4SQuentin Monnet * *flags* can be set to a combination of the following values: 2236c456dec4SQuentin Monnet * 2237c456dec4SQuentin Monnet * **BPF_F_TUNINFO_IPV6** 2238c456dec4SQuentin Monnet * Indicate that the tunnel is based on IPv6 protocol 2239c456dec4SQuentin Monnet * instead of IPv4. 2240c456dec4SQuentin Monnet * **BPF_F_ZERO_CSUM_TX** 2241c456dec4SQuentin Monnet * For IPv4 packets, add a flag to tunnel metadata 2242c456dec4SQuentin Monnet * indicating that checksum computation should be skipped 2243c456dec4SQuentin Monnet * and checksum set to zeroes. 2244c456dec4SQuentin Monnet * **BPF_F_DONT_FRAGMENT** 2245c456dec4SQuentin Monnet * Add a flag to tunnel metadata indicating that the 2246c456dec4SQuentin Monnet * packet should not be fragmented. 2247c456dec4SQuentin Monnet * **BPF_F_SEQ_NUMBER** 2248c456dec4SQuentin Monnet * Add a flag to tunnel metadata indicating that a 2249c456dec4SQuentin Monnet * sequence number should be added to tunnel header before 2250c456dec4SQuentin Monnet * sending the packet. This flag was added for GRE 2251c456dec4SQuentin Monnet * encapsulation, but might be used with other protocols 2252c456dec4SQuentin Monnet * as well in the future. 2253e26aa600SChristian Ehrig * **BPF_F_NO_TUNNEL_KEY** 2254e26aa600SChristian Ehrig * Add a flag to tunnel metadata indicating that no tunnel 2255e26aa600SChristian Ehrig * key should be set in the resulting tunnel header. 2256c456dec4SQuentin Monnet * 2257c456dec4SQuentin Monnet * Here is a typical usage on the transmit path: 2258c456dec4SQuentin Monnet * 2259c456dec4SQuentin Monnet * :: 2260c456dec4SQuentin Monnet * 2261c456dec4SQuentin Monnet * struct bpf_tunnel_key key; 2262c456dec4SQuentin Monnet * populate key ... 2263c456dec4SQuentin Monnet * bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0); 2264c456dec4SQuentin Monnet * bpf_clone_redirect(skb, vxlan_dev_ifindex, 0); 2265c456dec4SQuentin Monnet * 2266c456dec4SQuentin Monnet * See also the description of the **bpf_skb_get_tunnel_key**\ () 2267c456dec4SQuentin Monnet * helper for additional information. 2268c456dec4SQuentin Monnet * Return 2269c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 2270c456dec4SQuentin Monnet * 2271c6b5fb86SQuentin Monnet * u64 bpf_perf_event_read(struct bpf_map *map, u64 flags) 2272c6b5fb86SQuentin Monnet * Description 2273c6b5fb86SQuentin Monnet * Read the value of a perf event counter. This helper relies on a 2274c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of 2275c6b5fb86SQuentin Monnet * the perf event counter is selected when *map* is updated with 2276c6b5fb86SQuentin Monnet * perf event file descriptors. The *map* is an array whose size 2277c6b5fb86SQuentin Monnet * is the number of available CPUs, and each cell contains a value 2278c6b5fb86SQuentin Monnet * relative to one CPU. The value to retrieve is indicated by 2279c6b5fb86SQuentin Monnet * *flags*, that contains the index of the CPU to look up, masked 2280c6b5fb86SQuentin Monnet * with **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to 2281c6b5fb86SQuentin Monnet * **BPF_F_CURRENT_CPU** to indicate that the value for the 2282c6b5fb86SQuentin Monnet * current CPU should be retrieved. 2283c6b5fb86SQuentin Monnet * 2284c6b5fb86SQuentin Monnet * Note that before Linux 4.13, only hardware perf event can be 2285c6b5fb86SQuentin Monnet * retrieved. 2286c6b5fb86SQuentin Monnet * 2287c6b5fb86SQuentin Monnet * Also, be aware that the newer helper 2288c6b5fb86SQuentin Monnet * **bpf_perf_event_read_value**\ () is recommended over 22893bd5a09bSQuentin Monnet * **bpf_perf_event_read**\ () in general. The latter has some ABI 2290c6b5fb86SQuentin Monnet * quirks where error and counter value are used as a return code 2291c6b5fb86SQuentin Monnet * (which is wrong to do since ranges may overlap). This issue is 22923bd5a09bSQuentin Monnet * fixed with **bpf_perf_event_read_value**\ (), which at the same 22933bd5a09bSQuentin Monnet * time provides more features over the **bpf_perf_event_read**\ 22943bd5a09bSQuentin Monnet * () interface. Please refer to the description of 2295c6b5fb86SQuentin Monnet * **bpf_perf_event_read_value**\ () for details. 2296c6b5fb86SQuentin Monnet * Return 2297c6b5fb86SQuentin Monnet * The value of the perf event counter read from the map, or a 2298c6b5fb86SQuentin Monnet * negative error code in case of failure. 2299c6b5fb86SQuentin Monnet * 2300bdb7b79bSAndrii Nakryiko * long bpf_redirect(u32 ifindex, u64 flags) 2301c456dec4SQuentin Monnet * Description 2302c456dec4SQuentin Monnet * Redirect the packet to another net device of index *ifindex*. 2303c456dec4SQuentin Monnet * This helper is somewhat similar to **bpf_clone_redirect**\ 2304c456dec4SQuentin Monnet * (), except that the packet is not cloned, which provides 2305c456dec4SQuentin Monnet * increased performance. 2306c456dec4SQuentin Monnet * 2307c456dec4SQuentin Monnet * Except for XDP, both ingress and egress interfaces can be used 2308c456dec4SQuentin Monnet * for redirection. The **BPF_F_INGRESS** value in *flags* is used 2309c456dec4SQuentin Monnet * to make the distinction (ingress path is selected if the flag 2310c456dec4SQuentin Monnet * is present, egress path otherwise). Currently, XDP only 2311c456dec4SQuentin Monnet * supports redirection to the egress interface, and accepts no 2312c456dec4SQuentin Monnet * flag at all. 2313c456dec4SQuentin Monnet * 2314f25975f4SToke Høiland-Jørgensen * The same effect can also be attained with the more generic 2315f25975f4SToke Høiland-Jørgensen * **bpf_redirect_map**\ (), which uses a BPF map to store the 2316f25975f4SToke Høiland-Jørgensen * redirect target instead of providing it directly to the helper. 2317c456dec4SQuentin Monnet * Return 2318c456dec4SQuentin Monnet * For XDP, the helper returns **XDP_REDIRECT** on success or 2319c456dec4SQuentin Monnet * **XDP_ABORTED** on error. For other program types, the values 2320c456dec4SQuentin Monnet * are **TC_ACT_REDIRECT** on success or **TC_ACT_SHOT** on 2321c456dec4SQuentin Monnet * error. 2322c456dec4SQuentin Monnet * 23231fdd08beSQuentin Monnet * u32 bpf_get_route_realm(struct sk_buff *skb) 23241fdd08beSQuentin Monnet * Description 23251fdd08beSQuentin Monnet * Retrieve the realm or the route, that is to say the 23261fdd08beSQuentin Monnet * **tclassid** field of the destination for the *skb*. The 2327b16fc097STobias Klauser * identifier retrieved is a user-provided tag, similar to the 23281fdd08beSQuentin Monnet * one used with the net_cls cgroup (see description for 23291fdd08beSQuentin Monnet * **bpf_get_cgroup_classid**\ () helper), but here this tag is 23301fdd08beSQuentin Monnet * held by a route (a destination entry), not by a task. 23311fdd08beSQuentin Monnet * 23321fdd08beSQuentin Monnet * Retrieving this identifier works with the clsact TC egress hook 23331fdd08beSQuentin Monnet * (see also **tc-bpf(8)**), or alternatively on conventional 23341fdd08beSQuentin Monnet * classful egress qdiscs, but not on TC ingress path. In case of 23351fdd08beSQuentin Monnet * clsact TC egress hook, this has the advantage that, internally, 23361fdd08beSQuentin Monnet * the destination entry has not been dropped yet in the transmit 23371fdd08beSQuentin Monnet * path. Therefore, the destination entry does not need to be 23381fdd08beSQuentin Monnet * artificially held via **netif_keep_dst**\ () for a classful 23391fdd08beSQuentin Monnet * qdisc until the *skb* is freed. 23401fdd08beSQuentin Monnet * 23411fdd08beSQuentin Monnet * This helper is available only if the kernel was compiled with 23421fdd08beSQuentin Monnet * **CONFIG_IP_ROUTE_CLASSID** configuration option. 23431fdd08beSQuentin Monnet * Return 23441fdd08beSQuentin Monnet * The realm of the route for the packet associated to *skb*, or 0 23451fdd08beSQuentin Monnet * if none was found. 23461fdd08beSQuentin Monnet * 2347bdb7b79bSAndrii Nakryiko * long bpf_perf_event_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size) 2348c456dec4SQuentin Monnet * Description 2349c456dec4SQuentin Monnet * Write raw *data* blob into a special BPF perf event held by 2350c456dec4SQuentin Monnet * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 2351c456dec4SQuentin Monnet * event must have the following attributes: **PERF_SAMPLE_RAW** 2352c456dec4SQuentin Monnet * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 2353c456dec4SQuentin Monnet * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 2354c456dec4SQuentin Monnet * 2355c456dec4SQuentin Monnet * The *flags* are used to indicate the index in *map* for which 2356c456dec4SQuentin Monnet * the value must be put, masked with **BPF_F_INDEX_MASK**. 2357c456dec4SQuentin Monnet * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 2358c456dec4SQuentin Monnet * to indicate that the index of the current CPU core should be 2359c456dec4SQuentin Monnet * used. 2360c456dec4SQuentin Monnet * 2361c456dec4SQuentin Monnet * The value to write, of *size*, is passed through eBPF stack and 2362c456dec4SQuentin Monnet * pointed by *data*. 2363c456dec4SQuentin Monnet * 2364c456dec4SQuentin Monnet * The context of the program *ctx* needs also be passed to the 2365c456dec4SQuentin Monnet * helper. 2366c456dec4SQuentin Monnet * 2367c456dec4SQuentin Monnet * On user space, a program willing to read the values needs to 2368c456dec4SQuentin Monnet * call **perf_event_open**\ () on the perf event (either for 2369c456dec4SQuentin Monnet * one or for all CPUs) and to store the file descriptor into the 2370c456dec4SQuentin Monnet * *map*. This must be done before the eBPF program can send data 2371c456dec4SQuentin Monnet * into it. An example is available in file 2372c456dec4SQuentin Monnet * *samples/bpf/trace_output_user.c* in the Linux kernel source 2373c456dec4SQuentin Monnet * tree (the eBPF program counterpart is in 2374c456dec4SQuentin Monnet * *samples/bpf/trace_output_kern.c*). 2375c456dec4SQuentin Monnet * 2376c456dec4SQuentin Monnet * **bpf_perf_event_output**\ () achieves better performance 2377c456dec4SQuentin Monnet * than **bpf_trace_printk**\ () for sharing data with user 2378c456dec4SQuentin Monnet * space, and is much better suitable for streaming data from eBPF 2379c456dec4SQuentin Monnet * programs. 2380c456dec4SQuentin Monnet * 2381c456dec4SQuentin Monnet * Note that this helper is not restricted to tracing use cases 2382c456dec4SQuentin Monnet * and can be used with programs attached to TC or XDP as well, 2383c456dec4SQuentin Monnet * where it allows for passing data to user space listeners. Data 2384c456dec4SQuentin Monnet * can be: 2385c456dec4SQuentin Monnet * 2386c456dec4SQuentin Monnet * * Only custom structs, 2387c456dec4SQuentin Monnet * * Only the packet payload, or 2388c456dec4SQuentin Monnet * * A combination of both. 2389c456dec4SQuentin Monnet * Return 2390c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 2391c456dec4SQuentin Monnet * 2392bdb7b79bSAndrii Nakryiko * long bpf_skb_load_bytes(const void *skb, u32 offset, void *to, u32 len) 23931fdd08beSQuentin Monnet * Description 23941fdd08beSQuentin Monnet * This helper was provided as an easy way to load data from a 23951fdd08beSQuentin Monnet * packet. It can be used to load *len* bytes from *offset* from 23961fdd08beSQuentin Monnet * the packet associated to *skb*, into the buffer pointed by 23971fdd08beSQuentin Monnet * *to*. 23981fdd08beSQuentin Monnet * 23991fdd08beSQuentin Monnet * Since Linux 4.7, usage of this helper has mostly been replaced 24001fdd08beSQuentin Monnet * by "direct packet access", enabling packet data to be 24011fdd08beSQuentin Monnet * manipulated with *skb*\ **->data** and *skb*\ **->data_end** 24021fdd08beSQuentin Monnet * pointing respectively to the first byte of packet data and to 24031fdd08beSQuentin Monnet * the byte after the last byte of packet data. However, it 24041fdd08beSQuentin Monnet * remains useful if one wishes to read large quantities of data 24051fdd08beSQuentin Monnet * at once from a packet into the eBPF stack. 24061fdd08beSQuentin Monnet * Return 24071fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 24081fdd08beSQuentin Monnet * 2409bdb7b79bSAndrii Nakryiko * long bpf_get_stackid(void *ctx, struct bpf_map *map, u64 flags) 2410c456dec4SQuentin Monnet * Description 2411c456dec4SQuentin Monnet * Walk a user or a kernel stack and return its id. To achieve 2412c456dec4SQuentin Monnet * this, the helper needs *ctx*, which is a pointer to the context 2413c456dec4SQuentin Monnet * on which the tracing program is executed, and a pointer to a 2414c456dec4SQuentin Monnet * *map* of type **BPF_MAP_TYPE_STACK_TRACE**. 2415c456dec4SQuentin Monnet * 2416c456dec4SQuentin Monnet * The last argument, *flags*, holds the number of stack frames to 2417c456dec4SQuentin Monnet * skip (from 0 to 255), masked with 2418c456dec4SQuentin Monnet * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 2419c456dec4SQuentin Monnet * a combination of the following flags: 2420c456dec4SQuentin Monnet * 2421c456dec4SQuentin Monnet * **BPF_F_USER_STACK** 2422c456dec4SQuentin Monnet * Collect a user space stack instead of a kernel stack. 2423c456dec4SQuentin Monnet * **BPF_F_FAST_STACK_CMP** 2424c456dec4SQuentin Monnet * Compare stacks by hash only. 2425c456dec4SQuentin Monnet * **BPF_F_REUSE_STACKID** 2426c456dec4SQuentin Monnet * If two different stacks hash into the same *stackid*, 2427c456dec4SQuentin Monnet * discard the old one. 2428c456dec4SQuentin Monnet * 2429c456dec4SQuentin Monnet * The stack id retrieved is a 32 bit long integer handle which 2430c456dec4SQuentin Monnet * can be further combined with other data (including other stack 2431c456dec4SQuentin Monnet * ids) and used as a key into maps. This can be useful for 2432c456dec4SQuentin Monnet * generating a variety of graphs (such as flame graphs or off-cpu 2433c456dec4SQuentin Monnet * graphs). 2434c456dec4SQuentin Monnet * 2435c456dec4SQuentin Monnet * For walking a stack, this helper is an improvement over 2436c456dec4SQuentin Monnet * **bpf_probe_read**\ (), which can be used with unrolled loops 2437c456dec4SQuentin Monnet * but is not efficient and consumes a lot of eBPF instructions. 2438c456dec4SQuentin Monnet * Instead, **bpf_get_stackid**\ () can collect up to 2439c456dec4SQuentin Monnet * **PERF_MAX_STACK_DEPTH** both kernel and user frames. Note that 2440c456dec4SQuentin Monnet * this limit can be controlled with the **sysctl** program, and 2441c456dec4SQuentin Monnet * that it should be manually increased in order to profile long 2442c456dec4SQuentin Monnet * user stacks (such as stacks for Java programs). To do so, use: 2443c456dec4SQuentin Monnet * 2444c456dec4SQuentin Monnet * :: 2445c456dec4SQuentin Monnet * 2446c456dec4SQuentin Monnet * # sysctl kernel.perf_event_max_stack=<new value> 2447c456dec4SQuentin Monnet * Return 2448c456dec4SQuentin Monnet * The positive or null stack id on success, or a negative error 2449c456dec4SQuentin Monnet * in case of failure. 2450c456dec4SQuentin Monnet * 24511fdd08beSQuentin Monnet * s64 bpf_csum_diff(__be32 *from, u32 from_size, __be32 *to, u32 to_size, __wsum seed) 24521fdd08beSQuentin Monnet * Description 24531fdd08beSQuentin Monnet * Compute a checksum difference, from the raw buffer pointed by 24541fdd08beSQuentin Monnet * *from*, of length *from_size* (that must be a multiple of 4), 24551fdd08beSQuentin Monnet * towards the raw buffer pointed by *to*, of size *to_size* 24561fdd08beSQuentin Monnet * (same remark). An optional *seed* can be added to the value 24571fdd08beSQuentin Monnet * (this can be cascaded, the seed may come from a previous call 24581fdd08beSQuentin Monnet * to the helper). 24591fdd08beSQuentin Monnet * 24601fdd08beSQuentin Monnet * This is flexible enough to be used in several ways: 24611fdd08beSQuentin Monnet * 24621fdd08beSQuentin Monnet * * With *from_size* == 0, *to_size* > 0 and *seed* set to 24631fdd08beSQuentin Monnet * checksum, it can be used when pushing new data. 24641fdd08beSQuentin Monnet * * With *from_size* > 0, *to_size* == 0 and *seed* set to 24651fdd08beSQuentin Monnet * checksum, it can be used when removing data from a packet. 24661fdd08beSQuentin Monnet * * With *from_size* > 0, *to_size* > 0 and *seed* set to 0, it 24671fdd08beSQuentin Monnet * can be used to compute a diff. Note that *from_size* and 24681fdd08beSQuentin Monnet * *to_size* do not need to be equal. 24691fdd08beSQuentin Monnet * 24701fdd08beSQuentin Monnet * This helper can be used in combination with 24711fdd08beSQuentin Monnet * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ (), to 24721fdd08beSQuentin Monnet * which one can feed in the difference computed with 24731fdd08beSQuentin Monnet * **bpf_csum_diff**\ (). 24741fdd08beSQuentin Monnet * Return 24751fdd08beSQuentin Monnet * The checksum result, or a negative error code in case of 24761fdd08beSQuentin Monnet * failure. 24771fdd08beSQuentin Monnet * 2478bdb7b79bSAndrii Nakryiko * long bpf_skb_get_tunnel_opt(struct sk_buff *skb, void *opt, u32 size) 24791fdd08beSQuentin Monnet * Description 24801fdd08beSQuentin Monnet * Retrieve tunnel options metadata for the packet associated to 24811fdd08beSQuentin Monnet * *skb*, and store the raw tunnel option data to the buffer *opt* 24821fdd08beSQuentin Monnet * of *size*. 24831fdd08beSQuentin Monnet * 24841fdd08beSQuentin Monnet * This helper can be used with encapsulation devices that can 24851fdd08beSQuentin Monnet * operate in "collect metadata" mode (please refer to the related 24861fdd08beSQuentin Monnet * note in the description of **bpf_skb_get_tunnel_key**\ () for 24871fdd08beSQuentin Monnet * more details). A particular example where this can be used is 24881fdd08beSQuentin Monnet * in combination with the Geneve encapsulation protocol, where it 24891fdd08beSQuentin Monnet * allows for pushing (with **bpf_skb_get_tunnel_opt**\ () helper) 24901fdd08beSQuentin Monnet * and retrieving arbitrary TLVs (Type-Length-Value headers) from 24911fdd08beSQuentin Monnet * the eBPF program. This allows for full customization of these 24921fdd08beSQuentin Monnet * headers. 24931fdd08beSQuentin Monnet * Return 24941fdd08beSQuentin Monnet * The size of the option data retrieved. 24951fdd08beSQuentin Monnet * 2496bdb7b79bSAndrii Nakryiko * long bpf_skb_set_tunnel_opt(struct sk_buff *skb, void *opt, u32 size) 24971fdd08beSQuentin Monnet * Description 24981fdd08beSQuentin Monnet * Set tunnel options metadata for the packet associated to *skb* 24991fdd08beSQuentin Monnet * to the option data contained in the raw buffer *opt* of *size*. 25001fdd08beSQuentin Monnet * 25011fdd08beSQuentin Monnet * See also the description of the **bpf_skb_get_tunnel_opt**\ () 25021fdd08beSQuentin Monnet * helper for additional information. 25031fdd08beSQuentin Monnet * Return 25041fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 25051fdd08beSQuentin Monnet * 2506bdb7b79bSAndrii Nakryiko * long bpf_skb_change_proto(struct sk_buff *skb, __be16 proto, u64 flags) 25071fdd08beSQuentin Monnet * Description 25081fdd08beSQuentin Monnet * Change the protocol of the *skb* to *proto*. Currently 25091fdd08beSQuentin Monnet * supported are transition from IPv4 to IPv6, and from IPv6 to 25101fdd08beSQuentin Monnet * IPv4. The helper takes care of the groundwork for the 25111fdd08beSQuentin Monnet * transition, including resizing the socket buffer. The eBPF 25121fdd08beSQuentin Monnet * program is expected to fill the new headers, if any, via 25131fdd08beSQuentin Monnet * **skb_store_bytes**\ () and to recompute the checksums with 25141fdd08beSQuentin Monnet * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ 25151fdd08beSQuentin Monnet * (). The main case for this helper is to perform NAT64 25161fdd08beSQuentin Monnet * operations out of an eBPF program. 25171fdd08beSQuentin Monnet * 25181fdd08beSQuentin Monnet * Internally, the GSO type is marked as dodgy so that headers are 25191fdd08beSQuentin Monnet * checked and segments are recalculated by the GSO/GRO engine. 25201fdd08beSQuentin Monnet * The size for GSO target is adapted as well. 25211fdd08beSQuentin Monnet * 25221fdd08beSQuentin Monnet * All values for *flags* are reserved for future usage, and must 25231fdd08beSQuentin Monnet * be left at zero. 25241fdd08beSQuentin Monnet * 252532e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 25261fdd08beSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 25271fdd08beSQuentin Monnet * previously done by the verifier are invalidated and must be 25281fdd08beSQuentin Monnet * performed again, if the helper is used in combination with 25291fdd08beSQuentin Monnet * direct packet access. 25301fdd08beSQuentin Monnet * Return 25311fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 25321fdd08beSQuentin Monnet * 2533bdb7b79bSAndrii Nakryiko * long bpf_skb_change_type(struct sk_buff *skb, u32 type) 25341fdd08beSQuentin Monnet * Description 25351fdd08beSQuentin Monnet * Change the packet type for the packet associated to *skb*. This 25361fdd08beSQuentin Monnet * comes down to setting *skb*\ **->pkt_type** to *type*, except 25371fdd08beSQuentin Monnet * the eBPF program does not have a write access to *skb*\ 25381fdd08beSQuentin Monnet * **->pkt_type** beside this helper. Using a helper here allows 25391fdd08beSQuentin Monnet * for graceful handling of errors. 25401fdd08beSQuentin Monnet * 25411fdd08beSQuentin Monnet * The major use case is to change incoming *skb*s to 25421fdd08beSQuentin Monnet * **PACKET_HOST** in a programmatic way instead of having to 25431fdd08beSQuentin Monnet * recirculate via **redirect**\ (..., **BPF_F_INGRESS**), for 25441fdd08beSQuentin Monnet * example. 25451fdd08beSQuentin Monnet * 25461fdd08beSQuentin Monnet * Note that *type* only allows certain values. At this time, they 25471fdd08beSQuentin Monnet * are: 25481fdd08beSQuentin Monnet * 25491fdd08beSQuentin Monnet * **PACKET_HOST** 25501fdd08beSQuentin Monnet * Packet is for us. 25511fdd08beSQuentin Monnet * **PACKET_BROADCAST** 25521fdd08beSQuentin Monnet * Send packet to all. 25531fdd08beSQuentin Monnet * **PACKET_MULTICAST** 25541fdd08beSQuentin Monnet * Send packet to group. 25551fdd08beSQuentin Monnet * **PACKET_OTHERHOST** 25561fdd08beSQuentin Monnet * Send packet to someone else. 25571fdd08beSQuentin Monnet * Return 25581fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 25591fdd08beSQuentin Monnet * 2560bdb7b79bSAndrii Nakryiko * long bpf_skb_under_cgroup(struct sk_buff *skb, struct bpf_map *map, u32 index) 2561c6b5fb86SQuentin Monnet * Description 2562c6b5fb86SQuentin Monnet * Check whether *skb* is a descendant of the cgroup2 held by 2563c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. 2564c6b5fb86SQuentin Monnet * Return 2565c6b5fb86SQuentin Monnet * The return value depends on the result of the test, and can be: 2566c6b5fb86SQuentin Monnet * 2567c6b5fb86SQuentin Monnet * * 0, if the *skb* failed the cgroup2 descendant test. 2568c6b5fb86SQuentin Monnet * * 1, if the *skb* succeeded the cgroup2 descendant test. 2569c6b5fb86SQuentin Monnet * * A negative error code, if an error occurred. 2570c6b5fb86SQuentin Monnet * 2571fa15601aSQuentin Monnet * u32 bpf_get_hash_recalc(struct sk_buff *skb) 2572fa15601aSQuentin Monnet * Description 2573fa15601aSQuentin Monnet * Retrieve the hash of the packet, *skb*\ **->hash**. If it is 2574fa15601aSQuentin Monnet * not set, in particular if the hash was cleared due to mangling, 2575fa15601aSQuentin Monnet * recompute this hash. Later accesses to the hash can be done 2576fa15601aSQuentin Monnet * directly with *skb*\ **->hash**. 2577fa15601aSQuentin Monnet * 2578fa15601aSQuentin Monnet * Calling **bpf_set_hash_invalid**\ (), changing a packet 2579fa15601aSQuentin Monnet * prototype with **bpf_skb_change_proto**\ (), or calling 2580fa15601aSQuentin Monnet * **bpf_skb_store_bytes**\ () with the 2581fa15601aSQuentin Monnet * **BPF_F_INVALIDATE_HASH** are actions susceptible to clear 2582fa15601aSQuentin Monnet * the hash and to trigger a new computation for the next call to 2583fa15601aSQuentin Monnet * **bpf_get_hash_recalc**\ (). 2584fa15601aSQuentin Monnet * Return 2585fa15601aSQuentin Monnet * The 32-bit hash. 2586fa15601aSQuentin Monnet * 2587c456dec4SQuentin Monnet * u64 bpf_get_current_task(void) 2588e40fbbf0SUsama Arif * Description 2589e40fbbf0SUsama Arif * Get the current task. 2590c456dec4SQuentin Monnet * Return 2591c456dec4SQuentin Monnet * A pointer to the current task struct. 2592fa15601aSQuentin Monnet * 2593bdb7b79bSAndrii Nakryiko * long bpf_probe_write_user(void *dst, const void *src, u32 len) 2594c6b5fb86SQuentin Monnet * Description 2595c6b5fb86SQuentin Monnet * Attempt in a safe way to write *len* bytes from the buffer 2596c6b5fb86SQuentin Monnet * *src* to *dst* in memory. It only works for threads that are in 2597c6b5fb86SQuentin Monnet * user context, and *dst* must be a valid user space address. 2598c6b5fb86SQuentin Monnet * 2599c6b5fb86SQuentin Monnet * This helper should not be used to implement any kind of 2600c6b5fb86SQuentin Monnet * security mechanism because of TOC-TOU attacks, but rather to 2601c6b5fb86SQuentin Monnet * debug, divert, and manipulate execution of semi-cooperative 2602c6b5fb86SQuentin Monnet * processes. 2603c6b5fb86SQuentin Monnet * 2604c6b5fb86SQuentin Monnet * Keep in mind that this feature is meant for experiments, and it 2605c6b5fb86SQuentin Monnet * has a risk of crashing the system and running programs. 2606c6b5fb86SQuentin Monnet * Therefore, when an eBPF program using this helper is attached, 2607c6b5fb86SQuentin Monnet * a warning including PID and process name is printed to kernel 2608c6b5fb86SQuentin Monnet * logs. 2609c6b5fb86SQuentin Monnet * Return 2610c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 2611c6b5fb86SQuentin Monnet * 2612bdb7b79bSAndrii Nakryiko * long bpf_current_task_under_cgroup(struct bpf_map *map, u32 index) 2613c6b5fb86SQuentin Monnet * Description 2614c6b5fb86SQuentin Monnet * Check whether the probe is being run is the context of a given 2615c6b5fb86SQuentin Monnet * subset of the cgroup2 hierarchy. The cgroup2 to test is held by 2616c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. 2617c6b5fb86SQuentin Monnet * Return 2618c6b5fb86SQuentin Monnet * The return value depends on the result of the test, and can be: 2619c6b5fb86SQuentin Monnet * 262058617014SHengqi Chen * * 1, if current task belongs to the cgroup2. 262158617014SHengqi Chen * * 0, if current task does not belong to the cgroup2. 2622c6b5fb86SQuentin Monnet * * A negative error code, if an error occurred. 2623c6b5fb86SQuentin Monnet * 2624bdb7b79bSAndrii Nakryiko * long bpf_skb_change_tail(struct sk_buff *skb, u32 len, u64 flags) 2625fa15601aSQuentin Monnet * Description 2626fa15601aSQuentin Monnet * Resize (trim or grow) the packet associated to *skb* to the 2627fa15601aSQuentin Monnet * new *len*. The *flags* are reserved for future usage, and must 2628fa15601aSQuentin Monnet * be left at zero. 2629fa15601aSQuentin Monnet * 2630fa15601aSQuentin Monnet * The basic idea is that the helper performs the needed work to 2631fa15601aSQuentin Monnet * change the size of the packet, then the eBPF program rewrites 2632fa15601aSQuentin Monnet * the rest via helpers like **bpf_skb_store_bytes**\ (), 2633fa15601aSQuentin Monnet * **bpf_l3_csum_replace**\ (), **bpf_l3_csum_replace**\ () 2634fa15601aSQuentin Monnet * and others. This helper is a slow path utility intended for 2635fa15601aSQuentin Monnet * replies with control messages. And because it is targeted for 2636fa15601aSQuentin Monnet * slow path, the helper itself can afford to be slow: it 2637fa15601aSQuentin Monnet * implicitly linearizes, unclones and drops offloads from the 2638fa15601aSQuentin Monnet * *skb*. 2639fa15601aSQuentin Monnet * 264032e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2641fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2642fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 2643fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 2644fa15601aSQuentin Monnet * direct packet access. 2645fa15601aSQuentin Monnet * Return 2646fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 2647fa15601aSQuentin Monnet * 2648bdb7b79bSAndrii Nakryiko * long bpf_skb_pull_data(struct sk_buff *skb, u32 len) 2649fa15601aSQuentin Monnet * Description 2650fa15601aSQuentin Monnet * Pull in non-linear data in case the *skb* is non-linear and not 2651fa15601aSQuentin Monnet * all of *len* are part of the linear section. Make *len* bytes 2652fa15601aSQuentin Monnet * from *skb* readable and writable. If a zero value is passed for 2653bdb2bc75SJoanne Koong * *len*, then all bytes in the linear part of *skb* will be made 2654bdb2bc75SJoanne Koong * readable and writable. 2655fa15601aSQuentin Monnet * 2656fa15601aSQuentin Monnet * This helper is only needed for reading and writing with direct 2657fa15601aSQuentin Monnet * packet access. 2658fa15601aSQuentin Monnet * 2659fa15601aSQuentin Monnet * For direct packet access, testing that offsets to access 2660fa15601aSQuentin Monnet * are within packet boundaries (test on *skb*\ **->data_end**) is 2661fa15601aSQuentin Monnet * susceptible to fail if offsets are invalid, or if the requested 2662fa15601aSQuentin Monnet * data is in non-linear parts of the *skb*. On failure the 2663fa15601aSQuentin Monnet * program can just bail out, or in the case of a non-linear 2664fa15601aSQuentin Monnet * buffer, use a helper to make the data available. The 2665fa15601aSQuentin Monnet * **bpf_skb_load_bytes**\ () helper is a first solution to access 2666fa15601aSQuentin Monnet * the data. Another one consists in using **bpf_skb_pull_data** 2667fa15601aSQuentin Monnet * to pull in once the non-linear parts, then retesting and 2668fa15601aSQuentin Monnet * eventually access the data. 2669fa15601aSQuentin Monnet * 2670fa15601aSQuentin Monnet * At the same time, this also makes sure the *skb* is uncloned, 2671fa15601aSQuentin Monnet * which is a necessary condition for direct write. As this needs 2672fa15601aSQuentin Monnet * to be an invariant for the write part only, the verifier 2673fa15601aSQuentin Monnet * detects writes and adds a prologue that is calling 2674fa15601aSQuentin Monnet * **bpf_skb_pull_data()** to effectively unclone the *skb* from 2675fa15601aSQuentin Monnet * the very beginning in case it is indeed cloned. 2676fa15601aSQuentin Monnet * 267732e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2678fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2679fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 2680fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 2681fa15601aSQuentin Monnet * direct packet access. 2682fa15601aSQuentin Monnet * Return 2683fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 2684fa15601aSQuentin Monnet * 2685fa15601aSQuentin Monnet * s64 bpf_csum_update(struct sk_buff *skb, __wsum csum) 2686fa15601aSQuentin Monnet * Description 2687fa15601aSQuentin Monnet * Add the checksum *csum* into *skb*\ **->csum** in case the 2688fa15601aSQuentin Monnet * driver has supplied a checksum for the entire packet into that 2689fa15601aSQuentin Monnet * field. Return an error otherwise. This helper is intended to be 2690fa15601aSQuentin Monnet * used in combination with **bpf_csum_diff**\ (), in particular 2691fa15601aSQuentin Monnet * when the checksum needs to be updated after data has been 2692fa15601aSQuentin Monnet * written into the packet through direct packet access. 2693fa15601aSQuentin Monnet * Return 2694fa15601aSQuentin Monnet * The checksum on success, or a negative error code in case of 2695fa15601aSQuentin Monnet * failure. 2696fa15601aSQuentin Monnet * 2697fa15601aSQuentin Monnet * void bpf_set_hash_invalid(struct sk_buff *skb) 2698fa15601aSQuentin Monnet * Description 2699fa15601aSQuentin Monnet * Invalidate the current *skb*\ **->hash**. It can be used after 2700fa15601aSQuentin Monnet * mangling on headers through direct packet access, in order to 2701fa15601aSQuentin Monnet * indicate that the hash is outdated and to trigger a 2702fa15601aSQuentin Monnet * recalculation the next time the kernel tries to access this 2703fa15601aSQuentin Monnet * hash or when the **bpf_get_hash_recalc**\ () helper is called. 2704e40fbbf0SUsama Arif * Return 2705e40fbbf0SUsama Arif * void. 2706fa15601aSQuentin Monnet * 2707bdb7b79bSAndrii Nakryiko * long bpf_get_numa_node_id(void) 2708fa15601aSQuentin Monnet * Description 2709fa15601aSQuentin Monnet * Return the id of the current NUMA node. The primary use case 2710fa15601aSQuentin Monnet * for this helper is the selection of sockets for the local NUMA 2711fa15601aSQuentin Monnet * node, when the program is attached to sockets using the 2712fa15601aSQuentin Monnet * **SO_ATTACH_REUSEPORT_EBPF** option (see also **socket(7)**), 2713fa15601aSQuentin Monnet * but the helper is also available to other eBPF program types, 2714fa15601aSQuentin Monnet * similarly to **bpf_get_smp_processor_id**\ (). 2715fa15601aSQuentin Monnet * Return 2716fa15601aSQuentin Monnet * The id of current NUMA node. 2717fa15601aSQuentin Monnet * 2718bdb7b79bSAndrii Nakryiko * long bpf_skb_change_head(struct sk_buff *skb, u32 len, u64 flags) 2719c6b5fb86SQuentin Monnet * Description 2720c6b5fb86SQuentin Monnet * Grows headroom of packet associated to *skb* and adjusts the 2721c6b5fb86SQuentin Monnet * offset of the MAC header accordingly, adding *len* bytes of 2722c6b5fb86SQuentin Monnet * space. It automatically extends and reallocates memory as 2723c6b5fb86SQuentin Monnet * required. 2724c6b5fb86SQuentin Monnet * 2725c6b5fb86SQuentin Monnet * This helper can be used on a layer 3 *skb* to push a MAC header 2726c6b5fb86SQuentin Monnet * for redirection into a layer 2 device. 2727c6b5fb86SQuentin Monnet * 2728c6b5fb86SQuentin Monnet * All values for *flags* are reserved for future usage, and must 2729c6b5fb86SQuentin Monnet * be left at zero. 2730c6b5fb86SQuentin Monnet * 273132e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2732c6b5fb86SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2733c6b5fb86SQuentin Monnet * previously done by the verifier are invalidated and must be 2734c6b5fb86SQuentin Monnet * performed again, if the helper is used in combination with 2735c6b5fb86SQuentin Monnet * direct packet access. 2736c6b5fb86SQuentin Monnet * Return 2737c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 2738c6b5fb86SQuentin Monnet * 2739bdb7b79bSAndrii Nakryiko * long bpf_xdp_adjust_head(struct xdp_buff *xdp_md, int delta) 2740c6b5fb86SQuentin Monnet * Description 2741c6b5fb86SQuentin Monnet * Adjust (move) *xdp_md*\ **->data** by *delta* bytes. Note that 2742c6b5fb86SQuentin Monnet * it is possible to use a negative value for *delta*. This helper 2743c6b5fb86SQuentin Monnet * can be used to prepare the packet for pushing or popping 2744c6b5fb86SQuentin Monnet * headers. 2745c6b5fb86SQuentin Monnet * 274632e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2747c6b5fb86SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2748c6b5fb86SQuentin Monnet * previously done by the verifier are invalidated and must be 2749c6b5fb86SQuentin Monnet * performed again, if the helper is used in combination with 2750c6b5fb86SQuentin Monnet * direct packet access. 2751c6b5fb86SQuentin Monnet * Return 2752c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 2753c6b5fb86SQuentin Monnet * 2754bdb7b79bSAndrii Nakryiko * long bpf_probe_read_str(void *dst, u32 size, const void *unsafe_ptr) 2755c6b5fb86SQuentin Monnet * Description 27566ae08ae3SDaniel Borkmann * Copy a NUL terminated string from an unsafe kernel address 2757ab8d7809SQuentin Monnet * *unsafe_ptr* to *dst*. See **bpf_probe_read_kernel_str**\ () for 27586ae08ae3SDaniel Borkmann * more details. 2759c6b5fb86SQuentin Monnet * 2760ab8d7809SQuentin Monnet * Generally, use **bpf_probe_read_user_str**\ () or 2761ab8d7809SQuentin Monnet * **bpf_probe_read_kernel_str**\ () instead. 2762c6b5fb86SQuentin Monnet * Return 2763c6b5fb86SQuentin Monnet * On success, the strictly positive length of the string, 2764c6b5fb86SQuentin Monnet * including the trailing NUL character. On error, a negative 2765c6b5fb86SQuentin Monnet * value. 2766c6b5fb86SQuentin Monnet * 2767c6b5fb86SQuentin Monnet * u64 bpf_get_socket_cookie(struct sk_buff *skb) 2768c6b5fb86SQuentin Monnet * Description 2769c6b5fb86SQuentin Monnet * If the **struct sk_buff** pointed by *skb* has a known socket, 2770c6b5fb86SQuentin Monnet * retrieve the cookie (generated by the kernel) of this socket. 2771c6b5fb86SQuentin Monnet * If no cookie has been set yet, generate a new cookie. Once 2772c6b5fb86SQuentin Monnet * generated, the socket cookie remains stable for the life of the 2773c6b5fb86SQuentin Monnet * socket. This helper can be useful for monitoring per socket 2774cd48bddaSDaniel Borkmann * networking traffic statistics as it provides a global socket 2775cd48bddaSDaniel Borkmann * identifier that can be assumed unique. 2776c6b5fb86SQuentin Monnet * Return 277707881ccbSFlorent Revest * A 8-byte long unique number on success, or 0 if the socket 277807881ccbSFlorent Revest * field is missing inside *skb*. 2779c6b5fb86SQuentin Monnet * 2780d692f113SAndrey Ignatov * u64 bpf_get_socket_cookie(struct bpf_sock_addr *ctx) 2781d692f113SAndrey Ignatov * Description 2782d692f113SAndrey Ignatov * Equivalent to bpf_get_socket_cookie() helper that accepts 278362369db2SQuentin Monnet * *skb*, but gets socket from **struct bpf_sock_addr** context. 2784d692f113SAndrey Ignatov * Return 278507881ccbSFlorent Revest * A 8-byte long unique number. 2786d692f113SAndrey Ignatov * 2787d692f113SAndrey Ignatov * u64 bpf_get_socket_cookie(struct bpf_sock_ops *ctx) 2788d692f113SAndrey Ignatov * Description 2789ab8d7809SQuentin Monnet * Equivalent to **bpf_get_socket_cookie**\ () helper that accepts 279062369db2SQuentin Monnet * *skb*, but gets socket from **struct bpf_sock_ops** context. 2791d692f113SAndrey Ignatov * Return 279207881ccbSFlorent Revest * A 8-byte long unique number. 2793d692f113SAndrey Ignatov * 2794c5dbb89fSFlorent Revest * u64 bpf_get_socket_cookie(struct sock *sk) 2795c5dbb89fSFlorent Revest * Description 2796c5dbb89fSFlorent Revest * Equivalent to **bpf_get_socket_cookie**\ () helper that accepts 2797c5dbb89fSFlorent Revest * *sk*, but gets socket from a BTF **struct sock**. This helper 2798c5dbb89fSFlorent Revest * also works for sleepable programs. 2799c5dbb89fSFlorent Revest * Return 2800c5dbb89fSFlorent Revest * A 8-byte long unique number or 0 if *sk* is NULL. 2801c5dbb89fSFlorent Revest * 2802c6b5fb86SQuentin Monnet * u32 bpf_get_socket_uid(struct sk_buff *skb) 2803e40fbbf0SUsama Arif * Description 2804e40fbbf0SUsama Arif * Get the owner UID of the socked associated to *skb*. 2805c6b5fb86SQuentin Monnet * Return 2806c6b5fb86SQuentin Monnet * The owner UID of the socket associated to *skb*. If the socket 2807c6b5fb86SQuentin Monnet * is **NULL**, or if it is not a full socket (i.e. if it is a 2808c6b5fb86SQuentin Monnet * time-wait or a request socket instead), **overflowuid** value 2809c6b5fb86SQuentin Monnet * is returned (note that **overflowuid** might also be the actual 2810c6b5fb86SQuentin Monnet * UID value for the socket). 2811c6b5fb86SQuentin Monnet * 2812bdb7b79bSAndrii Nakryiko * long bpf_set_hash(struct sk_buff *skb, u32 hash) 2813fa15601aSQuentin Monnet * Description 2814fa15601aSQuentin Monnet * Set the full hash for *skb* (set the field *skb*\ **->hash**) 2815fa15601aSQuentin Monnet * to value *hash*. 2816fa15601aSQuentin Monnet * Return 2817fa15601aSQuentin Monnet * 0 2818fa15601aSQuentin Monnet * 2819bdb7b79bSAndrii Nakryiko * long bpf_setsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen) 28207aa79a86SQuentin Monnet * Description 28217aa79a86SQuentin Monnet * Emulate a call to **setsockopt()** on the socket associated to 28227aa79a86SQuentin Monnet * *bpf_socket*, which must be a full socket. The *level* at 28237aa79a86SQuentin Monnet * which the option resides and the name *optname* of the option 28247aa79a86SQuentin Monnet * must be specified, see **setsockopt(2)** for more information. 28257aa79a86SQuentin Monnet * The option value of length *optlen* is pointed by *optval*. 28267aa79a86SQuentin Monnet * 2827beecf11bSStanislav Fomichev * *bpf_socket* should be one of the following: 2828ab8d7809SQuentin Monnet * 2829beecf11bSStanislav Fomichev * * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**. 2830859051ddSDaan De Meyer * * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT**, 2831859051ddSDaan De Meyer * **BPF_CGROUP_INET6_CONNECT** and **BPF_CGROUP_UNIX_CONNECT**. 2832beecf11bSStanislav Fomichev * 28337aa79a86SQuentin Monnet * This helper actually implements a subset of **setsockopt()**. 28347aa79a86SQuentin Monnet * It supports the following *level*\ s: 28357aa79a86SQuentin Monnet * 28367aa79a86SQuentin Monnet * * **SOL_SOCKET**, which supports the following *optname*\ s: 28377aa79a86SQuentin Monnet * **SO_RCVBUF**, **SO_SNDBUF**, **SO_MAX_PACING_RATE**, 2838f9bcf968SDmitry Yakunin * **SO_PRIORITY**, **SO_RCVLOWAT**, **SO_MARK**, 283972b43bdeSJi Rongfeng * **SO_BINDTODEVICE**, **SO_KEEPALIVE**, **SO_REUSEADDR**, 284072b43bdeSJi Rongfeng * **SO_REUSEPORT**, **SO_BINDTOIFINDEX**, **SO_TXREHASH**. 28417aa79a86SQuentin Monnet * * **IPPROTO_TCP**, which supports the following *optname*\ s: 28427aa79a86SQuentin Monnet * **TCP_CONGESTION**, **TCP_BPF_IW**, 2843f9bcf968SDmitry Yakunin * **TCP_BPF_SNDCWND_CLAMP**, **TCP_SAVE_SYN**, 2844f9bcf968SDmitry Yakunin * **TCP_KEEPIDLE**, **TCP_KEEPINTVL**, **TCP_KEEPCNT**, 284572b43bdeSJi Rongfeng * **TCP_SYNCNT**, **TCP_USER_TIMEOUT**, **TCP_NOTSENT_LOWAT**, 284672b43bdeSJi Rongfeng * **TCP_NODELAY**, **TCP_MAXSEG**, **TCP_WINDOW_CLAMP**, 284772b43bdeSJi Rongfeng * **TCP_THIN_LINEAR_TIMEOUTS**, **TCP_BPF_DELACK_MAX**, 284872b43bdeSJi Rongfeng * **TCP_BPF_RTO_MIN**. 28497aa79a86SQuentin Monnet * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. 285072b43bdeSJi Rongfeng * * **IPPROTO_IPV6**, which supports the following *optname*\ s: 285172b43bdeSJi Rongfeng * **IPV6_TCLASS**, **IPV6_AUTOFLOWLABEL**. 28527aa79a86SQuentin Monnet * Return 28537aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 28547aa79a86SQuentin Monnet * 2855bdb7b79bSAndrii Nakryiko * long bpf_skb_adjust_room(struct sk_buff *skb, s32 len_diff, u32 mode, u64 flags) 2856fa15601aSQuentin Monnet * Description 2857fa15601aSQuentin Monnet * Grow or shrink the room for data in the packet associated to 2858fa15601aSQuentin Monnet * *skb* by *len_diff*, and according to the selected *mode*. 2859fa15601aSQuentin Monnet * 2860836e66c2SDaniel Borkmann * By default, the helper will reset any offloaded checksum 2861836e66c2SDaniel Borkmann * indicator of the skb to CHECKSUM_NONE. This can be avoided 2862836e66c2SDaniel Borkmann * by the following flag: 2863836e66c2SDaniel Borkmann * 2864836e66c2SDaniel Borkmann * * **BPF_F_ADJ_ROOM_NO_CSUM_RESET**: Do not reset offloaded 2865836e66c2SDaniel Borkmann * checksum data of the skb to CHECKSUM_NONE. 2866836e66c2SDaniel Borkmann * 286714aa3192SWillem de Bruijn * There are two supported modes at this time: 286814aa3192SWillem de Bruijn * 286914aa3192SWillem de Bruijn * * **BPF_ADJ_ROOM_MAC**: Adjust room at the mac layer 28704961d077SQuentin Monnet * (room space is added or removed between the layer 2 and 28714961d077SQuentin Monnet * layer 3 headers). 2872fa15601aSQuentin Monnet * 2873fa15601aSQuentin Monnet * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer 28744961d077SQuentin Monnet * (room space is added or removed between the layer 3 and 28754961d077SQuentin Monnet * layer 4 headers). 2876fa15601aSQuentin Monnet * 2877868d5235SWillem de Bruijn * The following flags are supported at this time: 28782278f6ccSWillem de Bruijn * 28792278f6ccSWillem de Bruijn * * **BPF_F_ADJ_ROOM_FIXED_GSO**: Do not adjust gso_size. 28802278f6ccSWillem de Bruijn * Adjusting mss in this way is not allowed for datagrams. 2881fa15601aSQuentin Monnet * 288280867c5eSQuentin Monnet * * **BPF_F_ADJ_ROOM_ENCAP_L3_IPV4**, 288380867c5eSQuentin Monnet * **BPF_F_ADJ_ROOM_ENCAP_L3_IPV6**: 2884868d5235SWillem de Bruijn * Any new space is reserved to hold a tunnel header. 2885868d5235SWillem de Bruijn * Configure skb offsets and other fields accordingly. 2886868d5235SWillem de Bruijn * 288780867c5eSQuentin Monnet * * **BPF_F_ADJ_ROOM_ENCAP_L4_GRE**, 288880867c5eSQuentin Monnet * **BPF_F_ADJ_ROOM_ENCAP_L4_UDP**: 2889868d5235SWillem de Bruijn * Use with ENCAP_L3 flags to further specify the tunnel type. 2890868d5235SWillem de Bruijn * 289180867c5eSQuentin Monnet * * **BPF_F_ADJ_ROOM_ENCAP_L2**\ (*len*): 289258dfc900SAlan Maguire * Use with ENCAP_L3/L4 flags to further specify the tunnel 289380867c5eSQuentin Monnet * type; *len* is the length of the inner MAC header. 289458dfc900SAlan Maguire * 2895d01b59c9SXuesen Huang * * **BPF_F_ADJ_ROOM_ENCAP_L2_ETH**: 2896d01b59c9SXuesen Huang * Use with BPF_F_ADJ_ROOM_ENCAP_L2 flag to further specify the 2897d01b59c9SXuesen Huang * L2 type as Ethernet. 2898d01b59c9SXuesen Huang * 2899d219df60SZiyang Xuan * * **BPF_F_ADJ_ROOM_DECAP_L3_IPV4**, 2900d219df60SZiyang Xuan * **BPF_F_ADJ_ROOM_DECAP_L3_IPV6**: 2901d219df60SZiyang Xuan * Indicate the new IP header version after decapsulating the outer 2902d219df60SZiyang Xuan * IP header. Used when the inner and outer IP versions are different. 2903d219df60SZiyang Xuan * 290432e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2905fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2906fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 2907fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 2908fa15601aSQuentin Monnet * direct packet access. 2909fa15601aSQuentin Monnet * Return 2910fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 2911fa15601aSQuentin Monnet * 291232637e33SToke Høiland-Jørgensen * long bpf_redirect_map(struct bpf_map *map, u64 key, u64 flags) 2913ab127040SQuentin Monnet * Description 2914ab127040SQuentin Monnet * Redirect the packet to the endpoint referenced by *map* at 2915ab127040SQuentin Monnet * index *key*. Depending on its type, this *map* can contain 2916ab127040SQuentin Monnet * references to net devices (for forwarding packets through other 2917ab127040SQuentin Monnet * ports), or to CPUs (for redirecting XDP frames to another CPU; 2918ab127040SQuentin Monnet * but this is only implemented for native XDP (with driver 2919ab127040SQuentin Monnet * support) as of this writing). 2920ab127040SQuentin Monnet * 292143e74c02SToke Høiland-Jørgensen * The lower two bits of *flags* are used as the return code if 292243e74c02SToke Høiland-Jørgensen * the map lookup fails. This is so that the return value can be 2923ab8d7809SQuentin Monnet * one of the XDP program return codes up to **XDP_TX**, as chosen 2924e624d4edSHangbin Liu * by the caller. The higher bits of *flags* can be set to 2925e624d4edSHangbin Liu * BPF_F_BROADCAST or BPF_F_EXCLUDE_INGRESS as defined below. 2926e624d4edSHangbin Liu * 2927e624d4edSHangbin Liu * With BPF_F_BROADCAST the packet will be broadcasted to all the 2928e624d4edSHangbin Liu * interfaces in the map, with BPF_F_EXCLUDE_INGRESS the ingress 2929e624d4edSHangbin Liu * interface will be excluded when do broadcasting. 2930ab127040SQuentin Monnet * 2931ab8d7809SQuentin Monnet * See also **bpf_redirect**\ (), which only supports redirecting 2932ab8d7809SQuentin Monnet * to an ifindex, but doesn't require a map to do so. 2933ab127040SQuentin Monnet * Return 2934f25975f4SToke Høiland-Jørgensen * **XDP_REDIRECT** on success, or the value of the two lower bits 2935a33d3147SJakub Wilk * of the *flags* argument on error. 2936ab127040SQuentin Monnet * 2937bdb7b79bSAndrii Nakryiko * long bpf_sk_redirect_map(struct sk_buff *skb, struct bpf_map *map, u32 key, u64 flags) 2938ab127040SQuentin Monnet * Description 2939ab127040SQuentin Monnet * Redirect the packet to the socket referenced by *map* (of type 2940ab127040SQuentin Monnet * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and 2941ab127040SQuentin Monnet * egress interfaces can be used for redirection. The 2942ab127040SQuentin Monnet * **BPF_F_INGRESS** value in *flags* is used to make the 2943ab127040SQuentin Monnet * distinction (ingress path is selected if the flag is present, 2944ab127040SQuentin Monnet * egress path otherwise). This is the only flag supported for now. 2945ab127040SQuentin Monnet * Return 2946ab127040SQuentin Monnet * **SK_PASS** on success, or **SK_DROP** on error. 2947ab127040SQuentin Monnet * 2948bdb7b79bSAndrii Nakryiko * long bpf_sock_map_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags) 2949ab127040SQuentin Monnet * Description 2950ab127040SQuentin Monnet * Add an entry to, or update a *map* referencing sockets. The 2951ab127040SQuentin Monnet * *skops* is used as a new value for the entry associated to 2952ab127040SQuentin Monnet * *key*. *flags* is one of: 2953ab127040SQuentin Monnet * 2954ab127040SQuentin Monnet * **BPF_NOEXIST** 2955ab127040SQuentin Monnet * The entry for *key* must not exist in the map. 2956ab127040SQuentin Monnet * **BPF_EXIST** 2957ab127040SQuentin Monnet * The entry for *key* must already exist in the map. 2958ab127040SQuentin Monnet * **BPF_ANY** 2959ab127040SQuentin Monnet * No condition on the existence of the entry for *key*. 2960ab127040SQuentin Monnet * 2961ab127040SQuentin Monnet * If the *map* has eBPF programs (parser and verdict), those will 2962ab127040SQuentin Monnet * be inherited by the socket being added. If the socket is 2963ab127040SQuentin Monnet * already attached to eBPF programs, this results in an error. 2964ab127040SQuentin Monnet * Return 2965ab127040SQuentin Monnet * 0 on success, or a negative error in case of failure. 2966ab127040SQuentin Monnet * 2967bdb7b79bSAndrii Nakryiko * long bpf_xdp_adjust_meta(struct xdp_buff *xdp_md, int delta) 2968fa15601aSQuentin Monnet * Description 2969fa15601aSQuentin Monnet * Adjust the address pointed by *xdp_md*\ **->data_meta** by 2970fa15601aSQuentin Monnet * *delta* (which can be positive or negative). Note that this 2971fa15601aSQuentin Monnet * operation modifies the address stored in *xdp_md*\ **->data**, 2972fa15601aSQuentin Monnet * so the latter must be loaded only after the helper has been 2973fa15601aSQuentin Monnet * called. 2974fa15601aSQuentin Monnet * 2975fa15601aSQuentin Monnet * The use of *xdp_md*\ **->data_meta** is optional and programs 2976fa15601aSQuentin Monnet * are not required to use it. The rationale is that when the 2977fa15601aSQuentin Monnet * packet is processed with XDP (e.g. as DoS filter), it is 2978fa15601aSQuentin Monnet * possible to push further meta data along with it before passing 2979fa15601aSQuentin Monnet * to the stack, and to give the guarantee that an ingress eBPF 2980fa15601aSQuentin Monnet * program attached as a TC classifier on the same device can pick 2981fa15601aSQuentin Monnet * this up for further post-processing. Since TC works with socket 2982fa15601aSQuentin Monnet * buffers, it remains possible to set from XDP the **mark** or 2983fa15601aSQuentin Monnet * **priority** pointers, or other pointers for the socket buffer. 2984fa15601aSQuentin Monnet * Having this scratch space generic and programmable allows for 2985fa15601aSQuentin Monnet * more flexibility as the user is free to store whatever meta 2986fa15601aSQuentin Monnet * data they need. 2987fa15601aSQuentin Monnet * 298832e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2989fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2990fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 2991fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 2992fa15601aSQuentin Monnet * direct packet access. 2993fa15601aSQuentin Monnet * Return 2994fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 29957aa79a86SQuentin Monnet * 2996bdb7b79bSAndrii Nakryiko * long bpf_perf_event_read_value(struct bpf_map *map, u64 flags, struct bpf_perf_event_value *buf, u32 buf_size) 29977aa79a86SQuentin Monnet * Description 29987aa79a86SQuentin Monnet * Read the value of a perf event counter, and store it into *buf* 29997aa79a86SQuentin Monnet * of size *buf_size*. This helper relies on a *map* of type 30007aa79a86SQuentin Monnet * **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of the perf event 30017aa79a86SQuentin Monnet * counter is selected when *map* is updated with perf event file 30027aa79a86SQuentin Monnet * descriptors. The *map* is an array whose size is the number of 30037aa79a86SQuentin Monnet * available CPUs, and each cell contains a value relative to one 30047aa79a86SQuentin Monnet * CPU. The value to retrieve is indicated by *flags*, that 30057aa79a86SQuentin Monnet * contains the index of the CPU to look up, masked with 30067aa79a86SQuentin Monnet * **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to 30077aa79a86SQuentin Monnet * **BPF_F_CURRENT_CPU** to indicate that the value for the 30087aa79a86SQuentin Monnet * current CPU should be retrieved. 30097aa79a86SQuentin Monnet * 30107aa79a86SQuentin Monnet * This helper behaves in a way close to 30117aa79a86SQuentin Monnet * **bpf_perf_event_read**\ () helper, save that instead of 30127aa79a86SQuentin Monnet * just returning the value observed, it fills the *buf* 30137aa79a86SQuentin Monnet * structure. This allows for additional data to be retrieved: in 30147aa79a86SQuentin Monnet * particular, the enabled and running times (in *buf*\ 30157aa79a86SQuentin Monnet * **->enabled** and *buf*\ **->running**, respectively) are 30167aa79a86SQuentin Monnet * copied. In general, **bpf_perf_event_read_value**\ () is 30177aa79a86SQuentin Monnet * recommended over **bpf_perf_event_read**\ (), which has some 30187aa79a86SQuentin Monnet * ABI issues and provides fewer functionalities. 30197aa79a86SQuentin Monnet * 30207aa79a86SQuentin Monnet * These values are interesting, because hardware PMU (Performance 30217aa79a86SQuentin Monnet * Monitoring Unit) counters are limited resources. When there are 30227aa79a86SQuentin Monnet * more PMU based perf events opened than available counters, 30237aa79a86SQuentin Monnet * kernel will multiplex these events so each event gets certain 30247aa79a86SQuentin Monnet * percentage (but not all) of the PMU time. In case that 30257aa79a86SQuentin Monnet * multiplexing happens, the number of samples or counter value 30267aa79a86SQuentin Monnet * will not reflect the case compared to when no multiplexing 30277aa79a86SQuentin Monnet * occurs. This makes comparison between different runs difficult. 30287aa79a86SQuentin Monnet * Typically, the counter value should be normalized before 30297aa79a86SQuentin Monnet * comparing to other experiments. The usual normalization is done 30307aa79a86SQuentin Monnet * as follows. 30317aa79a86SQuentin Monnet * 30327aa79a86SQuentin Monnet * :: 30337aa79a86SQuentin Monnet * 30347aa79a86SQuentin Monnet * normalized_counter = counter * t_enabled / t_running 30357aa79a86SQuentin Monnet * 30367aa79a86SQuentin Monnet * Where t_enabled is the time enabled for event and t_running is 30377aa79a86SQuentin Monnet * the time running for event since last normalization. The 30387aa79a86SQuentin Monnet * enabled and running times are accumulated since the perf event 30397aa79a86SQuentin Monnet * open. To achieve scaling factor between two invocations of an 3040ab8d7809SQuentin Monnet * eBPF program, users can use CPU id as the key (which is 30417aa79a86SQuentin Monnet * typical for perf array usage model) to remember the previous 30427aa79a86SQuentin Monnet * value and do the calculation inside the eBPF program. 30437aa79a86SQuentin Monnet * Return 30447aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 30457aa79a86SQuentin Monnet * 3046bdb7b79bSAndrii Nakryiko * long bpf_perf_prog_read_value(struct bpf_perf_event_data *ctx, struct bpf_perf_event_value *buf, u32 buf_size) 30477aa79a86SQuentin Monnet * Description 304817c9b4e1SFlorian Lehner * For an eBPF program attached to a perf event, retrieve the 30497aa79a86SQuentin Monnet * value of the event counter associated to *ctx* and store it in 30507aa79a86SQuentin Monnet * the structure pointed by *buf* and of size *buf_size*. Enabled 30517aa79a86SQuentin Monnet * and running times are also stored in the structure (see 30527aa79a86SQuentin Monnet * description of helper **bpf_perf_event_read_value**\ () for 30537aa79a86SQuentin Monnet * more details). 30547aa79a86SQuentin Monnet * Return 30557aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 30567aa79a86SQuentin Monnet * 3057bdb7b79bSAndrii Nakryiko * long bpf_getsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen) 30587aa79a86SQuentin Monnet * Description 30597aa79a86SQuentin Monnet * Emulate a call to **getsockopt()** on the socket associated to 30607aa79a86SQuentin Monnet * *bpf_socket*, which must be a full socket. The *level* at 30617aa79a86SQuentin Monnet * which the option resides and the name *optname* of the option 30627aa79a86SQuentin Monnet * must be specified, see **getsockopt(2)** for more information. 30637aa79a86SQuentin Monnet * The retrieved value is stored in the structure pointed by 30647aa79a86SQuentin Monnet * *opval* and of length *optlen*. 30657aa79a86SQuentin Monnet * 3066beecf11bSStanislav Fomichev * *bpf_socket* should be one of the following: 3067ab8d7809SQuentin Monnet * 3068beecf11bSStanislav Fomichev * * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**. 3069859051ddSDaan De Meyer * * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT**, 3070859051ddSDaan De Meyer * **BPF_CGROUP_INET6_CONNECT** and **BPF_CGROUP_UNIX_CONNECT**. 3071beecf11bSStanislav Fomichev * 30727aa79a86SQuentin Monnet * This helper actually implements a subset of **getsockopt()**. 307372b43bdeSJi Rongfeng * It supports the same set of *optname*\ s that is supported by 307472b43bdeSJi Rongfeng * the **bpf_setsockopt**\ () helper. The exceptions are 307572b43bdeSJi Rongfeng * **TCP_BPF_*** is **bpf_setsockopt**\ () only and 307672b43bdeSJi Rongfeng * **TCP_SAVED_SYN** is **bpf_getsockopt**\ () only. 30777aa79a86SQuentin Monnet * Return 30787aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 30797aa79a86SQuentin Monnet * 3080bdb7b79bSAndrii Nakryiko * long bpf_override_return(struct pt_regs *regs, u64 rc) 30817aa79a86SQuentin Monnet * Description 30827aa79a86SQuentin Monnet * Used for error injection, this helper uses kprobes to override 30837aa79a86SQuentin Monnet * the return value of the probed function, and to set it to *rc*. 30847aa79a86SQuentin Monnet * The first argument is the context *regs* on which the kprobe 30857aa79a86SQuentin Monnet * works. 30867aa79a86SQuentin Monnet * 3087ab8d7809SQuentin Monnet * This helper works by setting the PC (program counter) 30887aa79a86SQuentin Monnet * to an override function which is run in place of the original 30897aa79a86SQuentin Monnet * probed function. This means the probed function is not run at 30907aa79a86SQuentin Monnet * all. The replacement function just returns with the required 30917aa79a86SQuentin Monnet * value. 30927aa79a86SQuentin Monnet * 30937aa79a86SQuentin Monnet * This helper has security implications, and thus is subject to 30947aa79a86SQuentin Monnet * restrictions. It is only available if the kernel was compiled 30957aa79a86SQuentin Monnet * with the **CONFIG_BPF_KPROBE_OVERRIDE** configuration 30967aa79a86SQuentin Monnet * option, and in this case it only works on functions tagged with 30977aa79a86SQuentin Monnet * **ALLOW_ERROR_INJECTION** in the kernel code. 30987aa79a86SQuentin Monnet * 30997aa79a86SQuentin Monnet * Also, the helper is only available for the architectures having 31007aa79a86SQuentin Monnet * the CONFIG_FUNCTION_ERROR_INJECTION option. As of this writing, 31017aa79a86SQuentin Monnet * x86 architecture is the only one to support this feature. 31027aa79a86SQuentin Monnet * Return 31037aa79a86SQuentin Monnet * 0 31047aa79a86SQuentin Monnet * 3105bdb7b79bSAndrii Nakryiko * long bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *bpf_sock, int argval) 31067aa79a86SQuentin Monnet * Description 31077aa79a86SQuentin Monnet * Attempt to set the value of the **bpf_sock_ops_cb_flags** field 31087aa79a86SQuentin Monnet * for the full TCP socket associated to *bpf_sock_ops* to 31097aa79a86SQuentin Monnet * *argval*. 31107aa79a86SQuentin Monnet * 31117aa79a86SQuentin Monnet * The primary use of this field is to determine if there should 31127aa79a86SQuentin Monnet * be calls to eBPF programs of type 31137aa79a86SQuentin Monnet * **BPF_PROG_TYPE_SOCK_OPS** at various points in the TCP 31147aa79a86SQuentin Monnet * code. A program of the same type can change its value, per 31157aa79a86SQuentin Monnet * connection and as necessary, when the connection is 31167aa79a86SQuentin Monnet * established. This field is directly accessible for reading, but 31177aa79a86SQuentin Monnet * this helper must be used for updates in order to return an 31187aa79a86SQuentin Monnet * error if an eBPF program tries to set a callback that is not 31197aa79a86SQuentin Monnet * supported in the current kernel. 31207aa79a86SQuentin Monnet * 3121725721a6SViet Hoang Tran * *argval* is a flag array which can combine these flags: 31227aa79a86SQuentin Monnet * 31237aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_RTO_CB_FLAG** (retransmission time out) 31247aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_RETRANS_CB_FLAG** (retransmission) 31257aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_STATE_CB_FLAG** (TCP state change) 312623729ff2SStanislav Fomichev * * **BPF_SOCK_OPS_RTT_CB_FLAG** (every RTT) 31277aa79a86SQuentin Monnet * 3128725721a6SViet Hoang Tran * Therefore, this function can be used to clear a callback flag by 3129725721a6SViet Hoang Tran * setting the appropriate bit to zero. e.g. to disable the RTO 3130725721a6SViet Hoang Tran * callback: 3131725721a6SViet Hoang Tran * 3132725721a6SViet Hoang Tran * **bpf_sock_ops_cb_flags_set(bpf_sock,** 3133725721a6SViet Hoang Tran * **bpf_sock->bpf_sock_ops_cb_flags & ~BPF_SOCK_OPS_RTO_CB_FLAG)** 3134725721a6SViet Hoang Tran * 31357aa79a86SQuentin Monnet * Here are some examples of where one could call such eBPF 31367aa79a86SQuentin Monnet * program: 31377aa79a86SQuentin Monnet * 31387aa79a86SQuentin Monnet * * When RTO fires. 31397aa79a86SQuentin Monnet * * When a packet is retransmitted. 31407aa79a86SQuentin Monnet * * When the connection terminates. 31417aa79a86SQuentin Monnet * * When a packet is sent. 31427aa79a86SQuentin Monnet * * When a packet is received. 31437aa79a86SQuentin Monnet * Return 31447aa79a86SQuentin Monnet * Code **-EINVAL** if the socket is not a full TCP socket; 31457aa79a86SQuentin Monnet * otherwise, a positive number containing the bits that could not 31467aa79a86SQuentin Monnet * be set is returned (which comes down to 0 if all bits were set 31477aa79a86SQuentin Monnet * as required). 31487aa79a86SQuentin Monnet * 3149bdb7b79bSAndrii Nakryiko * long bpf_msg_redirect_map(struct sk_msg_buff *msg, struct bpf_map *map, u32 key, u64 flags) 3150ab127040SQuentin Monnet * Description 3151ab127040SQuentin Monnet * This helper is used in programs implementing policies at the 3152ab127040SQuentin Monnet * socket level. If the message *msg* is allowed to pass (i.e. if 3153ab127040SQuentin Monnet * the verdict eBPF program returns **SK_PASS**), redirect it to 3154ab127040SQuentin Monnet * the socket referenced by *map* (of type 3155ab127040SQuentin Monnet * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and 3156ab127040SQuentin Monnet * egress interfaces can be used for redirection. The 3157ab127040SQuentin Monnet * **BPF_F_INGRESS** value in *flags* is used to make the 3158ab127040SQuentin Monnet * distinction (ingress path is selected if the flag is present, 3159ab127040SQuentin Monnet * egress path otherwise). This is the only flag supported for now. 3160ab127040SQuentin Monnet * Return 3161ab127040SQuentin Monnet * **SK_PASS** on success, or **SK_DROP** on error. 3162ab127040SQuentin Monnet * 3163bdb7b79bSAndrii Nakryiko * long bpf_msg_apply_bytes(struct sk_msg_buff *msg, u32 bytes) 3164ab127040SQuentin Monnet * Description 3165ab127040SQuentin Monnet * For socket policies, apply the verdict of the eBPF program to 3166ab127040SQuentin Monnet * the next *bytes* (number of bytes) of message *msg*. 3167ab127040SQuentin Monnet * 3168ab127040SQuentin Monnet * For example, this helper can be used in the following cases: 3169ab127040SQuentin Monnet * 3170ab127040SQuentin Monnet * * A single **sendmsg**\ () or **sendfile**\ () system call 3171ab127040SQuentin Monnet * contains multiple logical messages that the eBPF program is 3172ab127040SQuentin Monnet * supposed to read and for which it should apply a verdict. 3173ab127040SQuentin Monnet * * An eBPF program only cares to read the first *bytes* of a 3174ab127040SQuentin Monnet * *msg*. If the message has a large payload, then setting up 3175ab127040SQuentin Monnet * and calling the eBPF program repeatedly for all bytes, even 3176ab127040SQuentin Monnet * though the verdict is already known, would create unnecessary 3177ab127040SQuentin Monnet * overhead. 3178ab127040SQuentin Monnet * 3179ab127040SQuentin Monnet * When called from within an eBPF program, the helper sets a 3180ab127040SQuentin Monnet * counter internal to the BPF infrastructure, that is used to 3181ab127040SQuentin Monnet * apply the last verdict to the next *bytes*. If *bytes* is 3182ab127040SQuentin Monnet * smaller than the current data being processed from a 3183ab127040SQuentin Monnet * **sendmsg**\ () or **sendfile**\ () system call, the first 3184ab127040SQuentin Monnet * *bytes* will be sent and the eBPF program will be re-run with 3185ab127040SQuentin Monnet * the pointer for start of data pointing to byte number *bytes* 3186ab127040SQuentin Monnet * **+ 1**. If *bytes* is larger than the current data being 3187ab127040SQuentin Monnet * processed, then the eBPF verdict will be applied to multiple 3188ab127040SQuentin Monnet * **sendmsg**\ () or **sendfile**\ () calls until *bytes* are 3189ab127040SQuentin Monnet * consumed. 3190ab127040SQuentin Monnet * 3191ab127040SQuentin Monnet * Note that if a socket closes with the internal counter holding 3192ab127040SQuentin Monnet * a non-zero value, this is not a problem because data is not 3193ab127040SQuentin Monnet * being buffered for *bytes* and is sent as it is received. 3194ab127040SQuentin Monnet * Return 3195ab127040SQuentin Monnet * 0 3196ab127040SQuentin Monnet * 3197bdb7b79bSAndrii Nakryiko * long bpf_msg_cork_bytes(struct sk_msg_buff *msg, u32 bytes) 3198ab127040SQuentin Monnet * Description 3199ab127040SQuentin Monnet * For socket policies, prevent the execution of the verdict eBPF 3200ab127040SQuentin Monnet * program for message *msg* until *bytes* (byte number) have been 3201ab127040SQuentin Monnet * accumulated. 3202ab127040SQuentin Monnet * 3203ab127040SQuentin Monnet * This can be used when one needs a specific number of bytes 3204ab127040SQuentin Monnet * before a verdict can be assigned, even if the data spans 3205ab127040SQuentin Monnet * multiple **sendmsg**\ () or **sendfile**\ () calls. The extreme 3206ab127040SQuentin Monnet * case would be a user calling **sendmsg**\ () repeatedly with 3207ab127040SQuentin Monnet * 1-byte long message segments. Obviously, this is bad for 3208ab127040SQuentin Monnet * performance, but it is still valid. If the eBPF program needs 3209ab127040SQuentin Monnet * *bytes* bytes to validate a header, this helper can be used to 3210ab127040SQuentin Monnet * prevent the eBPF program to be called again until *bytes* have 3211ab127040SQuentin Monnet * been accumulated. 3212ab127040SQuentin Monnet * Return 3213ab127040SQuentin Monnet * 0 3214ab127040SQuentin Monnet * 3215bdb7b79bSAndrii Nakryiko * long bpf_msg_pull_data(struct sk_msg_buff *msg, u32 start, u32 end, u64 flags) 3216ab127040SQuentin Monnet * Description 3217ab127040SQuentin Monnet * For socket policies, pull in non-linear data from user space 3218ab127040SQuentin Monnet * for *msg* and set pointers *msg*\ **->data** and *msg*\ 3219ab127040SQuentin Monnet * **->data_end** to *start* and *end* bytes offsets into *msg*, 3220ab127040SQuentin Monnet * respectively. 3221ab127040SQuentin Monnet * 3222ab127040SQuentin Monnet * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a 3223ab127040SQuentin Monnet * *msg* it can only parse data that the (**data**, **data_end**) 3224ab127040SQuentin Monnet * pointers have already consumed. For **sendmsg**\ () hooks this 3225ab127040SQuentin Monnet * is likely the first scatterlist element. But for calls relying 3226ab127040SQuentin Monnet * on the **sendpage** handler (e.g. **sendfile**\ ()) this will 3227ab127040SQuentin Monnet * be the range (**0**, **0**) because the data is shared with 3228ab127040SQuentin Monnet * user space and by default the objective is to avoid allowing 3229ab127040SQuentin Monnet * user space to modify data while (or after) eBPF verdict is 3230ab127040SQuentin Monnet * being decided. This helper can be used to pull in data and to 3231ab127040SQuentin Monnet * set the start and end pointer to given values. Data will be 3232ab127040SQuentin Monnet * copied if necessary (i.e. if data was not linear and if start 3233ab127040SQuentin Monnet * and end pointers do not point to the same chunk). 3234ab127040SQuentin Monnet * 323532e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 3236ab127040SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 3237ab127040SQuentin Monnet * previously done by the verifier are invalidated and must be 3238ab127040SQuentin Monnet * performed again, if the helper is used in combination with 3239ab127040SQuentin Monnet * direct packet access. 3240ab127040SQuentin Monnet * 3241ab127040SQuentin Monnet * All values for *flags* are reserved for future usage, and must 3242ab127040SQuentin Monnet * be left at zero. 3243ab127040SQuentin Monnet * Return 3244ab127040SQuentin Monnet * 0 on success, or a negative error in case of failure. 3245ab127040SQuentin Monnet * 3246bdb7b79bSAndrii Nakryiko * long bpf_bind(struct bpf_sock_addr *ctx, struct sockaddr *addr, int addr_len) 32477aa79a86SQuentin Monnet * Description 32487aa79a86SQuentin Monnet * Bind the socket associated to *ctx* to the address pointed by 32497aa79a86SQuentin Monnet * *addr*, of length *addr_len*. This allows for making outgoing 32507aa79a86SQuentin Monnet * connection from the desired IP address, which can be useful for 32517aa79a86SQuentin Monnet * example when all processes inside a cgroup should use one 32527aa79a86SQuentin Monnet * single IP address on a host that has multiple IP configured. 32537aa79a86SQuentin Monnet * 32547aa79a86SQuentin Monnet * This helper works for IPv4 and IPv6, TCP and UDP sockets. The 32557aa79a86SQuentin Monnet * domain (*addr*\ **->sa_family**) must be **AF_INET** (or 32568086fbafSStanislav Fomichev * **AF_INET6**). It's advised to pass zero port (**sin_port** 32578086fbafSStanislav Fomichev * or **sin6_port**) which triggers IP_BIND_ADDRESS_NO_PORT-like 32588086fbafSStanislav Fomichev * behavior and lets the kernel efficiently pick up an unused 32598086fbafSStanislav Fomichev * port as long as 4-tuple is unique. Passing non-zero port might 32608086fbafSStanislav Fomichev * lead to degraded performance. 32617aa79a86SQuentin Monnet * Return 32627aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 32632d020dd7SQuentin Monnet * 3264bdb7b79bSAndrii Nakryiko * long bpf_xdp_adjust_tail(struct xdp_buff *xdp_md, int delta) 32652d020dd7SQuentin Monnet * Description 32662d020dd7SQuentin Monnet * Adjust (move) *xdp_md*\ **->data_end** by *delta* bytes. It is 3267c8741e2bSJesper Dangaard Brouer * possible to both shrink and grow the packet tail. 3268c8741e2bSJesper Dangaard Brouer * Shrink done via *delta* being a negative integer. 32692d020dd7SQuentin Monnet * 327032e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 32712d020dd7SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 32722d020dd7SQuentin Monnet * previously done by the verifier are invalidated and must be 32732d020dd7SQuentin Monnet * performed again, if the helper is used in combination with 32742d020dd7SQuentin Monnet * direct packet access. 32752d020dd7SQuentin Monnet * Return 32762d020dd7SQuentin Monnet * 0 on success, or a negative error in case of failure. 32772d020dd7SQuentin Monnet * 3278bdb7b79bSAndrii Nakryiko * long bpf_skb_get_xfrm_state(struct sk_buff *skb, u32 index, struct bpf_xfrm_state *xfrm_state, u32 size, u64 flags) 32792d020dd7SQuentin Monnet * Description 32802d020dd7SQuentin Monnet * Retrieve the XFRM state (IP transform framework, see also 32812d020dd7SQuentin Monnet * **ip-xfrm(8)**) at *index* in XFRM "security path" for *skb*. 32822d020dd7SQuentin Monnet * 32832d020dd7SQuentin Monnet * The retrieved value is stored in the **struct bpf_xfrm_state** 32842d020dd7SQuentin Monnet * pointed by *xfrm_state* and of length *size*. 32852d020dd7SQuentin Monnet * 32862d020dd7SQuentin Monnet * All values for *flags* are reserved for future usage, and must 32872d020dd7SQuentin Monnet * be left at zero. 32882d020dd7SQuentin Monnet * 32892d020dd7SQuentin Monnet * This helper is available only if the kernel was compiled with 32902d020dd7SQuentin Monnet * **CONFIG_XFRM** configuration option. 32912d020dd7SQuentin Monnet * Return 32922d020dd7SQuentin Monnet * 0 on success, or a negative error in case of failure. 3293c195651eSYonghong Song * 3294bdb7b79bSAndrii Nakryiko * long bpf_get_stack(void *ctx, void *buf, u32 size, u64 flags) 3295c195651eSYonghong Song * Description 3296c195651eSYonghong Song * Return a user or a kernel stack in bpf program provided buffer. 3297c195651eSYonghong Song * To achieve this, the helper needs *ctx*, which is a pointer 3298c195651eSYonghong Song * to the context on which the tracing program is executed. 3299c195651eSYonghong Song * To store the stacktrace, the bpf program provides *buf* with 3300c195651eSYonghong Song * a nonnegative *size*. 3301c195651eSYonghong Song * 3302c195651eSYonghong Song * The last argument, *flags*, holds the number of stack frames to 3303c195651eSYonghong Song * skip (from 0 to 255), masked with 3304c195651eSYonghong Song * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 3305c195651eSYonghong Song * the following flags: 3306c195651eSYonghong Song * 3307c195651eSYonghong Song * **BPF_F_USER_STACK** 3308c195651eSYonghong Song * Collect a user space stack instead of a kernel stack. 3309c195651eSYonghong Song * **BPF_F_USER_BUILD_ID** 3310ca34ce29SDave Marchevsky * Collect (build_id, file_offset) instead of ips for user 3311ca34ce29SDave Marchevsky * stack, only valid if **BPF_F_USER_STACK** is also 3312ca34ce29SDave Marchevsky * specified. 3313ca34ce29SDave Marchevsky * 3314ca34ce29SDave Marchevsky * *file_offset* is an offset relative to the beginning 3315ca34ce29SDave Marchevsky * of the executable or shared object file backing the vma 3316ca34ce29SDave Marchevsky * which the *ip* falls in. It is *not* an offset relative 3317ca34ce29SDave Marchevsky * to that object's base address. Accordingly, it must be 3318ca34ce29SDave Marchevsky * adjusted by adding (sh_addr - sh_offset), where 3319ca34ce29SDave Marchevsky * sh_{addr,offset} correspond to the executable section 3320ca34ce29SDave Marchevsky * containing *file_offset* in the object, for comparisons 3321ca34ce29SDave Marchevsky * to symbols' st_value to be valid. 3322c195651eSYonghong Song * 3323c195651eSYonghong Song * **bpf_get_stack**\ () can collect up to 3324c195651eSYonghong Song * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject 3325c195651eSYonghong Song * to sufficient large buffer size. Note that 3326c195651eSYonghong Song * this limit can be controlled with the **sysctl** program, and 3327c195651eSYonghong Song * that it should be manually increased in order to profile long 3328c195651eSYonghong Song * user stacks (such as stacks for Java programs). To do so, use: 3329c195651eSYonghong Song * 3330c195651eSYonghong Song * :: 3331c195651eSYonghong Song * 3332c195651eSYonghong Song * # sysctl kernel.perf_event_max_stack=<new value> 3333c195651eSYonghong Song * Return 3334ee2a0988SNamhyung Kim * The non-negative copied *buf* length equal to or less than 3335ee2a0988SNamhyung Kim * *size* on success, or a negative error in case of failure. 33364e1ec56cSDaniel Borkmann * 3337bdb7b79bSAndrii Nakryiko * long bpf_skb_load_bytes_relative(const void *skb, u32 offset, void *to, u32 len, u32 start_header) 33384e1ec56cSDaniel Borkmann * Description 33394e1ec56cSDaniel Borkmann * This helper is similar to **bpf_skb_load_bytes**\ () in that 33404e1ec56cSDaniel Borkmann * it provides an easy way to load *len* bytes from *offset* 33414e1ec56cSDaniel Borkmann * from the packet associated to *skb*, into the buffer pointed 33424e1ec56cSDaniel Borkmann * by *to*. The difference to **bpf_skb_load_bytes**\ () is that 33434e1ec56cSDaniel Borkmann * a fifth argument *start_header* exists in order to select a 33444e1ec56cSDaniel Borkmann * base offset to start from. *start_header* can be one of: 33454e1ec56cSDaniel Borkmann * 33464e1ec56cSDaniel Borkmann * **BPF_HDR_START_MAC** 33474e1ec56cSDaniel Borkmann * Base offset to load data from is *skb*'s mac header. 33484e1ec56cSDaniel Borkmann * **BPF_HDR_START_NET** 33494e1ec56cSDaniel Borkmann * Base offset to load data from is *skb*'s network header. 33504e1ec56cSDaniel Borkmann * 33514e1ec56cSDaniel Borkmann * In general, "direct packet access" is the preferred method to 33524e1ec56cSDaniel Borkmann * access packet data, however, this helper is in particular useful 33534e1ec56cSDaniel Borkmann * in socket filters where *skb*\ **->data** does not always point 33544e1ec56cSDaniel Borkmann * to the start of the mac header and where "direct packet access" 33554e1ec56cSDaniel Borkmann * is not available. 33564e1ec56cSDaniel Borkmann * Return 33574e1ec56cSDaniel Borkmann * 0 on success, or a negative error in case of failure. 33584e1ec56cSDaniel Borkmann * 3359bdb7b79bSAndrii Nakryiko * long bpf_fib_lookup(void *ctx, struct bpf_fib_lookup *params, int plen, u32 flags) 336087f5fc7eSDavid Ahern * Description 336187f5fc7eSDavid Ahern * Do FIB lookup in kernel tables using parameters in *params*. 336287f5fc7eSDavid Ahern * If lookup is successful and result shows packet is to be 336387f5fc7eSDavid Ahern * forwarded, the neighbor tables are searched for the nexthop. 336487f5fc7eSDavid Ahern * If successful (ie., FIB lookup shows forwarding and nexthop 3365fa898d76SDavid Ahern * is resolved), the nexthop address is returned in ipv4_dst 3366fa898d76SDavid Ahern * or ipv6_dst based on family, smac is set to mac address of 3367fa898d76SDavid Ahern * egress device, dmac is set to nexthop mac address, rt_metric 33684c79579bSDavid Ahern * is set to metric from route (IPv4/IPv6 only), and ifindex 33694c79579bSDavid Ahern * is set to the device index of the nexthop from the FIB lookup. 337087f5fc7eSDavid Ahern * 337187f5fc7eSDavid Ahern * *plen* argument is the size of the passed in struct. 33727a279e93SQuentin Monnet * *flags* argument can be a combination of one or more of the 33737a279e93SQuentin Monnet * following values: 337487f5fc7eSDavid Ahern * 33757a279e93SQuentin Monnet * **BPF_FIB_LOOKUP_DIRECT** 33767a279e93SQuentin Monnet * Do a direct table lookup vs full lookup using FIB 33777a279e93SQuentin Monnet * rules. 33788ad77e72SLouis DeLosSantos * **BPF_FIB_LOOKUP_TBID** 33798ad77e72SLouis DeLosSantos * Used with BPF_FIB_LOOKUP_DIRECT. 33808ad77e72SLouis DeLosSantos * Use the routing table ID present in *params*->tbid 33818ad77e72SLouis DeLosSantos * for the fib lookup. 33827a279e93SQuentin Monnet * **BPF_FIB_LOOKUP_OUTPUT** 33837a279e93SQuentin Monnet * Perform lookup from an egress perspective (default is 33847a279e93SQuentin Monnet * ingress). 338531de4105SMartin KaFai Lau * **BPF_FIB_LOOKUP_SKIP_NEIGH** 338631de4105SMartin KaFai Lau * Skip the neighbour table lookup. *params*->dmac 338731de4105SMartin KaFai Lau * and *params*->smac will not be set as output. A common 338831de4105SMartin KaFai Lau * use case is to call **bpf_redirect_neigh**\ () after 338931de4105SMartin KaFai Lau * doing **bpf_fib_lookup**\ (). 3390dab4e1f0SMartynas Pumputis * **BPF_FIB_LOOKUP_SRC** 3391dab4e1f0SMartynas Pumputis * Derive and set source IP addr in *params*->ipv{4,6}_src 3392dab4e1f0SMartynas Pumputis * for the nexthop. If the src addr cannot be derived, 3393dab4e1f0SMartynas Pumputis * **BPF_FIB_LKUP_RET_NO_SRC_ADDR** is returned. In this 3394dab4e1f0SMartynas Pumputis * case, *params*->dmac and *params*->smac are not set either. 339587f5fc7eSDavid Ahern * 339687f5fc7eSDavid Ahern * *ctx* is either **struct xdp_md** for XDP programs or 339787f5fc7eSDavid Ahern * **struct sk_buff** tc cls_act programs. 339887f5fc7eSDavid Ahern * Return 33994c79579bSDavid Ahern * * < 0 if any input argument is invalid 34004c79579bSDavid Ahern * * 0 on success (packet is forwarded, nexthop neighbor exists) 34014c79579bSDavid Ahern * * > 0 one of **BPF_FIB_LKUP_RET_** codes explaining why the 34022bae79d2SQuentin Monnet * packet is not forwarded or needs assist from full stack 340381110384SJohn Fastabend * 3404e1850ea9SJesper Dangaard Brouer * If lookup fails with BPF_FIB_LKUP_RET_FRAG_NEEDED, then the MTU 3405e1850ea9SJesper Dangaard Brouer * was exceeded and output params->mtu_result contains the MTU. 3406e1850ea9SJesper Dangaard Brouer * 3407bdb7b79bSAndrii Nakryiko * long bpf_sock_hash_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags) 340881110384SJohn Fastabend * Description 340981110384SJohn Fastabend * Add an entry to, or update a sockhash *map* referencing sockets. 341081110384SJohn Fastabend * The *skops* is used as a new value for the entry associated to 341181110384SJohn Fastabend * *key*. *flags* is one of: 341281110384SJohn Fastabend * 341381110384SJohn Fastabend * **BPF_NOEXIST** 341481110384SJohn Fastabend * The entry for *key* must not exist in the map. 341581110384SJohn Fastabend * **BPF_EXIST** 341681110384SJohn Fastabend * The entry for *key* must already exist in the map. 341781110384SJohn Fastabend * **BPF_ANY** 341881110384SJohn Fastabend * No condition on the existence of the entry for *key*. 341981110384SJohn Fastabend * 342081110384SJohn Fastabend * If the *map* has eBPF programs (parser and verdict), those will 342181110384SJohn Fastabend * be inherited by the socket being added. If the socket is 342281110384SJohn Fastabend * already attached to eBPF programs, this results in an error. 342381110384SJohn Fastabend * Return 342481110384SJohn Fastabend * 0 on success, or a negative error in case of failure. 342581110384SJohn Fastabend * 3426bdb7b79bSAndrii Nakryiko * long bpf_msg_redirect_hash(struct sk_msg_buff *msg, struct bpf_map *map, void *key, u64 flags) 342781110384SJohn Fastabend * Description 342881110384SJohn Fastabend * This helper is used in programs implementing policies at the 342981110384SJohn Fastabend * socket level. If the message *msg* is allowed to pass (i.e. if 343081110384SJohn Fastabend * the verdict eBPF program returns **SK_PASS**), redirect it to 343181110384SJohn Fastabend * the socket referenced by *map* (of type 343281110384SJohn Fastabend * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and 343381110384SJohn Fastabend * egress interfaces can be used for redirection. The 343481110384SJohn Fastabend * **BPF_F_INGRESS** value in *flags* is used to make the 343581110384SJohn Fastabend * distinction (ingress path is selected if the flag is present, 343681110384SJohn Fastabend * egress path otherwise). This is the only flag supported for now. 343781110384SJohn Fastabend * Return 343881110384SJohn Fastabend * **SK_PASS** on success, or **SK_DROP** on error. 343981110384SJohn Fastabend * 3440bdb7b79bSAndrii Nakryiko * long bpf_sk_redirect_hash(struct sk_buff *skb, struct bpf_map *map, void *key, u64 flags) 344181110384SJohn Fastabend * Description 344281110384SJohn Fastabend * This helper is used in programs implementing policies at the 344381110384SJohn Fastabend * skb socket level. If the sk_buff *skb* is allowed to pass (i.e. 344449f3d12bSJakub Wilk * if the verdict eBPF program returns **SK_PASS**), redirect it 344581110384SJohn Fastabend * to the socket referenced by *map* (of type 344681110384SJohn Fastabend * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and 344781110384SJohn Fastabend * egress interfaces can be used for redirection. The 344881110384SJohn Fastabend * **BPF_F_INGRESS** value in *flags* is used to make the 344981110384SJohn Fastabend * distinction (ingress path is selected if the flag is present, 345081110384SJohn Fastabend * egress otherwise). This is the only flag supported for now. 345181110384SJohn Fastabend * Return 345281110384SJohn Fastabend * **SK_PASS** on success, or **SK_DROP** on error. 3453fe94cc29SMathieu Xhonneux * 3454bdb7b79bSAndrii Nakryiko * long bpf_lwt_push_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) 3455fe94cc29SMathieu Xhonneux * Description 3456fe94cc29SMathieu Xhonneux * Encapsulate the packet associated to *skb* within a Layer 3 3457fe94cc29SMathieu Xhonneux * protocol header. This header is provided in the buffer at 3458fe94cc29SMathieu Xhonneux * address *hdr*, with *len* its size in bytes. *type* indicates 3459fe94cc29SMathieu Xhonneux * the protocol of the header and can be one of: 3460fe94cc29SMathieu Xhonneux * 3461fe94cc29SMathieu Xhonneux * **BPF_LWT_ENCAP_SEG6** 3462fe94cc29SMathieu Xhonneux * IPv6 encapsulation with Segment Routing Header 3463fe94cc29SMathieu Xhonneux * (**struct ipv6_sr_hdr**). *hdr* only contains the SRH, 3464fe94cc29SMathieu Xhonneux * the IPv6 header is computed by the kernel. 3465fe94cc29SMathieu Xhonneux * **BPF_LWT_ENCAP_SEG6_INLINE** 3466fe94cc29SMathieu Xhonneux * Only works if *skb* contains an IPv6 packet. Insert a 3467fe94cc29SMathieu Xhonneux * Segment Routing Header (**struct ipv6_sr_hdr**) inside 3468fe94cc29SMathieu Xhonneux * the IPv6 header. 34693e0bd37cSPeter Oskolkov * **BPF_LWT_ENCAP_IP** 34703e0bd37cSPeter Oskolkov * IP encapsulation (GRE/GUE/IPIP/etc). The outer header 34713e0bd37cSPeter Oskolkov * must be IPv4 or IPv6, followed by zero or more 347280867c5eSQuentin Monnet * additional headers, up to **LWT_BPF_MAX_HEADROOM** 347380867c5eSQuentin Monnet * total bytes in all prepended headers. Please note that 347480867c5eSQuentin Monnet * if **skb_is_gso**\ (*skb*) is true, no more than two 347580867c5eSQuentin Monnet * headers can be prepended, and the inner header, if 347680867c5eSQuentin Monnet * present, should be either GRE or UDP/GUE. 34773e0bd37cSPeter Oskolkov * 347880867c5eSQuentin Monnet * **BPF_LWT_ENCAP_SEG6**\ \* types can be called by BPF programs 347980867c5eSQuentin Monnet * of type **BPF_PROG_TYPE_LWT_IN**; **BPF_LWT_ENCAP_IP** type can 348080867c5eSQuentin Monnet * be called by bpf programs of types **BPF_PROG_TYPE_LWT_IN** and 348180867c5eSQuentin Monnet * **BPF_PROG_TYPE_LWT_XMIT**. 3482fe94cc29SMathieu Xhonneux * 348332e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 3484fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 3485fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 3486fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 3487fe94cc29SMathieu Xhonneux * direct packet access. 3488fe94cc29SMathieu Xhonneux * Return 3489fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 3490fe94cc29SMathieu Xhonneux * 3491bdb7b79bSAndrii Nakryiko * long bpf_lwt_seg6_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len) 3492fe94cc29SMathieu Xhonneux * Description 3493fe94cc29SMathieu Xhonneux * Store *len* bytes from address *from* into the packet 3494fe94cc29SMathieu Xhonneux * associated to *skb*, at *offset*. Only the flags, tag and TLVs 3495fe94cc29SMathieu Xhonneux * inside the outermost IPv6 Segment Routing Header can be 3496fe94cc29SMathieu Xhonneux * modified through this helper. 3497fe94cc29SMathieu Xhonneux * 349832e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 3499fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 3500fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 3501fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 3502fe94cc29SMathieu Xhonneux * direct packet access. 3503fe94cc29SMathieu Xhonneux * Return 3504fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 3505fe94cc29SMathieu Xhonneux * 3506bdb7b79bSAndrii Nakryiko * long bpf_lwt_seg6_adjust_srh(struct sk_buff *skb, u32 offset, s32 delta) 3507fe94cc29SMathieu Xhonneux * Description 3508fe94cc29SMathieu Xhonneux * Adjust the size allocated to TLVs in the outermost IPv6 3509fe94cc29SMathieu Xhonneux * Segment Routing Header contained in the packet associated to 3510fe94cc29SMathieu Xhonneux * *skb*, at position *offset* by *delta* bytes. Only offsets 3511fe94cc29SMathieu Xhonneux * after the segments are accepted. *delta* can be as well 3512fe94cc29SMathieu Xhonneux * positive (growing) as negative (shrinking). 3513fe94cc29SMathieu Xhonneux * 351432e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 3515fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 3516fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 3517fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 3518fe94cc29SMathieu Xhonneux * direct packet access. 3519fe94cc29SMathieu Xhonneux * Return 3520fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 3521fe94cc29SMathieu Xhonneux * 3522bdb7b79bSAndrii Nakryiko * long bpf_lwt_seg6_action(struct sk_buff *skb, u32 action, void *param, u32 param_len) 3523fe94cc29SMathieu Xhonneux * Description 3524fe94cc29SMathieu Xhonneux * Apply an IPv6 Segment Routing action of type *action* to the 3525fe94cc29SMathieu Xhonneux * packet associated to *skb*. Each action takes a parameter 3526fe94cc29SMathieu Xhonneux * contained at address *param*, and of length *param_len* bytes. 3527fe94cc29SMathieu Xhonneux * *action* can be one of: 3528fe94cc29SMathieu Xhonneux * 3529fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_X** 3530fe94cc29SMathieu Xhonneux * End.X action: Endpoint with Layer-3 cross-connect. 3531fe94cc29SMathieu Xhonneux * Type of *param*: **struct in6_addr**. 3532fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_T** 3533fe94cc29SMathieu Xhonneux * End.T action: Endpoint with specific IPv6 table lookup. 3534fe94cc29SMathieu Xhonneux * Type of *param*: **int**. 3535fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_B6** 3536fe94cc29SMathieu Xhonneux * End.B6 action: Endpoint bound to an SRv6 policy. 353780867c5eSQuentin Monnet * Type of *param*: **struct ipv6_sr_hdr**. 3538fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_B6_ENCAP** 3539fe94cc29SMathieu Xhonneux * End.B6.Encap action: Endpoint bound to an SRv6 3540fe94cc29SMathieu Xhonneux * encapsulation policy. 354180867c5eSQuentin Monnet * Type of *param*: **struct ipv6_sr_hdr**. 3542fe94cc29SMathieu Xhonneux * 354332e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 3544fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 3545fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 3546fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 3547fe94cc29SMathieu Xhonneux * direct packet access. 3548fe94cc29SMathieu Xhonneux * Return 3549fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 3550f4364dcfSSean Young * 3551bdb7b79bSAndrii Nakryiko * long bpf_rc_repeat(void *ctx) 355262369db2SQuentin Monnet * Description 355362369db2SQuentin Monnet * This helper is used in programs implementing IR decoding, to 355462369db2SQuentin Monnet * report a successfully decoded repeat key message. This delays 355562369db2SQuentin Monnet * the generation of a key up event for previously generated 355662369db2SQuentin Monnet * key down event. 355762369db2SQuentin Monnet * 355862369db2SQuentin Monnet * Some IR protocols like NEC have a special IR message for 355962369db2SQuentin Monnet * repeating last button, for when a button is held down. 356062369db2SQuentin Monnet * 356162369db2SQuentin Monnet * The *ctx* should point to the lirc sample as passed into 356262369db2SQuentin Monnet * the program. 356362369db2SQuentin Monnet * 356462369db2SQuentin Monnet * This helper is only available is the kernel was compiled with 356562369db2SQuentin Monnet * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 356662369db2SQuentin Monnet * "**y**". 356762369db2SQuentin Monnet * Return 356862369db2SQuentin Monnet * 0 356962369db2SQuentin Monnet * 3570bdb7b79bSAndrii Nakryiko * long bpf_rc_keydown(void *ctx, u32 protocol, u64 scancode, u32 toggle) 3571f4364dcfSSean Young * Description 3572f4364dcfSSean Young * This helper is used in programs implementing IR decoding, to 3573f4364dcfSSean Young * report a successfully decoded key press with *scancode*, 3574f4364dcfSSean Young * *toggle* value in the given *protocol*. The scancode will be 3575f4364dcfSSean Young * translated to a keycode using the rc keymap, and reported as 3576f4364dcfSSean Young * an input key down event. After a period a key up event is 3577f4364dcfSSean Young * generated. This period can be extended by calling either 357890b1023fSQuentin Monnet * **bpf_rc_keydown**\ () again with the same values, or calling 357990b1023fSQuentin Monnet * **bpf_rc_repeat**\ (). 3580f4364dcfSSean Young * 3581f4364dcfSSean Young * Some protocols include a toggle bit, in case the button was 3582f4364dcfSSean Young * released and pressed again between consecutive scancodes. 3583f4364dcfSSean Young * 3584f4364dcfSSean Young * The *ctx* should point to the lirc sample as passed into 3585f4364dcfSSean Young * the program. 3586f4364dcfSSean Young * 3587f4364dcfSSean Young * The *protocol* is the decoded protocol number (see 3588f4364dcfSSean Young * **enum rc_proto** for some predefined values). 3589f4364dcfSSean Young * 3590f4364dcfSSean Young * This helper is only available is the kernel was compiled with 3591f4364dcfSSean Young * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 3592f4364dcfSSean Young * "**y**". 3593f4364dcfSSean Young * Return 3594f4364dcfSSean Young * 0 3595f4364dcfSSean Young * 359662369db2SQuentin Monnet * u64 bpf_skb_cgroup_id(struct sk_buff *skb) 3597cb20b08eSDaniel Borkmann * Description 3598cb20b08eSDaniel Borkmann * Return the cgroup v2 id of the socket associated with the *skb*. 3599cb20b08eSDaniel Borkmann * This is roughly similar to the **bpf_get_cgroup_classid**\ () 3600cb20b08eSDaniel Borkmann * helper for cgroup v1 by providing a tag resp. identifier that 3601cb20b08eSDaniel Borkmann * can be matched on or used for map lookups e.g. to implement 3602cb20b08eSDaniel Borkmann * policy. The cgroup v2 id of a given path in the hierarchy is 3603cb20b08eSDaniel Borkmann * exposed in user space through the f_handle API in order to get 3604cb20b08eSDaniel Borkmann * to the same 64-bit id. 3605cb20b08eSDaniel Borkmann * 3606cb20b08eSDaniel Borkmann * This helper can be used on TC egress path, but not on ingress, 3607cb20b08eSDaniel Borkmann * and is available only if the kernel was compiled with the 3608cb20b08eSDaniel Borkmann * **CONFIG_SOCK_CGROUP_DATA** configuration option. 3609cb20b08eSDaniel Borkmann * Return 3610cb20b08eSDaniel Borkmann * The id is returned or 0 in case the id could not be retrieved. 3611bf6fa2c8SYonghong Song * 3612bf6fa2c8SYonghong Song * u64 bpf_get_current_cgroup_id(void) 3613e40fbbf0SUsama Arif * Description 3614e40fbbf0SUsama Arif * Get the current cgroup id based on the cgroup within which 3615e40fbbf0SUsama Arif * the current task is running. 3616bf6fa2c8SYonghong Song * Return 3617bf6fa2c8SYonghong Song * A 64-bit integer containing the current cgroup id based 3618bf6fa2c8SYonghong Song * on the cgroup within which the current task is running. 3619cd339431SRoman Gushchin * 362062369db2SQuentin Monnet * void *bpf_get_local_storage(void *map, u64 flags) 3621cd339431SRoman Gushchin * Description 3622cd339431SRoman Gushchin * Get the pointer to the local storage area. 3623cd339431SRoman Gushchin * The type and the size of the local storage is defined 3624cd339431SRoman Gushchin * by the *map* argument. 3625cd339431SRoman Gushchin * The *flags* meaning is specific for each map type, 3626cd339431SRoman Gushchin * and has to be 0 for cgroup local storage. 3627cd339431SRoman Gushchin * 362890b1023fSQuentin Monnet * Depending on the BPF program type, a local storage area 362990b1023fSQuentin Monnet * can be shared between multiple instances of the BPF program, 3630cd339431SRoman Gushchin * running simultaneously. 3631cd339431SRoman Gushchin * 3632cd339431SRoman Gushchin * A user should care about the synchronization by himself. 363391c960b0SBrendan Jackman * For example, by using the **BPF_ATOMIC** instructions to alter 3634cd339431SRoman Gushchin * the shared data. 3635cd339431SRoman Gushchin * Return 363690b1023fSQuentin Monnet * A pointer to the local storage area. 36372dbb9b9eSMartin KaFai Lau * 3638bdb7b79bSAndrii Nakryiko * long bpf_sk_select_reuseport(struct sk_reuseport_md *reuse, struct bpf_map *map, void *key, u64 flags) 36392dbb9b9eSMartin KaFai Lau * Description 364090b1023fSQuentin Monnet * Select a **SO_REUSEPORT** socket from a 3641f170acdaSKuniyuki Iwashima * **BPF_MAP_TYPE_REUSEPORT_SOCKARRAY** *map*. 364290b1023fSQuentin Monnet * It checks the selected socket is matching the incoming 364390b1023fSQuentin Monnet * request in the socket buffer. 36442dbb9b9eSMartin KaFai Lau * Return 36452dbb9b9eSMartin KaFai Lau * 0 on success, or a negative error in case of failure. 36466acc9b43SJoe Stringer * 364762369db2SQuentin Monnet * u64 bpf_skb_ancestor_cgroup_id(struct sk_buff *skb, int ancestor_level) 364862369db2SQuentin Monnet * Description 364962369db2SQuentin Monnet * Return id of cgroup v2 that is ancestor of cgroup associated 365062369db2SQuentin Monnet * with the *skb* at the *ancestor_level*. The root cgroup is at 365162369db2SQuentin Monnet * *ancestor_level* zero and each step down the hierarchy 365262369db2SQuentin Monnet * increments the level. If *ancestor_level* == level of cgroup 365362369db2SQuentin Monnet * associated with *skb*, then return value will be same as that 365462369db2SQuentin Monnet * of **bpf_skb_cgroup_id**\ (). 365562369db2SQuentin Monnet * 365662369db2SQuentin Monnet * The helper is useful to implement policies based on cgroups 365762369db2SQuentin Monnet * that are upper in hierarchy than immediate cgroup associated 365862369db2SQuentin Monnet * with *skb*. 365962369db2SQuentin Monnet * 366062369db2SQuentin Monnet * The format of returned id and helper limitations are same as in 366162369db2SQuentin Monnet * **bpf_skb_cgroup_id**\ (). 366262369db2SQuentin Monnet * Return 366362369db2SQuentin Monnet * The id is returned or 0 in case the id could not be retrieved. 366462369db2SQuentin Monnet * 3665f71c6143SJoe Stringer * struct bpf_sock *bpf_sk_lookup_tcp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 36666acc9b43SJoe Stringer * Description 36676acc9b43SJoe Stringer * Look for TCP socket matching *tuple*, optionally in a child 36686acc9b43SJoe Stringer * network namespace *netns*. The return value must be checked, 366990b1023fSQuentin Monnet * and if non-**NULL**, released via **bpf_sk_release**\ (). 36706acc9b43SJoe Stringer * 36716acc9b43SJoe Stringer * The *ctx* should point to the context of the program, such as 36726acc9b43SJoe Stringer * the skb or socket (depending on the hook in use). This is used 36736acc9b43SJoe Stringer * to determine the base network namespace for the lookup. 36746acc9b43SJoe Stringer * 36756acc9b43SJoe Stringer * *tuple_size* must be one of: 36766acc9b43SJoe Stringer * 36776acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv4**) 36786acc9b43SJoe Stringer * Look for an IPv4 socket. 36796acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv6**) 36806acc9b43SJoe Stringer * Look for an IPv6 socket. 36816acc9b43SJoe Stringer * 3682f71c6143SJoe Stringer * If the *netns* is a negative signed 32-bit integer, then the 3683bfdfa517SRandy Dunlap * socket lookup table in the netns associated with the *ctx* 3684f71c6143SJoe Stringer * will be used. For the TC hooks, this is the netns of the device 3685f71c6143SJoe Stringer * in the skb. For socket hooks, this is the netns of the socket. 3686f71c6143SJoe Stringer * If *netns* is any other signed 32-bit value greater than or 3687f71c6143SJoe Stringer * equal to zero then it specifies the ID of the netns relative to 3688f71c6143SJoe Stringer * the netns associated with the *ctx*. *netns* values beyond the 3689f71c6143SJoe Stringer * range of 32-bit integers are reserved for future use. 36906acc9b43SJoe Stringer * 36916acc9b43SJoe Stringer * All values for *flags* are reserved for future usage, and must 36926acc9b43SJoe Stringer * be left at zero. 36936acc9b43SJoe Stringer * 36946acc9b43SJoe Stringer * This helper is available only if the kernel was compiled with 36956acc9b43SJoe Stringer * **CONFIG_NET** configuration option. 36966acc9b43SJoe Stringer * Return 36970bd72117SDaniel Borkmann * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 36980bd72117SDaniel Borkmann * For sockets with reuseport option, the **struct bpf_sock** 369980867c5eSQuentin Monnet * result is from *reuse*\ **->socks**\ [] using the hash of the 370080867c5eSQuentin Monnet * tuple. 37016acc9b43SJoe Stringer * 3702f71c6143SJoe Stringer * struct bpf_sock *bpf_sk_lookup_udp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 37036acc9b43SJoe Stringer * Description 37046acc9b43SJoe Stringer * Look for UDP socket matching *tuple*, optionally in a child 37056acc9b43SJoe Stringer * network namespace *netns*. The return value must be checked, 370690b1023fSQuentin Monnet * and if non-**NULL**, released via **bpf_sk_release**\ (). 37076acc9b43SJoe Stringer * 37086acc9b43SJoe Stringer * The *ctx* should point to the context of the program, such as 37096acc9b43SJoe Stringer * the skb or socket (depending on the hook in use). This is used 37106acc9b43SJoe Stringer * to determine the base network namespace for the lookup. 37116acc9b43SJoe Stringer * 37126acc9b43SJoe Stringer * *tuple_size* must be one of: 37136acc9b43SJoe Stringer * 37146acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv4**) 37156acc9b43SJoe Stringer * Look for an IPv4 socket. 37166acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv6**) 37176acc9b43SJoe Stringer * Look for an IPv6 socket. 37186acc9b43SJoe Stringer * 3719f71c6143SJoe Stringer * If the *netns* is a negative signed 32-bit integer, then the 3720bfdfa517SRandy Dunlap * socket lookup table in the netns associated with the *ctx* 3721f71c6143SJoe Stringer * will be used. For the TC hooks, this is the netns of the device 3722f71c6143SJoe Stringer * in the skb. For socket hooks, this is the netns of the socket. 3723f71c6143SJoe Stringer * If *netns* is any other signed 32-bit value greater than or 3724f71c6143SJoe Stringer * equal to zero then it specifies the ID of the netns relative to 3725f71c6143SJoe Stringer * the netns associated with the *ctx*. *netns* values beyond the 3726f71c6143SJoe Stringer * range of 32-bit integers are reserved for future use. 37276acc9b43SJoe Stringer * 37286acc9b43SJoe Stringer * All values for *flags* are reserved for future usage, and must 37296acc9b43SJoe Stringer * be left at zero. 37306acc9b43SJoe Stringer * 37316acc9b43SJoe Stringer * This helper is available only if the kernel was compiled with 37326acc9b43SJoe Stringer * **CONFIG_NET** configuration option. 37336acc9b43SJoe Stringer * Return 37340bd72117SDaniel Borkmann * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 37350bd72117SDaniel Borkmann * For sockets with reuseport option, the **struct bpf_sock** 373680867c5eSQuentin Monnet * result is from *reuse*\ **->socks**\ [] using the hash of the 373780867c5eSQuentin Monnet * tuple. 37386acc9b43SJoe Stringer * 3739a5fa25adSMartin KaFai Lau * long bpf_sk_release(void *sock) 37406acc9b43SJoe Stringer * Description 374190b1023fSQuentin Monnet * Release the reference held by *sock*. *sock* must be a 374290b1023fSQuentin Monnet * non-**NULL** pointer that was returned from 374390b1023fSQuentin Monnet * **bpf_sk_lookup_xxx**\ (). 374490b1023fSQuentin Monnet * Return 374590b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 374690b1023fSQuentin Monnet * 3747bdb7b79bSAndrii Nakryiko * long bpf_map_push_elem(struct bpf_map *map, const void *value, u64 flags) 374862369db2SQuentin Monnet * Description 374962369db2SQuentin Monnet * Push an element *value* in *map*. *flags* is one of: 375062369db2SQuentin Monnet * 375162369db2SQuentin Monnet * **BPF_EXIST** 375262369db2SQuentin Monnet * If the queue/stack is full, the oldest element is 375362369db2SQuentin Monnet * removed to make room for this. 375462369db2SQuentin Monnet * Return 375562369db2SQuentin Monnet * 0 on success, or a negative error in case of failure. 375662369db2SQuentin Monnet * 3757bdb7b79bSAndrii Nakryiko * long bpf_map_pop_elem(struct bpf_map *map, void *value) 375890b1023fSQuentin Monnet * Description 375990b1023fSQuentin Monnet * Pop an element from *map*. 376090b1023fSQuentin Monnet * Return 376190b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 376290b1023fSQuentin Monnet * 3763bdb7b79bSAndrii Nakryiko * long bpf_map_peek_elem(struct bpf_map *map, void *value) 376490b1023fSQuentin Monnet * Description 376590b1023fSQuentin Monnet * Get an element from *map* without removing it. 37666acc9b43SJoe Stringer * Return 37676acc9b43SJoe Stringer * 0 on success, or a negative error in case of failure. 37686fff607eSJohn Fastabend * 3769bdb7b79bSAndrii Nakryiko * long bpf_msg_push_data(struct sk_msg_buff *msg, u32 start, u32 len, u64 flags) 37706fff607eSJohn Fastabend * Description 377190b1023fSQuentin Monnet * For socket policies, insert *len* bytes into *msg* at offset 37726fff607eSJohn Fastabend * *start*. 37736fff607eSJohn Fastabend * 37746fff607eSJohn Fastabend * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a 377590b1023fSQuentin Monnet * *msg* it may want to insert metadata or options into the *msg*. 37766fff607eSJohn Fastabend * This can later be read and used by any of the lower layer BPF 37776fff607eSJohn Fastabend * hooks. 37786fff607eSJohn Fastabend * 37796fff607eSJohn Fastabend * This helper may fail if under memory pressure (a malloc 37806fff607eSJohn Fastabend * fails) in these cases BPF programs will get an appropriate 37816fff607eSJohn Fastabend * error and BPF programs will need to handle them. 37826fff607eSJohn Fastabend * Return 37836fff607eSJohn Fastabend * 0 on success, or a negative error in case of failure. 37847246d8edSJohn Fastabend * 3785bdb7b79bSAndrii Nakryiko * long bpf_msg_pop_data(struct sk_msg_buff *msg, u32 start, u32 len, u64 flags) 37867246d8edSJohn Fastabend * Description 37875f0e5412SAndrii Nakryiko * Will remove *len* bytes from a *msg* starting at byte *start*. 37887246d8edSJohn Fastabend * This may result in **ENOMEM** errors under certain situations if 37897246d8edSJohn Fastabend * an allocation and copy are required due to a full ring buffer. 37907246d8edSJohn Fastabend * However, the helper will try to avoid doing the allocation 37917246d8edSJohn Fastabend * if possible. Other errors can occur if input parameters are 379290b1023fSQuentin Monnet * invalid either due to *start* byte not being valid part of *msg* 37937246d8edSJohn Fastabend * payload and/or *pop* value being to large. 37947246d8edSJohn Fastabend * Return 379590b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 379601d3240aSSean Young * 3797bdb7b79bSAndrii Nakryiko * long bpf_rc_pointer_rel(void *ctx, s32 rel_x, s32 rel_y) 379801d3240aSSean Young * Description 379901d3240aSSean Young * This helper is used in programs implementing IR decoding, to 380001d3240aSSean Young * report a successfully decoded pointer movement. 380101d3240aSSean Young * 380201d3240aSSean Young * The *ctx* should point to the lirc sample as passed into 380301d3240aSSean Young * the program. 380401d3240aSSean Young * 380501d3240aSSean Young * This helper is only available is the kernel was compiled with 380601d3240aSSean Young * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 380701d3240aSSean Young * "**y**". 380801d3240aSSean Young * Return 380901d3240aSSean Young * 0 381046f8bc92SMartin KaFai Lau * 3811bdb7b79bSAndrii Nakryiko * long bpf_spin_lock(struct bpf_spin_lock *lock) 38120eb09785SQuentin Monnet * Description 38130eb09785SQuentin Monnet * Acquire a spinlock represented by the pointer *lock*, which is 38140eb09785SQuentin Monnet * stored as part of a value of a map. Taking the lock allows to 38150eb09785SQuentin Monnet * safely update the rest of the fields in that value. The 38160eb09785SQuentin Monnet * spinlock can (and must) later be released with a call to 38170eb09785SQuentin Monnet * **bpf_spin_unlock**\ (\ *lock*\ ). 38180eb09785SQuentin Monnet * 38190eb09785SQuentin Monnet * Spinlocks in BPF programs come with a number of restrictions 38200eb09785SQuentin Monnet * and constraints: 38210eb09785SQuentin Monnet * 38220eb09785SQuentin Monnet * * **bpf_spin_lock** objects are only allowed inside maps of 38230eb09785SQuentin Monnet * types **BPF_MAP_TYPE_HASH** and **BPF_MAP_TYPE_ARRAY** (this 38240eb09785SQuentin Monnet * list could be extended in the future). 38250eb09785SQuentin Monnet * * BTF description of the map is mandatory. 38260eb09785SQuentin Monnet * * The BPF program can take ONE lock at a time, since taking two 38270eb09785SQuentin Monnet * or more could cause dead locks. 38280eb09785SQuentin Monnet * * Only one **struct bpf_spin_lock** is allowed per map element. 38290eb09785SQuentin Monnet * * When the lock is taken, calls (either BPF to BPF or helpers) 38300eb09785SQuentin Monnet * are not allowed. 38310eb09785SQuentin Monnet * * The **BPF_LD_ABS** and **BPF_LD_IND** instructions are not 38320eb09785SQuentin Monnet * allowed inside a spinlock-ed region. 38330eb09785SQuentin Monnet * * The BPF program MUST call **bpf_spin_unlock**\ () to release 38340eb09785SQuentin Monnet * the lock, on all execution paths, before it returns. 38350eb09785SQuentin Monnet * * The BPF program can access **struct bpf_spin_lock** only via 38360eb09785SQuentin Monnet * the **bpf_spin_lock**\ () and **bpf_spin_unlock**\ () 38370eb09785SQuentin Monnet * helpers. Loading or storing data into the **struct 38380eb09785SQuentin Monnet * bpf_spin_lock** *lock*\ **;** field of a map is not allowed. 38390eb09785SQuentin Monnet * * To use the **bpf_spin_lock**\ () helper, the BTF description 38400eb09785SQuentin Monnet * of the map value must be a struct and have **struct 38410eb09785SQuentin Monnet * bpf_spin_lock** *anyname*\ **;** field at the top level. 38420eb09785SQuentin Monnet * Nested lock inside another struct is not allowed. 38430eb09785SQuentin Monnet * * The **struct bpf_spin_lock** *lock* field in a map value must 38440eb09785SQuentin Monnet * be aligned on a multiple of 4 bytes in that value. 38450eb09785SQuentin Monnet * * Syscall with command **BPF_MAP_LOOKUP_ELEM** does not copy 38460eb09785SQuentin Monnet * the **bpf_spin_lock** field to user space. 38470eb09785SQuentin Monnet * * Syscall with command **BPF_MAP_UPDATE_ELEM**, or update from 38480eb09785SQuentin Monnet * a BPF program, do not update the **bpf_spin_lock** field. 38490eb09785SQuentin Monnet * * **bpf_spin_lock** cannot be on the stack or inside a 38500eb09785SQuentin Monnet * networking packet (it can only be inside of a map values). 38510eb09785SQuentin Monnet * * **bpf_spin_lock** is available to root only. 38520eb09785SQuentin Monnet * * Tracing programs and socket filter programs cannot use 38530eb09785SQuentin Monnet * **bpf_spin_lock**\ () due to insufficient preemption checks 38540eb09785SQuentin Monnet * (but this may change in the future). 38550eb09785SQuentin Monnet * * **bpf_spin_lock** is not allowed in inner maps of map-in-map. 38560eb09785SQuentin Monnet * Return 38570eb09785SQuentin Monnet * 0 38580eb09785SQuentin Monnet * 3859bdb7b79bSAndrii Nakryiko * long bpf_spin_unlock(struct bpf_spin_lock *lock) 38600eb09785SQuentin Monnet * Description 38610eb09785SQuentin Monnet * Release the *lock* previously locked by a call to 38620eb09785SQuentin Monnet * **bpf_spin_lock**\ (\ *lock*\ ). 38630eb09785SQuentin Monnet * Return 38640eb09785SQuentin Monnet * 0 38650eb09785SQuentin Monnet * 386646f8bc92SMartin KaFai Lau * struct bpf_sock *bpf_sk_fullsock(struct bpf_sock *sk) 386746f8bc92SMartin KaFai Lau * Description 386846f8bc92SMartin KaFai Lau * This helper gets a **struct bpf_sock** pointer such 386962369db2SQuentin Monnet * that all the fields in this **bpf_sock** can be accessed. 387046f8bc92SMartin KaFai Lau * Return 387162369db2SQuentin Monnet * A **struct bpf_sock** pointer on success, or **NULL** in 387246f8bc92SMartin KaFai Lau * case of failure. 3873655a51e5SMartin KaFai Lau * 3874655a51e5SMartin KaFai Lau * struct bpf_tcp_sock *bpf_tcp_sock(struct bpf_sock *sk) 3875655a51e5SMartin KaFai Lau * Description 3876655a51e5SMartin KaFai Lau * This helper gets a **struct bpf_tcp_sock** pointer from a 3877655a51e5SMartin KaFai Lau * **struct bpf_sock** pointer. 3878655a51e5SMartin KaFai Lau * Return 387962369db2SQuentin Monnet * A **struct bpf_tcp_sock** pointer on success, or **NULL** in 3880655a51e5SMartin KaFai Lau * case of failure. 3881f7c917baSbrakmo * 3882bdb7b79bSAndrii Nakryiko * long bpf_skb_ecn_set_ce(struct sk_buff *skb) 3883f7c917baSbrakmo * Description 388462369db2SQuentin Monnet * Set ECN (Explicit Congestion Notification) field of IP header 388562369db2SQuentin Monnet * to **CE** (Congestion Encountered) if current value is **ECT** 388662369db2SQuentin Monnet * (ECN Capable Transport). Otherwise, do nothing. Works with IPv6 388762369db2SQuentin Monnet * and IPv4. 3888f7c917baSbrakmo * Return 388962369db2SQuentin Monnet * 1 if the **CE** flag is set (either by the current helper call 389062369db2SQuentin Monnet * or because it was already present), 0 if it is not set. 3891dbafd7ddSMartin KaFai Lau * 3892dbafd7ddSMartin KaFai Lau * struct bpf_sock *bpf_get_listener_sock(struct bpf_sock *sk) 3893dbafd7ddSMartin KaFai Lau * Description 389462369db2SQuentin Monnet * Return a **struct bpf_sock** pointer in **TCP_LISTEN** state. 389562369db2SQuentin Monnet * **bpf_sk_release**\ () is unnecessary and not allowed. 3896dbafd7ddSMartin KaFai Lau * Return 389762369db2SQuentin Monnet * A **struct bpf_sock** pointer on success, or **NULL** in 3898dbafd7ddSMartin KaFai Lau * case of failure. 3899edbf8c01SLorenz Bauer * 3900edbf8c01SLorenz Bauer * struct bpf_sock *bpf_skc_lookup_tcp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 3901edbf8c01SLorenz Bauer * Description 3902edbf8c01SLorenz Bauer * Look for TCP socket matching *tuple*, optionally in a child 3903edbf8c01SLorenz Bauer * network namespace *netns*. The return value must be checked, 3904edbf8c01SLorenz Bauer * and if non-**NULL**, released via **bpf_sk_release**\ (). 3905edbf8c01SLorenz Bauer * 390680867c5eSQuentin Monnet * This function is identical to **bpf_sk_lookup_tcp**\ (), except 390780867c5eSQuentin Monnet * that it also returns timewait or request sockets. Use 390880867c5eSQuentin Monnet * **bpf_sk_fullsock**\ () or **bpf_tcp_sock**\ () to access the 390980867c5eSQuentin Monnet * full structure. 3910edbf8c01SLorenz Bauer * 3911edbf8c01SLorenz Bauer * This helper is available only if the kernel was compiled with 3912edbf8c01SLorenz Bauer * **CONFIG_NET** configuration option. 3913edbf8c01SLorenz Bauer * Return 3914edbf8c01SLorenz Bauer * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 3915edbf8c01SLorenz Bauer * For sockets with reuseport option, the **struct bpf_sock** 391680867c5eSQuentin Monnet * result is from *reuse*\ **->socks**\ [] using the hash of the 391780867c5eSQuentin Monnet * tuple. 391839904084SLorenz Bauer * 3919c0df236eSMartin KaFai Lau * long bpf_tcp_check_syncookie(void *sk, void *iph, u32 iph_len, struct tcphdr *th, u32 th_len) 392039904084SLorenz Bauer * Description 392180867c5eSQuentin Monnet * Check whether *iph* and *th* contain a valid SYN cookie ACK for 392280867c5eSQuentin Monnet * the listening socket in *sk*. 392339904084SLorenz Bauer * 392480867c5eSQuentin Monnet * *iph* points to the start of the IPv4 or IPv6 header, while 392580867c5eSQuentin Monnet * *iph_len* contains **sizeof**\ (**struct iphdr**) or 3926ac80287aSMaxim Mikityanskiy * **sizeof**\ (**struct ipv6hdr**). 392739904084SLorenz Bauer * 392880867c5eSQuentin Monnet * *th* points to the start of the TCP header, while *th_len* 3929ac80287aSMaxim Mikityanskiy * contains the length of the TCP header (at least 3930ac80287aSMaxim Mikityanskiy * **sizeof**\ (**struct tcphdr**)). 393139904084SLorenz Bauer * Return 393280867c5eSQuentin Monnet * 0 if *iph* and *th* are a valid SYN cookie ACK, or a negative 393380867c5eSQuentin Monnet * error otherwise. 3934808649fbSAndrey Ignatov * 3935bdb7b79bSAndrii Nakryiko * long bpf_sysctl_get_name(struct bpf_sysctl *ctx, char *buf, size_t buf_len, u64 flags) 3936808649fbSAndrey Ignatov * Description 3937808649fbSAndrey Ignatov * Get name of sysctl in /proc/sys/ and copy it into provided by 3938808649fbSAndrey Ignatov * program buffer *buf* of size *buf_len*. 3939808649fbSAndrey Ignatov * 3940808649fbSAndrey Ignatov * The buffer is always NUL terminated, unless it's zero-sized. 3941808649fbSAndrey Ignatov * 3942808649fbSAndrey Ignatov * If *flags* is zero, full name (e.g. "net/ipv4/tcp_mem") is 3943808649fbSAndrey Ignatov * copied. Use **BPF_F_SYSCTL_BASE_NAME** flag to copy base name 3944808649fbSAndrey Ignatov * only (e.g. "tcp_mem"). 3945808649fbSAndrey Ignatov * Return 3946808649fbSAndrey Ignatov * Number of character copied (not including the trailing NUL). 3947808649fbSAndrey Ignatov * 3948808649fbSAndrey Ignatov * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 3949808649fbSAndrey Ignatov * truncated name in this case). 39501d11b301SAndrey Ignatov * 3951bdb7b79bSAndrii Nakryiko * long bpf_sysctl_get_current_value(struct bpf_sysctl *ctx, char *buf, size_t buf_len) 39521d11b301SAndrey Ignatov * Description 39531d11b301SAndrey Ignatov * Get current value of sysctl as it is presented in /proc/sys 39541d11b301SAndrey Ignatov * (incl. newline, etc), and copy it as a string into provided 39551d11b301SAndrey Ignatov * by program buffer *buf* of size *buf_len*. 39561d11b301SAndrey Ignatov * 39571d11b301SAndrey Ignatov * The whole value is copied, no matter what file position user 39581d11b301SAndrey Ignatov * space issued e.g. sys_read at. 39591d11b301SAndrey Ignatov * 39601d11b301SAndrey Ignatov * The buffer is always NUL terminated, unless it's zero-sized. 39611d11b301SAndrey Ignatov * Return 39621d11b301SAndrey Ignatov * Number of character copied (not including the trailing NUL). 39631d11b301SAndrey Ignatov * 39641d11b301SAndrey Ignatov * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 39651d11b301SAndrey Ignatov * truncated name in this case). 39661d11b301SAndrey Ignatov * 39671d11b301SAndrey Ignatov * **-EINVAL** if current value was unavailable, e.g. because 39681d11b301SAndrey Ignatov * sysctl is uninitialized and read returns -EIO for it. 39694e63acdfSAndrey Ignatov * 3970bdb7b79bSAndrii Nakryiko * long bpf_sysctl_get_new_value(struct bpf_sysctl *ctx, char *buf, size_t buf_len) 39714e63acdfSAndrey Ignatov * Description 39724e63acdfSAndrey Ignatov * Get new value being written by user space to sysctl (before 39734e63acdfSAndrey Ignatov * the actual write happens) and copy it as a string into 39744e63acdfSAndrey Ignatov * provided by program buffer *buf* of size *buf_len*. 39754e63acdfSAndrey Ignatov * 39764e63acdfSAndrey Ignatov * User space may write new value at file position > 0. 39774e63acdfSAndrey Ignatov * 39784e63acdfSAndrey Ignatov * The buffer is always NUL terminated, unless it's zero-sized. 39794e63acdfSAndrey Ignatov * Return 39804e63acdfSAndrey Ignatov * Number of character copied (not including the trailing NUL). 39814e63acdfSAndrey Ignatov * 39824e63acdfSAndrey Ignatov * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 39834e63acdfSAndrey Ignatov * truncated name in this case). 39844e63acdfSAndrey Ignatov * 39854e63acdfSAndrey Ignatov * **-EINVAL** if sysctl is being read. 39864e63acdfSAndrey Ignatov * 3987bdb7b79bSAndrii Nakryiko * long bpf_sysctl_set_new_value(struct bpf_sysctl *ctx, const char *buf, size_t buf_len) 39884e63acdfSAndrey Ignatov * Description 39894e63acdfSAndrey Ignatov * Override new value being written by user space to sysctl with 39904e63acdfSAndrey Ignatov * value provided by program in buffer *buf* of size *buf_len*. 39914e63acdfSAndrey Ignatov * 39924e63acdfSAndrey Ignatov * *buf* should contain a string in same form as provided by user 39934e63acdfSAndrey Ignatov * space on sysctl write. 39944e63acdfSAndrey Ignatov * 39954e63acdfSAndrey Ignatov * User space may write new value at file position > 0. To override 39964e63acdfSAndrey Ignatov * the whole sysctl value file position should be set to zero. 39974e63acdfSAndrey Ignatov * Return 39984e63acdfSAndrey Ignatov * 0 on success. 39994e63acdfSAndrey Ignatov * 40004e63acdfSAndrey Ignatov * **-E2BIG** if the *buf_len* is too big. 40014e63acdfSAndrey Ignatov * 40024e63acdfSAndrey Ignatov * **-EINVAL** if sysctl is being read. 4003d7a4cb9bSAndrey Ignatov * 4004bdb7b79bSAndrii Nakryiko * long bpf_strtol(const char *buf, size_t buf_len, u64 flags, long *res) 4005d7a4cb9bSAndrey Ignatov * Description 4006d7a4cb9bSAndrey Ignatov * Convert the initial part of the string from buffer *buf* of 4007d7a4cb9bSAndrey Ignatov * size *buf_len* to a long integer according to the given base 4008d7a4cb9bSAndrey Ignatov * and save the result in *res*. 4009d7a4cb9bSAndrey Ignatov * 4010d7a4cb9bSAndrey Ignatov * The string may begin with an arbitrary amount of white space 401180867c5eSQuentin Monnet * (as determined by **isspace**\ (3)) followed by a single 401280867c5eSQuentin Monnet * optional '**-**' sign. 4013d7a4cb9bSAndrey Ignatov * 4014d7a4cb9bSAndrey Ignatov * Five least significant bits of *flags* encode base, other bits 4015d7a4cb9bSAndrey Ignatov * are currently unused. 4016d7a4cb9bSAndrey Ignatov * 4017d7a4cb9bSAndrey Ignatov * Base must be either 8, 10, 16 or 0 to detect it automatically 401880867c5eSQuentin Monnet * similar to user space **strtol**\ (3). 4019d7a4cb9bSAndrey Ignatov * Return 4020d7a4cb9bSAndrey Ignatov * Number of characters consumed on success. Must be positive but 402180867c5eSQuentin Monnet * no more than *buf_len*. 4022d7a4cb9bSAndrey Ignatov * 4023d7a4cb9bSAndrey Ignatov * **-EINVAL** if no valid digits were found or unsupported base 4024d7a4cb9bSAndrey Ignatov * was provided. 4025d7a4cb9bSAndrey Ignatov * 4026d7a4cb9bSAndrey Ignatov * **-ERANGE** if resulting value was out of range. 4027d7a4cb9bSAndrey Ignatov * 4028bdb7b79bSAndrii Nakryiko * long bpf_strtoul(const char *buf, size_t buf_len, u64 flags, unsigned long *res) 4029d7a4cb9bSAndrey Ignatov * Description 4030d7a4cb9bSAndrey Ignatov * Convert the initial part of the string from buffer *buf* of 4031d7a4cb9bSAndrey Ignatov * size *buf_len* to an unsigned long integer according to the 4032d7a4cb9bSAndrey Ignatov * given base and save the result in *res*. 4033d7a4cb9bSAndrey Ignatov * 4034d7a4cb9bSAndrey Ignatov * The string may begin with an arbitrary amount of white space 403580867c5eSQuentin Monnet * (as determined by **isspace**\ (3)). 4036d7a4cb9bSAndrey Ignatov * 4037d7a4cb9bSAndrey Ignatov * Five least significant bits of *flags* encode base, other bits 4038d7a4cb9bSAndrey Ignatov * are currently unused. 4039d7a4cb9bSAndrey Ignatov * 4040d7a4cb9bSAndrey Ignatov * Base must be either 8, 10, 16 or 0 to detect it automatically 404180867c5eSQuentin Monnet * similar to user space **strtoul**\ (3). 4042d7a4cb9bSAndrey Ignatov * Return 4043d7a4cb9bSAndrey Ignatov * Number of characters consumed on success. Must be positive but 404480867c5eSQuentin Monnet * no more than *buf_len*. 4045d7a4cb9bSAndrey Ignatov * 4046d7a4cb9bSAndrey Ignatov * **-EINVAL** if no valid digits were found or unsupported base 4047d7a4cb9bSAndrey Ignatov * was provided. 4048d7a4cb9bSAndrey Ignatov * 4049d7a4cb9bSAndrey Ignatov * **-ERANGE** if resulting value was out of range. 40506ac99e8fSMartin KaFai Lau * 405130897832SKP Singh * void *bpf_sk_storage_get(struct bpf_map *map, void *sk, void *value, u64 flags) 40526ac99e8fSMartin KaFai Lau * Description 405380867c5eSQuentin Monnet * Get a bpf-local-storage from a *sk*. 40546ac99e8fSMartin KaFai Lau * 40556ac99e8fSMartin KaFai Lau * Logically, it could be thought of getting the value from 40566ac99e8fSMartin KaFai Lau * a *map* with *sk* as the **key**. From this 40576ac99e8fSMartin KaFai Lau * perspective, the usage is not much different from 405880867c5eSQuentin Monnet * **bpf_map_lookup_elem**\ (*map*, **&**\ *sk*) except this 405980867c5eSQuentin Monnet * helper enforces the key must be a full socket and the map must 406080867c5eSQuentin Monnet * be a **BPF_MAP_TYPE_SK_STORAGE** also. 40616ac99e8fSMartin KaFai Lau * 40626ac99e8fSMartin KaFai Lau * Underneath, the value is stored locally at *sk* instead of 406380867c5eSQuentin Monnet * the *map*. The *map* is used as the bpf-local-storage 406480867c5eSQuentin Monnet * "type". The bpf-local-storage "type" (i.e. the *map*) is 406580867c5eSQuentin Monnet * searched against all bpf-local-storages residing at *sk*. 40666ac99e8fSMartin KaFai Lau * 406730897832SKP Singh * *sk* is a kernel **struct sock** pointer for LSM program. 406830897832SKP Singh * *sk* is a **struct bpf_sock** pointer for other program types. 406930897832SKP Singh * 407080867c5eSQuentin Monnet * An optional *flags* (**BPF_SK_STORAGE_GET_F_CREATE**) can be 40716ac99e8fSMartin KaFai Lau * used such that a new bpf-local-storage will be 40726ac99e8fSMartin KaFai Lau * created if one does not exist. *value* can be used 407380867c5eSQuentin Monnet * together with **BPF_SK_STORAGE_GET_F_CREATE** to specify 40746ac99e8fSMartin KaFai Lau * the initial value of a bpf-local-storage. If *value* is 407580867c5eSQuentin Monnet * **NULL**, the new bpf-local-storage will be zero initialized. 40766ac99e8fSMartin KaFai Lau * Return 40776ac99e8fSMartin KaFai Lau * A bpf-local-storage pointer is returned on success. 40786ac99e8fSMartin KaFai Lau * 40796ac99e8fSMartin KaFai Lau * **NULL** if not found or there was an error in adding 40806ac99e8fSMartin KaFai Lau * a new bpf-local-storage. 40816ac99e8fSMartin KaFai Lau * 408230897832SKP Singh * long bpf_sk_storage_delete(struct bpf_map *map, void *sk) 40836ac99e8fSMartin KaFai Lau * Description 408480867c5eSQuentin Monnet * Delete a bpf-local-storage from a *sk*. 40856ac99e8fSMartin KaFai Lau * Return 40866ac99e8fSMartin KaFai Lau * 0 on success. 40876ac99e8fSMartin KaFai Lau * 40886ac99e8fSMartin KaFai Lau * **-ENOENT** if the bpf-local-storage cannot be found. 4089592a3498SMartin KaFai Lau * **-EINVAL** if sk is not a fullsock (e.g. a request_sock). 40908b401f9eSYonghong Song * 4091bdb7b79bSAndrii Nakryiko * long bpf_send_signal(u32 sig) 40928b401f9eSYonghong Song * Description 40938482941fSYonghong Song * Send signal *sig* to the process of the current task. 40948482941fSYonghong Song * The signal may be delivered to any of this process's threads. 40958b401f9eSYonghong Song * Return 40968b401f9eSYonghong Song * 0 on success or successfully queued. 40978b401f9eSYonghong Song * 40988b401f9eSYonghong Song * **-EBUSY** if work queue under nmi is full. 40998b401f9eSYonghong Song * 41008b401f9eSYonghong Song * **-EINVAL** if *sig* is invalid. 41018b401f9eSYonghong Song * 41028b401f9eSYonghong Song * **-EPERM** if no permission to send the *sig*. 41038b401f9eSYonghong Song * 41048b401f9eSYonghong Song * **-EAGAIN** if bpf program can try again. 410570d66244SPetar Penkov * 4106c0df236eSMartin KaFai Lau * s64 bpf_tcp_gen_syncookie(void *sk, void *iph, u32 iph_len, struct tcphdr *th, u32 th_len) 410770d66244SPetar Penkov * Description 410870d66244SPetar Penkov * Try to issue a SYN cookie for the packet with corresponding 410970d66244SPetar Penkov * IP/TCP headers, *iph* and *th*, on the listening socket in *sk*. 411070d66244SPetar Penkov * 411170d66244SPetar Penkov * *iph* points to the start of the IPv4 or IPv6 header, while 411270d66244SPetar Penkov * *iph_len* contains **sizeof**\ (**struct iphdr**) or 4113ac80287aSMaxim Mikityanskiy * **sizeof**\ (**struct ipv6hdr**). 411470d66244SPetar Penkov * 411570d66244SPetar Penkov * *th* points to the start of the TCP header, while *th_len* 4116ac80287aSMaxim Mikityanskiy * contains the length of the TCP header with options (at least 4117ac80287aSMaxim Mikityanskiy * **sizeof**\ (**struct tcphdr**)). 411870d66244SPetar Penkov * Return 411970d66244SPetar Penkov * On success, lower 32 bits hold the generated SYN cookie in 412070d66244SPetar Penkov * followed by 16 bits which hold the MSS value for that cookie, 412170d66244SPetar Penkov * and the top 16 bits are unused. 412270d66244SPetar Penkov * 412370d66244SPetar Penkov * On failure, the returned value is one of the following: 412470d66244SPetar Penkov * 412570d66244SPetar Penkov * **-EINVAL** SYN cookie cannot be issued due to error 412670d66244SPetar Penkov * 412770d66244SPetar Penkov * **-ENOENT** SYN cookie should not be issued (no SYN flood) 412870d66244SPetar Penkov * 412970d66244SPetar Penkov * **-EOPNOTSUPP** kernel configuration does not enable SYN cookies 413070d66244SPetar Penkov * 413170d66244SPetar Penkov * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 4132a7658e1aSAlexei Starovoitov * 4133bdb7b79bSAndrii Nakryiko * long bpf_skb_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size) 4134a7658e1aSAlexei Starovoitov * Description 4135a7658e1aSAlexei Starovoitov * Write raw *data* blob into a special BPF perf event held by 4136a7658e1aSAlexei Starovoitov * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 4137a7658e1aSAlexei Starovoitov * event must have the following attributes: **PERF_SAMPLE_RAW** 4138a7658e1aSAlexei Starovoitov * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 4139a7658e1aSAlexei Starovoitov * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 4140a7658e1aSAlexei Starovoitov * 4141a7658e1aSAlexei Starovoitov * The *flags* are used to indicate the index in *map* for which 4142a7658e1aSAlexei Starovoitov * the value must be put, masked with **BPF_F_INDEX_MASK**. 4143a7658e1aSAlexei Starovoitov * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 4144a7658e1aSAlexei Starovoitov * to indicate that the index of the current CPU core should be 4145a7658e1aSAlexei Starovoitov * used. 4146a7658e1aSAlexei Starovoitov * 4147a7658e1aSAlexei Starovoitov * The value to write, of *size*, is passed through eBPF stack and 4148a7658e1aSAlexei Starovoitov * pointed by *data*. 4149a7658e1aSAlexei Starovoitov * 4150a7658e1aSAlexei Starovoitov * *ctx* is a pointer to in-kernel struct sk_buff. 4151a7658e1aSAlexei Starovoitov * 4152a7658e1aSAlexei Starovoitov * This helper is similar to **bpf_perf_event_output**\ () but 4153a7658e1aSAlexei Starovoitov * restricted to raw_tracepoint bpf programs. 4154a7658e1aSAlexei Starovoitov * Return 4155a7658e1aSAlexei Starovoitov * 0 on success, or a negative error in case of failure. 41566ae08ae3SDaniel Borkmann * 4157bdb7b79bSAndrii Nakryiko * long bpf_probe_read_user(void *dst, u32 size, const void *unsafe_ptr) 41586ae08ae3SDaniel Borkmann * Description 41596ae08ae3SDaniel Borkmann * Safely attempt to read *size* bytes from user space address 41606ae08ae3SDaniel Borkmann * *unsafe_ptr* and store the data in *dst*. 41616ae08ae3SDaniel Borkmann * Return 41626ae08ae3SDaniel Borkmann * 0 on success, or a negative error in case of failure. 41636ae08ae3SDaniel Borkmann * 4164bdb7b79bSAndrii Nakryiko * long bpf_probe_read_kernel(void *dst, u32 size, const void *unsafe_ptr) 41656ae08ae3SDaniel Borkmann * Description 41666ae08ae3SDaniel Borkmann * Safely attempt to read *size* bytes from kernel space address 41676ae08ae3SDaniel Borkmann * *unsafe_ptr* and store the data in *dst*. 41686ae08ae3SDaniel Borkmann * Return 41696ae08ae3SDaniel Borkmann * 0 on success, or a negative error in case of failure. 41706ae08ae3SDaniel Borkmann * 4171bdb7b79bSAndrii Nakryiko * long bpf_probe_read_user_str(void *dst, u32 size, const void *unsafe_ptr) 41726ae08ae3SDaniel Borkmann * Description 41736ae08ae3SDaniel Borkmann * Copy a NUL terminated string from an unsafe user address 41746ae08ae3SDaniel Borkmann * *unsafe_ptr* to *dst*. The *size* should include the 41756ae08ae3SDaniel Borkmann * terminating NUL byte. In case the string length is smaller than 41766ae08ae3SDaniel Borkmann * *size*, the target is not padded with further NUL bytes. If the 41776ae08ae3SDaniel Borkmann * string length is larger than *size*, just *size*-1 bytes are 41786ae08ae3SDaniel Borkmann * copied and the last byte is set to NUL. 41796ae08ae3SDaniel Borkmann * 4180c6458e72SBrendan Jackman * On success, returns the number of bytes that were written, 4181c6458e72SBrendan Jackman * including the terminal NUL. This makes this helper useful in 4182c6458e72SBrendan Jackman * tracing programs for reading strings, and more importantly to 4183c6458e72SBrendan Jackman * get its length at runtime. See the following snippet: 41846ae08ae3SDaniel Borkmann * 41856ae08ae3SDaniel Borkmann * :: 41866ae08ae3SDaniel Borkmann * 41876ae08ae3SDaniel Borkmann * SEC("kprobe/sys_open") 41886ae08ae3SDaniel Borkmann * void bpf_sys_open(struct pt_regs *ctx) 41896ae08ae3SDaniel Borkmann * { 41906ae08ae3SDaniel Borkmann * char buf[PATHLEN]; // PATHLEN is defined to 256 41916ae08ae3SDaniel Borkmann * int res = bpf_probe_read_user_str(buf, sizeof(buf), 41926ae08ae3SDaniel Borkmann * ctx->di); 41936ae08ae3SDaniel Borkmann * 41946ae08ae3SDaniel Borkmann * // Consume buf, for example push it to 41956ae08ae3SDaniel Borkmann * // userspace via bpf_perf_event_output(); we 41966ae08ae3SDaniel Borkmann * // can use res (the string length) as event 41976ae08ae3SDaniel Borkmann * // size, after checking its boundaries. 41986ae08ae3SDaniel Borkmann * } 41996ae08ae3SDaniel Borkmann * 4200ab8d7809SQuentin Monnet * In comparison, using **bpf_probe_read_user**\ () helper here 42016ae08ae3SDaniel Borkmann * instead to read the string would require to estimate the length 42026ae08ae3SDaniel Borkmann * at compile time, and would often result in copying more memory 42036ae08ae3SDaniel Borkmann * than necessary. 42046ae08ae3SDaniel Borkmann * 42056ae08ae3SDaniel Borkmann * Another useful use case is when parsing individual process 42066ae08ae3SDaniel Borkmann * arguments or individual environment variables navigating 42076ae08ae3SDaniel Borkmann * *current*\ **->mm->arg_start** and *current*\ 42086ae08ae3SDaniel Borkmann * **->mm->env_start**: using this helper and the return value, 42096ae08ae3SDaniel Borkmann * one can quickly iterate at the right offset of the memory area. 42106ae08ae3SDaniel Borkmann * Return 4211c6458e72SBrendan Jackman * On success, the strictly positive length of the output string, 42126ae08ae3SDaniel Borkmann * including the trailing NUL character. On error, a negative 42136ae08ae3SDaniel Borkmann * value. 42146ae08ae3SDaniel Borkmann * 4215bdb7b79bSAndrii Nakryiko * long bpf_probe_read_kernel_str(void *dst, u32 size, const void *unsafe_ptr) 42166ae08ae3SDaniel Borkmann * Description 42176ae08ae3SDaniel Borkmann * Copy a NUL terminated string from an unsafe kernel address *unsafe_ptr* 4218ab8d7809SQuentin Monnet * to *dst*. Same semantics as with **bpf_probe_read_user_str**\ () apply. 42196ae08ae3SDaniel Borkmann * Return 42206ae08ae3SDaniel Borkmann * On success, the strictly positive length of the string, including 42216ae08ae3SDaniel Borkmann * the trailing NUL character. On error, a negative value. 4222206057feSMartin KaFai Lau * 4223bdb7b79bSAndrii Nakryiko * long bpf_tcp_send_ack(void *tp, u32 rcv_nxt) 4224206057feSMartin KaFai Lau * Description 4225ab8d7809SQuentin Monnet * Send out a tcp-ack. *tp* is the in-kernel struct **tcp_sock**. 4226206057feSMartin KaFai Lau * *rcv_nxt* is the ack_seq to be sent out. 4227206057feSMartin KaFai Lau * Return 4228206057feSMartin KaFai Lau * 0 on success, or a negative error in case of failure. 4229206057feSMartin KaFai Lau * 4230bdb7b79bSAndrii Nakryiko * long bpf_send_signal_thread(u32 sig) 42318482941fSYonghong Song * Description 42328482941fSYonghong Song * Send signal *sig* to the thread corresponding to the current task. 42338482941fSYonghong Song * Return 42348482941fSYonghong Song * 0 on success or successfully queued. 42358482941fSYonghong Song * 42368482941fSYonghong Song * **-EBUSY** if work queue under nmi is full. 42378482941fSYonghong Song * 42388482941fSYonghong Song * **-EINVAL** if *sig* is invalid. 42398482941fSYonghong Song * 42408482941fSYonghong Song * **-EPERM** if no permission to send the *sig*. 42418482941fSYonghong Song * 42428482941fSYonghong Song * **-EAGAIN** if bpf program can try again. 42435576b991SMartin KaFai Lau * 42445576b991SMartin KaFai Lau * u64 bpf_jiffies64(void) 42455576b991SMartin KaFai Lau * Description 42465576b991SMartin KaFai Lau * Obtain the 64bit jiffies 42475576b991SMartin KaFai Lau * Return 42485576b991SMartin KaFai Lau * The 64 bit jiffies 4249fff7b643SDaniel Xu * 4250bdb7b79bSAndrii Nakryiko * long bpf_read_branch_records(struct bpf_perf_event_data *ctx, void *buf, u32 size, u64 flags) 4251fff7b643SDaniel Xu * Description 4252fff7b643SDaniel Xu * For an eBPF program attached to a perf event, retrieve the 4253ab8d7809SQuentin Monnet * branch records (**struct perf_branch_entry**) associated to *ctx* 4254fff7b643SDaniel Xu * and store it in the buffer pointed by *buf* up to size 4255fff7b643SDaniel Xu * *size* bytes. 4256fff7b643SDaniel Xu * Return 4257fff7b643SDaniel Xu * On success, number of bytes written to *buf*. On error, a 4258fff7b643SDaniel Xu * negative value. 4259fff7b643SDaniel Xu * 4260fff7b643SDaniel Xu * The *flags* can be set to **BPF_F_GET_BRANCH_RECORDS_SIZE** to 4261fff7b643SDaniel Xu * instead return the number of bytes required to store all the 4262fff7b643SDaniel Xu * branch entries. If this flag is set, *buf* may be NULL. 4263fff7b643SDaniel Xu * 4264fff7b643SDaniel Xu * **-EINVAL** if arguments invalid or **size** not a multiple 4265ab8d7809SQuentin Monnet * of **sizeof**\ (**struct perf_branch_entry**\ ). 4266fff7b643SDaniel Xu * 4267fff7b643SDaniel Xu * **-ENOENT** if architecture does not support branch records. 4268b4490c5cSCarlos Neira * 4269bdb7b79bSAndrii Nakryiko * long bpf_get_ns_current_pid_tgid(u64 dev, u64 ino, struct bpf_pidns_info *nsdata, u32 size) 4270b4490c5cSCarlos Neira * Description 4271b4490c5cSCarlos Neira * Returns 0 on success, values for *pid* and *tgid* as seen from the current 4272b4490c5cSCarlos Neira * *namespace* will be returned in *nsdata*. 4273ab8d7809SQuentin Monnet * Return 4274ab8d7809SQuentin Monnet * 0 on success, or one of the following in case of failure: 4275b4490c5cSCarlos Neira * 4276b4490c5cSCarlos Neira * **-EINVAL** if dev and inum supplied don't match dev_t and inode number 4277b4490c5cSCarlos Neira * with nsfs of current task, or if dev conversion to dev_t lost high bits. 4278b4490c5cSCarlos Neira * 4279b4490c5cSCarlos Neira * **-ENOENT** if pidns does not exists for the current task. 4280b4490c5cSCarlos Neira * 4281bdb7b79bSAndrii Nakryiko * long bpf_xdp_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size) 4282d831ee84SEelco Chaudron * Description 4283d831ee84SEelco Chaudron * Write raw *data* blob into a special BPF perf event held by 4284d831ee84SEelco Chaudron * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 4285d831ee84SEelco Chaudron * event must have the following attributes: **PERF_SAMPLE_RAW** 4286d831ee84SEelco Chaudron * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 4287d831ee84SEelco Chaudron * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 4288d831ee84SEelco Chaudron * 4289d831ee84SEelco Chaudron * The *flags* are used to indicate the index in *map* for which 4290d831ee84SEelco Chaudron * the value must be put, masked with **BPF_F_INDEX_MASK**. 4291d831ee84SEelco Chaudron * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 4292d831ee84SEelco Chaudron * to indicate that the index of the current CPU core should be 4293d831ee84SEelco Chaudron * used. 4294d831ee84SEelco Chaudron * 4295d831ee84SEelco Chaudron * The value to write, of *size*, is passed through eBPF stack and 4296d831ee84SEelco Chaudron * pointed by *data*. 4297d831ee84SEelco Chaudron * 4298d831ee84SEelco Chaudron * *ctx* is a pointer to in-kernel struct xdp_buff. 4299d831ee84SEelco Chaudron * 4300d831ee84SEelco Chaudron * This helper is similar to **bpf_perf_eventoutput**\ () but 4301d831ee84SEelco Chaudron * restricted to raw_tracepoint bpf programs. 4302d831ee84SEelco Chaudron * Return 4303d831ee84SEelco Chaudron * 0 on success, or a negative error in case of failure. 4304f318903cSDaniel Borkmann * 4305f318903cSDaniel Borkmann * u64 bpf_get_netns_cookie(void *ctx) 4306f318903cSDaniel Borkmann * Description 4307f318903cSDaniel Borkmann * Retrieve the cookie (generated by the kernel) of the network 4308f318903cSDaniel Borkmann * namespace the input *ctx* is associated with. The network 4309f318903cSDaniel Borkmann * namespace cookie remains stable for its lifetime and provides 4310f318903cSDaniel Borkmann * a global identifier that can be assumed unique. If *ctx* is 4311f318903cSDaniel Borkmann * NULL, then the helper returns the cookie for the initial 4312f318903cSDaniel Borkmann * network namespace. The cookie itself is very similar to that 4313ab8d7809SQuentin Monnet * of **bpf_get_socket_cookie**\ () helper, but for network 4314ab8d7809SQuentin Monnet * namespaces instead of sockets. 4315f318903cSDaniel Borkmann * Return 4316f318903cSDaniel Borkmann * A 8-byte long opaque number. 43170f09abd1SDaniel Borkmann * 43180f09abd1SDaniel Borkmann * u64 bpf_get_current_ancestor_cgroup_id(int ancestor_level) 43190f09abd1SDaniel Borkmann * Description 43200f09abd1SDaniel Borkmann * Return id of cgroup v2 that is ancestor of the cgroup associated 43210f09abd1SDaniel Borkmann * with the current task at the *ancestor_level*. The root cgroup 43220f09abd1SDaniel Borkmann * is at *ancestor_level* zero and each step down the hierarchy 43230f09abd1SDaniel Borkmann * increments the level. If *ancestor_level* == level of cgroup 43240f09abd1SDaniel Borkmann * associated with the current task, then return value will be the 43250f09abd1SDaniel Borkmann * same as that of **bpf_get_current_cgroup_id**\ (). 43260f09abd1SDaniel Borkmann * 43270f09abd1SDaniel Borkmann * The helper is useful to implement policies based on cgroups 43280f09abd1SDaniel Borkmann * that are upper in hierarchy than immediate cgroup associated 43290f09abd1SDaniel Borkmann * with the current task. 43300f09abd1SDaniel Borkmann * 43310f09abd1SDaniel Borkmann * The format of returned id and helper limitations are same as in 43320f09abd1SDaniel Borkmann * **bpf_get_current_cgroup_id**\ (). 43330f09abd1SDaniel Borkmann * Return 43340f09abd1SDaniel Borkmann * The id is returned or 0 in case the id could not be retrieved. 4335cf7fbe66SJoe Stringer * 433627e5203bSMartin KaFai Lau * long bpf_sk_assign(struct sk_buff *skb, void *sk, u64 flags) 4337cf7fbe66SJoe Stringer * Description 4338e9ddbb77SJakub Sitnicki * Helper is overloaded depending on BPF program type. This 4339e9ddbb77SJakub Sitnicki * description applies to **BPF_PROG_TYPE_SCHED_CLS** and 4340e9ddbb77SJakub Sitnicki * **BPF_PROG_TYPE_SCHED_ACT** programs. 4341e9ddbb77SJakub Sitnicki * 4342cf7fbe66SJoe Stringer * Assign the *sk* to the *skb*. When combined with appropriate 4343cf7fbe66SJoe Stringer * routing configuration to receive the packet towards the socket, 4344cf7fbe66SJoe Stringer * will cause *skb* to be delivered to the specified socket. 4345cf7fbe66SJoe Stringer * Subsequent redirection of *skb* via **bpf_redirect**\ (), 4346cf7fbe66SJoe Stringer * **bpf_clone_redirect**\ () or other methods outside of BPF may 4347cf7fbe66SJoe Stringer * interfere with successful delivery to the socket. 4348cf7fbe66SJoe Stringer * 4349cf7fbe66SJoe Stringer * This operation is only valid from TC ingress path. 4350cf7fbe66SJoe Stringer * 4351cf7fbe66SJoe Stringer * The *flags* argument must be zero. 4352cf7fbe66SJoe Stringer * Return 4353ab8d7809SQuentin Monnet * 0 on success, or a negative error in case of failure: 4354cf7fbe66SJoe Stringer * 4355ab8d7809SQuentin Monnet * **-EINVAL** if specified *flags* are not supported. 4356ab8d7809SQuentin Monnet * 4357ab8d7809SQuentin Monnet * **-ENOENT** if the socket is unavailable for assignment. 4358ab8d7809SQuentin Monnet * 4359ab8d7809SQuentin Monnet * **-ENETUNREACH** if the socket is unreachable (wrong netns). 4360ab8d7809SQuentin Monnet * 4361ab8d7809SQuentin Monnet * **-EOPNOTSUPP** if the operation is not supported, for example 4362ab8d7809SQuentin Monnet * a call from outside of TC ingress. 4363ab8d7809SQuentin Monnet * 4364e9ddbb77SJakub Sitnicki * long bpf_sk_assign(struct bpf_sk_lookup *ctx, struct bpf_sock *sk, u64 flags) 4365e9ddbb77SJakub Sitnicki * Description 4366e9ddbb77SJakub Sitnicki * Helper is overloaded depending on BPF program type. This 4367e9ddbb77SJakub Sitnicki * description applies to **BPF_PROG_TYPE_SK_LOOKUP** programs. 4368e9ddbb77SJakub Sitnicki * 4369e9ddbb77SJakub Sitnicki * Select the *sk* as a result of a socket lookup. 4370e9ddbb77SJakub Sitnicki * 4371e9ddbb77SJakub Sitnicki * For the operation to succeed passed socket must be compatible 4372e9ddbb77SJakub Sitnicki * with the packet description provided by the *ctx* object. 4373e9ddbb77SJakub Sitnicki * 4374e9ddbb77SJakub Sitnicki * L4 protocol (**IPPROTO_TCP** or **IPPROTO_UDP**) must 4375e9ddbb77SJakub Sitnicki * be an exact match. While IP family (**AF_INET** or 4376e9ddbb77SJakub Sitnicki * **AF_INET6**) must be compatible, that is IPv6 sockets 4377e9ddbb77SJakub Sitnicki * that are not v6-only can be selected for IPv4 packets. 4378e9ddbb77SJakub Sitnicki * 4379e9ddbb77SJakub Sitnicki * Only TCP listeners and UDP unconnected sockets can be 4380e9ddbb77SJakub Sitnicki * selected. *sk* can also be NULL to reset any previous 4381e9ddbb77SJakub Sitnicki * selection. 4382e9ddbb77SJakub Sitnicki * 4383e9ddbb77SJakub Sitnicki * *flags* argument can combination of following values: 4384e9ddbb77SJakub Sitnicki * 4385e9ddbb77SJakub Sitnicki * * **BPF_SK_LOOKUP_F_REPLACE** to override the previous 4386e9ddbb77SJakub Sitnicki * socket selection, potentially done by a BPF program 4387e9ddbb77SJakub Sitnicki * that ran before us. 4388e9ddbb77SJakub Sitnicki * 4389e9ddbb77SJakub Sitnicki * * **BPF_SK_LOOKUP_F_NO_REUSEPORT** to skip 4390e9ddbb77SJakub Sitnicki * load-balancing within reuseport group for the socket 4391e9ddbb77SJakub Sitnicki * being selected. 4392e9ddbb77SJakub Sitnicki * 4393e9ddbb77SJakub Sitnicki * On success *ctx->sk* will point to the selected socket. 4394e9ddbb77SJakub Sitnicki * 4395e9ddbb77SJakub Sitnicki * Return 4396e9ddbb77SJakub Sitnicki * 0 on success, or a negative errno in case of failure. 4397e9ddbb77SJakub Sitnicki * 4398e9ddbb77SJakub Sitnicki * * **-EAFNOSUPPORT** if socket family (*sk->family*) is 4399e9ddbb77SJakub Sitnicki * not compatible with packet family (*ctx->family*). 4400e9ddbb77SJakub Sitnicki * 4401e9ddbb77SJakub Sitnicki * * **-EEXIST** if socket has been already selected, 4402e9ddbb77SJakub Sitnicki * potentially by another program, and 4403e9ddbb77SJakub Sitnicki * **BPF_SK_LOOKUP_F_REPLACE** flag was not specified. 4404e9ddbb77SJakub Sitnicki * 4405e9ddbb77SJakub Sitnicki * * **-EINVAL** if unsupported flags were specified. 4406e9ddbb77SJakub Sitnicki * 4407e9ddbb77SJakub Sitnicki * * **-EPROTOTYPE** if socket L4 protocol 4408e9ddbb77SJakub Sitnicki * (*sk->protocol*) doesn't match packet protocol 4409e9ddbb77SJakub Sitnicki * (*ctx->protocol*). 4410e9ddbb77SJakub Sitnicki * 4411e9ddbb77SJakub Sitnicki * * **-ESOCKTNOSUPPORT** if socket is not in allowed 4412e9ddbb77SJakub Sitnicki * state (TCP listening or UDP unconnected). 4413e9ddbb77SJakub Sitnicki * 441471d19214SMaciej Żenczykowski * u64 bpf_ktime_get_boot_ns(void) 441571d19214SMaciej Żenczykowski * Description 441671d19214SMaciej Żenczykowski * Return the time elapsed since system boot, in nanoseconds. 441771d19214SMaciej Żenczykowski * Does include the time the system was suspended. 4418ab8d7809SQuentin Monnet * See: **clock_gettime**\ (**CLOCK_BOOTTIME**) 441971d19214SMaciej Żenczykowski * Return 442071d19214SMaciej Żenczykowski * Current *ktime*. 4421492e639fSYonghong Song * 4422bdb7b79bSAndrii Nakryiko * long bpf_seq_printf(struct seq_file *m, const char *fmt, u32 fmt_size, const void *data, u32 data_len) 4423492e639fSYonghong Song * Description 4424ab8d7809SQuentin Monnet * **bpf_seq_printf**\ () uses seq_file **seq_printf**\ () to print 4425ab8d7809SQuentin Monnet * out the format string. 4426492e639fSYonghong Song * The *m* represents the seq_file. The *fmt* and *fmt_size* are for 4427492e639fSYonghong Song * the format string itself. The *data* and *data_len* are format string 4428ab8d7809SQuentin Monnet * arguments. The *data* are a **u64** array and corresponding format string 4429492e639fSYonghong Song * values are stored in the array. For strings and pointers where pointees 4430492e639fSYonghong Song * are accessed, only the pointer values are stored in the *data* array. 4431a42effb0SDave Marchevsky * The *data_len* is the size of *data* in bytes - must be a multiple of 8. 4432492e639fSYonghong Song * 4433492e639fSYonghong Song * Formats **%s**, **%p{i,I}{4,6}** requires to read kernel memory. 4434492e639fSYonghong Song * Reading kernel memory may fail due to either invalid address or 4435492e639fSYonghong Song * valid address but requiring a major memory fault. If reading kernel memory 4436492e639fSYonghong Song * fails, the string for **%s** will be an empty string, and the ip 4437492e639fSYonghong Song * address for **%p{i,I}{4,6}** will be 0. Not returning error to 4438ab8d7809SQuentin Monnet * bpf program is consistent with what **bpf_trace_printk**\ () does for now. 4439492e639fSYonghong Song * Return 4440ab8d7809SQuentin Monnet * 0 on success, or a negative error in case of failure: 4441492e639fSYonghong Song * 4442ab8d7809SQuentin Monnet * **-EBUSY** if per-CPU memory copy buffer is busy, can try again 4443492e639fSYonghong Song * by returning 1 from bpf program. 4444ab8d7809SQuentin Monnet * 4445ab8d7809SQuentin Monnet * **-EINVAL** if arguments are invalid, or if *fmt* is invalid/unsupported. 4446ab8d7809SQuentin Monnet * 4447ab8d7809SQuentin Monnet * **-E2BIG** if *fmt* contains too many format specifiers. 4448ab8d7809SQuentin Monnet * 4449ab8d7809SQuentin Monnet * **-EOVERFLOW** if an overflow happened: The same object will be tried again. 4450492e639fSYonghong Song * 4451bdb7b79bSAndrii Nakryiko * long bpf_seq_write(struct seq_file *m, const void *data, u32 len) 4452492e639fSYonghong Song * Description 4453ab8d7809SQuentin Monnet * **bpf_seq_write**\ () uses seq_file **seq_write**\ () to write the data. 4454492e639fSYonghong Song * The *m* represents the seq_file. The *data* and *len* represent the 4455492e639fSYonghong Song * data to write in bytes. 4456492e639fSYonghong Song * Return 4457ab8d7809SQuentin Monnet * 0 on success, or a negative error in case of failure: 4458492e639fSYonghong Song * 4459ab8d7809SQuentin Monnet * **-EOVERFLOW** if an overflow happened: The same object will be tried again. 4460f307fa2cSAndrey Ignatov * 4461a5fa25adSMartin KaFai Lau * u64 bpf_sk_cgroup_id(void *sk) 4462f307fa2cSAndrey Ignatov * Description 4463f307fa2cSAndrey Ignatov * Return the cgroup v2 id of the socket *sk*. 4464f307fa2cSAndrey Ignatov * 4465a5fa25adSMartin KaFai Lau * *sk* must be a non-**NULL** pointer to a socket, e.g. one 4466f307fa2cSAndrey Ignatov * returned from **bpf_sk_lookup_xxx**\ (), 4467f307fa2cSAndrey Ignatov * **bpf_sk_fullsock**\ (), etc. The format of returned id is 4468f307fa2cSAndrey Ignatov * same as in **bpf_skb_cgroup_id**\ (). 4469f307fa2cSAndrey Ignatov * 4470f307fa2cSAndrey Ignatov * This helper is available only if the kernel was compiled with 4471f307fa2cSAndrey Ignatov * the **CONFIG_SOCK_CGROUP_DATA** configuration option. 4472f307fa2cSAndrey Ignatov * Return 4473f307fa2cSAndrey Ignatov * The id is returned or 0 in case the id could not be retrieved. 4474f307fa2cSAndrey Ignatov * 4475a5fa25adSMartin KaFai Lau * u64 bpf_sk_ancestor_cgroup_id(void *sk, int ancestor_level) 4476f307fa2cSAndrey Ignatov * Description 4477f307fa2cSAndrey Ignatov * Return id of cgroup v2 that is ancestor of cgroup associated 4478f307fa2cSAndrey Ignatov * with the *sk* at the *ancestor_level*. The root cgroup is at 4479f307fa2cSAndrey Ignatov * *ancestor_level* zero and each step down the hierarchy 4480f307fa2cSAndrey Ignatov * increments the level. If *ancestor_level* == level of cgroup 4481f307fa2cSAndrey Ignatov * associated with *sk*, then return value will be same as that 4482f307fa2cSAndrey Ignatov * of **bpf_sk_cgroup_id**\ (). 4483f307fa2cSAndrey Ignatov * 4484f307fa2cSAndrey Ignatov * The helper is useful to implement policies based on cgroups 4485f307fa2cSAndrey Ignatov * that are upper in hierarchy than immediate cgroup associated 4486f307fa2cSAndrey Ignatov * with *sk*. 4487f307fa2cSAndrey Ignatov * 4488f307fa2cSAndrey Ignatov * The format of returned id and helper limitations are same as in 4489f307fa2cSAndrey Ignatov * **bpf_sk_cgroup_id**\ (). 4490f307fa2cSAndrey Ignatov * Return 4491f307fa2cSAndrey Ignatov * The id is returned or 0 in case the id could not be retrieved. 4492457f4436SAndrii Nakryiko * 4493e1613b57SAndrii Nakryiko * long bpf_ringbuf_output(void *ringbuf, void *data, u64 size, u64 flags) 4494457f4436SAndrii Nakryiko * Description 4495457f4436SAndrii Nakryiko * Copy *size* bytes from *data* into a ring buffer *ringbuf*. 4496bcc7f554SQuentin Monnet * If **BPF_RB_NO_WAKEUP** is specified in *flags*, no notification 4497bcc7f554SQuentin Monnet * of new data availability is sent. 4498bcc7f554SQuentin Monnet * If **BPF_RB_FORCE_WAKEUP** is specified in *flags*, notification 4499bcc7f554SQuentin Monnet * of new data availability is sent unconditionally. 45005c507329SPedro Tammela * If **0** is specified in *flags*, an adaptive notification 45015c507329SPedro Tammela * of new data availability is sent. 45025c507329SPedro Tammela * 45035c507329SPedro Tammela * An adaptive notification is a notification sent whenever the user-space 45045c507329SPedro Tammela * process has caught up and consumed all available payloads. In case the user-space 45055c507329SPedro Tammela * process is still processing a previous payload, then no notification is needed 45065c507329SPedro Tammela * as it will process the newly added payload automatically. 4507457f4436SAndrii Nakryiko * Return 4508bcc7f554SQuentin Monnet * 0 on success, or a negative error in case of failure. 4509457f4436SAndrii Nakryiko * 4510457f4436SAndrii Nakryiko * void *bpf_ringbuf_reserve(void *ringbuf, u64 size, u64 flags) 4511457f4436SAndrii Nakryiko * Description 4512457f4436SAndrii Nakryiko * Reserve *size* bytes of payload in a ring buffer *ringbuf*. 45135c507329SPedro Tammela * *flags* must be 0. 4514457f4436SAndrii Nakryiko * Return 4515457f4436SAndrii Nakryiko * Valid pointer with *size* bytes of memory available; NULL, 4516457f4436SAndrii Nakryiko * otherwise. 4517457f4436SAndrii Nakryiko * 4518457f4436SAndrii Nakryiko * void bpf_ringbuf_submit(void *data, u64 flags) 4519457f4436SAndrii Nakryiko * Description 4520457f4436SAndrii Nakryiko * Submit reserved ring buffer sample, pointed to by *data*. 4521bcc7f554SQuentin Monnet * If **BPF_RB_NO_WAKEUP** is specified in *flags*, no notification 4522bcc7f554SQuentin Monnet * of new data availability is sent. 4523bcc7f554SQuentin Monnet * If **BPF_RB_FORCE_WAKEUP** is specified in *flags*, notification 4524bcc7f554SQuentin Monnet * of new data availability is sent unconditionally. 45255c507329SPedro Tammela * If **0** is specified in *flags*, an adaptive notification 45265c507329SPedro Tammela * of new data availability is sent. 45275c507329SPedro Tammela * 45285c507329SPedro Tammela * See 'bpf_ringbuf_output()' for the definition of adaptive notification. 4529457f4436SAndrii Nakryiko * Return 4530457f4436SAndrii Nakryiko * Nothing. Always succeeds. 4531457f4436SAndrii Nakryiko * 4532457f4436SAndrii Nakryiko * void bpf_ringbuf_discard(void *data, u64 flags) 4533457f4436SAndrii Nakryiko * Description 4534457f4436SAndrii Nakryiko * Discard reserved ring buffer sample, pointed to by *data*. 4535bcc7f554SQuentin Monnet * If **BPF_RB_NO_WAKEUP** is specified in *flags*, no notification 4536bcc7f554SQuentin Monnet * of new data availability is sent. 4537bcc7f554SQuentin Monnet * If **BPF_RB_FORCE_WAKEUP** is specified in *flags*, notification 4538bcc7f554SQuentin Monnet * of new data availability is sent unconditionally. 45395c507329SPedro Tammela * If **0** is specified in *flags*, an adaptive notification 45405c507329SPedro Tammela * of new data availability is sent. 45415c507329SPedro Tammela * 45425c507329SPedro Tammela * See 'bpf_ringbuf_output()' for the definition of adaptive notification. 4543457f4436SAndrii Nakryiko * Return 4544457f4436SAndrii Nakryiko * Nothing. Always succeeds. 4545457f4436SAndrii Nakryiko * 4546457f4436SAndrii Nakryiko * u64 bpf_ringbuf_query(void *ringbuf, u64 flags) 4547457f4436SAndrii Nakryiko * Description 4548457f4436SAndrii Nakryiko * Query various characteristics of provided ring buffer. What 4549457f4436SAndrii Nakryiko * exactly is queries is determined by *flags*: 4550bcc7f554SQuentin Monnet * 4551bcc7f554SQuentin Monnet * * **BPF_RB_AVAIL_DATA**: Amount of data not yet consumed. 4552bcc7f554SQuentin Monnet * * **BPF_RB_RING_SIZE**: The size of ring buffer. 4553bcc7f554SQuentin Monnet * * **BPF_RB_CONS_POS**: Consumer position (can wrap around). 4554bcc7f554SQuentin Monnet * * **BPF_RB_PROD_POS**: Producer(s) position (can wrap around). 4555bcc7f554SQuentin Monnet * 4556bcc7f554SQuentin Monnet * Data returned is just a momentary snapshot of actual values 4557457f4436SAndrii Nakryiko * and could be inaccurate, so this facility should be used to 4558457f4436SAndrii Nakryiko * power heuristics and for reporting, not to make 100% correct 4559457f4436SAndrii Nakryiko * calculation. 4560457f4436SAndrii Nakryiko * Return 4561bcc7f554SQuentin Monnet * Requested value, or 0, if *flags* are not recognized. 45627cdec54fSDaniel Borkmann * 4563bdb7b79bSAndrii Nakryiko * long bpf_csum_level(struct sk_buff *skb, u64 level) 45647cdec54fSDaniel Borkmann * Description 45657cdec54fSDaniel Borkmann * Change the skbs checksum level by one layer up or down, or 45667cdec54fSDaniel Borkmann * reset it entirely to none in order to have the stack perform 45677cdec54fSDaniel Borkmann * checksum validation. The level is applicable to the following 45687cdec54fSDaniel Borkmann * protocols: TCP, UDP, GRE, SCTP, FCOE. For example, a decap of 45697cdec54fSDaniel Borkmann * | ETH | IP | UDP | GUE | IP | TCP | into | ETH | IP | TCP | 45707cdec54fSDaniel Borkmann * through **bpf_skb_adjust_room**\ () helper with passing in 45717cdec54fSDaniel Borkmann * **BPF_F_ADJ_ROOM_NO_CSUM_RESET** flag would require one call 45727cdec54fSDaniel Borkmann * to **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_DEC** since 45737cdec54fSDaniel Borkmann * the UDP header is removed. Similarly, an encap of the latter 45747cdec54fSDaniel Borkmann * into the former could be accompanied by a helper call to 45757cdec54fSDaniel Borkmann * **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_INC** if the 45767cdec54fSDaniel Borkmann * skb is still intended to be processed in higher layers of the 45777cdec54fSDaniel Borkmann * stack instead of just egressing at tc. 45787cdec54fSDaniel Borkmann * 45797cdec54fSDaniel Borkmann * There are three supported level settings at this time: 45807cdec54fSDaniel Borkmann * 45817cdec54fSDaniel Borkmann * * **BPF_CSUM_LEVEL_INC**: Increases skb->csum_level for skbs 45827cdec54fSDaniel Borkmann * with CHECKSUM_UNNECESSARY. 45837cdec54fSDaniel Borkmann * * **BPF_CSUM_LEVEL_DEC**: Decreases skb->csum_level for skbs 45847cdec54fSDaniel Borkmann * with CHECKSUM_UNNECESSARY. 45857cdec54fSDaniel Borkmann * * **BPF_CSUM_LEVEL_RESET**: Resets skb->csum_level to 0 and 45867cdec54fSDaniel Borkmann * sets CHECKSUM_NONE to force checksum validation by the stack. 45877cdec54fSDaniel Borkmann * * **BPF_CSUM_LEVEL_QUERY**: No-op, returns the current 45887cdec54fSDaniel Borkmann * skb->csum_level. 45897cdec54fSDaniel Borkmann * Return 45907cdec54fSDaniel Borkmann * 0 on success, or a negative error in case of failure. In the 45917cdec54fSDaniel Borkmann * case of **BPF_CSUM_LEVEL_QUERY**, the current skb->csum_level 45927cdec54fSDaniel Borkmann * is returned or the error code -EACCES in case the skb is not 45937cdec54fSDaniel Borkmann * subject to CHECKSUM_UNNECESSARY. 4594af7ec138SYonghong Song * 4595af7ec138SYonghong Song * struct tcp6_sock *bpf_skc_to_tcp6_sock(void *sk) 4596af7ec138SYonghong Song * Description 4597af7ec138SYonghong Song * Dynamically cast a *sk* pointer to a *tcp6_sock* pointer. 4598af7ec138SYonghong Song * Return 4599938c3efdSQuentin Monnet * *sk* if casting is valid, or **NULL** otherwise. 4600478cfbdfSYonghong Song * 4601478cfbdfSYonghong Song * struct tcp_sock *bpf_skc_to_tcp_sock(void *sk) 4602478cfbdfSYonghong Song * Description 4603478cfbdfSYonghong Song * Dynamically cast a *sk* pointer to a *tcp_sock* pointer. 4604478cfbdfSYonghong Song * Return 4605938c3efdSQuentin Monnet * *sk* if casting is valid, or **NULL** otherwise. 4606478cfbdfSYonghong Song * 4607478cfbdfSYonghong Song * struct tcp_timewait_sock *bpf_skc_to_tcp_timewait_sock(void *sk) 4608478cfbdfSYonghong Song * Description 4609478cfbdfSYonghong Song * Dynamically cast a *sk* pointer to a *tcp_timewait_sock* pointer. 4610478cfbdfSYonghong Song * Return 4611938c3efdSQuentin Monnet * *sk* if casting is valid, or **NULL** otherwise. 4612478cfbdfSYonghong Song * 4613478cfbdfSYonghong Song * struct tcp_request_sock *bpf_skc_to_tcp_request_sock(void *sk) 4614478cfbdfSYonghong Song * Description 4615478cfbdfSYonghong Song * Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer. 4616478cfbdfSYonghong Song * Return 4617938c3efdSQuentin Monnet * *sk* if casting is valid, or **NULL** otherwise. 46180d4fad3eSYonghong Song * 46190d4fad3eSYonghong Song * struct udp6_sock *bpf_skc_to_udp6_sock(void *sk) 46200d4fad3eSYonghong Song * Description 46210d4fad3eSYonghong Song * Dynamically cast a *sk* pointer to a *udp6_sock* pointer. 46220d4fad3eSYonghong Song * Return 4623938c3efdSQuentin Monnet * *sk* if casting is valid, or **NULL** otherwise. 4624fa28dcb8SSong Liu * 4625fa28dcb8SSong Liu * long bpf_get_task_stack(struct task_struct *task, void *buf, u32 size, u64 flags) 4626fa28dcb8SSong Liu * Description 4627fa28dcb8SSong Liu * Return a user or a kernel stack in bpf program provided buffer. 4628b8e3a87aSJordan Rome * Note: the user stack will only be populated if the *task* is 4629b8e3a87aSJordan Rome * the current task; all other tasks will return -EOPNOTSUPP. 4630fa28dcb8SSong Liu * To achieve this, the helper needs *task*, which is a valid 4631938c3efdSQuentin Monnet * pointer to **struct task_struct**. To store the stacktrace, the 4632fa28dcb8SSong Liu * bpf program provides *buf* with a nonnegative *size*. 4633fa28dcb8SSong Liu * 4634fa28dcb8SSong Liu * The last argument, *flags*, holds the number of stack frames to 4635fa28dcb8SSong Liu * skip (from 0 to 255), masked with 4636fa28dcb8SSong Liu * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 4637fa28dcb8SSong Liu * the following flags: 4638fa28dcb8SSong Liu * 4639fa28dcb8SSong Liu * **BPF_F_USER_STACK** 4640fa28dcb8SSong Liu * Collect a user space stack instead of a kernel stack. 4641b8e3a87aSJordan Rome * The *task* must be the current task. 4642fa28dcb8SSong Liu * **BPF_F_USER_BUILD_ID** 4643fa28dcb8SSong Liu * Collect buildid+offset instead of ips for user stack, 4644fa28dcb8SSong Liu * only valid if **BPF_F_USER_STACK** is also specified. 4645fa28dcb8SSong Liu * 4646fa28dcb8SSong Liu * **bpf_get_task_stack**\ () can collect up to 4647fa28dcb8SSong Liu * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject 4648fa28dcb8SSong Liu * to sufficient large buffer size. Note that 4649fa28dcb8SSong Liu * this limit can be controlled with the **sysctl** program, and 4650fa28dcb8SSong Liu * that it should be manually increased in order to profile long 4651fa28dcb8SSong Liu * user stacks (such as stacks for Java programs). To do so, use: 4652fa28dcb8SSong Liu * 4653fa28dcb8SSong Liu * :: 4654fa28dcb8SSong Liu * 4655fa28dcb8SSong Liu * # sysctl kernel.perf_event_max_stack=<new value> 4656fa28dcb8SSong Liu * Return 4657ee2a0988SNamhyung Kim * The non-negative copied *buf* length equal to or less than 4658ee2a0988SNamhyung Kim * *size* on success, or a negative error in case of failure. 4659fa28dcb8SSong Liu * 46600813a841SMartin KaFai Lau * long bpf_load_hdr_opt(struct bpf_sock_ops *skops, void *searchby_res, u32 len, u64 flags) 46610813a841SMartin KaFai Lau * Description 46620813a841SMartin KaFai Lau * Load header option. Support reading a particular TCP header 4663938c3efdSQuentin Monnet * option for bpf program (**BPF_PROG_TYPE_SOCK_OPS**). 46640813a841SMartin KaFai Lau * 46650813a841SMartin KaFai Lau * If *flags* is 0, it will search the option from the 4666938c3efdSQuentin Monnet * *skops*\ **->skb_data**. The comment in **struct bpf_sock_ops** 46670813a841SMartin KaFai Lau * has details on what skb_data contains under different 4668938c3efdSQuentin Monnet * *skops*\ **->op**. 46690813a841SMartin KaFai Lau * 46700813a841SMartin KaFai Lau * The first byte of the *searchby_res* specifies the 46710813a841SMartin KaFai Lau * kind that it wants to search. 46720813a841SMartin KaFai Lau * 46730813a841SMartin KaFai Lau * If the searching kind is an experimental kind 46740813a841SMartin KaFai Lau * (i.e. 253 or 254 according to RFC6994). It also 46750813a841SMartin KaFai Lau * needs to specify the "magic" which is either 46760813a841SMartin KaFai Lau * 2 bytes or 4 bytes. It then also needs to 46770813a841SMartin KaFai Lau * specify the size of the magic by using 46780813a841SMartin KaFai Lau * the 2nd byte which is "kind-length" of a TCP 46790813a841SMartin KaFai Lau * header option and the "kind-length" also 46800813a841SMartin KaFai Lau * includes the first 2 bytes "kind" and "kind-length" 46810813a841SMartin KaFai Lau * itself as a normal TCP header option also does. 46820813a841SMartin KaFai Lau * 46830813a841SMartin KaFai Lau * For example, to search experimental kind 254 with 46840813a841SMartin KaFai Lau * 2 byte magic 0xeB9F, the searchby_res should be 46850813a841SMartin KaFai Lau * [ 254, 4, 0xeB, 0x9F, 0, 0, .... 0 ]. 46860813a841SMartin KaFai Lau * 46870813a841SMartin KaFai Lau * To search for the standard window scale option (3), 4688938c3efdSQuentin Monnet * the *searchby_res* should be [ 3, 0, 0, .... 0 ]. 46890813a841SMartin KaFai Lau * Note, kind-length must be 0 for regular option. 46900813a841SMartin KaFai Lau * 46910813a841SMartin KaFai Lau * Searching for No-Op (0) and End-of-Option-List (1) are 46920813a841SMartin KaFai Lau * not supported. 46930813a841SMartin KaFai Lau * 46940813a841SMartin KaFai Lau * *len* must be at least 2 bytes which is the minimal size 46950813a841SMartin KaFai Lau * of a header option. 46960813a841SMartin KaFai Lau * 46970813a841SMartin KaFai Lau * Supported flags: 4698938c3efdSQuentin Monnet * 46990813a841SMartin KaFai Lau * * **BPF_LOAD_HDR_OPT_TCP_SYN** to search from the 47000813a841SMartin KaFai Lau * saved_syn packet or the just-received syn packet. 47010813a841SMartin KaFai Lau * 47020813a841SMartin KaFai Lau * Return 47030813a841SMartin KaFai Lau * > 0 when found, the header option is copied to *searchby_res*. 4704938c3efdSQuentin Monnet * The return value is the total length copied. On failure, a 4705938c3efdSQuentin Monnet * negative error code is returned: 47060813a841SMartin KaFai Lau * 4707938c3efdSQuentin Monnet * **-EINVAL** if a parameter is invalid. 47080813a841SMartin KaFai Lau * 4709938c3efdSQuentin Monnet * **-ENOMSG** if the option is not found. 47100813a841SMartin KaFai Lau * 4711938c3efdSQuentin Monnet * **-ENOENT** if no syn packet is available when 4712938c3efdSQuentin Monnet * **BPF_LOAD_HDR_OPT_TCP_SYN** is used. 47130813a841SMartin KaFai Lau * 4714938c3efdSQuentin Monnet * **-ENOSPC** if there is not enough space. Only *len* number of 47150813a841SMartin KaFai Lau * bytes are copied. 47160813a841SMartin KaFai Lau * 4717938c3efdSQuentin Monnet * **-EFAULT** on failure to parse the header options in the 4718938c3efdSQuentin Monnet * packet. 47190813a841SMartin KaFai Lau * 4720938c3efdSQuentin Monnet * **-EPERM** if the helper cannot be used under the current 4721938c3efdSQuentin Monnet * *skops*\ **->op**. 47220813a841SMartin KaFai Lau * 47230813a841SMartin KaFai Lau * long bpf_store_hdr_opt(struct bpf_sock_ops *skops, const void *from, u32 len, u64 flags) 47240813a841SMartin KaFai Lau * Description 47250813a841SMartin KaFai Lau * Store header option. The data will be copied 47260813a841SMartin KaFai Lau * from buffer *from* with length *len* to the TCP header. 47270813a841SMartin KaFai Lau * 47280813a841SMartin KaFai Lau * The buffer *from* should have the whole option that 47290813a841SMartin KaFai Lau * includes the kind, kind-length, and the actual 47300813a841SMartin KaFai Lau * option data. The *len* must be at least kind-length 47310813a841SMartin KaFai Lau * long. The kind-length does not have to be 4 byte 47320813a841SMartin KaFai Lau * aligned. The kernel will take care of the padding 47330813a841SMartin KaFai Lau * and setting the 4 bytes aligned value to th->doff. 47340813a841SMartin KaFai Lau * 47350813a841SMartin KaFai Lau * This helper will check for duplicated option 47360813a841SMartin KaFai Lau * by searching the same option in the outgoing skb. 47370813a841SMartin KaFai Lau * 47380813a841SMartin KaFai Lau * This helper can only be called during 4739938c3efdSQuentin Monnet * **BPF_SOCK_OPS_WRITE_HDR_OPT_CB**. 47400813a841SMartin KaFai Lau * 47410813a841SMartin KaFai Lau * Return 47420813a841SMartin KaFai Lau * 0 on success, or negative error in case of failure: 47430813a841SMartin KaFai Lau * 4744938c3efdSQuentin Monnet * **-EINVAL** If param is invalid. 47450813a841SMartin KaFai Lau * 4746938c3efdSQuentin Monnet * **-ENOSPC** if there is not enough space in the header. 47470813a841SMartin KaFai Lau * Nothing has been written 47480813a841SMartin KaFai Lau * 4749938c3efdSQuentin Monnet * **-EEXIST** if the option already exists. 47500813a841SMartin KaFai Lau * 4751aa75622cSQuentin Monnet * **-EFAULT** on failure to parse the existing header options. 47520813a841SMartin KaFai Lau * 4753938c3efdSQuentin Monnet * **-EPERM** if the helper cannot be used under the current 4754938c3efdSQuentin Monnet * *skops*\ **->op**. 47550813a841SMartin KaFai Lau * 47560813a841SMartin KaFai Lau * long bpf_reserve_hdr_opt(struct bpf_sock_ops *skops, u32 len, u64 flags) 47570813a841SMartin KaFai Lau * Description 47580813a841SMartin KaFai Lau * Reserve *len* bytes for the bpf header option. The 4759938c3efdSQuentin Monnet * space will be used by **bpf_store_hdr_opt**\ () later in 4760938c3efdSQuentin Monnet * **BPF_SOCK_OPS_WRITE_HDR_OPT_CB**. 47610813a841SMartin KaFai Lau * 4762938c3efdSQuentin Monnet * If **bpf_reserve_hdr_opt**\ () is called multiple times, 47630813a841SMartin KaFai Lau * the total number of bytes will be reserved. 47640813a841SMartin KaFai Lau * 47650813a841SMartin KaFai Lau * This helper can only be called during 4766938c3efdSQuentin Monnet * **BPF_SOCK_OPS_HDR_OPT_LEN_CB**. 47670813a841SMartin KaFai Lau * 47680813a841SMartin KaFai Lau * Return 47690813a841SMartin KaFai Lau * 0 on success, or negative error in case of failure: 47700813a841SMartin KaFai Lau * 4771938c3efdSQuentin Monnet * **-EINVAL** if a parameter is invalid. 47720813a841SMartin KaFai Lau * 4773938c3efdSQuentin Monnet * **-ENOSPC** if there is not enough space in the header. 47740813a841SMartin KaFai Lau * 4775938c3efdSQuentin Monnet * **-EPERM** if the helper cannot be used under the current 4776938c3efdSQuentin Monnet * *skops*\ **->op**. 47776e22ab9dSJiri Olsa * 47788ea63684SKP Singh * void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags) 47798ea63684SKP Singh * Description 47808ea63684SKP Singh * Get a bpf_local_storage from an *inode*. 47818ea63684SKP Singh * 47828ea63684SKP Singh * Logically, it could be thought of as getting the value from 47838ea63684SKP Singh * a *map* with *inode* as the **key**. From this 47848ea63684SKP Singh * perspective, the usage is not much different from 47858ea63684SKP Singh * **bpf_map_lookup_elem**\ (*map*, **&**\ *inode*) except this 47868ea63684SKP Singh * helper enforces the key must be an inode and the map must also 47878ea63684SKP Singh * be a **BPF_MAP_TYPE_INODE_STORAGE**. 47888ea63684SKP Singh * 47898ea63684SKP Singh * Underneath, the value is stored locally at *inode* instead of 47908ea63684SKP Singh * the *map*. The *map* is used as the bpf-local-storage 47918ea63684SKP Singh * "type". The bpf-local-storage "type" (i.e. the *map*) is 47928ea63684SKP Singh * searched against all bpf_local_storage residing at *inode*. 47938ea63684SKP Singh * 47948ea63684SKP Singh * An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be 47958ea63684SKP Singh * used such that a new bpf_local_storage will be 47968ea63684SKP Singh * created if one does not exist. *value* can be used 47978ea63684SKP Singh * together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify 47988ea63684SKP Singh * the initial value of a bpf_local_storage. If *value* is 47998ea63684SKP Singh * **NULL**, the new bpf_local_storage will be zero initialized. 48008ea63684SKP Singh * Return 48018ea63684SKP Singh * A bpf_local_storage pointer is returned on success. 48028ea63684SKP Singh * 48038ea63684SKP Singh * **NULL** if not found or there was an error in adding 48048ea63684SKP Singh * a new bpf_local_storage. 48058ea63684SKP Singh * 48068ea63684SKP Singh * int bpf_inode_storage_delete(struct bpf_map *map, void *inode) 48078ea63684SKP Singh * Description 48088ea63684SKP Singh * Delete a bpf_local_storage from an *inode*. 48098ea63684SKP Singh * Return 48108ea63684SKP Singh * 0 on success. 48118ea63684SKP Singh * 48128ea63684SKP Singh * **-ENOENT** if the bpf_local_storage cannot be found. 48136e22ab9dSJiri Olsa * 48146e22ab9dSJiri Olsa * long bpf_d_path(struct path *path, char *buf, u32 sz) 48156e22ab9dSJiri Olsa * Description 4816938c3efdSQuentin Monnet * Return full path for given **struct path** object, which 4817938c3efdSQuentin Monnet * needs to be the kernel BTF *path* object. The path is 4818938c3efdSQuentin Monnet * returned in the provided buffer *buf* of size *sz* and 48196e22ab9dSJiri Olsa * is zero terminated. 48206e22ab9dSJiri Olsa * 48216e22ab9dSJiri Olsa * Return 48226e22ab9dSJiri Olsa * On success, the strictly positive length of the string, 48236e22ab9dSJiri Olsa * including the trailing NUL character. On error, a negative 48246e22ab9dSJiri Olsa * value. 482507be4c4aSAlexei Starovoitov * 482607be4c4aSAlexei Starovoitov * long bpf_copy_from_user(void *dst, u32 size, const void *user_ptr) 482707be4c4aSAlexei Starovoitov * Description 482807be4c4aSAlexei Starovoitov * Read *size* bytes from user space address *user_ptr* and store 4829938c3efdSQuentin Monnet * the data in *dst*. This is a wrapper of **copy_from_user**\ (). 483007be4c4aSAlexei Starovoitov * Return 483107be4c4aSAlexei Starovoitov * 0 on success, or a negative error in case of failure. 4832c4d0bfb4SAlan Maguire * 4833c4d0bfb4SAlan Maguire * long bpf_snprintf_btf(char *str, u32 str_size, struct btf_ptr *ptr, u32 btf_ptr_size, u64 flags) 4834c4d0bfb4SAlan Maguire * Description 4835c4d0bfb4SAlan Maguire * Use BTF to store a string representation of *ptr*->ptr in *str*, 4836c4d0bfb4SAlan Maguire * using *ptr*->type_id. This value should specify the type 4837c4d0bfb4SAlan Maguire * that *ptr*->ptr points to. LLVM __builtin_btf_type_id(type, 1) 4838c4d0bfb4SAlan Maguire * can be used to look up vmlinux BTF type ids. Traversing the 4839c4d0bfb4SAlan Maguire * data structure using BTF, the type information and values are 4840c4d0bfb4SAlan Maguire * stored in the first *str_size* - 1 bytes of *str*. Safe copy of 4841c4d0bfb4SAlan Maguire * the pointer data is carried out to avoid kernel crashes during 4842c4d0bfb4SAlan Maguire * operation. Smaller types can use string space on the stack; 4843c4d0bfb4SAlan Maguire * larger programs can use map data to store the string 4844c4d0bfb4SAlan Maguire * representation. 4845c4d0bfb4SAlan Maguire * 4846c4d0bfb4SAlan Maguire * The string can be subsequently shared with userspace via 4847c4d0bfb4SAlan Maguire * bpf_perf_event_output() or ring buffer interfaces. 4848c4d0bfb4SAlan Maguire * bpf_trace_printk() is to be avoided as it places too small 4849c4d0bfb4SAlan Maguire * a limit on string size to be useful. 4850c4d0bfb4SAlan Maguire * 4851c4d0bfb4SAlan Maguire * *flags* is a combination of 4852c4d0bfb4SAlan Maguire * 4853c4d0bfb4SAlan Maguire * **BTF_F_COMPACT** 4854c4d0bfb4SAlan Maguire * no formatting around type information 4855c4d0bfb4SAlan Maguire * **BTF_F_NONAME** 4856c4d0bfb4SAlan Maguire * no struct/union member names/types 4857c4d0bfb4SAlan Maguire * **BTF_F_PTR_RAW** 4858c4d0bfb4SAlan Maguire * show raw (unobfuscated) pointer values; 4859c4d0bfb4SAlan Maguire * equivalent to printk specifier %px. 4860c4d0bfb4SAlan Maguire * **BTF_F_ZERO** 4861c4d0bfb4SAlan Maguire * show zero-valued struct/union members; they 4862c4d0bfb4SAlan Maguire * are not displayed by default 4863c4d0bfb4SAlan Maguire * 4864c4d0bfb4SAlan Maguire * Return 4865c4d0bfb4SAlan Maguire * The number of bytes that were written (or would have been 4866c4d0bfb4SAlan Maguire * written if output had to be truncated due to string size), 4867c4d0bfb4SAlan Maguire * or a negative error in cases of failure. 4868eb411377SAlan Maguire * 4869eb411377SAlan Maguire * long bpf_seq_printf_btf(struct seq_file *m, struct btf_ptr *ptr, u32 ptr_size, u64 flags) 4870eb411377SAlan Maguire * Description 4871eb411377SAlan Maguire * Use BTF to write to seq_write a string representation of 4872eb411377SAlan Maguire * *ptr*->ptr, using *ptr*->type_id as per bpf_snprintf_btf(). 4873eb411377SAlan Maguire * *flags* are identical to those used for bpf_snprintf_btf. 4874eb411377SAlan Maguire * Return 4875eb411377SAlan Maguire * 0 on success or a negative error in case of failure. 4876b426ce83SDaniel Borkmann * 4877b426ce83SDaniel Borkmann * u64 bpf_skb_cgroup_classid(struct sk_buff *skb) 4878b426ce83SDaniel Borkmann * Description 4879b426ce83SDaniel Borkmann * See **bpf_get_cgroup_classid**\ () for the main description. 4880b426ce83SDaniel Borkmann * This helper differs from **bpf_get_cgroup_classid**\ () in that 4881b426ce83SDaniel Borkmann * the cgroup v1 net_cls class is retrieved only from the *skb*'s 4882b426ce83SDaniel Borkmann * associated socket instead of the current process. 4883b426ce83SDaniel Borkmann * Return 4884b426ce83SDaniel Borkmann * The id is returned or 0 in case the id could not be retrieved. 4885b4ab3141SDaniel Borkmann * 4886ba452c9eSToke Høiland-Jørgensen * long bpf_redirect_neigh(u32 ifindex, struct bpf_redir_neigh *params, int plen, u64 flags) 4887b4ab3141SDaniel Borkmann * Description 4888b4ab3141SDaniel Borkmann * Redirect the packet to another net device of index *ifindex* 4889b4ab3141SDaniel Borkmann * and fill in L2 addresses from neighboring subsystem. This helper 4890b4ab3141SDaniel Borkmann * is somewhat similar to **bpf_redirect**\ (), except that it 4891dd2ce6a5SDaniel Borkmann * populates L2 addresses as well, meaning, internally, the helper 4892ba452c9eSToke Høiland-Jørgensen * relies on the neighbor lookup for the L2 address of the nexthop. 4893ba452c9eSToke Høiland-Jørgensen * 4894ba452c9eSToke Høiland-Jørgensen * The helper will perform a FIB lookup based on the skb's 4895ba452c9eSToke Høiland-Jørgensen * networking header to get the address of the next hop, unless 4896ba452c9eSToke Høiland-Jørgensen * this is supplied by the caller in the *params* argument. The 4897ba452c9eSToke Høiland-Jørgensen * *plen* argument indicates the len of *params* and should be set 4898ba452c9eSToke Høiland-Jørgensen * to 0 if *params* is NULL. 4899dd2ce6a5SDaniel Borkmann * 4900b4ab3141SDaniel Borkmann * The *flags* argument is reserved and must be 0. The helper is 4901dd2ce6a5SDaniel Borkmann * currently only supported for tc BPF program types, and enabled 4902dd2ce6a5SDaniel Borkmann * for IPv4 and IPv6 protocols. 4903b4ab3141SDaniel Borkmann * Return 4904b4ab3141SDaniel Borkmann * The helper returns **TC_ACT_REDIRECT** on success or 4905b4ab3141SDaniel Borkmann * **TC_ACT_SHOT** on error. 4906eaa6bcb7SHao Luo * 4907eaa6bcb7SHao Luo * void *bpf_per_cpu_ptr(const void *percpu_ptr, u32 cpu) 4908eaa6bcb7SHao Luo * Description 4909eaa6bcb7SHao Luo * Take a pointer to a percpu ksym, *percpu_ptr*, and return a 4910eaa6bcb7SHao Luo * pointer to the percpu kernel variable on *cpu*. A ksym is an 4911eaa6bcb7SHao Luo * extern variable decorated with '__ksym'. For ksym, there is a 4912eaa6bcb7SHao Luo * global var (either static or global) defined of the same name 4913eaa6bcb7SHao Luo * in the kernel. The ksym is percpu if the global var is percpu. 4914eaa6bcb7SHao Luo * The returned pointer points to the global percpu var on *cpu*. 4915eaa6bcb7SHao Luo * 4916eaa6bcb7SHao Luo * bpf_per_cpu_ptr() has the same semantic as per_cpu_ptr() in the 4917eaa6bcb7SHao Luo * kernel, except that bpf_per_cpu_ptr() may return NULL. This 4918eaa6bcb7SHao Luo * happens if *cpu* is larger than nr_cpu_ids. The caller of 4919eaa6bcb7SHao Luo * bpf_per_cpu_ptr() must check the returned value. 4920eaa6bcb7SHao Luo * Return 4921eaa6bcb7SHao Luo * A pointer pointing to the kernel percpu variable on *cpu*, or 4922eaa6bcb7SHao Luo * NULL, if *cpu* is invalid. 492363d9b80dSHao Luo * 492463d9b80dSHao Luo * void *bpf_this_cpu_ptr(const void *percpu_ptr) 492563d9b80dSHao Luo * Description 492663d9b80dSHao Luo * Take a pointer to a percpu ksym, *percpu_ptr*, and return a 492763d9b80dSHao Luo * pointer to the percpu kernel variable on this cpu. See the 492863d9b80dSHao Luo * description of 'ksym' in **bpf_per_cpu_ptr**\ (). 492963d9b80dSHao Luo * 493063d9b80dSHao Luo * bpf_this_cpu_ptr() has the same semantic as this_cpu_ptr() in 493163d9b80dSHao Luo * the kernel. Different from **bpf_per_cpu_ptr**\ (), it would 493263d9b80dSHao Luo * never return NULL. 493363d9b80dSHao Luo * Return 493463d9b80dSHao Luo * A pointer pointing to the kernel percpu variable on this cpu. 49359aa1206eSDaniel Borkmann * 49369aa1206eSDaniel Borkmann * long bpf_redirect_peer(u32 ifindex, u64 flags) 49379aa1206eSDaniel Borkmann * Description 49389aa1206eSDaniel Borkmann * Redirect the packet to another net device of index *ifindex*. 49399aa1206eSDaniel Borkmann * This helper is somewhat similar to **bpf_redirect**\ (), except 49409aa1206eSDaniel Borkmann * that the redirection happens to the *ifindex*' peer device and 49419aa1206eSDaniel Borkmann * the netns switch takes place from ingress to ingress without 49429aa1206eSDaniel Borkmann * going through the CPU's backlog queue. 49439aa1206eSDaniel Borkmann * 49449aa1206eSDaniel Borkmann * The *flags* argument is reserved and must be 0. The helper is 4945f98df79bSVictor Stewart * currently only supported for tc BPF program types at the 4946f98df79bSVictor Stewart * ingress hook and for veth and netkit target device types. The 4947f98df79bSVictor Stewart * peer device must reside in a different network namespace. 49489aa1206eSDaniel Borkmann * Return 49499aa1206eSDaniel Borkmann * The helper returns **TC_ACT_REDIRECT** on success or 49509aa1206eSDaniel Borkmann * **TC_ACT_SHOT** on error. 49514cf1bc1fSKP Singh * 49524cf1bc1fSKP Singh * void *bpf_task_storage_get(struct bpf_map *map, struct task_struct *task, void *value, u64 flags) 49534cf1bc1fSKP Singh * Description 49544cf1bc1fSKP Singh * Get a bpf_local_storage from the *task*. 49554cf1bc1fSKP Singh * 49564cf1bc1fSKP Singh * Logically, it could be thought of as getting the value from 49574cf1bc1fSKP Singh * a *map* with *task* as the **key**. From this 49584cf1bc1fSKP Singh * perspective, the usage is not much different from 49594cf1bc1fSKP Singh * **bpf_map_lookup_elem**\ (*map*, **&**\ *task*) except this 4960aa75622cSQuentin Monnet * helper enforces the key must be a task_struct and the map must also 49614cf1bc1fSKP Singh * be a **BPF_MAP_TYPE_TASK_STORAGE**. 49624cf1bc1fSKP Singh * 49634cf1bc1fSKP Singh * Underneath, the value is stored locally at *task* instead of 49644cf1bc1fSKP Singh * the *map*. The *map* is used as the bpf-local-storage 49654cf1bc1fSKP Singh * "type". The bpf-local-storage "type" (i.e. the *map*) is 49664cf1bc1fSKP Singh * searched against all bpf_local_storage residing at *task*. 49674cf1bc1fSKP Singh * 49684cf1bc1fSKP Singh * An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be 49694cf1bc1fSKP Singh * used such that a new bpf_local_storage will be 49704cf1bc1fSKP Singh * created if one does not exist. *value* can be used 49714cf1bc1fSKP Singh * together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify 49724cf1bc1fSKP Singh * the initial value of a bpf_local_storage. If *value* is 49734cf1bc1fSKP Singh * **NULL**, the new bpf_local_storage will be zero initialized. 49744cf1bc1fSKP Singh * Return 49754cf1bc1fSKP Singh * A bpf_local_storage pointer is returned on success. 49764cf1bc1fSKP Singh * 49774cf1bc1fSKP Singh * **NULL** if not found or there was an error in adding 49784cf1bc1fSKP Singh * a new bpf_local_storage. 49794cf1bc1fSKP Singh * 49804cf1bc1fSKP Singh * long bpf_task_storage_delete(struct bpf_map *map, struct task_struct *task) 49814cf1bc1fSKP Singh * Description 49824cf1bc1fSKP Singh * Delete a bpf_local_storage from a *task*. 49834cf1bc1fSKP Singh * Return 49844cf1bc1fSKP Singh * 0 on success. 49854cf1bc1fSKP Singh * 49864cf1bc1fSKP Singh * **-ENOENT** if the bpf_local_storage cannot be found. 49873ca1032aSKP Singh * 49883ca1032aSKP Singh * struct task_struct *bpf_get_current_task_btf(void) 49893ca1032aSKP Singh * Description 49903ca1032aSKP Singh * Return a BTF pointer to the "current" task. 49913ca1032aSKP Singh * This pointer can also be used in helpers that accept an 49923ca1032aSKP Singh * *ARG_PTR_TO_BTF_ID* of type *task_struct*. 49933ca1032aSKP Singh * Return 49943ca1032aSKP Singh * Pointer to the current task. 49953f6719c7SKP Singh * 49963f6719c7SKP Singh * long bpf_bprm_opts_set(struct linux_binprm *bprm, u64 flags) 49973f6719c7SKP Singh * Description 49983f6719c7SKP Singh * Set or clear certain options on *bprm*: 49993f6719c7SKP Singh * 50003f6719c7SKP Singh * **BPF_F_BPRM_SECUREEXEC** Set the secureexec bit 50013f6719c7SKP Singh * which sets the **AT_SECURE** auxv for glibc. The bit 50023f6719c7SKP Singh * is cleared if the flag is not specified. 50033f6719c7SKP Singh * Return 50043f6719c7SKP Singh * **-EINVAL** if invalid *flags* are passed, zero otherwise. 5005d0551261SDmitrii Banshchikov * 5006d0551261SDmitrii Banshchikov * u64 bpf_ktime_get_coarse_ns(void) 5007d0551261SDmitrii Banshchikov * Description 5008d0551261SDmitrii Banshchikov * Return a coarse-grained version of the time elapsed since 5009d0551261SDmitrii Banshchikov * system boot, in nanoseconds. Does not include time the system 5010d0551261SDmitrii Banshchikov * was suspended. 5011d0551261SDmitrii Banshchikov * 5012d0551261SDmitrii Banshchikov * See: **clock_gettime**\ (**CLOCK_MONOTONIC_COARSE**) 5013d0551261SDmitrii Banshchikov * Return 5014d0551261SDmitrii Banshchikov * Current *ktime*. 501527672f0dSKP Singh * 501627672f0dSKP Singh * long bpf_ima_inode_hash(struct inode *inode, void *dst, u32 size) 501727672f0dSKP Singh * Description 5018aa75622cSQuentin Monnet * Returns the stored IMA hash of the *inode* (if it's available). 501927672f0dSKP Singh * If the hash is larger than *size*, then only *size* 502027672f0dSKP Singh * bytes will be copied to *dst* 502127672f0dSKP Singh * Return 502227672f0dSKP Singh * The **hash_algo** is returned on success, 502327672f0dSKP Singh * **-EOPNOTSUP** if IMA is disabled or **-EINVAL** if 502427672f0dSKP Singh * invalid arguments are passed. 50254f19cab7SFlorent Revest * 50264f19cab7SFlorent Revest * struct socket *bpf_sock_from_file(struct file *file) 50274f19cab7SFlorent Revest * Description 50284f19cab7SFlorent Revest * If the given file represents a socket, returns the associated 50294f19cab7SFlorent Revest * socket. 50304f19cab7SFlorent Revest * Return 50314f19cab7SFlorent Revest * A pointer to a struct socket on success or NULL if the file is 50324f19cab7SFlorent Revest * not a socket. 503334b2021cSJesper Dangaard Brouer * 503434b2021cSJesper Dangaard Brouer * long bpf_check_mtu(void *ctx, u32 ifindex, u32 *mtu_len, s32 len_diff, u64 flags) 503534b2021cSJesper Dangaard Brouer * Description 5036e5e35e75SJesper Dangaard Brouer * Check packet size against exceeding MTU of net device (based 503734b2021cSJesper Dangaard Brouer * on *ifindex*). This helper will likely be used in combination 503834b2021cSJesper Dangaard Brouer * with helpers that adjust/change the packet size. 503934b2021cSJesper Dangaard Brouer * 504034b2021cSJesper Dangaard Brouer * The argument *len_diff* can be used for querying with a planned 504134b2021cSJesper Dangaard Brouer * size change. This allows to check MTU prior to changing packet 5042aa75622cSQuentin Monnet * ctx. Providing a *len_diff* adjustment that is larger than the 504334b2021cSJesper Dangaard Brouer * actual packet size (resulting in negative packet size) will in 5044aa75622cSQuentin Monnet * principle not exceed the MTU, which is why it is not considered 5045aa75622cSQuentin Monnet * a failure. Other BPF helpers are needed for performing the 5046aa75622cSQuentin Monnet * planned size change; therefore the responsibility for catching 5047aa75622cSQuentin Monnet * a negative packet size belongs in those helpers. 504834b2021cSJesper Dangaard Brouer * 504934b2021cSJesper Dangaard Brouer * Specifying *ifindex* zero means the MTU check is performed 505034b2021cSJesper Dangaard Brouer * against the current net device. This is practical if this isn't 505134b2021cSJesper Dangaard Brouer * used prior to redirect. 505234b2021cSJesper Dangaard Brouer * 5053e5e35e75SJesper Dangaard Brouer * On input *mtu_len* must be a valid pointer, else verifier will 5054e5e35e75SJesper Dangaard Brouer * reject BPF program. If the value *mtu_len* is initialized to 5055e5e35e75SJesper Dangaard Brouer * zero then the ctx packet size is use. When value *mtu_len* is 5056e5e35e75SJesper Dangaard Brouer * provided as input this specify the L3 length that the MTU check 5057e5e35e75SJesper Dangaard Brouer * is done against. Remember XDP and TC length operate at L2, but 5058e5e35e75SJesper Dangaard Brouer * this value is L3 as this correlate to MTU and IP-header tot_len 5059e5e35e75SJesper Dangaard Brouer * values which are L3 (similar behavior as bpf_fib_lookup). 5060e5e35e75SJesper Dangaard Brouer * 506134b2021cSJesper Dangaard Brouer * The Linux kernel route table can configure MTUs on a more 506234b2021cSJesper Dangaard Brouer * specific per route level, which is not provided by this helper. 506334b2021cSJesper Dangaard Brouer * For route level MTU checks use the **bpf_fib_lookup**\ () 506434b2021cSJesper Dangaard Brouer * helper. 506534b2021cSJesper Dangaard Brouer * 506634b2021cSJesper Dangaard Brouer * *ctx* is either **struct xdp_md** for XDP programs or 506734b2021cSJesper Dangaard Brouer * **struct sk_buff** for tc cls_act programs. 506834b2021cSJesper Dangaard Brouer * 506934b2021cSJesper Dangaard Brouer * The *flags* argument can be a combination of one or more of the 507034b2021cSJesper Dangaard Brouer * following values: 507134b2021cSJesper Dangaard Brouer * 507234b2021cSJesper Dangaard Brouer * **BPF_MTU_CHK_SEGS** 507334b2021cSJesper Dangaard Brouer * This flag will only works for *ctx* **struct sk_buff**. 507434b2021cSJesper Dangaard Brouer * If packet context contains extra packet segment buffers 507534b2021cSJesper Dangaard Brouer * (often knows as GSO skb), then MTU check is harder to 507634b2021cSJesper Dangaard Brouer * check at this point, because in transmit path it is 507734b2021cSJesper Dangaard Brouer * possible for the skb packet to get re-segmented 507834b2021cSJesper Dangaard Brouer * (depending on net device features). This could still be 507934b2021cSJesper Dangaard Brouer * a MTU violation, so this flag enables performing MTU 508034b2021cSJesper Dangaard Brouer * check against segments, with a different violation 508134b2021cSJesper Dangaard Brouer * return code to tell it apart. Check cannot use len_diff. 508234b2021cSJesper Dangaard Brouer * 508334b2021cSJesper Dangaard Brouer * On return *mtu_len* pointer contains the MTU value of the net 508434b2021cSJesper Dangaard Brouer * device. Remember the net device configured MTU is the L3 size, 5085e5e35e75SJesper Dangaard Brouer * which is returned here and XDP and TC length operate at L2. 508634b2021cSJesper Dangaard Brouer * Helper take this into account for you, but remember when using 5087e5e35e75SJesper Dangaard Brouer * MTU value in your BPF-code. 508834b2021cSJesper Dangaard Brouer * 508934b2021cSJesper Dangaard Brouer * Return 509034b2021cSJesper Dangaard Brouer * * 0 on success, and populate MTU value in *mtu_len* pointer. 509134b2021cSJesper Dangaard Brouer * 509234b2021cSJesper Dangaard Brouer * * < 0 if any input argument is invalid (*mtu_len* not updated) 509334b2021cSJesper Dangaard Brouer * 509434b2021cSJesper Dangaard Brouer * MTU violations return positive values, but also populate MTU 509534b2021cSJesper Dangaard Brouer * value in *mtu_len* pointer, as this can be needed for 509634b2021cSJesper Dangaard Brouer * implementing PMTU handing: 509734b2021cSJesper Dangaard Brouer * 509834b2021cSJesper Dangaard Brouer * * **BPF_MTU_CHK_RET_FRAG_NEEDED** 509934b2021cSJesper Dangaard Brouer * * **BPF_MTU_CHK_RET_SEGS_TOOBIG** 510034b2021cSJesper Dangaard Brouer * 510169c087baSYonghong Song * long bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, void *callback_ctx, u64 flags) 510269c087baSYonghong Song * Description 510369c087baSYonghong Song * For each element in **map**, call **callback_fn** function with 510469c087baSYonghong Song * **map**, **callback_ctx** and other map-specific parameters. 510569c087baSYonghong Song * The **callback_fn** should be a static function and 510669c087baSYonghong Song * the **callback_ctx** should be a pointer to the stack. 510769c087baSYonghong Song * The **flags** is used to control certain aspects of the helper. 510869c087baSYonghong Song * Currently, the **flags** must be 0. 510969c087baSYonghong Song * 511069c087baSYonghong Song * The following are a list of supported map types and their 511169c087baSYonghong Song * respective expected callback signatures: 511269c087baSYonghong Song * 511369c087baSYonghong Song * BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_PERCPU_HASH, 511469c087baSYonghong Song * BPF_MAP_TYPE_LRU_HASH, BPF_MAP_TYPE_LRU_PERCPU_HASH, 511569c087baSYonghong Song * BPF_MAP_TYPE_ARRAY, BPF_MAP_TYPE_PERCPU_ARRAY 511669c087baSYonghong Song * 511769c087baSYonghong Song * long (\*callback_fn)(struct bpf_map \*map, const void \*key, void \*value, void \*ctx); 511869c087baSYonghong Song * 511969c087baSYonghong Song * For per_cpu maps, the map_value is the value on the cpu where the 512069c087baSYonghong Song * bpf_prog is running. 512169c087baSYonghong Song * 512269c087baSYonghong Song * If **callback_fn** return 0, the helper will continue to the next 512369c087baSYonghong Song * element. If return value is 1, the helper will skip the rest of 512469c087baSYonghong Song * elements and return. Other return values are not used now. 512569c087baSYonghong Song * 512669c087baSYonghong Song * Return 512769c087baSYonghong Song * The number of traversed map elements for success, **-EINVAL** for 512869c087baSYonghong Song * invalid **flags**. 51297b15523aSFlorent Revest * 51307b15523aSFlorent Revest * long bpf_snprintf(char *str, u32 str_size, const char *fmt, u64 *data, u32 data_len) 51317b15523aSFlorent Revest * Description 51327b15523aSFlorent Revest * Outputs a string into the **str** buffer of size **str_size** 51337b15523aSFlorent Revest * based on a format string stored in a read-only map pointed by 51347b15523aSFlorent Revest * **fmt**. 51357b15523aSFlorent Revest * 51367b15523aSFlorent Revest * Each format specifier in **fmt** corresponds to one u64 element 51377b15523aSFlorent Revest * in the **data** array. For strings and pointers where pointees 51387b15523aSFlorent Revest * are accessed, only the pointer values are stored in the *data* 5139a42effb0SDave Marchevsky * array. The *data_len* is the size of *data* in bytes - must be 5140a42effb0SDave Marchevsky * a multiple of 8. 51417b15523aSFlorent Revest * 51427b15523aSFlorent Revest * Formats **%s** and **%p{i,I}{4,6}** require to read kernel 51437b15523aSFlorent Revest * memory. Reading kernel memory may fail due to either invalid 51447b15523aSFlorent Revest * address or valid address but requiring a major memory fault. If 51457b15523aSFlorent Revest * reading kernel memory fails, the string for **%s** will be an 51467b15523aSFlorent Revest * empty string, and the ip address for **%p{i,I}{4,6}** will be 0. 51477b15523aSFlorent Revest * Not returning error to bpf program is consistent with what 51487b15523aSFlorent Revest * **bpf_trace_printk**\ () does for now. 51497b15523aSFlorent Revest * 51507b15523aSFlorent Revest * Return 51517b15523aSFlorent Revest * The strictly positive length of the formatted string, including 51527b15523aSFlorent Revest * the trailing zero character. If the return value is greater than 51537b15523aSFlorent Revest * **str_size**, **str** contains a truncated string, guaranteed to 51547b15523aSFlorent Revest * be zero-terminated except when **str_size** is 0. 51557b15523aSFlorent Revest * 51567b15523aSFlorent Revest * Or **-EBUSY** if the per-CPU memory copy buffer is busy. 515779a7f8bdSAlexei Starovoitov * 515879a7f8bdSAlexei Starovoitov * long bpf_sys_bpf(u32 cmd, void *attr, u32 attr_size) 515979a7f8bdSAlexei Starovoitov * Description 516079a7f8bdSAlexei Starovoitov * Execute bpf syscall with given arguments. 516179a7f8bdSAlexei Starovoitov * Return 516279a7f8bdSAlexei Starovoitov * A syscall result. 51633d78417bSAlexei Starovoitov * 51643d78417bSAlexei Starovoitov * long bpf_btf_find_by_name_kind(char *name, int name_sz, u32 kind, int flags) 51653d78417bSAlexei Starovoitov * Description 51663d78417bSAlexei Starovoitov * Find BTF type with given name and kind in vmlinux BTF or in module's BTFs. 51673d78417bSAlexei Starovoitov * Return 51683d78417bSAlexei Starovoitov * Returns btf_id and btf_obj_fd in lower and upper 32 bits. 51693abea089SAlexei Starovoitov * 51703abea089SAlexei Starovoitov * long bpf_sys_close(u32 fd) 51713abea089SAlexei Starovoitov * Description 51723abea089SAlexei Starovoitov * Execute close syscall for given FD. 51733abea089SAlexei Starovoitov * Return 51743abea089SAlexei Starovoitov * A syscall result. 5175b00628b1SAlexei Starovoitov * 5176b00628b1SAlexei Starovoitov * long bpf_timer_init(struct bpf_timer *timer, struct bpf_map *map, u64 flags) 5177b00628b1SAlexei Starovoitov * Description 5178b00628b1SAlexei Starovoitov * Initialize the timer. 5179b00628b1SAlexei Starovoitov * First 4 bits of *flags* specify clockid. 5180b00628b1SAlexei Starovoitov * Only CLOCK_MONOTONIC, CLOCK_REALTIME, CLOCK_BOOTTIME are allowed. 5181b00628b1SAlexei Starovoitov * All other bits of *flags* are reserved. 5182b00628b1SAlexei Starovoitov * The verifier will reject the program if *timer* is not from 5183b00628b1SAlexei Starovoitov * the same *map*. 5184b00628b1SAlexei Starovoitov * Return 5185b00628b1SAlexei Starovoitov * 0 on success. 5186b00628b1SAlexei Starovoitov * **-EBUSY** if *timer* is already initialized. 5187b00628b1SAlexei Starovoitov * **-EINVAL** if invalid *flags* are passed. 5188b00628b1SAlexei Starovoitov * **-EPERM** if *timer* is in a map that doesn't have any user references. 5189b00628b1SAlexei Starovoitov * The user space should either hold a file descriptor to a map with timers 5190b00628b1SAlexei Starovoitov * or pin such map in bpffs. When map is unpinned or file descriptor is 5191b00628b1SAlexei Starovoitov * closed all timers in the map will be cancelled and freed. 5192b00628b1SAlexei Starovoitov * 5193b00628b1SAlexei Starovoitov * long bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn) 5194b00628b1SAlexei Starovoitov * Description 5195b00628b1SAlexei Starovoitov * Configure the timer to call *callback_fn* static function. 5196b00628b1SAlexei Starovoitov * Return 5197b00628b1SAlexei Starovoitov * 0 on success. 5198b00628b1SAlexei Starovoitov * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier. 5199b00628b1SAlexei Starovoitov * **-EPERM** if *timer* is in a map that doesn't have any user references. 5200b00628b1SAlexei Starovoitov * The user space should either hold a file descriptor to a map with timers 5201b00628b1SAlexei Starovoitov * or pin such map in bpffs. When map is unpinned or file descriptor is 5202b00628b1SAlexei Starovoitov * closed all timers in the map will be cancelled and freed. 5203b00628b1SAlexei Starovoitov * 5204b00628b1SAlexei Starovoitov * long bpf_timer_start(struct bpf_timer *timer, u64 nsecs, u64 flags) 5205b00628b1SAlexei Starovoitov * Description 5206b00628b1SAlexei Starovoitov * Set timer expiration N nanoseconds from the current time. The 5207b00628b1SAlexei Starovoitov * configured callback will be invoked in soft irq context on some cpu 5208b00628b1SAlexei Starovoitov * and will not repeat unless another bpf_timer_start() is made. 5209b00628b1SAlexei Starovoitov * In such case the next invocation can migrate to a different cpu. 5210b00628b1SAlexei Starovoitov * Since struct bpf_timer is a field inside map element the map 5211b00628b1SAlexei Starovoitov * owns the timer. The bpf_timer_set_callback() will increment refcnt 5212b00628b1SAlexei Starovoitov * of BPF program to make sure that callback_fn code stays valid. 5213b00628b1SAlexei Starovoitov * When user space reference to a map reaches zero all timers 5214b00628b1SAlexei Starovoitov * in a map are cancelled and corresponding program's refcnts are 5215b00628b1SAlexei Starovoitov * decremented. This is done to make sure that Ctrl-C of a user 5216b00628b1SAlexei Starovoitov * process doesn't leave any timers running. If map is pinned in 5217b00628b1SAlexei Starovoitov * bpffs the callback_fn can re-arm itself indefinitely. 5218b00628b1SAlexei Starovoitov * bpf_map_update/delete_elem() helpers and user space sys_bpf commands 5219b00628b1SAlexei Starovoitov * cancel and free the timer in the given map element. 5220b00628b1SAlexei Starovoitov * The map can contain timers that invoke callback_fn-s from different 5221b00628b1SAlexei Starovoitov * programs. The same callback_fn can serve different timers from 5222b00628b1SAlexei Starovoitov * different maps if key/value layout matches across maps. 5223b00628b1SAlexei Starovoitov * Every bpf_timer_set_callback() can have different callback_fn. 5224b00628b1SAlexei Starovoitov * 5225f71f8530STero Kristo * *flags* can be one of: 5226f71f8530STero Kristo * 5227f71f8530STero Kristo * **BPF_F_TIMER_ABS** 5228f71f8530STero Kristo * Start the timer in absolute expire value instead of the 5229f71f8530STero Kristo * default relative one. 5230d6247ecbSDavid Vernet * **BPF_F_TIMER_CPU_PIN** 5231d6247ecbSDavid Vernet * Timer will be pinned to the CPU of the caller. 5232f71f8530STero Kristo * 5233b00628b1SAlexei Starovoitov * Return 5234b00628b1SAlexei Starovoitov * 0 on success. 5235b00628b1SAlexei Starovoitov * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier 5236b00628b1SAlexei Starovoitov * or invalid *flags* are passed. 5237b00628b1SAlexei Starovoitov * 5238b00628b1SAlexei Starovoitov * long bpf_timer_cancel(struct bpf_timer *timer) 5239b00628b1SAlexei Starovoitov * Description 5240b00628b1SAlexei Starovoitov * Cancel the timer and wait for callback_fn to finish if it was running. 5241b00628b1SAlexei Starovoitov * Return 5242b00628b1SAlexei Starovoitov * 0 if the timer was not active. 5243b00628b1SAlexei Starovoitov * 1 if the timer was active. 5244b00628b1SAlexei Starovoitov * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier. 5245b00628b1SAlexei Starovoitov * **-EDEADLK** if callback_fn tried to call bpf_timer_cancel() on its 5246b00628b1SAlexei Starovoitov * own timer which would have led to a deadlock otherwise. 52479b99edcaSJiri Olsa * 52489b99edcaSJiri Olsa * u64 bpf_get_func_ip(void *ctx) 52499b99edcaSJiri Olsa * Description 52509ffd9f3fSJiri Olsa * Get address of the traced function (for tracing and kprobe programs). 5251a3c485a5SJiri Olsa * 5252a3c485a5SJiri Olsa * When called for kprobe program attached as uprobe it returns 5253a3c485a5SJiri Olsa * probe address for both entry and return uprobe. 5254a3c485a5SJiri Olsa * 52559b99edcaSJiri Olsa * Return 5256a3c485a5SJiri Olsa * Address of the traced function for kprobe. 52570e253f7eSJiri Olsa * 0 for kprobes placed within the function (not at the entry). 5258a3c485a5SJiri Olsa * Address of the probe for uprobe and return uprobe. 52597adfc6c9SAndrii Nakryiko * 52607adfc6c9SAndrii Nakryiko * u64 bpf_get_attach_cookie(void *ctx) 52617adfc6c9SAndrii Nakryiko * Description 52627adfc6c9SAndrii Nakryiko * Get bpf_cookie value provided (optionally) during the program 52637adfc6c9SAndrii Nakryiko * attachment. It might be different for each individual 52647adfc6c9SAndrii Nakryiko * attachment, even if BPF program itself is the same. 52657adfc6c9SAndrii Nakryiko * Expects BPF program context *ctx* as a first argument. 52667adfc6c9SAndrii Nakryiko * 52677adfc6c9SAndrii Nakryiko * Supported for the following program types: 52687adfc6c9SAndrii Nakryiko * - kprobe/uprobe; 52697adfc6c9SAndrii Nakryiko * - tracepoint; 52707adfc6c9SAndrii Nakryiko * - perf_event. 52717adfc6c9SAndrii Nakryiko * Return 52727adfc6c9SAndrii Nakryiko * Value specified by user at BPF link creation/attachment time 52737adfc6c9SAndrii Nakryiko * or 0, if it was not specified. 5274dd6e10fbSDaniel Xu * 5275dd6e10fbSDaniel Xu * long bpf_task_pt_regs(struct task_struct *task) 5276dd6e10fbSDaniel Xu * Description 5277dd6e10fbSDaniel Xu * Get the struct pt_regs associated with **task**. 5278dd6e10fbSDaniel Xu * Return 5279dd6e10fbSDaniel Xu * A pointer to struct pt_regs. 5280856c02dbSSong Liu * 5281856c02dbSSong Liu * long bpf_get_branch_snapshot(void *entries, u32 size, u64 flags) 5282856c02dbSSong Liu * Description 5283856c02dbSSong Liu * Get branch trace from hardware engines like Intel LBR. The 5284856c02dbSSong Liu * hardware engine is stopped shortly after the helper is 5285856c02dbSSong Liu * called. Therefore, the user need to filter branch entries 5286856c02dbSSong Liu * based on the actual use case. To capture branch trace 5287856c02dbSSong Liu * before the trigger point of the BPF program, the helper 5288856c02dbSSong Liu * should be called at the beginning of the BPF program. 5289856c02dbSSong Liu * 5290856c02dbSSong Liu * The data is stored as struct perf_branch_entry into output 5291856c02dbSSong Liu * buffer *entries*. *size* is the size of *entries* in bytes. 5292856c02dbSSong Liu * *flags* is reserved for now and must be zero. 5293856c02dbSSong Liu * 5294856c02dbSSong Liu * Return 5295856c02dbSSong Liu * On success, number of bytes written to *buf*. On error, a 5296856c02dbSSong Liu * negative value. 5297856c02dbSSong Liu * 5298856c02dbSSong Liu * **-EINVAL** if *flags* is not zero. 5299856c02dbSSong Liu * 5300856c02dbSSong Liu * **-ENOENT** if architecture does not support branch records. 530110aceb62SDave Marchevsky * 530210aceb62SDave Marchevsky * long bpf_trace_vprintk(const char *fmt, u32 fmt_size, const void *data, u32 data_len) 530310aceb62SDave Marchevsky * Description 530410aceb62SDave Marchevsky * Behaves like **bpf_trace_printk**\ () helper, but takes an array of u64 530510aceb62SDave Marchevsky * to format and can handle more format args as a result. 530610aceb62SDave Marchevsky * 530710aceb62SDave Marchevsky * Arguments are to be used as in **bpf_seq_printf**\ () helper. 530810aceb62SDave Marchevsky * Return 530910aceb62SDave Marchevsky * The number of bytes written to the buffer, or a negative error 531010aceb62SDave Marchevsky * in case of failure. 53119eeb3aa3SHengqi Chen * 53129eeb3aa3SHengqi Chen * struct unix_sock *bpf_skc_to_unix_sock(void *sk) 53139eeb3aa3SHengqi Chen * Description 53149eeb3aa3SHengqi Chen * Dynamically cast a *sk* pointer to a *unix_sock* pointer. 53159eeb3aa3SHengqi Chen * Return 53169eeb3aa3SHengqi Chen * *sk* if casting is valid, or **NULL** otherwise. 5317d6aef08aSKumar Kartikeya Dwivedi * 5318d6aef08aSKumar Kartikeya Dwivedi * long bpf_kallsyms_lookup_name(const char *name, int name_sz, int flags, u64 *res) 5319d6aef08aSKumar Kartikeya Dwivedi * Description 5320d6aef08aSKumar Kartikeya Dwivedi * Get the address of a kernel symbol, returned in *res*. *res* is 5321d6aef08aSKumar Kartikeya Dwivedi * set to 0 if the symbol is not found. 5322d6aef08aSKumar Kartikeya Dwivedi * Return 5323d6aef08aSKumar Kartikeya Dwivedi * On success, zero. On error, a negative value. 5324d6aef08aSKumar Kartikeya Dwivedi * 5325d6aef08aSKumar Kartikeya Dwivedi * **-EINVAL** if *flags* is not zero. 5326d6aef08aSKumar Kartikeya Dwivedi * 5327d6aef08aSKumar Kartikeya Dwivedi * **-EINVAL** if string *name* is not the same size as *name_sz*. 5328d6aef08aSKumar Kartikeya Dwivedi * 5329d6aef08aSKumar Kartikeya Dwivedi * **-ENOENT** if symbol is not found. 5330d6aef08aSKumar Kartikeya Dwivedi * 5331d6aef08aSKumar Kartikeya Dwivedi * **-EPERM** if caller does not have permission to obtain kernel address. 53327c7e3d31SSong Liu * 53337c7e3d31SSong Liu * long bpf_find_vma(struct task_struct *task, u64 addr, void *callback_fn, void *callback_ctx, u64 flags) 53347c7e3d31SSong Liu * Description 53357c7e3d31SSong Liu * Find vma of *task* that contains *addr*, call *callback_fn* 53367c7e3d31SSong Liu * function with *task*, *vma*, and *callback_ctx*. 53377c7e3d31SSong Liu * The *callback_fn* should be a static function and 53387c7e3d31SSong Liu * the *callback_ctx* should be a pointer to the stack. 53397c7e3d31SSong Liu * The *flags* is used to control certain aspects of the helper. 53407c7e3d31SSong Liu * Currently, the *flags* must be 0. 53417c7e3d31SSong Liu * 53427c7e3d31SSong Liu * The expected callback signature is 53437c7e3d31SSong Liu * 53447c7e3d31SSong Liu * long (\*callback_fn)(struct task_struct \*task, struct vm_area_struct \*vma, void \*callback_ctx); 53457c7e3d31SSong Liu * 53467c7e3d31SSong Liu * Return 53477c7e3d31SSong Liu * 0 on success. 53487c7e3d31SSong Liu * **-ENOENT** if *task->mm* is NULL, or no vma contains *addr*. 53497c7e3d31SSong Liu * **-EBUSY** if failed to try lock mmap_lock. 53507c7e3d31SSong Liu * **-EINVAL** for invalid **flags**. 5351e6f2dd0fSJoanne Koong * 5352e6f2dd0fSJoanne Koong * long bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, u64 flags) 5353e6f2dd0fSJoanne Koong * Description 5354e6f2dd0fSJoanne Koong * For **nr_loops**, call **callback_fn** function 5355e6f2dd0fSJoanne Koong * with **callback_ctx** as the context parameter. 5356e6f2dd0fSJoanne Koong * The **callback_fn** should be a static function and 5357e6f2dd0fSJoanne Koong * the **callback_ctx** should be a pointer to the stack. 5358e6f2dd0fSJoanne Koong * The **flags** is used to control certain aspects of the helper. 5359e6f2dd0fSJoanne Koong * Currently, the **flags** must be 0. Currently, nr_loops is 5360e6f2dd0fSJoanne Koong * limited to 1 << 23 (~8 million) loops. 5361e6f2dd0fSJoanne Koong * 5362e6f2dd0fSJoanne Koong * long (\*callback_fn)(u32 index, void \*ctx); 5363e6f2dd0fSJoanne Koong * 5364e6f2dd0fSJoanne Koong * where **index** is the current index in the loop. The index 5365e6f2dd0fSJoanne Koong * is zero-indexed. 5366e6f2dd0fSJoanne Koong * 5367e6f2dd0fSJoanne Koong * If **callback_fn** returns 0, the helper will continue to the next 5368e6f2dd0fSJoanne Koong * loop. If return value is 1, the helper will skip the rest of 5369e6f2dd0fSJoanne Koong * the loops and return. Other return values are not used now, 5370e6f2dd0fSJoanne Koong * and will be rejected by the verifier. 5371e6f2dd0fSJoanne Koong * 5372e6f2dd0fSJoanne Koong * Return 5373e6f2dd0fSJoanne Koong * The number of loops performed, **-EINVAL** for invalid **flags**, 5374e6f2dd0fSJoanne Koong * **-E2BIG** if **nr_loops** exceeds the maximum number of loops. 5375c5fb1993SHou Tao * 5376c5fb1993SHou Tao * long bpf_strncmp(const char *s1, u32 s1_sz, const char *s2) 5377c5fb1993SHou Tao * Description 5378c5fb1993SHou Tao * Do strncmp() between **s1** and **s2**. **s1** doesn't need 5379c5fb1993SHou Tao * to be null-terminated and **s1_sz** is the maximum storage 5380c5fb1993SHou Tao * size of **s1**. **s2** must be a read-only string. 5381c5fb1993SHou Tao * Return 5382c5fb1993SHou Tao * An integer less than, equal to, or greater than zero 5383c5fb1993SHou Tao * if the first **s1_sz** bytes of **s1** is found to be 5384c5fb1993SHou Tao * less than, to match, or be greater than **s2**. 5385f92c1e18SJiri Olsa * 5386f92c1e18SJiri Olsa * long bpf_get_func_arg(void *ctx, u32 n, u64 *value) 5387f92c1e18SJiri Olsa * Description 538827ed9353SYonghong Song * Get **n**-th argument register (zero based) of the traced function (for tracing programs) 5389f92c1e18SJiri Olsa * returned in **value**. 5390f92c1e18SJiri Olsa * 5391f92c1e18SJiri Olsa * Return 5392f92c1e18SJiri Olsa * 0 on success. 539327ed9353SYonghong Song * **-EINVAL** if n >= argument register count of traced function. 5394f92c1e18SJiri Olsa * 5395f92c1e18SJiri Olsa * long bpf_get_func_ret(void *ctx, u64 *value) 5396f92c1e18SJiri Olsa * Description 5397f92c1e18SJiri Olsa * Get return value of the traced function (for tracing programs) 5398f92c1e18SJiri Olsa * in **value**. 5399f92c1e18SJiri Olsa * 5400f92c1e18SJiri Olsa * Return 5401f92c1e18SJiri Olsa * 0 on success. 5402f92c1e18SJiri Olsa * **-EOPNOTSUPP** for tracing programs other than BPF_TRACE_FEXIT or BPF_MODIFY_RETURN. 5403f92c1e18SJiri Olsa * 5404f92c1e18SJiri Olsa * long bpf_get_func_arg_cnt(void *ctx) 5405f92c1e18SJiri Olsa * Description 540627ed9353SYonghong Song * Get number of registers of the traced function (for tracing programs) where 540727ed9353SYonghong Song * function arguments are stored in these registers. 5408f92c1e18SJiri Olsa * 5409f92c1e18SJiri Olsa * Return 541027ed9353SYonghong Song * The number of argument registers of the traced function. 5411b44123b4SYiFei Zhu * 5412b44123b4SYiFei Zhu * int bpf_get_retval(void) 5413b44123b4SYiFei Zhu * Description 54142172fb80SStanislav Fomichev * Get the BPF program's return value that will be returned to the upper layers. 5415b44123b4SYiFei Zhu * 54162172fb80SStanislav Fomichev * This helper is currently supported by cgroup programs and only by the hooks 54172172fb80SStanislav Fomichev * where BPF program's return value is returned to the userspace via errno. 5418b44123b4SYiFei Zhu * Return 54192172fb80SStanislav Fomichev * The BPF program's return value. 5420b44123b4SYiFei Zhu * 5421b44123b4SYiFei Zhu * int bpf_set_retval(int retval) 5422b44123b4SYiFei Zhu * Description 54232172fb80SStanislav Fomichev * Set the BPF program's return value that will be returned to the upper layers. 5424b44123b4SYiFei Zhu * 54252172fb80SStanislav Fomichev * This helper is currently supported by cgroup programs and only by the hooks 54262172fb80SStanislav Fomichev * where BPF program's return value is returned to the userspace via errno. 54272172fb80SStanislav Fomichev * 54282172fb80SStanislav Fomichev * Note that there is the following corner case where the program exports an error 54292172fb80SStanislav Fomichev * via bpf_set_retval but signals success via 'return 1': 54302172fb80SStanislav Fomichev * 54312172fb80SStanislav Fomichev * bpf_set_retval(-EPERM); 54322172fb80SStanislav Fomichev * return 1; 54332172fb80SStanislav Fomichev * 54342172fb80SStanislav Fomichev * In this case, the BPF program's return value will use helper's -EPERM. This 54352172fb80SStanislav Fomichev * still holds true for cgroup/bind{4,6} which supports extra 'return 3' success case. 54362172fb80SStanislav Fomichev * 5437b44123b4SYiFei Zhu * Return 5438b44123b4SYiFei Zhu * 0 on success, or a negative error in case of failure. 54390165cc81SLorenzo Bianconi * 54400165cc81SLorenzo Bianconi * u64 bpf_xdp_get_buff_len(struct xdp_buff *xdp_md) 54410165cc81SLorenzo Bianconi * Description 54420165cc81SLorenzo Bianconi * Get the total size of a given xdp buff (linear and paged area) 54430165cc81SLorenzo Bianconi * Return 54440165cc81SLorenzo Bianconi * The total size of a given xdp buffer. 54453f364222SLorenzo Bianconi * 54463f364222SLorenzo Bianconi * long bpf_xdp_load_bytes(struct xdp_buff *xdp_md, u32 offset, void *buf, u32 len) 54473f364222SLorenzo Bianconi * Description 54483f364222SLorenzo Bianconi * This helper is provided as an easy way to load data from a 54493f364222SLorenzo Bianconi * xdp buffer. It can be used to load *len* bytes from *offset* from 54503f364222SLorenzo Bianconi * the frame associated to *xdp_md*, into the buffer pointed by 54513f364222SLorenzo Bianconi * *buf*. 54523f364222SLorenzo Bianconi * Return 54533f364222SLorenzo Bianconi * 0 on success, or a negative error in case of failure. 54543f364222SLorenzo Bianconi * 54553f364222SLorenzo Bianconi * long bpf_xdp_store_bytes(struct xdp_buff *xdp_md, u32 offset, void *buf, u32 len) 54563f364222SLorenzo Bianconi * Description 54573f364222SLorenzo Bianconi * Store *len* bytes from buffer *buf* into the frame 54583f364222SLorenzo Bianconi * associated to *xdp_md*, at *offset*. 54593f364222SLorenzo Bianconi * Return 54603f364222SLorenzo Bianconi * 0 on success, or a negative error in case of failure. 5461376040e4SKenny Yu * 5462376040e4SKenny Yu * long bpf_copy_from_user_task(void *dst, u32 size, const void *user_ptr, struct task_struct *tsk, u64 flags) 5463376040e4SKenny Yu * Description 5464376040e4SKenny Yu * Read *size* bytes from user space address *user_ptr* in *tsk*'s 5465376040e4SKenny Yu * address space, and stores the data in *dst*. *flags* is not 5466376040e4SKenny Yu * used yet and is provided for future extensibility. This helper 5467376040e4SKenny Yu * can only be used by sleepable programs. 5468376040e4SKenny Yu * Return 5469376040e4SKenny Yu * 0 on success, or a negative error in case of failure. On error 5470376040e4SKenny Yu * *dst* buffer is zeroed out. 54718d21ec0eSMartin KaFai Lau * 54729bb984f2SMartin KaFai Lau * long bpf_skb_set_tstamp(struct sk_buff *skb, u64 tstamp, u32 tstamp_type) 54738d21ec0eSMartin KaFai Lau * Description 54749bb984f2SMartin KaFai Lau * Change the __sk_buff->tstamp_type to *tstamp_type* 54759bb984f2SMartin KaFai Lau * and set *tstamp* to the __sk_buff->tstamp together. 54768d21ec0eSMartin KaFai Lau * 54779bb984f2SMartin KaFai Lau * If there is no need to change the __sk_buff->tstamp_type, 54789bb984f2SMartin KaFai Lau * the tstamp value can be directly written to __sk_buff->tstamp 54798d21ec0eSMartin KaFai Lau * instead. 54808d21ec0eSMartin KaFai Lau * 54819bb984f2SMartin KaFai Lau * BPF_SKB_TSTAMP_DELIVERY_MONO is the only tstamp that 54829bb984f2SMartin KaFai Lau * will be kept during bpf_redirect_*(). A non zero 54839bb984f2SMartin KaFai Lau * *tstamp* must be used with the BPF_SKB_TSTAMP_DELIVERY_MONO 54849bb984f2SMartin KaFai Lau * *tstamp_type*. 54859bb984f2SMartin KaFai Lau * 54869bb984f2SMartin KaFai Lau * A BPF_SKB_TSTAMP_UNSPEC *tstamp_type* can only be used 54879bb984f2SMartin KaFai Lau * with a zero *tstamp*. 54888d21ec0eSMartin KaFai Lau * 54898d21ec0eSMartin KaFai Lau * Only IPv4 and IPv6 skb->protocol are supported. 54908d21ec0eSMartin KaFai Lau * 54918d21ec0eSMartin KaFai Lau * This function is most useful when it needs to set a 54928d21ec0eSMartin KaFai Lau * mono delivery time to __sk_buff->tstamp and then 54938d21ec0eSMartin KaFai Lau * bpf_redirect_*() to the egress of an iface. For example, 54948d21ec0eSMartin KaFai Lau * changing the (rcv) timestamp in __sk_buff->tstamp at 54958d21ec0eSMartin KaFai Lau * ingress to a mono delivery time and then bpf_redirect_*() 54968d21ec0eSMartin KaFai Lau * to sch_fq@phy-dev. 54978d21ec0eSMartin KaFai Lau * Return 54988d21ec0eSMartin KaFai Lau * 0 on success. 54998d21ec0eSMartin KaFai Lau * **-EINVAL** for invalid input 55009bb984f2SMartin KaFai Lau * **-EOPNOTSUPP** for unsupported protocol 5501174b1694SRoberto Sassu * 5502174b1694SRoberto Sassu * long bpf_ima_file_hash(struct file *file, void *dst, u32 size) 5503174b1694SRoberto Sassu * Description 5504174b1694SRoberto Sassu * Returns a calculated IMA hash of the *file*. 5505174b1694SRoberto Sassu * If the hash is larger than *size*, then only *size* 5506174b1694SRoberto Sassu * bytes will be copied to *dst* 5507174b1694SRoberto Sassu * Return 5508174b1694SRoberto Sassu * The **hash_algo** is returned on success, 5509174b1694SRoberto Sassu * **-EOPNOTSUP** if the hash calculation failed or **-EINVAL** if 5510174b1694SRoberto Sassu * invalid arguments are passed. 5511c0a5a21cSKumar Kartikeya Dwivedi * 5512c0a5a21cSKumar Kartikeya Dwivedi * void *bpf_kptr_xchg(void *map_value, void *ptr) 5513c0a5a21cSKumar Kartikeya Dwivedi * Description 5514c0a5a21cSKumar Kartikeya Dwivedi * Exchange kptr at pointer *map_value* with *ptr*, and return the 5515c0a5a21cSKumar Kartikeya Dwivedi * old value. *ptr* can be NULL, otherwise it must be a referenced 5516c0a5a21cSKumar Kartikeya Dwivedi * pointer which will be released when this helper is called. 5517c0a5a21cSKumar Kartikeya Dwivedi * Return 5518c0a5a21cSKumar Kartikeya Dwivedi * The old value of kptr (which can be NULL). The returned pointer 5519c0a5a21cSKumar Kartikeya Dwivedi * if not NULL, is a reference which must be released using its 5520c0a5a21cSKumar Kartikeya Dwivedi * corresponding release function, or moved into a BPF map before 5521c0a5a21cSKumar Kartikeya Dwivedi * program exit. 552207343110SFeng Zhou * 552307343110SFeng Zhou * void *bpf_map_lookup_percpu_elem(struct bpf_map *map, const void *key, u32 cpu) 552407343110SFeng Zhou * Description 552507343110SFeng Zhou * Perform a lookup in *percpu map* for an entry associated to 552607343110SFeng Zhou * *key* on *cpu*. 552707343110SFeng Zhou * Return 552807343110SFeng Zhou * Map value associated to *key* on *cpu*, or **NULL** if no entry 552907343110SFeng Zhou * was found or *cpu* is invalid. 55303bc253c2SGeliang Tang * 55313bc253c2SGeliang Tang * struct mptcp_sock *bpf_skc_to_mptcp_sock(void *sk) 55323bc253c2SGeliang Tang * Description 55333bc253c2SGeliang Tang * Dynamically cast a *sk* pointer to a *mptcp_sock* pointer. 55343bc253c2SGeliang Tang * Return 55353bc253c2SGeliang Tang * *sk* if casting is valid, or **NULL** otherwise. 5536263ae152SJoanne Koong * 5537263ae152SJoanne Koong * long bpf_dynptr_from_mem(void *data, u32 size, u64 flags, struct bpf_dynptr *ptr) 5538263ae152SJoanne Koong * Description 5539263ae152SJoanne Koong * Get a dynptr to local memory *data*. 5540263ae152SJoanne Koong * 5541263ae152SJoanne Koong * *data* must be a ptr to a map value. 5542263ae152SJoanne Koong * The maximum *size* supported is DYNPTR_MAX_SIZE. 5543263ae152SJoanne Koong * *flags* is currently unused. 5544263ae152SJoanne Koong * Return 5545263ae152SJoanne Koong * 0 on success, -E2BIG if the size exceeds DYNPTR_MAX_SIZE, 5546263ae152SJoanne Koong * -EINVAL if flags is not 0. 5547bc34dee6SJoanne Koong * 5548bc34dee6SJoanne Koong * long bpf_ringbuf_reserve_dynptr(void *ringbuf, u32 size, u64 flags, struct bpf_dynptr *ptr) 5549bc34dee6SJoanne Koong * Description 5550bc34dee6SJoanne Koong * Reserve *size* bytes of payload in a ring buffer *ringbuf* 5551bc34dee6SJoanne Koong * through the dynptr interface. *flags* must be 0. 5552bc34dee6SJoanne Koong * 5553bc34dee6SJoanne Koong * Please note that a corresponding bpf_ringbuf_submit_dynptr or 5554bc34dee6SJoanne Koong * bpf_ringbuf_discard_dynptr must be called on *ptr*, even if the 5555bc34dee6SJoanne Koong * reservation fails. This is enforced by the verifier. 5556bc34dee6SJoanne Koong * Return 5557bc34dee6SJoanne Koong * 0 on success, or a negative error in case of failure. 5558bc34dee6SJoanne Koong * 5559bc34dee6SJoanne Koong * void bpf_ringbuf_submit_dynptr(struct bpf_dynptr *ptr, u64 flags) 5560bc34dee6SJoanne Koong * Description 5561bc34dee6SJoanne Koong * Submit reserved ring buffer sample, pointed to by *data*, 5562bc34dee6SJoanne Koong * through the dynptr interface. This is a no-op if the dynptr is 5563bc34dee6SJoanne Koong * invalid/null. 5564bc34dee6SJoanne Koong * 5565bc34dee6SJoanne Koong * For more information on *flags*, please see 5566bc34dee6SJoanne Koong * 'bpf_ringbuf_submit'. 5567bc34dee6SJoanne Koong * Return 5568bc34dee6SJoanne Koong * Nothing. Always succeeds. 5569bc34dee6SJoanne Koong * 5570bc34dee6SJoanne Koong * void bpf_ringbuf_discard_dynptr(struct bpf_dynptr *ptr, u64 flags) 5571bc34dee6SJoanne Koong * Description 5572bc34dee6SJoanne Koong * Discard reserved ring buffer sample through the dynptr 5573bc34dee6SJoanne Koong * interface. This is a no-op if the dynptr is invalid/null. 5574bc34dee6SJoanne Koong * 5575bc34dee6SJoanne Koong * For more information on *flags*, please see 5576bc34dee6SJoanne Koong * 'bpf_ringbuf_discard'. 5577bc34dee6SJoanne Koong * Return 5578bc34dee6SJoanne Koong * Nothing. Always succeeds. 557913bbbfbeSJoanne Koong * 558027060531SKumar Kartikeya Dwivedi * long bpf_dynptr_read(void *dst, u32 len, const struct bpf_dynptr *src, u32 offset, u64 flags) 558113bbbfbeSJoanne Koong * Description 558213bbbfbeSJoanne Koong * Read *len* bytes from *src* into *dst*, starting from *offset* 558313bbbfbeSJoanne Koong * into *src*. 5584f8d3da4eSJoanne Koong * *flags* is currently unused. 558513bbbfbeSJoanne Koong * Return 558613bbbfbeSJoanne Koong * 0 on success, -E2BIG if *offset* + *len* exceeds the length 5587f8d3da4eSJoanne Koong * of *src*'s data, -EINVAL if *src* is an invalid dynptr or if 5588f8d3da4eSJoanne Koong * *flags* is not 0. 558913bbbfbeSJoanne Koong * 559027060531SKumar Kartikeya Dwivedi * long bpf_dynptr_write(const struct bpf_dynptr *dst, u32 offset, void *src, u32 len, u64 flags) 559113bbbfbeSJoanne Koong * Description 559213bbbfbeSJoanne Koong * Write *len* bytes from *src* into *dst*, starting from *offset* 559313bbbfbeSJoanne Koong * into *dst*. 5594b5964b96SJoanne Koong * 5595b5964b96SJoanne Koong * *flags* must be 0 except for skb-type dynptrs. 5596b5964b96SJoanne Koong * 5597b5964b96SJoanne Koong * For skb-type dynptrs: 559866e3a13eSJoanne Koong * * All data slices of the dynptr are automatically 559966e3a13eSJoanne Koong * invalidated after **bpf_dynptr_write**\ (). This is 560066e3a13eSJoanne Koong * because writing may pull the skb and change the 560166e3a13eSJoanne Koong * underlying packet buffer. 560266e3a13eSJoanne Koong * 5603b5964b96SJoanne Koong * * For *flags*, please see the flags accepted by 5604b5964b96SJoanne Koong * **bpf_skb_store_bytes**\ (). 560513bbbfbeSJoanne Koong * Return 560613bbbfbeSJoanne Koong * 0 on success, -E2BIG if *offset* + *len* exceeds the length 560713bbbfbeSJoanne Koong * of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst* 5608b5964b96SJoanne Koong * is a read-only dynptr or if *flags* is not correct. For skb-type dynptrs, 5609b5964b96SJoanne Koong * other errors correspond to errors returned by **bpf_skb_store_bytes**\ (). 561034d4ef57SJoanne Koong * 561127060531SKumar Kartikeya Dwivedi * void *bpf_dynptr_data(const struct bpf_dynptr *ptr, u32 offset, u32 len) 561234d4ef57SJoanne Koong * Description 561334d4ef57SJoanne Koong * Get a pointer to the underlying dynptr data. 561434d4ef57SJoanne Koong * 561534d4ef57SJoanne Koong * *len* must be a statically known value. The returned data slice 561634d4ef57SJoanne Koong * is invalidated whenever the dynptr is invalidated. 5617b5964b96SJoanne Koong * 561805421aecSJoanne Koong * skb and xdp type dynptrs may not use bpf_dynptr_data. They should 5619b5964b96SJoanne Koong * instead use bpf_dynptr_slice and bpf_dynptr_slice_rdwr. 562034d4ef57SJoanne Koong * Return 562134d4ef57SJoanne Koong * Pointer to the underlying dynptr data, NULL if the dynptr is 562234d4ef57SJoanne Koong * read-only, if the dynptr is invalid, or if the offset and length 562334d4ef57SJoanne Koong * is out of bounds. 562433bf9885SMaxim Mikityanskiy * 562533bf9885SMaxim Mikityanskiy * s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len) 562633bf9885SMaxim Mikityanskiy * Description 562733bf9885SMaxim Mikityanskiy * Try to issue a SYN cookie for the packet with corresponding 562833bf9885SMaxim Mikityanskiy * IPv4/TCP headers, *iph* and *th*, without depending on a 562933bf9885SMaxim Mikityanskiy * listening socket. 563033bf9885SMaxim Mikityanskiy * 563133bf9885SMaxim Mikityanskiy * *iph* points to the IPv4 header. 563233bf9885SMaxim Mikityanskiy * 563333bf9885SMaxim Mikityanskiy * *th* points to the start of the TCP header, while *th_len* 563433bf9885SMaxim Mikityanskiy * contains the length of the TCP header (at least 563533bf9885SMaxim Mikityanskiy * **sizeof**\ (**struct tcphdr**)). 563633bf9885SMaxim Mikityanskiy * Return 563733bf9885SMaxim Mikityanskiy * On success, lower 32 bits hold the generated SYN cookie in 563833bf9885SMaxim Mikityanskiy * followed by 16 bits which hold the MSS value for that cookie, 563933bf9885SMaxim Mikityanskiy * and the top 16 bits are unused. 564033bf9885SMaxim Mikityanskiy * 564133bf9885SMaxim Mikityanskiy * On failure, the returned value is one of the following: 564233bf9885SMaxim Mikityanskiy * 564333bf9885SMaxim Mikityanskiy * **-EINVAL** if *th_len* is invalid. 564433bf9885SMaxim Mikityanskiy * 564533bf9885SMaxim Mikityanskiy * s64 bpf_tcp_raw_gen_syncookie_ipv6(struct ipv6hdr *iph, struct tcphdr *th, u32 th_len) 564633bf9885SMaxim Mikityanskiy * Description 564733bf9885SMaxim Mikityanskiy * Try to issue a SYN cookie for the packet with corresponding 564833bf9885SMaxim Mikityanskiy * IPv6/TCP headers, *iph* and *th*, without depending on a 564933bf9885SMaxim Mikityanskiy * listening socket. 565033bf9885SMaxim Mikityanskiy * 565133bf9885SMaxim Mikityanskiy * *iph* points to the IPv6 header. 565233bf9885SMaxim Mikityanskiy * 565333bf9885SMaxim Mikityanskiy * *th* points to the start of the TCP header, while *th_len* 565433bf9885SMaxim Mikityanskiy * contains the length of the TCP header (at least 565533bf9885SMaxim Mikityanskiy * **sizeof**\ (**struct tcphdr**)). 565633bf9885SMaxim Mikityanskiy * Return 565733bf9885SMaxim Mikityanskiy * On success, lower 32 bits hold the generated SYN cookie in 565833bf9885SMaxim Mikityanskiy * followed by 16 bits which hold the MSS value for that cookie, 565933bf9885SMaxim Mikityanskiy * and the top 16 bits are unused. 566033bf9885SMaxim Mikityanskiy * 566133bf9885SMaxim Mikityanskiy * On failure, the returned value is one of the following: 566233bf9885SMaxim Mikityanskiy * 566333bf9885SMaxim Mikityanskiy * **-EINVAL** if *th_len* is invalid. 566433bf9885SMaxim Mikityanskiy * 566533bf9885SMaxim Mikityanskiy * **-EPROTONOSUPPORT** if CONFIG_IPV6 is not builtin. 566633bf9885SMaxim Mikityanskiy * 566733bf9885SMaxim Mikityanskiy * long bpf_tcp_raw_check_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th) 566833bf9885SMaxim Mikityanskiy * Description 566933bf9885SMaxim Mikityanskiy * Check whether *iph* and *th* contain a valid SYN cookie ACK 567033bf9885SMaxim Mikityanskiy * without depending on a listening socket. 567133bf9885SMaxim Mikityanskiy * 567233bf9885SMaxim Mikityanskiy * *iph* points to the IPv4 header. 567333bf9885SMaxim Mikityanskiy * 567433bf9885SMaxim Mikityanskiy * *th* points to the TCP header. 567533bf9885SMaxim Mikityanskiy * Return 567633bf9885SMaxim Mikityanskiy * 0 if *iph* and *th* are a valid SYN cookie ACK. 567733bf9885SMaxim Mikityanskiy * 567833bf9885SMaxim Mikityanskiy * On failure, the returned value is one of the following: 567933bf9885SMaxim Mikityanskiy * 568033bf9885SMaxim Mikityanskiy * **-EACCES** if the SYN cookie is not valid. 568133bf9885SMaxim Mikityanskiy * 568233bf9885SMaxim Mikityanskiy * long bpf_tcp_raw_check_syncookie_ipv6(struct ipv6hdr *iph, struct tcphdr *th) 568333bf9885SMaxim Mikityanskiy * Description 568433bf9885SMaxim Mikityanskiy * Check whether *iph* and *th* contain a valid SYN cookie ACK 568533bf9885SMaxim Mikityanskiy * without depending on a listening socket. 568633bf9885SMaxim Mikityanskiy * 568733bf9885SMaxim Mikityanskiy * *iph* points to the IPv6 header. 568833bf9885SMaxim Mikityanskiy * 568933bf9885SMaxim Mikityanskiy * *th* points to the TCP header. 569033bf9885SMaxim Mikityanskiy * Return 569133bf9885SMaxim Mikityanskiy * 0 if *iph* and *th* are a valid SYN cookie ACK. 569233bf9885SMaxim Mikityanskiy * 569333bf9885SMaxim Mikityanskiy * On failure, the returned value is one of the following: 569433bf9885SMaxim Mikityanskiy * 569533bf9885SMaxim Mikityanskiy * **-EACCES** if the SYN cookie is not valid. 569633bf9885SMaxim Mikityanskiy * 569733bf9885SMaxim Mikityanskiy * **-EPROTONOSUPPORT** if CONFIG_IPV6 is not builtin. 5698c8996c98SJesper Dangaard Brouer * 5699c8996c98SJesper Dangaard Brouer * u64 bpf_ktime_get_tai_ns(void) 5700c8996c98SJesper Dangaard Brouer * Description 5701c8996c98SJesper Dangaard Brouer * A nonsettable system-wide clock derived from wall-clock time but 5702c8996c98SJesper Dangaard Brouer * ignoring leap seconds. This clock does not experience 5703c8996c98SJesper Dangaard Brouer * discontinuities and backwards jumps caused by NTP inserting leap 5704c8996c98SJesper Dangaard Brouer * seconds as CLOCK_REALTIME does. 5705c8996c98SJesper Dangaard Brouer * 5706c8996c98SJesper Dangaard Brouer * See: **clock_gettime**\ (**CLOCK_TAI**) 5707c8996c98SJesper Dangaard Brouer * Return 5708c8996c98SJesper Dangaard Brouer * Current *ktime*. 5709c8996c98SJesper Dangaard Brouer * 571020571567SDavid Vernet * long bpf_user_ringbuf_drain(struct bpf_map *map, void *callback_fn, void *ctx, u64 flags) 571120571567SDavid Vernet * Description 571220571567SDavid Vernet * Drain samples from the specified user ring buffer, and invoke 571320571567SDavid Vernet * the provided callback for each such sample: 571420571567SDavid Vernet * 571527060531SKumar Kartikeya Dwivedi * long (\*callback_fn)(const struct bpf_dynptr \*dynptr, void \*ctx); 571620571567SDavid Vernet * 571720571567SDavid Vernet * If **callback_fn** returns 0, the helper will continue to try 571820571567SDavid Vernet * and drain the next sample, up to a maximum of 571920571567SDavid Vernet * BPF_MAX_USER_RINGBUF_SAMPLES samples. If the return value is 1, 572020571567SDavid Vernet * the helper will skip the rest of the samples and return. Other 572120571567SDavid Vernet * return values are not used now, and will be rejected by the 572220571567SDavid Vernet * verifier. 572320571567SDavid Vernet * Return 572420571567SDavid Vernet * The number of drained samples if no error was encountered while 572520571567SDavid Vernet * draining samples, or 0 if no samples were present in the ring 572620571567SDavid Vernet * buffer. If a user-space producer was epoll-waiting on this map, 572720571567SDavid Vernet * and at least one sample was drained, they will receive an event 572820571567SDavid Vernet * notification notifying them of available space in the ring 572920571567SDavid Vernet * buffer. If the BPF_RB_NO_WAKEUP flag is passed to this 573020571567SDavid Vernet * function, no wakeup notification will be sent. If the 573120571567SDavid Vernet * BPF_RB_FORCE_WAKEUP flag is passed, a wakeup notification will 573220571567SDavid Vernet * be sent even if no sample was drained. 573320571567SDavid Vernet * 573420571567SDavid Vernet * On failure, the returned value is one of the following: 573520571567SDavid Vernet * 573620571567SDavid Vernet * **-EBUSY** if the ring buffer is contended, and another calling 573720571567SDavid Vernet * context was concurrently draining the ring buffer. 573820571567SDavid Vernet * 573920571567SDavid Vernet * **-EINVAL** if user-space is not properly tracking the ring 574020571567SDavid Vernet * buffer due to the producer position not being aligned to 8 574120571567SDavid Vernet * bytes, a sample not being aligned to 8 bytes, or the producer 574220571567SDavid Vernet * position not matching the advertised length of a sample. 574320571567SDavid Vernet * 574420571567SDavid Vernet * **-E2BIG** if user-space has tried to publish a sample which is 574520571567SDavid Vernet * larger than the size of the ring buffer, or which cannot fit 574620571567SDavid Vernet * within a struct bpf_dynptr. 5747c4bcfb38SYonghong Song * 5748c4bcfb38SYonghong Song * void *bpf_cgrp_storage_get(struct bpf_map *map, struct cgroup *cgroup, void *value, u64 flags) 5749c4bcfb38SYonghong Song * Description 5750c4bcfb38SYonghong Song * Get a bpf_local_storage from the *cgroup*. 5751c4bcfb38SYonghong Song * 5752c4bcfb38SYonghong Song * Logically, it could be thought of as getting the value from 5753c4bcfb38SYonghong Song * a *map* with *cgroup* as the **key**. From this 5754c4bcfb38SYonghong Song * perspective, the usage is not much different from 5755c4bcfb38SYonghong Song * **bpf_map_lookup_elem**\ (*map*, **&**\ *cgroup*) except this 5756c4bcfb38SYonghong Song * helper enforces the key must be a cgroup struct and the map must also 5757c4bcfb38SYonghong Song * be a **BPF_MAP_TYPE_CGRP_STORAGE**. 5758c4bcfb38SYonghong Song * 5759c4bcfb38SYonghong Song * In reality, the local-storage value is embedded directly inside of the 5760c4bcfb38SYonghong Song * *cgroup* object itself, rather than being located in the 5761c4bcfb38SYonghong Song * **BPF_MAP_TYPE_CGRP_STORAGE** map. When the local-storage value is 5762c4bcfb38SYonghong Song * queried for some *map* on a *cgroup* object, the kernel will perform an 5763c4bcfb38SYonghong Song * O(n) iteration over all of the live local-storage values for that 5764c4bcfb38SYonghong Song * *cgroup* object until the local-storage value for the *map* is found. 5765c4bcfb38SYonghong Song * 5766c4bcfb38SYonghong Song * An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be 5767c4bcfb38SYonghong Song * used such that a new bpf_local_storage will be 5768c4bcfb38SYonghong Song * created if one does not exist. *value* can be used 5769c4bcfb38SYonghong Song * together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify 5770c4bcfb38SYonghong Song * the initial value of a bpf_local_storage. If *value* is 5771c4bcfb38SYonghong Song * **NULL**, the new bpf_local_storage will be zero initialized. 5772c4bcfb38SYonghong Song * Return 5773c4bcfb38SYonghong Song * A bpf_local_storage pointer is returned on success. 5774c4bcfb38SYonghong Song * 5775c4bcfb38SYonghong Song * **NULL** if not found or there was an error in adding 5776c4bcfb38SYonghong Song * a new bpf_local_storage. 5777c4bcfb38SYonghong Song * 5778c4bcfb38SYonghong Song * long bpf_cgrp_storage_delete(struct bpf_map *map, struct cgroup *cgroup) 5779c4bcfb38SYonghong Song * Description 5780c4bcfb38SYonghong Song * Delete a bpf_local_storage from a *cgroup*. 5781c4bcfb38SYonghong Song * Return 5782c4bcfb38SYonghong Song * 0 on success. 5783c4bcfb38SYonghong Song * 5784c4bcfb38SYonghong Song * **-ENOENT** if the bpf_local_storage cannot be found. 57857a4b28c6SDaniel Borkmann */ 57868a76145aSAndrii Nakryiko #define ___BPF_FUNC_MAPPER(FN, ctx...) \ 57878a76145aSAndrii Nakryiko FN(unspec, 0, ##ctx) \ 57888a76145aSAndrii Nakryiko FN(map_lookup_elem, 1, ##ctx) \ 57898a76145aSAndrii Nakryiko FN(map_update_elem, 2, ##ctx) \ 57908a76145aSAndrii Nakryiko FN(map_delete_elem, 3, ##ctx) \ 57918a76145aSAndrii Nakryiko FN(probe_read, 4, ##ctx) \ 57928a76145aSAndrii Nakryiko FN(ktime_get_ns, 5, ##ctx) \ 57938a76145aSAndrii Nakryiko FN(trace_printk, 6, ##ctx) \ 57948a76145aSAndrii Nakryiko FN(get_prandom_u32, 7, ##ctx) \ 57958a76145aSAndrii Nakryiko FN(get_smp_processor_id, 8, ##ctx) \ 57968a76145aSAndrii Nakryiko FN(skb_store_bytes, 9, ##ctx) \ 57978a76145aSAndrii Nakryiko FN(l3_csum_replace, 10, ##ctx) \ 57988a76145aSAndrii Nakryiko FN(l4_csum_replace, 11, ##ctx) \ 57998a76145aSAndrii Nakryiko FN(tail_call, 12, ##ctx) \ 58008a76145aSAndrii Nakryiko FN(clone_redirect, 13, ##ctx) \ 58018a76145aSAndrii Nakryiko FN(get_current_pid_tgid, 14, ##ctx) \ 58028a76145aSAndrii Nakryiko FN(get_current_uid_gid, 15, ##ctx) \ 58038a76145aSAndrii Nakryiko FN(get_current_comm, 16, ##ctx) \ 58048a76145aSAndrii Nakryiko FN(get_cgroup_classid, 17, ##ctx) \ 58058a76145aSAndrii Nakryiko FN(skb_vlan_push, 18, ##ctx) \ 58068a76145aSAndrii Nakryiko FN(skb_vlan_pop, 19, ##ctx) \ 58078a76145aSAndrii Nakryiko FN(skb_get_tunnel_key, 20, ##ctx) \ 58088a76145aSAndrii Nakryiko FN(skb_set_tunnel_key, 21, ##ctx) \ 58098a76145aSAndrii Nakryiko FN(perf_event_read, 22, ##ctx) \ 58108a76145aSAndrii Nakryiko FN(redirect, 23, ##ctx) \ 58118a76145aSAndrii Nakryiko FN(get_route_realm, 24, ##ctx) \ 58128a76145aSAndrii Nakryiko FN(perf_event_output, 25, ##ctx) \ 58138a76145aSAndrii Nakryiko FN(skb_load_bytes, 26, ##ctx) \ 58148a76145aSAndrii Nakryiko FN(get_stackid, 27, ##ctx) \ 58158a76145aSAndrii Nakryiko FN(csum_diff, 28, ##ctx) \ 58168a76145aSAndrii Nakryiko FN(skb_get_tunnel_opt, 29, ##ctx) \ 58178a76145aSAndrii Nakryiko FN(skb_set_tunnel_opt, 30, ##ctx) \ 58188a76145aSAndrii Nakryiko FN(skb_change_proto, 31, ##ctx) \ 58198a76145aSAndrii Nakryiko FN(skb_change_type, 32, ##ctx) \ 58208a76145aSAndrii Nakryiko FN(skb_under_cgroup, 33, ##ctx) \ 58218a76145aSAndrii Nakryiko FN(get_hash_recalc, 34, ##ctx) \ 58228a76145aSAndrii Nakryiko FN(get_current_task, 35, ##ctx) \ 58238a76145aSAndrii Nakryiko FN(probe_write_user, 36, ##ctx) \ 58248a76145aSAndrii Nakryiko FN(current_task_under_cgroup, 37, ##ctx) \ 58258a76145aSAndrii Nakryiko FN(skb_change_tail, 38, ##ctx) \ 58268a76145aSAndrii Nakryiko FN(skb_pull_data, 39, ##ctx) \ 58278a76145aSAndrii Nakryiko FN(csum_update, 40, ##ctx) \ 58288a76145aSAndrii Nakryiko FN(set_hash_invalid, 41, ##ctx) \ 58298a76145aSAndrii Nakryiko FN(get_numa_node_id, 42, ##ctx) \ 58308a76145aSAndrii Nakryiko FN(skb_change_head, 43, ##ctx) \ 58318a76145aSAndrii Nakryiko FN(xdp_adjust_head, 44, ##ctx) \ 58328a76145aSAndrii Nakryiko FN(probe_read_str, 45, ##ctx) \ 58338a76145aSAndrii Nakryiko FN(get_socket_cookie, 46, ##ctx) \ 58348a76145aSAndrii Nakryiko FN(get_socket_uid, 47, ##ctx) \ 58358a76145aSAndrii Nakryiko FN(set_hash, 48, ##ctx) \ 58368a76145aSAndrii Nakryiko FN(setsockopt, 49, ##ctx) \ 58378a76145aSAndrii Nakryiko FN(skb_adjust_room, 50, ##ctx) \ 58388a76145aSAndrii Nakryiko FN(redirect_map, 51, ##ctx) \ 58398a76145aSAndrii Nakryiko FN(sk_redirect_map, 52, ##ctx) \ 58408a76145aSAndrii Nakryiko FN(sock_map_update, 53, ##ctx) \ 58418a76145aSAndrii Nakryiko FN(xdp_adjust_meta, 54, ##ctx) \ 58428a76145aSAndrii Nakryiko FN(perf_event_read_value, 55, ##ctx) \ 58438a76145aSAndrii Nakryiko FN(perf_prog_read_value, 56, ##ctx) \ 58448a76145aSAndrii Nakryiko FN(getsockopt, 57, ##ctx) \ 58458a76145aSAndrii Nakryiko FN(override_return, 58, ##ctx) \ 58468a76145aSAndrii Nakryiko FN(sock_ops_cb_flags_set, 59, ##ctx) \ 58478a76145aSAndrii Nakryiko FN(msg_redirect_map, 60, ##ctx) \ 58488a76145aSAndrii Nakryiko FN(msg_apply_bytes, 61, ##ctx) \ 58498a76145aSAndrii Nakryiko FN(msg_cork_bytes, 62, ##ctx) \ 58508a76145aSAndrii Nakryiko FN(msg_pull_data, 63, ##ctx) \ 58518a76145aSAndrii Nakryiko FN(bind, 64, ##ctx) \ 58528a76145aSAndrii Nakryiko FN(xdp_adjust_tail, 65, ##ctx) \ 58538a76145aSAndrii Nakryiko FN(skb_get_xfrm_state, 66, ##ctx) \ 58548a76145aSAndrii Nakryiko FN(get_stack, 67, ##ctx) \ 58558a76145aSAndrii Nakryiko FN(skb_load_bytes_relative, 68, ##ctx) \ 58568a76145aSAndrii Nakryiko FN(fib_lookup, 69, ##ctx) \ 58578a76145aSAndrii Nakryiko FN(sock_hash_update, 70, ##ctx) \ 58588a76145aSAndrii Nakryiko FN(msg_redirect_hash, 71, ##ctx) \ 58598a76145aSAndrii Nakryiko FN(sk_redirect_hash, 72, ##ctx) \ 58608a76145aSAndrii Nakryiko FN(lwt_push_encap, 73, ##ctx) \ 58618a76145aSAndrii Nakryiko FN(lwt_seg6_store_bytes, 74, ##ctx) \ 58628a76145aSAndrii Nakryiko FN(lwt_seg6_adjust_srh, 75, ##ctx) \ 58638a76145aSAndrii Nakryiko FN(lwt_seg6_action, 76, ##ctx) \ 58648a76145aSAndrii Nakryiko FN(rc_repeat, 77, ##ctx) \ 58658a76145aSAndrii Nakryiko FN(rc_keydown, 78, ##ctx) \ 58668a76145aSAndrii Nakryiko FN(skb_cgroup_id, 79, ##ctx) \ 58678a76145aSAndrii Nakryiko FN(get_current_cgroup_id, 80, ##ctx) \ 58688a76145aSAndrii Nakryiko FN(get_local_storage, 81, ##ctx) \ 58698a76145aSAndrii Nakryiko FN(sk_select_reuseport, 82, ##ctx) \ 58708a76145aSAndrii Nakryiko FN(skb_ancestor_cgroup_id, 83, ##ctx) \ 58718a76145aSAndrii Nakryiko FN(sk_lookup_tcp, 84, ##ctx) \ 58728a76145aSAndrii Nakryiko FN(sk_lookup_udp, 85, ##ctx) \ 58738a76145aSAndrii Nakryiko FN(sk_release, 86, ##ctx) \ 58748a76145aSAndrii Nakryiko FN(map_push_elem, 87, ##ctx) \ 58758a76145aSAndrii Nakryiko FN(map_pop_elem, 88, ##ctx) \ 58768a76145aSAndrii Nakryiko FN(map_peek_elem, 89, ##ctx) \ 58778a76145aSAndrii Nakryiko FN(msg_push_data, 90, ##ctx) \ 58788a76145aSAndrii Nakryiko FN(msg_pop_data, 91, ##ctx) \ 58798a76145aSAndrii Nakryiko FN(rc_pointer_rel, 92, ##ctx) \ 58808a76145aSAndrii Nakryiko FN(spin_lock, 93, ##ctx) \ 58818a76145aSAndrii Nakryiko FN(spin_unlock, 94, ##ctx) \ 58828a76145aSAndrii Nakryiko FN(sk_fullsock, 95, ##ctx) \ 58838a76145aSAndrii Nakryiko FN(tcp_sock, 96, ##ctx) \ 58848a76145aSAndrii Nakryiko FN(skb_ecn_set_ce, 97, ##ctx) \ 58858a76145aSAndrii Nakryiko FN(get_listener_sock, 98, ##ctx) \ 58868a76145aSAndrii Nakryiko FN(skc_lookup_tcp, 99, ##ctx) \ 58878a76145aSAndrii Nakryiko FN(tcp_check_syncookie, 100, ##ctx) \ 58888a76145aSAndrii Nakryiko FN(sysctl_get_name, 101, ##ctx) \ 58898a76145aSAndrii Nakryiko FN(sysctl_get_current_value, 102, ##ctx) \ 58908a76145aSAndrii Nakryiko FN(sysctl_get_new_value, 103, ##ctx) \ 58918a76145aSAndrii Nakryiko FN(sysctl_set_new_value, 104, ##ctx) \ 58928a76145aSAndrii Nakryiko FN(strtol, 105, ##ctx) \ 58938a76145aSAndrii Nakryiko FN(strtoul, 106, ##ctx) \ 58948a76145aSAndrii Nakryiko FN(sk_storage_get, 107, ##ctx) \ 58958a76145aSAndrii Nakryiko FN(sk_storage_delete, 108, ##ctx) \ 58968a76145aSAndrii Nakryiko FN(send_signal, 109, ##ctx) \ 58978a76145aSAndrii Nakryiko FN(tcp_gen_syncookie, 110, ##ctx) \ 58988a76145aSAndrii Nakryiko FN(skb_output, 111, ##ctx) \ 58998a76145aSAndrii Nakryiko FN(probe_read_user, 112, ##ctx) \ 59008a76145aSAndrii Nakryiko FN(probe_read_kernel, 113, ##ctx) \ 59018a76145aSAndrii Nakryiko FN(probe_read_user_str, 114, ##ctx) \ 59028a76145aSAndrii Nakryiko FN(probe_read_kernel_str, 115, ##ctx) \ 59038a76145aSAndrii Nakryiko FN(tcp_send_ack, 116, ##ctx) \ 59048a76145aSAndrii Nakryiko FN(send_signal_thread, 117, ##ctx) \ 59058a76145aSAndrii Nakryiko FN(jiffies64, 118, ##ctx) \ 59068a76145aSAndrii Nakryiko FN(read_branch_records, 119, ##ctx) \ 59078a76145aSAndrii Nakryiko FN(get_ns_current_pid_tgid, 120, ##ctx) \ 59088a76145aSAndrii Nakryiko FN(xdp_output, 121, ##ctx) \ 59098a76145aSAndrii Nakryiko FN(get_netns_cookie, 122, ##ctx) \ 59108a76145aSAndrii Nakryiko FN(get_current_ancestor_cgroup_id, 123, ##ctx) \ 59118a76145aSAndrii Nakryiko FN(sk_assign, 124, ##ctx) \ 59128a76145aSAndrii Nakryiko FN(ktime_get_boot_ns, 125, ##ctx) \ 59138a76145aSAndrii Nakryiko FN(seq_printf, 126, ##ctx) \ 59148a76145aSAndrii Nakryiko FN(seq_write, 127, ##ctx) \ 59158a76145aSAndrii Nakryiko FN(sk_cgroup_id, 128, ##ctx) \ 59168a76145aSAndrii Nakryiko FN(sk_ancestor_cgroup_id, 129, ##ctx) \ 59178a76145aSAndrii Nakryiko FN(ringbuf_output, 130, ##ctx) \ 59188a76145aSAndrii Nakryiko FN(ringbuf_reserve, 131, ##ctx) \ 59198a76145aSAndrii Nakryiko FN(ringbuf_submit, 132, ##ctx) \ 59208a76145aSAndrii Nakryiko FN(ringbuf_discard, 133, ##ctx) \ 59218a76145aSAndrii Nakryiko FN(ringbuf_query, 134, ##ctx) \ 59228a76145aSAndrii Nakryiko FN(csum_level, 135, ##ctx) \ 59238a76145aSAndrii Nakryiko FN(skc_to_tcp6_sock, 136, ##ctx) \ 59248a76145aSAndrii Nakryiko FN(skc_to_tcp_sock, 137, ##ctx) \ 59258a76145aSAndrii Nakryiko FN(skc_to_tcp_timewait_sock, 138, ##ctx) \ 59268a76145aSAndrii Nakryiko FN(skc_to_tcp_request_sock, 139, ##ctx) \ 59278a76145aSAndrii Nakryiko FN(skc_to_udp6_sock, 140, ##ctx) \ 59288a76145aSAndrii Nakryiko FN(get_task_stack, 141, ##ctx) \ 59298a76145aSAndrii Nakryiko FN(load_hdr_opt, 142, ##ctx) \ 59308a76145aSAndrii Nakryiko FN(store_hdr_opt, 143, ##ctx) \ 59318a76145aSAndrii Nakryiko FN(reserve_hdr_opt, 144, ##ctx) \ 59328a76145aSAndrii Nakryiko FN(inode_storage_get, 145, ##ctx) \ 59338a76145aSAndrii Nakryiko FN(inode_storage_delete, 146, ##ctx) \ 59348a76145aSAndrii Nakryiko FN(d_path, 147, ##ctx) \ 59358a76145aSAndrii Nakryiko FN(copy_from_user, 148, ##ctx) \ 59368a76145aSAndrii Nakryiko FN(snprintf_btf, 149, ##ctx) \ 59378a76145aSAndrii Nakryiko FN(seq_printf_btf, 150, ##ctx) \ 59388a76145aSAndrii Nakryiko FN(skb_cgroup_classid, 151, ##ctx) \ 59398a76145aSAndrii Nakryiko FN(redirect_neigh, 152, ##ctx) \ 59408a76145aSAndrii Nakryiko FN(per_cpu_ptr, 153, ##ctx) \ 59418a76145aSAndrii Nakryiko FN(this_cpu_ptr, 154, ##ctx) \ 59428a76145aSAndrii Nakryiko FN(redirect_peer, 155, ##ctx) \ 59438a76145aSAndrii Nakryiko FN(task_storage_get, 156, ##ctx) \ 59448a76145aSAndrii Nakryiko FN(task_storage_delete, 157, ##ctx) \ 59458a76145aSAndrii Nakryiko FN(get_current_task_btf, 158, ##ctx) \ 59468a76145aSAndrii Nakryiko FN(bprm_opts_set, 159, ##ctx) \ 59478a76145aSAndrii Nakryiko FN(ktime_get_coarse_ns, 160, ##ctx) \ 59488a76145aSAndrii Nakryiko FN(ima_inode_hash, 161, ##ctx) \ 59498a76145aSAndrii Nakryiko FN(sock_from_file, 162, ##ctx) \ 59508a76145aSAndrii Nakryiko FN(check_mtu, 163, ##ctx) \ 59518a76145aSAndrii Nakryiko FN(for_each_map_elem, 164, ##ctx) \ 59528a76145aSAndrii Nakryiko FN(snprintf, 165, ##ctx) \ 59538a76145aSAndrii Nakryiko FN(sys_bpf, 166, ##ctx) \ 59548a76145aSAndrii Nakryiko FN(btf_find_by_name_kind, 167, ##ctx) \ 59558a76145aSAndrii Nakryiko FN(sys_close, 168, ##ctx) \ 59568a76145aSAndrii Nakryiko FN(timer_init, 169, ##ctx) \ 59578a76145aSAndrii Nakryiko FN(timer_set_callback, 170, ##ctx) \ 59588a76145aSAndrii Nakryiko FN(timer_start, 171, ##ctx) \ 59598a76145aSAndrii Nakryiko FN(timer_cancel, 172, ##ctx) \ 59608a76145aSAndrii Nakryiko FN(get_func_ip, 173, ##ctx) \ 59618a76145aSAndrii Nakryiko FN(get_attach_cookie, 174, ##ctx) \ 59628a76145aSAndrii Nakryiko FN(task_pt_regs, 175, ##ctx) \ 59638a76145aSAndrii Nakryiko FN(get_branch_snapshot, 176, ##ctx) \ 59648a76145aSAndrii Nakryiko FN(trace_vprintk, 177, ##ctx) \ 59658a76145aSAndrii Nakryiko FN(skc_to_unix_sock, 178, ##ctx) \ 59668a76145aSAndrii Nakryiko FN(kallsyms_lookup_name, 179, ##ctx) \ 59678a76145aSAndrii Nakryiko FN(find_vma, 180, ##ctx) \ 59688a76145aSAndrii Nakryiko FN(loop, 181, ##ctx) \ 59698a76145aSAndrii Nakryiko FN(strncmp, 182, ##ctx) \ 59708a76145aSAndrii Nakryiko FN(get_func_arg, 183, ##ctx) \ 59718a76145aSAndrii Nakryiko FN(get_func_ret, 184, ##ctx) \ 59728a76145aSAndrii Nakryiko FN(get_func_arg_cnt, 185, ##ctx) \ 59738a76145aSAndrii Nakryiko FN(get_retval, 186, ##ctx) \ 59748a76145aSAndrii Nakryiko FN(set_retval, 187, ##ctx) \ 59758a76145aSAndrii Nakryiko FN(xdp_get_buff_len, 188, ##ctx) \ 59768a76145aSAndrii Nakryiko FN(xdp_load_bytes, 189, ##ctx) \ 59778a76145aSAndrii Nakryiko FN(xdp_store_bytes, 190, ##ctx) \ 59788a76145aSAndrii Nakryiko FN(copy_from_user_task, 191, ##ctx) \ 59798a76145aSAndrii Nakryiko FN(skb_set_tstamp, 192, ##ctx) \ 59808a76145aSAndrii Nakryiko FN(ima_file_hash, 193, ##ctx) \ 59818a76145aSAndrii Nakryiko FN(kptr_xchg, 194, ##ctx) \ 59828a76145aSAndrii Nakryiko FN(map_lookup_percpu_elem, 195, ##ctx) \ 59838a76145aSAndrii Nakryiko FN(skc_to_mptcp_sock, 196, ##ctx) \ 59848a76145aSAndrii Nakryiko FN(dynptr_from_mem, 197, ##ctx) \ 59858a76145aSAndrii Nakryiko FN(ringbuf_reserve_dynptr, 198, ##ctx) \ 59868a76145aSAndrii Nakryiko FN(ringbuf_submit_dynptr, 199, ##ctx) \ 59878a76145aSAndrii Nakryiko FN(ringbuf_discard_dynptr, 200, ##ctx) \ 59888a76145aSAndrii Nakryiko FN(dynptr_read, 201, ##ctx) \ 59898a76145aSAndrii Nakryiko FN(dynptr_write, 202, ##ctx) \ 59908a76145aSAndrii Nakryiko FN(dynptr_data, 203, ##ctx) \ 59918a76145aSAndrii Nakryiko FN(tcp_raw_gen_syncookie_ipv4, 204, ##ctx) \ 59928a76145aSAndrii Nakryiko FN(tcp_raw_gen_syncookie_ipv6, 205, ##ctx) \ 59938a76145aSAndrii Nakryiko FN(tcp_raw_check_syncookie_ipv4, 206, ##ctx) \ 59948a76145aSAndrii Nakryiko FN(tcp_raw_check_syncookie_ipv6, 207, ##ctx) \ 59958a76145aSAndrii Nakryiko FN(ktime_get_tai_ns, 208, ##ctx) \ 59968a76145aSAndrii Nakryiko FN(user_ringbuf_drain, 209, ##ctx) \ 5997c4bcfb38SYonghong Song FN(cgrp_storage_get, 210, ##ctx) \ 5998c4bcfb38SYonghong Song FN(cgrp_storage_delete, 211, ##ctx) \ 5999fa28dcb8SSong Liu /* */ 60007a4b28c6SDaniel Borkmann 60018a76145aSAndrii Nakryiko /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't 60028a76145aSAndrii Nakryiko * know or care about integer value that is now passed as second argument 60038a76145aSAndrii Nakryiko */ 60048a76145aSAndrii Nakryiko #define __BPF_FUNC_MAPPER_APPLY(name, value, FN) FN(name), 60058a76145aSAndrii Nakryiko #define __BPF_FUNC_MAPPER(FN) ___BPF_FUNC_MAPPER(__BPF_FUNC_MAPPER_APPLY, FN) 60068a76145aSAndrii Nakryiko 6007ebb676daSThomas Graf /* integer value in 'imm' field of BPF_CALL instruction selects which helper 6008ebb676daSThomas Graf * function eBPF program intends to call 60092d0e30c3SDaniel Borkmann */ 60108a76145aSAndrii Nakryiko #define __BPF_ENUM_FN(x, y) BPF_FUNC_ ## x = y, 6011ebb676daSThomas Graf enum bpf_func_id { 60128a76145aSAndrii Nakryiko ___BPF_FUNC_MAPPER(__BPF_ENUM_FN) 601309756af4SAlexei Starovoitov __BPF_FUNC_MAX_ID, 601409756af4SAlexei Starovoitov }; 6015ebb676daSThomas Graf #undef __BPF_ENUM_FN 601609756af4SAlexei Starovoitov 6017781c53bcSDaniel Borkmann /* All flags used by eBPF helper functions, placed here. */ 6018781c53bcSDaniel Borkmann 6019781c53bcSDaniel Borkmann /* BPF_FUNC_skb_store_bytes flags. */ 60201aae4bddSAndrii Nakryiko enum { 60211aae4bddSAndrii Nakryiko BPF_F_RECOMPUTE_CSUM = (1ULL << 0), 60221aae4bddSAndrii Nakryiko BPF_F_INVALIDATE_HASH = (1ULL << 1), 60231aae4bddSAndrii Nakryiko }; 6024781c53bcSDaniel Borkmann 6025781c53bcSDaniel Borkmann /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags. 6026781c53bcSDaniel Borkmann * First 4 bits are for passing the header field size. 6027781c53bcSDaniel Borkmann */ 60281aae4bddSAndrii Nakryiko enum { 60291aae4bddSAndrii Nakryiko BPF_F_HDR_FIELD_MASK = 0xfULL, 60301aae4bddSAndrii Nakryiko }; 6031781c53bcSDaniel Borkmann 6032781c53bcSDaniel Borkmann /* BPF_FUNC_l4_csum_replace flags. */ 60331aae4bddSAndrii Nakryiko enum { 60341aae4bddSAndrii Nakryiko BPF_F_PSEUDO_HDR = (1ULL << 4), 60351aae4bddSAndrii Nakryiko BPF_F_MARK_MANGLED_0 = (1ULL << 5), 60361aae4bddSAndrii Nakryiko BPF_F_MARK_ENFORCE = (1ULL << 6), 60371aae4bddSAndrii Nakryiko }; 6038781c53bcSDaniel Borkmann 6039781c53bcSDaniel Borkmann /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */ 60401aae4bddSAndrii Nakryiko enum { 60411aae4bddSAndrii Nakryiko BPF_F_INGRESS = (1ULL << 0), 60421aae4bddSAndrii Nakryiko }; 6043781c53bcSDaniel Borkmann 6044c6c33454SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */ 60451aae4bddSAndrii Nakryiko enum { 60461aae4bddSAndrii Nakryiko BPF_F_TUNINFO_IPV6 = (1ULL << 0), 60471aae4bddSAndrii Nakryiko }; 6048c6c33454SDaniel Borkmann 6049c195651eSYonghong Song /* flags for both BPF_FUNC_get_stackid and BPF_FUNC_get_stack. */ 60501aae4bddSAndrii Nakryiko enum { 60511aae4bddSAndrii Nakryiko BPF_F_SKIP_FIELD_MASK = 0xffULL, 60521aae4bddSAndrii Nakryiko BPF_F_USER_STACK = (1ULL << 8), 6053c195651eSYonghong Song /* flags used by BPF_FUNC_get_stackid only. */ 60541aae4bddSAndrii Nakryiko BPF_F_FAST_STACK_CMP = (1ULL << 9), 60551aae4bddSAndrii Nakryiko BPF_F_REUSE_STACKID = (1ULL << 10), 6056c195651eSYonghong Song /* flags used by BPF_FUNC_get_stack only. */ 60571aae4bddSAndrii Nakryiko BPF_F_USER_BUILD_ID = (1ULL << 11), 60581aae4bddSAndrii Nakryiko }; 6059d5a3b1f6SAlexei Starovoitov 60602da897e5SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key flags. */ 60611aae4bddSAndrii Nakryiko enum { 60621aae4bddSAndrii Nakryiko BPF_F_ZERO_CSUM_TX = (1ULL << 1), 60631aae4bddSAndrii Nakryiko BPF_F_DONT_FRAGMENT = (1ULL << 2), 60641aae4bddSAndrii Nakryiko BPF_F_SEQ_NUMBER = (1ULL << 3), 6065e26aa600SChristian Ehrig BPF_F_NO_TUNNEL_KEY = (1ULL << 4), 60661aae4bddSAndrii Nakryiko }; 60672da897e5SDaniel Borkmann 606844c51472SShmulik Ladkani /* BPF_FUNC_skb_get_tunnel_key flags. */ 606944c51472SShmulik Ladkani enum { 607044c51472SShmulik Ladkani BPF_F_TUNINFO_FLAGS = (1ULL << 4), 607144c51472SShmulik Ladkani }; 607244c51472SShmulik Ladkani 6073908432caSYonghong Song /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and 6074908432caSYonghong Song * BPF_FUNC_perf_event_read_value flags. 6075908432caSYonghong Song */ 60761aae4bddSAndrii Nakryiko enum { 60771aae4bddSAndrii Nakryiko BPF_F_INDEX_MASK = 0xffffffffULL, 60781aae4bddSAndrii Nakryiko BPF_F_CURRENT_CPU = BPF_F_INDEX_MASK, 6079555c8a86SDaniel Borkmann /* BPF_FUNC_perf_event_output for sk_buff input context. */ 60801aae4bddSAndrii Nakryiko BPF_F_CTXLEN_MASK = (0xfffffULL << 32), 60811aae4bddSAndrii Nakryiko }; 60821e33759cSDaniel Borkmann 6083f71c6143SJoe Stringer /* Current network namespace */ 60841aae4bddSAndrii Nakryiko enum { 60851aae4bddSAndrii Nakryiko BPF_F_CURRENT_NETNS = (-1L), 60861aae4bddSAndrii Nakryiko }; 6087f71c6143SJoe Stringer 60887cdec54fSDaniel Borkmann /* BPF_FUNC_csum_level level values. */ 60897cdec54fSDaniel Borkmann enum { 60907cdec54fSDaniel Borkmann BPF_CSUM_LEVEL_QUERY, 60917cdec54fSDaniel Borkmann BPF_CSUM_LEVEL_INC, 60927cdec54fSDaniel Borkmann BPF_CSUM_LEVEL_DEC, 60937cdec54fSDaniel Borkmann BPF_CSUM_LEVEL_RESET, 60947cdec54fSDaniel Borkmann }; 60957cdec54fSDaniel Borkmann 60962278f6ccSWillem de Bruijn /* BPF_FUNC_skb_adjust_room flags. */ 60971aae4bddSAndrii Nakryiko enum { 60981aae4bddSAndrii Nakryiko BPF_F_ADJ_ROOM_FIXED_GSO = (1ULL << 0), 60991aae4bddSAndrii Nakryiko BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = (1ULL << 1), 61001aae4bddSAndrii Nakryiko BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = (1ULL << 2), 61011aae4bddSAndrii Nakryiko BPF_F_ADJ_ROOM_ENCAP_L4_GRE = (1ULL << 3), 61021aae4bddSAndrii Nakryiko BPF_F_ADJ_ROOM_ENCAP_L4_UDP = (1ULL << 4), 6103836e66c2SDaniel Borkmann BPF_F_ADJ_ROOM_NO_CSUM_RESET = (1ULL << 5), 6104d01b59c9SXuesen Huang BPF_F_ADJ_ROOM_ENCAP_L2_ETH = (1ULL << 6), 6105d219df60SZiyang Xuan BPF_F_ADJ_ROOM_DECAP_L3_IPV4 = (1ULL << 7), 6106d219df60SZiyang Xuan BPF_F_ADJ_ROOM_DECAP_L3_IPV6 = (1ULL << 8), 61071aae4bddSAndrii Nakryiko }; 61082278f6ccSWillem de Bruijn 61091aae4bddSAndrii Nakryiko enum { 61101aae4bddSAndrii Nakryiko BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff, 61111aae4bddSAndrii Nakryiko BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 56, 61121aae4bddSAndrii Nakryiko }; 611358dfc900SAlan Maguire 611458dfc900SAlan Maguire #define BPF_F_ADJ_ROOM_ENCAP_L2(len) (((__u64)len & \ 611558dfc900SAlan Maguire BPF_ADJ_ROOM_ENCAP_L2_MASK) \ 611658dfc900SAlan Maguire << BPF_ADJ_ROOM_ENCAP_L2_SHIFT) 6117868d5235SWillem de Bruijn 6118808649fbSAndrey Ignatov /* BPF_FUNC_sysctl_get_name flags. */ 61191aae4bddSAndrii Nakryiko enum { 61201aae4bddSAndrii Nakryiko BPF_F_SYSCTL_BASE_NAME = (1ULL << 0), 61211aae4bddSAndrii Nakryiko }; 6122808649fbSAndrey Ignatov 6123f836a56eSKP Singh /* BPF_FUNC_<kernel_obj>_storage_get flags */ 61241aae4bddSAndrii Nakryiko enum { 6125f836a56eSKP Singh BPF_LOCAL_STORAGE_GET_F_CREATE = (1ULL << 0), 6126f836a56eSKP Singh /* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility 6127f836a56eSKP Singh * and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead. 6128f836a56eSKP Singh */ 6129f836a56eSKP Singh BPF_SK_STORAGE_GET_F_CREATE = BPF_LOCAL_STORAGE_GET_F_CREATE, 61301aae4bddSAndrii Nakryiko }; 61316ac99e8fSMartin KaFai Lau 6132fff7b643SDaniel Xu /* BPF_FUNC_read_branch_records flags. */ 61331aae4bddSAndrii Nakryiko enum { 61341aae4bddSAndrii Nakryiko BPF_F_GET_BRANCH_RECORDS_SIZE = (1ULL << 0), 61351aae4bddSAndrii Nakryiko }; 6136fff7b643SDaniel Xu 6137457f4436SAndrii Nakryiko /* BPF_FUNC_bpf_ringbuf_commit, BPF_FUNC_bpf_ringbuf_discard, and 6138457f4436SAndrii Nakryiko * BPF_FUNC_bpf_ringbuf_output flags. 6139457f4436SAndrii Nakryiko */ 6140457f4436SAndrii Nakryiko enum { 6141457f4436SAndrii Nakryiko BPF_RB_NO_WAKEUP = (1ULL << 0), 6142457f4436SAndrii Nakryiko BPF_RB_FORCE_WAKEUP = (1ULL << 1), 6143457f4436SAndrii Nakryiko }; 6144457f4436SAndrii Nakryiko 6145457f4436SAndrii Nakryiko /* BPF_FUNC_bpf_ringbuf_query flags */ 6146457f4436SAndrii Nakryiko enum { 6147457f4436SAndrii Nakryiko BPF_RB_AVAIL_DATA = 0, 6148457f4436SAndrii Nakryiko BPF_RB_RING_SIZE = 1, 6149457f4436SAndrii Nakryiko BPF_RB_CONS_POS = 2, 6150457f4436SAndrii Nakryiko BPF_RB_PROD_POS = 3, 6151457f4436SAndrii Nakryiko }; 6152457f4436SAndrii Nakryiko 6153457f4436SAndrii Nakryiko /* BPF ring buffer constants */ 6154457f4436SAndrii Nakryiko enum { 6155457f4436SAndrii Nakryiko BPF_RINGBUF_BUSY_BIT = (1U << 31), 6156457f4436SAndrii Nakryiko BPF_RINGBUF_DISCARD_BIT = (1U << 30), 6157457f4436SAndrii Nakryiko BPF_RINGBUF_HDR_SZ = 8, 6158457f4436SAndrii Nakryiko }; 6159457f4436SAndrii Nakryiko 6160e9ddbb77SJakub Sitnicki /* BPF_FUNC_sk_assign flags in bpf_sk_lookup context. */ 6161e9ddbb77SJakub Sitnicki enum { 6162e9ddbb77SJakub Sitnicki BPF_SK_LOOKUP_F_REPLACE = (1ULL << 0), 6163e9ddbb77SJakub Sitnicki BPF_SK_LOOKUP_F_NO_REUSEPORT = (1ULL << 1), 6164e9ddbb77SJakub Sitnicki }; 6165e9ddbb77SJakub Sitnicki 61662be7e212SDaniel Borkmann /* Mode for BPF_FUNC_skb_adjust_room helper. */ 61672be7e212SDaniel Borkmann enum bpf_adj_room_mode { 61682be7e212SDaniel Borkmann BPF_ADJ_ROOM_NET, 616914aa3192SWillem de Bruijn BPF_ADJ_ROOM_MAC, 61702be7e212SDaniel Borkmann }; 61712be7e212SDaniel Borkmann 61724e1ec56cSDaniel Borkmann /* Mode for BPF_FUNC_skb_load_bytes_relative helper. */ 61734e1ec56cSDaniel Borkmann enum bpf_hdr_start_off { 61744e1ec56cSDaniel Borkmann BPF_HDR_START_MAC, 61754e1ec56cSDaniel Borkmann BPF_HDR_START_NET, 61764e1ec56cSDaniel Borkmann }; 61774e1ec56cSDaniel Borkmann 6178fe94cc29SMathieu Xhonneux /* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */ 6179fe94cc29SMathieu Xhonneux enum bpf_lwt_encap_mode { 6180fe94cc29SMathieu Xhonneux BPF_LWT_ENCAP_SEG6, 61813e0bd37cSPeter Oskolkov BPF_LWT_ENCAP_SEG6_INLINE, 61823e0bd37cSPeter Oskolkov BPF_LWT_ENCAP_IP, 6183fe94cc29SMathieu Xhonneux }; 6184fe94cc29SMathieu Xhonneux 61853f6719c7SKP Singh /* Flags for bpf_bprm_opts_set helper */ 61863f6719c7SKP Singh enum { 61873f6719c7SKP Singh BPF_F_BPRM_SECUREEXEC = (1ULL << 0), 61883f6719c7SKP Singh }; 61893f6719c7SKP Singh 6190e624d4edSHangbin Liu /* Flags for bpf_redirect_map helper */ 6191e624d4edSHangbin Liu enum { 6192e624d4edSHangbin Liu BPF_F_BROADCAST = (1ULL << 3), 6193e624d4edSHangbin Liu BPF_F_EXCLUDE_INGRESS = (1ULL << 4), 6194e624d4edSHangbin Liu }; 6195e624d4edSHangbin Liu 6196b7df9adaSDaniel Borkmann #define __bpf_md_ptr(type, name) \ 6197b7df9adaSDaniel Borkmann union { \ 6198b7df9adaSDaniel Borkmann type name; \ 6199b7df9adaSDaniel Borkmann __u64 :64; \ 6200b7df9adaSDaniel Borkmann } __attribute__((aligned(8))) 6201b7df9adaSDaniel Borkmann 62028d21ec0eSMartin KaFai Lau enum { 62039bb984f2SMartin KaFai Lau BPF_SKB_TSTAMP_UNSPEC, 62049bb984f2SMartin KaFai Lau BPF_SKB_TSTAMP_DELIVERY_MONO, /* tstamp has mono delivery time */ 62059bb984f2SMartin KaFai Lau /* For any BPF_SKB_TSTAMP_* that the bpf prog cannot handle, 62069bb984f2SMartin KaFai Lau * the bpf prog should handle it like BPF_SKB_TSTAMP_UNSPEC 62079bb984f2SMartin KaFai Lau * and try to deduce it by ingress, egress or skb->sk->sk_clockid. 62089bb984f2SMartin KaFai Lau */ 62098d21ec0eSMartin KaFai Lau }; 62108d21ec0eSMartin KaFai Lau 62119bac3d6dSAlexei Starovoitov /* user accessible mirror of in-kernel sk_buff. 62129bac3d6dSAlexei Starovoitov * new fields can only be added to the end of this structure 62139bac3d6dSAlexei Starovoitov */ 62149bac3d6dSAlexei Starovoitov struct __sk_buff { 62159bac3d6dSAlexei Starovoitov __u32 len; 62169bac3d6dSAlexei Starovoitov __u32 pkt_type; 62179bac3d6dSAlexei Starovoitov __u32 mark; 62189bac3d6dSAlexei Starovoitov __u32 queue_mapping; 6219c2497395SAlexei Starovoitov __u32 protocol; 6220c2497395SAlexei Starovoitov __u32 vlan_present; 6221c2497395SAlexei Starovoitov __u32 vlan_tci; 622227cd5452SMichal Sekletar __u32 vlan_proto; 6223bcad5718SDaniel Borkmann __u32 priority; 622437e82c2fSAlexei Starovoitov __u32 ingress_ifindex; 622537e82c2fSAlexei Starovoitov __u32 ifindex; 6226d691f9e8SAlexei Starovoitov __u32 tc_index; 6227d691f9e8SAlexei Starovoitov __u32 cb[5]; 6228ba7591d8SDaniel Borkmann __u32 hash; 6229045efa82SDaniel Borkmann __u32 tc_classid; 6230969bf05eSAlexei Starovoitov __u32 data; 6231969bf05eSAlexei Starovoitov __u32 data_end; 6232b1d9fc41SDaniel Borkmann __u32 napi_id; 62338a31db56SJohn Fastabend 6234de8f3a83SDaniel Borkmann /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */ 62358a31db56SJohn Fastabend __u32 family; 62368a31db56SJohn Fastabend __u32 remote_ip4; /* Stored in network byte order */ 62378a31db56SJohn Fastabend __u32 local_ip4; /* Stored in network byte order */ 62388a31db56SJohn Fastabend __u32 remote_ip6[4]; /* Stored in network byte order */ 62398a31db56SJohn Fastabend __u32 local_ip6[4]; /* Stored in network byte order */ 62408a31db56SJohn Fastabend __u32 remote_port; /* Stored in network byte order */ 62418a31db56SJohn Fastabend __u32 local_port; /* stored in host byte order */ 6242de8f3a83SDaniel Borkmann /* ... here. */ 6243de8f3a83SDaniel Borkmann 6244de8f3a83SDaniel Borkmann __u32 data_meta; 6245b7df9adaSDaniel Borkmann __bpf_md_ptr(struct bpf_flow_keys *, flow_keys); 6246f11216b2SVlad Dumitrescu __u64 tstamp; 6247e3da08d0SPetar Penkov __u32 wire_len; 6248d9ff286aSEric Dumazet __u32 gso_segs; 624946f8bc92SMartin KaFai Lau __bpf_md_ptr(struct bpf_sock *, sk); 6250cf62089bSWillem de Bruijn __u32 gso_size; 62519bb984f2SMartin KaFai Lau __u8 tstamp_type; 62528d21ec0eSMartin KaFai Lau __u32 :24; /* Padding, future use. */ 6253f64c4aceSVadim Fedorenko __u64 hwtstamp; 62549bac3d6dSAlexei Starovoitov }; 62559bac3d6dSAlexei Starovoitov 6256d3aa45ceSAlexei Starovoitov struct bpf_tunnel_key { 6257d3aa45ceSAlexei Starovoitov __u32 tunnel_id; 6258c6c33454SDaniel Borkmann union { 6259d3aa45ceSAlexei Starovoitov __u32 remote_ipv4; 6260c6c33454SDaniel Borkmann __u32 remote_ipv6[4]; 6261c6c33454SDaniel Borkmann }; 6262c6c33454SDaniel Borkmann __u8 tunnel_tos; 6263c6c33454SDaniel Borkmann __u8 tunnel_ttl; 626444c51472SShmulik Ladkani union { 626544c51472SShmulik Ladkani __u16 tunnel_ext; /* compat */ 626644c51472SShmulik Ladkani __be16 tunnel_flags; 626744c51472SShmulik Ladkani }; 62684018ab18SDaniel Borkmann __u32 tunnel_label; 626926101f5aSKaixi Fan union { 627026101f5aSKaixi Fan __u32 local_ipv4; 627126101f5aSKaixi Fan __u32 local_ipv6[4]; 627226101f5aSKaixi Fan }; 6273d3aa45ceSAlexei Starovoitov }; 6274d3aa45ceSAlexei Starovoitov 627512bed760SEyal Birger /* user accessible mirror of in-kernel xfrm_state. 627612bed760SEyal Birger * new fields can only be added to the end of this structure 627712bed760SEyal Birger */ 627812bed760SEyal Birger struct bpf_xfrm_state { 627912bed760SEyal Birger __u32 reqid; 628012bed760SEyal Birger __u32 spi; /* Stored in network byte order */ 628112bed760SEyal Birger __u16 family; 62821fbc2e0cSDaniel Borkmann __u16 ext; /* Padding, future use. */ 628312bed760SEyal Birger union { 628412bed760SEyal Birger __u32 remote_ipv4; /* Stored in network byte order */ 628512bed760SEyal Birger __u32 remote_ipv6[4]; /* Stored in network byte order */ 628612bed760SEyal Birger }; 628712bed760SEyal Birger }; 628812bed760SEyal Birger 62893a0af8fdSThomas Graf /* Generic BPF return codes which all BPF program types may support. 62903a0af8fdSThomas Graf * The values are binary compatible with their TC_ACT_* counter-part to 62913a0af8fdSThomas Graf * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT 62923a0af8fdSThomas Graf * programs. 62933a0af8fdSThomas Graf * 62943a0af8fdSThomas Graf * XDP is handled seprately, see XDP_*. 62953a0af8fdSThomas Graf */ 62963a0af8fdSThomas Graf enum bpf_ret_code { 62973a0af8fdSThomas Graf BPF_OK = 0, 62983a0af8fdSThomas Graf /* 1 reserved */ 62993a0af8fdSThomas Graf BPF_DROP = 2, 63003a0af8fdSThomas Graf /* 3-6 reserved */ 63013a0af8fdSThomas Graf BPF_REDIRECT = 7, 63023e0bd37cSPeter Oskolkov /* >127 are reserved for prog type specific return codes. 63033e0bd37cSPeter Oskolkov * 63043e0bd37cSPeter Oskolkov * BPF_LWT_REROUTE: used by BPF_PROG_TYPE_LWT_IN and 63053e0bd37cSPeter Oskolkov * BPF_PROG_TYPE_LWT_XMIT to indicate that skb had been 63063e0bd37cSPeter Oskolkov * changed and should be routed based on its new L3 header. 63073e0bd37cSPeter Oskolkov * (This is an L3 redirect, as opposed to L2 redirect 63083e0bd37cSPeter Oskolkov * represented by BPF_REDIRECT above). 63093e0bd37cSPeter Oskolkov */ 63103e0bd37cSPeter Oskolkov BPF_LWT_REROUTE = 128, 631191350fe1SShmulik Ladkani /* BPF_FLOW_DISSECTOR_CONTINUE: used by BPF_PROG_TYPE_FLOW_DISSECTOR 631291350fe1SShmulik Ladkani * to indicate that no custom dissection was performed, and 631391350fe1SShmulik Ladkani * fallback to standard dissector is requested. 631491350fe1SShmulik Ladkani */ 631591350fe1SShmulik Ladkani BPF_FLOW_DISSECTOR_CONTINUE = 129, 63163a0af8fdSThomas Graf }; 63173a0af8fdSThomas Graf 631861023658SDavid Ahern struct bpf_sock { 631961023658SDavid Ahern __u32 bound_dev_if; 6320aa4c1037SDavid Ahern __u32 family; 6321aa4c1037SDavid Ahern __u32 type; 6322aa4c1037SDavid Ahern __u32 protocol; 6323482dca93SDavid Ahern __u32 mark; 6324482dca93SDavid Ahern __u32 priority; 6325aa65d696SMartin KaFai Lau /* IP address also allows 1 and 2 bytes access */ 6326aa65d696SMartin KaFai Lau __u32 src_ip4; 6327aa65d696SMartin KaFai Lau __u32 src_ip6[4]; 6328aa65d696SMartin KaFai Lau __u32 src_port; /* host byte order */ 63294421a582SJakub Sitnicki __be16 dst_port; /* network byte order */ 63304421a582SJakub Sitnicki __u16 :16; /* zero padding */ 6331aa65d696SMartin KaFai Lau __u32 dst_ip4; 6332aa65d696SMartin KaFai Lau __u32 dst_ip6[4]; 6333aa65d696SMartin KaFai Lau __u32 state; 6334c3c16f2eSAmritha Nambiar __s32 rx_queue_mapping; 633561023658SDavid Ahern }; 633661023658SDavid Ahern 6337655a51e5SMartin KaFai Lau struct bpf_tcp_sock { 6338655a51e5SMartin KaFai Lau __u32 snd_cwnd; /* Sending congestion window */ 6339655a51e5SMartin KaFai Lau __u32 srtt_us; /* smoothed round trip time << 3 in usecs */ 6340655a51e5SMartin KaFai Lau __u32 rtt_min; 6341655a51e5SMartin KaFai Lau __u32 snd_ssthresh; /* Slow start size threshold */ 6342655a51e5SMartin KaFai Lau __u32 rcv_nxt; /* What we want to receive next */ 6343655a51e5SMartin KaFai Lau __u32 snd_nxt; /* Next sequence we send */ 6344655a51e5SMartin KaFai Lau __u32 snd_una; /* First byte we want an ack for */ 6345655a51e5SMartin KaFai Lau __u32 mss_cache; /* Cached effective mss, not including SACKS */ 6346655a51e5SMartin KaFai Lau __u32 ecn_flags; /* ECN status bits. */ 6347655a51e5SMartin KaFai Lau __u32 rate_delivered; /* saved rate sample: packets delivered */ 6348655a51e5SMartin KaFai Lau __u32 rate_interval_us; /* saved rate sample: time elapsed */ 6349655a51e5SMartin KaFai Lau __u32 packets_out; /* Packets which are "in flight" */ 6350655a51e5SMartin KaFai Lau __u32 retrans_out; /* Retransmitted packets out */ 6351655a51e5SMartin KaFai Lau __u32 total_retrans; /* Total retransmits for entire connection */ 6352655a51e5SMartin KaFai Lau __u32 segs_in; /* RFC4898 tcpEStatsPerfSegsIn 6353655a51e5SMartin KaFai Lau * total number of segments in. 6354655a51e5SMartin KaFai Lau */ 6355655a51e5SMartin KaFai Lau __u32 data_segs_in; /* RFC4898 tcpEStatsPerfDataSegsIn 6356655a51e5SMartin KaFai Lau * total number of data segments in. 6357655a51e5SMartin KaFai Lau */ 6358655a51e5SMartin KaFai Lau __u32 segs_out; /* RFC4898 tcpEStatsPerfSegsOut 6359655a51e5SMartin KaFai Lau * The total number of segments sent. 6360655a51e5SMartin KaFai Lau */ 6361655a51e5SMartin KaFai Lau __u32 data_segs_out; /* RFC4898 tcpEStatsPerfDataSegsOut 6362655a51e5SMartin KaFai Lau * total number of data segments sent. 6363655a51e5SMartin KaFai Lau */ 6364655a51e5SMartin KaFai Lau __u32 lost_out; /* Lost packets */ 6365655a51e5SMartin KaFai Lau __u32 sacked_out; /* SACK'd packets */ 6366655a51e5SMartin KaFai Lau __u64 bytes_received; /* RFC4898 tcpEStatsAppHCThruOctetsReceived 6367655a51e5SMartin KaFai Lau * sum(delta(rcv_nxt)), or how many bytes 6368655a51e5SMartin KaFai Lau * were acked. 6369655a51e5SMartin KaFai Lau */ 6370655a51e5SMartin KaFai Lau __u64 bytes_acked; /* RFC4898 tcpEStatsAppHCThruOctetsAcked 6371655a51e5SMartin KaFai Lau * sum(delta(snd_una)), or how many bytes 6372655a51e5SMartin KaFai Lau * were acked. 6373655a51e5SMartin KaFai Lau */ 63740357746dSStanislav Fomichev __u32 dsack_dups; /* RFC4898 tcpEStatsStackDSACKDups 63750357746dSStanislav Fomichev * total number of DSACK blocks received 63760357746dSStanislav Fomichev */ 63770357746dSStanislav Fomichev __u32 delivered; /* Total data packets delivered incl. rexmits */ 63780357746dSStanislav Fomichev __u32 delivered_ce; /* Like the above but only ECE marked packets */ 6379c2cb5e82SStanislav Fomichev __u32 icsk_retransmits; /* Number of unrecovered [RTO] timeouts */ 6380655a51e5SMartin KaFai Lau }; 6381655a51e5SMartin KaFai Lau 63826acc9b43SJoe Stringer struct bpf_sock_tuple { 63836acc9b43SJoe Stringer union { 63846acc9b43SJoe Stringer struct { 63856acc9b43SJoe Stringer __be32 saddr; 63866acc9b43SJoe Stringer __be32 daddr; 63876acc9b43SJoe Stringer __be16 sport; 63886acc9b43SJoe Stringer __be16 dport; 63896acc9b43SJoe Stringer } ipv4; 63906acc9b43SJoe Stringer struct { 63916acc9b43SJoe Stringer __be32 saddr[4]; 63926acc9b43SJoe Stringer __be32 daddr[4]; 63936acc9b43SJoe Stringer __be16 sport; 63946acc9b43SJoe Stringer __be16 dport; 63956acc9b43SJoe Stringer } ipv6; 63966acc9b43SJoe Stringer }; 63976acc9b43SJoe Stringer }; 63986acc9b43SJoe Stringer 6399e420bed0SDaniel Borkmann /* (Simplified) user return codes for tcx prog type. 6400e420bed0SDaniel Borkmann * A valid tcx program must return one of these defined values. All other 6401e420bed0SDaniel Borkmann * return codes are reserved for future use. Must remain compatible with 6402e420bed0SDaniel Borkmann * their TC_ACT_* counter-parts. For compatibility in behavior, unknown 6403e420bed0SDaniel Borkmann * return codes are mapped to TCX_NEXT. 6404e420bed0SDaniel Borkmann */ 6405e420bed0SDaniel Borkmann enum tcx_action_base { 6406e420bed0SDaniel Borkmann TCX_NEXT = -1, 6407e420bed0SDaniel Borkmann TCX_PASS = 0, 6408e420bed0SDaniel Borkmann TCX_DROP = 2, 6409e420bed0SDaniel Borkmann TCX_REDIRECT = 7, 6410e420bed0SDaniel Borkmann }; 6411e420bed0SDaniel Borkmann 6412fada7fdcSJonathan Lemon struct bpf_xdp_sock { 6413fada7fdcSJonathan Lemon __u32 queue_id; 6414fada7fdcSJonathan Lemon }; 6415fada7fdcSJonathan Lemon 641617bedab2SMartin KaFai Lau #define XDP_PACKET_HEADROOM 256 641717bedab2SMartin KaFai Lau 64186a773a15SBrenden Blanco /* User return codes for XDP prog type. 64196a773a15SBrenden Blanco * A valid XDP program must return one of these defined values. All other 64209beb8bedSDaniel Borkmann * return codes are reserved for future use. Unknown return codes will 64219beb8bedSDaniel Borkmann * result in packet drops and a warning via bpf_warn_invalid_xdp_action(). 64226a773a15SBrenden Blanco */ 64236a773a15SBrenden Blanco enum xdp_action { 64246a773a15SBrenden Blanco XDP_ABORTED = 0, 64256a773a15SBrenden Blanco XDP_DROP, 64266a773a15SBrenden Blanco XDP_PASS, 64276ce96ca3SBrenden Blanco XDP_TX, 6428814abfabSJohn Fastabend XDP_REDIRECT, 64296a773a15SBrenden Blanco }; 64306a773a15SBrenden Blanco 64316a773a15SBrenden Blanco /* user accessible metadata for XDP packet hook 64326a773a15SBrenden Blanco * new fields must be added to the end of this structure 64336a773a15SBrenden Blanco */ 64346a773a15SBrenden Blanco struct xdp_md { 64356a773a15SBrenden Blanco __u32 data; 64366a773a15SBrenden Blanco __u32 data_end; 6437de8f3a83SDaniel Borkmann __u32 data_meta; 6438daaf24c6SJesper Dangaard Brouer /* Below access go through struct xdp_rxq_info */ 643902dd3291SJesper Dangaard Brouer __u32 ingress_ifindex; /* rxq->dev->ifindex */ 644002dd3291SJesper Dangaard Brouer __u32 rx_queue_index; /* rxq->queue_index */ 644164b59025SDavid Ahern 644264b59025SDavid Ahern __u32 egress_ifindex; /* txq->dev->ifindex */ 64436a773a15SBrenden Blanco }; 64446a773a15SBrenden Blanco 6445281920b7SJesper Dangaard Brouer /* DEVMAP map-value layout 6446281920b7SJesper Dangaard Brouer * 6447281920b7SJesper Dangaard Brouer * The struct data-layout of map-value is a configuration interface. 6448281920b7SJesper Dangaard Brouer * New members can only be added to the end of this structure. 6449281920b7SJesper Dangaard Brouer */ 6450281920b7SJesper Dangaard Brouer struct bpf_devmap_val { 6451281920b7SJesper Dangaard Brouer __u32 ifindex; /* device index */ 6452281920b7SJesper Dangaard Brouer union { 6453281920b7SJesper Dangaard Brouer int fd; /* prog fd on map write */ 6454281920b7SJesper Dangaard Brouer __u32 id; /* prog id on map read */ 6455281920b7SJesper Dangaard Brouer } bpf_prog; 6456281920b7SJesper Dangaard Brouer }; 6457281920b7SJesper Dangaard Brouer 6458644bfe51SLorenzo Bianconi /* CPUMAP map-value layout 6459644bfe51SLorenzo Bianconi * 6460644bfe51SLorenzo Bianconi * The struct data-layout of map-value is a configuration interface. 6461644bfe51SLorenzo Bianconi * New members can only be added to the end of this structure. 6462644bfe51SLorenzo Bianconi */ 6463644bfe51SLorenzo Bianconi struct bpf_cpumap_val { 6464644bfe51SLorenzo Bianconi __u32 qsize; /* queue size to remote target CPU */ 646592164774SLorenzo Bianconi union { 646692164774SLorenzo Bianconi int fd; /* prog fd on map write */ 646792164774SLorenzo Bianconi __u32 id; /* prog id on map read */ 646892164774SLorenzo Bianconi } bpf_prog; 6469644bfe51SLorenzo Bianconi }; 6470644bfe51SLorenzo Bianconi 6471174a79ffSJohn Fastabend enum sk_action { 6472bfa64075SJohn Fastabend SK_DROP = 0, 6473bfa64075SJohn Fastabend SK_PASS, 6474174a79ffSJohn Fastabend }; 6475174a79ffSJohn Fastabend 64764f738adbSJohn Fastabend /* user accessible metadata for SK_MSG packet hook, new fields must 64774f738adbSJohn Fastabend * be added to the end of this structure 64784f738adbSJohn Fastabend */ 64794f738adbSJohn Fastabend struct sk_msg_md { 6480b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data); 6481b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data_end); 6482303def35SJohn Fastabend 6483303def35SJohn Fastabend __u32 family; 6484303def35SJohn Fastabend __u32 remote_ip4; /* Stored in network byte order */ 6485303def35SJohn Fastabend __u32 local_ip4; /* Stored in network byte order */ 6486303def35SJohn Fastabend __u32 remote_ip6[4]; /* Stored in network byte order */ 6487303def35SJohn Fastabend __u32 local_ip6[4]; /* Stored in network byte order */ 6488303def35SJohn Fastabend __u32 remote_port; /* Stored in network byte order */ 6489303def35SJohn Fastabend __u32 local_port; /* stored in host byte order */ 64903bdbd022SJohn Fastabend __u32 size; /* Total size of sk_msg */ 649113d70f5aSJohn Fastabend 649213d70f5aSJohn Fastabend __bpf_md_ptr(struct bpf_sock *, sk); /* current socket */ 64934f738adbSJohn Fastabend }; 64944f738adbSJohn Fastabend 64952dbb9b9eSMartin KaFai Lau struct sk_reuseport_md { 64962dbb9b9eSMartin KaFai Lau /* 64972dbb9b9eSMartin KaFai Lau * Start of directly accessible data. It begins from 64982dbb9b9eSMartin KaFai Lau * the tcp/udp header. 64992dbb9b9eSMartin KaFai Lau */ 6500b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data); 6501b7df9adaSDaniel Borkmann /* End of directly accessible data */ 6502b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data_end); 65032dbb9b9eSMartin KaFai Lau /* 65042dbb9b9eSMartin KaFai Lau * Total length of packet (starting from the tcp/udp header). 65052dbb9b9eSMartin KaFai Lau * Note that the directly accessible bytes (data_end - data) 65062dbb9b9eSMartin KaFai Lau * could be less than this "len". Those bytes could be 65072dbb9b9eSMartin KaFai Lau * indirectly read by a helper "bpf_skb_load_bytes()". 65082dbb9b9eSMartin KaFai Lau */ 65092dbb9b9eSMartin KaFai Lau __u32 len; 65102dbb9b9eSMartin KaFai Lau /* 65112dbb9b9eSMartin KaFai Lau * Eth protocol in the mac header (network byte order). e.g. 65122dbb9b9eSMartin KaFai Lau * ETH_P_IP(0x0800) and ETH_P_IPV6(0x86DD) 65132dbb9b9eSMartin KaFai Lau */ 65142dbb9b9eSMartin KaFai Lau __u32 eth_protocol; 65152dbb9b9eSMartin KaFai Lau __u32 ip_protocol; /* IP protocol. e.g. IPPROTO_TCP, IPPROTO_UDP */ 65162dbb9b9eSMartin KaFai Lau __u32 bind_inany; /* Is sock bound to an INANY address? */ 65172dbb9b9eSMartin KaFai Lau __u32 hash; /* A hash of the packet 4 tuples */ 6518d5e4ddaeSKuniyuki Iwashima /* When reuse->migrating_sk is NULL, it is selecting a sk for the 6519d5e4ddaeSKuniyuki Iwashima * new incoming connection request (e.g. selecting a listen sk for 6520d5e4ddaeSKuniyuki Iwashima * the received SYN in the TCP case). reuse->sk is one of the sk 6521d5e4ddaeSKuniyuki Iwashima * in the reuseport group. The bpf prog can use reuse->sk to learn 6522d5e4ddaeSKuniyuki Iwashima * the local listening ip/port without looking into the skb. 6523d5e4ddaeSKuniyuki Iwashima * 6524d5e4ddaeSKuniyuki Iwashima * When reuse->migrating_sk is not NULL, reuse->sk is closed and 6525d5e4ddaeSKuniyuki Iwashima * reuse->migrating_sk is the socket that needs to be migrated 6526d5e4ddaeSKuniyuki Iwashima * to another listening socket. migrating_sk could be a fullsock 6527d5e4ddaeSKuniyuki Iwashima * sk that is fully established or a reqsk that is in-the-middle 6528d5e4ddaeSKuniyuki Iwashima * of 3-way handshake. 6529d5e4ddaeSKuniyuki Iwashima */ 6530e0610476SKuniyuki Iwashima __bpf_md_ptr(struct bpf_sock *, sk); 6531d5e4ddaeSKuniyuki Iwashima __bpf_md_ptr(struct bpf_sock *, migrating_sk); 65322dbb9b9eSMartin KaFai Lau }; 65332dbb9b9eSMartin KaFai Lau 65341e270976SMartin KaFai Lau #define BPF_TAG_SIZE 8 65351e270976SMartin KaFai Lau 65361e270976SMartin KaFai Lau struct bpf_prog_info { 65371e270976SMartin KaFai Lau __u32 type; 65381e270976SMartin KaFai Lau __u32 id; 65391e270976SMartin KaFai Lau __u8 tag[BPF_TAG_SIZE]; 65401e270976SMartin KaFai Lau __u32 jited_prog_len; 65411e270976SMartin KaFai Lau __u32 xlated_prog_len; 65421e270976SMartin KaFai Lau __aligned_u64 jited_prog_insns; 65431e270976SMartin KaFai Lau __aligned_u64 xlated_prog_insns; 6544cb4d2b3fSMartin KaFai Lau __u64 load_time; /* ns since boottime */ 6545cb4d2b3fSMartin KaFai Lau __u32 created_by_uid; 6546cb4d2b3fSMartin KaFai Lau __u32 nr_map_ids; 6547cb4d2b3fSMartin KaFai Lau __aligned_u64 map_ids; 6548067cae47SMartin KaFai Lau char name[BPF_OBJ_NAME_LEN]; 6549675fc275SJakub Kicinski __u32 ifindex; 6550b85fab0eSJiri Olsa __u32 gpl_compatible:1; 65510472301aSBaruch Siach __u32 :31; /* alignment pad */ 6552675fc275SJakub Kicinski __u64 netns_dev; 6553675fc275SJakub Kicinski __u64 netns_ino; 6554dbecd738SSandipan Das __u32 nr_jited_ksyms; 6555815581c1SSandipan Das __u32 nr_jited_func_lens; 6556dbecd738SSandipan Das __aligned_u64 jited_ksyms; 6557815581c1SSandipan Das __aligned_u64 jited_func_lens; 6558838e9690SYonghong Song __u32 btf_id; 6559838e9690SYonghong Song __u32 func_info_rec_size; 6560838e9690SYonghong Song __aligned_u64 func_info; 656111d8b82dSYonghong Song __u32 nr_func_info; 656211d8b82dSYonghong Song __u32 nr_line_info; 6563c454a46bSMartin KaFai Lau __aligned_u64 line_info; 6564c454a46bSMartin KaFai Lau __aligned_u64 jited_line_info; 656511d8b82dSYonghong Song __u32 nr_jited_line_info; 6566c454a46bSMartin KaFai Lau __u32 line_info_rec_size; 6567c454a46bSMartin KaFai Lau __u32 jited_line_info_rec_size; 6568c872bdb3SSong Liu __u32 nr_prog_tags; 6569c872bdb3SSong Liu __aligned_u64 prog_tags; 65705f8f8b93SAlexei Starovoitov __u64 run_time_ns; 65715f8f8b93SAlexei Starovoitov __u64 run_cnt; 65729ed9e9baSAlexei Starovoitov __u64 recursion_misses; 6573aba64c7dSDave Marchevsky __u32 verified_insns; 6574b79c9fc9SStanislav Fomichev __u32 attach_btf_obj_id; 6575b79c9fc9SStanislav Fomichev __u32 attach_btf_id; 65761e270976SMartin KaFai Lau } __attribute__((aligned(8))); 65771e270976SMartin KaFai Lau 65781e270976SMartin KaFai Lau struct bpf_map_info { 65791e270976SMartin KaFai Lau __u32 type; 65801e270976SMartin KaFai Lau __u32 id; 65811e270976SMartin KaFai Lau __u32 key_size; 65821e270976SMartin KaFai Lau __u32 value_size; 65831e270976SMartin KaFai Lau __u32 max_entries; 65841e270976SMartin KaFai Lau __u32 map_flags; 6585067cae47SMartin KaFai Lau char name[BPF_OBJ_NAME_LEN]; 658652775b33SJakub Kicinski __u32 ifindex; 658785d33df3SMartin KaFai Lau __u32 btf_vmlinux_value_type_id; 658852775b33SJakub Kicinski __u64 netns_dev; 658952775b33SJakub Kicinski __u64 netns_ino; 659078958fcaSMartin KaFai Lau __u32 btf_id; 65919b2cf328SMartin KaFai Lau __u32 btf_key_type_id; 65929b2cf328SMartin KaFai Lau __u32 btf_value_type_id; 65931338b933SKui-Feng Lee __u32 btf_vmlinux_id; 65949330986cSJoanne Koong __u64 map_extra; 65951e270976SMartin KaFai Lau } __attribute__((aligned(8))); 65961e270976SMartin KaFai Lau 659762dab84cSMartin KaFai Lau struct bpf_btf_info { 659862dab84cSMartin KaFai Lau __aligned_u64 btf; 659962dab84cSMartin KaFai Lau __u32 btf_size; 660062dab84cSMartin KaFai Lau __u32 id; 660153297220SAndrii Nakryiko __aligned_u64 name; 660253297220SAndrii Nakryiko __u32 name_len; 660353297220SAndrii Nakryiko __u32 kernel_btf; 660462dab84cSMartin KaFai Lau } __attribute__((aligned(8))); 660562dab84cSMartin KaFai Lau 6606f2e10bffSAndrii Nakryiko struct bpf_link_info { 6607f2e10bffSAndrii Nakryiko __u32 type; 6608f2e10bffSAndrii Nakryiko __u32 id; 6609f2e10bffSAndrii Nakryiko __u32 prog_id; 6610f2e10bffSAndrii Nakryiko union { 6611f2e10bffSAndrii Nakryiko struct { 6612f2e10bffSAndrii Nakryiko __aligned_u64 tp_name; /* in/out: tp_name buffer ptr */ 6613f2e10bffSAndrii Nakryiko __u32 tp_name_len; /* in/out: tp_name buffer len */ 6614f2e10bffSAndrii Nakryiko } raw_tracepoint; 6615f2e10bffSAndrii Nakryiko struct { 6616f2e10bffSAndrii Nakryiko __u32 attach_type; 6617441e8c66SToke Høiland-Jørgensen __u32 target_obj_id; /* prog_id for PROG_EXT, otherwise btf object id */ 6618441e8c66SToke Høiland-Jørgensen __u32 target_btf_id; /* BTF type id inside the object */ 6619f2e10bffSAndrii Nakryiko } tracing; 6620f2e10bffSAndrii Nakryiko struct { 6621f2e10bffSAndrii Nakryiko __u64 cgroup_id; 6622f2e10bffSAndrii Nakryiko __u32 attach_type; 6623f2e10bffSAndrii Nakryiko } cgroup; 66247f045a49SJakub Sitnicki struct { 66256b0a249aSYonghong Song __aligned_u64 target_name; /* in/out: target_name buffer ptr */ 66266b0a249aSYonghong Song __u32 target_name_len; /* in/out: target_name buffer len */ 6627d4ccaf58SHao Luo 6628d4ccaf58SHao Luo /* If the iter specific field is 32 bits, it can be put 6629d4ccaf58SHao Luo * in the first or second union. Otherwise it should be 6630d4ccaf58SHao Luo * put in the second union. 6631d4ccaf58SHao Luo */ 66326b0a249aSYonghong Song union { 6633b0c9eb37SYonghong Song struct { 66346b0a249aSYonghong Song __u32 map_id; 66356b0a249aSYonghong Song } map; 6636b0c9eb37SYonghong Song }; 6637d4ccaf58SHao Luo union { 6638d4ccaf58SHao Luo struct { 6639d4ccaf58SHao Luo __u64 cgroup_id; 6640d4ccaf58SHao Luo __u32 order; 6641d4ccaf58SHao Luo } cgroup; 664221fb6f2aSKui-Feng Lee struct { 664321fb6f2aSKui-Feng Lee __u32 tid; 664421fb6f2aSKui-Feng Lee __u32 pid; 664521fb6f2aSKui-Feng Lee } task; 6646d4ccaf58SHao Luo }; 66476b0a249aSYonghong Song } iter; 66486b0a249aSYonghong Song struct { 66497f045a49SJakub Sitnicki __u32 netns_ino; 66507f045a49SJakub Sitnicki __u32 attach_type; 66517f045a49SJakub Sitnicki } netns; 6652c1931c97SAndrii Nakryiko struct { 6653c1931c97SAndrii Nakryiko __u32 ifindex; 6654c1931c97SAndrii Nakryiko } xdp; 665568b04864SKui-Feng Lee struct { 665668b04864SKui-Feng Lee __u32 map_id; 665768b04864SKui-Feng Lee } struct_ops; 665884601d6eSFlorian Westphal struct { 665984601d6eSFlorian Westphal __u32 pf; 666084601d6eSFlorian Westphal __u32 hooknum; 666184601d6eSFlorian Westphal __s32 priority; 666284601d6eSFlorian Westphal __u32 flags; 666384601d6eSFlorian Westphal } netfilter; 66647ac8d0d2SYafang Shao struct { 66657ac8d0d2SYafang Shao __aligned_u64 addrs; 66667ac8d0d2SYafang Shao __u32 count; /* in/out: kprobe_multi function count */ 66677ac8d0d2SYafang Shao __u32 flags; 6668e2b2cd59SJiri Olsa __u64 missed; 66699fd112b1SJiri Olsa __aligned_u64 cookies; 66707ac8d0d2SYafang Shao } kprobe_multi; 66711b715e1bSYafang Shao struct { 6672e56fdbfbSJiri Olsa __aligned_u64 path; 6673e56fdbfbSJiri Olsa __aligned_u64 offsets; 6674e56fdbfbSJiri Olsa __aligned_u64 ref_ctr_offsets; 6675e56fdbfbSJiri Olsa __aligned_u64 cookies; 6676e56fdbfbSJiri Olsa __u32 path_size; /* in/out: real path size on success, including zero byte */ 6677e56fdbfbSJiri Olsa __u32 count; /* in/out: uprobe_multi offsets/ref_ctr_offsets/cookies count */ 6678e56fdbfbSJiri Olsa __u32 flags; 6679e56fdbfbSJiri Olsa __u32 pid; 6680e56fdbfbSJiri Olsa } uprobe_multi; 6681e56fdbfbSJiri Olsa struct { 66821b715e1bSYafang Shao __u32 type; /* enum bpf_perf_event_type */ 66831b715e1bSYafang Shao __u32 :32; 66841b715e1bSYafang Shao union { 66851b715e1bSYafang Shao struct { 66861b715e1bSYafang Shao __aligned_u64 file_name; /* in/out */ 66871b715e1bSYafang Shao __u32 name_len; 66881b715e1bSYafang Shao __u32 offset; /* offset from file_name */ 6689d5c16492SJiri Olsa __u64 cookie; 66901b715e1bSYafang Shao } uprobe; /* BPF_PERF_EVENT_UPROBE, BPF_PERF_EVENT_URETPROBE */ 66911b715e1bSYafang Shao struct { 66921b715e1bSYafang Shao __aligned_u64 func_name; /* in/out */ 66931b715e1bSYafang Shao __u32 name_len; 66941b715e1bSYafang Shao __u32 offset; /* offset from func_name */ 66951b715e1bSYafang Shao __u64 addr; 66963acf8aceSJiri Olsa __u64 missed; 6697d5c16492SJiri Olsa __u64 cookie; 66981b715e1bSYafang Shao } kprobe; /* BPF_PERF_EVENT_KPROBE, BPF_PERF_EVENT_KRETPROBE */ 66991b715e1bSYafang Shao struct { 67001b715e1bSYafang Shao __aligned_u64 tp_name; /* in/out */ 67011b715e1bSYafang Shao __u32 name_len; 6702d5c16492SJiri Olsa __u32 :32; 6703d5c16492SJiri Olsa __u64 cookie; 67041b715e1bSYafang Shao } tracepoint; /* BPF_PERF_EVENT_TRACEPOINT */ 67051b715e1bSYafang Shao struct { 67061b715e1bSYafang Shao __u64 config; 67071b715e1bSYafang Shao __u32 type; 6708d5c16492SJiri Olsa __u32 :32; 6709d5c16492SJiri Olsa __u64 cookie; 67101b715e1bSYafang Shao } event; /* BPF_PERF_EVENT_EVENT */ 67111b715e1bSYafang Shao }; 67121b715e1bSYafang Shao } perf_event; 6713e420bed0SDaniel Borkmann struct { 6714e420bed0SDaniel Borkmann __u32 ifindex; 6715e420bed0SDaniel Borkmann __u32 attach_type; 6716e420bed0SDaniel Borkmann } tcx; 671735dfaad7SDaniel Borkmann struct { 671835dfaad7SDaniel Borkmann __u32 ifindex; 671935dfaad7SDaniel Borkmann __u32 attach_type; 672035dfaad7SDaniel Borkmann } netkit; 6721f2e10bffSAndrii Nakryiko }; 6722f2e10bffSAndrii Nakryiko } __attribute__((aligned(8))); 6723f2e10bffSAndrii Nakryiko 67244fbac77dSAndrey Ignatov /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed 67254fbac77dSAndrey Ignatov * by user and intended to be used by socket (e.g. to bind to, depends on 6726bfdfa517SRandy Dunlap * attach type). 67274fbac77dSAndrey Ignatov */ 67284fbac77dSAndrey Ignatov struct bpf_sock_addr { 67294fbac77dSAndrey Ignatov __u32 user_family; /* Allows 4-byte read, but no write. */ 67304fbac77dSAndrey Ignatov __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write. 67314fbac77dSAndrey Ignatov * Stored in network byte order. 67324fbac77dSAndrey Ignatov */ 6733d4ecfeb1SStanislav Fomichev __u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write. 67344fbac77dSAndrey Ignatov * Stored in network byte order. 67354fbac77dSAndrey Ignatov */ 67367aebfa1bSAndrey Ignatov __u32 user_port; /* Allows 1,2,4-byte read and 4-byte write. 67374fbac77dSAndrey Ignatov * Stored in network byte order 67384fbac77dSAndrey Ignatov */ 67394fbac77dSAndrey Ignatov __u32 family; /* Allows 4-byte read, but no write */ 67404fbac77dSAndrey Ignatov __u32 type; /* Allows 4-byte read, but no write */ 67414fbac77dSAndrey Ignatov __u32 protocol; /* Allows 4-byte read, but no write */ 6742600c70baSStanislav Fomichev __u32 msg_src_ip4; /* Allows 1,2,4-byte read and 4-byte write. 67431cedee13SAndrey Ignatov * Stored in network byte order. 67441cedee13SAndrey Ignatov */ 6745d4ecfeb1SStanislav Fomichev __u32 msg_src_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write. 67461cedee13SAndrey Ignatov * Stored in network byte order. 67471cedee13SAndrey Ignatov */ 6748fb85c4a7SStanislav Fomichev __bpf_md_ptr(struct bpf_sock *, sk); 67494fbac77dSAndrey Ignatov }; 67504fbac77dSAndrey Ignatov 675140304b2aSLawrence Brakmo /* User bpf_sock_ops struct to access socket values and specify request ops 675240304b2aSLawrence Brakmo * and their replies. 675340304b2aSLawrence Brakmo * Some of this fields are in network (bigendian) byte order and may need 675440304b2aSLawrence Brakmo * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h). 675540304b2aSLawrence Brakmo * New fields can only be added at the end of this structure 675640304b2aSLawrence Brakmo */ 675740304b2aSLawrence Brakmo struct bpf_sock_ops { 675840304b2aSLawrence Brakmo __u32 op; 675940304b2aSLawrence Brakmo union { 6760de525be2SLawrence Brakmo __u32 args[4]; /* Optionally passed to bpf program */ 6761de525be2SLawrence Brakmo __u32 reply; /* Returned by bpf program */ 6762de525be2SLawrence Brakmo __u32 replylong[4]; /* Optionally returned by bpf prog */ 676340304b2aSLawrence Brakmo }; 676440304b2aSLawrence Brakmo __u32 family; 676540304b2aSLawrence Brakmo __u32 remote_ip4; /* Stored in network byte order */ 676640304b2aSLawrence Brakmo __u32 local_ip4; /* Stored in network byte order */ 676740304b2aSLawrence Brakmo __u32 remote_ip6[4]; /* Stored in network byte order */ 676840304b2aSLawrence Brakmo __u32 local_ip6[4]; /* Stored in network byte order */ 676940304b2aSLawrence Brakmo __u32 remote_port; /* Stored in network byte order */ 677040304b2aSLawrence Brakmo __u32 local_port; /* stored in host byte order */ 6771f19397a5SLawrence Brakmo __u32 is_fullsock; /* Some TCP fields are only valid if 6772f19397a5SLawrence Brakmo * there is a full socket. If not, the 6773f19397a5SLawrence Brakmo * fields read as zero. 6774f19397a5SLawrence Brakmo */ 6775f19397a5SLawrence Brakmo __u32 snd_cwnd; 6776f19397a5SLawrence Brakmo __u32 srtt_us; /* Averaged RTT << 3 in usecs */ 6777b13d8807SLawrence Brakmo __u32 bpf_sock_ops_cb_flags; /* flags defined in uapi/linux/tcp.h */ 677844f0e430SLawrence Brakmo __u32 state; 677944f0e430SLawrence Brakmo __u32 rtt_min; 678044f0e430SLawrence Brakmo __u32 snd_ssthresh; 678144f0e430SLawrence Brakmo __u32 rcv_nxt; 678244f0e430SLawrence Brakmo __u32 snd_nxt; 678344f0e430SLawrence Brakmo __u32 snd_una; 678444f0e430SLawrence Brakmo __u32 mss_cache; 678544f0e430SLawrence Brakmo __u32 ecn_flags; 678644f0e430SLawrence Brakmo __u32 rate_delivered; 678744f0e430SLawrence Brakmo __u32 rate_interval_us; 678844f0e430SLawrence Brakmo __u32 packets_out; 678944f0e430SLawrence Brakmo __u32 retrans_out; 679044f0e430SLawrence Brakmo __u32 total_retrans; 679144f0e430SLawrence Brakmo __u32 segs_in; 679244f0e430SLawrence Brakmo __u32 data_segs_in; 679344f0e430SLawrence Brakmo __u32 segs_out; 679444f0e430SLawrence Brakmo __u32 data_segs_out; 679544f0e430SLawrence Brakmo __u32 lost_out; 679644f0e430SLawrence Brakmo __u32 sacked_out; 679744f0e430SLawrence Brakmo __u32 sk_txhash; 679844f0e430SLawrence Brakmo __u64 bytes_received; 679944f0e430SLawrence Brakmo __u64 bytes_acked; 68001314ef56SStanislav Fomichev __bpf_md_ptr(struct bpf_sock *, sk); 68010813a841SMartin KaFai Lau /* [skb_data, skb_data_end) covers the whole TCP header. 68020813a841SMartin KaFai Lau * 68030813a841SMartin KaFai Lau * BPF_SOCK_OPS_PARSE_HDR_OPT_CB: The packet received 68040813a841SMartin KaFai Lau * BPF_SOCK_OPS_HDR_OPT_LEN_CB: Not useful because the 68050813a841SMartin KaFai Lau * header has not been written. 68060813a841SMartin KaFai Lau * BPF_SOCK_OPS_WRITE_HDR_OPT_CB: The header and options have 68070813a841SMartin KaFai Lau * been written so far. 68080813a841SMartin KaFai Lau * BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: The SYNACK that concludes 68090813a841SMartin KaFai Lau * the 3WHS. 68100813a841SMartin KaFai Lau * BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: The ACK that concludes 68110813a841SMartin KaFai Lau * the 3WHS. 68120813a841SMartin KaFai Lau * 68130813a841SMartin KaFai Lau * bpf_load_hdr_opt() can also be used to read a particular option. 68140813a841SMartin KaFai Lau */ 68150813a841SMartin KaFai Lau __bpf_md_ptr(void *, skb_data); 68160813a841SMartin KaFai Lau __bpf_md_ptr(void *, skb_data_end); 68170813a841SMartin KaFai Lau __u32 skb_len; /* The total length of a packet. 68180813a841SMartin KaFai Lau * It includes the header, options, 68190813a841SMartin KaFai Lau * and payload. 68200813a841SMartin KaFai Lau */ 68210813a841SMartin KaFai Lau __u32 skb_tcp_flags; /* tcp_flags of the header. It provides 68220813a841SMartin KaFai Lau * an easy way to check for tcp_flags 68230813a841SMartin KaFai Lau * without parsing skb_data. 68240813a841SMartin KaFai Lau * 68250813a841SMartin KaFai Lau * In particular, the skb_tcp_flags 68260813a841SMartin KaFai Lau * will still be available in 68270813a841SMartin KaFai Lau * BPF_SOCK_OPS_HDR_OPT_LEN even though 68280813a841SMartin KaFai Lau * the outgoing header has not 68290813a841SMartin KaFai Lau * been written yet. 68300813a841SMartin KaFai Lau */ 68319bb05349SMartin KaFai Lau __u64 skb_hwtstamp; 683240304b2aSLawrence Brakmo }; 683340304b2aSLawrence Brakmo 6834b13d8807SLawrence Brakmo /* Definitions for bpf_sock_ops_cb_flags */ 68351aae4bddSAndrii Nakryiko enum { 68361aae4bddSAndrii Nakryiko BPF_SOCK_OPS_RTO_CB_FLAG = (1<<0), 68371aae4bddSAndrii Nakryiko BPF_SOCK_OPS_RETRANS_CB_FLAG = (1<<1), 68381aae4bddSAndrii Nakryiko BPF_SOCK_OPS_STATE_CB_FLAG = (1<<2), 68391aae4bddSAndrii Nakryiko BPF_SOCK_OPS_RTT_CB_FLAG = (1<<3), 68400813a841SMartin KaFai Lau /* Call bpf for all received TCP headers. The bpf prog will be 68410813a841SMartin KaFai Lau * called under sock_ops->op == BPF_SOCK_OPS_PARSE_HDR_OPT_CB 68420813a841SMartin KaFai Lau * 68430813a841SMartin KaFai Lau * Please refer to the comment in BPF_SOCK_OPS_PARSE_HDR_OPT_CB 68440813a841SMartin KaFai Lau * for the header option related helpers that will be useful 68450813a841SMartin KaFai Lau * to the bpf programs. 68460813a841SMartin KaFai Lau * 68470813a841SMartin KaFai Lau * It could be used at the client/active side (i.e. connect() side) 68480813a841SMartin KaFai Lau * when the server told it that the server was in syncookie 68490813a841SMartin KaFai Lau * mode and required the active side to resend the bpf-written 68500813a841SMartin KaFai Lau * options. The active side can keep writing the bpf-options until 68510813a841SMartin KaFai Lau * it received a valid packet from the server side to confirm 68520813a841SMartin KaFai Lau * the earlier packet (and options) has been received. The later 68530813a841SMartin KaFai Lau * example patch is using it like this at the active side when the 68540813a841SMartin KaFai Lau * server is in syncookie mode. 68550813a841SMartin KaFai Lau * 68560813a841SMartin KaFai Lau * The bpf prog will usually turn this off in the common cases. 68570813a841SMartin KaFai Lau */ 685800d211a4SMartin KaFai Lau BPF_SOCK_OPS_PARSE_ALL_HDR_OPT_CB_FLAG = (1<<4), 68590813a841SMartin KaFai Lau /* Call bpf when kernel has received a header option that 68600813a841SMartin KaFai Lau * the kernel cannot handle. The bpf prog will be called under 68610813a841SMartin KaFai Lau * sock_ops->op == BPF_SOCK_OPS_PARSE_HDR_OPT_CB. 68620813a841SMartin KaFai Lau * 68630813a841SMartin KaFai Lau * Please refer to the comment in BPF_SOCK_OPS_PARSE_HDR_OPT_CB 68640813a841SMartin KaFai Lau * for the header option related helpers that will be useful 68650813a841SMartin KaFai Lau * to the bpf programs. 68660813a841SMartin KaFai Lau */ 686700d211a4SMartin KaFai Lau BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG = (1<<5), 68680813a841SMartin KaFai Lau /* Call bpf when the kernel is writing header options for the 68690813a841SMartin KaFai Lau * outgoing packet. The bpf prog will first be called 68700813a841SMartin KaFai Lau * to reserve space in a skb under 68710813a841SMartin KaFai Lau * sock_ops->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB. Then 68720813a841SMartin KaFai Lau * the bpf prog will be called to write the header option(s) 68730813a841SMartin KaFai Lau * under sock_ops->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB. 68740813a841SMartin KaFai Lau * 68750813a841SMartin KaFai Lau * Please refer to the comment in BPF_SOCK_OPS_HDR_OPT_LEN_CB 68760813a841SMartin KaFai Lau * and BPF_SOCK_OPS_WRITE_HDR_OPT_CB for the header option 68770813a841SMartin KaFai Lau * related helpers that will be useful to the bpf programs. 68780813a841SMartin KaFai Lau * 68790813a841SMartin KaFai Lau * The kernel gets its chance to reserve space and write 68800813a841SMartin KaFai Lau * options first before the BPF program does. 68810813a841SMartin KaFai Lau */ 6882331fca43SMartin KaFai Lau BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1<<6), 68831aae4bddSAndrii Nakryiko /* Mask of all currently supported cb flags */ 6884331fca43SMartin KaFai Lau BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, 68851aae4bddSAndrii Nakryiko }; 6886b13d8807SLawrence Brakmo 688740304b2aSLawrence Brakmo /* List of known BPF sock_ops operators. 688840304b2aSLawrence Brakmo * New entries can only be added at the end 688940304b2aSLawrence Brakmo */ 689040304b2aSLawrence Brakmo enum { 689140304b2aSLawrence Brakmo BPF_SOCK_OPS_VOID, 68928550f328SLawrence Brakmo BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or 68938550f328SLawrence Brakmo * -1 if default value should be used 68948550f328SLawrence Brakmo */ 689513d3b1ebSLawrence Brakmo BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized 689613d3b1ebSLawrence Brakmo * window (in packets) or -1 if default 689713d3b1ebSLawrence Brakmo * value should be used 689813d3b1ebSLawrence Brakmo */ 68999872a4bdSLawrence Brakmo BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an 69009872a4bdSLawrence Brakmo * active connection is initialized 69019872a4bdSLawrence Brakmo */ 69029872a4bdSLawrence Brakmo BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an 69039872a4bdSLawrence Brakmo * active connection is 69049872a4bdSLawrence Brakmo * established 69059872a4bdSLawrence Brakmo */ 69069872a4bdSLawrence Brakmo BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a 69079872a4bdSLawrence Brakmo * passive connection is 69089872a4bdSLawrence Brakmo * established 69099872a4bdSLawrence Brakmo */ 691091b5b21cSLawrence Brakmo BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control 691191b5b21cSLawrence Brakmo * needs ECN 691291b5b21cSLawrence Brakmo */ 6913e6546ef6SLawrence Brakmo BPF_SOCK_OPS_BASE_RTT, /* Get base RTT. The correct value is 6914e6546ef6SLawrence Brakmo * based on the path and may be 6915e6546ef6SLawrence Brakmo * dependent on the congestion control 6916e6546ef6SLawrence Brakmo * algorithm. In general it indicates 6917e6546ef6SLawrence Brakmo * a congestion threshold. RTTs above 6918e6546ef6SLawrence Brakmo * this indicate congestion 6919e6546ef6SLawrence Brakmo */ 6920f89013f6SLawrence Brakmo BPF_SOCK_OPS_RTO_CB, /* Called when an RTO has triggered. 6921f89013f6SLawrence Brakmo * Arg1: value of icsk_retransmits 6922f89013f6SLawrence Brakmo * Arg2: value of icsk_rto 6923f89013f6SLawrence Brakmo * Arg3: whether RTO has expired 6924f89013f6SLawrence Brakmo */ 6925a31ad29eSLawrence Brakmo BPF_SOCK_OPS_RETRANS_CB, /* Called when skb is retransmitted. 6926a31ad29eSLawrence Brakmo * Arg1: sequence number of 1st byte 6927a31ad29eSLawrence Brakmo * Arg2: # segments 6928a31ad29eSLawrence Brakmo * Arg3: return value of 6929a31ad29eSLawrence Brakmo * tcp_transmit_skb (0 => success) 6930a31ad29eSLawrence Brakmo */ 6931d4487491SLawrence Brakmo BPF_SOCK_OPS_STATE_CB, /* Called when TCP changes state. 6932d4487491SLawrence Brakmo * Arg1: old_state 6933d4487491SLawrence Brakmo * Arg2: new_state 6934d4487491SLawrence Brakmo */ 6935f333ee0cSAndrey Ignatov BPF_SOCK_OPS_TCP_LISTEN_CB, /* Called on listen(2), right after 6936f333ee0cSAndrey Ignatov * socket transition to LISTEN state. 6937f333ee0cSAndrey Ignatov */ 693823729ff2SStanislav Fomichev BPF_SOCK_OPS_RTT_CB, /* Called on every RTT. 693923729ff2SStanislav Fomichev */ 69400813a841SMartin KaFai Lau BPF_SOCK_OPS_PARSE_HDR_OPT_CB, /* Parse the header option. 69410813a841SMartin KaFai Lau * It will be called to handle 69420813a841SMartin KaFai Lau * the packets received at 69430813a841SMartin KaFai Lau * an already established 69440813a841SMartin KaFai Lau * connection. 69450813a841SMartin KaFai Lau * 69460813a841SMartin KaFai Lau * sock_ops->skb_data: 69470813a841SMartin KaFai Lau * Referring to the received skb. 69480813a841SMartin KaFai Lau * It covers the TCP header only. 69490813a841SMartin KaFai Lau * 69500813a841SMartin KaFai Lau * bpf_load_hdr_opt() can also 69510813a841SMartin KaFai Lau * be used to search for a 69520813a841SMartin KaFai Lau * particular option. 69530813a841SMartin KaFai Lau */ 69540813a841SMartin KaFai Lau BPF_SOCK_OPS_HDR_OPT_LEN_CB, /* Reserve space for writing the 69550813a841SMartin KaFai Lau * header option later in 69560813a841SMartin KaFai Lau * BPF_SOCK_OPS_WRITE_HDR_OPT_CB. 69570813a841SMartin KaFai Lau * Arg1: bool want_cookie. (in 69580813a841SMartin KaFai Lau * writing SYNACK only) 69590813a841SMartin KaFai Lau * 69600813a841SMartin KaFai Lau * sock_ops->skb_data: 69610813a841SMartin KaFai Lau * Not available because no header has 69620813a841SMartin KaFai Lau * been written yet. 69630813a841SMartin KaFai Lau * 69640813a841SMartin KaFai Lau * sock_ops->skb_tcp_flags: 69650813a841SMartin KaFai Lau * The tcp_flags of the 69660813a841SMartin KaFai Lau * outgoing skb. (e.g. SYN, ACK, FIN). 69670813a841SMartin KaFai Lau * 69680813a841SMartin KaFai Lau * bpf_reserve_hdr_opt() should 69690813a841SMartin KaFai Lau * be used to reserve space. 69700813a841SMartin KaFai Lau */ 69710813a841SMartin KaFai Lau BPF_SOCK_OPS_WRITE_HDR_OPT_CB, /* Write the header options 69720813a841SMartin KaFai Lau * Arg1: bool want_cookie. (in 69730813a841SMartin KaFai Lau * writing SYNACK only) 69740813a841SMartin KaFai Lau * 69750813a841SMartin KaFai Lau * sock_ops->skb_data: 69760813a841SMartin KaFai Lau * Referring to the outgoing skb. 69770813a841SMartin KaFai Lau * It covers the TCP header 69780813a841SMartin KaFai Lau * that has already been written 69790813a841SMartin KaFai Lau * by the kernel and the 69800813a841SMartin KaFai Lau * earlier bpf-progs. 69810813a841SMartin KaFai Lau * 69820813a841SMartin KaFai Lau * sock_ops->skb_tcp_flags: 69830813a841SMartin KaFai Lau * The tcp_flags of the outgoing 69840813a841SMartin KaFai Lau * skb. (e.g. SYN, ACK, FIN). 69850813a841SMartin KaFai Lau * 69860813a841SMartin KaFai Lau * bpf_store_hdr_opt() should 69870813a841SMartin KaFai Lau * be used to write the 69880813a841SMartin KaFai Lau * option. 69890813a841SMartin KaFai Lau * 69900813a841SMartin KaFai Lau * bpf_load_hdr_opt() can also 69910813a841SMartin KaFai Lau * be used to search for a 69920813a841SMartin KaFai Lau * particular option that 69930813a841SMartin KaFai Lau * has already been written 69940813a841SMartin KaFai Lau * by the kernel or the 69950813a841SMartin KaFai Lau * earlier bpf-progs. 69960813a841SMartin KaFai Lau */ 6997d4487491SLawrence Brakmo }; 6998d4487491SLawrence Brakmo 6999d4487491SLawrence Brakmo /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect 7000d4487491SLawrence Brakmo * changes between the TCP and BPF versions. Ideally this should never happen. 7001d4487491SLawrence Brakmo * If it does, we need to add code to convert them before calling 7002d4487491SLawrence Brakmo * the BPF sock_ops function. 7003d4487491SLawrence Brakmo */ 7004d4487491SLawrence Brakmo enum { 7005d4487491SLawrence Brakmo BPF_TCP_ESTABLISHED = 1, 7006d4487491SLawrence Brakmo BPF_TCP_SYN_SENT, 7007d4487491SLawrence Brakmo BPF_TCP_SYN_RECV, 7008d4487491SLawrence Brakmo BPF_TCP_FIN_WAIT1, 7009d4487491SLawrence Brakmo BPF_TCP_FIN_WAIT2, 7010d4487491SLawrence Brakmo BPF_TCP_TIME_WAIT, 7011d4487491SLawrence Brakmo BPF_TCP_CLOSE, 7012d4487491SLawrence Brakmo BPF_TCP_CLOSE_WAIT, 7013d4487491SLawrence Brakmo BPF_TCP_LAST_ACK, 7014d4487491SLawrence Brakmo BPF_TCP_LISTEN, 7015d4487491SLawrence Brakmo BPF_TCP_CLOSING, /* Now a valid state */ 7016d4487491SLawrence Brakmo BPF_TCP_NEW_SYN_RECV, 701791051f00SGuillaume Nault BPF_TCP_BOUND_INACTIVE, 7018d4487491SLawrence Brakmo 7019d4487491SLawrence Brakmo BPF_TCP_MAX_STATES /* Leave at the end! */ 702040304b2aSLawrence Brakmo }; 702140304b2aSLawrence Brakmo 70221aae4bddSAndrii Nakryiko enum { 70231aae4bddSAndrii Nakryiko TCP_BPF_IW = 1001, /* Set TCP initial congestion window */ 70241aae4bddSAndrii Nakryiko TCP_BPF_SNDCWND_CLAMP = 1002, /* Set sndcwnd_clamp */ 70252b8ee4f0SMartin KaFai Lau TCP_BPF_DELACK_MAX = 1003, /* Max delay ack in usecs */ 7026ca584ba0SMartin KaFai Lau TCP_BPF_RTO_MIN = 1004, /* Min delay ack in usecs */ 70270813a841SMartin KaFai Lau /* Copy the SYN pkt to optval 70280813a841SMartin KaFai Lau * 70290813a841SMartin KaFai Lau * BPF_PROG_TYPE_SOCK_OPS only. It is similar to the 70300813a841SMartin KaFai Lau * bpf_getsockopt(TCP_SAVED_SYN) but it does not limit 70310813a841SMartin KaFai Lau * to only getting from the saved_syn. It can either get the 70320813a841SMartin KaFai Lau * syn packet from: 70330813a841SMartin KaFai Lau * 70340813a841SMartin KaFai Lau * 1. the just-received SYN packet (only available when writing the 70350813a841SMartin KaFai Lau * SYNACK). It will be useful when it is not necessary to 70360813a841SMartin KaFai Lau * save the SYN packet for latter use. It is also the only way 70370813a841SMartin KaFai Lau * to get the SYN during syncookie mode because the syn 70380813a841SMartin KaFai Lau * packet cannot be saved during syncookie. 70390813a841SMartin KaFai Lau * 70400813a841SMartin KaFai Lau * OR 70410813a841SMartin KaFai Lau * 70420813a841SMartin KaFai Lau * 2. the earlier saved syn which was done by 70430813a841SMartin KaFai Lau * bpf_setsockopt(TCP_SAVE_SYN). 70440813a841SMartin KaFai Lau * 70450813a841SMartin KaFai Lau * The bpf_getsockopt(TCP_BPF_SYN*) option will hide where the 70460813a841SMartin KaFai Lau * SYN packet is obtained. 70470813a841SMartin KaFai Lau * 70480813a841SMartin KaFai Lau * If the bpf-prog does not need the IP[46] header, the 70490813a841SMartin KaFai Lau * bpf-prog can avoid parsing the IP header by using 70500813a841SMartin KaFai Lau * TCP_BPF_SYN. Otherwise, the bpf-prog can get both 70510813a841SMartin KaFai Lau * IP[46] and TCP header by using TCP_BPF_SYN_IP. 70520813a841SMartin KaFai Lau * 70530813a841SMartin KaFai Lau * >0: Total number of bytes copied 70540813a841SMartin KaFai Lau * -ENOSPC: Not enough space in optval. Only optlen number of 70550813a841SMartin KaFai Lau * bytes is copied. 70560813a841SMartin KaFai Lau * -ENOENT: The SYN skb is not available now and the earlier SYN pkt 70570813a841SMartin KaFai Lau * is not saved by setsockopt(TCP_SAVE_SYN). 70580813a841SMartin KaFai Lau */ 70590813a841SMartin KaFai Lau TCP_BPF_SYN = 1005, /* Copy the TCP header */ 70600813a841SMartin KaFai Lau TCP_BPF_SYN_IP = 1006, /* Copy the IP[46] and TCP header */ 7061267cf9faSMartin KaFai Lau TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */ 70620813a841SMartin KaFai Lau }; 70630813a841SMartin KaFai Lau 70640813a841SMartin KaFai Lau enum { 70650813a841SMartin KaFai Lau BPF_LOAD_HDR_OPT_TCP_SYN = (1ULL << 0), 70660813a841SMartin KaFai Lau }; 70670813a841SMartin KaFai Lau 70680813a841SMartin KaFai Lau /* args[0] value during BPF_SOCK_OPS_HDR_OPT_LEN_CB and 70690813a841SMartin KaFai Lau * BPF_SOCK_OPS_WRITE_HDR_OPT_CB. 70700813a841SMartin KaFai Lau */ 70710813a841SMartin KaFai Lau enum { 70720813a841SMartin KaFai Lau BPF_WRITE_HDR_TCP_CURRENT_MSS = 1, /* Kernel is finding the 70730813a841SMartin KaFai Lau * total option spaces 70740813a841SMartin KaFai Lau * required for an established 70750813a841SMartin KaFai Lau * sk in order to calculate the 70760813a841SMartin KaFai Lau * MSS. No skb is actually 70770813a841SMartin KaFai Lau * sent. 70780813a841SMartin KaFai Lau */ 70790813a841SMartin KaFai Lau BPF_WRITE_HDR_TCP_SYNACK_COOKIE = 2, /* Kernel is in syncookie mode 70800813a841SMartin KaFai Lau * when sending a SYN. 70810813a841SMartin KaFai Lau */ 70821aae4bddSAndrii Nakryiko }; 7083fc747810SLawrence Brakmo 7084908432caSYonghong Song struct bpf_perf_event_value { 7085908432caSYonghong Song __u64 counter; 7086908432caSYonghong Song __u64 enabled; 7087908432caSYonghong Song __u64 running; 7088908432caSYonghong Song }; 7089908432caSYonghong Song 70901aae4bddSAndrii Nakryiko enum { 70911aae4bddSAndrii Nakryiko BPF_DEVCG_ACC_MKNOD = (1ULL << 0), 70921aae4bddSAndrii Nakryiko BPF_DEVCG_ACC_READ = (1ULL << 1), 70931aae4bddSAndrii Nakryiko BPF_DEVCG_ACC_WRITE = (1ULL << 2), 70941aae4bddSAndrii Nakryiko }; 7095ebc614f6SRoman Gushchin 70961aae4bddSAndrii Nakryiko enum { 70971aae4bddSAndrii Nakryiko BPF_DEVCG_DEV_BLOCK = (1ULL << 0), 70981aae4bddSAndrii Nakryiko BPF_DEVCG_DEV_CHAR = (1ULL << 1), 70991aae4bddSAndrii Nakryiko }; 7100ebc614f6SRoman Gushchin 7101ebc614f6SRoman Gushchin struct bpf_cgroup_dev_ctx { 710206ef0ccbSYonghong Song /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */ 710306ef0ccbSYonghong Song __u32 access_type; 7104ebc614f6SRoman Gushchin __u32 major; 7105ebc614f6SRoman Gushchin __u32 minor; 7106ebc614f6SRoman Gushchin }; 7107ebc614f6SRoman Gushchin 7108c4f6699dSAlexei Starovoitov struct bpf_raw_tracepoint_args { 7109c4f6699dSAlexei Starovoitov __u64 args[0]; 7110c4f6699dSAlexei Starovoitov }; 7111c4f6699dSAlexei Starovoitov 711287f5fc7eSDavid Ahern /* DIRECT: Skip the FIB rules and go to FIB table associated with device 711387f5fc7eSDavid Ahern * OUTPUT: Do lookup from egress perspective; default is ingress 711487f5fc7eSDavid Ahern */ 71151aae4bddSAndrii Nakryiko enum { 71161aae4bddSAndrii Nakryiko BPF_FIB_LOOKUP_DIRECT = (1U << 0), 71171aae4bddSAndrii Nakryiko BPF_FIB_LOOKUP_OUTPUT = (1U << 1), 711831de4105SMartin KaFai Lau BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2), 71198ad77e72SLouis DeLosSantos BPF_FIB_LOOKUP_TBID = (1U << 3), 7120dab4e1f0SMartynas Pumputis BPF_FIB_LOOKUP_SRC = (1U << 4), 71211aae4bddSAndrii Nakryiko }; 712287f5fc7eSDavid Ahern 71234c79579bSDavid Ahern enum { 71244c79579bSDavid Ahern BPF_FIB_LKUP_RET_SUCCESS, /* lookup successful */ 71254c79579bSDavid Ahern BPF_FIB_LKUP_RET_BLACKHOLE, /* dest is blackholed; can be dropped */ 71264c79579bSDavid Ahern BPF_FIB_LKUP_RET_UNREACHABLE, /* dest is unreachable; can be dropped */ 71274c79579bSDavid Ahern BPF_FIB_LKUP_RET_PROHIBIT, /* dest not allowed; can be dropped */ 71284c79579bSDavid Ahern BPF_FIB_LKUP_RET_NOT_FWDED, /* packet is not forwarded */ 71294c79579bSDavid Ahern BPF_FIB_LKUP_RET_FWD_DISABLED, /* fwding is not enabled on ingress */ 71304c79579bSDavid Ahern BPF_FIB_LKUP_RET_UNSUPP_LWT, /* fwd requires encapsulation */ 71314c79579bSDavid Ahern BPF_FIB_LKUP_RET_NO_NEIGH, /* no neighbor entry for nh */ 71324c79579bSDavid Ahern BPF_FIB_LKUP_RET_FRAG_NEEDED, /* fragmentation required to fwd */ 7133dab4e1f0SMartynas Pumputis BPF_FIB_LKUP_RET_NO_SRC_ADDR, /* failed to derive IP src addr */ 71344c79579bSDavid Ahern }; 71354c79579bSDavid Ahern 713687f5fc7eSDavid Ahern struct bpf_fib_lookup { 7137fa898d76SDavid Ahern /* input: network family for lookup (AF_INET, AF_INET6) 7138fa898d76SDavid Ahern * output: network family of egress nexthop 7139fa898d76SDavid Ahern */ 7140fa898d76SDavid Ahern __u8 family; 714187f5fc7eSDavid Ahern 714287f5fc7eSDavid Ahern /* set if lookup is to consider L4 data - e.g., FIB rules */ 714387f5fc7eSDavid Ahern __u8 l4_protocol; 714487f5fc7eSDavid Ahern __be16 sport; 714587f5fc7eSDavid Ahern __be16 dport; 714687f5fc7eSDavid Ahern 7147e1850ea9SJesper Dangaard Brouer union { /* used for MTU check */ 7148e1850ea9SJesper Dangaard Brouer /* input to lookup */ 7149e1850ea9SJesper Dangaard Brouer __u16 tot_len; /* L3 length from network hdr (iph->tot_len) */ 71504c79579bSDavid Ahern 7151e1850ea9SJesper Dangaard Brouer /* output: MTU value */ 7152e1850ea9SJesper Dangaard Brouer __u16 mtu_result; 7153e1850ea9SJesper Dangaard Brouer }; 71544c79579bSDavid Ahern /* input: L3 device index for lookup 71554c79579bSDavid Ahern * output: device index from FIB lookup 71564c79579bSDavid Ahern */ 71574c79579bSDavid Ahern __u32 ifindex; 715887f5fc7eSDavid Ahern 715987f5fc7eSDavid Ahern union { 716087f5fc7eSDavid Ahern /* inputs to lookup */ 716187f5fc7eSDavid Ahern __u8 tos; /* AF_INET */ 7162bd3a08aaSDavid Ahern __be32 flowinfo; /* AF_INET6, flow_label + priority */ 716387f5fc7eSDavid Ahern 7164fa898d76SDavid Ahern /* output: metric of fib result (IPv4/IPv6 only) */ 716587f5fc7eSDavid Ahern __u32 rt_metric; 716687f5fc7eSDavid Ahern }; 716787f5fc7eSDavid Ahern 7168dab4e1f0SMartynas Pumputis /* input: source address to consider for lookup 7169dab4e1f0SMartynas Pumputis * output: source address result from lookup 7170dab4e1f0SMartynas Pumputis */ 717187f5fc7eSDavid Ahern union { 717287f5fc7eSDavid Ahern __be32 ipv4_src; 717387f5fc7eSDavid Ahern __u32 ipv6_src[4]; /* in6_addr; network order */ 717487f5fc7eSDavid Ahern }; 717587f5fc7eSDavid Ahern 7176fa898d76SDavid Ahern /* input to bpf_fib_lookup, ipv{4,6}_dst is destination address in 7177fa898d76SDavid Ahern * network header. output: bpf_fib_lookup sets to gateway address 7178fa898d76SDavid Ahern * if FIB lookup returns gateway route 717987f5fc7eSDavid Ahern */ 718087f5fc7eSDavid Ahern union { 718187f5fc7eSDavid Ahern __be32 ipv4_dst; 718287f5fc7eSDavid Ahern __u32 ipv6_dst[4]; /* in6_addr; network order */ 718387f5fc7eSDavid Ahern }; 718487f5fc7eSDavid Ahern 71858ad77e72SLouis DeLosSantos union { 71868ad77e72SLouis DeLosSantos struct { 718787f5fc7eSDavid Ahern /* output */ 718887f5fc7eSDavid Ahern __be16 h_vlan_proto; 718987f5fc7eSDavid Ahern __be16 h_vlan_TCI; 71908ad77e72SLouis DeLosSantos }; 71918ad77e72SLouis DeLosSantos /* input: when accompanied with the 71928ad77e72SLouis DeLosSantos * 'BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID` flags, a 71938ad77e72SLouis DeLosSantos * specific routing table to use for the fib lookup. 71948ad77e72SLouis DeLosSantos */ 71958ad77e72SLouis DeLosSantos __u32 tbid; 71968ad77e72SLouis DeLosSantos }; 71978ad77e72SLouis DeLosSantos 719887f5fc7eSDavid Ahern __u8 smac[6]; /* ETH_ALEN */ 719987f5fc7eSDavid Ahern __u8 dmac[6]; /* ETH_ALEN */ 720087f5fc7eSDavid Ahern }; 720187f5fc7eSDavid Ahern 7202ba452c9eSToke Høiland-Jørgensen struct bpf_redir_neigh { 7203ba452c9eSToke Høiland-Jørgensen /* network family for lookup (AF_INET, AF_INET6) */ 7204ba452c9eSToke Høiland-Jørgensen __u32 nh_family; 7205ba452c9eSToke Høiland-Jørgensen /* network address of nexthop; skips fib lookup to find gateway */ 7206ba452c9eSToke Høiland-Jørgensen union { 7207ba452c9eSToke Høiland-Jørgensen __be32 ipv4_nh; 7208ba452c9eSToke Høiland-Jørgensen __u32 ipv6_nh[4]; /* in6_addr; network order */ 7209ba452c9eSToke Høiland-Jørgensen }; 7210ba452c9eSToke Høiland-Jørgensen }; 7211ba452c9eSToke Høiland-Jørgensen 721234b2021cSJesper Dangaard Brouer /* bpf_check_mtu flags*/ 721334b2021cSJesper Dangaard Brouer enum bpf_check_mtu_flags { 721434b2021cSJesper Dangaard Brouer BPF_MTU_CHK_SEGS = (1U << 0), 721534b2021cSJesper Dangaard Brouer }; 721634b2021cSJesper Dangaard Brouer 721734b2021cSJesper Dangaard Brouer enum bpf_check_mtu_ret { 721834b2021cSJesper Dangaard Brouer BPF_MTU_CHK_RET_SUCCESS, /* check and lookup successful */ 721934b2021cSJesper Dangaard Brouer BPF_MTU_CHK_RET_FRAG_NEEDED, /* fragmentation required to fwd */ 722034b2021cSJesper Dangaard Brouer BPF_MTU_CHK_RET_SEGS_TOOBIG, /* GSO re-segmentation needed to fwd */ 722134b2021cSJesper Dangaard Brouer }; 722234b2021cSJesper Dangaard Brouer 722341bdc4b4SYonghong Song enum bpf_task_fd_type { 722441bdc4b4SYonghong Song BPF_FD_TYPE_RAW_TRACEPOINT, /* tp name */ 722541bdc4b4SYonghong Song BPF_FD_TYPE_TRACEPOINT, /* tp name */ 722641bdc4b4SYonghong Song BPF_FD_TYPE_KPROBE, /* (symbol + offset) or addr */ 722741bdc4b4SYonghong Song BPF_FD_TYPE_KRETPROBE, /* (symbol + offset) or addr */ 722841bdc4b4SYonghong Song BPF_FD_TYPE_UPROBE, /* filename + offset */ 722941bdc4b4SYonghong Song BPF_FD_TYPE_URETPROBE, /* filename + offset */ 723041bdc4b4SYonghong Song }; 723141bdc4b4SYonghong Song 72321aae4bddSAndrii Nakryiko enum { 72331aae4bddSAndrii Nakryiko BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG = (1U << 0), 72341aae4bddSAndrii Nakryiko BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL = (1U << 1), 72351aae4bddSAndrii Nakryiko BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP = (1U << 2), 72361aae4bddSAndrii Nakryiko }; 7237086f9568SStanislav Fomichev 7238d58e468bSPetar Penkov struct bpf_flow_keys { 7239d58e468bSPetar Penkov __u16 nhoff; 7240d58e468bSPetar Penkov __u16 thoff; 7241d58e468bSPetar Penkov __u16 addr_proto; /* ETH_P_* of valid addrs */ 7242d58e468bSPetar Penkov __u8 is_frag; 7243d58e468bSPetar Penkov __u8 is_first_frag; 7244d58e468bSPetar Penkov __u8 is_encap; 7245d58e468bSPetar Penkov __u8 ip_proto; 7246d58e468bSPetar Penkov __be16 n_proto; 7247d58e468bSPetar Penkov __be16 sport; 7248d58e468bSPetar Penkov __be16 dport; 7249d58e468bSPetar Penkov union { 7250d58e468bSPetar Penkov struct { 7251d58e468bSPetar Penkov __be32 ipv4_src; 7252d58e468bSPetar Penkov __be32 ipv4_dst; 7253d58e468bSPetar Penkov }; 7254d58e468bSPetar Penkov struct { 7255d58e468bSPetar Penkov __u32 ipv6_src[4]; /* in6_addr; network order */ 7256d58e468bSPetar Penkov __u32 ipv6_dst[4]; /* in6_addr; network order */ 7257d58e468bSPetar Penkov }; 7258d58e468bSPetar Penkov }; 7259086f9568SStanislav Fomichev __u32 flags; 726071c99e32SStanislav Fomichev __be32 flow_label; 7261d58e468bSPetar Penkov }; 7262d58e468bSPetar Penkov 7263838e9690SYonghong Song struct bpf_func_info { 7264d30d42e0SMartin KaFai Lau __u32 insn_off; 7265838e9690SYonghong Song __u32 type_id; 7266838e9690SYonghong Song }; 7267838e9690SYonghong Song 7268c454a46bSMartin KaFai Lau #define BPF_LINE_INFO_LINE_NUM(line_col) ((line_col) >> 10) 7269c454a46bSMartin KaFai Lau #define BPF_LINE_INFO_LINE_COL(line_col) ((line_col) & 0x3ff) 7270c454a46bSMartin KaFai Lau 7271c454a46bSMartin KaFai Lau struct bpf_line_info { 7272c454a46bSMartin KaFai Lau __u32 insn_off; 7273c454a46bSMartin KaFai Lau __u32 file_name_off; 7274c454a46bSMartin KaFai Lau __u32 line_off; 7275c454a46bSMartin KaFai Lau __u32 line_col; 7276c454a46bSMartin KaFai Lau }; 7277c454a46bSMartin KaFai Lau 7278d83525caSAlexei Starovoitov struct bpf_spin_lock { 7279d83525caSAlexei Starovoitov __u32 val; 7280d83525caSAlexei Starovoitov }; 72817b146cebSAndrey Ignatov 7282b00628b1SAlexei Starovoitov struct bpf_timer { 7283155addf0SYonghong Song __u64 __opaque[2]; 7284b00628b1SAlexei Starovoitov } __attribute__((aligned(8))); 7285b00628b1SAlexei Starovoitov 728697e03f52SJoanne Koong struct bpf_dynptr { 7287155addf0SYonghong Song __u64 __opaque[2]; 728897e03f52SJoanne Koong } __attribute__((aligned(8))); 728997e03f52SJoanne Koong 7290f0c5941fSKumar Kartikeya Dwivedi struct bpf_list_head { 7291155addf0SYonghong Song __u64 __opaque[2]; 7292f0c5941fSKumar Kartikeya Dwivedi } __attribute__((aligned(8))); 7293f0c5941fSKumar Kartikeya Dwivedi 7294f0c5941fSKumar Kartikeya Dwivedi struct bpf_list_node { 7295155addf0SYonghong Song __u64 __opaque[3]; 7296f0c5941fSKumar Kartikeya Dwivedi } __attribute__((aligned(8))); 7297f0c5941fSKumar Kartikeya Dwivedi 72989c395c1bSDave Marchevsky struct bpf_rb_root { 7299155addf0SYonghong Song __u64 __opaque[2]; 73009c395c1bSDave Marchevsky } __attribute__((aligned(8))); 73019c395c1bSDave Marchevsky 73029c395c1bSDave Marchevsky struct bpf_rb_node { 7303155addf0SYonghong Song __u64 __opaque[4]; 73049c395c1bSDave Marchevsky } __attribute__((aligned(8))); 73059c395c1bSDave Marchevsky 7306d54730b5SDave Marchevsky struct bpf_refcount { 7307155addf0SYonghong Song __u32 __opaque[1]; 7308d54730b5SDave Marchevsky } __attribute__((aligned(4))); 7309d54730b5SDave Marchevsky 73107b146cebSAndrey Ignatov struct bpf_sysctl { 73117b146cebSAndrey Ignatov __u32 write; /* Sysctl is being read (= 0) or written (= 1). 73127b146cebSAndrey Ignatov * Allows 1,2,4-byte read, but no write. 73137b146cebSAndrey Ignatov */ 7314e1550bfeSAndrey Ignatov __u32 file_pos; /* Sysctl file position to read from, write to. 7315e1550bfeSAndrey Ignatov * Allows 1,2,4-byte read an 4-byte write. 7316e1550bfeSAndrey Ignatov */ 73177b146cebSAndrey Ignatov }; 73187b146cebSAndrey Ignatov 73190d01da6aSStanislav Fomichev struct bpf_sockopt { 73200d01da6aSStanislav Fomichev __bpf_md_ptr(struct bpf_sock *, sk); 73210d01da6aSStanislav Fomichev __bpf_md_ptr(void *, optval); 73220d01da6aSStanislav Fomichev __bpf_md_ptr(void *, optval_end); 73230d01da6aSStanislav Fomichev 73240d01da6aSStanislav Fomichev __s32 level; 73250d01da6aSStanislav Fomichev __s32 optname; 73260d01da6aSStanislav Fomichev __s32 optlen; 73270d01da6aSStanislav Fomichev __s32 retval; 73280d01da6aSStanislav Fomichev }; 73290d01da6aSStanislav Fomichev 7330b4490c5cSCarlos Neira struct bpf_pidns_info { 7331b4490c5cSCarlos Neira __u32 pid; 7332b4490c5cSCarlos Neira __u32 tgid; 7333b4490c5cSCarlos Neira }; 7334e9ddbb77SJakub Sitnicki 7335e9ddbb77SJakub Sitnicki /* User accessible data for SK_LOOKUP programs. Add new fields at the end. */ 7336e9ddbb77SJakub Sitnicki struct bpf_sk_lookup { 73377c32e8f8SLorenz Bauer union { 7338e9ddbb77SJakub Sitnicki __bpf_md_ptr(struct bpf_sock *, sk); /* Selected socket */ 73397c32e8f8SLorenz Bauer __u64 cookie; /* Non-zero if socket was selected in PROG_TEST_RUN */ 73407c32e8f8SLorenz Bauer }; 7341e9ddbb77SJakub Sitnicki 7342e9ddbb77SJakub Sitnicki __u32 family; /* Protocol family (AF_INET, AF_INET6) */ 7343e9ddbb77SJakub Sitnicki __u32 protocol; /* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */ 7344e9ddbb77SJakub Sitnicki __u32 remote_ip4; /* Network byte order */ 7345e9ddbb77SJakub Sitnicki __u32 remote_ip6[4]; /* Network byte order */ 73469a69e2b3SJakub Sitnicki __be16 remote_port; /* Network byte order */ 73479a69e2b3SJakub Sitnicki __u16 :16; /* Zero padding */ 7348e9ddbb77SJakub Sitnicki __u32 local_ip4; /* Network byte order */ 7349e9ddbb77SJakub Sitnicki __u32 local_ip6[4]; /* Network byte order */ 7350e9ddbb77SJakub Sitnicki __u32 local_port; /* Host byte order */ 7351f8931565SMark Pashmfouroush __u32 ingress_ifindex; /* The arriving interface. Determined by inet_iif. */ 7352e9ddbb77SJakub Sitnicki }; 7353e9ddbb77SJakub Sitnicki 7354c4d0bfb4SAlan Maguire /* 7355c4d0bfb4SAlan Maguire * struct btf_ptr is used for typed pointer representation; the 7356c4d0bfb4SAlan Maguire * type id is used to render the pointer data as the appropriate type 7357c4d0bfb4SAlan Maguire * via the bpf_snprintf_btf() helper described above. A flags field - 7358c4d0bfb4SAlan Maguire * potentially to specify additional details about the BTF pointer 7359c4d0bfb4SAlan Maguire * (rather than its mode of display) - is included for future use. 7360c4d0bfb4SAlan Maguire * Display flags - BTF_F_* - are passed to bpf_snprintf_btf separately. 7361c4d0bfb4SAlan Maguire */ 7362c4d0bfb4SAlan Maguire struct btf_ptr { 7363c4d0bfb4SAlan Maguire void *ptr; 7364c4d0bfb4SAlan Maguire __u32 type_id; 7365c4d0bfb4SAlan Maguire __u32 flags; /* BTF ptr flags; unused at present. */ 7366c4d0bfb4SAlan Maguire }; 7367c4d0bfb4SAlan Maguire 7368c4d0bfb4SAlan Maguire /* 7369c4d0bfb4SAlan Maguire * Flags to control bpf_snprintf_btf() behaviour. 7370c4d0bfb4SAlan Maguire * - BTF_F_COMPACT: no formatting around type information 7371c4d0bfb4SAlan Maguire * - BTF_F_NONAME: no struct/union member names/types 7372c4d0bfb4SAlan Maguire * - BTF_F_PTR_RAW: show raw (unobfuscated) pointer values; 7373c4d0bfb4SAlan Maguire * equivalent to %px. 7374c4d0bfb4SAlan Maguire * - BTF_F_ZERO: show zero-valued struct/union members; they 7375c4d0bfb4SAlan Maguire * are not displayed by default 7376c4d0bfb4SAlan Maguire */ 7377c4d0bfb4SAlan Maguire enum { 7378c4d0bfb4SAlan Maguire BTF_F_COMPACT = (1ULL << 0), 7379c4d0bfb4SAlan Maguire BTF_F_NONAME = (1ULL << 1), 7380c4d0bfb4SAlan Maguire BTF_F_PTR_RAW = (1ULL << 2), 7381c4d0bfb4SAlan Maguire BTF_F_ZERO = (1ULL << 3), 7382c4d0bfb4SAlan Maguire }; 7383c4d0bfb4SAlan Maguire 738446334a0cSAlexei Starovoitov /* bpf_core_relo_kind encodes which aspect of captured field/type/enum value 738546334a0cSAlexei Starovoitov * has to be adjusted by relocations. It is emitted by llvm and passed to 738646334a0cSAlexei Starovoitov * libbpf and later to the kernel. 738746334a0cSAlexei Starovoitov */ 738846334a0cSAlexei Starovoitov enum bpf_core_relo_kind { 738946334a0cSAlexei Starovoitov BPF_CORE_FIELD_BYTE_OFFSET = 0, /* field byte offset */ 739046334a0cSAlexei Starovoitov BPF_CORE_FIELD_BYTE_SIZE = 1, /* field size in bytes */ 739146334a0cSAlexei Starovoitov BPF_CORE_FIELD_EXISTS = 2, /* field existence in target kernel */ 739246334a0cSAlexei Starovoitov BPF_CORE_FIELD_SIGNED = 3, /* field signedness (0 - unsigned, 1 - signed) */ 739346334a0cSAlexei Starovoitov BPF_CORE_FIELD_LSHIFT_U64 = 4, /* bitfield-specific left bitshift */ 739446334a0cSAlexei Starovoitov BPF_CORE_FIELD_RSHIFT_U64 = 5, /* bitfield-specific right bitshift */ 739546334a0cSAlexei Starovoitov BPF_CORE_TYPE_ID_LOCAL = 6, /* type ID in local BPF object */ 739646334a0cSAlexei Starovoitov BPF_CORE_TYPE_ID_TARGET = 7, /* type ID in target kernel */ 739746334a0cSAlexei Starovoitov BPF_CORE_TYPE_EXISTS = 8, /* type existence in target kernel */ 739846334a0cSAlexei Starovoitov BPF_CORE_TYPE_SIZE = 9, /* type size in bytes */ 739946334a0cSAlexei Starovoitov BPF_CORE_ENUMVAL_EXISTS = 10, /* enum value existence in target kernel */ 740046334a0cSAlexei Starovoitov BPF_CORE_ENUMVAL_VALUE = 11, /* enum value integer value */ 74013c660a5dSDaniel Müller BPF_CORE_TYPE_MATCHES = 12, /* type match in target kernel */ 740246334a0cSAlexei Starovoitov }; 740346334a0cSAlexei Starovoitov 7404fbd94c7aSAlexei Starovoitov /* 7405fbd94c7aSAlexei Starovoitov * "struct bpf_core_relo" is used to pass relocation data form LLVM to libbpf 7406fbd94c7aSAlexei Starovoitov * and from libbpf to the kernel. 7407fbd94c7aSAlexei Starovoitov * 7408fbd94c7aSAlexei Starovoitov * CO-RE relocation captures the following data: 7409fbd94c7aSAlexei Starovoitov * - insn_off - instruction offset (in bytes) within a BPF program that needs 7410fbd94c7aSAlexei Starovoitov * its insn->imm field to be relocated with actual field info; 7411fbd94c7aSAlexei Starovoitov * - type_id - BTF type ID of the "root" (containing) entity of a relocatable 7412fbd94c7aSAlexei Starovoitov * type or field; 7413fbd94c7aSAlexei Starovoitov * - access_str_off - offset into corresponding .BTF string section. String 7414fbd94c7aSAlexei Starovoitov * interpretation depends on specific relocation kind: 7415fbd94c7aSAlexei Starovoitov * - for field-based relocations, string encodes an accessed field using 7416fbd94c7aSAlexei Starovoitov * a sequence of field and array indices, separated by colon (:). It's 7417fbd94c7aSAlexei Starovoitov * conceptually very close to LLVM's getelementptr ([0]) instruction's 7418fbd94c7aSAlexei Starovoitov * arguments for identifying offset to a field. 7419fbd94c7aSAlexei Starovoitov * - for type-based relocations, strings is expected to be just "0"; 7420fbd94c7aSAlexei Starovoitov * - for enum value-based relocations, string contains an index of enum 7421fbd94c7aSAlexei Starovoitov * value within its enum type; 7422fbd94c7aSAlexei Starovoitov * - kind - one of enum bpf_core_relo_kind; 7423fbd94c7aSAlexei Starovoitov * 7424fbd94c7aSAlexei Starovoitov * Example: 7425fbd94c7aSAlexei Starovoitov * struct sample { 7426fbd94c7aSAlexei Starovoitov * int a; 7427fbd94c7aSAlexei Starovoitov * struct { 7428fbd94c7aSAlexei Starovoitov * int b[10]; 7429fbd94c7aSAlexei Starovoitov * }; 7430fbd94c7aSAlexei Starovoitov * }; 7431fbd94c7aSAlexei Starovoitov * 7432fbd94c7aSAlexei Starovoitov * struct sample *s = ...; 7433fbd94c7aSAlexei Starovoitov * int *x = &s->a; // encoded as "0:0" (a is field #0) 7434fbd94c7aSAlexei Starovoitov * int *y = &s->b[5]; // encoded as "0:1:0:5" (anon struct is field #1, 7435fbd94c7aSAlexei Starovoitov * // b is field #0 inside anon struct, accessing elem #5) 7436fbd94c7aSAlexei Starovoitov * int *z = &s[10]->b; // encoded as "10:1" (ptr is used as an array) 7437fbd94c7aSAlexei Starovoitov * 7438fbd94c7aSAlexei Starovoitov * type_id for all relocs in this example will capture BTF type id of 7439fbd94c7aSAlexei Starovoitov * `struct sample`. 7440fbd94c7aSAlexei Starovoitov * 7441fbd94c7aSAlexei Starovoitov * Such relocation is emitted when using __builtin_preserve_access_index() 7442fbd94c7aSAlexei Starovoitov * Clang built-in, passing expression that captures field address, e.g.: 7443fbd94c7aSAlexei Starovoitov * 7444fbd94c7aSAlexei Starovoitov * bpf_probe_read(&dst, sizeof(dst), 7445fbd94c7aSAlexei Starovoitov * __builtin_preserve_access_index(&src->a.b.c)); 7446fbd94c7aSAlexei Starovoitov * 7447fbd94c7aSAlexei Starovoitov * In this case Clang will emit field relocation recording necessary data to 7448fbd94c7aSAlexei Starovoitov * be able to find offset of embedded `a.b.c` field within `src` struct. 7449fbd94c7aSAlexei Starovoitov * 7450fbd94c7aSAlexei Starovoitov * [0] https://llvm.org/docs/LangRef.html#getelementptr-instruction 7451fbd94c7aSAlexei Starovoitov */ 7452fbd94c7aSAlexei Starovoitov struct bpf_core_relo { 7453fbd94c7aSAlexei Starovoitov __u32 insn_off; 7454fbd94c7aSAlexei Starovoitov __u32 type_id; 7455fbd94c7aSAlexei Starovoitov __u32 access_str_off; 7456fbd94c7aSAlexei Starovoitov enum bpf_core_relo_kind kind; 7457fbd94c7aSAlexei Starovoitov }; 7458fbd94c7aSAlexei Starovoitov 7459f71f8530STero Kristo /* 7460f71f8530STero Kristo * Flags to control bpf_timer_start() behaviour. 7461f71f8530STero Kristo * - BPF_F_TIMER_ABS: Timeout passed is absolute time, by default it is 7462f71f8530STero Kristo * relative to current time. 7463d6247ecbSDavid Vernet * - BPF_F_TIMER_CPU_PIN: Timer will be pinned to the CPU of the caller. 7464f71f8530STero Kristo */ 7465f71f8530STero Kristo enum { 7466f71f8530STero Kristo BPF_F_TIMER_ABS = (1ULL << 0), 7467d6247ecbSDavid Vernet BPF_F_TIMER_CPU_PIN = (1ULL << 1), 7468f71f8530STero Kristo }; 7469f71f8530STero Kristo 74706018e1f4SAndrii Nakryiko /* BPF numbers iterator state */ 74716018e1f4SAndrii Nakryiko struct bpf_iter_num { 74726018e1f4SAndrii Nakryiko /* opaque iterator state; having __u64 here allows to preserve correct 74736018e1f4SAndrii Nakryiko * alignment requirements in vmlinux.h, generated from BTF 74746018e1f4SAndrii Nakryiko */ 74756018e1f4SAndrii Nakryiko __u64 __opaque[1]; 74766018e1f4SAndrii Nakryiko } __attribute__((aligned(8))); 74776018e1f4SAndrii Nakryiko 7478daedfb22SAlexei Starovoitov #endif /* _UAPI__LINUX_BPF_H__ */ 7479