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
34 /* Author: Jakub Kicinski <kubakici@wp.pl> */
44 #include <sys/types.h>
51 static const char * const map_type_name[] = {
52 [BPF_MAP_TYPE_UNSPEC] = "unspec",
53 [BPF_MAP_TYPE_HASH] = "hash",
54 [BPF_MAP_TYPE_ARRAY] = "array",
55 [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
56 [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
57 [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
58 [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
59 [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
60 [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
61 [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
62 [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
63 [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
64 [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
65 [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
66 [BPF_MAP_TYPE_DEVMAP] = "devmap",
67 [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
68 [BPF_MAP_TYPE_CPUMAP] = "cpumap",
69 [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
72 static bool map_is_per_cpu(__u32 type)
74 return type == BPF_MAP_TYPE_PERCPU_HASH ||
75 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
76 type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
79 static bool map_is_map_of_maps(__u32 type)
81 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
82 type == BPF_MAP_TYPE_HASH_OF_MAPS;
85 static bool map_is_map_of_progs(__u32 type)
87 return type == BPF_MAP_TYPE_PROG_ARRAY;
90 static void *alloc_value(struct bpf_map_info *info)
92 if (map_is_per_cpu(info->type))
93 return malloc(info->value_size * get_possible_cpus());
95 return malloc(info->value_size);
98 static int map_parse_fd(int *argc, char ***argv)
102 if (is_prefix(**argv, "id")) {
108 id = strtoul(**argv, &endptr, 0);
110 p_err("can't parse %s as ID", **argv);
115 fd = bpf_map_get_fd_by_id(id);
117 p_err("get map by id (%u): %s", id, strerror(errno));
119 } else if (is_prefix(**argv, "pinned")) {
127 return open_obj_pinned_any(path, BPF_OBJ_MAP);
130 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
134 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
139 fd = map_parse_fd(argc, argv);
143 err = bpf_obj_get_info_by_fd(fd, info, info_len);
145 p_err("can't get map info: %s", strerror(errno));
153 static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
154 unsigned char *value)
156 jsonw_start_object(json_wtr);
158 if (!map_is_per_cpu(info->type)) {
159 jsonw_name(json_wtr, "key");
160 print_hex_data_json(key, info->key_size);
161 jsonw_name(json_wtr, "value");
162 print_hex_data_json(value, info->value_size);
166 n = get_possible_cpus();
168 jsonw_name(json_wtr, "key");
169 print_hex_data_json(key, info->key_size);
171 jsonw_name(json_wtr, "values");
172 jsonw_start_array(json_wtr);
173 for (i = 0; i < n; i++) {
174 jsonw_start_object(json_wtr);
176 jsonw_int_field(json_wtr, "cpu", i);
178 jsonw_name(json_wtr, "value");
179 print_hex_data_json(value + i * info->value_size,
182 jsonw_end_object(json_wtr);
184 jsonw_end_array(json_wtr);
187 jsonw_end_object(json_wtr);
190 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
191 unsigned char *value)
193 if (!map_is_per_cpu(info->type)) {
194 bool single_line, break_names;
196 break_names = info->key_size > 16 || info->value_size > 16;
197 single_line = info->key_size + info->value_size <= 24 &&
200 printf("key:%c", break_names ? '\n' : ' ');
201 fprint_hex(stdout, key, info->key_size, " ");
203 printf(single_line ? " " : "\n");
205 printf("value:%c", break_names ? '\n' : ' ');
206 fprint_hex(stdout, value, info->value_size, " ");
212 n = get_possible_cpus();
215 fprint_hex(stdout, key, info->key_size, " ");
217 for (i = 0; i < n; i++) {
218 printf("value (CPU %02d):%c",
219 i, info->value_size > 16 ? '\n' : ' ');
220 fprint_hex(stdout, value + i * info->value_size,
221 info->value_size, " ");
227 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
230 unsigned int i = 0, base = 0;
233 if (is_prefix(*argv, "hex")) {
238 while (i < n && argv[i]) {
239 val[i] = strtoul(argv[i], &endptr, base);
241 p_err("error parsing byte: %s", argv[i]);
248 p_err("%s expected %d bytes got %d", name, n, i);
255 static int parse_elem(char **argv, struct bpf_map_info *info,
256 void *key, void *value, __u32 key_size, __u32 value_size,
257 __u32 *flags, __u32 **value_fd)
262 p_err("did not find %s", key ? "key" : "value");
266 if (is_prefix(*argv, "key")) {
269 p_err("duplicate key");
271 p_err("unnecessary key");
275 argv = parse_bytes(argv + 1, "key", key, key_size);
279 return parse_elem(argv, info, NULL, value, key_size, value_size,
281 } else if (is_prefix(*argv, "value")) {
286 p_err("duplicate value");
288 p_err("unnecessary value");
294 if (map_is_map_of_maps(info->type)) {
297 if (value_size != 4) {
298 p_err("value smaller than 4B for map in map?");
301 if (!argv[0] || !argv[1]) {
302 p_err("not enough value arguments for map in map");
306 fd = map_parse_fd(&argc, &argv);
312 } else if (map_is_map_of_progs(info->type)) {
315 if (value_size != 4) {
316 p_err("value smaller than 4B for map of progs?");
319 if (!argv[0] || !argv[1]) {
320 p_err("not enough value arguments for map of progs");
324 fd = prog_parse_fd(&argc, &argv);
331 argv = parse_bytes(argv, "value", value, value_size);
336 return parse_elem(argv, info, key, NULL, key_size, value_size,
338 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
339 is_prefix(*argv, "exist")) {
341 p_err("flags specified multiple times: %s", *argv);
345 if (is_prefix(*argv, "any"))
347 else if (is_prefix(*argv, "noexist"))
348 *flags = BPF_NOEXIST;
349 else if (is_prefix(*argv, "exist"))
352 return parse_elem(argv + 1, info, key, value, key_size,
353 value_size, NULL, value_fd);
356 p_err("expected key or value, got: %s", *argv);
360 static int show_map_close_json(int fd, struct bpf_map_info *info)
364 memlock = get_fdinfo(fd, "memlock");
367 jsonw_start_object(json_wtr);
369 jsonw_uint_field(json_wtr, "id", info->id);
370 if (info->type < ARRAY_SIZE(map_type_name))
371 jsonw_string_field(json_wtr, "type",
372 map_type_name[info->type]);
374 jsonw_uint_field(json_wtr, "type", info->type);
377 jsonw_string_field(json_wtr, "name", info->name);
379 jsonw_name(json_wtr, "flags");
380 jsonw_printf(json_wtr, "%d", info->map_flags);
382 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
384 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
385 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
386 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
389 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
392 if (!hash_empty(map_table.table)) {
393 struct pinned_obj *obj;
395 jsonw_name(json_wtr, "pinned");
396 jsonw_start_array(json_wtr);
397 hash_for_each_possible(map_table.table, obj, hash, info->id) {
398 if (obj->id == info->id)
399 jsonw_string(json_wtr, obj->path);
401 jsonw_end_array(json_wtr);
404 jsonw_end_object(json_wtr);
409 static int show_map_close_plain(int fd, struct bpf_map_info *info)
413 memlock = get_fdinfo(fd, "memlock");
416 printf("%u: ", info->id);
417 if (info->type < ARRAY_SIZE(map_type_name))
418 printf("%s ", map_type_name[info->type]);
420 printf("type %u ", info->type);
423 printf("name %s ", info->name);
425 printf("flags 0x%x", info->map_flags);
426 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
428 printf("\tkey %uB value %uB max_entries %u",
429 info->key_size, info->value_size, info->max_entries);
432 printf(" memlock %sB", memlock);
436 if (!hash_empty(map_table.table)) {
437 struct pinned_obj *obj;
439 hash_for_each_possible(map_table.table, obj, hash, info->id) {
440 if (obj->id == info->id)
441 printf("\tpinned %s\n", obj->path);
447 static int do_show(int argc, char **argv)
449 struct bpf_map_info info = {};
450 __u32 len = sizeof(info);
456 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
459 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
464 return show_map_close_json(fd, &info);
466 return show_map_close_plain(fd, &info);
473 jsonw_start_array(json_wtr);
475 err = bpf_map_get_next_id(id, &id);
479 p_err("can't get next map: %s%s", strerror(errno),
480 errno == EINVAL ? " -- kernel too old?" : "");
484 fd = bpf_map_get_fd_by_id(id);
488 p_err("can't get map by id (%u): %s",
489 id, strerror(errno));
493 err = bpf_obj_get_info_by_fd(fd, &info, &len);
495 p_err("can't get map info: %s", strerror(errno));
501 show_map_close_json(fd, &info);
503 show_map_close_plain(fd, &info);
506 jsonw_end_array(json_wtr);
508 return errno == ENOENT ? 0 : -1;
511 static int do_dump(int argc, char **argv)
513 void *key, *value, *prev_key;
514 unsigned int num_elems = 0;
515 struct bpf_map_info info = {};
516 __u32 len = sizeof(info);
523 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
527 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
528 p_err("Dumping maps of maps and program maps not supported");
533 key = malloc(info.key_size);
534 value = alloc_value(&info);
535 if (!key || !value) {
536 p_err("mem alloc failed");
543 jsonw_start_array(json_wtr);
545 err = bpf_map_get_next_key(fd, prev_key, key);
552 if (!bpf_map_lookup_elem(fd, key, value)) {
554 print_entry_json(&info, key, value);
556 print_entry_plain(&info, key, value);
559 jsonw_name(json_wtr, "key");
560 print_hex_data_json(key, info.key_size);
561 jsonw_name(json_wtr, "value");
562 jsonw_start_object(json_wtr);
563 jsonw_string_field(json_wtr, "error",
564 "can't lookup element");
565 jsonw_end_object(json_wtr);
567 p_info("can't lookup element with key: ");
568 fprint_hex(stderr, key, info.key_size, " ");
569 fprintf(stderr, "\n");
578 jsonw_end_array(json_wtr);
580 printf("Found %u element%s\n", num_elems,
581 num_elems != 1 ? "s" : "");
591 static int do_update(int argc, char **argv)
593 struct bpf_map_info info = {};
594 __u32 len = sizeof(info);
595 __u32 *value_fd = NULL;
596 __u32 flags = BPF_ANY;
603 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
607 key = malloc(info.key_size);
608 value = alloc_value(&info);
609 if (!key || !value) {
610 p_err("mem alloc failed");
615 err = parse_elem(argv, &info, key, value, info.key_size,
616 info.value_size, &flags, &value_fd);
620 err = bpf_map_update_elem(fd, key, value, flags);
622 p_err("update failed: %s", strerror(errno));
633 if (!err && json_output)
634 jsonw_null(json_wtr);
638 static int do_lookup(int argc, char **argv)
640 struct bpf_map_info info = {};
641 __u32 len = sizeof(info);
649 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
653 key = malloc(info.key_size);
654 value = alloc_value(&info);
655 if (!key || !value) {
656 p_err("mem alloc failed");
661 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
665 err = bpf_map_lookup_elem(fd, key, value);
668 print_entry_json(&info, key, value);
670 print_entry_plain(&info, key, value);
671 } else if (errno == ENOENT) {
673 jsonw_null(json_wtr);
676 fprint_hex(stdout, key, info.key_size, " ");
677 printf("\n\nNot found\n");
680 p_err("lookup failed: %s", strerror(errno));
691 static int do_getnext(int argc, char **argv)
693 struct bpf_map_info info = {};
694 __u32 len = sizeof(info);
702 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
706 key = malloc(info.key_size);
707 nextkey = malloc(info.key_size);
708 if (!key || !nextkey) {
709 p_err("mem alloc failed");
715 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
724 err = bpf_map_get_next_key(fd, key, nextkey);
726 p_err("can't get next key: %s", strerror(errno));
731 jsonw_start_object(json_wtr);
733 jsonw_name(json_wtr, "key");
734 print_hex_data_json(key, info.key_size);
736 jsonw_null_field(json_wtr, "key");
738 jsonw_name(json_wtr, "next_key");
739 print_hex_data_json(nextkey, info.key_size);
740 jsonw_end_object(json_wtr);
744 fprint_hex(stdout, key, info.key_size, " ");
747 printf("key: None\n");
749 printf("next key:\n");
750 fprint_hex(stdout, nextkey, info.key_size, " ");
762 static int do_delete(int argc, char **argv)
764 struct bpf_map_info info = {};
765 __u32 len = sizeof(info);
773 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
777 key = malloc(info.key_size);
779 p_err("mem alloc failed");
784 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
788 err = bpf_map_delete_elem(fd, key);
790 p_err("delete failed: %s", strerror(errno));
796 if (!err && json_output)
797 jsonw_null(json_wtr);
801 static int do_pin(int argc, char **argv)
805 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
806 if (!err && json_output)
807 jsonw_null(json_wtr);
811 static int do_help(int argc, char **argv)
814 jsonw_null(json_wtr);
819 "Usage: %s %s { show | list } [MAP]\n"
821 " %s %s update MAP key DATA value VALUE [UPDATE_FLAGS]\n"
822 " %s %s lookup MAP key DATA\n"
823 " %s %s getnext MAP [key DATA]\n"
824 " %s %s delete MAP key DATA\n"
825 " %s %s pin MAP FILE\n"
826 " %s %s event_pipe MAP [cpu N index M]\n"
829 " MAP := { id MAP_ID | pinned FILE }\n"
830 " DATA := { [hex] BYTES }\n"
831 " " HELP_SPEC_PROGRAM "\n"
832 " VALUE := { DATA | MAP | PROG }\n"
833 " UPDATE_FLAGS := { any | exist | noexist }\n"
834 " " HELP_SPEC_OPTIONS "\n"
836 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
837 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
838 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
843 static const struct cmd cmds[] = {
848 { "update", do_update },
849 { "lookup", do_lookup },
850 { "getnext", do_getnext },
851 { "delete", do_delete },
853 { "event_pipe", do_event_pipe },
857 int do_map(int argc, char **argv)
859 return cmd_select(cmds, argc, argv, do_help);