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