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) */ 22daedfb22SAlexei Starovoitov #define BPF_XADD 0xc0 /* exclusive add */ 23daedfb22SAlexei Starovoitov 24daedfb22SAlexei Starovoitov /* alu/jmp fields */ 25daedfb22SAlexei Starovoitov #define BPF_MOV 0xb0 /* mov reg to reg */ 26daedfb22SAlexei Starovoitov #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */ 27daedfb22SAlexei Starovoitov 28daedfb22SAlexei Starovoitov /* change endianness of a register */ 29daedfb22SAlexei Starovoitov #define BPF_END 0xd0 /* flags for endianness conversion: */ 30daedfb22SAlexei Starovoitov #define BPF_TO_LE 0x00 /* convert to little-endian */ 31daedfb22SAlexei Starovoitov #define BPF_TO_BE 0x08 /* convert to big-endian */ 32daedfb22SAlexei Starovoitov #define BPF_FROM_LE BPF_TO_LE 33daedfb22SAlexei Starovoitov #define BPF_FROM_BE BPF_TO_BE 34daedfb22SAlexei Starovoitov 3592b31a9aSDaniel Borkmann /* jmp encodings */ 36daedfb22SAlexei Starovoitov #define BPF_JNE 0x50 /* jump != */ 3792b31a9aSDaniel Borkmann #define BPF_JLT 0xa0 /* LT is unsigned, '<' */ 3892b31a9aSDaniel Borkmann #define BPF_JLE 0xb0 /* LE is unsigned, '<=' */ 39daedfb22SAlexei Starovoitov #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */ 40daedfb22SAlexei Starovoitov #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */ 4192b31a9aSDaniel Borkmann #define BPF_JSLT 0xc0 /* SLT is signed, '<' */ 4292b31a9aSDaniel Borkmann #define BPF_JSLE 0xd0 /* SLE is signed, '<=' */ 43daedfb22SAlexei Starovoitov #define BPF_CALL 0x80 /* function call */ 44daedfb22SAlexei Starovoitov #define BPF_EXIT 0x90 /* function return */ 45daedfb22SAlexei Starovoitov 46daedfb22SAlexei Starovoitov /* Register numbers */ 47daedfb22SAlexei Starovoitov enum { 48daedfb22SAlexei Starovoitov BPF_REG_0 = 0, 49daedfb22SAlexei Starovoitov BPF_REG_1, 50daedfb22SAlexei Starovoitov BPF_REG_2, 51daedfb22SAlexei Starovoitov BPF_REG_3, 52daedfb22SAlexei Starovoitov BPF_REG_4, 53daedfb22SAlexei Starovoitov BPF_REG_5, 54daedfb22SAlexei Starovoitov BPF_REG_6, 55daedfb22SAlexei Starovoitov BPF_REG_7, 56daedfb22SAlexei Starovoitov BPF_REG_8, 57daedfb22SAlexei Starovoitov BPF_REG_9, 58daedfb22SAlexei Starovoitov BPF_REG_10, 59daedfb22SAlexei Starovoitov __MAX_BPF_REG, 60daedfb22SAlexei Starovoitov }; 61daedfb22SAlexei Starovoitov 62daedfb22SAlexei Starovoitov /* BPF has 10 general purpose 64-bit registers and stack frame. */ 63daedfb22SAlexei Starovoitov #define MAX_BPF_REG __MAX_BPF_REG 64daedfb22SAlexei Starovoitov 65daedfb22SAlexei Starovoitov struct bpf_insn { 66daedfb22SAlexei Starovoitov __u8 code; /* opcode */ 67daedfb22SAlexei Starovoitov __u8 dst_reg:4; /* dest register */ 68daedfb22SAlexei Starovoitov __u8 src_reg:4; /* source register */ 69daedfb22SAlexei Starovoitov __s16 off; /* signed offset */ 70daedfb22SAlexei Starovoitov __s32 imm; /* signed immediate constant */ 71daedfb22SAlexei Starovoitov }; 72daedfb22SAlexei Starovoitov 73b95a5c4dSDaniel Mack /* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */ 74b95a5c4dSDaniel Mack struct bpf_lpm_trie_key { 75b95a5c4dSDaniel Mack __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */ 76b95a5c4dSDaniel Mack __u8 data[0]; /* Arbitrary size */ 77b95a5c4dSDaniel Mack }; 78b95a5c4dSDaniel Mack 79de9cbbaaSRoman Gushchin struct bpf_cgroup_storage_key { 80de9cbbaaSRoman Gushchin __u64 cgroup_inode_id; /* cgroup inode id */ 81de9cbbaaSRoman Gushchin __u32 attach_type; /* program attach type */ 82de9cbbaaSRoman Gushchin }; 83de9cbbaaSRoman Gushchin 84b2197755SDaniel Borkmann /* BPF syscall commands, see bpf(2) man-page for details. */ 8599c55f7dSAlexei Starovoitov enum bpf_cmd { 8699c55f7dSAlexei Starovoitov BPF_MAP_CREATE, 87db20fd2bSAlexei Starovoitov BPF_MAP_LOOKUP_ELEM, 88db20fd2bSAlexei Starovoitov BPF_MAP_UPDATE_ELEM, 89db20fd2bSAlexei Starovoitov BPF_MAP_DELETE_ELEM, 90db20fd2bSAlexei Starovoitov BPF_MAP_GET_NEXT_KEY, 9109756af4SAlexei Starovoitov BPF_PROG_LOAD, 92b2197755SDaniel Borkmann BPF_OBJ_PIN, 93b2197755SDaniel Borkmann BPF_OBJ_GET, 94f4324551SDaniel Mack BPF_PROG_ATTACH, 95f4324551SDaniel Mack BPF_PROG_DETACH, 961cf1cae9SAlexei Starovoitov BPF_PROG_TEST_RUN, 9734ad5580SMartin KaFai Lau BPF_PROG_GET_NEXT_ID, 9834ad5580SMartin KaFai Lau BPF_MAP_GET_NEXT_ID, 99b16d9aa4SMartin KaFai Lau BPF_PROG_GET_FD_BY_ID, 100bd5f5f4eSMartin KaFai Lau BPF_MAP_GET_FD_BY_ID, 1011e270976SMartin KaFai Lau BPF_OBJ_GET_INFO_BY_FD, 102468e2f64SAlexei Starovoitov BPF_PROG_QUERY, 103c4f6699dSAlexei Starovoitov BPF_RAW_TRACEPOINT_OPEN, 104f56a653cSMartin KaFai Lau BPF_BTF_LOAD, 10578958fcaSMartin KaFai Lau BPF_BTF_GET_FD_BY_ID, 10641bdc4b4SYonghong Song BPF_TASK_FD_QUERY, 107bd513cd0SMauricio Vasquez B BPF_MAP_LOOKUP_AND_DELETE_ELEM, 10899c55f7dSAlexei Starovoitov }; 10999c55f7dSAlexei Starovoitov 11099c55f7dSAlexei Starovoitov enum bpf_map_type { 11199c55f7dSAlexei Starovoitov BPF_MAP_TYPE_UNSPEC, 1120f8e4bd8SAlexei Starovoitov BPF_MAP_TYPE_HASH, 11328fbcfa0SAlexei Starovoitov BPF_MAP_TYPE_ARRAY, 11404fd61abSAlexei Starovoitov BPF_MAP_TYPE_PROG_ARRAY, 115ea317b26SKaixu Xia BPF_MAP_TYPE_PERF_EVENT_ARRAY, 116824bd0ceSAlexei Starovoitov BPF_MAP_TYPE_PERCPU_HASH, 117a10423b8SAlexei Starovoitov BPF_MAP_TYPE_PERCPU_ARRAY, 118d5a3b1f6SAlexei Starovoitov BPF_MAP_TYPE_STACK_TRACE, 1194ed8ec52SMartin KaFai Lau BPF_MAP_TYPE_CGROUP_ARRAY, 12029ba732aSMartin KaFai Lau BPF_MAP_TYPE_LRU_HASH, 1218f844938SMartin KaFai Lau BPF_MAP_TYPE_LRU_PERCPU_HASH, 122b95a5c4dSDaniel Mack BPF_MAP_TYPE_LPM_TRIE, 12356f668dfSMartin KaFai Lau BPF_MAP_TYPE_ARRAY_OF_MAPS, 124bcc6b1b7SMartin KaFai Lau BPF_MAP_TYPE_HASH_OF_MAPS, 125546ac1ffSJohn Fastabend BPF_MAP_TYPE_DEVMAP, 126174a79ffSJohn Fastabend BPF_MAP_TYPE_SOCKMAP, 1276710e112SJesper Dangaard Brouer BPF_MAP_TYPE_CPUMAP, 128fbfc504aSBjörn Töpel BPF_MAP_TYPE_XSKMAP, 12981110384SJohn Fastabend BPF_MAP_TYPE_SOCKHASH, 130de9cbbaaSRoman Gushchin BPF_MAP_TYPE_CGROUP_STORAGE, 1315dc4c4b7SMartin KaFai Lau BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, 132b741f163SRoman Gushchin BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, 133f1a2e44aSMauricio Vasquez B BPF_MAP_TYPE_QUEUE, 134f1a2e44aSMauricio Vasquez B BPF_MAP_TYPE_STACK, 13599c55f7dSAlexei Starovoitov }; 13699c55f7dSAlexei Starovoitov 1376c4fc209SDaniel Borkmann /* Note that tracing related programs such as 1386c4fc209SDaniel Borkmann * BPF_PROG_TYPE_{KPROBE,TRACEPOINT,PERF_EVENT,RAW_TRACEPOINT} 1396c4fc209SDaniel Borkmann * are not subject to a stable API since kernel internal data 1406c4fc209SDaniel Borkmann * structures can change from release to release and may 1416c4fc209SDaniel Borkmann * therefore break existing tracing BPF programs. Tracing BPF 1426c4fc209SDaniel Borkmann * programs correspond to /a/ specific kernel which is to be 1436c4fc209SDaniel Borkmann * analyzed, and not /a/ specific kernel /and/ all future ones. 1446c4fc209SDaniel Borkmann */ 14509756af4SAlexei Starovoitov enum bpf_prog_type { 14609756af4SAlexei Starovoitov BPF_PROG_TYPE_UNSPEC, 147ddd872bcSAlexei Starovoitov BPF_PROG_TYPE_SOCKET_FILTER, 1482541517cSAlexei Starovoitov BPF_PROG_TYPE_KPROBE, 14996be4325SDaniel Borkmann BPF_PROG_TYPE_SCHED_CLS, 15094caee8cSDaniel Borkmann BPF_PROG_TYPE_SCHED_ACT, 15198b5c2c6SAlexei Starovoitov BPF_PROG_TYPE_TRACEPOINT, 1526a773a15SBrenden Blanco BPF_PROG_TYPE_XDP, 1530515e599SAlexei Starovoitov BPF_PROG_TYPE_PERF_EVENT, 1540e33661dSDaniel Mack BPF_PROG_TYPE_CGROUP_SKB, 15561023658SDavid Ahern BPF_PROG_TYPE_CGROUP_SOCK, 1563a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_IN, 1573a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_OUT, 1583a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_XMIT, 15940304b2aSLawrence Brakmo BPF_PROG_TYPE_SOCK_OPS, 160b005fd18SJohn Fastabend BPF_PROG_TYPE_SK_SKB, 161ebc614f6SRoman Gushchin BPF_PROG_TYPE_CGROUP_DEVICE, 1624f738adbSJohn Fastabend BPF_PROG_TYPE_SK_MSG, 163c4f6699dSAlexei Starovoitov BPF_PROG_TYPE_RAW_TRACEPOINT, 1644fbac77dSAndrey Ignatov BPF_PROG_TYPE_CGROUP_SOCK_ADDR, 165004d4b27SMathieu Xhonneux BPF_PROG_TYPE_LWT_SEG6LOCAL, 166f4364dcfSSean Young BPF_PROG_TYPE_LIRC_MODE2, 1672dbb9b9eSMartin KaFai Lau BPF_PROG_TYPE_SK_REUSEPORT, 168d58e468bSPetar Penkov BPF_PROG_TYPE_FLOW_DISSECTOR, 16909756af4SAlexei Starovoitov }; 17009756af4SAlexei Starovoitov 1710e33661dSDaniel Mack enum bpf_attach_type { 1720e33661dSDaniel Mack BPF_CGROUP_INET_INGRESS, 1730e33661dSDaniel Mack BPF_CGROUP_INET_EGRESS, 17461023658SDavid Ahern BPF_CGROUP_INET_SOCK_CREATE, 17540304b2aSLawrence Brakmo BPF_CGROUP_SOCK_OPS, 176464bc0fdSJohn Fastabend BPF_SK_SKB_STREAM_PARSER, 177464bc0fdSJohn Fastabend BPF_SK_SKB_STREAM_VERDICT, 178ebc614f6SRoman Gushchin BPF_CGROUP_DEVICE, 1794f738adbSJohn Fastabend BPF_SK_MSG_VERDICT, 1804fbac77dSAndrey Ignatov BPF_CGROUP_INET4_BIND, 1814fbac77dSAndrey Ignatov BPF_CGROUP_INET6_BIND, 182d74bad4eSAndrey Ignatov BPF_CGROUP_INET4_CONNECT, 183d74bad4eSAndrey Ignatov BPF_CGROUP_INET6_CONNECT, 184aac3fc32SAndrey Ignatov BPF_CGROUP_INET4_POST_BIND, 185aac3fc32SAndrey Ignatov BPF_CGROUP_INET6_POST_BIND, 1861cedee13SAndrey Ignatov BPF_CGROUP_UDP4_SENDMSG, 1871cedee13SAndrey Ignatov BPF_CGROUP_UDP6_SENDMSG, 188f4364dcfSSean Young BPF_LIRC_MODE2, 189d58e468bSPetar Penkov BPF_FLOW_DISSECTOR, 1900e33661dSDaniel Mack __MAX_BPF_ATTACH_TYPE 1910e33661dSDaniel Mack }; 1920e33661dSDaniel Mack 1930e33661dSDaniel Mack #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE 1940e33661dSDaniel Mack 195324bda9eSAlexei Starovoitov /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command 196324bda9eSAlexei Starovoitov * 197324bda9eSAlexei Starovoitov * NONE(default): No further bpf programs allowed in the subtree. 198324bda9eSAlexei Starovoitov * 199324bda9eSAlexei Starovoitov * BPF_F_ALLOW_OVERRIDE: If a sub-cgroup installs some bpf program, 200324bda9eSAlexei Starovoitov * the program in this cgroup yields to sub-cgroup program. 201324bda9eSAlexei Starovoitov * 202324bda9eSAlexei Starovoitov * BPF_F_ALLOW_MULTI: If a sub-cgroup installs some bpf program, 203324bda9eSAlexei Starovoitov * that cgroup program gets run in addition to the program in this cgroup. 204324bda9eSAlexei Starovoitov * 205324bda9eSAlexei Starovoitov * Only one program is allowed to be attached to a cgroup with 206324bda9eSAlexei Starovoitov * NONE or BPF_F_ALLOW_OVERRIDE flag. 207324bda9eSAlexei Starovoitov * Attaching another program on top of NONE or BPF_F_ALLOW_OVERRIDE will 208324bda9eSAlexei Starovoitov * release old program and attach the new one. Attach flags has to match. 209324bda9eSAlexei Starovoitov * 210324bda9eSAlexei Starovoitov * Multiple programs are allowed to be attached to a cgroup with 211324bda9eSAlexei Starovoitov * BPF_F_ALLOW_MULTI flag. They are executed in FIFO order 212324bda9eSAlexei Starovoitov * (those that were attached first, run first) 213324bda9eSAlexei Starovoitov * The programs of sub-cgroup are executed first, then programs of 214324bda9eSAlexei Starovoitov * this cgroup and then programs of parent cgroup. 215324bda9eSAlexei Starovoitov * When children program makes decision (like picking TCP CA or sock bind) 216324bda9eSAlexei Starovoitov * parent program has a chance to override it. 217324bda9eSAlexei Starovoitov * 218324bda9eSAlexei Starovoitov * A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups. 219324bda9eSAlexei Starovoitov * A cgroup with NONE doesn't allow any programs in sub-cgroups. 220324bda9eSAlexei Starovoitov * Ex1: 221324bda9eSAlexei Starovoitov * cgrp1 (MULTI progs A, B) -> 222324bda9eSAlexei Starovoitov * cgrp2 (OVERRIDE prog C) -> 223324bda9eSAlexei Starovoitov * cgrp3 (MULTI prog D) -> 224324bda9eSAlexei Starovoitov * cgrp4 (OVERRIDE prog E) -> 225324bda9eSAlexei Starovoitov * cgrp5 (NONE prog F) 226324bda9eSAlexei Starovoitov * the event in cgrp5 triggers execution of F,D,A,B in that order. 227324bda9eSAlexei Starovoitov * if prog F is detached, the execution is E,D,A,B 228324bda9eSAlexei Starovoitov * if prog F and D are detached, the execution is E,A,B 229324bda9eSAlexei Starovoitov * if prog F, E and D are detached, the execution is C,A,B 230324bda9eSAlexei Starovoitov * 231324bda9eSAlexei Starovoitov * All eligible programs are executed regardless of return code from 232324bda9eSAlexei Starovoitov * earlier programs. 2337f677633SAlexei Starovoitov */ 2347f677633SAlexei Starovoitov #define BPF_F_ALLOW_OVERRIDE (1U << 0) 235324bda9eSAlexei Starovoitov #define BPF_F_ALLOW_MULTI (1U << 1) 2367f677633SAlexei Starovoitov 237e07b98d9SDavid S. Miller /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the 238e07b98d9SDavid S. Miller * verifier will perform strict alignment checking as if the kernel 239e07b98d9SDavid S. Miller * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set, 240e07b98d9SDavid S. Miller * and NET_IP_ALIGN defined to 2. 241e07b98d9SDavid S. Miller */ 242e07b98d9SDavid S. Miller #define BPF_F_STRICT_ALIGNMENT (1U << 0) 243e07b98d9SDavid S. Miller 244e9ee9efcSDavid Miller /* If BPF_F_ANY_ALIGNMENT is used in BPF_PROF_LOAD command, the 245e9ee9efcSDavid Miller * verifier will allow any alignment whatsoever. On platforms 246e9ee9efcSDavid Miller * with strict alignment requirements for loads ands stores (such 247e9ee9efcSDavid Miller * as sparc and mips) the verifier validates that all loads and 248e9ee9efcSDavid Miller * stores provably follow this requirement. This flag turns that 249e9ee9efcSDavid Miller * checking and enforcement off. 250e9ee9efcSDavid Miller * 251e9ee9efcSDavid Miller * It is mostly used for testing when we want to validate the 252e9ee9efcSDavid Miller * context and memory access aspects of the verifier, but because 253e9ee9efcSDavid Miller * of an unaligned access the alignment check would trigger before 254e9ee9efcSDavid Miller * the one we are interested in. 255e9ee9efcSDavid Miller */ 256e9ee9efcSDavid Miller #define BPF_F_ANY_ALIGNMENT (1U << 1) 257e9ee9efcSDavid Miller 258cc8b0b92SAlexei Starovoitov /* when bpf_ldimm64->src_reg == BPF_PSEUDO_MAP_FD, bpf_ldimm64->imm == fd */ 259f1a66f85SDaniel Borkmann #define BPF_PSEUDO_MAP_FD 1 260f1a66f85SDaniel Borkmann 261cc8b0b92SAlexei Starovoitov /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative 262cc8b0b92SAlexei Starovoitov * offset to another bpf function 263cc8b0b92SAlexei Starovoitov */ 264cc8b0b92SAlexei Starovoitov #define BPF_PSEUDO_CALL 1 265cc8b0b92SAlexei Starovoitov 2663274f520SAlexei Starovoitov /* flags for BPF_MAP_UPDATE_ELEM command */ 2673274f520SAlexei Starovoitov #define BPF_ANY 0 /* create new element or update existing */ 2683274f520SAlexei Starovoitov #define BPF_NOEXIST 1 /* create new element if it didn't exist */ 2693274f520SAlexei Starovoitov #define BPF_EXIST 2 /* update existing element */ 2703274f520SAlexei Starovoitov 27196eabe7aSMartin KaFai Lau /* flags for BPF_MAP_CREATE command */ 2726c905981SAlexei Starovoitov #define BPF_F_NO_PREALLOC (1U << 0) 27329ba732aSMartin KaFai Lau /* Instead of having one common LRU list in the 2748f844938SMartin KaFai Lau * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list 27529ba732aSMartin KaFai Lau * which can scale and perform better. 27629ba732aSMartin KaFai Lau * Note, the LRU nodes (including free nodes) cannot be moved 27729ba732aSMartin KaFai Lau * across different LRU lists. 27829ba732aSMartin KaFai Lau */ 27929ba732aSMartin KaFai Lau #define BPF_F_NO_COMMON_LRU (1U << 1) 28096eabe7aSMartin KaFai Lau /* Specify numa node during map creation */ 28196eabe7aSMartin KaFai Lau #define BPF_F_NUMA_NODE (1U << 2) 2826c905981SAlexei Starovoitov 283cb4d2b3fSMartin KaFai Lau #define BPF_OBJ_NAME_LEN 16U 284cb4d2b3fSMartin KaFai Lau 2856e71b04aSChenbo Feng /* Flags for accessing BPF object */ 2866e71b04aSChenbo Feng #define BPF_F_RDONLY (1U << 3) 2876e71b04aSChenbo Feng #define BPF_F_WRONLY (1U << 4) 2886e71b04aSChenbo Feng 289615755a7SSong Liu /* Flag for stack_map, store build_id+offset instead of pointer */ 290615755a7SSong Liu #define BPF_F_STACK_BUILD_ID (1U << 5) 291615755a7SSong Liu 29296b3b6c9SLorenz Bauer /* Zero-initialize hash function seed. This should only be used for testing. */ 29396b3b6c9SLorenz Bauer #define BPF_F_ZERO_SEED (1U << 6) 29496b3b6c9SLorenz Bauer 2952f183360SLorenz Bauer /* flags for BPF_PROG_QUERY */ 2962f183360SLorenz Bauer #define BPF_F_QUERY_EFFECTIVE (1U << 0) 2972f183360SLorenz Bauer 298615755a7SSong Liu enum bpf_stack_build_id_status { 299615755a7SSong Liu /* user space need an empty entry to identify end of a trace */ 300615755a7SSong Liu BPF_STACK_BUILD_ID_EMPTY = 0, 301615755a7SSong Liu /* with valid build_id and offset */ 302615755a7SSong Liu BPF_STACK_BUILD_ID_VALID = 1, 303615755a7SSong Liu /* couldn't get build_id, fallback to ip */ 304615755a7SSong Liu BPF_STACK_BUILD_ID_IP = 2, 305615755a7SSong Liu }; 306615755a7SSong Liu 307615755a7SSong Liu #define BPF_BUILD_ID_SIZE 20 308615755a7SSong Liu struct bpf_stack_build_id { 309615755a7SSong Liu __s32 status; 310615755a7SSong Liu unsigned char build_id[BPF_BUILD_ID_SIZE]; 311615755a7SSong Liu union { 312615755a7SSong Liu __u64 offset; 313615755a7SSong Liu __u64 ip; 314615755a7SSong Liu }; 315615755a7SSong Liu }; 316615755a7SSong Liu 31799c55f7dSAlexei Starovoitov union bpf_attr { 31899c55f7dSAlexei Starovoitov struct { /* anonymous struct used by BPF_MAP_CREATE command */ 31999c55f7dSAlexei Starovoitov __u32 map_type; /* one of enum bpf_map_type */ 32099c55f7dSAlexei Starovoitov __u32 key_size; /* size of key in bytes */ 32199c55f7dSAlexei Starovoitov __u32 value_size; /* size of value in bytes */ 32299c55f7dSAlexei Starovoitov __u32 max_entries; /* max number of entries in a map */ 32396eabe7aSMartin KaFai Lau __u32 map_flags; /* BPF_MAP_CREATE related 32496eabe7aSMartin KaFai Lau * flags defined above. 32596eabe7aSMartin KaFai Lau */ 32656f668dfSMartin KaFai Lau __u32 inner_map_fd; /* fd pointing to the inner map */ 32796eabe7aSMartin KaFai Lau __u32 numa_node; /* numa node (effective only if 32896eabe7aSMartin KaFai Lau * BPF_F_NUMA_NODE is set). 32996eabe7aSMartin KaFai Lau */ 330067cae47SMartin KaFai Lau char map_name[BPF_OBJ_NAME_LEN]; 331a3884572SJakub Kicinski __u32 map_ifindex; /* ifindex of netdev to create on */ 332a26ca7c9SMartin KaFai Lau __u32 btf_fd; /* fd pointing to a BTF type data */ 3339b2cf328SMartin KaFai Lau __u32 btf_key_type_id; /* BTF type_id of the key */ 3349b2cf328SMartin KaFai Lau __u32 btf_value_type_id; /* BTF type_id of the value */ 33599c55f7dSAlexei Starovoitov }; 336db20fd2bSAlexei Starovoitov 337db20fd2bSAlexei Starovoitov struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ 338db20fd2bSAlexei Starovoitov __u32 map_fd; 339db20fd2bSAlexei Starovoitov __aligned_u64 key; 340db20fd2bSAlexei Starovoitov union { 341db20fd2bSAlexei Starovoitov __aligned_u64 value; 342db20fd2bSAlexei Starovoitov __aligned_u64 next_key; 343db20fd2bSAlexei Starovoitov }; 3443274f520SAlexei Starovoitov __u64 flags; 345db20fd2bSAlexei Starovoitov }; 34609756af4SAlexei Starovoitov 34709756af4SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_LOAD command */ 34809756af4SAlexei Starovoitov __u32 prog_type; /* one of enum bpf_prog_type */ 34909756af4SAlexei Starovoitov __u32 insn_cnt; 35009756af4SAlexei Starovoitov __aligned_u64 insns; 35109756af4SAlexei Starovoitov __aligned_u64 license; 352cbd35700SAlexei Starovoitov __u32 log_level; /* verbosity level of verifier */ 353cbd35700SAlexei Starovoitov __u32 log_size; /* size of user buffer */ 354cbd35700SAlexei Starovoitov __aligned_u64 log_buf; /* user supplied buffer */ 3556c4fc209SDaniel Borkmann __u32 kern_version; /* not used */ 356e07b98d9SDavid S. Miller __u32 prog_flags; 357067cae47SMartin KaFai Lau char prog_name[BPF_OBJ_NAME_LEN]; 3581f6f4cb7SJakub Kicinski __u32 prog_ifindex; /* ifindex of netdev to prep for */ 3595e43f899SAndrey Ignatov /* For some prog types expected attach type must be known at 3605e43f899SAndrey Ignatov * load time to verify attach type specific parts of prog 3615e43f899SAndrey Ignatov * (context accesses, allowed helpers, etc). 3625e43f899SAndrey Ignatov */ 3635e43f899SAndrey Ignatov __u32 expected_attach_type; 364838e9690SYonghong Song __u32 prog_btf_fd; /* fd pointing to BTF type data */ 365838e9690SYonghong Song __u32 func_info_rec_size; /* userspace bpf_func_info size */ 366838e9690SYonghong Song __aligned_u64 func_info; /* func info */ 367838e9690SYonghong Song __u32 func_info_cnt; /* number of bpf_func_info records */ 368c454a46bSMartin KaFai Lau __u32 line_info_rec_size; /* userspace bpf_line_info size */ 369c454a46bSMartin KaFai Lau __aligned_u64 line_info; /* line info */ 370c454a46bSMartin KaFai Lau __u32 line_info_cnt; /* number of bpf_line_info records */ 37109756af4SAlexei Starovoitov }; 372b2197755SDaniel Borkmann 373b2197755SDaniel Borkmann struct { /* anonymous struct used by BPF_OBJ_* commands */ 374b2197755SDaniel Borkmann __aligned_u64 pathname; 375b2197755SDaniel Borkmann __u32 bpf_fd; 3766e71b04aSChenbo Feng __u32 file_flags; 377b2197755SDaniel Borkmann }; 378f4324551SDaniel Mack 379f4324551SDaniel Mack struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ 380f4324551SDaniel Mack __u32 target_fd; /* container object to attach to */ 381f4324551SDaniel Mack __u32 attach_bpf_fd; /* eBPF program to attach */ 382f4324551SDaniel Mack __u32 attach_type; 3837f677633SAlexei Starovoitov __u32 attach_flags; 384f4324551SDaniel Mack }; 3851cf1cae9SAlexei Starovoitov 3861cf1cae9SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */ 3871cf1cae9SAlexei Starovoitov __u32 prog_fd; 3881cf1cae9SAlexei Starovoitov __u32 retval; 389b5a36b1eSLorenz Bauer __u32 data_size_in; /* input: len of data_in */ 390b5a36b1eSLorenz Bauer __u32 data_size_out; /* input/output: len of data_out 391b5a36b1eSLorenz Bauer * returns ENOSPC if data_out 392b5a36b1eSLorenz Bauer * is too small. 393b5a36b1eSLorenz Bauer */ 3941cf1cae9SAlexei Starovoitov __aligned_u64 data_in; 3951cf1cae9SAlexei Starovoitov __aligned_u64 data_out; 3961cf1cae9SAlexei Starovoitov __u32 repeat; 3971cf1cae9SAlexei Starovoitov __u32 duration; 3981cf1cae9SAlexei Starovoitov } test; 39934ad5580SMartin KaFai Lau 400b16d9aa4SMartin KaFai Lau struct { /* anonymous struct used by BPF_*_GET_*_ID */ 401b16d9aa4SMartin KaFai Lau union { 40234ad5580SMartin KaFai Lau __u32 start_id; 403b16d9aa4SMartin KaFai Lau __u32 prog_id; 404bd5f5f4eSMartin KaFai Lau __u32 map_id; 40578958fcaSMartin KaFai Lau __u32 btf_id; 406b16d9aa4SMartin KaFai Lau }; 40734ad5580SMartin KaFai Lau __u32 next_id; 4086e71b04aSChenbo Feng __u32 open_flags; 40934ad5580SMartin KaFai Lau }; 4101e270976SMartin KaFai Lau 4111e270976SMartin KaFai Lau struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ 4121e270976SMartin KaFai Lau __u32 bpf_fd; 4131e270976SMartin KaFai Lau __u32 info_len; 4141e270976SMartin KaFai Lau __aligned_u64 info; 4151e270976SMartin KaFai Lau } info; 416468e2f64SAlexei Starovoitov 417468e2f64SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_QUERY command */ 418468e2f64SAlexei Starovoitov __u32 target_fd; /* container object to query */ 419468e2f64SAlexei Starovoitov __u32 attach_type; 420468e2f64SAlexei Starovoitov __u32 query_flags; 421468e2f64SAlexei Starovoitov __u32 attach_flags; 422468e2f64SAlexei Starovoitov __aligned_u64 prog_ids; 423468e2f64SAlexei Starovoitov __u32 prog_cnt; 424468e2f64SAlexei Starovoitov } query; 425c4f6699dSAlexei Starovoitov 426c4f6699dSAlexei Starovoitov struct { 427c4f6699dSAlexei Starovoitov __u64 name; 428c4f6699dSAlexei Starovoitov __u32 prog_fd; 429c4f6699dSAlexei Starovoitov } raw_tracepoint; 430f56a653cSMartin KaFai Lau 431f56a653cSMartin KaFai Lau struct { /* anonymous struct for BPF_BTF_LOAD */ 432f56a653cSMartin KaFai Lau __aligned_u64 btf; 433f56a653cSMartin KaFai Lau __aligned_u64 btf_log_buf; 434f56a653cSMartin KaFai Lau __u32 btf_size; 435f56a653cSMartin KaFai Lau __u32 btf_log_size; 436f56a653cSMartin KaFai Lau __u32 btf_log_level; 437f56a653cSMartin KaFai Lau }; 43841bdc4b4SYonghong Song 43941bdc4b4SYonghong Song struct { 44041bdc4b4SYonghong Song __u32 pid; /* input: pid */ 44141bdc4b4SYonghong Song __u32 fd; /* input: fd */ 44241bdc4b4SYonghong Song __u32 flags; /* input: flags */ 44341bdc4b4SYonghong Song __u32 buf_len; /* input/output: buf len */ 44441bdc4b4SYonghong Song __aligned_u64 buf; /* input/output: 44541bdc4b4SYonghong Song * tp_name for tracepoint 44641bdc4b4SYonghong Song * symbol for kprobe 44741bdc4b4SYonghong Song * filename for uprobe 44841bdc4b4SYonghong Song */ 44941bdc4b4SYonghong Song __u32 prog_id; /* output: prod_id */ 45041bdc4b4SYonghong Song __u32 fd_type; /* output: BPF_FD_TYPE_* */ 45141bdc4b4SYonghong Song __u64 probe_offset; /* output: probe_offset */ 45241bdc4b4SYonghong Song __u64 probe_addr; /* output: probe_addr */ 45341bdc4b4SYonghong Song } task_fd_query; 45499c55f7dSAlexei Starovoitov } __attribute__((aligned(8))); 45599c55f7dSAlexei Starovoitov 45656a092c8SQuentin Monnet /* The description below is an attempt at providing documentation to eBPF 45756a092c8SQuentin Monnet * developers about the multiple available eBPF helper functions. It can be 45856a092c8SQuentin Monnet * parsed and used to produce a manual page. The workflow is the following, 45956a092c8SQuentin Monnet * and requires the rst2man utility: 460ebb676daSThomas Graf * 46156a092c8SQuentin Monnet * $ ./scripts/bpf_helpers_doc.py \ 46256a092c8SQuentin Monnet * --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst 46356a092c8SQuentin Monnet * $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7 46456a092c8SQuentin Monnet * $ man /tmp/bpf-helpers.7 465ebb676daSThomas Graf * 46656a092c8SQuentin Monnet * Note that in order to produce this external documentation, some RST 46756a092c8SQuentin Monnet * formatting is used in the descriptions to get "bold" and "italics" in 46856a092c8SQuentin Monnet * manual pages. Also note that the few trailing white spaces are 46956a092c8SQuentin Monnet * intentional, removing them would break paragraphs for rst2man. 470ebb676daSThomas Graf * 47156a092c8SQuentin Monnet * Start of BPF helper function descriptions: 472ad4a5223SQuentin Monnet * 473ad4a5223SQuentin Monnet * void *bpf_map_lookup_elem(struct bpf_map *map, const void *key) 474ad4a5223SQuentin Monnet * Description 475ad4a5223SQuentin Monnet * Perform a lookup in *map* for an entry associated to *key*. 476ad4a5223SQuentin Monnet * Return 477ad4a5223SQuentin Monnet * Map value associated to *key*, or **NULL** if no entry was 478ad4a5223SQuentin Monnet * found. 479ad4a5223SQuentin Monnet * 480ad4a5223SQuentin Monnet * int bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags) 481ad4a5223SQuentin Monnet * Description 482ad4a5223SQuentin Monnet * Add or update the value of the entry associated to *key* in 483ad4a5223SQuentin Monnet * *map* with *value*. *flags* is one of: 484ad4a5223SQuentin Monnet * 485ad4a5223SQuentin Monnet * **BPF_NOEXIST** 486ad4a5223SQuentin Monnet * The entry for *key* must not exist in the map. 487ad4a5223SQuentin Monnet * **BPF_EXIST** 488ad4a5223SQuentin Monnet * The entry for *key* must already exist in the map. 489ad4a5223SQuentin Monnet * **BPF_ANY** 490ad4a5223SQuentin Monnet * No condition on the existence of the entry for *key*. 491ad4a5223SQuentin Monnet * 492ad4a5223SQuentin Monnet * Flag value **BPF_NOEXIST** cannot be used for maps of types 493ad4a5223SQuentin Monnet * **BPF_MAP_TYPE_ARRAY** or **BPF_MAP_TYPE_PERCPU_ARRAY** (all 494ad4a5223SQuentin Monnet * elements always exist), the helper would return an error. 495ad4a5223SQuentin Monnet * Return 496ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 497ad4a5223SQuentin Monnet * 498ad4a5223SQuentin Monnet * int bpf_map_delete_elem(struct bpf_map *map, const void *key) 499ad4a5223SQuentin Monnet * Description 500ad4a5223SQuentin Monnet * Delete entry with *key* from *map*. 501ad4a5223SQuentin Monnet * Return 502ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 503ad4a5223SQuentin Monnet * 504f1a2e44aSMauricio Vasquez B * int bpf_map_push_elem(struct bpf_map *map, const void *value, u64 flags) 505f1a2e44aSMauricio Vasquez B * Description 506f1a2e44aSMauricio Vasquez B * Push an element *value* in *map*. *flags* is one of: 507f1a2e44aSMauricio Vasquez B * 508f1a2e44aSMauricio Vasquez B * **BPF_EXIST** 509f1a2e44aSMauricio Vasquez B * If the queue/stack is full, the oldest element is removed to 510f1a2e44aSMauricio Vasquez B * make room for this. 511f1a2e44aSMauricio Vasquez B * Return 512f1a2e44aSMauricio Vasquez B * 0 on success, or a negative error in case of failure. 513f1a2e44aSMauricio Vasquez B * 514ad4a5223SQuentin Monnet * int bpf_probe_read(void *dst, u32 size, const void *src) 515ad4a5223SQuentin Monnet * Description 516ad4a5223SQuentin Monnet * For tracing programs, safely attempt to read *size* bytes from 517ad4a5223SQuentin Monnet * address *src* and store the data in *dst*. 518ad4a5223SQuentin Monnet * Return 519ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 520ad4a5223SQuentin Monnet * 521ad4a5223SQuentin Monnet * u64 bpf_ktime_get_ns(void) 522ad4a5223SQuentin Monnet * Description 523ad4a5223SQuentin Monnet * Return the time elapsed since system boot, in nanoseconds. 524ad4a5223SQuentin Monnet * Return 525ad4a5223SQuentin Monnet * Current *ktime*. 526ad4a5223SQuentin Monnet * 527ad4a5223SQuentin Monnet * int bpf_trace_printk(const char *fmt, u32 fmt_size, ...) 528ad4a5223SQuentin Monnet * Description 529ad4a5223SQuentin Monnet * This helper is a "printk()-like" facility for debugging. It 530ad4a5223SQuentin Monnet * prints a message defined by format *fmt* (of size *fmt_size*) 531ad4a5223SQuentin Monnet * to file *\/sys/kernel/debug/tracing/trace* from DebugFS, if 532ad4a5223SQuentin Monnet * available. It can take up to three additional **u64** 533ad4a5223SQuentin Monnet * arguments (as an eBPF helpers, the total number of arguments is 534ad4a5223SQuentin Monnet * limited to five). 535ad4a5223SQuentin Monnet * 536ad4a5223SQuentin Monnet * Each time the helper is called, it appends a line to the trace. 537ad4a5223SQuentin Monnet * The format of the trace is customizable, and the exact output 538ad4a5223SQuentin Monnet * one will get depends on the options set in 539ad4a5223SQuentin Monnet * *\/sys/kernel/debug/tracing/trace_options* (see also the 540ad4a5223SQuentin Monnet * *README* file under the same directory). However, it usually 541ad4a5223SQuentin Monnet * defaults to something like: 542ad4a5223SQuentin Monnet * 543ad4a5223SQuentin Monnet * :: 544ad4a5223SQuentin Monnet * 545ad4a5223SQuentin Monnet * telnet-470 [001] .N.. 419421.045894: 0x00000001: <formatted msg> 546ad4a5223SQuentin Monnet * 547ad4a5223SQuentin Monnet * In the above: 548ad4a5223SQuentin Monnet * 549ad4a5223SQuentin Monnet * * ``telnet`` is the name of the current task. 550ad4a5223SQuentin Monnet * * ``470`` is the PID of the current task. 551ad4a5223SQuentin Monnet * * ``001`` is the CPU number on which the task is 552ad4a5223SQuentin Monnet * running. 553ad4a5223SQuentin Monnet * * In ``.N..``, each character refers to a set of 554ad4a5223SQuentin Monnet * options (whether irqs are enabled, scheduling 555ad4a5223SQuentin Monnet * options, whether hard/softirqs are running, level of 556ad4a5223SQuentin Monnet * preempt_disabled respectively). **N** means that 557ad4a5223SQuentin Monnet * **TIF_NEED_RESCHED** and **PREEMPT_NEED_RESCHED** 558ad4a5223SQuentin Monnet * are set. 559ad4a5223SQuentin Monnet * * ``419421.045894`` is a timestamp. 560ad4a5223SQuentin Monnet * * ``0x00000001`` is a fake value used by BPF for the 561ad4a5223SQuentin Monnet * instruction pointer register. 562ad4a5223SQuentin Monnet * * ``<formatted msg>`` is the message formatted with 563ad4a5223SQuentin Monnet * *fmt*. 564ad4a5223SQuentin Monnet * 565ad4a5223SQuentin Monnet * The conversion specifiers supported by *fmt* are similar, but 566ad4a5223SQuentin Monnet * more limited than for printk(). They are **%d**, **%i**, 567ad4a5223SQuentin Monnet * **%u**, **%x**, **%ld**, **%li**, **%lu**, **%lx**, **%lld**, 568ad4a5223SQuentin Monnet * **%lli**, **%llu**, **%llx**, **%p**, **%s**. No modifier (size 569ad4a5223SQuentin Monnet * of field, padding with zeroes, etc.) is available, and the 570ad4a5223SQuentin Monnet * helper will return **-EINVAL** (but print nothing) if it 571ad4a5223SQuentin Monnet * encounters an unknown specifier. 572ad4a5223SQuentin Monnet * 573ad4a5223SQuentin Monnet * Also, note that **bpf_trace_printk**\ () is slow, and should 574ad4a5223SQuentin Monnet * only be used for debugging purposes. For this reason, a notice 575ad4a5223SQuentin Monnet * bloc (spanning several lines) is printed to kernel logs and 576ad4a5223SQuentin Monnet * states that the helper should not be used "for production use" 577ad4a5223SQuentin Monnet * the first time this helper is used (or more precisely, when 578ad4a5223SQuentin Monnet * **trace_printk**\ () buffers are allocated). For passing values 579ad4a5223SQuentin Monnet * to user space, perf events should be preferred. 580ad4a5223SQuentin Monnet * Return 581ad4a5223SQuentin Monnet * The number of bytes written to the buffer, or a negative error 582ad4a5223SQuentin Monnet * in case of failure. 583ad4a5223SQuentin Monnet * 5841fdd08beSQuentin Monnet * u32 bpf_get_prandom_u32(void) 5851fdd08beSQuentin Monnet * Description 5861fdd08beSQuentin Monnet * Get a pseudo-random number. 5871fdd08beSQuentin Monnet * 5881fdd08beSQuentin Monnet * From a security point of view, this helper uses its own 5891fdd08beSQuentin Monnet * pseudo-random internal state, and cannot be used to infer the 5901fdd08beSQuentin Monnet * seed of other random functions in the kernel. However, it is 5911fdd08beSQuentin Monnet * essential to note that the generator used by the helper is not 5921fdd08beSQuentin Monnet * cryptographically secure. 5931fdd08beSQuentin Monnet * Return 5941fdd08beSQuentin Monnet * A random 32-bit unsigned value. 5951fdd08beSQuentin Monnet * 5961fdd08beSQuentin Monnet * u32 bpf_get_smp_processor_id(void) 5971fdd08beSQuentin Monnet * Description 5981fdd08beSQuentin Monnet * Get the SMP (symmetric multiprocessing) processor id. Note that 5991fdd08beSQuentin Monnet * all programs run with preemption disabled, which means that the 6001fdd08beSQuentin Monnet * SMP processor id is stable during all the execution of the 6011fdd08beSQuentin Monnet * program. 6021fdd08beSQuentin Monnet * Return 6031fdd08beSQuentin Monnet * The SMP id of the processor running the program. 6041fdd08beSQuentin Monnet * 605ad4a5223SQuentin Monnet * int bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags) 606ad4a5223SQuentin Monnet * Description 607ad4a5223SQuentin Monnet * Store *len* bytes from address *from* into the packet 608ad4a5223SQuentin Monnet * associated to *skb*, at *offset*. *flags* are a combination of 609ad4a5223SQuentin Monnet * **BPF_F_RECOMPUTE_CSUM** (automatically recompute the 610ad4a5223SQuentin Monnet * checksum for the packet after storing the bytes) and 611ad4a5223SQuentin Monnet * **BPF_F_INVALIDATE_HASH** (set *skb*\ **->hash**, *skb*\ 612ad4a5223SQuentin Monnet * **->swhash** and *skb*\ **->l4hash** to 0). 613ad4a5223SQuentin Monnet * 614ad4a5223SQuentin Monnet * A call to this helper is susceptible to change the underlaying 615ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 616ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 617ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 618ad4a5223SQuentin Monnet * direct packet access. 619ad4a5223SQuentin Monnet * Return 620ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 621ad4a5223SQuentin Monnet * 622ad4a5223SQuentin Monnet * int bpf_l3_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 size) 623ad4a5223SQuentin Monnet * Description 624ad4a5223SQuentin Monnet * Recompute the layer 3 (e.g. IP) checksum for the packet 625ad4a5223SQuentin Monnet * associated to *skb*. Computation is incremental, so the helper 626ad4a5223SQuentin Monnet * must know the former value of the header field that was 627ad4a5223SQuentin Monnet * modified (*from*), the new value of this field (*to*), and the 628ad4a5223SQuentin Monnet * number of bytes (2 or 4) for this field, stored in *size*. 629ad4a5223SQuentin Monnet * Alternatively, it is possible to store the difference between 630ad4a5223SQuentin Monnet * the previous and the new values of the header field in *to*, by 631ad4a5223SQuentin Monnet * setting *from* and *size* to 0. For both methods, *offset* 632ad4a5223SQuentin Monnet * indicates the location of the IP checksum within the packet. 633ad4a5223SQuentin Monnet * 634ad4a5223SQuentin Monnet * This helper works in combination with **bpf_csum_diff**\ (), 635ad4a5223SQuentin Monnet * which does not update the checksum in-place, but offers more 636ad4a5223SQuentin Monnet * flexibility and can handle sizes larger than 2 or 4 for the 637ad4a5223SQuentin Monnet * checksum to update. 638ad4a5223SQuentin Monnet * 639ad4a5223SQuentin Monnet * A call to this helper is susceptible to change the underlaying 640ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 641ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 642ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 643ad4a5223SQuentin Monnet * direct packet access. 644ad4a5223SQuentin Monnet * Return 645ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 646ad4a5223SQuentin Monnet * 647ad4a5223SQuentin Monnet * int bpf_l4_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 flags) 648ad4a5223SQuentin Monnet * Description 649ad4a5223SQuentin Monnet * Recompute the layer 4 (e.g. TCP, UDP or ICMP) checksum for the 650ad4a5223SQuentin Monnet * packet associated to *skb*. Computation is incremental, so the 651ad4a5223SQuentin Monnet * helper must know the former value of the header field that was 652ad4a5223SQuentin Monnet * modified (*from*), the new value of this field (*to*), and the 653ad4a5223SQuentin Monnet * number of bytes (2 or 4) for this field, stored on the lowest 654ad4a5223SQuentin Monnet * four bits of *flags*. Alternatively, it is possible to store 655ad4a5223SQuentin Monnet * the difference between the previous and the new values of the 656ad4a5223SQuentin Monnet * header field in *to*, by setting *from* and the four lowest 657ad4a5223SQuentin Monnet * bits of *flags* to 0. For both methods, *offset* indicates the 658ad4a5223SQuentin Monnet * location of the IP checksum within the packet. In addition to 659ad4a5223SQuentin Monnet * the size of the field, *flags* can be added (bitwise OR) actual 660ad4a5223SQuentin Monnet * flags. With **BPF_F_MARK_MANGLED_0**, a null checksum is left 661ad4a5223SQuentin Monnet * untouched (unless **BPF_F_MARK_ENFORCE** is added as well), and 662ad4a5223SQuentin Monnet * for updates resulting in a null checksum the value is set to 663ad4a5223SQuentin Monnet * **CSUM_MANGLED_0** instead. Flag **BPF_F_PSEUDO_HDR** indicates 664ad4a5223SQuentin Monnet * the checksum is to be computed against a pseudo-header. 665ad4a5223SQuentin Monnet * 666ad4a5223SQuentin Monnet * This helper works in combination with **bpf_csum_diff**\ (), 667ad4a5223SQuentin Monnet * which does not update the checksum in-place, but offers more 668ad4a5223SQuentin Monnet * flexibility and can handle sizes larger than 2 or 4 for the 669ad4a5223SQuentin Monnet * checksum to update. 670ad4a5223SQuentin Monnet * 671ad4a5223SQuentin Monnet * A call to this helper is susceptible to change the underlaying 672ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 673ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 674ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 675ad4a5223SQuentin Monnet * direct packet access. 676ad4a5223SQuentin Monnet * Return 677ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 678ad4a5223SQuentin Monnet * 679ad4a5223SQuentin Monnet * int bpf_tail_call(void *ctx, struct bpf_map *prog_array_map, u32 index) 680ad4a5223SQuentin Monnet * Description 681ad4a5223SQuentin Monnet * This special helper is used to trigger a "tail call", or in 682ad4a5223SQuentin Monnet * other words, to jump into another eBPF program. The same stack 683ad4a5223SQuentin Monnet * frame is used (but values on stack and in registers for the 684ad4a5223SQuentin Monnet * caller are not accessible to the callee). This mechanism allows 685ad4a5223SQuentin Monnet * for program chaining, either for raising the maximum number of 686ad4a5223SQuentin Monnet * available eBPF instructions, or to execute given programs in 687ad4a5223SQuentin Monnet * conditional blocks. For security reasons, there is an upper 688ad4a5223SQuentin Monnet * limit to the number of successive tail calls that can be 689ad4a5223SQuentin Monnet * performed. 690ad4a5223SQuentin Monnet * 691ad4a5223SQuentin Monnet * Upon call of this helper, the program attempts to jump into a 692ad4a5223SQuentin Monnet * program referenced at index *index* in *prog_array_map*, a 693ad4a5223SQuentin Monnet * special map of type **BPF_MAP_TYPE_PROG_ARRAY**, and passes 694ad4a5223SQuentin Monnet * *ctx*, a pointer to the context. 695ad4a5223SQuentin Monnet * 696ad4a5223SQuentin Monnet * If the call succeeds, the kernel immediately runs the first 697ad4a5223SQuentin Monnet * instruction of the new program. This is not a function call, 698ad4a5223SQuentin Monnet * and it never returns to the previous program. If the call 699ad4a5223SQuentin Monnet * fails, then the helper has no effect, and the caller continues 700ad4a5223SQuentin Monnet * to run its subsequent instructions. A call can fail if the 701ad4a5223SQuentin Monnet * destination program for the jump does not exist (i.e. *index* 702ad4a5223SQuentin Monnet * is superior to the number of entries in *prog_array_map*), or 703ad4a5223SQuentin Monnet * if the maximum number of tail calls has been reached for this 704ad4a5223SQuentin Monnet * chain of programs. This limit is defined in the kernel by the 705ad4a5223SQuentin Monnet * macro **MAX_TAIL_CALL_CNT** (not accessible to user space), 706ad4a5223SQuentin Monnet * which is currently set to 32. 707ad4a5223SQuentin Monnet * Return 708ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 709ad4a5223SQuentin Monnet * 710ad4a5223SQuentin Monnet * int bpf_clone_redirect(struct sk_buff *skb, u32 ifindex, u64 flags) 711ad4a5223SQuentin Monnet * Description 712ad4a5223SQuentin Monnet * Clone and redirect the packet associated to *skb* to another 713ad4a5223SQuentin Monnet * net device of index *ifindex*. Both ingress and egress 714ad4a5223SQuentin Monnet * interfaces can be used for redirection. The **BPF_F_INGRESS** 715ad4a5223SQuentin Monnet * value in *flags* is used to make the distinction (ingress path 716ad4a5223SQuentin Monnet * is selected if the flag is present, egress path otherwise). 717ad4a5223SQuentin Monnet * This is the only flag supported for now. 718ad4a5223SQuentin Monnet * 719ad4a5223SQuentin Monnet * In comparison with **bpf_redirect**\ () helper, 720ad4a5223SQuentin Monnet * **bpf_clone_redirect**\ () has the associated cost of 721ad4a5223SQuentin Monnet * duplicating the packet buffer, but this can be executed out of 722ad4a5223SQuentin Monnet * the eBPF program. Conversely, **bpf_redirect**\ () is more 723ad4a5223SQuentin Monnet * efficient, but it is handled through an action code where the 724ad4a5223SQuentin Monnet * redirection happens only after the eBPF program has returned. 725ad4a5223SQuentin Monnet * 726ad4a5223SQuentin Monnet * A call to this helper is susceptible to change the underlaying 727ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 728ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 729ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 730ad4a5223SQuentin Monnet * direct packet access. 731ad4a5223SQuentin Monnet * Return 732ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 733c456dec4SQuentin Monnet * 734c456dec4SQuentin Monnet * u64 bpf_get_current_pid_tgid(void) 735c456dec4SQuentin Monnet * Return 736c456dec4SQuentin Monnet * A 64-bit integer containing the current tgid and pid, and 737c456dec4SQuentin Monnet * created as such: 738c456dec4SQuentin Monnet * *current_task*\ **->tgid << 32 \|** 739c456dec4SQuentin Monnet * *current_task*\ **->pid**. 740c456dec4SQuentin Monnet * 741c456dec4SQuentin Monnet * u64 bpf_get_current_uid_gid(void) 742c456dec4SQuentin Monnet * Return 743c456dec4SQuentin Monnet * A 64-bit integer containing the current GID and UID, and 744c456dec4SQuentin Monnet * created as such: *current_gid* **<< 32 \|** *current_uid*. 745c456dec4SQuentin Monnet * 746c456dec4SQuentin Monnet * int bpf_get_current_comm(char *buf, u32 size_of_buf) 747c456dec4SQuentin Monnet * Description 748c456dec4SQuentin Monnet * Copy the **comm** attribute of the current task into *buf* of 749c456dec4SQuentin Monnet * *size_of_buf*. The **comm** attribute contains the name of 750c456dec4SQuentin Monnet * the executable (excluding the path) for the current task. The 751c456dec4SQuentin Monnet * *size_of_buf* must be strictly positive. On success, the 752c456dec4SQuentin Monnet * helper makes sure that the *buf* is NUL-terminated. On failure, 753c456dec4SQuentin Monnet * it is filled with zeroes. 754c456dec4SQuentin Monnet * Return 755c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 756c456dec4SQuentin Monnet * 7571fdd08beSQuentin Monnet * u32 bpf_get_cgroup_classid(struct sk_buff *skb) 7581fdd08beSQuentin Monnet * Description 7591fdd08beSQuentin Monnet * Retrieve the classid for the current task, i.e. for the net_cls 7601fdd08beSQuentin Monnet * cgroup to which *skb* belongs. 7611fdd08beSQuentin Monnet * 7621fdd08beSQuentin Monnet * This helper can be used on TC egress path, but not on ingress. 7631fdd08beSQuentin Monnet * 7641fdd08beSQuentin Monnet * The net_cls cgroup provides an interface to tag network packets 7651fdd08beSQuentin Monnet * based on a user-provided identifier for all traffic coming from 7661fdd08beSQuentin Monnet * the tasks belonging to the related cgroup. See also the related 7671fdd08beSQuentin Monnet * kernel documentation, available from the Linux sources in file 7681fdd08beSQuentin Monnet * *Documentation/cgroup-v1/net_cls.txt*. 7691fdd08beSQuentin Monnet * 7701fdd08beSQuentin Monnet * The Linux kernel has two versions for cgroups: there are 7711fdd08beSQuentin Monnet * cgroups v1 and cgroups v2. Both are available to users, who can 7721fdd08beSQuentin Monnet * use a mixture of them, but note that the net_cls cgroup is for 7731fdd08beSQuentin Monnet * cgroup v1 only. This makes it incompatible with BPF programs 7741fdd08beSQuentin Monnet * run on cgroups, which is a cgroup-v2-only feature (a socket can 7751fdd08beSQuentin Monnet * only hold data for one version of cgroups at a time). 7761fdd08beSQuentin Monnet * 7771fdd08beSQuentin Monnet * This helper is only available is the kernel was compiled with 7781fdd08beSQuentin Monnet * the **CONFIG_CGROUP_NET_CLASSID** configuration option set to 7791fdd08beSQuentin Monnet * "**y**" or to "**m**". 7801fdd08beSQuentin Monnet * Return 7811fdd08beSQuentin Monnet * The classid, or 0 for the default unconfigured classid. 7821fdd08beSQuentin Monnet * 783c456dec4SQuentin Monnet * int bpf_skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 784c456dec4SQuentin Monnet * Description 785c456dec4SQuentin Monnet * Push a *vlan_tci* (VLAN tag control information) of protocol 786c456dec4SQuentin Monnet * *vlan_proto* to the packet associated to *skb*, then update 787c456dec4SQuentin Monnet * the checksum. Note that if *vlan_proto* is different from 788c456dec4SQuentin Monnet * **ETH_P_8021Q** and **ETH_P_8021AD**, it is considered to 789c456dec4SQuentin Monnet * be **ETH_P_8021Q**. 790c456dec4SQuentin Monnet * 791c456dec4SQuentin Monnet * A call to this helper is susceptible to change the underlaying 792c456dec4SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 793c456dec4SQuentin Monnet * previously done by the verifier are invalidated and must be 794c456dec4SQuentin Monnet * performed again, if the helper is used in combination with 795c456dec4SQuentin Monnet * direct packet access. 796c456dec4SQuentin Monnet * Return 797c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 798c456dec4SQuentin Monnet * 799c456dec4SQuentin Monnet * int bpf_skb_vlan_pop(struct sk_buff *skb) 800c456dec4SQuentin Monnet * Description 801c456dec4SQuentin Monnet * Pop a VLAN header from the packet associated to *skb*. 802c456dec4SQuentin Monnet * 803c456dec4SQuentin Monnet * A call to this helper is susceptible to change the underlaying 804c456dec4SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 805c456dec4SQuentin Monnet * previously done by the verifier are invalidated and must be 806c456dec4SQuentin Monnet * performed again, if the helper is used in combination with 807c456dec4SQuentin Monnet * direct packet access. 808c456dec4SQuentin Monnet * Return 809c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 810c456dec4SQuentin Monnet * 811c456dec4SQuentin Monnet * int bpf_skb_get_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags) 812c456dec4SQuentin Monnet * Description 813c456dec4SQuentin Monnet * Get tunnel metadata. This helper takes a pointer *key* to an 814c456dec4SQuentin Monnet * empty **struct bpf_tunnel_key** of **size**, that will be 815c456dec4SQuentin Monnet * filled with tunnel metadata for the packet associated to *skb*. 816c456dec4SQuentin Monnet * The *flags* can be set to **BPF_F_TUNINFO_IPV6**, which 817c456dec4SQuentin Monnet * indicates that the tunnel is based on IPv6 protocol instead of 818c456dec4SQuentin Monnet * IPv4. 819c456dec4SQuentin Monnet * 820c456dec4SQuentin Monnet * The **struct bpf_tunnel_key** is an object that generalizes the 821c456dec4SQuentin Monnet * principal parameters used by various tunneling protocols into a 822c456dec4SQuentin Monnet * single struct. This way, it can be used to easily make a 823c456dec4SQuentin Monnet * decision based on the contents of the encapsulation header, 824c456dec4SQuentin Monnet * "summarized" in this struct. In particular, it holds the IP 825c456dec4SQuentin Monnet * address of the remote end (IPv4 or IPv6, depending on the case) 826c456dec4SQuentin Monnet * in *key*\ **->remote_ipv4** or *key*\ **->remote_ipv6**. Also, 827c456dec4SQuentin Monnet * this struct exposes the *key*\ **->tunnel_id**, which is 828c456dec4SQuentin Monnet * generally mapped to a VNI (Virtual Network Identifier), making 829c456dec4SQuentin Monnet * it programmable together with the **bpf_skb_set_tunnel_key**\ 830c456dec4SQuentin Monnet * () helper. 831c456dec4SQuentin Monnet * 832c456dec4SQuentin Monnet * Let's imagine that the following code is part of a program 833c456dec4SQuentin Monnet * attached to the TC ingress interface, on one end of a GRE 834c456dec4SQuentin Monnet * tunnel, and is supposed to filter out all messages coming from 835c456dec4SQuentin Monnet * remote ends with IPv4 address other than 10.0.0.1: 836c456dec4SQuentin Monnet * 837c456dec4SQuentin Monnet * :: 838c456dec4SQuentin Monnet * 839c456dec4SQuentin Monnet * int ret; 840c456dec4SQuentin Monnet * struct bpf_tunnel_key key = {}; 841c456dec4SQuentin Monnet * 842c456dec4SQuentin Monnet * ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0); 843c456dec4SQuentin Monnet * if (ret < 0) 844c456dec4SQuentin Monnet * return TC_ACT_SHOT; // drop packet 845c456dec4SQuentin Monnet * 846c456dec4SQuentin Monnet * if (key.remote_ipv4 != 0x0a000001) 847c456dec4SQuentin Monnet * return TC_ACT_SHOT; // drop packet 848c456dec4SQuentin Monnet * 849c456dec4SQuentin Monnet * return TC_ACT_OK; // accept packet 850c456dec4SQuentin Monnet * 851c456dec4SQuentin Monnet * This interface can also be used with all encapsulation devices 852c456dec4SQuentin Monnet * that can operate in "collect metadata" mode: instead of having 853c456dec4SQuentin Monnet * one network device per specific configuration, the "collect 854c456dec4SQuentin Monnet * metadata" mode only requires a single device where the 855c456dec4SQuentin Monnet * configuration can be extracted from this helper. 856c456dec4SQuentin Monnet * 857c456dec4SQuentin Monnet * This can be used together with various tunnels such as VXLan, 858c456dec4SQuentin Monnet * Geneve, GRE or IP in IP (IPIP). 859c456dec4SQuentin Monnet * Return 860c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 861c456dec4SQuentin Monnet * 862c456dec4SQuentin Monnet * int bpf_skb_set_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags) 863c456dec4SQuentin Monnet * Description 864c456dec4SQuentin Monnet * Populate tunnel metadata for packet associated to *skb.* The 865c456dec4SQuentin Monnet * tunnel metadata is set to the contents of *key*, of *size*. The 866c456dec4SQuentin Monnet * *flags* can be set to a combination of the following values: 867c456dec4SQuentin Monnet * 868c456dec4SQuentin Monnet * **BPF_F_TUNINFO_IPV6** 869c456dec4SQuentin Monnet * Indicate that the tunnel is based on IPv6 protocol 870c456dec4SQuentin Monnet * instead of IPv4. 871c456dec4SQuentin Monnet * **BPF_F_ZERO_CSUM_TX** 872c456dec4SQuentin Monnet * For IPv4 packets, add a flag to tunnel metadata 873c456dec4SQuentin Monnet * indicating that checksum computation should be skipped 874c456dec4SQuentin Monnet * and checksum set to zeroes. 875c456dec4SQuentin Monnet * **BPF_F_DONT_FRAGMENT** 876c456dec4SQuentin Monnet * Add a flag to tunnel metadata indicating that the 877c456dec4SQuentin Monnet * packet should not be fragmented. 878c456dec4SQuentin Monnet * **BPF_F_SEQ_NUMBER** 879c456dec4SQuentin Monnet * Add a flag to tunnel metadata indicating that a 880c456dec4SQuentin Monnet * sequence number should be added to tunnel header before 881c456dec4SQuentin Monnet * sending the packet. This flag was added for GRE 882c456dec4SQuentin Monnet * encapsulation, but might be used with other protocols 883c456dec4SQuentin Monnet * as well in the future. 884c456dec4SQuentin Monnet * 885c456dec4SQuentin Monnet * Here is a typical usage on the transmit path: 886c456dec4SQuentin Monnet * 887c456dec4SQuentin Monnet * :: 888c456dec4SQuentin Monnet * 889c456dec4SQuentin Monnet * struct bpf_tunnel_key key; 890c456dec4SQuentin Monnet * populate key ... 891c456dec4SQuentin Monnet * bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0); 892c456dec4SQuentin Monnet * bpf_clone_redirect(skb, vxlan_dev_ifindex, 0); 893c456dec4SQuentin Monnet * 894c456dec4SQuentin Monnet * See also the description of the **bpf_skb_get_tunnel_key**\ () 895c456dec4SQuentin Monnet * helper for additional information. 896c456dec4SQuentin Monnet * Return 897c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 898c456dec4SQuentin Monnet * 899c6b5fb86SQuentin Monnet * u64 bpf_perf_event_read(struct bpf_map *map, u64 flags) 900c6b5fb86SQuentin Monnet * Description 901c6b5fb86SQuentin Monnet * Read the value of a perf event counter. This helper relies on a 902c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of 903c6b5fb86SQuentin Monnet * the perf event counter is selected when *map* is updated with 904c6b5fb86SQuentin Monnet * perf event file descriptors. The *map* is an array whose size 905c6b5fb86SQuentin Monnet * is the number of available CPUs, and each cell contains a value 906c6b5fb86SQuentin Monnet * relative to one CPU. The value to retrieve is indicated by 907c6b5fb86SQuentin Monnet * *flags*, that contains the index of the CPU to look up, masked 908c6b5fb86SQuentin Monnet * with **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to 909c6b5fb86SQuentin Monnet * **BPF_F_CURRENT_CPU** to indicate that the value for the 910c6b5fb86SQuentin Monnet * current CPU should be retrieved. 911c6b5fb86SQuentin Monnet * 912c6b5fb86SQuentin Monnet * Note that before Linux 4.13, only hardware perf event can be 913c6b5fb86SQuentin Monnet * retrieved. 914c6b5fb86SQuentin Monnet * 915c6b5fb86SQuentin Monnet * Also, be aware that the newer helper 916c6b5fb86SQuentin Monnet * **bpf_perf_event_read_value**\ () is recommended over 9173bd5a09bSQuentin Monnet * **bpf_perf_event_read**\ () in general. The latter has some ABI 918c6b5fb86SQuentin Monnet * quirks where error and counter value are used as a return code 919c6b5fb86SQuentin Monnet * (which is wrong to do since ranges may overlap). This issue is 9203bd5a09bSQuentin Monnet * fixed with **bpf_perf_event_read_value**\ (), which at the same 9213bd5a09bSQuentin Monnet * time provides more features over the **bpf_perf_event_read**\ 9223bd5a09bSQuentin Monnet * () interface. Please refer to the description of 923c6b5fb86SQuentin Monnet * **bpf_perf_event_read_value**\ () for details. 924c6b5fb86SQuentin Monnet * Return 925c6b5fb86SQuentin Monnet * The value of the perf event counter read from the map, or a 926c6b5fb86SQuentin Monnet * negative error code in case of failure. 927c6b5fb86SQuentin Monnet * 928c456dec4SQuentin Monnet * int bpf_redirect(u32 ifindex, u64 flags) 929c456dec4SQuentin Monnet * Description 930c456dec4SQuentin Monnet * Redirect the packet to another net device of index *ifindex*. 931c456dec4SQuentin Monnet * This helper is somewhat similar to **bpf_clone_redirect**\ 932c456dec4SQuentin Monnet * (), except that the packet is not cloned, which provides 933c456dec4SQuentin Monnet * increased performance. 934c456dec4SQuentin Monnet * 935c456dec4SQuentin Monnet * Except for XDP, both ingress and egress interfaces can be used 936c456dec4SQuentin Monnet * for redirection. The **BPF_F_INGRESS** value in *flags* is used 937c456dec4SQuentin Monnet * to make the distinction (ingress path is selected if the flag 938c456dec4SQuentin Monnet * is present, egress path otherwise). Currently, XDP only 939c456dec4SQuentin Monnet * supports redirection to the egress interface, and accepts no 940c456dec4SQuentin Monnet * flag at all. 941c456dec4SQuentin Monnet * 942c456dec4SQuentin Monnet * The same effect can be attained with the more generic 943c456dec4SQuentin Monnet * **bpf_redirect_map**\ (), which requires specific maps to be 944c456dec4SQuentin Monnet * used but offers better performance. 945c456dec4SQuentin Monnet * Return 946c456dec4SQuentin Monnet * For XDP, the helper returns **XDP_REDIRECT** on success or 947c456dec4SQuentin Monnet * **XDP_ABORTED** on error. For other program types, the values 948c456dec4SQuentin Monnet * are **TC_ACT_REDIRECT** on success or **TC_ACT_SHOT** on 949c456dec4SQuentin Monnet * error. 950c456dec4SQuentin Monnet * 9511fdd08beSQuentin Monnet * u32 bpf_get_route_realm(struct sk_buff *skb) 9521fdd08beSQuentin Monnet * Description 9531fdd08beSQuentin Monnet * Retrieve the realm or the route, that is to say the 9541fdd08beSQuentin Monnet * **tclassid** field of the destination for the *skb*. The 9551fdd08beSQuentin Monnet * indentifier retrieved is a user-provided tag, similar to the 9561fdd08beSQuentin Monnet * one used with the net_cls cgroup (see description for 9571fdd08beSQuentin Monnet * **bpf_get_cgroup_classid**\ () helper), but here this tag is 9581fdd08beSQuentin Monnet * held by a route (a destination entry), not by a task. 9591fdd08beSQuentin Monnet * 9601fdd08beSQuentin Monnet * Retrieving this identifier works with the clsact TC egress hook 9611fdd08beSQuentin Monnet * (see also **tc-bpf(8)**), or alternatively on conventional 9621fdd08beSQuentin Monnet * classful egress qdiscs, but not on TC ingress path. In case of 9631fdd08beSQuentin Monnet * clsact TC egress hook, this has the advantage that, internally, 9641fdd08beSQuentin Monnet * the destination entry has not been dropped yet in the transmit 9651fdd08beSQuentin Monnet * path. Therefore, the destination entry does not need to be 9661fdd08beSQuentin Monnet * artificially held via **netif_keep_dst**\ () for a classful 9671fdd08beSQuentin Monnet * qdisc until the *skb* is freed. 9681fdd08beSQuentin Monnet * 9691fdd08beSQuentin Monnet * This helper is available only if the kernel was compiled with 9701fdd08beSQuentin Monnet * **CONFIG_IP_ROUTE_CLASSID** configuration option. 9711fdd08beSQuentin Monnet * Return 9721fdd08beSQuentin Monnet * The realm of the route for the packet associated to *skb*, or 0 9731fdd08beSQuentin Monnet * if none was found. 9741fdd08beSQuentin Monnet * 975c456dec4SQuentin Monnet * int bpf_perf_event_output(struct pt_reg *ctx, struct bpf_map *map, u64 flags, void *data, u64 size) 976c456dec4SQuentin Monnet * Description 977c456dec4SQuentin Monnet * Write raw *data* blob into a special BPF perf event held by 978c456dec4SQuentin Monnet * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 979c456dec4SQuentin Monnet * event must have the following attributes: **PERF_SAMPLE_RAW** 980c456dec4SQuentin Monnet * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 981c456dec4SQuentin Monnet * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 982c456dec4SQuentin Monnet * 983c456dec4SQuentin Monnet * The *flags* are used to indicate the index in *map* for which 984c456dec4SQuentin Monnet * the value must be put, masked with **BPF_F_INDEX_MASK**. 985c456dec4SQuentin Monnet * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 986c456dec4SQuentin Monnet * to indicate that the index of the current CPU core should be 987c456dec4SQuentin Monnet * used. 988c456dec4SQuentin Monnet * 989c456dec4SQuentin Monnet * The value to write, of *size*, is passed through eBPF stack and 990c456dec4SQuentin Monnet * pointed by *data*. 991c456dec4SQuentin Monnet * 992c456dec4SQuentin Monnet * The context of the program *ctx* needs also be passed to the 993c456dec4SQuentin Monnet * helper. 994c456dec4SQuentin Monnet * 995c456dec4SQuentin Monnet * On user space, a program willing to read the values needs to 996c456dec4SQuentin Monnet * call **perf_event_open**\ () on the perf event (either for 997c456dec4SQuentin Monnet * one or for all CPUs) and to store the file descriptor into the 998c456dec4SQuentin Monnet * *map*. This must be done before the eBPF program can send data 999c456dec4SQuentin Monnet * into it. An example is available in file 1000c456dec4SQuentin Monnet * *samples/bpf/trace_output_user.c* in the Linux kernel source 1001c456dec4SQuentin Monnet * tree (the eBPF program counterpart is in 1002c456dec4SQuentin Monnet * *samples/bpf/trace_output_kern.c*). 1003c456dec4SQuentin Monnet * 1004c456dec4SQuentin Monnet * **bpf_perf_event_output**\ () achieves better performance 1005c456dec4SQuentin Monnet * than **bpf_trace_printk**\ () for sharing data with user 1006c456dec4SQuentin Monnet * space, and is much better suitable for streaming data from eBPF 1007c456dec4SQuentin Monnet * programs. 1008c456dec4SQuentin Monnet * 1009c456dec4SQuentin Monnet * Note that this helper is not restricted to tracing use cases 1010c456dec4SQuentin Monnet * and can be used with programs attached to TC or XDP as well, 1011c456dec4SQuentin Monnet * where it allows for passing data to user space listeners. Data 1012c456dec4SQuentin Monnet * can be: 1013c456dec4SQuentin Monnet * 1014c456dec4SQuentin Monnet * * Only custom structs, 1015c456dec4SQuentin Monnet * * Only the packet payload, or 1016c456dec4SQuentin Monnet * * A combination of both. 1017c456dec4SQuentin Monnet * Return 1018c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 1019c456dec4SQuentin Monnet * 10201fdd08beSQuentin Monnet * int bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len) 10211fdd08beSQuentin Monnet * Description 10221fdd08beSQuentin Monnet * This helper was provided as an easy way to load data from a 10231fdd08beSQuentin Monnet * packet. It can be used to load *len* bytes from *offset* from 10241fdd08beSQuentin Monnet * the packet associated to *skb*, into the buffer pointed by 10251fdd08beSQuentin Monnet * *to*. 10261fdd08beSQuentin Monnet * 10271fdd08beSQuentin Monnet * Since Linux 4.7, usage of this helper has mostly been replaced 10281fdd08beSQuentin Monnet * by "direct packet access", enabling packet data to be 10291fdd08beSQuentin Monnet * manipulated with *skb*\ **->data** and *skb*\ **->data_end** 10301fdd08beSQuentin Monnet * pointing respectively to the first byte of packet data and to 10311fdd08beSQuentin Monnet * the byte after the last byte of packet data. However, it 10321fdd08beSQuentin Monnet * remains useful if one wishes to read large quantities of data 10331fdd08beSQuentin Monnet * at once from a packet into the eBPF stack. 10341fdd08beSQuentin Monnet * Return 10351fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 10361fdd08beSQuentin Monnet * 1037c456dec4SQuentin Monnet * int bpf_get_stackid(struct pt_reg *ctx, struct bpf_map *map, u64 flags) 1038c456dec4SQuentin Monnet * Description 1039c456dec4SQuentin Monnet * Walk a user or a kernel stack and return its id. To achieve 1040c456dec4SQuentin Monnet * this, the helper needs *ctx*, which is a pointer to the context 1041c456dec4SQuentin Monnet * on which the tracing program is executed, and a pointer to a 1042c456dec4SQuentin Monnet * *map* of type **BPF_MAP_TYPE_STACK_TRACE**. 1043c456dec4SQuentin Monnet * 1044c456dec4SQuentin Monnet * The last argument, *flags*, holds the number of stack frames to 1045c456dec4SQuentin Monnet * skip (from 0 to 255), masked with 1046c456dec4SQuentin Monnet * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 1047c456dec4SQuentin Monnet * a combination of the following flags: 1048c456dec4SQuentin Monnet * 1049c456dec4SQuentin Monnet * **BPF_F_USER_STACK** 1050c456dec4SQuentin Monnet * Collect a user space stack instead of a kernel stack. 1051c456dec4SQuentin Monnet * **BPF_F_FAST_STACK_CMP** 1052c456dec4SQuentin Monnet * Compare stacks by hash only. 1053c456dec4SQuentin Monnet * **BPF_F_REUSE_STACKID** 1054c456dec4SQuentin Monnet * If two different stacks hash into the same *stackid*, 1055c456dec4SQuentin Monnet * discard the old one. 1056c456dec4SQuentin Monnet * 1057c456dec4SQuentin Monnet * The stack id retrieved is a 32 bit long integer handle which 1058c456dec4SQuentin Monnet * can be further combined with other data (including other stack 1059c456dec4SQuentin Monnet * ids) and used as a key into maps. This can be useful for 1060c456dec4SQuentin Monnet * generating a variety of graphs (such as flame graphs or off-cpu 1061c456dec4SQuentin Monnet * graphs). 1062c456dec4SQuentin Monnet * 1063c456dec4SQuentin Monnet * For walking a stack, this helper is an improvement over 1064c456dec4SQuentin Monnet * **bpf_probe_read**\ (), which can be used with unrolled loops 1065c456dec4SQuentin Monnet * but is not efficient and consumes a lot of eBPF instructions. 1066c456dec4SQuentin Monnet * Instead, **bpf_get_stackid**\ () can collect up to 1067c456dec4SQuentin Monnet * **PERF_MAX_STACK_DEPTH** both kernel and user frames. Note that 1068c456dec4SQuentin Monnet * this limit can be controlled with the **sysctl** program, and 1069c456dec4SQuentin Monnet * that it should be manually increased in order to profile long 1070c456dec4SQuentin Monnet * user stacks (such as stacks for Java programs). To do so, use: 1071c456dec4SQuentin Monnet * 1072c456dec4SQuentin Monnet * :: 1073c456dec4SQuentin Monnet * 1074c456dec4SQuentin Monnet * # sysctl kernel.perf_event_max_stack=<new value> 1075c456dec4SQuentin Monnet * Return 1076c456dec4SQuentin Monnet * The positive or null stack id on success, or a negative error 1077c456dec4SQuentin Monnet * in case of failure. 1078c456dec4SQuentin Monnet * 10791fdd08beSQuentin Monnet * s64 bpf_csum_diff(__be32 *from, u32 from_size, __be32 *to, u32 to_size, __wsum seed) 10801fdd08beSQuentin Monnet * Description 10811fdd08beSQuentin Monnet * Compute a checksum difference, from the raw buffer pointed by 10821fdd08beSQuentin Monnet * *from*, of length *from_size* (that must be a multiple of 4), 10831fdd08beSQuentin Monnet * towards the raw buffer pointed by *to*, of size *to_size* 10841fdd08beSQuentin Monnet * (same remark). An optional *seed* can be added to the value 10851fdd08beSQuentin Monnet * (this can be cascaded, the seed may come from a previous call 10861fdd08beSQuentin Monnet * to the helper). 10871fdd08beSQuentin Monnet * 10881fdd08beSQuentin Monnet * This is flexible enough to be used in several ways: 10891fdd08beSQuentin Monnet * 10901fdd08beSQuentin Monnet * * With *from_size* == 0, *to_size* > 0 and *seed* set to 10911fdd08beSQuentin Monnet * checksum, it can be used when pushing new data. 10921fdd08beSQuentin Monnet * * With *from_size* > 0, *to_size* == 0 and *seed* set to 10931fdd08beSQuentin Monnet * checksum, it can be used when removing data from a packet. 10941fdd08beSQuentin Monnet * * With *from_size* > 0, *to_size* > 0 and *seed* set to 0, it 10951fdd08beSQuentin Monnet * can be used to compute a diff. Note that *from_size* and 10961fdd08beSQuentin Monnet * *to_size* do not need to be equal. 10971fdd08beSQuentin Monnet * 10981fdd08beSQuentin Monnet * This helper can be used in combination with 10991fdd08beSQuentin Monnet * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ (), to 11001fdd08beSQuentin Monnet * which one can feed in the difference computed with 11011fdd08beSQuentin Monnet * **bpf_csum_diff**\ (). 11021fdd08beSQuentin Monnet * Return 11031fdd08beSQuentin Monnet * The checksum result, or a negative error code in case of 11041fdd08beSQuentin Monnet * failure. 11051fdd08beSQuentin Monnet * 11061fdd08beSQuentin Monnet * int bpf_skb_get_tunnel_opt(struct sk_buff *skb, u8 *opt, u32 size) 11071fdd08beSQuentin Monnet * Description 11081fdd08beSQuentin Monnet * Retrieve tunnel options metadata for the packet associated to 11091fdd08beSQuentin Monnet * *skb*, and store the raw tunnel option data to the buffer *opt* 11101fdd08beSQuentin Monnet * of *size*. 11111fdd08beSQuentin Monnet * 11121fdd08beSQuentin Monnet * This helper can be used with encapsulation devices that can 11131fdd08beSQuentin Monnet * operate in "collect metadata" mode (please refer to the related 11141fdd08beSQuentin Monnet * note in the description of **bpf_skb_get_tunnel_key**\ () for 11151fdd08beSQuentin Monnet * more details). A particular example where this can be used is 11161fdd08beSQuentin Monnet * in combination with the Geneve encapsulation protocol, where it 11171fdd08beSQuentin Monnet * allows for pushing (with **bpf_skb_get_tunnel_opt**\ () helper) 11181fdd08beSQuentin Monnet * and retrieving arbitrary TLVs (Type-Length-Value headers) from 11191fdd08beSQuentin Monnet * the eBPF program. This allows for full customization of these 11201fdd08beSQuentin Monnet * headers. 11211fdd08beSQuentin Monnet * Return 11221fdd08beSQuentin Monnet * The size of the option data retrieved. 11231fdd08beSQuentin Monnet * 11241fdd08beSQuentin Monnet * int bpf_skb_set_tunnel_opt(struct sk_buff *skb, u8 *opt, u32 size) 11251fdd08beSQuentin Monnet * Description 11261fdd08beSQuentin Monnet * Set tunnel options metadata for the packet associated to *skb* 11271fdd08beSQuentin Monnet * to the option data contained in the raw buffer *opt* of *size*. 11281fdd08beSQuentin Monnet * 11291fdd08beSQuentin Monnet * See also the description of the **bpf_skb_get_tunnel_opt**\ () 11301fdd08beSQuentin Monnet * helper for additional information. 11311fdd08beSQuentin Monnet * Return 11321fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 11331fdd08beSQuentin Monnet * 11341fdd08beSQuentin Monnet * int bpf_skb_change_proto(struct sk_buff *skb, __be16 proto, u64 flags) 11351fdd08beSQuentin Monnet * Description 11361fdd08beSQuentin Monnet * Change the protocol of the *skb* to *proto*. Currently 11371fdd08beSQuentin Monnet * supported are transition from IPv4 to IPv6, and from IPv6 to 11381fdd08beSQuentin Monnet * IPv4. The helper takes care of the groundwork for the 11391fdd08beSQuentin Monnet * transition, including resizing the socket buffer. The eBPF 11401fdd08beSQuentin Monnet * program is expected to fill the new headers, if any, via 11411fdd08beSQuentin Monnet * **skb_store_bytes**\ () and to recompute the checksums with 11421fdd08beSQuentin Monnet * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ 11431fdd08beSQuentin Monnet * (). The main case for this helper is to perform NAT64 11441fdd08beSQuentin Monnet * operations out of an eBPF program. 11451fdd08beSQuentin Monnet * 11461fdd08beSQuentin Monnet * Internally, the GSO type is marked as dodgy so that headers are 11471fdd08beSQuentin Monnet * checked and segments are recalculated by the GSO/GRO engine. 11481fdd08beSQuentin Monnet * The size for GSO target is adapted as well. 11491fdd08beSQuentin Monnet * 11501fdd08beSQuentin Monnet * All values for *flags* are reserved for future usage, and must 11511fdd08beSQuentin Monnet * be left at zero. 11521fdd08beSQuentin Monnet * 11531fdd08beSQuentin Monnet * A call to this helper is susceptible to change the underlaying 11541fdd08beSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 11551fdd08beSQuentin Monnet * previously done by the verifier are invalidated and must be 11561fdd08beSQuentin Monnet * performed again, if the helper is used in combination with 11571fdd08beSQuentin Monnet * direct packet access. 11581fdd08beSQuentin Monnet * Return 11591fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 11601fdd08beSQuentin Monnet * 11611fdd08beSQuentin Monnet * int bpf_skb_change_type(struct sk_buff *skb, u32 type) 11621fdd08beSQuentin Monnet * Description 11631fdd08beSQuentin Monnet * Change the packet type for the packet associated to *skb*. This 11641fdd08beSQuentin Monnet * comes down to setting *skb*\ **->pkt_type** to *type*, except 11651fdd08beSQuentin Monnet * the eBPF program does not have a write access to *skb*\ 11661fdd08beSQuentin Monnet * **->pkt_type** beside this helper. Using a helper here allows 11671fdd08beSQuentin Monnet * for graceful handling of errors. 11681fdd08beSQuentin Monnet * 11691fdd08beSQuentin Monnet * The major use case is to change incoming *skb*s to 11701fdd08beSQuentin Monnet * **PACKET_HOST** in a programmatic way instead of having to 11711fdd08beSQuentin Monnet * recirculate via **redirect**\ (..., **BPF_F_INGRESS**), for 11721fdd08beSQuentin Monnet * example. 11731fdd08beSQuentin Monnet * 11741fdd08beSQuentin Monnet * Note that *type* only allows certain values. At this time, they 11751fdd08beSQuentin Monnet * are: 11761fdd08beSQuentin Monnet * 11771fdd08beSQuentin Monnet * **PACKET_HOST** 11781fdd08beSQuentin Monnet * Packet is for us. 11791fdd08beSQuentin Monnet * **PACKET_BROADCAST** 11801fdd08beSQuentin Monnet * Send packet to all. 11811fdd08beSQuentin Monnet * **PACKET_MULTICAST** 11821fdd08beSQuentin Monnet * Send packet to group. 11831fdd08beSQuentin Monnet * **PACKET_OTHERHOST** 11841fdd08beSQuentin Monnet * Send packet to someone else. 11851fdd08beSQuentin Monnet * Return 11861fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 11871fdd08beSQuentin Monnet * 1188c6b5fb86SQuentin Monnet * int bpf_skb_under_cgroup(struct sk_buff *skb, struct bpf_map *map, u32 index) 1189c6b5fb86SQuentin Monnet * Description 1190c6b5fb86SQuentin Monnet * Check whether *skb* is a descendant of the cgroup2 held by 1191c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. 1192c6b5fb86SQuentin Monnet * Return 1193c6b5fb86SQuentin Monnet * The return value depends on the result of the test, and can be: 1194c6b5fb86SQuentin Monnet * 1195c6b5fb86SQuentin Monnet * * 0, if the *skb* failed the cgroup2 descendant test. 1196c6b5fb86SQuentin Monnet * * 1, if the *skb* succeeded the cgroup2 descendant test. 1197c6b5fb86SQuentin Monnet * * A negative error code, if an error occurred. 1198c6b5fb86SQuentin Monnet * 1199fa15601aSQuentin Monnet * u32 bpf_get_hash_recalc(struct sk_buff *skb) 1200fa15601aSQuentin Monnet * Description 1201fa15601aSQuentin Monnet * Retrieve the hash of the packet, *skb*\ **->hash**. If it is 1202fa15601aSQuentin Monnet * not set, in particular if the hash was cleared due to mangling, 1203fa15601aSQuentin Monnet * recompute this hash. Later accesses to the hash can be done 1204fa15601aSQuentin Monnet * directly with *skb*\ **->hash**. 1205fa15601aSQuentin Monnet * 1206fa15601aSQuentin Monnet * Calling **bpf_set_hash_invalid**\ (), changing a packet 1207fa15601aSQuentin Monnet * prototype with **bpf_skb_change_proto**\ (), or calling 1208fa15601aSQuentin Monnet * **bpf_skb_store_bytes**\ () with the 1209fa15601aSQuentin Monnet * **BPF_F_INVALIDATE_HASH** are actions susceptible to clear 1210fa15601aSQuentin Monnet * the hash and to trigger a new computation for the next call to 1211fa15601aSQuentin Monnet * **bpf_get_hash_recalc**\ (). 1212fa15601aSQuentin Monnet * Return 1213fa15601aSQuentin Monnet * The 32-bit hash. 1214fa15601aSQuentin Monnet * 1215c456dec4SQuentin Monnet * u64 bpf_get_current_task(void) 1216c456dec4SQuentin Monnet * Return 1217c456dec4SQuentin Monnet * A pointer to the current task struct. 1218fa15601aSQuentin Monnet * 1219c6b5fb86SQuentin Monnet * int bpf_probe_write_user(void *dst, const void *src, u32 len) 1220c6b5fb86SQuentin Monnet * Description 1221c6b5fb86SQuentin Monnet * Attempt in a safe way to write *len* bytes from the buffer 1222c6b5fb86SQuentin Monnet * *src* to *dst* in memory. It only works for threads that are in 1223c6b5fb86SQuentin Monnet * user context, and *dst* must be a valid user space address. 1224c6b5fb86SQuentin Monnet * 1225c6b5fb86SQuentin Monnet * This helper should not be used to implement any kind of 1226c6b5fb86SQuentin Monnet * security mechanism because of TOC-TOU attacks, but rather to 1227c6b5fb86SQuentin Monnet * debug, divert, and manipulate execution of semi-cooperative 1228c6b5fb86SQuentin Monnet * processes. 1229c6b5fb86SQuentin Monnet * 1230c6b5fb86SQuentin Monnet * Keep in mind that this feature is meant for experiments, and it 1231c6b5fb86SQuentin Monnet * has a risk of crashing the system and running programs. 1232c6b5fb86SQuentin Monnet * Therefore, when an eBPF program using this helper is attached, 1233c6b5fb86SQuentin Monnet * a warning including PID and process name is printed to kernel 1234c6b5fb86SQuentin Monnet * logs. 1235c6b5fb86SQuentin Monnet * Return 1236c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 1237c6b5fb86SQuentin Monnet * 1238c6b5fb86SQuentin Monnet * int bpf_current_task_under_cgroup(struct bpf_map *map, u32 index) 1239c6b5fb86SQuentin Monnet * Description 1240c6b5fb86SQuentin Monnet * Check whether the probe is being run is the context of a given 1241c6b5fb86SQuentin Monnet * subset of the cgroup2 hierarchy. The cgroup2 to test is held by 1242c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. 1243c6b5fb86SQuentin Monnet * Return 1244c6b5fb86SQuentin Monnet * The return value depends on the result of the test, and can be: 1245c6b5fb86SQuentin Monnet * 1246c6b5fb86SQuentin Monnet * * 0, if the *skb* task belongs to the cgroup2. 1247c6b5fb86SQuentin Monnet * * 1, if the *skb* task does not belong to the cgroup2. 1248c6b5fb86SQuentin Monnet * * A negative error code, if an error occurred. 1249c6b5fb86SQuentin Monnet * 1250fa15601aSQuentin Monnet * int bpf_skb_change_tail(struct sk_buff *skb, u32 len, u64 flags) 1251fa15601aSQuentin Monnet * Description 1252fa15601aSQuentin Monnet * Resize (trim or grow) the packet associated to *skb* to the 1253fa15601aSQuentin Monnet * new *len*. The *flags* are reserved for future usage, and must 1254fa15601aSQuentin Monnet * be left at zero. 1255fa15601aSQuentin Monnet * 1256fa15601aSQuentin Monnet * The basic idea is that the helper performs the needed work to 1257fa15601aSQuentin Monnet * change the size of the packet, then the eBPF program rewrites 1258fa15601aSQuentin Monnet * the rest via helpers like **bpf_skb_store_bytes**\ (), 1259fa15601aSQuentin Monnet * **bpf_l3_csum_replace**\ (), **bpf_l3_csum_replace**\ () 1260fa15601aSQuentin Monnet * and others. This helper is a slow path utility intended for 1261fa15601aSQuentin Monnet * replies with control messages. And because it is targeted for 1262fa15601aSQuentin Monnet * slow path, the helper itself can afford to be slow: it 1263fa15601aSQuentin Monnet * implicitly linearizes, unclones and drops offloads from the 1264fa15601aSQuentin Monnet * *skb*. 1265fa15601aSQuentin Monnet * 1266fa15601aSQuentin Monnet * A call to this helper is susceptible to change the underlaying 1267fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1268fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1269fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1270fa15601aSQuentin Monnet * direct packet access. 1271fa15601aSQuentin Monnet * Return 1272fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 1273fa15601aSQuentin Monnet * 1274fa15601aSQuentin Monnet * int bpf_skb_pull_data(struct sk_buff *skb, u32 len) 1275fa15601aSQuentin Monnet * Description 1276fa15601aSQuentin Monnet * Pull in non-linear data in case the *skb* is non-linear and not 1277fa15601aSQuentin Monnet * all of *len* are part of the linear section. Make *len* bytes 1278fa15601aSQuentin Monnet * from *skb* readable and writable. If a zero value is passed for 1279fa15601aSQuentin Monnet * *len*, then the whole length of the *skb* is pulled. 1280fa15601aSQuentin Monnet * 1281fa15601aSQuentin Monnet * This helper is only needed for reading and writing with direct 1282fa15601aSQuentin Monnet * packet access. 1283fa15601aSQuentin Monnet * 1284fa15601aSQuentin Monnet * For direct packet access, testing that offsets to access 1285fa15601aSQuentin Monnet * are within packet boundaries (test on *skb*\ **->data_end**) is 1286fa15601aSQuentin Monnet * susceptible to fail if offsets are invalid, or if the requested 1287fa15601aSQuentin Monnet * data is in non-linear parts of the *skb*. On failure the 1288fa15601aSQuentin Monnet * program can just bail out, or in the case of a non-linear 1289fa15601aSQuentin Monnet * buffer, use a helper to make the data available. The 1290fa15601aSQuentin Monnet * **bpf_skb_load_bytes**\ () helper is a first solution to access 1291fa15601aSQuentin Monnet * the data. Another one consists in using **bpf_skb_pull_data** 1292fa15601aSQuentin Monnet * to pull in once the non-linear parts, then retesting and 1293fa15601aSQuentin Monnet * eventually access the data. 1294fa15601aSQuentin Monnet * 1295fa15601aSQuentin Monnet * At the same time, this also makes sure the *skb* is uncloned, 1296fa15601aSQuentin Monnet * which is a necessary condition for direct write. As this needs 1297fa15601aSQuentin Monnet * to be an invariant for the write part only, the verifier 1298fa15601aSQuentin Monnet * detects writes and adds a prologue that is calling 1299fa15601aSQuentin Monnet * **bpf_skb_pull_data()** to effectively unclone the *skb* from 1300fa15601aSQuentin Monnet * the very beginning in case it is indeed cloned. 1301fa15601aSQuentin Monnet * 1302fa15601aSQuentin Monnet * A call to this helper is susceptible to change the underlaying 1303fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1304fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1305fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1306fa15601aSQuentin Monnet * direct packet access. 1307fa15601aSQuentin Monnet * Return 1308fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 1309fa15601aSQuentin Monnet * 1310fa15601aSQuentin Monnet * s64 bpf_csum_update(struct sk_buff *skb, __wsum csum) 1311fa15601aSQuentin Monnet * Description 1312fa15601aSQuentin Monnet * Add the checksum *csum* into *skb*\ **->csum** in case the 1313fa15601aSQuentin Monnet * driver has supplied a checksum for the entire packet into that 1314fa15601aSQuentin Monnet * field. Return an error otherwise. This helper is intended to be 1315fa15601aSQuentin Monnet * used in combination with **bpf_csum_diff**\ (), in particular 1316fa15601aSQuentin Monnet * when the checksum needs to be updated after data has been 1317fa15601aSQuentin Monnet * written into the packet through direct packet access. 1318fa15601aSQuentin Monnet * Return 1319fa15601aSQuentin Monnet * The checksum on success, or a negative error code in case of 1320fa15601aSQuentin Monnet * failure. 1321fa15601aSQuentin Monnet * 1322fa15601aSQuentin Monnet * void bpf_set_hash_invalid(struct sk_buff *skb) 1323fa15601aSQuentin Monnet * Description 1324fa15601aSQuentin Monnet * Invalidate the current *skb*\ **->hash**. It can be used after 1325fa15601aSQuentin Monnet * mangling on headers through direct packet access, in order to 1326fa15601aSQuentin Monnet * indicate that the hash is outdated and to trigger a 1327fa15601aSQuentin Monnet * recalculation the next time the kernel tries to access this 1328fa15601aSQuentin Monnet * hash or when the **bpf_get_hash_recalc**\ () helper is called. 1329fa15601aSQuentin Monnet * 1330fa15601aSQuentin Monnet * int bpf_get_numa_node_id(void) 1331fa15601aSQuentin Monnet * Description 1332fa15601aSQuentin Monnet * Return the id of the current NUMA node. The primary use case 1333fa15601aSQuentin Monnet * for this helper is the selection of sockets for the local NUMA 1334fa15601aSQuentin Monnet * node, when the program is attached to sockets using the 1335fa15601aSQuentin Monnet * **SO_ATTACH_REUSEPORT_EBPF** option (see also **socket(7)**), 1336fa15601aSQuentin Monnet * but the helper is also available to other eBPF program types, 1337fa15601aSQuentin Monnet * similarly to **bpf_get_smp_processor_id**\ (). 1338fa15601aSQuentin Monnet * Return 1339fa15601aSQuentin Monnet * The id of current NUMA node. 1340fa15601aSQuentin Monnet * 1341c6b5fb86SQuentin Monnet * int bpf_skb_change_head(struct sk_buff *skb, u32 len, u64 flags) 1342c6b5fb86SQuentin Monnet * Description 1343c6b5fb86SQuentin Monnet * Grows headroom of packet associated to *skb* and adjusts the 1344c6b5fb86SQuentin Monnet * offset of the MAC header accordingly, adding *len* bytes of 1345c6b5fb86SQuentin Monnet * space. It automatically extends and reallocates memory as 1346c6b5fb86SQuentin Monnet * required. 1347c6b5fb86SQuentin Monnet * 1348c6b5fb86SQuentin Monnet * This helper can be used on a layer 3 *skb* to push a MAC header 1349c6b5fb86SQuentin Monnet * for redirection into a layer 2 device. 1350c6b5fb86SQuentin Monnet * 1351c6b5fb86SQuentin Monnet * All values for *flags* are reserved for future usage, and must 1352c6b5fb86SQuentin Monnet * be left at zero. 1353c6b5fb86SQuentin Monnet * 1354c6b5fb86SQuentin Monnet * A call to this helper is susceptible to change the underlaying 1355c6b5fb86SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1356c6b5fb86SQuentin Monnet * previously done by the verifier are invalidated and must be 1357c6b5fb86SQuentin Monnet * performed again, if the helper is used in combination with 1358c6b5fb86SQuentin Monnet * direct packet access. 1359c6b5fb86SQuentin Monnet * Return 1360c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 1361c6b5fb86SQuentin Monnet * 1362c6b5fb86SQuentin Monnet * int bpf_xdp_adjust_head(struct xdp_buff *xdp_md, int delta) 1363c6b5fb86SQuentin Monnet * Description 1364c6b5fb86SQuentin Monnet * Adjust (move) *xdp_md*\ **->data** by *delta* bytes. Note that 1365c6b5fb86SQuentin Monnet * it is possible to use a negative value for *delta*. This helper 1366c6b5fb86SQuentin Monnet * can be used to prepare the packet for pushing or popping 1367c6b5fb86SQuentin Monnet * headers. 1368c6b5fb86SQuentin Monnet * 1369c6b5fb86SQuentin Monnet * A call to this helper is susceptible to change the underlaying 1370c6b5fb86SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1371c6b5fb86SQuentin Monnet * previously done by the verifier are invalidated and must be 1372c6b5fb86SQuentin Monnet * performed again, if the helper is used in combination with 1373c6b5fb86SQuentin Monnet * direct packet access. 1374c6b5fb86SQuentin Monnet * Return 1375c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 1376c6b5fb86SQuentin Monnet * 1377c6b5fb86SQuentin Monnet * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr) 1378c6b5fb86SQuentin Monnet * Description 1379c6b5fb86SQuentin Monnet * Copy a NUL terminated string from an unsafe address 1380c6b5fb86SQuentin Monnet * *unsafe_ptr* to *dst*. The *size* should include the 1381c6b5fb86SQuentin Monnet * terminating NUL byte. In case the string length is smaller than 1382c6b5fb86SQuentin Monnet * *size*, the target is not padded with further NUL bytes. If the 1383c6b5fb86SQuentin Monnet * string length is larger than *size*, just *size*-1 bytes are 1384c6b5fb86SQuentin Monnet * copied and the last byte is set to NUL. 1385c6b5fb86SQuentin Monnet * 1386c6b5fb86SQuentin Monnet * On success, the length of the copied string is returned. This 1387c6b5fb86SQuentin Monnet * makes this helper useful in tracing programs for reading 1388c6b5fb86SQuentin Monnet * strings, and more importantly to get its length at runtime. See 1389c6b5fb86SQuentin Monnet * the following snippet: 1390c6b5fb86SQuentin Monnet * 1391c6b5fb86SQuentin Monnet * :: 1392c6b5fb86SQuentin Monnet * 1393c6b5fb86SQuentin Monnet * SEC("kprobe/sys_open") 1394c6b5fb86SQuentin Monnet * void bpf_sys_open(struct pt_regs *ctx) 1395c6b5fb86SQuentin Monnet * { 1396c6b5fb86SQuentin Monnet * char buf[PATHLEN]; // PATHLEN is defined to 256 1397c6b5fb86SQuentin Monnet * int res = bpf_probe_read_str(buf, sizeof(buf), 1398c6b5fb86SQuentin Monnet * ctx->di); 1399c6b5fb86SQuentin Monnet * 1400c6b5fb86SQuentin Monnet * // Consume buf, for example push it to 1401c6b5fb86SQuentin Monnet * // userspace via bpf_perf_event_output(); we 1402c6b5fb86SQuentin Monnet * // can use res (the string length) as event 1403c6b5fb86SQuentin Monnet * // size, after checking its boundaries. 1404c6b5fb86SQuentin Monnet * } 1405c6b5fb86SQuentin Monnet * 1406c6b5fb86SQuentin Monnet * In comparison, using **bpf_probe_read()** helper here instead 1407c6b5fb86SQuentin Monnet * to read the string would require to estimate the length at 1408c6b5fb86SQuentin Monnet * compile time, and would often result in copying more memory 1409c6b5fb86SQuentin Monnet * than necessary. 1410c6b5fb86SQuentin Monnet * 1411c6b5fb86SQuentin Monnet * Another useful use case is when parsing individual process 1412c6b5fb86SQuentin Monnet * arguments or individual environment variables navigating 1413c6b5fb86SQuentin Monnet * *current*\ **->mm->arg_start** and *current*\ 1414c6b5fb86SQuentin Monnet * **->mm->env_start**: using this helper and the return value, 1415c6b5fb86SQuentin Monnet * one can quickly iterate at the right offset of the memory area. 1416c6b5fb86SQuentin Monnet * Return 1417c6b5fb86SQuentin Monnet * On success, the strictly positive length of the string, 1418c6b5fb86SQuentin Monnet * including the trailing NUL character. On error, a negative 1419c6b5fb86SQuentin Monnet * value. 1420c6b5fb86SQuentin Monnet * 1421c6b5fb86SQuentin Monnet * u64 bpf_get_socket_cookie(struct sk_buff *skb) 1422c6b5fb86SQuentin Monnet * Description 1423c6b5fb86SQuentin Monnet * If the **struct sk_buff** pointed by *skb* has a known socket, 1424c6b5fb86SQuentin Monnet * retrieve the cookie (generated by the kernel) of this socket. 1425c6b5fb86SQuentin Monnet * If no cookie has been set yet, generate a new cookie. Once 1426c6b5fb86SQuentin Monnet * generated, the socket cookie remains stable for the life of the 1427c6b5fb86SQuentin Monnet * socket. This helper can be useful for monitoring per socket 1428c6b5fb86SQuentin Monnet * networking traffic statistics as it provides a unique socket 1429c6b5fb86SQuentin Monnet * identifier per namespace. 1430c6b5fb86SQuentin Monnet * Return 1431c6b5fb86SQuentin Monnet * A 8-byte long non-decreasing number on success, or 0 if the 1432c6b5fb86SQuentin Monnet * socket field is missing inside *skb*. 1433c6b5fb86SQuentin Monnet * 1434d692f113SAndrey Ignatov * u64 bpf_get_socket_cookie(struct bpf_sock_addr *ctx) 1435d692f113SAndrey Ignatov * Description 1436d692f113SAndrey Ignatov * Equivalent to bpf_get_socket_cookie() helper that accepts 1437d692f113SAndrey Ignatov * *skb*, but gets socket from **struct bpf_sock_addr** contex. 1438d692f113SAndrey Ignatov * Return 1439d692f113SAndrey Ignatov * A 8-byte long non-decreasing number. 1440d692f113SAndrey Ignatov * 1441d692f113SAndrey Ignatov * u64 bpf_get_socket_cookie(struct bpf_sock_ops *ctx) 1442d692f113SAndrey Ignatov * Description 1443d692f113SAndrey Ignatov * Equivalent to bpf_get_socket_cookie() helper that accepts 1444d692f113SAndrey Ignatov * *skb*, but gets socket from **struct bpf_sock_ops** contex. 1445d692f113SAndrey Ignatov * Return 1446d692f113SAndrey Ignatov * A 8-byte long non-decreasing number. 1447d692f113SAndrey Ignatov * 1448c6b5fb86SQuentin Monnet * u32 bpf_get_socket_uid(struct sk_buff *skb) 1449c6b5fb86SQuentin Monnet * Return 1450c6b5fb86SQuentin Monnet * The owner UID of the socket associated to *skb*. If the socket 1451c6b5fb86SQuentin Monnet * is **NULL**, or if it is not a full socket (i.e. if it is a 1452c6b5fb86SQuentin Monnet * time-wait or a request socket instead), **overflowuid** value 1453c6b5fb86SQuentin Monnet * is returned (note that **overflowuid** might also be the actual 1454c6b5fb86SQuentin Monnet * UID value for the socket). 1455c6b5fb86SQuentin Monnet * 1456fa15601aSQuentin Monnet * u32 bpf_set_hash(struct sk_buff *skb, u32 hash) 1457fa15601aSQuentin Monnet * Description 1458fa15601aSQuentin Monnet * Set the full hash for *skb* (set the field *skb*\ **->hash**) 1459fa15601aSQuentin Monnet * to value *hash*. 1460fa15601aSQuentin Monnet * Return 1461fa15601aSQuentin Monnet * 0 1462fa15601aSQuentin Monnet * 1463a3ef8e9aSAndrey Ignatov * int bpf_setsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, char *optval, int optlen) 14647aa79a86SQuentin Monnet * Description 14657aa79a86SQuentin Monnet * Emulate a call to **setsockopt()** on the socket associated to 14667aa79a86SQuentin Monnet * *bpf_socket*, which must be a full socket. The *level* at 14677aa79a86SQuentin Monnet * which the option resides and the name *optname* of the option 14687aa79a86SQuentin Monnet * must be specified, see **setsockopt(2)** for more information. 14697aa79a86SQuentin Monnet * The option value of length *optlen* is pointed by *optval*. 14707aa79a86SQuentin Monnet * 14717aa79a86SQuentin Monnet * This helper actually implements a subset of **setsockopt()**. 14727aa79a86SQuentin Monnet * It supports the following *level*\ s: 14737aa79a86SQuentin Monnet * 14747aa79a86SQuentin Monnet * * **SOL_SOCKET**, which supports the following *optname*\ s: 14757aa79a86SQuentin Monnet * **SO_RCVBUF**, **SO_SNDBUF**, **SO_MAX_PACING_RATE**, 14767aa79a86SQuentin Monnet * **SO_PRIORITY**, **SO_RCVLOWAT**, **SO_MARK**. 14777aa79a86SQuentin Monnet * * **IPPROTO_TCP**, which supports the following *optname*\ s: 14787aa79a86SQuentin Monnet * **TCP_CONGESTION**, **TCP_BPF_IW**, 14797aa79a86SQuentin Monnet * **TCP_BPF_SNDCWND_CLAMP**. 14807aa79a86SQuentin Monnet * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. 14817aa79a86SQuentin Monnet * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**. 14827aa79a86SQuentin Monnet * Return 14837aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 14847aa79a86SQuentin Monnet * 1485b55cbc8dSNicolas Dichtel * int bpf_skb_adjust_room(struct sk_buff *skb, s32 len_diff, u32 mode, u64 flags) 1486fa15601aSQuentin Monnet * Description 1487fa15601aSQuentin Monnet * Grow or shrink the room for data in the packet associated to 1488fa15601aSQuentin Monnet * *skb* by *len_diff*, and according to the selected *mode*. 1489fa15601aSQuentin Monnet * 1490fa15601aSQuentin Monnet * There is a single supported mode at this time: 1491fa15601aSQuentin Monnet * 1492fa15601aSQuentin Monnet * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer 1493fa15601aSQuentin Monnet * (room space is added or removed below the layer 3 header). 1494fa15601aSQuentin Monnet * 1495fa15601aSQuentin Monnet * All values for *flags* are reserved for future usage, and must 1496fa15601aSQuentin Monnet * be left at zero. 1497fa15601aSQuentin Monnet * 1498fa15601aSQuentin Monnet * A call to this helper is susceptible to change the underlaying 1499fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1500fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1501fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1502fa15601aSQuentin Monnet * direct packet access. 1503fa15601aSQuentin Monnet * Return 1504fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 1505fa15601aSQuentin Monnet * 1506ab127040SQuentin Monnet * int bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags) 1507ab127040SQuentin Monnet * Description 1508ab127040SQuentin Monnet * Redirect the packet to the endpoint referenced by *map* at 1509ab127040SQuentin Monnet * index *key*. Depending on its type, this *map* can contain 1510ab127040SQuentin Monnet * references to net devices (for forwarding packets through other 1511ab127040SQuentin Monnet * ports), or to CPUs (for redirecting XDP frames to another CPU; 1512ab127040SQuentin Monnet * but this is only implemented for native XDP (with driver 1513ab127040SQuentin Monnet * support) as of this writing). 1514ab127040SQuentin Monnet * 1515ab127040SQuentin Monnet * All values for *flags* are reserved for future usage, and must 1516ab127040SQuentin Monnet * be left at zero. 1517ab127040SQuentin Monnet * 1518ab127040SQuentin Monnet * When used to redirect packets to net devices, this helper 1519ab127040SQuentin Monnet * provides a high performance increase over **bpf_redirect**\ (). 1520ab127040SQuentin Monnet * This is due to various implementation details of the underlying 1521ab127040SQuentin Monnet * mechanisms, one of which is the fact that **bpf_redirect_map**\ 1522ab127040SQuentin Monnet * () tries to send packet as a "bulk" to the device. 1523ab127040SQuentin Monnet * Return 1524ab127040SQuentin Monnet * **XDP_REDIRECT** on success, or **XDP_ABORTED** on error. 1525ab127040SQuentin Monnet * 1526ab127040SQuentin Monnet * int bpf_sk_redirect_map(struct bpf_map *map, u32 key, u64 flags) 1527ab127040SQuentin Monnet * Description 1528ab127040SQuentin Monnet * Redirect the packet to the socket referenced by *map* (of type 1529ab127040SQuentin Monnet * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and 1530ab127040SQuentin Monnet * egress interfaces can be used for redirection. The 1531ab127040SQuentin Monnet * **BPF_F_INGRESS** value in *flags* is used to make the 1532ab127040SQuentin Monnet * distinction (ingress path is selected if the flag is present, 1533ab127040SQuentin Monnet * egress path otherwise). This is the only flag supported for now. 1534ab127040SQuentin Monnet * Return 1535ab127040SQuentin Monnet * **SK_PASS** on success, or **SK_DROP** on error. 1536ab127040SQuentin Monnet * 1537a3ef8e9aSAndrey Ignatov * int bpf_sock_map_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags) 1538ab127040SQuentin Monnet * Description 1539ab127040SQuentin Monnet * Add an entry to, or update a *map* referencing sockets. The 1540ab127040SQuentin Monnet * *skops* is used as a new value for the entry associated to 1541ab127040SQuentin Monnet * *key*. *flags* is one of: 1542ab127040SQuentin Monnet * 1543ab127040SQuentin Monnet * **BPF_NOEXIST** 1544ab127040SQuentin Monnet * The entry for *key* must not exist in the map. 1545ab127040SQuentin Monnet * **BPF_EXIST** 1546ab127040SQuentin Monnet * The entry for *key* must already exist in the map. 1547ab127040SQuentin Monnet * **BPF_ANY** 1548ab127040SQuentin Monnet * No condition on the existence of the entry for *key*. 1549ab127040SQuentin Monnet * 1550ab127040SQuentin Monnet * If the *map* has eBPF programs (parser and verdict), those will 1551ab127040SQuentin Monnet * be inherited by the socket being added. If the socket is 1552ab127040SQuentin Monnet * already attached to eBPF programs, this results in an error. 1553ab127040SQuentin Monnet * Return 1554ab127040SQuentin Monnet * 0 on success, or a negative error in case of failure. 1555ab127040SQuentin Monnet * 1556fa15601aSQuentin Monnet * int bpf_xdp_adjust_meta(struct xdp_buff *xdp_md, int delta) 1557fa15601aSQuentin Monnet * Description 1558fa15601aSQuentin Monnet * Adjust the address pointed by *xdp_md*\ **->data_meta** by 1559fa15601aSQuentin Monnet * *delta* (which can be positive or negative). Note that this 1560fa15601aSQuentin Monnet * operation modifies the address stored in *xdp_md*\ **->data**, 1561fa15601aSQuentin Monnet * so the latter must be loaded only after the helper has been 1562fa15601aSQuentin Monnet * called. 1563fa15601aSQuentin Monnet * 1564fa15601aSQuentin Monnet * The use of *xdp_md*\ **->data_meta** is optional and programs 1565fa15601aSQuentin Monnet * are not required to use it. The rationale is that when the 1566fa15601aSQuentin Monnet * packet is processed with XDP (e.g. as DoS filter), it is 1567fa15601aSQuentin Monnet * possible to push further meta data along with it before passing 1568fa15601aSQuentin Monnet * to the stack, and to give the guarantee that an ingress eBPF 1569fa15601aSQuentin Monnet * program attached as a TC classifier on the same device can pick 1570fa15601aSQuentin Monnet * this up for further post-processing. Since TC works with socket 1571fa15601aSQuentin Monnet * buffers, it remains possible to set from XDP the **mark** or 1572fa15601aSQuentin Monnet * **priority** pointers, or other pointers for the socket buffer. 1573fa15601aSQuentin Monnet * Having this scratch space generic and programmable allows for 1574fa15601aSQuentin Monnet * more flexibility as the user is free to store whatever meta 1575fa15601aSQuentin Monnet * data they need. 1576fa15601aSQuentin Monnet * 1577fa15601aSQuentin Monnet * A call to this helper is susceptible to change the underlaying 1578fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1579fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1580fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1581fa15601aSQuentin Monnet * direct packet access. 1582fa15601aSQuentin Monnet * Return 1583fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 15847aa79a86SQuentin Monnet * 15857aa79a86SQuentin Monnet * int bpf_perf_event_read_value(struct bpf_map *map, u64 flags, struct bpf_perf_event_value *buf, u32 buf_size) 15867aa79a86SQuentin Monnet * Description 15877aa79a86SQuentin Monnet * Read the value of a perf event counter, and store it into *buf* 15887aa79a86SQuentin Monnet * of size *buf_size*. This helper relies on a *map* of type 15897aa79a86SQuentin Monnet * **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of the perf event 15907aa79a86SQuentin Monnet * counter is selected when *map* is updated with perf event file 15917aa79a86SQuentin Monnet * descriptors. The *map* is an array whose size is the number of 15927aa79a86SQuentin Monnet * available CPUs, and each cell contains a value relative to one 15937aa79a86SQuentin Monnet * CPU. The value to retrieve is indicated by *flags*, that 15947aa79a86SQuentin Monnet * contains the index of the CPU to look up, masked with 15957aa79a86SQuentin Monnet * **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to 15967aa79a86SQuentin Monnet * **BPF_F_CURRENT_CPU** to indicate that the value for the 15977aa79a86SQuentin Monnet * current CPU should be retrieved. 15987aa79a86SQuentin Monnet * 15997aa79a86SQuentin Monnet * This helper behaves in a way close to 16007aa79a86SQuentin Monnet * **bpf_perf_event_read**\ () helper, save that instead of 16017aa79a86SQuentin Monnet * just returning the value observed, it fills the *buf* 16027aa79a86SQuentin Monnet * structure. This allows for additional data to be retrieved: in 16037aa79a86SQuentin Monnet * particular, the enabled and running times (in *buf*\ 16047aa79a86SQuentin Monnet * **->enabled** and *buf*\ **->running**, respectively) are 16057aa79a86SQuentin Monnet * copied. In general, **bpf_perf_event_read_value**\ () is 16067aa79a86SQuentin Monnet * recommended over **bpf_perf_event_read**\ (), which has some 16077aa79a86SQuentin Monnet * ABI issues and provides fewer functionalities. 16087aa79a86SQuentin Monnet * 16097aa79a86SQuentin Monnet * These values are interesting, because hardware PMU (Performance 16107aa79a86SQuentin Monnet * Monitoring Unit) counters are limited resources. When there are 16117aa79a86SQuentin Monnet * more PMU based perf events opened than available counters, 16127aa79a86SQuentin Monnet * kernel will multiplex these events so each event gets certain 16137aa79a86SQuentin Monnet * percentage (but not all) of the PMU time. In case that 16147aa79a86SQuentin Monnet * multiplexing happens, the number of samples or counter value 16157aa79a86SQuentin Monnet * will not reflect the case compared to when no multiplexing 16167aa79a86SQuentin Monnet * occurs. This makes comparison between different runs difficult. 16177aa79a86SQuentin Monnet * Typically, the counter value should be normalized before 16187aa79a86SQuentin Monnet * comparing to other experiments. The usual normalization is done 16197aa79a86SQuentin Monnet * as follows. 16207aa79a86SQuentin Monnet * 16217aa79a86SQuentin Monnet * :: 16227aa79a86SQuentin Monnet * 16237aa79a86SQuentin Monnet * normalized_counter = counter * t_enabled / t_running 16247aa79a86SQuentin Monnet * 16257aa79a86SQuentin Monnet * Where t_enabled is the time enabled for event and t_running is 16267aa79a86SQuentin Monnet * the time running for event since last normalization. The 16277aa79a86SQuentin Monnet * enabled and running times are accumulated since the perf event 16287aa79a86SQuentin Monnet * open. To achieve scaling factor between two invocations of an 16297aa79a86SQuentin Monnet * eBPF program, users can can use CPU id as the key (which is 16307aa79a86SQuentin Monnet * typical for perf array usage model) to remember the previous 16317aa79a86SQuentin Monnet * value and do the calculation inside the eBPF program. 16327aa79a86SQuentin Monnet * Return 16337aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 16347aa79a86SQuentin Monnet * 1635a3ef8e9aSAndrey Ignatov * int bpf_perf_prog_read_value(struct bpf_perf_event_data *ctx, struct bpf_perf_event_value *buf, u32 buf_size) 16367aa79a86SQuentin Monnet * Description 16377aa79a86SQuentin Monnet * For en eBPF program attached to a perf event, retrieve the 16387aa79a86SQuentin Monnet * value of the event counter associated to *ctx* and store it in 16397aa79a86SQuentin Monnet * the structure pointed by *buf* and of size *buf_size*. Enabled 16407aa79a86SQuentin Monnet * and running times are also stored in the structure (see 16417aa79a86SQuentin Monnet * description of helper **bpf_perf_event_read_value**\ () for 16427aa79a86SQuentin Monnet * more details). 16437aa79a86SQuentin Monnet * Return 16447aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 16457aa79a86SQuentin Monnet * 1646a3ef8e9aSAndrey Ignatov * int bpf_getsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, char *optval, int optlen) 16477aa79a86SQuentin Monnet * Description 16487aa79a86SQuentin Monnet * Emulate a call to **getsockopt()** on the socket associated to 16497aa79a86SQuentin Monnet * *bpf_socket*, which must be a full socket. The *level* at 16507aa79a86SQuentin Monnet * which the option resides and the name *optname* of the option 16517aa79a86SQuentin Monnet * must be specified, see **getsockopt(2)** for more information. 16527aa79a86SQuentin Monnet * The retrieved value is stored in the structure pointed by 16537aa79a86SQuentin Monnet * *opval* and of length *optlen*. 16547aa79a86SQuentin Monnet * 16557aa79a86SQuentin Monnet * This helper actually implements a subset of **getsockopt()**. 16567aa79a86SQuentin Monnet * It supports the following *level*\ s: 16577aa79a86SQuentin Monnet * 16587aa79a86SQuentin Monnet * * **IPPROTO_TCP**, which supports *optname* 16597aa79a86SQuentin Monnet * **TCP_CONGESTION**. 16607aa79a86SQuentin Monnet * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. 16617aa79a86SQuentin Monnet * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**. 16627aa79a86SQuentin Monnet * Return 16637aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 16647aa79a86SQuentin Monnet * 16657aa79a86SQuentin Monnet * int bpf_override_return(struct pt_reg *regs, u64 rc) 16667aa79a86SQuentin Monnet * Description 16677aa79a86SQuentin Monnet * Used for error injection, this helper uses kprobes to override 16687aa79a86SQuentin Monnet * the return value of the probed function, and to set it to *rc*. 16697aa79a86SQuentin Monnet * The first argument is the context *regs* on which the kprobe 16707aa79a86SQuentin Monnet * works. 16717aa79a86SQuentin Monnet * 16727aa79a86SQuentin Monnet * This helper works by setting setting the PC (program counter) 16737aa79a86SQuentin Monnet * to an override function which is run in place of the original 16747aa79a86SQuentin Monnet * probed function. This means the probed function is not run at 16757aa79a86SQuentin Monnet * all. The replacement function just returns with the required 16767aa79a86SQuentin Monnet * value. 16777aa79a86SQuentin Monnet * 16787aa79a86SQuentin Monnet * This helper has security implications, and thus is subject to 16797aa79a86SQuentin Monnet * restrictions. It is only available if the kernel was compiled 16807aa79a86SQuentin Monnet * with the **CONFIG_BPF_KPROBE_OVERRIDE** configuration 16817aa79a86SQuentin Monnet * option, and in this case it only works on functions tagged with 16827aa79a86SQuentin Monnet * **ALLOW_ERROR_INJECTION** in the kernel code. 16837aa79a86SQuentin Monnet * 16847aa79a86SQuentin Monnet * Also, the helper is only available for the architectures having 16857aa79a86SQuentin Monnet * the CONFIG_FUNCTION_ERROR_INJECTION option. As of this writing, 16867aa79a86SQuentin Monnet * x86 architecture is the only one to support this feature. 16877aa79a86SQuentin Monnet * Return 16887aa79a86SQuentin Monnet * 0 16897aa79a86SQuentin Monnet * 1690a3ef8e9aSAndrey Ignatov * int bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *bpf_sock, int argval) 16917aa79a86SQuentin Monnet * Description 16927aa79a86SQuentin Monnet * Attempt to set the value of the **bpf_sock_ops_cb_flags** field 16937aa79a86SQuentin Monnet * for the full TCP socket associated to *bpf_sock_ops* to 16947aa79a86SQuentin Monnet * *argval*. 16957aa79a86SQuentin Monnet * 16967aa79a86SQuentin Monnet * The primary use of this field is to determine if there should 16977aa79a86SQuentin Monnet * be calls to eBPF programs of type 16987aa79a86SQuentin Monnet * **BPF_PROG_TYPE_SOCK_OPS** at various points in the TCP 16997aa79a86SQuentin Monnet * code. A program of the same type can change its value, per 17007aa79a86SQuentin Monnet * connection and as necessary, when the connection is 17017aa79a86SQuentin Monnet * established. This field is directly accessible for reading, but 17027aa79a86SQuentin Monnet * this helper must be used for updates in order to return an 17037aa79a86SQuentin Monnet * error if an eBPF program tries to set a callback that is not 17047aa79a86SQuentin Monnet * supported in the current kernel. 17057aa79a86SQuentin Monnet * 17067aa79a86SQuentin Monnet * The supported callback values that *argval* can combine are: 17077aa79a86SQuentin Monnet * 17087aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_RTO_CB_FLAG** (retransmission time out) 17097aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_RETRANS_CB_FLAG** (retransmission) 17107aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_STATE_CB_FLAG** (TCP state change) 17117aa79a86SQuentin Monnet * 17127aa79a86SQuentin Monnet * Here are some examples of where one could call such eBPF 17137aa79a86SQuentin Monnet * program: 17147aa79a86SQuentin Monnet * 17157aa79a86SQuentin Monnet * * When RTO fires. 17167aa79a86SQuentin Monnet * * When a packet is retransmitted. 17177aa79a86SQuentin Monnet * * When the connection terminates. 17187aa79a86SQuentin Monnet * * When a packet is sent. 17197aa79a86SQuentin Monnet * * When a packet is received. 17207aa79a86SQuentin Monnet * Return 17217aa79a86SQuentin Monnet * Code **-EINVAL** if the socket is not a full TCP socket; 17227aa79a86SQuentin Monnet * otherwise, a positive number containing the bits that could not 17237aa79a86SQuentin Monnet * be set is returned (which comes down to 0 if all bits were set 17247aa79a86SQuentin Monnet * as required). 17257aa79a86SQuentin Monnet * 1726ab127040SQuentin Monnet * int bpf_msg_redirect_map(struct sk_msg_buff *msg, struct bpf_map *map, u32 key, u64 flags) 1727ab127040SQuentin Monnet * Description 1728ab127040SQuentin Monnet * This helper is used in programs implementing policies at the 1729ab127040SQuentin Monnet * socket level. If the message *msg* is allowed to pass (i.e. if 1730ab127040SQuentin Monnet * the verdict eBPF program returns **SK_PASS**), redirect it to 1731ab127040SQuentin Monnet * the socket referenced by *map* (of type 1732ab127040SQuentin Monnet * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and 1733ab127040SQuentin Monnet * egress interfaces can be used for redirection. The 1734ab127040SQuentin Monnet * **BPF_F_INGRESS** value in *flags* is used to make the 1735ab127040SQuentin Monnet * distinction (ingress path is selected if the flag is present, 1736ab127040SQuentin Monnet * egress path otherwise). This is the only flag supported for now. 1737ab127040SQuentin Monnet * Return 1738ab127040SQuentin Monnet * **SK_PASS** on success, or **SK_DROP** on error. 1739ab127040SQuentin Monnet * 1740ab127040SQuentin Monnet * int bpf_msg_apply_bytes(struct sk_msg_buff *msg, u32 bytes) 1741ab127040SQuentin Monnet * Description 1742ab127040SQuentin Monnet * For socket policies, apply the verdict of the eBPF program to 1743ab127040SQuentin Monnet * the next *bytes* (number of bytes) of message *msg*. 1744ab127040SQuentin Monnet * 1745ab127040SQuentin Monnet * For example, this helper can be used in the following cases: 1746ab127040SQuentin Monnet * 1747ab127040SQuentin Monnet * * A single **sendmsg**\ () or **sendfile**\ () system call 1748ab127040SQuentin Monnet * contains multiple logical messages that the eBPF program is 1749ab127040SQuentin Monnet * supposed to read and for which it should apply a verdict. 1750ab127040SQuentin Monnet * * An eBPF program only cares to read the first *bytes* of a 1751ab127040SQuentin Monnet * *msg*. If the message has a large payload, then setting up 1752ab127040SQuentin Monnet * and calling the eBPF program repeatedly for all bytes, even 1753ab127040SQuentin Monnet * though the verdict is already known, would create unnecessary 1754ab127040SQuentin Monnet * overhead. 1755ab127040SQuentin Monnet * 1756ab127040SQuentin Monnet * When called from within an eBPF program, the helper sets a 1757ab127040SQuentin Monnet * counter internal to the BPF infrastructure, that is used to 1758ab127040SQuentin Monnet * apply the last verdict to the next *bytes*. If *bytes* is 1759ab127040SQuentin Monnet * smaller than the current data being processed from a 1760ab127040SQuentin Monnet * **sendmsg**\ () or **sendfile**\ () system call, the first 1761ab127040SQuentin Monnet * *bytes* will be sent and the eBPF program will be re-run with 1762ab127040SQuentin Monnet * the pointer for start of data pointing to byte number *bytes* 1763ab127040SQuentin Monnet * **+ 1**. If *bytes* is larger than the current data being 1764ab127040SQuentin Monnet * processed, then the eBPF verdict will be applied to multiple 1765ab127040SQuentin Monnet * **sendmsg**\ () or **sendfile**\ () calls until *bytes* are 1766ab127040SQuentin Monnet * consumed. 1767ab127040SQuentin Monnet * 1768ab127040SQuentin Monnet * Note that if a socket closes with the internal counter holding 1769ab127040SQuentin Monnet * a non-zero value, this is not a problem because data is not 1770ab127040SQuentin Monnet * being buffered for *bytes* and is sent as it is received. 1771ab127040SQuentin Monnet * Return 1772ab127040SQuentin Monnet * 0 1773ab127040SQuentin Monnet * 1774ab127040SQuentin Monnet * int bpf_msg_cork_bytes(struct sk_msg_buff *msg, u32 bytes) 1775ab127040SQuentin Monnet * Description 1776ab127040SQuentin Monnet * For socket policies, prevent the execution of the verdict eBPF 1777ab127040SQuentin Monnet * program for message *msg* until *bytes* (byte number) have been 1778ab127040SQuentin Monnet * accumulated. 1779ab127040SQuentin Monnet * 1780ab127040SQuentin Monnet * This can be used when one needs a specific number of bytes 1781ab127040SQuentin Monnet * before a verdict can be assigned, even if the data spans 1782ab127040SQuentin Monnet * multiple **sendmsg**\ () or **sendfile**\ () calls. The extreme 1783ab127040SQuentin Monnet * case would be a user calling **sendmsg**\ () repeatedly with 1784ab127040SQuentin Monnet * 1-byte long message segments. Obviously, this is bad for 1785ab127040SQuentin Monnet * performance, but it is still valid. If the eBPF program needs 1786ab127040SQuentin Monnet * *bytes* bytes to validate a header, this helper can be used to 1787ab127040SQuentin Monnet * prevent the eBPF program to be called again until *bytes* have 1788ab127040SQuentin Monnet * been accumulated. 1789ab127040SQuentin Monnet * Return 1790ab127040SQuentin Monnet * 0 1791ab127040SQuentin Monnet * 1792ab127040SQuentin Monnet * int bpf_msg_pull_data(struct sk_msg_buff *msg, u32 start, u32 end, u64 flags) 1793ab127040SQuentin Monnet * Description 1794ab127040SQuentin Monnet * For socket policies, pull in non-linear data from user space 1795ab127040SQuentin Monnet * for *msg* and set pointers *msg*\ **->data** and *msg*\ 1796ab127040SQuentin Monnet * **->data_end** to *start* and *end* bytes offsets into *msg*, 1797ab127040SQuentin Monnet * respectively. 1798ab127040SQuentin Monnet * 1799ab127040SQuentin Monnet * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a 1800ab127040SQuentin Monnet * *msg* it can only parse data that the (**data**, **data_end**) 1801ab127040SQuentin Monnet * pointers have already consumed. For **sendmsg**\ () hooks this 1802ab127040SQuentin Monnet * is likely the first scatterlist element. But for calls relying 1803ab127040SQuentin Monnet * on the **sendpage** handler (e.g. **sendfile**\ ()) this will 1804ab127040SQuentin Monnet * be the range (**0**, **0**) because the data is shared with 1805ab127040SQuentin Monnet * user space and by default the objective is to avoid allowing 1806ab127040SQuentin Monnet * user space to modify data while (or after) eBPF verdict is 1807ab127040SQuentin Monnet * being decided. This helper can be used to pull in data and to 1808ab127040SQuentin Monnet * set the start and end pointer to given values. Data will be 1809ab127040SQuentin Monnet * copied if necessary (i.e. if data was not linear and if start 1810ab127040SQuentin Monnet * and end pointers do not point to the same chunk). 1811ab127040SQuentin Monnet * 1812ab127040SQuentin Monnet * A call to this helper is susceptible to change the underlaying 1813ab127040SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1814ab127040SQuentin Monnet * previously done by the verifier are invalidated and must be 1815ab127040SQuentin Monnet * performed again, if the helper is used in combination with 1816ab127040SQuentin Monnet * direct packet access. 1817ab127040SQuentin Monnet * 1818ab127040SQuentin Monnet * All values for *flags* are reserved for future usage, and must 1819ab127040SQuentin Monnet * be left at zero. 1820ab127040SQuentin Monnet * Return 1821ab127040SQuentin Monnet * 0 on success, or a negative error in case of failure. 1822ab127040SQuentin Monnet * 1823a3ef8e9aSAndrey Ignatov * int bpf_bind(struct bpf_sock_addr *ctx, struct sockaddr *addr, int addr_len) 18247aa79a86SQuentin Monnet * Description 18257aa79a86SQuentin Monnet * Bind the socket associated to *ctx* to the address pointed by 18267aa79a86SQuentin Monnet * *addr*, of length *addr_len*. This allows for making outgoing 18277aa79a86SQuentin Monnet * connection from the desired IP address, which can be useful for 18287aa79a86SQuentin Monnet * example when all processes inside a cgroup should use one 18297aa79a86SQuentin Monnet * single IP address on a host that has multiple IP configured. 18307aa79a86SQuentin Monnet * 18317aa79a86SQuentin Monnet * This helper works for IPv4 and IPv6, TCP and UDP sockets. The 18327aa79a86SQuentin Monnet * domain (*addr*\ **->sa_family**) must be **AF_INET** (or 18337aa79a86SQuentin Monnet * **AF_INET6**). Looking for a free port to bind to can be 18347aa79a86SQuentin Monnet * expensive, therefore binding to port is not permitted by the 18357aa79a86SQuentin Monnet * helper: *addr*\ **->sin_port** (or **sin6_port**, respectively) 18367aa79a86SQuentin Monnet * must be set to zero. 18377aa79a86SQuentin Monnet * Return 18387aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 18392d020dd7SQuentin Monnet * 18402d020dd7SQuentin Monnet * int bpf_xdp_adjust_tail(struct xdp_buff *xdp_md, int delta) 18412d020dd7SQuentin Monnet * Description 18422d020dd7SQuentin Monnet * Adjust (move) *xdp_md*\ **->data_end** by *delta* bytes. It is 18432d020dd7SQuentin Monnet * only possible to shrink the packet as of this writing, 18442d020dd7SQuentin Monnet * therefore *delta* must be a negative integer. 18452d020dd7SQuentin Monnet * 18462d020dd7SQuentin Monnet * A call to this helper is susceptible to change the underlaying 18472d020dd7SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 18482d020dd7SQuentin Monnet * previously done by the verifier are invalidated and must be 18492d020dd7SQuentin Monnet * performed again, if the helper is used in combination with 18502d020dd7SQuentin Monnet * direct packet access. 18512d020dd7SQuentin Monnet * Return 18522d020dd7SQuentin Monnet * 0 on success, or a negative error in case of failure. 18532d020dd7SQuentin Monnet * 18542d020dd7SQuentin Monnet * int bpf_skb_get_xfrm_state(struct sk_buff *skb, u32 index, struct bpf_xfrm_state *xfrm_state, u32 size, u64 flags) 18552d020dd7SQuentin Monnet * Description 18562d020dd7SQuentin Monnet * Retrieve the XFRM state (IP transform framework, see also 18572d020dd7SQuentin Monnet * **ip-xfrm(8)**) at *index* in XFRM "security path" for *skb*. 18582d020dd7SQuentin Monnet * 18592d020dd7SQuentin Monnet * The retrieved value is stored in the **struct bpf_xfrm_state** 18602d020dd7SQuentin Monnet * pointed by *xfrm_state* and of length *size*. 18612d020dd7SQuentin Monnet * 18622d020dd7SQuentin Monnet * All values for *flags* are reserved for future usage, and must 18632d020dd7SQuentin Monnet * be left at zero. 18642d020dd7SQuentin Monnet * 18652d020dd7SQuentin Monnet * This helper is available only if the kernel was compiled with 18662d020dd7SQuentin Monnet * **CONFIG_XFRM** configuration option. 18672d020dd7SQuentin Monnet * Return 18682d020dd7SQuentin Monnet * 0 on success, or a negative error in case of failure. 1869c195651eSYonghong Song * 1870c195651eSYonghong Song * int bpf_get_stack(struct pt_regs *regs, void *buf, u32 size, u64 flags) 1871c195651eSYonghong Song * Description 1872c195651eSYonghong Song * Return a user or a kernel stack in bpf program provided buffer. 1873c195651eSYonghong Song * To achieve this, the helper needs *ctx*, which is a pointer 1874c195651eSYonghong Song * to the context on which the tracing program is executed. 1875c195651eSYonghong Song * To store the stacktrace, the bpf program provides *buf* with 1876c195651eSYonghong Song * a nonnegative *size*. 1877c195651eSYonghong Song * 1878c195651eSYonghong Song * The last argument, *flags*, holds the number of stack frames to 1879c195651eSYonghong Song * skip (from 0 to 255), masked with 1880c195651eSYonghong Song * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 1881c195651eSYonghong Song * the following flags: 1882c195651eSYonghong Song * 1883c195651eSYonghong Song * **BPF_F_USER_STACK** 1884c195651eSYonghong Song * Collect a user space stack instead of a kernel stack. 1885c195651eSYonghong Song * **BPF_F_USER_BUILD_ID** 1886c195651eSYonghong Song * Collect buildid+offset instead of ips for user stack, 1887c195651eSYonghong Song * only valid if **BPF_F_USER_STACK** is also specified. 1888c195651eSYonghong Song * 1889c195651eSYonghong Song * **bpf_get_stack**\ () can collect up to 1890c195651eSYonghong Song * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject 1891c195651eSYonghong Song * to sufficient large buffer size. Note that 1892c195651eSYonghong Song * this limit can be controlled with the **sysctl** program, and 1893c195651eSYonghong Song * that it should be manually increased in order to profile long 1894c195651eSYonghong Song * user stacks (such as stacks for Java programs). To do so, use: 1895c195651eSYonghong Song * 1896c195651eSYonghong Song * :: 1897c195651eSYonghong Song * 1898c195651eSYonghong Song * # sysctl kernel.perf_event_max_stack=<new value> 1899c195651eSYonghong Song * Return 19007a279e93SQuentin Monnet * A non-negative value equal to or less than *size* on success, 19017a279e93SQuentin Monnet * or a negative error in case of failure. 19024e1ec56cSDaniel Borkmann * 19032bae79d2SQuentin Monnet * int bpf_skb_load_bytes_relative(const struct sk_buff *skb, u32 offset, void *to, u32 len, u32 start_header) 19044e1ec56cSDaniel Borkmann * Description 19054e1ec56cSDaniel Borkmann * This helper is similar to **bpf_skb_load_bytes**\ () in that 19064e1ec56cSDaniel Borkmann * it provides an easy way to load *len* bytes from *offset* 19074e1ec56cSDaniel Borkmann * from the packet associated to *skb*, into the buffer pointed 19084e1ec56cSDaniel Borkmann * by *to*. The difference to **bpf_skb_load_bytes**\ () is that 19094e1ec56cSDaniel Borkmann * a fifth argument *start_header* exists in order to select a 19104e1ec56cSDaniel Borkmann * base offset to start from. *start_header* can be one of: 19114e1ec56cSDaniel Borkmann * 19124e1ec56cSDaniel Borkmann * **BPF_HDR_START_MAC** 19134e1ec56cSDaniel Borkmann * Base offset to load data from is *skb*'s mac header. 19144e1ec56cSDaniel Borkmann * **BPF_HDR_START_NET** 19154e1ec56cSDaniel Borkmann * Base offset to load data from is *skb*'s network header. 19164e1ec56cSDaniel Borkmann * 19174e1ec56cSDaniel Borkmann * In general, "direct packet access" is the preferred method to 19184e1ec56cSDaniel Borkmann * access packet data, however, this helper is in particular useful 19194e1ec56cSDaniel Borkmann * in socket filters where *skb*\ **->data** does not always point 19204e1ec56cSDaniel Borkmann * to the start of the mac header and where "direct packet access" 19214e1ec56cSDaniel Borkmann * is not available. 19224e1ec56cSDaniel Borkmann * Return 19234e1ec56cSDaniel Borkmann * 0 on success, or a negative error in case of failure. 19244e1ec56cSDaniel Borkmann * 192587f5fc7eSDavid Ahern * int bpf_fib_lookup(void *ctx, struct bpf_fib_lookup *params, int plen, u32 flags) 192687f5fc7eSDavid Ahern * Description 192787f5fc7eSDavid Ahern * Do FIB lookup in kernel tables using parameters in *params*. 192887f5fc7eSDavid Ahern * If lookup is successful and result shows packet is to be 192987f5fc7eSDavid Ahern * forwarded, the neighbor tables are searched for the nexthop. 193087f5fc7eSDavid Ahern * If successful (ie., FIB lookup shows forwarding and nexthop 1931fa898d76SDavid Ahern * is resolved), the nexthop address is returned in ipv4_dst 1932fa898d76SDavid Ahern * or ipv6_dst based on family, smac is set to mac address of 1933fa898d76SDavid Ahern * egress device, dmac is set to nexthop mac address, rt_metric 19344c79579bSDavid Ahern * is set to metric from route (IPv4/IPv6 only), and ifindex 19354c79579bSDavid Ahern * is set to the device index of the nexthop from the FIB lookup. 193687f5fc7eSDavid Ahern * 193787f5fc7eSDavid Ahern * *plen* argument is the size of the passed in struct. 19387a279e93SQuentin Monnet * *flags* argument can be a combination of one or more of the 19397a279e93SQuentin Monnet * following values: 194087f5fc7eSDavid Ahern * 19417a279e93SQuentin Monnet * **BPF_FIB_LOOKUP_DIRECT** 19427a279e93SQuentin Monnet * Do a direct table lookup vs full lookup using FIB 19437a279e93SQuentin Monnet * rules. 19447a279e93SQuentin Monnet * **BPF_FIB_LOOKUP_OUTPUT** 19457a279e93SQuentin Monnet * Perform lookup from an egress perspective (default is 19467a279e93SQuentin Monnet * ingress). 194787f5fc7eSDavid Ahern * 194887f5fc7eSDavid Ahern * *ctx* is either **struct xdp_md** for XDP programs or 194987f5fc7eSDavid Ahern * **struct sk_buff** tc cls_act programs. 195087f5fc7eSDavid Ahern * Return 19514c79579bSDavid Ahern * * < 0 if any input argument is invalid 19524c79579bSDavid Ahern * * 0 on success (packet is forwarded, nexthop neighbor exists) 19534c79579bSDavid Ahern * * > 0 one of **BPF_FIB_LKUP_RET_** codes explaining why the 19542bae79d2SQuentin Monnet * packet is not forwarded or needs assist from full stack 195581110384SJohn Fastabend * 195681110384SJohn Fastabend * int bpf_sock_hash_update(struct bpf_sock_ops_kern *skops, struct bpf_map *map, void *key, u64 flags) 195781110384SJohn Fastabend * Description 195881110384SJohn Fastabend * Add an entry to, or update a sockhash *map* referencing sockets. 195981110384SJohn Fastabend * The *skops* is used as a new value for the entry associated to 196081110384SJohn Fastabend * *key*. *flags* is one of: 196181110384SJohn Fastabend * 196281110384SJohn Fastabend * **BPF_NOEXIST** 196381110384SJohn Fastabend * The entry for *key* must not exist in the map. 196481110384SJohn Fastabend * **BPF_EXIST** 196581110384SJohn Fastabend * The entry for *key* must already exist in the map. 196681110384SJohn Fastabend * **BPF_ANY** 196781110384SJohn Fastabend * No condition on the existence of the entry for *key*. 196881110384SJohn Fastabend * 196981110384SJohn Fastabend * If the *map* has eBPF programs (parser and verdict), those will 197081110384SJohn Fastabend * be inherited by the socket being added. If the socket is 197181110384SJohn Fastabend * already attached to eBPF programs, this results in an error. 197281110384SJohn Fastabend * Return 197381110384SJohn Fastabend * 0 on success, or a negative error in case of failure. 197481110384SJohn Fastabend * 197581110384SJohn Fastabend * int bpf_msg_redirect_hash(struct sk_msg_buff *msg, struct bpf_map *map, void *key, u64 flags) 197681110384SJohn Fastabend * Description 197781110384SJohn Fastabend * This helper is used in programs implementing policies at the 197881110384SJohn Fastabend * socket level. If the message *msg* is allowed to pass (i.e. if 197981110384SJohn Fastabend * the verdict eBPF program returns **SK_PASS**), redirect it to 198081110384SJohn Fastabend * the socket referenced by *map* (of type 198181110384SJohn Fastabend * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and 198281110384SJohn Fastabend * egress interfaces can be used for redirection. The 198381110384SJohn Fastabend * **BPF_F_INGRESS** value in *flags* is used to make the 198481110384SJohn Fastabend * distinction (ingress path is selected if the flag is present, 198581110384SJohn Fastabend * egress path otherwise). This is the only flag supported for now. 198681110384SJohn Fastabend * Return 198781110384SJohn Fastabend * **SK_PASS** on success, or **SK_DROP** on error. 198881110384SJohn Fastabend * 198981110384SJohn Fastabend * int bpf_sk_redirect_hash(struct sk_buff *skb, struct bpf_map *map, void *key, u64 flags) 199081110384SJohn Fastabend * Description 199181110384SJohn Fastabend * This helper is used in programs implementing policies at the 199281110384SJohn Fastabend * skb socket level. If the sk_buff *skb* is allowed to pass (i.e. 199381110384SJohn Fastabend * if the verdeict eBPF program returns **SK_PASS**), redirect it 199481110384SJohn Fastabend * to the socket referenced by *map* (of type 199581110384SJohn Fastabend * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and 199681110384SJohn Fastabend * egress interfaces can be used for redirection. The 199781110384SJohn Fastabend * **BPF_F_INGRESS** value in *flags* is used to make the 199881110384SJohn Fastabend * distinction (ingress path is selected if the flag is present, 199981110384SJohn Fastabend * egress otherwise). This is the only flag supported for now. 200081110384SJohn Fastabend * Return 200181110384SJohn Fastabend * **SK_PASS** on success, or **SK_DROP** on error. 2002fe94cc29SMathieu Xhonneux * 2003fe94cc29SMathieu Xhonneux * int bpf_lwt_push_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) 2004fe94cc29SMathieu Xhonneux * Description 2005fe94cc29SMathieu Xhonneux * Encapsulate the packet associated to *skb* within a Layer 3 2006fe94cc29SMathieu Xhonneux * protocol header. This header is provided in the buffer at 2007fe94cc29SMathieu Xhonneux * address *hdr*, with *len* its size in bytes. *type* indicates 2008fe94cc29SMathieu Xhonneux * the protocol of the header and can be one of: 2009fe94cc29SMathieu Xhonneux * 2010fe94cc29SMathieu Xhonneux * **BPF_LWT_ENCAP_SEG6** 2011fe94cc29SMathieu Xhonneux * IPv6 encapsulation with Segment Routing Header 2012fe94cc29SMathieu Xhonneux * (**struct ipv6_sr_hdr**). *hdr* only contains the SRH, 2013fe94cc29SMathieu Xhonneux * the IPv6 header is computed by the kernel. 2014fe94cc29SMathieu Xhonneux * **BPF_LWT_ENCAP_SEG6_INLINE** 2015fe94cc29SMathieu Xhonneux * Only works if *skb* contains an IPv6 packet. Insert a 2016fe94cc29SMathieu Xhonneux * Segment Routing Header (**struct ipv6_sr_hdr**) inside 2017fe94cc29SMathieu Xhonneux * the IPv6 header. 2018fe94cc29SMathieu Xhonneux * 2019fe94cc29SMathieu Xhonneux * A call to this helper is susceptible to change the underlaying 2020fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2021fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2022fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2023fe94cc29SMathieu Xhonneux * direct packet access. 2024fe94cc29SMathieu Xhonneux * Return 2025fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2026fe94cc29SMathieu Xhonneux * 2027fe94cc29SMathieu Xhonneux * int bpf_lwt_seg6_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len) 2028fe94cc29SMathieu Xhonneux * Description 2029fe94cc29SMathieu Xhonneux * Store *len* bytes from address *from* into the packet 2030fe94cc29SMathieu Xhonneux * associated to *skb*, at *offset*. Only the flags, tag and TLVs 2031fe94cc29SMathieu Xhonneux * inside the outermost IPv6 Segment Routing Header can be 2032fe94cc29SMathieu Xhonneux * modified through this helper. 2033fe94cc29SMathieu Xhonneux * 2034fe94cc29SMathieu Xhonneux * A call to this helper is susceptible to change the underlaying 2035fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2036fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2037fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2038fe94cc29SMathieu Xhonneux * direct packet access. 2039fe94cc29SMathieu Xhonneux * Return 2040fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2041fe94cc29SMathieu Xhonneux * 2042fe94cc29SMathieu Xhonneux * int bpf_lwt_seg6_adjust_srh(struct sk_buff *skb, u32 offset, s32 delta) 2043fe94cc29SMathieu Xhonneux * Description 2044fe94cc29SMathieu Xhonneux * Adjust the size allocated to TLVs in the outermost IPv6 2045fe94cc29SMathieu Xhonneux * Segment Routing Header contained in the packet associated to 2046fe94cc29SMathieu Xhonneux * *skb*, at position *offset* by *delta* bytes. Only offsets 2047fe94cc29SMathieu Xhonneux * after the segments are accepted. *delta* can be as well 2048fe94cc29SMathieu Xhonneux * positive (growing) as negative (shrinking). 2049fe94cc29SMathieu Xhonneux * 2050fe94cc29SMathieu Xhonneux * A call to this helper is susceptible to change the underlaying 2051fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2052fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2053fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2054fe94cc29SMathieu Xhonneux * direct packet access. 2055fe94cc29SMathieu Xhonneux * Return 2056fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2057fe94cc29SMathieu Xhonneux * 2058fe94cc29SMathieu Xhonneux * int bpf_lwt_seg6_action(struct sk_buff *skb, u32 action, void *param, u32 param_len) 2059fe94cc29SMathieu Xhonneux * Description 2060fe94cc29SMathieu Xhonneux * Apply an IPv6 Segment Routing action of type *action* to the 2061fe94cc29SMathieu Xhonneux * packet associated to *skb*. Each action takes a parameter 2062fe94cc29SMathieu Xhonneux * contained at address *param*, and of length *param_len* bytes. 2063fe94cc29SMathieu Xhonneux * *action* can be one of: 2064fe94cc29SMathieu Xhonneux * 2065fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_X** 2066fe94cc29SMathieu Xhonneux * End.X action: Endpoint with Layer-3 cross-connect. 2067fe94cc29SMathieu Xhonneux * Type of *param*: **struct in6_addr**. 2068fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_T** 2069fe94cc29SMathieu Xhonneux * End.T action: Endpoint with specific IPv6 table lookup. 2070fe94cc29SMathieu Xhonneux * Type of *param*: **int**. 2071fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_B6** 2072fe94cc29SMathieu Xhonneux * End.B6 action: Endpoint bound to an SRv6 policy. 2073fe94cc29SMathieu Xhonneux * Type of param: **struct ipv6_sr_hdr**. 2074fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_B6_ENCAP** 2075fe94cc29SMathieu Xhonneux * End.B6.Encap action: Endpoint bound to an SRv6 2076fe94cc29SMathieu Xhonneux * encapsulation policy. 2077fe94cc29SMathieu Xhonneux * Type of param: **struct ipv6_sr_hdr**. 2078fe94cc29SMathieu Xhonneux * 2079fe94cc29SMathieu Xhonneux * A call to this helper is susceptible to change the underlaying 2080fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2081fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2082fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2083fe94cc29SMathieu Xhonneux * direct packet access. 2084fe94cc29SMathieu Xhonneux * Return 2085fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2086f4364dcfSSean Young * 2087f4364dcfSSean Young * int bpf_rc_keydown(void *ctx, u32 protocol, u64 scancode, u32 toggle) 2088f4364dcfSSean Young * Description 2089f4364dcfSSean Young * This helper is used in programs implementing IR decoding, to 2090f4364dcfSSean Young * report a successfully decoded key press with *scancode*, 2091f4364dcfSSean Young * *toggle* value in the given *protocol*. The scancode will be 2092f4364dcfSSean Young * translated to a keycode using the rc keymap, and reported as 2093f4364dcfSSean Young * an input key down event. After a period a key up event is 2094f4364dcfSSean Young * generated. This period can be extended by calling either 209590b1023fSQuentin Monnet * **bpf_rc_keydown**\ () again with the same values, or calling 209690b1023fSQuentin Monnet * **bpf_rc_repeat**\ (). 2097f4364dcfSSean Young * 2098f4364dcfSSean Young * Some protocols include a toggle bit, in case the button was 2099f4364dcfSSean Young * released and pressed again between consecutive scancodes. 2100f4364dcfSSean Young * 2101f4364dcfSSean Young * The *ctx* should point to the lirc sample as passed into 2102f4364dcfSSean Young * the program. 2103f4364dcfSSean Young * 2104f4364dcfSSean Young * The *protocol* is the decoded protocol number (see 2105f4364dcfSSean Young * **enum rc_proto** for some predefined values). 2106f4364dcfSSean Young * 2107f4364dcfSSean Young * This helper is only available is the kernel was compiled with 2108f4364dcfSSean Young * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 2109f4364dcfSSean Young * "**y**". 2110f4364dcfSSean Young * Return 2111f4364dcfSSean Young * 0 2112f4364dcfSSean Young * 2113f4364dcfSSean Young * int bpf_rc_repeat(void *ctx) 2114f4364dcfSSean Young * Description 2115f4364dcfSSean Young * This helper is used in programs implementing IR decoding, to 2116f4364dcfSSean Young * report a successfully decoded repeat key message. This delays 2117f4364dcfSSean Young * the generation of a key up event for previously generated 2118f4364dcfSSean Young * key down event. 2119f4364dcfSSean Young * 2120f4364dcfSSean Young * Some IR protocols like NEC have a special IR message for 2121f4364dcfSSean Young * repeating last button, for when a button is held down. 2122f4364dcfSSean Young * 2123f4364dcfSSean Young * The *ctx* should point to the lirc sample as passed into 2124f4364dcfSSean Young * the program. 2125f4364dcfSSean Young * 2126f4364dcfSSean Young * This helper is only available is the kernel was compiled with 2127f4364dcfSSean Young * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 2128f4364dcfSSean Young * "**y**". 2129f4364dcfSSean Young * Return 2130f4364dcfSSean Young * 0 2131cb20b08eSDaniel Borkmann * 2132cb20b08eSDaniel Borkmann * uint64_t bpf_skb_cgroup_id(struct sk_buff *skb) 2133cb20b08eSDaniel Borkmann * Description 2134cb20b08eSDaniel Borkmann * Return the cgroup v2 id of the socket associated with the *skb*. 2135cb20b08eSDaniel Borkmann * This is roughly similar to the **bpf_get_cgroup_classid**\ () 2136cb20b08eSDaniel Borkmann * helper for cgroup v1 by providing a tag resp. identifier that 2137cb20b08eSDaniel Borkmann * can be matched on or used for map lookups e.g. to implement 2138cb20b08eSDaniel Borkmann * policy. The cgroup v2 id of a given path in the hierarchy is 2139cb20b08eSDaniel Borkmann * exposed in user space through the f_handle API in order to get 2140cb20b08eSDaniel Borkmann * to the same 64-bit id. 2141cb20b08eSDaniel Borkmann * 2142cb20b08eSDaniel Borkmann * This helper can be used on TC egress path, but not on ingress, 2143cb20b08eSDaniel Borkmann * and is available only if the kernel was compiled with the 2144cb20b08eSDaniel Borkmann * **CONFIG_SOCK_CGROUP_DATA** configuration option. 2145cb20b08eSDaniel Borkmann * Return 2146cb20b08eSDaniel Borkmann * The id is returned or 0 in case the id could not be retrieved. 2147bf6fa2c8SYonghong Song * 214877236281SAndrey Ignatov * u64 bpf_skb_ancestor_cgroup_id(struct sk_buff *skb, int ancestor_level) 214977236281SAndrey Ignatov * Description 215077236281SAndrey Ignatov * Return id of cgroup v2 that is ancestor of cgroup associated 215177236281SAndrey Ignatov * with the *skb* at the *ancestor_level*. The root cgroup is at 215277236281SAndrey Ignatov * *ancestor_level* zero and each step down the hierarchy 215377236281SAndrey Ignatov * increments the level. If *ancestor_level* == level of cgroup 215477236281SAndrey Ignatov * associated with *skb*, then return value will be same as that 215577236281SAndrey Ignatov * of **bpf_skb_cgroup_id**\ (). 215677236281SAndrey Ignatov * 215777236281SAndrey Ignatov * The helper is useful to implement policies based on cgroups 215877236281SAndrey Ignatov * that are upper in hierarchy than immediate cgroup associated 215977236281SAndrey Ignatov * with *skb*. 216077236281SAndrey Ignatov * 216177236281SAndrey Ignatov * The format of returned id and helper limitations are same as in 216277236281SAndrey Ignatov * **bpf_skb_cgroup_id**\ (). 216377236281SAndrey Ignatov * Return 216477236281SAndrey Ignatov * The id is returned or 0 in case the id could not be retrieved. 216577236281SAndrey Ignatov * 2166bf6fa2c8SYonghong Song * u64 bpf_get_current_cgroup_id(void) 2167bf6fa2c8SYonghong Song * Return 2168bf6fa2c8SYonghong Song * A 64-bit integer containing the current cgroup id based 2169bf6fa2c8SYonghong Song * on the cgroup within which the current task is running. 2170cd339431SRoman Gushchin * 2171cd339431SRoman Gushchin * void* get_local_storage(void *map, u64 flags) 2172cd339431SRoman Gushchin * Description 2173cd339431SRoman Gushchin * Get the pointer to the local storage area. 2174cd339431SRoman Gushchin * The type and the size of the local storage is defined 2175cd339431SRoman Gushchin * by the *map* argument. 2176cd339431SRoman Gushchin * The *flags* meaning is specific for each map type, 2177cd339431SRoman Gushchin * and has to be 0 for cgroup local storage. 2178cd339431SRoman Gushchin * 217990b1023fSQuentin Monnet * Depending on the BPF program type, a local storage area 218090b1023fSQuentin Monnet * can be shared between multiple instances of the BPF program, 2181cd339431SRoman Gushchin * running simultaneously. 2182cd339431SRoman Gushchin * 2183cd339431SRoman Gushchin * A user should care about the synchronization by himself. 218490b1023fSQuentin Monnet * For example, by using the **BPF_STX_XADD** instruction to alter 2185cd339431SRoman Gushchin * the shared data. 2186cd339431SRoman Gushchin * Return 218790b1023fSQuentin Monnet * A pointer to the local storage area. 21882dbb9b9eSMartin KaFai Lau * 21892dbb9b9eSMartin KaFai Lau * int bpf_sk_select_reuseport(struct sk_reuseport_md *reuse, struct bpf_map *map, void *key, u64 flags) 21902dbb9b9eSMartin KaFai Lau * Description 219190b1023fSQuentin Monnet * Select a **SO_REUSEPORT** socket from a 219290b1023fSQuentin Monnet * **BPF_MAP_TYPE_REUSEPORT_ARRAY** *map*. 219390b1023fSQuentin Monnet * It checks the selected socket is matching the incoming 219490b1023fSQuentin Monnet * request in the socket buffer. 21952dbb9b9eSMartin KaFai Lau * Return 21962dbb9b9eSMartin KaFai Lau * 0 on success, or a negative error in case of failure. 21976acc9b43SJoe Stringer * 2198f71c6143SJoe Stringer * struct bpf_sock *bpf_sk_lookup_tcp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 21996acc9b43SJoe Stringer * Description 22006acc9b43SJoe Stringer * Look for TCP socket matching *tuple*, optionally in a child 22016acc9b43SJoe Stringer * network namespace *netns*. The return value must be checked, 220290b1023fSQuentin Monnet * and if non-**NULL**, released via **bpf_sk_release**\ (). 22036acc9b43SJoe Stringer * 22046acc9b43SJoe Stringer * The *ctx* should point to the context of the program, such as 22056acc9b43SJoe Stringer * the skb or socket (depending on the hook in use). This is used 22066acc9b43SJoe Stringer * to determine the base network namespace for the lookup. 22076acc9b43SJoe Stringer * 22086acc9b43SJoe Stringer * *tuple_size* must be one of: 22096acc9b43SJoe Stringer * 22106acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv4**) 22116acc9b43SJoe Stringer * Look for an IPv4 socket. 22126acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv6**) 22136acc9b43SJoe Stringer * Look for an IPv6 socket. 22146acc9b43SJoe Stringer * 2215f71c6143SJoe Stringer * If the *netns* is a negative signed 32-bit integer, then the 2216f71c6143SJoe Stringer * socket lookup table in the netns associated with the *ctx* will 2217f71c6143SJoe Stringer * will be used. For the TC hooks, this is the netns of the device 2218f71c6143SJoe Stringer * in the skb. For socket hooks, this is the netns of the socket. 2219f71c6143SJoe Stringer * If *netns* is any other signed 32-bit value greater than or 2220f71c6143SJoe Stringer * equal to zero then it specifies the ID of the netns relative to 2221f71c6143SJoe Stringer * the netns associated with the *ctx*. *netns* values beyond the 2222f71c6143SJoe Stringer * range of 32-bit integers are reserved for future use. 22236acc9b43SJoe Stringer * 22246acc9b43SJoe Stringer * All values for *flags* are reserved for future usage, and must 22256acc9b43SJoe Stringer * be left at zero. 22266acc9b43SJoe Stringer * 22276acc9b43SJoe Stringer * This helper is available only if the kernel was compiled with 22286acc9b43SJoe Stringer * **CONFIG_NET** configuration option. 22296acc9b43SJoe Stringer * Return 22300bd72117SDaniel Borkmann * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 22310bd72117SDaniel Borkmann * For sockets with reuseport option, the **struct bpf_sock** 22320bd72117SDaniel Borkmann * result is from **reuse->socks**\ [] using the hash of the tuple. 22336acc9b43SJoe Stringer * 2234f71c6143SJoe Stringer * struct bpf_sock *bpf_sk_lookup_udp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 22356acc9b43SJoe Stringer * Description 22366acc9b43SJoe Stringer * Look for UDP socket matching *tuple*, optionally in a child 22376acc9b43SJoe Stringer * network namespace *netns*. The return value must be checked, 223890b1023fSQuentin Monnet * and if non-**NULL**, released via **bpf_sk_release**\ (). 22396acc9b43SJoe Stringer * 22406acc9b43SJoe Stringer * The *ctx* should point to the context of the program, such as 22416acc9b43SJoe Stringer * the skb or socket (depending on the hook in use). This is used 22426acc9b43SJoe Stringer * to determine the base network namespace for the lookup. 22436acc9b43SJoe Stringer * 22446acc9b43SJoe Stringer * *tuple_size* must be one of: 22456acc9b43SJoe Stringer * 22466acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv4**) 22476acc9b43SJoe Stringer * Look for an IPv4 socket. 22486acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv6**) 22496acc9b43SJoe Stringer * Look for an IPv6 socket. 22506acc9b43SJoe Stringer * 2251f71c6143SJoe Stringer * If the *netns* is a negative signed 32-bit integer, then the 2252f71c6143SJoe Stringer * socket lookup table in the netns associated with the *ctx* will 2253f71c6143SJoe Stringer * will be used. For the TC hooks, this is the netns of the device 2254f71c6143SJoe Stringer * in the skb. For socket hooks, this is the netns of the socket. 2255f71c6143SJoe Stringer * If *netns* is any other signed 32-bit value greater than or 2256f71c6143SJoe Stringer * equal to zero then it specifies the ID of the netns relative to 2257f71c6143SJoe Stringer * the netns associated with the *ctx*. *netns* values beyond the 2258f71c6143SJoe Stringer * range of 32-bit integers are reserved for future use. 22596acc9b43SJoe Stringer * 22606acc9b43SJoe Stringer * All values for *flags* are reserved for future usage, and must 22616acc9b43SJoe Stringer * be left at zero. 22626acc9b43SJoe Stringer * 22636acc9b43SJoe Stringer * This helper is available only if the kernel was compiled with 22646acc9b43SJoe Stringer * **CONFIG_NET** configuration option. 22656acc9b43SJoe Stringer * Return 22660bd72117SDaniel Borkmann * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 22670bd72117SDaniel Borkmann * For sockets with reuseport option, the **struct bpf_sock** 22680bd72117SDaniel Borkmann * result is from **reuse->socks**\ [] using the hash of the tuple. 22696acc9b43SJoe Stringer * 227090b1023fSQuentin Monnet * int bpf_sk_release(struct bpf_sock *sock) 22716acc9b43SJoe Stringer * Description 227290b1023fSQuentin Monnet * Release the reference held by *sock*. *sock* must be a 227390b1023fSQuentin Monnet * non-**NULL** pointer that was returned from 227490b1023fSQuentin Monnet * **bpf_sk_lookup_xxx**\ (). 227590b1023fSQuentin Monnet * Return 227690b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 227790b1023fSQuentin Monnet * 227890b1023fSQuentin Monnet * int bpf_map_pop_elem(struct bpf_map *map, void *value) 227990b1023fSQuentin Monnet * Description 228090b1023fSQuentin Monnet * Pop an element from *map*. 228190b1023fSQuentin Monnet * Return 228290b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 228390b1023fSQuentin Monnet * 228490b1023fSQuentin Monnet * int bpf_map_peek_elem(struct bpf_map *map, void *value) 228590b1023fSQuentin Monnet * Description 228690b1023fSQuentin Monnet * Get an element from *map* without removing it. 22876acc9b43SJoe Stringer * Return 22886acc9b43SJoe Stringer * 0 on success, or a negative error in case of failure. 22896fff607eSJohn Fastabend * 22906fff607eSJohn Fastabend * int bpf_msg_push_data(struct sk_buff *skb, u32 start, u32 len, u64 flags) 22916fff607eSJohn Fastabend * Description 229290b1023fSQuentin Monnet * For socket policies, insert *len* bytes into *msg* at offset 22936fff607eSJohn Fastabend * *start*. 22946fff607eSJohn Fastabend * 22956fff607eSJohn Fastabend * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a 229690b1023fSQuentin Monnet * *msg* it may want to insert metadata or options into the *msg*. 22976fff607eSJohn Fastabend * This can later be read and used by any of the lower layer BPF 22986fff607eSJohn Fastabend * hooks. 22996fff607eSJohn Fastabend * 23006fff607eSJohn Fastabend * This helper may fail if under memory pressure (a malloc 23016fff607eSJohn Fastabend * fails) in these cases BPF programs will get an appropriate 23026fff607eSJohn Fastabend * error and BPF programs will need to handle them. 23036fff607eSJohn Fastabend * Return 23046fff607eSJohn Fastabend * 0 on success, or a negative error in case of failure. 23057246d8edSJohn Fastabend * 23067246d8edSJohn Fastabend * int bpf_msg_pop_data(struct sk_msg_buff *msg, u32 start, u32 pop, u64 flags) 23077246d8edSJohn Fastabend * Description 23087246d8edSJohn Fastabend * Will remove *pop* bytes from a *msg* starting at byte *start*. 23097246d8edSJohn Fastabend * This may result in **ENOMEM** errors under certain situations if 23107246d8edSJohn Fastabend * an allocation and copy are required due to a full ring buffer. 23117246d8edSJohn Fastabend * However, the helper will try to avoid doing the allocation 23127246d8edSJohn Fastabend * if possible. Other errors can occur if input parameters are 231390b1023fSQuentin Monnet * invalid either due to *start* byte not being valid part of *msg* 23147246d8edSJohn Fastabend * payload and/or *pop* value being to large. 23157246d8edSJohn Fastabend * Return 231690b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 231701d3240aSSean Young * 231801d3240aSSean Young * int bpf_rc_pointer_rel(void *ctx, s32 rel_x, s32 rel_y) 231901d3240aSSean Young * Description 232001d3240aSSean Young * This helper is used in programs implementing IR decoding, to 232101d3240aSSean Young * report a successfully decoded pointer movement. 232201d3240aSSean Young * 232301d3240aSSean Young * The *ctx* should point to the lirc sample as passed into 232401d3240aSSean Young * the program. 232501d3240aSSean Young * 232601d3240aSSean Young * This helper is only available is the kernel was compiled with 232701d3240aSSean Young * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 232801d3240aSSean Young * "**y**". 232901d3240aSSean Young * Return 233001d3240aSSean Young * 0 23317a4b28c6SDaniel Borkmann */ 2332ebb676daSThomas Graf #define __BPF_FUNC_MAPPER(FN) \ 2333ebb676daSThomas Graf FN(unspec), \ 2334ebb676daSThomas Graf FN(map_lookup_elem), \ 2335ebb676daSThomas Graf FN(map_update_elem), \ 2336ebb676daSThomas Graf FN(map_delete_elem), \ 2337ebb676daSThomas Graf FN(probe_read), \ 2338ebb676daSThomas Graf FN(ktime_get_ns), \ 2339ebb676daSThomas Graf FN(trace_printk), \ 2340ebb676daSThomas Graf FN(get_prandom_u32), \ 2341ebb676daSThomas Graf FN(get_smp_processor_id), \ 2342ebb676daSThomas Graf FN(skb_store_bytes), \ 2343ebb676daSThomas Graf FN(l3_csum_replace), \ 2344ebb676daSThomas Graf FN(l4_csum_replace), \ 2345ebb676daSThomas Graf FN(tail_call), \ 2346ebb676daSThomas Graf FN(clone_redirect), \ 2347ebb676daSThomas Graf FN(get_current_pid_tgid), \ 2348ebb676daSThomas Graf FN(get_current_uid_gid), \ 2349ebb676daSThomas Graf FN(get_current_comm), \ 2350ebb676daSThomas Graf FN(get_cgroup_classid), \ 2351ebb676daSThomas Graf FN(skb_vlan_push), \ 2352ebb676daSThomas Graf FN(skb_vlan_pop), \ 2353ebb676daSThomas Graf FN(skb_get_tunnel_key), \ 2354ebb676daSThomas Graf FN(skb_set_tunnel_key), \ 2355ebb676daSThomas Graf FN(perf_event_read), \ 2356ebb676daSThomas Graf FN(redirect), \ 2357ebb676daSThomas Graf FN(get_route_realm), \ 2358ebb676daSThomas Graf FN(perf_event_output), \ 2359ebb676daSThomas Graf FN(skb_load_bytes), \ 2360ebb676daSThomas Graf FN(get_stackid), \ 2361ebb676daSThomas Graf FN(csum_diff), \ 2362ebb676daSThomas Graf FN(skb_get_tunnel_opt), \ 2363ebb676daSThomas Graf FN(skb_set_tunnel_opt), \ 2364ebb676daSThomas Graf FN(skb_change_proto), \ 2365ebb676daSThomas Graf FN(skb_change_type), \ 2366ebb676daSThomas Graf FN(skb_under_cgroup), \ 2367ebb676daSThomas Graf FN(get_hash_recalc), \ 2368ebb676daSThomas Graf FN(get_current_task), \ 2369ebb676daSThomas Graf FN(probe_write_user), \ 2370ebb676daSThomas Graf FN(current_task_under_cgroup), \ 2371ebb676daSThomas Graf FN(skb_change_tail), \ 2372ebb676daSThomas Graf FN(skb_pull_data), \ 2373ebb676daSThomas Graf FN(csum_update), \ 2374ebb676daSThomas Graf FN(set_hash_invalid), \ 23753a0af8fdSThomas Graf FN(get_numa_node_id), \ 237617bedab2SMartin KaFai Lau FN(skb_change_head), \ 2377a5e8c070SGianluca Borello FN(xdp_adjust_head), \ 237891b8270fSChenbo Feng FN(probe_read_str), \ 23796acc5c29SChenbo Feng FN(get_socket_cookie), \ 2380ded092cdSDaniel Borkmann FN(get_socket_uid), \ 23818c4b4c7eSLawrence Brakmo FN(set_hash), \ 23822be7e212SDaniel Borkmann FN(setsockopt), \ 238397f91a7cSJohn Fastabend FN(skb_adjust_room), \ 2384174a79ffSJohn Fastabend FN(redirect_map), \ 2385174a79ffSJohn Fastabend FN(sk_redirect_map), \ 2386174a79ffSJohn Fastabend FN(sock_map_update), \ 2387908432caSYonghong Song FN(xdp_adjust_meta), \ 23884bebdc7aSYonghong Song FN(perf_event_read_value), \ 2389cd86d1fdSLawrence Brakmo FN(perf_prog_read_value), \ 23909802d865SJosef Bacik FN(getsockopt), \ 2391b13d8807SLawrence Brakmo FN(override_return), \ 23924f738adbSJohn Fastabend FN(sock_ops_cb_flags_set), \ 23932a100317SJohn Fastabend FN(msg_redirect_map), \ 239491843d54SJohn Fastabend FN(msg_apply_bytes), \ 2395015632bbSJohn Fastabend FN(msg_cork_bytes), \ 2396d74bad4eSAndrey Ignatov FN(msg_pull_data), \ 2397b32cc5b9SNikita V. Shirokov FN(bind), \ 239812bed760SEyal Birger FN(xdp_adjust_tail), \ 2399c195651eSYonghong Song FN(skb_get_xfrm_state), \ 24004e1ec56cSDaniel Borkmann FN(get_stack), \ 240187f5fc7eSDavid Ahern FN(skb_load_bytes_relative), \ 240281110384SJohn Fastabend FN(fib_lookup), \ 240381110384SJohn Fastabend FN(sock_hash_update), \ 240481110384SJohn Fastabend FN(msg_redirect_hash), \ 2405fe94cc29SMathieu Xhonneux FN(sk_redirect_hash), \ 2406fe94cc29SMathieu Xhonneux FN(lwt_push_encap), \ 2407fe94cc29SMathieu Xhonneux FN(lwt_seg6_store_bytes), \ 2408fe94cc29SMathieu Xhonneux FN(lwt_seg6_adjust_srh), \ 2409f4364dcfSSean Young FN(lwt_seg6_action), \ 2410f4364dcfSSean Young FN(rc_repeat), \ 2411cb20b08eSDaniel Borkmann FN(rc_keydown), \ 2412bf6fa2c8SYonghong Song FN(skb_cgroup_id), \ 2413cd339431SRoman Gushchin FN(get_current_cgroup_id), \ 24142dbb9b9eSMartin KaFai Lau FN(get_local_storage), \ 241577236281SAndrey Ignatov FN(sk_select_reuseport), \ 24166acc9b43SJoe Stringer FN(skb_ancestor_cgroup_id), \ 24176acc9b43SJoe Stringer FN(sk_lookup_tcp), \ 24186acc9b43SJoe Stringer FN(sk_lookup_udp), \ 2419f1a2e44aSMauricio Vasquez B FN(sk_release), \ 2420f1a2e44aSMauricio Vasquez B FN(map_push_elem), \ 2421f1a2e44aSMauricio Vasquez B FN(map_pop_elem), \ 24226fff607eSJohn Fastabend FN(map_peek_elem), \ 24237246d8edSJohn Fastabend FN(msg_push_data), \ 242401d3240aSSean Young FN(msg_pop_data), \ 2425*d83525caSAlexei Starovoitov FN(rc_pointer_rel), \ 2426*d83525caSAlexei Starovoitov FN(spin_lock), \ 2427*d83525caSAlexei Starovoitov FN(spin_unlock), 24287a4b28c6SDaniel Borkmann 2429ebb676daSThomas Graf /* integer value in 'imm' field of BPF_CALL instruction selects which helper 2430ebb676daSThomas Graf * function eBPF program intends to call 24312d0e30c3SDaniel Borkmann */ 2432ebb676daSThomas Graf #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x 2433ebb676daSThomas Graf enum bpf_func_id { 2434ebb676daSThomas Graf __BPF_FUNC_MAPPER(__BPF_ENUM_FN) 243509756af4SAlexei Starovoitov __BPF_FUNC_MAX_ID, 243609756af4SAlexei Starovoitov }; 2437ebb676daSThomas Graf #undef __BPF_ENUM_FN 243809756af4SAlexei Starovoitov 2439781c53bcSDaniel Borkmann /* All flags used by eBPF helper functions, placed here. */ 2440781c53bcSDaniel Borkmann 2441781c53bcSDaniel Borkmann /* BPF_FUNC_skb_store_bytes flags. */ 2442781c53bcSDaniel Borkmann #define BPF_F_RECOMPUTE_CSUM (1ULL << 0) 24438afd54c8SDaniel Borkmann #define BPF_F_INVALIDATE_HASH (1ULL << 1) 2444781c53bcSDaniel Borkmann 2445781c53bcSDaniel Borkmann /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags. 2446781c53bcSDaniel Borkmann * First 4 bits are for passing the header field size. 2447781c53bcSDaniel Borkmann */ 2448781c53bcSDaniel Borkmann #define BPF_F_HDR_FIELD_MASK 0xfULL 2449781c53bcSDaniel Borkmann 2450781c53bcSDaniel Borkmann /* BPF_FUNC_l4_csum_replace flags. */ 2451781c53bcSDaniel Borkmann #define BPF_F_PSEUDO_HDR (1ULL << 4) 24522f72959aSDaniel Borkmann #define BPF_F_MARK_MANGLED_0 (1ULL << 5) 2453d1b662adSDaniel Borkmann #define BPF_F_MARK_ENFORCE (1ULL << 6) 2454781c53bcSDaniel Borkmann 2455781c53bcSDaniel Borkmann /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */ 2456781c53bcSDaniel Borkmann #define BPF_F_INGRESS (1ULL << 0) 2457781c53bcSDaniel Borkmann 2458c6c33454SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */ 2459c6c33454SDaniel Borkmann #define BPF_F_TUNINFO_IPV6 (1ULL << 0) 2460c6c33454SDaniel Borkmann 2461c195651eSYonghong Song /* flags for both BPF_FUNC_get_stackid and BPF_FUNC_get_stack. */ 2462d5a3b1f6SAlexei Starovoitov #define BPF_F_SKIP_FIELD_MASK 0xffULL 2463d5a3b1f6SAlexei Starovoitov #define BPF_F_USER_STACK (1ULL << 8) 2464c195651eSYonghong Song /* flags used by BPF_FUNC_get_stackid only. */ 2465d5a3b1f6SAlexei Starovoitov #define BPF_F_FAST_STACK_CMP (1ULL << 9) 2466d5a3b1f6SAlexei Starovoitov #define BPF_F_REUSE_STACKID (1ULL << 10) 2467c195651eSYonghong Song /* flags used by BPF_FUNC_get_stack only. */ 2468c195651eSYonghong Song #define BPF_F_USER_BUILD_ID (1ULL << 11) 2469d5a3b1f6SAlexei Starovoitov 24702da897e5SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key flags. */ 24712da897e5SDaniel Borkmann #define BPF_F_ZERO_CSUM_TX (1ULL << 1) 247222080870SDaniel Borkmann #define BPF_F_DONT_FRAGMENT (1ULL << 2) 247377a5196aSWilliam Tu #define BPF_F_SEQ_NUMBER (1ULL << 3) 24742da897e5SDaniel Borkmann 2475908432caSYonghong Song /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and 2476908432caSYonghong Song * BPF_FUNC_perf_event_read_value flags. 2477908432caSYonghong Song */ 24781e33759cSDaniel Borkmann #define BPF_F_INDEX_MASK 0xffffffffULL 24791e33759cSDaniel Borkmann #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK 2480555c8a86SDaniel Borkmann /* BPF_FUNC_perf_event_output for sk_buff input context. */ 2481555c8a86SDaniel Borkmann #define BPF_F_CTXLEN_MASK (0xfffffULL << 32) 24821e33759cSDaniel Borkmann 2483f71c6143SJoe Stringer /* Current network namespace */ 2484f71c6143SJoe Stringer #define BPF_F_CURRENT_NETNS (-1L) 2485f71c6143SJoe Stringer 24862be7e212SDaniel Borkmann /* Mode for BPF_FUNC_skb_adjust_room helper. */ 24872be7e212SDaniel Borkmann enum bpf_adj_room_mode { 24882be7e212SDaniel Borkmann BPF_ADJ_ROOM_NET, 24892be7e212SDaniel Borkmann }; 24902be7e212SDaniel Borkmann 24914e1ec56cSDaniel Borkmann /* Mode for BPF_FUNC_skb_load_bytes_relative helper. */ 24924e1ec56cSDaniel Borkmann enum bpf_hdr_start_off { 24934e1ec56cSDaniel Borkmann BPF_HDR_START_MAC, 24944e1ec56cSDaniel Borkmann BPF_HDR_START_NET, 24954e1ec56cSDaniel Borkmann }; 24964e1ec56cSDaniel Borkmann 2497fe94cc29SMathieu Xhonneux /* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */ 2498fe94cc29SMathieu Xhonneux enum bpf_lwt_encap_mode { 2499fe94cc29SMathieu Xhonneux BPF_LWT_ENCAP_SEG6, 2500fe94cc29SMathieu Xhonneux BPF_LWT_ENCAP_SEG6_INLINE 2501fe94cc29SMathieu Xhonneux }; 2502fe94cc29SMathieu Xhonneux 2503b7df9adaSDaniel Borkmann #define __bpf_md_ptr(type, name) \ 2504b7df9adaSDaniel Borkmann union { \ 2505b7df9adaSDaniel Borkmann type name; \ 2506b7df9adaSDaniel Borkmann __u64 :64; \ 2507b7df9adaSDaniel Borkmann } __attribute__((aligned(8))) 2508b7df9adaSDaniel Borkmann 25099bac3d6dSAlexei Starovoitov /* user accessible mirror of in-kernel sk_buff. 25109bac3d6dSAlexei Starovoitov * new fields can only be added to the end of this structure 25119bac3d6dSAlexei Starovoitov */ 25129bac3d6dSAlexei Starovoitov struct __sk_buff { 25139bac3d6dSAlexei Starovoitov __u32 len; 25149bac3d6dSAlexei Starovoitov __u32 pkt_type; 25159bac3d6dSAlexei Starovoitov __u32 mark; 25169bac3d6dSAlexei Starovoitov __u32 queue_mapping; 2517c2497395SAlexei Starovoitov __u32 protocol; 2518c2497395SAlexei Starovoitov __u32 vlan_present; 2519c2497395SAlexei Starovoitov __u32 vlan_tci; 252027cd5452SMichal Sekletar __u32 vlan_proto; 2521bcad5718SDaniel Borkmann __u32 priority; 252237e82c2fSAlexei Starovoitov __u32 ingress_ifindex; 252337e82c2fSAlexei Starovoitov __u32 ifindex; 2524d691f9e8SAlexei Starovoitov __u32 tc_index; 2525d691f9e8SAlexei Starovoitov __u32 cb[5]; 2526ba7591d8SDaniel Borkmann __u32 hash; 2527045efa82SDaniel Borkmann __u32 tc_classid; 2528969bf05eSAlexei Starovoitov __u32 data; 2529969bf05eSAlexei Starovoitov __u32 data_end; 2530b1d9fc41SDaniel Borkmann __u32 napi_id; 25318a31db56SJohn Fastabend 2532de8f3a83SDaniel Borkmann /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */ 25338a31db56SJohn Fastabend __u32 family; 25348a31db56SJohn Fastabend __u32 remote_ip4; /* Stored in network byte order */ 25358a31db56SJohn Fastabend __u32 local_ip4; /* Stored in network byte order */ 25368a31db56SJohn Fastabend __u32 remote_ip6[4]; /* Stored in network byte order */ 25378a31db56SJohn Fastabend __u32 local_ip6[4]; /* Stored in network byte order */ 25388a31db56SJohn Fastabend __u32 remote_port; /* Stored in network byte order */ 25398a31db56SJohn Fastabend __u32 local_port; /* stored in host byte order */ 2540de8f3a83SDaniel Borkmann /* ... here. */ 2541de8f3a83SDaniel Borkmann 2542de8f3a83SDaniel Borkmann __u32 data_meta; 2543b7df9adaSDaniel Borkmann __bpf_md_ptr(struct bpf_flow_keys *, flow_keys); 2544f11216b2SVlad Dumitrescu __u64 tstamp; 2545e3da08d0SPetar Penkov __u32 wire_len; 2546d9ff286aSEric Dumazet __u32 gso_segs; 25479bac3d6dSAlexei Starovoitov }; 25489bac3d6dSAlexei Starovoitov 2549d3aa45ceSAlexei Starovoitov struct bpf_tunnel_key { 2550d3aa45ceSAlexei Starovoitov __u32 tunnel_id; 2551c6c33454SDaniel Borkmann union { 2552d3aa45ceSAlexei Starovoitov __u32 remote_ipv4; 2553c6c33454SDaniel Borkmann __u32 remote_ipv6[4]; 2554c6c33454SDaniel Borkmann }; 2555c6c33454SDaniel Borkmann __u8 tunnel_tos; 2556c6c33454SDaniel Borkmann __u8 tunnel_ttl; 25571fbc2e0cSDaniel Borkmann __u16 tunnel_ext; /* Padding, future use. */ 25584018ab18SDaniel Borkmann __u32 tunnel_label; 2559d3aa45ceSAlexei Starovoitov }; 2560d3aa45ceSAlexei Starovoitov 256112bed760SEyal Birger /* user accessible mirror of in-kernel xfrm_state. 256212bed760SEyal Birger * new fields can only be added to the end of this structure 256312bed760SEyal Birger */ 256412bed760SEyal Birger struct bpf_xfrm_state { 256512bed760SEyal Birger __u32 reqid; 256612bed760SEyal Birger __u32 spi; /* Stored in network byte order */ 256712bed760SEyal Birger __u16 family; 25681fbc2e0cSDaniel Borkmann __u16 ext; /* Padding, future use. */ 256912bed760SEyal Birger union { 257012bed760SEyal Birger __u32 remote_ipv4; /* Stored in network byte order */ 257112bed760SEyal Birger __u32 remote_ipv6[4]; /* Stored in network byte order */ 257212bed760SEyal Birger }; 257312bed760SEyal Birger }; 257412bed760SEyal Birger 25753a0af8fdSThomas Graf /* Generic BPF return codes which all BPF program types may support. 25763a0af8fdSThomas Graf * The values are binary compatible with their TC_ACT_* counter-part to 25773a0af8fdSThomas Graf * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT 25783a0af8fdSThomas Graf * programs. 25793a0af8fdSThomas Graf * 25803a0af8fdSThomas Graf * XDP is handled seprately, see XDP_*. 25813a0af8fdSThomas Graf */ 25823a0af8fdSThomas Graf enum bpf_ret_code { 25833a0af8fdSThomas Graf BPF_OK = 0, 25843a0af8fdSThomas Graf /* 1 reserved */ 25853a0af8fdSThomas Graf BPF_DROP = 2, 25863a0af8fdSThomas Graf /* 3-6 reserved */ 25873a0af8fdSThomas Graf BPF_REDIRECT = 7, 25883a0af8fdSThomas Graf /* >127 are reserved for prog type specific return codes */ 25893a0af8fdSThomas Graf }; 25903a0af8fdSThomas Graf 259161023658SDavid Ahern struct bpf_sock { 259261023658SDavid Ahern __u32 bound_dev_if; 2593aa4c1037SDavid Ahern __u32 family; 2594aa4c1037SDavid Ahern __u32 type; 2595aa4c1037SDavid Ahern __u32 protocol; 2596482dca93SDavid Ahern __u32 mark; 2597482dca93SDavid Ahern __u32 priority; 2598aac3fc32SAndrey Ignatov __u32 src_ip4; /* Allows 1,2,4-byte read. 2599aac3fc32SAndrey Ignatov * Stored in network byte order. 2600aac3fc32SAndrey Ignatov */ 2601aac3fc32SAndrey Ignatov __u32 src_ip6[4]; /* Allows 1,2,4-byte read. 2602aac3fc32SAndrey Ignatov * Stored in network byte order. 2603aac3fc32SAndrey Ignatov */ 2604aac3fc32SAndrey Ignatov __u32 src_port; /* Allows 4-byte read. 2605aac3fc32SAndrey Ignatov * Stored in host byte order 2606aac3fc32SAndrey Ignatov */ 260761023658SDavid Ahern }; 260861023658SDavid Ahern 26096acc9b43SJoe Stringer struct bpf_sock_tuple { 26106acc9b43SJoe Stringer union { 26116acc9b43SJoe Stringer struct { 26126acc9b43SJoe Stringer __be32 saddr; 26136acc9b43SJoe Stringer __be32 daddr; 26146acc9b43SJoe Stringer __be16 sport; 26156acc9b43SJoe Stringer __be16 dport; 26166acc9b43SJoe Stringer } ipv4; 26176acc9b43SJoe Stringer struct { 26186acc9b43SJoe Stringer __be32 saddr[4]; 26196acc9b43SJoe Stringer __be32 daddr[4]; 26206acc9b43SJoe Stringer __be16 sport; 26216acc9b43SJoe Stringer __be16 dport; 26226acc9b43SJoe Stringer } ipv6; 26236acc9b43SJoe Stringer }; 26246acc9b43SJoe Stringer }; 26256acc9b43SJoe Stringer 262617bedab2SMartin KaFai Lau #define XDP_PACKET_HEADROOM 256 262717bedab2SMartin KaFai Lau 26286a773a15SBrenden Blanco /* User return codes for XDP prog type. 26296a773a15SBrenden Blanco * A valid XDP program must return one of these defined values. All other 26309beb8bedSDaniel Borkmann * return codes are reserved for future use. Unknown return codes will 26319beb8bedSDaniel Borkmann * result in packet drops and a warning via bpf_warn_invalid_xdp_action(). 26326a773a15SBrenden Blanco */ 26336a773a15SBrenden Blanco enum xdp_action { 26346a773a15SBrenden Blanco XDP_ABORTED = 0, 26356a773a15SBrenden Blanco XDP_DROP, 26366a773a15SBrenden Blanco XDP_PASS, 26376ce96ca3SBrenden Blanco XDP_TX, 2638814abfabSJohn Fastabend XDP_REDIRECT, 26396a773a15SBrenden Blanco }; 26406a773a15SBrenden Blanco 26416a773a15SBrenden Blanco /* user accessible metadata for XDP packet hook 26426a773a15SBrenden Blanco * new fields must be added to the end of this structure 26436a773a15SBrenden Blanco */ 26446a773a15SBrenden Blanco struct xdp_md { 26456a773a15SBrenden Blanco __u32 data; 26466a773a15SBrenden Blanco __u32 data_end; 2647de8f3a83SDaniel Borkmann __u32 data_meta; 2648daaf24c6SJesper Dangaard Brouer /* Below access go through struct xdp_rxq_info */ 264902dd3291SJesper Dangaard Brouer __u32 ingress_ifindex; /* rxq->dev->ifindex */ 265002dd3291SJesper Dangaard Brouer __u32 rx_queue_index; /* rxq->queue_index */ 26516a773a15SBrenden Blanco }; 26526a773a15SBrenden Blanco 2653174a79ffSJohn Fastabend enum sk_action { 2654bfa64075SJohn Fastabend SK_DROP = 0, 2655bfa64075SJohn Fastabend SK_PASS, 2656174a79ffSJohn Fastabend }; 2657174a79ffSJohn Fastabend 26584f738adbSJohn Fastabend /* user accessible metadata for SK_MSG packet hook, new fields must 26594f738adbSJohn Fastabend * be added to the end of this structure 26604f738adbSJohn Fastabend */ 26614f738adbSJohn Fastabend struct sk_msg_md { 2662b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data); 2663b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data_end); 2664303def35SJohn Fastabend 2665303def35SJohn Fastabend __u32 family; 2666303def35SJohn Fastabend __u32 remote_ip4; /* Stored in network byte order */ 2667303def35SJohn Fastabend __u32 local_ip4; /* Stored in network byte order */ 2668303def35SJohn Fastabend __u32 remote_ip6[4]; /* Stored in network byte order */ 2669303def35SJohn Fastabend __u32 local_ip6[4]; /* Stored in network byte order */ 2670303def35SJohn Fastabend __u32 remote_port; /* Stored in network byte order */ 2671303def35SJohn Fastabend __u32 local_port; /* stored in host byte order */ 26723bdbd022SJohn Fastabend __u32 size; /* Total size of sk_msg */ 26734f738adbSJohn Fastabend }; 26744f738adbSJohn Fastabend 26752dbb9b9eSMartin KaFai Lau struct sk_reuseport_md { 26762dbb9b9eSMartin KaFai Lau /* 26772dbb9b9eSMartin KaFai Lau * Start of directly accessible data. It begins from 26782dbb9b9eSMartin KaFai Lau * the tcp/udp header. 26792dbb9b9eSMartin KaFai Lau */ 2680b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data); 2681b7df9adaSDaniel Borkmann /* End of directly accessible data */ 2682b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data_end); 26832dbb9b9eSMartin KaFai Lau /* 26842dbb9b9eSMartin KaFai Lau * Total length of packet (starting from the tcp/udp header). 26852dbb9b9eSMartin KaFai Lau * Note that the directly accessible bytes (data_end - data) 26862dbb9b9eSMartin KaFai Lau * could be less than this "len". Those bytes could be 26872dbb9b9eSMartin KaFai Lau * indirectly read by a helper "bpf_skb_load_bytes()". 26882dbb9b9eSMartin KaFai Lau */ 26892dbb9b9eSMartin KaFai Lau __u32 len; 26902dbb9b9eSMartin KaFai Lau /* 26912dbb9b9eSMartin KaFai Lau * Eth protocol in the mac header (network byte order). e.g. 26922dbb9b9eSMartin KaFai Lau * ETH_P_IP(0x0800) and ETH_P_IPV6(0x86DD) 26932dbb9b9eSMartin KaFai Lau */ 26942dbb9b9eSMartin KaFai Lau __u32 eth_protocol; 26952dbb9b9eSMartin KaFai Lau __u32 ip_protocol; /* IP protocol. e.g. IPPROTO_TCP, IPPROTO_UDP */ 26962dbb9b9eSMartin KaFai Lau __u32 bind_inany; /* Is sock bound to an INANY address? */ 26972dbb9b9eSMartin KaFai Lau __u32 hash; /* A hash of the packet 4 tuples */ 26982dbb9b9eSMartin KaFai Lau }; 26992dbb9b9eSMartin KaFai Lau 27001e270976SMartin KaFai Lau #define BPF_TAG_SIZE 8 27011e270976SMartin KaFai Lau 27021e270976SMartin KaFai Lau struct bpf_prog_info { 27031e270976SMartin KaFai Lau __u32 type; 27041e270976SMartin KaFai Lau __u32 id; 27051e270976SMartin KaFai Lau __u8 tag[BPF_TAG_SIZE]; 27061e270976SMartin KaFai Lau __u32 jited_prog_len; 27071e270976SMartin KaFai Lau __u32 xlated_prog_len; 27081e270976SMartin KaFai Lau __aligned_u64 jited_prog_insns; 27091e270976SMartin KaFai Lau __aligned_u64 xlated_prog_insns; 2710cb4d2b3fSMartin KaFai Lau __u64 load_time; /* ns since boottime */ 2711cb4d2b3fSMartin KaFai Lau __u32 created_by_uid; 2712cb4d2b3fSMartin KaFai Lau __u32 nr_map_ids; 2713cb4d2b3fSMartin KaFai Lau __aligned_u64 map_ids; 2714067cae47SMartin KaFai Lau char name[BPF_OBJ_NAME_LEN]; 2715675fc275SJakub Kicinski __u32 ifindex; 2716b85fab0eSJiri Olsa __u32 gpl_compatible:1; 2717675fc275SJakub Kicinski __u64 netns_dev; 2718675fc275SJakub Kicinski __u64 netns_ino; 2719dbecd738SSandipan Das __u32 nr_jited_ksyms; 2720815581c1SSandipan Das __u32 nr_jited_func_lens; 2721dbecd738SSandipan Das __aligned_u64 jited_ksyms; 2722815581c1SSandipan Das __aligned_u64 jited_func_lens; 2723838e9690SYonghong Song __u32 btf_id; 2724838e9690SYonghong Song __u32 func_info_rec_size; 2725838e9690SYonghong Song __aligned_u64 func_info; 272611d8b82dSYonghong Song __u32 nr_func_info; 272711d8b82dSYonghong Song __u32 nr_line_info; 2728c454a46bSMartin KaFai Lau __aligned_u64 line_info; 2729c454a46bSMartin KaFai Lau __aligned_u64 jited_line_info; 273011d8b82dSYonghong Song __u32 nr_jited_line_info; 2731c454a46bSMartin KaFai Lau __u32 line_info_rec_size; 2732c454a46bSMartin KaFai Lau __u32 jited_line_info_rec_size; 2733c872bdb3SSong Liu __u32 nr_prog_tags; 2734c872bdb3SSong Liu __aligned_u64 prog_tags; 27351e270976SMartin KaFai Lau } __attribute__((aligned(8))); 27361e270976SMartin KaFai Lau 27371e270976SMartin KaFai Lau struct bpf_map_info { 27381e270976SMartin KaFai Lau __u32 type; 27391e270976SMartin KaFai Lau __u32 id; 27401e270976SMartin KaFai Lau __u32 key_size; 27411e270976SMartin KaFai Lau __u32 value_size; 27421e270976SMartin KaFai Lau __u32 max_entries; 27431e270976SMartin KaFai Lau __u32 map_flags; 2744067cae47SMartin KaFai Lau char name[BPF_OBJ_NAME_LEN]; 274552775b33SJakub Kicinski __u32 ifindex; 274636f9814aSDaniel Borkmann __u32 :32; 274752775b33SJakub Kicinski __u64 netns_dev; 274852775b33SJakub Kicinski __u64 netns_ino; 274978958fcaSMartin KaFai Lau __u32 btf_id; 27509b2cf328SMartin KaFai Lau __u32 btf_key_type_id; 27519b2cf328SMartin KaFai Lau __u32 btf_value_type_id; 27521e270976SMartin KaFai Lau } __attribute__((aligned(8))); 27531e270976SMartin KaFai Lau 275462dab84cSMartin KaFai Lau struct bpf_btf_info { 275562dab84cSMartin KaFai Lau __aligned_u64 btf; 275662dab84cSMartin KaFai Lau __u32 btf_size; 275762dab84cSMartin KaFai Lau __u32 id; 275862dab84cSMartin KaFai Lau } __attribute__((aligned(8))); 275962dab84cSMartin KaFai Lau 27604fbac77dSAndrey Ignatov /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed 27614fbac77dSAndrey Ignatov * by user and intended to be used by socket (e.g. to bind to, depends on 27624fbac77dSAndrey Ignatov * attach attach type). 27634fbac77dSAndrey Ignatov */ 27644fbac77dSAndrey Ignatov struct bpf_sock_addr { 27654fbac77dSAndrey Ignatov __u32 user_family; /* Allows 4-byte read, but no write. */ 27664fbac77dSAndrey Ignatov __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write. 27674fbac77dSAndrey Ignatov * Stored in network byte order. 27684fbac77dSAndrey Ignatov */ 27694fbac77dSAndrey Ignatov __u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write. 27704fbac77dSAndrey Ignatov * Stored in network byte order. 27714fbac77dSAndrey Ignatov */ 27724fbac77dSAndrey Ignatov __u32 user_port; /* Allows 4-byte read and write. 27734fbac77dSAndrey Ignatov * Stored in network byte order 27744fbac77dSAndrey Ignatov */ 27754fbac77dSAndrey Ignatov __u32 family; /* Allows 4-byte read, but no write */ 27764fbac77dSAndrey Ignatov __u32 type; /* Allows 4-byte read, but no write */ 27774fbac77dSAndrey Ignatov __u32 protocol; /* Allows 4-byte read, but no write */ 27781cedee13SAndrey Ignatov __u32 msg_src_ip4; /* Allows 1,2,4-byte read an 4-byte write. 27791cedee13SAndrey Ignatov * Stored in network byte order. 27801cedee13SAndrey Ignatov */ 27811cedee13SAndrey Ignatov __u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write. 27821cedee13SAndrey Ignatov * Stored in network byte order. 27831cedee13SAndrey Ignatov */ 27844fbac77dSAndrey Ignatov }; 27854fbac77dSAndrey Ignatov 278640304b2aSLawrence Brakmo /* User bpf_sock_ops struct to access socket values and specify request ops 278740304b2aSLawrence Brakmo * and their replies. 278840304b2aSLawrence Brakmo * Some of this fields are in network (bigendian) byte order and may need 278940304b2aSLawrence Brakmo * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h). 279040304b2aSLawrence Brakmo * New fields can only be added at the end of this structure 279140304b2aSLawrence Brakmo */ 279240304b2aSLawrence Brakmo struct bpf_sock_ops { 279340304b2aSLawrence Brakmo __u32 op; 279440304b2aSLawrence Brakmo union { 2795de525be2SLawrence Brakmo __u32 args[4]; /* Optionally passed to bpf program */ 2796de525be2SLawrence Brakmo __u32 reply; /* Returned by bpf program */ 2797de525be2SLawrence Brakmo __u32 replylong[4]; /* Optionally returned by bpf prog */ 279840304b2aSLawrence Brakmo }; 279940304b2aSLawrence Brakmo __u32 family; 280040304b2aSLawrence Brakmo __u32 remote_ip4; /* Stored in network byte order */ 280140304b2aSLawrence Brakmo __u32 local_ip4; /* Stored in network byte order */ 280240304b2aSLawrence Brakmo __u32 remote_ip6[4]; /* Stored in network byte order */ 280340304b2aSLawrence Brakmo __u32 local_ip6[4]; /* Stored in network byte order */ 280440304b2aSLawrence Brakmo __u32 remote_port; /* Stored in network byte order */ 280540304b2aSLawrence Brakmo __u32 local_port; /* stored in host byte order */ 2806f19397a5SLawrence Brakmo __u32 is_fullsock; /* Some TCP fields are only valid if 2807f19397a5SLawrence Brakmo * there is a full socket. If not, the 2808f19397a5SLawrence Brakmo * fields read as zero. 2809f19397a5SLawrence Brakmo */ 2810f19397a5SLawrence Brakmo __u32 snd_cwnd; 2811f19397a5SLawrence Brakmo __u32 srtt_us; /* Averaged RTT << 3 in usecs */ 2812b13d8807SLawrence Brakmo __u32 bpf_sock_ops_cb_flags; /* flags defined in uapi/linux/tcp.h */ 281344f0e430SLawrence Brakmo __u32 state; 281444f0e430SLawrence Brakmo __u32 rtt_min; 281544f0e430SLawrence Brakmo __u32 snd_ssthresh; 281644f0e430SLawrence Brakmo __u32 rcv_nxt; 281744f0e430SLawrence Brakmo __u32 snd_nxt; 281844f0e430SLawrence Brakmo __u32 snd_una; 281944f0e430SLawrence Brakmo __u32 mss_cache; 282044f0e430SLawrence Brakmo __u32 ecn_flags; 282144f0e430SLawrence Brakmo __u32 rate_delivered; 282244f0e430SLawrence Brakmo __u32 rate_interval_us; 282344f0e430SLawrence Brakmo __u32 packets_out; 282444f0e430SLawrence Brakmo __u32 retrans_out; 282544f0e430SLawrence Brakmo __u32 total_retrans; 282644f0e430SLawrence Brakmo __u32 segs_in; 282744f0e430SLawrence Brakmo __u32 data_segs_in; 282844f0e430SLawrence Brakmo __u32 segs_out; 282944f0e430SLawrence Brakmo __u32 data_segs_out; 283044f0e430SLawrence Brakmo __u32 lost_out; 283144f0e430SLawrence Brakmo __u32 sacked_out; 283244f0e430SLawrence Brakmo __u32 sk_txhash; 283344f0e430SLawrence Brakmo __u64 bytes_received; 283444f0e430SLawrence Brakmo __u64 bytes_acked; 283540304b2aSLawrence Brakmo }; 283640304b2aSLawrence Brakmo 2837b13d8807SLawrence Brakmo /* Definitions for bpf_sock_ops_cb_flags */ 2838f89013f6SLawrence Brakmo #define BPF_SOCK_OPS_RTO_CB_FLAG (1<<0) 2839a31ad29eSLawrence Brakmo #define BPF_SOCK_OPS_RETRANS_CB_FLAG (1<<1) 2840d4487491SLawrence Brakmo #define BPF_SOCK_OPS_STATE_CB_FLAG (1<<2) 2841d4487491SLawrence Brakmo #define BPF_SOCK_OPS_ALL_CB_FLAGS 0x7 /* Mask of all currently 2842b13d8807SLawrence Brakmo * supported cb flags 2843b13d8807SLawrence Brakmo */ 2844b13d8807SLawrence Brakmo 284540304b2aSLawrence Brakmo /* List of known BPF sock_ops operators. 284640304b2aSLawrence Brakmo * New entries can only be added at the end 284740304b2aSLawrence Brakmo */ 284840304b2aSLawrence Brakmo enum { 284940304b2aSLawrence Brakmo BPF_SOCK_OPS_VOID, 28508550f328SLawrence Brakmo BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or 28518550f328SLawrence Brakmo * -1 if default value should be used 28528550f328SLawrence Brakmo */ 285313d3b1ebSLawrence Brakmo BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized 285413d3b1ebSLawrence Brakmo * window (in packets) or -1 if default 285513d3b1ebSLawrence Brakmo * value should be used 285613d3b1ebSLawrence Brakmo */ 28579872a4bdSLawrence Brakmo BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an 28589872a4bdSLawrence Brakmo * active connection is initialized 28599872a4bdSLawrence Brakmo */ 28609872a4bdSLawrence Brakmo BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an 28619872a4bdSLawrence Brakmo * active connection is 28629872a4bdSLawrence Brakmo * established 28639872a4bdSLawrence Brakmo */ 28649872a4bdSLawrence Brakmo BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a 28659872a4bdSLawrence Brakmo * passive connection is 28669872a4bdSLawrence Brakmo * established 28679872a4bdSLawrence Brakmo */ 286891b5b21cSLawrence Brakmo BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control 286991b5b21cSLawrence Brakmo * needs ECN 287091b5b21cSLawrence Brakmo */ 2871e6546ef6SLawrence Brakmo BPF_SOCK_OPS_BASE_RTT, /* Get base RTT. The correct value is 2872e6546ef6SLawrence Brakmo * based on the path and may be 2873e6546ef6SLawrence Brakmo * dependent on the congestion control 2874e6546ef6SLawrence Brakmo * algorithm. In general it indicates 2875e6546ef6SLawrence Brakmo * a congestion threshold. RTTs above 2876e6546ef6SLawrence Brakmo * this indicate congestion 2877e6546ef6SLawrence Brakmo */ 2878f89013f6SLawrence Brakmo BPF_SOCK_OPS_RTO_CB, /* Called when an RTO has triggered. 2879f89013f6SLawrence Brakmo * Arg1: value of icsk_retransmits 2880f89013f6SLawrence Brakmo * Arg2: value of icsk_rto 2881f89013f6SLawrence Brakmo * Arg3: whether RTO has expired 2882f89013f6SLawrence Brakmo */ 2883a31ad29eSLawrence Brakmo BPF_SOCK_OPS_RETRANS_CB, /* Called when skb is retransmitted. 2884a31ad29eSLawrence Brakmo * Arg1: sequence number of 1st byte 2885a31ad29eSLawrence Brakmo * Arg2: # segments 2886a31ad29eSLawrence Brakmo * Arg3: return value of 2887a31ad29eSLawrence Brakmo * tcp_transmit_skb (0 => success) 2888a31ad29eSLawrence Brakmo */ 2889d4487491SLawrence Brakmo BPF_SOCK_OPS_STATE_CB, /* Called when TCP changes state. 2890d4487491SLawrence Brakmo * Arg1: old_state 2891d4487491SLawrence Brakmo * Arg2: new_state 2892d4487491SLawrence Brakmo */ 2893f333ee0cSAndrey Ignatov BPF_SOCK_OPS_TCP_LISTEN_CB, /* Called on listen(2), right after 2894f333ee0cSAndrey Ignatov * socket transition to LISTEN state. 2895f333ee0cSAndrey Ignatov */ 2896d4487491SLawrence Brakmo }; 2897d4487491SLawrence Brakmo 2898d4487491SLawrence Brakmo /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect 2899d4487491SLawrence Brakmo * changes between the TCP and BPF versions. Ideally this should never happen. 2900d4487491SLawrence Brakmo * If it does, we need to add code to convert them before calling 2901d4487491SLawrence Brakmo * the BPF sock_ops function. 2902d4487491SLawrence Brakmo */ 2903d4487491SLawrence Brakmo enum { 2904d4487491SLawrence Brakmo BPF_TCP_ESTABLISHED = 1, 2905d4487491SLawrence Brakmo BPF_TCP_SYN_SENT, 2906d4487491SLawrence Brakmo BPF_TCP_SYN_RECV, 2907d4487491SLawrence Brakmo BPF_TCP_FIN_WAIT1, 2908d4487491SLawrence Brakmo BPF_TCP_FIN_WAIT2, 2909d4487491SLawrence Brakmo BPF_TCP_TIME_WAIT, 2910d4487491SLawrence Brakmo BPF_TCP_CLOSE, 2911d4487491SLawrence Brakmo BPF_TCP_CLOSE_WAIT, 2912d4487491SLawrence Brakmo BPF_TCP_LAST_ACK, 2913d4487491SLawrence Brakmo BPF_TCP_LISTEN, 2914d4487491SLawrence Brakmo BPF_TCP_CLOSING, /* Now a valid state */ 2915d4487491SLawrence Brakmo BPF_TCP_NEW_SYN_RECV, 2916d4487491SLawrence Brakmo 2917d4487491SLawrence Brakmo BPF_TCP_MAX_STATES /* Leave at the end! */ 291840304b2aSLawrence Brakmo }; 291940304b2aSLawrence Brakmo 2920fc747810SLawrence Brakmo #define TCP_BPF_IW 1001 /* Set TCP initial congestion window */ 292113bf9641SLawrence Brakmo #define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */ 2922fc747810SLawrence Brakmo 2923908432caSYonghong Song struct bpf_perf_event_value { 2924908432caSYonghong Song __u64 counter; 2925908432caSYonghong Song __u64 enabled; 2926908432caSYonghong Song __u64 running; 2927908432caSYonghong Song }; 2928908432caSYonghong Song 2929ebc614f6SRoman Gushchin #define BPF_DEVCG_ACC_MKNOD (1ULL << 0) 2930ebc614f6SRoman Gushchin #define BPF_DEVCG_ACC_READ (1ULL << 1) 2931ebc614f6SRoman Gushchin #define BPF_DEVCG_ACC_WRITE (1ULL << 2) 2932ebc614f6SRoman Gushchin 2933ebc614f6SRoman Gushchin #define BPF_DEVCG_DEV_BLOCK (1ULL << 0) 2934ebc614f6SRoman Gushchin #define BPF_DEVCG_DEV_CHAR (1ULL << 1) 2935ebc614f6SRoman Gushchin 2936ebc614f6SRoman Gushchin struct bpf_cgroup_dev_ctx { 293706ef0ccbSYonghong Song /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */ 293806ef0ccbSYonghong Song __u32 access_type; 2939ebc614f6SRoman Gushchin __u32 major; 2940ebc614f6SRoman Gushchin __u32 minor; 2941ebc614f6SRoman Gushchin }; 2942ebc614f6SRoman Gushchin 2943c4f6699dSAlexei Starovoitov struct bpf_raw_tracepoint_args { 2944c4f6699dSAlexei Starovoitov __u64 args[0]; 2945c4f6699dSAlexei Starovoitov }; 2946c4f6699dSAlexei Starovoitov 294787f5fc7eSDavid Ahern /* DIRECT: Skip the FIB rules and go to FIB table associated with device 294887f5fc7eSDavid Ahern * OUTPUT: Do lookup from egress perspective; default is ingress 294987f5fc7eSDavid Ahern */ 295087f5fc7eSDavid Ahern #define BPF_FIB_LOOKUP_DIRECT BIT(0) 295187f5fc7eSDavid Ahern #define BPF_FIB_LOOKUP_OUTPUT BIT(1) 295287f5fc7eSDavid Ahern 29534c79579bSDavid Ahern enum { 29544c79579bSDavid Ahern BPF_FIB_LKUP_RET_SUCCESS, /* lookup successful */ 29554c79579bSDavid Ahern BPF_FIB_LKUP_RET_BLACKHOLE, /* dest is blackholed; can be dropped */ 29564c79579bSDavid Ahern BPF_FIB_LKUP_RET_UNREACHABLE, /* dest is unreachable; can be dropped */ 29574c79579bSDavid Ahern BPF_FIB_LKUP_RET_PROHIBIT, /* dest not allowed; can be dropped */ 29584c79579bSDavid Ahern BPF_FIB_LKUP_RET_NOT_FWDED, /* packet is not forwarded */ 29594c79579bSDavid Ahern BPF_FIB_LKUP_RET_FWD_DISABLED, /* fwding is not enabled on ingress */ 29604c79579bSDavid Ahern BPF_FIB_LKUP_RET_UNSUPP_LWT, /* fwd requires encapsulation */ 29614c79579bSDavid Ahern BPF_FIB_LKUP_RET_NO_NEIGH, /* no neighbor entry for nh */ 29624c79579bSDavid Ahern BPF_FIB_LKUP_RET_FRAG_NEEDED, /* fragmentation required to fwd */ 29634c79579bSDavid Ahern }; 29644c79579bSDavid Ahern 296587f5fc7eSDavid Ahern struct bpf_fib_lookup { 2966fa898d76SDavid Ahern /* input: network family for lookup (AF_INET, AF_INET6) 2967fa898d76SDavid Ahern * output: network family of egress nexthop 2968fa898d76SDavid Ahern */ 2969fa898d76SDavid Ahern __u8 family; 297087f5fc7eSDavid Ahern 297187f5fc7eSDavid Ahern /* set if lookup is to consider L4 data - e.g., FIB rules */ 297287f5fc7eSDavid Ahern __u8 l4_protocol; 297387f5fc7eSDavid Ahern __be16 sport; 297487f5fc7eSDavid Ahern __be16 dport; 297587f5fc7eSDavid Ahern 297687f5fc7eSDavid Ahern /* total length of packet from network header - used for MTU check */ 297787f5fc7eSDavid Ahern __u16 tot_len; 29784c79579bSDavid Ahern 29794c79579bSDavid Ahern /* input: L3 device index for lookup 29804c79579bSDavid Ahern * output: device index from FIB lookup 29814c79579bSDavid Ahern */ 29824c79579bSDavid Ahern __u32 ifindex; 298387f5fc7eSDavid Ahern 298487f5fc7eSDavid Ahern union { 298587f5fc7eSDavid Ahern /* inputs to lookup */ 298687f5fc7eSDavid Ahern __u8 tos; /* AF_INET */ 2987bd3a08aaSDavid Ahern __be32 flowinfo; /* AF_INET6, flow_label + priority */ 298887f5fc7eSDavid Ahern 2989fa898d76SDavid Ahern /* output: metric of fib result (IPv4/IPv6 only) */ 299087f5fc7eSDavid Ahern __u32 rt_metric; 299187f5fc7eSDavid Ahern }; 299287f5fc7eSDavid Ahern 299387f5fc7eSDavid Ahern union { 299487f5fc7eSDavid Ahern __be32 ipv4_src; 299587f5fc7eSDavid Ahern __u32 ipv6_src[4]; /* in6_addr; network order */ 299687f5fc7eSDavid Ahern }; 299787f5fc7eSDavid Ahern 2998fa898d76SDavid Ahern /* input to bpf_fib_lookup, ipv{4,6}_dst is destination address in 2999fa898d76SDavid Ahern * network header. output: bpf_fib_lookup sets to gateway address 3000fa898d76SDavid Ahern * if FIB lookup returns gateway route 300187f5fc7eSDavid Ahern */ 300287f5fc7eSDavid Ahern union { 300387f5fc7eSDavid Ahern __be32 ipv4_dst; 300487f5fc7eSDavid Ahern __u32 ipv6_dst[4]; /* in6_addr; network order */ 300587f5fc7eSDavid Ahern }; 300687f5fc7eSDavid Ahern 300787f5fc7eSDavid Ahern /* output */ 300887f5fc7eSDavid Ahern __be16 h_vlan_proto; 300987f5fc7eSDavid Ahern __be16 h_vlan_TCI; 301087f5fc7eSDavid Ahern __u8 smac[6]; /* ETH_ALEN */ 301187f5fc7eSDavid Ahern __u8 dmac[6]; /* ETH_ALEN */ 301287f5fc7eSDavid Ahern }; 301387f5fc7eSDavid Ahern 301441bdc4b4SYonghong Song enum bpf_task_fd_type { 301541bdc4b4SYonghong Song BPF_FD_TYPE_RAW_TRACEPOINT, /* tp name */ 301641bdc4b4SYonghong Song BPF_FD_TYPE_TRACEPOINT, /* tp name */ 301741bdc4b4SYonghong Song BPF_FD_TYPE_KPROBE, /* (symbol + offset) or addr */ 301841bdc4b4SYonghong Song BPF_FD_TYPE_KRETPROBE, /* (symbol + offset) or addr */ 301941bdc4b4SYonghong Song BPF_FD_TYPE_UPROBE, /* filename + offset */ 302041bdc4b4SYonghong Song BPF_FD_TYPE_URETPROBE, /* filename + offset */ 302141bdc4b4SYonghong Song }; 302241bdc4b4SYonghong Song 3023d58e468bSPetar Penkov struct bpf_flow_keys { 3024d58e468bSPetar Penkov __u16 nhoff; 3025d58e468bSPetar Penkov __u16 thoff; 3026d58e468bSPetar Penkov __u16 addr_proto; /* ETH_P_* of valid addrs */ 3027d58e468bSPetar Penkov __u8 is_frag; 3028d58e468bSPetar Penkov __u8 is_first_frag; 3029d58e468bSPetar Penkov __u8 is_encap; 3030d58e468bSPetar Penkov __u8 ip_proto; 3031d58e468bSPetar Penkov __be16 n_proto; 3032d58e468bSPetar Penkov __be16 sport; 3033d58e468bSPetar Penkov __be16 dport; 3034d58e468bSPetar Penkov union { 3035d58e468bSPetar Penkov struct { 3036d58e468bSPetar Penkov __be32 ipv4_src; 3037d58e468bSPetar Penkov __be32 ipv4_dst; 3038d58e468bSPetar Penkov }; 3039d58e468bSPetar Penkov struct { 3040d58e468bSPetar Penkov __u32 ipv6_src[4]; /* in6_addr; network order */ 3041d58e468bSPetar Penkov __u32 ipv6_dst[4]; /* in6_addr; network order */ 3042d58e468bSPetar Penkov }; 3043d58e468bSPetar Penkov }; 3044d58e468bSPetar Penkov }; 3045d58e468bSPetar Penkov 3046838e9690SYonghong Song struct bpf_func_info { 3047d30d42e0SMartin KaFai Lau __u32 insn_off; 3048838e9690SYonghong Song __u32 type_id; 3049838e9690SYonghong Song }; 3050838e9690SYonghong Song 3051c454a46bSMartin KaFai Lau #define BPF_LINE_INFO_LINE_NUM(line_col) ((line_col) >> 10) 3052c454a46bSMartin KaFai Lau #define BPF_LINE_INFO_LINE_COL(line_col) ((line_col) & 0x3ff) 3053c454a46bSMartin KaFai Lau 3054c454a46bSMartin KaFai Lau struct bpf_line_info { 3055c454a46bSMartin KaFai Lau __u32 insn_off; 3056c454a46bSMartin KaFai Lau __u32 file_name_off; 3057c454a46bSMartin KaFai Lau __u32 line_off; 3058c454a46bSMartin KaFai Lau __u32 line_col; 3059c454a46bSMartin KaFai Lau }; 3060c454a46bSMartin KaFai Lau 3061*d83525caSAlexei Starovoitov struct bpf_spin_lock { 3062*d83525caSAlexei Starovoitov __u32 val; 3063*d83525caSAlexei Starovoitov }; 3064daedfb22SAlexei Starovoitov #endif /* _UAPI__LINUX_BPF_H__ */ 3065