1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 3 /* 4 * Common BPF ELF 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 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU Lesser General Public 12 * License as published by the Free Software Foundation; 13 * version 2.1 of the License (not later!) 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License for more details. 19 * 20 * You should have received a copy of the GNU Lesser General Public 21 * License along with this program; if not, see <http://www.gnu.org/licenses> 22 */ 23 #ifndef __LIBBPF_BPF_H 24 #define __LIBBPF_BPF_H 25 26 #include <linux/bpf.h> 27 #include <stdbool.h> 28 #include <stddef.h> 29 #include <stdint.h> 30 31 #include "libbpf_common.h" 32 #include "libbpf_legacy.h" 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes); 39 40 struct bpf_log_opts { 41 size_t sz; /* size of this struct for forward/backward compatibility */ 42 43 char *buf; 44 __u32 size; 45 __u32 level; 46 __u32 true_size; /* out parameter set by kernel */ 47 48 size_t :0; 49 }; 50 #define bpf_log_opts__last_field true_size 51 52 struct bpf_map_create_opts { 53 size_t sz; /* size of this struct for forward/backward compatibility */ 54 55 __u32 btf_fd; 56 __u32 btf_key_type_id; 57 __u32 btf_value_type_id; 58 __u32 btf_vmlinux_value_type_id; 59 60 __u32 inner_map_fd; 61 __u32 map_flags; 62 __u64 map_extra; 63 64 __u32 numa_node; 65 __u32 map_ifindex; 66 __s32 value_type_btf_obj_fd; 67 68 __u32 token_fd; 69 70 const void *excl_prog_hash; 71 __u32 excl_prog_hash_size; 72 73 struct bpf_log_opts *log_opts; 74 75 size_t :0; 76 }; 77 #define bpf_map_create_opts__last_field log_opts 78 79 LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, 80 const char *map_name, 81 __u32 key_size, 82 __u32 value_size, 83 __u32 max_entries, 84 const struct bpf_map_create_opts *opts); 85 86 struct bpf_prog_load_opts { 87 size_t sz; /* size of this struct for forward/backward compatibility */ 88 89 /* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns 90 * -EAGAIN. This field determines how many attempts libbpf has to 91 * make. If not specified, libbpf will use default value of 5. 92 */ 93 int attempts; 94 95 enum bpf_attach_type expected_attach_type; 96 __u32 prog_btf_fd; 97 __u32 prog_flags; 98 __u32 prog_ifindex; 99 __u32 kern_version; 100 101 __u32 attach_btf_id; 102 __u32 attach_prog_fd; 103 __u32 attach_btf_obj_fd; 104 105 const int *fd_array; 106 107 /* .BTF.ext func info data */ 108 const void *func_info; 109 __u32 func_info_cnt; 110 __u32 func_info_rec_size; 111 112 /* .BTF.ext line info data */ 113 const void *line_info; 114 __u32 line_info_cnt; 115 __u32 line_info_rec_size; 116 117 /* verifier log options */ 118 __u32 log_level; 119 __u32 log_size; 120 char *log_buf; 121 /* output: actual total log contents size (including terminating zero). 122 * It could be both larger than original log_size (if log was 123 * truncated), or smaller (if log buffer wasn't filled completely). 124 * If kernel doesn't support this feature, log_size is left unchanged. 125 */ 126 __u32 log_true_size; 127 __u32 token_fd; 128 129 /* if set, provides the length of fd_array */ 130 __u32 fd_array_cnt; 131 size_t :0; 132 }; 133 #define bpf_prog_load_opts__last_field fd_array_cnt 134 135 LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type, 136 const char *prog_name, const char *license, 137 const struct bpf_insn *insns, size_t insn_cnt, 138 struct bpf_prog_load_opts *opts); 139 140 /* Flags to direct loading requirements */ 141 #define MAPS_RELAX_COMPAT 0x01 142 143 /* Recommended log buffer size */ 144 #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */ 145 146 struct bpf_btf_load_opts { 147 size_t sz; /* size of this struct for forward/backward compatibility */ 148 149 /* kernel log options */ 150 char *log_buf; 151 __u32 log_level; 152 __u32 log_size; 153 /* output: actual total log contents size (including terminating zero). 154 * It could be both larger than original log_size (if log was 155 * truncated), or smaller (if log buffer wasn't filled completely). 156 * If kernel doesn't support this feature, log_size is left unchanged. 157 */ 158 __u32 log_true_size; 159 160 __u32 btf_flags; 161 __u32 token_fd; 162 size_t :0; 163 }; 164 #define bpf_btf_load_opts__last_field token_fd 165 166 LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size, 167 struct bpf_btf_load_opts *opts); 168 169 LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value, 170 __u64 flags); 171 172 LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value); 173 LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, 174 __u64 flags); 175 LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key, 176 void *value); 177 LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, 178 void *value, __u64 flags); 179 LIBBPF_API int bpf_map_delete_elem(int fd, const void *key); 180 LIBBPF_API int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags); 181 LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key); 182 LIBBPF_API int bpf_map_freeze(int fd); 183 184 struct bpf_map_batch_opts { 185 size_t sz; /* size of this struct for forward/backward compatibility */ 186 __u64 elem_flags; 187 __u64 flags; 188 }; 189 #define bpf_map_batch_opts__last_field flags 190 191 192 /** 193 * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple 194 * elements in a BPF map. 195 * 196 * @param fd BPF map file descriptor 197 * @param keys pointer to an array of *count* keys 198 * @param count input and output parameter; on input **count** represents the 199 * number of elements in the map to delete in batch; 200 * on output if a non-EFAULT error is returned, **count** represents the number of deleted 201 * elements if the output **count** value is not equal to the input **count** value 202 * If EFAULT is returned, **count** should not be trusted to be correct. 203 * @param opts options for configuring the way the batch deletion works 204 * @return 0, on success; negative error code, otherwise (errno is also set to 205 * the error code) 206 */ 207 LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys, 208 __u32 *count, 209 const struct bpf_map_batch_opts *opts); 210 211 /** 212 * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements. 213 * 214 * The parameter *in_batch* is the address of the first element in the batch to 215 * read. *out_batch* is an output parameter that should be passed as *in_batch* 216 * to subsequent calls to **bpf_map_lookup_batch()**. NULL can be passed for 217 * *in_batch* to indicate that the batched lookup starts from the beginning of 218 * the map. Both *in_batch* and *out_batch* must point to memory large enough to 219 * hold a single key, except for maps of type **BPF_MAP_TYPE_{HASH, PERCPU_HASH, 220 * LRU_HASH, LRU_PERCPU_HASH}**, for which the memory size must be at 221 * least 4 bytes wide regardless of key size. 222 * 223 * The *keys* and *values* are output parameters which must point to memory large enough to 224 * hold *count* items based on the key and value size of the map *map_fd*. The *keys* 225 * buffer must be of *key_size* * *count*. The *values* buffer must be of 226 * *value_size* * *count*. 227 * 228 * @param fd BPF map file descriptor 229 * @param in_batch address of the first element in batch to read, can pass NULL to 230 * indicate that the batched lookup starts from the beginning of the map. 231 * @param out_batch output parameter that should be passed to next call as *in_batch* 232 * @param keys pointer to an array large enough for *count* keys 233 * @param values pointer to an array large enough for *count* values 234 * @param count input and output parameter; on input it's the number of elements 235 * in the map to read in batch; on output it's the number of elements that were 236 * successfully read. 237 * If a non-EFAULT error is returned, count will be set as the number of elements 238 * that were read before the error occurred. 239 * If EFAULT is returned, **count** should not be trusted to be correct. 240 * @param opts options for configuring the way the batch lookup works 241 * @return 0, on success; negative error code, otherwise (errno is also set to 242 * the error code) 243 */ 244 LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, 245 void *keys, void *values, __u32 *count, 246 const struct bpf_map_batch_opts *opts); 247 248 /** 249 * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion 250 * of BPF map elements where each element is deleted after being retrieved. 251 * 252 * @param fd BPF map file descriptor 253 * @param in_batch address of the first element in batch to read, can pass NULL to 254 * get address of the first element in *out_batch*. If not NULL, must be large 255 * enough to hold a key. For **BPF_MAP_TYPE_{HASH, PERCPU_HASH, LRU_HASH, 256 * LRU_PERCPU_HASH}**, the memory size must be at least 4 bytes wide regardless 257 * of key size. 258 * @param out_batch output parameter that should be passed to next call as *in_batch* 259 * @param keys pointer to an array of *count* keys 260 * @param values pointer to an array large enough for *count* values 261 * @param count input and output parameter; on input it's the number of elements 262 * in the map to read and delete in batch; on output it represents the number of 263 * elements that were successfully read and deleted 264 * If a non-**EFAULT** error code is returned and if the output **count** value 265 * is not equal to the input **count** value, up to **count** elements may 266 * have been deleted. 267 * if **EFAULT** is returned up to *count* elements may have been deleted without 268 * being returned via the *keys* and *values* output parameters. 269 * @param opts options for configuring the way the batch lookup and delete works 270 * @return 0, on success; negative error code, otherwise (errno is also set to 271 * the error code) 272 */ 273 LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, 274 void *out_batch, void *keys, 275 void *values, __u32 *count, 276 const struct bpf_map_batch_opts *opts); 277 278 /** 279 * @brief **bpf_map_update_batch()** updates multiple elements in a map 280 * by specifying keys and their corresponding values. 281 * 282 * The *keys* and *values* parameters must point to memory large enough 283 * to hold *count* items based on the key and value size of the map. 284 * 285 * The *opts* parameter can be used to control how *bpf_map_update_batch()* 286 * should handle keys that either do or do not already exist in the map. 287 * In particular the *flags* parameter of *bpf_map_batch_opts* can be 288 * one of the following: 289 * 290 * Note that *count* is an input and output parameter, where on output it 291 * represents how many elements were successfully updated. Also note that if 292 * **EFAULT** then *count* should not be trusted to be correct. 293 * 294 * **BPF_ANY** 295 * Create new elements or update existing. 296 * 297 * **BPF_NOEXIST** 298 * Create new elements only if they do not exist. 299 * 300 * **BPF_EXIST** 301 * Update existing elements. 302 * 303 * **BPF_F_LOCK** 304 * Update spin_lock-ed map elements. This must be 305 * specified if the map value contains a spinlock. 306 * 307 * **BPF_F_CPU** 308 * As for percpu maps, update value on the specified CPU. And the cpu 309 * info is embedded into the high 32 bits of **opts->elem_flags**. 310 * 311 * **BPF_F_ALL_CPUS** 312 * As for percpu maps, update value across all CPUs. This flag cannot 313 * be used with BPF_F_CPU at the same time. 314 * 315 * @param fd BPF map file descriptor 316 * @param keys pointer to an array of *count* keys 317 * @param values pointer to an array of *count* values 318 * @param count input and output parameter; on input it's the number of elements 319 * in the map to update in batch; on output if a non-EFAULT error is returned, 320 * **count** represents the number of updated elements if the output **count** 321 * value is not equal to the input **count** value. 322 * If EFAULT is returned, **count** should not be trusted to be correct. 323 * @param opts options for configuring the way the batch update works 324 * @return 0, on success; negative error code, otherwise (errno is also set to 325 * the error code) 326 */ 327 LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values, 328 __u32 *count, 329 const struct bpf_map_batch_opts *opts); 330 331 struct bpf_obj_pin_opts { 332 size_t sz; /* size of this struct for forward/backward compatibility */ 333 334 __u32 file_flags; 335 int path_fd; 336 337 size_t :0; 338 }; 339 #define bpf_obj_pin_opts__last_field path_fd 340 341 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname); 342 LIBBPF_API int bpf_obj_pin_opts(int fd, const char *pathname, 343 const struct bpf_obj_pin_opts *opts); 344 345 struct bpf_obj_get_opts { 346 size_t sz; /* size of this struct for forward/backward compatibility */ 347 348 __u32 file_flags; 349 int path_fd; 350 351 size_t :0; 352 }; 353 #define bpf_obj_get_opts__last_field path_fd 354 355 LIBBPF_API int bpf_obj_get(const char *pathname); 356 LIBBPF_API int bpf_obj_get_opts(const char *pathname, 357 const struct bpf_obj_get_opts *opts); 358 359 LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd, 360 enum bpf_attach_type type, unsigned int flags); 361 LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); 362 LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd, 363 enum bpf_attach_type type); 364 365 struct bpf_prog_attach_opts { 366 size_t sz; /* size of this struct for forward/backward compatibility */ 367 __u32 flags; 368 union { 369 int replace_prog_fd; 370 int replace_fd; 371 }; 372 int relative_fd; 373 __u32 relative_id; 374 __u64 expected_revision; 375 size_t :0; 376 }; 377 #define bpf_prog_attach_opts__last_field expected_revision 378 379 struct bpf_prog_detach_opts { 380 size_t sz; /* size of this struct for forward/backward compatibility */ 381 __u32 flags; 382 int relative_fd; 383 __u32 relative_id; 384 __u64 expected_revision; 385 size_t :0; 386 }; 387 #define bpf_prog_detach_opts__last_field expected_revision 388 389 /** 390 * @brief **bpf_prog_attach_opts()** attaches the BPF program corresponding to 391 * *prog_fd* to a *target* which can represent a file descriptor or netdevice 392 * ifindex. 393 * 394 * @param prog_fd BPF program file descriptor 395 * @param target attach location file descriptor or ifindex 396 * @param type attach type for the BPF program 397 * @param opts options for configuring the attachment 398 * @return 0, on success; negative error code, otherwise (errno is also set to 399 * the error code) 400 */ 401 LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int target, 402 enum bpf_attach_type type, 403 const struct bpf_prog_attach_opts *opts); 404 405 /** 406 * @brief **bpf_prog_detach_opts()** detaches the BPF program corresponding to 407 * *prog_fd* from a *target* which can represent a file descriptor or netdevice 408 * ifindex. 409 * 410 * @param prog_fd BPF program file descriptor 411 * @param target detach location file descriptor or ifindex 412 * @param type detach type for the BPF program 413 * @param opts options for configuring the detachment 414 * @return 0, on success; negative error code, otherwise (errno is also set to 415 * the error code) 416 */ 417 LIBBPF_API int bpf_prog_detach_opts(int prog_fd, int target, 418 enum bpf_attach_type type, 419 const struct bpf_prog_detach_opts *opts); 420 421 union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */ 422 struct bpf_link_create_opts { 423 size_t sz; /* size of this struct for forward/backward compatibility */ 424 __u32 flags; 425 union bpf_iter_link_info *iter_info; 426 __u32 iter_info_len; 427 __u32 target_btf_id; 428 union { 429 struct { 430 __u64 bpf_cookie; 431 } perf_event; 432 struct { 433 __u32 flags; 434 __u32 cnt; 435 const char **syms; 436 const unsigned long *addrs; 437 const __u64 *cookies; 438 } kprobe_multi; 439 struct { 440 __u32 flags; 441 __u32 cnt; 442 const char *path; 443 const unsigned long *offsets; 444 const unsigned long *ref_ctr_offsets; 445 const __u64 *cookies; 446 __u32 pid; 447 } uprobe_multi; 448 struct { 449 __u64 cookie; 450 } tracing; 451 struct { 452 __u32 pf; 453 __u32 hooknum; 454 __s32 priority; 455 __u32 flags; 456 } netfilter; 457 struct { 458 __u32 relative_fd; 459 __u32 relative_id; 460 __u64 expected_revision; 461 } tcx; 462 struct { 463 __u32 relative_fd; 464 __u32 relative_id; 465 __u64 expected_revision; 466 } netkit; 467 struct { 468 __u32 relative_fd; 469 __u32 relative_id; 470 __u64 expected_revision; 471 } cgroup; 472 struct { 473 const __u32 *ids; 474 const __u64 *cookies; 475 __u32 cnt; 476 } tracing_multi; 477 }; 478 size_t :0; 479 }; 480 #define bpf_link_create_opts__last_field uprobe_multi.pid 481 482 LIBBPF_API int bpf_link_create(int prog_fd, int target_fd, 483 enum bpf_attach_type attach_type, 484 const struct bpf_link_create_opts *opts); 485 486 LIBBPF_API int bpf_link_detach(int link_fd); 487 488 struct bpf_link_update_opts { 489 size_t sz; /* size of this struct for forward/backward compatibility */ 490 __u32 flags; /* extra flags */ 491 __u32 old_prog_fd; /* expected old program FD */ 492 __u32 old_map_fd; /* expected old map FD */ 493 }; 494 #define bpf_link_update_opts__last_field old_map_fd 495 496 LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd, 497 const struct bpf_link_update_opts *opts); 498 499 LIBBPF_API int bpf_iter_create(int link_fd); 500 501 struct bpf_prog_test_run_attr { 502 int prog_fd; 503 int repeat; 504 const void *data_in; 505 __u32 data_size_in; 506 void *data_out; /* optional */ 507 __u32 data_size_out; /* in: max length of data_out 508 * out: length of data_out */ 509 __u32 retval; /* out: return code of the BPF program */ 510 __u32 duration; /* out: average per repetition in ns */ 511 const void *ctx_in; /* optional */ 512 __u32 ctx_size_in; 513 void *ctx_out; /* optional */ 514 __u32 ctx_size_out; /* in: max length of ctx_out 515 * out: length of cxt_out */ 516 }; 517 518 LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id); 519 LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); 520 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id); 521 LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id); 522 523 struct bpf_get_fd_by_id_opts { 524 size_t sz; /* size of this struct for forward/backward compatibility */ 525 __u32 open_flags; /* permissions requested for the operation on fd */ 526 __u32 token_fd; 527 size_t :0; 528 }; 529 #define bpf_get_fd_by_id_opts__last_field token_fd 530 531 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id); 532 LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id, 533 const struct bpf_get_fd_by_id_opts *opts); 534 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id); 535 LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id, 536 const struct bpf_get_fd_by_id_opts *opts); 537 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id); 538 LIBBPF_API int bpf_btf_get_fd_by_id_opts(__u32 id, 539 const struct bpf_get_fd_by_id_opts *opts); 540 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id); 541 LIBBPF_API int bpf_link_get_fd_by_id_opts(__u32 id, 542 const struct bpf_get_fd_by_id_opts *opts); 543 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len); 544 545 /** 546 * @brief **bpf_prog_get_info_by_fd()** obtains information about the BPF 547 * program corresponding to *prog_fd*. 548 * 549 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 550 * actual number of bytes written to *info*. Note that *info* should be 551 * zero-initialized or initialized as expected by the requested *info* 552 * type. Failing to (zero-)initialize *info* under certain circumstances can 553 * result in this helper returning an error. 554 * 555 * @param prog_fd BPF program file descriptor 556 * @param info pointer to **struct bpf_prog_info** that will be populated with 557 * BPF program information 558 * @param info_len pointer to the size of *info*; on success updated with the 559 * number of bytes written to *info* 560 * @return 0, on success; negative error code, otherwise (errno is also set to 561 * the error code) 562 */ 563 LIBBPF_API int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len); 564 565 /** 566 * @brief **bpf_map_get_info_by_fd()** obtains information about the BPF 567 * map corresponding to *map_fd*. 568 * 569 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 570 * actual number of bytes written to *info*. Note that *info* should be 571 * zero-initialized or initialized as expected by the requested *info* 572 * type. Failing to (zero-)initialize *info* under certain circumstances can 573 * result in this helper returning an error. 574 * 575 * @param map_fd BPF map file descriptor 576 * @param info pointer to **struct bpf_map_info** that will be populated with 577 * BPF map information 578 * @param info_len pointer to the size of *info*; on success updated with the 579 * number of bytes written to *info* 580 * @return 0, on success; negative error code, otherwise (errno is also set to 581 * the error code) 582 */ 583 LIBBPF_API int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len); 584 585 /** 586 * @brief **bpf_btf_get_info_by_fd()** obtains information about the 587 * BTF object corresponding to *btf_fd*. 588 * 589 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 590 * actual number of bytes written to *info*. Note that *info* should be 591 * zero-initialized or initialized as expected by the requested *info* 592 * type. Failing to (zero-)initialize *info* under certain circumstances can 593 * result in this helper returning an error. 594 * 595 * @param btf_fd BTF object file descriptor 596 * @param info pointer to **struct bpf_btf_info** that will be populated with 597 * BTF object information 598 * @param info_len pointer to the size of *info*; on success updated with the 599 * number of bytes written to *info* 600 * @return 0, on success; negative error code, otherwise (errno is also set to 601 * the error code) 602 */ 603 LIBBPF_API int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len); 604 605 /** 606 * @brief **bpf_btf_get_info_by_fd()** obtains information about the BPF 607 * link corresponding to *link_fd*. 608 * 609 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 610 * actual number of bytes written to *info*. Note that *info* should be 611 * zero-initialized or initialized as expected by the requested *info* 612 * type. Failing to (zero-)initialize *info* under certain circumstances can 613 * result in this helper returning an error. 614 * 615 * @param link_fd BPF link file descriptor 616 * @param info pointer to **struct bpf_link_info** that will be populated with 617 * BPF link information 618 * @param info_len pointer to the size of *info*; on success updated with the 619 * number of bytes written to *info* 620 * @return 0, on success; negative error code, otherwise (errno is also set to 621 * the error code) 622 */ 623 LIBBPF_API int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len); 624 625 struct bpf_prog_query_opts { 626 size_t sz; /* size of this struct for forward/backward compatibility */ 627 __u32 query_flags; 628 __u32 attach_flags; /* output argument */ 629 __u32 *prog_ids; 630 union { 631 /* input+output argument */ 632 __u32 prog_cnt; 633 __u32 count; 634 }; 635 __u32 *prog_attach_flags; 636 __u32 *link_ids; 637 __u32 *link_attach_flags; 638 __u64 revision; 639 size_t :0; 640 }; 641 #define bpf_prog_query_opts__last_field revision 642 643 /** 644 * @brief **bpf_prog_query_opts()** queries the BPF programs and BPF links 645 * which are attached to *target* which can represent a file descriptor or 646 * netdevice ifindex. 647 * 648 * @param target query location file descriptor or ifindex 649 * @param type attach type for the BPF program 650 * @param opts options for configuring the query 651 * @return 0, on success; negative error code, otherwise (errno is also set to 652 * the error code) 653 */ 654 LIBBPF_API int bpf_prog_query_opts(int target, enum bpf_attach_type type, 655 struct bpf_prog_query_opts *opts); 656 LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type, 657 __u32 query_flags, __u32 *attach_flags, 658 __u32 *prog_ids, __u32 *prog_cnt); 659 660 struct bpf_raw_tp_opts { 661 size_t sz; /* size of this struct for forward/backward compatibility */ 662 const char *tp_name; 663 __u64 cookie; 664 size_t :0; 665 }; 666 #define bpf_raw_tp_opts__last_field cookie 667 668 LIBBPF_API int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts); 669 LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); 670 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, 671 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type, 672 __u64 *probe_offset, __u64 *probe_addr); 673 674 #ifdef __cplusplus 675 /* forward-declaring enums in C++ isn't compatible with pure C enums, so 676 * instead define bpf_enable_stats() as accepting int as an input 677 */ 678 LIBBPF_API int bpf_enable_stats(int type); 679 #else 680 enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */ 681 LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type); 682 #endif 683 684 struct bpf_prog_bind_opts { 685 size_t sz; /* size of this struct for forward/backward compatibility */ 686 __u32 flags; 687 }; 688 #define bpf_prog_bind_opts__last_field flags 689 690 LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd, 691 const struct bpf_prog_bind_opts *opts); 692 693 struct bpf_test_run_opts { 694 size_t sz; /* size of this struct for forward/backward compatibility */ 695 const void *data_in; /* optional */ 696 void *data_out; /* optional */ 697 __u32 data_size_in; 698 __u32 data_size_out; /* in: max length of data_out 699 * out: length of data_out 700 */ 701 const void *ctx_in; /* optional */ 702 void *ctx_out; /* optional */ 703 __u32 ctx_size_in; 704 __u32 ctx_size_out; /* in: max length of ctx_out 705 * out: length of cxt_out 706 */ 707 __u32 retval; /* out: return code of the BPF program */ 708 int repeat; 709 __u32 duration; /* out: average per repetition in ns */ 710 __u32 flags; 711 __u32 cpu; 712 __u32 batch_size; 713 }; 714 #define bpf_test_run_opts__last_field batch_size 715 716 LIBBPF_API int bpf_prog_test_run_opts(int prog_fd, 717 struct bpf_test_run_opts *opts); 718 719 struct bpf_token_create_opts { 720 size_t sz; /* size of this struct for forward/backward compatibility */ 721 __u32 flags; 722 size_t :0; 723 }; 724 #define bpf_token_create_opts__last_field flags 725 726 /** 727 * @brief **bpf_token_create()** creates a new instance of BPF token derived 728 * from specified BPF FS mount point. 729 * 730 * BPF token created with this API can be passed to bpf() syscall for 731 * commands like BPF_PROG_LOAD, BPF_MAP_CREATE, etc. 732 * 733 * @param bpffs_fd FD for BPF FS instance from which to derive a BPF token 734 * instance. 735 * @param opts optional BPF token creation options, can be NULL 736 * 737 * @return BPF token FD > 0, on success; negative error code, otherwise (errno 738 * is also set to the error code) 739 */ 740 LIBBPF_API int bpf_token_create(int bpffs_fd, 741 struct bpf_token_create_opts *opts); 742 743 struct bpf_prog_stream_read_opts { 744 size_t sz; 745 size_t :0; 746 }; 747 #define bpf_prog_stream_read_opts__last_field sz 748 /** 749 * @brief **bpf_prog_stream_read** reads data from the BPF stream of a given BPF 750 * program. 751 * 752 * @param prog_fd FD for the BPF program whose BPF stream is to be read. 753 * @param stream_id ID of the BPF stream to be read. 754 * @param buf Buffer to read data into from the BPF stream. 755 * @param buf_len Maximum number of bytes to read from the BPF stream. 756 * @param opts optional options, can be NULL 757 * 758 * @return The number of bytes read, on success; negative error code, otherwise 759 * (errno is also set to the error code) 760 */ 761 LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len, 762 struct bpf_prog_stream_read_opts *opts); 763 764 struct bpf_prog_assoc_struct_ops_opts { 765 size_t sz; 766 __u32 flags; 767 size_t :0; 768 }; 769 #define bpf_prog_assoc_struct_ops_opts__last_field flags 770 771 /** 772 * @brief **bpf_prog_assoc_struct_ops** associates a BPF program with a 773 * struct_ops map. 774 * 775 * @param prog_fd FD for the BPF program 776 * @param map_fd FD for the struct_ops map to be associated with the BPF program 777 * @param opts optional options, can be NULL 778 * 779 * @return 0 on success; negative error code, otherwise (errno is also set to 780 * the error code) 781 */ 782 LIBBPF_API int bpf_prog_assoc_struct_ops(int prog_fd, int map_fd, 783 struct bpf_prog_assoc_struct_ops_opts *opts); 784 785 #ifdef __cplusplus 786 } /* extern "C" */ 787 #endif 788 789 #endif /* __LIBBPF_BPF_H */ 790