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_SOCKHASH] = "sockhash",
72 [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage",
75 static bool map_is_per_cpu(__u32 type)
77 return type == BPF_MAP_TYPE_PERCPU_HASH ||
78 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
79 type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
82 static bool map_is_map_of_maps(__u32 type)
84 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
85 type == BPF_MAP_TYPE_HASH_OF_MAPS;
88 static bool map_is_map_of_progs(__u32 type)
90 return type == BPF_MAP_TYPE_PROG_ARRAY;
93 static void *alloc_value(struct bpf_map_info *info)
95 if (map_is_per_cpu(info->type))
96 return malloc(round_up(info->value_size, 8) *
99 return malloc(info->value_size);
102 int map_parse_fd(int *argc, char ***argv)
106 if (is_prefix(**argv, "id")) {
112 id = strtoul(**argv, &endptr, 0);
114 p_err("can't parse %s as ID", **argv);
119 fd = bpf_map_get_fd_by_id(id);
121 p_err("get map by id (%u): %s", id, strerror(errno));
123 } else if (is_prefix(**argv, "pinned")) {
131 return open_obj_pinned_any(path, BPF_OBJ_MAP);
134 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
138 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
143 fd = map_parse_fd(argc, argv);
147 err = bpf_obj_get_info_by_fd(fd, info, info_len);
149 p_err("can't get map info: %s", strerror(errno));
157 static int do_dump_btf(const struct btf_dumper *d,
158 struct bpf_map_info *map_info, void *key,
163 /* start of key-value pair */
164 jsonw_start_object(d->jw);
166 jsonw_name(d->jw, "key");
168 ret = btf_dumper_type(d, map_info->btf_key_type_id, key);
172 jsonw_name(d->jw, "value");
174 ret = btf_dumper_type(d, map_info->btf_value_type_id, value);
177 /* end of key-value pair */
178 jsonw_end_object(d->jw);
183 static int get_btf(struct bpf_map_info *map_info, struct btf **btf)
185 struct bpf_btf_info btf_info = { 0 };
186 __u32 len = sizeof(btf_info);
194 btf_fd = bpf_btf_get_fd_by_id(map_info->btf_id);
198 /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
199 * let's start with a sane default - 4KiB here - and resize it only if
200 * bpf_obj_get_info_by_fd() needs a bigger buffer.
202 btf_info.btf_size = 4096;
203 last_size = btf_info.btf_size;
204 ptr = malloc(last_size);
210 bzero(ptr, last_size);
211 btf_info.btf = ptr_to_u64(ptr);
212 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
214 if (!err && btf_info.btf_size > last_size) {
217 last_size = btf_info.btf_size;
218 temp_ptr = realloc(ptr, last_size);
224 bzero(ptr, last_size);
225 btf_info.btf = ptr_to_u64(ptr);
226 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
229 if (err || btf_info.btf_size > last_size) {
234 *btf = btf__new((__u8 *)btf_info.btf, btf_info.btf_size, NULL);
247 static json_writer_t *get_btf_writer(void)
249 json_writer_t *jw = jsonw_new(stdout);
253 jsonw_pretty(jw, true);
258 static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
259 unsigned char *value, struct btf *btf)
261 jsonw_start_object(json_wtr);
263 if (!map_is_per_cpu(info->type)) {
264 jsonw_name(json_wtr, "key");
265 print_hex_data_json(key, info->key_size);
266 jsonw_name(json_wtr, "value");
267 print_hex_data_json(value, info->value_size);
269 struct btf_dumper d = {
272 .is_plain_text = false,
275 jsonw_name(json_wtr, "formatted");
276 do_dump_btf(&d, info, key, value);
279 unsigned int i, n, step;
281 n = get_possible_cpus();
282 step = round_up(info->value_size, 8);
284 jsonw_name(json_wtr, "key");
285 print_hex_data_json(key, info->key_size);
287 jsonw_name(json_wtr, "values");
288 jsonw_start_array(json_wtr);
289 for (i = 0; i < n; i++) {
290 jsonw_start_object(json_wtr);
292 jsonw_int_field(json_wtr, "cpu", i);
294 jsonw_name(json_wtr, "value");
295 print_hex_data_json(value + i * step,
298 jsonw_end_object(json_wtr);
300 jsonw_end_array(json_wtr);
303 jsonw_end_object(json_wtr);
306 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
307 unsigned char *value)
309 if (!map_is_per_cpu(info->type)) {
310 bool single_line, break_names;
312 break_names = info->key_size > 16 || info->value_size > 16;
313 single_line = info->key_size + info->value_size <= 24 &&
316 printf("key:%c", break_names ? '\n' : ' ');
317 fprint_hex(stdout, key, info->key_size, " ");
319 printf(single_line ? " " : "\n");
321 printf("value:%c", break_names ? '\n' : ' ');
322 fprint_hex(stdout, value, info->value_size, " ");
326 unsigned int i, n, step;
328 n = get_possible_cpus();
329 step = round_up(info->value_size, 8);
332 fprint_hex(stdout, key, info->key_size, " ");
334 for (i = 0; i < n; i++) {
335 printf("value (CPU %02d):%c",
336 i, info->value_size > 16 ? '\n' : ' ');
337 fprint_hex(stdout, value + i * step,
338 info->value_size, " ");
344 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
347 unsigned int i = 0, base = 0;
350 if (is_prefix(*argv, "hex")) {
355 while (i < n && argv[i]) {
356 val[i] = strtoul(argv[i], &endptr, base);
358 p_err("error parsing byte: %s", argv[i]);
365 p_err("%s expected %d bytes got %d", name, n, i);
372 static int parse_elem(char **argv, struct bpf_map_info *info,
373 void *key, void *value, __u32 key_size, __u32 value_size,
374 __u32 *flags, __u32 **value_fd)
379 p_err("did not find %s", key ? "key" : "value");
383 if (is_prefix(*argv, "key")) {
386 p_err("duplicate key");
388 p_err("unnecessary key");
392 argv = parse_bytes(argv + 1, "key", key, key_size);
396 return parse_elem(argv, info, NULL, value, key_size, value_size,
398 } else if (is_prefix(*argv, "value")) {
403 p_err("duplicate value");
405 p_err("unnecessary value");
411 if (map_is_map_of_maps(info->type)) {
414 if (value_size != 4) {
415 p_err("value smaller than 4B for map in map?");
418 if (!argv[0] || !argv[1]) {
419 p_err("not enough value arguments for map in map");
423 fd = map_parse_fd(&argc, &argv);
429 } else if (map_is_map_of_progs(info->type)) {
432 if (value_size != 4) {
433 p_err("value smaller than 4B for map of progs?");
436 if (!argv[0] || !argv[1]) {
437 p_err("not enough value arguments for map of progs");
441 fd = prog_parse_fd(&argc, &argv);
448 argv = parse_bytes(argv, "value", value, value_size);
453 return parse_elem(argv, info, key, NULL, key_size, value_size,
455 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
456 is_prefix(*argv, "exist")) {
458 p_err("flags specified multiple times: %s", *argv);
462 if (is_prefix(*argv, "any"))
464 else if (is_prefix(*argv, "noexist"))
465 *flags = BPF_NOEXIST;
466 else if (is_prefix(*argv, "exist"))
469 return parse_elem(argv + 1, info, key, value, key_size,
470 value_size, NULL, value_fd);
473 p_err("expected key or value, got: %s", *argv);
477 static int show_map_close_json(int fd, struct bpf_map_info *info)
481 memlock = get_fdinfo(fd, "memlock");
484 jsonw_start_object(json_wtr);
486 jsonw_uint_field(json_wtr, "id", info->id);
487 if (info->type < ARRAY_SIZE(map_type_name))
488 jsonw_string_field(json_wtr, "type",
489 map_type_name[info->type]);
491 jsonw_uint_field(json_wtr, "type", info->type);
494 jsonw_string_field(json_wtr, "name", info->name);
496 jsonw_name(json_wtr, "flags");
497 jsonw_printf(json_wtr, "%d", info->map_flags);
499 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
501 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
502 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
503 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
506 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
509 if (!hash_empty(map_table.table)) {
510 struct pinned_obj *obj;
512 jsonw_name(json_wtr, "pinned");
513 jsonw_start_array(json_wtr);
514 hash_for_each_possible(map_table.table, obj, hash, info->id) {
515 if (obj->id == info->id)
516 jsonw_string(json_wtr, obj->path);
518 jsonw_end_array(json_wtr);
521 jsonw_end_object(json_wtr);
526 static int show_map_close_plain(int fd, struct bpf_map_info *info)
530 memlock = get_fdinfo(fd, "memlock");
533 printf("%u: ", info->id);
534 if (info->type < ARRAY_SIZE(map_type_name))
535 printf("%s ", map_type_name[info->type]);
537 printf("type %u ", info->type);
540 printf("name %s ", info->name);
542 printf("flags 0x%x", info->map_flags);
543 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
545 printf("\tkey %uB value %uB max_entries %u",
546 info->key_size, info->value_size, info->max_entries);
549 printf(" memlock %sB", memlock);
553 if (!hash_empty(map_table.table)) {
554 struct pinned_obj *obj;
556 hash_for_each_possible(map_table.table, obj, hash, info->id) {
557 if (obj->id == info->id)
558 printf("\tpinned %s\n", obj->path);
564 static int do_show(int argc, char **argv)
566 struct bpf_map_info info = {};
567 __u32 len = sizeof(info);
573 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
576 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
581 return show_map_close_json(fd, &info);
583 return show_map_close_plain(fd, &info);
590 jsonw_start_array(json_wtr);
592 err = bpf_map_get_next_id(id, &id);
596 p_err("can't get next map: %s%s", strerror(errno),
597 errno == EINVAL ? " -- kernel too old?" : "");
601 fd = bpf_map_get_fd_by_id(id);
605 p_err("can't get map by id (%u): %s",
606 id, strerror(errno));
610 err = bpf_obj_get_info_by_fd(fd, &info, &len);
612 p_err("can't get map info: %s", strerror(errno));
618 show_map_close_json(fd, &info);
620 show_map_close_plain(fd, &info);
623 jsonw_end_array(json_wtr);
625 return errno == ENOENT ? 0 : -1;
628 static int do_dump(int argc, char **argv)
630 struct bpf_map_info info = {};
631 void *key, *value, *prev_key;
632 unsigned int num_elems = 0;
633 __u32 len = sizeof(info);
634 json_writer_t *btf_wtr;
635 struct btf *btf = NULL;
642 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
646 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
647 p_err("Dumping maps of maps and program maps not supported");
652 key = malloc(info.key_size);
653 value = alloc_value(&info);
654 if (!key || !value) {
655 p_err("mem alloc failed");
662 err = get_btf(&info, &btf);
664 p_err("failed to get btf");
669 jsonw_start_array(json_wtr);
672 btf_wtr = get_btf_writer();
674 p_info("failed to create json writer for btf. falling back to plain output");
678 jsonw_start_array(btf_wtr);
683 err = bpf_map_get_next_key(fd, prev_key, key);
690 if (!bpf_map_lookup_elem(fd, key, value)) {
692 print_entry_json(&info, key, value, btf);
695 struct btf_dumper d = {
698 .is_plain_text = true,
701 do_dump_btf(&d, &info, key, value);
703 print_entry_plain(&info, key, value);
707 jsonw_name(json_wtr, "key");
708 print_hex_data_json(key, info.key_size);
709 jsonw_name(json_wtr, "value");
710 jsonw_start_object(json_wtr);
711 jsonw_string_field(json_wtr, "error",
712 "can't lookup element");
713 jsonw_end_object(json_wtr);
715 p_info("can't lookup element with key: ");
716 fprint_hex(stderr, key, info.key_size, " ");
717 fprintf(stderr, "\n");
726 jsonw_end_array(json_wtr);
728 jsonw_end_array(btf_wtr);
729 jsonw_destroy(&btf_wtr);
731 printf("Found %u element%s\n", num_elems,
732 num_elems != 1 ? "s" : "");
744 static int do_update(int argc, char **argv)
746 struct bpf_map_info info = {};
747 __u32 len = sizeof(info);
748 __u32 *value_fd = NULL;
749 __u32 flags = BPF_ANY;
756 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
760 key = malloc(info.key_size);
761 value = alloc_value(&info);
762 if (!key || !value) {
763 p_err("mem alloc failed");
768 err = parse_elem(argv, &info, key, value, info.key_size,
769 info.value_size, &flags, &value_fd);
773 err = bpf_map_update_elem(fd, key, value, flags);
775 p_err("update failed: %s", strerror(errno));
786 if (!err && json_output)
787 jsonw_null(json_wtr);
791 static int do_lookup(int argc, char **argv)
793 struct bpf_map_info info = {};
794 __u32 len = sizeof(info);
795 json_writer_t *btf_wtr;
796 struct btf *btf = NULL;
804 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
808 key = malloc(info.key_size);
809 value = alloc_value(&info);
810 if (!key || !value) {
811 p_err("mem alloc failed");
816 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
820 err = bpf_map_lookup_elem(fd, key, value);
822 if (errno == ENOENT) {
824 jsonw_null(json_wtr);
827 fprint_hex(stdout, key, info.key_size, " ");
828 printf("\n\nNot found\n");
831 p_err("lookup failed: %s", strerror(errno));
837 /* here means bpf_map_lookup_elem() succeeded */
838 err = get_btf(&info, &btf);
840 p_err("failed to get btf");
845 print_entry_json(&info, key, value, btf);
847 /* if here json_wtr wouldn't have been initialised,
848 * so let's create separate writer for btf
850 btf_wtr = get_btf_writer();
852 p_info("failed to create json writer for btf. falling back to plain output");
855 print_entry_plain(&info, key, value);
857 struct btf_dumper d = {
860 .is_plain_text = true,
863 do_dump_btf(&d, &info, key, value);
864 jsonw_destroy(&btf_wtr);
867 print_entry_plain(&info, key, value);
879 static int do_getnext(int argc, char **argv)
881 struct bpf_map_info info = {};
882 __u32 len = sizeof(info);
890 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
894 key = malloc(info.key_size);
895 nextkey = malloc(info.key_size);
896 if (!key || !nextkey) {
897 p_err("mem alloc failed");
903 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
912 err = bpf_map_get_next_key(fd, key, nextkey);
914 p_err("can't get next key: %s", strerror(errno));
919 jsonw_start_object(json_wtr);
921 jsonw_name(json_wtr, "key");
922 print_hex_data_json(key, info.key_size);
924 jsonw_null_field(json_wtr, "key");
926 jsonw_name(json_wtr, "next_key");
927 print_hex_data_json(nextkey, info.key_size);
928 jsonw_end_object(json_wtr);
932 fprint_hex(stdout, key, info.key_size, " ");
935 printf("key: None\n");
937 printf("next key:\n");
938 fprint_hex(stdout, nextkey, info.key_size, " ");
950 static int do_delete(int argc, char **argv)
952 struct bpf_map_info info = {};
953 __u32 len = sizeof(info);
961 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
965 key = malloc(info.key_size);
967 p_err("mem alloc failed");
972 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
976 err = bpf_map_delete_elem(fd, key);
978 p_err("delete failed: %s", strerror(errno));
984 if (!err && json_output)
985 jsonw_null(json_wtr);
989 static int do_pin(int argc, char **argv)
993 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
994 if (!err && json_output)
995 jsonw_null(json_wtr);
999 static int do_help(int argc, char **argv)
1002 jsonw_null(json_wtr);
1007 "Usage: %s %s { show | list } [MAP]\n"
1009 " %s %s update MAP key DATA value VALUE [UPDATE_FLAGS]\n"
1010 " %s %s lookup MAP key DATA\n"
1011 " %s %s getnext MAP [key DATA]\n"
1012 " %s %s delete MAP key DATA\n"
1013 " %s %s pin MAP FILE\n"
1014 " %s %s event_pipe MAP [cpu N index M]\n"
1017 " " HELP_SPEC_MAP "\n"
1018 " DATA := { [hex] BYTES }\n"
1019 " " HELP_SPEC_PROGRAM "\n"
1020 " VALUE := { DATA | MAP | PROG }\n"
1021 " UPDATE_FLAGS := { any | exist | noexist }\n"
1022 " " HELP_SPEC_OPTIONS "\n"
1024 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1025 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1026 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
1031 static const struct cmd cmds[] = {
1032 { "show", do_show },
1033 { "list", do_show },
1034 { "help", do_help },
1035 { "dump", do_dump },
1036 { "update", do_update },
1037 { "lookup", do_lookup },
1038 { "getnext", do_getnext },
1039 { "delete", do_delete },
1041 { "event_pipe", do_event_pipe },
1045 int do_map(int argc, char **argv)
1047 return cmd_select(cmds, argc, argv, do_help);