1 /* 2 * Copyright (C) 2017 Netronome Systems, Inc. 3 * 4 * This software is dual licensed under the GNU General License Version 2, 5 * June 1991 as shown in the file COPYING in the top-level directory of this 6 * source tree or the BSD 2-Clause License provided below. You have the 7 * option to license this software under the complete terms of either license. 8 * 9 * The BSD 2-Clause License: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * 1. Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 */ 33 34 /* Author: Jakub Kicinski <kubakici@wp.pl> */ 35 36 #include <assert.h> 37 #include <ctype.h> 38 #include <errno.h> 39 #include <fcntl.h> 40 #include <stdbool.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include <sys/types.h> 46 #include <sys/stat.h> 47 48 #include <bpf.h> 49 50 #include "main.h" 51 52 static const char * const map_type_name[] = { 53 [BPF_MAP_TYPE_UNSPEC] = "unspec", 54 [BPF_MAP_TYPE_HASH] = "hash", 55 [BPF_MAP_TYPE_ARRAY] = "array", 56 [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array", 57 [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array", 58 [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash", 59 [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array", 60 [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace", 61 [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array", 62 [BPF_MAP_TYPE_LRU_HASH] = "lru_hash", 63 [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash", 64 [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie", 65 [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps", 66 [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps", 67 [BPF_MAP_TYPE_DEVMAP] = "devmap", 68 [BPF_MAP_TYPE_SOCKMAP] = "sockmap", 69 }; 70 71 static unsigned int get_possible_cpus(void) 72 { 73 static unsigned int result; 74 char buf[128]; 75 long int n; 76 char *ptr; 77 int fd; 78 79 if (result) 80 return result; 81 82 fd = open("/sys/devices/system/cpu/possible", O_RDONLY); 83 if (fd < 0) { 84 p_err("can't open sysfs possible cpus"); 85 exit(-1); 86 } 87 88 n = read(fd, buf, sizeof(buf)); 89 if (n < 2) { 90 p_err("can't read sysfs possible cpus"); 91 exit(-1); 92 } 93 close(fd); 94 95 if (n == sizeof(buf)) { 96 p_err("read sysfs possible cpus overflow"); 97 exit(-1); 98 } 99 100 ptr = buf; 101 n = 0; 102 while (*ptr && *ptr != '\n') { 103 unsigned int a, b; 104 105 if (sscanf(ptr, "%u-%u", &a, &b) == 2) { 106 n += b - a + 1; 107 108 ptr = strchr(ptr, '-') + 1; 109 } else if (sscanf(ptr, "%u", &a) == 1) { 110 n++; 111 } else { 112 assert(0); 113 } 114 115 while (isdigit(*ptr)) 116 ptr++; 117 if (*ptr == ',') 118 ptr++; 119 } 120 121 result = n; 122 123 return result; 124 } 125 126 static bool map_is_per_cpu(__u32 type) 127 { 128 return type == BPF_MAP_TYPE_PERCPU_HASH || 129 type == BPF_MAP_TYPE_PERCPU_ARRAY || 130 type == BPF_MAP_TYPE_LRU_PERCPU_HASH; 131 } 132 133 static bool map_is_map_of_maps(__u32 type) 134 { 135 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS || 136 type == BPF_MAP_TYPE_HASH_OF_MAPS; 137 } 138 139 static bool map_is_map_of_progs(__u32 type) 140 { 141 return type == BPF_MAP_TYPE_PROG_ARRAY; 142 } 143 144 static void *alloc_value(struct bpf_map_info *info) 145 { 146 if (map_is_per_cpu(info->type)) 147 return malloc(info->value_size * get_possible_cpus()); 148 else 149 return malloc(info->value_size); 150 } 151 152 static int map_parse_fd(int *argc, char ***argv) 153 { 154 int fd; 155 156 if (is_prefix(**argv, "id")) { 157 unsigned int id; 158 char *endptr; 159 160 NEXT_ARGP(); 161 162 id = strtoul(**argv, &endptr, 0); 163 if (*endptr) { 164 p_err("can't parse %s as ID", **argv); 165 return -1; 166 } 167 NEXT_ARGP(); 168 169 fd = bpf_map_get_fd_by_id(id); 170 if (fd < 0) 171 p_err("get map by id (%u): %s", id, strerror(errno)); 172 return fd; 173 } else if (is_prefix(**argv, "pinned")) { 174 char *path; 175 176 NEXT_ARGP(); 177 178 path = **argv; 179 NEXT_ARGP(); 180 181 return open_obj_pinned_any(path, BPF_OBJ_MAP); 182 } 183 184 p_err("expected 'id' or 'pinned', got: '%s'?", **argv); 185 return -1; 186 } 187 188 static int 189 map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len) 190 { 191 int err; 192 int fd; 193 194 fd = map_parse_fd(argc, argv); 195 if (fd < 0) 196 return -1; 197 198 err = bpf_obj_get_info_by_fd(fd, info, info_len); 199 if (err) { 200 p_err("can't get map info: %s", strerror(errno)); 201 close(fd); 202 return err; 203 } 204 205 return fd; 206 } 207 208 static void print_entry_json(struct bpf_map_info *info, unsigned char *key, 209 unsigned char *value) 210 { 211 jsonw_start_object(json_wtr); 212 213 if (!map_is_per_cpu(info->type)) { 214 jsonw_name(json_wtr, "key"); 215 print_hex_data_json(key, info->key_size); 216 jsonw_name(json_wtr, "value"); 217 print_hex_data_json(value, info->value_size); 218 } else { 219 unsigned int i, n; 220 221 n = get_possible_cpus(); 222 223 jsonw_name(json_wtr, "key"); 224 print_hex_data_json(key, info->key_size); 225 226 jsonw_name(json_wtr, "values"); 227 jsonw_start_array(json_wtr); 228 for (i = 0; i < n; i++) { 229 jsonw_start_object(json_wtr); 230 231 jsonw_int_field(json_wtr, "cpu", i); 232 233 jsonw_name(json_wtr, "value"); 234 print_hex_data_json(value + i * info->value_size, 235 info->value_size); 236 237 jsonw_end_object(json_wtr); 238 } 239 jsonw_end_array(json_wtr); 240 } 241 242 jsonw_end_object(json_wtr); 243 } 244 245 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key, 246 unsigned char *value) 247 { 248 if (!map_is_per_cpu(info->type)) { 249 bool single_line, break_names; 250 251 break_names = info->key_size > 16 || info->value_size > 16; 252 single_line = info->key_size + info->value_size <= 24 && 253 !break_names; 254 255 printf("key:%c", break_names ? '\n' : ' '); 256 fprint_hex(stdout, key, info->key_size, " "); 257 258 printf(single_line ? " " : "\n"); 259 260 printf("value:%c", break_names ? '\n' : ' '); 261 fprint_hex(stdout, value, info->value_size, " "); 262 263 printf("\n"); 264 } else { 265 unsigned int i, n; 266 267 n = get_possible_cpus(); 268 269 printf("key:\n"); 270 fprint_hex(stdout, key, info->key_size, " "); 271 printf("\n"); 272 for (i = 0; i < n; i++) { 273 printf("value (CPU %02d):%c", 274 i, info->value_size > 16 ? '\n' : ' '); 275 fprint_hex(stdout, value + i * info->value_size, 276 info->value_size, " "); 277 printf("\n"); 278 } 279 } 280 } 281 282 static char **parse_bytes(char **argv, const char *name, unsigned char *val, 283 unsigned int n) 284 { 285 unsigned int i = 0; 286 char *endptr; 287 288 while (i < n && argv[i]) { 289 val[i] = strtoul(argv[i], &endptr, 0); 290 if (*endptr) { 291 p_err("error parsing byte: %s", argv[i]); 292 return NULL; 293 } 294 i++; 295 } 296 297 if (i != n) { 298 p_err("%s expected %d bytes got %d", name, n, i); 299 return NULL; 300 } 301 302 return argv + i; 303 } 304 305 static int parse_elem(char **argv, struct bpf_map_info *info, 306 void *key, void *value, __u32 key_size, __u32 value_size, 307 __u32 *flags, __u32 **value_fd) 308 { 309 if (!*argv) { 310 if (!key && !value) 311 return 0; 312 p_err("did not find %s", key ? "key" : "value"); 313 return -1; 314 } 315 316 if (is_prefix(*argv, "key")) { 317 if (!key) { 318 if (key_size) 319 p_err("duplicate key"); 320 else 321 p_err("unnecessary key"); 322 return -1; 323 } 324 325 argv = parse_bytes(argv + 1, "key", key, key_size); 326 if (!argv) 327 return -1; 328 329 return parse_elem(argv, info, NULL, value, key_size, value_size, 330 flags, value_fd); 331 } else if (is_prefix(*argv, "value")) { 332 int fd; 333 334 if (!value) { 335 if (value_size) 336 p_err("duplicate value"); 337 else 338 p_err("unnecessary value"); 339 return -1; 340 } 341 342 argv++; 343 344 if (map_is_map_of_maps(info->type)) { 345 int argc = 2; 346 347 if (value_size != 4) { 348 p_err("value smaller than 4B for map in map?"); 349 return -1; 350 } 351 if (!argv[0] || !argv[1]) { 352 p_err("not enough value arguments for map in map"); 353 return -1; 354 } 355 356 fd = map_parse_fd(&argc, &argv); 357 if (fd < 0) 358 return -1; 359 360 *value_fd = value; 361 **value_fd = fd; 362 } else if (map_is_map_of_progs(info->type)) { 363 int argc = 2; 364 365 if (value_size != 4) { 366 p_err("value smaller than 4B for map of progs?"); 367 return -1; 368 } 369 if (!argv[0] || !argv[1]) { 370 p_err("not enough value arguments for map of progs"); 371 return -1; 372 } 373 374 fd = prog_parse_fd(&argc, &argv); 375 if (fd < 0) 376 return -1; 377 378 *value_fd = value; 379 **value_fd = fd; 380 } else { 381 argv = parse_bytes(argv, "value", value, value_size); 382 if (!argv) 383 return -1; 384 } 385 386 return parse_elem(argv, info, key, NULL, key_size, value_size, 387 flags, NULL); 388 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") || 389 is_prefix(*argv, "exist")) { 390 if (!flags) { 391 p_err("flags specified multiple times: %s", *argv); 392 return -1; 393 } 394 395 if (is_prefix(*argv, "any")) 396 *flags = BPF_ANY; 397 else if (is_prefix(*argv, "noexist")) 398 *flags = BPF_NOEXIST; 399 else if (is_prefix(*argv, "exist")) 400 *flags = BPF_EXIST; 401 402 return parse_elem(argv + 1, info, key, value, key_size, 403 value_size, NULL, value_fd); 404 } 405 406 p_err("expected key or value, got: %s", *argv); 407 return -1; 408 } 409 410 static int show_map_close_json(int fd, struct bpf_map_info *info) 411 { 412 char *memlock; 413 414 memlock = get_fdinfo(fd, "memlock"); 415 close(fd); 416 417 jsonw_start_object(json_wtr); 418 419 jsonw_uint_field(json_wtr, "id", info->id); 420 if (info->type < ARRAY_SIZE(map_type_name)) 421 jsonw_string_field(json_wtr, "type", 422 map_type_name[info->type]); 423 else 424 jsonw_uint_field(json_wtr, "type", info->type); 425 426 if (*info->name) 427 jsonw_string_field(json_wtr, "name", info->name); 428 429 jsonw_name(json_wtr, "flags"); 430 jsonw_printf(json_wtr, "%#x", info->map_flags); 431 jsonw_uint_field(json_wtr, "bytes_key", info->key_size); 432 jsonw_uint_field(json_wtr, "bytes_value", info->value_size); 433 jsonw_uint_field(json_wtr, "max_entries", info->max_entries); 434 435 if (memlock) 436 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock)); 437 free(memlock); 438 439 if (!hash_empty(map_table.table)) { 440 struct pinned_obj *obj; 441 442 jsonw_name(json_wtr, "pinned"); 443 jsonw_start_array(json_wtr); 444 hash_for_each_possible(map_table.table, obj, hash, info->id) { 445 if (obj->id == info->id) 446 jsonw_string(json_wtr, obj->path); 447 } 448 jsonw_end_array(json_wtr); 449 } 450 451 jsonw_end_object(json_wtr); 452 453 return 0; 454 } 455 456 static int show_map_close_plain(int fd, struct bpf_map_info *info) 457 { 458 char *memlock; 459 460 memlock = get_fdinfo(fd, "memlock"); 461 close(fd); 462 463 printf("%u: ", info->id); 464 if (info->type < ARRAY_SIZE(map_type_name)) 465 printf("%s ", map_type_name[info->type]); 466 else 467 printf("type %u ", info->type); 468 469 if (*info->name) 470 printf("name %s ", info->name); 471 472 printf("flags 0x%x\n", info->map_flags); 473 printf("\tkey %uB value %uB max_entries %u", 474 info->key_size, info->value_size, info->max_entries); 475 476 if (memlock) 477 printf(" memlock %sB", memlock); 478 free(memlock); 479 480 printf("\n"); 481 if (!hash_empty(map_table.table)) { 482 struct pinned_obj *obj; 483 484 hash_for_each_possible(map_table.table, obj, hash, info->id) { 485 if (obj->id == info->id) 486 printf("\tpinned %s\n", obj->path); 487 } 488 } 489 return 0; 490 } 491 492 static int do_show(int argc, char **argv) 493 { 494 struct bpf_map_info info = {}; 495 __u32 len = sizeof(info); 496 __u32 id = 0; 497 int err; 498 int fd; 499 500 if (show_pinned) 501 build_pinned_obj_table(&map_table, BPF_OBJ_MAP); 502 503 if (argc == 2) { 504 fd = map_parse_fd_and_info(&argc, &argv, &info, &len); 505 if (fd < 0) 506 return -1; 507 508 if (json_output) 509 return show_map_close_json(fd, &info); 510 else 511 return show_map_close_plain(fd, &info); 512 } 513 514 if (argc) 515 return BAD_ARG(); 516 517 if (json_output) 518 jsonw_start_array(json_wtr); 519 while (true) { 520 err = bpf_map_get_next_id(id, &id); 521 if (err) { 522 if (errno == ENOENT) 523 break; 524 p_err("can't get next map: %s%s", strerror(errno), 525 errno == EINVAL ? " -- kernel too old?" : ""); 526 return -1; 527 } 528 529 fd = bpf_map_get_fd_by_id(id); 530 if (fd < 0) { 531 p_err("can't get map by id (%u): %s", 532 id, strerror(errno)); 533 return -1; 534 } 535 536 err = bpf_obj_get_info_by_fd(fd, &info, &len); 537 if (err) { 538 p_err("can't get map info: %s", strerror(errno)); 539 close(fd); 540 return -1; 541 } 542 543 if (json_output) 544 show_map_close_json(fd, &info); 545 else 546 show_map_close_plain(fd, &info); 547 } 548 if (json_output) 549 jsonw_end_array(json_wtr); 550 551 return errno == ENOENT ? 0 : -1; 552 } 553 554 static int do_dump(int argc, char **argv) 555 { 556 void *key, *value, *prev_key; 557 unsigned int num_elems = 0; 558 struct bpf_map_info info = {}; 559 __u32 len = sizeof(info); 560 int err; 561 int fd; 562 563 if (argc != 2) 564 usage(); 565 566 fd = map_parse_fd_and_info(&argc, &argv, &info, &len); 567 if (fd < 0) 568 return -1; 569 570 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) { 571 p_err("Dumping maps of maps and program maps not supported"); 572 close(fd); 573 return -1; 574 } 575 576 key = malloc(info.key_size); 577 value = alloc_value(&info); 578 if (!key || !value) { 579 p_err("mem alloc failed"); 580 err = -1; 581 goto exit_free; 582 } 583 584 prev_key = NULL; 585 if (json_output) 586 jsonw_start_array(json_wtr); 587 while (true) { 588 err = bpf_map_get_next_key(fd, prev_key, key); 589 if (err) { 590 if (errno == ENOENT) 591 err = 0; 592 break; 593 } 594 595 if (!bpf_map_lookup_elem(fd, key, value)) { 596 if (json_output) 597 print_entry_json(&info, key, value); 598 else 599 print_entry_plain(&info, key, value); 600 } else { 601 if (json_output) { 602 jsonw_name(json_wtr, "key"); 603 print_hex_data_json(key, info.key_size); 604 jsonw_name(json_wtr, "value"); 605 jsonw_start_object(json_wtr); 606 jsonw_string_field(json_wtr, "error", 607 "can't lookup element"); 608 jsonw_end_object(json_wtr); 609 } else { 610 p_info("can't lookup element with key: "); 611 fprint_hex(stderr, key, info.key_size, " "); 612 fprintf(stderr, "\n"); 613 } 614 } 615 616 prev_key = key; 617 num_elems++; 618 } 619 620 if (json_output) 621 jsonw_end_array(json_wtr); 622 else 623 printf("Found %u element%s\n", num_elems, 624 num_elems != 1 ? "s" : ""); 625 626 exit_free: 627 free(key); 628 free(value); 629 close(fd); 630 631 return err; 632 } 633 634 static int do_update(int argc, char **argv) 635 { 636 struct bpf_map_info info = {}; 637 __u32 len = sizeof(info); 638 __u32 *value_fd = NULL; 639 __u32 flags = BPF_ANY; 640 void *key, *value; 641 int fd, err; 642 643 if (argc < 2) 644 usage(); 645 646 fd = map_parse_fd_and_info(&argc, &argv, &info, &len); 647 if (fd < 0) 648 return -1; 649 650 key = malloc(info.key_size); 651 value = alloc_value(&info); 652 if (!key || !value) { 653 p_err("mem alloc failed"); 654 err = -1; 655 goto exit_free; 656 } 657 658 err = parse_elem(argv, &info, key, value, info.key_size, 659 info.value_size, &flags, &value_fd); 660 if (err) 661 goto exit_free; 662 663 err = bpf_map_update_elem(fd, key, value, flags); 664 if (err) { 665 p_err("update failed: %s", strerror(errno)); 666 goto exit_free; 667 } 668 669 exit_free: 670 if (value_fd) 671 close(*value_fd); 672 free(key); 673 free(value); 674 close(fd); 675 676 if (!err && json_output) 677 jsonw_null(json_wtr); 678 return err; 679 } 680 681 static int do_lookup(int argc, char **argv) 682 { 683 struct bpf_map_info info = {}; 684 __u32 len = sizeof(info); 685 void *key, *value; 686 int err; 687 int fd; 688 689 if (argc < 2) 690 usage(); 691 692 fd = map_parse_fd_and_info(&argc, &argv, &info, &len); 693 if (fd < 0) 694 return -1; 695 696 key = malloc(info.key_size); 697 value = alloc_value(&info); 698 if (!key || !value) { 699 p_err("mem alloc failed"); 700 err = -1; 701 goto exit_free; 702 } 703 704 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL); 705 if (err) 706 goto exit_free; 707 708 err = bpf_map_lookup_elem(fd, key, value); 709 if (!err) { 710 if (json_output) 711 print_entry_json(&info, key, value); 712 else 713 print_entry_plain(&info, key, value); 714 } else if (errno == ENOENT) { 715 if (json_output) { 716 jsonw_null(json_wtr); 717 } else { 718 printf("key:\n"); 719 fprint_hex(stdout, key, info.key_size, " "); 720 printf("\n\nNot found\n"); 721 } 722 } else { 723 p_err("lookup failed: %s", strerror(errno)); 724 } 725 726 exit_free: 727 free(key); 728 free(value); 729 close(fd); 730 731 return err; 732 } 733 734 static int do_getnext(int argc, char **argv) 735 { 736 struct bpf_map_info info = {}; 737 __u32 len = sizeof(info); 738 void *key, *nextkey; 739 int err; 740 int fd; 741 742 if (argc < 2) 743 usage(); 744 745 fd = map_parse_fd_and_info(&argc, &argv, &info, &len); 746 if (fd < 0) 747 return -1; 748 749 key = malloc(info.key_size); 750 nextkey = malloc(info.key_size); 751 if (!key || !nextkey) { 752 p_err("mem alloc failed"); 753 err = -1; 754 goto exit_free; 755 } 756 757 if (argc) { 758 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, 759 NULL, NULL); 760 if (err) 761 goto exit_free; 762 } else { 763 free(key); 764 key = NULL; 765 } 766 767 err = bpf_map_get_next_key(fd, key, nextkey); 768 if (err) { 769 p_err("can't get next key: %s", strerror(errno)); 770 goto exit_free; 771 } 772 773 if (json_output) { 774 jsonw_start_object(json_wtr); 775 if (key) { 776 jsonw_name(json_wtr, "key"); 777 print_hex_data_json(key, info.key_size); 778 } else { 779 jsonw_null_field(json_wtr, "key"); 780 } 781 jsonw_name(json_wtr, "next_key"); 782 print_hex_data_json(nextkey, info.key_size); 783 jsonw_end_object(json_wtr); 784 } else { 785 if (key) { 786 printf("key:\n"); 787 fprint_hex(stdout, key, info.key_size, " "); 788 printf("\n"); 789 } else { 790 printf("key: None\n"); 791 } 792 printf("next key:\n"); 793 fprint_hex(stdout, nextkey, info.key_size, " "); 794 printf("\n"); 795 } 796 797 exit_free: 798 free(nextkey); 799 free(key); 800 close(fd); 801 802 return err; 803 } 804 805 static int do_delete(int argc, char **argv) 806 { 807 struct bpf_map_info info = {}; 808 __u32 len = sizeof(info); 809 void *key; 810 int err; 811 int fd; 812 813 if (argc < 2) 814 usage(); 815 816 fd = map_parse_fd_and_info(&argc, &argv, &info, &len); 817 if (fd < 0) 818 return -1; 819 820 key = malloc(info.key_size); 821 if (!key) { 822 p_err("mem alloc failed"); 823 err = -1; 824 goto exit_free; 825 } 826 827 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL); 828 if (err) 829 goto exit_free; 830 831 err = bpf_map_delete_elem(fd, key); 832 if (err) 833 p_err("delete failed: %s", strerror(errno)); 834 835 exit_free: 836 free(key); 837 close(fd); 838 839 if (!err && json_output) 840 jsonw_null(json_wtr); 841 return err; 842 } 843 844 static int do_pin(int argc, char **argv) 845 { 846 int err; 847 848 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id); 849 if (!err && json_output) 850 jsonw_null(json_wtr); 851 return err; 852 } 853 854 static int do_help(int argc, char **argv) 855 { 856 if (json_output) { 857 jsonw_null(json_wtr); 858 return 0; 859 } 860 861 fprintf(stderr, 862 "Usage: %s %s show [MAP]\n" 863 " %s %s dump MAP\n" 864 " %s %s update MAP key BYTES value VALUE [UPDATE_FLAGS]\n" 865 " %s %s lookup MAP key BYTES\n" 866 " %s %s getnext MAP [key BYTES]\n" 867 " %s %s delete MAP key BYTES\n" 868 " %s %s pin MAP FILE\n" 869 " %s %s help\n" 870 "\n" 871 " MAP := { id MAP_ID | pinned FILE }\n" 872 " " HELP_SPEC_PROGRAM "\n" 873 " VALUE := { BYTES | MAP | PROG }\n" 874 " UPDATE_FLAGS := { any | exist | noexist }\n" 875 " " HELP_SPEC_OPTIONS "\n" 876 "", 877 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], 878 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], 879 bin_name, argv[-2], bin_name, argv[-2]); 880 881 return 0; 882 } 883 884 static const struct cmd cmds[] = { 885 { "show", do_show }, 886 { "help", do_help }, 887 { "dump", do_dump }, 888 { "update", do_update }, 889 { "lookup", do_lookup }, 890 { "getnext", do_getnext }, 891 { "delete", do_delete }, 892 { "pin", do_pin }, 893 { 0 } 894 }; 895 896 int do_map(int argc, char **argv) 897 { 898 return cmd_select(cmds, argc, argv, do_help); 899 } 900