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_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
250 bpf_object_clear_priv_t clear_priv);
251 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
254 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
255 enum bpf_attach_type *expected_attach_type);
256 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
257 enum bpf_attach_type *attach_type);
258 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
259 enum bpf_attach_type attach_type);
261 /* Accessors of bpf_program */
263 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_program() instead")
264 struct bpf_program *bpf_program__next(struct bpf_program *prog,
265 const struct bpf_object *obj);
266 LIBBPF_API struct bpf_program *
267 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
269 #define bpf_object__for_each_program(pos, obj) \
270 for ((pos) = bpf_object__next_program((obj), NULL); \
272 (pos) = bpf_object__next_program((obj), (pos)))
274 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_program() instead")
275 struct bpf_program *bpf_program__prev(struct bpf_program *prog,
276 const struct bpf_object *obj);
277 LIBBPF_API struct bpf_program *
278 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
280 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
282 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
283 bpf_program_clear_priv_t clear_priv);
285 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
286 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
289 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
290 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
291 LIBBPF_API LIBBPF_DEPRECATED("BPF program title is confusing term; please use bpf_program__section_name() instead")
292 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy);
293 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
294 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
296 /* returns program size in bytes */
297 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insn_cnt() instead")
298 LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
303 * @brief **bpf_program__insns()** gives read-only access to BPF program's
304 * underlying BPF instructions.
305 * @param prog BPF program for which to return instructions
306 * @return a pointer to an array of BPF instructions that belong to the
307 * specified BPF program
309 * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
310 * pointed to can be fetched using **bpf_program__insn_cnt()** API.
312 * Keep in mind, libbpf can modify and append/delete BPF program's
313 * instructions as it processes BPF object file and prepares everything for
314 * uploading into the kernel. So depending on the point in BPF object
315 * lifetime, **bpf_program__insns()** can return different sets of
316 * instructions. As an example, during BPF object load phase BPF program
317 * instructions will be CO-RE-relocated, BPF subprograms instructions will be
318 * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
319 * returned before **bpf_object__load()** and after it might be quite
322 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
324 * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
325 * that form specified BPF program.
326 * @param prog BPF program for which to return number of BPF instructions
328 * See **bpf_program__insns()** documentation for notes on how libbpf can
329 * change instructions and their count during different phases of
330 * **bpf_object** lifetime.
332 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
334 LIBBPF_DEPRECATED_SINCE(0, 6, "use bpf_object__load() instead")
335 LIBBPF_API int bpf_program__load(struct bpf_program *prog, const char *license, __u32 kern_version);
336 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
337 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
338 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
341 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
342 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
347 * @brief **bpf_program__pin()** pins the BPF program to a file
348 * in the BPF FS specified by a path. This increments the programs
349 * reference count, allowing it to stay loaded after the process
350 * which loaded it has exited.
352 * @param prog BPF program to pin, must already be loaded
353 * @param path file path in a BPF file system
354 * @return 0, on success; negative error code, otherwise
356 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
359 * @brief **bpf_program__unpin()** unpins the BPF program from a file
360 * in the BPFFS specified by a path. This decrements the programs
363 * The file pinning the BPF program can also be unlinked by a different
364 * process in which case this function will return an error.
366 * @param prog BPF program to unpin
367 * @param path file path to the pin in a BPF file system
368 * @return 0, on success; negative error code, otherwise
370 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
371 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
375 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
376 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
377 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
378 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
379 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
380 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
381 struct bpf_program *prog);
382 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
383 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
384 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
386 LIBBPF_API struct bpf_link *
387 bpf_program__attach(const struct bpf_program *prog);
389 struct bpf_perf_event_opts {
390 /* size of this struct, for forward/backward compatiblity */
392 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
395 #define bpf_perf_event_opts__last_field bpf_cookie
397 LIBBPF_API struct bpf_link *
398 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
400 LIBBPF_API struct bpf_link *
401 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
402 const struct bpf_perf_event_opts *opts);
404 struct bpf_kprobe_opts {
405 /* size of this struct, for forward/backward compatiblity */
407 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
409 /* function's offset to install kprobe to */
411 /* kprobe is return probe */
415 #define bpf_kprobe_opts__last_field retprobe
417 LIBBPF_API struct bpf_link *
418 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
419 const char *func_name);
420 LIBBPF_API struct bpf_link *
421 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
422 const char *func_name,
423 const struct bpf_kprobe_opts *opts);
425 struct bpf_uprobe_opts {
426 /* size of this struct, for forward/backward compatiblity */
428 /* offset of kernel reference counted USDT semaphore, added in
429 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
431 size_t ref_ctr_offset;
432 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
434 /* uprobe is return probe, invoked at function return time */
438 #define bpf_uprobe_opts__last_field retprobe
441 * @brief **bpf_program__attach_uprobe()** attaches a BPF program
442 * to the userspace function which is found by binary path and
443 * offset. You can optionally specify a particular proccess to attach
444 * to. You can also optionally attach the program to the function
445 * exit instead of entry.
447 * @param prog BPF program to attach
448 * @param retprobe Attach to function exit
449 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
450 * -1 for all processes
451 * @param binary_path Path to binary that contains the function symbol
452 * @param func_offset Offset within the binary of the function symbol
453 * @return Reference to the newly created BPF link; or NULL is returned on error,
454 * error code is stored in errno
456 LIBBPF_API struct bpf_link *
457 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
458 pid_t pid, const char *binary_path,
462 * @brief **bpf_program__attach_uprobe_opts()** is just like
463 * bpf_program__attach_uprobe() except with a options struct
464 * for various configurations.
466 * @param prog BPF program to attach
467 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
468 * -1 for all processes
469 * @param binary_path Path to binary that contains the function symbol
470 * @param func_offset Offset within the binary of the function symbol
471 * @param opts Options for altering program attachment
472 * @return Reference to the newly created BPF link; or NULL is returned on error,
473 * error code is stored in errno
475 LIBBPF_API struct bpf_link *
476 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
477 const char *binary_path, size_t func_offset,
478 const struct bpf_uprobe_opts *opts);
480 struct bpf_tracepoint_opts {
481 /* size of this struct, for forward/backward compatiblity */
483 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
486 #define bpf_tracepoint_opts__last_field bpf_cookie
488 LIBBPF_API struct bpf_link *
489 bpf_program__attach_tracepoint(const struct bpf_program *prog,
490 const char *tp_category,
491 const char *tp_name);
492 LIBBPF_API struct bpf_link *
493 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
494 const char *tp_category,
496 const struct bpf_tracepoint_opts *opts);
498 LIBBPF_API struct bpf_link *
499 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
500 const char *tp_name);
501 LIBBPF_API struct bpf_link *
502 bpf_program__attach_trace(const struct bpf_program *prog);
503 LIBBPF_API struct bpf_link *
504 bpf_program__attach_lsm(const struct bpf_program *prog);
505 LIBBPF_API struct bpf_link *
506 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
507 LIBBPF_API struct bpf_link *
508 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
509 LIBBPF_API struct bpf_link *
510 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
511 LIBBPF_API struct bpf_link *
512 bpf_program__attach_freplace(const struct bpf_program *prog,
513 int target_fd, const char *attach_func_name);
517 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
519 struct bpf_iter_attach_opts {
520 size_t sz; /* size of this struct for forward/backward compatibility */
521 union bpf_iter_link_info *link_info;
524 #define bpf_iter_attach_opts__last_field link_info_len
526 LIBBPF_API struct bpf_link *
527 bpf_program__attach_iter(const struct bpf_program *prog,
528 const struct bpf_iter_attach_opts *opts);
531 * Libbpf allows callers to adjust BPF programs before being loaded
532 * into kernel. One program in an object file can be transformed into
533 * multiple variants to be attached to different hooks.
535 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
536 * form an API for this purpose.
538 * - bpf_program_prep_t:
539 * Defines a 'preprocessor', which is a caller defined function
540 * passed to libbpf through bpf_program__set_prep(), and will be
541 * called before program is loaded. The processor should adjust
542 * the program one time for each instance according to the instance id
545 * - bpf_program__set_prep:
546 * Attaches a preprocessor to a BPF program. The number of instances
547 * that should be created is also passed through this function.
549 * - bpf_program__nth_fd:
550 * After the program is loaded, get resulting FD of a given instance
551 * of the BPF program.
553 * If bpf_program__set_prep() is not used, the program would be loaded
554 * without adjustment during bpf_object__load(). The program has only
555 * one instance. In this case bpf_program__fd(prog) is equal to
556 * bpf_program__nth_fd(prog, 0).
558 struct bpf_prog_prep_result {
560 * If not NULL, load new instruction array.
561 * If set to NULL, don't load this instance.
563 struct bpf_insn *new_insn_ptr;
566 /* If not NULL, result FD is written to it. */
571 * Parameters of bpf_program_prep_t:
572 * - prog: The bpf_program being loaded.
573 * - n: Index of instance being generated.
574 * - insns: BPF instructions array.
575 * - insns_cnt:Number of instructions in insns.
576 * - res: Output parameter, result of transformation.
579 * - Zero: pre-processing success.
580 * - Non-zero: pre-processing error, stop loading.
582 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
583 struct bpf_insn *insns, int insns_cnt,
584 struct bpf_prog_prep_result *res);
586 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions")
587 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
588 bpf_program_prep_t prep);
590 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
591 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
594 * Adjust type of BPF program. Default is kprobe.
596 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
597 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
598 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
599 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
600 LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
601 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
602 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
603 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
604 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
605 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
606 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
607 LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog);
608 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
610 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
611 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
612 enum bpf_prog_type type);
614 LIBBPF_API enum bpf_attach_type
615 bpf_program__expected_attach_type(const struct bpf_program *prog);
617 bpf_program__set_expected_attach_type(struct bpf_program *prog,
618 enum bpf_attach_type type);
620 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
621 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
623 /* Per-program log level and log buffer getters/setters.
624 * See bpf_object_open_opts comments regarding log_level and log_buf
627 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
628 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
629 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
630 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
633 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
634 const char *attach_func_name);
636 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
637 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
638 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
639 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
640 LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
641 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
642 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
643 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
644 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
645 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
646 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
647 LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog);
648 LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog);
651 * No need for __attribute__((packed)), all members of 'bpf_map_def'
652 * are all aligned. In addition, using __attribute__((packed))
653 * would trigger a -Wpacked warning message, and lead to an error
658 unsigned int key_size;
659 unsigned int value_size;
660 unsigned int max_entries;
661 unsigned int map_flags;
665 * @brief **bpf_object__find_map_by_name()** returns BPF map of
666 * the given name, if it exists within the passed BPF object
667 * @param obj BPF object
668 * @param name name of the BPF map
669 * @return BPF map instance, if such map exists within the BPF object;
672 LIBBPF_API struct bpf_map *
673 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
676 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
679 * Get bpf_map through the offset of corresponding struct bpf_map_def
680 * in the BPF object file.
682 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__find_map_by_name() instead")
684 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
686 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead")
687 struct bpf_map *bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
688 LIBBPF_API struct bpf_map *
689 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
691 #define bpf_object__for_each_map(pos, obj) \
692 for ((pos) = bpf_object__next_map((obj), NULL); \
694 (pos) = bpf_object__next_map((obj), (pos)))
695 #define bpf_map__for_each bpf_object__for_each_map
697 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_map() instead")
698 struct bpf_map *bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
699 LIBBPF_API struct bpf_map *
700 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
703 * @brief **bpf_map__fd()** gets the file descriptor of the passed
705 * @param map the BPF map instance
706 * @return the file descriptor; or -EINVAL in case of an error
708 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
709 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
710 /* get map definition */
711 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 8, "use appropriate getters or setters instead")
712 const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
714 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
715 /* get/set map type */
716 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
717 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
718 /* get/set map size (max_entries) */
719 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
720 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
721 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
722 /* get/set map flags */
723 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
724 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
725 /* get/set map NUMA node */
726 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
727 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
728 /* get/set map key size */
729 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
730 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
731 /* get/set map value size */
732 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
733 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
734 /* get map key/value BTF type IDs */
735 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
736 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
737 /* get/set map if_index */
738 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
739 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
740 /* get/set map map_extra flags */
741 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
742 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
744 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
745 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
746 bpf_map_clear_priv_t clear_priv);
747 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
748 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
749 const void *data, size_t size);
750 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
751 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_map__type() instead")
752 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
755 * @brief **bpf_map__is_internal()** tells the caller whether or not the
756 * passed map is a special map created by libbpf automatically for things like
757 * global variables, __ksym externs, Kconfig values, etc
758 * @param map the bpf_map
759 * @return true, if the map is an internal map; false, otherwise
761 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
762 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
763 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
764 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
765 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
766 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
768 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
769 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
772 * @brief **libbpf_get_error()** extracts the error code from the passed
774 * @param ptr pointer returned from libbpf API function
775 * @return error code; or 0 if no error occured
777 * Many libbpf API functions which return pointers have logic to encode error
778 * codes as pointers, and do not return NULL. Meaning **libbpf_get_error()**
779 * should be used on the return value from these functions immediately after
780 * calling the API function, with no intervening calls that could clobber the
781 * `errno` variable. Consult the individual functions documentation to verify
782 * if this logic applies should be used.
784 * For these API functions, if `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)`
785 * is enabled, NULL is returned on error instead.
787 * If ptr is NULL, then errno should be already set by the failing
788 * API, because libbpf never returns NULL on success and it now always
789 * sets errno on error.
793 * struct perf_buffer *pb;
795 * pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &opts);
796 * err = libbpf_get_error(pb);
799 * fprintf(stderr, "failed to open perf buffer: %d\n", err);
803 LIBBPF_API long libbpf_get_error(const void *ptr);
805 struct bpf_prog_load_attr {
807 enum bpf_prog_type prog_type;
808 enum bpf_attach_type expected_attach_type;
814 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__open() and bpf_object__load() instead")
815 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
816 struct bpf_object **pobj, int *prog_fd);
817 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open() and bpf_object__load() instead")
818 LIBBPF_API int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type,
819 struct bpf_object **pobj, int *prog_fd);
821 /* XDP related API */
822 struct xdp_link_info {
830 struct bpf_xdp_set_link_opts {
835 #define bpf_xdp_set_link_opts__last_field old_fd
837 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_attach() instead")
838 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
839 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_attach() instead")
840 LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
841 const struct bpf_xdp_set_link_opts *opts);
842 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_query_id() instead")
843 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
844 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_query() instead")
845 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
846 size_t info_size, __u32 flags);
848 struct bpf_xdp_attach_opts {
853 #define bpf_xdp_attach_opts__last_field old_prog_fd
855 struct bpf_xdp_query_opts {
857 __u32 prog_id; /* output */
858 __u32 drv_prog_id; /* output */
859 __u32 hw_prog_id; /* output */
860 __u32 skb_prog_id; /* output */
861 __u8 attach_mode; /* output */
864 #define bpf_xdp_query_opts__last_field attach_mode
866 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
867 const struct bpf_xdp_attach_opts *opts);
868 LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
869 const struct bpf_xdp_attach_opts *opts);
870 LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
871 LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
874 enum bpf_tc_attach_point {
875 BPF_TC_INGRESS = 1 << 0,
876 BPF_TC_EGRESS = 1 << 1,
877 BPF_TC_CUSTOM = 1 << 2,
880 #define BPF_TC_PARENT(a, b) \
881 ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
884 BPF_TC_F_REPLACE = 1 << 0,
890 enum bpf_tc_attach_point attach_point;
894 #define bpf_tc_hook__last_field parent
905 #define bpf_tc_opts__last_field priority
907 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
908 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
909 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
910 struct bpf_tc_opts *opts);
911 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
912 const struct bpf_tc_opts *opts);
913 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
914 struct bpf_tc_opts *opts);
916 /* Ring buffer APIs */
919 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
921 struct ring_buffer_opts {
922 size_t sz; /* size of this struct, for forward/backward compatiblity */
925 #define ring_buffer_opts__last_field sz
927 LIBBPF_API struct ring_buffer *
928 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
929 const struct ring_buffer_opts *opts);
930 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
931 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
932 ring_buffer_sample_fn sample_cb, void *ctx);
933 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
934 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
935 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
937 /* Perf buffer APIs */
940 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
941 void *data, __u32 size);
942 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
944 /* common use perf buffer options */
945 struct perf_buffer_opts {
948 struct { /* DEPRECATED: will be removed in v1.0 */
949 /* if specified, sample_cb is called for each sample */
950 perf_buffer_sample_fn sample_cb;
951 /* if specified, lost_cb is called for each batch of lost samples */
952 perf_buffer_lost_fn lost_cb;
953 /* ctx is provided to sample_cb and lost_cb */
958 #define perf_buffer_opts__last_field sz
961 * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
962 * BPF_PERF_EVENT_ARRAY map
963 * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
964 * code to send data over to user-space
965 * @param page_cnt number of memory pages allocated for each per-CPU buffer
966 * @param sample_cb function called on each received data record
967 * @param lost_cb function called when record loss has occurred
968 * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
969 * @return a new instance of struct perf_buffer on success, NULL on error with
970 * *errno* containing an error code
972 LIBBPF_API struct perf_buffer *
973 perf_buffer__new(int map_fd, size_t page_cnt,
974 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
975 const struct perf_buffer_opts *opts);
977 LIBBPF_API struct perf_buffer *
978 perf_buffer__new_v0_6_0(int map_fd, size_t page_cnt,
979 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
980 const struct perf_buffer_opts *opts);
982 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new() instead")
983 struct perf_buffer *perf_buffer__new_deprecated(int map_fd, size_t page_cnt,
984 const struct perf_buffer_opts *opts);
986 #define perf_buffer__new(...) ___libbpf_overload(___perf_buffer_new, __VA_ARGS__)
987 #define ___perf_buffer_new6(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts) \
988 perf_buffer__new(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts)
989 #define ___perf_buffer_new3(map_fd, page_cnt, opts) \
990 perf_buffer__new_deprecated(map_fd, page_cnt, opts)
992 enum bpf_perf_event_ret {
993 LIBBPF_PERF_EVENT_DONE = 0,
994 LIBBPF_PERF_EVENT_ERROR = -1,
995 LIBBPF_PERF_EVENT_CONT = -2,
998 struct perf_event_header;
1000 typedef enum bpf_perf_event_ret
1001 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1003 /* raw perf buffer options, giving most power and control */
1004 struct perf_buffer_raw_opts {
1011 struct { /* DEPRECATED: will be removed in v1.0 */
1012 /* perf event attrs passed directly into perf_event_open() */
1013 struct perf_event_attr *attr;
1014 /* raw event callback */
1015 perf_buffer_event_fn event_cb;
1016 /* ctx is provided to event_cb */
1020 /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1021 * max_entries of given PERF_EVENT_ARRAY map)
1024 /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1026 /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1029 #define perf_buffer_raw_opts__last_field map_keys
1031 LIBBPF_API struct perf_buffer *
1032 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1033 perf_buffer_event_fn event_cb, void *ctx,
1034 const struct perf_buffer_raw_opts *opts);
1036 LIBBPF_API struct perf_buffer *
1037 perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1038 perf_buffer_event_fn event_cb, void *ctx,
1039 const struct perf_buffer_raw_opts *opts);
1041 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new_raw() instead")
1042 struct perf_buffer *perf_buffer__new_raw_deprecated(int map_fd, size_t page_cnt,
1043 const struct perf_buffer_raw_opts *opts);
1045 #define perf_buffer__new_raw(...) ___libbpf_overload(___perf_buffer_new_raw, __VA_ARGS__)
1046 #define ___perf_buffer_new_raw6(map_fd, page_cnt, attr, event_cb, ctx, opts) \
1047 perf_buffer__new_raw(map_fd, page_cnt, attr, event_cb, ctx, opts)
1048 #define ___perf_buffer_new_raw3(map_fd, page_cnt, opts) \
1049 perf_buffer__new_raw_deprecated(map_fd, page_cnt, opts)
1051 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1052 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1053 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1054 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1055 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1056 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1057 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1059 typedef enum bpf_perf_event_ret
1060 (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
1061 void *private_data);
1062 LIBBPF_DEPRECATED_SINCE(0, 8, "use perf_buffer__poll() or perf_buffer__consume() instead")
1063 LIBBPF_API enum bpf_perf_event_ret
1064 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
1065 void **copy_mem, size_t *copy_size,
1066 bpf_perf_event_print_t fn, void *private_data);
1068 struct bpf_prog_linfo;
1069 struct bpf_prog_info;
1071 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1072 LIBBPF_API struct bpf_prog_linfo *
1073 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1074 LIBBPF_API const struct bpf_line_info *
1075 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1076 __u64 addr, __u32 func_idx, __u32 nr_skip);
1077 LIBBPF_API const struct bpf_line_info *
1078 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1079 __u32 insn_off, __u32 nr_skip);
1082 * Probe for supported system features
1084 * Note that running many of these probes in a short amount of time can cause
1085 * the kernel to reach the maximal size of lockable memory allowed for the
1086 * user, causing subsequent probes to fail. In this case, the caller may want
1087 * to adjust that limit with setrlimit().
1089 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_prog_type() instead")
1090 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex);
1091 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_map_type() instead")
1092 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
1093 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_helper() instead")
1094 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type, __u32 ifindex);
1095 LIBBPF_DEPRECATED_SINCE(0, 8, "implement your own or use bpftool for feature detection")
1096 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
1099 * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1100 * BPF programs of a given type.
1101 * @param prog_type BPF program type to detect kernel support for
1102 * @param opts reserved for future extensibility, should be NULL
1103 * @return 1, if given program type is supported; 0, if given program type is
1104 * not supported; negative error code if feature detection failed or can't be
1107 * Make sure the process has required set of CAP_* permissions (or runs as
1108 * root) when performing feature checking.
1110 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1112 * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1113 * BPF maps of a given type.
1114 * @param map_type BPF map type to detect kernel support for
1115 * @param opts reserved for future extensibility, should be NULL
1116 * @return 1, if given map type is supported; 0, if given map type is
1117 * not supported; negative error code if feature detection failed or can't be
1120 * Make sure the process has required set of CAP_* permissions (or runs as
1121 * root) when performing feature checking.
1123 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1125 * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1126 * use of a given BPF helper from specified BPF program type.
1127 * @param prog_type BPF program type used to check the support of BPF helper
1128 * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1129 * @param opts reserved for future extensibility, should be NULL
1130 * @return 1, if given combination of program type and helper is supported; 0,
1131 * if the combination is not supported; negative error code if feature
1132 * detection for provided input arguments failed or can't be performed
1134 * Make sure the process has required set of CAP_* permissions (or runs as
1135 * root) when performing feature checking.
1137 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1138 enum bpf_func_id helper_id, const void *opts);
1141 * Get bpf_prog_info in continuous memory
1143 * struct bpf_prog_info has multiple arrays. The user has option to choose
1144 * arrays to fetch from kernel. The following APIs provide an uniform way to
1145 * fetch these data. All arrays in bpf_prog_info are stored in a single
1146 * continuous memory region. This makes it easy to store the info in a
1149 * Before writing bpf_prog_info_linear to files, it is necessary to
1150 * translate pointers in bpf_prog_info to offsets. Helper functions
1151 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
1152 * are introduced to switch between pointers and offsets.
1155 * # To fetch map_ids and prog_tags:
1156 * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
1157 * (1UL << BPF_PROG_INFO_PROG_TAGS);
1158 * struct bpf_prog_info_linear *info_linear =
1159 * bpf_program__get_prog_info_linear(fd, arrays);
1161 * # To save data in file
1162 * bpf_program__bpil_addr_to_offs(info_linear);
1163 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
1165 * # To read data from file
1166 * read(f, info_linear, <proper_size>);
1167 * bpf_program__bpil_offs_to_addr(info_linear);
1169 enum bpf_prog_info_array {
1170 BPF_PROG_INFO_FIRST_ARRAY = 0,
1171 BPF_PROG_INFO_JITED_INSNS = 0,
1172 BPF_PROG_INFO_XLATED_INSNS,
1173 BPF_PROG_INFO_MAP_IDS,
1174 BPF_PROG_INFO_JITED_KSYMS,
1175 BPF_PROG_INFO_JITED_FUNC_LENS,
1176 BPF_PROG_INFO_FUNC_INFO,
1177 BPF_PROG_INFO_LINE_INFO,
1178 BPF_PROG_INFO_JITED_LINE_INFO,
1179 BPF_PROG_INFO_PROG_TAGS,
1180 BPF_PROG_INFO_LAST_ARRAY,
1183 struct bpf_prog_info_linear {
1184 /* size of struct bpf_prog_info, when the tool is compiled */
1186 /* total bytes allocated for data, round up to 8 bytes */
1188 /* which arrays are included in data */
1190 struct bpf_prog_info info;
1194 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1195 LIBBPF_API struct bpf_prog_info_linear *
1196 bpf_program__get_prog_info_linear(int fd, __u64 arrays);
1198 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1200 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
1202 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1204 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
1207 * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1208 * number of possible CPUs that the host kernel supports and expects.
1209 * @return number of possible CPUs; or error code on failure
1213 * int ncpus = libbpf_num_possible_cpus();
1217 * long values[ncpus];
1218 * bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1220 LIBBPF_API int libbpf_num_possible_cpus(void);
1222 struct bpf_map_skeleton {
1224 struct bpf_map **map;
1228 struct bpf_prog_skeleton {
1230 struct bpf_program **prog;
1231 struct bpf_link **link;
1234 struct bpf_object_skeleton {
1235 size_t sz; /* size of this struct, for forward/backward compatibility */
1241 struct bpf_object **obj;
1244 int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1245 struct bpf_map_skeleton *maps;
1248 int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1249 struct bpf_prog_skeleton *progs;
1253 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1254 const struct bpf_object_open_opts *opts);
1255 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1256 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1257 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1258 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1260 struct gen_loader_opts {
1261 size_t sz; /* size of this struct, for forward/backward compatiblity */
1268 #define gen_loader_opts__last_field insns_sz
1269 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1270 struct gen_loader_opts *opts);
1272 enum libbpf_tristate {
1278 struct bpf_linker_opts {
1279 /* size of this struct, for forward/backward compatiblity */
1282 #define bpf_linker_opts__last_field sz
1284 struct bpf_linker_file_opts {
1285 /* size of this struct, for forward/backward compatiblity */
1288 #define bpf_linker_file_opts__last_field sz
1292 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1293 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1294 const char *filename,
1295 const struct bpf_linker_file_opts *opts);
1296 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1297 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1303 #endif /* __LIBBPF_LIBBPF_H */