2 * Copyright (C) 2017-2018 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
37 #include <linux/err.h>
43 #include <sys/types.h>
49 #include "json_writer.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",
70 [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
71 [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage",
74 static bool map_is_per_cpu(__u32 type)
76 return type == BPF_MAP_TYPE_PERCPU_HASH ||
77 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
78 type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
81 static bool map_is_map_of_maps(__u32 type)
83 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
84 type == BPF_MAP_TYPE_HASH_OF_MAPS;
87 static bool map_is_map_of_progs(__u32 type)
89 return type == BPF_MAP_TYPE_PROG_ARRAY;
92 static void *alloc_value(struct bpf_map_info *info)
94 if (map_is_per_cpu(info->type))
95 return malloc(info->value_size * get_possible_cpus());
97 return malloc(info->value_size);
100 int map_parse_fd(int *argc, char ***argv)
104 if (is_prefix(**argv, "id")) {
110 id = strtoul(**argv, &endptr, 0);
112 p_err("can't parse %s as ID", **argv);
117 fd = bpf_map_get_fd_by_id(id);
119 p_err("get map by id (%u): %s", id, strerror(errno));
121 } else if (is_prefix(**argv, "pinned")) {
129 return open_obj_pinned_any(path, BPF_OBJ_MAP);
132 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
136 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
141 fd = map_parse_fd(argc, argv);
145 err = bpf_obj_get_info_by_fd(fd, info, info_len);
147 p_err("can't get map info: %s", strerror(errno));
155 static int do_dump_btf(const struct btf_dumper *d,
156 struct bpf_map_info *map_info, void *key,
161 /* start of key-value pair */
162 jsonw_start_object(d->jw);
164 jsonw_name(d->jw, "key");
166 ret = btf_dumper_type(d, map_info->btf_key_type_id, key);
170 jsonw_name(d->jw, "value");
172 ret = btf_dumper_type(d, map_info->btf_value_type_id, value);
175 /* end of key-value pair */
176 jsonw_end_object(d->jw);
181 static int get_btf(struct bpf_map_info *map_info, struct btf **btf)
183 struct bpf_btf_info btf_info = { 0 };
184 __u32 len = sizeof(btf_info);
192 btf_fd = bpf_btf_get_fd_by_id(map_info->btf_id);
196 /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
197 * let's start with a sane default - 4KiB here - and resize it only if
198 * bpf_obj_get_info_by_fd() needs a bigger buffer.
200 btf_info.btf_size = 4096;
201 last_size = btf_info.btf_size;
202 ptr = malloc(last_size);
208 bzero(ptr, last_size);
209 btf_info.btf = ptr_to_u64(ptr);
210 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
212 if (!err && btf_info.btf_size > last_size) {
215 last_size = btf_info.btf_size;
216 temp_ptr = realloc(ptr, last_size);
222 bzero(ptr, last_size);
223 btf_info.btf = ptr_to_u64(ptr);
224 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
227 if (err || btf_info.btf_size > last_size) {
232 *btf = btf__new((__u8 *)btf_info.btf, btf_info.btf_size, NULL);
245 static json_writer_t *get_btf_writer(void)
247 json_writer_t *jw = jsonw_new(stdout);
251 jsonw_pretty(jw, true);
256 static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
257 unsigned char *value, struct btf *btf)
259 jsonw_start_object(json_wtr);
261 if (!map_is_per_cpu(info->type)) {
262 jsonw_name(json_wtr, "key");
263 print_hex_data_json(key, info->key_size);
264 jsonw_name(json_wtr, "value");
265 print_hex_data_json(value, info->value_size);
267 struct btf_dumper d = {
270 .is_plain_text = false,
273 jsonw_name(json_wtr, "formatted");
274 do_dump_btf(&d, info, key, value);
279 n = get_possible_cpus();
281 jsonw_name(json_wtr, "key");
282 print_hex_data_json(key, info->key_size);
284 jsonw_name(json_wtr, "values");
285 jsonw_start_array(json_wtr);
286 for (i = 0; i < n; i++) {
287 jsonw_start_object(json_wtr);
289 jsonw_int_field(json_wtr, "cpu", i);
291 jsonw_name(json_wtr, "value");
292 print_hex_data_json(value + i * info->value_size,
295 jsonw_end_object(json_wtr);
297 jsonw_end_array(json_wtr);
300 jsonw_end_object(json_wtr);
303 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
304 unsigned char *value)
306 if (!map_is_per_cpu(info->type)) {
307 bool single_line, break_names;
309 break_names = info->key_size > 16 || info->value_size > 16;
310 single_line = info->key_size + info->value_size <= 24 &&
313 printf("key:%c", break_names ? '\n' : ' ');
314 fprint_hex(stdout, key, info->key_size, " ");
316 printf(single_line ? " " : "\n");
318 printf("value:%c", break_names ? '\n' : ' ');
319 fprint_hex(stdout, value, info->value_size, " ");
325 n = get_possible_cpus();
328 fprint_hex(stdout, key, info->key_size, " ");
330 for (i = 0; i < n; i++) {
331 printf("value (CPU %02d):%c",
332 i, info->value_size > 16 ? '\n' : ' ');
333 fprint_hex(stdout, value + i * info->value_size,
334 info->value_size, " ");
340 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
343 unsigned int i = 0, base = 0;
346 if (is_prefix(*argv, "hex")) {
351 while (i < n && argv[i]) {
352 val[i] = strtoul(argv[i], &endptr, base);
354 p_err("error parsing byte: %s", argv[i]);
361 p_err("%s expected %d bytes got %d", name, n, i);
368 static int parse_elem(char **argv, struct bpf_map_info *info,
369 void *key, void *value, __u32 key_size, __u32 value_size,
370 __u32 *flags, __u32 **value_fd)
375 p_err("did not find %s", key ? "key" : "value");
379 if (is_prefix(*argv, "key")) {
382 p_err("duplicate key");
384 p_err("unnecessary key");
388 argv = parse_bytes(argv + 1, "key", key, key_size);
392 return parse_elem(argv, info, NULL, value, key_size, value_size,
394 } else if (is_prefix(*argv, "value")) {
399 p_err("duplicate value");
401 p_err("unnecessary value");
407 if (map_is_map_of_maps(info->type)) {
410 if (value_size != 4) {
411 p_err("value smaller than 4B for map in map?");
414 if (!argv[0] || !argv[1]) {
415 p_err("not enough value arguments for map in map");
419 fd = map_parse_fd(&argc, &argv);
425 } else if (map_is_map_of_progs(info->type)) {
428 if (value_size != 4) {
429 p_err("value smaller than 4B for map of progs?");
432 if (!argv[0] || !argv[1]) {
433 p_err("not enough value arguments for map of progs");
437 fd = prog_parse_fd(&argc, &argv);
444 argv = parse_bytes(argv, "value", value, value_size);
449 return parse_elem(argv, info, key, NULL, key_size, value_size,
451 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
452 is_prefix(*argv, "exist")) {
454 p_err("flags specified multiple times: %s", *argv);
458 if (is_prefix(*argv, "any"))
460 else if (is_prefix(*argv, "noexist"))
461 *flags = BPF_NOEXIST;
462 else if (is_prefix(*argv, "exist"))
465 return parse_elem(argv + 1, info, key, value, key_size,
466 value_size, NULL, value_fd);
469 p_err("expected key or value, got: %s", *argv);
473 static int show_map_close_json(int fd, struct bpf_map_info *info)
477 memlock = get_fdinfo(fd, "memlock");
480 jsonw_start_object(json_wtr);
482 jsonw_uint_field(json_wtr, "id", info->id);
483 if (info->type < ARRAY_SIZE(map_type_name))
484 jsonw_string_field(json_wtr, "type",
485 map_type_name[info->type]);
487 jsonw_uint_field(json_wtr, "type", info->type);
490 jsonw_string_field(json_wtr, "name", info->name);
492 jsonw_name(json_wtr, "flags");
493 jsonw_printf(json_wtr, "%d", info->map_flags);
495 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
497 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
498 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
499 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
502 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
505 if (!hash_empty(map_table.table)) {
506 struct pinned_obj *obj;
508 jsonw_name(json_wtr, "pinned");
509 jsonw_start_array(json_wtr);
510 hash_for_each_possible(map_table.table, obj, hash, info->id) {
511 if (obj->id == info->id)
512 jsonw_string(json_wtr, obj->path);
514 jsonw_end_array(json_wtr);
517 jsonw_end_object(json_wtr);
522 static int show_map_close_plain(int fd, struct bpf_map_info *info)
526 memlock = get_fdinfo(fd, "memlock");
529 printf("%u: ", info->id);
530 if (info->type < ARRAY_SIZE(map_type_name))
531 printf("%s ", map_type_name[info->type]);
533 printf("type %u ", info->type);
536 printf("name %s ", info->name);
538 printf("flags 0x%x", info->map_flags);
539 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
541 printf("\tkey %uB value %uB max_entries %u",
542 info->key_size, info->value_size, info->max_entries);
545 printf(" memlock %sB", memlock);
549 if (!hash_empty(map_table.table)) {
550 struct pinned_obj *obj;
552 hash_for_each_possible(map_table.table, obj, hash, info->id) {
553 if (obj->id == info->id)
554 printf("\tpinned %s\n", obj->path);
560 static int do_show(int argc, char **argv)
562 struct bpf_map_info info = {};
563 __u32 len = sizeof(info);
569 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
572 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
577 return show_map_close_json(fd, &info);
579 return show_map_close_plain(fd, &info);
586 jsonw_start_array(json_wtr);
588 err = bpf_map_get_next_id(id, &id);
592 p_err("can't get next map: %s%s", strerror(errno),
593 errno == EINVAL ? " -- kernel too old?" : "");
597 fd = bpf_map_get_fd_by_id(id);
601 p_err("can't get map by id (%u): %s",
602 id, strerror(errno));
606 err = bpf_obj_get_info_by_fd(fd, &info, &len);
608 p_err("can't get map info: %s", strerror(errno));
614 show_map_close_json(fd, &info);
616 show_map_close_plain(fd, &info);
619 jsonw_end_array(json_wtr);
621 return errno == ENOENT ? 0 : -1;
624 static int do_dump(int argc, char **argv)
626 struct bpf_map_info info = {};
627 void *key, *value, *prev_key;
628 unsigned int num_elems = 0;
629 __u32 len = sizeof(info);
630 json_writer_t *btf_wtr;
631 struct btf *btf = NULL;
638 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
642 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
643 p_err("Dumping maps of maps and program maps not supported");
648 key = malloc(info.key_size);
649 value = alloc_value(&info);
650 if (!key || !value) {
651 p_err("mem alloc failed");
658 err = get_btf(&info, &btf);
660 p_err("failed to get btf");
665 jsonw_start_array(json_wtr);
668 btf_wtr = get_btf_writer();
670 p_info("failed to create json writer for btf. falling back to plain output");
674 jsonw_start_array(btf_wtr);
679 err = bpf_map_get_next_key(fd, prev_key, key);
686 if (!bpf_map_lookup_elem(fd, key, value)) {
688 print_entry_json(&info, key, value, btf);
691 struct btf_dumper d = {
694 .is_plain_text = true,
697 do_dump_btf(&d, &info, key, value);
699 print_entry_plain(&info, key, value);
703 jsonw_name(json_wtr, "key");
704 print_hex_data_json(key, info.key_size);
705 jsonw_name(json_wtr, "value");
706 jsonw_start_object(json_wtr);
707 jsonw_string_field(json_wtr, "error",
708 "can't lookup element");
709 jsonw_end_object(json_wtr);
711 p_info("can't lookup element with key: ");
712 fprint_hex(stderr, key, info.key_size, " ");
713 fprintf(stderr, "\n");
722 jsonw_end_array(json_wtr);
724 jsonw_end_array(btf_wtr);
725 jsonw_destroy(&btf_wtr);
727 printf("Found %u element%s\n", num_elems,
728 num_elems != 1 ? "s" : "");
740 static int do_update(int argc, char **argv)
742 struct bpf_map_info info = {};
743 __u32 len = sizeof(info);
744 __u32 *value_fd = NULL;
745 __u32 flags = BPF_ANY;
752 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
756 key = malloc(info.key_size);
757 value = alloc_value(&info);
758 if (!key || !value) {
759 p_err("mem alloc failed");
764 err = parse_elem(argv, &info, key, value, info.key_size,
765 info.value_size, &flags, &value_fd);
769 err = bpf_map_update_elem(fd, key, value, flags);
771 p_err("update failed: %s", strerror(errno));
782 if (!err && json_output)
783 jsonw_null(json_wtr);
787 static int do_lookup(int argc, char **argv)
789 struct bpf_map_info info = {};
790 __u32 len = sizeof(info);
791 json_writer_t *btf_wtr;
792 struct btf *btf = NULL;
800 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
804 key = malloc(info.key_size);
805 value = alloc_value(&info);
806 if (!key || !value) {
807 p_err("mem alloc failed");
812 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
816 err = bpf_map_lookup_elem(fd, key, value);
818 if (errno == ENOENT) {
820 jsonw_null(json_wtr);
823 fprint_hex(stdout, key, info.key_size, " ");
824 printf("\n\nNot found\n");
827 p_err("lookup failed: %s", strerror(errno));
833 /* here means bpf_map_lookup_elem() succeeded */
834 err = get_btf(&info, &btf);
836 p_err("failed to get btf");
841 print_entry_json(&info, key, value, btf);
843 /* if here json_wtr wouldn't have been initialised,
844 * so let's create separate writer for btf
846 btf_wtr = get_btf_writer();
848 p_info("failed to create json writer for btf. falling back to plain output");
851 print_entry_plain(&info, key, value);
853 struct btf_dumper d = {
856 .is_plain_text = true,
859 do_dump_btf(&d, &info, key, value);
860 jsonw_destroy(&btf_wtr);
863 print_entry_plain(&info, key, value);
875 static int do_getnext(int argc, char **argv)
877 struct bpf_map_info info = {};
878 __u32 len = sizeof(info);
886 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
890 key = malloc(info.key_size);
891 nextkey = malloc(info.key_size);
892 if (!key || !nextkey) {
893 p_err("mem alloc failed");
899 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
908 err = bpf_map_get_next_key(fd, key, nextkey);
910 p_err("can't get next key: %s", strerror(errno));
915 jsonw_start_object(json_wtr);
917 jsonw_name(json_wtr, "key");
918 print_hex_data_json(key, info.key_size);
920 jsonw_null_field(json_wtr, "key");
922 jsonw_name(json_wtr, "next_key");
923 print_hex_data_json(nextkey, info.key_size);
924 jsonw_end_object(json_wtr);
928 fprint_hex(stdout, key, info.key_size, " ");
931 printf("key: None\n");
933 printf("next key:\n");
934 fprint_hex(stdout, nextkey, info.key_size, " ");
946 static int do_delete(int argc, char **argv)
948 struct bpf_map_info info = {};
949 __u32 len = sizeof(info);
957 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
961 key = malloc(info.key_size);
963 p_err("mem alloc failed");
968 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
972 err = bpf_map_delete_elem(fd, key);
974 p_err("delete failed: %s", strerror(errno));
980 if (!err && json_output)
981 jsonw_null(json_wtr);
985 static int do_pin(int argc, char **argv)
989 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
990 if (!err && json_output)
991 jsonw_null(json_wtr);
995 static int do_help(int argc, char **argv)
998 jsonw_null(json_wtr);
1003 "Usage: %s %s { show | list } [MAP]\n"
1005 " %s %s update MAP key DATA value VALUE [UPDATE_FLAGS]\n"
1006 " %s %s lookup MAP key DATA\n"
1007 " %s %s getnext MAP [key DATA]\n"
1008 " %s %s delete MAP key DATA\n"
1009 " %s %s pin MAP FILE\n"
1010 " %s %s event_pipe MAP [cpu N index M]\n"
1013 " " HELP_SPEC_MAP "\n"
1014 " DATA := { [hex] BYTES }\n"
1015 " " HELP_SPEC_PROGRAM "\n"
1016 " VALUE := { DATA | MAP | PROG }\n"
1017 " UPDATE_FLAGS := { any | exist | noexist }\n"
1018 " " HELP_SPEC_OPTIONS "\n"
1020 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1021 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1022 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
1027 static const struct cmd cmds[] = {
1028 { "show", do_show },
1029 { "list", do_show },
1030 { "help", do_help },
1031 { "dump", do_dump },
1032 { "update", do_update },
1033 { "lookup", do_lookup },
1034 { "getnext", do_getnext },
1035 { "delete", do_delete },
1037 { "event_pipe", do_event_pipe },
1041 int do_map(int argc, char **argv)
1043 return cmd_select(cmds, argc, argv, do_help);