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 attr.link_create.uprobe_multi.path_fd = OPTS_GET(opts, uprobe_multi.path_fd, 0); 846 if (!OPTS_ZEROED(opts, uprobe_multi)) 847 return libbpf_err(-EINVAL); 848 break; 849 case BPF_TRACE_FENTRY_MULTI: 850 case BPF_TRACE_FEXIT_MULTI: 851 case BPF_TRACE_FSESSION_MULTI: 852 attr.link_create.tracing_multi.ids = ptr_to_u64(OPTS_GET(opts, tracing_multi.ids, 0)); 853 attr.link_create.tracing_multi.cookies = ptr_to_u64(OPTS_GET(opts, tracing_multi.cookies, 0)); 854 attr.link_create.tracing_multi.cnt = OPTS_GET(opts, tracing_multi.cnt, 0); 855 if (!OPTS_ZEROED(opts, tracing_multi)) 856 return libbpf_err(-EINVAL); 857 break; 858 case BPF_TRACE_RAW_TP: 859 case BPF_TRACE_FENTRY: 860 case BPF_TRACE_FEXIT: 861 case BPF_MODIFY_RETURN: 862 case BPF_TRACE_FSESSION: 863 case BPF_LSM_MAC: 864 attr.link_create.tracing.cookie = OPTS_GET(opts, tracing.cookie, 0); 865 if (!OPTS_ZEROED(opts, tracing)) 866 return libbpf_err(-EINVAL); 867 break; 868 case BPF_NETFILTER: 869 attr.link_create.netfilter.pf = OPTS_GET(opts, netfilter.pf, 0); 870 attr.link_create.netfilter.hooknum = OPTS_GET(opts, netfilter.hooknum, 0); 871 attr.link_create.netfilter.priority = OPTS_GET(opts, netfilter.priority, 0); 872 attr.link_create.netfilter.flags = OPTS_GET(opts, netfilter.flags, 0); 873 if (!OPTS_ZEROED(opts, netfilter)) 874 return libbpf_err(-EINVAL); 875 break; 876 case BPF_TCX_INGRESS: 877 case BPF_TCX_EGRESS: 878 relative_fd = OPTS_GET(opts, tcx.relative_fd, 0); 879 relative_id = OPTS_GET(opts, tcx.relative_id, 0); 880 if (relative_fd && relative_id) 881 return libbpf_err(-EINVAL); 882 if (relative_id) { 883 attr.link_create.tcx.relative_id = relative_id; 884 attr.link_create.flags |= BPF_F_ID; 885 } else { 886 attr.link_create.tcx.relative_fd = relative_fd; 887 } 888 attr.link_create.tcx.expected_revision = OPTS_GET(opts, tcx.expected_revision, 0); 889 if (!OPTS_ZEROED(opts, tcx)) 890 return libbpf_err(-EINVAL); 891 break; 892 case BPF_NETKIT_PRIMARY: 893 case BPF_NETKIT_PEER: 894 relative_fd = OPTS_GET(opts, netkit.relative_fd, 0); 895 relative_id = OPTS_GET(opts, netkit.relative_id, 0); 896 if (relative_fd && relative_id) 897 return libbpf_err(-EINVAL); 898 if (relative_id) { 899 attr.link_create.netkit.relative_id = relative_id; 900 attr.link_create.flags |= BPF_F_ID; 901 } else { 902 attr.link_create.netkit.relative_fd = relative_fd; 903 } 904 attr.link_create.netkit.expected_revision = OPTS_GET(opts, netkit.expected_revision, 0); 905 if (!OPTS_ZEROED(opts, netkit)) 906 return libbpf_err(-EINVAL); 907 break; 908 case BPF_CGROUP_INET_INGRESS: 909 case BPF_CGROUP_INET_EGRESS: 910 case BPF_CGROUP_INET_SOCK_CREATE: 911 case BPF_CGROUP_INET_SOCK_RELEASE: 912 case BPF_CGROUP_INET4_BIND: 913 case BPF_CGROUP_INET6_BIND: 914 case BPF_CGROUP_INET4_POST_BIND: 915 case BPF_CGROUP_INET6_POST_BIND: 916 case BPF_CGROUP_INET4_CONNECT: 917 case BPF_CGROUP_INET6_CONNECT: 918 case BPF_CGROUP_UNIX_CONNECT: 919 case BPF_CGROUP_INET4_GETPEERNAME: 920 case BPF_CGROUP_INET6_GETPEERNAME: 921 case BPF_CGROUP_UNIX_GETPEERNAME: 922 case BPF_CGROUP_INET4_GETSOCKNAME: 923 case BPF_CGROUP_INET6_GETSOCKNAME: 924 case BPF_CGROUP_UNIX_GETSOCKNAME: 925 case BPF_CGROUP_UDP4_SENDMSG: 926 case BPF_CGROUP_UDP6_SENDMSG: 927 case BPF_CGROUP_UNIX_SENDMSG: 928 case BPF_CGROUP_UDP4_RECVMSG: 929 case BPF_CGROUP_UDP6_RECVMSG: 930 case BPF_CGROUP_UNIX_RECVMSG: 931 case BPF_CGROUP_SOCK_OPS: 932 case BPF_CGROUP_DEVICE: 933 case BPF_CGROUP_SYSCTL: 934 case BPF_CGROUP_GETSOCKOPT: 935 case BPF_CGROUP_SETSOCKOPT: 936 case BPF_LSM_CGROUP: 937 case BPF_STRUCT_OPS: 938 relative_fd = OPTS_GET(opts, cgroup.relative_fd, 0); 939 relative_id = OPTS_GET(opts, cgroup.relative_id, 0); 940 if (relative_fd && relative_id) 941 return libbpf_err(-EINVAL); 942 if (relative_id) { 943 attr.link_create.cgroup.relative_id = relative_id; 944 attr.link_create.flags |= BPF_F_ID; 945 } else { 946 attr.link_create.cgroup.relative_fd = relative_fd; 947 } 948 attr.link_create.cgroup.expected_revision = 949 OPTS_GET(opts, cgroup.expected_revision, 0); 950 if (!OPTS_ZEROED(opts, cgroup)) 951 return libbpf_err(-EINVAL); 952 break; 953 default: 954 if (!OPTS_ZEROED(opts, flags)) 955 return libbpf_err(-EINVAL); 956 break; 957 } 958 proceed: 959 fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz); 960 if (fd >= 0) 961 return fd; 962 /* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry 963 * and other similar programs 964 */ 965 err = -errno; 966 if (err != -EINVAL) 967 return libbpf_err(err); 968 969 /* if user used features not supported by 970 * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately 971 */ 972 if (attr.link_create.target_fd || attr.link_create.target_btf_id) 973 return libbpf_err(err); 974 if (!OPTS_ZEROED(opts, sz)) 975 return libbpf_err(err); 976 977 /* otherwise, for few select kinds of programs that can be 978 * attached using BPF_RAW_TRACEPOINT_OPEN command, try that as 979 * a fallback for older kernels 980 */ 981 switch (attach_type) { 982 case BPF_TRACE_RAW_TP: 983 case BPF_LSM_MAC: 984 case BPF_TRACE_FENTRY: 985 case BPF_TRACE_FEXIT: 986 case BPF_MODIFY_RETURN: 987 return bpf_raw_tracepoint_open(NULL, prog_fd); 988 default: 989 return libbpf_err(err); 990 } 991 } 992 993 int bpf_link_detach(int link_fd) 994 { 995 const size_t attr_sz = offsetofend(union bpf_attr, link_detach); 996 union bpf_attr attr; 997 int ret; 998 999 memset(&attr, 0, attr_sz); 1000 attr.link_detach.link_fd = link_fd; 1001 1002 ret = sys_bpf(BPF_LINK_DETACH, &attr, attr_sz); 1003 return libbpf_err_errno(ret); 1004 } 1005 1006 int bpf_link_update(int link_fd, int new_prog_fd, 1007 const struct bpf_link_update_opts *opts) 1008 { 1009 const size_t attr_sz = offsetofend(union bpf_attr, link_update); 1010 union bpf_attr attr; 1011 int ret; 1012 1013 if (!OPTS_VALID(opts, bpf_link_update_opts)) 1014 return libbpf_err(-EINVAL); 1015 1016 if (OPTS_GET(opts, old_prog_fd, 0) && OPTS_GET(opts, old_map_fd, 0)) 1017 return libbpf_err(-EINVAL); 1018 1019 memset(&attr, 0, attr_sz); 1020 attr.link_update.link_fd = link_fd; 1021 attr.link_update.new_prog_fd = new_prog_fd; 1022 attr.link_update.flags = OPTS_GET(opts, flags, 0); 1023 if (OPTS_GET(opts, old_prog_fd, 0)) 1024 attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0); 1025 else if (OPTS_GET(opts, old_map_fd, 0)) 1026 attr.link_update.old_map_fd = OPTS_GET(opts, old_map_fd, 0); 1027 1028 ret = sys_bpf(BPF_LINK_UPDATE, &attr, attr_sz); 1029 return libbpf_err_errno(ret); 1030 } 1031 1032 int bpf_iter_create(int link_fd) 1033 { 1034 const size_t attr_sz = offsetofend(union bpf_attr, iter_create); 1035 union bpf_attr attr; 1036 int fd; 1037 1038 memset(&attr, 0, attr_sz); 1039 attr.iter_create.link_fd = link_fd; 1040 1041 fd = sys_bpf_fd(BPF_ITER_CREATE, &attr, attr_sz); 1042 return libbpf_err_errno(fd); 1043 } 1044 1045 int bpf_prog_query_opts(int target, enum bpf_attach_type type, 1046 struct bpf_prog_query_opts *opts) 1047 { 1048 const size_t attr_sz = offsetofend(union bpf_attr, query); 1049 union bpf_attr attr; 1050 int ret; 1051 1052 if (!OPTS_VALID(opts, bpf_prog_query_opts)) 1053 return libbpf_err(-EINVAL); 1054 1055 memset(&attr, 0, attr_sz); 1056 attr.query.target_fd = target; 1057 attr.query.attach_type = type; 1058 attr.query.query_flags = OPTS_GET(opts, query_flags, 0); 1059 attr.query.count = OPTS_GET(opts, count, 0); 1060 attr.query.type_id = OPTS_GET(opts, type_id, 0); 1061 attr.query.prog_ids = ptr_to_u64(OPTS_GET(opts, prog_ids, NULL)); 1062 attr.query.link_ids = ptr_to_u64(OPTS_GET(opts, link_ids, NULL)); 1063 attr.query.prog_attach_flags = ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL)); 1064 attr.query.link_attach_flags = ptr_to_u64(OPTS_GET(opts, link_attach_flags, NULL)); 1065 1066 ret = sys_bpf(BPF_PROG_QUERY, &attr, attr_sz); 1067 1068 OPTS_SET(opts, attach_flags, attr.query.attach_flags); 1069 OPTS_SET(opts, revision, attr.query.revision); 1070 OPTS_SET(opts, count, attr.query.count); 1071 1072 return libbpf_err_errno(ret); 1073 } 1074 1075 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags, 1076 __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt) 1077 { 1078 LIBBPF_OPTS(bpf_prog_query_opts, opts); 1079 int ret; 1080 1081 opts.query_flags = query_flags; 1082 opts.prog_ids = prog_ids; 1083 opts.prog_cnt = *prog_cnt; 1084 1085 ret = bpf_prog_query_opts(target_fd, type, &opts); 1086 1087 if (attach_flags) 1088 *attach_flags = opts.attach_flags; 1089 *prog_cnt = opts.prog_cnt; 1090 1091 return libbpf_err_errno(ret); 1092 } 1093 1094 int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts) 1095 { 1096 const size_t attr_sz = offsetofend(union bpf_attr, test); 1097 union bpf_attr attr; 1098 int ret; 1099 1100 if (!OPTS_VALID(opts, bpf_test_run_opts)) 1101 return libbpf_err(-EINVAL); 1102 1103 memset(&attr, 0, attr_sz); 1104 attr.test.prog_fd = prog_fd; 1105 attr.test.batch_size = OPTS_GET(opts, batch_size, 0); 1106 attr.test.cpu = OPTS_GET(opts, cpu, 0); 1107 attr.test.flags = OPTS_GET(opts, flags, 0); 1108 attr.test.repeat = OPTS_GET(opts, repeat, 0); 1109 attr.test.duration = OPTS_GET(opts, duration, 0); 1110 attr.test.ctx_size_in = OPTS_GET(opts, ctx_size_in, 0); 1111 attr.test.ctx_size_out = OPTS_GET(opts, ctx_size_out, 0); 1112 attr.test.data_size_in = OPTS_GET(opts, data_size_in, 0); 1113 attr.test.data_size_out = OPTS_GET(opts, data_size_out, 0); 1114 attr.test.ctx_in = ptr_to_u64(OPTS_GET(opts, ctx_in, NULL)); 1115 attr.test.ctx_out = ptr_to_u64(OPTS_GET(opts, ctx_out, NULL)); 1116 attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL)); 1117 attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL)); 1118 1119 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, attr_sz); 1120 1121 OPTS_SET(opts, data_size_out, attr.test.data_size_out); 1122 OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out); 1123 OPTS_SET(opts, duration, attr.test.duration); 1124 OPTS_SET(opts, retval, attr.test.retval); 1125 1126 return libbpf_err_errno(ret); 1127 } 1128 1129 static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd) 1130 { 1131 const size_t attr_sz = offsetofend(union bpf_attr, open_flags); 1132 union bpf_attr attr; 1133 int err; 1134 1135 memset(&attr, 0, attr_sz); 1136 attr.start_id = start_id; 1137 1138 err = sys_bpf(cmd, &attr, attr_sz); 1139 if (!err) 1140 *next_id = attr.next_id; 1141 1142 return libbpf_err_errno(err); 1143 } 1144 1145 int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id) 1146 { 1147 return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID); 1148 } 1149 1150 int bpf_map_get_next_id(__u32 start_id, __u32 *next_id) 1151 { 1152 return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID); 1153 } 1154 1155 int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id) 1156 { 1157 return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID); 1158 } 1159 1160 int bpf_link_get_next_id(__u32 start_id, __u32 *next_id) 1161 { 1162 return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID); 1163 } 1164 1165 int bpf_prog_get_fd_by_id_opts(__u32 id, 1166 const struct bpf_get_fd_by_id_opts *opts) 1167 { 1168 const size_t attr_sz = offsetofend(union bpf_attr, open_flags); 1169 union bpf_attr attr; 1170 int fd; 1171 1172 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts)) 1173 return libbpf_err(-EINVAL); 1174 1175 memset(&attr, 0, attr_sz); 1176 attr.prog_id = id; 1177 attr.open_flags = OPTS_GET(opts, open_flags, 0); 1178 1179 fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, attr_sz); 1180 return libbpf_err_errno(fd); 1181 } 1182 1183 int bpf_prog_get_fd_by_id(__u32 id) 1184 { 1185 return bpf_prog_get_fd_by_id_opts(id, NULL); 1186 } 1187 1188 int bpf_map_get_fd_by_id_opts(__u32 id, 1189 const struct bpf_get_fd_by_id_opts *opts) 1190 { 1191 const size_t attr_sz = offsetofend(union bpf_attr, open_flags); 1192 union bpf_attr attr; 1193 int fd; 1194 1195 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts)) 1196 return libbpf_err(-EINVAL); 1197 1198 memset(&attr, 0, attr_sz); 1199 attr.map_id = id; 1200 attr.open_flags = OPTS_GET(opts, open_flags, 0); 1201 1202 fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, attr_sz); 1203 return libbpf_err_errno(fd); 1204 } 1205 1206 int bpf_map_get_fd_by_id(__u32 id) 1207 { 1208 return bpf_map_get_fd_by_id_opts(id, NULL); 1209 } 1210 1211 int bpf_btf_get_fd_by_id_opts(__u32 id, 1212 const struct bpf_get_fd_by_id_opts *opts) 1213 { 1214 const size_t attr_sz = offsetofend(union bpf_attr, fd_by_id_token_fd); 1215 union bpf_attr attr; 1216 int fd; 1217 1218 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts)) 1219 return libbpf_err(-EINVAL); 1220 1221 memset(&attr, 0, attr_sz); 1222 attr.btf_id = id; 1223 attr.open_flags = OPTS_GET(opts, open_flags, 0); 1224 attr.fd_by_id_token_fd = OPTS_GET(opts, token_fd, 0); 1225 1226 fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz); 1227 return libbpf_err_errno(fd); 1228 } 1229 1230 int bpf_btf_get_fd_by_id(__u32 id) 1231 { 1232 return bpf_btf_get_fd_by_id_opts(id, NULL); 1233 } 1234 1235 int bpf_link_get_fd_by_id_opts(__u32 id, 1236 const struct bpf_get_fd_by_id_opts *opts) 1237 { 1238 const size_t attr_sz = offsetofend(union bpf_attr, open_flags); 1239 union bpf_attr attr; 1240 int fd; 1241 1242 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts)) 1243 return libbpf_err(-EINVAL); 1244 1245 memset(&attr, 0, attr_sz); 1246 attr.link_id = id; 1247 attr.open_flags = OPTS_GET(opts, open_flags, 0); 1248 1249 fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, attr_sz); 1250 return libbpf_err_errno(fd); 1251 } 1252 1253 int bpf_link_get_fd_by_id(__u32 id) 1254 { 1255 return bpf_link_get_fd_by_id_opts(id, NULL); 1256 } 1257 1258 int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len) 1259 { 1260 const size_t attr_sz = offsetofend(union bpf_attr, info); 1261 union bpf_attr attr; 1262 int err; 1263 1264 memset(&attr, 0, attr_sz); 1265 attr.info.bpf_fd = bpf_fd; 1266 attr.info.info_len = *info_len; 1267 attr.info.info = ptr_to_u64(info); 1268 1269 err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, attr_sz); 1270 if (!err) 1271 *info_len = attr.info.info_len; 1272 return libbpf_err_errno(err); 1273 } 1274 1275 int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len) 1276 { 1277 return bpf_obj_get_info_by_fd(prog_fd, info, info_len); 1278 } 1279 1280 int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len) 1281 { 1282 return bpf_obj_get_info_by_fd(map_fd, info, info_len); 1283 } 1284 1285 int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len) 1286 { 1287 return bpf_obj_get_info_by_fd(btf_fd, info, info_len); 1288 } 1289 1290 int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len) 1291 { 1292 return bpf_obj_get_info_by_fd(link_fd, info, info_len); 1293 } 1294 1295 int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts) 1296 { 1297 const size_t attr_sz = offsetofend(union bpf_attr, raw_tracepoint); 1298 union bpf_attr attr; 1299 int fd; 1300 1301 if (!OPTS_VALID(opts, bpf_raw_tp_opts)) 1302 return libbpf_err(-EINVAL); 1303 1304 memset(&attr, 0, attr_sz); 1305 attr.raw_tracepoint.prog_fd = prog_fd; 1306 attr.raw_tracepoint.name = ptr_to_u64(OPTS_GET(opts, tp_name, NULL)); 1307 attr.raw_tracepoint.cookie = OPTS_GET(opts, cookie, 0); 1308 1309 fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz); 1310 return libbpf_err_errno(fd); 1311 } 1312 1313 int bpf_raw_tracepoint_open(const char *name, int prog_fd) 1314 { 1315 LIBBPF_OPTS(bpf_raw_tp_opts, opts, .tp_name = name); 1316 1317 return bpf_raw_tracepoint_open_opts(prog_fd, &opts); 1318 } 1319 1320 int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts *opts) 1321 { 1322 const size_t attr_sz = offsetofend(union bpf_attr, btf_token_fd); 1323 union bpf_attr attr; 1324 char *log_buf; 1325 size_t log_size; 1326 __u32 log_level; 1327 int fd; 1328 1329 bump_rlimit_memlock(); 1330 1331 memset(&attr, 0, attr_sz); 1332 1333 if (!OPTS_VALID(opts, bpf_btf_load_opts)) 1334 return libbpf_err(-EINVAL); 1335 1336 log_buf = OPTS_GET(opts, log_buf, NULL); 1337 log_size = OPTS_GET(opts, log_size, 0); 1338 log_level = OPTS_GET(opts, log_level, 0); 1339 1340 if (log_size > UINT_MAX) 1341 return libbpf_err(-EINVAL); 1342 if (log_size && !log_buf) 1343 return libbpf_err(-EINVAL); 1344 1345 attr.btf = ptr_to_u64(btf_data); 1346 attr.btf_size = btf_size; 1347 1348 attr.btf_flags = OPTS_GET(opts, btf_flags, 0); 1349 attr.btf_token_fd = OPTS_GET(opts, token_fd, 0); 1350 1351 /* log_level == 0 and log_buf != NULL means "try loading without 1352 * log_buf, but retry with log_buf and log_level=1 on error", which is 1353 * consistent across low-level and high-level BTF and program loading 1354 * APIs within libbpf and provides a sensible behavior in practice 1355 */ 1356 if (log_level) { 1357 attr.btf_log_buf = ptr_to_u64(log_buf); 1358 attr.btf_log_size = (__u32)log_size; 1359 attr.btf_log_level = log_level; 1360 } 1361 1362 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz); 1363 if (fd < 0 && log_buf && log_level == 0) { 1364 attr.btf_log_buf = ptr_to_u64(log_buf); 1365 attr.btf_log_size = (__u32)log_size; 1366 attr.btf_log_level = 1; 1367 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz); 1368 } 1369 1370 OPTS_SET(opts, log_true_size, attr.btf_log_true_size); 1371 return libbpf_err_errno(fd); 1372 } 1373 1374 int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len, 1375 __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset, 1376 __u64 *probe_addr) 1377 { 1378 const size_t attr_sz = offsetofend(union bpf_attr, task_fd_query); 1379 union bpf_attr attr; 1380 int err; 1381 1382 memset(&attr, 0, attr_sz); 1383 attr.task_fd_query.pid = pid; 1384 attr.task_fd_query.fd = fd; 1385 attr.task_fd_query.flags = flags; 1386 attr.task_fd_query.buf = ptr_to_u64(buf); 1387 attr.task_fd_query.buf_len = *buf_len; 1388 1389 err = sys_bpf(BPF_TASK_FD_QUERY, &attr, attr_sz); 1390 1391 *buf_len = attr.task_fd_query.buf_len; 1392 *prog_id = attr.task_fd_query.prog_id; 1393 *fd_type = attr.task_fd_query.fd_type; 1394 *probe_offset = attr.task_fd_query.probe_offset; 1395 *probe_addr = attr.task_fd_query.probe_addr; 1396 1397 return libbpf_err_errno(err); 1398 } 1399 1400 int bpf_enable_stats(enum bpf_stats_type type) 1401 { 1402 const size_t attr_sz = offsetofend(union bpf_attr, enable_stats); 1403 union bpf_attr attr; 1404 int fd; 1405 1406 memset(&attr, 0, attr_sz); 1407 attr.enable_stats.type = type; 1408 1409 fd = sys_bpf_fd(BPF_ENABLE_STATS, &attr, attr_sz); 1410 return libbpf_err_errno(fd); 1411 } 1412 1413 int bpf_prog_bind_map(int prog_fd, int map_fd, 1414 const struct bpf_prog_bind_opts *opts) 1415 { 1416 const size_t attr_sz = offsetofend(union bpf_attr, prog_bind_map); 1417 union bpf_attr attr; 1418 int ret; 1419 1420 if (!OPTS_VALID(opts, bpf_prog_bind_opts)) 1421 return libbpf_err(-EINVAL); 1422 1423 memset(&attr, 0, attr_sz); 1424 attr.prog_bind_map.prog_fd = prog_fd; 1425 attr.prog_bind_map.map_fd = map_fd; 1426 attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0); 1427 1428 ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz); 1429 return libbpf_err_errno(ret); 1430 } 1431 1432 int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts) 1433 { 1434 const size_t attr_sz = offsetofend(union bpf_attr, token_create); 1435 union bpf_attr attr; 1436 int fd; 1437 1438 if (!OPTS_VALID(opts, bpf_token_create_opts)) 1439 return libbpf_err(-EINVAL); 1440 1441 memset(&attr, 0, attr_sz); 1442 attr.token_create.bpffs_fd = bpffs_fd; 1443 attr.token_create.flags = OPTS_GET(opts, flags, 0); 1444 1445 fd = sys_bpf_fd(BPF_TOKEN_CREATE, &attr, attr_sz); 1446 return libbpf_err_errno(fd); 1447 } 1448 1449 int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len, 1450 struct bpf_prog_stream_read_opts *opts) 1451 { 1452 const size_t attr_sz = offsetofend(union bpf_attr, prog_stream_read); 1453 union bpf_attr attr; 1454 int err; 1455 1456 if (!OPTS_VALID(opts, bpf_prog_stream_read_opts)) 1457 return libbpf_err(-EINVAL); 1458 1459 memset(&attr, 0, attr_sz); 1460 attr.prog_stream_read.stream_buf = ptr_to_u64(buf); 1461 attr.prog_stream_read.stream_buf_len = buf_len; 1462 attr.prog_stream_read.stream_id = stream_id; 1463 attr.prog_stream_read.prog_fd = prog_fd; 1464 1465 err = sys_bpf(BPF_PROG_STREAM_READ_BY_FD, &attr, attr_sz); 1466 return libbpf_err_errno(err); 1467 } 1468 1469 int bpf_prog_stream_open(int prog_fd, __u32 stream_id, 1470 const struct bpf_prog_stream_open_opts *opts) 1471 { 1472 const size_t attr_sz = offsetofend(union bpf_attr, prog_stream_open); 1473 union bpf_attr attr; 1474 int fd; 1475 1476 if (!OPTS_VALID(opts, bpf_prog_stream_open_opts)) 1477 return libbpf_err(-EINVAL); 1478 1479 memset(&attr, 0, attr_sz); 1480 attr.prog_stream_open.prog_fd = prog_fd; 1481 attr.prog_stream_open.stream_id = stream_id; 1482 attr.prog_stream_open.flags = OPTS_GET(opts, flags, 0); 1483 1484 fd = sys_bpf_fd(BPF_PROG_STREAM_OPEN, &attr, attr_sz); 1485 return libbpf_err_errno(fd); 1486 } 1487 1488 int bpf_prog_assoc_struct_ops(int prog_fd, int map_fd, 1489 struct bpf_prog_assoc_struct_ops_opts *opts) 1490 { 1491 const size_t attr_sz = offsetofend(union bpf_attr, prog_assoc_struct_ops); 1492 union bpf_attr attr; 1493 int err; 1494 1495 if (!OPTS_VALID(opts, bpf_prog_assoc_struct_ops_opts)) 1496 return libbpf_err(-EINVAL); 1497 1498 memset(&attr, 0, attr_sz); 1499 attr.prog_assoc_struct_ops.map_fd = map_fd; 1500 attr.prog_assoc_struct_ops.prog_fd = prog_fd; 1501 attr.prog_assoc_struct_ops.flags = OPTS_GET(opts, flags, 0); 1502 1503 err = sys_bpf(BPF_PROG_ASSOC_STRUCT_OPS, &attr, attr_sz); 1504 return libbpf_err_errno(err); 1505 } 1506