diff options
Diffstat (limited to 'tools/lib/bpf/libbpf.h')
| -rw-r--r-- | tools/lib/bpf/libbpf.h | 297 | 
1 files changed, 271 insertions, 26 deletions
| diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 9de0f299706b..8b9bc5e90c2b 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -24,6 +24,10 @@  extern "C" {  #endif +LIBBPF_API __u32 libbpf_major_version(void); +LIBBPF_API __u32 libbpf_minor_version(void); +LIBBPF_API const char *libbpf_version_string(void); +  enum libbpf_errno {  	__LIBBPF_ERRNO__START = 4000, @@ -104,12 +108,73 @@ struct bpf_object_open_opts {  	 * 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 overriden +	 *     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; + +	size_t :0;  }; -#define bpf_object_open_opts__last_field btf_custom_path +#define bpf_object_open_opts__last_field kernel_log_level  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); @@ -149,6 +214,7 @@ struct bpf_object_load_attr {  /* Load/unload object into/from kernel */  LIBBPF_API int bpf_object__load(struct bpf_object *obj); +LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__load() instead")  LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);  LIBBPF_DEPRECATED_SINCE(0, 6, "bpf_object__unload() is deprecated, use bpf_object__close() instead")  LIBBPF_API int bpf_object__unload(struct bpf_object *obj); @@ -161,6 +227,7 @@ 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_DEPRECATED_SINCE(0, 7, "use bpf_object__find_program_by_name() instead")  LIBBPF_API struct bpf_program *  bpf_object__find_program_by_title(const struct bpf_object *obj,  				  const char *title); @@ -262,8 +329,8 @@ LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *p   */  LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog); -LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license, -				 __u32 kern_version); +LIBBPF_DEPRECATED_SINCE(0, 6, "use bpf_object__load() instead") +LIBBPF_API int bpf_program__load(struct bpf_program *prog, const char *license, __u32 kern_version);  LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);  LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")  LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog, @@ -273,7 +340,31 @@ LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated"  LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,  					   const char *path,  					   int instance); + +/** + * @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 the programs + * 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); @@ -344,10 +435,41 @@ struct bpf_uprobe_opts {  };  #define bpf_uprobe_opts__last_field retprobe +/** + * @brief **bpf_program__attach_uprobe()** attaches a BPF program + * to the userspace function which is found by binary path and + * offset. You can optionally specify a particular proccess to attach + * to. You can also optionally attach the program to the function + * exit instead of entry. + * + * @param prog BPF program to attach + * @param retprobe Attach to function exit + * @param pid Process ID to attach the uprobe to, 0 for self (own process), + * -1 for all processes + * @param binary_path Path to binary that contains the function symbol + * @param func_offset Offset within the binary of the function symbol + * @return Reference to the newly created BPF link; or NULL is returned on error, + * error code is stored in errno + */  LIBBPF_API struct bpf_link *  bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,  			   pid_t pid, const char *binary_path,  			   size_t func_offset); + +/** + * @brief **bpf_program__attach_uprobe_opts()** is just like + * bpf_program__attach_uprobe() except with a options struct + * for various configurations. + * + * @param prog BPF program to attach + * @param pid Process ID to attach the uprobe to, 0 for self (own process), + * -1 for all processes + * @param binary_path Path to binary that contains the function symbol + * @param func_offset Offset within the binary of the function symbol + * @param opts Options for altering program attachment + * @return Reference to the newly created BPF link; or NULL is returned on error, + * error code is stored in errno + */  LIBBPF_API struct bpf_link *  bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,  				const char *binary_path, size_t func_offset, @@ -431,7 +553,6 @@ bpf_program__attach_iter(const struct bpf_program *prog,   * one instance. In this case bpf_program__fd(prog) is equal to   * bpf_program__nth_fd(prog, 0).   */ -LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions")  struct bpf_prog_prep_result {  	/*  	 * If not NULL, load new instruction array. @@ -494,6 +615,18 @@ LIBBPF_API void  bpf_program__set_expected_attach_type(struct bpf_program *prog,  				      enum bpf_attach_type type); +LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog); +LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags); + +/* Per-program log level and log buffer getters/setters. + * See bpf_object_open_opts comments regarding log_level and log_buf + * interactions. + */ +LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog); +LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level); +LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size); +LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size); +  LIBBPF_API int  bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,  			       const char *attach_func_name); @@ -544,7 +677,8 @@ bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);   * Get bpf_map through the offset of corresponding struct bpf_map_def   * in the BPF object file.   */ -LIBBPF_API struct bpf_map * +LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__find_map_by_name() instead") +struct bpf_map *  bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);  LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead") @@ -611,6 +745,7 @@ LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);  LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,  					  const void *data, size_t size);  LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize); +LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_map__type() instead")  LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);  /** @@ -674,10 +809,12 @@ struct bpf_prog_load_attr {  	int prog_flags;  }; +LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__open() and bpf_object__load() instead")  LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,  				   struct bpf_object **pobj, int *prog_fd); -LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type, -			     struct bpf_object **pobj, int *prog_fd); +LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open() and bpf_object__load() instead") +LIBBPF_API int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type, +					struct bpf_object **pobj, int *prog_fd);  /* XDP related API */  struct xdp_link_info { @@ -775,18 +912,52 @@ typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);  /* common use perf buffer options */  struct perf_buffer_opts { -	/* if specified, sample_cb is called for each sample */ -	perf_buffer_sample_fn sample_cb; -	/* if specified, lost_cb is called for each batch of lost samples */ -	perf_buffer_lost_fn lost_cb; -	/* ctx is provided to sample_cb and lost_cb */ -	void *ctx; +	union { +		size_t sz; +		struct { /* DEPRECATED: will be removed in v1.0 */ +			/* if specified, sample_cb is called for each sample */ +			perf_buffer_sample_fn sample_cb; +			/* if specified, lost_cb is called for each batch of lost samples */ +			perf_buffer_lost_fn lost_cb; +			/* ctx is provided to sample_cb and lost_cb */ +			void *ctx; +		}; +	};  }; +#define perf_buffer_opts__last_field sz +/** + * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified + * BPF_PERF_EVENT_ARRAY map + * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF + * code to send data over to user-space + * @param page_cnt number of memory pages allocated for each per-CPU buffer + * @param sample_cb function called on each received data record + * @param lost_cb function called when record loss has occurred + * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb* + * @return a new instance of struct perf_buffer on success, NULL on error with + * *errno* containing an error code + */  LIBBPF_API struct perf_buffer *  perf_buffer__new(int map_fd, size_t page_cnt, +		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,  		 const struct perf_buffer_opts *opts); +LIBBPF_API struct perf_buffer * +perf_buffer__new_v0_6_0(int map_fd, size_t page_cnt, +			perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx, +			const struct perf_buffer_opts *opts); + +LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new() instead") +struct perf_buffer *perf_buffer__new_deprecated(int map_fd, size_t page_cnt, +						const struct perf_buffer_opts *opts); + +#define perf_buffer__new(...) ___libbpf_overload(___perf_buffer_new, __VA_ARGS__) +#define ___perf_buffer_new6(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts) \ +	perf_buffer__new(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts) +#define ___perf_buffer_new3(map_fd, page_cnt, opts) \ +	perf_buffer__new_deprecated(map_fd, page_cnt, opts) +  enum bpf_perf_event_ret {  	LIBBPF_PERF_EVENT_DONE	= 0,  	LIBBPF_PERF_EVENT_ERROR	= -1, @@ -800,12 +971,21 @@ typedef enum bpf_perf_event_ret  /* raw perf buffer options, giving most power and control */  struct perf_buffer_raw_opts { -	/* perf event attrs passed directly into perf_event_open() */ -	struct perf_event_attr *attr; -	/* raw event callback */ -	perf_buffer_event_fn event_cb; -	/* ctx is provided to event_cb */ -	void *ctx; +	union { +		struct { +			size_t sz; +			long :0; +			long :0; +		}; +		struct { /* DEPRECATED: will be removed in v1.0 */ +			/* perf event attrs passed directly into perf_event_open() */ +			struct perf_event_attr *attr; +			/* raw event callback */ +			perf_buffer_event_fn event_cb; +			/* ctx is provided to event_cb */ +			void *ctx; +		}; +	};  	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of  	 * max_entries of given PERF_EVENT_ARRAY map)  	 */ @@ -815,11 +995,28 @@ struct perf_buffer_raw_opts {  	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */  	int *map_keys;  }; +#define perf_buffer_raw_opts__last_field map_keys  LIBBPF_API struct perf_buffer * -perf_buffer__new_raw(int map_fd, size_t page_cnt, +perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr, +		     perf_buffer_event_fn event_cb, void *ctx,  		     const struct perf_buffer_raw_opts *opts); +LIBBPF_API struct perf_buffer * +perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt, struct perf_event_attr *attr, +			    perf_buffer_event_fn event_cb, void *ctx, +			    const struct perf_buffer_raw_opts *opts); + +LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new_raw() instead") +struct perf_buffer *perf_buffer__new_raw_deprecated(int map_fd, size_t page_cnt, +						    const struct perf_buffer_raw_opts *opts); + +#define perf_buffer__new_raw(...) ___libbpf_overload(___perf_buffer_new_raw, __VA_ARGS__) +#define ___perf_buffer_new_raw6(map_fd, page_cnt, attr, event_cb, ctx, opts) \ +	perf_buffer__new_raw(map_fd, page_cnt, attr, event_cb, ctx, opts) +#define ___perf_buffer_new_raw3(map_fd, page_cnt, opts) \ +	perf_buffer__new_raw_deprecated(map_fd, page_cnt, opts) +  LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);  LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);  LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms); @@ -831,6 +1028,7 @@ LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_i  typedef enum bpf_perf_event_ret  	(*bpf_perf_event_print_t)(struct perf_event_header *hdr,  				  void *private_data); +LIBBPF_DEPRECATED_SINCE(0, 8, "use perf_buffer__poll() or  perf_buffer__consume() instead")  LIBBPF_API enum bpf_perf_event_ret  bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,  			   void **copy_mem, size_t *copy_size, @@ -857,13 +1055,57 @@ bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,   * user, causing subsequent probes to fail. In this case, the caller may want   * to adjust that limit with setrlimit().   */ -LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, -				    __u32 ifindex); +LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_prog_type() instead") +LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex); +LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_map_type() instead")  LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex); -LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, -				 enum bpf_prog_type prog_type, __u32 ifindex); +LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_helper() instead") +LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type, __u32 ifindex); +LIBBPF_DEPRECATED_SINCE(0, 8, "implement your own or use bpftool for feature detection")  LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex); +/** + * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports + * BPF programs of a given type. + * @param prog_type BPF program type to detect kernel support for + * @param opts reserved for future extensibility, should be NULL + * @return 1, if given program type is supported; 0, if given program type is + * not supported; negative error code if feature detection failed or can't be + * performed + * + * Make sure the process has required set of CAP_* permissions (or runs as + * root) when performing feature checking. + */ +LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts); +/** + * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports + * BPF maps of a given type. + * @param map_type BPF map type to detect kernel support for + * @param opts reserved for future extensibility, should be NULL + * @return 1, if given map type is supported; 0, if given map type is + * not supported; negative error code if feature detection failed or can't be + * performed + * + * Make sure the process has required set of CAP_* permissions (or runs as + * root) when performing feature checking. + */ +LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts); +/** + * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the + * use of a given BPF helper from specified BPF program type. + * @param prog_type BPF program type used to check the support of BPF helper + * @param helper_id BPF helper ID (enum bpf_func_id) to check support for + * @param opts reserved for future extensibility, should be NULL + * @return 1, if given combination of program type and helper is supported; 0, + * if the combination is not supported; negative error code if feature + * detection for provided input arguments failed or can't be performed + * + * Make sure the process has required set of CAP_* permissions (or runs as + * root) when performing feature checking. + */ +LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, +				       enum bpf_func_id helper_id, const void *opts); +  /*   * Get bpf_prog_info in continuous memory   * @@ -918,12 +1160,15 @@ struct bpf_prog_info_linear {  	__u8			data[];  }; +LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")  LIBBPF_API struct bpf_prog_info_linear *  bpf_program__get_prog_info_linear(int fd, __u64 arrays); +LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")  LIBBPF_API void  bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear); +LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")  LIBBPF_API void  bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear); @@ -965,11 +1210,11 @@ struct bpf_object_skeleton {  	struct bpf_object **obj;  	int map_cnt; -	int map_skel_sz; /* sizeof(struct bpf_skeleton_map) */ +	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */  	struct bpf_map_skeleton *maps;  	int prog_cnt; -	int prog_skel_sz; /* sizeof(struct bpf_skeleton_prog) */ +	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */  	struct bpf_prog_skeleton *progs;  }; |