1*f7c14bbaSAndroid Build Coastguard Worker /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2*f7c14bbaSAndroid Build Coastguard Worker 3*f7c14bbaSAndroid Build Coastguard Worker /* 4*f7c14bbaSAndroid Build Coastguard Worker * Common BPF ELF operations. 5*f7c14bbaSAndroid Build Coastguard Worker * 6*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2013-2015 Alexei Starovoitov <[email protected]> 7*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2015 Wang Nan <[email protected]> 8*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2015 Huawei Inc. 9*f7c14bbaSAndroid Build Coastguard Worker * 10*f7c14bbaSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or 11*f7c14bbaSAndroid Build Coastguard Worker * modify it under the terms of the GNU Lesser General Public 12*f7c14bbaSAndroid Build Coastguard Worker * License as published by the Free Software Foundation; 13*f7c14bbaSAndroid Build Coastguard Worker * version 2.1 of the License (not later!) 14*f7c14bbaSAndroid Build Coastguard Worker * 15*f7c14bbaSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful, 16*f7c14bbaSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*f7c14bbaSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*f7c14bbaSAndroid Build Coastguard Worker * GNU Lesser General Public License for more details. 19*f7c14bbaSAndroid Build Coastguard Worker * 20*f7c14bbaSAndroid Build Coastguard Worker * You should have received a copy of the GNU Lesser General Public 21*f7c14bbaSAndroid Build Coastguard Worker * License along with this program; if not, see <http://www.gnu.org/licenses> 22*f7c14bbaSAndroid Build Coastguard Worker */ 23*f7c14bbaSAndroid Build Coastguard Worker #ifndef __LIBBPF_BPF_H 24*f7c14bbaSAndroid Build Coastguard Worker #define __LIBBPF_BPF_H 25*f7c14bbaSAndroid Build Coastguard Worker 26*f7c14bbaSAndroid Build Coastguard Worker #include <linux/bpf.h> 27*f7c14bbaSAndroid Build Coastguard Worker #include <stdbool.h> 28*f7c14bbaSAndroid Build Coastguard Worker #include <stddef.h> 29*f7c14bbaSAndroid Build Coastguard Worker #include <stdint.h> 30*f7c14bbaSAndroid Build Coastguard Worker 31*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_common.h" 32*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_legacy.h" 33*f7c14bbaSAndroid Build Coastguard Worker 34*f7c14bbaSAndroid Build Coastguard Worker #ifdef __cplusplus 35*f7c14bbaSAndroid Build Coastguard Worker extern "C" { 36*f7c14bbaSAndroid Build Coastguard Worker #endif 37*f7c14bbaSAndroid Build Coastguard Worker 38*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes); 39*f7c14bbaSAndroid Build Coastguard Worker 40*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_create_opts { 41*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 42*f7c14bbaSAndroid Build Coastguard Worker 43*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_fd; 44*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_key_type_id; 45*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_value_type_id; 46*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_vmlinux_value_type_id; 47*f7c14bbaSAndroid Build Coastguard Worker 48*f7c14bbaSAndroid Build Coastguard Worker __u32 inner_map_fd; 49*f7c14bbaSAndroid Build Coastguard Worker __u32 map_flags; 50*f7c14bbaSAndroid Build Coastguard Worker __u64 map_extra; 51*f7c14bbaSAndroid Build Coastguard Worker 52*f7c14bbaSAndroid Build Coastguard Worker __u32 numa_node; 53*f7c14bbaSAndroid Build Coastguard Worker __u32 map_ifindex; 54*f7c14bbaSAndroid Build Coastguard Worker __s32 value_type_btf_obj_fd; 55*f7c14bbaSAndroid Build Coastguard Worker 56*f7c14bbaSAndroid Build Coastguard Worker __u32 token_fd; 57*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 58*f7c14bbaSAndroid Build Coastguard Worker }; 59*f7c14bbaSAndroid Build Coastguard Worker #define bpf_map_create_opts__last_field token_fd 60*f7c14bbaSAndroid Build Coastguard Worker 61*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, 62*f7c14bbaSAndroid Build Coastguard Worker const char *map_name, 63*f7c14bbaSAndroid Build Coastguard Worker __u32 key_size, 64*f7c14bbaSAndroid Build Coastguard Worker __u32 value_size, 65*f7c14bbaSAndroid Build Coastguard Worker __u32 max_entries, 66*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_map_create_opts *opts); 67*f7c14bbaSAndroid Build Coastguard Worker 68*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_load_opts { 69*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 70*f7c14bbaSAndroid Build Coastguard Worker 71*f7c14bbaSAndroid Build Coastguard Worker /* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns 72*f7c14bbaSAndroid Build Coastguard Worker * -EAGAIN. This field determines how many attempts libbpf has to 73*f7c14bbaSAndroid Build Coastguard Worker * make. If not specified, libbpf will use default value of 5. 74*f7c14bbaSAndroid Build Coastguard Worker */ 75*f7c14bbaSAndroid Build Coastguard Worker int attempts; 76*f7c14bbaSAndroid Build Coastguard Worker 77*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type expected_attach_type; 78*f7c14bbaSAndroid Build Coastguard Worker __u32 prog_btf_fd; 79*f7c14bbaSAndroid Build Coastguard Worker __u32 prog_flags; 80*f7c14bbaSAndroid Build Coastguard Worker __u32 prog_ifindex; 81*f7c14bbaSAndroid Build Coastguard Worker __u32 kern_version; 82*f7c14bbaSAndroid Build Coastguard Worker 83*f7c14bbaSAndroid Build Coastguard Worker __u32 attach_btf_id; 84*f7c14bbaSAndroid Build Coastguard Worker __u32 attach_prog_fd; 85*f7c14bbaSAndroid Build Coastguard Worker __u32 attach_btf_obj_fd; 86*f7c14bbaSAndroid Build Coastguard Worker 87*f7c14bbaSAndroid Build Coastguard Worker const int *fd_array; 88*f7c14bbaSAndroid Build Coastguard Worker 89*f7c14bbaSAndroid Build Coastguard Worker /* .BTF.ext func info data */ 90*f7c14bbaSAndroid Build Coastguard Worker const void *func_info; 91*f7c14bbaSAndroid Build Coastguard Worker __u32 func_info_cnt; 92*f7c14bbaSAndroid Build Coastguard Worker __u32 func_info_rec_size; 93*f7c14bbaSAndroid Build Coastguard Worker 94*f7c14bbaSAndroid Build Coastguard Worker /* .BTF.ext line info data */ 95*f7c14bbaSAndroid Build Coastguard Worker const void *line_info; 96*f7c14bbaSAndroid Build Coastguard Worker __u32 line_info_cnt; 97*f7c14bbaSAndroid Build Coastguard Worker __u32 line_info_rec_size; 98*f7c14bbaSAndroid Build Coastguard Worker 99*f7c14bbaSAndroid Build Coastguard Worker /* verifier log options */ 100*f7c14bbaSAndroid Build Coastguard Worker __u32 log_level; 101*f7c14bbaSAndroid Build Coastguard Worker __u32 log_size; 102*f7c14bbaSAndroid Build Coastguard Worker char *log_buf; 103*f7c14bbaSAndroid Build Coastguard Worker /* output: actual total log contents size (including termintaing zero). 104*f7c14bbaSAndroid Build Coastguard Worker * It could be both larger than original log_size (if log was 105*f7c14bbaSAndroid Build Coastguard Worker * truncated), or smaller (if log buffer wasn't filled completely). 106*f7c14bbaSAndroid Build Coastguard Worker * If kernel doesn't support this feature, log_size is left unchanged. 107*f7c14bbaSAndroid Build Coastguard Worker */ 108*f7c14bbaSAndroid Build Coastguard Worker __u32 log_true_size; 109*f7c14bbaSAndroid Build Coastguard Worker __u32 token_fd; 110*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 111*f7c14bbaSAndroid Build Coastguard Worker }; 112*f7c14bbaSAndroid Build Coastguard Worker #define bpf_prog_load_opts__last_field token_fd 113*f7c14bbaSAndroid Build Coastguard Worker 114*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type, 115*f7c14bbaSAndroid Build Coastguard Worker const char *prog_name, const char *license, 116*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_insn *insns, size_t insn_cnt, 117*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_load_opts *opts); 118*f7c14bbaSAndroid Build Coastguard Worker 119*f7c14bbaSAndroid Build Coastguard Worker /* Flags to direct loading requirements */ 120*f7c14bbaSAndroid Build Coastguard Worker #define MAPS_RELAX_COMPAT 0x01 121*f7c14bbaSAndroid Build Coastguard Worker 122*f7c14bbaSAndroid Build Coastguard Worker /* Recommended log buffer size */ 123*f7c14bbaSAndroid Build Coastguard Worker #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */ 124*f7c14bbaSAndroid Build Coastguard Worker 125*f7c14bbaSAndroid Build Coastguard Worker struct bpf_btf_load_opts { 126*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 127*f7c14bbaSAndroid Build Coastguard Worker 128*f7c14bbaSAndroid Build Coastguard Worker /* kernel log options */ 129*f7c14bbaSAndroid Build Coastguard Worker char *log_buf; 130*f7c14bbaSAndroid Build Coastguard Worker __u32 log_level; 131*f7c14bbaSAndroid Build Coastguard Worker __u32 log_size; 132*f7c14bbaSAndroid Build Coastguard Worker /* output: actual total log contents size (including termintaing zero). 133*f7c14bbaSAndroid Build Coastguard Worker * It could be both larger than original log_size (if log was 134*f7c14bbaSAndroid Build Coastguard Worker * truncated), or smaller (if log buffer wasn't filled completely). 135*f7c14bbaSAndroid Build Coastguard Worker * If kernel doesn't support this feature, log_size is left unchanged. 136*f7c14bbaSAndroid Build Coastguard Worker */ 137*f7c14bbaSAndroid Build Coastguard Worker __u32 log_true_size; 138*f7c14bbaSAndroid Build Coastguard Worker 139*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_flags; 140*f7c14bbaSAndroid Build Coastguard Worker __u32 token_fd; 141*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 142*f7c14bbaSAndroid Build Coastguard Worker }; 143*f7c14bbaSAndroid Build Coastguard Worker #define bpf_btf_load_opts__last_field token_fd 144*f7c14bbaSAndroid Build Coastguard Worker 145*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size, 146*f7c14bbaSAndroid Build Coastguard Worker struct bpf_btf_load_opts *opts); 147*f7c14bbaSAndroid Build Coastguard Worker 148*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value, 149*f7c14bbaSAndroid Build Coastguard Worker __u64 flags); 150*f7c14bbaSAndroid Build Coastguard Worker 151*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value); 152*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, 153*f7c14bbaSAndroid Build Coastguard Worker __u64 flags); 154*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key, 155*f7c14bbaSAndroid Build Coastguard Worker void *value); 156*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, 157*f7c14bbaSAndroid Build Coastguard Worker void *value, __u64 flags); 158*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_delete_elem(int fd, const void *key); 159*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags); 160*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key); 161*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_freeze(int fd); 162*f7c14bbaSAndroid Build Coastguard Worker 163*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_batch_opts { 164*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 165*f7c14bbaSAndroid Build Coastguard Worker __u64 elem_flags; 166*f7c14bbaSAndroid Build Coastguard Worker __u64 flags; 167*f7c14bbaSAndroid Build Coastguard Worker }; 168*f7c14bbaSAndroid Build Coastguard Worker #define bpf_map_batch_opts__last_field flags 169*f7c14bbaSAndroid Build Coastguard Worker 170*f7c14bbaSAndroid Build Coastguard Worker 171*f7c14bbaSAndroid Build Coastguard Worker /** 172*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple 173*f7c14bbaSAndroid Build Coastguard Worker * elements in a BPF map. 174*f7c14bbaSAndroid Build Coastguard Worker * 175*f7c14bbaSAndroid Build Coastguard Worker * @param fd BPF map file descriptor 176*f7c14bbaSAndroid Build Coastguard Worker * @param keys pointer to an array of *count* keys 177*f7c14bbaSAndroid Build Coastguard Worker * @param count input and output parameter; on input **count** represents the 178*f7c14bbaSAndroid Build Coastguard Worker * number of elements in the map to delete in batch; 179*f7c14bbaSAndroid Build Coastguard Worker * on output if a non-EFAULT error is returned, **count** represents the number of deleted 180*f7c14bbaSAndroid Build Coastguard Worker * elements if the output **count** value is not equal to the input **count** value 181*f7c14bbaSAndroid Build Coastguard Worker * If EFAULT is returned, **count** should not be trusted to be correct. 182*f7c14bbaSAndroid Build Coastguard Worker * @param opts options for configuring the way the batch deletion works 183*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 184*f7c14bbaSAndroid Build Coastguard Worker * the error code) 185*f7c14bbaSAndroid Build Coastguard Worker */ 186*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys, 187*f7c14bbaSAndroid Build Coastguard Worker __u32 *count, 188*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_map_batch_opts *opts); 189*f7c14bbaSAndroid Build Coastguard Worker 190*f7c14bbaSAndroid Build Coastguard Worker /** 191*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements. 192*f7c14bbaSAndroid Build Coastguard Worker * 193*f7c14bbaSAndroid Build Coastguard Worker * The parameter *in_batch* is the address of the first element in the batch to 194*f7c14bbaSAndroid Build Coastguard Worker * read. *out_batch* is an output parameter that should be passed as *in_batch* 195*f7c14bbaSAndroid Build Coastguard Worker * to subsequent calls to **bpf_map_lookup_batch()**. NULL can be passed for 196*f7c14bbaSAndroid Build Coastguard Worker * *in_batch* to indicate that the batched lookup starts from the beginning of 197*f7c14bbaSAndroid Build Coastguard Worker * the map. Both *in_batch* and *out_batch* must point to memory large enough to 198*f7c14bbaSAndroid Build Coastguard Worker * hold a single key, except for maps of type **BPF_MAP_TYPE_{HASH, PERCPU_HASH, 199*f7c14bbaSAndroid Build Coastguard Worker * LRU_HASH, LRU_PERCPU_HASH}**, for which the memory size must be at 200*f7c14bbaSAndroid Build Coastguard Worker * least 4 bytes wide regardless of key size. 201*f7c14bbaSAndroid Build Coastguard Worker * 202*f7c14bbaSAndroid Build Coastguard Worker * The *keys* and *values* are output parameters which must point to memory large enough to 203*f7c14bbaSAndroid Build Coastguard Worker * hold *count* items based on the key and value size of the map *map_fd*. The *keys* 204*f7c14bbaSAndroid Build Coastguard Worker * buffer must be of *key_size* * *count*. The *values* buffer must be of 205*f7c14bbaSAndroid Build Coastguard Worker * *value_size* * *count*. 206*f7c14bbaSAndroid Build Coastguard Worker * 207*f7c14bbaSAndroid Build Coastguard Worker * @param fd BPF map file descriptor 208*f7c14bbaSAndroid Build Coastguard Worker * @param in_batch address of the first element in batch to read, can pass NULL to 209*f7c14bbaSAndroid Build Coastguard Worker * indicate that the batched lookup starts from the beginning of the map. 210*f7c14bbaSAndroid Build Coastguard Worker * @param out_batch output parameter that should be passed to next call as *in_batch* 211*f7c14bbaSAndroid Build Coastguard Worker * @param keys pointer to an array large enough for *count* keys 212*f7c14bbaSAndroid Build Coastguard Worker * @param values pointer to an array large enough for *count* values 213*f7c14bbaSAndroid Build Coastguard Worker * @param count input and output parameter; on input it's the number of elements 214*f7c14bbaSAndroid Build Coastguard Worker * in the map to read in batch; on output it's the number of elements that were 215*f7c14bbaSAndroid Build Coastguard Worker * successfully read. 216*f7c14bbaSAndroid Build Coastguard Worker * If a non-EFAULT error is returned, count will be set as the number of elements 217*f7c14bbaSAndroid Build Coastguard Worker * that were read before the error occurred. 218*f7c14bbaSAndroid Build Coastguard Worker * If EFAULT is returned, **count** should not be trusted to be correct. 219*f7c14bbaSAndroid Build Coastguard Worker * @param opts options for configuring the way the batch lookup works 220*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 221*f7c14bbaSAndroid Build Coastguard Worker * the error code) 222*f7c14bbaSAndroid Build Coastguard Worker */ 223*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, 224*f7c14bbaSAndroid Build Coastguard Worker void *keys, void *values, __u32 *count, 225*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_map_batch_opts *opts); 226*f7c14bbaSAndroid Build Coastguard Worker 227*f7c14bbaSAndroid Build Coastguard Worker /** 228*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion 229*f7c14bbaSAndroid Build Coastguard Worker * of BPF map elements where each element is deleted after being retrieved. 230*f7c14bbaSAndroid Build Coastguard Worker * 231*f7c14bbaSAndroid Build Coastguard Worker * @param fd BPF map file descriptor 232*f7c14bbaSAndroid Build Coastguard Worker * @param in_batch address of the first element in batch to read, can pass NULL to 233*f7c14bbaSAndroid Build Coastguard Worker * get address of the first element in *out_batch*. If not NULL, must be large 234*f7c14bbaSAndroid Build Coastguard Worker * enough to hold a key. For **BPF_MAP_TYPE_{HASH, PERCPU_HASH, LRU_HASH, 235*f7c14bbaSAndroid Build Coastguard Worker * LRU_PERCPU_HASH}**, the memory size must be at least 4 bytes wide regardless 236*f7c14bbaSAndroid Build Coastguard Worker * of key size. 237*f7c14bbaSAndroid Build Coastguard Worker * @param out_batch output parameter that should be passed to next call as *in_batch* 238*f7c14bbaSAndroid Build Coastguard Worker * @param keys pointer to an array of *count* keys 239*f7c14bbaSAndroid Build Coastguard Worker * @param values pointer to an array large enough for *count* values 240*f7c14bbaSAndroid Build Coastguard Worker * @param count input and output parameter; on input it's the number of elements 241*f7c14bbaSAndroid Build Coastguard Worker * in the map to read and delete in batch; on output it represents the number of 242*f7c14bbaSAndroid Build Coastguard Worker * elements that were successfully read and deleted 243*f7c14bbaSAndroid Build Coastguard Worker * If a non-**EFAULT** error code is returned and if the output **count** value 244*f7c14bbaSAndroid Build Coastguard Worker * is not equal to the input **count** value, up to **count** elements may 245*f7c14bbaSAndroid Build Coastguard Worker * have been deleted. 246*f7c14bbaSAndroid Build Coastguard Worker * if **EFAULT** is returned up to *count* elements may have been deleted without 247*f7c14bbaSAndroid Build Coastguard Worker * being returned via the *keys* and *values* output parameters. 248*f7c14bbaSAndroid Build Coastguard Worker * @param opts options for configuring the way the batch lookup and delete works 249*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 250*f7c14bbaSAndroid Build Coastguard Worker * the error code) 251*f7c14bbaSAndroid Build Coastguard Worker */ 252*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, 253*f7c14bbaSAndroid Build Coastguard Worker void *out_batch, void *keys, 254*f7c14bbaSAndroid Build Coastguard Worker void *values, __u32 *count, 255*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_map_batch_opts *opts); 256*f7c14bbaSAndroid Build Coastguard Worker 257*f7c14bbaSAndroid Build Coastguard Worker /** 258*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map_update_batch()** updates multiple elements in a map 259*f7c14bbaSAndroid Build Coastguard Worker * by specifying keys and their corresponding values. 260*f7c14bbaSAndroid Build Coastguard Worker * 261*f7c14bbaSAndroid Build Coastguard Worker * The *keys* and *values* parameters must point to memory large enough 262*f7c14bbaSAndroid Build Coastguard Worker * to hold *count* items based on the key and value size of the map. 263*f7c14bbaSAndroid Build Coastguard Worker * 264*f7c14bbaSAndroid Build Coastguard Worker * The *opts* parameter can be used to control how *bpf_map_update_batch()* 265*f7c14bbaSAndroid Build Coastguard Worker * should handle keys that either do or do not already exist in the map. 266*f7c14bbaSAndroid Build Coastguard Worker * In particular the *flags* parameter of *bpf_map_batch_opts* can be 267*f7c14bbaSAndroid Build Coastguard Worker * one of the following: 268*f7c14bbaSAndroid Build Coastguard Worker * 269*f7c14bbaSAndroid Build Coastguard Worker * Note that *count* is an input and output parameter, where on output it 270*f7c14bbaSAndroid Build Coastguard Worker * represents how many elements were successfully updated. Also note that if 271*f7c14bbaSAndroid Build Coastguard Worker * **EFAULT** then *count* should not be trusted to be correct. 272*f7c14bbaSAndroid Build Coastguard Worker * 273*f7c14bbaSAndroid Build Coastguard Worker * **BPF_ANY** 274*f7c14bbaSAndroid Build Coastguard Worker * Create new elements or update existing. 275*f7c14bbaSAndroid Build Coastguard Worker * 276*f7c14bbaSAndroid Build Coastguard Worker * **BPF_NOEXIST** 277*f7c14bbaSAndroid Build Coastguard Worker * Create new elements only if they do not exist. 278*f7c14bbaSAndroid Build Coastguard Worker * 279*f7c14bbaSAndroid Build Coastguard Worker * **BPF_EXIST** 280*f7c14bbaSAndroid Build Coastguard Worker * Update existing elements. 281*f7c14bbaSAndroid Build Coastguard Worker * 282*f7c14bbaSAndroid Build Coastguard Worker * **BPF_F_LOCK** 283*f7c14bbaSAndroid Build Coastguard Worker * Update spin_lock-ed map elements. This must be 284*f7c14bbaSAndroid Build Coastguard Worker * specified if the map value contains a spinlock. 285*f7c14bbaSAndroid Build Coastguard Worker * 286*f7c14bbaSAndroid Build Coastguard Worker * @param fd BPF map file descriptor 287*f7c14bbaSAndroid Build Coastguard Worker * @param keys pointer to an array of *count* keys 288*f7c14bbaSAndroid Build Coastguard Worker * @param values pointer to an array of *count* values 289*f7c14bbaSAndroid Build Coastguard Worker * @param count input and output parameter; on input it's the number of elements 290*f7c14bbaSAndroid Build Coastguard Worker * in the map to update in batch; on output if a non-EFAULT error is returned, 291*f7c14bbaSAndroid Build Coastguard Worker * **count** represents the number of updated elements if the output **count** 292*f7c14bbaSAndroid Build Coastguard Worker * value is not equal to the input **count** value. 293*f7c14bbaSAndroid Build Coastguard Worker * If EFAULT is returned, **count** should not be trusted to be correct. 294*f7c14bbaSAndroid Build Coastguard Worker * @param opts options for configuring the way the batch update works 295*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 296*f7c14bbaSAndroid Build Coastguard Worker * the error code) 297*f7c14bbaSAndroid Build Coastguard Worker */ 298*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values, 299*f7c14bbaSAndroid Build Coastguard Worker __u32 *count, 300*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_map_batch_opts *opts); 301*f7c14bbaSAndroid Build Coastguard Worker 302*f7c14bbaSAndroid Build Coastguard Worker struct bpf_obj_pin_opts { 303*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 304*f7c14bbaSAndroid Build Coastguard Worker 305*f7c14bbaSAndroid Build Coastguard Worker __u32 file_flags; 306*f7c14bbaSAndroid Build Coastguard Worker int path_fd; 307*f7c14bbaSAndroid Build Coastguard Worker 308*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 309*f7c14bbaSAndroid Build Coastguard Worker }; 310*f7c14bbaSAndroid Build Coastguard Worker #define bpf_obj_pin_opts__last_field path_fd 311*f7c14bbaSAndroid Build Coastguard Worker 312*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_obj_pin(int fd, const char *pathname); 313*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_obj_pin_opts(int fd, const char *pathname, 314*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_obj_pin_opts *opts); 315*f7c14bbaSAndroid Build Coastguard Worker 316*f7c14bbaSAndroid Build Coastguard Worker struct bpf_obj_get_opts { 317*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 318*f7c14bbaSAndroid Build Coastguard Worker 319*f7c14bbaSAndroid Build Coastguard Worker __u32 file_flags; 320*f7c14bbaSAndroid Build Coastguard Worker int path_fd; 321*f7c14bbaSAndroid Build Coastguard Worker 322*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 323*f7c14bbaSAndroid Build Coastguard Worker }; 324*f7c14bbaSAndroid Build Coastguard Worker #define bpf_obj_get_opts__last_field path_fd 325*f7c14bbaSAndroid Build Coastguard Worker 326*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_obj_get(const char *pathname); 327*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_obj_get_opts(const char *pathname, 328*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_obj_get_opts *opts); 329*f7c14bbaSAndroid Build Coastguard Worker 330*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd, 331*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type type, unsigned int flags); 332*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); 333*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd, 334*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type type); 335*f7c14bbaSAndroid Build Coastguard Worker 336*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_attach_opts { 337*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 338*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 339*f7c14bbaSAndroid Build Coastguard Worker union { 340*f7c14bbaSAndroid Build Coastguard Worker int replace_prog_fd; 341*f7c14bbaSAndroid Build Coastguard Worker int replace_fd; 342*f7c14bbaSAndroid Build Coastguard Worker }; 343*f7c14bbaSAndroid Build Coastguard Worker int relative_fd; 344*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_id; 345*f7c14bbaSAndroid Build Coastguard Worker __u64 expected_revision; 346*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 347*f7c14bbaSAndroid Build Coastguard Worker }; 348*f7c14bbaSAndroid Build Coastguard Worker #define bpf_prog_attach_opts__last_field expected_revision 349*f7c14bbaSAndroid Build Coastguard Worker 350*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_detach_opts { 351*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 352*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 353*f7c14bbaSAndroid Build Coastguard Worker int relative_fd; 354*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_id; 355*f7c14bbaSAndroid Build Coastguard Worker __u64 expected_revision; 356*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 357*f7c14bbaSAndroid Build Coastguard Worker }; 358*f7c14bbaSAndroid Build Coastguard Worker #define bpf_prog_detach_opts__last_field expected_revision 359*f7c14bbaSAndroid Build Coastguard Worker 360*f7c14bbaSAndroid Build Coastguard Worker /** 361*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_prog_attach_opts()** attaches the BPF program corresponding to 362*f7c14bbaSAndroid Build Coastguard Worker * *prog_fd* to a *target* which can represent a file descriptor or netdevice 363*f7c14bbaSAndroid Build Coastguard Worker * ifindex. 364*f7c14bbaSAndroid Build Coastguard Worker * 365*f7c14bbaSAndroid Build Coastguard Worker * @param prog_fd BPF program file descriptor 366*f7c14bbaSAndroid Build Coastguard Worker * @param target attach location file descriptor or ifindex 367*f7c14bbaSAndroid Build Coastguard Worker * @param type attach type for the BPF program 368*f7c14bbaSAndroid Build Coastguard Worker * @param opts options for configuring the attachment 369*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 370*f7c14bbaSAndroid Build Coastguard Worker * the error code) 371*f7c14bbaSAndroid Build Coastguard Worker */ 372*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int target, 373*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type type, 374*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_prog_attach_opts *opts); 375*f7c14bbaSAndroid Build Coastguard Worker 376*f7c14bbaSAndroid Build Coastguard Worker /** 377*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_prog_detach_opts()** detaches the BPF program corresponding to 378*f7c14bbaSAndroid Build Coastguard Worker * *prog_fd* from a *target* which can represent a file descriptor or netdevice 379*f7c14bbaSAndroid Build Coastguard Worker * ifindex. 380*f7c14bbaSAndroid Build Coastguard Worker * 381*f7c14bbaSAndroid Build Coastguard Worker * @param prog_fd BPF program file descriptor 382*f7c14bbaSAndroid Build Coastguard Worker * @param target detach location file descriptor or ifindex 383*f7c14bbaSAndroid Build Coastguard Worker * @param type detach type for the BPF program 384*f7c14bbaSAndroid Build Coastguard Worker * @param opts options for configuring the detachment 385*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 386*f7c14bbaSAndroid Build Coastguard Worker * the error code) 387*f7c14bbaSAndroid Build Coastguard Worker */ 388*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_detach_opts(int prog_fd, int target, 389*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type type, 390*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_prog_detach_opts *opts); 391*f7c14bbaSAndroid Build Coastguard Worker 392*f7c14bbaSAndroid Build Coastguard Worker union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */ 393*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_create_opts { 394*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 395*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 396*f7c14bbaSAndroid Build Coastguard Worker union bpf_iter_link_info *iter_info; 397*f7c14bbaSAndroid Build Coastguard Worker __u32 iter_info_len; 398*f7c14bbaSAndroid Build Coastguard Worker __u32 target_btf_id; 399*f7c14bbaSAndroid Build Coastguard Worker union { 400*f7c14bbaSAndroid Build Coastguard Worker struct { 401*f7c14bbaSAndroid Build Coastguard Worker __u64 bpf_cookie; 402*f7c14bbaSAndroid Build Coastguard Worker } perf_event; 403*f7c14bbaSAndroid Build Coastguard Worker struct { 404*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 405*f7c14bbaSAndroid Build Coastguard Worker __u32 cnt; 406*f7c14bbaSAndroid Build Coastguard Worker const char **syms; 407*f7c14bbaSAndroid Build Coastguard Worker const unsigned long *addrs; 408*f7c14bbaSAndroid Build Coastguard Worker const __u64 *cookies; 409*f7c14bbaSAndroid Build Coastguard Worker } kprobe_multi; 410*f7c14bbaSAndroid Build Coastguard Worker struct { 411*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 412*f7c14bbaSAndroid Build Coastguard Worker __u32 cnt; 413*f7c14bbaSAndroid Build Coastguard Worker const char *path; 414*f7c14bbaSAndroid Build Coastguard Worker const unsigned long *offsets; 415*f7c14bbaSAndroid Build Coastguard Worker const unsigned long *ref_ctr_offsets; 416*f7c14bbaSAndroid Build Coastguard Worker const __u64 *cookies; 417*f7c14bbaSAndroid Build Coastguard Worker __u32 pid; 418*f7c14bbaSAndroid Build Coastguard Worker } uprobe_multi; 419*f7c14bbaSAndroid Build Coastguard Worker struct { 420*f7c14bbaSAndroid Build Coastguard Worker __u64 cookie; 421*f7c14bbaSAndroid Build Coastguard Worker } tracing; 422*f7c14bbaSAndroid Build Coastguard Worker struct { 423*f7c14bbaSAndroid Build Coastguard Worker __u32 pf; 424*f7c14bbaSAndroid Build Coastguard Worker __u32 hooknum; 425*f7c14bbaSAndroid Build Coastguard Worker __s32 priority; 426*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 427*f7c14bbaSAndroid Build Coastguard Worker } netfilter; 428*f7c14bbaSAndroid Build Coastguard Worker struct { 429*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_fd; 430*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_id; 431*f7c14bbaSAndroid Build Coastguard Worker __u64 expected_revision; 432*f7c14bbaSAndroid Build Coastguard Worker } tcx; 433*f7c14bbaSAndroid Build Coastguard Worker struct { 434*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_fd; 435*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_id; 436*f7c14bbaSAndroid Build Coastguard Worker __u64 expected_revision; 437*f7c14bbaSAndroid Build Coastguard Worker } netkit; 438*f7c14bbaSAndroid Build Coastguard Worker }; 439*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 440*f7c14bbaSAndroid Build Coastguard Worker }; 441*f7c14bbaSAndroid Build Coastguard Worker #define bpf_link_create_opts__last_field uprobe_multi.pid 442*f7c14bbaSAndroid Build Coastguard Worker 443*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link_create(int prog_fd, int target_fd, 444*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type attach_type, 445*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_link_create_opts *opts); 446*f7c14bbaSAndroid Build Coastguard Worker 447*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link_detach(int link_fd); 448*f7c14bbaSAndroid Build Coastguard Worker 449*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_update_opts { 450*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 451*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; /* extra flags */ 452*f7c14bbaSAndroid Build Coastguard Worker __u32 old_prog_fd; /* expected old program FD */ 453*f7c14bbaSAndroid Build Coastguard Worker __u32 old_map_fd; /* expected old map FD */ 454*f7c14bbaSAndroid Build Coastguard Worker }; 455*f7c14bbaSAndroid Build Coastguard Worker #define bpf_link_update_opts__last_field old_map_fd 456*f7c14bbaSAndroid Build Coastguard Worker 457*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd, 458*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_link_update_opts *opts); 459*f7c14bbaSAndroid Build Coastguard Worker 460*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_iter_create(int link_fd); 461*f7c14bbaSAndroid Build Coastguard Worker 462*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_test_run_attr { 463*f7c14bbaSAndroid Build Coastguard Worker int prog_fd; 464*f7c14bbaSAndroid Build Coastguard Worker int repeat; 465*f7c14bbaSAndroid Build Coastguard Worker const void *data_in; 466*f7c14bbaSAndroid Build Coastguard Worker __u32 data_size_in; 467*f7c14bbaSAndroid Build Coastguard Worker void *data_out; /* optional */ 468*f7c14bbaSAndroid Build Coastguard Worker __u32 data_size_out; /* in: max length of data_out 469*f7c14bbaSAndroid Build Coastguard Worker * out: length of data_out */ 470*f7c14bbaSAndroid Build Coastguard Worker __u32 retval; /* out: return code of the BPF program */ 471*f7c14bbaSAndroid Build Coastguard Worker __u32 duration; /* out: average per repetition in ns */ 472*f7c14bbaSAndroid Build Coastguard Worker const void *ctx_in; /* optional */ 473*f7c14bbaSAndroid Build Coastguard Worker __u32 ctx_size_in; 474*f7c14bbaSAndroid Build Coastguard Worker void *ctx_out; /* optional */ 475*f7c14bbaSAndroid Build Coastguard Worker __u32 ctx_size_out; /* in: max length of ctx_out 476*f7c14bbaSAndroid Build Coastguard Worker * out: length of cxt_out */ 477*f7c14bbaSAndroid Build Coastguard Worker }; 478*f7c14bbaSAndroid Build Coastguard Worker 479*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id); 480*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); 481*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id); 482*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id); 483*f7c14bbaSAndroid Build Coastguard Worker 484*f7c14bbaSAndroid Build Coastguard Worker struct bpf_get_fd_by_id_opts { 485*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 486*f7c14bbaSAndroid Build Coastguard Worker __u32 open_flags; /* permissions requested for the operation on fd */ 487*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 488*f7c14bbaSAndroid Build Coastguard Worker }; 489*f7c14bbaSAndroid Build Coastguard Worker #define bpf_get_fd_by_id_opts__last_field open_flags 490*f7c14bbaSAndroid Build Coastguard Worker 491*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id); 492*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id, 493*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_get_fd_by_id_opts *opts); 494*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_get_fd_by_id(__u32 id); 495*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id, 496*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_get_fd_by_id_opts *opts); 497*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id); 498*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_btf_get_fd_by_id_opts(__u32 id, 499*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_get_fd_by_id_opts *opts); 500*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link_get_fd_by_id(__u32 id); 501*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link_get_fd_by_id_opts(__u32 id, 502*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_get_fd_by_id_opts *opts); 503*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len); 504*f7c14bbaSAndroid Build Coastguard Worker 505*f7c14bbaSAndroid Build Coastguard Worker /** 506*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_prog_get_info_by_fd()** obtains information about the BPF 507*f7c14bbaSAndroid Build Coastguard Worker * program corresponding to *prog_fd*. 508*f7c14bbaSAndroid Build Coastguard Worker * 509*f7c14bbaSAndroid Build Coastguard Worker * Populates up to *info_len* bytes of *info* and updates *info_len* with the 510*f7c14bbaSAndroid Build Coastguard Worker * actual number of bytes written to *info*. Note that *info* should be 511*f7c14bbaSAndroid Build Coastguard Worker * zero-initialized or initialized as expected by the requested *info* 512*f7c14bbaSAndroid Build Coastguard Worker * type. Failing to (zero-)initialize *info* under certain circumstances can 513*f7c14bbaSAndroid Build Coastguard Worker * result in this helper returning an error. 514*f7c14bbaSAndroid Build Coastguard Worker * 515*f7c14bbaSAndroid Build Coastguard Worker * @param prog_fd BPF program file descriptor 516*f7c14bbaSAndroid Build Coastguard Worker * @param info pointer to **struct bpf_prog_info** that will be populated with 517*f7c14bbaSAndroid Build Coastguard Worker * BPF program information 518*f7c14bbaSAndroid Build Coastguard Worker * @param info_len pointer to the size of *info*; on success updated with the 519*f7c14bbaSAndroid Build Coastguard Worker * number of bytes written to *info* 520*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 521*f7c14bbaSAndroid Build Coastguard Worker * the error code) 522*f7c14bbaSAndroid Build Coastguard Worker */ 523*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len); 524*f7c14bbaSAndroid Build Coastguard Worker 525*f7c14bbaSAndroid Build Coastguard Worker /** 526*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map_get_info_by_fd()** obtains information about the BPF 527*f7c14bbaSAndroid Build Coastguard Worker * map corresponding to *map_fd*. 528*f7c14bbaSAndroid Build Coastguard Worker * 529*f7c14bbaSAndroid Build Coastguard Worker * Populates up to *info_len* bytes of *info* and updates *info_len* with the 530*f7c14bbaSAndroid Build Coastguard Worker * actual number of bytes written to *info*. Note that *info* should be 531*f7c14bbaSAndroid Build Coastguard Worker * zero-initialized or initialized as expected by the requested *info* 532*f7c14bbaSAndroid Build Coastguard Worker * type. Failing to (zero-)initialize *info* under certain circumstances can 533*f7c14bbaSAndroid Build Coastguard Worker * result in this helper returning an error. 534*f7c14bbaSAndroid Build Coastguard Worker * 535*f7c14bbaSAndroid Build Coastguard Worker * @param map_fd BPF map file descriptor 536*f7c14bbaSAndroid Build Coastguard Worker * @param info pointer to **struct bpf_map_info** that will be populated with 537*f7c14bbaSAndroid Build Coastguard Worker * BPF map information 538*f7c14bbaSAndroid Build Coastguard Worker * @param info_len pointer to the size of *info*; on success updated with the 539*f7c14bbaSAndroid Build Coastguard Worker * number of bytes written to *info* 540*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 541*f7c14bbaSAndroid Build Coastguard Worker * the error code) 542*f7c14bbaSAndroid Build Coastguard Worker */ 543*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len); 544*f7c14bbaSAndroid Build Coastguard Worker 545*f7c14bbaSAndroid Build Coastguard Worker /** 546*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_btf_get_info_by_fd()** obtains information about the 547*f7c14bbaSAndroid Build Coastguard Worker * BTF object corresponding to *btf_fd*. 548*f7c14bbaSAndroid Build Coastguard Worker * 549*f7c14bbaSAndroid Build Coastguard Worker * Populates up to *info_len* bytes of *info* and updates *info_len* with the 550*f7c14bbaSAndroid Build Coastguard Worker * actual number of bytes written to *info*. Note that *info* should be 551*f7c14bbaSAndroid Build Coastguard Worker * zero-initialized or initialized as expected by the requested *info* 552*f7c14bbaSAndroid Build Coastguard Worker * type. Failing to (zero-)initialize *info* under certain circumstances can 553*f7c14bbaSAndroid Build Coastguard Worker * result in this helper returning an error. 554*f7c14bbaSAndroid Build Coastguard Worker * 555*f7c14bbaSAndroid Build Coastguard Worker * @param btf_fd BTF object file descriptor 556*f7c14bbaSAndroid Build Coastguard Worker * @param info pointer to **struct bpf_btf_info** that will be populated with 557*f7c14bbaSAndroid Build Coastguard Worker * BTF object information 558*f7c14bbaSAndroid Build Coastguard Worker * @param info_len pointer to the size of *info*; on success updated with the 559*f7c14bbaSAndroid Build Coastguard Worker * number of bytes written to *info* 560*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 561*f7c14bbaSAndroid Build Coastguard Worker * the error code) 562*f7c14bbaSAndroid Build Coastguard Worker */ 563*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len); 564*f7c14bbaSAndroid Build Coastguard Worker 565*f7c14bbaSAndroid Build Coastguard Worker /** 566*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_btf_get_info_by_fd()** obtains information about the BPF 567*f7c14bbaSAndroid Build Coastguard Worker * link corresponding to *link_fd*. 568*f7c14bbaSAndroid Build Coastguard Worker * 569*f7c14bbaSAndroid Build Coastguard Worker * Populates up to *info_len* bytes of *info* and updates *info_len* with the 570*f7c14bbaSAndroid Build Coastguard Worker * actual number of bytes written to *info*. Note that *info* should be 571*f7c14bbaSAndroid Build Coastguard Worker * zero-initialized or initialized as expected by the requested *info* 572*f7c14bbaSAndroid Build Coastguard Worker * type. Failing to (zero-)initialize *info* under certain circumstances can 573*f7c14bbaSAndroid Build Coastguard Worker * result in this helper returning an error. 574*f7c14bbaSAndroid Build Coastguard Worker * 575*f7c14bbaSAndroid Build Coastguard Worker * @param link_fd BPF link file descriptor 576*f7c14bbaSAndroid Build Coastguard Worker * @param info pointer to **struct bpf_link_info** that will be populated with 577*f7c14bbaSAndroid Build Coastguard Worker * BPF link information 578*f7c14bbaSAndroid Build Coastguard Worker * @param info_len pointer to the size of *info*; on success updated with the 579*f7c14bbaSAndroid Build Coastguard Worker * number of bytes written to *info* 580*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 581*f7c14bbaSAndroid Build Coastguard Worker * the error code) 582*f7c14bbaSAndroid Build Coastguard Worker */ 583*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len); 584*f7c14bbaSAndroid Build Coastguard Worker 585*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_query_opts { 586*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 587*f7c14bbaSAndroid Build Coastguard Worker __u32 query_flags; 588*f7c14bbaSAndroid Build Coastguard Worker __u32 attach_flags; /* output argument */ 589*f7c14bbaSAndroid Build Coastguard Worker __u32 *prog_ids; 590*f7c14bbaSAndroid Build Coastguard Worker union { 591*f7c14bbaSAndroid Build Coastguard Worker /* input+output argument */ 592*f7c14bbaSAndroid Build Coastguard Worker __u32 prog_cnt; 593*f7c14bbaSAndroid Build Coastguard Worker __u32 count; 594*f7c14bbaSAndroid Build Coastguard Worker }; 595*f7c14bbaSAndroid Build Coastguard Worker __u32 *prog_attach_flags; 596*f7c14bbaSAndroid Build Coastguard Worker __u32 *link_ids; 597*f7c14bbaSAndroid Build Coastguard Worker __u32 *link_attach_flags; 598*f7c14bbaSAndroid Build Coastguard Worker __u64 revision; 599*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 600*f7c14bbaSAndroid Build Coastguard Worker }; 601*f7c14bbaSAndroid Build Coastguard Worker #define bpf_prog_query_opts__last_field revision 602*f7c14bbaSAndroid Build Coastguard Worker 603*f7c14bbaSAndroid Build Coastguard Worker /** 604*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_prog_query_opts()** queries the BPF programs and BPF links 605*f7c14bbaSAndroid Build Coastguard Worker * which are attached to *target* which can represent a file descriptor or 606*f7c14bbaSAndroid Build Coastguard Worker * netdevice ifindex. 607*f7c14bbaSAndroid Build Coastguard Worker * 608*f7c14bbaSAndroid Build Coastguard Worker * @param target query location file descriptor or ifindex 609*f7c14bbaSAndroid Build Coastguard Worker * @param type attach type for the BPF program 610*f7c14bbaSAndroid Build Coastguard Worker * @param opts options for configuring the query 611*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise (errno is also set to 612*f7c14bbaSAndroid Build Coastguard Worker * the error code) 613*f7c14bbaSAndroid Build Coastguard Worker */ 614*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_query_opts(int target, enum bpf_attach_type type, 615*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_query_opts *opts); 616*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type, 617*f7c14bbaSAndroid Build Coastguard Worker __u32 query_flags, __u32 *attach_flags, 618*f7c14bbaSAndroid Build Coastguard Worker __u32 *prog_ids, __u32 *prog_cnt); 619*f7c14bbaSAndroid Build Coastguard Worker 620*f7c14bbaSAndroid Build Coastguard Worker struct bpf_raw_tp_opts { 621*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 622*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name; 623*f7c14bbaSAndroid Build Coastguard Worker __u64 cookie; 624*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 625*f7c14bbaSAndroid Build Coastguard Worker }; 626*f7c14bbaSAndroid Build Coastguard Worker #define bpf_raw_tp_opts__last_field cookie 627*f7c14bbaSAndroid Build Coastguard Worker 628*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts); 629*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); 630*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, 631*f7c14bbaSAndroid Build Coastguard Worker __u32 *buf_len, __u32 *prog_id, __u32 *fd_type, 632*f7c14bbaSAndroid Build Coastguard Worker __u64 *probe_offset, __u64 *probe_addr); 633*f7c14bbaSAndroid Build Coastguard Worker 634*f7c14bbaSAndroid Build Coastguard Worker #ifdef __cplusplus 635*f7c14bbaSAndroid Build Coastguard Worker /* forward-declaring enums in C++ isn't compatible with pure C enums, so 636*f7c14bbaSAndroid Build Coastguard Worker * instead define bpf_enable_stats() as accepting int as an input 637*f7c14bbaSAndroid Build Coastguard Worker */ 638*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_enable_stats(int type); 639*f7c14bbaSAndroid Build Coastguard Worker #else 640*f7c14bbaSAndroid Build Coastguard Worker enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */ 641*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type); 642*f7c14bbaSAndroid Build Coastguard Worker #endif 643*f7c14bbaSAndroid Build Coastguard Worker 644*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_bind_opts { 645*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 646*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 647*f7c14bbaSAndroid Build Coastguard Worker }; 648*f7c14bbaSAndroid Build Coastguard Worker #define bpf_prog_bind_opts__last_field flags 649*f7c14bbaSAndroid Build Coastguard Worker 650*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd, 651*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_prog_bind_opts *opts); 652*f7c14bbaSAndroid Build Coastguard Worker 653*f7c14bbaSAndroid Build Coastguard Worker struct bpf_test_run_opts { 654*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 655*f7c14bbaSAndroid Build Coastguard Worker const void *data_in; /* optional */ 656*f7c14bbaSAndroid Build Coastguard Worker void *data_out; /* optional */ 657*f7c14bbaSAndroid Build Coastguard Worker __u32 data_size_in; 658*f7c14bbaSAndroid Build Coastguard Worker __u32 data_size_out; /* in: max length of data_out 659*f7c14bbaSAndroid Build Coastguard Worker * out: length of data_out 660*f7c14bbaSAndroid Build Coastguard Worker */ 661*f7c14bbaSAndroid Build Coastguard Worker const void *ctx_in; /* optional */ 662*f7c14bbaSAndroid Build Coastguard Worker void *ctx_out; /* optional */ 663*f7c14bbaSAndroid Build Coastguard Worker __u32 ctx_size_in; 664*f7c14bbaSAndroid Build Coastguard Worker __u32 ctx_size_out; /* in: max length of ctx_out 665*f7c14bbaSAndroid Build Coastguard Worker * out: length of cxt_out 666*f7c14bbaSAndroid Build Coastguard Worker */ 667*f7c14bbaSAndroid Build Coastguard Worker __u32 retval; /* out: return code of the BPF program */ 668*f7c14bbaSAndroid Build Coastguard Worker int repeat; 669*f7c14bbaSAndroid Build Coastguard Worker __u32 duration; /* out: average per repetition in ns */ 670*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 671*f7c14bbaSAndroid Build Coastguard Worker __u32 cpu; 672*f7c14bbaSAndroid Build Coastguard Worker __u32 batch_size; 673*f7c14bbaSAndroid Build Coastguard Worker }; 674*f7c14bbaSAndroid Build Coastguard Worker #define bpf_test_run_opts__last_field batch_size 675*f7c14bbaSAndroid Build Coastguard Worker 676*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_test_run_opts(int prog_fd, 677*f7c14bbaSAndroid Build Coastguard Worker struct bpf_test_run_opts *opts); 678*f7c14bbaSAndroid Build Coastguard Worker 679*f7c14bbaSAndroid Build Coastguard Worker struct bpf_token_create_opts { 680*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 681*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 682*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 683*f7c14bbaSAndroid Build Coastguard Worker }; 684*f7c14bbaSAndroid Build Coastguard Worker #define bpf_token_create_opts__last_field flags 685*f7c14bbaSAndroid Build Coastguard Worker 686*f7c14bbaSAndroid Build Coastguard Worker /** 687*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_token_create()** creates a new instance of BPF token derived 688*f7c14bbaSAndroid Build Coastguard Worker * from specified BPF FS mount point. 689*f7c14bbaSAndroid Build Coastguard Worker * 690*f7c14bbaSAndroid Build Coastguard Worker * BPF token created with this API can be passed to bpf() syscall for 691*f7c14bbaSAndroid Build Coastguard Worker * commands like BPF_PROG_LOAD, BPF_MAP_CREATE, etc. 692*f7c14bbaSAndroid Build Coastguard Worker * 693*f7c14bbaSAndroid Build Coastguard Worker * @param bpffs_fd FD for BPF FS instance from which to derive a BPF token 694*f7c14bbaSAndroid Build Coastguard Worker * instance. 695*f7c14bbaSAndroid Build Coastguard Worker * @param opts optional BPF token creation options, can be NULL 696*f7c14bbaSAndroid Build Coastguard Worker * 697*f7c14bbaSAndroid Build Coastguard Worker * @return BPF token FD > 0, on success; negative error code, otherwise (errno 698*f7c14bbaSAndroid Build Coastguard Worker * is also set to the error code) 699*f7c14bbaSAndroid Build Coastguard Worker */ 700*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_token_create(int bpffs_fd, 701*f7c14bbaSAndroid Build Coastguard Worker struct bpf_token_create_opts *opts); 702*f7c14bbaSAndroid Build Coastguard Worker 703*f7c14bbaSAndroid Build Coastguard Worker #ifdef __cplusplus 704*f7c14bbaSAndroid Build Coastguard Worker } /* extern "C" */ 705*f7c14bbaSAndroid Build Coastguard Worker #endif 706*f7c14bbaSAndroid Build Coastguard Worker 707*f7c14bbaSAndroid Build Coastguard Worker #endif /* __LIBBPF_BPF_H */ 708