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