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 }; 473 size_t :0; 474 }; 475 #define bpf_link_create_opts__last_field uprobe_multi.pid 476 477 LIBBPF_API int bpf_link_create(int prog_fd, int target_fd, 478 enum bpf_attach_type attach_type, 479 const struct bpf_link_create_opts *opts); 480 481 LIBBPF_API int bpf_link_detach(int link_fd); 482 483 struct bpf_link_update_opts { 484 size_t sz; /* size of this struct for forward/backward compatibility */ 485 __u32 flags; /* extra flags */ 486 __u32 old_prog_fd; /* expected old program FD */ 487 __u32 old_map_fd; /* expected old map FD */ 488 }; 489 #define bpf_link_update_opts__last_field old_map_fd 490 491 LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd, 492 const struct bpf_link_update_opts *opts); 493 494 LIBBPF_API int bpf_iter_create(int link_fd); 495 496 struct bpf_prog_test_run_attr { 497 int prog_fd; 498 int repeat; 499 const void *data_in; 500 __u32 data_size_in; 501 void *data_out; /* optional */ 502 __u32 data_size_out; /* in: max length of data_out 503 * out: length of data_out */ 504 __u32 retval; /* out: return code of the BPF program */ 505 __u32 duration; /* out: average per repetition in ns */ 506 const void *ctx_in; /* optional */ 507 __u32 ctx_size_in; 508 void *ctx_out; /* optional */ 509 __u32 ctx_size_out; /* in: max length of ctx_out 510 * out: length of cxt_out */ 511 }; 512 513 LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id); 514 LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); 515 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id); 516 LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id); 517 518 struct bpf_get_fd_by_id_opts { 519 size_t sz; /* size of this struct for forward/backward compatibility */ 520 __u32 open_flags; /* permissions requested for the operation on fd */ 521 __u32 token_fd; 522 size_t :0; 523 }; 524 #define bpf_get_fd_by_id_opts__last_field token_fd 525 526 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id); 527 LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id, 528 const struct bpf_get_fd_by_id_opts *opts); 529 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id); 530 LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id, 531 const struct bpf_get_fd_by_id_opts *opts); 532 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id); 533 LIBBPF_API int bpf_btf_get_fd_by_id_opts(__u32 id, 534 const struct bpf_get_fd_by_id_opts *opts); 535 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id); 536 LIBBPF_API int bpf_link_get_fd_by_id_opts(__u32 id, 537 const struct bpf_get_fd_by_id_opts *opts); 538 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len); 539 540 /** 541 * @brief **bpf_prog_get_info_by_fd()** obtains information about the BPF 542 * program corresponding to *prog_fd*. 543 * 544 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 545 * actual number of bytes written to *info*. Note that *info* should be 546 * zero-initialized or initialized as expected by the requested *info* 547 * type. Failing to (zero-)initialize *info* under certain circumstances can 548 * result in this helper returning an error. 549 * 550 * @param prog_fd BPF program file descriptor 551 * @param info pointer to **struct bpf_prog_info** that will be populated with 552 * BPF program information 553 * @param info_len pointer to the size of *info*; on success updated with the 554 * number of bytes written to *info* 555 * @return 0, on success; negative error code, otherwise (errno is also set to 556 * the error code) 557 */ 558 LIBBPF_API int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len); 559 560 /** 561 * @brief **bpf_map_get_info_by_fd()** obtains information about the BPF 562 * map corresponding to *map_fd*. 563 * 564 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 565 * actual number of bytes written to *info*. Note that *info* should be 566 * zero-initialized or initialized as expected by the requested *info* 567 * type. Failing to (zero-)initialize *info* under certain circumstances can 568 * result in this helper returning an error. 569 * 570 * @param map_fd BPF map file descriptor 571 * @param info pointer to **struct bpf_map_info** that will be populated with 572 * BPF map information 573 * @param info_len pointer to the size of *info*; on success updated with the 574 * number of bytes written to *info* 575 * @return 0, on success; negative error code, otherwise (errno is also set to 576 * the error code) 577 */ 578 LIBBPF_API int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len); 579 580 /** 581 * @brief **bpf_btf_get_info_by_fd()** obtains information about the 582 * BTF object corresponding to *btf_fd*. 583 * 584 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 585 * actual number of bytes written to *info*. Note that *info* should be 586 * zero-initialized or initialized as expected by the requested *info* 587 * type. Failing to (zero-)initialize *info* under certain circumstances can 588 * result in this helper returning an error. 589 * 590 * @param btf_fd BTF object file descriptor 591 * @param info pointer to **struct bpf_btf_info** that will be populated with 592 * BTF object information 593 * @param info_len pointer to the size of *info*; on success updated with the 594 * number of bytes written to *info* 595 * @return 0, on success; negative error code, otherwise (errno is also set to 596 * the error code) 597 */ 598 LIBBPF_API int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len); 599 600 /** 601 * @brief **bpf_btf_get_info_by_fd()** obtains information about the BPF 602 * link corresponding to *link_fd*. 603 * 604 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 605 * actual number of bytes written to *info*. Note that *info* should be 606 * zero-initialized or initialized as expected by the requested *info* 607 * type. Failing to (zero-)initialize *info* under certain circumstances can 608 * result in this helper returning an error. 609 * 610 * @param link_fd BPF link file descriptor 611 * @param info pointer to **struct bpf_link_info** that will be populated with 612 * BPF link information 613 * @param info_len pointer to the size of *info*; on success updated with the 614 * number of bytes written to *info* 615 * @return 0, on success; negative error code, otherwise (errno is also set to 616 * the error code) 617 */ 618 LIBBPF_API int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len); 619 620 struct bpf_prog_query_opts { 621 size_t sz; /* size of this struct for forward/backward compatibility */ 622 __u32 query_flags; 623 __u32 attach_flags; /* output argument */ 624 __u32 *prog_ids; 625 union { 626 /* input+output argument */ 627 __u32 prog_cnt; 628 __u32 count; 629 }; 630 __u32 *prog_attach_flags; 631 __u32 *link_ids; 632 __u32 *link_attach_flags; 633 __u64 revision; 634 size_t :0; 635 }; 636 #define bpf_prog_query_opts__last_field revision 637 638 /** 639 * @brief **bpf_prog_query_opts()** queries the BPF programs and BPF links 640 * which are attached to *target* which can represent a file descriptor or 641 * netdevice ifindex. 642 * 643 * @param target query location file descriptor or ifindex 644 * @param type attach type for the BPF program 645 * @param opts options for configuring the query 646 * @return 0, on success; negative error code, otherwise (errno is also set to 647 * the error code) 648 */ 649 LIBBPF_API int bpf_prog_query_opts(int target, enum bpf_attach_type type, 650 struct bpf_prog_query_opts *opts); 651 LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type, 652 __u32 query_flags, __u32 *attach_flags, 653 __u32 *prog_ids, __u32 *prog_cnt); 654 655 struct bpf_raw_tp_opts { 656 size_t sz; /* size of this struct for forward/backward compatibility */ 657 const char *tp_name; 658 __u64 cookie; 659 size_t :0; 660 }; 661 #define bpf_raw_tp_opts__last_field cookie 662 663 LIBBPF_API int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts); 664 LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); 665 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, 666 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type, 667 __u64 *probe_offset, __u64 *probe_addr); 668 669 #ifdef __cplusplus 670 /* forward-declaring enums in C++ isn't compatible with pure C enums, so 671 * instead define bpf_enable_stats() as accepting int as an input 672 */ 673 LIBBPF_API int bpf_enable_stats(int type); 674 #else 675 enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */ 676 LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type); 677 #endif 678 679 struct bpf_prog_bind_opts { 680 size_t sz; /* size of this struct for forward/backward compatibility */ 681 __u32 flags; 682 }; 683 #define bpf_prog_bind_opts__last_field flags 684 685 LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd, 686 const struct bpf_prog_bind_opts *opts); 687 688 struct bpf_test_run_opts { 689 size_t sz; /* size of this struct for forward/backward compatibility */ 690 const void *data_in; /* optional */ 691 void *data_out; /* optional */ 692 __u32 data_size_in; 693 __u32 data_size_out; /* in: max length of data_out 694 * out: length of data_out 695 */ 696 const void *ctx_in; /* optional */ 697 void *ctx_out; /* optional */ 698 __u32 ctx_size_in; 699 __u32 ctx_size_out; /* in: max length of ctx_out 700 * out: length of cxt_out 701 */ 702 __u32 retval; /* out: return code of the BPF program */ 703 int repeat; 704 __u32 duration; /* out: average per repetition in ns */ 705 __u32 flags; 706 __u32 cpu; 707 __u32 batch_size; 708 }; 709 #define bpf_test_run_opts__last_field batch_size 710 711 LIBBPF_API int bpf_prog_test_run_opts(int prog_fd, 712 struct bpf_test_run_opts *opts); 713 714 struct bpf_token_create_opts { 715 size_t sz; /* size of this struct for forward/backward compatibility */ 716 __u32 flags; 717 size_t :0; 718 }; 719 #define bpf_token_create_opts__last_field flags 720 721 /** 722 * @brief **bpf_token_create()** creates a new instance of BPF token derived 723 * from specified BPF FS mount point. 724 * 725 * BPF token created with this API can be passed to bpf() syscall for 726 * commands like BPF_PROG_LOAD, BPF_MAP_CREATE, etc. 727 * 728 * @param bpffs_fd FD for BPF FS instance from which to derive a BPF token 729 * instance. 730 * @param opts optional BPF token creation options, can be NULL 731 * 732 * @return BPF token FD > 0, on success; negative error code, otherwise (errno 733 * is also set to the error code) 734 */ 735 LIBBPF_API int bpf_token_create(int bpffs_fd, 736 struct bpf_token_create_opts *opts); 737 738 struct bpf_prog_stream_read_opts { 739 size_t sz; 740 size_t :0; 741 }; 742 #define bpf_prog_stream_read_opts__last_field sz 743 /** 744 * @brief **bpf_prog_stream_read** reads data from the BPF stream of a given BPF 745 * program. 746 * 747 * @param prog_fd FD for the BPF program whose BPF stream is to be read. 748 * @param stream_id ID of the BPF stream to be read. 749 * @param buf Buffer to read data into from the BPF stream. 750 * @param buf_len Maximum number of bytes to read from the BPF stream. 751 * @param opts optional options, can be NULL 752 * 753 * @return The number of bytes read, on success; negative error code, otherwise 754 * (errno is also set to the error code) 755 */ 756 LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len, 757 struct bpf_prog_stream_read_opts *opts); 758 759 struct bpf_prog_assoc_struct_ops_opts { 760 size_t sz; 761 __u32 flags; 762 size_t :0; 763 }; 764 #define bpf_prog_assoc_struct_ops_opts__last_field flags 765 766 /** 767 * @brief **bpf_prog_assoc_struct_ops** associates a BPF program with a 768 * struct_ops map. 769 * 770 * @param prog_fd FD for the BPF program 771 * @param map_fd FD for the struct_ops map to be associated with the BPF program 772 * @param opts optional options, can be NULL 773 * 774 * @return 0 on success; negative error code, otherwise (errno is also set to 775 * the error code) 776 */ 777 LIBBPF_API int bpf_prog_assoc_struct_ops(int prog_fd, int map_fd, 778 struct bpf_prog_assoc_struct_ops_opts *opts); 779 780 #ifdef __cplusplus 781 } /* extern "C" */ 782 #endif 783 784 #endif /* __LIBBPF_BPF_H */ 785