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> */
39 #include <linux/kernel.h>
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",
70 [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
73 static bool map_is_per_cpu(__u32 type)
75 return type == BPF_MAP_TYPE_PERCPU_HASH ||
76 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
77 type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
80 static bool map_is_map_of_maps(__u32 type)
82 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
83 type == BPF_MAP_TYPE_HASH_OF_MAPS;
86 static bool map_is_map_of_progs(__u32 type)
88 return type == BPF_MAP_TYPE_PROG_ARRAY;
91 static void *alloc_value(struct bpf_map_info *info)
93 if (map_is_per_cpu(info->type))
94 return malloc(round_up(info->value_size, 8) *
97 return malloc(info->value_size);
100 static 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 void print_entry_json(struct bpf_map_info *info, unsigned char *key,
156 unsigned char *value)
158 jsonw_start_object(json_wtr);
160 if (!map_is_per_cpu(info->type)) {
161 jsonw_name(json_wtr, "key");
162 print_hex_data_json(key, info->key_size);
163 jsonw_name(json_wtr, "value");
164 print_hex_data_json(value, info->value_size);
166 unsigned int i, n, step;
168 n = get_possible_cpus();
169 step = round_up(info->value_size, 8);
171 jsonw_name(json_wtr, "key");
172 print_hex_data_json(key, info->key_size);
174 jsonw_name(json_wtr, "values");
175 jsonw_start_array(json_wtr);
176 for (i = 0; i < n; i++) {
177 jsonw_start_object(json_wtr);
179 jsonw_int_field(json_wtr, "cpu", i);
181 jsonw_name(json_wtr, "value");
182 print_hex_data_json(value + i * step,
185 jsonw_end_object(json_wtr);
187 jsonw_end_array(json_wtr);
190 jsonw_end_object(json_wtr);
193 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
194 unsigned char *value)
196 if (!map_is_per_cpu(info->type)) {
197 bool single_line, break_names;
199 break_names = info->key_size > 16 || info->value_size > 16;
200 single_line = info->key_size + info->value_size <= 24 &&
203 printf("key:%c", break_names ? '\n' : ' ');
204 fprint_hex(stdout, key, info->key_size, " ");
206 printf(single_line ? " " : "\n");
208 printf("value:%c", break_names ? '\n' : ' ');
209 fprint_hex(stdout, value, info->value_size, " ");
213 unsigned int i, n, step;
215 n = get_possible_cpus();
216 step = round_up(info->value_size, 8);
219 fprint_hex(stdout, key, info->key_size, " ");
221 for (i = 0; i < n; i++) {
222 printf("value (CPU %02d):%c",
223 i, info->value_size > 16 ? '\n' : ' ');
224 fprint_hex(stdout, value + i * step,
225 info->value_size, " ");
231 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
234 unsigned int i = 0, base = 0;
237 if (is_prefix(*argv, "hex")) {
242 while (i < n && argv[i]) {
243 val[i] = strtoul(argv[i], &endptr, base);
245 p_err("error parsing byte: %s", argv[i]);
252 p_err("%s expected %d bytes got %d", name, n, i);
259 static int parse_elem(char **argv, struct bpf_map_info *info,
260 void *key, void *value, __u32 key_size, __u32 value_size,
261 __u32 *flags, __u32 **value_fd)
266 p_err("did not find %s", key ? "key" : "value");
270 if (is_prefix(*argv, "key")) {
273 p_err("duplicate key");
275 p_err("unnecessary key");
279 argv = parse_bytes(argv + 1, "key", key, key_size);
283 return parse_elem(argv, info, NULL, value, key_size, value_size,
285 } else if (is_prefix(*argv, "value")) {
290 p_err("duplicate value");
292 p_err("unnecessary value");
298 if (map_is_map_of_maps(info->type)) {
301 if (value_size != 4) {
302 p_err("value smaller than 4B for map in map?");
305 if (!argv[0] || !argv[1]) {
306 p_err("not enough value arguments for map in map");
310 fd = map_parse_fd(&argc, &argv);
316 } else if (map_is_map_of_progs(info->type)) {
319 if (value_size != 4) {
320 p_err("value smaller than 4B for map of progs?");
323 if (!argv[0] || !argv[1]) {
324 p_err("not enough value arguments for map of progs");
328 fd = prog_parse_fd(&argc, &argv);
335 argv = parse_bytes(argv, "value", value, value_size);
340 return parse_elem(argv, info, key, NULL, key_size, value_size,
342 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
343 is_prefix(*argv, "exist")) {
345 p_err("flags specified multiple times: %s", *argv);
349 if (is_prefix(*argv, "any"))
351 else if (is_prefix(*argv, "noexist"))
352 *flags = BPF_NOEXIST;
353 else if (is_prefix(*argv, "exist"))
356 return parse_elem(argv + 1, info, key, value, key_size,
357 value_size, NULL, value_fd);
360 p_err("expected key or value, got: %s", *argv);
364 static int show_map_close_json(int fd, struct bpf_map_info *info)
368 memlock = get_fdinfo(fd, "memlock");
371 jsonw_start_object(json_wtr);
373 jsonw_uint_field(json_wtr, "id", info->id);
374 if (info->type < ARRAY_SIZE(map_type_name))
375 jsonw_string_field(json_wtr, "type",
376 map_type_name[info->type]);
378 jsonw_uint_field(json_wtr, "type", info->type);
381 jsonw_string_field(json_wtr, "name", info->name);
383 jsonw_name(json_wtr, "flags");
384 jsonw_printf(json_wtr, "%d", info->map_flags);
386 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
388 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
389 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
390 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
393 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
396 if (!hash_empty(map_table.table)) {
397 struct pinned_obj *obj;
399 jsonw_name(json_wtr, "pinned");
400 jsonw_start_array(json_wtr);
401 hash_for_each_possible(map_table.table, obj, hash, info->id) {
402 if (obj->id == info->id)
403 jsonw_string(json_wtr, obj->path);
405 jsonw_end_array(json_wtr);
408 jsonw_end_object(json_wtr);
413 static int show_map_close_plain(int fd, struct bpf_map_info *info)
417 memlock = get_fdinfo(fd, "memlock");
420 printf("%u: ", info->id);
421 if (info->type < ARRAY_SIZE(map_type_name))
422 printf("%s ", map_type_name[info->type]);
424 printf("type %u ", info->type);
427 printf("name %s ", info->name);
429 printf("flags 0x%x", info->map_flags);
430 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
432 printf("\tkey %uB value %uB max_entries %u",
433 info->key_size, info->value_size, info->max_entries);
436 printf(" memlock %sB", memlock);
440 if (!hash_empty(map_table.table)) {
441 struct pinned_obj *obj;
443 hash_for_each_possible(map_table.table, obj, hash, info->id) {
444 if (obj->id == info->id)
445 printf("\tpinned %s\n", obj->path);
451 static int do_show(int argc, char **argv)
453 struct bpf_map_info info = {};
454 __u32 len = sizeof(info);
460 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
463 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
468 return show_map_close_json(fd, &info);
470 return show_map_close_plain(fd, &info);
477 jsonw_start_array(json_wtr);
479 err = bpf_map_get_next_id(id, &id);
483 p_err("can't get next map: %s%s", strerror(errno),
484 errno == EINVAL ? " -- kernel too old?" : "");
488 fd = bpf_map_get_fd_by_id(id);
492 p_err("can't get map by id (%u): %s",
493 id, strerror(errno));
497 err = bpf_obj_get_info_by_fd(fd, &info, &len);
499 p_err("can't get map info: %s", strerror(errno));
505 show_map_close_json(fd, &info);
507 show_map_close_plain(fd, &info);
510 jsonw_end_array(json_wtr);
512 return errno == ENOENT ? 0 : -1;
515 static int do_dump(int argc, char **argv)
517 void *key, *value, *prev_key;
518 unsigned int num_elems = 0;
519 struct bpf_map_info info = {};
520 __u32 len = sizeof(info);
527 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
531 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
532 p_err("Dumping maps of maps and program maps not supported");
537 key = malloc(info.key_size);
538 value = alloc_value(&info);
539 if (!key || !value) {
540 p_err("mem alloc failed");
547 jsonw_start_array(json_wtr);
549 err = bpf_map_get_next_key(fd, prev_key, key);
556 if (!bpf_map_lookup_elem(fd, key, value)) {
558 print_entry_json(&info, key, value);
560 print_entry_plain(&info, key, value);
563 jsonw_name(json_wtr, "key");
564 print_hex_data_json(key, info.key_size);
565 jsonw_name(json_wtr, "value");
566 jsonw_start_object(json_wtr);
567 jsonw_string_field(json_wtr, "error",
568 "can't lookup element");
569 jsonw_end_object(json_wtr);
571 p_info("can't lookup element with key: ");
572 fprint_hex(stderr, key, info.key_size, " ");
573 fprintf(stderr, "\n");
582 jsonw_end_array(json_wtr);
584 printf("Found %u element%s\n", num_elems,
585 num_elems != 1 ? "s" : "");
595 static int do_update(int argc, char **argv)
597 struct bpf_map_info info = {};
598 __u32 len = sizeof(info);
599 __u32 *value_fd = NULL;
600 __u32 flags = BPF_ANY;
607 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
611 key = malloc(info.key_size);
612 value = alloc_value(&info);
613 if (!key || !value) {
614 p_err("mem alloc failed");
619 err = parse_elem(argv, &info, key, value, info.key_size,
620 info.value_size, &flags, &value_fd);
624 err = bpf_map_update_elem(fd, key, value, flags);
626 p_err("update failed: %s", strerror(errno));
637 if (!err && json_output)
638 jsonw_null(json_wtr);
642 static int do_lookup(int argc, char **argv)
644 struct bpf_map_info info = {};
645 __u32 len = sizeof(info);
653 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
657 key = malloc(info.key_size);
658 value = alloc_value(&info);
659 if (!key || !value) {
660 p_err("mem alloc failed");
665 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
669 err = bpf_map_lookup_elem(fd, key, value);
672 print_entry_json(&info, key, value);
674 print_entry_plain(&info, key, value);
675 } else if (errno == ENOENT) {
677 jsonw_null(json_wtr);
680 fprint_hex(stdout, key, info.key_size, " ");
681 printf("\n\nNot found\n");
684 p_err("lookup failed: %s", strerror(errno));
695 static int do_getnext(int argc, char **argv)
697 struct bpf_map_info info = {};
698 __u32 len = sizeof(info);
706 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
710 key = malloc(info.key_size);
711 nextkey = malloc(info.key_size);
712 if (!key || !nextkey) {
713 p_err("mem alloc failed");
719 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
728 err = bpf_map_get_next_key(fd, key, nextkey);
730 p_err("can't get next key: %s", strerror(errno));
735 jsonw_start_object(json_wtr);
737 jsonw_name(json_wtr, "key");
738 print_hex_data_json(key, info.key_size);
740 jsonw_null_field(json_wtr, "key");
742 jsonw_name(json_wtr, "next_key");
743 print_hex_data_json(nextkey, info.key_size);
744 jsonw_end_object(json_wtr);
748 fprint_hex(stdout, key, info.key_size, " ");
751 printf("key: None\n");
753 printf("next key:\n");
754 fprint_hex(stdout, nextkey, info.key_size, " ");
766 static int do_delete(int argc, char **argv)
768 struct bpf_map_info info = {};
769 __u32 len = sizeof(info);
777 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
781 key = malloc(info.key_size);
783 p_err("mem alloc failed");
788 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
792 err = bpf_map_delete_elem(fd, key);
794 p_err("delete failed: %s", strerror(errno));
800 if (!err && json_output)
801 jsonw_null(json_wtr);
805 static int do_pin(int argc, char **argv)
809 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
810 if (!err && json_output)
811 jsonw_null(json_wtr);
815 static int do_help(int argc, char **argv)
818 jsonw_null(json_wtr);
823 "Usage: %s %s { show | list } [MAP]\n"
825 " %s %s update MAP key DATA value VALUE [UPDATE_FLAGS]\n"
826 " %s %s lookup MAP key DATA\n"
827 " %s %s getnext MAP [key DATA]\n"
828 " %s %s delete MAP key DATA\n"
829 " %s %s pin MAP FILE\n"
830 " %s %s event_pipe MAP [cpu N index M]\n"
833 " MAP := { id MAP_ID | pinned FILE }\n"
834 " DATA := { [hex] BYTES }\n"
835 " " HELP_SPEC_PROGRAM "\n"
836 " VALUE := { DATA | MAP | PROG }\n"
837 " UPDATE_FLAGS := { any | exist | noexist }\n"
838 " " HELP_SPEC_OPTIONS "\n"
840 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
841 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
842 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
847 static const struct cmd cmds[] = {
852 { "update", do_update },
853 { "lookup", do_lookup },
854 { "getnext", do_getnext },
855 { "delete", do_delete },
857 { "event_pipe", do_event_pipe },
861 int do_map(int argc, char **argv)
863 return cmd_select(cmds, argc, argv, do_help);