2 * Copyright (C) 2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 /* Author: Jakub Kicinski <kubakici@wp.pl> */
45 #include <sys/types.h>
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 [BPF_MAP_TYPE_CPUMAP
] = "cpumap",
72 static unsigned int get_possible_cpus(void)
74 static unsigned int result
;
83 fd
= open("/sys/devices/system/cpu/possible", O_RDONLY
);
85 p_err("can't open sysfs possible cpus");
89 n
= read(fd
, buf
, sizeof(buf
));
91 p_err("can't read sysfs possible cpus");
96 if (n
== sizeof(buf
)) {
97 p_err("read sysfs possible cpus overflow");
103 while (*ptr
&& *ptr
!= '\n') {
106 if (sscanf(ptr
, "%u-%u", &a
, &b
) == 2) {
109 ptr
= strchr(ptr
, '-') + 1;
110 } else if (sscanf(ptr
, "%u", &a
) == 1) {
116 while (isdigit(*ptr
))
127 static bool map_is_per_cpu(__u32 type
)
129 return type
== BPF_MAP_TYPE_PERCPU_HASH
||
130 type
== BPF_MAP_TYPE_PERCPU_ARRAY
||
131 type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
;
134 static bool map_is_map_of_maps(__u32 type
)
136 return type
== BPF_MAP_TYPE_ARRAY_OF_MAPS
||
137 type
== BPF_MAP_TYPE_HASH_OF_MAPS
;
140 static bool map_is_map_of_progs(__u32 type
)
142 return type
== BPF_MAP_TYPE_PROG_ARRAY
;
145 static void *alloc_value(struct bpf_map_info
*info
)
147 if (map_is_per_cpu(info
->type
))
148 return malloc(info
->value_size
* get_possible_cpus());
150 return malloc(info
->value_size
);
153 static int map_parse_fd(int *argc
, char ***argv
)
157 if (is_prefix(**argv
, "id")) {
163 id
= strtoul(**argv
, &endptr
, 0);
165 p_err("can't parse %s as ID", **argv
);
170 fd
= bpf_map_get_fd_by_id(id
);
172 p_err("get map by id (%u): %s", id
, strerror(errno
));
174 } else if (is_prefix(**argv
, "pinned")) {
182 return open_obj_pinned_any(path
, BPF_OBJ_MAP
);
185 p_err("expected 'id' or 'pinned', got: '%s'?", **argv
);
190 map_parse_fd_and_info(int *argc
, char ***argv
, void *info
, __u32
*info_len
)
195 fd
= map_parse_fd(argc
, argv
);
199 err
= bpf_obj_get_info_by_fd(fd
, info
, info_len
);
201 p_err("can't get map info: %s", strerror(errno
));
209 static void print_entry_json(struct bpf_map_info
*info
, unsigned char *key
,
210 unsigned char *value
)
212 jsonw_start_object(json_wtr
);
214 if (!map_is_per_cpu(info
->type
)) {
215 jsonw_name(json_wtr
, "key");
216 print_hex_data_json(key
, info
->key_size
);
217 jsonw_name(json_wtr
, "value");
218 print_hex_data_json(value
, info
->value_size
);
222 n
= get_possible_cpus();
224 jsonw_name(json_wtr
, "key");
225 print_hex_data_json(key
, info
->key_size
);
227 jsonw_name(json_wtr
, "values");
228 jsonw_start_array(json_wtr
);
229 for (i
= 0; i
< n
; i
++) {
230 jsonw_start_object(json_wtr
);
232 jsonw_int_field(json_wtr
, "cpu", i
);
234 jsonw_name(json_wtr
, "value");
235 print_hex_data_json(value
+ i
* info
->value_size
,
238 jsonw_end_object(json_wtr
);
240 jsonw_end_array(json_wtr
);
243 jsonw_end_object(json_wtr
);
246 static void print_entry_plain(struct bpf_map_info
*info
, unsigned char *key
,
247 unsigned char *value
)
249 if (!map_is_per_cpu(info
->type
)) {
250 bool single_line
, break_names
;
252 break_names
= info
->key_size
> 16 || info
->value_size
> 16;
253 single_line
= info
->key_size
+ info
->value_size
<= 24 &&
256 printf("key:%c", break_names
? '\n' : ' ');
257 fprint_hex(stdout
, key
, info
->key_size
, " ");
259 printf(single_line
? " " : "\n");
261 printf("value:%c", break_names
? '\n' : ' ');
262 fprint_hex(stdout
, value
, info
->value_size
, " ");
268 n
= get_possible_cpus();
271 fprint_hex(stdout
, key
, info
->key_size
, " ");
273 for (i
= 0; i
< n
; i
++) {
274 printf("value (CPU %02d):%c",
275 i
, info
->value_size
> 16 ? '\n' : ' ');
276 fprint_hex(stdout
, value
+ i
* info
->value_size
,
277 info
->value_size
, " ");
283 static char **parse_bytes(char **argv
, const char *name
, unsigned char *val
,
289 while (i
< n
&& argv
[i
]) {
290 val
[i
] = strtoul(argv
[i
], &endptr
, 0);
292 p_err("error parsing byte: %s", argv
[i
]);
299 p_err("%s expected %d bytes got %d", name
, n
, i
);
306 static int parse_elem(char **argv
, struct bpf_map_info
*info
,
307 void *key
, void *value
, __u32 key_size
, __u32 value_size
,
308 __u32
*flags
, __u32
**value_fd
)
313 p_err("did not find %s", key
? "key" : "value");
317 if (is_prefix(*argv
, "key")) {
320 p_err("duplicate key");
322 p_err("unnecessary key");
326 argv
= parse_bytes(argv
+ 1, "key", key
, key_size
);
330 return parse_elem(argv
, info
, NULL
, value
, key_size
, value_size
,
332 } else if (is_prefix(*argv
, "value")) {
337 p_err("duplicate value");
339 p_err("unnecessary value");
345 if (map_is_map_of_maps(info
->type
)) {
348 if (value_size
!= 4) {
349 p_err("value smaller than 4B for map in map?");
352 if (!argv
[0] || !argv
[1]) {
353 p_err("not enough value arguments for map in map");
357 fd
= map_parse_fd(&argc
, &argv
);
363 } else if (map_is_map_of_progs(info
->type
)) {
366 if (value_size
!= 4) {
367 p_err("value smaller than 4B for map of progs?");
370 if (!argv
[0] || !argv
[1]) {
371 p_err("not enough value arguments for map of progs");
375 fd
= prog_parse_fd(&argc
, &argv
);
382 argv
= parse_bytes(argv
, "value", value
, value_size
);
387 return parse_elem(argv
, info
, key
, NULL
, key_size
, value_size
,
389 } else if (is_prefix(*argv
, "any") || is_prefix(*argv
, "noexist") ||
390 is_prefix(*argv
, "exist")) {
392 p_err("flags specified multiple times: %s", *argv
);
396 if (is_prefix(*argv
, "any"))
398 else if (is_prefix(*argv
, "noexist"))
399 *flags
= BPF_NOEXIST
;
400 else if (is_prefix(*argv
, "exist"))
403 return parse_elem(argv
+ 1, info
, key
, value
, key_size
,
404 value_size
, NULL
, value_fd
);
407 p_err("expected key or value, got: %s", *argv
);
411 static int show_map_close_json(int fd
, struct bpf_map_info
*info
)
415 memlock
= get_fdinfo(fd
, "memlock");
418 jsonw_start_object(json_wtr
);
420 jsonw_uint_field(json_wtr
, "id", info
->id
);
421 if (info
->type
< ARRAY_SIZE(map_type_name
))
422 jsonw_string_field(json_wtr
, "type",
423 map_type_name
[info
->type
]);
425 jsonw_uint_field(json_wtr
, "type", info
->type
);
428 jsonw_string_field(json_wtr
, "name", info
->name
);
430 jsonw_name(json_wtr
, "flags");
431 jsonw_printf(json_wtr
, "%#x", info
->map_flags
);
433 print_dev_json(info
->ifindex
, info
->netns_dev
, info
->netns_ino
);
435 jsonw_uint_field(json_wtr
, "bytes_key", info
->key_size
);
436 jsonw_uint_field(json_wtr
, "bytes_value", info
->value_size
);
437 jsonw_uint_field(json_wtr
, "max_entries", info
->max_entries
);
440 jsonw_int_field(json_wtr
, "bytes_memlock", atoi(memlock
));
443 if (!hash_empty(map_table
.table
)) {
444 struct pinned_obj
*obj
;
446 jsonw_name(json_wtr
, "pinned");
447 jsonw_start_array(json_wtr
);
448 hash_for_each_possible(map_table
.table
, obj
, hash
, info
->id
) {
449 if (obj
->id
== info
->id
)
450 jsonw_string(json_wtr
, obj
->path
);
452 jsonw_end_array(json_wtr
);
455 jsonw_end_object(json_wtr
);
460 static int show_map_close_plain(int fd
, struct bpf_map_info
*info
)
464 memlock
= get_fdinfo(fd
, "memlock");
467 printf("%u: ", info
->id
);
468 if (info
->type
< ARRAY_SIZE(map_type_name
))
469 printf("%s ", map_type_name
[info
->type
]);
471 printf("type %u ", info
->type
);
474 printf("name %s ", info
->name
);
476 printf("flags 0x%x", info
->map_flags
);
477 print_dev_plain(info
->ifindex
, info
->netns_dev
, info
->netns_ino
);
479 printf("\tkey %uB value %uB max_entries %u",
480 info
->key_size
, info
->value_size
, info
->max_entries
);
483 printf(" memlock %sB", memlock
);
487 if (!hash_empty(map_table
.table
)) {
488 struct pinned_obj
*obj
;
490 hash_for_each_possible(map_table
.table
, obj
, hash
, info
->id
) {
491 if (obj
->id
== info
->id
)
492 printf("\tpinned %s\n", obj
->path
);
498 static int do_show(int argc
, char **argv
)
500 struct bpf_map_info info
= {};
501 __u32 len
= sizeof(info
);
507 build_pinned_obj_table(&map_table
, BPF_OBJ_MAP
);
510 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
515 return show_map_close_json(fd
, &info
);
517 return show_map_close_plain(fd
, &info
);
524 jsonw_start_array(json_wtr
);
526 err
= bpf_map_get_next_id(id
, &id
);
530 p_err("can't get next map: %s%s", strerror(errno
),
531 errno
== EINVAL
? " -- kernel too old?" : "");
535 fd
= bpf_map_get_fd_by_id(id
);
539 p_err("can't get map by id (%u): %s",
540 id
, strerror(errno
));
544 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
546 p_err("can't get map info: %s", strerror(errno
));
552 show_map_close_json(fd
, &info
);
554 show_map_close_plain(fd
, &info
);
557 jsonw_end_array(json_wtr
);
559 return errno
== ENOENT
? 0 : -1;
562 static int do_dump(int argc
, char **argv
)
564 void *key
, *value
, *prev_key
;
565 unsigned int num_elems
= 0;
566 struct bpf_map_info info
= {};
567 __u32 len
= sizeof(info
);
574 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
578 if (map_is_map_of_maps(info
.type
) || map_is_map_of_progs(info
.type
)) {
579 p_err("Dumping maps of maps and program maps not supported");
584 key
= malloc(info
.key_size
);
585 value
= alloc_value(&info
);
586 if (!key
|| !value
) {
587 p_err("mem alloc failed");
594 jsonw_start_array(json_wtr
);
596 err
= bpf_map_get_next_key(fd
, prev_key
, key
);
603 if (!bpf_map_lookup_elem(fd
, key
, value
)) {
605 print_entry_json(&info
, key
, value
);
607 print_entry_plain(&info
, key
, value
);
610 jsonw_name(json_wtr
, "key");
611 print_hex_data_json(key
, info
.key_size
);
612 jsonw_name(json_wtr
, "value");
613 jsonw_start_object(json_wtr
);
614 jsonw_string_field(json_wtr
, "error",
615 "can't lookup element");
616 jsonw_end_object(json_wtr
);
618 p_info("can't lookup element with key: ");
619 fprint_hex(stderr
, key
, info
.key_size
, " ");
620 fprintf(stderr
, "\n");
629 jsonw_end_array(json_wtr
);
631 printf("Found %u element%s\n", num_elems
,
632 num_elems
!= 1 ? "s" : "");
642 static int do_update(int argc
, char **argv
)
644 struct bpf_map_info info
= {};
645 __u32 len
= sizeof(info
);
646 __u32
*value_fd
= NULL
;
647 __u32 flags
= BPF_ANY
;
654 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
658 key
= malloc(info
.key_size
);
659 value
= alloc_value(&info
);
660 if (!key
|| !value
) {
661 p_err("mem alloc failed");
666 err
= parse_elem(argv
, &info
, key
, value
, info
.key_size
,
667 info
.value_size
, &flags
, &value_fd
);
671 err
= bpf_map_update_elem(fd
, key
, value
, flags
);
673 p_err("update failed: %s", strerror(errno
));
684 if (!err
&& json_output
)
685 jsonw_null(json_wtr
);
689 static int do_lookup(int argc
, char **argv
)
691 struct bpf_map_info info
= {};
692 __u32 len
= sizeof(info
);
700 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
704 key
= malloc(info
.key_size
);
705 value
= alloc_value(&info
);
706 if (!key
|| !value
) {
707 p_err("mem alloc failed");
712 err
= parse_elem(argv
, &info
, key
, NULL
, info
.key_size
, 0, NULL
, NULL
);
716 err
= bpf_map_lookup_elem(fd
, key
, value
);
719 print_entry_json(&info
, key
, value
);
721 print_entry_plain(&info
, key
, value
);
722 } else if (errno
== ENOENT
) {
724 jsonw_null(json_wtr
);
727 fprint_hex(stdout
, key
, info
.key_size
, " ");
728 printf("\n\nNot found\n");
731 p_err("lookup failed: %s", strerror(errno
));
742 static int do_getnext(int argc
, char **argv
)
744 struct bpf_map_info info
= {};
745 __u32 len
= sizeof(info
);
753 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
757 key
= malloc(info
.key_size
);
758 nextkey
= malloc(info
.key_size
);
759 if (!key
|| !nextkey
) {
760 p_err("mem alloc failed");
766 err
= parse_elem(argv
, &info
, key
, NULL
, info
.key_size
, 0,
775 err
= bpf_map_get_next_key(fd
, key
, nextkey
);
777 p_err("can't get next key: %s", strerror(errno
));
782 jsonw_start_object(json_wtr
);
784 jsonw_name(json_wtr
, "key");
785 print_hex_data_json(key
, info
.key_size
);
787 jsonw_null_field(json_wtr
, "key");
789 jsonw_name(json_wtr
, "next_key");
790 print_hex_data_json(nextkey
, info
.key_size
);
791 jsonw_end_object(json_wtr
);
795 fprint_hex(stdout
, key
, info
.key_size
, " ");
798 printf("key: None\n");
800 printf("next key:\n");
801 fprint_hex(stdout
, nextkey
, info
.key_size
, " ");
813 static int do_delete(int argc
, char **argv
)
815 struct bpf_map_info info
= {};
816 __u32 len
= sizeof(info
);
824 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
828 key
= malloc(info
.key_size
);
830 p_err("mem alloc failed");
835 err
= parse_elem(argv
, &info
, key
, NULL
, info
.key_size
, 0, NULL
, NULL
);
839 err
= bpf_map_delete_elem(fd
, key
);
841 p_err("delete failed: %s", strerror(errno
));
847 if (!err
&& json_output
)
848 jsonw_null(json_wtr
);
852 static int do_pin(int argc
, char **argv
)
856 err
= do_pin_any(argc
, argv
, bpf_map_get_fd_by_id
);
857 if (!err
&& json_output
)
858 jsonw_null(json_wtr
);
862 static int do_help(int argc
, char **argv
)
865 jsonw_null(json_wtr
);
870 "Usage: %s %s { show | list } [MAP]\n"
872 " %s %s update MAP key BYTES value VALUE [UPDATE_FLAGS]\n"
873 " %s %s lookup MAP key BYTES\n"
874 " %s %s getnext MAP [key BYTES]\n"
875 " %s %s delete MAP key BYTES\n"
876 " %s %s pin MAP FILE\n"
879 " MAP := { id MAP_ID | pinned FILE }\n"
880 " " HELP_SPEC_PROGRAM
"\n"
881 " VALUE := { BYTES | MAP | PROG }\n"
882 " UPDATE_FLAGS := { any | exist | noexist }\n"
883 " " HELP_SPEC_OPTIONS
"\n"
885 bin_name
, argv
[-2], bin_name
, argv
[-2], bin_name
, argv
[-2],
886 bin_name
, argv
[-2], bin_name
, argv
[-2], bin_name
, argv
[-2],
887 bin_name
, argv
[-2], bin_name
, argv
[-2]);
892 static const struct cmd cmds
[] = {
897 { "update", do_update
},
898 { "lookup", do_lookup
},
899 { "getnext", do_getnext
},
900 { "delete", do_delete
},
905 int do_map(int argc
, char **argv
)
907 return cmd_select(cmds
, argc
, argv
, do_help
);