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>
20 #include <bpf/hashmap.h>
22 #include "json_writer.h"
25 static struct hashmap *map_table;
27 static bool map_is_per_cpu(__u32 type)
29 return type == BPF_MAP_TYPE_PERCPU_HASH ||
30 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
31 type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
32 type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE;
35 static bool map_is_map_of_maps(__u32 type)
37 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
38 type == BPF_MAP_TYPE_HASH_OF_MAPS;
41 static bool map_is_map_of_progs(__u32 type)
43 return type == BPF_MAP_TYPE_PROG_ARRAY;
46 static int map_type_from_str(const char *type)
48 const char *map_type_str;
52 map_type_str = libbpf_bpf_map_type_str(i);
56 /* Don't allow prefixing in case of possible future shadowing */
57 if (!strcmp(map_type_str, type))
63 static void *alloc_value(struct bpf_map_info *info)
65 if (map_is_per_cpu(info->type))
66 return malloc(round_up(info->value_size, 8) *
69 return malloc(info->value_size);
72 static int do_dump_btf(const struct btf_dumper *d,
73 struct bpf_map_info *map_info, void *key,
79 /* start of key-value pair */
80 jsonw_start_object(d->jw);
82 if (map_info->btf_key_type_id) {
83 jsonw_name(d->jw, "key");
85 ret = btf_dumper_type(d, map_info->btf_key_type_id, key);
90 value_id = map_info->btf_vmlinux_value_type_id ?
91 : map_info->btf_value_type_id;
93 if (!map_is_per_cpu(map_info->type)) {
94 jsonw_name(d->jw, "value");
95 ret = btf_dumper_type(d, value_id, value);
97 unsigned int i, n, step;
99 jsonw_name(d->jw, "values");
100 jsonw_start_array(d->jw);
101 n = get_possible_cpus();
102 step = round_up(map_info->value_size, 8);
103 for (i = 0; i < n; i++) {
104 jsonw_start_object(d->jw);
105 jsonw_int_field(d->jw, "cpu", i);
106 jsonw_name(d->jw, "value");
107 ret = btf_dumper_type(d, value_id, value + i * step);
108 jsonw_end_object(d->jw);
112 jsonw_end_array(d->jw);
116 /* end of key-value pair */
117 jsonw_end_object(d->jw);
122 static json_writer_t *get_btf_writer(void)
124 json_writer_t *jw = jsonw_new(stdout);
128 jsonw_pretty(jw, true);
133 static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
134 unsigned char *value, struct btf *btf)
136 jsonw_start_object(json_wtr);
138 if (!map_is_per_cpu(info->type)) {
139 jsonw_name(json_wtr, "key");
140 print_hex_data_json(key, info->key_size);
141 jsonw_name(json_wtr, "value");
142 print_hex_data_json(value, info->value_size);
144 struct btf_dumper d = {
147 .is_plain_text = false,
150 jsonw_name(json_wtr, "formatted");
151 do_dump_btf(&d, info, key, value);
154 unsigned int i, n, step;
156 n = get_possible_cpus();
157 step = round_up(info->value_size, 8);
159 jsonw_name(json_wtr, "key");
160 print_hex_data_json(key, info->key_size);
162 jsonw_name(json_wtr, "values");
163 jsonw_start_array(json_wtr);
164 for (i = 0; i < n; i++) {
165 jsonw_start_object(json_wtr);
167 jsonw_int_field(json_wtr, "cpu", i);
169 jsonw_name(json_wtr, "value");
170 print_hex_data_json(value + i * step,
173 jsonw_end_object(json_wtr);
175 jsonw_end_array(json_wtr);
177 struct btf_dumper d = {
180 .is_plain_text = false,
183 jsonw_name(json_wtr, "formatted");
184 do_dump_btf(&d, info, key, value);
188 jsonw_end_object(json_wtr);
192 print_entry_error_msg(struct bpf_map_info *info, unsigned char *key,
193 const char *error_msg)
195 int msg_size = strlen(error_msg);
196 bool single_line, break_names;
198 break_names = info->key_size > 16 || msg_size > 16;
199 single_line = info->key_size + msg_size <= 24 && !break_names;
201 printf("key:%c", break_names ? '\n' : ' ');
202 fprint_hex(stdout, key, info->key_size, " ");
204 printf(single_line ? " " : "\n");
206 printf("value:%c%s", break_names ? '\n' : ' ', error_msg);
212 print_entry_error(struct bpf_map_info *map_info, void *key, int lookup_errno)
214 /* For prog_array maps or arrays of maps, failure to lookup the value
215 * means there is no entry for that key. Do not print an error message
218 if ((map_is_map_of_maps(map_info->type) ||
219 map_is_map_of_progs(map_info->type)) && lookup_errno == ENOENT)
223 jsonw_start_object(json_wtr); /* entry */
224 jsonw_name(json_wtr, "key");
225 print_hex_data_json(key, map_info->key_size);
226 jsonw_name(json_wtr, "value");
227 jsonw_start_object(json_wtr); /* error */
228 jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
229 jsonw_end_object(json_wtr); /* error */
230 jsonw_end_object(json_wtr); /* entry */
232 const char *msg = NULL;
234 if (lookup_errno == ENOENT)
236 else if (lookup_errno == ENOSPC &&
237 map_info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
238 msg = "<cannot read>";
240 print_entry_error_msg(map_info, key,
241 msg ? : strerror(lookup_errno));
245 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
246 unsigned char *value)
248 if (!map_is_per_cpu(info->type)) {
249 bool single_line, break_names;
251 break_names = info->key_size > 16 || info->value_size > 16;
252 single_line = info->key_size + info->value_size <= 24 &&
255 if (info->key_size) {
256 printf("key:%c", break_names ? '\n' : ' ');
257 fprint_hex(stdout, key, info->key_size, " ");
259 printf(single_line ? " " : "\n");
262 if (info->value_size) {
263 printf("value:%c", break_names ? '\n' : ' ');
264 fprint_hex(stdout, value, info->value_size, " ");
269 unsigned int i, n, step;
271 n = get_possible_cpus();
272 step = round_up(info->value_size, 8);
274 if (info->key_size) {
276 fprint_hex(stdout, key, info->key_size, " ");
279 if (info->value_size) {
280 for (i = 0; i < n; i++) {
281 printf("value (CPU %02d):%c",
282 i, info->value_size > 16 ? '\n' : ' ');
283 fprint_hex(stdout, value + i * step,
284 info->value_size, " ");
291 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
294 unsigned int i = 0, base = 0;
297 if (is_prefix(*argv, "hex")) {
302 while (i < n && argv[i]) {
303 val[i] = strtoul(argv[i], &endptr, base);
305 p_err("error parsing byte: %s", argv[i]);
312 p_err("%s expected %d bytes got %d", name, n, i);
319 /* on per cpu maps we must copy the provided value on all value instances */
320 static void fill_per_cpu_value(struct bpf_map_info *info, void *value)
322 unsigned int i, n, step;
324 if (!map_is_per_cpu(info->type))
327 n = get_possible_cpus();
328 step = round_up(info->value_size, 8);
329 for (i = 1; i < n; i++)
330 memcpy(value + i * step, value, info->value_size);
333 static int parse_elem(char **argv, struct bpf_map_info *info,
334 void *key, void *value, __u32 key_size, __u32 value_size,
335 __u32 *flags, __u32 **value_fd)
340 p_err("did not find %s", key ? "key" : "value");
344 if (is_prefix(*argv, "key")) {
347 p_err("duplicate key");
349 p_err("unnecessary key");
353 argv = parse_bytes(argv + 1, "key", key, key_size);
357 return parse_elem(argv, info, NULL, value, key_size, value_size,
359 } else if (is_prefix(*argv, "value")) {
364 p_err("duplicate value");
366 p_err("unnecessary value");
372 if (map_is_map_of_maps(info->type)) {
375 if (value_size != 4) {
376 p_err("value smaller than 4B for map in map?");
379 if (!argv[0] || !argv[1]) {
380 p_err("not enough value arguments for map in map");
384 fd = map_parse_fd(&argc, &argv);
390 } else if (map_is_map_of_progs(info->type)) {
393 if (value_size != 4) {
394 p_err("value smaller than 4B for map of progs?");
397 if (!argv[0] || !argv[1]) {
398 p_err("not enough value arguments for map of progs");
401 if (is_prefix(*argv, "id"))
402 p_info("Warning: updating program array via MAP_ID, make sure this map is kept open\n"
403 " by some process or pinned otherwise update will be lost");
405 fd = prog_parse_fd(&argc, &argv);
412 argv = parse_bytes(argv, "value", value, value_size);
416 fill_per_cpu_value(info, value);
419 return parse_elem(argv, info, key, NULL, key_size, value_size,
421 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
422 is_prefix(*argv, "exist")) {
424 p_err("flags specified multiple times: %s", *argv);
428 if (is_prefix(*argv, "any"))
430 else if (is_prefix(*argv, "noexist"))
431 *flags = BPF_NOEXIST;
432 else if (is_prefix(*argv, "exist"))
435 return parse_elem(argv + 1, info, key, value, key_size,
436 value_size, NULL, value_fd);
439 p_err("expected key or value, got: %s", *argv);
443 static void show_map_header_json(struct bpf_map_info *info, json_writer_t *wtr)
445 const char *map_type_str;
447 jsonw_uint_field(wtr, "id", info->id);
448 map_type_str = libbpf_bpf_map_type_str(info->type);
450 jsonw_string_field(wtr, "type", map_type_str);
452 jsonw_uint_field(wtr, "type", info->type);
455 jsonw_string_field(wtr, "name", info->name);
457 jsonw_name(wtr, "flags");
458 jsonw_printf(wtr, "%d", info->map_flags);
461 static int show_map_close_json(int fd, struct bpf_map_info *info)
463 char *memlock, *frozen_str;
466 memlock = get_fdinfo(fd, "memlock");
467 frozen_str = get_fdinfo(fd, "frozen");
469 jsonw_start_object(json_wtr);
471 show_map_header_json(info, json_wtr);
473 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
475 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
476 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
477 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
480 jsonw_int_field(json_wtr, "bytes_memlock", atoll(memlock));
483 if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
484 char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
485 char *owner_jited = get_fdinfo(fd, "owner_jited");
487 if (owner_prog_type) {
488 unsigned int prog_type = atoi(owner_prog_type);
489 const char *prog_type_str;
491 prog_type_str = libbpf_bpf_prog_type_str(prog_type);
493 jsonw_string_field(json_wtr, "owner_prog_type",
496 jsonw_uint_field(json_wtr, "owner_prog_type",
500 jsonw_bool_field(json_wtr, "owner_jited",
501 !!atoi(owner_jited));
503 free(owner_prog_type);
509 frozen = atoi(frozen_str);
512 jsonw_int_field(json_wtr, "frozen", frozen);
515 jsonw_int_field(json_wtr, "btf_id", info->btf_id);
517 if (!hashmap__empty(map_table)) {
518 struct hashmap_entry *entry;
520 jsonw_name(json_wtr, "pinned");
521 jsonw_start_array(json_wtr);
522 hashmap__for_each_key_entry(map_table, entry,
523 u32_as_hash_field(info->id))
524 jsonw_string(json_wtr, entry->value);
525 jsonw_end_array(json_wtr);
528 emit_obj_refs_json(refs_table, info->id, json_wtr);
530 jsonw_end_object(json_wtr);
535 static void show_map_header_plain(struct bpf_map_info *info)
537 const char *map_type_str;
539 printf("%u: ", info->id);
541 map_type_str = libbpf_bpf_map_type_str(info->type);
543 printf("%s ", map_type_str);
545 printf("type %u ", info->type);
548 printf("name %s ", info->name);
550 printf("flags 0x%x", info->map_flags);
551 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
555 static int show_map_close_plain(int fd, struct bpf_map_info *info)
557 char *memlock, *frozen_str;
560 memlock = get_fdinfo(fd, "memlock");
561 frozen_str = get_fdinfo(fd, "frozen");
563 show_map_header_plain(info);
564 printf("\tkey %uB value %uB max_entries %u",
565 info->key_size, info->value_size, info->max_entries);
568 printf(" memlock %sB", memlock);
571 if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
572 char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
573 char *owner_jited = get_fdinfo(fd, "owner_jited");
575 if (owner_prog_type || owner_jited)
577 if (owner_prog_type) {
578 unsigned int prog_type = atoi(owner_prog_type);
579 const char *prog_type_str;
581 prog_type_str = libbpf_bpf_prog_type_str(prog_type);
583 printf("owner_prog_type %s ", prog_type_str);
585 printf("owner_prog_type %d ", prog_type);
588 printf("owner%s jited",
589 atoi(owner_jited) ? "" : " not");
591 free(owner_prog_type);
596 if (!hashmap__empty(map_table)) {
597 struct hashmap_entry *entry;
599 hashmap__for_each_key_entry(map_table, entry,
600 u32_as_hash_field(info->id))
601 printf("\n\tpinned %s", (char *)entry->value);
605 frozen = atoi(frozen_str);
609 if (info->btf_id || frozen)
613 printf("btf_id %d", info->btf_id);
616 printf("%sfrozen", info->btf_id ? " " : "");
618 emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
624 static int do_show_subset(int argc, char **argv)
626 struct bpf_map_info info = {};
627 __u32 len = sizeof(info);
632 fds = malloc(sizeof(int));
634 p_err("mem alloc failed");
637 nb_fds = map_parse_fds(&argc, &argv, &fds);
641 if (json_output && nb_fds > 1)
642 jsonw_start_array(json_wtr); /* root array */
643 for (i = 0; i < nb_fds; i++) {
644 err = bpf_obj_get_info_by_fd(fds[i], &info, &len);
646 p_err("can't get map info: %s",
648 for (; i < nb_fds; i++)
654 show_map_close_json(fds[i], &info);
656 show_map_close_plain(fds[i], &info);
660 if (json_output && nb_fds > 1)
661 jsonw_end_array(json_wtr); /* root array */
668 static int do_show(int argc, char **argv)
670 struct bpf_map_info info = {};
671 __u32 len = sizeof(info);
677 map_table = hashmap__new(hash_fn_for_key_as_id,
678 equal_fn_for_key_as_id, NULL);
679 if (IS_ERR(map_table)) {
680 p_err("failed to create hashmap for pinned paths");
683 build_pinned_obj_table(map_table, BPF_OBJ_MAP);
685 build_obj_refs_table(&refs_table, BPF_OBJ_MAP);
688 return do_show_subset(argc, argv);
694 jsonw_start_array(json_wtr);
696 err = bpf_map_get_next_id(id, &id);
700 p_err("can't get next map: %s%s", strerror(errno),
701 errno == EINVAL ? " -- kernel too old?" : "");
705 fd = bpf_map_get_fd_by_id(id);
709 p_err("can't get map by id (%u): %s",
710 id, strerror(errno));
714 err = bpf_obj_get_info_by_fd(fd, &info, &len);
716 p_err("can't get map info: %s", strerror(errno));
722 show_map_close_json(fd, &info);
724 show_map_close_plain(fd, &info);
727 jsonw_end_array(json_wtr);
729 delete_obj_refs_table(refs_table);
732 delete_pinned_obj_table(map_table);
734 return errno == ENOENT ? 0 : -1;
737 static int dump_map_elem(int fd, void *key, void *value,
738 struct bpf_map_info *map_info, struct btf *btf,
739 json_writer_t *btf_wtr)
741 if (bpf_map_lookup_elem(fd, key, value)) {
742 print_entry_error(map_info, key, errno);
747 print_entry_json(map_info, key, value, btf);
749 struct btf_dumper d = {
752 .is_plain_text = true,
755 do_dump_btf(&d, map_info, key, value);
757 print_entry_plain(map_info, key, value);
763 static int maps_have_btf(int *fds, int nb_fds)
765 struct bpf_map_info info = {};
766 __u32 len = sizeof(info);
769 for (i = 0; i < nb_fds; i++) {
770 err = bpf_obj_get_info_by_fd(fds[i], &info, &len);
772 p_err("can't get map info: %s", strerror(errno));
783 static struct btf *btf_vmlinux;
785 static int get_map_kv_btf(const struct bpf_map_info *info, struct btf **btf)
789 if (info->btf_vmlinux_value_type_id) {
791 btf_vmlinux = libbpf_find_kernel_btf();
792 err = libbpf_get_error(btf_vmlinux);
794 p_err("failed to get kernel btf");
799 } else if (info->btf_value_type_id) {
800 *btf = btf__load_from_kernel_by_id(info->btf_id);
801 err = libbpf_get_error(*btf);
803 p_err("failed to get btf");
811 static void free_map_kv_btf(struct btf *btf)
813 if (!libbpf_get_error(btf) && btf != btf_vmlinux)
817 static void free_btf_vmlinux(void)
819 if (!libbpf_get_error(btf_vmlinux))
820 btf__free(btf_vmlinux);
824 map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
827 void *key, *value, *prev_key;
828 unsigned int num_elems = 0;
829 struct btf *btf = NULL;
832 key = malloc(info->key_size);
833 value = alloc_value(info);
834 if (!key || !value) {
835 p_err("mem alloc failed");
843 err = get_map_kv_btf(info, &btf);
849 jsonw_start_object(wtr); /* map object */
850 show_map_header_json(info, wtr);
851 jsonw_name(wtr, "elements");
853 jsonw_start_array(wtr); /* elements */
854 } else if (show_header) {
855 show_map_header_plain(info);
858 if (info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY &&
859 info->value_size != 8) {
860 const char *map_type_str;
862 map_type_str = libbpf_bpf_map_type_str(info->type);
863 p_info("Warning: cannot read values from %s map with value_size != 8",
867 err = bpf_map_get_next_key(fd, prev_key, key);
873 if (!dump_map_elem(fd, key, value, info, btf, wtr))
879 jsonw_end_array(wtr); /* elements */
881 jsonw_end_object(wtr); /* map object */
883 printf("Found %u element%s\n", num_elems,
884 num_elems != 1 ? "s" : "");
891 free_map_kv_btf(btf);
896 static int do_dump(int argc, char **argv)
898 json_writer_t *wtr = NULL, *btf_wtr = NULL;
899 struct bpf_map_info info = {};
901 __u32 len = sizeof(info);
908 fds = malloc(sizeof(int));
910 p_err("mem alloc failed");
913 nb_fds = map_parse_fds(&argc, &argv, &fds);
922 do_plain_btf = maps_have_btf(fds, nb_fds);
923 if (do_plain_btf < 0)
927 btf_wtr = get_btf_writer();
930 p_info("failed to create json writer for btf. falling back to plain output");
934 if (wtr && nb_fds > 1)
935 jsonw_start_array(wtr); /* root array */
936 for (i = 0; i < nb_fds; i++) {
937 if (bpf_obj_get_info_by_fd(fds[i], &info, &len)) {
938 p_err("can't get map info: %s", strerror(errno));
941 err = map_dump(fds[i], &info, wtr, nb_fds > 1);
942 if (!wtr && i != nb_fds - 1)
949 if (wtr && nb_fds > 1)
950 jsonw_end_array(wtr); /* root array */
953 jsonw_destroy(&btf_wtr);
955 for (; i < nb_fds; i++)
963 static int alloc_key_value(struct bpf_map_info *info, void **key, void **value)
968 if (info->key_size) {
969 *key = malloc(info->key_size);
971 p_err("key mem alloc failed");
976 if (info->value_size) {
977 *value = alloc_value(info);
979 p_err("value mem alloc failed");
989 static int do_update(int argc, char **argv)
991 struct bpf_map_info info = {};
992 __u32 len = sizeof(info);
993 __u32 *value_fd = NULL;
994 __u32 flags = BPF_ANY;
1001 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1005 err = alloc_key_value(&info, &key, &value);
1009 err = parse_elem(argv, &info, key, value, info.key_size,
1010 info.value_size, &flags, &value_fd);
1014 err = bpf_map_update_elem(fd, key, value, flags);
1016 p_err("update failed: %s", strerror(errno));
1027 if (!err && json_output)
1028 jsonw_null(json_wtr);
1032 static void print_key_value(struct bpf_map_info *info, void *key,
1035 json_writer_t *btf_wtr;
1038 if (get_map_kv_btf(info, &btf))
1042 print_entry_json(info, key, value, btf);
1044 /* if here json_wtr wouldn't have been initialised,
1045 * so let's create separate writer for btf
1047 btf_wtr = get_btf_writer();
1049 p_info("failed to create json writer for btf. falling back to plain output");
1052 print_entry_plain(info, key, value);
1054 struct btf_dumper d = {
1057 .is_plain_text = true,
1060 do_dump_btf(&d, info, key, value);
1061 jsonw_destroy(&btf_wtr);
1064 print_entry_plain(info, key, value);
1069 static int do_lookup(int argc, char **argv)
1071 struct bpf_map_info info = {};
1072 __u32 len = sizeof(info);
1080 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1084 err = alloc_key_value(&info, &key, &value);
1088 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
1092 err = bpf_map_lookup_elem(fd, key, value);
1094 if (errno == ENOENT) {
1096 jsonw_null(json_wtr);
1099 fprint_hex(stdout, key, info.key_size, " ");
1100 printf("\n\nNot found\n");
1103 p_err("lookup failed: %s", strerror(errno));
1109 /* here means bpf_map_lookup_elem() succeeded */
1110 print_key_value(&info, key, value);
1120 static int do_getnext(int argc, char **argv)
1122 struct bpf_map_info info = {};
1123 __u32 len = sizeof(info);
1124 void *key, *nextkey;
1131 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1135 key = malloc(info.key_size);
1136 nextkey = malloc(info.key_size);
1137 if (!key || !nextkey) {
1138 p_err("mem alloc failed");
1144 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
1153 err = bpf_map_get_next_key(fd, key, nextkey);
1155 p_err("can't get next key: %s", strerror(errno));
1160 jsonw_start_object(json_wtr);
1162 jsonw_name(json_wtr, "key");
1163 print_hex_data_json(key, info.key_size);
1165 jsonw_null_field(json_wtr, "key");
1167 jsonw_name(json_wtr, "next_key");
1168 print_hex_data_json(nextkey, info.key_size);
1169 jsonw_end_object(json_wtr);
1173 fprint_hex(stdout, key, info.key_size, " ");
1176 printf("key: None\n");
1178 printf("next key:\n");
1179 fprint_hex(stdout, nextkey, info.key_size, " ");
1191 static int do_delete(int argc, char **argv)
1193 struct bpf_map_info info = {};
1194 __u32 len = sizeof(info);
1202 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1206 key = malloc(info.key_size);
1208 p_err("mem alloc failed");
1213 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
1217 err = bpf_map_delete_elem(fd, key);
1219 p_err("delete failed: %s", strerror(errno));
1225 if (!err && json_output)
1226 jsonw_null(json_wtr);
1230 static int do_pin(int argc, char **argv)
1234 err = do_pin_any(argc, argv, map_parse_fd);
1235 if (!err && json_output)
1236 jsonw_null(json_wtr);
1240 static int do_create(int argc, char **argv)
1242 LIBBPF_OPTS(bpf_map_create_opts, attr);
1243 enum bpf_map_type map_type = BPF_MAP_TYPE_UNSPEC;
1244 __u32 key_size = 0, value_size = 0, max_entries = 0;
1245 const char *map_name = NULL;
1246 const char *pinfile;
1251 pinfile = GET_ARG();
1257 if (is_prefix(*argv, "type")) {
1261 p_err("map type already specified");
1265 map_type = map_type_from_str(*argv);
1266 if ((int)map_type < 0) {
1267 p_err("unrecognized map type: %s", *argv);
1271 } else if (is_prefix(*argv, "name")) {
1273 map_name = GET_ARG();
1274 } else if (is_prefix(*argv, "key")) {
1275 if (parse_u32_arg(&argc, &argv, &key_size,
1278 } else if (is_prefix(*argv, "value")) {
1279 if (parse_u32_arg(&argc, &argv, &value_size,
1282 } else if (is_prefix(*argv, "entries")) {
1283 if (parse_u32_arg(&argc, &argv, &max_entries,
1286 } else if (is_prefix(*argv, "flags")) {
1287 if (parse_u32_arg(&argc, &argv, &attr.map_flags,
1290 } else if (is_prefix(*argv, "dev")) {
1293 if (attr.map_ifindex) {
1294 p_err("offload device already specified");
1298 attr.map_ifindex = if_nametoindex(*argv);
1299 if (!attr.map_ifindex) {
1300 p_err("unrecognized netdevice '%s': %s",
1301 *argv, strerror(errno));
1305 } else if (is_prefix(*argv, "inner_map")) {
1306 struct bpf_map_info info = {};
1307 __u32 len = sizeof(info);
1313 inner_map_fd = map_parse_fd_and_info(&argc, &argv,
1315 if (inner_map_fd < 0)
1317 attr.inner_map_fd = inner_map_fd;
1319 p_err("unknown arg %s", *argv);
1325 p_err("map name not specified");
1331 fd = bpf_map_create(map_type, map_name, key_size, value_size, max_entries, &attr);
1333 p_err("map create failed: %s", strerror(errno));
1337 err = do_pin_fd(fd, pinfile);
1343 jsonw_null(json_wtr);
1346 if (attr.inner_map_fd > 0)
1347 close(attr.inner_map_fd);
1352 static int do_pop_dequeue(int argc, char **argv)
1354 struct bpf_map_info info = {};
1355 __u32 len = sizeof(info);
1363 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1367 err = alloc_key_value(&info, &key, &value);
1371 err = bpf_map_lookup_and_delete_elem(fd, key, value);
1373 if (errno == ENOENT) {
1375 jsonw_null(json_wtr);
1377 printf("Error: empty map\n");
1379 p_err("pop failed: %s", strerror(errno));
1385 print_key_value(&info, key, value);
1395 static int do_freeze(int argc, char **argv)
1402 fd = map_parse_fd(&argc, &argv);
1411 err = bpf_map_freeze(fd);
1414 p_err("failed to freeze map: %s", strerror(errno));
1419 jsonw_null(json_wtr);
1424 static int do_help(int argc, char **argv)
1427 jsonw_null(json_wtr);
1432 "Usage: %1$s %2$s { show | list } [MAP]\n"
1433 " %1$s %2$s create FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n"
1434 " entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
1435 " [inner_map MAP] [dev NAME]\n"
1436 " %1$s %2$s dump MAP\n"
1437 " %1$s %2$s update MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
1438 " %1$s %2$s lookup MAP [key DATA]\n"
1439 " %1$s %2$s getnext MAP [key DATA]\n"
1440 " %1$s %2$s delete MAP key DATA\n"
1441 " %1$s %2$s pin MAP FILE\n"
1442 " %1$s %2$s event_pipe MAP [cpu N index M]\n"
1443 " %1$s %2$s peek MAP\n"
1444 " %1$s %2$s push MAP value VALUE\n"
1445 " %1$s %2$s pop MAP\n"
1446 " %1$s %2$s enqueue MAP value VALUE\n"
1447 " %1$s %2$s dequeue MAP\n"
1448 " %1$s %2$s freeze MAP\n"
1451 " " HELP_SPEC_MAP "\n"
1452 " DATA := { [hex] BYTES }\n"
1453 " " HELP_SPEC_PROGRAM "\n"
1454 " VALUE := { DATA | MAP | PROG }\n"
1455 " UPDATE_FLAGS := { any | exist | noexist }\n"
1456 " TYPE := { hash | array | prog_array | perf_event_array | percpu_hash |\n"
1457 " percpu_array | stack_trace | cgroup_array | lru_hash |\n"
1458 " lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n"
1459 " devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash |\n"
1460 " cgroup_storage | reuseport_sockarray | percpu_cgroup_storage |\n"
1461 " queue | stack | sk_storage | struct_ops | ringbuf | inode_storage |\n"
1462 " task_storage | bloom_filter | user_ringbuf }\n"
1463 " " HELP_SPEC_OPTIONS " |\n"
1464 " {-f|--bpffs} | {-n|--nomount} }\n"
1466 bin_name, argv[-2]);
1471 static const struct cmd cmds[] = {
1472 { "show", do_show },
1473 { "list", do_show },
1474 { "help", do_help },
1475 { "dump", do_dump },
1476 { "update", do_update },
1477 { "lookup", do_lookup },
1478 { "getnext", do_getnext },
1479 { "delete", do_delete },
1481 { "event_pipe", do_event_pipe },
1482 { "create", do_create },
1483 { "peek", do_lookup },
1484 { "push", do_update },
1485 { "enqueue", do_update },
1486 { "pop", do_pop_dequeue },
1487 { "dequeue", do_pop_dequeue },
1488 { "freeze", do_freeze },
1492 int do_map(int argc, char **argv)
1494 return cmd_select(cmds, argc, argv, do_help);