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