Linux Audio

Check our new training course

Loading...
v5.14.15
   1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
   2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
   3
   4#define _GNU_SOURCE
   5#include <errno.h>
   6#include <fcntl.h>
   7#include <signal.h>
   8#include <stdarg.h>
   9#include <stdio.h>
  10#include <stdlib.h>
  11#include <string.h>
  12#include <time.h>
  13#include <unistd.h>
  14#include <net/if.h>
  15#include <sys/ioctl.h>
  16#include <sys/types.h>
  17#include <sys/stat.h>
  18#include <sys/syscall.h>
  19#include <dirent.h>
  20
  21#include <linux/err.h>
  22#include <linux/perf_event.h>
  23#include <linux/sizes.h>
  24
  25#include <bpf/bpf.h>
  26#include <bpf/btf.h>
  27#include <bpf/libbpf.h>
  28#include <bpf/bpf_gen_internal.h>
  29#include <bpf/skel_internal.h>
  30
  31#include "cfg.h"
  32#include "main.h"
  33#include "xlated_dumper.h"
  34
  35#define BPF_METADATA_PREFIX "bpf_metadata_"
  36#define BPF_METADATA_PREFIX_LEN (sizeof(BPF_METADATA_PREFIX) - 1)
  37
  38const char * const prog_type_name[] = {
  39	[BPF_PROG_TYPE_UNSPEC]			= "unspec",
  40	[BPF_PROG_TYPE_SOCKET_FILTER]		= "socket_filter",
  41	[BPF_PROG_TYPE_KPROBE]			= "kprobe",
  42	[BPF_PROG_TYPE_SCHED_CLS]		= "sched_cls",
  43	[BPF_PROG_TYPE_SCHED_ACT]		= "sched_act",
  44	[BPF_PROG_TYPE_TRACEPOINT]		= "tracepoint",
  45	[BPF_PROG_TYPE_XDP]			= "xdp",
  46	[BPF_PROG_TYPE_PERF_EVENT]		= "perf_event",
  47	[BPF_PROG_TYPE_CGROUP_SKB]		= "cgroup_skb",
  48	[BPF_PROG_TYPE_CGROUP_SOCK]		= "cgroup_sock",
  49	[BPF_PROG_TYPE_LWT_IN]			= "lwt_in",
  50	[BPF_PROG_TYPE_LWT_OUT]			= "lwt_out",
  51	[BPF_PROG_TYPE_LWT_XMIT]		= "lwt_xmit",
  52	[BPF_PROG_TYPE_SOCK_OPS]		= "sock_ops",
  53	[BPF_PROG_TYPE_SK_SKB]			= "sk_skb",
  54	[BPF_PROG_TYPE_CGROUP_DEVICE]		= "cgroup_device",
  55	[BPF_PROG_TYPE_SK_MSG]			= "sk_msg",
  56	[BPF_PROG_TYPE_RAW_TRACEPOINT]		= "raw_tracepoint",
  57	[BPF_PROG_TYPE_CGROUP_SOCK_ADDR]	= "cgroup_sock_addr",
  58	[BPF_PROG_TYPE_LWT_SEG6LOCAL]		= "lwt_seg6local",
  59	[BPF_PROG_TYPE_LIRC_MODE2]		= "lirc_mode2",
  60	[BPF_PROG_TYPE_SK_REUSEPORT]		= "sk_reuseport",
  61	[BPF_PROG_TYPE_FLOW_DISSECTOR]		= "flow_dissector",
  62	[BPF_PROG_TYPE_CGROUP_SYSCTL]		= "cgroup_sysctl",
  63	[BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE]	= "raw_tracepoint_writable",
  64	[BPF_PROG_TYPE_CGROUP_SOCKOPT]		= "cgroup_sockopt",
  65	[BPF_PROG_TYPE_TRACING]			= "tracing",
  66	[BPF_PROG_TYPE_STRUCT_OPS]		= "struct_ops",
  67	[BPF_PROG_TYPE_EXT]			= "ext",
  68	[BPF_PROG_TYPE_LSM]			= "lsm",
  69	[BPF_PROG_TYPE_SK_LOOKUP]		= "sk_lookup",
  70};
  71
  72const size_t prog_type_name_size = ARRAY_SIZE(prog_type_name);
  73
  74enum dump_mode {
  75	DUMP_JITED,
  76	DUMP_XLATED,
  77};
  78
  79static const char * const attach_type_strings[] = {
  80	[BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
  81	[BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
  82	[BPF_SK_SKB_VERDICT] = "skb_verdict",
  83	[BPF_SK_MSG_VERDICT] = "msg_verdict",
  84	[BPF_FLOW_DISSECTOR] = "flow_dissector",
  85	[__MAX_BPF_ATTACH_TYPE] = NULL,
  86};
  87
  88static enum bpf_attach_type parse_attach_type(const char *str)
  89{
  90	enum bpf_attach_type type;
  91
  92	for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
  93		if (attach_type_strings[type] &&
  94		    is_prefix(str, attach_type_strings[type]))
  95			return type;
  96	}
  97
  98	return __MAX_BPF_ATTACH_TYPE;
  99}
 100
 101static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
 102{
 103	struct timespec real_time_ts, boot_time_ts;
 104	time_t wallclock_secs;
 105	struct tm load_tm;
 106
 107	buf[--size] = '\0';
 108
 109	if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
 110	    clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
 111		perror("Can't read clocks");
 112		snprintf(buf, size, "%llu", nsecs / 1000000000);
 113		return;
 114	}
 115
 116	wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
 117		(real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
 118		1000000000;
 119
 120
 121	if (!localtime_r(&wallclock_secs, &load_tm)) {
 122		snprintf(buf, size, "%llu", nsecs / 1000000000);
 123		return;
 124	}
 125
 126	if (json_output)
 127		strftime(buf, size, "%s", &load_tm);
 128	else
 129		strftime(buf, size, "%FT%T%z", &load_tm);
 130}
 131
 132static void show_prog_maps(int fd, __u32 num_maps)
 133{
 134	struct bpf_prog_info info = {};
 135	__u32 len = sizeof(info);
 136	__u32 map_ids[num_maps];
 137	unsigned int i;
 138	int err;
 
 139
 140	info.nr_map_ids = num_maps;
 141	info.map_ids = ptr_to_u64(map_ids);
 142
 143	err = bpf_obj_get_info_by_fd(fd, &info, &len);
 144	if (err || !info.nr_map_ids)
 145		return;
 146
 147	if (json_output) {
 148		jsonw_name(json_wtr, "map_ids");
 149		jsonw_start_array(json_wtr);
 150		for (i = 0; i < info.nr_map_ids; i++)
 151			jsonw_uint(json_wtr, map_ids[i]);
 152		jsonw_end_array(json_wtr);
 153	} else {
 154		printf("  map_ids ");
 155		for (i = 0; i < info.nr_map_ids; i++)
 156			printf("%u%s", map_ids[i],
 157			       i == info.nr_map_ids - 1 ? "" : ",");
 158	}
 159}
 160
 161static void *find_metadata(int prog_fd, struct bpf_map_info *map_info)
 162{
 163	struct bpf_prog_info prog_info;
 164	__u32 prog_info_len;
 165	__u32 map_info_len;
 166	void *value = NULL;
 167	__u32 *map_ids;
 168	int nr_maps;
 169	int key = 0;
 170	int map_fd;
 171	int ret;
 172	__u32 i;
 173
 174	memset(&prog_info, 0, sizeof(prog_info));
 175	prog_info_len = sizeof(prog_info);
 176	ret = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
 177	if (ret)
 178		return NULL;
 179
 180	if (!prog_info.nr_map_ids)
 181		return NULL;
 
 
 
 
 182
 183	map_ids = calloc(prog_info.nr_map_ids, sizeof(__u32));
 184	if (!map_ids)
 185		return NULL;
 
 
 
 
 186
 187	nr_maps = prog_info.nr_map_ids;
 188	memset(&prog_info, 0, sizeof(prog_info));
 189	prog_info.nr_map_ids = nr_maps;
 190	prog_info.map_ids = ptr_to_u64(map_ids);
 191	prog_info_len = sizeof(prog_info);
 192
 193	ret = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
 194	if (ret)
 195		goto free_map_ids;
 196
 197	for (i = 0; i < prog_info.nr_map_ids; i++) {
 198		map_fd = bpf_map_get_fd_by_id(map_ids[i]);
 199		if (map_fd < 0)
 200			goto free_map_ids;
 201
 202		memset(map_info, 0, sizeof(*map_info));
 203		map_info_len = sizeof(*map_info);
 204		ret = bpf_obj_get_info_by_fd(map_fd, map_info, &map_info_len);
 205		if (ret < 0) {
 206			close(map_fd);
 207			goto free_map_ids;
 
 
 
 
 208		}
 
 209
 210		if (map_info->type != BPF_MAP_TYPE_ARRAY ||
 211		    map_info->key_size != sizeof(int) ||
 212		    map_info->max_entries != 1 ||
 213		    !map_info->btf_value_type_id ||
 214		    !strstr(map_info->name, ".rodata")) {
 215			close(map_fd);
 216			continue;
 
 
 
 
 
 
 
 217		}
 
 218
 219		value = malloc(map_info->value_size);
 220		if (!value) {
 221			close(map_fd);
 222			goto free_map_ids;
 223		}
 224
 225		if (bpf_map_lookup_elem(map_fd, &key, value)) {
 226			close(map_fd);
 227			free(value);
 228			value = NULL;
 229			goto free_map_ids;
 230		}
 231
 232		close(map_fd);
 233		break;
 234	}
 235
 236free_map_ids:
 237	free(map_ids);
 238	return value;
 239}
 240
 241static bool has_metadata_prefix(const char *s)
 242{
 243	return strncmp(s, BPF_METADATA_PREFIX, BPF_METADATA_PREFIX_LEN) == 0;
 244}
 245
 246static void show_prog_metadata(int fd, __u32 num_maps)
 247{
 248	const struct btf_type *t_datasec, *t_var;
 249	struct bpf_map_info map_info;
 250	struct btf_var_secinfo *vsi;
 251	bool printed_header = false;
 252	struct btf *btf = NULL;
 253	unsigned int i, vlen;
 254	void *value = NULL;
 255	const char *name;
 256	int err;
 257
 258	if (!num_maps)
 259		return;
 260
 261	memset(&map_info, 0, sizeof(map_info));
 262	value = find_metadata(fd, &map_info);
 263	if (!value)
 264		return;
 265
 266	err = btf__get_from_id(map_info.btf_id, &btf);
 267	if (err || !btf)
 268		goto out_free;
 269
 270	t_datasec = btf__type_by_id(btf, map_info.btf_value_type_id);
 271	if (!btf_is_datasec(t_datasec))
 272		goto out_free;
 273
 274	vlen = btf_vlen(t_datasec);
 275	vsi = btf_var_secinfos(t_datasec);
 276
 277	/* We don't proceed to check the kinds of the elements of the DATASEC.
 278	 * The verifier enforces them to be BTF_KIND_VAR.
 279	 */
 280
 281	if (json_output) {
 282		struct btf_dumper d = {
 283			.btf = btf,
 284			.jw = json_wtr,
 285			.is_plain_text = false,
 286		};
 287
 288		for (i = 0; i < vlen; i++, vsi++) {
 289			t_var = btf__type_by_id(btf, vsi->type);
 290			name = btf__name_by_offset(btf, t_var->name_off);
 291
 292			if (!has_metadata_prefix(name))
 293				continue;
 294
 295			if (!printed_header) {
 296				jsonw_name(json_wtr, "metadata");
 297				jsonw_start_object(json_wtr);
 298				printed_header = true;
 299			}
 300
 301			jsonw_name(json_wtr, name + BPF_METADATA_PREFIX_LEN);
 302			err = btf_dumper_type(&d, t_var->type, value + vsi->offset);
 303			if (err) {
 304				p_err("btf dump failed: %d", err);
 305				break;
 306			}
 307		}
 308		if (printed_header)
 309			jsonw_end_object(json_wtr);
 310	} else {
 311		json_writer_t *btf_wtr = jsonw_new(stdout);
 312		struct btf_dumper d = {
 313			.btf = btf,
 314			.jw = btf_wtr,
 315			.is_plain_text = true,
 316		};
 317
 318		if (!btf_wtr) {
 319			p_err("jsonw alloc failed");
 320			goto out_free;
 321		}
 322
 323		for (i = 0; i < vlen; i++, vsi++) {
 324			t_var = btf__type_by_id(btf, vsi->type);
 325			name = btf__name_by_offset(btf, t_var->name_off);
 326
 327			if (!has_metadata_prefix(name))
 328				continue;
 329
 330			if (!printed_header) {
 331				printf("\tmetadata:");
 332				printed_header = true;
 333			}
 334
 335			printf("\n\t\t%s = ", name + BPF_METADATA_PREFIX_LEN);
 336
 337			jsonw_reset(btf_wtr);
 338			err = btf_dumper_type(&d, t_var->type, value + vsi->offset);
 339			if (err) {
 340				p_err("btf dump failed: %d", err);
 341				break;
 342			}
 343		}
 344		if (printed_header)
 345			jsonw_destroy(&btf_wtr);
 346	}
 347
 348out_free:
 349	btf__free(btf);
 350	free(value);
 351}
 352
 353static void print_prog_header_json(struct bpf_prog_info *info)
 354{
 
 
 
 355	jsonw_uint_field(json_wtr, "id", info->id);
 356	if (info->type < ARRAY_SIZE(prog_type_name))
 357		jsonw_string_field(json_wtr, "type",
 358				   prog_type_name[info->type]);
 359	else
 360		jsonw_uint_field(json_wtr, "type", info->type);
 361
 362	if (*info->name)
 363		jsonw_string_field(json_wtr, "name", info->name);
 364
 365	jsonw_name(json_wtr, "tag");
 366	jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
 367		     info->tag[0], info->tag[1], info->tag[2], info->tag[3],
 368		     info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
 369
 370	jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
 371	if (info->run_time_ns) {
 372		jsonw_uint_field(json_wtr, "run_time_ns", info->run_time_ns);
 373		jsonw_uint_field(json_wtr, "run_cnt", info->run_cnt);
 374	}
 375	if (info->recursion_misses)
 376		jsonw_uint_field(json_wtr, "recursion_misses", info->recursion_misses);
 377}
 378
 379static void print_prog_json(struct bpf_prog_info *info, int fd)
 380{
 381	char *memlock;
 382
 383	jsonw_start_object(json_wtr);
 384	print_prog_header_json(info);
 385	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
 386
 387	if (info->load_time) {
 388		char buf[32];
 389
 390		print_boot_time(info->load_time, buf, sizeof(buf));
 391
 392		/* Piggy back on load_time, since 0 uid is a valid one */
 393		jsonw_name(json_wtr, "loaded_at");
 394		jsonw_printf(json_wtr, "%s", buf);
 395		jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
 396	}
 397
 398	jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
 399
 400	if (info->jited_prog_len) {
 401		jsonw_bool_field(json_wtr, "jited", true);
 402		jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
 403	} else {
 404		jsonw_bool_field(json_wtr, "jited", false);
 405	}
 406
 407	memlock = get_fdinfo(fd, "memlock");
 408	if (memlock)
 409		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
 410	free(memlock);
 411
 412	if (info->nr_map_ids)
 413		show_prog_maps(fd, info->nr_map_ids);
 414
 415	if (info->btf_id)
 416		jsonw_int_field(json_wtr, "btf_id", info->btf_id);
 417
 418	if (!hash_empty(prog_table.table)) {
 419		struct pinned_obj *obj;
 420
 421		jsonw_name(json_wtr, "pinned");
 422		jsonw_start_array(json_wtr);
 423		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
 424			if (obj->id == info->id)
 425				jsonw_string(json_wtr, obj->path);
 426		}
 427		jsonw_end_array(json_wtr);
 428	}
 429
 430	emit_obj_refs_json(&refs_table, info->id, json_wtr);
 431
 432	show_prog_metadata(fd, info->nr_map_ids);
 433
 434	jsonw_end_object(json_wtr);
 435}
 436
 437static void print_prog_header_plain(struct bpf_prog_info *info)
 438{
 
 
 439	printf("%u: ", info->id);
 440	if (info->type < ARRAY_SIZE(prog_type_name))
 441		printf("%s  ", prog_type_name[info->type]);
 442	else
 443		printf("type %u  ", info->type);
 444
 445	if (*info->name)
 446		printf("name %s  ", info->name);
 447
 448	printf("tag ");
 449	fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
 450	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
 451	printf("%s", info->gpl_compatible ? "  gpl" : "");
 452	if (info->run_time_ns)
 453		printf(" run_time_ns %lld run_cnt %lld",
 454		       info->run_time_ns, info->run_cnt);
 455	if (info->recursion_misses)
 456		printf(" recursion_misses %lld", info->recursion_misses);
 457	printf("\n");
 458}
 459
 460static void print_prog_plain(struct bpf_prog_info *info, int fd)
 461{
 462	char *memlock;
 463
 464	print_prog_header_plain(info);
 465
 466	if (info->load_time) {
 467		char buf[32];
 468
 469		print_boot_time(info->load_time, buf, sizeof(buf));
 470
 471		/* Piggy back on load_time, since 0 uid is a valid one */
 472		printf("\tloaded_at %s  uid %u\n", buf, info->created_by_uid);
 473	}
 474
 475	printf("\txlated %uB", info->xlated_prog_len);
 476
 477	if (info->jited_prog_len)
 478		printf("  jited %uB", info->jited_prog_len);
 479	else
 480		printf("  not jited");
 481
 482	memlock = get_fdinfo(fd, "memlock");
 483	if (memlock)
 484		printf("  memlock %sB", memlock);
 485	free(memlock);
 486
 487	if (info->nr_map_ids)
 488		show_prog_maps(fd, info->nr_map_ids);
 489
 490	if (!hash_empty(prog_table.table)) {
 491		struct pinned_obj *obj;
 492
 493		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
 494			if (obj->id == info->id)
 495				printf("\n\tpinned %s", obj->path);
 496		}
 497	}
 498
 499	if (info->btf_id)
 500		printf("\n\tbtf_id %d", info->btf_id);
 501
 502	emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
 503
 504	printf("\n");
 505
 506	show_prog_metadata(fd, info->nr_map_ids);
 507}
 508
 509static int show_prog(int fd)
 510{
 511	struct bpf_prog_info info = {};
 512	__u32 len = sizeof(info);
 513	int err;
 514
 515	err = bpf_obj_get_info_by_fd(fd, &info, &len);
 516	if (err) {
 517		p_err("can't get prog info: %s", strerror(errno));
 518		return -1;
 519	}
 520
 521	if (json_output)
 522		print_prog_json(&info, fd);
 523	else
 524		print_prog_plain(&info, fd);
 525
 526	return 0;
 527}
 528
 529static int do_show_subset(int argc, char **argv)
 530{
 531	int *fds = NULL;
 532	int nb_fds, i;
 533	int err = -1;
 534
 535	fds = malloc(sizeof(int));
 536	if (!fds) {
 537		p_err("mem alloc failed");
 538		return -1;
 539	}
 540	nb_fds = prog_parse_fds(&argc, &argv, &fds);
 541	if (nb_fds < 1)
 542		goto exit_free;
 543
 544	if (json_output && nb_fds > 1)
 545		jsonw_start_array(json_wtr);	/* root array */
 546	for (i = 0; i < nb_fds; i++) {
 547		err = show_prog(fds[i]);
 548		if (err) {
 549			for (; i < nb_fds; i++)
 550				close(fds[i]);
 551			break;
 552		}
 553		close(fds[i]);
 554	}
 555	if (json_output && nb_fds > 1)
 556		jsonw_end_array(json_wtr);	/* root array */
 557
 558exit_free:
 559	free(fds);
 560	return err;
 561}
 562
 563static int do_show(int argc, char **argv)
 564{
 565	__u32 id = 0;
 566	int err;
 567	int fd;
 568
 569	if (show_pinned)
 570		build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
 571	build_obj_refs_table(&refs_table, BPF_OBJ_PROG);
 572
 573	if (argc == 2)
 574		return do_show_subset(argc, argv);
 
 
 
 
 
 
 
 575
 576	if (argc)
 577		return BAD_ARG();
 578
 579	if (json_output)
 580		jsonw_start_array(json_wtr);
 581	while (true) {
 582		err = bpf_prog_get_next_id(id, &id);
 583		if (err) {
 584			if (errno == ENOENT) {
 585				err = 0;
 586				break;
 587			}
 588			p_err("can't get next program: %s%s", strerror(errno),
 589			      errno == EINVAL ? " -- kernel too old?" : "");
 590			err = -1;
 591			break;
 592		}
 593
 594		fd = bpf_prog_get_fd_by_id(id);
 595		if (fd < 0) {
 596			if (errno == ENOENT)
 597				continue;
 598			p_err("can't get prog by id (%u): %s",
 599			      id, strerror(errno));
 600			err = -1;
 601			break;
 602		}
 603
 604		err = show_prog(fd);
 605		close(fd);
 606		if (err)
 607			break;
 608	}
 609
 610	if (json_output)
 611		jsonw_end_array(json_wtr);
 612
 613	delete_obj_refs_table(&refs_table);
 614
 615	return err;
 616}
 617
 618static int
 619prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
 620	  char *filepath, bool opcodes, bool visual, bool linum)
 621{
 
 622	struct bpf_prog_linfo *prog_linfo = NULL;
 
 623	const char *disasm_opt = NULL;
 
 624	struct dump_data dd = {};
 625	void *func_info = NULL;
 626	struct btf *btf = NULL;
 
 
 
 627	char func_sig[1024];
 628	unsigned char *buf;
 
 629	__u32 member_len;
 
 630	ssize_t n;
 631	int fd;
 632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 633	if (mode == DUMP_JITED) {
 634		if (info->jited_prog_len == 0 || !info->jited_prog_insns) {
 635			p_info("no instructions returned");
 636			return -1;
 637		}
 638		buf = u64_to_ptr(info->jited_prog_insns);
 639		member_len = info->jited_prog_len;
 640	} else {	/* DUMP_XLATED */
 641		if (info->xlated_prog_len == 0 || !info->xlated_prog_insns) {
 642			p_err("error retrieving insn dump: kernel.kptr_restrict set?");
 643			return -1;
 644		}
 645		buf = u64_to_ptr(info->xlated_prog_insns);
 646		member_len = info->xlated_prog_len;
 647	}
 648
 649	if (info->btf_id && btf__get_from_id(info->btf_id, &btf)) {
 650		p_err("failed to get btf");
 651		return -1;
 652	}
 653
 654	func_info = u64_to_ptr(info->func_info);
 655
 656	if (info->nr_line_info) {
 657		prog_linfo = bpf_prog_linfo__new(info);
 658		if (!prog_linfo)
 659			p_info("error in processing bpf_line_info.  continue without it.");
 660	}
 661
 662	if (filepath) {
 663		fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
 664		if (fd < 0) {
 665			p_err("can't open file %s: %s", filepath,
 666			      strerror(errno));
 667			return -1;
 668		}
 669
 670		n = write(fd, buf, member_len);
 671		close(fd);
 672		if (n != (ssize_t)member_len) {
 673			p_err("error writing output file: %s",
 674			      n < 0 ? strerror(errno) : "short write");
 675			return -1;
 676		}
 677
 678		if (json_output)
 679			jsonw_null(json_wtr);
 680	} else if (mode == DUMP_JITED) {
 681		const char *name = NULL;
 682
 683		if (info->ifindex) {
 684			name = ifindex_to_bfd_params(info->ifindex,
 685						     info->netns_dev,
 686						     info->netns_ino,
 687						     &disasm_opt);
 688			if (!name)
 689				return -1;
 690		}
 691
 692		if (info->nr_jited_func_lens && info->jited_func_lens) {
 693			struct kernel_sym *sym = NULL;
 694			struct bpf_func_info *record;
 695			char sym_name[SYM_MAX_NAME];
 696			unsigned char *img = buf;
 697			__u64 *ksyms = NULL;
 698			__u32 *lens;
 699			__u32 i;
 700			if (info->nr_jited_ksyms) {
 701				kernel_syms_load(&dd);
 702				ksyms = u64_to_ptr(info->jited_ksyms);
 703			}
 704
 705			if (json_output)
 706				jsonw_start_array(json_wtr);
 707
 708			lens = u64_to_ptr(info->jited_func_lens);
 709			for (i = 0; i < info->nr_jited_func_lens; i++) {
 710				if (ksyms) {
 711					sym = kernel_syms_search(&dd, ksyms[i]);
 712					if (sym)
 713						sprintf(sym_name, "%s", sym->name);
 714					else
 715						sprintf(sym_name, "0x%016llx", ksyms[i]);
 716				} else {
 717					strcpy(sym_name, "unknown");
 718				}
 719
 720				if (func_info) {
 721					record = func_info + i * info->func_info_rec_size;
 722					btf_dumper_type_only(btf, record->type_id,
 723							     func_sig,
 724							     sizeof(func_sig));
 725				}
 726
 727				if (json_output) {
 728					jsonw_start_object(json_wtr);
 729					if (func_info && func_sig[0] != '\0') {
 730						jsonw_name(json_wtr, "proto");
 731						jsonw_string(json_wtr, func_sig);
 732					}
 733					jsonw_name(json_wtr, "name");
 734					jsonw_string(json_wtr, sym_name);
 735					jsonw_name(json_wtr, "insns");
 736				} else {
 737					if (func_info && func_sig[0] != '\0')
 738						printf("%s:\n", func_sig);
 739					printf("%s:\n", sym_name);
 740				}
 741
 742				disasm_print_insn(img, lens[i], opcodes,
 743						  name, disasm_opt, btf,
 744						  prog_linfo, ksyms[i], i,
 745						  linum);
 746
 747				img += lens[i];
 748
 749				if (json_output)
 750					jsonw_end_object(json_wtr);
 751				else
 752					printf("\n");
 753			}
 754
 755			if (json_output)
 756				jsonw_end_array(json_wtr);
 757		} else {
 758			disasm_print_insn(buf, member_len, opcodes, name,
 759					  disasm_opt, btf, NULL, 0, 0, false);
 760		}
 761	} else if (visual) {
 762		if (json_output)
 763			jsonw_null(json_wtr);
 764		else
 765			dump_xlated_cfg(buf, member_len);
 766	} else {
 767		kernel_syms_load(&dd);
 768		dd.nr_jited_ksyms = info->nr_jited_ksyms;
 769		dd.jited_ksyms = u64_to_ptr(info->jited_ksyms);
 770		dd.btf = btf;
 771		dd.func_info = func_info;
 772		dd.finfo_rec_size = info->func_info_rec_size;
 773		dd.prog_linfo = prog_linfo;
 774
 775		if (json_output)
 776			dump_xlated_json(&dd, buf, member_len, opcodes,
 777					 linum);
 778		else
 779			dump_xlated_plain(&dd, buf, member_len, opcodes,
 780					  linum);
 781		kernel_syms_destroy(&dd);
 782	}
 783
 784	btf__free(btf);
 785
 786	return 0;
 787}
 788
 789static int do_dump(int argc, char **argv)
 790{
 791	struct bpf_prog_info_linear *info_linear;
 792	char *filepath = NULL;
 793	bool opcodes = false;
 794	bool visual = false;
 795	enum dump_mode mode;
 796	bool linum = false;
 797	int *fds = NULL;
 798	int nb_fds, i = 0;
 799	int err = -1;
 800	__u64 arrays;
 801
 802	if (is_prefix(*argv, "jited")) {
 803		if (disasm_init())
 804			return -1;
 805		mode = DUMP_JITED;
 806	} else if (is_prefix(*argv, "xlated")) {
 807		mode = DUMP_XLATED;
 808	} else {
 809		p_err("expected 'xlated' or 'jited', got: %s", *argv);
 810		return -1;
 811	}
 812	NEXT_ARG();
 813
 814	if (argc < 2)
 815		usage();
 816
 817	fds = malloc(sizeof(int));
 818	if (!fds) {
 819		p_err("mem alloc failed");
 820		return -1;
 821	}
 822	nb_fds = prog_parse_fds(&argc, &argv, &fds);
 823	if (nb_fds < 1)
 824		goto exit_free;
 825
 826	if (is_prefix(*argv, "file")) {
 827		NEXT_ARG();
 828		if (!argc) {
 829			p_err("expected file path");
 830			goto exit_close;
 831		}
 832		if (nb_fds > 1) {
 833			p_err("several programs matched");
 834			goto exit_close;
 835		}
 836
 837		filepath = *argv;
 838		NEXT_ARG();
 839	} else if (is_prefix(*argv, "opcodes")) {
 840		opcodes = true;
 841		NEXT_ARG();
 842	} else if (is_prefix(*argv, "visual")) {
 843		if (nb_fds > 1) {
 844			p_err("several programs matched");
 845			goto exit_close;
 846		}
 847
 848		visual = true;
 849		NEXT_ARG();
 850	} else if (is_prefix(*argv, "linum")) {
 851		linum = true;
 852		NEXT_ARG();
 853	}
 854
 855	if (argc) {
 856		usage();
 857		goto exit_close;
 858	}
 859
 860	if (mode == DUMP_JITED)
 861		arrays = 1UL << BPF_PROG_INFO_JITED_INSNS;
 862	else
 863		arrays = 1UL << BPF_PROG_INFO_XLATED_INSNS;
 864
 865	arrays |= 1UL << BPF_PROG_INFO_JITED_KSYMS;
 866	arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
 867	arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
 868	arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
 869	arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
 870
 871	if (json_output && nb_fds > 1)
 872		jsonw_start_array(json_wtr);	/* root array */
 873	for (i = 0; i < nb_fds; i++) {
 874		info_linear = bpf_program__get_prog_info_linear(fds[i], arrays);
 875		if (IS_ERR_OR_NULL(info_linear)) {
 876			p_err("can't get prog info: %s", strerror(errno));
 877			break;
 878		}
 879
 880		if (json_output && nb_fds > 1) {
 881			jsonw_start_object(json_wtr);	/* prog object */
 882			print_prog_header_json(&info_linear->info);
 883			jsonw_name(json_wtr, "insns");
 884		} else if (nb_fds > 1) {
 885			print_prog_header_plain(&info_linear->info);
 886		}
 887
 888		err = prog_dump(&info_linear->info, mode, filepath, opcodes,
 889				visual, linum);
 890
 891		if (json_output && nb_fds > 1)
 892			jsonw_end_object(json_wtr);	/* prog object */
 893		else if (i != nb_fds - 1 && nb_fds > 1)
 894			printf("\n");
 895
 896		free(info_linear);
 897		if (err)
 898			break;
 899		close(fds[i]);
 900	}
 901	if (json_output && nb_fds > 1)
 902		jsonw_end_array(json_wtr);	/* root array */
 903
 904exit_close:
 905	for (; i < nb_fds; i++)
 906		close(fds[i]);
 907exit_free:
 908	free(fds);
 909	return err;
 910}
 911
 912static int do_pin(int argc, char **argv)
 913{
 914	int err;
 915
 916	err = do_pin_any(argc, argv, prog_parse_fd);
 917	if (!err && json_output)
 918		jsonw_null(json_wtr);
 919	return err;
 920}
 921
 922struct map_replace {
 923	int idx;
 924	int fd;
 925	char *name;
 926};
 927
 928static int map_replace_compar(const void *p1, const void *p2)
 929{
 930	const struct map_replace *a = p1, *b = p2;
 931
 932	return a->idx - b->idx;
 933}
 934
 935static int parse_attach_detach_args(int argc, char **argv, int *progfd,
 936				    enum bpf_attach_type *attach_type,
 937				    int *mapfd)
 938{
 939	if (!REQ_ARGS(3))
 940		return -EINVAL;
 941
 942	*progfd = prog_parse_fd(&argc, &argv);
 943	if (*progfd < 0)
 944		return *progfd;
 945
 946	*attach_type = parse_attach_type(*argv);
 947	if (*attach_type == __MAX_BPF_ATTACH_TYPE) {
 948		p_err("invalid attach/detach type");
 949		return -EINVAL;
 950	}
 951
 952	if (*attach_type == BPF_FLOW_DISSECTOR) {
 953		*mapfd = 0;
 954		return 0;
 955	}
 956
 957	NEXT_ARG();
 958	if (!REQ_ARGS(2))
 959		return -EINVAL;
 960
 961	*mapfd = map_parse_fd(&argc, &argv);
 962	if (*mapfd < 0)
 963		return *mapfd;
 964
 965	return 0;
 966}
 967
 968static int do_attach(int argc, char **argv)
 969{
 970	enum bpf_attach_type attach_type;
 971	int err, progfd;
 972	int mapfd;
 973
 974	err = parse_attach_detach_args(argc, argv,
 975				       &progfd, &attach_type, &mapfd);
 976	if (err)
 977		return err;
 978
 979	err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
 980	if (err) {
 981		p_err("failed prog attach to map");
 982		return -EINVAL;
 983	}
 984
 985	if (json_output)
 986		jsonw_null(json_wtr);
 987	return 0;
 988}
 989
 990static int do_detach(int argc, char **argv)
 991{
 992	enum bpf_attach_type attach_type;
 993	int err, progfd;
 994	int mapfd;
 995
 996	err = parse_attach_detach_args(argc, argv,
 997				       &progfd, &attach_type, &mapfd);
 998	if (err)
 999		return err;
1000
1001	err = bpf_prog_detach2(progfd, mapfd, attach_type);
1002	if (err) {
1003		p_err("failed prog detach from map");
1004		return -EINVAL;
1005	}
1006
1007	if (json_output)
1008		jsonw_null(json_wtr);
1009	return 0;
1010}
1011
1012static int check_single_stdin(char *file_data_in, char *file_ctx_in)
1013{
1014	if (file_data_in && file_ctx_in &&
1015	    !strcmp(file_data_in, "-") && !strcmp(file_ctx_in, "-")) {
1016		p_err("cannot use standard input for both data_in and ctx_in");
1017		return -1;
1018	}
1019
1020	return 0;
1021}
1022
1023static int get_run_data(const char *fname, void **data_ptr, unsigned int *size)
1024{
1025	size_t block_size = 256;
1026	size_t buf_size = block_size;
1027	size_t nb_read = 0;
1028	void *tmp;
1029	FILE *f;
1030
1031	if (!fname) {
1032		*data_ptr = NULL;
1033		*size = 0;
1034		return 0;
1035	}
1036
1037	if (!strcmp(fname, "-"))
1038		f = stdin;
1039	else
1040		f = fopen(fname, "r");
1041	if (!f) {
1042		p_err("failed to open %s: %s", fname, strerror(errno));
1043		return -1;
1044	}
1045
1046	*data_ptr = malloc(block_size);
1047	if (!*data_ptr) {
1048		p_err("failed to allocate memory for data_in/ctx_in: %s",
1049		      strerror(errno));
1050		goto err_fclose;
1051	}
1052
1053	while ((nb_read += fread(*data_ptr + nb_read, 1, block_size, f))) {
1054		if (feof(f))
1055			break;
1056		if (ferror(f)) {
1057			p_err("failed to read data_in/ctx_in from %s: %s",
1058			      fname, strerror(errno));
1059			goto err_free;
1060		}
1061		if (nb_read > buf_size - block_size) {
1062			if (buf_size == UINT32_MAX) {
1063				p_err("data_in/ctx_in is too long (max: %d)",
1064				      UINT32_MAX);
1065				goto err_free;
1066			}
1067			/* No space for fread()-ing next chunk; realloc() */
1068			buf_size *= 2;
1069			tmp = realloc(*data_ptr, buf_size);
1070			if (!tmp) {
1071				p_err("failed to reallocate data_in/ctx_in: %s",
1072				      strerror(errno));
1073				goto err_free;
1074			}
1075			*data_ptr = tmp;
1076		}
1077	}
1078	if (f != stdin)
1079		fclose(f);
1080
1081	*size = nb_read;
1082	return 0;
1083
1084err_free:
1085	free(*data_ptr);
1086	*data_ptr = NULL;
1087err_fclose:
1088	if (f != stdin)
1089		fclose(f);
1090	return -1;
1091}
1092
1093static void hex_print(void *data, unsigned int size, FILE *f)
1094{
1095	size_t i, j;
1096	char c;
1097
1098	for (i = 0; i < size; i += 16) {
1099		/* Row offset */
1100		fprintf(f, "%07zx\t", i);
1101
1102		/* Hexadecimal values */
1103		for (j = i; j < i + 16 && j < size; j++)
1104			fprintf(f, "%02x%s", *(uint8_t *)(data + j),
1105				j % 2 ? " " : "");
1106		for (; j < i + 16; j++)
1107			fprintf(f, "  %s", j % 2 ? " " : "");
1108
1109		/* ASCII values (if relevant), '.' otherwise */
1110		fprintf(f, "| ");
1111		for (j = i; j < i + 16 && j < size; j++) {
1112			c = *(char *)(data + j);
1113			if (c < ' ' || c > '~')
1114				c = '.';
1115			fprintf(f, "%c%s", c, j == i + 7 ? " " : "");
1116		}
1117
1118		fprintf(f, "\n");
1119	}
1120}
1121
1122static int
1123print_run_output(void *data, unsigned int size, const char *fname,
1124		 const char *json_key)
1125{
1126	size_t nb_written;
1127	FILE *f;
1128
1129	if (!fname)
1130		return 0;
1131
1132	if (!strcmp(fname, "-")) {
1133		f = stdout;
1134		if (json_output) {
1135			jsonw_name(json_wtr, json_key);
1136			print_data_json(data, size);
1137		} else {
1138			hex_print(data, size, f);
1139		}
1140		return 0;
1141	}
1142
1143	f = fopen(fname, "w");
1144	if (!f) {
1145		p_err("failed to open %s: %s", fname, strerror(errno));
1146		return -1;
1147	}
1148
1149	nb_written = fwrite(data, 1, size, f);
1150	fclose(f);
1151	if (nb_written != size) {
1152		p_err("failed to write output data/ctx: %s", strerror(errno));
1153		return -1;
1154	}
1155
1156	return 0;
1157}
1158
1159static int alloc_run_data(void **data_ptr, unsigned int size_out)
1160{
1161	*data_ptr = calloc(size_out, 1);
1162	if (!*data_ptr) {
1163		p_err("failed to allocate memory for output data/ctx: %s",
1164		      strerror(errno));
1165		return -1;
1166	}
1167
1168	return 0;
1169}
1170
1171static int do_run(int argc, char **argv)
1172{
1173	char *data_fname_in = NULL, *data_fname_out = NULL;
1174	char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
1175	struct bpf_prog_test_run_attr test_attr = {0};
1176	const unsigned int default_size = SZ_32K;
1177	void *data_in = NULL, *data_out = NULL;
1178	void *ctx_in = NULL, *ctx_out = NULL;
1179	unsigned int repeat = 1;
1180	int fd, err;
1181
1182	if (!REQ_ARGS(4))
1183		return -1;
1184
1185	fd = prog_parse_fd(&argc, &argv);
1186	if (fd < 0)
1187		return -1;
1188
1189	while (argc) {
1190		if (detect_common_prefix(*argv, "data_in", "data_out",
1191					 "data_size_out", NULL))
1192			return -1;
1193		if (detect_common_prefix(*argv, "ctx_in", "ctx_out",
1194					 "ctx_size_out", NULL))
1195			return -1;
1196
1197		if (is_prefix(*argv, "data_in")) {
1198			NEXT_ARG();
1199			if (!REQ_ARGS(1))
1200				return -1;
1201
1202			data_fname_in = GET_ARG();
1203			if (check_single_stdin(data_fname_in, ctx_fname_in))
1204				return -1;
1205		} else if (is_prefix(*argv, "data_out")) {
1206			NEXT_ARG();
1207			if (!REQ_ARGS(1))
1208				return -1;
1209
1210			data_fname_out = GET_ARG();
1211		} else if (is_prefix(*argv, "data_size_out")) {
1212			char *endptr;
1213
1214			NEXT_ARG();
1215			if (!REQ_ARGS(1))
1216				return -1;
1217
1218			test_attr.data_size_out = strtoul(*argv, &endptr, 0);
1219			if (*endptr) {
1220				p_err("can't parse %s as output data size",
1221				      *argv);
1222				return -1;
1223			}
1224			NEXT_ARG();
1225		} else if (is_prefix(*argv, "ctx_in")) {
1226			NEXT_ARG();
1227			if (!REQ_ARGS(1))
1228				return -1;
1229
1230			ctx_fname_in = GET_ARG();
1231			if (check_single_stdin(data_fname_in, ctx_fname_in))
1232				return -1;
1233		} else if (is_prefix(*argv, "ctx_out")) {
1234			NEXT_ARG();
1235			if (!REQ_ARGS(1))
1236				return -1;
1237
1238			ctx_fname_out = GET_ARG();
1239		} else if (is_prefix(*argv, "ctx_size_out")) {
1240			char *endptr;
1241
1242			NEXT_ARG();
1243			if (!REQ_ARGS(1))
1244				return -1;
1245
1246			test_attr.ctx_size_out = strtoul(*argv, &endptr, 0);
1247			if (*endptr) {
1248				p_err("can't parse %s as output context size",
1249				      *argv);
1250				return -1;
1251			}
1252			NEXT_ARG();
1253		} else if (is_prefix(*argv, "repeat")) {
1254			char *endptr;
1255
1256			NEXT_ARG();
1257			if (!REQ_ARGS(1))
1258				return -1;
1259
1260			repeat = strtoul(*argv, &endptr, 0);
1261			if (*endptr) {
1262				p_err("can't parse %s as repeat number",
1263				      *argv);
1264				return -1;
1265			}
1266			NEXT_ARG();
1267		} else {
1268			p_err("expected no more arguments, 'data_in', 'data_out', 'data_size_out', 'ctx_in', 'ctx_out', 'ctx_size_out' or 'repeat', got: '%s'?",
1269			      *argv);
1270			return -1;
1271		}
1272	}
1273
1274	err = get_run_data(data_fname_in, &data_in, &test_attr.data_size_in);
1275	if (err)
1276		return -1;
1277
1278	if (data_in) {
1279		if (!test_attr.data_size_out)
1280			test_attr.data_size_out = default_size;
1281		err = alloc_run_data(&data_out, test_attr.data_size_out);
1282		if (err)
1283			goto free_data_in;
1284	}
1285
1286	err = get_run_data(ctx_fname_in, &ctx_in, &test_attr.ctx_size_in);
1287	if (err)
1288		goto free_data_out;
1289
1290	if (ctx_in) {
1291		if (!test_attr.ctx_size_out)
1292			test_attr.ctx_size_out = default_size;
1293		err = alloc_run_data(&ctx_out, test_attr.ctx_size_out);
1294		if (err)
1295			goto free_ctx_in;
1296	}
1297
1298	test_attr.prog_fd	= fd;
1299	test_attr.repeat	= repeat;
1300	test_attr.data_in	= data_in;
1301	test_attr.data_out	= data_out;
1302	test_attr.ctx_in	= ctx_in;
1303	test_attr.ctx_out	= ctx_out;
1304
1305	err = bpf_prog_test_run_xattr(&test_attr);
1306	if (err) {
1307		p_err("failed to run program: %s", strerror(errno));
1308		goto free_ctx_out;
1309	}
1310
1311	err = 0;
1312
1313	if (json_output)
1314		jsonw_start_object(json_wtr);	/* root */
1315
1316	/* Do not exit on errors occurring when printing output data/context,
1317	 * we still want to print return value and duration for program run.
1318	 */
1319	if (test_attr.data_size_out)
1320		err += print_run_output(test_attr.data_out,
1321					test_attr.data_size_out,
1322					data_fname_out, "data_out");
1323	if (test_attr.ctx_size_out)
1324		err += print_run_output(test_attr.ctx_out,
1325					test_attr.ctx_size_out,
1326					ctx_fname_out, "ctx_out");
1327
1328	if (json_output) {
1329		jsonw_uint_field(json_wtr, "retval", test_attr.retval);
1330		jsonw_uint_field(json_wtr, "duration", test_attr.duration);
1331		jsonw_end_object(json_wtr);	/* root */
1332	} else {
1333		fprintf(stdout, "Return value: %u, duration%s: %uns\n",
1334			test_attr.retval,
1335			repeat > 1 ? " (average)" : "", test_attr.duration);
1336	}
1337
1338free_ctx_out:
1339	free(ctx_out);
1340free_ctx_in:
1341	free(ctx_in);
1342free_data_out:
1343	free(data_out);
1344free_data_in:
1345	free(data_in);
1346
1347	return err;
1348}
1349
1350static int
1351get_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
1352		      enum bpf_attach_type *expected_attach_type)
1353{
1354	libbpf_print_fn_t print_backup;
1355	int ret;
1356
1357	ret = libbpf_prog_type_by_name(name, prog_type, expected_attach_type);
1358	if (!ret)
1359		return ret;
1360
1361	/* libbpf_prog_type_by_name() failed, let's re-run with debug level */
1362	print_backup = libbpf_set_print(print_all_levels);
1363	ret = libbpf_prog_type_by_name(name, prog_type, expected_attach_type);
1364	libbpf_set_print(print_backup);
1365
1366	return ret;
1367}
1368
1369static int load_with_options(int argc, char **argv, bool first_prog_only)
1370{
1371	enum bpf_prog_type common_prog_type = BPF_PROG_TYPE_UNSPEC;
1372	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts,
1373		.relaxed_maps = relaxed_maps,
1374	);
1375	struct bpf_object_load_attr load_attr = { 0 };
 
 
 
