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>
38 #include <linux/kernel.h>
44 #include <sys/types.h>
50 #include "json_writer.h"
53 static const char * const map_type_name[] = {
54 [BPF_MAP_TYPE_UNSPEC] = "unspec",
55 [BPF_MAP_TYPE_HASH] = "hash",
56 [BPF_MAP_TYPE_ARRAY] = "array",
57 [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
58 [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
59 [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
60 [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
61 [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
62 [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
63 [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
64 [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
65 [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
66 [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
67 [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
68 [BPF_MAP_TYPE_DEVMAP] = "devmap",
69 [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
70 [BPF_MAP_TYPE_CPUMAP] = "cpumap",
71 [BPF_MAP_TYPE_XSKMAP] = "xskmap",
72 [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
73 [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage",
76 static bool map_is_per_cpu(__u32 type)
78 return type == BPF_MAP_TYPE_PERCPU_HASH ||
79 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
80 type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
83 static bool map_is_map_of_maps(__u32 type)
85 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
86 type == BPF_MAP_TYPE_HASH_OF_MAPS;
89 static bool map_is_map_of_progs(__u32 type)
91 return type == BPF_MAP_TYPE_PROG_ARRAY;
94 static void *alloc_value(struct bpf_map_info *info)
96 if (map_is_per_cpu(info->type))
97 return malloc(round_up(info->value_size, 8) *
100 return malloc(info->value_size);
103 int map_parse_fd(int *argc, char ***argv)
107 if (is_prefix(**argv, "id")) {
113 id = strtoul(**argv, &endptr, 0);
115 p_err("can't parse %s as ID", **argv);
120 fd = bpf_map_get_fd_by_id(id);
122 p_err("get map by id (%u): %s", id, strerror(errno));
124 } else if (is_prefix(**argv, "pinned")) {
132 return open_obj_pinned_any(path, BPF_OBJ_MAP);
135 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
139 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
144 fd = map_parse_fd(argc, argv);
148 err = bpf_obj_get_info_by_fd(fd, info, info_len);
150 p_err("can't get map info: %s", strerror(errno));
158 static int do_dump_btf(const struct btf_dumper *d,
159 struct bpf_map_info *map_info, void *key,
164 /* start of key-value pair */
165 jsonw_start_object(d->jw);
167 jsonw_name(d->jw, "key");
169 ret = btf_dumper_type(d, map_info->btf_key_type_id, key);
173 jsonw_name(d->jw, "value");
175 ret = btf_dumper_type(d, map_info->btf_value_type_id, value);
178 /* end of key-value pair */
179 jsonw_end_object(d->jw);
184 static int get_btf(struct bpf_map_info *map_info, struct btf **btf)
186 struct bpf_btf_info btf_info = { 0 };
187 __u32 len = sizeof(btf_info);
195 btf_fd = bpf_btf_get_fd_by_id(map_info->btf_id);
199 /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
200 * let's start with a sane default - 4KiB here - and resize it only if
201 * bpf_obj_get_info_by_fd() needs a bigger buffer.
203 btf_info.btf_size = 4096;
204 last_size = btf_info.btf_size;
205 ptr = malloc(last_size);
211 bzero(ptr, last_size);
212 btf_info.btf = ptr_to_u64(ptr);
213 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
215 if (!err && btf_info.btf_size > last_size) {
218 last_size = btf_info.btf_size;
219 temp_ptr = realloc(ptr, last_size);
225 bzero(ptr, last_size);
226 btf_info.btf = ptr_to_u64(ptr);
227 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
230 if (err || btf_info.btf_size > last_size) {
235 *btf = btf__new((__u8 *)btf_info.btf, btf_info.btf_size, NULL);
248 static json_writer_t *get_btf_writer(void)
250 json_writer_t *jw = jsonw_new(stdout);
254 jsonw_pretty(jw, true);
259 static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
260 unsigned char *value, struct btf *btf)
262 jsonw_start_object(json_wtr);
264 if (!map_is_per_cpu(info->type)) {
265 jsonw_name(json_wtr, "key");
266 print_hex_data_json(key, info->key_size);
267 jsonw_name(json_wtr, "value");
268 print_hex_data_json(value, info->value_size);
270 struct btf_dumper d = {
273 .is_plain_text = false,
276 jsonw_name(json_wtr, "formatted");
277 do_dump_btf(&d, info, key, value);
280 unsigned int i, n, step;
282 n = get_possible_cpus();
283 step = round_up(info->value_size, 8);
285 jsonw_name(json_wtr, "key");
286 print_hex_data_json(key, info->key_size);
288 jsonw_name(json_wtr, "values");
289 jsonw_start_array(json_wtr);
290 for (i = 0; i < n; i++) {
291 jsonw_start_object(json_wtr);
293 jsonw_int_field(json_wtr, "cpu", i);
295 jsonw_name(json_wtr, "value");
296 print_hex_data_json(value + i * step,
299 jsonw_end_object(json_wtr);
301 jsonw_end_array(json_wtr);
304 jsonw_end_object(json_wtr);
307 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
308 unsigned char *value)
310 if (!map_is_per_cpu(info->type)) {
311 bool single_line, break_names;
313 break_names = info->key_size > 16 || info->value_size > 16;
314 single_line = info->key_size + info->value_size <= 24 &&
317 printf("key:%c", break_names ? '\n' : ' ');
318 fprint_hex(stdout, key, info->key_size, " ");
320 printf(single_line ? " " : "\n");
322 printf("value:%c", break_names ? '\n' : ' ');
323 fprint_hex(stdout, value, info->value_size, " ");
327 unsigned int i, n, step;
329 n = get_possible_cpus();
330 step = round_up(info->value_size, 8);
333 fprint_hex(stdout, key, info->key_size, " ");
335 for (i = 0; i < n; i++) {
336 printf("value (CPU %02d):%c",
337 i, info->value_size > 16 ? '\n' : ' ');
338 fprint_hex(stdout, value + i * step,
339 info->value_size, " ");
345 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
348 unsigned int i = 0, base = 0;
351 if (is_prefix(*argv, "hex")) {
356 while (i < n && argv[i]) {
357 val[i] = strtoul(argv[i], &endptr, base);
359 p_err("error parsing byte: %s", argv[i]);
366 p_err("%s expected %d bytes got %d", name, n, i);
373 /* on per cpu maps we must copy the provided value on all value instances */
374 static void fill_per_cpu_value(struct bpf_map_info *info, void *value)
376 unsigned int i, n, step;
378 if (!map_is_per_cpu(info->type))
381 n = get_possible_cpus();
382 step = round_up(info->value_size, 8);
383 for (i = 1; i < n; i++)
384 memcpy(value + i * step, value, info->value_size);
387 static int parse_elem(char **argv, struct bpf_map_info *info,
388 void *key, void *value, __u32 key_size, __u32 value_size,
389 __u32 *flags, __u32 **value_fd)
394 p_err("did not find %s", key ? "key" : "value");
398 if (is_prefix(*argv, "key")) {
401 p_err("duplicate key");
403 p_err("unnecessary key");
407 argv = parse_bytes(argv + 1, "key", key, key_size);
411 return parse_elem(argv, info, NULL, value, key_size, value_size,
413 } else if (is_prefix(*argv, "value")) {
418 p_err("duplicate value");
420 p_err("unnecessary value");
426 if (map_is_map_of_maps(info->type)) {
429 if (value_size != 4) {
430 p_err("value smaller than 4B for map in map?");
433 if (!argv[0] || !argv[1]) {
434 p_err("not enough value arguments for map in map");
438 fd = map_parse_fd(&argc, &argv);
444 } else if (map_is_map_of_progs(info->type)) {
447 if (value_size != 4) {
448 p_err("value smaller than 4B for map of progs?");
451 if (!argv[0] || !argv[1]) {
452 p_err("not enough value arguments for map of progs");
456 fd = prog_parse_fd(&argc, &argv);
463 argv = parse_bytes(argv, "value", value, value_size);
467 fill_per_cpu_value(info, value);
470 return parse_elem(argv, info, key, NULL, key_size, value_size,
472 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
473 is_prefix(*argv, "exist")) {
475 p_err("flags specified multiple times: %s", *argv);
479 if (is_prefix(*argv, "any"))
481 else if (is_prefix(*argv, "noexist"))
482 *flags = BPF_NOEXIST;
483 else if (is_prefix(*argv, "exist"))
486 return parse_elem(argv + 1, info, key, value, key_size,
487 value_size, NULL, value_fd);
490 p_err("expected key or value, got: %s", *argv);
494 static int show_map_close_json(int fd, struct bpf_map_info *info)
498 memlock = get_fdinfo(fd, "memlock");
501 jsonw_start_object(json_wtr);
503 jsonw_uint_field(json_wtr, "id", info->id);
504 if (info->type < ARRAY_SIZE(map_type_name))
505 jsonw_string_field(json_wtr, "type",
506 map_type_name[info->type]);
508 jsonw_uint_field(json_wtr, "type", info->type);
511 jsonw_string_field(json_wtr, "name", info->name);
513 jsonw_name(json_wtr, "flags");
514 jsonw_printf(json_wtr, "%d", info->map_flags);
516 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
518 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
519 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
520 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
523 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
526 if (!hash_empty(map_table.table)) {
527 struct pinned_obj *obj;
529 jsonw_name(json_wtr, "pinned");
530 jsonw_start_array(json_wtr);
531 hash_for_each_possible(map_table.table, obj, hash, info->id) {
532 if (obj->id == info->id)
533 jsonw_string(json_wtr, obj->path);
535 jsonw_end_array(json_wtr);
538 jsonw_end_object(json_wtr);
543 static int show_map_close_plain(int fd, struct bpf_map_info *info)
547 memlock = get_fdinfo(fd, "memlock");
550 printf("%u: ", info->id);
551 if (info->type < ARRAY_SIZE(map_type_name))
552 printf("%s ", map_type_name[info->type]);
554 printf("type %u ", info->type);
557 printf("name %s ", info->name);
559 printf("flags 0x%x", info->map_flags);
560 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
562 printf("\tkey %uB value %uB max_entries %u",
563 info->key_size, info->value_size, info->max_entries);
566 printf(" memlock %sB", memlock);
570 if (!hash_empty(map_table.table)) {
571 struct pinned_obj *obj;
573 hash_for_each_possible(map_table.table, obj, hash, info->id) {
574 if (obj->id == info->id)
575 printf("\tpinned %s\n", obj->path);
581 static int do_show(int argc, char **argv)
583 struct bpf_map_info info = {};
584 __u32 len = sizeof(info);
590 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
593 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
598 return show_map_close_json(fd, &info);
600 return show_map_close_plain(fd, &info);
607 jsonw_start_array(json_wtr);
609 err = bpf_map_get_next_id(id, &id);
613 p_err("can't get next map: %s%s", strerror(errno),
614 errno == EINVAL ? " -- kernel too old?" : "");
618 fd = bpf_map_get_fd_by_id(id);
622 p_err("can't get map by id (%u): %s",
623 id, strerror(errno));
627 err = bpf_obj_get_info_by_fd(fd, &info, &len);
629 p_err("can't get map info: %s", strerror(errno));
635 show_map_close_json(fd, &info);
637 show_map_close_plain(fd, &info);
640 jsonw_end_array(json_wtr);
642 return errno == ENOENT ? 0 : -1;
645 static int do_dump(int argc, char **argv)
647 struct bpf_map_info info = {};
648 void *key, *value, *prev_key;
649 unsigned int num_elems = 0;
650 __u32 len = sizeof(info);
651 json_writer_t *btf_wtr;
652 struct btf *btf = NULL;
659 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
663 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
664 p_err("Dumping maps of maps and program maps not supported");
669 key = malloc(info.key_size);
670 value = alloc_value(&info);
671 if (!key || !value) {
672 p_err("mem alloc failed");
679 err = get_btf(&info, &btf);
681 p_err("failed to get btf");
686 jsonw_start_array(json_wtr);
689 btf_wtr = get_btf_writer();
691 p_info("failed to create json writer for btf. falling back to plain output");
695 jsonw_start_array(btf_wtr);
700 err = bpf_map_get_next_key(fd, prev_key, key);
707 if (!bpf_map_lookup_elem(fd, key, value)) {
709 print_entry_json(&info, key, value, btf);
712 struct btf_dumper d = {
715 .is_plain_text = true,
718 do_dump_btf(&d, &info, key, value);
720 print_entry_plain(&info, key, value);
724 jsonw_name(json_wtr, "key");
725 print_hex_data_json(key, info.key_size);
726 jsonw_name(json_wtr, "value");
727 jsonw_start_object(json_wtr);
728 jsonw_string_field(json_wtr, "error",
729 "can't lookup element");
730 jsonw_end_object(json_wtr);
732 p_info("can't lookup element with key: ");
733 fprint_hex(stderr, key, info.key_size, " ");
734 fprintf(stderr, "\n");
743 jsonw_end_array(json_wtr);
745 jsonw_end_array(btf_wtr);
746 jsonw_destroy(&btf_wtr);
748 printf("Found %u element%s\n", num_elems,
749 num_elems != 1 ? "s" : "");
761 static int do_update(int argc, char **argv)
763 struct bpf_map_info info = {};
764 __u32 len = sizeof(info);
765 __u32 *value_fd = NULL;
766 __u32 flags = BPF_ANY;
773 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
777 key = malloc(info.key_size);
778 value = alloc_value(&info);
779 if (!key || !value) {
780 p_err("mem alloc failed");
785 err = parse_elem(argv, &info, key, value, info.key_size,
786 info.value_size, &flags, &value_fd);
790 err = bpf_map_update_elem(fd, key, value, flags);
792 p_err("update failed: %s", strerror(errno));
803 if (!err && json_output)
804 jsonw_null(json_wtr);
808 static int do_lookup(int argc, char **argv)
810 struct bpf_map_info info = {};
811 __u32 len = sizeof(info);
812 json_writer_t *btf_wtr;
813 struct btf *btf = NULL;
821 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
825 key = malloc(info.key_size);
826 value = alloc_value(&info);
827 if (!key || !value) {
828 p_err("mem alloc failed");
833 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
837 err = bpf_map_lookup_elem(fd, key, value);
839 if (errno == ENOENT) {
841 jsonw_null(json_wtr);
844 fprint_hex(stdout, key, info.key_size, " ");
845 printf("\n\nNot found\n");
848 p_err("lookup failed: %s", strerror(errno));
854 /* here means bpf_map_lookup_elem() succeeded */
855 err = get_btf(&info, &btf);
857 p_err("failed to get btf");
862 print_entry_json(&info, key, value, btf);
864 /* if here json_wtr wouldn't have been initialised,
865 * so let's create separate writer for btf
867 btf_wtr = get_btf_writer();
869 p_info("failed to create json writer for btf. falling back to plain output");
872 print_entry_plain(&info, key, value);
874 struct btf_dumper d = {
877 .is_plain_text = true,
880 do_dump_btf(&d, &info, key, value);
881 jsonw_destroy(&btf_wtr);
884 print_entry_plain(&info, key, value);
896 static int do_getnext(int argc, char **argv)
898 struct bpf_map_info info = {};
899 __u32 len = sizeof(info);
907 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
911 key = malloc(info.key_size);
912 nextkey = malloc(info.key_size);
913 if (!key || !nextkey) {
914 p_err("mem alloc failed");
920 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
929 err = bpf_map_get_next_key(fd, key, nextkey);
931 p_err("can't get next key: %s", strerror(errno));
936 jsonw_start_object(json_wtr);
938 jsonw_name(json_wtr, "key");
939 print_hex_data_json(key, info.key_size);
941 jsonw_null_field(json_wtr, "key");
943 jsonw_name(json_wtr, "next_key");
944 print_hex_data_json(nextkey, info.key_size);
945 jsonw_end_object(json_wtr);
949 fprint_hex(stdout, key, info.key_size, " ");
952 printf("key: None\n");
954 printf("next key:\n");
955 fprint_hex(stdout, nextkey, info.key_size, " ");
967 static int do_delete(int argc, char **argv)
969 struct bpf_map_info info = {};
970 __u32 len = sizeof(info);
978 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
982 key = malloc(info.key_size);
984 p_err("mem alloc failed");
989 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
993 err = bpf_map_delete_elem(fd, key);
995 p_err("delete failed: %s", strerror(errno));
1001 if (!err && json_output)
1002 jsonw_null(json_wtr);
1006 static int do_pin(int argc, char **argv)
1010 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
1011 if (!err && json_output)
1012 jsonw_null(json_wtr);
1016 static int do_help(int argc, char **argv)
1019 jsonw_null(json_wtr);
1024 "Usage: %s %s { show | list } [MAP]\n"
1026 " %s %s update MAP key DATA value VALUE [UPDATE_FLAGS]\n"
1027 " %s %s lookup MAP key DATA\n"
1028 " %s %s getnext MAP [key DATA]\n"
1029 " %s %s delete MAP key DATA\n"
1030 " %s %s pin MAP FILE\n"
1031 " %s %s event_pipe MAP [cpu N index M]\n"
1034 " " HELP_SPEC_MAP "\n"
1035 " DATA := { [hex] BYTES }\n"
1036 " " HELP_SPEC_PROGRAM "\n"
1037 " VALUE := { DATA | MAP | PROG }\n"
1038 " UPDATE_FLAGS := { any | exist | noexist }\n"
1039 " " HELP_SPEC_OPTIONS "\n"
1041 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1042 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1043 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
1048 static const struct cmd cmds[] = {
1049 { "show", do_show },
1050 { "list", do_show },
1051 { "help", do_help },
1052 { "dump", do_dump },
1053 { "update", do_update },
1054 { "lookup", do_lookup },
1055 { "getnext", do_getnext },
1056 { "delete", do_delete },
1058 { "event_pipe", do_event_pipe },
1062 int do_map(int argc, char **argv)
1064 return cmd_select(cmds, argc, argv, do_help);