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 */ 17daedfb22SAlexei Starovoitov #define BPF_ALU64 0x07 /* alu mode in double word width */ 18daedfb22SAlexei Starovoitov 19daedfb22SAlexei Starovoitov /* ld/ldx fields */ 20cb5f7334SJesper Dangaard Brouer #define BPF_DW 0x18 /* double word (64-bit) */ 21daedfb22SAlexei Starovoitov #define BPF_XADD 0xc0 /* exclusive add */ 22daedfb22SAlexei Starovoitov 23daedfb22SAlexei Starovoitov /* alu/jmp fields */ 24daedfb22SAlexei Starovoitov #define BPF_MOV 0xb0 /* mov reg to reg */ 25daedfb22SAlexei Starovoitov #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */ 26daedfb22SAlexei Starovoitov 27daedfb22SAlexei Starovoitov /* change endianness of a register */ 28daedfb22SAlexei Starovoitov #define BPF_END 0xd0 /* flags for endianness conversion: */ 29daedfb22SAlexei Starovoitov #define BPF_TO_LE 0x00 /* convert to little-endian */ 30daedfb22SAlexei Starovoitov #define BPF_TO_BE 0x08 /* convert to big-endian */ 31daedfb22SAlexei Starovoitov #define BPF_FROM_LE BPF_TO_LE 32daedfb22SAlexei Starovoitov #define BPF_FROM_BE BPF_TO_BE 33daedfb22SAlexei Starovoitov 3492b31a9aSDaniel Borkmann /* jmp encodings */ 35daedfb22SAlexei Starovoitov #define BPF_JNE 0x50 /* jump != */ 3692b31a9aSDaniel Borkmann #define BPF_JLT 0xa0 /* LT is unsigned, '<' */ 3792b31a9aSDaniel Borkmann #define BPF_JLE 0xb0 /* LE is unsigned, '<=' */ 38daedfb22SAlexei Starovoitov #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */ 39daedfb22SAlexei Starovoitov #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */ 4092b31a9aSDaniel Borkmann #define BPF_JSLT 0xc0 /* SLT is signed, '<' */ 4192b31a9aSDaniel Borkmann #define BPF_JSLE 0xd0 /* SLE is signed, '<=' */ 42daedfb22SAlexei Starovoitov #define BPF_CALL 0x80 /* function call */ 43daedfb22SAlexei Starovoitov #define BPF_EXIT 0x90 /* function return */ 44daedfb22SAlexei Starovoitov 45daedfb22SAlexei Starovoitov /* Register numbers */ 46daedfb22SAlexei Starovoitov enum { 47daedfb22SAlexei Starovoitov BPF_REG_0 = 0, 48daedfb22SAlexei Starovoitov BPF_REG_1, 49daedfb22SAlexei Starovoitov BPF_REG_2, 50daedfb22SAlexei Starovoitov BPF_REG_3, 51daedfb22SAlexei Starovoitov BPF_REG_4, 52daedfb22SAlexei Starovoitov BPF_REG_5, 53daedfb22SAlexei Starovoitov BPF_REG_6, 54daedfb22SAlexei Starovoitov BPF_REG_7, 55daedfb22SAlexei Starovoitov BPF_REG_8, 56daedfb22SAlexei Starovoitov BPF_REG_9, 57daedfb22SAlexei Starovoitov BPF_REG_10, 58daedfb22SAlexei Starovoitov __MAX_BPF_REG, 59daedfb22SAlexei Starovoitov }; 60daedfb22SAlexei Starovoitov 61daedfb22SAlexei Starovoitov /* BPF has 10 general purpose 64-bit registers and stack frame. */ 62daedfb22SAlexei Starovoitov #define MAX_BPF_REG __MAX_BPF_REG 63daedfb22SAlexei Starovoitov 64daedfb22SAlexei Starovoitov struct bpf_insn { 65daedfb22SAlexei Starovoitov __u8 code; /* opcode */ 66daedfb22SAlexei Starovoitov __u8 dst_reg:4; /* dest register */ 67daedfb22SAlexei Starovoitov __u8 src_reg:4; /* source register */ 68daedfb22SAlexei Starovoitov __s16 off; /* signed offset */ 69daedfb22SAlexei Starovoitov __s32 imm; /* signed immediate constant */ 70daedfb22SAlexei Starovoitov }; 71daedfb22SAlexei Starovoitov 72b95a5c4dSDaniel Mack /* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */ 73b95a5c4dSDaniel Mack struct bpf_lpm_trie_key { 74b95a5c4dSDaniel Mack __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */ 75b95a5c4dSDaniel Mack __u8 data[0]; /* Arbitrary size */ 76b95a5c4dSDaniel Mack }; 77b95a5c4dSDaniel Mack 78b2197755SDaniel Borkmann /* BPF syscall commands, see bpf(2) man-page for details. */ 7999c55f7dSAlexei Starovoitov enum bpf_cmd { 8099c55f7dSAlexei Starovoitov BPF_MAP_CREATE, 81db20fd2bSAlexei Starovoitov BPF_MAP_LOOKUP_ELEM, 82db20fd2bSAlexei Starovoitov BPF_MAP_UPDATE_ELEM, 83db20fd2bSAlexei Starovoitov BPF_MAP_DELETE_ELEM, 84db20fd2bSAlexei Starovoitov BPF_MAP_GET_NEXT_KEY, 8509756af4SAlexei Starovoitov BPF_PROG_LOAD, 86b2197755SDaniel Borkmann BPF_OBJ_PIN, 87b2197755SDaniel Borkmann BPF_OBJ_GET, 88f4324551SDaniel Mack BPF_PROG_ATTACH, 89f4324551SDaniel Mack BPF_PROG_DETACH, 901cf1cae9SAlexei Starovoitov BPF_PROG_TEST_RUN, 9134ad5580SMartin KaFai Lau BPF_PROG_GET_NEXT_ID, 9234ad5580SMartin KaFai Lau BPF_MAP_GET_NEXT_ID, 93b16d9aa4SMartin KaFai Lau BPF_PROG_GET_FD_BY_ID, 94bd5f5f4eSMartin KaFai Lau BPF_MAP_GET_FD_BY_ID, 951e270976SMartin KaFai Lau BPF_OBJ_GET_INFO_BY_FD, 96468e2f64SAlexei Starovoitov BPF_PROG_QUERY, 97c4f6699dSAlexei Starovoitov BPF_RAW_TRACEPOINT_OPEN, 98*f56a653cSMartin KaFai Lau BPF_BTF_LOAD, 9999c55f7dSAlexei Starovoitov }; 10099c55f7dSAlexei Starovoitov 10199c55f7dSAlexei Starovoitov enum bpf_map_type { 10299c55f7dSAlexei Starovoitov BPF_MAP_TYPE_UNSPEC, 1030f8e4bd8SAlexei Starovoitov BPF_MAP_TYPE_HASH, 10428fbcfa0SAlexei Starovoitov BPF_MAP_TYPE_ARRAY, 10504fd61abSAlexei Starovoitov BPF_MAP_TYPE_PROG_ARRAY, 106ea317b26SKaixu Xia BPF_MAP_TYPE_PERF_EVENT_ARRAY, 107824bd0ceSAlexei Starovoitov BPF_MAP_TYPE_PERCPU_HASH, 108a10423b8SAlexei Starovoitov BPF_MAP_TYPE_PERCPU_ARRAY, 109d5a3b1f6SAlexei Starovoitov BPF_MAP_TYPE_STACK_TRACE, 1104ed8ec52SMartin KaFai Lau BPF_MAP_TYPE_CGROUP_ARRAY, 11129ba732aSMartin KaFai Lau BPF_MAP_TYPE_LRU_HASH, 1128f844938SMartin KaFai Lau BPF_MAP_TYPE_LRU_PERCPU_HASH, 113b95a5c4dSDaniel Mack BPF_MAP_TYPE_LPM_TRIE, 11456f668dfSMartin KaFai Lau BPF_MAP_TYPE_ARRAY_OF_MAPS, 115bcc6b1b7SMartin KaFai Lau BPF_MAP_TYPE_HASH_OF_MAPS, 116546ac1ffSJohn Fastabend BPF_MAP_TYPE_DEVMAP, 117174a79ffSJohn Fastabend BPF_MAP_TYPE_SOCKMAP, 1186710e112SJesper Dangaard Brouer BPF_MAP_TYPE_CPUMAP, 11999c55f7dSAlexei Starovoitov }; 12099c55f7dSAlexei Starovoitov 12109756af4SAlexei Starovoitov enum bpf_prog_type { 12209756af4SAlexei Starovoitov BPF_PROG_TYPE_UNSPEC, 123ddd872bcSAlexei Starovoitov BPF_PROG_TYPE_SOCKET_FILTER, 1242541517cSAlexei Starovoitov BPF_PROG_TYPE_KPROBE, 12596be4325SDaniel Borkmann BPF_PROG_TYPE_SCHED_CLS, 12694caee8cSDaniel Borkmann BPF_PROG_TYPE_SCHED_ACT, 12798b5c2c6SAlexei Starovoitov BPF_PROG_TYPE_TRACEPOINT, 1286a773a15SBrenden Blanco BPF_PROG_TYPE_XDP, 1290515e599SAlexei Starovoitov BPF_PROG_TYPE_PERF_EVENT, 1300e33661dSDaniel Mack BPF_PROG_TYPE_CGROUP_SKB, 13161023658SDavid Ahern BPF_PROG_TYPE_CGROUP_SOCK, 1323a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_IN, 1333a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_OUT, 1343a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_XMIT, 13540304b2aSLawrence Brakmo BPF_PROG_TYPE_SOCK_OPS, 136b005fd18SJohn Fastabend BPF_PROG_TYPE_SK_SKB, 137ebc614f6SRoman Gushchin BPF_PROG_TYPE_CGROUP_DEVICE, 1384f738adbSJohn Fastabend BPF_PROG_TYPE_SK_MSG, 139c4f6699dSAlexei Starovoitov BPF_PROG_TYPE_RAW_TRACEPOINT, 1404fbac77dSAndrey Ignatov BPF_PROG_TYPE_CGROUP_SOCK_ADDR, 14109756af4SAlexei Starovoitov }; 14209756af4SAlexei Starovoitov 1430e33661dSDaniel Mack enum bpf_attach_type { 1440e33661dSDaniel Mack BPF_CGROUP_INET_INGRESS, 1450e33661dSDaniel Mack BPF_CGROUP_INET_EGRESS, 14661023658SDavid Ahern BPF_CGROUP_INET_SOCK_CREATE, 14740304b2aSLawrence Brakmo BPF_CGROUP_SOCK_OPS, 148464bc0fdSJohn Fastabend BPF_SK_SKB_STREAM_PARSER, 149464bc0fdSJohn Fastabend BPF_SK_SKB_STREAM_VERDICT, 150ebc614f6SRoman Gushchin BPF_CGROUP_DEVICE, 1514f738adbSJohn Fastabend BPF_SK_MSG_VERDICT, 1524fbac77dSAndrey Ignatov BPF_CGROUP_INET4_BIND, 1534fbac77dSAndrey Ignatov BPF_CGROUP_INET6_BIND, 154d74bad4eSAndrey Ignatov BPF_CGROUP_INET4_CONNECT, 155d74bad4eSAndrey Ignatov BPF_CGROUP_INET6_CONNECT, 156aac3fc32SAndrey Ignatov BPF_CGROUP_INET4_POST_BIND, 157aac3fc32SAndrey Ignatov BPF_CGROUP_INET6_POST_BIND, 1580e33661dSDaniel Mack __MAX_BPF_ATTACH_TYPE 1590e33661dSDaniel Mack }; 1600e33661dSDaniel Mack 1610e33661dSDaniel Mack #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE 1620e33661dSDaniel Mack 163324bda9eSAlexei Starovoitov /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command 164324bda9eSAlexei Starovoitov * 165324bda9eSAlexei Starovoitov * NONE(default): No further bpf programs allowed in the subtree. 166324bda9eSAlexei Starovoitov * 167324bda9eSAlexei Starovoitov * BPF_F_ALLOW_OVERRIDE: If a sub-cgroup installs some bpf program, 168324bda9eSAlexei Starovoitov * the program in this cgroup yields to sub-cgroup program. 169324bda9eSAlexei Starovoitov * 170324bda9eSAlexei Starovoitov * BPF_F_ALLOW_MULTI: If a sub-cgroup installs some bpf program, 171324bda9eSAlexei Starovoitov * that cgroup program gets run in addition to the program in this cgroup. 172324bda9eSAlexei Starovoitov * 173324bda9eSAlexei Starovoitov * Only one program is allowed to be attached to a cgroup with 174324bda9eSAlexei Starovoitov * NONE or BPF_F_ALLOW_OVERRIDE flag. 175324bda9eSAlexei Starovoitov * Attaching another program on top of NONE or BPF_F_ALLOW_OVERRIDE will 176324bda9eSAlexei Starovoitov * release old program and attach the new one. Attach flags has to match. 177324bda9eSAlexei Starovoitov * 178324bda9eSAlexei Starovoitov * Multiple programs are allowed to be attached to a cgroup with 179324bda9eSAlexei Starovoitov * BPF_F_ALLOW_MULTI flag. They are executed in FIFO order 180324bda9eSAlexei Starovoitov * (those that were attached first, run first) 181324bda9eSAlexei Starovoitov * The programs of sub-cgroup are executed first, then programs of 182324bda9eSAlexei Starovoitov * this cgroup and then programs of parent cgroup. 183324bda9eSAlexei Starovoitov * When children program makes decision (like picking TCP CA or sock bind) 184324bda9eSAlexei Starovoitov * parent program has a chance to override it. 185324bda9eSAlexei Starovoitov * 186324bda9eSAlexei Starovoitov * A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups. 187324bda9eSAlexei Starovoitov * A cgroup with NONE doesn't allow any programs in sub-cgroups. 188324bda9eSAlexei Starovoitov * Ex1: 189324bda9eSAlexei Starovoitov * cgrp1 (MULTI progs A, B) -> 190324bda9eSAlexei Starovoitov * cgrp2 (OVERRIDE prog C) -> 191324bda9eSAlexei Starovoitov * cgrp3 (MULTI prog D) -> 192324bda9eSAlexei Starovoitov * cgrp4 (OVERRIDE prog E) -> 193324bda9eSAlexei Starovoitov * cgrp5 (NONE prog F) 194324bda9eSAlexei Starovoitov * the event in cgrp5 triggers execution of F,D,A,B in that order. 195324bda9eSAlexei Starovoitov * if prog F is detached, the execution is E,D,A,B 196324bda9eSAlexei Starovoitov * if prog F and D are detached, the execution is E,A,B 197324bda9eSAlexei Starovoitov * if prog F, E and D are detached, the execution is C,A,B 198324bda9eSAlexei Starovoitov * 199324bda9eSAlexei Starovoitov * All eligible programs are executed regardless of return code from 200324bda9eSAlexei Starovoitov * earlier programs. 2017f677633SAlexei Starovoitov */ 2027f677633SAlexei Starovoitov #define BPF_F_ALLOW_OVERRIDE (1U << 0) 203324bda9eSAlexei Starovoitov #define BPF_F_ALLOW_MULTI (1U << 1) 2047f677633SAlexei Starovoitov 205e07b98d9SDavid S. Miller /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the 206e07b98d9SDavid S. Miller * verifier will perform strict alignment checking as if the kernel 207e07b98d9SDavid S. Miller * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set, 208e07b98d9SDavid S. Miller * and NET_IP_ALIGN defined to 2. 209e07b98d9SDavid S. Miller */ 210e07b98d9SDavid S. Miller #define BPF_F_STRICT_ALIGNMENT (1U << 0) 211e07b98d9SDavid S. Miller 212cc8b0b92SAlexei Starovoitov /* when bpf_ldimm64->src_reg == BPF_PSEUDO_MAP_FD, bpf_ldimm64->imm == fd */ 213f1a66f85SDaniel Borkmann #define BPF_PSEUDO_MAP_FD 1 214f1a66f85SDaniel Borkmann 215cc8b0b92SAlexei Starovoitov /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative 216cc8b0b92SAlexei Starovoitov * offset to another bpf function 217cc8b0b92SAlexei Starovoitov */ 218cc8b0b92SAlexei Starovoitov #define BPF_PSEUDO_CALL 1 219cc8b0b92SAlexei Starovoitov 2203274f520SAlexei Starovoitov /* flags for BPF_MAP_UPDATE_ELEM command */ 2213274f520SAlexei Starovoitov #define BPF_ANY 0 /* create new element or update existing */ 2223274f520SAlexei Starovoitov #define BPF_NOEXIST 1 /* create new element if it didn't exist */ 2233274f520SAlexei Starovoitov #define BPF_EXIST 2 /* update existing element */ 2243274f520SAlexei Starovoitov 22596eabe7aSMartin KaFai Lau /* flags for BPF_MAP_CREATE command */ 2266c905981SAlexei Starovoitov #define BPF_F_NO_PREALLOC (1U << 0) 22729ba732aSMartin KaFai Lau /* Instead of having one common LRU list in the 2288f844938SMartin KaFai Lau * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list 22929ba732aSMartin KaFai Lau * which can scale and perform better. 23029ba732aSMartin KaFai Lau * Note, the LRU nodes (including free nodes) cannot be moved 23129ba732aSMartin KaFai Lau * across different LRU lists. 23229ba732aSMartin KaFai Lau */ 23329ba732aSMartin KaFai Lau #define BPF_F_NO_COMMON_LRU (1U << 1) 23496eabe7aSMartin KaFai Lau /* Specify numa node during map creation */ 23596eabe7aSMartin KaFai Lau #define BPF_F_NUMA_NODE (1U << 2) 2366c905981SAlexei Starovoitov 237468e2f64SAlexei Starovoitov /* flags for BPF_PROG_QUERY */ 238468e2f64SAlexei Starovoitov #define BPF_F_QUERY_EFFECTIVE (1U << 0) 239468e2f64SAlexei Starovoitov 240cb4d2b3fSMartin KaFai Lau #define BPF_OBJ_NAME_LEN 16U 241cb4d2b3fSMartin KaFai Lau 2426e71b04aSChenbo Feng /* Flags for accessing BPF object */ 2436e71b04aSChenbo Feng #define BPF_F_RDONLY (1U << 3) 2446e71b04aSChenbo Feng #define BPF_F_WRONLY (1U << 4) 2456e71b04aSChenbo Feng 246615755a7SSong Liu /* Flag for stack_map, store build_id+offset instead of pointer */ 247615755a7SSong Liu #define BPF_F_STACK_BUILD_ID (1U << 5) 248615755a7SSong Liu 249615755a7SSong Liu enum bpf_stack_build_id_status { 250615755a7SSong Liu /* user space need an empty entry to identify end of a trace */ 251615755a7SSong Liu BPF_STACK_BUILD_ID_EMPTY = 0, 252615755a7SSong Liu /* with valid build_id and offset */ 253615755a7SSong Liu BPF_STACK_BUILD_ID_VALID = 1, 254615755a7SSong Liu /* couldn't get build_id, fallback to ip */ 255615755a7SSong Liu BPF_STACK_BUILD_ID_IP = 2, 256615755a7SSong Liu }; 257615755a7SSong Liu 258615755a7SSong Liu #define BPF_BUILD_ID_SIZE 20 259615755a7SSong Liu struct bpf_stack_build_id { 260615755a7SSong Liu __s32 status; 261615755a7SSong Liu unsigned char build_id[BPF_BUILD_ID_SIZE]; 262615755a7SSong Liu union { 263615755a7SSong Liu __u64 offset; 264615755a7SSong Liu __u64 ip; 265615755a7SSong Liu }; 266615755a7SSong Liu }; 267615755a7SSong Liu 26899c55f7dSAlexei Starovoitov union bpf_attr { 26999c55f7dSAlexei Starovoitov struct { /* anonymous struct used by BPF_MAP_CREATE command */ 27099c55f7dSAlexei Starovoitov __u32 map_type; /* one of enum bpf_map_type */ 27199c55f7dSAlexei Starovoitov __u32 key_size; /* size of key in bytes */ 27299c55f7dSAlexei Starovoitov __u32 value_size; /* size of value in bytes */ 27399c55f7dSAlexei Starovoitov __u32 max_entries; /* max number of entries in a map */ 27496eabe7aSMartin KaFai Lau __u32 map_flags; /* BPF_MAP_CREATE related 27596eabe7aSMartin KaFai Lau * flags defined above. 27696eabe7aSMartin KaFai Lau */ 27756f668dfSMartin KaFai Lau __u32 inner_map_fd; /* fd pointing to the inner map */ 27896eabe7aSMartin KaFai Lau __u32 numa_node; /* numa node (effective only if 27996eabe7aSMartin KaFai Lau * BPF_F_NUMA_NODE is set). 28096eabe7aSMartin KaFai Lau */ 281067cae47SMartin KaFai Lau char map_name[BPF_OBJ_NAME_LEN]; 282a3884572SJakub Kicinski __u32 map_ifindex; /* ifindex of netdev to create on */ 28399c55f7dSAlexei Starovoitov }; 284db20fd2bSAlexei Starovoitov 285db20fd2bSAlexei Starovoitov struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ 286db20fd2bSAlexei Starovoitov __u32 map_fd; 287db20fd2bSAlexei Starovoitov __aligned_u64 key; 288db20fd2bSAlexei Starovoitov union { 289db20fd2bSAlexei Starovoitov __aligned_u64 value; 290db20fd2bSAlexei Starovoitov __aligned_u64 next_key; 291db20fd2bSAlexei Starovoitov }; 2923274f520SAlexei Starovoitov __u64 flags; 293db20fd2bSAlexei Starovoitov }; 29409756af4SAlexei Starovoitov 29509756af4SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_LOAD command */ 29609756af4SAlexei Starovoitov __u32 prog_type; /* one of enum bpf_prog_type */ 29709756af4SAlexei Starovoitov __u32 insn_cnt; 29809756af4SAlexei Starovoitov __aligned_u64 insns; 29909756af4SAlexei Starovoitov __aligned_u64 license; 300cbd35700SAlexei Starovoitov __u32 log_level; /* verbosity level of verifier */ 301cbd35700SAlexei Starovoitov __u32 log_size; /* size of user buffer */ 302cbd35700SAlexei Starovoitov __aligned_u64 log_buf; /* user supplied buffer */ 3032541517cSAlexei Starovoitov __u32 kern_version; /* checked when prog_type=kprobe */ 304e07b98d9SDavid S. Miller __u32 prog_flags; 305067cae47SMartin KaFai Lau char prog_name[BPF_OBJ_NAME_LEN]; 3061f6f4cb7SJakub Kicinski __u32 prog_ifindex; /* ifindex of netdev to prep for */ 3075e43f899SAndrey Ignatov /* For some prog types expected attach type must be known at 3085e43f899SAndrey Ignatov * load time to verify attach type specific parts of prog 3095e43f899SAndrey Ignatov * (context accesses, allowed helpers, etc). 3105e43f899SAndrey Ignatov */ 3115e43f899SAndrey Ignatov __u32 expected_attach_type; 31209756af4SAlexei Starovoitov }; 313b2197755SDaniel Borkmann 314b2197755SDaniel Borkmann struct { /* anonymous struct used by BPF_OBJ_* commands */ 315b2197755SDaniel Borkmann __aligned_u64 pathname; 316b2197755SDaniel Borkmann __u32 bpf_fd; 3176e71b04aSChenbo Feng __u32 file_flags; 318b2197755SDaniel Borkmann }; 319f4324551SDaniel Mack 320f4324551SDaniel Mack struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ 321f4324551SDaniel Mack __u32 target_fd; /* container object to attach to */ 322f4324551SDaniel Mack __u32 attach_bpf_fd; /* eBPF program to attach */ 323f4324551SDaniel Mack __u32 attach_type; 3247f677633SAlexei Starovoitov __u32 attach_flags; 325f4324551SDaniel Mack }; 3261cf1cae9SAlexei Starovoitov 3271cf1cae9SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */ 3281cf1cae9SAlexei Starovoitov __u32 prog_fd; 3291cf1cae9SAlexei Starovoitov __u32 retval; 3301cf1cae9SAlexei Starovoitov __u32 data_size_in; 3311cf1cae9SAlexei Starovoitov __u32 data_size_out; 3321cf1cae9SAlexei Starovoitov __aligned_u64 data_in; 3331cf1cae9SAlexei Starovoitov __aligned_u64 data_out; 3341cf1cae9SAlexei Starovoitov __u32 repeat; 3351cf1cae9SAlexei Starovoitov __u32 duration; 3361cf1cae9SAlexei Starovoitov } test; 33734ad5580SMartin KaFai Lau 338b16d9aa4SMartin KaFai Lau struct { /* anonymous struct used by BPF_*_GET_*_ID */ 339b16d9aa4SMartin KaFai Lau union { 34034ad5580SMartin KaFai Lau __u32 start_id; 341b16d9aa4SMartin KaFai Lau __u32 prog_id; 342bd5f5f4eSMartin KaFai Lau __u32 map_id; 343b16d9aa4SMartin KaFai Lau }; 34434ad5580SMartin KaFai Lau __u32 next_id; 3456e71b04aSChenbo Feng __u32 open_flags; 34634ad5580SMartin KaFai Lau }; 3471e270976SMartin KaFai Lau 3481e270976SMartin KaFai Lau struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ 3491e270976SMartin KaFai Lau __u32 bpf_fd; 3501e270976SMartin KaFai Lau __u32 info_len; 3511e270976SMartin KaFai Lau __aligned_u64 info; 3521e270976SMartin KaFai Lau } info; 353468e2f64SAlexei Starovoitov 354468e2f64SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_QUERY command */ 355468e2f64SAlexei Starovoitov __u32 target_fd; /* container object to query */ 356468e2f64SAlexei Starovoitov __u32 attach_type; 357468e2f64SAlexei Starovoitov __u32 query_flags; 358468e2f64SAlexei Starovoitov __u32 attach_flags; 359468e2f64SAlexei Starovoitov __aligned_u64 prog_ids; 360468e2f64SAlexei Starovoitov __u32 prog_cnt; 361468e2f64SAlexei Starovoitov } query; 362c4f6699dSAlexei Starovoitov 363c4f6699dSAlexei Starovoitov struct { 364c4f6699dSAlexei Starovoitov __u64 name; 365c4f6699dSAlexei Starovoitov __u32 prog_fd; 366c4f6699dSAlexei Starovoitov } raw_tracepoint; 367*f56a653cSMartin KaFai Lau 368*f56a653cSMartin KaFai Lau struct { /* anonymous struct for BPF_BTF_LOAD */ 369*f56a653cSMartin KaFai Lau __aligned_u64 btf; 370*f56a653cSMartin KaFai Lau __aligned_u64 btf_log_buf; 371*f56a653cSMartin KaFai Lau __u32 btf_size; 372*f56a653cSMartin KaFai Lau __u32 btf_log_size; 373*f56a653cSMartin KaFai Lau __u32 btf_log_level; 374*f56a653cSMartin KaFai Lau }; 37599c55f7dSAlexei Starovoitov } __attribute__((aligned(8))); 37699c55f7dSAlexei Starovoitov 377ebb676daSThomas Graf /* BPF helper function descriptions: 378ebb676daSThomas Graf * 379ebb676daSThomas Graf * void *bpf_map_lookup_elem(&map, &key) 380ebb676daSThomas Graf * Return: Map value or NULL 381ebb676daSThomas Graf * 382ebb676daSThomas Graf * int bpf_map_update_elem(&map, &key, &value, flags) 383ebb676daSThomas Graf * Return: 0 on success or negative error 384ebb676daSThomas Graf * 385ebb676daSThomas Graf * int bpf_map_delete_elem(&map, &key) 386ebb676daSThomas Graf * Return: 0 on success or negative error 387ebb676daSThomas Graf * 388ebb676daSThomas Graf * int bpf_probe_read(void *dst, int size, void *src) 389ebb676daSThomas Graf * Return: 0 on success or negative error 390ebb676daSThomas Graf * 391ebb676daSThomas Graf * u64 bpf_ktime_get_ns(void) 392ebb676daSThomas Graf * Return: current ktime 393ebb676daSThomas Graf * 394ebb676daSThomas Graf * int bpf_trace_printk(const char *fmt, int fmt_size, ...) 395ebb676daSThomas Graf * Return: length of buffer written or negative error 396ebb676daSThomas Graf * 397ebb676daSThomas Graf * u32 bpf_prandom_u32(void) 398ebb676daSThomas Graf * Return: random value 399ebb676daSThomas Graf * 400ebb676daSThomas Graf * u32 bpf_raw_smp_processor_id(void) 401ebb676daSThomas Graf * Return: SMP processor ID 402ebb676daSThomas Graf * 403ebb676daSThomas Graf * int bpf_skb_store_bytes(skb, offset, from, len, flags) 404ebb676daSThomas Graf * store bytes into packet 40591bc4822SAlexei Starovoitov * @skb: pointer to skb 406a166151cSAlexei Starovoitov * @offset: offset within packet from skb->mac_header 40791bc4822SAlexei Starovoitov * @from: pointer where to copy bytes from 40891bc4822SAlexei Starovoitov * @len: number of bytes to store into packet 40991bc4822SAlexei Starovoitov * @flags: bit 0 - if true, recompute skb->csum 41091bc4822SAlexei Starovoitov * other bits - reserved 411ebb676daSThomas Graf * Return: 0 on success or negative error 412ebb676daSThomas Graf * 413ebb676daSThomas Graf * int bpf_l3_csum_replace(skb, offset, from, to, flags) 414ebb676daSThomas Graf * recompute IP checksum 41591bc4822SAlexei Starovoitov * @skb: pointer to skb 41691bc4822SAlexei Starovoitov * @offset: offset within packet where IP checksum is located 41791bc4822SAlexei Starovoitov * @from: old value of header field 41891bc4822SAlexei Starovoitov * @to: new value of header field 41991bc4822SAlexei Starovoitov * @flags: bits 0-3 - size of header field 42091bc4822SAlexei Starovoitov * other bits - reserved 421ebb676daSThomas Graf * Return: 0 on success or negative error 422ebb676daSThomas Graf * 423ebb676daSThomas Graf * int bpf_l4_csum_replace(skb, offset, from, to, flags) 424ebb676daSThomas Graf * recompute TCP/UDP checksum 42591bc4822SAlexei Starovoitov * @skb: pointer to skb 42691bc4822SAlexei Starovoitov * @offset: offset within packet where TCP/UDP checksum is located 42791bc4822SAlexei Starovoitov * @from: old value of header field 42891bc4822SAlexei Starovoitov * @to: new value of header field 42991bc4822SAlexei Starovoitov * @flags: bits 0-3 - size of header field 43091bc4822SAlexei Starovoitov * bit 4 - is pseudo header 43191bc4822SAlexei Starovoitov * other bits - reserved 432ebb676daSThomas Graf * Return: 0 on success or negative error 433ebb676daSThomas Graf * 434ebb676daSThomas Graf * int bpf_tail_call(ctx, prog_array_map, index) 435ebb676daSThomas Graf * jump into another BPF program 43604fd61abSAlexei Starovoitov * @ctx: context pointer passed to next program 43704fd61abSAlexei Starovoitov * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY 43890caccddSAlexei Starovoitov * @index: 32-bit index inside array that selects specific program to run 439ebb676daSThomas Graf * Return: 0 on success or negative error 440ebb676daSThomas Graf * 441ebb676daSThomas Graf * int bpf_clone_redirect(skb, ifindex, flags) 442ebb676daSThomas Graf * redirect to another netdev 4433896d655SAlexei Starovoitov * @skb: pointer to skb 4443896d655SAlexei Starovoitov * @ifindex: ifindex of the net device 4453896d655SAlexei Starovoitov * @flags: bit 0 - if set, redirect to ingress instead of egress 4463896d655SAlexei Starovoitov * other bits - reserved 447ebb676daSThomas Graf * Return: 0 on success or negative error 448ebb676daSThomas Graf * 449ffeedafbSAlexei Starovoitov * u64 bpf_get_current_pid_tgid(void) 450ffeedafbSAlexei Starovoitov * Return: current->tgid << 32 | current->pid 451ebb676daSThomas Graf * 452ffeedafbSAlexei Starovoitov * u64 bpf_get_current_uid_gid(void) 453ffeedafbSAlexei Starovoitov * Return: current_gid << 32 | current_uid 454ebb676daSThomas Graf * 455ebb676daSThomas Graf * int bpf_get_current_comm(char *buf, int size_of_buf) 456ffeedafbSAlexei Starovoitov * stores current->comm into buf 457ebb676daSThomas Graf * Return: 0 on success or negative error 458ebb676daSThomas Graf * 459ebb676daSThomas Graf * u32 bpf_get_cgroup_classid(skb) 460ebb676daSThomas Graf * retrieve a proc's classid 4618d20aabeSDaniel Borkmann * @skb: pointer to skb 4628d20aabeSDaniel Borkmann * Return: classid if != 0 463ebb676daSThomas Graf * 464ebb676daSThomas Graf * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci) 465ebb676daSThomas Graf * Return: 0 on success or negative error 466ebb676daSThomas Graf * 467ebb676daSThomas Graf * int bpf_skb_vlan_pop(skb) 468ebb676daSThomas Graf * Return: 0 on success or negative error 469ebb676daSThomas Graf * 470ebb676daSThomas Graf * int bpf_skb_get_tunnel_key(skb, key, size, flags) 471ebb676daSThomas Graf * int bpf_skb_set_tunnel_key(skb, key, size, flags) 472d3aa45ceSAlexei Starovoitov * retrieve or populate tunnel metadata 473d3aa45ceSAlexei Starovoitov * @skb: pointer to skb 474d3aa45ceSAlexei Starovoitov * @key: pointer to 'struct bpf_tunnel_key' 475d3aa45ceSAlexei Starovoitov * @size: size of 'struct bpf_tunnel_key' 476d3aa45ceSAlexei Starovoitov * @flags: room for future extensions 477ebb676daSThomas Graf * Return: 0 on success or negative error 478ebb676daSThomas Graf * 479b7d3ed5bSTeng Qin * u64 bpf_perf_event_read(map, flags) 480b7d3ed5bSTeng Qin * read perf event counter value 481b7d3ed5bSTeng Qin * @map: pointer to perf_event_array map 482b7d3ed5bSTeng Qin * @flags: index of event in the map or bitmask flags 483b7d3ed5bSTeng Qin * Return: value of perf event counter read or error code 484ebb676daSThomas Graf * 485ebb676daSThomas Graf * int bpf_redirect(ifindex, flags) 486ebb676daSThomas Graf * redirect to another netdev 48727b29f63SAlexei Starovoitov * @ifindex: ifindex of the net device 48856ce097cSJohn Fastabend * @flags: 48956ce097cSJohn Fastabend * cls_bpf: 49056ce097cSJohn Fastabend * bit 0 - if set, redirect to ingress instead of egress 49127b29f63SAlexei Starovoitov * other bits - reserved 49256ce097cSJohn Fastabend * xdp_bpf: 49356ce097cSJohn Fastabend * all bits - reserved 49456ce097cSJohn Fastabend * Return: cls_bpf: TC_ACT_REDIRECT on success or TC_ACT_SHOT on error 49556ce097cSJohn Fastabend * xdp_bfp: XDP_REDIRECT on success or XDP_ABORT on error 49656ce097cSJohn Fastabend * int bpf_redirect_map(map, key, flags) 49797f91a7cSJohn Fastabend * redirect to endpoint in map 49856ce097cSJohn Fastabend * @map: pointer to dev map 49997f91a7cSJohn Fastabend * @key: index in map to lookup 50097f91a7cSJohn Fastabend * @flags: -- 50156ce097cSJohn Fastabend * Return: XDP_REDIRECT on success or XDP_ABORT on error 502ebb676daSThomas Graf * 503ebb676daSThomas Graf * u32 bpf_get_route_realm(skb) 504ebb676daSThomas Graf * retrieve a dst's tclassid 505c46646d0SDaniel Borkmann * @skb: pointer to skb 506c46646d0SDaniel Borkmann * Return: realm if != 0 507ebb676daSThomas Graf * 508b7d3ed5bSTeng Qin * int bpf_perf_event_output(ctx, map, flags, data, size) 509ebb676daSThomas Graf * output perf raw sample 510a43eec30SAlexei Starovoitov * @ctx: struct pt_regs* 511a43eec30SAlexei Starovoitov * @map: pointer to perf_event_array map 512b7d3ed5bSTeng Qin * @flags: index of event in the map or bitmask flags 513a43eec30SAlexei Starovoitov * @data: data on stack to be output as raw data 514a43eec30SAlexei Starovoitov * @size: size of data 515ebb676daSThomas Graf * Return: 0 on success or negative error 516ebb676daSThomas Graf * 517ebb676daSThomas Graf * int bpf_get_stackid(ctx, map, flags) 518ebb676daSThomas Graf * walk user or kernel stack and return id 519d5a3b1f6SAlexei Starovoitov * @ctx: struct pt_regs* 520d5a3b1f6SAlexei Starovoitov * @map: pointer to stack_trace map 521d5a3b1f6SAlexei Starovoitov * @flags: bits 0-7 - numer of stack frames to skip 522d5a3b1f6SAlexei Starovoitov * bit 8 - collect user stack instead of kernel 523d5a3b1f6SAlexei Starovoitov * bit 9 - compare stacks by hash only 524d5a3b1f6SAlexei Starovoitov * bit 10 - if two different stacks hash into the same stackid 525d5a3b1f6SAlexei Starovoitov * discard old 526d5a3b1f6SAlexei Starovoitov * other bits - reserved 527d5a3b1f6SAlexei Starovoitov * Return: >= 0 stackid on success or negative error 528ebb676daSThomas Graf * 529ebb676daSThomas Graf * s64 bpf_csum_diff(from, from_size, to, to_size, seed) 530ebb676daSThomas Graf * calculate csum diff 5317d672345SDaniel Borkmann * @from: raw from buffer 5327d672345SDaniel Borkmann * @from_size: length of from buffer 5337d672345SDaniel Borkmann * @to: raw to buffer 5347d672345SDaniel Borkmann * @to_size: length of to buffer 5357d672345SDaniel Borkmann * @seed: optional seed 536ebb676daSThomas Graf * Return: csum result or negative error code 537ebb676daSThomas Graf * 538ebb676daSThomas Graf * int bpf_skb_get_tunnel_opt(skb, opt, size) 539ebb676daSThomas Graf * retrieve tunnel options metadata 54014ca0751SDaniel Borkmann * @skb: pointer to skb 54114ca0751SDaniel Borkmann * @opt: pointer to raw tunnel option data 54214ca0751SDaniel Borkmann * @size: size of @opt 543ebb676daSThomas Graf * Return: option size 544ebb676daSThomas Graf * 545ebb676daSThomas Graf * int bpf_skb_set_tunnel_opt(skb, opt, size) 546ebb676daSThomas Graf * populate tunnel options metadata 547ebb676daSThomas Graf * @skb: pointer to skb 548ebb676daSThomas Graf * @opt: pointer to raw tunnel option data 549ebb676daSThomas Graf * @size: size of @opt 550ebb676daSThomas Graf * Return: 0 on success or negative error 551ebb676daSThomas Graf * 552ebb676daSThomas Graf * int bpf_skb_change_proto(skb, proto, flags) 553ebb676daSThomas Graf * Change protocol of the skb. Currently supported is v4 -> v6, 554ebb676daSThomas Graf * v6 -> v4 transitions. The helper will also resize the skb. eBPF 555ebb676daSThomas Graf * program is expected to fill the new headers via skb_store_bytes 556ebb676daSThomas Graf * and lX_csum_replace. 5576578171aSDaniel Borkmann * @skb: pointer to skb 5586578171aSDaniel Borkmann * @proto: new skb->protocol type 5596578171aSDaniel Borkmann * @flags: reserved 5606578171aSDaniel Borkmann * Return: 0 on success or negative error 561ebb676daSThomas Graf * 562ebb676daSThomas Graf * int bpf_skb_change_type(skb, type) 563d2485c42SDaniel Borkmann * Change packet type of skb. 564d2485c42SDaniel Borkmann * @skb: pointer to skb 565d2485c42SDaniel Borkmann * @type: new skb->pkt_type type 566d2485c42SDaniel Borkmann * Return: 0 on success or negative error 567ebb676daSThomas Graf * 568ebb676daSThomas Graf * int bpf_skb_under_cgroup(skb, map, index) 569ebb676daSThomas Graf * Check cgroup2 membership of skb 5704a482f34SMartin KaFai Lau * @skb: pointer to skb 5714a482f34SMartin KaFai Lau * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type 5724a482f34SMartin KaFai Lau * @index: index of the cgroup in the bpf_map 5734a482f34SMartin KaFai Lau * Return: 5744a482f34SMartin KaFai Lau * == 0 skb failed the cgroup2 descendant test 5754a482f34SMartin KaFai Lau * == 1 skb succeeded the cgroup2 descendant test 5764a482f34SMartin KaFai Lau * < 0 error 577ebb676daSThomas Graf * 578ebb676daSThomas Graf * u32 bpf_get_hash_recalc(skb) 57913c5c240SDaniel Borkmann * Retrieve and possibly recalculate skb->hash. 58013c5c240SDaniel Borkmann * @skb: pointer to skb 58113c5c240SDaniel Borkmann * Return: hash 582ebb676daSThomas Graf * 583606274c5SAlexei Starovoitov * u64 bpf_get_current_task(void) 584606274c5SAlexei Starovoitov * Returns current task_struct 585606274c5SAlexei Starovoitov * Return: current 586ebb676daSThomas Graf * 587ebb676daSThomas Graf * int bpf_probe_write_user(void *dst, void *src, int len) 58896ae5227SSargun Dhillon * safely attempt to write to a location 58996ae5227SSargun Dhillon * @dst: destination address in userspace 59096ae5227SSargun Dhillon * @src: source address on stack 59196ae5227SSargun Dhillon * @len: number of bytes to copy 59296ae5227SSargun Dhillon * Return: 0 on success or negative error 593ebb676daSThomas Graf * 594ebb676daSThomas Graf * int bpf_current_task_under_cgroup(map, index) 595ebb676daSThomas Graf * Check cgroup2 membership of current task 59660d20f91SSargun Dhillon * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type 59760d20f91SSargun Dhillon * @index: index of the cgroup in the bpf_map 59860d20f91SSargun Dhillon * Return: 59960d20f91SSargun Dhillon * == 0 current failed the cgroup2 descendant test 60060d20f91SSargun Dhillon * == 1 current succeeded the cgroup2 descendant test 60160d20f91SSargun Dhillon * < 0 error 602ebb676daSThomas Graf * 603ebb676daSThomas Graf * int bpf_skb_change_tail(skb, len, flags) 604ebb676daSThomas Graf * The helper will resize the skb to the given new size, to be used f.e. 605ebb676daSThomas Graf * with control messages. 6065293efe6SDaniel Borkmann * @skb: pointer to skb 6075293efe6SDaniel Borkmann * @len: new skb length 6085293efe6SDaniel Borkmann * @flags: reserved 6095293efe6SDaniel Borkmann * Return: 0 on success or negative error 610ebb676daSThomas Graf * 611ebb676daSThomas Graf * int bpf_skb_pull_data(skb, len) 612ebb676daSThomas Graf * The helper will pull in non-linear data in case the skb is non-linear 613ebb676daSThomas Graf * and not all of len are part of the linear section. Only needed for 614ebb676daSThomas Graf * read/write with direct packet access. 61536bbef52SDaniel Borkmann * @skb: pointer to skb 61636bbef52SDaniel Borkmann * @len: len to make read/writeable 61736bbef52SDaniel Borkmann * Return: 0 on success or negative error 618ebb676daSThomas Graf * 619ebb676daSThomas Graf * s64 bpf_csum_update(skb, csum) 62036bbef52SDaniel Borkmann * Adds csum into skb->csum in case of CHECKSUM_COMPLETE. 62136bbef52SDaniel Borkmann * @skb: pointer to skb 62236bbef52SDaniel Borkmann * @csum: csum to add 62336bbef52SDaniel Borkmann * Return: csum on success or negative error 624ebb676daSThomas Graf * 625ebb676daSThomas Graf * void bpf_set_hash_invalid(skb) 626ebb676daSThomas Graf * Invalidate current skb->hash. 6277a4b28c6SDaniel Borkmann * @skb: pointer to skb 628ebb676daSThomas Graf * 629ebb676daSThomas Graf * int bpf_get_numa_node_id() 630ebb676daSThomas Graf * Return: Id of current NUMA node. 6313a0af8fdSThomas Graf * 6323a0af8fdSThomas Graf * int bpf_skb_change_head() 6333a0af8fdSThomas Graf * Grows headroom of skb and adjusts MAC header offset accordingly. 6343a0af8fdSThomas Graf * Will extends/reallocae as required automatically. 6353a0af8fdSThomas Graf * May change skb data pointer and will thus invalidate any check 6363a0af8fdSThomas Graf * performed for direct packet access. 6373a0af8fdSThomas Graf * @skb: pointer to skb 6383a0af8fdSThomas Graf * @len: length of header to be pushed in front 6393a0af8fdSThomas Graf * @flags: Flags (unused for now) 6403a0af8fdSThomas Graf * Return: 0 on success or negative error 64117bedab2SMartin KaFai Lau * 64217bedab2SMartin KaFai Lau * int bpf_xdp_adjust_head(xdp_md, delta) 64317bedab2SMartin KaFai Lau * Adjust the xdp_md.data by delta 64417bedab2SMartin KaFai Lau * @xdp_md: pointer to xdp_md 64517bedab2SMartin KaFai Lau * @delta: An positive/negative integer to be added to xdp_md.data 64617bedab2SMartin KaFai Lau * Return: 0 on success or negative on error 647a5e8c070SGianluca Borello * 648a5e8c070SGianluca Borello * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr) 649a5e8c070SGianluca Borello * Copy a NUL terminated string from unsafe address. In case the string 650a5e8c070SGianluca Borello * length is smaller than size, the target is not padded with further NUL 651a5e8c070SGianluca Borello * bytes. In case the string length is larger than size, just count-1 652a5e8c070SGianluca Borello * bytes are copied and the last byte is set to NUL. 653a5e8c070SGianluca Borello * @dst: destination address 654a5e8c070SGianluca Borello * @size: maximum number of bytes to copy, including the trailing NUL 655a5e8c070SGianluca Borello * @unsafe_ptr: unsafe address 656a5e8c070SGianluca Borello * Return: 657a5e8c070SGianluca Borello * > 0 length of the string including the trailing NUL on success 658a5e8c070SGianluca Borello * < 0 error 65991b8270fSChenbo Feng * 6603c60a531SAlexander Alemayhu * u64 bpf_get_socket_cookie(skb) 66191b8270fSChenbo Feng * Get the cookie for the socket stored inside sk_buff. 66291b8270fSChenbo Feng * @skb: pointer to skb 66391b8270fSChenbo Feng * Return: 8 Bytes non-decreasing number on success or 0 if the socket 66491b8270fSChenbo Feng * field is missing inside sk_buff 6656acc5c29SChenbo Feng * 6666acc5c29SChenbo Feng * u32 bpf_get_socket_uid(skb) 6676acc5c29SChenbo Feng * Get the owner uid of the socket stored inside sk_buff. 6686acc5c29SChenbo Feng * @skb: pointer to skb 6695d4e3443SChenbo Feng * Return: uid of the socket owner on success or overflowuid if failed. 670ded092cdSDaniel Borkmann * 671ded092cdSDaniel Borkmann * u32 bpf_set_hash(skb, hash) 672ded092cdSDaniel Borkmann * Set full skb->hash. 673ded092cdSDaniel Borkmann * @skb: pointer to skb 674ded092cdSDaniel Borkmann * @hash: hash to set 6758c4b4c7eSLawrence Brakmo * 6768c4b4c7eSLawrence Brakmo * int bpf_setsockopt(bpf_socket, level, optname, optval, optlen) 6778c4b4c7eSLawrence Brakmo * Calls setsockopt. Not all opts are available, only those with 6788c4b4c7eSLawrence Brakmo * integer optvals plus TCP_CONGESTION. 679cd86d1fdSLawrence Brakmo * Supported levels: SOL_SOCKET and IPPROTO_TCP 6808c4b4c7eSLawrence Brakmo * @bpf_socket: pointer to bpf_socket 681cd86d1fdSLawrence Brakmo * @level: SOL_SOCKET or IPPROTO_TCP 6828c4b4c7eSLawrence Brakmo * @optname: option name 6838c4b4c7eSLawrence Brakmo * @optval: pointer to option value 684cd86d1fdSLawrence Brakmo * @optlen: length of optval in bytes 685cd86d1fdSLawrence Brakmo * Return: 0 or negative error 686cd86d1fdSLawrence Brakmo * 687cd86d1fdSLawrence Brakmo * int bpf_getsockopt(bpf_socket, level, optname, optval, optlen) 688cd86d1fdSLawrence Brakmo * Calls getsockopt. Not all opts are available. 689cd86d1fdSLawrence Brakmo * Supported levels: IPPROTO_TCP 690cd86d1fdSLawrence Brakmo * @bpf_socket: pointer to bpf_socket 691cd86d1fdSLawrence Brakmo * @level: IPPROTO_TCP 692cd86d1fdSLawrence Brakmo * @optname: option name 693cd86d1fdSLawrence Brakmo * @optval: pointer to option value 694cd86d1fdSLawrence Brakmo * @optlen: length of optval in bytes 6958c4b4c7eSLawrence Brakmo * Return: 0 or negative error 6962be7e212SDaniel Borkmann * 697b13d8807SLawrence Brakmo * int bpf_sock_ops_cb_flags_set(bpf_sock_ops, flags) 698b13d8807SLawrence Brakmo * Set callback flags for sock_ops 699b13d8807SLawrence Brakmo * @bpf_sock_ops: pointer to bpf_sock_ops_kern struct 700b13d8807SLawrence Brakmo * @flags: flags value 701b13d8807SLawrence Brakmo * Return: 0 for no error 702b13d8807SLawrence Brakmo * -EINVAL if there is no full tcp socket 703b13d8807SLawrence Brakmo * bits in flags that are not supported by current kernel 704b13d8807SLawrence Brakmo * 7052be7e212SDaniel Borkmann * int bpf_skb_adjust_room(skb, len_diff, mode, flags) 7062be7e212SDaniel Borkmann * Grow or shrink room in sk_buff. 7072be7e212SDaniel Borkmann * @skb: pointer to skb 7082be7e212SDaniel Borkmann * @len_diff: (signed) amount of room to grow/shrink 7092be7e212SDaniel Borkmann * @mode: operation mode (enum bpf_adj_room_mode) 7102be7e212SDaniel Borkmann * @flags: reserved for future use 7112be7e212SDaniel Borkmann * Return: 0 on success or negative error code 712174a79ffSJohn Fastabend * 713174a79ffSJohn Fastabend * int bpf_sk_redirect_map(map, key, flags) 714174a79ffSJohn Fastabend * Redirect skb to a sock in map using key as a lookup key for the 715174a79ffSJohn Fastabend * sock in map. 716174a79ffSJohn Fastabend * @map: pointer to sockmap 717174a79ffSJohn Fastabend * @key: key to lookup sock in map 718174a79ffSJohn Fastabend * @flags: reserved for future use 719bfa64075SJohn Fastabend * Return: SK_PASS 720174a79ffSJohn Fastabend * 721464bc0fdSJohn Fastabend * int bpf_sock_map_update(skops, map, key, flags) 722174a79ffSJohn Fastabend * @skops: pointer to bpf_sock_ops 723174a79ffSJohn Fastabend * @map: pointer to sockmap to update 724174a79ffSJohn Fastabend * @key: key to insert/update sock in map 725174a79ffSJohn Fastabend * @flags: same flags as map update elem 726de8f3a83SDaniel Borkmann * 727de8f3a83SDaniel Borkmann * int bpf_xdp_adjust_meta(xdp_md, delta) 728de8f3a83SDaniel Borkmann * Adjust the xdp_md.data_meta by delta 729de8f3a83SDaniel Borkmann * @xdp_md: pointer to xdp_md 730de8f3a83SDaniel Borkmann * @delta: An positive/negative integer to be added to xdp_md.data_meta 731de8f3a83SDaniel Borkmann * Return: 0 on success or negative on error 732908432caSYonghong Song * 733908432caSYonghong Song * int bpf_perf_event_read_value(map, flags, buf, buf_size) 734908432caSYonghong Song * read perf event counter value and perf event enabled/running time 735908432caSYonghong Song * @map: pointer to perf_event_array map 736908432caSYonghong Song * @flags: index of event in the map or bitmask flags 737908432caSYonghong Song * @buf: buf to fill 738908432caSYonghong Song * @buf_size: size of the buf 739908432caSYonghong Song * Return: 0 on success or negative error code 7404bebdc7aSYonghong Song * 7414bebdc7aSYonghong Song * int bpf_perf_prog_read_value(ctx, buf, buf_size) 7424bebdc7aSYonghong Song * read perf prog attached perf event counter and enabled/running time 7434bebdc7aSYonghong Song * @ctx: pointer to ctx 7444bebdc7aSYonghong Song * @buf: buf to fill 7454bebdc7aSYonghong Song * @buf_size: size of the buf 7464bebdc7aSYonghong Song * Return : 0 on success or negative error code 7479802d865SJosef Bacik * 7489802d865SJosef Bacik * int bpf_override_return(pt_regs, rc) 7499802d865SJosef Bacik * @pt_regs: pointer to struct pt_regs 7509802d865SJosef Bacik * @rc: the return value to set 7514f738adbSJohn Fastabend * 7524f738adbSJohn Fastabend * int bpf_msg_redirect_map(map, key, flags) 7534f738adbSJohn Fastabend * Redirect msg to a sock in map using key as a lookup key for the 7544f738adbSJohn Fastabend * sock in map. 7554f738adbSJohn Fastabend * @map: pointer to sockmap 7564f738adbSJohn Fastabend * @key: key to lookup sock in map 7574f738adbSJohn Fastabend * @flags: reserved for future use 7584f738adbSJohn Fastabend * Return: SK_PASS 7594f738adbSJohn Fastabend * 760d74bad4eSAndrey Ignatov * int bpf_bind(ctx, addr, addr_len) 761d74bad4eSAndrey Ignatov * Bind socket to address. Only binding to IP is supported, no port can be 762d74bad4eSAndrey Ignatov * set in addr. 763d74bad4eSAndrey Ignatov * @ctx: pointer to context of type bpf_sock_addr 764d74bad4eSAndrey Ignatov * @addr: pointer to struct sockaddr to bind socket to 765d74bad4eSAndrey Ignatov * @addr_len: length of sockaddr structure 766d74bad4eSAndrey Ignatov * Return: 0 on success or negative error code 767b32cc5b9SNikita V. Shirokov * 768b32cc5b9SNikita V. Shirokov * int bpf_xdp_adjust_tail(xdp_md, delta) 769b32cc5b9SNikita V. Shirokov * Adjust the xdp_md.data_end by delta. Only shrinking of packet's 770b32cc5b9SNikita V. Shirokov * size is supported. 771b32cc5b9SNikita V. Shirokov * @xdp_md: pointer to xdp_md 772b32cc5b9SNikita V. Shirokov * @delta: A negative integer to be added to xdp_md.data_end 773b32cc5b9SNikita V. Shirokov * Return: 0 on success or negative on error 7747a4b28c6SDaniel Borkmann */ 775ebb676daSThomas Graf #define __BPF_FUNC_MAPPER(FN) \ 776ebb676daSThomas Graf FN(unspec), \ 777ebb676daSThomas Graf FN(map_lookup_elem), \ 778ebb676daSThomas Graf FN(map_update_elem), \ 779ebb676daSThomas Graf FN(map_delete_elem), \ 780ebb676daSThomas Graf FN(probe_read), \ 781ebb676daSThomas Graf FN(ktime_get_ns), \ 782ebb676daSThomas Graf FN(trace_printk), \ 783ebb676daSThomas Graf FN(get_prandom_u32), \ 784ebb676daSThomas Graf FN(get_smp_processor_id), \ 785ebb676daSThomas Graf FN(skb_store_bytes), \ 786ebb676daSThomas Graf FN(l3_csum_replace), \ 787ebb676daSThomas Graf FN(l4_csum_replace), \ 788ebb676daSThomas Graf FN(tail_call), \ 789ebb676daSThomas Graf FN(clone_redirect), \ 790ebb676daSThomas Graf FN(get_current_pid_tgid), \ 791ebb676daSThomas Graf FN(get_current_uid_gid), \ 792ebb676daSThomas Graf FN(get_current_comm), \ 793ebb676daSThomas Graf FN(get_cgroup_classid), \ 794ebb676daSThomas Graf FN(skb_vlan_push), \ 795ebb676daSThomas Graf FN(skb_vlan_pop), \ 796ebb676daSThomas Graf FN(skb_get_tunnel_key), \ 797ebb676daSThomas Graf FN(skb_set_tunnel_key), \ 798ebb676daSThomas Graf FN(perf_event_read), \ 799ebb676daSThomas Graf FN(redirect), \ 800ebb676daSThomas Graf FN(get_route_realm), \ 801ebb676daSThomas Graf FN(perf_event_output), \ 802ebb676daSThomas Graf FN(skb_load_bytes), \ 803ebb676daSThomas Graf FN(get_stackid), \ 804ebb676daSThomas Graf FN(csum_diff), \ 805ebb676daSThomas Graf FN(skb_get_tunnel_opt), \ 806ebb676daSThomas Graf FN(skb_set_tunnel_opt), \ 807ebb676daSThomas Graf FN(skb_change_proto), \ 808ebb676daSThomas Graf FN(skb_change_type), \ 809ebb676daSThomas Graf FN(skb_under_cgroup), \ 810ebb676daSThomas Graf FN(get_hash_recalc), \ 811ebb676daSThomas Graf FN(get_current_task), \ 812ebb676daSThomas Graf FN(probe_write_user), \ 813ebb676daSThomas Graf FN(current_task_under_cgroup), \ 814ebb676daSThomas Graf FN(skb_change_tail), \ 815ebb676daSThomas Graf FN(skb_pull_data), \ 816ebb676daSThomas Graf FN(csum_update), \ 817ebb676daSThomas Graf FN(set_hash_invalid), \ 8183a0af8fdSThomas Graf FN(get_numa_node_id), \ 81917bedab2SMartin KaFai Lau FN(skb_change_head), \ 820a5e8c070SGianluca Borello FN(xdp_adjust_head), \ 82191b8270fSChenbo Feng FN(probe_read_str), \ 8226acc5c29SChenbo Feng FN(get_socket_cookie), \ 823ded092cdSDaniel Borkmann FN(get_socket_uid), \ 8248c4b4c7eSLawrence Brakmo FN(set_hash), \ 8252be7e212SDaniel Borkmann FN(setsockopt), \ 82697f91a7cSJohn Fastabend FN(skb_adjust_room), \ 827174a79ffSJohn Fastabend FN(redirect_map), \ 828174a79ffSJohn Fastabend FN(sk_redirect_map), \ 829174a79ffSJohn Fastabend FN(sock_map_update), \ 830908432caSYonghong Song FN(xdp_adjust_meta), \ 8314bebdc7aSYonghong Song FN(perf_event_read_value), \ 832cd86d1fdSLawrence Brakmo FN(perf_prog_read_value), \ 8339802d865SJosef Bacik FN(getsockopt), \ 834b13d8807SLawrence Brakmo FN(override_return), \ 8354f738adbSJohn Fastabend FN(sock_ops_cb_flags_set), \ 8362a100317SJohn Fastabend FN(msg_redirect_map), \ 83791843d54SJohn Fastabend FN(msg_apply_bytes), \ 838015632bbSJohn Fastabend FN(msg_cork_bytes), \ 839d74bad4eSAndrey Ignatov FN(msg_pull_data), \ 840b32cc5b9SNikita V. Shirokov FN(bind), \ 841b32cc5b9SNikita V. Shirokov FN(xdp_adjust_tail), 8427a4b28c6SDaniel Borkmann 843ebb676daSThomas Graf /* integer value in 'imm' field of BPF_CALL instruction selects which helper 844ebb676daSThomas Graf * function eBPF program intends to call 8452d0e30c3SDaniel Borkmann */ 846ebb676daSThomas Graf #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x 847ebb676daSThomas Graf enum bpf_func_id { 848ebb676daSThomas Graf __BPF_FUNC_MAPPER(__BPF_ENUM_FN) 84909756af4SAlexei Starovoitov __BPF_FUNC_MAX_ID, 85009756af4SAlexei Starovoitov }; 851ebb676daSThomas Graf #undef __BPF_ENUM_FN 85209756af4SAlexei Starovoitov 853781c53bcSDaniel Borkmann /* All flags used by eBPF helper functions, placed here. */ 854781c53bcSDaniel Borkmann 855781c53bcSDaniel Borkmann /* BPF_FUNC_skb_store_bytes flags. */ 856781c53bcSDaniel Borkmann #define BPF_F_RECOMPUTE_CSUM (1ULL << 0) 8578afd54c8SDaniel Borkmann #define BPF_F_INVALIDATE_HASH (1ULL << 1) 858781c53bcSDaniel Borkmann 859781c53bcSDaniel Borkmann /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags. 860781c53bcSDaniel Borkmann * First 4 bits are for passing the header field size. 861781c53bcSDaniel Borkmann */ 862781c53bcSDaniel Borkmann #define BPF_F_HDR_FIELD_MASK 0xfULL 863781c53bcSDaniel Borkmann 864781c53bcSDaniel Borkmann /* BPF_FUNC_l4_csum_replace flags. */ 865781c53bcSDaniel Borkmann #define BPF_F_PSEUDO_HDR (1ULL << 4) 8662f72959aSDaniel Borkmann #define BPF_F_MARK_MANGLED_0 (1ULL << 5) 867d1b662adSDaniel Borkmann #define BPF_F_MARK_ENFORCE (1ULL << 6) 868781c53bcSDaniel Borkmann 869781c53bcSDaniel Borkmann /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */ 870781c53bcSDaniel Borkmann #define BPF_F_INGRESS (1ULL << 0) 871781c53bcSDaniel Borkmann 872c6c33454SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */ 873c6c33454SDaniel Borkmann #define BPF_F_TUNINFO_IPV6 (1ULL << 0) 874c6c33454SDaniel Borkmann 875d5a3b1f6SAlexei Starovoitov /* BPF_FUNC_get_stackid flags. */ 876d5a3b1f6SAlexei Starovoitov #define BPF_F_SKIP_FIELD_MASK 0xffULL 877d5a3b1f6SAlexei Starovoitov #define BPF_F_USER_STACK (1ULL << 8) 878d5a3b1f6SAlexei Starovoitov #define BPF_F_FAST_STACK_CMP (1ULL << 9) 879d5a3b1f6SAlexei Starovoitov #define BPF_F_REUSE_STACKID (1ULL << 10) 880d5a3b1f6SAlexei Starovoitov 8812da897e5SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key flags. */ 8822da897e5SDaniel Borkmann #define BPF_F_ZERO_CSUM_TX (1ULL << 1) 88322080870SDaniel Borkmann #define BPF_F_DONT_FRAGMENT (1ULL << 2) 88477a5196aSWilliam Tu #define BPF_F_SEQ_NUMBER (1ULL << 3) 8852da897e5SDaniel Borkmann 886908432caSYonghong Song /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and 887908432caSYonghong Song * BPF_FUNC_perf_event_read_value flags. 888908432caSYonghong Song */ 8891e33759cSDaniel Borkmann #define BPF_F_INDEX_MASK 0xffffffffULL 8901e33759cSDaniel Borkmann #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK 891555c8a86SDaniel Borkmann /* BPF_FUNC_perf_event_output for sk_buff input context. */ 892555c8a86SDaniel Borkmann #define BPF_F_CTXLEN_MASK (0xfffffULL << 32) 8931e33759cSDaniel Borkmann 8942be7e212SDaniel Borkmann /* Mode for BPF_FUNC_skb_adjust_room helper. */ 8952be7e212SDaniel Borkmann enum bpf_adj_room_mode { 8962be7e212SDaniel Borkmann BPF_ADJ_ROOM_NET, 8972be7e212SDaniel Borkmann }; 8982be7e212SDaniel Borkmann 8999bac3d6dSAlexei Starovoitov /* user accessible mirror of in-kernel sk_buff. 9009bac3d6dSAlexei Starovoitov * new fields can only be added to the end of this structure 9019bac3d6dSAlexei Starovoitov */ 9029bac3d6dSAlexei Starovoitov struct __sk_buff { 9039bac3d6dSAlexei Starovoitov __u32 len; 9049bac3d6dSAlexei Starovoitov __u32 pkt_type; 9059bac3d6dSAlexei Starovoitov __u32 mark; 9069bac3d6dSAlexei Starovoitov __u32 queue_mapping; 907c2497395SAlexei Starovoitov __u32 protocol; 908c2497395SAlexei Starovoitov __u32 vlan_present; 909c2497395SAlexei Starovoitov __u32 vlan_tci; 91027cd5452SMichal Sekletar __u32 vlan_proto; 911bcad5718SDaniel Borkmann __u32 priority; 91237e82c2fSAlexei Starovoitov __u32 ingress_ifindex; 91337e82c2fSAlexei Starovoitov __u32 ifindex; 914d691f9e8SAlexei Starovoitov __u32 tc_index; 915d691f9e8SAlexei Starovoitov __u32 cb[5]; 916ba7591d8SDaniel Borkmann __u32 hash; 917045efa82SDaniel Borkmann __u32 tc_classid; 918969bf05eSAlexei Starovoitov __u32 data; 919969bf05eSAlexei Starovoitov __u32 data_end; 920b1d9fc41SDaniel Borkmann __u32 napi_id; 9218a31db56SJohn Fastabend 922de8f3a83SDaniel Borkmann /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */ 9238a31db56SJohn Fastabend __u32 family; 9248a31db56SJohn Fastabend __u32 remote_ip4; /* Stored in network byte order */ 9258a31db56SJohn Fastabend __u32 local_ip4; /* Stored in network byte order */ 9268a31db56SJohn Fastabend __u32 remote_ip6[4]; /* Stored in network byte order */ 9278a31db56SJohn Fastabend __u32 local_ip6[4]; /* Stored in network byte order */ 9288a31db56SJohn Fastabend __u32 remote_port; /* Stored in network byte order */ 9298a31db56SJohn Fastabend __u32 local_port; /* stored in host byte order */ 930de8f3a83SDaniel Borkmann /* ... here. */ 931de8f3a83SDaniel Borkmann 932de8f3a83SDaniel Borkmann __u32 data_meta; 9339bac3d6dSAlexei Starovoitov }; 9349bac3d6dSAlexei Starovoitov 935d3aa45ceSAlexei Starovoitov struct bpf_tunnel_key { 936d3aa45ceSAlexei Starovoitov __u32 tunnel_id; 937c6c33454SDaniel Borkmann union { 938d3aa45ceSAlexei Starovoitov __u32 remote_ipv4; 939c6c33454SDaniel Borkmann __u32 remote_ipv6[4]; 940c6c33454SDaniel Borkmann }; 941c6c33454SDaniel Borkmann __u8 tunnel_tos; 942c6c33454SDaniel Borkmann __u8 tunnel_ttl; 943c0e760c9SDaniel Borkmann __u16 tunnel_ext; 9444018ab18SDaniel Borkmann __u32 tunnel_label; 945d3aa45ceSAlexei Starovoitov }; 946d3aa45ceSAlexei Starovoitov 9473a0af8fdSThomas Graf /* Generic BPF return codes which all BPF program types may support. 9483a0af8fdSThomas Graf * The values are binary compatible with their TC_ACT_* counter-part to 9493a0af8fdSThomas Graf * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT 9503a0af8fdSThomas Graf * programs. 9513a0af8fdSThomas Graf * 9523a0af8fdSThomas Graf * XDP is handled seprately, see XDP_*. 9533a0af8fdSThomas Graf */ 9543a0af8fdSThomas Graf enum bpf_ret_code { 9553a0af8fdSThomas Graf BPF_OK = 0, 9563a0af8fdSThomas Graf /* 1 reserved */ 9573a0af8fdSThomas Graf BPF_DROP = 2, 9583a0af8fdSThomas Graf /* 3-6 reserved */ 9593a0af8fdSThomas Graf BPF_REDIRECT = 7, 9603a0af8fdSThomas Graf /* >127 are reserved for prog type specific return codes */ 9613a0af8fdSThomas Graf }; 9623a0af8fdSThomas Graf 96361023658SDavid Ahern struct bpf_sock { 96461023658SDavid Ahern __u32 bound_dev_if; 965aa4c1037SDavid Ahern __u32 family; 966aa4c1037SDavid Ahern __u32 type; 967aa4c1037SDavid Ahern __u32 protocol; 968482dca93SDavid Ahern __u32 mark; 969482dca93SDavid Ahern __u32 priority; 970aac3fc32SAndrey Ignatov __u32 src_ip4; /* Allows 1,2,4-byte read. 971aac3fc32SAndrey Ignatov * Stored in network byte order. 972aac3fc32SAndrey Ignatov */ 973aac3fc32SAndrey Ignatov __u32 src_ip6[4]; /* Allows 1,2,4-byte read. 974aac3fc32SAndrey Ignatov * Stored in network byte order. 975aac3fc32SAndrey Ignatov */ 976aac3fc32SAndrey Ignatov __u32 src_port; /* Allows 4-byte read. 977aac3fc32SAndrey Ignatov * Stored in host byte order 978aac3fc32SAndrey Ignatov */ 97961023658SDavid Ahern }; 98061023658SDavid Ahern 98117bedab2SMartin KaFai Lau #define XDP_PACKET_HEADROOM 256 98217bedab2SMartin KaFai Lau 9836a773a15SBrenden Blanco /* User return codes for XDP prog type. 9846a773a15SBrenden Blanco * A valid XDP program must return one of these defined values. All other 9859beb8bedSDaniel Borkmann * return codes are reserved for future use. Unknown return codes will 9869beb8bedSDaniel Borkmann * result in packet drops and a warning via bpf_warn_invalid_xdp_action(). 9876a773a15SBrenden Blanco */ 9886a773a15SBrenden Blanco enum xdp_action { 9896a773a15SBrenden Blanco XDP_ABORTED = 0, 9906a773a15SBrenden Blanco XDP_DROP, 9916a773a15SBrenden Blanco XDP_PASS, 9926ce96ca3SBrenden Blanco XDP_TX, 993814abfabSJohn Fastabend XDP_REDIRECT, 9946a773a15SBrenden Blanco }; 9956a773a15SBrenden Blanco 9966a773a15SBrenden Blanco /* user accessible metadata for XDP packet hook 9976a773a15SBrenden Blanco * new fields must be added to the end of this structure 9986a773a15SBrenden Blanco */ 9996a773a15SBrenden Blanco struct xdp_md { 10006a773a15SBrenden Blanco __u32 data; 10016a773a15SBrenden Blanco __u32 data_end; 1002de8f3a83SDaniel Borkmann __u32 data_meta; 1003daaf24c6SJesper Dangaard Brouer /* Below access go through struct xdp_rxq_info */ 100402dd3291SJesper Dangaard Brouer __u32 ingress_ifindex; /* rxq->dev->ifindex */ 100502dd3291SJesper Dangaard Brouer __u32 rx_queue_index; /* rxq->queue_index */ 10066a773a15SBrenden Blanco }; 10076a773a15SBrenden Blanco 1008174a79ffSJohn Fastabend enum sk_action { 1009bfa64075SJohn Fastabend SK_DROP = 0, 1010bfa64075SJohn Fastabend SK_PASS, 1011174a79ffSJohn Fastabend }; 1012174a79ffSJohn Fastabend 10134f738adbSJohn Fastabend /* user accessible metadata for SK_MSG packet hook, new fields must 10144f738adbSJohn Fastabend * be added to the end of this structure 10154f738adbSJohn Fastabend */ 10164f738adbSJohn Fastabend struct sk_msg_md { 10174f738adbSJohn Fastabend void *data; 10184f738adbSJohn Fastabend void *data_end; 10194f738adbSJohn Fastabend }; 10204f738adbSJohn Fastabend 10211e270976SMartin KaFai Lau #define BPF_TAG_SIZE 8 10221e270976SMartin KaFai Lau 10231e270976SMartin KaFai Lau struct bpf_prog_info { 10241e270976SMartin KaFai Lau __u32 type; 10251e270976SMartin KaFai Lau __u32 id; 10261e270976SMartin KaFai Lau __u8 tag[BPF_TAG_SIZE]; 10271e270976SMartin KaFai Lau __u32 jited_prog_len; 10281e270976SMartin KaFai Lau __u32 xlated_prog_len; 10291e270976SMartin KaFai Lau __aligned_u64 jited_prog_insns; 10301e270976SMartin KaFai Lau __aligned_u64 xlated_prog_insns; 1031cb4d2b3fSMartin KaFai Lau __u64 load_time; /* ns since boottime */ 1032cb4d2b3fSMartin KaFai Lau __u32 created_by_uid; 1033cb4d2b3fSMartin KaFai Lau __u32 nr_map_ids; 1034cb4d2b3fSMartin KaFai Lau __aligned_u64 map_ids; 1035067cae47SMartin KaFai Lau char name[BPF_OBJ_NAME_LEN]; 1036675fc275SJakub Kicinski __u32 ifindex; 1037675fc275SJakub Kicinski __u64 netns_dev; 1038675fc275SJakub Kicinski __u64 netns_ino; 10391e270976SMartin KaFai Lau } __attribute__((aligned(8))); 10401e270976SMartin KaFai Lau 10411e270976SMartin KaFai Lau struct bpf_map_info { 10421e270976SMartin KaFai Lau __u32 type; 10431e270976SMartin KaFai Lau __u32 id; 10441e270976SMartin KaFai Lau __u32 key_size; 10451e270976SMartin KaFai Lau __u32 value_size; 10461e270976SMartin KaFai Lau __u32 max_entries; 10471e270976SMartin KaFai Lau __u32 map_flags; 1048067cae47SMartin KaFai Lau char name[BPF_OBJ_NAME_LEN]; 104952775b33SJakub Kicinski __u32 ifindex; 105052775b33SJakub Kicinski __u64 netns_dev; 105152775b33SJakub Kicinski __u64 netns_ino; 10521e270976SMartin KaFai Lau } __attribute__((aligned(8))); 10531e270976SMartin KaFai Lau 10544fbac77dSAndrey Ignatov /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed 10554fbac77dSAndrey Ignatov * by user and intended to be used by socket (e.g. to bind to, depends on 10564fbac77dSAndrey Ignatov * attach attach type). 10574fbac77dSAndrey Ignatov */ 10584fbac77dSAndrey Ignatov struct bpf_sock_addr { 10594fbac77dSAndrey Ignatov __u32 user_family; /* Allows 4-byte read, but no write. */ 10604fbac77dSAndrey Ignatov __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write. 10614fbac77dSAndrey Ignatov * Stored in network byte order. 10624fbac77dSAndrey Ignatov */ 10634fbac77dSAndrey Ignatov __u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write. 10644fbac77dSAndrey Ignatov * Stored in network byte order. 10654fbac77dSAndrey Ignatov */ 10664fbac77dSAndrey Ignatov __u32 user_port; /* Allows 4-byte read and write. 10674fbac77dSAndrey Ignatov * Stored in network byte order 10684fbac77dSAndrey Ignatov */ 10694fbac77dSAndrey Ignatov __u32 family; /* Allows 4-byte read, but no write */ 10704fbac77dSAndrey Ignatov __u32 type; /* Allows 4-byte read, but no write */ 10714fbac77dSAndrey Ignatov __u32 protocol; /* Allows 4-byte read, but no write */ 10724fbac77dSAndrey Ignatov }; 10734fbac77dSAndrey Ignatov 107440304b2aSLawrence Brakmo /* User bpf_sock_ops struct to access socket values and specify request ops 107540304b2aSLawrence Brakmo * and their replies. 107640304b2aSLawrence Brakmo * Some of this fields are in network (bigendian) byte order and may need 107740304b2aSLawrence Brakmo * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h). 107840304b2aSLawrence Brakmo * New fields can only be added at the end of this structure 107940304b2aSLawrence Brakmo */ 108040304b2aSLawrence Brakmo struct bpf_sock_ops { 108140304b2aSLawrence Brakmo __u32 op; 108240304b2aSLawrence Brakmo union { 1083de525be2SLawrence Brakmo __u32 args[4]; /* Optionally passed to bpf program */ 1084de525be2SLawrence Brakmo __u32 reply; /* Returned by bpf program */ 1085de525be2SLawrence Brakmo __u32 replylong[4]; /* Optionally returned by bpf prog */ 108640304b2aSLawrence Brakmo }; 108740304b2aSLawrence Brakmo __u32 family; 108840304b2aSLawrence Brakmo __u32 remote_ip4; /* Stored in network byte order */ 108940304b2aSLawrence Brakmo __u32 local_ip4; /* Stored in network byte order */ 109040304b2aSLawrence Brakmo __u32 remote_ip6[4]; /* Stored in network byte order */ 109140304b2aSLawrence Brakmo __u32 local_ip6[4]; /* Stored in network byte order */ 109240304b2aSLawrence Brakmo __u32 remote_port; /* Stored in network byte order */ 109340304b2aSLawrence Brakmo __u32 local_port; /* stored in host byte order */ 1094f19397a5SLawrence Brakmo __u32 is_fullsock; /* Some TCP fields are only valid if 1095f19397a5SLawrence Brakmo * there is a full socket. If not, the 1096f19397a5SLawrence Brakmo * fields read as zero. 1097f19397a5SLawrence Brakmo */ 1098f19397a5SLawrence Brakmo __u32 snd_cwnd; 1099f19397a5SLawrence Brakmo __u32 srtt_us; /* Averaged RTT << 3 in usecs */ 1100b13d8807SLawrence Brakmo __u32 bpf_sock_ops_cb_flags; /* flags defined in uapi/linux/tcp.h */ 110144f0e430SLawrence Brakmo __u32 state; 110244f0e430SLawrence Brakmo __u32 rtt_min; 110344f0e430SLawrence Brakmo __u32 snd_ssthresh; 110444f0e430SLawrence Brakmo __u32 rcv_nxt; 110544f0e430SLawrence Brakmo __u32 snd_nxt; 110644f0e430SLawrence Brakmo __u32 snd_una; 110744f0e430SLawrence Brakmo __u32 mss_cache; 110844f0e430SLawrence Brakmo __u32 ecn_flags; 110944f0e430SLawrence Brakmo __u32 rate_delivered; 111044f0e430SLawrence Brakmo __u32 rate_interval_us; 111144f0e430SLawrence Brakmo __u32 packets_out; 111244f0e430SLawrence Brakmo __u32 retrans_out; 111344f0e430SLawrence Brakmo __u32 total_retrans; 111444f0e430SLawrence Brakmo __u32 segs_in; 111544f0e430SLawrence Brakmo __u32 data_segs_in; 111644f0e430SLawrence Brakmo __u32 segs_out; 111744f0e430SLawrence Brakmo __u32 data_segs_out; 111844f0e430SLawrence Brakmo __u32 lost_out; 111944f0e430SLawrence Brakmo __u32 sacked_out; 112044f0e430SLawrence Brakmo __u32 sk_txhash; 112144f0e430SLawrence Brakmo __u64 bytes_received; 112244f0e430SLawrence Brakmo __u64 bytes_acked; 112340304b2aSLawrence Brakmo }; 112440304b2aSLawrence Brakmo 1125b13d8807SLawrence Brakmo /* Definitions for bpf_sock_ops_cb_flags */ 1126f89013f6SLawrence Brakmo #define BPF_SOCK_OPS_RTO_CB_FLAG (1<<0) 1127a31ad29eSLawrence Brakmo #define BPF_SOCK_OPS_RETRANS_CB_FLAG (1<<1) 1128d4487491SLawrence Brakmo #define BPF_SOCK_OPS_STATE_CB_FLAG (1<<2) 1129d4487491SLawrence Brakmo #define BPF_SOCK_OPS_ALL_CB_FLAGS 0x7 /* Mask of all currently 1130b13d8807SLawrence Brakmo * supported cb flags 1131b13d8807SLawrence Brakmo */ 1132b13d8807SLawrence Brakmo 113340304b2aSLawrence Brakmo /* List of known BPF sock_ops operators. 113440304b2aSLawrence Brakmo * New entries can only be added at the end 113540304b2aSLawrence Brakmo */ 113640304b2aSLawrence Brakmo enum { 113740304b2aSLawrence Brakmo BPF_SOCK_OPS_VOID, 11388550f328SLawrence Brakmo BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or 11398550f328SLawrence Brakmo * -1 if default value should be used 11408550f328SLawrence Brakmo */ 114113d3b1ebSLawrence Brakmo BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized 114213d3b1ebSLawrence Brakmo * window (in packets) or -1 if default 114313d3b1ebSLawrence Brakmo * value should be used 114413d3b1ebSLawrence Brakmo */ 11459872a4bdSLawrence Brakmo BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an 11469872a4bdSLawrence Brakmo * active connection is initialized 11479872a4bdSLawrence Brakmo */ 11489872a4bdSLawrence Brakmo BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an 11499872a4bdSLawrence Brakmo * active connection is 11509872a4bdSLawrence Brakmo * established 11519872a4bdSLawrence Brakmo */ 11529872a4bdSLawrence Brakmo BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a 11539872a4bdSLawrence Brakmo * passive connection is 11549872a4bdSLawrence Brakmo * established 11559872a4bdSLawrence Brakmo */ 115691b5b21cSLawrence Brakmo BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control 115791b5b21cSLawrence Brakmo * needs ECN 115891b5b21cSLawrence Brakmo */ 1159e6546ef6SLawrence Brakmo BPF_SOCK_OPS_BASE_RTT, /* Get base RTT. The correct value is 1160e6546ef6SLawrence Brakmo * based on the path and may be 1161e6546ef6SLawrence Brakmo * dependent on the congestion control 1162e6546ef6SLawrence Brakmo * algorithm. In general it indicates 1163e6546ef6SLawrence Brakmo * a congestion threshold. RTTs above 1164e6546ef6SLawrence Brakmo * this indicate congestion 1165e6546ef6SLawrence Brakmo */ 1166f89013f6SLawrence Brakmo BPF_SOCK_OPS_RTO_CB, /* Called when an RTO has triggered. 1167f89013f6SLawrence Brakmo * Arg1: value of icsk_retransmits 1168f89013f6SLawrence Brakmo * Arg2: value of icsk_rto 1169f89013f6SLawrence Brakmo * Arg3: whether RTO has expired 1170f89013f6SLawrence Brakmo */ 1171a31ad29eSLawrence Brakmo BPF_SOCK_OPS_RETRANS_CB, /* Called when skb is retransmitted. 1172a31ad29eSLawrence Brakmo * Arg1: sequence number of 1st byte 1173a31ad29eSLawrence Brakmo * Arg2: # segments 1174a31ad29eSLawrence Brakmo * Arg3: return value of 1175a31ad29eSLawrence Brakmo * tcp_transmit_skb (0 => success) 1176a31ad29eSLawrence Brakmo */ 1177d4487491SLawrence Brakmo BPF_SOCK_OPS_STATE_CB, /* Called when TCP changes state. 1178d4487491SLawrence Brakmo * Arg1: old_state 1179d4487491SLawrence Brakmo * Arg2: new_state 1180d4487491SLawrence Brakmo */ 1181d4487491SLawrence Brakmo }; 1182d4487491SLawrence Brakmo 1183d4487491SLawrence Brakmo /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect 1184d4487491SLawrence Brakmo * changes between the TCP and BPF versions. Ideally this should never happen. 1185d4487491SLawrence Brakmo * If it does, we need to add code to convert them before calling 1186d4487491SLawrence Brakmo * the BPF sock_ops function. 1187d4487491SLawrence Brakmo */ 1188d4487491SLawrence Brakmo enum { 1189d4487491SLawrence Brakmo BPF_TCP_ESTABLISHED = 1, 1190d4487491SLawrence Brakmo BPF_TCP_SYN_SENT, 1191d4487491SLawrence Brakmo BPF_TCP_SYN_RECV, 1192d4487491SLawrence Brakmo BPF_TCP_FIN_WAIT1, 1193d4487491SLawrence Brakmo BPF_TCP_FIN_WAIT2, 1194d4487491SLawrence Brakmo BPF_TCP_TIME_WAIT, 1195d4487491SLawrence Brakmo BPF_TCP_CLOSE, 1196d4487491SLawrence Brakmo BPF_TCP_CLOSE_WAIT, 1197d4487491SLawrence Brakmo BPF_TCP_LAST_ACK, 1198d4487491SLawrence Brakmo BPF_TCP_LISTEN, 1199d4487491SLawrence Brakmo BPF_TCP_CLOSING, /* Now a valid state */ 1200d4487491SLawrence Brakmo BPF_TCP_NEW_SYN_RECV, 1201d4487491SLawrence Brakmo 1202d4487491SLawrence Brakmo BPF_TCP_MAX_STATES /* Leave at the end! */ 120340304b2aSLawrence Brakmo }; 120440304b2aSLawrence Brakmo 1205fc747810SLawrence Brakmo #define TCP_BPF_IW 1001 /* Set TCP initial congestion window */ 120613bf9641SLawrence Brakmo #define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */ 1207fc747810SLawrence Brakmo 1208908432caSYonghong Song struct bpf_perf_event_value { 1209908432caSYonghong Song __u64 counter; 1210908432caSYonghong Song __u64 enabled; 1211908432caSYonghong Song __u64 running; 1212908432caSYonghong Song }; 1213908432caSYonghong Song 1214ebc614f6SRoman Gushchin #define BPF_DEVCG_ACC_MKNOD (1ULL << 0) 1215ebc614f6SRoman Gushchin #define BPF_DEVCG_ACC_READ (1ULL << 1) 1216ebc614f6SRoman Gushchin #define BPF_DEVCG_ACC_WRITE (1ULL << 2) 1217ebc614f6SRoman Gushchin 1218ebc614f6SRoman Gushchin #define BPF_DEVCG_DEV_BLOCK (1ULL << 0) 1219ebc614f6SRoman Gushchin #define BPF_DEVCG_DEV_CHAR (1ULL << 1) 1220ebc614f6SRoman Gushchin 1221ebc614f6SRoman Gushchin struct bpf_cgroup_dev_ctx { 122206ef0ccbSYonghong Song /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */ 122306ef0ccbSYonghong Song __u32 access_type; 1224ebc614f6SRoman Gushchin __u32 major; 1225ebc614f6SRoman Gushchin __u32 minor; 1226ebc614f6SRoman Gushchin }; 1227ebc614f6SRoman Gushchin 1228c4f6699dSAlexei Starovoitov struct bpf_raw_tracepoint_args { 1229c4f6699dSAlexei Starovoitov __u64 args[0]; 1230c4f6699dSAlexei Starovoitov }; 1231c4f6699dSAlexei Starovoitov 1232daedfb22SAlexei Starovoitov #endif /* _UAPI__LINUX_BPF_H__ */ 1233