xref: /openbmc/linux/tools/bpf/bpftool/map.c (revision c127f98ba9aba1818a6ca3a1da5a24653a10d966)
1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
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.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
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.
23  *
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
31  * SOFTWARE.
32  */
33 
34 /* Author: Jakub Kicinski <kubakici@wp.pl> */
35 
36 #include <assert.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdbool.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 
48 #include <bpf.h>
49 
50 #include "main.h"
51 
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 };
70 
71 static unsigned int get_possible_cpus(void)
72 {
73 	static unsigned int result;
74 	char buf[128];
75 	long int n;
76 	char *ptr;
77 	int fd;
78 
79 	if (result)
80 		return result;
81 
82 	fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
83 	if (fd < 0) {
84 		p_err("can't open sysfs possible cpus");
85 		exit(-1);
86 	}
87 
88 	n = read(fd, buf, sizeof(buf));
89 	if (n < 2) {
90 		p_err("can't read sysfs possible cpus");
91 		exit(-1);
92 	}
93 	close(fd);
94 
95 	if (n == sizeof(buf)) {
96 		p_err("read sysfs possible cpus overflow");
97 		exit(-1);
98 	}
99 
100 	ptr = buf;
101 	n = 0;
102 	while (*ptr && *ptr != '\n') {
103 		unsigned int a, b;
104 
105 		if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
106 			n += b - a + 1;
107 
108 			ptr = strchr(ptr, '-') + 1;
109 		} else if (sscanf(ptr, "%u", &a) == 1) {
110 			n++;
111 		} else {
112 			assert(0);
113 		}
114 
115 		while (isdigit(*ptr))
116 			ptr++;
117 		if (*ptr == ',')
118 			ptr++;
119 	}
120 
121 	result = n;
122 
123 	return result;
124 }
125 
126 static bool map_is_per_cpu(__u32 type)
127 {
128 	return type == BPF_MAP_TYPE_PERCPU_HASH ||
129 	       type == BPF_MAP_TYPE_PERCPU_ARRAY ||
130 	       type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
131 }
132 
133 static bool map_is_map_of_maps(__u32 type)
134 {
135 	return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
136 	       type == BPF_MAP_TYPE_HASH_OF_MAPS;
137 }
138 
139 static bool map_is_map_of_progs(__u32 type)
140 {
141 	return type == BPF_MAP_TYPE_PROG_ARRAY;
142 }
143 
144 static void *alloc_value(struct bpf_map_info *info)
145 {
146 	if (map_is_per_cpu(info->type))
147 		return malloc(info->value_size * get_possible_cpus());
148 	else
149 		return malloc(info->value_size);
150 }
151 
152 static int map_parse_fd(int *argc, char ***argv)
153 {
154 	int fd;
155 
156 	if (is_prefix(**argv, "id")) {
157 		unsigned int id;
158 		char *endptr;
159 
160 		NEXT_ARGP();
161 
162 		id = strtoul(**argv, &endptr, 0);
163 		if (*endptr) {
164 			p_err("can't parse %s as ID", **argv);
165 			return -1;
166 		}
167 		NEXT_ARGP();
168 
169 		fd = bpf_map_get_fd_by_id(id);
170 		if (fd < 0)
171 			p_err("get map by id (%u): %s", id, strerror(errno));
172 		return fd;
173 	} else if (is_prefix(**argv, "pinned")) {
174 		char *path;
175 
176 		NEXT_ARGP();
177 
178 		path = **argv;
179 		NEXT_ARGP();
180 
181 		return open_obj_pinned_any(path, BPF_OBJ_MAP);
182 	}
183 
184 	p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
185 	return -1;
186 }
187 
188 static int
189 map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
190 {
191 	int err;
192 	int fd;
193 
194 	fd = map_parse_fd(argc, argv);
195 	if (fd < 0)
196 		return -1;
197 
198 	err = bpf_obj_get_info_by_fd(fd, info, info_len);
199 	if (err) {
200 		p_err("can't get map info: %s", strerror(errno));
201 		close(fd);
202 		return err;
203 	}
204 
205 	return fd;
206 }
207 
208 static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
209 			     unsigned char *value)
210 {
211 	jsonw_start_object(json_wtr);
212 
213 	if (!map_is_per_cpu(info->type)) {
214 		jsonw_name(json_wtr, "key");
215 		print_hex_data_json(key, info->key_size);
216 		jsonw_name(json_wtr, "value");
217 		print_hex_data_json(value, info->value_size);
218 	} else {
219 		unsigned int i, n;
220 
221 		n = get_possible_cpus();
222 
223 		jsonw_name(json_wtr, "key");
224 		print_hex_data_json(key, info->key_size);
225 
226 		jsonw_name(json_wtr, "values");
227 		jsonw_start_array(json_wtr);
228 		for (i = 0; i < n; i++) {
229 			jsonw_start_object(json_wtr);
230 
231 			jsonw_int_field(json_wtr, "cpu", i);
232 
233 			jsonw_name(json_wtr, "value");
234 			print_hex_data_json(value + i * info->value_size,
235 					    info->value_size);
236 
237 			jsonw_end_object(json_wtr);
238 		}
239 		jsonw_end_array(json_wtr);
240 	}
241 
242 	jsonw_end_object(json_wtr);
243 }
244 
245 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
246 			      unsigned char *value)
247 {
248 	if (!map_is_per_cpu(info->type)) {
249 		bool single_line, break_names;
250 
251 		break_names = info->key_size > 16 || info->value_size > 16;
252 		single_line = info->key_size + info->value_size <= 24 &&
253 			!break_names;
254 
255 		printf("key:%c", break_names ? '\n' : ' ');
256 		fprint_hex(stdout, key, info->key_size, " ");
257 
258 		printf(single_line ? "  " : "\n");
259 
260 		printf("value:%c", break_names ? '\n' : ' ');
261 		fprint_hex(stdout, value, info->value_size, " ");
262 
263 		printf("\n");
264 	} else {
265 		unsigned int i, n;
266 
267 		n = get_possible_cpus();
268 
269 		printf("key:\n");
270 		fprint_hex(stdout, key, info->key_size, " ");
271 		printf("\n");
272 		for (i = 0; i < n; i++) {
273 			printf("value (CPU %02d):%c",
274 			       i, info->value_size > 16 ? '\n' : ' ');
275 			fprint_hex(stdout, value + i * info->value_size,
276 				   info->value_size, " ");
277 			printf("\n");
278 		}
279 	}
280 }
281 
282 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
283 			  unsigned int n)
284 {
285 	unsigned int i = 0;
286 	char *endptr;
287 
288 	while (i < n && argv[i]) {
289 		val[i] = strtoul(argv[i], &endptr, 0);
290 		if (*endptr) {
291 			p_err("error parsing byte: %s", argv[i]);
292 			return NULL;
293 		}
294 		i++;
295 	}
296 
297 	if (i != n) {
298 		p_err("%s expected %d bytes got %d", name, n, i);
299 		return NULL;
300 	}
301 
302 	return argv + i;
303 }
304 
305 static int parse_elem(char **argv, struct bpf_map_info *info,
306 		      void *key, void *value, __u32 key_size, __u32 value_size,
307 		      __u32 *flags, __u32 **value_fd)
308 {
309 	if (!*argv) {
310 		if (!key && !value)
311 			return 0;
312 		p_err("did not find %s", key ? "key" : "value");
313 		return -1;
314 	}
315 
316 	if (is_prefix(*argv, "key")) {
317 		if (!key) {
318 			if (key_size)
319 				p_err("duplicate key");
320 			else
321 				p_err("unnecessary key");
322 			return -1;
323 		}
324 
325 		argv = parse_bytes(argv + 1, "key", key, key_size);
326 		if (!argv)
327 			return -1;
328 
329 		return parse_elem(argv, info, NULL, value, key_size, value_size,
330 				  flags, value_fd);
331 	} else if (is_prefix(*argv, "value")) {
332 		int fd;
333 
334 		if (!value) {
335 			if (value_size)
336 				p_err("duplicate value");
337 			else
338 				p_err("unnecessary value");
339 			return -1;
340 		}
341 
342 		argv++;
343 
344 		if (map_is_map_of_maps(info->type)) {
345 			int argc = 2;
346 
347 			if (value_size != 4) {
348 				p_err("value smaller than 4B for map in map?");
349 				return -1;
350 			}
351 			if (!argv[0] || !argv[1]) {
352 				p_err("not enough value arguments for map in map");
353 				return -1;
354 			}
355 
356 			fd = map_parse_fd(&argc, &argv);
357 			if (fd < 0)
358 				return -1;
359 
360 			*value_fd = value;
361 			**value_fd = fd;
362 		} else if (map_is_map_of_progs(info->type)) {
363 			int argc = 2;
364 
365 			if (value_size != 4) {
366 				p_err("value smaller than 4B for map of progs?");
367 				return -1;
368 			}
369 			if (!argv[0] || !argv[1]) {
370 				p_err("not enough value arguments for map of progs");
371 				return -1;
372 			}
373 
374 			fd = prog_parse_fd(&argc, &argv);
375 			if (fd < 0)
376 				return -1;
377 
378 			*value_fd = value;
379 			**value_fd = fd;
380 		} else {
381 			argv = parse_bytes(argv, "value", value, value_size);
382 			if (!argv)
383 				return -1;
384 		}
385 
386 		return parse_elem(argv, info, key, NULL, key_size, value_size,
387 				  flags, NULL);
388 	} else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
389 		   is_prefix(*argv, "exist")) {
390 		if (!flags) {
391 			p_err("flags specified multiple times: %s", *argv);
392 			return -1;
393 		}
394 
395 		if (is_prefix(*argv, "any"))
396 			*flags = BPF_ANY;
397 		else if (is_prefix(*argv, "noexist"))
398 			*flags = BPF_NOEXIST;
399 		else if (is_prefix(*argv, "exist"))
400 			*flags = BPF_EXIST;
401 
402 		return parse_elem(argv + 1, info, key, value, key_size,
403 				  value_size, NULL, value_fd);
404 	}
405 
406 	p_err("expected key or value, got: %s", *argv);
407 	return -1;
408 }
409 
410 static int show_map_close_json(int fd, struct bpf_map_info *info)
411 {
412 	char *memlock;
413 
414 	memlock = get_fdinfo(fd, "memlock");
415 	close(fd);
416 
417 	jsonw_start_object(json_wtr);
418 
419 	jsonw_uint_field(json_wtr, "id", info->id);
420 	if (info->type < ARRAY_SIZE(map_type_name))
421 		jsonw_string_field(json_wtr, "type",
422 				   map_type_name[info->type]);
423 	else
424 		jsonw_uint_field(json_wtr, "type", info->type);
425 
426 	if (*info->name)
427 		jsonw_string_field(json_wtr, "name", info->name);
428 
429 	jsonw_name(json_wtr, "flags");
430 	jsonw_printf(json_wtr, "%#x", info->map_flags);
431 	jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
432 	jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
433 	jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
434 
435 	if (memlock)
436 		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
437 	free(memlock);
438 
439 	if (!hash_empty(map_table.table)) {
440 		struct pinned_obj *obj;
441 
442 		jsonw_name(json_wtr, "pinned");
443 		jsonw_start_array(json_wtr);
444 		hash_for_each_possible(map_table.table, obj, hash, info->id) {
445 			if (obj->id == info->id)
446 				jsonw_string(json_wtr, obj->path);
447 		}
448 		jsonw_end_array(json_wtr);
449 	}
450 
451 	jsonw_end_object(json_wtr);
452 
453 	return 0;
454 }
455 
456 static int show_map_close_plain(int fd, struct bpf_map_info *info)
457 {
458 	char *memlock;
459 
460 	memlock = get_fdinfo(fd, "memlock");
461 	close(fd);
462 
463 	printf("%u: ", info->id);
464 	if (info->type < ARRAY_SIZE(map_type_name))
465 		printf("%s  ", map_type_name[info->type]);
466 	else
467 		printf("type %u  ", info->type);
468 
469 	if (*info->name)
470 		printf("name %s  ", info->name);
471 
472 	printf("flags 0x%x\n", info->map_flags);
473 	printf("\tkey %uB  value %uB  max_entries %u",
474 	       info->key_size, info->value_size, info->max_entries);
475 
476 	if (memlock)
477 		printf("  memlock %sB", memlock);
478 	free(memlock);
479 
480 	printf("\n");
481 	if (!hash_empty(map_table.table)) {
482 		struct pinned_obj *obj;
483 
484 		hash_for_each_possible(map_table.table, obj, hash, info->id) {
485 			if (obj->id == info->id)
486 				printf("\tpinned %s\n", obj->path);
487 		}
488 	}
489 	return 0;
490 }
491 
492 static int do_show(int argc, char **argv)
493 {
494 	struct bpf_map_info info = {};
495 	__u32 len = sizeof(info);
496 	__u32 id = 0;
497 	int err;
498 	int fd;
499 
500 	if (show_pinned)
501 		build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
502 
503 	if (argc == 2) {
504 		fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
505 		if (fd < 0)
506 			return -1;
507 
508 		if (json_output)
509 			return show_map_close_json(fd, &info);
510 		else
511 			return show_map_close_plain(fd, &info);
512 	}
513 
514 	if (argc)
515 		return BAD_ARG();
516 
517 	if (json_output)
518 		jsonw_start_array(json_wtr);
519 	while (true) {
520 		err = bpf_map_get_next_id(id, &id);
521 		if (err) {
522 			if (errno == ENOENT)
523 				break;
524 			p_err("can't get next map: %s%s", strerror(errno),
525 			      errno == EINVAL ? " -- kernel too old?" : "");
526 			break;
527 		}
528 
529 		fd = bpf_map_get_fd_by_id(id);
530 		if (fd < 0) {
531 			if (errno == ENOENT)
532 				continue;
533 			p_err("can't get map by id (%u): %s",
534 			      id, strerror(errno));
535 			break;
536 		}
537 
538 		err = bpf_obj_get_info_by_fd(fd, &info, &len);
539 		if (err) {
540 			p_err("can't get map info: %s", strerror(errno));
541 			close(fd);
542 			break;
543 		}
544 
545 		if (json_output)
546 			show_map_close_json(fd, &info);
547 		else
548 			show_map_close_plain(fd, &info);
549 	}
550 	if (json_output)
551 		jsonw_end_array(json_wtr);
552 
553 	return errno == ENOENT ? 0 : -1;
554 }
555 
556 static int do_dump(int argc, char **argv)
557 {
558 	void *key, *value, *prev_key;
559 	unsigned int num_elems = 0;
560 	struct bpf_map_info info = {};
561 	__u32 len = sizeof(info);
562 	int err;
563 	int fd;
564 
565 	if (argc != 2)
566 		usage();
567 
568 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
569 	if (fd < 0)
570 		return -1;
571 
572 	if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
573 		p_err("Dumping maps of maps and program maps not supported");
574 		close(fd);
575 		return -1;
576 	}
577 
578 	key = malloc(info.key_size);
579 	value = alloc_value(&info);
580 	if (!key || !value) {
581 		p_err("mem alloc failed");
582 		err = -1;
583 		goto exit_free;
584 	}
585 
586 	prev_key = NULL;
587 	if (json_output)
588 		jsonw_start_array(json_wtr);
589 	while (true) {
590 		err = bpf_map_get_next_key(fd, prev_key, key);
591 		if (err) {
592 			if (errno == ENOENT)
593 				err = 0;
594 			break;
595 		}
596 
597 		if (!bpf_map_lookup_elem(fd, key, value)) {
598 			if (json_output)
599 				print_entry_json(&info, key, value);
600 			else
601 				print_entry_plain(&info, key, value);
602 		} else {
603 			if (json_output) {
604 				jsonw_name(json_wtr, "key");
605 				print_hex_data_json(key, info.key_size);
606 				jsonw_name(json_wtr, "value");
607 				jsonw_start_object(json_wtr);
608 				jsonw_string_field(json_wtr, "error",
609 						   "can't lookup element");
610 				jsonw_end_object(json_wtr);
611 			} else {
612 				p_info("can't lookup element with key: ");
613 				fprint_hex(stderr, key, info.key_size, " ");
614 				fprintf(stderr, "\n");
615 			}
616 		}
617 
618 		prev_key = key;
619 		num_elems++;
620 	}
621 
622 	if (json_output)
623 		jsonw_end_array(json_wtr);
624 	else
625 		printf("Found %u element%s\n", num_elems,
626 		       num_elems != 1 ? "s" : "");
627 
628 exit_free:
629 	free(key);
630 	free(value);
631 	close(fd);
632 
633 	return err;
634 }
635 
636 static int do_update(int argc, char **argv)
637 {
638 	struct bpf_map_info info = {};
639 	__u32 len = sizeof(info);
640 	__u32 *value_fd = NULL;
641 	__u32 flags = BPF_ANY;
642 	void *key, *value;
643 	int fd, err;
644 
645 	if (argc < 2)
646 		usage();
647 
648 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
649 	if (fd < 0)
650 		return -1;
651 
652 	key = malloc(info.key_size);
653 	value = alloc_value(&info);
654 	if (!key || !value) {
655 		p_err("mem alloc failed");
656 		err = -1;
657 		goto exit_free;
658 	}
659 
660 	err = parse_elem(argv, &info, key, value, info.key_size,
661 			 info.value_size, &flags, &value_fd);
662 	if (err)
663 		goto exit_free;
664 
665 	err = bpf_map_update_elem(fd, key, value, flags);
666 	if (err) {
667 		p_err("update failed: %s", strerror(errno));
668 		goto exit_free;
669 	}
670 
671 exit_free:
672 	if (value_fd)
673 		close(*value_fd);
674 	free(key);
675 	free(value);
676 	close(fd);
677 
678 	if (!err && json_output)
679 		jsonw_null(json_wtr);
680 	return err;
681 }
682 
683 static int do_lookup(int argc, char **argv)
684 {
685 	struct bpf_map_info info = {};
686 	__u32 len = sizeof(info);
687 	void *key, *value;
688 	int err;
689 	int fd;
690 
691 	if (argc < 2)
692 		usage();
693 
694 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
695 	if (fd < 0)
696 		return -1;
697 
698 	key = malloc(info.key_size);
699 	value = alloc_value(&info);
700 	if (!key || !value) {
701 		p_err("mem alloc failed");
702 		err = -1;
703 		goto exit_free;
704 	}
705 
706 	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
707 	if (err)
708 		goto exit_free;
709 
710 	err = bpf_map_lookup_elem(fd, key, value);
711 	if (!err) {
712 		if (json_output)
713 			print_entry_json(&info, key, value);
714 		else
715 			print_entry_plain(&info, key, value);
716 	} else if (errno == ENOENT) {
717 		if (json_output) {
718 			jsonw_null(json_wtr);
719 		} else {
720 			printf("key:\n");
721 			fprint_hex(stdout, key, info.key_size, " ");
722 			printf("\n\nNot found\n");
723 		}
724 	} else {
725 		p_err("lookup failed: %s", strerror(errno));
726 	}
727 
728 exit_free:
729 	free(key);
730 	free(value);
731 	close(fd);
732 
733 	return err;
734 }
735 
736 static int do_getnext(int argc, char **argv)
737 {
738 	struct bpf_map_info info = {};
739 	__u32 len = sizeof(info);
740 	void *key, *nextkey;
741 	int err;
742 	int fd;
743 
744 	if (argc < 2)
745 		usage();
746 
747 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
748 	if (fd < 0)
749 		return -1;
750 
751 	key = malloc(info.key_size);
752 	nextkey = malloc(info.key_size);
753 	if (!key || !nextkey) {
754 		p_err("mem alloc failed");
755 		err = -1;
756 		goto exit_free;
757 	}
758 
759 	if (argc) {
760 		err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
761 				 NULL, NULL);
762 		if (err)
763 			goto exit_free;
764 	} else {
765 		free(key);
766 		key = NULL;
767 	}
768 
769 	err = bpf_map_get_next_key(fd, key, nextkey);
770 	if (err) {
771 		p_err("can't get next key: %s", strerror(errno));
772 		goto exit_free;
773 	}
774 
775 	if (json_output) {
776 		jsonw_start_object(json_wtr);
777 		if (key) {
778 			jsonw_name(json_wtr, "key");
779 			print_hex_data_json(key, info.key_size);
780 		} else {
781 			jsonw_null_field(json_wtr, "key");
782 		}
783 		jsonw_name(json_wtr, "next_key");
784 		print_hex_data_json(nextkey, info.key_size);
785 		jsonw_end_object(json_wtr);
786 	} else {
787 		if (key) {
788 			printf("key:\n");
789 			fprint_hex(stdout, key, info.key_size, " ");
790 			printf("\n");
791 		} else {
792 			printf("key: None\n");
793 		}
794 		printf("next key:\n");
795 		fprint_hex(stdout, nextkey, info.key_size, " ");
796 		printf("\n");
797 	}
798 
799 exit_free:
800 	free(nextkey);
801 	free(key);
802 	close(fd);
803 
804 	return err;
805 }
806 
807 static int do_delete(int argc, char **argv)
808 {
809 	struct bpf_map_info info = {};
810 	__u32 len = sizeof(info);
811 	void *key;
812 	int err;
813 	int fd;
814 
815 	if (argc < 2)
816 		usage();
817 
818 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
819 	if (fd < 0)
820 		return -1;
821 
822 	key = malloc(info.key_size);
823 	if (!key) {
824 		p_err("mem alloc failed");
825 		err = -1;
826 		goto exit_free;
827 	}
828 
829 	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
830 	if (err)
831 		goto exit_free;
832 
833 	err = bpf_map_delete_elem(fd, key);
834 	if (err)
835 		p_err("delete failed: %s", strerror(errno));
836 
837 exit_free:
838 	free(key);
839 	close(fd);
840 
841 	if (!err && json_output)
842 		jsonw_null(json_wtr);
843 	return err;
844 }
845 
846 static int do_pin(int argc, char **argv)
847 {
848 	int err;
849 
850 	err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
851 	if (!err && json_output)
852 		jsonw_null(json_wtr);
853 	return err;
854 }
855 
856 static int do_help(int argc, char **argv)
857 {
858 	if (json_output) {
859 		jsonw_null(json_wtr);
860 		return 0;
861 	}
862 
863 	fprintf(stderr,
864 		"Usage: %s %s show   [MAP]\n"
865 		"       %s %s dump    MAP\n"
866 		"       %s %s update  MAP  key BYTES value VALUE [UPDATE_FLAGS]\n"
867 		"       %s %s lookup  MAP  key BYTES\n"
868 		"       %s %s getnext MAP [key BYTES]\n"
869 		"       %s %s delete  MAP  key BYTES\n"
870 		"       %s %s pin     MAP  FILE\n"
871 		"       %s %s help\n"
872 		"\n"
873 		"       MAP := { id MAP_ID | pinned FILE }\n"
874 		"       " HELP_SPEC_PROGRAM "\n"
875 		"       VALUE := { BYTES | MAP | PROG }\n"
876 		"       UPDATE_FLAGS := { any | exist | noexist }\n"
877 		"       " HELP_SPEC_OPTIONS "\n"
878 		"",
879 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
880 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
881 		bin_name, argv[-2], bin_name, argv[-2]);
882 
883 	return 0;
884 }
885 
886 static const struct cmd cmds[] = {
887 	{ "show",	do_show },
888 	{ "help",	do_help },
889 	{ "dump",	do_dump },
890 	{ "update",	do_update },
891 	{ "lookup",	do_lookup },
892 	{ "getnext",	do_getnext },
893 	{ "delete",	do_delete },
894 	{ "pin",	do_pin },
895 	{ 0 }
896 };
897 
898 int do_map(int argc, char **argv)
899 {
900 	return cmd_select(cmds, argc, argv, do_help);
901 }
902