xref: /openbmc/linux/tools/bpf/bpftool/map.c (revision ac73d4bf)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <linux/err.h>
7 #include <linux/kernel.h>
8 #include <net/if.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 
17 #include <bpf/bpf.h>
18 #include <bpf/btf.h>
19 #include <bpf/hashmap.h>
20 
21 #include "json_writer.h"
22 #include "main.h"
23 
24 static struct hashmap *map_table;
25 
26 static bool map_is_per_cpu(__u32 type)
27 {
28 	return type == BPF_MAP_TYPE_PERCPU_HASH ||
29 	       type == BPF_MAP_TYPE_PERCPU_ARRAY ||
30 	       type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
31 	       type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE;
32 }
33 
34 static bool map_is_map_of_maps(__u32 type)
35 {
36 	return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
37 	       type == BPF_MAP_TYPE_HASH_OF_MAPS;
38 }
39 
40 static bool map_is_map_of_progs(__u32 type)
41 {
42 	return type == BPF_MAP_TYPE_PROG_ARRAY;
43 }
44 
45 static int map_type_from_str(const char *type)
46 {
47 	const char *map_type_str;
48 	unsigned int i;
49 
50 	for (i = 0; ; i++) {
51 		map_type_str = libbpf_bpf_map_type_str(i);
52 		if (!map_type_str)
53 			break;
54 
55 		/* Don't allow prefixing in case of possible future shadowing */
56 		if (!strcmp(map_type_str, type))
57 			return i;
58 	}
59 	return -1;
60 }
61 
62 static void *alloc_value(struct bpf_map_info *info)
63 {
64 	if (map_is_per_cpu(info->type))
65 		return malloc(round_up(info->value_size, 8) *
66 			      get_possible_cpus());
67 	else
68 		return malloc(info->value_size);
69 }
70 
71 static int do_dump_btf(const struct btf_dumper *d,
72 		       struct bpf_map_info *map_info, void *key,
73 		       void *value)
74 {
75 	__u32 value_id;
76 	int ret = 0;
77 
78 	/* start of key-value pair */
79 	jsonw_start_object(d->jw);
80 
81 	if (map_info->btf_key_type_id) {
82 		jsonw_name(d->jw, "key");
83 
84 		ret = btf_dumper_type(d, map_info->btf_key_type_id, key);
85 		if (ret)
86 			goto err_end_obj;
87 	}
88 
89 	value_id = map_info->btf_vmlinux_value_type_id ?
90 		: map_info->btf_value_type_id;
91 
92 	if (!map_is_per_cpu(map_info->type)) {
93 		jsonw_name(d->jw, "value");
94 		ret = btf_dumper_type(d, value_id, value);
95 	} else {
96 		unsigned int i, n, step;
97 
98 		jsonw_name(d->jw, "values");
99 		jsonw_start_array(d->jw);
100 		n = get_possible_cpus();
101 		step = round_up(map_info->value_size, 8);
102 		for (i = 0; i < n; i++) {
103 			jsonw_start_object(d->jw);
104 			jsonw_int_field(d->jw, "cpu", i);
105 			jsonw_name(d->jw, "value");
106 			ret = btf_dumper_type(d, value_id, value + i * step);
107 			jsonw_end_object(d->jw);
108 			if (ret)
109 				break;
110 		}
111 		jsonw_end_array(d->jw);
112 	}
113 
114 err_end_obj:
115 	/* end of key-value pair */
116 	jsonw_end_object(d->jw);
117 
118 	return ret;
119 }
120 
121 static json_writer_t *get_btf_writer(void)
122 {
123 	json_writer_t *jw = jsonw_new(stdout);
124 
125 	if (!jw)
126 		return NULL;
127 	jsonw_pretty(jw, true);
128 
129 	return jw;
130 }
131 
132 static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
133 			     unsigned char *value, struct btf *btf)
134 {
135 	jsonw_start_object(json_wtr);
136 
137 	if (!map_is_per_cpu(info->type)) {
138 		jsonw_name(json_wtr, "key");
139 		print_hex_data_json(key, info->key_size);
140 		jsonw_name(json_wtr, "value");
141 		print_hex_data_json(value, info->value_size);
142 		if (btf) {
143 			struct btf_dumper d = {
144 				.btf = btf,
145 				.jw = json_wtr,
146 				.is_plain_text = false,
147 			};
148 
149 			jsonw_name(json_wtr, "formatted");
150 			do_dump_btf(&d, info, key, value);
151 		}
152 	} else {
153 		unsigned int i, n, step;
154 
155 		n = get_possible_cpus();
156 		step = round_up(info->value_size, 8);
157 
158 		jsonw_name(json_wtr, "key");
159 		print_hex_data_json(key, info->key_size);
160 
161 		jsonw_name(json_wtr, "values");
162 		jsonw_start_array(json_wtr);
163 		for (i = 0; i < n; i++) {
164 			jsonw_start_object(json_wtr);
165 
166 			jsonw_int_field(json_wtr, "cpu", i);
167 
168 			jsonw_name(json_wtr, "value");
169 			print_hex_data_json(value + i * step,
170 					    info->value_size);
171 
172 			jsonw_end_object(json_wtr);
173 		}
174 		jsonw_end_array(json_wtr);
175 		if (btf) {
176 			struct btf_dumper d = {
177 				.btf = btf,
178 				.jw = json_wtr,
179 				.is_plain_text = false,
180 			};
181 
182 			jsonw_name(json_wtr, "formatted");
183 			do_dump_btf(&d, info, key, value);
184 		}
185 	}
186 
187 	jsonw_end_object(json_wtr);
188 }
189 
190 static void
191 print_entry_error_msg(struct bpf_map_info *info, unsigned char *key,
192 		      const char *error_msg)
193 {
194 	int msg_size = strlen(error_msg);
195 	bool single_line, break_names;
196 
197 	break_names = info->key_size > 16 || msg_size > 16;
198 	single_line = info->key_size + msg_size <= 24 && !break_names;
199 
200 	printf("key:%c", break_names ? '\n' : ' ');
201 	fprint_hex(stdout, key, info->key_size, " ");
202 
203 	printf(single_line ? "  " : "\n");
204 
205 	printf("value:%c%s", break_names ? '\n' : ' ', error_msg);
206 
207 	printf("\n");
208 }
209 
210 static void
211 print_entry_error(struct bpf_map_info *map_info, void *key, int lookup_errno)
212 {
213 	/* For prog_array maps or arrays of maps, failure to lookup the value
214 	 * means there is no entry for that key. Do not print an error message
215 	 * in that case.
216 	 */
217 	if ((map_is_map_of_maps(map_info->type) ||
218 	     map_is_map_of_progs(map_info->type)) && lookup_errno == ENOENT)
219 		return;
220 
221 	if (json_output) {
222 		jsonw_start_object(json_wtr);	/* entry */
223 		jsonw_name(json_wtr, "key");
224 		print_hex_data_json(key, map_info->key_size);
225 		jsonw_name(json_wtr, "value");
226 		jsonw_start_object(json_wtr);	/* error */
227 		jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
228 		jsonw_end_object(json_wtr);	/* error */
229 		jsonw_end_object(json_wtr);	/* entry */
230 	} else {
231 		const char *msg = NULL;
232 
233 		if (lookup_errno == ENOENT)
234 			msg = "<no entry>";
235 		else if (lookup_errno == ENOSPC &&
236 			 map_info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
237 			msg = "<cannot read>";
238 
239 		print_entry_error_msg(map_info, key,
240 				      msg ? : strerror(lookup_errno));
241 	}
242 }
243 
244 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
245 			      unsigned char *value)
246 {
247 	if (!map_is_per_cpu(info->type)) {
248 		bool single_line, break_names;
249 
250 		break_names = info->key_size > 16 || info->value_size > 16;
251 		single_line = info->key_size + info->value_size <= 24 &&
252 			!break_names;
253 
254 		if (info->key_size) {
255 			printf("key:%c", break_names ? '\n' : ' ');
256 			fprint_hex(stdout, key, info->key_size, " ");
257 
258 			printf(single_line ? "  " : "\n");
259 		}
260 
261 		if (info->value_size) {
262 			printf("value:%c", break_names ? '\n' : ' ');
263 			fprint_hex(stdout, value, info->value_size, " ");
264 		}
265 
266 		printf("\n");
267 	} else {
268 		unsigned int i, n, step;
269 
270 		n = get_possible_cpus();
271 		step = round_up(info->value_size, 8);
272 
273 		if (info->key_size) {
274 			printf("key:\n");
275 			fprint_hex(stdout, key, info->key_size, " ");
276 			printf("\n");
277 		}
278 		if (info->value_size) {
279 			for (i = 0; i < n; i++) {
280 				printf("value (CPU %02d):%c",
281 				       i, info->value_size > 16 ? '\n' : ' ');
282 				fprint_hex(stdout, value + i * step,
283 					   info->value_size, " ");
284 				printf("\n");
285 			}
286 		}
287 	}
288 }
289 
290 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
291 			  unsigned int n)
292 {
293 	unsigned int i = 0, base = 0;
294 	char *endptr;
295 
296 	if (is_prefix(*argv, "hex")) {
297 		base = 16;
298 		argv++;
299 	}
300 
301 	while (i < n && argv[i]) {
302 		val[i] = strtoul(argv[i], &endptr, base);
303 		if (*endptr) {
304 			p_err("error parsing byte: %s", argv[i]);
305 			return NULL;
306 		}
307 		i++;
308 	}
309 
310 	if (i != n) {
311 		p_err("%s expected %d bytes got %d", name, n, i);
312 		return NULL;
313 	}
314 
315 	return argv + i;
316 }
317 
318 /* on per cpu maps we must copy the provided value on all value instances */
319 static void fill_per_cpu_value(struct bpf_map_info *info, void *value)
320 {
321 	unsigned int i, n, step;
322 
323 	if (!map_is_per_cpu(info->type))
324 		return;
325 
326 	n = get_possible_cpus();
327 	step = round_up(info->value_size, 8);
328 	for (i = 1; i < n; i++)
329 		memcpy(value + i * step, value, info->value_size);
330 }
331 
332 static int parse_elem(char **argv, struct bpf_map_info *info,
333 		      void *key, void *value, __u32 key_size, __u32 value_size,
334 		      __u32 *flags, __u32 **value_fd)
335 {
336 	if (!*argv) {
337 		if (!key && !value)
338 			return 0;
339 		p_err("did not find %s", key ? "key" : "value");
340 		return -1;
341 	}
342 
343 	if (is_prefix(*argv, "key")) {
344 		if (!key) {
345 			if (key_size)
346 				p_err("duplicate key");
347 			else
348 				p_err("unnecessary key");
349 			return -1;
350 		}
351 
352 		argv = parse_bytes(argv + 1, "key", key, key_size);
353 		if (!argv)
354 			return -1;
355 
356 		return parse_elem(argv, info, NULL, value, key_size, value_size,
357 				  flags, value_fd);
358 	} else if (is_prefix(*argv, "value")) {
359 		int fd;
360 
361 		if (!value) {
362 			if (value_size)
363 				p_err("duplicate value");
364 			else
365 				p_err("unnecessary value");
366 			return -1;
367 		}
368 
369 		argv++;
370 
371 		if (map_is_map_of_maps(info->type)) {
372 			int argc = 2;
373 
374 			if (value_size != 4) {
375 				p_err("value smaller than 4B for map in map?");
376 				return -1;
377 			}
378 			if (!argv[0] || !argv[1]) {
379 				p_err("not enough value arguments for map in map");
380 				return -1;
381 			}
382 
383 			fd = map_parse_fd(&argc, &argv);
384 			if (fd < 0)
385 				return -1;
386 
387 			*value_fd = value;
388 			**value_fd = fd;
389 		} else if (map_is_map_of_progs(info->type)) {
390 			int argc = 2;
391 
392 			if (value_size != 4) {
393 				p_err("value smaller than 4B for map of progs?");
394 				return -1;
395 			}
396 			if (!argv[0] || !argv[1]) {
397 				p_err("not enough value arguments for map of progs");
398 				return -1;
399 			}
400 			if (is_prefix(*argv, "id"))
401 				p_info("Warning: updating program array via MAP_ID, make sure this map is kept open\n"
402 				       "         by some process or pinned otherwise update will be lost");
403 
404 			fd = prog_parse_fd(&argc, &argv);
405 			if (fd < 0)
406 				return -1;
407 
408 			*value_fd = value;
409 			**value_fd = fd;
410 		} else {
411 			argv = parse_bytes(argv, "value", value, value_size);
412 			if (!argv)
413 				return -1;
414 
415 			fill_per_cpu_value(info, value);
416 		}
417 
418 		return parse_elem(argv, info, key, NULL, key_size, value_size,
419 				  flags, NULL);
420 	} else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
421 		   is_prefix(*argv, "exist")) {
422 		if (!flags) {
423 			p_err("flags specified multiple times: %s", *argv);
424 			return -1;
425 		}
426 
427 		if (is_prefix(*argv, "any"))
428 			*flags = BPF_ANY;
429 		else if (is_prefix(*argv, "noexist"))
430 			*flags = BPF_NOEXIST;
431 		else if (is_prefix(*argv, "exist"))
432 			*flags = BPF_EXIST;
433 
434 		return parse_elem(argv + 1, info, key, value, key_size,
435 				  value_size, NULL, value_fd);
436 	}
437 
438 	p_err("expected key or value, got: %s", *argv);
439 	return -1;
440 }
441 
442 static void show_map_header_json(struct bpf_map_info *info, json_writer_t *wtr)
443 {
444 	const char *map_type_str;
445 
446 	jsonw_uint_field(wtr, "id", info->id);
447 	map_type_str = libbpf_bpf_map_type_str(info->type);
448 	if (map_type_str)
449 		jsonw_string_field(wtr, "type", map_type_str);
450 	else
451 		jsonw_uint_field(wtr, "type", info->type);
452 
453 	if (*info->name)
454 		jsonw_string_field(wtr, "name", info->name);
455 
456 	jsonw_name(wtr, "flags");
457 	jsonw_printf(wtr, "%d", info->map_flags);
458 }
459 
460 static int show_map_close_json(int fd, struct bpf_map_info *info)
461 {
462 	char *memlock, *frozen_str;
463 	int frozen = 0;
464 
465 	memlock = get_fdinfo(fd, "memlock");
466 	frozen_str = get_fdinfo(fd, "frozen");
467 
468 	jsonw_start_object(json_wtr);
469 
470 	show_map_header_json(info, json_wtr);
471 
472 	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
473 
474 	jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
475 	jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
476 	jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
477 
478 	if (memlock)
479 		jsonw_int_field(json_wtr, "bytes_memlock", atoll(memlock));
480 	free(memlock);
481 
482 	if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
483 		char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
484 		char *owner_jited = get_fdinfo(fd, "owner_jited");
485 
486 		if (owner_prog_type) {
487 			unsigned int prog_type = atoi(owner_prog_type);
488 			const char *prog_type_str;
489 
490 			prog_type_str = libbpf_bpf_prog_type_str(prog_type);
491 			if (prog_type_str)
492 				jsonw_string_field(json_wtr, "owner_prog_type",
493 						   prog_type_str);
494 			else
495 				jsonw_uint_field(json_wtr, "owner_prog_type",
496 						 prog_type);
497 		}
498 		if (owner_jited)
499 			jsonw_bool_field(json_wtr, "owner_jited",
500 					 !!atoi(owner_jited));
501 
502 		free(owner_prog_type);
503 		free(owner_jited);
504 	}
505 	close(fd);
506 
507 	if (frozen_str) {
508 		frozen = atoi(frozen_str);
509 		free(frozen_str);
510 	}
511 	jsonw_int_field(json_wtr, "frozen", frozen);
512 
513 	if (info->btf_id)
514 		jsonw_int_field(json_wtr, "btf_id", info->btf_id);
515 
516 	if (!hashmap__empty(map_table)) {
517 		struct hashmap_entry *entry;
518 
519 		jsonw_name(json_wtr, "pinned");
520 		jsonw_start_array(json_wtr);
521 		hashmap__for_each_key_entry(map_table, entry,
522 					    u32_as_hash_field(info->id))
523 			jsonw_string(json_wtr, entry->value);
524 		jsonw_end_array(json_wtr);
525 	}
526 
527 	emit_obj_refs_json(refs_table, info->id, json_wtr);
528 
529 	jsonw_end_object(json_wtr);
530 
531 	return 0;
532 }
533 
534 static void show_map_header_plain(struct bpf_map_info *info)
535 {
536 	const char *map_type_str;
537 
538 	printf("%u: ", info->id);
539 
540 	map_type_str = libbpf_bpf_map_type_str(info->type);
541 	if (map_type_str)
542 		printf("%s  ", map_type_str);
543 	else
544 		printf("type %u  ", info->type);
545 
546 	if (*info->name)
547 		printf("name %s  ", info->name);
548 
549 	printf("flags 0x%x", info->map_flags);
550 	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
551 	printf("\n");
552 }
553 
554 static int show_map_close_plain(int fd, struct bpf_map_info *info)
555 {
556 	char *memlock, *frozen_str;
557 	int frozen = 0;
558 
559 	memlock = get_fdinfo(fd, "memlock");
560 	frozen_str = get_fdinfo(fd, "frozen");
561 
562 	show_map_header_plain(info);
563 	printf("\tkey %uB  value %uB  max_entries %u",
564 	       info->key_size, info->value_size, info->max_entries);
565 
566 	if (memlock)
567 		printf("  memlock %sB", memlock);
568 	free(memlock);
569 
570 	if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
571 		char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
572 		char *owner_jited = get_fdinfo(fd, "owner_jited");
573 
574 		if (owner_prog_type || owner_jited)
575 			printf("\n\t");
576 		if (owner_prog_type) {
577 			unsigned int prog_type = atoi(owner_prog_type);
578 			const char *prog_type_str;
579 
580 			prog_type_str = libbpf_bpf_prog_type_str(prog_type);
581 			if (prog_type_str)
582 				printf("owner_prog_type %s  ", prog_type_str);
583 			else
584 				printf("owner_prog_type %d  ", prog_type);
585 		}
586 		if (owner_jited)
587 			printf("owner%s jited",
588 			       atoi(owner_jited) ? "" : " not");
589 
590 		free(owner_prog_type);
591 		free(owner_jited);
592 	}
593 	close(fd);
594 
595 	if (!hashmap__empty(map_table)) {
596 		struct hashmap_entry *entry;
597 
598 		hashmap__for_each_key_entry(map_table, entry,
599 					    u32_as_hash_field(info->id))
600 			printf("\n\tpinned %s", (char *)entry->value);
601 	}
602 
603 	if (frozen_str) {
604 		frozen = atoi(frozen_str);
605 		free(frozen_str);
606 	}
607 
608 	if (info->btf_id || frozen)
609 		printf("\n\t");
610 
611 	if (info->btf_id)
612 		printf("btf_id %d", info->btf_id);
613 
614 	if (frozen)
615 		printf("%sfrozen", info->btf_id ? "  " : "");
616 
617 	emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
618 
619 	printf("\n");
620 	return 0;
621 }
622 
623 static int do_show_subset(int argc, char **argv)
624 {
625 	struct bpf_map_info info = {};
626 	__u32 len = sizeof(info);
627 	int *fds = NULL;
628 	int nb_fds, i;
629 	int err = -1;
630 
631 	fds = malloc(sizeof(int));
632 	if (!fds) {
633 		p_err("mem alloc failed");
634 		return -1;
635 	}
636 	nb_fds = map_parse_fds(&argc, &argv, &fds);
637 	if (nb_fds < 1)
638 		goto exit_free;
639 
640 	if (json_output && nb_fds > 1)
641 		jsonw_start_array(json_wtr);	/* root array */
642 	for (i = 0; i < nb_fds; i++) {
643 		err = bpf_obj_get_info_by_fd(fds[i], &info, &len);
644 		if (err) {
645 			p_err("can't get map info: %s",
646 			      strerror(errno));
647 			for (; i < nb_fds; i++)
648 				close(fds[i]);
649 			break;
650 		}
651 
652 		if (json_output)
653 			show_map_close_json(fds[i], &info);
654 		else
655 			show_map_close_plain(fds[i], &info);
656 
657 		close(fds[i]);
658 	}
659 	if (json_output && nb_fds > 1)
660 		jsonw_end_array(json_wtr);	/* root array */
661 
662 exit_free:
663 	free(fds);
664 	return err;
665 }
666 
667 static int do_show(int argc, char **argv)
668 {
669 	struct bpf_map_info info = {};
670 	__u32 len = sizeof(info);
671 	__u32 id = 0;
672 	int err;
673 	int fd;
674 
675 	if (show_pinned) {
676 		map_table = hashmap__new(hash_fn_for_key_as_id,
677 					 equal_fn_for_key_as_id, NULL);
678 		if (IS_ERR(map_table)) {
679 			p_err("failed to create hashmap for pinned paths");
680 			return -1;
681 		}
682 		build_pinned_obj_table(map_table, BPF_OBJ_MAP);
683 	}
684 	build_obj_refs_table(&refs_table, BPF_OBJ_MAP);
685 
686 	if (argc == 2)
687 		return do_show_subset(argc, argv);
688 
689 	if (argc)
690 		return BAD_ARG();
691 
692 	if (json_output)
693 		jsonw_start_array(json_wtr);
694 	while (true) {
695 		err = bpf_map_get_next_id(id, &id);
696 		if (err) {
697 			if (errno == ENOENT)
698 				break;
699 			p_err("can't get next map: %s%s", strerror(errno),
700 			      errno == EINVAL ? " -- kernel too old?" : "");
701 			break;
702 		}
703 
704 		fd = bpf_map_get_fd_by_id(id);
705 		if (fd < 0) {
706 			if (errno == ENOENT)
707 				continue;
708 			p_err("can't get map by id (%u): %s",
709 			      id, strerror(errno));
710 			break;
711 		}
712 
713 		err = bpf_obj_get_info_by_fd(fd, &info, &len);
714 		if (err) {
715 			p_err("can't get map info: %s", strerror(errno));
716 			close(fd);
717 			break;
718 		}
719 
720 		if (json_output)
721 			show_map_close_json(fd, &info);
722 		else
723 			show_map_close_plain(fd, &info);
724 	}
725 	if (json_output)
726 		jsonw_end_array(json_wtr);
727 
728 	delete_obj_refs_table(refs_table);
729 
730 	if (show_pinned)
731 		delete_pinned_obj_table(map_table);
732 
733 	return errno == ENOENT ? 0 : -1;
734 }
735 
736 static int dump_map_elem(int fd, void *key, void *value,
737 			 struct bpf_map_info *map_info, struct btf *btf,
738 			 json_writer_t *btf_wtr)
739 {
740 	if (bpf_map_lookup_elem(fd, key, value)) {
741 		print_entry_error(map_info, key, errno);
742 		return -1;
743 	}
744 
745 	if (json_output) {
746 		print_entry_json(map_info, key, value, btf);
747 	} else if (btf) {
748 		struct btf_dumper d = {
749 			.btf = btf,
750 			.jw = btf_wtr,
751 			.is_plain_text = true,
752 		};
753 
754 		do_dump_btf(&d, map_info, key, value);
755 	} else {
756 		print_entry_plain(map_info, key, value);
757 	}
758 
759 	return 0;
760 }
761 
762 static int maps_have_btf(int *fds, int nb_fds)
763 {
764 	struct bpf_map_info info = {};
765 	__u32 len = sizeof(info);
766 	int err, i;
767 
768 	for (i = 0; i < nb_fds; i++) {
769 		err = bpf_obj_get_info_by_fd(fds[i], &info, &len);
770 		if (err) {
771 			p_err("can't get map info: %s", strerror(errno));
772 			return -1;
773 		}
774 
775 		if (!info.btf_id)
776 			return 0;
777 	}
778 
779 	return 1;
780 }
781 
782 static struct btf *btf_vmlinux;
783 
784 static int get_map_kv_btf(const struct bpf_map_info *info, struct btf **btf)
785 {
786 	int err = 0;
787 
788 	if (info->btf_vmlinux_value_type_id) {
789 		if (!btf_vmlinux) {
790 			btf_vmlinux = libbpf_find_kernel_btf();
791 			err = libbpf_get_error(btf_vmlinux);
792 			if (err) {
793 				p_err("failed to get kernel btf");
794 				return err;
795 			}
796 		}
797 		*btf = btf_vmlinux;
798 	} else if (info->btf_value_type_id) {
799 		*btf = btf__load_from_kernel_by_id(info->btf_id);
800 		err = libbpf_get_error(*btf);
801 		if (err)
802 			p_err("failed to get btf");
803 	} else {
804 		*btf = NULL;
805 	}
806 
807 	return err;
808 }
809 
810 static void free_map_kv_btf(struct btf *btf)
811 {
812 	if (!libbpf_get_error(btf) && btf != btf_vmlinux)
813 		btf__free(btf);
814 }
815 
816 static void free_btf_vmlinux(void)
817 {
818 	if (!libbpf_get_error(btf_vmlinux))
819 		btf__free(btf_vmlinux);
820 }
821 
822 static int
823 map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
824 	 bool show_header)
825 {
826 	void *key, *value, *prev_key;
827 	unsigned int num_elems = 0;
828 	struct btf *btf = NULL;
829 	int err;
830 
831 	key = malloc(info->key_size);
832 	value = alloc_value(info);
833 	if (!key || !value) {
834 		p_err("mem alloc failed");
835 		err = -1;
836 		goto exit_free;
837 	}
838 
839 	prev_key = NULL;
840 
841 	if (wtr) {
842 		err = get_map_kv_btf(info, &btf);
843 		if (err) {
844 			goto exit_free;
845 		}
846 
847 		if (show_header) {
848 			jsonw_start_object(wtr);	/* map object */
849 			show_map_header_json(info, wtr);
850 			jsonw_name(wtr, "elements");
851 		}
852 		jsonw_start_array(wtr);		/* elements */
853 	} else if (show_header) {
854 		show_map_header_plain(info);
855 	}
856 
857 	if (info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY &&
858 	    info->value_size != 8) {
859 		const char *map_type_str;
860 
861 		map_type_str = libbpf_bpf_map_type_str(info->type);
862 		p_info("Warning: cannot read values from %s map with value_size != 8",
863 		       map_type_str);
864 	}
865 	while (true) {
866 		err = bpf_map_get_next_key(fd, prev_key, key);
867 		if (err) {
868 			if (errno == ENOENT)
869 				err = 0;
870 			break;
871 		}
872 		if (!dump_map_elem(fd, key, value, info, btf, wtr))
873 			num_elems++;
874 		prev_key = key;
875 	}
876 
877 	if (wtr) {
878 		jsonw_end_array(wtr);	/* elements */
879 		if (show_header)
880 			jsonw_end_object(wtr);	/* map object */
881 	} else {
882 		printf("Found %u element%s\n", num_elems,
883 		       num_elems != 1 ? "s" : "");
884 	}
885 
886 exit_free:
887 	free(key);
888 	free(value);
889 	close(fd);
890 	free_map_kv_btf(btf);
891 
892 	return err;
893 }
894 
895 static int do_dump(int argc, char **argv)
896 {
897 	json_writer_t *wtr = NULL, *btf_wtr = NULL;
898 	struct bpf_map_info info = {};
899 	int nb_fds, i = 0;
900 	__u32 len = sizeof(info);
901 	int *fds = NULL;
902 	int err = -1;
903 
904 	if (argc != 2)
905 		usage();
906 
907 	fds = malloc(sizeof(int));
908 	if (!fds) {
909 		p_err("mem alloc failed");
910 		return -1;
911 	}
912 	nb_fds = map_parse_fds(&argc, &argv, &fds);
913 	if (nb_fds < 1)
914 		goto exit_free;
915 
916 	if (json_output) {
917 		wtr = json_wtr;
918 	} else {
919 		int do_plain_btf;
920 
921 		do_plain_btf = maps_have_btf(fds, nb_fds);
922 		if (do_plain_btf < 0)
923 			goto exit_close;
924 
925 		if (do_plain_btf) {
926 			btf_wtr = get_btf_writer();
927 			wtr = btf_wtr;
928 			if (!btf_wtr)
929 				p_info("failed to create json writer for btf. falling back to plain output");
930 		}
931 	}
932 
933 	if (wtr && nb_fds > 1)
934 		jsonw_start_array(wtr);	/* root array */
935 	for (i = 0; i < nb_fds; i++) {
936 		if (bpf_obj_get_info_by_fd(fds[i], &info, &len)) {
937 			p_err("can't get map info: %s", strerror(errno));
938 			break;
939 		}
940 		err = map_dump(fds[i], &info, wtr, nb_fds > 1);
941 		if (!wtr && i != nb_fds - 1)
942 			printf("\n");
943 
944 		if (err)
945 			break;
946 		close(fds[i]);
947 	}
948 	if (wtr && nb_fds > 1)
949 		jsonw_end_array(wtr);	/* root array */
950 
951 	if (btf_wtr)
952 		jsonw_destroy(&btf_wtr);
953 exit_close:
954 	for (; i < nb_fds; i++)
955 		close(fds[i]);
956 exit_free:
957 	free(fds);
958 	free_btf_vmlinux();
959 	return err;
960 }
961 
962 static int alloc_key_value(struct bpf_map_info *info, void **key, void **value)
963 {
964 	*key = NULL;
965 	*value = NULL;
966 
967 	if (info->key_size) {
968 		*key = malloc(info->key_size);
969 		if (!*key) {
970 			p_err("key mem alloc failed");
971 			return -1;
972 		}
973 	}
974 
975 	if (info->value_size) {
976 		*value = alloc_value(info);
977 		if (!*value) {
978 			p_err("value mem alloc failed");
979 			free(*key);
980 			*key = NULL;
981 			return -1;
982 		}
983 	}
984 
985 	return 0;
986 }
987 
988 static int do_update(int argc, char **argv)
989 {
990 	struct bpf_map_info info = {};
991 	__u32 len = sizeof(info);
992 	__u32 *value_fd = NULL;
993 	__u32 flags = BPF_ANY;
994 	void *key, *value;
995 	int fd, err;
996 
997 	if (argc < 2)
998 		usage();
999 
1000 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1001 	if (fd < 0)
1002 		return -1;
1003 
1004 	err = alloc_key_value(&info, &key, &value);
1005 	if (err)
1006 		goto exit_free;
1007 
1008 	err = parse_elem(argv, &info, key, value, info.key_size,
1009 			 info.value_size, &flags, &value_fd);
1010 	if (err)
1011 		goto exit_free;
1012 
1013 	err = bpf_map_update_elem(fd, key, value, flags);
1014 	if (err) {
1015 		p_err("update failed: %s", strerror(errno));
1016 		goto exit_free;
1017 	}
1018 
1019 exit_free:
1020 	if (value_fd)
1021 		close(*value_fd);
1022 	free(key);
1023 	free(value);
1024 	close(fd);
1025 
1026 	if (!err && json_output)
1027 		jsonw_null(json_wtr);
1028 	return err;
1029 }
1030 
1031 static void print_key_value(struct bpf_map_info *info, void *key,
1032 			    void *value)
1033 {
1034 	json_writer_t *btf_wtr;
1035 	struct btf *btf;
1036 
1037 	if (get_map_kv_btf(info, &btf))
1038 		return;
1039 
1040 	if (json_output) {
1041 		print_entry_json(info, key, value, btf);
1042 	} else if (btf) {
1043 		/* if here json_wtr wouldn't have been initialised,
1044 		 * so let's create separate writer for btf
1045 		 */
1046 		btf_wtr = get_btf_writer();
1047 		if (!btf_wtr) {
1048 			p_info("failed to create json writer for btf. falling back to plain output");
1049 			btf__free(btf);
1050 			btf = NULL;
1051 			print_entry_plain(info, key, value);
1052 		} else {
1053 			struct btf_dumper d = {
1054 				.btf = btf,
1055 				.jw = btf_wtr,
1056 				.is_plain_text = true,
1057 			};
1058 
1059 			do_dump_btf(&d, info, key, value);
1060 			jsonw_destroy(&btf_wtr);
1061 		}
1062 	} else {
1063 		print_entry_plain(info, key, value);
1064 	}
1065 	btf__free(btf);
1066 }
1067 
1068 static int do_lookup(int argc, char **argv)
1069 {
1070 	struct bpf_map_info info = {};
1071 	__u32 len = sizeof(info);
1072 	void *key, *value;
1073 	int err;
1074 	int fd;
1075 
1076 	if (argc < 2)
1077 		usage();
1078 
1079 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1080 	if (fd < 0)
1081 		return -1;
1082 
1083 	err = alloc_key_value(&info, &key, &value);
1084 	if (err)
1085 		goto exit_free;
1086 
1087 	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
1088 	if (err)
1089 		goto exit_free;
1090 
1091 	err = bpf_map_lookup_elem(fd, key, value);
1092 	if (err) {
1093 		if (errno == ENOENT) {
1094 			if (json_output) {
1095 				jsonw_null(json_wtr);
1096 			} else {
1097 				printf("key:\n");
1098 				fprint_hex(stdout, key, info.key_size, " ");
1099 				printf("\n\nNot found\n");
1100 			}
1101 		} else {
1102 			p_err("lookup failed: %s", strerror(errno));
1103 		}
1104 
1105 		goto exit_free;
1106 	}
1107 
1108 	/* here means bpf_map_lookup_elem() succeeded */
1109 	print_key_value(&info, key, value);
1110 
1111 exit_free:
1112 	free(key);
1113 	free(value);
1114 	close(fd);
1115 
1116 	return err;
1117 }
1118 
1119 static int do_getnext(int argc, char **argv)
1120 {
1121 	struct bpf_map_info info = {};
1122 	__u32 len = sizeof(info);
1123 	void *key, *nextkey;
1124 	int err;
1125 	int fd;
1126 
1127 	if (argc < 2)
1128 		usage();
1129 
1130 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1131 	if (fd < 0)
1132 		return -1;
1133 
1134 	key = malloc(info.key_size);
1135 	nextkey = malloc(info.key_size);
1136 	if (!key || !nextkey) {
1137 		p_err("mem alloc failed");
1138 		err = -1;
1139 		goto exit_free;
1140 	}
1141 
1142 	if (argc) {
1143 		err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
1144 				 NULL, NULL);
1145 		if (err)
1146 			goto exit_free;
1147 	} else {
1148 		free(key);
1149 		key = NULL;
1150 	}
1151 
1152 	err = bpf_map_get_next_key(fd, key, nextkey);
1153 	if (err) {
1154 		p_err("can't get next key: %s", strerror(errno));
1155 		goto exit_free;
1156 	}
1157 
1158 	if (json_output) {
1159 		jsonw_start_object(json_wtr);
1160 		if (key) {
1161 			jsonw_name(json_wtr, "key");
1162 			print_hex_data_json(key, info.key_size);
1163 		} else {
1164 			jsonw_null_field(json_wtr, "key");
1165 		}
1166 		jsonw_name(json_wtr, "next_key");
1167 		print_hex_data_json(nextkey, info.key_size);
1168 		jsonw_end_object(json_wtr);
1169 	} else {
1170 		if (key) {
1171 			printf("key:\n");
1172 			fprint_hex(stdout, key, info.key_size, " ");
1173 			printf("\n");
1174 		} else {
1175 			printf("key: None\n");
1176 		}
1177 		printf("next key:\n");
1178 		fprint_hex(stdout, nextkey, info.key_size, " ");
1179 		printf("\n");
1180 	}
1181 
1182 exit_free:
1183 	free(nextkey);
1184 	free(key);
1185 	close(fd);
1186 
1187 	return err;
1188 }
1189 
1190 static int do_delete(int argc, char **argv)
1191 {
1192 	struct bpf_map_info info = {};
1193 	__u32 len = sizeof(info);
1194 	void *key;
1195 	int err;
1196 	int fd;
1197 
1198 	if (argc < 2)
1199 		usage();
1200 
1201 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1202 	if (fd < 0)
1203 		return -1;
1204 
1205 	key = malloc(info.key_size);
1206 	if (!key) {
1207 		p_err("mem alloc failed");
1208 		err = -1;
1209 		goto exit_free;
1210 	}
1211 
1212 	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
1213 	if (err)
1214 		goto exit_free;
1215 
1216 	err = bpf_map_delete_elem(fd, key);
1217 	if (err)
1218 		p_err("delete failed: %s", strerror(errno));
1219 
1220 exit_free:
1221 	free(key);
1222 	close(fd);
1223 
1224 	if (!err && json_output)
1225 		jsonw_null(json_wtr);
1226 	return err;
1227 }
1228 
1229 static int do_pin(int argc, char **argv)
1230 {
1231 	int err;
1232 
1233 	err = do_pin_any(argc, argv, map_parse_fd);
1234 	if (!err && json_output)
1235 		jsonw_null(json_wtr);
1236 	return err;
1237 }
1238 
1239 static int do_create(int argc, char **argv)
1240 {
1241 	LIBBPF_OPTS(bpf_map_create_opts, attr);
1242 	enum bpf_map_type map_type = BPF_MAP_TYPE_UNSPEC;
1243 	__u32 key_size = 0, value_size = 0, max_entries = 0;
1244 	const char *map_name = NULL;
1245 	const char *pinfile;
1246 	int err = -1, fd;
1247 
1248 	if (!REQ_ARGS(7))
1249 		return -1;
1250 	pinfile = GET_ARG();
1251 
1252 	while (argc) {
1253 		if (!REQ_ARGS(2))
1254 			return -1;
1255 
1256 		if (is_prefix(*argv, "type")) {
1257 			NEXT_ARG();
1258 
1259 			if (map_type) {
1260 				p_err("map type already specified");
1261 				goto exit;
1262 			}
1263 
1264 			map_type = map_type_from_str(*argv);
1265 			if ((int)map_type < 0) {
1266 				p_err("unrecognized map type: %s", *argv);
1267 				goto exit;
1268 			}
1269 			NEXT_ARG();
1270 		} else if (is_prefix(*argv, "name")) {
1271 			NEXT_ARG();
1272 			map_name = GET_ARG();
1273 		} else if (is_prefix(*argv, "key")) {
1274 			if (parse_u32_arg(&argc, &argv, &key_size,
1275 					  "key size"))
1276 				goto exit;
1277 		} else if (is_prefix(*argv, "value")) {
1278 			if (parse_u32_arg(&argc, &argv, &value_size,
1279 					  "value size"))
1280 				goto exit;
1281 		} else if (is_prefix(*argv, "entries")) {
1282 			if (parse_u32_arg(&argc, &argv, &max_entries,
1283 					  "max entries"))
1284 				goto exit;
1285 		} else if (is_prefix(*argv, "flags")) {
1286 			if (parse_u32_arg(&argc, &argv, &attr.map_flags,
1287 					  "flags"))
1288 				goto exit;
1289 		} else if (is_prefix(*argv, "dev")) {
1290 			NEXT_ARG();
1291 
1292 			if (attr.map_ifindex) {
1293 				p_err("offload device already specified");
1294 				goto exit;
1295 			}
1296 
1297 			attr.map_ifindex = if_nametoindex(*argv);
1298 			if (!attr.map_ifindex) {
1299 				p_err("unrecognized netdevice '%s': %s",
1300 				      *argv, strerror(errno));
1301 				goto exit;
1302 			}
1303 			NEXT_ARG();
1304 		} else if (is_prefix(*argv, "inner_map")) {
1305 			struct bpf_map_info info = {};
1306 			__u32 len = sizeof(info);
1307 			int inner_map_fd;
1308 
1309 			NEXT_ARG();
1310 			if (!REQ_ARGS(2))
1311 				usage();
1312 			inner_map_fd = map_parse_fd_and_info(&argc, &argv,
1313 							     &info, &len);
1314 			if (inner_map_fd < 0)
1315 				return -1;
1316 			attr.inner_map_fd = inner_map_fd;
1317 		} else {
1318 			p_err("unknown arg %s", *argv);
1319 			goto exit;
1320 		}
1321 	}
1322 
1323 	if (!map_name) {
1324 		p_err("map name not specified");
1325 		goto exit;
1326 	}
1327 
1328 	set_max_rlimit();
1329 
1330 	fd = bpf_map_create(map_type, map_name, key_size, value_size, max_entries, &attr);
1331 	if (fd < 0) {
1332 		p_err("map create failed: %s", strerror(errno));
1333 		goto exit;
1334 	}
1335 
1336 	err = do_pin_fd(fd, pinfile);
1337 	close(fd);
1338 	if (err)
1339 		goto exit;
1340 
1341 	if (json_output)
1342 		jsonw_null(json_wtr);
1343 
1344 exit:
1345 	if (attr.inner_map_fd > 0)
1346 		close(attr.inner_map_fd);
1347 
1348 	return err;
1349 }
1350 
1351 static int do_pop_dequeue(int argc, char **argv)
1352 {
1353 	struct bpf_map_info info = {};
1354 	__u32 len = sizeof(info);
1355 	void *key, *value;
1356 	int err;
1357 	int fd;
1358 
1359 	if (argc < 2)
1360 		usage();
1361 
1362 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1363 	if (fd < 0)
1364 		return -1;
1365 
1366 	err = alloc_key_value(&info, &key, &value);
1367 	if (err)
1368 		goto exit_free;
1369 
1370 	err = bpf_map_lookup_and_delete_elem(fd, key, value);
1371 	if (err) {
1372 		if (errno == ENOENT) {
1373 			if (json_output)
1374 				jsonw_null(json_wtr);
1375 			else
1376 				printf("Error: empty map\n");
1377 		} else {
1378 			p_err("pop failed: %s", strerror(errno));
1379 		}
1380 
1381 		goto exit_free;
1382 	}
1383 
1384 	print_key_value(&info, key, value);
1385 
1386 exit_free:
1387 	free(key);
1388 	free(value);
1389 	close(fd);
1390 
1391 	return err;
1392 }
1393 
1394 static int do_freeze(int argc, char **argv)
1395 {
1396 	int err, fd;
1397 
1398 	if (!REQ_ARGS(2))
1399 		return -1;
1400 
1401 	fd = map_parse_fd(&argc, &argv);
1402 	if (fd < 0)
1403 		return -1;
1404 
1405 	if (argc) {
1406 		close(fd);
1407 		return BAD_ARG();
1408 	}
1409 
1410 	err = bpf_map_freeze(fd);
1411 	close(fd);
1412 	if (err) {
1413 		p_err("failed to freeze map: %s", strerror(errno));
1414 		return err;
1415 	}
1416 
1417 	if (json_output)
1418 		jsonw_null(json_wtr);
1419 
1420 	return 0;
1421 }
1422 
1423 static int do_help(int argc, char **argv)
1424 {
1425 	if (json_output) {
1426 		jsonw_null(json_wtr);
1427 		return 0;
1428 	}
1429 
1430 	fprintf(stderr,
1431 		"Usage: %1$s %2$s { show | list }   [MAP]\n"
1432 		"       %1$s %2$s create     FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n"
1433 		"                                  entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
1434 		"                                  [inner_map MAP] [dev NAME]\n"
1435 		"       %1$s %2$s dump       MAP\n"
1436 		"       %1$s %2$s update     MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
1437 		"       %1$s %2$s lookup     MAP [key DATA]\n"
1438 		"       %1$s %2$s getnext    MAP [key DATA]\n"
1439 		"       %1$s %2$s delete     MAP  key DATA\n"
1440 		"       %1$s %2$s pin        MAP  FILE\n"
1441 		"       %1$s %2$s event_pipe MAP [cpu N index M]\n"
1442 		"       %1$s %2$s peek       MAP\n"
1443 		"       %1$s %2$s push       MAP value VALUE\n"
1444 		"       %1$s %2$s pop        MAP\n"
1445 		"       %1$s %2$s enqueue    MAP value VALUE\n"
1446 		"       %1$s %2$s dequeue    MAP\n"
1447 		"       %1$s %2$s freeze     MAP\n"
1448 		"       %1$s %2$s help\n"
1449 		"\n"
1450 		"       " HELP_SPEC_MAP "\n"
1451 		"       DATA := { [hex] BYTES }\n"
1452 		"       " HELP_SPEC_PROGRAM "\n"
1453 		"       VALUE := { DATA | MAP | PROG }\n"
1454 		"       UPDATE_FLAGS := { any | exist | noexist }\n"
1455 		"       TYPE := { hash | array | prog_array | perf_event_array | percpu_hash |\n"
1456 		"                 percpu_array | stack_trace | cgroup_array | lru_hash |\n"
1457 		"                 lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n"
1458 		"                 devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash |\n"
1459 		"                 cgroup_storage | reuseport_sockarray | percpu_cgroup_storage |\n"
1460 		"                 queue | stack | sk_storage | struct_ops | ringbuf | inode_storage |\n"
1461 		"                 task_storage | bloom_filter | user_ringbuf | cgrp_storage }\n"
1462 		"       " HELP_SPEC_OPTIONS " |\n"
1463 		"                    {-f|--bpffs} | {-n|--nomount} }\n"
1464 		"",
1465 		bin_name, argv[-2]);
1466 
1467 	return 0;
1468 }
1469 
1470 static const struct cmd cmds[] = {
1471 	{ "show",	do_show },
1472 	{ "list",	do_show },
1473 	{ "help",	do_help },
1474 	{ "dump",	do_dump },
1475 	{ "update",	do_update },
1476 	{ "lookup",	do_lookup },
1477 	{ "getnext",	do_getnext },
1478 	{ "delete",	do_delete },
1479 	{ "pin",	do_pin },
1480 	{ "event_pipe",	do_event_pipe },
1481 	{ "create",	do_create },
1482 	{ "peek",	do_lookup },
1483 	{ "push",	do_update },
1484 	{ "enqueue",	do_update },
1485 	{ "pop",	do_pop_dequeue },
1486 	{ "dequeue",	do_pop_dequeue },
1487 	{ "freeze",	do_freeze },
1488 	{ 0 }
1489 };
1490 
1491 int do_map(int argc, char **argv)
1492 {
1493 	return cmd_select(cmds, argc, argv, do_help);
1494 }
1495