1 // SPDX-License-Identifier: GPL-2.0-only
3 * vsock_test - vsock.ko test suite
5 * Copyright (C) 2017 Red Hat, Inc.
16 #include <linux/kernel.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
23 #include <sys/ioctl.h>
24 #include <linux/sockios.h>
26 #include "vsock_test_zerocopy.h"
31 static void test_stream_connection_reset(const struct test_opts *opts)
35 struct sockaddr_vm svm;
38 .svm_family = AF_VSOCK,
39 .svm_port = opts->peer_port,
40 .svm_cid = opts->peer_cid,
46 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
48 timeout_begin(TIMEOUT);
50 ret = connect(fd, &addr.sa, sizeof(addr.svm));
51 timeout_check("connect");
52 } while (ret < 0 && errno == EINTR);
56 fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
59 if (errno != ECONNRESET) {
60 fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
67 static void test_stream_bind_only_client(const struct test_opts *opts)
71 struct sockaddr_vm svm;
74 .svm_family = AF_VSOCK,
75 .svm_port = opts->peer_port,
76 .svm_cid = opts->peer_cid,
82 /* Wait for the server to be ready */
83 control_expectln("BIND");
85 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
87 timeout_begin(TIMEOUT);
89 ret = connect(fd, &addr.sa, sizeof(addr.svm));
90 timeout_check("connect");
91 } while (ret < 0 && errno == EINTR);
95 fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
98 if (errno != ECONNRESET) {
99 fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
103 /* Notify the server that the client has finished */
104 control_writeln("DONE");
109 static void test_stream_bind_only_server(const struct test_opts *opts)
113 struct sockaddr_vm svm;
116 .svm_family = AF_VSOCK,
117 .svm_port = opts->peer_port,
118 .svm_cid = VMADDR_CID_ANY,
123 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
125 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
130 /* Notify the client that the server is ready */
131 control_writeln("BIND");
133 /* Wait for the client to finish */
134 control_expectln("DONE");
139 static void test_stream_client_close_client(const struct test_opts *opts)
143 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
153 static void test_stream_client_close_server(const struct test_opts *opts)
157 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
163 /* Wait for the remote to close the connection, before check
164 * -EPIPE error on send.
166 vsock_wait_remote_close(fd);
168 send_byte(fd, -EPIPE, 0);
174 static void test_stream_server_close_client(const struct test_opts *opts)
178 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
184 /* Wait for the remote to close the connection, before check
185 * -EPIPE error on send.
187 vsock_wait_remote_close(fd);
189 send_byte(fd, -EPIPE, 0);
195 static void test_stream_server_close_server(const struct test_opts *opts)
199 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
209 /* With the standard socket sizes, VMCI is able to support about 100
210 * concurrent stream connections.
212 #define MULTICONN_NFDS 100
214 static void test_stream_multiconn_client(const struct test_opts *opts)
216 int fds[MULTICONN_NFDS];
219 for (i = 0; i < MULTICONN_NFDS; i++) {
220 fds[i] = vsock_stream_connect(opts->peer_cid, opts->peer_port);
227 for (i = 0; i < MULTICONN_NFDS; i++) {
229 recv_byte(fds[i], 1, 0);
231 send_byte(fds[i], 1, 0);
234 for (i = 0; i < MULTICONN_NFDS; i++)
238 static void test_stream_multiconn_server(const struct test_opts *opts)
240 int fds[MULTICONN_NFDS];
243 for (i = 0; i < MULTICONN_NFDS; i++) {
244 fds[i] = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
251 for (i = 0; i < MULTICONN_NFDS; i++) {
253 send_byte(fds[i], 1, 0);
255 recv_byte(fds[i], 1, 0);
258 for (i = 0; i < MULTICONN_NFDS; i++)
262 #define MSG_PEEK_BUF_LEN 64
264 static void test_msg_peek_client(const struct test_opts *opts,
267 unsigned char buf[MSG_PEEK_BUF_LEN];
272 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
274 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
281 for (i = 0; i < sizeof(buf); i++)
282 buf[i] = rand() & 0xFF;
284 control_expectln("SRVREADY");
286 send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
291 static void test_msg_peek_server(const struct test_opts *opts,
294 unsigned char buf_half[MSG_PEEK_BUF_LEN / 2];
295 unsigned char buf_normal[MSG_PEEK_BUF_LEN];
296 unsigned char buf_peek[MSG_PEEK_BUF_LEN];
300 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
302 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
309 /* Peek from empty socket. */
310 recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK | MSG_DONTWAIT,
313 control_writeln("SRVREADY");
315 /* Peek part of data. */
316 recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK, sizeof(buf_half));
318 /* Peek whole data. */
319 recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK, sizeof(buf_peek));
321 /* Compare partial and full peek. */
322 if (memcmp(buf_half, buf_peek, sizeof(buf_half))) {
323 fprintf(stderr, "Partial peek data mismatch\n");
328 /* This type of socket supports MSG_TRUNC flag,
329 * so check it with MSG_PEEK. We must get length
332 recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK | MSG_TRUNC,
336 recv_buf(fd, buf_normal, sizeof(buf_normal), 0, sizeof(buf_normal));
338 /* Compare full peek and normal read. */
339 if (memcmp(buf_peek, buf_normal, sizeof(buf_peek))) {
340 fprintf(stderr, "Full peek data mismatch\n");
347 static void test_stream_msg_peek_client(const struct test_opts *opts)
349 return test_msg_peek_client(opts, false);
352 static void test_stream_msg_peek_server(const struct test_opts *opts)
354 return test_msg_peek_server(opts, false);
357 #define SOCK_BUF_SIZE (2 * 1024 * 1024)
358 #define MAX_MSG_PAGES 4
360 static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
362 unsigned long curr_hash;
368 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
374 /* Wait, until receiver sets buffer size. */
375 control_expectln("SRVREADY");
378 page_size = getpagesize();
379 max_msg_size = MAX_MSG_PAGES * page_size;
380 msg_count = SOCK_BUF_SIZE / max_msg_size;
382 for (int i = 0; i < msg_count; i++) {
387 /* Use "small" buffers and "big" buffers. */
389 buf_size = page_size +
390 (rand() % (max_msg_size - page_size));
392 buf_size = 1 + (rand() % page_size);
394 buf = malloc(buf_size);
401 memset(buf, rand() & 0xff, buf_size);
402 /* Set at least one MSG_EOR + some random. */
403 if (i == (msg_count / 2) || (rand() & 1)) {
410 send_buf(fd, buf, buf_size, flags, buf_size);
413 * Hash sum is computed at both client and server in
415 * H += hash('message data')
416 * Such hash "controls" both data integrity and message
417 * bounds. After data exchange, both sums are compared
418 * using control socket, and if message bounds wasn't
419 * broken - two values must be equal.
421 curr_hash += hash_djb2(buf, buf_size);
425 control_writeln("SENDDONE");
426 control_writeulong(curr_hash);
430 static void test_seqpacket_msg_bounds_server(const struct test_opts *opts)
432 unsigned long long sock_buf_size;
433 unsigned long remote_hash;
434 unsigned long curr_hash;
436 struct msghdr msg = {0};
437 struct iovec iov = {0};
439 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
445 sock_buf_size = SOCK_BUF_SIZE;
447 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
449 "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
451 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
453 "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
455 /* Ready to receive data. */
456 control_writeln("SRVREADY");
457 /* Wait, until peer sends whole data. */
458 control_expectln("SENDDONE");
459 iov.iov_len = MAX_MSG_PAGES * getpagesize();
460 iov.iov_base = malloc(iov.iov_len);
474 recv_size = recvmsg(fd, &msg, 0);
484 if (msg.msg_flags & MSG_EOR)
487 curr_hash += hash_djb2(msg.msg_iov[0].iov_base, recv_size);
492 remote_hash = control_readulong();
494 if (curr_hash != remote_hash) {
495 fprintf(stderr, "Message bounds broken\n");
500 #define MESSAGE_TRUNC_SZ 32
501 static void test_seqpacket_msg_trunc_client(const struct test_opts *opts)
504 char buf[MESSAGE_TRUNC_SZ];
506 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
512 send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
514 control_writeln("SENDDONE");
518 static void test_seqpacket_msg_trunc_server(const struct test_opts *opts)
521 char buf[MESSAGE_TRUNC_SZ / 2];
522 struct msghdr msg = {0};
523 struct iovec iov = {0};
525 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
531 control_expectln("SENDDONE");
533 iov.iov_len = sizeof(buf);
537 ssize_t ret = recvmsg(fd, &msg, MSG_TRUNC);
539 if (ret != MESSAGE_TRUNC_SZ) {
540 printf("%zi\n", ret);
541 perror("MSG_TRUNC doesn't work");
545 if (!(msg.msg_flags & MSG_TRUNC)) {
546 fprintf(stderr, "MSG_TRUNC expected\n");
553 static time_t current_nsec(void)
557 if (clock_gettime(CLOCK_REALTIME, &ts)) {
558 perror("clock_gettime(3) failed");
562 return (ts.tv_sec * 1000000000ULL) + ts.tv_nsec;
565 #define RCVTIMEO_TIMEOUT_SEC 1
566 #define READ_OVERHEAD_NSEC 250000000 /* 0.25 sec */
568 static void test_seqpacket_timeout_client(const struct test_opts *opts)
573 time_t read_enter_ns;
574 time_t read_overhead_ns;
576 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
582 tv.tv_sec = RCVTIMEO_TIMEOUT_SEC;
585 setsockopt_timeval_check(fd, SOL_SOCKET, SO_RCVTIMEO, tv,
586 "setsockopt(SO_RCVTIMEO)");
588 read_enter_ns = current_nsec();
590 if (read(fd, &dummy, sizeof(dummy)) != -1) {
592 "expected 'dummy' read(2) failure\n");
596 if (errno != EAGAIN) {
597 perror("EAGAIN expected");
601 read_overhead_ns = current_nsec() - read_enter_ns -
602 1000000000ULL * RCVTIMEO_TIMEOUT_SEC;
604 if (read_overhead_ns > READ_OVERHEAD_NSEC) {
606 "too much time in read(2), %lu > %i ns\n",
607 read_overhead_ns, READ_OVERHEAD_NSEC);
611 control_writeln("WAITDONE");
615 static void test_seqpacket_timeout_server(const struct test_opts *opts)
619 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
625 control_expectln("WAITDONE");
629 static void test_seqpacket_bigmsg_client(const struct test_opts *opts)
631 unsigned long long sock_buf_size;
637 len = sizeof(sock_buf_size);
639 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
645 if (getsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
646 &sock_buf_size, &len)) {
647 perror("getsockopt");
653 /* size_t can be < unsigned long long */
654 buf_size = (size_t)sock_buf_size;
655 if (buf_size != sock_buf_size) {
656 fprintf(stderr, "Returned BUFFER_SIZE too large\n");
660 data = malloc(buf_size);
666 send_buf(fd, data, buf_size, 0, -EMSGSIZE);
668 control_writeln("CLISENT");
674 static void test_seqpacket_bigmsg_server(const struct test_opts *opts)
678 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
684 control_expectln("CLISENT");
689 #define BUF_PATTERN_1 'a'
690 #define BUF_PATTERN_2 'b'
692 static void test_seqpacket_invalid_rec_buffer_client(const struct test_opts *opts)
697 int buf_size = getpagesize() * 3;
699 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
705 buf1 = malloc(buf_size);
707 perror("'malloc()' for 'buf1'");
711 buf2 = malloc(buf_size);
713 perror("'malloc()' for 'buf2'");
717 memset(buf1, BUF_PATTERN_1, buf_size);
718 memset(buf2, BUF_PATTERN_2, buf_size);
720 send_buf(fd, buf1, buf_size, 0, buf_size);
722 send_buf(fd, buf2, buf_size, 0, buf_size);
727 static void test_seqpacket_invalid_rec_buffer_server(const struct test_opts *opts)
730 unsigned char *broken_buf;
731 unsigned char *valid_buf;
732 int page_size = getpagesize();
733 int buf_size = page_size * 3;
735 int prot = PROT_READ | PROT_WRITE;
736 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
739 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
745 /* Setup first buffer. */
746 broken_buf = mmap(NULL, buf_size, prot, flags, -1, 0);
747 if (broken_buf == MAP_FAILED) {
748 perror("mmap for 'broken_buf'");
752 /* Unmap "hole" in buffer. */
753 if (munmap(broken_buf + page_size, page_size)) {
754 perror("'broken_buf' setup");
758 valid_buf = mmap(NULL, buf_size, prot, flags, -1, 0);
759 if (valid_buf == MAP_FAILED) {
760 perror("mmap for 'valid_buf'");
764 /* Try to fill buffer with unmapped middle. */
765 res = read(fd, broken_buf, buf_size);
768 "expected 'broken_buf' read(2) failure, got %zi\n",
773 if (errno != EFAULT) {
774 perror("unexpected errno of 'broken_buf'");
778 /* Try to fill valid buffer. */
779 res = read(fd, valid_buf, buf_size);
781 perror("unexpected 'valid_buf' read(2) failure");
785 if (res != buf_size) {
787 "invalid 'valid_buf' read(2), expected %i, got %zi\n",
792 for (i = 0; i < buf_size; i++) {
793 if (valid_buf[i] != BUF_PATTERN_2) {
795 "invalid pattern for 'valid_buf' at %i, expected %hhX, got %hhX\n",
796 i, BUF_PATTERN_2, valid_buf[i]);
802 munmap(broken_buf, page_size);
803 munmap(broken_buf + page_size * 2, page_size);
804 munmap(valid_buf, buf_size);
808 #define RCVLOWAT_BUF_SIZE 128
810 static void test_stream_poll_rcvlowat_server(const struct test_opts *opts)
815 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
824 control_writeln("SRVSENT");
826 /* Wait until client is ready to receive rest of data. */
827 control_expectln("CLNSENT");
829 for (i = 0; i < RCVLOWAT_BUF_SIZE - 1; i++)
832 /* Keep socket in active state. */
833 control_expectln("POLLDONE");
838 static void test_stream_poll_rcvlowat_client(const struct test_opts *opts)
840 int lowat_val = RCVLOWAT_BUF_SIZE;
841 char buf[RCVLOWAT_BUF_SIZE];
846 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
852 setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
853 lowat_val, "setsockopt(SO_RCVLOWAT)");
855 control_expectln("SRVSENT");
857 /* At this point, server sent 1 byte. */
859 poll_flags = POLLIN | POLLRDNORM;
860 fds.events = poll_flags;
862 /* Try to wait for 1 sec. */
863 if (poll(&fds, 1, 1000) < 0) {
868 /* poll() must return nothing. */
870 fprintf(stderr, "Unexpected poll result %hx\n",
875 /* Tell server to send rest of data. */
876 control_writeln("CLNSENT");
879 if (poll(&fds, 1, 10000) < 0) {
884 /* Only these two bits are expected. */
885 if (fds.revents != poll_flags) {
886 fprintf(stderr, "Unexpected poll result %hx\n",
891 /* Use MSG_DONTWAIT, if call is going to wait, EAGAIN
894 recv_buf(fd, buf, sizeof(buf), MSG_DONTWAIT, RCVLOWAT_BUF_SIZE);
896 control_writeln("POLLDONE");
901 #define INV_BUF_TEST_DATA_LEN 512
903 static void test_inv_buf_client(const struct test_opts *opts, bool stream)
905 unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
906 ssize_t expected_ret;
910 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
912 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
919 control_expectln("SENDDONE");
921 /* Use invalid buffer here. */
922 recv_buf(fd, NULL, sizeof(data), 0, -EFAULT);
925 /* For SOCK_STREAM we must continue reading. */
926 expected_ret = sizeof(data);
928 /* For SOCK_SEQPACKET socket's queue must be empty. */
929 expected_ret = -EAGAIN;
932 recv_buf(fd, data, sizeof(data), MSG_DONTWAIT, expected_ret);
934 control_writeln("DONE");
939 static void test_inv_buf_server(const struct test_opts *opts, bool stream)
941 unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
945 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
947 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
954 send_buf(fd, data, sizeof(data), 0, sizeof(data));
956 control_writeln("SENDDONE");
958 control_expectln("DONE");
963 static void test_stream_inv_buf_client(const struct test_opts *opts)
965 test_inv_buf_client(opts, true);
968 static void test_stream_inv_buf_server(const struct test_opts *opts)
970 test_inv_buf_server(opts, true);
973 static void test_seqpacket_inv_buf_client(const struct test_opts *opts)
975 test_inv_buf_client(opts, false);
978 static void test_seqpacket_inv_buf_server(const struct test_opts *opts)
980 test_inv_buf_server(opts, false);
983 #define HELLO_STR "HELLO"
984 #define WORLD_STR "WORLD"
986 static void test_stream_virtio_skb_merge_client(const struct test_opts *opts)
990 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
996 /* Send first skbuff. */
997 send_buf(fd, HELLO_STR, strlen(HELLO_STR), 0, strlen(HELLO_STR));
999 control_writeln("SEND0");
1000 /* Peer reads part of first skbuff. */
1001 control_expectln("REPLY0");
1003 /* Send second skbuff, it will be appended to the first. */
1004 send_buf(fd, WORLD_STR, strlen(WORLD_STR), 0, strlen(WORLD_STR));
1006 control_writeln("SEND1");
1007 /* Peer reads merged skbuff packet. */
1008 control_expectln("REPLY1");
1013 static void test_stream_virtio_skb_merge_server(const struct test_opts *opts)
1015 size_t read = 0, to_read;
1016 unsigned char buf[64];
1019 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1025 control_expectln("SEND0");
1027 /* Read skbuff partially. */
1029 recv_buf(fd, buf + read, to_read, 0, to_read);
1032 control_writeln("REPLY0");
1033 control_expectln("SEND1");
1035 /* Read the rest of both buffers */
1036 to_read = strlen(HELLO_STR WORLD_STR) - read;
1037 recv_buf(fd, buf + read, to_read, 0, to_read);
1040 /* No more bytes should be there */
1041 to_read = sizeof(buf) - read;
1042 recv_buf(fd, buf + read, to_read, MSG_DONTWAIT, -EAGAIN);
1044 if (memcmp(buf, HELLO_STR WORLD_STR, strlen(HELLO_STR WORLD_STR))) {
1045 fprintf(stderr, "pattern mismatch\n");
1049 control_writeln("REPLY1");
1054 static void test_seqpacket_msg_peek_client(const struct test_opts *opts)
1056 return test_msg_peek_client(opts, true);
1059 static void test_seqpacket_msg_peek_server(const struct test_opts *opts)
1061 return test_msg_peek_server(opts, true);
1064 static sig_atomic_t have_sigpipe;
1066 static void sigpipe(int signo)
1071 static void test_stream_check_sigpipe(int fd)
1077 res = send(fd, "A", 1, 0);
1079 fprintf(stderr, "expected send(2) failure, got %zi\n", res);
1083 if (!have_sigpipe) {
1084 fprintf(stderr, "SIGPIPE expected\n");
1090 res = send(fd, "A", 1, MSG_NOSIGNAL);
1092 fprintf(stderr, "expected send(2) failure, got %zi\n", res);
1097 fprintf(stderr, "SIGPIPE not expected\n");
1102 static void test_stream_shutwr_client(const struct test_opts *opts)
1106 struct sigaction act = {
1107 .sa_handler = sigpipe,
1110 sigaction(SIGPIPE, &act, NULL);
1112 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1118 if (shutdown(fd, SHUT_WR)) {
1123 test_stream_check_sigpipe(fd);
1125 control_writeln("CLIENTDONE");
1130 static void test_stream_shutwr_server(const struct test_opts *opts)
1134 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1140 control_expectln("CLIENTDONE");
1145 static void test_stream_shutrd_client(const struct test_opts *opts)
1149 struct sigaction act = {
1150 .sa_handler = sigpipe,
1153 sigaction(SIGPIPE, &act, NULL);
1155 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1161 control_expectln("SHUTRDDONE");
1163 test_stream_check_sigpipe(fd);
1165 control_writeln("CLIENTDONE");
1170 static void test_stream_shutrd_server(const struct test_opts *opts)
1174 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1180 if (shutdown(fd, SHUT_RD)) {
1185 control_writeln("SHUTRDDONE");
1186 control_expectln("CLIENTDONE");
1191 static void test_double_bind_connect_server(const struct test_opts *opts)
1193 int listen_fd, client_fd, i;
1194 struct sockaddr_vm sa_client;
1195 socklen_t socklen_client = sizeof(sa_client);
1197 listen_fd = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port);
1199 for (i = 0; i < 2; i++) {
1200 control_writeln("LISTENING");
1202 timeout_begin(TIMEOUT);
1204 client_fd = accept(listen_fd, (struct sockaddr *)&sa_client,
1206 timeout_check("accept");
1207 } while (client_fd < 0 && errno == EINTR);
1210 if (client_fd < 0) {
1215 /* Waiting for remote peer to close connection */
1216 vsock_wait_remote_close(client_fd);
1222 static void test_double_bind_connect_client(const struct test_opts *opts)
1226 for (i = 0; i < 2; i++) {
1227 /* Wait until server is ready to accept a new connection */
1228 control_expectln("LISTENING");
1230 /* We use 'peer_port + 1' as "some" port for the 'bind()'
1231 * call. It is safe for overflow, but must be considered,
1232 * when running multiple test applications simultaneously
1233 * where 'peer-port' argument differs by 1.
1235 client_fd = vsock_bind_connect(opts->peer_cid, opts->peer_port,
1236 opts->peer_port + 1, SOCK_STREAM);
1242 #define MSG_BUF_IOCTL_LEN 64
1243 static void test_unsent_bytes_server(const struct test_opts *opts, int type)
1245 unsigned char buf[MSG_BUF_IOCTL_LEN];
1248 client_fd = vsock_accept(VMADDR_CID_ANY, opts->peer_port, NULL, type);
1249 if (client_fd < 0) {
1254 recv_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf));
1255 control_writeln("RECEIVED");
1260 static void test_unsent_bytes_client(const struct test_opts *opts, int type)
1262 unsigned char buf[MSG_BUF_IOCTL_LEN];
1263 int ret, fd, sock_bytes_unsent;
1265 fd = vsock_connect(opts->peer_cid, opts->peer_port, type);
1271 for (int i = 0; i < sizeof(buf); i++)
1272 buf[i] = rand() & 0xFF;
1274 send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
1275 control_expectln("RECEIVED");
1277 ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
1279 if (errno == EOPNOTSUPP) {
1280 fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
1285 } else if (ret == 0 && sock_bytes_unsent != 0) {
1287 "Unexpected 'SIOCOUTQ' value, expected 0, got %i\n",
1295 static void test_stream_unsent_bytes_client(const struct test_opts *opts)
1297 test_unsent_bytes_client(opts, SOCK_STREAM);
1300 static void test_stream_unsent_bytes_server(const struct test_opts *opts)
1302 test_unsent_bytes_server(opts, SOCK_STREAM);
1305 static void test_seqpacket_unsent_bytes_client(const struct test_opts *opts)
1307 test_unsent_bytes_client(opts, SOCK_SEQPACKET);
1310 static void test_seqpacket_unsent_bytes_server(const struct test_opts *opts)
1312 test_unsent_bytes_server(opts, SOCK_SEQPACKET);
1315 #define RCVLOWAT_CREDIT_UPD_BUF_SIZE (1024 * 128)
1316 /* This define is the same as in 'include/linux/virtio_vsock.h':
1317 * it is used to decide when to send credit update message during
1318 * reading from rx queue of a socket. Value and its usage in
1319 * kernel is important for this test.
1321 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (1024 * 64)
1323 static void test_stream_rcvlowat_def_cred_upd_client(const struct test_opts *opts)
1329 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1335 /* Send 1 byte more than peer's buffer size. */
1336 buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE + 1;
1338 buf = malloc(buf_size);
1344 /* Wait until peer sets needed buffer size. */
1345 recv_byte(fd, 1, 0);
1347 if (send(fd, buf, buf_size, 0) != buf_size) {
1348 perror("send failed");
1356 static void test_stream_credit_update_test(const struct test_opts *opts,
1357 bool low_rx_bytes_test)
1362 unsigned long long sock_buf_size;
1366 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1372 buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE;
1374 /* size_t can be < unsigned long long */
1375 sock_buf_size = buf_size;
1377 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
1379 "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
1381 if (low_rx_bytes_test) {
1382 /* Set new SO_RCVLOWAT here. This enables sending credit
1383 * update when number of bytes if our rx queue become <
1384 * SO_RCVLOWAT value.
1386 recv_buf_size = 1 + VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
1388 setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
1389 recv_buf_size, "setsockopt(SO_RCVLOWAT)");
1392 /* Send one dummy byte here, because 'setsockopt()' above also
1393 * sends special packet which tells sender to update our buffer
1394 * size. This 'send_byte()' will serialize such packet with data
1395 * reads in a loop below. Sender starts transmission only when
1396 * it receives this single byte.
1398 send_byte(fd, 1, 0);
1400 buf = malloc(buf_size);
1406 /* Wait until there will be 128KB of data in rx queue. */
1410 res = recv(fd, buf, buf_size, MSG_PEEK);
1411 if (res == buf_size)
1415 fprintf(stderr, "unexpected 'recv()' return: %zi\n", res);
1420 /* There is 128KB of data in the socket's rx queue, dequeue first
1421 * 64KB, credit update is sent if 'low_rx_bytes_test' == true.
1422 * Otherwise, credit update is sent in 'if (!low_rx_bytes_test)'.
1424 recv_buf_size = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
1425 recv_buf(fd, buf, recv_buf_size, 0, recv_buf_size);
1427 if (!low_rx_bytes_test) {
1430 /* Updating SO_RCVLOWAT will send credit update. */
1431 setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
1432 recv_buf_size, "setsockopt(SO_RCVLOWAT)");
1436 fds.events = POLLIN | POLLRDNORM | POLLERR |
1437 POLLRDHUP | POLLHUP;
1439 /* This 'poll()' will return once we receive last byte
1442 if (poll(&fds, 1, -1) < 0) {
1447 if (fds.revents & POLLERR) {
1448 fprintf(stderr, "'poll()' error\n");
1452 if (fds.revents & (POLLIN | POLLRDNORM)) {
1453 recv_buf(fd, buf, recv_buf_size, MSG_DONTWAIT, recv_buf_size);
1455 /* These flags must be set, as there is at
1456 * least 64KB of data ready to read.
1458 fprintf(stderr, "POLLIN | POLLRDNORM expected\n");
1466 static void test_stream_cred_upd_on_low_rx_bytes(const struct test_opts *opts)
1468 test_stream_credit_update_test(opts, true);
1471 static void test_stream_cred_upd_on_set_rcvlowat(const struct test_opts *opts)
1473 test_stream_credit_update_test(opts, false);
1476 static struct test_case test_cases[] = {
1478 .name = "SOCK_STREAM connection reset",
1479 .run_client = test_stream_connection_reset,
1482 .name = "SOCK_STREAM bind only",
1483 .run_client = test_stream_bind_only_client,
1484 .run_server = test_stream_bind_only_server,
1487 .name = "SOCK_STREAM client close",
1488 .run_client = test_stream_client_close_client,
1489 .run_server = test_stream_client_close_server,
1492 .name = "SOCK_STREAM server close",
1493 .run_client = test_stream_server_close_client,
1494 .run_server = test_stream_server_close_server,
1497 .name = "SOCK_STREAM multiple connections",
1498 .run_client = test_stream_multiconn_client,
1499 .run_server = test_stream_multiconn_server,
1502 .name = "SOCK_STREAM MSG_PEEK",
1503 .run_client = test_stream_msg_peek_client,
1504 .run_server = test_stream_msg_peek_server,
1507 .name = "SOCK_SEQPACKET msg bounds",
1508 .run_client = test_seqpacket_msg_bounds_client,
1509 .run_server = test_seqpacket_msg_bounds_server,
1512 .name = "SOCK_SEQPACKET MSG_TRUNC flag",
1513 .run_client = test_seqpacket_msg_trunc_client,
1514 .run_server = test_seqpacket_msg_trunc_server,
1517 .name = "SOCK_SEQPACKET timeout",
1518 .run_client = test_seqpacket_timeout_client,
1519 .run_server = test_seqpacket_timeout_server,
1522 .name = "SOCK_SEQPACKET invalid receive buffer",
1523 .run_client = test_seqpacket_invalid_rec_buffer_client,
1524 .run_server = test_seqpacket_invalid_rec_buffer_server,
1527 .name = "SOCK_STREAM poll() + SO_RCVLOWAT",
1528 .run_client = test_stream_poll_rcvlowat_client,
1529 .run_server = test_stream_poll_rcvlowat_server,
1532 .name = "SOCK_SEQPACKET big message",
1533 .run_client = test_seqpacket_bigmsg_client,
1534 .run_server = test_seqpacket_bigmsg_server,
1537 .name = "SOCK_STREAM test invalid buffer",
1538 .run_client = test_stream_inv_buf_client,
1539 .run_server = test_stream_inv_buf_server,
1542 .name = "SOCK_SEQPACKET test invalid buffer",
1543 .run_client = test_seqpacket_inv_buf_client,
1544 .run_server = test_seqpacket_inv_buf_server,
1547 .name = "SOCK_STREAM virtio skb merge",
1548 .run_client = test_stream_virtio_skb_merge_client,
1549 .run_server = test_stream_virtio_skb_merge_server,
1552 .name = "SOCK_SEQPACKET MSG_PEEK",
1553 .run_client = test_seqpacket_msg_peek_client,
1554 .run_server = test_seqpacket_msg_peek_server,
1557 .name = "SOCK_STREAM SHUT_WR",
1558 .run_client = test_stream_shutwr_client,
1559 .run_server = test_stream_shutwr_server,
1562 .name = "SOCK_STREAM SHUT_RD",
1563 .run_client = test_stream_shutrd_client,
1564 .run_server = test_stream_shutrd_server,
1567 .name = "SOCK_STREAM MSG_ZEROCOPY",
1568 .run_client = test_stream_msgzcopy_client,
1569 .run_server = test_stream_msgzcopy_server,
1572 .name = "SOCK_SEQPACKET MSG_ZEROCOPY",
1573 .run_client = test_seqpacket_msgzcopy_client,
1574 .run_server = test_seqpacket_msgzcopy_server,
1577 .name = "SOCK_STREAM MSG_ZEROCOPY empty MSG_ERRQUEUE",
1578 .run_client = test_stream_msgzcopy_empty_errq_client,
1579 .run_server = test_stream_msgzcopy_empty_errq_server,
1582 .name = "SOCK_STREAM double bind connect",
1583 .run_client = test_double_bind_connect_client,
1584 .run_server = test_double_bind_connect_server,
1587 .name = "SOCK_STREAM virtio credit update + SO_RCVLOWAT",
1588 .run_client = test_stream_rcvlowat_def_cred_upd_client,
1589 .run_server = test_stream_cred_upd_on_set_rcvlowat,
1592 .name = "SOCK_STREAM virtio credit update + low rx_bytes",
1593 .run_client = test_stream_rcvlowat_def_cred_upd_client,
1594 .run_server = test_stream_cred_upd_on_low_rx_bytes,
1597 .name = "SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes",
1598 .run_client = test_stream_unsent_bytes_client,
1599 .run_server = test_stream_unsent_bytes_server,
1602 .name = "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes",
1603 .run_client = test_seqpacket_unsent_bytes_client,
1604 .run_server = test_seqpacket_unsent_bytes_server,
1609 static const char optstring[] = "";
1610 static const struct option longopts[] = {
1612 .name = "control-host",
1613 .has_arg = required_argument,
1617 .name = "control-port",
1618 .has_arg = required_argument,
1623 .has_arg = required_argument,
1628 .has_arg = required_argument,
1632 .name = "peer-port",
1633 .has_arg = required_argument,
1638 .has_arg = no_argument,
1643 .has_arg = required_argument,
1648 .has_arg = no_argument,
1654 static void usage(void)
1656 fprintf(stderr, "Usage: vsock_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--peer-port=<port>] [--list] [--skip=<test_id>]\n"
1658 " Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n"
1659 " Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
1661 "Run vsock.ko tests. Must be launched in both guest\n"
1662 "and host. One side must use --mode=client and\n"
1663 "the other side must use --mode=server.\n"
1665 "A TCP control socket connection is used to coordinate tests\n"
1666 "between the client and the server. The server requires a\n"
1667 "listen address and the client requires an address to\n"
1670 "The CID of the other side must be given with --peer-cid=<cid>.\n"
1671 "During the test, two AF_VSOCK ports will be used: the port\n"
1672 "specified with --peer-port=<port> (or the default port)\n"
1673 "and the next one.\n"
1676 " --help This help message\n"
1677 " --control-host <host> Server IP address to connect to\n"
1678 " --control-port <port> Server port to listen on/connect to\n"
1679 " --mode client|server Server or client mode\n"
1680 " --peer-cid <cid> CID of the other side\n"
1681 " --peer-port <port> AF_VSOCK port used for the test [default: %d]\n"
1682 " --list List of tests that will be executed\n"
1683 " --skip <test_id> Test ID to skip;\n"
1684 " use multiple --skip options to skip more tests\n",
1690 int main(int argc, char **argv)
1692 const char *control_host = NULL;
1693 const char *control_port = NULL;
1694 struct test_opts opts = {
1695 .mode = TEST_MODE_UNSET,
1696 .peer_cid = VMADDR_CID_ANY,
1697 .peer_port = DEFAULT_PEER_PORT,
1704 int opt = getopt_long(argc, argv, optstring, longopts, NULL);
1711 control_host = optarg;
1714 if (strcmp(optarg, "client") == 0)
1715 opts.mode = TEST_MODE_CLIENT;
1716 else if (strcmp(optarg, "server") == 0)
1717 opts.mode = TEST_MODE_SERVER;
1719 fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
1720 return EXIT_FAILURE;
1724 opts.peer_cid = parse_cid(optarg);
1727 opts.peer_port = parse_port(optarg);
1730 control_port = optarg;
1733 list_tests(test_cases);
1736 skip_test(test_cases, ARRAY_SIZE(test_cases) - 1,
1747 if (opts.mode == TEST_MODE_UNSET)
1749 if (opts.peer_cid == VMADDR_CID_ANY)
1752 if (!control_host) {
1753 if (opts.mode != TEST_MODE_SERVER)
1755 control_host = "0.0.0.0";
1758 control_init(control_host, control_port,
1759 opts.mode == TEST_MODE_SERVER);
1761 run_tests(test_cases, &opts);
1764 return EXIT_SUCCESS;