xref: /openbmc/linux/tools/lib/bpf/libbpf.h (revision c0c45238)
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 enum libbpf_print_level {
55         LIBBPF_WARN,
56         LIBBPF_INFO,
57         LIBBPF_DEBUG,
58 };
59 
60 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
61 				 const char *, va_list ap);
62 
63 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
64 
65 /* Hide internal to user */
66 struct bpf_object;
67 
68 struct bpf_object_open_attr {
69 	const char *file;
70 	enum bpf_prog_type prog_type;
71 };
72 
73 struct bpf_object_open_opts {
74 	/* size of this struct, for forward/backward compatiblity */
75 	size_t sz;
76 	/* object name override, if provided:
77 	 * - for object open from file, this will override setting object
78 	 *   name from file path's base name;
79 	 * - for object open from memory buffer, this will specify an object
80 	 *   name and will override default "<addr>-<buf-size>" name;
81 	 */
82 	const char *object_name;
83 	/* parse map definitions non-strictly, allowing extra attributes/data */
84 	bool relaxed_maps;
85 	/* DEPRECATED: handle CO-RE relocations non-strictly, allowing failures.
86 	 * Value is ignored. Relocations always are processed non-strictly.
87 	 * Non-relocatable instructions are replaced with invalid ones to
88 	 * prevent accidental errors.
89 	 * */
90 	LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect")
91 	bool relaxed_core_relocs;
92 	/* maps that set the 'pinning' attribute in their definition will have
93 	 * their pin_path attribute set to a file in this directory, and be
94 	 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
95 	 */
96 	const char *pin_root_path;
97 
98 	LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__set_attach_target() on each individual bpf_program")
99 	__u32 attach_prog_fd;
100 	/* Additional kernel config content that augments and overrides
101 	 * system Kconfig for CONFIG_xxx externs.
102 	 */
103 	const char *kconfig;
104 	/* Path to the custom BTF to be used for BPF CO-RE relocations.
105 	 * This custom BTF completely replaces the use of vmlinux BTF
106 	 * for the purpose of CO-RE relocations.
107 	 * NOTE: any other BPF feature (e.g., fentry/fexit programs,
108 	 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
109 	 */
110 	const char *btf_custom_path;
111 	/* Pointer to a buffer for storing kernel logs for applicable BPF
112 	 * commands. Valid kernel_log_size has to be specified as well and are
113 	 * passed-through to bpf() syscall. Keep in mind that kernel might
114 	 * fail operation with -ENOSPC error if provided buffer is too small
115 	 * to contain entire log output.
116 	 * See the comment below for kernel_log_level for interaction between
117 	 * log_buf and log_level settings.
118 	 *
119 	 * If specified, this log buffer will be passed for:
120 	 *   - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden
121 	 *     with bpf_program__set_log() on per-program level, to get
122 	 *     BPF verifier log output.
123 	 *   - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
124 	 *     BTF sanity checking log.
125 	 *
126 	 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
127 	 * previous contents, so if you need more fine-grained control, set
128 	 * per-program buffer with bpf_program__set_log_buf() to preserve each
129 	 * individual program's verification log. Keep using kernel_log_buf
130 	 * for BTF verification log, if necessary.
131 	 */
132 	char *kernel_log_buf;
133 	size_t kernel_log_size;
134 	/*
135 	 * Log level can be set independently from log buffer. Log_level=0
136 	 * means that libbpf will attempt loading BTF or program without any
137 	 * logging requested, but will retry with either its own or custom log
138 	 * buffer, if provided, and log_level=1 on any error.
139 	 * And vice versa, setting log_level>0 will request BTF or prog
140 	 * loading with verbose log from the first attempt (and as such also
141 	 * for successfully loaded BTF or program), and the actual log buffer
142 	 * could be either libbpf's own auto-allocated log buffer, if
143 	 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
144 	 * If user didn't provide custom log buffer, libbpf will emit captured
145 	 * logs through its print callback.
146 	 */
147 	__u32 kernel_log_level;
148 
149 	size_t :0;
150 };
151 #define bpf_object_open_opts__last_field kernel_log_level
152 
153 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
154 
155 /**
156  * @brief **bpf_object__open_file()** creates a bpf_object by opening
157  * the BPF ELF object file pointed to by the passed path and loading it
158  * into memory.
159  * @param path BPF object file path
160  * @param opts options for how to load the bpf object, this parameter is
161  * optional and can be set to NULL
162  * @return pointer to the new bpf_object; or NULL is returned on error,
163  * error code is stored in errno
164  */
165 LIBBPF_API struct bpf_object *
166 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
167 
168 /**
169  * @brief **bpf_object__open_mem()** creates a bpf_object by reading
170  * the BPF objects raw bytes from a memory buffer containing a valid
171  * BPF ELF object file.
172  * @param obj_buf pointer to the buffer containing ELF file bytes
173  * @param obj_buf_sz number of bytes in the buffer
174  * @param opts options for how to load the bpf object
175  * @return pointer to the new bpf_object; or NULL is returned on error,
176  * error code is stored in errno
177  */
178 LIBBPF_API struct bpf_object *
179 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
180 		     const struct bpf_object_open_opts *opts);
181 
182 /* deprecated bpf_object__open variants */
183 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__open_mem() instead")
184 LIBBPF_API struct bpf_object *
185 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
186 			const char *name);
187 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open_file() instead")
188 LIBBPF_API struct bpf_object *
189 bpf_object__open_xattr(struct bpf_object_open_attr *attr);
190 
191 enum libbpf_pin_type {
192 	LIBBPF_PIN_NONE,
193 	/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
194 	LIBBPF_PIN_BY_NAME,
195 };
196 
197 /* pin_maps and unpin_maps can both be called with a NULL path, in which case
198  * they will use the pin_path attribute of each map (and ignore all maps that
199  * don't have a pin_path set).
200  */
201 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
202 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
203 				      const char *path);
204 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
205 					const char *path);
206 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
207 					  const char *path);
208 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
209 LIBBPF_API void bpf_object__close(struct bpf_object *object);
210 
211 struct bpf_object_load_attr {
212 	struct bpf_object *obj;
213 	int log_level;
214 	const char *target_btf_path;
215 };
216 
217 /* Load/unload object into/from kernel */
218 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
219 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__load() instead")
220 LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
221 LIBBPF_DEPRECATED_SINCE(0, 6, "bpf_object__unload() is deprecated, use bpf_object__close() instead")
222 LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
223 
224 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
225 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
226 LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
227 
228 struct btf;
229 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
230 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
231 
232 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__find_program_by_name() instead")
233 LIBBPF_API struct bpf_program *
234 bpf_object__find_program_by_title(const struct bpf_object *obj,
235 				  const char *title);
236 LIBBPF_API struct bpf_program *
237 bpf_object__find_program_by_name(const struct bpf_object *obj,
238 				 const char *name);
239 
240 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "track bpf_objects in application code instead")
241 struct bpf_object *bpf_object__next(struct bpf_object *prev);
242 #define bpf_object__for_each_safe(pos, tmp)			\
243 	for ((pos) = bpf_object__next(NULL),		\
244 		(tmp) = bpf_object__next(pos);		\
245 	     (pos) != NULL;				\
246 	     (pos) = (tmp), (tmp) = bpf_object__next(tmp))
247 
248 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
249 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
250 LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
251 				    bpf_object_clear_priv_t clear_priv);
252 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
253 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
254 
255 LIBBPF_API int
256 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
257 			 enum bpf_attach_type *expected_attach_type);
258 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
259 					  enum bpf_attach_type *attach_type);
260 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
261 					  enum bpf_attach_type attach_type);
262 
263 /* Accessors of bpf_program */
264 struct bpf_program;
265 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_program() instead")
266 struct bpf_program *bpf_program__next(struct bpf_program *prog,
267 				      const struct bpf_object *obj);
268 LIBBPF_API struct bpf_program *
269 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
270 
271 #define bpf_object__for_each_program(pos, obj)			\
272 	for ((pos) = bpf_object__next_program((obj), NULL);	\
273 	     (pos) != NULL;					\
274 	     (pos) = bpf_object__next_program((obj), (pos)))
275 
276 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_program() instead")
277 struct bpf_program *bpf_program__prev(struct bpf_program *prog,
278 				      const struct bpf_object *obj);
279 LIBBPF_API struct bpf_program *
280 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
281 
282 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
283 
284 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
285 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
286 				     bpf_program_clear_priv_t clear_priv);
287 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
288 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
289 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
290 					 __u32 ifindex);
291 
292 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
293 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
294 LIBBPF_API LIBBPF_DEPRECATED("BPF program title is confusing term; please use bpf_program__section_name() instead")
295 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy);
296 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
297 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
298 
299 /* returns program size in bytes */
300 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insn_cnt() instead")
301 LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
302 
303 struct bpf_insn;
304 
305 /**
306  * @brief **bpf_program__insns()** gives read-only access to BPF program's
307  * underlying BPF instructions.
308  * @param prog BPF program for which to return instructions
309  * @return a pointer to an array of BPF instructions that belong to the
310  * specified BPF program
311  *
312  * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
313  * pointed to can be fetched using **bpf_program__insn_cnt()** API.
314  *
315  * Keep in mind, libbpf can modify and append/delete BPF program's
316  * instructions as it processes BPF object file and prepares everything for
317  * uploading into the kernel. So depending on the point in BPF object
318  * lifetime, **bpf_program__insns()** can return different sets of
319  * instructions. As an example, during BPF object load phase BPF program
320  * instructions will be CO-RE-relocated, BPF subprograms instructions will be
321  * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
322  * returned before **bpf_object__load()** and after it might be quite
323  * different.
324  */
325 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
326 /**
327  * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
328  * that form specified BPF program.
329  * @param prog BPF program for which to return number of BPF instructions
330  *
331  * See **bpf_program__insns()** documentation for notes on how libbpf can
332  * change instructions and their count during different phases of
333  * **bpf_object** lifetime.
334  */
335 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
336 
337 LIBBPF_DEPRECATED_SINCE(0, 6, "use bpf_object__load() instead")
338 LIBBPF_API int bpf_program__load(struct bpf_program *prog, const char *license, __u32 kern_version);
339 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
340 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
341 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
342 					 const char *path,
343 					 int instance);
344 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
345 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
346 					   const char *path,
347 					   int instance);
348 
349 /**
350  * @brief **bpf_program__pin()** pins the BPF program to a file
351  * in the BPF FS specified by a path. This increments the programs
352  * reference count, allowing it to stay loaded after the process
353  * which loaded it has exited.
354  *
355  * @param prog BPF program to pin, must already be loaded
356  * @param path file path in a BPF file system
357  * @return 0, on success; negative error code, otherwise
358  */
359 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
360 
361 /**
362  * @brief **bpf_program__unpin()** unpins the BPF program from a file
363  * in the BPFFS specified by a path. This decrements the programs
364  * reference count.
365  *
366  * The file pinning the BPF program can also be unlinked by a different
367  * process in which case this function will return an error.
368  *
369  * @param prog BPF program to unpin
370  * @param path file path to the pin in a BPF file system
371  * @return 0, on success; negative error code, otherwise
372  */
373 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
374 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
375 
376 struct bpf_link;
377 
378 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
379 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
380 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
381 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
382 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
383 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
384 					struct bpf_program *prog);
385 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
386 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
387 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
388 
389 LIBBPF_API struct bpf_link *
390 bpf_program__attach(const struct bpf_program *prog);
391 
392 struct bpf_perf_event_opts {
393 	/* size of this struct, for forward/backward compatiblity */
394 	size_t sz;
395 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
396 	__u64 bpf_cookie;
397 };
398 #define bpf_perf_event_opts__last_field bpf_cookie
399 
400 LIBBPF_API struct bpf_link *
401 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
402 
403 LIBBPF_API struct bpf_link *
404 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
405 				    const struct bpf_perf_event_opts *opts);
406 
407 struct bpf_kprobe_opts {
408 	/* size of this struct, for forward/backward compatiblity */
409 	size_t sz;
410 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
411 	__u64 bpf_cookie;
412 	/* function's offset to install kprobe to */
413 	size_t offset;
414 	/* kprobe is return probe */
415 	bool retprobe;
416 	size_t :0;
417 };
418 #define bpf_kprobe_opts__last_field retprobe
419 
420 LIBBPF_API struct bpf_link *
421 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
422 			   const char *func_name);
423 LIBBPF_API struct bpf_link *
424 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
425                                 const char *func_name,
426                                 const struct bpf_kprobe_opts *opts);
427 
428 struct bpf_kprobe_multi_opts {
429 	/* size of this struct, for forward/backward compatibility */
430 	size_t sz;
431 	/* array of function symbols to attach */
432 	const char **syms;
433 	/* array of function addresses to attach */
434 	const unsigned long *addrs;
435 	/* array of user-provided values fetchable through bpf_get_attach_cookie */
436 	const __u64 *cookies;
437 	/* number of elements in syms/addrs/cookies arrays */
438 	size_t cnt;
439 	/* create return kprobes */
440 	bool retprobe;
441 	size_t :0;
442 };
443 
444 #define bpf_kprobe_multi_opts__last_field retprobe
445 
446 LIBBPF_API struct bpf_link *
447 bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
448 				      const char *pattern,
449 				      const struct bpf_kprobe_multi_opts *opts);
450 
451 struct bpf_uprobe_opts {
452 	/* size of this struct, for forward/backward compatiblity */
453 	size_t sz;
454 	/* offset of kernel reference counted USDT semaphore, added in
455 	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
456 	 */
457 	size_t ref_ctr_offset;
458 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
459 	__u64 bpf_cookie;
460 	/* uprobe is return probe, invoked at function return time */
461 	bool retprobe;
462 	size_t :0;
463 };
464 #define bpf_uprobe_opts__last_field retprobe
465 
466 /**
467  * @brief **bpf_program__attach_uprobe()** attaches a BPF program
468  * to the userspace function which is found by binary path and
469  * offset. You can optionally specify a particular proccess to attach
470  * to. You can also optionally attach the program to the function
471  * exit instead of entry.
472  *
473  * @param prog BPF program to attach
474  * @param retprobe Attach to function exit
475  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
476  * -1 for all processes
477  * @param binary_path Path to binary that contains the function symbol
478  * @param func_offset Offset within the binary of the function symbol
479  * @return Reference to the newly created BPF link; or NULL is returned on error,
480  * error code is stored in errno
481  */
482 LIBBPF_API struct bpf_link *
483 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
484 			   pid_t pid, const char *binary_path,
485 			   size_t func_offset);
486 
487 /**
488  * @brief **bpf_program__attach_uprobe_opts()** is just like
489  * bpf_program__attach_uprobe() except with a options struct
490  * for various configurations.
491  *
492  * @param prog BPF program to attach
493  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
494  * -1 for all processes
495  * @param binary_path Path to binary that contains the function symbol
496  * @param func_offset Offset within the binary of the function symbol
497  * @param opts Options for altering program attachment
498  * @return Reference to the newly created BPF link; or NULL is returned on error,
499  * error code is stored in errno
500  */
501 LIBBPF_API struct bpf_link *
502 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
503 				const char *binary_path, size_t func_offset,
504 				const struct bpf_uprobe_opts *opts);
505 
506 struct bpf_tracepoint_opts {
507 	/* size of this struct, for forward/backward compatiblity */
508 	size_t sz;
509 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
510 	__u64 bpf_cookie;
511 };
512 #define bpf_tracepoint_opts__last_field bpf_cookie
513 
514 LIBBPF_API struct bpf_link *
515 bpf_program__attach_tracepoint(const struct bpf_program *prog,
516 			       const char *tp_category,
517 			       const char *tp_name);
518 LIBBPF_API struct bpf_link *
519 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
520 				    const char *tp_category,
521 				    const char *tp_name,
522 				    const struct bpf_tracepoint_opts *opts);
523 
524 LIBBPF_API struct bpf_link *
525 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
526 				   const char *tp_name);
527 LIBBPF_API struct bpf_link *
528 bpf_program__attach_trace(const struct bpf_program *prog);
529 LIBBPF_API struct bpf_link *
530 bpf_program__attach_lsm(const struct bpf_program *prog);
531 LIBBPF_API struct bpf_link *
532 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
533 LIBBPF_API struct bpf_link *
534 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
535 LIBBPF_API struct bpf_link *
536 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
537 LIBBPF_API struct bpf_link *
538 bpf_program__attach_freplace(const struct bpf_program *prog,
539 			     int target_fd, const char *attach_func_name);
540 
541 struct bpf_map;
542 
543 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
544 
545 struct bpf_iter_attach_opts {
546 	size_t sz; /* size of this struct for forward/backward compatibility */
547 	union bpf_iter_link_info *link_info;
548 	__u32 link_info_len;
549 };
550 #define bpf_iter_attach_opts__last_field link_info_len
551 
552 LIBBPF_API struct bpf_link *
553 bpf_program__attach_iter(const struct bpf_program *prog,
554 			 const struct bpf_iter_attach_opts *opts);
555 
556 /*
557  * Libbpf allows callers to adjust BPF programs before being loaded
558  * into kernel. One program in an object file can be transformed into
559  * multiple variants to be attached to different hooks.
560  *
561  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
562  * form an API for this purpose.
563  *
564  * - bpf_program_prep_t:
565  *   Defines a 'preprocessor', which is a caller defined function
566  *   passed to libbpf through bpf_program__set_prep(), and will be
567  *   called before program is loaded. The processor should adjust
568  *   the program one time for each instance according to the instance id
569  *   passed to it.
570  *
571  * - bpf_program__set_prep:
572  *   Attaches a preprocessor to a BPF program. The number of instances
573  *   that should be created is also passed through this function.
574  *
575  * - bpf_program__nth_fd:
576  *   After the program is loaded, get resulting FD of a given instance
577  *   of the BPF program.
578  *
579  * If bpf_program__set_prep() is not used, the program would be loaded
580  * without adjustment during bpf_object__load(). The program has only
581  * one instance. In this case bpf_program__fd(prog) is equal to
582  * bpf_program__nth_fd(prog, 0).
583  */
584 struct bpf_prog_prep_result {
585 	/*
586 	 * If not NULL, load new instruction array.
587 	 * If set to NULL, don't load this instance.
588 	 */
589 	struct bpf_insn *new_insn_ptr;
590 	int new_insn_cnt;
591 
592 	/* If not NULL, result FD is written to it. */
593 	int *pfd;
594 };
595 
596 /*
597  * Parameters of bpf_program_prep_t:
598  *  - prog:	The bpf_program being loaded.
599  *  - n:	Index of instance being generated.
600  *  - insns:	BPF instructions array.
601  *  - insns_cnt:Number of instructions in insns.
602  *  - res:	Output parameter, result of transformation.
603  *
604  * Return value:
605  *  - Zero:	pre-processing success.
606  *  - Non-zero:	pre-processing error, stop loading.
607  */
608 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
609 				  struct bpf_insn *insns, int insns_cnt,
610 				  struct bpf_prog_prep_result *res);
611 
612 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions")
613 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
614 				     bpf_program_prep_t prep);
615 
616 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
617 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
618 
619 /*
620  * Adjust type of BPF program. Default is kprobe.
621  */
622 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
623 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
624 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
625 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
626 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
627 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
628 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
629 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
630 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
631 LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
632 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
633 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
634 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
635 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
636 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
637 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
638 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
639 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
640 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
641 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
642 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
643 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
644 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
645 LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog);
646 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
647 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
648 
649 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
650 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
651 				      enum bpf_prog_type type);
652 
653 LIBBPF_API enum bpf_attach_type
654 bpf_program__expected_attach_type(const struct bpf_program *prog);
655 LIBBPF_API void
656 bpf_program__set_expected_attach_type(struct bpf_program *prog,
657 				      enum bpf_attach_type type);
658 
659 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
660 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
661 
662 /* Per-program log level and log buffer getters/setters.
663  * See bpf_object_open_opts comments regarding log_level and log_buf
664  * interactions.
665  */
666 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
667 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
668 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
669 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
670 
671 LIBBPF_API int
672 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
673 			       const char *attach_func_name);
674 
675 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
676 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
677 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
678 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
679 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
680 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
681 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
682 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
683 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
684 LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
685 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
686 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
687 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
688 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
689 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
690 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
691 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
692 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
693 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
694 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
695 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
696 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
697 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
698 LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog);
699 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
700 LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog);
701 
702 /*
703  * No need for __attribute__((packed)), all members of 'bpf_map_def'
704  * are all aligned.  In addition, using __attribute__((packed))
705  * would trigger a -Wpacked warning message, and lead to an error
706  * if -Werror is set.
707  */
708 struct bpf_map_def {
709 	unsigned int type;
710 	unsigned int key_size;
711 	unsigned int value_size;
712 	unsigned int max_entries;
713 	unsigned int map_flags;
714 };
715 
716 /**
717  * @brief **bpf_object__find_map_by_name()** returns BPF map of
718  * the given name, if it exists within the passed BPF object
719  * @param obj BPF object
720  * @param name name of the BPF map
721  * @return BPF map instance, if such map exists within the BPF object;
722  * or NULL otherwise.
723  */
724 LIBBPF_API struct bpf_map *
725 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
726 
727 LIBBPF_API int
728 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
729 
730 /*
731  * Get bpf_map through the offset of corresponding struct bpf_map_def
732  * in the BPF object file.
733  */
734 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__find_map_by_name() instead")
735 struct bpf_map *
736 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
737 
738 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead")
739 struct bpf_map *bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
740 LIBBPF_API struct bpf_map *
741 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
742 
743 #define bpf_object__for_each_map(pos, obj)		\
744 	for ((pos) = bpf_object__next_map((obj), NULL);	\
745 	     (pos) != NULL;				\
746 	     (pos) = bpf_object__next_map((obj), (pos)))
747 #define bpf_map__for_each bpf_object__for_each_map
748 
749 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_map() instead")
750 struct bpf_map *bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
751 LIBBPF_API struct bpf_map *
752 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
753 
754 /**
755  * @brief **bpf_map__fd()** gets the file descriptor of the passed
756  * BPF map
757  * @param map the BPF map instance
758  * @return the file descriptor; or -EINVAL in case of an error
759  */
760 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
761 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
762 /* get map definition */
763 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 8, "use appropriate getters or setters instead")
764 const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
765 /* get map name */
766 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
767 /* get/set map type */
768 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
769 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
770 /* get/set map size (max_entries) */
771 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
772 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
773 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_map__set_max_entries() instead")
774 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
775 /* get/set map flags */
776 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
777 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
778 /* get/set map NUMA node */
779 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
780 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
781 /* get/set map key size */
782 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
783 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
784 /* get/set map value size */
785 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
786 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
787 /* get map key/value BTF type IDs */
788 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
789 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
790 /* get/set map if_index */
791 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
792 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
793 /* get/set map map_extra flags */
794 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
795 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
796 
797 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
798 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
799 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
800 				 bpf_map_clear_priv_t clear_priv);
801 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
802 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
803 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
804 					  const void *data, size_t size);
805 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
806 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_map__type() instead")
807 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
808 
809 /**
810  * @brief **bpf_map__is_internal()** tells the caller whether or not the
811  * passed map is a special map created by libbpf automatically for things like
812  * global variables, __ksym externs, Kconfig values, etc
813  * @param map the bpf_map
814  * @return true, if the map is an internal map; false, otherwise
815  */
816 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
817 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
818 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
819 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
820 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
821 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
822 
823 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
824 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
825 
826 /**
827  * @brief **libbpf_get_error()** extracts the error code from the passed
828  * pointer
829  * @param ptr pointer returned from libbpf API function
830  * @return error code; or 0 if no error occured
831  *
832  * Many libbpf API functions which return pointers have logic to encode error
833  * codes as pointers, and do not return NULL. Meaning **libbpf_get_error()**
834  * should be used on the return value from these functions immediately after
835  * calling the API function, with no intervening calls that could clobber the
836  * `errno` variable. Consult the individual functions documentation to verify
837  * if this logic applies should be used.
838  *
839  * For these API functions, if `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)`
840  * is enabled, NULL is returned on error instead.
841  *
842  * If ptr is NULL, then errno should be already set by the failing
843  * API, because libbpf never returns NULL on success and it now always
844  * sets errno on error.
845  *
846  * Example usage:
847  *
848  *   struct perf_buffer *pb;
849  *
850  *   pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &opts);
851  *   err = libbpf_get_error(pb);
852  *   if (err) {
853  *	  pb = NULL;
854  *	  fprintf(stderr, "failed to open perf buffer: %d\n", err);
855  *	  goto cleanup;
856  *   }
857  */
858 LIBBPF_API long libbpf_get_error(const void *ptr);
859 
860 struct bpf_prog_load_attr {
861 	const char *file;
862 	enum bpf_prog_type prog_type;
863 	enum bpf_attach_type expected_attach_type;
864 	int ifindex;
865 	int log_level;
866 	int prog_flags;
867 };
868 
869 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__open() and bpf_object__load() instead")
870 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
871 				   struct bpf_object **pobj, int *prog_fd);
872 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open() and bpf_object__load() instead")
873 LIBBPF_API int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type,
874 					struct bpf_object **pobj, int *prog_fd);
875 
876 /* XDP related API */
877 struct xdp_link_info {
878 	__u32 prog_id;
879 	__u32 drv_prog_id;
880 	__u32 hw_prog_id;
881 	__u32 skb_prog_id;
882 	__u8 attach_mode;
883 };
884 
885 struct bpf_xdp_set_link_opts {
886 	size_t sz;
887 	int old_fd;
888 	size_t :0;
889 };
890 #define bpf_xdp_set_link_opts__last_field old_fd
891 
892 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_attach() instead")
893 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
894 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_attach() instead")
895 LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
896 					const struct bpf_xdp_set_link_opts *opts);
897 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_query_id() instead")
898 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
899 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_query() instead")
900 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
901 				     size_t info_size, __u32 flags);
902 
903 struct bpf_xdp_attach_opts {
904 	size_t sz;
905 	int old_prog_fd;
906 	size_t :0;
907 };
908 #define bpf_xdp_attach_opts__last_field old_prog_fd
909 
910 struct bpf_xdp_query_opts {
911 	size_t sz;
912 	__u32 prog_id;		/* output */
913 	__u32 drv_prog_id;	/* output */
914 	__u32 hw_prog_id;	/* output */
915 	__u32 skb_prog_id;	/* output */
916 	__u8 attach_mode;	/* output */
917 	size_t :0;
918 };
919 #define bpf_xdp_query_opts__last_field attach_mode
920 
921 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
922 			      const struct bpf_xdp_attach_opts *opts);
923 LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
924 			      const struct bpf_xdp_attach_opts *opts);
925 LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
926 LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
927 
928 /* TC related API */
929 enum bpf_tc_attach_point {
930 	BPF_TC_INGRESS = 1 << 0,
931 	BPF_TC_EGRESS  = 1 << 1,
932 	BPF_TC_CUSTOM  = 1 << 2,
933 };
934 
935 #define BPF_TC_PARENT(a, b) 	\
936 	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
937 
938 enum bpf_tc_flags {
939 	BPF_TC_F_REPLACE = 1 << 0,
940 };
941 
942 struct bpf_tc_hook {
943 	size_t sz;
944 	int ifindex;
945 	enum bpf_tc_attach_point attach_point;
946 	__u32 parent;
947 	size_t :0;
948 };
949 #define bpf_tc_hook__last_field parent
950 
951 struct bpf_tc_opts {
952 	size_t sz;
953 	int prog_fd;
954 	__u32 flags;
955 	__u32 prog_id;
956 	__u32 handle;
957 	__u32 priority;
958 	size_t :0;
959 };
960 #define bpf_tc_opts__last_field priority
961 
962 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
963 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
964 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
965 			     struct bpf_tc_opts *opts);
966 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
967 			     const struct bpf_tc_opts *opts);
968 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
969 			    struct bpf_tc_opts *opts);
970 
971 /* Ring buffer APIs */
972 struct ring_buffer;
973 
974 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
975 
976 struct ring_buffer_opts {
977 	size_t sz; /* size of this struct, for forward/backward compatiblity */
978 };
979 
980 #define ring_buffer_opts__last_field sz
981 
982 LIBBPF_API struct ring_buffer *
983 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
984 		 const struct ring_buffer_opts *opts);
985 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
986 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
987 				ring_buffer_sample_fn sample_cb, void *ctx);
988 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
989 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
990 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
991 
992 /* Perf buffer APIs */
993 struct perf_buffer;
994 
995 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
996 				      void *data, __u32 size);
997 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
998 
999 /* common use perf buffer options */
1000 struct perf_buffer_opts {
1001 	union {
1002 		size_t sz;
1003 		struct { /* DEPRECATED: will be removed in v1.0 */
1004 			/* if specified, sample_cb is called for each sample */
1005 			perf_buffer_sample_fn sample_cb;
1006 			/* if specified, lost_cb is called for each batch of lost samples */
1007 			perf_buffer_lost_fn lost_cb;
1008 			/* ctx is provided to sample_cb and lost_cb */
1009 			void *ctx;
1010 		};
1011 	};
1012 };
1013 #define perf_buffer_opts__last_field sz
1014 
1015 /**
1016  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1017  * BPF_PERF_EVENT_ARRAY map
1018  * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1019  * code to send data over to user-space
1020  * @param page_cnt number of memory pages allocated for each per-CPU buffer
1021  * @param sample_cb function called on each received data record
1022  * @param lost_cb function called when record loss has occurred
1023  * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1024  * @return a new instance of struct perf_buffer on success, NULL on error with
1025  * *errno* containing an error code
1026  */
1027 LIBBPF_API struct perf_buffer *
1028 perf_buffer__new(int map_fd, size_t page_cnt,
1029 		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1030 		 const struct perf_buffer_opts *opts);
1031 
1032 LIBBPF_API struct perf_buffer *
1033 perf_buffer__new_v0_6_0(int map_fd, size_t page_cnt,
1034 			perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1035 			const struct perf_buffer_opts *opts);
1036 
1037 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new() instead")
1038 struct perf_buffer *perf_buffer__new_deprecated(int map_fd, size_t page_cnt,
1039 						const struct perf_buffer_opts *opts);
1040 
1041 #define perf_buffer__new(...) ___libbpf_overload(___perf_buffer_new, __VA_ARGS__)
1042 #define ___perf_buffer_new6(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts) \
1043 	perf_buffer__new(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts)
1044 #define ___perf_buffer_new3(map_fd, page_cnt, opts) \
1045 	perf_buffer__new_deprecated(map_fd, page_cnt, opts)
1046 
1047 enum bpf_perf_event_ret {
1048 	LIBBPF_PERF_EVENT_DONE	= 0,
1049 	LIBBPF_PERF_EVENT_ERROR	= -1,
1050 	LIBBPF_PERF_EVENT_CONT	= -2,
1051 };
1052 
1053 struct perf_event_header;
1054 
1055 typedef enum bpf_perf_event_ret
1056 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1057 
1058 /* raw perf buffer options, giving most power and control */
1059 struct perf_buffer_raw_opts {
1060 	union {
1061 		struct {
1062 			size_t sz;
1063 			long :0;
1064 			long :0;
1065 		};
1066 		struct { /* DEPRECATED: will be removed in v1.0 */
1067 			/* perf event attrs passed directly into perf_event_open() */
1068 			struct perf_event_attr *attr;
1069 			/* raw event callback */
1070 			perf_buffer_event_fn event_cb;
1071 			/* ctx is provided to event_cb */
1072 			void *ctx;
1073 		};
1074 	};
1075 	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1076 	 * max_entries of given PERF_EVENT_ARRAY map)
1077 	 */
1078 	int cpu_cnt;
1079 	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1080 	int *cpus;
1081 	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1082 	int *map_keys;
1083 };
1084 #define perf_buffer_raw_opts__last_field map_keys
1085 
1086 LIBBPF_API struct perf_buffer *
1087 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1088 		     perf_buffer_event_fn event_cb, void *ctx,
1089 		     const struct perf_buffer_raw_opts *opts);
1090 
1091 LIBBPF_API struct perf_buffer *
1092 perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1093 			    perf_buffer_event_fn event_cb, void *ctx,
1094 			    const struct perf_buffer_raw_opts *opts);
1095 
1096 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new_raw() instead")
1097 struct perf_buffer *perf_buffer__new_raw_deprecated(int map_fd, size_t page_cnt,
1098 						    const struct perf_buffer_raw_opts *opts);
1099 
1100 #define perf_buffer__new_raw(...) ___libbpf_overload(___perf_buffer_new_raw, __VA_ARGS__)
1101 #define ___perf_buffer_new_raw6(map_fd, page_cnt, attr, event_cb, ctx, opts) \
1102 	perf_buffer__new_raw(map_fd, page_cnt, attr, event_cb, ctx, opts)
1103 #define ___perf_buffer_new_raw3(map_fd, page_cnt, opts) \
1104 	perf_buffer__new_raw_deprecated(map_fd, page_cnt, opts)
1105 
1106 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1107 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1108 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1109 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1110 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1111 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1112 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1113 
1114 typedef enum bpf_perf_event_ret
1115 	(*bpf_perf_event_print_t)(struct perf_event_header *hdr,
1116 				  void *private_data);
1117 LIBBPF_DEPRECATED_SINCE(0, 8, "use perf_buffer__poll() or  perf_buffer__consume() instead")
1118 LIBBPF_API enum bpf_perf_event_ret
1119 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
1120 			   void **copy_mem, size_t *copy_size,
1121 			   bpf_perf_event_print_t fn, void *private_data);
1122 
1123 struct bpf_prog_linfo;
1124 struct bpf_prog_info;
1125 
1126 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1127 LIBBPF_API struct bpf_prog_linfo *
1128 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1129 LIBBPF_API const struct bpf_line_info *
1130 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1131 				__u64 addr, __u32 func_idx, __u32 nr_skip);
1132 LIBBPF_API const struct bpf_line_info *
1133 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1134 		      __u32 insn_off, __u32 nr_skip);
1135 
1136 /*
1137  * Probe for supported system features
1138  *
1139  * Note that running many of these probes in a short amount of time can cause
1140  * the kernel to reach the maximal size of lockable memory allowed for the
1141  * user, causing subsequent probes to fail. In this case, the caller may want
1142  * to adjust that limit with setrlimit().
1143  */
1144 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_prog_type() instead")
1145 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex);
1146 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_map_type() instead")
1147 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
1148 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_helper() instead")
1149 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type, __u32 ifindex);
1150 LIBBPF_DEPRECATED_SINCE(0, 8, "implement your own or use bpftool for feature detection")
1151 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
1152 
1153 /**
1154  * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1155  * BPF programs of a given type.
1156  * @param prog_type BPF program type to detect kernel support for
1157  * @param opts reserved for future extensibility, should be NULL
1158  * @return 1, if given program type is supported; 0, if given program type is
1159  * not supported; negative error code if feature detection failed or can't be
1160  * performed
1161  *
1162  * Make sure the process has required set of CAP_* permissions (or runs as
1163  * root) when performing feature checking.
1164  */
1165 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1166 /**
1167  * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1168  * BPF maps of a given type.
1169  * @param map_type BPF map type to detect kernel support for
1170  * @param opts reserved for future extensibility, should be NULL
1171  * @return 1, if given map type is supported; 0, if given map type is
1172  * not supported; negative error code if feature detection failed or can't be
1173  * performed
1174  *
1175  * Make sure the process has required set of CAP_* permissions (or runs as
1176  * root) when performing feature checking.
1177  */
1178 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1179 /**
1180  * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1181  * use of a given BPF helper from specified BPF program type.
1182  * @param prog_type BPF program type used to check the support of BPF helper
1183  * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1184  * @param opts reserved for future extensibility, should be NULL
1185  * @return 1, if given combination of program type and helper is supported; 0,
1186  * if the combination is not supported; negative error code if feature
1187  * detection for provided input arguments failed or can't be performed
1188  *
1189  * Make sure the process has required set of CAP_* permissions (or runs as
1190  * root) when performing feature checking.
1191  */
1192 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1193 				       enum bpf_func_id helper_id, const void *opts);
1194 
1195 /*
1196  * Get bpf_prog_info in continuous memory
1197  *
1198  * struct bpf_prog_info has multiple arrays. The user has option to choose
1199  * arrays to fetch from kernel. The following APIs provide an uniform way to
1200  * fetch these data. All arrays in bpf_prog_info are stored in a single
1201  * continuous memory region. This makes it easy to store the info in a
1202  * file.
1203  *
1204  * Before writing bpf_prog_info_linear to files, it is necessary to
1205  * translate pointers in bpf_prog_info to offsets. Helper functions
1206  * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
1207  * are introduced to switch between pointers and offsets.
1208  *
1209  * Examples:
1210  *   # To fetch map_ids and prog_tags:
1211  *   __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
1212  *           (1UL << BPF_PROG_INFO_PROG_TAGS);
1213  *   struct bpf_prog_info_linear *info_linear =
1214  *           bpf_program__get_prog_info_linear(fd, arrays);
1215  *
1216  *   # To save data in file
1217  *   bpf_program__bpil_addr_to_offs(info_linear);
1218  *   write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
1219  *
1220  *   # To read data from file
1221  *   read(f, info_linear, <proper_size>);
1222  *   bpf_program__bpil_offs_to_addr(info_linear);
1223  */
1224 enum bpf_prog_info_array {
1225 	BPF_PROG_INFO_FIRST_ARRAY = 0,
1226 	BPF_PROG_INFO_JITED_INSNS = 0,
1227 	BPF_PROG_INFO_XLATED_INSNS,
1228 	BPF_PROG_INFO_MAP_IDS,
1229 	BPF_PROG_INFO_JITED_KSYMS,
1230 	BPF_PROG_INFO_JITED_FUNC_LENS,
1231 	BPF_PROG_INFO_FUNC_INFO,
1232 	BPF_PROG_INFO_LINE_INFO,
1233 	BPF_PROG_INFO_JITED_LINE_INFO,
1234 	BPF_PROG_INFO_PROG_TAGS,
1235 	BPF_PROG_INFO_LAST_ARRAY,
1236 };
1237 
1238 struct bpf_prog_info_linear {
1239 	/* size of struct bpf_prog_info, when the tool is compiled */
1240 	__u32			info_len;
1241 	/* total bytes allocated for data, round up to 8 bytes */
1242 	__u32			data_len;
1243 	/* which arrays are included in data */
1244 	__u64			arrays;
1245 	struct bpf_prog_info	info;
1246 	__u8			data[];
1247 };
1248 
1249 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1250 LIBBPF_API struct bpf_prog_info_linear *
1251 bpf_program__get_prog_info_linear(int fd, __u64 arrays);
1252 
1253 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1254 LIBBPF_API void
1255 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
1256 
1257 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1258 LIBBPF_API void
1259 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
1260 
1261 /**
1262  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1263  * number of possible CPUs that the host kernel supports and expects.
1264  * @return number of possible CPUs; or error code on failure
1265  *
1266  * Example usage:
1267  *
1268  *     int ncpus = libbpf_num_possible_cpus();
1269  *     if (ncpus < 0) {
1270  *          // error handling
1271  *     }
1272  *     long values[ncpus];
1273  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1274  */
1275 LIBBPF_API int libbpf_num_possible_cpus(void);
1276 
1277 struct bpf_map_skeleton {
1278 	const char *name;
1279 	struct bpf_map **map;
1280 	void **mmaped;
1281 };
1282 
1283 struct bpf_prog_skeleton {
1284 	const char *name;
1285 	struct bpf_program **prog;
1286 	struct bpf_link **link;
1287 };
1288 
1289 struct bpf_object_skeleton {
1290 	size_t sz; /* size of this struct, for forward/backward compatibility */
1291 
1292 	const char *name;
1293 	const void *data;
1294 	size_t data_sz;
1295 
1296 	struct bpf_object **obj;
1297 
1298 	int map_cnt;
1299 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1300 	struct bpf_map_skeleton *maps;
1301 
1302 	int prog_cnt;
1303 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1304 	struct bpf_prog_skeleton *progs;
1305 };
1306 
1307 LIBBPF_API int
1308 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1309 			  const struct bpf_object_open_opts *opts);
1310 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1311 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1312 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1313 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1314 
1315 struct bpf_var_skeleton {
1316 	const char *name;
1317 	struct bpf_map **map;
1318 	void **addr;
1319 };
1320 
1321 struct bpf_object_subskeleton {
1322 	size_t sz; /* size of this struct, for forward/backward compatibility */
1323 
1324 	const struct bpf_object *obj;
1325 
1326 	int map_cnt;
1327 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1328 	struct bpf_map_skeleton *maps;
1329 
1330 	int prog_cnt;
1331 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1332 	struct bpf_prog_skeleton *progs;
1333 
1334 	int var_cnt;
1335 	int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1336 	struct bpf_var_skeleton *vars;
1337 };
1338 
1339 LIBBPF_API int
1340 bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1341 LIBBPF_API void
1342 bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1343 
1344 struct gen_loader_opts {
1345 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1346 	const char *data;
1347 	const char *insns;
1348 	__u32 data_sz;
1349 	__u32 insns_sz;
1350 };
1351 
1352 #define gen_loader_opts__last_field insns_sz
1353 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1354 				      struct gen_loader_opts *opts);
1355 
1356 enum libbpf_tristate {
1357 	TRI_NO = 0,
1358 	TRI_YES = 1,
1359 	TRI_MODULE = 2,
1360 };
1361 
1362 struct bpf_linker_opts {
1363 	/* size of this struct, for forward/backward compatiblity */
1364 	size_t sz;
1365 };
1366 #define bpf_linker_opts__last_field sz
1367 
1368 struct bpf_linker_file_opts {
1369 	/* size of this struct, for forward/backward compatiblity */
1370 	size_t sz;
1371 };
1372 #define bpf_linker_file_opts__last_field sz
1373 
1374 struct bpf_linker;
1375 
1376 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1377 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1378 				    const char *filename,
1379 				    const struct bpf_linker_file_opts *opts);
1380 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1381 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1382 
1383 /*
1384  * Custom handling of BPF program's SEC() definitions
1385  */
1386 
1387 struct bpf_prog_load_opts; /* defined in bpf.h */
1388 
1389 /* Called during bpf_object__open() for each recognized BPF program. Callback
1390  * can use various bpf_program__set_*() setters to adjust whatever properties
1391  * are necessary.
1392  */
1393 typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1394 
1395 /* Called right before libbpf performs bpf_prog_load() to load BPF program
1396  * into the kernel. Callback can adjust opts as necessary.
1397  */
1398 typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1399 					     struct bpf_prog_load_opts *opts, long cookie);
1400 
1401 /* Called during skeleton attach or through bpf_program__attach(). If
1402  * auto-attach is not supported, callback should return 0 and set link to
1403  * NULL (it's not considered an error during skeleton attach, but it will be
1404  * an error for bpf_program__attach() calls). On error, error should be
1405  * returned directly and link set to NULL. On success, return 0 and set link
1406  * to a valid struct bpf_link.
1407  */
1408 typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1409 				       struct bpf_link **link);
1410 
1411 struct libbpf_prog_handler_opts {
1412 	/* size of this struct, for forward/backward compatiblity */
1413 	size_t sz;
1414 	/* User-provided value that is passed to prog_setup_fn,
1415 	 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1416 	 * register one set of callbacks for multiple SEC() definitions and
1417 	 * still be able to distinguish them, if necessary. For example,
1418 	 * libbpf itself is using this to pass necessary flags (e.g.,
1419 	 * sleepable flag) to a common internal SEC() handler.
1420 	 */
1421 	long cookie;
1422 	/* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1423 	 * Callback is optional, pass NULL if it's not necessary.
1424 	 */
1425 	libbpf_prog_setup_fn_t prog_setup_fn;
1426 	/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1427 	 * Callback is optional, pass NULL if it's not necessary.
1428 	 */
1429 	libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1430 	/* BPF program attach callback (see libbpf_prog_attach_fn_t).
1431 	 * Callback is optional, pass NULL if it's not necessary.
1432 	 */
1433 	libbpf_prog_attach_fn_t prog_attach_fn;
1434 };
1435 #define libbpf_prog_handler_opts__last_field prog_attach_fn
1436 
1437 /**
1438  * @brief **libbpf_register_prog_handler()** registers a custom BPF program
1439  * SEC() handler.
1440  * @param sec section prefix for which custom handler is registered
1441  * @param prog_type BPF program type associated with specified section
1442  * @param exp_attach_type Expected BPF attach type associated with specified section
1443  * @param opts optional cookie, callbacks, and other extra options
1444  * @return Non-negative handler ID is returned on success. This handler ID has
1445  * to be passed to *libbpf_unregister_prog_handler()* to unregister such
1446  * custom handler. Negative error code is returned on error.
1447  *
1448  * *sec* defines which SEC() definitions are handled by this custom handler
1449  * registration. *sec* can have few different forms:
1450  *   - if *sec* is just a plain string (e.g., "abc"), it will match only
1451  *   SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1452  *   in an error;
1453  *   - if *sec* is of the form "abc/", proper SEC() form is
1454  *   SEC("abc/something"), where acceptable "something" should be checked by
1455  *   *prog_init_fn* callback, if there are additional restrictions;
1456  *   - if *sec* is of the form "abc+", it will successfully match both
1457  *   SEC("abc") and SEC("abc/whatever") forms;
1458  *   - if *sec* is NULL, custom handler is registered for any BPF program that
1459  *   doesn't match any of the registered (custom or libbpf's own) SEC()
1460  *   handlers. There could be only one such generic custom handler registered
1461  *   at any given time.
1462  *
1463  * All custom handlers (except the one with *sec* == NULL) are processed
1464  * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1465  * SEC() handlers by registering custom ones for the same section prefix
1466  * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1467  * handler).
1468  *
1469  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1470  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1471  * to ensure synchronization if there is a risk of running this API from
1472  * multiple threads simultaneously.
1473  */
1474 LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1475 					    enum bpf_prog_type prog_type,
1476 					    enum bpf_attach_type exp_attach_type,
1477 					    const struct libbpf_prog_handler_opts *opts);
1478 /**
1479  * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1480  * custom BPF program SEC() handler.
1481  * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1482  * after successful registration
1483  * @return 0 on success, negative error code if handler isn't found
1484  *
1485  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1486  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1487  * to ensure synchronization if there is a risk of running this API from
1488  * multiple threads simultaneously.
1489  */
1490 LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1491 
1492 #ifdef __cplusplus
1493 } /* extern "C" */
1494 #endif
1495 
1496 #endif /* __LIBBPF_LIBBPF_H */
1497