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 enum libbpf_errno { 28 __LIBBPF_ERRNO__START = 4000, 29 30 /* Something wrong in libelf */ 31 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START, 32 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */ 33 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */ 34 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */ 35 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */ 36 LIBBPF_ERRNO__RELOC, /* Relocation failed */ 37 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */ 38 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */ 39 LIBBPF_ERRNO__PROG2BIG, /* Program too big */ 40 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */ 41 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */ 42 LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */ 43 LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */ 44 LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */ 45 __LIBBPF_ERRNO__END, 46 }; 47 48 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size); 49 50 enum libbpf_print_level { 51 LIBBPF_WARN, 52 LIBBPF_INFO, 53 LIBBPF_DEBUG, 54 }; 55 56 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level, 57 const char *, va_list ap); 58 59 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn); 60 61 /* Hide internal to user */ 62 struct bpf_object; 63 64 struct bpf_object_open_attr { 65 const char *file; 66 enum bpf_prog_type prog_type; 67 }; 68 69 struct bpf_object_open_opts { 70 /* size of this struct, for forward/backward compatiblity */ 71 size_t sz; 72 /* object name override, if provided: 73 * - for object open from file, this will override setting object 74 * name from file path's base name; 75 * - for object open from memory buffer, this will specify an object 76 * name and will override default "<addr>-<buf-size>" name; 77 */ 78 const char *object_name; 79 /* parse map definitions non-strictly, allowing extra attributes/data */ 80 bool relaxed_maps; 81 /* DEPRECATED: handle CO-RE relocations non-strictly, allowing failures. 82 * Value is ignored. Relocations always are processed non-strictly. 83 * Non-relocatable instructions are replaced with invalid ones to 84 * prevent accidental errors. 85 * */ 86 LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect") 87 bool relaxed_core_relocs; 88 /* maps that set the 'pinning' attribute in their definition will have 89 * their pin_path attribute set to a file in this directory, and be 90 * auto-pinned to that path on load; defaults to "/sys/fs/bpf". 91 */ 92 const char *pin_root_path; 93 94 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__set_attach_target() on each individual bpf_program") 95 __u32 attach_prog_fd; 96 /* Additional kernel config content that augments and overrides 97 * system Kconfig for CONFIG_xxx externs. 98 */ 99 const char *kconfig; 100 /* Path to the custom BTF to be used for BPF CO-RE relocations. 101 * This custom BTF completely replaces the use of vmlinux BTF 102 * for the purpose of CO-RE relocations. 103 * NOTE: any other BPF feature (e.g., fentry/fexit programs, 104 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux. 105 */ 106 const char *btf_custom_path; 107 }; 108 #define bpf_object_open_opts__last_field btf_custom_path 109 110 LIBBPF_API struct bpf_object *bpf_object__open(const char *path); 111 LIBBPF_API struct bpf_object * 112 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts); 113 LIBBPF_API struct bpf_object * 114 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, 115 const struct bpf_object_open_opts *opts); 116 117 /* deprecated bpf_object__open variants */ 118 LIBBPF_API struct bpf_object * 119 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz, 120 const char *name); 121 LIBBPF_API struct bpf_object * 122 bpf_object__open_xattr(struct bpf_object_open_attr *attr); 123 124 enum libbpf_pin_type { 125 LIBBPF_PIN_NONE, 126 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */ 127 LIBBPF_PIN_BY_NAME, 128 }; 129 130 /* pin_maps and unpin_maps can both be called with a NULL path, in which case 131 * they will use the pin_path attribute of each map (and ignore all maps that 132 * don't have a pin_path set). 133 */ 134 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path); 135 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj, 136 const char *path); 137 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj, 138 const char *path); 139 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj, 140 const char *path); 141 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path); 142 LIBBPF_API void bpf_object__close(struct bpf_object *object); 143 144 struct bpf_object_load_attr { 145 struct bpf_object *obj; 146 int log_level; 147 const char *target_btf_path; 148 }; 149 150 /* Load/unload object into/from kernel */ 151 LIBBPF_API int bpf_object__load(struct bpf_object *obj); 152 LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr); 153 LIBBPF_DEPRECATED_SINCE(0, 6, "bpf_object__unload() is deprecated, use bpf_object__close() instead") 154 LIBBPF_API int bpf_object__unload(struct bpf_object *obj); 155 156 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj); 157 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj); 158 LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version); 159 160 struct btf; 161 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj); 162 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj); 163 164 LIBBPF_API struct bpf_program * 165 bpf_object__find_program_by_title(const struct bpf_object *obj, 166 const char *title); 167 LIBBPF_API struct bpf_program * 168 bpf_object__find_program_by_name(const struct bpf_object *obj, 169 const char *name); 170 171 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "track bpf_objects in application code instead") 172 struct bpf_object *bpf_object__next(struct bpf_object *prev); 173 #define bpf_object__for_each_safe(pos, tmp) \ 174 for ((pos) = bpf_object__next(NULL), \ 175 (tmp) = bpf_object__next(pos); \ 176 (pos) != NULL; \ 177 (pos) = (tmp), (tmp) = bpf_object__next(tmp)) 178 179 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *); 180 LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv, 181 bpf_object_clear_priv_t clear_priv); 182 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog); 183 184 LIBBPF_API int 185 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, 186 enum bpf_attach_type *expected_attach_type); 187 LIBBPF_API int libbpf_attach_type_by_name(const char *name, 188 enum bpf_attach_type *attach_type); 189 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name, 190 enum bpf_attach_type attach_type); 191 192 /* Accessors of bpf_program */ 193 struct bpf_program; 194 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_program() instead") 195 struct bpf_program *bpf_program__next(struct bpf_program *prog, 196 const struct bpf_object *obj); 197 LIBBPF_API struct bpf_program * 198 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog); 199 200 #define bpf_object__for_each_program(pos, obj) \ 201 for ((pos) = bpf_object__next_program((obj), NULL); \ 202 (pos) != NULL; \ 203 (pos) = bpf_object__next_program((obj), (pos))) 204 205 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_program() instead") 206 struct bpf_program *bpf_program__prev(struct bpf_program *prog, 207 const struct bpf_object *obj); 208 LIBBPF_API struct bpf_program * 209 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog); 210 211 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *); 212 213 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv, 214 bpf_program_clear_priv_t clear_priv); 215 216 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog); 217 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog, 218 __u32 ifindex); 219 220 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); 221 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog); 222 LIBBPF_API LIBBPF_DEPRECATED("BPF program title is confusing term; please use bpf_program__section_name() instead") 223 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy); 224 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog); 225 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload); 226 227 /* returns program size in bytes */ 228 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insn_cnt() instead") 229 LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog); 230 231 struct bpf_insn; 232 233 /** 234 * @brief **bpf_program__insns()** gives read-only access to BPF program's 235 * underlying BPF instructions. 236 * @param prog BPF program for which to return instructions 237 * @return a pointer to an array of BPF instructions that belong to the 238 * specified BPF program 239 * 240 * Returned pointer is always valid and not NULL. Number of `struct bpf_insn` 241 * pointed to can be fetched using **bpf_program__insn_cnt()** API. 242 * 243 * Keep in mind, libbpf can modify and append/delete BPF program's 244 * instructions as it processes BPF object file and prepares everything for 245 * uploading into the kernel. So depending on the point in BPF object 246 * lifetime, **bpf_program__insns()** can return different sets of 247 * instructions. As an example, during BPF object load phase BPF program 248 * instructions will be CO-RE-relocated, BPF subprograms instructions will be 249 * appended, ldimm64 instructions will have FDs embedded, etc. So instructions 250 * returned before **bpf_object__load()** and after it might be quite 251 * different. 252 */ 253 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog); 254 /** 255 * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s 256 * that form specified BPF program. 257 * @param prog BPF program for which to return number of BPF instructions 258 * 259 * See **bpf_program__insns()** documentation for notes on how libbpf can 260 * change instructions and their count during different phases of 261 * **bpf_object** lifetime. 262 */ 263 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog); 264 265 LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license, 266 __u32 kern_version); 267 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog); 268 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated") 269 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog, 270 const char *path, 271 int instance); 272 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated") 273 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog, 274 const char *path, 275 int instance); 276 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path); 277 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path); 278 LIBBPF_API void bpf_program__unload(struct bpf_program *prog); 279 280 struct bpf_link; 281 282 LIBBPF_API struct bpf_link *bpf_link__open(const char *path); 283 LIBBPF_API int bpf_link__fd(const struct bpf_link *link); 284 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link); 285 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path); 286 LIBBPF_API int bpf_link__unpin(struct bpf_link *link); 287 LIBBPF_API int bpf_link__update_program(struct bpf_link *link, 288 struct bpf_program *prog); 289 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link); 290 LIBBPF_API int bpf_link__detach(struct bpf_link *link); 291 LIBBPF_API int bpf_link__destroy(struct bpf_link *link); 292 293 LIBBPF_API struct bpf_link * 294 bpf_program__attach(const struct bpf_program *prog); 295 296 struct bpf_perf_event_opts { 297 /* size of this struct, for forward/backward compatiblity */ 298 size_t sz; 299 /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 300 __u64 bpf_cookie; 301 }; 302 #define bpf_perf_event_opts__last_field bpf_cookie 303 304 LIBBPF_API struct bpf_link * 305 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd); 306 307 LIBBPF_API struct bpf_link * 308 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd, 309 const struct bpf_perf_event_opts *opts); 310 311 struct bpf_kprobe_opts { 312 /* size of this struct, for forward/backward compatiblity */ 313 size_t sz; 314 /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 315 __u64 bpf_cookie; 316 /* function's offset to install kprobe to */ 317 size_t offset; 318 /* kprobe is return probe */ 319 bool retprobe; 320 size_t :0; 321 }; 322 #define bpf_kprobe_opts__last_field retprobe 323 324 LIBBPF_API struct bpf_link * 325 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe, 326 const char *func_name); 327 LIBBPF_API struct bpf_link * 328 bpf_program__attach_kprobe_opts(const struct bpf_program *prog, 329 const char *func_name, 330 const struct bpf_kprobe_opts *opts); 331 332 struct bpf_uprobe_opts { 333 /* size of this struct, for forward/backward compatiblity */ 334 size_t sz; 335 /* offset of kernel reference counted USDT semaphore, added in 336 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe") 337 */ 338 size_t ref_ctr_offset; 339 /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 340 __u64 bpf_cookie; 341 /* uprobe is return probe, invoked at function return time */ 342 bool retprobe; 343 size_t :0; 344 }; 345 #define bpf_uprobe_opts__last_field retprobe 346 347 LIBBPF_API struct bpf_link * 348 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe, 349 pid_t pid, const char *binary_path, 350 size_t func_offset); 351 LIBBPF_API struct bpf_link * 352 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, 353 const char *binary_path, size_t func_offset, 354 const struct bpf_uprobe_opts *opts); 355 356 struct bpf_tracepoint_opts { 357 /* size of this struct, for forward/backward compatiblity */ 358 size_t sz; 359 /* custom user-provided value fetchable through bpf_get_attach_cookie() */ 360 __u64 bpf_cookie; 361 }; 362 #define bpf_tracepoint_opts__last_field bpf_cookie 363 364 LIBBPF_API struct bpf_link * 365 bpf_program__attach_tracepoint(const struct bpf_program *prog, 366 const char *tp_category, 367 const char *tp_name); 368 LIBBPF_API struct bpf_link * 369 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog, 370 const char *tp_category, 371 const char *tp_name, 372 const struct bpf_tracepoint_opts *opts); 373 374 LIBBPF_API struct bpf_link * 375 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, 376 const char *tp_name); 377 LIBBPF_API struct bpf_link * 378 bpf_program__attach_trace(const struct bpf_program *prog); 379 LIBBPF_API struct bpf_link * 380 bpf_program__attach_lsm(const struct bpf_program *prog); 381 LIBBPF_API struct bpf_link * 382 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd); 383 LIBBPF_API struct bpf_link * 384 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd); 385 LIBBPF_API struct bpf_link * 386 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex); 387 LIBBPF_API struct bpf_link * 388 bpf_program__attach_freplace(const struct bpf_program *prog, 389 int target_fd, const char *attach_func_name); 390 391 struct bpf_map; 392 393 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map); 394 395 struct bpf_iter_attach_opts { 396 size_t sz; /* size of this struct for forward/backward compatibility */ 397 union bpf_iter_link_info *link_info; 398 __u32 link_info_len; 399 }; 400 #define bpf_iter_attach_opts__last_field link_info_len 401 402 LIBBPF_API struct bpf_link * 403 bpf_program__attach_iter(const struct bpf_program *prog, 404 const struct bpf_iter_attach_opts *opts); 405 406 /* 407 * Libbpf allows callers to adjust BPF programs before being loaded 408 * into kernel. One program in an object file can be transformed into 409 * multiple variants to be attached to different hooks. 410 * 411 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd 412 * form an API for this purpose. 413 * 414 * - bpf_program_prep_t: 415 * Defines a 'preprocessor', which is a caller defined function 416 * passed to libbpf through bpf_program__set_prep(), and will be 417 * called before program is loaded. The processor should adjust 418 * the program one time for each instance according to the instance id 419 * passed to it. 420 * 421 * - bpf_program__set_prep: 422 * Attaches a preprocessor to a BPF program. The number of instances 423 * that should be created is also passed through this function. 424 * 425 * - bpf_program__nth_fd: 426 * After the program is loaded, get resulting FD of a given instance 427 * of the BPF program. 428 * 429 * If bpf_program__set_prep() is not used, the program would be loaded 430 * without adjustment during bpf_object__load(). The program has only 431 * one instance. In this case bpf_program__fd(prog) is equal to 432 * bpf_program__nth_fd(prog, 0). 433 */ 434 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions") 435 struct bpf_prog_prep_result { 436 /* 437 * If not NULL, load new instruction array. 438 * If set to NULL, don't load this instance. 439 */ 440 struct bpf_insn *new_insn_ptr; 441 int new_insn_cnt; 442 443 /* If not NULL, result FD is written to it. */ 444 int *pfd; 445 }; 446 447 /* 448 * Parameters of bpf_program_prep_t: 449 * - prog: The bpf_program being loaded. 450 * - n: Index of instance being generated. 451 * - insns: BPF instructions array. 452 * - insns_cnt:Number of instructions in insns. 453 * - res: Output parameter, result of transformation. 454 * 455 * Return value: 456 * - Zero: pre-processing success. 457 * - Non-zero: pre-processing error, stop loading. 458 */ 459 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n, 460 struct bpf_insn *insns, int insns_cnt, 461 struct bpf_prog_prep_result *res); 462 463 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions") 464 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, 465 bpf_program_prep_t prep); 466 467 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated") 468 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n); 469 470 /* 471 * Adjust type of BPF program. Default is kprobe. 472 */ 473 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog); 474 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog); 475 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog); 476 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog); 477 LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog); 478 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog); 479 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog); 480 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog); 481 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog); 482 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog); 483 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog); 484 LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog); 485 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog); 486 487 LIBBPF_API enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog); 488 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog, 489 enum bpf_prog_type type); 490 491 LIBBPF_API enum bpf_attach_type 492 bpf_program__get_expected_attach_type(const struct bpf_program *prog); 493 LIBBPF_API void 494 bpf_program__set_expected_attach_type(struct bpf_program *prog, 495 enum bpf_attach_type type); 496 497 LIBBPF_API int 498 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd, 499 const char *attach_func_name); 500 501 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog); 502 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog); 503 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog); 504 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog); 505 LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog); 506 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog); 507 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog); 508 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog); 509 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog); 510 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog); 511 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog); 512 LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog); 513 LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog); 514 515 /* 516 * No need for __attribute__((packed)), all members of 'bpf_map_def' 517 * are all aligned. In addition, using __attribute__((packed)) 518 * would trigger a -Wpacked warning message, and lead to an error 519 * if -Werror is set. 520 */ 521 struct bpf_map_def { 522 unsigned int type; 523 unsigned int key_size; 524 unsigned int value_size; 525 unsigned int max_entries; 526 unsigned int map_flags; 527 }; 528 529 /** 530 * @brief **bpf_object__find_map_by_name()** returns BPF map of 531 * the given name, if it exists within the passed BPF object 532 * @param obj BPF object 533 * @param name name of the BPF map 534 * @return BPF map instance, if such map exists within the BPF object; 535 * or NULL otherwise. 536 */ 537 LIBBPF_API struct bpf_map * 538 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name); 539 540 LIBBPF_API int 541 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name); 542 543 /* 544 * Get bpf_map through the offset of corresponding struct bpf_map_def 545 * in the BPF object file. 546 */ 547 LIBBPF_API struct bpf_map * 548 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset); 549 550 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead") 551 struct bpf_map *bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj); 552 LIBBPF_API struct bpf_map * 553 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map); 554 555 #define bpf_object__for_each_map(pos, obj) \ 556 for ((pos) = bpf_object__next_map((obj), NULL); \ 557 (pos) != NULL; \ 558 (pos) = bpf_object__next_map((obj), (pos))) 559 #define bpf_map__for_each bpf_object__for_each_map 560 561 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_map() instead") 562 struct bpf_map *bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj); 563 LIBBPF_API struct bpf_map * 564 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map); 565 566 /** 567 * @brief **bpf_map__fd()** gets the file descriptor of the passed 568 * BPF map 569 * @param map the BPF map instance 570 * @return the file descriptor; or -EINVAL in case of an error 571 */ 572 LIBBPF_API int bpf_map__fd(const struct bpf_map *map); 573 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd); 574 /* get map definition */ 575 LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map); 576 /* get map name */ 577 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map); 578 /* get/set map type */ 579 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map); 580 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type); 581 /* get/set map size (max_entries) */ 582 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map); 583 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries); 584 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries); 585 /* get/set map flags */ 586 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map); 587 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags); 588 /* get/set map NUMA node */ 589 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map); 590 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node); 591 /* get/set map key size */ 592 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map); 593 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size); 594 /* get/set map value size */ 595 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map); 596 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size); 597 /* get map key/value BTF type IDs */ 598 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map); 599 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map); 600 /* get/set map if_index */ 601 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map); 602 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); 603 /* get/set map map_extra flags */ 604 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map); 605 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra); 606 607 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); 608 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv, 609 bpf_map_clear_priv_t clear_priv); 610 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map); 611 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map, 612 const void *data, size_t size); 613 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize); 614 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map); 615 616 /** 617 * @brief **bpf_map__is_internal()** tells the caller whether or not the 618 * passed map is a special map created by libbpf automatically for things like 619 * global variables, __ksym externs, Kconfig values, etc 620 * @param map the bpf_map 621 * @return true, if the map is an internal map; false, otherwise 622 */ 623 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map); 624 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path); 625 LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map); 626 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map); 627 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map); 628 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path); 629 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path); 630 631 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd); 632 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map); 633 634 /** 635 * @brief **libbpf_get_error()** extracts the error code from the passed 636 * pointer 637 * @param ptr pointer returned from libbpf API function 638 * @return error code; or 0 if no error occured 639 * 640 * Many libbpf API functions which return pointers have logic to encode error 641 * codes as pointers, and do not return NULL. Meaning **libbpf_get_error()** 642 * should be used on the return value from these functions immediately after 643 * calling the API function, with no intervening calls that could clobber the 644 * `errno` variable. Consult the individual functions documentation to verify 645 * if this logic applies should be used. 646 * 647 * For these API functions, if `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)` 648 * is enabled, NULL is returned on error instead. 649 * 650 * If ptr is NULL, then errno should be already set by the failing 651 * API, because libbpf never returns NULL on success and it now always 652 * sets errno on error. 653 * 654 * Example usage: 655 * 656 * struct perf_buffer *pb; 657 * 658 * pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &opts); 659 * err = libbpf_get_error(pb); 660 * if (err) { 661 * pb = NULL; 662 * fprintf(stderr, "failed to open perf buffer: %d\n", err); 663 * goto cleanup; 664 * } 665 */ 666 LIBBPF_API long libbpf_get_error(const void *ptr); 667 668 struct bpf_prog_load_attr { 669 const char *file; 670 enum bpf_prog_type prog_type; 671 enum bpf_attach_type expected_attach_type; 672 int ifindex; 673 int log_level; 674 int prog_flags; 675 }; 676 677 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, 678 struct bpf_object **pobj, int *prog_fd); 679 LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type, 680 struct bpf_object **pobj, int *prog_fd); 681 682 /* XDP related API */ 683 struct xdp_link_info { 684 __u32 prog_id; 685 __u32 drv_prog_id; 686 __u32 hw_prog_id; 687 __u32 skb_prog_id; 688 __u8 attach_mode; 689 }; 690 691 struct bpf_xdp_set_link_opts { 692 size_t sz; 693 int old_fd; 694 size_t :0; 695 }; 696 #define bpf_xdp_set_link_opts__last_field old_fd 697 698 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); 699 LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags, 700 const struct bpf_xdp_set_link_opts *opts); 701 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags); 702 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info, 703 size_t info_size, __u32 flags); 704 705 /* TC related API */ 706 enum bpf_tc_attach_point { 707 BPF_TC_INGRESS = 1 << 0, 708 BPF_TC_EGRESS = 1 << 1, 709 BPF_TC_CUSTOM = 1 << 2, 710 }; 711 712 #define BPF_TC_PARENT(a, b) \ 713 ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU)) 714 715 enum bpf_tc_flags { 716 BPF_TC_F_REPLACE = 1 << 0, 717 }; 718 719 struct bpf_tc_hook { 720 size_t sz; 721 int ifindex; 722 enum bpf_tc_attach_point attach_point; 723 __u32 parent; 724 size_t :0; 725 }; 726 #define bpf_tc_hook__last_field parent 727 728 struct bpf_tc_opts { 729 size_t sz; 730 int prog_fd; 731 __u32 flags; 732 __u32 prog_id; 733 __u32 handle; 734 __u32 priority; 735 size_t :0; 736 }; 737 #define bpf_tc_opts__last_field priority 738 739 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook); 740 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook); 741 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook, 742 struct bpf_tc_opts *opts); 743 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook, 744 const struct bpf_tc_opts *opts); 745 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook, 746 struct bpf_tc_opts *opts); 747 748 /* Ring buffer APIs */ 749 struct ring_buffer; 750 751 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size); 752 753 struct ring_buffer_opts { 754 size_t sz; /* size of this struct, for forward/backward compatiblity */ 755 }; 756 757 #define ring_buffer_opts__last_field sz 758 759 LIBBPF_API struct ring_buffer * 760 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx, 761 const struct ring_buffer_opts *opts); 762 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb); 763 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd, 764 ring_buffer_sample_fn sample_cb, void *ctx); 765 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms); 766 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb); 767 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb); 768 769 /* Perf buffer APIs */ 770 struct perf_buffer; 771 772 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu, 773 void *data, __u32 size); 774 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt); 775 776 /* common use perf buffer options */ 777 struct perf_buffer_opts { 778 /* if specified, sample_cb is called for each sample */ 779 perf_buffer_sample_fn sample_cb; 780 /* if specified, lost_cb is called for each batch of lost samples */ 781 perf_buffer_lost_fn lost_cb; 782 /* ctx is provided to sample_cb and lost_cb */ 783 void *ctx; 784 }; 785 786 LIBBPF_API struct perf_buffer * 787 perf_buffer__new(int map_fd, size_t page_cnt, 788 const struct perf_buffer_opts *opts); 789 790 enum bpf_perf_event_ret { 791 LIBBPF_PERF_EVENT_DONE = 0, 792 LIBBPF_PERF_EVENT_ERROR = -1, 793 LIBBPF_PERF_EVENT_CONT = -2, 794 }; 795 796 struct perf_event_header; 797 798 typedef enum bpf_perf_event_ret 799 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event); 800 801 /* raw perf buffer options, giving most power and control */ 802 struct perf_buffer_raw_opts { 803 /* perf event attrs passed directly into perf_event_open() */ 804 struct perf_event_attr *attr; 805 /* raw event callback */ 806 perf_buffer_event_fn event_cb; 807 /* ctx is provided to event_cb */ 808 void *ctx; 809 /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of 810 * max_entries of given PERF_EVENT_ARRAY map) 811 */ 812 int cpu_cnt; 813 /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */ 814 int *cpus; 815 /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */ 816 int *map_keys; 817 }; 818 819 LIBBPF_API struct perf_buffer * 820 perf_buffer__new_raw(int map_fd, size_t page_cnt, 821 const struct perf_buffer_raw_opts *opts); 822 823 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb); 824 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb); 825 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms); 826 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb); 827 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx); 828 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb); 829 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx); 830 831 typedef enum bpf_perf_event_ret 832 (*bpf_perf_event_print_t)(struct perf_event_header *hdr, 833 void *private_data); 834 LIBBPF_API enum bpf_perf_event_ret 835 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, 836 void **copy_mem, size_t *copy_size, 837 bpf_perf_event_print_t fn, void *private_data); 838 839 struct bpf_prog_linfo; 840 struct bpf_prog_info; 841 842 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo); 843 LIBBPF_API struct bpf_prog_linfo * 844 bpf_prog_linfo__new(const struct bpf_prog_info *info); 845 LIBBPF_API const struct bpf_line_info * 846 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo, 847 __u64 addr, __u32 func_idx, __u32 nr_skip); 848 LIBBPF_API const struct bpf_line_info * 849 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo, 850 __u32 insn_off, __u32 nr_skip); 851 852 /* 853 * Probe for supported system features 854 * 855 * Note that running many of these probes in a short amount of time can cause 856 * the kernel to reach the maximal size of lockable memory allowed for the 857 * user, causing subsequent probes to fail. In this case, the caller may want 858 * to adjust that limit with setrlimit(). 859 */ 860 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, 861 __u32 ifindex); 862 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex); 863 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, 864 enum bpf_prog_type prog_type, __u32 ifindex); 865 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex); 866 867 /* 868 * Get bpf_prog_info in continuous memory 869 * 870 * struct bpf_prog_info has multiple arrays. The user has option to choose 871 * arrays to fetch from kernel. The following APIs provide an uniform way to 872 * fetch these data. All arrays in bpf_prog_info are stored in a single 873 * continuous memory region. This makes it easy to store the info in a 874 * file. 875 * 876 * Before writing bpf_prog_info_linear to files, it is necessary to 877 * translate pointers in bpf_prog_info to offsets. Helper functions 878 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr() 879 * are introduced to switch between pointers and offsets. 880 * 881 * Examples: 882 * # To fetch map_ids and prog_tags: 883 * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) | 884 * (1UL << BPF_PROG_INFO_PROG_TAGS); 885 * struct bpf_prog_info_linear *info_linear = 886 * bpf_program__get_prog_info_linear(fd, arrays); 887 * 888 * # To save data in file 889 * bpf_program__bpil_addr_to_offs(info_linear); 890 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len); 891 * 892 * # To read data from file 893 * read(f, info_linear, <proper_size>); 894 * bpf_program__bpil_offs_to_addr(info_linear); 895 */ 896 enum bpf_prog_info_array { 897 BPF_PROG_INFO_FIRST_ARRAY = 0, 898 BPF_PROG_INFO_JITED_INSNS = 0, 899 BPF_PROG_INFO_XLATED_INSNS, 900 BPF_PROG_INFO_MAP_IDS, 901 BPF_PROG_INFO_JITED_KSYMS, 902 BPF_PROG_INFO_JITED_FUNC_LENS, 903 BPF_PROG_INFO_FUNC_INFO, 904 BPF_PROG_INFO_LINE_INFO, 905 BPF_PROG_INFO_JITED_LINE_INFO, 906 BPF_PROG_INFO_PROG_TAGS, 907 BPF_PROG_INFO_LAST_ARRAY, 908 }; 909 910 struct bpf_prog_info_linear { 911 /* size of struct bpf_prog_info, when the tool is compiled */ 912 __u32 info_len; 913 /* total bytes allocated for data, round up to 8 bytes */ 914 __u32 data_len; 915 /* which arrays are included in data */ 916 __u64 arrays; 917 struct bpf_prog_info info; 918 __u8 data[]; 919 }; 920 921 LIBBPF_API struct bpf_prog_info_linear * 922 bpf_program__get_prog_info_linear(int fd, __u64 arrays); 923 924 LIBBPF_API void 925 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear); 926 927 LIBBPF_API void 928 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear); 929 930 /** 931 * @brief **libbpf_num_possible_cpus()** is a helper function to get the 932 * number of possible CPUs that the host kernel supports and expects. 933 * @return number of possible CPUs; or error code on failure 934 * 935 * Example usage: 936 * 937 * int ncpus = libbpf_num_possible_cpus(); 938 * if (ncpus < 0) { 939 * // error handling 940 * } 941 * long values[ncpus]; 942 * bpf_map_lookup_elem(per_cpu_map_fd, key, values); 943 */ 944 LIBBPF_API int libbpf_num_possible_cpus(void); 945 946 struct bpf_map_skeleton { 947 const char *name; 948 struct bpf_map **map; 949 void **mmaped; 950 }; 951 952 struct bpf_prog_skeleton { 953 const char *name; 954 struct bpf_program **prog; 955 struct bpf_link **link; 956 }; 957 958 struct bpf_object_skeleton { 959 size_t sz; /* size of this struct, for forward/backward compatibility */ 960 961 const char *name; 962 const void *data; 963 size_t data_sz; 964 965 struct bpf_object **obj; 966 967 int map_cnt; 968 int map_skel_sz; /* sizeof(struct bpf_skeleton_map) */ 969 struct bpf_map_skeleton *maps; 970 971 int prog_cnt; 972 int prog_skel_sz; /* sizeof(struct bpf_skeleton_prog) */ 973 struct bpf_prog_skeleton *progs; 974 }; 975 976 LIBBPF_API int 977 bpf_object__open_skeleton(struct bpf_object_skeleton *s, 978 const struct bpf_object_open_opts *opts); 979 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s); 980 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s); 981 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s); 982 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s); 983 984 struct gen_loader_opts { 985 size_t sz; /* size of this struct, for forward/backward compatiblity */ 986 const char *data; 987 const char *insns; 988 __u32 data_sz; 989 __u32 insns_sz; 990 }; 991 992 #define gen_loader_opts__last_field insns_sz 993 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj, 994 struct gen_loader_opts *opts); 995 996 enum libbpf_tristate { 997 TRI_NO = 0, 998 TRI_YES = 1, 999 TRI_MODULE = 2, 1000 }; 1001 1002 struct bpf_linker_opts { 1003 /* size of this struct, for forward/backward compatiblity */ 1004 size_t sz; 1005 }; 1006 #define bpf_linker_opts__last_field sz 1007 1008 struct bpf_linker_file_opts { 1009 /* size of this struct, for forward/backward compatiblity */ 1010 size_t sz; 1011 }; 1012 #define bpf_linker_file_opts__last_field sz 1013 1014 struct bpf_linker; 1015 1016 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts); 1017 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, 1018 const char *filename, 1019 const struct bpf_linker_file_opts *opts); 1020 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); 1021 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); 1022 1023 #ifdef __cplusplus 1024 } /* extern "C" */ 1025 #endif 1026 1027 #endif /* __LIBBPF_LIBBPF_H */ 1028