2085 lines · 79.6 KB · C
RawDownload
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ /* * Common eBPF ELF object loading operations. * * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> * Copyright (C) 2015 Huawei Inc. */#ifndef __LIBBPF_LIBBPF_H#define __LIBBPF_LIBBPF_H #include <stdarg.h>#include <stdio.h>#include <stdint.h>#include <stdbool.h>#include <sys/types.h>  // for size_t#include <linux/bpf.h> #include "libbpf_common.h"#include "libbpf_legacy.h" #ifdef __cplusplusextern "C" {#endif /** * @brief **libbpf_major_version()** provides the major version of libbpf. * @return An integer, the major version number */LIBBPF_API __u32 libbpf_major_version(void); /** * @brief **libbpf_minor_version()** provides the minor version of libbpf. * @return An integer, the minor version number */LIBBPF_API __u32 libbpf_minor_version(void); /** * @brief **libbpf_version_string()** provides the version of libbpf in a * human-readable form, e.g., "v1.7". * @return Pointer to a static string containing the version * * The format is *not* a part of a stable API and may change in the future. */LIBBPF_API const char *libbpf_version_string(void); enum libbpf_errno {	__LIBBPF_ERRNO__START = 4000, 	/* Something wrong in libelf */	LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,	LIBBPF_ERRNO__FORMAT,	/* BPF object format invalid */	LIBBPF_ERRNO__KVERSION,	/* Incorrect or no 'version' section */	LIBBPF_ERRNO__ENDIAN,	/* Endian mismatch */	LIBBPF_ERRNO__INTERNAL,	/* Internal error in libbpf */	LIBBPF_ERRNO__RELOC,	/* Relocation failed */	LIBBPF_ERRNO__LOAD,	/* Load program failure for unknown reason */	LIBBPF_ERRNO__VERIFY,	/* Kernel verifier blocks program loading */	LIBBPF_ERRNO__PROG2BIG,	/* Program too big */	LIBBPF_ERRNO__KVER,	/* Incorrect kernel version */	LIBBPF_ERRNO__PROGTYPE,	/* Kernel doesn't support this program type */	LIBBPF_ERRNO__WRNGPID,	/* Wrong pid in netlink message */	LIBBPF_ERRNO__INVSEQ,	/* Invalid netlink sequence */	LIBBPF_ERRNO__NLPARSE,	/* netlink parsing error */	__LIBBPF_ERRNO__END,}; /** * @brief **libbpf_strerror()** converts the provided error code into a * human-readable string. * @param err The error code to convert * @param buf Pointer to a buffer where the error message will be stored * @param size The number of bytes in the buffer * @return 0, on success; negative error code, otherwise */LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size); /** * @brief **libbpf_bpf_attach_type_str()** converts the provided attach type * value into a textual representation. * @param t The attach type. * @return Pointer to a static string identifying the attach type. NULL is * returned for unknown **bpf_attach_type** values. */LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t); /** * @brief **libbpf_bpf_link_type_str()** converts the provided link type value * into a textual representation. * @param t The link type. * @return Pointer to a static string identifying the link type. NULL is * returned for unknown **bpf_link_type** values. */LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t); /** * @brief **libbpf_bpf_map_type_str()** converts the provided map type value * into a textual representation. * @param t The map type. * @return Pointer to a static string identifying the map type. NULL is * returned for unknown **bpf_map_type** values. */LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t); /** * @brief **libbpf_bpf_prog_type_str()** converts the provided program type * value into a textual representation. * @param t The program type. * @return Pointer to a static string identifying the program type. NULL is * returned for unknown **bpf_prog_type** values. */LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t); enum libbpf_print_level {        LIBBPF_WARN,        LIBBPF_INFO,        LIBBPF_DEBUG,}; typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,				 const char *, va_list ap); /** * @brief **libbpf_set_print()** sets user-provided log callback function to * be used for libbpf warnings and informational messages. If the user callback * is not set, messages are logged to stderr by default. The verbosity of these * messages can be controlled by setting the environment variable * LIBBPF_LOG_LEVEL to either warn, info, or debug. * @param fn The log print function. If NULL, libbpf won't print anything. * @return Pointer to old print function. * * This function is thread-safe. */LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn); /* Hide internal to user */struct bpf_object; struct bpf_object_open_opts {	/* size of this struct, for forward/backward compatibility */	size_t sz;	/* object name override, if provided:	 * - for object open from file, this will override setting object	 *   name from file path's base name;	 * - for object open from memory buffer, this will specify an object	 *   name and will override default "<addr>-<buf-size>" name;	 */	const char *object_name;	/* parse map definitions non-strictly, allowing extra attributes/data */	bool relaxed_maps;	/* maps that set the 'pinning' attribute in their definition will have	 * their pin_path attribute set to a file in this directory, and be	 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".	 */	const char *pin_root_path; 	__u32 :32; /* stub out now removed attach_prog_fd */ 	/* Additional kernel config content that augments and overrides	 * system Kconfig for CONFIG_xxx externs.	 */	const char *kconfig;	/* Path to the custom BTF to be used for BPF CO-RE relocations.	 * This custom BTF completely replaces the use of vmlinux BTF	 * for the purpose of CO-RE relocations.	 * NOTE: any other BPF feature (e.g., fentry/fexit programs,	 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.	 */	const char *btf_custom_path;	/* Pointer to a buffer for storing kernel logs for applicable BPF	 * commands. Valid kernel_log_size has to be specified as well and are	 * passed-through to bpf() syscall. Keep in mind that kernel might	 * fail operation with -ENOSPC error if provided buffer is too small	 * to contain entire log output.	 * See the comment below for kernel_log_level for interaction between	 * log_buf and log_level settings.	 *	 * If specified, this log buffer will be passed for:	 *   - each BPF progral load (BPF_PROG_LOAD) attempt, unless overridden	 *     with bpf_program__set_log() on per-program level, to get	 *     BPF verifier log output.	 *   - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get	 *     BTF sanity checking log.	 *	 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite	 * previous contents, so if you need more fine-grained control, set	 * per-program buffer with bpf_program__set_log_buf() to preserve each	 * individual program's verification log. Keep using kernel_log_buf	 * for BTF verification log, if necessary.	 */	char *kernel_log_buf;	size_t kernel_log_size;	/*	 * Log level can be set independently from log buffer. Log_level=0	 * means that libbpf will attempt loading BTF or program without any	 * logging requested, but will retry with either its own or custom log	 * buffer, if provided, and log_level=1 on any error.	 * And vice versa, setting log_level>0 will request BTF or prog	 * loading with verbose log from the first attempt (and as such also	 * for successfully loaded BTF or program), and the actual log buffer	 * could be either libbpf's own auto-allocated log buffer, if	 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.	 * If user didn't provide custom log buffer, libbpf will emit captured	 * logs through its print callback.	 */	__u32 kernel_log_level;	/* Path to BPF FS mount point to derive BPF token from.	 *	 * Created BPF token will be used for all bpf() syscall operations	 * that accept BPF token (e.g., map creation, BTF and program loads,	 * etc) automatically within instantiated BPF object.	 *	 * If bpf_token_path is not specified, libbpf will consult	 * LIBBPF_BPF_TOKEN_PATH environment variable. If set, it will be	 * taken as a value of bpf_token_path option and will force libbpf to	 * either create BPF token from provided custom BPF FS path, or will	 * disable implicit BPF token creation, if envvar value is an empty	 * string. bpf_token_path overrides LIBBPF_BPF_TOKEN_PATH, if both are	 * set at the same time.	 *	 * Setting bpf_token_path option to empty string disables libbpf's	 * automatic attempt to create BPF token from default BPF FS mount	 * point (/sys/fs/bpf), in case this default behavior is undesirable.	 */	const char *bpf_token_path; 	size_t :0;};#define bpf_object_open_opts__last_field bpf_token_path /** * @brief **bpf_object__open()** creates a bpf_object by opening * the BPF ELF object file pointed to by the passed path and loading it * into memory. * @param path BPF object file path. * @return pointer to the new bpf_object; or NULL is returned on error, * error code is stored in errno */LIBBPF_API struct bpf_object *bpf_object__open(const char *path); /** * @brief **bpf_object__open_file()** creates a bpf_object by opening * the BPF ELF object file pointed to by the passed path and loading it * into memory. * @param path BPF object file path * @param opts options for how to load the bpf object, this parameter is * optional and can be set to NULL * @return pointer to the new bpf_object; or NULL is returned on error, * error code is stored in errno */LIBBPF_API struct bpf_object *bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts); /** * @brief **bpf_object__open_mem()** creates a bpf_object by reading * the BPF objects raw bytes from a memory buffer containing a valid * BPF ELF object file. * @param obj_buf pointer to the buffer containing ELF file bytes * @param obj_buf_sz number of bytes in the buffer * @param opts options for how to load the bpf object * @return pointer to the new bpf_object; or NULL is returned on error, * error code is stored in errno */LIBBPF_API struct bpf_object *bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,		     const struct bpf_object_open_opts *opts); /** * @brief **bpf_object__prepare()** prepares BPF object for loading: * performs ELF processing, relocations, prepares final state of BPF program * instructions (accessible with bpf_program__insns()), creates and * (potentially) pins maps. Leaves BPF object in the state ready for program * loading. * @param obj Pointer to a valid BPF object instance returned by * **bpf_object__open*()** API * @return 0, on success; negative error code, otherwise, error code is * stored in errno */LIBBPF_API int bpf_object__prepare(struct bpf_object *obj); /** * @brief **bpf_object__load()** loads BPF object into kernel. * @param obj Pointer to a valid BPF object instance returned by * **bpf_object__open*()** APIs * @return 0, on success; negative error code, otherwise, error code is * stored in errno */LIBBPF_API int bpf_object__load(struct bpf_object *obj); /** * @brief **bpf_object__close()** closes a BPF object and releases all * resources. * @param obj Pointer to a valid BPF object */LIBBPF_API void bpf_object__close(struct bpf_object *obj); /** * @brief **bpf_object__pin_maps()** pins each map contained within * the BPF object at the passed directory. * @param obj Pointer to a valid BPF object * @param path A directory where maps should be pinned. * @return 0, on success; negative error code, otherwise * * If `path` is NULL `bpf_map__pin` (which is being used on each map) * will use the pin_path attribute of each map. In this case, maps that * don't have a pin_path set will be ignored. */LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path); /** * @brief **bpf_object__unpin_maps()** unpins each map contained within * the BPF object found in the passed directory. * @param obj Pointer to a valid BPF object * @param path A directory where pinned maps should be searched for. * @return 0, on success; negative error code, otherwise * * If `path` is NULL `bpf_map__unpin` (which is being used on each map) * will use the pin_path attribute of each map. In this case, maps that * don't have a pin_path set will be ignored. */LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,				      const char *path);LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,					const char *path);LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,					  const char *path);LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path); LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version); /** * @brief **bpf_object__token_fd** is an accessor for BPF token FD associated * with BPF object. * @param obj Pointer to a valid BPF object * @return BPF token FD or -1, if it wasn't set */LIBBPF_API int bpf_object__token_fd(const struct bpf_object *obj); struct btf;LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj); LIBBPF_API struct bpf_program *bpf_object__find_program_by_name(const struct bpf_object *obj,				 const char *name); LIBBPF_API intlibbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,			 enum bpf_attach_type *expected_attach_type);LIBBPF_API int libbpf_attach_type_by_name(const char *name,					  enum bpf_attach_type *attach_type);LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,					  enum bpf_attach_type attach_type); /* Accessors of bpf_program */struct bpf_program; LIBBPF_API struct bpf_program *bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog); #define bpf_object__for_each_program(pos, obj)			\	for ((pos) = bpf_object__next_program((obj), NULL);	\	     (pos) != NULL;					\	     (pos) = bpf_object__next_program((obj), (pos))) LIBBPF_API struct bpf_program *bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog); LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,					 __u32 ifindex); LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach); struct bpf_insn; /** * @brief **bpf_program__insns()** gives read-only access to BPF program's * underlying BPF instructions. * @param prog BPF program for which to return instructions * @return a pointer to an array of BPF instructions that belong to the * specified BPF program * * Returned pointer is always valid and not NULL. Number of `struct bpf_insn` * pointed to can be fetched using **bpf_program__insn_cnt()** API. * * Keep in mind, libbpf can modify and append/delete BPF program's * instructions as it processes BPF object file and prepares everything for * uploading into the kernel. So depending on the point in BPF object * lifetime, **bpf_program__insns()** can return different sets of * instructions. As an example, during BPF object load phase BPF program * instructions will be CO-RE-relocated, BPF subprograms instructions will be * appended, ldimm64 instructions will have FDs embedded, etc. So instructions * returned before **bpf_object__load()** and after it might be quite * different. */LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog); /** * @brief **bpf_program__set_insns()** can set BPF program's underlying * BPF instructions. * * WARNING: This is a very advanced libbpf API and users need to know * what they are doing. This should be used from prog_prepare_load_fn * callback only. * * @param prog BPF program for which to return instructions * @param new_insns a pointer to an array of BPF instructions * @param new_insn_cnt number of `struct bpf_insn`'s that form * specified BPF program * @return 0, on success; negative error code, otherwise */LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,				      struct bpf_insn *new_insns, size_t new_insn_cnt); /** * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s * that form specified BPF program. * @param prog BPF program for which to return number of BPF instructions * * See **bpf_program__insns()** documentation for notes on how libbpf can * change instructions and their count during different phases of * **bpf_object** lifetime. */LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog); LIBBPF_API int bpf_program__fd(const struct bpf_program *prog); /** * @brief **bpf_program__pin()** pins the BPF program to a file * in the BPF FS specified by a path. This increments the programs * reference count, allowing it to stay loaded after the process * which loaded it has exited. * * @param prog BPF program to pin, must already be loaded * @param path file path in a BPF file system * @return 0, on success; negative error code, otherwise */LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path); /** * @brief **bpf_program__unpin()** unpins the BPF program from a file * in the BPFFS specified by a path. This decrements program's in-kernel * reference count. * * The file pinning the BPF program can also be unlinked by a different * process in which case this function will return an error. * * @param prog BPF program to unpin * @param path file path to the pin in a BPF file system * @return 0, on success; negative error code, otherwise */LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);LIBBPF_API void bpf_program__unload(struct bpf_program *prog); struct bpf_link; LIBBPF_API struct bpf_link *bpf_link__open(const char *path);LIBBPF_API int bpf_link__fd(const struct bpf_link *link);LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);/** * @brief **bpf_link__pin()** pins the BPF link to a file * in the BPF FS specified by a path. This increments the links * reference count, allowing it to stay loaded after the process * which loaded it has exited. * * @param link BPF link to pin, must already be loaded * @param path file path in a BPF file system * @return 0, on success; negative error code, otherwise */ LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path); /** * @brief **bpf_link__unpin()** unpins the BPF link from a file * in the BPFFS. This decrements link's in-kernel reference count. * * The file pinning the BPF link can also be unlinked by a different * process in which case this function will return an error. * * @param link BPF link to unpin * @return 0, on success; negative error code, otherwise */LIBBPF_API int bpf_link__unpin(struct bpf_link *link);LIBBPF_API int bpf_link__update_program(struct bpf_link *link,					struct bpf_program *prog);LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);LIBBPF_API int bpf_link__detach(struct bpf_link *link);LIBBPF_API int bpf_link__destroy(struct bpf_link *link); /** * @brief **bpf_program__attach()** is a generic function for attaching

Lines 1–500 of 2085.