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