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 */ 761e6e9d0fSGustavo A. R. Silva __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 845e7b3020SYonghong Song union bpf_iter_link_info { 855e7b3020SYonghong Song struct { 865e7b3020SYonghong Song __u32 map_fd; 875e7b3020SYonghong Song } map; 885e7b3020SYonghong Song }; 895e7b3020SYonghong Song 90b2197755SDaniel Borkmann /* BPF syscall commands, see bpf(2) man-page for details. */ 9199c55f7dSAlexei Starovoitov enum bpf_cmd { 9299c55f7dSAlexei Starovoitov BPF_MAP_CREATE, 93db20fd2bSAlexei Starovoitov BPF_MAP_LOOKUP_ELEM, 94db20fd2bSAlexei Starovoitov BPF_MAP_UPDATE_ELEM, 95db20fd2bSAlexei Starovoitov BPF_MAP_DELETE_ELEM, 96db20fd2bSAlexei Starovoitov BPF_MAP_GET_NEXT_KEY, 9709756af4SAlexei Starovoitov BPF_PROG_LOAD, 98b2197755SDaniel Borkmann BPF_OBJ_PIN, 99b2197755SDaniel Borkmann BPF_OBJ_GET, 100f4324551SDaniel Mack BPF_PROG_ATTACH, 101f4324551SDaniel Mack BPF_PROG_DETACH, 1021cf1cae9SAlexei Starovoitov BPF_PROG_TEST_RUN, 10334ad5580SMartin KaFai Lau BPF_PROG_GET_NEXT_ID, 10434ad5580SMartin KaFai Lau BPF_MAP_GET_NEXT_ID, 105b16d9aa4SMartin KaFai Lau BPF_PROG_GET_FD_BY_ID, 106bd5f5f4eSMartin KaFai Lau BPF_MAP_GET_FD_BY_ID, 1071e270976SMartin KaFai Lau BPF_OBJ_GET_INFO_BY_FD, 108468e2f64SAlexei Starovoitov BPF_PROG_QUERY, 109c4f6699dSAlexei Starovoitov BPF_RAW_TRACEPOINT_OPEN, 110f56a653cSMartin KaFai Lau BPF_BTF_LOAD, 11178958fcaSMartin KaFai Lau BPF_BTF_GET_FD_BY_ID, 11241bdc4b4SYonghong Song BPF_TASK_FD_QUERY, 113bd513cd0SMauricio Vasquez B BPF_MAP_LOOKUP_AND_DELETE_ELEM, 11487df15deSDaniel Borkmann BPF_MAP_FREEZE, 1151b9ed84eSQuentin Monnet BPF_BTF_GET_NEXT_ID, 116cb4d03abSBrian Vazquez BPF_MAP_LOOKUP_BATCH, 11705799638SYonghong Song BPF_MAP_LOOKUP_AND_DELETE_BATCH, 118aa2e93b8SBrian Vazquez BPF_MAP_UPDATE_BATCH, 119aa2e93b8SBrian Vazquez BPF_MAP_DELETE_BATCH, 120af6eea57SAndrii Nakryiko BPF_LINK_CREATE, 1210c991ebcSAndrii Nakryiko BPF_LINK_UPDATE, 1222d602c8cSAndrii Nakryiko BPF_LINK_GET_FD_BY_ID, 1232d602c8cSAndrii Nakryiko BPF_LINK_GET_NEXT_ID, 124d46edd67SSong Liu BPF_ENABLE_STATS, 125ac51d99bSYonghong Song BPF_ITER_CREATE, 12673b11c2aSAndrii Nakryiko BPF_LINK_DETACH, 127ef15314aSYiFei Zhu BPF_PROG_BIND_MAP, 12899c55f7dSAlexei Starovoitov }; 12999c55f7dSAlexei Starovoitov 13099c55f7dSAlexei Starovoitov enum bpf_map_type { 13199c55f7dSAlexei Starovoitov BPF_MAP_TYPE_UNSPEC, 1320f8e4bd8SAlexei Starovoitov BPF_MAP_TYPE_HASH, 13328fbcfa0SAlexei Starovoitov BPF_MAP_TYPE_ARRAY, 13404fd61abSAlexei Starovoitov BPF_MAP_TYPE_PROG_ARRAY, 135ea317b26SKaixu Xia BPF_MAP_TYPE_PERF_EVENT_ARRAY, 136824bd0ceSAlexei Starovoitov BPF_MAP_TYPE_PERCPU_HASH, 137a10423b8SAlexei Starovoitov BPF_MAP_TYPE_PERCPU_ARRAY, 138d5a3b1f6SAlexei Starovoitov BPF_MAP_TYPE_STACK_TRACE, 1394ed8ec52SMartin KaFai Lau BPF_MAP_TYPE_CGROUP_ARRAY, 14029ba732aSMartin KaFai Lau BPF_MAP_TYPE_LRU_HASH, 1418f844938SMartin KaFai Lau BPF_MAP_TYPE_LRU_PERCPU_HASH, 142b95a5c4dSDaniel Mack BPF_MAP_TYPE_LPM_TRIE, 14356f668dfSMartin KaFai Lau BPF_MAP_TYPE_ARRAY_OF_MAPS, 144bcc6b1b7SMartin KaFai Lau BPF_MAP_TYPE_HASH_OF_MAPS, 145546ac1ffSJohn Fastabend BPF_MAP_TYPE_DEVMAP, 146174a79ffSJohn Fastabend BPF_MAP_TYPE_SOCKMAP, 1476710e112SJesper Dangaard Brouer BPF_MAP_TYPE_CPUMAP, 148fbfc504aSBjörn Töpel BPF_MAP_TYPE_XSKMAP, 14981110384SJohn Fastabend BPF_MAP_TYPE_SOCKHASH, 150de9cbbaaSRoman Gushchin BPF_MAP_TYPE_CGROUP_STORAGE, 1515dc4c4b7SMartin KaFai Lau BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, 152b741f163SRoman Gushchin BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, 153f1a2e44aSMauricio Vasquez B BPF_MAP_TYPE_QUEUE, 154f1a2e44aSMauricio Vasquez B BPF_MAP_TYPE_STACK, 1556ac99e8fSMartin KaFai Lau BPF_MAP_TYPE_SK_STORAGE, 1566f9d451aSToke Høiland-Jørgensen BPF_MAP_TYPE_DEVMAP_HASH, 15785d33df3SMartin KaFai Lau BPF_MAP_TYPE_STRUCT_OPS, 158457f4436SAndrii Nakryiko BPF_MAP_TYPE_RINGBUF, 1598ea63684SKP Singh BPF_MAP_TYPE_INODE_STORAGE, 16099c55f7dSAlexei Starovoitov }; 16199c55f7dSAlexei Starovoitov 1626c4fc209SDaniel Borkmann /* Note that tracing related programs such as 1636c4fc209SDaniel Borkmann * BPF_PROG_TYPE_{KPROBE,TRACEPOINT,PERF_EVENT,RAW_TRACEPOINT} 1646c4fc209SDaniel Borkmann * are not subject to a stable API since kernel internal data 1656c4fc209SDaniel Borkmann * structures can change from release to release and may 1666c4fc209SDaniel Borkmann * therefore break existing tracing BPF programs. Tracing BPF 1676c4fc209SDaniel Borkmann * programs correspond to /a/ specific kernel which is to be 1686c4fc209SDaniel Borkmann * analyzed, and not /a/ specific kernel /and/ all future ones. 1696c4fc209SDaniel Borkmann */ 17009756af4SAlexei Starovoitov enum bpf_prog_type { 17109756af4SAlexei Starovoitov BPF_PROG_TYPE_UNSPEC, 172ddd872bcSAlexei Starovoitov BPF_PROG_TYPE_SOCKET_FILTER, 1732541517cSAlexei Starovoitov BPF_PROG_TYPE_KPROBE, 17496be4325SDaniel Borkmann BPF_PROG_TYPE_SCHED_CLS, 17594caee8cSDaniel Borkmann BPF_PROG_TYPE_SCHED_ACT, 17698b5c2c6SAlexei Starovoitov BPF_PROG_TYPE_TRACEPOINT, 1776a773a15SBrenden Blanco BPF_PROG_TYPE_XDP, 1780515e599SAlexei Starovoitov BPF_PROG_TYPE_PERF_EVENT, 1790e33661dSDaniel Mack BPF_PROG_TYPE_CGROUP_SKB, 18061023658SDavid Ahern BPF_PROG_TYPE_CGROUP_SOCK, 1813a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_IN, 1823a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_OUT, 1833a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_XMIT, 18440304b2aSLawrence Brakmo BPF_PROG_TYPE_SOCK_OPS, 185b005fd18SJohn Fastabend BPF_PROG_TYPE_SK_SKB, 186ebc614f6SRoman Gushchin BPF_PROG_TYPE_CGROUP_DEVICE, 1874f738adbSJohn Fastabend BPF_PROG_TYPE_SK_MSG, 188c4f6699dSAlexei Starovoitov BPF_PROG_TYPE_RAW_TRACEPOINT, 1894fbac77dSAndrey Ignatov BPF_PROG_TYPE_CGROUP_SOCK_ADDR, 190004d4b27SMathieu Xhonneux BPF_PROG_TYPE_LWT_SEG6LOCAL, 191f4364dcfSSean Young BPF_PROG_TYPE_LIRC_MODE2, 1922dbb9b9eSMartin KaFai Lau BPF_PROG_TYPE_SK_REUSEPORT, 193d58e468bSPetar Penkov BPF_PROG_TYPE_FLOW_DISSECTOR, 1947b146cebSAndrey Ignatov BPF_PROG_TYPE_CGROUP_SYSCTL, 1959df1c28bSMatt Mullins BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, 1960d01da6aSStanislav Fomichev BPF_PROG_TYPE_CGROUP_SOCKOPT, 197f1b9509cSAlexei Starovoitov BPF_PROG_TYPE_TRACING, 19827ae7997SMartin KaFai Lau BPF_PROG_TYPE_STRUCT_OPS, 199be8704ffSAlexei Starovoitov BPF_PROG_TYPE_EXT, 200fc611f47SKP Singh BPF_PROG_TYPE_LSM, 201e9ddbb77SJakub Sitnicki BPF_PROG_TYPE_SK_LOOKUP, 20209756af4SAlexei Starovoitov }; 20309756af4SAlexei Starovoitov 2040e33661dSDaniel Mack enum bpf_attach_type { 2050e33661dSDaniel Mack BPF_CGROUP_INET_INGRESS, 2060e33661dSDaniel Mack BPF_CGROUP_INET_EGRESS, 20761023658SDavid Ahern BPF_CGROUP_INET_SOCK_CREATE, 20840304b2aSLawrence Brakmo BPF_CGROUP_SOCK_OPS, 209464bc0fdSJohn Fastabend BPF_SK_SKB_STREAM_PARSER, 210464bc0fdSJohn Fastabend BPF_SK_SKB_STREAM_VERDICT, 211ebc614f6SRoman Gushchin BPF_CGROUP_DEVICE, 2124f738adbSJohn Fastabend BPF_SK_MSG_VERDICT, 2134fbac77dSAndrey Ignatov BPF_CGROUP_INET4_BIND, 2144fbac77dSAndrey Ignatov BPF_CGROUP_INET6_BIND, 215d74bad4eSAndrey Ignatov BPF_CGROUP_INET4_CONNECT, 216d74bad4eSAndrey Ignatov BPF_CGROUP_INET6_CONNECT, 217aac3fc32SAndrey Ignatov BPF_CGROUP_INET4_POST_BIND, 218aac3fc32SAndrey Ignatov BPF_CGROUP_INET6_POST_BIND, 2191cedee13SAndrey Ignatov BPF_CGROUP_UDP4_SENDMSG, 2201cedee13SAndrey Ignatov BPF_CGROUP_UDP6_SENDMSG, 221f4364dcfSSean Young BPF_LIRC_MODE2, 222d58e468bSPetar Penkov BPF_FLOW_DISSECTOR, 2237b146cebSAndrey Ignatov BPF_CGROUP_SYSCTL, 224983695faSDaniel Borkmann BPF_CGROUP_UDP4_RECVMSG, 225983695faSDaniel Borkmann BPF_CGROUP_UDP6_RECVMSG, 2260d01da6aSStanislav Fomichev BPF_CGROUP_GETSOCKOPT, 2270d01da6aSStanislav Fomichev BPF_CGROUP_SETSOCKOPT, 228f1b9509cSAlexei Starovoitov BPF_TRACE_RAW_TP, 229fec56f58SAlexei Starovoitov BPF_TRACE_FENTRY, 230fec56f58SAlexei Starovoitov BPF_TRACE_FEXIT, 231ae240823SKP Singh BPF_MODIFY_RETURN, 232fc611f47SKP Singh BPF_LSM_MAC, 23315d83c4dSYonghong Song BPF_TRACE_ITER, 2341b66d253SDaniel Borkmann BPF_CGROUP_INET4_GETPEERNAME, 2351b66d253SDaniel Borkmann BPF_CGROUP_INET6_GETPEERNAME, 2361b66d253SDaniel Borkmann BPF_CGROUP_INET4_GETSOCKNAME, 2371b66d253SDaniel Borkmann BPF_CGROUP_INET6_GETSOCKNAME, 238fbee97feSDavid Ahern BPF_XDP_DEVMAP, 239f5836749SStanislav Fomichev BPF_CGROUP_INET_SOCK_RELEASE, 24092164774SLorenzo Bianconi BPF_XDP_CPUMAP, 241e9ddbb77SJakub Sitnicki BPF_SK_LOOKUP, 242aa8d3a71SAndrii Nakryiko BPF_XDP, 2430e33661dSDaniel Mack __MAX_BPF_ATTACH_TYPE 2440e33661dSDaniel Mack }; 2450e33661dSDaniel Mack 2460e33661dSDaniel Mack #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE 2470e33661dSDaniel Mack 248f2e10bffSAndrii Nakryiko enum bpf_link_type { 249f2e10bffSAndrii Nakryiko BPF_LINK_TYPE_UNSPEC = 0, 250f2e10bffSAndrii Nakryiko BPF_LINK_TYPE_RAW_TRACEPOINT = 1, 251f2e10bffSAndrii Nakryiko BPF_LINK_TYPE_TRACING = 2, 252f2e10bffSAndrii Nakryiko BPF_LINK_TYPE_CGROUP = 3, 253de4e05caSYonghong Song BPF_LINK_TYPE_ITER = 4, 2547f045a49SJakub Sitnicki BPF_LINK_TYPE_NETNS = 5, 255aa8d3a71SAndrii Nakryiko BPF_LINK_TYPE_XDP = 6, 256f2e10bffSAndrii Nakryiko 257f2e10bffSAndrii Nakryiko MAX_BPF_LINK_TYPE, 258f2e10bffSAndrii Nakryiko }; 259f2e10bffSAndrii Nakryiko 260324bda9eSAlexei Starovoitov /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command 261324bda9eSAlexei Starovoitov * 262324bda9eSAlexei Starovoitov * NONE(default): No further bpf programs allowed in the subtree. 263324bda9eSAlexei Starovoitov * 264324bda9eSAlexei Starovoitov * BPF_F_ALLOW_OVERRIDE: If a sub-cgroup installs some bpf program, 265324bda9eSAlexei Starovoitov * the program in this cgroup yields to sub-cgroup program. 266324bda9eSAlexei Starovoitov * 267324bda9eSAlexei Starovoitov * BPF_F_ALLOW_MULTI: If a sub-cgroup installs some bpf program, 268324bda9eSAlexei Starovoitov * that cgroup program gets run in addition to the program in this cgroup. 269324bda9eSAlexei Starovoitov * 270324bda9eSAlexei Starovoitov * Only one program is allowed to be attached to a cgroup with 271324bda9eSAlexei Starovoitov * NONE or BPF_F_ALLOW_OVERRIDE flag. 272324bda9eSAlexei Starovoitov * Attaching another program on top of NONE or BPF_F_ALLOW_OVERRIDE will 273324bda9eSAlexei Starovoitov * release old program and attach the new one. Attach flags has to match. 274324bda9eSAlexei Starovoitov * 275324bda9eSAlexei Starovoitov * Multiple programs are allowed to be attached to a cgroup with 276324bda9eSAlexei Starovoitov * BPF_F_ALLOW_MULTI flag. They are executed in FIFO order 277324bda9eSAlexei Starovoitov * (those that were attached first, run first) 278324bda9eSAlexei Starovoitov * The programs of sub-cgroup are executed first, then programs of 279324bda9eSAlexei Starovoitov * this cgroup and then programs of parent cgroup. 280324bda9eSAlexei Starovoitov * When children program makes decision (like picking TCP CA or sock bind) 281324bda9eSAlexei Starovoitov * parent program has a chance to override it. 282324bda9eSAlexei Starovoitov * 2837dd68b32SAndrey Ignatov * With BPF_F_ALLOW_MULTI a new program is added to the end of the list of 2847dd68b32SAndrey Ignatov * programs for a cgroup. Though it's possible to replace an old program at 2857dd68b32SAndrey Ignatov * any position by also specifying BPF_F_REPLACE flag and position itself in 2867dd68b32SAndrey Ignatov * replace_bpf_fd attribute. Old program at this position will be released. 2877dd68b32SAndrey Ignatov * 288324bda9eSAlexei Starovoitov * A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups. 289324bda9eSAlexei Starovoitov * A cgroup with NONE doesn't allow any programs in sub-cgroups. 290324bda9eSAlexei Starovoitov * Ex1: 291324bda9eSAlexei Starovoitov * cgrp1 (MULTI progs A, B) -> 292324bda9eSAlexei Starovoitov * cgrp2 (OVERRIDE prog C) -> 293324bda9eSAlexei Starovoitov * cgrp3 (MULTI prog D) -> 294324bda9eSAlexei Starovoitov * cgrp4 (OVERRIDE prog E) -> 295324bda9eSAlexei Starovoitov * cgrp5 (NONE prog F) 296324bda9eSAlexei Starovoitov * the event in cgrp5 triggers execution of F,D,A,B in that order. 297324bda9eSAlexei Starovoitov * if prog F is detached, the execution is E,D,A,B 298324bda9eSAlexei Starovoitov * if prog F and D are detached, the execution is E,A,B 299324bda9eSAlexei Starovoitov * if prog F, E and D are detached, the execution is C,A,B 300324bda9eSAlexei Starovoitov * 301324bda9eSAlexei Starovoitov * All eligible programs are executed regardless of return code from 302324bda9eSAlexei Starovoitov * earlier programs. 3037f677633SAlexei Starovoitov */ 3047f677633SAlexei Starovoitov #define BPF_F_ALLOW_OVERRIDE (1U << 0) 305324bda9eSAlexei Starovoitov #define BPF_F_ALLOW_MULTI (1U << 1) 3067dd68b32SAndrey Ignatov #define BPF_F_REPLACE (1U << 2) 3077f677633SAlexei Starovoitov 308e07b98d9SDavid S. Miller /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the 309e07b98d9SDavid S. Miller * verifier will perform strict alignment checking as if the kernel 310e07b98d9SDavid S. Miller * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set, 311e07b98d9SDavid S. Miller * and NET_IP_ALIGN defined to 2. 312e07b98d9SDavid S. Miller */ 313e07b98d9SDavid S. Miller #define BPF_F_STRICT_ALIGNMENT (1U << 0) 314e07b98d9SDavid S. Miller 315e9ee9efcSDavid Miller /* If BPF_F_ANY_ALIGNMENT is used in BPF_PROF_LOAD command, the 316e9ee9efcSDavid Miller * verifier will allow any alignment whatsoever. On platforms 317e9ee9efcSDavid Miller * with strict alignment requirements for loads ands stores (such 318e9ee9efcSDavid Miller * as sparc and mips) the verifier validates that all loads and 319e9ee9efcSDavid Miller * stores provably follow this requirement. This flag turns that 320e9ee9efcSDavid Miller * checking and enforcement off. 321e9ee9efcSDavid Miller * 322e9ee9efcSDavid Miller * It is mostly used for testing when we want to validate the 323e9ee9efcSDavid Miller * context and memory access aspects of the verifier, but because 324e9ee9efcSDavid Miller * of an unaligned access the alignment check would trigger before 325e9ee9efcSDavid Miller * the one we are interested in. 326e9ee9efcSDavid Miller */ 327e9ee9efcSDavid Miller #define BPF_F_ANY_ALIGNMENT (1U << 1) 328e9ee9efcSDavid Miller 329c240eff6SJiong Wang /* BPF_F_TEST_RND_HI32 is used in BPF_PROG_LOAD command for testing purpose. 330c240eff6SJiong Wang * Verifier does sub-register def/use analysis and identifies instructions whose 331c240eff6SJiong Wang * def only matters for low 32-bit, high 32-bit is never referenced later 332c240eff6SJiong Wang * through implicit zero extension. Therefore verifier notifies JIT back-ends 333c240eff6SJiong Wang * that it is safe to ignore clearing high 32-bit for these instructions. This 334c240eff6SJiong Wang * saves some back-ends a lot of code-gen. However such optimization is not 335c240eff6SJiong Wang * necessary on some arches, for example x86_64, arm64 etc, whose JIT back-ends 336c240eff6SJiong Wang * hence hasn't used verifier's analysis result. But, we really want to have a 337c240eff6SJiong Wang * way to be able to verify the correctness of the described optimization on 338c240eff6SJiong Wang * x86_64 on which testsuites are frequently exercised. 339c240eff6SJiong Wang * 340c240eff6SJiong Wang * So, this flag is introduced. Once it is set, verifier will randomize high 341c240eff6SJiong Wang * 32-bit for those instructions who has been identified as safe to ignore them. 342c240eff6SJiong Wang * Then, if verifier is not doing correct analysis, such randomization will 343c240eff6SJiong Wang * regress tests to expose bugs. 344c240eff6SJiong Wang */ 345c240eff6SJiong Wang #define BPF_F_TEST_RND_HI32 (1U << 2) 346c240eff6SJiong Wang 34710d274e8SAlexei Starovoitov /* The verifier internal test flag. Behavior is undefined */ 34810d274e8SAlexei Starovoitov #define BPF_F_TEST_STATE_FREQ (1U << 3) 34910d274e8SAlexei Starovoitov 3501e6c62a8SAlexei Starovoitov /* If BPF_F_SLEEPABLE is used in BPF_PROG_LOAD command, the verifier will 3511e6c62a8SAlexei Starovoitov * restrict map and helper usage for such programs. Sleepable BPF programs can 3521e6c62a8SAlexei Starovoitov * only be attached to hooks where kernel execution context allows sleeping. 3531e6c62a8SAlexei Starovoitov * Such programs are allowed to use helpers that may sleep like 3541e6c62a8SAlexei Starovoitov * bpf_copy_from_user(). 3551e6c62a8SAlexei Starovoitov */ 3561e6c62a8SAlexei Starovoitov #define BPF_F_SLEEPABLE (1U << 4) 3571e6c62a8SAlexei Starovoitov 358d8eca5bbSDaniel Borkmann /* When BPF ldimm64's insn[0].src_reg != 0 then this can have 359d8eca5bbSDaniel Borkmann * two extensions: 360d8eca5bbSDaniel Borkmann * 361d8eca5bbSDaniel Borkmann * insn[0].src_reg: BPF_PSEUDO_MAP_FD BPF_PSEUDO_MAP_VALUE 362d8eca5bbSDaniel Borkmann * insn[0].imm: map fd map fd 363d8eca5bbSDaniel Borkmann * insn[1].imm: 0 offset into value 364d8eca5bbSDaniel Borkmann * insn[0].off: 0 0 365d8eca5bbSDaniel Borkmann * insn[1].off: 0 0 366d8eca5bbSDaniel Borkmann * ldimm64 rewrite: address of map address of map[0]+offset 367d8eca5bbSDaniel Borkmann * verifier type: CONST_PTR_TO_MAP PTR_TO_MAP_VALUE 368d8eca5bbSDaniel Borkmann */ 369f1a66f85SDaniel Borkmann #define BPF_PSEUDO_MAP_FD 1 370d8eca5bbSDaniel Borkmann #define BPF_PSEUDO_MAP_VALUE 2 371f1a66f85SDaniel Borkmann 372cc8b0b92SAlexei Starovoitov /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative 373cc8b0b92SAlexei Starovoitov * offset to another bpf function 374cc8b0b92SAlexei Starovoitov */ 375cc8b0b92SAlexei Starovoitov #define BPF_PSEUDO_CALL 1 376cc8b0b92SAlexei Starovoitov 3773274f520SAlexei Starovoitov /* flags for BPF_MAP_UPDATE_ELEM command */ 3781aae4bddSAndrii Nakryiko enum { 3791aae4bddSAndrii Nakryiko BPF_ANY = 0, /* create new element or update existing */ 3801aae4bddSAndrii Nakryiko BPF_NOEXIST = 1, /* create new element if it didn't exist */ 3811aae4bddSAndrii Nakryiko BPF_EXIST = 2, /* update existing element */ 3821aae4bddSAndrii Nakryiko BPF_F_LOCK = 4, /* spin_lock-ed map_lookup/map_update */ 3831aae4bddSAndrii Nakryiko }; 3843274f520SAlexei Starovoitov 38596eabe7aSMartin KaFai Lau /* flags for BPF_MAP_CREATE command */ 3861aae4bddSAndrii Nakryiko enum { 3871aae4bddSAndrii Nakryiko BPF_F_NO_PREALLOC = (1U << 0), 38829ba732aSMartin KaFai Lau /* Instead of having one common LRU list in the 3898f844938SMartin KaFai Lau * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list 39029ba732aSMartin KaFai Lau * which can scale and perform better. 39129ba732aSMartin KaFai Lau * Note, the LRU nodes (including free nodes) cannot be moved 39229ba732aSMartin KaFai Lau * across different LRU lists. 39329ba732aSMartin KaFai Lau */ 3941aae4bddSAndrii Nakryiko BPF_F_NO_COMMON_LRU = (1U << 1), 39596eabe7aSMartin KaFai Lau /* Specify numa node during map creation */ 3961aae4bddSAndrii Nakryiko BPF_F_NUMA_NODE = (1U << 2), 397cb4d2b3fSMartin KaFai Lau 398591fe988SDaniel Borkmann /* Flags for accessing BPF object from syscall side. */ 3991aae4bddSAndrii Nakryiko BPF_F_RDONLY = (1U << 3), 4001aae4bddSAndrii Nakryiko BPF_F_WRONLY = (1U << 4), 4016e71b04aSChenbo Feng 402615755a7SSong Liu /* Flag for stack_map, store build_id+offset instead of pointer */ 4031aae4bddSAndrii Nakryiko BPF_F_STACK_BUILD_ID = (1U << 5), 404615755a7SSong Liu 40596b3b6c9SLorenz Bauer /* Zero-initialize hash function seed. This should only be used for testing. */ 4061aae4bddSAndrii Nakryiko BPF_F_ZERO_SEED = (1U << 6), 40796b3b6c9SLorenz Bauer 408591fe988SDaniel Borkmann /* Flags for accessing BPF object from program side. */ 4091aae4bddSAndrii Nakryiko BPF_F_RDONLY_PROG = (1U << 7), 4101aae4bddSAndrii Nakryiko BPF_F_WRONLY_PROG = (1U << 8), 411591fe988SDaniel Borkmann 4128f51dfc7SStanislav Fomichev /* Clone map from listener for newly accepted socket */ 4131aae4bddSAndrii Nakryiko BPF_F_CLONE = (1U << 9), 4148f51dfc7SStanislav Fomichev 415fc970227SAndrii Nakryiko /* Enable memory-mapping BPF map */ 4161aae4bddSAndrii Nakryiko BPF_F_MMAPABLE = (1U << 10), 4171aae4bddSAndrii Nakryiko }; 418fc970227SAndrii Nakryiko 419f5bfcd95SAndrey Ignatov /* Flags for BPF_PROG_QUERY. */ 420f5bfcd95SAndrey Ignatov 421f5bfcd95SAndrey Ignatov /* Query effective (directly attached + inherited from ancestor cgroups) 422f5bfcd95SAndrey Ignatov * programs that will be executed for events within a cgroup. 423f5bfcd95SAndrey Ignatov * attach_flags with this flag are returned only for directly attached programs. 424f5bfcd95SAndrey Ignatov */ 4252f183360SLorenz Bauer #define BPF_F_QUERY_EFFECTIVE (1U << 0) 4262f183360SLorenz Bauer 4271b4d60ecSSong Liu /* Flags for BPF_PROG_TEST_RUN */ 4281b4d60ecSSong Liu 4291b4d60ecSSong Liu /* If set, run the test on the cpu specified by bpf_attr.test.cpu */ 4301b4d60ecSSong Liu #define BPF_F_TEST_RUN_ON_CPU (1U << 0) 4311b4d60ecSSong Liu 432d46edd67SSong Liu /* type for BPF_ENABLE_STATS */ 433d46edd67SSong Liu enum bpf_stats_type { 434d46edd67SSong Liu /* enabled run_time_ns and run_cnt */ 435d46edd67SSong Liu BPF_STATS_RUN_TIME = 0, 436d46edd67SSong Liu }; 437d46edd67SSong Liu 438615755a7SSong Liu enum bpf_stack_build_id_status { 439615755a7SSong Liu /* user space need an empty entry to identify end of a trace */ 440615755a7SSong Liu BPF_STACK_BUILD_ID_EMPTY = 0, 441615755a7SSong Liu /* with valid build_id and offset */ 442615755a7SSong Liu BPF_STACK_BUILD_ID_VALID = 1, 443615755a7SSong Liu /* couldn't get build_id, fallback to ip */ 444615755a7SSong Liu BPF_STACK_BUILD_ID_IP = 2, 445615755a7SSong Liu }; 446615755a7SSong Liu 447615755a7SSong Liu #define BPF_BUILD_ID_SIZE 20 448615755a7SSong Liu struct bpf_stack_build_id { 449615755a7SSong Liu __s32 status; 450615755a7SSong Liu unsigned char build_id[BPF_BUILD_ID_SIZE]; 451615755a7SSong Liu union { 452615755a7SSong Liu __u64 offset; 453615755a7SSong Liu __u64 ip; 454615755a7SSong Liu }; 455615755a7SSong Liu }; 456615755a7SSong Liu 4571aae4bddSAndrii Nakryiko #define BPF_OBJ_NAME_LEN 16U 4581aae4bddSAndrii Nakryiko 45999c55f7dSAlexei Starovoitov union bpf_attr { 46099c55f7dSAlexei Starovoitov struct { /* anonymous struct used by BPF_MAP_CREATE command */ 46199c55f7dSAlexei Starovoitov __u32 map_type; /* one of enum bpf_map_type */ 46299c55f7dSAlexei Starovoitov __u32 key_size; /* size of key in bytes */ 46399c55f7dSAlexei Starovoitov __u32 value_size; /* size of value in bytes */ 46499c55f7dSAlexei Starovoitov __u32 max_entries; /* max number of entries in a map */ 46596eabe7aSMartin KaFai Lau __u32 map_flags; /* BPF_MAP_CREATE related 46696eabe7aSMartin KaFai Lau * flags defined above. 46796eabe7aSMartin KaFai Lau */ 46856f668dfSMartin KaFai Lau __u32 inner_map_fd; /* fd pointing to the inner map */ 46996eabe7aSMartin KaFai Lau __u32 numa_node; /* numa node (effective only if 47096eabe7aSMartin KaFai Lau * BPF_F_NUMA_NODE is set). 47196eabe7aSMartin KaFai Lau */ 472067cae47SMartin KaFai Lau char map_name[BPF_OBJ_NAME_LEN]; 473a3884572SJakub Kicinski __u32 map_ifindex; /* ifindex of netdev to create on */ 474a26ca7c9SMartin KaFai Lau __u32 btf_fd; /* fd pointing to a BTF type data */ 4759b2cf328SMartin KaFai Lau __u32 btf_key_type_id; /* BTF type_id of the key */ 4769b2cf328SMartin KaFai Lau __u32 btf_value_type_id; /* BTF type_id of the value */ 47785d33df3SMartin KaFai Lau __u32 btf_vmlinux_value_type_id;/* BTF type_id of a kernel- 47885d33df3SMartin KaFai Lau * struct stored as the 47985d33df3SMartin KaFai Lau * map value 48085d33df3SMartin KaFai Lau */ 48199c55f7dSAlexei Starovoitov }; 482db20fd2bSAlexei Starovoitov 483db20fd2bSAlexei Starovoitov struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ 484db20fd2bSAlexei Starovoitov __u32 map_fd; 485db20fd2bSAlexei Starovoitov __aligned_u64 key; 486db20fd2bSAlexei Starovoitov union { 487db20fd2bSAlexei Starovoitov __aligned_u64 value; 488db20fd2bSAlexei Starovoitov __aligned_u64 next_key; 489db20fd2bSAlexei Starovoitov }; 4903274f520SAlexei Starovoitov __u64 flags; 491db20fd2bSAlexei Starovoitov }; 49209756af4SAlexei Starovoitov 493cb4d03abSBrian Vazquez struct { /* struct used by BPF_MAP_*_BATCH commands */ 494cb4d03abSBrian Vazquez __aligned_u64 in_batch; /* start batch, 495cb4d03abSBrian Vazquez * NULL to start from beginning 496cb4d03abSBrian Vazquez */ 497cb4d03abSBrian Vazquez __aligned_u64 out_batch; /* output: next start batch */ 498cb4d03abSBrian Vazquez __aligned_u64 keys; 499cb4d03abSBrian Vazquez __aligned_u64 values; 500cb4d03abSBrian Vazquez __u32 count; /* input/output: 501cb4d03abSBrian Vazquez * input: # of key/value 502cb4d03abSBrian Vazquez * elements 503cb4d03abSBrian Vazquez * output: # of filled elements 504cb4d03abSBrian Vazquez */ 505cb4d03abSBrian Vazquez __u32 map_fd; 506cb4d03abSBrian Vazquez __u64 elem_flags; 507cb4d03abSBrian Vazquez __u64 flags; 508cb4d03abSBrian Vazquez } batch; 509cb4d03abSBrian Vazquez 51009756af4SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_LOAD command */ 51109756af4SAlexei Starovoitov __u32 prog_type; /* one of enum bpf_prog_type */ 51209756af4SAlexei Starovoitov __u32 insn_cnt; 51309756af4SAlexei Starovoitov __aligned_u64 insns; 51409756af4SAlexei Starovoitov __aligned_u64 license; 515cbd35700SAlexei Starovoitov __u32 log_level; /* verbosity level of verifier */ 516cbd35700SAlexei Starovoitov __u32 log_size; /* size of user buffer */ 517cbd35700SAlexei Starovoitov __aligned_u64 log_buf; /* user supplied buffer */ 5186c4fc209SDaniel Borkmann __u32 kern_version; /* not used */ 519e07b98d9SDavid S. Miller __u32 prog_flags; 520067cae47SMartin KaFai Lau char prog_name[BPF_OBJ_NAME_LEN]; 5211f6f4cb7SJakub Kicinski __u32 prog_ifindex; /* ifindex of netdev to prep for */ 5225e43f899SAndrey Ignatov /* For some prog types expected attach type must be known at 5235e43f899SAndrey Ignatov * load time to verify attach type specific parts of prog 5245e43f899SAndrey Ignatov * (context accesses, allowed helpers, etc). 5255e43f899SAndrey Ignatov */ 5265e43f899SAndrey Ignatov __u32 expected_attach_type; 527838e9690SYonghong Song __u32 prog_btf_fd; /* fd pointing to BTF type data */ 528838e9690SYonghong Song __u32 func_info_rec_size; /* userspace bpf_func_info size */ 529838e9690SYonghong Song __aligned_u64 func_info; /* func info */ 530838e9690SYonghong Song __u32 func_info_cnt; /* number of bpf_func_info records */ 531c454a46bSMartin KaFai Lau __u32 line_info_rec_size; /* userspace bpf_line_info size */ 532c454a46bSMartin KaFai Lau __aligned_u64 line_info; /* line info */ 533c454a46bSMartin KaFai Lau __u32 line_info_cnt; /* number of bpf_line_info records */ 534ccfe29ebSAlexei Starovoitov __u32 attach_btf_id; /* in-kernel BTF type id to attach to */ 5355b92a28aSAlexei Starovoitov __u32 attach_prog_fd; /* 0 to attach to vmlinux */ 53609756af4SAlexei Starovoitov }; 537b2197755SDaniel Borkmann 538b2197755SDaniel Borkmann struct { /* anonymous struct used by BPF_OBJ_* commands */ 539b2197755SDaniel Borkmann __aligned_u64 pathname; 540b2197755SDaniel Borkmann __u32 bpf_fd; 5416e71b04aSChenbo Feng __u32 file_flags; 542b2197755SDaniel Borkmann }; 543f4324551SDaniel Mack 544f4324551SDaniel Mack struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ 545f4324551SDaniel Mack __u32 target_fd; /* container object to attach to */ 546f4324551SDaniel Mack __u32 attach_bpf_fd; /* eBPF program to attach */ 547f4324551SDaniel Mack __u32 attach_type; 5487f677633SAlexei Starovoitov __u32 attach_flags; 5497dd68b32SAndrey Ignatov __u32 replace_bpf_fd; /* previously attached eBPF 5507dd68b32SAndrey Ignatov * program to replace if 5517dd68b32SAndrey Ignatov * BPF_F_REPLACE is used 5527dd68b32SAndrey Ignatov */ 553f4324551SDaniel Mack }; 5541cf1cae9SAlexei Starovoitov 5551cf1cae9SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */ 5561cf1cae9SAlexei Starovoitov __u32 prog_fd; 5571cf1cae9SAlexei Starovoitov __u32 retval; 558b5a36b1eSLorenz Bauer __u32 data_size_in; /* input: len of data_in */ 559b5a36b1eSLorenz Bauer __u32 data_size_out; /* input/output: len of data_out 560b5a36b1eSLorenz Bauer * returns ENOSPC if data_out 561b5a36b1eSLorenz Bauer * is too small. 562b5a36b1eSLorenz Bauer */ 5631cf1cae9SAlexei Starovoitov __aligned_u64 data_in; 5641cf1cae9SAlexei Starovoitov __aligned_u64 data_out; 5651cf1cae9SAlexei Starovoitov __u32 repeat; 5661cf1cae9SAlexei Starovoitov __u32 duration; 567b0b9395dSStanislav Fomichev __u32 ctx_size_in; /* input: len of ctx_in */ 568b0b9395dSStanislav Fomichev __u32 ctx_size_out; /* input/output: len of ctx_out 569b0b9395dSStanislav Fomichev * returns ENOSPC if ctx_out 570b0b9395dSStanislav Fomichev * is too small. 571b0b9395dSStanislav Fomichev */ 572b0b9395dSStanislav Fomichev __aligned_u64 ctx_in; 573b0b9395dSStanislav Fomichev __aligned_u64 ctx_out; 5741b4d60ecSSong Liu __u32 flags; 5751b4d60ecSSong Liu __u32 cpu; 5761cf1cae9SAlexei Starovoitov } test; 57734ad5580SMartin KaFai Lau 578b16d9aa4SMartin KaFai Lau struct { /* anonymous struct used by BPF_*_GET_*_ID */ 579b16d9aa4SMartin KaFai Lau union { 58034ad5580SMartin KaFai Lau __u32 start_id; 581b16d9aa4SMartin KaFai Lau __u32 prog_id; 582bd5f5f4eSMartin KaFai Lau __u32 map_id; 58378958fcaSMartin KaFai Lau __u32 btf_id; 584a3b80e10SAndrii Nakryiko __u32 link_id; 585b16d9aa4SMartin KaFai Lau }; 58634ad5580SMartin KaFai Lau __u32 next_id; 5876e71b04aSChenbo Feng __u32 open_flags; 58834ad5580SMartin KaFai Lau }; 5891e270976SMartin KaFai Lau 5901e270976SMartin KaFai Lau struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ 5911e270976SMartin KaFai Lau __u32 bpf_fd; 5921e270976SMartin KaFai Lau __u32 info_len; 5931e270976SMartin KaFai Lau __aligned_u64 info; 5941e270976SMartin KaFai Lau } info; 595468e2f64SAlexei Starovoitov 596468e2f64SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_QUERY command */ 597468e2f64SAlexei Starovoitov __u32 target_fd; /* container object to query */ 598468e2f64SAlexei Starovoitov __u32 attach_type; 599468e2f64SAlexei Starovoitov __u32 query_flags; 600468e2f64SAlexei Starovoitov __u32 attach_flags; 601468e2f64SAlexei Starovoitov __aligned_u64 prog_ids; 602468e2f64SAlexei Starovoitov __u32 prog_cnt; 603468e2f64SAlexei Starovoitov } query; 604c4f6699dSAlexei Starovoitov 605af6eea57SAndrii Nakryiko struct { /* anonymous struct used by BPF_RAW_TRACEPOINT_OPEN command */ 606c4f6699dSAlexei Starovoitov __u64 name; 607c4f6699dSAlexei Starovoitov __u32 prog_fd; 608c4f6699dSAlexei Starovoitov } raw_tracepoint; 609f56a653cSMartin KaFai Lau 610f56a653cSMartin KaFai Lau struct { /* anonymous struct for BPF_BTF_LOAD */ 611f56a653cSMartin KaFai Lau __aligned_u64 btf; 612f56a653cSMartin KaFai Lau __aligned_u64 btf_log_buf; 613f56a653cSMartin KaFai Lau __u32 btf_size; 614f56a653cSMartin KaFai Lau __u32 btf_log_size; 615f56a653cSMartin KaFai Lau __u32 btf_log_level; 616f56a653cSMartin KaFai Lau }; 61741bdc4b4SYonghong Song 61841bdc4b4SYonghong Song struct { 61941bdc4b4SYonghong Song __u32 pid; /* input: pid */ 62041bdc4b4SYonghong Song __u32 fd; /* input: fd */ 62141bdc4b4SYonghong Song __u32 flags; /* input: flags */ 62241bdc4b4SYonghong Song __u32 buf_len; /* input/output: buf len */ 62341bdc4b4SYonghong Song __aligned_u64 buf; /* input/output: 62441bdc4b4SYonghong Song * tp_name for tracepoint 62541bdc4b4SYonghong Song * symbol for kprobe 62641bdc4b4SYonghong Song * filename for uprobe 62741bdc4b4SYonghong Song */ 62841bdc4b4SYonghong Song __u32 prog_id; /* output: prod_id */ 62941bdc4b4SYonghong Song __u32 fd_type; /* output: BPF_FD_TYPE_* */ 63041bdc4b4SYonghong Song __u64 probe_offset; /* output: probe_offset */ 63141bdc4b4SYonghong Song __u64 probe_addr; /* output: probe_addr */ 63241bdc4b4SYonghong Song } task_fd_query; 633af6eea57SAndrii Nakryiko 634af6eea57SAndrii Nakryiko struct { /* struct used by BPF_LINK_CREATE command */ 635af6eea57SAndrii Nakryiko __u32 prog_fd; /* eBPF program to attach */ 636aa8d3a71SAndrii Nakryiko union { 637af6eea57SAndrii Nakryiko __u32 target_fd; /* object to attach to */ 638aa8d3a71SAndrii Nakryiko __u32 target_ifindex; /* target ifindex */ 639aa8d3a71SAndrii Nakryiko }; 640af6eea57SAndrii Nakryiko __u32 attach_type; /* attach type */ 641af6eea57SAndrii Nakryiko __u32 flags; /* extra flags */ 642*4a1e7c0cSToke Høiland-Jørgensen union { 643*4a1e7c0cSToke Høiland-Jørgensen __u32 target_btf_id; /* btf_id of target to attach to */ 644*4a1e7c0cSToke Høiland-Jørgensen struct { 6455e7b3020SYonghong Song __aligned_u64 iter_info; /* extra bpf_iter_link_info */ 6465e7b3020SYonghong Song __u32 iter_info_len; /* iter_info length */ 647*4a1e7c0cSToke Høiland-Jørgensen }; 648*4a1e7c0cSToke Høiland-Jørgensen }; 649af6eea57SAndrii Nakryiko } link_create; 6500c991ebcSAndrii Nakryiko 6510c991ebcSAndrii Nakryiko struct { /* struct used by BPF_LINK_UPDATE command */ 6520c991ebcSAndrii Nakryiko __u32 link_fd; /* link fd */ 6530c991ebcSAndrii Nakryiko /* new program fd to update link with */ 6540c991ebcSAndrii Nakryiko __u32 new_prog_fd; 6550c991ebcSAndrii Nakryiko __u32 flags; /* extra flags */ 6560c991ebcSAndrii Nakryiko /* expected link's program fd; is specified only if 6570c991ebcSAndrii Nakryiko * BPF_F_REPLACE flag is set in flags */ 6580c991ebcSAndrii Nakryiko __u32 old_prog_fd; 6590c991ebcSAndrii Nakryiko } link_update; 6600c991ebcSAndrii Nakryiko 66173b11c2aSAndrii Nakryiko struct { 66273b11c2aSAndrii Nakryiko __u32 link_fd; 66373b11c2aSAndrii Nakryiko } link_detach; 66473b11c2aSAndrii Nakryiko 665d46edd67SSong Liu struct { /* struct used by BPF_ENABLE_STATS command */ 666d46edd67SSong Liu __u32 type; 667d46edd67SSong Liu } enable_stats; 668d46edd67SSong Liu 669ac51d99bSYonghong Song struct { /* struct used by BPF_ITER_CREATE command */ 670ac51d99bSYonghong Song __u32 link_fd; 671ac51d99bSYonghong Song __u32 flags; 672ac51d99bSYonghong Song } iter_create; 673ac51d99bSYonghong Song 674ef15314aSYiFei Zhu struct { /* struct used by BPF_PROG_BIND_MAP command */ 675ef15314aSYiFei Zhu __u32 prog_fd; 676ef15314aSYiFei Zhu __u32 map_fd; 677ef15314aSYiFei Zhu __u32 flags; /* extra flags */ 678ef15314aSYiFei Zhu } prog_bind_map; 679ef15314aSYiFei Zhu 68099c55f7dSAlexei Starovoitov } __attribute__((aligned(8))); 68199c55f7dSAlexei Starovoitov 68256a092c8SQuentin Monnet /* The description below is an attempt at providing documentation to eBPF 68356a092c8SQuentin Monnet * developers about the multiple available eBPF helper functions. It can be 68456a092c8SQuentin Monnet * parsed and used to produce a manual page. The workflow is the following, 68556a092c8SQuentin Monnet * and requires the rst2man utility: 686ebb676daSThomas Graf * 68756a092c8SQuentin Monnet * $ ./scripts/bpf_helpers_doc.py \ 68856a092c8SQuentin Monnet * --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst 68956a092c8SQuentin Monnet * $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7 69056a092c8SQuentin Monnet * $ man /tmp/bpf-helpers.7 691ebb676daSThomas Graf * 69256a092c8SQuentin Monnet * Note that in order to produce this external documentation, some RST 69356a092c8SQuentin Monnet * formatting is used in the descriptions to get "bold" and "italics" in 69456a092c8SQuentin Monnet * manual pages. Also note that the few trailing white spaces are 69556a092c8SQuentin Monnet * intentional, removing them would break paragraphs for rst2man. 696ebb676daSThomas Graf * 69756a092c8SQuentin Monnet * Start of BPF helper function descriptions: 698ad4a5223SQuentin Monnet * 699ad4a5223SQuentin Monnet * void *bpf_map_lookup_elem(struct bpf_map *map, const void *key) 700ad4a5223SQuentin Monnet * Description 701ad4a5223SQuentin Monnet * Perform a lookup in *map* for an entry associated to *key*. 702ad4a5223SQuentin Monnet * Return 703ad4a5223SQuentin Monnet * Map value associated to *key*, or **NULL** if no entry was 704ad4a5223SQuentin Monnet * found. 705ad4a5223SQuentin Monnet * 706bdb7b79bSAndrii Nakryiko * long bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags) 707ad4a5223SQuentin Monnet * Description 708ad4a5223SQuentin Monnet * Add or update the value of the entry associated to *key* in 709ad4a5223SQuentin Monnet * *map* with *value*. *flags* is one of: 710ad4a5223SQuentin Monnet * 711ad4a5223SQuentin Monnet * **BPF_NOEXIST** 712ad4a5223SQuentin Monnet * The entry for *key* must not exist in the map. 713ad4a5223SQuentin Monnet * **BPF_EXIST** 714ad4a5223SQuentin Monnet * The entry for *key* must already exist in the map. 715ad4a5223SQuentin Monnet * **BPF_ANY** 716ad4a5223SQuentin Monnet * No condition on the existence of the entry for *key*. 717ad4a5223SQuentin Monnet * 718ad4a5223SQuentin Monnet * Flag value **BPF_NOEXIST** cannot be used for maps of types 719ad4a5223SQuentin Monnet * **BPF_MAP_TYPE_ARRAY** or **BPF_MAP_TYPE_PERCPU_ARRAY** (all 720ad4a5223SQuentin Monnet * elements always exist), the helper would return an error. 721ad4a5223SQuentin Monnet * Return 722ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 723ad4a5223SQuentin Monnet * 724bdb7b79bSAndrii Nakryiko * long bpf_map_delete_elem(struct bpf_map *map, const void *key) 725ad4a5223SQuentin Monnet * Description 726ad4a5223SQuentin Monnet * Delete entry with *key* from *map*. 727ad4a5223SQuentin Monnet * Return 728ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 729ad4a5223SQuentin Monnet * 730bdb7b79bSAndrii Nakryiko * long bpf_probe_read(void *dst, u32 size, const void *unsafe_ptr) 731ad4a5223SQuentin Monnet * Description 732ad4a5223SQuentin Monnet * For tracing programs, safely attempt to read *size* bytes from 7336ae08ae3SDaniel Borkmann * kernel space address *unsafe_ptr* and store the data in *dst*. 7346ae08ae3SDaniel Borkmann * 735ab8d7809SQuentin Monnet * Generally, use **bpf_probe_read_user**\ () or 736ab8d7809SQuentin Monnet * **bpf_probe_read_kernel**\ () instead. 737ad4a5223SQuentin Monnet * Return 738ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 739ad4a5223SQuentin Monnet * 740ad4a5223SQuentin Monnet * u64 bpf_ktime_get_ns(void) 741ad4a5223SQuentin Monnet * Description 742ad4a5223SQuentin Monnet * Return the time elapsed since system boot, in nanoseconds. 74371d19214SMaciej Żenczykowski * Does not include time the system was suspended. 744ab8d7809SQuentin Monnet * See: **clock_gettime**\ (**CLOCK_MONOTONIC**) 745ad4a5223SQuentin Monnet * Return 746ad4a5223SQuentin Monnet * Current *ktime*. 747ad4a5223SQuentin Monnet * 748bdb7b79bSAndrii Nakryiko * long bpf_trace_printk(const char *fmt, u32 fmt_size, ...) 749ad4a5223SQuentin Monnet * Description 750ad4a5223SQuentin Monnet * This helper is a "printk()-like" facility for debugging. It 751ad4a5223SQuentin Monnet * prints a message defined by format *fmt* (of size *fmt_size*) 752ad4a5223SQuentin Monnet * to file *\/sys/kernel/debug/tracing/trace* from DebugFS, if 753ad4a5223SQuentin Monnet * available. It can take up to three additional **u64** 754ad4a5223SQuentin Monnet * arguments (as an eBPF helpers, the total number of arguments is 755ad4a5223SQuentin Monnet * limited to five). 756ad4a5223SQuentin Monnet * 757ad4a5223SQuentin Monnet * Each time the helper is called, it appends a line to the trace. 75855c33dfbSPeter Wu * Lines are discarded while *\/sys/kernel/debug/tracing/trace* is 75955c33dfbSPeter Wu * open, use *\/sys/kernel/debug/tracing/trace_pipe* to avoid this. 760ad4a5223SQuentin Monnet * The format of the trace is customizable, and the exact output 761ad4a5223SQuentin Monnet * one will get depends on the options set in 762ad4a5223SQuentin Monnet * *\/sys/kernel/debug/tracing/trace_options* (see also the 763ad4a5223SQuentin Monnet * *README* file under the same directory). However, it usually 764ad4a5223SQuentin Monnet * defaults to something like: 765ad4a5223SQuentin Monnet * 766ad4a5223SQuentin Monnet * :: 767ad4a5223SQuentin Monnet * 768ad4a5223SQuentin Monnet * telnet-470 [001] .N.. 419421.045894: 0x00000001: <formatted msg> 769ad4a5223SQuentin Monnet * 770ad4a5223SQuentin Monnet * In the above: 771ad4a5223SQuentin Monnet * 772ad4a5223SQuentin Monnet * * ``telnet`` is the name of the current task. 773ad4a5223SQuentin Monnet * * ``470`` is the PID of the current task. 774ad4a5223SQuentin Monnet * * ``001`` is the CPU number on which the task is 775ad4a5223SQuentin Monnet * running. 776ad4a5223SQuentin Monnet * * In ``.N..``, each character refers to a set of 777ad4a5223SQuentin Monnet * options (whether irqs are enabled, scheduling 778ad4a5223SQuentin Monnet * options, whether hard/softirqs are running, level of 779ad4a5223SQuentin Monnet * preempt_disabled respectively). **N** means that 780ad4a5223SQuentin Monnet * **TIF_NEED_RESCHED** and **PREEMPT_NEED_RESCHED** 781ad4a5223SQuentin Monnet * are set. 782ad4a5223SQuentin Monnet * * ``419421.045894`` is a timestamp. 783ad4a5223SQuentin Monnet * * ``0x00000001`` is a fake value used by BPF for the 784ad4a5223SQuentin Monnet * instruction pointer register. 785ad4a5223SQuentin Monnet * * ``<formatted msg>`` is the message formatted with 786ad4a5223SQuentin Monnet * *fmt*. 787ad4a5223SQuentin Monnet * 788ad4a5223SQuentin Monnet * The conversion specifiers supported by *fmt* are similar, but 789ad4a5223SQuentin Monnet * more limited than for printk(). They are **%d**, **%i**, 790ad4a5223SQuentin Monnet * **%u**, **%x**, **%ld**, **%li**, **%lu**, **%lx**, **%lld**, 791ad4a5223SQuentin Monnet * **%lli**, **%llu**, **%llx**, **%p**, **%s**. No modifier (size 792ad4a5223SQuentin Monnet * of field, padding with zeroes, etc.) is available, and the 793ad4a5223SQuentin Monnet * helper will return **-EINVAL** (but print nothing) if it 794ad4a5223SQuentin Monnet * encounters an unknown specifier. 795ad4a5223SQuentin Monnet * 796ad4a5223SQuentin Monnet * Also, note that **bpf_trace_printk**\ () is slow, and should 797ad4a5223SQuentin Monnet * only be used for debugging purposes. For this reason, a notice 798b16fc097STobias Klauser * block (spanning several lines) is printed to kernel logs and 799ad4a5223SQuentin Monnet * states that the helper should not be used "for production use" 800ad4a5223SQuentin Monnet * the first time this helper is used (or more precisely, when 801ad4a5223SQuentin Monnet * **trace_printk**\ () buffers are allocated). For passing values 802ad4a5223SQuentin Monnet * to user space, perf events should be preferred. 803ad4a5223SQuentin Monnet * Return 804ad4a5223SQuentin Monnet * The number of bytes written to the buffer, or a negative error 805ad4a5223SQuentin Monnet * in case of failure. 806ad4a5223SQuentin Monnet * 8071fdd08beSQuentin Monnet * u32 bpf_get_prandom_u32(void) 8081fdd08beSQuentin Monnet * Description 8091fdd08beSQuentin Monnet * Get a pseudo-random number. 8101fdd08beSQuentin Monnet * 8111fdd08beSQuentin Monnet * From a security point of view, this helper uses its own 8121fdd08beSQuentin Monnet * pseudo-random internal state, and cannot be used to infer the 8131fdd08beSQuentin Monnet * seed of other random functions in the kernel. However, it is 8141fdd08beSQuentin Monnet * essential to note that the generator used by the helper is not 8151fdd08beSQuentin Monnet * cryptographically secure. 8161fdd08beSQuentin Monnet * Return 8171fdd08beSQuentin Monnet * A random 32-bit unsigned value. 8181fdd08beSQuentin Monnet * 8191fdd08beSQuentin Monnet * u32 bpf_get_smp_processor_id(void) 8201fdd08beSQuentin Monnet * Description 8211fdd08beSQuentin Monnet * Get the SMP (symmetric multiprocessing) processor id. Note that 8221fdd08beSQuentin Monnet * all programs run with preemption disabled, which means that the 8231fdd08beSQuentin Monnet * SMP processor id is stable during all the execution of the 8241fdd08beSQuentin Monnet * program. 8251fdd08beSQuentin Monnet * Return 8261fdd08beSQuentin Monnet * The SMP id of the processor running the program. 8271fdd08beSQuentin Monnet * 828bdb7b79bSAndrii Nakryiko * long bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags) 829ad4a5223SQuentin Monnet * Description 830ad4a5223SQuentin Monnet * Store *len* bytes from address *from* into the packet 831ad4a5223SQuentin Monnet * associated to *skb*, at *offset*. *flags* are a combination of 832ad4a5223SQuentin Monnet * **BPF_F_RECOMPUTE_CSUM** (automatically recompute the 833ad4a5223SQuentin Monnet * checksum for the packet after storing the bytes) and 834ad4a5223SQuentin Monnet * **BPF_F_INVALIDATE_HASH** (set *skb*\ **->hash**, *skb*\ 835ad4a5223SQuentin Monnet * **->swhash** and *skb*\ **->l4hash** to 0). 836ad4a5223SQuentin Monnet * 83732e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 838ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 839ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 840ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 841ad4a5223SQuentin Monnet * direct packet access. 842ad4a5223SQuentin Monnet * Return 843ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 844ad4a5223SQuentin Monnet * 845bdb7b79bSAndrii Nakryiko * long bpf_l3_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 size) 846ad4a5223SQuentin Monnet * Description 847ad4a5223SQuentin Monnet * Recompute the layer 3 (e.g. IP) checksum for the packet 848ad4a5223SQuentin Monnet * associated to *skb*. Computation is incremental, so the helper 849ad4a5223SQuentin Monnet * must know the former value of the header field that was 850ad4a5223SQuentin Monnet * modified (*from*), the new value of this field (*to*), and the 851ad4a5223SQuentin Monnet * number of bytes (2 or 4) for this field, stored in *size*. 852ad4a5223SQuentin Monnet * Alternatively, it is possible to store the difference between 853ad4a5223SQuentin Monnet * the previous and the new values of the header field in *to*, by 854ad4a5223SQuentin Monnet * setting *from* and *size* to 0. For both methods, *offset* 855ad4a5223SQuentin Monnet * indicates the location of the IP checksum within the packet. 856ad4a5223SQuentin Monnet * 857ad4a5223SQuentin Monnet * This helper works in combination with **bpf_csum_diff**\ (), 858ad4a5223SQuentin Monnet * which does not update the checksum in-place, but offers more 859ad4a5223SQuentin Monnet * flexibility and can handle sizes larger than 2 or 4 for the 860ad4a5223SQuentin Monnet * checksum to update. 861ad4a5223SQuentin Monnet * 86232e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 863ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 864ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 865ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 866ad4a5223SQuentin Monnet * direct packet access. 867ad4a5223SQuentin Monnet * Return 868ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 869ad4a5223SQuentin Monnet * 870bdb7b79bSAndrii Nakryiko * long bpf_l4_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 flags) 871ad4a5223SQuentin Monnet * Description 872ad4a5223SQuentin Monnet * Recompute the layer 4 (e.g. TCP, UDP or ICMP) checksum for the 873ad4a5223SQuentin Monnet * packet associated to *skb*. Computation is incremental, so the 874ad4a5223SQuentin Monnet * helper must know the former value of the header field that was 875ad4a5223SQuentin Monnet * modified (*from*), the new value of this field (*to*), and the 876ad4a5223SQuentin Monnet * number of bytes (2 or 4) for this field, stored on the lowest 877ad4a5223SQuentin Monnet * four bits of *flags*. Alternatively, it is possible to store 878ad4a5223SQuentin Monnet * the difference between the previous and the new values of the 879ad4a5223SQuentin Monnet * header field in *to*, by setting *from* and the four lowest 880ad4a5223SQuentin Monnet * bits of *flags* to 0. For both methods, *offset* indicates the 881ad4a5223SQuentin Monnet * location of the IP checksum within the packet. In addition to 882ad4a5223SQuentin Monnet * the size of the field, *flags* can be added (bitwise OR) actual 883ad4a5223SQuentin Monnet * flags. With **BPF_F_MARK_MANGLED_0**, a null checksum is left 884ad4a5223SQuentin Monnet * untouched (unless **BPF_F_MARK_ENFORCE** is added as well), and 885ad4a5223SQuentin Monnet * for updates resulting in a null checksum the value is set to 886ad4a5223SQuentin Monnet * **CSUM_MANGLED_0** instead. Flag **BPF_F_PSEUDO_HDR** indicates 887ad4a5223SQuentin Monnet * the checksum is to be computed against a pseudo-header. 888ad4a5223SQuentin Monnet * 889ad4a5223SQuentin Monnet * This helper works in combination with **bpf_csum_diff**\ (), 890ad4a5223SQuentin Monnet * which does not update the checksum in-place, but offers more 891ad4a5223SQuentin Monnet * flexibility and can handle sizes larger than 2 or 4 for the 892ad4a5223SQuentin Monnet * checksum to update. 893ad4a5223SQuentin Monnet * 89432e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 895ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 896ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 897ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 898ad4a5223SQuentin Monnet * direct packet access. 899ad4a5223SQuentin Monnet * Return 900ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 901ad4a5223SQuentin Monnet * 902bdb7b79bSAndrii Nakryiko * long bpf_tail_call(void *ctx, struct bpf_map *prog_array_map, u32 index) 903ad4a5223SQuentin Monnet * Description 904ad4a5223SQuentin Monnet * This special helper is used to trigger a "tail call", or in 905ad4a5223SQuentin Monnet * other words, to jump into another eBPF program. The same stack 906ad4a5223SQuentin Monnet * frame is used (but values on stack and in registers for the 907ad4a5223SQuentin Monnet * caller are not accessible to the callee). This mechanism allows 908ad4a5223SQuentin Monnet * for program chaining, either for raising the maximum number of 909ad4a5223SQuentin Monnet * available eBPF instructions, or to execute given programs in 910ad4a5223SQuentin Monnet * conditional blocks. For security reasons, there is an upper 911ad4a5223SQuentin Monnet * limit to the number of successive tail calls that can be 912ad4a5223SQuentin Monnet * performed. 913ad4a5223SQuentin Monnet * 914ad4a5223SQuentin Monnet * Upon call of this helper, the program attempts to jump into a 915ad4a5223SQuentin Monnet * program referenced at index *index* in *prog_array_map*, a 916ad4a5223SQuentin Monnet * special map of type **BPF_MAP_TYPE_PROG_ARRAY**, and passes 917ad4a5223SQuentin Monnet * *ctx*, a pointer to the context. 918ad4a5223SQuentin Monnet * 919ad4a5223SQuentin Monnet * If the call succeeds, the kernel immediately runs the first 920ad4a5223SQuentin Monnet * instruction of the new program. This is not a function call, 921ad4a5223SQuentin Monnet * and it never returns to the previous program. If the call 922ad4a5223SQuentin Monnet * fails, then the helper has no effect, and the caller continues 923ad4a5223SQuentin Monnet * to run its subsequent instructions. A call can fail if the 924ad4a5223SQuentin Monnet * destination program for the jump does not exist (i.e. *index* 925ad4a5223SQuentin Monnet * is superior to the number of entries in *prog_array_map*), or 926ad4a5223SQuentin Monnet * if the maximum number of tail calls has been reached for this 927ad4a5223SQuentin Monnet * chain of programs. This limit is defined in the kernel by the 928ad4a5223SQuentin Monnet * macro **MAX_TAIL_CALL_CNT** (not accessible to user space), 929ad4a5223SQuentin Monnet * which is currently set to 32. 930ad4a5223SQuentin Monnet * Return 931ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 932ad4a5223SQuentin Monnet * 933bdb7b79bSAndrii Nakryiko * long bpf_clone_redirect(struct sk_buff *skb, u32 ifindex, u64 flags) 934ad4a5223SQuentin Monnet * Description 935ad4a5223SQuentin Monnet * Clone and redirect the packet associated to *skb* to another 936ad4a5223SQuentin Monnet * net device of index *ifindex*. Both ingress and egress 937ad4a5223SQuentin Monnet * interfaces can be used for redirection. The **BPF_F_INGRESS** 938ad4a5223SQuentin Monnet * value in *flags* is used to make the distinction (ingress path 939ad4a5223SQuentin Monnet * is selected if the flag is present, egress path otherwise). 940ad4a5223SQuentin Monnet * This is the only flag supported for now. 941ad4a5223SQuentin Monnet * 942ad4a5223SQuentin Monnet * In comparison with **bpf_redirect**\ () helper, 943ad4a5223SQuentin Monnet * **bpf_clone_redirect**\ () has the associated cost of 944ad4a5223SQuentin Monnet * duplicating the packet buffer, but this can be executed out of 945ad4a5223SQuentin Monnet * the eBPF program. Conversely, **bpf_redirect**\ () is more 946ad4a5223SQuentin Monnet * efficient, but it is handled through an action code where the 947ad4a5223SQuentin Monnet * redirection happens only after the eBPF program has returned. 948ad4a5223SQuentin Monnet * 94932e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 950ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 951ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 952ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 953ad4a5223SQuentin Monnet * direct packet access. 954ad4a5223SQuentin Monnet * Return 955ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 956c456dec4SQuentin Monnet * 957c456dec4SQuentin Monnet * u64 bpf_get_current_pid_tgid(void) 958c456dec4SQuentin Monnet * Return 959c456dec4SQuentin Monnet * A 64-bit integer containing the current tgid and pid, and 960c456dec4SQuentin Monnet * created as such: 961c456dec4SQuentin Monnet * *current_task*\ **->tgid << 32 \|** 962c456dec4SQuentin Monnet * *current_task*\ **->pid**. 963c456dec4SQuentin Monnet * 964c456dec4SQuentin Monnet * u64 bpf_get_current_uid_gid(void) 965c456dec4SQuentin Monnet * Return 966c456dec4SQuentin Monnet * A 64-bit integer containing the current GID and UID, and 967c456dec4SQuentin Monnet * created as such: *current_gid* **<< 32 \|** *current_uid*. 968c456dec4SQuentin Monnet * 969bdb7b79bSAndrii Nakryiko * long bpf_get_current_comm(void *buf, u32 size_of_buf) 970c456dec4SQuentin Monnet * Description 971c456dec4SQuentin Monnet * Copy the **comm** attribute of the current task into *buf* of 972c456dec4SQuentin Monnet * *size_of_buf*. The **comm** attribute contains the name of 973c456dec4SQuentin Monnet * the executable (excluding the path) for the current task. The 974c456dec4SQuentin Monnet * *size_of_buf* must be strictly positive. On success, the 975c456dec4SQuentin Monnet * helper makes sure that the *buf* is NUL-terminated. On failure, 976c456dec4SQuentin Monnet * it is filled with zeroes. 977c456dec4SQuentin Monnet * Return 978c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 979c456dec4SQuentin Monnet * 9801fdd08beSQuentin Monnet * u32 bpf_get_cgroup_classid(struct sk_buff *skb) 9811fdd08beSQuentin Monnet * Description 9821fdd08beSQuentin Monnet * Retrieve the classid for the current task, i.e. for the net_cls 9831fdd08beSQuentin Monnet * cgroup to which *skb* belongs. 9841fdd08beSQuentin Monnet * 9851fdd08beSQuentin Monnet * This helper can be used on TC egress path, but not on ingress. 9861fdd08beSQuentin Monnet * 9871fdd08beSQuentin Monnet * The net_cls cgroup provides an interface to tag network packets 9881fdd08beSQuentin Monnet * based on a user-provided identifier for all traffic coming from 9891fdd08beSQuentin Monnet * the tasks belonging to the related cgroup. See also the related 9901fdd08beSQuentin Monnet * kernel documentation, available from the Linux sources in file 991da82c92fSMauro Carvalho Chehab * *Documentation/admin-guide/cgroup-v1/net_cls.rst*. 9921fdd08beSQuentin Monnet * 9931fdd08beSQuentin Monnet * The Linux kernel has two versions for cgroups: there are 9941fdd08beSQuentin Monnet * cgroups v1 and cgroups v2. Both are available to users, who can 9951fdd08beSQuentin Monnet * use a mixture of them, but note that the net_cls cgroup is for 9961fdd08beSQuentin Monnet * cgroup v1 only. This makes it incompatible with BPF programs 9971fdd08beSQuentin Monnet * run on cgroups, which is a cgroup-v2-only feature (a socket can 9981fdd08beSQuentin Monnet * only hold data for one version of cgroups at a time). 9991fdd08beSQuentin Monnet * 10001fdd08beSQuentin Monnet * This helper is only available is the kernel was compiled with 10011fdd08beSQuentin Monnet * the **CONFIG_CGROUP_NET_CLASSID** configuration option set to 10021fdd08beSQuentin Monnet * "**y**" or to "**m**". 10031fdd08beSQuentin Monnet * Return 10041fdd08beSQuentin Monnet * The classid, or 0 for the default unconfigured classid. 10051fdd08beSQuentin Monnet * 1006bdb7b79bSAndrii Nakryiko * long bpf_skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 1007c456dec4SQuentin Monnet * Description 1008c456dec4SQuentin Monnet * Push a *vlan_tci* (VLAN tag control information) of protocol 1009c456dec4SQuentin Monnet * *vlan_proto* to the packet associated to *skb*, then update 1010c456dec4SQuentin Monnet * the checksum. Note that if *vlan_proto* is different from 1011c456dec4SQuentin Monnet * **ETH_P_8021Q** and **ETH_P_8021AD**, it is considered to 1012c456dec4SQuentin Monnet * be **ETH_P_8021Q**. 1013c456dec4SQuentin Monnet * 101432e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1015c456dec4SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1016c456dec4SQuentin Monnet * previously done by the verifier are invalidated and must be 1017c456dec4SQuentin Monnet * performed again, if the helper is used in combination with 1018c456dec4SQuentin Monnet * direct packet access. 1019c456dec4SQuentin Monnet * Return 1020c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 1021c456dec4SQuentin Monnet * 1022bdb7b79bSAndrii Nakryiko * long bpf_skb_vlan_pop(struct sk_buff *skb) 1023c456dec4SQuentin Monnet * Description 1024c456dec4SQuentin Monnet * Pop a VLAN header from the packet associated to *skb*. 1025c456dec4SQuentin Monnet * 102632e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1027c456dec4SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1028c456dec4SQuentin Monnet * previously done by the verifier are invalidated and must be 1029c456dec4SQuentin Monnet * performed again, if the helper is used in combination with 1030c456dec4SQuentin Monnet * direct packet access. 1031c456dec4SQuentin Monnet * Return 1032c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 1033c456dec4SQuentin Monnet * 1034bdb7b79bSAndrii Nakryiko * long bpf_skb_get_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags) 1035c456dec4SQuentin Monnet * Description 1036c456dec4SQuentin Monnet * Get tunnel metadata. This helper takes a pointer *key* to an 1037c456dec4SQuentin Monnet * empty **struct bpf_tunnel_key** of **size**, that will be 1038c456dec4SQuentin Monnet * filled with tunnel metadata for the packet associated to *skb*. 1039c456dec4SQuentin Monnet * The *flags* can be set to **BPF_F_TUNINFO_IPV6**, which 1040c456dec4SQuentin Monnet * indicates that the tunnel is based on IPv6 protocol instead of 1041c456dec4SQuentin Monnet * IPv4. 1042c456dec4SQuentin Monnet * 1043c456dec4SQuentin Monnet * The **struct bpf_tunnel_key** is an object that generalizes the 1044c456dec4SQuentin Monnet * principal parameters used by various tunneling protocols into a 1045c456dec4SQuentin Monnet * single struct. This way, it can be used to easily make a 1046c456dec4SQuentin Monnet * decision based on the contents of the encapsulation header, 1047c456dec4SQuentin Monnet * "summarized" in this struct. In particular, it holds the IP 1048c456dec4SQuentin Monnet * address of the remote end (IPv4 or IPv6, depending on the case) 1049c456dec4SQuentin Monnet * in *key*\ **->remote_ipv4** or *key*\ **->remote_ipv6**. Also, 1050c456dec4SQuentin Monnet * this struct exposes the *key*\ **->tunnel_id**, which is 1051c456dec4SQuentin Monnet * generally mapped to a VNI (Virtual Network Identifier), making 1052c456dec4SQuentin Monnet * it programmable together with the **bpf_skb_set_tunnel_key**\ 1053c456dec4SQuentin Monnet * () helper. 1054c456dec4SQuentin Monnet * 1055c456dec4SQuentin Monnet * Let's imagine that the following code is part of a program 1056c456dec4SQuentin Monnet * attached to the TC ingress interface, on one end of a GRE 1057c456dec4SQuentin Monnet * tunnel, and is supposed to filter out all messages coming from 1058c456dec4SQuentin Monnet * remote ends with IPv4 address other than 10.0.0.1: 1059c456dec4SQuentin Monnet * 1060c456dec4SQuentin Monnet * :: 1061c456dec4SQuentin Monnet * 1062c456dec4SQuentin Monnet * int ret; 1063c456dec4SQuentin Monnet * struct bpf_tunnel_key key = {}; 1064c456dec4SQuentin Monnet * 1065c456dec4SQuentin Monnet * ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0); 1066c456dec4SQuentin Monnet * if (ret < 0) 1067c456dec4SQuentin Monnet * return TC_ACT_SHOT; // drop packet 1068c456dec4SQuentin Monnet * 1069c456dec4SQuentin Monnet * if (key.remote_ipv4 != 0x0a000001) 1070c456dec4SQuentin Monnet * return TC_ACT_SHOT; // drop packet 1071c456dec4SQuentin Monnet * 1072c456dec4SQuentin Monnet * return TC_ACT_OK; // accept packet 1073c456dec4SQuentin Monnet * 1074c456dec4SQuentin Monnet * This interface can also be used with all encapsulation devices 1075c456dec4SQuentin Monnet * that can operate in "collect metadata" mode: instead of having 1076c456dec4SQuentin Monnet * one network device per specific configuration, the "collect 1077c456dec4SQuentin Monnet * metadata" mode only requires a single device where the 1078c456dec4SQuentin Monnet * configuration can be extracted from this helper. 1079c456dec4SQuentin Monnet * 1080c456dec4SQuentin Monnet * This can be used together with various tunnels such as VXLan, 1081c456dec4SQuentin Monnet * Geneve, GRE or IP in IP (IPIP). 1082c456dec4SQuentin Monnet * Return 1083c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 1084c456dec4SQuentin Monnet * 1085bdb7b79bSAndrii Nakryiko * long bpf_skb_set_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags) 1086c456dec4SQuentin Monnet * Description 1087c456dec4SQuentin Monnet * Populate tunnel metadata for packet associated to *skb.* The 1088c456dec4SQuentin Monnet * tunnel metadata is set to the contents of *key*, of *size*. The 1089c456dec4SQuentin Monnet * *flags* can be set to a combination of the following values: 1090c456dec4SQuentin Monnet * 1091c456dec4SQuentin Monnet * **BPF_F_TUNINFO_IPV6** 1092c456dec4SQuentin Monnet * Indicate that the tunnel is based on IPv6 protocol 1093c456dec4SQuentin Monnet * instead of IPv4. 1094c456dec4SQuentin Monnet * **BPF_F_ZERO_CSUM_TX** 1095c456dec4SQuentin Monnet * For IPv4 packets, add a flag to tunnel metadata 1096c456dec4SQuentin Monnet * indicating that checksum computation should be skipped 1097c456dec4SQuentin Monnet * and checksum set to zeroes. 1098c456dec4SQuentin Monnet * **BPF_F_DONT_FRAGMENT** 1099c456dec4SQuentin Monnet * Add a flag to tunnel metadata indicating that the 1100c456dec4SQuentin Monnet * packet should not be fragmented. 1101c456dec4SQuentin Monnet * **BPF_F_SEQ_NUMBER** 1102c456dec4SQuentin Monnet * Add a flag to tunnel metadata indicating that a 1103c456dec4SQuentin Monnet * sequence number should be added to tunnel header before 1104c456dec4SQuentin Monnet * sending the packet. This flag was added for GRE 1105c456dec4SQuentin Monnet * encapsulation, but might be used with other protocols 1106c456dec4SQuentin Monnet * as well in the future. 1107c456dec4SQuentin Monnet * 1108c456dec4SQuentin Monnet * Here is a typical usage on the transmit path: 1109c456dec4SQuentin Monnet * 1110c456dec4SQuentin Monnet * :: 1111c456dec4SQuentin Monnet * 1112c456dec4SQuentin Monnet * struct bpf_tunnel_key key; 1113c456dec4SQuentin Monnet * populate key ... 1114c456dec4SQuentin Monnet * bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0); 1115c456dec4SQuentin Monnet * bpf_clone_redirect(skb, vxlan_dev_ifindex, 0); 1116c456dec4SQuentin Monnet * 1117c456dec4SQuentin Monnet * See also the description of the **bpf_skb_get_tunnel_key**\ () 1118c456dec4SQuentin Monnet * helper for additional information. 1119c456dec4SQuentin Monnet * Return 1120c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 1121c456dec4SQuentin Monnet * 1122c6b5fb86SQuentin Monnet * u64 bpf_perf_event_read(struct bpf_map *map, u64 flags) 1123c6b5fb86SQuentin Monnet * Description 1124c6b5fb86SQuentin Monnet * Read the value of a perf event counter. This helper relies on a 1125c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of 1126c6b5fb86SQuentin Monnet * the perf event counter is selected when *map* is updated with 1127c6b5fb86SQuentin Monnet * perf event file descriptors. The *map* is an array whose size 1128c6b5fb86SQuentin Monnet * is the number of available CPUs, and each cell contains a value 1129c6b5fb86SQuentin Monnet * relative to one CPU. The value to retrieve is indicated by 1130c6b5fb86SQuentin Monnet * *flags*, that contains the index of the CPU to look up, masked 1131c6b5fb86SQuentin Monnet * with **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to 1132c6b5fb86SQuentin Monnet * **BPF_F_CURRENT_CPU** to indicate that the value for the 1133c6b5fb86SQuentin Monnet * current CPU should be retrieved. 1134c6b5fb86SQuentin Monnet * 1135c6b5fb86SQuentin Monnet * Note that before Linux 4.13, only hardware perf event can be 1136c6b5fb86SQuentin Monnet * retrieved. 1137c6b5fb86SQuentin Monnet * 1138c6b5fb86SQuentin Monnet * Also, be aware that the newer helper 1139c6b5fb86SQuentin Monnet * **bpf_perf_event_read_value**\ () is recommended over 11403bd5a09bSQuentin Monnet * **bpf_perf_event_read**\ () in general. The latter has some ABI 1141c6b5fb86SQuentin Monnet * quirks where error and counter value are used as a return code 1142c6b5fb86SQuentin Monnet * (which is wrong to do since ranges may overlap). This issue is 11433bd5a09bSQuentin Monnet * fixed with **bpf_perf_event_read_value**\ (), which at the same 11443bd5a09bSQuentin Monnet * time provides more features over the **bpf_perf_event_read**\ 11453bd5a09bSQuentin Monnet * () interface. Please refer to the description of 1146c6b5fb86SQuentin Monnet * **bpf_perf_event_read_value**\ () for details. 1147c6b5fb86SQuentin Monnet * Return 1148c6b5fb86SQuentin Monnet * The value of the perf event counter read from the map, or a 1149c6b5fb86SQuentin Monnet * negative error code in case of failure. 1150c6b5fb86SQuentin Monnet * 1151bdb7b79bSAndrii Nakryiko * long bpf_redirect(u32 ifindex, u64 flags) 1152c456dec4SQuentin Monnet * Description 1153c456dec4SQuentin Monnet * Redirect the packet to another net device of index *ifindex*. 1154c456dec4SQuentin Monnet * This helper is somewhat similar to **bpf_clone_redirect**\ 1155c456dec4SQuentin Monnet * (), except that the packet is not cloned, which provides 1156c456dec4SQuentin Monnet * increased performance. 1157c456dec4SQuentin Monnet * 1158c456dec4SQuentin Monnet * Except for XDP, both ingress and egress interfaces can be used 1159c456dec4SQuentin Monnet * for redirection. The **BPF_F_INGRESS** value in *flags* is used 1160c456dec4SQuentin Monnet * to make the distinction (ingress path is selected if the flag 1161c456dec4SQuentin Monnet * is present, egress path otherwise). Currently, XDP only 1162c456dec4SQuentin Monnet * supports redirection to the egress interface, and accepts no 1163c456dec4SQuentin Monnet * flag at all. 1164c456dec4SQuentin Monnet * 1165f25975f4SToke Høiland-Jørgensen * The same effect can also be attained with the more generic 1166f25975f4SToke Høiland-Jørgensen * **bpf_redirect_map**\ (), which uses a BPF map to store the 1167f25975f4SToke Høiland-Jørgensen * redirect target instead of providing it directly to the helper. 1168c456dec4SQuentin Monnet * Return 1169c456dec4SQuentin Monnet * For XDP, the helper returns **XDP_REDIRECT** on success or 1170c456dec4SQuentin Monnet * **XDP_ABORTED** on error. For other program types, the values 1171c456dec4SQuentin Monnet * are **TC_ACT_REDIRECT** on success or **TC_ACT_SHOT** on 1172c456dec4SQuentin Monnet * error. 1173c456dec4SQuentin Monnet * 11741fdd08beSQuentin Monnet * u32 bpf_get_route_realm(struct sk_buff *skb) 11751fdd08beSQuentin Monnet * Description 11761fdd08beSQuentin Monnet * Retrieve the realm or the route, that is to say the 11771fdd08beSQuentin Monnet * **tclassid** field of the destination for the *skb*. The 1178b16fc097STobias Klauser * identifier retrieved is a user-provided tag, similar to the 11791fdd08beSQuentin Monnet * one used with the net_cls cgroup (see description for 11801fdd08beSQuentin Monnet * **bpf_get_cgroup_classid**\ () helper), but here this tag is 11811fdd08beSQuentin Monnet * held by a route (a destination entry), not by a task. 11821fdd08beSQuentin Monnet * 11831fdd08beSQuentin Monnet * Retrieving this identifier works with the clsact TC egress hook 11841fdd08beSQuentin Monnet * (see also **tc-bpf(8)**), or alternatively on conventional 11851fdd08beSQuentin Monnet * classful egress qdiscs, but not on TC ingress path. In case of 11861fdd08beSQuentin Monnet * clsact TC egress hook, this has the advantage that, internally, 11871fdd08beSQuentin Monnet * the destination entry has not been dropped yet in the transmit 11881fdd08beSQuentin Monnet * path. Therefore, the destination entry does not need to be 11891fdd08beSQuentin Monnet * artificially held via **netif_keep_dst**\ () for a classful 11901fdd08beSQuentin Monnet * qdisc until the *skb* is freed. 11911fdd08beSQuentin Monnet * 11921fdd08beSQuentin Monnet * This helper is available only if the kernel was compiled with 11931fdd08beSQuentin Monnet * **CONFIG_IP_ROUTE_CLASSID** configuration option. 11941fdd08beSQuentin Monnet * Return 11951fdd08beSQuentin Monnet * The realm of the route for the packet associated to *skb*, or 0 11961fdd08beSQuentin Monnet * if none was found. 11971fdd08beSQuentin Monnet * 1198bdb7b79bSAndrii Nakryiko * long bpf_perf_event_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size) 1199c456dec4SQuentin Monnet * Description 1200c456dec4SQuentin Monnet * Write raw *data* blob into a special BPF perf event held by 1201c456dec4SQuentin Monnet * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 1202c456dec4SQuentin Monnet * event must have the following attributes: **PERF_SAMPLE_RAW** 1203c456dec4SQuentin Monnet * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 1204c456dec4SQuentin Monnet * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 1205c456dec4SQuentin Monnet * 1206c456dec4SQuentin Monnet * The *flags* are used to indicate the index in *map* for which 1207c456dec4SQuentin Monnet * the value must be put, masked with **BPF_F_INDEX_MASK**. 1208c456dec4SQuentin Monnet * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 1209c456dec4SQuentin Monnet * to indicate that the index of the current CPU core should be 1210c456dec4SQuentin Monnet * used. 1211c456dec4SQuentin Monnet * 1212c456dec4SQuentin Monnet * The value to write, of *size*, is passed through eBPF stack and 1213c456dec4SQuentin Monnet * pointed by *data*. 1214c456dec4SQuentin Monnet * 1215c456dec4SQuentin Monnet * The context of the program *ctx* needs also be passed to the 1216c456dec4SQuentin Monnet * helper. 1217c456dec4SQuentin Monnet * 1218c456dec4SQuentin Monnet * On user space, a program willing to read the values needs to 1219c456dec4SQuentin Monnet * call **perf_event_open**\ () on the perf event (either for 1220c456dec4SQuentin Monnet * one or for all CPUs) and to store the file descriptor into the 1221c456dec4SQuentin Monnet * *map*. This must be done before the eBPF program can send data 1222c456dec4SQuentin Monnet * into it. An example is available in file 1223c456dec4SQuentin Monnet * *samples/bpf/trace_output_user.c* in the Linux kernel source 1224c456dec4SQuentin Monnet * tree (the eBPF program counterpart is in 1225c456dec4SQuentin Monnet * *samples/bpf/trace_output_kern.c*). 1226c456dec4SQuentin Monnet * 1227c456dec4SQuentin Monnet * **bpf_perf_event_output**\ () achieves better performance 1228c456dec4SQuentin Monnet * than **bpf_trace_printk**\ () for sharing data with user 1229c456dec4SQuentin Monnet * space, and is much better suitable for streaming data from eBPF 1230c456dec4SQuentin Monnet * programs. 1231c456dec4SQuentin Monnet * 1232c456dec4SQuentin Monnet * Note that this helper is not restricted to tracing use cases 1233c456dec4SQuentin Monnet * and can be used with programs attached to TC or XDP as well, 1234c456dec4SQuentin Monnet * where it allows for passing data to user space listeners. Data 1235c456dec4SQuentin Monnet * can be: 1236c456dec4SQuentin Monnet * 1237c456dec4SQuentin Monnet * * Only custom structs, 1238c456dec4SQuentin Monnet * * Only the packet payload, or 1239c456dec4SQuentin Monnet * * A combination of both. 1240c456dec4SQuentin Monnet * Return 1241c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 1242c456dec4SQuentin Monnet * 1243bdb7b79bSAndrii Nakryiko * long bpf_skb_load_bytes(const void *skb, u32 offset, void *to, u32 len) 12441fdd08beSQuentin Monnet * Description 12451fdd08beSQuentin Monnet * This helper was provided as an easy way to load data from a 12461fdd08beSQuentin Monnet * packet. It can be used to load *len* bytes from *offset* from 12471fdd08beSQuentin Monnet * the packet associated to *skb*, into the buffer pointed by 12481fdd08beSQuentin Monnet * *to*. 12491fdd08beSQuentin Monnet * 12501fdd08beSQuentin Monnet * Since Linux 4.7, usage of this helper has mostly been replaced 12511fdd08beSQuentin Monnet * by "direct packet access", enabling packet data to be 12521fdd08beSQuentin Monnet * manipulated with *skb*\ **->data** and *skb*\ **->data_end** 12531fdd08beSQuentin Monnet * pointing respectively to the first byte of packet data and to 12541fdd08beSQuentin Monnet * the byte after the last byte of packet data. However, it 12551fdd08beSQuentin Monnet * remains useful if one wishes to read large quantities of data 12561fdd08beSQuentin Monnet * at once from a packet into the eBPF stack. 12571fdd08beSQuentin Monnet * Return 12581fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 12591fdd08beSQuentin Monnet * 1260bdb7b79bSAndrii Nakryiko * long bpf_get_stackid(void *ctx, struct bpf_map *map, u64 flags) 1261c456dec4SQuentin Monnet * Description 1262c456dec4SQuentin Monnet * Walk a user or a kernel stack and return its id. To achieve 1263c456dec4SQuentin Monnet * this, the helper needs *ctx*, which is a pointer to the context 1264c456dec4SQuentin Monnet * on which the tracing program is executed, and a pointer to a 1265c456dec4SQuentin Monnet * *map* of type **BPF_MAP_TYPE_STACK_TRACE**. 1266c456dec4SQuentin Monnet * 1267c456dec4SQuentin Monnet * The last argument, *flags*, holds the number of stack frames to 1268c456dec4SQuentin Monnet * skip (from 0 to 255), masked with 1269c456dec4SQuentin Monnet * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 1270c456dec4SQuentin Monnet * a combination of the following flags: 1271c456dec4SQuentin Monnet * 1272c456dec4SQuentin Monnet * **BPF_F_USER_STACK** 1273c456dec4SQuentin Monnet * Collect a user space stack instead of a kernel stack. 1274c456dec4SQuentin Monnet * **BPF_F_FAST_STACK_CMP** 1275c456dec4SQuentin Monnet * Compare stacks by hash only. 1276c456dec4SQuentin Monnet * **BPF_F_REUSE_STACKID** 1277c456dec4SQuentin Monnet * If two different stacks hash into the same *stackid*, 1278c456dec4SQuentin Monnet * discard the old one. 1279c456dec4SQuentin Monnet * 1280c456dec4SQuentin Monnet * The stack id retrieved is a 32 bit long integer handle which 1281c456dec4SQuentin Monnet * can be further combined with other data (including other stack 1282c456dec4SQuentin Monnet * ids) and used as a key into maps. This can be useful for 1283c456dec4SQuentin Monnet * generating a variety of graphs (such as flame graphs or off-cpu 1284c456dec4SQuentin Monnet * graphs). 1285c456dec4SQuentin Monnet * 1286c456dec4SQuentin Monnet * For walking a stack, this helper is an improvement over 1287c456dec4SQuentin Monnet * **bpf_probe_read**\ (), which can be used with unrolled loops 1288c456dec4SQuentin Monnet * but is not efficient and consumes a lot of eBPF instructions. 1289c456dec4SQuentin Monnet * Instead, **bpf_get_stackid**\ () can collect up to 1290c456dec4SQuentin Monnet * **PERF_MAX_STACK_DEPTH** both kernel and user frames. Note that 1291c456dec4SQuentin Monnet * this limit can be controlled with the **sysctl** program, and 1292c456dec4SQuentin Monnet * that it should be manually increased in order to profile long 1293c456dec4SQuentin Monnet * user stacks (such as stacks for Java programs). To do so, use: 1294c456dec4SQuentin Monnet * 1295c456dec4SQuentin Monnet * :: 1296c456dec4SQuentin Monnet * 1297c456dec4SQuentin Monnet * # sysctl kernel.perf_event_max_stack=<new value> 1298c456dec4SQuentin Monnet * Return 1299c456dec4SQuentin Monnet * The positive or null stack id on success, or a negative error 1300c456dec4SQuentin Monnet * in case of failure. 1301c456dec4SQuentin Monnet * 13021fdd08beSQuentin Monnet * s64 bpf_csum_diff(__be32 *from, u32 from_size, __be32 *to, u32 to_size, __wsum seed) 13031fdd08beSQuentin Monnet * Description 13041fdd08beSQuentin Monnet * Compute a checksum difference, from the raw buffer pointed by 13051fdd08beSQuentin Monnet * *from*, of length *from_size* (that must be a multiple of 4), 13061fdd08beSQuentin Monnet * towards the raw buffer pointed by *to*, of size *to_size* 13071fdd08beSQuentin Monnet * (same remark). An optional *seed* can be added to the value 13081fdd08beSQuentin Monnet * (this can be cascaded, the seed may come from a previous call 13091fdd08beSQuentin Monnet * to the helper). 13101fdd08beSQuentin Monnet * 13111fdd08beSQuentin Monnet * This is flexible enough to be used in several ways: 13121fdd08beSQuentin Monnet * 13131fdd08beSQuentin Monnet * * With *from_size* == 0, *to_size* > 0 and *seed* set to 13141fdd08beSQuentin Monnet * checksum, it can be used when pushing new data. 13151fdd08beSQuentin Monnet * * With *from_size* > 0, *to_size* == 0 and *seed* set to 13161fdd08beSQuentin Monnet * checksum, it can be used when removing data from a packet. 13171fdd08beSQuentin Monnet * * With *from_size* > 0, *to_size* > 0 and *seed* set to 0, it 13181fdd08beSQuentin Monnet * can be used to compute a diff. Note that *from_size* and 13191fdd08beSQuentin Monnet * *to_size* do not need to be equal. 13201fdd08beSQuentin Monnet * 13211fdd08beSQuentin Monnet * This helper can be used in combination with 13221fdd08beSQuentin Monnet * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ (), to 13231fdd08beSQuentin Monnet * which one can feed in the difference computed with 13241fdd08beSQuentin Monnet * **bpf_csum_diff**\ (). 13251fdd08beSQuentin Monnet * Return 13261fdd08beSQuentin Monnet * The checksum result, or a negative error code in case of 13271fdd08beSQuentin Monnet * failure. 13281fdd08beSQuentin Monnet * 1329bdb7b79bSAndrii Nakryiko * long bpf_skb_get_tunnel_opt(struct sk_buff *skb, void *opt, u32 size) 13301fdd08beSQuentin Monnet * Description 13311fdd08beSQuentin Monnet * Retrieve tunnel options metadata for the packet associated to 13321fdd08beSQuentin Monnet * *skb*, and store the raw tunnel option data to the buffer *opt* 13331fdd08beSQuentin Monnet * of *size*. 13341fdd08beSQuentin Monnet * 13351fdd08beSQuentin Monnet * This helper can be used with encapsulation devices that can 13361fdd08beSQuentin Monnet * operate in "collect metadata" mode (please refer to the related 13371fdd08beSQuentin Monnet * note in the description of **bpf_skb_get_tunnel_key**\ () for 13381fdd08beSQuentin Monnet * more details). A particular example where this can be used is 13391fdd08beSQuentin Monnet * in combination with the Geneve encapsulation protocol, where it 13401fdd08beSQuentin Monnet * allows for pushing (with **bpf_skb_get_tunnel_opt**\ () helper) 13411fdd08beSQuentin Monnet * and retrieving arbitrary TLVs (Type-Length-Value headers) from 13421fdd08beSQuentin Monnet * the eBPF program. This allows for full customization of these 13431fdd08beSQuentin Monnet * headers. 13441fdd08beSQuentin Monnet * Return 13451fdd08beSQuentin Monnet * The size of the option data retrieved. 13461fdd08beSQuentin Monnet * 1347bdb7b79bSAndrii Nakryiko * long bpf_skb_set_tunnel_opt(struct sk_buff *skb, void *opt, u32 size) 13481fdd08beSQuentin Monnet * Description 13491fdd08beSQuentin Monnet * Set tunnel options metadata for the packet associated to *skb* 13501fdd08beSQuentin Monnet * to the option data contained in the raw buffer *opt* of *size*. 13511fdd08beSQuentin Monnet * 13521fdd08beSQuentin Monnet * See also the description of the **bpf_skb_get_tunnel_opt**\ () 13531fdd08beSQuentin Monnet * helper for additional information. 13541fdd08beSQuentin Monnet * Return 13551fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 13561fdd08beSQuentin Monnet * 1357bdb7b79bSAndrii Nakryiko * long bpf_skb_change_proto(struct sk_buff *skb, __be16 proto, u64 flags) 13581fdd08beSQuentin Monnet * Description 13591fdd08beSQuentin Monnet * Change the protocol of the *skb* to *proto*. Currently 13601fdd08beSQuentin Monnet * supported are transition from IPv4 to IPv6, and from IPv6 to 13611fdd08beSQuentin Monnet * IPv4. The helper takes care of the groundwork for the 13621fdd08beSQuentin Monnet * transition, including resizing the socket buffer. The eBPF 13631fdd08beSQuentin Monnet * program is expected to fill the new headers, if any, via 13641fdd08beSQuentin Monnet * **skb_store_bytes**\ () and to recompute the checksums with 13651fdd08beSQuentin Monnet * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ 13661fdd08beSQuentin Monnet * (). The main case for this helper is to perform NAT64 13671fdd08beSQuentin Monnet * operations out of an eBPF program. 13681fdd08beSQuentin Monnet * 13691fdd08beSQuentin Monnet * Internally, the GSO type is marked as dodgy so that headers are 13701fdd08beSQuentin Monnet * checked and segments are recalculated by the GSO/GRO engine. 13711fdd08beSQuentin Monnet * The size for GSO target is adapted as well. 13721fdd08beSQuentin Monnet * 13731fdd08beSQuentin Monnet * All values for *flags* are reserved for future usage, and must 13741fdd08beSQuentin Monnet * be left at zero. 13751fdd08beSQuentin Monnet * 137632e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 13771fdd08beSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 13781fdd08beSQuentin Monnet * previously done by the verifier are invalidated and must be 13791fdd08beSQuentin Monnet * performed again, if the helper is used in combination with 13801fdd08beSQuentin Monnet * direct packet access. 13811fdd08beSQuentin Monnet * Return 13821fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 13831fdd08beSQuentin Monnet * 1384bdb7b79bSAndrii Nakryiko * long bpf_skb_change_type(struct sk_buff *skb, u32 type) 13851fdd08beSQuentin Monnet * Description 13861fdd08beSQuentin Monnet * Change the packet type for the packet associated to *skb*. This 13871fdd08beSQuentin Monnet * comes down to setting *skb*\ **->pkt_type** to *type*, except 13881fdd08beSQuentin Monnet * the eBPF program does not have a write access to *skb*\ 13891fdd08beSQuentin Monnet * **->pkt_type** beside this helper. Using a helper here allows 13901fdd08beSQuentin Monnet * for graceful handling of errors. 13911fdd08beSQuentin Monnet * 13921fdd08beSQuentin Monnet * The major use case is to change incoming *skb*s to 13931fdd08beSQuentin Monnet * **PACKET_HOST** in a programmatic way instead of having to 13941fdd08beSQuentin Monnet * recirculate via **redirect**\ (..., **BPF_F_INGRESS**), for 13951fdd08beSQuentin Monnet * example. 13961fdd08beSQuentin Monnet * 13971fdd08beSQuentin Monnet * Note that *type* only allows certain values. At this time, they 13981fdd08beSQuentin Monnet * are: 13991fdd08beSQuentin Monnet * 14001fdd08beSQuentin Monnet * **PACKET_HOST** 14011fdd08beSQuentin Monnet * Packet is for us. 14021fdd08beSQuentin Monnet * **PACKET_BROADCAST** 14031fdd08beSQuentin Monnet * Send packet to all. 14041fdd08beSQuentin Monnet * **PACKET_MULTICAST** 14051fdd08beSQuentin Monnet * Send packet to group. 14061fdd08beSQuentin Monnet * **PACKET_OTHERHOST** 14071fdd08beSQuentin Monnet * Send packet to someone else. 14081fdd08beSQuentin Monnet * Return 14091fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 14101fdd08beSQuentin Monnet * 1411bdb7b79bSAndrii Nakryiko * long bpf_skb_under_cgroup(struct sk_buff *skb, struct bpf_map *map, u32 index) 1412c6b5fb86SQuentin Monnet * Description 1413c6b5fb86SQuentin Monnet * Check whether *skb* is a descendant of the cgroup2 held by 1414c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. 1415c6b5fb86SQuentin Monnet * Return 1416c6b5fb86SQuentin Monnet * The return value depends on the result of the test, and can be: 1417c6b5fb86SQuentin Monnet * 1418c6b5fb86SQuentin Monnet * * 0, if the *skb* failed the cgroup2 descendant test. 1419c6b5fb86SQuentin Monnet * * 1, if the *skb* succeeded the cgroup2 descendant test. 1420c6b5fb86SQuentin Monnet * * A negative error code, if an error occurred. 1421c6b5fb86SQuentin Monnet * 1422fa15601aSQuentin Monnet * u32 bpf_get_hash_recalc(struct sk_buff *skb) 1423fa15601aSQuentin Monnet * Description 1424fa15601aSQuentin Monnet * Retrieve the hash of the packet, *skb*\ **->hash**. If it is 1425fa15601aSQuentin Monnet * not set, in particular if the hash was cleared due to mangling, 1426fa15601aSQuentin Monnet * recompute this hash. Later accesses to the hash can be done 1427fa15601aSQuentin Monnet * directly with *skb*\ **->hash**. 1428fa15601aSQuentin Monnet * 1429fa15601aSQuentin Monnet * Calling **bpf_set_hash_invalid**\ (), changing a packet 1430fa15601aSQuentin Monnet * prototype with **bpf_skb_change_proto**\ (), or calling 1431fa15601aSQuentin Monnet * **bpf_skb_store_bytes**\ () with the 1432fa15601aSQuentin Monnet * **BPF_F_INVALIDATE_HASH** are actions susceptible to clear 1433fa15601aSQuentin Monnet * the hash and to trigger a new computation for the next call to 1434fa15601aSQuentin Monnet * **bpf_get_hash_recalc**\ (). 1435fa15601aSQuentin Monnet * Return 1436fa15601aSQuentin Monnet * The 32-bit hash. 1437fa15601aSQuentin Monnet * 1438c456dec4SQuentin Monnet * u64 bpf_get_current_task(void) 1439c456dec4SQuentin Monnet * Return 1440c456dec4SQuentin Monnet * A pointer to the current task struct. 1441fa15601aSQuentin Monnet * 1442bdb7b79bSAndrii Nakryiko * long bpf_probe_write_user(void *dst, const void *src, u32 len) 1443c6b5fb86SQuentin Monnet * Description 1444c6b5fb86SQuentin Monnet * Attempt in a safe way to write *len* bytes from the buffer 1445c6b5fb86SQuentin Monnet * *src* to *dst* in memory. It only works for threads that are in 1446c6b5fb86SQuentin Monnet * user context, and *dst* must be a valid user space address. 1447c6b5fb86SQuentin Monnet * 1448c6b5fb86SQuentin Monnet * This helper should not be used to implement any kind of 1449c6b5fb86SQuentin Monnet * security mechanism because of TOC-TOU attacks, but rather to 1450c6b5fb86SQuentin Monnet * debug, divert, and manipulate execution of semi-cooperative 1451c6b5fb86SQuentin Monnet * processes. 1452c6b5fb86SQuentin Monnet * 1453c6b5fb86SQuentin Monnet * Keep in mind that this feature is meant for experiments, and it 1454c6b5fb86SQuentin Monnet * has a risk of crashing the system and running programs. 1455c6b5fb86SQuentin Monnet * Therefore, when an eBPF program using this helper is attached, 1456c6b5fb86SQuentin Monnet * a warning including PID and process name is printed to kernel 1457c6b5fb86SQuentin Monnet * logs. 1458c6b5fb86SQuentin Monnet * Return 1459c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 1460c6b5fb86SQuentin Monnet * 1461bdb7b79bSAndrii Nakryiko * long bpf_current_task_under_cgroup(struct bpf_map *map, u32 index) 1462c6b5fb86SQuentin Monnet * Description 1463c6b5fb86SQuentin Monnet * Check whether the probe is being run is the context of a given 1464c6b5fb86SQuentin Monnet * subset of the cgroup2 hierarchy. The cgroup2 to test is held by 1465c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. 1466c6b5fb86SQuentin Monnet * Return 1467c6b5fb86SQuentin Monnet * The return value depends on the result of the test, and can be: 1468c6b5fb86SQuentin Monnet * 14691aef5b43SSong Liu * * 0, if current task belongs to the cgroup2. 14701aef5b43SSong Liu * * 1, if current task does not belong to the cgroup2. 1471c6b5fb86SQuentin Monnet * * A negative error code, if an error occurred. 1472c6b5fb86SQuentin Monnet * 1473bdb7b79bSAndrii Nakryiko * long bpf_skb_change_tail(struct sk_buff *skb, u32 len, u64 flags) 1474fa15601aSQuentin Monnet * Description 1475fa15601aSQuentin Monnet * Resize (trim or grow) the packet associated to *skb* to the 1476fa15601aSQuentin Monnet * new *len*. The *flags* are reserved for future usage, and must 1477fa15601aSQuentin Monnet * be left at zero. 1478fa15601aSQuentin Monnet * 1479fa15601aSQuentin Monnet * The basic idea is that the helper performs the needed work to 1480fa15601aSQuentin Monnet * change the size of the packet, then the eBPF program rewrites 1481fa15601aSQuentin Monnet * the rest via helpers like **bpf_skb_store_bytes**\ (), 1482fa15601aSQuentin Monnet * **bpf_l3_csum_replace**\ (), **bpf_l3_csum_replace**\ () 1483fa15601aSQuentin Monnet * and others. This helper is a slow path utility intended for 1484fa15601aSQuentin Monnet * replies with control messages. And because it is targeted for 1485fa15601aSQuentin Monnet * slow path, the helper itself can afford to be slow: it 1486fa15601aSQuentin Monnet * implicitly linearizes, unclones and drops offloads from the 1487fa15601aSQuentin Monnet * *skb*. 1488fa15601aSQuentin Monnet * 148932e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1490fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1491fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1492fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1493fa15601aSQuentin Monnet * direct packet access. 1494fa15601aSQuentin Monnet * Return 1495fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 1496fa15601aSQuentin Monnet * 1497bdb7b79bSAndrii Nakryiko * long bpf_skb_pull_data(struct sk_buff *skb, u32 len) 1498fa15601aSQuentin Monnet * Description 1499fa15601aSQuentin Monnet * Pull in non-linear data in case the *skb* is non-linear and not 1500fa15601aSQuentin Monnet * all of *len* are part of the linear section. Make *len* bytes 1501fa15601aSQuentin Monnet * from *skb* readable and writable. If a zero value is passed for 1502fa15601aSQuentin Monnet * *len*, then the whole length of the *skb* is pulled. 1503fa15601aSQuentin Monnet * 1504fa15601aSQuentin Monnet * This helper is only needed for reading and writing with direct 1505fa15601aSQuentin Monnet * packet access. 1506fa15601aSQuentin Monnet * 1507fa15601aSQuentin Monnet * For direct packet access, testing that offsets to access 1508fa15601aSQuentin Monnet * are within packet boundaries (test on *skb*\ **->data_end**) is 1509fa15601aSQuentin Monnet * susceptible to fail if offsets are invalid, or if the requested 1510fa15601aSQuentin Monnet * data is in non-linear parts of the *skb*. On failure the 1511fa15601aSQuentin Monnet * program can just bail out, or in the case of a non-linear 1512fa15601aSQuentin Monnet * buffer, use a helper to make the data available. The 1513fa15601aSQuentin Monnet * **bpf_skb_load_bytes**\ () helper is a first solution to access 1514fa15601aSQuentin Monnet * the data. Another one consists in using **bpf_skb_pull_data** 1515fa15601aSQuentin Monnet * to pull in once the non-linear parts, then retesting and 1516fa15601aSQuentin Monnet * eventually access the data. 1517fa15601aSQuentin Monnet * 1518fa15601aSQuentin Monnet * At the same time, this also makes sure the *skb* is uncloned, 1519fa15601aSQuentin Monnet * which is a necessary condition for direct write. As this needs 1520fa15601aSQuentin Monnet * to be an invariant for the write part only, the verifier 1521fa15601aSQuentin Monnet * detects writes and adds a prologue that is calling 1522fa15601aSQuentin Monnet * **bpf_skb_pull_data()** to effectively unclone the *skb* from 1523fa15601aSQuentin Monnet * the very beginning in case it is indeed cloned. 1524fa15601aSQuentin Monnet * 152532e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1526fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1527fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1528fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1529fa15601aSQuentin Monnet * direct packet access. 1530fa15601aSQuentin Monnet * Return 1531fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 1532fa15601aSQuentin Monnet * 1533fa15601aSQuentin Monnet * s64 bpf_csum_update(struct sk_buff *skb, __wsum csum) 1534fa15601aSQuentin Monnet * Description 1535fa15601aSQuentin Monnet * Add the checksum *csum* into *skb*\ **->csum** in case the 1536fa15601aSQuentin Monnet * driver has supplied a checksum for the entire packet into that 1537fa15601aSQuentin Monnet * field. Return an error otherwise. This helper is intended to be 1538fa15601aSQuentin Monnet * used in combination with **bpf_csum_diff**\ (), in particular 1539fa15601aSQuentin Monnet * when the checksum needs to be updated after data has been 1540fa15601aSQuentin Monnet * written into the packet through direct packet access. 1541fa15601aSQuentin Monnet * Return 1542fa15601aSQuentin Monnet * The checksum on success, or a negative error code in case of 1543fa15601aSQuentin Monnet * failure. 1544fa15601aSQuentin Monnet * 1545fa15601aSQuentin Monnet * void bpf_set_hash_invalid(struct sk_buff *skb) 1546fa15601aSQuentin Monnet * Description 1547fa15601aSQuentin Monnet * Invalidate the current *skb*\ **->hash**. It can be used after 1548fa15601aSQuentin Monnet * mangling on headers through direct packet access, in order to 1549fa15601aSQuentin Monnet * indicate that the hash is outdated and to trigger a 1550fa15601aSQuentin Monnet * recalculation the next time the kernel tries to access this 1551fa15601aSQuentin Monnet * hash or when the **bpf_get_hash_recalc**\ () helper is called. 1552fa15601aSQuentin Monnet * 1553bdb7b79bSAndrii Nakryiko * long bpf_get_numa_node_id(void) 1554fa15601aSQuentin Monnet * Description 1555fa15601aSQuentin Monnet * Return the id of the current NUMA node. The primary use case 1556fa15601aSQuentin Monnet * for this helper is the selection of sockets for the local NUMA 1557fa15601aSQuentin Monnet * node, when the program is attached to sockets using the 1558fa15601aSQuentin Monnet * **SO_ATTACH_REUSEPORT_EBPF** option (see also **socket(7)**), 1559fa15601aSQuentin Monnet * but the helper is also available to other eBPF program types, 1560fa15601aSQuentin Monnet * similarly to **bpf_get_smp_processor_id**\ (). 1561fa15601aSQuentin Monnet * Return 1562fa15601aSQuentin Monnet * The id of current NUMA node. 1563fa15601aSQuentin Monnet * 1564bdb7b79bSAndrii Nakryiko * long bpf_skb_change_head(struct sk_buff *skb, u32 len, u64 flags) 1565c6b5fb86SQuentin Monnet * Description 1566c6b5fb86SQuentin Monnet * Grows headroom of packet associated to *skb* and adjusts the 1567c6b5fb86SQuentin Monnet * offset of the MAC header accordingly, adding *len* bytes of 1568c6b5fb86SQuentin Monnet * space. It automatically extends and reallocates memory as 1569c6b5fb86SQuentin Monnet * required. 1570c6b5fb86SQuentin Monnet * 1571c6b5fb86SQuentin Monnet * This helper can be used on a layer 3 *skb* to push a MAC header 1572c6b5fb86SQuentin Monnet * for redirection into a layer 2 device. 1573c6b5fb86SQuentin Monnet * 1574c6b5fb86SQuentin Monnet * All values for *flags* are reserved for future usage, and must 1575c6b5fb86SQuentin Monnet * be left at zero. 1576c6b5fb86SQuentin Monnet * 157732e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1578c6b5fb86SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1579c6b5fb86SQuentin Monnet * previously done by the verifier are invalidated and must be 1580c6b5fb86SQuentin Monnet * performed again, if the helper is used in combination with 1581c6b5fb86SQuentin Monnet * direct packet access. 1582c6b5fb86SQuentin Monnet * Return 1583c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 1584c6b5fb86SQuentin Monnet * 1585bdb7b79bSAndrii Nakryiko * long bpf_xdp_adjust_head(struct xdp_buff *xdp_md, int delta) 1586c6b5fb86SQuentin Monnet * Description 1587c6b5fb86SQuentin Monnet * Adjust (move) *xdp_md*\ **->data** by *delta* bytes. Note that 1588c6b5fb86SQuentin Monnet * it is possible to use a negative value for *delta*. This helper 1589c6b5fb86SQuentin Monnet * can be used to prepare the packet for pushing or popping 1590c6b5fb86SQuentin Monnet * headers. 1591c6b5fb86SQuentin Monnet * 159232e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1593c6b5fb86SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1594c6b5fb86SQuentin Monnet * previously done by the verifier are invalidated and must be 1595c6b5fb86SQuentin Monnet * performed again, if the helper is used in combination with 1596c6b5fb86SQuentin Monnet * direct packet access. 1597c6b5fb86SQuentin Monnet * Return 1598c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 1599c6b5fb86SQuentin Monnet * 1600bdb7b79bSAndrii Nakryiko * long bpf_probe_read_str(void *dst, u32 size, const void *unsafe_ptr) 1601c6b5fb86SQuentin Monnet * Description 16026ae08ae3SDaniel Borkmann * Copy a NUL terminated string from an unsafe kernel address 1603ab8d7809SQuentin Monnet * *unsafe_ptr* to *dst*. See **bpf_probe_read_kernel_str**\ () for 16046ae08ae3SDaniel Borkmann * more details. 1605c6b5fb86SQuentin Monnet * 1606ab8d7809SQuentin Monnet * Generally, use **bpf_probe_read_user_str**\ () or 1607ab8d7809SQuentin Monnet * **bpf_probe_read_kernel_str**\ () instead. 1608c6b5fb86SQuentin Monnet * Return 1609c6b5fb86SQuentin Monnet * On success, the strictly positive length of the string, 1610c6b5fb86SQuentin Monnet * including the trailing NUL character. On error, a negative 1611c6b5fb86SQuentin Monnet * value. 1612c6b5fb86SQuentin Monnet * 1613c6b5fb86SQuentin Monnet * u64 bpf_get_socket_cookie(struct sk_buff *skb) 1614c6b5fb86SQuentin Monnet * Description 1615c6b5fb86SQuentin Monnet * If the **struct sk_buff** pointed by *skb* has a known socket, 1616c6b5fb86SQuentin Monnet * retrieve the cookie (generated by the kernel) of this socket. 1617c6b5fb86SQuentin Monnet * If no cookie has been set yet, generate a new cookie. Once 1618c6b5fb86SQuentin Monnet * generated, the socket cookie remains stable for the life of the 1619c6b5fb86SQuentin Monnet * socket. This helper can be useful for monitoring per socket 1620cd48bddaSDaniel Borkmann * networking traffic statistics as it provides a global socket 1621cd48bddaSDaniel Borkmann * identifier that can be assumed unique. 1622c6b5fb86SQuentin Monnet * Return 1623c6b5fb86SQuentin Monnet * A 8-byte long non-decreasing number on success, or 0 if the 1624c6b5fb86SQuentin Monnet * socket field is missing inside *skb*. 1625c6b5fb86SQuentin Monnet * 1626d692f113SAndrey Ignatov * u64 bpf_get_socket_cookie(struct bpf_sock_addr *ctx) 1627d692f113SAndrey Ignatov * Description 1628d692f113SAndrey Ignatov * Equivalent to bpf_get_socket_cookie() helper that accepts 162962369db2SQuentin Monnet * *skb*, but gets socket from **struct bpf_sock_addr** context. 1630d692f113SAndrey Ignatov * Return 1631d692f113SAndrey Ignatov * A 8-byte long non-decreasing number. 1632d692f113SAndrey Ignatov * 1633d692f113SAndrey Ignatov * u64 bpf_get_socket_cookie(struct bpf_sock_ops *ctx) 1634d692f113SAndrey Ignatov * Description 1635ab8d7809SQuentin Monnet * Equivalent to **bpf_get_socket_cookie**\ () helper that accepts 163662369db2SQuentin Monnet * *skb*, but gets socket from **struct bpf_sock_ops** context. 1637d692f113SAndrey Ignatov * Return 1638d692f113SAndrey Ignatov * A 8-byte long non-decreasing number. 1639d692f113SAndrey Ignatov * 1640c6b5fb86SQuentin Monnet * u32 bpf_get_socket_uid(struct sk_buff *skb) 1641c6b5fb86SQuentin Monnet * Return 1642c6b5fb86SQuentin Monnet * The owner UID of the socket associated to *skb*. If the socket 1643c6b5fb86SQuentin Monnet * is **NULL**, or if it is not a full socket (i.e. if it is a 1644c6b5fb86SQuentin Monnet * time-wait or a request socket instead), **overflowuid** value 1645c6b5fb86SQuentin Monnet * is returned (note that **overflowuid** might also be the actual 1646c6b5fb86SQuentin Monnet * UID value for the socket). 1647c6b5fb86SQuentin Monnet * 1648bdb7b79bSAndrii Nakryiko * long bpf_set_hash(struct sk_buff *skb, u32 hash) 1649fa15601aSQuentin Monnet * Description 1650fa15601aSQuentin Monnet * Set the full hash for *skb* (set the field *skb*\ **->hash**) 1651fa15601aSQuentin Monnet * to value *hash*. 1652fa15601aSQuentin Monnet * Return 1653fa15601aSQuentin Monnet * 0 1654fa15601aSQuentin Monnet * 1655bdb7b79bSAndrii Nakryiko * long bpf_setsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen) 16567aa79a86SQuentin Monnet * Description 16577aa79a86SQuentin Monnet * Emulate a call to **setsockopt()** on the socket associated to 16587aa79a86SQuentin Monnet * *bpf_socket*, which must be a full socket. The *level* at 16597aa79a86SQuentin Monnet * which the option resides and the name *optname* of the option 16607aa79a86SQuentin Monnet * must be specified, see **setsockopt(2)** for more information. 16617aa79a86SQuentin Monnet * The option value of length *optlen* is pointed by *optval*. 16627aa79a86SQuentin Monnet * 1663beecf11bSStanislav Fomichev * *bpf_socket* should be one of the following: 1664ab8d7809SQuentin Monnet * 1665beecf11bSStanislav Fomichev * * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**. 1666beecf11bSStanislav Fomichev * * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT** 1667beecf11bSStanislav Fomichev * and **BPF_CGROUP_INET6_CONNECT**. 1668beecf11bSStanislav Fomichev * 16697aa79a86SQuentin Monnet * This helper actually implements a subset of **setsockopt()**. 16707aa79a86SQuentin Monnet * It supports the following *level*\ s: 16717aa79a86SQuentin Monnet * 16727aa79a86SQuentin Monnet * * **SOL_SOCKET**, which supports the following *optname*\ s: 16737aa79a86SQuentin Monnet * **SO_RCVBUF**, **SO_SNDBUF**, **SO_MAX_PACING_RATE**, 1674f9bcf968SDmitry Yakunin * **SO_PRIORITY**, **SO_RCVLOWAT**, **SO_MARK**, 1675f9bcf968SDmitry Yakunin * **SO_BINDTODEVICE**, **SO_KEEPALIVE**. 16767aa79a86SQuentin Monnet * * **IPPROTO_TCP**, which supports the following *optname*\ s: 16777aa79a86SQuentin Monnet * **TCP_CONGESTION**, **TCP_BPF_IW**, 1678f9bcf968SDmitry Yakunin * **TCP_BPF_SNDCWND_CLAMP**, **TCP_SAVE_SYN**, 1679f9bcf968SDmitry Yakunin * **TCP_KEEPIDLE**, **TCP_KEEPINTVL**, **TCP_KEEPCNT**, 1680f9bcf968SDmitry Yakunin * **TCP_SYNCNT**, **TCP_USER_TIMEOUT**. 16817aa79a86SQuentin Monnet * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. 16827aa79a86SQuentin Monnet * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**. 16837aa79a86SQuentin Monnet * Return 16847aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 16857aa79a86SQuentin Monnet * 1686bdb7b79bSAndrii Nakryiko * long bpf_skb_adjust_room(struct sk_buff *skb, s32 len_diff, u32 mode, u64 flags) 1687fa15601aSQuentin Monnet * Description 1688fa15601aSQuentin Monnet * Grow or shrink the room for data in the packet associated to 1689fa15601aSQuentin Monnet * *skb* by *len_diff*, and according to the selected *mode*. 1690fa15601aSQuentin Monnet * 1691836e66c2SDaniel Borkmann * By default, the helper will reset any offloaded checksum 1692836e66c2SDaniel Borkmann * indicator of the skb to CHECKSUM_NONE. This can be avoided 1693836e66c2SDaniel Borkmann * by the following flag: 1694836e66c2SDaniel Borkmann * 1695836e66c2SDaniel Borkmann * * **BPF_F_ADJ_ROOM_NO_CSUM_RESET**: Do not reset offloaded 1696836e66c2SDaniel Borkmann * checksum data of the skb to CHECKSUM_NONE. 1697836e66c2SDaniel Borkmann * 169814aa3192SWillem de Bruijn * There are two supported modes at this time: 169914aa3192SWillem de Bruijn * 170014aa3192SWillem de Bruijn * * **BPF_ADJ_ROOM_MAC**: Adjust room at the mac layer 170114aa3192SWillem de Bruijn * (room space is added or removed below the layer 2 header). 1702fa15601aSQuentin Monnet * 1703fa15601aSQuentin Monnet * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer 1704fa15601aSQuentin Monnet * (room space is added or removed below the layer 3 header). 1705fa15601aSQuentin Monnet * 1706868d5235SWillem de Bruijn * The following flags are supported at this time: 17072278f6ccSWillem de Bruijn * 17082278f6ccSWillem de Bruijn * * **BPF_F_ADJ_ROOM_FIXED_GSO**: Do not adjust gso_size. 17092278f6ccSWillem de Bruijn * Adjusting mss in this way is not allowed for datagrams. 1710fa15601aSQuentin Monnet * 171180867c5eSQuentin Monnet * * **BPF_F_ADJ_ROOM_ENCAP_L3_IPV4**, 171280867c5eSQuentin Monnet * **BPF_F_ADJ_ROOM_ENCAP_L3_IPV6**: 1713868d5235SWillem de Bruijn * Any new space is reserved to hold a tunnel header. 1714868d5235SWillem de Bruijn * Configure skb offsets and other fields accordingly. 1715868d5235SWillem de Bruijn * 171680867c5eSQuentin Monnet * * **BPF_F_ADJ_ROOM_ENCAP_L4_GRE**, 171780867c5eSQuentin Monnet * **BPF_F_ADJ_ROOM_ENCAP_L4_UDP**: 1718868d5235SWillem de Bruijn * Use with ENCAP_L3 flags to further specify the tunnel type. 1719868d5235SWillem de Bruijn * 172080867c5eSQuentin Monnet * * **BPF_F_ADJ_ROOM_ENCAP_L2**\ (*len*): 172158dfc900SAlan Maguire * Use with ENCAP_L3/L4 flags to further specify the tunnel 172280867c5eSQuentin Monnet * type; *len* is the length of the inner MAC header. 172358dfc900SAlan Maguire * 172432e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1725fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1726fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1727fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1728fa15601aSQuentin Monnet * direct packet access. 1729fa15601aSQuentin Monnet * Return 1730fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 1731fa15601aSQuentin Monnet * 1732bdb7b79bSAndrii Nakryiko * long bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags) 1733ab127040SQuentin Monnet * Description 1734ab127040SQuentin Monnet * Redirect the packet to the endpoint referenced by *map* at 1735ab127040SQuentin Monnet * index *key*. Depending on its type, this *map* can contain 1736ab127040SQuentin Monnet * references to net devices (for forwarding packets through other 1737ab127040SQuentin Monnet * ports), or to CPUs (for redirecting XDP frames to another CPU; 1738ab127040SQuentin Monnet * but this is only implemented for native XDP (with driver 1739ab127040SQuentin Monnet * support) as of this writing). 1740ab127040SQuentin Monnet * 174143e74c02SToke Høiland-Jørgensen * The lower two bits of *flags* are used as the return code if 174243e74c02SToke Høiland-Jørgensen * the map lookup fails. This is so that the return value can be 1743ab8d7809SQuentin Monnet * one of the XDP program return codes up to **XDP_TX**, as chosen 1744ab8d7809SQuentin Monnet * by the caller. Any higher bits in the *flags* argument must be 174543e74c02SToke Høiland-Jørgensen * unset. 1746ab127040SQuentin Monnet * 1747ab8d7809SQuentin Monnet * See also **bpf_redirect**\ (), which only supports redirecting 1748ab8d7809SQuentin Monnet * to an ifindex, but doesn't require a map to do so. 1749ab127040SQuentin Monnet * Return 1750f25975f4SToke Høiland-Jørgensen * **XDP_REDIRECT** on success, or the value of the two lower bits 1751a33d3147SJakub Wilk * of the *flags* argument on error. 1752ab127040SQuentin Monnet * 1753bdb7b79bSAndrii Nakryiko * long bpf_sk_redirect_map(struct sk_buff *skb, struct bpf_map *map, u32 key, u64 flags) 1754ab127040SQuentin Monnet * Description 1755ab127040SQuentin Monnet * Redirect the packet to the socket referenced by *map* (of type 1756ab127040SQuentin Monnet * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and 1757ab127040SQuentin Monnet * egress interfaces can be used for redirection. The 1758ab127040SQuentin Monnet * **BPF_F_INGRESS** value in *flags* is used to make the 1759ab127040SQuentin Monnet * distinction (ingress path is selected if the flag is present, 1760ab127040SQuentin Monnet * egress path otherwise). This is the only flag supported for now. 1761ab127040SQuentin Monnet * Return 1762ab127040SQuentin Monnet * **SK_PASS** on success, or **SK_DROP** on error. 1763ab127040SQuentin Monnet * 1764bdb7b79bSAndrii Nakryiko * long bpf_sock_map_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags) 1765ab127040SQuentin Monnet * Description 1766ab127040SQuentin Monnet * Add an entry to, or update a *map* referencing sockets. The 1767ab127040SQuentin Monnet * *skops* is used as a new value for the entry associated to 1768ab127040SQuentin Monnet * *key*. *flags* is one of: 1769ab127040SQuentin Monnet * 1770ab127040SQuentin Monnet * **BPF_NOEXIST** 1771ab127040SQuentin Monnet * The entry for *key* must not exist in the map. 1772ab127040SQuentin Monnet * **BPF_EXIST** 1773ab127040SQuentin Monnet * The entry for *key* must already exist in the map. 1774ab127040SQuentin Monnet * **BPF_ANY** 1775ab127040SQuentin Monnet * No condition on the existence of the entry for *key*. 1776ab127040SQuentin Monnet * 1777ab127040SQuentin Monnet * If the *map* has eBPF programs (parser and verdict), those will 1778ab127040SQuentin Monnet * be inherited by the socket being added. If the socket is 1779ab127040SQuentin Monnet * already attached to eBPF programs, this results in an error. 1780ab127040SQuentin Monnet * Return 1781ab127040SQuentin Monnet * 0 on success, or a negative error in case of failure. 1782ab127040SQuentin Monnet * 1783bdb7b79bSAndrii Nakryiko * long bpf_xdp_adjust_meta(struct xdp_buff *xdp_md, int delta) 1784fa15601aSQuentin Monnet * Description 1785fa15601aSQuentin Monnet * Adjust the address pointed by *xdp_md*\ **->data_meta** by 1786fa15601aSQuentin Monnet * *delta* (which can be positive or negative). Note that this 1787fa15601aSQuentin Monnet * operation modifies the address stored in *xdp_md*\ **->data**, 1788fa15601aSQuentin Monnet * so the latter must be loaded only after the helper has been 1789fa15601aSQuentin Monnet * called. 1790fa15601aSQuentin Monnet * 1791fa15601aSQuentin Monnet * The use of *xdp_md*\ **->data_meta** is optional and programs 1792fa15601aSQuentin Monnet * are not required to use it. The rationale is that when the 1793fa15601aSQuentin Monnet * packet is processed with XDP (e.g. as DoS filter), it is 1794fa15601aSQuentin Monnet * possible to push further meta data along with it before passing 1795fa15601aSQuentin Monnet * to the stack, and to give the guarantee that an ingress eBPF 1796fa15601aSQuentin Monnet * program attached as a TC classifier on the same device can pick 1797fa15601aSQuentin Monnet * this up for further post-processing. Since TC works with socket 1798fa15601aSQuentin Monnet * buffers, it remains possible to set from XDP the **mark** or 1799fa15601aSQuentin Monnet * **priority** pointers, or other pointers for the socket buffer. 1800fa15601aSQuentin Monnet * Having this scratch space generic and programmable allows for 1801fa15601aSQuentin Monnet * more flexibility as the user is free to store whatever meta 1802fa15601aSQuentin Monnet * data they need. 1803fa15601aSQuentin Monnet * 180432e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1805fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1806fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1807fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1808fa15601aSQuentin Monnet * direct packet access. 1809fa15601aSQuentin Monnet * Return 1810fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 18117aa79a86SQuentin Monnet * 1812bdb7b79bSAndrii Nakryiko * long bpf_perf_event_read_value(struct bpf_map *map, u64 flags, struct bpf_perf_event_value *buf, u32 buf_size) 18137aa79a86SQuentin Monnet * Description 18147aa79a86SQuentin Monnet * Read the value of a perf event counter, and store it into *buf* 18157aa79a86SQuentin Monnet * of size *buf_size*. This helper relies on a *map* of type 18167aa79a86SQuentin Monnet * **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of the perf event 18177aa79a86SQuentin Monnet * counter is selected when *map* is updated with perf event file 18187aa79a86SQuentin Monnet * descriptors. The *map* is an array whose size is the number of 18197aa79a86SQuentin Monnet * available CPUs, and each cell contains a value relative to one 18207aa79a86SQuentin Monnet * CPU. The value to retrieve is indicated by *flags*, that 18217aa79a86SQuentin Monnet * contains the index of the CPU to look up, masked with 18227aa79a86SQuentin Monnet * **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to 18237aa79a86SQuentin Monnet * **BPF_F_CURRENT_CPU** to indicate that the value for the 18247aa79a86SQuentin Monnet * current CPU should be retrieved. 18257aa79a86SQuentin Monnet * 18267aa79a86SQuentin Monnet * This helper behaves in a way close to 18277aa79a86SQuentin Monnet * **bpf_perf_event_read**\ () helper, save that instead of 18287aa79a86SQuentin Monnet * just returning the value observed, it fills the *buf* 18297aa79a86SQuentin Monnet * structure. This allows for additional data to be retrieved: in 18307aa79a86SQuentin Monnet * particular, the enabled and running times (in *buf*\ 18317aa79a86SQuentin Monnet * **->enabled** and *buf*\ **->running**, respectively) are 18327aa79a86SQuentin Monnet * copied. In general, **bpf_perf_event_read_value**\ () is 18337aa79a86SQuentin Monnet * recommended over **bpf_perf_event_read**\ (), which has some 18347aa79a86SQuentin Monnet * ABI issues and provides fewer functionalities. 18357aa79a86SQuentin Monnet * 18367aa79a86SQuentin Monnet * These values are interesting, because hardware PMU (Performance 18377aa79a86SQuentin Monnet * Monitoring Unit) counters are limited resources. When there are 18387aa79a86SQuentin Monnet * more PMU based perf events opened than available counters, 18397aa79a86SQuentin Monnet * kernel will multiplex these events so each event gets certain 18407aa79a86SQuentin Monnet * percentage (but not all) of the PMU time. In case that 18417aa79a86SQuentin Monnet * multiplexing happens, the number of samples or counter value 18427aa79a86SQuentin Monnet * will not reflect the case compared to when no multiplexing 18437aa79a86SQuentin Monnet * occurs. This makes comparison between different runs difficult. 18447aa79a86SQuentin Monnet * Typically, the counter value should be normalized before 18457aa79a86SQuentin Monnet * comparing to other experiments. The usual normalization is done 18467aa79a86SQuentin Monnet * as follows. 18477aa79a86SQuentin Monnet * 18487aa79a86SQuentin Monnet * :: 18497aa79a86SQuentin Monnet * 18507aa79a86SQuentin Monnet * normalized_counter = counter * t_enabled / t_running 18517aa79a86SQuentin Monnet * 18527aa79a86SQuentin Monnet * Where t_enabled is the time enabled for event and t_running is 18537aa79a86SQuentin Monnet * the time running for event since last normalization. The 18547aa79a86SQuentin Monnet * enabled and running times are accumulated since the perf event 18557aa79a86SQuentin Monnet * open. To achieve scaling factor between two invocations of an 1856ab8d7809SQuentin Monnet * eBPF program, users can use CPU id as the key (which is 18577aa79a86SQuentin Monnet * typical for perf array usage model) to remember the previous 18587aa79a86SQuentin Monnet * value and do the calculation inside the eBPF program. 18597aa79a86SQuentin Monnet * Return 18607aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 18617aa79a86SQuentin Monnet * 1862bdb7b79bSAndrii Nakryiko * long bpf_perf_prog_read_value(struct bpf_perf_event_data *ctx, struct bpf_perf_event_value *buf, u32 buf_size) 18637aa79a86SQuentin Monnet * Description 18647aa79a86SQuentin Monnet * For en eBPF program attached to a perf event, retrieve the 18657aa79a86SQuentin Monnet * value of the event counter associated to *ctx* and store it in 18667aa79a86SQuentin Monnet * the structure pointed by *buf* and of size *buf_size*. Enabled 18677aa79a86SQuentin Monnet * and running times are also stored in the structure (see 18687aa79a86SQuentin Monnet * description of helper **bpf_perf_event_read_value**\ () for 18697aa79a86SQuentin Monnet * more details). 18707aa79a86SQuentin Monnet * Return 18717aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 18727aa79a86SQuentin Monnet * 1873bdb7b79bSAndrii Nakryiko * long bpf_getsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen) 18747aa79a86SQuentin Monnet * Description 18757aa79a86SQuentin Monnet * Emulate a call to **getsockopt()** on the socket associated to 18767aa79a86SQuentin Monnet * *bpf_socket*, which must be a full socket. The *level* at 18777aa79a86SQuentin Monnet * which the option resides and the name *optname* of the option 18787aa79a86SQuentin Monnet * must be specified, see **getsockopt(2)** for more information. 18797aa79a86SQuentin Monnet * The retrieved value is stored in the structure pointed by 18807aa79a86SQuentin Monnet * *opval* and of length *optlen*. 18817aa79a86SQuentin Monnet * 1882beecf11bSStanislav Fomichev * *bpf_socket* should be one of the following: 1883ab8d7809SQuentin Monnet * 1884beecf11bSStanislav Fomichev * * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**. 1885beecf11bSStanislav Fomichev * * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT** 1886beecf11bSStanislav Fomichev * and **BPF_CGROUP_INET6_CONNECT**. 1887beecf11bSStanislav Fomichev * 18887aa79a86SQuentin Monnet * This helper actually implements a subset of **getsockopt()**. 18897aa79a86SQuentin Monnet * It supports the following *level*\ s: 18907aa79a86SQuentin Monnet * 18917aa79a86SQuentin Monnet * * **IPPROTO_TCP**, which supports *optname* 18927aa79a86SQuentin Monnet * **TCP_CONGESTION**. 18937aa79a86SQuentin Monnet * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. 18947aa79a86SQuentin Monnet * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**. 18957aa79a86SQuentin Monnet * Return 18967aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 18977aa79a86SQuentin Monnet * 1898bdb7b79bSAndrii Nakryiko * long bpf_override_return(struct pt_regs *regs, u64 rc) 18997aa79a86SQuentin Monnet * Description 19007aa79a86SQuentin Monnet * Used for error injection, this helper uses kprobes to override 19017aa79a86SQuentin Monnet * the return value of the probed function, and to set it to *rc*. 19027aa79a86SQuentin Monnet * The first argument is the context *regs* on which the kprobe 19037aa79a86SQuentin Monnet * works. 19047aa79a86SQuentin Monnet * 1905ab8d7809SQuentin Monnet * This helper works by setting the PC (program counter) 19067aa79a86SQuentin Monnet * to an override function which is run in place of the original 19077aa79a86SQuentin Monnet * probed function. This means the probed function is not run at 19087aa79a86SQuentin Monnet * all. The replacement function just returns with the required 19097aa79a86SQuentin Monnet * value. 19107aa79a86SQuentin Monnet * 19117aa79a86SQuentin Monnet * This helper has security implications, and thus is subject to 19127aa79a86SQuentin Monnet * restrictions. It is only available if the kernel was compiled 19137aa79a86SQuentin Monnet * with the **CONFIG_BPF_KPROBE_OVERRIDE** configuration 19147aa79a86SQuentin Monnet * option, and in this case it only works on functions tagged with 19157aa79a86SQuentin Monnet * **ALLOW_ERROR_INJECTION** in the kernel code. 19167aa79a86SQuentin Monnet * 19177aa79a86SQuentin Monnet * Also, the helper is only available for the architectures having 19187aa79a86SQuentin Monnet * the CONFIG_FUNCTION_ERROR_INJECTION option. As of this writing, 19197aa79a86SQuentin Monnet * x86 architecture is the only one to support this feature. 19207aa79a86SQuentin Monnet * Return 19217aa79a86SQuentin Monnet * 0 19227aa79a86SQuentin Monnet * 1923bdb7b79bSAndrii Nakryiko * long bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *bpf_sock, int argval) 19247aa79a86SQuentin Monnet * Description 19257aa79a86SQuentin Monnet * Attempt to set the value of the **bpf_sock_ops_cb_flags** field 19267aa79a86SQuentin Monnet * for the full TCP socket associated to *bpf_sock_ops* to 19277aa79a86SQuentin Monnet * *argval*. 19287aa79a86SQuentin Monnet * 19297aa79a86SQuentin Monnet * The primary use of this field is to determine if there should 19307aa79a86SQuentin Monnet * be calls to eBPF programs of type 19317aa79a86SQuentin Monnet * **BPF_PROG_TYPE_SOCK_OPS** at various points in the TCP 19327aa79a86SQuentin Monnet * code. A program of the same type can change its value, per 19337aa79a86SQuentin Monnet * connection and as necessary, when the connection is 19347aa79a86SQuentin Monnet * established. This field is directly accessible for reading, but 19357aa79a86SQuentin Monnet * this helper must be used for updates in order to return an 19367aa79a86SQuentin Monnet * error if an eBPF program tries to set a callback that is not 19377aa79a86SQuentin Monnet * supported in the current kernel. 19387aa79a86SQuentin Monnet * 1939725721a6SViet Hoang Tran * *argval* is a flag array which can combine these flags: 19407aa79a86SQuentin Monnet * 19417aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_RTO_CB_FLAG** (retransmission time out) 19427aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_RETRANS_CB_FLAG** (retransmission) 19437aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_STATE_CB_FLAG** (TCP state change) 194423729ff2SStanislav Fomichev * * **BPF_SOCK_OPS_RTT_CB_FLAG** (every RTT) 19457aa79a86SQuentin Monnet * 1946725721a6SViet Hoang Tran * Therefore, this function can be used to clear a callback flag by 1947725721a6SViet Hoang Tran * setting the appropriate bit to zero. e.g. to disable the RTO 1948725721a6SViet Hoang Tran * callback: 1949725721a6SViet Hoang Tran * 1950725721a6SViet Hoang Tran * **bpf_sock_ops_cb_flags_set(bpf_sock,** 1951725721a6SViet Hoang Tran * **bpf_sock->bpf_sock_ops_cb_flags & ~BPF_SOCK_OPS_RTO_CB_FLAG)** 1952725721a6SViet Hoang Tran * 19537aa79a86SQuentin Monnet * Here are some examples of where one could call such eBPF 19547aa79a86SQuentin Monnet * program: 19557aa79a86SQuentin Monnet * 19567aa79a86SQuentin Monnet * * When RTO fires. 19577aa79a86SQuentin Monnet * * When a packet is retransmitted. 19587aa79a86SQuentin Monnet * * When the connection terminates. 19597aa79a86SQuentin Monnet * * When a packet is sent. 19607aa79a86SQuentin Monnet * * When a packet is received. 19617aa79a86SQuentin Monnet * Return 19627aa79a86SQuentin Monnet * Code **-EINVAL** if the socket is not a full TCP socket; 19637aa79a86SQuentin Monnet * otherwise, a positive number containing the bits that could not 19647aa79a86SQuentin Monnet * be set is returned (which comes down to 0 if all bits were set 19657aa79a86SQuentin Monnet * as required). 19667aa79a86SQuentin Monnet * 1967bdb7b79bSAndrii Nakryiko * long bpf_msg_redirect_map(struct sk_msg_buff *msg, struct bpf_map *map, u32 key, u64 flags) 1968ab127040SQuentin Monnet * Description 1969ab127040SQuentin Monnet * This helper is used in programs implementing policies at the 1970ab127040SQuentin Monnet * socket level. If the message *msg* is allowed to pass (i.e. if 1971ab127040SQuentin Monnet * the verdict eBPF program returns **SK_PASS**), redirect it to 1972ab127040SQuentin Monnet * the socket referenced by *map* (of type 1973ab127040SQuentin Monnet * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and 1974ab127040SQuentin Monnet * egress interfaces can be used for redirection. The 1975ab127040SQuentin Monnet * **BPF_F_INGRESS** value in *flags* is used to make the 1976ab127040SQuentin Monnet * distinction (ingress path is selected if the flag is present, 1977ab127040SQuentin Monnet * egress path otherwise). This is the only flag supported for now. 1978ab127040SQuentin Monnet * Return 1979ab127040SQuentin Monnet * **SK_PASS** on success, or **SK_DROP** on error. 1980ab127040SQuentin Monnet * 1981bdb7b79bSAndrii Nakryiko * long bpf_msg_apply_bytes(struct sk_msg_buff *msg, u32 bytes) 1982ab127040SQuentin Monnet * Description 1983ab127040SQuentin Monnet * For socket policies, apply the verdict of the eBPF program to 1984ab127040SQuentin Monnet * the next *bytes* (number of bytes) of message *msg*. 1985ab127040SQuentin Monnet * 1986ab127040SQuentin Monnet * For example, this helper can be used in the following cases: 1987ab127040SQuentin Monnet * 1988ab127040SQuentin Monnet * * A single **sendmsg**\ () or **sendfile**\ () system call 1989ab127040SQuentin Monnet * contains multiple logical messages that the eBPF program is 1990ab127040SQuentin Monnet * supposed to read and for which it should apply a verdict. 1991ab127040SQuentin Monnet * * An eBPF program only cares to read the first *bytes* of a 1992ab127040SQuentin Monnet * *msg*. If the message has a large payload, then setting up 1993ab127040SQuentin Monnet * and calling the eBPF program repeatedly for all bytes, even 1994ab127040SQuentin Monnet * though the verdict is already known, would create unnecessary 1995ab127040SQuentin Monnet * overhead. 1996ab127040SQuentin Monnet * 1997ab127040SQuentin Monnet * When called from within an eBPF program, the helper sets a 1998ab127040SQuentin Monnet * counter internal to the BPF infrastructure, that is used to 1999ab127040SQuentin Monnet * apply the last verdict to the next *bytes*. If *bytes* is 2000ab127040SQuentin Monnet * smaller than the current data being processed from a 2001ab127040SQuentin Monnet * **sendmsg**\ () or **sendfile**\ () system call, the first 2002ab127040SQuentin Monnet * *bytes* will be sent and the eBPF program will be re-run with 2003ab127040SQuentin Monnet * the pointer for start of data pointing to byte number *bytes* 2004ab127040SQuentin Monnet * **+ 1**. If *bytes* is larger than the current data being 2005ab127040SQuentin Monnet * processed, then the eBPF verdict will be applied to multiple 2006ab127040SQuentin Monnet * **sendmsg**\ () or **sendfile**\ () calls until *bytes* are 2007ab127040SQuentin Monnet * consumed. 2008ab127040SQuentin Monnet * 2009ab127040SQuentin Monnet * Note that if a socket closes with the internal counter holding 2010ab127040SQuentin Monnet * a non-zero value, this is not a problem because data is not 2011ab127040SQuentin Monnet * being buffered for *bytes* and is sent as it is received. 2012ab127040SQuentin Monnet * Return 2013ab127040SQuentin Monnet * 0 2014ab127040SQuentin Monnet * 2015bdb7b79bSAndrii Nakryiko * long bpf_msg_cork_bytes(struct sk_msg_buff *msg, u32 bytes) 2016ab127040SQuentin Monnet * Description 2017ab127040SQuentin Monnet * For socket policies, prevent the execution of the verdict eBPF 2018ab127040SQuentin Monnet * program for message *msg* until *bytes* (byte number) have been 2019ab127040SQuentin Monnet * accumulated. 2020ab127040SQuentin Monnet * 2021ab127040SQuentin Monnet * This can be used when one needs a specific number of bytes 2022ab127040SQuentin Monnet * before a verdict can be assigned, even if the data spans 2023ab127040SQuentin Monnet * multiple **sendmsg**\ () or **sendfile**\ () calls. The extreme 2024ab127040SQuentin Monnet * case would be a user calling **sendmsg**\ () repeatedly with 2025ab127040SQuentin Monnet * 1-byte long message segments. Obviously, this is bad for 2026ab127040SQuentin Monnet * performance, but it is still valid. If the eBPF program needs 2027ab127040SQuentin Monnet * *bytes* bytes to validate a header, this helper can be used to 2028ab127040SQuentin Monnet * prevent the eBPF program to be called again until *bytes* have 2029ab127040SQuentin Monnet * been accumulated. 2030ab127040SQuentin Monnet * Return 2031ab127040SQuentin Monnet * 0 2032ab127040SQuentin Monnet * 2033bdb7b79bSAndrii Nakryiko * long bpf_msg_pull_data(struct sk_msg_buff *msg, u32 start, u32 end, u64 flags) 2034ab127040SQuentin Monnet * Description 2035ab127040SQuentin Monnet * For socket policies, pull in non-linear data from user space 2036ab127040SQuentin Monnet * for *msg* and set pointers *msg*\ **->data** and *msg*\ 2037ab127040SQuentin Monnet * **->data_end** to *start* and *end* bytes offsets into *msg*, 2038ab127040SQuentin Monnet * respectively. 2039ab127040SQuentin Monnet * 2040ab127040SQuentin Monnet * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a 2041ab127040SQuentin Monnet * *msg* it can only parse data that the (**data**, **data_end**) 2042ab127040SQuentin Monnet * pointers have already consumed. For **sendmsg**\ () hooks this 2043ab127040SQuentin Monnet * is likely the first scatterlist element. But for calls relying 2044ab127040SQuentin Monnet * on the **sendpage** handler (e.g. **sendfile**\ ()) this will 2045ab127040SQuentin Monnet * be the range (**0**, **0**) because the data is shared with 2046ab127040SQuentin Monnet * user space and by default the objective is to avoid allowing 2047ab127040SQuentin Monnet * user space to modify data while (or after) eBPF verdict is 2048ab127040SQuentin Monnet * being decided. This helper can be used to pull in data and to 2049ab127040SQuentin Monnet * set the start and end pointer to given values. Data will be 2050ab127040SQuentin Monnet * copied if necessary (i.e. if data was not linear and if start 2051ab127040SQuentin Monnet * and end pointers do not point to the same chunk). 2052ab127040SQuentin Monnet * 205332e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2054ab127040SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 2055ab127040SQuentin Monnet * previously done by the verifier are invalidated and must be 2056ab127040SQuentin Monnet * performed again, if the helper is used in combination with 2057ab127040SQuentin Monnet * direct packet access. 2058ab127040SQuentin Monnet * 2059ab127040SQuentin Monnet * All values for *flags* are reserved for future usage, and must 2060ab127040SQuentin Monnet * be left at zero. 2061ab127040SQuentin Monnet * Return 2062ab127040SQuentin Monnet * 0 on success, or a negative error in case of failure. 2063ab127040SQuentin Monnet * 2064bdb7b79bSAndrii Nakryiko * long bpf_bind(struct bpf_sock_addr *ctx, struct sockaddr *addr, int addr_len) 20657aa79a86SQuentin Monnet * Description 20667aa79a86SQuentin Monnet * Bind the socket associated to *ctx* to the address pointed by 20677aa79a86SQuentin Monnet * *addr*, of length *addr_len*. This allows for making outgoing 20687aa79a86SQuentin Monnet * connection from the desired IP address, which can be useful for 20697aa79a86SQuentin Monnet * example when all processes inside a cgroup should use one 20707aa79a86SQuentin Monnet * single IP address on a host that has multiple IP configured. 20717aa79a86SQuentin Monnet * 20727aa79a86SQuentin Monnet * This helper works for IPv4 and IPv6, TCP and UDP sockets. The 20737aa79a86SQuentin Monnet * domain (*addr*\ **->sa_family**) must be **AF_INET** (or 20748086fbafSStanislav Fomichev * **AF_INET6**). It's advised to pass zero port (**sin_port** 20758086fbafSStanislav Fomichev * or **sin6_port**) which triggers IP_BIND_ADDRESS_NO_PORT-like 20768086fbafSStanislav Fomichev * behavior and lets the kernel efficiently pick up an unused 20778086fbafSStanislav Fomichev * port as long as 4-tuple is unique. Passing non-zero port might 20788086fbafSStanislav Fomichev * lead to degraded performance. 20797aa79a86SQuentin Monnet * Return 20807aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 20812d020dd7SQuentin Monnet * 2082bdb7b79bSAndrii Nakryiko * long bpf_xdp_adjust_tail(struct xdp_buff *xdp_md, int delta) 20832d020dd7SQuentin Monnet * Description 20842d020dd7SQuentin Monnet * Adjust (move) *xdp_md*\ **->data_end** by *delta* bytes. It is 2085c8741e2bSJesper Dangaard Brouer * possible to both shrink and grow the packet tail. 2086c8741e2bSJesper Dangaard Brouer * Shrink done via *delta* being a negative integer. 20872d020dd7SQuentin Monnet * 208832e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 20892d020dd7SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 20902d020dd7SQuentin Monnet * previously done by the verifier are invalidated and must be 20912d020dd7SQuentin Monnet * performed again, if the helper is used in combination with 20922d020dd7SQuentin Monnet * direct packet access. 20932d020dd7SQuentin Monnet * Return 20942d020dd7SQuentin Monnet * 0 on success, or a negative error in case of failure. 20952d020dd7SQuentin Monnet * 2096bdb7b79bSAndrii Nakryiko * long bpf_skb_get_xfrm_state(struct sk_buff *skb, u32 index, struct bpf_xfrm_state *xfrm_state, u32 size, u64 flags) 20972d020dd7SQuentin Monnet * Description 20982d020dd7SQuentin Monnet * Retrieve the XFRM state (IP transform framework, see also 20992d020dd7SQuentin Monnet * **ip-xfrm(8)**) at *index* in XFRM "security path" for *skb*. 21002d020dd7SQuentin Monnet * 21012d020dd7SQuentin Monnet * The retrieved value is stored in the **struct bpf_xfrm_state** 21022d020dd7SQuentin Monnet * pointed by *xfrm_state* and of length *size*. 21032d020dd7SQuentin Monnet * 21042d020dd7SQuentin Monnet * All values for *flags* are reserved for future usage, and must 21052d020dd7SQuentin Monnet * be left at zero. 21062d020dd7SQuentin Monnet * 21072d020dd7SQuentin Monnet * This helper is available only if the kernel was compiled with 21082d020dd7SQuentin Monnet * **CONFIG_XFRM** configuration option. 21092d020dd7SQuentin Monnet * Return 21102d020dd7SQuentin Monnet * 0 on success, or a negative error in case of failure. 2111c195651eSYonghong Song * 2112bdb7b79bSAndrii Nakryiko * long bpf_get_stack(void *ctx, void *buf, u32 size, u64 flags) 2113c195651eSYonghong Song * Description 2114c195651eSYonghong Song * Return a user or a kernel stack in bpf program provided buffer. 2115c195651eSYonghong Song * To achieve this, the helper needs *ctx*, which is a pointer 2116c195651eSYonghong Song * to the context on which the tracing program is executed. 2117c195651eSYonghong Song * To store the stacktrace, the bpf program provides *buf* with 2118c195651eSYonghong Song * a nonnegative *size*. 2119c195651eSYonghong Song * 2120c195651eSYonghong Song * The last argument, *flags*, holds the number of stack frames to 2121c195651eSYonghong Song * skip (from 0 to 255), masked with 2122c195651eSYonghong Song * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 2123c195651eSYonghong Song * the following flags: 2124c195651eSYonghong Song * 2125c195651eSYonghong Song * **BPF_F_USER_STACK** 2126c195651eSYonghong Song * Collect a user space stack instead of a kernel stack. 2127c195651eSYonghong Song * **BPF_F_USER_BUILD_ID** 2128c195651eSYonghong Song * Collect buildid+offset instead of ips for user stack, 2129c195651eSYonghong Song * only valid if **BPF_F_USER_STACK** is also specified. 2130c195651eSYonghong Song * 2131c195651eSYonghong Song * **bpf_get_stack**\ () can collect up to 2132c195651eSYonghong Song * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject 2133c195651eSYonghong Song * to sufficient large buffer size. Note that 2134c195651eSYonghong Song * this limit can be controlled with the **sysctl** program, and 2135c195651eSYonghong Song * that it should be manually increased in order to profile long 2136c195651eSYonghong Song * user stacks (such as stacks for Java programs). To do so, use: 2137c195651eSYonghong Song * 2138c195651eSYonghong Song * :: 2139c195651eSYonghong Song * 2140c195651eSYonghong Song * # sysctl kernel.perf_event_max_stack=<new value> 2141c195651eSYonghong Song * Return 21427a279e93SQuentin Monnet * A non-negative value equal to or less than *size* on success, 21437a279e93SQuentin Monnet * or a negative error in case of failure. 21444e1ec56cSDaniel Borkmann * 2145bdb7b79bSAndrii Nakryiko * long bpf_skb_load_bytes_relative(const void *skb, u32 offset, void *to, u32 len, u32 start_header) 21464e1ec56cSDaniel Borkmann * Description 21474e1ec56cSDaniel Borkmann * This helper is similar to **bpf_skb_load_bytes**\ () in that 21484e1ec56cSDaniel Borkmann * it provides an easy way to load *len* bytes from *offset* 21494e1ec56cSDaniel Borkmann * from the packet associated to *skb*, into the buffer pointed 21504e1ec56cSDaniel Borkmann * by *to*. The difference to **bpf_skb_load_bytes**\ () is that 21514e1ec56cSDaniel Borkmann * a fifth argument *start_header* exists in order to select a 21524e1ec56cSDaniel Borkmann * base offset to start from. *start_header* can be one of: 21534e1ec56cSDaniel Borkmann * 21544e1ec56cSDaniel Borkmann * **BPF_HDR_START_MAC** 21554e1ec56cSDaniel Borkmann * Base offset to load data from is *skb*'s mac header. 21564e1ec56cSDaniel Borkmann * **BPF_HDR_START_NET** 21574e1ec56cSDaniel Borkmann * Base offset to load data from is *skb*'s network header. 21584e1ec56cSDaniel Borkmann * 21594e1ec56cSDaniel Borkmann * In general, "direct packet access" is the preferred method to 21604e1ec56cSDaniel Borkmann * access packet data, however, this helper is in particular useful 21614e1ec56cSDaniel Borkmann * in socket filters where *skb*\ **->data** does not always point 21624e1ec56cSDaniel Borkmann * to the start of the mac header and where "direct packet access" 21634e1ec56cSDaniel Borkmann * is not available. 21644e1ec56cSDaniel Borkmann * Return 21654e1ec56cSDaniel Borkmann * 0 on success, or a negative error in case of failure. 21664e1ec56cSDaniel Borkmann * 2167bdb7b79bSAndrii Nakryiko * long bpf_fib_lookup(void *ctx, struct bpf_fib_lookup *params, int plen, u32 flags) 216887f5fc7eSDavid Ahern * Description 216987f5fc7eSDavid Ahern * Do FIB lookup in kernel tables using parameters in *params*. 217087f5fc7eSDavid Ahern * If lookup is successful and result shows packet is to be 217187f5fc7eSDavid Ahern * forwarded, the neighbor tables are searched for the nexthop. 217287f5fc7eSDavid Ahern * If successful (ie., FIB lookup shows forwarding and nexthop 2173fa898d76SDavid Ahern * is resolved), the nexthop address is returned in ipv4_dst 2174fa898d76SDavid Ahern * or ipv6_dst based on family, smac is set to mac address of 2175fa898d76SDavid Ahern * egress device, dmac is set to nexthop mac address, rt_metric 21764c79579bSDavid Ahern * is set to metric from route (IPv4/IPv6 only), and ifindex 21774c79579bSDavid Ahern * is set to the device index of the nexthop from the FIB lookup. 217887f5fc7eSDavid Ahern * 217987f5fc7eSDavid Ahern * *plen* argument is the size of the passed in struct. 21807a279e93SQuentin Monnet * *flags* argument can be a combination of one or more of the 21817a279e93SQuentin Monnet * following values: 218287f5fc7eSDavid Ahern * 21837a279e93SQuentin Monnet * **BPF_FIB_LOOKUP_DIRECT** 21847a279e93SQuentin Monnet * Do a direct table lookup vs full lookup using FIB 21857a279e93SQuentin Monnet * rules. 21867a279e93SQuentin Monnet * **BPF_FIB_LOOKUP_OUTPUT** 21877a279e93SQuentin Monnet * Perform lookup from an egress perspective (default is 21887a279e93SQuentin Monnet * ingress). 218987f5fc7eSDavid Ahern * 219087f5fc7eSDavid Ahern * *ctx* is either **struct xdp_md** for XDP programs or 219187f5fc7eSDavid Ahern * **struct sk_buff** tc cls_act programs. 219287f5fc7eSDavid Ahern * Return 21934c79579bSDavid Ahern * * < 0 if any input argument is invalid 21944c79579bSDavid Ahern * * 0 on success (packet is forwarded, nexthop neighbor exists) 21954c79579bSDavid Ahern * * > 0 one of **BPF_FIB_LKUP_RET_** codes explaining why the 21962bae79d2SQuentin Monnet * packet is not forwarded or needs assist from full stack 219781110384SJohn Fastabend * 2198bdb7b79bSAndrii Nakryiko * long bpf_sock_hash_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags) 219981110384SJohn Fastabend * Description 220081110384SJohn Fastabend * Add an entry to, or update a sockhash *map* referencing sockets. 220181110384SJohn Fastabend * The *skops* is used as a new value for the entry associated to 220281110384SJohn Fastabend * *key*. *flags* is one of: 220381110384SJohn Fastabend * 220481110384SJohn Fastabend * **BPF_NOEXIST** 220581110384SJohn Fastabend * The entry for *key* must not exist in the map. 220681110384SJohn Fastabend * **BPF_EXIST** 220781110384SJohn Fastabend * The entry for *key* must already exist in the map. 220881110384SJohn Fastabend * **BPF_ANY** 220981110384SJohn Fastabend * No condition on the existence of the entry for *key*. 221081110384SJohn Fastabend * 221181110384SJohn Fastabend * If the *map* has eBPF programs (parser and verdict), those will 221281110384SJohn Fastabend * be inherited by the socket being added. If the socket is 221381110384SJohn Fastabend * already attached to eBPF programs, this results in an error. 221481110384SJohn Fastabend * Return 221581110384SJohn Fastabend * 0 on success, or a negative error in case of failure. 221681110384SJohn Fastabend * 2217bdb7b79bSAndrii Nakryiko * long bpf_msg_redirect_hash(struct sk_msg_buff *msg, struct bpf_map *map, void *key, u64 flags) 221881110384SJohn Fastabend * Description 221981110384SJohn Fastabend * This helper is used in programs implementing policies at the 222081110384SJohn Fastabend * socket level. If the message *msg* is allowed to pass (i.e. if 222181110384SJohn Fastabend * the verdict eBPF program returns **SK_PASS**), redirect it to 222281110384SJohn Fastabend * the socket referenced by *map* (of type 222381110384SJohn Fastabend * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and 222481110384SJohn Fastabend * egress interfaces can be used for redirection. The 222581110384SJohn Fastabend * **BPF_F_INGRESS** value in *flags* is used to make the 222681110384SJohn Fastabend * distinction (ingress path is selected if the flag is present, 222781110384SJohn Fastabend * egress path otherwise). This is the only flag supported for now. 222881110384SJohn Fastabend * Return 222981110384SJohn Fastabend * **SK_PASS** on success, or **SK_DROP** on error. 223081110384SJohn Fastabend * 2231bdb7b79bSAndrii Nakryiko * long bpf_sk_redirect_hash(struct sk_buff *skb, struct bpf_map *map, void *key, u64 flags) 223281110384SJohn Fastabend * Description 223381110384SJohn Fastabend * This helper is used in programs implementing policies at the 223481110384SJohn Fastabend * skb socket level. If the sk_buff *skb* is allowed to pass (i.e. 223581110384SJohn Fastabend * if the verdeict eBPF program returns **SK_PASS**), redirect it 223681110384SJohn Fastabend * to the socket referenced by *map* (of type 223781110384SJohn Fastabend * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and 223881110384SJohn Fastabend * egress interfaces can be used for redirection. The 223981110384SJohn Fastabend * **BPF_F_INGRESS** value in *flags* is used to make the 224081110384SJohn Fastabend * distinction (ingress path is selected if the flag is present, 224181110384SJohn Fastabend * egress otherwise). This is the only flag supported for now. 224281110384SJohn Fastabend * Return 224381110384SJohn Fastabend * **SK_PASS** on success, or **SK_DROP** on error. 2244fe94cc29SMathieu Xhonneux * 2245bdb7b79bSAndrii Nakryiko * long bpf_lwt_push_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) 2246fe94cc29SMathieu Xhonneux * Description 2247fe94cc29SMathieu Xhonneux * Encapsulate the packet associated to *skb* within a Layer 3 2248fe94cc29SMathieu Xhonneux * protocol header. This header is provided in the buffer at 2249fe94cc29SMathieu Xhonneux * address *hdr*, with *len* its size in bytes. *type* indicates 2250fe94cc29SMathieu Xhonneux * the protocol of the header and can be one of: 2251fe94cc29SMathieu Xhonneux * 2252fe94cc29SMathieu Xhonneux * **BPF_LWT_ENCAP_SEG6** 2253fe94cc29SMathieu Xhonneux * IPv6 encapsulation with Segment Routing Header 2254fe94cc29SMathieu Xhonneux * (**struct ipv6_sr_hdr**). *hdr* only contains the SRH, 2255fe94cc29SMathieu Xhonneux * the IPv6 header is computed by the kernel. 2256fe94cc29SMathieu Xhonneux * **BPF_LWT_ENCAP_SEG6_INLINE** 2257fe94cc29SMathieu Xhonneux * Only works if *skb* contains an IPv6 packet. Insert a 2258fe94cc29SMathieu Xhonneux * Segment Routing Header (**struct ipv6_sr_hdr**) inside 2259fe94cc29SMathieu Xhonneux * the IPv6 header. 22603e0bd37cSPeter Oskolkov * **BPF_LWT_ENCAP_IP** 22613e0bd37cSPeter Oskolkov * IP encapsulation (GRE/GUE/IPIP/etc). The outer header 22623e0bd37cSPeter Oskolkov * must be IPv4 or IPv6, followed by zero or more 226380867c5eSQuentin Monnet * additional headers, up to **LWT_BPF_MAX_HEADROOM** 226480867c5eSQuentin Monnet * total bytes in all prepended headers. Please note that 226580867c5eSQuentin Monnet * if **skb_is_gso**\ (*skb*) is true, no more than two 226680867c5eSQuentin Monnet * headers can be prepended, and the inner header, if 226780867c5eSQuentin Monnet * present, should be either GRE or UDP/GUE. 22683e0bd37cSPeter Oskolkov * 226980867c5eSQuentin Monnet * **BPF_LWT_ENCAP_SEG6**\ \* types can be called by BPF programs 227080867c5eSQuentin Monnet * of type **BPF_PROG_TYPE_LWT_IN**; **BPF_LWT_ENCAP_IP** type can 227180867c5eSQuentin Monnet * be called by bpf programs of types **BPF_PROG_TYPE_LWT_IN** and 227280867c5eSQuentin Monnet * **BPF_PROG_TYPE_LWT_XMIT**. 2273fe94cc29SMathieu Xhonneux * 227432e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2275fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2276fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2277fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2278fe94cc29SMathieu Xhonneux * direct packet access. 2279fe94cc29SMathieu Xhonneux * Return 2280fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2281fe94cc29SMathieu Xhonneux * 2282bdb7b79bSAndrii Nakryiko * long bpf_lwt_seg6_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len) 2283fe94cc29SMathieu Xhonneux * Description 2284fe94cc29SMathieu Xhonneux * Store *len* bytes from address *from* into the packet 2285fe94cc29SMathieu Xhonneux * associated to *skb*, at *offset*. Only the flags, tag and TLVs 2286fe94cc29SMathieu Xhonneux * inside the outermost IPv6 Segment Routing Header can be 2287fe94cc29SMathieu Xhonneux * modified through this helper. 2288fe94cc29SMathieu Xhonneux * 228932e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2290fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2291fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2292fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2293fe94cc29SMathieu Xhonneux * direct packet access. 2294fe94cc29SMathieu Xhonneux * Return 2295fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2296fe94cc29SMathieu Xhonneux * 2297bdb7b79bSAndrii Nakryiko * long bpf_lwt_seg6_adjust_srh(struct sk_buff *skb, u32 offset, s32 delta) 2298fe94cc29SMathieu Xhonneux * Description 2299fe94cc29SMathieu Xhonneux * Adjust the size allocated to TLVs in the outermost IPv6 2300fe94cc29SMathieu Xhonneux * Segment Routing Header contained in the packet associated to 2301fe94cc29SMathieu Xhonneux * *skb*, at position *offset* by *delta* bytes. Only offsets 2302fe94cc29SMathieu Xhonneux * after the segments are accepted. *delta* can be as well 2303fe94cc29SMathieu Xhonneux * positive (growing) as negative (shrinking). 2304fe94cc29SMathieu Xhonneux * 230532e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2306fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2307fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2308fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2309fe94cc29SMathieu Xhonneux * direct packet access. 2310fe94cc29SMathieu Xhonneux * Return 2311fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2312fe94cc29SMathieu Xhonneux * 2313bdb7b79bSAndrii Nakryiko * long bpf_lwt_seg6_action(struct sk_buff *skb, u32 action, void *param, u32 param_len) 2314fe94cc29SMathieu Xhonneux * Description 2315fe94cc29SMathieu Xhonneux * Apply an IPv6 Segment Routing action of type *action* to the 2316fe94cc29SMathieu Xhonneux * packet associated to *skb*. Each action takes a parameter 2317fe94cc29SMathieu Xhonneux * contained at address *param*, and of length *param_len* bytes. 2318fe94cc29SMathieu Xhonneux * *action* can be one of: 2319fe94cc29SMathieu Xhonneux * 2320fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_X** 2321fe94cc29SMathieu Xhonneux * End.X action: Endpoint with Layer-3 cross-connect. 2322fe94cc29SMathieu Xhonneux * Type of *param*: **struct in6_addr**. 2323fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_T** 2324fe94cc29SMathieu Xhonneux * End.T action: Endpoint with specific IPv6 table lookup. 2325fe94cc29SMathieu Xhonneux * Type of *param*: **int**. 2326fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_B6** 2327fe94cc29SMathieu Xhonneux * End.B6 action: Endpoint bound to an SRv6 policy. 232880867c5eSQuentin Monnet * Type of *param*: **struct ipv6_sr_hdr**. 2329fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_B6_ENCAP** 2330fe94cc29SMathieu Xhonneux * End.B6.Encap action: Endpoint bound to an SRv6 2331fe94cc29SMathieu Xhonneux * encapsulation policy. 233280867c5eSQuentin Monnet * Type of *param*: **struct ipv6_sr_hdr**. 2333fe94cc29SMathieu Xhonneux * 233432e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2335fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2336fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2337fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2338fe94cc29SMathieu Xhonneux * direct packet access. 2339fe94cc29SMathieu Xhonneux * Return 2340fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2341f4364dcfSSean Young * 2342bdb7b79bSAndrii Nakryiko * long bpf_rc_repeat(void *ctx) 234362369db2SQuentin Monnet * Description 234462369db2SQuentin Monnet * This helper is used in programs implementing IR decoding, to 234562369db2SQuentin Monnet * report a successfully decoded repeat key message. This delays 234662369db2SQuentin Monnet * the generation of a key up event for previously generated 234762369db2SQuentin Monnet * key down event. 234862369db2SQuentin Monnet * 234962369db2SQuentin Monnet * Some IR protocols like NEC have a special IR message for 235062369db2SQuentin Monnet * repeating last button, for when a button is held down. 235162369db2SQuentin Monnet * 235262369db2SQuentin Monnet * The *ctx* should point to the lirc sample as passed into 235362369db2SQuentin Monnet * the program. 235462369db2SQuentin Monnet * 235562369db2SQuentin Monnet * This helper is only available is the kernel was compiled with 235662369db2SQuentin Monnet * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 235762369db2SQuentin Monnet * "**y**". 235862369db2SQuentin Monnet * Return 235962369db2SQuentin Monnet * 0 236062369db2SQuentin Monnet * 2361bdb7b79bSAndrii Nakryiko * long bpf_rc_keydown(void *ctx, u32 protocol, u64 scancode, u32 toggle) 2362f4364dcfSSean Young * Description 2363f4364dcfSSean Young * This helper is used in programs implementing IR decoding, to 2364f4364dcfSSean Young * report a successfully decoded key press with *scancode*, 2365f4364dcfSSean Young * *toggle* value in the given *protocol*. The scancode will be 2366f4364dcfSSean Young * translated to a keycode using the rc keymap, and reported as 2367f4364dcfSSean Young * an input key down event. After a period a key up event is 2368f4364dcfSSean Young * generated. This period can be extended by calling either 236990b1023fSQuentin Monnet * **bpf_rc_keydown**\ () again with the same values, or calling 237090b1023fSQuentin Monnet * **bpf_rc_repeat**\ (). 2371f4364dcfSSean Young * 2372f4364dcfSSean Young * Some protocols include a toggle bit, in case the button was 2373f4364dcfSSean Young * released and pressed again between consecutive scancodes. 2374f4364dcfSSean Young * 2375f4364dcfSSean Young * The *ctx* should point to the lirc sample as passed into 2376f4364dcfSSean Young * the program. 2377f4364dcfSSean Young * 2378f4364dcfSSean Young * The *protocol* is the decoded protocol number (see 2379f4364dcfSSean Young * **enum rc_proto** for some predefined values). 2380f4364dcfSSean Young * 2381f4364dcfSSean Young * This helper is only available is the kernel was compiled with 2382f4364dcfSSean Young * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 2383f4364dcfSSean Young * "**y**". 2384f4364dcfSSean Young * Return 2385f4364dcfSSean Young * 0 2386f4364dcfSSean Young * 238762369db2SQuentin Monnet * u64 bpf_skb_cgroup_id(struct sk_buff *skb) 2388cb20b08eSDaniel Borkmann * Description 2389cb20b08eSDaniel Borkmann * Return the cgroup v2 id of the socket associated with the *skb*. 2390cb20b08eSDaniel Borkmann * This is roughly similar to the **bpf_get_cgroup_classid**\ () 2391cb20b08eSDaniel Borkmann * helper for cgroup v1 by providing a tag resp. identifier that 2392cb20b08eSDaniel Borkmann * can be matched on or used for map lookups e.g. to implement 2393cb20b08eSDaniel Borkmann * policy. The cgroup v2 id of a given path in the hierarchy is 2394cb20b08eSDaniel Borkmann * exposed in user space through the f_handle API in order to get 2395cb20b08eSDaniel Borkmann * to the same 64-bit id. 2396cb20b08eSDaniel Borkmann * 2397cb20b08eSDaniel Borkmann * This helper can be used on TC egress path, but not on ingress, 2398cb20b08eSDaniel Borkmann * and is available only if the kernel was compiled with the 2399cb20b08eSDaniel Borkmann * **CONFIG_SOCK_CGROUP_DATA** configuration option. 2400cb20b08eSDaniel Borkmann * Return 2401cb20b08eSDaniel Borkmann * The id is returned or 0 in case the id could not be retrieved. 2402bf6fa2c8SYonghong Song * 2403bf6fa2c8SYonghong Song * u64 bpf_get_current_cgroup_id(void) 2404bf6fa2c8SYonghong Song * Return 2405bf6fa2c8SYonghong Song * A 64-bit integer containing the current cgroup id based 2406bf6fa2c8SYonghong Song * on the cgroup within which the current task is running. 2407cd339431SRoman Gushchin * 240862369db2SQuentin Monnet * void *bpf_get_local_storage(void *map, u64 flags) 2409cd339431SRoman Gushchin * Description 2410cd339431SRoman Gushchin * Get the pointer to the local storage area. 2411cd339431SRoman Gushchin * The type and the size of the local storage is defined 2412cd339431SRoman Gushchin * by the *map* argument. 2413cd339431SRoman Gushchin * The *flags* meaning is specific for each map type, 2414cd339431SRoman Gushchin * and has to be 0 for cgroup local storage. 2415cd339431SRoman Gushchin * 241690b1023fSQuentin Monnet * Depending on the BPF program type, a local storage area 241790b1023fSQuentin Monnet * can be shared between multiple instances of the BPF program, 2418cd339431SRoman Gushchin * running simultaneously. 2419cd339431SRoman Gushchin * 2420cd339431SRoman Gushchin * A user should care about the synchronization by himself. 242190b1023fSQuentin Monnet * For example, by using the **BPF_STX_XADD** instruction to alter 2422cd339431SRoman Gushchin * the shared data. 2423cd339431SRoman Gushchin * Return 242490b1023fSQuentin Monnet * A pointer to the local storage area. 24252dbb9b9eSMartin KaFai Lau * 2426bdb7b79bSAndrii Nakryiko * long bpf_sk_select_reuseport(struct sk_reuseport_md *reuse, struct bpf_map *map, void *key, u64 flags) 24272dbb9b9eSMartin KaFai Lau * Description 242890b1023fSQuentin Monnet * Select a **SO_REUSEPORT** socket from a 242990b1023fSQuentin Monnet * **BPF_MAP_TYPE_REUSEPORT_ARRAY** *map*. 243090b1023fSQuentin Monnet * It checks the selected socket is matching the incoming 243190b1023fSQuentin Monnet * request in the socket buffer. 24322dbb9b9eSMartin KaFai Lau * Return 24332dbb9b9eSMartin KaFai Lau * 0 on success, or a negative error in case of failure. 24346acc9b43SJoe Stringer * 243562369db2SQuentin Monnet * u64 bpf_skb_ancestor_cgroup_id(struct sk_buff *skb, int ancestor_level) 243662369db2SQuentin Monnet * Description 243762369db2SQuentin Monnet * Return id of cgroup v2 that is ancestor of cgroup associated 243862369db2SQuentin Monnet * with the *skb* at the *ancestor_level*. The root cgroup is at 243962369db2SQuentin Monnet * *ancestor_level* zero and each step down the hierarchy 244062369db2SQuentin Monnet * increments the level. If *ancestor_level* == level of cgroup 244162369db2SQuentin Monnet * associated with *skb*, then return value will be same as that 244262369db2SQuentin Monnet * of **bpf_skb_cgroup_id**\ (). 244362369db2SQuentin Monnet * 244462369db2SQuentin Monnet * The helper is useful to implement policies based on cgroups 244562369db2SQuentin Monnet * that are upper in hierarchy than immediate cgroup associated 244662369db2SQuentin Monnet * with *skb*. 244762369db2SQuentin Monnet * 244862369db2SQuentin Monnet * The format of returned id and helper limitations are same as in 244962369db2SQuentin Monnet * **bpf_skb_cgroup_id**\ (). 245062369db2SQuentin Monnet * Return 245162369db2SQuentin Monnet * The id is returned or 0 in case the id could not be retrieved. 245262369db2SQuentin Monnet * 2453f71c6143SJoe Stringer * struct bpf_sock *bpf_sk_lookup_tcp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 24546acc9b43SJoe Stringer * Description 24556acc9b43SJoe Stringer * Look for TCP socket matching *tuple*, optionally in a child 24566acc9b43SJoe Stringer * network namespace *netns*. The return value must be checked, 245790b1023fSQuentin Monnet * and if non-**NULL**, released via **bpf_sk_release**\ (). 24586acc9b43SJoe Stringer * 24596acc9b43SJoe Stringer * The *ctx* should point to the context of the program, such as 24606acc9b43SJoe Stringer * the skb or socket (depending on the hook in use). This is used 24616acc9b43SJoe Stringer * to determine the base network namespace for the lookup. 24626acc9b43SJoe Stringer * 24636acc9b43SJoe Stringer * *tuple_size* must be one of: 24646acc9b43SJoe Stringer * 24656acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv4**) 24666acc9b43SJoe Stringer * Look for an IPv4 socket. 24676acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv6**) 24686acc9b43SJoe Stringer * Look for an IPv6 socket. 24696acc9b43SJoe Stringer * 2470f71c6143SJoe Stringer * If the *netns* is a negative signed 32-bit integer, then the 2471bfdfa517SRandy Dunlap * socket lookup table in the netns associated with the *ctx* 2472f71c6143SJoe Stringer * will be used. For the TC hooks, this is the netns of the device 2473f71c6143SJoe Stringer * in the skb. For socket hooks, this is the netns of the socket. 2474f71c6143SJoe Stringer * If *netns* is any other signed 32-bit value greater than or 2475f71c6143SJoe Stringer * equal to zero then it specifies the ID of the netns relative to 2476f71c6143SJoe Stringer * the netns associated with the *ctx*. *netns* values beyond the 2477f71c6143SJoe Stringer * range of 32-bit integers are reserved for future use. 24786acc9b43SJoe Stringer * 24796acc9b43SJoe Stringer * All values for *flags* are reserved for future usage, and must 24806acc9b43SJoe Stringer * be left at zero. 24816acc9b43SJoe Stringer * 24826acc9b43SJoe Stringer * This helper is available only if the kernel was compiled with 24836acc9b43SJoe Stringer * **CONFIG_NET** configuration option. 24846acc9b43SJoe Stringer * Return 24850bd72117SDaniel Borkmann * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 24860bd72117SDaniel Borkmann * For sockets with reuseport option, the **struct bpf_sock** 248780867c5eSQuentin Monnet * result is from *reuse*\ **->socks**\ [] using the hash of the 248880867c5eSQuentin Monnet * tuple. 24896acc9b43SJoe Stringer * 2490f71c6143SJoe Stringer * struct bpf_sock *bpf_sk_lookup_udp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 24916acc9b43SJoe Stringer * Description 24926acc9b43SJoe Stringer * Look for UDP socket matching *tuple*, optionally in a child 24936acc9b43SJoe Stringer * network namespace *netns*. The return value must be checked, 249490b1023fSQuentin Monnet * and if non-**NULL**, released via **bpf_sk_release**\ (). 24956acc9b43SJoe Stringer * 24966acc9b43SJoe Stringer * The *ctx* should point to the context of the program, such as 24976acc9b43SJoe Stringer * the skb or socket (depending on the hook in use). This is used 24986acc9b43SJoe Stringer * to determine the base network namespace for the lookup. 24996acc9b43SJoe Stringer * 25006acc9b43SJoe Stringer * *tuple_size* must be one of: 25016acc9b43SJoe Stringer * 25026acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv4**) 25036acc9b43SJoe Stringer * Look for an IPv4 socket. 25046acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv6**) 25056acc9b43SJoe Stringer * Look for an IPv6 socket. 25066acc9b43SJoe Stringer * 2507f71c6143SJoe Stringer * If the *netns* is a negative signed 32-bit integer, then the 2508bfdfa517SRandy Dunlap * socket lookup table in the netns associated with the *ctx* 2509f71c6143SJoe Stringer * will be used. For the TC hooks, this is the netns of the device 2510f71c6143SJoe Stringer * in the skb. For socket hooks, this is the netns of the socket. 2511f71c6143SJoe Stringer * If *netns* is any other signed 32-bit value greater than or 2512f71c6143SJoe Stringer * equal to zero then it specifies the ID of the netns relative to 2513f71c6143SJoe Stringer * the netns associated with the *ctx*. *netns* values beyond the 2514f71c6143SJoe Stringer * range of 32-bit integers are reserved for future use. 25156acc9b43SJoe Stringer * 25166acc9b43SJoe Stringer * All values for *flags* are reserved for future usage, and must 25176acc9b43SJoe Stringer * be left at zero. 25186acc9b43SJoe Stringer * 25196acc9b43SJoe Stringer * This helper is available only if the kernel was compiled with 25206acc9b43SJoe Stringer * **CONFIG_NET** configuration option. 25216acc9b43SJoe Stringer * Return 25220bd72117SDaniel Borkmann * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 25230bd72117SDaniel Borkmann * For sockets with reuseport option, the **struct bpf_sock** 252480867c5eSQuentin Monnet * result is from *reuse*\ **->socks**\ [] using the hash of the 252580867c5eSQuentin Monnet * tuple. 25266acc9b43SJoe Stringer * 2527a5fa25adSMartin KaFai Lau * long bpf_sk_release(void *sock) 25286acc9b43SJoe Stringer * Description 252990b1023fSQuentin Monnet * Release the reference held by *sock*. *sock* must be a 253090b1023fSQuentin Monnet * non-**NULL** pointer that was returned from 253190b1023fSQuentin Monnet * **bpf_sk_lookup_xxx**\ (). 253290b1023fSQuentin Monnet * Return 253390b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 253490b1023fSQuentin Monnet * 2535bdb7b79bSAndrii Nakryiko * long bpf_map_push_elem(struct bpf_map *map, const void *value, u64 flags) 253662369db2SQuentin Monnet * Description 253762369db2SQuentin Monnet * Push an element *value* in *map*. *flags* is one of: 253862369db2SQuentin Monnet * 253962369db2SQuentin Monnet * **BPF_EXIST** 254062369db2SQuentin Monnet * If the queue/stack is full, the oldest element is 254162369db2SQuentin Monnet * removed to make room for this. 254262369db2SQuentin Monnet * Return 254362369db2SQuentin Monnet * 0 on success, or a negative error in case of failure. 254462369db2SQuentin Monnet * 2545bdb7b79bSAndrii Nakryiko * long bpf_map_pop_elem(struct bpf_map *map, void *value) 254690b1023fSQuentin Monnet * Description 254790b1023fSQuentin Monnet * Pop an element from *map*. 254890b1023fSQuentin Monnet * Return 254990b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 255090b1023fSQuentin Monnet * 2551bdb7b79bSAndrii Nakryiko * long bpf_map_peek_elem(struct bpf_map *map, void *value) 255290b1023fSQuentin Monnet * Description 255390b1023fSQuentin Monnet * Get an element from *map* without removing it. 25546acc9b43SJoe Stringer * Return 25556acc9b43SJoe Stringer * 0 on success, or a negative error in case of failure. 25566fff607eSJohn Fastabend * 2557bdb7b79bSAndrii Nakryiko * long bpf_msg_push_data(struct sk_msg_buff *msg, u32 start, u32 len, u64 flags) 25586fff607eSJohn Fastabend * Description 255990b1023fSQuentin Monnet * For socket policies, insert *len* bytes into *msg* at offset 25606fff607eSJohn Fastabend * *start*. 25616fff607eSJohn Fastabend * 25626fff607eSJohn Fastabend * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a 256390b1023fSQuentin Monnet * *msg* it may want to insert metadata or options into the *msg*. 25646fff607eSJohn Fastabend * This can later be read and used by any of the lower layer BPF 25656fff607eSJohn Fastabend * hooks. 25666fff607eSJohn Fastabend * 25676fff607eSJohn Fastabend * This helper may fail if under memory pressure (a malloc 25686fff607eSJohn Fastabend * fails) in these cases BPF programs will get an appropriate 25696fff607eSJohn Fastabend * error and BPF programs will need to handle them. 25706fff607eSJohn Fastabend * Return 25716fff607eSJohn Fastabend * 0 on success, or a negative error in case of failure. 25727246d8edSJohn Fastabend * 2573bdb7b79bSAndrii Nakryiko * long bpf_msg_pop_data(struct sk_msg_buff *msg, u32 start, u32 len, u64 flags) 25747246d8edSJohn Fastabend * Description 25755f0e5412SAndrii Nakryiko * Will remove *len* bytes from a *msg* starting at byte *start*. 25767246d8edSJohn Fastabend * This may result in **ENOMEM** errors under certain situations if 25777246d8edSJohn Fastabend * an allocation and copy are required due to a full ring buffer. 25787246d8edSJohn Fastabend * However, the helper will try to avoid doing the allocation 25797246d8edSJohn Fastabend * if possible. Other errors can occur if input parameters are 258090b1023fSQuentin Monnet * invalid either due to *start* byte not being valid part of *msg* 25817246d8edSJohn Fastabend * payload and/or *pop* value being to large. 25827246d8edSJohn Fastabend * Return 258390b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 258401d3240aSSean Young * 2585bdb7b79bSAndrii Nakryiko * long bpf_rc_pointer_rel(void *ctx, s32 rel_x, s32 rel_y) 258601d3240aSSean Young * Description 258701d3240aSSean Young * This helper is used in programs implementing IR decoding, to 258801d3240aSSean Young * report a successfully decoded pointer movement. 258901d3240aSSean Young * 259001d3240aSSean Young * The *ctx* should point to the lirc sample as passed into 259101d3240aSSean Young * the program. 259201d3240aSSean Young * 259301d3240aSSean Young * This helper is only available is the kernel was compiled with 259401d3240aSSean Young * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 259501d3240aSSean Young * "**y**". 259601d3240aSSean Young * Return 259701d3240aSSean Young * 0 259846f8bc92SMartin KaFai Lau * 2599bdb7b79bSAndrii Nakryiko * long bpf_spin_lock(struct bpf_spin_lock *lock) 26000eb09785SQuentin Monnet * Description 26010eb09785SQuentin Monnet * Acquire a spinlock represented by the pointer *lock*, which is 26020eb09785SQuentin Monnet * stored as part of a value of a map. Taking the lock allows to 26030eb09785SQuentin Monnet * safely update the rest of the fields in that value. The 26040eb09785SQuentin Monnet * spinlock can (and must) later be released with a call to 26050eb09785SQuentin Monnet * **bpf_spin_unlock**\ (\ *lock*\ ). 26060eb09785SQuentin Monnet * 26070eb09785SQuentin Monnet * Spinlocks in BPF programs come with a number of restrictions 26080eb09785SQuentin Monnet * and constraints: 26090eb09785SQuentin Monnet * 26100eb09785SQuentin Monnet * * **bpf_spin_lock** objects are only allowed inside maps of 26110eb09785SQuentin Monnet * types **BPF_MAP_TYPE_HASH** and **BPF_MAP_TYPE_ARRAY** (this 26120eb09785SQuentin Monnet * list could be extended in the future). 26130eb09785SQuentin Monnet * * BTF description of the map is mandatory. 26140eb09785SQuentin Monnet * * The BPF program can take ONE lock at a time, since taking two 26150eb09785SQuentin Monnet * or more could cause dead locks. 26160eb09785SQuentin Monnet * * Only one **struct bpf_spin_lock** is allowed per map element. 26170eb09785SQuentin Monnet * * When the lock is taken, calls (either BPF to BPF or helpers) 26180eb09785SQuentin Monnet * are not allowed. 26190eb09785SQuentin Monnet * * The **BPF_LD_ABS** and **BPF_LD_IND** instructions are not 26200eb09785SQuentin Monnet * allowed inside a spinlock-ed region. 26210eb09785SQuentin Monnet * * The BPF program MUST call **bpf_spin_unlock**\ () to release 26220eb09785SQuentin Monnet * the lock, on all execution paths, before it returns. 26230eb09785SQuentin Monnet * * The BPF program can access **struct bpf_spin_lock** only via 26240eb09785SQuentin Monnet * the **bpf_spin_lock**\ () and **bpf_spin_unlock**\ () 26250eb09785SQuentin Monnet * helpers. Loading or storing data into the **struct 26260eb09785SQuentin Monnet * bpf_spin_lock** *lock*\ **;** field of a map is not allowed. 26270eb09785SQuentin Monnet * * To use the **bpf_spin_lock**\ () helper, the BTF description 26280eb09785SQuentin Monnet * of the map value must be a struct and have **struct 26290eb09785SQuentin Monnet * bpf_spin_lock** *anyname*\ **;** field at the top level. 26300eb09785SQuentin Monnet * Nested lock inside another struct is not allowed. 26310eb09785SQuentin Monnet * * The **struct bpf_spin_lock** *lock* field in a map value must 26320eb09785SQuentin Monnet * be aligned on a multiple of 4 bytes in that value. 26330eb09785SQuentin Monnet * * Syscall with command **BPF_MAP_LOOKUP_ELEM** does not copy 26340eb09785SQuentin Monnet * the **bpf_spin_lock** field to user space. 26350eb09785SQuentin Monnet * * Syscall with command **BPF_MAP_UPDATE_ELEM**, or update from 26360eb09785SQuentin Monnet * a BPF program, do not update the **bpf_spin_lock** field. 26370eb09785SQuentin Monnet * * **bpf_spin_lock** cannot be on the stack or inside a 26380eb09785SQuentin Monnet * networking packet (it can only be inside of a map values). 26390eb09785SQuentin Monnet * * **bpf_spin_lock** is available to root only. 26400eb09785SQuentin Monnet * * Tracing programs and socket filter programs cannot use 26410eb09785SQuentin Monnet * **bpf_spin_lock**\ () due to insufficient preemption checks 26420eb09785SQuentin Monnet * (but this may change in the future). 26430eb09785SQuentin Monnet * * **bpf_spin_lock** is not allowed in inner maps of map-in-map. 26440eb09785SQuentin Monnet * Return 26450eb09785SQuentin Monnet * 0 26460eb09785SQuentin Monnet * 2647bdb7b79bSAndrii Nakryiko * long bpf_spin_unlock(struct bpf_spin_lock *lock) 26480eb09785SQuentin Monnet * Description 26490eb09785SQuentin Monnet * Release the *lock* previously locked by a call to 26500eb09785SQuentin Monnet * **bpf_spin_lock**\ (\ *lock*\ ). 26510eb09785SQuentin Monnet * Return 26520eb09785SQuentin Monnet * 0 26530eb09785SQuentin Monnet * 265446f8bc92SMartin KaFai Lau * struct bpf_sock *bpf_sk_fullsock(struct bpf_sock *sk) 265546f8bc92SMartin KaFai Lau * Description 265646f8bc92SMartin KaFai Lau * This helper gets a **struct bpf_sock** pointer such 265762369db2SQuentin Monnet * that all the fields in this **bpf_sock** can be accessed. 265846f8bc92SMartin KaFai Lau * Return 265962369db2SQuentin Monnet * A **struct bpf_sock** pointer on success, or **NULL** in 266046f8bc92SMartin KaFai Lau * case of failure. 2661655a51e5SMartin KaFai Lau * 2662655a51e5SMartin KaFai Lau * struct bpf_tcp_sock *bpf_tcp_sock(struct bpf_sock *sk) 2663655a51e5SMartin KaFai Lau * Description 2664655a51e5SMartin KaFai Lau * This helper gets a **struct bpf_tcp_sock** pointer from a 2665655a51e5SMartin KaFai Lau * **struct bpf_sock** pointer. 2666655a51e5SMartin KaFai Lau * Return 266762369db2SQuentin Monnet * A **struct bpf_tcp_sock** pointer on success, or **NULL** in 2668655a51e5SMartin KaFai Lau * case of failure. 2669f7c917baSbrakmo * 2670bdb7b79bSAndrii Nakryiko * long bpf_skb_ecn_set_ce(struct sk_buff *skb) 2671f7c917baSbrakmo * Description 267262369db2SQuentin Monnet * Set ECN (Explicit Congestion Notification) field of IP header 267362369db2SQuentin Monnet * to **CE** (Congestion Encountered) if current value is **ECT** 267462369db2SQuentin Monnet * (ECN Capable Transport). Otherwise, do nothing. Works with IPv6 267562369db2SQuentin Monnet * and IPv4. 2676f7c917baSbrakmo * Return 267762369db2SQuentin Monnet * 1 if the **CE** flag is set (either by the current helper call 267862369db2SQuentin Monnet * or because it was already present), 0 if it is not set. 2679dbafd7ddSMartin KaFai Lau * 2680dbafd7ddSMartin KaFai Lau * struct bpf_sock *bpf_get_listener_sock(struct bpf_sock *sk) 2681dbafd7ddSMartin KaFai Lau * Description 268262369db2SQuentin Monnet * Return a **struct bpf_sock** pointer in **TCP_LISTEN** state. 268362369db2SQuentin Monnet * **bpf_sk_release**\ () is unnecessary and not allowed. 2684dbafd7ddSMartin KaFai Lau * Return 268562369db2SQuentin Monnet * A **struct bpf_sock** pointer on success, or **NULL** in 2686dbafd7ddSMartin KaFai Lau * case of failure. 2687edbf8c01SLorenz Bauer * 2688edbf8c01SLorenz Bauer * struct bpf_sock *bpf_skc_lookup_tcp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 2689edbf8c01SLorenz Bauer * Description 2690edbf8c01SLorenz Bauer * Look for TCP socket matching *tuple*, optionally in a child 2691edbf8c01SLorenz Bauer * network namespace *netns*. The return value must be checked, 2692edbf8c01SLorenz Bauer * and if non-**NULL**, released via **bpf_sk_release**\ (). 2693edbf8c01SLorenz Bauer * 269480867c5eSQuentin Monnet * This function is identical to **bpf_sk_lookup_tcp**\ (), except 269580867c5eSQuentin Monnet * that it also returns timewait or request sockets. Use 269680867c5eSQuentin Monnet * **bpf_sk_fullsock**\ () or **bpf_tcp_sock**\ () to access the 269780867c5eSQuentin Monnet * full structure. 2698edbf8c01SLorenz Bauer * 2699edbf8c01SLorenz Bauer * This helper is available only if the kernel was compiled with 2700edbf8c01SLorenz Bauer * **CONFIG_NET** configuration option. 2701edbf8c01SLorenz Bauer * Return 2702edbf8c01SLorenz Bauer * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 2703edbf8c01SLorenz Bauer * For sockets with reuseport option, the **struct bpf_sock** 270480867c5eSQuentin Monnet * result is from *reuse*\ **->socks**\ [] using the hash of the 270580867c5eSQuentin Monnet * tuple. 270639904084SLorenz Bauer * 2707c0df236eSMartin KaFai Lau * long bpf_tcp_check_syncookie(void *sk, void *iph, u32 iph_len, struct tcphdr *th, u32 th_len) 270839904084SLorenz Bauer * Description 270980867c5eSQuentin Monnet * Check whether *iph* and *th* contain a valid SYN cookie ACK for 271080867c5eSQuentin Monnet * the listening socket in *sk*. 271139904084SLorenz Bauer * 271280867c5eSQuentin Monnet * *iph* points to the start of the IPv4 or IPv6 header, while 271380867c5eSQuentin Monnet * *iph_len* contains **sizeof**\ (**struct iphdr**) or 271480867c5eSQuentin Monnet * **sizeof**\ (**struct ip6hdr**). 271539904084SLorenz Bauer * 271680867c5eSQuentin Monnet * *th* points to the start of the TCP header, while *th_len* 271780867c5eSQuentin Monnet * contains **sizeof**\ (**struct tcphdr**). 271839904084SLorenz Bauer * Return 271980867c5eSQuentin Monnet * 0 if *iph* and *th* are a valid SYN cookie ACK, or a negative 272080867c5eSQuentin Monnet * error otherwise. 2721808649fbSAndrey Ignatov * 2722bdb7b79bSAndrii Nakryiko * long bpf_sysctl_get_name(struct bpf_sysctl *ctx, char *buf, size_t buf_len, u64 flags) 2723808649fbSAndrey Ignatov * Description 2724808649fbSAndrey Ignatov * Get name of sysctl in /proc/sys/ and copy it into provided by 2725808649fbSAndrey Ignatov * program buffer *buf* of size *buf_len*. 2726808649fbSAndrey Ignatov * 2727808649fbSAndrey Ignatov * The buffer is always NUL terminated, unless it's zero-sized. 2728808649fbSAndrey Ignatov * 2729808649fbSAndrey Ignatov * If *flags* is zero, full name (e.g. "net/ipv4/tcp_mem") is 2730808649fbSAndrey Ignatov * copied. Use **BPF_F_SYSCTL_BASE_NAME** flag to copy base name 2731808649fbSAndrey Ignatov * only (e.g. "tcp_mem"). 2732808649fbSAndrey Ignatov * Return 2733808649fbSAndrey Ignatov * Number of character copied (not including the trailing NUL). 2734808649fbSAndrey Ignatov * 2735808649fbSAndrey Ignatov * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 2736808649fbSAndrey Ignatov * truncated name in this case). 27371d11b301SAndrey Ignatov * 2738bdb7b79bSAndrii Nakryiko * long bpf_sysctl_get_current_value(struct bpf_sysctl *ctx, char *buf, size_t buf_len) 27391d11b301SAndrey Ignatov * Description 27401d11b301SAndrey Ignatov * Get current value of sysctl as it is presented in /proc/sys 27411d11b301SAndrey Ignatov * (incl. newline, etc), and copy it as a string into provided 27421d11b301SAndrey Ignatov * by program buffer *buf* of size *buf_len*. 27431d11b301SAndrey Ignatov * 27441d11b301SAndrey Ignatov * The whole value is copied, no matter what file position user 27451d11b301SAndrey Ignatov * space issued e.g. sys_read at. 27461d11b301SAndrey Ignatov * 27471d11b301SAndrey Ignatov * The buffer is always NUL terminated, unless it's zero-sized. 27481d11b301SAndrey Ignatov * Return 27491d11b301SAndrey Ignatov * Number of character copied (not including the trailing NUL). 27501d11b301SAndrey Ignatov * 27511d11b301SAndrey Ignatov * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 27521d11b301SAndrey Ignatov * truncated name in this case). 27531d11b301SAndrey Ignatov * 27541d11b301SAndrey Ignatov * **-EINVAL** if current value was unavailable, e.g. because 27551d11b301SAndrey Ignatov * sysctl is uninitialized and read returns -EIO for it. 27564e63acdfSAndrey Ignatov * 2757bdb7b79bSAndrii Nakryiko * long bpf_sysctl_get_new_value(struct bpf_sysctl *ctx, char *buf, size_t buf_len) 27584e63acdfSAndrey Ignatov * Description 27594e63acdfSAndrey Ignatov * Get new value being written by user space to sysctl (before 27604e63acdfSAndrey Ignatov * the actual write happens) and copy it as a string into 27614e63acdfSAndrey Ignatov * provided by program buffer *buf* of size *buf_len*. 27624e63acdfSAndrey Ignatov * 27634e63acdfSAndrey Ignatov * User space may write new value at file position > 0. 27644e63acdfSAndrey Ignatov * 27654e63acdfSAndrey Ignatov * The buffer is always NUL terminated, unless it's zero-sized. 27664e63acdfSAndrey Ignatov * Return 27674e63acdfSAndrey Ignatov * Number of character copied (not including the trailing NUL). 27684e63acdfSAndrey Ignatov * 27694e63acdfSAndrey Ignatov * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 27704e63acdfSAndrey Ignatov * truncated name in this case). 27714e63acdfSAndrey Ignatov * 27724e63acdfSAndrey Ignatov * **-EINVAL** if sysctl is being read. 27734e63acdfSAndrey Ignatov * 2774bdb7b79bSAndrii Nakryiko * long bpf_sysctl_set_new_value(struct bpf_sysctl *ctx, const char *buf, size_t buf_len) 27754e63acdfSAndrey Ignatov * Description 27764e63acdfSAndrey Ignatov * Override new value being written by user space to sysctl with 27774e63acdfSAndrey Ignatov * value provided by program in buffer *buf* of size *buf_len*. 27784e63acdfSAndrey Ignatov * 27794e63acdfSAndrey Ignatov * *buf* should contain a string in same form as provided by user 27804e63acdfSAndrey Ignatov * space on sysctl write. 27814e63acdfSAndrey Ignatov * 27824e63acdfSAndrey Ignatov * User space may write new value at file position > 0. To override 27834e63acdfSAndrey Ignatov * the whole sysctl value file position should be set to zero. 27844e63acdfSAndrey Ignatov * Return 27854e63acdfSAndrey Ignatov * 0 on success. 27864e63acdfSAndrey Ignatov * 27874e63acdfSAndrey Ignatov * **-E2BIG** if the *buf_len* is too big. 27884e63acdfSAndrey Ignatov * 27894e63acdfSAndrey Ignatov * **-EINVAL** if sysctl is being read. 2790d7a4cb9bSAndrey Ignatov * 2791bdb7b79bSAndrii Nakryiko * long bpf_strtol(const char *buf, size_t buf_len, u64 flags, long *res) 2792d7a4cb9bSAndrey Ignatov * Description 2793d7a4cb9bSAndrey Ignatov * Convert the initial part of the string from buffer *buf* of 2794d7a4cb9bSAndrey Ignatov * size *buf_len* to a long integer according to the given base 2795d7a4cb9bSAndrey Ignatov * and save the result in *res*. 2796d7a4cb9bSAndrey Ignatov * 2797d7a4cb9bSAndrey Ignatov * The string may begin with an arbitrary amount of white space 279880867c5eSQuentin Monnet * (as determined by **isspace**\ (3)) followed by a single 279980867c5eSQuentin Monnet * optional '**-**' sign. 2800d7a4cb9bSAndrey Ignatov * 2801d7a4cb9bSAndrey Ignatov * Five least significant bits of *flags* encode base, other bits 2802d7a4cb9bSAndrey Ignatov * are currently unused. 2803d7a4cb9bSAndrey Ignatov * 2804d7a4cb9bSAndrey Ignatov * Base must be either 8, 10, 16 or 0 to detect it automatically 280580867c5eSQuentin Monnet * similar to user space **strtol**\ (3). 2806d7a4cb9bSAndrey Ignatov * Return 2807d7a4cb9bSAndrey Ignatov * Number of characters consumed on success. Must be positive but 280880867c5eSQuentin Monnet * no more than *buf_len*. 2809d7a4cb9bSAndrey Ignatov * 2810d7a4cb9bSAndrey Ignatov * **-EINVAL** if no valid digits were found or unsupported base 2811d7a4cb9bSAndrey Ignatov * was provided. 2812d7a4cb9bSAndrey Ignatov * 2813d7a4cb9bSAndrey Ignatov * **-ERANGE** if resulting value was out of range. 2814d7a4cb9bSAndrey Ignatov * 2815bdb7b79bSAndrii Nakryiko * long bpf_strtoul(const char *buf, size_t buf_len, u64 flags, unsigned long *res) 2816d7a4cb9bSAndrey Ignatov * Description 2817d7a4cb9bSAndrey Ignatov * Convert the initial part of the string from buffer *buf* of 2818d7a4cb9bSAndrey Ignatov * size *buf_len* to an unsigned long integer according to the 2819d7a4cb9bSAndrey Ignatov * given base and save the result in *res*. 2820d7a4cb9bSAndrey Ignatov * 2821d7a4cb9bSAndrey Ignatov * The string may begin with an arbitrary amount of white space 282280867c5eSQuentin Monnet * (as determined by **isspace**\ (3)). 2823d7a4cb9bSAndrey Ignatov * 2824d7a4cb9bSAndrey Ignatov * Five least significant bits of *flags* encode base, other bits 2825d7a4cb9bSAndrey Ignatov * are currently unused. 2826d7a4cb9bSAndrey Ignatov * 2827d7a4cb9bSAndrey Ignatov * Base must be either 8, 10, 16 or 0 to detect it automatically 282880867c5eSQuentin Monnet * similar to user space **strtoul**\ (3). 2829d7a4cb9bSAndrey Ignatov * Return 2830d7a4cb9bSAndrey Ignatov * Number of characters consumed on success. Must be positive but 283180867c5eSQuentin Monnet * no more than *buf_len*. 2832d7a4cb9bSAndrey Ignatov * 2833d7a4cb9bSAndrey Ignatov * **-EINVAL** if no valid digits were found or unsupported base 2834d7a4cb9bSAndrey Ignatov * was provided. 2835d7a4cb9bSAndrey Ignatov * 2836d7a4cb9bSAndrey Ignatov * **-ERANGE** if resulting value was out of range. 28376ac99e8fSMartin KaFai Lau * 283830897832SKP Singh * void *bpf_sk_storage_get(struct bpf_map *map, void *sk, void *value, u64 flags) 28396ac99e8fSMartin KaFai Lau * Description 284080867c5eSQuentin Monnet * Get a bpf-local-storage from a *sk*. 28416ac99e8fSMartin KaFai Lau * 28426ac99e8fSMartin KaFai Lau * Logically, it could be thought of getting the value from 28436ac99e8fSMartin KaFai Lau * a *map* with *sk* as the **key**. From this 28446ac99e8fSMartin KaFai Lau * perspective, the usage is not much different from 284580867c5eSQuentin Monnet * **bpf_map_lookup_elem**\ (*map*, **&**\ *sk*) except this 284680867c5eSQuentin Monnet * helper enforces the key must be a full socket and the map must 284780867c5eSQuentin Monnet * be a **BPF_MAP_TYPE_SK_STORAGE** also. 28486ac99e8fSMartin KaFai Lau * 28496ac99e8fSMartin KaFai Lau * Underneath, the value is stored locally at *sk* instead of 285080867c5eSQuentin Monnet * the *map*. The *map* is used as the bpf-local-storage 285180867c5eSQuentin Monnet * "type". The bpf-local-storage "type" (i.e. the *map*) is 285280867c5eSQuentin Monnet * searched against all bpf-local-storages residing at *sk*. 28536ac99e8fSMartin KaFai Lau * 285430897832SKP Singh * *sk* is a kernel **struct sock** pointer for LSM program. 285530897832SKP Singh * *sk* is a **struct bpf_sock** pointer for other program types. 285630897832SKP Singh * 285780867c5eSQuentin Monnet * An optional *flags* (**BPF_SK_STORAGE_GET_F_CREATE**) can be 28586ac99e8fSMartin KaFai Lau * used such that a new bpf-local-storage will be 28596ac99e8fSMartin KaFai Lau * created if one does not exist. *value* can be used 286080867c5eSQuentin Monnet * together with **BPF_SK_STORAGE_GET_F_CREATE** to specify 28616ac99e8fSMartin KaFai Lau * the initial value of a bpf-local-storage. If *value* is 286280867c5eSQuentin Monnet * **NULL**, the new bpf-local-storage will be zero initialized. 28636ac99e8fSMartin KaFai Lau * Return 28646ac99e8fSMartin KaFai Lau * A bpf-local-storage pointer is returned on success. 28656ac99e8fSMartin KaFai Lau * 28666ac99e8fSMartin KaFai Lau * **NULL** if not found or there was an error in adding 28676ac99e8fSMartin KaFai Lau * a new bpf-local-storage. 28686ac99e8fSMartin KaFai Lau * 286930897832SKP Singh * long bpf_sk_storage_delete(struct bpf_map *map, void *sk) 28706ac99e8fSMartin KaFai Lau * Description 287180867c5eSQuentin Monnet * Delete a bpf-local-storage from a *sk*. 28726ac99e8fSMartin KaFai Lau * Return 28736ac99e8fSMartin KaFai Lau * 0 on success. 28746ac99e8fSMartin KaFai Lau * 28756ac99e8fSMartin KaFai Lau * **-ENOENT** if the bpf-local-storage cannot be found. 2876592a3498SMartin KaFai Lau * **-EINVAL** if sk is not a fullsock (e.g. a request_sock). 28778b401f9eSYonghong Song * 2878bdb7b79bSAndrii Nakryiko * long bpf_send_signal(u32 sig) 28798b401f9eSYonghong Song * Description 28808482941fSYonghong Song * Send signal *sig* to the process of the current task. 28818482941fSYonghong Song * The signal may be delivered to any of this process's threads. 28828b401f9eSYonghong Song * Return 28838b401f9eSYonghong Song * 0 on success or successfully queued. 28848b401f9eSYonghong Song * 28858b401f9eSYonghong Song * **-EBUSY** if work queue under nmi is full. 28868b401f9eSYonghong Song * 28878b401f9eSYonghong Song * **-EINVAL** if *sig* is invalid. 28888b401f9eSYonghong Song * 28898b401f9eSYonghong Song * **-EPERM** if no permission to send the *sig*. 28908b401f9eSYonghong Song * 28918b401f9eSYonghong Song * **-EAGAIN** if bpf program can try again. 289270d66244SPetar Penkov * 2893c0df236eSMartin KaFai Lau * s64 bpf_tcp_gen_syncookie(void *sk, void *iph, u32 iph_len, struct tcphdr *th, u32 th_len) 289470d66244SPetar Penkov * Description 289570d66244SPetar Penkov * Try to issue a SYN cookie for the packet with corresponding 289670d66244SPetar Penkov * IP/TCP headers, *iph* and *th*, on the listening socket in *sk*. 289770d66244SPetar Penkov * 289870d66244SPetar Penkov * *iph* points to the start of the IPv4 or IPv6 header, while 289970d66244SPetar Penkov * *iph_len* contains **sizeof**\ (**struct iphdr**) or 290070d66244SPetar Penkov * **sizeof**\ (**struct ip6hdr**). 290170d66244SPetar Penkov * 290270d66244SPetar Penkov * *th* points to the start of the TCP header, while *th_len* 290370d66244SPetar Penkov * contains the length of the TCP header. 290470d66244SPetar Penkov * Return 290570d66244SPetar Penkov * On success, lower 32 bits hold the generated SYN cookie in 290670d66244SPetar Penkov * followed by 16 bits which hold the MSS value for that cookie, 290770d66244SPetar Penkov * and the top 16 bits are unused. 290870d66244SPetar Penkov * 290970d66244SPetar Penkov * On failure, the returned value is one of the following: 291070d66244SPetar Penkov * 291170d66244SPetar Penkov * **-EINVAL** SYN cookie cannot be issued due to error 291270d66244SPetar Penkov * 291370d66244SPetar Penkov * **-ENOENT** SYN cookie should not be issued (no SYN flood) 291470d66244SPetar Penkov * 291570d66244SPetar Penkov * **-EOPNOTSUPP** kernel configuration does not enable SYN cookies 291670d66244SPetar Penkov * 291770d66244SPetar Penkov * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 2918a7658e1aSAlexei Starovoitov * 2919bdb7b79bSAndrii Nakryiko * long bpf_skb_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size) 2920a7658e1aSAlexei Starovoitov * Description 2921a7658e1aSAlexei Starovoitov * Write raw *data* blob into a special BPF perf event held by 2922a7658e1aSAlexei Starovoitov * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 2923a7658e1aSAlexei Starovoitov * event must have the following attributes: **PERF_SAMPLE_RAW** 2924a7658e1aSAlexei Starovoitov * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 2925a7658e1aSAlexei Starovoitov * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 2926a7658e1aSAlexei Starovoitov * 2927a7658e1aSAlexei Starovoitov * The *flags* are used to indicate the index in *map* for which 2928a7658e1aSAlexei Starovoitov * the value must be put, masked with **BPF_F_INDEX_MASK**. 2929a7658e1aSAlexei Starovoitov * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 2930a7658e1aSAlexei Starovoitov * to indicate that the index of the current CPU core should be 2931a7658e1aSAlexei Starovoitov * used. 2932a7658e1aSAlexei Starovoitov * 2933a7658e1aSAlexei Starovoitov * The value to write, of *size*, is passed through eBPF stack and 2934a7658e1aSAlexei Starovoitov * pointed by *data*. 2935a7658e1aSAlexei Starovoitov * 2936a7658e1aSAlexei Starovoitov * *ctx* is a pointer to in-kernel struct sk_buff. 2937a7658e1aSAlexei Starovoitov * 2938a7658e1aSAlexei Starovoitov * This helper is similar to **bpf_perf_event_output**\ () but 2939a7658e1aSAlexei Starovoitov * restricted to raw_tracepoint bpf programs. 2940a7658e1aSAlexei Starovoitov * Return 2941a7658e1aSAlexei Starovoitov * 0 on success, or a negative error in case of failure. 29426ae08ae3SDaniel Borkmann * 2943bdb7b79bSAndrii Nakryiko * long bpf_probe_read_user(void *dst, u32 size, const void *unsafe_ptr) 29446ae08ae3SDaniel Borkmann * Description 29456ae08ae3SDaniel Borkmann * Safely attempt to read *size* bytes from user space address 29466ae08ae3SDaniel Borkmann * *unsafe_ptr* and store the data in *dst*. 29476ae08ae3SDaniel Borkmann * Return 29486ae08ae3SDaniel Borkmann * 0 on success, or a negative error in case of failure. 29496ae08ae3SDaniel Borkmann * 2950bdb7b79bSAndrii Nakryiko * long bpf_probe_read_kernel(void *dst, u32 size, const void *unsafe_ptr) 29516ae08ae3SDaniel Borkmann * Description 29526ae08ae3SDaniel Borkmann * Safely attempt to read *size* bytes from kernel space address 29536ae08ae3SDaniel Borkmann * *unsafe_ptr* and store the data in *dst*. 29546ae08ae3SDaniel Borkmann * Return 29556ae08ae3SDaniel Borkmann * 0 on success, or a negative error in case of failure. 29566ae08ae3SDaniel Borkmann * 2957bdb7b79bSAndrii Nakryiko * long bpf_probe_read_user_str(void *dst, u32 size, const void *unsafe_ptr) 29586ae08ae3SDaniel Borkmann * Description 29596ae08ae3SDaniel Borkmann * Copy a NUL terminated string from an unsafe user address 29606ae08ae3SDaniel Borkmann * *unsafe_ptr* to *dst*. The *size* should include the 29616ae08ae3SDaniel Borkmann * terminating NUL byte. In case the string length is smaller than 29626ae08ae3SDaniel Borkmann * *size*, the target is not padded with further NUL bytes. If the 29636ae08ae3SDaniel Borkmann * string length is larger than *size*, just *size*-1 bytes are 29646ae08ae3SDaniel Borkmann * copied and the last byte is set to NUL. 29656ae08ae3SDaniel Borkmann * 29666ae08ae3SDaniel Borkmann * On success, the length of the copied string is returned. This 29676ae08ae3SDaniel Borkmann * makes this helper useful in tracing programs for reading 29686ae08ae3SDaniel Borkmann * strings, and more importantly to get its length at runtime. See 29696ae08ae3SDaniel Borkmann * the following snippet: 29706ae08ae3SDaniel Borkmann * 29716ae08ae3SDaniel Borkmann * :: 29726ae08ae3SDaniel Borkmann * 29736ae08ae3SDaniel Borkmann * SEC("kprobe/sys_open") 29746ae08ae3SDaniel Borkmann * void bpf_sys_open(struct pt_regs *ctx) 29756ae08ae3SDaniel Borkmann * { 29766ae08ae3SDaniel Borkmann * char buf[PATHLEN]; // PATHLEN is defined to 256 29776ae08ae3SDaniel Borkmann * int res = bpf_probe_read_user_str(buf, sizeof(buf), 29786ae08ae3SDaniel Borkmann * ctx->di); 29796ae08ae3SDaniel Borkmann * 29806ae08ae3SDaniel Borkmann * // Consume buf, for example push it to 29816ae08ae3SDaniel Borkmann * // userspace via bpf_perf_event_output(); we 29826ae08ae3SDaniel Borkmann * // can use res (the string length) as event 29836ae08ae3SDaniel Borkmann * // size, after checking its boundaries. 29846ae08ae3SDaniel Borkmann * } 29856ae08ae3SDaniel Borkmann * 2986ab8d7809SQuentin Monnet * In comparison, using **bpf_probe_read_user**\ () helper here 29876ae08ae3SDaniel Borkmann * instead to read the string would require to estimate the length 29886ae08ae3SDaniel Borkmann * at compile time, and would often result in copying more memory 29896ae08ae3SDaniel Borkmann * than necessary. 29906ae08ae3SDaniel Borkmann * 29916ae08ae3SDaniel Borkmann * Another useful use case is when parsing individual process 29926ae08ae3SDaniel Borkmann * arguments or individual environment variables navigating 29936ae08ae3SDaniel Borkmann * *current*\ **->mm->arg_start** and *current*\ 29946ae08ae3SDaniel Borkmann * **->mm->env_start**: using this helper and the return value, 29956ae08ae3SDaniel Borkmann * one can quickly iterate at the right offset of the memory area. 29966ae08ae3SDaniel Borkmann * Return 29976ae08ae3SDaniel Borkmann * On success, the strictly positive length of the string, 29986ae08ae3SDaniel Borkmann * including the trailing NUL character. On error, a negative 29996ae08ae3SDaniel Borkmann * value. 30006ae08ae3SDaniel Borkmann * 3001bdb7b79bSAndrii Nakryiko * long bpf_probe_read_kernel_str(void *dst, u32 size, const void *unsafe_ptr) 30026ae08ae3SDaniel Borkmann * Description 30036ae08ae3SDaniel Borkmann * Copy a NUL terminated string from an unsafe kernel address *unsafe_ptr* 3004ab8d7809SQuentin Monnet * to *dst*. Same semantics as with **bpf_probe_read_user_str**\ () apply. 30056ae08ae3SDaniel Borkmann * Return 30066ae08ae3SDaniel Borkmann * On success, the strictly positive length of the string, including 30076ae08ae3SDaniel Borkmann * the trailing NUL character. On error, a negative value. 3008206057feSMartin KaFai Lau * 3009bdb7b79bSAndrii Nakryiko * long bpf_tcp_send_ack(void *tp, u32 rcv_nxt) 3010206057feSMartin KaFai Lau * Description 3011ab8d7809SQuentin Monnet * Send out a tcp-ack. *tp* is the in-kernel struct **tcp_sock**. 3012206057feSMartin KaFai Lau * *rcv_nxt* is the ack_seq to be sent out. 3013206057feSMartin KaFai Lau * Return 3014206057feSMartin KaFai Lau * 0 on success, or a negative error in case of failure. 3015206057feSMartin KaFai Lau * 3016bdb7b79bSAndrii Nakryiko * long bpf_send_signal_thread(u32 sig) 30178482941fSYonghong Song * Description 30188482941fSYonghong Song * Send signal *sig* to the thread corresponding to the current task. 30198482941fSYonghong Song * Return 30208482941fSYonghong Song * 0 on success or successfully queued. 30218482941fSYonghong Song * 30228482941fSYonghong Song * **-EBUSY** if work queue under nmi is full. 30238482941fSYonghong Song * 30248482941fSYonghong Song * **-EINVAL** if *sig* is invalid. 30258482941fSYonghong Song * 30268482941fSYonghong Song * **-EPERM** if no permission to send the *sig*. 30278482941fSYonghong Song * 30288482941fSYonghong Song * **-EAGAIN** if bpf program can try again. 30295576b991SMartin KaFai Lau * 30305576b991SMartin KaFai Lau * u64 bpf_jiffies64(void) 30315576b991SMartin KaFai Lau * Description 30325576b991SMartin KaFai Lau * Obtain the 64bit jiffies 30335576b991SMartin KaFai Lau * Return 30345576b991SMartin KaFai Lau * The 64 bit jiffies 3035fff7b643SDaniel Xu * 3036bdb7b79bSAndrii Nakryiko * long bpf_read_branch_records(struct bpf_perf_event_data *ctx, void *buf, u32 size, u64 flags) 3037fff7b643SDaniel Xu * Description 3038fff7b643SDaniel Xu * For an eBPF program attached to a perf event, retrieve the 3039ab8d7809SQuentin Monnet * branch records (**struct perf_branch_entry**) associated to *ctx* 3040fff7b643SDaniel Xu * and store it in the buffer pointed by *buf* up to size 3041fff7b643SDaniel Xu * *size* bytes. 3042fff7b643SDaniel Xu * Return 3043fff7b643SDaniel Xu * On success, number of bytes written to *buf*. On error, a 3044fff7b643SDaniel Xu * negative value. 3045fff7b643SDaniel Xu * 3046fff7b643SDaniel Xu * The *flags* can be set to **BPF_F_GET_BRANCH_RECORDS_SIZE** to 3047fff7b643SDaniel Xu * instead return the number of bytes required to store all the 3048fff7b643SDaniel Xu * branch entries. If this flag is set, *buf* may be NULL. 3049fff7b643SDaniel Xu * 3050fff7b643SDaniel Xu * **-EINVAL** if arguments invalid or **size** not a multiple 3051ab8d7809SQuentin Monnet * of **sizeof**\ (**struct perf_branch_entry**\ ). 3052fff7b643SDaniel Xu * 3053fff7b643SDaniel Xu * **-ENOENT** if architecture does not support branch records. 3054b4490c5cSCarlos Neira * 3055bdb7b79bSAndrii Nakryiko * long bpf_get_ns_current_pid_tgid(u64 dev, u64 ino, struct bpf_pidns_info *nsdata, u32 size) 3056b4490c5cSCarlos Neira * Description 3057b4490c5cSCarlos Neira * Returns 0 on success, values for *pid* and *tgid* as seen from the current 3058b4490c5cSCarlos Neira * *namespace* will be returned in *nsdata*. 3059ab8d7809SQuentin Monnet * Return 3060ab8d7809SQuentin Monnet * 0 on success, or one of the following in case of failure: 3061b4490c5cSCarlos Neira * 3062b4490c5cSCarlos Neira * **-EINVAL** if dev and inum supplied don't match dev_t and inode number 3063b4490c5cSCarlos Neira * with nsfs of current task, or if dev conversion to dev_t lost high bits. 3064b4490c5cSCarlos Neira * 3065b4490c5cSCarlos Neira * **-ENOENT** if pidns does not exists for the current task. 3066b4490c5cSCarlos Neira * 3067bdb7b79bSAndrii Nakryiko * long bpf_xdp_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size) 3068d831ee84SEelco Chaudron * Description 3069d831ee84SEelco Chaudron * Write raw *data* blob into a special BPF perf event held by 3070d831ee84SEelco Chaudron * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 3071d831ee84SEelco Chaudron * event must have the following attributes: **PERF_SAMPLE_RAW** 3072d831ee84SEelco Chaudron * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 3073d831ee84SEelco Chaudron * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 3074d831ee84SEelco Chaudron * 3075d831ee84SEelco Chaudron * The *flags* are used to indicate the index in *map* for which 3076d831ee84SEelco Chaudron * the value must be put, masked with **BPF_F_INDEX_MASK**. 3077d831ee84SEelco Chaudron * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 3078d831ee84SEelco Chaudron * to indicate that the index of the current CPU core should be 3079d831ee84SEelco Chaudron * used. 3080d831ee84SEelco Chaudron * 3081d831ee84SEelco Chaudron * The value to write, of *size*, is passed through eBPF stack and 3082d831ee84SEelco Chaudron * pointed by *data*. 3083d831ee84SEelco Chaudron * 3084d831ee84SEelco Chaudron * *ctx* is a pointer to in-kernel struct xdp_buff. 3085d831ee84SEelco Chaudron * 3086d831ee84SEelco Chaudron * This helper is similar to **bpf_perf_eventoutput**\ () but 3087d831ee84SEelco Chaudron * restricted to raw_tracepoint bpf programs. 3088d831ee84SEelco Chaudron * Return 3089d831ee84SEelco Chaudron * 0 on success, or a negative error in case of failure. 3090f318903cSDaniel Borkmann * 3091f318903cSDaniel Borkmann * u64 bpf_get_netns_cookie(void *ctx) 3092f318903cSDaniel Borkmann * Description 3093f318903cSDaniel Borkmann * Retrieve the cookie (generated by the kernel) of the network 3094f318903cSDaniel Borkmann * namespace the input *ctx* is associated with. The network 3095f318903cSDaniel Borkmann * namespace cookie remains stable for its lifetime and provides 3096f318903cSDaniel Borkmann * a global identifier that can be assumed unique. If *ctx* is 3097f318903cSDaniel Borkmann * NULL, then the helper returns the cookie for the initial 3098f318903cSDaniel Borkmann * network namespace. The cookie itself is very similar to that 3099ab8d7809SQuentin Monnet * of **bpf_get_socket_cookie**\ () helper, but for network 3100ab8d7809SQuentin Monnet * namespaces instead of sockets. 3101f318903cSDaniel Borkmann * Return 3102f318903cSDaniel Borkmann * A 8-byte long opaque number. 31030f09abd1SDaniel Borkmann * 31040f09abd1SDaniel Borkmann * u64 bpf_get_current_ancestor_cgroup_id(int ancestor_level) 31050f09abd1SDaniel Borkmann * Description 31060f09abd1SDaniel Borkmann * Return id of cgroup v2 that is ancestor of the cgroup associated 31070f09abd1SDaniel Borkmann * with the current task at the *ancestor_level*. The root cgroup 31080f09abd1SDaniel Borkmann * is at *ancestor_level* zero and each step down the hierarchy 31090f09abd1SDaniel Borkmann * increments the level. If *ancestor_level* == level of cgroup 31100f09abd1SDaniel Borkmann * associated with the current task, then return value will be the 31110f09abd1SDaniel Borkmann * same as that of **bpf_get_current_cgroup_id**\ (). 31120f09abd1SDaniel Borkmann * 31130f09abd1SDaniel Borkmann * The helper is useful to implement policies based on cgroups 31140f09abd1SDaniel Borkmann * that are upper in hierarchy than immediate cgroup associated 31150f09abd1SDaniel Borkmann * with the current task. 31160f09abd1SDaniel Borkmann * 31170f09abd1SDaniel Borkmann * The format of returned id and helper limitations are same as in 31180f09abd1SDaniel Borkmann * **bpf_get_current_cgroup_id**\ (). 31190f09abd1SDaniel Borkmann * Return 31200f09abd1SDaniel Borkmann * The id is returned or 0 in case the id could not be retrieved. 3121cf7fbe66SJoe Stringer * 312227e5203bSMartin KaFai Lau * long bpf_sk_assign(struct sk_buff *skb, void *sk, u64 flags) 3123cf7fbe66SJoe Stringer * Description 3124e9ddbb77SJakub Sitnicki * Helper is overloaded depending on BPF program type. This 3125e9ddbb77SJakub Sitnicki * description applies to **BPF_PROG_TYPE_SCHED_CLS** and 3126e9ddbb77SJakub Sitnicki * **BPF_PROG_TYPE_SCHED_ACT** programs. 3127e9ddbb77SJakub Sitnicki * 3128cf7fbe66SJoe Stringer * Assign the *sk* to the *skb*. When combined with appropriate 3129cf7fbe66SJoe Stringer * routing configuration to receive the packet towards the socket, 3130cf7fbe66SJoe Stringer * will cause *skb* to be delivered to the specified socket. 3131cf7fbe66SJoe Stringer * Subsequent redirection of *skb* via **bpf_redirect**\ (), 3132cf7fbe66SJoe Stringer * **bpf_clone_redirect**\ () or other methods outside of BPF may 3133cf7fbe66SJoe Stringer * interfere with successful delivery to the socket. 3134cf7fbe66SJoe Stringer * 3135cf7fbe66SJoe Stringer * This operation is only valid from TC ingress path. 3136cf7fbe66SJoe Stringer * 3137cf7fbe66SJoe Stringer * The *flags* argument must be zero. 3138cf7fbe66SJoe Stringer * Return 3139ab8d7809SQuentin Monnet * 0 on success, or a negative error in case of failure: 3140cf7fbe66SJoe Stringer * 3141ab8d7809SQuentin Monnet * **-EINVAL** if specified *flags* are not supported. 3142ab8d7809SQuentin Monnet * 3143ab8d7809SQuentin Monnet * **-ENOENT** if the socket is unavailable for assignment. 3144ab8d7809SQuentin Monnet * 3145ab8d7809SQuentin Monnet * **-ENETUNREACH** if the socket is unreachable (wrong netns). 3146ab8d7809SQuentin Monnet * 3147ab8d7809SQuentin Monnet * **-EOPNOTSUPP** if the operation is not supported, for example 3148ab8d7809SQuentin Monnet * a call from outside of TC ingress. 3149ab8d7809SQuentin Monnet * 3150ab8d7809SQuentin Monnet * **-ESOCKTNOSUPPORT** if the socket type is not supported 3151ab8d7809SQuentin Monnet * (reuseport). 315271d19214SMaciej Żenczykowski * 3153e9ddbb77SJakub Sitnicki * long bpf_sk_assign(struct bpf_sk_lookup *ctx, struct bpf_sock *sk, u64 flags) 3154e9ddbb77SJakub Sitnicki * Description 3155e9ddbb77SJakub Sitnicki * Helper is overloaded depending on BPF program type. This 3156e9ddbb77SJakub Sitnicki * description applies to **BPF_PROG_TYPE_SK_LOOKUP** programs. 3157e9ddbb77SJakub Sitnicki * 3158e9ddbb77SJakub Sitnicki * Select the *sk* as a result of a socket lookup. 3159e9ddbb77SJakub Sitnicki * 3160e9ddbb77SJakub Sitnicki * For the operation to succeed passed socket must be compatible 3161e9ddbb77SJakub Sitnicki * with the packet description provided by the *ctx* object. 3162e9ddbb77SJakub Sitnicki * 3163e9ddbb77SJakub Sitnicki * L4 protocol (**IPPROTO_TCP** or **IPPROTO_UDP**) must 3164e9ddbb77SJakub Sitnicki * be an exact match. While IP family (**AF_INET** or 3165e9ddbb77SJakub Sitnicki * **AF_INET6**) must be compatible, that is IPv6 sockets 3166e9ddbb77SJakub Sitnicki * that are not v6-only can be selected for IPv4 packets. 3167e9ddbb77SJakub Sitnicki * 3168e9ddbb77SJakub Sitnicki * Only TCP listeners and UDP unconnected sockets can be 3169e9ddbb77SJakub Sitnicki * selected. *sk* can also be NULL to reset any previous 3170e9ddbb77SJakub Sitnicki * selection. 3171e9ddbb77SJakub Sitnicki * 3172e9ddbb77SJakub Sitnicki * *flags* argument can combination of following values: 3173e9ddbb77SJakub Sitnicki * 3174e9ddbb77SJakub Sitnicki * * **BPF_SK_LOOKUP_F_REPLACE** to override the previous 3175e9ddbb77SJakub Sitnicki * socket selection, potentially done by a BPF program 3176e9ddbb77SJakub Sitnicki * that ran before us. 3177e9ddbb77SJakub Sitnicki * 3178e9ddbb77SJakub Sitnicki * * **BPF_SK_LOOKUP_F_NO_REUSEPORT** to skip 3179e9ddbb77SJakub Sitnicki * load-balancing within reuseport group for the socket 3180e9ddbb77SJakub Sitnicki * being selected. 3181e9ddbb77SJakub Sitnicki * 3182e9ddbb77SJakub Sitnicki * On success *ctx->sk* will point to the selected socket. 3183e9ddbb77SJakub Sitnicki * 3184e9ddbb77SJakub Sitnicki * Return 3185e9ddbb77SJakub Sitnicki * 0 on success, or a negative errno in case of failure. 3186e9ddbb77SJakub Sitnicki * 3187e9ddbb77SJakub Sitnicki * * **-EAFNOSUPPORT** if socket family (*sk->family*) is 3188e9ddbb77SJakub Sitnicki * not compatible with packet family (*ctx->family*). 3189e9ddbb77SJakub Sitnicki * 3190e9ddbb77SJakub Sitnicki * * **-EEXIST** if socket has been already selected, 3191e9ddbb77SJakub Sitnicki * potentially by another program, and 3192e9ddbb77SJakub Sitnicki * **BPF_SK_LOOKUP_F_REPLACE** flag was not specified. 3193e9ddbb77SJakub Sitnicki * 3194e9ddbb77SJakub Sitnicki * * **-EINVAL** if unsupported flags were specified. 3195e9ddbb77SJakub Sitnicki * 3196e9ddbb77SJakub Sitnicki * * **-EPROTOTYPE** if socket L4 protocol 3197e9ddbb77SJakub Sitnicki * (*sk->protocol*) doesn't match packet protocol 3198e9ddbb77SJakub Sitnicki * (*ctx->protocol*). 3199e9ddbb77SJakub Sitnicki * 3200e9ddbb77SJakub Sitnicki * * **-ESOCKTNOSUPPORT** if socket is not in allowed 3201e9ddbb77SJakub Sitnicki * state (TCP listening or UDP unconnected). 3202e9ddbb77SJakub Sitnicki * 320371d19214SMaciej Żenczykowski * u64 bpf_ktime_get_boot_ns(void) 320471d19214SMaciej Żenczykowski * Description 320571d19214SMaciej Żenczykowski * Return the time elapsed since system boot, in nanoseconds. 320671d19214SMaciej Żenczykowski * Does include the time the system was suspended. 3207ab8d7809SQuentin Monnet * See: **clock_gettime**\ (**CLOCK_BOOTTIME**) 320871d19214SMaciej Żenczykowski * Return 320971d19214SMaciej Żenczykowski * Current *ktime*. 3210492e639fSYonghong Song * 3211bdb7b79bSAndrii Nakryiko * long bpf_seq_printf(struct seq_file *m, const char *fmt, u32 fmt_size, const void *data, u32 data_len) 3212492e639fSYonghong Song * Description 3213ab8d7809SQuentin Monnet * **bpf_seq_printf**\ () uses seq_file **seq_printf**\ () to print 3214ab8d7809SQuentin Monnet * out the format string. 3215492e639fSYonghong Song * The *m* represents the seq_file. The *fmt* and *fmt_size* are for 3216492e639fSYonghong Song * the format string itself. The *data* and *data_len* are format string 3217ab8d7809SQuentin Monnet * arguments. The *data* are a **u64** array and corresponding format string 3218492e639fSYonghong Song * values are stored in the array. For strings and pointers where pointees 3219492e639fSYonghong Song * are accessed, only the pointer values are stored in the *data* array. 3220ab8d7809SQuentin Monnet * The *data_len* is the size of *data* in bytes. 3221492e639fSYonghong Song * 3222492e639fSYonghong Song * Formats **%s**, **%p{i,I}{4,6}** requires to read kernel memory. 3223492e639fSYonghong Song * Reading kernel memory may fail due to either invalid address or 3224492e639fSYonghong Song * valid address but requiring a major memory fault. If reading kernel memory 3225492e639fSYonghong Song * fails, the string for **%s** will be an empty string, and the ip 3226492e639fSYonghong Song * address for **%p{i,I}{4,6}** will be 0. Not returning error to 3227ab8d7809SQuentin Monnet * bpf program is consistent with what **bpf_trace_printk**\ () does for now. 3228492e639fSYonghong Song * Return 3229ab8d7809SQuentin Monnet * 0 on success, or a negative error in case of failure: 3230492e639fSYonghong Song * 3231ab8d7809SQuentin Monnet * **-EBUSY** if per-CPU memory copy buffer is busy, can try again 3232492e639fSYonghong Song * by returning 1 from bpf program. 3233ab8d7809SQuentin Monnet * 3234ab8d7809SQuentin Monnet * **-EINVAL** if arguments are invalid, or if *fmt* is invalid/unsupported. 3235ab8d7809SQuentin Monnet * 3236ab8d7809SQuentin Monnet * **-E2BIG** if *fmt* contains too many format specifiers. 3237ab8d7809SQuentin Monnet * 3238ab8d7809SQuentin Monnet * **-EOVERFLOW** if an overflow happened: The same object will be tried again. 3239492e639fSYonghong Song * 3240bdb7b79bSAndrii Nakryiko * long bpf_seq_write(struct seq_file *m, const void *data, u32 len) 3241492e639fSYonghong Song * Description 3242ab8d7809SQuentin Monnet * **bpf_seq_write**\ () uses seq_file **seq_write**\ () to write the data. 3243492e639fSYonghong Song * The *m* represents the seq_file. The *data* and *len* represent the 3244492e639fSYonghong Song * data to write in bytes. 3245492e639fSYonghong Song * Return 3246ab8d7809SQuentin Monnet * 0 on success, or a negative error in case of failure: 3247492e639fSYonghong Song * 3248ab8d7809SQuentin Monnet * **-EOVERFLOW** if an overflow happened: The same object will be tried again. 3249f307fa2cSAndrey Ignatov * 3250a5fa25adSMartin KaFai Lau * u64 bpf_sk_cgroup_id(void *sk) 3251f307fa2cSAndrey Ignatov * Description 3252f307fa2cSAndrey Ignatov * Return the cgroup v2 id of the socket *sk*. 3253f307fa2cSAndrey Ignatov * 3254a5fa25adSMartin KaFai Lau * *sk* must be a non-**NULL** pointer to a socket, e.g. one 3255f307fa2cSAndrey Ignatov * returned from **bpf_sk_lookup_xxx**\ (), 3256f307fa2cSAndrey Ignatov * **bpf_sk_fullsock**\ (), etc. The format of returned id is 3257f307fa2cSAndrey Ignatov * same as in **bpf_skb_cgroup_id**\ (). 3258f307fa2cSAndrey Ignatov * 3259f307fa2cSAndrey Ignatov * This helper is available only if the kernel was compiled with 3260f307fa2cSAndrey Ignatov * the **CONFIG_SOCK_CGROUP_DATA** configuration option. 3261f307fa2cSAndrey Ignatov * Return 3262f307fa2cSAndrey Ignatov * The id is returned or 0 in case the id could not be retrieved. 3263f307fa2cSAndrey Ignatov * 3264a5fa25adSMartin KaFai Lau * u64 bpf_sk_ancestor_cgroup_id(void *sk, int ancestor_level) 3265f307fa2cSAndrey Ignatov * Description 3266f307fa2cSAndrey Ignatov * Return id of cgroup v2 that is ancestor of cgroup associated 3267f307fa2cSAndrey Ignatov * with the *sk* at the *ancestor_level*. The root cgroup is at 3268f307fa2cSAndrey Ignatov * *ancestor_level* zero and each step down the hierarchy 3269f307fa2cSAndrey Ignatov * increments the level. If *ancestor_level* == level of cgroup 3270f307fa2cSAndrey Ignatov * associated with *sk*, then return value will be same as that 3271f307fa2cSAndrey Ignatov * of **bpf_sk_cgroup_id**\ (). 3272f307fa2cSAndrey Ignatov * 3273f307fa2cSAndrey Ignatov * The helper is useful to implement policies based on cgroups 3274f307fa2cSAndrey Ignatov * that are upper in hierarchy than immediate cgroup associated 3275f307fa2cSAndrey Ignatov * with *sk*. 3276f307fa2cSAndrey Ignatov * 3277f307fa2cSAndrey Ignatov * The format of returned id and helper limitations are same as in 3278f307fa2cSAndrey Ignatov * **bpf_sk_cgroup_id**\ (). 3279f307fa2cSAndrey Ignatov * Return 3280f307fa2cSAndrey Ignatov * The id is returned or 0 in case the id could not be retrieved. 3281457f4436SAndrii Nakryiko * 3282e1613b57SAndrii Nakryiko * long bpf_ringbuf_output(void *ringbuf, void *data, u64 size, u64 flags) 3283457f4436SAndrii Nakryiko * Description 3284457f4436SAndrii Nakryiko * Copy *size* bytes from *data* into a ring buffer *ringbuf*. 3285bcc7f554SQuentin Monnet * If **BPF_RB_NO_WAKEUP** is specified in *flags*, no notification 3286bcc7f554SQuentin Monnet * of new data availability is sent. 3287bcc7f554SQuentin Monnet * If **BPF_RB_FORCE_WAKEUP** is specified in *flags*, notification 3288bcc7f554SQuentin Monnet * of new data availability is sent unconditionally. 3289457f4436SAndrii Nakryiko * Return 3290bcc7f554SQuentin Monnet * 0 on success, or a negative error in case of failure. 3291457f4436SAndrii Nakryiko * 3292457f4436SAndrii Nakryiko * void *bpf_ringbuf_reserve(void *ringbuf, u64 size, u64 flags) 3293457f4436SAndrii Nakryiko * Description 3294457f4436SAndrii Nakryiko * Reserve *size* bytes of payload in a ring buffer *ringbuf*. 3295457f4436SAndrii Nakryiko * Return 3296457f4436SAndrii Nakryiko * Valid pointer with *size* bytes of memory available; NULL, 3297457f4436SAndrii Nakryiko * otherwise. 3298457f4436SAndrii Nakryiko * 3299457f4436SAndrii Nakryiko * void bpf_ringbuf_submit(void *data, u64 flags) 3300457f4436SAndrii Nakryiko * Description 3301457f4436SAndrii Nakryiko * Submit reserved ring buffer sample, pointed to by *data*. 3302bcc7f554SQuentin Monnet * If **BPF_RB_NO_WAKEUP** is specified in *flags*, no notification 3303bcc7f554SQuentin Monnet * of new data availability is sent. 3304bcc7f554SQuentin Monnet * If **BPF_RB_FORCE_WAKEUP** is specified in *flags*, notification 3305bcc7f554SQuentin Monnet * of new data availability is sent unconditionally. 3306457f4436SAndrii Nakryiko * Return 3307457f4436SAndrii Nakryiko * Nothing. Always succeeds. 3308457f4436SAndrii Nakryiko * 3309457f4436SAndrii Nakryiko * void bpf_ringbuf_discard(void *data, u64 flags) 3310457f4436SAndrii Nakryiko * Description 3311457f4436SAndrii Nakryiko * Discard reserved ring buffer sample, pointed to by *data*. 3312bcc7f554SQuentin Monnet * If **BPF_RB_NO_WAKEUP** is specified in *flags*, no notification 3313bcc7f554SQuentin Monnet * of new data availability is sent. 3314bcc7f554SQuentin Monnet * If **BPF_RB_FORCE_WAKEUP** is specified in *flags*, notification 3315bcc7f554SQuentin Monnet * of new data availability is sent unconditionally. 3316457f4436SAndrii Nakryiko * Return 3317457f4436SAndrii Nakryiko * Nothing. Always succeeds. 3318457f4436SAndrii Nakryiko * 3319457f4436SAndrii Nakryiko * u64 bpf_ringbuf_query(void *ringbuf, u64 flags) 3320457f4436SAndrii Nakryiko * Description 3321457f4436SAndrii Nakryiko * Query various characteristics of provided ring buffer. What 3322457f4436SAndrii Nakryiko * exactly is queries is determined by *flags*: 3323bcc7f554SQuentin Monnet * 3324bcc7f554SQuentin Monnet * * **BPF_RB_AVAIL_DATA**: Amount of data not yet consumed. 3325bcc7f554SQuentin Monnet * * **BPF_RB_RING_SIZE**: The size of ring buffer. 3326bcc7f554SQuentin Monnet * * **BPF_RB_CONS_POS**: Consumer position (can wrap around). 3327bcc7f554SQuentin Monnet * * **BPF_RB_PROD_POS**: Producer(s) position (can wrap around). 3328bcc7f554SQuentin Monnet * 3329bcc7f554SQuentin Monnet * Data returned is just a momentary snapshot of actual values 3330457f4436SAndrii Nakryiko * and could be inaccurate, so this facility should be used to 3331457f4436SAndrii Nakryiko * power heuristics and for reporting, not to make 100% correct 3332457f4436SAndrii Nakryiko * calculation. 3333457f4436SAndrii Nakryiko * Return 3334bcc7f554SQuentin Monnet * Requested value, or 0, if *flags* are not recognized. 33357cdec54fSDaniel Borkmann * 3336bdb7b79bSAndrii Nakryiko * long bpf_csum_level(struct sk_buff *skb, u64 level) 33377cdec54fSDaniel Borkmann * Description 33387cdec54fSDaniel Borkmann * Change the skbs checksum level by one layer up or down, or 33397cdec54fSDaniel Borkmann * reset it entirely to none in order to have the stack perform 33407cdec54fSDaniel Borkmann * checksum validation. The level is applicable to the following 33417cdec54fSDaniel Borkmann * protocols: TCP, UDP, GRE, SCTP, FCOE. For example, a decap of 33427cdec54fSDaniel Borkmann * | ETH | IP | UDP | GUE | IP | TCP | into | ETH | IP | TCP | 33437cdec54fSDaniel Borkmann * through **bpf_skb_adjust_room**\ () helper with passing in 33447cdec54fSDaniel Borkmann * **BPF_F_ADJ_ROOM_NO_CSUM_RESET** flag would require one call 33457cdec54fSDaniel Borkmann * to **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_DEC** since 33467cdec54fSDaniel Borkmann * the UDP header is removed. Similarly, an encap of the latter 33477cdec54fSDaniel Borkmann * into the former could be accompanied by a helper call to 33487cdec54fSDaniel Borkmann * **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_INC** if the 33497cdec54fSDaniel Borkmann * skb is still intended to be processed in higher layers of the 33507cdec54fSDaniel Borkmann * stack instead of just egressing at tc. 33517cdec54fSDaniel Borkmann * 33527cdec54fSDaniel Borkmann * There are three supported level settings at this time: 33537cdec54fSDaniel Borkmann * 33547cdec54fSDaniel Borkmann * * **BPF_CSUM_LEVEL_INC**: Increases skb->csum_level for skbs 33557cdec54fSDaniel Borkmann * with CHECKSUM_UNNECESSARY. 33567cdec54fSDaniel Borkmann * * **BPF_CSUM_LEVEL_DEC**: Decreases skb->csum_level for skbs 33577cdec54fSDaniel Borkmann * with CHECKSUM_UNNECESSARY. 33587cdec54fSDaniel Borkmann * * **BPF_CSUM_LEVEL_RESET**: Resets skb->csum_level to 0 and 33597cdec54fSDaniel Borkmann * sets CHECKSUM_NONE to force checksum validation by the stack. 33607cdec54fSDaniel Borkmann * * **BPF_CSUM_LEVEL_QUERY**: No-op, returns the current 33617cdec54fSDaniel Borkmann * skb->csum_level. 33627cdec54fSDaniel Borkmann * Return 33637cdec54fSDaniel Borkmann * 0 on success, or a negative error in case of failure. In the 33647cdec54fSDaniel Borkmann * case of **BPF_CSUM_LEVEL_QUERY**, the current skb->csum_level 33657cdec54fSDaniel Borkmann * is returned or the error code -EACCES in case the skb is not 33667cdec54fSDaniel Borkmann * subject to CHECKSUM_UNNECESSARY. 3367af7ec138SYonghong Song * 3368af7ec138SYonghong Song * struct tcp6_sock *bpf_skc_to_tcp6_sock(void *sk) 3369af7ec138SYonghong Song * Description 3370af7ec138SYonghong Song * Dynamically cast a *sk* pointer to a *tcp6_sock* pointer. 3371af7ec138SYonghong Song * Return 3372938c3efdSQuentin Monnet * *sk* if casting is valid, or **NULL** otherwise. 3373478cfbdfSYonghong Song * 3374478cfbdfSYonghong Song * struct tcp_sock *bpf_skc_to_tcp_sock(void *sk) 3375478cfbdfSYonghong Song * Description 3376478cfbdfSYonghong Song * Dynamically cast a *sk* pointer to a *tcp_sock* pointer. 3377478cfbdfSYonghong Song * Return 3378938c3efdSQuentin Monnet * *sk* if casting is valid, or **NULL** otherwise. 3379478cfbdfSYonghong Song * 3380478cfbdfSYonghong Song * struct tcp_timewait_sock *bpf_skc_to_tcp_timewait_sock(void *sk) 3381478cfbdfSYonghong Song * Description 3382478cfbdfSYonghong Song * Dynamically cast a *sk* pointer to a *tcp_timewait_sock* pointer. 3383478cfbdfSYonghong Song * Return 3384938c3efdSQuentin Monnet * *sk* if casting is valid, or **NULL** otherwise. 3385478cfbdfSYonghong Song * 3386478cfbdfSYonghong Song * struct tcp_request_sock *bpf_skc_to_tcp_request_sock(void *sk) 3387478cfbdfSYonghong Song * Description 3388478cfbdfSYonghong Song * Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer. 3389478cfbdfSYonghong Song * Return 3390938c3efdSQuentin Monnet * *sk* if casting is valid, or **NULL** otherwise. 33910d4fad3eSYonghong Song * 33920d4fad3eSYonghong Song * struct udp6_sock *bpf_skc_to_udp6_sock(void *sk) 33930d4fad3eSYonghong Song * Description 33940d4fad3eSYonghong Song * Dynamically cast a *sk* pointer to a *udp6_sock* pointer. 33950d4fad3eSYonghong Song * Return 3396938c3efdSQuentin Monnet * *sk* if casting is valid, or **NULL** otherwise. 3397fa28dcb8SSong Liu * 3398fa28dcb8SSong Liu * long bpf_get_task_stack(struct task_struct *task, void *buf, u32 size, u64 flags) 3399fa28dcb8SSong Liu * Description 3400fa28dcb8SSong Liu * Return a user or a kernel stack in bpf program provided buffer. 3401fa28dcb8SSong Liu * To achieve this, the helper needs *task*, which is a valid 3402938c3efdSQuentin Monnet * pointer to **struct task_struct**. To store the stacktrace, the 3403fa28dcb8SSong Liu * bpf program provides *buf* with a nonnegative *size*. 3404fa28dcb8SSong Liu * 3405fa28dcb8SSong Liu * The last argument, *flags*, holds the number of stack frames to 3406fa28dcb8SSong Liu * skip (from 0 to 255), masked with 3407fa28dcb8SSong Liu * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 3408fa28dcb8SSong Liu * the following flags: 3409fa28dcb8SSong Liu * 3410fa28dcb8SSong Liu * **BPF_F_USER_STACK** 3411fa28dcb8SSong Liu * Collect a user space stack instead of a kernel stack. 3412fa28dcb8SSong Liu * **BPF_F_USER_BUILD_ID** 3413fa28dcb8SSong Liu * Collect buildid+offset instead of ips for user stack, 3414fa28dcb8SSong Liu * only valid if **BPF_F_USER_STACK** is also specified. 3415fa28dcb8SSong Liu * 3416fa28dcb8SSong Liu * **bpf_get_task_stack**\ () can collect up to 3417fa28dcb8SSong Liu * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject 3418fa28dcb8SSong Liu * to sufficient large buffer size. Note that 3419fa28dcb8SSong Liu * this limit can be controlled with the **sysctl** program, and 3420fa28dcb8SSong Liu * that it should be manually increased in order to profile long 3421fa28dcb8SSong Liu * user stacks (such as stacks for Java programs). To do so, use: 3422fa28dcb8SSong Liu * 3423fa28dcb8SSong Liu * :: 3424fa28dcb8SSong Liu * 3425fa28dcb8SSong Liu * # sysctl kernel.perf_event_max_stack=<new value> 3426fa28dcb8SSong Liu * Return 3427fa28dcb8SSong Liu * A non-negative value equal to or less than *size* on success, 3428fa28dcb8SSong Liu * or a negative error in case of failure. 3429fa28dcb8SSong Liu * 34300813a841SMartin KaFai Lau * long bpf_load_hdr_opt(struct bpf_sock_ops *skops, void *searchby_res, u32 len, u64 flags) 34310813a841SMartin KaFai Lau * Description 34320813a841SMartin KaFai Lau * Load header option. Support reading a particular TCP header 3433938c3efdSQuentin Monnet * option for bpf program (**BPF_PROG_TYPE_SOCK_OPS**). 34340813a841SMartin KaFai Lau * 34350813a841SMartin KaFai Lau * If *flags* is 0, it will search the option from the 3436938c3efdSQuentin Monnet * *skops*\ **->skb_data**. The comment in **struct bpf_sock_ops** 34370813a841SMartin KaFai Lau * has details on what skb_data contains under different 3438938c3efdSQuentin Monnet * *skops*\ **->op**. 34390813a841SMartin KaFai Lau * 34400813a841SMartin KaFai Lau * The first byte of the *searchby_res* specifies the 34410813a841SMartin KaFai Lau * kind that it wants to search. 34420813a841SMartin KaFai Lau * 34430813a841SMartin KaFai Lau * If the searching kind is an experimental kind 34440813a841SMartin KaFai Lau * (i.e. 253 or 254 according to RFC6994). It also 34450813a841SMartin KaFai Lau * needs to specify the "magic" which is either 34460813a841SMartin KaFai Lau * 2 bytes or 4 bytes. It then also needs to 34470813a841SMartin KaFai Lau * specify the size of the magic by using 34480813a841SMartin KaFai Lau * the 2nd byte which is "kind-length" of a TCP 34490813a841SMartin KaFai Lau * header option and the "kind-length" also 34500813a841SMartin KaFai Lau * includes the first 2 bytes "kind" and "kind-length" 34510813a841SMartin KaFai Lau * itself as a normal TCP header option also does. 34520813a841SMartin KaFai Lau * 34530813a841SMartin KaFai Lau * For example, to search experimental kind 254 with 34540813a841SMartin KaFai Lau * 2 byte magic 0xeB9F, the searchby_res should be 34550813a841SMartin KaFai Lau * [ 254, 4, 0xeB, 0x9F, 0, 0, .... 0 ]. 34560813a841SMartin KaFai Lau * 34570813a841SMartin KaFai Lau * To search for the standard window scale option (3), 3458938c3efdSQuentin Monnet * the *searchby_res* should be [ 3, 0, 0, .... 0 ]. 34590813a841SMartin KaFai Lau * Note, kind-length must be 0 for regular option. 34600813a841SMartin KaFai Lau * 34610813a841SMartin KaFai Lau * Searching for No-Op (0) and End-of-Option-List (1) are 34620813a841SMartin KaFai Lau * not supported. 34630813a841SMartin KaFai Lau * 34640813a841SMartin KaFai Lau * *len* must be at least 2 bytes which is the minimal size 34650813a841SMartin KaFai Lau * of a header option. 34660813a841SMartin KaFai Lau * 34670813a841SMartin KaFai Lau * Supported flags: 3468938c3efdSQuentin Monnet * 34690813a841SMartin KaFai Lau * * **BPF_LOAD_HDR_OPT_TCP_SYN** to search from the 34700813a841SMartin KaFai Lau * saved_syn packet or the just-received syn packet. 34710813a841SMartin KaFai Lau * 34720813a841SMartin KaFai Lau * Return 34730813a841SMartin KaFai Lau * > 0 when found, the header option is copied to *searchby_res*. 3474938c3efdSQuentin Monnet * The return value is the total length copied. On failure, a 3475938c3efdSQuentin Monnet * negative error code is returned: 34760813a841SMartin KaFai Lau * 3477938c3efdSQuentin Monnet * **-EINVAL** if a parameter is invalid. 34780813a841SMartin KaFai Lau * 3479938c3efdSQuentin Monnet * **-ENOMSG** if the option is not found. 34800813a841SMartin KaFai Lau * 3481938c3efdSQuentin Monnet * **-ENOENT** if no syn packet is available when 3482938c3efdSQuentin Monnet * **BPF_LOAD_HDR_OPT_TCP_SYN** is used. 34830813a841SMartin KaFai Lau * 3484938c3efdSQuentin Monnet * **-ENOSPC** if there is not enough space. Only *len* number of 34850813a841SMartin KaFai Lau * bytes are copied. 34860813a841SMartin KaFai Lau * 3487938c3efdSQuentin Monnet * **-EFAULT** on failure to parse the header options in the 3488938c3efdSQuentin Monnet * packet. 34890813a841SMartin KaFai Lau * 3490938c3efdSQuentin Monnet * **-EPERM** if the helper cannot be used under the current 3491938c3efdSQuentin Monnet * *skops*\ **->op**. 34920813a841SMartin KaFai Lau * 34930813a841SMartin KaFai Lau * long bpf_store_hdr_opt(struct bpf_sock_ops *skops, const void *from, u32 len, u64 flags) 34940813a841SMartin KaFai Lau * Description 34950813a841SMartin KaFai Lau * Store header option. The data will be copied 34960813a841SMartin KaFai Lau * from buffer *from* with length *len* to the TCP header. 34970813a841SMartin KaFai Lau * 34980813a841SMartin KaFai Lau * The buffer *from* should have the whole option that 34990813a841SMartin KaFai Lau * includes the kind, kind-length, and the actual 35000813a841SMartin KaFai Lau * option data. The *len* must be at least kind-length 35010813a841SMartin KaFai Lau * long. The kind-length does not have to be 4 byte 35020813a841SMartin KaFai Lau * aligned. The kernel will take care of the padding 35030813a841SMartin KaFai Lau * and setting the 4 bytes aligned value to th->doff. 35040813a841SMartin KaFai Lau * 35050813a841SMartin KaFai Lau * This helper will check for duplicated option 35060813a841SMartin KaFai Lau * by searching the same option in the outgoing skb. 35070813a841SMartin KaFai Lau * 35080813a841SMartin KaFai Lau * This helper can only be called during 3509938c3efdSQuentin Monnet * **BPF_SOCK_OPS_WRITE_HDR_OPT_CB**. 35100813a841SMartin KaFai Lau * 35110813a841SMartin KaFai Lau * Return 35120813a841SMartin KaFai Lau * 0 on success, or negative error in case of failure: 35130813a841SMartin KaFai Lau * 3514938c3efdSQuentin Monnet * **-EINVAL** If param is invalid. 35150813a841SMartin KaFai Lau * 3516938c3efdSQuentin Monnet * **-ENOSPC** if there is not enough space in the header. 35170813a841SMartin KaFai Lau * Nothing has been written 35180813a841SMartin KaFai Lau * 3519938c3efdSQuentin Monnet * **-EEXIST** if the option already exists. 35200813a841SMartin KaFai Lau * 3521938c3efdSQuentin Monnet * **-EFAULT** on failrue to parse the existing header options. 35220813a841SMartin KaFai Lau * 3523938c3efdSQuentin Monnet * **-EPERM** if the helper cannot be used under the current 3524938c3efdSQuentin Monnet * *skops*\ **->op**. 35250813a841SMartin KaFai Lau * 35260813a841SMartin KaFai Lau * long bpf_reserve_hdr_opt(struct bpf_sock_ops *skops, u32 len, u64 flags) 35270813a841SMartin KaFai Lau * Description 35280813a841SMartin KaFai Lau * Reserve *len* bytes for the bpf header option. The 3529938c3efdSQuentin Monnet * space will be used by **bpf_store_hdr_opt**\ () later in 3530938c3efdSQuentin Monnet * **BPF_SOCK_OPS_WRITE_HDR_OPT_CB**. 35310813a841SMartin KaFai Lau * 3532938c3efdSQuentin Monnet * If **bpf_reserve_hdr_opt**\ () is called multiple times, 35330813a841SMartin KaFai Lau * the total number of bytes will be reserved. 35340813a841SMartin KaFai Lau * 35350813a841SMartin KaFai Lau * This helper can only be called during 3536938c3efdSQuentin Monnet * **BPF_SOCK_OPS_HDR_OPT_LEN_CB**. 35370813a841SMartin KaFai Lau * 35380813a841SMartin KaFai Lau * Return 35390813a841SMartin KaFai Lau * 0 on success, or negative error in case of failure: 35400813a841SMartin KaFai Lau * 3541938c3efdSQuentin Monnet * **-EINVAL** if a parameter is invalid. 35420813a841SMartin KaFai Lau * 3543938c3efdSQuentin Monnet * **-ENOSPC** if there is not enough space in the header. 35440813a841SMartin KaFai Lau * 3545938c3efdSQuentin Monnet * **-EPERM** if the helper cannot be used under the current 3546938c3efdSQuentin Monnet * *skops*\ **->op**. 35476e22ab9dSJiri Olsa * 35488ea63684SKP Singh * void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags) 35498ea63684SKP Singh * Description 35508ea63684SKP Singh * Get a bpf_local_storage from an *inode*. 35518ea63684SKP Singh * 35528ea63684SKP Singh * Logically, it could be thought of as getting the value from 35538ea63684SKP Singh * a *map* with *inode* as the **key**. From this 35548ea63684SKP Singh * perspective, the usage is not much different from 35558ea63684SKP Singh * **bpf_map_lookup_elem**\ (*map*, **&**\ *inode*) except this 35568ea63684SKP Singh * helper enforces the key must be an inode and the map must also 35578ea63684SKP Singh * be a **BPF_MAP_TYPE_INODE_STORAGE**. 35588ea63684SKP Singh * 35598ea63684SKP Singh * Underneath, the value is stored locally at *inode* instead of 35608ea63684SKP Singh * the *map*. The *map* is used as the bpf-local-storage 35618ea63684SKP Singh * "type". The bpf-local-storage "type" (i.e. the *map*) is 35628ea63684SKP Singh * searched against all bpf_local_storage residing at *inode*. 35638ea63684SKP Singh * 35648ea63684SKP Singh * An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be 35658ea63684SKP Singh * used such that a new bpf_local_storage will be 35668ea63684SKP Singh * created if one does not exist. *value* can be used 35678ea63684SKP Singh * together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify 35688ea63684SKP Singh * the initial value of a bpf_local_storage. If *value* is 35698ea63684SKP Singh * **NULL**, the new bpf_local_storage will be zero initialized. 35708ea63684SKP Singh * Return 35718ea63684SKP Singh * A bpf_local_storage pointer is returned on success. 35728ea63684SKP Singh * 35738ea63684SKP Singh * **NULL** if not found or there was an error in adding 35748ea63684SKP Singh * a new bpf_local_storage. 35758ea63684SKP Singh * 35768ea63684SKP Singh * int bpf_inode_storage_delete(struct bpf_map *map, void *inode) 35778ea63684SKP Singh * Description 35788ea63684SKP Singh * Delete a bpf_local_storage from an *inode*. 35798ea63684SKP Singh * Return 35808ea63684SKP Singh * 0 on success. 35818ea63684SKP Singh * 35828ea63684SKP Singh * **-ENOENT** if the bpf_local_storage cannot be found. 35836e22ab9dSJiri Olsa * 35846e22ab9dSJiri Olsa * long bpf_d_path(struct path *path, char *buf, u32 sz) 35856e22ab9dSJiri Olsa * Description 3586938c3efdSQuentin Monnet * Return full path for given **struct path** object, which 3587938c3efdSQuentin Monnet * needs to be the kernel BTF *path* object. The path is 3588938c3efdSQuentin Monnet * returned in the provided buffer *buf* of size *sz* and 35896e22ab9dSJiri Olsa * is zero terminated. 35906e22ab9dSJiri Olsa * 35916e22ab9dSJiri Olsa * Return 35926e22ab9dSJiri Olsa * On success, the strictly positive length of the string, 35936e22ab9dSJiri Olsa * including the trailing NUL character. On error, a negative 35946e22ab9dSJiri Olsa * value. 359507be4c4aSAlexei Starovoitov * 359607be4c4aSAlexei Starovoitov * long bpf_copy_from_user(void *dst, u32 size, const void *user_ptr) 359707be4c4aSAlexei Starovoitov * Description 359807be4c4aSAlexei Starovoitov * Read *size* bytes from user space address *user_ptr* and store 3599938c3efdSQuentin Monnet * the data in *dst*. This is a wrapper of **copy_from_user**\ (). 360007be4c4aSAlexei Starovoitov * Return 360107be4c4aSAlexei Starovoitov * 0 on success, or a negative error in case of failure. 3602c4d0bfb4SAlan Maguire * 3603c4d0bfb4SAlan Maguire * long bpf_snprintf_btf(char *str, u32 str_size, struct btf_ptr *ptr, u32 btf_ptr_size, u64 flags) 3604c4d0bfb4SAlan Maguire * Description 3605c4d0bfb4SAlan Maguire * Use BTF to store a string representation of *ptr*->ptr in *str*, 3606c4d0bfb4SAlan Maguire * using *ptr*->type_id. This value should specify the type 3607c4d0bfb4SAlan Maguire * that *ptr*->ptr points to. LLVM __builtin_btf_type_id(type, 1) 3608c4d0bfb4SAlan Maguire * can be used to look up vmlinux BTF type ids. Traversing the 3609c4d0bfb4SAlan Maguire * data structure using BTF, the type information and values are 3610c4d0bfb4SAlan Maguire * stored in the first *str_size* - 1 bytes of *str*. Safe copy of 3611c4d0bfb4SAlan Maguire * the pointer data is carried out to avoid kernel crashes during 3612c4d0bfb4SAlan Maguire * operation. Smaller types can use string space on the stack; 3613c4d0bfb4SAlan Maguire * larger programs can use map data to store the string 3614c4d0bfb4SAlan Maguire * representation. 3615c4d0bfb4SAlan Maguire * 3616c4d0bfb4SAlan Maguire * The string can be subsequently shared with userspace via 3617c4d0bfb4SAlan Maguire * bpf_perf_event_output() or ring buffer interfaces. 3618c4d0bfb4SAlan Maguire * bpf_trace_printk() is to be avoided as it places too small 3619c4d0bfb4SAlan Maguire * a limit on string size to be useful. 3620c4d0bfb4SAlan Maguire * 3621c4d0bfb4SAlan Maguire * *flags* is a combination of 3622c4d0bfb4SAlan Maguire * 3623c4d0bfb4SAlan Maguire * **BTF_F_COMPACT** 3624c4d0bfb4SAlan Maguire * no formatting around type information 3625c4d0bfb4SAlan Maguire * **BTF_F_NONAME** 3626c4d0bfb4SAlan Maguire * no struct/union member names/types 3627c4d0bfb4SAlan Maguire * **BTF_F_PTR_RAW** 3628c4d0bfb4SAlan Maguire * show raw (unobfuscated) pointer values; 3629c4d0bfb4SAlan Maguire * equivalent to printk specifier %px. 3630c4d0bfb4SAlan Maguire * **BTF_F_ZERO** 3631c4d0bfb4SAlan Maguire * show zero-valued struct/union members; they 3632c4d0bfb4SAlan Maguire * are not displayed by default 3633c4d0bfb4SAlan Maguire * 3634c4d0bfb4SAlan Maguire * Return 3635c4d0bfb4SAlan Maguire * The number of bytes that were written (or would have been 3636c4d0bfb4SAlan Maguire * written if output had to be truncated due to string size), 3637c4d0bfb4SAlan Maguire * or a negative error in cases of failure. 3638eb411377SAlan Maguire * 3639eb411377SAlan Maguire * long bpf_seq_printf_btf(struct seq_file *m, struct btf_ptr *ptr, u32 ptr_size, u64 flags) 3640eb411377SAlan Maguire * Description 3641eb411377SAlan Maguire * Use BTF to write to seq_write a string representation of 3642eb411377SAlan Maguire * *ptr*->ptr, using *ptr*->type_id as per bpf_snprintf_btf(). 3643eb411377SAlan Maguire * *flags* are identical to those used for bpf_snprintf_btf. 3644eb411377SAlan Maguire * Return 3645eb411377SAlan Maguire * 0 on success or a negative error in case of failure. 36467a4b28c6SDaniel Borkmann */ 3647ebb676daSThomas Graf #define __BPF_FUNC_MAPPER(FN) \ 3648ebb676daSThomas Graf FN(unspec), \ 3649ebb676daSThomas Graf FN(map_lookup_elem), \ 3650ebb676daSThomas Graf FN(map_update_elem), \ 3651ebb676daSThomas Graf FN(map_delete_elem), \ 3652ebb676daSThomas Graf FN(probe_read), \ 3653ebb676daSThomas Graf FN(ktime_get_ns), \ 3654ebb676daSThomas Graf FN(trace_printk), \ 3655ebb676daSThomas Graf FN(get_prandom_u32), \ 3656ebb676daSThomas Graf FN(get_smp_processor_id), \ 3657ebb676daSThomas Graf FN(skb_store_bytes), \ 3658ebb676daSThomas Graf FN(l3_csum_replace), \ 3659ebb676daSThomas Graf FN(l4_csum_replace), \ 3660ebb676daSThomas Graf FN(tail_call), \ 3661ebb676daSThomas Graf FN(clone_redirect), \ 3662ebb676daSThomas Graf FN(get_current_pid_tgid), \ 3663ebb676daSThomas Graf FN(get_current_uid_gid), \ 3664ebb676daSThomas Graf FN(get_current_comm), \ 3665ebb676daSThomas Graf FN(get_cgroup_classid), \ 3666ebb676daSThomas Graf FN(skb_vlan_push), \ 3667ebb676daSThomas Graf FN(skb_vlan_pop), \ 3668ebb676daSThomas Graf FN(skb_get_tunnel_key), \ 3669ebb676daSThomas Graf FN(skb_set_tunnel_key), \ 3670ebb676daSThomas Graf FN(perf_event_read), \ 3671ebb676daSThomas Graf FN(redirect), \ 3672ebb676daSThomas Graf FN(get_route_realm), \ 3673ebb676daSThomas Graf FN(perf_event_output), \ 3674ebb676daSThomas Graf FN(skb_load_bytes), \ 3675ebb676daSThomas Graf FN(get_stackid), \ 3676ebb676daSThomas Graf FN(csum_diff), \ 3677ebb676daSThomas Graf FN(skb_get_tunnel_opt), \ 3678ebb676daSThomas Graf FN(skb_set_tunnel_opt), \ 3679ebb676daSThomas Graf FN(skb_change_proto), \ 3680ebb676daSThomas Graf FN(skb_change_type), \ 3681ebb676daSThomas Graf FN(skb_under_cgroup), \ 3682ebb676daSThomas Graf FN(get_hash_recalc), \ 3683ebb676daSThomas Graf FN(get_current_task), \ 3684ebb676daSThomas Graf FN(probe_write_user), \ 3685ebb676daSThomas Graf FN(current_task_under_cgroup), \ 3686ebb676daSThomas Graf FN(skb_change_tail), \ 3687ebb676daSThomas Graf FN(skb_pull_data), \ 3688ebb676daSThomas Graf FN(csum_update), \ 3689ebb676daSThomas Graf FN(set_hash_invalid), \ 36903a0af8fdSThomas Graf FN(get_numa_node_id), \ 369117bedab2SMartin KaFai Lau FN(skb_change_head), \ 3692a5e8c070SGianluca Borello FN(xdp_adjust_head), \ 369391b8270fSChenbo Feng FN(probe_read_str), \ 36946acc5c29SChenbo Feng FN(get_socket_cookie), \ 3695ded092cdSDaniel Borkmann FN(get_socket_uid), \ 36968c4b4c7eSLawrence Brakmo FN(set_hash), \ 36972be7e212SDaniel Borkmann FN(setsockopt), \ 369897f91a7cSJohn Fastabend FN(skb_adjust_room), \ 3699174a79ffSJohn Fastabend FN(redirect_map), \ 3700174a79ffSJohn Fastabend FN(sk_redirect_map), \ 3701174a79ffSJohn Fastabend FN(sock_map_update), \ 3702908432caSYonghong Song FN(xdp_adjust_meta), \ 37034bebdc7aSYonghong Song FN(perf_event_read_value), \ 3704cd86d1fdSLawrence Brakmo FN(perf_prog_read_value), \ 37059802d865SJosef Bacik FN(getsockopt), \ 3706b13d8807SLawrence Brakmo FN(override_return), \ 37074f738adbSJohn Fastabend FN(sock_ops_cb_flags_set), \ 37082a100317SJohn Fastabend FN(msg_redirect_map), \ 370991843d54SJohn Fastabend FN(msg_apply_bytes), \ 3710015632bbSJohn Fastabend FN(msg_cork_bytes), \ 3711d74bad4eSAndrey Ignatov FN(msg_pull_data), \ 3712b32cc5b9SNikita V. Shirokov FN(bind), \ 371312bed760SEyal Birger FN(xdp_adjust_tail), \ 3714c195651eSYonghong Song FN(skb_get_xfrm_state), \ 37154e1ec56cSDaniel Borkmann FN(get_stack), \ 371687f5fc7eSDavid Ahern FN(skb_load_bytes_relative), \ 371781110384SJohn Fastabend FN(fib_lookup), \ 371881110384SJohn Fastabend FN(sock_hash_update), \ 371981110384SJohn Fastabend FN(msg_redirect_hash), \ 3720fe94cc29SMathieu Xhonneux FN(sk_redirect_hash), \ 3721fe94cc29SMathieu Xhonneux FN(lwt_push_encap), \ 3722fe94cc29SMathieu Xhonneux FN(lwt_seg6_store_bytes), \ 3723fe94cc29SMathieu Xhonneux FN(lwt_seg6_adjust_srh), \ 3724f4364dcfSSean Young FN(lwt_seg6_action), \ 3725f4364dcfSSean Young FN(rc_repeat), \ 3726cb20b08eSDaniel Borkmann FN(rc_keydown), \ 3727bf6fa2c8SYonghong Song FN(skb_cgroup_id), \ 3728cd339431SRoman Gushchin FN(get_current_cgroup_id), \ 37292dbb9b9eSMartin KaFai Lau FN(get_local_storage), \ 373077236281SAndrey Ignatov FN(sk_select_reuseport), \ 37316acc9b43SJoe Stringer FN(skb_ancestor_cgroup_id), \ 37326acc9b43SJoe Stringer FN(sk_lookup_tcp), \ 37336acc9b43SJoe Stringer FN(sk_lookup_udp), \ 3734f1a2e44aSMauricio Vasquez B FN(sk_release), \ 3735f1a2e44aSMauricio Vasquez B FN(map_push_elem), \ 3736f1a2e44aSMauricio Vasquez B FN(map_pop_elem), \ 37376fff607eSJohn Fastabend FN(map_peek_elem), \ 37387246d8edSJohn Fastabend FN(msg_push_data), \ 373901d3240aSSean Young FN(msg_pop_data), \ 3740d83525caSAlexei Starovoitov FN(rc_pointer_rel), \ 3741d83525caSAlexei Starovoitov FN(spin_lock), \ 374246f8bc92SMartin KaFai Lau FN(spin_unlock), \ 3743655a51e5SMartin KaFai Lau FN(sk_fullsock), \ 3744f7c917baSbrakmo FN(tcp_sock), \ 3745dbafd7ddSMartin KaFai Lau FN(skb_ecn_set_ce), \ 3746edbf8c01SLorenz Bauer FN(get_listener_sock), \ 374739904084SLorenz Bauer FN(skc_lookup_tcp), \ 3748808649fbSAndrey Ignatov FN(tcp_check_syncookie), \ 37491d11b301SAndrey Ignatov FN(sysctl_get_name), \ 37504e63acdfSAndrey Ignatov FN(sysctl_get_current_value), \ 37514e63acdfSAndrey Ignatov FN(sysctl_get_new_value), \ 3752d7a4cb9bSAndrey Ignatov FN(sysctl_set_new_value), \ 3753d7a4cb9bSAndrey Ignatov FN(strtol), \ 37546ac99e8fSMartin KaFai Lau FN(strtoul), \ 37556ac99e8fSMartin KaFai Lau FN(sk_storage_get), \ 37568b401f9eSYonghong Song FN(sk_storage_delete), \ 375770d66244SPetar Penkov FN(send_signal), \ 3758a7658e1aSAlexei Starovoitov FN(tcp_gen_syncookie), \ 37596ae08ae3SDaniel Borkmann FN(skb_output), \ 37606ae08ae3SDaniel Borkmann FN(probe_read_user), \ 37616ae08ae3SDaniel Borkmann FN(probe_read_kernel), \ 37626ae08ae3SDaniel Borkmann FN(probe_read_user_str), \ 3763206057feSMartin KaFai Lau FN(probe_read_kernel_str), \ 37648482941fSYonghong Song FN(tcp_send_ack), \ 37655576b991SMartin KaFai Lau FN(send_signal_thread), \ 3766fff7b643SDaniel Xu FN(jiffies64), \ 3767b4490c5cSCarlos Neira FN(read_branch_records), \ 3768d831ee84SEelco Chaudron FN(get_ns_current_pid_tgid), \ 3769f318903cSDaniel Borkmann FN(xdp_output), \ 37700f09abd1SDaniel Borkmann FN(get_netns_cookie), \ 3771cf7fbe66SJoe Stringer FN(get_current_ancestor_cgroup_id), \ 377271d19214SMaciej Żenczykowski FN(sk_assign), \ 3773492e639fSYonghong Song FN(ktime_get_boot_ns), \ 3774492e639fSYonghong Song FN(seq_printf), \ 3775f307fa2cSAndrey Ignatov FN(seq_write), \ 3776f307fa2cSAndrey Ignatov FN(sk_cgroup_id), \ 3777457f4436SAndrii Nakryiko FN(sk_ancestor_cgroup_id), \ 3778457f4436SAndrii Nakryiko FN(ringbuf_output), \ 3779457f4436SAndrii Nakryiko FN(ringbuf_reserve), \ 3780457f4436SAndrii Nakryiko FN(ringbuf_submit), \ 3781457f4436SAndrii Nakryiko FN(ringbuf_discard), \ 37827cdec54fSDaniel Borkmann FN(ringbuf_query), \ 3783af7ec138SYonghong Song FN(csum_level), \ 3784478cfbdfSYonghong Song FN(skc_to_tcp6_sock), \ 3785478cfbdfSYonghong Song FN(skc_to_tcp_sock), \ 3786478cfbdfSYonghong Song FN(skc_to_tcp_timewait_sock), \ 37870d4fad3eSYonghong Song FN(skc_to_tcp_request_sock), \ 3788fa28dcb8SSong Liu FN(skc_to_udp6_sock), \ 3789fa28dcb8SSong Liu FN(get_task_stack), \ 37900813a841SMartin KaFai Lau FN(load_hdr_opt), \ 37910813a841SMartin KaFai Lau FN(store_hdr_opt), \ 37928ea63684SKP Singh FN(reserve_hdr_opt), \ 37938ea63684SKP Singh FN(inode_storage_get), \ 37948ea63684SKP Singh FN(inode_storage_delete), \ 37956e22ab9dSJiri Olsa FN(d_path), \ 379607be4c4aSAlexei Starovoitov FN(copy_from_user), \ 3797c4d0bfb4SAlan Maguire FN(snprintf_btf), \ 3798eb411377SAlan Maguire FN(seq_printf_btf), \ 3799fa28dcb8SSong Liu /* */ 38007a4b28c6SDaniel Borkmann 3801ebb676daSThomas Graf /* integer value in 'imm' field of BPF_CALL instruction selects which helper 3802ebb676daSThomas Graf * function eBPF program intends to call 38032d0e30c3SDaniel Borkmann */ 3804ebb676daSThomas Graf #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x 3805ebb676daSThomas Graf enum bpf_func_id { 3806ebb676daSThomas Graf __BPF_FUNC_MAPPER(__BPF_ENUM_FN) 380709756af4SAlexei Starovoitov __BPF_FUNC_MAX_ID, 380809756af4SAlexei Starovoitov }; 3809ebb676daSThomas Graf #undef __BPF_ENUM_FN 381009756af4SAlexei Starovoitov 3811781c53bcSDaniel Borkmann /* All flags used by eBPF helper functions, placed here. */ 3812781c53bcSDaniel Borkmann 3813781c53bcSDaniel Borkmann /* BPF_FUNC_skb_store_bytes flags. */ 38141aae4bddSAndrii Nakryiko enum { 38151aae4bddSAndrii Nakryiko BPF_F_RECOMPUTE_CSUM = (1ULL << 0), 38161aae4bddSAndrii Nakryiko BPF_F_INVALIDATE_HASH = (1ULL << 1), 38171aae4bddSAndrii Nakryiko }; 3818781c53bcSDaniel Borkmann 3819781c53bcSDaniel Borkmann /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags. 3820781c53bcSDaniel Borkmann * First 4 bits are for passing the header field size. 3821781c53bcSDaniel Borkmann */ 38221aae4bddSAndrii Nakryiko enum { 38231aae4bddSAndrii Nakryiko BPF_F_HDR_FIELD_MASK = 0xfULL, 38241aae4bddSAndrii Nakryiko }; 3825781c53bcSDaniel Borkmann 3826781c53bcSDaniel Borkmann /* BPF_FUNC_l4_csum_replace flags. */ 38271aae4bddSAndrii Nakryiko enum { 38281aae4bddSAndrii Nakryiko BPF_F_PSEUDO_HDR = (1ULL << 4), 38291aae4bddSAndrii Nakryiko BPF_F_MARK_MANGLED_0 = (1ULL << 5), 38301aae4bddSAndrii Nakryiko BPF_F_MARK_ENFORCE = (1ULL << 6), 38311aae4bddSAndrii Nakryiko }; 3832781c53bcSDaniel Borkmann 3833781c53bcSDaniel Borkmann /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */ 38341aae4bddSAndrii Nakryiko enum { 38351aae4bddSAndrii Nakryiko BPF_F_INGRESS = (1ULL << 0), 38361aae4bddSAndrii Nakryiko }; 3837781c53bcSDaniel Borkmann 3838c6c33454SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */ 38391aae4bddSAndrii Nakryiko enum { 38401aae4bddSAndrii Nakryiko BPF_F_TUNINFO_IPV6 = (1ULL << 0), 38411aae4bddSAndrii Nakryiko }; 3842c6c33454SDaniel Borkmann 3843c195651eSYonghong Song /* flags for both BPF_FUNC_get_stackid and BPF_FUNC_get_stack. */ 38441aae4bddSAndrii Nakryiko enum { 38451aae4bddSAndrii Nakryiko BPF_F_SKIP_FIELD_MASK = 0xffULL, 38461aae4bddSAndrii Nakryiko BPF_F_USER_STACK = (1ULL << 8), 3847c195651eSYonghong Song /* flags used by BPF_FUNC_get_stackid only. */ 38481aae4bddSAndrii Nakryiko BPF_F_FAST_STACK_CMP = (1ULL << 9), 38491aae4bddSAndrii Nakryiko BPF_F_REUSE_STACKID = (1ULL << 10), 3850c195651eSYonghong Song /* flags used by BPF_FUNC_get_stack only. */ 38511aae4bddSAndrii Nakryiko BPF_F_USER_BUILD_ID = (1ULL << 11), 38521aae4bddSAndrii Nakryiko }; 3853d5a3b1f6SAlexei Starovoitov 38542da897e5SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key flags. */ 38551aae4bddSAndrii Nakryiko enum { 38561aae4bddSAndrii Nakryiko BPF_F_ZERO_CSUM_TX = (1ULL << 1), 38571aae4bddSAndrii Nakryiko BPF_F_DONT_FRAGMENT = (1ULL << 2), 38581aae4bddSAndrii Nakryiko BPF_F_SEQ_NUMBER = (1ULL << 3), 38591aae4bddSAndrii Nakryiko }; 38602da897e5SDaniel Borkmann 3861908432caSYonghong Song /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and 3862908432caSYonghong Song * BPF_FUNC_perf_event_read_value flags. 3863908432caSYonghong Song */ 38641aae4bddSAndrii Nakryiko enum { 38651aae4bddSAndrii Nakryiko BPF_F_INDEX_MASK = 0xffffffffULL, 38661aae4bddSAndrii Nakryiko BPF_F_CURRENT_CPU = BPF_F_INDEX_MASK, 3867555c8a86SDaniel Borkmann /* BPF_FUNC_perf_event_output for sk_buff input context. */ 38681aae4bddSAndrii Nakryiko BPF_F_CTXLEN_MASK = (0xfffffULL << 32), 38691aae4bddSAndrii Nakryiko }; 38701e33759cSDaniel Borkmann 3871f71c6143SJoe Stringer /* Current network namespace */ 38721aae4bddSAndrii Nakryiko enum { 38731aae4bddSAndrii Nakryiko BPF_F_CURRENT_NETNS = (-1L), 38741aae4bddSAndrii Nakryiko }; 3875f71c6143SJoe Stringer 38767cdec54fSDaniel Borkmann /* BPF_FUNC_csum_level level values. */ 38777cdec54fSDaniel Borkmann enum { 38787cdec54fSDaniel Borkmann BPF_CSUM_LEVEL_QUERY, 38797cdec54fSDaniel Borkmann BPF_CSUM_LEVEL_INC, 38807cdec54fSDaniel Borkmann BPF_CSUM_LEVEL_DEC, 38817cdec54fSDaniel Borkmann BPF_CSUM_LEVEL_RESET, 38827cdec54fSDaniel Borkmann }; 38837cdec54fSDaniel Borkmann 38842278f6ccSWillem de Bruijn /* BPF_FUNC_skb_adjust_room flags. */ 38851aae4bddSAndrii Nakryiko enum { 38861aae4bddSAndrii Nakryiko BPF_F_ADJ_ROOM_FIXED_GSO = (1ULL << 0), 38871aae4bddSAndrii Nakryiko BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = (1ULL << 1), 38881aae4bddSAndrii Nakryiko BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = (1ULL << 2), 38891aae4bddSAndrii Nakryiko BPF_F_ADJ_ROOM_ENCAP_L4_GRE = (1ULL << 3), 38901aae4bddSAndrii Nakryiko BPF_F_ADJ_ROOM_ENCAP_L4_UDP = (1ULL << 4), 3891836e66c2SDaniel Borkmann BPF_F_ADJ_ROOM_NO_CSUM_RESET = (1ULL << 5), 38921aae4bddSAndrii Nakryiko }; 38932278f6ccSWillem de Bruijn 38941aae4bddSAndrii Nakryiko enum { 38951aae4bddSAndrii Nakryiko BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff, 38961aae4bddSAndrii Nakryiko BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 56, 38971aae4bddSAndrii Nakryiko }; 389858dfc900SAlan Maguire 389958dfc900SAlan Maguire #define BPF_F_ADJ_ROOM_ENCAP_L2(len) (((__u64)len & \ 390058dfc900SAlan Maguire BPF_ADJ_ROOM_ENCAP_L2_MASK) \ 390158dfc900SAlan Maguire << BPF_ADJ_ROOM_ENCAP_L2_SHIFT) 3902868d5235SWillem de Bruijn 3903808649fbSAndrey Ignatov /* BPF_FUNC_sysctl_get_name flags. */ 39041aae4bddSAndrii Nakryiko enum { 39051aae4bddSAndrii Nakryiko BPF_F_SYSCTL_BASE_NAME = (1ULL << 0), 39061aae4bddSAndrii Nakryiko }; 3907808649fbSAndrey Ignatov 3908f836a56eSKP Singh /* BPF_FUNC_<kernel_obj>_storage_get flags */ 39091aae4bddSAndrii Nakryiko enum { 3910f836a56eSKP Singh BPF_LOCAL_STORAGE_GET_F_CREATE = (1ULL << 0), 3911f836a56eSKP Singh /* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility 3912f836a56eSKP Singh * and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead. 3913f836a56eSKP Singh */ 3914f836a56eSKP Singh BPF_SK_STORAGE_GET_F_CREATE = BPF_LOCAL_STORAGE_GET_F_CREATE, 39151aae4bddSAndrii Nakryiko }; 39166ac99e8fSMartin KaFai Lau 3917fff7b643SDaniel Xu /* BPF_FUNC_read_branch_records flags. */ 39181aae4bddSAndrii Nakryiko enum { 39191aae4bddSAndrii Nakryiko BPF_F_GET_BRANCH_RECORDS_SIZE = (1ULL << 0), 39201aae4bddSAndrii Nakryiko }; 3921fff7b643SDaniel Xu 3922457f4436SAndrii Nakryiko /* BPF_FUNC_bpf_ringbuf_commit, BPF_FUNC_bpf_ringbuf_discard, and 3923457f4436SAndrii Nakryiko * BPF_FUNC_bpf_ringbuf_output flags. 3924457f4436SAndrii Nakryiko */ 3925457f4436SAndrii Nakryiko enum { 3926457f4436SAndrii Nakryiko BPF_RB_NO_WAKEUP = (1ULL << 0), 3927457f4436SAndrii Nakryiko BPF_RB_FORCE_WAKEUP = (1ULL << 1), 3928457f4436SAndrii Nakryiko }; 3929457f4436SAndrii Nakryiko 3930457f4436SAndrii Nakryiko /* BPF_FUNC_bpf_ringbuf_query flags */ 3931457f4436SAndrii Nakryiko enum { 3932457f4436SAndrii Nakryiko BPF_RB_AVAIL_DATA = 0, 3933457f4436SAndrii Nakryiko BPF_RB_RING_SIZE = 1, 3934457f4436SAndrii Nakryiko BPF_RB_CONS_POS = 2, 3935457f4436SAndrii Nakryiko BPF_RB_PROD_POS = 3, 3936457f4436SAndrii Nakryiko }; 3937457f4436SAndrii Nakryiko 3938457f4436SAndrii Nakryiko /* BPF ring buffer constants */ 3939457f4436SAndrii Nakryiko enum { 3940457f4436SAndrii Nakryiko BPF_RINGBUF_BUSY_BIT = (1U << 31), 3941457f4436SAndrii Nakryiko BPF_RINGBUF_DISCARD_BIT = (1U << 30), 3942457f4436SAndrii Nakryiko BPF_RINGBUF_HDR_SZ = 8, 3943457f4436SAndrii Nakryiko }; 3944457f4436SAndrii Nakryiko 3945e9ddbb77SJakub Sitnicki /* BPF_FUNC_sk_assign flags in bpf_sk_lookup context. */ 3946e9ddbb77SJakub Sitnicki enum { 3947e9ddbb77SJakub Sitnicki BPF_SK_LOOKUP_F_REPLACE = (1ULL << 0), 3948e9ddbb77SJakub Sitnicki BPF_SK_LOOKUP_F_NO_REUSEPORT = (1ULL << 1), 3949e9ddbb77SJakub Sitnicki }; 3950e9ddbb77SJakub Sitnicki 39512be7e212SDaniel Borkmann /* Mode for BPF_FUNC_skb_adjust_room helper. */ 39522be7e212SDaniel Borkmann enum bpf_adj_room_mode { 39532be7e212SDaniel Borkmann BPF_ADJ_ROOM_NET, 395414aa3192SWillem de Bruijn BPF_ADJ_ROOM_MAC, 39552be7e212SDaniel Borkmann }; 39562be7e212SDaniel Borkmann 39574e1ec56cSDaniel Borkmann /* Mode for BPF_FUNC_skb_load_bytes_relative helper. */ 39584e1ec56cSDaniel Borkmann enum bpf_hdr_start_off { 39594e1ec56cSDaniel Borkmann BPF_HDR_START_MAC, 39604e1ec56cSDaniel Borkmann BPF_HDR_START_NET, 39614e1ec56cSDaniel Borkmann }; 39624e1ec56cSDaniel Borkmann 3963fe94cc29SMathieu Xhonneux /* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */ 3964fe94cc29SMathieu Xhonneux enum bpf_lwt_encap_mode { 3965fe94cc29SMathieu Xhonneux BPF_LWT_ENCAP_SEG6, 39663e0bd37cSPeter Oskolkov BPF_LWT_ENCAP_SEG6_INLINE, 39673e0bd37cSPeter Oskolkov BPF_LWT_ENCAP_IP, 3968fe94cc29SMathieu Xhonneux }; 3969fe94cc29SMathieu Xhonneux 3970b7df9adaSDaniel Borkmann #define __bpf_md_ptr(type, name) \ 3971b7df9adaSDaniel Borkmann union { \ 3972b7df9adaSDaniel Borkmann type name; \ 3973b7df9adaSDaniel Borkmann __u64 :64; \ 3974b7df9adaSDaniel Borkmann } __attribute__((aligned(8))) 3975b7df9adaSDaniel Borkmann 39769bac3d6dSAlexei Starovoitov /* user accessible mirror of in-kernel sk_buff. 39779bac3d6dSAlexei Starovoitov * new fields can only be added to the end of this structure 39789bac3d6dSAlexei Starovoitov */ 39799bac3d6dSAlexei Starovoitov struct __sk_buff { 39809bac3d6dSAlexei Starovoitov __u32 len; 39819bac3d6dSAlexei Starovoitov __u32 pkt_type; 39829bac3d6dSAlexei Starovoitov __u32 mark; 39839bac3d6dSAlexei Starovoitov __u32 queue_mapping; 3984c2497395SAlexei Starovoitov __u32 protocol; 3985c2497395SAlexei Starovoitov __u32 vlan_present; 3986c2497395SAlexei Starovoitov __u32 vlan_tci; 398727cd5452SMichal Sekletar __u32 vlan_proto; 3988bcad5718SDaniel Borkmann __u32 priority; 398937e82c2fSAlexei Starovoitov __u32 ingress_ifindex; 399037e82c2fSAlexei Starovoitov __u32 ifindex; 3991d691f9e8SAlexei Starovoitov __u32 tc_index; 3992d691f9e8SAlexei Starovoitov __u32 cb[5]; 3993ba7591d8SDaniel Borkmann __u32 hash; 3994045efa82SDaniel Borkmann __u32 tc_classid; 3995969bf05eSAlexei Starovoitov __u32 data; 3996969bf05eSAlexei Starovoitov __u32 data_end; 3997b1d9fc41SDaniel Borkmann __u32 napi_id; 39988a31db56SJohn Fastabend 3999de8f3a83SDaniel Borkmann /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */ 40008a31db56SJohn Fastabend __u32 family; 40018a31db56SJohn Fastabend __u32 remote_ip4; /* Stored in network byte order */ 40028a31db56SJohn Fastabend __u32 local_ip4; /* Stored in network byte order */ 40038a31db56SJohn Fastabend __u32 remote_ip6[4]; /* Stored in network byte order */ 40048a31db56SJohn Fastabend __u32 local_ip6[4]; /* Stored in network byte order */ 40058a31db56SJohn Fastabend __u32 remote_port; /* Stored in network byte order */ 40068a31db56SJohn Fastabend __u32 local_port; /* stored in host byte order */ 4007de8f3a83SDaniel Borkmann /* ... here. */ 4008de8f3a83SDaniel Borkmann 4009de8f3a83SDaniel Borkmann __u32 data_meta; 4010b7df9adaSDaniel Borkmann __bpf_md_ptr(struct bpf_flow_keys *, flow_keys); 4011f11216b2SVlad Dumitrescu __u64 tstamp; 4012e3da08d0SPetar Penkov __u32 wire_len; 4013d9ff286aSEric Dumazet __u32 gso_segs; 401446f8bc92SMartin KaFai Lau __bpf_md_ptr(struct bpf_sock *, sk); 4015cf62089bSWillem de Bruijn __u32 gso_size; 40169bac3d6dSAlexei Starovoitov }; 40179bac3d6dSAlexei Starovoitov 4018d3aa45ceSAlexei Starovoitov struct bpf_tunnel_key { 4019d3aa45ceSAlexei Starovoitov __u32 tunnel_id; 4020c6c33454SDaniel Borkmann union { 4021d3aa45ceSAlexei Starovoitov __u32 remote_ipv4; 4022c6c33454SDaniel Borkmann __u32 remote_ipv6[4]; 4023c6c33454SDaniel Borkmann }; 4024c6c33454SDaniel Borkmann __u8 tunnel_tos; 4025c6c33454SDaniel Borkmann __u8 tunnel_ttl; 40261fbc2e0cSDaniel Borkmann __u16 tunnel_ext; /* Padding, future use. */ 40274018ab18SDaniel Borkmann __u32 tunnel_label; 4028d3aa45ceSAlexei Starovoitov }; 4029d3aa45ceSAlexei Starovoitov 403012bed760SEyal Birger /* user accessible mirror of in-kernel xfrm_state. 403112bed760SEyal Birger * new fields can only be added to the end of this structure 403212bed760SEyal Birger */ 403312bed760SEyal Birger struct bpf_xfrm_state { 403412bed760SEyal Birger __u32 reqid; 403512bed760SEyal Birger __u32 spi; /* Stored in network byte order */ 403612bed760SEyal Birger __u16 family; 40371fbc2e0cSDaniel Borkmann __u16 ext; /* Padding, future use. */ 403812bed760SEyal Birger union { 403912bed760SEyal Birger __u32 remote_ipv4; /* Stored in network byte order */ 404012bed760SEyal Birger __u32 remote_ipv6[4]; /* Stored in network byte order */ 404112bed760SEyal Birger }; 404212bed760SEyal Birger }; 404312bed760SEyal Birger 40443a0af8fdSThomas Graf /* Generic BPF return codes which all BPF program types may support. 40453a0af8fdSThomas Graf * The values are binary compatible with their TC_ACT_* counter-part to 40463a0af8fdSThomas Graf * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT 40473a0af8fdSThomas Graf * programs. 40483a0af8fdSThomas Graf * 40493a0af8fdSThomas Graf * XDP is handled seprately, see XDP_*. 40503a0af8fdSThomas Graf */ 40513a0af8fdSThomas Graf enum bpf_ret_code { 40523a0af8fdSThomas Graf BPF_OK = 0, 40533a0af8fdSThomas Graf /* 1 reserved */ 40543a0af8fdSThomas Graf BPF_DROP = 2, 40553a0af8fdSThomas Graf /* 3-6 reserved */ 40563a0af8fdSThomas Graf BPF_REDIRECT = 7, 40573e0bd37cSPeter Oskolkov /* >127 are reserved for prog type specific return codes. 40583e0bd37cSPeter Oskolkov * 40593e0bd37cSPeter Oskolkov * BPF_LWT_REROUTE: used by BPF_PROG_TYPE_LWT_IN and 40603e0bd37cSPeter Oskolkov * BPF_PROG_TYPE_LWT_XMIT to indicate that skb had been 40613e0bd37cSPeter Oskolkov * changed and should be routed based on its new L3 header. 40623e0bd37cSPeter Oskolkov * (This is an L3 redirect, as opposed to L2 redirect 40633e0bd37cSPeter Oskolkov * represented by BPF_REDIRECT above). 40643e0bd37cSPeter Oskolkov */ 40653e0bd37cSPeter Oskolkov BPF_LWT_REROUTE = 128, 40663a0af8fdSThomas Graf }; 40673a0af8fdSThomas Graf 406861023658SDavid Ahern struct bpf_sock { 406961023658SDavid Ahern __u32 bound_dev_if; 4070aa4c1037SDavid Ahern __u32 family; 4071aa4c1037SDavid Ahern __u32 type; 4072aa4c1037SDavid Ahern __u32 protocol; 4073482dca93SDavid Ahern __u32 mark; 4074482dca93SDavid Ahern __u32 priority; 4075aa65d696SMartin KaFai Lau /* IP address also allows 1 and 2 bytes access */ 4076aa65d696SMartin KaFai Lau __u32 src_ip4; 4077aa65d696SMartin KaFai Lau __u32 src_ip6[4]; 4078aa65d696SMartin KaFai Lau __u32 src_port; /* host byte order */ 4079aa65d696SMartin KaFai Lau __u32 dst_port; /* network byte order */ 4080aa65d696SMartin KaFai Lau __u32 dst_ip4; 4081aa65d696SMartin KaFai Lau __u32 dst_ip6[4]; 4082aa65d696SMartin KaFai Lau __u32 state; 4083c3c16f2eSAmritha Nambiar __s32 rx_queue_mapping; 408461023658SDavid Ahern }; 408561023658SDavid Ahern 4086655a51e5SMartin KaFai Lau struct bpf_tcp_sock { 4087655a51e5SMartin KaFai Lau __u32 snd_cwnd; /* Sending congestion window */ 4088655a51e5SMartin KaFai Lau __u32 srtt_us; /* smoothed round trip time << 3 in usecs */ 4089655a51e5SMartin KaFai Lau __u32 rtt_min; 4090655a51e5SMartin KaFai Lau __u32 snd_ssthresh; /* Slow start size threshold */ 4091655a51e5SMartin KaFai Lau __u32 rcv_nxt; /* What we want to receive next */ 4092655a51e5SMartin KaFai Lau __u32 snd_nxt; /* Next sequence we send */ 4093655a51e5SMartin KaFai Lau __u32 snd_una; /* First byte we want an ack for */ 4094655a51e5SMartin KaFai Lau __u32 mss_cache; /* Cached effective mss, not including SACKS */ 4095655a51e5SMartin KaFai Lau __u32 ecn_flags; /* ECN status bits. */ 4096655a51e5SMartin KaFai Lau __u32 rate_delivered; /* saved rate sample: packets delivered */ 4097655a51e5SMartin KaFai Lau __u32 rate_interval_us; /* saved rate sample: time elapsed */ 4098655a51e5SMartin KaFai Lau __u32 packets_out; /* Packets which are "in flight" */ 4099655a51e5SMartin KaFai Lau __u32 retrans_out; /* Retransmitted packets out */ 4100655a51e5SMartin KaFai Lau __u32 total_retrans; /* Total retransmits for entire connection */ 4101655a51e5SMartin KaFai Lau __u32 segs_in; /* RFC4898 tcpEStatsPerfSegsIn 4102655a51e5SMartin KaFai Lau * total number of segments in. 4103655a51e5SMartin KaFai Lau */ 4104655a51e5SMartin KaFai Lau __u32 data_segs_in; /* RFC4898 tcpEStatsPerfDataSegsIn 4105655a51e5SMartin KaFai Lau * total number of data segments in. 4106655a51e5SMartin KaFai Lau */ 4107655a51e5SMartin KaFai Lau __u32 segs_out; /* RFC4898 tcpEStatsPerfSegsOut 4108655a51e5SMartin KaFai Lau * The total number of segments sent. 4109655a51e5SMartin KaFai Lau */ 4110655a51e5SMartin KaFai Lau __u32 data_segs_out; /* RFC4898 tcpEStatsPerfDataSegsOut 4111655a51e5SMartin KaFai Lau * total number of data segments sent. 4112655a51e5SMartin KaFai Lau */ 4113655a51e5SMartin KaFai Lau __u32 lost_out; /* Lost packets */ 4114655a51e5SMartin KaFai Lau __u32 sacked_out; /* SACK'd packets */ 4115655a51e5SMartin KaFai Lau __u64 bytes_received; /* RFC4898 tcpEStatsAppHCThruOctetsReceived 4116655a51e5SMartin KaFai Lau * sum(delta(rcv_nxt)), or how many bytes 4117655a51e5SMartin KaFai Lau * were acked. 4118655a51e5SMartin KaFai Lau */ 4119655a51e5SMartin KaFai Lau __u64 bytes_acked; /* RFC4898 tcpEStatsAppHCThruOctetsAcked 4120655a51e5SMartin KaFai Lau * sum(delta(snd_una)), or how many bytes 4121655a51e5SMartin KaFai Lau * were acked. 4122655a51e5SMartin KaFai Lau */ 41230357746dSStanislav Fomichev __u32 dsack_dups; /* RFC4898 tcpEStatsStackDSACKDups 41240357746dSStanislav Fomichev * total number of DSACK blocks received 41250357746dSStanislav Fomichev */ 41260357746dSStanislav Fomichev __u32 delivered; /* Total data packets delivered incl. rexmits */ 41270357746dSStanislav Fomichev __u32 delivered_ce; /* Like the above but only ECE marked packets */ 4128c2cb5e82SStanislav Fomichev __u32 icsk_retransmits; /* Number of unrecovered [RTO] timeouts */ 4129655a51e5SMartin KaFai Lau }; 4130655a51e5SMartin KaFai Lau 41316acc9b43SJoe Stringer struct bpf_sock_tuple { 41326acc9b43SJoe Stringer union { 41336acc9b43SJoe Stringer struct { 41346acc9b43SJoe Stringer __be32 saddr; 41356acc9b43SJoe Stringer __be32 daddr; 41366acc9b43SJoe Stringer __be16 sport; 41376acc9b43SJoe Stringer __be16 dport; 41386acc9b43SJoe Stringer } ipv4; 41396acc9b43SJoe Stringer struct { 41406acc9b43SJoe Stringer __be32 saddr[4]; 41416acc9b43SJoe Stringer __be32 daddr[4]; 41426acc9b43SJoe Stringer __be16 sport; 41436acc9b43SJoe Stringer __be16 dport; 41446acc9b43SJoe Stringer } ipv6; 41456acc9b43SJoe Stringer }; 41466acc9b43SJoe Stringer }; 41476acc9b43SJoe Stringer 4148fada7fdcSJonathan Lemon struct bpf_xdp_sock { 4149fada7fdcSJonathan Lemon __u32 queue_id; 4150fada7fdcSJonathan Lemon }; 4151fada7fdcSJonathan Lemon 415217bedab2SMartin KaFai Lau #define XDP_PACKET_HEADROOM 256 415317bedab2SMartin KaFai Lau 41546a773a15SBrenden Blanco /* User return codes for XDP prog type. 41556a773a15SBrenden Blanco * A valid XDP program must return one of these defined values. All other 41569beb8bedSDaniel Borkmann * return codes are reserved for future use. Unknown return codes will 41579beb8bedSDaniel Borkmann * result in packet drops and a warning via bpf_warn_invalid_xdp_action(). 41586a773a15SBrenden Blanco */ 41596a773a15SBrenden Blanco enum xdp_action { 41606a773a15SBrenden Blanco XDP_ABORTED = 0, 41616a773a15SBrenden Blanco XDP_DROP, 41626a773a15SBrenden Blanco XDP_PASS, 41636ce96ca3SBrenden Blanco XDP_TX, 4164814abfabSJohn Fastabend XDP_REDIRECT, 41656a773a15SBrenden Blanco }; 41666a773a15SBrenden Blanco 41676a773a15SBrenden Blanco /* user accessible metadata for XDP packet hook 41686a773a15SBrenden Blanco * new fields must be added to the end of this structure 41696a773a15SBrenden Blanco */ 41706a773a15SBrenden Blanco struct xdp_md { 41716a773a15SBrenden Blanco __u32 data; 41726a773a15SBrenden Blanco __u32 data_end; 4173de8f3a83SDaniel Borkmann __u32 data_meta; 4174daaf24c6SJesper Dangaard Brouer /* Below access go through struct xdp_rxq_info */ 417502dd3291SJesper Dangaard Brouer __u32 ingress_ifindex; /* rxq->dev->ifindex */ 417602dd3291SJesper Dangaard Brouer __u32 rx_queue_index; /* rxq->queue_index */ 417764b59025SDavid Ahern 417864b59025SDavid Ahern __u32 egress_ifindex; /* txq->dev->ifindex */ 41796a773a15SBrenden Blanco }; 41806a773a15SBrenden Blanco 4181281920b7SJesper Dangaard Brouer /* DEVMAP map-value layout 4182281920b7SJesper Dangaard Brouer * 4183281920b7SJesper Dangaard Brouer * The struct data-layout of map-value is a configuration interface. 4184281920b7SJesper Dangaard Brouer * New members can only be added to the end of this structure. 4185281920b7SJesper Dangaard Brouer */ 4186281920b7SJesper Dangaard Brouer struct bpf_devmap_val { 4187281920b7SJesper Dangaard Brouer __u32 ifindex; /* device index */ 4188281920b7SJesper Dangaard Brouer union { 4189281920b7SJesper Dangaard Brouer int fd; /* prog fd on map write */ 4190281920b7SJesper Dangaard Brouer __u32 id; /* prog id on map read */ 4191281920b7SJesper Dangaard Brouer } bpf_prog; 4192281920b7SJesper Dangaard Brouer }; 4193281920b7SJesper Dangaard Brouer 4194644bfe51SLorenzo Bianconi /* CPUMAP map-value layout 4195644bfe51SLorenzo Bianconi * 4196644bfe51SLorenzo Bianconi * The struct data-layout of map-value is a configuration interface. 4197644bfe51SLorenzo Bianconi * New members can only be added to the end of this structure. 4198644bfe51SLorenzo Bianconi */ 4199644bfe51SLorenzo Bianconi struct bpf_cpumap_val { 4200644bfe51SLorenzo Bianconi __u32 qsize; /* queue size to remote target CPU */ 420192164774SLorenzo Bianconi union { 420292164774SLorenzo Bianconi int fd; /* prog fd on map write */ 420392164774SLorenzo Bianconi __u32 id; /* prog id on map read */ 420492164774SLorenzo Bianconi } bpf_prog; 4205644bfe51SLorenzo Bianconi }; 4206644bfe51SLorenzo Bianconi 4207174a79ffSJohn Fastabend enum sk_action { 4208bfa64075SJohn Fastabend SK_DROP = 0, 4209bfa64075SJohn Fastabend SK_PASS, 4210174a79ffSJohn Fastabend }; 4211174a79ffSJohn Fastabend 42124f738adbSJohn Fastabend /* user accessible metadata for SK_MSG packet hook, new fields must 42134f738adbSJohn Fastabend * be added to the end of this structure 42144f738adbSJohn Fastabend */ 42154f738adbSJohn Fastabend struct sk_msg_md { 4216b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data); 4217b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data_end); 4218303def35SJohn Fastabend 4219303def35SJohn Fastabend __u32 family; 4220303def35SJohn Fastabend __u32 remote_ip4; /* Stored in network byte order */ 4221303def35SJohn Fastabend __u32 local_ip4; /* Stored in network byte order */ 4222303def35SJohn Fastabend __u32 remote_ip6[4]; /* Stored in network byte order */ 4223303def35SJohn Fastabend __u32 local_ip6[4]; /* Stored in network byte order */ 4224303def35SJohn Fastabend __u32 remote_port; /* Stored in network byte order */ 4225303def35SJohn Fastabend __u32 local_port; /* stored in host byte order */ 42263bdbd022SJohn Fastabend __u32 size; /* Total size of sk_msg */ 422713d70f5aSJohn Fastabend 422813d70f5aSJohn Fastabend __bpf_md_ptr(struct bpf_sock *, sk); /* current socket */ 42294f738adbSJohn Fastabend }; 42304f738adbSJohn Fastabend 42312dbb9b9eSMartin KaFai Lau struct sk_reuseport_md { 42322dbb9b9eSMartin KaFai Lau /* 42332dbb9b9eSMartin KaFai Lau * Start of directly accessible data. It begins from 42342dbb9b9eSMartin KaFai Lau * the tcp/udp header. 42352dbb9b9eSMartin KaFai Lau */ 4236b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data); 4237b7df9adaSDaniel Borkmann /* End of directly accessible data */ 4238b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data_end); 42392dbb9b9eSMartin KaFai Lau /* 42402dbb9b9eSMartin KaFai Lau * Total length of packet (starting from the tcp/udp header). 42412dbb9b9eSMartin KaFai Lau * Note that the directly accessible bytes (data_end - data) 42422dbb9b9eSMartin KaFai Lau * could be less than this "len". Those bytes could be 42432dbb9b9eSMartin KaFai Lau * indirectly read by a helper "bpf_skb_load_bytes()". 42442dbb9b9eSMartin KaFai Lau */ 42452dbb9b9eSMartin KaFai Lau __u32 len; 42462dbb9b9eSMartin KaFai Lau /* 42472dbb9b9eSMartin KaFai Lau * Eth protocol in the mac header (network byte order). e.g. 42482dbb9b9eSMartin KaFai Lau * ETH_P_IP(0x0800) and ETH_P_IPV6(0x86DD) 42492dbb9b9eSMartin KaFai Lau */ 42502dbb9b9eSMartin KaFai Lau __u32 eth_protocol; 42512dbb9b9eSMartin KaFai Lau __u32 ip_protocol; /* IP protocol. e.g. IPPROTO_TCP, IPPROTO_UDP */ 42522dbb9b9eSMartin KaFai Lau __u32 bind_inany; /* Is sock bound to an INANY address? */ 42532dbb9b9eSMartin KaFai Lau __u32 hash; /* A hash of the packet 4 tuples */ 42542dbb9b9eSMartin KaFai Lau }; 42552dbb9b9eSMartin KaFai Lau 42561e270976SMartin KaFai Lau #define BPF_TAG_SIZE 8 42571e270976SMartin KaFai Lau 42581e270976SMartin KaFai Lau struct bpf_prog_info { 42591e270976SMartin KaFai Lau __u32 type; 42601e270976SMartin KaFai Lau __u32 id; 42611e270976SMartin KaFai Lau __u8 tag[BPF_TAG_SIZE]; 42621e270976SMartin KaFai Lau __u32 jited_prog_len; 42631e270976SMartin KaFai Lau __u32 xlated_prog_len; 42641e270976SMartin KaFai Lau __aligned_u64 jited_prog_insns; 42651e270976SMartin KaFai Lau __aligned_u64 xlated_prog_insns; 4266cb4d2b3fSMartin KaFai Lau __u64 load_time; /* ns since boottime */ 4267cb4d2b3fSMartin KaFai Lau __u32 created_by_uid; 4268cb4d2b3fSMartin KaFai Lau __u32 nr_map_ids; 4269cb4d2b3fSMartin KaFai Lau __aligned_u64 map_ids; 4270067cae47SMartin KaFai Lau char name[BPF_OBJ_NAME_LEN]; 4271675fc275SJakub Kicinski __u32 ifindex; 4272b85fab0eSJiri Olsa __u32 gpl_compatible:1; 42730472301aSBaruch Siach __u32 :31; /* alignment pad */ 4274675fc275SJakub Kicinski __u64 netns_dev; 4275675fc275SJakub Kicinski __u64 netns_ino; 4276dbecd738SSandipan Das __u32 nr_jited_ksyms; 4277815581c1SSandipan Das __u32 nr_jited_func_lens; 4278dbecd738SSandipan Das __aligned_u64 jited_ksyms; 4279815581c1SSandipan Das __aligned_u64 jited_func_lens; 4280838e9690SYonghong Song __u32 btf_id; 4281838e9690SYonghong Song __u32 func_info_rec_size; 4282838e9690SYonghong Song __aligned_u64 func_info; 428311d8b82dSYonghong Song __u32 nr_func_info; 428411d8b82dSYonghong Song __u32 nr_line_info; 4285c454a46bSMartin KaFai Lau __aligned_u64 line_info; 4286c454a46bSMartin KaFai Lau __aligned_u64 jited_line_info; 428711d8b82dSYonghong Song __u32 nr_jited_line_info; 4288c454a46bSMartin KaFai Lau __u32 line_info_rec_size; 4289c454a46bSMartin KaFai Lau __u32 jited_line_info_rec_size; 4290c872bdb3SSong Liu __u32 nr_prog_tags; 4291c872bdb3SSong Liu __aligned_u64 prog_tags; 42925f8f8b93SAlexei Starovoitov __u64 run_time_ns; 42935f8f8b93SAlexei Starovoitov __u64 run_cnt; 42941e270976SMartin KaFai Lau } __attribute__((aligned(8))); 42951e270976SMartin KaFai Lau 42961e270976SMartin KaFai Lau struct bpf_map_info { 42971e270976SMartin KaFai Lau __u32 type; 42981e270976SMartin KaFai Lau __u32 id; 42991e270976SMartin KaFai Lau __u32 key_size; 43001e270976SMartin KaFai Lau __u32 value_size; 43011e270976SMartin KaFai Lau __u32 max_entries; 43021e270976SMartin KaFai Lau __u32 map_flags; 4303067cae47SMartin KaFai Lau char name[BPF_OBJ_NAME_LEN]; 430452775b33SJakub Kicinski __u32 ifindex; 430585d33df3SMartin KaFai Lau __u32 btf_vmlinux_value_type_id; 430652775b33SJakub Kicinski __u64 netns_dev; 430752775b33SJakub Kicinski __u64 netns_ino; 430878958fcaSMartin KaFai Lau __u32 btf_id; 43099b2cf328SMartin KaFai Lau __u32 btf_key_type_id; 43109b2cf328SMartin KaFai Lau __u32 btf_value_type_id; 43111e270976SMartin KaFai Lau } __attribute__((aligned(8))); 43121e270976SMartin KaFai Lau 431362dab84cSMartin KaFai Lau struct bpf_btf_info { 431462dab84cSMartin KaFai Lau __aligned_u64 btf; 431562dab84cSMartin KaFai Lau __u32 btf_size; 431662dab84cSMartin KaFai Lau __u32 id; 431762dab84cSMartin KaFai Lau } __attribute__((aligned(8))); 431862dab84cSMartin KaFai Lau 4319f2e10bffSAndrii Nakryiko struct bpf_link_info { 4320f2e10bffSAndrii Nakryiko __u32 type; 4321f2e10bffSAndrii Nakryiko __u32 id; 4322f2e10bffSAndrii Nakryiko __u32 prog_id; 4323f2e10bffSAndrii Nakryiko union { 4324f2e10bffSAndrii Nakryiko struct { 4325f2e10bffSAndrii Nakryiko __aligned_u64 tp_name; /* in/out: tp_name buffer ptr */ 4326f2e10bffSAndrii Nakryiko __u32 tp_name_len; /* in/out: tp_name buffer len */ 4327f2e10bffSAndrii Nakryiko } raw_tracepoint; 4328f2e10bffSAndrii Nakryiko struct { 4329f2e10bffSAndrii Nakryiko __u32 attach_type; 4330f2e10bffSAndrii Nakryiko } tracing; 4331f2e10bffSAndrii Nakryiko struct { 4332f2e10bffSAndrii Nakryiko __u64 cgroup_id; 4333f2e10bffSAndrii Nakryiko __u32 attach_type; 4334f2e10bffSAndrii Nakryiko } cgroup; 43357f045a49SJakub Sitnicki struct { 43366b0a249aSYonghong Song __aligned_u64 target_name; /* in/out: target_name buffer ptr */ 43376b0a249aSYonghong Song __u32 target_name_len; /* in/out: target_name buffer len */ 43386b0a249aSYonghong Song union { 4339b0c9eb37SYonghong Song struct { 43406b0a249aSYonghong Song __u32 map_id; 43416b0a249aSYonghong Song } map; 4342b0c9eb37SYonghong Song }; 43436b0a249aSYonghong Song } iter; 43446b0a249aSYonghong Song struct { 43457f045a49SJakub Sitnicki __u32 netns_ino; 43467f045a49SJakub Sitnicki __u32 attach_type; 43477f045a49SJakub Sitnicki } netns; 4348c1931c97SAndrii Nakryiko struct { 4349c1931c97SAndrii Nakryiko __u32 ifindex; 4350c1931c97SAndrii Nakryiko } xdp; 4351f2e10bffSAndrii Nakryiko }; 4352f2e10bffSAndrii Nakryiko } __attribute__((aligned(8))); 4353f2e10bffSAndrii Nakryiko 43544fbac77dSAndrey Ignatov /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed 43554fbac77dSAndrey Ignatov * by user and intended to be used by socket (e.g. to bind to, depends on 4356bfdfa517SRandy Dunlap * attach type). 43574fbac77dSAndrey Ignatov */ 43584fbac77dSAndrey Ignatov struct bpf_sock_addr { 43594fbac77dSAndrey Ignatov __u32 user_family; /* Allows 4-byte read, but no write. */ 43604fbac77dSAndrey Ignatov __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write. 43614fbac77dSAndrey Ignatov * Stored in network byte order. 43624fbac77dSAndrey Ignatov */ 4363d4ecfeb1SStanislav Fomichev __u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write. 43644fbac77dSAndrey Ignatov * Stored in network byte order. 43654fbac77dSAndrey Ignatov */ 43667aebfa1bSAndrey Ignatov __u32 user_port; /* Allows 1,2,4-byte read and 4-byte write. 43674fbac77dSAndrey Ignatov * Stored in network byte order 43684fbac77dSAndrey Ignatov */ 43694fbac77dSAndrey Ignatov __u32 family; /* Allows 4-byte read, but no write */ 43704fbac77dSAndrey Ignatov __u32 type; /* Allows 4-byte read, but no write */ 43714fbac77dSAndrey Ignatov __u32 protocol; /* Allows 4-byte read, but no write */ 4372600c70baSStanislav Fomichev __u32 msg_src_ip4; /* Allows 1,2,4-byte read and 4-byte write. 43731cedee13SAndrey Ignatov * Stored in network byte order. 43741cedee13SAndrey Ignatov */ 4375d4ecfeb1SStanislav Fomichev __u32 msg_src_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write. 43761cedee13SAndrey Ignatov * Stored in network byte order. 43771cedee13SAndrey Ignatov */ 4378fb85c4a7SStanislav Fomichev __bpf_md_ptr(struct bpf_sock *, sk); 43794fbac77dSAndrey Ignatov }; 43804fbac77dSAndrey Ignatov 438140304b2aSLawrence Brakmo /* User bpf_sock_ops struct to access socket values and specify request ops 438240304b2aSLawrence Brakmo * and their replies. 438340304b2aSLawrence Brakmo * Some of this fields are in network (bigendian) byte order and may need 438440304b2aSLawrence Brakmo * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h). 438540304b2aSLawrence Brakmo * New fields can only be added at the end of this structure 438640304b2aSLawrence Brakmo */ 438740304b2aSLawrence Brakmo struct bpf_sock_ops { 438840304b2aSLawrence Brakmo __u32 op; 438940304b2aSLawrence Brakmo union { 4390de525be2SLawrence Brakmo __u32 args[4]; /* Optionally passed to bpf program */ 4391de525be2SLawrence Brakmo __u32 reply; /* Returned by bpf program */ 4392de525be2SLawrence Brakmo __u32 replylong[4]; /* Optionally returned by bpf prog */ 439340304b2aSLawrence Brakmo }; 439440304b2aSLawrence Brakmo __u32 family; 439540304b2aSLawrence Brakmo __u32 remote_ip4; /* Stored in network byte order */ 439640304b2aSLawrence Brakmo __u32 local_ip4; /* Stored in network byte order */ 439740304b2aSLawrence Brakmo __u32 remote_ip6[4]; /* Stored in network byte order */ 439840304b2aSLawrence Brakmo __u32 local_ip6[4]; /* Stored in network byte order */ 439940304b2aSLawrence Brakmo __u32 remote_port; /* Stored in network byte order */ 440040304b2aSLawrence Brakmo __u32 local_port; /* stored in host byte order */ 4401f19397a5SLawrence Brakmo __u32 is_fullsock; /* Some TCP fields are only valid if 4402f19397a5SLawrence Brakmo * there is a full socket. If not, the 4403f19397a5SLawrence Brakmo * fields read as zero. 4404f19397a5SLawrence Brakmo */ 4405f19397a5SLawrence Brakmo __u32 snd_cwnd; 4406f19397a5SLawrence Brakmo __u32 srtt_us; /* Averaged RTT << 3 in usecs */ 4407b13d8807SLawrence Brakmo __u32 bpf_sock_ops_cb_flags; /* flags defined in uapi/linux/tcp.h */ 440844f0e430SLawrence Brakmo __u32 state; 440944f0e430SLawrence Brakmo __u32 rtt_min; 441044f0e430SLawrence Brakmo __u32 snd_ssthresh; 441144f0e430SLawrence Brakmo __u32 rcv_nxt; 441244f0e430SLawrence Brakmo __u32 snd_nxt; 441344f0e430SLawrence Brakmo __u32 snd_una; 441444f0e430SLawrence Brakmo __u32 mss_cache; 441544f0e430SLawrence Brakmo __u32 ecn_flags; 441644f0e430SLawrence Brakmo __u32 rate_delivered; 441744f0e430SLawrence Brakmo __u32 rate_interval_us; 441844f0e430SLawrence Brakmo __u32 packets_out; 441944f0e430SLawrence Brakmo __u32 retrans_out; 442044f0e430SLawrence Brakmo __u32 total_retrans; 442144f0e430SLawrence Brakmo __u32 segs_in; 442244f0e430SLawrence Brakmo __u32 data_segs_in; 442344f0e430SLawrence Brakmo __u32 segs_out; 442444f0e430SLawrence Brakmo __u32 data_segs_out; 442544f0e430SLawrence Brakmo __u32 lost_out; 442644f0e430SLawrence Brakmo __u32 sacked_out; 442744f0e430SLawrence Brakmo __u32 sk_txhash; 442844f0e430SLawrence Brakmo __u64 bytes_received; 442944f0e430SLawrence Brakmo __u64 bytes_acked; 44301314ef56SStanislav Fomichev __bpf_md_ptr(struct bpf_sock *, sk); 44310813a841SMartin KaFai Lau /* [skb_data, skb_data_end) covers the whole TCP header. 44320813a841SMartin KaFai Lau * 44330813a841SMartin KaFai Lau * BPF_SOCK_OPS_PARSE_HDR_OPT_CB: The packet received 44340813a841SMartin KaFai Lau * BPF_SOCK_OPS_HDR_OPT_LEN_CB: Not useful because the 44350813a841SMartin KaFai Lau * header has not been written. 44360813a841SMartin KaFai Lau * BPF_SOCK_OPS_WRITE_HDR_OPT_CB: The header and options have 44370813a841SMartin KaFai Lau * been written so far. 44380813a841SMartin KaFai Lau * BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: The SYNACK that concludes 44390813a841SMartin KaFai Lau * the 3WHS. 44400813a841SMartin KaFai Lau * BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: The ACK that concludes 44410813a841SMartin KaFai Lau * the 3WHS. 44420813a841SMartin KaFai Lau * 44430813a841SMartin KaFai Lau * bpf_load_hdr_opt() can also be used to read a particular option. 44440813a841SMartin KaFai Lau */ 44450813a841SMartin KaFai Lau __bpf_md_ptr(void *, skb_data); 44460813a841SMartin KaFai Lau __bpf_md_ptr(void *, skb_data_end); 44470813a841SMartin KaFai Lau __u32 skb_len; /* The total length of a packet. 44480813a841SMartin KaFai Lau * It includes the header, options, 44490813a841SMartin KaFai Lau * and payload. 44500813a841SMartin KaFai Lau */ 44510813a841SMartin KaFai Lau __u32 skb_tcp_flags; /* tcp_flags of the header. It provides 44520813a841SMartin KaFai Lau * an easy way to check for tcp_flags 44530813a841SMartin KaFai Lau * without parsing skb_data. 44540813a841SMartin KaFai Lau * 44550813a841SMartin KaFai Lau * In particular, the skb_tcp_flags 44560813a841SMartin KaFai Lau * will still be available in 44570813a841SMartin KaFai Lau * BPF_SOCK_OPS_HDR_OPT_LEN even though 44580813a841SMartin KaFai Lau * the outgoing header has not 44590813a841SMartin KaFai Lau * been written yet. 44600813a841SMartin KaFai Lau */ 446140304b2aSLawrence Brakmo }; 446240304b2aSLawrence Brakmo 4463b13d8807SLawrence Brakmo /* Definitions for bpf_sock_ops_cb_flags */ 44641aae4bddSAndrii Nakryiko enum { 44651aae4bddSAndrii Nakryiko BPF_SOCK_OPS_RTO_CB_FLAG = (1<<0), 44661aae4bddSAndrii Nakryiko BPF_SOCK_OPS_RETRANS_CB_FLAG = (1<<1), 44671aae4bddSAndrii Nakryiko BPF_SOCK_OPS_STATE_CB_FLAG = (1<<2), 44681aae4bddSAndrii Nakryiko BPF_SOCK_OPS_RTT_CB_FLAG = (1<<3), 44690813a841SMartin KaFai Lau /* Call bpf for all received TCP headers. The bpf prog will be 44700813a841SMartin KaFai Lau * called under sock_ops->op == BPF_SOCK_OPS_PARSE_HDR_OPT_CB 44710813a841SMartin KaFai Lau * 44720813a841SMartin KaFai Lau * Please refer to the comment in BPF_SOCK_OPS_PARSE_HDR_OPT_CB 44730813a841SMartin KaFai Lau * for the header option related helpers that will be useful 44740813a841SMartin KaFai Lau * to the bpf programs. 44750813a841SMartin KaFai Lau * 44760813a841SMartin KaFai Lau * It could be used at the client/active side (i.e. connect() side) 44770813a841SMartin KaFai Lau * when the server told it that the server was in syncookie 44780813a841SMartin KaFai Lau * mode and required the active side to resend the bpf-written 44790813a841SMartin KaFai Lau * options. The active side can keep writing the bpf-options until 44800813a841SMartin KaFai Lau * it received a valid packet from the server side to confirm 44810813a841SMartin KaFai Lau * the earlier packet (and options) has been received. The later 44820813a841SMartin KaFai Lau * example patch is using it like this at the active side when the 44830813a841SMartin KaFai Lau * server is in syncookie mode. 44840813a841SMartin KaFai Lau * 44850813a841SMartin KaFai Lau * The bpf prog will usually turn this off in the common cases. 44860813a841SMartin KaFai Lau */ 448700d211a4SMartin KaFai Lau BPF_SOCK_OPS_PARSE_ALL_HDR_OPT_CB_FLAG = (1<<4), 44880813a841SMartin KaFai Lau /* Call bpf when kernel has received a header option that 44890813a841SMartin KaFai Lau * the kernel cannot handle. The bpf prog will be called under 44900813a841SMartin KaFai Lau * sock_ops->op == BPF_SOCK_OPS_PARSE_HDR_OPT_CB. 44910813a841SMartin KaFai Lau * 44920813a841SMartin KaFai Lau * Please refer to the comment in BPF_SOCK_OPS_PARSE_HDR_OPT_CB 44930813a841SMartin KaFai Lau * for the header option related helpers that will be useful 44940813a841SMartin KaFai Lau * to the bpf programs. 44950813a841SMartin KaFai Lau */ 449600d211a4SMartin KaFai Lau BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG = (1<<5), 44970813a841SMartin KaFai Lau /* Call bpf when the kernel is writing header options for the 44980813a841SMartin KaFai Lau * outgoing packet. The bpf prog will first be called 44990813a841SMartin KaFai Lau * to reserve space in a skb under 45000813a841SMartin KaFai Lau * sock_ops->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB. Then 45010813a841SMartin KaFai Lau * the bpf prog will be called to write the header option(s) 45020813a841SMartin KaFai Lau * under sock_ops->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB. 45030813a841SMartin KaFai Lau * 45040813a841SMartin KaFai Lau * Please refer to the comment in BPF_SOCK_OPS_HDR_OPT_LEN_CB 45050813a841SMartin KaFai Lau * and BPF_SOCK_OPS_WRITE_HDR_OPT_CB for the header option 45060813a841SMartin KaFai Lau * related helpers that will be useful to the bpf programs. 45070813a841SMartin KaFai Lau * 45080813a841SMartin KaFai Lau * The kernel gets its chance to reserve space and write 45090813a841SMartin KaFai Lau * options first before the BPF program does. 45100813a841SMartin KaFai Lau */ 4511331fca43SMartin KaFai Lau BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1<<6), 45121aae4bddSAndrii Nakryiko /* Mask of all currently supported cb flags */ 4513331fca43SMartin KaFai Lau BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, 45141aae4bddSAndrii Nakryiko }; 4515b13d8807SLawrence Brakmo 451640304b2aSLawrence Brakmo /* List of known BPF sock_ops operators. 451740304b2aSLawrence Brakmo * New entries can only be added at the end 451840304b2aSLawrence Brakmo */ 451940304b2aSLawrence Brakmo enum { 452040304b2aSLawrence Brakmo BPF_SOCK_OPS_VOID, 45218550f328SLawrence Brakmo BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or 45228550f328SLawrence Brakmo * -1 if default value should be used 45238550f328SLawrence Brakmo */ 452413d3b1ebSLawrence Brakmo BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized 452513d3b1ebSLawrence Brakmo * window (in packets) or -1 if default 452613d3b1ebSLawrence Brakmo * value should be used 452713d3b1ebSLawrence Brakmo */ 45289872a4bdSLawrence Brakmo BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an 45299872a4bdSLawrence Brakmo * active connection is initialized 45309872a4bdSLawrence Brakmo */ 45319872a4bdSLawrence Brakmo BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an 45329872a4bdSLawrence Brakmo * active connection is 45339872a4bdSLawrence Brakmo * established 45349872a4bdSLawrence Brakmo */ 45359872a4bdSLawrence Brakmo BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a 45369872a4bdSLawrence Brakmo * passive connection is 45379872a4bdSLawrence Brakmo * established 45389872a4bdSLawrence Brakmo */ 453991b5b21cSLawrence Brakmo BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control 454091b5b21cSLawrence Brakmo * needs ECN 454191b5b21cSLawrence Brakmo */ 4542e6546ef6SLawrence Brakmo BPF_SOCK_OPS_BASE_RTT, /* Get base RTT. The correct value is 4543e6546ef6SLawrence Brakmo * based on the path and may be 4544e6546ef6SLawrence Brakmo * dependent on the congestion control 4545e6546ef6SLawrence Brakmo * algorithm. In general it indicates 4546e6546ef6SLawrence Brakmo * a congestion threshold. RTTs above 4547e6546ef6SLawrence Brakmo * this indicate congestion 4548e6546ef6SLawrence Brakmo */ 4549f89013f6SLawrence Brakmo BPF_SOCK_OPS_RTO_CB, /* Called when an RTO has triggered. 4550f89013f6SLawrence Brakmo * Arg1: value of icsk_retransmits 4551f89013f6SLawrence Brakmo * Arg2: value of icsk_rto 4552f89013f6SLawrence Brakmo * Arg3: whether RTO has expired 4553f89013f6SLawrence Brakmo */ 4554a31ad29eSLawrence Brakmo BPF_SOCK_OPS_RETRANS_CB, /* Called when skb is retransmitted. 4555a31ad29eSLawrence Brakmo * Arg1: sequence number of 1st byte 4556a31ad29eSLawrence Brakmo * Arg2: # segments 4557a31ad29eSLawrence Brakmo * Arg3: return value of 4558a31ad29eSLawrence Brakmo * tcp_transmit_skb (0 => success) 4559a31ad29eSLawrence Brakmo */ 4560d4487491SLawrence Brakmo BPF_SOCK_OPS_STATE_CB, /* Called when TCP changes state. 4561d4487491SLawrence Brakmo * Arg1: old_state 4562d4487491SLawrence Brakmo * Arg2: new_state 4563d4487491SLawrence Brakmo */ 4564f333ee0cSAndrey Ignatov BPF_SOCK_OPS_TCP_LISTEN_CB, /* Called on listen(2), right after 4565f333ee0cSAndrey Ignatov * socket transition to LISTEN state. 4566f333ee0cSAndrey Ignatov */ 456723729ff2SStanislav Fomichev BPF_SOCK_OPS_RTT_CB, /* Called on every RTT. 456823729ff2SStanislav Fomichev */ 45690813a841SMartin KaFai Lau BPF_SOCK_OPS_PARSE_HDR_OPT_CB, /* Parse the header option. 45700813a841SMartin KaFai Lau * It will be called to handle 45710813a841SMartin KaFai Lau * the packets received at 45720813a841SMartin KaFai Lau * an already established 45730813a841SMartin KaFai Lau * connection. 45740813a841SMartin KaFai Lau * 45750813a841SMartin KaFai Lau * sock_ops->skb_data: 45760813a841SMartin KaFai Lau * Referring to the received skb. 45770813a841SMartin KaFai Lau * It covers the TCP header only. 45780813a841SMartin KaFai Lau * 45790813a841SMartin KaFai Lau * bpf_load_hdr_opt() can also 45800813a841SMartin KaFai Lau * be used to search for a 45810813a841SMartin KaFai Lau * particular option. 45820813a841SMartin KaFai Lau */ 45830813a841SMartin KaFai Lau BPF_SOCK_OPS_HDR_OPT_LEN_CB, /* Reserve space for writing the 45840813a841SMartin KaFai Lau * header option later in 45850813a841SMartin KaFai Lau * BPF_SOCK_OPS_WRITE_HDR_OPT_CB. 45860813a841SMartin KaFai Lau * Arg1: bool want_cookie. (in 45870813a841SMartin KaFai Lau * writing SYNACK only) 45880813a841SMartin KaFai Lau * 45890813a841SMartin KaFai Lau * sock_ops->skb_data: 45900813a841SMartin KaFai Lau * Not available because no header has 45910813a841SMartin KaFai Lau * been written yet. 45920813a841SMartin KaFai Lau * 45930813a841SMartin KaFai Lau * sock_ops->skb_tcp_flags: 45940813a841SMartin KaFai Lau * The tcp_flags of the 45950813a841SMartin KaFai Lau * outgoing skb. (e.g. SYN, ACK, FIN). 45960813a841SMartin KaFai Lau * 45970813a841SMartin KaFai Lau * bpf_reserve_hdr_opt() should 45980813a841SMartin KaFai Lau * be used to reserve space. 45990813a841SMartin KaFai Lau */ 46000813a841SMartin KaFai Lau BPF_SOCK_OPS_WRITE_HDR_OPT_CB, /* Write the header options 46010813a841SMartin KaFai Lau * Arg1: bool want_cookie. (in 46020813a841SMartin KaFai Lau * writing SYNACK only) 46030813a841SMartin KaFai Lau * 46040813a841SMartin KaFai Lau * sock_ops->skb_data: 46050813a841SMartin KaFai Lau * Referring to the outgoing skb. 46060813a841SMartin KaFai Lau * It covers the TCP header 46070813a841SMartin KaFai Lau * that has already been written 46080813a841SMartin KaFai Lau * by the kernel and the 46090813a841SMartin KaFai Lau * earlier bpf-progs. 46100813a841SMartin KaFai Lau * 46110813a841SMartin KaFai Lau * sock_ops->skb_tcp_flags: 46120813a841SMartin KaFai Lau * The tcp_flags of the outgoing 46130813a841SMartin KaFai Lau * skb. (e.g. SYN, ACK, FIN). 46140813a841SMartin KaFai Lau * 46150813a841SMartin KaFai Lau * bpf_store_hdr_opt() should 46160813a841SMartin KaFai Lau * be used to write the 46170813a841SMartin KaFai Lau * option. 46180813a841SMartin KaFai Lau * 46190813a841SMartin KaFai Lau * bpf_load_hdr_opt() can also 46200813a841SMartin KaFai Lau * be used to search for a 46210813a841SMartin KaFai Lau * particular option that 46220813a841SMartin KaFai Lau * has already been written 46230813a841SMartin KaFai Lau * by the kernel or the 46240813a841SMartin KaFai Lau * earlier bpf-progs. 46250813a841SMartin KaFai Lau */ 4626d4487491SLawrence Brakmo }; 4627d4487491SLawrence Brakmo 4628d4487491SLawrence Brakmo /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect 4629d4487491SLawrence Brakmo * changes between the TCP and BPF versions. Ideally this should never happen. 4630d4487491SLawrence Brakmo * If it does, we need to add code to convert them before calling 4631d4487491SLawrence Brakmo * the BPF sock_ops function. 4632d4487491SLawrence Brakmo */ 4633d4487491SLawrence Brakmo enum { 4634d4487491SLawrence Brakmo BPF_TCP_ESTABLISHED = 1, 4635d4487491SLawrence Brakmo BPF_TCP_SYN_SENT, 4636d4487491SLawrence Brakmo BPF_TCP_SYN_RECV, 4637d4487491SLawrence Brakmo BPF_TCP_FIN_WAIT1, 4638d4487491SLawrence Brakmo BPF_TCP_FIN_WAIT2, 4639d4487491SLawrence Brakmo BPF_TCP_TIME_WAIT, 4640d4487491SLawrence Brakmo BPF_TCP_CLOSE, 4641d4487491SLawrence Brakmo BPF_TCP_CLOSE_WAIT, 4642d4487491SLawrence Brakmo BPF_TCP_LAST_ACK, 4643d4487491SLawrence Brakmo BPF_TCP_LISTEN, 4644d4487491SLawrence Brakmo BPF_TCP_CLOSING, /* Now a valid state */ 4645d4487491SLawrence Brakmo BPF_TCP_NEW_SYN_RECV, 4646d4487491SLawrence Brakmo 4647d4487491SLawrence Brakmo BPF_TCP_MAX_STATES /* Leave at the end! */ 464840304b2aSLawrence Brakmo }; 464940304b2aSLawrence Brakmo 46501aae4bddSAndrii Nakryiko enum { 46511aae4bddSAndrii Nakryiko TCP_BPF_IW = 1001, /* Set TCP initial congestion window */ 46521aae4bddSAndrii Nakryiko TCP_BPF_SNDCWND_CLAMP = 1002, /* Set sndcwnd_clamp */ 46532b8ee4f0SMartin KaFai Lau TCP_BPF_DELACK_MAX = 1003, /* Max delay ack in usecs */ 4654ca584ba0SMartin KaFai Lau TCP_BPF_RTO_MIN = 1004, /* Min delay ack in usecs */ 46550813a841SMartin KaFai Lau /* Copy the SYN pkt to optval 46560813a841SMartin KaFai Lau * 46570813a841SMartin KaFai Lau * BPF_PROG_TYPE_SOCK_OPS only. It is similar to the 46580813a841SMartin KaFai Lau * bpf_getsockopt(TCP_SAVED_SYN) but it does not limit 46590813a841SMartin KaFai Lau * to only getting from the saved_syn. It can either get the 46600813a841SMartin KaFai Lau * syn packet from: 46610813a841SMartin KaFai Lau * 46620813a841SMartin KaFai Lau * 1. the just-received SYN packet (only available when writing the 46630813a841SMartin KaFai Lau * SYNACK). It will be useful when it is not necessary to 46640813a841SMartin KaFai Lau * save the SYN packet for latter use. It is also the only way 46650813a841SMartin KaFai Lau * to get the SYN during syncookie mode because the syn 46660813a841SMartin KaFai Lau * packet cannot be saved during syncookie. 46670813a841SMartin KaFai Lau * 46680813a841SMartin KaFai Lau * OR 46690813a841SMartin KaFai Lau * 46700813a841SMartin KaFai Lau * 2. the earlier saved syn which was done by 46710813a841SMartin KaFai Lau * bpf_setsockopt(TCP_SAVE_SYN). 46720813a841SMartin KaFai Lau * 46730813a841SMartin KaFai Lau * The bpf_getsockopt(TCP_BPF_SYN*) option will hide where the 46740813a841SMartin KaFai Lau * SYN packet is obtained. 46750813a841SMartin KaFai Lau * 46760813a841SMartin KaFai Lau * If the bpf-prog does not need the IP[46] header, the 46770813a841SMartin KaFai Lau * bpf-prog can avoid parsing the IP header by using 46780813a841SMartin KaFai Lau * TCP_BPF_SYN. Otherwise, the bpf-prog can get both 46790813a841SMartin KaFai Lau * IP[46] and TCP header by using TCP_BPF_SYN_IP. 46800813a841SMartin KaFai Lau * 46810813a841SMartin KaFai Lau * >0: Total number of bytes copied 46820813a841SMartin KaFai Lau * -ENOSPC: Not enough space in optval. Only optlen number of 46830813a841SMartin KaFai Lau * bytes is copied. 46840813a841SMartin KaFai Lau * -ENOENT: The SYN skb is not available now and the earlier SYN pkt 46850813a841SMartin KaFai Lau * is not saved by setsockopt(TCP_SAVE_SYN). 46860813a841SMartin KaFai Lau */ 46870813a841SMartin KaFai Lau TCP_BPF_SYN = 1005, /* Copy the TCP header */ 46880813a841SMartin KaFai Lau TCP_BPF_SYN_IP = 1006, /* Copy the IP[46] and TCP header */ 4689267cf9faSMartin KaFai Lau TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */ 46900813a841SMartin KaFai Lau }; 46910813a841SMartin KaFai Lau 46920813a841SMartin KaFai Lau enum { 46930813a841SMartin KaFai Lau BPF_LOAD_HDR_OPT_TCP_SYN = (1ULL << 0), 46940813a841SMartin KaFai Lau }; 46950813a841SMartin KaFai Lau 46960813a841SMartin KaFai Lau /* args[0] value during BPF_SOCK_OPS_HDR_OPT_LEN_CB and 46970813a841SMartin KaFai Lau * BPF_SOCK_OPS_WRITE_HDR_OPT_CB. 46980813a841SMartin KaFai Lau */ 46990813a841SMartin KaFai Lau enum { 47000813a841SMartin KaFai Lau BPF_WRITE_HDR_TCP_CURRENT_MSS = 1, /* Kernel is finding the 47010813a841SMartin KaFai Lau * total option spaces 47020813a841SMartin KaFai Lau * required for an established 47030813a841SMartin KaFai Lau * sk in order to calculate the 47040813a841SMartin KaFai Lau * MSS. No skb is actually 47050813a841SMartin KaFai Lau * sent. 47060813a841SMartin KaFai Lau */ 47070813a841SMartin KaFai Lau BPF_WRITE_HDR_TCP_SYNACK_COOKIE = 2, /* Kernel is in syncookie mode 47080813a841SMartin KaFai Lau * when sending a SYN. 47090813a841SMartin KaFai Lau */ 47101aae4bddSAndrii Nakryiko }; 4711fc747810SLawrence Brakmo 4712908432caSYonghong Song struct bpf_perf_event_value { 4713908432caSYonghong Song __u64 counter; 4714908432caSYonghong Song __u64 enabled; 4715908432caSYonghong Song __u64 running; 4716908432caSYonghong Song }; 4717908432caSYonghong Song 47181aae4bddSAndrii Nakryiko enum { 47191aae4bddSAndrii Nakryiko BPF_DEVCG_ACC_MKNOD = (1ULL << 0), 47201aae4bddSAndrii Nakryiko BPF_DEVCG_ACC_READ = (1ULL << 1), 47211aae4bddSAndrii Nakryiko BPF_DEVCG_ACC_WRITE = (1ULL << 2), 47221aae4bddSAndrii Nakryiko }; 4723ebc614f6SRoman Gushchin 47241aae4bddSAndrii Nakryiko enum { 47251aae4bddSAndrii Nakryiko BPF_DEVCG_DEV_BLOCK = (1ULL << 0), 47261aae4bddSAndrii Nakryiko BPF_DEVCG_DEV_CHAR = (1ULL << 1), 47271aae4bddSAndrii Nakryiko }; 4728ebc614f6SRoman Gushchin 4729ebc614f6SRoman Gushchin struct bpf_cgroup_dev_ctx { 473006ef0ccbSYonghong Song /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */ 473106ef0ccbSYonghong Song __u32 access_type; 4732ebc614f6SRoman Gushchin __u32 major; 4733ebc614f6SRoman Gushchin __u32 minor; 4734ebc614f6SRoman Gushchin }; 4735ebc614f6SRoman Gushchin 4736c4f6699dSAlexei Starovoitov struct bpf_raw_tracepoint_args { 4737c4f6699dSAlexei Starovoitov __u64 args[0]; 4738c4f6699dSAlexei Starovoitov }; 4739c4f6699dSAlexei Starovoitov 474087f5fc7eSDavid Ahern /* DIRECT: Skip the FIB rules and go to FIB table associated with device 474187f5fc7eSDavid Ahern * OUTPUT: Do lookup from egress perspective; default is ingress 474287f5fc7eSDavid Ahern */ 47431aae4bddSAndrii Nakryiko enum { 47441aae4bddSAndrii Nakryiko BPF_FIB_LOOKUP_DIRECT = (1U << 0), 47451aae4bddSAndrii Nakryiko BPF_FIB_LOOKUP_OUTPUT = (1U << 1), 47461aae4bddSAndrii Nakryiko }; 474787f5fc7eSDavid Ahern 47484c79579bSDavid Ahern enum { 47494c79579bSDavid Ahern BPF_FIB_LKUP_RET_SUCCESS, /* lookup successful */ 47504c79579bSDavid Ahern BPF_FIB_LKUP_RET_BLACKHOLE, /* dest is blackholed; can be dropped */ 47514c79579bSDavid Ahern BPF_FIB_LKUP_RET_UNREACHABLE, /* dest is unreachable; can be dropped */ 47524c79579bSDavid Ahern BPF_FIB_LKUP_RET_PROHIBIT, /* dest not allowed; can be dropped */ 47534c79579bSDavid Ahern BPF_FIB_LKUP_RET_NOT_FWDED, /* packet is not forwarded */ 47544c79579bSDavid Ahern BPF_FIB_LKUP_RET_FWD_DISABLED, /* fwding is not enabled on ingress */ 47554c79579bSDavid Ahern BPF_FIB_LKUP_RET_UNSUPP_LWT, /* fwd requires encapsulation */ 47564c79579bSDavid Ahern BPF_FIB_LKUP_RET_NO_NEIGH, /* no neighbor entry for nh */ 47574c79579bSDavid Ahern BPF_FIB_LKUP_RET_FRAG_NEEDED, /* fragmentation required to fwd */ 47584c79579bSDavid Ahern }; 47594c79579bSDavid Ahern 476087f5fc7eSDavid Ahern struct bpf_fib_lookup { 4761fa898d76SDavid Ahern /* input: network family for lookup (AF_INET, AF_INET6) 4762fa898d76SDavid Ahern * output: network family of egress nexthop 4763fa898d76SDavid Ahern */ 4764fa898d76SDavid Ahern __u8 family; 476587f5fc7eSDavid Ahern 476687f5fc7eSDavid Ahern /* set if lookup is to consider L4 data - e.g., FIB rules */ 476787f5fc7eSDavid Ahern __u8 l4_protocol; 476887f5fc7eSDavid Ahern __be16 sport; 476987f5fc7eSDavid Ahern __be16 dport; 477087f5fc7eSDavid Ahern 477187f5fc7eSDavid Ahern /* total length of packet from network header - used for MTU check */ 477287f5fc7eSDavid Ahern __u16 tot_len; 47734c79579bSDavid Ahern 47744c79579bSDavid Ahern /* input: L3 device index for lookup 47754c79579bSDavid Ahern * output: device index from FIB lookup 47764c79579bSDavid Ahern */ 47774c79579bSDavid Ahern __u32 ifindex; 477887f5fc7eSDavid Ahern 477987f5fc7eSDavid Ahern union { 478087f5fc7eSDavid Ahern /* inputs to lookup */ 478187f5fc7eSDavid Ahern __u8 tos; /* AF_INET */ 4782bd3a08aaSDavid Ahern __be32 flowinfo; /* AF_INET6, flow_label + priority */ 478387f5fc7eSDavid Ahern 4784fa898d76SDavid Ahern /* output: metric of fib result (IPv4/IPv6 only) */ 478587f5fc7eSDavid Ahern __u32 rt_metric; 478687f5fc7eSDavid Ahern }; 478787f5fc7eSDavid Ahern 478887f5fc7eSDavid Ahern union { 478987f5fc7eSDavid Ahern __be32 ipv4_src; 479087f5fc7eSDavid Ahern __u32 ipv6_src[4]; /* in6_addr; network order */ 479187f5fc7eSDavid Ahern }; 479287f5fc7eSDavid Ahern 4793fa898d76SDavid Ahern /* input to bpf_fib_lookup, ipv{4,6}_dst is destination address in 4794fa898d76SDavid Ahern * network header. output: bpf_fib_lookup sets to gateway address 4795fa898d76SDavid Ahern * if FIB lookup returns gateway route 479687f5fc7eSDavid Ahern */ 479787f5fc7eSDavid Ahern union { 479887f5fc7eSDavid Ahern __be32 ipv4_dst; 479987f5fc7eSDavid Ahern __u32 ipv6_dst[4]; /* in6_addr; network order */ 480087f5fc7eSDavid Ahern }; 480187f5fc7eSDavid Ahern 480287f5fc7eSDavid Ahern /* output */ 480387f5fc7eSDavid Ahern __be16 h_vlan_proto; 480487f5fc7eSDavid Ahern __be16 h_vlan_TCI; 480587f5fc7eSDavid Ahern __u8 smac[6]; /* ETH_ALEN */ 480687f5fc7eSDavid Ahern __u8 dmac[6]; /* ETH_ALEN */ 480787f5fc7eSDavid Ahern }; 480887f5fc7eSDavid Ahern 480941bdc4b4SYonghong Song enum bpf_task_fd_type { 481041bdc4b4SYonghong Song BPF_FD_TYPE_RAW_TRACEPOINT, /* tp name */ 481141bdc4b4SYonghong Song BPF_FD_TYPE_TRACEPOINT, /* tp name */ 481241bdc4b4SYonghong Song BPF_FD_TYPE_KPROBE, /* (symbol + offset) or addr */ 481341bdc4b4SYonghong Song BPF_FD_TYPE_KRETPROBE, /* (symbol + offset) or addr */ 481441bdc4b4SYonghong Song BPF_FD_TYPE_UPROBE, /* filename + offset */ 481541bdc4b4SYonghong Song BPF_FD_TYPE_URETPROBE, /* filename + offset */ 481641bdc4b4SYonghong Song }; 481741bdc4b4SYonghong Song 48181aae4bddSAndrii Nakryiko enum { 48191aae4bddSAndrii Nakryiko BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG = (1U << 0), 48201aae4bddSAndrii Nakryiko BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL = (1U << 1), 48211aae4bddSAndrii Nakryiko BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP = (1U << 2), 48221aae4bddSAndrii Nakryiko }; 4823086f9568SStanislav Fomichev 4824d58e468bSPetar Penkov struct bpf_flow_keys { 4825d58e468bSPetar Penkov __u16 nhoff; 4826d58e468bSPetar Penkov __u16 thoff; 4827d58e468bSPetar Penkov __u16 addr_proto; /* ETH_P_* of valid addrs */ 4828d58e468bSPetar Penkov __u8 is_frag; 4829d58e468bSPetar Penkov __u8 is_first_frag; 4830d58e468bSPetar Penkov __u8 is_encap; 4831d58e468bSPetar Penkov __u8 ip_proto; 4832d58e468bSPetar Penkov __be16 n_proto; 4833d58e468bSPetar Penkov __be16 sport; 4834d58e468bSPetar Penkov __be16 dport; 4835d58e468bSPetar Penkov union { 4836d58e468bSPetar Penkov struct { 4837d58e468bSPetar Penkov __be32 ipv4_src; 4838d58e468bSPetar Penkov __be32 ipv4_dst; 4839d58e468bSPetar Penkov }; 4840d58e468bSPetar Penkov struct { 4841d58e468bSPetar Penkov __u32 ipv6_src[4]; /* in6_addr; network order */ 4842d58e468bSPetar Penkov __u32 ipv6_dst[4]; /* in6_addr; network order */ 4843d58e468bSPetar Penkov }; 4844d58e468bSPetar Penkov }; 4845086f9568SStanislav Fomichev __u32 flags; 484671c99e32SStanislav Fomichev __be32 flow_label; 4847d58e468bSPetar Penkov }; 4848d58e468bSPetar Penkov 4849838e9690SYonghong Song struct bpf_func_info { 4850d30d42e0SMartin KaFai Lau __u32 insn_off; 4851838e9690SYonghong Song __u32 type_id; 4852838e9690SYonghong Song }; 4853838e9690SYonghong Song 4854c454a46bSMartin KaFai Lau #define BPF_LINE_INFO_LINE_NUM(line_col) ((line_col) >> 10) 4855c454a46bSMartin KaFai Lau #define BPF_LINE_INFO_LINE_COL(line_col) ((line_col) & 0x3ff) 4856c454a46bSMartin KaFai Lau 4857c454a46bSMartin KaFai Lau struct bpf_line_info { 4858c454a46bSMartin KaFai Lau __u32 insn_off; 4859c454a46bSMartin KaFai Lau __u32 file_name_off; 4860c454a46bSMartin KaFai Lau __u32 line_off; 4861c454a46bSMartin KaFai Lau __u32 line_col; 4862c454a46bSMartin KaFai Lau }; 4863c454a46bSMartin KaFai Lau 4864d83525caSAlexei Starovoitov struct bpf_spin_lock { 4865d83525caSAlexei Starovoitov __u32 val; 4866d83525caSAlexei Starovoitov }; 48677b146cebSAndrey Ignatov 48687b146cebSAndrey Ignatov struct bpf_sysctl { 48697b146cebSAndrey Ignatov __u32 write; /* Sysctl is being read (= 0) or written (= 1). 48707b146cebSAndrey Ignatov * Allows 1,2,4-byte read, but no write. 48717b146cebSAndrey Ignatov */ 4872e1550bfeSAndrey Ignatov __u32 file_pos; /* Sysctl file position to read from, write to. 4873e1550bfeSAndrey Ignatov * Allows 1,2,4-byte read an 4-byte write. 4874e1550bfeSAndrey Ignatov */ 48757b146cebSAndrey Ignatov }; 48767b146cebSAndrey Ignatov 48770d01da6aSStanislav Fomichev struct bpf_sockopt { 48780d01da6aSStanislav Fomichev __bpf_md_ptr(struct bpf_sock *, sk); 48790d01da6aSStanislav Fomichev __bpf_md_ptr(void *, optval); 48800d01da6aSStanislav Fomichev __bpf_md_ptr(void *, optval_end); 48810d01da6aSStanislav Fomichev 48820d01da6aSStanislav Fomichev __s32 level; 48830d01da6aSStanislav Fomichev __s32 optname; 48840d01da6aSStanislav Fomichev __s32 optlen; 48850d01da6aSStanislav Fomichev __s32 retval; 48860d01da6aSStanislav Fomichev }; 48870d01da6aSStanislav Fomichev 4888b4490c5cSCarlos Neira struct bpf_pidns_info { 4889b4490c5cSCarlos Neira __u32 pid; 4890b4490c5cSCarlos Neira __u32 tgid; 4891b4490c5cSCarlos Neira }; 4892e9ddbb77SJakub Sitnicki 4893e9ddbb77SJakub Sitnicki /* User accessible data for SK_LOOKUP programs. Add new fields at the end. */ 4894e9ddbb77SJakub Sitnicki struct bpf_sk_lookup { 4895e9ddbb77SJakub Sitnicki __bpf_md_ptr(struct bpf_sock *, sk); /* Selected socket */ 4896e9ddbb77SJakub Sitnicki 4897e9ddbb77SJakub Sitnicki __u32 family; /* Protocol family (AF_INET, AF_INET6) */ 4898e9ddbb77SJakub Sitnicki __u32 protocol; /* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */ 4899e9ddbb77SJakub Sitnicki __u32 remote_ip4; /* Network byte order */ 4900e9ddbb77SJakub Sitnicki __u32 remote_ip6[4]; /* Network byte order */ 4901e9ddbb77SJakub Sitnicki __u32 remote_port; /* Network byte order */ 4902e9ddbb77SJakub Sitnicki __u32 local_ip4; /* Network byte order */ 4903e9ddbb77SJakub Sitnicki __u32 local_ip6[4]; /* Network byte order */ 4904e9ddbb77SJakub Sitnicki __u32 local_port; /* Host byte order */ 4905e9ddbb77SJakub Sitnicki }; 4906e9ddbb77SJakub Sitnicki 4907c4d0bfb4SAlan Maguire /* 4908c4d0bfb4SAlan Maguire * struct btf_ptr is used for typed pointer representation; the 4909c4d0bfb4SAlan Maguire * type id is used to render the pointer data as the appropriate type 4910c4d0bfb4SAlan Maguire * via the bpf_snprintf_btf() helper described above. A flags field - 4911c4d0bfb4SAlan Maguire * potentially to specify additional details about the BTF pointer 4912c4d0bfb4SAlan Maguire * (rather than its mode of display) - is included for future use. 4913c4d0bfb4SAlan Maguire * Display flags - BTF_F_* - are passed to bpf_snprintf_btf separately. 4914c4d0bfb4SAlan Maguire */ 4915c4d0bfb4SAlan Maguire struct btf_ptr { 4916c4d0bfb4SAlan Maguire void *ptr; 4917c4d0bfb4SAlan Maguire __u32 type_id; 4918c4d0bfb4SAlan Maguire __u32 flags; /* BTF ptr flags; unused at present. */ 4919c4d0bfb4SAlan Maguire }; 4920c4d0bfb4SAlan Maguire 4921c4d0bfb4SAlan Maguire /* 4922c4d0bfb4SAlan Maguire * Flags to control bpf_snprintf_btf() behaviour. 4923c4d0bfb4SAlan Maguire * - BTF_F_COMPACT: no formatting around type information 4924c4d0bfb4SAlan Maguire * - BTF_F_NONAME: no struct/union member names/types 4925c4d0bfb4SAlan Maguire * - BTF_F_PTR_RAW: show raw (unobfuscated) pointer values; 4926c4d0bfb4SAlan Maguire * equivalent to %px. 4927c4d0bfb4SAlan Maguire * - BTF_F_ZERO: show zero-valued struct/union members; they 4928c4d0bfb4SAlan Maguire * are not displayed by default 4929c4d0bfb4SAlan Maguire */ 4930c4d0bfb4SAlan Maguire enum { 4931c4d0bfb4SAlan Maguire BTF_F_COMPACT = (1ULL << 0), 4932c4d0bfb4SAlan Maguire BTF_F_NONAME = (1ULL << 1), 4933c4d0bfb4SAlan Maguire BTF_F_PTR_RAW = (1ULL << 2), 4934c4d0bfb4SAlan Maguire BTF_F_ZERO = (1ULL << 3), 4935c4d0bfb4SAlan Maguire }; 4936c4d0bfb4SAlan Maguire 4937daedfb22SAlexei Starovoitov #endif /* _UAPI__LINUX_BPF_H__ */ 4938