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. If the user callback 102 * is not set, messages are logged to stderr by default. The verbosity of these 103 * messages can be controlled by setting the environment variable 104 * LIBBPF_LOG_LEVEL to either warn, info, or debug. 105 * @param fn The log print function. If NULL, libbpf won't print anything. 106 * @return Pointer to old print function. 107 * 108 * This function is thread-safe. 109 */ 110 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn); 111 112 /* Hide internal to user */ 113 struct bpf_object; 114 115 struct bpf_object_open_opts { 116 /* size of this struct, for forward/backward compatibility */ 117 size_t sz; 118 /* object name override, if provided: 119 * - for object open from file, this will override setting object 120 * name from file path's base name; 121 * - for object open from memory buffer, this will specify an object 122 * name and will override default "<addr>-<buf-size>" name; 123 */ 124 const char *object_name; 125 /* parse map definitions non-strictly, allowing extra attributes/data */ 126 bool relaxed_maps; 127 /* maps that set the 'pinning' attribute in their definition will have 128 * their pin_path attribute set to a file in this directory, and be 129 * auto-pinned to that path on load; defaults to "/sys/fs/bpf". 130 */ 131 const char *pin_root_path; 132 133 __u32 :32; /* stub out now removed attach_prog_fd */ 134 135 /* Additional kernel config content that augments and overrides 136 * system Kconfig for CONFIG_xxx externs. 137 */ 138 const char *kconfig; 139 /* Path to the custom BTF to be used for BPF CO-RE relocations. 140 * This custom BTF completely replaces the use of vmlinux BTF 141 * for the purpose of CO-RE relocations. 142 * NOTE: any other BPF feature (e.g., fentry/fexit programs, 143 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux. 144 */ 145 const char *btf_custom_path; 146 /* Pointer to a buffer for storing kernel logs for applicable BPF 147 * commands. Valid kernel_log_size has to be specified as well and are 148 * passed-through to bpf() syscall. Keep in mind that kernel might 149 * fail operation with -ENOSPC error if provided buffer is too small 150 * to contain entire log output. 151 * See the comment below for kernel_log_level for interaction between 152 * log_buf and log_level settings. 153 * 154 * If specified, this log buffer will be passed for: 155 * - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden 156 * with bpf_program__set_log() on per-program level, to get 157 * BPF verifier log output. 158 * - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get 159 * BTF sanity checking log. 160 * 161 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite 162 * previous contents, so if you need more fine-grained control, set 163 * per-program buffer with bpf_program__set_log_buf() to preserve each 164 * individual program's verification log. Keep using kernel_log_buf 165 * for BTF verification log, if necessary. 166 */ 167 char *kernel_log_buf; 168 size_t kernel_log_size; 169 /* 170 * Log level can be set independently from log buffer. Log_level=0 171 * means that libbpf will attempt loading BTF or program without any 172 * logging requested, but will retry with either its own or custom log 173 * buffer, if provided, and log_level=1 on any error. 174 * And vice versa, setting log_level>0 will request BTF or prog 175 * loading with verbose log from the first attempt (and as such also 176 * for successfully loaded BTF or program), and the actual log buffer 177 * could be either libbpf's own auto-allocated log buffer, if 178 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf. 179 * If user didn't provide custom log buffer, libbpf will emit captured 180 * logs through its print callback. 181 */ 182 __u32 kernel_log_level; 183 /* Path to BPF FS mount point to derive BPF token from. 184 * 185 * Created BPF token will be used for all bpf() syscall operations 186 * that accept BPF token (e.g., map creation, BTF and program loads, 187 * etc) automatically within instantiated BPF object. 188 * 189 * If bpf_token_path is not specified, libbpf will consult 190 * LIBBPF_BPF_TOKEN_PATH environment variable. If set, it will be 191 * taken as a value of bpf_token_path option and will force libbpf to 192 * either create BPF token from provided custom BPF FS path, or will 193 * disable implicit BPF token creation, if envvar value is an empty 194 * string. bpf_token_path overrides LIBBPF_BPF_TOKEN_PATH, if both are 195 * set at the same time. 196 * 197 * Setting bpf_token_path option to empty string disables libbpf's 198 * automatic attempt to create BPF token from default BPF FS mount 199 * point (/sys/fs/bpf), in case this default behavior is undesirable. 200 */ 201 const char *bpf_token_path; 202 203 size_t :0; 204 }; 205 #define bpf_object_open_opts__last_field bpf_token_path 206 207 /** 208 * @brief **bpf_object__open()** creates a bpf_object by opening 209 * the BPF ELF object file pointed to by the passed path and loading it 210 * into memory. 211 * @param path BPF object file path. 212 * @return pointer to the new bpf_object; or NULL is returned on error, 213 * error code is stored in errno 214 */ 215 LIBBPF_API struct bpf_object *bpf_object__open(const char *path); 216 217 /** 218 * @brief **bpf_object__open_file()** creates a bpf_object by opening 219 * the BPF ELF object file pointed to by the passed path and loading it 220 * into memory. 221 * @param path BPF object file path 222 * @param opts options for how to load the bpf object, this parameter is 223 * optional and can be set to NULL 224 * @return pointer to the new bpf_object; or NULL is returned on error, 225 * error code is stored in errno 226 */ 227 LIBBPF_API struct bpf_object * 228 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts); 229 230 /** 231 * @brief **bpf_object__open_mem()** creates a bpf_object by reading 232 * the BPF objects raw bytes from a memory buffer containing a valid 233 * BPF ELF object file. 234 * @param obj_buf pointer to the buffer containing ELF file bytes 235 * @param obj_buf_sz number of bytes in the buffer 236 * @param opts options for how to load the bpf object 237 * @return pointer to the new bpf_object; or NULL is returned on error, 238 * error code is stored in errno 239 */ 240 LIBBPF_API struct bpf_object * 241 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, 242 const struct bpf_object_open_opts *opts); 243 244 /** 245 * @brief **bpf_object__load()** loads BPF object into kernel. 246 * @param obj Pointer to a valid BPF object instance returned by 247 * **bpf_object__open*()** APIs 248 * @return 0, on success; negative error code, otherwise, error code is 249 * stored in errno 250 */ 251 LIBBPF_API int bpf_object__load(struct bpf_object *obj); 252 253 /** 254 * @brief **bpf_object__close()** closes a BPF object and releases all 255 * resources. 256 * @param obj Pointer to a valid BPF object 257 */ 258 LIBBPF_API void bpf_object__close(struct bpf_object *obj); 259 260 /** 261 * @brief **bpf_object__pin_maps()** pins each map contained within 262 * the BPF object at the passed directory. 263 * @param obj Pointer to a valid BPF object 264 * @param path A directory where maps should be pinned. 265 * @return 0, on success; negative error code, otherwise 266 * 267 * If `path` is NULL `bpf_map__pin` (which is being used on each map) 268 * will use the pin_path attribute of each map. In this case, maps that 269 * don't have a pin_path set will be ignored. 270 */ 271 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path); 272 273 /** 274 * @brief **bpf_object__unpin_maps()** unpins each map contained within 275 * the BPF object found in the passed directory. 276 * @param obj Pointer to a valid BPF object 277 * @param path A directory where pinned maps should be searched for. 278 * @return 0, on success; negative error code, otherwise 279 * 280 * If `path` is NULL `bpf_map__unpin` (which is being used on each map) 281 * will use the pin_path attribute of each map. In this case, maps that 282 * don't have a pin_path set will be ignored. 283 */ 284 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj, 285 const char *path); 286 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj, 287 const char *path); 288 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj, 289 const char *path); 290 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path); 291 LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path); 292 293 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj); 294 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj); 295 LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version); 296 297 struct btf; 298 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj); 299 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj); 300 301 LIBBPF_API struct bpf_program * 302 bpf_object__find_program_by_name(const struct bpf_object *obj, 303 const char *name); 304 305 LIBBPF_API int 306 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, 307 enum bpf_attach_type *expected_attach_type); 308 LIBBPF_API int libbpf_attach_type_by_name(const char *name, 309 enum bpf_attach_type *attach_type); 310 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name, 311 enum bpf_attach_type attach_type); 312 313 /* Accessors of bpf_program */ 314 struct bpf_program; 315 316 LIBBPF_API struct bpf_program * 317 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog); 318 319 #define bpf_object__for_each_program(pos, obj) \ 320 for ((pos) = bpf_object__next_program((obj), NULL); \ 321 (pos) != NULL; \ 322 (pos) = bpf_object__next_program((obj), (pos))) 323 324 LIBBPF_API struct bpf_program * 325 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog); 326 327 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog, 328 __u32 ifindex); 329 330 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); 331 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog); 332 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog); 333 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload); 334 LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog); 335 LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach); 336 337 struct bpf_insn; 338 339 /** 340 * @brief **bpf_program__insns()** gives read-only access to BPF program's 341 * underlying BPF instructions. 342 * @param prog BPF program for which to return instructions 343 * @return a pointer to an array of BPF instructions that belong to the 344 * specified BPF program 345 * 346 * Returned pointer is always valid and not NULL. Number of `struct bpf_insn` 347 * pointed to can be fetched using **bpf_program__insn_cnt()** API. 348 * 349 * Keep in mind, libbpf can modify and append/delete BPF program's 350 * instructions as it processes BPF object file and prepares everything for 351 * uploading into the kernel. So depending on the point in BPF object 352 * lifetime, **bpf_program__insns()** can return different sets of 353 * instructions. As an example, during BPF object load phase BPF program 354 * instructions will be CO-RE-relocated, BPF subprograms instructions will be 355 * appended, ldimm64 instructions will have FDs embedded, etc. So instructions 356 * returned before **bpf_object__load()** and after it might be quite 357 * different. 358 */ 359 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog); 360 361 /** 362 * @brief **bpf_program__set_insns()** can set BPF program's underlying 363 * BPF instructions. 364 * 365 * WARNING: This is a very advanced libbpf API and users need to know 366 * what they are doing. This should be used from prog_prepare_load_fn 367 * callback only. 368 * 369 * @param prog BPF program for which to return instructions 370 * @param new_insns a pointer to an array of BPF instructions 371 * @param new_insn_cnt number of `struct bpf_insn`'s that form 372 * specified BPF program 373 * @return 0, on success; negative error code, otherwise 374 */ 375 LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog, 376 struct bpf_insn *new_insns, size_t new_insn_cnt); 377 378 /** 379 * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s 380 * that form specified BPF program. 381 * @param prog BPF program for which to return number of BPF instructions 382 * 383 * See **bpf_program__insns()** documentation for notes on how libbpf can 384 * change instructions and their count during different phases of 385 * **bpf_object** lifetime. 386 */ 387 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog); 388 389 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog); 390 391 /** 392 * @brief **bpf_program__pin()** pins the BPF program to a file 393 * in the BPF FS specified by a path. This increments the programs 394 * reference count, allowing it to stay loaded after the process 395 * which loaded it has exited. 396 * 397 * @param prog BPF program to pin, must already be loaded 398 * @param path file path in a BPF file system 399 * @return 0, on success; negative error code, otherwise 400 */ 401 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path); 402 403 /** 404 * @brief **bpf_program__unpin()** unpins the BPF program from a file 405 * in the BPFFS specified by a path. This decrements the programs 406 * reference count. 407 * 408 * The file pinning the BPF program can also be unlinked by a different 409 * process in which case this function will return an error. 410 * 411 * @param prog BPF program to unpin 412 * @param path file path to the pin in a BPF file system 413 * @return 0, on success; negative error code, otherwise 414 */ 415 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path); 416 LIBBPF_API void bpf_program__unload(struct bpf_program *prog); 417 418 struct bpf_link; 419 420 LIBBPF_API struct bpf_link *bpf_link__open(const char *path); 421 LIBBPF_API int bpf_link__fd(const struct bpf_link *link); 422 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link); 423 /** 424 * @brief **bpf_link__pin()** pins the BPF link to a file 425 * in the BPF FS specified by a path. This increments the links 426 * reference count, allowing it to stay loaded after the process 427 * which loaded it has exited. 428 * 429 * @param link BPF link to pin, must already be loaded 430 * @param path file path in a BPF file system 431 * @return 0, on success; negative error code, otherwise 432 */ 433 434 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path); 435 436 /** 437 * @brief **bpf_link__unpin()** unpins the BPF link from a file 438 * in the BPFFS specified by a path. This decrements the links 439 * reference count. 440 * 441 * The file pinning the BPF link can also be unlinked by a different 442 * process in which case this function will return an error. 443 * 444 * @param prog BPF program to unpin 445 * @param path file path to the pin in a BPF file system 446 * @return 0, on success; negative error code, otherwise 447 */ 448 LIBBPF_API int bpf_link__unpin(struct bpf_link *link); 449 LIBBPF_API int bpf_link__update_program(struct bpf_link *link, 450 struct bpf_program *prog); 451 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link); 452 LIBBPF_API int bpf_link__detach(struct bpf_link *link); 453 LIBBPF_API int bpf_link__destroy(struct bpf_link *link); 454 455 /** 456 * @brief **bpf_program__attach()** is a generic function for attaching 457 * a BPF program based on auto-detection of program type, attach type, 458 * and extra paremeters, where applicable. 459 * 460 * @param prog BPF program to attach 461 * @return Reference to the newly created BPF link; or NULL is returned on error, 462 * error code is stored in errno 463 * 464 * This is supported for: 465 * - kprobe/kretprobe (depends on SEC() definition) 466 * - uprobe/uretprobe (depends on SEC() definition) 467 * - tracepoint 468 * - raw tracepoint 469 * - tracing programs (typed raw TP/fentry/fexit/fmod_ret) 470 */ 471 LIBBPF_API struct bpf_link * 472 bpf_program__attach(const struct bpf_program *prog); 473 474 struct bpf_perf_event_opts { 475 /* size of this struct, for forward/backward compatibility */ 476 size_t sz; 477 /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 478 __u64 bpf_cookie; 479 /* don't use BPF link when attach BPF program */ 480 bool force_ioctl_attach; 481 size_t :0; 482 }; 483 #define bpf_perf_event_opts__last_field force_ioctl_attach 484 485 LIBBPF_API struct bpf_link * 486 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd); 487 488 LIBBPF_API struct bpf_link * 489 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd, 490 const struct bpf_perf_event_opts *opts); 491 492 /** 493 * enum probe_attach_mode - the mode to attach kprobe/uprobe 494 * 495 * force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will 496 * be returned if it is not supported by the kernel. 497 */ 498 enum probe_attach_mode { 499 /* attach probe in latest supported mode by kernel */ 500 PROBE_ATTACH_MODE_DEFAULT = 0, 501 /* attach probe in legacy mode, using debugfs/tracefs */ 502 PROBE_ATTACH_MODE_LEGACY, 503 /* create perf event with perf_event_open() syscall */ 504 PROBE_ATTACH_MODE_PERF, 505 /* attach probe with BPF link */ 506 PROBE_ATTACH_MODE_LINK, 507 }; 508 509 struct bpf_kprobe_opts { 510 /* size of this struct, for forward/backward compatibility */ 511 size_t sz; 512 /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 513 __u64 bpf_cookie; 514 /* function's offset to install kprobe to */ 515 size_t offset; 516 /* kprobe is return probe */ 517 bool retprobe; 518 /* kprobe attach mode */ 519 enum probe_attach_mode attach_mode; 520 size_t :0; 521 }; 522 #define bpf_kprobe_opts__last_field attach_mode 523 524 LIBBPF_API struct bpf_link * 525 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe, 526 const char *func_name); 527 LIBBPF_API struct bpf_link * 528 bpf_program__attach_kprobe_opts(const struct bpf_program *prog, 529 const char *func_name, 530 const struct bpf_kprobe_opts *opts); 531 532 struct bpf_kprobe_multi_opts { 533 /* size of this struct, for forward/backward compatibility */ 534 size_t sz; 535 /* array of function symbols to attach */ 536 const char **syms; 537 /* array of function addresses to attach */ 538 const unsigned long *addrs; 539 /* array of user-provided values fetchable through bpf_get_attach_cookie */ 540 const __u64 *cookies; 541 /* number of elements in syms/addrs/cookies arrays */ 542 size_t cnt; 543 /* create return kprobes */ 544 bool retprobe; 545 /* create session kprobes */ 546 bool session; 547 size_t :0; 548 }; 549 550 #define bpf_kprobe_multi_opts__last_field session 551 552 LIBBPF_API struct bpf_link * 553 bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, 554 const char *pattern, 555 const struct bpf_kprobe_multi_opts *opts); 556 557 struct bpf_uprobe_multi_opts { 558 /* size of this struct, for forward/backward compatibility */ 559 size_t sz; 560 /* array of function symbols to attach to */ 561 const char **syms; 562 /* array of function addresses to attach to */ 563 const unsigned long *offsets; 564 /* optional, array of associated ref counter offsets */ 565 const unsigned long *ref_ctr_offsets; 566 /* optional, array of associated BPF cookies */ 567 const __u64 *cookies; 568 /* number of elements in syms/addrs/cookies arrays */ 569 size_t cnt; 570 /* create return uprobes */ 571 bool retprobe; 572 size_t :0; 573 }; 574 575 #define bpf_uprobe_multi_opts__last_field retprobe 576 577 /** 578 * @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program 579 * to multiple uprobes with uprobe_multi link. 580 * 581 * User can specify 2 mutually exclusive set of inputs: 582 * 583 * 1) use only path/func_pattern/pid arguments 584 * 585 * 2) use path/pid with allowed combinations of 586 * syms/offsets/ref_ctr_offsets/cookies/cnt 587 * 588 * - syms and offsets are mutually exclusive 589 * - ref_ctr_offsets and cookies are optional 590 * 591 * 592 * @param prog BPF program to attach 593 * @param pid Process ID to attach the uprobe to, 0 for self (own process), 594 * -1 for all processes 595 * @param binary_path Path to binary 596 * @param func_pattern Regular expression to specify functions to attach 597 * BPF program to 598 * @param opts Additional options (see **struct bpf_uprobe_multi_opts**) 599 * @return 0, on success; negative error code, otherwise 600 */ 601 LIBBPF_API struct bpf_link * 602 bpf_program__attach_uprobe_multi(const struct bpf_program *prog, 603 pid_t pid, 604 const char *binary_path, 605 const char *func_pattern, 606 const struct bpf_uprobe_multi_opts *opts); 607 608 struct bpf_ksyscall_opts { 609 /* size of this struct, for forward/backward compatibility */ 610 size_t sz; 611 /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 612 __u64 bpf_cookie; 613 /* attach as return probe? */ 614 bool retprobe; 615 size_t :0; 616 }; 617 #define bpf_ksyscall_opts__last_field retprobe 618 619 /** 620 * @brief **bpf_program__attach_ksyscall()** attaches a BPF program 621 * to kernel syscall handler of a specified syscall. Optionally it's possible 622 * to request to install retprobe that will be triggered at syscall exit. It's 623 * also possible to associate BPF cookie (though options). 624 * 625 * Libbpf automatically will determine correct full kernel function name, 626 * which depending on system architecture and kernel version/configuration 627 * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will 628 * attach specified program using kprobe/kretprobe mechanism. 629 * 630 * **bpf_program__attach_ksyscall()** is an API counterpart of declarative 631 * **SEC("ksyscall/<syscall>")** annotation of BPF programs. 632 * 633 * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do 634 * not handle all the calling convention quirks for mmap(), clone() and compat 635 * syscalls. It also only attaches to "native" syscall interfaces. If host 636 * system supports compat syscalls or defines 32-bit syscalls in 64-bit 637 * kernel, such syscall interfaces won't be attached to by libbpf. 638 * 639 * These limitations may or may not change in the future. Therefore it is 640 * recommended to use SEC("kprobe") for these syscalls or if working with 641 * compat and 32-bit interfaces is required. 642 * 643 * @param prog BPF program to attach 644 * @param syscall_name Symbolic name of the syscall (e.g., "bpf") 645 * @param opts Additional options (see **struct bpf_ksyscall_opts**) 646 * @return Reference to the newly created BPF link; or NULL is returned on 647 * error, error code is stored in errno 648 */ 649 LIBBPF_API struct bpf_link * 650 bpf_program__attach_ksyscall(const struct bpf_program *prog, 651 const char *syscall_name, 652 const struct bpf_ksyscall_opts *opts); 653 654 struct bpf_uprobe_opts { 655 /* size of this struct, for forward/backward compatibility */ 656 size_t sz; 657 /* offset of kernel reference counted USDT semaphore, added in 658 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe") 659 */ 660 size_t ref_ctr_offset; 661 /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 662 __u64 bpf_cookie; 663 /* uprobe is return probe, invoked at function return time */ 664 bool retprobe; 665 /* Function name to attach to. Could be an unqualified ("abc") or library-qualified 666 * "abc@LIBXYZ" name. To specify function entry, func_name should be set while 667 * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an 668 * offset within a function, specify func_name and use func_offset argument to specify 669 * offset within the function. Shared library functions must specify the shared library 670 * binary_path. 671 */ 672 const char *func_name; 673 /* uprobe attach mode */ 674 enum probe_attach_mode attach_mode; 675 size_t :0; 676 }; 677 #define bpf_uprobe_opts__last_field attach_mode 678 679 /** 680 * @brief **bpf_program__attach_uprobe()** attaches a BPF program 681 * to the userspace function which is found by binary path and 682 * offset. You can optionally specify a particular proccess to attach 683 * to. You can also optionally attach the program to the function 684 * exit instead of entry. 685 * 686 * @param prog BPF program to attach 687 * @param retprobe Attach to function exit 688 * @param pid Process ID to attach the uprobe to, 0 for self (own process), 689 * -1 for all processes 690 * @param binary_path Path to binary that contains the function symbol 691 * @param func_offset Offset within the binary of the function symbol 692 * @return Reference to the newly created BPF link; or NULL is returned on error, 693 * error code is stored in errno 694 */ 695 LIBBPF_API struct bpf_link * 696 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe, 697 pid_t pid, const char *binary_path, 698 size_t func_offset); 699 700 /** 701 * @brief **bpf_program__attach_uprobe_opts()** is just like 702 * bpf_program__attach_uprobe() except with a options struct 703 * for various configurations. 704 * 705 * @param prog BPF program to attach 706 * @param pid Process ID to attach the uprobe to, 0 for self (own process), 707 * -1 for all processes 708 * @param binary_path Path to binary that contains the function symbol 709 * @param func_offset Offset within the binary of the function symbol 710 * @param opts Options for altering program attachment 711 * @return Reference to the newly created BPF link; or NULL is returned on error, 712 * error code is stored in errno 713 */ 714 LIBBPF_API struct bpf_link * 715 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, 716 const char *binary_path, size_t func_offset, 717 const struct bpf_uprobe_opts *opts); 718 719 struct bpf_usdt_opts { 720 /* size of this struct, for forward/backward compatibility */ 721 size_t sz; 722 /* custom user-provided value accessible through usdt_cookie() */ 723 __u64 usdt_cookie; 724 size_t :0; 725 }; 726 #define bpf_usdt_opts__last_field usdt_cookie 727 728 /** 729 * @brief **bpf_program__attach_usdt()** is just like 730 * bpf_program__attach_uprobe_opts() except it covers USDT (User-space 731 * Statically Defined Tracepoint) attachment, instead of attaching to 732 * user-space function entry or exit. 733 * 734 * @param prog BPF program to attach 735 * @param pid Process ID to attach the uprobe to, 0 for self (own process), 736 * -1 for all processes 737 * @param binary_path Path to binary that contains provided USDT probe 738 * @param usdt_provider USDT provider name 739 * @param usdt_name USDT probe name 740 * @param opts Options for altering program attachment 741 * @return Reference to the newly created BPF link; or NULL is returned on error, 742 * error code is stored in errno 743 */ 744 LIBBPF_API struct bpf_link * 745 bpf_program__attach_usdt(const struct bpf_program *prog, 746 pid_t pid, const char *binary_path, 747 const char *usdt_provider, const char *usdt_name, 748 const struct bpf_usdt_opts *opts); 749 750 struct bpf_tracepoint_opts { 751 /* size of this struct, for forward/backward compatibility */ 752 size_t sz; 753 /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 754 __u64 bpf_cookie; 755 }; 756 #define bpf_tracepoint_opts__last_field bpf_cookie 757 758 LIBBPF_API struct bpf_link * 759 bpf_program__attach_tracepoint(const struct bpf_program *prog, 760 const char *tp_category, 761 const char *tp_name); 762 LIBBPF_API struct bpf_link * 763 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog, 764 const char *tp_category, 765 const char *tp_name, 766 const struct bpf_tracepoint_opts *opts); 767 768 struct bpf_raw_tracepoint_opts { 769 size_t sz; /* size of this struct for forward/backward compatibility */ 770 __u64 cookie; 771 size_t :0; 772 }; 773 #define bpf_raw_tracepoint_opts__last_field cookie 774 775 LIBBPF_API struct bpf_link * 776 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, 777 const char *tp_name); 778 LIBBPF_API struct bpf_link * 779 bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog, 780 const char *tp_name, 781 struct bpf_raw_tracepoint_opts *opts); 782 783 struct bpf_trace_opts { 784 /* size of this struct, for forward/backward compatibility */ 785 size_t sz; 786 /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 787 __u64 cookie; 788 }; 789 #define bpf_trace_opts__last_field cookie 790 791 LIBBPF_API struct bpf_link * 792 bpf_program__attach_trace(const struct bpf_program *prog); 793 LIBBPF_API struct bpf_link * 794 bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts); 795 796 LIBBPF_API struct bpf_link * 797 bpf_program__attach_lsm(const struct bpf_program *prog); 798 LIBBPF_API struct bpf_link * 799 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd); 800 LIBBPF_API struct bpf_link * 801 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd); 802 LIBBPF_API struct bpf_link * 803 bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd); 804 LIBBPF_API struct bpf_link * 805 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex); 806 LIBBPF_API struct bpf_link * 807 bpf_program__attach_freplace(const struct bpf_program *prog, 808 int target_fd, const char *attach_func_name); 809 810 struct bpf_netfilter_opts { 811 /* size of this struct, for forward/backward compatibility */ 812 size_t sz; 813 814 __u32 pf; 815 __u32 hooknum; 816 __s32 priority; 817 __u32 flags; 818 }; 819 #define bpf_netfilter_opts__last_field flags 820 821 LIBBPF_API struct bpf_link * 822 bpf_program__attach_netfilter(const struct bpf_program *prog, 823 const struct bpf_netfilter_opts *opts); 824 825 struct bpf_tcx_opts { 826 /* size of this struct, for forward/backward compatibility */ 827 size_t sz; 828 __u32 flags; 829 __u32 relative_fd; 830 __u32 relative_id; 831 __u64 expected_revision; 832 size_t :0; 833 }; 834 #define bpf_tcx_opts__last_field expected_revision 835 836 LIBBPF_API struct bpf_link * 837 bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex, 838 const struct bpf_tcx_opts *opts); 839 840 struct bpf_netkit_opts { 841 /* size of this struct, for forward/backward compatibility */ 842 size_t sz; 843 __u32 flags; 844 __u32 relative_fd; 845 __u32 relative_id; 846 __u64 expected_revision; 847 size_t :0; 848 }; 849 #define bpf_netkit_opts__last_field expected_revision 850 851 LIBBPF_API struct bpf_link * 852 bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex, 853 const struct bpf_netkit_opts *opts); 854 855 struct bpf_map; 856 857 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map); 858 LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map); 859 860 struct bpf_iter_attach_opts { 861 size_t sz; /* size of this struct for forward/backward compatibility */ 862 union bpf_iter_link_info *link_info; 863 __u32 link_info_len; 864 }; 865 #define bpf_iter_attach_opts__last_field link_info_len 866 867 LIBBPF_API struct bpf_link * 868 bpf_program__attach_iter(const struct bpf_program *prog, 869 const struct bpf_iter_attach_opts *opts); 870 871 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog); 872 873 /** 874 * @brief **bpf_program__set_type()** sets the program 875 * type of the passed BPF program. 876 * @param prog BPF program to set the program type for 877 * @param type program type to set the BPF map to have 878 * @return error code; or 0 if no error. An error occurs 879 * if the object is already loaded. 880 * 881 * This must be called before the BPF object is loaded, 882 * otherwise it has no effect and an error is returned. 883 */ 884 LIBBPF_API int bpf_program__set_type(struct bpf_program *prog, 885 enum bpf_prog_type type); 886 887 LIBBPF_API enum bpf_attach_type 888 bpf_program__expected_attach_type(const struct bpf_program *prog); 889 890 /** 891 * @brief **bpf_program__set_expected_attach_type()** sets the 892 * attach type of the passed BPF program. This is used for 893 * auto-detection of attachment when programs are loaded. 894 * @param prog BPF program to set the attach type for 895 * @param type attach type to set the BPF map to have 896 * @return error code; or 0 if no error. An error occurs 897 * if the object is already loaded. 898 * 899 * This must be called before the BPF object is loaded, 900 * otherwise it has no effect and an error is returned. 901 */ 902 LIBBPF_API int 903 bpf_program__set_expected_attach_type(struct bpf_program *prog, 904 enum bpf_attach_type type); 905 906 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog); 907 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags); 908 909 /* Per-program log level and log buffer getters/setters. 910 * See bpf_object_open_opts comments regarding log_level and log_buf 911 * interactions. 912 */ 913 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog); 914 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level); 915 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size); 916 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size); 917 918 /** 919 * @brief **bpf_program__set_attach_target()** sets BTF-based attach target 920 * for supported BPF program types: 921 * - BTF-aware raw tracepoints (tp_btf); 922 * - fentry/fexit/fmod_ret; 923 * - lsm; 924 * - freplace. 925 * @param prog BPF program to set the attach type for 926 * @param type attach type to set the BPF map to have 927 * @return error code; or 0 if no error occurred. 928 */ 929 LIBBPF_API int 930 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd, 931 const char *attach_func_name); 932 933 /** 934 * @brief **bpf_object__find_map_by_name()** returns BPF map of 935 * the given name, if it exists within the passed BPF object 936 * @param obj BPF object 937 * @param name name of the BPF map 938 * @return BPF map instance, if such map exists within the BPF object; 939 * or NULL otherwise. 940 */ 941 LIBBPF_API struct bpf_map * 942 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name); 943 944 LIBBPF_API int 945 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name); 946 947 LIBBPF_API struct bpf_map * 948 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map); 949 950 #define bpf_object__for_each_map(pos, obj) \ 951 for ((pos) = bpf_object__next_map((obj), NULL); \ 952 (pos) != NULL; \ 953 (pos) = bpf_object__next_map((obj), (pos))) 954 #define bpf_map__for_each bpf_object__for_each_map 955 956 LIBBPF_API struct bpf_map * 957 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map); 958 959 /** 960 * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create 961 * BPF map during BPF object load phase. 962 * @param map the BPF map instance 963 * @param autocreate whether to create BPF map during BPF object load 964 * @return 0 on success; -EBUSY if BPF object was already loaded 965 * 966 * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating 967 * BPF map. By default, libbpf will attempt to create every single BPF map 968 * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall 969 * and fill in map FD in BPF instructions. 970 * 971 * This API allows to opt-out of this process for specific map instance. This 972 * can be useful if host kernel doesn't support such BPF map type or used 973 * combination of flags and user application wants to avoid creating such 974 * a map in the first place. User is still responsible to make sure that their 975 * BPF-side code that expects to use such missing BPF map is recognized by BPF 976 * verifier as dead code, otherwise BPF verifier will reject such BPF program. 977 */ 978 LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate); 979 LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map); 980 981 /** 982 * @brief **bpf_map__fd()** gets the file descriptor of the passed 983 * BPF map 984 * @param map the BPF map instance 985 * @return the file descriptor; or -EINVAL in case of an error 986 */ 987 LIBBPF_API int bpf_map__fd(const struct bpf_map *map); 988 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd); 989 /* get map name */ 990 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map); 991 /* get/set map type */ 992 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map); 993 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type); 994 /* get/set map size (max_entries) */ 995 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map); 996 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries); 997 /* get/set map flags */ 998 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map); 999 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags); 1000 /* get/set map NUMA node */ 1001 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map); 1002 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node); 1003 /* get/set map key size */ 1004 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map); 1005 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size); 1006 /* get map value size */ 1007 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map); 1008 /** 1009 * @brief **bpf_map__set_value_size()** sets map value size. 1010 * @param map the BPF map instance 1011 * @return 0, on success; negative error, otherwise 1012 * 1013 * There is a special case for maps with associated memory-mapped regions, like 1014 * the global data section maps (bss, data, rodata). When this function is used 1015 * on such a map, the mapped region is resized. Afterward, an attempt is made to 1016 * adjust the corresponding BTF info. This attempt is best-effort and can only 1017 * succeed if the last variable of the data section map is an array. The array 1018 * BTF type is replaced by a new BTF array type with a different length. 1019 * Any previously existing pointers returned from bpf_map__initial_value() or 1020 * corresponding data section skeleton pointer must be reinitialized. 1021 */ 1022 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size); 1023 /* get map key/value BTF type IDs */ 1024 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map); 1025 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map); 1026 /* get/set map if_index */ 1027 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map); 1028 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); 1029 /* get/set map map_extra flags */ 1030 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map); 1031 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra); 1032 1033 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map, 1034 const void *data, size_t size); 1035 LIBBPF_API void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize); 1036 1037 /** 1038 * @brief **bpf_map__is_internal()** tells the caller whether or not the 1039 * passed map is a special map created by libbpf automatically for things like 1040 * global variables, __ksym externs, Kconfig values, etc 1041 * @param map the bpf_map 1042 * @return true, if the map is an internal map; false, otherwise 1043 */ 1044 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map); 1045 1046 /** 1047 * @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the 1048 * BPF map should be pinned. This does not actually create the 'pin'. 1049 * @param map The bpf_map 1050 * @param path The path 1051 * @return 0, on success; negative error, otherwise 1052 */ 1053 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path); 1054 1055 /** 1056 * @brief **bpf_map__pin_path()** gets the path attribute that tells where the 1057 * BPF map should be pinned. 1058 * @param map The bpf_map 1059 * @return The path string; which can be NULL 1060 */ 1061 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map); 1062 1063 /** 1064 * @brief **bpf_map__is_pinned()** tells the caller whether or not the 1065 * passed map has been pinned via a 'pin' file. 1066 * @param map The bpf_map 1067 * @return true, if the map is pinned; false, otherwise 1068 */ 1069 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map); 1070 1071 /** 1072 * @brief **bpf_map__pin()** creates a file that serves as a 'pin' 1073 * for the BPF map. This increments the reference count on the 1074 * BPF map which will keep the BPF map loaded even after the 1075 * userspace process which loaded it has exited. 1076 * @param map The bpf_map to pin 1077 * @param path A file path for the 'pin' 1078 * @return 0, on success; negative error, otherwise 1079 * 1080 * If `path` is NULL the maps `pin_path` attribute will be used. If this is 1081 * also NULL, an error will be returned and the map will not be pinned. 1082 */ 1083 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path); 1084 1085 /** 1086 * @brief **bpf_map__unpin()** removes the file that serves as a 1087 * 'pin' for the BPF map. 1088 * @param map The bpf_map to unpin 1089 * @param path A file path for the 'pin' 1090 * @return 0, on success; negative error, otherwise 1091 * 1092 * The `path` parameter can be NULL, in which case the `pin_path` 1093 * map attribute is unpinned. If both the `path` parameter and 1094 * `pin_path` map attribute are set, they must be equal. 1095 */ 1096 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path); 1097 1098 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd); 1099 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map); 1100 1101 /** 1102 * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value 1103 * corresponding to provided key. 1104 * @param map BPF map to lookup element in 1105 * @param key pointer to memory containing bytes of the key used for lookup 1106 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1107 * @param value pointer to memory in which looked up value will be stored 1108 * @param value_sz size in byte of value data memory; it has to match BPF map 1109 * definition's **value_size**. For per-CPU BPF maps value size has to be 1110 * a product of BPF map value size and number of possible CPUs in the system 1111 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for 1112 * per-CPU values value size has to be aligned up to closest 8 bytes for 1113 * alignment reasons, so expected size is: `round_up(value_size, 8) 1114 * * libbpf_num_possible_cpus()`. 1115 * @flags extra flags passed to kernel for this operation 1116 * @return 0, on success; negative error, otherwise 1117 * 1118 * **bpf_map__lookup_elem()** is high-level equivalent of 1119 * **bpf_map_lookup_elem()** API with added check for key and value size. 1120 */ 1121 LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map, 1122 const void *key, size_t key_sz, 1123 void *value, size_t value_sz, __u64 flags); 1124 1125 /** 1126 * @brief **bpf_map__update_elem()** allows to insert or update value in BPF 1127 * map that corresponds to provided key. 1128 * @param map BPF map to insert to or update element in 1129 * @param key pointer to memory containing bytes of the key 1130 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1131 * @param value pointer to memory containing bytes of the value 1132 * @param value_sz size in byte of value data memory; it has to match BPF map 1133 * definition's **value_size**. For per-CPU BPF maps value size has to be 1134 * a product of BPF map value size and number of possible CPUs in the system 1135 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for 1136 * per-CPU values value size has to be aligned up to closest 8 bytes for 1137 * alignment reasons, so expected size is: `round_up(value_size, 8) 1138 * * libbpf_num_possible_cpus()`. 1139 * @flags extra flags passed to kernel for this operation 1140 * @return 0, on success; negative error, otherwise 1141 * 1142 * **bpf_map__update_elem()** is high-level equivalent of 1143 * **bpf_map_update_elem()** API with added check for key and value size. 1144 */ 1145 LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map, 1146 const void *key, size_t key_sz, 1147 const void *value, size_t value_sz, __u64 flags); 1148 1149 /** 1150 * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that 1151 * corresponds to provided key. 1152 * @param map BPF map to delete element from 1153 * @param key pointer to memory containing bytes of the key 1154 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1155 * @flags extra flags passed to kernel for this operation 1156 * @return 0, on success; negative error, otherwise 1157 * 1158 * **bpf_map__delete_elem()** is high-level equivalent of 1159 * **bpf_map_delete_elem()** API with added check for key size. 1160 */ 1161 LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map, 1162 const void *key, size_t key_sz, __u64 flags); 1163 1164 /** 1165 * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value 1166 * corresponding to provided key and atomically delete it afterwards. 1167 * @param map BPF map to lookup element in 1168 * @param key pointer to memory containing bytes of the key used for lookup 1169 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1170 * @param value pointer to memory in which looked up value will be stored 1171 * @param value_sz size in byte of value data memory; it has to match BPF map 1172 * definition's **value_size**. For per-CPU BPF maps value size has to be 1173 * a product of BPF map value size and number of possible CPUs in the system 1174 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for 1175 * per-CPU values value size has to be aligned up to closest 8 bytes for 1176 * alignment reasons, so expected size is: `round_up(value_size, 8) 1177 * * libbpf_num_possible_cpus()`. 1178 * @flags extra flags passed to kernel for this operation 1179 * @return 0, on success; negative error, otherwise 1180 * 1181 * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of 1182 * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size. 1183 */ 1184 LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map, 1185 const void *key, size_t key_sz, 1186 void *value, size_t value_sz, __u64 flags); 1187 1188 /** 1189 * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by 1190 * fetching next key that follows current key. 1191 * @param map BPF map to fetch next key from 1192 * @param cur_key pointer to memory containing bytes of current key or NULL to 1193 * fetch the first key 1194 * @param next_key pointer to memory to write next key into 1195 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size** 1196 * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map; 1197 * negative error, otherwise 1198 * 1199 * **bpf_map__get_next_key()** is high-level equivalent of 1200 * **bpf_map_get_next_key()** API with added check for key size. 1201 */ 1202 LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map, 1203 const void *cur_key, void *next_key, size_t key_sz); 1204 1205 struct bpf_xdp_set_link_opts { 1206 size_t sz; 1207 int old_fd; 1208 size_t :0; 1209 }; 1210 #define bpf_xdp_set_link_opts__last_field old_fd 1211 1212 struct bpf_xdp_attach_opts { 1213 size_t sz; 1214 int old_prog_fd; 1215 size_t :0; 1216 }; 1217 #define bpf_xdp_attach_opts__last_field old_prog_fd 1218 1219 struct bpf_xdp_query_opts { 1220 size_t sz; 1221 __u32 prog_id; /* output */ 1222 __u32 drv_prog_id; /* output */ 1223 __u32 hw_prog_id; /* output */ 1224 __u32 skb_prog_id; /* output */ 1225 __u8 attach_mode; /* output */ 1226 __u64 feature_flags; /* output */ 1227 __u32 xdp_zc_max_segs; /* output */ 1228 size_t :0; 1229 }; 1230 #define bpf_xdp_query_opts__last_field xdp_zc_max_segs 1231 1232 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags, 1233 const struct bpf_xdp_attach_opts *opts); 1234 LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags, 1235 const struct bpf_xdp_attach_opts *opts); 1236 LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts); 1237 LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id); 1238 1239 /* TC related API */ 1240 enum bpf_tc_attach_point { 1241 BPF_TC_INGRESS = 1 << 0, 1242 BPF_TC_EGRESS = 1 << 1, 1243 BPF_TC_CUSTOM = 1 << 2, 1244 }; 1245 1246 #define BPF_TC_PARENT(a, b) \ 1247 ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU)) 1248 1249 enum bpf_tc_flags { 1250 BPF_TC_F_REPLACE = 1 << 0, 1251 }; 1252 1253 struct bpf_tc_hook { 1254 size_t sz; 1255 int ifindex; 1256 enum bpf_tc_attach_point attach_point; 1257 __u32 parent; 1258 size_t :0; 1259 }; 1260 #define bpf_tc_hook__last_field parent 1261 1262 struct bpf_tc_opts { 1263 size_t sz; 1264 int prog_fd; 1265 __u32 flags; 1266 __u32 prog_id; 1267 __u32 handle; 1268 __u32 priority; 1269 size_t :0; 1270 }; 1271 #define bpf_tc_opts__last_field priority 1272 1273 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook); 1274 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook); 1275 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook, 1276 struct bpf_tc_opts *opts); 1277 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook, 1278 const struct bpf_tc_opts *opts); 1279 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook, 1280 struct bpf_tc_opts *opts); 1281 1282 /* Ring buffer APIs */ 1283 struct ring_buffer; 1284 struct ring; 1285 struct user_ring_buffer; 1286 1287 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size); 1288 1289 struct ring_buffer_opts { 1290 size_t sz; /* size of this struct, for forward/backward compatibility */ 1291 }; 1292 1293 #define ring_buffer_opts__last_field sz 1294 1295 LIBBPF_API struct ring_buffer * 1296 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx, 1297 const struct ring_buffer_opts *opts); 1298 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb); 1299 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd, 1300 ring_buffer_sample_fn sample_cb, void *ctx); 1301 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms); 1302 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb); 1303 LIBBPF_API int ring_buffer__consume_n(struct ring_buffer *rb, size_t n); 1304 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb); 1305 1306 /** 1307 * @brief **ring_buffer__ring()** returns the ringbuffer object inside a given 1308 * ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance. 1309 * 1310 * @param rb A ringbuffer manager object. 1311 * @param idx An index into the ringbuffers contained within the ringbuffer 1312 * manager object. The index is 0-based and corresponds to the order in which 1313 * ring_buffer__add was called. 1314 * @return A ringbuffer object on success; NULL and errno set if the index is 1315 * invalid. 1316 */ 1317 LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb, 1318 unsigned int idx); 1319 1320 /** 1321 * @brief **ring__consumer_pos()** returns the current consumer position in the 1322 * given ringbuffer. 1323 * 1324 * @param r A ringbuffer object. 1325 * @return The current consumer position. 1326 */ 1327 LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r); 1328 1329 /** 1330 * @brief **ring__producer_pos()** returns the current producer position in the 1331 * given ringbuffer. 1332 * 1333 * @param r A ringbuffer object. 1334 * @return The current producer position. 1335 */ 1336 LIBBPF_API unsigned long ring__producer_pos(const struct ring *r); 1337 1338 /** 1339 * @brief **ring__avail_data_size()** returns the number of bytes in the 1340 * ringbuffer not yet consumed. This has no locking associated with it, so it 1341 * can be inaccurate if operations are ongoing while this is called. However, it 1342 * should still show the correct trend over the long-term. 1343 * 1344 * @param r A ringbuffer object. 1345 * @return The number of bytes not yet consumed. 1346 */ 1347 LIBBPF_API size_t ring__avail_data_size(const struct ring *r); 1348 1349 /** 1350 * @brief **ring__size()** returns the total size of the ringbuffer's map data 1351 * area (excluding special producer/consumer pages). Effectively this gives the 1352 * amount of usable bytes of data inside the ringbuffer. 1353 * 1354 * @param r A ringbuffer object. 1355 * @return The total size of the ringbuffer map data area. 1356 */ 1357 LIBBPF_API size_t ring__size(const struct ring *r); 1358 1359 /** 1360 * @brief **ring__map_fd()** returns the file descriptor underlying the given 1361 * ringbuffer. 1362 * 1363 * @param r A ringbuffer object. 1364 * @return The underlying ringbuffer file descriptor 1365 */ 1366 LIBBPF_API int ring__map_fd(const struct ring *r); 1367 1368 /** 1369 * @brief **ring__consume()** consumes available ringbuffer data without event 1370 * polling. 1371 * 1372 * @param r A ringbuffer object. 1373 * @return The number of records consumed (or INT_MAX, whichever is less), or 1374 * a negative number if any of the callbacks return an error. 1375 */ 1376 LIBBPF_API int ring__consume(struct ring *r); 1377 1378 /** 1379 * @brief **ring__consume_n()** consumes up to a requested amount of items from 1380 * a ringbuffer without event polling. 1381 * 1382 * @param r A ringbuffer object. 1383 * @param n Maximum amount of items to consume. 1384 * @return The number of items consumed, or a negative number if any of the 1385 * callbacks return an error. 1386 */ 1387 LIBBPF_API int ring__consume_n(struct ring *r, size_t n); 1388 1389 struct user_ring_buffer_opts { 1390 size_t sz; /* size of this struct, for forward/backward compatibility */ 1391 }; 1392 1393 #define user_ring_buffer_opts__last_field sz 1394 1395 /** 1396 * @brief **user_ring_buffer__new()** creates a new instance of a user ring 1397 * buffer. 1398 * 1399 * @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map. 1400 * @param opts Options for how the ring buffer should be created. 1401 * @return A user ring buffer on success; NULL and errno being set on a 1402 * failure. 1403 */ 1404 LIBBPF_API struct user_ring_buffer * 1405 user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts); 1406 1407 /** 1408 * @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the 1409 * user ring buffer. 1410 * @param rb A pointer to a user ring buffer. 1411 * @param size The size of the sample, in bytes. 1412 * @return A pointer to an 8-byte aligned reserved region of the user ring 1413 * buffer; NULL, and errno being set if a sample could not be reserved. 1414 * 1415 * This function is *not* thread safe, and callers must synchronize accessing 1416 * this function if there are multiple producers. If a size is requested that 1417 * is larger than the size of the entire ring buffer, errno will be set to 1418 * E2BIG and NULL is returned. If the ring buffer could accommodate the size, 1419 * but currently does not have enough space, errno is set to ENOSPC and NULL is 1420 * returned. 1421 * 1422 * After initializing the sample, callers must invoke 1423 * **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise, 1424 * the sample must be freed with **user_ring_buffer__discard()**. 1425 */ 1426 LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size); 1427 1428 /** 1429 * @brief **user_ring_buffer__reserve_blocking()** reserves a record in the 1430 * ring buffer, possibly blocking for up to @timeout_ms until a sample becomes 1431 * available. 1432 * @param rb The user ring buffer. 1433 * @param size The size of the sample, in bytes. 1434 * @param timeout_ms The amount of time, in milliseconds, for which the caller 1435 * should block when waiting for a sample. -1 causes the caller to block 1436 * indefinitely. 1437 * @return A pointer to an 8-byte aligned reserved region of the user ring 1438 * buffer; NULL, and errno being set if a sample could not be reserved. 1439 * 1440 * This function is *not* thread safe, and callers must synchronize 1441 * accessing this function if there are multiple producers 1442 * 1443 * If **timeout_ms** is -1, the function will block indefinitely until a sample 1444 * becomes available. Otherwise, **timeout_ms** must be non-negative, or errno 1445 * is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking 1446 * will occur and the function will return immediately after attempting to 1447 * reserve a sample. 1448 * 1449 * If **size** is larger than the size of the entire ring buffer, errno is set 1450 * to E2BIG and NULL is returned. If the ring buffer could accommodate 1451 * **size**, but currently does not have enough space, the caller will block 1452 * until at most **timeout_ms** has elapsed. If insufficient space is available 1453 * at that time, errno is set to ENOSPC, and NULL is returned. 1454 * 1455 * The kernel guarantees that it will wake up this thread to check if 1456 * sufficient space is available in the ring buffer at least once per 1457 * invocation of the **bpf_ringbuf_drain()** helper function, provided that at 1458 * least one sample is consumed, and the BPF program did not invoke the 1459 * function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the 1460 * kernel does not guarantee this. If the helper function is invoked with 1461 * BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is 1462 * consumed. 1463 * 1464 * When a sample of size **size** is found within **timeout_ms**, a pointer to 1465 * the sample is returned. After initializing the sample, callers must invoke 1466 * **user_ring_buffer__submit()** to post the sample to the ring buffer. 1467 * Otherwise, the sample must be freed with **user_ring_buffer__discard()**. 1468 */ 1469 LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb, 1470 __u32 size, 1471 int timeout_ms); 1472 1473 /** 1474 * @brief **user_ring_buffer__submit()** submits a previously reserved sample 1475 * into the ring buffer. 1476 * @param rb The user ring buffer. 1477 * @param sample A reserved sample. 1478 * 1479 * It is not necessary to synchronize amongst multiple producers when invoking 1480 * this function. 1481 */ 1482 LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample); 1483 1484 /** 1485 * @brief **user_ring_buffer__discard()** discards a previously reserved sample. 1486 * @param rb The user ring buffer. 1487 * @param sample A reserved sample. 1488 * 1489 * It is not necessary to synchronize amongst multiple producers when invoking 1490 * this function. 1491 */ 1492 LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample); 1493 1494 /** 1495 * @brief **user_ring_buffer__free()** frees a ring buffer that was previously 1496 * created with **user_ring_buffer__new()**. 1497 * @param rb The user ring buffer being freed. 1498 */ 1499 LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb); 1500 1501 /* Perf buffer APIs */ 1502 struct perf_buffer; 1503 1504 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu, 1505 void *data, __u32 size); 1506 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt); 1507 1508 /* common use perf buffer options */ 1509 struct perf_buffer_opts { 1510 size_t sz; 1511 __u32 sample_period; 1512 size_t :0; 1513 }; 1514 #define perf_buffer_opts__last_field sample_period 1515 1516 /** 1517 * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified 1518 * BPF_PERF_EVENT_ARRAY map 1519 * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF 1520 * code to send data over to user-space 1521 * @param page_cnt number of memory pages allocated for each per-CPU buffer 1522 * @param sample_cb function called on each received data record 1523 * @param lost_cb function called when record loss has occurred 1524 * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb* 1525 * @return a new instance of struct perf_buffer on success, NULL on error with 1526 * *errno* containing an error code 1527 */ 1528 LIBBPF_API struct perf_buffer * 1529 perf_buffer__new(int map_fd, size_t page_cnt, 1530 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx, 1531 const struct perf_buffer_opts *opts); 1532 1533 enum bpf_perf_event_ret { 1534 LIBBPF_PERF_EVENT_DONE = 0, 1535 LIBBPF_PERF_EVENT_ERROR = -1, 1536 LIBBPF_PERF_EVENT_CONT = -2, 1537 }; 1538 1539 struct perf_event_header; 1540 1541 typedef enum bpf_perf_event_ret 1542 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event); 1543 1544 /* raw perf buffer options, giving most power and control */ 1545 struct perf_buffer_raw_opts { 1546 size_t sz; 1547 long :0; 1548 long :0; 1549 /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of 1550 * max_entries of given PERF_EVENT_ARRAY map) 1551 */ 1552 int cpu_cnt; 1553 /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */ 1554 int *cpus; 1555 /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */ 1556 int *map_keys; 1557 }; 1558 #define perf_buffer_raw_opts__last_field map_keys 1559 1560 struct perf_event_attr; 1561 1562 LIBBPF_API struct perf_buffer * 1563 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr, 1564 perf_buffer_event_fn event_cb, void *ctx, 1565 const struct perf_buffer_raw_opts *opts); 1566 1567 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb); 1568 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb); 1569 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms); 1570 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb); 1571 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx); 1572 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb); 1573 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx); 1574 /** 1575 * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying 1576 * memory region of the ring buffer. 1577 * This ring buffer can be used to implement a custom events consumer. 1578 * The ring buffer starts with the *struct perf_event_mmap_page*, which 1579 * holds the ring buffer managment fields, when accessing the header 1580 * structure it's important to be SMP aware. 1581 * You can refer to *perf_event_read_simple* for a simple example. 1582 * @param pb the perf buffer structure 1583 * @param buf_idx the buffer index to retreive 1584 * @param buf (out) gets the base pointer of the mmap()'ed memory 1585 * @param buf_size (out) gets the size of the mmap()'ed region 1586 * @return 0 on success, negative error code for failure 1587 */ 1588 LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf, 1589 size_t *buf_size); 1590 1591 struct bpf_prog_linfo; 1592 struct bpf_prog_info; 1593 1594 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo); 1595 LIBBPF_API struct bpf_prog_linfo * 1596 bpf_prog_linfo__new(const struct bpf_prog_info *info); 1597 LIBBPF_API const struct bpf_line_info * 1598 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo, 1599 __u64 addr, __u32 func_idx, __u32 nr_skip); 1600 LIBBPF_API const struct bpf_line_info * 1601 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo, 1602 __u32 insn_off, __u32 nr_skip); 1603 1604 /* 1605 * Probe for supported system features 1606 * 1607 * Note that running many of these probes in a short amount of time can cause 1608 * the kernel to reach the maximal size of lockable memory allowed for the 1609 * user, causing subsequent probes to fail. In this case, the caller may want 1610 * to adjust that limit with setrlimit(). 1611 */ 1612 1613 /** 1614 * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports 1615 * BPF programs of a given type. 1616 * @param prog_type BPF program type to detect kernel support for 1617 * @param opts reserved for future extensibility, should be NULL 1618 * @return 1, if given program type is supported; 0, if given program type is 1619 * not supported; negative error code if feature detection failed or can't be 1620 * performed 1621 * 1622 * Make sure the process has required set of CAP_* permissions (or runs as 1623 * root) when performing feature checking. 1624 */ 1625 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts); 1626 /** 1627 * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports 1628 * BPF maps of a given type. 1629 * @param map_type BPF map type to detect kernel support for 1630 * @param opts reserved for future extensibility, should be NULL 1631 * @return 1, if given map type is supported; 0, if given map type is 1632 * not supported; negative error code if feature detection failed or can't be 1633 * performed 1634 * 1635 * Make sure the process has required set of CAP_* permissions (or runs as 1636 * root) when performing feature checking. 1637 */ 1638 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts); 1639 /** 1640 * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the 1641 * use of a given BPF helper from specified BPF program type. 1642 * @param prog_type BPF program type used to check the support of BPF helper 1643 * @param helper_id BPF helper ID (enum bpf_func_id) to check support for 1644 * @param opts reserved for future extensibility, should be NULL 1645 * @return 1, if given combination of program type and helper is supported; 0, 1646 * if the combination is not supported; negative error code if feature 1647 * detection for provided input arguments failed or can't be performed 1648 * 1649 * Make sure the process has required set of CAP_* permissions (or runs as 1650 * root) when performing feature checking. 1651 */ 1652 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, 1653 enum bpf_func_id helper_id, const void *opts); 1654 1655 /** 1656 * @brief **libbpf_num_possible_cpus()** is a helper function to get the 1657 * number of possible CPUs that the host kernel supports and expects. 1658 * @return number of possible CPUs; or error code on failure 1659 * 1660 * Example usage: 1661 * 1662 * int ncpus = libbpf_num_possible_cpus(); 1663 * if (ncpus < 0) { 1664 * // error handling 1665 * } 1666 * long values[ncpus]; 1667 * bpf_map_lookup_elem(per_cpu_map_fd, key, values); 1668 */ 1669 LIBBPF_API int libbpf_num_possible_cpus(void); 1670 1671 struct bpf_map_skeleton { 1672 const char *name; 1673 struct bpf_map **map; 1674 void **mmaped; 1675 }; 1676 1677 struct bpf_prog_skeleton { 1678 const char *name; 1679 struct bpf_program **prog; 1680 struct bpf_link **link; 1681 }; 1682 1683 struct bpf_object_skeleton { 1684 size_t sz; /* size of this struct, for forward/backward compatibility */ 1685 1686 const char *name; 1687 const void *data; 1688 size_t data_sz; 1689 1690 struct bpf_object **obj; 1691 1692 int map_cnt; 1693 int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */ 1694 struct bpf_map_skeleton *maps; 1695 1696 int prog_cnt; 1697 int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */ 1698 struct bpf_prog_skeleton *progs; 1699 }; 1700 1701 LIBBPF_API int 1702 bpf_object__open_skeleton(struct bpf_object_skeleton *s, 1703 const struct bpf_object_open_opts *opts); 1704 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s); 1705 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s); 1706 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s); 1707 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s); 1708 1709 struct bpf_var_skeleton { 1710 const char *name; 1711 struct bpf_map **map; 1712 void **addr; 1713 }; 1714 1715 struct bpf_object_subskeleton { 1716 size_t sz; /* size of this struct, for forward/backward compatibility */ 1717 1718 const struct bpf_object *obj; 1719 1720 int map_cnt; 1721 int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */ 1722 struct bpf_map_skeleton *maps; 1723 1724 int prog_cnt; 1725 int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */ 1726 struct bpf_prog_skeleton *progs; 1727 1728 int var_cnt; 1729 int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */ 1730 struct bpf_var_skeleton *vars; 1731 }; 1732 1733 LIBBPF_API int 1734 bpf_object__open_subskeleton(struct bpf_object_subskeleton *s); 1735 LIBBPF_API void 1736 bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s); 1737 1738 struct gen_loader_opts { 1739 size_t sz; /* size of this struct, for forward/backward compatibility */ 1740 const char *data; 1741 const char *insns; 1742 __u32 data_sz; 1743 __u32 insns_sz; 1744 }; 1745 1746 #define gen_loader_opts__last_field insns_sz 1747 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj, 1748 struct gen_loader_opts *opts); 1749 1750 enum libbpf_tristate { 1751 TRI_NO = 0, 1752 TRI_YES = 1, 1753 TRI_MODULE = 2, 1754 }; 1755 1756 struct bpf_linker_opts { 1757 /* size of this struct, for forward/backward compatibility */ 1758 size_t sz; 1759 }; 1760 #define bpf_linker_opts__last_field sz 1761 1762 struct bpf_linker_file_opts { 1763 /* size of this struct, for forward/backward compatibility */ 1764 size_t sz; 1765 }; 1766 #define bpf_linker_file_opts__last_field sz 1767 1768 struct bpf_linker; 1769 1770 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts); 1771 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, 1772 const char *filename, 1773 const struct bpf_linker_file_opts *opts); 1774 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); 1775 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); 1776 1777 /* 1778 * Custom handling of BPF program's SEC() definitions 1779 */ 1780 1781 struct bpf_prog_load_opts; /* defined in bpf.h */ 1782 1783 /* Called during bpf_object__open() for each recognized BPF program. Callback 1784 * can use various bpf_program__set_*() setters to adjust whatever properties 1785 * are necessary. 1786 */ 1787 typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie); 1788 1789 /* Called right before libbpf performs bpf_prog_load() to load BPF program 1790 * into the kernel. Callback can adjust opts as necessary. 1791 */ 1792 typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog, 1793 struct bpf_prog_load_opts *opts, long cookie); 1794 1795 /* Called during skeleton attach or through bpf_program__attach(). If 1796 * auto-attach is not supported, callback should return 0 and set link to 1797 * NULL (it's not considered an error during skeleton attach, but it will be 1798 * an error for bpf_program__attach() calls). On error, error should be 1799 * returned directly and link set to NULL. On success, return 0 and set link 1800 * to a valid struct bpf_link. 1801 */ 1802 typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie, 1803 struct bpf_link **link); 1804 1805 struct libbpf_prog_handler_opts { 1806 /* size of this struct, for forward/backward compatibility */ 1807 size_t sz; 1808 /* User-provided value that is passed to prog_setup_fn, 1809 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to 1810 * register one set of callbacks for multiple SEC() definitions and 1811 * still be able to distinguish them, if necessary. For example, 1812 * libbpf itself is using this to pass necessary flags (e.g., 1813 * sleepable flag) to a common internal SEC() handler. 1814 */ 1815 long cookie; 1816 /* BPF program initialization callback (see libbpf_prog_setup_fn_t). 1817 * Callback is optional, pass NULL if it's not necessary. 1818 */ 1819 libbpf_prog_setup_fn_t prog_setup_fn; 1820 /* BPF program loading callback (see libbpf_prog_prepare_load_fn_t). 1821 * Callback is optional, pass NULL if it's not necessary. 1822 */ 1823 libbpf_prog_prepare_load_fn_t prog_prepare_load_fn; 1824 /* BPF program attach callback (see libbpf_prog_attach_fn_t). 1825 * Callback is optional, pass NULL if it's not necessary. 1826 */ 1827 libbpf_prog_attach_fn_t prog_attach_fn; 1828 }; 1829 #define libbpf_prog_handler_opts__last_field prog_attach_fn 1830 1831 /** 1832 * @brief **libbpf_register_prog_handler()** registers a custom BPF program 1833 * SEC() handler. 1834 * @param sec section prefix for which custom handler is registered 1835 * @param prog_type BPF program type associated with specified section 1836 * @param exp_attach_type Expected BPF attach type associated with specified section 1837 * @param opts optional cookie, callbacks, and other extra options 1838 * @return Non-negative handler ID is returned on success. This handler ID has 1839 * to be passed to *libbpf_unregister_prog_handler()* to unregister such 1840 * custom handler. Negative error code is returned on error. 1841 * 1842 * *sec* defines which SEC() definitions are handled by this custom handler 1843 * registration. *sec* can have few different forms: 1844 * - if *sec* is just a plain string (e.g., "abc"), it will match only 1845 * SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result 1846 * in an error; 1847 * - if *sec* is of the form "abc/", proper SEC() form is 1848 * SEC("abc/something"), where acceptable "something" should be checked by 1849 * *prog_init_fn* callback, if there are additional restrictions; 1850 * - if *sec* is of the form "abc+", it will successfully match both 1851 * SEC("abc") and SEC("abc/whatever") forms; 1852 * - if *sec* is NULL, custom handler is registered for any BPF program that 1853 * doesn't match any of the registered (custom or libbpf's own) SEC() 1854 * handlers. There could be only one such generic custom handler registered 1855 * at any given time. 1856 * 1857 * All custom handlers (except the one with *sec* == NULL) are processed 1858 * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's 1859 * SEC() handlers by registering custom ones for the same section prefix 1860 * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses") 1861 * handler). 1862 * 1863 * Note, like much of global libbpf APIs (e.g., libbpf_set_print(), 1864 * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs 1865 * to ensure synchronization if there is a risk of running this API from 1866 * multiple threads simultaneously. 1867 */ 1868 LIBBPF_API int libbpf_register_prog_handler(const char *sec, 1869 enum bpf_prog_type prog_type, 1870 enum bpf_attach_type exp_attach_type, 1871 const struct libbpf_prog_handler_opts *opts); 1872 /** 1873 * @brief *libbpf_unregister_prog_handler()* unregisters previously registered 1874 * custom BPF program SEC() handler. 1875 * @param handler_id handler ID returned by *libbpf_register_prog_handler()* 1876 * after successful registration 1877 * @return 0 on success, negative error code if handler isn't found 1878 * 1879 * Note, like much of global libbpf APIs (e.g., libbpf_set_print(), 1880 * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs 1881 * to ensure synchronization if there is a risk of running this API from 1882 * multiple threads simultaneously. 1883 */ 1884 LIBBPF_API int libbpf_unregister_prog_handler(int handler_id); 1885 1886 #ifdef __cplusplus 1887 } /* extern "C" */ 1888 #endif 1889 1890 #endif /* __LIBBPF_LIBBPF_H */ 1891