xref: /linux/tools/bpf/bpftool/prog.c (revision c034a177d3c898f370f52877e7252da8c4f8235c)
1 /*
2  * Copyright (C) 2017-2018 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 #define _GNU_SOURCE
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include <net/if.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 
47 #include <linux/err.h>
48 
49 #include <bpf.h>
50 #include <libbpf.h>
51 
52 #include "cfg.h"
53 #include "main.h"
54 #include "xlated_dumper.h"
55 
56 static const char * const prog_type_name[] = {
57 	[BPF_PROG_TYPE_UNSPEC]		= "unspec",
58 	[BPF_PROG_TYPE_SOCKET_FILTER]	= "socket_filter",
59 	[BPF_PROG_TYPE_KPROBE]		= "kprobe",
60 	[BPF_PROG_TYPE_SCHED_CLS]	= "sched_cls",
61 	[BPF_PROG_TYPE_SCHED_ACT]	= "sched_act",
62 	[BPF_PROG_TYPE_TRACEPOINT]	= "tracepoint",
63 	[BPF_PROG_TYPE_XDP]		= "xdp",
64 	[BPF_PROG_TYPE_PERF_EVENT]	= "perf_event",
65 	[BPF_PROG_TYPE_CGROUP_SKB]	= "cgroup_skb",
66 	[BPF_PROG_TYPE_CGROUP_SOCK]	= "cgroup_sock",
67 	[BPF_PROG_TYPE_LWT_IN]		= "lwt_in",
68 	[BPF_PROG_TYPE_LWT_OUT]		= "lwt_out",
69 	[BPF_PROG_TYPE_LWT_XMIT]	= "lwt_xmit",
70 	[BPF_PROG_TYPE_SOCK_OPS]	= "sock_ops",
71 	[BPF_PROG_TYPE_SK_SKB]		= "sk_skb",
72 	[BPF_PROG_TYPE_CGROUP_DEVICE]	= "cgroup_device",
73 	[BPF_PROG_TYPE_SK_MSG]		= "sk_msg",
74 	[BPF_PROG_TYPE_RAW_TRACEPOINT]	= "raw_tracepoint",
75 	[BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
76 	[BPF_PROG_TYPE_LIRC_MODE2]	= "lirc_mode2",
77 	[BPF_PROG_TYPE_FLOW_DISSECTOR]	= "flow_dissector",
78 };
79 
80 static const char * const attach_type_strings[] = {
81 	[BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
82 	[BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
83 	[BPF_SK_MSG_VERDICT] = "msg_verdict",
84 	[__MAX_BPF_ATTACH_TYPE] = NULL,
85 };
86 
87 enum bpf_attach_type parse_attach_type(const char *str)
88 {
89 	enum bpf_attach_type type;
90 
91 	for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
92 		if (attach_type_strings[type] &&
93 		    is_prefix(str, attach_type_strings[type]))
94 			return type;
95 	}
96 
97 	return __MAX_BPF_ATTACH_TYPE;
98 }
99 
100 static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
101 {
102 	struct timespec real_time_ts, boot_time_ts;
103 	time_t wallclock_secs;
104 	struct tm load_tm;
105 
106 	buf[--size] = '\0';
107 
108 	if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
109 	    clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
110 		perror("Can't read clocks");
111 		snprintf(buf, size, "%llu", nsecs / 1000000000);
112 		return;
113 	}
114 
115 	wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
116 		(real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
117 		1000000000;
118 
119 
120 	if (!localtime_r(&wallclock_secs, &load_tm)) {
121 		snprintf(buf, size, "%llu", nsecs / 1000000000);
122 		return;
123 	}
124 
125 	if (json_output)
126 		strftime(buf, size, "%s", &load_tm);
127 	else
128 		strftime(buf, size, "%FT%T%z", &load_tm);
129 }
130 
131 static int prog_fd_by_tag(unsigned char *tag)
132 {
133 	struct bpf_prog_info info = {};
134 	__u32 len = sizeof(info);
135 	unsigned int id = 0;
136 	int err;
137 	int fd;
138 
139 	while (true) {
140 		err = bpf_prog_get_next_id(id, &id);
141 		if (err) {
142 			p_err("%s", strerror(errno));
143 			return -1;
144 		}
145 
146 		fd = bpf_prog_get_fd_by_id(id);
147 		if (fd < 0) {
148 			p_err("can't get prog by id (%u): %s",
149 			      id, strerror(errno));
150 			return -1;
151 		}
152 
153 		err = bpf_obj_get_info_by_fd(fd, &info, &len);
154 		if (err) {
155 			p_err("can't get prog info (%u): %s",
156 			      id, strerror(errno));
157 			close(fd);
158 			return -1;
159 		}
160 
161 		if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
162 			return fd;
163 
164 		close(fd);
165 	}
166 }
167 
168 int prog_parse_fd(int *argc, char ***argv)
169 {
170 	int fd;
171 
172 	if (is_prefix(**argv, "id")) {
173 		unsigned int id;
174 		char *endptr;
175 
176 		NEXT_ARGP();
177 
178 		id = strtoul(**argv, &endptr, 0);
179 		if (*endptr) {
180 			p_err("can't parse %s as ID", **argv);
181 			return -1;
182 		}
183 		NEXT_ARGP();
184 
185 		fd = bpf_prog_get_fd_by_id(id);
186 		if (fd < 0)
187 			p_err("get by id (%u): %s", id, strerror(errno));
188 		return fd;
189 	} else if (is_prefix(**argv, "tag")) {
190 		unsigned char tag[BPF_TAG_SIZE];
191 
192 		NEXT_ARGP();
193 
194 		if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
195 			   tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
196 		    != BPF_TAG_SIZE) {
197 			p_err("can't parse tag");
198 			return -1;
199 		}
200 		NEXT_ARGP();
201 
202 		return prog_fd_by_tag(tag);
203 	} else if (is_prefix(**argv, "pinned")) {
204 		char *path;
205 
206 		NEXT_ARGP();
207 
208 		path = **argv;
209 		NEXT_ARGP();
210 
211 		return open_obj_pinned_any(path, BPF_OBJ_PROG);
212 	}
213 
214 	p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
215 	return -1;
216 }
217 
218 static void show_prog_maps(int fd, u32 num_maps)
219 {
220 	struct bpf_prog_info info = {};
221 	__u32 len = sizeof(info);
222 	__u32 map_ids[num_maps];
223 	unsigned int i;
224 	int err;
225 
226 	info.nr_map_ids = num_maps;
227 	info.map_ids = ptr_to_u64(map_ids);
228 
229 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
230 	if (err || !info.nr_map_ids)
231 		return;
232 
233 	if (json_output) {
234 		jsonw_name(json_wtr, "map_ids");
235 		jsonw_start_array(json_wtr);
236 		for (i = 0; i < info.nr_map_ids; i++)
237 			jsonw_uint(json_wtr, map_ids[i]);
238 		jsonw_end_array(json_wtr);
239 	} else {
240 		printf("  map_ids ");
241 		for (i = 0; i < info.nr_map_ids; i++)
242 			printf("%u%s", map_ids[i],
243 			       i == info.nr_map_ids - 1 ? "" : ",");
244 	}
245 }
246 
247 static void print_prog_json(struct bpf_prog_info *info, int fd)
248 {
249 	char *memlock;
250 
251 	jsonw_start_object(json_wtr);
252 	jsonw_uint_field(json_wtr, "id", info->id);
253 	if (info->type < ARRAY_SIZE(prog_type_name))
254 		jsonw_string_field(json_wtr, "type",
255 				   prog_type_name[info->type]);
256 	else
257 		jsonw_uint_field(json_wtr, "type", info->type);
258 
259 	if (*info->name)
260 		jsonw_string_field(json_wtr, "name", info->name);
261 
262 	jsonw_name(json_wtr, "tag");
263 	jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
264 		     info->tag[0], info->tag[1], info->tag[2], info->tag[3],
265 		     info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
266 
267 	jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
268 
269 	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
270 
271 	if (info->load_time) {
272 		char buf[32];
273 
274 		print_boot_time(info->load_time, buf, sizeof(buf));
275 
276 		/* Piggy back on load_time, since 0 uid is a valid one */
277 		jsonw_name(json_wtr, "loaded_at");
278 		jsonw_printf(json_wtr, "%s", buf);
279 		jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
280 	}
281 
282 	jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
283 
284 	if (info->jited_prog_len) {
285 		jsonw_bool_field(json_wtr, "jited", true);
286 		jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
287 	} else {
288 		jsonw_bool_field(json_wtr, "jited", false);
289 	}
290 
291 	memlock = get_fdinfo(fd, "memlock");
292 	if (memlock)
293 		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
294 	free(memlock);
295 
296 	if (info->nr_map_ids)
297 		show_prog_maps(fd, info->nr_map_ids);
298 
299 	if (!hash_empty(prog_table.table)) {
300 		struct pinned_obj *obj;
301 
302 		jsonw_name(json_wtr, "pinned");
303 		jsonw_start_array(json_wtr);
304 		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
305 			if (obj->id == info->id)
306 				jsonw_string(json_wtr, obj->path);
307 		}
308 		jsonw_end_array(json_wtr);
309 	}
310 
311 	jsonw_end_object(json_wtr);
312 }
313 
314 static void print_prog_plain(struct bpf_prog_info *info, int fd)
315 {
316 	char *memlock;
317 
318 	printf("%u: ", info->id);
319 	if (info->type < ARRAY_SIZE(prog_type_name))
320 		printf("%s  ", prog_type_name[info->type]);
321 	else
322 		printf("type %u  ", info->type);
323 
324 	if (*info->name)
325 		printf("name %s  ", info->name);
326 
327 	printf("tag ");
328 	fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
329 	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
330 	printf("%s", info->gpl_compatible ? "  gpl" : "");
331 	printf("\n");
332 
333 	if (info->load_time) {
334 		char buf[32];
335 
336 		print_boot_time(info->load_time, buf, sizeof(buf));
337 
338 		/* Piggy back on load_time, since 0 uid is a valid one */
339 		printf("\tloaded_at %s  uid %u\n", buf, info->created_by_uid);
340 	}
341 
342 	printf("\txlated %uB", info->xlated_prog_len);
343 
344 	if (info->jited_prog_len)
345 		printf("  jited %uB", info->jited_prog_len);
346 	else
347 		printf("  not jited");
348 
349 	memlock = get_fdinfo(fd, "memlock");
350 	if (memlock)
351 		printf("  memlock %sB", memlock);
352 	free(memlock);
353 
354 	if (info->nr_map_ids)
355 		show_prog_maps(fd, info->nr_map_ids);
356 
357 	if (!hash_empty(prog_table.table)) {
358 		struct pinned_obj *obj;
359 
360 		printf("\n");
361 		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
362 			if (obj->id == info->id)
363 				printf("\tpinned %s\n", obj->path);
364 		}
365 	}
366 
367 	printf("\n");
368 }
369 
370 static int show_prog(int fd)
371 {
372 	struct bpf_prog_info info = {};
373 	__u32 len = sizeof(info);
374 	int err;
375 
376 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
377 	if (err) {
378 		p_err("can't get prog info: %s", strerror(errno));
379 		return -1;
380 	}
381 
382 	if (json_output)
383 		print_prog_json(&info, fd);
384 	else
385 		print_prog_plain(&info, fd);
386 
387 	return 0;
388 }
389 
390 static int do_show(int argc, char **argv)
391 {
392 	__u32 id = 0;
393 	int err;
394 	int fd;
395 
396 	if (show_pinned)
397 		build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
398 
399 	if (argc == 2) {
400 		fd = prog_parse_fd(&argc, &argv);
401 		if (fd < 0)
402 			return -1;
403 
404 		return show_prog(fd);
405 	}
406 
407 	if (argc)
408 		return BAD_ARG();
409 
410 	if (json_output)
411 		jsonw_start_array(json_wtr);
412 	while (true) {
413 		err = bpf_prog_get_next_id(id, &id);
414 		if (err) {
415 			if (errno == ENOENT) {
416 				err = 0;
417 				break;
418 			}
419 			p_err("can't get next program: %s%s", strerror(errno),
420 			      errno == EINVAL ? " -- kernel too old?" : "");
421 			err = -1;
422 			break;
423 		}
424 
425 		fd = bpf_prog_get_fd_by_id(id);
426 		if (fd < 0) {
427 			if (errno == ENOENT)
428 				continue;
429 			p_err("can't get prog by id (%u): %s",
430 			      id, strerror(errno));
431 			err = -1;
432 			break;
433 		}
434 
435 		err = show_prog(fd);
436 		close(fd);
437 		if (err)
438 			break;
439 	}
440 
441 	if (json_output)
442 		jsonw_end_array(json_wtr);
443 
444 	return err;
445 }
446 
447 static int do_dump(int argc, char **argv)
448 {
449 	unsigned long *func_ksyms = NULL;
450 	struct bpf_prog_info info = {};
451 	unsigned int *func_lens = NULL;
452 	unsigned int nr_func_ksyms;
453 	unsigned int nr_func_lens;
454 	struct dump_data dd = {};
455 	__u32 len = sizeof(info);
456 	unsigned int buf_size;
457 	char *filepath = NULL;
458 	bool opcodes = false;
459 	bool visual = false;
460 	unsigned char *buf;
461 	__u32 *member_len;
462 	__u64 *member_ptr;
463 	ssize_t n;
464 	int err;
465 	int fd;
466 
467 	if (is_prefix(*argv, "jited")) {
468 		member_len = &info.jited_prog_len;
469 		member_ptr = &info.jited_prog_insns;
470 	} else if (is_prefix(*argv, "xlated")) {
471 		member_len = &info.xlated_prog_len;
472 		member_ptr = &info.xlated_prog_insns;
473 	} else {
474 		p_err("expected 'xlated' or 'jited', got: %s", *argv);
475 		return -1;
476 	}
477 	NEXT_ARG();
478 
479 	if (argc < 2)
480 		usage();
481 
482 	fd = prog_parse_fd(&argc, &argv);
483 	if (fd < 0)
484 		return -1;
485 
486 	if (is_prefix(*argv, "file")) {
487 		NEXT_ARG();
488 		if (!argc) {
489 			p_err("expected file path");
490 			return -1;
491 		}
492 
493 		filepath = *argv;
494 		NEXT_ARG();
495 	} else if (is_prefix(*argv, "opcodes")) {
496 		opcodes = true;
497 		NEXT_ARG();
498 	} else if (is_prefix(*argv, "visual")) {
499 		visual = true;
500 		NEXT_ARG();
501 	}
502 
503 	if (argc) {
504 		usage();
505 		return -1;
506 	}
507 
508 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
509 	if (err) {
510 		p_err("can't get prog info: %s", strerror(errno));
511 		return -1;
512 	}
513 
514 	if (!*member_len) {
515 		p_info("no instructions returned");
516 		close(fd);
517 		return 0;
518 	}
519 
520 	buf_size = *member_len;
521 
522 	buf = malloc(buf_size);
523 	if (!buf) {
524 		p_err("mem alloc failed");
525 		close(fd);
526 		return -1;
527 	}
528 
529 	nr_func_ksyms = info.nr_jited_ksyms;
530 	if (nr_func_ksyms) {
531 		func_ksyms = malloc(nr_func_ksyms * sizeof(__u64));
532 		if (!func_ksyms) {
533 			p_err("mem alloc failed");
534 			close(fd);
535 			goto err_free;
536 		}
537 	}
538 
539 	nr_func_lens = info.nr_jited_func_lens;
540 	if (nr_func_lens) {
541 		func_lens = malloc(nr_func_lens * sizeof(__u32));
542 		if (!func_lens) {
543 			p_err("mem alloc failed");
544 			close(fd);
545 			goto err_free;
546 		}
547 	}
548 
549 	memset(&info, 0, sizeof(info));
550 
551 	*member_ptr = ptr_to_u64(buf);
552 	*member_len = buf_size;
553 	info.jited_ksyms = ptr_to_u64(func_ksyms);
554 	info.nr_jited_ksyms = nr_func_ksyms;
555 	info.jited_func_lens = ptr_to_u64(func_lens);
556 	info.nr_jited_func_lens = nr_func_lens;
557 
558 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
559 	close(fd);
560 	if (err) {
561 		p_err("can't get prog info: %s", strerror(errno));
562 		goto err_free;
563 	}
564 
565 	if (*member_len > buf_size) {
566 		p_err("too many instructions returned");
567 		goto err_free;
568 	}
569 
570 	if (info.nr_jited_ksyms > nr_func_ksyms) {
571 		p_err("too many addresses returned");
572 		goto err_free;
573 	}
574 
575 	if (info.nr_jited_func_lens > nr_func_lens) {
576 		p_err("too many values returned");
577 		goto err_free;
578 	}
579 
580 	if ((member_len == &info.jited_prog_len &&
581 	     info.jited_prog_insns == 0) ||
582 	    (member_len == &info.xlated_prog_len &&
583 	     info.xlated_prog_insns == 0)) {
584 		p_err("error retrieving insn dump: kernel.kptr_restrict set?");
585 		goto err_free;
586 	}
587 
588 	if (filepath) {
589 		fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
590 		if (fd < 0) {
591 			p_err("can't open file %s: %s", filepath,
592 			      strerror(errno));
593 			goto err_free;
594 		}
595 
596 		n = write(fd, buf, *member_len);
597 		close(fd);
598 		if (n != *member_len) {
599 			p_err("error writing output file: %s",
600 			      n < 0 ? strerror(errno) : "short write");
601 			goto err_free;
602 		}
603 
604 		if (json_output)
605 			jsonw_null(json_wtr);
606 	} else if (member_len == &info.jited_prog_len) {
607 		const char *name = NULL;
608 
609 		if (info.ifindex) {
610 			name = ifindex_to_bfd_name_ns(info.ifindex,
611 						      info.netns_dev,
612 						      info.netns_ino);
613 			if (!name)
614 				goto err_free;
615 		}
616 
617 		if (info.nr_jited_func_lens && info.jited_func_lens) {
618 			struct kernel_sym *sym = NULL;
619 			char sym_name[SYM_MAX_NAME];
620 			unsigned char *img = buf;
621 			__u64 *ksyms = NULL;
622 			__u32 *lens;
623 			__u32 i;
624 
625 			if (info.nr_jited_ksyms) {
626 				kernel_syms_load(&dd);
627 				ksyms = (__u64 *) info.jited_ksyms;
628 			}
629 
630 			if (json_output)
631 				jsonw_start_array(json_wtr);
632 
633 			lens = (__u32 *) info.jited_func_lens;
634 			for (i = 0; i < info.nr_jited_func_lens; i++) {
635 				if (ksyms) {
636 					sym = kernel_syms_search(&dd, ksyms[i]);
637 					if (sym)
638 						sprintf(sym_name, "%s", sym->name);
639 					else
640 						sprintf(sym_name, "0x%016llx", ksyms[i]);
641 				} else {
642 					strcpy(sym_name, "unknown");
643 				}
644 
645 				if (json_output) {
646 					jsonw_start_object(json_wtr);
647 					jsonw_name(json_wtr, "name");
648 					jsonw_string(json_wtr, sym_name);
649 					jsonw_name(json_wtr, "insns");
650 				} else {
651 					printf("%s:\n", sym_name);
652 				}
653 
654 				disasm_print_insn(img, lens[i], opcodes, name);
655 				img += lens[i];
656 
657 				if (json_output)
658 					jsonw_end_object(json_wtr);
659 				else
660 					printf("\n");
661 			}
662 
663 			if (json_output)
664 				jsonw_end_array(json_wtr);
665 		} else {
666 			disasm_print_insn(buf, *member_len, opcodes, name);
667 		}
668 	} else if (visual) {
669 		if (json_output)
670 			jsonw_null(json_wtr);
671 		else
672 			dump_xlated_cfg(buf, *member_len);
673 	} else {
674 		kernel_syms_load(&dd);
675 		dd.nr_jited_ksyms = info.nr_jited_ksyms;
676 		dd.jited_ksyms = (__u64 *) info.jited_ksyms;
677 
678 		if (json_output)
679 			dump_xlated_json(&dd, buf, *member_len, opcodes);
680 		else
681 			dump_xlated_plain(&dd, buf, *member_len, opcodes);
682 		kernel_syms_destroy(&dd);
683 	}
684 
685 	free(buf);
686 	free(func_ksyms);
687 	free(func_lens);
688 	return 0;
689 
690 err_free:
691 	free(buf);
692 	free(func_ksyms);
693 	free(func_lens);
694 	return -1;
695 }
696 
697 static int do_pin(int argc, char **argv)
698 {
699 	int err;
700 
701 	err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
702 	if (!err && json_output)
703 		jsonw_null(json_wtr);
704 	return err;
705 }
706 
707 struct map_replace {
708 	int idx;
709 	int fd;
710 	char *name;
711 };
712 
713 int map_replace_compar(const void *p1, const void *p2)
714 {
715 	const struct map_replace *a = p1, *b = p2;
716 
717 	return a->idx - b->idx;
718 }
719 
720 static int do_attach(int argc, char **argv)
721 {
722 	enum bpf_attach_type attach_type;
723 	int err, mapfd, progfd;
724 
725 	if (!REQ_ARGS(5)) {
726 		p_err("too few parameters for map attach");
727 		return -EINVAL;
728 	}
729 
730 	progfd = prog_parse_fd(&argc, &argv);
731 	if (progfd < 0)
732 		return progfd;
733 
734 	attach_type = parse_attach_type(*argv);
735 	if (attach_type == __MAX_BPF_ATTACH_TYPE) {
736 		p_err("invalid attach type");
737 		return -EINVAL;
738 	}
739 	NEXT_ARG();
740 
741 	mapfd = map_parse_fd(&argc, &argv);
742 	if (mapfd < 0)
743 		return mapfd;
744 
745 	err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
746 	if (err) {
747 		p_err("failed prog attach to map");
748 		return -EINVAL;
749 	}
750 
751 	if (json_output)
752 		jsonw_null(json_wtr);
753 	return 0;
754 }
755 
756 static int do_detach(int argc, char **argv)
757 {
758 	enum bpf_attach_type attach_type;
759 	int err, mapfd, progfd;
760 
761 	if (!REQ_ARGS(5)) {
762 		p_err("too few parameters for map detach");
763 		return -EINVAL;
764 	}
765 
766 	progfd = prog_parse_fd(&argc, &argv);
767 	if (progfd < 0)
768 		return progfd;
769 
770 	attach_type = parse_attach_type(*argv);
771 	if (attach_type == __MAX_BPF_ATTACH_TYPE) {
772 		p_err("invalid attach type");
773 		return -EINVAL;
774 	}
775 	NEXT_ARG();
776 
777 	mapfd = map_parse_fd(&argc, &argv);
778 	if (mapfd < 0)
779 		return mapfd;
780 
781 	err = bpf_prog_detach2(progfd, mapfd, attach_type);
782 	if (err) {
783 		p_err("failed prog detach from map");
784 		return -EINVAL;
785 	}
786 
787 	if (json_output)
788 		jsonw_null(json_wtr);
789 	return 0;
790 }
791 static int do_load(int argc, char **argv)
792 {
793 	enum bpf_attach_type expected_attach_type;
794 	struct bpf_object_open_attr attr = {
795 		.prog_type	= BPF_PROG_TYPE_UNSPEC,
796 	};
797 	struct map_replace *map_replace = NULL;
798 	unsigned int old_map_fds = 0;
799 	struct bpf_program *prog;
800 	struct bpf_object *obj;
801 	struct bpf_map *map;
802 	const char *pinfile;
803 	unsigned int i, j;
804 	__u32 ifindex = 0;
805 	int idx, err;
806 
807 	if (!REQ_ARGS(2))
808 		return -1;
809 	attr.file = GET_ARG();
810 	pinfile = GET_ARG();
811 
812 	while (argc) {
813 		if (is_prefix(*argv, "type")) {
814 			char *type;
815 
816 			NEXT_ARG();
817 
818 			if (attr.prog_type != BPF_PROG_TYPE_UNSPEC) {
819 				p_err("program type already specified");
820 				goto err_free_reuse_maps;
821 			}
822 			if (!REQ_ARGS(1))
823 				goto err_free_reuse_maps;
824 
825 			/* Put a '/' at the end of type to appease libbpf */
826 			type = malloc(strlen(*argv) + 2);
827 			if (!type) {
828 				p_err("mem alloc failed");
829 				goto err_free_reuse_maps;
830 			}
831 			*type = 0;
832 			strcat(type, *argv);
833 			strcat(type, "/");
834 
835 			err = libbpf_prog_type_by_name(type, &attr.prog_type,
836 						       &expected_attach_type);
837 			free(type);
838 			if (err < 0) {
839 				p_err("unknown program type '%s'", *argv);
840 				goto err_free_reuse_maps;
841 			}
842 			NEXT_ARG();
843 		} else if (is_prefix(*argv, "map")) {
844 			char *endptr, *name;
845 			int fd;
846 
847 			NEXT_ARG();
848 
849 			if (!REQ_ARGS(4))
850 				goto err_free_reuse_maps;
851 
852 			if (is_prefix(*argv, "idx")) {
853 				NEXT_ARG();
854 
855 				idx = strtoul(*argv, &endptr, 0);
856 				if (*endptr) {
857 					p_err("can't parse %s as IDX", *argv);
858 					goto err_free_reuse_maps;
859 				}
860 				name = NULL;
861 			} else if (is_prefix(*argv, "name")) {
862 				NEXT_ARG();
863 
864 				name = *argv;
865 				idx = -1;
866 			} else {
867 				p_err("expected 'idx' or 'name', got: '%s'?",
868 				      *argv);
869 				goto err_free_reuse_maps;
870 			}
871 			NEXT_ARG();
872 
873 			fd = map_parse_fd(&argc, &argv);
874 			if (fd < 0)
875 				goto err_free_reuse_maps;
876 
877 			map_replace = reallocarray(map_replace, old_map_fds + 1,
878 						   sizeof(*map_replace));
879 			if (!map_replace) {
880 				p_err("mem alloc failed");
881 				goto err_free_reuse_maps;
882 			}
883 			map_replace[old_map_fds].idx = idx;
884 			map_replace[old_map_fds].name = name;
885 			map_replace[old_map_fds].fd = fd;
886 			old_map_fds++;
887 		} else if (is_prefix(*argv, "dev")) {
888 			NEXT_ARG();
889 
890 			if (ifindex) {
891 				p_err("offload device already specified");
892 				goto err_free_reuse_maps;
893 			}
894 			if (!REQ_ARGS(1))
895 				goto err_free_reuse_maps;
896 
897 			ifindex = if_nametoindex(*argv);
898 			if (!ifindex) {
899 				p_err("unrecognized netdevice '%s': %s",
900 				      *argv, strerror(errno));
901 				goto err_free_reuse_maps;
902 			}
903 			NEXT_ARG();
904 		} else {
905 			p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
906 			      *argv);
907 			goto err_free_reuse_maps;
908 		}
909 	}
910 
911 	obj = __bpf_object__open_xattr(&attr, bpf_flags);
912 	if (IS_ERR_OR_NULL(obj)) {
913 		p_err("failed to open object file");
914 		goto err_free_reuse_maps;
915 	}
916 
917 	prog = bpf_program__next(NULL, obj);
918 	if (!prog) {
919 		p_err("object file doesn't contain any bpf program");
920 		goto err_close_obj;
921 	}
922 
923 	bpf_program__set_ifindex(prog, ifindex);
924 	if (attr.prog_type == BPF_PROG_TYPE_UNSPEC) {
925 		const char *sec_name = bpf_program__title(prog, false);
926 
927 		err = libbpf_prog_type_by_name(sec_name, &attr.prog_type,
928 					       &expected_attach_type);
929 		if (err < 0) {
930 			p_err("failed to guess program type based on section name %s\n",
931 			      sec_name);
932 			goto err_close_obj;
933 		}
934 	}
935 	bpf_program__set_type(prog, attr.prog_type);
936 	bpf_program__set_expected_attach_type(prog, expected_attach_type);
937 
938 	qsort(map_replace, old_map_fds, sizeof(*map_replace),
939 	      map_replace_compar);
940 
941 	/* After the sort maps by name will be first on the list, because they
942 	 * have idx == -1.  Resolve them.
943 	 */
944 	j = 0;
945 	while (j < old_map_fds && map_replace[j].name) {
946 		i = 0;
947 		bpf_map__for_each(map, obj) {
948 			if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
949 				map_replace[j].idx = i;
950 				break;
951 			}
952 			i++;
953 		}
954 		if (map_replace[j].idx == -1) {
955 			p_err("unable to find map '%s'", map_replace[j].name);
956 			goto err_close_obj;
957 		}
958 		j++;
959 	}
960 	/* Resort if any names were resolved */
961 	if (j)
962 		qsort(map_replace, old_map_fds, sizeof(*map_replace),
963 		      map_replace_compar);
964 
965 	/* Set ifindex and name reuse */
966 	j = 0;
967 	idx = 0;
968 	bpf_map__for_each(map, obj) {
969 		if (!bpf_map__is_offload_neutral(map))
970 			bpf_map__set_ifindex(map, ifindex);
971 
972 		if (j < old_map_fds && idx == map_replace[j].idx) {
973 			err = bpf_map__reuse_fd(map, map_replace[j++].fd);
974 			if (err) {
975 				p_err("unable to set up map reuse: %d", err);
976 				goto err_close_obj;
977 			}
978 
979 			/* Next reuse wants to apply to the same map */
980 			if (j < old_map_fds && map_replace[j].idx == idx) {
981 				p_err("replacement for map idx %d specified more than once",
982 				      idx);
983 				goto err_close_obj;
984 			}
985 		}
986 
987 		idx++;
988 	}
989 	if (j < old_map_fds) {
990 		p_err("map idx '%d' not used", map_replace[j].idx);
991 		goto err_close_obj;
992 	}
993 
994 	err = bpf_object__load(obj);
995 	if (err) {
996 		p_err("failed to load object file");
997 		goto err_close_obj;
998 	}
999 
1000 	if (do_pin_fd(bpf_program__fd(prog), pinfile))
1001 		goto err_close_obj;
1002 
1003 	if (json_output)
1004 		jsonw_null(json_wtr);
1005 
1006 	bpf_object__close(obj);
1007 	for (i = 0; i < old_map_fds; i++)
1008 		close(map_replace[i].fd);
1009 	free(map_replace);
1010 
1011 	return 0;
1012 
1013 err_close_obj:
1014 	bpf_object__close(obj);
1015 err_free_reuse_maps:
1016 	for (i = 0; i < old_map_fds; i++)
1017 		close(map_replace[i].fd);
1018 	free(map_replace);
1019 	return -1;
1020 }
1021 
1022 static int do_help(int argc, char **argv)
1023 {
1024 	if (json_output) {
1025 		jsonw_null(json_wtr);
1026 		return 0;
1027 	}
1028 
1029 	fprintf(stderr,
1030 		"Usage: %s %s { show | list } [PROG]\n"
1031 		"       %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
1032 		"       %s %s dump jited  PROG [{ file FILE | opcodes }]\n"
1033 		"       %s %s pin   PROG FILE\n"
1034 		"       %s %s load  OBJ  FILE [type TYPE] [dev NAME] \\\n"
1035 		"                         [map { idx IDX | name NAME } MAP]\n"
1036 		"       %s %s attach PROG ATTACH_TYPE MAP\n"
1037 		"       %s %s detach PROG ATTACH_TYPE MAP\n"
1038 		"       %s %s help\n"
1039 		"\n"
1040 		"       " HELP_SPEC_MAP "\n"
1041 		"       " HELP_SPEC_PROGRAM "\n"
1042 		"       TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
1043 		"                 tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
1044 		"                 cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
1045 		"                 lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
1046 		"                 cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
1047 		"                 cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
1048 		"                 cgroup/sendmsg4 | cgroup/sendmsg6 }\n"
1049 		"       ATTACH_TYPE := { msg_verdict | skb_verdict | skb_parse }\n"
1050 		"       " HELP_SPEC_OPTIONS "\n"
1051 		"",
1052 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1053 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1054 		bin_name, argv[-2], bin_name, argv[-2]);
1055 
1056 	return 0;
1057 }
1058 
1059 static const struct cmd cmds[] = {
1060 	{ "show",	do_show },
1061 	{ "list",	do_show },
1062 	{ "help",	do_help },
1063 	{ "dump",	do_dump },
1064 	{ "pin",	do_pin },
1065 	{ "load",	do_load },
1066 	{ "attach",	do_attach },
1067 	{ "detach",	do_detach },
1068 	{ 0 }
1069 };
1070 
1071 int do_prog(int argc, char **argv)
1072 {
1073 	return cmd_select(cmds, argc, argv, do_help);
1074 }
1075