1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
8 #include <linux/kernel.h>
15 #include <sys/types.h>
21 #include "json_writer.h"
24 const char * const map_type_name[] = {
25 [BPF_MAP_TYPE_UNSPEC] = "unspec",
26 [BPF_MAP_TYPE_HASH] = "hash",
27 [BPF_MAP_TYPE_ARRAY] = "array",
28 [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
29 [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
30 [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
31 [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
32 [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
33 [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
34 [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
35 [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
36 [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
37 [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
38 [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
39 [BPF_MAP_TYPE_DEVMAP] = "devmap",
40 [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
41 [BPF_MAP_TYPE_CPUMAP] = "cpumap",
42 [BPF_MAP_TYPE_XSKMAP] = "xskmap",
43 [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
44 [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage",
45 [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray",
46 [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage",
47 [BPF_MAP_TYPE_QUEUE] = "queue",
48 [BPF_MAP_TYPE_STACK] = "stack",
51 const size_t map_type_name_size = ARRAY_SIZE(map_type_name);
53 static bool map_is_per_cpu(__u32 type)
55 return type == BPF_MAP_TYPE_PERCPU_HASH ||
56 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
57 type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
58 type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE;
61 static bool map_is_map_of_maps(__u32 type)
63 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
64 type == BPF_MAP_TYPE_HASH_OF_MAPS;
67 static bool map_is_map_of_progs(__u32 type)
69 return type == BPF_MAP_TYPE_PROG_ARRAY;
72 static int map_type_from_str(const char *type)
76 for (i = 0; i < ARRAY_SIZE(map_type_name); i++)
77 /* Don't allow prefixing in case of possible future shadowing */
78 if (map_type_name[i] && !strcmp(map_type_name[i], type))
83 static void *alloc_value(struct bpf_map_info *info)
85 if (map_is_per_cpu(info->type))
86 return malloc(round_up(info->value_size, 8) *
89 return malloc(info->value_size);
92 int map_parse_fd(int *argc, char ***argv)
96 if (is_prefix(**argv, "id")) {
102 id = strtoul(**argv, &endptr, 0);
104 p_err("can't parse %s as ID", **argv);
109 fd = bpf_map_get_fd_by_id(id);
111 p_err("get map by id (%u): %s", id, strerror(errno));
113 } else if (is_prefix(**argv, "pinned")) {
121 return open_obj_pinned_any(path, BPF_OBJ_MAP);
124 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
128 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
133 fd = map_parse_fd(argc, argv);
137 err = bpf_obj_get_info_by_fd(fd, info, info_len);
139 p_err("can't get map info: %s", strerror(errno));
147 static int do_dump_btf(const struct btf_dumper *d,
148 struct bpf_map_info *map_info, void *key,
153 /* start of key-value pair */
154 jsonw_start_object(d->jw);
156 jsonw_name(d->jw, "key");
158 ret = btf_dumper_type(d, map_info->btf_key_type_id, key);
162 if (!map_is_per_cpu(map_info->type)) {
163 jsonw_name(d->jw, "value");
164 ret = btf_dumper_type(d, map_info->btf_value_type_id, value);
166 unsigned int i, n, step;
168 jsonw_name(d->jw, "values");
169 jsonw_start_array(d->jw);
170 n = get_possible_cpus();
171 step = round_up(map_info->value_size, 8);
172 for (i = 0; i < n; i++) {
173 jsonw_start_object(d->jw);
174 jsonw_int_field(d->jw, "cpu", i);
175 jsonw_name(d->jw, "value");
176 ret = btf_dumper_type(d, map_info->btf_value_type_id,
178 jsonw_end_object(d->jw);
182 jsonw_end_array(d->jw);
186 /* end of key-value pair */
187 jsonw_end_object(d->jw);
192 static json_writer_t *get_btf_writer(void)
194 json_writer_t *jw = jsonw_new(stdout);
198 jsonw_pretty(jw, true);
203 static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
204 unsigned char *value, struct btf *btf)
206 jsonw_start_object(json_wtr);
208 if (!map_is_per_cpu(info->type)) {
209 jsonw_name(json_wtr, "key");
210 print_hex_data_json(key, info->key_size);
211 jsonw_name(json_wtr, "value");
212 print_hex_data_json(value, info->value_size);
214 struct btf_dumper d = {
217 .is_plain_text = false,
220 jsonw_name(json_wtr, "formatted");
221 do_dump_btf(&d, info, key, value);
224 unsigned int i, n, step;
226 n = get_possible_cpus();
227 step = round_up(info->value_size, 8);
229 jsonw_name(json_wtr, "key");
230 print_hex_data_json(key, info->key_size);
232 jsonw_name(json_wtr, "values");
233 jsonw_start_array(json_wtr);
234 for (i = 0; i < n; i++) {
235 jsonw_start_object(json_wtr);
237 jsonw_int_field(json_wtr, "cpu", i);
239 jsonw_name(json_wtr, "value");
240 print_hex_data_json(value + i * step,
243 jsonw_end_object(json_wtr);
245 jsonw_end_array(json_wtr);
247 struct btf_dumper d = {
250 .is_plain_text = false,
253 jsonw_name(json_wtr, "formatted");
254 do_dump_btf(&d, info, key, value);
258 jsonw_end_object(json_wtr);
261 static void print_entry_error(struct bpf_map_info *info, unsigned char *key,
264 int value_size = strlen(value);
265 bool single_line, break_names;
267 break_names = info->key_size > 16 || value_size > 16;
268 single_line = info->key_size + value_size <= 24 && !break_names;
270 printf("key:%c", break_names ? '\n' : ' ');
271 fprint_hex(stdout, key, info->key_size, " ");
273 printf(single_line ? " " : "\n");
275 printf("value:%c%s", break_names ? '\n' : ' ', value);
280 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
281 unsigned char *value)
283 if (!map_is_per_cpu(info->type)) {
284 bool single_line, break_names;
286 break_names = info->key_size > 16 || info->value_size > 16;
287 single_line = info->key_size + info->value_size <= 24 &&
290 if (info->key_size) {
291 printf("key:%c", break_names ? '\n' : ' ');
292 fprint_hex(stdout, key, info->key_size, " ");
294 printf(single_line ? " " : "\n");
297 if (info->value_size) {
298 printf("value:%c", break_names ? '\n' : ' ');
300 fprint_hex(stdout, value, info->value_size,
303 printf("<no entry>");
308 unsigned int i, n, step;
310 n = get_possible_cpus();
311 step = round_up(info->value_size, 8);
313 if (info->key_size) {
315 fprint_hex(stdout, key, info->key_size, " ");
318 if (info->value_size) {
319 for (i = 0; i < n; i++) {
320 printf("value (CPU %02d):%c",
321 i, info->value_size > 16 ? '\n' : ' ');
323 fprint_hex(stdout, value + i * step,
324 info->value_size, " ");
326 printf("<no entry>");
333 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
336 unsigned int i = 0, base = 0;
339 if (is_prefix(*argv, "hex")) {
344 while (i < n && argv[i]) {
345 val[i] = strtoul(argv[i], &endptr, base);
347 p_err("error parsing byte: %s", argv[i]);
354 p_err("%s expected %d bytes got %d", name, n, i);
361 /* on per cpu maps we must copy the provided value on all value instances */
362 static void fill_per_cpu_value(struct bpf_map_info *info, void *value)
364 unsigned int i, n, step;
366 if (!map_is_per_cpu(info->type))
369 n = get_possible_cpus();
370 step = round_up(info->value_size, 8);
371 for (i = 1; i < n; i++)
372 memcpy(value + i * step, value, info->value_size);
375 static int parse_elem(char **argv, struct bpf_map_info *info,
376 void *key, void *value, __u32 key_size, __u32 value_size,
377 __u32 *flags, __u32 **value_fd)
382 p_err("did not find %s", key ? "key" : "value");
386 if (is_prefix(*argv, "key")) {
389 p_err("duplicate key");
391 p_err("unnecessary key");
395 argv = parse_bytes(argv + 1, "key", key, key_size);
399 return parse_elem(argv, info, NULL, value, key_size, value_size,
401 } else if (is_prefix(*argv, "value")) {
406 p_err("duplicate value");
408 p_err("unnecessary value");
414 if (map_is_map_of_maps(info->type)) {
417 if (value_size != 4) {
418 p_err("value smaller than 4B for map in map?");
421 if (!argv[0] || !argv[1]) {
422 p_err("not enough value arguments for map in map");
426 fd = map_parse_fd(&argc, &argv);
432 } else if (map_is_map_of_progs(info->type)) {
435 if (value_size != 4) {
436 p_err("value smaller than 4B for map of progs?");
439 if (!argv[0] || !argv[1]) {
440 p_err("not enough value arguments for map of progs");
443 if (is_prefix(*argv, "id"))
444 p_info("Warning: updating program array via MAP_ID, make sure this map is kept open\n"
445 " by some process or pinned otherwise update will be lost");
447 fd = prog_parse_fd(&argc, &argv);
454 argv = parse_bytes(argv, "value", value, value_size);
458 fill_per_cpu_value(info, value);
461 return parse_elem(argv, info, key, NULL, key_size, value_size,
463 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
464 is_prefix(*argv, "exist")) {
466 p_err("flags specified multiple times: %s", *argv);
470 if (is_prefix(*argv, "any"))
472 else if (is_prefix(*argv, "noexist"))
473 *flags = BPF_NOEXIST;
474 else if (is_prefix(*argv, "exist"))
477 return parse_elem(argv + 1, info, key, value, key_size,
478 value_size, NULL, value_fd);
481 p_err("expected key or value, got: %s", *argv);
485 static int show_map_close_json(int fd, struct bpf_map_info *info)
489 memlock = get_fdinfo(fd, "memlock");
491 jsonw_start_object(json_wtr);
493 jsonw_uint_field(json_wtr, "id", info->id);
494 if (info->type < ARRAY_SIZE(map_type_name))
495 jsonw_string_field(json_wtr, "type",
496 map_type_name[info->type]);
498 jsonw_uint_field(json_wtr, "type", info->type);
501 jsonw_string_field(json_wtr, "name", info->name);
503 jsonw_name(json_wtr, "flags");
504 jsonw_printf(json_wtr, "%d", info->map_flags);
506 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
508 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
509 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
510 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
513 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
516 if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
517 char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
518 char *owner_jited = get_fdinfo(fd, "owner_jited");
520 if (owner_prog_type) {
521 unsigned int prog_type = atoi(owner_prog_type);
523 if (prog_type < ARRAY_SIZE(prog_type_name))
524 jsonw_string_field(json_wtr, "owner_prog_type",
525 prog_type_name[prog_type]);
527 jsonw_uint_field(json_wtr, "owner_prog_type",
531 jsonw_bool_field(json_wtr, "owner_jited",
532 !!atoi(owner_jited));
534 free(owner_prog_type);
539 if (!hash_empty(map_table.table)) {
540 struct pinned_obj *obj;
542 jsonw_name(json_wtr, "pinned");
543 jsonw_start_array(json_wtr);
544 hash_for_each_possible(map_table.table, obj, hash, info->id) {
545 if (obj->id == info->id)
546 jsonw_string(json_wtr, obj->path);
548 jsonw_end_array(json_wtr);
551 jsonw_end_object(json_wtr);
556 static int show_map_close_plain(int fd, struct bpf_map_info *info)
560 memlock = get_fdinfo(fd, "memlock");
562 printf("%u: ", info->id);
563 if (info->type < ARRAY_SIZE(map_type_name))
564 printf("%s ", map_type_name[info->type]);
566 printf("type %u ", info->type);
569 printf("name %s ", info->name);
571 printf("flags 0x%x", info->map_flags);
572 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
574 printf("\tkey %uB value %uB max_entries %u",
575 info->key_size, info->value_size, info->max_entries);
578 printf(" memlock %sB", memlock);
581 if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
582 char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
583 char *owner_jited = get_fdinfo(fd, "owner_jited");
585 if (owner_prog_type || owner_jited)
587 if (owner_prog_type) {
588 unsigned int prog_type = atoi(owner_prog_type);
590 if (prog_type < ARRAY_SIZE(prog_type_name))
591 printf("owner_prog_type %s ",
592 prog_type_name[prog_type]);
594 printf("owner_prog_type %d ", prog_type);
597 printf("owner%s jited",
598 atoi(owner_jited) ? "" : " not");
600 free(owner_prog_type);
606 if (!hash_empty(map_table.table)) {
607 struct pinned_obj *obj;
609 hash_for_each_possible(map_table.table, obj, hash, info->id) {
610 if (obj->id == info->id)
611 printf("\tpinned %s\n", obj->path);
617 static int do_show(int argc, char **argv)
619 struct bpf_map_info info = {};
620 __u32 len = sizeof(info);
626 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
629 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
634 return show_map_close_json(fd, &info);
636 return show_map_close_plain(fd, &info);
643 jsonw_start_array(json_wtr);
645 err = bpf_map_get_next_id(id, &id);
649 p_err("can't get next map: %s%s", strerror(errno),
650 errno == EINVAL ? " -- kernel too old?" : "");
654 fd = bpf_map_get_fd_by_id(id);
658 p_err("can't get map by id (%u): %s",
659 id, strerror(errno));
663 err = bpf_obj_get_info_by_fd(fd, &info, &len);
665 p_err("can't get map info: %s", strerror(errno));
671 show_map_close_json(fd, &info);
673 show_map_close_plain(fd, &info);
676 jsonw_end_array(json_wtr);
678 return errno == ENOENT ? 0 : -1;
681 static int dump_map_elem(int fd, void *key, void *value,
682 struct bpf_map_info *map_info, struct btf *btf,
683 json_writer_t *btf_wtr)
688 if (!bpf_map_lookup_elem(fd, key, value)) {
690 print_entry_json(map_info, key, value, btf);
693 struct btf_dumper d = {
696 .is_plain_text = true,
699 do_dump_btf(&d, map_info, key, value);
701 print_entry_plain(map_info, key, value);
708 /* lookup error handling */
709 lookup_errno = errno;
711 if (map_is_map_of_maps(map_info->type) ||
712 map_is_map_of_progs(map_info->type))
716 jsonw_name(json_wtr, "key");
717 print_hex_data_json(key, map_info->key_size);
718 jsonw_name(json_wtr, "value");
719 jsonw_start_object(json_wtr);
720 jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
721 jsonw_end_object(json_wtr);
724 print_entry_plain(map_info, key, NULL);
726 print_entry_error(map_info, key,
727 strerror(lookup_errno));
733 static int do_dump(int argc, char **argv)
735 struct bpf_map_info info = {};
736 void *key, *value, *prev_key;
737 unsigned int num_elems = 0;
738 __u32 len = sizeof(info);
739 json_writer_t *btf_wtr;
740 struct btf *btf = NULL;
747 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
751 key = malloc(info.key_size);
752 value = alloc_value(&info);
753 if (!key || !value) {
754 p_err("mem alloc failed");
761 err = btf__get_from_id(info.btf_id, &btf);
763 p_err("failed to get btf");
768 jsonw_start_array(json_wtr);
771 btf_wtr = get_btf_writer();
773 p_info("failed to create json writer for btf. falling back to plain output");
777 jsonw_start_array(btf_wtr);
782 err = bpf_map_get_next_key(fd, prev_key, key);
788 num_elems += dump_map_elem(fd, key, value, &info, btf, btf_wtr);
793 jsonw_end_array(json_wtr);
795 jsonw_end_array(btf_wtr);
796 jsonw_destroy(&btf_wtr);
798 printf("Found %u element%s\n", num_elems,
799 num_elems != 1 ? "s" : "");
811 static int alloc_key_value(struct bpf_map_info *info, void **key, void **value)
816 if (info->key_size) {
817 *key = malloc(info->key_size);
819 p_err("key mem alloc failed");
824 if (info->value_size) {
825 *value = alloc_value(info);
827 p_err("value mem alloc failed");
837 static int do_update(int argc, char **argv)
839 struct bpf_map_info info = {};
840 __u32 len = sizeof(info);
841 __u32 *value_fd = NULL;
842 __u32 flags = BPF_ANY;
849 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
853 err = alloc_key_value(&info, &key, &value);
857 err = parse_elem(argv, &info, key, value, info.key_size,
858 info.value_size, &flags, &value_fd);
862 err = bpf_map_update_elem(fd, key, value, flags);
864 p_err("update failed: %s", strerror(errno));
875 if (!err && json_output)
876 jsonw_null(json_wtr);
880 static void print_key_value(struct bpf_map_info *info, void *key,
883 json_writer_t *btf_wtr;
884 struct btf *btf = NULL;
887 err = btf__get_from_id(info->btf_id, &btf);
889 p_err("failed to get btf");
894 print_entry_json(info, key, value, btf);
896 /* if here json_wtr wouldn't have been initialised,
897 * so let's create separate writer for btf
899 btf_wtr = get_btf_writer();
901 p_info("failed to create json writer for btf. falling back to plain output");
904 print_entry_plain(info, key, value);
906 struct btf_dumper d = {
909 .is_plain_text = true,
912 do_dump_btf(&d, info, key, value);
913 jsonw_destroy(&btf_wtr);
916 print_entry_plain(info, key, value);
921 static int do_lookup(int argc, char **argv)
923 struct bpf_map_info info = {};
924 __u32 len = sizeof(info);
932 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
936 err = alloc_key_value(&info, &key, &value);
940 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
944 err = bpf_map_lookup_elem(fd, key, value);
946 if (errno == ENOENT) {
948 jsonw_null(json_wtr);
951 fprint_hex(stdout, key, info.key_size, " ");
952 printf("\n\nNot found\n");
955 p_err("lookup failed: %s", strerror(errno));
961 /* here means bpf_map_lookup_elem() succeeded */
962 print_key_value(&info, key, value);
972 static int do_getnext(int argc, char **argv)
974 struct bpf_map_info info = {};
975 __u32 len = sizeof(info);
983 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
987 key = malloc(info.key_size);
988 nextkey = malloc(info.key_size);
989 if (!key || !nextkey) {
990 p_err("mem alloc failed");
996 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
1005 err = bpf_map_get_next_key(fd, key, nextkey);
1007 p_err("can't get next key: %s", strerror(errno));
1012 jsonw_start_object(json_wtr);
1014 jsonw_name(json_wtr, "key");
1015 print_hex_data_json(key, info.key_size);
1017 jsonw_null_field(json_wtr, "key");
1019 jsonw_name(json_wtr, "next_key");
1020 print_hex_data_json(nextkey, info.key_size);
1021 jsonw_end_object(json_wtr);
1025 fprint_hex(stdout, key, info.key_size, " ");
1028 printf("key: None\n");
1030 printf("next key:\n");
1031 fprint_hex(stdout, nextkey, info.key_size, " ");
1043 static int do_delete(int argc, char **argv)
1045 struct bpf_map_info info = {};
1046 __u32 len = sizeof(info);
1054 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1058 key = malloc(info.key_size);
1060 p_err("mem alloc failed");
1065 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
1069 err = bpf_map_delete_elem(fd, key);
1071 p_err("delete failed: %s", strerror(errno));
1077 if (!err && json_output)
1078 jsonw_null(json_wtr);
1082 static int do_pin(int argc, char **argv)
1086 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
1087 if (!err && json_output)
1088 jsonw_null(json_wtr);
1092 static int do_create(int argc, char **argv)
1094 struct bpf_create_map_attr attr = { NULL, };
1095 const char *pinfile;
1100 pinfile = GET_ARG();
1106 if (is_prefix(*argv, "type")) {
1109 if (attr.map_type) {
1110 p_err("map type already specified");
1114 attr.map_type = map_type_from_str(*argv);
1115 if ((int)attr.map_type < 0) {
1116 p_err("unrecognized map type: %s", *argv);
1120 } else if (is_prefix(*argv, "name")) {
1122 attr.name = GET_ARG();
1123 } else if (is_prefix(*argv, "key")) {
1124 if (parse_u32_arg(&argc, &argv, &attr.key_size,
1127 } else if (is_prefix(*argv, "value")) {
1128 if (parse_u32_arg(&argc, &argv, &attr.value_size,
1131 } else if (is_prefix(*argv, "entries")) {
1132 if (parse_u32_arg(&argc, &argv, &attr.max_entries,
1135 } else if (is_prefix(*argv, "flags")) {
1136 if (parse_u32_arg(&argc, &argv, &attr.map_flags,
1139 } else if (is_prefix(*argv, "dev")) {
1142 if (attr.map_ifindex) {
1143 p_err("offload device already specified");
1147 attr.map_ifindex = if_nametoindex(*argv);
1148 if (!attr.map_ifindex) {
1149 p_err("unrecognized netdevice '%s': %s",
1150 *argv, strerror(errno));
1155 p_err("unknown arg %s", *argv);
1161 p_err("map name not specified");
1167 fd = bpf_create_map_xattr(&attr);
1169 p_err("map create failed: %s", strerror(errno));
1173 err = do_pin_fd(fd, pinfile);
1179 jsonw_null(json_wtr);
1183 static int do_pop_dequeue(int argc, char **argv)
1185 struct bpf_map_info info = {};
1186 __u32 len = sizeof(info);
1194 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1198 err = alloc_key_value(&info, &key, &value);
1202 err = bpf_map_lookup_and_delete_elem(fd, key, value);
1204 if (errno == ENOENT) {
1206 jsonw_null(json_wtr);
1208 printf("Error: empty map\n");
1210 p_err("pop failed: %s", strerror(errno));
1216 print_key_value(&info, key, value);
1226 static int do_help(int argc, char **argv)
1229 jsonw_null(json_wtr);
1234 "Usage: %s %s { show | list } [MAP]\n"
1235 " %s %s create FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n"
1236 " entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
1239 " %s %s update MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
1240 " %s %s lookup MAP [key DATA]\n"
1241 " %s %s getnext MAP [key DATA]\n"
1242 " %s %s delete MAP key DATA\n"
1243 " %s %s pin MAP FILE\n"
1244 " %s %s event_pipe MAP [cpu N index M]\n"
1246 " %s %s push MAP value VALUE\n"
1248 " %s %s enqueue MAP value VALUE\n"
1249 " %s %s dequeue MAP\n"
1252 " " HELP_SPEC_MAP "\n"
1253 " DATA := { [hex] BYTES }\n"
1254 " " HELP_SPEC_PROGRAM "\n"
1255 " VALUE := { DATA | MAP | PROG }\n"
1256 " UPDATE_FLAGS := { any | exist | noexist }\n"
1257 " TYPE := { hash | array | prog_array | perf_event_array | percpu_hash |\n"
1258 " percpu_array | stack_trace | cgroup_array | lru_hash |\n"
1259 " lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n"
1260 " devmap | sockmap | cpumap | xskmap | sockhash |\n"
1261 " cgroup_storage | reuseport_sockarray | percpu_cgroup_storage }\n"
1262 " " HELP_SPEC_OPTIONS "\n"
1264 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1265 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1266 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1267 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1268 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
1273 static const struct cmd cmds[] = {
1274 { "show", do_show },
1275 { "list", do_show },
1276 { "help", do_help },
1277 { "dump", do_dump },
1278 { "update", do_update },
1279 { "lookup", do_lookup },
1280 { "getnext", do_getnext },
1281 { "delete", do_delete },
1283 { "event_pipe", do_event_pipe },
1284 { "create", do_create },
1285 { "peek", do_lookup },
1286 { "push", do_update },
1287 { "enqueue", do_update },
1288 { "pop", do_pop_dequeue },
1289 { "dequeue", do_pop_dequeue },
1293 int do_map(int argc, char **argv)
1295 return cmd_select(cmds, argc, argv, do_help);