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