xref: /openbmc/linux/tools/bpf/bpftool/map.c (revision 2eb3ed33e55d003d721d4d1a5e72fe323c12b4c0)
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 	jsonw_end_object(json_wtr);
440 
441 	return 0;
442 }
443 
444 static int show_map_close_plain(int fd, struct bpf_map_info *info)
445 {
446 	char *memlock;
447 
448 	memlock = get_fdinfo(fd, "memlock");
449 	close(fd);
450 
451 	printf("%u: ", info->id);
452 	if (info->type < ARRAY_SIZE(map_type_name))
453 		printf("%s  ", map_type_name[info->type]);
454 	else
455 		printf("type %u  ", info->type);
456 
457 	if (*info->name)
458 		printf("name %s  ", info->name);
459 
460 	printf("flags 0x%x\n", info->map_flags);
461 	printf("\tkey %uB  value %uB  max_entries %u",
462 	       info->key_size, info->value_size, info->max_entries);
463 
464 	if (memlock)
465 		printf("  memlock %sB", memlock);
466 	free(memlock);
467 
468 	printf("\n");
469 
470 	return 0;
471 }
472 
473 static int do_show(int argc, char **argv)
474 {
475 	struct bpf_map_info info = {};
476 	__u32 len = sizeof(info);
477 	__u32 id = 0;
478 	int err;
479 	int fd;
480 
481 	if (argc == 2) {
482 		fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
483 		if (fd < 0)
484 			return -1;
485 
486 		if (json_output)
487 			return show_map_close_json(fd, &info);
488 		else
489 			return show_map_close_plain(fd, &info);
490 	}
491 
492 	if (argc)
493 		return BAD_ARG();
494 
495 	if (json_output)
496 		jsonw_start_array(json_wtr);
497 	while (true) {
498 		err = bpf_map_get_next_id(id, &id);
499 		if (err) {
500 			if (errno == ENOENT)
501 				break;
502 			p_err("can't get next map: %s%s", strerror(errno),
503 			      errno == EINVAL ? " -- kernel too old?" : "");
504 			return -1;
505 		}
506 
507 		fd = bpf_map_get_fd_by_id(id);
508 		if (fd < 0) {
509 			p_err("can't get map by id (%u): %s",
510 			      id, strerror(errno));
511 			return -1;
512 		}
513 
514 		err = bpf_obj_get_info_by_fd(fd, &info, &len);
515 		if (err) {
516 			p_err("can't get map info: %s", strerror(errno));
517 			close(fd);
518 			return -1;
519 		}
520 
521 		if (json_output)
522 			show_map_close_json(fd, &info);
523 		else
524 			show_map_close_plain(fd, &info);
525 	}
526 	if (json_output)
527 		jsonw_end_array(json_wtr);
528 
529 	return errno == ENOENT ? 0 : -1;
530 }
531 
532 static int do_dump(int argc, char **argv)
533 {
534 	void *key, *value, *prev_key;
535 	unsigned int num_elems = 0;
536 	struct bpf_map_info info = {};
537 	__u32 len = sizeof(info);
538 	int err;
539 	int fd;
540 
541 	if (argc != 2)
542 		usage();
543 
544 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
545 	if (fd < 0)
546 		return -1;
547 
548 	if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
549 		p_err("Dumping maps of maps and program maps not supported");
550 		close(fd);
551 		return -1;
552 	}
553 
554 	key = malloc(info.key_size);
555 	value = alloc_value(&info);
556 	if (!key || !value) {
557 		p_err("mem alloc failed");
558 		err = -1;
559 		goto exit_free;
560 	}
561 
562 	prev_key = NULL;
563 	if (json_output)
564 		jsonw_start_array(json_wtr);
565 	while (true) {
566 		err = bpf_map_get_next_key(fd, prev_key, key);
567 		if (err) {
568 			if (errno == ENOENT)
569 				err = 0;
570 			break;
571 		}
572 
573 		if (!bpf_map_lookup_elem(fd, key, value)) {
574 			if (json_output)
575 				print_entry_json(&info, key, value);
576 			else
577 				print_entry_plain(&info, key, value);
578 		} else {
579 			if (json_output) {
580 				jsonw_name(json_wtr, "key");
581 				print_hex_data_json(key, info.key_size);
582 				jsonw_name(json_wtr, "value");
583 				jsonw_start_object(json_wtr);
584 				jsonw_string_field(json_wtr, "error",
585 						   "can't lookup element");
586 				jsonw_end_object(json_wtr);
587 			} else {
588 				p_info("can't lookup element with key: ");
589 				fprint_hex(stderr, key, info.key_size, " ");
590 				fprintf(stderr, "\n");
591 			}
592 		}
593 
594 		prev_key = key;
595 		num_elems++;
596 	}
597 
598 	if (json_output)
599 		jsonw_end_array(json_wtr);
600 	else
601 		printf("Found %u element%s\n", num_elems,
602 		       num_elems != 1 ? "s" : "");
603 
604 exit_free:
605 	free(key);
606 	free(value);
607 	close(fd);
608 
609 	return err;
610 }
611 
612 static int do_update(int argc, char **argv)
613 {
614 	struct bpf_map_info info = {};
615 	__u32 len = sizeof(info);
616 	__u32 *value_fd = NULL;
617 	__u32 flags = BPF_ANY;
618 	void *key, *value;
619 	int fd, err;
620 
621 	if (argc < 2)
622 		usage();
623 
624 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
625 	if (fd < 0)
626 		return -1;
627 
628 	key = malloc(info.key_size);
629 	value = alloc_value(&info);
630 	if (!key || !value) {
631 		p_err("mem alloc failed");
632 		err = -1;
633 		goto exit_free;
634 	}
635 
636 	err = parse_elem(argv, &info, key, value, info.key_size,
637 			 info.value_size, &flags, &value_fd);
638 	if (err)
639 		goto exit_free;
640 
641 	err = bpf_map_update_elem(fd, key, value, flags);
642 	if (err) {
643 		p_err("update failed: %s", strerror(errno));
644 		goto exit_free;
645 	}
646 
647 exit_free:
648 	if (value_fd)
649 		close(*value_fd);
650 	free(key);
651 	free(value);
652 	close(fd);
653 
654 	if (!err && json_output)
655 		jsonw_null(json_wtr);
656 	return err;
657 }
658 
659 static int do_lookup(int argc, char **argv)
660 {
661 	struct bpf_map_info info = {};
662 	__u32 len = sizeof(info);
663 	void *key, *value;
664 	int err;
665 	int fd;
666 
667 	if (argc < 2)
668 		usage();
669 
670 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
671 	if (fd < 0)
672 		return -1;
673 
674 	key = malloc(info.key_size);
675 	value = alloc_value(&info);
676 	if (!key || !value) {
677 		p_err("mem alloc failed");
678 		err = -1;
679 		goto exit_free;
680 	}
681 
682 	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
683 	if (err)
684 		goto exit_free;
685 
686 	err = bpf_map_lookup_elem(fd, key, value);
687 	if (!err) {
688 		if (json_output)
689 			print_entry_json(&info, key, value);
690 		else
691 			print_entry_plain(&info, key, value);
692 	} else if (errno == ENOENT) {
693 		if (json_output) {
694 			jsonw_null(json_wtr);
695 		} else {
696 			printf("key:\n");
697 			fprint_hex(stdout, key, info.key_size, " ");
698 			printf("\n\nNot found\n");
699 		}
700 	} else {
701 		p_err("lookup failed: %s", strerror(errno));
702 	}
703 
704 exit_free:
705 	free(key);
706 	free(value);
707 	close(fd);
708 
709 	return err;
710 }
711 
712 static int do_getnext(int argc, char **argv)
713 {
714 	struct bpf_map_info info = {};
715 	__u32 len = sizeof(info);
716 	void *key, *nextkey;
717 	int err;
718 	int fd;
719 
720 	if (argc < 2)
721 		usage();
722 
723 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
724 	if (fd < 0)
725 		return -1;
726 
727 	key = malloc(info.key_size);
728 	nextkey = malloc(info.key_size);
729 	if (!key || !nextkey) {
730 		p_err("mem alloc failed");
731 		err = -1;
732 		goto exit_free;
733 	}
734 
735 	if (argc) {
736 		err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
737 				 NULL, NULL);
738 		if (err)
739 			goto exit_free;
740 	} else {
741 		free(key);
742 		key = NULL;
743 	}
744 
745 	err = bpf_map_get_next_key(fd, key, nextkey);
746 	if (err) {
747 		p_err("can't get next key: %s", strerror(errno));
748 		goto exit_free;
749 	}
750 
751 	if (json_output) {
752 		jsonw_start_object(json_wtr);
753 		if (key) {
754 			jsonw_name(json_wtr, "key");
755 			print_hex_data_json(key, info.key_size);
756 		} else {
757 			jsonw_null_field(json_wtr, "key");
758 		}
759 		jsonw_name(json_wtr, "next_key");
760 		print_hex_data_json(nextkey, info.key_size);
761 		jsonw_end_object(json_wtr);
762 	} else {
763 		if (key) {
764 			printf("key:\n");
765 			fprint_hex(stdout, key, info.key_size, " ");
766 			printf("\n");
767 		} else {
768 			printf("key: None\n");
769 		}
770 		printf("next key:\n");
771 		fprint_hex(stdout, nextkey, info.key_size, " ");
772 		printf("\n");
773 	}
774 
775 exit_free:
776 	free(nextkey);
777 	free(key);
778 	close(fd);
779 
780 	return err;
781 }
782 
783 static int do_delete(int argc, char **argv)
784 {
785 	struct bpf_map_info info = {};
786 	__u32 len = sizeof(info);
787 	void *key;
788 	int err;
789 	int fd;
790 
791 	if (argc < 2)
792 		usage();
793 
794 	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
795 	if (fd < 0)
796 		return -1;
797 
798 	key = malloc(info.key_size);
799 	if (!key) {
800 		p_err("mem alloc failed");
801 		err = -1;
802 		goto exit_free;
803 	}
804 
805 	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
806 	if (err)
807 		goto exit_free;
808 
809 	err = bpf_map_delete_elem(fd, key);
810 	if (err)
811 		p_err("delete failed: %s", strerror(errno));
812 
813 exit_free:
814 	free(key);
815 	close(fd);
816 
817 	if (!err && json_output)
818 		jsonw_null(json_wtr);
819 	return err;
820 }
821 
822 static int do_pin(int argc, char **argv)
823 {
824 	int err;
825 
826 	err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
827 	if (!err && json_output)
828 		jsonw_null(json_wtr);
829 	return err;
830 }
831 
832 static int do_help(int argc, char **argv)
833 {
834 	if (json_output) {
835 		jsonw_null(json_wtr);
836 		return 0;
837 	}
838 
839 	fprintf(stderr,
840 		"Usage: %s %s show   [MAP]\n"
841 		"       %s %s dump    MAP\n"
842 		"       %s %s update  MAP  key BYTES value VALUE [UPDATE_FLAGS]\n"
843 		"       %s %s lookup  MAP  key BYTES\n"
844 		"       %s %s getnext MAP [key BYTES]\n"
845 		"       %s %s delete  MAP  key BYTES\n"
846 		"       %s %s pin     MAP  FILE\n"
847 		"       %s %s help\n"
848 		"\n"
849 		"       MAP := { id MAP_ID | pinned FILE }\n"
850 		"       " HELP_SPEC_PROGRAM "\n"
851 		"       VALUE := { BYTES | MAP | PROG }\n"
852 		"       UPDATE_FLAGS := { any | exist | noexist }\n"
853 		"       " HELP_SPEC_OPTIONS "\n"
854 		"",
855 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
856 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
857 		bin_name, argv[-2], bin_name, argv[-2]);
858 
859 	return 0;
860 }
861 
862 static const struct cmd cmds[] = {
863 	{ "show",	do_show },
864 	{ "help",	do_help },
865 	{ "dump",	do_dump },
866 	{ "update",	do_update },
867 	{ "lookup",	do_lookup },
868 	{ "getnext",	do_getnext },
869 	{ "delete",	do_delete },
870 	{ "pin",	do_pin },
871 	{ 0 }
872 };
873 
874 int do_map(int argc, char **argv)
875 {
876 	return cmd_select(cmds, argc, argv, do_help);
877 }
878