summaryrefslogtreecommitdiff
path: root/tests/common.c
blob: 2155280797638dfc1599aa38d3b8b91c358291cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "common.h"
#include <stdarg.h>
#include <errno.h>
#include <err.h>
#include <signal.h>
#include <sched.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

static pid_t latency_pid = -1;
static pid_t server_pid = -1;
static bool cleanup_registered = false;

static void
cleanup_atexit(void)
{
	if (latency_pid != -1)
		kill(SIGKILL, latency_pid);
	if (server_pid != -1)
		kill(SIGKILL, server_pid);
}

static void
register_cleanup(void)
{
	if (cleanup_registered)
		return;
	atexit(cleanup_atexit);
	cleanup_registered = true;
}

bool
latency_running(void)
{
	int socktrue = 1;
	sockaddr_all addr;

	int sock = socket(AF_INET, SOCK_DGRAM, 0);
	assert(sock >= 0);
	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &socktrue, sizeof(socktrue));
	setup_addr(&addr, "192.168.127.0", 0);
	bool res;
	if (bind(sock, &addr.generic, sizeof(addr)) == 0) {
		res = true;
	} else {
		assert(errno == EADDRNOTAVAIL);
		res = false;
	}
	close(sock);
	return res;
}

static int
get_process_netns(pid_t pid)
{
	char ns_path[128];
	sprintf(ns_path, "/proc/%u/ns/net", (unsigned) pid);
	int ns = open(ns_path, O_RDONLY);
	assert(ns >= 0);
	return ns;
}

static void
launch_latency_client(bool local, const char *fmt, va_list ap)
{
	register_cleanup();
	assert(latency_pid == -1);

	while (latency_running()) {
		system("killall latency > /dev/null 2> /dev/null");
		usleep(2000);
	}

	char cmd[1024];

	if (local)
		strcpy(cmd, "exec ../latency ");
	else
		strcpy(cmd, "exec ../latency --client 192.168.128.2 ");
	size_t cmd_len = strlen(cmd);

	vsnprintf(cmd + cmd_len, sizeof(cmd) - cmd_len, fmt, ap);

	printf("starting %s\n", cmd);
	latency_pid = fork();
	assert(latency_pid != -1);

	if (latency_pid == 0) {
		execlp("sh", "sh", "-c", cmd, NULL);
		kill(getpid(), SIGKILL);
		exit(1);
	}

	int count;
	while (!latency_running()) {
		usleep(2000);
		assert(++count < 100);
	}
	usleep(50000);
}

void
launch_latency(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	launch_latency_client(true, fmt, ap);
	va_end(ap);
}

void
launch_latency_remote(const char *fmt, ...)
{
	register_cleanup();
	assert(latency_pid == -1 && server_pid == -1);

	if (unshare(CLONE_NEWNET))
		err(1, "unshare");
	assert(system("ifconfig lo 127.0.0.1 netmask 255.255.255.0 up") == 0);

	int pipe_fds[2];

	assert(pipe(pipe_fds) == 0);

	server_pid = fork();
	assert(server_pid != -1);

	char c;
	if (server_pid == 0) {
		char cmd[256];

		close(pipe_fds[0]);
		pipe_fds[0] = -1;

		if (unshare(CLONE_NEWNET))
			err(1, "unshare");
		assert(system("ifconfig lo 127.0.0.1 netmask 255.255.255.0 up") == 0);
		sprintf(cmd, "ip link add server type veth peer name client netns %d", (int) getppid());
		assert(system(cmd) == 0);
		assert(system("ifconfig server 192.168.128.2 netmask 255.255.255.0 up") == 0);
		c = 'x';
		write(pipe_fds[1], &c, 1);
		close(pipe_fds[1]);
		execl("../latency", "latency", "--server", "--framing-bytes", "0", NULL);
		exit(1);
	}

	close(pipe_fds[1]);
	pipe_fds[1] = -1;
	assert(read(pipe_fds[0], &c, 1) == 1);
	assert(c == 'x');
	assert(system("ifconfig client 192.168.128.3 netmask 255.255.255.0 up") == 0);

	va_list ap;
	va_start(ap, fmt);
	launch_latency_client(false, fmt, ap);
	va_end(ap);
}

static void
handle_alarm(int sig)
{
}

static void
kill_pid(pid_t *p_pid)
{
	assert(*p_pid != -1);
	kill(*p_pid, SIGTERM);

	// handle some timeout
	signal(SIGALRM, handle_alarm);
	alarm(1);

	int status;
	pid_t pid = waitpid(*p_pid, &status, 0);
	alarm(0);
	signal(SIGALRM, SIG_DFL);

	assert(pid == *p_pid);
	assert(WIFEXITED(status));
	assert(WEXITSTATUS(status) == 0);

	*p_pid = -1;
}

void
kill_latency(void)
{
	kill_pid(&latency_pid);
	if (server_pid != -1) {
		kill_pid(&server_pid);

		int parent_ns = get_process_netns(getppid());
		assert(setns(parent_ns, 0) == 0);
		close(parent_ns);
	}
}

void
setup_addr(sockaddr_all *addr, const char *ip, int port)
{
	struct in_addr inet;
	assert(inet_aton(ip, &inet) == 1);

	addr->inet.sin_family = AF_INET;
	addr->inet.sin_addr = inet;
	addr->inet.sin_port = htons(port);
}

void
create_udp_pair(int socks[2])
{
	int sock;
	int socktrue = 1;
	sockaddr_all addr;
	socklen_t addr_len;
	int ports[2];

	sock = socket(AF_INET, SOCK_DGRAM, 0);
	assert(sock >= 0);
	socks[0] = sock;
	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &socktrue, sizeof(socktrue));
	setup_addr(&addr, "192.168.127.0", 0);
	assert(bind(sock, &addr.generic, sizeof(addr)) == 0);
	addr_len = sizeof(addr);
	assert(getsockname(sock, &addr.generic, &addr_len) == 0);
	ports[0] = ntohs(addr.inet.sin_port);

	const char *remote_listen_ip = "192.168.127.2";
	const char *remote_connect_ip = "192.168.127.3";

	int save_ns = -1;
	if (server_pid != -1) {
		save_ns = get_process_netns(getpid());

		int server_ns = get_process_netns(server_pid);
		assert(setns(server_ns, 0) == 0);
		close(server_ns);

		remote_listen_ip = "192.168.127.0";
		remote_connect_ip = "192.168.127.1";
	}

	sock = socket(AF_INET, SOCK_DGRAM, 0);
	assert(sock >= 0);
	socks[1] = sock;
	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &socktrue, sizeof(socktrue));
	setup_addr(&addr, remote_listen_ip, 0);
	assert(bind(sock, &addr.generic, sizeof(addr)) == 0);
	addr_len = sizeof(addr);
	assert(getsockname(sock, &addr.generic, &addr_len) == 0);
	ports[1] = ntohs(addr.inet.sin_port);

	if (save_ns != -1) {
		assert(setns(save_ns, 0) == 0);
		close(save_ns);
	}

	// make a pair from the above ones
	setup_addr(&addr, "192.168.127.1", ports[1]);
	assert(connect(socks[0], &addr.generic, sizeof(addr)) == 0);
	setup_addr(&addr, remote_connect_ip, ports[0]);
	assert(connect(socks[1], &addr.generic, sizeof(addr)) == 0);
}

void
close_udp_pair(int socks[2])
{
	close(socks[0]);
	close(socks[1]);
	socks[0] = socks[1] = -1;
}