1376	enum bpf_attach_type expected_attach_type;
1377	struct map_replace *map_replace = NULL;
1378	struct bpf_program *prog = NULL, *pos;
1379	unsigned int old_map_fds = 0;
1380	const char *pinmaps = NULL;
1381	struct bpf_object *obj;
1382	struct bpf_map *map;
1383	const char *pinfile;
1384	unsigned int i, j;
1385	__u32 ifindex = 0;
1386	const char *file;
1387	int idx, err;
1388
1389
1390	if (!REQ_ARGS(2))
1391		return -1;
1392	file = GET_ARG();
1393	pinfile = GET_ARG();
1394
1395	while (argc) {
1396		if (is_prefix(*argv, "type")) {
1397			char *type;
1398
1399			NEXT_ARG();
1400
1401			if (common_prog_type != BPF_PROG_TYPE_UNSPEC) {
1402				p_err("program type already specified");
1403				goto err_free_reuse_maps;
1404			}
1405			if (!REQ_ARGS(1))
1406				goto err_free_reuse_maps;
1407
1408			/* Put a '/' at the end of type to appease libbpf */
1409			type = malloc(strlen(*argv) + 2);
1410			if (!type) {
1411				p_err("mem alloc failed");
1412				goto err_free_reuse_maps;
1413			}
1414			*type = 0;
1415			strcat(type, *argv);
1416			strcat(type, "/");
1417
1418			err = get_prog_type_by_name(type, &common_prog_type,
1419						    &expected_attach_type);
 
1420			free(type);
1421			if (err < 0)
1422				goto err_free_reuse_maps;
1423
1424			NEXT_ARG();
1425		} else if (is_prefix(*argv, "map")) {
1426			void *new_map_replace;
1427			char *endptr, *name;
1428			int fd;
1429
1430			NEXT_ARG();
1431
1432			if (!REQ_ARGS(4))
1433				goto err_free_reuse_maps;
1434
1435			if (is_prefix(*argv, "idx")) {
1436				NEXT_ARG();
1437
1438				idx = strtoul(*argv, &endptr, 0);
1439				if (*endptr) {
1440					p_err("can't parse %s as IDX", *argv);
1441					goto err_free_reuse_maps;
1442				}
1443				name = NULL;
1444			} else if (is_prefix(*argv, "name")) {
1445				NEXT_ARG();
1446
1447				name = *argv;
1448				idx = -1;
1449			} else {
1450				p_err("expected 'idx' or 'name', got: '%s'?",
1451				      *argv);
1452				goto err_free_reuse_maps;
1453			}
1454			NEXT_ARG();
1455
1456			fd = map_parse_fd(&argc, &argv);
1457			if (fd < 0)
1458				goto err_free_reuse_maps;
1459
1460			new_map_replace = reallocarray(map_replace,
1461						       old_map_fds + 1,
1462						       sizeof(*map_replace));
1463			if (!new_map_replace) {
1464				p_err("mem alloc failed");
1465				goto err_free_reuse_maps;
1466			}
1467			map_replace = new_map_replace;
1468
1469			map_replace[old_map_fds].idx = idx;
1470			map_replace[old_map_fds].name = name;
1471			map_replace[old_map_fds].fd = fd;
1472			old_map_fds++;
1473		} else if (is_prefix(*argv, "dev")) {
1474			NEXT_ARG();
1475
1476			if (ifindex) {
1477				p_err("offload device already specified");
1478				goto err_free_reuse_maps;
1479			}
1480			if (!REQ_ARGS(1))
1481				goto err_free_reuse_maps;
1482
1483			ifindex = if_nametoindex(*argv);
1484			if (!ifindex) {
1485				p_err("unrecognized netdevice '%s': %s",
1486				      *argv, strerror(errno));
1487				goto err_free_reuse_maps;
1488			}
1489			NEXT_ARG();
1490		} else if (is_prefix(*argv, "pinmaps")) {
1491			NEXT_ARG();
1492
1493			if (!REQ_ARGS(1))
1494				goto err_free_reuse_maps;
1495
1496			pinmaps = GET_ARG();
1497		} else {
1498			p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
1499			      *argv);
1500			goto err_free_reuse_maps;
1501		}
1502	}
1503
1504	set_max_rlimit();
1505
1506	obj = bpf_object__open_file(file, &open_opts);
1507	if (libbpf_get_error(obj)) {
1508		p_err("failed to open object file");
1509		goto err_free_reuse_maps;
1510	}
1511
1512	bpf_object__for_each_program(pos, obj) {
1513		enum bpf_prog_type prog_type = common_prog_type;
1514
1515		if (prog_type == BPF_PROG_TYPE_UNSPEC) {
1516			const char *sec_name = bpf_program__section_name(pos);
1517
1518			err = get_prog_type_by_name(sec_name, &prog_type,
1519						    &expected_attach_type);
1520			if (err < 0)
1521				goto err_close_obj;
1522		}
1523
1524		bpf_program__set_ifindex(pos, ifindex);
1525		bpf_program__set_type(pos, prog_type);
1526		bpf_program__set_expected_attach_type(pos, expected_attach_type);
1527	}
1528
1529	qsort(map_replace, old_map_fds, sizeof(*map_replace),
1530	      map_replace_compar);
1531
1532	/* After the sort maps by name will be first on the list, because they
1533	 * have idx == -1.  Resolve them.
1534	 */
1535	j = 0;
1536	while (j < old_map_fds && map_replace[j].name) {
1537		i = 0;
1538		bpf_object__for_each_map(map, obj) {
1539			if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
1540				map_replace[j].idx = i;
1541				break;
1542			}
1543			i++;
1544		}
1545		if (map_replace[j].idx == -1) {
1546			p_err("unable to find map '%s'", map_replace[j].name);
1547			goto err_close_obj;
1548		}
1549		j++;
1550	}
1551	/* Resort if any names were resolved */
1552	if (j)
1553		qsort(map_replace, old_map_fds, sizeof(*map_replace),
1554		      map_replace_compar);
1555
1556	/* Set ifindex and name reuse */
1557	j = 0;
1558	idx = 0;
1559	bpf_object__for_each_map(map, obj) {
1560		if (!bpf_map__is_offload_neutral(map))
1561			bpf_map__set_ifindex(map, ifindex);
1562
1563		if (j < old_map_fds && idx == map_replace[j].idx) {
1564			err = bpf_map__reuse_fd(map, map_replace[j++].fd);
1565			if (err) {
1566				p_err("unable to set up map reuse: %d", err);
1567				goto err_close_obj;
1568			}
1569
1570			/* Next reuse wants to apply to the same map */
1571			if (j < old_map_fds && map_replace[j].idx == idx) {
1572				p_err("replacement for map idx %d specified more than once",
1573				      idx);
1574				goto err_close_obj;
1575			}
1576		}
1577
1578		idx++;
1579	}
1580	if (j < old_map_fds) {
1581		p_err("map idx '%d' not used", map_replace[j].idx);
1582		goto err_close_obj;
1583	}
1584
1585	load_attr.obj = obj;
1586	if (verifier_logs)
1587		/* log_level1 + log_level2 + stats, but not stable UAPI */
1588		load_attr.log_level = 1 + 2 + 4;
1589
1590	err = bpf_object__load_xattr(&load_attr);
1591	if (err) {
1592		p_err("failed to load object file");
1593		goto err_close_obj;
1594	}
1595
1596	err = mount_bpffs_for_pin(pinfile);
1597	if (err)
1598		goto err_close_obj;
1599
1600	if (first_prog_only) {
1601		prog = bpf_program__next(NULL, obj);
1602		if (!prog) {
1603			p_err("object file doesn't contain any bpf program");
1604			goto err_close_obj;
1605		}
1606
1607		err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
1608		if (err) {
1609			p_err("failed to pin program %s",
1610			      bpf_program__section_name(prog));
1611			goto err_close_obj;
1612		}
1613	} else {
1614		err = bpf_object__pin_programs(obj, pinfile);
1615		if (err) {
1616			p_err("failed to pin all programs");
1617			goto err_close_obj;
1618		}
1619	}
1620
1621	if (pinmaps) {
1622		err = bpf_object__pin_maps(obj, pinmaps);
1623		if (err) {
1624			p_err("failed to pin all maps");
1625			goto err_unpin;
1626		}
1627	}
1628
1629	if (json_output)
1630		jsonw_null(json_wtr);
1631
1632	bpf_object__close(obj);
1633	for (i = 0; i < old_map_fds; i++)
1634		close(map_replace[i].fd);
1635	free(map_replace);
1636
1637	return 0;
1638
1639err_unpin:
1640	if (first_prog_only)
1641		unlink(pinfile);
1642	else
1643		bpf_object__unpin_programs(obj, pinfile);
1644err_close_obj:
1645	bpf_object__close(obj);
1646err_free_reuse_maps:
1647	for (i = 0; i < old_map_fds; i++)
1648		close(map_replace[i].fd);
1649	free(map_replace);
1650	return -1;
1651}
1652
1653static int count_open_fds(void)
1654{
1655	DIR *dp = opendir("/proc/self/fd");
1656	struct dirent *de;
1657	int cnt = -3;
1658
1659	if (!dp)
1660		return -1;
1661
1662	while ((de = readdir(dp)))
1663		cnt++;
1664
1665	closedir(dp);
1666	return cnt;
1667}
1668
1669static int try_loader(struct gen_loader_opts *gen)
1670{
1671	struct bpf_load_and_run_opts opts = {};
1672	struct bpf_loader_ctx *ctx;
1673	int ctx_sz = sizeof(*ctx) + 64 * max(sizeof(struct bpf_map_desc),
1674					     sizeof(struct bpf_prog_desc));
1675	int log_buf_sz = (1u << 24) - 1;
1676	int err, fds_before, fd_delta;
1677	char *log_buf;
1678
1679	ctx = alloca(ctx_sz);
1680	memset(ctx, 0, ctx_sz);
1681	ctx->sz = ctx_sz;
1682	ctx->log_level = 1;
1683	ctx->log_size = log_buf_sz;
1684	log_buf = malloc(log_buf_sz);
1685	if (!log_buf)
1686		return -ENOMEM;
1687	ctx->log_buf = (long) log_buf;
1688	opts.ctx = ctx;
1689	opts.data = gen->data;
1690	opts.data_sz = gen->data_sz;
1691	opts.insns = gen->insns;
1692	opts.insns_sz = gen->insns_sz;
1693	fds_before = count_open_fds();
1694	err = bpf_load_and_run(&opts);
1695	fd_delta = count_open_fds() - fds_before;
1696	if (err < 0) {
1697		fprintf(stderr, "err %d\n%s\n%s", err, opts.errstr, log_buf);
1698		if (fd_delta)
1699			fprintf(stderr, "loader prog leaked %d FDs\n",
1700				fd_delta);
1701	}
1702	free(log_buf);
1703	return err;
1704}
1705
1706static int do_loader(int argc, char **argv)
1707{
1708	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts);
1709	DECLARE_LIBBPF_OPTS(gen_loader_opts, gen);
1710	struct bpf_object_load_attr load_attr = {};
1711	struct bpf_object *obj;
1712	const char *file;
1713	int err = 0;
1714
1715	if (!REQ_ARGS(1))
1716		return -1;
1717	file = GET_ARG();
1718
1719	obj = bpf_object__open_file(file, &open_opts);
1720	if (libbpf_get_error(obj)) {
1721		p_err("failed to open object file");
1722		goto err_close_obj;
1723	}
1724
1725	err = bpf_object__gen_loader(obj, &gen);
1726	if (err)
1727		goto err_close_obj;
1728
1729	load_attr.obj = obj;
1730	if (verifier_logs)
1731		/* log_level1 + log_level2 + stats, but not stable UAPI */
1732		load_attr.log_level = 1 + 2 + 4;
1733
1734	err = bpf_object__load_xattr(&load_attr);
1735	if (err) {
1736		p_err("failed to load object file");
1737		goto err_close_obj;
1738	}
1739
1740	if (verifier_logs) {
1741		struct dump_data dd = {};
1742
1743		kernel_syms_load(&dd);
1744		dump_xlated_plain(&dd, (void *)gen.insns, gen.insns_sz, false, false);
1745		kernel_syms_destroy(&dd);
1746	}
1747	err = try_loader(&gen);
1748err_close_obj:
1749	bpf_object__close(obj);
1750	return err;
1751}
1752
1753static int do_load(int argc, char **argv)
1754{
1755	if (use_loader)
1756		return do_loader(argc, argv);
1757	return load_with_options(argc, argv, true);
1758}
1759
1760static int do_loadall(int argc, char **argv)
1761{
1762	return load_with_options(argc, argv, false);
1763}
1764
1765#ifdef BPFTOOL_WITHOUT_SKELETONS
1766
1767static int do_profile(int argc, char **argv)
1768{
1769	p_err("bpftool prog profile command is not supported. Please build bpftool with clang >= 10.0.0");
1770	return 0;
1771}
1772
1773#else /* BPFTOOL_WITHOUT_SKELETONS */
1774
1775#include "profiler.skel.h"
1776
1777struct profile_metric {
1778	const char *name;
1779	struct bpf_perf_event_value val;
1780	struct perf_event_attr attr;
1781	bool selected;
1782
1783	/* calculate ratios like instructions per cycle */
1784	const int ratio_metric; /* 0 for N/A, 1 for index 0 (cycles) */
1785	const char *ratio_desc;
1786	const float ratio_mul;
1787} metrics[] = {
1788	{
1789		.name = "cycles",
1790		.attr = {
1791			.type = PERF_TYPE_HARDWARE,
1792			.config = PERF_COUNT_HW_CPU_CYCLES,
1793			.exclude_user = 1,
1794		},
1795	},
1796	{
1797		.name = "instructions",
1798		.attr = {
1799			.type = PERF_TYPE_HARDWARE,
1800			.config = PERF_COUNT_HW_INSTRUCTIONS,
1801			.exclude_user = 1,
1802		},
1803		.ratio_metric = 1,
1804		.ratio_desc = "insns per cycle",
1805		.ratio_mul = 1.0,
1806	},
1807	{
1808		.name = "l1d_loads",
1809		.attr = {
1810			.type = PERF_TYPE_HW_CACHE,
1811			.config =
1812				PERF_COUNT_HW_CACHE_L1D |
1813				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
1814				(PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
1815			.exclude_user = 1,
1816		},
1817	},
1818	{
1819		.name = "llc_misses",
1820		.attr = {
1821			.type = PERF_TYPE_HW_CACHE,
1822			.config =
1823				PERF_COUNT_HW_CACHE_LL |
1824				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
1825				(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
1826			.exclude_user = 1
1827		},
1828		.ratio_metric = 2,
1829		.ratio_desc = "LLC misses per million insns",
1830		.ratio_mul = 1e6,
1831	},
1832	{
1833		.name = "itlb_misses",
1834		.attr = {
1835			.type = PERF_TYPE_HW_CACHE,
1836			.config =
1837				PERF_COUNT_HW_CACHE_ITLB |
1838				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
1839				(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
1840			.exclude_user = 1
1841		},
1842		.ratio_metric = 2,
1843		.ratio_desc = "itlb misses per million insns",
1844		.ratio_mul = 1e6,
1845	},
1846	{
1847		.name = "dtlb_misses",
1848		.attr = {
1849			.type = PERF_TYPE_HW_CACHE,
1850			.config =
1851				PERF_COUNT_HW_CACHE_DTLB |
1852				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
1853				(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
1854			.exclude_user = 1
1855		},
1856		.ratio_metric = 2,
1857		.ratio_desc = "dtlb misses per million insns",
1858		.ratio_mul = 1e6,
1859	},
1860};
1861
1862static __u64 profile_total_count;
1863
1864#define MAX_NUM_PROFILE_METRICS 4
1865
1866static int profile_parse_metrics(int argc, char **argv)
1867{
1868	unsigned int metric_cnt;
1869	int selected_cnt = 0;
1870	unsigned int i;
1871
1872	metric_cnt = sizeof(metrics) / sizeof(struct profile_metric);
1873
1874	while (argc > 0) {
1875		for (i = 0; i < metric_cnt; i++) {
1876			if (is_prefix(argv[0], metrics[i].name)) {
1877				if (!metrics[i].selected)
1878					selected_cnt++;
1879				metrics[i].selected = true;
1880				break;
1881			}
1882		}
1883		if (i == metric_cnt) {
1884			p_err("unknown metric %s", argv[0]);
1885			return -1;
1886		}
1887		NEXT_ARG();
1888	}
1889	if (selected_cnt > MAX_NUM_PROFILE_METRICS) {
1890		p_err("too many (%d) metrics, please specify no more than %d metrics at at time",
1891		      selected_cnt, MAX_NUM_PROFILE_METRICS);
1892		return -1;
1893	}
1894	return selected_cnt;
1895}
1896
1897static void profile_read_values(struct profiler_bpf *obj)
1898{
1899	__u32 m, cpu, num_cpu = obj->rodata->num_cpu;
1900	int reading_map_fd, count_map_fd;
1901	__u64 counts[num_cpu];
1902	__u32 key = 0;
1903	int err;
1904
1905	reading_map_fd = bpf_map__fd(obj->maps.accum_readings);
1906	count_map_fd = bpf_map__fd(obj->maps.counts);
1907	if (reading_map_fd < 0 || count_map_fd < 0) {
1908		p_err("failed to get fd for map");
1909		return;
1910	}
1911
1912	err = bpf_map_lookup_elem(count_map_fd, &key, counts);
1913	if (err) {
1914		p_err("failed to read count_map: %s", strerror(errno));
1915		return;
1916	}
1917
1918	profile_total_count = 0;
1919	for (cpu = 0; cpu < num_cpu; cpu++)
1920		profile_total_count += counts[cpu];
1921
1922	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
1923		struct bpf_perf_event_value values[num_cpu];
1924
1925		if (!metrics[m].selected)
1926			continue;
1927
1928		err = bpf_map_lookup_elem(reading_map_fd, &key, values);
1929		if (err) {
1930			p_err("failed to read reading_map: %s",
1931			      strerror(errno));
1932			return;
1933		}
1934		for (cpu = 0; cpu < num_cpu; cpu++) {
1935			metrics[m].val.counter += values[cpu].counter;
1936			metrics[m].val.enabled += values[cpu].enabled;
1937			metrics[m].val.running += values[cpu].running;
1938		}
1939		key++;
1940	}
1941}
1942
1943static void profile_print_readings_json(void)
1944{
1945	__u32 m;
1946
1947	jsonw_start_array(json_wtr);
1948	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
1949		if (!metrics[m].selected)
1950			continue;
1951		jsonw_start_object(json_wtr);
1952		jsonw_string_field(json_wtr, "metric", metrics[m].name);
1953		jsonw_lluint_field(json_wtr, "run_cnt", profile_total_count);
1954		jsonw_lluint_field(json_wtr, "value", metrics[m].val.counter);
1955		jsonw_lluint_field(json_wtr, "enabled", metrics[m].val.enabled);
1956		jsonw_lluint_field(json_wtr, "running", metrics[m].val.running);
1957
1958		jsonw_end_object(json_wtr);
1959	}
1960	jsonw_end_array(json_wtr);
1961}
1962
1963static void profile_print_readings_plain(void)
1964{
1965	__u32 m;
1966
1967	printf("\n%18llu %-20s\n", profile_total_count, "run_cnt");
1968	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
1969		struct bpf_perf_event_value *val = &metrics[m].val;
1970		int r;
1971
1972		if (!metrics[m].selected)
1973			continue;
1974		printf("%18llu %-20s", val->counter, metrics[m].name);
1975
1976		r = metrics[m].ratio_metric - 1;
1977		if (r >= 0 && metrics[r].selected &&
1978		    metrics[r].val.counter > 0) {
1979			printf("# %8.2f %-30s",
1980			       val->counter * metrics[m].ratio_mul /
1981			       metrics[r].val.counter,
1982			       metrics[m].ratio_desc);
1983		} else {
1984			printf("%-41s", "");
1985		}
1986
1987		if (val->enabled > val->running)
1988			printf("(%4.2f%%)",
1989			       val->running * 100.0 / val->enabled);
1990		printf("\n");
1991	}
1992}
1993
1994static void profile_print_readings(void)
1995{
1996	if (json_output)
1997		profile_print_readings_json();
1998	else
1999		profile_print_readings_plain();
2000}
2001
2002static char *profile_target_name(int tgt_fd)
2003{
2004	struct bpf_prog_info_linear *info_linear;
2005	struct bpf_func_info *func_info;
2006	const struct btf_type *t;
2007	struct btf *btf = NULL;
2008	char *name = NULL;
2009
2010	info_linear = bpf_program__get_prog_info_linear(
2011		tgt_fd, 1UL << BPF_PROG_INFO_FUNC_INFO);
2012	if (IS_ERR_OR_NULL(info_linear)) {
2013		p_err("failed to get info_linear for prog FD %d", tgt_fd);
2014		return NULL;
2015	}
2016
2017	if (info_linear->info.btf_id == 0 ||
2018	    btf__get_from_id(info_linear->info.btf_id, &btf)) {
2019		p_err("prog FD %d doesn't have valid btf", tgt_fd);
2020		goto out;
2021	}
2022
2023	func_info = u64_to_ptr(info_linear->info.func_info);
2024	t = btf__type_by_id(btf, func_info[0].type_id);
2025	if (!t) {
2026		p_err("btf %d doesn't have type %d",
2027		      info_linear->info.btf_id, func_info[0].type_id);
2028		goto out;
2029	}
2030	name = strdup(btf__name_by_offset(btf, t->name_off));
2031out:
2032	btf__free(btf);
2033	free(info_linear);
2034	return name;
2035}
2036
2037static struct profiler_bpf *profile_obj;
2038static int profile_tgt_fd = -1;
2039static char *profile_tgt_name;
2040static int *profile_perf_events;
2041static int profile_perf_event_cnt;
2042
2043static void profile_close_perf_events(struct profiler_bpf *obj)
2044{
2045	int i;
2046
2047	for (i = profile_perf_event_cnt - 1; i >= 0; i--)
2048		close(profile_perf_events[i]);
2049
2050	free(profile_perf_events);
2051	profile_perf_event_cnt = 0;
2052}
2053
2054static int profile_open_perf_events(struct profiler_bpf *obj)
2055{
2056	unsigned int cpu, m;
2057	int map_fd, pmu_fd;
2058
2059	profile_perf_events = calloc(
2060		sizeof(int), obj->rodata->num_cpu * obj->rodata->num_metric);
2061	if (!profile_perf_events) {
2062		p_err("failed to allocate memory for perf_event array: %s",
2063		      strerror(errno));
2064		return -1;
2065	}
2066	map_fd = bpf_map__fd(obj->maps.events);
2067	if (map_fd < 0) {
2068		p_err("failed to get fd for events map");
2069		return -1;
2070	}
2071
2072	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
2073		if (!metrics[m].selected)
2074			continue;
2075		for (cpu = 0; cpu < obj->rodata->num_cpu; cpu++) {
2076			pmu_fd = syscall(__NR_perf_event_open, &metrics[m].attr,
2077					 -1/*pid*/, cpu, -1/*group_fd*/, 0);
2078			if (pmu_fd < 0 ||
2079			    bpf_map_update_elem(map_fd, &profile_perf_event_cnt,
2080						&pmu_fd, BPF_ANY) ||
2081			    ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) {
2082				p_err("failed to create event %s on cpu %d",
2083				      metrics[m].name, cpu);
2084				return -1;
2085			}
2086			profile_perf_events[profile_perf_event_cnt++] = pmu_fd;
2087		}
2088	}
2089	return 0;
2090}
2091
2092static void profile_print_and_cleanup(void)
2093{
2094	profile_close_perf_events(profile_obj);
2095	profile_read_values(profile_obj);
2096	profile_print_readings();
2097	profiler_bpf__destroy(profile_obj);
2098
2099	close(profile_tgt_fd);
2100	free(profile_tgt_name);
2101}
2102
2103static void int_exit(int signo)
2104{
2105	profile_print_and_cleanup();
2106	exit(0);
2107}
2108
2109static int do_profile(int argc, char **argv)
2110{
2111	int num_metric, num_cpu, err = -1;
2112	struct bpf_program *prog;
2113	unsigned long duration;
2114	char *endptr;
2115
2116	/* we at least need two args for the prog and one metric */
2117	if (!REQ_ARGS(3))
2118		return -EINVAL;
2119
2120	/* parse target fd */
2121	profile_tgt_fd = prog_parse_fd(&argc, &argv);
2122	if (profile_tgt_fd < 0) {
2123		p_err("failed to parse fd");
2124		return -1;
2125	}
2126
2127	/* parse profiling optional duration */
2128	if (argc > 2 && is_prefix(argv[0], "duration")) {
2129		NEXT_ARG();
2130		duration = strtoul(*argv, &endptr, 0);
2131		if (*endptr)
2132			usage();
2133		NEXT_ARG();
2134	} else {
2135		duration = UINT_MAX;
2136	}
2137
2138	num_metric = profile_parse_metrics(argc, argv);
2139	if (num_metric <= 0)
2140		goto out;
2141
2142	num_cpu = libbpf_num_possible_cpus();
2143	if (num_cpu <= 0) {
2144		p_err("failed to identify number of CPUs");
2145		goto out;
2146	}
2147
2148	profile_obj = profiler_bpf__open();
2149	if (!profile_obj) {
2150		p_err("failed to open and/or load BPF object");
2151		goto out;
2152	}
2153
2154	profile_obj->rodata->num_cpu = num_cpu;
2155	profile_obj->rodata->num_metric = num_metric;
2156
2157	/* adjust map sizes */
2158	bpf_map__resize(profile_obj->maps.events, num_metric * num_cpu);
2159	bpf_map__resize(profile_obj->maps.fentry_readings, num_metric);
2160	bpf_map__resize(profile_obj->maps.accum_readings, num_metric);
2161	bpf_map__resize(profile_obj->maps.counts, 1);
2162
2163	/* change target name */
2164	profile_tgt_name = profile_target_name(profile_tgt_fd);
2165	if (!profile_tgt_name)
2166		goto out;
2167
2168	bpf_object__for_each_program(prog, profile_obj->obj) {
2169		err = bpf_program__set_attach_target(prog, profile_tgt_fd,
2170						     profile_tgt_name);
2171		if (err) {
2172			p_err("failed to set attach target\n");
2173			goto out;
2174		}
2175	}
2176
2177	set_max_rlimit();
2178	err = profiler_bpf__load(profile_obj);
2179	if (err) {
2180		p_err("failed to load profile_obj");
2181		goto out;
2182	}
2183
2184	err = profile_open_perf_events(profile_obj);
2185	if (err)
2186		goto out;
2187
2188	err = profiler_bpf__attach(profile_obj);
2189	if (err) {
2190		p_err("failed to attach profile_obj");
2191		goto out;
2192	}
2193	signal(SIGINT, int_exit);
2194
2195	sleep(duration);
2196	profile_print_and_cleanup();
2197	return 0;
2198
2199out:
2200	profile_close_perf_events(profile_obj);
2201	if (profile_obj)
2202		profiler_bpf__destroy(profile_obj);
2203	close(profile_tgt_fd);
2204	free(profile_tgt_name);
2205	return err;
2206}
2207
2208#endif /* BPFTOOL_WITHOUT_SKELETONS */
2209
2210static int do_help(int argc, char **argv)
2211{
2212	if (json_output) {
2213		jsonw_null(json_wtr);
2214		return 0;
2215	}
2216
2217	fprintf(stderr,
2218		"Usage: %1$s %2$s { show | list } [PROG]\n"
2219		"       %1$s %2$s dump xlated PROG [{ file FILE | opcodes | visual | linum }]\n"
2220		"       %1$s %2$s dump jited  PROG [{ file FILE | opcodes | linum }]\n"
2221		"       %1$s %2$s pin   PROG FILE\n"
2222		"       %1$s %2$s { load | loadall } OBJ  PATH \\\n"
2223		"                         [type TYPE] [dev NAME] \\\n"
2224		"                         [map { idx IDX | name NAME } MAP]\\\n"
2225		"                         [pinmaps MAP_DIR]\n"
2226		"       %1$s %2$s attach PROG ATTACH_TYPE [MAP]\n"
2227		"       %1$s %2$s detach PROG ATTACH_TYPE [MAP]\n"
2228		"       %1$s %2$s run PROG \\\n"
2229		"                         data_in FILE \\\n"
2230		"                         [data_out FILE [data_size_out L]] \\\n"
2231		"                         [ctx_in FILE [ctx_out FILE [ctx_size_out M]]] \\\n"
2232		"                         [repeat N]\n"
2233		"       %1$s %2$s profile PROG [duration DURATION] METRICs\n"
2234		"       %1$s %2$s tracelog\n"
2235		"       %1$s %2$s help\n"
2236		"\n"
2237		"       " HELP_SPEC_MAP "\n"
2238		"       " HELP_SPEC_PROGRAM "\n"
2239		"       TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
2240		"                 tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
2241		"                 cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
2242		"                 lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
2243		"                 sk_reuseport | flow_dissector | cgroup/sysctl |\n"
2244		"                 cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
2245		"                 cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
2246		"                 cgroup/getpeername4 | cgroup/getpeername6 |\n"
2247		"                 cgroup/getsockname4 | cgroup/getsockname6 | cgroup/sendmsg4 |\n"
2248		"                 cgroup/sendmsg6 | cgroup/recvmsg4 | cgroup/recvmsg6 |\n"
2249		"                 cgroup/getsockopt | cgroup/setsockopt | cgroup/sock_release |\n"
2250		"                 struct_ops | fentry | fexit | freplace | sk_lookup }\n"
2251		"       ATTACH_TYPE := { msg_verdict | stream_verdict | stream_parser |\n"
2252		"                        flow_dissector }\n"
2253		"       METRIC := { cycles | instructions | l1d_loads | llc_misses | itlb_misses | dtlb_misses }\n"
2254		"       " HELP_SPEC_OPTIONS "\n"
2255		"",
 
 
 
2256		bin_name, argv[-2]);
2257
2258	return 0;
2259}
2260
2261static const struct cmd cmds[] = {
2262	{ "show",	do_show },
2263	{ "list",	do_show },
2264	{ "help",	do_help },
2265	{ "dump",	do_dump },
2266	{ "pin",	do_pin },
2267	{ "load",	do_load },
2268	{ "loadall",	do_loadall },
2269	{ "attach",	do_attach },
2270	{ "detach",	do_detach },
2271	{ "tracelog",	do_tracelog },
2272	{ "run",	do_run },
2273	{ "profile",	do_profile },
2274	{ 0 }
2275};
2276
2277int do_prog(int argc, char **argv)
2278{
2279	return cmd_select(cmds, argc, argv, do_help);
2280}
v5.4
   1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
   2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
   3
   4#define _GNU_SOURCE
   5#include <errno.h>
   6#include <fcntl.h>
 
   7#include <stdarg.h>
   8#include <stdio.h>
   9#include <stdlib.h>
  10#include <string.h>
  11#include <time.h>
  12#include <unistd.h>
  13#include <net/if.h>
 
  14#include <sys/types.h>
  15#include <sys/stat.h>
 
 
  16
  17#include <linux/err.h>
 
  18#include <linux/sizes.h>
  19
  20#include <bpf.h>
  21#include <btf.h>
  22#include <libbpf.h>
 
 
  23
  24#include "cfg.h"
  25#include "main.h"
  26#include "xlated_dumper.h"
  27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  28static const char * const attach_type_strings[] = {
  29	[BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
  30	[BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
 
  31	[BPF_SK_MSG_VERDICT] = "msg_verdict",
  32	[BPF_FLOW_DISSECTOR] = "flow_dissector",
  33	[__MAX_BPF_ATTACH_TYPE] = NULL,
  34};
  35
  36static enum bpf_attach_type parse_attach_type(const char *str)
  37{
  38	enum bpf_attach_type type;
  39
  40	for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
  41		if (attach_type_strings[type] &&
  42		    is_prefix(str, attach_type_strings[type]))
  43			return type;
  44	}
  45
  46	return __MAX_BPF_ATTACH_TYPE;
  47}
  48
  49static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
  50{
  51	struct timespec real_time_ts, boot_time_ts;
  52	time_t wallclock_secs;
  53	struct tm load_tm;
  54
  55	buf[--size] = '\0';
  56
  57	if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
  58	    clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
  59		perror("Can't read clocks");
  60		snprintf(buf, size, "%llu", nsecs / 1000000000);
  61		return;
  62	}
  63
  64	wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
  65		(real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
  66		1000000000;
  67
  68
  69	if (!localtime_r(&wallclock_secs, &load_tm)) {
  70		snprintf(buf, size, "%llu", nsecs / 1000000000);
  71		return;
  72	}
  73
  74	if (json_output)
  75		strftime(buf, size, "%s", &load_tm);
  76	else
  77		strftime(buf, size, "%FT%T%z", &load_tm);
  78}
  79
  80static int prog_fd_by_tag(unsigned char *tag)
  81{
  82	unsigned int id = 0;
 
 
 
  83	int err;
  84	int fd;
  85
  86	while (true) {
  87		struct bpf_prog_info info = {};
  88		__u32 len = sizeof(info);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  89
  90		err = bpf_prog_get_next_id(id, &id);
  91		if (err) {
  92			p_err("%s", strerror(errno));
  93			return -1;
  94		}
  95
  96		fd = bpf_prog_get_fd_by_id(id);
  97		if (fd < 0) {
  98			p_err("can't get prog by id (%u): %s",
  99			      id, strerror(errno));
 100			return -1;
 101		}
 102
 103		err = bpf_obj_get_info_by_fd(fd, &info, &len);
 104		if (err) {
 105			p_err("can't get prog info (%u): %s",
 106			      id, strerror(errno));
 107			close(fd);
 108			return -1;
 109		}
 110
 111		if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
 112			return fd;
 
 
 
 113
 114		close(fd);
 115	}
 116}
 117
 118int prog_parse_fd(int *argc, char ***argv)
 119{
 120	int fd;
 
 121
 122	if (is_prefix(**argv, "id")) {
 123		unsigned int id;
 124		char *endptr;
 125
 126		NEXT_ARGP();
 127
 128		id = strtoul(**argv, &endptr, 0);
 129		if (*endptr) {
 130			p_err("can't parse %s as ID", **argv);
 131			return -1;
 132		}
 133		NEXT_ARGP();
 134
 135		fd = bpf_prog_get_fd_by_id(id);
 136		if (fd < 0)
 137			p_err("get by id (%u): %s", id, strerror(errno));
 138		return fd;
 139	} else if (is_prefix(**argv, "tag")) {
 140		unsigned char tag[BPF_TAG_SIZE];
 141
 142		NEXT_ARGP();
 143
 144		if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
 145			   tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
 146		    != BPF_TAG_SIZE) {
 147			p_err("can't parse tag");
 148			return -1;
 149		}
 150		NEXT_ARGP();
 151
 152		return prog_fd_by_tag(tag);
 153	} else if (is_prefix(**argv, "pinned")) {
 154		char *path;
 
 
 155
 156		NEXT_ARGP();
 
 
 
 
 
 157
 158		path = **argv;
 159		NEXT_ARGP();
 
 160
 161		return open_obj_pinned_any(path, BPF_OBJ_PROG);
 162	}
 
 
 163
 164	p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
 165	return -1;
 
 166}
 167
 168static void show_prog_maps(int fd, u32 num_maps)
 169{
 170	struct bpf_prog_info info = {};
 171	__u32 len = sizeof(info);
 172	__u32 map_ids[num_maps];
 173	unsigned int i;
 
 
 
 
 174	int err;
 175
 176	info.nr_map_ids = num_maps;
 177	info.map_ids = ptr_to_u64(map_ids);
 178
 179	err = bpf_obj_get_info_by_fd(fd, &info, &len);
 180	if (err || !info.nr_map_ids)
 
 181		return;
 182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 183	if (json_output) {
 184		jsonw_name(json_wtr, "map_ids");
 185		jsonw_start_array(json_wtr);
 186		for (i = 0; i < info.nr_map_ids; i++)
 187			jsonw_uint(json_wtr, map_ids[i]);
 188		jsonw_end_array(json_wtr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 189	} else {
 190		printf("  map_ids ");
 191		for (i = 0; i < info.nr_map_ids; i++)
 192			printf("%u%s", map_ids[i],
 193			       i == info.nr_map_ids - 1 ? "" : ",");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 194	}
 
 
 
 
 195}
 196
 197static void print_prog_json(struct bpf_prog_info *info, int fd)
 198{
 199	char *memlock;
 200
 201	jsonw_start_object(json_wtr);
 202	jsonw_uint_field(json_wtr, "id", info->id);
 203	if (info->type < ARRAY_SIZE(prog_type_name))
 204		jsonw_string_field(json_wtr, "type",
 205				   prog_type_name[info->type]);
 206	else
 207		jsonw_uint_field(json_wtr, "type", info->type);
 208
 209	if (*info->name)
 210		jsonw_string_field(json_wtr, "name", info->name);
 211
 212	jsonw_name(json_wtr, "tag");
 213	jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
 214		     info->tag[0], info->tag[1], info->tag[2], info->tag[3],
 215		     info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
 216
 217	jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
 218	if (info->run_time_ns) {
 219		jsonw_uint_field(json_wtr, "run_time_ns", info->run_time_ns);
 220		jsonw_uint_field(json_wtr, "run_cnt", info->run_cnt);
 221	}
 
 
 
 222
 
 
 
 
 
 
 223	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
 224
 225	if (info->load_time) {
 226		char buf[32];
 227
 228		print_boot_time(info->load_time, buf, sizeof(buf));
 229
 230		/* Piggy back on load_time, since 0 uid is a valid one */
 231		jsonw_name(json_wtr, "loaded_at");
 232		jsonw_printf(json_wtr, "%s", buf);
 233		jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
 234	}
 235
 236	jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
 237
 238	if (info->jited_prog_len) {
 239		jsonw_bool_field(json_wtr, "jited", true);
 240		jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
 241	} else {
 242		jsonw_bool_field(json_wtr, "jited", false);
 243	}
 244
 245	memlock = get_fdinfo(fd, "memlock");
 246	if (memlock)
 247		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
 248	free(memlock);
 249
 250	if (info->nr_map_ids)
 251		show_prog_maps(fd, info->nr_map_ids);
 252
 253	if (info->btf_id)
 254		jsonw_int_field(json_wtr, "btf_id", info->btf_id);
 255
 256	if (!hash_empty(prog_table.table)) {
 257		struct pinned_obj *obj;
 258
 259		jsonw_name(json_wtr, "pinned");
 260		jsonw_start_array(json_wtr);
 261		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
 262			if (obj->id == info->id)
 263				jsonw_string(json_wtr, obj->path);
 264		}
 265		jsonw_end_array(json_wtr);
 266	}
 267
 
 
 
 
 268	jsonw_end_object(json_wtr);
 269}
 270
 271static void print_prog_plain(struct bpf_prog_info *info, int fd)
 272{
 273	char *memlock;
 274
 275	printf("%u: ", info->id);
 276	if (info->type < ARRAY_SIZE(prog_type_name))
 277		printf("%s  ", prog_type_name[info->type]);
 278	else
 279		printf("type %u  ", info->type);
 280
 281	if (*info->name)
 282		printf("name %s  ", info->name);
 283
 284	printf("tag ");
 285	fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
 286	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
 287	printf("%s", info->gpl_compatible ? "  gpl" : "");
 288	if (info->run_time_ns)
 289		printf(" run_time_ns %lld run_cnt %lld",
 290		       info->run_time_ns, info->run_cnt);
 
 
 291	printf("\n");
 
 
 
 
 
 
 
 292
 293	if (info->load_time) {
 294		char buf[32];
 295
 296		print_boot_time(info->load_time, buf, sizeof(buf));
 297
 298		/* Piggy back on load_time, since 0 uid is a valid one */
 299		printf("\tloaded_at %s  uid %u\n", buf, info->created_by_uid);
 300	}
 301
 302	printf("\txlated %uB", info->xlated_prog_len);
 303
 304	if (info->jited_prog_len)
 305		printf("  jited %uB", info->jited_prog_len);
 306	else
 307		printf("  not jited");
 308
 309	memlock = get_fdinfo(fd, "memlock");
 310	if (memlock)
 311		printf("  memlock %sB", memlock);
 312	free(memlock);
 313
 314	if (info->nr_map_ids)
 315		show_prog_maps(fd, info->nr_map_ids);
 316
 317	if (!hash_empty(prog_table.table)) {
 318		struct pinned_obj *obj;
 319
 320		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
 321			if (obj->id == info->id)
 322				printf("\n\tpinned %s", obj->path);
 323		}
 324	}
 325
 326	if (info->btf_id)
 327		printf("\n\tbtf_id %d", info->btf_id);
 328
 
 
 329	printf("\n");
 
 
 330}
 331
 332static int show_prog(int fd)
 333{
 334	struct bpf_prog_info info = {};
 335	__u32 len = sizeof(info);
 336	int err;
 337
 338	err = bpf_obj_get_info_by_fd(fd, &info, &len);
 339	if (err) {
 340		p_err("can't get prog info: %s", strerror(errno));
 341		return -1;
 342	}
 343
 344	if (json_output)
 345		print_prog_json(&info, fd);
 346	else
 347		print_prog_plain(&info, fd);
 348
 349	return 0;
 350}
 351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 352static int do_show(int argc, char **argv)
 353{
 354	__u32 id = 0;
 355	int err;
 356	int fd;
 357
 358	if (show_pinned)
 359		build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
 
 360
 361	if (argc == 2) {
 362		fd = prog_parse_fd(&argc, &argv);
 363		if (fd < 0)
 364			return -1;
 365
 366		err = show_prog(fd);
 367		close(fd);
 368		return err;
 369	}
 370
 371	if (argc)
 372		return BAD_ARG();
 373
 374	if (json_output)
 375		jsonw_start_array(json_wtr);
 376	while (true) {
 377		err = bpf_prog_get_next_id(id, &id);
 378		if (err) {
 379			if (errno == ENOENT) {
 380				err = 0;
 381				break;
 382			}
 383			p_err("can't get next program: %s%s", strerror(errno),
 384			      errno == EINVAL ? " -- kernel too old?" : "");
 385			err = -1;
 386			break;
 387		}
 388
 389		fd = bpf_prog_get_fd_by_id(id);
 390		if (fd < 0) {
 391			if (errno == ENOENT)
 392				continue;
 393			p_err("can't get prog by id (%u): %s",
 394			      id, strerror(errno));
 395			err = -1;
 396			break;
 397		}
 398
 399		err = show_prog(fd);
 400		close(fd);
 401		if (err)
 402			break;
 403	}
 404
 405	if (json_output)
 406		jsonw_end_array(json_wtr);
 407
 
 
 408	return err;
 409}
 410
 411static int do_dump(int argc, char **argv)
 
 
 412{
 413	struct bpf_prog_info_linear *info_linear;
 414	struct bpf_prog_linfo *prog_linfo = NULL;
 415	enum {DUMP_JITED, DUMP_XLATED} mode;
 416	const char *disasm_opt = NULL;
 417	struct bpf_prog_info *info;
 418	struct dump_data dd = {};
 419	void *func_info = NULL;
 420	struct btf *btf = NULL;
 421	char *filepath = NULL;
 422	bool opcodes = false;
 423	bool visual = false;
 424	char func_sig[1024];
 425	unsigned char *buf;
 426	bool linum = false;
 427	__u32 member_len;
 428	__u64 arrays;
 429	ssize_t n;
 430	int fd;
 431
 432	if (is_prefix(*argv, "jited")) {
 433		if (disasm_init())
 434			return -1;
 435		mode = DUMP_JITED;
 436	} else if (is_prefix(*argv, "xlated")) {
 437		mode = DUMP_XLATED;
 438	} else {
 439		p_err("expected 'xlated' or 'jited', got: %s", *argv);
 440		return -1;
 441	}
 442	NEXT_ARG();
 443
 444	if (argc < 2)
 445		usage();
 446
 447	fd = prog_parse_fd(&argc, &argv);
 448	if (fd < 0)
 449		return -1;
 450
 451	if (is_prefix(*argv, "file")) {
 452		NEXT_ARG();
 453		if (!argc) {
 454			p_err("expected file path");
 455			return -1;
 456		}
 457
 458		filepath = *argv;
 459		NEXT_ARG();
 460	} else if (is_prefix(*argv, "opcodes")) {
 461		opcodes = true;
 462		NEXT_ARG();
 463	} else if (is_prefix(*argv, "visual")) {
 464		visual = true;
 465		NEXT_ARG();
 466	} else if (is_prefix(*argv, "linum")) {
 467		linum = true;
 468		NEXT_ARG();
 469	}
 470
 471	if (argc) {
 472		usage();
 473		return -1;
 474	}
 475
 476	if (mode == DUMP_JITED)
 477		arrays = 1UL << BPF_PROG_INFO_JITED_INSNS;
 478	else
 479		arrays = 1UL << BPF_PROG_INFO_XLATED_INSNS;
 480
 481	arrays |= 1UL << BPF_PROG_INFO_JITED_KSYMS;
 482	arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
 483	arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
 484	arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
 485	arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
 486
 487	info_linear = bpf_program__get_prog_info_linear(fd, arrays);
 488	close(fd);
 489	if (IS_ERR_OR_NULL(info_linear)) {
 490		p_err("can't get prog info: %s", strerror(errno));
 491		return -1;
 492	}
 493
 494	info = &info_linear->info;
 495	if (mode == DUMP_JITED) {
 496		if (info->jited_prog_len == 0) {
 497			p_info("no instructions returned");
 498			goto err_free;
 499		}
 500		buf = (unsigned char *)(info->jited_prog_insns);
 501		member_len = info->jited_prog_len;
 502	} else {	/* DUMP_XLATED */
 503		if (info->xlated_prog_len == 0) {
 504			p_err("error retrieving insn dump: kernel.kptr_restrict set?");
 505			goto err_free;
 506		}
 507		buf = (unsigned char *)info->xlated_prog_insns;
 508		member_len = info->xlated_prog_len;
 509	}
 510
 511	if (info->btf_id && btf__get_from_id(info->btf_id, &btf)) {
 512		p_err("failed to get btf");
 513		goto err_free;
 514	}
 515
 516	func_info = (void *)info->func_info;
 517
 518	if (info->nr_line_info) {
 519		prog_linfo = bpf_prog_linfo__new(info);
 520		if (!prog_linfo)
 521			p_info("error in processing bpf_line_info.  continue without it.");
 522	}
 523
 524	if (filepath) {
 525		fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
 526		if (fd < 0) {
 527			p_err("can't open file %s: %s", filepath,
 528			      strerror(errno));
 529			goto err_free;
 530		}
 531
 532		n = write(fd, buf, member_len);
 533		close(fd);
 534		if (n != member_len) {
 535			p_err("error writing output file: %s",
 536			      n < 0 ? strerror(errno) : "short write");
 537			goto err_free;
 538		}
 539
 540		if (json_output)
 541			jsonw_null(json_wtr);
 542	} else if (mode == DUMP_JITED) {
 543		const char *name = NULL;
 544
 545		if (info->ifindex) {
 546			name = ifindex_to_bfd_params(info->ifindex,
 547						     info->netns_dev,
 548						     info->netns_ino,
 549						     &disasm_opt);
 550			if (!name)
 551				goto err_free;
 552		}
 553
 554		if (info->nr_jited_func_lens && info->jited_func_lens) {
 555			struct kernel_sym *sym = NULL;
 556			struct bpf_func_info *record;
 557			char sym_name[SYM_MAX_NAME];
 558			unsigned char *img = buf;
 559			__u64 *ksyms = NULL;
 560			__u32 *lens;
 561			__u32 i;
 562			if (info->nr_jited_ksyms) {
 563				kernel_syms_load(&dd);
 564				ksyms = (__u64 *) info->jited_ksyms;
 565			}
 566
 567			if (json_output)
 568				jsonw_start_array(json_wtr);
 569
 570			lens = (__u32 *) info->jited_func_lens;
 571			for (i = 0; i < info->nr_jited_func_lens; i++) {
 572				if (ksyms) {
 573					sym = kernel_syms_search(&dd, ksyms[i]);
 574					if (sym)
 575						sprintf(sym_name, "%s", sym->name);
 576					else
 577						sprintf(sym_name, "0x%016llx", ksyms[i]);
 578				} else {
 579					strcpy(sym_name, "unknown");
 580				}
 581
 582				if (func_info) {
 583					record = func_info + i * info->func_info_rec_size;
 584					btf_dumper_type_only(btf, record->type_id,
 585							     func_sig,
 586							     sizeof(func_sig));
 587				}
 588
 589				if (json_output) {
 590					jsonw_start_object(json_wtr);
 591					if (func_info && func_sig[0] != '\0') {
 592						jsonw_name(json_wtr, "proto");
 593						jsonw_string(json_wtr, func_sig);
 594					}
 595					jsonw_name(json_wtr, "name");
 596					jsonw_string(json_wtr, sym_name);
 597					jsonw_name(json_wtr, "insns");
 598				} else {
 599					if (func_info && func_sig[0] != '\0')
 600						printf("%s:\n", func_sig);
 601					printf("%s:\n", sym_name);
 602				}
 603
 604				disasm_print_insn(img, lens[i], opcodes,
 605						  name, disasm_opt, btf,
 606						  prog_linfo, ksyms[i], i,
 607						  linum);
 608
 609				img += lens[i];
 610
 611				if (json_output)
 612					jsonw_end_object(json_wtr);
 613				else
 614					printf("\n");
 615			}
 616
 617			if (json_output)
 618				jsonw_end_array(json_wtr);
 619		} else {
 620			disasm_print_insn(buf, member_len, opcodes, name,
 621					  disasm_opt, btf, NULL, 0, 0, false);
 622		}
 623	} else if (visual) {
 624		if (json_output)
 625			jsonw_null(json_wtr);
 626		else
 627			dump_xlated_cfg(buf, member_len);
 628	} else {
 629		kernel_syms_load(&dd);
 630		dd.nr_jited_ksyms = info->nr_jited_ksyms;
 631		dd.jited_ksyms = (__u64 *) info->jited_ksyms;
 632		dd.btf = btf;
 633		dd.func_info = func_info;
 634		dd.finfo_rec_size = info->func_info_rec_size;
 635		dd.prog_linfo = prog_linfo;
 636
 637		if (json_output)
 638			dump_xlated_json(&dd, buf, member_len, opcodes,
 639					 linum);
 640		else
 641			dump_xlated_plain(&dd, buf, member_len, opcodes,
 642					  linum);
 643		kernel_syms_destroy(&dd);
 644	}
 645
 646	free(info_linear);
 
 647	return 0;
 
 648
 649err_free:
 650	free(info_linear);
 651	return -1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 652}
 653
 654static int do_pin(int argc, char **argv)
 655{
 656	int err;
 657
 658	err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
 659	if (!err && json_output)
 660		jsonw_null(json_wtr);
 661	return err;
 662}
 663
 664struct map_replace {
 665	int idx;
 666	int fd;
 667	char *name;
 668};
 669
 670static int map_replace_compar(const void *p1, const void *p2)
 671{
 672	const struct map_replace *a = p1, *b = p2;
 673
 674	return a->idx - b->idx;
 675}
 676
 677static int parse_attach_detach_args(int argc, char **argv, int *progfd,
 678				    enum bpf_attach_type *attach_type,
 679				    int *mapfd)
 680{
 681	if (!REQ_ARGS(3))
 682		return -EINVAL;
 683
 684	*progfd = prog_parse_fd(&argc, &argv);
 685	if (*progfd < 0)
 686		return *progfd;
 687
 688	*attach_type = parse_attach_type(*argv);
 689	if (*attach_type == __MAX_BPF_ATTACH_TYPE) {
 690		p_err("invalid attach/detach type");
 691		return -EINVAL;
 692	}
 693
 694	if (*attach_type == BPF_FLOW_DISSECTOR) {
 695		*mapfd = -1;
 696		return 0;
 697	}
 698
 699	NEXT_ARG();
 700	if (!REQ_ARGS(2))
 701		return -EINVAL;
 702
 703	*mapfd = map_parse_fd(&argc, &argv);
 704	if (*mapfd < 0)
 705		return *mapfd;
 706
 707	return 0;
 708}
 709
 710static int do_attach(int argc, char **argv)
 711{
 712	enum bpf_attach_type attach_type;
 713	int err, progfd;
 714	int mapfd;
 715
 716	err = parse_attach_detach_args(argc, argv,
 717				       &progfd, &attach_type, &mapfd);
 718	if (err)
 719		return err;
 720
 721	err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
 722	if (err) {
 723		p_err("failed prog attach to map");
 724		return -EINVAL;
 725	}
 726
 727	if (json_output)
 728		jsonw_null(json_wtr);
 729	return 0;
 730}
 731
 732static int do_detach(int argc, char **argv)
 733{
 734	enum bpf_attach_type attach_type;
 735	int err, progfd;
 736	int mapfd;
 737
 738	err = parse_attach_detach_args(argc, argv,
 739				       &progfd, &attach_type, &mapfd);
 740	if (err)
 741		return err;
 742
 743	err = bpf_prog_detach2(progfd, mapfd, attach_type);
 744	if (err) {
 745		p_err("failed prog detach from map");
 746		return -EINVAL;
 747	}
 748
 749	if (json_output)
 750		jsonw_null(json_wtr);
 751	return 0;
 752}
 753
 754static int check_single_stdin(char *file_data_in, char *file_ctx_in)
 755{
 756	if (file_data_in && file_ctx_in &&
 757	    !strcmp(file_data_in, "-") && !strcmp(file_ctx_in, "-")) {
 758		p_err("cannot use standard input for both data_in and ctx_in");
 759		return -1;
 760	}
 761
 762	return 0;
 763}
 764
 765static int get_run_data(const char *fname, void **data_ptr, unsigned int *size)
 766{
 767	size_t block_size = 256;
 768	size_t buf_size = block_size;
 769	size_t nb_read = 0;
 770	void *tmp;
 771	FILE *f;
 772
 773	if (!fname) {
 774		*data_ptr = NULL;
 775		*size = 0;
 776		return 0;
 777	}
 778
 779	if (!strcmp(fname, "-"))
 780		f = stdin;
 781	else
 782		f = fopen(fname, "r");
 783	if (!f) {
 784		p_err("failed to open %s: %s", fname, strerror(errno));
 785		return -1;
 786	}
 787
 788	*data_ptr = malloc(block_size);
 789	if (!*data_ptr) {
 790		p_err("failed to allocate memory for data_in/ctx_in: %s",
 791		      strerror(errno));
 792		goto err_fclose;
 793	}
 794
 795	while ((nb_read += fread(*data_ptr + nb_read, 1, block_size, f))) {
 796		if (feof(f))
 797			break;
 798		if (ferror(f)) {
 799			p_err("failed to read data_in/ctx_in from %s: %s",
 800			      fname, strerror(errno));
 801			goto err_free;
 802		}
 803		if (nb_read > buf_size - block_size) {
 804			if (buf_size == UINT32_MAX) {
 805				p_err("data_in/ctx_in is too long (max: %d)",
 806				      UINT32_MAX);
 807				goto err_free;
 808			}
 809			/* No space for fread()-ing next chunk; realloc() */
 810			buf_size *= 2;
 811			tmp = realloc(*data_ptr, buf_size);
 812			if (!tmp) {
 813				p_err("failed to reallocate data_in/ctx_in: %s",
 814				      strerror(errno));
 815				goto err_free;
 816			}
 817			*data_ptr = tmp;
 818		}
 819	}
 820	if (f != stdin)
 821		fclose(f);
 822
 823	*size = nb_read;
 824	return 0;
 825
 826err_free:
 827	free(*data_ptr);
 828	*data_ptr = NULL;
 829err_fclose:
 830	if (f != stdin)
 831		fclose(f);
 832	return -1;
 833}
 834
 835static void hex_print(void *data, unsigned int size, FILE *f)
 836{
 837	size_t i, j;
 838	char c;
 839
 840	for (i = 0; i < size; i += 16) {
 841		/* Row offset */
 842		fprintf(f, "%07zx\t", i);
 843
 844		/* Hexadecimal values */
 845		for (j = i; j < i + 16 && j < size; j++)
 846			fprintf(f, "%02x%s", *(uint8_t *)(data + j),
 847				j % 2 ? " " : "");
 848		for (; j < i + 16; j++)
 849			fprintf(f, "  %s", j % 2 ? " " : "");
 850
 851		/* ASCII values (if relevant), '.' otherwise */
 852		fprintf(f, "| ");
 853		for (j = i; j < i + 16 && j < size; j++) {
 854			c = *(char *)(data + j);
 855			if (c < ' ' || c > '~')
 856				c = '.';
 857			fprintf(f, "%c%s", c, j == i + 7 ? " " : "");
 858		}
 859
 860		fprintf(f, "\n");
 861	}
 862}
 863
 864static int
 865print_run_output(void *data, unsigned int size, const char *fname,
 866		 const char *json_key)
 867{
 868	size_t nb_written;
 869	FILE *f;
 870
 871	if (!fname)
 872		return 0;
 873
 874	if (!strcmp(fname, "-")) {
 875		f = stdout;
 876		if (json_output) {
 877			jsonw_name(json_wtr, json_key);
 878			print_data_json(data, size);
 879		} else {
 880			hex_print(data, size, f);
 881		}
 882		return 0;
 883	}
 884
 885	f = fopen(fname, "w");
 886	if (!f) {
 887		p_err("failed to open %s: %s", fname, strerror(errno));
 888		return -1;
 889	}
 890
 891	nb_written = fwrite(data, 1, size, f);
 892	fclose(f);
 893	if (nb_written != size) {
 894		p_err("failed to write output data/ctx: %s", strerror(errno));
 895		return -1;
 896	}
 897
 898	return 0;
 899}
 900
 901static int alloc_run_data(void **data_ptr, unsigned int size_out)
 902{
 903	*data_ptr = calloc(size_out, 1);
 904	if (!*data_ptr) {
 905		p_err("failed to allocate memory for output data/ctx: %s",
 906		      strerror(errno));
 907		return -1;
 908	}
 909
 910	return 0;
 911}
 912
 913static int do_run(int argc, char **argv)
 914{
 915	char *data_fname_in = NULL, *data_fname_out = NULL;
 916	char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
 917	struct bpf_prog_test_run_attr test_attr = {0};
 918	const unsigned int default_size = SZ_32K;
 919	void *data_in = NULL, *data_out = NULL;
 920	void *ctx_in = NULL, *ctx_out = NULL;
 921	unsigned int repeat = 1;
 922	int fd, err;
 923
 924	if (!REQ_ARGS(4))
 925		return -1;
 926
 927	fd = prog_parse_fd(&argc, &argv);
 928	if (fd < 0)
 929		return -1;
 930
 931	while (argc) {
 932		if (detect_common_prefix(*argv, "data_in", "data_out",
 933					 "data_size_out", NULL))
 934			return -1;
 935		if (detect_common_prefix(*argv, "ctx_in", "ctx_out",
 936					 "ctx_size_out", NULL))
 937			return -1;
 938
 939		if (is_prefix(*argv, "data_in")) {
 940			NEXT_ARG();
 941			if (!REQ_ARGS(1))
 942				return -1;
 943
 944			data_fname_in = GET_ARG();
 945			if (check_single_stdin(data_fname_in, ctx_fname_in))
 946				return -1;
 947		} else if (is_prefix(*argv, "data_out")) {
 948			NEXT_ARG();
 949			if (!REQ_ARGS(1))
 950				return -1;
 951
 952			data_fname_out = GET_ARG();
 953		} else if (is_prefix(*argv, "data_size_out")) {
 954			char *endptr;
 955
 956			NEXT_ARG();
 957			if (!REQ_ARGS(1))
 958				return -1;
 959
 960			test_attr.data_size_out = strtoul(*argv, &endptr, 0);
 961			if (*endptr) {
 962				p_err("can't parse %s as output data size",
 963				      *argv);
 964				return -1;
 965			}
 966			NEXT_ARG();
 967		} else if (is_prefix(*argv, "ctx_in")) {
 968			NEXT_ARG();
 969			if (!REQ_ARGS(1))
 970				return -1;
 971
 972			ctx_fname_in = GET_ARG();
 973			if (check_single_stdin(data_fname_in, ctx_fname_in))
 974				return -1;
 975		} else if (is_prefix(*argv, "ctx_out")) {
 976			NEXT_ARG();
 977			if (!REQ_ARGS(1))
 978				return -1;
 979
 980			ctx_fname_out = GET_ARG();
 981		} else if (is_prefix(*argv, "ctx_size_out")) {
 982			char *endptr;
 983
 984			NEXT_ARG();
 985			if (!REQ_ARGS(1))
 986				return -1;
 987
 988			test_attr.ctx_size_out = strtoul(*argv, &endptr, 0);
 989			if (*endptr) {
 990				p_err("can't parse %s as output context size",
 991				      *argv);
 992				return -1;
 993			}
 994			NEXT_ARG();
 995		} else if (is_prefix(*argv, "repeat")) {
 996			char *endptr;
 997
 998			NEXT_ARG();
 999			if (!REQ_ARGS(1))
1000				return -1;
1001
1002			repeat = strtoul(*argv, &endptr, 0);
1003			if (*endptr) {
1004				p_err("can't parse %s as repeat number",
1005				      *argv);
1006				return -1;
1007			}
1008			NEXT_ARG();
1009		} else {
1010			p_err("expected no more arguments, 'data_in', 'data_out', 'data_size_out', 'ctx_in', 'ctx_out', 'ctx_size_out' or 'repeat', got: '%s'?",
1011			      *argv);
1012			return -1;
1013		}
1014	}
1015
1016	err = get_run_data(data_fname_in, &data_in, &test_attr.data_size_in);
1017	if (err)
1018		return -1;
1019
1020	if (data_in) {
1021		if (!test_attr.data_size_out)
1022			test_attr.data_size_out = default_size;
1023		err = alloc_run_data(&data_out, test_attr.data_size_out);
1024		if (err)
1025			goto free_data_in;
1026	}
1027
1028	err = get_run_data(ctx_fname_in, &ctx_in, &test_attr.ctx_size_in);
1029	if (err)
1030		goto free_data_out;
1031
1032	if (ctx_in) {
1033		if (!test_attr.ctx_size_out)
1034			test_attr.ctx_size_out = default_size;
1035		err = alloc_run_data(&ctx_out, test_attr.ctx_size_out);
1036		if (err)
1037			goto free_ctx_in;
1038	}
1039
1040	test_attr.prog_fd	= fd;
1041	test_attr.repeat	= repeat;
1042	test_attr.data_in	= data_in;
1043	test_attr.data_out	= data_out;
1044	test_attr.ctx_in	= ctx_in;
1045	test_attr.ctx_out	= ctx_out;
1046
1047	err = bpf_prog_test_run_xattr(&test_attr);
1048	if (err) {
1049		p_err("failed to run program: %s", strerror(errno));
1050		goto free_ctx_out;
1051	}
1052
1053	err = 0;
1054
1055	if (json_output)
1056		jsonw_start_object(json_wtr);	/* root */
1057
1058	/* Do not exit on errors occurring when printing output data/context,
1059	 * we still want to print return value and duration for program run.
1060	 */
1061	if (test_attr.data_size_out)
1062		err += print_run_output(test_attr.data_out,
1063					test_attr.data_size_out,
1064					data_fname_out, "data_out");
1065	if (test_attr.ctx_size_out)
1066		err += print_run_output(test_attr.ctx_out,
1067					test_attr.ctx_size_out,
1068					ctx_fname_out, "ctx_out");
1069
1070	if (json_output) {
1071		jsonw_uint_field(json_wtr, "retval", test_attr.retval);
1072		jsonw_uint_field(json_wtr, "duration", test_attr.duration);
1073		jsonw_end_object(json_wtr);	/* root */
1074	} else {
1075		fprintf(stdout, "Return value: %u, duration%s: %uns\n",
1076			test_attr.retval,
1077			repeat > 1 ? " (average)" : "", test_attr.duration);
1078	}
1079
1080free_ctx_out:
1081	free(ctx_out);
1082free_ctx_in:
1083	free(ctx_in);
1084free_data_out:
1085	free(data_out);
1086free_data_in:
1087	free(data_in);
1088
1089	return err;
1090}
1091
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1092static int load_with_options(int argc, char **argv, bool first_prog_only)
1093{
 
 
 
 
1094	struct bpf_object_load_attr load_attr = { 0 };
1095	struct bpf_object_open_attr open_attr = {
1096		.prog_type = BPF_PROG_TYPE_UNSPEC,
1097	};
1098	enum bpf_attach_type expected_attach_type;
1099	struct map_replace *map_replace = NULL;
1100	struct bpf_program *prog = NULL, *pos;
1101	unsigned int old_map_fds = 0;
1102	const char *pinmaps = NULL;
1103	struct bpf_object *obj;
1104	struct bpf_map *map;
1105	const char *pinfile;
1106	unsigned int i, j;
1107	__u32 ifindex = 0;
 
1108	int idx, err;
1109
 
1110	if (!REQ_ARGS(2))
1111		return -1;
1112	open_attr.file = GET_ARG();
1113	pinfile = GET_ARG();
1114
1115	while (argc) {
1116		if (is_prefix(*argv, "type")) {
1117			char *type;
1118
1119			NEXT_ARG();
1120
1121			if (open_attr.prog_type != BPF_PROG_TYPE_UNSPEC) {
1122				p_err("program type already specified");
1123				goto err_free_reuse_maps;
1124			}
1125			if (!REQ_ARGS(1))
1126				goto err_free_reuse_maps;
1127
1128			/* Put a '/' at the end of type to appease libbpf */
1129			type = malloc(strlen(*argv) + 2);
1130			if (!type) {
1131				p_err("mem alloc failed");
1132				goto err_free_reuse_maps;
1133			}
1134			*type = 0;
1135			strcat(type, *argv);
1136			strcat(type, "/");
1137
1138			err = libbpf_prog_type_by_name(type,
1139						       &open_attr.prog_type,
1140						       &expected_attach_type);
1141			free(type);
1142			if (err < 0)
1143				goto err_free_reuse_maps;
1144
1145			NEXT_ARG();
1146		} else if (is_prefix(*argv, "map")) {
1147			void *new_map_replace;
1148			char *endptr, *name;
1149			int fd;
1150
1151			NEXT_ARG();
1152
1153			if (!REQ_ARGS(4))
1154				goto err_free_reuse_maps;
1155
1156			if (is_prefix(*argv, "idx")) {
1157				NEXT_ARG();
1158
1159				idx = strtoul(*argv, &endptr, 0);
1160				if (*endptr) {
1161					p_err("can't parse %s as IDX", *argv);
1162					goto err_free_reuse_maps;
1163				}
1164				name = NULL;
1165			} else if (is_prefix(*argv, "name")) {
1166				NEXT_ARG();
1167
1168				name = *argv;
1169				idx = -1;
1170			} else {
1171				p_err("expected 'idx' or 'name', got: '%s'?",
1172				      *argv);
1173				goto err_free_reuse_maps;
1174			}
1175			NEXT_ARG();
1176
1177			fd = map_parse_fd(&argc, &argv);
1178			if (fd < 0)
1179				goto err_free_reuse_maps;
1180
1181			new_map_replace = reallocarray(map_replace,
1182						       old_map_fds + 1,
1183						       sizeof(*map_replace));
1184			if (!new_map_replace) {
1185				p_err("mem alloc failed");
1186				goto err_free_reuse_maps;
1187			}
1188			map_replace = new_map_replace;
1189
1190			map_replace[old_map_fds].idx = idx;
1191			map_replace[old_map_fds].name = name;
1192			map_replace[old_map_fds].fd = fd;
1193			old_map_fds++;
1194		} else if (is_prefix(*argv, "dev")) {
1195			NEXT_ARG();
1196
1197			if (ifindex) {
1198				p_err("offload device already specified");
1199				goto err_free_reuse_maps;
1200			}
1201			if (!REQ_ARGS(1))
1202				goto err_free_reuse_maps;
1203
1204			ifindex = if_nametoindex(*argv);
1205			if (!ifindex) {
1206				p_err("unrecognized netdevice '%s': %s",
1207				      *argv, strerror(errno));
1208				goto err_free_reuse_maps;
1209			}
1210			NEXT_ARG();
1211		} else if (is_prefix(*argv, "pinmaps")) {
1212			NEXT_ARG();
1213
1214			if (!REQ_ARGS(1))
1215				goto err_free_reuse_maps;
1216
1217			pinmaps = GET_ARG();
1218		} else {
1219			p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
1220			      *argv);
1221			goto err_free_reuse_maps;
1222		}
1223	}
1224
1225	set_max_rlimit();
1226
1227	obj = __bpf_object__open_xattr(&open_attr, bpf_flags);
1228	if (IS_ERR_OR_NULL(obj)) {
1229		p_err("failed to open object file");
1230		goto err_free_reuse_maps;
1231	}
1232
1233	bpf_object__for_each_program(pos, obj) {
1234		enum bpf_prog_type prog_type = open_attr.prog_type;
1235
1236		if (open_attr.prog_type == BPF_PROG_TYPE_UNSPEC) {
1237			const char *sec_name = bpf_program__title(pos, false);
1238
1239			err = libbpf_prog_type_by_name(sec_name, &prog_type,
1240						       &expected_attach_type);
1241			if (err < 0)
1242				goto err_close_obj;
1243		}
1244
1245		bpf_program__set_ifindex(pos, ifindex);
1246		bpf_program__set_type(pos, prog_type);
1247		bpf_program__set_expected_attach_type(pos, expected_attach_type);
1248	}
1249
1250	qsort(map_replace, old_map_fds, sizeof(*map_replace),
1251	      map_replace_compar);
1252
1253	/* After the sort maps by name will be first on the list, because they
1254	 * have idx == -1.  Resolve them.
1255	 */
1256	j = 0;
1257	while (j < old_map_fds && map_replace[j].name) {
1258		i = 0;
1259		bpf_object__for_each_map(map, obj) {
1260			if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
1261				map_replace[j].idx = i;
1262				break;
1263			}
1264			i++;
1265		}
1266		if (map_replace[j].idx == -1) {
1267			p_err("unable to find map '%s'", map_replace[j].name);
1268			goto err_close_obj;
1269		}
1270		j++;
1271	}
1272	/* Resort if any names were resolved */
1273	if (j)
1274		qsort(map_replace, old_map_fds, sizeof(*map_replace),
1275		      map_replace_compar);
1276
1277	/* Set ifindex and name reuse */
1278	j = 0;
1279	idx = 0;
1280	bpf_object__for_each_map(map, obj) {
1281		if (!bpf_map__is_offload_neutral(map))
1282			bpf_map__set_ifindex(map, ifindex);
1283
1284		if (j < old_map_fds && idx == map_replace[j].idx) {
1285			err = bpf_map__reuse_fd(map, map_replace[j++].fd);
1286			if (err) {
1287				p_err("unable to set up map reuse: %d", err);
1288				goto err_close_obj;
1289			}
1290
1291			/* Next reuse wants to apply to the same map */
1292			if (j < old_map_fds && map_replace[j].idx == idx) {
1293				p_err("replacement for map idx %d specified more than once",
1294				      idx);
1295				goto err_close_obj;
1296			}
1297		}
1298
1299		idx++;
1300	}
1301	if (j < old_map_fds) {
1302		p_err("map idx '%d' not used", map_replace[j].idx);
1303		goto err_close_obj;
1304	}
1305
1306	load_attr.obj = obj;
1307	if (verifier_logs)
1308		/* log_level1 + log_level2 + stats, but not stable UAPI */
1309		load_attr.log_level = 1 + 2 + 4;
1310
1311	err = bpf_object__load_xattr(&load_attr);
1312	if (err) {
1313		p_err("failed to load object file");
1314		goto err_close_obj;
1315	}
1316
1317	err = mount_bpffs_for_pin(pinfile);
1318	if (err)
1319		goto err_close_obj;
1320
1321	if (first_prog_only) {
1322		prog = bpf_program__next(NULL, obj);
1323		if (!prog) {
1324			p_err("object file doesn't contain any bpf program");
1325			goto err_close_obj;
1326		}
1327
1328		err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
1329		if (err) {
1330			p_err("failed to pin program %s",
1331			      bpf_program__title(prog, false));
1332			goto err_close_obj;
1333		}
1334	} else {
1335		err = bpf_object__pin_programs(obj, pinfile);
1336		if (err) {
1337			p_err("failed to pin all programs");
1338			goto err_close_obj;
1339		}
1340	}
1341
1342	if (pinmaps) {
1343		err = bpf_object__pin_maps(obj, pinmaps);
1344		if (err) {
1345			p_err("failed to pin all maps");
1346			goto err_unpin;
1347		}
1348	}
1349
1350	if (json_output)
1351		jsonw_null(json_wtr);
1352
1353	bpf_object__close(obj);
1354	for (i = 0; i < old_map_fds; i++)
1355		close(map_replace[i].fd);
1356	free(map_replace);
1357
1358	return 0;
1359
1360err_unpin:
1361	if (first_prog_only)
1362		unlink(pinfile);
1363	else
1364		bpf_object__unpin_programs(obj, pinfile);
1365err_close_obj:
1366	bpf_object__close(obj);
1367err_free_reuse_maps:
1368	for (i = 0; i < old_map_fds; i++)
1369		close(map_replace[i].fd);
1370	free(map_replace);
1371	return -1;
1372}
1373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1374static int do_load(int argc, char **argv)
1375{
 
 
1376	return load_with_options(argc, argv, true);
1377}
1378
1379static int do_loadall(int argc, char **argv)
1380{
1381	return load_with_options(argc, argv, false);
1382}
1383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1384static int do_help(int argc, char **argv)
1385{
1386	if (json_output) {
1387		jsonw_null(json_wtr);
1388		return 0;
1389	}
1390
1391	fprintf(stderr,
1392		"Usage: %s %s { show | list } [PROG]\n"
1393		"       %s %s dump xlated PROG [{ file FILE | opcodes | visual | linum }]\n"
1394		"       %s %s dump jited  PROG [{ file FILE | opcodes | linum }]\n"
1395		"       %s %s pin   PROG FILE\n"
1396		"       %s %s { load | loadall } OBJ  PATH \\\n"
1397		"                         [type TYPE] [dev NAME] \\\n"
1398		"                         [map { idx IDX | name NAME } MAP]\\\n"
1399		"                         [pinmaps MAP_DIR]\n"
1400		"       %s %s attach PROG ATTACH_TYPE [MAP]\n"
1401		"       %s %s detach PROG ATTACH_TYPE [MAP]\n"
1402		"       %s %s run PROG \\\n"
1403		"                         data_in FILE \\\n"
1404		"                         [data_out FILE [data_size_out L]] \\\n"
1405		"                         [ctx_in FILE [ctx_out FILE [ctx_size_out M]]] \\\n"
1406		"                         [repeat N]\n"
1407		"       %s %s tracelog\n"
1408		"       %s %s help\n"
 
1409		"\n"
1410		"       " HELP_SPEC_MAP "\n"
1411		"       " HELP_SPEC_PROGRAM "\n"
1412		"       TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
1413		"                 tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
1414		"                 cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
1415		"                 lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
1416		"                 sk_reuseport | flow_dissector | cgroup/sysctl |\n"
1417		"                 cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
1418		"                 cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
1419		"                 cgroup/sendmsg4 | cgroup/sendmsg6 | cgroup/recvmsg4 |\n"
1420		"                 cgroup/recvmsg6 | cgroup/getsockopt |\n"
1421		"                 cgroup/setsockopt }\n"
 
 
1422		"       ATTACH_TYPE := { msg_verdict | stream_verdict | stream_parser |\n"
1423		"                        flow_dissector }\n"
 
1424		"       " HELP_SPEC_OPTIONS "\n"
1425		"",
1426		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1427		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1428		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1429		bin_name, argv[-2]);
1430
1431	return 0;
1432}
1433
1434static const struct cmd cmds[] = {
1435	{ "show",	do_show },
1436	{ "list",	do_show },
1437	{ "help",	do_help },
1438	{ "dump",	do_dump },
1439	{ "pin",	do_pin },
1440	{ "load",	do_load },
1441	{ "loadall",	do_loadall },
1442	{ "attach",	do_attach },
1443	{ "detach",	do_detach },
1444	{ "tracelog",	do_tracelog },
1445	{ "run",	do_run },
 
1446	{ 0 }
1447};
1448
1449int do_prog(int argc, char **argv)
1450{
1451	return cmd_select(cmds, argc, argv, do_help);
1452}