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 eBPF ELF object loading 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 #ifndef __LIBBPF_LIBBPF_H 11*f7c14bbaSAndroid Build Coastguard Worker #define __LIBBPF_LIBBPF_H 12*f7c14bbaSAndroid Build Coastguard Worker 13*f7c14bbaSAndroid Build Coastguard Worker #include <stdarg.h> 14*f7c14bbaSAndroid Build Coastguard Worker #include <stdio.h> 15*f7c14bbaSAndroid Build Coastguard Worker #include <stdint.h> 16*f7c14bbaSAndroid Build Coastguard Worker #include <stdbool.h> 17*f7c14bbaSAndroid Build Coastguard Worker #include <sys/types.h> // for size_t 18*f7c14bbaSAndroid Build Coastguard Worker #include <linux/bpf.h> 19*f7c14bbaSAndroid Build Coastguard Worker 20*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_common.h" 21*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_legacy.h" 22*f7c14bbaSAndroid Build Coastguard Worker 23*f7c14bbaSAndroid Build Coastguard Worker #ifdef __cplusplus 24*f7c14bbaSAndroid Build Coastguard Worker extern "C" { 25*f7c14bbaSAndroid Build Coastguard Worker #endif 26*f7c14bbaSAndroid Build Coastguard Worker 27*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 libbpf_major_version(void); 28*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 libbpf_minor_version(void); 29*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *libbpf_version_string(void); 30*f7c14bbaSAndroid Build Coastguard Worker 31*f7c14bbaSAndroid Build Coastguard Worker enum libbpf_errno { 32*f7c14bbaSAndroid Build Coastguard Worker __LIBBPF_ERRNO__START = 4000, 33*f7c14bbaSAndroid Build Coastguard Worker 34*f7c14bbaSAndroid Build Coastguard Worker /* Something wrong in libelf */ 35*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START, 36*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */ 37*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */ 38*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */ 39*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */ 40*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__RELOC, /* Relocation failed */ 41*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */ 42*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */ 43*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__PROG2BIG, /* Program too big */ 44*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__KVER, /* Incorrect kernel version */ 45*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */ 46*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */ 47*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */ 48*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */ 49*f7c14bbaSAndroid Build Coastguard Worker __LIBBPF_ERRNO__END, 50*f7c14bbaSAndroid Build Coastguard Worker }; 51*f7c14bbaSAndroid Build Coastguard Worker 52*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size); 53*f7c14bbaSAndroid Build Coastguard Worker 54*f7c14bbaSAndroid Build Coastguard Worker /** 55*f7c14bbaSAndroid Build Coastguard Worker * @brief **libbpf_bpf_attach_type_str()** converts the provided attach type 56*f7c14bbaSAndroid Build Coastguard Worker * value into a textual representation. 57*f7c14bbaSAndroid Build Coastguard Worker * @param t The attach type. 58*f7c14bbaSAndroid Build Coastguard Worker * @return Pointer to a static string identifying the attach type. NULL is 59*f7c14bbaSAndroid Build Coastguard Worker * returned for unknown **bpf_attach_type** values. 60*f7c14bbaSAndroid Build Coastguard Worker */ 61*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t); 62*f7c14bbaSAndroid Build Coastguard Worker 63*f7c14bbaSAndroid Build Coastguard Worker /** 64*f7c14bbaSAndroid Build Coastguard Worker * @brief **libbpf_bpf_link_type_str()** converts the provided link type value 65*f7c14bbaSAndroid Build Coastguard Worker * into a textual representation. 66*f7c14bbaSAndroid Build Coastguard Worker * @param t The link type. 67*f7c14bbaSAndroid Build Coastguard Worker * @return Pointer to a static string identifying the link type. NULL is 68*f7c14bbaSAndroid Build Coastguard Worker * returned for unknown **bpf_link_type** values. 69*f7c14bbaSAndroid Build Coastguard Worker */ 70*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t); 71*f7c14bbaSAndroid Build Coastguard Worker 72*f7c14bbaSAndroid Build Coastguard Worker /** 73*f7c14bbaSAndroid Build Coastguard Worker * @brief **libbpf_bpf_map_type_str()** converts the provided map type value 74*f7c14bbaSAndroid Build Coastguard Worker * into a textual representation. 75*f7c14bbaSAndroid Build Coastguard Worker * @param t The map type. 76*f7c14bbaSAndroid Build Coastguard Worker * @return Pointer to a static string identifying the map type. NULL is 77*f7c14bbaSAndroid Build Coastguard Worker * returned for unknown **bpf_map_type** values. 78*f7c14bbaSAndroid Build Coastguard Worker */ 79*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t); 80*f7c14bbaSAndroid Build Coastguard Worker 81*f7c14bbaSAndroid Build Coastguard Worker /** 82*f7c14bbaSAndroid Build Coastguard Worker * @brief **libbpf_bpf_prog_type_str()** converts the provided program type 83*f7c14bbaSAndroid Build Coastguard Worker * value into a textual representation. 84*f7c14bbaSAndroid Build Coastguard Worker * @param t The program type. 85*f7c14bbaSAndroid Build Coastguard Worker * @return Pointer to a static string identifying the program type. NULL is 86*f7c14bbaSAndroid Build Coastguard Worker * returned for unknown **bpf_prog_type** values. 87*f7c14bbaSAndroid Build Coastguard Worker */ 88*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t); 89*f7c14bbaSAndroid Build Coastguard Worker 90*f7c14bbaSAndroid Build Coastguard Worker enum libbpf_print_level { 91*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_WARN, 92*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_INFO, 93*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_DEBUG, 94*f7c14bbaSAndroid Build Coastguard Worker }; 95*f7c14bbaSAndroid Build Coastguard Worker 96*f7c14bbaSAndroid Build Coastguard Worker typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level, 97*f7c14bbaSAndroid Build Coastguard Worker const char *, va_list ap); 98*f7c14bbaSAndroid Build Coastguard Worker 99*f7c14bbaSAndroid Build Coastguard Worker /** 100*f7c14bbaSAndroid Build Coastguard Worker * @brief **libbpf_set_print()** sets user-provided log callback function to 101*f7c14bbaSAndroid Build Coastguard Worker * be used for libbpf warnings and informational messages. 102*f7c14bbaSAndroid Build Coastguard Worker * @param fn The log print function. If NULL, libbpf won't print anything. 103*f7c14bbaSAndroid Build Coastguard Worker * @return Pointer to old print function. 104*f7c14bbaSAndroid Build Coastguard Worker * 105*f7c14bbaSAndroid Build Coastguard Worker * This function is thread-safe. 106*f7c14bbaSAndroid Build Coastguard Worker */ 107*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn); 108*f7c14bbaSAndroid Build Coastguard Worker 109*f7c14bbaSAndroid Build Coastguard Worker /* Hide internal to user */ 110*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object; 111*f7c14bbaSAndroid Build Coastguard Worker 112*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object_open_opts { 113*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 114*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 115*f7c14bbaSAndroid Build Coastguard Worker /* object name override, if provided: 116*f7c14bbaSAndroid Build Coastguard Worker * - for object open from file, this will override setting object 117*f7c14bbaSAndroid Build Coastguard Worker * name from file path's base name; 118*f7c14bbaSAndroid Build Coastguard Worker * - for object open from memory buffer, this will specify an object 119*f7c14bbaSAndroid Build Coastguard Worker * name and will override default "<addr>-<buf-size>" name; 120*f7c14bbaSAndroid Build Coastguard Worker */ 121*f7c14bbaSAndroid Build Coastguard Worker const char *object_name; 122*f7c14bbaSAndroid Build Coastguard Worker /* parse map definitions non-strictly, allowing extra attributes/data */ 123*f7c14bbaSAndroid Build Coastguard Worker bool relaxed_maps; 124*f7c14bbaSAndroid Build Coastguard Worker /* maps that set the 'pinning' attribute in their definition will have 125*f7c14bbaSAndroid Build Coastguard Worker * their pin_path attribute set to a file in this directory, and be 126*f7c14bbaSAndroid Build Coastguard Worker * auto-pinned to that path on load; defaults to "/sys/fs/bpf". 127*f7c14bbaSAndroid Build Coastguard Worker */ 128*f7c14bbaSAndroid Build Coastguard Worker const char *pin_root_path; 129*f7c14bbaSAndroid Build Coastguard Worker 130*f7c14bbaSAndroid Build Coastguard Worker __u32 :32; /* stub out now removed attach_prog_fd */ 131*f7c14bbaSAndroid Build Coastguard Worker 132*f7c14bbaSAndroid Build Coastguard Worker /* Additional kernel config content that augments and overrides 133*f7c14bbaSAndroid Build Coastguard Worker * system Kconfig for CONFIG_xxx externs. 134*f7c14bbaSAndroid Build Coastguard Worker */ 135*f7c14bbaSAndroid Build Coastguard Worker const char *kconfig; 136*f7c14bbaSAndroid Build Coastguard Worker /* Path to the custom BTF to be used for BPF CO-RE relocations. 137*f7c14bbaSAndroid Build Coastguard Worker * This custom BTF completely replaces the use of vmlinux BTF 138*f7c14bbaSAndroid Build Coastguard Worker * for the purpose of CO-RE relocations. 139*f7c14bbaSAndroid Build Coastguard Worker * NOTE: any other BPF feature (e.g., fentry/fexit programs, 140*f7c14bbaSAndroid Build Coastguard Worker * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux. 141*f7c14bbaSAndroid Build Coastguard Worker */ 142*f7c14bbaSAndroid Build Coastguard Worker const char *btf_custom_path; 143*f7c14bbaSAndroid Build Coastguard Worker /* Pointer to a buffer for storing kernel logs for applicable BPF 144*f7c14bbaSAndroid Build Coastguard Worker * commands. Valid kernel_log_size has to be specified as well and are 145*f7c14bbaSAndroid Build Coastguard Worker * passed-through to bpf() syscall. Keep in mind that kernel might 146*f7c14bbaSAndroid Build Coastguard Worker * fail operation with -ENOSPC error if provided buffer is too small 147*f7c14bbaSAndroid Build Coastguard Worker * to contain entire log output. 148*f7c14bbaSAndroid Build Coastguard Worker * See the comment below for kernel_log_level for interaction between 149*f7c14bbaSAndroid Build Coastguard Worker * log_buf and log_level settings. 150*f7c14bbaSAndroid Build Coastguard Worker * 151*f7c14bbaSAndroid Build Coastguard Worker * If specified, this log buffer will be passed for: 152*f7c14bbaSAndroid Build Coastguard Worker * - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden 153*f7c14bbaSAndroid Build Coastguard Worker * with bpf_program__set_log() on per-program level, to get 154*f7c14bbaSAndroid Build Coastguard Worker * BPF verifier log output. 155*f7c14bbaSAndroid Build Coastguard Worker * - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get 156*f7c14bbaSAndroid Build Coastguard Worker * BTF sanity checking log. 157*f7c14bbaSAndroid Build Coastguard Worker * 158*f7c14bbaSAndroid Build Coastguard Worker * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite 159*f7c14bbaSAndroid Build Coastguard Worker * previous contents, so if you need more fine-grained control, set 160*f7c14bbaSAndroid Build Coastguard Worker * per-program buffer with bpf_program__set_log_buf() to preserve each 161*f7c14bbaSAndroid Build Coastguard Worker * individual program's verification log. Keep using kernel_log_buf 162*f7c14bbaSAndroid Build Coastguard Worker * for BTF verification log, if necessary. 163*f7c14bbaSAndroid Build Coastguard Worker */ 164*f7c14bbaSAndroid Build Coastguard Worker char *kernel_log_buf; 165*f7c14bbaSAndroid Build Coastguard Worker size_t kernel_log_size; 166*f7c14bbaSAndroid Build Coastguard Worker /* 167*f7c14bbaSAndroid Build Coastguard Worker * Log level can be set independently from log buffer. Log_level=0 168*f7c14bbaSAndroid Build Coastguard Worker * means that libbpf will attempt loading BTF or program without any 169*f7c14bbaSAndroid Build Coastguard Worker * logging requested, but will retry with either its own or custom log 170*f7c14bbaSAndroid Build Coastguard Worker * buffer, if provided, and log_level=1 on any error. 171*f7c14bbaSAndroid Build Coastguard Worker * And vice versa, setting log_level>0 will request BTF or prog 172*f7c14bbaSAndroid Build Coastguard Worker * loading with verbose log from the first attempt (and as such also 173*f7c14bbaSAndroid Build Coastguard Worker * for successfully loaded BTF or program), and the actual log buffer 174*f7c14bbaSAndroid Build Coastguard Worker * could be either libbpf's own auto-allocated log buffer, if 175*f7c14bbaSAndroid Build Coastguard Worker * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf. 176*f7c14bbaSAndroid Build Coastguard Worker * If user didn't provide custom log buffer, libbpf will emit captured 177*f7c14bbaSAndroid Build Coastguard Worker * logs through its print callback. 178*f7c14bbaSAndroid Build Coastguard Worker */ 179*f7c14bbaSAndroid Build Coastguard Worker __u32 kernel_log_level; 180*f7c14bbaSAndroid Build Coastguard Worker /* Path to BPF FS mount point to derive BPF token from. 181*f7c14bbaSAndroid Build Coastguard Worker * 182*f7c14bbaSAndroid Build Coastguard Worker * Created BPF token will be used for all bpf() syscall operations 183*f7c14bbaSAndroid Build Coastguard Worker * that accept BPF token (e.g., map creation, BTF and program loads, 184*f7c14bbaSAndroid Build Coastguard Worker * etc) automatically within instantiated BPF object. 185*f7c14bbaSAndroid Build Coastguard Worker * 186*f7c14bbaSAndroid Build Coastguard Worker * If bpf_token_path is not specified, libbpf will consult 187*f7c14bbaSAndroid Build Coastguard Worker * LIBBPF_BPF_TOKEN_PATH environment variable. If set, it will be 188*f7c14bbaSAndroid Build Coastguard Worker * taken as a value of bpf_token_path option and will force libbpf to 189*f7c14bbaSAndroid Build Coastguard Worker * either create BPF token from provided custom BPF FS path, or will 190*f7c14bbaSAndroid Build Coastguard Worker * disable implicit BPF token creation, if envvar value is an empty 191*f7c14bbaSAndroid Build Coastguard Worker * string. bpf_token_path overrides LIBBPF_BPF_TOKEN_PATH, if both are 192*f7c14bbaSAndroid Build Coastguard Worker * set at the same time. 193*f7c14bbaSAndroid Build Coastguard Worker * 194*f7c14bbaSAndroid Build Coastguard Worker * Setting bpf_token_path option to empty string disables libbpf's 195*f7c14bbaSAndroid Build Coastguard Worker * automatic attempt to create BPF token from default BPF FS mount 196*f7c14bbaSAndroid Build Coastguard Worker * point (/sys/fs/bpf), in case this default behavior is undesirable. 197*f7c14bbaSAndroid Build Coastguard Worker */ 198*f7c14bbaSAndroid Build Coastguard Worker const char *bpf_token_path; 199*f7c14bbaSAndroid Build Coastguard Worker 200*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 201*f7c14bbaSAndroid Build Coastguard Worker }; 202*f7c14bbaSAndroid Build Coastguard Worker #define bpf_object_open_opts__last_field bpf_token_path 203*f7c14bbaSAndroid Build Coastguard Worker 204*f7c14bbaSAndroid Build Coastguard Worker /** 205*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_object__open()** creates a bpf_object by opening 206*f7c14bbaSAndroid Build Coastguard Worker * the BPF ELF object file pointed to by the passed path and loading it 207*f7c14bbaSAndroid Build Coastguard Worker * into memory. 208*f7c14bbaSAndroid Build Coastguard Worker * @param path BPF object file path. 209*f7c14bbaSAndroid Build Coastguard Worker * @return pointer to the new bpf_object; or NULL is returned on error, 210*f7c14bbaSAndroid Build Coastguard Worker * error code is stored in errno 211*f7c14bbaSAndroid Build Coastguard Worker */ 212*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_object *bpf_object__open(const char *path); 213*f7c14bbaSAndroid Build Coastguard Worker 214*f7c14bbaSAndroid Build Coastguard Worker /** 215*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_object__open_file()** creates a bpf_object by opening 216*f7c14bbaSAndroid Build Coastguard Worker * the BPF ELF object file pointed to by the passed path and loading it 217*f7c14bbaSAndroid Build Coastguard Worker * into memory. 218*f7c14bbaSAndroid Build Coastguard Worker * @param path BPF object file path 219*f7c14bbaSAndroid Build Coastguard Worker * @param opts options for how to load the bpf object, this parameter is 220*f7c14bbaSAndroid Build Coastguard Worker * optional and can be set to NULL 221*f7c14bbaSAndroid Build Coastguard Worker * @return pointer to the new bpf_object; or NULL is returned on error, 222*f7c14bbaSAndroid Build Coastguard Worker * error code is stored in errno 223*f7c14bbaSAndroid Build Coastguard Worker */ 224*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_object * 225*f7c14bbaSAndroid Build Coastguard Worker bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts); 226*f7c14bbaSAndroid Build Coastguard Worker 227*f7c14bbaSAndroid Build Coastguard Worker /** 228*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_object__open_mem()** creates a bpf_object by reading 229*f7c14bbaSAndroid Build Coastguard Worker * the BPF objects raw bytes from a memory buffer containing a valid 230*f7c14bbaSAndroid Build Coastguard Worker * BPF ELF object file. 231*f7c14bbaSAndroid Build Coastguard Worker * @param obj_buf pointer to the buffer containing ELF file bytes 232*f7c14bbaSAndroid Build Coastguard Worker * @param obj_buf_sz number of bytes in the buffer 233*f7c14bbaSAndroid Build Coastguard Worker * @param opts options for how to load the bpf object 234*f7c14bbaSAndroid Build Coastguard Worker * @return pointer to the new bpf_object; or NULL is returned on error, 235*f7c14bbaSAndroid Build Coastguard Worker * error code is stored in errno 236*f7c14bbaSAndroid Build Coastguard Worker */ 237*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_object * 238*f7c14bbaSAndroid Build Coastguard Worker bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, 239*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_object_open_opts *opts); 240*f7c14bbaSAndroid Build Coastguard Worker 241*f7c14bbaSAndroid Build Coastguard Worker /** 242*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_object__load()** loads BPF object into kernel. 243*f7c14bbaSAndroid Build Coastguard Worker * @param obj Pointer to a valid BPF object instance returned by 244*f7c14bbaSAndroid Build Coastguard Worker * **bpf_object__open*()** APIs 245*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise, error code is 246*f7c14bbaSAndroid Build Coastguard Worker * stored in errno 247*f7c14bbaSAndroid Build Coastguard Worker */ 248*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__load(struct bpf_object *obj); 249*f7c14bbaSAndroid Build Coastguard Worker 250*f7c14bbaSAndroid Build Coastguard Worker /** 251*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_object__close()** closes a BPF object and releases all 252*f7c14bbaSAndroid Build Coastguard Worker * resources. 253*f7c14bbaSAndroid Build Coastguard Worker * @param obj Pointer to a valid BPF object 254*f7c14bbaSAndroid Build Coastguard Worker */ 255*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void bpf_object__close(struct bpf_object *obj); 256*f7c14bbaSAndroid Build Coastguard Worker 257*f7c14bbaSAndroid Build Coastguard Worker /** 258*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_object__pin_maps()** pins each map contained within 259*f7c14bbaSAndroid Build Coastguard Worker * the BPF object at the passed directory. 260*f7c14bbaSAndroid Build Coastguard Worker * @param obj Pointer to a valid BPF object 261*f7c14bbaSAndroid Build Coastguard Worker * @param path A directory where maps should be pinned. 262*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise 263*f7c14bbaSAndroid Build Coastguard Worker * 264*f7c14bbaSAndroid Build Coastguard Worker * If `path` is NULL `bpf_map__pin` (which is being used on each map) 265*f7c14bbaSAndroid Build Coastguard Worker * will use the pin_path attribute of each map. In this case, maps that 266*f7c14bbaSAndroid Build Coastguard Worker * don't have a pin_path set will be ignored. 267*f7c14bbaSAndroid Build Coastguard Worker */ 268*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path); 269*f7c14bbaSAndroid Build Coastguard Worker 270*f7c14bbaSAndroid Build Coastguard Worker /** 271*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_object__unpin_maps()** unpins each map contained within 272*f7c14bbaSAndroid Build Coastguard Worker * the BPF object found in the passed directory. 273*f7c14bbaSAndroid Build Coastguard Worker * @param obj Pointer to a valid BPF object 274*f7c14bbaSAndroid Build Coastguard Worker * @param path A directory where pinned maps should be searched for. 275*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise 276*f7c14bbaSAndroid Build Coastguard Worker * 277*f7c14bbaSAndroid Build Coastguard Worker * If `path` is NULL `bpf_map__unpin` (which is being used on each map) 278*f7c14bbaSAndroid Build Coastguard Worker * will use the pin_path attribute of each map. In this case, maps that 279*f7c14bbaSAndroid Build Coastguard Worker * don't have a pin_path set will be ignored. 280*f7c14bbaSAndroid Build Coastguard Worker */ 281*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj, 282*f7c14bbaSAndroid Build Coastguard Worker const char *path); 283*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj, 284*f7c14bbaSAndroid Build Coastguard Worker const char *path); 285*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj, 286*f7c14bbaSAndroid Build Coastguard Worker const char *path); 287*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path); 288*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path); 289*f7c14bbaSAndroid Build Coastguard Worker 290*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj); 291*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj); 292*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version); 293*f7c14bbaSAndroid Build Coastguard Worker 294*f7c14bbaSAndroid Build Coastguard Worker struct btf; 295*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj); 296*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj); 297*f7c14bbaSAndroid Build Coastguard Worker 298*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_program * 299*f7c14bbaSAndroid Build Coastguard Worker bpf_object__find_program_by_name(const struct bpf_object *obj, 300*f7c14bbaSAndroid Build Coastguard Worker const char *name); 301*f7c14bbaSAndroid Build Coastguard Worker 302*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int 303*f7c14bbaSAndroid Build Coastguard Worker libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, 304*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type *expected_attach_type); 305*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_attach_type_by_name(const char *name, 306*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type *attach_type); 307*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name, 308*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type attach_type); 309*f7c14bbaSAndroid Build Coastguard Worker 310*f7c14bbaSAndroid Build Coastguard Worker /* Accessors of bpf_program */ 311*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program; 312*f7c14bbaSAndroid Build Coastguard Worker 313*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_program * 314*f7c14bbaSAndroid Build Coastguard Worker bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog); 315*f7c14bbaSAndroid Build Coastguard Worker 316*f7c14bbaSAndroid Build Coastguard Worker #define bpf_object__for_each_program(pos, obj) \ 317*f7c14bbaSAndroid Build Coastguard Worker for ((pos) = bpf_object__next_program((obj), NULL); \ 318*f7c14bbaSAndroid Build Coastguard Worker (pos) != NULL; \ 319*f7c14bbaSAndroid Build Coastguard Worker (pos) = bpf_object__next_program((obj), (pos))) 320*f7c14bbaSAndroid Build Coastguard Worker 321*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_program * 322*f7c14bbaSAndroid Build Coastguard Worker bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog); 323*f7c14bbaSAndroid Build Coastguard Worker 324*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog, 325*f7c14bbaSAndroid Build Coastguard Worker __u32 ifindex); 326*f7c14bbaSAndroid Build Coastguard Worker 327*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); 328*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog); 329*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog); 330*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload); 331*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog); 332*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach); 333*f7c14bbaSAndroid Build Coastguard Worker 334*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn; 335*f7c14bbaSAndroid Build Coastguard Worker 336*f7c14bbaSAndroid Build Coastguard Worker /** 337*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__insns()** gives read-only access to BPF program's 338*f7c14bbaSAndroid Build Coastguard Worker * underlying BPF instructions. 339*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program for which to return instructions 340*f7c14bbaSAndroid Build Coastguard Worker * @return a pointer to an array of BPF instructions that belong to the 341*f7c14bbaSAndroid Build Coastguard Worker * specified BPF program 342*f7c14bbaSAndroid Build Coastguard Worker * 343*f7c14bbaSAndroid Build Coastguard Worker * Returned pointer is always valid and not NULL. Number of `struct bpf_insn` 344*f7c14bbaSAndroid Build Coastguard Worker * pointed to can be fetched using **bpf_program__insn_cnt()** API. 345*f7c14bbaSAndroid Build Coastguard Worker * 346*f7c14bbaSAndroid Build Coastguard Worker * Keep in mind, libbpf can modify and append/delete BPF program's 347*f7c14bbaSAndroid Build Coastguard Worker * instructions as it processes BPF object file and prepares everything for 348*f7c14bbaSAndroid Build Coastguard Worker * uploading into the kernel. So depending on the point in BPF object 349*f7c14bbaSAndroid Build Coastguard Worker * lifetime, **bpf_program__insns()** can return different sets of 350*f7c14bbaSAndroid Build Coastguard Worker * instructions. As an example, during BPF object load phase BPF program 351*f7c14bbaSAndroid Build Coastguard Worker * instructions will be CO-RE-relocated, BPF subprograms instructions will be 352*f7c14bbaSAndroid Build Coastguard Worker * appended, ldimm64 instructions will have FDs embedded, etc. So instructions 353*f7c14bbaSAndroid Build Coastguard Worker * returned before **bpf_object__load()** and after it might be quite 354*f7c14bbaSAndroid Build Coastguard Worker * different. 355*f7c14bbaSAndroid Build Coastguard Worker */ 356*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog); 357*f7c14bbaSAndroid Build Coastguard Worker 358*f7c14bbaSAndroid Build Coastguard Worker /** 359*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__set_insns()** can set BPF program's underlying 360*f7c14bbaSAndroid Build Coastguard Worker * BPF instructions. 361*f7c14bbaSAndroid Build Coastguard Worker * 362*f7c14bbaSAndroid Build Coastguard Worker * WARNING: This is a very advanced libbpf API and users need to know 363*f7c14bbaSAndroid Build Coastguard Worker * what they are doing. This should be used from prog_prepare_load_fn 364*f7c14bbaSAndroid Build Coastguard Worker * callback only. 365*f7c14bbaSAndroid Build Coastguard Worker * 366*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program for which to return instructions 367*f7c14bbaSAndroid Build Coastguard Worker * @param new_insns a pointer to an array of BPF instructions 368*f7c14bbaSAndroid Build Coastguard Worker * @param new_insn_cnt number of `struct bpf_insn`'s that form 369*f7c14bbaSAndroid Build Coastguard Worker * specified BPF program 370*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise 371*f7c14bbaSAndroid Build Coastguard Worker */ 372*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog, 373*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *new_insns, size_t new_insn_cnt); 374*f7c14bbaSAndroid Build Coastguard Worker 375*f7c14bbaSAndroid Build Coastguard Worker /** 376*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s 377*f7c14bbaSAndroid Build Coastguard Worker * that form specified BPF program. 378*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program for which to return number of BPF instructions 379*f7c14bbaSAndroid Build Coastguard Worker * 380*f7c14bbaSAndroid Build Coastguard Worker * See **bpf_program__insns()** documentation for notes on how libbpf can 381*f7c14bbaSAndroid Build Coastguard Worker * change instructions and their count during different phases of 382*f7c14bbaSAndroid Build Coastguard Worker * **bpf_object** lifetime. 383*f7c14bbaSAndroid Build Coastguard Worker */ 384*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog); 385*f7c14bbaSAndroid Build Coastguard Worker 386*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_program__fd(const struct bpf_program *prog); 387*f7c14bbaSAndroid Build Coastguard Worker 388*f7c14bbaSAndroid Build Coastguard Worker /** 389*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__pin()** pins the BPF program to a file 390*f7c14bbaSAndroid Build Coastguard Worker * in the BPF FS specified by a path. This increments the programs 391*f7c14bbaSAndroid Build Coastguard Worker * reference count, allowing it to stay loaded after the process 392*f7c14bbaSAndroid Build Coastguard Worker * which loaded it has exited. 393*f7c14bbaSAndroid Build Coastguard Worker * 394*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to pin, must already be loaded 395*f7c14bbaSAndroid Build Coastguard Worker * @param path file path in a BPF file system 396*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise 397*f7c14bbaSAndroid Build Coastguard Worker */ 398*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path); 399*f7c14bbaSAndroid Build Coastguard Worker 400*f7c14bbaSAndroid Build Coastguard Worker /** 401*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__unpin()** unpins the BPF program from a file 402*f7c14bbaSAndroid Build Coastguard Worker * in the BPFFS specified by a path. This decrements the programs 403*f7c14bbaSAndroid Build Coastguard Worker * reference count. 404*f7c14bbaSAndroid Build Coastguard Worker * 405*f7c14bbaSAndroid Build Coastguard Worker * The file pinning the BPF program can also be unlinked by a different 406*f7c14bbaSAndroid Build Coastguard Worker * process in which case this function will return an error. 407*f7c14bbaSAndroid Build Coastguard Worker * 408*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to unpin 409*f7c14bbaSAndroid Build Coastguard Worker * @param path file path to the pin in a BPF file system 410*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise 411*f7c14bbaSAndroid Build Coastguard Worker */ 412*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path); 413*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void bpf_program__unload(struct bpf_program *prog); 414*f7c14bbaSAndroid Build Coastguard Worker 415*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link; 416*f7c14bbaSAndroid Build Coastguard Worker 417*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link *bpf_link__open(const char *path); 418*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link__fd(const struct bpf_link *link); 419*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link); 420*f7c14bbaSAndroid Build Coastguard Worker /** 421*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_link__pin()** pins the BPF link to a file 422*f7c14bbaSAndroid Build Coastguard Worker * in the BPF FS specified by a path. This increments the links 423*f7c14bbaSAndroid Build Coastguard Worker * reference count, allowing it to stay loaded after the process 424*f7c14bbaSAndroid Build Coastguard Worker * which loaded it has exited. 425*f7c14bbaSAndroid Build Coastguard Worker * 426*f7c14bbaSAndroid Build Coastguard Worker * @param link BPF link to pin, must already be loaded 427*f7c14bbaSAndroid Build Coastguard Worker * @param path file path in a BPF file system 428*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise 429*f7c14bbaSAndroid Build Coastguard Worker */ 430*f7c14bbaSAndroid Build Coastguard Worker 431*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path); 432*f7c14bbaSAndroid Build Coastguard Worker 433*f7c14bbaSAndroid Build Coastguard Worker /** 434*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_link__unpin()** unpins the BPF link from a file 435*f7c14bbaSAndroid Build Coastguard Worker * in the BPFFS specified by a path. This decrements the links 436*f7c14bbaSAndroid Build Coastguard Worker * reference count. 437*f7c14bbaSAndroid Build Coastguard Worker * 438*f7c14bbaSAndroid Build Coastguard Worker * The file pinning the BPF link can also be unlinked by a different 439*f7c14bbaSAndroid Build Coastguard Worker * process in which case this function will return an error. 440*f7c14bbaSAndroid Build Coastguard Worker * 441*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to unpin 442*f7c14bbaSAndroid Build Coastguard Worker * @param path file path to the pin in a BPF file system 443*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise 444*f7c14bbaSAndroid Build Coastguard Worker */ 445*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link__unpin(struct bpf_link *link); 446*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link__update_program(struct bpf_link *link, 447*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog); 448*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void bpf_link__disconnect(struct bpf_link *link); 449*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link__detach(struct bpf_link *link); 450*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link__destroy(struct bpf_link *link); 451*f7c14bbaSAndroid Build Coastguard Worker 452*f7c14bbaSAndroid Build Coastguard Worker /** 453*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__attach()** is a generic function for attaching 454*f7c14bbaSAndroid Build Coastguard Worker * a BPF program based on auto-detection of program type, attach type, 455*f7c14bbaSAndroid Build Coastguard Worker * and extra paremeters, where applicable. 456*f7c14bbaSAndroid Build Coastguard Worker * 457*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to attach 458*f7c14bbaSAndroid Build Coastguard Worker * @return Reference to the newly created BPF link; or NULL is returned on error, 459*f7c14bbaSAndroid Build Coastguard Worker * error code is stored in errno 460*f7c14bbaSAndroid Build Coastguard Worker * 461*f7c14bbaSAndroid Build Coastguard Worker * This is supported for: 462*f7c14bbaSAndroid Build Coastguard Worker * - kprobe/kretprobe (depends on SEC() definition) 463*f7c14bbaSAndroid Build Coastguard Worker * - uprobe/uretprobe (depends on SEC() definition) 464*f7c14bbaSAndroid Build Coastguard Worker * - tracepoint 465*f7c14bbaSAndroid Build Coastguard Worker * - raw tracepoint 466*f7c14bbaSAndroid Build Coastguard Worker * - tracing programs (typed raw TP/fentry/fexit/fmod_ret) 467*f7c14bbaSAndroid Build Coastguard Worker */ 468*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 469*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach(const struct bpf_program *prog); 470*f7c14bbaSAndroid Build Coastguard Worker 471*f7c14bbaSAndroid Build Coastguard Worker struct bpf_perf_event_opts { 472*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 473*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 474*f7c14bbaSAndroid Build Coastguard Worker /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 475*f7c14bbaSAndroid Build Coastguard Worker __u64 bpf_cookie; 476*f7c14bbaSAndroid Build Coastguard Worker /* don't use BPF link when attach BPF program */ 477*f7c14bbaSAndroid Build Coastguard Worker bool force_ioctl_attach; 478*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 479*f7c14bbaSAndroid Build Coastguard Worker }; 480*f7c14bbaSAndroid Build Coastguard Worker #define bpf_perf_event_opts__last_field force_ioctl_attach 481*f7c14bbaSAndroid Build Coastguard Worker 482*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 483*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd); 484*f7c14bbaSAndroid Build Coastguard Worker 485*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 486*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd, 487*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_perf_event_opts *opts); 488*f7c14bbaSAndroid Build Coastguard Worker 489*f7c14bbaSAndroid Build Coastguard Worker /** 490*f7c14bbaSAndroid Build Coastguard Worker * enum probe_attach_mode - the mode to attach kprobe/uprobe 491*f7c14bbaSAndroid Build Coastguard Worker * 492*f7c14bbaSAndroid Build Coastguard Worker * force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will 493*f7c14bbaSAndroid Build Coastguard Worker * be returned if it is not supported by the kernel. 494*f7c14bbaSAndroid Build Coastguard Worker */ 495*f7c14bbaSAndroid Build Coastguard Worker enum probe_attach_mode { 496*f7c14bbaSAndroid Build Coastguard Worker /* attach probe in latest supported mode by kernel */ 497*f7c14bbaSAndroid Build Coastguard Worker PROBE_ATTACH_MODE_DEFAULT = 0, 498*f7c14bbaSAndroid Build Coastguard Worker /* attach probe in legacy mode, using debugfs/tracefs */ 499*f7c14bbaSAndroid Build Coastguard Worker PROBE_ATTACH_MODE_LEGACY, 500*f7c14bbaSAndroid Build Coastguard Worker /* create perf event with perf_event_open() syscall */ 501*f7c14bbaSAndroid Build Coastguard Worker PROBE_ATTACH_MODE_PERF, 502*f7c14bbaSAndroid Build Coastguard Worker /* attach probe with BPF link */ 503*f7c14bbaSAndroid Build Coastguard Worker PROBE_ATTACH_MODE_LINK, 504*f7c14bbaSAndroid Build Coastguard Worker }; 505*f7c14bbaSAndroid Build Coastguard Worker 506*f7c14bbaSAndroid Build Coastguard Worker struct bpf_kprobe_opts { 507*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 508*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 509*f7c14bbaSAndroid Build Coastguard Worker /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 510*f7c14bbaSAndroid Build Coastguard Worker __u64 bpf_cookie; 511*f7c14bbaSAndroid Build Coastguard Worker /* function's offset to install kprobe to */ 512*f7c14bbaSAndroid Build Coastguard Worker size_t offset; 513*f7c14bbaSAndroid Build Coastguard Worker /* kprobe is return probe */ 514*f7c14bbaSAndroid Build Coastguard Worker bool retprobe; 515*f7c14bbaSAndroid Build Coastguard Worker /* kprobe attach mode */ 516*f7c14bbaSAndroid Build Coastguard Worker enum probe_attach_mode attach_mode; 517*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 518*f7c14bbaSAndroid Build Coastguard Worker }; 519*f7c14bbaSAndroid Build Coastguard Worker #define bpf_kprobe_opts__last_field attach_mode 520*f7c14bbaSAndroid Build Coastguard Worker 521*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 522*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe, 523*f7c14bbaSAndroid Build Coastguard Worker const char *func_name); 524*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 525*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_kprobe_opts(const struct bpf_program *prog, 526*f7c14bbaSAndroid Build Coastguard Worker const char *func_name, 527*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_kprobe_opts *opts); 528*f7c14bbaSAndroid Build Coastguard Worker 529*f7c14bbaSAndroid Build Coastguard Worker struct bpf_kprobe_multi_opts { 530*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 531*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 532*f7c14bbaSAndroid Build Coastguard Worker /* array of function symbols to attach */ 533*f7c14bbaSAndroid Build Coastguard Worker const char **syms; 534*f7c14bbaSAndroid Build Coastguard Worker /* array of function addresses to attach */ 535*f7c14bbaSAndroid Build Coastguard Worker const unsigned long *addrs; 536*f7c14bbaSAndroid Build Coastguard Worker /* array of user-provided values fetchable through bpf_get_attach_cookie */ 537*f7c14bbaSAndroid Build Coastguard Worker const __u64 *cookies; 538*f7c14bbaSAndroid Build Coastguard Worker /* number of elements in syms/addrs/cookies arrays */ 539*f7c14bbaSAndroid Build Coastguard Worker size_t cnt; 540*f7c14bbaSAndroid Build Coastguard Worker /* create return kprobes */ 541*f7c14bbaSAndroid Build Coastguard Worker bool retprobe; 542*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 543*f7c14bbaSAndroid Build Coastguard Worker }; 544*f7c14bbaSAndroid Build Coastguard Worker 545*f7c14bbaSAndroid Build Coastguard Worker #define bpf_kprobe_multi_opts__last_field retprobe 546*f7c14bbaSAndroid Build Coastguard Worker 547*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 548*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, 549*f7c14bbaSAndroid Build Coastguard Worker const char *pattern, 550*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_kprobe_multi_opts *opts); 551*f7c14bbaSAndroid Build Coastguard Worker 552*f7c14bbaSAndroid Build Coastguard Worker struct bpf_uprobe_multi_opts { 553*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 554*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 555*f7c14bbaSAndroid Build Coastguard Worker /* array of function symbols to attach to */ 556*f7c14bbaSAndroid Build Coastguard Worker const char **syms; 557*f7c14bbaSAndroid Build Coastguard Worker /* array of function addresses to attach to */ 558*f7c14bbaSAndroid Build Coastguard Worker const unsigned long *offsets; 559*f7c14bbaSAndroid Build Coastguard Worker /* optional, array of associated ref counter offsets */ 560*f7c14bbaSAndroid Build Coastguard Worker const unsigned long *ref_ctr_offsets; 561*f7c14bbaSAndroid Build Coastguard Worker /* optional, array of associated BPF cookies */ 562*f7c14bbaSAndroid Build Coastguard Worker const __u64 *cookies; 563*f7c14bbaSAndroid Build Coastguard Worker /* number of elements in syms/addrs/cookies arrays */ 564*f7c14bbaSAndroid Build Coastguard Worker size_t cnt; 565*f7c14bbaSAndroid Build Coastguard Worker /* create return uprobes */ 566*f7c14bbaSAndroid Build Coastguard Worker bool retprobe; 567*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 568*f7c14bbaSAndroid Build Coastguard Worker }; 569*f7c14bbaSAndroid Build Coastguard Worker 570*f7c14bbaSAndroid Build Coastguard Worker #define bpf_uprobe_multi_opts__last_field retprobe 571*f7c14bbaSAndroid Build Coastguard Worker 572*f7c14bbaSAndroid Build Coastguard Worker /** 573*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program 574*f7c14bbaSAndroid Build Coastguard Worker * to multiple uprobes with uprobe_multi link. 575*f7c14bbaSAndroid Build Coastguard Worker * 576*f7c14bbaSAndroid Build Coastguard Worker * User can specify 2 mutually exclusive set of inputs: 577*f7c14bbaSAndroid Build Coastguard Worker * 578*f7c14bbaSAndroid Build Coastguard Worker * 1) use only path/func_pattern/pid arguments 579*f7c14bbaSAndroid Build Coastguard Worker * 580*f7c14bbaSAndroid Build Coastguard Worker * 2) use path/pid with allowed combinations of 581*f7c14bbaSAndroid Build Coastguard Worker * syms/offsets/ref_ctr_offsets/cookies/cnt 582*f7c14bbaSAndroid Build Coastguard Worker * 583*f7c14bbaSAndroid Build Coastguard Worker * - syms and offsets are mutually exclusive 584*f7c14bbaSAndroid Build Coastguard Worker * - ref_ctr_offsets and cookies are optional 585*f7c14bbaSAndroid Build Coastguard Worker * 586*f7c14bbaSAndroid Build Coastguard Worker * 587*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to attach 588*f7c14bbaSAndroid Build Coastguard Worker * @param pid Process ID to attach the uprobe to, 0 for self (own process), 589*f7c14bbaSAndroid Build Coastguard Worker * -1 for all processes 590*f7c14bbaSAndroid Build Coastguard Worker * @param binary_path Path to binary 591*f7c14bbaSAndroid Build Coastguard Worker * @param func_pattern Regular expression to specify functions to attach 592*f7c14bbaSAndroid Build Coastguard Worker * BPF program to 593*f7c14bbaSAndroid Build Coastguard Worker * @param opts Additional options (see **struct bpf_uprobe_multi_opts**) 594*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error code, otherwise 595*f7c14bbaSAndroid Build Coastguard Worker */ 596*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 597*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_uprobe_multi(const struct bpf_program *prog, 598*f7c14bbaSAndroid Build Coastguard Worker pid_t pid, 599*f7c14bbaSAndroid Build Coastguard Worker const char *binary_path, 600*f7c14bbaSAndroid Build Coastguard Worker const char *func_pattern, 601*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_uprobe_multi_opts *opts); 602*f7c14bbaSAndroid Build Coastguard Worker 603*f7c14bbaSAndroid Build Coastguard Worker struct bpf_ksyscall_opts { 604*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 605*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 606*f7c14bbaSAndroid Build Coastguard Worker /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 607*f7c14bbaSAndroid Build Coastguard Worker __u64 bpf_cookie; 608*f7c14bbaSAndroid Build Coastguard Worker /* attach as return probe? */ 609*f7c14bbaSAndroid Build Coastguard Worker bool retprobe; 610*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 611*f7c14bbaSAndroid Build Coastguard Worker }; 612*f7c14bbaSAndroid Build Coastguard Worker #define bpf_ksyscall_opts__last_field retprobe 613*f7c14bbaSAndroid Build Coastguard Worker 614*f7c14bbaSAndroid Build Coastguard Worker /** 615*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__attach_ksyscall()** attaches a BPF program 616*f7c14bbaSAndroid Build Coastguard Worker * to kernel syscall handler of a specified syscall. Optionally it's possible 617*f7c14bbaSAndroid Build Coastguard Worker * to request to install retprobe that will be triggered at syscall exit. It's 618*f7c14bbaSAndroid Build Coastguard Worker * also possible to associate BPF cookie (though options). 619*f7c14bbaSAndroid Build Coastguard Worker * 620*f7c14bbaSAndroid Build Coastguard Worker * Libbpf automatically will determine correct full kernel function name, 621*f7c14bbaSAndroid Build Coastguard Worker * which depending on system architecture and kernel version/configuration 622*f7c14bbaSAndroid Build Coastguard Worker * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will 623*f7c14bbaSAndroid Build Coastguard Worker * attach specified program using kprobe/kretprobe mechanism. 624*f7c14bbaSAndroid Build Coastguard Worker * 625*f7c14bbaSAndroid Build Coastguard Worker * **bpf_program__attach_ksyscall()** is an API counterpart of declarative 626*f7c14bbaSAndroid Build Coastguard Worker * **SEC("ksyscall/<syscall>")** annotation of BPF programs. 627*f7c14bbaSAndroid Build Coastguard Worker * 628*f7c14bbaSAndroid Build Coastguard Worker * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do 629*f7c14bbaSAndroid Build Coastguard Worker * not handle all the calling convention quirks for mmap(), clone() and compat 630*f7c14bbaSAndroid Build Coastguard Worker * syscalls. It also only attaches to "native" syscall interfaces. If host 631*f7c14bbaSAndroid Build Coastguard Worker * system supports compat syscalls or defines 32-bit syscalls in 64-bit 632*f7c14bbaSAndroid Build Coastguard Worker * kernel, such syscall interfaces won't be attached to by libbpf. 633*f7c14bbaSAndroid Build Coastguard Worker * 634*f7c14bbaSAndroid Build Coastguard Worker * These limitations may or may not change in the future. Therefore it is 635*f7c14bbaSAndroid Build Coastguard Worker * recommended to use SEC("kprobe") for these syscalls or if working with 636*f7c14bbaSAndroid Build Coastguard Worker * compat and 32-bit interfaces is required. 637*f7c14bbaSAndroid Build Coastguard Worker * 638*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to attach 639*f7c14bbaSAndroid Build Coastguard Worker * @param syscall_name Symbolic name of the syscall (e.g., "bpf") 640*f7c14bbaSAndroid Build Coastguard Worker * @param opts Additional options (see **struct bpf_ksyscall_opts**) 641*f7c14bbaSAndroid Build Coastguard Worker * @return Reference to the newly created BPF link; or NULL is returned on 642*f7c14bbaSAndroid Build Coastguard Worker * error, error code is stored in errno 643*f7c14bbaSAndroid Build Coastguard Worker */ 644*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 645*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_ksyscall(const struct bpf_program *prog, 646*f7c14bbaSAndroid Build Coastguard Worker const char *syscall_name, 647*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_ksyscall_opts *opts); 648*f7c14bbaSAndroid Build Coastguard Worker 649*f7c14bbaSAndroid Build Coastguard Worker struct bpf_uprobe_opts { 650*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 651*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 652*f7c14bbaSAndroid Build Coastguard Worker /* offset of kernel reference counted USDT semaphore, added in 653*f7c14bbaSAndroid Build Coastguard Worker * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe") 654*f7c14bbaSAndroid Build Coastguard Worker */ 655*f7c14bbaSAndroid Build Coastguard Worker size_t ref_ctr_offset; 656*f7c14bbaSAndroid Build Coastguard Worker /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 657*f7c14bbaSAndroid Build Coastguard Worker __u64 bpf_cookie; 658*f7c14bbaSAndroid Build Coastguard Worker /* uprobe is return probe, invoked at function return time */ 659*f7c14bbaSAndroid Build Coastguard Worker bool retprobe; 660*f7c14bbaSAndroid Build Coastguard Worker /* Function name to attach to. Could be an unqualified ("abc") or library-qualified 661*f7c14bbaSAndroid Build Coastguard Worker * "abc@LIBXYZ" name. To specify function entry, func_name should be set while 662*f7c14bbaSAndroid Build Coastguard Worker * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an 663*f7c14bbaSAndroid Build Coastguard Worker * offset within a function, specify func_name and use func_offset argument to specify 664*f7c14bbaSAndroid Build Coastguard Worker * offset within the function. Shared library functions must specify the shared library 665*f7c14bbaSAndroid Build Coastguard Worker * binary_path. 666*f7c14bbaSAndroid Build Coastguard Worker */ 667*f7c14bbaSAndroid Build Coastguard Worker const char *func_name; 668*f7c14bbaSAndroid Build Coastguard Worker /* uprobe attach mode */ 669*f7c14bbaSAndroid Build Coastguard Worker enum probe_attach_mode attach_mode; 670*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 671*f7c14bbaSAndroid Build Coastguard Worker }; 672*f7c14bbaSAndroid Build Coastguard Worker #define bpf_uprobe_opts__last_field attach_mode 673*f7c14bbaSAndroid Build Coastguard Worker 674*f7c14bbaSAndroid Build Coastguard Worker /** 675*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__attach_uprobe()** attaches a BPF program 676*f7c14bbaSAndroid Build Coastguard Worker * to the userspace function which is found by binary path and 677*f7c14bbaSAndroid Build Coastguard Worker * offset. You can optionally specify a particular proccess to attach 678*f7c14bbaSAndroid Build Coastguard Worker * to. You can also optionally attach the program to the function 679*f7c14bbaSAndroid Build Coastguard Worker * exit instead of entry. 680*f7c14bbaSAndroid Build Coastguard Worker * 681*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to attach 682*f7c14bbaSAndroid Build Coastguard Worker * @param retprobe Attach to function exit 683*f7c14bbaSAndroid Build Coastguard Worker * @param pid Process ID to attach the uprobe to, 0 for self (own process), 684*f7c14bbaSAndroid Build Coastguard Worker * -1 for all processes 685*f7c14bbaSAndroid Build Coastguard Worker * @param binary_path Path to binary that contains the function symbol 686*f7c14bbaSAndroid Build Coastguard Worker * @param func_offset Offset within the binary of the function symbol 687*f7c14bbaSAndroid Build Coastguard Worker * @return Reference to the newly created BPF link; or NULL is returned on error, 688*f7c14bbaSAndroid Build Coastguard Worker * error code is stored in errno 689*f7c14bbaSAndroid Build Coastguard Worker */ 690*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 691*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe, 692*f7c14bbaSAndroid Build Coastguard Worker pid_t pid, const char *binary_path, 693*f7c14bbaSAndroid Build Coastguard Worker size_t func_offset); 694*f7c14bbaSAndroid Build Coastguard Worker 695*f7c14bbaSAndroid Build Coastguard Worker /** 696*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__attach_uprobe_opts()** is just like 697*f7c14bbaSAndroid Build Coastguard Worker * bpf_program__attach_uprobe() except with a options struct 698*f7c14bbaSAndroid Build Coastguard Worker * for various configurations. 699*f7c14bbaSAndroid Build Coastguard Worker * 700*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to attach 701*f7c14bbaSAndroid Build Coastguard Worker * @param pid Process ID to attach the uprobe to, 0 for self (own process), 702*f7c14bbaSAndroid Build Coastguard Worker * -1 for all processes 703*f7c14bbaSAndroid Build Coastguard Worker * @param binary_path Path to binary that contains the function symbol 704*f7c14bbaSAndroid Build Coastguard Worker * @param func_offset Offset within the binary of the function symbol 705*f7c14bbaSAndroid Build Coastguard Worker * @param opts Options for altering program attachment 706*f7c14bbaSAndroid Build Coastguard Worker * @return Reference to the newly created BPF link; or NULL is returned on error, 707*f7c14bbaSAndroid Build Coastguard Worker * error code is stored in errno 708*f7c14bbaSAndroid Build Coastguard Worker */ 709*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 710*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, 711*f7c14bbaSAndroid Build Coastguard Worker const char *binary_path, size_t func_offset, 712*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_uprobe_opts *opts); 713*f7c14bbaSAndroid Build Coastguard Worker 714*f7c14bbaSAndroid Build Coastguard Worker struct bpf_usdt_opts { 715*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 716*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 717*f7c14bbaSAndroid Build Coastguard Worker /* custom user-provided value accessible through usdt_cookie() */ 718*f7c14bbaSAndroid Build Coastguard Worker __u64 usdt_cookie; 719*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 720*f7c14bbaSAndroid Build Coastguard Worker }; 721*f7c14bbaSAndroid Build Coastguard Worker #define bpf_usdt_opts__last_field usdt_cookie 722*f7c14bbaSAndroid Build Coastguard Worker 723*f7c14bbaSAndroid Build Coastguard Worker /** 724*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__attach_usdt()** is just like 725*f7c14bbaSAndroid Build Coastguard Worker * bpf_program__attach_uprobe_opts() except it covers USDT (User-space 726*f7c14bbaSAndroid Build Coastguard Worker * Statically Defined Tracepoint) attachment, instead of attaching to 727*f7c14bbaSAndroid Build Coastguard Worker * user-space function entry or exit. 728*f7c14bbaSAndroid Build Coastguard Worker * 729*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to attach 730*f7c14bbaSAndroid Build Coastguard Worker * @param pid Process ID to attach the uprobe to, 0 for self (own process), 731*f7c14bbaSAndroid Build Coastguard Worker * -1 for all processes 732*f7c14bbaSAndroid Build Coastguard Worker * @param binary_path Path to binary that contains provided USDT probe 733*f7c14bbaSAndroid Build Coastguard Worker * @param usdt_provider USDT provider name 734*f7c14bbaSAndroid Build Coastguard Worker * @param usdt_name USDT probe name 735*f7c14bbaSAndroid Build Coastguard Worker * @param opts Options for altering program attachment 736*f7c14bbaSAndroid Build Coastguard Worker * @return Reference to the newly created BPF link; or NULL is returned on error, 737*f7c14bbaSAndroid Build Coastguard Worker * error code is stored in errno 738*f7c14bbaSAndroid Build Coastguard Worker */ 739*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 740*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_usdt(const struct bpf_program *prog, 741*f7c14bbaSAndroid Build Coastguard Worker pid_t pid, const char *binary_path, 742*f7c14bbaSAndroid Build Coastguard Worker const char *usdt_provider, const char *usdt_name, 743*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_usdt_opts *opts); 744*f7c14bbaSAndroid Build Coastguard Worker 745*f7c14bbaSAndroid Build Coastguard Worker struct bpf_tracepoint_opts { 746*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 747*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 748*f7c14bbaSAndroid Build Coastguard Worker /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 749*f7c14bbaSAndroid Build Coastguard Worker __u64 bpf_cookie; 750*f7c14bbaSAndroid Build Coastguard Worker }; 751*f7c14bbaSAndroid Build Coastguard Worker #define bpf_tracepoint_opts__last_field bpf_cookie 752*f7c14bbaSAndroid Build Coastguard Worker 753*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 754*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_tracepoint(const struct bpf_program *prog, 755*f7c14bbaSAndroid Build Coastguard Worker const char *tp_category, 756*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name); 757*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 758*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_tracepoint_opts(const struct bpf_program *prog, 759*f7c14bbaSAndroid Build Coastguard Worker const char *tp_category, 760*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name, 761*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_tracepoint_opts *opts); 762*f7c14bbaSAndroid Build Coastguard Worker 763*f7c14bbaSAndroid Build Coastguard Worker struct bpf_raw_tracepoint_opts { 764*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 765*f7c14bbaSAndroid Build Coastguard Worker __u64 cookie; 766*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 767*f7c14bbaSAndroid Build Coastguard Worker }; 768*f7c14bbaSAndroid Build Coastguard Worker #define bpf_raw_tracepoint_opts__last_field cookie 769*f7c14bbaSAndroid Build Coastguard Worker 770*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 771*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, 772*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name); 773*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 774*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog, 775*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name, 776*f7c14bbaSAndroid Build Coastguard Worker struct bpf_raw_tracepoint_opts *opts); 777*f7c14bbaSAndroid Build Coastguard Worker 778*f7c14bbaSAndroid Build Coastguard Worker struct bpf_trace_opts { 779*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 780*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 781*f7c14bbaSAndroid Build Coastguard Worker /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 782*f7c14bbaSAndroid Build Coastguard Worker __u64 cookie; 783*f7c14bbaSAndroid Build Coastguard Worker }; 784*f7c14bbaSAndroid Build Coastguard Worker #define bpf_trace_opts__last_field cookie 785*f7c14bbaSAndroid Build Coastguard Worker 786*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 787*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_trace(const struct bpf_program *prog); 788*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 789*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts); 790*f7c14bbaSAndroid Build Coastguard Worker 791*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 792*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_lsm(const struct bpf_program *prog); 793*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 794*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd); 795*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 796*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd); 797*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 798*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex); 799*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 800*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_freplace(const struct bpf_program *prog, 801*f7c14bbaSAndroid Build Coastguard Worker int target_fd, const char *attach_func_name); 802*f7c14bbaSAndroid Build Coastguard Worker 803*f7c14bbaSAndroid Build Coastguard Worker struct bpf_netfilter_opts { 804*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 805*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 806*f7c14bbaSAndroid Build Coastguard Worker 807*f7c14bbaSAndroid Build Coastguard Worker __u32 pf; 808*f7c14bbaSAndroid Build Coastguard Worker __u32 hooknum; 809*f7c14bbaSAndroid Build Coastguard Worker __s32 priority; 810*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 811*f7c14bbaSAndroid Build Coastguard Worker }; 812*f7c14bbaSAndroid Build Coastguard Worker #define bpf_netfilter_opts__last_field flags 813*f7c14bbaSAndroid Build Coastguard Worker 814*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 815*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_netfilter(const struct bpf_program *prog, 816*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_netfilter_opts *opts); 817*f7c14bbaSAndroid Build Coastguard Worker 818*f7c14bbaSAndroid Build Coastguard Worker struct bpf_tcx_opts { 819*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 820*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 821*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 822*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_fd; 823*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_id; 824*f7c14bbaSAndroid Build Coastguard Worker __u64 expected_revision; 825*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 826*f7c14bbaSAndroid Build Coastguard Worker }; 827*f7c14bbaSAndroid Build Coastguard Worker #define bpf_tcx_opts__last_field expected_revision 828*f7c14bbaSAndroid Build Coastguard Worker 829*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 830*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex, 831*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_tcx_opts *opts); 832*f7c14bbaSAndroid Build Coastguard Worker 833*f7c14bbaSAndroid Build Coastguard Worker struct bpf_netkit_opts { 834*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 835*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 836*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 837*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_fd; 838*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_id; 839*f7c14bbaSAndroid Build Coastguard Worker __u64 expected_revision; 840*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 841*f7c14bbaSAndroid Build Coastguard Worker }; 842*f7c14bbaSAndroid Build Coastguard Worker #define bpf_netkit_opts__last_field expected_revision 843*f7c14bbaSAndroid Build Coastguard Worker 844*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 845*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex, 846*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_netkit_opts *opts); 847*f7c14bbaSAndroid Build Coastguard Worker 848*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map; 849*f7c14bbaSAndroid Build Coastguard Worker 850*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map); 851*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map); 852*f7c14bbaSAndroid Build Coastguard Worker 853*f7c14bbaSAndroid Build Coastguard Worker struct bpf_iter_attach_opts { 854*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct for forward/backward compatibility */ 855*f7c14bbaSAndroid Build Coastguard Worker union bpf_iter_link_info *link_info; 856*f7c14bbaSAndroid Build Coastguard Worker __u32 link_info_len; 857*f7c14bbaSAndroid Build Coastguard Worker }; 858*f7c14bbaSAndroid Build Coastguard Worker #define bpf_iter_attach_opts__last_field link_info_len 859*f7c14bbaSAndroid Build Coastguard Worker 860*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link * 861*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_iter(const struct bpf_program *prog, 862*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_iter_attach_opts *opts); 863*f7c14bbaSAndroid Build Coastguard Worker 864*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog); 865*f7c14bbaSAndroid Build Coastguard Worker 866*f7c14bbaSAndroid Build Coastguard Worker /** 867*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__set_type()** sets the program 868*f7c14bbaSAndroid Build Coastguard Worker * type of the passed BPF program. 869*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to set the program type for 870*f7c14bbaSAndroid Build Coastguard Worker * @param type program type to set the BPF map to have 871*f7c14bbaSAndroid Build Coastguard Worker * @return error code; or 0 if no error. An error occurs 872*f7c14bbaSAndroid Build Coastguard Worker * if the object is already loaded. 873*f7c14bbaSAndroid Build Coastguard Worker * 874*f7c14bbaSAndroid Build Coastguard Worker * This must be called before the BPF object is loaded, 875*f7c14bbaSAndroid Build Coastguard Worker * otherwise it has no effect and an error is returned. 876*f7c14bbaSAndroid Build Coastguard Worker */ 877*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_program__set_type(struct bpf_program *prog, 878*f7c14bbaSAndroid Build Coastguard Worker enum bpf_prog_type type); 879*f7c14bbaSAndroid Build Coastguard Worker 880*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API enum bpf_attach_type 881*f7c14bbaSAndroid Build Coastguard Worker bpf_program__expected_attach_type(const struct bpf_program *prog); 882*f7c14bbaSAndroid Build Coastguard Worker 883*f7c14bbaSAndroid Build Coastguard Worker /** 884*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__set_expected_attach_type()** sets the 885*f7c14bbaSAndroid Build Coastguard Worker * attach type of the passed BPF program. This is used for 886*f7c14bbaSAndroid Build Coastguard Worker * auto-detection of attachment when programs are loaded. 887*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to set the attach type for 888*f7c14bbaSAndroid Build Coastguard Worker * @param type attach type to set the BPF map to have 889*f7c14bbaSAndroid Build Coastguard Worker * @return error code; or 0 if no error. An error occurs 890*f7c14bbaSAndroid Build Coastguard Worker * if the object is already loaded. 891*f7c14bbaSAndroid Build Coastguard Worker * 892*f7c14bbaSAndroid Build Coastguard Worker * This must be called before the BPF object is loaded, 893*f7c14bbaSAndroid Build Coastguard Worker * otherwise it has no effect and an error is returned. 894*f7c14bbaSAndroid Build Coastguard Worker */ 895*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int 896*f7c14bbaSAndroid Build Coastguard Worker bpf_program__set_expected_attach_type(struct bpf_program *prog, 897*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type type); 898*f7c14bbaSAndroid Build Coastguard Worker 899*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog); 900*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags); 901*f7c14bbaSAndroid Build Coastguard Worker 902*f7c14bbaSAndroid Build Coastguard Worker /* Per-program log level and log buffer getters/setters. 903*f7c14bbaSAndroid Build Coastguard Worker * See bpf_object_open_opts comments regarding log_level and log_buf 904*f7c14bbaSAndroid Build Coastguard Worker * interactions. 905*f7c14bbaSAndroid Build Coastguard Worker */ 906*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog); 907*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level); 908*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size); 909*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size); 910*f7c14bbaSAndroid Build Coastguard Worker 911*f7c14bbaSAndroid Build Coastguard Worker /** 912*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_program__set_attach_target()** sets BTF-based attach target 913*f7c14bbaSAndroid Build Coastguard Worker * for supported BPF program types: 914*f7c14bbaSAndroid Build Coastguard Worker * - BTF-aware raw tracepoints (tp_btf); 915*f7c14bbaSAndroid Build Coastguard Worker * - fentry/fexit/fmod_ret; 916*f7c14bbaSAndroid Build Coastguard Worker * - lsm; 917*f7c14bbaSAndroid Build Coastguard Worker * - freplace. 918*f7c14bbaSAndroid Build Coastguard Worker * @param prog BPF program to set the attach type for 919*f7c14bbaSAndroid Build Coastguard Worker * @param type attach type to set the BPF map to have 920*f7c14bbaSAndroid Build Coastguard Worker * @return error code; or 0 if no error occurred. 921*f7c14bbaSAndroid Build Coastguard Worker */ 922*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int 923*f7c14bbaSAndroid Build Coastguard Worker bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd, 924*f7c14bbaSAndroid Build Coastguard Worker const char *attach_func_name); 925*f7c14bbaSAndroid Build Coastguard Worker 926*f7c14bbaSAndroid Build Coastguard Worker /** 927*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_object__find_map_by_name()** returns BPF map of 928*f7c14bbaSAndroid Build Coastguard Worker * the given name, if it exists within the passed BPF object 929*f7c14bbaSAndroid Build Coastguard Worker * @param obj BPF object 930*f7c14bbaSAndroid Build Coastguard Worker * @param name name of the BPF map 931*f7c14bbaSAndroid Build Coastguard Worker * @return BPF map instance, if such map exists within the BPF object; 932*f7c14bbaSAndroid Build Coastguard Worker * or NULL otherwise. 933*f7c14bbaSAndroid Build Coastguard Worker */ 934*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_map * 935*f7c14bbaSAndroid Build Coastguard Worker bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name); 936*f7c14bbaSAndroid Build Coastguard Worker 937*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int 938*f7c14bbaSAndroid Build Coastguard Worker bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name); 939*f7c14bbaSAndroid Build Coastguard Worker 940*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_map * 941*f7c14bbaSAndroid Build Coastguard Worker bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map); 942*f7c14bbaSAndroid Build Coastguard Worker 943*f7c14bbaSAndroid Build Coastguard Worker #define bpf_object__for_each_map(pos, obj) \ 944*f7c14bbaSAndroid Build Coastguard Worker for ((pos) = bpf_object__next_map((obj), NULL); \ 945*f7c14bbaSAndroid Build Coastguard Worker (pos) != NULL; \ 946*f7c14bbaSAndroid Build Coastguard Worker (pos) = bpf_object__next_map((obj), (pos))) 947*f7c14bbaSAndroid Build Coastguard Worker #define bpf_map__for_each bpf_object__for_each_map 948*f7c14bbaSAndroid Build Coastguard Worker 949*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_map * 950*f7c14bbaSAndroid Build Coastguard Worker bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map); 951*f7c14bbaSAndroid Build Coastguard Worker 952*f7c14bbaSAndroid Build Coastguard Worker /** 953*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create 954*f7c14bbaSAndroid Build Coastguard Worker * BPF map during BPF object load phase. 955*f7c14bbaSAndroid Build Coastguard Worker * @param map the BPF map instance 956*f7c14bbaSAndroid Build Coastguard Worker * @param autocreate whether to create BPF map during BPF object load 957*f7c14bbaSAndroid Build Coastguard Worker * @return 0 on success; -EBUSY if BPF object was already loaded 958*f7c14bbaSAndroid Build Coastguard Worker * 959*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating 960*f7c14bbaSAndroid Build Coastguard Worker * BPF map. By default, libbpf will attempt to create every single BPF map 961*f7c14bbaSAndroid Build Coastguard Worker * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall 962*f7c14bbaSAndroid Build Coastguard Worker * and fill in map FD in BPF instructions. 963*f7c14bbaSAndroid Build Coastguard Worker * 964*f7c14bbaSAndroid Build Coastguard Worker * This API allows to opt-out of this process for specific map instance. This 965*f7c14bbaSAndroid Build Coastguard Worker * can be useful if host kernel doesn't support such BPF map type or used 966*f7c14bbaSAndroid Build Coastguard Worker * combination of flags and user application wants to avoid creating such 967*f7c14bbaSAndroid Build Coastguard Worker * a map in the first place. User is still responsible to make sure that their 968*f7c14bbaSAndroid Build Coastguard Worker * BPF-side code that expects to use such missing BPF map is recognized by BPF 969*f7c14bbaSAndroid Build Coastguard Worker * verifier as dead code, otherwise BPF verifier will reject such BPF program. 970*f7c14bbaSAndroid Build Coastguard Worker */ 971*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate); 972*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map); 973*f7c14bbaSAndroid Build Coastguard Worker 974*f7c14bbaSAndroid Build Coastguard Worker /** 975*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__fd()** gets the file descriptor of the passed 976*f7c14bbaSAndroid Build Coastguard Worker * BPF map 977*f7c14bbaSAndroid Build Coastguard Worker * @param map the BPF map instance 978*f7c14bbaSAndroid Build Coastguard Worker * @return the file descriptor; or -EINVAL in case of an error 979*f7c14bbaSAndroid Build Coastguard Worker */ 980*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__fd(const struct bpf_map *map); 981*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd); 982*f7c14bbaSAndroid Build Coastguard Worker /* get map name */ 983*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *bpf_map__name(const struct bpf_map *map); 984*f7c14bbaSAndroid Build Coastguard Worker /* get/set map type */ 985*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map); 986*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type); 987*f7c14bbaSAndroid Build Coastguard Worker /* get/set map size (max_entries) */ 988*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map); 989*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries); 990*f7c14bbaSAndroid Build Coastguard Worker /* get/set map flags */ 991*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map); 992*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags); 993*f7c14bbaSAndroid Build Coastguard Worker /* get/set map NUMA node */ 994*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map); 995*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node); 996*f7c14bbaSAndroid Build Coastguard Worker /* get/set map key size */ 997*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map); 998*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size); 999*f7c14bbaSAndroid Build Coastguard Worker /* get map value size */ 1000*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map); 1001*f7c14bbaSAndroid Build Coastguard Worker /** 1002*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__set_value_size()** sets map value size. 1003*f7c14bbaSAndroid Build Coastguard Worker * @param map the BPF map instance 1004*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error, otherwise 1005*f7c14bbaSAndroid Build Coastguard Worker * 1006*f7c14bbaSAndroid Build Coastguard Worker * There is a special case for maps with associated memory-mapped regions, like 1007*f7c14bbaSAndroid Build Coastguard Worker * the global data section maps (bss, data, rodata). When this function is used 1008*f7c14bbaSAndroid Build Coastguard Worker * on such a map, the mapped region is resized. Afterward, an attempt is made to 1009*f7c14bbaSAndroid Build Coastguard Worker * adjust the corresponding BTF info. This attempt is best-effort and can only 1010*f7c14bbaSAndroid Build Coastguard Worker * succeed if the last variable of the data section map is an array. The array 1011*f7c14bbaSAndroid Build Coastguard Worker * BTF type is replaced by a new BTF array type with a different length. 1012*f7c14bbaSAndroid Build Coastguard Worker * Any previously existing pointers returned from bpf_map__initial_value() or 1013*f7c14bbaSAndroid Build Coastguard Worker * corresponding data section skeleton pointer must be reinitialized. 1014*f7c14bbaSAndroid Build Coastguard Worker */ 1015*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size); 1016*f7c14bbaSAndroid Build Coastguard Worker /* get map key/value BTF type IDs */ 1017*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map); 1018*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map); 1019*f7c14bbaSAndroid Build Coastguard Worker /* get/set map if_index */ 1020*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map); 1021*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); 1022*f7c14bbaSAndroid Build Coastguard Worker /* get/set map map_extra flags */ 1023*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map); 1024*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra); 1025*f7c14bbaSAndroid Build Coastguard Worker 1026*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map, 1027*f7c14bbaSAndroid Build Coastguard Worker const void *data, size_t size); 1028*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize); 1029*f7c14bbaSAndroid Build Coastguard Worker 1030*f7c14bbaSAndroid Build Coastguard Worker /** 1031*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__is_internal()** tells the caller whether or not the 1032*f7c14bbaSAndroid Build Coastguard Worker * passed map is a special map created by libbpf automatically for things like 1033*f7c14bbaSAndroid Build Coastguard Worker * global variables, __ksym externs, Kconfig values, etc 1034*f7c14bbaSAndroid Build Coastguard Worker * @param map the bpf_map 1035*f7c14bbaSAndroid Build Coastguard Worker * @return true, if the map is an internal map; false, otherwise 1036*f7c14bbaSAndroid Build Coastguard Worker */ 1037*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map); 1038*f7c14bbaSAndroid Build Coastguard Worker 1039*f7c14bbaSAndroid Build Coastguard Worker /** 1040*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the 1041*f7c14bbaSAndroid Build Coastguard Worker * BPF map should be pinned. This does not actually create the 'pin'. 1042*f7c14bbaSAndroid Build Coastguard Worker * @param map The bpf_map 1043*f7c14bbaSAndroid Build Coastguard Worker * @param path The path 1044*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error, otherwise 1045*f7c14bbaSAndroid Build Coastguard Worker */ 1046*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path); 1047*f7c14bbaSAndroid Build Coastguard Worker 1048*f7c14bbaSAndroid Build Coastguard Worker /** 1049*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__pin_path()** gets the path attribute that tells where the 1050*f7c14bbaSAndroid Build Coastguard Worker * BPF map should be pinned. 1051*f7c14bbaSAndroid Build Coastguard Worker * @param map The bpf_map 1052*f7c14bbaSAndroid Build Coastguard Worker * @return The path string; which can be NULL 1053*f7c14bbaSAndroid Build Coastguard Worker */ 1054*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map); 1055*f7c14bbaSAndroid Build Coastguard Worker 1056*f7c14bbaSAndroid Build Coastguard Worker /** 1057*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__is_pinned()** tells the caller whether or not the 1058*f7c14bbaSAndroid Build Coastguard Worker * passed map has been pinned via a 'pin' file. 1059*f7c14bbaSAndroid Build Coastguard Worker * @param map The bpf_map 1060*f7c14bbaSAndroid Build Coastguard Worker * @return true, if the map is pinned; false, otherwise 1061*f7c14bbaSAndroid Build Coastguard Worker */ 1062*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map); 1063*f7c14bbaSAndroid Build Coastguard Worker 1064*f7c14bbaSAndroid Build Coastguard Worker /** 1065*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__pin()** creates a file that serves as a 'pin' 1066*f7c14bbaSAndroid Build Coastguard Worker * for the BPF map. This increments the reference count on the 1067*f7c14bbaSAndroid Build Coastguard Worker * BPF map which will keep the BPF map loaded even after the 1068*f7c14bbaSAndroid Build Coastguard Worker * userspace process which loaded it has exited. 1069*f7c14bbaSAndroid Build Coastguard Worker * @param map The bpf_map to pin 1070*f7c14bbaSAndroid Build Coastguard Worker * @param path A file path for the 'pin' 1071*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error, otherwise 1072*f7c14bbaSAndroid Build Coastguard Worker * 1073*f7c14bbaSAndroid Build Coastguard Worker * If `path` is NULL the maps `pin_path` attribute will be used. If this is 1074*f7c14bbaSAndroid Build Coastguard Worker * also NULL, an error will be returned and the map will not be pinned. 1075*f7c14bbaSAndroid Build Coastguard Worker */ 1076*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path); 1077*f7c14bbaSAndroid Build Coastguard Worker 1078*f7c14bbaSAndroid Build Coastguard Worker /** 1079*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__unpin()** removes the file that serves as a 1080*f7c14bbaSAndroid Build Coastguard Worker * 'pin' for the BPF map. 1081*f7c14bbaSAndroid Build Coastguard Worker * @param map The bpf_map to unpin 1082*f7c14bbaSAndroid Build Coastguard Worker * @param path A file path for the 'pin' 1083*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error, otherwise 1084*f7c14bbaSAndroid Build Coastguard Worker * 1085*f7c14bbaSAndroid Build Coastguard Worker * The `path` parameter can be NULL, in which case the `pin_path` 1086*f7c14bbaSAndroid Build Coastguard Worker * map attribute is unpinned. If both the `path` parameter and 1087*f7c14bbaSAndroid Build Coastguard Worker * `pin_path` map attribute are set, they must be equal. 1088*f7c14bbaSAndroid Build Coastguard Worker */ 1089*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path); 1090*f7c14bbaSAndroid Build Coastguard Worker 1091*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd); 1092*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map); 1093*f7c14bbaSAndroid Build Coastguard Worker 1094*f7c14bbaSAndroid Build Coastguard Worker /** 1095*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value 1096*f7c14bbaSAndroid Build Coastguard Worker * corresponding to provided key. 1097*f7c14bbaSAndroid Build Coastguard Worker * @param map BPF map to lookup element in 1098*f7c14bbaSAndroid Build Coastguard Worker * @param key pointer to memory containing bytes of the key used for lookup 1099*f7c14bbaSAndroid Build Coastguard Worker * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1100*f7c14bbaSAndroid Build Coastguard Worker * @param value pointer to memory in which looked up value will be stored 1101*f7c14bbaSAndroid Build Coastguard Worker * @param value_sz size in byte of value data memory; it has to match BPF map 1102*f7c14bbaSAndroid Build Coastguard Worker * definition's **value_size**. For per-CPU BPF maps value size has to be 1103*f7c14bbaSAndroid Build Coastguard Worker * a product of BPF map value size and number of possible CPUs in the system 1104*f7c14bbaSAndroid Build Coastguard Worker * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for 1105*f7c14bbaSAndroid Build Coastguard Worker * per-CPU values value size has to be aligned up to closest 8 bytes for 1106*f7c14bbaSAndroid Build Coastguard Worker * alignment reasons, so expected size is: `round_up(value_size, 8) 1107*f7c14bbaSAndroid Build Coastguard Worker * * libbpf_num_possible_cpus()`. 1108*f7c14bbaSAndroid Build Coastguard Worker * @flags extra flags passed to kernel for this operation 1109*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error, otherwise 1110*f7c14bbaSAndroid Build Coastguard Worker * 1111*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map__lookup_elem()** is high-level equivalent of 1112*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map_lookup_elem()** API with added check for key and value size. 1113*f7c14bbaSAndroid Build Coastguard Worker */ 1114*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map, 1115*f7c14bbaSAndroid Build Coastguard Worker const void *key, size_t key_sz, 1116*f7c14bbaSAndroid Build Coastguard Worker void *value, size_t value_sz, __u64 flags); 1117*f7c14bbaSAndroid Build Coastguard Worker 1118*f7c14bbaSAndroid Build Coastguard Worker /** 1119*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__update_elem()** allows to insert or update value in BPF 1120*f7c14bbaSAndroid Build Coastguard Worker * map that corresponds to provided key. 1121*f7c14bbaSAndroid Build Coastguard Worker * @param map BPF map to insert to or update element in 1122*f7c14bbaSAndroid Build Coastguard Worker * @param key pointer to memory containing bytes of the key 1123*f7c14bbaSAndroid Build Coastguard Worker * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1124*f7c14bbaSAndroid Build Coastguard Worker * @param value pointer to memory containing bytes of the value 1125*f7c14bbaSAndroid Build Coastguard Worker * @param value_sz size in byte of value data memory; it has to match BPF map 1126*f7c14bbaSAndroid Build Coastguard Worker * definition's **value_size**. For per-CPU BPF maps value size has to be 1127*f7c14bbaSAndroid Build Coastguard Worker * a product of BPF map value size and number of possible CPUs in the system 1128*f7c14bbaSAndroid Build Coastguard Worker * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for 1129*f7c14bbaSAndroid Build Coastguard Worker * per-CPU values value size has to be aligned up to closest 8 bytes for 1130*f7c14bbaSAndroid Build Coastguard Worker * alignment reasons, so expected size is: `round_up(value_size, 8) 1131*f7c14bbaSAndroid Build Coastguard Worker * * libbpf_num_possible_cpus()`. 1132*f7c14bbaSAndroid Build Coastguard Worker * @flags extra flags passed to kernel for this operation 1133*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error, otherwise 1134*f7c14bbaSAndroid Build Coastguard Worker * 1135*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map__update_elem()** is high-level equivalent of 1136*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map_update_elem()** API with added check for key and value size. 1137*f7c14bbaSAndroid Build Coastguard Worker */ 1138*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map, 1139*f7c14bbaSAndroid Build Coastguard Worker const void *key, size_t key_sz, 1140*f7c14bbaSAndroid Build Coastguard Worker const void *value, size_t value_sz, __u64 flags); 1141*f7c14bbaSAndroid Build Coastguard Worker 1142*f7c14bbaSAndroid Build Coastguard Worker /** 1143*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that 1144*f7c14bbaSAndroid Build Coastguard Worker * corresponds to provided key. 1145*f7c14bbaSAndroid Build Coastguard Worker * @param map BPF map to delete element from 1146*f7c14bbaSAndroid Build Coastguard Worker * @param key pointer to memory containing bytes of the key 1147*f7c14bbaSAndroid Build Coastguard Worker * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1148*f7c14bbaSAndroid Build Coastguard Worker * @flags extra flags passed to kernel for this operation 1149*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error, otherwise 1150*f7c14bbaSAndroid Build Coastguard Worker * 1151*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map__delete_elem()** is high-level equivalent of 1152*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map_delete_elem()** API with added check for key size. 1153*f7c14bbaSAndroid Build Coastguard Worker */ 1154*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map, 1155*f7c14bbaSAndroid Build Coastguard Worker const void *key, size_t key_sz, __u64 flags); 1156*f7c14bbaSAndroid Build Coastguard Worker 1157*f7c14bbaSAndroid Build Coastguard Worker /** 1158*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value 1159*f7c14bbaSAndroid Build Coastguard Worker * corresponding to provided key and atomically delete it afterwards. 1160*f7c14bbaSAndroid Build Coastguard Worker * @param map BPF map to lookup element in 1161*f7c14bbaSAndroid Build Coastguard Worker * @param key pointer to memory containing bytes of the key used for lookup 1162*f7c14bbaSAndroid Build Coastguard Worker * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1163*f7c14bbaSAndroid Build Coastguard Worker * @param value pointer to memory in which looked up value will be stored 1164*f7c14bbaSAndroid Build Coastguard Worker * @param value_sz size in byte of value data memory; it has to match BPF map 1165*f7c14bbaSAndroid Build Coastguard Worker * definition's **value_size**. For per-CPU BPF maps value size has to be 1166*f7c14bbaSAndroid Build Coastguard Worker * a product of BPF map value size and number of possible CPUs in the system 1167*f7c14bbaSAndroid Build Coastguard Worker * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for 1168*f7c14bbaSAndroid Build Coastguard Worker * per-CPU values value size has to be aligned up to closest 8 bytes for 1169*f7c14bbaSAndroid Build Coastguard Worker * alignment reasons, so expected size is: `round_up(value_size, 8) 1170*f7c14bbaSAndroid Build Coastguard Worker * * libbpf_num_possible_cpus()`. 1171*f7c14bbaSAndroid Build Coastguard Worker * @flags extra flags passed to kernel for this operation 1172*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; negative error, otherwise 1173*f7c14bbaSAndroid Build Coastguard Worker * 1174*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of 1175*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size. 1176*f7c14bbaSAndroid Build Coastguard Worker */ 1177*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map, 1178*f7c14bbaSAndroid Build Coastguard Worker const void *key, size_t key_sz, 1179*f7c14bbaSAndroid Build Coastguard Worker void *value, size_t value_sz, __u64 flags); 1180*f7c14bbaSAndroid Build Coastguard Worker 1181*f7c14bbaSAndroid Build Coastguard Worker /** 1182*f7c14bbaSAndroid Build Coastguard Worker * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by 1183*f7c14bbaSAndroid Build Coastguard Worker * fetching next key that follows current key. 1184*f7c14bbaSAndroid Build Coastguard Worker * @param map BPF map to fetch next key from 1185*f7c14bbaSAndroid Build Coastguard Worker * @param cur_key pointer to memory containing bytes of current key or NULL to 1186*f7c14bbaSAndroid Build Coastguard Worker * fetch the first key 1187*f7c14bbaSAndroid Build Coastguard Worker * @param next_key pointer to memory to write next key into 1188*f7c14bbaSAndroid Build Coastguard Worker * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1189*f7c14bbaSAndroid Build Coastguard Worker * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map; 1190*f7c14bbaSAndroid Build Coastguard Worker * negative error, otherwise 1191*f7c14bbaSAndroid Build Coastguard Worker * 1192*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map__get_next_key()** is high-level equivalent of 1193*f7c14bbaSAndroid Build Coastguard Worker * **bpf_map_get_next_key()** API with added check for key size. 1194*f7c14bbaSAndroid Build Coastguard Worker */ 1195*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map, 1196*f7c14bbaSAndroid Build Coastguard Worker const void *cur_key, void *next_key, size_t key_sz); 1197*f7c14bbaSAndroid Build Coastguard Worker 1198*f7c14bbaSAndroid Build Coastguard Worker struct bpf_xdp_set_link_opts { 1199*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 1200*f7c14bbaSAndroid Build Coastguard Worker int old_fd; 1201*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 1202*f7c14bbaSAndroid Build Coastguard Worker }; 1203*f7c14bbaSAndroid Build Coastguard Worker #define bpf_xdp_set_link_opts__last_field old_fd 1204*f7c14bbaSAndroid Build Coastguard Worker 1205*f7c14bbaSAndroid Build Coastguard Worker struct bpf_xdp_attach_opts { 1206*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 1207*f7c14bbaSAndroid Build Coastguard Worker int old_prog_fd; 1208*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 1209*f7c14bbaSAndroid Build Coastguard Worker }; 1210*f7c14bbaSAndroid Build Coastguard Worker #define bpf_xdp_attach_opts__last_field old_prog_fd 1211*f7c14bbaSAndroid Build Coastguard Worker 1212*f7c14bbaSAndroid Build Coastguard Worker struct bpf_xdp_query_opts { 1213*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 1214*f7c14bbaSAndroid Build Coastguard Worker __u32 prog_id; /* output */ 1215*f7c14bbaSAndroid Build Coastguard Worker __u32 drv_prog_id; /* output */ 1216*f7c14bbaSAndroid Build Coastguard Worker __u32 hw_prog_id; /* output */ 1217*f7c14bbaSAndroid Build Coastguard Worker __u32 skb_prog_id; /* output */ 1218*f7c14bbaSAndroid Build Coastguard Worker __u8 attach_mode; /* output */ 1219*f7c14bbaSAndroid Build Coastguard Worker __u64 feature_flags; /* output */ 1220*f7c14bbaSAndroid Build Coastguard Worker __u32 xdp_zc_max_segs; /* output */ 1221*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 1222*f7c14bbaSAndroid Build Coastguard Worker }; 1223*f7c14bbaSAndroid Build Coastguard Worker #define bpf_xdp_query_opts__last_field xdp_zc_max_segs 1224*f7c14bbaSAndroid Build Coastguard Worker 1225*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags, 1226*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_xdp_attach_opts *opts); 1227*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags, 1228*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_xdp_attach_opts *opts); 1229*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts); 1230*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id); 1231*f7c14bbaSAndroid Build Coastguard Worker 1232*f7c14bbaSAndroid Build Coastguard Worker /* TC related API */ 1233*f7c14bbaSAndroid Build Coastguard Worker enum bpf_tc_attach_point { 1234*f7c14bbaSAndroid Build Coastguard Worker BPF_TC_INGRESS = 1 << 0, 1235*f7c14bbaSAndroid Build Coastguard Worker BPF_TC_EGRESS = 1 << 1, 1236*f7c14bbaSAndroid Build Coastguard Worker BPF_TC_CUSTOM = 1 << 2, 1237*f7c14bbaSAndroid Build Coastguard Worker }; 1238*f7c14bbaSAndroid Build Coastguard Worker 1239*f7c14bbaSAndroid Build Coastguard Worker #define BPF_TC_PARENT(a, b) \ 1240*f7c14bbaSAndroid Build Coastguard Worker ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU)) 1241*f7c14bbaSAndroid Build Coastguard Worker 1242*f7c14bbaSAndroid Build Coastguard Worker enum bpf_tc_flags { 1243*f7c14bbaSAndroid Build Coastguard Worker BPF_TC_F_REPLACE = 1 << 0, 1244*f7c14bbaSAndroid Build Coastguard Worker }; 1245*f7c14bbaSAndroid Build Coastguard Worker 1246*f7c14bbaSAndroid Build Coastguard Worker struct bpf_tc_hook { 1247*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 1248*f7c14bbaSAndroid Build Coastguard Worker int ifindex; 1249*f7c14bbaSAndroid Build Coastguard Worker enum bpf_tc_attach_point attach_point; 1250*f7c14bbaSAndroid Build Coastguard Worker __u32 parent; 1251*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 1252*f7c14bbaSAndroid Build Coastguard Worker }; 1253*f7c14bbaSAndroid Build Coastguard Worker #define bpf_tc_hook__last_field parent 1254*f7c14bbaSAndroid Build Coastguard Worker 1255*f7c14bbaSAndroid Build Coastguard Worker struct bpf_tc_opts { 1256*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 1257*f7c14bbaSAndroid Build Coastguard Worker int prog_fd; 1258*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 1259*f7c14bbaSAndroid Build Coastguard Worker __u32 prog_id; 1260*f7c14bbaSAndroid Build Coastguard Worker __u32 handle; 1261*f7c14bbaSAndroid Build Coastguard Worker __u32 priority; 1262*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 1263*f7c14bbaSAndroid Build Coastguard Worker }; 1264*f7c14bbaSAndroid Build Coastguard Worker #define bpf_tc_opts__last_field priority 1265*f7c14bbaSAndroid Build Coastguard Worker 1266*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook); 1267*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook); 1268*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook, 1269*f7c14bbaSAndroid Build Coastguard Worker struct bpf_tc_opts *opts); 1270*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook, 1271*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_tc_opts *opts); 1272*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook, 1273*f7c14bbaSAndroid Build Coastguard Worker struct bpf_tc_opts *opts); 1274*f7c14bbaSAndroid Build Coastguard Worker 1275*f7c14bbaSAndroid Build Coastguard Worker /* Ring buffer APIs */ 1276*f7c14bbaSAndroid Build Coastguard Worker struct ring_buffer; 1277*f7c14bbaSAndroid Build Coastguard Worker struct ring; 1278*f7c14bbaSAndroid Build Coastguard Worker struct user_ring_buffer; 1279*f7c14bbaSAndroid Build Coastguard Worker 1280*f7c14bbaSAndroid Build Coastguard Worker typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size); 1281*f7c14bbaSAndroid Build Coastguard Worker 1282*f7c14bbaSAndroid Build Coastguard Worker struct ring_buffer_opts { 1283*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct, for forward/backward compatibility */ 1284*f7c14bbaSAndroid Build Coastguard Worker }; 1285*f7c14bbaSAndroid Build Coastguard Worker 1286*f7c14bbaSAndroid Build Coastguard Worker #define ring_buffer_opts__last_field sz 1287*f7c14bbaSAndroid Build Coastguard Worker 1288*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct ring_buffer * 1289*f7c14bbaSAndroid Build Coastguard Worker ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx, 1290*f7c14bbaSAndroid Build Coastguard Worker const struct ring_buffer_opts *opts); 1291*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void ring_buffer__free(struct ring_buffer *rb); 1292*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd, 1293*f7c14bbaSAndroid Build Coastguard Worker ring_buffer_sample_fn sample_cb, void *ctx); 1294*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms); 1295*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb); 1296*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb); 1297*f7c14bbaSAndroid Build Coastguard Worker 1298*f7c14bbaSAndroid Build Coastguard Worker /** 1299*f7c14bbaSAndroid Build Coastguard Worker * @brief **ring_buffer__ring()** returns the ringbuffer object inside a given 1300*f7c14bbaSAndroid Build Coastguard Worker * ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance. 1301*f7c14bbaSAndroid Build Coastguard Worker * 1302*f7c14bbaSAndroid Build Coastguard Worker * @param rb A ringbuffer manager object. 1303*f7c14bbaSAndroid Build Coastguard Worker * @param idx An index into the ringbuffers contained within the ringbuffer 1304*f7c14bbaSAndroid Build Coastguard Worker * manager object. The index is 0-based and corresponds to the order in which 1305*f7c14bbaSAndroid Build Coastguard Worker * ring_buffer__add was called. 1306*f7c14bbaSAndroid Build Coastguard Worker * @return A ringbuffer object on success; NULL and errno set if the index is 1307*f7c14bbaSAndroid Build Coastguard Worker * invalid. 1308*f7c14bbaSAndroid Build Coastguard Worker */ 1309*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb, 1310*f7c14bbaSAndroid Build Coastguard Worker unsigned int idx); 1311*f7c14bbaSAndroid Build Coastguard Worker 1312*f7c14bbaSAndroid Build Coastguard Worker /** 1313*f7c14bbaSAndroid Build Coastguard Worker * @brief **ring__consumer_pos()** returns the current consumer position in the 1314*f7c14bbaSAndroid Build Coastguard Worker * given ringbuffer. 1315*f7c14bbaSAndroid Build Coastguard Worker * 1316*f7c14bbaSAndroid Build Coastguard Worker * @param r A ringbuffer object. 1317*f7c14bbaSAndroid Build Coastguard Worker * @return The current consumer position. 1318*f7c14bbaSAndroid Build Coastguard Worker */ 1319*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r); 1320*f7c14bbaSAndroid Build Coastguard Worker 1321*f7c14bbaSAndroid Build Coastguard Worker /** 1322*f7c14bbaSAndroid Build Coastguard Worker * @brief **ring__producer_pos()** returns the current producer position in the 1323*f7c14bbaSAndroid Build Coastguard Worker * given ringbuffer. 1324*f7c14bbaSAndroid Build Coastguard Worker * 1325*f7c14bbaSAndroid Build Coastguard Worker * @param r A ringbuffer object. 1326*f7c14bbaSAndroid Build Coastguard Worker * @return The current producer position. 1327*f7c14bbaSAndroid Build Coastguard Worker */ 1328*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API unsigned long ring__producer_pos(const struct ring *r); 1329*f7c14bbaSAndroid Build Coastguard Worker 1330*f7c14bbaSAndroid Build Coastguard Worker /** 1331*f7c14bbaSAndroid Build Coastguard Worker * @brief **ring__avail_data_size()** returns the number of bytes in the 1332*f7c14bbaSAndroid Build Coastguard Worker * ringbuffer not yet consumed. This has no locking associated with it, so it 1333*f7c14bbaSAndroid Build Coastguard Worker * can be inaccurate if operations are ongoing while this is called. However, it 1334*f7c14bbaSAndroid Build Coastguard Worker * should still show the correct trend over the long-term. 1335*f7c14bbaSAndroid Build Coastguard Worker * 1336*f7c14bbaSAndroid Build Coastguard Worker * @param r A ringbuffer object. 1337*f7c14bbaSAndroid Build Coastguard Worker * @return The number of bytes not yet consumed. 1338*f7c14bbaSAndroid Build Coastguard Worker */ 1339*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API size_t ring__avail_data_size(const struct ring *r); 1340*f7c14bbaSAndroid Build Coastguard Worker 1341*f7c14bbaSAndroid Build Coastguard Worker /** 1342*f7c14bbaSAndroid Build Coastguard Worker * @brief **ring__size()** returns the total size of the ringbuffer's map data 1343*f7c14bbaSAndroid Build Coastguard Worker * area (excluding special producer/consumer pages). Effectively this gives the 1344*f7c14bbaSAndroid Build Coastguard Worker * amount of usable bytes of data inside the ringbuffer. 1345*f7c14bbaSAndroid Build Coastguard Worker * 1346*f7c14bbaSAndroid Build Coastguard Worker * @param r A ringbuffer object. 1347*f7c14bbaSAndroid Build Coastguard Worker * @return The total size of the ringbuffer map data area. 1348*f7c14bbaSAndroid Build Coastguard Worker */ 1349*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API size_t ring__size(const struct ring *r); 1350*f7c14bbaSAndroid Build Coastguard Worker 1351*f7c14bbaSAndroid Build Coastguard Worker /** 1352*f7c14bbaSAndroid Build Coastguard Worker * @brief **ring__map_fd()** returns the file descriptor underlying the given 1353*f7c14bbaSAndroid Build Coastguard Worker * ringbuffer. 1354*f7c14bbaSAndroid Build Coastguard Worker * 1355*f7c14bbaSAndroid Build Coastguard Worker * @param r A ringbuffer object. 1356*f7c14bbaSAndroid Build Coastguard Worker * @return The underlying ringbuffer file descriptor 1357*f7c14bbaSAndroid Build Coastguard Worker */ 1358*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int ring__map_fd(const struct ring *r); 1359*f7c14bbaSAndroid Build Coastguard Worker 1360*f7c14bbaSAndroid Build Coastguard Worker /** 1361*f7c14bbaSAndroid Build Coastguard Worker * @brief **ring__consume()** consumes available ringbuffer data without event 1362*f7c14bbaSAndroid Build Coastguard Worker * polling. 1363*f7c14bbaSAndroid Build Coastguard Worker * 1364*f7c14bbaSAndroid Build Coastguard Worker * @param r A ringbuffer object. 1365*f7c14bbaSAndroid Build Coastguard Worker * @return The number of records consumed (or INT_MAX, whichever is less), or 1366*f7c14bbaSAndroid Build Coastguard Worker * a negative number if any of the callbacks return an error. 1367*f7c14bbaSAndroid Build Coastguard Worker */ 1368*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int ring__consume(struct ring *r); 1369*f7c14bbaSAndroid Build Coastguard Worker 1370*f7c14bbaSAndroid Build Coastguard Worker struct user_ring_buffer_opts { 1371*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct, for forward/backward compatibility */ 1372*f7c14bbaSAndroid Build Coastguard Worker }; 1373*f7c14bbaSAndroid Build Coastguard Worker 1374*f7c14bbaSAndroid Build Coastguard Worker #define user_ring_buffer_opts__last_field sz 1375*f7c14bbaSAndroid Build Coastguard Worker 1376*f7c14bbaSAndroid Build Coastguard Worker /** 1377*f7c14bbaSAndroid Build Coastguard Worker * @brief **user_ring_buffer__new()** creates a new instance of a user ring 1378*f7c14bbaSAndroid Build Coastguard Worker * buffer. 1379*f7c14bbaSAndroid Build Coastguard Worker * 1380*f7c14bbaSAndroid Build Coastguard Worker * @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map. 1381*f7c14bbaSAndroid Build Coastguard Worker * @param opts Options for how the ring buffer should be created. 1382*f7c14bbaSAndroid Build Coastguard Worker * @return A user ring buffer on success; NULL and errno being set on a 1383*f7c14bbaSAndroid Build Coastguard Worker * failure. 1384*f7c14bbaSAndroid Build Coastguard Worker */ 1385*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct user_ring_buffer * 1386*f7c14bbaSAndroid Build Coastguard Worker user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts); 1387*f7c14bbaSAndroid Build Coastguard Worker 1388*f7c14bbaSAndroid Build Coastguard Worker /** 1389*f7c14bbaSAndroid Build Coastguard Worker * @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the 1390*f7c14bbaSAndroid Build Coastguard Worker * user ring buffer. 1391*f7c14bbaSAndroid Build Coastguard Worker * @param rb A pointer to a user ring buffer. 1392*f7c14bbaSAndroid Build Coastguard Worker * @param size The size of the sample, in bytes. 1393*f7c14bbaSAndroid Build Coastguard Worker * @return A pointer to an 8-byte aligned reserved region of the user ring 1394*f7c14bbaSAndroid Build Coastguard Worker * buffer; NULL, and errno being set if a sample could not be reserved. 1395*f7c14bbaSAndroid Build Coastguard Worker * 1396*f7c14bbaSAndroid Build Coastguard Worker * This function is *not* thread safe, and callers must synchronize accessing 1397*f7c14bbaSAndroid Build Coastguard Worker * this function if there are multiple producers. If a size is requested that 1398*f7c14bbaSAndroid Build Coastguard Worker * is larger than the size of the entire ring buffer, errno will be set to 1399*f7c14bbaSAndroid Build Coastguard Worker * E2BIG and NULL is returned. If the ring buffer could accommodate the size, 1400*f7c14bbaSAndroid Build Coastguard Worker * but currently does not have enough space, errno is set to ENOSPC and NULL is 1401*f7c14bbaSAndroid Build Coastguard Worker * returned. 1402*f7c14bbaSAndroid Build Coastguard Worker * 1403*f7c14bbaSAndroid Build Coastguard Worker * After initializing the sample, callers must invoke 1404*f7c14bbaSAndroid Build Coastguard Worker * **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise, 1405*f7c14bbaSAndroid Build Coastguard Worker * the sample must be freed with **user_ring_buffer__discard()**. 1406*f7c14bbaSAndroid Build Coastguard Worker */ 1407*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size); 1408*f7c14bbaSAndroid Build Coastguard Worker 1409*f7c14bbaSAndroid Build Coastguard Worker /** 1410*f7c14bbaSAndroid Build Coastguard Worker * @brief **user_ring_buffer__reserve_blocking()** reserves a record in the 1411*f7c14bbaSAndroid Build Coastguard Worker * ring buffer, possibly blocking for up to @timeout_ms until a sample becomes 1412*f7c14bbaSAndroid Build Coastguard Worker * available. 1413*f7c14bbaSAndroid Build Coastguard Worker * @param rb The user ring buffer. 1414*f7c14bbaSAndroid Build Coastguard Worker * @param size The size of the sample, in bytes. 1415*f7c14bbaSAndroid Build Coastguard Worker * @param timeout_ms The amount of time, in milliseconds, for which the caller 1416*f7c14bbaSAndroid Build Coastguard Worker * should block when waiting for a sample. -1 causes the caller to block 1417*f7c14bbaSAndroid Build Coastguard Worker * indefinitely. 1418*f7c14bbaSAndroid Build Coastguard Worker * @return A pointer to an 8-byte aligned reserved region of the user ring 1419*f7c14bbaSAndroid Build Coastguard Worker * buffer; NULL, and errno being set if a sample could not be reserved. 1420*f7c14bbaSAndroid Build Coastguard Worker * 1421*f7c14bbaSAndroid Build Coastguard Worker * This function is *not* thread safe, and callers must synchronize 1422*f7c14bbaSAndroid Build Coastguard Worker * accessing this function if there are multiple producers 1423*f7c14bbaSAndroid Build Coastguard Worker * 1424*f7c14bbaSAndroid Build Coastguard Worker * If **timeout_ms** is -1, the function will block indefinitely until a sample 1425*f7c14bbaSAndroid Build Coastguard Worker * becomes available. Otherwise, **timeout_ms** must be non-negative, or errno 1426*f7c14bbaSAndroid Build Coastguard Worker * is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking 1427*f7c14bbaSAndroid Build Coastguard Worker * will occur and the function will return immediately after attempting to 1428*f7c14bbaSAndroid Build Coastguard Worker * reserve a sample. 1429*f7c14bbaSAndroid Build Coastguard Worker * 1430*f7c14bbaSAndroid Build Coastguard Worker * If **size** is larger than the size of the entire ring buffer, errno is set 1431*f7c14bbaSAndroid Build Coastguard Worker * to E2BIG and NULL is returned. If the ring buffer could accommodate 1432*f7c14bbaSAndroid Build Coastguard Worker * **size**, but currently does not have enough space, the caller will block 1433*f7c14bbaSAndroid Build Coastguard Worker * until at most **timeout_ms** has elapsed. If insufficient space is available 1434*f7c14bbaSAndroid Build Coastguard Worker * at that time, errno is set to ENOSPC, and NULL is returned. 1435*f7c14bbaSAndroid Build Coastguard Worker * 1436*f7c14bbaSAndroid Build Coastguard Worker * The kernel guarantees that it will wake up this thread to check if 1437*f7c14bbaSAndroid Build Coastguard Worker * sufficient space is available in the ring buffer at least once per 1438*f7c14bbaSAndroid Build Coastguard Worker * invocation of the **bpf_ringbuf_drain()** helper function, provided that at 1439*f7c14bbaSAndroid Build Coastguard Worker * least one sample is consumed, and the BPF program did not invoke the 1440*f7c14bbaSAndroid Build Coastguard Worker * function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the 1441*f7c14bbaSAndroid Build Coastguard Worker * kernel does not guarantee this. If the helper function is invoked with 1442*f7c14bbaSAndroid Build Coastguard Worker * BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is 1443*f7c14bbaSAndroid Build Coastguard Worker * consumed. 1444*f7c14bbaSAndroid Build Coastguard Worker * 1445*f7c14bbaSAndroid Build Coastguard Worker * When a sample of size **size** is found within **timeout_ms**, a pointer to 1446*f7c14bbaSAndroid Build Coastguard Worker * the sample is returned. After initializing the sample, callers must invoke 1447*f7c14bbaSAndroid Build Coastguard Worker * **user_ring_buffer__submit()** to post the sample to the ring buffer. 1448*f7c14bbaSAndroid Build Coastguard Worker * Otherwise, the sample must be freed with **user_ring_buffer__discard()**. 1449*f7c14bbaSAndroid Build Coastguard Worker */ 1450*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb, 1451*f7c14bbaSAndroid Build Coastguard Worker __u32 size, 1452*f7c14bbaSAndroid Build Coastguard Worker int timeout_ms); 1453*f7c14bbaSAndroid Build Coastguard Worker 1454*f7c14bbaSAndroid Build Coastguard Worker /** 1455*f7c14bbaSAndroid Build Coastguard Worker * @brief **user_ring_buffer__submit()** submits a previously reserved sample 1456*f7c14bbaSAndroid Build Coastguard Worker * into the ring buffer. 1457*f7c14bbaSAndroid Build Coastguard Worker * @param rb The user ring buffer. 1458*f7c14bbaSAndroid Build Coastguard Worker * @param sample A reserved sample. 1459*f7c14bbaSAndroid Build Coastguard Worker * 1460*f7c14bbaSAndroid Build Coastguard Worker * It is not necessary to synchronize amongst multiple producers when invoking 1461*f7c14bbaSAndroid Build Coastguard Worker * this function. 1462*f7c14bbaSAndroid Build Coastguard Worker */ 1463*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample); 1464*f7c14bbaSAndroid Build Coastguard Worker 1465*f7c14bbaSAndroid Build Coastguard Worker /** 1466*f7c14bbaSAndroid Build Coastguard Worker * @brief **user_ring_buffer__discard()** discards a previously reserved sample. 1467*f7c14bbaSAndroid Build Coastguard Worker * @param rb The user ring buffer. 1468*f7c14bbaSAndroid Build Coastguard Worker * @param sample A reserved sample. 1469*f7c14bbaSAndroid Build Coastguard Worker * 1470*f7c14bbaSAndroid Build Coastguard Worker * It is not necessary to synchronize amongst multiple producers when invoking 1471*f7c14bbaSAndroid Build Coastguard Worker * this function. 1472*f7c14bbaSAndroid Build Coastguard Worker */ 1473*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample); 1474*f7c14bbaSAndroid Build Coastguard Worker 1475*f7c14bbaSAndroid Build Coastguard Worker /** 1476*f7c14bbaSAndroid Build Coastguard Worker * @brief **user_ring_buffer__free()** frees a ring buffer that was previously 1477*f7c14bbaSAndroid Build Coastguard Worker * created with **user_ring_buffer__new()**. 1478*f7c14bbaSAndroid Build Coastguard Worker * @param rb The user ring buffer being freed. 1479*f7c14bbaSAndroid Build Coastguard Worker */ 1480*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb); 1481*f7c14bbaSAndroid Build Coastguard Worker 1482*f7c14bbaSAndroid Build Coastguard Worker /* Perf buffer APIs */ 1483*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer; 1484*f7c14bbaSAndroid Build Coastguard Worker 1485*f7c14bbaSAndroid Build Coastguard Worker typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu, 1486*f7c14bbaSAndroid Build Coastguard Worker void *data, __u32 size); 1487*f7c14bbaSAndroid Build Coastguard Worker typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt); 1488*f7c14bbaSAndroid Build Coastguard Worker 1489*f7c14bbaSAndroid Build Coastguard Worker /* common use perf buffer options */ 1490*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer_opts { 1491*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 1492*f7c14bbaSAndroid Build Coastguard Worker __u32 sample_period; 1493*f7c14bbaSAndroid Build Coastguard Worker size_t :0; 1494*f7c14bbaSAndroid Build Coastguard Worker }; 1495*f7c14bbaSAndroid Build Coastguard Worker #define perf_buffer_opts__last_field sample_period 1496*f7c14bbaSAndroid Build Coastguard Worker 1497*f7c14bbaSAndroid Build Coastguard Worker /** 1498*f7c14bbaSAndroid Build Coastguard Worker * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified 1499*f7c14bbaSAndroid Build Coastguard Worker * BPF_PERF_EVENT_ARRAY map 1500*f7c14bbaSAndroid Build Coastguard Worker * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF 1501*f7c14bbaSAndroid Build Coastguard Worker * code to send data over to user-space 1502*f7c14bbaSAndroid Build Coastguard Worker * @param page_cnt number of memory pages allocated for each per-CPU buffer 1503*f7c14bbaSAndroid Build Coastguard Worker * @param sample_cb function called on each received data record 1504*f7c14bbaSAndroid Build Coastguard Worker * @param lost_cb function called when record loss has occurred 1505*f7c14bbaSAndroid Build Coastguard Worker * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb* 1506*f7c14bbaSAndroid Build Coastguard Worker * @return a new instance of struct perf_buffer on success, NULL on error with 1507*f7c14bbaSAndroid Build Coastguard Worker * *errno* containing an error code 1508*f7c14bbaSAndroid Build Coastguard Worker */ 1509*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct perf_buffer * 1510*f7c14bbaSAndroid Build Coastguard Worker perf_buffer__new(int map_fd, size_t page_cnt, 1511*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx, 1512*f7c14bbaSAndroid Build Coastguard Worker const struct perf_buffer_opts *opts); 1513*f7c14bbaSAndroid Build Coastguard Worker 1514*f7c14bbaSAndroid Build Coastguard Worker enum bpf_perf_event_ret { 1515*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_PERF_EVENT_DONE = 0, 1516*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_PERF_EVENT_ERROR = -1, 1517*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_PERF_EVENT_CONT = -2, 1518*f7c14bbaSAndroid Build Coastguard Worker }; 1519*f7c14bbaSAndroid Build Coastguard Worker 1520*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_header; 1521*f7c14bbaSAndroid Build Coastguard Worker 1522*f7c14bbaSAndroid Build Coastguard Worker typedef enum bpf_perf_event_ret 1523*f7c14bbaSAndroid Build Coastguard Worker (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event); 1524*f7c14bbaSAndroid Build Coastguard Worker 1525*f7c14bbaSAndroid Build Coastguard Worker /* raw perf buffer options, giving most power and control */ 1526*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer_raw_opts { 1527*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 1528*f7c14bbaSAndroid Build Coastguard Worker long :0; 1529*f7c14bbaSAndroid Build Coastguard Worker long :0; 1530*f7c14bbaSAndroid Build Coastguard Worker /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of 1531*f7c14bbaSAndroid Build Coastguard Worker * max_entries of given PERF_EVENT_ARRAY map) 1532*f7c14bbaSAndroid Build Coastguard Worker */ 1533*f7c14bbaSAndroid Build Coastguard Worker int cpu_cnt; 1534*f7c14bbaSAndroid Build Coastguard Worker /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */ 1535*f7c14bbaSAndroid Build Coastguard Worker int *cpus; 1536*f7c14bbaSAndroid Build Coastguard Worker /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */ 1537*f7c14bbaSAndroid Build Coastguard Worker int *map_keys; 1538*f7c14bbaSAndroid Build Coastguard Worker }; 1539*f7c14bbaSAndroid Build Coastguard Worker #define perf_buffer_raw_opts__last_field map_keys 1540*f7c14bbaSAndroid Build Coastguard Worker 1541*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_attr; 1542*f7c14bbaSAndroid Build Coastguard Worker 1543*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct perf_buffer * 1544*f7c14bbaSAndroid Build Coastguard Worker perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr, 1545*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_event_fn event_cb, void *ctx, 1546*f7c14bbaSAndroid Build Coastguard Worker const struct perf_buffer_raw_opts *opts); 1547*f7c14bbaSAndroid Build Coastguard Worker 1548*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void perf_buffer__free(struct perf_buffer *pb); 1549*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb); 1550*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms); 1551*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb); 1552*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx); 1553*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb); 1554*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx); 1555*f7c14bbaSAndroid Build Coastguard Worker /** 1556*f7c14bbaSAndroid Build Coastguard Worker * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying 1557*f7c14bbaSAndroid Build Coastguard Worker * memory region of the ring buffer. 1558*f7c14bbaSAndroid Build Coastguard Worker * This ring buffer can be used to implement a custom events consumer. 1559*f7c14bbaSAndroid Build Coastguard Worker * The ring buffer starts with the *struct perf_event_mmap_page*, which 1560*f7c14bbaSAndroid Build Coastguard Worker * holds the ring buffer managment fields, when accessing the header 1561*f7c14bbaSAndroid Build Coastguard Worker * structure it's important to be SMP aware. 1562*f7c14bbaSAndroid Build Coastguard Worker * You can refer to *perf_event_read_simple* for a simple example. 1563*f7c14bbaSAndroid Build Coastguard Worker * @param pb the perf buffer structure 1564*f7c14bbaSAndroid Build Coastguard Worker * @param buf_idx the buffer index to retreive 1565*f7c14bbaSAndroid Build Coastguard Worker * @param buf (out) gets the base pointer of the mmap()'ed memory 1566*f7c14bbaSAndroid Build Coastguard Worker * @param buf_size (out) gets the size of the mmap()'ed region 1567*f7c14bbaSAndroid Build Coastguard Worker * @return 0 on success, negative error code for failure 1568*f7c14bbaSAndroid Build Coastguard Worker */ 1569*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf, 1570*f7c14bbaSAndroid Build Coastguard Worker size_t *buf_size); 1571*f7c14bbaSAndroid Build Coastguard Worker 1572*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_linfo; 1573*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_info; 1574*f7c14bbaSAndroid Build Coastguard Worker 1575*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo); 1576*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_prog_linfo * 1577*f7c14bbaSAndroid Build Coastguard Worker bpf_prog_linfo__new(const struct bpf_prog_info *info); 1578*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const struct bpf_line_info * 1579*f7c14bbaSAndroid Build Coastguard Worker bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo, 1580*f7c14bbaSAndroid Build Coastguard Worker __u64 addr, __u32 func_idx, __u32 nr_skip); 1581*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const struct bpf_line_info * 1582*f7c14bbaSAndroid Build Coastguard Worker bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo, 1583*f7c14bbaSAndroid Build Coastguard Worker __u32 insn_off, __u32 nr_skip); 1584*f7c14bbaSAndroid Build Coastguard Worker 1585*f7c14bbaSAndroid Build Coastguard Worker /* 1586*f7c14bbaSAndroid Build Coastguard Worker * Probe for supported system features 1587*f7c14bbaSAndroid Build Coastguard Worker * 1588*f7c14bbaSAndroid Build Coastguard Worker * Note that running many of these probes in a short amount of time can cause 1589*f7c14bbaSAndroid Build Coastguard Worker * the kernel to reach the maximal size of lockable memory allowed for the 1590*f7c14bbaSAndroid Build Coastguard Worker * user, causing subsequent probes to fail. In this case, the caller may want 1591*f7c14bbaSAndroid Build Coastguard Worker * to adjust that limit with setrlimit(). 1592*f7c14bbaSAndroid Build Coastguard Worker */ 1593*f7c14bbaSAndroid Build Coastguard Worker 1594*f7c14bbaSAndroid Build Coastguard Worker /** 1595*f7c14bbaSAndroid Build Coastguard Worker * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports 1596*f7c14bbaSAndroid Build Coastguard Worker * BPF programs of a given type. 1597*f7c14bbaSAndroid Build Coastguard Worker * @param prog_type BPF program type to detect kernel support for 1598*f7c14bbaSAndroid Build Coastguard Worker * @param opts reserved for future extensibility, should be NULL 1599*f7c14bbaSAndroid Build Coastguard Worker * @return 1, if given program type is supported; 0, if given program type is 1600*f7c14bbaSAndroid Build Coastguard Worker * not supported; negative error code if feature detection failed or can't be 1601*f7c14bbaSAndroid Build Coastguard Worker * performed 1602*f7c14bbaSAndroid Build Coastguard Worker * 1603*f7c14bbaSAndroid Build Coastguard Worker * Make sure the process has required set of CAP_* permissions (or runs as 1604*f7c14bbaSAndroid Build Coastguard Worker * root) when performing feature checking. 1605*f7c14bbaSAndroid Build Coastguard Worker */ 1606*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts); 1607*f7c14bbaSAndroid Build Coastguard Worker /** 1608*f7c14bbaSAndroid Build Coastguard Worker * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports 1609*f7c14bbaSAndroid Build Coastguard Worker * BPF maps of a given type. 1610*f7c14bbaSAndroid Build Coastguard Worker * @param map_type BPF map type to detect kernel support for 1611*f7c14bbaSAndroid Build Coastguard Worker * @param opts reserved for future extensibility, should be NULL 1612*f7c14bbaSAndroid Build Coastguard Worker * @return 1, if given map type is supported; 0, if given map type is 1613*f7c14bbaSAndroid Build Coastguard Worker * not supported; negative error code if feature detection failed or can't be 1614*f7c14bbaSAndroid Build Coastguard Worker * performed 1615*f7c14bbaSAndroid Build Coastguard Worker * 1616*f7c14bbaSAndroid Build Coastguard Worker * Make sure the process has required set of CAP_* permissions (or runs as 1617*f7c14bbaSAndroid Build Coastguard Worker * root) when performing feature checking. 1618*f7c14bbaSAndroid Build Coastguard Worker */ 1619*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts); 1620*f7c14bbaSAndroid Build Coastguard Worker /** 1621*f7c14bbaSAndroid Build Coastguard Worker * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the 1622*f7c14bbaSAndroid Build Coastguard Worker * use of a given BPF helper from specified BPF program type. 1623*f7c14bbaSAndroid Build Coastguard Worker * @param prog_type BPF program type used to check the support of BPF helper 1624*f7c14bbaSAndroid Build Coastguard Worker * @param helper_id BPF helper ID (enum bpf_func_id) to check support for 1625*f7c14bbaSAndroid Build Coastguard Worker * @param opts reserved for future extensibility, should be NULL 1626*f7c14bbaSAndroid Build Coastguard Worker * @return 1, if given combination of program type and helper is supported; 0, 1627*f7c14bbaSAndroid Build Coastguard Worker * if the combination is not supported; negative error code if feature 1628*f7c14bbaSAndroid Build Coastguard Worker * detection for provided input arguments failed or can't be performed 1629*f7c14bbaSAndroid Build Coastguard Worker * 1630*f7c14bbaSAndroid Build Coastguard Worker * Make sure the process has required set of CAP_* permissions (or runs as 1631*f7c14bbaSAndroid Build Coastguard Worker * root) when performing feature checking. 1632*f7c14bbaSAndroid Build Coastguard Worker */ 1633*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, 1634*f7c14bbaSAndroid Build Coastguard Worker enum bpf_func_id helper_id, const void *opts); 1635*f7c14bbaSAndroid Build Coastguard Worker 1636*f7c14bbaSAndroid Build Coastguard Worker /** 1637*f7c14bbaSAndroid Build Coastguard Worker * @brief **libbpf_num_possible_cpus()** is a helper function to get the 1638*f7c14bbaSAndroid Build Coastguard Worker * number of possible CPUs that the host kernel supports and expects. 1639*f7c14bbaSAndroid Build Coastguard Worker * @return number of possible CPUs; or error code on failure 1640*f7c14bbaSAndroid Build Coastguard Worker * 1641*f7c14bbaSAndroid Build Coastguard Worker * Example usage: 1642*f7c14bbaSAndroid Build Coastguard Worker * 1643*f7c14bbaSAndroid Build Coastguard Worker * int ncpus = libbpf_num_possible_cpus(); 1644*f7c14bbaSAndroid Build Coastguard Worker * if (ncpus < 0) { 1645*f7c14bbaSAndroid Build Coastguard Worker * // error handling 1646*f7c14bbaSAndroid Build Coastguard Worker * } 1647*f7c14bbaSAndroid Build Coastguard Worker * long values[ncpus]; 1648*f7c14bbaSAndroid Build Coastguard Worker * bpf_map_lookup_elem(per_cpu_map_fd, key, values); 1649*f7c14bbaSAndroid Build Coastguard Worker */ 1650*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_num_possible_cpus(void); 1651*f7c14bbaSAndroid Build Coastguard Worker 1652*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_skeleton { 1653*f7c14bbaSAndroid Build Coastguard Worker const char *name; 1654*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map **map; 1655*f7c14bbaSAndroid Build Coastguard Worker void **mmaped; 1656*f7c14bbaSAndroid Build Coastguard Worker }; 1657*f7c14bbaSAndroid Build Coastguard Worker 1658*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_skeleton { 1659*f7c14bbaSAndroid Build Coastguard Worker const char *name; 1660*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program **prog; 1661*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link **link; 1662*f7c14bbaSAndroid Build Coastguard Worker }; 1663*f7c14bbaSAndroid Build Coastguard Worker 1664*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object_skeleton { 1665*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct, for forward/backward compatibility */ 1666*f7c14bbaSAndroid Build Coastguard Worker 1667*f7c14bbaSAndroid Build Coastguard Worker const char *name; 1668*f7c14bbaSAndroid Build Coastguard Worker const void *data; 1669*f7c14bbaSAndroid Build Coastguard Worker size_t data_sz; 1670*f7c14bbaSAndroid Build Coastguard Worker 1671*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object **obj; 1672*f7c14bbaSAndroid Build Coastguard Worker 1673*f7c14bbaSAndroid Build Coastguard Worker int map_cnt; 1674*f7c14bbaSAndroid Build Coastguard Worker int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */ 1675*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_skeleton *maps; 1676*f7c14bbaSAndroid Build Coastguard Worker 1677*f7c14bbaSAndroid Build Coastguard Worker int prog_cnt; 1678*f7c14bbaSAndroid Build Coastguard Worker int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */ 1679*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_skeleton *progs; 1680*f7c14bbaSAndroid Build Coastguard Worker }; 1681*f7c14bbaSAndroid Build Coastguard Worker 1682*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int 1683*f7c14bbaSAndroid Build Coastguard Worker bpf_object__open_skeleton(struct bpf_object_skeleton *s, 1684*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_object_open_opts *opts); 1685*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s); 1686*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s); 1687*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s); 1688*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s); 1689*f7c14bbaSAndroid Build Coastguard Worker 1690*f7c14bbaSAndroid Build Coastguard Worker struct bpf_var_skeleton { 1691*f7c14bbaSAndroid Build Coastguard Worker const char *name; 1692*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map **map; 1693*f7c14bbaSAndroid Build Coastguard Worker void **addr; 1694*f7c14bbaSAndroid Build Coastguard Worker }; 1695*f7c14bbaSAndroid Build Coastguard Worker 1696*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object_subskeleton { 1697*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct, for forward/backward compatibility */ 1698*f7c14bbaSAndroid Build Coastguard Worker 1699*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_object *obj; 1700*f7c14bbaSAndroid Build Coastguard Worker 1701*f7c14bbaSAndroid Build Coastguard Worker int map_cnt; 1702*f7c14bbaSAndroid Build Coastguard Worker int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */ 1703*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_skeleton *maps; 1704*f7c14bbaSAndroid Build Coastguard Worker 1705*f7c14bbaSAndroid Build Coastguard Worker int prog_cnt; 1706*f7c14bbaSAndroid Build Coastguard Worker int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */ 1707*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_skeleton *progs; 1708*f7c14bbaSAndroid Build Coastguard Worker 1709*f7c14bbaSAndroid Build Coastguard Worker int var_cnt; 1710*f7c14bbaSAndroid Build Coastguard Worker int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */ 1711*f7c14bbaSAndroid Build Coastguard Worker struct bpf_var_skeleton *vars; 1712*f7c14bbaSAndroid Build Coastguard Worker }; 1713*f7c14bbaSAndroid Build Coastguard Worker 1714*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int 1715*f7c14bbaSAndroid Build Coastguard Worker bpf_object__open_subskeleton(struct bpf_object_subskeleton *s); 1716*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void 1717*f7c14bbaSAndroid Build Coastguard Worker bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s); 1718*f7c14bbaSAndroid Build Coastguard Worker 1719*f7c14bbaSAndroid Build Coastguard Worker struct gen_loader_opts { 1720*f7c14bbaSAndroid Build Coastguard Worker size_t sz; /* size of this struct, for forward/backward compatibility */ 1721*f7c14bbaSAndroid Build Coastguard Worker const char *data; 1722*f7c14bbaSAndroid Build Coastguard Worker const char *insns; 1723*f7c14bbaSAndroid Build Coastguard Worker __u32 data_sz; 1724*f7c14bbaSAndroid Build Coastguard Worker __u32 insns_sz; 1725*f7c14bbaSAndroid Build Coastguard Worker }; 1726*f7c14bbaSAndroid Build Coastguard Worker 1727*f7c14bbaSAndroid Build Coastguard Worker #define gen_loader_opts__last_field insns_sz 1728*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj, 1729*f7c14bbaSAndroid Build Coastguard Worker struct gen_loader_opts *opts); 1730*f7c14bbaSAndroid Build Coastguard Worker 1731*f7c14bbaSAndroid Build Coastguard Worker enum libbpf_tristate { 1732*f7c14bbaSAndroid Build Coastguard Worker TRI_NO = 0, 1733*f7c14bbaSAndroid Build Coastguard Worker TRI_YES = 1, 1734*f7c14bbaSAndroid Build Coastguard Worker TRI_MODULE = 2, 1735*f7c14bbaSAndroid Build Coastguard Worker }; 1736*f7c14bbaSAndroid Build Coastguard Worker 1737*f7c14bbaSAndroid Build Coastguard Worker struct bpf_linker_opts { 1738*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 1739*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 1740*f7c14bbaSAndroid Build Coastguard Worker }; 1741*f7c14bbaSAndroid Build Coastguard Worker #define bpf_linker_opts__last_field sz 1742*f7c14bbaSAndroid Build Coastguard Worker 1743*f7c14bbaSAndroid Build Coastguard Worker struct bpf_linker_file_opts { 1744*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 1745*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 1746*f7c14bbaSAndroid Build Coastguard Worker }; 1747*f7c14bbaSAndroid Build Coastguard Worker #define bpf_linker_file_opts__last_field sz 1748*f7c14bbaSAndroid Build Coastguard Worker 1749*f7c14bbaSAndroid Build Coastguard Worker struct bpf_linker; 1750*f7c14bbaSAndroid Build Coastguard Worker 1751*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts); 1752*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, 1753*f7c14bbaSAndroid Build Coastguard Worker const char *filename, 1754*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_linker_file_opts *opts); 1755*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); 1756*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); 1757*f7c14bbaSAndroid Build Coastguard Worker 1758*f7c14bbaSAndroid Build Coastguard Worker /* 1759*f7c14bbaSAndroid Build Coastguard Worker * Custom handling of BPF program's SEC() definitions 1760*f7c14bbaSAndroid Build Coastguard Worker */ 1761*f7c14bbaSAndroid Build Coastguard Worker 1762*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_load_opts; /* defined in bpf.h */ 1763*f7c14bbaSAndroid Build Coastguard Worker 1764*f7c14bbaSAndroid Build Coastguard Worker /* Called during bpf_object__open() for each recognized BPF program. Callback 1765*f7c14bbaSAndroid Build Coastguard Worker * can use various bpf_program__set_*() setters to adjust whatever properties 1766*f7c14bbaSAndroid Build Coastguard Worker * are necessary. 1767*f7c14bbaSAndroid Build Coastguard Worker */ 1768*f7c14bbaSAndroid Build Coastguard Worker typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie); 1769*f7c14bbaSAndroid Build Coastguard Worker 1770*f7c14bbaSAndroid Build Coastguard Worker /* Called right before libbpf performs bpf_prog_load() to load BPF program 1771*f7c14bbaSAndroid Build Coastguard Worker * into the kernel. Callback can adjust opts as necessary. 1772*f7c14bbaSAndroid Build Coastguard Worker */ 1773*f7c14bbaSAndroid Build Coastguard Worker typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog, 1774*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_load_opts *opts, long cookie); 1775*f7c14bbaSAndroid Build Coastguard Worker 1776*f7c14bbaSAndroid Build Coastguard Worker /* Called during skeleton attach or through bpf_program__attach(). If 1777*f7c14bbaSAndroid Build Coastguard Worker * auto-attach is not supported, callback should return 0 and set link to 1778*f7c14bbaSAndroid Build Coastguard Worker * NULL (it's not considered an error during skeleton attach, but it will be 1779*f7c14bbaSAndroid Build Coastguard Worker * an error for bpf_program__attach() calls). On error, error should be 1780*f7c14bbaSAndroid Build Coastguard Worker * returned directly and link set to NULL. On success, return 0 and set link 1781*f7c14bbaSAndroid Build Coastguard Worker * to a valid struct bpf_link. 1782*f7c14bbaSAndroid Build Coastguard Worker */ 1783*f7c14bbaSAndroid Build Coastguard Worker typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie, 1784*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link **link); 1785*f7c14bbaSAndroid Build Coastguard Worker 1786*f7c14bbaSAndroid Build Coastguard Worker struct libbpf_prog_handler_opts { 1787*f7c14bbaSAndroid Build Coastguard Worker /* size of this struct, for forward/backward compatibility */ 1788*f7c14bbaSAndroid Build Coastguard Worker size_t sz; 1789*f7c14bbaSAndroid Build Coastguard Worker /* User-provided value that is passed to prog_setup_fn, 1790*f7c14bbaSAndroid Build Coastguard Worker * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to 1791*f7c14bbaSAndroid Build Coastguard Worker * register one set of callbacks for multiple SEC() definitions and 1792*f7c14bbaSAndroid Build Coastguard Worker * still be able to distinguish them, if necessary. For example, 1793*f7c14bbaSAndroid Build Coastguard Worker * libbpf itself is using this to pass necessary flags (e.g., 1794*f7c14bbaSAndroid Build Coastguard Worker * sleepable flag) to a common internal SEC() handler. 1795*f7c14bbaSAndroid Build Coastguard Worker */ 1796*f7c14bbaSAndroid Build Coastguard Worker long cookie; 1797*f7c14bbaSAndroid Build Coastguard Worker /* BPF program initialization callback (see libbpf_prog_setup_fn_t). 1798*f7c14bbaSAndroid Build Coastguard Worker * Callback is optional, pass NULL if it's not necessary. 1799*f7c14bbaSAndroid Build Coastguard Worker */ 1800*f7c14bbaSAndroid Build Coastguard Worker libbpf_prog_setup_fn_t prog_setup_fn; 1801*f7c14bbaSAndroid Build Coastguard Worker /* BPF program loading callback (see libbpf_prog_prepare_load_fn_t). 1802*f7c14bbaSAndroid Build Coastguard Worker * Callback is optional, pass NULL if it's not necessary. 1803*f7c14bbaSAndroid Build Coastguard Worker */ 1804*f7c14bbaSAndroid Build Coastguard Worker libbpf_prog_prepare_load_fn_t prog_prepare_load_fn; 1805*f7c14bbaSAndroid Build Coastguard Worker /* BPF program attach callback (see libbpf_prog_attach_fn_t). 1806*f7c14bbaSAndroid Build Coastguard Worker * Callback is optional, pass NULL if it's not necessary. 1807*f7c14bbaSAndroid Build Coastguard Worker */ 1808*f7c14bbaSAndroid Build Coastguard Worker libbpf_prog_attach_fn_t prog_attach_fn; 1809*f7c14bbaSAndroid Build Coastguard Worker }; 1810*f7c14bbaSAndroid Build Coastguard Worker #define libbpf_prog_handler_opts__last_field prog_attach_fn 1811*f7c14bbaSAndroid Build Coastguard Worker 1812*f7c14bbaSAndroid Build Coastguard Worker /** 1813*f7c14bbaSAndroid Build Coastguard Worker * @brief **libbpf_register_prog_handler()** registers a custom BPF program 1814*f7c14bbaSAndroid Build Coastguard Worker * SEC() handler. 1815*f7c14bbaSAndroid Build Coastguard Worker * @param sec section prefix for which custom handler is registered 1816*f7c14bbaSAndroid Build Coastguard Worker * @param prog_type BPF program type associated with specified section 1817*f7c14bbaSAndroid Build Coastguard Worker * @param exp_attach_type Expected BPF attach type associated with specified section 1818*f7c14bbaSAndroid Build Coastguard Worker * @param opts optional cookie, callbacks, and other extra options 1819*f7c14bbaSAndroid Build Coastguard Worker * @return Non-negative handler ID is returned on success. This handler ID has 1820*f7c14bbaSAndroid Build Coastguard Worker * to be passed to *libbpf_unregister_prog_handler()* to unregister such 1821*f7c14bbaSAndroid Build Coastguard Worker * custom handler. Negative error code is returned on error. 1822*f7c14bbaSAndroid Build Coastguard Worker * 1823*f7c14bbaSAndroid Build Coastguard Worker * *sec* defines which SEC() definitions are handled by this custom handler 1824*f7c14bbaSAndroid Build Coastguard Worker * registration. *sec* can have few different forms: 1825*f7c14bbaSAndroid Build Coastguard Worker * - if *sec* is just a plain string (e.g., "abc"), it will match only 1826*f7c14bbaSAndroid Build Coastguard Worker * SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result 1827*f7c14bbaSAndroid Build Coastguard Worker * in an error; 1828*f7c14bbaSAndroid Build Coastguard Worker * - if *sec* is of the form "abc/", proper SEC() form is 1829*f7c14bbaSAndroid Build Coastguard Worker * SEC("abc/something"), where acceptable "something" should be checked by 1830*f7c14bbaSAndroid Build Coastguard Worker * *prog_init_fn* callback, if there are additional restrictions; 1831*f7c14bbaSAndroid Build Coastguard Worker * - if *sec* is of the form "abc+", it will successfully match both 1832*f7c14bbaSAndroid Build Coastguard Worker * SEC("abc") and SEC("abc/whatever") forms; 1833*f7c14bbaSAndroid Build Coastguard Worker * - if *sec* is NULL, custom handler is registered for any BPF program that 1834*f7c14bbaSAndroid Build Coastguard Worker * doesn't match any of the registered (custom or libbpf's own) SEC() 1835*f7c14bbaSAndroid Build Coastguard Worker * handlers. There could be only one such generic custom handler registered 1836*f7c14bbaSAndroid Build Coastguard Worker * at any given time. 1837*f7c14bbaSAndroid Build Coastguard Worker * 1838*f7c14bbaSAndroid Build Coastguard Worker * All custom handlers (except the one with *sec* == NULL) are processed 1839*f7c14bbaSAndroid Build Coastguard Worker * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's 1840*f7c14bbaSAndroid Build Coastguard Worker * SEC() handlers by registering custom ones for the same section prefix 1841*f7c14bbaSAndroid Build Coastguard Worker * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses") 1842*f7c14bbaSAndroid Build Coastguard Worker * handler). 1843*f7c14bbaSAndroid Build Coastguard Worker * 1844*f7c14bbaSAndroid Build Coastguard Worker * Note, like much of global libbpf APIs (e.g., libbpf_set_print(), 1845*f7c14bbaSAndroid Build Coastguard Worker * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs 1846*f7c14bbaSAndroid Build Coastguard Worker * to ensure synchronization if there is a risk of running this API from 1847*f7c14bbaSAndroid Build Coastguard Worker * multiple threads simultaneously. 1848*f7c14bbaSAndroid Build Coastguard Worker */ 1849*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_register_prog_handler(const char *sec, 1850*f7c14bbaSAndroid Build Coastguard Worker enum bpf_prog_type prog_type, 1851*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type exp_attach_type, 1852*f7c14bbaSAndroid Build Coastguard Worker const struct libbpf_prog_handler_opts *opts); 1853*f7c14bbaSAndroid Build Coastguard Worker /** 1854*f7c14bbaSAndroid Build Coastguard Worker * @brief *libbpf_unregister_prog_handler()* unregisters previously registered 1855*f7c14bbaSAndroid Build Coastguard Worker * custom BPF program SEC() handler. 1856*f7c14bbaSAndroid Build Coastguard Worker * @param handler_id handler ID returned by *libbpf_register_prog_handler()* 1857*f7c14bbaSAndroid Build Coastguard Worker * after successful registration 1858*f7c14bbaSAndroid Build Coastguard Worker * @return 0 on success, negative error code if handler isn't found 1859*f7c14bbaSAndroid Build Coastguard Worker * 1860*f7c14bbaSAndroid Build Coastguard Worker * Note, like much of global libbpf APIs (e.g., libbpf_set_print(), 1861*f7c14bbaSAndroid Build Coastguard Worker * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs 1862*f7c14bbaSAndroid Build Coastguard Worker * to ensure synchronization if there is a risk of running this API from 1863*f7c14bbaSAndroid Build Coastguard Worker * multiple threads simultaneously. 1864*f7c14bbaSAndroid Build Coastguard Worker */ 1865*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_unregister_prog_handler(int handler_id); 1866*f7c14bbaSAndroid Build Coastguard Worker 1867*f7c14bbaSAndroid Build Coastguard Worker #ifdef __cplusplus 1868*f7c14bbaSAndroid Build Coastguard Worker } /* extern "C" */ 1869*f7c14bbaSAndroid Build Coastguard Worker #endif 1870*f7c14bbaSAndroid Build Coastguard Worker 1871*f7c14bbaSAndroid Build Coastguard Worker #endif /* __LIBBPF_LIBBPF_H */ 1872