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