aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/vsock/util.c
blob: 6026ef3ce51222c89f7e428d4988c2bd3bfeedf6 (plain) (blame)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// SPDX-License-Identifier: GPL-2.0-only
/*
 * vsock test utilities
 *
 * Copyright (C) 2017 Red Hat, Inc.
 *
 * Author: Stefan Hajnoczi <stefanha@redhat.com>
 */

#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

#include "timeout.h"
#include "control.h"
#include "util.h"

/* Install signal handlers */
void init_signals(void)
{
	struct sigaction act = {
		.sa_handler = sigalrm,
	};

	sigaction(SIGALRM, &act, NULL);
	signal(SIGPIPE, SIG_IGN);
}

/* Parse a CID in string representation */
unsigned int parse_cid(const char *str)
{
	char *endptr = NULL;
	unsigned long n;

	errno = 0;
	n = strtoul(str, &endptr, 10);
	if (errno || *endptr != '\0') {
		fprintf(stderr, "malformed CID \"%s\"\n", str);
		exit(EXIT_FAILURE);
	}
	return n;
}

/* Connect to <cid, port> and return the file descriptor. */
int vsock_stream_connect(unsigned int cid, unsigned int port)
{
	union {
		struct sockaddr sa;
		struct sockaddr_vm svm;
	} addr = {
		.svm = {
			.svm_family = AF_VSOCK,
			.svm_port = port,
			.svm_cid = cid,
		},
	};
	int ret;
	int fd;

	control_expectln("LISTENING");

	fd = socket(AF_VSOCK, SOCK_STREAM, 0);

	timeout_begin(TIMEOUT);
	do {
		ret = connect(fd, &addr.sa, sizeof(addr.svm));
		timeout_check("connect");
	} while (ret < 0 && errno == EINTR);
	timeout_end();

	if (ret < 0) {
		int old_errno = errno;

		close(fd);
		fd = -1;
		errno = old_errno;
	}
	return fd;
}

/* Listen on <cid, port> and return the first incoming connection.  The remote
 * address is stored to clientaddrp.  clientaddrp may be NULL.
 */
int vsock_stream_accept(unsigned int cid, unsigned int port,
			struct sockaddr_vm *clientaddrp)
{
	union {
		struct sockaddr sa;
		struct sockaddr_vm svm;
	} addr = {
		.svm = {
			.svm_family = AF_VSOCK,
			.svm_port = port,
			.svm_cid = cid,
		},
	};
	union {
		struct sockaddr sa;
		struct sockaddr_vm svm;
	} clientaddr;
	socklen_t clientaddr_len = sizeof(clientaddr.svm);
	int fd;
	int client_fd;
	int old_errno;

	fd = socket(AF_VSOCK, SOCK_STREAM, 0);

	if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
		perror("bind");
		exit(EXIT_FAILURE);
	}

	if (listen(fd, 1) < 0) {
		perror("listen");
		exit(EXIT_FAILURE);
	}

	control_writeln("LISTENING");

	timeout_begin(TIMEOUT);
	do {
		client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
		timeout_check("accept");
	} while (client_fd < 0 && errno == EINTR);
	timeout_end();

	old_errno = errno;
	close(fd);
	errno = old_errno;

	if (client_fd < 0)
		return client_fd;

	if (clientaddr_len != sizeof(clientaddr.svm)) {
		fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
			(size_t)clientaddr_len);
		exit(EXIT_FAILURE);
	}
	if (clientaddr.sa.sa_family != AF_VSOCK) {
		fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
			clientaddr.sa.sa_family);
		exit(EXIT_FAILURE);
	}

	if (clientaddrp)
		*clientaddrp = clientaddr.svm;
	return client_fd;
}

/* Transmit one byte and check the return value.
 *
 * expected_ret:
 *  <0 Negative errno (for testing errors)
 *   0 End-of-file
 *   1 Success
 */
void send_byte(int fd, int expected_ret, int flags)
{
	const uint8_t byte = 'A';
	ssize_t nwritten;

	timeout_begin(TIMEOUT);
	do {
		nwritten = send(fd, &byte, sizeof(byte), flags);
		timeout_check("write");
	} while (nwritten < 0 && errno == EINTR);
	timeout_end();

	if (expected_ret < 0) {
		if (nwritten != -1) {
			fprintf(stderr, "bogus send(2) return value %zd\n",
				nwritten);
			exit(EXIT_FAILURE);
		}
		if (errno != -expected_ret) {
			perror("write");
			exit(EXIT_FAILURE);
		}
		return;
	}

	if (nwritten < 0) {
		perror("write");
		exit(EXIT_FAILURE);
	}
	if (nwritten == 0) {
		if (expected_ret == 0)
			return;

		fprintf(stderr, "unexpected EOF while sending byte\n");
		exit(EXIT_FAILURE);
	}
	if (nwritten != sizeof(byte)) {
		fprintf(stderr, "bogus send(2) return value %zd\n", nwritten);
		exit(EXIT_FAILURE);
	}
}

/* Receive one byte and check the return value.
 *
 * expected_ret:
 *  <0 Negative errno (for testing errors)
 *   0 End-of-file
 *   1 Success
 */
void recv_byte(int fd, int expected_ret, int flags)
{
	uint8_t byte;
	ssize_t nread;

	timeout_begin(TIMEOUT);
	do {
		nread = recv(fd, &byte, sizeof(byte), flags);
		timeout_check("read");
	} while (nread < 0 && errno == EINTR);
	timeout_end();

	if (expected_ret < 0) {
		if (nread != -1) {
			fprintf(stderr, "bogus recv(2) return value %zd\n",
				nread);
			exit(EXIT_FAILURE);
		}
		if (errno != -expected_ret) {
			perror("read");
			exit(EXIT_FAILURE);
		}
		return;
	}

	if (nread < 0) {
		perror("read");
		exit(EXIT_FAILURE);
	}
	if (nread == 0) {
		if (expected_ret == 0)
			return;

		fprintf(stderr, "unexpected EOF while receiving byte\n");
		exit(EXIT_FAILURE);
	}
	if (nread != sizeof(byte)) {
		fprintf(stderr, "bogus recv(2) return value %zd\n", nread);
		exit(EXIT_FAILURE);
	}
	if (byte != 'A') {
		fprintf(stderr, "unexpected byte read %c\n", byte);
		exit(EXIT_FAILURE);
	}
}

/* Run test cases.  The program terminates if a failure occurs. */
void run_tests(const struct test_case *test_cases,
	       const struct test_opts *opts)
{
	int i;

	for (i = 0; test_cases[i].name; i++) {
		void (*run)(const struct test_opts *opts);

		printf("%s...", test_cases[i].name);
		fflush(stdout);

		if (opts->mode == TEST_MODE_CLIENT) {
			/* Full barrier before executing the next test.  This
			 * ensures that client and server are executing the
			 * same test case.  In particular, it means whoever is
			 * faster will not see the peer still executing the
			 * last test.  This is important because port numbers
			 * can be used by multiple test cases.
			 */
			control_expectln("NEXT");
			control_writeln("NEXT");

			run = test_cases[i].run_client;
		} else {
			control_writeln("NEXT");
			control_expectln("NEXT");

			run = test_cases[i].run_server;
		}

		if (run)
			run(opts);

		printf("ok\n");
	}
}