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