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