1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
4 * Common eBPF ELF object loading operations.
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.
10 #ifndef __LIBBPF_LIBBPF_H
11 #define __LIBBPF_LIBBPF_H
17 #include <sys/types.h> // for size_t
18 #include <linux/bpf.h>
20 #include "libbpf_common.h"
21 #include "libbpf_legacy.h"
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);
32 __LIBBPF_ERRNO__START = 4000,
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 */
52 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
54 enum libbpf_print_level {
60 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
61 const char *, va_list ap);
63 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
65 /* Hide internal to user */
68 struct bpf_object_open_attr {
70 enum bpf_prog_type prog_type;
73 struct bpf_object_open_opts {
74 /* size of this struct, for forward/backward compatiblity */
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;
82 const char *object_name;
83 /* parse map definitions non-strictly, allowing extra attributes/data */
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.
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".
96 const char *pin_root_path;
98 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__set_attach_target() on each individual bpf_program")
100 /* Additional kernel config content that augments and overrides
101 * system Kconfig for CONFIG_xxx externs.
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.
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.
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.
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.
132 char *kernel_log_buf;
133 size_t kernel_log_size;
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.
147 __u32 kernel_log_level;
151 #define bpf_object_open_opts__last_field kernel_log_level
153 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
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
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
165 LIBBPF_API struct bpf_object *
166 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
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
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);
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,
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);
191 enum libbpf_pin_type {
193 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
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).
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,
204 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
206 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
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);
211 struct bpf_object_load_attr {
212 struct bpf_object *obj;
214 const char *target_btf_path;
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);
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);
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);
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,
236 LIBBPF_API struct bpf_program *
237 bpf_object__find_program_by_name(const struct bpf_object *obj,
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); \
246 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
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);
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);
263 /* Accessors of 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);
271 #define bpf_object__for_each_program(pos, obj) \
272 for ((pos) = bpf_object__next_program((obj), NULL); \
274 (pos) = bpf_object__next_program((obj), (pos)))
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);
282 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
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,
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);
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);
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
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.
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
325 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
328 * @brief **bpf_program__set_insns()** can set BPF program's underlying
331 * WARNING: This is a very advanced libbpf API and users need to know
332 * what they are doing. This should be used from prog_prepare_load_fn
335 * @param prog BPF program for which to return instructions
336 * @param new_insns a pointer to an array of BPF instructions
337 * @param new_insn_cnt number of `struct bpf_insn`'s that form
338 * specified BPF program
339 * @return 0, on success; negative error code, otherwise
341 LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,
342 struct bpf_insn *new_insns, size_t new_insn_cnt);
345 * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
346 * that form specified BPF program.
347 * @param prog BPF program for which to return number of BPF instructions
349 * See **bpf_program__insns()** documentation for notes on how libbpf can
350 * change instructions and their count during different phases of
351 * **bpf_object** lifetime.
353 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
355 LIBBPF_DEPRECATED_SINCE(0, 6, "use bpf_object__load() instead")
356 LIBBPF_API int bpf_program__load(struct bpf_program *prog, const char *license, __u32 kern_version);
357 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
358 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
359 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
362 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
363 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
368 * @brief **bpf_program__pin()** pins the BPF program to a file
369 * in the BPF FS specified by a path. This increments the programs
370 * reference count, allowing it to stay loaded after the process
371 * which loaded it has exited.
373 * @param prog BPF program to pin, must already be loaded
374 * @param path file path in a BPF file system
375 * @return 0, on success; negative error code, otherwise
377 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
380 * @brief **bpf_program__unpin()** unpins the BPF program from a file
381 * in the BPFFS specified by a path. This decrements the programs
384 * The file pinning the BPF program can also be unlinked by a different
385 * process in which case this function will return an error.
387 * @param prog BPF program to unpin
388 * @param path file path to the pin in a BPF file system
389 * @return 0, on success; negative error code, otherwise
391 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
392 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
396 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
397 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
398 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
400 * @brief **bpf_link__pin()** pins the BPF link to a file
401 * in the BPF FS specified by a path. This increments the links
402 * reference count, allowing it to stay loaded after the process
403 * which loaded it has exited.
405 * @param link BPF link to pin, must already be loaded
406 * @param path file path in a BPF file system
407 * @return 0, on success; negative error code, otherwise
410 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
413 * @brief **bpf_link__unpin()** unpins the BPF link from a file
414 * in the BPFFS specified by a path. This decrements the links
417 * The file pinning the BPF link can also be unlinked by a different
418 * process in which case this function will return an error.
420 * @param prog BPF program to unpin
421 * @param path file path to the pin in a BPF file system
422 * @return 0, on success; negative error code, otherwise
424 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
425 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
426 struct bpf_program *prog);
427 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
428 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
429 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
432 * @brief **bpf_program__attach()** is a generic function for attaching
433 * a BPF program based on auto-detection of program type, attach type,
434 * and extra paremeters, where applicable.
436 * @param prog BPF program to attach
437 * @return Reference to the newly created BPF link; or NULL is returned on error,
438 * error code is stored in errno
440 * This is supported for:
441 * - kprobe/kretprobe (depends on SEC() definition)
442 * - uprobe/uretprobe (depends on SEC() definition)
445 * - tracing programs (typed raw TP/fentry/fexit/fmod_ret)
447 LIBBPF_API struct bpf_link *
448 bpf_program__attach(const struct bpf_program *prog);
450 struct bpf_perf_event_opts {
451 /* size of this struct, for forward/backward compatiblity */
453 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
456 #define bpf_perf_event_opts__last_field bpf_cookie
458 LIBBPF_API struct bpf_link *
459 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
461 LIBBPF_API struct bpf_link *
462 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
463 const struct bpf_perf_event_opts *opts);
465 struct bpf_kprobe_opts {
466 /* size of this struct, for forward/backward compatiblity */
468 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
470 /* function's offset to install kprobe to */
472 /* kprobe is return probe */
476 #define bpf_kprobe_opts__last_field retprobe
478 LIBBPF_API struct bpf_link *
479 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
480 const char *func_name);
481 LIBBPF_API struct bpf_link *
482 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
483 const char *func_name,
484 const struct bpf_kprobe_opts *opts);
486 struct bpf_kprobe_multi_opts {
487 /* size of this struct, for forward/backward compatibility */
489 /* array of function symbols to attach */
491 /* array of function addresses to attach */
492 const unsigned long *addrs;
493 /* array of user-provided values fetchable through bpf_get_attach_cookie */
494 const __u64 *cookies;
495 /* number of elements in syms/addrs/cookies arrays */
497 /* create return kprobes */
502 #define bpf_kprobe_multi_opts__last_field retprobe
504 LIBBPF_API struct bpf_link *
505 bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
507 const struct bpf_kprobe_multi_opts *opts);
509 struct bpf_uprobe_opts {
510 /* size of this struct, for forward/backward compatiblity */
512 /* offset of kernel reference counted USDT semaphore, added in
513 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
515 size_t ref_ctr_offset;
516 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
518 /* uprobe is return probe, invoked at function return time */
520 /* Function name to attach to. Could be an unqualified ("abc") or library-qualified
521 * "abc@LIBXYZ" name. To specify function entry, func_name should be set while
522 * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an
523 * offset within a function, specify func_name and use func_offset argument to specify
524 * offset within the function. Shared library functions must specify the shared library
527 const char *func_name;
530 #define bpf_uprobe_opts__last_field func_name
533 * @brief **bpf_program__attach_uprobe()** attaches a BPF program
534 * to the userspace function which is found by binary path and
535 * offset. You can optionally specify a particular proccess to attach
536 * to. You can also optionally attach the program to the function
537 * exit instead of entry.
539 * @param prog BPF program to attach
540 * @param retprobe Attach to function exit
541 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
542 * -1 for all processes
543 * @param binary_path Path to binary that contains the function symbol
544 * @param func_offset Offset within the binary of the function symbol
545 * @return Reference to the newly created BPF link; or NULL is returned on error,
546 * error code is stored in errno
548 LIBBPF_API struct bpf_link *
549 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
550 pid_t pid, const char *binary_path,
554 * @brief **bpf_program__attach_uprobe_opts()** is just like
555 * bpf_program__attach_uprobe() except with a options struct
556 * for various configurations.
558 * @param prog BPF program to attach
559 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
560 * -1 for all processes
561 * @param binary_path Path to binary that contains the function symbol
562 * @param func_offset Offset within the binary of the function symbol
563 * @param opts Options for altering program attachment
564 * @return Reference to the newly created BPF link; or NULL is returned on error,
565 * error code is stored in errno
567 LIBBPF_API struct bpf_link *
568 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
569 const char *binary_path, size_t func_offset,
570 const struct bpf_uprobe_opts *opts);
572 struct bpf_usdt_opts {
573 /* size of this struct, for forward/backward compatibility */
575 /* custom user-provided value accessible through usdt_cookie() */
579 #define bpf_usdt_opts__last_field usdt_cookie
582 * @brief **bpf_program__attach_usdt()** is just like
583 * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
584 * Statically Defined Tracepoint) attachment, instead of attaching to
585 * user-space function entry or exit.
587 * @param prog BPF program to attach
588 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
589 * -1 for all processes
590 * @param binary_path Path to binary that contains provided USDT probe
591 * @param usdt_provider USDT provider name
592 * @param usdt_name USDT probe name
593 * @param opts Options for altering program attachment
594 * @return Reference to the newly created BPF link; or NULL is returned on error,
595 * error code is stored in errno
597 LIBBPF_API struct bpf_link *
598 bpf_program__attach_usdt(const struct bpf_program *prog,
599 pid_t pid, const char *binary_path,
600 const char *usdt_provider, const char *usdt_name,
601 const struct bpf_usdt_opts *opts);
603 struct bpf_tracepoint_opts {
604 /* size of this struct, for forward/backward compatiblity */
606 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
609 #define bpf_tracepoint_opts__last_field bpf_cookie
611 LIBBPF_API struct bpf_link *
612 bpf_program__attach_tracepoint(const struct bpf_program *prog,
613 const char *tp_category,
614 const char *tp_name);
615 LIBBPF_API struct bpf_link *
616 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
617 const char *tp_category,
619 const struct bpf_tracepoint_opts *opts);
621 LIBBPF_API struct bpf_link *
622 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
623 const char *tp_name);
625 struct bpf_trace_opts {
626 /* size of this struct, for forward/backward compatibility */
628 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
631 #define bpf_trace_opts__last_field cookie
633 LIBBPF_API struct bpf_link *
634 bpf_program__attach_trace(const struct bpf_program *prog);
635 LIBBPF_API struct bpf_link *
636 bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
638 LIBBPF_API struct bpf_link *
639 bpf_program__attach_lsm(const struct bpf_program *prog);
640 LIBBPF_API struct bpf_link *
641 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
642 LIBBPF_API struct bpf_link *
643 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
644 LIBBPF_API struct bpf_link *
645 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
646 LIBBPF_API struct bpf_link *
647 bpf_program__attach_freplace(const struct bpf_program *prog,
648 int target_fd, const char *attach_func_name);
652 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
654 struct bpf_iter_attach_opts {
655 size_t sz; /* size of this struct for forward/backward compatibility */
656 union bpf_iter_link_info *link_info;
659 #define bpf_iter_attach_opts__last_field link_info_len
661 LIBBPF_API struct bpf_link *
662 bpf_program__attach_iter(const struct bpf_program *prog,
663 const struct bpf_iter_attach_opts *opts);
666 * Libbpf allows callers to adjust BPF programs before being loaded
667 * into kernel. One program in an object file can be transformed into
668 * multiple variants to be attached to different hooks.
670 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
671 * form an API for this purpose.
673 * - bpf_program_prep_t:
674 * Defines a 'preprocessor', which is a caller defined function
675 * passed to libbpf through bpf_program__set_prep(), and will be
676 * called before program is loaded. The processor should adjust
677 * the program one time for each instance according to the instance id
680 * - bpf_program__set_prep:
681 * Attaches a preprocessor to a BPF program. The number of instances
682 * that should be created is also passed through this function.
684 * - bpf_program__nth_fd:
685 * After the program is loaded, get resulting FD of a given instance
686 * of the BPF program.
688 * If bpf_program__set_prep() is not used, the program would be loaded
689 * without adjustment during bpf_object__load(). The program has only
690 * one instance. In this case bpf_program__fd(prog) is equal to
691 * bpf_program__nth_fd(prog, 0).
693 struct bpf_prog_prep_result {
695 * If not NULL, load new instruction array.
696 * If set to NULL, don't load this instance.
698 struct bpf_insn *new_insn_ptr;
701 /* If not NULL, result FD is written to it. */
706 * Parameters of bpf_program_prep_t:
707 * - prog: The bpf_program being loaded.
708 * - n: Index of instance being generated.
709 * - insns: BPF instructions array.
710 * - insns_cnt:Number of instructions in insns.
711 * - res: Output parameter, result of transformation.
714 * - Zero: pre-processing success.
715 * - Non-zero: pre-processing error, stop loading.
717 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
718 struct bpf_insn *insns, int insns_cnt,
719 struct bpf_prog_prep_result *res);
721 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions")
722 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
723 bpf_program_prep_t prep);
725 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
726 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
729 * Adjust type of BPF program. Default is kprobe.
731 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
732 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
733 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
734 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
735 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
736 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
737 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
738 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
739 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
740 LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
741 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
742 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
743 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
744 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
745 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
746 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
747 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
748 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
749 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
750 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
751 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
752 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
753 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
754 LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog);
755 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
756 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
758 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
761 * @brief **bpf_program__set_type()** sets the program
762 * type of the passed BPF program.
763 * @param prog BPF program to set the program type for
764 * @param type program type to set the BPF map to have
765 * @return error code; or 0 if no error. An error occurs
766 * if the object is already loaded.
768 * This must be called before the BPF object is loaded,
769 * otherwise it has no effect and an error is returned.
771 LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
772 enum bpf_prog_type type);
774 LIBBPF_API enum bpf_attach_type
775 bpf_program__expected_attach_type(const struct bpf_program *prog);
778 * @brief **bpf_program__set_expected_attach_type()** sets the
779 * attach type of the passed BPF program. This is used for
780 * auto-detection of attachment when programs are loaded.
781 * @param prog BPF program to set the attach type for
782 * @param type attach type to set the BPF map to have
783 * @return error code; or 0 if no error. An error occurs
784 * if the object is already loaded.
786 * This must be called before the BPF object is loaded,
787 * otherwise it has no effect and an error is returned.
790 bpf_program__set_expected_attach_type(struct bpf_program *prog,
791 enum bpf_attach_type type);
793 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
794 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
796 /* Per-program log level and log buffer getters/setters.
797 * See bpf_object_open_opts comments regarding log_level and log_buf
800 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
801 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
802 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
803 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
806 * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
807 * for supported BPF program types:
808 * - BTF-aware raw tracepoints (tp_btf);
809 * - fentry/fexit/fmod_ret;
812 * @param prog BPF program to set the attach type for
813 * @param type attach type to set the BPF map to have
814 * @return error code; or 0 if no error occurred.
817 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
818 const char *attach_func_name);
820 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
821 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
822 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
823 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
824 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
825 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
826 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
827 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
828 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
829 LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
830 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
831 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
832 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
833 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
834 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
835 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
836 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
837 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
838 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
839 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
840 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
841 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
842 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
843 LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog);
844 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
845 LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog);
848 * No need for __attribute__((packed)), all members of 'bpf_map_def'
849 * are all aligned. In addition, using __attribute__((packed))
850 * would trigger a -Wpacked warning message, and lead to an error
855 unsigned int key_size;
856 unsigned int value_size;
857 unsigned int max_entries;
858 unsigned int map_flags;
862 * @brief **bpf_object__find_map_by_name()** returns BPF map of
863 * the given name, if it exists within the passed BPF object
864 * @param obj BPF object
865 * @param name name of the BPF map
866 * @return BPF map instance, if such map exists within the BPF object;
869 LIBBPF_API struct bpf_map *
870 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
873 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
876 * Get bpf_map through the offset of corresponding struct bpf_map_def
877 * in the BPF object file.
879 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__find_map_by_name() instead")
881 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
883 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead")
884 struct bpf_map *bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
885 LIBBPF_API struct bpf_map *
886 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
888 #define bpf_object__for_each_map(pos, obj) \
889 for ((pos) = bpf_object__next_map((obj), NULL); \
891 (pos) = bpf_object__next_map((obj), (pos)))
892 #define bpf_map__for_each bpf_object__for_each_map
894 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_map() instead")
895 struct bpf_map *bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
896 LIBBPF_API struct bpf_map *
897 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
900 * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
901 * BPF map during BPF object load phase.
902 * @param map the BPF map instance
903 * @param autocreate whether to create BPF map during BPF object load
904 * @return 0 on success; -EBUSY if BPF object was already loaded
906 * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
907 * BPF map. By default, libbpf will attempt to create every single BPF map
908 * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
909 * and fill in map FD in BPF instructions.
911 * This API allows to opt-out of this process for specific map instance. This
912 * can be useful if host kernel doesn't support such BPF map type or used
913 * combination of flags and user application wants to avoid creating such
914 * a map in the first place. User is still responsible to make sure that their
915 * BPF-side code that expects to use such missing BPF map is recognized by BPF
916 * verifier as dead code, otherwise BPF verifier will reject such BPF program.
918 LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
919 LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
922 * @brief **bpf_map__fd()** gets the file descriptor of the passed
924 * @param map the BPF map instance
925 * @return the file descriptor; or -EINVAL in case of an error
927 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
928 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
929 /* get map definition */
930 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 8, "use appropriate getters or setters instead")
931 const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
933 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
934 /* get/set map type */
935 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
936 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
937 /* get/set map size (max_entries) */
938 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
939 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
940 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_map__set_max_entries() instead")
941 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
942 /* get/set map flags */
943 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
944 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
945 /* get/set map NUMA node */
946 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
947 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
948 /* get/set map key size */
949 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
950 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
951 /* get/set map value size */
952 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
953 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
954 /* get map key/value BTF type IDs */
955 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
956 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
957 /* get/set map if_index */
958 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
959 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
960 /* get/set map map_extra flags */
961 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
962 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
964 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
965 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
966 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
967 bpf_map_clear_priv_t clear_priv);
968 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
969 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
970 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
971 const void *data, size_t size);
972 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
973 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_map__type() instead")
974 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
977 * @brief **bpf_map__is_internal()** tells the caller whether or not the
978 * passed map is a special map created by libbpf automatically for things like
979 * global variables, __ksym externs, Kconfig values, etc
980 * @param map the bpf_map
981 * @return true, if the map is an internal map; false, otherwise
983 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
984 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
985 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
986 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
987 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
988 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
990 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
991 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
994 * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
995 * corresponding to provided key.
996 * @param map BPF map to lookup element in
997 * @param key pointer to memory containing bytes of the key used for lookup
998 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
999 * @param value pointer to memory in which looked up value will be stored
1000 * @param value_sz size in byte of value data memory; it has to match BPF map
1001 * definition's **value_size**. For per-CPU BPF maps value size has to be
1002 * a product of BPF map value size and number of possible CPUs in the system
1003 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1004 * per-CPU values value size has to be aligned up to closest 8 bytes for
1005 * alignment reasons, so expected size is: `round_up(value_size, 8)
1006 * * libbpf_num_possible_cpus()`.
1007 * @flags extra flags passed to kernel for this operation
1008 * @return 0, on success; negative error, otherwise
1010 * **bpf_map__lookup_elem()** is high-level equivalent of
1011 * **bpf_map_lookup_elem()** API with added check for key and value size.
1013 LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
1014 const void *key, size_t key_sz,
1015 void *value, size_t value_sz, __u64 flags);
1018 * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
1019 * map that corresponds to provided key.
1020 * @param map BPF map to insert to or update element in
1021 * @param key pointer to memory containing bytes of the key
1022 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1023 * @param value pointer to memory containing bytes of the value
1024 * @param value_sz size in byte of value data memory; it has to match BPF map
1025 * definition's **value_size**. For per-CPU BPF maps value size has to be
1026 * a product of BPF map value size and number of possible CPUs in the system
1027 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1028 * per-CPU values value size has to be aligned up to closest 8 bytes for
1029 * alignment reasons, so expected size is: `round_up(value_size, 8)
1030 * * libbpf_num_possible_cpus()`.
1031 * @flags extra flags passed to kernel for this operation
1032 * @return 0, on success; negative error, otherwise
1034 * **bpf_map__update_elem()** is high-level equivalent of
1035 * **bpf_map_update_elem()** API with added check for key and value size.
1037 LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
1038 const void *key, size_t key_sz,
1039 const void *value, size_t value_sz, __u64 flags);
1042 * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
1043 * corresponds to provided key.
1044 * @param map BPF map to delete element from
1045 * @param key pointer to memory containing bytes of the key
1046 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1047 * @flags extra flags passed to kernel for this operation
1048 * @return 0, on success; negative error, otherwise
1050 * **bpf_map__delete_elem()** is high-level equivalent of
1051 * **bpf_map_delete_elem()** API with added check for key size.
1053 LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
1054 const void *key, size_t key_sz, __u64 flags);
1057 * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
1058 * corresponding to provided key and atomically delete it afterwards.
1059 * @param map BPF map to lookup element in
1060 * @param key pointer to memory containing bytes of the key used for lookup
1061 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1062 * @param value pointer to memory in which looked up value will be stored
1063 * @param value_sz size in byte of value data memory; it has to match BPF map
1064 * definition's **value_size**. For per-CPU BPF maps value size has to be
1065 * a product of BPF map value size and number of possible CPUs in the system
1066 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1067 * per-CPU values value size has to be aligned up to closest 8 bytes for
1068 * alignment reasons, so expected size is: `round_up(value_size, 8)
1069 * * libbpf_num_possible_cpus()`.
1070 * @flags extra flags passed to kernel for this operation
1071 * @return 0, on success; negative error, otherwise
1073 * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
1074 * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
1076 LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
1077 const void *key, size_t key_sz,
1078 void *value, size_t value_sz, __u64 flags);
1081 * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
1082 * fetching next key that follows current key.
1083 * @param map BPF map to fetch next key from
1084 * @param cur_key pointer to memory containing bytes of current key or NULL to
1085 * fetch the first key
1086 * @param next_key pointer to memory to write next key into
1087 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1088 * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
1089 * negative error, otherwise
1091 * **bpf_map__get_next_key()** is high-level equivalent of
1092 * **bpf_map_get_next_key()** API with added check for key size.
1094 LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
1095 const void *cur_key, void *next_key, size_t key_sz);
1098 * @brief **libbpf_get_error()** extracts the error code from the passed
1100 * @param ptr pointer returned from libbpf API function
1101 * @return error code; or 0 if no error occured
1103 * Many libbpf API functions which return pointers have logic to encode error
1104 * codes as pointers, and do not return NULL. Meaning **libbpf_get_error()**
1105 * should be used on the return value from these functions immediately after
1106 * calling the API function, with no intervening calls that could clobber the
1107 * `errno` variable. Consult the individual functions documentation to verify
1108 * if this logic applies should be used.
1110 * For these API functions, if `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)`
1111 * is enabled, NULL is returned on error instead.
1113 * If ptr is NULL, then errno should be already set by the failing
1114 * API, because libbpf never returns NULL on success and it now always
1115 * sets errno on error.
1119 * struct perf_buffer *pb;
1121 * pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &opts);
1122 * err = libbpf_get_error(pb);
1125 * fprintf(stderr, "failed to open perf buffer: %d\n", err);
1129 LIBBPF_API long libbpf_get_error(const void *ptr);
1131 struct bpf_prog_load_attr {
1133 enum bpf_prog_type prog_type;
1134 enum bpf_attach_type expected_attach_type;
1140 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__open() and bpf_object__load() instead")
1141 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
1142 struct bpf_object **pobj, int *prog_fd);
1143 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open() and bpf_object__load() instead")
1144 LIBBPF_API int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type,
1145 struct bpf_object **pobj, int *prog_fd);
1147 /* XDP related API */
1148 struct xdp_link_info {
1156 struct bpf_xdp_set_link_opts {
1161 #define bpf_xdp_set_link_opts__last_field old_fd
1163 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_attach() instead")
1164 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
1165 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_attach() instead")
1166 LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
1167 const struct bpf_xdp_set_link_opts *opts);
1168 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_query_id() instead")
1169 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
1170 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_query() instead")
1171 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
1172 size_t info_size, __u32 flags);
1174 struct bpf_xdp_attach_opts {
1179 #define bpf_xdp_attach_opts__last_field old_prog_fd
1181 struct bpf_xdp_query_opts {
1183 __u32 prog_id; /* output */
1184 __u32 drv_prog_id; /* output */
1185 __u32 hw_prog_id; /* output */
1186 __u32 skb_prog_id; /* output */
1187 __u8 attach_mode; /* output */
1190 #define bpf_xdp_query_opts__last_field attach_mode
1192 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
1193 const struct bpf_xdp_attach_opts *opts);
1194 LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
1195 const struct bpf_xdp_attach_opts *opts);
1196 LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
1197 LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
1199 /* TC related API */
1200 enum bpf_tc_attach_point {
1201 BPF_TC_INGRESS = 1 << 0,
1202 BPF_TC_EGRESS = 1 << 1,
1203 BPF_TC_CUSTOM = 1 << 2,
1206 #define BPF_TC_PARENT(a, b) \
1207 ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
1210 BPF_TC_F_REPLACE = 1 << 0,
1213 struct bpf_tc_hook {
1216 enum bpf_tc_attach_point attach_point;
1220 #define bpf_tc_hook__last_field parent
1222 struct bpf_tc_opts {
1231 #define bpf_tc_opts__last_field priority
1233 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
1234 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
1235 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
1236 struct bpf_tc_opts *opts);
1237 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
1238 const struct bpf_tc_opts *opts);
1239 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
1240 struct bpf_tc_opts *opts);
1242 /* Ring buffer APIs */
1245 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
1247 struct ring_buffer_opts {
1248 size_t sz; /* size of this struct, for forward/backward compatiblity */
1251 #define ring_buffer_opts__last_field sz
1253 LIBBPF_API struct ring_buffer *
1254 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
1255 const struct ring_buffer_opts *opts);
1256 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
1257 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
1258 ring_buffer_sample_fn sample_cb, void *ctx);
1259 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
1260 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
1261 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
1263 /* Perf buffer APIs */
1266 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
1267 void *data, __u32 size);
1268 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
1270 /* common use perf buffer options */
1271 struct perf_buffer_opts {
1274 struct { /* DEPRECATED: will be removed in v1.0 */
1275 /* if specified, sample_cb is called for each sample */
1276 perf_buffer_sample_fn sample_cb;
1277 /* if specified, lost_cb is called for each batch of lost samples */
1278 perf_buffer_lost_fn lost_cb;
1279 /* ctx is provided to sample_cb and lost_cb */
1284 #define perf_buffer_opts__last_field sz
1287 * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1288 * BPF_PERF_EVENT_ARRAY map
1289 * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1290 * code to send data over to user-space
1291 * @param page_cnt number of memory pages allocated for each per-CPU buffer
1292 * @param sample_cb function called on each received data record
1293 * @param lost_cb function called when record loss has occurred
1294 * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1295 * @return a new instance of struct perf_buffer on success, NULL on error with
1296 * *errno* containing an error code
1298 LIBBPF_API struct perf_buffer *
1299 perf_buffer__new(int map_fd, size_t page_cnt,
1300 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1301 const struct perf_buffer_opts *opts);
1303 LIBBPF_API struct perf_buffer *
1304 perf_buffer__new_v0_6_0(int map_fd, size_t page_cnt,
1305 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1306 const struct perf_buffer_opts *opts);
1308 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new() instead")
1309 struct perf_buffer *perf_buffer__new_deprecated(int map_fd, size_t page_cnt,
1310 const struct perf_buffer_opts *opts);
1312 #define perf_buffer__new(...) ___libbpf_overload(___perf_buffer_new, __VA_ARGS__)
1313 #define ___perf_buffer_new6(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts) \
1314 perf_buffer__new(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts)
1315 #define ___perf_buffer_new3(map_fd, page_cnt, opts) \
1316 perf_buffer__new_deprecated(map_fd, page_cnt, opts)
1318 enum bpf_perf_event_ret {
1319 LIBBPF_PERF_EVENT_DONE = 0,
1320 LIBBPF_PERF_EVENT_ERROR = -1,
1321 LIBBPF_PERF_EVENT_CONT = -2,
1324 struct perf_event_header;
1326 typedef enum bpf_perf_event_ret
1327 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1329 /* raw perf buffer options, giving most power and control */
1330 struct perf_buffer_raw_opts {
1337 struct { /* DEPRECATED: will be removed in v1.0 */
1338 /* perf event attrs passed directly into perf_event_open() */
1339 struct perf_event_attr *attr;
1340 /* raw event callback */
1341 perf_buffer_event_fn event_cb;
1342 /* ctx is provided to event_cb */
1346 /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1347 * max_entries of given PERF_EVENT_ARRAY map)
1350 /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1352 /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1355 #define perf_buffer_raw_opts__last_field map_keys
1357 LIBBPF_API struct perf_buffer *
1358 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1359 perf_buffer_event_fn event_cb, void *ctx,
1360 const struct perf_buffer_raw_opts *opts);
1362 LIBBPF_API struct perf_buffer *
1363 perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1364 perf_buffer_event_fn event_cb, void *ctx,
1365 const struct perf_buffer_raw_opts *opts);
1367 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new_raw() instead")
1368 struct perf_buffer *perf_buffer__new_raw_deprecated(int map_fd, size_t page_cnt,
1369 const struct perf_buffer_raw_opts *opts);
1371 #define perf_buffer__new_raw(...) ___libbpf_overload(___perf_buffer_new_raw, __VA_ARGS__)
1372 #define ___perf_buffer_new_raw6(map_fd, page_cnt, attr, event_cb, ctx, opts) \
1373 perf_buffer__new_raw(map_fd, page_cnt, attr, event_cb, ctx, opts)
1374 #define ___perf_buffer_new_raw3(map_fd, page_cnt, opts) \
1375 perf_buffer__new_raw_deprecated(map_fd, page_cnt, opts)
1377 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1378 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1379 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1380 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1381 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1382 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1383 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1385 typedef enum bpf_perf_event_ret
1386 (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
1387 void *private_data);
1388 LIBBPF_DEPRECATED_SINCE(0, 8, "use perf_buffer__poll() or perf_buffer__consume() instead")
1389 LIBBPF_API enum bpf_perf_event_ret
1390 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
1391 void **copy_mem, size_t *copy_size,
1392 bpf_perf_event_print_t fn, void *private_data);
1394 struct bpf_prog_linfo;
1395 struct bpf_prog_info;
1397 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1398 LIBBPF_API struct bpf_prog_linfo *
1399 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1400 LIBBPF_API const struct bpf_line_info *
1401 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1402 __u64 addr, __u32 func_idx, __u32 nr_skip);
1403 LIBBPF_API const struct bpf_line_info *
1404 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1405 __u32 insn_off, __u32 nr_skip);
1408 * Probe for supported system features
1410 * Note that running many of these probes in a short amount of time can cause
1411 * the kernel to reach the maximal size of lockable memory allowed for the
1412 * user, causing subsequent probes to fail. In this case, the caller may want
1413 * to adjust that limit with setrlimit().
1415 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_prog_type() instead")
1416 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex);
1417 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_map_type() instead")
1418 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
1419 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_helper() instead")
1420 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type, __u32 ifindex);
1421 LIBBPF_DEPRECATED_SINCE(0, 8, "implement your own or use bpftool for feature detection")
1422 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
1425 * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1426 * BPF programs of a given type.
1427 * @param prog_type BPF program type to detect kernel support for
1428 * @param opts reserved for future extensibility, should be NULL
1429 * @return 1, if given program type is supported; 0, if given program type is
1430 * not supported; negative error code if feature detection failed or can't be
1433 * Make sure the process has required set of CAP_* permissions (or runs as
1434 * root) when performing feature checking.
1436 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1438 * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1439 * BPF maps of a given type.
1440 * @param map_type BPF map type to detect kernel support for
1441 * @param opts reserved for future extensibility, should be NULL
1442 * @return 1, if given map type is supported; 0, if given map type is
1443 * not supported; negative error code if feature detection failed or can't be
1446 * Make sure the process has required set of CAP_* permissions (or runs as
1447 * root) when performing feature checking.
1449 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1451 * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1452 * use of a given BPF helper from specified BPF program type.
1453 * @param prog_type BPF program type used to check the support of BPF helper
1454 * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1455 * @param opts reserved for future extensibility, should be NULL
1456 * @return 1, if given combination of program type and helper is supported; 0,
1457 * if the combination is not supported; negative error code if feature
1458 * detection for provided input arguments failed or can't be performed
1460 * Make sure the process has required set of CAP_* permissions (or runs as
1461 * root) when performing feature checking.
1463 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1464 enum bpf_func_id helper_id, const void *opts);
1467 * Get bpf_prog_info in continuous memory
1469 * struct bpf_prog_info has multiple arrays. The user has option to choose
1470 * arrays to fetch from kernel. The following APIs provide an uniform way to
1471 * fetch these data. All arrays in bpf_prog_info are stored in a single
1472 * continuous memory region. This makes it easy to store the info in a
1475 * Before writing bpf_prog_info_linear to files, it is necessary to
1476 * translate pointers in bpf_prog_info to offsets. Helper functions
1477 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
1478 * are introduced to switch between pointers and offsets.
1481 * # To fetch map_ids and prog_tags:
1482 * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
1483 * (1UL << BPF_PROG_INFO_PROG_TAGS);
1484 * struct bpf_prog_info_linear *info_linear =
1485 * bpf_program__get_prog_info_linear(fd, arrays);
1487 * # To save data in file
1488 * bpf_program__bpil_addr_to_offs(info_linear);
1489 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
1491 * # To read data from file
1492 * read(f, info_linear, <proper_size>);
1493 * bpf_program__bpil_offs_to_addr(info_linear);
1495 enum bpf_prog_info_array {
1496 BPF_PROG_INFO_FIRST_ARRAY = 0,
1497 BPF_PROG_INFO_JITED_INSNS = 0,
1498 BPF_PROG_INFO_XLATED_INSNS,
1499 BPF_PROG_INFO_MAP_IDS,
1500 BPF_PROG_INFO_JITED_KSYMS,
1501 BPF_PROG_INFO_JITED_FUNC_LENS,
1502 BPF_PROG_INFO_FUNC_INFO,
1503 BPF_PROG_INFO_LINE_INFO,
1504 BPF_PROG_INFO_JITED_LINE_INFO,
1505 BPF_PROG_INFO_PROG_TAGS,
1506 BPF_PROG_INFO_LAST_ARRAY,
1509 struct bpf_prog_info_linear {
1510 /* size of struct bpf_prog_info, when the tool is compiled */
1512 /* total bytes allocated for data, round up to 8 bytes */
1514 /* which arrays are included in data */
1516 struct bpf_prog_info info;
1520 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1521 LIBBPF_API struct bpf_prog_info_linear *
1522 bpf_program__get_prog_info_linear(int fd, __u64 arrays);
1524 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1526 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
1528 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1530 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
1533 * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1534 * number of possible CPUs that the host kernel supports and expects.
1535 * @return number of possible CPUs; or error code on failure
1539 * int ncpus = libbpf_num_possible_cpus();
1543 * long values[ncpus];
1544 * bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1546 LIBBPF_API int libbpf_num_possible_cpus(void);
1548 struct bpf_map_skeleton {
1550 struct bpf_map **map;
1554 struct bpf_prog_skeleton {
1556 struct bpf_program **prog;
1557 struct bpf_link **link;
1560 struct bpf_object_skeleton {
1561 size_t sz; /* size of this struct, for forward/backward compatibility */
1567 struct bpf_object **obj;
1570 int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1571 struct bpf_map_skeleton *maps;
1574 int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1575 struct bpf_prog_skeleton *progs;
1579 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1580 const struct bpf_object_open_opts *opts);
1581 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1582 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1583 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1584 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1586 struct bpf_var_skeleton {
1588 struct bpf_map **map;
1592 struct bpf_object_subskeleton {
1593 size_t sz; /* size of this struct, for forward/backward compatibility */
1595 const struct bpf_object *obj;
1598 int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1599 struct bpf_map_skeleton *maps;
1602 int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1603 struct bpf_prog_skeleton *progs;
1606 int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1607 struct bpf_var_skeleton *vars;
1611 bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1613 bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1615 struct gen_loader_opts {
1616 size_t sz; /* size of this struct, for forward/backward compatiblity */
1623 #define gen_loader_opts__last_field insns_sz
1624 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1625 struct gen_loader_opts *opts);
1627 enum libbpf_tristate {
1633 struct bpf_linker_opts {
1634 /* size of this struct, for forward/backward compatiblity */
1637 #define bpf_linker_opts__last_field sz
1639 struct bpf_linker_file_opts {
1640 /* size of this struct, for forward/backward compatiblity */
1643 #define bpf_linker_file_opts__last_field sz
1647 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1648 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1649 const char *filename,
1650 const struct bpf_linker_file_opts *opts);
1651 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1652 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1655 * Custom handling of BPF program's SEC() definitions
1658 struct bpf_prog_load_opts; /* defined in bpf.h */
1660 /* Called during bpf_object__open() for each recognized BPF program. Callback
1661 * can use various bpf_program__set_*() setters to adjust whatever properties
1664 typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1666 /* Called right before libbpf performs bpf_prog_load() to load BPF program
1667 * into the kernel. Callback can adjust opts as necessary.
1669 typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1670 struct bpf_prog_load_opts *opts, long cookie);
1672 /* Called during skeleton attach or through bpf_program__attach(). If
1673 * auto-attach is not supported, callback should return 0 and set link to
1674 * NULL (it's not considered an error during skeleton attach, but it will be
1675 * an error for bpf_program__attach() calls). On error, error should be
1676 * returned directly and link set to NULL. On success, return 0 and set link
1677 * to a valid struct bpf_link.
1679 typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1680 struct bpf_link **link);
1682 struct libbpf_prog_handler_opts {
1683 /* size of this struct, for forward/backward compatiblity */
1685 /* User-provided value that is passed to prog_setup_fn,
1686 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1687 * register one set of callbacks for multiple SEC() definitions and
1688 * still be able to distinguish them, if necessary. For example,
1689 * libbpf itself is using this to pass necessary flags (e.g.,
1690 * sleepable flag) to a common internal SEC() handler.
1693 /* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1694 * Callback is optional, pass NULL if it's not necessary.
1696 libbpf_prog_setup_fn_t prog_setup_fn;
1697 /* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1698 * Callback is optional, pass NULL if it's not necessary.
1700 libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1701 /* BPF program attach callback (see libbpf_prog_attach_fn_t).
1702 * Callback is optional, pass NULL if it's not necessary.
1704 libbpf_prog_attach_fn_t prog_attach_fn;
1706 #define libbpf_prog_handler_opts__last_field prog_attach_fn
1709 * @brief **libbpf_register_prog_handler()** registers a custom BPF program
1711 * @param sec section prefix for which custom handler is registered
1712 * @param prog_type BPF program type associated with specified section
1713 * @param exp_attach_type Expected BPF attach type associated with specified section
1714 * @param opts optional cookie, callbacks, and other extra options
1715 * @return Non-negative handler ID is returned on success. This handler ID has
1716 * to be passed to *libbpf_unregister_prog_handler()* to unregister such
1717 * custom handler. Negative error code is returned on error.
1719 * *sec* defines which SEC() definitions are handled by this custom handler
1720 * registration. *sec* can have few different forms:
1721 * - if *sec* is just a plain string (e.g., "abc"), it will match only
1722 * SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1724 * - if *sec* is of the form "abc/", proper SEC() form is
1725 * SEC("abc/something"), where acceptable "something" should be checked by
1726 * *prog_init_fn* callback, if there are additional restrictions;
1727 * - if *sec* is of the form "abc+", it will successfully match both
1728 * SEC("abc") and SEC("abc/whatever") forms;
1729 * - if *sec* is NULL, custom handler is registered for any BPF program that
1730 * doesn't match any of the registered (custom or libbpf's own) SEC()
1731 * handlers. There could be only one such generic custom handler registered
1732 * at any given time.
1734 * All custom handlers (except the one with *sec* == NULL) are processed
1735 * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1736 * SEC() handlers by registering custom ones for the same section prefix
1737 * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1740 * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1741 * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1742 * to ensure synchronization if there is a risk of running this API from
1743 * multiple threads simultaneously.
1745 LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1746 enum bpf_prog_type prog_type,
1747 enum bpf_attach_type exp_attach_type,
1748 const struct libbpf_prog_handler_opts *opts);
1750 * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1751 * custom BPF program SEC() handler.
1752 * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1753 * after successful registration
1754 * @return 0 on success, negative error code if handler isn't found
1756 * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1757 * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1758 * to ensure synchronization if there is a risk of running this API from
1759 * multiple threads simultaneously.
1761 LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1767 #endif /* __LIBBPF_LIBBPF_H */