/* 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 the programs451* 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 specified by a path. This decrements the links484* reference count.485*486* The file pinning the BPF link can also be unlinked by a different487* process in which case this function will return an error.488*489* @param prog BPF program to unpin490* @param path file path to the pin in a BPF file system491* @return 0, on success; negative error code, otherwise492*/493LIBBPF_API int bpf_link__unpin(struct bpf_link *link);494LIBBPF_API int bpf_link__update_program(struct bpf_link *link,495struct bpf_program *prog);496LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);497LIBBPF_API int bpf_link__detach(struct bpf_link *link);498LIBBPF_API int bpf_link__destroy(struct bpf_link *link);499500/**501* @brief **bpf_program__attach()** is a generic function for attaching502* a BPF program based on auto-detection of program type, attach type,503* and extra parameters, where applicable.504*505* @param prog BPF program to attach506* @return Reference to the newly created BPF link; or NULL is returned on error,507* error code is stored in errno508*509* This is supported for:510* - kprobe/kretprobe (depends on SEC() definition)511* - uprobe/uretprobe (depends on SEC() definition)512* - tracepoint513* - raw tracepoint514* - tracing programs (typed raw TP/fentry/fexit/fmod_ret)515*/516LIBBPF_API struct bpf_link *517bpf_program__attach(const struct bpf_program *prog);518519struct bpf_perf_event_opts {520/* size of this struct, for forward/backward compatibility */521size_t sz;522/* custom user-provided value fetchable through bpf_get_attach_cookie() */523__u64 bpf_cookie;524/* don't use BPF link when attach BPF program */525bool force_ioctl_attach;526/* don't automatically enable the event */527bool dont_enable;528size_t :0;529};530#define bpf_perf_event_opts__last_field dont_enable531532LIBBPF_API struct bpf_link *533bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);534535LIBBPF_API struct bpf_link *536bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,537const struct bpf_perf_event_opts *opts);538539/**540* enum probe_attach_mode - the mode to attach kprobe/uprobe541*542* force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will543* be returned if it is not supported by the kernel.544*/545enum probe_attach_mode {546/* attach probe in latest supported mode by kernel */547PROBE_ATTACH_MODE_DEFAULT = 0,548/* attach probe in legacy mode, using debugfs/tracefs */549PROBE_ATTACH_MODE_LEGACY,550/* create perf event with perf_event_open() syscall */551PROBE_ATTACH_MODE_PERF,552/* attach probe with BPF link */553PROBE_ATTACH_MODE_LINK,554};555556struct bpf_kprobe_opts {557/* size of this struct, for forward/backward compatibility */558size_t sz;559/* custom user-provided value fetchable through bpf_get_attach_cookie() */560__u64 bpf_cookie;561/* function's offset to install kprobe to */562size_t offset;563/* kprobe is return probe */564bool retprobe;565/* kprobe attach mode */566enum probe_attach_mode attach_mode;567size_t :0;568};569#define bpf_kprobe_opts__last_field attach_mode570571LIBBPF_API struct bpf_link *572bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,573const char *func_name);574LIBBPF_API struct bpf_link *575bpf_program__attach_kprobe_opts(const struct bpf_program *prog,576const char *func_name,577const struct bpf_kprobe_opts *opts);578579struct bpf_kprobe_multi_opts {580/* size of this struct, for forward/backward compatibility */581size_t sz;582/* array of function symbols to attach */583const char **syms;584/* array of function addresses to attach */585const unsigned long *addrs;586/* array of user-provided values fetchable through bpf_get_attach_cookie */587const __u64 *cookies;588/* number of elements in syms/addrs/cookies arrays */589size_t cnt;590/* create return kprobes */591bool retprobe;592/* create session kprobes */593bool session;594/* enforce unique match */595bool unique_match;596size_t :0;597};598599#define bpf_kprobe_multi_opts__last_field unique_match600601LIBBPF_API struct bpf_link *602bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,603const char *pattern,604const struct bpf_kprobe_multi_opts *opts);605606struct bpf_uprobe_multi_opts {607/* size of this struct, for forward/backward compatibility */608size_t sz;609/* array of function symbols to attach to */610const char **syms;611/* array of function addresses to attach to */612const unsigned long *offsets;613/* optional, array of associated ref counter offsets */614const unsigned long *ref_ctr_offsets;615/* optional, array of associated BPF cookies */616const __u64 *cookies;617/* number of elements in syms/addrs/cookies arrays */618size_t cnt;619/* create return uprobes */620bool retprobe;621/* create session kprobes */622bool session;623size_t :0;624};625626#define bpf_uprobe_multi_opts__last_field session627628/**629* @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program630* to multiple uprobes with uprobe_multi link.631*632* User can specify 2 mutually exclusive set of inputs:633*634* 1) use only path/func_pattern/pid arguments635*636* 2) use path/pid with allowed combinations of637* syms/offsets/ref_ctr_offsets/cookies/cnt638*639* - syms and offsets are mutually exclusive640* - ref_ctr_offsets and cookies are optional641*642*643* @param prog BPF program to attach644* @param pid Process ID to attach the uprobe to, 0 for self (own process),645* -1 for all processes646* @param binary_path Path to binary647* @param func_pattern Regular expression to specify functions to attach648* BPF program to649* @param opts Additional options (see **struct bpf_uprobe_multi_opts**)650* @return 0, on success; negative error code, otherwise651*/652LIBBPF_API struct bpf_link *653bpf_program__attach_uprobe_multi(const struct bpf_program *prog,654pid_t pid,655const char *binary_path,656const char *func_pattern,657const struct bpf_uprobe_multi_opts *opts);658659struct bpf_ksyscall_opts {660/* size of this struct, for forward/backward compatibility */661size_t sz;662/* custom user-provided value fetchable through bpf_get_attach_cookie() */663__u64 bpf_cookie;664/* attach as return probe? */665bool retprobe;666size_t :0;667};668#define bpf_ksyscall_opts__last_field retprobe669670/**671* @brief **bpf_program__attach_ksyscall()** attaches a BPF program672* to kernel syscall handler of a specified syscall. Optionally it's possible673* to request to install retprobe that will be triggered at syscall exit. It's674* also possible to associate BPF cookie (though options).675*676* Libbpf automatically will determine correct full kernel function name,677* which depending on system architecture and kernel version/configuration678* could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will679* attach specified program using kprobe/kretprobe mechanism.680*681* **bpf_program__attach_ksyscall()** is an API counterpart of declarative682* **SEC("ksyscall/<syscall>")** annotation of BPF programs.683*684* At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do685* not handle all the calling convention quirks for mmap(), clone() and compat686* syscalls. It also only attaches to "native" syscall interfaces. If host687* system supports compat syscalls or defines 32-bit syscalls in 64-bit688* kernel, such syscall interfaces won't be attached to by libbpf.689*690* These limitations may or may not change in the future. Therefore it is691* recommended to use SEC("kprobe") for these syscalls or if working with692* compat and 32-bit interfaces is required.693*694* @param prog BPF program to attach695* @param syscall_name Symbolic name of the syscall (e.g., "bpf")696* @param opts Additional options (see **struct bpf_ksyscall_opts**)697* @return Reference to the newly created BPF link; or NULL is returned on698* error, error code is stored in errno699*/700LIBBPF_API struct bpf_link *701bpf_program__attach_ksyscall(const struct bpf_program *prog,702const char *syscall_name,703const struct bpf_ksyscall_opts *opts);704705struct bpf_uprobe_opts {706/* size of this struct, for forward/backward compatibility */707size_t sz;708/* offset of kernel reference counted USDT semaphore, added in709* a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")710*/711size_t ref_ctr_offset;712/* custom user-provided value fetchable through bpf_get_attach_cookie() */713__u64 bpf_cookie;714/* uprobe is return probe, invoked at function return time */715bool retprobe;716/* Function name to attach to. Could be an unqualified ("abc") or library-qualified717* "abc@LIBXYZ" name. To specify function entry, func_name should be set while718* func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an719* offset within a function, specify func_name and use func_offset argument to specify720* offset within the function. Shared library functions must specify the shared library721* binary_path.722*/723const char *func_name;724/* uprobe attach mode */725enum probe_attach_mode attach_mode;726size_t :0;727};728#define bpf_uprobe_opts__last_field attach_mode729730/**731* @brief **bpf_program__attach_uprobe()** attaches a BPF program732* to the userspace function which is found by binary path and733* offset. You can optionally specify a particular process to attach734* to. You can also optionally attach the program to the function735* exit instead of entry.736*737* @param prog BPF program to attach738* @param retprobe Attach to function exit739* @param pid Process ID to attach the uprobe to, 0 for self (own process),740* -1 for all processes741* @param binary_path Path to binary that contains the function symbol742* @param func_offset Offset within the binary of the function symbol743* @return Reference to the newly created BPF link; or NULL is returned on error,744* error code is stored in errno745*/746LIBBPF_API struct bpf_link *747bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,748pid_t pid, const char *binary_path,749size_t func_offset);750751/**752* @brief **bpf_program__attach_uprobe_opts()** is just like753* bpf_program__attach_uprobe() except with a options struct754* for various configurations.755*756* @param prog BPF program to attach757* @param pid Process ID to attach the uprobe to, 0 for self (own process),758* -1 for all processes759* @param binary_path Path to binary that contains the function symbol760* @param func_offset Offset within the binary of the function symbol761* @param opts Options for altering program attachment762* @return Reference to the newly created BPF link; or NULL is returned on error,763* error code is stored in errno764*/765LIBBPF_API struct bpf_link *766bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,767const char *binary_path, size_t func_offset,768const struct bpf_uprobe_opts *opts);769770struct bpf_usdt_opts {771/* size of this struct, for forward/backward compatibility */772size_t sz;773/* custom user-provided value accessible through usdt_cookie() */774__u64 usdt_cookie;775size_t :0;776};777#define bpf_usdt_opts__last_field usdt_cookie778779/**780* @brief **bpf_program__attach_usdt()** is just like781* bpf_program__attach_uprobe_opts() except it covers USDT (User-space782* Statically Defined Tracepoint) attachment, instead of attaching to783* user-space function entry or exit.784*785* @param prog BPF program to attach786* @param pid Process ID to attach the uprobe to, 0 for self (own process),787* -1 for all processes788* @param binary_path Path to binary that contains provided USDT probe789* @param usdt_provider USDT provider name790* @param usdt_name USDT probe name791* @param opts Options for altering program attachment792* @return Reference to the newly created BPF link; or NULL is returned on error,793* error code is stored in errno794*/795LIBBPF_API struct bpf_link *796bpf_program__attach_usdt(const struct bpf_program *prog,797pid_t pid, const char *binary_path,798const char *usdt_provider, const char *usdt_name,799const struct bpf_usdt_opts *opts);800801struct bpf_tracepoint_opts {802/* size of this struct, for forward/backward compatibility */803size_t sz;804/* custom user-provided value fetchable through bpf_get_attach_cookie() */805__u64 bpf_cookie;806};807#define bpf_tracepoint_opts__last_field bpf_cookie808809LIBBPF_API struct bpf_link *810bpf_program__attach_tracepoint(const struct bpf_program *prog,811const char *tp_category,812const char *tp_name);813LIBBPF_API struct bpf_link *814bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,815const char *tp_category,816const char *tp_name,817const struct bpf_tracepoint_opts *opts);818819struct bpf_raw_tracepoint_opts {820size_t sz; /* size of this struct for forward/backward compatibility */821__u64 cookie;822size_t :0;823};824#define bpf_raw_tracepoint_opts__last_field cookie825826LIBBPF_API struct bpf_link *827bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,828const char *tp_name);829LIBBPF_API struct bpf_link *830bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog,831const char *tp_name,832struct bpf_raw_tracepoint_opts *opts);833834struct bpf_trace_opts {835/* size of this struct, for forward/backward compatibility */836size_t sz;837/* custom user-provided value fetchable through bpf_get_attach_cookie() */838__u64 cookie;839};840#define bpf_trace_opts__last_field cookie841842LIBBPF_API struct bpf_link *843bpf_program__attach_trace(const struct bpf_program *prog);844LIBBPF_API struct bpf_link *845bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);846847LIBBPF_API struct bpf_link *848bpf_program__attach_lsm(const struct bpf_program *prog);849LIBBPF_API struct bpf_link *850bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);851LIBBPF_API struct bpf_link *852bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);853LIBBPF_API struct bpf_link *854bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd);855LIBBPF_API struct bpf_link *856bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);857LIBBPF_API struct bpf_link *858bpf_program__attach_freplace(const struct bpf_program *prog,859int target_fd, const char *attach_func_name);860861struct bpf_netfilter_opts {862/* size of this struct, for forward/backward compatibility */863size_t sz;864865__u32 pf;866__u32 hooknum;867__s32 priority;868__u32 flags;869};870#define bpf_netfilter_opts__last_field flags871872LIBBPF_API struct bpf_link *873bpf_program__attach_netfilter(const struct bpf_program *prog,874const struct bpf_netfilter_opts *opts);875876struct bpf_tcx_opts {877/* size of this struct, for forward/backward compatibility */878size_t sz;879__u32 flags;880__u32 relative_fd;881__u32 relative_id;882__u64 expected_revision;883size_t :0;884};885#define bpf_tcx_opts__last_field expected_revision886887LIBBPF_API struct bpf_link *888bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,889const struct bpf_tcx_opts *opts);890891struct bpf_netkit_opts {892/* size of this struct, for forward/backward compatibility */893size_t sz;894__u32 flags;895__u32 relative_fd;896__u32 relative_id;897__u64 expected_revision;898size_t :0;899};900#define bpf_netkit_opts__last_field expected_revision901902LIBBPF_API struct bpf_link *903bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,904const struct bpf_netkit_opts *opts);905906struct bpf_cgroup_opts {907/* size of this struct, for forward/backward compatibility */908size_t sz;909__u32 flags;910__u32 relative_fd;911__u32 relative_id;912__u64 expected_revision;913size_t :0;914};915#define bpf_cgroup_opts__last_field expected_revision916917LIBBPF_API struct bpf_link *918bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,919const struct bpf_cgroup_opts *opts);920921struct bpf_map;922923LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);924LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);925926struct bpf_iter_attach_opts {927size_t sz; /* size of this struct for forward/backward compatibility */928union bpf_iter_link_info *link_info;929__u32 link_info_len;930};931#define bpf_iter_attach_opts__last_field link_info_len932933LIBBPF_API struct bpf_link *934bpf_program__attach_iter(const struct bpf_program *prog,935const struct bpf_iter_attach_opts *opts);936937LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);938939/**940* @brief **bpf_program__set_type()** sets the program941* type of the passed BPF program.942* @param prog BPF program to set the program type for943* @param type program type to set the BPF map to have944* @return error code; or 0 if no error. An error occurs945* if the object is already loaded.946*947* This must be called before the BPF object is loaded,948* otherwise it has no effect and an error is returned.949*/950LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,951enum bpf_prog_type type);952953LIBBPF_API enum bpf_attach_type954bpf_program__expected_attach_type(const struct bpf_program *prog);955956/**957* @brief **bpf_program__set_expected_attach_type()** sets the958* attach type of the passed BPF program. This is used for959* auto-detection of attachment when programs are loaded.960* @param prog BPF program to set the attach type for961* @param type attach type to set the BPF map to have962* @return error code; or 0 if no error. An error occurs963* if the object is already loaded.964*965* This must be called before the BPF object is loaded,966* otherwise it has no effect and an error is returned.967*/968LIBBPF_API int969bpf_program__set_expected_attach_type(struct bpf_program *prog,970enum bpf_attach_type type);971972LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);973LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);974975/* Per-program log level and log buffer getters/setters.976* See bpf_object_open_opts comments regarding log_level and log_buf977* interactions.978*/979LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);980LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);981LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);982LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);983984LIBBPF_API struct bpf_func_info *bpf_program__func_info(const struct bpf_program *prog);985LIBBPF_API __u32 bpf_program__func_info_cnt(const struct bpf_program *prog);986987LIBBPF_API struct bpf_line_info *bpf_program__line_info(const struct bpf_program *prog);988LIBBPF_API __u32 bpf_program__line_info_cnt(const struct bpf_program *prog);989990/**991* @brief **bpf_program__set_attach_target()** sets BTF-based attach target992* for supported BPF program types:993* - BTF-aware raw tracepoints (tp_btf);994* - fentry/fexit/fmod_ret;995* - lsm;996* - freplace.997* @param prog BPF program to set the attach type for998* @param type attach type to set the BPF map to have999* @return error code; or 0 if no error occurred.1000*/1001LIBBPF_API int1002bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,1003const char *attach_func_name);10041005/**1006* @brief **bpf_object__find_map_by_name()** returns BPF map of1007* the given name, if it exists within the passed BPF object1008* @param obj BPF object1009* @param name name of the BPF map1010* @return BPF map instance, if such map exists within the BPF object;1011* or NULL otherwise.1012*/1013LIBBPF_API struct bpf_map *1014bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);10151016LIBBPF_API int1017bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);10181019LIBBPF_API struct bpf_map *1020bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);10211022#define bpf_object__for_each_map(pos, obj) \1023for ((pos) = bpf_object__next_map((obj), NULL); \1024(pos) != NULL; \1025(pos) = bpf_object__next_map((obj), (pos)))1026#define bpf_map__for_each bpf_object__for_each_map10271028LIBBPF_API struct bpf_map *1029bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);10301031/**1032* @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create1033* BPF map during BPF object load phase.1034* @param map the BPF map instance1035* @param autocreate whether to create BPF map during BPF object load1036* @return 0 on success; -EBUSY if BPF object was already loaded1037*1038* **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating1039* BPF map. By default, libbpf will attempt to create every single BPF map1040* defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall1041* and fill in map FD in BPF instructions.1042*1043* This API allows to opt-out of this process for specific map instance. This1044* can be useful if host kernel doesn't support such BPF map type or used1045* combination of flags and user application wants to avoid creating such1046* a map in the first place. User is still responsible to make sure that their1047* BPF-side code that expects to use such missing BPF map is recognized by BPF1048* verifier as dead code, otherwise BPF verifier will reject such BPF program.1049*/1050LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);1051LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);10521053/**1054* @brief **bpf_map__set_autoattach()** sets whether libbpf has to auto-attach1055* map during BPF skeleton attach phase.1056* @param map the BPF map instance1057* @param autoattach whether to attach map during BPF skeleton attach phase1058* @return 0 on success; negative error code, otherwise1059*/1060LIBBPF_API int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach);10611062/**1063* @brief **bpf_map__autoattach()** returns whether BPF map is configured to1064* auto-attach during BPF skeleton attach phase.1065* @param map the BPF map instance1066* @return true if map is set to auto-attach during skeleton attach phase; false, otherwise1067*/1068LIBBPF_API bool bpf_map__autoattach(const struct bpf_map *map);10691070/**1071* @brief **bpf_map__fd()** gets the file descriptor of the passed1072* BPF map1073* @param map the BPF map instance1074* @return the file descriptor; or -EINVAL in case of an error1075*/1076LIBBPF_API int bpf_map__fd(const struct bpf_map *map);1077LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);1078/* get map name */1079LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);1080/* get/set map type */1081LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);1082LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);1083/* get/set map size (max_entries) */1084LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);1085LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);1086/* get/set map flags */1087LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);1088LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);1089/* get/set map NUMA node */1090LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);1091LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);1092/* get/set map key size */1093LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);1094LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);1095/* get map value size */1096LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);1097/**1098* @brief **bpf_map__set_value_size()** sets map value size.1099* @param map the BPF map instance1100* @return 0, on success; negative error, otherwise1101*1102* There is a special case for maps with associated memory-mapped regions, like1103* the global data section maps (bss, data, rodata). When this function is used1104* on such a map, the mapped region is resized. Afterward, an attempt is made to1105* adjust the corresponding BTF info. This attempt is best-effort and can only1106* succeed if the last variable of the data section map is an array. The array1107* BTF type is replaced by a new BTF array type with a different length.1108* Any previously existing pointers returned from bpf_map__initial_value() or1109* corresponding data section skeleton pointer must be reinitialized.1110*/1111LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);1112/* get map key/value BTF type IDs */1113LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);1114LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);1115/* get/set map if_index */1116LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);1117LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);1118/* get/set map map_extra flags */1119LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);1120LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);11211122LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,1123const void *data, size_t size);1124LIBBPF_API void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize);11251126/**1127* @brief **bpf_map__is_internal()** tells the caller whether or not the1128* passed map is a special map created by libbpf automatically for things like1129* global variables, __ksym externs, Kconfig values, etc1130* @param map the bpf_map1131* @return true, if the map is an internal map; false, otherwise1132*/1133LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);11341135/**1136* @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the1137* BPF map should be pinned. This does not actually create the 'pin'.1138* @param map The bpf_map1139* @param path The path1140* @return 0, on success; negative error, otherwise1141*/1142LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);11431144/**1145* @brief **bpf_map__pin_path()** gets the path attribute that tells where the1146* BPF map should be pinned.1147* @param map The bpf_map1148* @return The path string; which can be NULL1149*/1150LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);11511152/**1153* @brief **bpf_map__is_pinned()** tells the caller whether or not the1154* passed map has been pinned via a 'pin' file.1155* @param map The bpf_map1156* @return true, if the map is pinned; false, otherwise1157*/1158LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);11591160/**1161* @brief **bpf_map__pin()** creates a file that serves as a 'pin'1162* for the BPF map. This increments the reference count on the1163* BPF map which will keep the BPF map loaded even after the1164* userspace process which loaded it has exited.1165* @param map The bpf_map to pin1166* @param path A file path for the 'pin'1167* @return 0, on success; negative error, otherwise1168*1169* If `path` is NULL the maps `pin_path` attribute will be used. If this is1170* also NULL, an error will be returned and the map will not be pinned.1171*/1172LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);11731174/**1175* @brief **bpf_map__unpin()** removes the file that serves as a1176* 'pin' for the BPF map.1177* @param map The bpf_map to unpin1178* @param path A file path for the 'pin'1179* @return 0, on success; negative error, otherwise1180*1181* The `path` parameter can be NULL, in which case the `pin_path`1182* map attribute is unpinned. If both the `path` parameter and1183* `pin_path` map attribute are set, they must be equal.1184*/1185LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);11861187LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);1188LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);11891190/**1191* @brief **bpf_map__lookup_elem()** allows to lookup BPF map value1192* corresponding to provided key.1193* @param map BPF map to lookup element in1194* @param key pointer to memory containing bytes of the key used for lookup1195* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1196* @param value pointer to memory in which looked up value will be stored1197* @param value_sz size in byte of value data memory; it has to match BPF map1198* definition's **value_size**. For per-CPU BPF maps value size has to be1199* a product of BPF map value size and number of possible CPUs in the system1200* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1201* per-CPU values value size has to be aligned up to closest 8 bytes for1202* alignment reasons, so expected size is: `round_up(value_size, 8)1203* * libbpf_num_possible_cpus()`.1204* @flags extra flags passed to kernel for this operation1205* @return 0, on success; negative error, otherwise1206*1207* **bpf_map__lookup_elem()** is high-level equivalent of1208* **bpf_map_lookup_elem()** API with added check for key and value size.1209*/1210LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,1211const void *key, size_t key_sz,1212void *value, size_t value_sz, __u64 flags);12131214/**1215* @brief **bpf_map__update_elem()** allows to insert or update value in BPF1216* map that corresponds to provided key.1217* @param map BPF map to insert to or update element in1218* @param key pointer to memory containing bytes of the key1219* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1220* @param value pointer to memory containing bytes of the value1221* @param value_sz size in byte of value data memory; it has to match BPF map1222* definition's **value_size**. For per-CPU BPF maps value size has to be1223* a product of BPF map value size and number of possible CPUs in the system1224* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1225* per-CPU values value size has to be aligned up to closest 8 bytes for1226* alignment reasons, so expected size is: `round_up(value_size, 8)1227* * libbpf_num_possible_cpus()`.1228* @flags extra flags passed to kernel for this operation1229* @return 0, on success; negative error, otherwise1230*1231* **bpf_map__update_elem()** is high-level equivalent of1232* **bpf_map_update_elem()** API with added check for key and value size.1233*/1234LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,1235const void *key, size_t key_sz,1236const void *value, size_t value_sz, __u64 flags);12371238/**1239* @brief **bpf_map__delete_elem()** allows to delete element in BPF map that1240* corresponds to provided key.1241* @param map BPF map to delete element from1242* @param key pointer to memory containing bytes of the key1243* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1244* @flags extra flags passed to kernel for this operation1245* @return 0, on success; negative error, otherwise1246*1247* **bpf_map__delete_elem()** is high-level equivalent of1248* **bpf_map_delete_elem()** API with added check for key size.1249*/1250LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,1251const void *key, size_t key_sz, __u64 flags);12521253/**1254* @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value1255* corresponding to provided key and atomically delete it afterwards.1256* @param map BPF map to lookup element in1257* @param key pointer to memory containing bytes of the key used for lookup1258* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1259* @param value pointer to memory in which looked up value will be stored1260* @param value_sz size in byte of value data memory; it has to match BPF map1261* definition's **value_size**. For per-CPU BPF maps value size has to be1262* a product of BPF map value size and number of possible CPUs in the system1263* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for1264* per-CPU values value size has to be aligned up to closest 8 bytes for1265* alignment reasons, so expected size is: `round_up(value_size, 8)1266* * libbpf_num_possible_cpus()`.1267* @flags extra flags passed to kernel for this operation1268* @return 0, on success; negative error, otherwise1269*1270* **bpf_map__lookup_and_delete_elem()** is high-level equivalent of1271* **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.1272*/1273LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,1274const void *key, size_t key_sz,1275void *value, size_t value_sz, __u64 flags);12761277/**1278* @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by1279* fetching next key that follows current key.1280* @param map BPF map to fetch next key from1281* @param cur_key pointer to memory containing bytes of current key or NULL to1282* fetch the first key1283* @param next_key pointer to memory to write next key into1284* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**1285* @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;1286* negative error, otherwise1287*1288* **bpf_map__get_next_key()** is high-level equivalent of1289* **bpf_map_get_next_key()** API with added check for key size.1290*/1291LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,1292const void *cur_key, void *next_key, size_t key_sz);1293/**1294* @brief **bpf_map__set_exclusive_program()** sets a map to be exclusive to the1295* specified program. This must be called *before* the map is created.1296*1297* @param map BPF map to make exclusive.1298* @param prog BPF program to be the exclusive user of the map. Must belong1299* to the same bpf_object as the map.1300* @return 0 on success; a negative error code otherwise.1301*1302* This function must be called after the BPF object is opened but before1303* it is loaded. Once the object is loaded, only the specified program1304* will be able to access the map's contents.1305*/1306LIBBPF_API int bpf_map__set_exclusive_program(struct bpf_map *map, struct bpf_program *prog);13071308/**1309* @brief **bpf_map__exclusive_program()** returns the exclusive program1310* that is registered with the map (if any).1311* @param map BPF map to which the exclusive program is registered.1312* @return the registered exclusive program.1313*/1314LIBBPF_API struct bpf_program *bpf_map__exclusive_program(struct bpf_map *map);13151316struct bpf_xdp_set_link_opts {1317size_t sz;1318int old_fd;1319size_t :0;1320};1321#define bpf_xdp_set_link_opts__last_field old_fd13221323struct bpf_xdp_attach_opts {1324size_t sz;1325int old_prog_fd;1326size_t :0;1327};1328#define bpf_xdp_attach_opts__last_field old_prog_fd13291330struct bpf_xdp_query_opts {1331size_t sz;1332__u32 prog_id; /* output */1333__u32 drv_prog_id; /* output */1334__u32 hw_prog_id; /* output */1335__u32 skb_prog_id; /* output */1336__u8 attach_mode; /* output */1337__u64 feature_flags; /* output */1338__u32 xdp_zc_max_segs; /* output */1339size_t :0;1340};1341#define bpf_xdp_query_opts__last_field xdp_zc_max_segs13421343LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,1344const struct bpf_xdp_attach_opts *opts);1345LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,1346const struct bpf_xdp_attach_opts *opts);1347LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);1348LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);13491350/* TC related API */1351enum bpf_tc_attach_point {1352BPF_TC_INGRESS = 1 << 0,1353BPF_TC_EGRESS = 1 << 1,1354BPF_TC_CUSTOM = 1 << 2,1355BPF_TC_QDISC = 1 << 3,1356};13571358#define BPF_TC_PARENT(a, b) \1359((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))13601361enum bpf_tc_flags {1362BPF_TC_F_REPLACE = 1 << 0,1363};13641365struct bpf_tc_hook {1366size_t sz;1367int ifindex;1368enum bpf_tc_attach_point attach_point;1369__u32 parent;1370__u32 handle;1371const char *qdisc;1372size_t :0;1373};1374#define bpf_tc_hook__last_field qdisc13751376struct bpf_tc_opts {1377size_t sz;1378int prog_fd;1379__u32 flags;1380__u32 prog_id;1381__u32 handle;1382__u32 priority;1383size_t :0;1384};1385#define bpf_tc_opts__last_field priority13861387LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);1388LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);1389LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,1390struct bpf_tc_opts *opts);1391LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,1392const struct bpf_tc_opts *opts);1393LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,1394struct bpf_tc_opts *opts);13951396/* Ring buffer APIs */1397struct ring_buffer;1398struct ring;1399struct user_ring_buffer;14001401typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);14021403struct ring_buffer_opts {1404size_t sz; /* size of this struct, for forward/backward compatibility */1405};14061407#define ring_buffer_opts__last_field sz14081409LIBBPF_API struct ring_buffer *1410ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,1411const struct ring_buffer_opts *opts);1412LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);1413LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,1414ring_buffer_sample_fn sample_cb, void *ctx);1415LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);1416LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);1417LIBBPF_API int ring_buffer__consume_n(struct ring_buffer *rb, size_t n);1418LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);14191420/**1421* @brief **ring_buffer__ring()** returns the ringbuffer object inside a given1422* ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance.1423*1424* @param rb A ringbuffer manager object.1425* @param idx An index into the ringbuffers contained within the ringbuffer1426* manager object. The index is 0-based and corresponds to the order in which1427* ring_buffer__add was called.1428* @return A ringbuffer object on success; NULL and errno set if the index is1429* invalid.1430*/1431LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,1432unsigned int idx);14331434/**1435* @brief **ring__consumer_pos()** returns the current consumer position in the1436* given ringbuffer.1437*1438* @param r A ringbuffer object.1439* @return The current consumer position.1440*/1441LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);14421443/**1444* @brief **ring__producer_pos()** returns the current producer position in the1445* given ringbuffer.1446*1447* @param r A ringbuffer object.1448* @return The current producer position.1449*/1450LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);14511452/**1453* @brief **ring__avail_data_size()** returns the number of bytes in the1454* ringbuffer not yet consumed. This has no locking associated with it, so it1455* can be inaccurate if operations are ongoing while this is called. However, it1456* should still show the correct trend over the long-term.1457*1458* @param r A ringbuffer object.1459* @return The number of bytes not yet consumed.1460*/1461LIBBPF_API size_t ring__avail_data_size(const struct ring *r);14621463/**1464* @brief **ring__size()** returns the total size of the ringbuffer's map data1465* area (excluding special producer/consumer pages). Effectively this gives the1466* amount of usable bytes of data inside the ringbuffer.1467*1468* @param r A ringbuffer object.1469* @return The total size of the ringbuffer map data area.1470*/1471LIBBPF_API size_t ring__size(const struct ring *r);14721473/**1474* @brief **ring__map_fd()** returns the file descriptor underlying the given1475* ringbuffer.1476*1477* @param r A ringbuffer object.1478* @return The underlying ringbuffer file descriptor1479*/1480LIBBPF_API int ring__map_fd(const struct ring *r);14811482/**1483* @brief **ring__consume()** consumes available ringbuffer data without event1484* polling.1485*1486* @param r A ringbuffer object.1487* @return The number of records consumed (or INT_MAX, whichever is less), or1488* a negative number if any of the callbacks return an error.1489*/1490LIBBPF_API int ring__consume(struct ring *r);14911492/**1493* @brief **ring__consume_n()** consumes up to a requested amount of items from1494* a ringbuffer without event polling.1495*1496* @param r A ringbuffer object.1497* @param n Maximum amount of items to consume.1498* @return The number of items consumed, or a negative number if any of the1499* callbacks return an error.1500*/1501LIBBPF_API int ring__consume_n(struct ring *r, size_t n);15021503struct user_ring_buffer_opts {1504size_t sz; /* size of this struct, for forward/backward compatibility */1505};15061507#define user_ring_buffer_opts__last_field sz15081509/**1510* @brief **user_ring_buffer__new()** creates a new instance of a user ring1511* buffer.1512*1513* @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.1514* @param opts Options for how the ring buffer should be created.1515* @return A user ring buffer on success; NULL and errno being set on a1516* failure.1517*/1518LIBBPF_API struct user_ring_buffer *1519user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);15201521/**1522* @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the1523* user ring buffer.1524* @param rb A pointer to a user ring buffer.1525* @param size The size of the sample, in bytes.1526* @return A pointer to an 8-byte aligned reserved region of the user ring1527* buffer; NULL, and errno being set if a sample could not be reserved.1528*1529* This function is *not* thread safe, and callers must synchronize accessing1530* this function if there are multiple producers. If a size is requested that1531* is larger than the size of the entire ring buffer, errno will be set to1532* E2BIG and NULL is returned. If the ring buffer could accommodate the size,1533* but currently does not have enough space, errno is set to ENOSPC and NULL is1534* returned.1535*1536* After initializing the sample, callers must invoke1537* **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,1538* the sample must be freed with **user_ring_buffer__discard()**.1539*/1540LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);15411542/**1543* @brief **user_ring_buffer__reserve_blocking()** reserves a record in the1544* ring buffer, possibly blocking for up to @timeout_ms until a sample becomes1545* available.1546* @param rb The user ring buffer.1547* @param size The size of the sample, in bytes.1548* @param timeout_ms The amount of time, in milliseconds, for which the caller1549* should block when waiting for a sample. -1 causes the caller to block1550* indefinitely.1551* @return A pointer to an 8-byte aligned reserved region of the user ring1552* buffer; NULL, and errno being set if a sample could not be reserved.1553*1554* This function is *not* thread safe, and callers must synchronize1555* accessing this function if there are multiple producers1556*1557* If **timeout_ms** is -1, the function will block indefinitely until a sample1558* becomes available. Otherwise, **timeout_ms** must be non-negative, or errno1559* is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking1560* will occur and the function will return immediately after attempting to1561* reserve a sample.1562*1563* If **size** is larger than the size of the entire ring buffer, errno is set1564* to E2BIG and NULL is returned. If the ring buffer could accommodate1565* **size**, but currently does not have enough space, the caller will block1566* until at most **timeout_ms** has elapsed. If insufficient space is available1567* at that time, errno is set to ENOSPC, and NULL is returned.1568*1569* The kernel guarantees that it will wake up this thread to check if1570* sufficient space is available in the ring buffer at least once per1571* invocation of the **bpf_ringbuf_drain()** helper function, provided that at1572* least one sample is consumed, and the BPF program did not invoke the1573* function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the1574* kernel does not guarantee this. If the helper function is invoked with1575* BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is1576* consumed.1577*1578* When a sample of size **size** is found within **timeout_ms**, a pointer to1579* the sample is returned. After initializing the sample, callers must invoke1580* **user_ring_buffer__submit()** to post the sample to the ring buffer.1581* Otherwise, the sample must be freed with **user_ring_buffer__discard()**.1582*/1583LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,1584__u32 size,1585int timeout_ms);15861587/**1588* @brief **user_ring_buffer__submit()** submits a previously reserved sample1589* into the ring buffer.1590* @param rb The user ring buffer.1591* @param sample A reserved sample.1592*1593* It is not necessary to synchronize amongst multiple producers when invoking1594* this function.1595*/1596LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);15971598/**1599* @brief **user_ring_buffer__discard()** discards a previously reserved sample.1600* @param rb The user ring buffer.1601* @param sample A reserved sample.1602*1603* It is not necessary to synchronize amongst multiple producers when invoking1604* this function.1605*/1606LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);16071608/**1609* @brief **user_ring_buffer__free()** frees a ring buffer that was previously1610* created with **user_ring_buffer__new()**.1611* @param rb The user ring buffer being freed.1612*/1613LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);16141615/* Perf buffer APIs */1616struct perf_buffer;16171618typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,1619void *data, __u32 size);1620typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);16211622/* common use perf buffer options */1623struct perf_buffer_opts {1624size_t sz;1625__u32 sample_period;1626size_t :0;1627};1628#define perf_buffer_opts__last_field sample_period16291630/**1631* @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified1632* BPF_PERF_EVENT_ARRAY map1633* @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF1634* code to send data over to user-space1635* @param page_cnt number of memory pages allocated for each per-CPU buffer1636* @param sample_cb function called on each received data record1637* @param lost_cb function called when record loss has occurred1638* @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*1639* @return a new instance of struct perf_buffer on success, NULL on error with1640* *errno* containing an error code1641*/1642LIBBPF_API struct perf_buffer *1643perf_buffer__new(int map_fd, size_t page_cnt,1644perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,1645const struct perf_buffer_opts *opts);16461647enum bpf_perf_event_ret {1648LIBBPF_PERF_EVENT_DONE = 0,1649LIBBPF_PERF_EVENT_ERROR = -1,1650LIBBPF_PERF_EVENT_CONT = -2,1651};16521653struct perf_event_header;16541655typedef enum bpf_perf_event_ret1656(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);16571658/* raw perf buffer options, giving most power and control */1659struct perf_buffer_raw_opts {1660size_t sz;1661long :0;1662long :0;1663/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of1664* max_entries of given PERF_EVENT_ARRAY map)1665*/1666int cpu_cnt;1667/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */1668int *cpus;1669/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */1670int *map_keys;1671};1672#define perf_buffer_raw_opts__last_field map_keys16731674struct perf_event_attr;16751676LIBBPF_API struct perf_buffer *1677perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,1678perf_buffer_event_fn event_cb, void *ctx,1679const struct perf_buffer_raw_opts *opts);16801681LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);1682LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);1683LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);1684LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);1685LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);1686LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);1687LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);1688/**1689* @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying1690* memory region of the ring buffer.1691* This ring buffer can be used to implement a custom events consumer.1692* The ring buffer starts with the *struct perf_event_mmap_page*, which1693* holds the ring buffer management fields, when accessing the header1694* structure it's important to be SMP aware.1695* You can refer to *perf_event_read_simple* for a simple example.1696* @param pb the perf buffer structure1697* @param buf_idx the buffer index to retrieve1698* @param buf (out) gets the base pointer of the mmap()'ed memory1699* @param buf_size (out) gets the size of the mmap()'ed region1700* @return 0 on success, negative error code for failure1701*/1702LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,1703size_t *buf_size);17041705struct bpf_prog_linfo;1706struct bpf_prog_info;17071708LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);1709LIBBPF_API struct bpf_prog_linfo *1710bpf_prog_linfo__new(const struct bpf_prog_info *info);1711LIBBPF_API const struct bpf_line_info *1712bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,1713__u64 addr, __u32 func_idx, __u32 nr_skip);1714LIBBPF_API const struct bpf_line_info *1715bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,1716__u32 insn_off, __u32 nr_skip);17171718/*1719* Probe for supported system features1720*1721* Note that running many of these probes in a short amount of time can cause1722* the kernel to reach the maximal size of lockable memory allowed for the1723* user, causing subsequent probes to fail. In this case, the caller may want1724* to adjust that limit with setrlimit().1725*/17261727/**1728* @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports1729* BPF programs of a given type.1730* @param prog_type BPF program type to detect kernel support for1731* @param opts reserved for future extensibility, should be NULL1732* @return 1, if given program type is supported; 0, if given program type is1733* not supported; negative error code if feature detection failed or can't be1734* performed1735*1736* Make sure the process has required set of CAP_* permissions (or runs as1737* root) when performing feature checking.1738*/1739LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);1740/**1741* @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports1742* BPF maps of a given type.1743* @param map_type BPF map type to detect kernel support for1744* @param opts reserved for future extensibility, should be NULL1745* @return 1, if given map type is supported; 0, if given map type is1746* not supported; negative error code if feature detection failed or can't be1747* performed1748*1749* Make sure the process has required set of CAP_* permissions (or runs as1750* root) when performing feature checking.1751*/1752LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);1753/**1754* @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the1755* use of a given BPF helper from specified BPF program type.1756* @param prog_type BPF program type used to check the support of BPF helper1757* @param helper_id BPF helper ID (enum bpf_func_id) to check support for1758* @param opts reserved for future extensibility, should be NULL1759* @return 1, if given combination of program type and helper is supported; 0,1760* if the combination is not supported; negative error code if feature1761* detection for provided input arguments failed or can't be performed1762*1763* Make sure the process has required set of CAP_* permissions (or runs as1764* root) when performing feature checking.1765*/1766LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,1767enum bpf_func_id helper_id, const void *opts);17681769/**1770* @brief **libbpf_num_possible_cpus()** is a helper function to get the1771* number of possible CPUs that the host kernel supports and expects.1772* @return number of possible CPUs; or error code on failure1773*1774* Example usage:1775*1776* int ncpus = libbpf_num_possible_cpus();1777* if (ncpus < 0) {1778* // error handling1779* }1780* long values[ncpus];1781* bpf_map_lookup_elem(per_cpu_map_fd, key, values);1782*/1783LIBBPF_API int libbpf_num_possible_cpus(void);17841785struct bpf_map_skeleton {1786const char *name;1787struct bpf_map **map;1788void **mmaped;1789struct bpf_link **link;1790};17911792struct bpf_prog_skeleton {1793const char *name;1794struct bpf_program **prog;1795struct bpf_link **link;1796};17971798struct bpf_object_skeleton {1799size_t sz; /* size of this struct, for forward/backward compatibility */18001801const char *name;1802const void *data;1803size_t data_sz;18041805struct bpf_object **obj;18061807int map_cnt;1808int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */1809struct bpf_map_skeleton *maps;18101811int prog_cnt;1812int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */1813struct bpf_prog_skeleton *progs;1814};18151816LIBBPF_API int1817bpf_object__open_skeleton(struct bpf_object_skeleton *s,1818const struct bpf_object_open_opts *opts);1819LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);1820LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);1821LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);1822LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);18231824struct bpf_var_skeleton {1825const char *name;1826struct bpf_map **map;1827void **addr;1828};18291830struct bpf_object_subskeleton {1831size_t sz; /* size of this struct, for forward/backward compatibility */18321833const struct bpf_object *obj;18341835int map_cnt;1836int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */1837struct bpf_map_skeleton *maps;18381839int prog_cnt;1840int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */1841struct bpf_prog_skeleton *progs;18421843int var_cnt;1844int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */1845struct bpf_var_skeleton *vars;1846};18471848LIBBPF_API int1849bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);1850LIBBPF_API void1851bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);18521853struct gen_loader_opts {1854size_t sz; /* size of this struct, for forward/backward compatibility */1855const char *data;1856const char *insns;1857__u32 data_sz;1858__u32 insns_sz;1859bool gen_hash;1860};18611862#define gen_loader_opts__last_field gen_hash1863LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,1864struct gen_loader_opts *opts);18651866enum libbpf_tristate {1867TRI_NO = 0,1868TRI_YES = 1,1869TRI_MODULE = 2,1870};18711872struct bpf_linker_opts {1873/* size of this struct, for forward/backward compatibility */1874size_t sz;1875};1876#define bpf_linker_opts__last_field sz18771878struct bpf_linker_file_opts {1879/* size of this struct, for forward/backward compatibility */1880size_t sz;1881};1882#define bpf_linker_file_opts__last_field sz18831884struct bpf_linker;18851886LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);1887LIBBPF_API struct bpf_linker *bpf_linker__new_fd(int fd, struct bpf_linker_opts *opts);1888LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,1889const char *filename,1890const struct bpf_linker_file_opts *opts);1891LIBBPF_API int bpf_linker__add_fd(struct bpf_linker *linker, int fd,1892const struct bpf_linker_file_opts *opts);1893LIBBPF_API int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz,1894const struct bpf_linker_file_opts *opts);1895LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);1896LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);18971898/*1899* Custom handling of BPF program's SEC() definitions1900*/19011902struct bpf_prog_load_opts; /* defined in bpf.h */19031904/* Called during bpf_object__open() for each recognized BPF program. Callback1905* can use various bpf_program__set_*() setters to adjust whatever properties1906* are necessary.1907*/1908typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);19091910/* Called right before libbpf performs bpf_prog_load() to load BPF program1911* into the kernel. Callback can adjust opts as necessary.1912*/1913typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,1914struct bpf_prog_load_opts *opts, long cookie);19151916/* Called during skeleton attach or through bpf_program__attach(). If1917* auto-attach is not supported, callback should return 0 and set link to1918* NULL (it's not considered an error during skeleton attach, but it will be1919* an error for bpf_program__attach() calls). On error, error should be1920* returned directly and link set to NULL. On success, return 0 and set link1921* to a valid struct bpf_link.1922*/1923typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,1924struct bpf_link **link);19251926struct libbpf_prog_handler_opts {1927/* size of this struct, for forward/backward compatibility */1928size_t sz;1929/* User-provided value that is passed to prog_setup_fn,1930* prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to1931* register one set of callbacks for multiple SEC() definitions and1932* still be able to distinguish them, if necessary. For example,1933* libbpf itself is using this to pass necessary flags (e.g.,1934* sleepable flag) to a common internal SEC() handler.1935*/1936long cookie;1937/* BPF program initialization callback (see libbpf_prog_setup_fn_t).1938* Callback is optional, pass NULL if it's not necessary.1939*/1940libbpf_prog_setup_fn_t prog_setup_fn;1941/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).1942* Callback is optional, pass NULL if it's not necessary.1943*/1944libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;1945/* BPF program attach callback (see libbpf_prog_attach_fn_t).1946* Callback is optional, pass NULL if it's not necessary.1947*/1948libbpf_prog_attach_fn_t prog_attach_fn;1949};1950#define libbpf_prog_handler_opts__last_field prog_attach_fn19511952/**1953* @brief **libbpf_register_prog_handler()** registers a custom BPF program1954* SEC() handler.1955* @param sec section prefix for which custom handler is registered1956* @param prog_type BPF program type associated with specified section1957* @param exp_attach_type Expected BPF attach type associated with specified section1958* @param opts optional cookie, callbacks, and other extra options1959* @return Non-negative handler ID is returned on success. This handler ID has1960* to be passed to *libbpf_unregister_prog_handler()* to unregister such1961* custom handler. Negative error code is returned on error.1962*1963* *sec* defines which SEC() definitions are handled by this custom handler1964* registration. *sec* can have few different forms:1965* - if *sec* is just a plain string (e.g., "abc"), it will match only1966* SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result1967* in an error;1968* - if *sec* is of the form "abc/", proper SEC() form is1969* SEC("abc/something"), where acceptable "something" should be checked by1970* *prog_init_fn* callback, if there are additional restrictions;1971* - if *sec* is of the form "abc+", it will successfully match both1972* SEC("abc") and SEC("abc/whatever") forms;1973* - if *sec* is NULL, custom handler is registered for any BPF program that1974* doesn't match any of the registered (custom or libbpf's own) SEC()1975* handlers. There could be only one such generic custom handler registered1976* at any given time.1977*1978* All custom handlers (except the one with *sec* == NULL) are processed1979* before libbpf's own SEC() handlers. It is allowed to "override" libbpf's1980* SEC() handlers by registering custom ones for the same section prefix1981* (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")1982* handler).1983*1984* Note, like much of global libbpf APIs (e.g., libbpf_set_print(),1985* libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs1986* to ensure synchronization if there is a risk of running this API from1987* multiple threads simultaneously.1988*/1989LIBBPF_API int libbpf_register_prog_handler(const char *sec,1990enum bpf_prog_type prog_type,1991enum bpf_attach_type exp_attach_type,1992const struct libbpf_prog_handler_opts *opts);1993/**1994* @brief *libbpf_unregister_prog_handler()* unregisters previously registered1995* custom BPF program SEC() handler.1996* @param handler_id handler ID returned by *libbpf_register_prog_handler()*1997* after successful registration1998* @return 0 on success, negative error code if handler isn't found1999*2000* Note, like much of global libbpf APIs (e.g., libbpf_set_print(),2001* libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs2002* to ensure synchronization if there is a risk of running this API from2003* multiple threads simultaneously.2004*/2005LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);20062007#ifdef __cplusplus2008} /* extern "C" */2009#endif20102011#endif /* __LIBBPF_LIBBPF_H */201220132014