xref: /openbmc/linux/tools/lib/bpf/libbpf.h (revision 604ba230902d23c6e85c7dba9cfcb6a37661cb12)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 /*
4  * Common eBPF ELF object loading operations.
5  *
6  * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7  * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8  * Copyright (C) 2015 Huawei Inc.
9  */
10 #ifndef __LIBBPF_LIBBPF_H
11 #define __LIBBPF_LIBBPF_H
12 
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <sys/types.h>  // for size_t
18 #include <linux/bpf.h>
19 
20 #include "libbpf_common.h"
21 #include "libbpf_legacy.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 LIBBPF_API __u32 libbpf_major_version(void);
28 LIBBPF_API __u32 libbpf_minor_version(void);
29 LIBBPF_API const char *libbpf_version_string(void);
30 
31 enum libbpf_errno {
32 	__LIBBPF_ERRNO__START = 4000,
33 
34 	/* Something wrong in libelf */
35 	LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
36 	LIBBPF_ERRNO__FORMAT,	/* BPF object format invalid */
37 	LIBBPF_ERRNO__KVERSION,	/* Incorrect or no 'version' section */
38 	LIBBPF_ERRNO__ENDIAN,	/* Endian mismatch */
39 	LIBBPF_ERRNO__INTERNAL,	/* Internal error in libbpf */
40 	LIBBPF_ERRNO__RELOC,	/* Relocation failed */
41 	LIBBPF_ERRNO__LOAD,	/* Load program failure for unknown reason */
42 	LIBBPF_ERRNO__VERIFY,	/* Kernel verifier blocks program loading */
43 	LIBBPF_ERRNO__PROG2BIG,	/* Program too big */
44 	LIBBPF_ERRNO__KVER,	/* Incorrect kernel version */
45 	LIBBPF_ERRNO__PROGTYPE,	/* Kernel doesn't support this program type */
46 	LIBBPF_ERRNO__WRNGPID,	/* Wrong pid in netlink message */
47 	LIBBPF_ERRNO__INVSEQ,	/* Invalid netlink sequence */
48 	LIBBPF_ERRNO__NLPARSE,	/* netlink parsing error */
49 	__LIBBPF_ERRNO__END,
50 };
51 
52 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
53 
54 enum libbpf_print_level {
55         LIBBPF_WARN,
56         LIBBPF_INFO,
57         LIBBPF_DEBUG,
58 };
59 
60 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
61 				 const char *, va_list ap);
62 
63 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
64 
65 /* Hide internal to user */
66 struct bpf_object;
67 
68 struct bpf_object_open_attr {
69 	const char *file;
70 	enum bpf_prog_type prog_type;
71 };
72 
73 struct bpf_object_open_opts {
74 	/* size of this struct, for forward/backward compatiblity */
75 	size_t sz;
76 	/* object name override, if provided:
77 	 * - for object open from file, this will override setting object
78 	 *   name from file path's base name;
79 	 * - for object open from memory buffer, this will specify an object
80 	 *   name and will override default "<addr>-<buf-size>" name;
81 	 */
82 	const char *object_name;
83 	/* parse map definitions non-strictly, allowing extra attributes/data */
84 	bool relaxed_maps;
85 	/* DEPRECATED: handle CO-RE relocations non-strictly, allowing failures.
86 	 * Value is ignored. Relocations always are processed non-strictly.
87 	 * Non-relocatable instructions are replaced with invalid ones to
88 	 * prevent accidental errors.
89 	 * */
90 	LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect")
91 	bool relaxed_core_relocs;
92 	/* maps that set the 'pinning' attribute in their definition will have
93 	 * their pin_path attribute set to a file in this directory, and be
94 	 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
95 	 */
96 	const char *pin_root_path;
97 
98 	LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__set_attach_target() on each individual bpf_program")
99 	__u32 attach_prog_fd;
100 	/* Additional kernel config content that augments and overrides
101 	 * system Kconfig for CONFIG_xxx externs.
102 	 */
103 	const char *kconfig;
104 	/* Path to the custom BTF to be used for BPF CO-RE relocations.
105 	 * This custom BTF completely replaces the use of vmlinux BTF
106 	 * for the purpose of CO-RE relocations.
107 	 * NOTE: any other BPF feature (e.g., fentry/fexit programs,
108 	 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
109 	 */
110 	const char *btf_custom_path;
111 	/* Pointer to a buffer for storing kernel logs for applicable BPF
112 	 * commands. Valid kernel_log_size has to be specified as well and are
113 	 * passed-through to bpf() syscall. Keep in mind that kernel might
114 	 * fail operation with -ENOSPC error if provided buffer is too small
115 	 * to contain entire log output.
116 	 * See the comment below for kernel_log_level for interaction between
117 	 * log_buf and log_level settings.
118 	 *
119 	 * If specified, this log buffer will be passed for:
120 	 *   - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden
121 	 *     with bpf_program__set_log() on per-program level, to get
122 	 *     BPF verifier log output.
123 	 *   - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
124 	 *     BTF sanity checking log.
125 	 *
126 	 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
127 	 * previous contents, so if you need more fine-grained control, set
128 	 * per-program buffer with bpf_program__set_log_buf() to preserve each
129 	 * individual program's verification log. Keep using kernel_log_buf
130 	 * for BTF verification log, if necessary.
131 	 */
132 	char *kernel_log_buf;
133 	size_t kernel_log_size;
134 	/*
135 	 * Log level can be set independently from log buffer. Log_level=0
136 	 * means that libbpf will attempt loading BTF or program without any
137 	 * logging requested, but will retry with either its own or custom log
138 	 * buffer, if provided, and log_level=1 on any error.
139 	 * And vice versa, setting log_level>0 will request BTF or prog
140 	 * loading with verbose log from the first attempt (and as such also
141 	 * for successfully loaded BTF or program), and the actual log buffer
142 	 * could be either libbpf's own auto-allocated log buffer, if
143 	 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
144 	 * If user didn't provide custom log buffer, libbpf will emit captured
145 	 * logs through its print callback.
146 	 */
147 	__u32 kernel_log_level;
148 
149 	size_t :0;
150 };
151 #define bpf_object_open_opts__last_field kernel_log_level
152 
153 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
154 
155 /**
156  * @brief **bpf_object__open_file()** creates a bpf_object by opening
157  * the BPF ELF object file pointed to by the passed path and loading it
158  * into memory.
159  * @param path BPF object file path
160  * @param opts options for how to load the bpf object, this parameter is
161  * optional and can be set to NULL
162  * @return pointer to the new bpf_object; or NULL is returned on error,
163  * error code is stored in errno
164  */
165 LIBBPF_API struct bpf_object *
166 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
167 
168 /**
169  * @brief **bpf_object__open_mem()** creates a bpf_object by reading
170  * the BPF objects raw bytes from a memory buffer containing a valid
171  * BPF ELF object file.
172  * @param obj_buf pointer to the buffer containing ELF file bytes
173  * @param obj_buf_sz number of bytes in the buffer
174  * @param opts options for how to load the bpf object
175  * @return pointer to the new bpf_object; or NULL is returned on error,
176  * error code is stored in errno
177  */
178 LIBBPF_API struct bpf_object *
179 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
180 		     const struct bpf_object_open_opts *opts);
181 
182 /* deprecated bpf_object__open variants */
183 LIBBPF_API struct bpf_object *
184 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
185 			const char *name);
186 LIBBPF_API struct bpf_object *
187 bpf_object__open_xattr(struct bpf_object_open_attr *attr);
188 
189 enum libbpf_pin_type {
190 	LIBBPF_PIN_NONE,
191 	/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
192 	LIBBPF_PIN_BY_NAME,
193 };
194 
195 /* pin_maps and unpin_maps can both be called with a NULL path, in which case
196  * they will use the pin_path attribute of each map (and ignore all maps that
197  * don't have a pin_path set).
198  */
199 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
200 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
201 				      const char *path);
202 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
203 					const char *path);
204 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
205 					  const char *path);
206 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
207 LIBBPF_API void bpf_object__close(struct bpf_object *object);
208 
209 struct bpf_object_load_attr {
210 	struct bpf_object *obj;
211 	int log_level;
212 	const char *target_btf_path;
213 };
214 
215 /* Load/unload object into/from kernel */
216 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
217 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__load() instead")
218 LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
219 LIBBPF_DEPRECATED_SINCE(0, 6, "bpf_object__unload() is deprecated, use bpf_object__close() instead")
220 LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
221 
222 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
223 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
224 LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
225 
226 struct btf;
227 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
228 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
229 
230 LIBBPF_API struct bpf_program *
231 bpf_object__find_program_by_title(const struct bpf_object *obj,
232 				  const char *title);
233 LIBBPF_API struct bpf_program *
234 bpf_object__find_program_by_name(const struct bpf_object *obj,
235 				 const char *name);
236 
237 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "track bpf_objects in application code instead")
238 struct bpf_object *bpf_object__next(struct bpf_object *prev);
239 #define bpf_object__for_each_safe(pos, tmp)			\
240 	for ((pos) = bpf_object__next(NULL),		\
241 		(tmp) = bpf_object__next(pos);		\
242 	     (pos) != NULL;				\
243 	     (pos) = (tmp), (tmp) = bpf_object__next(tmp))
244 
245 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
246 LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
247 				    bpf_object_clear_priv_t clear_priv);
248 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
249 
250 LIBBPF_API int
251 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
252 			 enum bpf_attach_type *expected_attach_type);
253 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
254 					  enum bpf_attach_type *attach_type);
255 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
256 					  enum bpf_attach_type attach_type);
257 
258 /* Accessors of bpf_program */
259 struct bpf_program;
260 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_program() instead")
261 struct bpf_program *bpf_program__next(struct bpf_program *prog,
262 				      const struct bpf_object *obj);
263 LIBBPF_API struct bpf_program *
264 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
265 
266 #define bpf_object__for_each_program(pos, obj)			\
267 	for ((pos) = bpf_object__next_program((obj), NULL);	\
268 	     (pos) != NULL;					\
269 	     (pos) = bpf_object__next_program((obj), (pos)))
270 
271 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_program() instead")
272 struct bpf_program *bpf_program__prev(struct bpf_program *prog,
273 				      const struct bpf_object *obj);
274 LIBBPF_API struct bpf_program *
275 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
276 
277 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
278 
279 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
280 				     bpf_program_clear_priv_t clear_priv);
281 
282 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
283 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
284 					 __u32 ifindex);
285 
286 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
287 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
288 LIBBPF_API LIBBPF_DEPRECATED("BPF program title is confusing term; please use bpf_program__section_name() instead")
289 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy);
290 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
291 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
292 
293 /* returns program size in bytes */
294 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insn_cnt() instead")
295 LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
296 
297 struct bpf_insn;
298 
299 /**
300  * @brief **bpf_program__insns()** gives read-only access to BPF program's
301  * underlying BPF instructions.
302  * @param prog BPF program for which to return instructions
303  * @return a pointer to an array of BPF instructions that belong to the
304  * specified BPF program
305  *
306  * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
307  * pointed to can be fetched using **bpf_program__insn_cnt()** API.
308  *
309  * Keep in mind, libbpf can modify and append/delete BPF program's
310  * instructions as it processes BPF object file and prepares everything for
311  * uploading into the kernel. So depending on the point in BPF object
312  * lifetime, **bpf_program__insns()** can return different sets of
313  * instructions. As an example, during BPF object load phase BPF program
314  * instructions will be CO-RE-relocated, BPF subprograms instructions will be
315  * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
316  * returned before **bpf_object__load()** and after it might be quite
317  * different.
318  */
319 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
320 /**
321  * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
322  * that form specified BPF program.
323  * @param prog BPF program for which to return number of BPF instructions
324  *
325  * See **bpf_program__insns()** documentation for notes on how libbpf can
326  * change instructions and their count during different phases of
327  * **bpf_object** lifetime.
328  */
329 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
330 
331 LIBBPF_DEPRECATED_SINCE(0, 6, "use bpf_object__load() instead")
332 LIBBPF_API int bpf_program__load(struct bpf_program *prog, const char *license, __u32 kern_version);
333 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
334 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
335 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
336 					 const char *path,
337 					 int instance);
338 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
339 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
340 					   const char *path,
341 					   int instance);
342 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
343 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
344 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
345 
346 struct bpf_link;
347 
348 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
349 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
350 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
351 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
352 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
353 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
354 					struct bpf_program *prog);
355 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
356 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
357 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
358 
359 LIBBPF_API struct bpf_link *
360 bpf_program__attach(const struct bpf_program *prog);
361 
362 struct bpf_perf_event_opts {
363 	/* size of this struct, for forward/backward compatiblity */
364 	size_t sz;
365 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
366 	__u64 bpf_cookie;
367 };
368 #define bpf_perf_event_opts__last_field bpf_cookie
369 
370 LIBBPF_API struct bpf_link *
371 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
372 
373 LIBBPF_API struct bpf_link *
374 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
375 				    const struct bpf_perf_event_opts *opts);
376 
377 struct bpf_kprobe_opts {
378 	/* size of this struct, for forward/backward compatiblity */
379 	size_t sz;
380 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
381 	__u64 bpf_cookie;
382 	/* function's offset to install kprobe to */
383 	size_t offset;
384 	/* kprobe is return probe */
385 	bool retprobe;
386 	size_t :0;
387 };
388 #define bpf_kprobe_opts__last_field retprobe
389 
390 LIBBPF_API struct bpf_link *
391 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
392 			   const char *func_name);
393 LIBBPF_API struct bpf_link *
394 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
395                                 const char *func_name,
396                                 const struct bpf_kprobe_opts *opts);
397 
398 struct bpf_uprobe_opts {
399 	/* size of this struct, for forward/backward compatiblity */
400 	size_t sz;
401 	/* offset of kernel reference counted USDT semaphore, added in
402 	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
403 	 */
404 	size_t ref_ctr_offset;
405 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
406 	__u64 bpf_cookie;
407 	/* uprobe is return probe, invoked at function return time */
408 	bool retprobe;
409 	size_t :0;
410 };
411 #define bpf_uprobe_opts__last_field retprobe
412 
413 /**
414  * @brief **bpf_program__attach_uprobe()** attaches a BPF program
415  * to the userspace function which is found by binary path and
416  * offset. You can optionally specify a particular proccess to attach
417  * to. You can also optionally attach the program to the function
418  * exit instead of entry.
419  *
420  * @param prog BPF program to attach
421  * @param retprobe Attach to function exit
422  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
423  * -1 for all processes
424  * @param binary_path Path to binary that contains the function symbol
425  * @param func_offset Offset within the binary of the function symbol
426  * @return Reference to the newly created BPF link; or NULL is returned on error,
427  * error code is stored in errno
428  */
429 LIBBPF_API struct bpf_link *
430 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
431 			   pid_t pid, const char *binary_path,
432 			   size_t func_offset);
433 
434 /**
435  * @brief **bpf_program__attach_uprobe_opts()** is just like
436  * bpf_program__attach_uprobe() except with a options struct
437  * for various configurations.
438  *
439  * @param prog BPF program to attach
440  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
441  * -1 for all processes
442  * @param binary_path Path to binary that contains the function symbol
443  * @param func_offset Offset within the binary of the function symbol
444  * @param opts Options for altering program attachment
445  * @return Reference to the newly created BPF link; or NULL is returned on error,
446  * error code is stored in errno
447  */
448 LIBBPF_API struct bpf_link *
449 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
450 				const char *binary_path, size_t func_offset,
451 				const struct bpf_uprobe_opts *opts);
452 
453 struct bpf_tracepoint_opts {
454 	/* size of this struct, for forward/backward compatiblity */
455 	size_t sz;
456 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
457 	__u64 bpf_cookie;
458 };
459 #define bpf_tracepoint_opts__last_field bpf_cookie
460 
461 LIBBPF_API struct bpf_link *
462 bpf_program__attach_tracepoint(const struct bpf_program *prog,
463 			       const char *tp_category,
464 			       const char *tp_name);
465 LIBBPF_API struct bpf_link *
466 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
467 				    const char *tp_category,
468 				    const char *tp_name,
469 				    const struct bpf_tracepoint_opts *opts);
470 
471 LIBBPF_API struct bpf_link *
472 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
473 				   const char *tp_name);
474 LIBBPF_API struct bpf_link *
475 bpf_program__attach_trace(const struct bpf_program *prog);
476 LIBBPF_API struct bpf_link *
477 bpf_program__attach_lsm(const struct bpf_program *prog);
478 LIBBPF_API struct bpf_link *
479 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
480 LIBBPF_API struct bpf_link *
481 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
482 LIBBPF_API struct bpf_link *
483 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
484 LIBBPF_API struct bpf_link *
485 bpf_program__attach_freplace(const struct bpf_program *prog,
486 			     int target_fd, const char *attach_func_name);
487 
488 struct bpf_map;
489 
490 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
491 
492 struct bpf_iter_attach_opts {
493 	size_t sz; /* size of this struct for forward/backward compatibility */
494 	union bpf_iter_link_info *link_info;
495 	__u32 link_info_len;
496 };
497 #define bpf_iter_attach_opts__last_field link_info_len
498 
499 LIBBPF_API struct bpf_link *
500 bpf_program__attach_iter(const struct bpf_program *prog,
501 			 const struct bpf_iter_attach_opts *opts);
502 
503 /*
504  * Libbpf allows callers to adjust BPF programs before being loaded
505  * into kernel. One program in an object file can be transformed into
506  * multiple variants to be attached to different hooks.
507  *
508  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
509  * form an API for this purpose.
510  *
511  * - bpf_program_prep_t:
512  *   Defines a 'preprocessor', which is a caller defined function
513  *   passed to libbpf through bpf_program__set_prep(), and will be
514  *   called before program is loaded. The processor should adjust
515  *   the program one time for each instance according to the instance id
516  *   passed to it.
517  *
518  * - bpf_program__set_prep:
519  *   Attaches a preprocessor to a BPF program. The number of instances
520  *   that should be created is also passed through this function.
521  *
522  * - bpf_program__nth_fd:
523  *   After the program is loaded, get resulting FD of a given instance
524  *   of the BPF program.
525  *
526  * If bpf_program__set_prep() is not used, the program would be loaded
527  * without adjustment during bpf_object__load(). The program has only
528  * one instance. In this case bpf_program__fd(prog) is equal to
529  * bpf_program__nth_fd(prog, 0).
530  */
531 struct bpf_prog_prep_result {
532 	/*
533 	 * If not NULL, load new instruction array.
534 	 * If set to NULL, don't load this instance.
535 	 */
536 	struct bpf_insn *new_insn_ptr;
537 	int new_insn_cnt;
538 
539 	/* If not NULL, result FD is written to it. */
540 	int *pfd;
541 };
542 
543 /*
544  * Parameters of bpf_program_prep_t:
545  *  - prog:	The bpf_program being loaded.
546  *  - n:	Index of instance being generated.
547  *  - insns:	BPF instructions array.
548  *  - insns_cnt:Number of instructions in insns.
549  *  - res:	Output parameter, result of transformation.
550  *
551  * Return value:
552  *  - Zero:	pre-processing success.
553  *  - Non-zero:	pre-processing error, stop loading.
554  */
555 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
556 				  struct bpf_insn *insns, int insns_cnt,
557 				  struct bpf_prog_prep_result *res);
558 
559 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions")
560 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
561 				     bpf_program_prep_t prep);
562 
563 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
564 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
565 
566 /*
567  * Adjust type of BPF program. Default is kprobe.
568  */
569 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
570 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
571 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
572 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
573 LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
574 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
575 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
576 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
577 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
578 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
579 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
580 LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog);
581 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
582 
583 LIBBPF_API enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
584 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
585 				      enum bpf_prog_type type);
586 
587 LIBBPF_API enum bpf_attach_type
588 bpf_program__get_expected_attach_type(const struct bpf_program *prog);
589 LIBBPF_API void
590 bpf_program__set_expected_attach_type(struct bpf_program *prog,
591 				      enum bpf_attach_type type);
592 
593 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
594 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
595 
596 /* Per-program log level and log buffer getters/setters.
597  * See bpf_object_open_opts comments regarding log_level and log_buf
598  * interactions.
599  */
600 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
601 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
602 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
603 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
604 
605 LIBBPF_API int
606 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
607 			       const char *attach_func_name);
608 
609 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
610 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
611 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
612 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
613 LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
614 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
615 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
616 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
617 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
618 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
619 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
620 LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog);
621 LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog);
622 
623 /*
624  * No need for __attribute__((packed)), all members of 'bpf_map_def'
625  * are all aligned.  In addition, using __attribute__((packed))
626  * would trigger a -Wpacked warning message, and lead to an error
627  * if -Werror is set.
628  */
629 struct bpf_map_def {
630 	unsigned int type;
631 	unsigned int key_size;
632 	unsigned int value_size;
633 	unsigned int max_entries;
634 	unsigned int map_flags;
635 };
636 
637 /**
638  * @brief **bpf_object__find_map_by_name()** returns BPF map of
639  * the given name, if it exists within the passed BPF object
640  * @param obj BPF object
641  * @param name name of the BPF map
642  * @return BPF map instance, if such map exists within the BPF object;
643  * or NULL otherwise.
644  */
645 LIBBPF_API struct bpf_map *
646 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
647 
648 LIBBPF_API int
649 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
650 
651 /*
652  * Get bpf_map through the offset of corresponding struct bpf_map_def
653  * in the BPF object file.
654  */
655 LIBBPF_API struct bpf_map *
656 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
657 
658 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead")
659 struct bpf_map *bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
660 LIBBPF_API struct bpf_map *
661 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
662 
663 #define bpf_object__for_each_map(pos, obj)		\
664 	for ((pos) = bpf_object__next_map((obj), NULL);	\
665 	     (pos) != NULL;				\
666 	     (pos) = bpf_object__next_map((obj), (pos)))
667 #define bpf_map__for_each bpf_object__for_each_map
668 
669 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_map() instead")
670 struct bpf_map *bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
671 LIBBPF_API struct bpf_map *
672 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
673 
674 /**
675  * @brief **bpf_map__fd()** gets the file descriptor of the passed
676  * BPF map
677  * @param map the BPF map instance
678  * @return the file descriptor; or -EINVAL in case of an error
679  */
680 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
681 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
682 /* get map definition */
683 LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
684 /* get map name */
685 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
686 /* get/set map type */
687 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
688 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
689 /* get/set map size (max_entries) */
690 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
691 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
692 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
693 /* get/set map flags */
694 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
695 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
696 /* get/set map NUMA node */
697 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
698 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
699 /* get/set map key size */
700 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
701 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
702 /* get/set map value size */
703 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
704 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
705 /* get map key/value BTF type IDs */
706 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
707 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
708 /* get/set map if_index */
709 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
710 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
711 /* get/set map map_extra flags */
712 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
713 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
714 
715 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
716 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
717 				 bpf_map_clear_priv_t clear_priv);
718 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
719 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
720 					  const void *data, size_t size);
721 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
722 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
723 
724 /**
725  * @brief **bpf_map__is_internal()** tells the caller whether or not the
726  * passed map is a special map created by libbpf automatically for things like
727  * global variables, __ksym externs, Kconfig values, etc
728  * @param map the bpf_map
729  * @return true, if the map is an internal map; false, otherwise
730  */
731 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
732 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
733 LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
734 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
735 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
736 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
737 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
738 
739 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
740 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
741 
742 /**
743  * @brief **libbpf_get_error()** extracts the error code from the passed
744  * pointer
745  * @param ptr pointer returned from libbpf API function
746  * @return error code; or 0 if no error occured
747  *
748  * Many libbpf API functions which return pointers have logic to encode error
749  * codes as pointers, and do not return NULL. Meaning **libbpf_get_error()**
750  * should be used on the return value from these functions immediately after
751  * calling the API function, with no intervening calls that could clobber the
752  * `errno` variable. Consult the individual functions documentation to verify
753  * if this logic applies should be used.
754  *
755  * For these API functions, if `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)`
756  * is enabled, NULL is returned on error instead.
757  *
758  * If ptr is NULL, then errno should be already set by the failing
759  * API, because libbpf never returns NULL on success and it now always
760  * sets errno on error.
761  *
762  * Example usage:
763  *
764  *   struct perf_buffer *pb;
765  *
766  *   pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &opts);
767  *   err = libbpf_get_error(pb);
768  *   if (err) {
769  *	  pb = NULL;
770  *	  fprintf(stderr, "failed to open perf buffer: %d\n", err);
771  *	  goto cleanup;
772  *   }
773  */
774 LIBBPF_API long libbpf_get_error(const void *ptr);
775 
776 struct bpf_prog_load_attr {
777 	const char *file;
778 	enum bpf_prog_type prog_type;
779 	enum bpf_attach_type expected_attach_type;
780 	int ifindex;
781 	int log_level;
782 	int prog_flags;
783 };
784 
785 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__open() and bpf_object__load() instead")
786 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
787 				   struct bpf_object **pobj, int *prog_fd);
788 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open() and bpf_object__load() instead")
789 LIBBPF_API int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type,
790 					struct bpf_object **pobj, int *prog_fd);
791 
792 /* XDP related API */
793 struct xdp_link_info {
794 	__u32 prog_id;
795 	__u32 drv_prog_id;
796 	__u32 hw_prog_id;
797 	__u32 skb_prog_id;
798 	__u8 attach_mode;
799 };
800 
801 struct bpf_xdp_set_link_opts {
802 	size_t sz;
803 	int old_fd;
804 	size_t :0;
805 };
806 #define bpf_xdp_set_link_opts__last_field old_fd
807 
808 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
809 LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
810 					const struct bpf_xdp_set_link_opts *opts);
811 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
812 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
813 				     size_t info_size, __u32 flags);
814 
815 /* TC related API */
816 enum bpf_tc_attach_point {
817 	BPF_TC_INGRESS = 1 << 0,
818 	BPF_TC_EGRESS  = 1 << 1,
819 	BPF_TC_CUSTOM  = 1 << 2,
820 };
821 
822 #define BPF_TC_PARENT(a, b) 	\
823 	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
824 
825 enum bpf_tc_flags {
826 	BPF_TC_F_REPLACE = 1 << 0,
827 };
828 
829 struct bpf_tc_hook {
830 	size_t sz;
831 	int ifindex;
832 	enum bpf_tc_attach_point attach_point;
833 	__u32 parent;
834 	size_t :0;
835 };
836 #define bpf_tc_hook__last_field parent
837 
838 struct bpf_tc_opts {
839 	size_t sz;
840 	int prog_fd;
841 	__u32 flags;
842 	__u32 prog_id;
843 	__u32 handle;
844 	__u32 priority;
845 	size_t :0;
846 };
847 #define bpf_tc_opts__last_field priority
848 
849 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
850 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
851 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
852 			     struct bpf_tc_opts *opts);
853 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
854 			     const struct bpf_tc_opts *opts);
855 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
856 			    struct bpf_tc_opts *opts);
857 
858 /* Ring buffer APIs */
859 struct ring_buffer;
860 
861 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
862 
863 struct ring_buffer_opts {
864 	size_t sz; /* size of this struct, for forward/backward compatiblity */
865 };
866 
867 #define ring_buffer_opts__last_field sz
868 
869 LIBBPF_API struct ring_buffer *
870 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
871 		 const struct ring_buffer_opts *opts);
872 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
873 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
874 				ring_buffer_sample_fn sample_cb, void *ctx);
875 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
876 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
877 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
878 
879 /* Perf buffer APIs */
880 struct perf_buffer;
881 
882 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
883 				      void *data, __u32 size);
884 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
885 
886 /* common use perf buffer options */
887 struct perf_buffer_opts {
888 	union {
889 		size_t sz;
890 		struct { /* DEPRECATED: will be removed in v1.0 */
891 			/* if specified, sample_cb is called for each sample */
892 			perf_buffer_sample_fn sample_cb;
893 			/* if specified, lost_cb is called for each batch of lost samples */
894 			perf_buffer_lost_fn lost_cb;
895 			/* ctx is provided to sample_cb and lost_cb */
896 			void *ctx;
897 		};
898 	};
899 };
900 #define perf_buffer_opts__last_field sz
901 
902 /**
903  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
904  * BPF_PERF_EVENT_ARRAY map
905  * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
906  * code to send data over to user-space
907  * @param page_cnt number of memory pages allocated for each per-CPU buffer
908  * @param sample_cb function called on each received data record
909  * @param lost_cb function called when record loss has occurred
910  * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
911  * @return a new instance of struct perf_buffer on success, NULL on error with
912  * *errno* containing an error code
913  */
914 LIBBPF_API struct perf_buffer *
915 perf_buffer__new(int map_fd, size_t page_cnt,
916 		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
917 		 const struct perf_buffer_opts *opts);
918 
919 LIBBPF_API struct perf_buffer *
920 perf_buffer__new_v0_6_0(int map_fd, size_t page_cnt,
921 			perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
922 			const struct perf_buffer_opts *opts);
923 
924 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new() instead")
925 struct perf_buffer *perf_buffer__new_deprecated(int map_fd, size_t page_cnt,
926 						const struct perf_buffer_opts *opts);
927 
928 #define perf_buffer__new(...) ___libbpf_overload(___perf_buffer_new, __VA_ARGS__)
929 #define ___perf_buffer_new6(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts) \
930 	perf_buffer__new(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts)
931 #define ___perf_buffer_new3(map_fd, page_cnt, opts) \
932 	perf_buffer__new_deprecated(map_fd, page_cnt, opts)
933 
934 enum bpf_perf_event_ret {
935 	LIBBPF_PERF_EVENT_DONE	= 0,
936 	LIBBPF_PERF_EVENT_ERROR	= -1,
937 	LIBBPF_PERF_EVENT_CONT	= -2,
938 };
939 
940 struct perf_event_header;
941 
942 typedef enum bpf_perf_event_ret
943 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
944 
945 /* raw perf buffer options, giving most power and control */
946 struct perf_buffer_raw_opts {
947 	union {
948 		struct {
949 			size_t sz;
950 			long :0;
951 			long :0;
952 		};
953 		struct { /* DEPRECATED: will be removed in v1.0 */
954 			/* perf event attrs passed directly into perf_event_open() */
955 			struct perf_event_attr *attr;
956 			/* raw event callback */
957 			perf_buffer_event_fn event_cb;
958 			/* ctx is provided to event_cb */
959 			void *ctx;
960 		};
961 	};
962 	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
963 	 * max_entries of given PERF_EVENT_ARRAY map)
964 	 */
965 	int cpu_cnt;
966 	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
967 	int *cpus;
968 	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
969 	int *map_keys;
970 };
971 #define perf_buffer_raw_opts__last_field map_keys
972 
973 LIBBPF_API struct perf_buffer *
974 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
975 		     perf_buffer_event_fn event_cb, void *ctx,
976 		     const struct perf_buffer_raw_opts *opts);
977 
978 LIBBPF_API struct perf_buffer *
979 perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
980 			    perf_buffer_event_fn event_cb, void *ctx,
981 			    const struct perf_buffer_raw_opts *opts);
982 
983 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new_raw() instead")
984 struct perf_buffer *perf_buffer__new_raw_deprecated(int map_fd, size_t page_cnt,
985 						    const struct perf_buffer_raw_opts *opts);
986 
987 #define perf_buffer__new_raw(...) ___libbpf_overload(___perf_buffer_new_raw, __VA_ARGS__)
988 #define ___perf_buffer_new_raw6(map_fd, page_cnt, attr, event_cb, ctx, opts) \
989 	perf_buffer__new_raw(map_fd, page_cnt, attr, event_cb, ctx, opts)
990 #define ___perf_buffer_new_raw3(map_fd, page_cnt, opts) \
991 	perf_buffer__new_raw_deprecated(map_fd, page_cnt, opts)
992 
993 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
994 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
995 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
996 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
997 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
998 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
999 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1000 
1001 typedef enum bpf_perf_event_ret
1002 	(*bpf_perf_event_print_t)(struct perf_event_header *hdr,
1003 				  void *private_data);
1004 LIBBPF_API enum bpf_perf_event_ret
1005 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
1006 			   void **copy_mem, size_t *copy_size,
1007 			   bpf_perf_event_print_t fn, void *private_data);
1008 
1009 struct bpf_prog_linfo;
1010 struct bpf_prog_info;
1011 
1012 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1013 LIBBPF_API struct bpf_prog_linfo *
1014 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1015 LIBBPF_API const struct bpf_line_info *
1016 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1017 				__u64 addr, __u32 func_idx, __u32 nr_skip);
1018 LIBBPF_API const struct bpf_line_info *
1019 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1020 		      __u32 insn_off, __u32 nr_skip);
1021 
1022 /*
1023  * Probe for supported system features
1024  *
1025  * Note that running many of these probes in a short amount of time can cause
1026  * the kernel to reach the maximal size of lockable memory allowed for the
1027  * user, causing subsequent probes to fail. In this case, the caller may want
1028  * to adjust that limit with setrlimit().
1029  */
1030 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
1031 				    __u32 ifindex);
1032 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
1033 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
1034 				 enum bpf_prog_type prog_type, __u32 ifindex);
1035 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
1036 
1037 /*
1038  * Get bpf_prog_info in continuous memory
1039  *
1040  * struct bpf_prog_info has multiple arrays. The user has option to choose
1041  * arrays to fetch from kernel. The following APIs provide an uniform way to
1042  * fetch these data. All arrays in bpf_prog_info are stored in a single
1043  * continuous memory region. This makes it easy to store the info in a
1044  * file.
1045  *
1046  * Before writing bpf_prog_info_linear to files, it is necessary to
1047  * translate pointers in bpf_prog_info to offsets. Helper functions
1048  * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
1049  * are introduced to switch between pointers and offsets.
1050  *
1051  * Examples:
1052  *   # To fetch map_ids and prog_tags:
1053  *   __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
1054  *           (1UL << BPF_PROG_INFO_PROG_TAGS);
1055  *   struct bpf_prog_info_linear *info_linear =
1056  *           bpf_program__get_prog_info_linear(fd, arrays);
1057  *
1058  *   # To save data in file
1059  *   bpf_program__bpil_addr_to_offs(info_linear);
1060  *   write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
1061  *
1062  *   # To read data from file
1063  *   read(f, info_linear, <proper_size>);
1064  *   bpf_program__bpil_offs_to_addr(info_linear);
1065  */
1066 enum bpf_prog_info_array {
1067 	BPF_PROG_INFO_FIRST_ARRAY = 0,
1068 	BPF_PROG_INFO_JITED_INSNS = 0,
1069 	BPF_PROG_INFO_XLATED_INSNS,
1070 	BPF_PROG_INFO_MAP_IDS,
1071 	BPF_PROG_INFO_JITED_KSYMS,
1072 	BPF_PROG_INFO_JITED_FUNC_LENS,
1073 	BPF_PROG_INFO_FUNC_INFO,
1074 	BPF_PROG_INFO_LINE_INFO,
1075 	BPF_PROG_INFO_JITED_LINE_INFO,
1076 	BPF_PROG_INFO_PROG_TAGS,
1077 	BPF_PROG_INFO_LAST_ARRAY,
1078 };
1079 
1080 struct bpf_prog_info_linear {
1081 	/* size of struct bpf_prog_info, when the tool is compiled */
1082 	__u32			info_len;
1083 	/* total bytes allocated for data, round up to 8 bytes */
1084 	__u32			data_len;
1085 	/* which arrays are included in data */
1086 	__u64			arrays;
1087 	struct bpf_prog_info	info;
1088 	__u8			data[];
1089 };
1090 
1091 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1092 LIBBPF_API struct bpf_prog_info_linear *
1093 bpf_program__get_prog_info_linear(int fd, __u64 arrays);
1094 
1095 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1096 LIBBPF_API void
1097 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
1098 
1099 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1100 LIBBPF_API void
1101 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
1102 
1103 /**
1104  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1105  * number of possible CPUs that the host kernel supports and expects.
1106  * @return number of possible CPUs; or error code on failure
1107  *
1108  * Example usage:
1109  *
1110  *     int ncpus = libbpf_num_possible_cpus();
1111  *     if (ncpus < 0) {
1112  *          // error handling
1113  *     }
1114  *     long values[ncpus];
1115  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1116  */
1117 LIBBPF_API int libbpf_num_possible_cpus(void);
1118 
1119 struct bpf_map_skeleton {
1120 	const char *name;
1121 	struct bpf_map **map;
1122 	void **mmaped;
1123 };
1124 
1125 struct bpf_prog_skeleton {
1126 	const char *name;
1127 	struct bpf_program **prog;
1128 	struct bpf_link **link;
1129 };
1130 
1131 struct bpf_object_skeleton {
1132 	size_t sz; /* size of this struct, for forward/backward compatibility */
1133 
1134 	const char *name;
1135 	const void *data;
1136 	size_t data_sz;
1137 
1138 	struct bpf_object **obj;
1139 
1140 	int map_cnt;
1141 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1142 	struct bpf_map_skeleton *maps;
1143 
1144 	int prog_cnt;
1145 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1146 	struct bpf_prog_skeleton *progs;
1147 };
1148 
1149 LIBBPF_API int
1150 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1151 			  const struct bpf_object_open_opts *opts);
1152 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1153 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1154 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1155 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1156 
1157 struct gen_loader_opts {
1158 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1159 	const char *data;
1160 	const char *insns;
1161 	__u32 data_sz;
1162 	__u32 insns_sz;
1163 };
1164 
1165 #define gen_loader_opts__last_field insns_sz
1166 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1167 				      struct gen_loader_opts *opts);
1168 
1169 enum libbpf_tristate {
1170 	TRI_NO = 0,
1171 	TRI_YES = 1,
1172 	TRI_MODULE = 2,
1173 };
1174 
1175 struct bpf_linker_opts {
1176 	/* size of this struct, for forward/backward compatiblity */
1177 	size_t sz;
1178 };
1179 #define bpf_linker_opts__last_field sz
1180 
1181 struct bpf_linker_file_opts {
1182 	/* size of this struct, for forward/backward compatiblity */
1183 	size_t sz;
1184 };
1185 #define bpf_linker_file_opts__last_field sz
1186 
1187 struct bpf_linker;
1188 
1189 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1190 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1191 				    const char *filename,
1192 				    const struct bpf_linker_file_opts *opts);
1193 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1194 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1195 
1196 #ifdef __cplusplus
1197 } /* extern "C" */
1198 #endif
1199 
1200 #endif /* __LIBBPF_LIBBPF_H */
1201