1 // SPDX-License-Identifier: GPL-2.0-only
3 * vsock_test - vsock.ko test suite
5 * Copyright (C) 2017 Red Hat, Inc.
7 * Author: Stefan Hajnoczi <stefanha@redhat.com>
16 #include <linux/kernel.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
24 static void test_stream_connection_reset(const struct test_opts *opts)
28 struct sockaddr_vm svm;
31 .svm_family = AF_VSOCK,
33 .svm_cid = opts->peer_cid,
39 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
41 timeout_begin(TIMEOUT);
43 ret = connect(fd, &addr.sa, sizeof(addr.svm));
44 timeout_check("connect");
45 } while (ret < 0 && errno == EINTR);
49 fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
52 if (errno != ECONNRESET) {
53 fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
60 static void test_stream_bind_only_client(const struct test_opts *opts)
64 struct sockaddr_vm svm;
67 .svm_family = AF_VSOCK,
69 .svm_cid = opts->peer_cid,
75 /* Wait for the server to be ready */
76 control_expectln("BIND");
78 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
80 timeout_begin(TIMEOUT);
82 ret = connect(fd, &addr.sa, sizeof(addr.svm));
83 timeout_check("connect");
84 } while (ret < 0 && errno == EINTR);
88 fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
91 if (errno != ECONNRESET) {
92 fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
96 /* Notify the server that the client has finished */
97 control_writeln("DONE");
102 static void test_stream_bind_only_server(const struct test_opts *opts)
106 struct sockaddr_vm svm;
109 .svm_family = AF_VSOCK,
111 .svm_cid = VMADDR_CID_ANY,
116 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
118 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
123 /* Notify the client that the server is ready */
124 control_writeln("BIND");
126 /* Wait for the client to finish */
127 control_expectln("DONE");
132 static void test_stream_client_close_client(const struct test_opts *opts)
136 fd = vsock_stream_connect(opts->peer_cid, 1234);
146 static void test_stream_client_close_server(const struct test_opts *opts)
150 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
156 /* Wait for the remote to close the connection, before check
157 * -EPIPE error on send.
159 vsock_wait_remote_close(fd);
161 send_byte(fd, -EPIPE, 0);
167 static void test_stream_server_close_client(const struct test_opts *opts)
171 fd = vsock_stream_connect(opts->peer_cid, 1234);
177 /* Wait for the remote to close the connection, before check
178 * -EPIPE error on send.
180 vsock_wait_remote_close(fd);
182 send_byte(fd, -EPIPE, 0);
188 static void test_stream_server_close_server(const struct test_opts *opts)
192 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
202 /* With the standard socket sizes, VMCI is able to support about 100
203 * concurrent stream connections.
205 #define MULTICONN_NFDS 100
207 static void test_stream_multiconn_client(const struct test_opts *opts)
209 int fds[MULTICONN_NFDS];
212 for (i = 0; i < MULTICONN_NFDS; i++) {
213 fds[i] = vsock_stream_connect(opts->peer_cid, 1234);
220 for (i = 0; i < MULTICONN_NFDS; i++) {
222 recv_byte(fds[i], 1, 0);
224 send_byte(fds[i], 1, 0);
227 for (i = 0; i < MULTICONN_NFDS; i++)
231 static void test_stream_multiconn_server(const struct test_opts *opts)
233 int fds[MULTICONN_NFDS];
236 for (i = 0; i < MULTICONN_NFDS; i++) {
237 fds[i] = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
244 for (i = 0; i < MULTICONN_NFDS; i++) {
246 send_byte(fds[i], 1, 0);
248 recv_byte(fds[i], 1, 0);
251 for (i = 0; i < MULTICONN_NFDS; i++)
255 static void test_stream_msg_peek_client(const struct test_opts *opts)
259 fd = vsock_stream_connect(opts->peer_cid, 1234);
269 static void test_stream_msg_peek_server(const struct test_opts *opts)
273 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
279 recv_byte(fd, 1, MSG_PEEK);
284 #define MESSAGES_CNT 7
285 static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
289 fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
295 /* Send several messages, one with MSG_EOR flag */
296 for (int i = 0; i < MESSAGES_CNT; i++)
299 control_writeln("SENDDONE");
303 static void test_seqpacket_msg_bounds_server(const struct test_opts *opts)
307 struct msghdr msg = {0};
308 struct iovec iov = {0};
310 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
316 control_expectln("SENDDONE");
318 iov.iov_len = sizeof(buf);
322 for (int i = 0; i < MESSAGES_CNT; i++) {
323 if (recvmsg(fd, &msg, 0) != 1) {
324 perror("message bound violated");
332 #define MESSAGE_TRUNC_SZ 32
333 static void test_seqpacket_msg_trunc_client(const struct test_opts *opts)
336 char buf[MESSAGE_TRUNC_SZ];
338 fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
344 if (send(fd, buf, sizeof(buf), 0) != sizeof(buf)) {
345 perror("send failed");
349 control_writeln("SENDDONE");
353 static void test_seqpacket_msg_trunc_server(const struct test_opts *opts)
356 char buf[MESSAGE_TRUNC_SZ / 2];
357 struct msghdr msg = {0};
358 struct iovec iov = {0};
360 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
366 control_expectln("SENDDONE");
368 iov.iov_len = sizeof(buf);
372 ssize_t ret = recvmsg(fd, &msg, MSG_TRUNC);
374 if (ret != MESSAGE_TRUNC_SZ) {
375 printf("%zi\n", ret);
376 perror("MSG_TRUNC doesn't work");
380 if (!(msg.msg_flags & MSG_TRUNC)) {
381 fprintf(stderr, "MSG_TRUNC expected\n");
388 static struct test_case test_cases[] = {
390 .name = "SOCK_STREAM connection reset",
391 .run_client = test_stream_connection_reset,
394 .name = "SOCK_STREAM bind only",
395 .run_client = test_stream_bind_only_client,
396 .run_server = test_stream_bind_only_server,
399 .name = "SOCK_STREAM client close",
400 .run_client = test_stream_client_close_client,
401 .run_server = test_stream_client_close_server,
404 .name = "SOCK_STREAM server close",
405 .run_client = test_stream_server_close_client,
406 .run_server = test_stream_server_close_server,
409 .name = "SOCK_STREAM multiple connections",
410 .run_client = test_stream_multiconn_client,
411 .run_server = test_stream_multiconn_server,
414 .name = "SOCK_STREAM MSG_PEEK",
415 .run_client = test_stream_msg_peek_client,
416 .run_server = test_stream_msg_peek_server,
419 .name = "SOCK_SEQPACKET msg bounds",
420 .run_client = test_seqpacket_msg_bounds_client,
421 .run_server = test_seqpacket_msg_bounds_server,
424 .name = "SOCK_SEQPACKET MSG_TRUNC flag",
425 .run_client = test_seqpacket_msg_trunc_client,
426 .run_server = test_seqpacket_msg_trunc_server,
431 static const char optstring[] = "";
432 static const struct option longopts[] = {
434 .name = "control-host",
435 .has_arg = required_argument,
439 .name = "control-port",
440 .has_arg = required_argument,
445 .has_arg = required_argument,
450 .has_arg = required_argument,
455 .has_arg = no_argument,
460 .has_arg = required_argument,
465 .has_arg = no_argument,
471 static void usage(void)
473 fprintf(stderr, "Usage: vsock_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--list] [--skip=<test_id>]\n"
475 " Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n"
476 " Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
478 "Run vsock.ko tests. Must be launched in both guest\n"
479 "and host. One side must use --mode=client and\n"
480 "the other side must use --mode=server.\n"
482 "A TCP control socket connection is used to coordinate tests\n"
483 "between the client and the server. The server requires a\n"
484 "listen address and the client requires an address to\n"
487 "The CID of the other side must be given with --peer-cid=<cid>.\n"
490 " --help This help message\n"
491 " --control-host <host> Server IP address to connect to\n"
492 " --control-port <port> Server port to listen on/connect to\n"
493 " --mode client|server Server or client mode\n"
494 " --peer-cid <cid> CID of the other side\n"
495 " --list List of tests that will be executed\n"
496 " --skip <test_id> Test ID to skip;\n"
497 " use multiple --skip options to skip more tests\n"
502 int main(int argc, char **argv)
504 const char *control_host = NULL;
505 const char *control_port = NULL;
506 struct test_opts opts = {
507 .mode = TEST_MODE_UNSET,
508 .peer_cid = VMADDR_CID_ANY,
514 int opt = getopt_long(argc, argv, optstring, longopts, NULL);
521 control_host = optarg;
524 if (strcmp(optarg, "client") == 0)
525 opts.mode = TEST_MODE_CLIENT;
526 else if (strcmp(optarg, "server") == 0)
527 opts.mode = TEST_MODE_SERVER;
529 fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
534 opts.peer_cid = parse_cid(optarg);
537 control_port = optarg;
540 list_tests(test_cases);
543 skip_test(test_cases, ARRAY_SIZE(test_cases) - 1,
554 if (opts.mode == TEST_MODE_UNSET)
556 if (opts.peer_cid == VMADDR_CID_ANY)
560 if (opts.mode != TEST_MODE_SERVER)
562 control_host = "0.0.0.0";
565 control_init(control_host, control_port,
566 opts.mode == TEST_MODE_SERVER);
568 run_tests(test_cases, &opts);