Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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