1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 3 /* 4 * common eBPF 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 24 #include <stdlib.h> 25 #include <string.h> 26 #include <memory.h> 27 #include <unistd.h> 28 #include <asm/unistd.h> 29 #include <errno.h> 30 #include <linux/bpf.h> 31 #include <linux/filter.h> 32 #include <linux/kernel.h> 33 #include <limits.h> 34 #include <sys/resource.h> 35 #include "bpf.h" 36 #include "libbpf.h" 37 #include "libbpf_internal.h" 38 39 /* 40 * When building perf, unistd.h is overridden. __NR_bpf is 41 * required to be defined explicitly. 42 */ 43 #ifndef __NR_bpf 44 # if defined(__i386__) 45 # define __NR_bpf 357 46 # elif defined(__x86_64__) 47 # define __NR_bpf 321 48 # elif defined(__aarch64__) 49 # define __NR_bpf 280 50 # elif defined(__sparc__) 51 # define __NR_bpf 349 52 # elif defined(__s390__) 53 # define __NR_bpf 351 54 # elif defined(__arc__) 55 # define __NR_bpf 280 56 # elif defined(__mips__) && defined(_ABIO32) 57 # define __NR_bpf 4355 58 # elif defined(__mips__) && defined(_ABIN32) 59 # define __NR_bpf 6319 60 # elif defined(__mips__) && defined(_ABI64) 61 # define __NR_bpf 5315 62 # elif defined(__loongarch__) 63 # define __NR_bpf 280 64 # else 65 # error __NR_bpf not defined. libbpf does not support your arch. 66 # endif 67 #endif 68 69 static inline __u64 ptr_to_u64(const void *ptr) 70 { 71 return (__u64) (unsigned long) ptr; 72 } 73 74 static inline int sys_bpf_ext(enum bpf_cmd cmd, union bpf_attr *attr, 75 unsigned int size, 76 struct bpf_common_attr *attr_common, 77 unsigned int size_common) 78 { 79 cmd = attr_common ? (cmd | BPF_COMMON_ATTRS) : (cmd & ~BPF_COMMON_ATTRS); 80 return syscall(__NR_bpf, cmd, attr, size, attr_common, size_common); 81 } 82 83 static inline int sys_bpf_ext_fd(enum bpf_cmd cmd, union bpf_attr *attr, 84 unsigned int size, 85 struct bpf_common_attr *attr_common, 86 unsigned int size_common) 87 { 88 int fd; 89 90 fd = sys_bpf_ext(cmd, attr, size, attr_common, size_common); 91 return ensure_good_fd(fd); 92 } 93 94 int probe_sys_bpf_ext(void) 95 { 96 const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); 97 union bpf_attr attr; 98 int fd; 99 100 memset(&attr, 0, attr_sz); 101 fd = syscall(__NR_bpf, BPF_PROG_LOAD | BPF_COMMON_ATTRS, &attr, attr_sz, NULL, 102 sizeof(struct bpf_common_attr)); 103 if (fd >= 0) { 104 close(fd); 105 return -EINVAL; 106 } 107 return errno == EFAULT ? 1 : 0; 108 } 109 110 static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, 111 unsigned int size) 112 { 113 return syscall(__NR_bpf, cmd, attr, size); 114 } 115 116 static inline int sys_bpf_fd(enum bpf_cmd cmd, union bpf_attr *attr, 117 unsigned int size) 118 { 119 int fd; 120 121 fd = sys_bpf(cmd, attr, size); 122 return ensure_good_fd(fd); 123 } 124 125 int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts) 126 { 127 int fd; 128 129 do { 130 fd = sys_bpf_fd(BPF_PROG_LOAD, attr, size); 131 } while (fd < 0 && errno == EAGAIN && --attempts > 0); 132 133 return fd; 134 } 135 136 /* Probe whether kernel switched from memlock-based (RLIMIT_MEMLOCK) to 137 * memcg-based memory accounting for BPF maps and progs. This was done in [0]. 138 * We use the support for bpf_ktime_get_coarse_ns() helper, which was added in 139 * the same 5.11 Linux release ([1]), to detect memcg-based accounting for BPF. 140 * 141 * [0] https://lore.kernel.org/bpf/20201201215900.3569844-1-guro@fb.com/ 142 * [1] d05512618056 ("bpf: Add bpf_ktime_get_coarse_ns helper") 143 */ 144 int probe_memcg_account(int token_fd) 145 { 146 const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); 147 struct bpf_insn insns[] = { 148 BPF_EMIT_CALL(BPF_FUNC_ktime_get_coarse_ns), 149 BPF_EXIT_INSN(), 150 }; 151 size_t insn_cnt = ARRAY_SIZE(insns); 152 union bpf_attr attr; 153 int prog_fd; 154 155 /* attempt loading freplace trying to use custom BTF */ 156 memset(&attr, 0, attr_sz); 157 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; 158 attr.insns = ptr_to_u64(insns); 159 attr.insn_cnt = insn_cnt; 160 attr.license = ptr_to_u64("GPL"); 161 attr.prog_token_fd = token_fd; 162 if (token_fd) 163 attr.prog_flags |= BPF_F_TOKEN_FD; 164 165 prog_fd = sys_bpf_fd(BPF_PROG_LOAD, &attr, attr_sz); 166 if (prog_fd >= 0) { 167 close(prog_fd); 168 return 1; 169 } 170 return 0; 171 } 172 173 static bool memlock_bumped; 174 static rlim_t memlock_rlim = RLIM_INFINITY; 175 176 int libbpf_set_memlock_rlim(size_t memlock_bytes) 177 { 178 if (memlock_bumped) 179 return libbpf_err(-EBUSY); 180 181 memlock_rlim = memlock_bytes; 182 return 0; 183 } 184 185 int bump_rlimit_memlock(void) 186 { 187 struct rlimit rlim; 188 189 /* if kernel supports memcg-based accounting, skip bumping RLIMIT_MEMLOCK */ 190 if (memlock_bumped || feat_supported(NULL, FEAT_MEMCG_ACCOUNT)) 191 return 0; 192 193 memlock_bumped = true; 194 195 /* zero memlock_rlim disables auto-bumping RLIMIT_MEMLOCK */ 196 if (memlock_rlim == 0) 197 return 0; 198 199 rlim.rlim_cur = rlim.rlim_max = memlock_rlim; 200 if (setrlimit(RLIMIT_MEMLOCK, &rlim)) 201 return -errno; 202 203 return 0; 204 } 205 206 int bpf_map_create(enum bpf_map_type map_type, 207 const char *map_name, 208 __u32 key_size, 209 __u32 value_size, 210 __u32 max_entries, 211 const struct bpf_map_create_opts *opts) 212 { 213 const size_t attr_sz = offsetofend(union bpf_attr, excl_prog_hash_size); 214 const size_t attr_common_sz = sizeof(struct bpf_common_attr); 215 struct bpf_common_attr attr_common; 216 struct bpf_log_opts *log_opts; 217 union bpf_attr attr; 218 int fd; 219 220 bump_rlimit_memlock(); 221 222 memset(&attr, 0, attr_sz); 223 224 if (!OPTS_VALID(opts, bpf_map_create_opts)) 225 return libbpf_err(-EINVAL); 226 227 attr.map_type = map_type; 228 if (map_name && feat_supported(NULL, FEAT_PROG_NAME)) 229 libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name)); 230 attr.key_size = key_size; 231 attr.value_size = value_size; 232 attr.max_entries = max_entries; 233 234 attr.btf_fd = OPTS_GET(opts, btf_fd, 0); 235 attr.btf_key_type_id = OPTS_GET(opts, btf_key_type_id, 0); 236 attr.btf_value_type_id = OPTS_GET(opts, btf_value_type_id, 0); 237 attr.btf_vmlinux_value_type_id = OPTS_GET(opts, btf_vmlinux_value_type_id, 0); 238 attr.value_type_btf_obj_fd = OPTS_GET(opts, value_type_btf_obj_fd, 0); 239 240 attr.inner_map_fd = OPTS_GET(opts, inner_map_fd, 0); 241 attr.map_flags = OPTS_GET(opts, map_flags, 0); 242 attr.map_extra = OPTS_GET(opts, map_extra, 0); 243 attr.numa_node = OPTS_GET(opts, numa_node, 0); 244 attr.map_ifindex = OPTS_GET(opts, map_ifindex, 0); 245 246 attr.map_token_fd = OPTS_GET(opts, token_fd, 0); 247 attr.excl_prog_hash = ptr_to_u64(OPTS_GET(opts, excl_prog_hash, NULL)); 248 attr.excl_prog_hash_size = OPTS_GET(opts, excl_prog_hash_size, 0); 249 250 log_opts = OPTS_GET(opts, log_opts, NULL); 251 if (!OPTS_VALID(log_opts, bpf_log_opts)) 252 return libbpf_err(-EINVAL); 253 254 if (log_opts && feat_supported(NULL, FEAT_BPF_SYSCALL_COMMON_ATTRS)) { 255 memset(&attr_common, 0, attr_common_sz); 256 attr_common.log_buf = ptr_to_u64(OPTS_GET(log_opts, buf, NULL)); 257 attr_common.log_size = OPTS_GET(log_opts, size, 0); 258 attr_common.log_level = OPTS_GET(log_opts, level, 0); 259 fd = sys_bpf_ext_fd(BPF_MAP_CREATE, &attr, attr_sz, &attr_common, attr_common_sz); 260 OPTS_SET(log_opts, true_size, attr_common.log_true_size); 261 } else { 262 fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz); 263 OPTS_SET(log_opts, true_size, 0); 264 } 265 return libbpf_err_errno(fd); 266 } 267 268 static void * 269 alloc_zero_tailing_info(const void *orecord, __u32 cnt, 270 __u32 actual_rec_size, __u32 expected_rec_size) 271 { 272 __u64 info_len = (__u64)actual_rec_size * cnt; 273 void *info, *nrecord; 274 int i; 275 276 info = malloc(info_len); 277 if (!info) 278 return NULL; 279 280 /* zero out bytes kernel does not understand */ 281 nrecord = info; 282 for (i = 0; i < cnt; i++) { 283 memcpy(nrecord, orecord, expected_rec_size); 284 memset(nrecord + expected_rec_size, 0, 285 actual_rec_size - expected_rec_size); 286 orecord += actual_rec_size; 287 nrecord += actual_rec_size; 288 } 289 290 return info; 291 } 292 293 int bpf_prog_load(enum bpf_prog_type prog_type, 294 const char *prog_name, const char *license, 295 const struct bpf_insn *insns, size_t insn_cnt, 296 struct bpf_prog_load_opts *opts) 297 { 298 const size_t attr_sz = offsetofend(union bpf_attr, keyring_id); 299 void *finfo = NULL, *linfo = NULL; 300 const char *func_info, *line_info; 301 __u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd; 302 __u32 func_info_rec_size, line_info_rec_size; 303 int fd, attempts; 304 union bpf_attr attr; 305 char *log_buf; 306 307 bump_rlimit_memlock(); 308 309 if (!OPTS_VALID(opts, bpf_prog_load_opts)) 310 return libbpf_err(-EINVAL); 311 312 attempts = OPTS_GET(opts, attempts, 0); 313 if (attempts < 0) 314 return libbpf_err(-EINVAL); 315 if (attempts == 0) 316 attempts = PROG_LOAD_ATTEMPTS; 317 318 memset(&attr, 0, attr_sz); 319 320 attr.prog_type = prog_type; 321 attr.expected_attach_type = OPTS_GET(opts, expected_attach_type, 0); 322 323 attr.prog_btf_fd = OPTS_GET(opts, prog_btf_fd, 0); 324 attr.prog_flags = OPTS_GET(opts, prog_flags, 0); 325 attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0); 326 attr.kern_version = OPTS_GET(opts, kern_version, 0); 327 attr.prog_token_fd = OPTS_GET(opts, token_fd, 0); 328 329 if (prog_name && feat_supported(NULL, FEAT_PROG_NAME)) 330 libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name)); 331 attr.license = ptr_to_u64(license); 332 333 if (insn_cnt > UINT_MAX) 334 return libbpf_err(-E2BIG); 335 336 attr.insns = ptr_to_u64(insns); 337 attr.insn_cnt = (__u32)insn_cnt; 338 339 attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0); 340 attach_btf_obj_fd = OPTS_GET(opts, attach_btf_obj_fd, 0); 341 342 if (attach_prog_fd && attach_btf_obj_fd) 343 return libbpf_err(-EINVAL); 344 345 attr.attach_btf_id = OPTS_GET(opts, attach_btf_id, 0); 346 if (attach_prog_fd) 347 attr.attach_prog_fd = attach_prog_fd; 348 else 349 attr.attach_btf_obj_fd = attach_btf_obj_fd; 350 351 log_buf = OPTS_GET(opts, log_buf, NULL); 352 log_size = OPTS_GET(opts, log_size, 0); 353 log_level = OPTS_GET(opts, log_level, 0); 354 355 if (!!log_buf != !!log_size) 356 return libbpf_err(-EINVAL); 357 358 func_info_rec_size = OPTS_GET(opts, func_info_rec_size, 0); 359 func_info = OPTS_GET(opts, func_info, NULL); 360 attr.func_info_rec_size = func_info_rec_size; 361 attr.func_info = ptr_to_u64(func_info); 362 attr.func_info_cnt = OPTS_GET(opts, func_info_cnt, 0); 363 364 line_info_rec_size = OPTS_GET(opts, line_info_rec_size, 0); 365 line_info = OPTS_GET(opts, line_info, NULL); 366 attr.line_info_rec_size = line_info_rec_size; 367 attr.line_info = ptr_to_u64(line_info); 368 attr.line_info_cnt = OPTS_GET(opts, line_info_cnt, 0); 369 370 attr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL)); 371 attr.fd_array_cnt = OPTS_GET(opts, fd_array_cnt, 0); 372 373 if (log_level) { 374 attr.log_buf = ptr_to_u64(log_buf); 375 attr.log_size = log_size; 376 attr.log_level = log_level; 377 } 378 379 fd = sys_bpf_prog_load(&attr, attr_sz, attempts); 380 OPTS_SET(opts, log_true_size, attr.log_true_size); 381 if (fd >= 0) 382 return fd; 383 384 /* After bpf_prog_load, the kernel may modify certain attributes 385 * to give user space a hint how to deal with loading failure. 386 * Check to see whether we can make some changes and load again. 387 */ 388 while (errno == E2BIG && (!finfo || !linfo)) { 389 if (!finfo && attr.func_info_cnt && 390 attr.func_info_rec_size < func_info_rec_size) { 391 /* try with corrected func info records */ 392 finfo = alloc_zero_tailing_info(func_info, 393 attr.func_info_cnt, 394 func_info_rec_size, 395 attr.func_info_rec_size); 396 if (!finfo) { 397 errno = E2BIG; 398 goto done; 399 } 400 401 attr.func_info = ptr_to_u64(finfo); 402 attr.func_info_rec_size = func_info_rec_size; 403 } else if (!linfo && attr.line_info_cnt && 404 attr.line_info_rec_size < line_info_rec_size) { 405 linfo = alloc_zero_tailing_info(line_info, 406 attr.line_info_cnt, 407 line_info_rec_size, 408 attr.line_info_rec_size); 409 if (!linfo) { 410 errno = E2BIG; 411 goto done; 412 } 413 414 attr.line_info = ptr_to_u64(linfo); 415 attr.line_info_rec_size = line_info_rec_size; 416 } else { 417 break; 418 } 419 420 fd = sys_bpf_prog_load(&attr, attr_sz, attempts); 421 OPTS_SET(opts, log_true_size, attr.log_true_size); 422 if (fd >= 0) 423 goto done; 424 } 425 426 if (log_level == 0 && log_buf) { 427 /* log_level == 0 with non-NULL log_buf requires retrying on error 428 * with log_level == 1 and log_buf/log_buf_size set, to get details of 429 * failure 430 */ 431 attr.log_buf = ptr_to_u64(log_buf); 432 attr.log_size = log_size; 433 attr.log_level = 1; 434 435 fd = sys_bpf_prog_load(&attr, attr_sz, attempts); 436 OPTS_SET(opts, log_true_size, attr.log_true_size); 437 } 438 done: 439 /* free() doesn't affect errno, so we don't need to restore it */ 440 free(finfo); 441 free(linfo); 442 return libbpf_err_errno(fd); 443 } 444 445 int bpf_map_update_elem(int fd, const void *key, const void *value, 446 __u64 flags) 447 { 448 const size_t attr_sz = offsetofend(union bpf_attr, flags); 449 union bpf_attr attr; 450 int ret; 451 452 memset(&attr, 0, attr_sz); 453 attr.map_fd = fd; 454 attr.key = ptr_to_u64(key); 455 attr.value = ptr_to_u64(value); 456 attr.flags = flags; 457 458 ret = sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, attr_sz); 459 return libbpf_err_errno(ret); 460 } 461 462 int bpf_map_lookup_elem(int fd, const void *key, void *value) 463 { 464 const size_t attr_sz = offsetofend(union bpf_attr, flags); 465 union bpf_attr attr; 466 int ret; 467 468 memset(&attr, 0, attr_sz); 469 attr.map_fd = fd; 470 attr.key = ptr_to_u64(key); 471 attr.value = ptr_to_u64(value); 472 473 ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz); 474 return libbpf_err_errno(ret); 475 } 476 477 int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags) 478 { 479 const size_t attr_sz = offsetofend(union bpf_attr, flags); 480 union bpf_attr attr; 481 int ret; 482 483 memset(&attr, 0, attr_sz); 484 attr.map_fd = fd; 485 attr.key = ptr_to_u64(key); 486 attr.value = ptr_to_u64(value); 487 attr.flags = flags; 488 489 ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz); 490 return libbpf_err_errno(ret); 491 } 492 493 int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value) 494 { 495 const size_t attr_sz = offsetofend(union bpf_attr, flags); 496 union bpf_attr attr; 497 int ret; 498 499 memset(&attr, 0, attr_sz); 500 attr.map_fd = fd; 501 attr.key = ptr_to_u64(key); 502 attr.value = ptr_to_u64(value); 503 504 ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz); 505 return libbpf_err_errno(ret); 506 } 507 508 int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, void *value, __u64 flags) 509 { 510 const size_t attr_sz = offsetofend(union bpf_attr, flags); 511 union bpf_attr attr; 512 int ret; 513 514 memset(&attr, 0, attr_sz); 515 attr.map_fd = fd; 516 attr.key = ptr_to_u64(key); 517 attr.value = ptr_to_u64(value); 518 attr.flags = flags; 519 520 ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz); 521 return libbpf_err_errno(ret); 522 } 523 524 int bpf_map_delete_elem(int fd, const void *key) 525 { 526 const size_t attr_sz = offsetofend(union bpf_attr, flags); 527 union bpf_attr attr; 528 int ret; 529 530 memset(&attr, 0, attr_sz); 531 attr.map_fd = fd; 532 attr.key = ptr_to_u64(key); 533 534 ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz); 535 return libbpf_err_errno(ret); 536 } 537 538 int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags) 539 { 540 const size_t attr_sz = offsetofend(union bpf_attr, flags); 541 union bpf_attr attr; 542 int ret; 543 544 memset(&attr, 0, attr_sz); 545 attr.map_fd = fd; 546 attr.key = ptr_to_u64(key); 547 attr.flags = flags; 548 549 ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz); 550 return libbpf_err_errno(ret); 551 } 552 553 int bpf_map_get_next_key(int fd, const void *key, void *next_key) 554 { 555 const size_t attr_sz = offsetofend(union bpf_attr, next_key); 556 union bpf_attr attr; 557 int ret; 558 559 memset(&attr, 0, attr_sz); 560 attr.map_fd = fd; 561 attr.key = ptr_to_u64(key); 562 attr.next_key = ptr_to_u64(next_key); 563 564 ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, attr_sz); 565 return libbpf_err_errno(ret); 566 } 567 568 int bpf_map_freeze(int fd) 569 { 570 const size_t attr_sz = offsetofend(union bpf_attr, map_fd); 571 union bpf_attr attr; 572 int ret; 573 574 memset(&attr, 0, attr_sz); 575 attr.map_fd = fd; 576 577 ret = sys_bpf(BPF_MAP_FREEZE, &attr, attr_sz); 578 return libbpf_err_errno(ret); 579 } 580 581 static int bpf_map_batch_common(int cmd, int fd, void *in_batch, 582 void *out_batch, void *keys, void *values, 583 __u32 *count, 584 const struct bpf_map_batch_opts *opts) 585 { 586 const size_t attr_sz = offsetofend(union bpf_attr, batch); 587 union bpf_attr attr; 588 int ret; 589 590 if (!OPTS_VALID(opts, bpf_map_batch_opts)) 591 return libbpf_err(-EINVAL); 592 593 memset(&attr, 0, attr_sz); 594 attr.batch.map_fd = fd; 595 attr.batch.in_batch = ptr_to_u64(in_batch); 596 attr.batch.out_batch = ptr_to_u64(out_batch); 597 attr.batch.keys = ptr_to_u64(keys); 598 attr.batch.values = ptr_to_u64(values); 599 attr.batch.count = *count; 600 attr.batch.elem_flags = OPTS_GET(opts, elem_flags, 0); 601 attr.batch.flags = OPTS_GET(opts, flags, 0); 602 603 ret = sys_bpf(cmd, &attr, attr_sz); 604 *count = attr.batch.count; 605 606 return libbpf_err_errno(ret); 607 } 608 609 int bpf_map_delete_batch(int fd, const void *keys, __u32 *count, 610 const struct bpf_map_batch_opts *opts) 611 { 612 return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL, 613 NULL, (void *)keys, NULL, count, opts); 614 } 615 616 int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, void *keys, 617 void *values, __u32 *count, 618 const struct bpf_map_batch_opts *opts) 619 { 620 return bpf_map_batch_common(BPF_MAP_LOOKUP_BATCH, fd, in_batch, 621 out_batch, keys, values, count, opts); 622 } 623 624 int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch, 625 void *keys, void *values, __u32 *count, 626 const struct bpf_map_batch_opts *opts) 627 { 628 return bpf_map_batch_common(BPF_MAP_LOOKUP_AND_DELETE_BATCH, 629 fd, in_batch, out_batch, keys, values, 630 count, opts); 631 } 632 633 int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count, 634 const struct bpf_map_batch_opts *opts) 635 { 636 return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL, 637 (void *)keys, (void *)values, count, opts); 638 } 639 640 int bpf_obj_pin_opts(int fd, const char *pathname, const struct bpf_obj_pin_opts *opts) 641 { 642 const size_t attr_sz = offsetofend(union bpf_attr, path_fd); 643 union bpf_attr attr; 644 int ret; 645 646 if (!OPTS_VALID(opts, bpf_obj_pin_opts)) 647 return libbpf_err(-EINVAL); 648 649 memset(&attr, 0, attr_sz); 650 attr.path_fd = OPTS_GET(opts, path_fd, 0); 651 attr.pathname = ptr_to_u64((void *)pathname); 652 attr.file_flags = OPTS_GET(opts, file_flags, 0); 653 attr.bpf_fd = fd; 654 655 ret = sys_bpf(BPF_OBJ_PIN, &attr, attr_sz); 656 return libbpf_err_errno(ret); 657 } 658 659 int bpf_obj_pin(int fd, const char *pathname) 660 { 661 return bpf_obj_pin_opts(fd, pathname, NULL); 662 } 663 664 int bpf_obj_get(const char *pathname) 665 { 666 return bpf_obj_get_opts(pathname, NULL); 667 } 668 669 int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts) 670 { 671 const size_t attr_sz = offsetofend(union bpf_attr, path_fd); 672 union bpf_attr attr; 673 int fd; 674 675 if (!OPTS_VALID(opts, bpf_obj_get_opts)) 676 return libbpf_err(-EINVAL); 677 678 memset(&attr, 0, attr_sz); 679 attr.path_fd = OPTS_GET(opts, path_fd, 0); 680 attr.pathname = ptr_to_u64((void *)pathname); 681 attr.file_flags = OPTS_GET(opts, file_flags, 0); 682 683 fd = sys_bpf_fd(BPF_OBJ_GET, &attr, attr_sz); 684 return libbpf_err_errno(fd); 685 } 686 687 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type, 688 unsigned int flags) 689 { 690 DECLARE_LIBBPF_OPTS(bpf_prog_attach_opts, opts, 691 .flags = flags, 692 ); 693 694 return bpf_prog_attach_opts(prog_fd, target_fd, type, &opts); 695 } 696 697 int bpf_prog_attach_opts(int prog_fd, int target, enum bpf_attach_type type, 698 const struct bpf_prog_attach_opts *opts) 699 { 700 const size_t attr_sz = offsetofend(union bpf_attr, expected_revision); 701 __u32 relative_id, flags; 702 int ret, relative_fd; 703 union bpf_attr attr; 704 705 if (!OPTS_VALID(opts, bpf_prog_attach_opts)) 706 return libbpf_err(-EINVAL); 707 708 relative_id = OPTS_GET(opts, relative_id, 0); 709 relative_fd = OPTS_GET(opts, relative_fd, 0); 710 flags = OPTS_GET(opts, flags, 0); 711 712 /* validate we don't have unexpected combinations of non-zero fields */ 713 if (relative_fd && relative_id) 714 return libbpf_err(-EINVAL); 715 716 memset(&attr, 0, attr_sz); 717 attr.target_fd = target; 718 attr.attach_bpf_fd = prog_fd; 719 attr.attach_type = type; 720 attr.replace_bpf_fd = OPTS_GET(opts, replace_fd, 0); 721 attr.expected_revision = OPTS_GET(opts, expected_revision, 0); 722 723 if (relative_id) { 724 attr.attach_flags = flags | BPF_F_ID; 725 attr.relative_id = relative_id; 726 } else { 727 attr.attach_flags = flags; 728 attr.relative_fd = relative_fd; 729 } 730 731 ret = sys_bpf(BPF_PROG_ATTACH, &attr, attr_sz); 732 return libbpf_err_errno(ret); 733 } 734 735 int bpf_prog_detach_opts(int prog_fd, int target, enum bpf_attach_type type, 736 const struct bpf_prog_detach_opts *opts) 737 { 738 const size_t attr_sz = offsetofend(union bpf_attr, expected_revision); 739 __u32 relative_id, flags; 740 int ret, relative_fd; 741 union bpf_attr attr; 742 743 if (!OPTS_VALID(opts, bpf_prog_detach_opts)) 744 return libbpf_err(-EINVAL); 745 746 relative_id = OPTS_GET(opts, relative_id, 0); 747 relative_fd = OPTS_GET(opts, relative_fd, 0); 748 flags = OPTS_GET(opts, flags, 0); 749 750 /* validate we don't have unexpected combinations of non-zero fields */ 751 if (relative_fd && relative_id) 752 return libbpf_err(-EINVAL); 753 754 memset(&attr, 0, attr_sz); 755 attr.target_fd = target; 756 attr.attach_bpf_fd = prog_fd; 757 attr.attach_type = type; 758 attr.expected_revision = OPTS_GET(opts, expected_revision, 0); 759 760 if (relative_id) { 761 attr.attach_flags = flags | BPF_F_ID; 762 attr.relative_id = relative_id; 763 } else { 764 attr.attach_flags = flags; 765 attr.relative_fd = relative_fd; 766 } 767 768 ret = sys_bpf(BPF_PROG_DETACH, &attr, attr_sz); 769 return libbpf_err_errno(ret); 770 } 771 772 int bpf_prog_detach(int target_fd, enum bpf_attach_type type) 773 { 774 return bpf_prog_detach_opts(0, target_fd, type, NULL); 775 } 776 777 int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type) 778 { 779 return bpf_prog_detach_opts(prog_fd, target_fd, type, NULL); 780 } 781 782 int bpf_link_create(int prog_fd, int target_fd, 783 enum bpf_attach_type attach_type, 784 const struct bpf_link_create_opts *opts) 785 { 786 const size_t attr_sz = offsetofend(union bpf_attr, link_create); 787 __u32 target_btf_id, iter_info_len, relative_id; 788 int fd, err, relative_fd; 789 union bpf_attr attr; 790 791 if (!OPTS_VALID(opts, bpf_link_create_opts)) 792 return libbpf_err(-EINVAL); 793 794 iter_info_len = OPTS_GET(opts, iter_info_len, 0); 795 target_btf_id = OPTS_GET(opts, target_btf_id, 0); 796 797 /* validate we don't have unexpected combinations of non-zero fields */ 798 if (iter_info_len || target_btf_id) { 799 if (iter_info_len && target_btf_id) 800 return libbpf_err(-EINVAL); 801 if (!OPTS_ZEROED(opts, target_btf_id)) 802 return libbpf_err(-EINVAL); 803 } 804 805 memset(&attr, 0, attr_sz); 806 attr.link_create.prog_fd = prog_fd; 807 attr.link_create.target_fd = target_fd; 808 attr.link_create.attach_type = attach_type; 809 attr.link_create.flags = OPTS_GET(opts, flags, 0); 810 811 if (target_btf_id) { 812 attr.link_create.target_btf_id = target_btf_id; 813 goto proceed; 814 } 815 816 switch (attach_type) { 817 case BPF_TRACE_ITER: 818 attr.link_create.iter_info = ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0)); 819 attr.link_create.iter_info_len = iter_info_len; 820 break; 821 case BPF_PERF_EVENT: 822 attr.link_create.perf_event.bpf_cookie = OPTS_GET(opts, perf_event.bpf_cookie, 0); 823 if (!OPTS_ZEROED(opts, perf_event)) 824 return libbpf_err(-EINVAL); 825 break; 826 case BPF_TRACE_KPROBE_MULTI: 827 case BPF_TRACE_KPROBE_SESSION: 828 attr.link_create.kprobe_multi.flags = OPTS_GET(opts, kprobe_multi.flags, 0); 829 attr.link_create.kprobe_multi.cnt = OPTS_GET(opts, kprobe_multi.cnt, 0); 830 attr.link_create.kprobe_multi.syms = ptr_to_u64(OPTS_GET(opts, kprobe_multi.syms, 0)); 831 attr.link_create.kprobe_multi.addrs = ptr_to_u64(OPTS_GET(opts, kprobe_multi.addrs, 0)); 832 attr.link_create.kprobe_multi.cookies = ptr_to_u64(OPTS_GET(opts, kprobe_multi.cookies, 0)); 833 if (!OPTS_ZEROED(opts, kprobe_multi)) 834 return libbpf_err(-EINVAL); 835 break; 836 case BPF_TRACE_UPROBE_MULTI: 837 case BPF_TRACE_UPROBE_SESSION: 838 attr.link_create.uprobe_multi.flags = OPTS_GET(opts, uprobe_multi.flags, 0); 839 attr.link_create.uprobe_multi.cnt = OPTS_GET(opts, uprobe_multi.cnt, 0); 840 attr.link_create.uprobe_multi.path = ptr_to_u64(OPTS_GET(opts, uprobe_multi.path, 0)); 841 attr.link_create.uprobe_multi.offsets = ptr_to_u64(OPTS_GET(opts, uprobe_multi.offsets, 0)); 842 attr.link_create.uprobe_multi.ref_ctr_offsets = ptr_to_u64(OPTS_GET(opts, uprobe_multi.ref_ctr_offsets, 0)); 843 attr.link_create.uprobe_multi.cookies = ptr_to_u64(OPTS_GET(opts, uprobe_multi.cookies, 0)); 844 attr.link_create.uprobe_multi.pid = OPTS_GET(opts, uprobe_multi.pid, 0); 845 if (!OPTS_ZEROED(opts, uprobe_multi)) 846 return libbpf_err(-EINVAL); 847 break; 848 case BPF_TRACE_FENTRY_MULTI: 849 case BPF_TRACE_FEXIT_MULTI: 850 case BPF_TRACE_FSESSION_MULTI: 851 attr.link_create.tracing_multi.ids = ptr_to_u64(OPTS_GET(opts, tracing_multi.ids, 0)); 852 attr.link_create.tracing_multi.cookies = ptr_to_u64(OPTS_GET(opts, tracing_multi.cookies, 0)); 853 attr.link_create.tracing_multi.cnt = OPTS_GET(opts, tracing_multi.cnt, 0); 854 if (!OPTS_ZEROED(opts, tracing_multi)) 855 return libbpf_err(-EINVAL); 856 break; 857 case BPF_TRACE_RAW_TP: 858 case BPF_TRACE_FENTRY: 859 case BPF_TRACE_FEXIT: 860 case BPF_MODIFY_RETURN: 861 case BPF_TRACE_FSESSION: 862 case BPF_LSM_MAC: 863 attr.link_create.tracing.cookie = OPTS_GET(opts, tracing.cookie, 0); 864 if (!OPTS_ZEROED(opts, tracing)) 865 return libbpf_err(-EINVAL); 866 break; 867 case BPF_NETFILTER: 868 attr.link_create.netfilter.pf = OPTS_GET(opts, netfilter.pf, 0); 869 attr.link_create.netfilter.hooknum = OPTS_GET(opts, netfilter.hooknum, 0); 870 attr.link_create.netfilter.priority = OPTS_GET(opts, netfilter.priority, 0); 871 attr.link_create.netfilter.flags = OPTS_GET(opts, netfilter.flags, 0); 872 if (!OPTS_ZEROED(opts, netfilter)) 873 return libbpf_err(-EINVAL); 874 break; 875 case BPF_TCX_INGRESS: 876 case BPF_TCX_EGRESS: 877 relative_fd = OPTS_GET(opts, tcx.relative_fd, 0); 878 relative_id = OPTS_GET(opts, tcx.relative_id, 0); 879 if (relative_fd && relative_id) 880 return libbpf_err(-EINVAL); 881 if (relative_id) { 882 attr.link_create.tcx.relative_id = relative_id; 883 attr.link_create.flags |= BPF_F_ID; 884 } else { 885 attr.link_create.tcx.relative_fd = relative_fd; 886 } 887 attr.link_create.tcx.expected_revision = OPTS_GET(opts, tcx.expected_revision, 0); 888 if (!OPTS_ZEROED(opts, tcx)) 889 return libbpf_err(-EINVAL); 890 break; 891 case BPF_NETKIT_PRIMARY: 892 case BPF_NETKIT_PEER: 893 relative_fd = OPTS_GET(opts, netkit.relative_fd, 0); 894 relative_id = OPTS_GET(opts, netkit.relative_id, 0); 895 if (relative_fd && relative_id) 896 return libbpf_err(-EINVAL); 897 if (relative_id) { 898 attr.link_create.netkit.relative_id = relative_id; 899 attr.link_create.flags |= BPF_F_ID; 900 } else { 901 attr.link_create.netkit.relative_fd = relative_fd; 902 } 903 attr.link_create.netkit.expected_revision = OPTS_GET(opts, netkit.expected_revision, 0); 904 if (!OPTS_ZEROED(opts, netkit)) 905 return libbpf_err(-EINVAL); 906 break; 907 case BPF_CGROUP_INET_INGRESS: 908 case BPF_CGROUP_INET_EGRESS: 909 case BPF_CGROUP_INET_SOCK_CREATE: 910 case BPF_CGROUP_INET_SOCK_RELEASE: 911 case BPF_CGROUP_INET4_BIND: 912 case BPF_CGROUP_INET6_BIND: 913 case BPF_CGROUP_INET4_POST_BIND: 914 case BPF_CGROUP_INET6_POST_BIND: 915 case BPF_CGROUP_INET4_CONNECT: 916 case BPF_CGROUP_INET6_CONNECT: 917 case BPF_CGROUP_UNIX_CONNECT: 918 case BPF_CGROUP_INET4_GETPEERNAME: 919 case BPF_CGROUP_INET6_GETPEERNAME: 920 case BPF_CGROUP_UNIX_GETPEERNAME: 921 case BPF_CGROUP_INET4_GETSOCKNAME: 922 case BPF_CGROUP_INET6_GETSOCKNAME: 923 case BPF_CGROUP_UNIX_GETSOCKNAME: 924 case BPF_CGROUP_UDP4_SENDMSG: 925 case BPF_CGROUP_UDP6_SENDMSG: 926 case BPF_CGROUP_UNIX_SENDMSG: 927 case BPF_CGROUP_UDP4_RECVMSG: 928 case BPF_CGROUP_UDP6_RECVMSG: 929 case BPF_CGROUP_UNIX_RECVMSG: 930 case BPF_CGROUP_SOCK_OPS: 931 case BPF_CGROUP_DEVICE: 932 case BPF_CGROUP_SYSCTL: 933 case BPF_CGROUP_GETSOCKOPT: 934 case BPF_CGROUP_SETSOCKOPT: 935 case BPF_LSM_CGROUP: 936 relative_fd = OPTS_GET(opts, cgroup.relative_fd, 0); 937 relative_id = OPTS_GET(opts, cgroup.relative_id, 0); 938 if (relative_fd && relative_id) 939 return libbpf_err(-EINVAL); 940 if (relative_id) { 941 attr.link_create.cgroup.relative_id = relative_id; 942 attr.link_create.flags |= BPF_F_ID; 943 } else { 944 attr.link_create.cgroup.relative_fd = relative_fd; 945 } 946 attr.link_create.cgroup.expected_revision = 947 OPTS_GET(opts, cgroup.expected_revision, 0); 948 if (!OPTS_ZEROED(opts, cgroup)) 949 return libbpf_err(-EINVAL); 950 break; 951 default: 952 if (!OPTS_ZEROED(opts, flags)) 953 return libbpf_err(-EINVAL); 954 break; 955 } 956 proceed: 957 fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz); 958 if (fd >= 0) 959 return fd; 960 /* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry 961 * and other similar programs 962 */ 963 err = -errno; 964 if (err != -EINVAL) 965 return libbpf_err(err); 966 967 /* if user used features not supported by 968 * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately 969 */ 970 if (attr.link_create.target_fd || attr.link_create.target_btf_id) 971 return libbpf_err(err); 972 if (!OPTS_ZEROED(opts, sz)) 973 return libbpf_err(err); 974 975 /* otherwise, for few select kinds of programs that can be 976 * attached using BPF_RAW_TRACEPOINT_OPEN command, try that as 977 * a fallback for older kernels 978 */ 979 switch (attach_type) { 980 case BPF_TRACE_RAW_TP: 981 case BPF_LSM_MAC: 982 case BPF_TRACE_FENTRY: 983 case BPF_TRACE_FEXIT: 984 case BPF_MODIFY_RETURN: 985 return bpf_raw_tracepoint_open(NULL, prog_fd); 986 default: 987 return libbpf_err(err); 988 } 989 } 990 991 int bpf_link_detach(int link_fd) 992 { 993 const size_t attr_sz = offsetofend(union bpf_attr, link_detach); 994 union bpf_attr attr; 995 int ret; 996 997 memset(&attr, 0, attr_sz); 998 attr.link_detach.link_fd = link_fd; 999 1000 ret = sys_bpf(BPF_LINK_DETACH, &attr, attr_sz); 1001 return libbpf_err_errno(ret); 1002 } 1003 1004 int bpf_link_update(int link_fd, int new_prog_fd, 1005 const struct bpf_link_update_opts *opts) 1006 { 1007 const size_t attr_sz = offsetofend(union bpf_attr, link_update); 1008 union bpf_attr attr; 1009 int ret; 1010 1011 if (!OPTS_VALID(opts, bpf_link_update_opts)) 1012 return libbpf_err(-EINVAL); 1013 1014 if (OPTS_GET(opts, old_prog_fd, 0) && OPTS_GET(opts, old_map_fd, 0)) 1015 return libbpf_err(-EINVAL); 1016 1017 memset(&attr, 0, attr_sz); 1018 attr.link_update.link_fd = link_fd; 1019 attr.link_update.new_prog_fd = new_prog_fd; 1020 attr.link_update.flags = OPTS_GET(opts, flags, 0); 1021 if (OPTS_GET(opts, old_prog_fd, 0)) 1022 attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0); 1023 else if (OPTS_GET(opts, old_map_fd, 0)) 1024 attr.link_update.old_map_fd = OPTS_GET(opts, old_map_fd, 0); 1025 1026 ret = sys_bpf(BPF_LINK_UPDATE, &attr, attr_sz); 1027 return libbpf_err_errno(ret); 1028 } 1029 1030 int bpf_iter_create(int link_fd) 1031 { 1032 const size_t attr_sz = offsetofend(union bpf_attr, iter_create); 1033 union bpf_attr attr; 1034 int fd; 1035 1036 memset(&attr, 0, attr_sz); 1037 attr.iter_create.link_fd = link_fd; 1038 1039 fd = sys_bpf_fd(BPF_ITER_CREATE, &attr, attr_sz); 1040 return libbpf_err_errno(fd); 1041 } 1042 1043 int bpf_prog_query_opts(int target, enum bpf_attach_type type, 1044 struct bpf_prog_query_opts *opts) 1045 { 1046 const size_t attr_sz = offsetofend(union bpf_attr, query); 1047 union bpf_attr attr; 1048 int ret; 1049 1050 if (!OPTS_VALID(opts, bpf_prog_query_opts)) 1051 return libbpf_err(-EINVAL); 1052 1053 memset(&attr, 0, attr_sz); 1054 attr.query.target_fd = target; 1055 attr.query.attach_type = type; 1056 attr.query.query_flags = OPTS_GET(opts, query_flags, 0); 1057 attr.query.count = OPTS_GET(opts, count, 0); 1058 attr.query.prog_ids = ptr_to_u64(OPTS_GET(opts, prog_ids, NULL)); 1059 attr.query.link_ids = ptr_to_u64(OPTS_GET(opts, link_ids, NULL)); 1060 attr.query.prog_attach_flags = ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL)); 1061 attr.query.link_attach_flags = ptr_to_u64(OPTS_GET(opts, link_attach_flags, NULL)); 1062 1063 ret = sys_bpf(BPF_PROG_QUERY, &attr, attr_sz); 1064 1065 OPTS_SET(opts, attach_flags, attr.query.attach_flags); 1066 OPTS_SET(opts, revision, attr.query.revision); 1067 OPTS_SET(opts, count, attr.query.count); 1068 1069 return libbpf_err_errno(ret); 1070 } 1071 1072 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags, 1073 __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt) 1074 { 1075 LIBBPF_OPTS(bpf_prog_query_opts, opts); 1076 int ret; 1077 1078 opts.query_flags = query_flags; 1079 opts.prog_ids = prog_ids; 1080 opts.prog_cnt = *prog_cnt; 1081 1082 ret = bpf_prog_query_opts(target_fd, type, &opts); 1083 1084 if (attach_flags) 1085 *attach_flags = opts.attach_flags; 1086 *prog_cnt = opts.prog_cnt; 1087 1088 return libbpf_err_errno(ret); 1089 } 1090 1091 int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts) 1092 { 1093 const size_t attr_sz = offsetofend(union bpf_attr, test); 1094 union bpf_attr attr; 1095 int ret; 1096 1097 if (!OPTS_VALID(opts, bpf_test_run_opts)) 1098 return libbpf_err(-EINVAL); 1099 1100 memset(&attr, 0, attr_sz); 1101 attr.test.prog_fd = prog_fd; 1102 attr.test.batch_size = OPTS_GET(opts, batch_size, 0); 1103 attr.test.cpu = OPTS_GET(opts, cpu, 0); 1104 attr.test.flags = OPTS_GET(opts, flags, 0); 1105 attr.test.repeat = OPTS_GET(opts, repeat, 0); 1106 attr.test.duration = OPTS_GET(opts, duration, 0); 1107 attr.test.ctx_size_in = OPTS_GET(opts, ctx_size_in, 0); 1108 attr.test.ctx_size_out = OPTS_GET(opts, ctx_size_out, 0); 1109 attr.test.data_size_in = OPTS_GET(opts, data_size_in, 0); 1110 attr.test.data_size_out = OPTS_GET(opts, data_size_out, 0); 1111 attr.test.ctx_in = ptr_to_u64(OPTS_GET(opts, ctx_in, NULL)); 1112 attr.test.ctx_out = ptr_to_u64(OPTS_GET(opts, ctx_out, NULL)); 1113 attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL)); 1114 attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL)); 1115 1116 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, attr_sz); 1117 1118 OPTS_SET(opts, data_size_out, attr.test.data_size_out); 1119 OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out); 1120 OPTS_SET(opts, duration, attr.test.duration); 1121 OPTS_SET(opts, retval, attr.test.retval); 1122 1123 return libbpf_err_errno(ret); 1124 } 1125 1126 static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd) 1127 { 1128 const size_t attr_sz = offsetofend(union bpf_attr, open_flags); 1129 union bpf_attr attr; 1130 int err; 1131 1132 memset(&attr, 0, attr_sz); 1133 attr.start_id = start_id; 1134 1135 err = sys_bpf(cmd, &attr, attr_sz); 1136 if (!err) 1137 *next_id = attr.next_id; 1138 1139 return libbpf_err_errno(err); 1140 } 1141 1142 int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id) 1143 { 1144 return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID); 1145 } 1146 1147 int bpf_map_get_next_id(__u32 start_id, __u32 *next_id) 1148 { 1149 return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID); 1150 } 1151 1152 int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id) 1153 { 1154 return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID); 1155 } 1156 1157 int bpf_link_get_next_id(__u32 start_id, __u32 *next_id) 1158 { 1159 return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID); 1160 } 1161 1162 int bpf_prog_get_fd_by_id_opts(__u32 id, 1163 const struct bpf_get_fd_by_id_opts *opts) 1164 { 1165 const size_t attr_sz = offsetofend(union bpf_attr, open_flags); 1166 union bpf_attr attr; 1167 int fd; 1168 1169 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts)) 1170 return libbpf_err(-EINVAL); 1171 1172 memset(&attr, 0, attr_sz); 1173 attr.prog_id = id; 1174 attr.open_flags = OPTS_GET(opts, open_flags, 0); 1175 1176 fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, attr_sz); 1177 return libbpf_err_errno(fd); 1178 } 1179 1180 int bpf_prog_get_fd_by_id(__u32 id) 1181 { 1182 return bpf_prog_get_fd_by_id_opts(id, NULL); 1183 } 1184 1185 int bpf_map_get_fd_by_id_opts(__u32 id, 1186 const struct bpf_get_fd_by_id_opts *opts) 1187 { 1188 const size_t attr_sz = offsetofend(union bpf_attr, open_flags); 1189 union bpf_attr attr; 1190 int fd; 1191 1192 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts)) 1193 return libbpf_err(-EINVAL); 1194 1195 memset(&attr, 0, attr_sz); 1196 attr.map_id = id; 1197 attr.open_flags = OPTS_GET(opts, open_flags, 0); 1198 1199 fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, attr_sz); 1200 return libbpf_err_errno(fd); 1201 } 1202 1203 int bpf_map_get_fd_by_id(__u32 id) 1204 { 1205 return bpf_map_get_fd_by_id_opts(id, NULL); 1206 } 1207 1208 int bpf_btf_get_fd_by_id_opts(__u32 id, 1209 const struct bpf_get_fd_by_id_opts *opts) 1210 { 1211 const size_t attr_sz = offsetofend(union bpf_attr, fd_by_id_token_fd); 1212 union bpf_attr attr; 1213 int fd; 1214 1215 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts)) 1216 return libbpf_err(-EINVAL); 1217 1218 memset(&attr, 0, attr_sz); 1219 attr.btf_id = id; 1220 attr.open_flags = OPTS_GET(opts, open_flags, 0); 1221 attr.fd_by_id_token_fd = OPTS_GET(opts, token_fd, 0); 1222 1223 fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz); 1224 return libbpf_err_errno(fd); 1225 } 1226 1227 int bpf_btf_get_fd_by_id(__u32 id) 1228 { 1229 return bpf_btf_get_fd_by_id_opts(id, NULL); 1230 } 1231 1232 int bpf_link_get_fd_by_id_opts(__u32 id, 1233 const struct bpf_get_fd_by_id_opts *opts) 1234 { 1235 const size_t attr_sz = offsetofend(union bpf_attr, open_flags); 1236 union bpf_attr attr; 1237 int fd; 1238 1239 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts)) 1240 return libbpf_err(-EINVAL); 1241 1242 memset(&attr, 0, attr_sz); 1243 attr.link_id = id; 1244 attr.open_flags = OPTS_GET(opts, open_flags, 0); 1245 1246 fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, attr_sz); 1247 return libbpf_err_errno(fd); 1248 } 1249 1250 int bpf_link_get_fd_by_id(__u32 id) 1251 { 1252 return bpf_link_get_fd_by_id_opts(id, NULL); 1253 } 1254 1255 int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len) 1256 { 1257 const size_t attr_sz = offsetofend(union bpf_attr, info); 1258 union bpf_attr attr; 1259 int err; 1260 1261 memset(&attr, 0, attr_sz); 1262 attr.info.bpf_fd = bpf_fd; 1263 attr.info.info_len = *info_len; 1264 attr.info.info = ptr_to_u64(info); 1265 1266 err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, attr_sz); 1267 if (!err) 1268 *info_len = attr.info.info_len; 1269 return libbpf_err_errno(err); 1270 } 1271 1272 int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len) 1273 { 1274 return bpf_obj_get_info_by_fd(prog_fd, info, info_len); 1275 } 1276 1277 int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len) 1278 { 1279 return bpf_obj_get_info_by_fd(map_fd, info, info_len); 1280 } 1281 1282 int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len) 1283 { 1284 return bpf_obj_get_info_by_fd(btf_fd, info, info_len); 1285 } 1286 1287 int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len) 1288 { 1289 return bpf_obj_get_info_by_fd(link_fd, info, info_len); 1290 } 1291 1292 int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts) 1293 { 1294 const size_t attr_sz = offsetofend(union bpf_attr, raw_tracepoint); 1295 union bpf_attr attr; 1296 int fd; 1297 1298 if (!OPTS_VALID(opts, bpf_raw_tp_opts)) 1299 return libbpf_err(-EINVAL); 1300 1301 memset(&attr, 0, attr_sz); 1302 attr.raw_tracepoint.prog_fd = prog_fd; 1303 attr.raw_tracepoint.name = ptr_to_u64(OPTS_GET(opts, tp_name, NULL)); 1304 attr.raw_tracepoint.cookie = OPTS_GET(opts, cookie, 0); 1305 1306 fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz); 1307 return libbpf_err_errno(fd); 1308 } 1309 1310 int bpf_raw_tracepoint_open(const char *name, int prog_fd) 1311 { 1312 LIBBPF_OPTS(bpf_raw_tp_opts, opts, .tp_name = name); 1313 1314 return bpf_raw_tracepoint_open_opts(prog_fd, &opts); 1315 } 1316 1317 int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts *opts) 1318 { 1319 const size_t attr_sz = offsetofend(union bpf_attr, btf_token_fd); 1320 union bpf_attr attr; 1321 char *log_buf; 1322 size_t log_size; 1323 __u32 log_level; 1324 int fd; 1325 1326 bump_rlimit_memlock(); 1327 1328 memset(&attr, 0, attr_sz); 1329 1330 if (!OPTS_VALID(opts, bpf_btf_load_opts)) 1331 return libbpf_err(-EINVAL); 1332 1333 log_buf = OPTS_GET(opts, log_buf, NULL); 1334 log_size = OPTS_GET(opts, log_size, 0); 1335 log_level = OPTS_GET(opts, log_level, 0); 1336 1337 if (log_size > UINT_MAX) 1338 return libbpf_err(-EINVAL); 1339 if (log_size && !log_buf) 1340 return libbpf_err(-EINVAL); 1341 1342 attr.btf = ptr_to_u64(btf_data); 1343 attr.btf_size = btf_size; 1344 1345 attr.btf_flags = OPTS_GET(opts, btf_flags, 0); 1346 attr.btf_token_fd = OPTS_GET(opts, token_fd, 0); 1347 1348 /* log_level == 0 and log_buf != NULL means "try loading without 1349 * log_buf, but retry with log_buf and log_level=1 on error", which is 1350 * consistent across low-level and high-level BTF and program loading 1351 * APIs within libbpf and provides a sensible behavior in practice 1352 */ 1353 if (log_level) { 1354 attr.btf_log_buf = ptr_to_u64(log_buf); 1355 attr.btf_log_size = (__u32)log_size; 1356 attr.btf_log_level = log_level; 1357 } 1358 1359 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz); 1360 if (fd < 0 && log_buf && log_level == 0) { 1361 attr.btf_log_buf = ptr_to_u64(log_buf); 1362 attr.btf_log_size = (__u32)log_size; 1363 attr.btf_log_level = 1; 1364 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz); 1365 } 1366 1367 OPTS_SET(opts, log_true_size, attr.btf_log_true_size); 1368 return libbpf_err_errno(fd); 1369 } 1370 1371 int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len, 1372 __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset, 1373 __u64 *probe_addr) 1374 { 1375 const size_t attr_sz = offsetofend(union bpf_attr, task_fd_query); 1376 union bpf_attr attr; 1377 int err; 1378 1379 memset(&attr, 0, attr_sz); 1380 attr.task_fd_query.pid = pid; 1381 attr.task_fd_query.fd = fd; 1382 attr.task_fd_query.flags = flags; 1383 attr.task_fd_query.buf = ptr_to_u64(buf); 1384 attr.task_fd_query.buf_len = *buf_len; 1385 1386 err = sys_bpf(BPF_TASK_FD_QUERY, &attr, attr_sz); 1387 1388 *buf_len = attr.task_fd_query.buf_len; 1389 *prog_id = attr.task_fd_query.prog_id; 1390 *fd_type = attr.task_fd_query.fd_type; 1391 *probe_offset = attr.task_fd_query.probe_offset; 1392 *probe_addr = attr.task_fd_query.probe_addr; 1393 1394 return libbpf_err_errno(err); 1395 } 1396 1397 int bpf_enable_stats(enum bpf_stats_type type) 1398 { 1399 const size_t attr_sz = offsetofend(union bpf_attr, enable_stats); 1400 union bpf_attr attr; 1401 int fd; 1402 1403 memset(&attr, 0, attr_sz); 1404 attr.enable_stats.type = type; 1405 1406 fd = sys_bpf_fd(BPF_ENABLE_STATS, &attr, attr_sz); 1407 return libbpf_err_errno(fd); 1408 } 1409 1410 int bpf_prog_bind_map(int prog_fd, int map_fd, 1411 const struct bpf_prog_bind_opts *opts) 1412 { 1413 const size_t attr_sz = offsetofend(union bpf_attr, prog_bind_map); 1414 union bpf_attr attr; 1415 int ret; 1416 1417 if (!OPTS_VALID(opts, bpf_prog_bind_opts)) 1418 return libbpf_err(-EINVAL); 1419 1420 memset(&attr, 0, attr_sz); 1421 attr.prog_bind_map.prog_fd = prog_fd; 1422 attr.prog_bind_map.map_fd = map_fd; 1423 attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0); 1424 1425 ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz); 1426 return libbpf_err_errno(ret); 1427 } 1428 1429 int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts) 1430 { 1431 const size_t attr_sz = offsetofend(union bpf_attr, token_create); 1432 union bpf_attr attr; 1433 int fd; 1434 1435 if (!OPTS_VALID(opts, bpf_token_create_opts)) 1436 return libbpf_err(-EINVAL); 1437 1438 memset(&attr, 0, attr_sz); 1439 attr.token_create.bpffs_fd = bpffs_fd; 1440 attr.token_create.flags = OPTS_GET(opts, flags, 0); 1441 1442 fd = sys_bpf_fd(BPF_TOKEN_CREATE, &attr, attr_sz); 1443 return libbpf_err_errno(fd); 1444 } 1445 1446 int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len, 1447 struct bpf_prog_stream_read_opts *opts) 1448 { 1449 const size_t attr_sz = offsetofend(union bpf_attr, prog_stream_read); 1450 union bpf_attr attr; 1451 int err; 1452 1453 if (!OPTS_VALID(opts, bpf_prog_stream_read_opts)) 1454 return libbpf_err(-EINVAL); 1455 1456 memset(&attr, 0, attr_sz); 1457 attr.prog_stream_read.stream_buf = ptr_to_u64(buf); 1458 attr.prog_stream_read.stream_buf_len = buf_len; 1459 attr.prog_stream_read.stream_id = stream_id; 1460 attr.prog_stream_read.prog_fd = prog_fd; 1461 1462 err = sys_bpf(BPF_PROG_STREAM_READ_BY_FD, &attr, attr_sz); 1463 return libbpf_err_errno(err); 1464 } 1465 1466 int bpf_prog_assoc_struct_ops(int prog_fd, int map_fd, 1467 struct bpf_prog_assoc_struct_ops_opts *opts) 1468 { 1469 const size_t attr_sz = offsetofend(union bpf_attr, prog_assoc_struct_ops); 1470 union bpf_attr attr; 1471 int err; 1472 1473 if (!OPTS_VALID(opts, bpf_prog_assoc_struct_ops_opts)) 1474 return libbpf_err(-EINVAL); 1475 1476 memset(&attr, 0, attr_sz); 1477 attr.prog_assoc_struct_ops.map_fd = map_fd; 1478 attr.prog_assoc_struct_ops.prog_fd = prog_fd; 1479 attr.prog_assoc_struct_ops.flags = OPTS_GET(opts, flags, 0); 1480 1481 err = sys_bpf(BPF_PROG_ASSOC_STRUCT_OPS, &attr, attr_sz); 1482 return libbpf_err_errno(err); 1483 } 1484