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