1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ 3 4 #define _GNU_SOURCE 5 #include <ctype.h> 6 #include <errno.h> 7 #include <fcntl.h> 8 #include <ftw.h> 9 #include <libgen.h> 10 #include <mntent.h> 11 #include <stdbool.h> 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include <unistd.h> 16 #include <linux/limits.h> 17 #include <linux/magic.h> 18 #include <net/if.h> 19 #include <sys/mount.h> 20 #include <sys/resource.h> 21 #include <sys/stat.h> 22 #include <sys/vfs.h> 23 24 #include <bpf/bpf.h> 25 #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */ 26 27 #include "main.h" 28 29 #ifndef BPF_FS_MAGIC 30 #define BPF_FS_MAGIC 0xcafe4a11 31 #endif 32 33 const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = { 34 [BPF_CGROUP_INET_INGRESS] = "ingress", 35 [BPF_CGROUP_INET_EGRESS] = "egress", 36 [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create", 37 [BPF_CGROUP_INET_SOCK_RELEASE] = "sock_release", 38 [BPF_CGROUP_SOCK_OPS] = "sock_ops", 39 [BPF_CGROUP_DEVICE] = "device", 40 [BPF_CGROUP_INET4_BIND] = "bind4", 41 [BPF_CGROUP_INET6_BIND] = "bind6", 42 [BPF_CGROUP_INET4_CONNECT] = "connect4", 43 [BPF_CGROUP_INET6_CONNECT] = "connect6", 44 [BPF_CGROUP_INET4_POST_BIND] = "post_bind4", 45 [BPF_CGROUP_INET6_POST_BIND] = "post_bind6", 46 [BPF_CGROUP_INET4_GETPEERNAME] = "getpeername4", 47 [BPF_CGROUP_INET6_GETPEERNAME] = "getpeername6", 48 [BPF_CGROUP_INET4_GETSOCKNAME] = "getsockname4", 49 [BPF_CGROUP_INET6_GETSOCKNAME] = "getsockname6", 50 [BPF_CGROUP_UDP4_SENDMSG] = "sendmsg4", 51 [BPF_CGROUP_UDP6_SENDMSG] = "sendmsg6", 52 [BPF_CGROUP_SYSCTL] = "sysctl", 53 [BPF_CGROUP_UDP4_RECVMSG] = "recvmsg4", 54 [BPF_CGROUP_UDP6_RECVMSG] = "recvmsg6", 55 [BPF_CGROUP_GETSOCKOPT] = "getsockopt", 56 [BPF_CGROUP_SETSOCKOPT] = "setsockopt", 57 58 [BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser", 59 [BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict", 60 [BPF_SK_SKB_VERDICT] = "sk_skb_verdict", 61 [BPF_SK_MSG_VERDICT] = "sk_msg_verdict", 62 [BPF_LIRC_MODE2] = "lirc_mode2", 63 [BPF_FLOW_DISSECTOR] = "flow_dissector", 64 [BPF_TRACE_RAW_TP] = "raw_tp", 65 [BPF_TRACE_FENTRY] = "fentry", 66 [BPF_TRACE_FEXIT] = "fexit", 67 [BPF_MODIFY_RETURN] = "mod_ret", 68 [BPF_LSM_MAC] = "lsm_mac", 69 [BPF_SK_LOOKUP] = "sk_lookup", 70 }; 71 72 void p_err(const char *fmt, ...) 73 { 74 va_list ap; 75 76 va_start(ap, fmt); 77 if (json_output) { 78 jsonw_start_object(json_wtr); 79 jsonw_name(json_wtr, "error"); 80 jsonw_vprintf_enquote(json_wtr, fmt, ap); 81 jsonw_end_object(json_wtr); 82 } else { 83 fprintf(stderr, "Error: "); 84 vfprintf(stderr, fmt, ap); 85 fprintf(stderr, "\n"); 86 } 87 va_end(ap); 88 } 89 90 void p_info(const char *fmt, ...) 91 { 92 va_list ap; 93 94 if (json_output) 95 return; 96 97 va_start(ap, fmt); 98 vfprintf(stderr, fmt, ap); 99 fprintf(stderr, "\n"); 100 va_end(ap); 101 } 102 103 static bool is_bpffs(char *path) 104 { 105 struct statfs st_fs; 106 107 if (statfs(path, &st_fs) < 0) 108 return false; 109 110 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC; 111 } 112 113 void set_max_rlimit(void) 114 { 115 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; 116 117 setrlimit(RLIMIT_MEMLOCK, &rinf); 118 } 119 120 static int 121 mnt_fs(const char *target, const char *type, char *buff, size_t bufflen) 122 { 123 bool bind_done = false; 124 125 while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) { 126 if (errno != EINVAL || bind_done) { 127 snprintf(buff, bufflen, 128 "mount --make-private %s failed: %s", 129 target, strerror(errno)); 130 return -1; 131 } 132 133 if (mount(target, target, "none", MS_BIND, NULL)) { 134 snprintf(buff, bufflen, 135 "mount --bind %s %s failed: %s", 136 target, target, strerror(errno)); 137 return -1; 138 } 139 140 bind_done = true; 141 } 142 143 if (mount(type, target, type, 0, "mode=0700")) { 144 snprintf(buff, bufflen, "mount -t %s %s %s failed: %s", 145 type, type, target, strerror(errno)); 146 return -1; 147 } 148 149 return 0; 150 } 151 152 int mount_tracefs(const char *target) 153 { 154 char err_str[ERR_MAX_LEN]; 155 int err; 156 157 err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN); 158 if (err) { 159 err_str[ERR_MAX_LEN - 1] = '\0'; 160 p_err("can't mount tracefs: %s", err_str); 161 } 162 163 return err; 164 } 165 166 int open_obj_pinned(const char *path, bool quiet) 167 { 168 char *pname; 169 int fd = -1; 170 171 pname = strdup(path); 172 if (!pname) { 173 if (!quiet) 174 p_err("mem alloc failed"); 175 goto out_ret; 176 } 177 178 fd = bpf_obj_get(pname); 179 if (fd < 0) { 180 if (!quiet) 181 p_err("bpf obj get (%s): %s", pname, 182 errno == EACCES && !is_bpffs(dirname(pname)) ? 183 "directory not in bpf file system (bpffs)" : 184 strerror(errno)); 185 goto out_free; 186 } 187 188 out_free: 189 free(pname); 190 out_ret: 191 return fd; 192 } 193 194 int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type) 195 { 196 enum bpf_obj_type type; 197 int fd; 198 199 fd = open_obj_pinned(path, false); 200 if (fd < 0) 201 return -1; 202 203 type = get_fd_type(fd); 204 if (type < 0) { 205 close(fd); 206 return type; 207 } 208 if (type != exp_type) { 209 p_err("incorrect object type: %s", get_fd_type_name(type)); 210 close(fd); 211 return -1; 212 } 213 214 return fd; 215 } 216 217 int mount_bpffs_for_pin(const char *name) 218 { 219 char err_str[ERR_MAX_LEN]; 220 char *file; 221 char *dir; 222 int err = 0; 223 224 file = malloc(strlen(name) + 1); 225 if (!file) { 226 p_err("mem alloc failed"); 227 return -1; 228 } 229 230 strcpy(file, name); 231 dir = dirname(file); 232 233 if (is_bpffs(dir)) 234 /* nothing to do if already mounted */ 235 goto out_free; 236 237 if (block_mount) { 238 p_err("no BPF file system found, not mounting it due to --nomount option"); 239 err = -1; 240 goto out_free; 241 } 242 243 err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN); 244 if (err) { 245 err_str[ERR_MAX_LEN - 1] = '\0'; 246 p_err("can't mount BPF file system to pin the object (%s): %s", 247 name, err_str); 248 } 249 250 out_free: 251 free(file); 252 return err; 253 } 254 255 int do_pin_fd(int fd, const char *name) 256 { 257 int err; 258 259 err = mount_bpffs_for_pin(name); 260 if (err) 261 return err; 262 263 err = bpf_obj_pin(fd, name); 264 if (err) 265 p_err("can't pin the object (%s): %s", name, strerror(errno)); 266 267 return err; 268 } 269 270 int do_pin_any(int argc, char **argv, int (*get_fd)(int *, char ***)) 271 { 272 int err; 273 int fd; 274 275 fd = get_fd(&argc, &argv); 276 if (fd < 0) 277 return fd; 278 279 err = do_pin_fd(fd, *argv); 280 281 close(fd); 282 return err; 283 } 284 285 const char *get_fd_type_name(enum bpf_obj_type type) 286 { 287 static const char * const names[] = { 288 [BPF_OBJ_UNKNOWN] = "unknown", 289 [BPF_OBJ_PROG] = "prog", 290 [BPF_OBJ_MAP] = "map", 291 }; 292 293 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type]) 294 return names[BPF_OBJ_UNKNOWN]; 295 296 return names[type]; 297 } 298 299 int get_fd_type(int fd) 300 { 301 char path[PATH_MAX]; 302 char buf[512]; 303 ssize_t n; 304 305 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd); 306 307 n = readlink(path, buf, sizeof(buf)); 308 if (n < 0) { 309 p_err("can't read link type: %s", strerror(errno)); 310 return -1; 311 } 312 if (n == sizeof(path)) { 313 p_err("can't read link type: path too long!"); 314 return -1; 315 } 316 317 if (strstr(buf, "bpf-map")) 318 return BPF_OBJ_MAP; 319 else if (strstr(buf, "bpf-prog")) 320 return BPF_OBJ_PROG; 321 else if (strstr(buf, "bpf-link")) 322 return BPF_OBJ_LINK; 323 324 return BPF_OBJ_UNKNOWN; 325 } 326 327 char *get_fdinfo(int fd, const char *key) 328 { 329 char path[PATH_MAX]; 330 char *line = NULL; 331 size_t line_n = 0; 332 ssize_t n; 333 FILE *fdi; 334 335 snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd); 336 337 fdi = fopen(path, "r"); 338 if (!fdi) 339 return NULL; 340 341 while ((n = getline(&line, &line_n, fdi)) > 0) { 342 char *value; 343 int len; 344 345 if (!strstr(line, key)) 346 continue; 347 348 fclose(fdi); 349 350 value = strchr(line, '\t'); 351 if (!value || !value[1]) { 352 free(line); 353 return NULL; 354 } 355 value++; 356 357 len = strlen(value); 358 memmove(line, value, len); 359 line[len - 1] = '\0'; 360 361 return line; 362 } 363 364 free(line); 365 fclose(fdi); 366 return NULL; 367 } 368 369 void print_data_json(uint8_t *data, size_t len) 370 { 371 unsigned int i; 372 373 jsonw_start_array(json_wtr); 374 for (i = 0; i < len; i++) 375 jsonw_printf(json_wtr, "%d", data[i]); 376 jsonw_end_array(json_wtr); 377 } 378 379 void print_hex_data_json(uint8_t *data, size_t len) 380 { 381 unsigned int i; 382 383 jsonw_start_array(json_wtr); 384 for (i = 0; i < len; i++) 385 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]); 386 jsonw_end_array(json_wtr); 387 } 388 389 /* extra params for nftw cb */ 390 static struct pinned_obj_table *build_fn_table; 391 static enum bpf_obj_type build_fn_type; 392 393 static int do_build_table_cb(const char *fpath, const struct stat *sb, 394 int typeflag, struct FTW *ftwbuf) 395 { 396 struct bpf_prog_info pinned_info; 397 __u32 len = sizeof(pinned_info); 398 struct pinned_obj *obj_node; 399 enum bpf_obj_type objtype; 400 int fd, err = 0; 401 402 if (typeflag != FTW_F) 403 goto out_ret; 404 405 fd = open_obj_pinned(fpath, true); 406 if (fd < 0) 407 goto out_ret; 408 409 objtype = get_fd_type(fd); 410 if (objtype != build_fn_type) 411 goto out_close; 412 413 memset(&pinned_info, 0, sizeof(pinned_info)); 414 if (bpf_obj_get_info_by_fd(fd, &pinned_info, &len)) 415 goto out_close; 416 417 obj_node = calloc(1, sizeof(*obj_node)); 418 if (!obj_node) { 419 err = -1; 420 goto out_close; 421 } 422 423 obj_node->id = pinned_info.id; 424 obj_node->path = strdup(fpath); 425 if (!obj_node->path) { 426 err = -1; 427 free(obj_node); 428 goto out_close; 429 } 430 431 hash_add(build_fn_table->table, &obj_node->hash, obj_node->id); 432 out_close: 433 close(fd); 434 out_ret: 435 return err; 436 } 437 438 int build_pinned_obj_table(struct pinned_obj_table *tab, 439 enum bpf_obj_type type) 440 { 441 struct mntent *mntent = NULL; 442 FILE *mntfile = NULL; 443 int flags = FTW_PHYS; 444 int nopenfd = 16; 445 int err = 0; 446 447 mntfile = setmntent("/proc/mounts", "r"); 448 if (!mntfile) 449 return -1; 450 451 build_fn_table = tab; 452 build_fn_type = type; 453 454 while ((mntent = getmntent(mntfile))) { 455 char *path = mntent->mnt_dir; 456 457 if (strncmp(mntent->mnt_type, "bpf", 3) != 0) 458 continue; 459 err = nftw(path, do_build_table_cb, nopenfd, flags); 460 if (err) 461 break; 462 } 463 fclose(mntfile); 464 return err; 465 } 466 467 void delete_pinned_obj_table(struct pinned_obj_table *tab) 468 { 469 struct pinned_obj *obj; 470 struct hlist_node *tmp; 471 unsigned int bkt; 472 473 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) { 474 hash_del(&obj->hash); 475 free(obj->path); 476 free(obj); 477 } 478 } 479 480 unsigned int get_page_size(void) 481 { 482 static int result; 483 484 if (!result) 485 result = getpagesize(); 486 return result; 487 } 488 489 unsigned int get_possible_cpus(void) 490 { 491 int cpus = libbpf_num_possible_cpus(); 492 493 if (cpus < 0) { 494 p_err("Can't get # of possible cpus: %s", strerror(-cpus)); 495 exit(-1); 496 } 497 return cpus; 498 } 499 500 static char * 501 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf) 502 { 503 struct stat st; 504 int err; 505 506 err = stat("/proc/self/ns/net", &st); 507 if (err) { 508 p_err("Can't stat /proc/self: %s", strerror(errno)); 509 return NULL; 510 } 511 512 if (st.st_dev != ns_dev || st.st_ino != ns_ino) 513 return NULL; 514 515 return if_indextoname(ifindex, buf); 516 } 517 518 static int read_sysfs_hex_int(char *path) 519 { 520 char vendor_id_buf[8]; 521 int len; 522 int fd; 523 524 fd = open(path, O_RDONLY); 525 if (fd < 0) { 526 p_err("Can't open %s: %s", path, strerror(errno)); 527 return -1; 528 } 529 530 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf)); 531 close(fd); 532 if (len < 0) { 533 p_err("Can't read %s: %s", path, strerror(errno)); 534 return -1; 535 } 536 if (len >= (int)sizeof(vendor_id_buf)) { 537 p_err("Value in %s too long", path); 538 return -1; 539 } 540 541 vendor_id_buf[len] = 0; 542 543 return strtol(vendor_id_buf, NULL, 0); 544 } 545 546 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name) 547 { 548 char full_path[64]; 549 550 snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s", 551 devname, entry_name); 552 553 return read_sysfs_hex_int(full_path); 554 } 555 556 const char * 557 ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, 558 const char **opt) 559 { 560 char devname[IF_NAMESIZE]; 561 int vendor_id; 562 int device_id; 563 564 if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) { 565 p_err("Can't get net device name for ifindex %d: %s", ifindex, 566 strerror(errno)); 567 return NULL; 568 } 569 570 vendor_id = read_sysfs_netdev_hex_int(devname, "vendor"); 571 if (vendor_id < 0) { 572 p_err("Can't get device vendor id for %s", devname); 573 return NULL; 574 } 575 576 switch (vendor_id) { 577 case 0x19ee: 578 device_id = read_sysfs_netdev_hex_int(devname, "device"); 579 if (device_id != 0x4000 && 580 device_id != 0x6000 && 581 device_id != 0x6003) 582 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch"); 583 *opt = "ctx4"; 584 return "NFP-6xxx"; 585 default: 586 p_err("Can't get bfd arch name for device vendor id 0x%04x", 587 vendor_id); 588 return NULL; 589 } 590 } 591 592 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode) 593 { 594 char name[IF_NAMESIZE]; 595 596 if (!ifindex) 597 return; 598 599 printf(" offloaded_to "); 600 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name)) 601 printf("%s", name); 602 else 603 printf("ifindex %u ns_dev %llu ns_ino %llu", 604 ifindex, ns_dev, ns_inode); 605 } 606 607 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode) 608 { 609 char name[IF_NAMESIZE]; 610 611 if (!ifindex) 612 return; 613 614 jsonw_name(json_wtr, "dev"); 615 jsonw_start_object(json_wtr); 616 jsonw_uint_field(json_wtr, "ifindex", ifindex); 617 jsonw_uint_field(json_wtr, "ns_dev", ns_dev); 618 jsonw_uint_field(json_wtr, "ns_inode", ns_inode); 619 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name)) 620 jsonw_string_field(json_wtr, "ifname", name); 621 jsonw_end_object(json_wtr); 622 } 623 624 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what) 625 { 626 char *endptr; 627 628 NEXT_ARGP(); 629 630 if (*val) { 631 p_err("%s already specified", what); 632 return -1; 633 } 634 635 *val = strtoul(**argv, &endptr, 0); 636 if (*endptr) { 637 p_err("can't parse %s as %s", **argv, what); 638 return -1; 639 } 640 NEXT_ARGP(); 641 642 return 0; 643 } 644 645 int __printf(2, 0) 646 print_all_levels(__maybe_unused enum libbpf_print_level level, 647 const char *format, va_list args) 648 { 649 return vfprintf(stderr, format, args); 650 } 651 652 static int prog_fd_by_nametag(void *nametag, int **fds, bool tag) 653 { 654 unsigned int id = 0; 655 int fd, nb_fds = 0; 656 void *tmp; 657 int err; 658 659 while (true) { 660 struct bpf_prog_info info = {}; 661 __u32 len = sizeof(info); 662 663 err = bpf_prog_get_next_id(id, &id); 664 if (err) { 665 if (errno != ENOENT) { 666 p_err("%s", strerror(errno)); 667 goto err_close_fds; 668 } 669 return nb_fds; 670 } 671 672 fd = bpf_prog_get_fd_by_id(id); 673 if (fd < 0) { 674 p_err("can't get prog by id (%u): %s", 675 id, strerror(errno)); 676 goto err_close_fds; 677 } 678 679 err = bpf_obj_get_info_by_fd(fd, &info, &len); 680 if (err) { 681 p_err("can't get prog info (%u): %s", 682 id, strerror(errno)); 683 goto err_close_fd; 684 } 685 686 if ((tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) || 687 (!tag && strncmp(nametag, info.name, BPF_OBJ_NAME_LEN))) { 688 close(fd); 689 continue; 690 } 691 692 if (nb_fds > 0) { 693 tmp = realloc(*fds, (nb_fds + 1) * sizeof(int)); 694 if (!tmp) { 695 p_err("failed to realloc"); 696 goto err_close_fd; 697 } 698 *fds = tmp; 699 } 700 (*fds)[nb_fds++] = fd; 701 } 702 703 err_close_fd: 704 close(fd); 705 err_close_fds: 706 while (--nb_fds >= 0) 707 close((*fds)[nb_fds]); 708 return -1; 709 } 710 711 int prog_parse_fds(int *argc, char ***argv, int **fds) 712 { 713 if (is_prefix(**argv, "id")) { 714 unsigned int id; 715 char *endptr; 716 717 NEXT_ARGP(); 718 719 id = strtoul(**argv, &endptr, 0); 720 if (*endptr) { 721 p_err("can't parse %s as ID", **argv); 722 return -1; 723 } 724 NEXT_ARGP(); 725 726 (*fds)[0] = bpf_prog_get_fd_by_id(id); 727 if ((*fds)[0] < 0) { 728 p_err("get by id (%u): %s", id, strerror(errno)); 729 return -1; 730 } 731 return 1; 732 } else if (is_prefix(**argv, "tag")) { 733 unsigned char tag[BPF_TAG_SIZE]; 734 735 NEXT_ARGP(); 736 737 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2, 738 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7) 739 != BPF_TAG_SIZE) { 740 p_err("can't parse tag"); 741 return -1; 742 } 743 NEXT_ARGP(); 744 745 return prog_fd_by_nametag(tag, fds, true); 746 } else if (is_prefix(**argv, "name")) { 747 char *name; 748 749 NEXT_ARGP(); 750 751 name = **argv; 752 if (strlen(name) > BPF_OBJ_NAME_LEN - 1) { 753 p_err("can't parse name"); 754 return -1; 755 } 756 NEXT_ARGP(); 757 758 return prog_fd_by_nametag(name, fds, false); 759 } else if (is_prefix(**argv, "pinned")) { 760 char *path; 761 762 NEXT_ARGP(); 763 764 path = **argv; 765 NEXT_ARGP(); 766 767 (*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_PROG); 768 if ((*fds)[0] < 0) 769 return -1; 770 return 1; 771 } 772 773 p_err("expected 'id', 'tag', 'name' or 'pinned', got: '%s'?", **argv); 774 return -1; 775 } 776 777 int prog_parse_fd(int *argc, char ***argv) 778 { 779 int *fds = NULL; 780 int nb_fds, fd; 781 782 fds = malloc(sizeof(int)); 783 if (!fds) { 784 p_err("mem alloc failed"); 785 return -1; 786 } 787 nb_fds = prog_parse_fds(argc, argv, &fds); 788 if (nb_fds != 1) { 789 if (nb_fds > 1) { 790 p_err("several programs match this handle"); 791 while (nb_fds--) 792 close(fds[nb_fds]); 793 } 794 fd = -1; 795 goto exit_free; 796 } 797 798 fd = fds[0]; 799 exit_free: 800 free(fds); 801 return fd; 802 } 803 804 static int map_fd_by_name(char *name, int **fds) 805 { 806 unsigned int id = 0; 807 int fd, nb_fds = 0; 808 void *tmp; 809 int err; 810 811 while (true) { 812 struct bpf_map_info info = {}; 813 __u32 len = sizeof(info); 814 815 err = bpf_map_get_next_id(id, &id); 816 if (err) { 817 if (errno != ENOENT) { 818 p_err("%s", strerror(errno)); 819 goto err_close_fds; 820 } 821 return nb_fds; 822 } 823 824 fd = bpf_map_get_fd_by_id(id); 825 if (fd < 0) { 826 p_err("can't get map by id (%u): %s", 827 id, strerror(errno)); 828 goto err_close_fds; 829 } 830 831 err = bpf_obj_get_info_by_fd(fd, &info, &len); 832 if (err) { 833 p_err("can't get map info (%u): %s", 834 id, strerror(errno)); 835 goto err_close_fd; 836 } 837 838 if (strncmp(name, info.name, BPF_OBJ_NAME_LEN)) { 839 close(fd); 840 continue; 841 } 842 843 if (nb_fds > 0) { 844 tmp = realloc(*fds, (nb_fds + 1) * sizeof(int)); 845 if (!tmp) { 846 p_err("failed to realloc"); 847 goto err_close_fd; 848 } 849 *fds = tmp; 850 } 851 (*fds)[nb_fds++] = fd; 852 } 853 854 err_close_fd: 855 close(fd); 856 err_close_fds: 857 while (--nb_fds >= 0) 858 close((*fds)[nb_fds]); 859 return -1; 860 } 861 862 int map_parse_fds(int *argc, char ***argv, int **fds) 863 { 864 if (is_prefix(**argv, "id")) { 865 unsigned int id; 866 char *endptr; 867 868 NEXT_ARGP(); 869 870 id = strtoul(**argv, &endptr, 0); 871 if (*endptr) { 872 p_err("can't parse %s as ID", **argv); 873 return -1; 874 } 875 NEXT_ARGP(); 876 877 (*fds)[0] = bpf_map_get_fd_by_id(id); 878 if ((*fds)[0] < 0) { 879 p_err("get map by id (%u): %s", id, strerror(errno)); 880 return -1; 881 } 882 return 1; 883 } else if (is_prefix(**argv, "name")) { 884 char *name; 885 886 NEXT_ARGP(); 887 888 name = **argv; 889 if (strlen(name) > BPF_OBJ_NAME_LEN - 1) { 890 p_err("can't parse name"); 891 return -1; 892 } 893 NEXT_ARGP(); 894 895 return map_fd_by_name(name, fds); 896 } else if (is_prefix(**argv, "pinned")) { 897 char *path; 898 899 NEXT_ARGP(); 900 901 path = **argv; 902 NEXT_ARGP(); 903 904 (*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_MAP); 905 if ((*fds)[0] < 0) 906 return -1; 907 return 1; 908 } 909 910 p_err("expected 'id', 'name' or 'pinned', got: '%s'?", **argv); 911 return -1; 912 } 913 914 int map_parse_fd(int *argc, char ***argv) 915 { 916 int *fds = NULL; 917 int nb_fds, fd; 918 919 fds = malloc(sizeof(int)); 920 if (!fds) { 921 p_err("mem alloc failed"); 922 return -1; 923 } 924 nb_fds = map_parse_fds(argc, argv, &fds); 925 if (nb_fds != 1) { 926 if (nb_fds > 1) { 927 p_err("several maps match this handle"); 928 while (nb_fds--) 929 close(fds[nb_fds]); 930 } 931 fd = -1; 932 goto exit_free; 933 } 934 935 fd = fds[0]; 936 exit_free: 937 free(fds); 938 return fd; 939 } 940 941 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len) 942 { 943 int err; 944 int fd; 945 946 fd = map_parse_fd(argc, argv); 947 if (fd < 0) 948 return -1; 949 950 err = bpf_obj_get_info_by_fd(fd, info, info_len); 951 if (err) { 952 p_err("can't get map info: %s", strerror(errno)); 953 close(fd); 954 return err; 955 } 956 957 return fd; 958 } 959