1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
7 #include <linux/kernel.h>
14 #include <sys/types.h>
19 #include <bpf/hashmap.h>
21 #include "json_writer.h"
24 static struct hashmap
*map_table
;
26 static bool map_is_per_cpu(__u32 type
)
28 return type
== BPF_MAP_TYPE_PERCPU_HASH
||
29 type
== BPF_MAP_TYPE_PERCPU_ARRAY
||
30 type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
31 type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
;
34 static bool map_is_map_of_maps(__u32 type
)
36 return type
== BPF_MAP_TYPE_ARRAY_OF_MAPS
||
37 type
== BPF_MAP_TYPE_HASH_OF_MAPS
;
40 static bool map_is_map_of_progs(__u32 type
)
42 return type
== BPF_MAP_TYPE_PROG_ARRAY
;
45 static int map_type_from_str(const char *type
)
47 const char *map_type_str
;
51 map_type_str
= libbpf_bpf_map_type_str(i
);
55 /* Don't allow prefixing in case of possible future shadowing */
56 if (!strcmp(map_type_str
, type
))
62 static void *alloc_value(struct bpf_map_info
*info
)
64 if (map_is_per_cpu(info
->type
))
65 return malloc(round_up(info
->value_size
, 8) *
68 return malloc(info
->value_size
);
71 static int do_dump_btf(const struct btf_dumper
*d
,
72 struct bpf_map_info
*map_info
, void *key
,
78 /* start of key-value pair */
79 jsonw_start_object(d
->jw
);
81 if (map_info
->btf_key_type_id
) {
82 jsonw_name(d
->jw
, "key");
84 ret
= btf_dumper_type(d
, map_info
->btf_key_type_id
, key
);
89 value_id
= map_info
->btf_vmlinux_value_type_id
?
90 : map_info
->btf_value_type_id
;
92 if (!map_is_per_cpu(map_info
->type
)) {
93 jsonw_name(d
->jw
, "value");
94 ret
= btf_dumper_type(d
, value_id
, value
);
96 unsigned int i
, n
, step
;
98 jsonw_name(d
->jw
, "values");
99 jsonw_start_array(d
->jw
);
100 n
= get_possible_cpus();
101 step
= round_up(map_info
->value_size
, 8);
102 for (i
= 0; i
< n
; i
++) {
103 jsonw_start_object(d
->jw
);
104 jsonw_int_field(d
->jw
, "cpu", i
);
105 jsonw_name(d
->jw
, "value");
106 ret
= btf_dumper_type(d
, value_id
, value
+ i
* step
);
107 jsonw_end_object(d
->jw
);
111 jsonw_end_array(d
->jw
);
115 /* end of key-value pair */
116 jsonw_end_object(d
->jw
);
121 static json_writer_t
*get_btf_writer(void)
123 json_writer_t
*jw
= jsonw_new(stdout
);
127 jsonw_pretty(jw
, true);
132 static void print_entry_json(struct bpf_map_info
*info
, unsigned char *key
,
133 unsigned char *value
, struct btf
*btf
)
135 jsonw_start_object(json_wtr
);
137 if (!map_is_per_cpu(info
->type
)) {
138 jsonw_name(json_wtr
, "key");
139 print_hex_data_json(key
, info
->key_size
);
140 jsonw_name(json_wtr
, "value");
141 print_hex_data_json(value
, info
->value_size
);
142 if (map_is_map_of_maps(info
->type
))
143 jsonw_uint_field(json_wtr
, "inner_map_id",
144 *(unsigned int *)value
);
146 struct btf_dumper d
= {
149 .is_plain_text
= false,
152 jsonw_name(json_wtr
, "formatted");
153 do_dump_btf(&d
, info
, key
, value
);
156 unsigned int i
, n
, step
;
158 n
= get_possible_cpus();
159 step
= round_up(info
->value_size
, 8);
161 jsonw_name(json_wtr
, "key");
162 print_hex_data_json(key
, info
->key_size
);
164 jsonw_name(json_wtr
, "values");
165 jsonw_start_array(json_wtr
);
166 for (i
= 0; i
< n
; i
++) {
167 jsonw_start_object(json_wtr
);
169 jsonw_int_field(json_wtr
, "cpu", i
);
171 jsonw_name(json_wtr
, "value");
172 print_hex_data_json(value
+ i
* step
,
175 jsonw_end_object(json_wtr
);
177 jsonw_end_array(json_wtr
);
179 struct btf_dumper d
= {
182 .is_plain_text
= false,
185 jsonw_name(json_wtr
, "formatted");
186 do_dump_btf(&d
, info
, key
, value
);
190 jsonw_end_object(json_wtr
);
194 print_entry_error_msg(struct bpf_map_info
*info
, unsigned char *key
,
195 const char *error_msg
)
197 int msg_size
= strlen(error_msg
);
198 bool single_line
, break_names
;
200 break_names
= info
->key_size
> 16 || msg_size
> 16;
201 single_line
= info
->key_size
+ msg_size
<= 24 && !break_names
;
203 printf("key:%c", break_names
? '\n' : ' ');
204 fprint_hex(stdout
, key
, info
->key_size
, " ");
206 printf(single_line
? " " : "\n");
208 printf("value:%c%s", break_names
? '\n' : ' ', error_msg
);
214 print_entry_error(struct bpf_map_info
*map_info
, void *key
, int lookup_errno
)
216 /* For prog_array maps or arrays of maps, failure to lookup the value
217 * means there is no entry for that key. Do not print an error message
220 if ((map_is_map_of_maps(map_info
->type
) ||
221 map_is_map_of_progs(map_info
->type
)) && lookup_errno
== ENOENT
)
225 jsonw_start_object(json_wtr
); /* entry */
226 jsonw_name(json_wtr
, "key");
227 print_hex_data_json(key
, map_info
->key_size
);
228 jsonw_name(json_wtr
, "value");
229 jsonw_start_object(json_wtr
); /* error */
230 jsonw_string_field(json_wtr
, "error", strerror(lookup_errno
));
231 jsonw_end_object(json_wtr
); /* error */
232 jsonw_end_object(json_wtr
); /* entry */
234 const char *msg
= NULL
;
236 if (lookup_errno
== ENOENT
)
238 else if (lookup_errno
== ENOSPC
&&
239 map_info
->type
== BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
)
240 msg
= "<cannot read>";
242 print_entry_error_msg(map_info
, key
,
243 msg
? : strerror(lookup_errno
));
247 static void print_entry_plain(struct bpf_map_info
*info
, unsigned char *key
,
248 unsigned char *value
)
250 if (!map_is_per_cpu(info
->type
)) {
251 bool single_line
, break_names
;
253 break_names
= info
->key_size
> 16 || info
->value_size
> 16;
254 single_line
= info
->key_size
+ info
->value_size
<= 24 &&
257 if (info
->key_size
) {
258 printf("key:%c", break_names
? '\n' : ' ');
259 fprint_hex(stdout
, key
, info
->key_size
, " ");
261 printf(single_line
? " " : "\n");
264 if (info
->value_size
) {
265 if (map_is_map_of_maps(info
->type
)) {
266 printf("inner_map_id:%c", break_names
? '\n' : ' ');
267 printf("%u ", *(unsigned int *)value
);
269 printf("value:%c", break_names
? '\n' : ' ');
270 fprint_hex(stdout
, value
, info
->value_size
, " ");
276 unsigned int i
, n
, step
;
278 n
= get_possible_cpus();
279 step
= round_up(info
->value_size
, 8);
281 if (info
->key_size
) {
283 fprint_hex(stdout
, key
, info
->key_size
, " ");
286 if (info
->value_size
) {
287 for (i
= 0; i
< n
; i
++) {
288 printf("value (CPU %02d):%c",
289 i
, info
->value_size
> 16 ? '\n' : ' ');
290 fprint_hex(stdout
, value
+ i
* step
,
291 info
->value_size
, " ");
298 static char **parse_bytes(char **argv
, const char *name
, unsigned char *val
,
301 unsigned int i
= 0, base
= 0;
304 if (is_prefix(*argv
, "hex")) {
309 while (i
< n
&& argv
[i
]) {
310 val
[i
] = strtoul(argv
[i
], &endptr
, base
);
312 p_err("error parsing byte: %s", argv
[i
]);
319 p_err("%s expected %d bytes got %d", name
, n
, i
);
326 /* on per cpu maps we must copy the provided value on all value instances */
327 static void fill_per_cpu_value(struct bpf_map_info
*info
, void *value
)
329 unsigned int i
, n
, step
;
331 if (!map_is_per_cpu(info
->type
))
334 n
= get_possible_cpus();
335 step
= round_up(info
->value_size
, 8);
336 for (i
= 1; i
< n
; i
++)
337 memcpy(value
+ i
* step
, value
, info
->value_size
);
340 static int parse_elem(char **argv
, struct bpf_map_info
*info
,
341 void *key
, void *value
, __u32 key_size
, __u32 value_size
,
342 __u32
*flags
, __u32
**value_fd
)
347 p_err("did not find %s", key
? "key" : "value");
351 if (is_prefix(*argv
, "key")) {
354 p_err("duplicate key");
356 p_err("unnecessary key");
360 argv
= parse_bytes(argv
+ 1, "key", key
, key_size
);
364 return parse_elem(argv
, info
, NULL
, value
, key_size
, value_size
,
366 } else if (is_prefix(*argv
, "value")) {
371 p_err("duplicate value");
373 p_err("unnecessary value");
379 if (map_is_map_of_maps(info
->type
)) {
382 if (value_size
!= 4) {
383 p_err("value smaller than 4B for map in map?");
386 if (!argv
[0] || !argv
[1]) {
387 p_err("not enough value arguments for map in map");
391 fd
= map_parse_fd(&argc
, &argv
);
397 } else if (map_is_map_of_progs(info
->type
)) {
400 if (value_size
!= 4) {
401 p_err("value smaller than 4B for map of progs?");
404 if (!argv
[0] || !argv
[1]) {
405 p_err("not enough value arguments for map of progs");
408 if (is_prefix(*argv
, "id"))
409 p_info("Warning: updating program array via MAP_ID, make sure this map is kept open\n"
410 " by some process or pinned otherwise update will be lost");
412 fd
= prog_parse_fd(&argc
, &argv
);
419 argv
= parse_bytes(argv
, "value", value
, value_size
);
423 fill_per_cpu_value(info
, value
);
426 return parse_elem(argv
, info
, key
, NULL
, key_size
, value_size
,
428 } else if (is_prefix(*argv
, "any") || is_prefix(*argv
, "noexist") ||
429 is_prefix(*argv
, "exist")) {
431 p_err("flags specified multiple times: %s", *argv
);
435 if (is_prefix(*argv
, "any"))
437 else if (is_prefix(*argv
, "noexist"))
438 *flags
= BPF_NOEXIST
;
439 else if (is_prefix(*argv
, "exist"))
442 return parse_elem(argv
+ 1, info
, key
, value
, key_size
,
443 value_size
, NULL
, value_fd
);
446 p_err("expected key or value, got: %s", *argv
);
450 static void show_map_header_json(struct bpf_map_info
*info
, json_writer_t
*wtr
)
452 const char *map_type_str
;
454 jsonw_uint_field(wtr
, "id", info
->id
);
455 map_type_str
= libbpf_bpf_map_type_str(info
->type
);
457 jsonw_string_field(wtr
, "type", map_type_str
);
459 jsonw_uint_field(wtr
, "type", info
->type
);
462 jsonw_string_field(wtr
, "name", info
->name
);
464 jsonw_name(wtr
, "flags");
465 jsonw_printf(wtr
, "%d", info
->map_flags
);
468 static int show_map_close_json(int fd
, struct bpf_map_info
*info
)
470 char *memlock
, *frozen_str
;
473 memlock
= get_fdinfo(fd
, "memlock");
474 frozen_str
= get_fdinfo(fd
, "frozen");
476 jsonw_start_object(json_wtr
);
478 show_map_header_json(info
, json_wtr
);
480 print_dev_json(info
->ifindex
, info
->netns_dev
, info
->netns_ino
);
482 jsonw_uint_field(json_wtr
, "bytes_key", info
->key_size
);
483 jsonw_uint_field(json_wtr
, "bytes_value", info
->value_size
);
484 jsonw_uint_field(json_wtr
, "max_entries", info
->max_entries
);
487 jsonw_int_field(json_wtr
, "bytes_memlock", atoll(memlock
));
490 if (info
->type
== BPF_MAP_TYPE_PROG_ARRAY
) {
491 char *owner_prog_type
= get_fdinfo(fd
, "owner_prog_type");
492 char *owner_jited
= get_fdinfo(fd
, "owner_jited");
494 if (owner_prog_type
) {
495 unsigned int prog_type
= atoi(owner_prog_type
);
496 const char *prog_type_str
;
498 prog_type_str
= libbpf_bpf_prog_type_str(prog_type
);
500 jsonw_string_field(json_wtr
, "owner_prog_type",
503 jsonw_uint_field(json_wtr
, "owner_prog_type",
507 jsonw_bool_field(json_wtr
, "owner_jited",
508 !!atoi(owner_jited
));
510 free(owner_prog_type
);
516 frozen
= atoi(frozen_str
);
519 jsonw_int_field(json_wtr
, "frozen", frozen
);
522 jsonw_int_field(json_wtr
, "btf_id", info
->btf_id
);
524 if (!hashmap__empty(map_table
)) {
525 struct hashmap_entry
*entry
;
527 jsonw_name(json_wtr
, "pinned");
528 jsonw_start_array(json_wtr
);
529 hashmap__for_each_key_entry(map_table
, entry
, info
->id
)
530 jsonw_string(json_wtr
, entry
->pvalue
);
531 jsonw_end_array(json_wtr
);
534 emit_obj_refs_json(refs_table
, info
->id
, json_wtr
);
536 jsonw_end_object(json_wtr
);
541 static void show_map_header_plain(struct bpf_map_info
*info
)
543 const char *map_type_str
;
545 printf("%u: ", info
->id
);
547 map_type_str
= libbpf_bpf_map_type_str(info
->type
);
549 printf("%s ", map_type_str
);
551 printf("type %u ", info
->type
);
554 printf("name %s ", info
->name
);
556 printf("flags 0x%x", info
->map_flags
);
557 print_dev_plain(info
->ifindex
, info
->netns_dev
, info
->netns_ino
);
561 static int show_map_close_plain(int fd
, struct bpf_map_info
*info
)
563 char *memlock
, *frozen_str
;
566 memlock
= get_fdinfo(fd
, "memlock");
567 frozen_str
= get_fdinfo(fd
, "frozen");
569 show_map_header_plain(info
);
570 printf("\tkey %uB value %uB max_entries %u",
571 info
->key_size
, info
->value_size
, info
->max_entries
);
574 printf(" memlock %sB", memlock
);
577 if (info
->type
== BPF_MAP_TYPE_PROG_ARRAY
) {
578 char *owner_prog_type
= get_fdinfo(fd
, "owner_prog_type");
579 char *owner_jited
= get_fdinfo(fd
, "owner_jited");
581 if (owner_prog_type
|| owner_jited
)
583 if (owner_prog_type
) {
584 unsigned int prog_type
= atoi(owner_prog_type
);
585 const char *prog_type_str
;
587 prog_type_str
= libbpf_bpf_prog_type_str(prog_type
);
589 printf("owner_prog_type %s ", prog_type_str
);
591 printf("owner_prog_type %d ", prog_type
);
594 printf("owner%s jited",
595 atoi(owner_jited
) ? "" : " not");
597 free(owner_prog_type
);
602 if (!hashmap__empty(map_table
)) {
603 struct hashmap_entry
*entry
;
605 hashmap__for_each_key_entry(map_table
, entry
, info
->id
)
606 printf("\n\tpinned %s", (char *)entry
->pvalue
);
610 frozen
= atoi(frozen_str
);
614 if (info
->btf_id
|| frozen
)
618 printf("btf_id %d", info
->btf_id
);
621 printf("%sfrozen", info
->btf_id
? " " : "");
623 emit_obj_refs_plain(refs_table
, info
->id
, "\n\tpids ");
629 static int do_show_subset(int argc
, char **argv
)
631 struct bpf_map_info info
= {};
632 __u32 len
= sizeof(info
);
637 fds
= malloc(sizeof(int));
639 p_err("mem alloc failed");
642 nb_fds
= map_parse_fds(&argc
, &argv
, &fds
);
646 if (json_output
&& nb_fds
> 1)
647 jsonw_start_array(json_wtr
); /* root array */
648 for (i
= 0; i
< nb_fds
; i
++) {
649 err
= bpf_map_get_info_by_fd(fds
[i
], &info
, &len
);
651 p_err("can't get map info: %s",
653 for (; i
< nb_fds
; i
++)
659 show_map_close_json(fds
[i
], &info
);
661 show_map_close_plain(fds
[i
], &info
);
665 if (json_output
&& nb_fds
> 1)
666 jsonw_end_array(json_wtr
); /* root array */
673 static int do_show(int argc
, char **argv
)
675 struct bpf_map_info info
= {};
676 __u32 len
= sizeof(info
);
682 map_table
= hashmap__new(hash_fn_for_key_as_id
,
683 equal_fn_for_key_as_id
, NULL
);
684 if (IS_ERR(map_table
)) {
685 p_err("failed to create hashmap for pinned paths");
688 build_pinned_obj_table(map_table
, BPF_OBJ_MAP
);
690 build_obj_refs_table(&refs_table
, BPF_OBJ_MAP
);
693 return do_show_subset(argc
, argv
);
699 jsonw_start_array(json_wtr
);
701 err
= bpf_map_get_next_id(id
, &id
);
705 p_err("can't get next map: %s%s", strerror(errno
),
706 errno
== EINVAL
? " -- kernel too old?" : "");
710 fd
= bpf_map_get_fd_by_id(id
);
714 p_err("can't get map by id (%u): %s",
715 id
, strerror(errno
));
719 err
= bpf_map_get_info_by_fd(fd
, &info
, &len
);
721 p_err("can't get map info: %s", strerror(errno
));
727 show_map_close_json(fd
, &info
);
729 show_map_close_plain(fd
, &info
);
732 jsonw_end_array(json_wtr
);
734 delete_obj_refs_table(refs_table
);
737 delete_pinned_obj_table(map_table
);
739 return errno
== ENOENT
? 0 : -1;
742 static int dump_map_elem(int fd
, void *key
, void *value
,
743 struct bpf_map_info
*map_info
, struct btf
*btf
,
744 json_writer_t
*btf_wtr
)
746 if (bpf_map_lookup_elem(fd
, key
, value
)) {
747 print_entry_error(map_info
, key
, errno
);
752 print_entry_json(map_info
, key
, value
, btf
);
754 struct btf_dumper d
= {
757 .is_plain_text
= true,
760 do_dump_btf(&d
, map_info
, key
, value
);
762 print_entry_plain(map_info
, key
, value
);
768 static int maps_have_btf(int *fds
, int nb_fds
)
770 struct bpf_map_info info
= {};
771 __u32 len
= sizeof(info
);
774 for (i
= 0; i
< nb_fds
; i
++) {
775 err
= bpf_map_get_info_by_fd(fds
[i
], &info
, &len
);
777 p_err("can't get map info: %s", strerror(errno
));
788 static struct btf
*btf_vmlinux
;
790 static int get_map_kv_btf(const struct bpf_map_info
*info
, struct btf
**btf
)
794 if (info
->btf_vmlinux_value_type_id
) {
796 btf_vmlinux
= libbpf_find_kernel_btf();
798 p_err("failed to get kernel btf");
803 } else if (info
->btf_value_type_id
) {
804 *btf
= btf__load_from_kernel_by_id(info
->btf_id
);
807 p_err("failed to get btf");
816 static void free_map_kv_btf(struct btf
*btf
)
818 if (btf
!= btf_vmlinux
)
823 map_dump(int fd
, struct bpf_map_info
*info
, json_writer_t
*wtr
,
826 void *key
, *value
, *prev_key
;
827 unsigned int num_elems
= 0;
828 struct btf
*btf
= NULL
;
831 key
= malloc(info
->key_size
);
832 value
= alloc_value(info
);
833 if (!key
|| !value
) {
834 p_err("mem alloc failed");
842 err
= get_map_kv_btf(info
, &btf
);
848 jsonw_start_object(wtr
); /* map object */
849 show_map_header_json(info
, wtr
);
850 jsonw_name(wtr
, "elements");
852 jsonw_start_array(wtr
); /* elements */
853 } else if (show_header
) {
854 show_map_header_plain(info
);
857 if (info
->type
== BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
&&
858 info
->value_size
!= 8) {
859 const char *map_type_str
;
861 map_type_str
= libbpf_bpf_map_type_str(info
->type
);
862 p_info("Warning: cannot read values from %s map with value_size != 8",
866 err
= bpf_map_get_next_key(fd
, prev_key
, key
);
872 if (!dump_map_elem(fd
, key
, value
, info
, btf
, wtr
))
878 jsonw_end_array(wtr
); /* elements */
880 jsonw_end_object(wtr
); /* map object */
882 printf("Found %u element%s\n", num_elems
,
883 num_elems
!= 1 ? "s" : "");
890 free_map_kv_btf(btf
);
895 static int do_dump(int argc
, char **argv
)
897 json_writer_t
*wtr
= NULL
, *btf_wtr
= NULL
;
898 struct bpf_map_info info
= {};
900 __u32 len
= sizeof(info
);
907 fds
= malloc(sizeof(int));
909 p_err("mem alloc failed");
912 nb_fds
= map_parse_fds(&argc
, &argv
, &fds
);
921 do_plain_btf
= maps_have_btf(fds
, nb_fds
);
922 if (do_plain_btf
< 0)
926 btf_wtr
= get_btf_writer();
929 p_info("failed to create json writer for btf. falling back to plain output");
933 if (wtr
&& nb_fds
> 1)
934 jsonw_start_array(wtr
); /* root array */
935 for (i
= 0; i
< nb_fds
; i
++) {
936 if (bpf_map_get_info_by_fd(fds
[i
], &info
, &len
)) {
937 p_err("can't get map info: %s", strerror(errno
));
940 err
= map_dump(fds
[i
], &info
, wtr
, nb_fds
> 1);
941 if (!wtr
&& i
!= nb_fds
- 1)
948 if (wtr
&& nb_fds
> 1)
949 jsonw_end_array(wtr
); /* root array */
952 jsonw_destroy(&btf_wtr
);
954 for (; i
< nb_fds
; i
++)
958 btf__free(btf_vmlinux
);
962 static int alloc_key_value(struct bpf_map_info
*info
, void **key
, void **value
)
967 if (info
->key_size
) {
968 *key
= malloc(info
->key_size
);
970 p_err("key mem alloc failed");
975 if (info
->value_size
) {
976 *value
= alloc_value(info
);
978 p_err("value mem alloc failed");
988 static int do_update(int argc
, char **argv
)
990 struct bpf_map_info info
= {};
991 __u32 len
= sizeof(info
);
992 __u32
*value_fd
= NULL
;
993 __u32 flags
= BPF_ANY
;
1000 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
1004 err
= alloc_key_value(&info
, &key
, &value
);
1008 err
= parse_elem(argv
, &info
, key
, value
, info
.key_size
,
1009 info
.value_size
, &flags
, &value_fd
);
1013 err
= bpf_map_update_elem(fd
, key
, value
, flags
);
1015 p_err("update failed: %s", strerror(errno
));
1026 if (!err
&& json_output
)
1027 jsonw_null(json_wtr
);
1031 static void print_key_value(struct bpf_map_info
*info
, void *key
,
1034 json_writer_t
*btf_wtr
;
1037 if (get_map_kv_btf(info
, &btf
))
1041 print_entry_json(info
, key
, value
, btf
);
1043 /* if here json_wtr wouldn't have been initialised,
1044 * so let's create separate writer for btf
1046 btf_wtr
= get_btf_writer();
1048 p_info("failed to create json writer for btf. falling back to plain output");
1051 print_entry_plain(info
, key
, value
);
1053 struct btf_dumper d
= {
1056 .is_plain_text
= true,
1059 do_dump_btf(&d
, info
, key
, value
);
1060 jsonw_destroy(&btf_wtr
);
1063 print_entry_plain(info
, key
, value
);
1068 static int do_lookup(int argc
, char **argv
)
1070 struct bpf_map_info info
= {};
1071 __u32 len
= sizeof(info
);
1079 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
1083 err
= alloc_key_value(&info
, &key
, &value
);
1087 err
= parse_elem(argv
, &info
, key
, NULL
, info
.key_size
, 0, NULL
, NULL
);
1091 err
= bpf_map_lookup_elem(fd
, key
, value
);
1093 if (errno
== ENOENT
) {
1095 jsonw_null(json_wtr
);
1098 fprint_hex(stdout
, key
, info
.key_size
, " ");
1099 printf("\n\nNot found\n");
1102 p_err("lookup failed: %s", strerror(errno
));
1108 /* here means bpf_map_lookup_elem() succeeded */
1109 print_key_value(&info
, key
, value
);
1119 static int do_getnext(int argc
, char **argv
)
1121 struct bpf_map_info info
= {};
1122 __u32 len
= sizeof(info
);
1123 void *key
, *nextkey
;
1130 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
1134 key
= malloc(info
.key_size
);
1135 nextkey
= malloc(info
.key_size
);
1136 if (!key
|| !nextkey
) {
1137 p_err("mem alloc failed");
1143 err
= parse_elem(argv
, &info
, key
, NULL
, info
.key_size
, 0,
1152 err
= bpf_map_get_next_key(fd
, key
, nextkey
);
1154 p_err("can't get next key: %s", strerror(errno
));
1159 jsonw_start_object(json_wtr
);
1161 jsonw_name(json_wtr
, "key");
1162 print_hex_data_json(key
, info
.key_size
);
1164 jsonw_null_field(json_wtr
, "key");
1166 jsonw_name(json_wtr
, "next_key");
1167 print_hex_data_json(nextkey
, info
.key_size
);
1168 jsonw_end_object(json_wtr
);
1172 fprint_hex(stdout
, key
, info
.key_size
, " ");
1175 printf("key: None\n");
1177 printf("next key:\n");
1178 fprint_hex(stdout
, nextkey
, info
.key_size
, " ");
1190 static int do_delete(int argc
, char **argv
)
1192 struct bpf_map_info info
= {};
1193 __u32 len
= sizeof(info
);
1201 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
1205 key
= malloc(info
.key_size
);
1207 p_err("mem alloc failed");
1212 err
= parse_elem(argv
, &info
, key
, NULL
, info
.key_size
, 0, NULL
, NULL
);
1216 err
= bpf_map_delete_elem(fd
, key
);
1218 p_err("delete failed: %s", strerror(errno
));
1224 if (!err
&& json_output
)
1225 jsonw_null(json_wtr
);
1229 static int do_pin(int argc
, char **argv
)
1233 err
= do_pin_any(argc
, argv
, map_parse_fd
);
1234 if (!err
&& json_output
)
1235 jsonw_null(json_wtr
);
1239 static int do_create(int argc
, char **argv
)
1241 LIBBPF_OPTS(bpf_map_create_opts
, attr
);
1242 enum bpf_map_type map_type
= BPF_MAP_TYPE_UNSPEC
;
1243 __u32 key_size
= 0, value_size
= 0, max_entries
= 0;
1244 const char *map_name
= NULL
;
1245 const char *pinfile
;
1250 pinfile
= GET_ARG();
1256 if (is_prefix(*argv
, "type")) {
1260 p_err("map type already specified");
1264 map_type
= map_type_from_str(*argv
);
1265 if ((int)map_type
< 0) {
1266 p_err("unrecognized map type: %s", *argv
);
1270 } else if (is_prefix(*argv
, "name")) {
1272 map_name
= GET_ARG();
1273 } else if (is_prefix(*argv
, "key")) {
1274 if (parse_u32_arg(&argc
, &argv
, &key_size
,
1277 } else if (is_prefix(*argv
, "value")) {
1278 if (parse_u32_arg(&argc
, &argv
, &value_size
,
1281 } else if (is_prefix(*argv
, "entries")) {
1282 if (parse_u32_arg(&argc
, &argv
, &max_entries
,
1285 } else if (is_prefix(*argv
, "flags")) {
1286 if (parse_u32_arg(&argc
, &argv
, &attr
.map_flags
,
1289 } else if (is_prefix(*argv
, "dev")) {
1290 p_info("Warning: 'bpftool map create [...] dev <ifname>' syntax is deprecated.\n"
1291 "Going further, please use 'offload_dev <ifname>' to request hardware offload for the map.");
1293 } else if (is_prefix(*argv
, "offload_dev")) {
1297 if (attr
.map_ifindex
) {
1298 p_err("offload device already specified");
1302 attr
.map_ifindex
= if_nametoindex(*argv
);
1303 if (!attr
.map_ifindex
) {
1304 p_err("unrecognized netdevice '%s': %s",
1305 *argv
, strerror(errno
));
1309 } else if (is_prefix(*argv
, "inner_map")) {
1310 struct bpf_map_info info
= {};
1311 __u32 len
= sizeof(info
);
1317 inner_map_fd
= map_parse_fd_and_info(&argc
, &argv
,
1319 if (inner_map_fd
< 0)
1321 attr
.inner_map_fd
= inner_map_fd
;
1323 p_err("unknown arg %s", *argv
);
1329 p_err("map name not specified");
1335 fd
= bpf_map_create(map_type
, map_name
, key_size
, value_size
, max_entries
, &attr
);
1337 p_err("map create failed: %s", strerror(errno
));
1341 err
= do_pin_fd(fd
, pinfile
);
1347 jsonw_null(json_wtr
);
1350 if (attr
.inner_map_fd
> 0)
1351 close(attr
.inner_map_fd
);
1356 static int do_pop_dequeue(int argc
, char **argv
)
1358 struct bpf_map_info info
= {};
1359 __u32 len
= sizeof(info
);
1367 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
1371 err
= alloc_key_value(&info
, &key
, &value
);
1375 err
= bpf_map_lookup_and_delete_elem(fd
, key
, value
);
1377 if (errno
== ENOENT
) {
1379 jsonw_null(json_wtr
);
1381 printf("Error: empty map\n");
1383 p_err("pop failed: %s", strerror(errno
));
1389 print_key_value(&info
, key
, value
);
1399 static int do_freeze(int argc
, char **argv
)
1406 fd
= map_parse_fd(&argc
, &argv
);
1415 err
= bpf_map_freeze(fd
);
1418 p_err("failed to freeze map: %s", strerror(errno
));
1423 jsonw_null(json_wtr
);
1428 static int do_help(int argc
, char **argv
)
1431 jsonw_null(json_wtr
);
1436 "Usage: %1$s %2$s { show | list } [MAP]\n"
1437 " %1$s %2$s create FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n"
1438 " entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
1439 " [inner_map MAP] [offload_dev NAME]\n"
1440 " %1$s %2$s dump MAP\n"
1441 " %1$s %2$s update MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
1442 " %1$s %2$s lookup MAP [key DATA]\n"
1443 " %1$s %2$s getnext MAP [key DATA]\n"
1444 " %1$s %2$s delete MAP key DATA\n"
1445 " %1$s %2$s pin MAP FILE\n"
1446 " %1$s %2$s event_pipe MAP [cpu N index M]\n"
1447 " %1$s %2$s peek MAP\n"
1448 " %1$s %2$s push MAP value VALUE\n"
1449 " %1$s %2$s pop MAP\n"
1450 " %1$s %2$s enqueue MAP value VALUE\n"
1451 " %1$s %2$s dequeue MAP\n"
1452 " %1$s %2$s freeze MAP\n"
1455 " " HELP_SPEC_MAP
"\n"
1456 " DATA := { [hex] BYTES }\n"
1457 " " HELP_SPEC_PROGRAM
"\n"
1458 " VALUE := { DATA | MAP | PROG }\n"
1459 " UPDATE_FLAGS := { any | exist | noexist }\n"
1460 " TYPE := { hash | array | prog_array | perf_event_array | percpu_hash |\n"
1461 " percpu_array | stack_trace | cgroup_array | lru_hash |\n"
1462 " lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n"
1463 " devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash |\n"
1464 " cgroup_storage | reuseport_sockarray | percpu_cgroup_storage |\n"
1465 " queue | stack | sk_storage | struct_ops | ringbuf | inode_storage |\n"
1466 " task_storage | bloom_filter | user_ringbuf | cgrp_storage | arena }\n"
1467 " " HELP_SPEC_OPTIONS
" |\n"
1468 " {-f|--bpffs} | {-n|--nomount} }\n"
1470 bin_name
, argv
[-2]);
1475 static const struct cmd cmds
[] = {
1476 { "show", do_show
},
1477 { "list", do_show
},
1478 { "help", do_help
},
1479 { "dump", do_dump
},
1480 { "update", do_update
},
1481 { "lookup", do_lookup
},
1482 { "getnext", do_getnext
},
1483 { "delete", do_delete
},
1485 { "event_pipe", do_event_pipe
},
1486 { "create", do_create
},
1487 { "peek", do_lookup
},
1488 { "push", do_update
},
1489 { "enqueue", do_update
},
1490 { "pop", do_pop_dequeue
},
1491 { "dequeue", do_pop_dequeue
},
1492 { "freeze", do_freeze
},
1496 int do_map(int argc
, char **argv
)
1498 return cmd_select(cmds
, argc
, argv
, do_help
);