11bc38b8fSAlexei Starovoitov /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 26061a3d6SEric Leblond 31b76c13eSWang Nan /* 41b76c13eSWang Nan * Common eBPF ELF object loading operations. 51b76c13eSWang Nan * 61b76c13eSWang Nan * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 71b76c13eSWang Nan * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 81b76c13eSWang Nan * Copyright (C) 2015 Huawei Inc. 91b76c13eSWang Nan */ 10eff81908SAndrey Ignatov #ifndef __LIBBPF_LIBBPF_H 11eff81908SAndrey Ignatov #define __LIBBPF_LIBBPF_H 121b76c13eSWang Nan 13dfcbc2f2SArnaldo Carvalho de Melo #include <stdarg.h> 141a5e3fb1SWang Nan #include <stdio.h> 15e28ff1a8SJoe Stringer #include <stdint.h> 16aa9b1ac3SWang Nan #include <stdbool.h> 175a6acad1SWang Nan #include <sys/types.h> // for size_t 18dd26b7f5SAlexei Starovoitov #include <linux/bpf.h> 196371ca3bSWang Nan 20544402d4SAndrii Nakryiko #include "libbpf_common.h" 215981881dSAndrii Nakryiko #include "libbpf_legacy.h" 22544402d4SAndrii Nakryiko 238c4905b9SStanislav Fomichev #ifdef __cplusplus 248c4905b9SStanislav Fomichev extern "C" { 258c4905b9SStanislav Fomichev #endif 268c4905b9SStanislav Fomichev 277615209fSAndrii Nakryiko LIBBPF_API __u32 libbpf_major_version(void); 287615209fSAndrii Nakryiko LIBBPF_API __u32 libbpf_minor_version(void); 297615209fSAndrii Nakryiko LIBBPF_API const char *libbpf_version_string(void); 307615209fSAndrii Nakryiko 316371ca3bSWang Nan enum libbpf_errno { 326371ca3bSWang Nan __LIBBPF_ERRNO__START = 4000, 336371ca3bSWang Nan 346371ca3bSWang Nan /* Something wrong in libelf */ 356371ca3bSWang Nan LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START, 366371ca3bSWang Nan LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */ 376371ca3bSWang Nan LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */ 38de8a63bdSColin Ian King LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */ 396371ca3bSWang Nan LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */ 406371ca3bSWang Nan LIBBPF_ERRNO__RELOC, /* Relocation failed */ 416371ca3bSWang Nan LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */ 426371ca3bSWang Nan LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */ 436371ca3bSWang Nan LIBBPF_ERRNO__PROG2BIG, /* Program too big */ 446371ca3bSWang Nan LIBBPF_ERRNO__KVER, /* Incorrect kernel version */ 45705fa219SWang Nan LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */ 46949abbe8SEric Leblond LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */ 47949abbe8SEric Leblond LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */ 4836f1678dSYonghong Song LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */ 496371ca3bSWang Nan __LIBBPF_ERRNO__END, 506371ca3bSWang Nan }; 516371ca3bSWang Nan 52ab9e0848SAndrey Ignatov LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size); 531a5e3fb1SWang Nan 54d18616e7SDaniel Müller /** 55ccde5760SDaniel Müller * @brief **libbpf_bpf_attach_type_str()** converts the provided attach type 56ccde5760SDaniel Müller * value into a textual representation. 57ccde5760SDaniel Müller * @param t The attach type. 58ccde5760SDaniel Müller * @return Pointer to a static string identifying the attach type. NULL is 59ccde5760SDaniel Müller * returned for unknown **bpf_attach_type** values. 60ccde5760SDaniel Müller */ 61ccde5760SDaniel Müller LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t); 62ccde5760SDaniel Müller 63ccde5760SDaniel Müller /** 64ba5d1b58SDaniel Müller * @brief **libbpf_bpf_link_type_str()** converts the provided link type value 65ba5d1b58SDaniel Müller * into a textual representation. 66ba5d1b58SDaniel Müller * @param t The link type. 67ba5d1b58SDaniel Müller * @return Pointer to a static string identifying the link type. NULL is 68ba5d1b58SDaniel Müller * returned for unknown **bpf_link_type** values. 69ba5d1b58SDaniel Müller */ 70ba5d1b58SDaniel Müller LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t); 71ba5d1b58SDaniel Müller 72ba5d1b58SDaniel Müller /** 733e6dc020SDaniel Müller * @brief **libbpf_bpf_map_type_str()** converts the provided map type value 743e6dc020SDaniel Müller * into a textual representation. 753e6dc020SDaniel Müller * @param t The map type. 763e6dc020SDaniel Müller * @return Pointer to a static string identifying the map type. NULL is 773e6dc020SDaniel Müller * returned for unknown **bpf_map_type** values. 783e6dc020SDaniel Müller */ 793e6dc020SDaniel Müller LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t); 803e6dc020SDaniel Müller 813e6dc020SDaniel Müller /** 82d18616e7SDaniel Müller * @brief **libbpf_bpf_prog_type_str()** converts the provided program type 83d18616e7SDaniel Müller * value into a textual representation. 84d18616e7SDaniel Müller * @param t The program type. 85d18616e7SDaniel Müller * @return Pointer to a static string identifying the program type. NULL is 86d18616e7SDaniel Müller * returned for unknown **bpf_prog_type** values. 87d18616e7SDaniel Müller */ 88d18616e7SDaniel Müller LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t); 89d18616e7SDaniel Müller 908461ef8bSYonghong Song enum libbpf_print_level { 918461ef8bSYonghong Song LIBBPF_WARN, 928461ef8bSYonghong Song LIBBPF_INFO, 938461ef8bSYonghong Song LIBBPF_DEBUG, 948461ef8bSYonghong Song }; 958461ef8bSYonghong Song 966f1ae8b6SYonghong Song typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level, 97a8a1f7d0SStanislav Fomichev const char *, va_list ap); 98b3f59d66SWang Nan 99678a1c03SXin Liu /** 100678a1c03SXin Liu * @brief **libbpf_set_print()** sets user-provided log callback function to 101678a1c03SXin Liu * be used for libbpf warnings and informational messages. 102678a1c03SXin Liu * @param fn The log print function. If NULL, libbpf won't print anything. 103678a1c03SXin Liu * @return Pointer to old print function. 104f1cb927cSJP Kobryn * 105f1cb927cSJP Kobryn * This function is thread-safe. 106678a1c03SXin Liu */ 107e87fd8baSAndrii Nakryiko LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn); 108b3f59d66SWang Nan 1091a5e3fb1SWang Nan /* Hide internal to user */ 1101a5e3fb1SWang Nan struct bpf_object; 1111a5e3fb1SWang Nan 1122ce8450eSAndrii Nakryiko struct bpf_object_open_opts { 1139bbdfad8SDaniel Müller /* size of this struct, for forward/backward compatibility */ 1142ce8450eSAndrii Nakryiko size_t sz; 1152ce8450eSAndrii Nakryiko /* object name override, if provided: 1162ce8450eSAndrii Nakryiko * - for object open from file, this will override setting object 1172ce8450eSAndrii Nakryiko * name from file path's base name; 1182ce8450eSAndrii Nakryiko * - for object open from memory buffer, this will specify an object 1192ce8450eSAndrii Nakryiko * name and will override default "<addr>-<buf-size>" name; 1202ce8450eSAndrii Nakryiko */ 1212ce8450eSAndrii Nakryiko const char *object_name; 1222ce8450eSAndrii Nakryiko /* parse map definitions non-strictly, allowing extra attributes/data */ 1232ce8450eSAndrii Nakryiko bool relaxed_maps; 12457a00f41SToke Høiland-Jørgensen /* maps that set the 'pinning' attribute in their definition will have 12557a00f41SToke Høiland-Jørgensen * their pin_path attribute set to a file in this directory, and be 12657a00f41SToke Høiland-Jørgensen * auto-pinned to that path on load; defaults to "/sys/fs/bpf". 12757a00f41SToke Høiland-Jørgensen */ 12857a00f41SToke Høiland-Jørgensen const char *pin_root_path; 129dbdea9b3SAndrii Nakryiko 130dbdea9b3SAndrii Nakryiko __u32 :32; /* stub out now removed attach_prog_fd */ 131dbdea9b3SAndrii Nakryiko 1328601fd42SAndrii Nakryiko /* Additional kernel config content that augments and overrides 1338601fd42SAndrii Nakryiko * system Kconfig for CONFIG_xxx externs. 134166750bcSAndrii Nakryiko */ 1358601fd42SAndrii Nakryiko const char *kconfig; 1361373ff59SShuyi Cheng /* Path to the custom BTF to be used for BPF CO-RE relocations. 1371373ff59SShuyi Cheng * This custom BTF completely replaces the use of vmlinux BTF 1381373ff59SShuyi Cheng * for the purpose of CO-RE relocations. 1391373ff59SShuyi Cheng * NOTE: any other BPF feature (e.g., fentry/fexit programs, 1401373ff59SShuyi Cheng * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux. 1411373ff59SShuyi Cheng */ 1421373ff59SShuyi Cheng const char *btf_custom_path; 143e0e3ea88SAndrii Nakryiko /* Pointer to a buffer for storing kernel logs for applicable BPF 144e0e3ea88SAndrii Nakryiko * commands. Valid kernel_log_size has to be specified as well and are 145e0e3ea88SAndrii Nakryiko * passed-through to bpf() syscall. Keep in mind that kernel might 146e0e3ea88SAndrii Nakryiko * fail operation with -ENOSPC error if provided buffer is too small 147e0e3ea88SAndrii Nakryiko * to contain entire log output. 148e0e3ea88SAndrii Nakryiko * See the comment below for kernel_log_level for interaction between 149e0e3ea88SAndrii Nakryiko * log_buf and log_level settings. 150e0e3ea88SAndrii Nakryiko * 151e0e3ea88SAndrii Nakryiko * If specified, this log buffer will be passed for: 152e0e3ea88SAndrii Nakryiko * - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden 153e0e3ea88SAndrii Nakryiko * with bpf_program__set_log() on per-program level, to get 154e0e3ea88SAndrii Nakryiko * BPF verifier log output. 155e0e3ea88SAndrii Nakryiko * - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get 156e0e3ea88SAndrii Nakryiko * BTF sanity checking log. 157e0e3ea88SAndrii Nakryiko * 158e0e3ea88SAndrii Nakryiko * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite 159e0e3ea88SAndrii Nakryiko * previous contents, so if you need more fine-grained control, set 160e0e3ea88SAndrii Nakryiko * per-program buffer with bpf_program__set_log_buf() to preserve each 161e0e3ea88SAndrii Nakryiko * individual program's verification log. Keep using kernel_log_buf 162e0e3ea88SAndrii Nakryiko * for BTF verification log, if necessary. 163e0e3ea88SAndrii Nakryiko */ 164e0e3ea88SAndrii Nakryiko char *kernel_log_buf; 165e0e3ea88SAndrii Nakryiko size_t kernel_log_size; 166e0e3ea88SAndrii Nakryiko /* 167e0e3ea88SAndrii Nakryiko * Log level can be set independently from log buffer. Log_level=0 168e0e3ea88SAndrii Nakryiko * means that libbpf will attempt loading BTF or program without any 169e0e3ea88SAndrii Nakryiko * logging requested, but will retry with either its own or custom log 170e0e3ea88SAndrii Nakryiko * buffer, if provided, and log_level=1 on any error. 171e0e3ea88SAndrii Nakryiko * And vice versa, setting log_level>0 will request BTF or prog 172e0e3ea88SAndrii Nakryiko * loading with verbose log from the first attempt (and as such also 173e0e3ea88SAndrii Nakryiko * for successfully loaded BTF or program), and the actual log buffer 174e0e3ea88SAndrii Nakryiko * could be either libbpf's own auto-allocated log buffer, if 175e0e3ea88SAndrii Nakryiko * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf. 176e0e3ea88SAndrii Nakryiko * If user didn't provide custom log buffer, libbpf will emit captured 177e0e3ea88SAndrii Nakryiko * logs through its print callback. 178e0e3ea88SAndrii Nakryiko */ 179e0e3ea88SAndrii Nakryiko __u32 kernel_log_level; 180e0e3ea88SAndrii Nakryiko 181e0e3ea88SAndrii Nakryiko size_t :0; 1822ce8450eSAndrii Nakryiko }; 183e0e3ea88SAndrii Nakryiko #define bpf_object_open_opts__last_field kernel_log_level 1842ce8450eSAndrii Nakryiko 185678a1c03SXin Liu /** 186678a1c03SXin Liu * @brief **bpf_object__open()** creates a bpf_object by opening 187678a1c03SXin Liu * the BPF ELF object file pointed to by the passed path and loading it 188678a1c03SXin Liu * into memory. 189678a1c03SXin Liu * @param path BPF object file path. 190678a1c03SXin Liu * @return pointer to the new bpf_object; or NULL is returned on error, 191678a1c03SXin Liu * error code is stored in errno 192678a1c03SXin Liu */ 193ab9e0848SAndrey Ignatov LIBBPF_API struct bpf_object *bpf_object__open(const char *path); 194d5284dedSGrant Seltzer 195d5284dedSGrant Seltzer /** 196d5284dedSGrant Seltzer * @brief **bpf_object__open_file()** creates a bpf_object by opening 197d5284dedSGrant Seltzer * the BPF ELF object file pointed to by the passed path and loading it 198d5284dedSGrant Seltzer * into memory. 199d5284dedSGrant Seltzer * @param path BPF object file path 200d5284dedSGrant Seltzer * @param opts options for how to load the bpf object, this parameter is 201d5284dedSGrant Seltzer * optional and can be set to NULL 202d5284dedSGrant Seltzer * @return pointer to the new bpf_object; or NULL is returned on error, 203d5284dedSGrant Seltzer * error code is stored in errno 204d5284dedSGrant Seltzer */ 205ab9e0848SAndrey Ignatov LIBBPF_API struct bpf_object * 20601af3bf0SAndrii Nakryiko bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts); 207d5284dedSGrant Seltzer 208d5284dedSGrant Seltzer /** 209d5284dedSGrant Seltzer * @brief **bpf_object__open_mem()** creates a bpf_object by reading 210d5284dedSGrant Seltzer * the BPF objects raw bytes from a memory buffer containing a valid 211d5284dedSGrant Seltzer * BPF ELF object file. 212d5284dedSGrant Seltzer * @param obj_buf pointer to the buffer containing ELF file bytes 213d5284dedSGrant Seltzer * @param obj_buf_sz number of bytes in the buffer 214d5284dedSGrant Seltzer * @param opts options for how to load the bpf object 215d5284dedSGrant Seltzer * @return pointer to the new bpf_object; or NULL is returned on error, 216d5284dedSGrant Seltzer * error code is stored in errno 217d5284dedSGrant Seltzer */ 2182ce8450eSAndrii Nakryiko LIBBPF_API struct bpf_object * 2192ce8450eSAndrii Nakryiko bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, 22001af3bf0SAndrii Nakryiko const struct bpf_object_open_opts *opts); 2212ce8450eSAndrii Nakryiko 222678a1c03SXin Liu /** 223678a1c03SXin Liu * @brief **bpf_object__load()** loads BPF object into kernel. 224678a1c03SXin Liu * @param obj Pointer to a valid BPF object instance returned by 225678a1c03SXin Liu * **bpf_object__open*()** APIs 226678a1c03SXin Liu * @return 0, on success; negative error code, otherwise, error code is 227678a1c03SXin Liu * stored in errno 228678a1c03SXin Liu */ 229146bf811SAndrii Nakryiko LIBBPF_API int bpf_object__load(struct bpf_object *obj); 2302ce8450eSAndrii Nakryiko 231678a1c03SXin Liu /** 232678a1c03SXin Liu * @brief **bpf_object__close()** closes a BPF object and releases all 233678a1c03SXin Liu * resources. 234678a1c03SXin Liu * @param obj Pointer to a valid BPF object 235678a1c03SXin Liu */ 236678a1c03SXin Liu LIBBPF_API void bpf_object__close(struct bpf_object *obj); 23757a00f41SToke Høiland-Jørgensen 238e4ce876fSGrant Seltzer /** 239e4ce876fSGrant Seltzer * @brief **bpf_object__pin_maps()** pins each map contained within 240e4ce876fSGrant Seltzer * the BPF object at the passed directory. 241e4ce876fSGrant Seltzer * @param obj Pointer to a valid BPF object 242e4ce876fSGrant Seltzer * @param path A directory where maps should be pinned. 243e4ce876fSGrant Seltzer * @return 0, on success; negative error code, otherwise 244e4ce876fSGrant Seltzer * 245e4ce876fSGrant Seltzer * If `path` is NULL `bpf_map__pin` (which is being used on each map) 246e4ce876fSGrant Seltzer * will use the pin_path attribute of each map. In this case, maps that 247e4ce876fSGrant Seltzer * don't have a pin_path set will be ignored. 2484580b25fSToke Høiland-Jørgensen */ 2490c19a9fbSStanislav Fomichev LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path); 250e4ce876fSGrant Seltzer 251e4ce876fSGrant Seltzer /** 252e4ce876fSGrant Seltzer * @brief **bpf_object__unpin_maps()** unpins each map contained within 253e4ce876fSGrant Seltzer * the BPF object found in the passed directory. 254e4ce876fSGrant Seltzer * @param obj Pointer to a valid BPF object 255e4ce876fSGrant Seltzer * @param path A directory where pinned maps should be searched for. 256e4ce876fSGrant Seltzer * @return 0, on success; negative error code, otherwise 257e4ce876fSGrant Seltzer * 258e4ce876fSGrant Seltzer * If `path` is NULL `bpf_map__unpin` (which is being used on each map) 259e4ce876fSGrant Seltzer * will use the pin_path attribute of each map. In this case, maps that 260e4ce876fSGrant Seltzer * don't have a pin_path set will be ignored. 261e4ce876fSGrant Seltzer */ 2620c19a9fbSStanislav Fomichev LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj, 2630c19a9fbSStanislav Fomichev const char *path); 2640c19a9fbSStanislav Fomichev LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj, 2650c19a9fbSStanislav Fomichev const char *path); 2660c19a9fbSStanislav Fomichev LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj, 2670c19a9fbSStanislav Fomichev const char *path); 268ab9e0848SAndrey Ignatov LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path); 269*068ca522SDaniel Xu LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path); 27001af3bf0SAndrii Nakryiko 271a324aae3SAndrii Nakryiko LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj); 272a324aae3SAndrii Nakryiko LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj); 273155f556dSRafael David Tinoco LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version); 274789f6babSAndrey Ignatov 275789f6babSAndrey Ignatov struct btf; 276a324aae3SAndrii Nakryiko LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj); 277ab9e0848SAndrey Ignatov LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj); 27852d3352eSWang Nan 27901af3bf0SAndrii Nakryiko LIBBPF_API struct bpf_program * 28001af3bf0SAndrii Nakryiko bpf_object__find_program_by_name(const struct bpf_object *obj, 28101af3bf0SAndrii Nakryiko const char *name); 2826d4b198bSJakub Kicinski 283ab9e0848SAndrey Ignatov LIBBPF_API int 284ab9e0848SAndrey Ignatov libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, 285b60df2a0SJakub Kicinski enum bpf_attach_type *expected_attach_type); 286ab9e0848SAndrey Ignatov LIBBPF_API int libbpf_attach_type_by_name(const char *name, 287956b620fSAndrey Ignatov enum bpf_attach_type *attach_type); 288b8c54ea4SAlexei Starovoitov LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name, 289b8c54ea4SAlexei Starovoitov enum bpf_attach_type attach_type); 290b60df2a0SJakub Kicinski 2912eb57bb8SJakub Kicinski /* Accessors of bpf_program */ 292aa9b1ac3SWang Nan struct bpf_program; 293146bf811SAndrii Nakryiko 2942088a3a7SHengqi Chen LIBBPF_API struct bpf_program * 2952088a3a7SHengqi Chen bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog); 296aa9b1ac3SWang Nan 297aa9b1ac3SWang Nan #define bpf_object__for_each_program(pos, obj) \ 2982088a3a7SHengqi Chen for ((pos) = bpf_object__next_program((obj), NULL); \ 299aa9b1ac3SWang Nan (pos) != NULL; \ 3002088a3a7SHengqi Chen (pos) = bpf_object__next_program((obj), (pos))) 301aa9b1ac3SWang Nan 3022088a3a7SHengqi Chen LIBBPF_API struct bpf_program * 3032088a3a7SHengqi Chen bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog); 3040c19a9fbSStanislav Fomichev 305ab9e0848SAndrey Ignatov LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog, 306ab9e0848SAndrey Ignatov __u32 ifindex); 307aa9b1ac3SWang Nan 30801af3bf0SAndrii Nakryiko LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); 30952109584SAndrii Nakryiko LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog); 310d9297581SAndrii Nakryiko LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog); 311d9297581SAndrii Nakryiko LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload); 31243cb8cbaSHao Luo LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog); 31343cb8cbaSHao Luo LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach); 314aa9b1ac3SWang Nan 31565a7fa2eSAndrii Nakryiko struct bpf_insn; 31665a7fa2eSAndrii Nakryiko 31765a7fa2eSAndrii Nakryiko /** 31865a7fa2eSAndrii Nakryiko * @brief **bpf_program__insns()** gives read-only access to BPF program's 31965a7fa2eSAndrii Nakryiko * underlying BPF instructions. 32065a7fa2eSAndrii Nakryiko * @param prog BPF program for which to return instructions 32165a7fa2eSAndrii Nakryiko * @return a pointer to an array of BPF instructions that belong to the 32265a7fa2eSAndrii Nakryiko * specified BPF program 32365a7fa2eSAndrii Nakryiko * 32465a7fa2eSAndrii Nakryiko * Returned pointer is always valid and not NULL. Number of `struct bpf_insn` 32565a7fa2eSAndrii Nakryiko * pointed to can be fetched using **bpf_program__insn_cnt()** API. 32665a7fa2eSAndrii Nakryiko * 32765a7fa2eSAndrii Nakryiko * Keep in mind, libbpf can modify and append/delete BPF program's 32865a7fa2eSAndrii Nakryiko * instructions as it processes BPF object file and prepares everything for 32965a7fa2eSAndrii Nakryiko * uploading into the kernel. So depending on the point in BPF object 33065a7fa2eSAndrii Nakryiko * lifetime, **bpf_program__insns()** can return different sets of 33165a7fa2eSAndrii Nakryiko * instructions. As an example, during BPF object load phase BPF program 33265a7fa2eSAndrii Nakryiko * instructions will be CO-RE-relocated, BPF subprograms instructions will be 33365a7fa2eSAndrii Nakryiko * appended, ldimm64 instructions will have FDs embedded, etc. So instructions 33465a7fa2eSAndrii Nakryiko * returned before **bpf_object__load()** and after it might be quite 33565a7fa2eSAndrii Nakryiko * different. 33665a7fa2eSAndrii Nakryiko */ 33765a7fa2eSAndrii Nakryiko LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog); 338b63b3c49SJiri Olsa 339b63b3c49SJiri Olsa /** 340b63b3c49SJiri Olsa * @brief **bpf_program__set_insns()** can set BPF program's underlying 341b63b3c49SJiri Olsa * BPF instructions. 342b63b3c49SJiri Olsa * 343b63b3c49SJiri Olsa * WARNING: This is a very advanced libbpf API and users need to know 344b63b3c49SJiri Olsa * what they are doing. This should be used from prog_prepare_load_fn 345b63b3c49SJiri Olsa * callback only. 346b63b3c49SJiri Olsa * 347b63b3c49SJiri Olsa * @param prog BPF program for which to return instructions 348b63b3c49SJiri Olsa * @param new_insns a pointer to an array of BPF instructions 349b63b3c49SJiri Olsa * @param new_insn_cnt number of `struct bpf_insn`'s that form 350b63b3c49SJiri Olsa * specified BPF program 351b63b3c49SJiri Olsa * @return 0, on success; negative error code, otherwise 352b63b3c49SJiri Olsa */ 353b63b3c49SJiri Olsa LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog, 354b63b3c49SJiri Olsa struct bpf_insn *new_insns, size_t new_insn_cnt); 355b63b3c49SJiri Olsa 35665a7fa2eSAndrii Nakryiko /** 35765a7fa2eSAndrii Nakryiko * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s 35865a7fa2eSAndrii Nakryiko * that form specified BPF program. 35965a7fa2eSAndrii Nakryiko * @param prog BPF program for which to return number of BPF instructions 36065a7fa2eSAndrii Nakryiko * 36165a7fa2eSAndrii Nakryiko * See **bpf_program__insns()** documentation for notes on how libbpf can 36265a7fa2eSAndrii Nakryiko * change instructions and their count during different phases of 36365a7fa2eSAndrii Nakryiko * **bpf_object** lifetime. 36465a7fa2eSAndrii Nakryiko */ 36565a7fa2eSAndrii Nakryiko LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog); 36665a7fa2eSAndrii Nakryiko 367a324aae3SAndrii Nakryiko LIBBPF_API int bpf_program__fd(const struct bpf_program *prog); 368f742fc68SGrant Seltzer 369f742fc68SGrant Seltzer /** 370f742fc68SGrant Seltzer * @brief **bpf_program__pin()** pins the BPF program to a file 371f742fc68SGrant Seltzer * in the BPF FS specified by a path. This increments the programs 372f742fc68SGrant Seltzer * reference count, allowing it to stay loaded after the process 373f742fc68SGrant Seltzer * which loaded it has exited. 374f742fc68SGrant Seltzer * 375f742fc68SGrant Seltzer * @param prog BPF program to pin, must already be loaded 376f742fc68SGrant Seltzer * @param path file path in a BPF file system 377f742fc68SGrant Seltzer * @return 0, on success; negative error code, otherwise 378f742fc68SGrant Seltzer */ 379ab9e0848SAndrey Ignatov LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path); 380f742fc68SGrant Seltzer 381f742fc68SGrant Seltzer /** 382f742fc68SGrant Seltzer * @brief **bpf_program__unpin()** unpins the BPF program from a file 383f742fc68SGrant Seltzer * in the BPFFS specified by a path. This decrements the programs 384f742fc68SGrant Seltzer * reference count. 385f742fc68SGrant Seltzer * 386f742fc68SGrant Seltzer * The file pinning the BPF program can also be unlinked by a different 387f742fc68SGrant Seltzer * process in which case this function will return an error. 388f742fc68SGrant Seltzer * 389f742fc68SGrant Seltzer * @param prog BPF program to unpin 390f742fc68SGrant Seltzer * @param path file path to the pin in a BPF file system 391f742fc68SGrant Seltzer * @return 0, on success; negative error code, otherwise 392f742fc68SGrant Seltzer */ 3930c19a9fbSStanislav Fomichev LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path); 394ab9e0848SAndrey Ignatov LIBBPF_API void bpf_program__unload(struct bpf_program *prog); 395aa9b1ac3SWang Nan 3961c2e9efcSAndrii Nakryiko struct bpf_link; 3971c2e9efcSAndrii Nakryiko 398c016b68eSAndrii Nakryiko LIBBPF_API struct bpf_link *bpf_link__open(const char *path); 399c016b68eSAndrii Nakryiko LIBBPF_API int bpf_link__fd(const struct bpf_link *link); 400c016b68eSAndrii Nakryiko LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link); 401a66ab9a9SGrant Seltzer /** 402a66ab9a9SGrant Seltzer * @brief **bpf_link__pin()** pins the BPF link to a file 403a66ab9a9SGrant Seltzer * in the BPF FS specified by a path. This increments the links 404a66ab9a9SGrant Seltzer * reference count, allowing it to stay loaded after the process 405a66ab9a9SGrant Seltzer * which loaded it has exited. 406a66ab9a9SGrant Seltzer * 407a66ab9a9SGrant Seltzer * @param link BPF link to pin, must already be loaded 408a66ab9a9SGrant Seltzer * @param path file path in a BPF file system 409a66ab9a9SGrant Seltzer * @return 0, on success; negative error code, otherwise 410a66ab9a9SGrant Seltzer */ 411a66ab9a9SGrant Seltzer 412c016b68eSAndrii Nakryiko LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path); 413a66ab9a9SGrant Seltzer 414a66ab9a9SGrant Seltzer /** 415a66ab9a9SGrant Seltzer * @brief **bpf_link__unpin()** unpins the BPF link from a file 416a66ab9a9SGrant Seltzer * in the BPFFS specified by a path. This decrements the links 417a66ab9a9SGrant Seltzer * reference count. 418a66ab9a9SGrant Seltzer * 419a66ab9a9SGrant Seltzer * The file pinning the BPF link can also be unlinked by a different 420a66ab9a9SGrant Seltzer * process in which case this function will return an error. 421a66ab9a9SGrant Seltzer * 422a66ab9a9SGrant Seltzer * @param prog BPF program to unpin 423a66ab9a9SGrant Seltzer * @param path file path to the pin in a BPF file system 424a66ab9a9SGrant Seltzer * @return 0, on success; negative error code, otherwise 425a66ab9a9SGrant Seltzer */ 426c016b68eSAndrii Nakryiko LIBBPF_API int bpf_link__unpin(struct bpf_link *link); 427cc4f864bSAndrii Nakryiko LIBBPF_API int bpf_link__update_program(struct bpf_link *link, 428cc4f864bSAndrii Nakryiko struct bpf_program *prog); 429d6958706SAndrii Nakryiko LIBBPF_API void bpf_link__disconnect(struct bpf_link *link); 4302e49527eSAndrii Nakryiko LIBBPF_API int bpf_link__detach(struct bpf_link *link); 4311c2e9efcSAndrii Nakryiko LIBBPF_API int bpf_link__destroy(struct bpf_link *link); 4321c2e9efcSAndrii Nakryiko 433a66ab9a9SGrant Seltzer /** 434a66ab9a9SGrant Seltzer * @brief **bpf_program__attach()** is a generic function for attaching 435a66ab9a9SGrant Seltzer * a BPF program based on auto-detection of program type, attach type, 436a66ab9a9SGrant Seltzer * and extra paremeters, where applicable. 437a66ab9a9SGrant Seltzer * 438a66ab9a9SGrant Seltzer * @param prog BPF program to attach 439a66ab9a9SGrant Seltzer * @return Reference to the newly created BPF link; or NULL is returned on error, 440a66ab9a9SGrant Seltzer * error code is stored in errno 441a66ab9a9SGrant Seltzer * 442a66ab9a9SGrant Seltzer * This is supported for: 443a66ab9a9SGrant Seltzer * - kprobe/kretprobe (depends on SEC() definition) 444a66ab9a9SGrant Seltzer * - uprobe/uretprobe (depends on SEC() definition) 445a66ab9a9SGrant Seltzer * - tracepoint 446a66ab9a9SGrant Seltzer * - raw tracepoint 447a66ab9a9SGrant Seltzer * - tracing programs (typed raw TP/fentry/fexit/fmod_ret) 448a66ab9a9SGrant Seltzer */ 44963f2f5eeSAndrii Nakryiko LIBBPF_API struct bpf_link * 450942025c9SAndrii Nakryiko bpf_program__attach(const struct bpf_program *prog); 45147faff37SAndrii Nakryiko 45247faff37SAndrii Nakryiko struct bpf_perf_event_opts { 453f8b299bcSMenglong Dong /* size of this struct, for forward/backward compatibility */ 45447faff37SAndrii Nakryiko size_t sz; 45547faff37SAndrii Nakryiko /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 45647faff37SAndrii Nakryiko __u64 bpf_cookie; 457f8b299bcSMenglong Dong /* don't use BPF link when attach BPF program */ 458f8b299bcSMenglong Dong bool force_ioctl_attach; 459f8b299bcSMenglong Dong size_t :0; 46047faff37SAndrii Nakryiko }; 461f8b299bcSMenglong Dong #define bpf_perf_event_opts__last_field force_ioctl_attach 46247faff37SAndrii Nakryiko 463d7a18ea7SAndrii Nakryiko LIBBPF_API struct bpf_link * 464942025c9SAndrii Nakryiko bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd); 46547faff37SAndrii Nakryiko 46647faff37SAndrii Nakryiko LIBBPF_API struct bpf_link * 467942025c9SAndrii Nakryiko bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd, 46847faff37SAndrii Nakryiko const struct bpf_perf_event_opts *opts); 46947faff37SAndrii Nakryiko 470f8b299bcSMenglong Dong /** 471f8b299bcSMenglong Dong * enum probe_attach_mode - the mode to attach kprobe/uprobe 472f8b299bcSMenglong Dong * 473f8b299bcSMenglong Dong * force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will 474f8b299bcSMenglong Dong * be returned if it is not supported by the kernel. 475f8b299bcSMenglong Dong */ 476f8b299bcSMenglong Dong enum probe_attach_mode { 477f8b299bcSMenglong Dong /* attach probe in latest supported mode by kernel */ 478f8b299bcSMenglong Dong PROBE_ATTACH_MODE_DEFAULT = 0, 479f8b299bcSMenglong Dong /* attach probe in legacy mode, using debugfs/tracefs */ 480f8b299bcSMenglong Dong PROBE_ATTACH_MODE_LEGACY, 481f8b299bcSMenglong Dong /* create perf event with perf_event_open() syscall */ 482f8b299bcSMenglong Dong PROBE_ATTACH_MODE_PERF, 483f8b299bcSMenglong Dong /* attach probe with BPF link */ 484f8b299bcSMenglong Dong PROBE_ATTACH_MODE_LINK, 485f8b299bcSMenglong Dong }; 486f8b299bcSMenglong Dong 48747faff37SAndrii Nakryiko struct bpf_kprobe_opts { 488f8b299bcSMenglong Dong /* size of this struct, for forward/backward compatibility */ 48947faff37SAndrii Nakryiko size_t sz; 49047faff37SAndrii Nakryiko /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 49147faff37SAndrii Nakryiko __u64 bpf_cookie; 49247faff37SAndrii Nakryiko /* function's offset to install kprobe to */ 49346ed5fc3SAndrii Nakryiko size_t offset; 49447faff37SAndrii Nakryiko /* kprobe is return probe */ 49547faff37SAndrii Nakryiko bool retprobe; 496f8b299bcSMenglong Dong /* kprobe attach mode */ 497f8b299bcSMenglong Dong enum probe_attach_mode attach_mode; 49847faff37SAndrii Nakryiko size_t :0; 49947faff37SAndrii Nakryiko }; 500f8b299bcSMenglong Dong #define bpf_kprobe_opts__last_field attach_mode 50147faff37SAndrii Nakryiko 502b2650027SAndrii Nakryiko LIBBPF_API struct bpf_link * 503942025c9SAndrii Nakryiko bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe, 504b2650027SAndrii Nakryiko const char *func_name); 505b2650027SAndrii Nakryiko LIBBPF_API struct bpf_link * 506942025c9SAndrii Nakryiko bpf_program__attach_kprobe_opts(const struct bpf_program *prog, 507da97553eSJiri Olsa const char *func_name, 50847faff37SAndrii Nakryiko const struct bpf_kprobe_opts *opts); 50947faff37SAndrii Nakryiko 510ddc6b049SJiri Olsa struct bpf_kprobe_multi_opts { 511ddc6b049SJiri Olsa /* size of this struct, for forward/backward compatibility */ 512ddc6b049SJiri Olsa size_t sz; 513ddc6b049SJiri Olsa /* array of function symbols to attach */ 514ddc6b049SJiri Olsa const char **syms; 515ddc6b049SJiri Olsa /* array of function addresses to attach */ 516ddc6b049SJiri Olsa const unsigned long *addrs; 517ddc6b049SJiri Olsa /* array of user-provided values fetchable through bpf_get_attach_cookie */ 518ddc6b049SJiri Olsa const __u64 *cookies; 519ddc6b049SJiri Olsa /* number of elements in syms/addrs/cookies arrays */ 520ddc6b049SJiri Olsa size_t cnt; 521ddc6b049SJiri Olsa /* create return kprobes */ 522ddc6b049SJiri Olsa bool retprobe; 523ddc6b049SJiri Olsa size_t :0; 524ddc6b049SJiri Olsa }; 525ddc6b049SJiri Olsa 526ddc6b049SJiri Olsa #define bpf_kprobe_multi_opts__last_field retprobe 527ddc6b049SJiri Olsa 528ddc6b049SJiri Olsa LIBBPF_API struct bpf_link * 529ddc6b049SJiri Olsa bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, 530ddc6b049SJiri Olsa const char *pattern, 531ddc6b049SJiri Olsa const struct bpf_kprobe_multi_opts *opts); 532ddc6b049SJiri Olsa 5333140cf12SJiri Olsa struct bpf_uprobe_multi_opts { 5343140cf12SJiri Olsa /* size of this struct, for forward/backward compatibility */ 5353140cf12SJiri Olsa size_t sz; 5363140cf12SJiri Olsa /* array of function symbols to attach to */ 5373140cf12SJiri Olsa const char **syms; 5383140cf12SJiri Olsa /* array of function addresses to attach to */ 5393140cf12SJiri Olsa const unsigned long *offsets; 5403140cf12SJiri Olsa /* optional, array of associated ref counter offsets */ 5413140cf12SJiri Olsa const unsigned long *ref_ctr_offsets; 5423140cf12SJiri Olsa /* optional, array of associated BPF cookies */ 5433140cf12SJiri Olsa const __u64 *cookies; 5443140cf12SJiri Olsa /* number of elements in syms/addrs/cookies arrays */ 5453140cf12SJiri Olsa size_t cnt; 5463140cf12SJiri Olsa /* create return uprobes */ 5473140cf12SJiri Olsa bool retprobe; 5483140cf12SJiri Olsa size_t :0; 5493140cf12SJiri Olsa }; 5503140cf12SJiri Olsa 5513140cf12SJiri Olsa #define bpf_uprobe_multi_opts__last_field retprobe 5523140cf12SJiri Olsa 5533140cf12SJiri Olsa /** 5543140cf12SJiri Olsa * @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program 5553140cf12SJiri Olsa * to multiple uprobes with uprobe_multi link. 5563140cf12SJiri Olsa * 5573140cf12SJiri Olsa * User can specify 2 mutually exclusive set of inputs: 5583140cf12SJiri Olsa * 5593140cf12SJiri Olsa * 1) use only path/func_pattern/pid arguments 5603140cf12SJiri Olsa * 5613140cf12SJiri Olsa * 2) use path/pid with allowed combinations of 5623140cf12SJiri Olsa * syms/offsets/ref_ctr_offsets/cookies/cnt 5633140cf12SJiri Olsa * 5643140cf12SJiri Olsa * - syms and offsets are mutually exclusive 5653140cf12SJiri Olsa * - ref_ctr_offsets and cookies are optional 5663140cf12SJiri Olsa * 5673140cf12SJiri Olsa * 5683140cf12SJiri Olsa * @param prog BPF program to attach 5693140cf12SJiri Olsa * @param pid Process ID to attach the uprobe to, 0 for self (own process), 5703140cf12SJiri Olsa * -1 for all processes 5713140cf12SJiri Olsa * @param binary_path Path to binary 5723140cf12SJiri Olsa * @param func_pattern Regular expression to specify functions to attach 5733140cf12SJiri Olsa * BPF program to 5743140cf12SJiri Olsa * @param opts Additional options (see **struct bpf_uprobe_multi_opts**) 5753140cf12SJiri Olsa * @return 0, on success; negative error code, otherwise 5763140cf12SJiri Olsa */ 5773140cf12SJiri Olsa LIBBPF_API struct bpf_link * 5783140cf12SJiri Olsa bpf_program__attach_uprobe_multi(const struct bpf_program *prog, 5793140cf12SJiri Olsa pid_t pid, 5803140cf12SJiri Olsa const char *binary_path, 5813140cf12SJiri Olsa const char *func_pattern, 5823140cf12SJiri Olsa const struct bpf_uprobe_multi_opts *opts); 5833140cf12SJiri Olsa 584708ac5beSAndrii Nakryiko struct bpf_ksyscall_opts { 585f8b299bcSMenglong Dong /* size of this struct, for forward/backward compatibility */ 586708ac5beSAndrii Nakryiko size_t sz; 587708ac5beSAndrii Nakryiko /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 588708ac5beSAndrii Nakryiko __u64 bpf_cookie; 589708ac5beSAndrii Nakryiko /* attach as return probe? */ 590708ac5beSAndrii Nakryiko bool retprobe; 591708ac5beSAndrii Nakryiko size_t :0; 592708ac5beSAndrii Nakryiko }; 593708ac5beSAndrii Nakryiko #define bpf_ksyscall_opts__last_field retprobe 594708ac5beSAndrii Nakryiko 595708ac5beSAndrii Nakryiko /** 596708ac5beSAndrii Nakryiko * @brief **bpf_program__attach_ksyscall()** attaches a BPF program 597708ac5beSAndrii Nakryiko * to kernel syscall handler of a specified syscall. Optionally it's possible 598708ac5beSAndrii Nakryiko * to request to install retprobe that will be triggered at syscall exit. It's 599708ac5beSAndrii Nakryiko * also possible to associate BPF cookie (though options). 600708ac5beSAndrii Nakryiko * 601708ac5beSAndrii Nakryiko * Libbpf automatically will determine correct full kernel function name, 602708ac5beSAndrii Nakryiko * which depending on system architecture and kernel version/configuration 603708ac5beSAndrii Nakryiko * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will 604708ac5beSAndrii Nakryiko * attach specified program using kprobe/kretprobe mechanism. 605708ac5beSAndrii Nakryiko * 606708ac5beSAndrii Nakryiko * **bpf_program__attach_ksyscall()** is an API counterpart of declarative 607708ac5beSAndrii Nakryiko * **SEC("ksyscall/<syscall>")** annotation of BPF programs. 608708ac5beSAndrii Nakryiko * 609708ac5beSAndrii Nakryiko * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do 610708ac5beSAndrii Nakryiko * not handle all the calling convention quirks for mmap(), clone() and compat 611708ac5beSAndrii Nakryiko * syscalls. It also only attaches to "native" syscall interfaces. If host 612708ac5beSAndrii Nakryiko * system supports compat syscalls or defines 32-bit syscalls in 64-bit 613708ac5beSAndrii Nakryiko * kernel, such syscall interfaces won't be attached to by libbpf. 614708ac5beSAndrii Nakryiko * 615708ac5beSAndrii Nakryiko * These limitations may or may not change in the future. Therefore it is 616708ac5beSAndrii Nakryiko * recommended to use SEC("kprobe") for these syscalls or if working with 617708ac5beSAndrii Nakryiko * compat and 32-bit interfaces is required. 618708ac5beSAndrii Nakryiko * 619708ac5beSAndrii Nakryiko * @param prog BPF program to attach 620708ac5beSAndrii Nakryiko * @param syscall_name Symbolic name of the syscall (e.g., "bpf") 621708ac5beSAndrii Nakryiko * @param opts Additional options (see **struct bpf_ksyscall_opts**) 622708ac5beSAndrii Nakryiko * @return Reference to the newly created BPF link; or NULL is returned on 623708ac5beSAndrii Nakryiko * error, error code is stored in errno 624708ac5beSAndrii Nakryiko */ 625708ac5beSAndrii Nakryiko LIBBPF_API struct bpf_link * 626708ac5beSAndrii Nakryiko bpf_program__attach_ksyscall(const struct bpf_program *prog, 627708ac5beSAndrii Nakryiko const char *syscall_name, 628708ac5beSAndrii Nakryiko const struct bpf_ksyscall_opts *opts); 629708ac5beSAndrii Nakryiko 63047faff37SAndrii Nakryiko struct bpf_uprobe_opts { 631f8b299bcSMenglong Dong /* size of this struct, for forward/backward compatibility */ 63247faff37SAndrii Nakryiko size_t sz; 6335e3b8356SAndrii Nakryiko /* offset of kernel reference counted USDT semaphore, added in 6345e3b8356SAndrii Nakryiko * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe") 6355e3b8356SAndrii Nakryiko */ 6365e3b8356SAndrii Nakryiko size_t ref_ctr_offset; 63747faff37SAndrii Nakryiko /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 63847faff37SAndrii Nakryiko __u64 bpf_cookie; 63947faff37SAndrii Nakryiko /* uprobe is return probe, invoked at function return time */ 64047faff37SAndrii Nakryiko bool retprobe; 641433966e3SAlan Maguire /* Function name to attach to. Could be an unqualified ("abc") or library-qualified 642433966e3SAlan Maguire * "abc@LIBXYZ" name. To specify function entry, func_name should be set while 643433966e3SAlan Maguire * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an 644433966e3SAlan Maguire * offset within a function, specify func_name and use func_offset argument to specify 645433966e3SAlan Maguire * offset within the function. Shared library functions must specify the shared library 646433966e3SAlan Maguire * binary_path. 647433966e3SAlan Maguire */ 648433966e3SAlan Maguire const char *func_name; 649f8b299bcSMenglong Dong /* uprobe attach mode */ 650f8b299bcSMenglong Dong enum probe_attach_mode attach_mode; 65147faff37SAndrii Nakryiko size_t :0; 65247faff37SAndrii Nakryiko }; 653f8b299bcSMenglong Dong #define bpf_uprobe_opts__last_field attach_mode 65447faff37SAndrii Nakryiko 655d5284dedSGrant Seltzer /** 656d5284dedSGrant Seltzer * @brief **bpf_program__attach_uprobe()** attaches a BPF program 657d5284dedSGrant Seltzer * to the userspace function which is found by binary path and 658d5284dedSGrant Seltzer * offset. You can optionally specify a particular proccess to attach 659d5284dedSGrant Seltzer * to. You can also optionally attach the program to the function 660d5284dedSGrant Seltzer * exit instead of entry. 661d5284dedSGrant Seltzer * 662d5284dedSGrant Seltzer * @param prog BPF program to attach 663d5284dedSGrant Seltzer * @param retprobe Attach to function exit 664d5284dedSGrant Seltzer * @param pid Process ID to attach the uprobe to, 0 for self (own process), 665d5284dedSGrant Seltzer * -1 for all processes 666d5284dedSGrant Seltzer * @param binary_path Path to binary that contains the function symbol 667d5284dedSGrant Seltzer * @param func_offset Offset within the binary of the function symbol 668d5284dedSGrant Seltzer * @return Reference to the newly created BPF link; or NULL is returned on error, 669d5284dedSGrant Seltzer * error code is stored in errno 670d5284dedSGrant Seltzer */ 671da97553eSJiri Olsa LIBBPF_API struct bpf_link * 672942025c9SAndrii Nakryiko bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe, 673b2650027SAndrii Nakryiko pid_t pid, const char *binary_path, 674b2650027SAndrii Nakryiko size_t func_offset); 675d5284dedSGrant Seltzer 676d5284dedSGrant Seltzer /** 677d5284dedSGrant Seltzer * @brief **bpf_program__attach_uprobe_opts()** is just like 678d5284dedSGrant Seltzer * bpf_program__attach_uprobe() except with a options struct 679d5284dedSGrant Seltzer * for various configurations. 680d5284dedSGrant Seltzer * 681d5284dedSGrant Seltzer * @param prog BPF program to attach 682d5284dedSGrant Seltzer * @param pid Process ID to attach the uprobe to, 0 for self (own process), 683d5284dedSGrant Seltzer * -1 for all processes 684d5284dedSGrant Seltzer * @param binary_path Path to binary that contains the function symbol 685d5284dedSGrant Seltzer * @param func_offset Offset within the binary of the function symbol 686d5284dedSGrant Seltzer * @param opts Options for altering program attachment 687d5284dedSGrant Seltzer * @return Reference to the newly created BPF link; or NULL is returned on error, 688d5284dedSGrant Seltzer * error code is stored in errno 689d5284dedSGrant Seltzer */ 690f6de59c1SAndrii Nakryiko LIBBPF_API struct bpf_link * 691942025c9SAndrii Nakryiko bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, 69247faff37SAndrii Nakryiko const char *binary_path, size_t func_offset, 69347faff37SAndrii Nakryiko const struct bpf_uprobe_opts *opts); 69447faff37SAndrii Nakryiko 6952e4913e0SAndrii Nakryiko struct bpf_usdt_opts { 6962e4913e0SAndrii Nakryiko /* size of this struct, for forward/backward compatibility */ 6972e4913e0SAndrii Nakryiko size_t sz; 6982e4913e0SAndrii Nakryiko /* custom user-provided value accessible through usdt_cookie() */ 6992e4913e0SAndrii Nakryiko __u64 usdt_cookie; 7002e4913e0SAndrii Nakryiko size_t :0; 7012e4913e0SAndrii Nakryiko }; 7022e4913e0SAndrii Nakryiko #define bpf_usdt_opts__last_field usdt_cookie 7032e4913e0SAndrii Nakryiko 7042e4913e0SAndrii Nakryiko /** 7052e4913e0SAndrii Nakryiko * @brief **bpf_program__attach_usdt()** is just like 7062e4913e0SAndrii Nakryiko * bpf_program__attach_uprobe_opts() except it covers USDT (User-space 7072e4913e0SAndrii Nakryiko * Statically Defined Tracepoint) attachment, instead of attaching to 7082e4913e0SAndrii Nakryiko * user-space function entry or exit. 7092e4913e0SAndrii Nakryiko * 7102e4913e0SAndrii Nakryiko * @param prog BPF program to attach 7112e4913e0SAndrii Nakryiko * @param pid Process ID to attach the uprobe to, 0 for self (own process), 7122e4913e0SAndrii Nakryiko * -1 for all processes 7132e4913e0SAndrii Nakryiko * @param binary_path Path to binary that contains provided USDT probe 7142e4913e0SAndrii Nakryiko * @param usdt_provider USDT provider name 7152e4913e0SAndrii Nakryiko * @param usdt_name USDT probe name 7162e4913e0SAndrii Nakryiko * @param opts Options for altering program attachment 7172e4913e0SAndrii Nakryiko * @return Reference to the newly created BPF link; or NULL is returned on error, 7182e4913e0SAndrii Nakryiko * error code is stored in errno 7192e4913e0SAndrii Nakryiko */ 7202e4913e0SAndrii Nakryiko LIBBPF_API struct bpf_link * 7212e4913e0SAndrii Nakryiko bpf_program__attach_usdt(const struct bpf_program *prog, 7222e4913e0SAndrii Nakryiko pid_t pid, const char *binary_path, 7232e4913e0SAndrii Nakryiko const char *usdt_provider, const char *usdt_name, 7242e4913e0SAndrii Nakryiko const struct bpf_usdt_opts *opts); 7252e4913e0SAndrii Nakryiko 72647faff37SAndrii Nakryiko struct bpf_tracepoint_opts { 727f8b299bcSMenglong Dong /* size of this struct, for forward/backward compatibility */ 72847faff37SAndrii Nakryiko size_t sz; 72947faff37SAndrii Nakryiko /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 73047faff37SAndrii Nakryiko __u64 bpf_cookie; 73147faff37SAndrii Nakryiko }; 73247faff37SAndrii Nakryiko #define bpf_tracepoint_opts__last_field bpf_cookie 73347faff37SAndrii Nakryiko 73447faff37SAndrii Nakryiko LIBBPF_API struct bpf_link * 735942025c9SAndrii Nakryiko bpf_program__attach_tracepoint(const struct bpf_program *prog, 736f6de59c1SAndrii Nakryiko const char *tp_category, 737f6de59c1SAndrii Nakryiko const char *tp_name); 73884bf5e1fSAndrii Nakryiko LIBBPF_API struct bpf_link * 739942025c9SAndrii Nakryiko bpf_program__attach_tracepoint_opts(const struct bpf_program *prog, 74047faff37SAndrii Nakryiko const char *tp_category, 74147faff37SAndrii Nakryiko const char *tp_name, 74247faff37SAndrii Nakryiko const struct bpf_tracepoint_opts *opts); 74347faff37SAndrii Nakryiko 74447faff37SAndrii Nakryiko LIBBPF_API struct bpf_link * 745942025c9SAndrii Nakryiko bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, 74684bf5e1fSAndrii Nakryiko const char *tp_name); 747129b9c5eSKui-Feng Lee 748129b9c5eSKui-Feng Lee struct bpf_trace_opts { 749129b9c5eSKui-Feng Lee /* size of this struct, for forward/backward compatibility */ 750129b9c5eSKui-Feng Lee size_t sz; 751129b9c5eSKui-Feng Lee /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 752129b9c5eSKui-Feng Lee __u64 cookie; 753129b9c5eSKui-Feng Lee }; 754129b9c5eSKui-Feng Lee #define bpf_trace_opts__last_field cookie 755129b9c5eSKui-Feng Lee 756b8c54ea4SAlexei Starovoitov LIBBPF_API struct bpf_link * 757942025c9SAndrii Nakryiko bpf_program__attach_trace(const struct bpf_program *prog); 7581e092a03SKP Singh LIBBPF_API struct bpf_link * 759129b9c5eSKui-Feng Lee bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts); 760129b9c5eSKui-Feng Lee 761129b9c5eSKui-Feng Lee LIBBPF_API struct bpf_link * 762942025c9SAndrii Nakryiko bpf_program__attach_lsm(const struct bpf_program *prog); 763cc4f864bSAndrii Nakryiko LIBBPF_API struct bpf_link * 764942025c9SAndrii Nakryiko bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd); 765d60d81acSJakub Sitnicki LIBBPF_API struct bpf_link * 766942025c9SAndrii Nakryiko bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd); 767dc8698caSAndrii Nakryiko LIBBPF_API struct bpf_link * 768942025c9SAndrii Nakryiko bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex); 769a5359091SToke Høiland-Jørgensen LIBBPF_API struct bpf_link * 770942025c9SAndrii Nakryiko bpf_program__attach_freplace(const struct bpf_program *prog, 771a5359091SToke Høiland-Jørgensen int target_fd, const char *attach_func_name); 772cc4f864bSAndrii Nakryiko 77352364abbSFlorian Westphal struct bpf_netfilter_opts { 77452364abbSFlorian Westphal /* size of this struct, for forward/backward compatibility */ 77552364abbSFlorian Westphal size_t sz; 77652364abbSFlorian Westphal 77752364abbSFlorian Westphal __u32 pf; 77852364abbSFlorian Westphal __u32 hooknum; 77952364abbSFlorian Westphal __s32 priority; 78052364abbSFlorian Westphal __u32 flags; 78152364abbSFlorian Westphal }; 78252364abbSFlorian Westphal #define bpf_netfilter_opts__last_field flags 78352364abbSFlorian Westphal 78452364abbSFlorian Westphal LIBBPF_API struct bpf_link * 78552364abbSFlorian Westphal bpf_program__attach_netfilter(const struct bpf_program *prog, 78652364abbSFlorian Westphal const struct bpf_netfilter_opts *opts); 78752364abbSFlorian Westphal 78855cc3768SDaniel Borkmann struct bpf_tcx_opts { 78955cc3768SDaniel Borkmann /* size of this struct, for forward/backward compatibility */ 79055cc3768SDaniel Borkmann size_t sz; 79155cc3768SDaniel Borkmann __u32 flags; 79255cc3768SDaniel Borkmann __u32 relative_fd; 79355cc3768SDaniel Borkmann __u32 relative_id; 79455cc3768SDaniel Borkmann __u64 expected_revision; 79555cc3768SDaniel Borkmann size_t :0; 79655cc3768SDaniel Borkmann }; 79755cc3768SDaniel Borkmann #define bpf_tcx_opts__last_field expected_revision 79855cc3768SDaniel Borkmann 79955cc3768SDaniel Borkmann LIBBPF_API struct bpf_link * 80055cc3768SDaniel Borkmann bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex, 80155cc3768SDaniel Borkmann const struct bpf_tcx_opts *opts); 80255cc3768SDaniel Borkmann 803590a0088SMartin KaFai Lau struct bpf_map; 804cc4f864bSAndrii Nakryiko 805942025c9SAndrii Nakryiko LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map); 806912dd4b0SKui-Feng Lee LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map); 807cc4f864bSAndrii Nakryiko 808c09add2fSYonghong Song struct bpf_iter_attach_opts { 809c09add2fSYonghong Song size_t sz; /* size of this struct for forward/backward compatibility */ 81074fc097dSYonghong Song union bpf_iter_link_info *link_info; 81174fc097dSYonghong Song __u32 link_info_len; 812c09add2fSYonghong Song }; 81374fc097dSYonghong Song #define bpf_iter_attach_opts__last_field link_info_len 814c09add2fSYonghong Song 815c09add2fSYonghong Song LIBBPF_API struct bpf_link * 816942025c9SAndrii Nakryiko bpf_program__attach_iter(const struct bpf_program *prog, 817c09add2fSYonghong Song const struct bpf_iter_attach_opts *opts); 818c09add2fSYonghong Song 81920eccf29SAndrii Nakryiko LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog); 820a66ab9a9SGrant Seltzer 821a66ab9a9SGrant Seltzer /** 822a66ab9a9SGrant Seltzer * @brief **bpf_program__set_type()** sets the program 823a66ab9a9SGrant Seltzer * type of the passed BPF program. 824a66ab9a9SGrant Seltzer * @param prog BPF program to set the program type for 825a66ab9a9SGrant Seltzer * @param type program type to set the BPF map to have 826a66ab9a9SGrant Seltzer * @return error code; or 0 if no error. An error occurs 827a66ab9a9SGrant Seltzer * if the object is already loaded. 828a66ab9a9SGrant Seltzer * 829a66ab9a9SGrant Seltzer * This must be called before the BPF object is loaded, 830a66ab9a9SGrant Seltzer * otherwise it has no effect and an error is returned. 831a66ab9a9SGrant Seltzer */ 83293442f13SGrant Seltzer LIBBPF_API int bpf_program__set_type(struct bpf_program *prog, 833ab9e0848SAndrey Ignatov enum bpf_prog_type type); 834f1eead9eSAndrii Nakryiko 835f1eead9eSAndrii Nakryiko LIBBPF_API enum bpf_attach_type 83620eccf29SAndrii Nakryiko bpf_program__expected_attach_type(const struct bpf_program *prog); 837a66ab9a9SGrant Seltzer 838a66ab9a9SGrant Seltzer /** 839a66ab9a9SGrant Seltzer * @brief **bpf_program__set_expected_attach_type()** sets the 840a66ab9a9SGrant Seltzer * attach type of the passed BPF program. This is used for 841a66ab9a9SGrant Seltzer * auto-detection of attachment when programs are loaded. 842a66ab9a9SGrant Seltzer * @param prog BPF program to set the attach type for 843a66ab9a9SGrant Seltzer * @param type attach type to set the BPF map to have 844a66ab9a9SGrant Seltzer * @return error code; or 0 if no error. An error occurs 845a66ab9a9SGrant Seltzer * if the object is already loaded. 846a66ab9a9SGrant Seltzer * 847a66ab9a9SGrant Seltzer * This must be called before the BPF object is loaded, 848a66ab9a9SGrant Seltzer * otherwise it has no effect and an error is returned. 849a66ab9a9SGrant Seltzer */ 85093442f13SGrant Seltzer LIBBPF_API int 851ab9e0848SAndrey Ignatov bpf_program__set_expected_attach_type(struct bpf_program *prog, 85216962b24SJohn Fastabend enum bpf_attach_type type); 8535f44e4c8SWang Nan 854a6ca7158SAndrii Nakryiko LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog); 8558cccee9eSFlorent Revest LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags); 856b3ce9079SAndrii Nakryiko 857b3ce9079SAndrii Nakryiko /* Per-program log level and log buffer getters/setters. 858b3ce9079SAndrii Nakryiko * See bpf_object_open_opts comments regarding log_level and log_buf 859b3ce9079SAndrii Nakryiko * interactions. 860b3ce9079SAndrii Nakryiko */ 861dbdd2c7fSAndrii Nakryiko LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog); 862dbdd2c7fSAndrii Nakryiko LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level); 863b3ce9079SAndrii Nakryiko LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size); 864b3ce9079SAndrii Nakryiko LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size); 865a6ca7158SAndrii Nakryiko 866a66ab9a9SGrant Seltzer /** 867a66ab9a9SGrant Seltzer * @brief **bpf_program__set_attach_target()** sets BTF-based attach target 868a66ab9a9SGrant Seltzer * for supported BPF program types: 869a66ab9a9SGrant Seltzer * - BTF-aware raw tracepoints (tp_btf); 870a66ab9a9SGrant Seltzer * - fentry/fexit/fmod_ret; 871a66ab9a9SGrant Seltzer * - lsm; 872a66ab9a9SGrant Seltzer * - freplace. 873a66ab9a9SGrant Seltzer * @param prog BPF program to set the attach type for 874a66ab9a9SGrant Seltzer * @param type attach type to set the BPF map to have 875a66ab9a9SGrant Seltzer * @return error code; or 0 if no error occurred. 876a66ab9a9SGrant Seltzer */ 877ff26ce5cSEelco Chaudron LIBBPF_API int 878ff26ce5cSEelco Chaudron bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd, 879ff26ce5cSEelco Chaudron const char *attach_func_name); 880ff26ce5cSEelco Chaudron 88197c140d9SGrant Seltzer /** 88297c140d9SGrant Seltzer * @brief **bpf_object__find_map_by_name()** returns BPF map of 88397c140d9SGrant Seltzer * the given name, if it exists within the passed BPF object 88497c140d9SGrant Seltzer * @param obj BPF object 88597c140d9SGrant Seltzer * @param name name of the BPF map 88697c140d9SGrant Seltzer * @return BPF map instance, if such map exists within the BPF object; 88797c140d9SGrant Seltzer * or NULL otherwise. 8889d759a9bSWang Nan */ 889ab9e0848SAndrey Ignatov LIBBPF_API struct bpf_map * 890a324aae3SAndrii Nakryiko bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name); 8919d759a9bSWang Nan 892f3cea32dSMaciej Fijalkowski LIBBPF_API int 893a324aae3SAndrii Nakryiko bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name); 894f3cea32dSMaciej Fijalkowski 895ab9e0848SAndrey Ignatov LIBBPF_API struct bpf_map * 8962088a3a7SHengqi Chen bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map); 8972088a3a7SHengqi Chen 898f74a53d9SJakub Kicinski #define bpf_object__for_each_map(pos, obj) \ 8992088a3a7SHengqi Chen for ((pos) = bpf_object__next_map((obj), NULL); \ 9009d759a9bSWang Nan (pos) != NULL; \ 9012088a3a7SHengqi Chen (pos) = bpf_object__next_map((obj), (pos))) 902f74a53d9SJakub Kicinski #define bpf_map__for_each bpf_object__for_each_map 9039d759a9bSWang Nan 9040c19a9fbSStanislav Fomichev LIBBPF_API struct bpf_map * 9052088a3a7SHengqi Chen bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map); 9060c19a9fbSStanislav Fomichev 90797c140d9SGrant Seltzer /** 908ec41817bSAndrii Nakryiko * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create 909ec41817bSAndrii Nakryiko * BPF map during BPF object load phase. 910ec41817bSAndrii Nakryiko * @param map the BPF map instance 911ec41817bSAndrii Nakryiko * @param autocreate whether to create BPF map during BPF object load 912ec41817bSAndrii Nakryiko * @return 0 on success; -EBUSY if BPF object was already loaded 913ec41817bSAndrii Nakryiko * 914ec41817bSAndrii Nakryiko * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating 915ec41817bSAndrii Nakryiko * BPF map. By default, libbpf will attempt to create every single BPF map 916ec41817bSAndrii Nakryiko * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall 917ec41817bSAndrii Nakryiko * and fill in map FD in BPF instructions. 918ec41817bSAndrii Nakryiko * 919ec41817bSAndrii Nakryiko * This API allows to opt-out of this process for specific map instance. This 920ec41817bSAndrii Nakryiko * can be useful if host kernel doesn't support such BPF map type or used 921ec41817bSAndrii Nakryiko * combination of flags and user application wants to avoid creating such 922ec41817bSAndrii Nakryiko * a map in the first place. User is still responsible to make sure that their 923ec41817bSAndrii Nakryiko * BPF-side code that expects to use such missing BPF map is recognized by BPF 924ec41817bSAndrii Nakryiko * verifier as dead code, otherwise BPF verifier will reject such BPF program. 925ec41817bSAndrii Nakryiko */ 926ec41817bSAndrii Nakryiko LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate); 927ec41817bSAndrii Nakryiko LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map); 928ec41817bSAndrii Nakryiko 929ec41817bSAndrii Nakryiko /** 93097c140d9SGrant Seltzer * @brief **bpf_map__fd()** gets the file descriptor of the passed 93197c140d9SGrant Seltzer * BPF map 93297c140d9SGrant Seltzer * @param map the BPF map instance 93397c140d9SGrant Seltzer * @return the file descriptor; or -EINVAL in case of an error 93497c140d9SGrant Seltzer */ 935a324aae3SAndrii Nakryiko LIBBPF_API int bpf_map__fd(const struct bpf_map *map); 9361bdb6c9aSAndrii Nakryiko LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd); 9371bdb6c9aSAndrii Nakryiko /* get map name */ 938a324aae3SAndrii Nakryiko LIBBPF_API const char *bpf_map__name(const struct bpf_map *map); 9391bdb6c9aSAndrii Nakryiko /* get/set map type */ 9401bdb6c9aSAndrii Nakryiko LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map); 9411bdb6c9aSAndrii Nakryiko LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type); 9421bdb6c9aSAndrii Nakryiko /* get/set map size (max_entries) */ 9431bdb6c9aSAndrii Nakryiko LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map); 9441bdb6c9aSAndrii Nakryiko LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries); 9451bdb6c9aSAndrii Nakryiko /* get/set map flags */ 9461bdb6c9aSAndrii Nakryiko LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map); 9471bdb6c9aSAndrii Nakryiko LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags); 9481bdb6c9aSAndrii Nakryiko /* get/set map NUMA node */ 9491bdb6c9aSAndrii Nakryiko LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map); 9501bdb6c9aSAndrii Nakryiko LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node); 9511bdb6c9aSAndrii Nakryiko /* get/set map key size */ 9521bdb6c9aSAndrii Nakryiko LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map); 9531bdb6c9aSAndrii Nakryiko LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size); 9549d0a2331SJP Kobryn /* get map value size */ 9551bdb6c9aSAndrii Nakryiko LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map); 9569d0a2331SJP Kobryn /** 9579d0a2331SJP Kobryn * @brief **bpf_map__set_value_size()** sets map value size. 9589d0a2331SJP Kobryn * @param map the BPF map instance 9599d0a2331SJP Kobryn * @return 0, on success; negative error, otherwise 9609d0a2331SJP Kobryn * 9619d0a2331SJP Kobryn * There is a special case for maps with associated memory-mapped regions, like 9629d0a2331SJP Kobryn * the global data section maps (bss, data, rodata). When this function is used 9639d0a2331SJP Kobryn * on such a map, the mapped region is resized. Afterward, an attempt is made to 9649d0a2331SJP Kobryn * adjust the corresponding BTF info. This attempt is best-effort and can only 9659d0a2331SJP Kobryn * succeed if the last variable of the data section map is an array. The array 9669d0a2331SJP Kobryn * BTF type is replaced by a new BTF array type with a different length. 9679d0a2331SJP Kobryn * Any previously existing pointers returned from bpf_map__initial_value() or 9689d0a2331SJP Kobryn * corresponding data section skeleton pointer must be reinitialized. 9699d0a2331SJP Kobryn */ 9701bdb6c9aSAndrii Nakryiko LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size); 9711bdb6c9aSAndrii Nakryiko /* get map key/value BTF type IDs */ 972ab9e0848SAndrey Ignatov LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map); 973ab9e0848SAndrey Ignatov LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map); 9741bdb6c9aSAndrii Nakryiko /* get/set map if_index */ 9751bdb6c9aSAndrii Nakryiko LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map); 9761bdb6c9aSAndrii Nakryiko LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); 97747512102SJoanne Koong /* get/set map map_extra flags */ 97847512102SJoanne Koong LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map); 97947512102SJoanne Koong LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra); 9809d759a9bSWang Nan 981e2842be5SToke Høiland-Jørgensen LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map, 982e2842be5SToke Høiland-Jørgensen const void *data, size_t size); 9839d0a2331SJP Kobryn LIBBPF_API void *bpf_map__initial_value(struct bpf_map *map, size_t *psize); 98497c140d9SGrant Seltzer 98597c140d9SGrant Seltzer /** 98697c140d9SGrant Seltzer * @brief **bpf_map__is_internal()** tells the caller whether or not the 98797c140d9SGrant Seltzer * passed map is a special map created by libbpf automatically for things like 98897c140d9SGrant Seltzer * global variables, __ksym externs, Kconfig values, etc 98997c140d9SGrant Seltzer * @param map the bpf_map 99097c140d9SGrant Seltzer * @return true, if the map is an internal map; false, otherwise 99197c140d9SGrant Seltzer */ 992a324aae3SAndrii Nakryiko LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map); 993e4ce876fSGrant Seltzer 994e4ce876fSGrant Seltzer /** 995e4ce876fSGrant Seltzer * @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the 996e4ce876fSGrant Seltzer * BPF map should be pinned. This does not actually create the 'pin'. 997e4ce876fSGrant Seltzer * @param map The bpf_map 998e4ce876fSGrant Seltzer * @param path The path 999e4ce876fSGrant Seltzer * @return 0, on success; negative error, otherwise 1000e4ce876fSGrant Seltzer */ 10014580b25fSToke Høiland-Jørgensen LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path); 1002e4ce876fSGrant Seltzer 1003e4ce876fSGrant Seltzer /** 1004e4ce876fSGrant Seltzer * @brief **bpf_map__pin_path()** gets the path attribute that tells where the 1005e4ce876fSGrant Seltzer * BPF map should be pinned. 1006e4ce876fSGrant Seltzer * @param map The bpf_map 1007e4ce876fSGrant Seltzer * @return The path string; which can be NULL 1008e4ce876fSGrant Seltzer */ 1009e244d34dSEvgeniy Litvinenko LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map); 1010e4ce876fSGrant Seltzer 1011e4ce876fSGrant Seltzer /** 1012e4ce876fSGrant Seltzer * @brief **bpf_map__is_pinned()** tells the caller whether or not the 1013e4ce876fSGrant Seltzer * passed map has been pinned via a 'pin' file. 1014e4ce876fSGrant Seltzer * @param map The bpf_map 1015e4ce876fSGrant Seltzer * @return true, if the map is pinned; false, otherwise 1016e4ce876fSGrant Seltzer */ 10174580b25fSToke Høiland-Jørgensen LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map); 1018e4ce876fSGrant Seltzer 1019e4ce876fSGrant Seltzer /** 1020e4ce876fSGrant Seltzer * @brief **bpf_map__pin()** creates a file that serves as a 'pin' 1021e4ce876fSGrant Seltzer * for the BPF map. This increments the reference count on the 1022e4ce876fSGrant Seltzer * BPF map which will keep the BPF map loaded even after the 1023e4ce876fSGrant Seltzer * userspace process which loaded it has exited. 1024e4ce876fSGrant Seltzer * @param map The bpf_map to pin 1025e4ce876fSGrant Seltzer * @param path A file path for the 'pin' 1026e4ce876fSGrant Seltzer * @return 0, on success; negative error, otherwise 1027e4ce876fSGrant Seltzer * 1028e4ce876fSGrant Seltzer * If `path` is NULL the maps `pin_path` attribute will be used. If this is 1029e4ce876fSGrant Seltzer * also NULL, an error will be returned and the map will not be pinned. 1030e4ce876fSGrant Seltzer */ 1031ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path); 1032e4ce876fSGrant Seltzer 1033e4ce876fSGrant Seltzer /** 1034e4ce876fSGrant Seltzer * @brief **bpf_map__unpin()** removes the file that serves as a 1035e4ce876fSGrant Seltzer * 'pin' for the BPF map. 1036e4ce876fSGrant Seltzer * @param map The bpf_map to unpin 1037e4ce876fSGrant Seltzer * @param path A file path for the 'pin' 1038e4ce876fSGrant Seltzer * @return 0, on success; negative error, otherwise 1039e4ce876fSGrant Seltzer * 1040e4ce876fSGrant Seltzer * The `path` parameter can be NULL, in which case the `pin_path` 1041e4ce876fSGrant Seltzer * map attribute is unpinned. If both the `path` parameter and 1042e4ce876fSGrant Seltzer * `pin_path` map attribute are set, they must be equal. 1043e4ce876fSGrant Seltzer */ 10440c19a9fbSStanislav Fomichev LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path); 10459d759a9bSWang Nan 1046addb9fc9SNikita V. Shirokov LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd); 1047b3278099SAndrii Nakryiko LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map); 1048addb9fc9SNikita V. Shirokov 104997c140d9SGrant Seltzer /** 1050737d0646SAndrii Nakryiko * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value 1051737d0646SAndrii Nakryiko * corresponding to provided key. 1052737d0646SAndrii Nakryiko * @param map BPF map to lookup element in 1053737d0646SAndrii Nakryiko * @param key pointer to memory containing bytes of the key used for lookup 1054737d0646SAndrii Nakryiko * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1055737d0646SAndrii Nakryiko * @param value pointer to memory in which looked up value will be stored 1056737d0646SAndrii Nakryiko * @param value_sz size in byte of value data memory; it has to match BPF map 1057737d0646SAndrii Nakryiko * definition's **value_size**. For per-CPU BPF maps value size has to be 1058737d0646SAndrii Nakryiko * a product of BPF map value size and number of possible CPUs in the system 1059737d0646SAndrii Nakryiko * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for 1060737d0646SAndrii Nakryiko * per-CPU values value size has to be aligned up to closest 8 bytes for 1061737d0646SAndrii Nakryiko * alignment reasons, so expected size is: `round_up(value_size, 8) 1062737d0646SAndrii Nakryiko * * libbpf_num_possible_cpus()`. 1063737d0646SAndrii Nakryiko * @flags extra flags passed to kernel for this operation 1064737d0646SAndrii Nakryiko * @return 0, on success; negative error, otherwise 1065737d0646SAndrii Nakryiko * 1066737d0646SAndrii Nakryiko * **bpf_map__lookup_elem()** is high-level equivalent of 1067737d0646SAndrii Nakryiko * **bpf_map_lookup_elem()** API with added check for key and value size. 1068737d0646SAndrii Nakryiko */ 1069737d0646SAndrii Nakryiko LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map, 1070737d0646SAndrii Nakryiko const void *key, size_t key_sz, 1071737d0646SAndrii Nakryiko void *value, size_t value_sz, __u64 flags); 1072737d0646SAndrii Nakryiko 1073737d0646SAndrii Nakryiko /** 1074737d0646SAndrii Nakryiko * @brief **bpf_map__update_elem()** allows to insert or update value in BPF 1075737d0646SAndrii Nakryiko * map that corresponds to provided key. 1076737d0646SAndrii Nakryiko * @param map BPF map to insert to or update element in 1077737d0646SAndrii Nakryiko * @param key pointer to memory containing bytes of the key 1078737d0646SAndrii Nakryiko * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1079737d0646SAndrii Nakryiko * @param value pointer to memory containing bytes of the value 1080737d0646SAndrii Nakryiko * @param value_sz size in byte of value data memory; it has to match BPF map 1081737d0646SAndrii Nakryiko * definition's **value_size**. For per-CPU BPF maps value size has to be 1082737d0646SAndrii Nakryiko * a product of BPF map value size and number of possible CPUs in the system 1083737d0646SAndrii Nakryiko * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for 1084737d0646SAndrii Nakryiko * per-CPU values value size has to be aligned up to closest 8 bytes for 1085737d0646SAndrii Nakryiko * alignment reasons, so expected size is: `round_up(value_size, 8) 1086737d0646SAndrii Nakryiko * * libbpf_num_possible_cpus()`. 1087737d0646SAndrii Nakryiko * @flags extra flags passed to kernel for this operation 1088737d0646SAndrii Nakryiko * @return 0, on success; negative error, otherwise 1089737d0646SAndrii Nakryiko * 1090737d0646SAndrii Nakryiko * **bpf_map__update_elem()** is high-level equivalent of 1091737d0646SAndrii Nakryiko * **bpf_map_update_elem()** API with added check for key and value size. 1092737d0646SAndrii Nakryiko */ 1093737d0646SAndrii Nakryiko LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map, 1094737d0646SAndrii Nakryiko const void *key, size_t key_sz, 1095737d0646SAndrii Nakryiko const void *value, size_t value_sz, __u64 flags); 1096737d0646SAndrii Nakryiko 1097737d0646SAndrii Nakryiko /** 1098737d0646SAndrii Nakryiko * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that 1099737d0646SAndrii Nakryiko * corresponds to provided key. 1100737d0646SAndrii Nakryiko * @param map BPF map to delete element from 1101737d0646SAndrii Nakryiko * @param key pointer to memory containing bytes of the key 1102737d0646SAndrii Nakryiko * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1103737d0646SAndrii Nakryiko * @flags extra flags passed to kernel for this operation 1104737d0646SAndrii Nakryiko * @return 0, on success; negative error, otherwise 1105737d0646SAndrii Nakryiko * 1106737d0646SAndrii Nakryiko * **bpf_map__delete_elem()** is high-level equivalent of 1107737d0646SAndrii Nakryiko * **bpf_map_delete_elem()** API with added check for key size. 1108737d0646SAndrii Nakryiko */ 1109737d0646SAndrii Nakryiko LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map, 1110737d0646SAndrii Nakryiko const void *key, size_t key_sz, __u64 flags); 1111737d0646SAndrii Nakryiko 1112737d0646SAndrii Nakryiko /** 1113737d0646SAndrii Nakryiko * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value 1114737d0646SAndrii Nakryiko * corresponding to provided key and atomically delete it afterwards. 1115737d0646SAndrii Nakryiko * @param map BPF map to lookup element in 1116737d0646SAndrii Nakryiko * @param key pointer to memory containing bytes of the key used for lookup 1117737d0646SAndrii Nakryiko * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1118737d0646SAndrii Nakryiko * @param value pointer to memory in which looked up value will be stored 1119737d0646SAndrii Nakryiko * @param value_sz size in byte of value data memory; it has to match BPF map 1120737d0646SAndrii Nakryiko * definition's **value_size**. For per-CPU BPF maps value size has to be 1121737d0646SAndrii Nakryiko * a product of BPF map value size and number of possible CPUs in the system 1122737d0646SAndrii Nakryiko * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for 1123737d0646SAndrii Nakryiko * per-CPU values value size has to be aligned up to closest 8 bytes for 1124737d0646SAndrii Nakryiko * alignment reasons, so expected size is: `round_up(value_size, 8) 1125737d0646SAndrii Nakryiko * * libbpf_num_possible_cpus()`. 1126737d0646SAndrii Nakryiko * @flags extra flags passed to kernel for this operation 1127737d0646SAndrii Nakryiko * @return 0, on success; negative error, otherwise 1128737d0646SAndrii Nakryiko * 1129737d0646SAndrii Nakryiko * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of 1130737d0646SAndrii Nakryiko * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size. 1131737d0646SAndrii Nakryiko */ 1132737d0646SAndrii Nakryiko LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map, 1133737d0646SAndrii Nakryiko const void *key, size_t key_sz, 1134737d0646SAndrii Nakryiko void *value, size_t value_sz, __u64 flags); 1135737d0646SAndrii Nakryiko 1136737d0646SAndrii Nakryiko /** 1137737d0646SAndrii Nakryiko * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by 1138737d0646SAndrii Nakryiko * fetching next key that follows current key. 1139737d0646SAndrii Nakryiko * @param map BPF map to fetch next key from 1140737d0646SAndrii Nakryiko * @param cur_key pointer to memory containing bytes of current key or NULL to 1141737d0646SAndrii Nakryiko * fetch the first key 1142737d0646SAndrii Nakryiko * @param next_key pointer to memory to write next key into 1143737d0646SAndrii Nakryiko * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1144737d0646SAndrii Nakryiko * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map; 1145737d0646SAndrii Nakryiko * negative error, otherwise 1146737d0646SAndrii Nakryiko * 1147737d0646SAndrii Nakryiko * **bpf_map__get_next_key()** is high-level equivalent of 1148737d0646SAndrii Nakryiko * **bpf_map_get_next_key()** API with added check for key size. 1149737d0646SAndrii Nakryiko */ 1150737d0646SAndrii Nakryiko LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map, 1151737d0646SAndrii Nakryiko const void *cur_key, void *next_key, size_t key_sz); 1152737d0646SAndrii Nakryiko 1153bd5ca3efSToke Høiland-Jørgensen struct bpf_xdp_set_link_opts { 1154bd5ca3efSToke Høiland-Jørgensen size_t sz; 115549b452c3SToke Høiland-Jørgensen int old_fd; 1156dde7b3f5SAndrii Nakryiko size_t :0; 1157bd5ca3efSToke Høiland-Jørgensen }; 1158bd5ca3efSToke Høiland-Jørgensen #define bpf_xdp_set_link_opts__last_field old_fd 1159bd5ca3efSToke Høiland-Jørgensen 1160c359821aSAndrii Nakryiko struct bpf_xdp_attach_opts { 1161c359821aSAndrii Nakryiko size_t sz; 1162c359821aSAndrii Nakryiko int old_prog_fd; 1163c359821aSAndrii Nakryiko size_t :0; 1164c359821aSAndrii Nakryiko }; 1165c359821aSAndrii Nakryiko #define bpf_xdp_attach_opts__last_field old_prog_fd 1166c359821aSAndrii Nakryiko 1167c359821aSAndrii Nakryiko struct bpf_xdp_query_opts { 1168c359821aSAndrii Nakryiko size_t sz; 1169c359821aSAndrii Nakryiko __u32 prog_id; /* output */ 1170c359821aSAndrii Nakryiko __u32 drv_prog_id; /* output */ 1171c359821aSAndrii Nakryiko __u32 hw_prog_id; /* output */ 1172c359821aSAndrii Nakryiko __u32 skb_prog_id; /* output */ 1173c359821aSAndrii Nakryiko __u8 attach_mode; /* output */ 117404d58f1bSLorenzo Bianconi __u64 feature_flags; /* output */ 117513ce2daaSMaciej Fijalkowski __u32 xdp_zc_max_segs; /* output */ 1176c359821aSAndrii Nakryiko size_t :0; 1177c359821aSAndrii Nakryiko }; 117813ce2daaSMaciej Fijalkowski #define bpf_xdp_query_opts__last_field xdp_zc_max_segs 1179c359821aSAndrii Nakryiko 1180c359821aSAndrii Nakryiko LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags, 1181c359821aSAndrii Nakryiko const struct bpf_xdp_attach_opts *opts); 1182c359821aSAndrii Nakryiko LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags, 1183c359821aSAndrii Nakryiko const struct bpf_xdp_attach_opts *opts); 1184c359821aSAndrii Nakryiko LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts); 1185c359821aSAndrii Nakryiko LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id); 1186c359821aSAndrii Nakryiko 1187715c5ce4SKumar Kartikeya Dwivedi /* TC related API */ 1188715c5ce4SKumar Kartikeya Dwivedi enum bpf_tc_attach_point { 1189715c5ce4SKumar Kartikeya Dwivedi BPF_TC_INGRESS = 1 << 0, 1190715c5ce4SKumar Kartikeya Dwivedi BPF_TC_EGRESS = 1 << 1, 1191715c5ce4SKumar Kartikeya Dwivedi BPF_TC_CUSTOM = 1 << 2, 1192715c5ce4SKumar Kartikeya Dwivedi }; 1193715c5ce4SKumar Kartikeya Dwivedi 1194715c5ce4SKumar Kartikeya Dwivedi #define BPF_TC_PARENT(a, b) \ 1195715c5ce4SKumar Kartikeya Dwivedi ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU)) 1196715c5ce4SKumar Kartikeya Dwivedi 1197715c5ce4SKumar Kartikeya Dwivedi enum bpf_tc_flags { 1198715c5ce4SKumar Kartikeya Dwivedi BPF_TC_F_REPLACE = 1 << 0, 1199715c5ce4SKumar Kartikeya Dwivedi }; 1200715c5ce4SKumar Kartikeya Dwivedi 1201715c5ce4SKumar Kartikeya Dwivedi struct bpf_tc_hook { 1202715c5ce4SKumar Kartikeya Dwivedi size_t sz; 1203715c5ce4SKumar Kartikeya Dwivedi int ifindex; 1204715c5ce4SKumar Kartikeya Dwivedi enum bpf_tc_attach_point attach_point; 1205715c5ce4SKumar Kartikeya Dwivedi __u32 parent; 1206715c5ce4SKumar Kartikeya Dwivedi size_t :0; 1207715c5ce4SKumar Kartikeya Dwivedi }; 1208715c5ce4SKumar Kartikeya Dwivedi #define bpf_tc_hook__last_field parent 1209715c5ce4SKumar Kartikeya Dwivedi 1210715c5ce4SKumar Kartikeya Dwivedi struct bpf_tc_opts { 1211715c5ce4SKumar Kartikeya Dwivedi size_t sz; 1212715c5ce4SKumar Kartikeya Dwivedi int prog_fd; 1213715c5ce4SKumar Kartikeya Dwivedi __u32 flags; 1214715c5ce4SKumar Kartikeya Dwivedi __u32 prog_id; 1215715c5ce4SKumar Kartikeya Dwivedi __u32 handle; 1216715c5ce4SKumar Kartikeya Dwivedi __u32 priority; 1217715c5ce4SKumar Kartikeya Dwivedi size_t :0; 1218715c5ce4SKumar Kartikeya Dwivedi }; 1219715c5ce4SKumar Kartikeya Dwivedi #define bpf_tc_opts__last_field priority 1220715c5ce4SKumar Kartikeya Dwivedi 1221715c5ce4SKumar Kartikeya Dwivedi LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook); 1222715c5ce4SKumar Kartikeya Dwivedi LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook); 1223715c5ce4SKumar Kartikeya Dwivedi LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook, 1224715c5ce4SKumar Kartikeya Dwivedi struct bpf_tc_opts *opts); 1225715c5ce4SKumar Kartikeya Dwivedi LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook, 1226715c5ce4SKumar Kartikeya Dwivedi const struct bpf_tc_opts *opts); 1227715c5ce4SKumar Kartikeya Dwivedi LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook, 1228715c5ce4SKumar Kartikeya Dwivedi struct bpf_tc_opts *opts); 1229715c5ce4SKumar Kartikeya Dwivedi 1230bf99c936SAndrii Nakryiko /* Ring buffer APIs */ 1231bf99c936SAndrii Nakryiko struct ring_buffer; 1232b66ccae0SDavid Vernet struct user_ring_buffer; 1233bf99c936SAndrii Nakryiko 1234bf99c936SAndrii Nakryiko typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size); 1235bf99c936SAndrii Nakryiko 1236bf99c936SAndrii Nakryiko struct ring_buffer_opts { 1237f8b299bcSMenglong Dong size_t sz; /* size of this struct, for forward/backward compatibility */ 1238bf99c936SAndrii Nakryiko }; 1239bf99c936SAndrii Nakryiko 1240bf99c936SAndrii Nakryiko #define ring_buffer_opts__last_field sz 1241bf99c936SAndrii Nakryiko 1242bf99c936SAndrii Nakryiko LIBBPF_API struct ring_buffer * 1243bf99c936SAndrii Nakryiko ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx, 1244bf99c936SAndrii Nakryiko const struct ring_buffer_opts *opts); 1245bf99c936SAndrii Nakryiko LIBBPF_API void ring_buffer__free(struct ring_buffer *rb); 1246bf99c936SAndrii Nakryiko LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd, 1247bf99c936SAndrii Nakryiko ring_buffer_sample_fn sample_cb, void *ctx); 1248bf99c936SAndrii Nakryiko LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms); 1249bf99c936SAndrii Nakryiko LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb); 1250a4d2a7adSBrendan Jackman LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb); 1251bf99c936SAndrii Nakryiko 1252b66ccae0SDavid Vernet struct user_ring_buffer_opts { 1253b66ccae0SDavid Vernet size_t sz; /* size of this struct, for forward/backward compatibility */ 1254b66ccae0SDavid Vernet }; 1255b66ccae0SDavid Vernet 1256b66ccae0SDavid Vernet #define user_ring_buffer_opts__last_field sz 1257b66ccae0SDavid Vernet 1258139df64dSGrant Seltzer /** 1259139df64dSGrant Seltzer * @brief **user_ring_buffer__new()** creates a new instance of a user ring 1260b66ccae0SDavid Vernet * buffer. 1261b66ccae0SDavid Vernet * 1262b66ccae0SDavid Vernet * @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map. 1263b66ccae0SDavid Vernet * @param opts Options for how the ring buffer should be created. 1264b66ccae0SDavid Vernet * @return A user ring buffer on success; NULL and errno being set on a 1265b66ccae0SDavid Vernet * failure. 1266b66ccae0SDavid Vernet */ 1267b66ccae0SDavid Vernet LIBBPF_API struct user_ring_buffer * 1268b66ccae0SDavid Vernet user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts); 1269b66ccae0SDavid Vernet 1270139df64dSGrant Seltzer /** 1271139df64dSGrant Seltzer * @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the 1272b66ccae0SDavid Vernet * user ring buffer. 1273b66ccae0SDavid Vernet * @param rb A pointer to a user ring buffer. 1274b66ccae0SDavid Vernet * @param size The size of the sample, in bytes. 1275b66ccae0SDavid Vernet * @return A pointer to an 8-byte aligned reserved region of the user ring 1276b66ccae0SDavid Vernet * buffer; NULL, and errno being set if a sample could not be reserved. 1277b66ccae0SDavid Vernet * 1278b66ccae0SDavid Vernet * This function is *not* thread safe, and callers must synchronize accessing 1279b66ccae0SDavid Vernet * this function if there are multiple producers. If a size is requested that 1280b66ccae0SDavid Vernet * is larger than the size of the entire ring buffer, errno will be set to 1281b66ccae0SDavid Vernet * E2BIG and NULL is returned. If the ring buffer could accommodate the size, 1282b66ccae0SDavid Vernet * but currently does not have enough space, errno is set to ENOSPC and NULL is 1283b66ccae0SDavid Vernet * returned. 1284b66ccae0SDavid Vernet * 1285b66ccae0SDavid Vernet * After initializing the sample, callers must invoke 1286b66ccae0SDavid Vernet * **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise, 1287b66ccae0SDavid Vernet * the sample must be freed with **user_ring_buffer__discard()**. 1288b66ccae0SDavid Vernet */ 1289b66ccae0SDavid Vernet LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size); 1290b66ccae0SDavid Vernet 1291139df64dSGrant Seltzer /** 1292139df64dSGrant Seltzer * @brief **user_ring_buffer__reserve_blocking()** reserves a record in the 1293b66ccae0SDavid Vernet * ring buffer, possibly blocking for up to @timeout_ms until a sample becomes 1294b66ccae0SDavid Vernet * available. 1295b66ccae0SDavid Vernet * @param rb The user ring buffer. 1296b66ccae0SDavid Vernet * @param size The size of the sample, in bytes. 1297b66ccae0SDavid Vernet * @param timeout_ms The amount of time, in milliseconds, for which the caller 1298b66ccae0SDavid Vernet * should block when waiting for a sample. -1 causes the caller to block 1299b66ccae0SDavid Vernet * indefinitely. 1300b66ccae0SDavid Vernet * @return A pointer to an 8-byte aligned reserved region of the user ring 1301b66ccae0SDavid Vernet * buffer; NULL, and errno being set if a sample could not be reserved. 1302b66ccae0SDavid Vernet * 1303b66ccae0SDavid Vernet * This function is *not* thread safe, and callers must synchronize 1304b66ccae0SDavid Vernet * accessing this function if there are multiple producers 1305b66ccae0SDavid Vernet * 1306b66ccae0SDavid Vernet * If **timeout_ms** is -1, the function will block indefinitely until a sample 1307b66ccae0SDavid Vernet * becomes available. Otherwise, **timeout_ms** must be non-negative, or errno 1308b66ccae0SDavid Vernet * is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking 1309b66ccae0SDavid Vernet * will occur and the function will return immediately after attempting to 1310b66ccae0SDavid Vernet * reserve a sample. 1311b66ccae0SDavid Vernet * 1312b66ccae0SDavid Vernet * If **size** is larger than the size of the entire ring buffer, errno is set 1313b66ccae0SDavid Vernet * to E2BIG and NULL is returned. If the ring buffer could accommodate 1314b66ccae0SDavid Vernet * **size**, but currently does not have enough space, the caller will block 1315b66ccae0SDavid Vernet * until at most **timeout_ms** has elapsed. If insufficient space is available 1316b66ccae0SDavid Vernet * at that time, errno is set to ENOSPC, and NULL is returned. 1317b66ccae0SDavid Vernet * 1318b66ccae0SDavid Vernet * The kernel guarantees that it will wake up this thread to check if 1319b66ccae0SDavid Vernet * sufficient space is available in the ring buffer at least once per 1320b66ccae0SDavid Vernet * invocation of the **bpf_ringbuf_drain()** helper function, provided that at 1321b66ccae0SDavid Vernet * least one sample is consumed, and the BPF program did not invoke the 1322b66ccae0SDavid Vernet * function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the 1323b66ccae0SDavid Vernet * kernel does not guarantee this. If the helper function is invoked with 1324b66ccae0SDavid Vernet * BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is 1325b66ccae0SDavid Vernet * consumed. 1326b66ccae0SDavid Vernet * 1327b66ccae0SDavid Vernet * When a sample of size **size** is found within **timeout_ms**, a pointer to 1328b66ccae0SDavid Vernet * the sample is returned. After initializing the sample, callers must invoke 1329b66ccae0SDavid Vernet * **user_ring_buffer__submit()** to post the sample to the ring buffer. 1330b66ccae0SDavid Vernet * Otherwise, the sample must be freed with **user_ring_buffer__discard()**. 1331b66ccae0SDavid Vernet */ 1332b66ccae0SDavid Vernet LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb, 1333b66ccae0SDavid Vernet __u32 size, 1334b66ccae0SDavid Vernet int timeout_ms); 1335b66ccae0SDavid Vernet 1336139df64dSGrant Seltzer /** 1337139df64dSGrant Seltzer * @brief **user_ring_buffer__submit()** submits a previously reserved sample 1338b66ccae0SDavid Vernet * into the ring buffer. 1339b66ccae0SDavid Vernet * @param rb The user ring buffer. 1340b66ccae0SDavid Vernet * @param sample A reserved sample. 1341b66ccae0SDavid Vernet * 1342b66ccae0SDavid Vernet * It is not necessary to synchronize amongst multiple producers when invoking 1343b66ccae0SDavid Vernet * this function. 1344b66ccae0SDavid Vernet */ 1345b66ccae0SDavid Vernet LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample); 1346b66ccae0SDavid Vernet 1347139df64dSGrant Seltzer /** 1348139df64dSGrant Seltzer * @brief **user_ring_buffer__discard()** discards a previously reserved sample. 1349b66ccae0SDavid Vernet * @param rb The user ring buffer. 1350b66ccae0SDavid Vernet * @param sample A reserved sample. 1351b66ccae0SDavid Vernet * 1352b66ccae0SDavid Vernet * It is not necessary to synchronize amongst multiple producers when invoking 1353b66ccae0SDavid Vernet * this function. 1354b66ccae0SDavid Vernet */ 1355b66ccae0SDavid Vernet LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample); 1356b66ccae0SDavid Vernet 1357139df64dSGrant Seltzer /** 1358139df64dSGrant Seltzer * @brief **user_ring_buffer__free()** frees a ring buffer that was previously 1359b66ccae0SDavid Vernet * created with **user_ring_buffer__new()**. 1360b66ccae0SDavid Vernet * @param rb The user ring buffer being freed. 1361b66ccae0SDavid Vernet */ 1362b66ccae0SDavid Vernet LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb); 1363b66ccae0SDavid Vernet 1364bf99c936SAndrii Nakryiko /* Perf buffer APIs */ 1365fb84b822SAndrii Nakryiko struct perf_buffer; 1366fb84b822SAndrii Nakryiko 1367fb84b822SAndrii Nakryiko typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu, 1368fb84b822SAndrii Nakryiko void *data, __u32 size); 1369fb84b822SAndrii Nakryiko typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt); 1370fb84b822SAndrii Nakryiko 1371fb84b822SAndrii Nakryiko /* common use perf buffer options */ 1372fb84b822SAndrii Nakryiko struct perf_buffer_opts { 137341788934SAndrii Nakryiko size_t sz; 1374ab8684b8SJon Doron __u32 sample_period; 1375ab8684b8SJon Doron size_t :0; 137641788934SAndrii Nakryiko }; 1377ab8684b8SJon Doron #define perf_buffer_opts__last_field sample_period 1378fb84b822SAndrii Nakryiko 137941788934SAndrii Nakryiko /** 138041788934SAndrii Nakryiko * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified 138141788934SAndrii Nakryiko * BPF_PERF_EVENT_ARRAY map 138241788934SAndrii Nakryiko * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF 138341788934SAndrii Nakryiko * code to send data over to user-space 138441788934SAndrii Nakryiko * @param page_cnt number of memory pages allocated for each per-CPU buffer 138541788934SAndrii Nakryiko * @param sample_cb function called on each received data record 138641788934SAndrii Nakryiko * @param lost_cb function called when record loss has occurred 138741788934SAndrii Nakryiko * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb* 138841788934SAndrii Nakryiko * @return a new instance of struct perf_buffer on success, NULL on error with 138941788934SAndrii Nakryiko * *errno* containing an error code 139041788934SAndrii Nakryiko */ 1391fb84b822SAndrii Nakryiko LIBBPF_API struct perf_buffer * 1392fb84b822SAndrii Nakryiko perf_buffer__new(int map_fd, size_t page_cnt, 139341788934SAndrii Nakryiko perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx, 1394fb84b822SAndrii Nakryiko const struct perf_buffer_opts *opts); 1395fb84b822SAndrii Nakryiko 1396d0cabbb0SJakub Kicinski enum bpf_perf_event_ret { 1397d0cabbb0SJakub Kicinski LIBBPF_PERF_EVENT_DONE = 0, 1398d0cabbb0SJakub Kicinski LIBBPF_PERF_EVENT_ERROR = -1, 1399d0cabbb0SJakub Kicinski LIBBPF_PERF_EVENT_CONT = -2, 1400d0cabbb0SJakub Kicinski }; 1401d0cabbb0SJakub Kicinski 14023dca2115SDaniel Borkmann struct perf_event_header; 1403fb84b822SAndrii Nakryiko 1404fb84b822SAndrii Nakryiko typedef enum bpf_perf_event_ret 1405fb84b822SAndrii Nakryiko (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event); 1406fb84b822SAndrii Nakryiko 1407fb84b822SAndrii Nakryiko /* raw perf buffer options, giving most power and control */ 1408fb84b822SAndrii Nakryiko struct perf_buffer_raw_opts { 140941788934SAndrii Nakryiko size_t sz; 141041788934SAndrii Nakryiko long :0; 141141788934SAndrii Nakryiko long :0; 1412fb84b822SAndrii Nakryiko /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of 1413fb84b822SAndrii Nakryiko * max_entries of given PERF_EVENT_ARRAY map) 1414fb84b822SAndrii Nakryiko */ 1415fb84b822SAndrii Nakryiko int cpu_cnt; 1416fb84b822SAndrii Nakryiko /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */ 1417fb84b822SAndrii Nakryiko int *cpus; 1418fb84b822SAndrii Nakryiko /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */ 1419fb84b822SAndrii Nakryiko int *map_keys; 1420fb84b822SAndrii Nakryiko }; 142141788934SAndrii Nakryiko #define perf_buffer_raw_opts__last_field map_keys 1422fb84b822SAndrii Nakryiko 142322dd7a58SAndrii Nakryiko struct perf_event_attr; 142422dd7a58SAndrii Nakryiko 1425fb84b822SAndrii Nakryiko LIBBPF_API struct perf_buffer * 142641788934SAndrii Nakryiko perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr, 142741788934SAndrii Nakryiko perf_buffer_event_fn event_cb, void *ctx, 1428fb84b822SAndrii Nakryiko const struct perf_buffer_raw_opts *opts); 1429fb84b822SAndrii Nakryiko 1430fb84b822SAndrii Nakryiko LIBBPF_API void perf_buffer__free(struct perf_buffer *pb); 1431dca5612fSAndrii Nakryiko LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb); 1432fb84b822SAndrii Nakryiko LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms); 1433272d51afSEelco Chaudron LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb); 1434dca5612fSAndrii Nakryiko LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx); 1435dca5612fSAndrii Nakryiko LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb); 1436dca5612fSAndrii Nakryiko LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx); 14379ff5efdeSJon Doron /** 14389ff5efdeSJon Doron * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying 14399ff5efdeSJon Doron * memory region of the ring buffer. 14409ff5efdeSJon Doron * This ring buffer can be used to implement a custom events consumer. 14419ff5efdeSJon Doron * The ring buffer starts with the *struct perf_event_mmap_page*, which 14429ff5efdeSJon Doron * holds the ring buffer managment fields, when accessing the header 14439ff5efdeSJon Doron * structure it's important to be SMP aware. 14449ff5efdeSJon Doron * You can refer to *perf_event_read_simple* for a simple example. 14459ff5efdeSJon Doron * @param pb the perf buffer structure 14469ff5efdeSJon Doron * @param buf_idx the buffer index to retreive 14479ff5efdeSJon Doron * @param buf (out) gets the base pointer of the mmap()'ed memory 14489ff5efdeSJon Doron * @param buf_size (out) gets the size of the mmap()'ed region 14499ff5efdeSJon Doron * @return 0 on success, negative error code for failure 14509ff5efdeSJon Doron */ 14519ff5efdeSJon Doron LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf, 14529ff5efdeSJon Doron size_t *buf_size); 1453fb84b822SAndrii Nakryiko 1454b053b439SMartin KaFai Lau struct bpf_prog_linfo; 1455b053b439SMartin KaFai Lau struct bpf_prog_info; 1456b053b439SMartin KaFai Lau 1457b053b439SMartin KaFai Lau LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo); 1458b053b439SMartin KaFai Lau LIBBPF_API struct bpf_prog_linfo * 1459b053b439SMartin KaFai Lau bpf_prog_linfo__new(const struct bpf_prog_info *info); 1460b053b439SMartin KaFai Lau LIBBPF_API const struct bpf_line_info * 1461b053b439SMartin KaFai Lau bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo, 1462b053b439SMartin KaFai Lau __u64 addr, __u32 func_idx, __u32 nr_skip); 1463b053b439SMartin KaFai Lau LIBBPF_API const struct bpf_line_info * 1464b053b439SMartin KaFai Lau bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo, 1465b053b439SMartin KaFai Lau __u32 insn_off, __u32 nr_skip); 1466b053b439SMartin KaFai Lau 14671bf4b058SQuentin Monnet /* 14681bf4b058SQuentin Monnet * Probe for supported system features 14691bf4b058SQuentin Monnet * 14701bf4b058SQuentin Monnet * Note that running many of these probes in a short amount of time can cause 14711bf4b058SQuentin Monnet * the kernel to reach the maximal size of lockable memory allowed for the 14721bf4b058SQuentin Monnet * user, causing subsequent probes to fail. In this case, the caller may want 14731bf4b058SQuentin Monnet * to adjust that limit with setrlimit(). 14741bf4b058SQuentin Monnet */ 14751bf4b058SQuentin Monnet 1476878d8defSAndrii Nakryiko /** 1477878d8defSAndrii Nakryiko * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports 1478878d8defSAndrii Nakryiko * BPF programs of a given type. 1479878d8defSAndrii Nakryiko * @param prog_type BPF program type to detect kernel support for 1480878d8defSAndrii Nakryiko * @param opts reserved for future extensibility, should be NULL 1481878d8defSAndrii Nakryiko * @return 1, if given program type is supported; 0, if given program type is 1482878d8defSAndrii Nakryiko * not supported; negative error code if feature detection failed or can't be 1483878d8defSAndrii Nakryiko * performed 1484878d8defSAndrii Nakryiko * 1485878d8defSAndrii Nakryiko * Make sure the process has required set of CAP_* permissions (or runs as 1486878d8defSAndrii Nakryiko * root) when performing feature checking. 1487878d8defSAndrii Nakryiko */ 1488878d8defSAndrii Nakryiko LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts); 1489878d8defSAndrii Nakryiko /** 1490878d8defSAndrii Nakryiko * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports 1491878d8defSAndrii Nakryiko * BPF maps of a given type. 1492878d8defSAndrii Nakryiko * @param map_type BPF map type to detect kernel support for 1493878d8defSAndrii Nakryiko * @param opts reserved for future extensibility, should be NULL 1494878d8defSAndrii Nakryiko * @return 1, if given map type is supported; 0, if given map type is 1495878d8defSAndrii Nakryiko * not supported; negative error code if feature detection failed or can't be 1496878d8defSAndrii Nakryiko * performed 1497878d8defSAndrii Nakryiko * 1498878d8defSAndrii Nakryiko * Make sure the process has required set of CAP_* permissions (or runs as 1499878d8defSAndrii Nakryiko * root) when performing feature checking. 1500878d8defSAndrii Nakryiko */ 1501878d8defSAndrii Nakryiko LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts); 1502878d8defSAndrii Nakryiko /** 1503878d8defSAndrii Nakryiko * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the 1504878d8defSAndrii Nakryiko * use of a given BPF helper from specified BPF program type. 1505878d8defSAndrii Nakryiko * @param prog_type BPF program type used to check the support of BPF helper 1506878d8defSAndrii Nakryiko * @param helper_id BPF helper ID (enum bpf_func_id) to check support for 1507878d8defSAndrii Nakryiko * @param opts reserved for future extensibility, should be NULL 1508878d8defSAndrii Nakryiko * @return 1, if given combination of program type and helper is supported; 0, 1509878d8defSAndrii Nakryiko * if the combination is not supported; negative error code if feature 1510878d8defSAndrii Nakryiko * detection for provided input arguments failed or can't be performed 1511878d8defSAndrii Nakryiko * 1512878d8defSAndrii Nakryiko * Make sure the process has required set of CAP_* permissions (or runs as 1513878d8defSAndrii Nakryiko * root) when performing feature checking. 1514878d8defSAndrii Nakryiko */ 1515878d8defSAndrii Nakryiko LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, 1516878d8defSAndrii Nakryiko enum bpf_func_id helper_id, const void *opts); 1517878d8defSAndrii Nakryiko 151897c140d9SGrant Seltzer /** 151997c140d9SGrant Seltzer * @brief **libbpf_num_possible_cpus()** is a helper function to get the 152097c140d9SGrant Seltzer * number of possible CPUs that the host kernel supports and expects. 152197c140d9SGrant Seltzer * @return number of possible CPUs; or error code on failure 15226446b315SHechao Li * 15236446b315SHechao Li * Example usage: 15246446b315SHechao Li * 15256446b315SHechao Li * int ncpus = libbpf_num_possible_cpus(); 15266446b315SHechao Li * if (ncpus < 0) { 15276446b315SHechao Li * // error handling 15286446b315SHechao Li * } 15296446b315SHechao Li * long values[ncpus]; 15306446b315SHechao Li * bpf_map_lookup_elem(per_cpu_map_fd, key, values); 15316446b315SHechao Li */ 15326446b315SHechao Li LIBBPF_API int libbpf_num_possible_cpus(void); 15336446b315SHechao Li 1534d66562fbSAndrii Nakryiko struct bpf_map_skeleton { 1535d66562fbSAndrii Nakryiko const char *name; 1536d66562fbSAndrii Nakryiko struct bpf_map **map; 1537d66562fbSAndrii Nakryiko void **mmaped; 1538d66562fbSAndrii Nakryiko }; 1539d66562fbSAndrii Nakryiko 1540d66562fbSAndrii Nakryiko struct bpf_prog_skeleton { 1541d66562fbSAndrii Nakryiko const char *name; 1542d66562fbSAndrii Nakryiko struct bpf_program **prog; 1543d66562fbSAndrii Nakryiko struct bpf_link **link; 1544d66562fbSAndrii Nakryiko }; 1545d66562fbSAndrii Nakryiko 1546d66562fbSAndrii Nakryiko struct bpf_object_skeleton { 1547d66562fbSAndrii Nakryiko size_t sz; /* size of this struct, for forward/backward compatibility */ 1548d66562fbSAndrii Nakryiko 1549d66562fbSAndrii Nakryiko const char *name; 155008a6f22eSMatt Smith const void *data; 1551d66562fbSAndrii Nakryiko size_t data_sz; 1552d66562fbSAndrii Nakryiko 1553d66562fbSAndrii Nakryiko struct bpf_object **obj; 1554d66562fbSAndrii Nakryiko 1555d66562fbSAndrii Nakryiko int map_cnt; 1556222c98c7Shuangxuesen int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */ 1557d66562fbSAndrii Nakryiko struct bpf_map_skeleton *maps; 1558d66562fbSAndrii Nakryiko 1559d66562fbSAndrii Nakryiko int prog_cnt; 1560222c98c7Shuangxuesen int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */ 1561d66562fbSAndrii Nakryiko struct bpf_prog_skeleton *progs; 1562d66562fbSAndrii Nakryiko }; 1563d66562fbSAndrii Nakryiko 1564d66562fbSAndrii Nakryiko LIBBPF_API int 1565d66562fbSAndrii Nakryiko bpf_object__open_skeleton(struct bpf_object_skeleton *s, 1566d66562fbSAndrii Nakryiko const struct bpf_object_open_opts *opts); 1567d66562fbSAndrii Nakryiko LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s); 1568d66562fbSAndrii Nakryiko LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s); 1569d66562fbSAndrii Nakryiko LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s); 1570d66562fbSAndrii Nakryiko LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s); 1571d66562fbSAndrii Nakryiko 1572430025e5SDelyan Kratunov struct bpf_var_skeleton { 1573430025e5SDelyan Kratunov const char *name; 1574430025e5SDelyan Kratunov struct bpf_map **map; 1575430025e5SDelyan Kratunov void **addr; 1576430025e5SDelyan Kratunov }; 1577430025e5SDelyan Kratunov 1578430025e5SDelyan Kratunov struct bpf_object_subskeleton { 1579430025e5SDelyan Kratunov size_t sz; /* size of this struct, for forward/backward compatibility */ 1580430025e5SDelyan Kratunov 1581430025e5SDelyan Kratunov const struct bpf_object *obj; 1582430025e5SDelyan Kratunov 1583430025e5SDelyan Kratunov int map_cnt; 1584430025e5SDelyan Kratunov int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */ 1585430025e5SDelyan Kratunov struct bpf_map_skeleton *maps; 1586430025e5SDelyan Kratunov 1587430025e5SDelyan Kratunov int prog_cnt; 1588430025e5SDelyan Kratunov int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */ 1589430025e5SDelyan Kratunov struct bpf_prog_skeleton *progs; 1590430025e5SDelyan Kratunov 1591430025e5SDelyan Kratunov int var_cnt; 1592430025e5SDelyan Kratunov int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */ 1593430025e5SDelyan Kratunov struct bpf_var_skeleton *vars; 1594430025e5SDelyan Kratunov }; 1595430025e5SDelyan Kratunov 1596430025e5SDelyan Kratunov LIBBPF_API int 1597430025e5SDelyan Kratunov bpf_object__open_subskeleton(struct bpf_object_subskeleton *s); 1598430025e5SDelyan Kratunov LIBBPF_API void 1599430025e5SDelyan Kratunov bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s); 1600430025e5SDelyan Kratunov 160167234743SAlexei Starovoitov struct gen_loader_opts { 1602f8b299bcSMenglong Dong size_t sz; /* size of this struct, for forward/backward compatibility */ 160367234743SAlexei Starovoitov const char *data; 160467234743SAlexei Starovoitov const char *insns; 160567234743SAlexei Starovoitov __u32 data_sz; 160667234743SAlexei Starovoitov __u32 insns_sz; 160767234743SAlexei Starovoitov }; 160867234743SAlexei Starovoitov 160967234743SAlexei Starovoitov #define gen_loader_opts__last_field insns_sz 161067234743SAlexei Starovoitov LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj, 161167234743SAlexei Starovoitov struct gen_loader_opts *opts); 161267234743SAlexei Starovoitov 1613166750bcSAndrii Nakryiko enum libbpf_tristate { 1614166750bcSAndrii Nakryiko TRI_NO = 0, 1615166750bcSAndrii Nakryiko TRI_YES = 1, 1616166750bcSAndrii Nakryiko TRI_MODULE = 2, 1617166750bcSAndrii Nakryiko }; 1618166750bcSAndrii Nakryiko 1619faf6ed32SAndrii Nakryiko struct bpf_linker_opts { 1620f8b299bcSMenglong Dong /* size of this struct, for forward/backward compatibility */ 1621faf6ed32SAndrii Nakryiko size_t sz; 1622faf6ed32SAndrii Nakryiko }; 1623faf6ed32SAndrii Nakryiko #define bpf_linker_opts__last_field sz 1624faf6ed32SAndrii Nakryiko 1625fdbf5ddeSAndrii Nakryiko struct bpf_linker_file_opts { 1626f8b299bcSMenglong Dong /* size of this struct, for forward/backward compatibility */ 1627fdbf5ddeSAndrii Nakryiko size_t sz; 1628fdbf5ddeSAndrii Nakryiko }; 1629fdbf5ddeSAndrii Nakryiko #define bpf_linker_file_opts__last_field sz 1630fdbf5ddeSAndrii Nakryiko 1631faf6ed32SAndrii Nakryiko struct bpf_linker; 1632faf6ed32SAndrii Nakryiko 1633faf6ed32SAndrii Nakryiko LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts); 1634fdbf5ddeSAndrii Nakryiko LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, 1635fdbf5ddeSAndrii Nakryiko const char *filename, 1636fdbf5ddeSAndrii Nakryiko const struct bpf_linker_file_opts *opts); 1637faf6ed32SAndrii Nakryiko LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); 1638faf6ed32SAndrii Nakryiko LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); 1639faf6ed32SAndrii Nakryiko 1640697f104dSAndrii Nakryiko /* 1641697f104dSAndrii Nakryiko * Custom handling of BPF program's SEC() definitions 1642697f104dSAndrii Nakryiko */ 1643697f104dSAndrii Nakryiko 1644697f104dSAndrii Nakryiko struct bpf_prog_load_opts; /* defined in bpf.h */ 1645697f104dSAndrii Nakryiko 1646697f104dSAndrii Nakryiko /* Called during bpf_object__open() for each recognized BPF program. Callback 1647697f104dSAndrii Nakryiko * can use various bpf_program__set_*() setters to adjust whatever properties 1648697f104dSAndrii Nakryiko * are necessary. 1649697f104dSAndrii Nakryiko */ 1650697f104dSAndrii Nakryiko typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie); 1651697f104dSAndrii Nakryiko 1652697f104dSAndrii Nakryiko /* Called right before libbpf performs bpf_prog_load() to load BPF program 1653697f104dSAndrii Nakryiko * into the kernel. Callback can adjust opts as necessary. 1654697f104dSAndrii Nakryiko */ 1655697f104dSAndrii Nakryiko typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog, 1656697f104dSAndrii Nakryiko struct bpf_prog_load_opts *opts, long cookie); 1657697f104dSAndrii Nakryiko 1658697f104dSAndrii Nakryiko /* Called during skeleton attach or through bpf_program__attach(). If 1659697f104dSAndrii Nakryiko * auto-attach is not supported, callback should return 0 and set link to 1660697f104dSAndrii Nakryiko * NULL (it's not considered an error during skeleton attach, but it will be 1661697f104dSAndrii Nakryiko * an error for bpf_program__attach() calls). On error, error should be 1662697f104dSAndrii Nakryiko * returned directly and link set to NULL. On success, return 0 and set link 1663697f104dSAndrii Nakryiko * to a valid struct bpf_link. 1664697f104dSAndrii Nakryiko */ 1665697f104dSAndrii Nakryiko typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie, 1666697f104dSAndrii Nakryiko struct bpf_link **link); 1667697f104dSAndrii Nakryiko 1668697f104dSAndrii Nakryiko struct libbpf_prog_handler_opts { 1669f8b299bcSMenglong Dong /* size of this struct, for forward/backward compatibility */ 1670697f104dSAndrii Nakryiko size_t sz; 1671697f104dSAndrii Nakryiko /* User-provided value that is passed to prog_setup_fn, 1672697f104dSAndrii Nakryiko * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to 1673697f104dSAndrii Nakryiko * register one set of callbacks for multiple SEC() definitions and 1674697f104dSAndrii Nakryiko * still be able to distinguish them, if necessary. For example, 1675697f104dSAndrii Nakryiko * libbpf itself is using this to pass necessary flags (e.g., 1676697f104dSAndrii Nakryiko * sleepable flag) to a common internal SEC() handler. 1677697f104dSAndrii Nakryiko */ 1678697f104dSAndrii Nakryiko long cookie; 1679697f104dSAndrii Nakryiko /* BPF program initialization callback (see libbpf_prog_setup_fn_t). 1680697f104dSAndrii Nakryiko * Callback is optional, pass NULL if it's not necessary. 1681697f104dSAndrii Nakryiko */ 1682697f104dSAndrii Nakryiko libbpf_prog_setup_fn_t prog_setup_fn; 1683697f104dSAndrii Nakryiko /* BPF program loading callback (see libbpf_prog_prepare_load_fn_t). 1684697f104dSAndrii Nakryiko * Callback is optional, pass NULL if it's not necessary. 1685697f104dSAndrii Nakryiko */ 1686697f104dSAndrii Nakryiko libbpf_prog_prepare_load_fn_t prog_prepare_load_fn; 1687697f104dSAndrii Nakryiko /* BPF program attach callback (see libbpf_prog_attach_fn_t). 1688697f104dSAndrii Nakryiko * Callback is optional, pass NULL if it's not necessary. 1689697f104dSAndrii Nakryiko */ 1690697f104dSAndrii Nakryiko libbpf_prog_attach_fn_t prog_attach_fn; 1691697f104dSAndrii Nakryiko }; 1692697f104dSAndrii Nakryiko #define libbpf_prog_handler_opts__last_field prog_attach_fn 1693697f104dSAndrii Nakryiko 1694697f104dSAndrii Nakryiko /** 1695697f104dSAndrii Nakryiko * @brief **libbpf_register_prog_handler()** registers a custom BPF program 1696697f104dSAndrii Nakryiko * SEC() handler. 1697697f104dSAndrii Nakryiko * @param sec section prefix for which custom handler is registered 1698697f104dSAndrii Nakryiko * @param prog_type BPF program type associated with specified section 1699697f104dSAndrii Nakryiko * @param exp_attach_type Expected BPF attach type associated with specified section 1700697f104dSAndrii Nakryiko * @param opts optional cookie, callbacks, and other extra options 1701697f104dSAndrii Nakryiko * @return Non-negative handler ID is returned on success. This handler ID has 1702697f104dSAndrii Nakryiko * to be passed to *libbpf_unregister_prog_handler()* to unregister such 1703697f104dSAndrii Nakryiko * custom handler. Negative error code is returned on error. 1704697f104dSAndrii Nakryiko * 1705697f104dSAndrii Nakryiko * *sec* defines which SEC() definitions are handled by this custom handler 1706697f104dSAndrii Nakryiko * registration. *sec* can have few different forms: 1707697f104dSAndrii Nakryiko * - if *sec* is just a plain string (e.g., "abc"), it will match only 1708697f104dSAndrii Nakryiko * SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result 1709697f104dSAndrii Nakryiko * in an error; 1710697f104dSAndrii Nakryiko * - if *sec* is of the form "abc/", proper SEC() form is 1711697f104dSAndrii Nakryiko * SEC("abc/something"), where acceptable "something" should be checked by 1712697f104dSAndrii Nakryiko * *prog_init_fn* callback, if there are additional restrictions; 1713697f104dSAndrii Nakryiko * - if *sec* is of the form "abc+", it will successfully match both 1714697f104dSAndrii Nakryiko * SEC("abc") and SEC("abc/whatever") forms; 1715697f104dSAndrii Nakryiko * - if *sec* is NULL, custom handler is registered for any BPF program that 1716697f104dSAndrii Nakryiko * doesn't match any of the registered (custom or libbpf's own) SEC() 1717697f104dSAndrii Nakryiko * handlers. There could be only one such generic custom handler registered 1718697f104dSAndrii Nakryiko * at any given time. 1719697f104dSAndrii Nakryiko * 1720697f104dSAndrii Nakryiko * All custom handlers (except the one with *sec* == NULL) are processed 1721697f104dSAndrii Nakryiko * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's 1722697f104dSAndrii Nakryiko * SEC() handlers by registering custom ones for the same section prefix 1723697f104dSAndrii Nakryiko * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses") 1724697f104dSAndrii Nakryiko * handler). 1725697f104dSAndrii Nakryiko * 1726697f104dSAndrii Nakryiko * Note, like much of global libbpf APIs (e.g., libbpf_set_print(), 1727697f104dSAndrii Nakryiko * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs 1728697f104dSAndrii Nakryiko * to ensure synchronization if there is a risk of running this API from 1729697f104dSAndrii Nakryiko * multiple threads simultaneously. 1730697f104dSAndrii Nakryiko */ 1731697f104dSAndrii Nakryiko LIBBPF_API int libbpf_register_prog_handler(const char *sec, 1732697f104dSAndrii Nakryiko enum bpf_prog_type prog_type, 1733697f104dSAndrii Nakryiko enum bpf_attach_type exp_attach_type, 1734697f104dSAndrii Nakryiko const struct libbpf_prog_handler_opts *opts); 1735697f104dSAndrii Nakryiko /** 1736697f104dSAndrii Nakryiko * @brief *libbpf_unregister_prog_handler()* unregisters previously registered 1737697f104dSAndrii Nakryiko * custom BPF program SEC() handler. 1738697f104dSAndrii Nakryiko * @param handler_id handler ID returned by *libbpf_register_prog_handler()* 1739697f104dSAndrii Nakryiko * after successful registration 1740697f104dSAndrii Nakryiko * @return 0 on success, negative error code if handler isn't found 1741697f104dSAndrii Nakryiko * 1742697f104dSAndrii Nakryiko * Note, like much of global libbpf APIs (e.g., libbpf_set_print(), 1743697f104dSAndrii Nakryiko * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs 1744697f104dSAndrii Nakryiko * to ensure synchronization if there is a risk of running this API from 1745697f104dSAndrii Nakryiko * multiple threads simultaneously. 1746697f104dSAndrii Nakryiko */ 1747697f104dSAndrii Nakryiko LIBBPF_API int libbpf_unregister_prog_handler(int handler_id); 1748697f104dSAndrii Nakryiko 17498c4905b9SStanislav Fomichev #ifdef __cplusplus 17508c4905b9SStanislav Fomichev } /* extern "C" */ 17518c4905b9SStanislav Fomichev #endif 17528c4905b9SStanislav Fomichev 1753eff81908SAndrey Ignatov #endif /* __LIBBPF_LIBBPF_H */ 1754