xref: /openbmc/linux/tools/bpf/bpftool/prog.c (revision 752bcf80f5549c9901b2e8bc77b2138de55b1026)
102ff58dcSJakub Kicinski // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
202ff58dcSJakub Kicinski /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
371bb428fSJakub Kicinski 
43ff5a4dcSJakub Kicinski #define _GNU_SOURCE
571bb428fSJakub Kicinski #include <errno.h>
671bb428fSJakub Kicinski #include <fcntl.h>
7c9c35995SJakub Kicinski #include <stdarg.h>
871bb428fSJakub Kicinski #include <stdio.h>
971bb428fSJakub Kicinski #include <stdlib.h>
1071bb428fSJakub Kicinski #include <string.h>
1171bb428fSJakub Kicinski #include <time.h>
1271bb428fSJakub Kicinski #include <unistd.h>
13ba6dd679SJakub Kicinski #include <net/if.h>
1471bb428fSJakub Kicinski #include <sys/types.h>
1571bb428fSJakub Kicinski #include <sys/stat.h>
1671bb428fSJakub Kicinski 
17c8406848SJakub Kicinski #include <linux/err.h>
18c8406848SJakub Kicinski 
1971bb428fSJakub Kicinski #include <bpf.h>
20254471e5SYonghong Song #include <btf.h>
2149a086c2SRoman Gushchin #include <libbpf.h>
2271bb428fSJakub Kicinski 
23b6c1cedbSJiong Wang #include "cfg.h"
2471bb428fSJakub Kicinski #include "main.h"
2573bb5b4fSJiong Wang #include "xlated_dumper.h"
2671bb428fSJakub Kicinski 
27b7d3826cSJohn Fastabend static const char * const attach_type_strings[] = {
28b7d3826cSJohn Fastabend 	[BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
29b7d3826cSJohn Fastabend 	[BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
30b7d3826cSJohn Fastabend 	[BPF_SK_MSG_VERDICT] = "msg_verdict",
31092f0892SStanislav Fomichev 	[BPF_FLOW_DISSECTOR] = "flow_dissector",
32b7d3826cSJohn Fastabend 	[__MAX_BPF_ATTACH_TYPE] = NULL,
33b7d3826cSJohn Fastabend };
34b7d3826cSJohn Fastabend 
35c101189bSQuentin Monnet static enum bpf_attach_type parse_attach_type(const char *str)
36b7d3826cSJohn Fastabend {
37b7d3826cSJohn Fastabend 	enum bpf_attach_type type;
38b7d3826cSJohn Fastabend 
39b7d3826cSJohn Fastabend 	for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
40b7d3826cSJohn Fastabend 		if (attach_type_strings[type] &&
41b7d3826cSJohn Fastabend 		    is_prefix(str, attach_type_strings[type]))
42b7d3826cSJohn Fastabend 			return type;
43b7d3826cSJohn Fastabend 	}
44b7d3826cSJohn Fastabend 
45b7d3826cSJohn Fastabend 	return __MAX_BPF_ATTACH_TYPE;
46b7d3826cSJohn Fastabend }
47b7d3826cSJohn Fastabend 
4871bb428fSJakub Kicinski static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
4971bb428fSJakub Kicinski {
5071bb428fSJakub Kicinski 	struct timespec real_time_ts, boot_time_ts;
5171bb428fSJakub Kicinski 	time_t wallclock_secs;
5271bb428fSJakub Kicinski 	struct tm load_tm;
5371bb428fSJakub Kicinski 
5471bb428fSJakub Kicinski 	buf[--size] = '\0';
5571bb428fSJakub Kicinski 
5671bb428fSJakub Kicinski 	if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
5771bb428fSJakub Kicinski 	    clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
5871bb428fSJakub Kicinski 		perror("Can't read clocks");
5971bb428fSJakub Kicinski 		snprintf(buf, size, "%llu", nsecs / 1000000000);
6071bb428fSJakub Kicinski 		return;
6171bb428fSJakub Kicinski 	}
6271bb428fSJakub Kicinski 
6371bb428fSJakub Kicinski 	wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
6407480cbcSJakub Kicinski 		(real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
6507480cbcSJakub Kicinski 		1000000000;
6607480cbcSJakub Kicinski 
6771bb428fSJakub Kicinski 
6871bb428fSJakub Kicinski 	if (!localtime_r(&wallclock_secs, &load_tm)) {
6971bb428fSJakub Kicinski 		snprintf(buf, size, "%llu", nsecs / 1000000000);
7071bb428fSJakub Kicinski 		return;
7171bb428fSJakub Kicinski 	}
7271bb428fSJakub Kicinski 
73a3fe1f6fSQuentin Monnet 	if (json_output)
74a3fe1f6fSQuentin Monnet 		strftime(buf, size, "%s", &load_tm);
75a3fe1f6fSQuentin Monnet 	else
76a3fe1f6fSQuentin Monnet 		strftime(buf, size, "%FT%T%z", &load_tm);
7771bb428fSJakub Kicinski }
7871bb428fSJakub Kicinski 
7971bb428fSJakub Kicinski static int prog_fd_by_tag(unsigned char *tag)
8071bb428fSJakub Kicinski {
8171bb428fSJakub Kicinski 	unsigned int id = 0;
8271bb428fSJakub Kicinski 	int err;
8371bb428fSJakub Kicinski 	int fd;
8471bb428fSJakub Kicinski 
8571bb428fSJakub Kicinski 	while (true) {
86*752bcf80SJiri Olsa 		struct bpf_prog_info info = {};
87*752bcf80SJiri Olsa 		__u32 len = sizeof(info);
88*752bcf80SJiri Olsa 
8971bb428fSJakub Kicinski 		err = bpf_prog_get_next_id(id, &id);
9071bb428fSJakub Kicinski 		if (err) {
919a5ab8bfSQuentin Monnet 			p_err("%s", strerror(errno));
9271bb428fSJakub Kicinski 			return -1;
9371bb428fSJakub Kicinski 		}
9471bb428fSJakub Kicinski 
9571bb428fSJakub Kicinski 		fd = bpf_prog_get_fd_by_id(id);
9671bb428fSJakub Kicinski 		if (fd < 0) {
979a5ab8bfSQuentin Monnet 			p_err("can't get prog by id (%u): %s",
9871bb428fSJakub Kicinski 			      id, strerror(errno));
9971bb428fSJakub Kicinski 			return -1;
10071bb428fSJakub Kicinski 		}
10171bb428fSJakub Kicinski 
10271bb428fSJakub Kicinski 		err = bpf_obj_get_info_by_fd(fd, &info, &len);
10371bb428fSJakub Kicinski 		if (err) {
1049a5ab8bfSQuentin Monnet 			p_err("can't get prog info (%u): %s",
10571bb428fSJakub Kicinski 			      id, strerror(errno));
10671bb428fSJakub Kicinski 			close(fd);
10771bb428fSJakub Kicinski 			return -1;
10871bb428fSJakub Kicinski 		}
10971bb428fSJakub Kicinski 
11071bb428fSJakub Kicinski 		if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
11171bb428fSJakub Kicinski 			return fd;
11271bb428fSJakub Kicinski 
11371bb428fSJakub Kicinski 		close(fd);
11471bb428fSJakub Kicinski 	}
11571bb428fSJakub Kicinski }
11671bb428fSJakub Kicinski 
11771bb428fSJakub Kicinski int prog_parse_fd(int *argc, char ***argv)
11871bb428fSJakub Kicinski {
11971bb428fSJakub Kicinski 	int fd;
12071bb428fSJakub Kicinski 
12171bb428fSJakub Kicinski 	if (is_prefix(**argv, "id")) {
12271bb428fSJakub Kicinski 		unsigned int id;
12371bb428fSJakub Kicinski 		char *endptr;
12471bb428fSJakub Kicinski 
12571bb428fSJakub Kicinski 		NEXT_ARGP();
12671bb428fSJakub Kicinski 
12771bb428fSJakub Kicinski 		id = strtoul(**argv, &endptr, 0);
12871bb428fSJakub Kicinski 		if (*endptr) {
1299a5ab8bfSQuentin Monnet 			p_err("can't parse %s as ID", **argv);
13071bb428fSJakub Kicinski 			return -1;
13171bb428fSJakub Kicinski 		}
13271bb428fSJakub Kicinski 		NEXT_ARGP();
13371bb428fSJakub Kicinski 
13471bb428fSJakub Kicinski 		fd = bpf_prog_get_fd_by_id(id);
13571bb428fSJakub Kicinski 		if (fd < 0)
1369a5ab8bfSQuentin Monnet 			p_err("get by id (%u): %s", id, strerror(errno));
13771bb428fSJakub Kicinski 		return fd;
13871bb428fSJakub Kicinski 	} else if (is_prefix(**argv, "tag")) {
13971bb428fSJakub Kicinski 		unsigned char tag[BPF_TAG_SIZE];
14071bb428fSJakub Kicinski 
14171bb428fSJakub Kicinski 		NEXT_ARGP();
14271bb428fSJakub Kicinski 
14371bb428fSJakub Kicinski 		if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
14471bb428fSJakub Kicinski 			   tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
14571bb428fSJakub Kicinski 		    != BPF_TAG_SIZE) {
1469a5ab8bfSQuentin Monnet 			p_err("can't parse tag");
14771bb428fSJakub Kicinski 			return -1;
14871bb428fSJakub Kicinski 		}
14971bb428fSJakub Kicinski 		NEXT_ARGP();
15071bb428fSJakub Kicinski 
15171bb428fSJakub Kicinski 		return prog_fd_by_tag(tag);
15271bb428fSJakub Kicinski 	} else if (is_prefix(**argv, "pinned")) {
15371bb428fSJakub Kicinski 		char *path;
15471bb428fSJakub Kicinski 
15571bb428fSJakub Kicinski 		NEXT_ARGP();
15671bb428fSJakub Kicinski 
15771bb428fSJakub Kicinski 		path = **argv;
15871bb428fSJakub Kicinski 		NEXT_ARGP();
15971bb428fSJakub Kicinski 
16071bb428fSJakub Kicinski 		return open_obj_pinned_any(path, BPF_OBJ_PROG);
16171bb428fSJakub Kicinski 	}
16271bb428fSJakub Kicinski 
1639a5ab8bfSQuentin Monnet 	p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
16471bb428fSJakub Kicinski 	return -1;
16571bb428fSJakub Kicinski }
16671bb428fSJakub Kicinski 
16771bb428fSJakub Kicinski static void show_prog_maps(int fd, u32 num_maps)
16871bb428fSJakub Kicinski {
16971bb428fSJakub Kicinski 	struct bpf_prog_info info = {};
17071bb428fSJakub Kicinski 	__u32 len = sizeof(info);
17171bb428fSJakub Kicinski 	__u32 map_ids[num_maps];
17271bb428fSJakub Kicinski 	unsigned int i;
17371bb428fSJakub Kicinski 	int err;
17471bb428fSJakub Kicinski 
17571bb428fSJakub Kicinski 	info.nr_map_ids = num_maps;
17671bb428fSJakub Kicinski 	info.map_ids = ptr_to_u64(map_ids);
17771bb428fSJakub Kicinski 
17871bb428fSJakub Kicinski 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
17971bb428fSJakub Kicinski 	if (err || !info.nr_map_ids)
18071bb428fSJakub Kicinski 		return;
18171bb428fSJakub Kicinski 
182743cc665SQuentin Monnet 	if (json_output) {
183743cc665SQuentin Monnet 		jsonw_name(json_wtr, "map_ids");
184743cc665SQuentin Monnet 		jsonw_start_array(json_wtr);
185743cc665SQuentin Monnet 		for (i = 0; i < info.nr_map_ids; i++)
186743cc665SQuentin Monnet 			jsonw_uint(json_wtr, map_ids[i]);
187743cc665SQuentin Monnet 		jsonw_end_array(json_wtr);
188743cc665SQuentin Monnet 	} else {
18971bb428fSJakub Kicinski 		printf("  map_ids ");
19071bb428fSJakub Kicinski 		for (i = 0; i < info.nr_map_ids; i++)
19171bb428fSJakub Kicinski 			printf("%u%s", map_ids[i],
19271bb428fSJakub Kicinski 			       i == info.nr_map_ids - 1 ? "" : ",");
19371bb428fSJakub Kicinski 	}
19471bb428fSJakub Kicinski }
19571bb428fSJakub Kicinski 
196743cc665SQuentin Monnet static void print_prog_json(struct bpf_prog_info *info, int fd)
197743cc665SQuentin Monnet {
198743cc665SQuentin Monnet 	char *memlock;
199743cc665SQuentin Monnet 
200743cc665SQuentin Monnet 	jsonw_start_object(json_wtr);
201743cc665SQuentin Monnet 	jsonw_uint_field(json_wtr, "id", info->id);
202743cc665SQuentin Monnet 	if (info->type < ARRAY_SIZE(prog_type_name))
203743cc665SQuentin Monnet 		jsonw_string_field(json_wtr, "type",
204743cc665SQuentin Monnet 				   prog_type_name[info->type]);
20571bb428fSJakub Kicinski 	else
206743cc665SQuentin Monnet 		jsonw_uint_field(json_wtr, "type", info->type);
20771bb428fSJakub Kicinski 
208743cc665SQuentin Monnet 	if (*info->name)
209743cc665SQuentin Monnet 		jsonw_string_field(json_wtr, "name", info->name);
21071bb428fSJakub Kicinski 
211743cc665SQuentin Monnet 	jsonw_name(json_wtr, "tag");
212743cc665SQuentin Monnet 	jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
213743cc665SQuentin Monnet 		     info->tag[0], info->tag[1], info->tag[2], info->tag[3],
214743cc665SQuentin Monnet 		     info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
21571bb428fSJakub Kicinski 
2169b984a20SJiri Olsa 	jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
2179b984a20SJiri Olsa 
21852262210SJakub Kicinski 	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
21952262210SJakub Kicinski 
220743cc665SQuentin Monnet 	if (info->load_time) {
22171bb428fSJakub Kicinski 		char buf[32];
22271bb428fSJakub Kicinski 
223743cc665SQuentin Monnet 		print_boot_time(info->load_time, buf, sizeof(buf));
22471bb428fSJakub Kicinski 
22571bb428fSJakub Kicinski 		/* Piggy back on load_time, since 0 uid is a valid one */
226a3fe1f6fSQuentin Monnet 		jsonw_name(json_wtr, "loaded_at");
227a3fe1f6fSQuentin Monnet 		jsonw_printf(json_wtr, "%s", buf);
228743cc665SQuentin Monnet 		jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
22971bb428fSJakub Kicinski 	}
23071bb428fSJakub Kicinski 
231743cc665SQuentin Monnet 	jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
23271bb428fSJakub Kicinski 
233743cc665SQuentin Monnet 	if (info->jited_prog_len) {
234743cc665SQuentin Monnet 		jsonw_bool_field(json_wtr, "jited", true);
235743cc665SQuentin Monnet 		jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
236743cc665SQuentin Monnet 	} else {
237743cc665SQuentin Monnet 		jsonw_bool_field(json_wtr, "jited", false);
238743cc665SQuentin Monnet 	}
239743cc665SQuentin Monnet 
240743cc665SQuentin Monnet 	memlock = get_fdinfo(fd, "memlock");
241743cc665SQuentin Monnet 	if (memlock)
242743cc665SQuentin Monnet 		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
243743cc665SQuentin Monnet 	free(memlock);
244743cc665SQuentin Monnet 
245743cc665SQuentin Monnet 	if (info->nr_map_ids)
246743cc665SQuentin Monnet 		show_prog_maps(fd, info->nr_map_ids);
247743cc665SQuentin Monnet 
2484990f1f4SPrashant Bhole 	if (!hash_empty(prog_table.table)) {
2494990f1f4SPrashant Bhole 		struct pinned_obj *obj;
2504990f1f4SPrashant Bhole 
2514990f1f4SPrashant Bhole 		jsonw_name(json_wtr, "pinned");
2524990f1f4SPrashant Bhole 		jsonw_start_array(json_wtr);
2534990f1f4SPrashant Bhole 		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
2544990f1f4SPrashant Bhole 			if (obj->id == info->id)
2554990f1f4SPrashant Bhole 				jsonw_string(json_wtr, obj->path);
2564990f1f4SPrashant Bhole 		}
2574990f1f4SPrashant Bhole 		jsonw_end_array(json_wtr);
2584990f1f4SPrashant Bhole 	}
2594990f1f4SPrashant Bhole 
260743cc665SQuentin Monnet 	jsonw_end_object(json_wtr);
261743cc665SQuentin Monnet }
262743cc665SQuentin Monnet 
263743cc665SQuentin Monnet static void print_prog_plain(struct bpf_prog_info *info, int fd)
264743cc665SQuentin Monnet {
265743cc665SQuentin Monnet 	char *memlock;
266743cc665SQuentin Monnet 
267743cc665SQuentin Monnet 	printf("%u: ", info->id);
268743cc665SQuentin Monnet 	if (info->type < ARRAY_SIZE(prog_type_name))
269743cc665SQuentin Monnet 		printf("%s  ", prog_type_name[info->type]);
270743cc665SQuentin Monnet 	else
271743cc665SQuentin Monnet 		printf("type %u  ", info->type);
272743cc665SQuentin Monnet 
273743cc665SQuentin Monnet 	if (*info->name)
274743cc665SQuentin Monnet 		printf("name %s  ", info->name);
275743cc665SQuentin Monnet 
276743cc665SQuentin Monnet 	printf("tag ");
277743cc665SQuentin Monnet 	fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
27852262210SJakub Kicinski 	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
2799b984a20SJiri Olsa 	printf("%s", info->gpl_compatible ? "  gpl" : "");
280743cc665SQuentin Monnet 	printf("\n");
281743cc665SQuentin Monnet 
282743cc665SQuentin Monnet 	if (info->load_time) {
283743cc665SQuentin Monnet 		char buf[32];
284743cc665SQuentin Monnet 
285743cc665SQuentin Monnet 		print_boot_time(info->load_time, buf, sizeof(buf));
286743cc665SQuentin Monnet 
287743cc665SQuentin Monnet 		/* Piggy back on load_time, since 0 uid is a valid one */
288743cc665SQuentin Monnet 		printf("\tloaded_at %s  uid %u\n", buf, info->created_by_uid);
289743cc665SQuentin Monnet 	}
290743cc665SQuentin Monnet 
291743cc665SQuentin Monnet 	printf("\txlated %uB", info->xlated_prog_len);
292743cc665SQuentin Monnet 
293743cc665SQuentin Monnet 	if (info->jited_prog_len)
294743cc665SQuentin Monnet 		printf("  jited %uB", info->jited_prog_len);
29571bb428fSJakub Kicinski 	else
29671bb428fSJakub Kicinski 		printf("  not jited");
29771bb428fSJakub Kicinski 
29871bb428fSJakub Kicinski 	memlock = get_fdinfo(fd, "memlock");
29971bb428fSJakub Kicinski 	if (memlock)
30071bb428fSJakub Kicinski 		printf("  memlock %sB", memlock);
30171bb428fSJakub Kicinski 	free(memlock);
30271bb428fSJakub Kicinski 
303743cc665SQuentin Monnet 	if (info->nr_map_ids)
304743cc665SQuentin Monnet 		show_prog_maps(fd, info->nr_map_ids);
30571bb428fSJakub Kicinski 
3064990f1f4SPrashant Bhole 	if (!hash_empty(prog_table.table)) {
3074990f1f4SPrashant Bhole 		struct pinned_obj *obj;
3084990f1f4SPrashant Bhole 
3094990f1f4SPrashant Bhole 		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
3104990f1f4SPrashant Bhole 			if (obj->id == info->id)
311a8bfd2bcSQuentin Monnet 				printf("\n\tpinned %s", obj->path);
3124990f1f4SPrashant Bhole 		}
3134990f1f4SPrashant Bhole 	}
3144990f1f4SPrashant Bhole 
31571bb428fSJakub Kicinski 	printf("\n");
316743cc665SQuentin Monnet }
317743cc665SQuentin Monnet 
318743cc665SQuentin Monnet static int show_prog(int fd)
319743cc665SQuentin Monnet {
320743cc665SQuentin Monnet 	struct bpf_prog_info info = {};
321743cc665SQuentin Monnet 	__u32 len = sizeof(info);
322743cc665SQuentin Monnet 	int err;
323743cc665SQuentin Monnet 
324743cc665SQuentin Monnet 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
325743cc665SQuentin Monnet 	if (err) {
3269a5ab8bfSQuentin Monnet 		p_err("can't get prog info: %s", strerror(errno));
327743cc665SQuentin Monnet 		return -1;
328743cc665SQuentin Monnet 	}
329743cc665SQuentin Monnet 
330743cc665SQuentin Monnet 	if (json_output)
331743cc665SQuentin Monnet 		print_prog_json(&info, fd);
332743cc665SQuentin Monnet 	else
333743cc665SQuentin Monnet 		print_prog_plain(&info, fd);
33471bb428fSJakub Kicinski 
33571bb428fSJakub Kicinski 	return 0;
33671bb428fSJakub Kicinski }
33771bb428fSJakub Kicinski 
33871bb428fSJakub Kicinski static int do_show(int argc, char **argv)
339743cc665SQuentin Monnet {
340743cc665SQuentin Monnet 	__u32 id = 0;
34171bb428fSJakub Kicinski 	int err;
34271bb428fSJakub Kicinski 	int fd;
34371bb428fSJakub Kicinski 
344c541b734SPrashant Bhole 	if (show_pinned)
3454990f1f4SPrashant Bhole 		build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
3464990f1f4SPrashant Bhole 
34771bb428fSJakub Kicinski 	if (argc == 2) {
34871bb428fSJakub Kicinski 		fd = prog_parse_fd(&argc, &argv);
34971bb428fSJakub Kicinski 		if (fd < 0)
35071bb428fSJakub Kicinski 			return -1;
35171bb428fSJakub Kicinski 
35271bb428fSJakub Kicinski 		return show_prog(fd);
35371bb428fSJakub Kicinski 	}
35471bb428fSJakub Kicinski 
35571bb428fSJakub Kicinski 	if (argc)
35671bb428fSJakub Kicinski 		return BAD_ARG();
35771bb428fSJakub Kicinski 
358743cc665SQuentin Monnet 	if (json_output)
359743cc665SQuentin Monnet 		jsonw_start_array(json_wtr);
36071bb428fSJakub Kicinski 	while (true) {
36171bb428fSJakub Kicinski 		err = bpf_prog_get_next_id(id, &id);
36271bb428fSJakub Kicinski 		if (err) {
3631739c26dSQuentin Monnet 			if (errno == ENOENT) {
3641739c26dSQuentin Monnet 				err = 0;
36571bb428fSJakub Kicinski 				break;
3661739c26dSQuentin Monnet 			}
3679a5ab8bfSQuentin Monnet 			p_err("can't get next program: %s%s", strerror(errno),
3689a5ab8bfSQuentin Monnet 			      errno == EINVAL ? " -- kernel too old?" : "");
369743cc665SQuentin Monnet 			err = -1;
370743cc665SQuentin Monnet 			break;
37171bb428fSJakub Kicinski 		}
37271bb428fSJakub Kicinski 
37371bb428fSJakub Kicinski 		fd = bpf_prog_get_fd_by_id(id);
37471bb428fSJakub Kicinski 		if (fd < 0) {
3758207c6ddSJakub Kicinski 			if (errno == ENOENT)
3768207c6ddSJakub Kicinski 				continue;
3779a5ab8bfSQuentin Monnet 			p_err("can't get prog by id (%u): %s",
37871bb428fSJakub Kicinski 			      id, strerror(errno));
379743cc665SQuentin Monnet 			err = -1;
380743cc665SQuentin Monnet 			break;
38171bb428fSJakub Kicinski 		}
38271bb428fSJakub Kicinski 
38371bb428fSJakub Kicinski 		err = show_prog(fd);
38471bb428fSJakub Kicinski 		close(fd);
38571bb428fSJakub Kicinski 		if (err)
386743cc665SQuentin Monnet 			break;
38771bb428fSJakub Kicinski 	}
38871bb428fSJakub Kicinski 
389743cc665SQuentin Monnet 	if (json_output)
390743cc665SQuentin Monnet 		jsonw_end_array(json_wtr);
391743cc665SQuentin Monnet 
392743cc665SQuentin Monnet 	return err;
39371bb428fSJakub Kicinski }
39471bb428fSJakub Kicinski 
39571bb428fSJakub Kicinski static int do_dump(int argc, char **argv)
39671bb428fSJakub Kicinski {
397b053b439SMartin KaFai Lau 	unsigned int finfo_rec_size, linfo_rec_size, jited_linfo_rec_size;
398b053b439SMartin KaFai Lau 	void *func_info = NULL, *linfo = NULL, *jited_linfo = NULL;
399cfc54241SYonghong Song 	unsigned int nr_finfo, nr_linfo = 0, nr_jited_linfo = 0;
400b053b439SMartin KaFai Lau 	struct bpf_prog_linfo *prog_linfo = NULL;
401f84192eeSSandipan Das 	unsigned long *func_ksyms = NULL;
40271bb428fSJakub Kicinski 	struct bpf_prog_info info = {};
403f7f62c71SSandipan Das 	unsigned int *func_lens = NULL;
4043ddeac67SJakub Kicinski 	const char *disasm_opt = NULL;
405f84192eeSSandipan Das 	unsigned int nr_func_ksyms;
406f7f62c71SSandipan Das 	unsigned int nr_func_lens;
4077105e828SDaniel Borkmann 	struct dump_data dd = {};
40871bb428fSJakub Kicinski 	__u32 len = sizeof(info);
409254471e5SYonghong Song 	struct btf *btf = NULL;
41071bb428fSJakub Kicinski 	unsigned int buf_size;
41171bb428fSJakub Kicinski 	char *filepath = NULL;
41271bb428fSJakub Kicinski 	bool opcodes = false;
413b6c1cedbSJiong Wang 	bool visual = false;
414254471e5SYonghong Song 	char func_sig[1024];
41571bb428fSJakub Kicinski 	unsigned char *buf;
416b053b439SMartin KaFai Lau 	bool linum = false;
41771bb428fSJakub Kicinski 	__u32 *member_len;
41871bb428fSJakub Kicinski 	__u64 *member_ptr;
41971bb428fSJakub Kicinski 	ssize_t n;
42071bb428fSJakub Kicinski 	int err;
42171bb428fSJakub Kicinski 	int fd;
42271bb428fSJakub Kicinski 
42371bb428fSJakub Kicinski 	if (is_prefix(*argv, "jited")) {
42429a9c10eSStanislav Fomichev 		if (disasm_init())
42529a9c10eSStanislav Fomichev 			return -1;
42629a9c10eSStanislav Fomichev 
42771bb428fSJakub Kicinski 		member_len = &info.jited_prog_len;
42871bb428fSJakub Kicinski 		member_ptr = &info.jited_prog_insns;
42971bb428fSJakub Kicinski 	} else if (is_prefix(*argv, "xlated")) {
43071bb428fSJakub Kicinski 		member_len = &info.xlated_prog_len;
43171bb428fSJakub Kicinski 		member_ptr = &info.xlated_prog_insns;
43271bb428fSJakub Kicinski 	} else {
4339a5ab8bfSQuentin Monnet 		p_err("expected 'xlated' or 'jited', got: %s", *argv);
43471bb428fSJakub Kicinski 		return -1;
43571bb428fSJakub Kicinski 	}
43671bb428fSJakub Kicinski 	NEXT_ARG();
43771bb428fSJakub Kicinski 
43871bb428fSJakub Kicinski 	if (argc < 2)
43971bb428fSJakub Kicinski 		usage();
44071bb428fSJakub Kicinski 
44171bb428fSJakub Kicinski 	fd = prog_parse_fd(&argc, &argv);
44271bb428fSJakub Kicinski 	if (fd < 0)
44371bb428fSJakub Kicinski 		return -1;
44471bb428fSJakub Kicinski 
44571bb428fSJakub Kicinski 	if (is_prefix(*argv, "file")) {
44671bb428fSJakub Kicinski 		NEXT_ARG();
44771bb428fSJakub Kicinski 		if (!argc) {
4489a5ab8bfSQuentin Monnet 			p_err("expected file path");
44971bb428fSJakub Kicinski 			return -1;
45071bb428fSJakub Kicinski 		}
45171bb428fSJakub Kicinski 
45271bb428fSJakub Kicinski 		filepath = *argv;
45371bb428fSJakub Kicinski 		NEXT_ARG();
45471bb428fSJakub Kicinski 	} else if (is_prefix(*argv, "opcodes")) {
45571bb428fSJakub Kicinski 		opcodes = true;
45671bb428fSJakub Kicinski 		NEXT_ARG();
457b6c1cedbSJiong Wang 	} else if (is_prefix(*argv, "visual")) {
458b6c1cedbSJiong Wang 		visual = true;
459b6c1cedbSJiong Wang 		NEXT_ARG();
460b053b439SMartin KaFai Lau 	} else if (is_prefix(*argv, "linum")) {
461b053b439SMartin KaFai Lau 		linum = true;
462b053b439SMartin KaFai Lau 		NEXT_ARG();
46371bb428fSJakub Kicinski 	}
46471bb428fSJakub Kicinski 
46571bb428fSJakub Kicinski 	if (argc) {
46671bb428fSJakub Kicinski 		usage();
46771bb428fSJakub Kicinski 		return -1;
46871bb428fSJakub Kicinski 	}
46971bb428fSJakub Kicinski 
47071bb428fSJakub Kicinski 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
47171bb428fSJakub Kicinski 	if (err) {
4729a5ab8bfSQuentin Monnet 		p_err("can't get prog info: %s", strerror(errno));
47371bb428fSJakub Kicinski 		return -1;
47471bb428fSJakub Kicinski 	}
47571bb428fSJakub Kicinski 
47671bb428fSJakub Kicinski 	if (!*member_len) {
4779a5ab8bfSQuentin Monnet 		p_info("no instructions returned");
47871bb428fSJakub Kicinski 		close(fd);
47971bb428fSJakub Kicinski 		return 0;
48071bb428fSJakub Kicinski 	}
48171bb428fSJakub Kicinski 
48271bb428fSJakub Kicinski 	buf_size = *member_len;
48371bb428fSJakub Kicinski 
48471bb428fSJakub Kicinski 	buf = malloc(buf_size);
48571bb428fSJakub Kicinski 	if (!buf) {
4869a5ab8bfSQuentin Monnet 		p_err("mem alloc failed");
48771bb428fSJakub Kicinski 		close(fd);
48871bb428fSJakub Kicinski 		return -1;
48971bb428fSJakub Kicinski 	}
49071bb428fSJakub Kicinski 
491f84192eeSSandipan Das 	nr_func_ksyms = info.nr_jited_ksyms;
492f84192eeSSandipan Das 	if (nr_func_ksyms) {
493f84192eeSSandipan Das 		func_ksyms = malloc(nr_func_ksyms * sizeof(__u64));
494f84192eeSSandipan Das 		if (!func_ksyms) {
495f84192eeSSandipan Das 			p_err("mem alloc failed");
496f84192eeSSandipan Das 			close(fd);
497f84192eeSSandipan Das 			goto err_free;
498f84192eeSSandipan Das 		}
499f84192eeSSandipan Das 	}
500f84192eeSSandipan Das 
501f7f62c71SSandipan Das 	nr_func_lens = info.nr_jited_func_lens;
502f7f62c71SSandipan Das 	if (nr_func_lens) {
503f7f62c71SSandipan Das 		func_lens = malloc(nr_func_lens * sizeof(__u32));
504f7f62c71SSandipan Das 		if (!func_lens) {
505f7f62c71SSandipan Das 			p_err("mem alloc failed");
506f7f62c71SSandipan Das 			close(fd);
507f7f62c71SSandipan Das 			goto err_free;
508f7f62c71SSandipan Das 		}
509f7f62c71SSandipan Das 	}
510f7f62c71SSandipan Das 
511cfc54241SYonghong Song 	nr_finfo = info.nr_func_info;
512254471e5SYonghong Song 	finfo_rec_size = info.func_info_rec_size;
513cfc54241SYonghong Song 	if (nr_finfo && finfo_rec_size) {
514cfc54241SYonghong Song 		func_info = malloc(nr_finfo * finfo_rec_size);
515254471e5SYonghong Song 		if (!func_info) {
516254471e5SYonghong Song 			p_err("mem alloc failed");
517254471e5SYonghong Song 			close(fd);
518254471e5SYonghong Song 			goto err_free;
519254471e5SYonghong Song 		}
520254471e5SYonghong Song 	}
521254471e5SYonghong Song 
522b053b439SMartin KaFai Lau 	linfo_rec_size = info.line_info_rec_size;
523cfc54241SYonghong Song 	if (info.nr_line_info && linfo_rec_size && info.btf_id) {
524cfc54241SYonghong Song 		nr_linfo = info.nr_line_info;
525cfc54241SYonghong Song 		linfo = malloc(nr_linfo * linfo_rec_size);
526b053b439SMartin KaFai Lau 		if (!linfo) {
527b053b439SMartin KaFai Lau 			p_err("mem alloc failed");
528b053b439SMartin KaFai Lau 			close(fd);
529b053b439SMartin KaFai Lau 			goto err_free;
530b053b439SMartin KaFai Lau 		}
531b053b439SMartin KaFai Lau 	}
532b053b439SMartin KaFai Lau 
533b053b439SMartin KaFai Lau 	jited_linfo_rec_size = info.jited_line_info_rec_size;
534cfc54241SYonghong Song 	if (info.nr_jited_line_info &&
535b053b439SMartin KaFai Lau 	    jited_linfo_rec_size &&
536b053b439SMartin KaFai Lau 	    info.nr_jited_ksyms &&
537b053b439SMartin KaFai Lau 	    info.nr_jited_func_lens &&
538b053b439SMartin KaFai Lau 	    info.btf_id) {
539cfc54241SYonghong Song 		nr_jited_linfo = info.nr_jited_line_info;
540cfc54241SYonghong Song 		jited_linfo = malloc(nr_jited_linfo * jited_linfo_rec_size);
541b053b439SMartin KaFai Lau 		if (!jited_linfo) {
542b053b439SMartin KaFai Lau 			p_err("mem alloc failed");
543b053b439SMartin KaFai Lau 			close(fd);
544b053b439SMartin KaFai Lau 			goto err_free;
545b053b439SMartin KaFai Lau 		}
546b053b439SMartin KaFai Lau 	}
547b053b439SMartin KaFai Lau 
54871bb428fSJakub Kicinski 	memset(&info, 0, sizeof(info));
54971bb428fSJakub Kicinski 
55071bb428fSJakub Kicinski 	*member_ptr = ptr_to_u64(buf);
55171bb428fSJakub Kicinski 	*member_len = buf_size;
552f84192eeSSandipan Das 	info.jited_ksyms = ptr_to_u64(func_ksyms);
553f84192eeSSandipan Das 	info.nr_jited_ksyms = nr_func_ksyms;
554f7f62c71SSandipan Das 	info.jited_func_lens = ptr_to_u64(func_lens);
555f7f62c71SSandipan Das 	info.nr_jited_func_lens = nr_func_lens;
556cfc54241SYonghong Song 	info.nr_func_info = nr_finfo;
557254471e5SYonghong Song 	info.func_info_rec_size = finfo_rec_size;
558254471e5SYonghong Song 	info.func_info = ptr_to_u64(func_info);
559cfc54241SYonghong Song 	info.nr_line_info = nr_linfo;
560b053b439SMartin KaFai Lau 	info.line_info_rec_size = linfo_rec_size;
561b053b439SMartin KaFai Lau 	info.line_info = ptr_to_u64(linfo);
562cfc54241SYonghong Song 	info.nr_jited_line_info = nr_jited_linfo;
563b053b439SMartin KaFai Lau 	info.jited_line_info_rec_size = jited_linfo_rec_size;
564b053b439SMartin KaFai Lau 	info.jited_line_info = ptr_to_u64(jited_linfo);
565b053b439SMartin KaFai Lau 
56671bb428fSJakub Kicinski 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
56771bb428fSJakub Kicinski 	close(fd);
56871bb428fSJakub Kicinski 	if (err) {
5699a5ab8bfSQuentin Monnet 		p_err("can't get prog info: %s", strerror(errno));
57071bb428fSJakub Kicinski 		goto err_free;
57171bb428fSJakub Kicinski 	}
57271bb428fSJakub Kicinski 
57371bb428fSJakub Kicinski 	if (*member_len > buf_size) {
5749a5ab8bfSQuentin Monnet 		p_err("too many instructions returned");
57571bb428fSJakub Kicinski 		goto err_free;
57671bb428fSJakub Kicinski 	}
57771bb428fSJakub Kicinski 
578f84192eeSSandipan Das 	if (info.nr_jited_ksyms > nr_func_ksyms) {
579f84192eeSSandipan Das 		p_err("too many addresses returned");
580f84192eeSSandipan Das 		goto err_free;
581f84192eeSSandipan Das 	}
582f84192eeSSandipan Das 
583f7f62c71SSandipan Das 	if (info.nr_jited_func_lens > nr_func_lens) {
584f7f62c71SSandipan Das 		p_err("too many values returned");
585f7f62c71SSandipan Das 		goto err_free;
586f7f62c71SSandipan Das 	}
587f7f62c71SSandipan Das 
588cfc54241SYonghong Song 	if (info.nr_func_info != nr_finfo) {
589cfc54241SYonghong Song 		p_err("incorrect nr_func_info %d vs. expected %d",
590cfc54241SYonghong Song 		      info.nr_func_info, nr_finfo);
591254471e5SYonghong Song 		goto err_free;
592254471e5SYonghong Song 	}
593254471e5SYonghong Song 
594254471e5SYonghong Song 	if (info.func_info_rec_size != finfo_rec_size) {
595254471e5SYonghong Song 		p_err("incorrect func_info_rec_size %d vs. expected %d",
596254471e5SYonghong Song 		      info.func_info_rec_size, finfo_rec_size);
597254471e5SYonghong Song 		goto err_free;
598254471e5SYonghong Song 	}
599254471e5SYonghong Song 
600cfc54241SYonghong Song 	if (linfo && info.nr_line_info != nr_linfo) {
601cfc54241SYonghong Song 		p_err("incorrect nr_line_info %u vs. expected %u",
602cfc54241SYonghong Song 		      info.nr_line_info, nr_linfo);
603b053b439SMartin KaFai Lau 		goto err_free;
604b053b439SMartin KaFai Lau 	}
605b053b439SMartin KaFai Lau 
606b053b439SMartin KaFai Lau 	if (info.line_info_rec_size != linfo_rec_size) {
607b053b439SMartin KaFai Lau 		p_err("incorrect line_info_rec_size %u vs. expected %u",
608b053b439SMartin KaFai Lau 		      info.line_info_rec_size, linfo_rec_size);
609b053b439SMartin KaFai Lau 		goto err_free;
610b053b439SMartin KaFai Lau 	}
611b053b439SMartin KaFai Lau 
612cfc54241SYonghong Song 	if (jited_linfo && info.nr_jited_line_info != nr_jited_linfo) {
613cfc54241SYonghong Song 		p_err("incorrect nr_jited_line_info %u vs. expected %u",
614cfc54241SYonghong Song 		      info.nr_jited_line_info, nr_jited_linfo);
615b053b439SMartin KaFai Lau 		goto err_free;
616b053b439SMartin KaFai Lau 	}
617b053b439SMartin KaFai Lau 
618b053b439SMartin KaFai Lau 	if (info.jited_line_info_rec_size != jited_linfo_rec_size) {
619b053b439SMartin KaFai Lau 		p_err("incorrect jited_line_info_rec_size %u vs. expected %u",
620b053b439SMartin KaFai Lau 		      info.jited_line_info_rec_size, jited_linfo_rec_size);
621b053b439SMartin KaFai Lau 		goto err_free;
622b053b439SMartin KaFai Lau 	}
623b053b439SMartin KaFai Lau 
6247105e828SDaniel Borkmann 	if ((member_len == &info.jited_prog_len &&
6257105e828SDaniel Borkmann 	     info.jited_prog_insns == 0) ||
6267105e828SDaniel Borkmann 	    (member_len == &info.xlated_prog_len &&
6277105e828SDaniel Borkmann 	     info.xlated_prog_insns == 0)) {
6287105e828SDaniel Borkmann 		p_err("error retrieving insn dump: kernel.kptr_restrict set?");
6297105e828SDaniel Borkmann 		goto err_free;
6307105e828SDaniel Borkmann 	}
6317105e828SDaniel Borkmann 
6321d2f44caSMartin KaFai Lau 	if (info.btf_id && btf__get_from_id(info.btf_id, &btf)) {
633254471e5SYonghong Song 		p_err("failed to get btf");
634254471e5SYonghong Song 		goto err_free;
635254471e5SYonghong Song 	}
636254471e5SYonghong Song 
637cfc54241SYonghong Song 	if (nr_linfo) {
638b053b439SMartin KaFai Lau 		prog_linfo = bpf_prog_linfo__new(&info);
639b053b439SMartin KaFai Lau 		if (!prog_linfo)
64010a5ce98SMartin KaFai Lau 			p_info("error in processing bpf_line_info.  continue without it.");
641b053b439SMartin KaFai Lau 	}
642b053b439SMartin KaFai Lau 
64371bb428fSJakub Kicinski 	if (filepath) {
64471bb428fSJakub Kicinski 		fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
64571bb428fSJakub Kicinski 		if (fd < 0) {
6469a5ab8bfSQuentin Monnet 			p_err("can't open file %s: %s", filepath,
64771bb428fSJakub Kicinski 			      strerror(errno));
64871bb428fSJakub Kicinski 			goto err_free;
64971bb428fSJakub Kicinski 		}
65071bb428fSJakub Kicinski 
65171bb428fSJakub Kicinski 		n = write(fd, buf, *member_len);
65271bb428fSJakub Kicinski 		close(fd);
65371bb428fSJakub Kicinski 		if (n != *member_len) {
6549a5ab8bfSQuentin Monnet 			p_err("error writing output file: %s",
65571bb428fSJakub Kicinski 			      n < 0 ? strerror(errno) : "short write");
65671bb428fSJakub Kicinski 			goto err_free;
65771bb428fSJakub Kicinski 		}
65852c84d36SQuentin Monnet 
65952c84d36SQuentin Monnet 		if (json_output)
66052c84d36SQuentin Monnet 			jsonw_null(json_wtr);
6613197239dSJiong Wang 	} else if (member_len == &info.jited_prog_len) {
662e6593596SJiong Wang 		const char *name = NULL;
663e6593596SJiong Wang 
664e6593596SJiong Wang 		if (info.ifindex) {
6653ddeac67SJakub Kicinski 			name = ifindex_to_bfd_params(info.ifindex,
666e6593596SJiong Wang 						     info.netns_dev,
6673ddeac67SJakub Kicinski 						     info.netns_ino,
6683ddeac67SJakub Kicinski 						     &disasm_opt);
669e6593596SJiong Wang 			if (!name)
670e6593596SJiong Wang 				goto err_free;
671e6593596SJiong Wang 		}
672e6593596SJiong Wang 
673f7f62c71SSandipan Das 		if (info.nr_jited_func_lens && info.jited_func_lens) {
674f7f62c71SSandipan Das 			struct kernel_sym *sym = NULL;
675254471e5SYonghong Song 			struct bpf_func_info *record;
676f7f62c71SSandipan Das 			char sym_name[SYM_MAX_NAME];
677f7f62c71SSandipan Das 			unsigned char *img = buf;
678f7f62c71SSandipan Das 			__u64 *ksyms = NULL;
679f7f62c71SSandipan Das 			__u32 *lens;
680f7f62c71SSandipan Das 			__u32 i;
681f7f62c71SSandipan Das 
682f7f62c71SSandipan Das 			if (info.nr_jited_ksyms) {
683f7f62c71SSandipan Das 				kernel_syms_load(&dd);
684f7f62c71SSandipan Das 				ksyms = (__u64 *) info.jited_ksyms;
685f7f62c71SSandipan Das 			}
686f7f62c71SSandipan Das 
687f7f62c71SSandipan Das 			if (json_output)
688f7f62c71SSandipan Das 				jsonw_start_array(json_wtr);
689f7f62c71SSandipan Das 
690f7f62c71SSandipan Das 			lens = (__u32 *) info.jited_func_lens;
691f7f62c71SSandipan Das 			for (i = 0; i < info.nr_jited_func_lens; i++) {
692f7f62c71SSandipan Das 				if (ksyms) {
693f7f62c71SSandipan Das 					sym = kernel_syms_search(&dd, ksyms[i]);
694f7f62c71SSandipan Das 					if (sym)
695f7f62c71SSandipan Das 						sprintf(sym_name, "%s", sym->name);
696f7f62c71SSandipan Das 					else
697f7f62c71SSandipan Das 						sprintf(sym_name, "0x%016llx", ksyms[i]);
698f7f62c71SSandipan Das 				} else {
699f7f62c71SSandipan Das 					strcpy(sym_name, "unknown");
700f7f62c71SSandipan Das 				}
701f7f62c71SSandipan Das 
702254471e5SYonghong Song 				if (func_info) {
703254471e5SYonghong Song 					record = func_info + i * finfo_rec_size;
704254471e5SYonghong Song 					btf_dumper_type_only(btf, record->type_id,
705254471e5SYonghong Song 							     func_sig,
706254471e5SYonghong Song 							     sizeof(func_sig));
707254471e5SYonghong Song 				}
708254471e5SYonghong Song 
709f7f62c71SSandipan Das 				if (json_output) {
710f7f62c71SSandipan Das 					jsonw_start_object(json_wtr);
711254471e5SYonghong Song 					if (func_info && func_sig[0] != '\0') {
712254471e5SYonghong Song 						jsonw_name(json_wtr, "proto");
713254471e5SYonghong Song 						jsonw_string(json_wtr, func_sig);
714254471e5SYonghong Song 					}
715f7f62c71SSandipan Das 					jsonw_name(json_wtr, "name");
716f7f62c71SSandipan Das 					jsonw_string(json_wtr, sym_name);
717f7f62c71SSandipan Das 					jsonw_name(json_wtr, "insns");
718f7f62c71SSandipan Das 				} else {
719254471e5SYonghong Song 					if (func_info && func_sig[0] != '\0')
720254471e5SYonghong Song 						printf("%s:\n", func_sig);
721f7f62c71SSandipan Das 					printf("%s:\n", sym_name);
722f7f62c71SSandipan Das 				}
723f7f62c71SSandipan Das 
724b053b439SMartin KaFai Lau 				disasm_print_insn(img, lens[i], opcodes,
725b053b439SMartin KaFai Lau 						  name, disasm_opt, btf,
726b053b439SMartin KaFai Lau 						  prog_linfo, ksyms[i], i,
727b053b439SMartin KaFai Lau 						  linum);
728b053b439SMartin KaFai Lau 
729f7f62c71SSandipan Das 				img += lens[i];
730f7f62c71SSandipan Das 
731f7f62c71SSandipan Das 				if (json_output)
732f7f62c71SSandipan Das 					jsonw_end_object(json_wtr);
733f7f62c71SSandipan Das 				else
734f7f62c71SSandipan Das 					printf("\n");
735f7f62c71SSandipan Das 			}
736f7f62c71SSandipan Das 
737f7f62c71SSandipan Das 			if (json_output)
738f7f62c71SSandipan Das 				jsonw_end_array(json_wtr);
739f7f62c71SSandipan Das 		} else {
7403ddeac67SJakub Kicinski 			disasm_print_insn(buf, *member_len, opcodes, name,
741b053b439SMartin KaFai Lau 					  disasm_opt, btf, NULL, 0, 0, false);
742f7f62c71SSandipan Das 		}
743b6c1cedbSJiong Wang 	} else if (visual) {
744b6c1cedbSJiong Wang 		if (json_output)
745b6c1cedbSJiong Wang 			jsonw_null(json_wtr);
746b6c1cedbSJiong Wang 		else
747b6c1cedbSJiong Wang 			dump_xlated_cfg(buf, *member_len);
7487105e828SDaniel Borkmann 	} else {
7497105e828SDaniel Borkmann 		kernel_syms_load(&dd);
750f84192eeSSandipan Das 		dd.nr_jited_ksyms = info.nr_jited_ksyms;
751f84192eeSSandipan Das 		dd.jited_ksyms = (__u64 *) info.jited_ksyms;
752254471e5SYonghong Song 		dd.btf = btf;
753254471e5SYonghong Song 		dd.func_info = func_info;
754254471e5SYonghong Song 		dd.finfo_rec_size = finfo_rec_size;
755b053b439SMartin KaFai Lau 		dd.prog_linfo = prog_linfo;
756f84192eeSSandipan Das 
757f05e2c32SQuentin Monnet 		if (json_output)
758b053b439SMartin KaFai Lau 			dump_xlated_json(&dd, buf, *member_len, opcodes,
759b053b439SMartin KaFai Lau 					 linum);
760f05e2c32SQuentin Monnet 		else
761b053b439SMartin KaFai Lau 			dump_xlated_plain(&dd, buf, *member_len, opcodes,
762b053b439SMartin KaFai Lau 					  linum);
7637105e828SDaniel Borkmann 		kernel_syms_destroy(&dd);
7647105e828SDaniel Borkmann 	}
76571bb428fSJakub Kicinski 
76671bb428fSJakub Kicinski 	free(buf);
767f84192eeSSandipan Das 	free(func_ksyms);
768f7f62c71SSandipan Das 	free(func_lens);
769254471e5SYonghong Song 	free(func_info);
770b053b439SMartin KaFai Lau 	free(linfo);
771b053b439SMartin KaFai Lau 	free(jited_linfo);
772b053b439SMartin KaFai Lau 	bpf_prog_linfo__free(prog_linfo);
77371bb428fSJakub Kicinski 	return 0;
77471bb428fSJakub Kicinski 
77571bb428fSJakub Kicinski err_free:
77671bb428fSJakub Kicinski 	free(buf);
777f84192eeSSandipan Das 	free(func_ksyms);
778f7f62c71SSandipan Das 	free(func_lens);
779254471e5SYonghong Song 	free(func_info);
780b053b439SMartin KaFai Lau 	free(linfo);
781b053b439SMartin KaFai Lau 	free(jited_linfo);
782b053b439SMartin KaFai Lau 	bpf_prog_linfo__free(prog_linfo);
78371bb428fSJakub Kicinski 	return -1;
78471bb428fSJakub Kicinski }
78571bb428fSJakub Kicinski 
78671bb428fSJakub Kicinski static int do_pin(int argc, char **argv)
78771bb428fSJakub Kicinski {
788004b45c0SQuentin Monnet 	int err;
789004b45c0SQuentin Monnet 
790004b45c0SQuentin Monnet 	err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
791004b45c0SQuentin Monnet 	if (!err && json_output)
792004b45c0SQuentin Monnet 		jsonw_null(json_wtr);
793004b45c0SQuentin Monnet 	return err;
79471bb428fSJakub Kicinski }
79571bb428fSJakub Kicinski 
7963ff5a4dcSJakub Kicinski struct map_replace {
7973ff5a4dcSJakub Kicinski 	int idx;
7983ff5a4dcSJakub Kicinski 	int fd;
7993ff5a4dcSJakub Kicinski 	char *name;
8003ff5a4dcSJakub Kicinski };
8013ff5a4dcSJakub Kicinski 
802c101189bSQuentin Monnet static int map_replace_compar(const void *p1, const void *p2)
8033ff5a4dcSJakub Kicinski {
8043ff5a4dcSJakub Kicinski 	const struct map_replace *a = p1, *b = p2;
8053ff5a4dcSJakub Kicinski 
8063ff5a4dcSJakub Kicinski 	return a->idx - b->idx;
8073ff5a4dcSJakub Kicinski }
8083ff5a4dcSJakub Kicinski 
809092f0892SStanislav Fomichev static int parse_attach_detach_args(int argc, char **argv, int *progfd,
810092f0892SStanislav Fomichev 				    enum bpf_attach_type *attach_type,
811092f0892SStanislav Fomichev 				    int *mapfd)
812092f0892SStanislav Fomichev {
813092f0892SStanislav Fomichev 	if (!REQ_ARGS(3))
814092f0892SStanislav Fomichev 		return -EINVAL;
815092f0892SStanislav Fomichev 
816092f0892SStanislav Fomichev 	*progfd = prog_parse_fd(&argc, &argv);
817092f0892SStanislav Fomichev 	if (*progfd < 0)
818092f0892SStanislav Fomichev 		return *progfd;
819092f0892SStanislav Fomichev 
820092f0892SStanislav Fomichev 	*attach_type = parse_attach_type(*argv);
821092f0892SStanislav Fomichev 	if (*attach_type == __MAX_BPF_ATTACH_TYPE) {
822092f0892SStanislav Fomichev 		p_err("invalid attach/detach type");
823092f0892SStanislav Fomichev 		return -EINVAL;
824092f0892SStanislav Fomichev 	}
825092f0892SStanislav Fomichev 
826092f0892SStanislav Fomichev 	if (*attach_type == BPF_FLOW_DISSECTOR) {
827092f0892SStanislav Fomichev 		*mapfd = -1;
828092f0892SStanislav Fomichev 		return 0;
829092f0892SStanislav Fomichev 	}
830092f0892SStanislav Fomichev 
831092f0892SStanislav Fomichev 	NEXT_ARG();
832092f0892SStanislav Fomichev 	if (!REQ_ARGS(2))
833092f0892SStanislav Fomichev 		return -EINVAL;
834092f0892SStanislav Fomichev 
835092f0892SStanislav Fomichev 	*mapfd = map_parse_fd(&argc, &argv);
836092f0892SStanislav Fomichev 	if (*mapfd < 0)
837092f0892SStanislav Fomichev 		return *mapfd;
838092f0892SStanislav Fomichev 
839092f0892SStanislav Fomichev 	return 0;
840092f0892SStanislav Fomichev }
841092f0892SStanislav Fomichev 
842b7d3826cSJohn Fastabend static int do_attach(int argc, char **argv)
843b7d3826cSJohn Fastabend {
844b7d3826cSJohn Fastabend 	enum bpf_attach_type attach_type;
845092f0892SStanislav Fomichev 	int err, progfd;
846092f0892SStanislav Fomichev 	int mapfd;
847b7d3826cSJohn Fastabend 
848092f0892SStanislav Fomichev 	err = parse_attach_detach_args(argc, argv,
849092f0892SStanislav Fomichev 				       &progfd, &attach_type, &mapfd);
850092f0892SStanislav Fomichev 	if (err)
851092f0892SStanislav Fomichev 		return err;
852b7d3826cSJohn Fastabend 
853b7d3826cSJohn Fastabend 	err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
854b7d3826cSJohn Fastabend 	if (err) {
855b7d3826cSJohn Fastabend 		p_err("failed prog attach to map");
856b7d3826cSJohn Fastabend 		return -EINVAL;
857b7d3826cSJohn Fastabend 	}
858b7d3826cSJohn Fastabend 
859b7d3826cSJohn Fastabend 	if (json_output)
860b7d3826cSJohn Fastabend 		jsonw_null(json_wtr);
861b7d3826cSJohn Fastabend 	return 0;
862b7d3826cSJohn Fastabend }
863b7d3826cSJohn Fastabend 
864b7d3826cSJohn Fastabend static int do_detach(int argc, char **argv)
865b7d3826cSJohn Fastabend {
866b7d3826cSJohn Fastabend 	enum bpf_attach_type attach_type;
867092f0892SStanislav Fomichev 	int err, progfd;
868092f0892SStanislav Fomichev 	int mapfd;
869b7d3826cSJohn Fastabend 
870092f0892SStanislav Fomichev 	err = parse_attach_detach_args(argc, argv,
871092f0892SStanislav Fomichev 				       &progfd, &attach_type, &mapfd);
872092f0892SStanislav Fomichev 	if (err)
873092f0892SStanislav Fomichev 		return err;
874b7d3826cSJohn Fastabend 
875b7d3826cSJohn Fastabend 	err = bpf_prog_detach2(progfd, mapfd, attach_type);
876b7d3826cSJohn Fastabend 	if (err) {
877b7d3826cSJohn Fastabend 		p_err("failed prog detach from map");
878b7d3826cSJohn Fastabend 		return -EINVAL;
879b7d3826cSJohn Fastabend 	}
880b7d3826cSJohn Fastabend 
881b7d3826cSJohn Fastabend 	if (json_output)
882b7d3826cSJohn Fastabend 		jsonw_null(json_wtr);
883b7d3826cSJohn Fastabend 	return 0;
884b7d3826cSJohn Fastabend }
88577380998SStanislav Fomichev 
88677380998SStanislav Fomichev static int load_with_options(int argc, char **argv, bool first_prog_only)
88749a086c2SRoman Gushchin {
888c8406848SJakub Kicinski 	enum bpf_attach_type expected_attach_type;
889c8406848SJakub Kicinski 	struct bpf_object_open_attr attr = {
890ba6dd679SJakub Kicinski 		.prog_type	= BPF_PROG_TYPE_UNSPEC,
891ba6dd679SJakub Kicinski 	};
8923ff5a4dcSJakub Kicinski 	struct map_replace *map_replace = NULL;
89377380998SStanislav Fomichev 	struct bpf_program *prog = NULL, *pos;
8943ff5a4dcSJakub Kicinski 	unsigned int old_map_fds = 0;
8953767a94bSStanislav Fomichev 	const char *pinmaps = NULL;
89649a086c2SRoman Gushchin 	struct bpf_object *obj;
897c8406848SJakub Kicinski 	struct bpf_map *map;
898c8406848SJakub Kicinski 	const char *pinfile;
8993ff5a4dcSJakub Kicinski 	unsigned int i, j;
900c8406848SJakub Kicinski 	__u32 ifindex = 0;
9013ff5a4dcSJakub Kicinski 	int idx, err;
90249a086c2SRoman Gushchin 
9038d1fc3deSJakub Kicinski 	if (!REQ_ARGS(2))
9048d1fc3deSJakub Kicinski 		return -1;
905c8406848SJakub Kicinski 	attr.file = GET_ARG();
9068d1fc3deSJakub Kicinski 	pinfile = GET_ARG();
90749a086c2SRoman Gushchin 
908ba6dd679SJakub Kicinski 	while (argc) {
90949f2cba3SJakub Kicinski 		if (is_prefix(*argv, "type")) {
91049f2cba3SJakub Kicinski 			char *type;
91149f2cba3SJakub Kicinski 
91249f2cba3SJakub Kicinski 			NEXT_ARG();
91349f2cba3SJakub Kicinski 
91449f2cba3SJakub Kicinski 			if (attr.prog_type != BPF_PROG_TYPE_UNSPEC) {
91549f2cba3SJakub Kicinski 				p_err("program type already specified");
9163ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
91749f2cba3SJakub Kicinski 			}
91849f2cba3SJakub Kicinski 			if (!REQ_ARGS(1))
9193ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
92049f2cba3SJakub Kicinski 
92149f2cba3SJakub Kicinski 			/* Put a '/' at the end of type to appease libbpf */
92249f2cba3SJakub Kicinski 			type = malloc(strlen(*argv) + 2);
92349f2cba3SJakub Kicinski 			if (!type) {
92449f2cba3SJakub Kicinski 				p_err("mem alloc failed");
9253ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
92649f2cba3SJakub Kicinski 			}
92749f2cba3SJakub Kicinski 			*type = 0;
92849f2cba3SJakub Kicinski 			strcat(type, *argv);
92949f2cba3SJakub Kicinski 			strcat(type, "/");
93049f2cba3SJakub Kicinski 
93149f2cba3SJakub Kicinski 			err = libbpf_prog_type_by_name(type, &attr.prog_type,
932c8406848SJakub Kicinski 						       &expected_attach_type);
93349f2cba3SJakub Kicinski 			free(type);
93449f2cba3SJakub Kicinski 			if (err < 0) {
93549f2cba3SJakub Kicinski 				p_err("unknown program type '%s'", *argv);
9363ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
93749f2cba3SJakub Kicinski 			}
93849f2cba3SJakub Kicinski 			NEXT_ARG();
9393ff5a4dcSJakub Kicinski 		} else if (is_prefix(*argv, "map")) {
940dde7011aSJakub Kicinski 			void *new_map_replace;
9413ff5a4dcSJakub Kicinski 			char *endptr, *name;
9423ff5a4dcSJakub Kicinski 			int fd;
9433ff5a4dcSJakub Kicinski 
9443ff5a4dcSJakub Kicinski 			NEXT_ARG();
9453ff5a4dcSJakub Kicinski 
9463ff5a4dcSJakub Kicinski 			if (!REQ_ARGS(4))
9473ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
9483ff5a4dcSJakub Kicinski 
9493ff5a4dcSJakub Kicinski 			if (is_prefix(*argv, "idx")) {
9503ff5a4dcSJakub Kicinski 				NEXT_ARG();
9513ff5a4dcSJakub Kicinski 
9523ff5a4dcSJakub Kicinski 				idx = strtoul(*argv, &endptr, 0);
9533ff5a4dcSJakub Kicinski 				if (*endptr) {
9543ff5a4dcSJakub Kicinski 					p_err("can't parse %s as IDX", *argv);
9553ff5a4dcSJakub Kicinski 					goto err_free_reuse_maps;
9563ff5a4dcSJakub Kicinski 				}
9573ff5a4dcSJakub Kicinski 				name = NULL;
9583ff5a4dcSJakub Kicinski 			} else if (is_prefix(*argv, "name")) {
9593ff5a4dcSJakub Kicinski 				NEXT_ARG();
9603ff5a4dcSJakub Kicinski 
9613ff5a4dcSJakub Kicinski 				name = *argv;
9623ff5a4dcSJakub Kicinski 				idx = -1;
9633ff5a4dcSJakub Kicinski 			} else {
9643ff5a4dcSJakub Kicinski 				p_err("expected 'idx' or 'name', got: '%s'?",
9653ff5a4dcSJakub Kicinski 				      *argv);
9663ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
9673ff5a4dcSJakub Kicinski 			}
9683ff5a4dcSJakub Kicinski 			NEXT_ARG();
9693ff5a4dcSJakub Kicinski 
9703ff5a4dcSJakub Kicinski 			fd = map_parse_fd(&argc, &argv);
9713ff5a4dcSJakub Kicinski 			if (fd < 0)
9723ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
9733ff5a4dcSJakub Kicinski 
974dde7011aSJakub Kicinski 			new_map_replace = reallocarray(map_replace,
975dde7011aSJakub Kicinski 						       old_map_fds + 1,
9763ff5a4dcSJakub Kicinski 						       sizeof(*map_replace));
977dde7011aSJakub Kicinski 			if (!new_map_replace) {
9783ff5a4dcSJakub Kicinski 				p_err("mem alloc failed");
9793ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
9803ff5a4dcSJakub Kicinski 			}
981dde7011aSJakub Kicinski 			map_replace = new_map_replace;
982dde7011aSJakub Kicinski 
9833ff5a4dcSJakub Kicinski 			map_replace[old_map_fds].idx = idx;
9843ff5a4dcSJakub Kicinski 			map_replace[old_map_fds].name = name;
9853ff5a4dcSJakub Kicinski 			map_replace[old_map_fds].fd = fd;
9863ff5a4dcSJakub Kicinski 			old_map_fds++;
98749f2cba3SJakub Kicinski 		} else if (is_prefix(*argv, "dev")) {
988ba6dd679SJakub Kicinski 			NEXT_ARG();
989ba6dd679SJakub Kicinski 
990c8406848SJakub Kicinski 			if (ifindex) {
991ba6dd679SJakub Kicinski 				p_err("offload device already specified");
9923ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
993ba6dd679SJakub Kicinski 			}
994ba6dd679SJakub Kicinski 			if (!REQ_ARGS(1))
9953ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
996ba6dd679SJakub Kicinski 
997c8406848SJakub Kicinski 			ifindex = if_nametoindex(*argv);
998c8406848SJakub Kicinski 			if (!ifindex) {
999ba6dd679SJakub Kicinski 				p_err("unrecognized netdevice '%s': %s",
1000ba6dd679SJakub Kicinski 				      *argv, strerror(errno));
10013ff5a4dcSJakub Kicinski 				goto err_free_reuse_maps;
1002ba6dd679SJakub Kicinski 			}
1003ba6dd679SJakub Kicinski 			NEXT_ARG();
10043767a94bSStanislav Fomichev 		} else if (is_prefix(*argv, "pinmaps")) {
10053767a94bSStanislav Fomichev 			NEXT_ARG();
10063767a94bSStanislav Fomichev 
10073767a94bSStanislav Fomichev 			if (!REQ_ARGS(1))
10083767a94bSStanislav Fomichev 				goto err_free_reuse_maps;
10093767a94bSStanislav Fomichev 
10103767a94bSStanislav Fomichev 			pinmaps = GET_ARG();
1011ba6dd679SJakub Kicinski 		} else {
10123ff5a4dcSJakub Kicinski 			p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
1013ba6dd679SJakub Kicinski 			      *argv);
10143ff5a4dcSJakub Kicinski 			goto err_free_reuse_maps;
1015ba6dd679SJakub Kicinski 		}
1016ba6dd679SJakub Kicinski 	}
1017ba6dd679SJakub Kicinski 
1018c034a177SJohn Fastabend 	obj = __bpf_object__open_xattr(&attr, bpf_flags);
1019c8406848SJakub Kicinski 	if (IS_ERR_OR_NULL(obj)) {
1020c8406848SJakub Kicinski 		p_err("failed to open object file");
10213ff5a4dcSJakub Kicinski 		goto err_free_reuse_maps;
102249a086c2SRoman Gushchin 	}
102349a086c2SRoman Gushchin 
102477380998SStanislav Fomichev 	bpf_object__for_each_program(pos, obj) {
102577380998SStanislav Fomichev 		enum bpf_prog_type prog_type = attr.prog_type;
1026c8406848SJakub Kicinski 
1027c8406848SJakub Kicinski 		if (attr.prog_type == BPF_PROG_TYPE_UNSPEC) {
102877380998SStanislav Fomichev 			const char *sec_name = bpf_program__title(pos, false);
1029c8406848SJakub Kicinski 
103077380998SStanislav Fomichev 			err = libbpf_prog_type_by_name(sec_name, &prog_type,
1031c8406848SJakub Kicinski 						       &expected_attach_type);
1032c8406848SJakub Kicinski 			if (err < 0) {
1033c8406848SJakub Kicinski 				p_err("failed to guess program type based on section name %s\n",
1034c8406848SJakub Kicinski 				      sec_name);
1035c8406848SJakub Kicinski 				goto err_close_obj;
1036c8406848SJakub Kicinski 			}
1037c8406848SJakub Kicinski 		}
103877380998SStanislav Fomichev 
103977380998SStanislav Fomichev 		bpf_program__set_ifindex(pos, ifindex);
104077380998SStanislav Fomichev 		bpf_program__set_type(pos, prog_type);
104177380998SStanislav Fomichev 		bpf_program__set_expected_attach_type(pos, expected_attach_type);
104277380998SStanislav Fomichev 	}
1043c8406848SJakub Kicinski 
10443ff5a4dcSJakub Kicinski 	qsort(map_replace, old_map_fds, sizeof(*map_replace),
10453ff5a4dcSJakub Kicinski 	      map_replace_compar);
10463ff5a4dcSJakub Kicinski 
10473ff5a4dcSJakub Kicinski 	/* After the sort maps by name will be first on the list, because they
10483ff5a4dcSJakub Kicinski 	 * have idx == -1.  Resolve them.
10493ff5a4dcSJakub Kicinski 	 */
10503ff5a4dcSJakub Kicinski 	j = 0;
10513ff5a4dcSJakub Kicinski 	while (j < old_map_fds && map_replace[j].name) {
10523ff5a4dcSJakub Kicinski 		i = 0;
10533ff5a4dcSJakub Kicinski 		bpf_map__for_each(map, obj) {
10543ff5a4dcSJakub Kicinski 			if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
10553ff5a4dcSJakub Kicinski 				map_replace[j].idx = i;
10563ff5a4dcSJakub Kicinski 				break;
10573ff5a4dcSJakub Kicinski 			}
10583ff5a4dcSJakub Kicinski 			i++;
10593ff5a4dcSJakub Kicinski 		}
10603ff5a4dcSJakub Kicinski 		if (map_replace[j].idx == -1) {
10613ff5a4dcSJakub Kicinski 			p_err("unable to find map '%s'", map_replace[j].name);
10623ff5a4dcSJakub Kicinski 			goto err_close_obj;
10633ff5a4dcSJakub Kicinski 		}
10643ff5a4dcSJakub Kicinski 		j++;
10653ff5a4dcSJakub Kicinski 	}
10663ff5a4dcSJakub Kicinski 	/* Resort if any names were resolved */
10673ff5a4dcSJakub Kicinski 	if (j)
10683ff5a4dcSJakub Kicinski 		qsort(map_replace, old_map_fds, sizeof(*map_replace),
10693ff5a4dcSJakub Kicinski 		      map_replace_compar);
10703ff5a4dcSJakub Kicinski 
10713ff5a4dcSJakub Kicinski 	/* Set ifindex and name reuse */
10723ff5a4dcSJakub Kicinski 	j = 0;
10733ff5a4dcSJakub Kicinski 	idx = 0;
10743ff5a4dcSJakub Kicinski 	bpf_map__for_each(map, obj) {
1075c8406848SJakub Kicinski 		if (!bpf_map__is_offload_neutral(map))
1076c8406848SJakub Kicinski 			bpf_map__set_ifindex(map, ifindex);
1077c8406848SJakub Kicinski 
10783ff5a4dcSJakub Kicinski 		if (j < old_map_fds && idx == map_replace[j].idx) {
10793ff5a4dcSJakub Kicinski 			err = bpf_map__reuse_fd(map, map_replace[j++].fd);
10803ff5a4dcSJakub Kicinski 			if (err) {
10813ff5a4dcSJakub Kicinski 				p_err("unable to set up map reuse: %d", err);
10823ff5a4dcSJakub Kicinski 				goto err_close_obj;
10833ff5a4dcSJakub Kicinski 			}
10843ff5a4dcSJakub Kicinski 
10853ff5a4dcSJakub Kicinski 			/* Next reuse wants to apply to the same map */
10863ff5a4dcSJakub Kicinski 			if (j < old_map_fds && map_replace[j].idx == idx) {
10873ff5a4dcSJakub Kicinski 				p_err("replacement for map idx %d specified more than once",
10883ff5a4dcSJakub Kicinski 				      idx);
10893ff5a4dcSJakub Kicinski 				goto err_close_obj;
10903ff5a4dcSJakub Kicinski 			}
10913ff5a4dcSJakub Kicinski 		}
10923ff5a4dcSJakub Kicinski 
10933ff5a4dcSJakub Kicinski 		idx++;
10943ff5a4dcSJakub Kicinski 	}
10953ff5a4dcSJakub Kicinski 	if (j < old_map_fds) {
10963ff5a4dcSJakub Kicinski 		p_err("map idx '%d' not used", map_replace[j].idx);
10973ff5a4dcSJakub Kicinski 		goto err_close_obj;
10983ff5a4dcSJakub Kicinski 	}
10993ff5a4dcSJakub Kicinski 
11008302b9bdSQuentin Monnet 	set_max_rlimit();
11018302b9bdSQuentin Monnet 
1102c8406848SJakub Kicinski 	err = bpf_object__load(obj);
1103c8406848SJakub Kicinski 	if (err) {
1104c8406848SJakub Kicinski 		p_err("failed to load object file");
1105c8406848SJakub Kicinski 		goto err_close_obj;
1106c8406848SJakub Kicinski 	}
1107c8406848SJakub Kicinski 
110877380998SStanislav Fomichev 	err = mount_bpffs_for_pin(pinfile);
110977380998SStanislav Fomichev 	if (err)
1110bfee71fbSJakub Kicinski 		goto err_close_obj;
111149a086c2SRoman Gushchin 
111277380998SStanislav Fomichev 	if (first_prog_only) {
111377380998SStanislav Fomichev 		prog = bpf_program__next(NULL, obj);
111477380998SStanislav Fomichev 		if (!prog) {
111577380998SStanislav Fomichev 			p_err("object file doesn't contain any bpf program");
111677380998SStanislav Fomichev 			goto err_close_obj;
111777380998SStanislav Fomichev 		}
111877380998SStanislav Fomichev 
111977380998SStanislav Fomichev 		err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
112077380998SStanislav Fomichev 		if (err) {
112177380998SStanislav Fomichev 			p_err("failed to pin program %s",
112277380998SStanislav Fomichev 			      bpf_program__title(prog, false));
112377380998SStanislav Fomichev 			goto err_close_obj;
112477380998SStanislav Fomichev 		}
112577380998SStanislav Fomichev 	} else {
112677380998SStanislav Fomichev 		err = bpf_object__pin_programs(obj, pinfile);
112777380998SStanislav Fomichev 		if (err) {
112877380998SStanislav Fomichev 			p_err("failed to pin all programs");
112977380998SStanislav Fomichev 			goto err_close_obj;
113077380998SStanislav Fomichev 		}
113177380998SStanislav Fomichev 	}
113277380998SStanislav Fomichev 
11333767a94bSStanislav Fomichev 	if (pinmaps) {
11343767a94bSStanislav Fomichev 		err = bpf_object__pin_maps(obj, pinmaps);
11353767a94bSStanislav Fomichev 		if (err) {
11363767a94bSStanislav Fomichev 			p_err("failed to pin all maps");
11373767a94bSStanislav Fomichev 			goto err_unpin;
11383767a94bSStanislav Fomichev 		}
11393767a94bSStanislav Fomichev 	}
11403767a94bSStanislav Fomichev 
114149a086c2SRoman Gushchin 	if (json_output)
114249a086c2SRoman Gushchin 		jsonw_null(json_wtr);
114349a086c2SRoman Gushchin 
1144bfee71fbSJakub Kicinski 	bpf_object__close(obj);
11453ff5a4dcSJakub Kicinski 	for (i = 0; i < old_map_fds; i++)
11463ff5a4dcSJakub Kicinski 		close(map_replace[i].fd);
11473ff5a4dcSJakub Kicinski 	free(map_replace);
1148bfee71fbSJakub Kicinski 
114949a086c2SRoman Gushchin 	return 0;
1150bfee71fbSJakub Kicinski 
11513767a94bSStanislav Fomichev err_unpin:
11523767a94bSStanislav Fomichev 	if (first_prog_only)
11533767a94bSStanislav Fomichev 		unlink(pinfile);
11543767a94bSStanislav Fomichev 	else
11553767a94bSStanislav Fomichev 		bpf_object__unpin_programs(obj, pinfile);
1156bfee71fbSJakub Kicinski err_close_obj:
1157bfee71fbSJakub Kicinski 	bpf_object__close(obj);
11583ff5a4dcSJakub Kicinski err_free_reuse_maps:
11593ff5a4dcSJakub Kicinski 	for (i = 0; i < old_map_fds; i++)
11603ff5a4dcSJakub Kicinski 		close(map_replace[i].fd);
11613ff5a4dcSJakub Kicinski 	free(map_replace);
1162bfee71fbSJakub Kicinski 	return -1;
116349a086c2SRoman Gushchin }
116449a086c2SRoman Gushchin 
116577380998SStanislav Fomichev static int do_load(int argc, char **argv)
116677380998SStanislav Fomichev {
116777380998SStanislav Fomichev 	return load_with_options(argc, argv, true);
116877380998SStanislav Fomichev }
116977380998SStanislav Fomichev 
117077380998SStanislav Fomichev static int do_loadall(int argc, char **argv)
117177380998SStanislav Fomichev {
117277380998SStanislav Fomichev 	return load_with_options(argc, argv, false);
117377380998SStanislav Fomichev }
117477380998SStanislav Fomichev 
117571bb428fSJakub Kicinski static int do_help(int argc, char **argv)
117671bb428fSJakub Kicinski {
1177004b45c0SQuentin Monnet 	if (json_output) {
1178004b45c0SQuentin Monnet 		jsonw_null(json_wtr);
1179004b45c0SQuentin Monnet 		return 0;
1180004b45c0SQuentin Monnet 	}
1181004b45c0SQuentin Monnet 
118271bb428fSJakub Kicinski 	fprintf(stderr,
11836ebe6dbdSJakub Kicinski 		"Usage: %s %s { show | list } [PROG]\n"
1184b053b439SMartin KaFai Lau 		"       %s %s dump xlated PROG [{ file FILE | opcodes | visual | linum }]\n"
1185b053b439SMartin KaFai Lau 		"       %s %s dump jited  PROG [{ file FILE | opcodes | linum }]\n"
118671bb428fSJakub Kicinski 		"       %s %s pin   PROG FILE\n"
118777380998SStanislav Fomichev 		"       %s %s { load | loadall } OBJ  PATH \\\n"
118877380998SStanislav Fomichev 		"                         [type TYPE] [dev NAME] \\\n"
11893767a94bSStanislav Fomichev 		"                         [map { idx IDX | name NAME } MAP]\\\n"
11903767a94bSStanislav Fomichev 		"                         [pinmaps MAP_DIR]\n"
1191092f0892SStanislav Fomichev 		"       %s %s attach PROG ATTACH_TYPE [MAP]\n"
1192092f0892SStanislav Fomichev 		"       %s %s detach PROG ATTACH_TYPE [MAP]\n"
119330da46b5SQuentin Monnet 		"       %s %s tracelog\n"
119471bb428fSJakub Kicinski 		"       %s %s help\n"
119571bb428fSJakub Kicinski 		"\n"
11963ff5a4dcSJakub Kicinski 		"       " HELP_SPEC_MAP "\n"
119771bb428fSJakub Kicinski 		"       " HELP_SPEC_PROGRAM "\n"
119849f2cba3SJakub Kicinski 		"       TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
119949f2cba3SJakub Kicinski 		"                 tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
120049f2cba3SJakub Kicinski 		"                 cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
120149f2cba3SJakub Kicinski 		"                 lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
120299a44befSQuentin Monnet 		"                 sk_reuseport | flow_dissector |\n"
120349f2cba3SJakub Kicinski 		"                 cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
120449f2cba3SJakub Kicinski 		"                 cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
120549f2cba3SJakub Kicinski 		"                 cgroup/sendmsg4 | cgroup/sendmsg6 }\n"
1206092f0892SStanislav Fomichev 		"       ATTACH_TYPE := { msg_verdict | skb_verdict | skb_parse |\n"
1207092f0892SStanislav Fomichev 		"                        flow_dissector }\n"
12080641c3c8SQuentin Monnet 		"       " HELP_SPEC_OPTIONS "\n"
120971bb428fSJakub Kicinski 		"",
121071bb428fSJakub Kicinski 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1211b7d3826cSJohn Fastabend 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
121230da46b5SQuentin Monnet 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
121371bb428fSJakub Kicinski 
121471bb428fSJakub Kicinski 	return 0;
121571bb428fSJakub Kicinski }
121671bb428fSJakub Kicinski 
121771bb428fSJakub Kicinski static const struct cmd cmds[] = {
121871bb428fSJakub Kicinski 	{ "show",	do_show },
12196ebe6dbdSJakub Kicinski 	{ "list",	do_show },
12209f606179SQuentin Monnet 	{ "help",	do_help },
122171bb428fSJakub Kicinski 	{ "dump",	do_dump },
122271bb428fSJakub Kicinski 	{ "pin",	do_pin },
122349a086c2SRoman Gushchin 	{ "load",	do_load },
122477380998SStanislav Fomichev 	{ "loadall",	do_loadall },
1225b7d3826cSJohn Fastabend 	{ "attach",	do_attach },
1226b7d3826cSJohn Fastabend 	{ "detach",	do_detach },
122730da46b5SQuentin Monnet 	{ "tracelog",	do_tracelog },
122871bb428fSJakub Kicinski 	{ 0 }
122971bb428fSJakub Kicinski };
123071bb428fSJakub Kicinski 
123171bb428fSJakub Kicinski int do_prog(int argc, char **argv)
123271bb428fSJakub Kicinski {
123371bb428fSJakub Kicinski 	return cmd_select(cmds, argc, argv, do_help);
123471bb428fSJakub Kicinski }
1235