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