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