WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / vsock / vsock_test.c
blob5a4fb80fa832eb251bc02fefade77c5d8373c2e8
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * vsock_test - vsock.ko test suite
5 * Copyright (C) 2017 Red Hat, Inc.
7 * Author: Stefan Hajnoczi <stefanha@redhat.com>
8 */
10 #include <getopt.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <linux/kernel.h>
18 #include "timeout.h"
19 #include "control.h"
20 #include "util.h"
22 static void test_stream_connection_reset(const struct test_opts *opts)
24 union {
25 struct sockaddr sa;
26 struct sockaddr_vm svm;
27 } addr = {
28 .svm = {
29 .svm_family = AF_VSOCK,
30 .svm_port = 1234,
31 .svm_cid = opts->peer_cid,
34 int ret;
35 int fd;
37 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
39 timeout_begin(TIMEOUT);
40 do {
41 ret = connect(fd, &addr.sa, sizeof(addr.svm));
42 timeout_check("connect");
43 } while (ret < 0 && errno == EINTR);
44 timeout_end();
46 if (ret != -1) {
47 fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
48 exit(EXIT_FAILURE);
50 if (errno != ECONNRESET) {
51 fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
52 exit(EXIT_FAILURE);
55 close(fd);
58 static void test_stream_bind_only_client(const struct test_opts *opts)
60 union {
61 struct sockaddr sa;
62 struct sockaddr_vm svm;
63 } addr = {
64 .svm = {
65 .svm_family = AF_VSOCK,
66 .svm_port = 1234,
67 .svm_cid = opts->peer_cid,
70 int ret;
71 int fd;
73 /* Wait for the server to be ready */
74 control_expectln("BIND");
76 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
78 timeout_begin(TIMEOUT);
79 do {
80 ret = connect(fd, &addr.sa, sizeof(addr.svm));
81 timeout_check("connect");
82 } while (ret < 0 && errno == EINTR);
83 timeout_end();
85 if (ret != -1) {
86 fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
87 exit(EXIT_FAILURE);
89 if (errno != ECONNRESET) {
90 fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
91 exit(EXIT_FAILURE);
94 /* Notify the server that the client has finished */
95 control_writeln("DONE");
97 close(fd);
100 static void test_stream_bind_only_server(const struct test_opts *opts)
102 union {
103 struct sockaddr sa;
104 struct sockaddr_vm svm;
105 } addr = {
106 .svm = {
107 .svm_family = AF_VSOCK,
108 .svm_port = 1234,
109 .svm_cid = VMADDR_CID_ANY,
112 int fd;
114 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
116 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
117 perror("bind");
118 exit(EXIT_FAILURE);
121 /* Notify the client that the server is ready */
122 control_writeln("BIND");
124 /* Wait for the client to finish */
125 control_expectln("DONE");
127 close(fd);
130 static void test_stream_client_close_client(const struct test_opts *opts)
132 int fd;
134 fd = vsock_stream_connect(opts->peer_cid, 1234);
135 if (fd < 0) {
136 perror("connect");
137 exit(EXIT_FAILURE);
140 send_byte(fd, 1, 0);
141 close(fd);
144 static void test_stream_client_close_server(const struct test_opts *opts)
146 int fd;
148 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
149 if (fd < 0) {
150 perror("accept");
151 exit(EXIT_FAILURE);
154 /* Wait for the remote to close the connection, before check
155 * -EPIPE error on send.
157 vsock_wait_remote_close(fd);
159 send_byte(fd, -EPIPE, 0);
160 recv_byte(fd, 1, 0);
161 recv_byte(fd, 0, 0);
162 close(fd);
165 static void test_stream_server_close_client(const struct test_opts *opts)
167 int fd;
169 fd = vsock_stream_connect(opts->peer_cid, 1234);
170 if (fd < 0) {
171 perror("connect");
172 exit(EXIT_FAILURE);
175 /* Wait for the remote to close the connection, before check
176 * -EPIPE error on send.
178 vsock_wait_remote_close(fd);
180 send_byte(fd, -EPIPE, 0);
181 recv_byte(fd, 1, 0);
182 recv_byte(fd, 0, 0);
183 close(fd);
186 static void test_stream_server_close_server(const struct test_opts *opts)
188 int fd;
190 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
191 if (fd < 0) {
192 perror("accept");
193 exit(EXIT_FAILURE);
196 send_byte(fd, 1, 0);
197 close(fd);
200 /* With the standard socket sizes, VMCI is able to support about 100
201 * concurrent stream connections.
203 #define MULTICONN_NFDS 100
205 static void test_stream_multiconn_client(const struct test_opts *opts)
207 int fds[MULTICONN_NFDS];
208 int i;
210 for (i = 0; i < MULTICONN_NFDS; i++) {
211 fds[i] = vsock_stream_connect(opts->peer_cid, 1234);
212 if (fds[i] < 0) {
213 perror("connect");
214 exit(EXIT_FAILURE);
218 for (i = 0; i < MULTICONN_NFDS; i++) {
219 if (i % 2)
220 recv_byte(fds[i], 1, 0);
221 else
222 send_byte(fds[i], 1, 0);
225 for (i = 0; i < MULTICONN_NFDS; i++)
226 close(fds[i]);
229 static void test_stream_multiconn_server(const struct test_opts *opts)
231 int fds[MULTICONN_NFDS];
232 int i;
234 for (i = 0; i < MULTICONN_NFDS; i++) {
235 fds[i] = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
236 if (fds[i] < 0) {
237 perror("accept");
238 exit(EXIT_FAILURE);
242 for (i = 0; i < MULTICONN_NFDS; i++) {
243 if (i % 2)
244 send_byte(fds[i], 1, 0);
245 else
246 recv_byte(fds[i], 1, 0);
249 for (i = 0; i < MULTICONN_NFDS; i++)
250 close(fds[i]);
253 static void test_stream_msg_peek_client(const struct test_opts *opts)
255 int fd;
257 fd = vsock_stream_connect(opts->peer_cid, 1234);
258 if (fd < 0) {
259 perror("connect");
260 exit(EXIT_FAILURE);
263 send_byte(fd, 1, 0);
264 close(fd);
267 static void test_stream_msg_peek_server(const struct test_opts *opts)
269 int fd;
271 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
272 if (fd < 0) {
273 perror("accept");
274 exit(EXIT_FAILURE);
277 recv_byte(fd, 1, MSG_PEEK);
278 recv_byte(fd, 1, 0);
279 close(fd);
282 static struct test_case test_cases[] = {
284 .name = "SOCK_STREAM connection reset",
285 .run_client = test_stream_connection_reset,
288 .name = "SOCK_STREAM bind only",
289 .run_client = test_stream_bind_only_client,
290 .run_server = test_stream_bind_only_server,
293 .name = "SOCK_STREAM client close",
294 .run_client = test_stream_client_close_client,
295 .run_server = test_stream_client_close_server,
298 .name = "SOCK_STREAM server close",
299 .run_client = test_stream_server_close_client,
300 .run_server = test_stream_server_close_server,
303 .name = "SOCK_STREAM multiple connections",
304 .run_client = test_stream_multiconn_client,
305 .run_server = test_stream_multiconn_server,
308 .name = "SOCK_STREAM MSG_PEEK",
309 .run_client = test_stream_msg_peek_client,
310 .run_server = test_stream_msg_peek_server,
315 static const char optstring[] = "";
316 static const struct option longopts[] = {
318 .name = "control-host",
319 .has_arg = required_argument,
320 .val = 'H',
323 .name = "control-port",
324 .has_arg = required_argument,
325 .val = 'P',
328 .name = "mode",
329 .has_arg = required_argument,
330 .val = 'm',
333 .name = "peer-cid",
334 .has_arg = required_argument,
335 .val = 'p',
338 .name = "list",
339 .has_arg = no_argument,
340 .val = 'l',
343 .name = "skip",
344 .has_arg = required_argument,
345 .val = 's',
348 .name = "help",
349 .has_arg = no_argument,
350 .val = '?',
355 static void usage(void)
357 fprintf(stderr, "Usage: vsock_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--list] [--skip=<test_id>]\n"
358 "\n"
359 " Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n"
360 " Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
361 "\n"
362 "Run vsock.ko tests. Must be launched in both guest\n"
363 "and host. One side must use --mode=client and\n"
364 "the other side must use --mode=server.\n"
365 "\n"
366 "A TCP control socket connection is used to coordinate tests\n"
367 "between the client and the server. The server requires a\n"
368 "listen address and the client requires an address to\n"
369 "connect to.\n"
370 "\n"
371 "The CID of the other side must be given with --peer-cid=<cid>.\n"
372 "\n"
373 "Options:\n"
374 " --help This help message\n"
375 " --control-host <host> Server IP address to connect to\n"
376 " --control-port <port> Server port to listen on/connect to\n"
377 " --mode client|server Server or client mode\n"
378 " --peer-cid <cid> CID of the other side\n"
379 " --list List of tests that will be executed\n"
380 " --skip <test_id> Test ID to skip;\n"
381 " use multiple --skip options to skip more tests\n"
383 exit(EXIT_FAILURE);
386 int main(int argc, char **argv)
388 const char *control_host = NULL;
389 const char *control_port = NULL;
390 struct test_opts opts = {
391 .mode = TEST_MODE_UNSET,
392 .peer_cid = VMADDR_CID_ANY,
395 init_signals();
397 for (;;) {
398 int opt = getopt_long(argc, argv, optstring, longopts, NULL);
400 if (opt == -1)
401 break;
403 switch (opt) {
404 case 'H':
405 control_host = optarg;
406 break;
407 case 'm':
408 if (strcmp(optarg, "client") == 0)
409 opts.mode = TEST_MODE_CLIENT;
410 else if (strcmp(optarg, "server") == 0)
411 opts.mode = TEST_MODE_SERVER;
412 else {
413 fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
414 return EXIT_FAILURE;
416 break;
417 case 'p':
418 opts.peer_cid = parse_cid(optarg);
419 break;
420 case 'P':
421 control_port = optarg;
422 break;
423 case 'l':
424 list_tests(test_cases);
425 break;
426 case 's':
427 skip_test(test_cases, ARRAY_SIZE(test_cases) - 1,
428 optarg);
429 break;
430 case '?':
431 default:
432 usage();
436 if (!control_port)
437 usage();
438 if (opts.mode == TEST_MODE_UNSET)
439 usage();
440 if (opts.peer_cid == VMADDR_CID_ANY)
441 usage();
443 if (!control_host) {
444 if (opts.mode != TEST_MODE_SERVER)
445 usage();
446 control_host = "0.0.0.0";
449 control_init(control_host, control_port,
450 opts.mode == TEST_MODE_SERVER);
452 run_tests(test_cases, &opts);
454 control_cleanup();
455 return EXIT_SUCCESS;