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 #include "vsock_test_zerocopy.h"
29 static void test_stream_connection_reset(const struct test_opts *opts)
33 struct sockaddr_vm svm;
36 .svm_family = AF_VSOCK,
38 .svm_cid = opts->peer_cid,
44 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
46 timeout_begin(TIMEOUT);
48 ret = connect(fd, &addr.sa, sizeof(addr.svm));
49 timeout_check("connect");
50 } while (ret < 0 && errno == EINTR);
54 fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
57 if (errno != ECONNRESET) {
58 fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
65 static void test_stream_bind_only_client(const struct test_opts *opts)
69 struct sockaddr_vm svm;
72 .svm_family = AF_VSOCK,
74 .svm_cid = opts->peer_cid,
80 /* Wait for the server to be ready */
81 control_expectln("BIND");
83 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
85 timeout_begin(TIMEOUT);
87 ret = connect(fd, &addr.sa, sizeof(addr.svm));
88 timeout_check("connect");
89 } while (ret < 0 && errno == EINTR);
93 fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
96 if (errno != ECONNRESET) {
97 fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
101 /* Notify the server that the client has finished */
102 control_writeln("DONE");
107 static void test_stream_bind_only_server(const struct test_opts *opts)
111 struct sockaddr_vm svm;
114 .svm_family = AF_VSOCK,
116 .svm_cid = VMADDR_CID_ANY,
121 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
123 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
128 /* Notify the client that the server is ready */
129 control_writeln("BIND");
131 /* Wait for the client to finish */
132 control_expectln("DONE");
137 static void test_stream_client_close_client(const struct test_opts *opts)
141 fd = vsock_stream_connect(opts->peer_cid, 1234);
151 static void test_stream_client_close_server(const struct test_opts *opts)
155 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
161 /* Wait for the remote to close the connection, before check
162 * -EPIPE error on send.
164 vsock_wait_remote_close(fd);
166 send_byte(fd, -EPIPE, 0);
172 static void test_stream_server_close_client(const struct test_opts *opts)
176 fd = vsock_stream_connect(opts->peer_cid, 1234);
182 /* Wait for the remote to close the connection, before check
183 * -EPIPE error on send.
185 vsock_wait_remote_close(fd);
187 send_byte(fd, -EPIPE, 0);
193 static void test_stream_server_close_server(const struct test_opts *opts)
197 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
207 /* With the standard socket sizes, VMCI is able to support about 100
208 * concurrent stream connections.
210 #define MULTICONN_NFDS 100
212 static void test_stream_multiconn_client(const struct test_opts *opts)
214 int fds[MULTICONN_NFDS];
217 for (i = 0; i < MULTICONN_NFDS; i++) {
218 fds[i] = vsock_stream_connect(opts->peer_cid, 1234);
225 for (i = 0; i < MULTICONN_NFDS; i++) {
227 recv_byte(fds[i], 1, 0);
229 send_byte(fds[i], 1, 0);
232 for (i = 0; i < MULTICONN_NFDS; i++)
236 static void test_stream_multiconn_server(const struct test_opts *opts)
238 int fds[MULTICONN_NFDS];
241 for (i = 0; i < MULTICONN_NFDS; i++) {
242 fds[i] = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
249 for (i = 0; i < MULTICONN_NFDS; i++) {
251 send_byte(fds[i], 1, 0);
253 recv_byte(fds[i], 1, 0);
256 for (i = 0; i < MULTICONN_NFDS; i++)
260 #define MSG_PEEK_BUF_LEN 64
262 static void test_msg_peek_client(const struct test_opts *opts,
265 unsigned char buf[MSG_PEEK_BUF_LEN];
270 fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
272 fd = vsock_stream_connect(opts->peer_cid, 1234);
279 for (i = 0; i < sizeof(buf); i++)
280 buf[i] = rand() & 0xFF;
282 control_expectln("SRVREADY");
284 send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
289 static void test_msg_peek_server(const struct test_opts *opts,
292 unsigned char buf_half[MSG_PEEK_BUF_LEN / 2];
293 unsigned char buf_normal[MSG_PEEK_BUF_LEN];
294 unsigned char buf_peek[MSG_PEEK_BUF_LEN];
298 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
300 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
307 /* Peek from empty socket. */
308 recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK | MSG_DONTWAIT,
311 control_writeln("SRVREADY");
313 /* Peek part of data. */
314 recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK, sizeof(buf_half));
316 /* Peek whole data. */
317 recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK, sizeof(buf_peek));
319 /* Compare partial and full peek. */
320 if (memcmp(buf_half, buf_peek, sizeof(buf_half))) {
321 fprintf(stderr, "Partial peek data mismatch\n");
326 /* This type of socket supports MSG_TRUNC flag,
327 * so check it with MSG_PEEK. We must get length
330 recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK | MSG_TRUNC,
334 recv_buf(fd, buf_normal, sizeof(buf_normal), 0, sizeof(buf_normal));
336 /* Compare full peek and normal read. */
337 if (memcmp(buf_peek, buf_normal, sizeof(buf_peek))) {
338 fprintf(stderr, "Full peek data mismatch\n");
345 static void test_stream_msg_peek_client(const struct test_opts *opts)
347 return test_msg_peek_client(opts, false);
350 static void test_stream_msg_peek_server(const struct test_opts *opts)
352 return test_msg_peek_server(opts, false);
355 #define SOCK_BUF_SIZE (2 * 1024 * 1024)
356 #define MAX_MSG_PAGES 4
358 static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
360 unsigned long curr_hash;
366 fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
372 /* Wait, until receiver sets buffer size. */
373 control_expectln("SRVREADY");
376 page_size = getpagesize();
377 max_msg_size = MAX_MSG_PAGES * page_size;
378 msg_count = SOCK_BUF_SIZE / max_msg_size;
380 for (int i = 0; i < msg_count; i++) {
385 /* Use "small" buffers and "big" buffers. */
387 buf_size = page_size +
388 (rand() % (max_msg_size - page_size));
390 buf_size = 1 + (rand() % page_size);
392 buf = malloc(buf_size);
399 memset(buf, rand() & 0xff, buf_size);
400 /* Set at least one MSG_EOR + some random. */
401 if (i == (msg_count / 2) || (rand() & 1)) {
408 send_buf(fd, buf, buf_size, flags, buf_size);
411 * Hash sum is computed at both client and server in
413 * H += hash('message data')
414 * Such hash "controls" both data integrity and message
415 * bounds. After data exchange, both sums are compared
416 * using control socket, and if message bounds wasn't
417 * broken - two values must be equal.
419 curr_hash += hash_djb2(buf, buf_size);
423 control_writeln("SENDDONE");
424 control_writeulong(curr_hash);
428 static void test_seqpacket_msg_bounds_server(const struct test_opts *opts)
430 unsigned long sock_buf_size;
431 unsigned long remote_hash;
432 unsigned long curr_hash;
434 struct msghdr msg = {0};
435 struct iovec iov = {0};
437 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
443 sock_buf_size = SOCK_BUF_SIZE;
445 if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
446 &sock_buf_size, sizeof(sock_buf_size))) {
447 perror("setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
451 if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
452 &sock_buf_size, sizeof(sock_buf_size))) {
453 perror("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
457 /* Ready to receive data. */
458 control_writeln("SRVREADY");
459 /* Wait, until peer sends whole data. */
460 control_expectln("SENDDONE");
461 iov.iov_len = MAX_MSG_PAGES * getpagesize();
462 iov.iov_base = malloc(iov.iov_len);
476 recv_size = recvmsg(fd, &msg, 0);
486 if (msg.msg_flags & MSG_EOR)
489 curr_hash += hash_djb2(msg.msg_iov[0].iov_base, recv_size);
494 remote_hash = control_readulong();
496 if (curr_hash != remote_hash) {
497 fprintf(stderr, "Message bounds broken\n");
502 #define MESSAGE_TRUNC_SZ 32
503 static void test_seqpacket_msg_trunc_client(const struct test_opts *opts)
506 char buf[MESSAGE_TRUNC_SZ];
508 fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
514 send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
516 control_writeln("SENDDONE");
520 static void test_seqpacket_msg_trunc_server(const struct test_opts *opts)
523 char buf[MESSAGE_TRUNC_SZ / 2];
524 struct msghdr msg = {0};
525 struct iovec iov = {0};
527 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
533 control_expectln("SENDDONE");
535 iov.iov_len = sizeof(buf);
539 ssize_t ret = recvmsg(fd, &msg, MSG_TRUNC);
541 if (ret != MESSAGE_TRUNC_SZ) {
542 printf("%zi\n", ret);
543 perror("MSG_TRUNC doesn't work");
547 if (!(msg.msg_flags & MSG_TRUNC)) {
548 fprintf(stderr, "MSG_TRUNC expected\n");
555 static time_t current_nsec(void)
559 if (clock_gettime(CLOCK_REALTIME, &ts)) {
560 perror("clock_gettime(3) failed");
564 return (ts.tv_sec * 1000000000ULL) + ts.tv_nsec;
567 #define RCVTIMEO_TIMEOUT_SEC 1
568 #define READ_OVERHEAD_NSEC 250000000 /* 0.25 sec */
570 static void test_seqpacket_timeout_client(const struct test_opts *opts)
575 time_t read_enter_ns;
576 time_t read_overhead_ns;
578 fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
584 tv.tv_sec = RCVTIMEO_TIMEOUT_SEC;
587 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)) == -1) {
588 perror("setsockopt(SO_RCVTIMEO)");
592 read_enter_ns = current_nsec();
594 if (read(fd, &dummy, sizeof(dummy)) != -1) {
596 "expected 'dummy' read(2) failure\n");
600 if (errno != EAGAIN) {
601 perror("EAGAIN expected");
605 read_overhead_ns = current_nsec() - read_enter_ns -
606 1000000000ULL * RCVTIMEO_TIMEOUT_SEC;
608 if (read_overhead_ns > READ_OVERHEAD_NSEC) {
610 "too much time in read(2), %lu > %i ns\n",
611 read_overhead_ns, READ_OVERHEAD_NSEC);
615 control_writeln("WAITDONE");
619 static void test_seqpacket_timeout_server(const struct test_opts *opts)
623 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
629 control_expectln("WAITDONE");
633 static void test_seqpacket_bigmsg_client(const struct test_opts *opts)
635 unsigned long sock_buf_size;
640 len = sizeof(sock_buf_size);
642 fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
648 if (getsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
649 &sock_buf_size, &len)) {
650 perror("getsockopt");
656 data = malloc(sock_buf_size);
662 send_buf(fd, data, sock_buf_size, 0, -EMSGSIZE);
664 control_writeln("CLISENT");
670 static void test_seqpacket_bigmsg_server(const struct test_opts *opts)
674 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
680 control_expectln("CLISENT");
685 #define BUF_PATTERN_1 'a'
686 #define BUF_PATTERN_2 'b'
688 static void test_seqpacket_invalid_rec_buffer_client(const struct test_opts *opts)
693 int buf_size = getpagesize() * 3;
695 fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
701 buf1 = malloc(buf_size);
703 perror("'malloc()' for 'buf1'");
707 buf2 = malloc(buf_size);
709 perror("'malloc()' for 'buf2'");
713 memset(buf1, BUF_PATTERN_1, buf_size);
714 memset(buf2, BUF_PATTERN_2, buf_size);
716 send_buf(fd, buf1, buf_size, 0, buf_size);
718 send_buf(fd, buf2, buf_size, 0, buf_size);
723 static void test_seqpacket_invalid_rec_buffer_server(const struct test_opts *opts)
726 unsigned char *broken_buf;
727 unsigned char *valid_buf;
728 int page_size = getpagesize();
729 int buf_size = page_size * 3;
731 int prot = PROT_READ | PROT_WRITE;
732 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
735 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
741 /* Setup first buffer. */
742 broken_buf = mmap(NULL, buf_size, prot, flags, -1, 0);
743 if (broken_buf == MAP_FAILED) {
744 perror("mmap for 'broken_buf'");
748 /* Unmap "hole" in buffer. */
749 if (munmap(broken_buf + page_size, page_size)) {
750 perror("'broken_buf' setup");
754 valid_buf = mmap(NULL, buf_size, prot, flags, -1, 0);
755 if (valid_buf == MAP_FAILED) {
756 perror("mmap for 'valid_buf'");
760 /* Try to fill buffer with unmapped middle. */
761 res = read(fd, broken_buf, buf_size);
764 "expected 'broken_buf' read(2) failure, got %zi\n",
769 if (errno != EFAULT) {
770 perror("unexpected errno of 'broken_buf'");
774 /* Try to fill valid buffer. */
775 res = read(fd, valid_buf, buf_size);
777 perror("unexpected 'valid_buf' read(2) failure");
781 if (res != buf_size) {
783 "invalid 'valid_buf' read(2), expected %i, got %zi\n",
788 for (i = 0; i < buf_size; i++) {
789 if (valid_buf[i] != BUF_PATTERN_2) {
791 "invalid pattern for 'valid_buf' at %i, expected %hhX, got %hhX\n",
792 i, BUF_PATTERN_2, valid_buf[i]);
798 munmap(broken_buf, page_size);
799 munmap(broken_buf + page_size * 2, page_size);
800 munmap(valid_buf, buf_size);
804 #define RCVLOWAT_BUF_SIZE 128
806 static void test_stream_poll_rcvlowat_server(const struct test_opts *opts)
811 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
820 control_writeln("SRVSENT");
822 /* Wait until client is ready to receive rest of data. */
823 control_expectln("CLNSENT");
825 for (i = 0; i < RCVLOWAT_BUF_SIZE - 1; i++)
828 /* Keep socket in active state. */
829 control_expectln("POLLDONE");
834 static void test_stream_poll_rcvlowat_client(const struct test_opts *opts)
836 unsigned long lowat_val = RCVLOWAT_BUF_SIZE;
837 char buf[RCVLOWAT_BUF_SIZE];
842 fd = vsock_stream_connect(opts->peer_cid, 1234);
848 if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT,
849 &lowat_val, sizeof(lowat_val))) {
850 perror("setsockopt(SO_RCVLOWAT)");
854 control_expectln("SRVSENT");
856 /* At this point, server sent 1 byte. */
858 poll_flags = POLLIN | POLLRDNORM;
859 fds.events = poll_flags;
861 /* Try to wait for 1 sec. */
862 if (poll(&fds, 1, 1000) < 0) {
867 /* poll() must return nothing. */
869 fprintf(stderr, "Unexpected poll result %hx\n",
874 /* Tell server to send rest of data. */
875 control_writeln("CLNSENT");
878 if (poll(&fds, 1, 10000) < 0) {
883 /* Only these two bits are expected. */
884 if (fds.revents != poll_flags) {
885 fprintf(stderr, "Unexpected poll result %hx\n",
890 /* Use MSG_DONTWAIT, if call is going to wait, EAGAIN
893 recv_buf(fd, buf, sizeof(buf), MSG_DONTWAIT, RCVLOWAT_BUF_SIZE);
895 control_writeln("POLLDONE");
900 #define INV_BUF_TEST_DATA_LEN 512
902 static void test_inv_buf_client(const struct test_opts *opts, bool stream)
904 unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
905 ssize_t expected_ret;
909 fd = vsock_stream_connect(opts->peer_cid, 1234);
911 fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
918 control_expectln("SENDDONE");
920 /* Use invalid buffer here. */
921 recv_buf(fd, NULL, sizeof(data), 0, -EFAULT);
924 /* For SOCK_STREAM we must continue reading. */
925 expected_ret = sizeof(data);
927 /* For SOCK_SEQPACKET socket's queue must be empty. */
928 expected_ret = -EAGAIN;
931 recv_buf(fd, data, sizeof(data), MSG_DONTWAIT, expected_ret);
933 control_writeln("DONE");
938 static void test_inv_buf_server(const struct test_opts *opts, bool stream)
940 unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
944 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
946 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
953 send_buf(fd, data, sizeof(data), 0, sizeof(data));
955 control_writeln("SENDDONE");
957 control_expectln("DONE");
962 static void test_stream_inv_buf_client(const struct test_opts *opts)
964 test_inv_buf_client(opts, true);
967 static void test_stream_inv_buf_server(const struct test_opts *opts)
969 test_inv_buf_server(opts, true);
972 static void test_seqpacket_inv_buf_client(const struct test_opts *opts)
974 test_inv_buf_client(opts, false);
977 static void test_seqpacket_inv_buf_server(const struct test_opts *opts)
979 test_inv_buf_server(opts, false);
982 #define HELLO_STR "HELLO"
983 #define WORLD_STR "WORLD"
985 static void test_stream_virtio_skb_merge_client(const struct test_opts *opts)
989 fd = vsock_stream_connect(opts->peer_cid, 1234);
995 /* Send first skbuff. */
996 send_buf(fd, HELLO_STR, strlen(HELLO_STR), 0, strlen(HELLO_STR));
998 control_writeln("SEND0");
999 /* Peer reads part of first skbuff. */
1000 control_expectln("REPLY0");
1002 /* Send second skbuff, it will be appended to the first. */
1003 send_buf(fd, WORLD_STR, strlen(WORLD_STR), 0, strlen(WORLD_STR));
1005 control_writeln("SEND1");
1006 /* Peer reads merged skbuff packet. */
1007 control_expectln("REPLY1");
1012 static void test_stream_virtio_skb_merge_server(const struct test_opts *opts)
1014 size_t read = 0, to_read;
1015 unsigned char buf[64];
1018 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
1024 control_expectln("SEND0");
1026 /* Read skbuff partially. */
1028 recv_buf(fd, buf + read, to_read, 0, to_read);
1031 control_writeln("REPLY0");
1032 control_expectln("SEND1");
1034 /* Read the rest of both buffers */
1035 to_read = strlen(HELLO_STR WORLD_STR) - read;
1036 recv_buf(fd, buf + read, to_read, 0, to_read);
1039 /* No more bytes should be there */
1040 to_read = sizeof(buf) - read;
1041 recv_buf(fd, buf + read, to_read, MSG_DONTWAIT, -EAGAIN);
1043 if (memcmp(buf, HELLO_STR WORLD_STR, strlen(HELLO_STR WORLD_STR))) {
1044 fprintf(stderr, "pattern mismatch\n");
1048 control_writeln("REPLY1");
1053 static void test_seqpacket_msg_peek_client(const struct test_opts *opts)
1055 return test_msg_peek_client(opts, true);
1058 static void test_seqpacket_msg_peek_server(const struct test_opts *opts)
1060 return test_msg_peek_server(opts, true);
1063 static sig_atomic_t have_sigpipe;
1065 static void sigpipe(int signo)
1070 static void test_stream_check_sigpipe(int fd)
1076 res = send(fd, "A", 1, 0);
1078 fprintf(stderr, "expected send(2) failure, got %zi\n", res);
1082 if (!have_sigpipe) {
1083 fprintf(stderr, "SIGPIPE expected\n");
1089 res = send(fd, "A", 1, MSG_NOSIGNAL);
1091 fprintf(stderr, "expected send(2) failure, got %zi\n", res);
1096 fprintf(stderr, "SIGPIPE not expected\n");
1101 static void test_stream_shutwr_client(const struct test_opts *opts)
1105 struct sigaction act = {
1106 .sa_handler = sigpipe,
1109 sigaction(SIGPIPE, &act, NULL);
1111 fd = vsock_stream_connect(opts->peer_cid, 1234);
1117 if (shutdown(fd, SHUT_WR)) {
1122 test_stream_check_sigpipe(fd);
1124 control_writeln("CLIENTDONE");
1129 static void test_stream_shutwr_server(const struct test_opts *opts)
1133 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
1139 control_expectln("CLIENTDONE");
1144 static void test_stream_shutrd_client(const struct test_opts *opts)
1148 struct sigaction act = {
1149 .sa_handler = sigpipe,
1152 sigaction(SIGPIPE, &act, NULL);
1154 fd = vsock_stream_connect(opts->peer_cid, 1234);
1160 control_expectln("SHUTRDDONE");
1162 test_stream_check_sigpipe(fd);
1164 control_writeln("CLIENTDONE");
1169 static void test_stream_shutrd_server(const struct test_opts *opts)
1173 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
1179 if (shutdown(fd, SHUT_RD)) {
1184 control_writeln("SHUTRDDONE");
1185 control_expectln("CLIENTDONE");
1190 static void test_double_bind_connect_server(const struct test_opts *opts)
1192 int listen_fd, client_fd, i;
1193 struct sockaddr_vm sa_client;
1194 socklen_t socklen_client = sizeof(sa_client);
1196 listen_fd = vsock_stream_listen(VMADDR_CID_ANY, 1234);
1198 for (i = 0; i < 2; i++) {
1199 control_writeln("LISTENING");
1201 timeout_begin(TIMEOUT);
1203 client_fd = accept(listen_fd, (struct sockaddr *)&sa_client,
1205 timeout_check("accept");
1206 } while (client_fd < 0 && errno == EINTR);
1209 if (client_fd < 0) {
1214 /* Waiting for remote peer to close connection */
1215 vsock_wait_remote_close(client_fd);
1221 static void test_double_bind_connect_client(const struct test_opts *opts)
1225 for (i = 0; i < 2; i++) {
1226 /* Wait until server is ready to accept a new connection */
1227 control_expectln("LISTENING");
1229 client_fd = vsock_bind_connect(opts->peer_cid, 1234, 4321, SOCK_STREAM);
1235 #define RCVLOWAT_CREDIT_UPD_BUF_SIZE (1024 * 128)
1236 /* This define is the same as in 'include/linux/virtio_vsock.h':
1237 * it is used to decide when to send credit update message during
1238 * reading from rx queue of a socket. Value and its usage in
1239 * kernel is important for this test.
1241 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (1024 * 64)
1243 static void test_stream_rcvlowat_def_cred_upd_client(const struct test_opts *opts)
1249 fd = vsock_stream_connect(opts->peer_cid, 1234);
1255 /* Send 1 byte more than peer's buffer size. */
1256 buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE + 1;
1258 buf = malloc(buf_size);
1264 /* Wait until peer sets needed buffer size. */
1265 recv_byte(fd, 1, 0);
1267 if (send(fd, buf, buf_size, 0) != buf_size) {
1268 perror("send failed");
1276 static void test_stream_credit_update_test(const struct test_opts *opts,
1277 bool low_rx_bytes_test)
1279 size_t recv_buf_size;
1285 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
1291 buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE;
1293 if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
1294 &buf_size, sizeof(buf_size))) {
1295 perror("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
1299 if (low_rx_bytes_test) {
1300 /* Set new SO_RCVLOWAT here. This enables sending credit
1301 * update when number of bytes if our rx queue become <
1302 * SO_RCVLOWAT value.
1304 recv_buf_size = 1 + VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
1306 if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT,
1307 &recv_buf_size, sizeof(recv_buf_size))) {
1308 perror("setsockopt(SO_RCVLOWAT)");
1313 /* Send one dummy byte here, because 'setsockopt()' above also
1314 * sends special packet which tells sender to update our buffer
1315 * size. This 'send_byte()' will serialize such packet with data
1316 * reads in a loop below. Sender starts transmission only when
1317 * it receives this single byte.
1319 send_byte(fd, 1, 0);
1321 buf = malloc(buf_size);
1327 /* Wait until there will be 128KB of data in rx queue. */
1331 res = recv(fd, buf, buf_size, MSG_PEEK);
1332 if (res == buf_size)
1336 fprintf(stderr, "unexpected 'recv()' return: %zi\n", res);
1341 /* There is 128KB of data in the socket's rx queue, dequeue first
1342 * 64KB, credit update is sent if 'low_rx_bytes_test' == true.
1343 * Otherwise, credit update is sent in 'if (!low_rx_bytes_test)'.
1345 recv_buf_size = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
1346 recv_buf(fd, buf, recv_buf_size, 0, recv_buf_size);
1348 if (!low_rx_bytes_test) {
1351 /* Updating SO_RCVLOWAT will send credit update. */
1352 if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT,
1353 &recv_buf_size, sizeof(recv_buf_size))) {
1354 perror("setsockopt(SO_RCVLOWAT)");
1360 fds.events = POLLIN | POLLRDNORM | POLLERR |
1361 POLLRDHUP | POLLHUP;
1363 /* This 'poll()' will return once we receive last byte
1366 if (poll(&fds, 1, -1) < 0) {
1371 if (fds.revents & POLLERR) {
1372 fprintf(stderr, "'poll()' error\n");
1376 if (fds.revents & (POLLIN | POLLRDNORM)) {
1377 recv_buf(fd, buf, recv_buf_size, MSG_DONTWAIT, recv_buf_size);
1379 /* These flags must be set, as there is at
1380 * least 64KB of data ready to read.
1382 fprintf(stderr, "POLLIN | POLLRDNORM expected\n");
1390 static void test_stream_cred_upd_on_low_rx_bytes(const struct test_opts *opts)
1392 test_stream_credit_update_test(opts, true);
1395 static void test_stream_cred_upd_on_set_rcvlowat(const struct test_opts *opts)
1397 test_stream_credit_update_test(opts, false);
1400 static struct test_case test_cases[] = {
1402 .name = "SOCK_STREAM connection reset",
1403 .run_client = test_stream_connection_reset,
1406 .name = "SOCK_STREAM bind only",
1407 .run_client = test_stream_bind_only_client,
1408 .run_server = test_stream_bind_only_server,
1411 .name = "SOCK_STREAM client close",
1412 .run_client = test_stream_client_close_client,
1413 .run_server = test_stream_client_close_server,
1416 .name = "SOCK_STREAM server close",
1417 .run_client = test_stream_server_close_client,
1418 .run_server = test_stream_server_close_server,
1421 .name = "SOCK_STREAM multiple connections",
1422 .run_client = test_stream_multiconn_client,
1423 .run_server = test_stream_multiconn_server,
1426 .name = "SOCK_STREAM MSG_PEEK",
1427 .run_client = test_stream_msg_peek_client,
1428 .run_server = test_stream_msg_peek_server,
1431 .name = "SOCK_SEQPACKET msg bounds",
1432 .run_client = test_seqpacket_msg_bounds_client,
1433 .run_server = test_seqpacket_msg_bounds_server,
1436 .name = "SOCK_SEQPACKET MSG_TRUNC flag",
1437 .run_client = test_seqpacket_msg_trunc_client,
1438 .run_server = test_seqpacket_msg_trunc_server,
1441 .name = "SOCK_SEQPACKET timeout",
1442 .run_client = test_seqpacket_timeout_client,
1443 .run_server = test_seqpacket_timeout_server,
1446 .name = "SOCK_SEQPACKET invalid receive buffer",
1447 .run_client = test_seqpacket_invalid_rec_buffer_client,
1448 .run_server = test_seqpacket_invalid_rec_buffer_server,
1451 .name = "SOCK_STREAM poll() + SO_RCVLOWAT",
1452 .run_client = test_stream_poll_rcvlowat_client,
1453 .run_server = test_stream_poll_rcvlowat_server,
1456 .name = "SOCK_SEQPACKET big message",
1457 .run_client = test_seqpacket_bigmsg_client,
1458 .run_server = test_seqpacket_bigmsg_server,
1461 .name = "SOCK_STREAM test invalid buffer",
1462 .run_client = test_stream_inv_buf_client,
1463 .run_server = test_stream_inv_buf_server,
1466 .name = "SOCK_SEQPACKET test invalid buffer",
1467 .run_client = test_seqpacket_inv_buf_client,
1468 .run_server = test_seqpacket_inv_buf_server,
1471 .name = "SOCK_STREAM virtio skb merge",
1472 .run_client = test_stream_virtio_skb_merge_client,
1473 .run_server = test_stream_virtio_skb_merge_server,
1476 .name = "SOCK_SEQPACKET MSG_PEEK",
1477 .run_client = test_seqpacket_msg_peek_client,
1478 .run_server = test_seqpacket_msg_peek_server,
1481 .name = "SOCK_STREAM SHUT_WR",
1482 .run_client = test_stream_shutwr_client,
1483 .run_server = test_stream_shutwr_server,
1486 .name = "SOCK_STREAM SHUT_RD",
1487 .run_client = test_stream_shutrd_client,
1488 .run_server = test_stream_shutrd_server,
1491 .name = "SOCK_STREAM MSG_ZEROCOPY",
1492 .run_client = test_stream_msgzcopy_client,
1493 .run_server = test_stream_msgzcopy_server,
1496 .name = "SOCK_SEQPACKET MSG_ZEROCOPY",
1497 .run_client = test_seqpacket_msgzcopy_client,
1498 .run_server = test_seqpacket_msgzcopy_server,
1501 .name = "SOCK_STREAM MSG_ZEROCOPY empty MSG_ERRQUEUE",
1502 .run_client = test_stream_msgzcopy_empty_errq_client,
1503 .run_server = test_stream_msgzcopy_empty_errq_server,
1506 .name = "SOCK_STREAM double bind connect",
1507 .run_client = test_double_bind_connect_client,
1508 .run_server = test_double_bind_connect_server,
1511 .name = "SOCK_STREAM virtio credit update + SO_RCVLOWAT",
1512 .run_client = test_stream_rcvlowat_def_cred_upd_client,
1513 .run_server = test_stream_cred_upd_on_set_rcvlowat,
1516 .name = "SOCK_STREAM virtio credit update + low rx_bytes",
1517 .run_client = test_stream_rcvlowat_def_cred_upd_client,
1518 .run_server = test_stream_cred_upd_on_low_rx_bytes,
1523 static const char optstring[] = "";
1524 static const struct option longopts[] = {
1526 .name = "control-host",
1527 .has_arg = required_argument,
1531 .name = "control-port",
1532 .has_arg = required_argument,
1537 .has_arg = required_argument,
1542 .has_arg = required_argument,
1547 .has_arg = no_argument,
1552 .has_arg = required_argument,
1557 .has_arg = no_argument,
1563 static void usage(void)
1565 fprintf(stderr, "Usage: vsock_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--list] [--skip=<test_id>]\n"
1567 " Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n"
1568 " Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
1570 "Run vsock.ko tests. Must be launched in both guest\n"
1571 "and host. One side must use --mode=client and\n"
1572 "the other side must use --mode=server.\n"
1574 "A TCP control socket connection is used to coordinate tests\n"
1575 "between the client and the server. The server requires a\n"
1576 "listen address and the client requires an address to\n"
1579 "The CID of the other side must be given with --peer-cid=<cid>.\n"
1582 " --help This help message\n"
1583 " --control-host <host> Server IP address to connect to\n"
1584 " --control-port <port> Server port to listen on/connect to\n"
1585 " --mode client|server Server or client mode\n"
1586 " --peer-cid <cid> CID of the other side\n"
1587 " --list List of tests that will be executed\n"
1588 " --skip <test_id> Test ID to skip;\n"
1589 " use multiple --skip options to skip more tests\n"
1594 int main(int argc, char **argv)
1596 const char *control_host = NULL;
1597 const char *control_port = NULL;
1598 struct test_opts opts = {
1599 .mode = TEST_MODE_UNSET,
1600 .peer_cid = VMADDR_CID_ANY,
1607 int opt = getopt_long(argc, argv, optstring, longopts, NULL);
1614 control_host = optarg;
1617 if (strcmp(optarg, "client") == 0)
1618 opts.mode = TEST_MODE_CLIENT;
1619 else if (strcmp(optarg, "server") == 0)
1620 opts.mode = TEST_MODE_SERVER;
1622 fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
1623 return EXIT_FAILURE;
1627 opts.peer_cid = parse_cid(optarg);
1630 control_port = optarg;
1633 list_tests(test_cases);
1636 skip_test(test_cases, ARRAY_SIZE(test_cases) - 1,
1647 if (opts.mode == TEST_MODE_UNSET)
1649 if (opts.peer_cid == VMADDR_CID_ANY)
1652 if (!control_host) {
1653 if (opts.mode != TEST_MODE_SERVER)
1655 control_host = "0.0.0.0";
1658 control_init(control_host, control_port,
1659 opts.mode == TEST_MODE_SERVER);
1661 run_tests(test_cases, &opts);
1664 return EXIT_SUCCESS;