xref: /linux-6.15/tools/lib/bpf/libbpf.h (revision 1d0dd6ea)
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 <[email protected]>
7  * Copyright (C) 2015 Wang Nan <[email protected]>
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 /**
100  * @brief **libbpf_set_print()** sets user-provided log callback function to
101  * be used for libbpf warnings and informational messages.
102  * @param fn The log print function. If NULL, libbpf won't print anything.
103  * @return Pointer to old print function.
104  *
105  * This function is thread-safe.
106  */
107 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
108 
109 /* Hide internal to user */
110 struct bpf_object;
111 
112 struct bpf_object_open_opts {
113 	/* size of this struct, for forward/backward compatibility */
114 	size_t sz;
115 	/* object name override, if provided:
116 	 * - for object open from file, this will override setting object
117 	 *   name from file path's base name;
118 	 * - for object open from memory buffer, this will specify an object
119 	 *   name and will override default "<addr>-<buf-size>" name;
120 	 */
121 	const char *object_name;
122 	/* parse map definitions non-strictly, allowing extra attributes/data */
123 	bool relaxed_maps;
124 	/* maps that set the 'pinning' attribute in their definition will have
125 	 * their pin_path attribute set to a file in this directory, and be
126 	 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
127 	 */
128 	const char *pin_root_path;
129 
130 	__u32 :32; /* stub out now removed attach_prog_fd */
131 
132 	/* Additional kernel config content that augments and overrides
133 	 * system Kconfig for CONFIG_xxx externs.
134 	 */
135 	const char *kconfig;
136 	/* Path to the custom BTF to be used for BPF CO-RE relocations.
137 	 * This custom BTF completely replaces the use of vmlinux BTF
138 	 * for the purpose of CO-RE relocations.
139 	 * NOTE: any other BPF feature (e.g., fentry/fexit programs,
140 	 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
141 	 */
142 	const char *btf_custom_path;
143 	/* Pointer to a buffer for storing kernel logs for applicable BPF
144 	 * commands. Valid kernel_log_size has to be specified as well and are
145 	 * passed-through to bpf() syscall. Keep in mind that kernel might
146 	 * fail operation with -ENOSPC error if provided buffer is too small
147 	 * to contain entire log output.
148 	 * See the comment below for kernel_log_level for interaction between
149 	 * log_buf and log_level settings.
150 	 *
151 	 * If specified, this log buffer will be passed for:
152 	 *   - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden
153 	 *     with bpf_program__set_log() on per-program level, to get
154 	 *     BPF verifier log output.
155 	 *   - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
156 	 *     BTF sanity checking log.
157 	 *
158 	 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
159 	 * previous contents, so if you need more fine-grained control, set
160 	 * per-program buffer with bpf_program__set_log_buf() to preserve each
161 	 * individual program's verification log. Keep using kernel_log_buf
162 	 * for BTF verification log, if necessary.
163 	 */
164 	char *kernel_log_buf;
165 	size_t kernel_log_size;
166 	/*
167 	 * Log level can be set independently from log buffer. Log_level=0
168 	 * means that libbpf will attempt loading BTF or program without any
169 	 * logging requested, but will retry with either its own or custom log
170 	 * buffer, if provided, and log_level=1 on any error.
171 	 * And vice versa, setting log_level>0 will request BTF or prog
172 	 * loading with verbose log from the first attempt (and as such also
173 	 * for successfully loaded BTF or program), and the actual log buffer
174 	 * could be either libbpf's own auto-allocated log buffer, if
175 	 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
176 	 * If user didn't provide custom log buffer, libbpf will emit captured
177 	 * logs through its print callback.
178 	 */
179 	__u32 kernel_log_level;
180 	/* FD of a BPF token instantiated by user through bpf_token_create()
181 	 * API. BPF object will keep dup()'ed FD internally, so passed token
182 	 * FD can be closed after BPF object/skeleton open step.
183 	 *
184 	 * Setting bpf_token_fd to negative value disables libbpf's automatic
185 	 * attempt to create BPF token from default BPF FS mount point
186 	 * (/sys/fs/bpf), in case this default behavior is undesirable.
187 	 *
188 	 * bpf_token_path and bpf_token_fd are mutually exclusive and only one
189 	 * of those options should be set.
190 	 */
191 	int bpf_token_fd;
192 	/* Path to BPF FS mount point to derive BPF token from.
193 	 *
194 	 * Created BPF token will be used for all bpf() syscall operations
195 	 * that accept BPF token (e.g., map creation, BTF and program loads,
196 	 * etc) automatically within instantiated BPF object.
197 	 *
198 	 * Setting bpf_token_path option to empty string disables libbpf's
199 	 * automatic attempt to create BPF token from default BPF FS mount
200 	 * point (/sys/fs/bpf), in case this default behavior is undesirable.
201 	 *
202 	 * bpf_token_path and bpf_token_fd are mutually exclusive and only one
203 	 * of those options should be set.
204 	 */
205 	const char *bpf_token_path;
206 
207 	size_t :0;
208 };
209 #define bpf_object_open_opts__last_field bpf_token_path
210 
211 /**
212  * @brief **bpf_object__open()** creates a bpf_object by opening
213  * the BPF ELF object file pointed to by the passed path and loading it
214  * into memory.
215  * @param path BPF object file path.
216  * @return pointer to the new bpf_object; or NULL is returned on error,
217  * error code is stored in errno
218  */
219 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
220 
221 /**
222  * @brief **bpf_object__open_file()** creates a bpf_object by opening
223  * the BPF ELF object file pointed to by the passed path and loading it
224  * into memory.
225  * @param path BPF object file path
226  * @param opts options for how to load the bpf object, this parameter is
227  * optional and can be set to NULL
228  * @return pointer to the new bpf_object; or NULL is returned on error,
229  * error code is stored in errno
230  */
231 LIBBPF_API struct bpf_object *
232 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
233 
234 /**
235  * @brief **bpf_object__open_mem()** creates a bpf_object by reading
236  * the BPF objects raw bytes from a memory buffer containing a valid
237  * BPF ELF object file.
238  * @param obj_buf pointer to the buffer containing ELF file bytes
239  * @param obj_buf_sz number of bytes in the buffer
240  * @param opts options for how to load the bpf object
241  * @return pointer to the new bpf_object; or NULL is returned on error,
242  * error code is stored in errno
243  */
244 LIBBPF_API struct bpf_object *
245 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
246 		     const struct bpf_object_open_opts *opts);
247 
248 /**
249  * @brief **bpf_object__load()** loads BPF object into kernel.
250  * @param obj Pointer to a valid BPF object instance returned by
251  * **bpf_object__open*()** APIs
252  * @return 0, on success; negative error code, otherwise, error code is
253  * stored in errno
254  */
255 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
256 
257 /**
258  * @brief **bpf_object__close()** closes a BPF object and releases all
259  * resources.
260  * @param obj Pointer to a valid BPF object
261  */
262 LIBBPF_API void bpf_object__close(struct bpf_object *obj);
263 
264 /**
265  * @brief **bpf_object__pin_maps()** pins each map contained within
266  * the BPF object at the passed directory.
267  * @param obj Pointer to a valid BPF object
268  * @param path A directory where maps should be pinned.
269  * @return 0, on success; negative error code, otherwise
270  *
271  * If `path` is NULL `bpf_map__pin` (which is being used on each map)
272  * will use the pin_path attribute of each map. In this case, maps that
273  * don't have a pin_path set will be ignored.
274  */
275 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
276 
277 /**
278  * @brief **bpf_object__unpin_maps()** unpins each map contained within
279  * the BPF object found in the passed directory.
280  * @param obj Pointer to a valid BPF object
281  * @param path A directory where pinned maps should be searched for.
282  * @return 0, on success; negative error code, otherwise
283  *
284  * If `path` is NULL `bpf_map__unpin` (which is being used on each map)
285  * will use the pin_path attribute of each map. In this case, maps that
286  * don't have a pin_path set will be ignored.
287  */
288 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
289 				      const char *path);
290 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
291 					const char *path);
292 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
293 					  const char *path);
294 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
295 LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path);
296 
297 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
298 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
299 LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
300 
301 struct btf;
302 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
303 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
304 
305 LIBBPF_API struct bpf_program *
306 bpf_object__find_program_by_name(const struct bpf_object *obj,
307 				 const char *name);
308 
309 LIBBPF_API int
310 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
311 			 enum bpf_attach_type *expected_attach_type);
312 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
313 					  enum bpf_attach_type *attach_type);
314 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
315 					  enum bpf_attach_type attach_type);
316 
317 /* Accessors of bpf_program */
318 struct bpf_program;
319 
320 LIBBPF_API struct bpf_program *
321 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
322 
323 #define bpf_object__for_each_program(pos, obj)			\
324 	for ((pos) = bpf_object__next_program((obj), NULL);	\
325 	     (pos) != NULL;					\
326 	     (pos) = bpf_object__next_program((obj), (pos)))
327 
328 LIBBPF_API struct bpf_program *
329 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
330 
331 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
332 					 __u32 ifindex);
333 
334 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
335 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
336 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
337 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
338 LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);
339 LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);
340 
341 struct bpf_insn;
342 
343 /**
344  * @brief **bpf_program__insns()** gives read-only access to BPF program's
345  * underlying BPF instructions.
346  * @param prog BPF program for which to return instructions
347  * @return a pointer to an array of BPF instructions that belong to the
348  * specified BPF program
349  *
350  * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
351  * pointed to can be fetched using **bpf_program__insn_cnt()** API.
352  *
353  * Keep in mind, libbpf can modify and append/delete BPF program's
354  * instructions as it processes BPF object file and prepares everything for
355  * uploading into the kernel. So depending on the point in BPF object
356  * lifetime, **bpf_program__insns()** can return different sets of
357  * instructions. As an example, during BPF object load phase BPF program
358  * instructions will be CO-RE-relocated, BPF subprograms instructions will be
359  * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
360  * returned before **bpf_object__load()** and after it might be quite
361  * different.
362  */
363 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
364 
365 /**
366  * @brief **bpf_program__set_insns()** can set BPF program's underlying
367  * BPF instructions.
368  *
369  * WARNING: This is a very advanced libbpf API and users need to know
370  * what they are doing. This should be used from prog_prepare_load_fn
371  * callback only.
372  *
373  * @param prog BPF program for which to return instructions
374  * @param new_insns a pointer to an array of BPF instructions
375  * @param new_insn_cnt number of `struct bpf_insn`'s that form
376  * specified BPF program
377  * @return 0, on success; negative error code, otherwise
378  */
379 LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,
380 				      struct bpf_insn *new_insns, size_t new_insn_cnt);
381 
382 /**
383  * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
384  * that form specified BPF program.
385  * @param prog BPF program for which to return number of BPF instructions
386  *
387  * See **bpf_program__insns()** documentation for notes on how libbpf can
388  * change instructions and their count during different phases of
389  * **bpf_object** lifetime.
390  */
391 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
392 
393 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
394 
395 /**
396  * @brief **bpf_program__pin()** pins the BPF program to a file
397  * in the BPF FS specified by a path. This increments the programs
398  * reference count, allowing it to stay loaded after the process
399  * which loaded it has exited.
400  *
401  * @param prog BPF program to pin, must already be loaded
402  * @param path file path in a BPF file system
403  * @return 0, on success; negative error code, otherwise
404  */
405 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
406 
407 /**
408  * @brief **bpf_program__unpin()** unpins the BPF program from a file
409  * in the BPFFS specified by a path. This decrements the programs
410  * reference count.
411  *
412  * The file pinning the BPF program can also be unlinked by a different
413  * process in which case this function will return an error.
414  *
415  * @param prog BPF program to unpin
416  * @param path file path to the pin in a BPF file system
417  * @return 0, on success; negative error code, otherwise
418  */
419 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
420 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
421 
422 struct bpf_link;
423 
424 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
425 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
426 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
427 /**
428  * @brief **bpf_link__pin()** pins the BPF link to a file
429  * in the BPF FS specified by a path. This increments the links
430  * reference count, allowing it to stay loaded after the process
431  * which loaded it has exited.
432  *
433  * @param link BPF link to pin, must already be loaded
434  * @param path file path in a BPF file system
435  * @return 0, on success; negative error code, otherwise
436  */
437 
438 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
439 
440 /**
441  * @brief **bpf_link__unpin()** unpins the BPF link from a file
442  * in the BPFFS specified by a path. This decrements the links
443  * reference count.
444  *
445  * The file pinning the BPF link can also be unlinked by a different
446  * process in which case this function will return an error.
447  *
448  * @param prog BPF program to unpin
449  * @param path file path to the pin in a BPF file system
450  * @return 0, on success; negative error code, otherwise
451  */
452 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
453 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
454 					struct bpf_program *prog);
455 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
456 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
457 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
458 
459 /**
460  * @brief **bpf_program__attach()** is a generic function for attaching
461  * a BPF program based on auto-detection of program type, attach type,
462  * and extra paremeters, where applicable.
463  *
464  * @param prog BPF program to attach
465  * @return Reference to the newly created BPF link; or NULL is returned on error,
466  * error code is stored in errno
467  *
468  * This is supported for:
469  *   - kprobe/kretprobe (depends on SEC() definition)
470  *   - uprobe/uretprobe (depends on SEC() definition)
471  *   - tracepoint
472  *   - raw tracepoint
473  *   - tracing programs (typed raw TP/fentry/fexit/fmod_ret)
474  */
475 LIBBPF_API struct bpf_link *
476 bpf_program__attach(const struct bpf_program *prog);
477 
478 struct bpf_perf_event_opts {
479 	/* size of this struct, for forward/backward compatibility */
480 	size_t sz;
481 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
482 	__u64 bpf_cookie;
483 	/* don't use BPF link when attach BPF program */
484 	bool force_ioctl_attach;
485 	size_t :0;
486 };
487 #define bpf_perf_event_opts__last_field force_ioctl_attach
488 
489 LIBBPF_API struct bpf_link *
490 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
491 
492 LIBBPF_API struct bpf_link *
493 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
494 				    const struct bpf_perf_event_opts *opts);
495 
496 /**
497  * enum probe_attach_mode - the mode to attach kprobe/uprobe
498  *
499  * force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will
500  * be returned if it is not supported by the kernel.
501  */
502 enum probe_attach_mode {
503 	/* attach probe in latest supported mode by kernel */
504 	PROBE_ATTACH_MODE_DEFAULT = 0,
505 	/* attach probe in legacy mode, using debugfs/tracefs */
506 	PROBE_ATTACH_MODE_LEGACY,
507 	/* create perf event with perf_event_open() syscall */
508 	PROBE_ATTACH_MODE_PERF,
509 	/* attach probe with BPF link */
510 	PROBE_ATTACH_MODE_LINK,
511 };
512 
513 struct bpf_kprobe_opts {
514 	/* size of this struct, for forward/backward compatibility */
515 	size_t sz;
516 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
517 	__u64 bpf_cookie;
518 	/* function's offset to install kprobe to */
519 	size_t offset;
520 	/* kprobe is return probe */
521 	bool retprobe;
522 	/* kprobe attach mode */
523 	enum probe_attach_mode attach_mode;
524 	size_t :0;
525 };
526 #define bpf_kprobe_opts__last_field attach_mode
527 
528 LIBBPF_API struct bpf_link *
529 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
530 			   const char *func_name);
531 LIBBPF_API struct bpf_link *
532 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
533                                 const char *func_name,
534                                 const struct bpf_kprobe_opts *opts);
535 
536 struct bpf_kprobe_multi_opts {
537 	/* size of this struct, for forward/backward compatibility */
538 	size_t sz;
539 	/* array of function symbols to attach */
540 	const char **syms;
541 	/* array of function addresses to attach */
542 	const unsigned long *addrs;
543 	/* array of user-provided values fetchable through bpf_get_attach_cookie */
544 	const __u64 *cookies;
545 	/* number of elements in syms/addrs/cookies arrays */
546 	size_t cnt;
547 	/* create return kprobes */
548 	bool retprobe;
549 	size_t :0;
550 };
551 
552 #define bpf_kprobe_multi_opts__last_field retprobe
553 
554 LIBBPF_API struct bpf_link *
555 bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
556 				      const char *pattern,
557 				      const struct bpf_kprobe_multi_opts *opts);
558 
559 struct bpf_uprobe_multi_opts {
560 	/* size of this struct, for forward/backward compatibility */
561 	size_t sz;
562 	/* array of function symbols to attach to */
563 	const char **syms;
564 	/* array of function addresses to attach to */
565 	const unsigned long *offsets;
566 	/* optional, array of associated ref counter offsets */
567 	const unsigned long *ref_ctr_offsets;
568 	/* optional, array of associated BPF cookies */
569 	const __u64 *cookies;
570 	/* number of elements in syms/addrs/cookies arrays */
571 	size_t cnt;
572 	/* create return uprobes */
573 	bool retprobe;
574 	size_t :0;
575 };
576 
577 #define bpf_uprobe_multi_opts__last_field retprobe
578 
579 /**
580  * @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program
581  * to multiple uprobes with uprobe_multi link.
582  *
583  * User can specify 2 mutually exclusive set of inputs:
584  *
585  *   1) use only path/func_pattern/pid arguments
586  *
587  *   2) use path/pid with allowed combinations of
588  *      syms/offsets/ref_ctr_offsets/cookies/cnt
589  *
590  *      - syms and offsets are mutually exclusive
591  *      - ref_ctr_offsets and cookies are optional
592  *
593  *
594  * @param prog BPF program to attach
595  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
596  * -1 for all processes
597  * @param binary_path Path to binary
598  * @param func_pattern Regular expression to specify functions to attach
599  * BPF program to
600  * @param opts Additional options (see **struct bpf_uprobe_multi_opts**)
601  * @return 0, on success; negative error code, otherwise
602  */
603 LIBBPF_API struct bpf_link *
604 bpf_program__attach_uprobe_multi(const struct bpf_program *prog,
605 				 pid_t pid,
606 				 const char *binary_path,
607 				 const char *func_pattern,
608 				 const struct bpf_uprobe_multi_opts *opts);
609 
610 struct bpf_ksyscall_opts {
611 	/* size of this struct, for forward/backward compatibility */
612 	size_t sz;
613 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
614 	__u64 bpf_cookie;
615 	/* attach as return probe? */
616 	bool retprobe;
617 	size_t :0;
618 };
619 #define bpf_ksyscall_opts__last_field retprobe
620 
621 /**
622  * @brief **bpf_program__attach_ksyscall()** attaches a BPF program
623  * to kernel syscall handler of a specified syscall. Optionally it's possible
624  * to request to install retprobe that will be triggered at syscall exit. It's
625  * also possible to associate BPF cookie (though options).
626  *
627  * Libbpf automatically will determine correct full kernel function name,
628  * which depending on system architecture and kernel version/configuration
629  * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will
630  * attach specified program using kprobe/kretprobe mechanism.
631  *
632  * **bpf_program__attach_ksyscall()** is an API counterpart of declarative
633  * **SEC("ksyscall/<syscall>")** annotation of BPF programs.
634  *
635  * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do
636  * not handle all the calling convention quirks for mmap(), clone() and compat
637  * syscalls. It also only attaches to "native" syscall interfaces. If host
638  * system supports compat syscalls or defines 32-bit syscalls in 64-bit
639  * kernel, such syscall interfaces won't be attached to by libbpf.
640  *
641  * These limitations may or may not change in the future. Therefore it is
642  * recommended to use SEC("kprobe") for these syscalls or if working with
643  * compat and 32-bit interfaces is required.
644  *
645  * @param prog BPF program to attach
646  * @param syscall_name Symbolic name of the syscall (e.g., "bpf")
647  * @param opts Additional options (see **struct bpf_ksyscall_opts**)
648  * @return Reference to the newly created BPF link; or NULL is returned on
649  * error, error code is stored in errno
650  */
651 LIBBPF_API struct bpf_link *
652 bpf_program__attach_ksyscall(const struct bpf_program *prog,
653 			     const char *syscall_name,
654 			     const struct bpf_ksyscall_opts *opts);
655 
656 struct bpf_uprobe_opts {
657 	/* size of this struct, for forward/backward compatibility */
658 	size_t sz;
659 	/* offset of kernel reference counted USDT semaphore, added in
660 	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
661 	 */
662 	size_t ref_ctr_offset;
663 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
664 	__u64 bpf_cookie;
665 	/* uprobe is return probe, invoked at function return time */
666 	bool retprobe;
667 	/* Function name to attach to.  Could be an unqualified ("abc") or library-qualified
668 	 * "abc@LIBXYZ" name.  To specify function entry, func_name should be set while
669 	 * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0.  To trace an
670 	 * offset within a function, specify func_name and use func_offset argument to specify
671 	 * offset within the function.  Shared library functions must specify the shared library
672 	 * binary_path.
673 	 */
674 	const char *func_name;
675 	/* uprobe attach mode */
676 	enum probe_attach_mode attach_mode;
677 	size_t :0;
678 };
679 #define bpf_uprobe_opts__last_field attach_mode
680 
681 /**
682  * @brief **bpf_program__attach_uprobe()** attaches a BPF program
683  * to the userspace function which is found by binary path and
684  * offset. You can optionally specify a particular proccess to attach
685  * to. You can also optionally attach the program to the function
686  * exit instead of entry.
687  *
688  * @param prog BPF program to attach
689  * @param retprobe Attach to function exit
690  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
691  * -1 for all processes
692  * @param binary_path Path to binary that contains the function symbol
693  * @param func_offset Offset within the binary of the function symbol
694  * @return Reference to the newly created BPF link; or NULL is returned on error,
695  * error code is stored in errno
696  */
697 LIBBPF_API struct bpf_link *
698 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
699 			   pid_t pid, const char *binary_path,
700 			   size_t func_offset);
701 
702 /**
703  * @brief **bpf_program__attach_uprobe_opts()** is just like
704  * bpf_program__attach_uprobe() except with a options struct
705  * for various configurations.
706  *
707  * @param prog BPF program to attach
708  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
709  * -1 for all processes
710  * @param binary_path Path to binary that contains the function symbol
711  * @param func_offset Offset within the binary of the function symbol
712  * @param opts Options for altering program attachment
713  * @return Reference to the newly created BPF link; or NULL is returned on error,
714  * error code is stored in errno
715  */
716 LIBBPF_API struct bpf_link *
717 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
718 				const char *binary_path, size_t func_offset,
719 				const struct bpf_uprobe_opts *opts);
720 
721 struct bpf_usdt_opts {
722 	/* size of this struct, for forward/backward compatibility */
723 	size_t sz;
724 	/* custom user-provided value accessible through usdt_cookie() */
725 	__u64 usdt_cookie;
726 	size_t :0;
727 };
728 #define bpf_usdt_opts__last_field usdt_cookie
729 
730 /**
731  * @brief **bpf_program__attach_usdt()** is just like
732  * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
733  * Statically Defined Tracepoint) attachment, instead of attaching to
734  * user-space function entry or exit.
735  *
736  * @param prog BPF program to attach
737  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
738  * -1 for all processes
739  * @param binary_path Path to binary that contains provided USDT probe
740  * @param usdt_provider USDT provider name
741  * @param usdt_name USDT probe name
742  * @param opts Options for altering program attachment
743  * @return Reference to the newly created BPF link; or NULL is returned on error,
744  * error code is stored in errno
745  */
746 LIBBPF_API struct bpf_link *
747 bpf_program__attach_usdt(const struct bpf_program *prog,
748 			 pid_t pid, const char *binary_path,
749 			 const char *usdt_provider, const char *usdt_name,
750 			 const struct bpf_usdt_opts *opts);
751 
752 struct bpf_tracepoint_opts {
753 	/* size of this struct, for forward/backward compatibility */
754 	size_t sz;
755 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
756 	__u64 bpf_cookie;
757 };
758 #define bpf_tracepoint_opts__last_field bpf_cookie
759 
760 LIBBPF_API struct bpf_link *
761 bpf_program__attach_tracepoint(const struct bpf_program *prog,
762 			       const char *tp_category,
763 			       const char *tp_name);
764 LIBBPF_API struct bpf_link *
765 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
766 				    const char *tp_category,
767 				    const char *tp_name,
768 				    const struct bpf_tracepoint_opts *opts);
769 
770 LIBBPF_API struct bpf_link *
771 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
772 				   const char *tp_name);
773 
774 struct bpf_trace_opts {
775 	/* size of this struct, for forward/backward compatibility */
776 	size_t sz;
777 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
778 	__u64 cookie;
779 };
780 #define bpf_trace_opts__last_field cookie
781 
782 LIBBPF_API struct bpf_link *
783 bpf_program__attach_trace(const struct bpf_program *prog);
784 LIBBPF_API struct bpf_link *
785 bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
786 
787 LIBBPF_API struct bpf_link *
788 bpf_program__attach_lsm(const struct bpf_program *prog);
789 LIBBPF_API struct bpf_link *
790 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
791 LIBBPF_API struct bpf_link *
792 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
793 LIBBPF_API struct bpf_link *
794 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
795 LIBBPF_API struct bpf_link *
796 bpf_program__attach_freplace(const struct bpf_program *prog,
797 			     int target_fd, const char *attach_func_name);
798 
799 struct bpf_netfilter_opts {
800 	/* size of this struct, for forward/backward compatibility */
801 	size_t sz;
802 
803 	__u32 pf;
804 	__u32 hooknum;
805 	__s32 priority;
806 	__u32 flags;
807 };
808 #define bpf_netfilter_opts__last_field flags
809 
810 LIBBPF_API struct bpf_link *
811 bpf_program__attach_netfilter(const struct bpf_program *prog,
812 			      const struct bpf_netfilter_opts *opts);
813 
814 struct bpf_tcx_opts {
815 	/* size of this struct, for forward/backward compatibility */
816 	size_t sz;
817 	__u32 flags;
818 	__u32 relative_fd;
819 	__u32 relative_id;
820 	__u64 expected_revision;
821 	size_t :0;
822 };
823 #define bpf_tcx_opts__last_field expected_revision
824 
825 LIBBPF_API struct bpf_link *
826 bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
827 			const struct bpf_tcx_opts *opts);
828 
829 struct bpf_netkit_opts {
830 	/* size of this struct, for forward/backward compatibility */
831 	size_t sz;
832 	__u32 flags;
833 	__u32 relative_fd;
834 	__u32 relative_id;
835 	__u64 expected_revision;
836 	size_t :0;
837 };
838 #define bpf_netkit_opts__last_field expected_revision
839 
840 LIBBPF_API struct bpf_link *
841 bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
842 			   const struct bpf_netkit_opts *opts);
843 
844 struct bpf_map;
845 
846 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
847 LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);
848 
849 struct bpf_iter_attach_opts {
850 	size_t sz; /* size of this struct for forward/backward compatibility */
851 	union bpf_iter_link_info *link_info;
852 	__u32 link_info_len;
853 };
854 #define bpf_iter_attach_opts__last_field link_info_len
855 
856 LIBBPF_API struct bpf_link *
857 bpf_program__attach_iter(const struct bpf_program *prog,
858 			 const struct bpf_iter_attach_opts *opts);
859 
860 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
861 
862 /**
863  * @brief **bpf_program__set_type()** sets the program
864  * type of the passed BPF program.
865  * @param prog BPF program to set the program type for
866  * @param type program type to set the BPF map to have
867  * @return error code; or 0 if no error. An error occurs
868  * if the object is already loaded.
869  *
870  * This must be called before the BPF object is loaded,
871  * otherwise it has no effect and an error is returned.
872  */
873 LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
874 				     enum bpf_prog_type type);
875 
876 LIBBPF_API enum bpf_attach_type
877 bpf_program__expected_attach_type(const struct bpf_program *prog);
878 
879 /**
880  * @brief **bpf_program__set_expected_attach_type()** sets the
881  * attach type of the passed BPF program. This is used for
882  * auto-detection of attachment when programs are loaded.
883  * @param prog BPF program to set the attach type for
884  * @param type attach type to set the BPF map to have
885  * @return error code; or 0 if no error. An error occurs
886  * if the object is already loaded.
887  *
888  * This must be called before the BPF object is loaded,
889  * otherwise it has no effect and an error is returned.
890  */
891 LIBBPF_API int
892 bpf_program__set_expected_attach_type(struct bpf_program *prog,
893 				      enum bpf_attach_type type);
894 
895 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
896 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
897 
898 /* Per-program log level and log buffer getters/setters.
899  * See bpf_object_open_opts comments regarding log_level and log_buf
900  * interactions.
901  */
902 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
903 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
904 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
905 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
906 
907 /**
908  * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
909  * for supported BPF program types:
910  *   - BTF-aware raw tracepoints (tp_btf);
911  *   - fentry/fexit/fmod_ret;
912  *   - lsm;
913  *   - freplace.
914  * @param prog BPF program to set the attach type for
915  * @param type attach type to set the BPF map to have
916  * @return error code; or 0 if no error occurred.
917  */
918 LIBBPF_API int
919 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
920 			       const char *attach_func_name);
921 
922 /**
923  * @brief **bpf_object__find_map_by_name()** returns BPF map of
924  * the given name, if it exists within the passed BPF object
925  * @param obj BPF object
926  * @param name name of the BPF map
927  * @return BPF map instance, if such map exists within the BPF object;
928  * or NULL otherwise.
929  */
930 LIBBPF_API struct bpf_map *
931 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
932 
933 LIBBPF_API int
934 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
935 
936 LIBBPF_API struct bpf_map *
937 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
938 
939 #define bpf_object__for_each_map(pos, obj)		\
940 	for ((pos) = bpf_object__next_map((obj), NULL);	\
941 	     (pos) != NULL;				\
942 	     (pos) = bpf_object__next_map((obj), (pos)))
943 #define bpf_map__for_each bpf_object__for_each_map
944 
945 LIBBPF_API struct bpf_map *
946 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
947 
948 /**
949  * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
950  * BPF map during BPF object load phase.
951  * @param map the BPF map instance
952  * @param autocreate whether to create BPF map during BPF object load
953  * @return 0 on success; -EBUSY if BPF object was already loaded
954  *
955  * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
956  * BPF map. By default, libbpf will attempt to create every single BPF map
957  * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
958  * and fill in map FD in BPF instructions.
959  *
960  * This API allows to opt-out of this process for specific map instance. This
961  * can be useful if host kernel doesn't support such BPF map type or used
962  * combination of flags and user application wants to avoid creating such
963  * a map in the first place. User is still responsible to make sure that their
964  * BPF-side code that expects to use such missing BPF map is recognized by BPF
965  * verifier as dead code, otherwise BPF verifier will reject such BPF program.
966  */
967 LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
968 LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
969 
970 /**
971  * @brief **bpf_map__fd()** gets the file descriptor of the passed
972  * BPF map
973  * @param map the BPF map instance
974  * @return the file descriptor; or -EINVAL in case of an error
975  */
976 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
977 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
978 /* get map name */
979 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
980 /* get/set map type */
981 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
982 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
983 /* get/set map size (max_entries) */
984 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
985 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
986 /* get/set map flags */
987 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
988 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
989 /* get/set map NUMA node */
990 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
991 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
992 /* get/set map key size */
993 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
994 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
995 /* get map value size */
996 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
997 /**
998  * @brief **bpf_map__set_value_size()** sets map value size.
999  * @param map the BPF map instance
1000  * @return 0, on success; negative error, otherwise
1001  *
1002  * There is a special case for maps with associated memory-mapped regions, like
1003  * the global data section maps (bss, data, rodata). When this function is used
1004  * on such a map, the mapped region is resized. Afterward, an attempt is made to
1005  * adjust the corresponding BTF info. This attempt is best-effort and can only
1006  * succeed if the last variable of the data section map is an array. The array
1007  * BTF type is replaced by a new BTF array type with a different length.
1008  * Any previously existing pointers returned from bpf_map__initial_value() or
1009  * corresponding data section skeleton pointer must be reinitialized.
1010  */
1011 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
1012 /* get map key/value BTF type IDs */
1013 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
1014 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
1015 /* get/set map if_index */
1016 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
1017 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
1018 /* get/set map map_extra flags */
1019 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
1020 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
1021 
1022 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
1023 					  const void *data, size_t size);
1024 LIBBPF_API void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
1025 
1026 /**
1027  * @brief **bpf_map__is_internal()** tells the caller whether or not the
1028  * passed map is a special map created by libbpf automatically for things like
1029  * global variables, __ksym externs, Kconfig values, etc
1030  * @param map the bpf_map
1031  * @return true, if the map is an internal map; false, otherwise
1032  */
1033 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
1034 
1035 /**
1036  * @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the
1037  * BPF map should be pinned. This does not actually create the 'pin'.
1038  * @param map The bpf_map
1039  * @param path The path
1040  * @return 0, on success; negative error, otherwise
1041  */
1042 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
1043 
1044 /**
1045  * @brief **bpf_map__pin_path()** gets the path attribute that tells where the
1046  * BPF map should be pinned.
1047  * @param map The bpf_map
1048  * @return The path string; which can be NULL
1049  */
1050 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
1051 
1052 /**
1053  * @brief **bpf_map__is_pinned()** tells the caller whether or not the
1054  * passed map has been pinned via a 'pin' file.
1055  * @param map The bpf_map
1056  * @return true, if the map is pinned; false, otherwise
1057  */
1058 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
1059 
1060 /**
1061  * @brief **bpf_map__pin()** creates a file that serves as a 'pin'
1062  * for the BPF map. This increments the reference count on the
1063  * BPF map which will keep the BPF map loaded even after the
1064  * userspace process which loaded it has exited.
1065  * @param map The bpf_map to pin
1066  * @param path A file path for the 'pin'
1067  * @return 0, on success; negative error, otherwise
1068  *
1069  * If `path` is NULL the maps `pin_path` attribute will be used. If this is
1070  * also NULL, an error will be returned and the map will not be pinned.
1071  */
1072 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
1073 
1074 /**
1075  * @brief **bpf_map__unpin()** removes the file that serves as a
1076  * 'pin' for the BPF map.
1077  * @param map The bpf_map to unpin
1078  * @param path A file path for the 'pin'
1079  * @return 0, on success; negative error, otherwise
1080  *
1081  * The `path` parameter can be NULL, in which case the `pin_path`
1082  * map attribute is unpinned. If both the `path` parameter and
1083  * `pin_path` map attribute are set, they must be equal.
1084  */
1085 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
1086 
1087 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
1088 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
1089 
1090 /**
1091  * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
1092  * corresponding to provided key.
1093  * @param map BPF map to lookup element in
1094  * @param key pointer to memory containing bytes of the key used for lookup
1095  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1096  * @param value pointer to memory in which looked up value will be stored
1097  * @param value_sz size in byte of value data memory; it has to match BPF map
1098  * definition's **value_size**. For per-CPU BPF maps value size has to be
1099  * a product of BPF map value size and number of possible CPUs in the system
1100  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1101  * per-CPU values value size has to be aligned up to closest 8 bytes for
1102  * alignment reasons, so expected size is: `round_up(value_size, 8)
1103  * * libbpf_num_possible_cpus()`.
1104  * @flags extra flags passed to kernel for this operation
1105  * @return 0, on success; negative error, otherwise
1106  *
1107  * **bpf_map__lookup_elem()** is high-level equivalent of
1108  * **bpf_map_lookup_elem()** API with added check for key and value size.
1109  */
1110 LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
1111 				    const void *key, size_t key_sz,
1112 				    void *value, size_t value_sz, __u64 flags);
1113 
1114 /**
1115  * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
1116  * map that corresponds to provided key.
1117  * @param map BPF map to insert to or update element in
1118  * @param key pointer to memory containing bytes of the key
1119  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1120  * @param value pointer to memory containing bytes of the value
1121  * @param value_sz size in byte of value data memory; it has to match BPF map
1122  * definition's **value_size**. For per-CPU BPF maps value size has to be
1123  * a product of BPF map value size and number of possible CPUs in the system
1124  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1125  * per-CPU values value size has to be aligned up to closest 8 bytes for
1126  * alignment reasons, so expected size is: `round_up(value_size, 8)
1127  * * libbpf_num_possible_cpus()`.
1128  * @flags extra flags passed to kernel for this operation
1129  * @return 0, on success; negative error, otherwise
1130  *
1131  * **bpf_map__update_elem()** is high-level equivalent of
1132  * **bpf_map_update_elem()** API with added check for key and value size.
1133  */
1134 LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
1135 				    const void *key, size_t key_sz,
1136 				    const void *value, size_t value_sz, __u64 flags);
1137 
1138 /**
1139  * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
1140  * corresponds to provided key.
1141  * @param map BPF map to delete element from
1142  * @param key pointer to memory containing bytes of the key
1143  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1144  * @flags extra flags passed to kernel for this operation
1145  * @return 0, on success; negative error, otherwise
1146  *
1147  * **bpf_map__delete_elem()** is high-level equivalent of
1148  * **bpf_map_delete_elem()** API with added check for key size.
1149  */
1150 LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
1151 				    const void *key, size_t key_sz, __u64 flags);
1152 
1153 /**
1154  * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
1155  * corresponding to provided key and atomically delete it afterwards.
1156  * @param map BPF map to lookup element in
1157  * @param key pointer to memory containing bytes of the key used for lookup
1158  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1159  * @param value pointer to memory in which looked up value will be stored
1160  * @param value_sz size in byte of value data memory; it has to match BPF map
1161  * definition's **value_size**. For per-CPU BPF maps value size has to be
1162  * a product of BPF map value size and number of possible CPUs in the system
1163  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1164  * per-CPU values value size has to be aligned up to closest 8 bytes for
1165  * alignment reasons, so expected size is: `round_up(value_size, 8)
1166  * * libbpf_num_possible_cpus()`.
1167  * @flags extra flags passed to kernel for this operation
1168  * @return 0, on success; negative error, otherwise
1169  *
1170  * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
1171  * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
1172  */
1173 LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
1174 					       const void *key, size_t key_sz,
1175 					       void *value, size_t value_sz, __u64 flags);
1176 
1177 /**
1178  * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
1179  * fetching next key that follows current key.
1180  * @param map BPF map to fetch next key from
1181  * @param cur_key pointer to memory containing bytes of current key or NULL to
1182  * fetch the first key
1183  * @param next_key pointer to memory to write next key into
1184  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1185  * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
1186  * negative error, otherwise
1187  *
1188  * **bpf_map__get_next_key()** is high-level equivalent of
1189  * **bpf_map_get_next_key()** API with added check for key size.
1190  */
1191 LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
1192 				     const void *cur_key, void *next_key, size_t key_sz);
1193 
1194 struct bpf_xdp_set_link_opts {
1195 	size_t sz;
1196 	int old_fd;
1197 	size_t :0;
1198 };
1199 #define bpf_xdp_set_link_opts__last_field old_fd
1200 
1201 struct bpf_xdp_attach_opts {
1202 	size_t sz;
1203 	int old_prog_fd;
1204 	size_t :0;
1205 };
1206 #define bpf_xdp_attach_opts__last_field old_prog_fd
1207 
1208 struct bpf_xdp_query_opts {
1209 	size_t sz;
1210 	__u32 prog_id;		/* output */
1211 	__u32 drv_prog_id;	/* output */
1212 	__u32 hw_prog_id;	/* output */
1213 	__u32 skb_prog_id;	/* output */
1214 	__u8 attach_mode;	/* output */
1215 	__u64 feature_flags;	/* output */
1216 	__u32 xdp_zc_max_segs;	/* output */
1217 	size_t :0;
1218 };
1219 #define bpf_xdp_query_opts__last_field xdp_zc_max_segs
1220 
1221 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
1222 			      const struct bpf_xdp_attach_opts *opts);
1223 LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
1224 			      const struct bpf_xdp_attach_opts *opts);
1225 LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
1226 LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
1227 
1228 /* TC related API */
1229 enum bpf_tc_attach_point {
1230 	BPF_TC_INGRESS = 1 << 0,
1231 	BPF_TC_EGRESS  = 1 << 1,
1232 	BPF_TC_CUSTOM  = 1 << 2,
1233 };
1234 
1235 #define BPF_TC_PARENT(a, b) 	\
1236 	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
1237 
1238 enum bpf_tc_flags {
1239 	BPF_TC_F_REPLACE = 1 << 0,
1240 };
1241 
1242 struct bpf_tc_hook {
1243 	size_t sz;
1244 	int ifindex;
1245 	enum bpf_tc_attach_point attach_point;
1246 	__u32 parent;
1247 	size_t :0;
1248 };
1249 #define bpf_tc_hook__last_field parent
1250 
1251 struct bpf_tc_opts {
1252 	size_t sz;
1253 	int prog_fd;
1254 	__u32 flags;
1255 	__u32 prog_id;
1256 	__u32 handle;
1257 	__u32 priority;
1258 	size_t :0;
1259 };
1260 #define bpf_tc_opts__last_field priority
1261 
1262 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
1263 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
1264 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
1265 			     struct bpf_tc_opts *opts);
1266 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
1267 			     const struct bpf_tc_opts *opts);
1268 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
1269 			    struct bpf_tc_opts *opts);
1270 
1271 /* Ring buffer APIs */
1272 struct ring_buffer;
1273 struct ring;
1274 struct user_ring_buffer;
1275 
1276 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
1277 
1278 struct ring_buffer_opts {
1279 	size_t sz; /* size of this struct, for forward/backward compatibility */
1280 };
1281 
1282 #define ring_buffer_opts__last_field sz
1283 
1284 LIBBPF_API struct ring_buffer *
1285 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
1286 		 const struct ring_buffer_opts *opts);
1287 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
1288 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
1289 				ring_buffer_sample_fn sample_cb, void *ctx);
1290 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
1291 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
1292 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
1293 
1294 /**
1295  * @brief **ring_buffer__ring()** returns the ringbuffer object inside a given
1296  * ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance.
1297  *
1298  * @param rb A ringbuffer manager object.
1299  * @param idx An index into the ringbuffers contained within the ringbuffer
1300  * manager object. The index is 0-based and corresponds to the order in which
1301  * ring_buffer__add was called.
1302  * @return A ringbuffer object on success; NULL and errno set if the index is
1303  * invalid.
1304  */
1305 LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,
1306 					  unsigned int idx);
1307 
1308 /**
1309  * @brief **ring__consumer_pos()** returns the current consumer position in the
1310  * given ringbuffer.
1311  *
1312  * @param r A ringbuffer object.
1313  * @return The current consumer position.
1314  */
1315 LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);
1316 
1317 /**
1318  * @brief **ring__producer_pos()** returns the current producer position in the
1319  * given ringbuffer.
1320  *
1321  * @param r A ringbuffer object.
1322  * @return The current producer position.
1323  */
1324 LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
1325 
1326 /**
1327  * @brief **ring__avail_data_size()** returns the number of bytes in the
1328  * ringbuffer not yet consumed. This has no locking associated with it, so it
1329  * can be inaccurate if operations are ongoing while this is called. However, it
1330  * should still show the correct trend over the long-term.
1331  *
1332  * @param r A ringbuffer object.
1333  * @return The number of bytes not yet consumed.
1334  */
1335 LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
1336 
1337 /**
1338  * @brief **ring__size()** returns the total size of the ringbuffer's map data
1339  * area (excluding special producer/consumer pages). Effectively this gives the
1340  * amount of usable bytes of data inside the ringbuffer.
1341  *
1342  * @param r A ringbuffer object.
1343  * @return The total size of the ringbuffer map data area.
1344  */
1345 LIBBPF_API size_t ring__size(const struct ring *r);
1346 
1347 /**
1348  * @brief **ring__map_fd()** returns the file descriptor underlying the given
1349  * ringbuffer.
1350  *
1351  * @param r A ringbuffer object.
1352  * @return The underlying ringbuffer file descriptor
1353  */
1354 LIBBPF_API int ring__map_fd(const struct ring *r);
1355 
1356 /**
1357  * @brief **ring__consume()** consumes available ringbuffer data without event
1358  * polling.
1359  *
1360  * @param r A ringbuffer object.
1361  * @return The number of records consumed (or INT_MAX, whichever is less), or
1362  * a negative number if any of the callbacks return an error.
1363  */
1364 LIBBPF_API int ring__consume(struct ring *r);
1365 
1366 struct user_ring_buffer_opts {
1367 	size_t sz; /* size of this struct, for forward/backward compatibility */
1368 };
1369 
1370 #define user_ring_buffer_opts__last_field sz
1371 
1372 /**
1373  * @brief **user_ring_buffer__new()** creates a new instance of a user ring
1374  * buffer.
1375  *
1376  * @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.
1377  * @param opts Options for how the ring buffer should be created.
1378  * @return A user ring buffer on success; NULL and errno being set on a
1379  * failure.
1380  */
1381 LIBBPF_API struct user_ring_buffer *
1382 user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);
1383 
1384 /**
1385  * @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the
1386  * user ring buffer.
1387  * @param rb A pointer to a user ring buffer.
1388  * @param size The size of the sample, in bytes.
1389  * @return A pointer to an 8-byte aligned reserved region of the user ring
1390  * buffer; NULL, and errno being set if a sample could not be reserved.
1391  *
1392  * This function is *not* thread safe, and callers must synchronize accessing
1393  * this function if there are multiple producers.  If a size is requested that
1394  * is larger than the size of the entire ring buffer, errno will be set to
1395  * E2BIG and NULL is returned. If the ring buffer could accommodate the size,
1396  * but currently does not have enough space, errno is set to ENOSPC and NULL is
1397  * returned.
1398  *
1399  * After initializing the sample, callers must invoke
1400  * **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,
1401  * the sample must be freed with **user_ring_buffer__discard()**.
1402  */
1403 LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
1404 
1405 /**
1406  * @brief **user_ring_buffer__reserve_blocking()** reserves a record in the
1407  * ring buffer, possibly blocking for up to @timeout_ms until a sample becomes
1408  * available.
1409  * @param rb The user ring buffer.
1410  * @param size The size of the sample, in bytes.
1411  * @param timeout_ms The amount of time, in milliseconds, for which the caller
1412  * should block when waiting for a sample. -1 causes the caller to block
1413  * indefinitely.
1414  * @return A pointer to an 8-byte aligned reserved region of the user ring
1415  * buffer; NULL, and errno being set if a sample could not be reserved.
1416  *
1417  * This function is *not* thread safe, and callers must synchronize
1418  * accessing this function if there are multiple producers
1419  *
1420  * If **timeout_ms** is -1, the function will block indefinitely until a sample
1421  * becomes available. Otherwise, **timeout_ms** must be non-negative, or errno
1422  * is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking
1423  * will occur and the function will return immediately after attempting to
1424  * reserve a sample.
1425  *
1426  * If **size** is larger than the size of the entire ring buffer, errno is set
1427  * to E2BIG and NULL is returned. If the ring buffer could accommodate
1428  * **size**, but currently does not have enough space, the caller will block
1429  * until at most **timeout_ms** has elapsed. If insufficient space is available
1430  * at that time, errno is set to ENOSPC, and NULL is returned.
1431  *
1432  * The kernel guarantees that it will wake up this thread to check if
1433  * sufficient space is available in the ring buffer at least once per
1434  * invocation of the **bpf_ringbuf_drain()** helper function, provided that at
1435  * least one sample is consumed, and the BPF program did not invoke the
1436  * function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the
1437  * kernel does not guarantee this. If the helper function is invoked with
1438  * BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is
1439  * consumed.
1440  *
1441  * When a sample of size **size** is found within **timeout_ms**, a pointer to
1442  * the sample is returned. After initializing the sample, callers must invoke
1443  * **user_ring_buffer__submit()** to post the sample to the ring buffer.
1444  * Otherwise, the sample must be freed with **user_ring_buffer__discard()**.
1445  */
1446 LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
1447 						    __u32 size,
1448 						    int timeout_ms);
1449 
1450 /**
1451  * @brief **user_ring_buffer__submit()** submits a previously reserved sample
1452  * into the ring buffer.
1453  * @param rb The user ring buffer.
1454  * @param sample A reserved sample.
1455  *
1456  * It is not necessary to synchronize amongst multiple producers when invoking
1457  * this function.
1458  */
1459 LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);
1460 
1461 /**
1462  * @brief **user_ring_buffer__discard()** discards a previously reserved sample.
1463  * @param rb The user ring buffer.
1464  * @param sample A reserved sample.
1465  *
1466  * It is not necessary to synchronize amongst multiple producers when invoking
1467  * this function.
1468  */
1469 LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);
1470 
1471 /**
1472  * @brief **user_ring_buffer__free()** frees a ring buffer that was previously
1473  * created with **user_ring_buffer__new()**.
1474  * @param rb The user ring buffer being freed.
1475  */
1476 LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);
1477 
1478 /* Perf buffer APIs */
1479 struct perf_buffer;
1480 
1481 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
1482 				      void *data, __u32 size);
1483 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
1484 
1485 /* common use perf buffer options */
1486 struct perf_buffer_opts {
1487 	size_t sz;
1488 	__u32 sample_period;
1489 	size_t :0;
1490 };
1491 #define perf_buffer_opts__last_field sample_period
1492 
1493 /**
1494  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1495  * BPF_PERF_EVENT_ARRAY map
1496  * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1497  * code to send data over to user-space
1498  * @param page_cnt number of memory pages allocated for each per-CPU buffer
1499  * @param sample_cb function called on each received data record
1500  * @param lost_cb function called when record loss has occurred
1501  * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1502  * @return a new instance of struct perf_buffer on success, NULL on error with
1503  * *errno* containing an error code
1504  */
1505 LIBBPF_API struct perf_buffer *
1506 perf_buffer__new(int map_fd, size_t page_cnt,
1507 		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1508 		 const struct perf_buffer_opts *opts);
1509 
1510 enum bpf_perf_event_ret {
1511 	LIBBPF_PERF_EVENT_DONE	= 0,
1512 	LIBBPF_PERF_EVENT_ERROR	= -1,
1513 	LIBBPF_PERF_EVENT_CONT	= -2,
1514 };
1515 
1516 struct perf_event_header;
1517 
1518 typedef enum bpf_perf_event_ret
1519 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1520 
1521 /* raw perf buffer options, giving most power and control */
1522 struct perf_buffer_raw_opts {
1523 	size_t sz;
1524 	long :0;
1525 	long :0;
1526 	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1527 	 * max_entries of given PERF_EVENT_ARRAY map)
1528 	 */
1529 	int cpu_cnt;
1530 	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1531 	int *cpus;
1532 	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1533 	int *map_keys;
1534 };
1535 #define perf_buffer_raw_opts__last_field map_keys
1536 
1537 struct perf_event_attr;
1538 
1539 LIBBPF_API struct perf_buffer *
1540 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1541 		     perf_buffer_event_fn event_cb, void *ctx,
1542 		     const struct perf_buffer_raw_opts *opts);
1543 
1544 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1545 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1546 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1547 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1548 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1549 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1550 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1551 /**
1552  * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying
1553  * memory region of the ring buffer.
1554  * This ring buffer can be used to implement a custom events consumer.
1555  * The ring buffer starts with the *struct perf_event_mmap_page*, which
1556  * holds the ring buffer managment fields, when accessing the header
1557  * structure it's important to be SMP aware.
1558  * You can refer to *perf_event_read_simple* for a simple example.
1559  * @param pb the perf buffer structure
1560  * @param buf_idx the buffer index to retreive
1561  * @param buf (out) gets the base pointer of the mmap()'ed memory
1562  * @param buf_size (out) gets the size of the mmap()'ed region
1563  * @return 0 on success, negative error code for failure
1564  */
1565 LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,
1566 				   size_t *buf_size);
1567 
1568 struct bpf_prog_linfo;
1569 struct bpf_prog_info;
1570 
1571 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1572 LIBBPF_API struct bpf_prog_linfo *
1573 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1574 LIBBPF_API const struct bpf_line_info *
1575 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1576 				__u64 addr, __u32 func_idx, __u32 nr_skip);
1577 LIBBPF_API const struct bpf_line_info *
1578 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1579 		      __u32 insn_off, __u32 nr_skip);
1580 
1581 /*
1582  * Probe for supported system features
1583  *
1584  * Note that running many of these probes in a short amount of time can cause
1585  * the kernel to reach the maximal size of lockable memory allowed for the
1586  * user, causing subsequent probes to fail. In this case, the caller may want
1587  * to adjust that limit with setrlimit().
1588  */
1589 
1590 /**
1591  * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1592  * BPF programs of a given type.
1593  * @param prog_type BPF program type to detect kernel support for
1594  * @param opts reserved for future extensibility, should be NULL
1595  * @return 1, if given program type is supported; 0, if given program type is
1596  * not supported; negative error code if feature detection failed or can't be
1597  * performed
1598  *
1599  * Make sure the process has required set of CAP_* permissions (or runs as
1600  * root) when performing feature checking.
1601  */
1602 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1603 /**
1604  * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1605  * BPF maps of a given type.
1606  * @param map_type BPF map type to detect kernel support for
1607  * @param opts reserved for future extensibility, should be NULL
1608  * @return 1, if given map type is supported; 0, if given map type is
1609  * not supported; negative error code if feature detection failed or can't be
1610  * performed
1611  *
1612  * Make sure the process has required set of CAP_* permissions (or runs as
1613  * root) when performing feature checking.
1614  */
1615 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1616 /**
1617  * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1618  * use of a given BPF helper from specified BPF program type.
1619  * @param prog_type BPF program type used to check the support of BPF helper
1620  * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1621  * @param opts reserved for future extensibility, should be NULL
1622  * @return 1, if given combination of program type and helper is supported; 0,
1623  * if the combination is not supported; negative error code if feature
1624  * detection for provided input arguments failed or can't be performed
1625  *
1626  * Make sure the process has required set of CAP_* permissions (or runs as
1627  * root) when performing feature checking.
1628  */
1629 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1630 				       enum bpf_func_id helper_id, const void *opts);
1631 
1632 /**
1633  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1634  * number of possible CPUs that the host kernel supports and expects.
1635  * @return number of possible CPUs; or error code on failure
1636  *
1637  * Example usage:
1638  *
1639  *     int ncpus = libbpf_num_possible_cpus();
1640  *     if (ncpus < 0) {
1641  *          // error handling
1642  *     }
1643  *     long values[ncpus];
1644  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1645  */
1646 LIBBPF_API int libbpf_num_possible_cpus(void);
1647 
1648 struct bpf_map_skeleton {
1649 	const char *name;
1650 	struct bpf_map **map;
1651 	void **mmaped;
1652 };
1653 
1654 struct bpf_prog_skeleton {
1655 	const char *name;
1656 	struct bpf_program **prog;
1657 	struct bpf_link **link;
1658 };
1659 
1660 struct bpf_object_skeleton {
1661 	size_t sz; /* size of this struct, for forward/backward compatibility */
1662 
1663 	const char *name;
1664 	const void *data;
1665 	size_t data_sz;
1666 
1667 	struct bpf_object **obj;
1668 
1669 	int map_cnt;
1670 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1671 	struct bpf_map_skeleton *maps;
1672 
1673 	int prog_cnt;
1674 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1675 	struct bpf_prog_skeleton *progs;
1676 };
1677 
1678 LIBBPF_API int
1679 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1680 			  const struct bpf_object_open_opts *opts);
1681 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1682 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1683 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1684 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1685 
1686 struct bpf_var_skeleton {
1687 	const char *name;
1688 	struct bpf_map **map;
1689 	void **addr;
1690 };
1691 
1692 struct bpf_object_subskeleton {
1693 	size_t sz; /* size of this struct, for forward/backward compatibility */
1694 
1695 	const struct bpf_object *obj;
1696 
1697 	int map_cnt;
1698 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1699 	struct bpf_map_skeleton *maps;
1700 
1701 	int prog_cnt;
1702 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1703 	struct bpf_prog_skeleton *progs;
1704 
1705 	int var_cnt;
1706 	int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1707 	struct bpf_var_skeleton *vars;
1708 };
1709 
1710 LIBBPF_API int
1711 bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1712 LIBBPF_API void
1713 bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1714 
1715 struct gen_loader_opts {
1716 	size_t sz; /* size of this struct, for forward/backward compatibility */
1717 	const char *data;
1718 	const char *insns;
1719 	__u32 data_sz;
1720 	__u32 insns_sz;
1721 };
1722 
1723 #define gen_loader_opts__last_field insns_sz
1724 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1725 				      struct gen_loader_opts *opts);
1726 
1727 enum libbpf_tristate {
1728 	TRI_NO = 0,
1729 	TRI_YES = 1,
1730 	TRI_MODULE = 2,
1731 };
1732 
1733 struct bpf_linker_opts {
1734 	/* size of this struct, for forward/backward compatibility */
1735 	size_t sz;
1736 };
1737 #define bpf_linker_opts__last_field sz
1738 
1739 struct bpf_linker_file_opts {
1740 	/* size of this struct, for forward/backward compatibility */
1741 	size_t sz;
1742 };
1743 #define bpf_linker_file_opts__last_field sz
1744 
1745 struct bpf_linker;
1746 
1747 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1748 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1749 				    const char *filename,
1750 				    const struct bpf_linker_file_opts *opts);
1751 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1752 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1753 
1754 /*
1755  * Custom handling of BPF program's SEC() definitions
1756  */
1757 
1758 struct bpf_prog_load_opts; /* defined in bpf.h */
1759 
1760 /* Called during bpf_object__open() for each recognized BPF program. Callback
1761  * can use various bpf_program__set_*() setters to adjust whatever properties
1762  * are necessary.
1763  */
1764 typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1765 
1766 /* Called right before libbpf performs bpf_prog_load() to load BPF program
1767  * into the kernel. Callback can adjust opts as necessary.
1768  */
1769 typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1770 					     struct bpf_prog_load_opts *opts, long cookie);
1771 
1772 /* Called during skeleton attach or through bpf_program__attach(). If
1773  * auto-attach is not supported, callback should return 0 and set link to
1774  * NULL (it's not considered an error during skeleton attach, but it will be
1775  * an error for bpf_program__attach() calls). On error, error should be
1776  * returned directly and link set to NULL. On success, return 0 and set link
1777  * to a valid struct bpf_link.
1778  */
1779 typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1780 				       struct bpf_link **link);
1781 
1782 struct libbpf_prog_handler_opts {
1783 	/* size of this struct, for forward/backward compatibility */
1784 	size_t sz;
1785 	/* User-provided value that is passed to prog_setup_fn,
1786 	 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1787 	 * register one set of callbacks for multiple SEC() definitions and
1788 	 * still be able to distinguish them, if necessary. For example,
1789 	 * libbpf itself is using this to pass necessary flags (e.g.,
1790 	 * sleepable flag) to a common internal SEC() handler.
1791 	 */
1792 	long cookie;
1793 	/* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1794 	 * Callback is optional, pass NULL if it's not necessary.
1795 	 */
1796 	libbpf_prog_setup_fn_t prog_setup_fn;
1797 	/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1798 	 * Callback is optional, pass NULL if it's not necessary.
1799 	 */
1800 	libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1801 	/* BPF program attach callback (see libbpf_prog_attach_fn_t).
1802 	 * Callback is optional, pass NULL if it's not necessary.
1803 	 */
1804 	libbpf_prog_attach_fn_t prog_attach_fn;
1805 };
1806 #define libbpf_prog_handler_opts__last_field prog_attach_fn
1807 
1808 /**
1809  * @brief **libbpf_register_prog_handler()** registers a custom BPF program
1810  * SEC() handler.
1811  * @param sec section prefix for which custom handler is registered
1812  * @param prog_type BPF program type associated with specified section
1813  * @param exp_attach_type Expected BPF attach type associated with specified section
1814  * @param opts optional cookie, callbacks, and other extra options
1815  * @return Non-negative handler ID is returned on success. This handler ID has
1816  * to be passed to *libbpf_unregister_prog_handler()* to unregister such
1817  * custom handler. Negative error code is returned on error.
1818  *
1819  * *sec* defines which SEC() definitions are handled by this custom handler
1820  * registration. *sec* can have few different forms:
1821  *   - if *sec* is just a plain string (e.g., "abc"), it will match only
1822  *   SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1823  *   in an error;
1824  *   - if *sec* is of the form "abc/", proper SEC() form is
1825  *   SEC("abc/something"), where acceptable "something" should be checked by
1826  *   *prog_init_fn* callback, if there are additional restrictions;
1827  *   - if *sec* is of the form "abc+", it will successfully match both
1828  *   SEC("abc") and SEC("abc/whatever") forms;
1829  *   - if *sec* is NULL, custom handler is registered for any BPF program that
1830  *   doesn't match any of the registered (custom or libbpf's own) SEC()
1831  *   handlers. There could be only one such generic custom handler registered
1832  *   at any given time.
1833  *
1834  * All custom handlers (except the one with *sec* == NULL) are processed
1835  * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1836  * SEC() handlers by registering custom ones for the same section prefix
1837  * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1838  * handler).
1839  *
1840  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1841  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1842  * to ensure synchronization if there is a risk of running this API from
1843  * multiple threads simultaneously.
1844  */
1845 LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1846 					    enum bpf_prog_type prog_type,
1847 					    enum bpf_attach_type exp_attach_type,
1848 					    const struct libbpf_prog_handler_opts *opts);
1849 /**
1850  * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1851  * custom BPF program SEC() handler.
1852  * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1853  * after successful registration
1854  * @return 0 on success, negative error code if handler isn't found
1855  *
1856  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1857  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1858  * to ensure synchronization if there is a risk of running this API from
1859  * multiple threads simultaneously.
1860  */
1861 LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1862 
1863 #ifdef __cplusplus
1864 } /* extern "C" */
1865 #endif
1866 
1867 #endif /* __LIBBPF_LIBBPF_H */
1868