xref: /linux/tools/testing/vsock/vsock_test.c (revision 135a8a4d25a2937b2727e3857471f305d78496da)
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)
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 (!have_sigpipe) {
1091 		fprintf(stderr, "SIGPIPE expected\n");
1092 		exit(EXIT_FAILURE);
1093 	}
1094 
1095 	have_sigpipe = 0;
1096 
1097 	timeout_begin(TIMEOUT);
1098 	while(1) {
1099 		res = send(fd, "A", 1, MSG_NOSIGNAL);
1100 		if (res == -1)
1101 			break;
1102 
1103 		timeout_usleep(SEND_SLEEP_USEC);
1104 		timeout_check("send");
1105 	}
1106 	timeout_end();
1107 
1108 	if (have_sigpipe) {
1109 		fprintf(stderr, "SIGPIPE not expected\n");
1110 		exit(EXIT_FAILURE);
1111 	}
1112 }
1113 
1114 static void test_stream_shutwr_client(const struct test_opts *opts)
1115 {
1116 	int fd;
1117 
1118 	struct sigaction act = {
1119 		.sa_handler = sigpipe,
1120 	};
1121 
1122 	sigaction(SIGPIPE, &act, NULL);
1123 
1124 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1125 	if (fd < 0) {
1126 		perror("connect");
1127 		exit(EXIT_FAILURE);
1128 	}
1129 
1130 	if (shutdown(fd, SHUT_WR)) {
1131 		perror("shutdown");
1132 		exit(EXIT_FAILURE);
1133 	}
1134 
1135 	test_stream_check_sigpipe(fd);
1136 
1137 	control_writeln("CLIENTDONE");
1138 
1139 	close(fd);
1140 }
1141 
1142 static void test_stream_shutwr_server(const struct test_opts *opts)
1143 {
1144 	int fd;
1145 
1146 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1147 	if (fd < 0) {
1148 		perror("accept");
1149 		exit(EXIT_FAILURE);
1150 	}
1151 
1152 	control_expectln("CLIENTDONE");
1153 
1154 	close(fd);
1155 }
1156 
1157 static void test_stream_shutrd_client(const struct test_opts *opts)
1158 {
1159 	int fd;
1160 
1161 	struct sigaction act = {
1162 		.sa_handler = sigpipe,
1163 	};
1164 
1165 	sigaction(SIGPIPE, &act, NULL);
1166 
1167 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1168 	if (fd < 0) {
1169 		perror("connect");
1170 		exit(EXIT_FAILURE);
1171 	}
1172 
1173 	control_expectln("SHUTRDDONE");
1174 
1175 	test_stream_check_sigpipe(fd);
1176 
1177 	control_writeln("CLIENTDONE");
1178 
1179 	close(fd);
1180 }
1181 
1182 static void test_stream_shutrd_server(const struct test_opts *opts)
1183 {
1184 	int fd;
1185 
1186 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1187 	if (fd < 0) {
1188 		perror("accept");
1189 		exit(EXIT_FAILURE);
1190 	}
1191 
1192 	if (shutdown(fd, SHUT_RD)) {
1193 		perror("shutdown");
1194 		exit(EXIT_FAILURE);
1195 	}
1196 
1197 	control_writeln("SHUTRDDONE");
1198 	control_expectln("CLIENTDONE");
1199 
1200 	close(fd);
1201 }
1202 
1203 static void test_double_bind_connect_server(const struct test_opts *opts)
1204 {
1205 	int listen_fd, client_fd, i;
1206 	struct sockaddr_vm sa_client;
1207 	socklen_t socklen_client = sizeof(sa_client);
1208 
1209 	listen_fd = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port);
1210 
1211 	for (i = 0; i < 2; i++) {
1212 		control_writeln("LISTENING");
1213 
1214 		timeout_begin(TIMEOUT);
1215 		do {
1216 			client_fd = accept(listen_fd, (struct sockaddr *)&sa_client,
1217 					   &socklen_client);
1218 			timeout_check("accept");
1219 		} while (client_fd < 0 && errno == EINTR);
1220 		timeout_end();
1221 
1222 		if (client_fd < 0) {
1223 			perror("accept");
1224 			exit(EXIT_FAILURE);
1225 		}
1226 
1227 		/* Waiting for remote peer to close connection */
1228 		vsock_wait_remote_close(client_fd);
1229 	}
1230 
1231 	close(listen_fd);
1232 }
1233 
1234 static void test_double_bind_connect_client(const struct test_opts *opts)
1235 {
1236 	int i, client_fd;
1237 
1238 	for (i = 0; i < 2; i++) {
1239 		/* Wait until server is ready to accept a new connection */
1240 		control_expectln("LISTENING");
1241 
1242 		/* We use 'peer_port + 1' as "some" port for the 'bind()'
1243 		 * call. It is safe for overflow, but must be considered,
1244 		 * when running multiple test applications simultaneously
1245 		 * where 'peer-port' argument differs by 1.
1246 		 */
1247 		client_fd = vsock_bind_connect(opts->peer_cid, opts->peer_port,
1248 					       opts->peer_port + 1, SOCK_STREAM);
1249 
1250 		close(client_fd);
1251 	}
1252 }
1253 
1254 #define MSG_BUF_IOCTL_LEN 64
1255 static void test_unsent_bytes_server(const struct test_opts *opts, int type)
1256 {
1257 	unsigned char buf[MSG_BUF_IOCTL_LEN];
1258 	int client_fd;
1259 
1260 	client_fd = vsock_accept(VMADDR_CID_ANY, opts->peer_port, NULL, type);
1261 	if (client_fd < 0) {
1262 		perror("accept");
1263 		exit(EXIT_FAILURE);
1264 	}
1265 
1266 	recv_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf));
1267 	control_writeln("RECEIVED");
1268 
1269 	close(client_fd);
1270 }
1271 
1272 static void test_unsent_bytes_client(const struct test_opts *opts, int type)
1273 {
1274 	unsigned char buf[MSG_BUF_IOCTL_LEN];
1275 	int ret, fd, sock_bytes_unsent;
1276 
1277 	fd = vsock_connect(opts->peer_cid, opts->peer_port, type);
1278 	if (fd < 0) {
1279 		perror("connect");
1280 		exit(EXIT_FAILURE);
1281 	}
1282 
1283 	for (int i = 0; i < sizeof(buf); i++)
1284 		buf[i] = rand() & 0xFF;
1285 
1286 	send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
1287 	control_expectln("RECEIVED");
1288 
1289 	/* SIOCOUTQ isn't guaranteed to instantly track sent data. Even though
1290 	 * the "RECEIVED" message means that the other side has received the
1291 	 * data, there can be a delay in our kernel before updating the "unsent
1292 	 * bytes" counter. Repeat SIOCOUTQ until it returns 0.
1293 	 */
1294 	timeout_begin(TIMEOUT);
1295 	do {
1296 		ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
1297 		if (ret < 0) {
1298 			if (errno == EOPNOTSUPP) {
1299 				fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
1300 				break;
1301 			}
1302 			perror("ioctl");
1303 			exit(EXIT_FAILURE);
1304 		}
1305 		timeout_check("SIOCOUTQ");
1306 	} while (sock_bytes_unsent != 0);
1307 	timeout_end();
1308 	close(fd);
1309 }
1310 
1311 static void test_stream_unsent_bytes_client(const struct test_opts *opts)
1312 {
1313 	test_unsent_bytes_client(opts, SOCK_STREAM);
1314 }
1315 
1316 static void test_stream_unsent_bytes_server(const struct test_opts *opts)
1317 {
1318 	test_unsent_bytes_server(opts, SOCK_STREAM);
1319 }
1320 
1321 static void test_seqpacket_unsent_bytes_client(const struct test_opts *opts)
1322 {
1323 	test_unsent_bytes_client(opts, SOCK_SEQPACKET);
1324 }
1325 
1326 static void test_seqpacket_unsent_bytes_server(const struct test_opts *opts)
1327 {
1328 	test_unsent_bytes_server(opts, SOCK_SEQPACKET);
1329 }
1330 
1331 #define RCVLOWAT_CREDIT_UPD_BUF_SIZE	(1024 * 128)
1332 /* This define is the same as in 'include/linux/virtio_vsock.h':
1333  * it is used to decide when to send credit update message during
1334  * reading from rx queue of a socket. Value and its usage in
1335  * kernel is important for this test.
1336  */
1337 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE	(1024 * 64)
1338 
1339 static void test_stream_rcvlowat_def_cred_upd_client(const struct test_opts *opts)
1340 {
1341 	size_t buf_size;
1342 	void *buf;
1343 	int fd;
1344 
1345 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1346 	if (fd < 0) {
1347 		perror("connect");
1348 		exit(EXIT_FAILURE);
1349 	}
1350 
1351 	/* Send 1 byte more than peer's buffer size. */
1352 	buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE + 1;
1353 
1354 	buf = malloc(buf_size);
1355 	if (!buf) {
1356 		perror("malloc");
1357 		exit(EXIT_FAILURE);
1358 	}
1359 
1360 	/* Wait until peer sets needed buffer size. */
1361 	recv_byte(fd, 1, 0);
1362 
1363 	if (send(fd, buf, buf_size, 0) != buf_size) {
1364 		perror("send failed");
1365 		exit(EXIT_FAILURE);
1366 	}
1367 
1368 	free(buf);
1369 	close(fd);
1370 }
1371 
1372 static void test_stream_credit_update_test(const struct test_opts *opts,
1373 					   bool low_rx_bytes_test)
1374 {
1375 	int recv_buf_size;
1376 	struct pollfd fds;
1377 	size_t buf_size;
1378 	unsigned long long sock_buf_size;
1379 	void *buf;
1380 	int fd;
1381 
1382 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1383 	if (fd < 0) {
1384 		perror("accept");
1385 		exit(EXIT_FAILURE);
1386 	}
1387 
1388 	buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE;
1389 
1390 	/* size_t can be < unsigned long long */
1391 	sock_buf_size = buf_size;
1392 
1393 	setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
1394 			     sock_buf_size,
1395 			     "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
1396 
1397 	if (low_rx_bytes_test) {
1398 		/* Set new SO_RCVLOWAT here. This enables sending credit
1399 		 * update when number of bytes if our rx queue become <
1400 		 * SO_RCVLOWAT value.
1401 		 */
1402 		recv_buf_size = 1 + VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
1403 
1404 		setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
1405 				     recv_buf_size, "setsockopt(SO_RCVLOWAT)");
1406 	}
1407 
1408 	/* Send one dummy byte here, because 'setsockopt()' above also
1409 	 * sends special packet which tells sender to update our buffer
1410 	 * size. This 'send_byte()' will serialize such packet with data
1411 	 * reads in a loop below. Sender starts transmission only when
1412 	 * it receives this single byte.
1413 	 */
1414 	send_byte(fd, 1, 0);
1415 
1416 	buf = malloc(buf_size);
1417 	if (!buf) {
1418 		perror("malloc");
1419 		exit(EXIT_FAILURE);
1420 	}
1421 
1422 	/* Wait until there will be 128KB of data in rx queue. */
1423 	while (1) {
1424 		ssize_t res;
1425 
1426 		res = recv(fd, buf, buf_size, MSG_PEEK);
1427 		if (res == buf_size)
1428 			break;
1429 
1430 		if (res <= 0) {
1431 			fprintf(stderr, "unexpected 'recv()' return: %zi\n", res);
1432 			exit(EXIT_FAILURE);
1433 		}
1434 	}
1435 
1436 	/* There is 128KB of data in the socket's rx queue, dequeue first
1437 	 * 64KB, credit update is sent if 'low_rx_bytes_test' == true.
1438 	 * Otherwise, credit update is sent in 'if (!low_rx_bytes_test)'.
1439 	 */
1440 	recv_buf_size = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
1441 	recv_buf(fd, buf, recv_buf_size, 0, recv_buf_size);
1442 
1443 	if (!low_rx_bytes_test) {
1444 		recv_buf_size++;
1445 
1446 		/* Updating SO_RCVLOWAT will send credit update. */
1447 		setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
1448 				     recv_buf_size, "setsockopt(SO_RCVLOWAT)");
1449 	}
1450 
1451 	fds.fd = fd;
1452 	fds.events = POLLIN | POLLRDNORM | POLLERR |
1453 		     POLLRDHUP | POLLHUP;
1454 
1455 	/* This 'poll()' will return once we receive last byte
1456 	 * sent by client.
1457 	 */
1458 	if (poll(&fds, 1, -1) < 0) {
1459 		perror("poll");
1460 		exit(EXIT_FAILURE);
1461 	}
1462 
1463 	if (fds.revents & POLLERR) {
1464 		fprintf(stderr, "'poll()' error\n");
1465 		exit(EXIT_FAILURE);
1466 	}
1467 
1468 	if (fds.revents & (POLLIN | POLLRDNORM)) {
1469 		recv_buf(fd, buf, recv_buf_size, MSG_DONTWAIT, recv_buf_size);
1470 	} else {
1471 		/* These flags must be set, as there is at
1472 		 * least 64KB of data ready to read.
1473 		 */
1474 		fprintf(stderr, "POLLIN | POLLRDNORM expected\n");
1475 		exit(EXIT_FAILURE);
1476 	}
1477 
1478 	free(buf);
1479 	close(fd);
1480 }
1481 
1482 static void test_stream_cred_upd_on_low_rx_bytes(const struct test_opts *opts)
1483 {
1484 	test_stream_credit_update_test(opts, true);
1485 }
1486 
1487 static void test_stream_cred_upd_on_set_rcvlowat(const struct test_opts *opts)
1488 {
1489 	test_stream_credit_update_test(opts, false);
1490 }
1491 
1492 /* The goal of test leak_acceptq is to stress the race between connect() and
1493  * close(listener). Implementation of client/server loops boils down to:
1494  *
1495  * client                server
1496  * ------                ------
1497  * write(CONTINUE)
1498  *                       expect(CONTINUE)
1499  *                       listen()
1500  *                       write(LISTENING)
1501  * expect(LISTENING)
1502  * connect()             close()
1503  */
1504 #define ACCEPTQ_LEAK_RACE_TIMEOUT 2 /* seconds */
1505 
1506 static void test_stream_leak_acceptq_client(const struct test_opts *opts)
1507 {
1508 	time_t tout;
1509 	int fd;
1510 
1511 	tout = current_nsec() + ACCEPTQ_LEAK_RACE_TIMEOUT * NSEC_PER_SEC;
1512 	do {
1513 		control_writeulong(CONTROL_CONTINUE);
1514 
1515 		fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1516 		if (fd >= 0)
1517 			close(fd);
1518 	} while (current_nsec() < tout);
1519 
1520 	control_writeulong(CONTROL_DONE);
1521 }
1522 
1523 /* Test for a memory leak. User is expected to run kmemleak scan, see README. */
1524 static void test_stream_leak_acceptq_server(const struct test_opts *opts)
1525 {
1526 	int fd;
1527 
1528 	while (control_readulong() == CONTROL_CONTINUE) {
1529 		fd = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port);
1530 		control_writeln("LISTENING");
1531 		close(fd);
1532 	}
1533 }
1534 
1535 /* Test for a memory leak. User is expected to run kmemleak scan, see README. */
1536 static void test_stream_msgzcopy_leak_errq_client(const struct test_opts *opts)
1537 {
1538 	struct pollfd fds = { 0 };
1539 	int fd;
1540 
1541 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1542 	if (fd < 0) {
1543 		perror("connect");
1544 		exit(EXIT_FAILURE);
1545 	}
1546 
1547 	enable_so_zerocopy_check(fd);
1548 	send_byte(fd, 1, MSG_ZEROCOPY);
1549 
1550 	fds.fd = fd;
1551 	fds.events = 0;
1552 	if (poll(&fds, 1, -1) < 0) {
1553 		perror("poll");
1554 		exit(EXIT_FAILURE);
1555 	}
1556 
1557 	close(fd);
1558 }
1559 
1560 static void test_stream_msgzcopy_leak_errq_server(const struct test_opts *opts)
1561 {
1562 	int fd;
1563 
1564 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1565 	if (fd < 0) {
1566 		perror("accept");
1567 		exit(EXIT_FAILURE);
1568 	}
1569 
1570 	recv_byte(fd, 1, 0);
1571 	vsock_wait_remote_close(fd);
1572 	close(fd);
1573 }
1574 
1575 /* Test msgzcopy_leak_zcskb is meant to exercise sendmsg() error handling path,
1576  * that might leak an skb. The idea is to fail virtio_transport_init_zcopy_skb()
1577  * by hitting net.core.optmem_max limit in sock_omalloc(), specifically
1578  *
1579  *   vsock_connectible_sendmsg
1580  *     virtio_transport_stream_enqueue
1581  *       virtio_transport_send_pkt_info
1582  *         virtio_transport_init_zcopy_skb
1583  *         . msg_zerocopy_realloc
1584  *         .   msg_zerocopy_alloc
1585  *         .     sock_omalloc
1586  *         .       sk_omem_alloc + size > sysctl_optmem_max
1587  *         return -ENOMEM
1588  *
1589  * We abuse the implementation detail of net/socket.c:____sys_sendmsg().
1590  * sk_omem_alloc can be precisely bumped by sock_kmalloc(), as it is used to
1591  * fetch user-provided control data.
1592  *
1593  * While this approach works for now, it relies on assumptions regarding the
1594  * implementation and configuration (for example, order of net.core.optmem_max
1595  * can not exceed MAX_PAGE_ORDER), which may not hold in the future. A more
1596  * resilient testing could be implemented by leveraging the Fault injection
1597  * framework (CONFIG_FAULT_INJECTION), e.g.
1598  *
1599  *   client# echo N > /sys/kernel/debug/failslab/ignore-gfp-wait
1600  *   client# echo 0 > /sys/kernel/debug/failslab/verbose
1601  *
1602  *   void client(const struct test_opts *opts)
1603  *   {
1604  *       char buf[16];
1605  *       int f, s, i;
1606  *
1607  *       f = open("/proc/self/fail-nth", O_WRONLY);
1608  *
1609  *       for (i = 1; i < 32; i++) {
1610  *           control_writeulong(CONTROL_CONTINUE);
1611  *
1612  *           s = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1613  *           enable_so_zerocopy_check(s);
1614  *
1615  *           sprintf(buf, "%d", i);
1616  *           write(f, buf, strlen(buf));
1617  *
1618  *           send(s, &(char){ 0 }, 1, MSG_ZEROCOPY);
1619  *
1620  *           write(f, "0", 1);
1621  *           close(s);
1622  *       }
1623  *
1624  *       control_writeulong(CONTROL_DONE);
1625  *       close(f);
1626  *   }
1627  *
1628  *   void server(const struct test_opts *opts)
1629  *   {
1630  *       int fd;
1631  *
1632  *       while (control_readulong() == CONTROL_CONTINUE) {
1633  *           fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1634  *           vsock_wait_remote_close(fd);
1635  *           close(fd);
1636  *       }
1637  *   }
1638  *
1639  * Refer to Documentation/fault-injection/fault-injection.rst.
1640  */
1641 #define MAX_PAGE_ORDER	10	/* usually */
1642 #define PAGE_SIZE	4096
1643 
1644 /* Test for a memory leak. User is expected to run kmemleak scan, see README. */
1645 static void test_stream_msgzcopy_leak_zcskb_client(const struct test_opts *opts)
1646 {
1647 	size_t optmem_max, ctl_len, chunk_size;
1648 	struct msghdr msg = { 0 };
1649 	struct iovec iov;
1650 	char *chunk;
1651 	int fd, res;
1652 	FILE *f;
1653 
1654 	f = fopen("/proc/sys/net/core/optmem_max", "r");
1655 	if (!f) {
1656 		perror("fopen(optmem_max)");
1657 		exit(EXIT_FAILURE);
1658 	}
1659 
1660 	if (fscanf(f, "%zu", &optmem_max) != 1) {
1661 		fprintf(stderr, "fscanf(optmem_max) failed\n");
1662 		exit(EXIT_FAILURE);
1663 	}
1664 
1665 	fclose(f);
1666 
1667 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1668 	if (fd < 0) {
1669 		perror("connect");
1670 		exit(EXIT_FAILURE);
1671 	}
1672 
1673 	enable_so_zerocopy_check(fd);
1674 
1675 	ctl_len = optmem_max - 1;
1676 	if (ctl_len > PAGE_SIZE << MAX_PAGE_ORDER) {
1677 		fprintf(stderr, "Try with net.core.optmem_max = 100000\n");
1678 		exit(EXIT_FAILURE);
1679 	}
1680 
1681 	chunk_size = CMSG_SPACE(ctl_len);
1682 	chunk = malloc(chunk_size);
1683 	if (!chunk) {
1684 		perror("malloc");
1685 		exit(EXIT_FAILURE);
1686 	}
1687 	memset(chunk, 0, chunk_size);
1688 
1689 	iov.iov_base = &(char){ 0 };
1690 	iov.iov_len = 1;
1691 
1692 	msg.msg_iov = &iov;
1693 	msg.msg_iovlen = 1;
1694 	msg.msg_control = chunk;
1695 	msg.msg_controllen = ctl_len;
1696 
1697 	errno = 0;
1698 	res = sendmsg(fd, &msg, MSG_ZEROCOPY);
1699 	if (res >= 0 || errno != ENOMEM) {
1700 		fprintf(stderr, "Expected ENOMEM, got errno=%d res=%d\n",
1701 			errno, res);
1702 		exit(EXIT_FAILURE);
1703 	}
1704 
1705 	close(fd);
1706 }
1707 
1708 static void test_stream_msgzcopy_leak_zcskb_server(const struct test_opts *opts)
1709 {
1710 	int fd;
1711 
1712 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1713 	if (fd < 0) {
1714 		perror("accept");
1715 		exit(EXIT_FAILURE);
1716 	}
1717 
1718 	vsock_wait_remote_close(fd);
1719 	close(fd);
1720 }
1721 
1722 #define MAX_PORT_RETRIES	24	/* net/vmw_vsock/af_vsock.c */
1723 
1724 /* Test attempts to trigger a transport release for an unbound socket. This can
1725  * lead to a reference count mishandling.
1726  */
1727 static void test_stream_transport_uaf_client(const struct test_opts *opts)
1728 {
1729 	int sockets[MAX_PORT_RETRIES];
1730 	struct sockaddr_vm addr;
1731 	int fd, i, alen;
1732 
1733 	fd = vsock_bind(VMADDR_CID_ANY, VMADDR_PORT_ANY, SOCK_STREAM);
1734 
1735 	alen = sizeof(addr);
1736 	if (getsockname(fd, (struct sockaddr *)&addr, &alen)) {
1737 		perror("getsockname");
1738 		exit(EXIT_FAILURE);
1739 	}
1740 
1741 	for (i = 0; i < MAX_PORT_RETRIES; ++i)
1742 		sockets[i] = vsock_bind(VMADDR_CID_ANY, ++addr.svm_port,
1743 					SOCK_STREAM);
1744 
1745 	close(fd);
1746 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
1747 	if (fd < 0) {
1748 		perror("socket");
1749 		exit(EXIT_FAILURE);
1750 	}
1751 
1752 	if (!vsock_connect_fd(fd, addr.svm_cid, addr.svm_port)) {
1753 		perror("Unexpected connect() #1 success");
1754 		exit(EXIT_FAILURE);
1755 	}
1756 
1757 	/* Vulnerable system may crash now. */
1758 	if (!vsock_connect_fd(fd, VMADDR_CID_HOST, VMADDR_PORT_ANY)) {
1759 		perror("Unexpected connect() #2 success");
1760 		exit(EXIT_FAILURE);
1761 	}
1762 
1763 	close(fd);
1764 	while (i--)
1765 		close(sockets[i]);
1766 
1767 	control_writeln("DONE");
1768 }
1769 
1770 static void test_stream_transport_uaf_server(const struct test_opts *opts)
1771 {
1772 	control_expectln("DONE");
1773 }
1774 
1775 static void test_stream_connect_retry_client(const struct test_opts *opts)
1776 {
1777 	int fd;
1778 
1779 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
1780 	if (fd < 0) {
1781 		perror("socket");
1782 		exit(EXIT_FAILURE);
1783 	}
1784 
1785 	if (!vsock_connect_fd(fd, opts->peer_cid, opts->peer_port)) {
1786 		fprintf(stderr, "Unexpected connect() #1 success\n");
1787 		exit(EXIT_FAILURE);
1788 	}
1789 
1790 	control_writeln("LISTEN");
1791 	control_expectln("LISTENING");
1792 
1793 	if (vsock_connect_fd(fd, opts->peer_cid, opts->peer_port)) {
1794 		perror("connect() #2");
1795 		exit(EXIT_FAILURE);
1796 	}
1797 
1798 	close(fd);
1799 }
1800 
1801 static void test_stream_connect_retry_server(const struct test_opts *opts)
1802 {
1803 	int fd;
1804 
1805 	control_expectln("LISTEN");
1806 
1807 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1808 	if (fd < 0) {
1809 		perror("accept");
1810 		exit(EXIT_FAILURE);
1811 	}
1812 
1813 	vsock_wait_remote_close(fd);
1814 	close(fd);
1815 }
1816 
1817 static void test_stream_linger_client(const struct test_opts *opts)
1818 {
1819 	struct linger optval = {
1820 		.l_onoff = 1,
1821 		.l_linger = 1
1822 	};
1823 	int fd;
1824 
1825 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
1826 	if (fd < 0) {
1827 		perror("connect");
1828 		exit(EXIT_FAILURE);
1829 	}
1830 
1831 	if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) {
1832 		perror("setsockopt(SO_LINGER)");
1833 		exit(EXIT_FAILURE);
1834 	}
1835 
1836 	close(fd);
1837 }
1838 
1839 static void test_stream_linger_server(const struct test_opts *opts)
1840 {
1841 	int fd;
1842 
1843 	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
1844 	if (fd < 0) {
1845 		perror("accept");
1846 		exit(EXIT_FAILURE);
1847 	}
1848 
1849 	vsock_wait_remote_close(fd);
1850 	close(fd);
1851 }
1852 
1853 static struct test_case test_cases[] = {
1854 	{
1855 		.name = "SOCK_STREAM connection reset",
1856 		.run_client = test_stream_connection_reset,
1857 	},
1858 	{
1859 		.name = "SOCK_STREAM bind only",
1860 		.run_client = test_stream_bind_only_client,
1861 		.run_server = test_stream_bind_only_server,
1862 	},
1863 	{
1864 		.name = "SOCK_STREAM client close",
1865 		.run_client = test_stream_client_close_client,
1866 		.run_server = test_stream_client_close_server,
1867 	},
1868 	{
1869 		.name = "SOCK_STREAM server close",
1870 		.run_client = test_stream_server_close_client,
1871 		.run_server = test_stream_server_close_server,
1872 	},
1873 	{
1874 		.name = "SOCK_STREAM multiple connections",
1875 		.run_client = test_stream_multiconn_client,
1876 		.run_server = test_stream_multiconn_server,
1877 	},
1878 	{
1879 		.name = "SOCK_STREAM MSG_PEEK",
1880 		.run_client = test_stream_msg_peek_client,
1881 		.run_server = test_stream_msg_peek_server,
1882 	},
1883 	{
1884 		.name = "SOCK_SEQPACKET msg bounds",
1885 		.run_client = test_seqpacket_msg_bounds_client,
1886 		.run_server = test_seqpacket_msg_bounds_server,
1887 	},
1888 	{
1889 		.name = "SOCK_SEQPACKET MSG_TRUNC flag",
1890 		.run_client = test_seqpacket_msg_trunc_client,
1891 		.run_server = test_seqpacket_msg_trunc_server,
1892 	},
1893 	{
1894 		.name = "SOCK_SEQPACKET timeout",
1895 		.run_client = test_seqpacket_timeout_client,
1896 		.run_server = test_seqpacket_timeout_server,
1897 	},
1898 	{
1899 		.name = "SOCK_SEQPACKET invalid receive buffer",
1900 		.run_client = test_seqpacket_invalid_rec_buffer_client,
1901 		.run_server = test_seqpacket_invalid_rec_buffer_server,
1902 	},
1903 	{
1904 		.name = "SOCK_STREAM poll() + SO_RCVLOWAT",
1905 		.run_client = test_stream_poll_rcvlowat_client,
1906 		.run_server = test_stream_poll_rcvlowat_server,
1907 	},
1908 	{
1909 		.name = "SOCK_SEQPACKET big message",
1910 		.run_client = test_seqpacket_bigmsg_client,
1911 		.run_server = test_seqpacket_bigmsg_server,
1912 	},
1913 	{
1914 		.name = "SOCK_STREAM test invalid buffer",
1915 		.run_client = test_stream_inv_buf_client,
1916 		.run_server = test_stream_inv_buf_server,
1917 	},
1918 	{
1919 		.name = "SOCK_SEQPACKET test invalid buffer",
1920 		.run_client = test_seqpacket_inv_buf_client,
1921 		.run_server = test_seqpacket_inv_buf_server,
1922 	},
1923 	{
1924 		.name = "SOCK_STREAM virtio skb merge",
1925 		.run_client = test_stream_virtio_skb_merge_client,
1926 		.run_server = test_stream_virtio_skb_merge_server,
1927 	},
1928 	{
1929 		.name = "SOCK_SEQPACKET MSG_PEEK",
1930 		.run_client = test_seqpacket_msg_peek_client,
1931 		.run_server = test_seqpacket_msg_peek_server,
1932 	},
1933 	{
1934 		.name = "SOCK_STREAM SHUT_WR",
1935 		.run_client = test_stream_shutwr_client,
1936 		.run_server = test_stream_shutwr_server,
1937 	},
1938 	{
1939 		.name = "SOCK_STREAM SHUT_RD",
1940 		.run_client = test_stream_shutrd_client,
1941 		.run_server = test_stream_shutrd_server,
1942 	},
1943 	{
1944 		.name = "SOCK_STREAM MSG_ZEROCOPY",
1945 		.run_client = test_stream_msgzcopy_client,
1946 		.run_server = test_stream_msgzcopy_server,
1947 	},
1948 	{
1949 		.name = "SOCK_SEQPACKET MSG_ZEROCOPY",
1950 		.run_client = test_seqpacket_msgzcopy_client,
1951 		.run_server = test_seqpacket_msgzcopy_server,
1952 	},
1953 	{
1954 		.name = "SOCK_STREAM MSG_ZEROCOPY empty MSG_ERRQUEUE",
1955 		.run_client = test_stream_msgzcopy_empty_errq_client,
1956 		.run_server = test_stream_msgzcopy_empty_errq_server,
1957 	},
1958 	{
1959 		.name = "SOCK_STREAM double bind connect",
1960 		.run_client = test_double_bind_connect_client,
1961 		.run_server = test_double_bind_connect_server,
1962 	},
1963 	{
1964 		.name = "SOCK_STREAM virtio credit update + SO_RCVLOWAT",
1965 		.run_client = test_stream_rcvlowat_def_cred_upd_client,
1966 		.run_server = test_stream_cred_upd_on_set_rcvlowat,
1967 	},
1968 	{
1969 		.name = "SOCK_STREAM virtio credit update + low rx_bytes",
1970 		.run_client = test_stream_rcvlowat_def_cred_upd_client,
1971 		.run_server = test_stream_cred_upd_on_low_rx_bytes,
1972 	},
1973 	{
1974 		.name = "SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes",
1975 		.run_client = test_stream_unsent_bytes_client,
1976 		.run_server = test_stream_unsent_bytes_server,
1977 	},
1978 	{
1979 		.name = "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes",
1980 		.run_client = test_seqpacket_unsent_bytes_client,
1981 		.run_server = test_seqpacket_unsent_bytes_server,
1982 	},
1983 	{
1984 		.name = "SOCK_STREAM leak accept queue",
1985 		.run_client = test_stream_leak_acceptq_client,
1986 		.run_server = test_stream_leak_acceptq_server,
1987 	},
1988 	{
1989 		.name = "SOCK_STREAM MSG_ZEROCOPY leak MSG_ERRQUEUE",
1990 		.run_client = test_stream_msgzcopy_leak_errq_client,
1991 		.run_server = test_stream_msgzcopy_leak_errq_server,
1992 	},
1993 	{
1994 		.name = "SOCK_STREAM MSG_ZEROCOPY leak completion skb",
1995 		.run_client = test_stream_msgzcopy_leak_zcskb_client,
1996 		.run_server = test_stream_msgzcopy_leak_zcskb_server,
1997 	},
1998 	{
1999 		.name = "SOCK_STREAM transport release use-after-free",
2000 		.run_client = test_stream_transport_uaf_client,
2001 		.run_server = test_stream_transport_uaf_server,
2002 	},
2003 	{
2004 		.name = "SOCK_STREAM retry failed connect()",
2005 		.run_client = test_stream_connect_retry_client,
2006 		.run_server = test_stream_connect_retry_server,
2007 	},
2008 	{
2009 		.name = "SOCK_STREAM SO_LINGER null-ptr-deref",
2010 		.run_client = test_stream_linger_client,
2011 		.run_server = test_stream_linger_server,
2012 	},
2013 	{},
2014 };
2015 
2016 static const char optstring[] = "";
2017 static const struct option longopts[] = {
2018 	{
2019 		.name = "control-host",
2020 		.has_arg = required_argument,
2021 		.val = 'H',
2022 	},
2023 	{
2024 		.name = "control-port",
2025 		.has_arg = required_argument,
2026 		.val = 'P',
2027 	},
2028 	{
2029 		.name = "mode",
2030 		.has_arg = required_argument,
2031 		.val = 'm',
2032 	},
2033 	{
2034 		.name = "peer-cid",
2035 		.has_arg = required_argument,
2036 		.val = 'p',
2037 	},
2038 	{
2039 		.name = "peer-port",
2040 		.has_arg = required_argument,
2041 		.val = 'q',
2042 	},
2043 	{
2044 		.name = "list",
2045 		.has_arg = no_argument,
2046 		.val = 'l',
2047 	},
2048 	{
2049 		.name = "skip",
2050 		.has_arg = required_argument,
2051 		.val = 's',
2052 	},
2053 	{
2054 		.name = "pick",
2055 		.has_arg = required_argument,
2056 		.val = 't',
2057 	},
2058 	{
2059 		.name = "help",
2060 		.has_arg = no_argument,
2061 		.val = '?',
2062 	},
2063 	{},
2064 };
2065 
2066 static void usage(void)
2067 {
2068 	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"
2069 		"\n"
2070 		"  Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n"
2071 		"  Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
2072 		"\n"
2073 		"Run vsock.ko tests.  Must be launched in both guest\n"
2074 		"and host.  One side must use --mode=client and\n"
2075 		"the other side must use --mode=server.\n"
2076 		"\n"
2077 		"A TCP control socket connection is used to coordinate tests\n"
2078 		"between the client and the server.  The server requires a\n"
2079 		"listen address and the client requires an address to\n"
2080 		"connect to.\n"
2081 		"\n"
2082 		"The CID of the other side must be given with --peer-cid=<cid>.\n"
2083 		"During the test, two AF_VSOCK ports will be used: the port\n"
2084 		"specified with --peer-port=<port> (or the default port)\n"
2085 		"and the next one.\n"
2086 		"\n"
2087 		"Options:\n"
2088 		"  --help                 This help message\n"
2089 		"  --control-host <host>  Server IP address to connect to\n"
2090 		"  --control-port <port>  Server port to listen on/connect to\n"
2091 		"  --mode client|server   Server or client mode\n"
2092 		"  --peer-cid <cid>       CID of the other side\n"
2093 		"  --peer-port <port>     AF_VSOCK port used for the test [default: %d]\n"
2094 		"  --list                 List of tests that will be executed\n"
2095 		"  --pick <test_id>       Test ID to execute selectively;\n"
2096 		"                         use multiple --pick options to select more tests\n"
2097 		"  --skip <test_id>       Test ID to skip;\n"
2098 		"                         use multiple --skip options to skip more tests\n",
2099 		DEFAULT_PEER_PORT
2100 		);
2101 	exit(EXIT_FAILURE);
2102 }
2103 
2104 int main(int argc, char **argv)
2105 {
2106 	const char *control_host = NULL;
2107 	const char *control_port = NULL;
2108 	struct test_opts opts = {
2109 		.mode = TEST_MODE_UNSET,
2110 		.peer_cid = VMADDR_CID_ANY,
2111 		.peer_port = DEFAULT_PEER_PORT,
2112 	};
2113 
2114 	srand(time(NULL));
2115 	init_signals();
2116 
2117 	for (;;) {
2118 		int opt = getopt_long(argc, argv, optstring, longopts, NULL);
2119 
2120 		if (opt == -1)
2121 			break;
2122 
2123 		switch (opt) {
2124 		case 'H':
2125 			control_host = optarg;
2126 			break;
2127 		case 'm':
2128 			if (strcmp(optarg, "client") == 0)
2129 				opts.mode = TEST_MODE_CLIENT;
2130 			else if (strcmp(optarg, "server") == 0)
2131 				opts.mode = TEST_MODE_SERVER;
2132 			else {
2133 				fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
2134 				return EXIT_FAILURE;
2135 			}
2136 			break;
2137 		case 'p':
2138 			opts.peer_cid = parse_cid(optarg);
2139 			break;
2140 		case 'q':
2141 			opts.peer_port = parse_port(optarg);
2142 			break;
2143 		case 'P':
2144 			control_port = optarg;
2145 			break;
2146 		case 'l':
2147 			list_tests(test_cases);
2148 			break;
2149 		case 's':
2150 			skip_test(test_cases, ARRAY_SIZE(test_cases) - 1,
2151 				  optarg);
2152 			break;
2153 		case 't':
2154 			pick_test(test_cases, ARRAY_SIZE(test_cases) - 1,
2155 				  optarg);
2156 			break;
2157 		case '?':
2158 		default:
2159 			usage();
2160 		}
2161 	}
2162 
2163 	if (!control_port)
2164 		usage();
2165 	if (opts.mode == TEST_MODE_UNSET)
2166 		usage();
2167 	if (opts.peer_cid == VMADDR_CID_ANY)
2168 		usage();
2169 
2170 	if (!control_host) {
2171 		if (opts.mode != TEST_MODE_SERVER)
2172 			usage();
2173 		control_host = "0.0.0.0";
2174 	}
2175 
2176 	control_init(control_host, control_port,
2177 		     opts.mode == TEST_MODE_SERVER);
2178 
2179 	run_tests(test_cases, &opts);
2180 
2181 	control_cleanup();
2182 	return EXIT_SUCCESS;
2183 }
2184