xref: /linux/tools/lib/bpf/libbpf.h (revision b48b89f9c189d24eb5e2b4a0ac067da5a24ee86d)
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 /**
55  * @brief **libbpf_bpf_attach_type_str()** converts the provided attach type
56  * value into a textual representation.
57  * @param t The attach type.
58  * @return Pointer to a static string identifying the attach type. NULL is
59  * returned for unknown **bpf_attach_type** values.
60  */
61 LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);
62 
63 /**
64  * @brief **libbpf_bpf_link_type_str()** converts the provided link type value
65  * into a textual representation.
66  * @param t The link type.
67  * @return Pointer to a static string identifying the link type. NULL is
68  * returned for unknown **bpf_link_type** values.
69  */
70 LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t);
71 
72 /**
73  * @brief **libbpf_bpf_map_type_str()** converts the provided map type value
74  * into a textual representation.
75  * @param t The map type.
76  * @return Pointer to a static string identifying the map type. NULL is
77  * returned for unknown **bpf_map_type** values.
78  */
79 LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);
80 
81 /**
82  * @brief **libbpf_bpf_prog_type_str()** converts the provided program type
83  * value into a textual representation.
84  * @param t The program type.
85  * @return Pointer to a static string identifying the program type. NULL is
86  * returned for unknown **bpf_prog_type** values.
87  */
88 LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);
89 
90 enum libbpf_print_level {
91         LIBBPF_WARN,
92         LIBBPF_INFO,
93         LIBBPF_DEBUG,
94 };
95 
96 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
97 				 const char *, va_list ap);
98 
99 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
100 
101 /* Hide internal to user */
102 struct bpf_object;
103 
104 struct bpf_object_open_opts {
105 	/* size of this struct, for forward/backward compatibility */
106 	size_t sz;
107 	/* object name override, if provided:
108 	 * - for object open from file, this will override setting object
109 	 *   name from file path's base name;
110 	 * - for object open from memory buffer, this will specify an object
111 	 *   name and will override default "<addr>-<buf-size>" name;
112 	 */
113 	const char *object_name;
114 	/* parse map definitions non-strictly, allowing extra attributes/data */
115 	bool relaxed_maps;
116 	/* maps that set the 'pinning' attribute in their definition will have
117 	 * their pin_path attribute set to a file in this directory, and be
118 	 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
119 	 */
120 	const char *pin_root_path;
121 	long :0;
122 	/* Additional kernel config content that augments and overrides
123 	 * system Kconfig for CONFIG_xxx externs.
124 	 */
125 	const char *kconfig;
126 	/* Path to the custom BTF to be used for BPF CO-RE relocations.
127 	 * This custom BTF completely replaces the use of vmlinux BTF
128 	 * for the purpose of CO-RE relocations.
129 	 * NOTE: any other BPF feature (e.g., fentry/fexit programs,
130 	 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
131 	 */
132 	const char *btf_custom_path;
133 	/* Pointer to a buffer for storing kernel logs for applicable BPF
134 	 * commands. Valid kernel_log_size has to be specified as well and are
135 	 * passed-through to bpf() syscall. Keep in mind that kernel might
136 	 * fail operation with -ENOSPC error if provided buffer is too small
137 	 * to contain entire log output.
138 	 * See the comment below for kernel_log_level for interaction between
139 	 * log_buf and log_level settings.
140 	 *
141 	 * If specified, this log buffer will be passed for:
142 	 *   - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden
143 	 *     with bpf_program__set_log() on per-program level, to get
144 	 *     BPF verifier log output.
145 	 *   - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
146 	 *     BTF sanity checking log.
147 	 *
148 	 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
149 	 * previous contents, so if you need more fine-grained control, set
150 	 * per-program buffer with bpf_program__set_log_buf() to preserve each
151 	 * individual program's verification log. Keep using kernel_log_buf
152 	 * for BTF verification log, if necessary.
153 	 */
154 	char *kernel_log_buf;
155 	size_t kernel_log_size;
156 	/*
157 	 * Log level can be set independently from log buffer. Log_level=0
158 	 * means that libbpf will attempt loading BTF or program without any
159 	 * logging requested, but will retry with either its own or custom log
160 	 * buffer, if provided, and log_level=1 on any error.
161 	 * And vice versa, setting log_level>0 will request BTF or prog
162 	 * loading with verbose log from the first attempt (and as such also
163 	 * for successfully loaded BTF or program), and the actual log buffer
164 	 * could be either libbpf's own auto-allocated log buffer, if
165 	 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
166 	 * If user didn't provide custom log buffer, libbpf will emit captured
167 	 * logs through its print callback.
168 	 */
169 	__u32 kernel_log_level;
170 
171 	size_t :0;
172 };
173 #define bpf_object_open_opts__last_field kernel_log_level
174 
175 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
176 
177 /**
178  * @brief **bpf_object__open_file()** creates a bpf_object by opening
179  * the BPF ELF object file pointed to by the passed path and loading it
180  * into memory.
181  * @param path BPF object file path
182  * @param opts options for how to load the bpf object, this parameter is
183  * optional and can be set to NULL
184  * @return pointer to the new bpf_object; or NULL is returned on error,
185  * error code is stored in errno
186  */
187 LIBBPF_API struct bpf_object *
188 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
189 
190 /**
191  * @brief **bpf_object__open_mem()** creates a bpf_object by reading
192  * the BPF objects raw bytes from a memory buffer containing a valid
193  * BPF ELF object file.
194  * @param obj_buf pointer to the buffer containing ELF file bytes
195  * @param obj_buf_sz number of bytes in the buffer
196  * @param opts options for how to load the bpf object
197  * @return pointer to the new bpf_object; or NULL is returned on error,
198  * error code is stored in errno
199  */
200 LIBBPF_API struct bpf_object *
201 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
202 		     const struct bpf_object_open_opts *opts);
203 
204 /* Load/unload object into/from kernel */
205 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
206 
207 LIBBPF_API void bpf_object__close(struct bpf_object *object);
208 
209 /* pin_maps and unpin_maps can both be called with a NULL path, in which case
210  * they will use the pin_path attribute of each map (and ignore all maps that
211  * don't have a pin_path set).
212  */
213 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
214 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
215 				      const char *path);
216 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
217 					const char *path);
218 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
219 					  const char *path);
220 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
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_name(const struct bpf_object *obj,
232 				 const char *name);
233 
234 LIBBPF_API int
235 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
236 			 enum bpf_attach_type *expected_attach_type);
237 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
238 					  enum bpf_attach_type *attach_type);
239 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
240 					  enum bpf_attach_type attach_type);
241 
242 /* Accessors of bpf_program */
243 struct bpf_program;
244 
245 LIBBPF_API struct bpf_program *
246 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
247 
248 #define bpf_object__for_each_program(pos, obj)			\
249 	for ((pos) = bpf_object__next_program((obj), NULL);	\
250 	     (pos) != NULL;					\
251 	     (pos) = bpf_object__next_program((obj), (pos)))
252 
253 LIBBPF_API struct bpf_program *
254 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
255 
256 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
257 					 __u32 ifindex);
258 
259 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
260 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
261 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
262 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
263 LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);
264 LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);
265 
266 struct bpf_insn;
267 
268 /**
269  * @brief **bpf_program__insns()** gives read-only access to BPF program's
270  * underlying BPF instructions.
271  * @param prog BPF program for which to return instructions
272  * @return a pointer to an array of BPF instructions that belong to the
273  * specified BPF program
274  *
275  * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
276  * pointed to can be fetched using **bpf_program__insn_cnt()** API.
277  *
278  * Keep in mind, libbpf can modify and append/delete BPF program's
279  * instructions as it processes BPF object file and prepares everything for
280  * uploading into the kernel. So depending on the point in BPF object
281  * lifetime, **bpf_program__insns()** can return different sets of
282  * instructions. As an example, during BPF object load phase BPF program
283  * instructions will be CO-RE-relocated, BPF subprograms instructions will be
284  * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
285  * returned before **bpf_object__load()** and after it might be quite
286  * different.
287  */
288 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
289 
290 /**
291  * @brief **bpf_program__set_insns()** can set BPF program's underlying
292  * BPF instructions.
293  *
294  * WARNING: This is a very advanced libbpf API and users need to know
295  * what they are doing. This should be used from prog_prepare_load_fn
296  * callback only.
297  *
298  * @param prog BPF program for which to return instructions
299  * @param new_insns a pointer to an array of BPF instructions
300  * @param new_insn_cnt number of `struct bpf_insn`'s that form
301  * specified BPF program
302  * @return 0, on success; negative error code, otherwise
303  */
304 LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,
305 				      struct bpf_insn *new_insns, size_t new_insn_cnt);
306 
307 /**
308  * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
309  * that form specified BPF program.
310  * @param prog BPF program for which to return number of BPF instructions
311  *
312  * See **bpf_program__insns()** documentation for notes on how libbpf can
313  * change instructions and their count during different phases of
314  * **bpf_object** lifetime.
315  */
316 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
317 
318 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
319 
320 /**
321  * @brief **bpf_program__pin()** pins the BPF program to a file
322  * in the BPF FS specified by a path. This increments the programs
323  * reference count, allowing it to stay loaded after the process
324  * which loaded it has exited.
325  *
326  * @param prog BPF program to pin, must already be loaded
327  * @param path file path in a BPF file system
328  * @return 0, on success; negative error code, otherwise
329  */
330 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
331 
332 /**
333  * @brief **bpf_program__unpin()** unpins the BPF program from a file
334  * in the BPFFS specified by a path. This decrements the programs
335  * reference count.
336  *
337  * The file pinning the BPF program can also be unlinked by a different
338  * process in which case this function will return an error.
339  *
340  * @param prog BPF program to unpin
341  * @param path file path to the pin in a BPF file system
342  * @return 0, on success; negative error code, otherwise
343  */
344 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
345 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
346 
347 struct bpf_link;
348 
349 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
350 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
351 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
352 /**
353  * @brief **bpf_link__pin()** pins the BPF link to a file
354  * in the BPF FS specified by a path. This increments the links
355  * reference count, allowing it to stay loaded after the process
356  * which loaded it has exited.
357  *
358  * @param link BPF link to pin, must already be loaded
359  * @param path file path in a BPF file system
360  * @return 0, on success; negative error code, otherwise
361  */
362 
363 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
364 
365 /**
366  * @brief **bpf_link__unpin()** unpins the BPF link from a file
367  * in the BPFFS specified by a path. This decrements the links
368  * reference count.
369  *
370  * The file pinning the BPF link can also be unlinked by a different
371  * process in which case this function will return an error.
372  *
373  * @param prog BPF program to unpin
374  * @param path file path to the pin in a BPF file system
375  * @return 0, on success; negative error code, otherwise
376  */
377 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
378 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
379 					struct bpf_program *prog);
380 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
381 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
382 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
383 
384 /**
385  * @brief **bpf_program__attach()** is a generic function for attaching
386  * a BPF program based on auto-detection of program type, attach type,
387  * and extra paremeters, where applicable.
388  *
389  * @param prog BPF program to attach
390  * @return Reference to the newly created BPF link; or NULL is returned on error,
391  * error code is stored in errno
392  *
393  * This is supported for:
394  *   - kprobe/kretprobe (depends on SEC() definition)
395  *   - uprobe/uretprobe (depends on SEC() definition)
396  *   - tracepoint
397  *   - raw tracepoint
398  *   - tracing programs (typed raw TP/fentry/fexit/fmod_ret)
399  */
400 LIBBPF_API struct bpf_link *
401 bpf_program__attach(const struct bpf_program *prog);
402 
403 struct bpf_perf_event_opts {
404 	/* size of this struct, for forward/backward compatiblity */
405 	size_t sz;
406 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
407 	__u64 bpf_cookie;
408 };
409 #define bpf_perf_event_opts__last_field bpf_cookie
410 
411 LIBBPF_API struct bpf_link *
412 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
413 
414 LIBBPF_API struct bpf_link *
415 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
416 				    const struct bpf_perf_event_opts *opts);
417 
418 struct bpf_kprobe_opts {
419 	/* size of this struct, for forward/backward compatiblity */
420 	size_t sz;
421 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
422 	__u64 bpf_cookie;
423 	/* function's offset to install kprobe to */
424 	size_t offset;
425 	/* kprobe is return probe */
426 	bool retprobe;
427 	size_t :0;
428 };
429 #define bpf_kprobe_opts__last_field retprobe
430 
431 LIBBPF_API struct bpf_link *
432 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
433 			   const char *func_name);
434 LIBBPF_API struct bpf_link *
435 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
436                                 const char *func_name,
437                                 const struct bpf_kprobe_opts *opts);
438 
439 struct bpf_kprobe_multi_opts {
440 	/* size of this struct, for forward/backward compatibility */
441 	size_t sz;
442 	/* array of function symbols to attach */
443 	const char **syms;
444 	/* array of function addresses to attach */
445 	const unsigned long *addrs;
446 	/* array of user-provided values fetchable through bpf_get_attach_cookie */
447 	const __u64 *cookies;
448 	/* number of elements in syms/addrs/cookies arrays */
449 	size_t cnt;
450 	/* create return kprobes */
451 	bool retprobe;
452 	size_t :0;
453 };
454 
455 #define bpf_kprobe_multi_opts__last_field retprobe
456 
457 LIBBPF_API struct bpf_link *
458 bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
459 				      const char *pattern,
460 				      const struct bpf_kprobe_multi_opts *opts);
461 
462 struct bpf_ksyscall_opts {
463 	/* size of this struct, for forward/backward compatiblity */
464 	size_t sz;
465 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
466 	__u64 bpf_cookie;
467 	/* attach as return probe? */
468 	bool retprobe;
469 	size_t :0;
470 };
471 #define bpf_ksyscall_opts__last_field retprobe
472 
473 /**
474  * @brief **bpf_program__attach_ksyscall()** attaches a BPF program
475  * to kernel syscall handler of a specified syscall. Optionally it's possible
476  * to request to install retprobe that will be triggered at syscall exit. It's
477  * also possible to associate BPF cookie (though options).
478  *
479  * Libbpf automatically will determine correct full kernel function name,
480  * which depending on system architecture and kernel version/configuration
481  * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will
482  * attach specified program using kprobe/kretprobe mechanism.
483  *
484  * **bpf_program__attach_ksyscall()** is an API counterpart of declarative
485  * **SEC("ksyscall/<syscall>")** annotation of BPF programs.
486  *
487  * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do
488  * not handle all the calling convention quirks for mmap(), clone() and compat
489  * syscalls. It also only attaches to "native" syscall interfaces. If host
490  * system supports compat syscalls or defines 32-bit syscalls in 64-bit
491  * kernel, such syscall interfaces won't be attached to by libbpf.
492  *
493  * These limitations may or may not change in the future. Therefore it is
494  * recommended to use SEC("kprobe") for these syscalls or if working with
495  * compat and 32-bit interfaces is required.
496  *
497  * @param prog BPF program to attach
498  * @param syscall_name Symbolic name of the syscall (e.g., "bpf")
499  * @param opts Additional options (see **struct bpf_ksyscall_opts**)
500  * @return Reference to the newly created BPF link; or NULL is returned on
501  * error, error code is stored in errno
502  */
503 LIBBPF_API struct bpf_link *
504 bpf_program__attach_ksyscall(const struct bpf_program *prog,
505 			     const char *syscall_name,
506 			     const struct bpf_ksyscall_opts *opts);
507 
508 struct bpf_uprobe_opts {
509 	/* size of this struct, for forward/backward compatiblity */
510 	size_t sz;
511 	/* offset of kernel reference counted USDT semaphore, added in
512 	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
513 	 */
514 	size_t ref_ctr_offset;
515 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
516 	__u64 bpf_cookie;
517 	/* uprobe is return probe, invoked at function return time */
518 	bool retprobe;
519 	/* Function name to attach to.  Could be an unqualified ("abc") or library-qualified
520 	 * "abc@LIBXYZ" name.  To specify function entry, func_name should be set while
521 	 * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0.  To trace an
522 	 * offset within a function, specify func_name and use func_offset argument to specify
523 	 * offset within the function.  Shared library functions must specify the shared library
524 	 * binary_path.
525 	 */
526 	const char *func_name;
527 	size_t :0;
528 };
529 #define bpf_uprobe_opts__last_field func_name
530 
531 /**
532  * @brief **bpf_program__attach_uprobe()** attaches a BPF program
533  * to the userspace function which is found by binary path and
534  * offset. You can optionally specify a particular proccess to attach
535  * to. You can also optionally attach the program to the function
536  * exit instead of entry.
537  *
538  * @param prog BPF program to attach
539  * @param retprobe Attach to function exit
540  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
541  * -1 for all processes
542  * @param binary_path Path to binary that contains the function symbol
543  * @param func_offset Offset within the binary of the function symbol
544  * @return Reference to the newly created BPF link; or NULL is returned on error,
545  * error code is stored in errno
546  */
547 LIBBPF_API struct bpf_link *
548 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
549 			   pid_t pid, const char *binary_path,
550 			   size_t func_offset);
551 
552 /**
553  * @brief **bpf_program__attach_uprobe_opts()** is just like
554  * bpf_program__attach_uprobe() except with a options struct
555  * for various configurations.
556  *
557  * @param prog BPF program to attach
558  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
559  * -1 for all processes
560  * @param binary_path Path to binary that contains the function symbol
561  * @param func_offset Offset within the binary of the function symbol
562  * @param opts Options for altering program attachment
563  * @return Reference to the newly created BPF link; or NULL is returned on error,
564  * error code is stored in errno
565  */
566 LIBBPF_API struct bpf_link *
567 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
568 				const char *binary_path, size_t func_offset,
569 				const struct bpf_uprobe_opts *opts);
570 
571 struct bpf_usdt_opts {
572 	/* size of this struct, for forward/backward compatibility */
573 	size_t sz;
574 	/* custom user-provided value accessible through usdt_cookie() */
575 	__u64 usdt_cookie;
576 	size_t :0;
577 };
578 #define bpf_usdt_opts__last_field usdt_cookie
579 
580 /**
581  * @brief **bpf_program__attach_usdt()** is just like
582  * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
583  * Statically Defined Tracepoint) attachment, instead of attaching to
584  * user-space function entry or exit.
585  *
586  * @param prog BPF program to attach
587  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
588  * -1 for all processes
589  * @param binary_path Path to binary that contains provided USDT probe
590  * @param usdt_provider USDT provider name
591  * @param usdt_name USDT probe name
592  * @param opts Options for altering program attachment
593  * @return Reference to the newly created BPF link; or NULL is returned on error,
594  * error code is stored in errno
595  */
596 LIBBPF_API struct bpf_link *
597 bpf_program__attach_usdt(const struct bpf_program *prog,
598 			 pid_t pid, const char *binary_path,
599 			 const char *usdt_provider, const char *usdt_name,
600 			 const struct bpf_usdt_opts *opts);
601 
602 struct bpf_tracepoint_opts {
603 	/* size of this struct, for forward/backward compatiblity */
604 	size_t sz;
605 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
606 	__u64 bpf_cookie;
607 };
608 #define bpf_tracepoint_opts__last_field bpf_cookie
609 
610 LIBBPF_API struct bpf_link *
611 bpf_program__attach_tracepoint(const struct bpf_program *prog,
612 			       const char *tp_category,
613 			       const char *tp_name);
614 LIBBPF_API struct bpf_link *
615 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
616 				    const char *tp_category,
617 				    const char *tp_name,
618 				    const struct bpf_tracepoint_opts *opts);
619 
620 LIBBPF_API struct bpf_link *
621 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
622 				   const char *tp_name);
623 
624 struct bpf_trace_opts {
625 	/* size of this struct, for forward/backward compatibility */
626 	size_t sz;
627 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
628 	__u64 cookie;
629 };
630 #define bpf_trace_opts__last_field cookie
631 
632 LIBBPF_API struct bpf_link *
633 bpf_program__attach_trace(const struct bpf_program *prog);
634 LIBBPF_API struct bpf_link *
635 bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
636 
637 LIBBPF_API struct bpf_link *
638 bpf_program__attach_lsm(const struct bpf_program *prog);
639 LIBBPF_API struct bpf_link *
640 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
641 LIBBPF_API struct bpf_link *
642 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
643 LIBBPF_API struct bpf_link *
644 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
645 LIBBPF_API struct bpf_link *
646 bpf_program__attach_freplace(const struct bpf_program *prog,
647 			     int target_fd, const char *attach_func_name);
648 
649 struct bpf_map;
650 
651 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
652 
653 struct bpf_iter_attach_opts {
654 	size_t sz; /* size of this struct for forward/backward compatibility */
655 	union bpf_iter_link_info *link_info;
656 	__u32 link_info_len;
657 };
658 #define bpf_iter_attach_opts__last_field link_info_len
659 
660 LIBBPF_API struct bpf_link *
661 bpf_program__attach_iter(const struct bpf_program *prog,
662 			 const struct bpf_iter_attach_opts *opts);
663 
664 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
665 
666 /**
667  * @brief **bpf_program__set_type()** sets the program
668  * type of the passed BPF program.
669  * @param prog BPF program to set the program type for
670  * @param type program type to set the BPF map to have
671  * @return error code; or 0 if no error. An error occurs
672  * if the object is already loaded.
673  *
674  * This must be called before the BPF object is loaded,
675  * otherwise it has no effect and an error is returned.
676  */
677 LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
678 				     enum bpf_prog_type type);
679 
680 LIBBPF_API enum bpf_attach_type
681 bpf_program__expected_attach_type(const struct bpf_program *prog);
682 
683 /**
684  * @brief **bpf_program__set_expected_attach_type()** sets the
685  * attach type of the passed BPF program. This is used for
686  * auto-detection of attachment when programs are loaded.
687  * @param prog BPF program to set the attach type for
688  * @param type attach type to set the BPF map to have
689  * @return error code; or 0 if no error. An error occurs
690  * if the object is already loaded.
691  *
692  * This must be called before the BPF object is loaded,
693  * otherwise it has no effect and an error is returned.
694  */
695 LIBBPF_API int
696 bpf_program__set_expected_attach_type(struct bpf_program *prog,
697 				      enum bpf_attach_type type);
698 
699 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
700 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
701 
702 /* Per-program log level and log buffer getters/setters.
703  * See bpf_object_open_opts comments regarding log_level and log_buf
704  * interactions.
705  */
706 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
707 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
708 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
709 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
710 
711 /**
712  * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
713  * for supported BPF program types:
714  *   - BTF-aware raw tracepoints (tp_btf);
715  *   - fentry/fexit/fmod_ret;
716  *   - lsm;
717  *   - freplace.
718  * @param prog BPF program to set the attach type for
719  * @param type attach type to set the BPF map to have
720  * @return error code; or 0 if no error occurred.
721  */
722 LIBBPF_API int
723 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
724 			       const char *attach_func_name);
725 
726 /**
727  * @brief **bpf_object__find_map_by_name()** returns BPF map of
728  * the given name, if it exists within the passed BPF object
729  * @param obj BPF object
730  * @param name name of the BPF map
731  * @return BPF map instance, if such map exists within the BPF object;
732  * or NULL otherwise.
733  */
734 LIBBPF_API struct bpf_map *
735 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
736 
737 LIBBPF_API int
738 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
739 
740 LIBBPF_API struct bpf_map *
741 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
742 
743 #define bpf_object__for_each_map(pos, obj)		\
744 	for ((pos) = bpf_object__next_map((obj), NULL);	\
745 	     (pos) != NULL;				\
746 	     (pos) = bpf_object__next_map((obj), (pos)))
747 #define bpf_map__for_each bpf_object__for_each_map
748 
749 LIBBPF_API struct bpf_map *
750 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
751 
752 /**
753  * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
754  * BPF map during BPF object load phase.
755  * @param map the BPF map instance
756  * @param autocreate whether to create BPF map during BPF object load
757  * @return 0 on success; -EBUSY if BPF object was already loaded
758  *
759  * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
760  * BPF map. By default, libbpf will attempt to create every single BPF map
761  * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
762  * and fill in map FD in BPF instructions.
763  *
764  * This API allows to opt-out of this process for specific map instance. This
765  * can be useful if host kernel doesn't support such BPF map type or used
766  * combination of flags and user application wants to avoid creating such
767  * a map in the first place. User is still responsible to make sure that their
768  * BPF-side code that expects to use such missing BPF map is recognized by BPF
769  * verifier as dead code, otherwise BPF verifier will reject such BPF program.
770  */
771 LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
772 LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
773 
774 /**
775  * @brief **bpf_map__fd()** gets the file descriptor of the passed
776  * BPF map
777  * @param map the BPF map instance
778  * @return the file descriptor; or -EINVAL in case of an error
779  */
780 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
781 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
782 /* get map name */
783 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
784 /* get/set map type */
785 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
786 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
787 /* get/set map size (max_entries) */
788 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
789 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
790 /* get/set map flags */
791 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
792 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
793 /* get/set map NUMA node */
794 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
795 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
796 /* get/set map key size */
797 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
798 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
799 /* get/set map value size */
800 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
801 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
802 /* get map key/value BTF type IDs */
803 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
804 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
805 /* get/set map if_index */
806 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
807 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
808 /* get/set map map_extra flags */
809 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
810 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
811 
812 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
813 					  const void *data, size_t size);
814 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
815 
816 /**
817  * @brief **bpf_map__is_internal()** tells the caller whether or not the
818  * passed map is a special map created by libbpf automatically for things like
819  * global variables, __ksym externs, Kconfig values, etc
820  * @param map the bpf_map
821  * @return true, if the map is an internal map; false, otherwise
822  */
823 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
824 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
825 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
826 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
827 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
828 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
829 
830 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
831 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
832 
833 /**
834  * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
835  * corresponding to provided key.
836  * @param map BPF map to lookup element in
837  * @param key pointer to memory containing bytes of the key used for lookup
838  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
839  * @param value pointer to memory in which looked up value will be stored
840  * @param value_sz size in byte of value data memory; it has to match BPF map
841  * definition's **value_size**. For per-CPU BPF maps value size has to be
842  * a product of BPF map value size and number of possible CPUs in the system
843  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
844  * per-CPU values value size has to be aligned up to closest 8 bytes for
845  * alignment reasons, so expected size is: `round_up(value_size, 8)
846  * * libbpf_num_possible_cpus()`.
847  * @flags extra flags passed to kernel for this operation
848  * @return 0, on success; negative error, otherwise
849  *
850  * **bpf_map__lookup_elem()** is high-level equivalent of
851  * **bpf_map_lookup_elem()** API with added check for key and value size.
852  */
853 LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
854 				    const void *key, size_t key_sz,
855 				    void *value, size_t value_sz, __u64 flags);
856 
857 /**
858  * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
859  * map that corresponds to provided key.
860  * @param map BPF map to insert to or update element in
861  * @param key pointer to memory containing bytes of the key
862  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
863  * @param value pointer to memory containing bytes of the value
864  * @param value_sz size in byte of value data memory; it has to match BPF map
865  * definition's **value_size**. For per-CPU BPF maps value size has to be
866  * a product of BPF map value size and number of possible CPUs in the system
867  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
868  * per-CPU values value size has to be aligned up to closest 8 bytes for
869  * alignment reasons, so expected size is: `round_up(value_size, 8)
870  * * libbpf_num_possible_cpus()`.
871  * @flags extra flags passed to kernel for this operation
872  * @return 0, on success; negative error, otherwise
873  *
874  * **bpf_map__update_elem()** is high-level equivalent of
875  * **bpf_map_update_elem()** API with added check for key and value size.
876  */
877 LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
878 				    const void *key, size_t key_sz,
879 				    const void *value, size_t value_sz, __u64 flags);
880 
881 /**
882  * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
883  * corresponds to provided key.
884  * @param map BPF map to delete element from
885  * @param key pointer to memory containing bytes of the key
886  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
887  * @flags extra flags passed to kernel for this operation
888  * @return 0, on success; negative error, otherwise
889  *
890  * **bpf_map__delete_elem()** is high-level equivalent of
891  * **bpf_map_delete_elem()** API with added check for key size.
892  */
893 LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
894 				    const void *key, size_t key_sz, __u64 flags);
895 
896 /**
897  * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
898  * corresponding to provided key and atomically delete it afterwards.
899  * @param map BPF map to lookup element in
900  * @param key pointer to memory containing bytes of the key used for lookup
901  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
902  * @param value pointer to memory in which looked up value will be stored
903  * @param value_sz size in byte of value data memory; it has to match BPF map
904  * definition's **value_size**. For per-CPU BPF maps value size has to be
905  * a product of BPF map value size and number of possible CPUs in the system
906  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
907  * per-CPU values value size has to be aligned up to closest 8 bytes for
908  * alignment reasons, so expected size is: `round_up(value_size, 8)
909  * * libbpf_num_possible_cpus()`.
910  * @flags extra flags passed to kernel for this operation
911  * @return 0, on success; negative error, otherwise
912  *
913  * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
914  * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
915  */
916 LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
917 					       const void *key, size_t key_sz,
918 					       void *value, size_t value_sz, __u64 flags);
919 
920 /**
921  * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
922  * fetching next key that follows current key.
923  * @param map BPF map to fetch next key from
924  * @param cur_key pointer to memory containing bytes of current key or NULL to
925  * fetch the first key
926  * @param next_key pointer to memory to write next key into
927  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
928  * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
929  * negative error, otherwise
930  *
931  * **bpf_map__get_next_key()** is high-level equivalent of
932  * **bpf_map_get_next_key()** API with added check for key size.
933  */
934 LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
935 				     const void *cur_key, void *next_key, size_t key_sz);
936 
937 struct bpf_xdp_set_link_opts {
938 	size_t sz;
939 	int old_fd;
940 	size_t :0;
941 };
942 #define bpf_xdp_set_link_opts__last_field old_fd
943 
944 struct bpf_xdp_attach_opts {
945 	size_t sz;
946 	int old_prog_fd;
947 	size_t :0;
948 };
949 #define bpf_xdp_attach_opts__last_field old_prog_fd
950 
951 struct bpf_xdp_query_opts {
952 	size_t sz;
953 	__u32 prog_id;		/* output */
954 	__u32 drv_prog_id;	/* output */
955 	__u32 hw_prog_id;	/* output */
956 	__u32 skb_prog_id;	/* output */
957 	__u8 attach_mode;	/* output */
958 	size_t :0;
959 };
960 #define bpf_xdp_query_opts__last_field attach_mode
961 
962 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
963 			      const struct bpf_xdp_attach_opts *opts);
964 LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
965 			      const struct bpf_xdp_attach_opts *opts);
966 LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
967 LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
968 
969 /* TC related API */
970 enum bpf_tc_attach_point {
971 	BPF_TC_INGRESS = 1 << 0,
972 	BPF_TC_EGRESS  = 1 << 1,
973 	BPF_TC_CUSTOM  = 1 << 2,
974 };
975 
976 #define BPF_TC_PARENT(a, b) 	\
977 	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
978 
979 enum bpf_tc_flags {
980 	BPF_TC_F_REPLACE = 1 << 0,
981 };
982 
983 struct bpf_tc_hook {
984 	size_t sz;
985 	int ifindex;
986 	enum bpf_tc_attach_point attach_point;
987 	__u32 parent;
988 	size_t :0;
989 };
990 #define bpf_tc_hook__last_field parent
991 
992 struct bpf_tc_opts {
993 	size_t sz;
994 	int prog_fd;
995 	__u32 flags;
996 	__u32 prog_id;
997 	__u32 handle;
998 	__u32 priority;
999 	size_t :0;
1000 };
1001 #define bpf_tc_opts__last_field priority
1002 
1003 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
1004 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
1005 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
1006 			     struct bpf_tc_opts *opts);
1007 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
1008 			     const struct bpf_tc_opts *opts);
1009 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
1010 			    struct bpf_tc_opts *opts);
1011 
1012 /* Ring buffer APIs */
1013 struct ring_buffer;
1014 
1015 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
1016 
1017 struct ring_buffer_opts {
1018 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1019 };
1020 
1021 #define ring_buffer_opts__last_field sz
1022 
1023 LIBBPF_API struct ring_buffer *
1024 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
1025 		 const struct ring_buffer_opts *opts);
1026 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
1027 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
1028 				ring_buffer_sample_fn sample_cb, void *ctx);
1029 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
1030 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
1031 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
1032 
1033 /* Perf buffer APIs */
1034 struct perf_buffer;
1035 
1036 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
1037 				      void *data, __u32 size);
1038 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
1039 
1040 /* common use perf buffer options */
1041 struct perf_buffer_opts {
1042 	size_t sz;
1043 };
1044 #define perf_buffer_opts__last_field sz
1045 
1046 /**
1047  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1048  * BPF_PERF_EVENT_ARRAY map
1049  * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1050  * code to send data over to user-space
1051  * @param page_cnt number of memory pages allocated for each per-CPU buffer
1052  * @param sample_cb function called on each received data record
1053  * @param lost_cb function called when record loss has occurred
1054  * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1055  * @return a new instance of struct perf_buffer on success, NULL on error with
1056  * *errno* containing an error code
1057  */
1058 LIBBPF_API struct perf_buffer *
1059 perf_buffer__new(int map_fd, size_t page_cnt,
1060 		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1061 		 const struct perf_buffer_opts *opts);
1062 
1063 enum bpf_perf_event_ret {
1064 	LIBBPF_PERF_EVENT_DONE	= 0,
1065 	LIBBPF_PERF_EVENT_ERROR	= -1,
1066 	LIBBPF_PERF_EVENT_CONT	= -2,
1067 };
1068 
1069 struct perf_event_header;
1070 
1071 typedef enum bpf_perf_event_ret
1072 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1073 
1074 /* raw perf buffer options, giving most power and control */
1075 struct perf_buffer_raw_opts {
1076 	size_t sz;
1077 	long :0;
1078 	long :0;
1079 	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1080 	 * max_entries of given PERF_EVENT_ARRAY map)
1081 	 */
1082 	int cpu_cnt;
1083 	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1084 	int *cpus;
1085 	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1086 	int *map_keys;
1087 };
1088 #define perf_buffer_raw_opts__last_field map_keys
1089 
1090 struct perf_event_attr;
1091 
1092 LIBBPF_API struct perf_buffer *
1093 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1094 		     perf_buffer_event_fn event_cb, void *ctx,
1095 		     const struct perf_buffer_raw_opts *opts);
1096 
1097 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1098 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1099 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1100 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1101 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1102 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1103 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1104 /**
1105  * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying
1106  * memory region of the ring buffer.
1107  * This ring buffer can be used to implement a custom events consumer.
1108  * The ring buffer starts with the *struct perf_event_mmap_page*, which
1109  * holds the ring buffer managment fields, when accessing the header
1110  * structure it's important to be SMP aware.
1111  * You can refer to *perf_event_read_simple* for a simple example.
1112  * @param pb the perf buffer structure
1113  * @param buf_idx the buffer index to retreive
1114  * @param buf (out) gets the base pointer of the mmap()'ed memory
1115  * @param buf_size (out) gets the size of the mmap()'ed region
1116  * @return 0 on success, negative error code for failure
1117  */
1118 LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,
1119 				   size_t *buf_size);
1120 
1121 struct bpf_prog_linfo;
1122 struct bpf_prog_info;
1123 
1124 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1125 LIBBPF_API struct bpf_prog_linfo *
1126 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1127 LIBBPF_API const struct bpf_line_info *
1128 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1129 				__u64 addr, __u32 func_idx, __u32 nr_skip);
1130 LIBBPF_API const struct bpf_line_info *
1131 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1132 		      __u32 insn_off, __u32 nr_skip);
1133 
1134 /*
1135  * Probe for supported system features
1136  *
1137  * Note that running many of these probes in a short amount of time can cause
1138  * the kernel to reach the maximal size of lockable memory allowed for the
1139  * user, causing subsequent probes to fail. In this case, the caller may want
1140  * to adjust that limit with setrlimit().
1141  */
1142 
1143 /**
1144  * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1145  * BPF programs of a given type.
1146  * @param prog_type BPF program type to detect kernel support for
1147  * @param opts reserved for future extensibility, should be NULL
1148  * @return 1, if given program type is supported; 0, if given program type is
1149  * not supported; negative error code if feature detection failed or can't be
1150  * performed
1151  *
1152  * Make sure the process has required set of CAP_* permissions (or runs as
1153  * root) when performing feature checking.
1154  */
1155 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1156 /**
1157  * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1158  * BPF maps of a given type.
1159  * @param map_type BPF map type to detect kernel support for
1160  * @param opts reserved for future extensibility, should be NULL
1161  * @return 1, if given map type is supported; 0, if given map type is
1162  * not supported; negative error code if feature detection failed or can't be
1163  * performed
1164  *
1165  * Make sure the process has required set of CAP_* permissions (or runs as
1166  * root) when performing feature checking.
1167  */
1168 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1169 /**
1170  * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1171  * use of a given BPF helper from specified BPF program type.
1172  * @param prog_type BPF program type used to check the support of BPF helper
1173  * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1174  * @param opts reserved for future extensibility, should be NULL
1175  * @return 1, if given combination of program type and helper is supported; 0,
1176  * if the combination is not supported; negative error code if feature
1177  * detection for provided input arguments failed or can't be performed
1178  *
1179  * Make sure the process has required set of CAP_* permissions (or runs as
1180  * root) when performing feature checking.
1181  */
1182 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1183 				       enum bpf_func_id helper_id, const void *opts);
1184 
1185 /**
1186  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1187  * number of possible CPUs that the host kernel supports and expects.
1188  * @return number of possible CPUs; or error code on failure
1189  *
1190  * Example usage:
1191  *
1192  *     int ncpus = libbpf_num_possible_cpus();
1193  *     if (ncpus < 0) {
1194  *          // error handling
1195  *     }
1196  *     long values[ncpus];
1197  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1198  */
1199 LIBBPF_API int libbpf_num_possible_cpus(void);
1200 
1201 struct bpf_map_skeleton {
1202 	const char *name;
1203 	struct bpf_map **map;
1204 	void **mmaped;
1205 };
1206 
1207 struct bpf_prog_skeleton {
1208 	const char *name;
1209 	struct bpf_program **prog;
1210 	struct bpf_link **link;
1211 };
1212 
1213 struct bpf_object_skeleton {
1214 	size_t sz; /* size of this struct, for forward/backward compatibility */
1215 
1216 	const char *name;
1217 	const void *data;
1218 	size_t data_sz;
1219 
1220 	struct bpf_object **obj;
1221 
1222 	int map_cnt;
1223 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1224 	struct bpf_map_skeleton *maps;
1225 
1226 	int prog_cnt;
1227 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1228 	struct bpf_prog_skeleton *progs;
1229 };
1230 
1231 LIBBPF_API int
1232 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1233 			  const struct bpf_object_open_opts *opts);
1234 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1235 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1236 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1237 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1238 
1239 struct bpf_var_skeleton {
1240 	const char *name;
1241 	struct bpf_map **map;
1242 	void **addr;
1243 };
1244 
1245 struct bpf_object_subskeleton {
1246 	size_t sz; /* size of this struct, for forward/backward compatibility */
1247 
1248 	const struct bpf_object *obj;
1249 
1250 	int map_cnt;
1251 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1252 	struct bpf_map_skeleton *maps;
1253 
1254 	int prog_cnt;
1255 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1256 	struct bpf_prog_skeleton *progs;
1257 
1258 	int var_cnt;
1259 	int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1260 	struct bpf_var_skeleton *vars;
1261 };
1262 
1263 LIBBPF_API int
1264 bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1265 LIBBPF_API void
1266 bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1267 
1268 struct gen_loader_opts {
1269 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1270 	const char *data;
1271 	const char *insns;
1272 	__u32 data_sz;
1273 	__u32 insns_sz;
1274 };
1275 
1276 #define gen_loader_opts__last_field insns_sz
1277 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1278 				      struct gen_loader_opts *opts);
1279 
1280 enum libbpf_tristate {
1281 	TRI_NO = 0,
1282 	TRI_YES = 1,
1283 	TRI_MODULE = 2,
1284 };
1285 
1286 struct bpf_linker_opts {
1287 	/* size of this struct, for forward/backward compatiblity */
1288 	size_t sz;
1289 };
1290 #define bpf_linker_opts__last_field sz
1291 
1292 struct bpf_linker_file_opts {
1293 	/* size of this struct, for forward/backward compatiblity */
1294 	size_t sz;
1295 };
1296 #define bpf_linker_file_opts__last_field sz
1297 
1298 struct bpf_linker;
1299 
1300 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1301 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1302 				    const char *filename,
1303 				    const struct bpf_linker_file_opts *opts);
1304 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1305 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1306 
1307 /*
1308  * Custom handling of BPF program's SEC() definitions
1309  */
1310 
1311 struct bpf_prog_load_opts; /* defined in bpf.h */
1312 
1313 /* Called during bpf_object__open() for each recognized BPF program. Callback
1314  * can use various bpf_program__set_*() setters to adjust whatever properties
1315  * are necessary.
1316  */
1317 typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1318 
1319 /* Called right before libbpf performs bpf_prog_load() to load BPF program
1320  * into the kernel. Callback can adjust opts as necessary.
1321  */
1322 typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1323 					     struct bpf_prog_load_opts *opts, long cookie);
1324 
1325 /* Called during skeleton attach or through bpf_program__attach(). If
1326  * auto-attach is not supported, callback should return 0 and set link to
1327  * NULL (it's not considered an error during skeleton attach, but it will be
1328  * an error for bpf_program__attach() calls). On error, error should be
1329  * returned directly and link set to NULL. On success, return 0 and set link
1330  * to a valid struct bpf_link.
1331  */
1332 typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1333 				       struct bpf_link **link);
1334 
1335 struct libbpf_prog_handler_opts {
1336 	/* size of this struct, for forward/backward compatiblity */
1337 	size_t sz;
1338 	/* User-provided value that is passed to prog_setup_fn,
1339 	 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1340 	 * register one set of callbacks for multiple SEC() definitions and
1341 	 * still be able to distinguish them, if necessary. For example,
1342 	 * libbpf itself is using this to pass necessary flags (e.g.,
1343 	 * sleepable flag) to a common internal SEC() handler.
1344 	 */
1345 	long cookie;
1346 	/* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1347 	 * Callback is optional, pass NULL if it's not necessary.
1348 	 */
1349 	libbpf_prog_setup_fn_t prog_setup_fn;
1350 	/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1351 	 * Callback is optional, pass NULL if it's not necessary.
1352 	 */
1353 	libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1354 	/* BPF program attach callback (see libbpf_prog_attach_fn_t).
1355 	 * Callback is optional, pass NULL if it's not necessary.
1356 	 */
1357 	libbpf_prog_attach_fn_t prog_attach_fn;
1358 };
1359 #define libbpf_prog_handler_opts__last_field prog_attach_fn
1360 
1361 /**
1362  * @brief **libbpf_register_prog_handler()** registers a custom BPF program
1363  * SEC() handler.
1364  * @param sec section prefix for which custom handler is registered
1365  * @param prog_type BPF program type associated with specified section
1366  * @param exp_attach_type Expected BPF attach type associated with specified section
1367  * @param opts optional cookie, callbacks, and other extra options
1368  * @return Non-negative handler ID is returned on success. This handler ID has
1369  * to be passed to *libbpf_unregister_prog_handler()* to unregister such
1370  * custom handler. Negative error code is returned on error.
1371  *
1372  * *sec* defines which SEC() definitions are handled by this custom handler
1373  * registration. *sec* can have few different forms:
1374  *   - if *sec* is just a plain string (e.g., "abc"), it will match only
1375  *   SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1376  *   in an error;
1377  *   - if *sec* is of the form "abc/", proper SEC() form is
1378  *   SEC("abc/something"), where acceptable "something" should be checked by
1379  *   *prog_init_fn* callback, if there are additional restrictions;
1380  *   - if *sec* is of the form "abc+", it will successfully match both
1381  *   SEC("abc") and SEC("abc/whatever") forms;
1382  *   - if *sec* is NULL, custom handler is registered for any BPF program that
1383  *   doesn't match any of the registered (custom or libbpf's own) SEC()
1384  *   handlers. There could be only one such generic custom handler registered
1385  *   at any given time.
1386  *
1387  * All custom handlers (except the one with *sec* == NULL) are processed
1388  * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1389  * SEC() handlers by registering custom ones for the same section prefix
1390  * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1391  * handler).
1392  *
1393  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1394  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1395  * to ensure synchronization if there is a risk of running this API from
1396  * multiple threads simultaneously.
1397  */
1398 LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1399 					    enum bpf_prog_type prog_type,
1400 					    enum bpf_attach_type exp_attach_type,
1401 					    const struct libbpf_prog_handler_opts *opts);
1402 /**
1403  * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1404  * custom BPF program SEC() handler.
1405  * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1406  * after successful registration
1407  * @return 0 on success, negative error code if handler isn't found
1408  *
1409  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1410  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1411  * to ensure synchronization if there is a risk of running this API from
1412  * multiple threads simultaneously.
1413  */
1414 LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1415 
1416 #ifdef __cplusplus
1417 } /* extern "C" */
1418 #endif
1419 
1420 #endif /* __LIBBPF_LIBBPF_H */
1421