Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
0002 
0003 /*
0004  * Common eBPF ELF object loading operations.
0005  *
0006  * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
0007  * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
0008  * Copyright (C) 2015 Huawei Inc.
0009  */
0010 #ifndef __LIBBPF_LIBBPF_H
0011 #define __LIBBPF_LIBBPF_H
0012 
0013 #include <stdarg.h>
0014 #include <stdio.h>
0015 #include <stdint.h>
0016 #include <stdbool.h>
0017 #include <sys/types.h>  // for size_t
0018 #include <linux/bpf.h>
0019 
0020 #include "libbpf_common.h"
0021 #include "libbpf_legacy.h"
0022 
0023 #ifdef __cplusplus
0024 extern "C" {
0025 #endif
0026 
0027 LIBBPF_API __u32 libbpf_major_version(void);
0028 LIBBPF_API __u32 libbpf_minor_version(void);
0029 LIBBPF_API const char *libbpf_version_string(void);
0030 
0031 enum libbpf_errno {
0032     __LIBBPF_ERRNO__START = 4000,
0033 
0034     /* Something wrong in libelf */
0035     LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
0036     LIBBPF_ERRNO__FORMAT,   /* BPF object format invalid */
0037     LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
0038     LIBBPF_ERRNO__ENDIAN,   /* Endian mismatch */
0039     LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
0040     LIBBPF_ERRNO__RELOC,    /* Relocation failed */
0041     LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
0042     LIBBPF_ERRNO__VERIFY,   /* Kernel verifier blocks program loading */
0043     LIBBPF_ERRNO__PROG2BIG, /* Program too big */
0044     LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
0045     LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
0046     LIBBPF_ERRNO__WRNGPID,  /* Wrong pid in netlink message */
0047     LIBBPF_ERRNO__INVSEQ,   /* Invalid netlink sequence */
0048     LIBBPF_ERRNO__NLPARSE,  /* netlink parsing error */
0049     __LIBBPF_ERRNO__END,
0050 };
0051 
0052 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
0053 
0054 /**
0055  * @brief **libbpf_bpf_attach_type_str()** converts the provided attach type
0056  * value into a textual representation.
0057  * @param t The attach type.
0058  * @return Pointer to a static string identifying the attach type. NULL is
0059  * returned for unknown **bpf_attach_type** values.
0060  */
0061 LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);
0062 
0063 /**
0064  * @brief **libbpf_bpf_link_type_str()** converts the provided link type value
0065  * into a textual representation.
0066  * @param t The link type.
0067  * @return Pointer to a static string identifying the link type. NULL is
0068  * returned for unknown **bpf_link_type** values.
0069  */
0070 LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t);
0071 
0072 /**
0073  * @brief **libbpf_bpf_map_type_str()** converts the provided map type value
0074  * into a textual representation.
0075  * @param t The map type.
0076  * @return Pointer to a static string identifying the map type. NULL is
0077  * returned for unknown **bpf_map_type** values.
0078  */
0079 LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);
0080 
0081 /**
0082  * @brief **libbpf_bpf_prog_type_str()** converts the provided program type
0083  * value into a textual representation.
0084  * @param t The program type.
0085  * @return Pointer to a static string identifying the program type. NULL is
0086  * returned for unknown **bpf_prog_type** values.
0087  */
0088 LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);
0089 
0090 enum libbpf_print_level {
0091         LIBBPF_WARN,
0092         LIBBPF_INFO,
0093         LIBBPF_DEBUG,
0094 };
0095 
0096 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
0097                  const char *, va_list ap);
0098 
0099 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
0100 
0101 /* Hide internal to user */
0102 struct bpf_object;
0103 
0104 struct bpf_object_open_opts {
0105     /* size of this struct, for forward/backward compatibility */
0106     size_t sz;
0107     /* object name override, if provided:
0108      * - for object open from file, this will override setting object
0109      *   name from file path's base name;
0110      * - for object open from memory buffer, this will specify an object
0111      *   name and will override default "<addr>-<buf-size>" name;
0112      */
0113     const char *object_name;
0114     /* parse map definitions non-strictly, allowing extra attributes/data */
0115     bool relaxed_maps;
0116     /* maps that set the 'pinning' attribute in their definition will have
0117      * their pin_path attribute set to a file in this directory, and be
0118      * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
0119      */
0120     const char *pin_root_path;
0121     long :0;
0122     /* Additional kernel config content that augments and overrides
0123      * system Kconfig for CONFIG_xxx externs.
0124      */
0125     const char *kconfig;
0126     /* Path to the custom BTF to be used for BPF CO-RE relocations.
0127      * This custom BTF completely replaces the use of vmlinux BTF
0128      * for the purpose of CO-RE relocations.
0129      * NOTE: any other BPF feature (e.g., fentry/fexit programs,
0130      * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
0131      */
0132     const char *btf_custom_path;
0133     /* Pointer to a buffer for storing kernel logs for applicable BPF
0134      * commands. Valid kernel_log_size has to be specified as well and are
0135      * passed-through to bpf() syscall. Keep in mind that kernel might
0136      * fail operation with -ENOSPC error if provided buffer is too small
0137      * to contain entire log output.
0138      * See the comment below for kernel_log_level for interaction between
0139      * log_buf and log_level settings.
0140      *
0141      * If specified, this log buffer will be passed for:
0142      *   - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden
0143      *     with bpf_program__set_log() on per-program level, to get
0144      *     BPF verifier log output.
0145      *   - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
0146      *     BTF sanity checking log.
0147      *
0148      * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
0149      * previous contents, so if you need more fine-grained control, set
0150      * per-program buffer with bpf_program__set_log_buf() to preserve each
0151      * individual program's verification log. Keep using kernel_log_buf
0152      * for BTF verification log, if necessary.
0153      */
0154     char *kernel_log_buf;
0155     size_t kernel_log_size;
0156     /*
0157      * Log level can be set independently from log buffer. Log_level=0
0158      * means that libbpf will attempt loading BTF or program without any
0159      * logging requested, but will retry with either its own or custom log
0160      * buffer, if provided, and log_level=1 on any error.
0161      * And vice versa, setting log_level>0 will request BTF or prog
0162      * loading with verbose log from the first attempt (and as such also
0163      * for successfully loaded BTF or program), and the actual log buffer
0164      * could be either libbpf's own auto-allocated log buffer, if
0165      * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
0166      * If user didn't provide custom log buffer, libbpf will emit captured
0167      * logs through its print callback.
0168      */
0169     __u32 kernel_log_level;
0170 
0171     size_t :0;
0172 };
0173 #define bpf_object_open_opts__last_field kernel_log_level
0174 
0175 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
0176 
0177 /**
0178  * @brief **bpf_object__open_file()** creates a bpf_object by opening
0179  * the BPF ELF object file pointed to by the passed path and loading it
0180  * into memory.
0181  * @param path BPF object file path
0182  * @param opts options for how to load the bpf object, this parameter is
0183  * optional and can be set to NULL
0184  * @return pointer to the new bpf_object; or NULL is returned on error,
0185  * error code is stored in errno
0186  */
0187 LIBBPF_API struct bpf_object *
0188 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
0189 
0190 /**
0191  * @brief **bpf_object__open_mem()** creates a bpf_object by reading
0192  * the BPF objects raw bytes from a memory buffer containing a valid
0193  * BPF ELF object file.
0194  * @param obj_buf pointer to the buffer containing ELF file bytes
0195  * @param obj_buf_sz number of bytes in the buffer
0196  * @param opts options for how to load the bpf object
0197  * @return pointer to the new bpf_object; or NULL is returned on error,
0198  * error code is stored in errno
0199  */
0200 LIBBPF_API struct bpf_object *
0201 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
0202              const struct bpf_object_open_opts *opts);
0203 
0204 /* Load/unload object into/from kernel */
0205 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
0206 
0207 LIBBPF_API void bpf_object__close(struct bpf_object *object);
0208 
0209 /* pin_maps and unpin_maps can both be called with a NULL path, in which case
0210  * they will use the pin_path attribute of each map (and ignore all maps that
0211  * don't have a pin_path set).
0212  */
0213 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
0214 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
0215                       const char *path);
0216 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
0217                     const char *path);
0218 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
0219                       const char *path);
0220 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
0221 
0222 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
0223 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
0224 LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
0225 
0226 struct btf;
0227 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
0228 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
0229 
0230 LIBBPF_API struct bpf_program *
0231 bpf_object__find_program_by_name(const struct bpf_object *obj,
0232                  const char *name);
0233 
0234 LIBBPF_API int
0235 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
0236              enum bpf_attach_type *expected_attach_type);
0237 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
0238                       enum bpf_attach_type *attach_type);
0239 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
0240                       enum bpf_attach_type attach_type);
0241 
0242 /* Accessors of bpf_program */
0243 struct bpf_program;
0244 
0245 LIBBPF_API struct bpf_program *
0246 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
0247 
0248 #define bpf_object__for_each_program(pos, obj)          \
0249     for ((pos) = bpf_object__next_program((obj), NULL); \
0250          (pos) != NULL;                 \
0251          (pos) = bpf_object__next_program((obj), (pos)))
0252 
0253 LIBBPF_API struct bpf_program *
0254 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
0255 
0256 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
0257                      __u32 ifindex);
0258 
0259 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
0260 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
0261 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
0262 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
0263 
0264 struct bpf_insn;
0265 
0266 /**
0267  * @brief **bpf_program__insns()** gives read-only access to BPF program's
0268  * underlying BPF instructions.
0269  * @param prog BPF program for which to return instructions
0270  * @return a pointer to an array of BPF instructions that belong to the
0271  * specified BPF program
0272  *
0273  * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
0274  * pointed to can be fetched using **bpf_program__insn_cnt()** API.
0275  *
0276  * Keep in mind, libbpf can modify and append/delete BPF program's
0277  * instructions as it processes BPF object file and prepares everything for
0278  * uploading into the kernel. So depending on the point in BPF object
0279  * lifetime, **bpf_program__insns()** can return different sets of
0280  * instructions. As an example, during BPF object load phase BPF program
0281  * instructions will be CO-RE-relocated, BPF subprograms instructions will be
0282  * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
0283  * returned before **bpf_object__load()** and after it might be quite
0284  * different.
0285  */
0286 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
0287 
0288 /**
0289  * @brief **bpf_program__set_insns()** can set BPF program's underlying
0290  * BPF instructions.
0291  *
0292  * WARNING: This is a very advanced libbpf API and users need to know
0293  * what they are doing. This should be used from prog_prepare_load_fn
0294  * callback only.
0295  *
0296  * @param prog BPF program for which to return instructions
0297  * @param new_insns a pointer to an array of BPF instructions
0298  * @param new_insn_cnt number of `struct bpf_insn`'s that form
0299  * specified BPF program
0300  * @return 0, on success; negative error code, otherwise
0301  */
0302 LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,
0303                       struct bpf_insn *new_insns, size_t new_insn_cnt);
0304 
0305 /**
0306  * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
0307  * that form specified BPF program.
0308  * @param prog BPF program for which to return number of BPF instructions
0309  *
0310  * See **bpf_program__insns()** documentation for notes on how libbpf can
0311  * change instructions and their count during different phases of
0312  * **bpf_object** lifetime.
0313  */
0314 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
0315 
0316 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
0317 
0318 /**
0319  * @brief **bpf_program__pin()** pins the BPF program to a file
0320  * in the BPF FS specified by a path. This increments the programs
0321  * reference count, allowing it to stay loaded after the process
0322  * which loaded it has exited.
0323  *
0324  * @param prog BPF program to pin, must already be loaded
0325  * @param path file path in a BPF file system
0326  * @return 0, on success; negative error code, otherwise
0327  */
0328 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
0329 
0330 /**
0331  * @brief **bpf_program__unpin()** unpins the BPF program from a file
0332  * in the BPFFS specified by a path. This decrements the programs
0333  * reference count.
0334  *
0335  * The file pinning the BPF program can also be unlinked by a different
0336  * process in which case this function will return an error.
0337  *
0338  * @param prog BPF program to unpin
0339  * @param path file path to the pin in a BPF file system
0340  * @return 0, on success; negative error code, otherwise
0341  */
0342 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
0343 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
0344 
0345 struct bpf_link;
0346 
0347 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
0348 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
0349 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
0350 /**
0351  * @brief **bpf_link__pin()** pins the BPF link to a file
0352  * in the BPF FS specified by a path. This increments the links
0353  * reference count, allowing it to stay loaded after the process
0354  * which loaded it has exited.
0355  *
0356  * @param link BPF link to pin, must already be loaded
0357  * @param path file path in a BPF file system
0358  * @return 0, on success; negative error code, otherwise
0359  */
0360 
0361 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
0362 
0363 /**
0364  * @brief **bpf_link__unpin()** unpins the BPF link from a file
0365  * in the BPFFS specified by a path. This decrements the links
0366  * reference count.
0367  *
0368  * The file pinning the BPF link can also be unlinked by a different
0369  * process in which case this function will return an error.
0370  *
0371  * @param prog BPF program to unpin
0372  * @param path file path to the pin in a BPF file system
0373  * @return 0, on success; negative error code, otherwise
0374  */
0375 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
0376 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
0377                     struct bpf_program *prog);
0378 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
0379 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
0380 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
0381 
0382 /**
0383  * @brief **bpf_program__attach()** is a generic function for attaching
0384  * a BPF program based on auto-detection of program type, attach type,
0385  * and extra paremeters, where applicable.
0386  *
0387  * @param prog BPF program to attach
0388  * @return Reference to the newly created BPF link; or NULL is returned on error,
0389  * error code is stored in errno
0390  *
0391  * This is supported for:
0392  *   - kprobe/kretprobe (depends on SEC() definition)
0393  *   - uprobe/uretprobe (depends on SEC() definition)
0394  *   - tracepoint
0395  *   - raw tracepoint
0396  *   - tracing programs (typed raw TP/fentry/fexit/fmod_ret)
0397  */
0398 LIBBPF_API struct bpf_link *
0399 bpf_program__attach(const struct bpf_program *prog);
0400 
0401 struct bpf_perf_event_opts {
0402     /* size of this struct, for forward/backward compatiblity */
0403     size_t sz;
0404     /* custom user-provided value fetchable through bpf_get_attach_cookie() */
0405     __u64 bpf_cookie;
0406 };
0407 #define bpf_perf_event_opts__last_field bpf_cookie
0408 
0409 LIBBPF_API struct bpf_link *
0410 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
0411 
0412 LIBBPF_API struct bpf_link *
0413 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
0414                     const struct bpf_perf_event_opts *opts);
0415 
0416 struct bpf_kprobe_opts {
0417     /* size of this struct, for forward/backward compatiblity */
0418     size_t sz;
0419     /* custom user-provided value fetchable through bpf_get_attach_cookie() */
0420     __u64 bpf_cookie;
0421     /* function's offset to install kprobe to */
0422     size_t offset;
0423     /* kprobe is return probe */
0424     bool retprobe;
0425     size_t :0;
0426 };
0427 #define bpf_kprobe_opts__last_field retprobe
0428 
0429 LIBBPF_API struct bpf_link *
0430 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
0431                const char *func_name);
0432 LIBBPF_API struct bpf_link *
0433 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
0434                                 const char *func_name,
0435                                 const struct bpf_kprobe_opts *opts);
0436 
0437 struct bpf_kprobe_multi_opts {
0438     /* size of this struct, for forward/backward compatibility */
0439     size_t sz;
0440     /* array of function symbols to attach */
0441     const char **syms;
0442     /* array of function addresses to attach */
0443     const unsigned long *addrs;
0444     /* array of user-provided values fetchable through bpf_get_attach_cookie */
0445     const __u64 *cookies;
0446     /* number of elements in syms/addrs/cookies arrays */
0447     size_t cnt;
0448     /* create return kprobes */
0449     bool retprobe;
0450     size_t :0;
0451 };
0452 
0453 #define bpf_kprobe_multi_opts__last_field retprobe
0454 
0455 LIBBPF_API struct bpf_link *
0456 bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
0457                       const char *pattern,
0458                       const struct bpf_kprobe_multi_opts *opts);
0459 
0460 struct bpf_ksyscall_opts {
0461     /* size of this struct, for forward/backward compatiblity */
0462     size_t sz;
0463     /* custom user-provided value fetchable through bpf_get_attach_cookie() */
0464     __u64 bpf_cookie;
0465     /* attach as return probe? */
0466     bool retprobe;
0467     size_t :0;
0468 };
0469 #define bpf_ksyscall_opts__last_field retprobe
0470 
0471 /**
0472  * @brief **bpf_program__attach_ksyscall()** attaches a BPF program
0473  * to kernel syscall handler of a specified syscall. Optionally it's possible
0474  * to request to install retprobe that will be triggered at syscall exit. It's
0475  * also possible to associate BPF cookie (though options).
0476  *
0477  * Libbpf automatically will determine correct full kernel function name,
0478  * which depending on system architecture and kernel version/configuration
0479  * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will
0480  * attach specified program using kprobe/kretprobe mechanism.
0481  *
0482  * **bpf_program__attach_ksyscall()** is an API counterpart of declarative
0483  * **SEC("ksyscall/<syscall>")** annotation of BPF programs.
0484  *
0485  * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do
0486  * not handle all the calling convention quirks for mmap(), clone() and compat
0487  * syscalls. It also only attaches to "native" syscall interfaces. If host
0488  * system supports compat syscalls or defines 32-bit syscalls in 64-bit
0489  * kernel, such syscall interfaces won't be attached to by libbpf.
0490  *
0491  * These limitations may or may not change in the future. Therefore it is
0492  * recommended to use SEC("kprobe") for these syscalls or if working with
0493  * compat and 32-bit interfaces is required.
0494  *
0495  * @param prog BPF program to attach
0496  * @param syscall_name Symbolic name of the syscall (e.g., "bpf")
0497  * @param opts Additional options (see **struct bpf_ksyscall_opts**)
0498  * @return Reference to the newly created BPF link; or NULL is returned on
0499  * error, error code is stored in errno
0500  */
0501 LIBBPF_API struct bpf_link *
0502 bpf_program__attach_ksyscall(const struct bpf_program *prog,
0503                  const char *syscall_name,
0504                  const struct bpf_ksyscall_opts *opts);
0505 
0506 struct bpf_uprobe_opts {
0507     /* size of this struct, for forward/backward compatiblity */
0508     size_t sz;
0509     /* offset of kernel reference counted USDT semaphore, added in
0510      * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
0511      */
0512     size_t ref_ctr_offset;
0513     /* custom user-provided value fetchable through bpf_get_attach_cookie() */
0514     __u64 bpf_cookie;
0515     /* uprobe is return probe, invoked at function return time */
0516     bool retprobe;
0517     /* Function name to attach to.  Could be an unqualified ("abc") or library-qualified
0518      * "abc@LIBXYZ" name.  To specify function entry, func_name should be set while
0519      * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0.  To trace an
0520      * offset within a function, specify func_name and use func_offset argument to specify
0521      * offset within the function.  Shared library functions must specify the shared library
0522      * binary_path.
0523      */
0524     const char *func_name;
0525     size_t :0;
0526 };
0527 #define bpf_uprobe_opts__last_field func_name
0528 
0529 /**
0530  * @brief **bpf_program__attach_uprobe()** attaches a BPF program
0531  * to the userspace function which is found by binary path and
0532  * offset. You can optionally specify a particular proccess to attach
0533  * to. You can also optionally attach the program to the function
0534  * exit instead of entry.
0535  *
0536  * @param prog BPF program to attach
0537  * @param retprobe Attach to function exit
0538  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
0539  * -1 for all processes
0540  * @param binary_path Path to binary that contains the function symbol
0541  * @param func_offset Offset within the binary of the function symbol
0542  * @return Reference to the newly created BPF link; or NULL is returned on error,
0543  * error code is stored in errno
0544  */
0545 LIBBPF_API struct bpf_link *
0546 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
0547                pid_t pid, const char *binary_path,
0548                size_t func_offset);
0549 
0550 /**
0551  * @brief **bpf_program__attach_uprobe_opts()** is just like
0552  * bpf_program__attach_uprobe() except with a options struct
0553  * for various configurations.
0554  *
0555  * @param prog BPF program to attach
0556  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
0557  * -1 for all processes
0558  * @param binary_path Path to binary that contains the function symbol
0559  * @param func_offset Offset within the binary of the function symbol
0560  * @param opts Options for altering program attachment
0561  * @return Reference to the newly created BPF link; or NULL is returned on error,
0562  * error code is stored in errno
0563  */
0564 LIBBPF_API struct bpf_link *
0565 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
0566                 const char *binary_path, size_t func_offset,
0567                 const struct bpf_uprobe_opts *opts);
0568 
0569 struct bpf_usdt_opts {
0570     /* size of this struct, for forward/backward compatibility */
0571     size_t sz;
0572     /* custom user-provided value accessible through usdt_cookie() */
0573     __u64 usdt_cookie;
0574     size_t :0;
0575 };
0576 #define bpf_usdt_opts__last_field usdt_cookie
0577 
0578 /**
0579  * @brief **bpf_program__attach_usdt()** is just like
0580  * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
0581  * Statically Defined Tracepoint) attachment, instead of attaching to
0582  * user-space function entry or exit.
0583  *
0584  * @param prog BPF program to attach
0585  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
0586  * -1 for all processes
0587  * @param binary_path Path to binary that contains provided USDT probe
0588  * @param usdt_provider USDT provider name
0589  * @param usdt_name USDT probe name
0590  * @param opts Options for altering program attachment
0591  * @return Reference to the newly created BPF link; or NULL is returned on error,
0592  * error code is stored in errno
0593  */
0594 LIBBPF_API struct bpf_link *
0595 bpf_program__attach_usdt(const struct bpf_program *prog,
0596              pid_t pid, const char *binary_path,
0597              const char *usdt_provider, const char *usdt_name,
0598              const struct bpf_usdt_opts *opts);
0599 
0600 struct bpf_tracepoint_opts {
0601     /* size of this struct, for forward/backward compatiblity */
0602     size_t sz;
0603     /* custom user-provided value fetchable through bpf_get_attach_cookie() */
0604     __u64 bpf_cookie;
0605 };
0606 #define bpf_tracepoint_opts__last_field bpf_cookie
0607 
0608 LIBBPF_API struct bpf_link *
0609 bpf_program__attach_tracepoint(const struct bpf_program *prog,
0610                    const char *tp_category,
0611                    const char *tp_name);
0612 LIBBPF_API struct bpf_link *
0613 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
0614                     const char *tp_category,
0615                     const char *tp_name,
0616                     const struct bpf_tracepoint_opts *opts);
0617 
0618 LIBBPF_API struct bpf_link *
0619 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
0620                    const char *tp_name);
0621 
0622 struct bpf_trace_opts {
0623     /* size of this struct, for forward/backward compatibility */
0624     size_t sz;
0625     /* custom user-provided value fetchable through bpf_get_attach_cookie() */
0626     __u64 cookie;
0627 };
0628 #define bpf_trace_opts__last_field cookie
0629 
0630 LIBBPF_API struct bpf_link *
0631 bpf_program__attach_trace(const struct bpf_program *prog);
0632 LIBBPF_API struct bpf_link *
0633 bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
0634 
0635 LIBBPF_API struct bpf_link *
0636 bpf_program__attach_lsm(const struct bpf_program *prog);
0637 LIBBPF_API struct bpf_link *
0638 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
0639 LIBBPF_API struct bpf_link *
0640 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
0641 LIBBPF_API struct bpf_link *
0642 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
0643 LIBBPF_API struct bpf_link *
0644 bpf_program__attach_freplace(const struct bpf_program *prog,
0645                  int target_fd, const char *attach_func_name);
0646 
0647 struct bpf_map;
0648 
0649 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
0650 
0651 struct bpf_iter_attach_opts {
0652     size_t sz; /* size of this struct for forward/backward compatibility */
0653     union bpf_iter_link_info *link_info;
0654     __u32 link_info_len;
0655 };
0656 #define bpf_iter_attach_opts__last_field link_info_len
0657 
0658 LIBBPF_API struct bpf_link *
0659 bpf_program__attach_iter(const struct bpf_program *prog,
0660              const struct bpf_iter_attach_opts *opts);
0661 
0662 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
0663 
0664 /**
0665  * @brief **bpf_program__set_type()** sets the program
0666  * type of the passed BPF program.
0667  * @param prog BPF program to set the program type for
0668  * @param type program type to set the BPF map to have
0669  * @return error code; or 0 if no error. An error occurs
0670  * if the object is already loaded.
0671  *
0672  * This must be called before the BPF object is loaded,
0673  * otherwise it has no effect and an error is returned.
0674  */
0675 LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
0676                      enum bpf_prog_type type);
0677 
0678 LIBBPF_API enum bpf_attach_type
0679 bpf_program__expected_attach_type(const struct bpf_program *prog);
0680 
0681 /**
0682  * @brief **bpf_program__set_expected_attach_type()** sets the
0683  * attach type of the passed BPF program. This is used for
0684  * auto-detection of attachment when programs are loaded.
0685  * @param prog BPF program to set the attach type for
0686  * @param type attach type to set the BPF map to have
0687  * @return error code; or 0 if no error. An error occurs
0688  * if the object is already loaded.
0689  *
0690  * This must be called before the BPF object is loaded,
0691  * otherwise it has no effect and an error is returned.
0692  */
0693 LIBBPF_API int
0694 bpf_program__set_expected_attach_type(struct bpf_program *prog,
0695                       enum bpf_attach_type type);
0696 
0697 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
0698 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
0699 
0700 /* Per-program log level and log buffer getters/setters.
0701  * See bpf_object_open_opts comments regarding log_level and log_buf
0702  * interactions.
0703  */
0704 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
0705 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
0706 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
0707 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
0708 
0709 /**
0710  * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
0711  * for supported BPF program types:
0712  *   - BTF-aware raw tracepoints (tp_btf);
0713  *   - fentry/fexit/fmod_ret;
0714  *   - lsm;
0715  *   - freplace.
0716  * @param prog BPF program to set the attach type for
0717  * @param type attach type to set the BPF map to have
0718  * @return error code; or 0 if no error occurred.
0719  */
0720 LIBBPF_API int
0721 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
0722                    const char *attach_func_name);
0723 
0724 /**
0725  * @brief **bpf_object__find_map_by_name()** returns BPF map of
0726  * the given name, if it exists within the passed BPF object
0727  * @param obj BPF object
0728  * @param name name of the BPF map
0729  * @return BPF map instance, if such map exists within the BPF object;
0730  * or NULL otherwise.
0731  */
0732 LIBBPF_API struct bpf_map *
0733 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
0734 
0735 LIBBPF_API int
0736 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
0737 
0738 LIBBPF_API struct bpf_map *
0739 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
0740 
0741 #define bpf_object__for_each_map(pos, obj)      \
0742     for ((pos) = bpf_object__next_map((obj), NULL); \
0743          (pos) != NULL;             \
0744          (pos) = bpf_object__next_map((obj), (pos)))
0745 #define bpf_map__for_each bpf_object__for_each_map
0746 
0747 LIBBPF_API struct bpf_map *
0748 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
0749 
0750 /**
0751  * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
0752  * BPF map during BPF object load phase.
0753  * @param map the BPF map instance
0754  * @param autocreate whether to create BPF map during BPF object load
0755  * @return 0 on success; -EBUSY if BPF object was already loaded
0756  *
0757  * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
0758  * BPF map. By default, libbpf will attempt to create every single BPF map
0759  * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
0760  * and fill in map FD in BPF instructions.
0761  *
0762  * This API allows to opt-out of this process for specific map instance. This
0763  * can be useful if host kernel doesn't support such BPF map type or used
0764  * combination of flags and user application wants to avoid creating such
0765  * a map in the first place. User is still responsible to make sure that their
0766  * BPF-side code that expects to use such missing BPF map is recognized by BPF
0767  * verifier as dead code, otherwise BPF verifier will reject such BPF program.
0768  */
0769 LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
0770 LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
0771 
0772 /**
0773  * @brief **bpf_map__fd()** gets the file descriptor of the passed
0774  * BPF map
0775  * @param map the BPF map instance
0776  * @return the file descriptor; or -EINVAL in case of an error
0777  */
0778 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
0779 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
0780 /* get map name */
0781 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
0782 /* get/set map type */
0783 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
0784 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
0785 /* get/set map size (max_entries) */
0786 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
0787 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
0788 /* get/set map flags */
0789 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
0790 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
0791 /* get/set map NUMA node */
0792 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
0793 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
0794 /* get/set map key size */
0795 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
0796 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
0797 /* get/set map value size */
0798 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
0799 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
0800 /* get map key/value BTF type IDs */
0801 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
0802 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
0803 /* get/set map if_index */
0804 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
0805 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
0806 /* get/set map map_extra flags */
0807 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
0808 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
0809 
0810 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
0811                       const void *data, size_t size);
0812 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
0813 
0814 /**
0815  * @brief **bpf_map__is_internal()** tells the caller whether or not the
0816  * passed map is a special map created by libbpf automatically for things like
0817  * global variables, __ksym externs, Kconfig values, etc
0818  * @param map the bpf_map
0819  * @return true, if the map is an internal map; false, otherwise
0820  */
0821 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
0822 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
0823 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
0824 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
0825 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
0826 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
0827 
0828 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
0829 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
0830 
0831 /**
0832  * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
0833  * corresponding to provided key.
0834  * @param map BPF map to lookup element in
0835  * @param key pointer to memory containing bytes of the key used for lookup
0836  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
0837  * @param value pointer to memory in which looked up value will be stored
0838  * @param value_sz size in byte of value data memory; it has to match BPF map
0839  * definition's **value_size**. For per-CPU BPF maps value size has to be
0840  * a product of BPF map value size and number of possible CPUs in the system
0841  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
0842  * per-CPU values value size has to be aligned up to closest 8 bytes for
0843  * alignment reasons, so expected size is: `round_up(value_size, 8)
0844  * * libbpf_num_possible_cpus()`.
0845  * @flags extra flags passed to kernel for this operation
0846  * @return 0, on success; negative error, otherwise
0847  *
0848  * **bpf_map__lookup_elem()** is high-level equivalent of
0849  * **bpf_map_lookup_elem()** API with added check for key and value size.
0850  */
0851 LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
0852                     const void *key, size_t key_sz,
0853                     void *value, size_t value_sz, __u64 flags);
0854 
0855 /**
0856  * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
0857  * map that corresponds to provided key.
0858  * @param map BPF map to insert to or update element in
0859  * @param key pointer to memory containing bytes of the key
0860  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
0861  * @param value pointer to memory containing bytes of the value
0862  * @param value_sz size in byte of value data memory; it has to match BPF map
0863  * definition's **value_size**. For per-CPU BPF maps value size has to be
0864  * a product of BPF map value size and number of possible CPUs in the system
0865  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
0866  * per-CPU values value size has to be aligned up to closest 8 bytes for
0867  * alignment reasons, so expected size is: `round_up(value_size, 8)
0868  * * libbpf_num_possible_cpus()`.
0869  * @flags extra flags passed to kernel for this operation
0870  * @return 0, on success; negative error, otherwise
0871  *
0872  * **bpf_map__update_elem()** is high-level equivalent of
0873  * **bpf_map_update_elem()** API with added check for key and value size.
0874  */
0875 LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
0876                     const void *key, size_t key_sz,
0877                     const void *value, size_t value_sz, __u64 flags);
0878 
0879 /**
0880  * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
0881  * corresponds to provided key.
0882  * @param map BPF map to delete element from
0883  * @param key pointer to memory containing bytes of the key
0884  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
0885  * @flags extra flags passed to kernel for this operation
0886  * @return 0, on success; negative error, otherwise
0887  *
0888  * **bpf_map__delete_elem()** is high-level equivalent of
0889  * **bpf_map_delete_elem()** API with added check for key size.
0890  */
0891 LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
0892                     const void *key, size_t key_sz, __u64 flags);
0893 
0894 /**
0895  * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
0896  * corresponding to provided key and atomically delete it afterwards.
0897  * @param map BPF map to lookup element in
0898  * @param key pointer to memory containing bytes of the key used for lookup
0899  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
0900  * @param value pointer to memory in which looked up value will be stored
0901  * @param value_sz size in byte of value data memory; it has to match BPF map
0902  * definition's **value_size**. For per-CPU BPF maps value size has to be
0903  * a product of BPF map value size and number of possible CPUs in the system
0904  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
0905  * per-CPU values value size has to be aligned up to closest 8 bytes for
0906  * alignment reasons, so expected size is: `round_up(value_size, 8)
0907  * * libbpf_num_possible_cpus()`.
0908  * @flags extra flags passed to kernel for this operation
0909  * @return 0, on success; negative error, otherwise
0910  *
0911  * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
0912  * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
0913  */
0914 LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
0915                            const void *key, size_t key_sz,
0916                            void *value, size_t value_sz, __u64 flags);
0917 
0918 /**
0919  * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
0920  * fetching next key that follows current key.
0921  * @param map BPF map to fetch next key from
0922  * @param cur_key pointer to memory containing bytes of current key or NULL to
0923  * fetch the first key
0924  * @param next_key pointer to memory to write next key into
0925  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
0926  * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
0927  * negative error, otherwise
0928  *
0929  * **bpf_map__get_next_key()** is high-level equivalent of
0930  * **bpf_map_get_next_key()** API with added check for key size.
0931  */
0932 LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
0933                      const void *cur_key, void *next_key, size_t key_sz);
0934 
0935 struct bpf_xdp_set_link_opts {
0936     size_t sz;
0937     int old_fd;
0938     size_t :0;
0939 };
0940 #define bpf_xdp_set_link_opts__last_field old_fd
0941 
0942 struct bpf_xdp_attach_opts {
0943     size_t sz;
0944     int old_prog_fd;
0945     size_t :0;
0946 };
0947 #define bpf_xdp_attach_opts__last_field old_prog_fd
0948 
0949 struct bpf_xdp_query_opts {
0950     size_t sz;
0951     __u32 prog_id;      /* output */
0952     __u32 drv_prog_id;  /* output */
0953     __u32 hw_prog_id;   /* output */
0954     __u32 skb_prog_id;  /* output */
0955     __u8 attach_mode;   /* output */
0956     size_t :0;
0957 };
0958 #define bpf_xdp_query_opts__last_field attach_mode
0959 
0960 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
0961                   const struct bpf_xdp_attach_opts *opts);
0962 LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
0963                   const struct bpf_xdp_attach_opts *opts);
0964 LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
0965 LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
0966 
0967 /* TC related API */
0968 enum bpf_tc_attach_point {
0969     BPF_TC_INGRESS = 1 << 0,
0970     BPF_TC_EGRESS  = 1 << 1,
0971     BPF_TC_CUSTOM  = 1 << 2,
0972 };
0973 
0974 #define BPF_TC_PARENT(a, b)     \
0975     ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
0976 
0977 enum bpf_tc_flags {
0978     BPF_TC_F_REPLACE = 1 << 0,
0979 };
0980 
0981 struct bpf_tc_hook {
0982     size_t sz;
0983     int ifindex;
0984     enum bpf_tc_attach_point attach_point;
0985     __u32 parent;
0986     size_t :0;
0987 };
0988 #define bpf_tc_hook__last_field parent
0989 
0990 struct bpf_tc_opts {
0991     size_t sz;
0992     int prog_fd;
0993     __u32 flags;
0994     __u32 prog_id;
0995     __u32 handle;
0996     __u32 priority;
0997     size_t :0;
0998 };
0999 #define bpf_tc_opts__last_field priority
1000 
1001 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
1002 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
1003 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
1004                  struct bpf_tc_opts *opts);
1005 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
1006                  const struct bpf_tc_opts *opts);
1007 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
1008                 struct bpf_tc_opts *opts);
1009 
1010 /* Ring buffer APIs */
1011 struct ring_buffer;
1012 
1013 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
1014 
1015 struct ring_buffer_opts {
1016     size_t sz; /* size of this struct, for forward/backward compatiblity */
1017 };
1018 
1019 #define ring_buffer_opts__last_field sz
1020 
1021 LIBBPF_API struct ring_buffer *
1022 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
1023          const struct ring_buffer_opts *opts);
1024 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
1025 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
1026                 ring_buffer_sample_fn sample_cb, void *ctx);
1027 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
1028 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
1029 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
1030 
1031 /* Perf buffer APIs */
1032 struct perf_buffer;
1033 
1034 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
1035                       void *data, __u32 size);
1036 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
1037 
1038 /* common use perf buffer options */
1039 struct perf_buffer_opts {
1040     size_t sz;
1041 };
1042 #define perf_buffer_opts__last_field sz
1043 
1044 /**
1045  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1046  * BPF_PERF_EVENT_ARRAY map
1047  * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1048  * code to send data over to user-space
1049  * @param page_cnt number of memory pages allocated for each per-CPU buffer
1050  * @param sample_cb function called on each received data record
1051  * @param lost_cb function called when record loss has occurred
1052  * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1053  * @return a new instance of struct perf_buffer on success, NULL on error with
1054  * *errno* containing an error code
1055  */
1056 LIBBPF_API struct perf_buffer *
1057 perf_buffer__new(int map_fd, size_t page_cnt,
1058          perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1059          const struct perf_buffer_opts *opts);
1060 
1061 enum bpf_perf_event_ret {
1062     LIBBPF_PERF_EVENT_DONE  = 0,
1063     LIBBPF_PERF_EVENT_ERROR = -1,
1064     LIBBPF_PERF_EVENT_CONT  = -2,
1065 };
1066 
1067 struct perf_event_header;
1068 
1069 typedef enum bpf_perf_event_ret
1070 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1071 
1072 /* raw perf buffer options, giving most power and control */
1073 struct perf_buffer_raw_opts {
1074     size_t sz;
1075     long :0;
1076     long :0;
1077     /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1078      * max_entries of given PERF_EVENT_ARRAY map)
1079      */
1080     int cpu_cnt;
1081     /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1082     int *cpus;
1083     /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1084     int *map_keys;
1085 };
1086 #define perf_buffer_raw_opts__last_field map_keys
1087 
1088 struct perf_event_attr;
1089 
1090 LIBBPF_API struct perf_buffer *
1091 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1092              perf_buffer_event_fn event_cb, void *ctx,
1093              const struct perf_buffer_raw_opts *opts);
1094 
1095 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1096 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1097 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1098 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1099 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1100 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1101 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1102 /**
1103  * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying
1104  * memory region of the ring buffer.
1105  * This ring buffer can be used to implement a custom events consumer.
1106  * The ring buffer starts with the *struct perf_event_mmap_page*, which
1107  * holds the ring buffer managment fields, when accessing the header
1108  * structure it's important to be SMP aware.
1109  * You can refer to *perf_event_read_simple* for a simple example.
1110  * @param pb the perf buffer structure
1111  * @param buf_idx the buffer index to retreive
1112  * @param buf (out) gets the base pointer of the mmap()'ed memory
1113  * @param buf_size (out) gets the size of the mmap()'ed region
1114  * @return 0 on success, negative error code for failure
1115  */
1116 LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,
1117                    size_t *buf_size);
1118 
1119 struct bpf_prog_linfo;
1120 struct bpf_prog_info;
1121 
1122 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1123 LIBBPF_API struct bpf_prog_linfo *
1124 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1125 LIBBPF_API const struct bpf_line_info *
1126 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1127                 __u64 addr, __u32 func_idx, __u32 nr_skip);
1128 LIBBPF_API const struct bpf_line_info *
1129 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1130               __u32 insn_off, __u32 nr_skip);
1131 
1132 /*
1133  * Probe for supported system features
1134  *
1135  * Note that running many of these probes in a short amount of time can cause
1136  * the kernel to reach the maximal size of lockable memory allowed for the
1137  * user, causing subsequent probes to fail. In this case, the caller may want
1138  * to adjust that limit with setrlimit().
1139  */
1140 
1141 /**
1142  * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1143  * BPF programs of a given type.
1144  * @param prog_type BPF program type to detect kernel support for
1145  * @param opts reserved for future extensibility, should be NULL
1146  * @return 1, if given program type is supported; 0, if given program type is
1147  * not supported; negative error code if feature detection failed or can't be
1148  * performed
1149  *
1150  * Make sure the process has required set of CAP_* permissions (or runs as
1151  * root) when performing feature checking.
1152  */
1153 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1154 /**
1155  * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1156  * BPF maps of a given type.
1157  * @param map_type BPF map type to detect kernel support for
1158  * @param opts reserved for future extensibility, should be NULL
1159  * @return 1, if given map type is supported; 0, if given map type is
1160  * not supported; negative error code if feature detection failed or can't be
1161  * performed
1162  *
1163  * Make sure the process has required set of CAP_* permissions (or runs as
1164  * root) when performing feature checking.
1165  */
1166 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1167 /**
1168  * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1169  * use of a given BPF helper from specified BPF program type.
1170  * @param prog_type BPF program type used to check the support of BPF helper
1171  * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1172  * @param opts reserved for future extensibility, should be NULL
1173  * @return 1, if given combination of program type and helper is supported; 0,
1174  * if the combination is not supported; negative error code if feature
1175  * detection for provided input arguments failed or can't be performed
1176  *
1177  * Make sure the process has required set of CAP_* permissions (or runs as
1178  * root) when performing feature checking.
1179  */
1180 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1181                        enum bpf_func_id helper_id, const void *opts);
1182 
1183 /**
1184  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1185  * number of possible CPUs that the host kernel supports and expects.
1186  * @return number of possible CPUs; or error code on failure
1187  *
1188  * Example usage:
1189  *
1190  *     int ncpus = libbpf_num_possible_cpus();
1191  *     if (ncpus < 0) {
1192  *          // error handling
1193  *     }
1194  *     long values[ncpus];
1195  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1196  */
1197 LIBBPF_API int libbpf_num_possible_cpus(void);
1198 
1199 struct bpf_map_skeleton {
1200     const char *name;
1201     struct bpf_map **map;
1202     void **mmaped;
1203 };
1204 
1205 struct bpf_prog_skeleton {
1206     const char *name;
1207     struct bpf_program **prog;
1208     struct bpf_link **link;
1209 };
1210 
1211 struct bpf_object_skeleton {
1212     size_t sz; /* size of this struct, for forward/backward compatibility */
1213 
1214     const char *name;
1215     const void *data;
1216     size_t data_sz;
1217 
1218     struct bpf_object **obj;
1219 
1220     int map_cnt;
1221     int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1222     struct bpf_map_skeleton *maps;
1223 
1224     int prog_cnt;
1225     int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1226     struct bpf_prog_skeleton *progs;
1227 };
1228 
1229 LIBBPF_API int
1230 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1231               const struct bpf_object_open_opts *opts);
1232 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1233 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1234 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1235 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1236 
1237 struct bpf_var_skeleton {
1238     const char *name;
1239     struct bpf_map **map;
1240     void **addr;
1241 };
1242 
1243 struct bpf_object_subskeleton {
1244     size_t sz; /* size of this struct, for forward/backward compatibility */
1245 
1246     const struct bpf_object *obj;
1247 
1248     int map_cnt;
1249     int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1250     struct bpf_map_skeleton *maps;
1251 
1252     int prog_cnt;
1253     int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1254     struct bpf_prog_skeleton *progs;
1255 
1256     int var_cnt;
1257     int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1258     struct bpf_var_skeleton *vars;
1259 };
1260 
1261 LIBBPF_API int
1262 bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1263 LIBBPF_API void
1264 bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1265 
1266 struct gen_loader_opts {
1267     size_t sz; /* size of this struct, for forward/backward compatiblity */
1268     const char *data;
1269     const char *insns;
1270     __u32 data_sz;
1271     __u32 insns_sz;
1272 };
1273 
1274 #define gen_loader_opts__last_field insns_sz
1275 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1276                       struct gen_loader_opts *opts);
1277 
1278 enum libbpf_tristate {
1279     TRI_NO = 0,
1280     TRI_YES = 1,
1281     TRI_MODULE = 2,
1282 };
1283 
1284 struct bpf_linker_opts {
1285     /* size of this struct, for forward/backward compatiblity */
1286     size_t sz;
1287 };
1288 #define bpf_linker_opts__last_field sz
1289 
1290 struct bpf_linker_file_opts {
1291     /* size of this struct, for forward/backward compatiblity */
1292     size_t sz;
1293 };
1294 #define bpf_linker_file_opts__last_field sz
1295 
1296 struct bpf_linker;
1297 
1298 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1299 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1300                     const char *filename,
1301                     const struct bpf_linker_file_opts *opts);
1302 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1303 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1304 
1305 /*
1306  * Custom handling of BPF program's SEC() definitions
1307  */
1308 
1309 struct bpf_prog_load_opts; /* defined in bpf.h */
1310 
1311 /* Called during bpf_object__open() for each recognized BPF program. Callback
1312  * can use various bpf_program__set_*() setters to adjust whatever properties
1313  * are necessary.
1314  */
1315 typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1316 
1317 /* Called right before libbpf performs bpf_prog_load() to load BPF program
1318  * into the kernel. Callback can adjust opts as necessary.
1319  */
1320 typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1321                          struct bpf_prog_load_opts *opts, long cookie);
1322 
1323 /* Called during skeleton attach or through bpf_program__attach(). If
1324  * auto-attach is not supported, callback should return 0 and set link to
1325  * NULL (it's not considered an error during skeleton attach, but it will be
1326  * an error for bpf_program__attach() calls). On error, error should be
1327  * returned directly and link set to NULL. On success, return 0 and set link
1328  * to a valid struct bpf_link.
1329  */
1330 typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1331                        struct bpf_link **link);
1332 
1333 struct libbpf_prog_handler_opts {
1334     /* size of this struct, for forward/backward compatiblity */
1335     size_t sz;
1336     /* User-provided value that is passed to prog_setup_fn,
1337      * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1338      * register one set of callbacks for multiple SEC() definitions and
1339      * still be able to distinguish them, if necessary. For example,
1340      * libbpf itself is using this to pass necessary flags (e.g.,
1341      * sleepable flag) to a common internal SEC() handler.
1342      */
1343     long cookie;
1344     /* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1345      * Callback is optional, pass NULL if it's not necessary.
1346      */
1347     libbpf_prog_setup_fn_t prog_setup_fn;
1348     /* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1349      * Callback is optional, pass NULL if it's not necessary.
1350      */
1351     libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1352     /* BPF program attach callback (see libbpf_prog_attach_fn_t).
1353      * Callback is optional, pass NULL if it's not necessary.
1354      */
1355     libbpf_prog_attach_fn_t prog_attach_fn;
1356 };
1357 #define libbpf_prog_handler_opts__last_field prog_attach_fn
1358 
1359 /**
1360  * @brief **libbpf_register_prog_handler()** registers a custom BPF program
1361  * SEC() handler.
1362  * @param sec section prefix for which custom handler is registered
1363  * @param prog_type BPF program type associated with specified section
1364  * @param exp_attach_type Expected BPF attach type associated with specified section
1365  * @param opts optional cookie, callbacks, and other extra options
1366  * @return Non-negative handler ID is returned on success. This handler ID has
1367  * to be passed to *libbpf_unregister_prog_handler()* to unregister such
1368  * custom handler. Negative error code is returned on error.
1369  *
1370  * *sec* defines which SEC() definitions are handled by this custom handler
1371  * registration. *sec* can have few different forms:
1372  *   - if *sec* is just a plain string (e.g., "abc"), it will match only
1373  *   SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1374  *   in an error;
1375  *   - if *sec* is of the form "abc/", proper SEC() form is
1376  *   SEC("abc/something"), where acceptable "something" should be checked by
1377  *   *prog_init_fn* callback, if there are additional restrictions;
1378  *   - if *sec* is of the form "abc+", it will successfully match both
1379  *   SEC("abc") and SEC("abc/whatever") forms;
1380  *   - if *sec* is NULL, custom handler is registered for any BPF program that
1381  *   doesn't match any of the registered (custom or libbpf's own) SEC()
1382  *   handlers. There could be only one such generic custom handler registered
1383  *   at any given time.
1384  *
1385  * All custom handlers (except the one with *sec* == NULL) are processed
1386  * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1387  * SEC() handlers by registering custom ones for the same section prefix
1388  * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1389  * handler).
1390  *
1391  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1392  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1393  * to ensure synchronization if there is a risk of running this API from
1394  * multiple threads simultaneously.
1395  */
1396 LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1397                         enum bpf_prog_type prog_type,
1398                         enum bpf_attach_type exp_attach_type,
1399                         const struct libbpf_prog_handler_opts *opts);
1400 /**
1401  * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1402  * custom BPF program SEC() handler.
1403  * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1404  * after successful registration
1405  * @return 0 on success, negative error code if handler isn't found
1406  *
1407  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1408  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1409  * to ensure synchronization if there is a risk of running this API from
1410  * multiple threads simultaneously.
1411  */
1412 LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1413 
1414 #ifdef __cplusplus
1415 } /* extern "C" */
1416 #endif
1417 
1418 #endif /* __LIBBPF_LIBBPF_H */