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