xref: /linux/tools/testing/vsock/vsock_test.c (revision b803c4a4f78834b31ebfbbcea350473333760559)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vsock_test - vsock.ko test suite
4  *
5  * Copyright (C) 2017 Red Hat, Inc.
6  *
7  * Author: Stefan Hajnoczi <stefanha@redhat.com>
8  */
9 
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>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <time.h>
20 #include <sys/mman.h>
21 #include <poll.h>
22 #include <signal.h>
23 #include <sys/ioctl.h>
24 #include <linux/sockios.h>
25 #include <linux/time64.h>
26 
27 #include "vsock_test_zerocopy.h"
28 #include "timeout.h"
29 #include "control.h"
30 #include "util.h"
31 
32 /* Basic messages for control_writeulong(), control_readulong() */
33 #define CONTROL_CONTINUE	1
34 #define CONTROL_DONE		0
35 
36 static void test_stream_connection_reset(const struct test_opts *opts)
37 {
38 	union {
39 		struct sockaddr sa;
40 		struct sockaddr_vm svm;
41 	} addr = {
42 		.svm = {
43 			.svm_family = AF_VSOCK,
44 			.svm_port = opts->peer_port,
45 			.svm_cid = opts->peer_cid,
46 		},
47 	};
48 	int ret;
49 	int fd;
50 
51 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
52 
53 	timeout_begin(TIMEOUT);
54 	do {
55 		ret = connect(fd, &addr.sa, sizeof(addr.svm));
56 		timeout_check("connect");
57 	} while (ret < 0 && errno == EINTR);
58 	timeout_end();
59 
60 	if (ret != -1) {
61 		fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
62 		exit(EXIT_FAILURE);
63 	}
64 	if (errno != ECONNRESET) {
65 		fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
66 		exit(EXIT_FAILURE);
67 	}
68 
69 	close(fd);
70 }
71 
72 static void test_stream_bind_only_client(const struct test_opts *opts)
73 {
74 	union {
75 		struct sockaddr sa;
76 		struct sockaddr_vm svm;
77 	} addr = {
78 		.svm = {
79 			.svm_family = AF_VSOCK,
80 			.svm_port = opts->peer_port,
81 			.svm_cid = opts->peer_cid,
82 		},
83 	};
84 	int ret;
85 	int fd;
86 
87 	/* Wait for the server to be ready */
88 	control_expectln("BIND");
89 
90 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
91 
92 	timeout_begin(TIMEOUT);
93 	do {
94 		ret = connect(fd, &addr.sa, sizeof(addr.svm));
95 		timeout_check("connect");
96 	} while (ret < 0 && errno == EINTR);
97 	timeout_end();
98 
99 	if (ret != -1) {
100 		fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
101 		exit(EXIT_FAILURE);
102 	}
103 	if (errno != ECONNRESET) {
104 		fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
105 		exit(EXIT_FAILURE);
106 	}
107 
108 	/* Notify the server that the client has finished */
109 	control_writeln("DONE");
110 
111 	close(fd);
112 }
113 
114 static void test_stream_bind_only_server(const struct test_opts *opts)
115 {
116 	int fd;
117 
118 	fd = vsock_bind(VMADDR_CID_ANY, opts->peer_port, SOCK_STREAM);
119 
120 	/* Notify the client that the server is ready */
121 	control_writeln("BIND");
122 
123 	/* Wait for the client to finish */
124 	control_expectln("DONE");
125 
126 	close(fd);
127 }
128 
129 static void test_stream_client_close_client(const struct test_opts *opts)
130 {
131 	int fd;
132 
133 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
134 	if (fd < 0) {
135 		perror("connect");
136 		exit(EXIT_FAILURE);
137 	}
138 
139 	send_byte(fd, 1, 0);
140 	close(fd);
141 }
142 
143 static void test_stream_client_close_server(const struct test_opts *opts)
144 {
145 	int fd;
146 
147 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
148 	if (fd < 0) {
149 		perror("accept");
150 		exit(EXIT_FAILURE);
151 	}
152 
153 	/* Wait for the remote to close the connection, before check
154 	 * -EPIPE error on send.
155 	 */
156 	vsock_wait_remote_close(fd);
157 
158 	send_byte(fd, -EPIPE, 0);
159 	recv_byte(fd, 1, 0);
160 	recv_byte(fd, 0, 0);
161 	close(fd);
162 }
163 
164 static void test_stream_server_close_client(const struct test_opts *opts)
165 {
166 	int fd;
167 
168 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
169 	if (fd < 0) {
170 		perror("connect");
171 		exit(EXIT_FAILURE);
172 	}
173 
174 	/* Wait for the remote to close the connection, before check
175 	 * -EPIPE error on send.
176 	 */
177 	vsock_wait_remote_close(fd);
178 
179 	send_byte(fd, -EPIPE, 0);
180 	recv_byte(fd, 1, 0);
181 	recv_byte(fd, 0, 0);
182 	close(fd);
183 }
184 
185 static void test_stream_server_close_server(const struct test_opts *opts)
186 {
187 	int fd;
188 
189 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
190 	if (fd < 0) {
191 		perror("accept");
192 		exit(EXIT_FAILURE);
193 	}
194 
195 	send_byte(fd, 1, 0);
196 	close(fd);
197 }
198 
199 /* With the standard socket sizes, VMCI is able to support about 100
200  * concurrent stream connections.
201  */
202 #define MULTICONN_NFDS 100
203 
204 static void test_stream_multiconn_client(const struct test_opts *opts)
205 {
206 	int fds[MULTICONN_NFDS];
207 	int i;
208 
209 	for (i = 0; i < MULTICONN_NFDS; i++) {
210 		fds[i] = vsock_stream_connect(opts->peer_cid, opts->peer_port);
211 		if (fds[i] < 0) {
212 			perror("connect");
213 			exit(EXIT_FAILURE);
214 		}
215 	}
216 
217 	for (i = 0; i < MULTICONN_NFDS; i++) {
218 		if (i % 2)
219 			recv_byte(fds[i], 1, 0);
220 		else
221 			send_byte(fds[i], 1, 0);
222 	}
223 
224 	for (i = 0; i < MULTICONN_NFDS; i++)
225 		close(fds[i]);
226 }
227 
228 static void test_stream_multiconn_server(const struct test_opts *opts)
229 {
230 	int fds[MULTICONN_NFDS];
231 	int i;
232 
233 	for (i = 0; i < MULTICONN_NFDS; i++) {
234 		fds[i] = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
235 		if (fds[i] < 0) {
236 			perror("accept");
237 			exit(EXIT_FAILURE);
238 		}
239 	}
240 
241 	for (i = 0; i < MULTICONN_NFDS; i++) {
242 		if (i % 2)
243 			send_byte(fds[i], 1, 0);
244 		else
245 			recv_byte(fds[i], 1, 0);
246 	}
247 
248 	for (i = 0; i < MULTICONN_NFDS; i++)
249 		close(fds[i]);
250 }
251 
252 #define MSG_PEEK_BUF_LEN 64
253 
254 static void test_msg_peek_client(const struct test_opts *opts,
255 				 bool seqpacket)
256 {
257 	unsigned char buf[MSG_PEEK_BUF_LEN];
258 	int fd;
259 	int i;
260 
261 	if (seqpacket)
262 		fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
263 	else
264 		fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
265 
266 	if (fd < 0) {
267 		perror("connect");
268 		exit(EXIT_FAILURE);
269 	}
270 
271 	for (i = 0; i < sizeof(buf); i++)
272 		buf[i] = rand() & 0xFF;
273 
274 	control_expectln("SRVREADY");
275 
276 	send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
277 
278 	close(fd);
279 }
280 
281 static void test_msg_peek_server(const struct test_opts *opts,
282 				 bool seqpacket)
283 {
284 	unsigned char buf_half[MSG_PEEK_BUF_LEN / 2];
285 	unsigned char buf_normal[MSG_PEEK_BUF_LEN];
286 	unsigned char buf_peek[MSG_PEEK_BUF_LEN];
287 	int fd;
288 
289 	if (seqpacket)
290 		fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
291 	else
292 		fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
293 
294 	if (fd < 0) {
295 		perror("accept");
296 		exit(EXIT_FAILURE);
297 	}
298 
299 	/* Peek from empty socket. */
300 	recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK | MSG_DONTWAIT,
301 		 -EAGAIN);
302 
303 	control_writeln("SRVREADY");
304 
305 	/* Peek part of data. */
306 	recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK, sizeof(buf_half));
307 
308 	/* Peek whole data. */
309 	recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK, sizeof(buf_peek));
310 
311 	/* Compare partial and full peek. */
312 	if (memcmp(buf_half, buf_peek, sizeof(buf_half))) {
313 		fprintf(stderr, "Partial peek data mismatch\n");
314 		exit(EXIT_FAILURE);
315 	}
316 
317 	if (seqpacket) {
318 		/* This type of socket supports MSG_TRUNC flag,
319 		 * so check it with MSG_PEEK. We must get length
320 		 * of the message.
321 		 */
322 		recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK | MSG_TRUNC,
323 			 sizeof(buf_peek));
324 	}
325 
326 	recv_buf(fd, buf_normal, sizeof(buf_normal), 0, sizeof(buf_normal));
327 
328 	/* Compare full peek and normal read. */
329 	if (memcmp(buf_peek, buf_normal, sizeof(buf_peek))) {
330 		fprintf(stderr, "Full peek data mismatch\n");
331 		exit(EXIT_FAILURE);
332 	}
333 
334 	close(fd);
335 }
336 
337 static void test_stream_msg_peek_client(const struct test_opts *opts)
338 {
339 	return test_msg_peek_client(opts, false);
340 }
341 
342 static void test_stream_msg_peek_server(const struct test_opts *opts)
343 {
344 	return test_msg_peek_server(opts, false);
345 }
346 
347 #define SOCK_BUF_SIZE (2 * 1024 * 1024)
348 #define MAX_MSG_PAGES 4
349 
350 static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
351 {
352 	unsigned long curr_hash;
353 	size_t max_msg_size;
354 	int page_size;
355 	int msg_count;
356 	int fd;
357 
358 	fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
359 	if (fd < 0) {
360 		perror("connect");
361 		exit(EXIT_FAILURE);
362 	}
363 
364 	/* Wait, until receiver sets buffer size. */
365 	control_expectln("SRVREADY");
366 
367 	curr_hash = 0;
368 	page_size = getpagesize();
369 	max_msg_size = MAX_MSG_PAGES * page_size;
370 	msg_count = SOCK_BUF_SIZE / max_msg_size;
371 
372 	for (int i = 0; i < msg_count; i++) {
373 		size_t buf_size;
374 		int flags;
375 		void *buf;
376 
377 		/* Use "small" buffers and "big" buffers. */
378 		if (i & 1)
379 			buf_size = page_size +
380 					(rand() % (max_msg_size - page_size));
381 		else
382 			buf_size = 1 + (rand() % page_size);
383 
384 		buf = malloc(buf_size);
385 
386 		if (!buf) {
387 			perror("malloc");
388 			exit(EXIT_FAILURE);
389 		}
390 
391 		memset(buf, rand() & 0xff, buf_size);
392 		/* Set at least one MSG_EOR + some random. */
393 		if (i == (msg_count / 2) || (rand() & 1)) {
394 			flags = MSG_EOR;
395 			curr_hash++;
396 		} else {
397 			flags = 0;
398 		}
399 
400 		send_buf(fd, buf, buf_size, flags, buf_size);
401 
402 		/*
403 		 * Hash sum is computed at both client and server in
404 		 * the same way:
405 		 * H += hash('message data')
406 		 * Such hash "controls" both data integrity and message
407 		 * bounds. After data exchange, both sums are compared
408 		 * using control socket, and if message bounds wasn't
409 		 * broken - two values must be equal.
410 		 */
411 		curr_hash += hash_djb2(buf, buf_size);
412 		free(buf);
413 	}
414 
415 	control_writeln("SENDDONE");
416 	control_writeulong(curr_hash);
417 	close(fd);
418 }
419 
420 static void test_seqpacket_msg_bounds_server(const struct test_opts *opts)
421 {
422 	unsigned long long sock_buf_size;
423 	unsigned long remote_hash;
424 	unsigned long curr_hash;
425 	int fd;
426 	struct msghdr msg = {0};
427 	struct iovec iov = {0};
428 
429 	fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
430 	if (fd < 0) {
431 		perror("accept");
432 		exit(EXIT_FAILURE);
433 	}
434 
435 	sock_buf_size = SOCK_BUF_SIZE;
436 
437 	setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
438 			     sock_buf_size,
439 			     "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
440 
441 	setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
442 			     sock_buf_size,
443 			     "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
444 
445 	/* Ready to receive data. */
446 	control_writeln("SRVREADY");
447 	/* Wait, until peer sends whole data. */
448 	control_expectln("SENDDONE");
449 	iov.iov_len = MAX_MSG_PAGES * getpagesize();
450 	iov.iov_base = malloc(iov.iov_len);
451 	if (!iov.iov_base) {
452 		perror("malloc");
453 		exit(EXIT_FAILURE);
454 	}
455 
456 	msg.msg_iov = &iov;
457 	msg.msg_iovlen = 1;
458 
459 	curr_hash = 0;
460 
461 	while (1) {
462 		ssize_t recv_size;
463 
464 		recv_size = recvmsg(fd, &msg, 0);
465 
466 		if (!recv_size)
467 			break;
468 
469 		if (recv_size < 0) {
470 			perror("recvmsg");
471 			exit(EXIT_FAILURE);
472 		}
473 
474 		if (msg.msg_flags & MSG_EOR)
475 			curr_hash++;
476 
477 		curr_hash += hash_djb2(msg.msg_iov[0].iov_base, recv_size);
478 	}
479 
480 	free(iov.iov_base);
481 	close(fd);
482 	remote_hash = control_readulong();
483 
484 	if (curr_hash != remote_hash) {
485 		fprintf(stderr, "Message bounds broken\n");
486 		exit(EXIT_FAILURE);
487 	}
488 }
489 
490 #define MESSAGE_TRUNC_SZ 32
491 static void test_seqpacket_msg_trunc_client(const struct test_opts *opts)
492 {
493 	int fd;
494 	char buf[MESSAGE_TRUNC_SZ];
495 
496 	fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
497 	if (fd < 0) {
498 		perror("connect");
499 		exit(EXIT_FAILURE);
500 	}
501 
502 	send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
503 
504 	control_writeln("SENDDONE");
505 	close(fd);
506 }
507 
508 static void test_seqpacket_msg_trunc_server(const struct test_opts *opts)
509 {
510 	int fd;
511 	char buf[MESSAGE_TRUNC_SZ / 2];
512 	struct msghdr msg = {0};
513 	struct iovec iov = {0};
514 
515 	fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
516 	if (fd < 0) {
517 		perror("accept");
518 		exit(EXIT_FAILURE);
519 	}
520 
521 	control_expectln("SENDDONE");
522 	iov.iov_base = buf;
523 	iov.iov_len = sizeof(buf);
524 	msg.msg_iov = &iov;
525 	msg.msg_iovlen = 1;
526 
527 	ssize_t ret = recvmsg(fd, &msg, MSG_TRUNC);
528 
529 	if (ret != MESSAGE_TRUNC_SZ) {
530 		printf("%zi\n", ret);
531 		perror("MSG_TRUNC doesn't work");
532 		exit(EXIT_FAILURE);
533 	}
534 
535 	if (!(msg.msg_flags & MSG_TRUNC)) {
536 		fprintf(stderr, "MSG_TRUNC expected\n");
537 		exit(EXIT_FAILURE);
538 	}
539 
540 	close(fd);
541 }
542 
543 static time_t current_nsec(void)
544 {
545 	struct timespec ts;
546 
547 	if (clock_gettime(CLOCK_REALTIME, &ts)) {
548 		perror("clock_gettime(3) failed");
549 		exit(EXIT_FAILURE);
550 	}
551 
552 	return (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec;
553 }
554 
555 #define RCVTIMEO_TIMEOUT_SEC 1
556 #define READ_OVERHEAD_NSEC 250000000 /* 0.25 sec */
557 
558 static void test_seqpacket_timeout_client(const struct test_opts *opts)
559 {
560 	int fd;
561 	struct timeval tv;
562 	char dummy;
563 	time_t read_enter_ns;
564 	time_t read_overhead_ns;
565 
566 	fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
567 	if (fd < 0) {
568 		perror("connect");
569 		exit(EXIT_FAILURE);
570 	}
571 
572 	tv.tv_sec = RCVTIMEO_TIMEOUT_SEC;
573 	tv.tv_usec = 0;
574 
575 	setsockopt_timeval_check(fd, SOL_SOCKET, SO_RCVTIMEO, tv,
576 				 "setsockopt(SO_RCVTIMEO)");
577 
578 	read_enter_ns = current_nsec();
579 
580 	if (read(fd, &dummy, sizeof(dummy)) != -1) {
581 		fprintf(stderr,
582 			"expected 'dummy' read(2) failure\n");
583 		exit(EXIT_FAILURE);
584 	}
585 
586 	if (errno != EAGAIN) {
587 		perror("EAGAIN expected");
588 		exit(EXIT_FAILURE);
589 	}
590 
591 	read_overhead_ns = current_nsec() - read_enter_ns -
592 			   NSEC_PER_SEC * RCVTIMEO_TIMEOUT_SEC;
593 
594 	if (read_overhead_ns > READ_OVERHEAD_NSEC) {
595 		fprintf(stderr,
596 			"too much time in read(2), %lu > %i ns\n",
597 			read_overhead_ns, READ_OVERHEAD_NSEC);
598 		exit(EXIT_FAILURE);
599 	}
600 
601 	control_writeln("WAITDONE");
602 	close(fd);
603 }
604 
605 static void test_seqpacket_timeout_server(const struct test_opts *opts)
606 {
607 	int fd;
608 
609 	fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
610 	if (fd < 0) {
611 		perror("accept");
612 		exit(EXIT_FAILURE);
613 	}
614 
615 	control_expectln("WAITDONE");
616 	close(fd);
617 }
618 
619 static void test_seqpacket_bigmsg_client(const struct test_opts *opts)
620 {
621 	unsigned long long sock_buf_size;
622 	size_t buf_size;
623 	socklen_t len;
624 	void *data;
625 	int fd;
626 
627 	len = sizeof(sock_buf_size);
628 
629 	fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
630 	if (fd < 0) {
631 		perror("connect");
632 		exit(EXIT_FAILURE);
633 	}
634 
635 	if (getsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
636 		       &sock_buf_size, &len)) {
637 		perror("getsockopt");
638 		exit(EXIT_FAILURE);
639 	}
640 
641 	sock_buf_size++;
642 
643 	/* size_t can be < unsigned long long */
644 	buf_size = (size_t)sock_buf_size;
645 	if (buf_size != sock_buf_size) {
646 		fprintf(stderr, "Returned BUFFER_SIZE too large\n");
647 		exit(EXIT_FAILURE);
648 	}
649 
650 	data = malloc(buf_size);
651 	if (!data) {
652 		perror("malloc");
653 		exit(EXIT_FAILURE);
654 	}
655 
656 	send_buf(fd, data, buf_size, 0, -EMSGSIZE);
657 
658 	control_writeln("CLISENT");
659 
660 	free(data);
661 	close(fd);
662 }
663 
664 static void test_seqpacket_bigmsg_server(const struct test_opts *opts)
665 {
666 	int fd;
667 
668 	fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
669 	if (fd < 0) {
670 		perror("accept");
671 		exit(EXIT_FAILURE);
672 	}
673 
674 	control_expectln("CLISENT");
675 
676 	close(fd);
677 }
678 
679 #define BUF_PATTERN_1 'a'
680 #define BUF_PATTERN_2 'b'
681 
682 static void test_seqpacket_invalid_rec_buffer_client(const struct test_opts *opts)
683 {
684 	int fd;
685 	unsigned char *buf1;
686 	unsigned char *buf2;
687 	int buf_size = getpagesize() * 3;
688 
689 	fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
690 	if (fd < 0) {
691 		perror("connect");
692 		exit(EXIT_FAILURE);
693 	}
694 
695 	buf1 = malloc(buf_size);
696 	if (!buf1) {
697 		perror("'malloc()' for 'buf1'");
698 		exit(EXIT_FAILURE);
699 	}
700 
701 	buf2 = malloc(buf_size);
702 	if (!buf2) {
703 		perror("'malloc()' for 'buf2'");
704 		exit(EXIT_FAILURE);
705 	}
706 
707 	memset(buf1, BUF_PATTERN_1, buf_size);
708 	memset(buf2, BUF_PATTERN_2, buf_size);
709 
710 	send_buf(fd, buf1, buf_size, 0, buf_size);
711 
712 	send_buf(fd, buf2, buf_size, 0, buf_size);
713 
714 	close(fd);
715 }
716 
717 static void test_seqpacket_invalid_rec_buffer_server(const struct test_opts *opts)
718 {
719 	int fd;
720 	unsigned char *broken_buf;
721 	unsigned char *valid_buf;
722 	int page_size = getpagesize();
723 	int buf_size = page_size * 3;
724 	ssize_t res;
725 	int prot = PROT_READ | PROT_WRITE;
726 	int flags = MAP_PRIVATE | MAP_ANONYMOUS;
727 	int i;
728 
729 	fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
730 	if (fd < 0) {
731 		perror("accept");
732 		exit(EXIT_FAILURE);
733 	}
734 
735 	/* Setup first buffer. */
736 	broken_buf = mmap(NULL, buf_size, prot, flags, -1, 0);
737 	if (broken_buf == MAP_FAILED) {
738 		perror("mmap for 'broken_buf'");
739 		exit(EXIT_FAILURE);
740 	}
741 
742 	/* Unmap "hole" in buffer. */
743 	if (munmap(broken_buf + page_size, page_size)) {
744 		perror("'broken_buf' setup");
745 		exit(EXIT_FAILURE);
746 	}
747 
748 	valid_buf = mmap(NULL, buf_size, prot, flags, -1, 0);
749 	if (valid_buf == MAP_FAILED) {
750 		perror("mmap for 'valid_buf'");
751 		exit(EXIT_FAILURE);
752 	}
753 
754 	/* Try to fill buffer with unmapped middle. */
755 	res = read(fd, broken_buf, buf_size);
756 	if (res != -1) {
757 		fprintf(stderr,
758 			"expected 'broken_buf' read(2) failure, got %zi\n",
759 			res);
760 		exit(EXIT_FAILURE);
761 	}
762 
763 	if (errno != EFAULT) {
764 		perror("unexpected errno of 'broken_buf'");
765 		exit(EXIT_FAILURE);
766 	}
767 
768 	/* Try to fill valid buffer. */
769 	res = read(fd, valid_buf, buf_size);
770 	if (res < 0) {
771 		perror("unexpected 'valid_buf' read(2) failure");
772 		exit(EXIT_FAILURE);
773 	}
774 
775 	if (res != buf_size) {
776 		fprintf(stderr,
777 			"invalid 'valid_buf' read(2), expected %i, got %zi\n",
778 			buf_size, res);
779 		exit(EXIT_FAILURE);
780 	}
781 
782 	for (i = 0; i < buf_size; i++) {
783 		if (valid_buf[i] != BUF_PATTERN_2) {
784 			fprintf(stderr,
785 				"invalid pattern for 'valid_buf' at %i, expected %hhX, got %hhX\n",
786 				i, BUF_PATTERN_2, valid_buf[i]);
787 			exit(EXIT_FAILURE);
788 		}
789 	}
790 
791 	/* Unmap buffers. */
792 	munmap(broken_buf, page_size);
793 	munmap(broken_buf + page_size * 2, page_size);
794 	munmap(valid_buf, buf_size);
795 	close(fd);
796 }
797 
798 #define RCVLOWAT_BUF_SIZE 128
799 
800 static void test_stream_poll_rcvlowat_server(const struct test_opts *opts)
801 {
802 	int fd;
803 	int i;
804 
805 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
806 	if (fd < 0) {
807 		perror("accept");
808 		exit(EXIT_FAILURE);
809 	}
810 
811 	/* Send 1 byte. */
812 	send_byte(fd, 1, 0);
813 
814 	control_writeln("SRVSENT");
815 
816 	/* Wait until client is ready to receive rest of data. */
817 	control_expectln("CLNSENT");
818 
819 	for (i = 0; i < RCVLOWAT_BUF_SIZE - 1; i++)
820 		send_byte(fd, 1, 0);
821 
822 	/* Keep socket in active state. */
823 	control_expectln("POLLDONE");
824 
825 	close(fd);
826 }
827 
828 static void test_stream_poll_rcvlowat_client(const struct test_opts *opts)
829 {
830 	int lowat_val = RCVLOWAT_BUF_SIZE;
831 	char buf[RCVLOWAT_BUF_SIZE];
832 	struct pollfd fds;
833 	short poll_flags;
834 	int fd;
835 
836 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
837 	if (fd < 0) {
838 		perror("connect");
839 		exit(EXIT_FAILURE);
840 	}
841 
842 	setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
843 			     lowat_val, "setsockopt(SO_RCVLOWAT)");
844 
845 	control_expectln("SRVSENT");
846 
847 	/* At this point, server sent 1 byte. */
848 	fds.fd = fd;
849 	poll_flags = POLLIN | POLLRDNORM;
850 	fds.events = poll_flags;
851 
852 	/* Try to wait for 1 sec. */
853 	if (poll(&fds, 1, 1000) < 0) {
854 		perror("poll");
855 		exit(EXIT_FAILURE);
856 	}
857 
858 	/* poll() must return nothing. */
859 	if (fds.revents) {
860 		fprintf(stderr, "Unexpected poll result %hx\n",
861 			fds.revents);
862 		exit(EXIT_FAILURE);
863 	}
864 
865 	/* Tell server to send rest of data. */
866 	control_writeln("CLNSENT");
867 
868 	/* Poll for data. */
869 	if (poll(&fds, 1, 10000) < 0) {
870 		perror("poll");
871 		exit(EXIT_FAILURE);
872 	}
873 
874 	/* Only these two bits are expected. */
875 	if (fds.revents != poll_flags) {
876 		fprintf(stderr, "Unexpected poll result %hx\n",
877 			fds.revents);
878 		exit(EXIT_FAILURE);
879 	}
880 
881 	/* Use MSG_DONTWAIT, if call is going to wait, EAGAIN
882 	 * will be returned.
883 	 */
884 	recv_buf(fd, buf, sizeof(buf), MSG_DONTWAIT, RCVLOWAT_BUF_SIZE);
885 
886 	control_writeln("POLLDONE");
887 
888 	close(fd);
889 }
890 
891 #define INV_BUF_TEST_DATA_LEN 512
892 
893 static void test_inv_buf_client(const struct test_opts *opts, bool stream)
894 {
895 	unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
896 	ssize_t expected_ret;
897 	int fd;
898 
899 	if (stream)
900 		fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
901 	else
902 		fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
903 
904 	if (fd < 0) {
905 		perror("connect");
906 		exit(EXIT_FAILURE);
907 	}
908 
909 	control_expectln("SENDDONE");
910 
911 	/* Use invalid buffer here. */
912 	recv_buf(fd, NULL, sizeof(data), 0, -EFAULT);
913 
914 	if (stream) {
915 		/* For SOCK_STREAM we must continue reading. */
916 		expected_ret = sizeof(data);
917 	} else {
918 		/* For SOCK_SEQPACKET socket's queue must be empty. */
919 		expected_ret = -EAGAIN;
920 	}
921 
922 	recv_buf(fd, data, sizeof(data), MSG_DONTWAIT, expected_ret);
923 
924 	control_writeln("DONE");
925 
926 	close(fd);
927 }
928 
929 static void test_inv_buf_server(const struct test_opts *opts, bool stream)
930 {
931 	unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
932 	int fd;
933 
934 	if (stream)
935 		fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
936 	else
937 		fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
938 
939 	if (fd < 0) {
940 		perror("accept");
941 		exit(EXIT_FAILURE);
942 	}
943 
944 	send_buf(fd, data, sizeof(data), 0, sizeof(data));
945 
946 	control_writeln("SENDDONE");
947 
948 	control_expectln("DONE");
949 
950 	close(fd);
951 }
952 
953 static void test_stream_inv_buf_client(const struct test_opts *opts)
954 {
955 	test_inv_buf_client(opts, true);
956 }
957 
958 static void test_stream_inv_buf_server(const struct test_opts *opts)
959 {
960 	test_inv_buf_server(opts, true);
961 }
962 
963 static void test_seqpacket_inv_buf_client(const struct test_opts *opts)
964 {
965 	test_inv_buf_client(opts, false);
966 }
967 
968 static void test_seqpacket_inv_buf_server(const struct test_opts *opts)
969 {
970 	test_inv_buf_server(opts, false);
971 }
972 
973 #define HELLO_STR "HELLO"
974 #define WORLD_STR "WORLD"
975 
976 static void test_stream_virtio_skb_merge_client(const struct test_opts *opts)
977 {
978 	int fd;
979 
980 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
981 	if (fd < 0) {
982 		perror("connect");
983 		exit(EXIT_FAILURE);
984 	}
985 
986 	/* Send first skbuff. */
987 	send_buf(fd, HELLO_STR, strlen(HELLO_STR), 0, strlen(HELLO_STR));
988 
989 	control_writeln("SEND0");
990 	/* Peer reads part of first skbuff. */
991 	control_expectln("REPLY0");
992 
993 	/* Send second skbuff, it will be appended to the first. */
994 	send_buf(fd, WORLD_STR, strlen(WORLD_STR), 0, strlen(WORLD_STR));
995 
996 	control_writeln("SEND1");
997 	/* Peer reads merged skbuff packet. */
998 	control_expectln("REPLY1");
999 
1000 	close(fd);
1001 }
1002 
1003 static void test_stream_virtio_skb_merge_server(const struct test_opts *opts)
1004 {
1005 	size_t read = 0, to_read;
1006 	unsigned char buf[64];
1007 	int fd;
1008 
1009 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1010 	if (fd < 0) {
1011 		perror("accept");
1012 		exit(EXIT_FAILURE);
1013 	}
1014 
1015 	control_expectln("SEND0");
1016 
1017 	/* Read skbuff partially. */
1018 	to_read = 2;
1019 	recv_buf(fd, buf + read, to_read, 0, to_read);
1020 	read += to_read;
1021 
1022 	control_writeln("REPLY0");
1023 	control_expectln("SEND1");
1024 
1025 	/* Read the rest of both buffers */
1026 	to_read = strlen(HELLO_STR WORLD_STR) - read;
1027 	recv_buf(fd, buf + read, to_read, 0, to_read);
1028 	read += to_read;
1029 
1030 	/* No more bytes should be there */
1031 	to_read = sizeof(buf) - read;
1032 	recv_buf(fd, buf + read, to_read, MSG_DONTWAIT, -EAGAIN);
1033 
1034 	if (memcmp(buf, HELLO_STR WORLD_STR, strlen(HELLO_STR WORLD_STR))) {
1035 		fprintf(stderr, "pattern mismatch\n");
1036 		exit(EXIT_FAILURE);
1037 	}
1038 
1039 	control_writeln("REPLY1");
1040 
1041 	close(fd);
1042 }
1043 
1044 static void test_seqpacket_msg_peek_client(const struct test_opts *opts)
1045 {
1046 	return test_msg_peek_client(opts, true);
1047 }
1048 
1049 static void test_seqpacket_msg_peek_server(const struct test_opts *opts)
1050 {
1051 	return test_msg_peek_server(opts, true);
1052 }
1053 
1054 static sig_atomic_t have_sigpipe;
1055 
1056 static void sigpipe(int signo)
1057 {
1058 	have_sigpipe = 1;
1059 }
1060 
1061 #define SEND_SLEEP_USEC (10 * 1000)
1062 
1063 static void test_stream_check_sigpipe(int fd)
1064 {
1065 	ssize_t res;
1066 
1067 	have_sigpipe = 0;
1068 
1069 	/* When the other peer calls shutdown(SHUT_RD), there is a chance that
1070 	 * the send() call could occur before the message carrying the close
1071 	 * information arrives over the transport. In such cases, the send()
1072 	 * might still succeed. To avoid this race, let's retry the send() call
1073 	 * a few times, ensuring the test is more reliable.
1074 	 */
1075 	timeout_begin(TIMEOUT);
1076 	while(1) {
1077 		res = send(fd, "A", 1, 0);
1078 		if (res == -1 && errno != EINTR)
1079 			break;
1080 
1081 		/* Sleep a little before trying again to avoid flooding the
1082 		 * other peer and filling its receive buffer, causing
1083 		 * false-negative.
1084 		 */
1085 		timeout_usleep(SEND_SLEEP_USEC);
1086 		timeout_check("send");
1087 	}
1088 	timeout_end();
1089 
1090 	if (errno != EPIPE) {
1091 		fprintf(stderr, "unexpected send(2) errno %d\n", errno);
1092 		exit(EXIT_FAILURE);
1093 	}
1094 	if (!have_sigpipe) {
1095 		fprintf(stderr, "SIGPIPE expected\n");
1096 		exit(EXIT_FAILURE);
1097 	}
1098 
1099 	have_sigpipe = 0;
1100 
1101 	timeout_begin(TIMEOUT);
1102 	while(1) {
1103 		res = send(fd, "A", 1, MSG_NOSIGNAL);
1104 		if (res == -1 && errno != EINTR)
1105 			break;
1106 
1107 		timeout_usleep(SEND_SLEEP_USEC);
1108 		timeout_check("send");
1109 	}
1110 	timeout_end();
1111 
1112 	if (errno != EPIPE) {
1113 		fprintf(stderr, "unexpected send(2) errno %d\n", errno);
1114 		exit(EXIT_FAILURE);
1115 	}
1116 	if (have_sigpipe) {
1117 		fprintf(stderr, "SIGPIPE not expected\n");
1118 		exit(EXIT_FAILURE);
1119 	}
1120 }
1121 
1122 static void test_stream_shutwr_client(const struct test_opts *opts)
1123 {
1124 	int fd;
1125 
1126 	struct sigaction act = {
1127 		.sa_handler = sigpipe,
1128 	};
1129 
1130 	sigaction(SIGPIPE, &act, NULL);
1131 
1132 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1133 	if (fd < 0) {
1134 		perror("connect");
1135 		exit(EXIT_FAILURE);
1136 	}
1137 
1138 	if (shutdown(fd, SHUT_WR)) {
1139 		perror("shutdown");
1140 		exit(EXIT_FAILURE);
1141 	}
1142 
1143 	test_stream_check_sigpipe(fd);
1144 
1145 	control_writeln("CLIENTDONE");
1146 
1147 	close(fd);
1148 }
1149 
1150 static void test_stream_shutwr_server(const struct test_opts *opts)
1151 {
1152 	int fd;
1153 
1154 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1155 	if (fd < 0) {
1156 		perror("accept");
1157 		exit(EXIT_FAILURE);
1158 	}
1159 
1160 	control_expectln("CLIENTDONE");
1161 
1162 	close(fd);
1163 }
1164 
1165 static void test_stream_shutrd_client(const struct test_opts *opts)
1166 {
1167 	int fd;
1168 
1169 	struct sigaction act = {
1170 		.sa_handler = sigpipe,
1171 	};
1172 
1173 	sigaction(SIGPIPE, &act, NULL);
1174 
1175 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1176 	if (fd < 0) {
1177 		perror("connect");
1178 		exit(EXIT_FAILURE);
1179 	}
1180 
1181 	control_expectln("SHUTRDDONE");
1182 
1183 	test_stream_check_sigpipe(fd);
1184 
1185 	control_writeln("CLIENTDONE");
1186 
1187 	close(fd);
1188 }
1189 
1190 static void test_stream_shutrd_server(const struct test_opts *opts)
1191 {
1192 	int fd;
1193 
1194 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1195 	if (fd < 0) {
1196 		perror("accept");
1197 		exit(EXIT_FAILURE);
1198 	}
1199 
1200 	if (shutdown(fd, SHUT_RD)) {
1201 		perror("shutdown");
1202 		exit(EXIT_FAILURE);
1203 	}
1204 
1205 	control_writeln("SHUTRDDONE");
1206 	control_expectln("CLIENTDONE");
1207 
1208 	close(fd);
1209 }
1210 
1211 static void test_double_bind_connect_server(const struct test_opts *opts)
1212 {
1213 	int listen_fd, client_fd, i;
1214 	struct sockaddr_vm sa_client;
1215 	socklen_t socklen_client = sizeof(sa_client);
1216 
1217 	listen_fd = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port);
1218 
1219 	for (i = 0; i < 2; i++) {
1220 		control_writeln("LISTENING");
1221 
1222 		timeout_begin(TIMEOUT);
1223 		do {
1224 			client_fd = accept(listen_fd, (struct sockaddr *)&sa_client,
1225 					   &socklen_client);
1226 			timeout_check("accept");
1227 		} while (client_fd < 0 && errno == EINTR);
1228 		timeout_end();
1229 
1230 		if (client_fd < 0) {
1231 			perror("accept");
1232 			exit(EXIT_FAILURE);
1233 		}
1234 
1235 		/* Waiting for remote peer to close connection */
1236 		vsock_wait_remote_close(client_fd);
1237 	}
1238 
1239 	close(listen_fd);
1240 }
1241 
1242 static void test_double_bind_connect_client(const struct test_opts *opts)
1243 {
1244 	int i, client_fd;
1245 
1246 	for (i = 0; i < 2; i++) {
1247 		/* Wait until server is ready to accept a new connection */
1248 		control_expectln("LISTENING");
1249 
1250 		/* We use 'peer_port + 1' as "some" port for the 'bind()'
1251 		 * call. It is safe for overflow, but must be considered,
1252 		 * when running multiple test applications simultaneously
1253 		 * where 'peer-port' argument differs by 1.
1254 		 */
1255 		client_fd = vsock_bind_connect(opts->peer_cid, opts->peer_port,
1256 					       opts->peer_port + 1, SOCK_STREAM);
1257 
1258 		close(client_fd);
1259 	}
1260 }
1261 
1262 #define MSG_BUF_IOCTL_LEN 64
1263 static void test_unsent_bytes_server(const struct test_opts *opts, int type)
1264 {
1265 	unsigned char buf[MSG_BUF_IOCTL_LEN];
1266 	int client_fd;
1267 
1268 	client_fd = vsock_accept(VMADDR_CID_ANY, opts->peer_port, NULL, type);
1269 	if (client_fd < 0) {
1270 		perror("accept");
1271 		exit(EXIT_FAILURE);
1272 	}
1273 
1274 	recv_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf));
1275 	control_writeln("RECEIVED");
1276 
1277 	close(client_fd);
1278 }
1279 
1280 static void test_unsent_bytes_client(const struct test_opts *opts, int type)
1281 {
1282 	unsigned char buf[MSG_BUF_IOCTL_LEN];
1283 	int ret, fd, sock_bytes_unsent;
1284 
1285 	fd = vsock_connect(opts->peer_cid, opts->peer_port, type);
1286 	if (fd < 0) {
1287 		perror("connect");
1288 		exit(EXIT_FAILURE);
1289 	}
1290 
1291 	for (int i = 0; i < sizeof(buf); i++)
1292 		buf[i] = rand() & 0xFF;
1293 
1294 	send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
1295 	control_expectln("RECEIVED");
1296 
1297 	/* SIOCOUTQ isn't guaranteed to instantly track sent data. Even though
1298 	 * the "RECEIVED" message means that the other side has received the
1299 	 * data, there can be a delay in our kernel before updating the "unsent
1300 	 * bytes" counter. Repeat SIOCOUTQ until it returns 0.
1301 	 */
1302 	timeout_begin(TIMEOUT);
1303 	do {
1304 		ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
1305 		if (ret < 0) {
1306 			if (errno == EOPNOTSUPP) {
1307 				fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
1308 				break;
1309 			}
1310 			perror("ioctl");
1311 			exit(EXIT_FAILURE);
1312 		}
1313 		timeout_check("SIOCOUTQ");
1314 	} while (sock_bytes_unsent != 0);
1315 	timeout_end();
1316 	close(fd);
1317 }
1318 
1319 static void test_stream_unsent_bytes_client(const struct test_opts *opts)
1320 {
1321 	test_unsent_bytes_client(opts, SOCK_STREAM);
1322 }
1323 
1324 static void test_stream_unsent_bytes_server(const struct test_opts *opts)
1325 {
1326 	test_unsent_bytes_server(opts, SOCK_STREAM);
1327 }
1328 
1329 static void test_seqpacket_unsent_bytes_client(const struct test_opts *opts)
1330 {
1331 	test_unsent_bytes_client(opts, SOCK_SEQPACKET);
1332 }
1333 
1334 static void test_seqpacket_unsent_bytes_server(const struct test_opts *opts)
1335 {
1336 	test_unsent_bytes_server(opts, SOCK_SEQPACKET);
1337 }
1338 
1339 #define RCVLOWAT_CREDIT_UPD_BUF_SIZE	(1024 * 128)
1340 /* This define is the same as in 'include/linux/virtio_vsock.h':
1341  * it is used to decide when to send credit update message during
1342  * reading from rx queue of a socket. Value and its usage in
1343  * kernel is important for this test.
1344  */
1345 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE	(1024 * 64)
1346 
1347 static void test_stream_rcvlowat_def_cred_upd_client(const struct test_opts *opts)
1348 {
1349 	size_t buf_size;
1350 	void *buf;
1351 	int fd;
1352 
1353 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1354 	if (fd < 0) {
1355 		perror("connect");
1356 		exit(EXIT_FAILURE);
1357 	}
1358 
1359 	/* Send 1 byte more than peer's buffer size. */
1360 	buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE + 1;
1361 
1362 	buf = malloc(buf_size);
1363 	if (!buf) {
1364 		perror("malloc");
1365 		exit(EXIT_FAILURE);
1366 	}
1367 
1368 	/* Wait until peer sets needed buffer size. */
1369 	recv_byte(fd, 1, 0);
1370 
1371 	if (send(fd, buf, buf_size, 0) != buf_size) {
1372 		perror("send failed");
1373 		exit(EXIT_FAILURE);
1374 	}
1375 
1376 	free(buf);
1377 	close(fd);
1378 }
1379 
1380 static void test_stream_credit_update_test(const struct test_opts *opts,
1381 					   bool low_rx_bytes_test)
1382 {
1383 	int recv_buf_size;
1384 	struct pollfd fds;
1385 	size_t buf_size;
1386 	unsigned long long sock_buf_size;
1387 	void *buf;
1388 	int fd;
1389 
1390 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1391 	if (fd < 0) {
1392 		perror("accept");
1393 		exit(EXIT_FAILURE);
1394 	}
1395 
1396 	buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE;
1397 
1398 	/* size_t can be < unsigned long long */
1399 	sock_buf_size = buf_size;
1400 
1401 	setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
1402 			     sock_buf_size,
1403 			     "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
1404 
1405 	if (low_rx_bytes_test) {
1406 		/* Set new SO_RCVLOWAT here. This enables sending credit
1407 		 * update when number of bytes if our rx queue become <
1408 		 * SO_RCVLOWAT value.
1409 		 */
1410 		recv_buf_size = 1 + VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
1411 
1412 		setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
1413 				     recv_buf_size, "setsockopt(SO_RCVLOWAT)");
1414 	}
1415 
1416 	/* Send one dummy byte here, because 'setsockopt()' above also
1417 	 * sends special packet which tells sender to update our buffer
1418 	 * size. This 'send_byte()' will serialize such packet with data
1419 	 * reads in a loop below. Sender starts transmission only when
1420 	 * it receives this single byte.
1421 	 */
1422 	send_byte(fd, 1, 0);
1423 
1424 	buf = malloc(buf_size);
1425 	if (!buf) {
1426 		perror("malloc");
1427 		exit(EXIT_FAILURE);
1428 	}
1429 
1430 	/* Wait until there will be 128KB of data in rx queue. */
1431 	while (1) {
1432 		ssize_t res;
1433 
1434 		res = recv(fd, buf, buf_size, MSG_PEEK);
1435 		if (res == buf_size)
1436 			break;
1437 
1438 		if (res <= 0) {
1439 			fprintf(stderr, "unexpected 'recv()' return: %zi\n", res);
1440 			exit(EXIT_FAILURE);
1441 		}
1442 	}
1443 
1444 	/* There is 128KB of data in the socket's rx queue, dequeue first
1445 	 * 64KB, credit update is sent if 'low_rx_bytes_test' == true.
1446 	 * Otherwise, credit update is sent in 'if (!low_rx_bytes_test)'.
1447 	 */
1448 	recv_buf_size = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
1449 	recv_buf(fd, buf, recv_buf_size, 0, recv_buf_size);
1450 
1451 	if (!low_rx_bytes_test) {
1452 		recv_buf_size++;
1453 
1454 		/* Updating SO_RCVLOWAT will send credit update. */
1455 		setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
1456 				     recv_buf_size, "setsockopt(SO_RCVLOWAT)");
1457 	}
1458 
1459 	fds.fd = fd;
1460 	fds.events = POLLIN | POLLRDNORM | POLLERR |
1461 		     POLLRDHUP | POLLHUP;
1462 
1463 	/* This 'poll()' will return once we receive last byte
1464 	 * sent by client.
1465 	 */
1466 	if (poll(&fds, 1, -1) < 0) {
1467 		perror("poll");
1468 		exit(EXIT_FAILURE);
1469 	}
1470 
1471 	if (fds.revents & POLLERR) {
1472 		fprintf(stderr, "'poll()' error\n");
1473 		exit(EXIT_FAILURE);
1474 	}
1475 
1476 	if (fds.revents & (POLLIN | POLLRDNORM)) {
1477 		recv_buf(fd, buf, recv_buf_size, MSG_DONTWAIT, recv_buf_size);
1478 	} else {
1479 		/* These flags must be set, as there is at
1480 		 * least 64KB of data ready to read.
1481 		 */
1482 		fprintf(stderr, "POLLIN | POLLRDNORM expected\n");
1483 		exit(EXIT_FAILURE);
1484 	}
1485 
1486 	free(buf);
1487 	close(fd);
1488 }
1489 
1490 static void test_stream_cred_upd_on_low_rx_bytes(const struct test_opts *opts)
1491 {
1492 	test_stream_credit_update_test(opts, true);
1493 }
1494 
1495 static void test_stream_cred_upd_on_set_rcvlowat(const struct test_opts *opts)
1496 {
1497 	test_stream_credit_update_test(opts, false);
1498 }
1499 
1500 /* The goal of test leak_acceptq is to stress the race between connect() and
1501  * close(listener). Implementation of client/server loops boils down to:
1502  *
1503  * client                server
1504  * ------                ------
1505  * write(CONTINUE)
1506  *                       expect(CONTINUE)
1507  *                       listen()
1508  *                       write(LISTENING)
1509  * expect(LISTENING)
1510  * connect()             close()
1511  */
1512 #define ACCEPTQ_LEAK_RACE_TIMEOUT 2 /* seconds */
1513 
1514 static void test_stream_leak_acceptq_client(const struct test_opts *opts)
1515 {
1516 	time_t tout;
1517 	int fd;
1518 
1519 	tout = current_nsec() + ACCEPTQ_LEAK_RACE_TIMEOUT * NSEC_PER_SEC;
1520 	do {
1521 		control_writeulong(CONTROL_CONTINUE);
1522 
1523 		fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1524 		if (fd >= 0)
1525 			close(fd);
1526 	} while (current_nsec() < tout);
1527 
1528 	control_writeulong(CONTROL_DONE);
1529 }
1530 
1531 /* Test for a memory leak. User is expected to run kmemleak scan, see README. */
1532 static void test_stream_leak_acceptq_server(const struct test_opts *opts)
1533 {
1534 	int fd;
1535 
1536 	while (control_readulong() == CONTROL_CONTINUE) {
1537 		fd = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port);
1538 		control_writeln("LISTENING");
1539 		close(fd);
1540 	}
1541 }
1542 
1543 /* Test for a memory leak. User is expected to run kmemleak scan, see README. */
1544 static void test_stream_msgzcopy_leak_errq_client(const struct test_opts *opts)
1545 {
1546 	struct pollfd fds = { 0 };
1547 	int fd;
1548 
1549 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1550 	if (fd < 0) {
1551 		perror("connect");
1552 		exit(EXIT_FAILURE);
1553 	}
1554 
1555 	enable_so_zerocopy_check(fd);
1556 	send_byte(fd, 1, MSG_ZEROCOPY);
1557 
1558 	fds.fd = fd;
1559 	fds.events = 0;
1560 	if (poll(&fds, 1, -1) < 0) {
1561 		perror("poll");
1562 		exit(EXIT_FAILURE);
1563 	}
1564 
1565 	close(fd);
1566 }
1567 
1568 static void test_stream_msgzcopy_leak_errq_server(const struct test_opts *opts)
1569 {
1570 	int fd;
1571 
1572 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1573 	if (fd < 0) {
1574 		perror("accept");
1575 		exit(EXIT_FAILURE);
1576 	}
1577 
1578 	recv_byte(fd, 1, 0);
1579 	vsock_wait_remote_close(fd);
1580 	close(fd);
1581 }
1582 
1583 /* Test msgzcopy_leak_zcskb is meant to exercise sendmsg() error handling path,
1584  * that might leak an skb. The idea is to fail virtio_transport_init_zcopy_skb()
1585  * by hitting net.core.optmem_max limit in sock_omalloc(), specifically
1586  *
1587  *   vsock_connectible_sendmsg
1588  *     virtio_transport_stream_enqueue
1589  *       virtio_transport_send_pkt_info
1590  *         virtio_transport_init_zcopy_skb
1591  *         . msg_zerocopy_realloc
1592  *         .   msg_zerocopy_alloc
1593  *         .     sock_omalloc
1594  *         .       sk_omem_alloc + size > sysctl_optmem_max
1595  *         return -ENOMEM
1596  *
1597  * We abuse the implementation detail of net/socket.c:____sys_sendmsg().
1598  * sk_omem_alloc can be precisely bumped by sock_kmalloc(), as it is used to
1599  * fetch user-provided control data.
1600  *
1601  * While this approach works for now, it relies on assumptions regarding the
1602  * implementation and configuration (for example, order of net.core.optmem_max
1603  * can not exceed MAX_PAGE_ORDER), which may not hold in the future. A more
1604  * resilient testing could be implemented by leveraging the Fault injection
1605  * framework (CONFIG_FAULT_INJECTION), e.g.
1606  *
1607  *   client# echo N > /sys/kernel/debug/failslab/ignore-gfp-wait
1608  *   client# echo 0 > /sys/kernel/debug/failslab/verbose
1609  *
1610  *   void client(const struct test_opts *opts)
1611  *   {
1612  *       char buf[16];
1613  *       int f, s, i;
1614  *
1615  *       f = open("/proc/self/fail-nth", O_WRONLY);
1616  *
1617  *       for (i = 1; i < 32; i++) {
1618  *           control_writeulong(CONTROL_CONTINUE);
1619  *
1620  *           s = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1621  *           enable_so_zerocopy_check(s);
1622  *
1623  *           sprintf(buf, "%d", i);
1624  *           write(f, buf, strlen(buf));
1625  *
1626  *           send(s, &(char){ 0 }, 1, MSG_ZEROCOPY);
1627  *
1628  *           write(f, "0", 1);
1629  *           close(s);
1630  *       }
1631  *
1632  *       control_writeulong(CONTROL_DONE);
1633  *       close(f);
1634  *   }
1635  *
1636  *   void server(const struct test_opts *opts)
1637  *   {
1638  *       int fd;
1639  *
1640  *       while (control_readulong() == CONTROL_CONTINUE) {
1641  *           fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1642  *           vsock_wait_remote_close(fd);
1643  *           close(fd);
1644  *       }
1645  *   }
1646  *
1647  * Refer to Documentation/fault-injection/fault-injection.rst.
1648  */
1649 #define MAX_PAGE_ORDER	10	/* usually */
1650 #define PAGE_SIZE	4096
1651 
1652 /* Test for a memory leak. User is expected to run kmemleak scan, see README. */
1653 static void test_stream_msgzcopy_leak_zcskb_client(const struct test_opts *opts)
1654 {
1655 	size_t optmem_max, ctl_len, chunk_size;
1656 	struct msghdr msg = { 0 };
1657 	struct iovec iov;
1658 	char *chunk;
1659 	int fd, res;
1660 	FILE *f;
1661 
1662 	f = fopen("/proc/sys/net/core/optmem_max", "r");
1663 	if (!f) {
1664 		perror("fopen(optmem_max)");
1665 		exit(EXIT_FAILURE);
1666 	}
1667 
1668 	if (fscanf(f, "%zu", &optmem_max) != 1) {
1669 		fprintf(stderr, "fscanf(optmem_max) failed\n");
1670 		exit(EXIT_FAILURE);
1671 	}
1672 
1673 	fclose(f);
1674 
1675 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1676 	if (fd < 0) {
1677 		perror("connect");
1678 		exit(EXIT_FAILURE);
1679 	}
1680 
1681 	enable_so_zerocopy_check(fd);
1682 
1683 	ctl_len = optmem_max - 1;
1684 	if (ctl_len > PAGE_SIZE << MAX_PAGE_ORDER) {
1685 		fprintf(stderr, "Try with net.core.optmem_max = 100000\n");
1686 		exit(EXIT_FAILURE);
1687 	}
1688 
1689 	chunk_size = CMSG_SPACE(ctl_len);
1690 	chunk = malloc(chunk_size);
1691 	if (!chunk) {
1692 		perror("malloc");
1693 		exit(EXIT_FAILURE);
1694 	}
1695 	memset(chunk, 0, chunk_size);
1696 
1697 	iov.iov_base = &(char){ 0 };
1698 	iov.iov_len = 1;
1699 
1700 	msg.msg_iov = &iov;
1701 	msg.msg_iovlen = 1;
1702 	msg.msg_control = chunk;
1703 	msg.msg_controllen = ctl_len;
1704 
1705 	errno = 0;
1706 	res = sendmsg(fd, &msg, MSG_ZEROCOPY);
1707 	if (res >= 0 || errno != ENOMEM) {
1708 		fprintf(stderr, "Expected ENOMEM, got errno=%d res=%d\n",
1709 			errno, res);
1710 		exit(EXIT_FAILURE);
1711 	}
1712 
1713 	close(fd);
1714 }
1715 
1716 static void test_stream_msgzcopy_leak_zcskb_server(const struct test_opts *opts)
1717 {
1718 	int fd;
1719 
1720 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1721 	if (fd < 0) {
1722 		perror("accept");
1723 		exit(EXIT_FAILURE);
1724 	}
1725 
1726 	vsock_wait_remote_close(fd);
1727 	close(fd);
1728 }
1729 
1730 #define MAX_PORT_RETRIES	24	/* net/vmw_vsock/af_vsock.c */
1731 
1732 /* Test attempts to trigger a transport release for an unbound socket. This can
1733  * lead to a reference count mishandling.
1734  */
1735 static void test_stream_transport_uaf_client(const struct test_opts *opts)
1736 {
1737 	int sockets[MAX_PORT_RETRIES];
1738 	struct sockaddr_vm addr;
1739 	int fd, i, alen;
1740 
1741 	fd = vsock_bind(VMADDR_CID_ANY, VMADDR_PORT_ANY, SOCK_STREAM);
1742 
1743 	alen = sizeof(addr);
1744 	if (getsockname(fd, (struct sockaddr *)&addr, &alen)) {
1745 		perror("getsockname");
1746 		exit(EXIT_FAILURE);
1747 	}
1748 
1749 	for (i = 0; i < MAX_PORT_RETRIES; ++i)
1750 		sockets[i] = vsock_bind(VMADDR_CID_ANY, ++addr.svm_port,
1751 					SOCK_STREAM);
1752 
1753 	close(fd);
1754 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
1755 	if (fd < 0) {
1756 		perror("socket");
1757 		exit(EXIT_FAILURE);
1758 	}
1759 
1760 	if (!vsock_connect_fd(fd, addr.svm_cid, addr.svm_port)) {
1761 		perror("Unexpected connect() #1 success");
1762 		exit(EXIT_FAILURE);
1763 	}
1764 
1765 	/* Vulnerable system may crash now. */
1766 	if (!vsock_connect_fd(fd, VMADDR_CID_HOST, VMADDR_PORT_ANY)) {
1767 		perror("Unexpected connect() #2 success");
1768 		exit(EXIT_FAILURE);
1769 	}
1770 
1771 	close(fd);
1772 	while (i--)
1773 		close(sockets[i]);
1774 
1775 	control_writeln("DONE");
1776 }
1777 
1778 static void test_stream_transport_uaf_server(const struct test_opts *opts)
1779 {
1780 	control_expectln("DONE");
1781 }
1782 
1783 static void test_stream_connect_retry_client(const struct test_opts *opts)
1784 {
1785 	int fd;
1786 
1787 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
1788 	if (fd < 0) {
1789 		perror("socket");
1790 		exit(EXIT_FAILURE);
1791 	}
1792 
1793 	if (!vsock_connect_fd(fd, opts->peer_cid, opts->peer_port)) {
1794 		fprintf(stderr, "Unexpected connect() #1 success\n");
1795 		exit(EXIT_FAILURE);
1796 	}
1797 
1798 	control_writeln("LISTEN");
1799 	control_expectln("LISTENING");
1800 
1801 	if (vsock_connect_fd(fd, opts->peer_cid, opts->peer_port)) {
1802 		perror("connect() #2");
1803 		exit(EXIT_FAILURE);
1804 	}
1805 
1806 	close(fd);
1807 }
1808 
1809 static void test_stream_connect_retry_server(const struct test_opts *opts)
1810 {
1811 	int fd;
1812 
1813 	control_expectln("LISTEN");
1814 
1815 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1816 	if (fd < 0) {
1817 		perror("accept");
1818 		exit(EXIT_FAILURE);
1819 	}
1820 
1821 	vsock_wait_remote_close(fd);
1822 	close(fd);
1823 }
1824 
1825 static void test_stream_linger_client(const struct test_opts *opts)
1826 {
1827 	struct linger optval = {
1828 		.l_onoff = 1,
1829 		.l_linger = 1
1830 	};
1831 	int fd;
1832 
1833 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1834 	if (fd < 0) {
1835 		perror("connect");
1836 		exit(EXIT_FAILURE);
1837 	}
1838 
1839 	if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) {
1840 		perror("setsockopt(SO_LINGER)");
1841 		exit(EXIT_FAILURE);
1842 	}
1843 
1844 	close(fd);
1845 }
1846 
1847 static void test_stream_linger_server(const struct test_opts *opts)
1848 {
1849 	int fd;
1850 
1851 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1852 	if (fd < 0) {
1853 		perror("accept");
1854 		exit(EXIT_FAILURE);
1855 	}
1856 
1857 	vsock_wait_remote_close(fd);
1858 	close(fd);
1859 }
1860 
1861 static struct test_case test_cases[] = {
1862 	{
1863 		.name = "SOCK_STREAM connection reset",
1864 		.run_client = test_stream_connection_reset,
1865 	},
1866 	{
1867 		.name = "SOCK_STREAM bind only",
1868 		.run_client = test_stream_bind_only_client,
1869 		.run_server = test_stream_bind_only_server,
1870 	},
1871 	{
1872 		.name = "SOCK_STREAM client close",
1873 		.run_client = test_stream_client_close_client,
1874 		.run_server = test_stream_client_close_server,
1875 	},
1876 	{
1877 		.name = "SOCK_STREAM server close",
1878 		.run_client = test_stream_server_close_client,
1879 		.run_server = test_stream_server_close_server,
1880 	},
1881 	{
1882 		.name = "SOCK_STREAM multiple connections",
1883 		.run_client = test_stream_multiconn_client,
1884 		.run_server = test_stream_multiconn_server,
1885 	},
1886 	{
1887 		.name = "SOCK_STREAM MSG_PEEK",
1888 		.run_client = test_stream_msg_peek_client,
1889 		.run_server = test_stream_msg_peek_server,
1890 	},
1891 	{
1892 		.name = "SOCK_SEQPACKET msg bounds",
1893 		.run_client = test_seqpacket_msg_bounds_client,
1894 		.run_server = test_seqpacket_msg_bounds_server,
1895 	},
1896 	{
1897 		.name = "SOCK_SEQPACKET MSG_TRUNC flag",
1898 		.run_client = test_seqpacket_msg_trunc_client,
1899 		.run_server = test_seqpacket_msg_trunc_server,
1900 	},
1901 	{
1902 		.name = "SOCK_SEQPACKET timeout",
1903 		.run_client = test_seqpacket_timeout_client,
1904 		.run_server = test_seqpacket_timeout_server,
1905 	},
1906 	{
1907 		.name = "SOCK_SEQPACKET invalid receive buffer",
1908 		.run_client = test_seqpacket_invalid_rec_buffer_client,
1909 		.run_server = test_seqpacket_invalid_rec_buffer_server,
1910 	},
1911 	{
1912 		.name = "SOCK_STREAM poll() + SO_RCVLOWAT",
1913 		.run_client = test_stream_poll_rcvlowat_client,
1914 		.run_server = test_stream_poll_rcvlowat_server,
1915 	},
1916 	{
1917 		.name = "SOCK_SEQPACKET big message",
1918 		.run_client = test_seqpacket_bigmsg_client,
1919 		.run_server = test_seqpacket_bigmsg_server,
1920 	},
1921 	{
1922 		.name = "SOCK_STREAM test invalid buffer",
1923 		.run_client = test_stream_inv_buf_client,
1924 		.run_server = test_stream_inv_buf_server,
1925 	},
1926 	{
1927 		.name = "SOCK_SEQPACKET test invalid buffer",
1928 		.run_client = test_seqpacket_inv_buf_client,
1929 		.run_server = test_seqpacket_inv_buf_server,
1930 	},
1931 	{
1932 		.name = "SOCK_STREAM virtio skb merge",
1933 		.run_client = test_stream_virtio_skb_merge_client,
1934 		.run_server = test_stream_virtio_skb_merge_server,
1935 	},
1936 	{
1937 		.name = "SOCK_SEQPACKET MSG_PEEK",
1938 		.run_client = test_seqpacket_msg_peek_client,
1939 		.run_server = test_seqpacket_msg_peek_server,
1940 	},
1941 	{
1942 		.name = "SOCK_STREAM SHUT_WR",
1943 		.run_client = test_stream_shutwr_client,
1944 		.run_server = test_stream_shutwr_server,
1945 	},
1946 	{
1947 		.name = "SOCK_STREAM SHUT_RD",
1948 		.run_client = test_stream_shutrd_client,
1949 		.run_server = test_stream_shutrd_server,
1950 	},
1951 	{
1952 		.name = "SOCK_STREAM MSG_ZEROCOPY",
1953 		.run_client = test_stream_msgzcopy_client,
1954 		.run_server = test_stream_msgzcopy_server,
1955 	},
1956 	{
1957 		.name = "SOCK_SEQPACKET MSG_ZEROCOPY",
1958 		.run_client = test_seqpacket_msgzcopy_client,
1959 		.run_server = test_seqpacket_msgzcopy_server,
1960 	},
1961 	{
1962 		.name = "SOCK_STREAM MSG_ZEROCOPY empty MSG_ERRQUEUE",
1963 		.run_client = test_stream_msgzcopy_empty_errq_client,
1964 		.run_server = test_stream_msgzcopy_empty_errq_server,
1965 	},
1966 	{
1967 		.name = "SOCK_STREAM double bind connect",
1968 		.run_client = test_double_bind_connect_client,
1969 		.run_server = test_double_bind_connect_server,
1970 	},
1971 	{
1972 		.name = "SOCK_STREAM virtio credit update + SO_RCVLOWAT",
1973 		.run_client = test_stream_rcvlowat_def_cred_upd_client,
1974 		.run_server = test_stream_cred_upd_on_set_rcvlowat,
1975 	},
1976 	{
1977 		.name = "SOCK_STREAM virtio credit update + low rx_bytes",
1978 		.run_client = test_stream_rcvlowat_def_cred_upd_client,
1979 		.run_server = test_stream_cred_upd_on_low_rx_bytes,
1980 	},
1981 	{
1982 		.name = "SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes",
1983 		.run_client = test_stream_unsent_bytes_client,
1984 		.run_server = test_stream_unsent_bytes_server,
1985 	},
1986 	{
1987 		.name = "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes",
1988 		.run_client = test_seqpacket_unsent_bytes_client,
1989 		.run_server = test_seqpacket_unsent_bytes_server,
1990 	},
1991 	{
1992 		.name = "SOCK_STREAM leak accept queue",
1993 		.run_client = test_stream_leak_acceptq_client,
1994 		.run_server = test_stream_leak_acceptq_server,
1995 	},
1996 	{
1997 		.name = "SOCK_STREAM MSG_ZEROCOPY leak MSG_ERRQUEUE",
1998 		.run_client = test_stream_msgzcopy_leak_errq_client,
1999 		.run_server = test_stream_msgzcopy_leak_errq_server,
2000 	},
2001 	{
2002 		.name = "SOCK_STREAM MSG_ZEROCOPY leak completion skb",
2003 		.run_client = test_stream_msgzcopy_leak_zcskb_client,
2004 		.run_server = test_stream_msgzcopy_leak_zcskb_server,
2005 	},
2006 	{
2007 		.name = "SOCK_STREAM transport release use-after-free",
2008 		.run_client = test_stream_transport_uaf_client,
2009 		.run_server = test_stream_transport_uaf_server,
2010 	},
2011 	{
2012 		.name = "SOCK_STREAM retry failed connect()",
2013 		.run_client = test_stream_connect_retry_client,
2014 		.run_server = test_stream_connect_retry_server,
2015 	},
2016 	{
2017 		.name = "SOCK_STREAM SO_LINGER null-ptr-deref",
2018 		.run_client = test_stream_linger_client,
2019 		.run_server = test_stream_linger_server,
2020 	},
2021 	{},
2022 };
2023 
2024 static const char optstring[] = "";
2025 static const struct option longopts[] = {
2026 	{
2027 		.name = "control-host",
2028 		.has_arg = required_argument,
2029 		.val = 'H',
2030 	},
2031 	{
2032 		.name = "control-port",
2033 		.has_arg = required_argument,
2034 		.val = 'P',
2035 	},
2036 	{
2037 		.name = "mode",
2038 		.has_arg = required_argument,
2039 		.val = 'm',
2040 	},
2041 	{
2042 		.name = "peer-cid",
2043 		.has_arg = required_argument,
2044 		.val = 'p',
2045 	},
2046 	{
2047 		.name = "peer-port",
2048 		.has_arg = required_argument,
2049 		.val = 'q',
2050 	},
2051 	{
2052 		.name = "list",
2053 		.has_arg = no_argument,
2054 		.val = 'l',
2055 	},
2056 	{
2057 		.name = "skip",
2058 		.has_arg = required_argument,
2059 		.val = 's',
2060 	},
2061 	{
2062 		.name = "pick",
2063 		.has_arg = required_argument,
2064 		.val = 't',
2065 	},
2066 	{
2067 		.name = "help",
2068 		.has_arg = no_argument,
2069 		.val = '?',
2070 	},
2071 	{},
2072 };
2073 
2074 static void usage(void)
2075 {
2076 	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"
2077 		"\n"
2078 		"  Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n"
2079 		"  Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
2080 		"\n"
2081 		"Run vsock.ko tests.  Must be launched in both guest\n"
2082 		"and host.  One side must use --mode=client and\n"
2083 		"the other side must use --mode=server.\n"
2084 		"\n"
2085 		"A TCP control socket connection is used to coordinate tests\n"
2086 		"between the client and the server.  The server requires a\n"
2087 		"listen address and the client requires an address to\n"
2088 		"connect to.\n"
2089 		"\n"
2090 		"The CID of the other side must be given with --peer-cid=<cid>.\n"
2091 		"During the test, two AF_VSOCK ports will be used: the port\n"
2092 		"specified with --peer-port=<port> (or the default port)\n"
2093 		"and the next one.\n"
2094 		"\n"
2095 		"Options:\n"
2096 		"  --help                 This help message\n"
2097 		"  --control-host <host>  Server IP address to connect to\n"
2098 		"  --control-port <port>  Server port to listen on/connect to\n"
2099 		"  --mode client|server   Server or client mode\n"
2100 		"  --peer-cid <cid>       CID of the other side\n"
2101 		"  --peer-port <port>     AF_VSOCK port used for the test [default: %d]\n"
2102 		"  --list                 List of tests that will be executed\n"
2103 		"  --pick <test_id>       Test ID to execute selectively;\n"
2104 		"                         use multiple --pick options to select more tests\n"
2105 		"  --skip <test_id>       Test ID to skip;\n"
2106 		"                         use multiple --skip options to skip more tests\n",
2107 		DEFAULT_PEER_PORT
2108 		);
2109 	exit(EXIT_FAILURE);
2110 }
2111 
2112 int main(int argc, char **argv)
2113 {
2114 	const char *control_host = NULL;
2115 	const char *control_port = NULL;
2116 	struct test_opts opts = {
2117 		.mode = TEST_MODE_UNSET,
2118 		.peer_cid = VMADDR_CID_ANY,
2119 		.peer_port = DEFAULT_PEER_PORT,
2120 	};
2121 
2122 	srand(time(NULL));
2123 	init_signals();
2124 
2125 	for (;;) {
2126 		int opt = getopt_long(argc, argv, optstring, longopts, NULL);
2127 
2128 		if (opt == -1)
2129 			break;
2130 
2131 		switch (opt) {
2132 		case 'H':
2133 			control_host = optarg;
2134 			break;
2135 		case 'm':
2136 			if (strcmp(optarg, "client") == 0)
2137 				opts.mode = TEST_MODE_CLIENT;
2138 			else if (strcmp(optarg, "server") == 0)
2139 				opts.mode = TEST_MODE_SERVER;
2140 			else {
2141 				fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
2142 				return EXIT_FAILURE;
2143 			}
2144 			break;
2145 		case 'p':
2146 			opts.peer_cid = parse_cid(optarg);
2147 			break;
2148 		case 'q':
2149 			opts.peer_port = parse_port(optarg);
2150 			break;
2151 		case 'P':
2152 			control_port = optarg;
2153 			break;
2154 		case 'l':
2155 			list_tests(test_cases);
2156 			break;
2157 		case 's':
2158 			skip_test(test_cases, ARRAY_SIZE(test_cases) - 1,
2159 				  optarg);
2160 			break;
2161 		case 't':
2162 			pick_test(test_cases, ARRAY_SIZE(test_cases) - 1,
2163 				  optarg);
2164 			break;
2165 		case '?':
2166 		default:
2167 			usage();
2168 		}
2169 	}
2170 
2171 	if (!control_port)
2172 		usage();
2173 	if (opts.mode == TEST_MODE_UNSET)
2174 		usage();
2175 	if (opts.peer_cid == VMADDR_CID_ANY)
2176 		usage();
2177 
2178 	if (!control_host) {
2179 		if (opts.mode != TEST_MODE_SERVER)
2180 			usage();
2181 		control_host = "0.0.0.0";
2182 	}
2183 
2184 	control_init(control_host, control_port,
2185 		     opts.mode == TEST_MODE_SERVER);
2186 
2187 	run_tests(test_cases, &opts);
2188 
2189 	control_cleanup();
2190 	return EXIT_SUCCESS;
2191 }
2192