/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */12/*3* Common eBPF ELF object loading operations.4*5* Copyright (C) 2013-2015 Alexei Starovoitov <[email protected]>6* Copyright (C) 2015 Wang Nan <[email protected]>7* Copyright (C) 2015 Huawei Inc.8*/9#ifndef __LIBBPF_LIBBPF_H10#define __LIBBPF_LIBBPF_H1112#include <stdarg.h>13#include <stdio.h>14#include <stdint.h>15#include <stdbool.h>16#include <sys/types.h> // for size_t17#include <linux/bpf.h>1819#include "libbpf_common.h"20#include "libbpf_legacy.h"2122#ifdef __cplusplus23extern "C" {24#endif2526/**27* @brief **libbpf_major_version()** provides the major version of libbpf.28* @return An integer, the major version number29*/30LIBBPF_API __u32 libbpf_major_version(void);3132/**33* @brief **libbpf_minor_version()** provides the minor version of libbpf.34* @return An integer, the minor version number35*/36LIBBPF_API __u32 libbpf_minor_version(void);3738/**39* @brief **libbpf_version_string()** provides the version of libbpf in a40* human-readable form, e.g., "v1.7".41* @return Pointer to a static string containing the version42*43* The format is *not* a part of a stable API and may change in the future.44*/45LIBBPF_API const char *libbpf_version_string(void);4647enum libbpf_errno {48__LIBBPF_ERRNO__START = 4000,4950/* Something wrong in libelf */51LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,52LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */53LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */54LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */55LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */56LIBBPF_ERRNO__RELOC, /* Relocation failed */57LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */58LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */59LIBBPF_ERRNO__PROG2BIG, /* Program too big */60LIBBPF_ERRNO__KVER, /* Incorrect kernel version */61LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */62LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */63LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */64LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */65__LIBBPF_ERRNO__END,66};6768/**69* @brief **libbpf_strerror()** converts the provided error code into a70* human-readable string.71* @param err The error code to convert72* @param buf Pointer to a buffer where the error message will be stored73* @param size The number of bytes in the buffer74* @return 0, on success; negative error code, otherwise75*/76LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);7778/**79* @brief **libbpf_bpf_attach_type_str()** converts the provided attach type80* value into a textual representation.81* @param t The attach type.82* @return Pointer to a static string identifying the attach type. NULL is83* returned for unknown **bpf_attach_type** values.84*/85LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);8687/**88* @brief **libbpf_bpf_link_type_str()** converts the provided link type value89* into a textual representation.90* @param t The link type.91* @return Pointer to a static string identifying the link type. NULL is92* returned for unknown **bpf_link_type** values.93*/94LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t);9596/**97* @brief **libbpf_bpf_map_type_str()** converts the provided map type value98* into a textual representation.99* @param t The map type.100* @return Pointer to a static string identifying the map type. NULL is101* returned for unknown **bpf_map_type** values.102*/103LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);104105/**106* @brief **libbpf_bpf_prog_type_str()** converts the provided program type107* value into a textual representation.108* @param t The program type.109* @return Pointer to a static string identifying the program type. NULL is110* returned for unknown **bpf_prog_type** values.111*/112LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);113114enum libbpf_print_level {115LIBBPF_WARN,116LIBBPF_INFO,117LIBBPF_DEBUG,118};119120typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,121const char *, va_list ap);122123/**124* @brief **libbpf_set_print()** sets user-provided log callback function to125* be used for libbpf warnings and informational messages. If the user callback126* is not set, messages are logged to stderr by default. The verbosity of these127* messages can be controlled by setting the environment variable128* LIBBPF_LOG_LEVEL to either warn, info, or debug.129* @param fn The log print function. If NULL, libbpf won't print anything.130* @return Pointer to old print function.131*132* This function is thread-safe.133*/134LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);135136/* Hide internal to user */137struct bpf_object;138139struct bpf_object_open_opts {140/* size of this struct, for forward/backward compatibility */141size_t sz;142/* object name override, if provided:143* - for object open from file, this will override setting object144* name from file path's base name;145* - for object open from memory buffer, this will specify an object146* name and will override default "<addr>-<buf-size>" name;147*/148const char *object_name;149/* parse map definitions non-strictly, allowing extra attributes/data */150bool relaxed_maps;151/* maps that set the 'pinning' attribute in their definition will have152* their pin_path attribute set to a file in this directory, and be153* auto-pinned to that path on load; defaults to "/sys/fs/bpf".154*/155const char *pin_root_path;156157__u32 :32; /* stub out now removed attach_prog_fd */158159/* Additional kernel config content that augments and overrides160* system Kconfig for CONFIG_xxx externs.161*/162const char *kconfig;163/* Path to the custom BTF to be used for BPF CO-RE relocations.164* This custom BTF completely replaces the use of vmlinux BTF165* for the purpose of CO-RE relocations.166* NOTE: any other BPF feature (e.g., fentry/fexit programs,167* struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.168*/169const char *btf_custom_path;170/* Pointer to a buffer for storing kernel logs for applicable BPF171* commands. Valid kernel_log_size has to be specified as well and are172* passed-through to bpf() syscall. Keep in mind that kernel might173* fail operation with -ENOSPC error if provided buffer is too small174* to contain entire log output.175* See the comment below for kernel_log_level for interaction between176* log_buf and log_level settings.177*178* If specified, this log buffer will be passed for:179* - each BPF progral load (BPF_PROG_LOAD) attempt, unless overridden180* with bpf_program__set_log() on per-program level, to get181* BPF verifier log output.182* - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get183* BTF sanity checking log.184*185* Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite186* previous contents, so if you need more fine-grained control, set187* per-program buffer with bpf_program__set_log_buf() to preserve each188* individual program's verification log. Keep using kernel_log_buf189* for BTF verification log, if necessary.190*/191char *kernel_log_buf;192size_t kernel_log_size;193/*194* Log level can be set independently from log buffer. Log_level=0195* means that libbpf will attempt loading BTF or program without any196* logging requested, but will retry with either its own or custom log197* buffer, if provided, and log_level=1 on any error.198* And vice versa, setting log_level>0 will request BTF or prog199* loading with verbose log from the first attempt (and as such also200* for successfully loaded BTF or program), and the actual log buffer201* could be either libbpf's own auto-allocated log buffer, if202* kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.203* If user didn't provide custom log buffer, libbpf will emit captured204* logs through its print callback.205*/206__u32 kernel_log_level;207/* Path to BPF FS mount point to derive BPF token from.208*209* Created BPF token will be used for all bpf() syscall operations210* that accept BPF token (e.g., map creation, BTF and program loads,211* etc) automatically within instantiated BPF object.212*213* If bpf_token_path is not specified, libbpf will consult214* LIBBPF_BPF_TOKEN_PATH environment variable. If set, it will be215* taken as a value of bpf_token_path option and will force libbpf to216* either create BPF token from provided custom BPF FS path, or will217* disable implicit BPF token creation, if envvar value is an empty218* string. bpf_token_path overrides LIBBPF_BPF_TOKEN_PATH, if both are219* set at the same time.220*221* Setting bpf_token_path option to empty string disables libbpf's222* automatic attempt to create BPF token from default BPF FS mount223* point (/sys/fs/bpf), in case this default behavior is undesirable.224*/225const char *bpf_token_path;226227size_t :0;228};229#define bpf_object_open_opts__last_field bpf_token_path230231/**232* @brief **bpf_object__open()** creates a bpf_object by opening233* the BPF ELF object file pointed to by the passed path and loading it234* into memory.235* @param path BPF object file path.236* @return pointer to the new bpf_object; or NULL is returned on error,237* error code is stored in errno238*/239LIBBPF_API struct bpf_object *bpf_object__open(const char *path);240241/**242* @brief **bpf_object__open_file()** creates a bpf_object by opening243* the BPF ELF object file pointed to by the passed path and loading it244* into memory.245* @param path BPF object file path246* @param opts options for how to load the bpf object, this parameter is247* optional and can be set to NULL248* @return pointer to the new bpf_object; or NULL is returned on error,249* error code is stored in errno250*/251LIBBPF_API struct bpf_object *252bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);253254/**255* @brief **bpf_object__open_mem()** creates a bpf_object by reading256* the BPF objects raw bytes from a memory buffer containing a valid257* BPF ELF object file.258* @param obj_buf pointer to the buffer containing ELF file bytes259* @param obj_buf_sz number of bytes in the buffer260* @param opts options for how to load the bpf object261* @return pointer to the new bpf_object; or NULL is returned on error,262* error code is stored in errno263*/264LIBBPF_API struct bpf_object *265bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,266const struct bpf_object_open_opts *opts);267268/**269* @brief **bpf_object__prepare()** prepares BPF object for loading:270* performs ELF processing, relocations, prepares final state of BPF program271* instructions (accessible with bpf_program__insns()), creates and272* (potentially) pins maps. Leaves BPF object in the state ready for program273* loading.274* @param obj Pointer to a valid BPF object instance returned by275* **bpf_object__open*()** API276* @return 0, on success; negative error code, otherwise, error code is277* stored in errno278*/279LIBBPF_API int bpf_object__prepare(struct bpf_object *obj);280281/**282* @brief **bpf_object__load()** loads BPF object into kernel.283* @param obj Pointer to a valid BPF object instance returned by284* **bpf_object__open*()** APIs285* @return 0, on success; negative error code, otherwise, error code is286* stored in errno287*/288LIBBPF_API int bpf_object__load(struct bpf_object *obj);289290/**291* @brief **bpf_object__close()** closes a BPF object and releases all292* resources.293* @param obj Pointer to a valid BPF object294*/295LIBBPF_API void bpf_object__close(struct bpf_object *obj);296297/**298* @brief **bpf_object__pin_maps()** pins each map contained within299* the BPF object at the passed directory.300* @param obj Pointer to a valid BPF object301* @param path A directory where maps should be pinned.302* @return 0, on success; negative error code, otherwise303*304* If `path` is NULL `bpf_map__pin` (which is being used on each map)305* will use the pin_path attribute of each map. In this case, maps that306* don't have a pin_path set will be ignored.307*/308LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);309310/**311* @brief **bpf_object__unpin_maps()** unpins each map contained within312* the BPF object found in the passed directory.313* @param obj Pointer to a valid BPF object314* @param path A directory where pinned maps should be searched for.315* @return 0, on success; negative error code, otherwise316*317* If `path` is NULL `bpf_map__unpin` (which is being used on each map)318* will use the pin_path attribute of each map. In this case, maps that319* don't have a pin_path set will be ignored.320*/321LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,322const char *path);323LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,324const char *path);325LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,326const char *path);327LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);328LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path);329330LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);331LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);332LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);333334/**335* @brief **bpf_object__token_fd** is an accessor for BPF token FD associated336* with BPF object.337* @param obj Pointer to a valid BPF object338* @return BPF token FD or -1, if it wasn't set339*/340LIBBPF_API int bpf_object__token_fd(const struct bpf_object *obj);341342struct btf;343LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);344LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);345346LIBBPF_API struct bpf_program *347bpf_object__find_program_by_name(const struct bpf_object *obj,348const char *name);349350LIBBPF_API int351libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,352enum bpf_attach_type *expected_attach_type);353LIBBPF_API int libbpf_attach_type_by_name(const char *name,354enum bpf_attach_type *attach_type);355LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,356enum bpf_attach_type attach_type);357358/* Accessors of bpf_program */359struct bpf_program;360361LIBBPF_API struct bpf_program *362bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);363364#define bpf_object__for_each_program(pos, obj) \365for ((pos) = bpf_object__next_program((obj), NULL); \366(pos) != NULL; \367(pos) = bpf_object__next_program((obj), (pos)))368369LIBBPF_API struct bpf_program *370bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);371372LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,373__u32 ifindex);374375LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);376LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);377LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);378LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);379LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);380LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);381382struct bpf_insn;383384/**385* @brief **bpf_program__insns()** gives read-only access to BPF program's386* underlying BPF instructions.387* @param prog BPF program for which to return instructions388* @return a pointer to an array of BPF instructions that belong to the389* specified BPF program390*391* Returned pointer is always valid and not NULL. Number of `struct bpf_insn`392* pointed to can be fetched using **bpf_program__insn_cnt()** API.393*394* Keep in mind, libbpf can modify and append/delete BPF program's395* instructions as it processes BPF object file and prepares everything for396* uploading into the kernel. So depending on the point in BPF object397* lifetime, **bpf_program__insns()** can return different sets of398* instructions. As an example, during BPF object load phase BPF program399* instructions will be CO-RE-relocated, BPF subprograms instructions will be400* appended, ldimm64 instructions will have FDs embedded, etc. So instructions401* returned before **bpf_object__load()** and after it might be quite402* different.403*/404LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);405406/**407* @brief **bpf_program__set_insns()** can set BPF program's underlying408* BPF instructions.409*410* WARNING: This is a very advanced libbpf API and users need to know411* what they are doing. This should be used from prog_prepare_load_fn412* callback only.413*414* @param prog BPF program for which to return instructions415* @param new_insns a pointer to an array of BPF instructions416* @param new_insn_cnt number of `struct bpf_insn`'s that form417* specified BPF program418* @return 0, on success; negative error code, otherwise419*/420LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,421struct bpf_insn *new_insns, size_t new_insn_cnt);422423/**424* @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s425* that form specified BPF program.426* @param prog BPF program for which to return number of BPF instructions427*428* See **bpf_program__insns()** documentation for notes on how libbpf can429* change instructions and their count during different phases of430* **bpf_object** lifetime.431*/432LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);433434LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);435436/**437* @brief **bpf_program__pin()** pins the BPF program to a file438* in the BPF FS specified by a path. This increments the programs439* reference count, allowing it to stay loaded after the process440* which loaded it has exited.441*442* @param prog BPF program to pin, must already be loaded443* @param path file path in a BPF file system444* @return 0, on success; negative error code, otherwise445*/446LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);447448/**449* @brief **bpf_program__unpin()** unpins the BPF program from a file450* in the BPFFS specified by a path. This decrements program's in-kernel451* reference count.452*453* The file pinning the BPF program can also be unlinked by a different454* process in which case this function will return an error.455*456* @param prog BPF program to unpin457* @param path file path to the pin in a BPF file system458* @return 0, on success; negative error code, otherwise459*/460LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);461LIBBPF_API void bpf_program__unload(struct bpf_program *prog);462463struct bpf_link;464465LIBBPF_API struct bpf_link *bpf_link__open(const char *path);466LIBBPF_API int bpf_link__fd(const struct bpf_link *link);467LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);468/**469* @brief **bpf_link__pin()** pins the BPF link to a file470* in the BPF FS specified by a path. This increments the links471* reference count, allowing it to stay loaded after the process472* which loaded it has exited.473*474* @param link BPF link to pin, must already be loaded475* @param path file path in a BPF file system476* @return 0, on success; negative error code, otherwise477*/478479LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);480481/**482* @brief **bpf_link__unpin()** unpins the BPF link from a file483* in the BPFFS. This decrements link's in-kernel reference count.484*485* The file pinning the BPF link can also be unlinked by a different486* process in which case this function will return an error.487*488* @param link BPF link to unpin489* @return 0, on success; negative error code, otherwise490*/491LIBBPF_API int bpf_link__unpin(struct bpf_link *link);492LIBBPF_API int bpf_link__update_program(struct bpf_link *link,493struct bpf_program *prog);494LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);495LIBBPF_API int bpf_link__detach(struct bpf_link *link);496LIBBPF_API int bpf_link__destroy(struct bpf_link *link);497498/**499* @brief **bpf_program__attach()** is a generic function for attaching500* a BPF program based on auto-detection of program type, attach type,501* and extra parameters, where applicable.502*503* @param prog BPF program to attach504* @return Reference to the newly created BPF link; or NULL is returned on error,505* error code is stored in errno506*507* This is supported for:508* - kprobe/kretprobe (depends on SEC() definition)509* - uprobe/uretprobe (depends on SEC() definition)510* - tracepoint511* - raw tracepoint512* - tracing programs (typed raw TP/fentry/fexit/fmod_ret)513*/514LIBBPF_API struct bpf_link *515bpf_program__attach(const struct bpf_program *prog);516517struct bpf_perf_event_opts {518/* size of this struct, for forward/backward compatibility */519size_t sz;520/* custom user-provided value fetchable through bpf_get_attach_cookie() */521__u64 bpf_cookie;522/* don't use BPF link when attach BPF program */523bool force_ioctl_attach;524/* don't automatically enable the event */525bool dont_enable;526size_t :0;527};528#define bpf_perf_event_opts__last_field dont_enable529530LIBBPF_API struct bpf_link *531bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);532533LIBBPF_API struct bpf_link *534bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,535const struct bpf_perf_event_opts *opts);536537/**538* enum probe_attach_mode - the mode to attach kprobe/uprobe539*540* force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will541* be returned if it is not supported by the kernel.542*/543enum probe_attach_mode {544/* attach probe in latest supported mode by kernel */545PROBE_ATTACH_MODE_DEFAULT = 0,546/* attach probe in legacy mode, using debugfs/tracefs */547PROBE_ATTACH_MODE_LEGACY,548/* create perf event with perf_event_open() syscall */549PROBE_ATTACH_MODE_PERF,550/* attach probe with BPF link */551PROBE_ATTACH_MODE_LINK,552};553554struct bpf_kprobe_opts {555/* size of this struct, for forward/backward compatibility */556size_t sz;557/* custom user-provided value fetchable through bpf_get_attach_cookie() */558__u64 bpf_cookie;559/* function's offset to install kprobe to */560size_t offset;561/* kprobe is return probe */562bool retprobe;563/* kprobe attach mode */564enum probe_attach_mode attach_mode;565size_t :0;566};567#define bpf_kprobe_opts__last_field attach_mode568569LIBBPF_API struct bpf_link *570bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,571const char *func_name);572LIBBPF_API struct bpf_link *573bpf_program__attach_kprobe_opts(const struct bpf_program *prog,574const char *func_name,575const struct bpf_kprobe_opts *opts);576577struct bpf_kprobe_multi_opts {578/* size of this struct, for forward/backward compatibility */579size_t sz;580/* array of function symbols to attach */581const char **syms;582/* array of function addresses to attach */583const unsigned long *addrs;584/* array of user-provided values fetchable through bpf_get_attach_cookie */585const __u64 *cookies;586/* number of elements in syms/addrs/cookies arrays */587size_t cnt;588/* create return kprobes */589bool retprobe;590/* create session kprobes */591bool session;592/* enforce unique match */593bool unique_match;594size_t :0;595};596597#define bpf_kprobe_multi_opts__last_field unique_match598599LIBBPF_API struct bpf_link *600bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,601const char *pattern,602const struct bpf_kprobe_multi_opts *opts);603604struct bpf_uprobe_multi_opts {605/* size of this struct, for forward/backward compatibility */606size_t sz;607/* array of function symbols to attach to */608const char **syms;609/* array of function addresses to attach to */610const unsigned long *offsets;611/* optional, array of associated ref counter offsets */612const unsigned long *ref_ctr_offsets;613/* optional, array of associated BPF cookies */614const __u64 *cookies;615/* number of elements in syms/addrs/cookies arrays */616size_t cnt;617/* create return uprobes */618bool retprobe;619/* create session kprobes */620bool session;621size_t :0;622};623624#define bpf_uprobe_multi_opts__last_field session625626/**627* @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program628* to multiple uprobes with uprobe_multi link.629*630* User can specify 2 mutually exclusive set of inputs:631*632* 1) use only path/func_pattern/pid arguments633*634* 2) use path/pid with allowed combinations of635* syms/offsets/ref_ctr_offsets/cookies/cnt636*637* - syms and offsets are mutually exclusive638* - ref_ctr_offsets and cookies are optional639*640*641* @param prog BPF program to attach642* @param pid Process ID to attach the uprobe to, 0 for self (own process),643* -1 for all processes644* @param binary_path Path to binary645* @param func_pattern Regular expression to specify functions to attach646* BPF program to647* @param opts Additional options (see **struct bpf_uprobe_multi_opts**)648* @return 0, on success; negative error code, otherwise649*/650LIBBPF_API struct bpf_link *651bpf_program__attach_uprobe_multi(const struct bpf_program *prog,652pid_t pid,653const char *binary_path,654const char *func_pattern,655const struct bpf_uprobe_multi_opts *opts);656657struct bpf_ksyscall_opts {658/* size of this struct, for forward/backward compatibility */659size_t sz;660/* custom user-provided value fetchable through bpf_get_attach_cookie() */661__u64 bpf_cookie;662/* attach as return probe? */663bool retprobe;664size_t :0;665};666#define bpf_ksyscall_opts__last_field retprobe667668/**669* @brief **bpf_program__attach_ksyscall()** attaches a BPF program670* to kernel syscall handler of a specified syscall. Optionally it's possible671* to request to install retprobe that will be triggered at syscall exit. It's672* also possible to associate BPF cookie (though options).673*674* Libbpf automatically will determine correct full kernel function name,675* which depending on system architecture and kernel version/configuration676* could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will677* attach specified program using kprobe/kretprobe mechanism.678*679* **bpf_program__attach_ksyscall()** is an API counterpart of declarative680* **SEC("ksyscall/<syscall>")** annotation of BPF programs.681*682* At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do683* not handle all the calling convention quirks for mmap(), clone() and compat684* syscalls. It also only attaches to "native" syscall interfaces. If host685* system supports compat syscalls or defines 32-bit syscalls in 64-bit686* kernel, such syscall interfaces won't be attached to by libbpf.687*688* These limitations may or may not change in the future. Therefore it is689* recommended to use SEC("kprobe") for these syscalls or if working with690* compat and 32-bit interfaces is required.691*692* @param prog BPF program to attach693* @param syscall_name Symbolic name of the syscall (e.g., "bpf")694* @param opts Additional options (see **struct bpf_ksyscall_opts**)695* @return Reference to the newly created BPF link; or NULL is returned on696* error, error code is stored in errno697*/698LIBBPF_API struct bpf_link *699bpf_program__attach_ksyscall(const struct bpf_program *prog,700const char *syscall_name,701const struct bpf_ksyscall_opts *opts);702703struct bpf_uprobe_opts {704/* size of this struct, for forward/backward compatibility */705size_t sz;706/* offset of kernel reference counted USDT semaphore, added in707* a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")708*/709size_t ref_ctr_offset;710/* custom user-provided value fetchable through bpf_get_attach_cookie() */711__u64 bpf_cookie;712/* uprobe is return probe, invoked at function return time */713bool retprobe;714/* Function name to attach to. Could be an unqualified ("abc") or library-qualified715* "abc@LIBXYZ" name. To specify function entry, func_name should be set while716* func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an717* offset within a function, specify func_name and use func_offset argument to specify718* offset within the function. Shared library functions must specify the shared library719* binary_path.720*/721const char *func_name;722/* uprobe attach mode */723enum probe_attach_mode attach_mode;724size_t :0;725};726#define bpf_uprobe_opts__last_field attach_mode727728/**729* @brief **bpf_program__attach_uprobe()** attaches a BPF program730* to the userspace function which is found by binary path and731* offset. You can optionally specify a particular process to attach732* to. You can also optionally attach the program to the function733* exit instead of entry.734*735* @param prog BPF program to attach736* @param retprobe Attach to function exit737* @param pid Process ID to attach the uprobe to, 0 for self (own process),738* -1 for all processes739* @param binary_path Path to binary that contains the function symbol740* @param func_offset Offset within the binary of the function symbol741* @return Reference to the newly created BPF link; or NULL is returned on error,742* error code is stored in errno743*/744LIBBPF_API struct bpf_link *745bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,746pid_t pid, const char *binary_path,747size_t func_offset);748749/**750* @brief **bpf_program__attach_uprobe_opts()** is just like751* bpf_program__attach_uprobe() except with a options struct752* for various configurations.753*754* @param prog BPF program to attach755* @param pid Process ID to attach the uprobe to, 0 for self (own process),756* -1 for all processes757* @param binary_path Path to binary that contains the function symbol758* @param func_offset Offset within the binary of the function symbol759* @param opts Options for altering program attachment760* @return Reference to the newly created BPF link; or NULL is returned on error,761* error code is stored in errno762*/763LIBBPF_API struct bpf_link *764bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,765const char *binary_path, size_t func_offset,766const struct bpf_uprobe_opts *opts);767768struct bpf_usdt_opts {769/* size of this struct, for forward/backward compatibility */770size_t sz;771/* custom user-provided value accessible through usdt_cookie() */772__u64 usdt_cookie;773size_t :0;774};775#define bpf_usdt_opts__last_field usdt_cookie776777/**778* @brief **bpf_program__attach_usdt()** is just like779* bpf_program__attach_uprobe_opts() except it covers USDT (User-space780* Statically Defined Tracepoint) attachment, instead of attaching to781* user-space function entry or exit.782*783* @param prog BPF program to attach784* @param pid Process ID to attach the uprobe to, 0 for self (own process),785* -1 for all processes786* @param binary_path Path to binary that contains provided USDT probe787* @param usdt_provider USDT provider name788* @param usdt_name USDT probe name789* @param opts Options for altering program attachment790* @return Reference to the newly created BPF link; or NULL is returned on error,791* error code is stored in errno792*/793LIBBPF_API struct bpf_link *794bpf_program__attach_usdt(const struct bpf_program *prog,795pid_t pid, const char *binary_path,796const char *usdt_provider, const char *usdt_name,797const struct bpf_usdt_opts *opts);798799struct bpf_tracepoint_opts {800/* size of this struct, for forward/backward compatibility */801size_t sz;802/* custom user-provided value fetchable through bpf_get_attach_cookie() */803__u64 bpf_cookie;804};805#define bpf_tracepoint_opts__last_field bpf_cookie806807LIBBPF_API struct bpf_link *808bpf_program__attach_tracepoint(const struct bpf_program *prog,809const char *tp_category,810const char *tp_name);811LIBBPF_API struct bpf_link *812bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,813const char *tp_category,814const char *tp_name,815const struct bpf_tracepoint_opts *opts);816817struct bpf_raw_tracepoint_opts {818size_t sz; /* size of this struct for forward/backward compatibility */819__u64 cookie;820size_t :0;821};822#define bpf_raw_tracepoint_opts__last_field cookie823824LIBBPF_API struct bpf_link *825bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,826const char *tp_name);827LIBBPF_API struct bpf_link *828bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog,829const char *tp_name,830struct bpf_raw_tracepoint_opts *opts);831832struct bpf_trace_opts {833/* size of this struct, for forward/backward compatibility */834size_t sz;835/* custom user-provided value fetchable through bpf_get_attach_cookie() */836__u64 cookie;837};838#define bpf_trace_opts__last_field cookie839840LIBBPF_API struct bpf_link *841bpf_program__attach_trace(const struct bpf_program *prog);842LIBBPF_API struct bpf_link *843bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);844845LIBBPF_API struct bpf_link *846bpf_program__attach_lsm(const struct bpf_program *prog);847LIBBPF_API struct bpf_link *848bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);849LIBBPF_API struct bpf_link *850bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);851LIBBPF_API struct bpf_link *852bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd);853LIBBPF_API struct bpf_link *854bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);855LIBBPF_API struct bpf_link *856bpf_program__attach_freplace(const struct bpf_program *prog,857int target_fd, const char *attach_func_name);858859struct bpf_netfilter_opts {860/* size of this struct, for forward/backward compatibility */861size_t sz;862863__u32 pf;864__u32 hooknum;865__s32 priority;866__u32 flags;867};868#define bpf_netfilter_opts__last_field flags869870LIBBPF_API struct bpf_link *871bpf_program__attach_netfilter(const struct bpf_program *prog,872const struct bpf_netfilter_opts *opts);873874struct bpf_tcx_opts {875/* size of this struct, for forward/backward compatibility */876size_t sz;877__u32 flags;878__u32 relative_fd;879__u32 relative_id;880__u64 expected_revision;881size_t :0;882};883#define bpf_tcx_opts__last_field expected_revision884885LIBBPF_API struct bpf_link *886bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,887const struct bpf_tcx_opts *opts);888889struct bpf_netkit_opts {890/* size of this struct, for forward/backward compatibility */891size_t sz;892__u32 flags;893__u32 relative_fd;894__u32 relative_id;895__u64 expected_revision;896size_t :0;897};898#define bpf_netkit_opts__last_field expected_revision899900LIBBPF_API struct bpf_link *901bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,902const struct bpf_netkit_opts *opts);903904struct bpf_cgroup_opts {905/* size of this struct, for forward/backward compatibility */906size_t sz;907__u32 flags;908__u32 relative_fd;909__u32 relative_id;910__u64 expected_revision;911size_t :0;912};913#define bpf_cgroup_opts__last_field expected_revision914915LIBBPF_API struct bpf_link *916bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,917const struct bpf_cgroup_opts *opts);918919struct bpf_map;920921LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);922LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);923924struct bpf_iter_attach_opts {925size_t sz; /* size of this struct for forward/backward compatibility */926union bpf_iter_link_info *link_info;927__u32 link_info_len;928};929#define bpf_iter_attach_opts__last_field link_info_len930931LIBBPF_API struct bpf_link *932bpf_program__attach_iter(const struct bpf_program *prog,933const struct bpf_iter_attach_opts *opts);934935LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);936937/**938* @brief **bpf_program__set_type()** sets the program939* type of the passed BPF program.940* @param prog BPF program to set the program type for941* @param type program type to set the BPF map to have942* @return error code; or 0 if no error. An error occurs943* if the object is already loaded.944*945* This must be called before the BPF object is loaded,946* otherwise it has no effect and an error is returned.947*/948LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,949enum bpf_prog_type type);950951LIBBPF_API enum bpf_attach_type952bpf_program__expected_attach_type(const struct bpf_program *prog);953954/**955* @brief **bpf_program__set_expected_attach_type()** sets the956* attach type of the passed BPF program. This is used for957* auto-detection of attachment when programs are loaded.958* @param prog BPF program to set the attach type for959* @param type attach type to set the BPF map to have960* @return error code; or 0 if no error. An error occurs961* if the object is already loaded.962*963* This must be called before the BPF object is loaded,964* otherwise it has no effect and an error is returned.965*/966LIBBPF_API int967bpf_program__set_expected_attach_type(struct bpf_program *prog,968enum bpf_attach_type type);969970LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);971LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);972973/* Per-program log level and log buffer getters/setters.974* See bpf_object_open_opts comments regarding log_level and log_buf975* interactions.976*/977LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);978LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);979LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);980LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);981982LIBBPF_API struct bpf_func_info *bpf_program__func_info(const struct bpf_program *prog);983LIBBPF_API __u32 bpf_program__func_info_cnt(const struct bpf_program *prog);984985LIBBPF_API struct bpf_line_info *bpf_program__line_info(const struct bpf_program *prog);986LIBBPF_API __u32 bpf_program__line_info_cnt(const struct bpf_program *prog);987988/**989* @brief **bpf_program__set_attach_target()** sets BTF-based attach target990* for supported BPF program types:991* - BTF-aware raw tracepoints (tp_btf);992* - fentry/fexit/fmod_ret;993* - lsm;994* - freplace.995* @param prog BPF program to configure; must be not yet loaded.996* @param attach_prog_fd FD of target BPF program (for freplace/extension).997* If >0 and func name omitted, defers BTF ID resolution.998* @param attach_func_name Target function name. Used either with999* attach_prog_fd to find destination BTF type ID in that BPF program, or1000* alone (no attach_prog_fd) to resolve kernel (vmlinux/module) BTF ID.1001* Must be provided if attach_prog_fd is 0.1002* @return error code; or 0 if no error occurred.1003*/1004LIBBPF_API int1005bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,1006const char *attach_func_name);10071008/**1009* @brief **bpf_object__find_map_by_name()** returns BPF map of1010* the given name, if it exists within the passed BPF object1011* @param obj BPF object1012* @param name name of the BPF map1013* @return BPF map instance, if such map exists within the BPF object;1014* or NULL otherwise.1015*/1016LIBBPF_API struct bpf_map *1017bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);10181019LIBBPF_API int1020bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);10211022LIBBPF_API struct bpf_map *1023bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);10241025#define bpf_object__for_each_map(pos, obj) \1026for ((pos) = bpf_object__next_map((obj), NULL); \1027(pos) != NULL; \1028(pos) = bpf_object__next_map((obj), (pos)))1029#define bpf_map__for_each bpf_object__for_each_map10301031LIBBPF_API struct bpf_map *1032bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);10331034/**1035* @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create1036* BPF map during BPF object load phase.1037* @param map the BPF map instance1038* @param autocreate whether to create BPF map during BPF object load1039* @return 0 on success; -EBUSY if BPF object was already loaded1040*1041* **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating1042* BPF map. By default, libbpf will attempt to create every single BPF map1043* defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall1044* and fill in map FD in BPF instructions.1045*1046* This API allows to opt-out of this process for specific map instance. This1047* can be useful if host kernel doesn't support such BPF map type or used1048* combination of flags and user application wants to avoid creating such1049* a map in the first place. User is still responsible to make sure that their1050* BPF-side code that expects to use such missing BPF map is recognized by BPF1051* verifier as dead code, otherwise BPF verifier will reject such BPF program.1052*/1053LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);1054LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);10551056/**1057* @brief **bpf_map__set_autoattach()** sets whether libbpf has to auto-attach1058* map during BPF skeleton attach phase.1059* @param map the BPF map instance1060* @param autoattach whether to attach map during BPF skeleton attach phase1061* @return 0 on success; negative error code, otherwise1062*/1063LIBBPF_API int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach);10641065/**1066* @brief **bpf_map__autoattach()** returns whether BPF map is configured to1067* auto-attach during BPF skeleton attach phase.1068* @param map the BPF map instance1069* @return true if map is set to auto-attach during skeleton attach phase; false, otherwise1070*/1071LIBBPF_API bool bpf_map__autoattach(const struct bpf_map *map);10721073/**1074* @brief **bpf_map__fd()** gets the file descriptor of the passed1075* BPF map1076* @param map the BPF map instance1077* @return the file descriptor; or -EINVAL in case of an error1078*/1079LIBBPF_API int bpf_map__fd(const struct bpf_map *map);1080LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);1081/* get map name */1082LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);1083/* get/set map type */1084LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);1085LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);1086/* get/set map size (max_entries) */1087LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);1088LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);1089/* get/set map flags */1090LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);1091LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);1092/* get/set map NUMA node */1093LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);1094LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);1095/* get/set map key size */1096LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);1097LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);1098/* get map value size */1099LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);1100/**1101* @brief **bpf_map__set_value_size()** sets map value size.1102* @param map the BPF map instance1103* @param size the new value size1104* @return 0, on success; negative error, otherwise1105*1106* There is a special case for maps with associated memory-mapped regions, like1107* the global data section maps (bss, data, rodata). When this function is used1108* on such a map, the mapped region is resized. Afterward, an attempt is made to1109* adjust the corresponding BTF info. This attempt is best-effort and can only1110* succeed if the last variable of the data section map is an array. The array1111* BTF type is replaced by a new BTF array type with a different length.1112* Any previously existing pointers returned from bpf_map__initial_value() or1113* corresponding data section skeleton pointer must be reinitialized.1114*/1115LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);1116/* get map key/value BTF type IDs */1117LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);1118LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);1119/* get/set map if_index */1120LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);1121LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);1122/* get/set map map_extra flags */1123LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);1124LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);11251126LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,1127const void *data, size_t size);1128LIBBPF_API void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize);11291130/**1131* @brief **bpf_map__is_internal()** tells the caller whether or not the1132* passed map is a special map created by libbpf automatically for things like1133* global variables, __ksym externs, Kconfig values, etc1134* @param map the bpf_map1135* @return true, if the map is an internal map; false, otherwise1136*/1137LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);11381139/**1140* @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the1141* BPF map should be pinned. This does not actually create the 'pin'.1142* @param map The bpf_map1143* @param path The path1144* @return 0, on success; negative error, otherwise1145*/1146LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);11471148/**1149* @brief **bpf_map__pin_path()** gets the path attribute that tells where the1150* BPF map should be pinned.1151* @param map The bpf_map1152* @return The path string; which can be NULL1153*/1154LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);11551156/**1157* @brief **bpf_map__is_pinned()** tells the caller whether or not the1158* passed map has been pinned via a 'pin' file.1159* @param map The bpf_map1160* @return true, if the map is pinned; false, otherwise1161*/1162LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);11631164/**1165* @brief **bpf_map__pin()** creates a file that serves as a 'pin'1166* for the BPF map. This increments the reference count on the1167* BPF map which will keep the BPF map loaded even after the1168* userspace process which loaded it has exited.1169* @param map The bpf_map to pin1170* @param path A file path for the 'pin'1171* @return 0, on success; negative error, otherwise1172*1173* If `path` is NULL the maps `pin_path` attribute will be used. If this is1174* also NULL, an error will be returned and the map will not be pinned.1175*/1176LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);11771178/**1179* @brief **bpf_map__unpin()** removes the file that serves as a1180* 'pin' for the BPF map.1181* @param map The bpf_map to unpin1182* @param path A file path for the 'pin'1183* @return 0, on success; negative error, otherwise1184*1185* The `path` parameter can be NULL, in which case the `pin_path`1186* map attribute is unpinned. If both the `path` parameter and1187* `pin_path` map attribute are set, they must be equal.1188*/1189LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);11901191LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);1192LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);11931194/**1195* @brief **bpf_map__lookup_elem()** allows to lookup BPF map value1196* corresponding to provided key.1197* @param map BPF map to lookup element in1198* @param key pointer to memory containing bytes of the key used for lookup1199* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1200* @param value pointer to memory in which looked up value will be stored1201* @param value_sz size in byte of value data memory; it has to match BPF map1202* definition's **value_size**. For per-CPU BPF maps value size has to be1203* a product of BPF map value size and number of possible CPUs in the system1204* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1205* per-CPU values value size has to be aligned up to closest 8 bytes for1206* alignment reasons, so expected size is: `round_up(value_size, 8)1207* * libbpf_num_possible_cpus()`.1208* @param flags extra flags passed to kernel for this operation1209* @return 0, on success; negative error, otherwise1210*1211* **bpf_map__lookup_elem()** is high-level equivalent of1212* **bpf_map_lookup_elem()** API with added check for key and value size.1213*/1214LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,1215const void *key, size_t key_sz,1216void *value, size_t value_sz, __u64 flags);12171218/**1219* @brief **bpf_map__update_elem()** allows to insert or update value in BPF1220* map that corresponds to provided key.1221* @param map BPF map to insert to or update element in1222* @param key pointer to memory containing bytes of the key1223* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1224* @param value pointer to memory containing bytes of the value1225* @param value_sz size in byte of value data memory; it has to match BPF map1226* definition's **value_size**. For per-CPU BPF maps value size has to be1227* a product of BPF map value size and number of possible CPUs in the system1228* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1229* per-CPU values value size has to be aligned up to closest 8 bytes for1230* alignment reasons, so expected size is: `round_up(value_size, 8)1231* * libbpf_num_possible_cpus()`.1232* @param flags extra flags passed to kernel for this operation1233* @return 0, on success; negative error, otherwise1234*1235* **bpf_map__update_elem()** is high-level equivalent of1236* **bpf_map_update_elem()** API with added check for key and value size.1237*/1238LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,1239const void *key, size_t key_sz,1240const void *value, size_t value_sz, __u64 flags);12411242/**1243* @brief **bpf_map__delete_elem()** allows to delete element in BPF map that1244* corresponds to provided key.1245* @param map BPF map to delete element from1246* @param key pointer to memory containing bytes of the key1247* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1248* @param flags extra flags passed to kernel for this operation1249* @return 0, on success; negative error, otherwise1250*1251* **bpf_map__delete_elem()** is high-level equivalent of1252* **bpf_map_delete_elem()** API with added check for key size.1253*/1254LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,1255const void *key, size_t key_sz, __u64 flags);12561257/**1258* @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value1259* corresponding to provided key and atomically delete it afterwards.1260* @param map BPF map to lookup element in1261* @param key pointer to memory containing bytes of the key used for lookup1262* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1263* @param value pointer to memory in which looked up value will be stored1264* @param value_sz size in byte of value data memory; it has to match BPF map1265* definition's **value_size**. For per-CPU BPF maps value size has to be1266* a product of BPF map value size and number of possible CPUs in the system1267* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1268* per-CPU values value size has to be aligned up to closest 8 bytes for1269* alignment reasons, so expected size is: `round_up(value_size, 8)1270* * libbpf_num_possible_cpus()`.1271* @param flags extra flags passed to kernel for this operation1272* @return 0, on success; negative error, otherwise1273*1274* **bpf_map__lookup_and_delete_elem()** is high-level equivalent of1275* **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.1276*/1277LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,1278const void *key, size_t key_sz,1279void *value, size_t value_sz, __u64 flags);12801281/**1282* @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by1283* fetching next key that follows current key.1284* @param map BPF map to fetch next key from1285* @param cur_key pointer to memory containing bytes of current key or NULL to1286* fetch the first key1287* @param next_key pointer to memory to write next key into1288* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1289* @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;1290* negative error, otherwise1291*1292* **bpf_map__get_next_key()** is high-level equivalent of1293* **bpf_map_get_next_key()** API with added check for key size.1294*/1295LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,1296const void *cur_key, void *next_key, size_t key_sz);1297/**1298* @brief **bpf_map__set_exclusive_program()** sets a map to be exclusive to the1299* specified program. This must be called *before* the map is created.1300*1301* @param map BPF map to make exclusive.1302* @param prog BPF program to be the exclusive user of the map. Must belong1303* to the same bpf_object as the map.1304* @return 0 on success; a negative error code otherwise.1305*1306* This function must be called after the BPF object is opened but before1307* it is loaded. Once the object is loaded, only the specified program1308* will be able to access the map's contents.1309*/1310LIBBPF_API int bpf_map__set_exclusive_program(struct bpf_map *map, struct bpf_program *prog);13111312/**1313* @brief **bpf_map__exclusive_program()** returns the exclusive program1314* that is registered with the map (if any).1315* @param map BPF map to which the exclusive program is registered.1316* @return the registered exclusive program.1317*/1318LIBBPF_API struct bpf_program *bpf_map__exclusive_program(struct bpf_map *map);13191320struct bpf_xdp_set_link_opts {1321size_t sz;1322int old_fd;1323size_t :0;1324};1325#define bpf_xdp_set_link_opts__last_field old_fd13261327struct bpf_xdp_attach_opts {1328size_t sz;1329int old_prog_fd;1330size_t :0;1331};1332#define bpf_xdp_attach_opts__last_field old_prog_fd13331334struct bpf_xdp_query_opts {1335size_t sz;1336__u32 prog_id; /* output */1337__u32 drv_prog_id; /* output */1338__u32 hw_prog_id; /* output */1339__u32 skb_prog_id; /* output */1340__u8 attach_mode; /* output */1341__u64 feature_flags; /* output */1342__u32 xdp_zc_max_segs; /* output */1343size_t :0;1344};1345#define bpf_xdp_query_opts__last_field xdp_zc_max_segs13461347LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,1348const struct bpf_xdp_attach_opts *opts);1349LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,1350const struct bpf_xdp_attach_opts *opts);1351LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);1352LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);13531354/* TC related API */1355enum bpf_tc_attach_point {1356BPF_TC_INGRESS = 1 << 0,1357BPF_TC_EGRESS = 1 << 1,1358BPF_TC_CUSTOM = 1 << 2,1359BPF_TC_QDISC = 1 << 3,1360};13611362#define BPF_TC_PARENT(a, b) \1363((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))13641365enum bpf_tc_flags {1366BPF_TC_F_REPLACE = 1 << 0,1367};13681369struct bpf_tc_hook {1370size_t sz;1371int ifindex;1372enum bpf_tc_attach_point attach_point;1373__u32 parent;1374__u32 handle;1375const char *qdisc;1376size_t :0;1377};1378#define bpf_tc_hook__last_field qdisc13791380struct bpf_tc_opts {1381size_t sz;1382int prog_fd;1383__u32 flags;1384__u32 prog_id;1385__u32 handle;1386__u32 priority;1387size_t :0;1388};1389#define bpf_tc_opts__last_field priority13901391LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);1392LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);1393LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,1394struct bpf_tc_opts *opts);1395LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,1396const struct bpf_tc_opts *opts);1397LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,1398struct bpf_tc_opts *opts);13991400/* Ring buffer APIs */1401struct ring_buffer;1402struct ring;1403struct user_ring_buffer;14041405typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);14061407struct ring_buffer_opts {1408size_t sz; /* size of this struct, for forward/backward compatibility */1409};14101411#define ring_buffer_opts__last_field sz14121413LIBBPF_API struct ring_buffer *1414ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,1415const struct ring_buffer_opts *opts);1416LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);1417LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,1418ring_buffer_sample_fn sample_cb, void *ctx);1419LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);1420LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);1421LIBBPF_API int ring_buffer__consume_n(struct ring_buffer *rb, size_t n);1422LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);14231424/**1425* @brief **ring_buffer__ring()** returns the ringbuffer object inside a given1426* ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance.1427*1428* @param rb A ringbuffer manager object.1429* @param idx An index into the ringbuffers contained within the ringbuffer1430* manager object. The index is 0-based and corresponds to the order in which1431* ring_buffer__add was called.1432* @return A ringbuffer object on success; NULL and errno set if the index is1433* invalid.1434*/1435LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,1436unsigned int idx);14371438/**1439* @brief **ring__consumer_pos()** returns the current consumer position in the1440* given ringbuffer.1441*1442* @param r A ringbuffer object.1443* @return The current consumer position.1444*/1445LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);14461447/**1448* @brief **ring__producer_pos()** returns the current producer position in the1449* given ringbuffer.1450*1451* @param r A ringbuffer object.1452* @return The current producer position.1453*/1454LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);14551456/**1457* @brief **ring__avail_data_size()** returns the number of bytes in the1458* ringbuffer not yet consumed. This has no locking associated with it, so it1459* can be inaccurate if operations are ongoing while this is called. However, it1460* should still show the correct trend over the long-term.1461*1462* @param r A ringbuffer object.1463* @return The number of bytes not yet consumed.1464*/1465LIBBPF_API size_t ring__avail_data_size(const struct ring *r);14661467/**1468* @brief **ring__size()** returns the total size of the ringbuffer's map data1469* area (excluding special producer/consumer pages). Effectively this gives the1470* amount of usable bytes of data inside the ringbuffer.1471*1472* @param r A ringbuffer object.1473* @return The total size of the ringbuffer map data area.1474*/1475LIBBPF_API size_t ring__size(const struct ring *r);14761477/**1478* @brief **ring__map_fd()** returns the file descriptor underlying the given1479* ringbuffer.1480*1481* @param r A ringbuffer object.1482* @return The underlying ringbuffer file descriptor1483*/1484LIBBPF_API int ring__map_fd(const struct ring *r);14851486/**1487* @brief **ring__consume()** consumes available ringbuffer data without event1488* polling.1489*1490* @param r A ringbuffer object.1491* @return The number of records consumed (or INT_MAX, whichever is less), or1492* a negative number if any of the callbacks return an error.1493*/1494LIBBPF_API int ring__consume(struct ring *r);14951496/**1497* @brief **ring__consume_n()** consumes up to a requested amount of items from1498* a ringbuffer without event polling.1499*1500* @param r A ringbuffer object.1501* @param n Maximum amount of items to consume.1502* @return The number of items consumed, or a negative number if any of the1503* callbacks return an error.1504*/1505LIBBPF_API int ring__consume_n(struct ring *r, size_t n);15061507struct user_ring_buffer_opts {1508size_t sz; /* size of this struct, for forward/backward compatibility */1509};15101511#define user_ring_buffer_opts__last_field sz15121513/**1514* @brief **user_ring_buffer__new()** creates a new instance of a user ring1515* buffer.1516*1517* @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.1518* @param opts Options for how the ring buffer should be created.1519* @return A user ring buffer on success; NULL and errno being set on a1520* failure.1521*/1522LIBBPF_API struct user_ring_buffer *1523user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);15241525/**1526* @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the1527* user ring buffer.1528* @param rb A pointer to a user ring buffer.1529* @param size The size of the sample, in bytes.1530* @return A pointer to an 8-byte aligned reserved region of the user ring1531* buffer; NULL, and errno being set if a sample could not be reserved.1532*1533* This function is *not* thread safe, and callers must synchronize accessing1534* this function if there are multiple producers. If a size is requested that1535* is larger than the size of the entire ring buffer, errno will be set to1536* E2BIG and NULL is returned. If the ring buffer could accommodate the size,1537* but currently does not have enough space, errno is set to ENOSPC and NULL is1538* returned.1539*1540* After initializing the sample, callers must invoke1541* **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,1542* the sample must be freed with **user_ring_buffer__discard()**.1543*/1544LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);15451546/**1547* @brief **user_ring_buffer__reserve_blocking()** reserves a record in the1548* ring buffer, possibly blocking for up to @timeout_ms until a sample becomes1549* available.1550* @param rb The user ring buffer.1551* @param size The size of the sample, in bytes.1552* @param timeout_ms The amount of time, in milliseconds, for which the caller1553* should block when waiting for a sample. -1 causes the caller to block1554* indefinitely.1555* @return A pointer to an 8-byte aligned reserved region of the user ring1556* buffer; NULL, and errno being set if a sample could not be reserved.1557*1558* This function is *not* thread safe, and callers must synchronize1559* accessing this function if there are multiple producers1560*1561* If **timeout_ms** is -1, the function will block indefinitely until a sample1562* becomes available. Otherwise, **timeout_ms** must be non-negative, or errno1563* is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking1564* will occur and the function will return immediately after attempting to1565* reserve a sample.1566*1567* If **size** is larger than the size of the entire ring buffer, errno is set1568* to E2BIG and NULL is returned. If the ring buffer could accommodate1569* **size**, but currently does not have enough space, the caller will block1570* until at most **timeout_ms** has elapsed. If insufficient space is available1571* at that time, errno is set to ENOSPC, and NULL is returned.1572*1573* The kernel guarantees that it will wake up this thread to check if1574* sufficient space is available in the ring buffer at least once per1575* invocation of the **bpf_ringbuf_drain()** helper function, provided that at1576* least one sample is consumed, and the BPF program did not invoke the1577* function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the1578* kernel does not guarantee this. If the helper function is invoked with1579* BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is1580* consumed.1581*1582* When a sample of size **size** is found within **timeout_ms**, a pointer to1583* the sample is returned. After initializing the sample, callers must invoke1584* **user_ring_buffer__submit()** to post the sample to the ring buffer.1585* Otherwise, the sample must be freed with **user_ring_buffer__discard()**.1586*/1587LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,1588__u32 size,1589int timeout_ms);15901591/**1592* @brief **user_ring_buffer__submit()** submits a previously reserved sample1593* into the ring buffer.1594* @param rb The user ring buffer.1595* @param sample A reserved sample.1596*1597* It is not necessary to synchronize amongst multiple producers when invoking1598* this function.1599*/1600LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);16011602/**1603* @brief **user_ring_buffer__discard()** discards a previously reserved sample.1604* @param rb The user ring buffer.1605* @param sample A reserved sample.1606*1607* It is not necessary to synchronize amongst multiple producers when invoking1608* this function.1609*/1610LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);16111612/**1613* @brief **user_ring_buffer__free()** frees a ring buffer that was previously1614* created with **user_ring_buffer__new()**.1615* @param rb The user ring buffer being freed.1616*/1617LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);16181619/* Perf buffer APIs */1620struct perf_buffer;16211622typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,1623void *data, __u32 size);1624typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);16251626/* common use perf buffer options */1627struct perf_buffer_opts {1628size_t sz;1629__u32 sample_period;1630size_t :0;1631};1632#define perf_buffer_opts__last_field sample_period16331634/**1635* @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified1636* BPF_PERF_EVENT_ARRAY map1637* @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF1638* code to send data over to user-space1639* @param page_cnt number of memory pages allocated for each per-CPU buffer1640* @param sample_cb function called on each received data record1641* @param lost_cb function called when record loss has occurred1642* @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*1643* @param opts optional parameters for the perf buffer, can be null1644* @return a new instance of struct perf_buffer on success, NULL on error with1645* *errno* containing an error code1646*/1647LIBBPF_API struct perf_buffer *1648perf_buffer__new(int map_fd, size_t page_cnt,1649perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,1650const struct perf_buffer_opts *opts);16511652enum bpf_perf_event_ret {1653LIBBPF_PERF_EVENT_DONE = 0,1654LIBBPF_PERF_EVENT_ERROR = -1,1655LIBBPF_PERF_EVENT_CONT = -2,1656};16571658struct perf_event_header;16591660typedef enum bpf_perf_event_ret1661(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);16621663/* raw perf buffer options, giving most power and control */1664struct perf_buffer_raw_opts {1665size_t sz;1666long :0;1667long :0;1668/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of1669* max_entries of given PERF_EVENT_ARRAY map)1670*/1671int cpu_cnt;1672/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */1673int *cpus;1674/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */1675int *map_keys;1676};1677#define perf_buffer_raw_opts__last_field map_keys16781679struct perf_event_attr;16801681LIBBPF_API struct perf_buffer *1682perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,1683perf_buffer_event_fn event_cb, void *ctx,1684const struct perf_buffer_raw_opts *opts);16851686LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);1687LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);1688LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);1689LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);1690LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);1691LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);1692LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);1693/**1694* @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying1695* memory region of the ring buffer.1696* This ring buffer can be used to implement a custom events consumer.1697* The ring buffer starts with the *struct perf_event_mmap_page*, which1698* holds the ring buffer management fields, when accessing the header1699* structure it's important to be SMP aware.1700* You can refer to *perf_event_read_simple* for a simple example.1701* @param pb the perf buffer structure1702* @param buf_idx the buffer index to retrieve1703* @param buf (out) gets the base pointer of the mmap()'ed memory1704* @param buf_size (out) gets the size of the mmap()'ed region1705* @return 0 on success, negative error code for failure1706*/1707LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,1708size_t *buf_size);17091710struct bpf_prog_linfo;1711struct bpf_prog_info;17121713LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);1714LIBBPF_API struct bpf_prog_linfo *1715bpf_prog_linfo__new(const struct bpf_prog_info *info);1716LIBBPF_API const struct bpf_line_info *1717bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,1718__u64 addr, __u32 func_idx, __u32 nr_skip);1719LIBBPF_API const struct bpf_line_info *1720bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,1721__u32 insn_off, __u32 nr_skip);17221723/*1724* Probe for supported system features1725*1726* Note that running many of these probes in a short amount of time can cause1727* the kernel to reach the maximal size of lockable memory allowed for the1728* user, causing subsequent probes to fail. In this case, the caller may want1729* to adjust that limit with setrlimit().1730*/17311732/**1733* @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports1734* BPF programs of a given type.1735* @param prog_type BPF program type to detect kernel support for1736* @param opts reserved for future extensibility, should be NULL1737* @return 1, if given program type is supported; 0, if given program type is1738* not supported; negative error code if feature detection failed or can't be1739* performed1740*1741* Make sure the process has required set of CAP_* permissions (or runs as1742* root) when performing feature checking.1743*/1744LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);1745/**1746* @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports1747* BPF maps of a given type.1748* @param map_type BPF map type to detect kernel support for1749* @param opts reserved for future extensibility, should be NULL1750* @return 1, if given map type is supported; 0, if given map type is1751* not supported; negative error code if feature detection failed or can't be1752* performed1753*1754* Make sure the process has required set of CAP_* permissions (or runs as1755* root) when performing feature checking.1756*/1757LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);1758/**1759* @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the1760* use of a given BPF helper from specified BPF program type.1761* @param prog_type BPF program type used to check the support of BPF helper1762* @param helper_id BPF helper ID (enum bpf_func_id) to check support for1763* @param opts reserved for future extensibility, should be NULL1764* @return 1, if given combination of program type and helper is supported; 0,1765* if the combination is not supported; negative error code if feature1766* detection for provided input arguments failed or can't be performed1767*1768* Make sure the process has required set of CAP_* permissions (or runs as1769* root) when performing feature checking.1770*/1771LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,1772enum bpf_func_id helper_id, const void *opts);17731774/**1775* @brief **libbpf_num_possible_cpus()** is a helper function to get the1776* number of possible CPUs that the host kernel supports and expects.1777* @return number of possible CPUs; or error code on failure1778*1779* Example usage:1780*1781* int ncpus = libbpf_num_possible_cpus();1782* if (ncpus < 0) {1783* // error handling1784* }1785* long values[ncpus];1786* bpf_map_lookup_elem(per_cpu_map_fd, key, values);1787*/1788LIBBPF_API int libbpf_num_possible_cpus(void);17891790struct bpf_map_skeleton {1791const char *name;1792struct bpf_map **map;1793void **mmaped;1794struct bpf_link **link;1795};17961797struct bpf_prog_skeleton {1798const char *name;1799struct bpf_program **prog;1800struct bpf_link **link;1801};18021803struct bpf_object_skeleton {1804size_t sz; /* size of this struct, for forward/backward compatibility */18051806const char *name;1807const void *data;1808size_t data_sz;18091810struct bpf_object **obj;18111812int map_cnt;1813int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */1814struct bpf_map_skeleton *maps;18151816int prog_cnt;1817int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */1818struct bpf_prog_skeleton *progs;1819};18201821LIBBPF_API int1822bpf_object__open_skeleton(struct bpf_object_skeleton *s,1823const struct bpf_object_open_opts *opts);1824LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);1825LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);1826LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);1827LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);18281829struct bpf_var_skeleton {1830const char *name;1831struct bpf_map **map;1832void **addr;1833};18341835struct bpf_object_subskeleton {1836size_t sz; /* size of this struct, for forward/backward compatibility */18371838const struct bpf_object *obj;18391840int map_cnt;1841int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */1842struct bpf_map_skeleton *maps;18431844int prog_cnt;1845int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */1846struct bpf_prog_skeleton *progs;18471848int var_cnt;1849int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */1850struct bpf_var_skeleton *vars;1851};18521853LIBBPF_API int1854bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);1855LIBBPF_API void1856bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);18571858struct gen_loader_opts {1859size_t sz; /* size of this struct, for forward/backward compatibility */1860const char *data;1861const char *insns;1862__u32 data_sz;1863__u32 insns_sz;1864bool gen_hash;1865};18661867#define gen_loader_opts__last_field gen_hash1868LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,1869struct gen_loader_opts *opts);18701871enum libbpf_tristate {1872TRI_NO = 0,1873TRI_YES = 1,1874TRI_MODULE = 2,1875};18761877struct bpf_linker_opts {1878/* size of this struct, for forward/backward compatibility */1879size_t sz;1880};1881#define bpf_linker_opts__last_field sz18821883struct bpf_linker_file_opts {1884/* size of this struct, for forward/backward compatibility */1885size_t sz;1886};1887#define bpf_linker_file_opts__last_field sz18881889struct bpf_linker;18901891LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);1892LIBBPF_API struct bpf_linker *bpf_linker__new_fd(int fd, struct bpf_linker_opts *opts);1893LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,1894const char *filename,1895const struct bpf_linker_file_opts *opts);1896LIBBPF_API int bpf_linker__add_fd(struct bpf_linker *linker, int fd,1897const struct bpf_linker_file_opts *opts);1898LIBBPF_API int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz,1899const struct bpf_linker_file_opts *opts);1900LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);1901LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);19021903/*1904* Custom handling of BPF program's SEC() definitions1905*/19061907struct bpf_prog_load_opts; /* defined in bpf.h */19081909/* Called during bpf_object__open() for each recognized BPF program. Callback1910* can use various bpf_program__set_*() setters to adjust whatever properties1911* are necessary.1912*/1913typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);19141915/* Called right before libbpf performs bpf_prog_load() to load BPF program1916* into the kernel. Callback can adjust opts as necessary.1917*/1918typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,1919struct bpf_prog_load_opts *opts, long cookie);19201921/* Called during skeleton attach or through bpf_program__attach(). If1922* auto-attach is not supported, callback should return 0 and set link to1923* NULL (it's not considered an error during skeleton attach, but it will be1924* an error for bpf_program__attach() calls). On error, error should be1925* returned directly and link set to NULL. On success, return 0 and set link1926* to a valid struct bpf_link.1927*/1928typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,1929struct bpf_link **link);19301931struct libbpf_prog_handler_opts {1932/* size of this struct, for forward/backward compatibility */1933size_t sz;1934/* User-provided value that is passed to prog_setup_fn,1935* prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to1936* register one set of callbacks for multiple SEC() definitions and1937* still be able to distinguish them, if necessary. For example,1938* libbpf itself is using this to pass necessary flags (e.g.,1939* sleepable flag) to a common internal SEC() handler.1940*/1941long cookie;1942/* BPF program initialization callback (see libbpf_prog_setup_fn_t).1943* Callback is optional, pass NULL if it's not necessary.1944*/1945libbpf_prog_setup_fn_t prog_setup_fn;1946/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).1947* Callback is optional, pass NULL if it's not necessary.1948*/1949libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;1950/* BPF program attach callback (see libbpf_prog_attach_fn_t).1951* Callback is optional, pass NULL if it's not necessary.1952*/1953libbpf_prog_attach_fn_t prog_attach_fn;1954};1955#define libbpf_prog_handler_opts__last_field prog_attach_fn19561957/**1958* @brief **libbpf_register_prog_handler()** registers a custom BPF program1959* SEC() handler.1960* @param sec section prefix for which custom handler is registered1961* @param prog_type BPF program type associated with specified section1962* @param exp_attach_type Expected BPF attach type associated with specified section1963* @param opts optional cookie, callbacks, and other extra options1964* @return Non-negative handler ID is returned on success. This handler ID has1965* to be passed to *libbpf_unregister_prog_handler()* to unregister such1966* custom handler. Negative error code is returned on error.1967*1968* *sec* defines which SEC() definitions are handled by this custom handler1969* registration. *sec* can have few different forms:1970* - if *sec* is just a plain string (e.g., "abc"), it will match only1971* SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result1972* in an error;1973* - if *sec* is of the form "abc/", proper SEC() form is1974* SEC("abc/something"), where acceptable "something" should be checked by1975* *prog_init_fn* callback, if there are additional restrictions;1976* - if *sec* is of the form "abc+", it will successfully match both1977* SEC("abc") and SEC("abc/whatever") forms;1978* - if *sec* is NULL, custom handler is registered for any BPF program that1979* doesn't match any of the registered (custom or libbpf's own) SEC()1980* handlers. There could be only one such generic custom handler registered1981* at any given time.1982*1983* All custom handlers (except the one with *sec* == NULL) are processed1984* before libbpf's own SEC() handlers. It is allowed to "override" libbpf's1985* SEC() handlers by registering custom ones for the same section prefix1986* (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")1987* handler).1988*1989* Note, like much of global libbpf APIs (e.g., libbpf_set_print(),1990* libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs1991* to ensure synchronization if there is a risk of running this API from1992* multiple threads simultaneously.1993*/1994LIBBPF_API int libbpf_register_prog_handler(const char *sec,1995enum bpf_prog_type prog_type,1996enum bpf_attach_type exp_attach_type,1997const struct libbpf_prog_handler_opts *opts);1998/**1999* @brief *libbpf_unregister_prog_handler()* unregisters previously registered2000* custom BPF program SEC() handler.2001* @param handler_id handler ID returned by *libbpf_register_prog_handler()*2002* after successful registration2003* @return 0 on success, negative error code if handler isn't found2004*2005* Note, like much of global libbpf APIs (e.g., libbpf_set_print(),2006* libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs2007* to ensure synchronization if there is a risk of running this API from2008* multiple threads simultaneously.2009*/2010LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);20112012#ifdef __cplusplus2013} /* extern "C" */2014#endif20152016#endif /* __LIBBPF_LIBBPF_H */201720182019