1917 lines · c
1/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */2 3/*4 * Common eBPF ELF object loading operations.5 *6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>8 * Copyright (C) 2015 Huawei Inc.9 */10#ifndef __LIBBPF_LIBBPF_H11#define __LIBBPF_LIBBPF_H12 13#include <stdarg.h>14#include <stdio.h>15#include <stdint.h>16#include <stdbool.h>17#include <sys/types.h> // for size_t18#include <linux/bpf.h>19 20#include "libbpf_common.h"21#include "libbpf_legacy.h"22 23#ifdef __cplusplus24extern "C" {25#endif26 27LIBBPF_API __u32 libbpf_major_version(void);28LIBBPF_API __u32 libbpf_minor_version(void);29LIBBPF_API const char *libbpf_version_string(void);30 31enum libbpf_errno {32 __LIBBPF_ERRNO__START = 4000,33 34 /* Something wrong in libelf */35 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,36 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */37 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */38 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */39 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */40 LIBBPF_ERRNO__RELOC, /* Relocation failed */41 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */42 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */43 LIBBPF_ERRNO__PROG2BIG, /* Program too big */44 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */45 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */46 LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */47 LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */48 LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */49 __LIBBPF_ERRNO__END,50};51 52LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);53 54/**55 * @brief **libbpf_bpf_attach_type_str()** converts the provided attach type56 * value into a textual representation.57 * @param t The attach type.58 * @return Pointer to a static string identifying the attach type. NULL is59 * returned for unknown **bpf_attach_type** values.60 */61LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);62 63/**64 * @brief **libbpf_bpf_link_type_str()** converts the provided link type value65 * into a textual representation.66 * @param t The link type.67 * @return Pointer to a static string identifying the link type. NULL is68 * returned for unknown **bpf_link_type** values.69 */70LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t);71 72/**73 * @brief **libbpf_bpf_map_type_str()** converts the provided map type value74 * into a textual representation.75 * @param t The map type.76 * @return Pointer to a static string identifying the map type. NULL is77 * returned for unknown **bpf_map_type** values.78 */79LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);80 81/**82 * @brief **libbpf_bpf_prog_type_str()** converts the provided program type83 * value into a textual representation.84 * @param t The program type.85 * @return Pointer to a static string identifying the program type. NULL is86 * returned for unknown **bpf_prog_type** values.87 */88LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);89 90enum libbpf_print_level {91 LIBBPF_WARN,92 LIBBPF_INFO,93 LIBBPF_DEBUG,94};95 96typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,97 const char *, va_list ap);98 99/**100 * @brief **libbpf_set_print()** sets user-provided log callback function to101 * be used for libbpf warnings and informational messages. If the user callback102 * is not set, messages are logged to stderr by default. The verbosity of these103 * messages can be controlled by setting the environment variable104 * LIBBPF_LOG_LEVEL to either warn, info, or debug.105 * @param fn The log print function. If NULL, libbpf won't print anything.106 * @return Pointer to old print function.107 *108 * This function is thread-safe.109 */110LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);111 112/* Hide internal to user */113struct bpf_object;114 115struct bpf_object_open_opts {116 /* size of this struct, for forward/backward compatibility */117 size_t sz;118 /* object name override, if provided:119 * - for object open from file, this will override setting object120 * name from file path's base name;121 * - for object open from memory buffer, this will specify an object122 * name and will override default "<addr>-<buf-size>" name;123 */124 const char *object_name;125 /* parse map definitions non-strictly, allowing extra attributes/data */126 bool relaxed_maps;127 /* maps that set the 'pinning' attribute in their definition will have128 * their pin_path attribute set to a file in this directory, and be129 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".130 */131 const char *pin_root_path;132 133 __u32 :32; /* stub out now removed attach_prog_fd */134 135 /* Additional kernel config content that augments and overrides136 * system Kconfig for CONFIG_xxx externs.137 */138 const char *kconfig;139 /* Path to the custom BTF to be used for BPF CO-RE relocations.140 * This custom BTF completely replaces the use of vmlinux BTF141 * for the purpose of CO-RE relocations.142 * NOTE: any other BPF feature (e.g., fentry/fexit programs,143 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.144 */145 const char *btf_custom_path;146 /* Pointer to a buffer for storing kernel logs for applicable BPF147 * commands. Valid kernel_log_size has to be specified as well and are148 * passed-through to bpf() syscall. Keep in mind that kernel might149 * fail operation with -ENOSPC error if provided buffer is too small150 * to contain entire log output.151 * See the comment below for kernel_log_level for interaction between152 * log_buf and log_level settings.153 *154 * If specified, this log buffer will be passed for:155 * - each BPF progral load (BPF_PROG_LOAD) attempt, unless overridden156 * with bpf_program__set_log() on per-program level, to get157 * BPF verifier log output.158 * - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get159 * BTF sanity checking log.160 *161 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite162 * previous contents, so if you need more fine-grained control, set163 * per-program buffer with bpf_program__set_log_buf() to preserve each164 * individual program's verification log. Keep using kernel_log_buf165 * for BTF verification log, if necessary.166 */167 char *kernel_log_buf;168 size_t kernel_log_size;169 /*170 * Log level can be set independently from log buffer. Log_level=0171 * means that libbpf will attempt loading BTF or program without any172 * logging requested, but will retry with either its own or custom log173 * buffer, if provided, and log_level=1 on any error.174 * And vice versa, setting log_level>0 will request BTF or prog175 * loading with verbose log from the first attempt (and as such also176 * for successfully loaded BTF or program), and the actual log buffer177 * could be either libbpf's own auto-allocated log buffer, if178 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.179 * If user didn't provide custom log buffer, libbpf will emit captured180 * logs through its print callback.181 */182 __u32 kernel_log_level;183 /* Path to BPF FS mount point to derive BPF token from.184 *185 * Created BPF token will be used for all bpf() syscall operations186 * that accept BPF token (e.g., map creation, BTF and program loads,187 * etc) automatically within instantiated BPF object.188 *189 * If bpf_token_path is not specified, libbpf will consult190 * LIBBPF_BPF_TOKEN_PATH environment variable. If set, it will be191 * taken as a value of bpf_token_path option and will force libbpf to192 * either create BPF token from provided custom BPF FS path, or will193 * disable implicit BPF token creation, if envvar value is an empty194 * string. bpf_token_path overrides LIBBPF_BPF_TOKEN_PATH, if both are195 * set at the same time.196 *197 * Setting bpf_token_path option to empty string disables libbpf's198 * automatic attempt to create BPF token from default BPF FS mount199 * point (/sys/fs/bpf), in case this default behavior is undesirable.200 */201 const char *bpf_token_path;202 203 size_t :0;204};205#define bpf_object_open_opts__last_field bpf_token_path206 207/**208 * @brief **bpf_object__open()** creates a bpf_object by opening209 * the BPF ELF object file pointed to by the passed path and loading it210 * into memory.211 * @param path BPF object file path.212 * @return pointer to the new bpf_object; or NULL is returned on error,213 * error code is stored in errno214 */215LIBBPF_API struct bpf_object *bpf_object__open(const char *path);216 217/**218 * @brief **bpf_object__open_file()** creates a bpf_object by opening219 * the BPF ELF object file pointed to by the passed path and loading it220 * into memory.221 * @param path BPF object file path222 * @param opts options for how to load the bpf object, this parameter is223 * optional and can be set to NULL224 * @return pointer to the new bpf_object; or NULL is returned on error,225 * error code is stored in errno226 */227LIBBPF_API struct bpf_object *228bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);229 230/**231 * @brief **bpf_object__open_mem()** creates a bpf_object by reading232 * the BPF objects raw bytes from a memory buffer containing a valid233 * BPF ELF object file.234 * @param obj_buf pointer to the buffer containing ELF file bytes235 * @param obj_buf_sz number of bytes in the buffer236 * @param opts options for how to load the bpf object237 * @return pointer to the new bpf_object; or NULL is returned on error,238 * error code is stored in errno239 */240LIBBPF_API struct bpf_object *241bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,242 const struct bpf_object_open_opts *opts);243 244/**245 * @brief **bpf_object__load()** loads BPF object into kernel.246 * @param obj Pointer to a valid BPF object instance returned by247 * **bpf_object__open*()** APIs248 * @return 0, on success; negative error code, otherwise, error code is249 * stored in errno250 */251LIBBPF_API int bpf_object__load(struct bpf_object *obj);252 253/**254 * @brief **bpf_object__close()** closes a BPF object and releases all255 * resources.256 * @param obj Pointer to a valid BPF object257 */258LIBBPF_API void bpf_object__close(struct bpf_object *obj);259 260/**261 * @brief **bpf_object__pin_maps()** pins each map contained within262 * the BPF object at the passed directory.263 * @param obj Pointer to a valid BPF object264 * @param path A directory where maps should be pinned.265 * @return 0, on success; negative error code, otherwise266 *267 * If `path` is NULL `bpf_map__pin` (which is being used on each map)268 * will use the pin_path attribute of each map. In this case, maps that269 * don't have a pin_path set will be ignored.270 */271LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);272 273/**274 * @brief **bpf_object__unpin_maps()** unpins each map contained within275 * the BPF object found in the passed directory.276 * @param obj Pointer to a valid BPF object277 * @param path A directory where pinned maps should be searched for.278 * @return 0, on success; negative error code, otherwise279 *280 * If `path` is NULL `bpf_map__unpin` (which is being used on each map)281 * will use the pin_path attribute of each map. In this case, maps that282 * don't have a pin_path set will be ignored.283 */284LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,285 const char *path);286LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,287 const char *path);288LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,289 const char *path);290LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);291LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path);292 293LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);294LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);295LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);296 297/**298 * @brief **bpf_object__token_fd** is an accessor for BPF token FD associated299 * with BPF object.300 * @param obj Pointer to a valid BPF object301 * @return BPF token FD or -1, if it wasn't set302 */303LIBBPF_API int bpf_object__token_fd(const struct bpf_object *obj);304 305struct btf;306LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);307LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);308 309LIBBPF_API struct bpf_program *310bpf_object__find_program_by_name(const struct bpf_object *obj,311 const char *name);312 313LIBBPF_API int314libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,315 enum bpf_attach_type *expected_attach_type);316LIBBPF_API int libbpf_attach_type_by_name(const char *name,317 enum bpf_attach_type *attach_type);318LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,319 enum bpf_attach_type attach_type);320 321/* Accessors of bpf_program */322struct bpf_program;323 324LIBBPF_API struct bpf_program *325bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);326 327#define bpf_object__for_each_program(pos, obj) \328 for ((pos) = bpf_object__next_program((obj), NULL); \329 (pos) != NULL; \330 (pos) = bpf_object__next_program((obj), (pos)))331 332LIBBPF_API struct bpf_program *333bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);334 335LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,336 __u32 ifindex);337 338LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);339LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);340LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);341LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);342LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);343LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);344 345struct bpf_insn;346 347/**348 * @brief **bpf_program__insns()** gives read-only access to BPF program's349 * underlying BPF instructions.350 * @param prog BPF program for which to return instructions351 * @return a pointer to an array of BPF instructions that belong to the352 * specified BPF program353 *354 * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`355 * pointed to can be fetched using **bpf_program__insn_cnt()** API.356 *357 * Keep in mind, libbpf can modify and append/delete BPF program's358 * instructions as it processes BPF object file and prepares everything for359 * uploading into the kernel. So depending on the point in BPF object360 * lifetime, **bpf_program__insns()** can return different sets of361 * instructions. As an example, during BPF object load phase BPF program362 * instructions will be CO-RE-relocated, BPF subprograms instructions will be363 * appended, ldimm64 instructions will have FDs embedded, etc. So instructions364 * returned before **bpf_object__load()** and after it might be quite365 * different.366 */367LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);368 369/**370 * @brief **bpf_program__set_insns()** can set BPF program's underlying371 * BPF instructions.372 *373 * WARNING: This is a very advanced libbpf API and users need to know374 * what they are doing. This should be used from prog_prepare_load_fn375 * callback only.376 *377 * @param prog BPF program for which to return instructions378 * @param new_insns a pointer to an array of BPF instructions379 * @param new_insn_cnt number of `struct bpf_insn`'s that form380 * specified BPF program381 * @return 0, on success; negative error code, otherwise382 */383LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,384 struct bpf_insn *new_insns, size_t new_insn_cnt);385 386/**387 * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s388 * that form specified BPF program.389 * @param prog BPF program for which to return number of BPF instructions390 *391 * See **bpf_program__insns()** documentation for notes on how libbpf can392 * change instructions and their count during different phases of393 * **bpf_object** lifetime.394 */395LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);396 397LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);398 399/**400 * @brief **bpf_program__pin()** pins the BPF program to a file401 * in the BPF FS specified by a path. This increments the programs402 * reference count, allowing it to stay loaded after the process403 * which loaded it has exited.404 *405 * @param prog BPF program to pin, must already be loaded406 * @param path file path in a BPF file system407 * @return 0, on success; negative error code, otherwise408 */409LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);410 411/**412 * @brief **bpf_program__unpin()** unpins the BPF program from a file413 * in the BPFFS specified by a path. This decrements the programs414 * reference count.415 *416 * The file pinning the BPF program can also be unlinked by a different417 * process in which case this function will return an error.418 *419 * @param prog BPF program to unpin420 * @param path file path to the pin in a BPF file system421 * @return 0, on success; negative error code, otherwise422 */423LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);424LIBBPF_API void bpf_program__unload(struct bpf_program *prog);425 426struct bpf_link;427 428LIBBPF_API struct bpf_link *bpf_link__open(const char *path);429LIBBPF_API int bpf_link__fd(const struct bpf_link *link);430LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);431/**432 * @brief **bpf_link__pin()** pins the BPF link to a file433 * in the BPF FS specified by a path. This increments the links434 * reference count, allowing it to stay loaded after the process435 * which loaded it has exited.436 *437 * @param link BPF link to pin, must already be loaded438 * @param path file path in a BPF file system439 * @return 0, on success; negative error code, otherwise440 */441 442LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);443 444/**445 * @brief **bpf_link__unpin()** unpins the BPF link from a file446 * in the BPFFS specified by a path. This decrements the links447 * reference count.448 *449 * The file pinning the BPF link can also be unlinked by a different450 * process in which case this function will return an error.451 *452 * @param prog BPF program to unpin453 * @param path file path to the pin in a BPF file system454 * @return 0, on success; negative error code, otherwise455 */456LIBBPF_API int bpf_link__unpin(struct bpf_link *link);457LIBBPF_API int bpf_link__update_program(struct bpf_link *link,458 struct bpf_program *prog);459LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);460LIBBPF_API int bpf_link__detach(struct bpf_link *link);461LIBBPF_API int bpf_link__destroy(struct bpf_link *link);462 463/**464 * @brief **bpf_program__attach()** is a generic function for attaching465 * a BPF program based on auto-detection of program type, attach type,466 * and extra parameters, where applicable.467 *468 * @param prog BPF program to attach469 * @return Reference to the newly created BPF link; or NULL is returned on error,470 * error code is stored in errno471 *472 * This is supported for:473 * - kprobe/kretprobe (depends on SEC() definition)474 * - uprobe/uretprobe (depends on SEC() definition)475 * - tracepoint476 * - raw tracepoint477 * - tracing programs (typed raw TP/fentry/fexit/fmod_ret)478 */479LIBBPF_API struct bpf_link *480bpf_program__attach(const struct bpf_program *prog);481 482struct bpf_perf_event_opts {483 /* size of this struct, for forward/backward compatibility */484 size_t sz;485 /* custom user-provided value fetchable through bpf_get_attach_cookie() */486 __u64 bpf_cookie;487 /* don't use BPF link when attach BPF program */488 bool force_ioctl_attach;489 size_t :0;490};491#define bpf_perf_event_opts__last_field force_ioctl_attach492 493LIBBPF_API struct bpf_link *494bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);495 496LIBBPF_API struct bpf_link *497bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,498 const struct bpf_perf_event_opts *opts);499 500/**501 * enum probe_attach_mode - the mode to attach kprobe/uprobe502 *503 * force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will504 * be returned if it is not supported by the kernel.505 */506enum probe_attach_mode {507 /* attach probe in latest supported mode by kernel */508 PROBE_ATTACH_MODE_DEFAULT = 0,509 /* attach probe in legacy mode, using debugfs/tracefs */510 PROBE_ATTACH_MODE_LEGACY,511 /* create perf event with perf_event_open() syscall */512 PROBE_ATTACH_MODE_PERF,513 /* attach probe with BPF link */514 PROBE_ATTACH_MODE_LINK,515};516 517struct bpf_kprobe_opts {518 /* size of this struct, for forward/backward compatibility */519 size_t sz;520 /* custom user-provided value fetchable through bpf_get_attach_cookie() */521 __u64 bpf_cookie;522 /* function's offset to install kprobe to */523 size_t offset;524 /* kprobe is return probe */525 bool retprobe;526 /* kprobe attach mode */527 enum probe_attach_mode attach_mode;528 size_t :0;529};530#define bpf_kprobe_opts__last_field attach_mode531 532LIBBPF_API struct bpf_link *533bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,534 const char *func_name);535LIBBPF_API struct bpf_link *536bpf_program__attach_kprobe_opts(const struct bpf_program *prog,537 const char *func_name,538 const struct bpf_kprobe_opts *opts);539 540struct bpf_kprobe_multi_opts {541 /* size of this struct, for forward/backward compatibility */542 size_t sz;543 /* array of function symbols to attach */544 const char **syms;545 /* array of function addresses to attach */546 const unsigned long *addrs;547 /* array of user-provided values fetchable through bpf_get_attach_cookie */548 const __u64 *cookies;549 /* number of elements in syms/addrs/cookies arrays */550 size_t cnt;551 /* create return kprobes */552 bool retprobe;553 /* create session kprobes */554 bool session;555 size_t :0;556};557 558#define bpf_kprobe_multi_opts__last_field session559 560LIBBPF_API struct bpf_link *561bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,562 const char *pattern,563 const struct bpf_kprobe_multi_opts *opts);564 565struct bpf_uprobe_multi_opts {566 /* size of this struct, for forward/backward compatibility */567 size_t sz;568 /* array of function symbols to attach to */569 const char **syms;570 /* array of function addresses to attach to */571 const unsigned long *offsets;572 /* optional, array of associated ref counter offsets */573 const unsigned long *ref_ctr_offsets;574 /* optional, array of associated BPF cookies */575 const __u64 *cookies;576 /* number of elements in syms/addrs/cookies arrays */577 size_t cnt;578 /* create return uprobes */579 bool retprobe;580 size_t :0;581};582 583#define bpf_uprobe_multi_opts__last_field retprobe584 585/**586 * @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program587 * to multiple uprobes with uprobe_multi link.588 *589 * User can specify 2 mutually exclusive set of inputs:590 *591 * 1) use only path/func_pattern/pid arguments592 *593 * 2) use path/pid with allowed combinations of594 * syms/offsets/ref_ctr_offsets/cookies/cnt595 *596 * - syms and offsets are mutually exclusive597 * - ref_ctr_offsets and cookies are optional598 *599 *600 * @param prog BPF program to attach601 * @param pid Process ID to attach the uprobe to, 0 for self (own process),602 * -1 for all processes603 * @param binary_path Path to binary604 * @param func_pattern Regular expression to specify functions to attach605 * BPF program to606 * @param opts Additional options (see **struct bpf_uprobe_multi_opts**)607 * @return 0, on success; negative error code, otherwise608 */609LIBBPF_API struct bpf_link *610bpf_program__attach_uprobe_multi(const struct bpf_program *prog,611 pid_t pid,612 const char *binary_path,613 const char *func_pattern,614 const struct bpf_uprobe_multi_opts *opts);615 616struct bpf_ksyscall_opts {617 /* size of this struct, for forward/backward compatibility */618 size_t sz;619 /* custom user-provided value fetchable through bpf_get_attach_cookie() */620 __u64 bpf_cookie;621 /* attach as return probe? */622 bool retprobe;623 size_t :0;624};625#define bpf_ksyscall_opts__last_field retprobe626 627/**628 * @brief **bpf_program__attach_ksyscall()** attaches a BPF program629 * to kernel syscall handler of a specified syscall. Optionally it's possible630 * to request to install retprobe that will be triggered at syscall exit. It's631 * also possible to associate BPF cookie (though options).632 *633 * Libbpf automatically will determine correct full kernel function name,634 * which depending on system architecture and kernel version/configuration635 * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will636 * attach specified program using kprobe/kretprobe mechanism.637 *638 * **bpf_program__attach_ksyscall()** is an API counterpart of declarative639 * **SEC("ksyscall/<syscall>")** annotation of BPF programs.640 *641 * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do642 * not handle all the calling convention quirks for mmap(), clone() and compat643 * syscalls. It also only attaches to "native" syscall interfaces. If host644 * system supports compat syscalls or defines 32-bit syscalls in 64-bit645 * kernel, such syscall interfaces won't be attached to by libbpf.646 *647 * These limitations may or may not change in the future. Therefore it is648 * recommended to use SEC("kprobe") for these syscalls or if working with649 * compat and 32-bit interfaces is required.650 *651 * @param prog BPF program to attach652 * @param syscall_name Symbolic name of the syscall (e.g., "bpf")653 * @param opts Additional options (see **struct bpf_ksyscall_opts**)654 * @return Reference to the newly created BPF link; or NULL is returned on655 * error, error code is stored in errno656 */657LIBBPF_API struct bpf_link *658bpf_program__attach_ksyscall(const struct bpf_program *prog,659 const char *syscall_name,660 const struct bpf_ksyscall_opts *opts);661 662struct bpf_uprobe_opts {663 /* size of this struct, for forward/backward compatibility */664 size_t sz;665 /* offset of kernel reference counted USDT semaphore, added in666 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")667 */668 size_t ref_ctr_offset;669 /* custom user-provided value fetchable through bpf_get_attach_cookie() */670 __u64 bpf_cookie;671 /* uprobe is return probe, invoked at function return time */672 bool retprobe;673 /* Function name to attach to. Could be an unqualified ("abc") or library-qualified674 * "abc@LIBXYZ" name. To specify function entry, func_name should be set while675 * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an676 * offset within a function, specify func_name and use func_offset argument to specify677 * offset within the function. Shared library functions must specify the shared library678 * binary_path.679 */680 const char *func_name;681 /* uprobe attach mode */682 enum probe_attach_mode attach_mode;683 size_t :0;684};685#define bpf_uprobe_opts__last_field attach_mode686 687/**688 * @brief **bpf_program__attach_uprobe()** attaches a BPF program689 * to the userspace function which is found by binary path and690 * offset. You can optionally specify a particular process to attach691 * to. You can also optionally attach the program to the function692 * exit instead of entry.693 *694 * @param prog BPF program to attach695 * @param retprobe Attach to function exit696 * @param pid Process ID to attach the uprobe to, 0 for self (own process),697 * -1 for all processes698 * @param binary_path Path to binary that contains the function symbol699 * @param func_offset Offset within the binary of the function symbol700 * @return Reference to the newly created BPF link; or NULL is returned on error,701 * error code is stored in errno702 */703LIBBPF_API struct bpf_link *704bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,705 pid_t pid, const char *binary_path,706 size_t func_offset);707 708/**709 * @brief **bpf_program__attach_uprobe_opts()** is just like710 * bpf_program__attach_uprobe() except with a options struct711 * for various configurations.712 *713 * @param prog BPF program to attach714 * @param pid Process ID to attach the uprobe to, 0 for self (own process),715 * -1 for all processes716 * @param binary_path Path to binary that contains the function symbol717 * @param func_offset Offset within the binary of the function symbol718 * @param opts Options for altering program attachment719 * @return Reference to the newly created BPF link; or NULL is returned on error,720 * error code is stored in errno721 */722LIBBPF_API struct bpf_link *723bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,724 const char *binary_path, size_t func_offset,725 const struct bpf_uprobe_opts *opts);726 727struct bpf_usdt_opts {728 /* size of this struct, for forward/backward compatibility */729 size_t sz;730 /* custom user-provided value accessible through usdt_cookie() */731 __u64 usdt_cookie;732 size_t :0;733};734#define bpf_usdt_opts__last_field usdt_cookie735 736/**737 * @brief **bpf_program__attach_usdt()** is just like738 * bpf_program__attach_uprobe_opts() except it covers USDT (User-space739 * Statically Defined Tracepoint) attachment, instead of attaching to740 * user-space function entry or exit.741 *742 * @param prog BPF program to attach743 * @param pid Process ID to attach the uprobe to, 0 for self (own process),744 * -1 for all processes745 * @param binary_path Path to binary that contains provided USDT probe746 * @param usdt_provider USDT provider name747 * @param usdt_name USDT probe name748 * @param opts Options for altering program attachment749 * @return Reference to the newly created BPF link; or NULL is returned on error,750 * error code is stored in errno751 */752LIBBPF_API struct bpf_link *753bpf_program__attach_usdt(const struct bpf_program *prog,754 pid_t pid, const char *binary_path,755 const char *usdt_provider, const char *usdt_name,756 const struct bpf_usdt_opts *opts);757 758struct bpf_tracepoint_opts {759 /* size of this struct, for forward/backward compatibility */760 size_t sz;761 /* custom user-provided value fetchable through bpf_get_attach_cookie() */762 __u64 bpf_cookie;763};764#define bpf_tracepoint_opts__last_field bpf_cookie765 766LIBBPF_API struct bpf_link *767bpf_program__attach_tracepoint(const struct bpf_program *prog,768 const char *tp_category,769 const char *tp_name);770LIBBPF_API struct bpf_link *771bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,772 const char *tp_category,773 const char *tp_name,774 const struct bpf_tracepoint_opts *opts);775 776struct bpf_raw_tracepoint_opts {777 size_t sz; /* size of this struct for forward/backward compatibility */778 __u64 cookie;779 size_t :0;780};781#define bpf_raw_tracepoint_opts__last_field cookie782 783LIBBPF_API struct bpf_link *784bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,785 const char *tp_name);786LIBBPF_API struct bpf_link *787bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog,788 const char *tp_name,789 struct bpf_raw_tracepoint_opts *opts);790 791struct bpf_trace_opts {792 /* size of this struct, for forward/backward compatibility */793 size_t sz;794 /* custom user-provided value fetchable through bpf_get_attach_cookie() */795 __u64 cookie;796};797#define bpf_trace_opts__last_field cookie798 799LIBBPF_API struct bpf_link *800bpf_program__attach_trace(const struct bpf_program *prog);801LIBBPF_API struct bpf_link *802bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);803 804LIBBPF_API struct bpf_link *805bpf_program__attach_lsm(const struct bpf_program *prog);806LIBBPF_API struct bpf_link *807bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);808LIBBPF_API struct bpf_link *809bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);810LIBBPF_API struct bpf_link *811bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd);812LIBBPF_API struct bpf_link *813bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);814LIBBPF_API struct bpf_link *815bpf_program__attach_freplace(const struct bpf_program *prog,816 int target_fd, const char *attach_func_name);817 818struct bpf_netfilter_opts {819 /* size of this struct, for forward/backward compatibility */820 size_t sz;821 822 __u32 pf;823 __u32 hooknum;824 __s32 priority;825 __u32 flags;826};827#define bpf_netfilter_opts__last_field flags828 829LIBBPF_API struct bpf_link *830bpf_program__attach_netfilter(const struct bpf_program *prog,831 const struct bpf_netfilter_opts *opts);832 833struct bpf_tcx_opts {834 /* size of this struct, for forward/backward compatibility */835 size_t sz;836 __u32 flags;837 __u32 relative_fd;838 __u32 relative_id;839 __u64 expected_revision;840 size_t :0;841};842#define bpf_tcx_opts__last_field expected_revision843 844LIBBPF_API struct bpf_link *845bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,846 const struct bpf_tcx_opts *opts);847 848struct bpf_netkit_opts {849 /* size of this struct, for forward/backward compatibility */850 size_t sz;851 __u32 flags;852 __u32 relative_fd;853 __u32 relative_id;854 __u64 expected_revision;855 size_t :0;856};857#define bpf_netkit_opts__last_field expected_revision858 859LIBBPF_API struct bpf_link *860bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,861 const struct bpf_netkit_opts *opts);862 863struct bpf_map;864 865LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);866LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);867 868struct bpf_iter_attach_opts {869 size_t sz; /* size of this struct for forward/backward compatibility */870 union bpf_iter_link_info *link_info;871 __u32 link_info_len;872};873#define bpf_iter_attach_opts__last_field link_info_len874 875LIBBPF_API struct bpf_link *876bpf_program__attach_iter(const struct bpf_program *prog,877 const struct bpf_iter_attach_opts *opts);878 879LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);880 881/**882 * @brief **bpf_program__set_type()** sets the program883 * type of the passed BPF program.884 * @param prog BPF program to set the program type for885 * @param type program type to set the BPF map to have886 * @return error code; or 0 if no error. An error occurs887 * if the object is already loaded.888 *889 * This must be called before the BPF object is loaded,890 * otherwise it has no effect and an error is returned.891 */892LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,893 enum bpf_prog_type type);894 895LIBBPF_API enum bpf_attach_type896bpf_program__expected_attach_type(const struct bpf_program *prog);897 898/**899 * @brief **bpf_program__set_expected_attach_type()** sets the900 * attach type of the passed BPF program. This is used for901 * auto-detection of attachment when programs are loaded.902 * @param prog BPF program to set the attach type for903 * @param type attach type to set the BPF map to have904 * @return error code; or 0 if no error. An error occurs905 * if the object is already loaded.906 *907 * This must be called before the BPF object is loaded,908 * otherwise it has no effect and an error is returned.909 */910LIBBPF_API int911bpf_program__set_expected_attach_type(struct bpf_program *prog,912 enum bpf_attach_type type);913 914LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);915LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);916 917/* Per-program log level and log buffer getters/setters.918 * See bpf_object_open_opts comments regarding log_level and log_buf919 * interactions.920 */921LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);922LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);923LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);924LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);925 926/**927 * @brief **bpf_program__set_attach_target()** sets BTF-based attach target928 * for supported BPF program types:929 * - BTF-aware raw tracepoints (tp_btf);930 * - fentry/fexit/fmod_ret;931 * - lsm;932 * - freplace.933 * @param prog BPF program to set the attach type for934 * @param type attach type to set the BPF map to have935 * @return error code; or 0 if no error occurred.936 */937LIBBPF_API int938bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,939 const char *attach_func_name);940 941/**942 * @brief **bpf_object__find_map_by_name()** returns BPF map of943 * the given name, if it exists within the passed BPF object944 * @param obj BPF object945 * @param name name of the BPF map946 * @return BPF map instance, if such map exists within the BPF object;947 * or NULL otherwise.948 */949LIBBPF_API struct bpf_map *950bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);951 952LIBBPF_API int953bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);954 955LIBBPF_API struct bpf_map *956bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);957 958#define bpf_object__for_each_map(pos, obj) \959 for ((pos) = bpf_object__next_map((obj), NULL); \960 (pos) != NULL; \961 (pos) = bpf_object__next_map((obj), (pos)))962#define bpf_map__for_each bpf_object__for_each_map963 964LIBBPF_API struct bpf_map *965bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);966 967/**968 * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create969 * BPF map during BPF object load phase.970 * @param map the BPF map instance971 * @param autocreate whether to create BPF map during BPF object load972 * @return 0 on success; -EBUSY if BPF object was already loaded973 *974 * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating975 * BPF map. By default, libbpf will attempt to create every single BPF map976 * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall977 * and fill in map FD in BPF instructions.978 *979 * This API allows to opt-out of this process for specific map instance. This980 * can be useful if host kernel doesn't support such BPF map type or used981 * combination of flags and user application wants to avoid creating such982 * a map in the first place. User is still responsible to make sure that their983 * BPF-side code that expects to use such missing BPF map is recognized by BPF984 * verifier as dead code, otherwise BPF verifier will reject such BPF program.985 */986LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);987LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);988 989/**990 * @brief **bpf_map__set_autoattach()** sets whether libbpf has to auto-attach991 * map during BPF skeleton attach phase.992 * @param map the BPF map instance993 * @param autoattach whether to attach map during BPF skeleton attach phase994 * @return 0 on success; negative error code, otherwise995 */996LIBBPF_API int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach);997 998/**999 * @brief **bpf_map__autoattach()** returns whether BPF map is configured to1000 * auto-attach during BPF skeleton attach phase.1001 * @param map the BPF map instance1002 * @return true if map is set to auto-attach during skeleton attach phase; false, otherwise1003 */1004LIBBPF_API bool bpf_map__autoattach(const struct bpf_map *map);1005 1006/**1007 * @brief **bpf_map__fd()** gets the file descriptor of the passed1008 * BPF map1009 * @param map the BPF map instance1010 * @return the file descriptor; or -EINVAL in case of an error1011 */1012LIBBPF_API int bpf_map__fd(const struct bpf_map *map);1013LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);1014/* get map name */1015LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);1016/* get/set map type */1017LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);1018LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);1019/* get/set map size (max_entries) */1020LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);1021LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);1022/* get/set map flags */1023LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);1024LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);1025/* get/set map NUMA node */1026LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);1027LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);1028/* get/set map key size */1029LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);1030LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);1031/* get map value size */1032LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);1033/**1034 * @brief **bpf_map__set_value_size()** sets map value size.1035 * @param map the BPF map instance1036 * @return 0, on success; negative error, otherwise1037 *1038 * There is a special case for maps with associated memory-mapped regions, like1039 * the global data section maps (bss, data, rodata). When this function is used1040 * on such a map, the mapped region is resized. Afterward, an attempt is made to1041 * adjust the corresponding BTF info. This attempt is best-effort and can only1042 * succeed if the last variable of the data section map is an array. The array1043 * BTF type is replaced by a new BTF array type with a different length.1044 * Any previously existing pointers returned from bpf_map__initial_value() or1045 * corresponding data section skeleton pointer must be reinitialized.1046 */1047LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);1048/* get map key/value BTF type IDs */1049LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);1050LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);1051/* get/set map if_index */1052LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);1053LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);1054/* get/set map map_extra flags */1055LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);1056LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);1057 1058LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,1059 const void *data, size_t size);1060LIBBPF_API void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize);1061 1062/**1063 * @brief **bpf_map__is_internal()** tells the caller whether or not the1064 * passed map is a special map created by libbpf automatically for things like1065 * global variables, __ksym externs, Kconfig values, etc1066 * @param map the bpf_map1067 * @return true, if the map is an internal map; false, otherwise1068 */1069LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);1070 1071/**1072 * @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the1073 * BPF map should be pinned. This does not actually create the 'pin'.1074 * @param map The bpf_map1075 * @param path The path1076 * @return 0, on success; negative error, otherwise1077 */1078LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);1079 1080/**1081 * @brief **bpf_map__pin_path()** gets the path attribute that tells where the1082 * BPF map should be pinned.1083 * @param map The bpf_map1084 * @return The path string; which can be NULL1085 */1086LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);1087 1088/**1089 * @brief **bpf_map__is_pinned()** tells the caller whether or not the1090 * passed map has been pinned via a 'pin' file.1091 * @param map The bpf_map1092 * @return true, if the map is pinned; false, otherwise1093 */1094LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);1095 1096/**1097 * @brief **bpf_map__pin()** creates a file that serves as a 'pin'1098 * for the BPF map. This increments the reference count on the1099 * BPF map which will keep the BPF map loaded even after the1100 * userspace process which loaded it has exited.1101 * @param map The bpf_map to pin1102 * @param path A file path for the 'pin'1103 * @return 0, on success; negative error, otherwise1104 *1105 * If `path` is NULL the maps `pin_path` attribute will be used. If this is1106 * also NULL, an error will be returned and the map will not be pinned.1107 */1108LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);1109 1110/**1111 * @brief **bpf_map__unpin()** removes the file that serves as a1112 * 'pin' for the BPF map.1113 * @param map The bpf_map to unpin1114 * @param path A file path for the 'pin'1115 * @return 0, on success; negative error, otherwise1116 *1117 * The `path` parameter can be NULL, in which case the `pin_path`1118 * map attribute is unpinned. If both the `path` parameter and1119 * `pin_path` map attribute are set, they must be equal.1120 */1121LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);1122 1123LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);1124LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);1125 1126/**1127 * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value1128 * corresponding to provided key.1129 * @param map BPF map to lookup element in1130 * @param key pointer to memory containing bytes of the key used for lookup1131 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1132 * @param value pointer to memory in which looked up value will be stored1133 * @param value_sz size in byte of value data memory; it has to match BPF map1134 * definition's **value_size**. For per-CPU BPF maps value size has to be1135 * a product of BPF map value size and number of possible CPUs in the system1136 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1137 * per-CPU values value size has to be aligned up to closest 8 bytes for1138 * alignment reasons, so expected size is: `round_up(value_size, 8)1139 * * libbpf_num_possible_cpus()`.1140 * @flags extra flags passed to kernel for this operation1141 * @return 0, on success; negative error, otherwise1142 *1143 * **bpf_map__lookup_elem()** is high-level equivalent of1144 * **bpf_map_lookup_elem()** API with added check for key and value size.1145 */1146LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,1147 const void *key, size_t key_sz,1148 void *value, size_t value_sz, __u64 flags);1149 1150/**1151 * @brief **bpf_map__update_elem()** allows to insert or update value in BPF1152 * map that corresponds to provided key.1153 * @param map BPF map to insert to or update element in1154 * @param key pointer to memory containing bytes of the key1155 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1156 * @param value pointer to memory containing bytes of the value1157 * @param value_sz size in byte of value data memory; it has to match BPF map1158 * definition's **value_size**. For per-CPU BPF maps value size has to be1159 * a product of BPF map value size and number of possible CPUs in the system1160 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1161 * per-CPU values value size has to be aligned up to closest 8 bytes for1162 * alignment reasons, so expected size is: `round_up(value_size, 8)1163 * * libbpf_num_possible_cpus()`.1164 * @flags extra flags passed to kernel for this operation1165 * @return 0, on success; negative error, otherwise1166 *1167 * **bpf_map__update_elem()** is high-level equivalent of1168 * **bpf_map_update_elem()** API with added check for key and value size.1169 */1170LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,1171 const void *key, size_t key_sz,1172 const void *value, size_t value_sz, __u64 flags);1173 1174/**1175 * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that1176 * corresponds to provided key.1177 * @param map BPF map to delete element from1178 * @param key pointer to memory containing bytes of the key1179 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1180 * @flags extra flags passed to kernel for this operation1181 * @return 0, on success; negative error, otherwise1182 *1183 * **bpf_map__delete_elem()** is high-level equivalent of1184 * **bpf_map_delete_elem()** API with added check for key size.1185 */1186LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,1187 const void *key, size_t key_sz, __u64 flags);1188 1189/**1190 * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value1191 * corresponding to provided key and atomically delete it afterwards.1192 * @param map BPF map to lookup element in1193 * @param key pointer to memory containing bytes of the key used for lookup1194 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1195 * @param value pointer to memory in which looked up value will be stored1196 * @param value_sz size in byte of value data memory; it has to match BPF map1197 * definition's **value_size**. For per-CPU BPF maps value size has to be1198 * a product of BPF map value size and number of possible CPUs in the system1199 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1200 * per-CPU values value size has to be aligned up to closest 8 bytes for1201 * alignment reasons, so expected size is: `round_up(value_size, 8)1202 * * libbpf_num_possible_cpus()`.1203 * @flags extra flags passed to kernel for this operation1204 * @return 0, on success; negative error, otherwise1205 *1206 * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of1207 * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.1208 */1209LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,1210 const void *key, size_t key_sz,1211 void *value, size_t value_sz, __u64 flags);1212 1213/**1214 * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by1215 * fetching next key that follows current key.1216 * @param map BPF map to fetch next key from1217 * @param cur_key pointer to memory containing bytes of current key or NULL to1218 * fetch the first key1219 * @param next_key pointer to memory to write next key into1220 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1221 * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;1222 * negative error, otherwise1223 *1224 * **bpf_map__get_next_key()** is high-level equivalent of1225 * **bpf_map_get_next_key()** API with added check for key size.1226 */1227LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,1228 const void *cur_key, void *next_key, size_t key_sz);1229 1230struct bpf_xdp_set_link_opts {1231 size_t sz;1232 int old_fd;1233 size_t :0;1234};1235#define bpf_xdp_set_link_opts__last_field old_fd1236 1237struct bpf_xdp_attach_opts {1238 size_t sz;1239 int old_prog_fd;1240 size_t :0;1241};1242#define bpf_xdp_attach_opts__last_field old_prog_fd1243 1244struct bpf_xdp_query_opts {1245 size_t sz;1246 __u32 prog_id; /* output */1247 __u32 drv_prog_id; /* output */1248 __u32 hw_prog_id; /* output */1249 __u32 skb_prog_id; /* output */1250 __u8 attach_mode; /* output */1251 __u64 feature_flags; /* output */1252 __u32 xdp_zc_max_segs; /* output */1253 size_t :0;1254};1255#define bpf_xdp_query_opts__last_field xdp_zc_max_segs1256 1257LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,1258 const struct bpf_xdp_attach_opts *opts);1259LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,1260 const struct bpf_xdp_attach_opts *opts);1261LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);1262LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);1263 1264/* TC related API */1265enum bpf_tc_attach_point {1266 BPF_TC_INGRESS = 1 << 0,1267 BPF_TC_EGRESS = 1 << 1,1268 BPF_TC_CUSTOM = 1 << 2,1269};1270 1271#define BPF_TC_PARENT(a, b) \1272 ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))1273 1274enum bpf_tc_flags {1275 BPF_TC_F_REPLACE = 1 << 0,1276};1277 1278struct bpf_tc_hook {1279 size_t sz;1280 int ifindex;1281 enum bpf_tc_attach_point attach_point;1282 __u32 parent;1283 size_t :0;1284};1285#define bpf_tc_hook__last_field parent1286 1287struct bpf_tc_opts {1288 size_t sz;1289 int prog_fd;1290 __u32 flags;1291 __u32 prog_id;1292 __u32 handle;1293 __u32 priority;1294 size_t :0;1295};1296#define bpf_tc_opts__last_field priority1297 1298LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);1299LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);1300LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,1301 struct bpf_tc_opts *opts);1302LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,1303 const struct bpf_tc_opts *opts);1304LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,1305 struct bpf_tc_opts *opts);1306 1307/* Ring buffer APIs */1308struct ring_buffer;1309struct ring;1310struct user_ring_buffer;1311 1312typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);1313 1314struct ring_buffer_opts {1315 size_t sz; /* size of this struct, for forward/backward compatibility */1316};1317 1318#define ring_buffer_opts__last_field sz1319 1320LIBBPF_API struct ring_buffer *1321ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,1322 const struct ring_buffer_opts *opts);1323LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);1324LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,1325 ring_buffer_sample_fn sample_cb, void *ctx);1326LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);1327LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);1328LIBBPF_API int ring_buffer__consume_n(struct ring_buffer *rb, size_t n);1329LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);1330 1331/**1332 * @brief **ring_buffer__ring()** returns the ringbuffer object inside a given1333 * ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance.1334 *1335 * @param rb A ringbuffer manager object.1336 * @param idx An index into the ringbuffers contained within the ringbuffer1337 * manager object. The index is 0-based and corresponds to the order in which1338 * ring_buffer__add was called.1339 * @return A ringbuffer object on success; NULL and errno set if the index is1340 * invalid.1341 */1342LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,1343 unsigned int idx);1344 1345/**1346 * @brief **ring__consumer_pos()** returns the current consumer position in the1347 * given ringbuffer.1348 *1349 * @param r A ringbuffer object.1350 * @return The current consumer position.1351 */1352LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);1353 1354/**1355 * @brief **ring__producer_pos()** returns the current producer position in the1356 * given ringbuffer.1357 *1358 * @param r A ringbuffer object.1359 * @return The current producer position.1360 */1361LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);1362 1363/**1364 * @brief **ring__avail_data_size()** returns the number of bytes in the1365 * ringbuffer not yet consumed. This has no locking associated with it, so it1366 * can be inaccurate if operations are ongoing while this is called. However, it1367 * should still show the correct trend over the long-term.1368 *1369 * @param r A ringbuffer object.1370 * @return The number of bytes not yet consumed.1371 */1372LIBBPF_API size_t ring__avail_data_size(const struct ring *r);1373 1374/**1375 * @brief **ring__size()** returns the total size of the ringbuffer's map data1376 * area (excluding special producer/consumer pages). Effectively this gives the1377 * amount of usable bytes of data inside the ringbuffer.1378 *1379 * @param r A ringbuffer object.1380 * @return The total size of the ringbuffer map data area.1381 */1382LIBBPF_API size_t ring__size(const struct ring *r);1383 1384/**1385 * @brief **ring__map_fd()** returns the file descriptor underlying the given1386 * ringbuffer.1387 *1388 * @param r A ringbuffer object.1389 * @return The underlying ringbuffer file descriptor1390 */1391LIBBPF_API int ring__map_fd(const struct ring *r);1392 1393/**1394 * @brief **ring__consume()** consumes available ringbuffer data without event1395 * polling.1396 *1397 * @param r A ringbuffer object.1398 * @return The number of records consumed (or INT_MAX, whichever is less), or1399 * a negative number if any of the callbacks return an error.1400 */1401LIBBPF_API int ring__consume(struct ring *r);1402 1403/**1404 * @brief **ring__consume_n()** consumes up to a requested amount of items from1405 * a ringbuffer without event polling.1406 *1407 * @param r A ringbuffer object.1408 * @param n Maximum amount of items to consume.1409 * @return The number of items consumed, or a negative number if any of the1410 * callbacks return an error.1411 */1412LIBBPF_API int ring__consume_n(struct ring *r, size_t n);1413 1414struct user_ring_buffer_opts {1415 size_t sz; /* size of this struct, for forward/backward compatibility */1416};1417 1418#define user_ring_buffer_opts__last_field sz1419 1420/**1421 * @brief **user_ring_buffer__new()** creates a new instance of a user ring1422 * buffer.1423 *1424 * @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.1425 * @param opts Options for how the ring buffer should be created.1426 * @return A user ring buffer on success; NULL and errno being set on a1427 * failure.1428 */1429LIBBPF_API struct user_ring_buffer *1430user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);1431 1432/**1433 * @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the1434 * user ring buffer.1435 * @param rb A pointer to a user ring buffer.1436 * @param size The size of the sample, in bytes.1437 * @return A pointer to an 8-byte aligned reserved region of the user ring1438 * buffer; NULL, and errno being set if a sample could not be reserved.1439 *1440 * This function is *not* thread safe, and callers must synchronize accessing1441 * this function if there are multiple producers. If a size is requested that1442 * is larger than the size of the entire ring buffer, errno will be set to1443 * E2BIG and NULL is returned. If the ring buffer could accommodate the size,1444 * but currently does not have enough space, errno is set to ENOSPC and NULL is1445 * returned.1446 *1447 * After initializing the sample, callers must invoke1448 * **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,1449 * the sample must be freed with **user_ring_buffer__discard()**.1450 */1451LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);1452 1453/**1454 * @brief **user_ring_buffer__reserve_blocking()** reserves a record in the1455 * ring buffer, possibly blocking for up to @timeout_ms until a sample becomes1456 * available.1457 * @param rb The user ring buffer.1458 * @param size The size of the sample, in bytes.1459 * @param timeout_ms The amount of time, in milliseconds, for which the caller1460 * should block when waiting for a sample. -1 causes the caller to block1461 * indefinitely.1462 * @return A pointer to an 8-byte aligned reserved region of the user ring1463 * buffer; NULL, and errno being set if a sample could not be reserved.1464 *1465 * This function is *not* thread safe, and callers must synchronize1466 * accessing this function if there are multiple producers1467 *1468 * If **timeout_ms** is -1, the function will block indefinitely until a sample1469 * becomes available. Otherwise, **timeout_ms** must be non-negative, or errno1470 * is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking1471 * will occur and the function will return immediately after attempting to1472 * reserve a sample.1473 *1474 * If **size** is larger than the size of the entire ring buffer, errno is set1475 * to E2BIG and NULL is returned. If the ring buffer could accommodate1476 * **size**, but currently does not have enough space, the caller will block1477 * until at most **timeout_ms** has elapsed. If insufficient space is available1478 * at that time, errno is set to ENOSPC, and NULL is returned.1479 *1480 * The kernel guarantees that it will wake up this thread to check if1481 * sufficient space is available in the ring buffer at least once per1482 * invocation of the **bpf_ringbuf_drain()** helper function, provided that at1483 * least one sample is consumed, and the BPF program did not invoke the1484 * function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the1485 * kernel does not guarantee this. If the helper function is invoked with1486 * BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is1487 * consumed.1488 *1489 * When a sample of size **size** is found within **timeout_ms**, a pointer to1490 * the sample is returned. After initializing the sample, callers must invoke1491 * **user_ring_buffer__submit()** to post the sample to the ring buffer.1492 * Otherwise, the sample must be freed with **user_ring_buffer__discard()**.1493 */1494LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,1495 __u32 size,1496 int timeout_ms);1497 1498/**1499 * @brief **user_ring_buffer__submit()** submits a previously reserved sample1500 * into the ring buffer.1501 * @param rb The user ring buffer.1502 * @param sample A reserved sample.1503 *1504 * It is not necessary to synchronize amongst multiple producers when invoking1505 * this function.1506 */1507LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);1508 1509/**1510 * @brief **user_ring_buffer__discard()** discards a previously reserved sample.1511 * @param rb The user ring buffer.1512 * @param sample A reserved sample.1513 *1514 * It is not necessary to synchronize amongst multiple producers when invoking1515 * this function.1516 */1517LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);1518 1519/**1520 * @brief **user_ring_buffer__free()** frees a ring buffer that was previously1521 * created with **user_ring_buffer__new()**.1522 * @param rb The user ring buffer being freed.1523 */1524LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);1525 1526/* Perf buffer APIs */1527struct perf_buffer;1528 1529typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,1530 void *data, __u32 size);1531typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);1532 1533/* common use perf buffer options */1534struct perf_buffer_opts {1535 size_t sz;1536 __u32 sample_period;1537 size_t :0;1538};1539#define perf_buffer_opts__last_field sample_period1540 1541/**1542 * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified1543 * BPF_PERF_EVENT_ARRAY map1544 * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF1545 * code to send data over to user-space1546 * @param page_cnt number of memory pages allocated for each per-CPU buffer1547 * @param sample_cb function called on each received data record1548 * @param lost_cb function called when record loss has occurred1549 * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*1550 * @return a new instance of struct perf_buffer on success, NULL on error with1551 * *errno* containing an error code1552 */1553LIBBPF_API struct perf_buffer *1554perf_buffer__new(int map_fd, size_t page_cnt,1555 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,1556 const struct perf_buffer_opts *opts);1557 1558enum bpf_perf_event_ret {1559 LIBBPF_PERF_EVENT_DONE = 0,1560 LIBBPF_PERF_EVENT_ERROR = -1,1561 LIBBPF_PERF_EVENT_CONT = -2,1562};1563 1564struct perf_event_header;1565 1566typedef enum bpf_perf_event_ret1567(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);1568 1569/* raw perf buffer options, giving most power and control */1570struct perf_buffer_raw_opts {1571 size_t sz;1572 long :0;1573 long :0;1574 /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of1575 * max_entries of given PERF_EVENT_ARRAY map)1576 */1577 int cpu_cnt;1578 /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */1579 int *cpus;1580 /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */1581 int *map_keys;1582};1583#define perf_buffer_raw_opts__last_field map_keys1584 1585struct perf_event_attr;1586 1587LIBBPF_API struct perf_buffer *1588perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,1589 perf_buffer_event_fn event_cb, void *ctx,1590 const struct perf_buffer_raw_opts *opts);1591 1592LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);1593LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);1594LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);1595LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);1596LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);1597LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);1598LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);1599/**1600 * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying1601 * memory region of the ring buffer.1602 * This ring buffer can be used to implement a custom events consumer.1603 * The ring buffer starts with the *struct perf_event_mmap_page*, which1604 * holds the ring buffer management fields, when accessing the header1605 * structure it's important to be SMP aware.1606 * You can refer to *perf_event_read_simple* for a simple example.1607 * @param pb the perf buffer structure1608 * @param buf_idx the buffer index to retrieve1609 * @param buf (out) gets the base pointer of the mmap()'ed memory1610 * @param buf_size (out) gets the size of the mmap()'ed region1611 * @return 0 on success, negative error code for failure1612 */1613LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,1614 size_t *buf_size);1615 1616struct bpf_prog_linfo;1617struct bpf_prog_info;1618 1619LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);1620LIBBPF_API struct bpf_prog_linfo *1621bpf_prog_linfo__new(const struct bpf_prog_info *info);1622LIBBPF_API const struct bpf_line_info *1623bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,1624 __u64 addr, __u32 func_idx, __u32 nr_skip);1625LIBBPF_API const struct bpf_line_info *1626bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,1627 __u32 insn_off, __u32 nr_skip);1628 1629/*1630 * Probe for supported system features1631 *1632 * Note that running many of these probes in a short amount of time can cause1633 * the kernel to reach the maximal size of lockable memory allowed for the1634 * user, causing subsequent probes to fail. In this case, the caller may want1635 * to adjust that limit with setrlimit().1636 */1637 1638/**1639 * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports1640 * BPF programs of a given type.1641 * @param prog_type BPF program type to detect kernel support for1642 * @param opts reserved for future extensibility, should be NULL1643 * @return 1, if given program type is supported; 0, if given program type is1644 * not supported; negative error code if feature detection failed or can't be1645 * performed1646 *1647 * Make sure the process has required set of CAP_* permissions (or runs as1648 * root) when performing feature checking.1649 */1650LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);1651/**1652 * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports1653 * BPF maps of a given type.1654 * @param map_type BPF map type to detect kernel support for1655 * @param opts reserved for future extensibility, should be NULL1656 * @return 1, if given map type is supported; 0, if given map type is1657 * not supported; negative error code if feature detection failed or can't be1658 * performed1659 *1660 * Make sure the process has required set of CAP_* permissions (or runs as1661 * root) when performing feature checking.1662 */1663LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);1664/**1665 * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the1666 * use of a given BPF helper from specified BPF program type.1667 * @param prog_type BPF program type used to check the support of BPF helper1668 * @param helper_id BPF helper ID (enum bpf_func_id) to check support for1669 * @param opts reserved for future extensibility, should be NULL1670 * @return 1, if given combination of program type and helper is supported; 0,1671 * if the combination is not supported; negative error code if feature1672 * detection for provided input arguments failed or can't be performed1673 *1674 * Make sure the process has required set of CAP_* permissions (or runs as1675 * root) when performing feature checking.1676 */1677LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,1678 enum bpf_func_id helper_id, const void *opts);1679 1680/**1681 * @brief **libbpf_num_possible_cpus()** is a helper function to get the1682 * number of possible CPUs that the host kernel supports and expects.1683 * @return number of possible CPUs; or error code on failure1684 *1685 * Example usage:1686 *1687 * int ncpus = libbpf_num_possible_cpus();1688 * if (ncpus < 0) {1689 * // error handling1690 * }1691 * long values[ncpus];1692 * bpf_map_lookup_elem(per_cpu_map_fd, key, values);1693 */1694LIBBPF_API int libbpf_num_possible_cpus(void);1695 1696struct bpf_map_skeleton {1697 const char *name;1698 struct bpf_map **map;1699 void **mmaped;1700 struct bpf_link **link;1701};1702 1703struct bpf_prog_skeleton {1704 const char *name;1705 struct bpf_program **prog;1706 struct bpf_link **link;1707};1708 1709struct bpf_object_skeleton {1710 size_t sz; /* size of this struct, for forward/backward compatibility */1711 1712 const char *name;1713 const void *data;1714 size_t data_sz;1715 1716 struct bpf_object **obj;1717 1718 int map_cnt;1719 int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */1720 struct bpf_map_skeleton *maps;1721 1722 int prog_cnt;1723 int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */1724 struct bpf_prog_skeleton *progs;1725};1726 1727LIBBPF_API int1728bpf_object__open_skeleton(struct bpf_object_skeleton *s,1729 const struct bpf_object_open_opts *opts);1730LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);1731LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);1732LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);1733LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);1734 1735struct bpf_var_skeleton {1736 const char *name;1737 struct bpf_map **map;1738 void **addr;1739};1740 1741struct bpf_object_subskeleton {1742 size_t sz; /* size of this struct, for forward/backward compatibility */1743 1744 const struct bpf_object *obj;1745 1746 int map_cnt;1747 int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */1748 struct bpf_map_skeleton *maps;1749 1750 int prog_cnt;1751 int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */1752 struct bpf_prog_skeleton *progs;1753 1754 int var_cnt;1755 int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */1756 struct bpf_var_skeleton *vars;1757};1758 1759LIBBPF_API int1760bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);1761LIBBPF_API void1762bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);1763 1764struct gen_loader_opts {1765 size_t sz; /* size of this struct, for forward/backward compatibility */1766 const char *data;1767 const char *insns;1768 __u32 data_sz;1769 __u32 insns_sz;1770};1771 1772#define gen_loader_opts__last_field insns_sz1773LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,1774 struct gen_loader_opts *opts);1775 1776enum libbpf_tristate {1777 TRI_NO = 0,1778 TRI_YES = 1,1779 TRI_MODULE = 2,1780};1781 1782struct bpf_linker_opts {1783 /* size of this struct, for forward/backward compatibility */1784 size_t sz;1785};1786#define bpf_linker_opts__last_field sz1787 1788struct bpf_linker_file_opts {1789 /* size of this struct, for forward/backward compatibility */1790 size_t sz;1791};1792#define bpf_linker_file_opts__last_field sz1793 1794struct bpf_linker;1795 1796LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);1797LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,1798 const char *filename,1799 const struct bpf_linker_file_opts *opts);1800LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);1801LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);1802 1803/*1804 * Custom handling of BPF program's SEC() definitions1805 */1806 1807struct bpf_prog_load_opts; /* defined in bpf.h */1808 1809/* Called during bpf_object__open() for each recognized BPF program. Callback1810 * can use various bpf_program__set_*() setters to adjust whatever properties1811 * are necessary.1812 */1813typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);1814 1815/* Called right before libbpf performs bpf_prog_load() to load BPF program1816 * into the kernel. Callback can adjust opts as necessary.1817 */1818typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,1819 struct bpf_prog_load_opts *opts, long cookie);1820 1821/* Called during skeleton attach or through bpf_program__attach(). If1822 * auto-attach is not supported, callback should return 0 and set link to1823 * NULL (it's not considered an error during skeleton attach, but it will be1824 * an error for bpf_program__attach() calls). On error, error should be1825 * returned directly and link set to NULL. On success, return 0 and set link1826 * to a valid struct bpf_link.1827 */1828typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,1829 struct bpf_link **link);1830 1831struct libbpf_prog_handler_opts {1832 /* size of this struct, for forward/backward compatibility */1833 size_t sz;1834 /* User-provided value that is passed to prog_setup_fn,1835 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to1836 * register one set of callbacks for multiple SEC() definitions and1837 * still be able to distinguish them, if necessary. For example,1838 * libbpf itself is using this to pass necessary flags (e.g.,1839 * sleepable flag) to a common internal SEC() handler.1840 */1841 long cookie;1842 /* BPF program initialization callback (see libbpf_prog_setup_fn_t).1843 * Callback is optional, pass NULL if it's not necessary.1844 */1845 libbpf_prog_setup_fn_t prog_setup_fn;1846 /* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).1847 * Callback is optional, pass NULL if it's not necessary.1848 */1849 libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;1850 /* BPF program attach callback (see libbpf_prog_attach_fn_t).1851 * Callback is optional, pass NULL if it's not necessary.1852 */1853 libbpf_prog_attach_fn_t prog_attach_fn;1854};1855#define libbpf_prog_handler_opts__last_field prog_attach_fn1856 1857/**1858 * @brief **libbpf_register_prog_handler()** registers a custom BPF program1859 * SEC() handler.1860 * @param sec section prefix for which custom handler is registered1861 * @param prog_type BPF program type associated with specified section1862 * @param exp_attach_type Expected BPF attach type associated with specified section1863 * @param opts optional cookie, callbacks, and other extra options1864 * @return Non-negative handler ID is returned on success. This handler ID has1865 * to be passed to *libbpf_unregister_prog_handler()* to unregister such1866 * custom handler. Negative error code is returned on error.1867 *1868 * *sec* defines which SEC() definitions are handled by this custom handler1869 * registration. *sec* can have few different forms:1870 * - if *sec* is just a plain string (e.g., "abc"), it will match only1871 * SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result1872 * in an error;1873 * - if *sec* is of the form "abc/", proper SEC() form is1874 * SEC("abc/something"), where acceptable "something" should be checked by1875 * *prog_init_fn* callback, if there are additional restrictions;1876 * - if *sec* is of the form "abc+", it will successfully match both1877 * SEC("abc") and SEC("abc/whatever") forms;1878 * - if *sec* is NULL, custom handler is registered for any BPF program that1879 * doesn't match any of the registered (custom or libbpf's own) SEC()1880 * handlers. There could be only one such generic custom handler registered1881 * at any given time.1882 *1883 * All custom handlers (except the one with *sec* == NULL) are processed1884 * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's1885 * SEC() handlers by registering custom ones for the same section prefix1886 * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")1887 * handler).1888 *1889 * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),1890 * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs1891 * to ensure synchronization if there is a risk of running this API from1892 * multiple threads simultaneously.1893 */1894LIBBPF_API int libbpf_register_prog_handler(const char *sec,1895 enum bpf_prog_type prog_type,1896 enum bpf_attach_type exp_attach_type,1897 const struct libbpf_prog_handler_opts *opts);1898/**1899 * @brief *libbpf_unregister_prog_handler()* unregisters previously registered1900 * custom BPF program SEC() handler.1901 * @param handler_id handler ID returned by *libbpf_register_prog_handler()*1902 * after successful registration1903 * @return 0 on success, negative error code if handler isn't found1904 *1905 * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),1906 * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs1907 * to ensure synchronization if there is a risk of running this API from1908 * multiple threads simultaneously.1909 */1910LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);1911 1912#ifdef __cplusplus1913} /* extern "C" */1914#endif1915 1916#endif /* __LIBBPF_LIBBPF_H */1917