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> */
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,
286 unsigned int i = 0, base = 0;
289 if (is_prefix(*argv, "hex")) {
294 while (i < n && argv[i]) {
295 val[i] = strtoul(argv[i], &endptr, base);
297 p_err("error parsing byte: %s", argv[i]);
304 p_err("%s expected %d bytes got %d", name, n, i);
311 static int parse_elem(char **argv, struct bpf_map_info *info,
312 void *key, void *value, __u32 key_size, __u32 value_size,
313 __u32 *flags, __u32 **value_fd)
318 p_err("did not find %s", key ? "key" : "value");
322 if (is_prefix(*argv, "key")) {
325 p_err("duplicate key");
327 p_err("unnecessary key");
331 argv = parse_bytes(argv + 1, "key", key, key_size);
335 return parse_elem(argv, info, NULL, value, key_size, value_size,
337 } else if (is_prefix(*argv, "value")) {
342 p_err("duplicate value");
344 p_err("unnecessary value");
350 if (map_is_map_of_maps(info->type)) {
353 if (value_size != 4) {
354 p_err("value smaller than 4B for map in map?");
357 if (!argv[0] || !argv[1]) {
358 p_err("not enough value arguments for map in map");
362 fd = map_parse_fd(&argc, &argv);
368 } else if (map_is_map_of_progs(info->type)) {
371 if (value_size != 4) {
372 p_err("value smaller than 4B for map of progs?");
375 if (!argv[0] || !argv[1]) {
376 p_err("not enough value arguments for map of progs");
380 fd = prog_parse_fd(&argc, &argv);
387 argv = parse_bytes(argv, "value", value, value_size);
392 return parse_elem(argv, info, key, NULL, key_size, value_size,
394 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
395 is_prefix(*argv, "exist")) {
397 p_err("flags specified multiple times: %s", *argv);
401 if (is_prefix(*argv, "any"))
403 else if (is_prefix(*argv, "noexist"))
404 *flags = BPF_NOEXIST;
405 else if (is_prefix(*argv, "exist"))
408 return parse_elem(argv + 1, info, key, value, key_size,
409 value_size, NULL, value_fd);
412 p_err("expected key or value, got: %s", *argv);
416 static int show_map_close_json(int fd, struct bpf_map_info *info)
420 memlock = get_fdinfo(fd, "memlock");
423 jsonw_start_object(json_wtr);
425 jsonw_uint_field(json_wtr, "id", info->id);
426 if (info->type < ARRAY_SIZE(map_type_name))
427 jsonw_string_field(json_wtr, "type",
428 map_type_name[info->type]);
430 jsonw_uint_field(json_wtr, "type", info->type);
433 jsonw_string_field(json_wtr, "name", info->name);
435 jsonw_name(json_wtr, "flags");
436 jsonw_printf(json_wtr, "%d", info->map_flags);
438 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
440 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
441 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
442 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
445 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
448 if (!hash_empty(map_table.table)) {
449 struct pinned_obj *obj;
451 jsonw_name(json_wtr, "pinned");
452 jsonw_start_array(json_wtr);
453 hash_for_each_possible(map_table.table, obj, hash, info->id) {
454 if (obj->id == info->id)
455 jsonw_string(json_wtr, obj->path);
457 jsonw_end_array(json_wtr);
460 jsonw_end_object(json_wtr);
465 static int show_map_close_plain(int fd, struct bpf_map_info *info)
469 memlock = get_fdinfo(fd, "memlock");
472 printf("%u: ", info->id);
473 if (info->type < ARRAY_SIZE(map_type_name))
474 printf("%s ", map_type_name[info->type]);
476 printf("type %u ", info->type);
479 printf("name %s ", info->name);
481 printf("flags 0x%x", info->map_flags);
482 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
484 printf("\tkey %uB value %uB max_entries %u",
485 info->key_size, info->value_size, info->max_entries);
488 printf(" memlock %sB", memlock);
492 if (!hash_empty(map_table.table)) {
493 struct pinned_obj *obj;
495 hash_for_each_possible(map_table.table, obj, hash, info->id) {
496 if (obj->id == info->id)
497 printf("\tpinned %s\n", obj->path);
503 static int do_show(int argc, char **argv)
505 struct bpf_map_info info = {};
506 __u32 len = sizeof(info);
512 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
515 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
520 return show_map_close_json(fd, &info);
522 return show_map_close_plain(fd, &info);
529 jsonw_start_array(json_wtr);
531 err = bpf_map_get_next_id(id, &id);
535 p_err("can't get next map: %s%s", strerror(errno),
536 errno == EINVAL ? " -- kernel too old?" : "");
540 fd = bpf_map_get_fd_by_id(id);
544 p_err("can't get map by id (%u): %s",
545 id, strerror(errno));
549 err = bpf_obj_get_info_by_fd(fd, &info, &len);
551 p_err("can't get map info: %s", strerror(errno));
557 show_map_close_json(fd, &info);
559 show_map_close_plain(fd, &info);
562 jsonw_end_array(json_wtr);
564 return errno == ENOENT ? 0 : -1;
567 static int do_dump(int argc, char **argv)
569 void *key, *value, *prev_key;
570 unsigned int num_elems = 0;
571 struct bpf_map_info info = {};
572 __u32 len = sizeof(info);
579 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
583 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
584 p_err("Dumping maps of maps and program maps not supported");
589 key = malloc(info.key_size);
590 value = alloc_value(&info);
591 if (!key || !value) {
592 p_err("mem alloc failed");
599 jsonw_start_array(json_wtr);
601 err = bpf_map_get_next_key(fd, prev_key, key);
608 if (!bpf_map_lookup_elem(fd, key, value)) {
610 print_entry_json(&info, key, value);
612 print_entry_plain(&info, key, value);
615 jsonw_name(json_wtr, "key");
616 print_hex_data_json(key, info.key_size);
617 jsonw_name(json_wtr, "value");
618 jsonw_start_object(json_wtr);
619 jsonw_string_field(json_wtr, "error",
620 "can't lookup element");
621 jsonw_end_object(json_wtr);
623 p_info("can't lookup element with key: ");
624 fprint_hex(stderr, key, info.key_size, " ");
625 fprintf(stderr, "\n");
634 jsonw_end_array(json_wtr);
636 printf("Found %u element%s\n", num_elems,
637 num_elems != 1 ? "s" : "");
647 static int do_update(int argc, char **argv)
649 struct bpf_map_info info = {};
650 __u32 len = sizeof(info);
651 __u32 *value_fd = NULL;
652 __u32 flags = BPF_ANY;
659 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
663 key = malloc(info.key_size);
664 value = alloc_value(&info);
665 if (!key || !value) {
666 p_err("mem alloc failed");
671 err = parse_elem(argv, &info, key, value, info.key_size,
672 info.value_size, &flags, &value_fd);
676 err = bpf_map_update_elem(fd, key, value, flags);
678 p_err("update failed: %s", strerror(errno));
689 if (!err && json_output)
690 jsonw_null(json_wtr);
694 static int do_lookup(int argc, char **argv)
696 struct bpf_map_info info = {};
697 __u32 len = sizeof(info);
705 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
709 key = malloc(info.key_size);
710 value = alloc_value(&info);
711 if (!key || !value) {
712 p_err("mem alloc failed");
717 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
721 err = bpf_map_lookup_elem(fd, key, value);
724 print_entry_json(&info, key, value);
726 print_entry_plain(&info, key, value);
727 } else if (errno == ENOENT) {
729 jsonw_null(json_wtr);
732 fprint_hex(stdout, key, info.key_size, " ");
733 printf("\n\nNot found\n");
736 p_err("lookup failed: %s", strerror(errno));
747 static int do_getnext(int argc, char **argv)
749 struct bpf_map_info info = {};
750 __u32 len = sizeof(info);
758 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
762 key = malloc(info.key_size);
763 nextkey = malloc(info.key_size);
764 if (!key || !nextkey) {
765 p_err("mem alloc failed");
771 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
780 err = bpf_map_get_next_key(fd, key, nextkey);
782 p_err("can't get next key: %s", strerror(errno));
787 jsonw_start_object(json_wtr);
789 jsonw_name(json_wtr, "key");
790 print_hex_data_json(key, info.key_size);
792 jsonw_null_field(json_wtr, "key");
794 jsonw_name(json_wtr, "next_key");
795 print_hex_data_json(nextkey, info.key_size);
796 jsonw_end_object(json_wtr);
800 fprint_hex(stdout, key, info.key_size, " ");
803 printf("key: None\n");
805 printf("next key:\n");
806 fprint_hex(stdout, nextkey, info.key_size, " ");
818 static int do_delete(int argc, char **argv)
820 struct bpf_map_info info = {};
821 __u32 len = sizeof(info);
829 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
833 key = malloc(info.key_size);
835 p_err("mem alloc failed");
840 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
844 err = bpf_map_delete_elem(fd, key);
846 p_err("delete failed: %s", strerror(errno));
852 if (!err && json_output)
853 jsonw_null(json_wtr);
857 static int do_pin(int argc, char **argv)
861 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
862 if (!err && json_output)
863 jsonw_null(json_wtr);
867 static int do_help(int argc, char **argv)
870 jsonw_null(json_wtr);
875 "Usage: %s %s { show | list } [MAP]\n"
877 " %s %s update MAP key DATA value VALUE [UPDATE_FLAGS]\n"
878 " %s %s lookup MAP key DATA\n"
879 " %s %s getnext MAP [key DATA]\n"
880 " %s %s delete MAP key DATA\n"
881 " %s %s pin MAP FILE\n"
884 " MAP := { id MAP_ID | pinned FILE }\n"
885 " DATA := { [hex] BYTES }\n"
886 " " HELP_SPEC_PROGRAM "\n"
887 " VALUE := { DATA | MAP | PROG }\n"
888 " UPDATE_FLAGS := { any | exist | noexist }\n"
889 " " HELP_SPEC_OPTIONS "\n"
891 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
892 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
893 bin_name, argv[-2], bin_name, argv[-2]);
898 static const struct cmd cmds[] = {
903 { "update", do_update },
904 { "lookup", do_lookup },
905 { "getnext", do_getnext },
906 { "delete", do_delete },
911 int do_map(int argc, char **argv)
913 return cmd_select(cmds, argc, argv, do_help);