xref: /linux/tools/lib/bpf/libbpf.h (revision a266ef69b890f099069cf51bb40572611c435a54)
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 compatiblity */
451 	size_t sz;
452 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
453 	__u64 bpf_cookie;
454 };
455 #define bpf_perf_event_opts__last_field bpf_cookie
456 
457 LIBBPF_API struct bpf_link *
458 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
459 
460 LIBBPF_API struct bpf_link *
461 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
462 				    const struct bpf_perf_event_opts *opts);
463 
464 struct bpf_kprobe_opts {
465 	/* size of this struct, for forward/backward compatiblity */
466 	size_t sz;
467 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
468 	__u64 bpf_cookie;
469 	/* function's offset to install kprobe to */
470 	size_t offset;
471 	/* kprobe is return probe */
472 	bool retprobe;
473 	size_t :0;
474 };
475 #define bpf_kprobe_opts__last_field retprobe
476 
477 LIBBPF_API struct bpf_link *
478 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
479 			   const char *func_name);
480 LIBBPF_API struct bpf_link *
481 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
482                                 const char *func_name,
483                                 const struct bpf_kprobe_opts *opts);
484 
485 struct bpf_kprobe_multi_opts {
486 	/* size of this struct, for forward/backward compatibility */
487 	size_t sz;
488 	/* array of function symbols to attach */
489 	const char **syms;
490 	/* array of function addresses to attach */
491 	const unsigned long *addrs;
492 	/* array of user-provided values fetchable through bpf_get_attach_cookie */
493 	const __u64 *cookies;
494 	/* number of elements in syms/addrs/cookies arrays */
495 	size_t cnt;
496 	/* create return kprobes */
497 	bool retprobe;
498 	size_t :0;
499 };
500 
501 #define bpf_kprobe_multi_opts__last_field retprobe
502 
503 LIBBPF_API struct bpf_link *
504 bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
505 				      const char *pattern,
506 				      const struct bpf_kprobe_multi_opts *opts);
507 
508 struct bpf_ksyscall_opts {
509 	/* size of this struct, for forward/backward compatiblity */
510 	size_t sz;
511 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
512 	__u64 bpf_cookie;
513 	/* attach as return probe? */
514 	bool retprobe;
515 	size_t :0;
516 };
517 #define bpf_ksyscall_opts__last_field retprobe
518 
519 /**
520  * @brief **bpf_program__attach_ksyscall()** attaches a BPF program
521  * to kernel syscall handler of a specified syscall. Optionally it's possible
522  * to request to install retprobe that will be triggered at syscall exit. It's
523  * also possible to associate BPF cookie (though options).
524  *
525  * Libbpf automatically will determine correct full kernel function name,
526  * which depending on system architecture and kernel version/configuration
527  * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will
528  * attach specified program using kprobe/kretprobe mechanism.
529  *
530  * **bpf_program__attach_ksyscall()** is an API counterpart of declarative
531  * **SEC("ksyscall/<syscall>")** annotation of BPF programs.
532  *
533  * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do
534  * not handle all the calling convention quirks for mmap(), clone() and compat
535  * syscalls. It also only attaches to "native" syscall interfaces. If host
536  * system supports compat syscalls or defines 32-bit syscalls in 64-bit
537  * kernel, such syscall interfaces won't be attached to by libbpf.
538  *
539  * These limitations may or may not change in the future. Therefore it is
540  * recommended to use SEC("kprobe") for these syscalls or if working with
541  * compat and 32-bit interfaces is required.
542  *
543  * @param prog BPF program to attach
544  * @param syscall_name Symbolic name of the syscall (e.g., "bpf")
545  * @param opts Additional options (see **struct bpf_ksyscall_opts**)
546  * @return Reference to the newly created BPF link; or NULL is returned on
547  * error, error code is stored in errno
548  */
549 LIBBPF_API struct bpf_link *
550 bpf_program__attach_ksyscall(const struct bpf_program *prog,
551 			     const char *syscall_name,
552 			     const struct bpf_ksyscall_opts *opts);
553 
554 struct bpf_uprobe_opts {
555 	/* size of this struct, for forward/backward compatiblity */
556 	size_t sz;
557 	/* offset of kernel reference counted USDT semaphore, added in
558 	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
559 	 */
560 	size_t ref_ctr_offset;
561 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
562 	__u64 bpf_cookie;
563 	/* uprobe is return probe, invoked at function return time */
564 	bool retprobe;
565 	/* Function name to attach to.  Could be an unqualified ("abc") or library-qualified
566 	 * "abc@LIBXYZ" name.  To specify function entry, func_name should be set while
567 	 * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0.  To trace an
568 	 * offset within a function, specify func_name and use func_offset argument to specify
569 	 * offset within the function.  Shared library functions must specify the shared library
570 	 * binary_path.
571 	 */
572 	const char *func_name;
573 	size_t :0;
574 };
575 #define bpf_uprobe_opts__last_field func_name
576 
577 /**
578  * @brief **bpf_program__attach_uprobe()** attaches a BPF program
579  * to the userspace function which is found by binary path and
580  * offset. You can optionally specify a particular proccess to attach
581  * to. You can also optionally attach the program to the function
582  * exit instead of entry.
583  *
584  * @param prog BPF program to attach
585  * @param retprobe Attach to function exit
586  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
587  * -1 for all processes
588  * @param binary_path Path to binary that contains the function symbol
589  * @param func_offset Offset within the binary of the function symbol
590  * @return Reference to the newly created BPF link; or NULL is returned on error,
591  * error code is stored in errno
592  */
593 LIBBPF_API struct bpf_link *
594 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
595 			   pid_t pid, const char *binary_path,
596 			   size_t func_offset);
597 
598 /**
599  * @brief **bpf_program__attach_uprobe_opts()** is just like
600  * bpf_program__attach_uprobe() except with a options struct
601  * for various configurations.
602  *
603  * @param prog BPF program to attach
604  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
605  * -1 for all processes
606  * @param binary_path Path to binary that contains the function symbol
607  * @param func_offset Offset within the binary of the function symbol
608  * @param opts Options for altering program attachment
609  * @return Reference to the newly created BPF link; or NULL is returned on error,
610  * error code is stored in errno
611  */
612 LIBBPF_API struct bpf_link *
613 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
614 				const char *binary_path, size_t func_offset,
615 				const struct bpf_uprobe_opts *opts);
616 
617 struct bpf_usdt_opts {
618 	/* size of this struct, for forward/backward compatibility */
619 	size_t sz;
620 	/* custom user-provided value accessible through usdt_cookie() */
621 	__u64 usdt_cookie;
622 	size_t :0;
623 };
624 #define bpf_usdt_opts__last_field usdt_cookie
625 
626 /**
627  * @brief **bpf_program__attach_usdt()** is just like
628  * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
629  * Statically Defined Tracepoint) attachment, instead of attaching to
630  * user-space function entry or exit.
631  *
632  * @param prog BPF program to attach
633  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
634  * -1 for all processes
635  * @param binary_path Path to binary that contains provided USDT probe
636  * @param usdt_provider USDT provider name
637  * @param usdt_name USDT probe name
638  * @param opts Options for altering program attachment
639  * @return Reference to the newly created BPF link; or NULL is returned on error,
640  * error code is stored in errno
641  */
642 LIBBPF_API struct bpf_link *
643 bpf_program__attach_usdt(const struct bpf_program *prog,
644 			 pid_t pid, const char *binary_path,
645 			 const char *usdt_provider, const char *usdt_name,
646 			 const struct bpf_usdt_opts *opts);
647 
648 struct bpf_tracepoint_opts {
649 	/* size of this struct, for forward/backward compatiblity */
650 	size_t sz;
651 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
652 	__u64 bpf_cookie;
653 };
654 #define bpf_tracepoint_opts__last_field bpf_cookie
655 
656 LIBBPF_API struct bpf_link *
657 bpf_program__attach_tracepoint(const struct bpf_program *prog,
658 			       const char *tp_category,
659 			       const char *tp_name);
660 LIBBPF_API struct bpf_link *
661 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
662 				    const char *tp_category,
663 				    const char *tp_name,
664 				    const struct bpf_tracepoint_opts *opts);
665 
666 LIBBPF_API struct bpf_link *
667 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
668 				   const char *tp_name);
669 
670 struct bpf_trace_opts {
671 	/* size of this struct, for forward/backward compatibility */
672 	size_t sz;
673 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
674 	__u64 cookie;
675 };
676 #define bpf_trace_opts__last_field cookie
677 
678 LIBBPF_API struct bpf_link *
679 bpf_program__attach_trace(const struct bpf_program *prog);
680 LIBBPF_API struct bpf_link *
681 bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
682 
683 LIBBPF_API struct bpf_link *
684 bpf_program__attach_lsm(const struct bpf_program *prog);
685 LIBBPF_API struct bpf_link *
686 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
687 LIBBPF_API struct bpf_link *
688 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
689 LIBBPF_API struct bpf_link *
690 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
691 LIBBPF_API struct bpf_link *
692 bpf_program__attach_freplace(const struct bpf_program *prog,
693 			     int target_fd, const char *attach_func_name);
694 
695 struct bpf_map;
696 
697 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
698 
699 struct bpf_iter_attach_opts {
700 	size_t sz; /* size of this struct for forward/backward compatibility */
701 	union bpf_iter_link_info *link_info;
702 	__u32 link_info_len;
703 };
704 #define bpf_iter_attach_opts__last_field link_info_len
705 
706 LIBBPF_API struct bpf_link *
707 bpf_program__attach_iter(const struct bpf_program *prog,
708 			 const struct bpf_iter_attach_opts *opts);
709 
710 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
711 
712 /**
713  * @brief **bpf_program__set_type()** sets the program
714  * type of the passed BPF program.
715  * @param prog BPF program to set the program type for
716  * @param type program type to set the BPF map to have
717  * @return error code; or 0 if no error. An error occurs
718  * if the object is already loaded.
719  *
720  * This must be called before the BPF object is loaded,
721  * otherwise it has no effect and an error is returned.
722  */
723 LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
724 				     enum bpf_prog_type type);
725 
726 LIBBPF_API enum bpf_attach_type
727 bpf_program__expected_attach_type(const struct bpf_program *prog);
728 
729 /**
730  * @brief **bpf_program__set_expected_attach_type()** sets the
731  * attach type of the passed BPF program. This is used for
732  * auto-detection of attachment when programs are loaded.
733  * @param prog BPF program to set the attach type for
734  * @param type attach type to set the BPF map to have
735  * @return error code; or 0 if no error. An error occurs
736  * if the object is already loaded.
737  *
738  * This must be called before the BPF object is loaded,
739  * otherwise it has no effect and an error is returned.
740  */
741 LIBBPF_API int
742 bpf_program__set_expected_attach_type(struct bpf_program *prog,
743 				      enum bpf_attach_type type);
744 
745 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
746 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
747 
748 /* Per-program log level and log buffer getters/setters.
749  * See bpf_object_open_opts comments regarding log_level and log_buf
750  * interactions.
751  */
752 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
753 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
754 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
755 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
756 
757 /**
758  * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
759  * for supported BPF program types:
760  *   - BTF-aware raw tracepoints (tp_btf);
761  *   - fentry/fexit/fmod_ret;
762  *   - lsm;
763  *   - freplace.
764  * @param prog BPF program to set the attach type for
765  * @param type attach type to set the BPF map to have
766  * @return error code; or 0 if no error occurred.
767  */
768 LIBBPF_API int
769 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
770 			       const char *attach_func_name);
771 
772 /**
773  * @brief **bpf_object__find_map_by_name()** returns BPF map of
774  * the given name, if it exists within the passed BPF object
775  * @param obj BPF object
776  * @param name name of the BPF map
777  * @return BPF map instance, if such map exists within the BPF object;
778  * or NULL otherwise.
779  */
780 LIBBPF_API struct bpf_map *
781 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
782 
783 LIBBPF_API int
784 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
785 
786 LIBBPF_API struct bpf_map *
787 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
788 
789 #define bpf_object__for_each_map(pos, obj)		\
790 	for ((pos) = bpf_object__next_map((obj), NULL);	\
791 	     (pos) != NULL;				\
792 	     (pos) = bpf_object__next_map((obj), (pos)))
793 #define bpf_map__for_each bpf_object__for_each_map
794 
795 LIBBPF_API struct bpf_map *
796 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
797 
798 /**
799  * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
800  * BPF map during BPF object load phase.
801  * @param map the BPF map instance
802  * @param autocreate whether to create BPF map during BPF object load
803  * @return 0 on success; -EBUSY if BPF object was already loaded
804  *
805  * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
806  * BPF map. By default, libbpf will attempt to create every single BPF map
807  * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
808  * and fill in map FD in BPF instructions.
809  *
810  * This API allows to opt-out of this process for specific map instance. This
811  * can be useful if host kernel doesn't support such BPF map type or used
812  * combination of flags and user application wants to avoid creating such
813  * a map in the first place. User is still responsible to make sure that their
814  * BPF-side code that expects to use such missing BPF map is recognized by BPF
815  * verifier as dead code, otherwise BPF verifier will reject such BPF program.
816  */
817 LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
818 LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
819 
820 /**
821  * @brief **bpf_map__fd()** gets the file descriptor of the passed
822  * BPF map
823  * @param map the BPF map instance
824  * @return the file descriptor; or -EINVAL in case of an error
825  */
826 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
827 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
828 /* get map name */
829 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
830 /* get/set map type */
831 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
832 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
833 /* get/set map size (max_entries) */
834 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
835 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
836 /* get/set map flags */
837 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
838 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
839 /* get/set map NUMA node */
840 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
841 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
842 /* get/set map key size */
843 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
844 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
845 /* get/set map value size */
846 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
847 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
848 /* get map key/value BTF type IDs */
849 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
850 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
851 /* get/set map if_index */
852 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
853 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
854 /* get/set map map_extra flags */
855 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
856 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
857 
858 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
859 					  const void *data, size_t size);
860 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
861 
862 /**
863  * @brief **bpf_map__is_internal()** tells the caller whether or not the
864  * passed map is a special map created by libbpf automatically for things like
865  * global variables, __ksym externs, Kconfig values, etc
866  * @param map the bpf_map
867  * @return true, if the map is an internal map; false, otherwise
868  */
869 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
870 
871 /**
872  * @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the
873  * BPF map should be pinned. This does not actually create the 'pin'.
874  * @param map The bpf_map
875  * @param path The path
876  * @return 0, on success; negative error, otherwise
877  */
878 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
879 
880 /**
881  * @brief **bpf_map__pin_path()** gets the path attribute that tells where the
882  * BPF map should be pinned.
883  * @param map The bpf_map
884  * @return The path string; which can be NULL
885  */
886 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
887 
888 /**
889  * @brief **bpf_map__is_pinned()** tells the caller whether or not the
890  * passed map has been pinned via a 'pin' file.
891  * @param map The bpf_map
892  * @return true, if the map is pinned; false, otherwise
893  */
894 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
895 
896 /**
897  * @brief **bpf_map__pin()** creates a file that serves as a 'pin'
898  * for the BPF map. This increments the reference count on the
899  * BPF map which will keep the BPF map loaded even after the
900  * userspace process which loaded it has exited.
901  * @param map The bpf_map to pin
902  * @param path A file path for the 'pin'
903  * @return 0, on success; negative error, otherwise
904  *
905  * If `path` is NULL the maps `pin_path` attribute will be used. If this is
906  * also NULL, an error will be returned and the map will not be pinned.
907  */
908 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
909 
910 /**
911  * @brief **bpf_map__unpin()** removes the file that serves as a
912  * 'pin' for the BPF map.
913  * @param map The bpf_map to unpin
914  * @param path A file path for the 'pin'
915  * @return 0, on success; negative error, otherwise
916  *
917  * The `path` parameter can be NULL, in which case the `pin_path`
918  * map attribute is unpinned. If both the `path` parameter and
919  * `pin_path` map attribute are set, they must be equal.
920  */
921 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
922 
923 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
924 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
925 
926 /**
927  * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
928  * corresponding to provided key.
929  * @param map BPF map to lookup element in
930  * @param key pointer to memory containing bytes of the key used for lookup
931  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
932  * @param value pointer to memory in which looked up value will be stored
933  * @param value_sz size in byte of value data memory; it has to match BPF map
934  * definition's **value_size**. For per-CPU BPF maps value size has to be
935  * a product of BPF map value size and number of possible CPUs in the system
936  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
937  * per-CPU values value size has to be aligned up to closest 8 bytes for
938  * alignment reasons, so expected size is: `round_up(value_size, 8)
939  * * libbpf_num_possible_cpus()`.
940  * @flags extra flags passed to kernel for this operation
941  * @return 0, on success; negative error, otherwise
942  *
943  * **bpf_map__lookup_elem()** is high-level equivalent of
944  * **bpf_map_lookup_elem()** API with added check for key and value size.
945  */
946 LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
947 				    const void *key, size_t key_sz,
948 				    void *value, size_t value_sz, __u64 flags);
949 
950 /**
951  * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
952  * map that corresponds to provided key.
953  * @param map BPF map to insert to or update element in
954  * @param key pointer to memory containing bytes of the key
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 containing bytes of the value
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__update_elem()** is high-level equivalent of
968  * **bpf_map_update_elem()** API with added check for key and value size.
969  */
970 LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
971 				    const void *key, size_t key_sz,
972 				    const void *value, size_t value_sz, __u64 flags);
973 
974 /**
975  * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
976  * corresponds to provided key.
977  * @param map BPF map to delete element from
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  * @flags extra flags passed to kernel for this operation
981  * @return 0, on success; negative error, otherwise
982  *
983  * **bpf_map__delete_elem()** is high-level equivalent of
984  * **bpf_map_delete_elem()** API with added check for key size.
985  */
986 LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
987 				    const void *key, size_t key_sz, __u64 flags);
988 
989 /**
990  * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
991  * corresponding to provided key and atomically delete it afterwards.
992  * @param map BPF map to lookup element in
993  * @param key pointer to memory containing bytes of the key used for lookup
994  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
995  * @param value pointer to memory in which looked up value will be stored
996  * @param value_sz size in byte of value data memory; it has to match BPF map
997  * definition's **value_size**. For per-CPU BPF maps value size has to be
998  * a product of BPF map value size and number of possible CPUs in the system
999  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1000  * per-CPU values value size has to be aligned up to closest 8 bytes for
1001  * alignment reasons, so expected size is: `round_up(value_size, 8)
1002  * * libbpf_num_possible_cpus()`.
1003  * @flags extra flags passed to kernel for this operation
1004  * @return 0, on success; negative error, otherwise
1005  *
1006  * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
1007  * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
1008  */
1009 LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
1010 					       const void *key, size_t key_sz,
1011 					       void *value, size_t value_sz, __u64 flags);
1012 
1013 /**
1014  * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
1015  * fetching next key that follows current key.
1016  * @param map BPF map to fetch next key from
1017  * @param cur_key pointer to memory containing bytes of current key or NULL to
1018  * fetch the first key
1019  * @param next_key pointer to memory to write next key into
1020  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1021  * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
1022  * negative error, otherwise
1023  *
1024  * **bpf_map__get_next_key()** is high-level equivalent of
1025  * **bpf_map_get_next_key()** API with added check for key size.
1026  */
1027 LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
1028 				     const void *cur_key, void *next_key, size_t key_sz);
1029 
1030 struct bpf_xdp_set_link_opts {
1031 	size_t sz;
1032 	int old_fd;
1033 	size_t :0;
1034 };
1035 #define bpf_xdp_set_link_opts__last_field old_fd
1036 
1037 struct bpf_xdp_attach_opts {
1038 	size_t sz;
1039 	int old_prog_fd;
1040 	size_t :0;
1041 };
1042 #define bpf_xdp_attach_opts__last_field old_prog_fd
1043 
1044 struct bpf_xdp_query_opts {
1045 	size_t sz;
1046 	__u32 prog_id;		/* output */
1047 	__u32 drv_prog_id;	/* output */
1048 	__u32 hw_prog_id;	/* output */
1049 	__u32 skb_prog_id;	/* output */
1050 	__u8 attach_mode;	/* output */
1051 	size_t :0;
1052 };
1053 #define bpf_xdp_query_opts__last_field attach_mode
1054 
1055 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
1056 			      const struct bpf_xdp_attach_opts *opts);
1057 LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
1058 			      const struct bpf_xdp_attach_opts *opts);
1059 LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
1060 LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
1061 
1062 /* TC related API */
1063 enum bpf_tc_attach_point {
1064 	BPF_TC_INGRESS = 1 << 0,
1065 	BPF_TC_EGRESS  = 1 << 1,
1066 	BPF_TC_CUSTOM  = 1 << 2,
1067 };
1068 
1069 #define BPF_TC_PARENT(a, b) 	\
1070 	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
1071 
1072 enum bpf_tc_flags {
1073 	BPF_TC_F_REPLACE = 1 << 0,
1074 };
1075 
1076 struct bpf_tc_hook {
1077 	size_t sz;
1078 	int ifindex;
1079 	enum bpf_tc_attach_point attach_point;
1080 	__u32 parent;
1081 	size_t :0;
1082 };
1083 #define bpf_tc_hook__last_field parent
1084 
1085 struct bpf_tc_opts {
1086 	size_t sz;
1087 	int prog_fd;
1088 	__u32 flags;
1089 	__u32 prog_id;
1090 	__u32 handle;
1091 	__u32 priority;
1092 	size_t :0;
1093 };
1094 #define bpf_tc_opts__last_field priority
1095 
1096 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
1097 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
1098 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
1099 			     struct bpf_tc_opts *opts);
1100 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
1101 			     const struct bpf_tc_opts *opts);
1102 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
1103 			    struct bpf_tc_opts *opts);
1104 
1105 /* Ring buffer APIs */
1106 struct ring_buffer;
1107 struct user_ring_buffer;
1108 
1109 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
1110 
1111 struct ring_buffer_opts {
1112 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1113 };
1114 
1115 #define ring_buffer_opts__last_field sz
1116 
1117 LIBBPF_API struct ring_buffer *
1118 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
1119 		 const struct ring_buffer_opts *opts);
1120 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
1121 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
1122 				ring_buffer_sample_fn sample_cb, void *ctx);
1123 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
1124 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
1125 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
1126 
1127 struct user_ring_buffer_opts {
1128 	size_t sz; /* size of this struct, for forward/backward compatibility */
1129 };
1130 
1131 #define user_ring_buffer_opts__last_field sz
1132 
1133 /**
1134  * @brief **user_ring_buffer__new()** creates a new instance of a user ring
1135  * buffer.
1136  *
1137  * @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.
1138  * @param opts Options for how the ring buffer should be created.
1139  * @return A user ring buffer on success; NULL and errno being set on a
1140  * failure.
1141  */
1142 LIBBPF_API struct user_ring_buffer *
1143 user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);
1144 
1145 /**
1146  * @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the
1147  * user ring buffer.
1148  * @param rb A pointer to a user ring buffer.
1149  * @param size The size of the sample, in bytes.
1150  * @return A pointer to an 8-byte aligned reserved region of the user ring
1151  * buffer; NULL, and errno being set if a sample could not be reserved.
1152  *
1153  * This function is *not* thread safe, and callers must synchronize accessing
1154  * this function if there are multiple producers.  If a size is requested that
1155  * is larger than the size of the entire ring buffer, errno will be set to
1156  * E2BIG and NULL is returned. If the ring buffer could accommodate the size,
1157  * but currently does not have enough space, errno is set to ENOSPC and NULL is
1158  * returned.
1159  *
1160  * After initializing the sample, callers must invoke
1161  * **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,
1162  * the sample must be freed with **user_ring_buffer__discard()**.
1163  */
1164 LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
1165 
1166 /**
1167  * @brief **user_ring_buffer__reserve_blocking()** reserves a record in the
1168  * ring buffer, possibly blocking for up to @timeout_ms until a sample becomes
1169  * available.
1170  * @param rb The user ring buffer.
1171  * @param size The size of the sample, in bytes.
1172  * @param timeout_ms The amount of time, in milliseconds, for which the caller
1173  * should block when waiting for a sample. -1 causes the caller to block
1174  * indefinitely.
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
1179  * accessing this function if there are multiple producers
1180  *
1181  * If **timeout_ms** is -1, the function will block indefinitely until a sample
1182  * becomes available. Otherwise, **timeout_ms** must be non-negative, or errno
1183  * is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking
1184  * will occur and the function will return immediately after attempting to
1185  * reserve a sample.
1186  *
1187  * If **size** is larger than the size of the entire ring buffer, errno is set
1188  * to E2BIG and NULL is returned. If the ring buffer could accommodate
1189  * **size**, but currently does not have enough space, the caller will block
1190  * until at most **timeout_ms** has elapsed. If insufficient space is available
1191  * at that time, errno is set to ENOSPC, and NULL is returned.
1192  *
1193  * The kernel guarantees that it will wake up this thread to check if
1194  * sufficient space is available in the ring buffer at least once per
1195  * invocation of the **bpf_ringbuf_drain()** helper function, provided that at
1196  * least one sample is consumed, and the BPF program did not invoke the
1197  * function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the
1198  * kernel does not guarantee this. If the helper function is invoked with
1199  * BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is
1200  * consumed.
1201  *
1202  * When a sample of size **size** is found within **timeout_ms**, a pointer to
1203  * the sample is returned. After initializing the sample, callers must invoke
1204  * **user_ring_buffer__submit()** to post the sample to the ring buffer.
1205  * Otherwise, the sample must be freed with **user_ring_buffer__discard()**.
1206  */
1207 LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
1208 						    __u32 size,
1209 						    int timeout_ms);
1210 
1211 /**
1212  * @brief **user_ring_buffer__submit()** submits a previously reserved sample
1213  * into the ring buffer.
1214  * @param rb The user ring buffer.
1215  * @param sample A reserved sample.
1216  *
1217  * It is not necessary to synchronize amongst multiple producers when invoking
1218  * this function.
1219  */
1220 LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);
1221 
1222 /**
1223  * @brief **user_ring_buffer__discard()** discards a previously reserved sample.
1224  * @param rb The user ring buffer.
1225  * @param sample A reserved sample.
1226  *
1227  * It is not necessary to synchronize amongst multiple producers when invoking
1228  * this function.
1229  */
1230 LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);
1231 
1232 /**
1233  * @brief **user_ring_buffer__free()** frees a ring buffer that was previously
1234  * created with **user_ring_buffer__new()**.
1235  * @param rb The user ring buffer being freed.
1236  */
1237 LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);
1238 
1239 /* Perf buffer APIs */
1240 struct perf_buffer;
1241 
1242 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
1243 				      void *data, __u32 size);
1244 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
1245 
1246 /* common use perf buffer options */
1247 struct perf_buffer_opts {
1248 	size_t sz;
1249 };
1250 #define perf_buffer_opts__last_field sz
1251 
1252 /**
1253  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1254  * BPF_PERF_EVENT_ARRAY map
1255  * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1256  * code to send data over to user-space
1257  * @param page_cnt number of memory pages allocated for each per-CPU buffer
1258  * @param sample_cb function called on each received data record
1259  * @param lost_cb function called when record loss has occurred
1260  * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1261  * @return a new instance of struct perf_buffer on success, NULL on error with
1262  * *errno* containing an error code
1263  */
1264 LIBBPF_API struct perf_buffer *
1265 perf_buffer__new(int map_fd, size_t page_cnt,
1266 		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1267 		 const struct perf_buffer_opts *opts);
1268 
1269 enum bpf_perf_event_ret {
1270 	LIBBPF_PERF_EVENT_DONE	= 0,
1271 	LIBBPF_PERF_EVENT_ERROR	= -1,
1272 	LIBBPF_PERF_EVENT_CONT	= -2,
1273 };
1274 
1275 struct perf_event_header;
1276 
1277 typedef enum bpf_perf_event_ret
1278 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1279 
1280 /* raw perf buffer options, giving most power and control */
1281 struct perf_buffer_raw_opts {
1282 	size_t sz;
1283 	long :0;
1284 	long :0;
1285 	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1286 	 * max_entries of given PERF_EVENT_ARRAY map)
1287 	 */
1288 	int cpu_cnt;
1289 	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1290 	int *cpus;
1291 	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1292 	int *map_keys;
1293 };
1294 #define perf_buffer_raw_opts__last_field map_keys
1295 
1296 struct perf_event_attr;
1297 
1298 LIBBPF_API struct perf_buffer *
1299 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1300 		     perf_buffer_event_fn event_cb, void *ctx,
1301 		     const struct perf_buffer_raw_opts *opts);
1302 
1303 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1304 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1305 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1306 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1307 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1308 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1309 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1310 /**
1311  * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying
1312  * memory region of the ring buffer.
1313  * This ring buffer can be used to implement a custom events consumer.
1314  * The ring buffer starts with the *struct perf_event_mmap_page*, which
1315  * holds the ring buffer managment fields, when accessing the header
1316  * structure it's important to be SMP aware.
1317  * You can refer to *perf_event_read_simple* for a simple example.
1318  * @param pb the perf buffer structure
1319  * @param buf_idx the buffer index to retreive
1320  * @param buf (out) gets the base pointer of the mmap()'ed memory
1321  * @param buf_size (out) gets the size of the mmap()'ed region
1322  * @return 0 on success, negative error code for failure
1323  */
1324 LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,
1325 				   size_t *buf_size);
1326 
1327 struct bpf_prog_linfo;
1328 struct bpf_prog_info;
1329 
1330 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1331 LIBBPF_API struct bpf_prog_linfo *
1332 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1333 LIBBPF_API const struct bpf_line_info *
1334 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1335 				__u64 addr, __u32 func_idx, __u32 nr_skip);
1336 LIBBPF_API const struct bpf_line_info *
1337 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1338 		      __u32 insn_off, __u32 nr_skip);
1339 
1340 /*
1341  * Probe for supported system features
1342  *
1343  * Note that running many of these probes in a short amount of time can cause
1344  * the kernel to reach the maximal size of lockable memory allowed for the
1345  * user, causing subsequent probes to fail. In this case, the caller may want
1346  * to adjust that limit with setrlimit().
1347  */
1348 
1349 /**
1350  * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1351  * BPF programs of a given type.
1352  * @param prog_type BPF program type to detect kernel support for
1353  * @param opts reserved for future extensibility, should be NULL
1354  * @return 1, if given program type is supported; 0, if given program type is
1355  * not supported; negative error code if feature detection failed or can't be
1356  * performed
1357  *
1358  * Make sure the process has required set of CAP_* permissions (or runs as
1359  * root) when performing feature checking.
1360  */
1361 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1362 /**
1363  * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1364  * BPF maps of a given type.
1365  * @param map_type BPF map type to detect kernel support for
1366  * @param opts reserved for future extensibility, should be NULL
1367  * @return 1, if given map type is supported; 0, if given map type is
1368  * not supported; negative error code if feature detection failed or can't be
1369  * performed
1370  *
1371  * Make sure the process has required set of CAP_* permissions (or runs as
1372  * root) when performing feature checking.
1373  */
1374 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1375 /**
1376  * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1377  * use of a given BPF helper from specified BPF program type.
1378  * @param prog_type BPF program type used to check the support of BPF helper
1379  * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1380  * @param opts reserved for future extensibility, should be NULL
1381  * @return 1, if given combination of program type and helper is supported; 0,
1382  * if the combination is not supported; negative error code if feature
1383  * detection for provided input arguments failed or can't be 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_helper(enum bpf_prog_type prog_type,
1389 				       enum bpf_func_id helper_id, const void *opts);
1390 
1391 /**
1392  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1393  * number of possible CPUs that the host kernel supports and expects.
1394  * @return number of possible CPUs; or error code on failure
1395  *
1396  * Example usage:
1397  *
1398  *     int ncpus = libbpf_num_possible_cpus();
1399  *     if (ncpus < 0) {
1400  *          // error handling
1401  *     }
1402  *     long values[ncpus];
1403  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1404  */
1405 LIBBPF_API int libbpf_num_possible_cpus(void);
1406 
1407 struct bpf_map_skeleton {
1408 	const char *name;
1409 	struct bpf_map **map;
1410 	void **mmaped;
1411 };
1412 
1413 struct bpf_prog_skeleton {
1414 	const char *name;
1415 	struct bpf_program **prog;
1416 	struct bpf_link **link;
1417 };
1418 
1419 struct bpf_object_skeleton {
1420 	size_t sz; /* size of this struct, for forward/backward compatibility */
1421 
1422 	const char *name;
1423 	const void *data;
1424 	size_t data_sz;
1425 
1426 	struct bpf_object **obj;
1427 
1428 	int map_cnt;
1429 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1430 	struct bpf_map_skeleton *maps;
1431 
1432 	int prog_cnt;
1433 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1434 	struct bpf_prog_skeleton *progs;
1435 };
1436 
1437 LIBBPF_API int
1438 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1439 			  const struct bpf_object_open_opts *opts);
1440 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1441 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1442 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1443 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1444 
1445 struct bpf_var_skeleton {
1446 	const char *name;
1447 	struct bpf_map **map;
1448 	void **addr;
1449 };
1450 
1451 struct bpf_object_subskeleton {
1452 	size_t sz; /* size of this struct, for forward/backward compatibility */
1453 
1454 	const struct bpf_object *obj;
1455 
1456 	int map_cnt;
1457 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1458 	struct bpf_map_skeleton *maps;
1459 
1460 	int prog_cnt;
1461 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1462 	struct bpf_prog_skeleton *progs;
1463 
1464 	int var_cnt;
1465 	int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1466 	struct bpf_var_skeleton *vars;
1467 };
1468 
1469 LIBBPF_API int
1470 bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1471 LIBBPF_API void
1472 bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1473 
1474 struct gen_loader_opts {
1475 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1476 	const char *data;
1477 	const char *insns;
1478 	__u32 data_sz;
1479 	__u32 insns_sz;
1480 };
1481 
1482 #define gen_loader_opts__last_field insns_sz
1483 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1484 				      struct gen_loader_opts *opts);
1485 
1486 enum libbpf_tristate {
1487 	TRI_NO = 0,
1488 	TRI_YES = 1,
1489 	TRI_MODULE = 2,
1490 };
1491 
1492 struct bpf_linker_opts {
1493 	/* size of this struct, for forward/backward compatiblity */
1494 	size_t sz;
1495 };
1496 #define bpf_linker_opts__last_field sz
1497 
1498 struct bpf_linker_file_opts {
1499 	/* size of this struct, for forward/backward compatiblity */
1500 	size_t sz;
1501 };
1502 #define bpf_linker_file_opts__last_field sz
1503 
1504 struct bpf_linker;
1505 
1506 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1507 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1508 				    const char *filename,
1509 				    const struct bpf_linker_file_opts *opts);
1510 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1511 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1512 
1513 /*
1514  * Custom handling of BPF program's SEC() definitions
1515  */
1516 
1517 struct bpf_prog_load_opts; /* defined in bpf.h */
1518 
1519 /* Called during bpf_object__open() for each recognized BPF program. Callback
1520  * can use various bpf_program__set_*() setters to adjust whatever properties
1521  * are necessary.
1522  */
1523 typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1524 
1525 /* Called right before libbpf performs bpf_prog_load() to load BPF program
1526  * into the kernel. Callback can adjust opts as necessary.
1527  */
1528 typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1529 					     struct bpf_prog_load_opts *opts, long cookie);
1530 
1531 /* Called during skeleton attach or through bpf_program__attach(). If
1532  * auto-attach is not supported, callback should return 0 and set link to
1533  * NULL (it's not considered an error during skeleton attach, but it will be
1534  * an error for bpf_program__attach() calls). On error, error should be
1535  * returned directly and link set to NULL. On success, return 0 and set link
1536  * to a valid struct bpf_link.
1537  */
1538 typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1539 				       struct bpf_link **link);
1540 
1541 struct libbpf_prog_handler_opts {
1542 	/* size of this struct, for forward/backward compatiblity */
1543 	size_t sz;
1544 	/* User-provided value that is passed to prog_setup_fn,
1545 	 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1546 	 * register one set of callbacks for multiple SEC() definitions and
1547 	 * still be able to distinguish them, if necessary. For example,
1548 	 * libbpf itself is using this to pass necessary flags (e.g.,
1549 	 * sleepable flag) to a common internal SEC() handler.
1550 	 */
1551 	long cookie;
1552 	/* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1553 	 * Callback is optional, pass NULL if it's not necessary.
1554 	 */
1555 	libbpf_prog_setup_fn_t prog_setup_fn;
1556 	/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1557 	 * Callback is optional, pass NULL if it's not necessary.
1558 	 */
1559 	libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1560 	/* BPF program attach callback (see libbpf_prog_attach_fn_t).
1561 	 * Callback is optional, pass NULL if it's not necessary.
1562 	 */
1563 	libbpf_prog_attach_fn_t prog_attach_fn;
1564 };
1565 #define libbpf_prog_handler_opts__last_field prog_attach_fn
1566 
1567 /**
1568  * @brief **libbpf_register_prog_handler()** registers a custom BPF program
1569  * SEC() handler.
1570  * @param sec section prefix for which custom handler is registered
1571  * @param prog_type BPF program type associated with specified section
1572  * @param exp_attach_type Expected BPF attach type associated with specified section
1573  * @param opts optional cookie, callbacks, and other extra options
1574  * @return Non-negative handler ID is returned on success. This handler ID has
1575  * to be passed to *libbpf_unregister_prog_handler()* to unregister such
1576  * custom handler. Negative error code is returned on error.
1577  *
1578  * *sec* defines which SEC() definitions are handled by this custom handler
1579  * registration. *sec* can have few different forms:
1580  *   - if *sec* is just a plain string (e.g., "abc"), it will match only
1581  *   SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1582  *   in an error;
1583  *   - if *sec* is of the form "abc/", proper SEC() form is
1584  *   SEC("abc/something"), where acceptable "something" should be checked by
1585  *   *prog_init_fn* callback, if there are additional restrictions;
1586  *   - if *sec* is of the form "abc+", it will successfully match both
1587  *   SEC("abc") and SEC("abc/whatever") forms;
1588  *   - if *sec* is NULL, custom handler is registered for any BPF program that
1589  *   doesn't match any of the registered (custom or libbpf's own) SEC()
1590  *   handlers. There could be only one such generic custom handler registered
1591  *   at any given time.
1592  *
1593  * All custom handlers (except the one with *sec* == NULL) are processed
1594  * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1595  * SEC() handlers by registering custom ones for the same section prefix
1596  * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1597  * handler).
1598  *
1599  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1600  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1601  * to ensure synchronization if there is a risk of running this API from
1602  * multiple threads simultaneously.
1603  */
1604 LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1605 					    enum bpf_prog_type prog_type,
1606 					    enum bpf_attach_type exp_attach_type,
1607 					    const struct libbpf_prog_handler_opts *opts);
1608 /**
1609  * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1610  * custom BPF program SEC() handler.
1611  * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1612  * after successful registration
1613  * @return 0 on success, negative error code if handler isn't found
1614  *
1615  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1616  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1617  * to ensure synchronization if there is a risk of running this API from
1618  * multiple threads simultaneously.
1619  */
1620 LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1621 
1622 #ifdef __cplusplus
1623 } /* extern "C" */
1624 #endif
1625 
1626 #endif /* __LIBBPF_LIBBPF_H */
1627