/* 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#endif2526LIBBPF_API __u32 libbpf_major_version(void);27LIBBPF_API __u32 libbpf_minor_version(void);28LIBBPF_API const char *libbpf_version_string(void);2930enum libbpf_errno {31__LIBBPF_ERRNO__START = 4000,3233/* Something wrong in libelf */34LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,35LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */36LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */37LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */38LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */39LIBBPF_ERRNO__RELOC, /* Relocation failed */40LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */41LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */42LIBBPF_ERRNO__PROG2BIG, /* Program too big */43LIBBPF_ERRNO__KVER, /* Incorrect kernel version */44LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */45LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */46LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */47LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */48__LIBBPF_ERRNO__END,49};5051LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);5253/**54* @brief **libbpf_bpf_attach_type_str()** converts the provided attach type55* value into a textual representation.56* @param t The attach type.57* @return Pointer to a static string identifying the attach type. NULL is58* returned for unknown **bpf_attach_type** values.59*/60LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);6162/**63* @brief **libbpf_bpf_link_type_str()** converts the provided link type value64* into a textual representation.65* @param t The link type.66* @return Pointer to a static string identifying the link type. NULL is67* returned for unknown **bpf_link_type** values.68*/69LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t);7071/**72* @brief **libbpf_bpf_map_type_str()** converts the provided map type value73* into a textual representation.74* @param t The map type.75* @return Pointer to a static string identifying the map type. NULL is76* returned for unknown **bpf_map_type** values.77*/78LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);7980/**81* @brief **libbpf_bpf_prog_type_str()** converts the provided program type82* value into a textual representation.83* @param t The program type.84* @return Pointer to a static string identifying the program type. NULL is85* returned for unknown **bpf_prog_type** values.86*/87LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);8889enum libbpf_print_level {90LIBBPF_WARN,91LIBBPF_INFO,92LIBBPF_DEBUG,93};9495typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,96const char *, va_list ap);9798/**99* @brief **libbpf_set_print()** sets user-provided log callback function to100* be used for libbpf warnings and informational messages. If the user callback101* is not set, messages are logged to stderr by default. The verbosity of these102* messages can be controlled by setting the environment variable103* LIBBPF_LOG_LEVEL to either warn, info, or debug.104* @param fn The log print function. If NULL, libbpf won't print anything.105* @return Pointer to old print function.106*107* This function is thread-safe.108*/109LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);110111/* Hide internal to user */112struct bpf_object;113114struct bpf_object_open_opts {115/* size of this struct, for forward/backward compatibility */116size_t sz;117/* object name override, if provided:118* - for object open from file, this will override setting object119* name from file path's base name;120* - for object open from memory buffer, this will specify an object121* name and will override default "<addr>-<buf-size>" name;122*/123const char *object_name;124/* parse map definitions non-strictly, allowing extra attributes/data */125bool relaxed_maps;126/* maps that set the 'pinning' attribute in their definition will have127* their pin_path attribute set to a file in this directory, and be128* auto-pinned to that path on load; defaults to "/sys/fs/bpf".129*/130const char *pin_root_path;131132__u32 :32; /* stub out now removed attach_prog_fd */133134/* Additional kernel config content that augments and overrides135* system Kconfig for CONFIG_xxx externs.136*/137const char *kconfig;138/* Path to the custom BTF to be used for BPF CO-RE relocations.139* This custom BTF completely replaces the use of vmlinux BTF140* for the purpose of CO-RE relocations.141* NOTE: any other BPF feature (e.g., fentry/fexit programs,142* struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.143*/144const char *btf_custom_path;145/* Pointer to a buffer for storing kernel logs for applicable BPF146* commands. Valid kernel_log_size has to be specified as well and are147* passed-through to bpf() syscall. Keep in mind that kernel might148* fail operation with -ENOSPC error if provided buffer is too small149* to contain entire log output.150* See the comment below for kernel_log_level for interaction between151* log_buf and log_level settings.152*153* If specified, this log buffer will be passed for:154* - each BPF progral load (BPF_PROG_LOAD) attempt, unless overridden155* with bpf_program__set_log() on per-program level, to get156* BPF verifier log output.157* - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get158* BTF sanity checking log.159*160* Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite161* previous contents, so if you need more fine-grained control, set162* per-program buffer with bpf_program__set_log_buf() to preserve each163* individual program's verification log. Keep using kernel_log_buf164* for BTF verification log, if necessary.165*/166char *kernel_log_buf;167size_t kernel_log_size;168/*169* Log level can be set independently from log buffer. Log_level=0170* means that libbpf will attempt loading BTF or program without any171* logging requested, but will retry with either its own or custom log172* buffer, if provided, and log_level=1 on any error.173* And vice versa, setting log_level>0 will request BTF or prog174* loading with verbose log from the first attempt (and as such also175* for successfully loaded BTF or program), and the actual log buffer176* could be either libbpf's own auto-allocated log buffer, if177* kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.178* If user didn't provide custom log buffer, libbpf will emit captured179* logs through its print callback.180*/181__u32 kernel_log_level;182/* Path to BPF FS mount point to derive BPF token from.183*184* Created BPF token will be used for all bpf() syscall operations185* that accept BPF token (e.g., map creation, BTF and program loads,186* etc) automatically within instantiated BPF object.187*188* If bpf_token_path is not specified, libbpf will consult189* LIBBPF_BPF_TOKEN_PATH environment variable. If set, it will be190* taken as a value of bpf_token_path option and will force libbpf to191* either create BPF token from provided custom BPF FS path, or will192* disable implicit BPF token creation, if envvar value is an empty193* string. bpf_token_path overrides LIBBPF_BPF_TOKEN_PATH, if both are194* set at the same time.195*196* Setting bpf_token_path option to empty string disables libbpf's197* automatic attempt to create BPF token from default BPF FS mount198* point (/sys/fs/bpf), in case this default behavior is undesirable.199*/200const char *bpf_token_path;201202size_t :0;203};204#define bpf_object_open_opts__last_field bpf_token_path205206/**207* @brief **bpf_object__open()** creates a bpf_object by opening208* the BPF ELF object file pointed to by the passed path and loading it209* into memory.210* @param path BPF object file path.211* @return pointer to the new bpf_object; or NULL is returned on error,212* error code is stored in errno213*/214LIBBPF_API struct bpf_object *bpf_object__open(const char *path);215216/**217* @brief **bpf_object__open_file()** creates a bpf_object by opening218* the BPF ELF object file pointed to by the passed path and loading it219* into memory.220* @param path BPF object file path221* @param opts options for how to load the bpf object, this parameter is222* optional and can be set to NULL223* @return pointer to the new bpf_object; or NULL is returned on error,224* error code is stored in errno225*/226LIBBPF_API struct bpf_object *227bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);228229/**230* @brief **bpf_object__open_mem()** creates a bpf_object by reading231* the BPF objects raw bytes from a memory buffer containing a valid232* BPF ELF object file.233* @param obj_buf pointer to the buffer containing ELF file bytes234* @param obj_buf_sz number of bytes in the buffer235* @param opts options for how to load the bpf object236* @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 *240bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,241const struct bpf_object_open_opts *opts);242243/**244* @brief **bpf_object__prepare()** prepares BPF object for loading:245* performs ELF processing, relocations, prepares final state of BPF program246* instructions (accessible with bpf_program__insns()), creates and247* (potentially) pins maps. Leaves BPF object in the state ready for program248* loading.249* @param obj Pointer to a valid BPF object instance returned by250* **bpf_object__open*()** API251* @return 0, on success; negative error code, otherwise, error code is252* stored in errno253*/254int bpf_object__prepare(struct bpf_object *obj);255256/**257* @brief **bpf_object__load()** loads BPF object into kernel.258* @param obj Pointer to a valid BPF object instance returned by259* **bpf_object__open*()** APIs260* @return 0, on success; negative error code, otherwise, error code is261* stored in errno262*/263LIBBPF_API int bpf_object__load(struct bpf_object *obj);264265/**266* @brief **bpf_object__close()** closes a BPF object and releases all267* resources.268* @param obj Pointer to a valid BPF object269*/270LIBBPF_API void bpf_object__close(struct bpf_object *obj);271272/**273* @brief **bpf_object__pin_maps()** pins each map contained within274* the BPF object at the passed directory.275* @param obj Pointer to a valid BPF object276* @param path A directory where maps should be pinned.277* @return 0, on success; negative error code, otherwise278*279* If `path` is NULL `bpf_map__pin` (which is being used on each map)280* will use the pin_path attribute of each map. In this case, maps that281* don't have a pin_path set will be ignored.282*/283LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);284285/**286* @brief **bpf_object__unpin_maps()** unpins each map contained within287* the BPF object found in the passed directory.288* @param obj Pointer to a valid BPF object289* @param path A directory where pinned maps should be searched for.290* @return 0, on success; negative error code, otherwise291*292* If `path` is NULL `bpf_map__unpin` (which is being used on each map)293* will use the pin_path attribute of each map. In this case, maps that294* don't have a pin_path set will be ignored.295*/296LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,297const char *path);298LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,299const char *path);300LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,301const char *path);302LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);303LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path);304305LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);306LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);307LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);308309/**310* @brief **bpf_object__token_fd** is an accessor for BPF token FD associated311* with BPF object.312* @param obj Pointer to a valid BPF object313* @return BPF token FD or -1, if it wasn't set314*/315LIBBPF_API int bpf_object__token_fd(const struct bpf_object *obj);316317struct btf;318LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);319LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);320321LIBBPF_API struct bpf_program *322bpf_object__find_program_by_name(const struct bpf_object *obj,323const char *name);324325LIBBPF_API int326libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,327enum bpf_attach_type *expected_attach_type);328LIBBPF_API int libbpf_attach_type_by_name(const char *name,329enum bpf_attach_type *attach_type);330LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,331enum bpf_attach_type attach_type);332333/* Accessors of bpf_program */334struct bpf_program;335336LIBBPF_API struct bpf_program *337bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);338339#define bpf_object__for_each_program(pos, obj) \340for ((pos) = bpf_object__next_program((obj), NULL); \341(pos) != NULL; \342(pos) = bpf_object__next_program((obj), (pos)))343344LIBBPF_API struct bpf_program *345bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);346347LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,348__u32 ifindex);349350LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);351LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);352LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);353LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);354LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);355LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);356357struct bpf_insn;358359/**360* @brief **bpf_program__insns()** gives read-only access to BPF program's361* underlying BPF instructions.362* @param prog BPF program for which to return instructions363* @return a pointer to an array of BPF instructions that belong to the364* specified BPF program365*366* Returned pointer is always valid and not NULL. Number of `struct bpf_insn`367* pointed to can be fetched using **bpf_program__insn_cnt()** API.368*369* Keep in mind, libbpf can modify and append/delete BPF program's370* instructions as it processes BPF object file and prepares everything for371* uploading into the kernel. So depending on the point in BPF object372* lifetime, **bpf_program__insns()** can return different sets of373* instructions. As an example, during BPF object load phase BPF program374* instructions will be CO-RE-relocated, BPF subprograms instructions will be375* appended, ldimm64 instructions will have FDs embedded, etc. So instructions376* returned before **bpf_object__load()** and after it might be quite377* different.378*/379LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);380381/**382* @brief **bpf_program__set_insns()** can set BPF program's underlying383* BPF instructions.384*385* WARNING: This is a very advanced libbpf API and users need to know386* what they are doing. This should be used from prog_prepare_load_fn387* callback only.388*389* @param prog BPF program for which to return instructions390* @param new_insns a pointer to an array of BPF instructions391* @param new_insn_cnt number of `struct bpf_insn`'s that form392* specified BPF program393* @return 0, on success; negative error code, otherwise394*/395LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,396struct bpf_insn *new_insns, size_t new_insn_cnt);397398/**399* @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s400* that form specified BPF program.401* @param prog BPF program for which to return number of BPF instructions402*403* See **bpf_program__insns()** documentation for notes on how libbpf can404* change instructions and their count during different phases of405* **bpf_object** lifetime.406*/407LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);408409LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);410411/**412* @brief **bpf_program__pin()** pins the BPF program to a file413* in the BPF FS specified by a path. This increments the programs414* reference count, allowing it to stay loaded after the process415* which loaded it has exited.416*417* @param prog BPF program to pin, must already be loaded418* @param path file path in a BPF file system419* @return 0, on success; negative error code, otherwise420*/421LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);422423/**424* @brief **bpf_program__unpin()** unpins the BPF program from a file425* in the BPFFS specified by a path. This decrements the programs426* reference count.427*428* The file pinning the BPF program can also be unlinked by a different429* process in which case this function will return an error.430*431* @param prog BPF program to unpin432* @param path file path to the pin in a BPF file system433* @return 0, on success; negative error code, otherwise434*/435LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);436LIBBPF_API void bpf_program__unload(struct bpf_program *prog);437438struct bpf_link;439440LIBBPF_API struct bpf_link *bpf_link__open(const char *path);441LIBBPF_API int bpf_link__fd(const struct bpf_link *link);442LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);443/**444* @brief **bpf_link__pin()** pins the BPF link to a file445* in the BPF FS specified by a path. This increments the links446* reference count, allowing it to stay loaded after the process447* which loaded it has exited.448*449* @param link BPF link to pin, must already be loaded450* @param path file path in a BPF file system451* @return 0, on success; negative error code, otherwise452*/453454LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);455456/**457* @brief **bpf_link__unpin()** unpins the BPF link from a file458* in the BPFFS specified by a path. This decrements the links459* reference count.460*461* The file pinning the BPF link can also be unlinked by a different462* process in which case this function will return an error.463*464* @param prog BPF program to unpin465* @param path file path to the pin in a BPF file system466* @return 0, on success; negative error code, otherwise467*/468LIBBPF_API int bpf_link__unpin(struct bpf_link *link);469LIBBPF_API int bpf_link__update_program(struct bpf_link *link,470struct bpf_program *prog);471LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);472LIBBPF_API int bpf_link__detach(struct bpf_link *link);473LIBBPF_API int bpf_link__destroy(struct bpf_link *link);474475/**476* @brief **bpf_program__attach()** is a generic function for attaching477* a BPF program based on auto-detection of program type, attach type,478* and extra parameters, where applicable.479*480* @param prog BPF program to attach481* @return Reference to the newly created BPF link; or NULL is returned on error,482* error code is stored in errno483*484* This is supported for:485* - kprobe/kretprobe (depends on SEC() definition)486* - uprobe/uretprobe (depends on SEC() definition)487* - tracepoint488* - raw tracepoint489* - tracing programs (typed raw TP/fentry/fexit/fmod_ret)490*/491LIBBPF_API struct bpf_link *492bpf_program__attach(const struct bpf_program *prog);493494struct bpf_perf_event_opts {495/* size of this struct, for forward/backward compatibility */496size_t sz;497/* custom user-provided value fetchable through bpf_get_attach_cookie() */498__u64 bpf_cookie;499/* don't use BPF link when attach BPF program */500bool force_ioctl_attach;501/* don't automatically enable the event */502bool dont_enable;503size_t :0;504};505#define bpf_perf_event_opts__last_field dont_enable506507LIBBPF_API struct bpf_link *508bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);509510LIBBPF_API struct bpf_link *511bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,512const struct bpf_perf_event_opts *opts);513514/**515* enum probe_attach_mode - the mode to attach kprobe/uprobe516*517* force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will518* be returned if it is not supported by the kernel.519*/520enum probe_attach_mode {521/* attach probe in latest supported mode by kernel */522PROBE_ATTACH_MODE_DEFAULT = 0,523/* attach probe in legacy mode, using debugfs/tracefs */524PROBE_ATTACH_MODE_LEGACY,525/* create perf event with perf_event_open() syscall */526PROBE_ATTACH_MODE_PERF,527/* attach probe with BPF link */528PROBE_ATTACH_MODE_LINK,529};530531struct bpf_kprobe_opts {532/* size of this struct, for forward/backward compatibility */533size_t sz;534/* custom user-provided value fetchable through bpf_get_attach_cookie() */535__u64 bpf_cookie;536/* function's offset to install kprobe to */537size_t offset;538/* kprobe is return probe */539bool retprobe;540/* kprobe attach mode */541enum probe_attach_mode attach_mode;542size_t :0;543};544#define bpf_kprobe_opts__last_field attach_mode545546LIBBPF_API struct bpf_link *547bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,548const char *func_name);549LIBBPF_API struct bpf_link *550bpf_program__attach_kprobe_opts(const struct bpf_program *prog,551const char *func_name,552const struct bpf_kprobe_opts *opts);553554struct bpf_kprobe_multi_opts {555/* size of this struct, for forward/backward compatibility */556size_t sz;557/* array of function symbols to attach */558const char **syms;559/* array of function addresses to attach */560const unsigned long *addrs;561/* array of user-provided values fetchable through bpf_get_attach_cookie */562const __u64 *cookies;563/* number of elements in syms/addrs/cookies arrays */564size_t cnt;565/* create return kprobes */566bool retprobe;567/* create session kprobes */568bool session;569/* enforce unique match */570bool unique_match;571size_t :0;572};573574#define bpf_kprobe_multi_opts__last_field unique_match575576LIBBPF_API struct bpf_link *577bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,578const char *pattern,579const struct bpf_kprobe_multi_opts *opts);580581struct bpf_uprobe_multi_opts {582/* size of this struct, for forward/backward compatibility */583size_t sz;584/* array of function symbols to attach to */585const char **syms;586/* array of function addresses to attach to */587const unsigned long *offsets;588/* optional, array of associated ref counter offsets */589const unsigned long *ref_ctr_offsets;590/* optional, array of associated BPF cookies */591const __u64 *cookies;592/* number of elements in syms/addrs/cookies arrays */593size_t cnt;594/* create return uprobes */595bool retprobe;596/* create session kprobes */597bool session;598size_t :0;599};600601#define bpf_uprobe_multi_opts__last_field session602603/**604* @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program605* to multiple uprobes with uprobe_multi link.606*607* User can specify 2 mutually exclusive set of inputs:608*609* 1) use only path/func_pattern/pid arguments610*611* 2) use path/pid with allowed combinations of612* syms/offsets/ref_ctr_offsets/cookies/cnt613*614* - syms and offsets are mutually exclusive615* - ref_ctr_offsets and cookies are optional616*617*618* @param prog BPF program to attach619* @param pid Process ID to attach the uprobe to, 0 for self (own process),620* -1 for all processes621* @param binary_path Path to binary622* @param func_pattern Regular expression to specify functions to attach623* BPF program to624* @param opts Additional options (see **struct bpf_uprobe_multi_opts**)625* @return 0, on success; negative error code, otherwise626*/627LIBBPF_API struct bpf_link *628bpf_program__attach_uprobe_multi(const struct bpf_program *prog,629pid_t pid,630const char *binary_path,631const char *func_pattern,632const struct bpf_uprobe_multi_opts *opts);633634struct bpf_ksyscall_opts {635/* size of this struct, for forward/backward compatibility */636size_t sz;637/* custom user-provided value fetchable through bpf_get_attach_cookie() */638__u64 bpf_cookie;639/* attach as return probe? */640bool retprobe;641size_t :0;642};643#define bpf_ksyscall_opts__last_field retprobe644645/**646* @brief **bpf_program__attach_ksyscall()** attaches a BPF program647* to kernel syscall handler of a specified syscall. Optionally it's possible648* to request to install retprobe that will be triggered at syscall exit. It's649* also possible to associate BPF cookie (though options).650*651* Libbpf automatically will determine correct full kernel function name,652* which depending on system architecture and kernel version/configuration653* could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will654* attach specified program using kprobe/kretprobe mechanism.655*656* **bpf_program__attach_ksyscall()** is an API counterpart of declarative657* **SEC("ksyscall/<syscall>")** annotation of BPF programs.658*659* At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do660* not handle all the calling convention quirks for mmap(), clone() and compat661* syscalls. It also only attaches to "native" syscall interfaces. If host662* system supports compat syscalls or defines 32-bit syscalls in 64-bit663* kernel, such syscall interfaces won't be attached to by libbpf.664*665* These limitations may or may not change in the future. Therefore it is666* recommended to use SEC("kprobe") for these syscalls or if working with667* compat and 32-bit interfaces is required.668*669* @param prog BPF program to attach670* @param syscall_name Symbolic name of the syscall (e.g., "bpf")671* @param opts Additional options (see **struct bpf_ksyscall_opts**)672* @return Reference to the newly created BPF link; or NULL is returned on673* error, error code is stored in errno674*/675LIBBPF_API struct bpf_link *676bpf_program__attach_ksyscall(const struct bpf_program *prog,677const char *syscall_name,678const struct bpf_ksyscall_opts *opts);679680struct bpf_uprobe_opts {681/* size of this struct, for forward/backward compatibility */682size_t sz;683/* offset of kernel reference counted USDT semaphore, added in684* a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")685*/686size_t ref_ctr_offset;687/* custom user-provided value fetchable through bpf_get_attach_cookie() */688__u64 bpf_cookie;689/* uprobe is return probe, invoked at function return time */690bool retprobe;691/* Function name to attach to. Could be an unqualified ("abc") or library-qualified692* "abc@LIBXYZ" name. To specify function entry, func_name should be set while693* func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an694* offset within a function, specify func_name and use func_offset argument to specify695* offset within the function. Shared library functions must specify the shared library696* binary_path.697*/698const char *func_name;699/* uprobe attach mode */700enum probe_attach_mode attach_mode;701size_t :0;702};703#define bpf_uprobe_opts__last_field attach_mode704705/**706* @brief **bpf_program__attach_uprobe()** attaches a BPF program707* to the userspace function which is found by binary path and708* offset. You can optionally specify a particular process to attach709* to. You can also optionally attach the program to the function710* exit instead of entry.711*712* @param prog BPF program to attach713* @param retprobe Attach to function exit714* @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* @return Reference to the newly created BPF link; or NULL is returned on error,719* error code is stored in errno720*/721LIBBPF_API struct bpf_link *722bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,723pid_t pid, const char *binary_path,724size_t func_offset);725726/**727* @brief **bpf_program__attach_uprobe_opts()** is just like728* bpf_program__attach_uprobe() except with a options struct729* for various configurations.730*731* @param prog BPF program to attach732* @param pid Process ID to attach the uprobe to, 0 for self (own process),733* -1 for all processes734* @param binary_path Path to binary that contains the function symbol735* @param func_offset Offset within the binary of the function symbol736* @param opts Options for altering program attachment737* @return Reference to the newly created BPF link; or NULL is returned on error,738* error code is stored in errno739*/740LIBBPF_API struct bpf_link *741bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,742const char *binary_path, size_t func_offset,743const struct bpf_uprobe_opts *opts);744745struct bpf_usdt_opts {746/* size of this struct, for forward/backward compatibility */747size_t sz;748/* custom user-provided value accessible through usdt_cookie() */749__u64 usdt_cookie;750size_t :0;751};752#define bpf_usdt_opts__last_field usdt_cookie753754/**755* @brief **bpf_program__attach_usdt()** is just like756* bpf_program__attach_uprobe_opts() except it covers USDT (User-space757* Statically Defined Tracepoint) attachment, instead of attaching to758* user-space function entry or exit.759*760* @param prog BPF program to attach761* @param pid Process ID to attach the uprobe to, 0 for self (own process),762* -1 for all processes763* @param binary_path Path to binary that contains provided USDT probe764* @param usdt_provider USDT provider name765* @param usdt_name USDT probe name766* @param opts Options for altering program attachment767* @return Reference to the newly created BPF link; or NULL is returned on error,768* error code is stored in errno769*/770LIBBPF_API struct bpf_link *771bpf_program__attach_usdt(const struct bpf_program *prog,772pid_t pid, const char *binary_path,773const char *usdt_provider, const char *usdt_name,774const struct bpf_usdt_opts *opts);775776struct bpf_tracepoint_opts {777/* size of this struct, for forward/backward compatibility */778size_t sz;779/* custom user-provided value fetchable through bpf_get_attach_cookie() */780__u64 bpf_cookie;781};782#define bpf_tracepoint_opts__last_field bpf_cookie783784LIBBPF_API struct bpf_link *785bpf_program__attach_tracepoint(const struct bpf_program *prog,786const char *tp_category,787const char *tp_name);788LIBBPF_API struct bpf_link *789bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,790const char *tp_category,791const char *tp_name,792const struct bpf_tracepoint_opts *opts);793794struct bpf_raw_tracepoint_opts {795size_t sz; /* size of this struct for forward/backward compatibility */796__u64 cookie;797size_t :0;798};799#define bpf_raw_tracepoint_opts__last_field cookie800801LIBBPF_API struct bpf_link *802bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,803const char *tp_name);804LIBBPF_API struct bpf_link *805bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog,806const char *tp_name,807struct bpf_raw_tracepoint_opts *opts);808809struct bpf_trace_opts {810/* size of this struct, for forward/backward compatibility */811size_t sz;812/* custom user-provided value fetchable through bpf_get_attach_cookie() */813__u64 cookie;814};815#define bpf_trace_opts__last_field cookie816817LIBBPF_API struct bpf_link *818bpf_program__attach_trace(const struct bpf_program *prog);819LIBBPF_API struct bpf_link *820bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);821822LIBBPF_API struct bpf_link *823bpf_program__attach_lsm(const struct bpf_program *prog);824LIBBPF_API struct bpf_link *825bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);826LIBBPF_API struct bpf_link *827bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);828LIBBPF_API struct bpf_link *829bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd);830LIBBPF_API struct bpf_link *831bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);832LIBBPF_API struct bpf_link *833bpf_program__attach_freplace(const struct bpf_program *prog,834int target_fd, const char *attach_func_name);835836struct bpf_netfilter_opts {837/* size of this struct, for forward/backward compatibility */838size_t sz;839840__u32 pf;841__u32 hooknum;842__s32 priority;843__u32 flags;844};845#define bpf_netfilter_opts__last_field flags846847LIBBPF_API struct bpf_link *848bpf_program__attach_netfilter(const struct bpf_program *prog,849const struct bpf_netfilter_opts *opts);850851struct bpf_tcx_opts {852/* size of this struct, for forward/backward compatibility */853size_t sz;854__u32 flags;855__u32 relative_fd;856__u32 relative_id;857__u64 expected_revision;858size_t :0;859};860#define bpf_tcx_opts__last_field expected_revision861862LIBBPF_API struct bpf_link *863bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,864const struct bpf_tcx_opts *opts);865866struct bpf_netkit_opts {867/* size of this struct, for forward/backward compatibility */868size_t sz;869__u32 flags;870__u32 relative_fd;871__u32 relative_id;872__u64 expected_revision;873size_t :0;874};875#define bpf_netkit_opts__last_field expected_revision876877LIBBPF_API struct bpf_link *878bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,879const struct bpf_netkit_opts *opts);880881struct bpf_cgroup_opts {882/* size of this struct, for forward/backward compatibility */883size_t sz;884__u32 flags;885__u32 relative_fd;886__u32 relative_id;887__u64 expected_revision;888size_t :0;889};890#define bpf_cgroup_opts__last_field expected_revision891892LIBBPF_API struct bpf_link *893bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,894const struct bpf_cgroup_opts *opts);895896struct bpf_map;897898LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);899LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);900901struct bpf_iter_attach_opts {902size_t sz; /* size of this struct for forward/backward compatibility */903union bpf_iter_link_info *link_info;904__u32 link_info_len;905};906#define bpf_iter_attach_opts__last_field link_info_len907908LIBBPF_API struct bpf_link *909bpf_program__attach_iter(const struct bpf_program *prog,910const struct bpf_iter_attach_opts *opts);911912LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);913914/**915* @brief **bpf_program__set_type()** sets the program916* type of the passed BPF program.917* @param prog BPF program to set the program type for918* @param type program type to set the BPF map to have919* @return error code; or 0 if no error. An error occurs920* if the object is already loaded.921*922* This must be called before the BPF object is loaded,923* otherwise it has no effect and an error is returned.924*/925LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,926enum bpf_prog_type type);927928LIBBPF_API enum bpf_attach_type929bpf_program__expected_attach_type(const struct bpf_program *prog);930931/**932* @brief **bpf_program__set_expected_attach_type()** sets the933* attach type of the passed BPF program. This is used for934* auto-detection of attachment when programs are loaded.935* @param prog BPF program to set the attach type for936* @param type attach type to set the BPF map to have937* @return error code; or 0 if no error. An error occurs938* if the object is already loaded.939*940* This must be called before the BPF object is loaded,941* otherwise it has no effect and an error is returned.942*/943LIBBPF_API int944bpf_program__set_expected_attach_type(struct bpf_program *prog,945enum bpf_attach_type type);946947LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);948LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);949950/* Per-program log level and log buffer getters/setters.951* See bpf_object_open_opts comments regarding log_level and log_buf952* interactions.953*/954LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);955LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);956LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);957LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);958959LIBBPF_API struct bpf_func_info *bpf_program__func_info(const struct bpf_program *prog);960LIBBPF_API __u32 bpf_program__func_info_cnt(const struct bpf_program *prog);961962LIBBPF_API struct bpf_line_info *bpf_program__line_info(const struct bpf_program *prog);963LIBBPF_API __u32 bpf_program__line_info_cnt(const struct bpf_program *prog);964965/**966* @brief **bpf_program__set_attach_target()** sets BTF-based attach target967* for supported BPF program types:968* - BTF-aware raw tracepoints (tp_btf);969* - fentry/fexit/fmod_ret;970* - lsm;971* - freplace.972* @param prog BPF program to set the attach type for973* @param type attach type to set the BPF map to have974* @return error code; or 0 if no error occurred.975*/976LIBBPF_API int977bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,978const char *attach_func_name);979980/**981* @brief **bpf_object__find_map_by_name()** returns BPF map of982* the given name, if it exists within the passed BPF object983* @param obj BPF object984* @param name name of the BPF map985* @return BPF map instance, if such map exists within the BPF object;986* or NULL otherwise.987*/988LIBBPF_API struct bpf_map *989bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);990991LIBBPF_API int992bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);993994LIBBPF_API struct bpf_map *995bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);996997#define bpf_object__for_each_map(pos, obj) \998for ((pos) = bpf_object__next_map((obj), NULL); \999(pos) != NULL; \1000(pos) = bpf_object__next_map((obj), (pos)))1001#define bpf_map__for_each bpf_object__for_each_map10021003LIBBPF_API struct bpf_map *1004bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);10051006/**1007* @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create1008* BPF map during BPF object load phase.1009* @param map the BPF map instance1010* @param autocreate whether to create BPF map during BPF object load1011* @return 0 on success; -EBUSY if BPF object was already loaded1012*1013* **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating1014* BPF map. By default, libbpf will attempt to create every single BPF map1015* defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall1016* and fill in map FD in BPF instructions.1017*1018* This API allows to opt-out of this process for specific map instance. This1019* can be useful if host kernel doesn't support such BPF map type or used1020* combination of flags and user application wants to avoid creating such1021* a map in the first place. User is still responsible to make sure that their1022* BPF-side code that expects to use such missing BPF map is recognized by BPF1023* verifier as dead code, otherwise BPF verifier will reject such BPF program.1024*/1025LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);1026LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);10271028/**1029* @brief **bpf_map__set_autoattach()** sets whether libbpf has to auto-attach1030* map during BPF skeleton attach phase.1031* @param map the BPF map instance1032* @param autoattach whether to attach map during BPF skeleton attach phase1033* @return 0 on success; negative error code, otherwise1034*/1035LIBBPF_API int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach);10361037/**1038* @brief **bpf_map__autoattach()** returns whether BPF map is configured to1039* auto-attach during BPF skeleton attach phase.1040* @param map the BPF map instance1041* @return true if map is set to auto-attach during skeleton attach phase; false, otherwise1042*/1043LIBBPF_API bool bpf_map__autoattach(const struct bpf_map *map);10441045/**1046* @brief **bpf_map__fd()** gets the file descriptor of the passed1047* BPF map1048* @param map the BPF map instance1049* @return the file descriptor; or -EINVAL in case of an error1050*/1051LIBBPF_API int bpf_map__fd(const struct bpf_map *map);1052LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);1053/* get map name */1054LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);1055/* get/set map type */1056LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);1057LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);1058/* get/set map size (max_entries) */1059LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);1060LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);1061/* get/set map flags */1062LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);1063LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);1064/* get/set map NUMA node */1065LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);1066LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);1067/* get/set map key size */1068LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);1069LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);1070/* get map value size */1071LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);1072/**1073* @brief **bpf_map__set_value_size()** sets map value size.1074* @param map the BPF map instance1075* @return 0, on success; negative error, otherwise1076*1077* There is a special case for maps with associated memory-mapped regions, like1078* the global data section maps (bss, data, rodata). When this function is used1079* on such a map, the mapped region is resized. Afterward, an attempt is made to1080* adjust the corresponding BTF info. This attempt is best-effort and can only1081* succeed if the last variable of the data section map is an array. The array1082* BTF type is replaced by a new BTF array type with a different length.1083* Any previously existing pointers returned from bpf_map__initial_value() or1084* corresponding data section skeleton pointer must be reinitialized.1085*/1086LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);1087/* get map key/value BTF type IDs */1088LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);1089LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);1090/* get/set map if_index */1091LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);1092LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);1093/* get/set map map_extra flags */1094LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);1095LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);10961097LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,1098const void *data, size_t size);1099LIBBPF_API void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize);11001101/**1102* @brief **bpf_map__is_internal()** tells the caller whether or not the1103* passed map is a special map created by libbpf automatically for things like1104* global variables, __ksym externs, Kconfig values, etc1105* @param map the bpf_map1106* @return true, if the map is an internal map; false, otherwise1107*/1108LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);11091110/**1111* @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the1112* BPF map should be pinned. This does not actually create the 'pin'.1113* @param map The bpf_map1114* @param path The path1115* @return 0, on success; negative error, otherwise1116*/1117LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);11181119/**1120* @brief **bpf_map__pin_path()** gets the path attribute that tells where the1121* BPF map should be pinned.1122* @param map The bpf_map1123* @return The path string; which can be NULL1124*/1125LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);11261127/**1128* @brief **bpf_map__is_pinned()** tells the caller whether or not the1129* passed map has been pinned via a 'pin' file.1130* @param map The bpf_map1131* @return true, if the map is pinned; false, otherwise1132*/1133LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);11341135/**1136* @brief **bpf_map__pin()** creates a file that serves as a 'pin'1137* for the BPF map. This increments the reference count on the1138* BPF map which will keep the BPF map loaded even after the1139* userspace process which loaded it has exited.1140* @param map The bpf_map to pin1141* @param path A file path for the 'pin'1142* @return 0, on success; negative error, otherwise1143*1144* If `path` is NULL the maps `pin_path` attribute will be used. If this is1145* also NULL, an error will be returned and the map will not be pinned.1146*/1147LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);11481149/**1150* @brief **bpf_map__unpin()** removes the file that serves as a1151* 'pin' for the BPF map.1152* @param map The bpf_map to unpin1153* @param path A file path for the 'pin'1154* @return 0, on success; negative error, otherwise1155*1156* The `path` parameter can be NULL, in which case the `pin_path`1157* map attribute is unpinned. If both the `path` parameter and1158* `pin_path` map attribute are set, they must be equal.1159*/1160LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);11611162LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);1163LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);11641165/**1166* @brief **bpf_map__lookup_elem()** allows to lookup BPF map value1167* corresponding to provided key.1168* @param map BPF map to lookup element in1169* @param key pointer to memory containing bytes of the key used for lookup1170* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1171* @param value pointer to memory in which looked up value will be stored1172* @param value_sz size in byte of value data memory; it has to match BPF map1173* definition's **value_size**. For per-CPU BPF maps value size has to be1174* a product of BPF map value size and number of possible CPUs in the system1175* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1176* per-CPU values value size has to be aligned up to closest 8 bytes for1177* alignment reasons, so expected size is: `round_up(value_size, 8)1178* * libbpf_num_possible_cpus()`.1179* @flags extra flags passed to kernel for this operation1180* @return 0, on success; negative error, otherwise1181*1182* **bpf_map__lookup_elem()** is high-level equivalent of1183* **bpf_map_lookup_elem()** API with added check for key and value size.1184*/1185LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,1186const void *key, size_t key_sz,1187void *value, size_t value_sz, __u64 flags);11881189/**1190* @brief **bpf_map__update_elem()** allows to insert or update value in BPF1191* map that corresponds to provided key.1192* @param map BPF map to insert to or update element in1193* @param key pointer to memory containing bytes of the key1194* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1195* @param value pointer to memory containing bytes of the value1196* @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__update_elem()** is high-level equivalent of1207* **bpf_map_update_elem()** API with added check for key and value size.1208*/1209LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,1210const void *key, size_t key_sz,1211const void *value, size_t value_sz, __u64 flags);12121213/**1214* @brief **bpf_map__delete_elem()** allows to delete element in BPF map that1215* corresponds to provided key.1216* @param map BPF map to delete element from1217* @param key pointer to memory containing bytes of the key1218* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1219* @flags extra flags passed to kernel for this operation1220* @return 0, on success; negative error, otherwise1221*1222* **bpf_map__delete_elem()** is high-level equivalent of1223* **bpf_map_delete_elem()** API with added check for key size.1224*/1225LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,1226const void *key, size_t key_sz, __u64 flags);12271228/**1229* @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value1230* corresponding to provided key and atomically delete it afterwards.1231* @param map BPF map to lookup element in1232* @param key pointer to memory containing bytes of the key used for lookup1233* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1234* @param value pointer to memory in which looked up value will be stored1235* @param value_sz size in byte of value data memory; it has to match BPF map1236* definition's **value_size**. For per-CPU BPF maps value size has to be1237* a product of BPF map value size and number of possible CPUs in the system1238* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1239* per-CPU values value size has to be aligned up to closest 8 bytes for1240* alignment reasons, so expected size is: `round_up(value_size, 8)1241* * libbpf_num_possible_cpus()`.1242* @flags extra flags passed to kernel for this operation1243* @return 0, on success; negative error, otherwise1244*1245* **bpf_map__lookup_and_delete_elem()** is high-level equivalent of1246* **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.1247*/1248LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,1249const void *key, size_t key_sz,1250void *value, size_t value_sz, __u64 flags);12511252/**1253* @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by1254* fetching next key that follows current key.1255* @param map BPF map to fetch next key from1256* @param cur_key pointer to memory containing bytes of current key or NULL to1257* fetch the first key1258* @param next_key pointer to memory to write next key into1259* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1260* @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;1261* negative error, otherwise1262*1263* **bpf_map__get_next_key()** is high-level equivalent of1264* **bpf_map_get_next_key()** API with added check for key size.1265*/1266LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,1267const void *cur_key, void *next_key, size_t key_sz);12681269struct bpf_xdp_set_link_opts {1270size_t sz;1271int old_fd;1272size_t :0;1273};1274#define bpf_xdp_set_link_opts__last_field old_fd12751276struct bpf_xdp_attach_opts {1277size_t sz;1278int old_prog_fd;1279size_t :0;1280};1281#define bpf_xdp_attach_opts__last_field old_prog_fd12821283struct bpf_xdp_query_opts {1284size_t sz;1285__u32 prog_id; /* output */1286__u32 drv_prog_id; /* output */1287__u32 hw_prog_id; /* output */1288__u32 skb_prog_id; /* output */1289__u8 attach_mode; /* output */1290__u64 feature_flags; /* output */1291__u32 xdp_zc_max_segs; /* output */1292size_t :0;1293};1294#define bpf_xdp_query_opts__last_field xdp_zc_max_segs12951296LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,1297const struct bpf_xdp_attach_opts *opts);1298LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,1299const struct bpf_xdp_attach_opts *opts);1300LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);1301LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);13021303/* TC related API */1304enum bpf_tc_attach_point {1305BPF_TC_INGRESS = 1 << 0,1306BPF_TC_EGRESS = 1 << 1,1307BPF_TC_CUSTOM = 1 << 2,1308BPF_TC_QDISC = 1 << 3,1309};13101311#define BPF_TC_PARENT(a, b) \1312((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))13131314enum bpf_tc_flags {1315BPF_TC_F_REPLACE = 1 << 0,1316};13171318struct bpf_tc_hook {1319size_t sz;1320int ifindex;1321enum bpf_tc_attach_point attach_point;1322__u32 parent;1323__u32 handle;1324const char *qdisc;1325size_t :0;1326};1327#define bpf_tc_hook__last_field qdisc13281329struct bpf_tc_opts {1330size_t sz;1331int prog_fd;1332__u32 flags;1333__u32 prog_id;1334__u32 handle;1335__u32 priority;1336size_t :0;1337};1338#define bpf_tc_opts__last_field priority13391340LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);1341LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);1342LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,1343struct bpf_tc_opts *opts);1344LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,1345const struct bpf_tc_opts *opts);1346LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,1347struct bpf_tc_opts *opts);13481349/* Ring buffer APIs */1350struct ring_buffer;1351struct ring;1352struct user_ring_buffer;13531354typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);13551356struct ring_buffer_opts {1357size_t sz; /* size of this struct, for forward/backward compatibility */1358};13591360#define ring_buffer_opts__last_field sz13611362LIBBPF_API struct ring_buffer *1363ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,1364const struct ring_buffer_opts *opts);1365LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);1366LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,1367ring_buffer_sample_fn sample_cb, void *ctx);1368LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);1369LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);1370LIBBPF_API int ring_buffer__consume_n(struct ring_buffer *rb, size_t n);1371LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);13721373/**1374* @brief **ring_buffer__ring()** returns the ringbuffer object inside a given1375* ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance.1376*1377* @param rb A ringbuffer manager object.1378* @param idx An index into the ringbuffers contained within the ringbuffer1379* manager object. The index is 0-based and corresponds to the order in which1380* ring_buffer__add was called.1381* @return A ringbuffer object on success; NULL and errno set if the index is1382* invalid.1383*/1384LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,1385unsigned int idx);13861387/**1388* @brief **ring__consumer_pos()** returns the current consumer position in the1389* given ringbuffer.1390*1391* @param r A ringbuffer object.1392* @return The current consumer position.1393*/1394LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);13951396/**1397* @brief **ring__producer_pos()** returns the current producer position in the1398* given ringbuffer.1399*1400* @param r A ringbuffer object.1401* @return The current producer position.1402*/1403LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);14041405/**1406* @brief **ring__avail_data_size()** returns the number of bytes in the1407* ringbuffer not yet consumed. This has no locking associated with it, so it1408* can be inaccurate if operations are ongoing while this is called. However, it1409* should still show the correct trend over the long-term.1410*1411* @param r A ringbuffer object.1412* @return The number of bytes not yet consumed.1413*/1414LIBBPF_API size_t ring__avail_data_size(const struct ring *r);14151416/**1417* @brief **ring__size()** returns the total size of the ringbuffer's map data1418* area (excluding special producer/consumer pages). Effectively this gives the1419* amount of usable bytes of data inside the ringbuffer.1420*1421* @param r A ringbuffer object.1422* @return The total size of the ringbuffer map data area.1423*/1424LIBBPF_API size_t ring__size(const struct ring *r);14251426/**1427* @brief **ring__map_fd()** returns the file descriptor underlying the given1428* ringbuffer.1429*1430* @param r A ringbuffer object.1431* @return The underlying ringbuffer file descriptor1432*/1433LIBBPF_API int ring__map_fd(const struct ring *r);14341435/**1436* @brief **ring__consume()** consumes available ringbuffer data without event1437* polling.1438*1439* @param r A ringbuffer object.1440* @return The number of records consumed (or INT_MAX, whichever is less), or1441* a negative number if any of the callbacks return an error.1442*/1443LIBBPF_API int ring__consume(struct ring *r);14441445/**1446* @brief **ring__consume_n()** consumes up to a requested amount of items from1447* a ringbuffer without event polling.1448*1449* @param r A ringbuffer object.1450* @param n Maximum amount of items to consume.1451* @return The number of items consumed, or a negative number if any of the1452* callbacks return an error.1453*/1454LIBBPF_API int ring__consume_n(struct ring *r, size_t n);14551456struct user_ring_buffer_opts {1457size_t sz; /* size of this struct, for forward/backward compatibility */1458};14591460#define user_ring_buffer_opts__last_field sz14611462/**1463* @brief **user_ring_buffer__new()** creates a new instance of a user ring1464* buffer.1465*1466* @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.1467* @param opts Options for how the ring buffer should be created.1468* @return A user ring buffer on success; NULL and errno being set on a1469* failure.1470*/1471LIBBPF_API struct user_ring_buffer *1472user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);14731474/**1475* @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the1476* user ring buffer.1477* @param rb A pointer to a user ring buffer.1478* @param size The size of the sample, in bytes.1479* @return A pointer to an 8-byte aligned reserved region of the user ring1480* buffer; NULL, and errno being set if a sample could not be reserved.1481*1482* This function is *not* thread safe, and callers must synchronize accessing1483* this function if there are multiple producers. If a size is requested that1484* is larger than the size of the entire ring buffer, errno will be set to1485* E2BIG and NULL is returned. If the ring buffer could accommodate the size,1486* but currently does not have enough space, errno is set to ENOSPC and NULL is1487* returned.1488*1489* After initializing the sample, callers must invoke1490* **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,1491* the sample must be freed with **user_ring_buffer__discard()**.1492*/1493LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);14941495/**1496* @brief **user_ring_buffer__reserve_blocking()** reserves a record in the1497* ring buffer, possibly blocking for up to @timeout_ms until a sample becomes1498* available.1499* @param rb The user ring buffer.1500* @param size The size of the sample, in bytes.1501* @param timeout_ms The amount of time, in milliseconds, for which the caller1502* should block when waiting for a sample. -1 causes the caller to block1503* indefinitely.1504* @return A pointer to an 8-byte aligned reserved region of the user ring1505* buffer; NULL, and errno being set if a sample could not be reserved.1506*1507* This function is *not* thread safe, and callers must synchronize1508* accessing this function if there are multiple producers1509*1510* If **timeout_ms** is -1, the function will block indefinitely until a sample1511* becomes available. Otherwise, **timeout_ms** must be non-negative, or errno1512* is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking1513* will occur and the function will return immediately after attempting to1514* reserve a sample.1515*1516* If **size** is larger than the size of the entire ring buffer, errno is set1517* to E2BIG and NULL is returned. If the ring buffer could accommodate1518* **size**, but currently does not have enough space, the caller will block1519* until at most **timeout_ms** has elapsed. If insufficient space is available1520* at that time, errno is set to ENOSPC, and NULL is returned.1521*1522* The kernel guarantees that it will wake up this thread to check if1523* sufficient space is available in the ring buffer at least once per1524* invocation of the **bpf_ringbuf_drain()** helper function, provided that at1525* least one sample is consumed, and the BPF program did not invoke the1526* function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the1527* kernel does not guarantee this. If the helper function is invoked with1528* BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is1529* consumed.1530*1531* When a sample of size **size** is found within **timeout_ms**, a pointer to1532* the sample is returned. After initializing the sample, callers must invoke1533* **user_ring_buffer__submit()** to post the sample to the ring buffer.1534* Otherwise, the sample must be freed with **user_ring_buffer__discard()**.1535*/1536LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,1537__u32 size,1538int timeout_ms);15391540/**1541* @brief **user_ring_buffer__submit()** submits a previously reserved sample1542* into the ring buffer.1543* @param rb The user ring buffer.1544* @param sample A reserved sample.1545*1546* It is not necessary to synchronize amongst multiple producers when invoking1547* this function.1548*/1549LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);15501551/**1552* @brief **user_ring_buffer__discard()** discards a previously reserved sample.1553* @param rb The user ring buffer.1554* @param sample A reserved sample.1555*1556* It is not necessary to synchronize amongst multiple producers when invoking1557* this function.1558*/1559LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);15601561/**1562* @brief **user_ring_buffer__free()** frees a ring buffer that was previously1563* created with **user_ring_buffer__new()**.1564* @param rb The user ring buffer being freed.1565*/1566LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);15671568/* Perf buffer APIs */1569struct perf_buffer;15701571typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,1572void *data, __u32 size);1573typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);15741575/* common use perf buffer options */1576struct perf_buffer_opts {1577size_t sz;1578__u32 sample_period;1579size_t :0;1580};1581#define perf_buffer_opts__last_field sample_period15821583/**1584* @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified1585* BPF_PERF_EVENT_ARRAY map1586* @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF1587* code to send data over to user-space1588* @param page_cnt number of memory pages allocated for each per-CPU buffer1589* @param sample_cb function called on each received data record1590* @param lost_cb function called when record loss has occurred1591* @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*1592* @return a new instance of struct perf_buffer on success, NULL on error with1593* *errno* containing an error code1594*/1595LIBBPF_API struct perf_buffer *1596perf_buffer__new(int map_fd, size_t page_cnt,1597perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,1598const struct perf_buffer_opts *opts);15991600enum bpf_perf_event_ret {1601LIBBPF_PERF_EVENT_DONE = 0,1602LIBBPF_PERF_EVENT_ERROR = -1,1603LIBBPF_PERF_EVENT_CONT = -2,1604};16051606struct perf_event_header;16071608typedef enum bpf_perf_event_ret1609(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);16101611/* raw perf buffer options, giving most power and control */1612struct perf_buffer_raw_opts {1613size_t sz;1614long :0;1615long :0;1616/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of1617* max_entries of given PERF_EVENT_ARRAY map)1618*/1619int cpu_cnt;1620/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */1621int *cpus;1622/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */1623int *map_keys;1624};1625#define perf_buffer_raw_opts__last_field map_keys16261627struct perf_event_attr;16281629LIBBPF_API struct perf_buffer *1630perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,1631perf_buffer_event_fn event_cb, void *ctx,1632const struct perf_buffer_raw_opts *opts);16331634LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);1635LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);1636LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);1637LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);1638LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);1639LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);1640LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);1641/**1642* @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying1643* memory region of the ring buffer.1644* This ring buffer can be used to implement a custom events consumer.1645* The ring buffer starts with the *struct perf_event_mmap_page*, which1646* holds the ring buffer management fields, when accessing the header1647* structure it's important to be SMP aware.1648* You can refer to *perf_event_read_simple* for a simple example.1649* @param pb the perf buffer structure1650* @param buf_idx the buffer index to retrieve1651* @param buf (out) gets the base pointer of the mmap()'ed memory1652* @param buf_size (out) gets the size of the mmap()'ed region1653* @return 0 on success, negative error code for failure1654*/1655LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,1656size_t *buf_size);16571658struct bpf_prog_linfo;1659struct bpf_prog_info;16601661LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);1662LIBBPF_API struct bpf_prog_linfo *1663bpf_prog_linfo__new(const struct bpf_prog_info *info);1664LIBBPF_API const struct bpf_line_info *1665bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,1666__u64 addr, __u32 func_idx, __u32 nr_skip);1667LIBBPF_API const struct bpf_line_info *1668bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,1669__u32 insn_off, __u32 nr_skip);16701671/*1672* Probe for supported system features1673*1674* Note that running many of these probes in a short amount of time can cause1675* the kernel to reach the maximal size of lockable memory allowed for the1676* user, causing subsequent probes to fail. In this case, the caller may want1677* to adjust that limit with setrlimit().1678*/16791680/**1681* @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports1682* BPF programs of a given type.1683* @param prog_type BPF program type to detect kernel support for1684* @param opts reserved for future extensibility, should be NULL1685* @return 1, if given program type is supported; 0, if given program type is1686* not supported; negative error code if feature detection failed or can't be1687* performed1688*1689* Make sure the process has required set of CAP_* permissions (or runs as1690* root) when performing feature checking.1691*/1692LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);1693/**1694* @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports1695* BPF maps of a given type.1696* @param map_type BPF map type to detect kernel support for1697* @param opts reserved for future extensibility, should be NULL1698* @return 1, if given map type is supported; 0, if given map type is1699* not supported; negative error code if feature detection failed or can't be1700* performed1701*1702* Make sure the process has required set of CAP_* permissions (or runs as1703* root) when performing feature checking.1704*/1705LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);1706/**1707* @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the1708* use of a given BPF helper from specified BPF program type.1709* @param prog_type BPF program type used to check the support of BPF helper1710* @param helper_id BPF helper ID (enum bpf_func_id) to check support for1711* @param opts reserved for future extensibility, should be NULL1712* @return 1, if given combination of program type and helper is supported; 0,1713* if the combination is not supported; negative error code if feature1714* detection for provided input arguments failed or can't be performed1715*1716* Make sure the process has required set of CAP_* permissions (or runs as1717* root) when performing feature checking.1718*/1719LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,1720enum bpf_func_id helper_id, const void *opts);17211722/**1723* @brief **libbpf_num_possible_cpus()** is a helper function to get the1724* number of possible CPUs that the host kernel supports and expects.1725* @return number of possible CPUs; or error code on failure1726*1727* Example usage:1728*1729* int ncpus = libbpf_num_possible_cpus();1730* if (ncpus < 0) {1731* // error handling1732* }1733* long values[ncpus];1734* bpf_map_lookup_elem(per_cpu_map_fd, key, values);1735*/1736LIBBPF_API int libbpf_num_possible_cpus(void);17371738struct bpf_map_skeleton {1739const char *name;1740struct bpf_map **map;1741void **mmaped;1742struct bpf_link **link;1743};17441745struct bpf_prog_skeleton {1746const char *name;1747struct bpf_program **prog;1748struct bpf_link **link;1749};17501751struct bpf_object_skeleton {1752size_t sz; /* size of this struct, for forward/backward compatibility */17531754const char *name;1755const void *data;1756size_t data_sz;17571758struct bpf_object **obj;17591760int map_cnt;1761int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */1762struct bpf_map_skeleton *maps;17631764int prog_cnt;1765int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */1766struct bpf_prog_skeleton *progs;1767};17681769LIBBPF_API int1770bpf_object__open_skeleton(struct bpf_object_skeleton *s,1771const struct bpf_object_open_opts *opts);1772LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);1773LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);1774LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);1775LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);17761777struct bpf_var_skeleton {1778const char *name;1779struct bpf_map **map;1780void **addr;1781};17821783struct bpf_object_subskeleton {1784size_t sz; /* size of this struct, for forward/backward compatibility */17851786const struct bpf_object *obj;17871788int map_cnt;1789int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */1790struct bpf_map_skeleton *maps;17911792int prog_cnt;1793int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */1794struct bpf_prog_skeleton *progs;17951796int var_cnt;1797int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */1798struct bpf_var_skeleton *vars;1799};18001801LIBBPF_API int1802bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);1803LIBBPF_API void1804bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);18051806struct gen_loader_opts {1807size_t sz; /* size of this struct, for forward/backward compatibility */1808const char *data;1809const char *insns;1810__u32 data_sz;1811__u32 insns_sz;1812};18131814#define gen_loader_opts__last_field insns_sz1815LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,1816struct gen_loader_opts *opts);18171818enum libbpf_tristate {1819TRI_NO = 0,1820TRI_YES = 1,1821TRI_MODULE = 2,1822};18231824struct bpf_linker_opts {1825/* size of this struct, for forward/backward compatibility */1826size_t sz;1827};1828#define bpf_linker_opts__last_field sz18291830struct bpf_linker_file_opts {1831/* size of this struct, for forward/backward compatibility */1832size_t sz;1833};1834#define bpf_linker_file_opts__last_field sz18351836struct bpf_linker;18371838LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);1839LIBBPF_API struct bpf_linker *bpf_linker__new_fd(int fd, struct bpf_linker_opts *opts);1840LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,1841const char *filename,1842const struct bpf_linker_file_opts *opts);1843LIBBPF_API int bpf_linker__add_fd(struct bpf_linker *linker, int fd,1844const struct bpf_linker_file_opts *opts);1845LIBBPF_API int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz,1846const struct bpf_linker_file_opts *opts);1847LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);1848LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);18491850/*1851* Custom handling of BPF program's SEC() definitions1852*/18531854struct bpf_prog_load_opts; /* defined in bpf.h */18551856/* Called during bpf_object__open() for each recognized BPF program. Callback1857* can use various bpf_program__set_*() setters to adjust whatever properties1858* are necessary.1859*/1860typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);18611862/* Called right before libbpf performs bpf_prog_load() to load BPF program1863* into the kernel. Callback can adjust opts as necessary.1864*/1865typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,1866struct bpf_prog_load_opts *opts, long cookie);18671868/* Called during skeleton attach or through bpf_program__attach(). If1869* auto-attach is not supported, callback should return 0 and set link to1870* NULL (it's not considered an error during skeleton attach, but it will be1871* an error for bpf_program__attach() calls). On error, error should be1872* returned directly and link set to NULL. On success, return 0 and set link1873* to a valid struct bpf_link.1874*/1875typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,1876struct bpf_link **link);18771878struct libbpf_prog_handler_opts {1879/* size of this struct, for forward/backward compatibility */1880size_t sz;1881/* User-provided value that is passed to prog_setup_fn,1882* prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to1883* register one set of callbacks for multiple SEC() definitions and1884* still be able to distinguish them, if necessary. For example,1885* libbpf itself is using this to pass necessary flags (e.g.,1886* sleepable flag) to a common internal SEC() handler.1887*/1888long cookie;1889/* BPF program initialization callback (see libbpf_prog_setup_fn_t).1890* Callback is optional, pass NULL if it's not necessary.1891*/1892libbpf_prog_setup_fn_t prog_setup_fn;1893/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).1894* Callback is optional, pass NULL if it's not necessary.1895*/1896libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;1897/* BPF program attach callback (see libbpf_prog_attach_fn_t).1898* Callback is optional, pass NULL if it's not necessary.1899*/1900libbpf_prog_attach_fn_t prog_attach_fn;1901};1902#define libbpf_prog_handler_opts__last_field prog_attach_fn19031904/**1905* @brief **libbpf_register_prog_handler()** registers a custom BPF program1906* SEC() handler.1907* @param sec section prefix for which custom handler is registered1908* @param prog_type BPF program type associated with specified section1909* @param exp_attach_type Expected BPF attach type associated with specified section1910* @param opts optional cookie, callbacks, and other extra options1911* @return Non-negative handler ID is returned on success. This handler ID has1912* to be passed to *libbpf_unregister_prog_handler()* to unregister such1913* custom handler. Negative error code is returned on error.1914*1915* *sec* defines which SEC() definitions are handled by this custom handler1916* registration. *sec* can have few different forms:1917* - if *sec* is just a plain string (e.g., "abc"), it will match only1918* SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result1919* in an error;1920* - if *sec* is of the form "abc/", proper SEC() form is1921* SEC("abc/something"), where acceptable "something" should be checked by1922* *prog_init_fn* callback, if there are additional restrictions;1923* - if *sec* is of the form "abc+", it will successfully match both1924* SEC("abc") and SEC("abc/whatever") forms;1925* - if *sec* is NULL, custom handler is registered for any BPF program that1926* doesn't match any of the registered (custom or libbpf's own) SEC()1927* handlers. There could be only one such generic custom handler registered1928* at any given time.1929*1930* All custom handlers (except the one with *sec* == NULL) are processed1931* before libbpf's own SEC() handlers. It is allowed to "override" libbpf's1932* SEC() handlers by registering custom ones for the same section prefix1933* (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")1934* handler).1935*1936* Note, like much of global libbpf APIs (e.g., libbpf_set_print(),1937* libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs1938* to ensure synchronization if there is a risk of running this API from1939* multiple threads simultaneously.1940*/1941LIBBPF_API int libbpf_register_prog_handler(const char *sec,1942enum bpf_prog_type prog_type,1943enum bpf_attach_type exp_attach_type,1944const struct libbpf_prog_handler_opts *opts);1945/**1946* @brief *libbpf_unregister_prog_handler()* unregisters previously registered1947* custom BPF program SEC() handler.1948* @param handler_id handler ID returned by *libbpf_register_prog_handler()*1949* after successful registration1950* @return 0 on success, negative error code if handler isn't found1951*1952* Note, like much of global libbpf APIs (e.g., libbpf_set_print(),1953* libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs1954* to ensure synchronization if there is a risk of running this API from1955* multiple threads simultaneously.1956*/1957LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);19581959#ifdef __cplusplus1960} /* extern "C" */1961#endif19621963#endif /* __LIBBPF_LIBBPF_H */196419651966