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