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