1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Guest agent for virtio-trace
4 *
5 * Copyright (C) 2012 Hitachi, Ltd.
6 * Created by Yoshihiro Yunomae <yoshihiro.yunomae.ez@hitachi.com>
7 *            Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
8 */
9
10#define _GNU_SOURCE
11#include <limits.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include "trace-agent.h"
16
17#define PAGE_SIZE		(sysconf(_SC_PAGE_SIZE))
18#define PIPE_DEF_BUFS		16
19#define PIPE_MIN_SIZE		(PAGE_SIZE*PIPE_DEF_BUFS)
20#define PIPE_MAX_SIZE		(1024*1024)
21#define READ_PATH_FMT	\
22		"/sys/kernel/debug/tracing/per_cpu/cpu%d/trace_pipe_raw"
23#define WRITE_PATH_FMT		"/dev/virtio-ports/trace-path-cpu%d"
24#define CTL_PATH		"/dev/virtio-ports/agent-ctl-path"
25
26pthread_mutex_t mutex_notify = PTHREAD_MUTEX_INITIALIZER;
27pthread_cond_t cond_wakeup = PTHREAD_COND_INITIALIZER;
28
29static int get_total_cpus(void)
30{
31	int nr_cpus = (int)sysconf(_SC_NPROCESSORS_CONF);
32
33	if (nr_cpus <= 0) {
34		pr_err("Could not read cpus\n");
35		goto error;
36	} else if (nr_cpus > MAX_CPUS) {
37		pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS);
38		goto error;
39	}
40
41	return nr_cpus;
42
43error:
44	exit(EXIT_FAILURE);
45}
46
47static void *agent_info_new(void)
48{
49	struct agent_info *s;
50	int i;
51
52	s = zalloc(sizeof(struct agent_info));
53	if (s == NULL) {
54		pr_err("agent_info zalloc error\n");
55		exit(EXIT_FAILURE);
56	}
57
58	s->pipe_size = PIPE_INIT;
59	s->use_stdout = false;
60	s->cpus = get_total_cpus();
61	s->ctl_fd = -1;
62
63	/* read/write threads init */
64	for (i = 0; i < s->cpus; i++)
65		s->rw_ti[i] = rw_thread_info_new();
66
67	return s;
68}
69
70static unsigned long parse_size(const char *arg)
71{
72	unsigned long value, round;
73	char *ptr;
74
75	value = strtoul(arg, &ptr, 10);
76	switch (*ptr) {
77	case 'K': case 'k':
78		value <<= 10;
79		break;
80	case 'M': case 'm':
81		value <<= 20;
82		break;
83	default:
84		break;
85	}
86
87	if (value > PIPE_MAX_SIZE) {
88		pr_err("Pipe size must be less than 1MB\n");
89		goto error;
90	} else if (value < PIPE_MIN_SIZE) {
91		pr_err("Pipe size must be over 64KB\n");
92		goto error;
93	}
94
95	/* Align buffer size with page unit */
96	round = value & (PAGE_SIZE - 1);
97	value = value - round;
98
99	return value;
100error:
101	return 0;
102}
103
104static void usage(char const *prg)
105{
106	pr_err("usage: %s [-h] [-o] [-s <size of pipe>]\n", prg);
107}
108
109static const char *make_path(int cpu_num, bool this_is_write_path)
110{
111	int ret;
112	char *buf;
113
114	buf = zalloc(PATH_MAX);
115	if (buf == NULL) {
116		pr_err("Could not allocate buffer\n");
117		goto error;
118	}
119
120	if (this_is_write_path)
121		/* write(output) path */
122		ret = snprintf(buf, PATH_MAX, WRITE_PATH_FMT, cpu_num);
123	else
124		/* read(input) path */
125		ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, cpu_num);
126
127	if (ret <= 0) {
128		pr_err("Failed to generate %s path(CPU#%d):%d\n",
129			this_is_write_path ? "read" : "write", cpu_num, ret);
130		goto error;
131	}
132
133	return buf;
134
135error:
136	free(buf);
137	return NULL;
138}
139
140static const char *make_input_path(int cpu_num)
141{
142	return make_path(cpu_num, false);
143}
144
145static const char *make_output_path(int cpu_num)
146{
147	return make_path(cpu_num, true);
148}
149
150static void *agent_info_init(struct agent_info *s)
151{
152	int cpu;
153	const char *in_path = NULL;
154	const char *out_path = NULL;
155
156	/* init read/write threads */
157	for (cpu = 0; cpu < s->cpus; cpu++) {
158		/* set read(input) path per read/write thread */
159		in_path = make_input_path(cpu);
160		if (in_path == NULL)
161			goto error;
162
163		/* set write(output) path per read/write thread*/
164		if (!s->use_stdout) {
165			out_path = make_output_path(cpu);
166			if (out_path == NULL)
167				goto error;
168		} else
169			/* stdout mode */
170			pr_debug("stdout mode\n");
171
172		rw_thread_init(cpu, in_path, out_path, s->use_stdout,
173						s->pipe_size, s->rw_ti[cpu]);
174	}
175
176	/* init controller of read/write threads */
177	s->ctl_fd = rw_ctl_init((const char *)CTL_PATH);
178
179	return NULL;
180
181error:
182	exit(EXIT_FAILURE);
183}
184
185static void *parse_args(int argc, char *argv[], struct agent_info *s)
186{
187	int cmd;
188	unsigned long size;
189
190	while ((cmd = getopt(argc, argv, "hos:")) != -1) {
191		switch (cmd) {
192		/* stdout mode */
193		case 'o':
194			s->use_stdout = true;
195			break;
196		/* size of pipe */
197		case 's':
198			size = parse_size(optarg);
199			if (size == 0)
200				goto error;
201			s->pipe_size = size;
202			break;
203		case 'h':
204		default:
205			usage(argv[0]);
206			goto error;
207		}
208	}
209
210	agent_info_init(s);
211
212	return NULL;
213
214error:
215	exit(EXIT_FAILURE);
216}
217
218static void agent_main_loop(struct agent_info *s)
219{
220	int cpu;
221	pthread_t rw_thread_per_cpu[MAX_CPUS];
222
223	/* Start all read/write threads */
224	for (cpu = 0; cpu < s->cpus; cpu++)
225		rw_thread_per_cpu[cpu] = rw_thread_run(s->rw_ti[cpu]);
226
227	rw_ctl_loop(s->ctl_fd);
228
229	/* Finish all read/write threads */
230	for (cpu = 0; cpu < s->cpus; cpu++) {
231		int ret;
232
233		ret = pthread_join(rw_thread_per_cpu[cpu], NULL);
234		if (ret != 0) {
235			pr_err("pthread_join() error:%d (cpu %d)\n", ret, cpu);
236			exit(EXIT_FAILURE);
237		}
238	}
239}
240
241static void agent_info_free(struct agent_info *s)
242{
243	int i;
244
245	close(s->ctl_fd);
246	for (i = 0; i < s->cpus; i++) {
247		close(s->rw_ti[i]->in_fd);
248		close(s->rw_ti[i]->out_fd);
249		close(s->rw_ti[i]->read_pipe);
250		close(s->rw_ti[i]->write_pipe);
251		free(s->rw_ti[i]);
252	}
253	free(s);
254}
255
256int main(int argc, char *argv[])
257{
258	struct agent_info *s = NULL;
259
260	s = agent_info_new();
261	parse_args(argc, argv, s);
262
263	agent_main_loop(s);
264
265	agent_info_free(s);
266
267	return 0;
268}
269