xref: /linux/tools/lib/bpf/libbpf.h (revision 27b3f70553432114b3d26f4d9c72cf02f38b84ee)
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_uprobe_opts {
461 	/* size of this struct, for forward/backward compatiblity */
462 	size_t sz;
463 	/* offset of kernel reference counted USDT semaphore, added in
464 	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
465 	 */
466 	size_t ref_ctr_offset;
467 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
468 	__u64 bpf_cookie;
469 	/* uprobe is return probe, invoked at function return time */
470 	bool retprobe;
471 	/* Function name to attach to.  Could be an unqualified ("abc") or library-qualified
472 	 * "abc@LIBXYZ" name.  To specify function entry, func_name should be set while
473 	 * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0.  To trace an
474 	 * offset within a function, specify func_name and use func_offset argument to specify
475 	 * offset within the function.  Shared library functions must specify the shared library
476 	 * binary_path.
477 	 */
478 	const char *func_name;
479 	size_t :0;
480 };
481 #define bpf_uprobe_opts__last_field func_name
482 
483 /**
484  * @brief **bpf_program__attach_uprobe()** attaches a BPF program
485  * to the userspace function which is found by binary path and
486  * offset. You can optionally specify a particular proccess to attach
487  * to. You can also optionally attach the program to the function
488  * exit instead of entry.
489  *
490  * @param prog BPF program to attach
491  * @param retprobe Attach to function exit
492  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
493  * -1 for all processes
494  * @param binary_path Path to binary that contains the function symbol
495  * @param func_offset Offset within the binary of the function symbol
496  * @return Reference to the newly created BPF link; or NULL is returned on error,
497  * error code is stored in errno
498  */
499 LIBBPF_API struct bpf_link *
500 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
501 			   pid_t pid, const char *binary_path,
502 			   size_t func_offset);
503 
504 /**
505  * @brief **bpf_program__attach_uprobe_opts()** is just like
506  * bpf_program__attach_uprobe() except with a options struct
507  * for various configurations.
508  *
509  * @param prog BPF program to attach
510  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
511  * -1 for all processes
512  * @param binary_path Path to binary that contains the function symbol
513  * @param func_offset Offset within the binary of the function symbol
514  * @param opts Options for altering program attachment
515  * @return Reference to the newly created BPF link; or NULL is returned on error,
516  * error code is stored in errno
517  */
518 LIBBPF_API struct bpf_link *
519 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
520 				const char *binary_path, size_t func_offset,
521 				const struct bpf_uprobe_opts *opts);
522 
523 struct bpf_usdt_opts {
524 	/* size of this struct, for forward/backward compatibility */
525 	size_t sz;
526 	/* custom user-provided value accessible through usdt_cookie() */
527 	__u64 usdt_cookie;
528 	size_t :0;
529 };
530 #define bpf_usdt_opts__last_field usdt_cookie
531 
532 /**
533  * @brief **bpf_program__attach_usdt()** is just like
534  * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
535  * Statically Defined Tracepoint) attachment, instead of attaching to
536  * user-space function entry or exit.
537  *
538  * @param prog BPF program to attach
539  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
540  * -1 for all processes
541  * @param binary_path Path to binary that contains provided USDT probe
542  * @param usdt_provider USDT provider name
543  * @param usdt_name USDT probe name
544  * @param opts Options for altering program attachment
545  * @return Reference to the newly created BPF link; or NULL is returned on error,
546  * error code is stored in errno
547  */
548 LIBBPF_API struct bpf_link *
549 bpf_program__attach_usdt(const struct bpf_program *prog,
550 			 pid_t pid, const char *binary_path,
551 			 const char *usdt_provider, const char *usdt_name,
552 			 const struct bpf_usdt_opts *opts);
553 
554 struct bpf_tracepoint_opts {
555 	/* size of this struct, for forward/backward compatiblity */
556 	size_t sz;
557 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
558 	__u64 bpf_cookie;
559 };
560 #define bpf_tracepoint_opts__last_field bpf_cookie
561 
562 LIBBPF_API struct bpf_link *
563 bpf_program__attach_tracepoint(const struct bpf_program *prog,
564 			       const char *tp_category,
565 			       const char *tp_name);
566 LIBBPF_API struct bpf_link *
567 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
568 				    const char *tp_category,
569 				    const char *tp_name,
570 				    const struct bpf_tracepoint_opts *opts);
571 
572 LIBBPF_API struct bpf_link *
573 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
574 				   const char *tp_name);
575 
576 struct bpf_trace_opts {
577 	/* size of this struct, for forward/backward compatibility */
578 	size_t sz;
579 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
580 	__u64 cookie;
581 };
582 #define bpf_trace_opts__last_field cookie
583 
584 LIBBPF_API struct bpf_link *
585 bpf_program__attach_trace(const struct bpf_program *prog);
586 LIBBPF_API struct bpf_link *
587 bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
588 
589 LIBBPF_API struct bpf_link *
590 bpf_program__attach_lsm(const struct bpf_program *prog);
591 LIBBPF_API struct bpf_link *
592 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
593 LIBBPF_API struct bpf_link *
594 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
595 LIBBPF_API struct bpf_link *
596 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
597 LIBBPF_API struct bpf_link *
598 bpf_program__attach_freplace(const struct bpf_program *prog,
599 			     int target_fd, const char *attach_func_name);
600 
601 struct bpf_map;
602 
603 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
604 
605 struct bpf_iter_attach_opts {
606 	size_t sz; /* size of this struct for forward/backward compatibility */
607 	union bpf_iter_link_info *link_info;
608 	__u32 link_info_len;
609 };
610 #define bpf_iter_attach_opts__last_field link_info_len
611 
612 LIBBPF_API struct bpf_link *
613 bpf_program__attach_iter(const struct bpf_program *prog,
614 			 const struct bpf_iter_attach_opts *opts);
615 
616 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
617 
618 /**
619  * @brief **bpf_program__set_type()** sets the program
620  * type of the passed BPF program.
621  * @param prog BPF program to set the program type for
622  * @param type program type to set the BPF map to have
623  * @return error code; or 0 if no error. An error occurs
624  * if the object is already loaded.
625  *
626  * This must be called before the BPF object is loaded,
627  * otherwise it has no effect and an error is returned.
628  */
629 LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
630 				     enum bpf_prog_type type);
631 
632 LIBBPF_API enum bpf_attach_type
633 bpf_program__expected_attach_type(const struct bpf_program *prog);
634 
635 /**
636  * @brief **bpf_program__set_expected_attach_type()** sets the
637  * attach type of the passed BPF program. This is used for
638  * auto-detection of attachment when programs are loaded.
639  * @param prog BPF program to set the attach type for
640  * @param type attach type to set the BPF map to have
641  * @return error code; or 0 if no error. An error occurs
642  * if the object is already loaded.
643  *
644  * This must be called before the BPF object is loaded,
645  * otherwise it has no effect and an error is returned.
646  */
647 LIBBPF_API int
648 bpf_program__set_expected_attach_type(struct bpf_program *prog,
649 				      enum bpf_attach_type type);
650 
651 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
652 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
653 
654 /* Per-program log level and log buffer getters/setters.
655  * See bpf_object_open_opts comments regarding log_level and log_buf
656  * interactions.
657  */
658 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
659 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
660 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
661 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
662 
663 /**
664  * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
665  * for supported BPF program types:
666  *   - BTF-aware raw tracepoints (tp_btf);
667  *   - fentry/fexit/fmod_ret;
668  *   - lsm;
669  *   - freplace.
670  * @param prog BPF program to set the attach type for
671  * @param type attach type to set the BPF map to have
672  * @return error code; or 0 if no error occurred.
673  */
674 LIBBPF_API int
675 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
676 			       const char *attach_func_name);
677 
678 /**
679  * @brief **bpf_object__find_map_by_name()** returns BPF map of
680  * the given name, if it exists within the passed BPF object
681  * @param obj BPF object
682  * @param name name of the BPF map
683  * @return BPF map instance, if such map exists within the BPF object;
684  * or NULL otherwise.
685  */
686 LIBBPF_API struct bpf_map *
687 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
688 
689 LIBBPF_API int
690 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
691 
692 LIBBPF_API struct bpf_map *
693 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
694 
695 #define bpf_object__for_each_map(pos, obj)		\
696 	for ((pos) = bpf_object__next_map((obj), NULL);	\
697 	     (pos) != NULL;				\
698 	     (pos) = bpf_object__next_map((obj), (pos)))
699 #define bpf_map__for_each bpf_object__for_each_map
700 
701 LIBBPF_API struct bpf_map *
702 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
703 
704 /**
705  * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
706  * BPF map during BPF object load phase.
707  * @param map the BPF map instance
708  * @param autocreate whether to create BPF map during BPF object load
709  * @return 0 on success; -EBUSY if BPF object was already loaded
710  *
711  * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
712  * BPF map. By default, libbpf will attempt to create every single BPF map
713  * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
714  * and fill in map FD in BPF instructions.
715  *
716  * This API allows to opt-out of this process for specific map instance. This
717  * can be useful if host kernel doesn't support such BPF map type or used
718  * combination of flags and user application wants to avoid creating such
719  * a map in the first place. User is still responsible to make sure that their
720  * BPF-side code that expects to use such missing BPF map is recognized by BPF
721  * verifier as dead code, otherwise BPF verifier will reject such BPF program.
722  */
723 LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
724 LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
725 
726 /**
727  * @brief **bpf_map__fd()** gets the file descriptor of the passed
728  * BPF map
729  * @param map the BPF map instance
730  * @return the file descriptor; or -EINVAL in case of an error
731  */
732 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
733 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
734 /* get map name */
735 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
736 /* get/set map type */
737 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
738 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
739 /* get/set map size (max_entries) */
740 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
741 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
742 /* get/set map flags */
743 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
744 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
745 /* get/set map NUMA node */
746 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
747 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
748 /* get/set map key size */
749 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
750 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
751 /* get/set map value size */
752 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
753 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
754 /* get map key/value BTF type IDs */
755 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
756 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
757 /* get/set map if_index */
758 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
759 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
760 /* get/set map map_extra flags */
761 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
762 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
763 
764 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
765 					  const void *data, size_t size);
766 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
767 
768 /**
769  * @brief **bpf_map__is_internal()** tells the caller whether or not the
770  * passed map is a special map created by libbpf automatically for things like
771  * global variables, __ksym externs, Kconfig values, etc
772  * @param map the bpf_map
773  * @return true, if the map is an internal map; false, otherwise
774  */
775 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
776 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
777 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
778 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
779 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
780 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
781 
782 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
783 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
784 
785 /**
786  * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
787  * corresponding to provided key.
788  * @param map BPF map to lookup element in
789  * @param key pointer to memory containing bytes of the key used for lookup
790  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
791  * @param value pointer to memory in which looked up value will be stored
792  * @param value_sz size in byte of value data memory; it has to match BPF map
793  * definition's **value_size**. For per-CPU BPF maps value size has to be
794  * a product of BPF map value size and number of possible CPUs in the system
795  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
796  * per-CPU values value size has to be aligned up to closest 8 bytes for
797  * alignment reasons, so expected size is: `round_up(value_size, 8)
798  * * libbpf_num_possible_cpus()`.
799  * @flags extra flags passed to kernel for this operation
800  * @return 0, on success; negative error, otherwise
801  *
802  * **bpf_map__lookup_elem()** is high-level equivalent of
803  * **bpf_map_lookup_elem()** API with added check for key and value size.
804  */
805 LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
806 				    const void *key, size_t key_sz,
807 				    void *value, size_t value_sz, __u64 flags);
808 
809 /**
810  * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
811  * map that corresponds to provided key.
812  * @param map BPF map to insert to or update element in
813  * @param key pointer to memory containing bytes of the key
814  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
815  * @param value pointer to memory containing bytes of the value
816  * @param value_sz size in byte of value data memory; it has to match BPF map
817  * definition's **value_size**. For per-CPU BPF maps value size has to be
818  * a product of BPF map value size and number of possible CPUs in the system
819  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
820  * per-CPU values value size has to be aligned up to closest 8 bytes for
821  * alignment reasons, so expected size is: `round_up(value_size, 8)
822  * * libbpf_num_possible_cpus()`.
823  * @flags extra flags passed to kernel for this operation
824  * @return 0, on success; negative error, otherwise
825  *
826  * **bpf_map__update_elem()** is high-level equivalent of
827  * **bpf_map_update_elem()** API with added check for key and value size.
828  */
829 LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
830 				    const void *key, size_t key_sz,
831 				    const void *value, size_t value_sz, __u64 flags);
832 
833 /**
834  * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
835  * corresponds to provided key.
836  * @param map BPF map to delete element from
837  * @param key pointer to memory containing bytes of the key
838  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
839  * @flags extra flags passed to kernel for this operation
840  * @return 0, on success; negative error, otherwise
841  *
842  * **bpf_map__delete_elem()** is high-level equivalent of
843  * **bpf_map_delete_elem()** API with added check for key size.
844  */
845 LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
846 				    const void *key, size_t key_sz, __u64 flags);
847 
848 /**
849  * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
850  * corresponding to provided key and atomically delete it afterwards.
851  * @param map BPF map to lookup element in
852  * @param key pointer to memory containing bytes of the key used for lookup
853  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
854  * @param value pointer to memory in which looked up value will be stored
855  * @param value_sz size in byte of value data memory; it has to match BPF map
856  * definition's **value_size**. For per-CPU BPF maps value size has to be
857  * a product of BPF map value size and number of possible CPUs in the system
858  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
859  * per-CPU values value size has to be aligned up to closest 8 bytes for
860  * alignment reasons, so expected size is: `round_up(value_size, 8)
861  * * libbpf_num_possible_cpus()`.
862  * @flags extra flags passed to kernel for this operation
863  * @return 0, on success; negative error, otherwise
864  *
865  * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
866  * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
867  */
868 LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
869 					       const void *key, size_t key_sz,
870 					       void *value, size_t value_sz, __u64 flags);
871 
872 /**
873  * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
874  * fetching next key that follows current key.
875  * @param map BPF map to fetch next key from
876  * @param cur_key pointer to memory containing bytes of current key or NULL to
877  * fetch the first key
878  * @param next_key pointer to memory to write next key into
879  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
880  * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
881  * negative error, otherwise
882  *
883  * **bpf_map__get_next_key()** is high-level equivalent of
884  * **bpf_map_get_next_key()** API with added check for key size.
885  */
886 LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
887 				     const void *cur_key, void *next_key, size_t key_sz);
888 
889 struct bpf_xdp_set_link_opts {
890 	size_t sz;
891 	int old_fd;
892 	size_t :0;
893 };
894 #define bpf_xdp_set_link_opts__last_field old_fd
895 
896 struct bpf_xdp_attach_opts {
897 	size_t sz;
898 	int old_prog_fd;
899 	size_t :0;
900 };
901 #define bpf_xdp_attach_opts__last_field old_prog_fd
902 
903 struct bpf_xdp_query_opts {
904 	size_t sz;
905 	__u32 prog_id;		/* output */
906 	__u32 drv_prog_id;	/* output */
907 	__u32 hw_prog_id;	/* output */
908 	__u32 skb_prog_id;	/* output */
909 	__u8 attach_mode;	/* output */
910 	size_t :0;
911 };
912 #define bpf_xdp_query_opts__last_field attach_mode
913 
914 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
915 			      const struct bpf_xdp_attach_opts *opts);
916 LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
917 			      const struct bpf_xdp_attach_opts *opts);
918 LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
919 LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
920 
921 /* TC related API */
922 enum bpf_tc_attach_point {
923 	BPF_TC_INGRESS = 1 << 0,
924 	BPF_TC_EGRESS  = 1 << 1,
925 	BPF_TC_CUSTOM  = 1 << 2,
926 };
927 
928 #define BPF_TC_PARENT(a, b) 	\
929 	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
930 
931 enum bpf_tc_flags {
932 	BPF_TC_F_REPLACE = 1 << 0,
933 };
934 
935 struct bpf_tc_hook {
936 	size_t sz;
937 	int ifindex;
938 	enum bpf_tc_attach_point attach_point;
939 	__u32 parent;
940 	size_t :0;
941 };
942 #define bpf_tc_hook__last_field parent
943 
944 struct bpf_tc_opts {
945 	size_t sz;
946 	int prog_fd;
947 	__u32 flags;
948 	__u32 prog_id;
949 	__u32 handle;
950 	__u32 priority;
951 	size_t :0;
952 };
953 #define bpf_tc_opts__last_field priority
954 
955 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
956 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
957 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
958 			     struct bpf_tc_opts *opts);
959 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
960 			     const struct bpf_tc_opts *opts);
961 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
962 			    struct bpf_tc_opts *opts);
963 
964 /* Ring buffer APIs */
965 struct ring_buffer;
966 
967 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
968 
969 struct ring_buffer_opts {
970 	size_t sz; /* size of this struct, for forward/backward compatiblity */
971 };
972 
973 #define ring_buffer_opts__last_field sz
974 
975 LIBBPF_API struct ring_buffer *
976 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
977 		 const struct ring_buffer_opts *opts);
978 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
979 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
980 				ring_buffer_sample_fn sample_cb, void *ctx);
981 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
982 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
983 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
984 
985 /* Perf buffer APIs */
986 struct perf_buffer;
987 
988 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
989 				      void *data, __u32 size);
990 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
991 
992 /* common use perf buffer options */
993 struct perf_buffer_opts {
994 	size_t sz;
995 };
996 #define perf_buffer_opts__last_field sz
997 
998 /**
999  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1000  * BPF_PERF_EVENT_ARRAY map
1001  * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1002  * code to send data over to user-space
1003  * @param page_cnt number of memory pages allocated for each per-CPU buffer
1004  * @param sample_cb function called on each received data record
1005  * @param lost_cb function called when record loss has occurred
1006  * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1007  * @return a new instance of struct perf_buffer on success, NULL on error with
1008  * *errno* containing an error code
1009  */
1010 LIBBPF_API struct perf_buffer *
1011 perf_buffer__new(int map_fd, size_t page_cnt,
1012 		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1013 		 const struct perf_buffer_opts *opts);
1014 
1015 enum bpf_perf_event_ret {
1016 	LIBBPF_PERF_EVENT_DONE	= 0,
1017 	LIBBPF_PERF_EVENT_ERROR	= -1,
1018 	LIBBPF_PERF_EVENT_CONT	= -2,
1019 };
1020 
1021 struct perf_event_header;
1022 
1023 typedef enum bpf_perf_event_ret
1024 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1025 
1026 /* raw perf buffer options, giving most power and control */
1027 struct perf_buffer_raw_opts {
1028 	size_t sz;
1029 	long :0;
1030 	long :0;
1031 	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1032 	 * max_entries of given PERF_EVENT_ARRAY map)
1033 	 */
1034 	int cpu_cnt;
1035 	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1036 	int *cpus;
1037 	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1038 	int *map_keys;
1039 };
1040 #define perf_buffer_raw_opts__last_field map_keys
1041 
1042 struct perf_event_attr;
1043 
1044 LIBBPF_API struct perf_buffer *
1045 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1046 		     perf_buffer_event_fn event_cb, void *ctx,
1047 		     const struct perf_buffer_raw_opts *opts);
1048 
1049 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1050 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1051 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1052 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1053 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1054 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1055 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1056 
1057 struct bpf_prog_linfo;
1058 struct bpf_prog_info;
1059 
1060 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1061 LIBBPF_API struct bpf_prog_linfo *
1062 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1063 LIBBPF_API const struct bpf_line_info *
1064 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1065 				__u64 addr, __u32 func_idx, __u32 nr_skip);
1066 LIBBPF_API const struct bpf_line_info *
1067 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1068 		      __u32 insn_off, __u32 nr_skip);
1069 
1070 /*
1071  * Probe for supported system features
1072  *
1073  * Note that running many of these probes in a short amount of time can cause
1074  * the kernel to reach the maximal size of lockable memory allowed for the
1075  * user, causing subsequent probes to fail. In this case, the caller may want
1076  * to adjust that limit with setrlimit().
1077  */
1078 
1079 /**
1080  * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1081  * BPF programs of a given type.
1082  * @param prog_type BPF program type to detect kernel support for
1083  * @param opts reserved for future extensibility, should be NULL
1084  * @return 1, if given program type is supported; 0, if given program type is
1085  * not supported; negative error code if feature detection failed or can't be
1086  * performed
1087  *
1088  * Make sure the process has required set of CAP_* permissions (or runs as
1089  * root) when performing feature checking.
1090  */
1091 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1092 /**
1093  * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1094  * BPF maps of a given type.
1095  * @param map_type BPF map type to detect kernel support for
1096  * @param opts reserved for future extensibility, should be NULL
1097  * @return 1, if given map type is supported; 0, if given map type is
1098  * not supported; negative error code if feature detection failed or can't be
1099  * performed
1100  *
1101  * Make sure the process has required set of CAP_* permissions (or runs as
1102  * root) when performing feature checking.
1103  */
1104 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1105 /**
1106  * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1107  * use of a given BPF helper from specified BPF program type.
1108  * @param prog_type BPF program type used to check the support of BPF helper
1109  * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1110  * @param opts reserved for future extensibility, should be NULL
1111  * @return 1, if given combination of program type and helper is supported; 0,
1112  * if the combination is not supported; negative error code if feature
1113  * detection for provided input arguments failed or can't be performed
1114  *
1115  * Make sure the process has required set of CAP_* permissions (or runs as
1116  * root) when performing feature checking.
1117  */
1118 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1119 				       enum bpf_func_id helper_id, const void *opts);
1120 
1121 /**
1122  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1123  * number of possible CPUs that the host kernel supports and expects.
1124  * @return number of possible CPUs; or error code on failure
1125  *
1126  * Example usage:
1127  *
1128  *     int ncpus = libbpf_num_possible_cpus();
1129  *     if (ncpus < 0) {
1130  *          // error handling
1131  *     }
1132  *     long values[ncpus];
1133  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1134  */
1135 LIBBPF_API int libbpf_num_possible_cpus(void);
1136 
1137 struct bpf_map_skeleton {
1138 	const char *name;
1139 	struct bpf_map **map;
1140 	void **mmaped;
1141 };
1142 
1143 struct bpf_prog_skeleton {
1144 	const char *name;
1145 	struct bpf_program **prog;
1146 	struct bpf_link **link;
1147 };
1148 
1149 struct bpf_object_skeleton {
1150 	size_t sz; /* size of this struct, for forward/backward compatibility */
1151 
1152 	const char *name;
1153 	const void *data;
1154 	size_t data_sz;
1155 
1156 	struct bpf_object **obj;
1157 
1158 	int map_cnt;
1159 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1160 	struct bpf_map_skeleton *maps;
1161 
1162 	int prog_cnt;
1163 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1164 	struct bpf_prog_skeleton *progs;
1165 };
1166 
1167 LIBBPF_API int
1168 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1169 			  const struct bpf_object_open_opts *opts);
1170 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1171 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1172 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1173 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1174 
1175 struct bpf_var_skeleton {
1176 	const char *name;
1177 	struct bpf_map **map;
1178 	void **addr;
1179 };
1180 
1181 struct bpf_object_subskeleton {
1182 	size_t sz; /* size of this struct, for forward/backward compatibility */
1183 
1184 	const struct bpf_object *obj;
1185 
1186 	int map_cnt;
1187 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1188 	struct bpf_map_skeleton *maps;
1189 
1190 	int prog_cnt;
1191 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1192 	struct bpf_prog_skeleton *progs;
1193 
1194 	int var_cnt;
1195 	int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1196 	struct bpf_var_skeleton *vars;
1197 };
1198 
1199 LIBBPF_API int
1200 bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1201 LIBBPF_API void
1202 bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1203 
1204 struct gen_loader_opts {
1205 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1206 	const char *data;
1207 	const char *insns;
1208 	__u32 data_sz;
1209 	__u32 insns_sz;
1210 };
1211 
1212 #define gen_loader_opts__last_field insns_sz
1213 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1214 				      struct gen_loader_opts *opts);
1215 
1216 enum libbpf_tristate {
1217 	TRI_NO = 0,
1218 	TRI_YES = 1,
1219 	TRI_MODULE = 2,
1220 };
1221 
1222 struct bpf_linker_opts {
1223 	/* size of this struct, for forward/backward compatiblity */
1224 	size_t sz;
1225 };
1226 #define bpf_linker_opts__last_field sz
1227 
1228 struct bpf_linker_file_opts {
1229 	/* size of this struct, for forward/backward compatiblity */
1230 	size_t sz;
1231 };
1232 #define bpf_linker_file_opts__last_field sz
1233 
1234 struct bpf_linker;
1235 
1236 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1237 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1238 				    const char *filename,
1239 				    const struct bpf_linker_file_opts *opts);
1240 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1241 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1242 
1243 /*
1244  * Custom handling of BPF program's SEC() definitions
1245  */
1246 
1247 struct bpf_prog_load_opts; /* defined in bpf.h */
1248 
1249 /* Called during bpf_object__open() for each recognized BPF program. Callback
1250  * can use various bpf_program__set_*() setters to adjust whatever properties
1251  * are necessary.
1252  */
1253 typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1254 
1255 /* Called right before libbpf performs bpf_prog_load() to load BPF program
1256  * into the kernel. Callback can adjust opts as necessary.
1257  */
1258 typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1259 					     struct bpf_prog_load_opts *opts, long cookie);
1260 
1261 /* Called during skeleton attach or through bpf_program__attach(). If
1262  * auto-attach is not supported, callback should return 0 and set link to
1263  * NULL (it's not considered an error during skeleton attach, but it will be
1264  * an error for bpf_program__attach() calls). On error, error should be
1265  * returned directly and link set to NULL. On success, return 0 and set link
1266  * to a valid struct bpf_link.
1267  */
1268 typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1269 				       struct bpf_link **link);
1270 
1271 struct libbpf_prog_handler_opts {
1272 	/* size of this struct, for forward/backward compatiblity */
1273 	size_t sz;
1274 	/* User-provided value that is passed to prog_setup_fn,
1275 	 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1276 	 * register one set of callbacks for multiple SEC() definitions and
1277 	 * still be able to distinguish them, if necessary. For example,
1278 	 * libbpf itself is using this to pass necessary flags (e.g.,
1279 	 * sleepable flag) to a common internal SEC() handler.
1280 	 */
1281 	long cookie;
1282 	/* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1283 	 * Callback is optional, pass NULL if it's not necessary.
1284 	 */
1285 	libbpf_prog_setup_fn_t prog_setup_fn;
1286 	/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1287 	 * Callback is optional, pass NULL if it's not necessary.
1288 	 */
1289 	libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1290 	/* BPF program attach callback (see libbpf_prog_attach_fn_t).
1291 	 * Callback is optional, pass NULL if it's not necessary.
1292 	 */
1293 	libbpf_prog_attach_fn_t prog_attach_fn;
1294 };
1295 #define libbpf_prog_handler_opts__last_field prog_attach_fn
1296 
1297 /**
1298  * @brief **libbpf_register_prog_handler()** registers a custom BPF program
1299  * SEC() handler.
1300  * @param sec section prefix for which custom handler is registered
1301  * @param prog_type BPF program type associated with specified section
1302  * @param exp_attach_type Expected BPF attach type associated with specified section
1303  * @param opts optional cookie, callbacks, and other extra options
1304  * @return Non-negative handler ID is returned on success. This handler ID has
1305  * to be passed to *libbpf_unregister_prog_handler()* to unregister such
1306  * custom handler. Negative error code is returned on error.
1307  *
1308  * *sec* defines which SEC() definitions are handled by this custom handler
1309  * registration. *sec* can have few different forms:
1310  *   - if *sec* is just a plain string (e.g., "abc"), it will match only
1311  *   SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1312  *   in an error;
1313  *   - if *sec* is of the form "abc/", proper SEC() form is
1314  *   SEC("abc/something"), where acceptable "something" should be checked by
1315  *   *prog_init_fn* callback, if there are additional restrictions;
1316  *   - if *sec* is of the form "abc+", it will successfully match both
1317  *   SEC("abc") and SEC("abc/whatever") forms;
1318  *   - if *sec* is NULL, custom handler is registered for any BPF program that
1319  *   doesn't match any of the registered (custom or libbpf's own) SEC()
1320  *   handlers. There could be only one such generic custom handler registered
1321  *   at any given time.
1322  *
1323  * All custom handlers (except the one with *sec* == NULL) are processed
1324  * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1325  * SEC() handlers by registering custom ones for the same section prefix
1326  * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1327  * handler).
1328  *
1329  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1330  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1331  * to ensure synchronization if there is a risk of running this API from
1332  * multiple threads simultaneously.
1333  */
1334 LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1335 					    enum bpf_prog_type prog_type,
1336 					    enum bpf_attach_type exp_attach_type,
1337 					    const struct libbpf_prog_handler_opts *opts);
1338 /**
1339  * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1340  * custom BPF program SEC() handler.
1341  * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1342  * after successful registration
1343  * @return 0 on success, negative error code if handler isn't found
1344  *
1345  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1346  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1347  * to ensure synchronization if there is a risk of running this API from
1348  * multiple threads simultaneously.
1349  */
1350 LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1351 
1352 #ifdef __cplusplus
1353 } /* extern "C" */
1354 #endif
1355 
1356 #endif /* __LIBBPF_LIBBPF_H */
1357