xref: /linux/tools/testing/vsock/vsock_test.c (revision f26b118031205135c23b43a311712fe8f34febf9)
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 
24 #include "vsock_test_zerocopy.h"
25 #include "timeout.h"
26 #include "control.h"
27 #include "util.h"
28 
29 static void test_stream_connection_reset(const struct test_opts *opts)
30 {
31 	union {
32 		struct sockaddr sa;
33 		struct sockaddr_vm svm;
34 	} addr = {
35 		.svm = {
36 			.svm_family = AF_VSOCK,
37 			.svm_port = 1234,
38 			.svm_cid = opts->peer_cid,
39 		},
40 	};
41 	int ret;
42 	int fd;
43 
44 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
45 
46 	timeout_begin(TIMEOUT);
47 	do {
48 		ret = connect(fd, &addr.sa, sizeof(addr.svm));
49 		timeout_check("connect");
50 	} while (ret < 0 && errno == EINTR);
51 	timeout_end();
52 
53 	if (ret != -1) {
54 		fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
55 		exit(EXIT_FAILURE);
56 	}
57 	if (errno != ECONNRESET) {
58 		fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
59 		exit(EXIT_FAILURE);
60 	}
61 
62 	close(fd);
63 }
64 
65 static void test_stream_bind_only_client(const struct test_opts *opts)
66 {
67 	union {
68 		struct sockaddr sa;
69 		struct sockaddr_vm svm;
70 	} addr = {
71 		.svm = {
72 			.svm_family = AF_VSOCK,
73 			.svm_port = 1234,
74 			.svm_cid = opts->peer_cid,
75 		},
76 	};
77 	int ret;
78 	int fd;
79 
80 	/* Wait for the server to be ready */
81 	control_expectln("BIND");
82 
83 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
84 
85 	timeout_begin(TIMEOUT);
86 	do {
87 		ret = connect(fd, &addr.sa, sizeof(addr.svm));
88 		timeout_check("connect");
89 	} while (ret < 0 && errno == EINTR);
90 	timeout_end();
91 
92 	if (ret != -1) {
93 		fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
94 		exit(EXIT_FAILURE);
95 	}
96 	if (errno != ECONNRESET) {
97 		fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
98 		exit(EXIT_FAILURE);
99 	}
100 
101 	/* Notify the server that the client has finished */
102 	control_writeln("DONE");
103 
104 	close(fd);
105 }
106 
107 static void test_stream_bind_only_server(const struct test_opts *opts)
108 {
109 	union {
110 		struct sockaddr sa;
111 		struct sockaddr_vm svm;
112 	} addr = {
113 		.svm = {
114 			.svm_family = AF_VSOCK,
115 			.svm_port = 1234,
116 			.svm_cid = VMADDR_CID_ANY,
117 		},
118 	};
119 	int fd;
120 
121 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
122 
123 	if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
124 		perror("bind");
125 		exit(EXIT_FAILURE);
126 	}
127 
128 	/* Notify the client that the server is ready */
129 	control_writeln("BIND");
130 
131 	/* Wait for the client to finish */
132 	control_expectln("DONE");
133 
134 	close(fd);
135 }
136 
137 static void test_stream_client_close_client(const struct test_opts *opts)
138 {
139 	int fd;
140 
141 	fd = vsock_stream_connect(opts->peer_cid, 1234);
142 	if (fd < 0) {
143 		perror("connect");
144 		exit(EXIT_FAILURE);
145 	}
146 
147 	send_byte(fd, 1, 0);
148 	close(fd);
149 }
150 
151 static void test_stream_client_close_server(const struct test_opts *opts)
152 {
153 	int fd;
154 
155 	fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
156 	if (fd < 0) {
157 		perror("accept");
158 		exit(EXIT_FAILURE);
159 	}
160 
161 	/* Wait for the remote to close the connection, before check
162 	 * -EPIPE error on send.
163 	 */
164 	vsock_wait_remote_close(fd);
165 
166 	send_byte(fd, -EPIPE, 0);
167 	recv_byte(fd, 1, 0);
168 	recv_byte(fd, 0, 0);
169 	close(fd);
170 }
171 
172 static void test_stream_server_close_client(const struct test_opts *opts)
173 {
174 	int fd;
175 
176 	fd = vsock_stream_connect(opts->peer_cid, 1234);
177 	if (fd < 0) {
178 		perror("connect");
179 		exit(EXIT_FAILURE);
180 	}
181 
182 	/* Wait for the remote to close the connection, before check
183 	 * -EPIPE error on send.
184 	 */
185 	vsock_wait_remote_close(fd);
186 
187 	send_byte(fd, -EPIPE, 0);
188 	recv_byte(fd, 1, 0);
189 	recv_byte(fd, 0, 0);
190 	close(fd);
191 }
192 
193 static void test_stream_server_close_server(const struct test_opts *opts)
194 {
195 	int fd;
196 
197 	fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
198 	if (fd < 0) {
199 		perror("accept");
200 		exit(EXIT_FAILURE);
201 	}
202 
203 	send_byte(fd, 1, 0);
204 	close(fd);
205 }
206 
207 /* With the standard socket sizes, VMCI is able to support about 100
208  * concurrent stream connections.
209  */
210 #define MULTICONN_NFDS 100
211 
212 static void test_stream_multiconn_client(const struct test_opts *opts)
213 {
214 	int fds[MULTICONN_NFDS];
215 	int i;
216 
217 	for (i = 0; i < MULTICONN_NFDS; i++) {
218 		fds[i] = vsock_stream_connect(opts->peer_cid, 1234);
219 		if (fds[i] < 0) {
220 			perror("connect");
221 			exit(EXIT_FAILURE);
222 		}
223 	}
224 
225 	for (i = 0; i < MULTICONN_NFDS; i++) {
226 		if (i % 2)
227 			recv_byte(fds[i], 1, 0);
228 		else
229 			send_byte(fds[i], 1, 0);
230 	}
231 
232 	for (i = 0; i < MULTICONN_NFDS; i++)
233 		close(fds[i]);
234 }
235 
236 static void test_stream_multiconn_server(const struct test_opts *opts)
237 {
238 	int fds[MULTICONN_NFDS];
239 	int i;
240 
241 	for (i = 0; i < MULTICONN_NFDS; i++) {
242 		fds[i] = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
243 		if (fds[i] < 0) {
244 			perror("accept");
245 			exit(EXIT_FAILURE);
246 		}
247 	}
248 
249 	for (i = 0; i < MULTICONN_NFDS; i++) {
250 		if (i % 2)
251 			send_byte(fds[i], 1, 0);
252 		else
253 			recv_byte(fds[i], 1, 0);
254 	}
255 
256 	for (i = 0; i < MULTICONN_NFDS; i++)
257 		close(fds[i]);
258 }
259 
260 #define MSG_PEEK_BUF_LEN 64
261 
262 static void test_msg_peek_client(const struct test_opts *opts,
263 				 bool seqpacket)
264 {
265 	unsigned char buf[MSG_PEEK_BUF_LEN];
266 	int fd;
267 	int i;
268 
269 	if (seqpacket)
270 		fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
271 	else
272 		fd = vsock_stream_connect(opts->peer_cid, 1234);
273 
274 	if (fd < 0) {
275 		perror("connect");
276 		exit(EXIT_FAILURE);
277 	}
278 
279 	for (i = 0; i < sizeof(buf); i++)
280 		buf[i] = rand() & 0xFF;
281 
282 	control_expectln("SRVREADY");
283 
284 	send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
285 
286 	close(fd);
287 }
288 
289 static void test_msg_peek_server(const struct test_opts *opts,
290 				 bool seqpacket)
291 {
292 	unsigned char buf_half[MSG_PEEK_BUF_LEN / 2];
293 	unsigned char buf_normal[MSG_PEEK_BUF_LEN];
294 	unsigned char buf_peek[MSG_PEEK_BUF_LEN];
295 	int fd;
296 
297 	if (seqpacket)
298 		fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
299 	else
300 		fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
301 
302 	if (fd < 0) {
303 		perror("accept");
304 		exit(EXIT_FAILURE);
305 	}
306 
307 	/* Peek from empty socket. */
308 	recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK | MSG_DONTWAIT,
309 		 -EAGAIN);
310 
311 	control_writeln("SRVREADY");
312 
313 	/* Peek part of data. */
314 	recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK, sizeof(buf_half));
315 
316 	/* Peek whole data. */
317 	recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK, sizeof(buf_peek));
318 
319 	/* Compare partial and full peek. */
320 	if (memcmp(buf_half, buf_peek, sizeof(buf_half))) {
321 		fprintf(stderr, "Partial peek data mismatch\n");
322 		exit(EXIT_FAILURE);
323 	}
324 
325 	if (seqpacket) {
326 		/* This type of socket supports MSG_TRUNC flag,
327 		 * so check it with MSG_PEEK. We must get length
328 		 * of the message.
329 		 */
330 		recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK | MSG_TRUNC,
331 			 sizeof(buf_peek));
332 	}
333 
334 	recv_buf(fd, buf_normal, sizeof(buf_normal), 0, sizeof(buf_normal));
335 
336 	/* Compare full peek and normal read. */
337 	if (memcmp(buf_peek, buf_normal, sizeof(buf_peek))) {
338 		fprintf(stderr, "Full peek data mismatch\n");
339 		exit(EXIT_FAILURE);
340 	}
341 
342 	close(fd);
343 }
344 
345 static void test_stream_msg_peek_client(const struct test_opts *opts)
346 {
347 	return test_msg_peek_client(opts, false);
348 }
349 
350 static void test_stream_msg_peek_server(const struct test_opts *opts)
351 {
352 	return test_msg_peek_server(opts, false);
353 }
354 
355 #define SOCK_BUF_SIZE (2 * 1024 * 1024)
356 #define MAX_MSG_SIZE (32 * 1024)
357 
358 static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
359 {
360 	unsigned long curr_hash;
361 	int page_size;
362 	int msg_count;
363 	int fd;
364 
365 	fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
366 	if (fd < 0) {
367 		perror("connect");
368 		exit(EXIT_FAILURE);
369 	}
370 
371 	/* Wait, until receiver sets buffer size. */
372 	control_expectln("SRVREADY");
373 
374 	curr_hash = 0;
375 	page_size = getpagesize();
376 	msg_count = SOCK_BUF_SIZE / MAX_MSG_SIZE;
377 
378 	for (int i = 0; i < msg_count; i++) {
379 		size_t buf_size;
380 		int flags;
381 		void *buf;
382 
383 		/* Use "small" buffers and "big" buffers. */
384 		if (i & 1)
385 			buf_size = page_size +
386 					(rand() % (MAX_MSG_SIZE - page_size));
387 		else
388 			buf_size = 1 + (rand() % page_size);
389 
390 		buf = malloc(buf_size);
391 
392 		if (!buf) {
393 			perror("malloc");
394 			exit(EXIT_FAILURE);
395 		}
396 
397 		memset(buf, rand() & 0xff, buf_size);
398 		/* Set at least one MSG_EOR + some random. */
399 		if (i == (msg_count / 2) || (rand() & 1)) {
400 			flags = MSG_EOR;
401 			curr_hash++;
402 		} else {
403 			flags = 0;
404 		}
405 
406 		send_buf(fd, buf, buf_size, flags, buf_size);
407 
408 		/*
409 		 * Hash sum is computed at both client and server in
410 		 * the same way:
411 		 * H += hash('message data')
412 		 * Such hash "controls" both data integrity and message
413 		 * bounds. After data exchange, both sums are compared
414 		 * using control socket, and if message bounds wasn't
415 		 * broken - two values must be equal.
416 		 */
417 		curr_hash += hash_djb2(buf, buf_size);
418 		free(buf);
419 	}
420 
421 	control_writeln("SENDDONE");
422 	control_writeulong(curr_hash);
423 	close(fd);
424 }
425 
426 static void test_seqpacket_msg_bounds_server(const struct test_opts *opts)
427 {
428 	unsigned long sock_buf_size;
429 	unsigned long remote_hash;
430 	unsigned long curr_hash;
431 	int fd;
432 	char buf[MAX_MSG_SIZE];
433 	struct msghdr msg = {0};
434 	struct iovec iov = {0};
435 
436 	fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
437 	if (fd < 0) {
438 		perror("accept");
439 		exit(EXIT_FAILURE);
440 	}
441 
442 	sock_buf_size = SOCK_BUF_SIZE;
443 
444 	if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
445 		       &sock_buf_size, sizeof(sock_buf_size))) {
446 		perror("setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
447 		exit(EXIT_FAILURE);
448 	}
449 
450 	if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
451 		       &sock_buf_size, sizeof(sock_buf_size))) {
452 		perror("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
453 		exit(EXIT_FAILURE);
454 	}
455 
456 	/* Ready to receive data. */
457 	control_writeln("SRVREADY");
458 	/* Wait, until peer sends whole data. */
459 	control_expectln("SENDDONE");
460 	iov.iov_base = buf;
461 	iov.iov_len = sizeof(buf);
462 	msg.msg_iov = &iov;
463 	msg.msg_iovlen = 1;
464 
465 	curr_hash = 0;
466 
467 	while (1) {
468 		ssize_t recv_size;
469 
470 		recv_size = recvmsg(fd, &msg, 0);
471 
472 		if (!recv_size)
473 			break;
474 
475 		if (recv_size < 0) {
476 			perror("recvmsg");
477 			exit(EXIT_FAILURE);
478 		}
479 
480 		if (msg.msg_flags & MSG_EOR)
481 			curr_hash++;
482 
483 		curr_hash += hash_djb2(msg.msg_iov[0].iov_base, recv_size);
484 	}
485 
486 	close(fd);
487 	remote_hash = control_readulong();
488 
489 	if (curr_hash != remote_hash) {
490 		fprintf(stderr, "Message bounds broken\n");
491 		exit(EXIT_FAILURE);
492 	}
493 }
494 
495 #define MESSAGE_TRUNC_SZ 32
496 static void test_seqpacket_msg_trunc_client(const struct test_opts *opts)
497 {
498 	int fd;
499 	char buf[MESSAGE_TRUNC_SZ];
500 
501 	fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
502 	if (fd < 0) {
503 		perror("connect");
504 		exit(EXIT_FAILURE);
505 	}
506 
507 	send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
508 
509 	control_writeln("SENDDONE");
510 	close(fd);
511 }
512 
513 static void test_seqpacket_msg_trunc_server(const struct test_opts *opts)
514 {
515 	int fd;
516 	char buf[MESSAGE_TRUNC_SZ / 2];
517 	struct msghdr msg = {0};
518 	struct iovec iov = {0};
519 
520 	fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
521 	if (fd < 0) {
522 		perror("accept");
523 		exit(EXIT_FAILURE);
524 	}
525 
526 	control_expectln("SENDDONE");
527 	iov.iov_base = buf;
528 	iov.iov_len = sizeof(buf);
529 	msg.msg_iov = &iov;
530 	msg.msg_iovlen = 1;
531 
532 	ssize_t ret = recvmsg(fd, &msg, MSG_TRUNC);
533 
534 	if (ret != MESSAGE_TRUNC_SZ) {
535 		printf("%zi\n", ret);
536 		perror("MSG_TRUNC doesn't work");
537 		exit(EXIT_FAILURE);
538 	}
539 
540 	if (!(msg.msg_flags & MSG_TRUNC)) {
541 		fprintf(stderr, "MSG_TRUNC expected\n");
542 		exit(EXIT_FAILURE);
543 	}
544 
545 	close(fd);
546 }
547 
548 static time_t current_nsec(void)
549 {
550 	struct timespec ts;
551 
552 	if (clock_gettime(CLOCK_REALTIME, &ts)) {
553 		perror("clock_gettime(3) failed");
554 		exit(EXIT_FAILURE);
555 	}
556 
557 	return (ts.tv_sec * 1000000000ULL) + ts.tv_nsec;
558 }
559 
560 #define RCVTIMEO_TIMEOUT_SEC 1
561 #define READ_OVERHEAD_NSEC 250000000 /* 0.25 sec */
562 
563 static void test_seqpacket_timeout_client(const struct test_opts *opts)
564 {
565 	int fd;
566 	struct timeval tv;
567 	char dummy;
568 	time_t read_enter_ns;
569 	time_t read_overhead_ns;
570 
571 	fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
572 	if (fd < 0) {
573 		perror("connect");
574 		exit(EXIT_FAILURE);
575 	}
576 
577 	tv.tv_sec = RCVTIMEO_TIMEOUT_SEC;
578 	tv.tv_usec = 0;
579 
580 	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)) == -1) {
581 		perror("setsockopt(SO_RCVTIMEO)");
582 		exit(EXIT_FAILURE);
583 	}
584 
585 	read_enter_ns = current_nsec();
586 
587 	if (read(fd, &dummy, sizeof(dummy)) != -1) {
588 		fprintf(stderr,
589 			"expected 'dummy' read(2) failure\n");
590 		exit(EXIT_FAILURE);
591 	}
592 
593 	if (errno != EAGAIN) {
594 		perror("EAGAIN expected");
595 		exit(EXIT_FAILURE);
596 	}
597 
598 	read_overhead_ns = current_nsec() - read_enter_ns -
599 			1000000000ULL * RCVTIMEO_TIMEOUT_SEC;
600 
601 	if (read_overhead_ns > READ_OVERHEAD_NSEC) {
602 		fprintf(stderr,
603 			"too much time in read(2), %lu > %i ns\n",
604 			read_overhead_ns, READ_OVERHEAD_NSEC);
605 		exit(EXIT_FAILURE);
606 	}
607 
608 	control_writeln("WAITDONE");
609 	close(fd);
610 }
611 
612 static void test_seqpacket_timeout_server(const struct test_opts *opts)
613 {
614 	int fd;
615 
616 	fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
617 	if (fd < 0) {
618 		perror("accept");
619 		exit(EXIT_FAILURE);
620 	}
621 
622 	control_expectln("WAITDONE");
623 	close(fd);
624 }
625 
626 static void test_seqpacket_bigmsg_client(const struct test_opts *opts)
627 {
628 	unsigned long sock_buf_size;
629 	socklen_t len;
630 	void *data;
631 	int fd;
632 
633 	len = sizeof(sock_buf_size);
634 
635 	fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
636 	if (fd < 0) {
637 		perror("connect");
638 		exit(EXIT_FAILURE);
639 	}
640 
641 	if (getsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
642 		       &sock_buf_size, &len)) {
643 		perror("getsockopt");
644 		exit(EXIT_FAILURE);
645 	}
646 
647 	sock_buf_size++;
648 
649 	data = malloc(sock_buf_size);
650 	if (!data) {
651 		perror("malloc");
652 		exit(EXIT_FAILURE);
653 	}
654 
655 	send_buf(fd, data, sock_buf_size, 0, -EMSGSIZE);
656 
657 	control_writeln("CLISENT");
658 
659 	free(data);
660 	close(fd);
661 }
662 
663 static void test_seqpacket_bigmsg_server(const struct test_opts *opts)
664 {
665 	int fd;
666 
667 	fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
668 	if (fd < 0) {
669 		perror("accept");
670 		exit(EXIT_FAILURE);
671 	}
672 
673 	control_expectln("CLISENT");
674 
675 	close(fd);
676 }
677 
678 #define BUF_PATTERN_1 'a'
679 #define BUF_PATTERN_2 'b'
680 
681 static void test_seqpacket_invalid_rec_buffer_client(const struct test_opts *opts)
682 {
683 	int fd;
684 	unsigned char *buf1;
685 	unsigned char *buf2;
686 	int buf_size = getpagesize() * 3;
687 
688 	fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
689 	if (fd < 0) {
690 		perror("connect");
691 		exit(EXIT_FAILURE);
692 	}
693 
694 	buf1 = malloc(buf_size);
695 	if (!buf1) {
696 		perror("'malloc()' for 'buf1'");
697 		exit(EXIT_FAILURE);
698 	}
699 
700 	buf2 = malloc(buf_size);
701 	if (!buf2) {
702 		perror("'malloc()' for 'buf2'");
703 		exit(EXIT_FAILURE);
704 	}
705 
706 	memset(buf1, BUF_PATTERN_1, buf_size);
707 	memset(buf2, BUF_PATTERN_2, buf_size);
708 
709 	send_buf(fd, buf1, buf_size, 0, buf_size);
710 
711 	send_buf(fd, buf2, buf_size, 0, buf_size);
712 
713 	close(fd);
714 }
715 
716 static void test_seqpacket_invalid_rec_buffer_server(const struct test_opts *opts)
717 {
718 	int fd;
719 	unsigned char *broken_buf;
720 	unsigned char *valid_buf;
721 	int page_size = getpagesize();
722 	int buf_size = page_size * 3;
723 	ssize_t res;
724 	int prot = PROT_READ | PROT_WRITE;
725 	int flags = MAP_PRIVATE | MAP_ANONYMOUS;
726 	int i;
727 
728 	fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
729 	if (fd < 0) {
730 		perror("accept");
731 		exit(EXIT_FAILURE);
732 	}
733 
734 	/* Setup first buffer. */
735 	broken_buf = mmap(NULL, buf_size, prot, flags, -1, 0);
736 	if (broken_buf == MAP_FAILED) {
737 		perror("mmap for 'broken_buf'");
738 		exit(EXIT_FAILURE);
739 	}
740 
741 	/* Unmap "hole" in buffer. */
742 	if (munmap(broken_buf + page_size, page_size)) {
743 		perror("'broken_buf' setup");
744 		exit(EXIT_FAILURE);
745 	}
746 
747 	valid_buf = mmap(NULL, buf_size, prot, flags, -1, 0);
748 	if (valid_buf == MAP_FAILED) {
749 		perror("mmap for 'valid_buf'");
750 		exit(EXIT_FAILURE);
751 	}
752 
753 	/* Try to fill buffer with unmapped middle. */
754 	res = read(fd, broken_buf, buf_size);
755 	if (res != -1) {
756 		fprintf(stderr,
757 			"expected 'broken_buf' read(2) failure, got %zi\n",
758 			res);
759 		exit(EXIT_FAILURE);
760 	}
761 
762 	if (errno != EFAULT) {
763 		perror("unexpected errno of 'broken_buf'");
764 		exit(EXIT_FAILURE);
765 	}
766 
767 	/* Try to fill valid buffer. */
768 	res = read(fd, valid_buf, buf_size);
769 	if (res < 0) {
770 		perror("unexpected 'valid_buf' read(2) failure");
771 		exit(EXIT_FAILURE);
772 	}
773 
774 	if (res != buf_size) {
775 		fprintf(stderr,
776 			"invalid 'valid_buf' read(2), expected %i, got %zi\n",
777 			buf_size, res);
778 		exit(EXIT_FAILURE);
779 	}
780 
781 	for (i = 0; i < buf_size; i++) {
782 		if (valid_buf[i] != BUF_PATTERN_2) {
783 			fprintf(stderr,
784 				"invalid pattern for 'valid_buf' at %i, expected %hhX, got %hhX\n",
785 				i, BUF_PATTERN_2, valid_buf[i]);
786 			exit(EXIT_FAILURE);
787 		}
788 	}
789 
790 	/* Unmap buffers. */
791 	munmap(broken_buf, page_size);
792 	munmap(broken_buf + page_size * 2, page_size);
793 	munmap(valid_buf, buf_size);
794 	close(fd);
795 }
796 
797 #define RCVLOWAT_BUF_SIZE 128
798 
799 static void test_stream_poll_rcvlowat_server(const struct test_opts *opts)
800 {
801 	int fd;
802 	int i;
803 
804 	fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
805 	if (fd < 0) {
806 		perror("accept");
807 		exit(EXIT_FAILURE);
808 	}
809 
810 	/* Send 1 byte. */
811 	send_byte(fd, 1, 0);
812 
813 	control_writeln("SRVSENT");
814 
815 	/* Wait until client is ready to receive rest of data. */
816 	control_expectln("CLNSENT");
817 
818 	for (i = 0; i < RCVLOWAT_BUF_SIZE - 1; i++)
819 		send_byte(fd, 1, 0);
820 
821 	/* Keep socket in active state. */
822 	control_expectln("POLLDONE");
823 
824 	close(fd);
825 }
826 
827 static void test_stream_poll_rcvlowat_client(const struct test_opts *opts)
828 {
829 	unsigned long lowat_val = RCVLOWAT_BUF_SIZE;
830 	char buf[RCVLOWAT_BUF_SIZE];
831 	struct pollfd fds;
832 	short poll_flags;
833 	int fd;
834 
835 	fd = vsock_stream_connect(opts->peer_cid, 1234);
836 	if (fd < 0) {
837 		perror("connect");
838 		exit(EXIT_FAILURE);
839 	}
840 
841 	if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT,
842 		       &lowat_val, sizeof(lowat_val))) {
843 		perror("setsockopt(SO_RCVLOWAT)");
844 		exit(EXIT_FAILURE);
845 	}
846 
847 	control_expectln("SRVSENT");
848 
849 	/* At this point, server sent 1 byte. */
850 	fds.fd = fd;
851 	poll_flags = POLLIN | POLLRDNORM;
852 	fds.events = poll_flags;
853 
854 	/* Try to wait for 1 sec. */
855 	if (poll(&fds, 1, 1000) < 0) {
856 		perror("poll");
857 		exit(EXIT_FAILURE);
858 	}
859 
860 	/* poll() must return nothing. */
861 	if (fds.revents) {
862 		fprintf(stderr, "Unexpected poll result %hx\n",
863 			fds.revents);
864 		exit(EXIT_FAILURE);
865 	}
866 
867 	/* Tell server to send rest of data. */
868 	control_writeln("CLNSENT");
869 
870 	/* Poll for data. */
871 	if (poll(&fds, 1, 10000) < 0) {
872 		perror("poll");
873 		exit(EXIT_FAILURE);
874 	}
875 
876 	/* Only these two bits are expected. */
877 	if (fds.revents != poll_flags) {
878 		fprintf(stderr, "Unexpected poll result %hx\n",
879 			fds.revents);
880 		exit(EXIT_FAILURE);
881 	}
882 
883 	/* Use MSG_DONTWAIT, if call is going to wait, EAGAIN
884 	 * will be returned.
885 	 */
886 	recv_buf(fd, buf, sizeof(buf), MSG_DONTWAIT, RCVLOWAT_BUF_SIZE);
887 
888 	control_writeln("POLLDONE");
889 
890 	close(fd);
891 }
892 
893 #define INV_BUF_TEST_DATA_LEN 512
894 
895 static void test_inv_buf_client(const struct test_opts *opts, bool stream)
896 {
897 	unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
898 	ssize_t expected_ret;
899 	int fd;
900 
901 	if (stream)
902 		fd = vsock_stream_connect(opts->peer_cid, 1234);
903 	else
904 		fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
905 
906 	if (fd < 0) {
907 		perror("connect");
908 		exit(EXIT_FAILURE);
909 	}
910 
911 	control_expectln("SENDDONE");
912 
913 	/* Use invalid buffer here. */
914 	recv_buf(fd, NULL, sizeof(data), 0, -EFAULT);
915 
916 	if (stream) {
917 		/* For SOCK_STREAM we must continue reading. */
918 		expected_ret = sizeof(data);
919 	} else {
920 		/* For SOCK_SEQPACKET socket's queue must be empty. */
921 		expected_ret = -EAGAIN;
922 	}
923 
924 	recv_buf(fd, data, sizeof(data), MSG_DONTWAIT, expected_ret);
925 
926 	control_writeln("DONE");
927 
928 	close(fd);
929 }
930 
931 static void test_inv_buf_server(const struct test_opts *opts, bool stream)
932 {
933 	unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
934 	int fd;
935 
936 	if (stream)
937 		fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
938 	else
939 		fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
940 
941 	if (fd < 0) {
942 		perror("accept");
943 		exit(EXIT_FAILURE);
944 	}
945 
946 	send_buf(fd, data, sizeof(data), 0, sizeof(data));
947 
948 	control_writeln("SENDDONE");
949 
950 	control_expectln("DONE");
951 
952 	close(fd);
953 }
954 
955 static void test_stream_inv_buf_client(const struct test_opts *opts)
956 {
957 	test_inv_buf_client(opts, true);
958 }
959 
960 static void test_stream_inv_buf_server(const struct test_opts *opts)
961 {
962 	test_inv_buf_server(opts, true);
963 }
964 
965 static void test_seqpacket_inv_buf_client(const struct test_opts *opts)
966 {
967 	test_inv_buf_client(opts, false);
968 }
969 
970 static void test_seqpacket_inv_buf_server(const struct test_opts *opts)
971 {
972 	test_inv_buf_server(opts, false);
973 }
974 
975 #define HELLO_STR "HELLO"
976 #define WORLD_STR "WORLD"
977 
978 static void test_stream_virtio_skb_merge_client(const struct test_opts *opts)
979 {
980 	int fd;
981 
982 	fd = vsock_stream_connect(opts->peer_cid, 1234);
983 	if (fd < 0) {
984 		perror("connect");
985 		exit(EXIT_FAILURE);
986 	}
987 
988 	/* Send first skbuff. */
989 	send_buf(fd, HELLO_STR, strlen(HELLO_STR), 0, strlen(HELLO_STR));
990 
991 	control_writeln("SEND0");
992 	/* Peer reads part of first skbuff. */
993 	control_expectln("REPLY0");
994 
995 	/* Send second skbuff, it will be appended to the first. */
996 	send_buf(fd, WORLD_STR, strlen(WORLD_STR), 0, strlen(WORLD_STR));
997 
998 	control_writeln("SEND1");
999 	/* Peer reads merged skbuff packet. */
1000 	control_expectln("REPLY1");
1001 
1002 	close(fd);
1003 }
1004 
1005 static void test_stream_virtio_skb_merge_server(const struct test_opts *opts)
1006 {
1007 	size_t read = 0, to_read;
1008 	unsigned char buf[64];
1009 	int fd;
1010 
1011 	fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
1012 	if (fd < 0) {
1013 		perror("accept");
1014 		exit(EXIT_FAILURE);
1015 	}
1016 
1017 	control_expectln("SEND0");
1018 
1019 	/* Read skbuff partially. */
1020 	to_read = 2;
1021 	recv_buf(fd, buf + read, to_read, 0, to_read);
1022 	read += to_read;
1023 
1024 	control_writeln("REPLY0");
1025 	control_expectln("SEND1");
1026 
1027 	/* Read the rest of both buffers */
1028 	to_read = strlen(HELLO_STR WORLD_STR) - read;
1029 	recv_buf(fd, buf + read, to_read, 0, to_read);
1030 	read += to_read;
1031 
1032 	/* No more bytes should be there */
1033 	to_read = sizeof(buf) - read;
1034 	recv_buf(fd, buf + read, to_read, MSG_DONTWAIT, -EAGAIN);
1035 
1036 	if (memcmp(buf, HELLO_STR WORLD_STR, strlen(HELLO_STR WORLD_STR))) {
1037 		fprintf(stderr, "pattern mismatch\n");
1038 		exit(EXIT_FAILURE);
1039 	}
1040 
1041 	control_writeln("REPLY1");
1042 
1043 	close(fd);
1044 }
1045 
1046 static void test_seqpacket_msg_peek_client(const struct test_opts *opts)
1047 {
1048 	return test_msg_peek_client(opts, true);
1049 }
1050 
1051 static void test_seqpacket_msg_peek_server(const struct test_opts *opts)
1052 {
1053 	return test_msg_peek_server(opts, true);
1054 }
1055 
1056 static sig_atomic_t have_sigpipe;
1057 
1058 static void sigpipe(int signo)
1059 {
1060 	have_sigpipe = 1;
1061 }
1062 
1063 static void test_stream_check_sigpipe(int fd)
1064 {
1065 	ssize_t res;
1066 
1067 	have_sigpipe = 0;
1068 
1069 	res = send(fd, "A", 1, 0);
1070 	if (res != -1) {
1071 		fprintf(stderr, "expected send(2) failure, got %zi\n", res);
1072 		exit(EXIT_FAILURE);
1073 	}
1074 
1075 	if (!have_sigpipe) {
1076 		fprintf(stderr, "SIGPIPE expected\n");
1077 		exit(EXIT_FAILURE);
1078 	}
1079 
1080 	have_sigpipe = 0;
1081 
1082 	res = send(fd, "A", 1, MSG_NOSIGNAL);
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 not expected\n");
1090 		exit(EXIT_FAILURE);
1091 	}
1092 }
1093 
1094 static void test_stream_shutwr_client(const struct test_opts *opts)
1095 {
1096 	int fd;
1097 
1098 	struct sigaction act = {
1099 		.sa_handler = sigpipe,
1100 	};
1101 
1102 	sigaction(SIGPIPE, &act, NULL);
1103 
1104 	fd = vsock_stream_connect(opts->peer_cid, 1234);
1105 	if (fd < 0) {
1106 		perror("connect");
1107 		exit(EXIT_FAILURE);
1108 	}
1109 
1110 	if (shutdown(fd, SHUT_WR)) {
1111 		perror("shutdown");
1112 		exit(EXIT_FAILURE);
1113 	}
1114 
1115 	test_stream_check_sigpipe(fd);
1116 
1117 	control_writeln("CLIENTDONE");
1118 
1119 	close(fd);
1120 }
1121 
1122 static void test_stream_shutwr_server(const struct test_opts *opts)
1123 {
1124 	int fd;
1125 
1126 	fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
1127 	if (fd < 0) {
1128 		perror("accept");
1129 		exit(EXIT_FAILURE);
1130 	}
1131 
1132 	control_expectln("CLIENTDONE");
1133 
1134 	close(fd);
1135 }
1136 
1137 static void test_stream_shutrd_client(const struct test_opts *opts)
1138 {
1139 	int fd;
1140 
1141 	struct sigaction act = {
1142 		.sa_handler = sigpipe,
1143 	};
1144 
1145 	sigaction(SIGPIPE, &act, NULL);
1146 
1147 	fd = vsock_stream_connect(opts->peer_cid, 1234);
1148 	if (fd < 0) {
1149 		perror("connect");
1150 		exit(EXIT_FAILURE);
1151 	}
1152 
1153 	control_expectln("SHUTRDDONE");
1154 
1155 	test_stream_check_sigpipe(fd);
1156 
1157 	control_writeln("CLIENTDONE");
1158 
1159 	close(fd);
1160 }
1161 
1162 static void test_stream_shutrd_server(const struct test_opts *opts)
1163 {
1164 	int fd;
1165 
1166 	fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
1167 	if (fd < 0) {
1168 		perror("accept");
1169 		exit(EXIT_FAILURE);
1170 	}
1171 
1172 	if (shutdown(fd, SHUT_RD)) {
1173 		perror("shutdown");
1174 		exit(EXIT_FAILURE);
1175 	}
1176 
1177 	control_writeln("SHUTRDDONE");
1178 	control_expectln("CLIENTDONE");
1179 
1180 	close(fd);
1181 }
1182 
1183 static struct test_case test_cases[] = {
1184 	{
1185 		.name = "SOCK_STREAM connection reset",
1186 		.run_client = test_stream_connection_reset,
1187 	},
1188 	{
1189 		.name = "SOCK_STREAM bind only",
1190 		.run_client = test_stream_bind_only_client,
1191 		.run_server = test_stream_bind_only_server,
1192 	},
1193 	{
1194 		.name = "SOCK_STREAM client close",
1195 		.run_client = test_stream_client_close_client,
1196 		.run_server = test_stream_client_close_server,
1197 	},
1198 	{
1199 		.name = "SOCK_STREAM server close",
1200 		.run_client = test_stream_server_close_client,
1201 		.run_server = test_stream_server_close_server,
1202 	},
1203 	{
1204 		.name = "SOCK_STREAM multiple connections",
1205 		.run_client = test_stream_multiconn_client,
1206 		.run_server = test_stream_multiconn_server,
1207 	},
1208 	{
1209 		.name = "SOCK_STREAM MSG_PEEK",
1210 		.run_client = test_stream_msg_peek_client,
1211 		.run_server = test_stream_msg_peek_server,
1212 	},
1213 	{
1214 		.name = "SOCK_SEQPACKET msg bounds",
1215 		.run_client = test_seqpacket_msg_bounds_client,
1216 		.run_server = test_seqpacket_msg_bounds_server,
1217 	},
1218 	{
1219 		.name = "SOCK_SEQPACKET MSG_TRUNC flag",
1220 		.run_client = test_seqpacket_msg_trunc_client,
1221 		.run_server = test_seqpacket_msg_trunc_server,
1222 	},
1223 	{
1224 		.name = "SOCK_SEQPACKET timeout",
1225 		.run_client = test_seqpacket_timeout_client,
1226 		.run_server = test_seqpacket_timeout_server,
1227 	},
1228 	{
1229 		.name = "SOCK_SEQPACKET invalid receive buffer",
1230 		.run_client = test_seqpacket_invalid_rec_buffer_client,
1231 		.run_server = test_seqpacket_invalid_rec_buffer_server,
1232 	},
1233 	{
1234 		.name = "SOCK_STREAM poll() + SO_RCVLOWAT",
1235 		.run_client = test_stream_poll_rcvlowat_client,
1236 		.run_server = test_stream_poll_rcvlowat_server,
1237 	},
1238 	{
1239 		.name = "SOCK_SEQPACKET big message",
1240 		.run_client = test_seqpacket_bigmsg_client,
1241 		.run_server = test_seqpacket_bigmsg_server,
1242 	},
1243 	{
1244 		.name = "SOCK_STREAM test invalid buffer",
1245 		.run_client = test_stream_inv_buf_client,
1246 		.run_server = test_stream_inv_buf_server,
1247 	},
1248 	{
1249 		.name = "SOCK_SEQPACKET test invalid buffer",
1250 		.run_client = test_seqpacket_inv_buf_client,
1251 		.run_server = test_seqpacket_inv_buf_server,
1252 	},
1253 	{
1254 		.name = "SOCK_STREAM virtio skb merge",
1255 		.run_client = test_stream_virtio_skb_merge_client,
1256 		.run_server = test_stream_virtio_skb_merge_server,
1257 	},
1258 	{
1259 		.name = "SOCK_SEQPACKET MSG_PEEK",
1260 		.run_client = test_seqpacket_msg_peek_client,
1261 		.run_server = test_seqpacket_msg_peek_server,
1262 	},
1263 	{
1264 		.name = "SOCK_STREAM SHUT_WR",
1265 		.run_client = test_stream_shutwr_client,
1266 		.run_server = test_stream_shutwr_server,
1267 	},
1268 	{
1269 		.name = "SOCK_STREAM SHUT_RD",
1270 		.run_client = test_stream_shutrd_client,
1271 		.run_server = test_stream_shutrd_server,
1272 	},
1273 	{
1274 		.name = "SOCK_STREAM MSG_ZEROCOPY",
1275 		.run_client = test_stream_msgzcopy_client,
1276 		.run_server = test_stream_msgzcopy_server,
1277 	},
1278 	{
1279 		.name = "SOCK_SEQPACKET MSG_ZEROCOPY",
1280 		.run_client = test_seqpacket_msgzcopy_client,
1281 		.run_server = test_seqpacket_msgzcopy_server,
1282 	},
1283 	{
1284 		.name = "SOCK_STREAM MSG_ZEROCOPY empty MSG_ERRQUEUE",
1285 		.run_client = test_stream_msgzcopy_empty_errq_client,
1286 		.run_server = test_stream_msgzcopy_empty_errq_server,
1287 	},
1288 	{},
1289 };
1290 
1291 static const char optstring[] = "";
1292 static const struct option longopts[] = {
1293 	{
1294 		.name = "control-host",
1295 		.has_arg = required_argument,
1296 		.val = 'H',
1297 	},
1298 	{
1299 		.name = "control-port",
1300 		.has_arg = required_argument,
1301 		.val = 'P',
1302 	},
1303 	{
1304 		.name = "mode",
1305 		.has_arg = required_argument,
1306 		.val = 'm',
1307 	},
1308 	{
1309 		.name = "peer-cid",
1310 		.has_arg = required_argument,
1311 		.val = 'p',
1312 	},
1313 	{
1314 		.name = "list",
1315 		.has_arg = no_argument,
1316 		.val = 'l',
1317 	},
1318 	{
1319 		.name = "skip",
1320 		.has_arg = required_argument,
1321 		.val = 's',
1322 	},
1323 	{
1324 		.name = "help",
1325 		.has_arg = no_argument,
1326 		.val = '?',
1327 	},
1328 	{},
1329 };
1330 
1331 static void usage(void)
1332 {
1333 	fprintf(stderr, "Usage: vsock_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--list] [--skip=<test_id>]\n"
1334 		"\n"
1335 		"  Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n"
1336 		"  Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
1337 		"\n"
1338 		"Run vsock.ko tests.  Must be launched in both guest\n"
1339 		"and host.  One side must use --mode=client and\n"
1340 		"the other side must use --mode=server.\n"
1341 		"\n"
1342 		"A TCP control socket connection is used to coordinate tests\n"
1343 		"between the client and the server.  The server requires a\n"
1344 		"listen address and the client requires an address to\n"
1345 		"connect to.\n"
1346 		"\n"
1347 		"The CID of the other side must be given with --peer-cid=<cid>.\n"
1348 		"\n"
1349 		"Options:\n"
1350 		"  --help                 This help message\n"
1351 		"  --control-host <host>  Server IP address to connect to\n"
1352 		"  --control-port <port>  Server port to listen on/connect to\n"
1353 		"  --mode client|server   Server or client mode\n"
1354 		"  --peer-cid <cid>       CID of the other side\n"
1355 		"  --list                 List of tests that will be executed\n"
1356 		"  --skip <test_id>       Test ID to skip;\n"
1357 		"                         use multiple --skip options to skip more tests\n"
1358 		);
1359 	exit(EXIT_FAILURE);
1360 }
1361 
1362 int main(int argc, char **argv)
1363 {
1364 	const char *control_host = NULL;
1365 	const char *control_port = NULL;
1366 	struct test_opts opts = {
1367 		.mode = TEST_MODE_UNSET,
1368 		.peer_cid = VMADDR_CID_ANY,
1369 	};
1370 
1371 	srand(time(NULL));
1372 	init_signals();
1373 
1374 	for (;;) {
1375 		int opt = getopt_long(argc, argv, optstring, longopts, NULL);
1376 
1377 		if (opt == -1)
1378 			break;
1379 
1380 		switch (opt) {
1381 		case 'H':
1382 			control_host = optarg;
1383 			break;
1384 		case 'm':
1385 			if (strcmp(optarg, "client") == 0)
1386 				opts.mode = TEST_MODE_CLIENT;
1387 			else if (strcmp(optarg, "server") == 0)
1388 				opts.mode = TEST_MODE_SERVER;
1389 			else {
1390 				fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
1391 				return EXIT_FAILURE;
1392 			}
1393 			break;
1394 		case 'p':
1395 			opts.peer_cid = parse_cid(optarg);
1396 			break;
1397 		case 'P':
1398 			control_port = optarg;
1399 			break;
1400 		case 'l':
1401 			list_tests(test_cases);
1402 			break;
1403 		case 's':
1404 			skip_test(test_cases, ARRAY_SIZE(test_cases) - 1,
1405 				  optarg);
1406 			break;
1407 		case '?':
1408 		default:
1409 			usage();
1410 		}
1411 	}
1412 
1413 	if (!control_port)
1414 		usage();
1415 	if (opts.mode == TEST_MODE_UNSET)
1416 		usage();
1417 	if (opts.peer_cid == VMADDR_CID_ANY)
1418 		usage();
1419 
1420 	if (!control_host) {
1421 		if (opts.mode != TEST_MODE_SERVER)
1422 			usage();
1423 		control_host = "0.0.0.0";
1424 	}
1425 
1426 	control_init(control_host, control_port,
1427 		     opts.mode == TEST_MODE_SERVER);
1428 
1429 	run_tests(test_cases, &opts);
1430 
1431 	control_cleanup();
1432 	return EXIT_SUCCESS;
1433 }
1434