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/time64.h> 25 #include <pthread.h> 26 #include <fcntl.h> 27 #include <linux/sockios.h> 28 29 #include "vsock_test_zerocopy.h" 30 #include "timeout.h" 31 #include "control.h" 32 #include "util.h" 33 34 /* Basic messages for control_writeulong(), control_readulong() */ 35 #define CONTROL_CONTINUE 1 36 #define CONTROL_DONE 0 37 38 static void test_stream_connection_reset(const struct test_opts *opts) 39 { 40 union { 41 struct sockaddr sa; 42 struct sockaddr_vm svm; 43 } addr = { 44 .svm = { 45 .svm_family = AF_VSOCK, 46 .svm_port = opts->peer_port, 47 .svm_cid = opts->peer_cid, 48 }, 49 }; 50 int ret; 51 int fd; 52 53 fd = socket(AF_VSOCK, SOCK_STREAM, 0); 54 55 timeout_begin(TIMEOUT); 56 do { 57 ret = connect(fd, &addr.sa, sizeof(addr.svm)); 58 timeout_check("connect"); 59 } while (ret < 0 && errno == EINTR); 60 timeout_end(); 61 62 if (ret != -1) { 63 fprintf(stderr, "expected connect(2) failure, got %d\n", ret); 64 exit(EXIT_FAILURE); 65 } 66 if (errno != ECONNRESET) { 67 fprintf(stderr, "unexpected connect(2) errno %d\n", errno); 68 exit(EXIT_FAILURE); 69 } 70 71 close(fd); 72 } 73 74 static void test_stream_bind_only_client(const struct test_opts *opts) 75 { 76 union { 77 struct sockaddr sa; 78 struct sockaddr_vm svm; 79 } addr = { 80 .svm = { 81 .svm_family = AF_VSOCK, 82 .svm_port = opts->peer_port, 83 .svm_cid = opts->peer_cid, 84 }, 85 }; 86 int ret; 87 int fd; 88 89 /* Wait for the server to be ready */ 90 control_expectln("BIND"); 91 92 fd = socket(AF_VSOCK, SOCK_STREAM, 0); 93 94 timeout_begin(TIMEOUT); 95 do { 96 ret = connect(fd, &addr.sa, sizeof(addr.svm)); 97 timeout_check("connect"); 98 } while (ret < 0 && errno == EINTR); 99 timeout_end(); 100 101 if (ret != -1) { 102 fprintf(stderr, "expected connect(2) failure, got %d\n", ret); 103 exit(EXIT_FAILURE); 104 } 105 if (errno != ECONNRESET) { 106 fprintf(stderr, "unexpected connect(2) errno %d\n", errno); 107 exit(EXIT_FAILURE); 108 } 109 110 /* Notify the server that the client has finished */ 111 control_writeln("DONE"); 112 113 close(fd); 114 } 115 116 static void test_stream_bind_only_server(const struct test_opts *opts) 117 { 118 int fd; 119 120 fd = vsock_bind(VMADDR_CID_ANY, opts->peer_port, SOCK_STREAM); 121 122 /* Notify the client that the server is ready */ 123 control_writeln("BIND"); 124 125 /* Wait for the client to finish */ 126 control_expectln("DONE"); 127 128 close(fd); 129 } 130 131 static void test_stream_client_close_client(const struct test_opts *opts) 132 { 133 int fd; 134 135 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 136 if (fd < 0) { 137 perror("connect"); 138 exit(EXIT_FAILURE); 139 } 140 141 send_byte(fd, 1, 0); 142 close(fd); 143 } 144 145 static void test_stream_client_close_server(const struct test_opts *opts) 146 { 147 int fd; 148 149 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 150 if (fd < 0) { 151 perror("accept"); 152 exit(EXIT_FAILURE); 153 } 154 155 /* Wait for the remote to close the connection, before check 156 * -EPIPE error on send. 157 */ 158 vsock_wait_remote_close(fd); 159 160 send_byte(fd, -EPIPE, 0); 161 recv_byte(fd, 1, 0); 162 recv_byte(fd, 0, 0); 163 close(fd); 164 } 165 166 static void test_stream_server_close_client(const struct test_opts *opts) 167 { 168 int fd; 169 170 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 171 if (fd < 0) { 172 perror("connect"); 173 exit(EXIT_FAILURE); 174 } 175 176 /* Wait for the remote to close the connection, before check 177 * -EPIPE error on send. 178 */ 179 vsock_wait_remote_close(fd); 180 181 send_byte(fd, -EPIPE, 0); 182 recv_byte(fd, 1, 0); 183 recv_byte(fd, 0, 0); 184 close(fd); 185 } 186 187 static void test_stream_server_close_server(const struct test_opts *opts) 188 { 189 int fd; 190 191 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 192 if (fd < 0) { 193 perror("accept"); 194 exit(EXIT_FAILURE); 195 } 196 197 send_byte(fd, 1, 0); 198 close(fd); 199 } 200 201 /* With the standard socket sizes, VMCI is able to support about 100 202 * concurrent stream connections. 203 */ 204 #define MULTICONN_NFDS 100 205 206 static void test_stream_multiconn_client(const struct test_opts *opts) 207 { 208 int fds[MULTICONN_NFDS]; 209 int i; 210 211 for (i = 0; i < MULTICONN_NFDS; i++) { 212 fds[i] = vsock_stream_connect(opts->peer_cid, opts->peer_port); 213 if (fds[i] < 0) { 214 perror("connect"); 215 exit(EXIT_FAILURE); 216 } 217 } 218 219 for (i = 0; i < MULTICONN_NFDS; i++) { 220 if (i % 2) 221 recv_byte(fds[i], 1, 0); 222 else 223 send_byte(fds[i], 1, 0); 224 } 225 226 for (i = 0; i < MULTICONN_NFDS; i++) 227 close(fds[i]); 228 } 229 230 static void test_stream_multiconn_server(const struct test_opts *opts) 231 { 232 int fds[MULTICONN_NFDS]; 233 int i; 234 235 for (i = 0; i < MULTICONN_NFDS; i++) { 236 fds[i] = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 237 if (fds[i] < 0) { 238 perror("accept"); 239 exit(EXIT_FAILURE); 240 } 241 } 242 243 for (i = 0; i < MULTICONN_NFDS; i++) { 244 if (i % 2) 245 send_byte(fds[i], 1, 0); 246 else 247 recv_byte(fds[i], 1, 0); 248 } 249 250 for (i = 0; i < MULTICONN_NFDS; i++) 251 close(fds[i]); 252 } 253 254 #define MSG_PEEK_BUF_LEN 64 255 256 static void test_msg_peek_client(const struct test_opts *opts, 257 bool seqpacket) 258 { 259 unsigned char buf[MSG_PEEK_BUF_LEN]; 260 int fd; 261 int i; 262 263 if (seqpacket) 264 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port); 265 else 266 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 267 268 if (fd < 0) { 269 perror("connect"); 270 exit(EXIT_FAILURE); 271 } 272 273 for (i = 0; i < sizeof(buf); i++) 274 buf[i] = rand() & 0xFF; 275 276 control_expectln("SRVREADY"); 277 278 send_buf(fd, buf, sizeof(buf), 0, sizeof(buf)); 279 280 close(fd); 281 } 282 283 static void test_msg_peek_server(const struct test_opts *opts, 284 bool seqpacket) 285 { 286 unsigned char buf_half[MSG_PEEK_BUF_LEN / 2]; 287 unsigned char buf_normal[MSG_PEEK_BUF_LEN]; 288 unsigned char buf_peek[MSG_PEEK_BUF_LEN]; 289 int fd; 290 291 if (seqpacket) 292 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 293 else 294 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 295 296 if (fd < 0) { 297 perror("accept"); 298 exit(EXIT_FAILURE); 299 } 300 301 /* Peek from empty socket. */ 302 recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK | MSG_DONTWAIT, 303 -EAGAIN); 304 305 control_writeln("SRVREADY"); 306 307 /* Peek part of data. */ 308 recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK, sizeof(buf_half)); 309 310 /* Peek whole data. */ 311 recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK, sizeof(buf_peek)); 312 313 /* Compare partial and full peek. */ 314 if (memcmp(buf_half, buf_peek, sizeof(buf_half))) { 315 fprintf(stderr, "Partial peek data mismatch\n"); 316 exit(EXIT_FAILURE); 317 } 318 319 if (seqpacket) { 320 /* This type of socket supports MSG_TRUNC flag, 321 * so check it with MSG_PEEK. We must get length 322 * of the message. 323 */ 324 recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK | MSG_TRUNC, 325 sizeof(buf_peek)); 326 } 327 328 recv_buf(fd, buf_normal, sizeof(buf_normal), 0, sizeof(buf_normal)); 329 330 /* Compare full peek and normal read. */ 331 if (memcmp(buf_peek, buf_normal, sizeof(buf_peek))) { 332 fprintf(stderr, "Full peek data mismatch\n"); 333 exit(EXIT_FAILURE); 334 } 335 336 close(fd); 337 } 338 339 static void test_stream_msg_peek_client(const struct test_opts *opts) 340 { 341 return test_msg_peek_client(opts, false); 342 } 343 344 static void test_stream_msg_peek_server(const struct test_opts *opts) 345 { 346 return test_msg_peek_server(opts, false); 347 } 348 349 #define SOCK_BUF_SIZE (2 * 1024 * 1024) 350 #define SOCK_BUF_SIZE_SMALL (64 * 1024) 351 #define MAX_MSG_PAGES 4 352 353 static void test_seqpacket_msg_bounds_client(const struct test_opts *opts) 354 { 355 unsigned long long sock_buf_size; 356 unsigned long curr_hash; 357 size_t max_msg_size; 358 int page_size; 359 int msg_count; 360 int fd; 361 362 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port); 363 if (fd < 0) { 364 perror("connect"); 365 exit(EXIT_FAILURE); 366 } 367 368 sock_buf_size = SOCK_BUF_SIZE; 369 370 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, 371 sock_buf_size, 372 "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)"); 373 374 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 375 sock_buf_size, 376 "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)"); 377 378 /* Wait, until receiver sets buffer size. */ 379 control_expectln("SRVREADY"); 380 381 curr_hash = 0; 382 page_size = getpagesize(); 383 max_msg_size = MAX_MSG_PAGES * page_size; 384 msg_count = SOCK_BUF_SIZE / max_msg_size; 385 386 for (int i = 0; i < msg_count; i++) { 387 size_t buf_size; 388 int flags; 389 void *buf; 390 391 /* Use "small" buffers and "big" buffers. */ 392 if (i & 1) 393 buf_size = page_size + 394 (rand() % (max_msg_size - page_size)); 395 else 396 buf_size = 1 + (rand() % page_size); 397 398 buf = malloc(buf_size); 399 400 if (!buf) { 401 perror("malloc"); 402 exit(EXIT_FAILURE); 403 } 404 405 memset(buf, rand() & 0xff, buf_size); 406 /* Set at least one MSG_EOR + some random. */ 407 if (i == (msg_count / 2) || (rand() & 1)) { 408 flags = MSG_EOR; 409 curr_hash++; 410 } else { 411 flags = 0; 412 } 413 414 send_buf(fd, buf, buf_size, flags, buf_size); 415 416 /* 417 * Hash sum is computed at both client and server in 418 * the same way: 419 * H += hash('message data') 420 * Such hash "controls" both data integrity and message 421 * bounds. After data exchange, both sums are compared 422 * using control socket, and if message bounds wasn't 423 * broken - two values must be equal. 424 */ 425 curr_hash += hash_djb2(buf, buf_size); 426 free(buf); 427 } 428 429 control_writeln("SENDDONE"); 430 control_writeulong(curr_hash); 431 close(fd); 432 } 433 434 static void test_seqpacket_msg_bounds_server(const struct test_opts *opts) 435 { 436 unsigned long long sock_buf_size; 437 unsigned long remote_hash; 438 unsigned long curr_hash; 439 int fd; 440 struct msghdr msg = {0}; 441 struct iovec iov = {0}; 442 443 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 444 if (fd < 0) { 445 perror("accept"); 446 exit(EXIT_FAILURE); 447 } 448 449 sock_buf_size = SOCK_BUF_SIZE; 450 451 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, 452 sock_buf_size, 453 "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)"); 454 455 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 456 sock_buf_size, 457 "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)"); 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 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 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 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 * NSEC_PER_SEC) + ts.tv_nsec; 567 } 568 569 #define RCVTIMEO_TIMEOUT_SEC 1 570 #define READ_OVERHEAD_NSEC 250000000 /* 0.25 sec */ 571 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 setsockopt_timeval_check(fd, SOL_SOCKET, SO_RCVTIMEO, tv, 590 "setsockopt(SO_RCVTIMEO)"); 591 592 read_enter_ns = current_nsec(); 593 594 if (read(fd, &dummy, sizeof(dummy)) != -1) { 595 fprintf(stderr, 596 "expected 'dummy' read(2) failure\n"); 597 exit(EXIT_FAILURE); 598 } 599 600 if (errno != EAGAIN) { 601 perror("EAGAIN expected"); 602 exit(EXIT_FAILURE); 603 } 604 605 read_overhead_ns = current_nsec() - read_enter_ns - 606 NSEC_PER_SEC * RCVTIMEO_TIMEOUT_SEC; 607 608 if (read_overhead_ns > READ_OVERHEAD_NSEC) { 609 fprintf(stderr, 610 "too much time in read(2), %lu > %i ns\n", 611 read_overhead_ns, READ_OVERHEAD_NSEC); 612 exit(EXIT_FAILURE); 613 } 614 615 control_writeln("WAITDONE"); 616 close(fd); 617 } 618 619 static void test_seqpacket_timeout_server(const struct test_opts *opts) 620 { 621 int fd; 622 623 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 624 if (fd < 0) { 625 perror("accept"); 626 exit(EXIT_FAILURE); 627 } 628 629 control_expectln("WAITDONE"); 630 close(fd); 631 } 632 633 static void test_seqpacket_bigmsg_client(const struct test_opts *opts) 634 { 635 unsigned long long sock_buf_size; 636 size_t buf_size; 637 socklen_t len; 638 void *data; 639 int fd; 640 641 len = sizeof(sock_buf_size); 642 643 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port); 644 if (fd < 0) { 645 perror("connect"); 646 exit(EXIT_FAILURE); 647 } 648 649 if (getsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 650 &sock_buf_size, &len)) { 651 perror("getsockopt"); 652 exit(EXIT_FAILURE); 653 } 654 655 sock_buf_size++; 656 657 /* size_t can be < unsigned long long */ 658 buf_size = (size_t)sock_buf_size; 659 if (buf_size != sock_buf_size) { 660 fprintf(stderr, "Returned BUFFER_SIZE too large\n"); 661 exit(EXIT_FAILURE); 662 } 663 664 data = malloc(buf_size); 665 if (!data) { 666 perror("malloc"); 667 exit(EXIT_FAILURE); 668 } 669 670 send_buf(fd, data, buf_size, 0, -EMSGSIZE); 671 672 control_writeln("CLISENT"); 673 674 free(data); 675 close(fd); 676 } 677 678 static void test_seqpacket_bigmsg_server(const struct test_opts *opts) 679 { 680 int fd; 681 682 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 683 if (fd < 0) { 684 perror("accept"); 685 exit(EXIT_FAILURE); 686 } 687 688 control_expectln("CLISENT"); 689 690 close(fd); 691 } 692 693 #define BUF_PATTERN_1 'a' 694 #define BUF_PATTERN_2 'b' 695 696 static void test_seqpacket_invalid_rec_buffer_client(const struct test_opts *opts) 697 { 698 int fd; 699 unsigned char *buf1; 700 unsigned char *buf2; 701 int buf_size = getpagesize() * 3; 702 703 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port); 704 if (fd < 0) { 705 perror("connect"); 706 exit(EXIT_FAILURE); 707 } 708 709 buf1 = malloc(buf_size); 710 if (!buf1) { 711 perror("'malloc()' for 'buf1'"); 712 exit(EXIT_FAILURE); 713 } 714 715 buf2 = malloc(buf_size); 716 if (!buf2) { 717 perror("'malloc()' for 'buf2'"); 718 exit(EXIT_FAILURE); 719 } 720 721 memset(buf1, BUF_PATTERN_1, buf_size); 722 memset(buf2, BUF_PATTERN_2, buf_size); 723 724 send_buf(fd, buf1, buf_size, 0, buf_size); 725 726 send_buf(fd, buf2, buf_size, 0, buf_size); 727 728 close(fd); 729 } 730 731 static void test_seqpacket_invalid_rec_buffer_server(const struct test_opts *opts) 732 { 733 int fd; 734 unsigned char *broken_buf; 735 unsigned char *valid_buf; 736 int page_size = getpagesize(); 737 int buf_size = page_size * 3; 738 ssize_t res; 739 int prot = PROT_READ | PROT_WRITE; 740 int flags = MAP_PRIVATE | MAP_ANONYMOUS; 741 int i; 742 743 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 744 if (fd < 0) { 745 perror("accept"); 746 exit(EXIT_FAILURE); 747 } 748 749 /* Setup first buffer. */ 750 broken_buf = mmap(NULL, buf_size, prot, flags, -1, 0); 751 if (broken_buf == MAP_FAILED) { 752 perror("mmap for 'broken_buf'"); 753 exit(EXIT_FAILURE); 754 } 755 756 /* Unmap "hole" in buffer. */ 757 if (munmap(broken_buf + page_size, page_size)) { 758 perror("'broken_buf' setup"); 759 exit(EXIT_FAILURE); 760 } 761 762 valid_buf = mmap(NULL, buf_size, prot, flags, -1, 0); 763 if (valid_buf == MAP_FAILED) { 764 perror("mmap for 'valid_buf'"); 765 exit(EXIT_FAILURE); 766 } 767 768 /* Try to fill buffer with unmapped middle. */ 769 res = read(fd, broken_buf, buf_size); 770 if (res != -1) { 771 fprintf(stderr, 772 "expected 'broken_buf' read(2) failure, got %zi\n", 773 res); 774 exit(EXIT_FAILURE); 775 } 776 777 if (errno != EFAULT) { 778 perror("unexpected errno of 'broken_buf'"); 779 exit(EXIT_FAILURE); 780 } 781 782 /* Try to fill valid buffer. */ 783 res = read(fd, valid_buf, buf_size); 784 if (res < 0) { 785 perror("unexpected 'valid_buf' read(2) failure"); 786 exit(EXIT_FAILURE); 787 } 788 789 if (res != buf_size) { 790 fprintf(stderr, 791 "invalid 'valid_buf' read(2), expected %i, got %zi\n", 792 buf_size, res); 793 exit(EXIT_FAILURE); 794 } 795 796 for (i = 0; i < buf_size; i++) { 797 if (valid_buf[i] != BUF_PATTERN_2) { 798 fprintf(stderr, 799 "invalid pattern for 'valid_buf' at %i, expected %hhX, got %hhX\n", 800 i, BUF_PATTERN_2, valid_buf[i]); 801 exit(EXIT_FAILURE); 802 } 803 } 804 805 /* Unmap buffers. */ 806 munmap(broken_buf, page_size); 807 munmap(broken_buf + page_size * 2, page_size); 808 munmap(valid_buf, buf_size); 809 close(fd); 810 } 811 812 #define RCVLOWAT_BUF_SIZE 128 813 814 static void test_stream_poll_rcvlowat_server(const struct test_opts *opts) 815 { 816 int fd; 817 int i; 818 819 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 820 if (fd < 0) { 821 perror("accept"); 822 exit(EXIT_FAILURE); 823 } 824 825 /* Send 1 byte. */ 826 send_byte(fd, 1, 0); 827 828 control_writeln("SRVSENT"); 829 830 /* Wait until client is ready to receive rest of data. */ 831 control_expectln("CLNSENT"); 832 833 for (i = 0; i < RCVLOWAT_BUF_SIZE - 1; i++) 834 send_byte(fd, 1, 0); 835 836 /* Keep socket in active state. */ 837 control_expectln("POLLDONE"); 838 839 close(fd); 840 } 841 842 static void test_stream_poll_rcvlowat_client(const struct test_opts *opts) 843 { 844 int lowat_val = RCVLOWAT_BUF_SIZE; 845 char buf[RCVLOWAT_BUF_SIZE]; 846 struct pollfd fds; 847 short poll_flags; 848 int fd; 849 850 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 851 if (fd < 0) { 852 perror("connect"); 853 exit(EXIT_FAILURE); 854 } 855 856 setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT, 857 lowat_val, "setsockopt(SO_RCVLOWAT)"); 858 859 control_expectln("SRVSENT"); 860 861 /* At this point, server sent 1 byte. */ 862 fds.fd = fd; 863 poll_flags = POLLIN | POLLRDNORM; 864 fds.events = poll_flags; 865 866 /* Try to wait for 1 sec. */ 867 if (poll(&fds, 1, 1000) < 0) { 868 perror("poll"); 869 exit(EXIT_FAILURE); 870 } 871 872 /* poll() must return nothing. */ 873 if (fds.revents) { 874 fprintf(stderr, "Unexpected poll result %hx\n", 875 fds.revents); 876 exit(EXIT_FAILURE); 877 } 878 879 /* Tell server to send rest of data. */ 880 control_writeln("CLNSENT"); 881 882 /* Poll for data. */ 883 if (poll(&fds, 1, 10000) < 0) { 884 perror("poll"); 885 exit(EXIT_FAILURE); 886 } 887 888 /* Only these two bits are expected. */ 889 if (fds.revents != poll_flags) { 890 fprintf(stderr, "Unexpected poll result %hx\n", 891 fds.revents); 892 exit(EXIT_FAILURE); 893 } 894 895 /* Use MSG_DONTWAIT, if call is going to wait, EAGAIN 896 * will be returned. 897 */ 898 recv_buf(fd, buf, sizeof(buf), MSG_DONTWAIT, RCVLOWAT_BUF_SIZE); 899 900 control_writeln("POLLDONE"); 901 902 close(fd); 903 } 904 905 #define INV_BUF_TEST_DATA_LEN 512 906 907 static void test_inv_buf_client(const struct test_opts *opts, bool stream) 908 { 909 unsigned char data[INV_BUF_TEST_DATA_LEN] = {0}; 910 ssize_t expected_ret; 911 int fd; 912 913 if (stream) 914 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 915 else 916 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port); 917 918 if (fd < 0) { 919 perror("connect"); 920 exit(EXIT_FAILURE); 921 } 922 923 control_expectln("SENDDONE"); 924 925 /* Use invalid buffer here. */ 926 recv_buf(fd, NULL, sizeof(data), 0, -EFAULT); 927 928 if (stream) { 929 /* For SOCK_STREAM we must continue reading. */ 930 expected_ret = sizeof(data); 931 } else { 932 /* For SOCK_SEQPACKET socket's queue must be empty. */ 933 expected_ret = -EAGAIN; 934 } 935 936 recv_buf(fd, data, sizeof(data), MSG_DONTWAIT, expected_ret); 937 938 control_writeln("DONE"); 939 940 close(fd); 941 } 942 943 static void test_inv_buf_server(const struct test_opts *opts, bool stream) 944 { 945 unsigned char data[INV_BUF_TEST_DATA_LEN] = {0}; 946 int fd; 947 948 if (stream) 949 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 950 else 951 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 952 953 if (fd < 0) { 954 perror("accept"); 955 exit(EXIT_FAILURE); 956 } 957 958 send_buf(fd, data, sizeof(data), 0, sizeof(data)); 959 960 control_writeln("SENDDONE"); 961 962 control_expectln("DONE"); 963 964 close(fd); 965 } 966 967 static void test_stream_inv_buf_client(const struct test_opts *opts) 968 { 969 test_inv_buf_client(opts, true); 970 } 971 972 static void test_stream_inv_buf_server(const struct test_opts *opts) 973 { 974 test_inv_buf_server(opts, true); 975 } 976 977 static void test_seqpacket_inv_buf_client(const struct test_opts *opts) 978 { 979 test_inv_buf_client(opts, false); 980 } 981 982 static void test_seqpacket_inv_buf_server(const struct test_opts *opts) 983 { 984 test_inv_buf_server(opts, false); 985 } 986 987 #define HELLO_STR "HELLO" 988 #define WORLD_STR "WORLD" 989 990 static void test_stream_virtio_skb_merge_client(const struct test_opts *opts) 991 { 992 int fd; 993 994 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 995 if (fd < 0) { 996 perror("connect"); 997 exit(EXIT_FAILURE); 998 } 999 1000 /* Send first skbuff. */ 1001 send_buf(fd, HELLO_STR, strlen(HELLO_STR), 0, strlen(HELLO_STR)); 1002 1003 control_writeln("SEND0"); 1004 /* Peer reads part of first skbuff. */ 1005 control_expectln("REPLY0"); 1006 1007 /* Send second skbuff, it will be appended to the first. */ 1008 send_buf(fd, WORLD_STR, strlen(WORLD_STR), 0, strlen(WORLD_STR)); 1009 1010 control_writeln("SEND1"); 1011 /* Peer reads merged skbuff packet. */ 1012 control_expectln("REPLY1"); 1013 1014 close(fd); 1015 } 1016 1017 static void test_stream_virtio_skb_merge_server(const struct test_opts *opts) 1018 { 1019 size_t read = 0, to_read; 1020 unsigned char buf[64]; 1021 int fd; 1022 1023 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1024 if (fd < 0) { 1025 perror("accept"); 1026 exit(EXIT_FAILURE); 1027 } 1028 1029 control_expectln("SEND0"); 1030 1031 /* Read skbuff partially. */ 1032 to_read = 2; 1033 recv_buf(fd, buf + read, to_read, 0, to_read); 1034 read += to_read; 1035 1036 control_writeln("REPLY0"); 1037 control_expectln("SEND1"); 1038 1039 /* Read the rest of both buffers */ 1040 to_read = strlen(HELLO_STR WORLD_STR) - read; 1041 recv_buf(fd, buf + read, to_read, 0, to_read); 1042 read += to_read; 1043 1044 /* No more bytes should be there */ 1045 to_read = sizeof(buf) - read; 1046 recv_buf(fd, buf + read, to_read, MSG_DONTWAIT, -EAGAIN); 1047 1048 if (memcmp(buf, HELLO_STR WORLD_STR, strlen(HELLO_STR WORLD_STR))) { 1049 fprintf(stderr, "pattern mismatch\n"); 1050 exit(EXIT_FAILURE); 1051 } 1052 1053 control_writeln("REPLY1"); 1054 1055 close(fd); 1056 } 1057 1058 static void test_seqpacket_msg_peek_client(const struct test_opts *opts) 1059 { 1060 return test_msg_peek_client(opts, true); 1061 } 1062 1063 static void test_seqpacket_msg_peek_server(const struct test_opts *opts) 1064 { 1065 return test_msg_peek_server(opts, true); 1066 } 1067 1068 static sig_atomic_t have_sigpipe; 1069 1070 static void sigpipe(int signo) 1071 { 1072 have_sigpipe = 1; 1073 } 1074 1075 #define SEND_SLEEP_USEC (10 * 1000) 1076 1077 static void test_stream_check_sigpipe(int fd) 1078 { 1079 ssize_t res; 1080 1081 have_sigpipe = 0; 1082 1083 /* When the other peer calls shutdown(SHUT_RD), there is a chance that 1084 * the send() call could occur before the message carrying the close 1085 * information arrives over the transport. In such cases, the send() 1086 * might still succeed. To avoid this race, let's retry the send() call 1087 * a few times, ensuring the test is more reliable. 1088 */ 1089 timeout_begin(TIMEOUT); 1090 while(1) { 1091 res = send(fd, "A", 1, 0); 1092 if (res == -1 && errno != EINTR) 1093 break; 1094 1095 /* Sleep a little before trying again to avoid flooding the 1096 * other peer and filling its receive buffer, causing 1097 * false-negative. 1098 */ 1099 timeout_usleep(SEND_SLEEP_USEC); 1100 timeout_check("send"); 1101 } 1102 timeout_end(); 1103 1104 if (errno != EPIPE) { 1105 fprintf(stderr, "unexpected send(2) errno %d\n", errno); 1106 exit(EXIT_FAILURE); 1107 } 1108 if (!have_sigpipe) { 1109 fprintf(stderr, "SIGPIPE expected\n"); 1110 exit(EXIT_FAILURE); 1111 } 1112 1113 have_sigpipe = 0; 1114 1115 timeout_begin(TIMEOUT); 1116 while(1) { 1117 res = send(fd, "A", 1, MSG_NOSIGNAL); 1118 if (res == -1 && errno != EINTR) 1119 break; 1120 1121 timeout_usleep(SEND_SLEEP_USEC); 1122 timeout_check("send"); 1123 } 1124 timeout_end(); 1125 1126 if (errno != EPIPE) { 1127 fprintf(stderr, "unexpected send(2) errno %d\n", errno); 1128 exit(EXIT_FAILURE); 1129 } 1130 if (have_sigpipe) { 1131 fprintf(stderr, "SIGPIPE not expected\n"); 1132 exit(EXIT_FAILURE); 1133 } 1134 } 1135 1136 static void test_stream_shutwr_client(const struct test_opts *opts) 1137 { 1138 int fd; 1139 1140 struct sigaction act = { 1141 .sa_handler = sigpipe, 1142 }; 1143 1144 sigaction(SIGPIPE, &act, NULL); 1145 1146 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 1147 if (fd < 0) { 1148 perror("connect"); 1149 exit(EXIT_FAILURE); 1150 } 1151 1152 if (shutdown(fd, SHUT_WR)) { 1153 perror("shutdown"); 1154 exit(EXIT_FAILURE); 1155 } 1156 1157 test_stream_check_sigpipe(fd); 1158 1159 control_writeln("CLIENTDONE"); 1160 1161 close(fd); 1162 } 1163 1164 static void test_stream_shutwr_server(const struct test_opts *opts) 1165 { 1166 int fd; 1167 1168 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1169 if (fd < 0) { 1170 perror("accept"); 1171 exit(EXIT_FAILURE); 1172 } 1173 1174 control_expectln("CLIENTDONE"); 1175 1176 close(fd); 1177 } 1178 1179 static void test_stream_shutrd_client(const struct test_opts *opts) 1180 { 1181 int fd; 1182 1183 struct sigaction act = { 1184 .sa_handler = sigpipe, 1185 }; 1186 1187 sigaction(SIGPIPE, &act, NULL); 1188 1189 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 1190 if (fd < 0) { 1191 perror("connect"); 1192 exit(EXIT_FAILURE); 1193 } 1194 1195 control_expectln("SHUTRDDONE"); 1196 1197 test_stream_check_sigpipe(fd); 1198 1199 control_writeln("CLIENTDONE"); 1200 1201 close(fd); 1202 } 1203 1204 static void test_stream_shutrd_server(const struct test_opts *opts) 1205 { 1206 int fd; 1207 1208 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1209 if (fd < 0) { 1210 perror("accept"); 1211 exit(EXIT_FAILURE); 1212 } 1213 1214 if (shutdown(fd, SHUT_RD)) { 1215 perror("shutdown"); 1216 exit(EXIT_FAILURE); 1217 } 1218 1219 control_writeln("SHUTRDDONE"); 1220 control_expectln("CLIENTDONE"); 1221 1222 close(fd); 1223 } 1224 1225 static void test_double_bind_connect_server(const struct test_opts *opts) 1226 { 1227 int listen_fd, client_fd, i; 1228 struct sockaddr_vm sa_client; 1229 socklen_t socklen_client = sizeof(sa_client); 1230 1231 listen_fd = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port); 1232 1233 for (i = 0; i < 2; i++) { 1234 control_writeln("LISTENING"); 1235 1236 timeout_begin(TIMEOUT); 1237 do { 1238 client_fd = accept(listen_fd, (struct sockaddr *)&sa_client, 1239 &socklen_client); 1240 timeout_check("accept"); 1241 } while (client_fd < 0 && errno == EINTR); 1242 timeout_end(); 1243 1244 if (client_fd < 0) { 1245 perror("accept"); 1246 exit(EXIT_FAILURE); 1247 } 1248 1249 /* Waiting for remote peer to close connection */ 1250 vsock_wait_remote_close(client_fd); 1251 } 1252 1253 close(listen_fd); 1254 } 1255 1256 static void test_double_bind_connect_client(const struct test_opts *opts) 1257 { 1258 int i, client_fd; 1259 1260 for (i = 0; i < 2; i++) { 1261 /* Wait until server is ready to accept a new connection */ 1262 control_expectln("LISTENING"); 1263 1264 /* We use 'peer_port + 1' as "some" port for the 'bind()' 1265 * call. It is safe for overflow, but must be considered, 1266 * when running multiple test applications simultaneously 1267 * where 'peer-port' argument differs by 1. 1268 */ 1269 client_fd = vsock_bind_connect(opts->peer_cid, opts->peer_port, 1270 opts->peer_port + 1, SOCK_STREAM); 1271 1272 close(client_fd); 1273 } 1274 } 1275 1276 #define MSG_BUF_IOCTL_LEN 64 1277 static void test_unsent_bytes_server(const struct test_opts *opts, int type) 1278 { 1279 unsigned char buf[MSG_BUF_IOCTL_LEN]; 1280 int client_fd; 1281 1282 client_fd = vsock_accept(VMADDR_CID_ANY, opts->peer_port, NULL, type); 1283 if (client_fd < 0) { 1284 perror("accept"); 1285 exit(EXIT_FAILURE); 1286 } 1287 1288 recv_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf)); 1289 control_writeln("RECEIVED"); 1290 1291 close(client_fd); 1292 } 1293 1294 static void test_unsent_bytes_client(const struct test_opts *opts, int type) 1295 { 1296 unsigned char buf[MSG_BUF_IOCTL_LEN]; 1297 int fd; 1298 1299 fd = vsock_connect(opts->peer_cid, opts->peer_port, type); 1300 if (fd < 0) { 1301 perror("connect"); 1302 exit(EXIT_FAILURE); 1303 } 1304 1305 for (int i = 0; i < sizeof(buf); i++) 1306 buf[i] = rand() & 0xFF; 1307 1308 send_buf(fd, buf, sizeof(buf), 0, sizeof(buf)); 1309 control_expectln("RECEIVED"); 1310 1311 /* SIOCOUTQ isn't guaranteed to instantly track sent data. Even though 1312 * the "RECEIVED" message means that the other side has received the 1313 * data, there can be a delay in our kernel before updating the "unsent 1314 * bytes" counter. vsock_wait_sent() will repeat SIOCOUTQ until it 1315 * returns 0. 1316 */ 1317 if (!vsock_wait_sent(fd)) 1318 fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n"); 1319 1320 close(fd); 1321 } 1322 1323 static void test_unread_bytes_server(const struct test_opts *opts, int type) 1324 { 1325 unsigned char buf[MSG_BUF_IOCTL_LEN]; 1326 int client_fd; 1327 1328 client_fd = vsock_accept(VMADDR_CID_ANY, opts->peer_port, NULL, type); 1329 if (client_fd < 0) { 1330 perror("accept"); 1331 exit(EXIT_FAILURE); 1332 } 1333 1334 for (int i = 0; i < sizeof(buf); i++) 1335 buf[i] = rand() & 0xFF; 1336 1337 send_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf)); 1338 control_writeln("SENT"); 1339 1340 close(client_fd); 1341 } 1342 1343 static void test_unread_bytes_client(const struct test_opts *opts, int type) 1344 { 1345 unsigned char buf[MSG_BUF_IOCTL_LEN]; 1346 int fd; 1347 1348 fd = vsock_connect(opts->peer_cid, opts->peer_port, type); 1349 if (fd < 0) { 1350 perror("connect"); 1351 exit(EXIT_FAILURE); 1352 } 1353 1354 control_expectln("SENT"); 1355 /* The data has arrived but has not been read. The expected is 1356 * MSG_BUF_IOCTL_LEN. 1357 */ 1358 if (!vsock_ioctl_int(fd, SIOCINQ, MSG_BUF_IOCTL_LEN)) { 1359 fprintf(stderr, "Test skipped, SIOCINQ not supported.\n"); 1360 goto out; 1361 } 1362 1363 recv_buf(fd, buf, sizeof(buf), 0, sizeof(buf)); 1364 /* All data has been consumed, so the expected is 0. */ 1365 vsock_ioctl_int(fd, SIOCINQ, 0); 1366 1367 out: 1368 close(fd); 1369 } 1370 1371 static void test_stream_unsent_bytes_client(const struct test_opts *opts) 1372 { 1373 test_unsent_bytes_client(opts, SOCK_STREAM); 1374 } 1375 1376 static void test_stream_unsent_bytes_server(const struct test_opts *opts) 1377 { 1378 test_unsent_bytes_server(opts, SOCK_STREAM); 1379 } 1380 1381 static void test_seqpacket_unsent_bytes_client(const struct test_opts *opts) 1382 { 1383 test_unsent_bytes_client(opts, SOCK_SEQPACKET); 1384 } 1385 1386 static void test_seqpacket_unsent_bytes_server(const struct test_opts *opts) 1387 { 1388 test_unsent_bytes_server(opts, SOCK_SEQPACKET); 1389 } 1390 1391 static void test_stream_unread_bytes_client(const struct test_opts *opts) 1392 { 1393 test_unread_bytes_client(opts, SOCK_STREAM); 1394 } 1395 1396 static void test_stream_unread_bytes_server(const struct test_opts *opts) 1397 { 1398 test_unread_bytes_server(opts, SOCK_STREAM); 1399 } 1400 1401 static void test_seqpacket_unread_bytes_client(const struct test_opts *opts) 1402 { 1403 test_unread_bytes_client(opts, SOCK_SEQPACKET); 1404 } 1405 1406 static void test_seqpacket_unread_bytes_server(const struct test_opts *opts) 1407 { 1408 test_unread_bytes_server(opts, SOCK_SEQPACKET); 1409 } 1410 1411 #define RCVLOWAT_CREDIT_UPD_BUF_SIZE (1024 * 128) 1412 /* This define is the same as in 'include/linux/virtio_vsock.h': 1413 * it is used to decide when to send credit update message during 1414 * reading from rx queue of a socket. Value and its usage in 1415 * kernel is important for this test. 1416 */ 1417 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (1024 * 64) 1418 1419 static void test_stream_rcvlowat_def_cred_upd_client(const struct test_opts *opts) 1420 { 1421 size_t buf_size; 1422 void *buf; 1423 int fd; 1424 1425 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 1426 if (fd < 0) { 1427 perror("connect"); 1428 exit(EXIT_FAILURE); 1429 } 1430 1431 /* Send 1 byte more than peer's buffer size. */ 1432 buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE + 1; 1433 1434 buf = malloc(buf_size); 1435 if (!buf) { 1436 perror("malloc"); 1437 exit(EXIT_FAILURE); 1438 } 1439 1440 /* Wait until peer sets needed buffer size. */ 1441 recv_byte(fd, 1, 0); 1442 1443 if (send(fd, buf, buf_size, 0) != buf_size) { 1444 perror("send failed"); 1445 exit(EXIT_FAILURE); 1446 } 1447 1448 free(buf); 1449 close(fd); 1450 } 1451 1452 static void test_stream_credit_update_test(const struct test_opts *opts, 1453 bool low_rx_bytes_test) 1454 { 1455 int recv_buf_size; 1456 struct pollfd fds; 1457 size_t buf_size; 1458 unsigned long long sock_buf_size; 1459 void *buf; 1460 int fd; 1461 1462 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1463 if (fd < 0) { 1464 perror("accept"); 1465 exit(EXIT_FAILURE); 1466 } 1467 1468 buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE; 1469 1470 /* size_t can be < unsigned long long */ 1471 sock_buf_size = buf_size; 1472 1473 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 1474 sock_buf_size, 1475 "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)"); 1476 1477 if (low_rx_bytes_test) { 1478 /* Set new SO_RCVLOWAT here. This enables sending credit 1479 * update when number of bytes if our rx queue become < 1480 * SO_RCVLOWAT value. 1481 */ 1482 recv_buf_size = 1 + VIRTIO_VSOCK_MAX_PKT_BUF_SIZE; 1483 1484 setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT, 1485 recv_buf_size, "setsockopt(SO_RCVLOWAT)"); 1486 } 1487 1488 /* Send one dummy byte here, because 'setsockopt()' above also 1489 * sends special packet which tells sender to update our buffer 1490 * size. This 'send_byte()' will serialize such packet with data 1491 * reads in a loop below. Sender starts transmission only when 1492 * it receives this single byte. 1493 */ 1494 send_byte(fd, 1, 0); 1495 1496 buf = malloc(buf_size); 1497 if (!buf) { 1498 perror("malloc"); 1499 exit(EXIT_FAILURE); 1500 } 1501 1502 /* Wait until there will be 128KB of data in rx queue. */ 1503 recv_buf(fd, buf, buf_size, MSG_PEEK, buf_size); 1504 1505 /* There is 128KB of data in the socket's rx queue, dequeue first 1506 * 64KB, credit update is sent if 'low_rx_bytes_test' == true. 1507 * Otherwise, credit update is sent in 'if (!low_rx_bytes_test)'. 1508 */ 1509 recv_buf_size = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE; 1510 recv_buf(fd, buf, recv_buf_size, 0, recv_buf_size); 1511 1512 if (!low_rx_bytes_test) { 1513 recv_buf_size++; 1514 1515 /* Updating SO_RCVLOWAT will send credit update. */ 1516 setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT, 1517 recv_buf_size, "setsockopt(SO_RCVLOWAT)"); 1518 } 1519 1520 fds.fd = fd; 1521 fds.events = POLLIN | POLLRDNORM | POLLERR | 1522 POLLRDHUP | POLLHUP; 1523 1524 /* This 'poll()' will return once we receive last byte 1525 * sent by client. 1526 */ 1527 if (poll(&fds, 1, -1) < 0) { 1528 perror("poll"); 1529 exit(EXIT_FAILURE); 1530 } 1531 1532 if (fds.revents & POLLERR) { 1533 fprintf(stderr, "'poll()' error\n"); 1534 exit(EXIT_FAILURE); 1535 } 1536 1537 if (fds.revents & (POLLIN | POLLRDNORM)) { 1538 recv_buf(fd, buf, recv_buf_size, MSG_DONTWAIT, recv_buf_size); 1539 } else { 1540 /* These flags must be set, as there is at 1541 * least 64KB of data ready to read. 1542 */ 1543 fprintf(stderr, "POLLIN | POLLRDNORM expected\n"); 1544 exit(EXIT_FAILURE); 1545 } 1546 1547 free(buf); 1548 close(fd); 1549 } 1550 1551 static void test_stream_cred_upd_on_low_rx_bytes(const struct test_opts *opts) 1552 { 1553 test_stream_credit_update_test(opts, true); 1554 } 1555 1556 static void test_stream_cred_upd_on_set_rcvlowat(const struct test_opts *opts) 1557 { 1558 test_stream_credit_update_test(opts, false); 1559 } 1560 1561 /* The goal of test leak_acceptq is to stress the race between connect() and 1562 * close(listener). Implementation of client/server loops boils down to: 1563 * 1564 * client server 1565 * ------ ------ 1566 * write(CONTINUE) 1567 * expect(CONTINUE) 1568 * listen() 1569 * write(LISTENING) 1570 * expect(LISTENING) 1571 * connect() close() 1572 */ 1573 #define ACCEPTQ_LEAK_RACE_TIMEOUT 2 /* seconds */ 1574 1575 static void test_stream_leak_acceptq_client(const struct test_opts *opts) 1576 { 1577 time_t tout; 1578 int fd; 1579 1580 tout = current_nsec() + ACCEPTQ_LEAK_RACE_TIMEOUT * NSEC_PER_SEC; 1581 do { 1582 control_writeulong(CONTROL_CONTINUE); 1583 1584 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 1585 if (fd >= 0) 1586 close(fd); 1587 } while (current_nsec() < tout); 1588 1589 control_writeulong(CONTROL_DONE); 1590 } 1591 1592 /* Test for a memory leak. User is expected to run kmemleak scan, see README. */ 1593 static void test_stream_leak_acceptq_server(const struct test_opts *opts) 1594 { 1595 int fd; 1596 1597 while (control_readulong() == CONTROL_CONTINUE) { 1598 fd = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port); 1599 control_writeln("LISTENING"); 1600 close(fd); 1601 } 1602 } 1603 1604 /* Test for a memory leak. User is expected to run kmemleak scan, see README. */ 1605 static void test_stream_msgzcopy_leak_errq_client(const struct test_opts *opts) 1606 { 1607 struct pollfd fds = { 0 }; 1608 int fd; 1609 1610 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 1611 if (fd < 0) { 1612 perror("connect"); 1613 exit(EXIT_FAILURE); 1614 } 1615 1616 enable_so_zerocopy_check(fd); 1617 send_byte(fd, 1, MSG_ZEROCOPY); 1618 1619 fds.fd = fd; 1620 fds.events = 0; 1621 if (poll(&fds, 1, -1) < 0) { 1622 perror("poll"); 1623 exit(EXIT_FAILURE); 1624 } 1625 1626 close(fd); 1627 } 1628 1629 static void test_stream_msgzcopy_leak_errq_server(const struct test_opts *opts) 1630 { 1631 int fd; 1632 1633 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1634 if (fd < 0) { 1635 perror("accept"); 1636 exit(EXIT_FAILURE); 1637 } 1638 1639 recv_byte(fd, 1, 0); 1640 vsock_wait_remote_close(fd); 1641 close(fd); 1642 } 1643 1644 /* Test msgzcopy_leak_zcskb is meant to exercise sendmsg() error handling path, 1645 * that might leak an skb. The idea is to fail virtio_transport_init_zcopy_skb() 1646 * by hitting net.core.optmem_max limit in sock_omalloc(), specifically 1647 * 1648 * vsock_connectible_sendmsg 1649 * virtio_transport_stream_enqueue 1650 * virtio_transport_send_pkt_info 1651 * virtio_transport_init_zcopy_skb 1652 * . msg_zerocopy_realloc 1653 * . msg_zerocopy_alloc 1654 * . sock_omalloc 1655 * . sk_omem_alloc + size > sysctl_optmem_max 1656 * return -ENOMEM 1657 * 1658 * We abuse the implementation detail of net/socket.c:____sys_sendmsg(). 1659 * sk_omem_alloc can be precisely bumped by sock_kmalloc(), as it is used to 1660 * fetch user-provided control data. 1661 * 1662 * While this approach works for now, it relies on assumptions regarding the 1663 * implementation and configuration (for example, order of net.core.optmem_max 1664 * can not exceed MAX_PAGE_ORDER), which may not hold in the future. A more 1665 * resilient testing could be implemented by leveraging the Fault injection 1666 * framework (CONFIG_FAULT_INJECTION), e.g. 1667 * 1668 * client# echo N > /sys/kernel/debug/failslab/ignore-gfp-wait 1669 * client# echo 0 > /sys/kernel/debug/failslab/verbose 1670 * 1671 * void client(const struct test_opts *opts) 1672 * { 1673 * char buf[16]; 1674 * int f, s, i; 1675 * 1676 * f = open("/proc/self/fail-nth", O_WRONLY); 1677 * 1678 * for (i = 1; i < 32; i++) { 1679 * control_writeulong(CONTROL_CONTINUE); 1680 * 1681 * s = vsock_stream_connect(opts->peer_cid, opts->peer_port); 1682 * enable_so_zerocopy_check(s); 1683 * 1684 * sprintf(buf, "%d", i); 1685 * write(f, buf, strlen(buf)); 1686 * 1687 * send(s, &(char){ 0 }, 1, MSG_ZEROCOPY); 1688 * 1689 * write(f, "0", 1); 1690 * close(s); 1691 * } 1692 * 1693 * control_writeulong(CONTROL_DONE); 1694 * close(f); 1695 * } 1696 * 1697 * void server(const struct test_opts *opts) 1698 * { 1699 * int fd; 1700 * 1701 * while (control_readulong() == CONTROL_CONTINUE) { 1702 * fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1703 * vsock_wait_remote_close(fd); 1704 * close(fd); 1705 * } 1706 * } 1707 * 1708 * Refer to Documentation/fault-injection/fault-injection.rst. 1709 */ 1710 #define MAX_PAGE_ORDER 10 /* usually */ 1711 #define PAGE_SIZE 4096 1712 1713 /* Test for a memory leak. User is expected to run kmemleak scan, see README. */ 1714 static void test_stream_msgzcopy_leak_zcskb_client(const struct test_opts *opts) 1715 { 1716 size_t optmem_max, ctl_len, chunk_size; 1717 struct msghdr msg = { 0 }; 1718 struct iovec iov; 1719 char *chunk; 1720 int fd, res; 1721 FILE *f; 1722 1723 f = fopen("/proc/sys/net/core/optmem_max", "r"); 1724 if (!f) { 1725 perror("fopen(optmem_max)"); 1726 exit(EXIT_FAILURE); 1727 } 1728 1729 if (fscanf(f, "%zu", &optmem_max) != 1) { 1730 fprintf(stderr, "fscanf(optmem_max) failed\n"); 1731 exit(EXIT_FAILURE); 1732 } 1733 1734 fclose(f); 1735 1736 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 1737 if (fd < 0) { 1738 perror("connect"); 1739 exit(EXIT_FAILURE); 1740 } 1741 1742 enable_so_zerocopy_check(fd); 1743 1744 ctl_len = optmem_max - 1; 1745 if (ctl_len > PAGE_SIZE << MAX_PAGE_ORDER) { 1746 fprintf(stderr, "Try with net.core.optmem_max = 100000\n"); 1747 exit(EXIT_FAILURE); 1748 } 1749 1750 chunk_size = CMSG_SPACE(ctl_len); 1751 chunk = malloc(chunk_size); 1752 if (!chunk) { 1753 perror("malloc"); 1754 exit(EXIT_FAILURE); 1755 } 1756 memset(chunk, 0, chunk_size); 1757 1758 iov.iov_base = &(char){ 0 }; 1759 iov.iov_len = 1; 1760 1761 msg.msg_iov = &iov; 1762 msg.msg_iovlen = 1; 1763 msg.msg_control = chunk; 1764 msg.msg_controllen = ctl_len; 1765 1766 errno = 0; 1767 res = sendmsg(fd, &msg, MSG_ZEROCOPY); 1768 if (res >= 0 || errno != ENOMEM) { 1769 fprintf(stderr, "Expected ENOMEM, got errno=%d res=%d\n", 1770 errno, res); 1771 exit(EXIT_FAILURE); 1772 } 1773 1774 close(fd); 1775 } 1776 1777 static void test_stream_msgzcopy_leak_zcskb_server(const struct test_opts *opts) 1778 { 1779 int fd; 1780 1781 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1782 if (fd < 0) { 1783 perror("accept"); 1784 exit(EXIT_FAILURE); 1785 } 1786 1787 vsock_wait_remote_close(fd); 1788 close(fd); 1789 } 1790 1791 #define MAX_PORT_RETRIES 24 /* net/vmw_vsock/af_vsock.c */ 1792 1793 static bool test_stream_transport_uaf(int cid) 1794 { 1795 int sockets[MAX_PORT_RETRIES]; 1796 struct sockaddr_vm addr; 1797 socklen_t alen; 1798 int fd, i, c; 1799 bool ret; 1800 1801 /* Probe for a transport by attempting a local CID bind. Unavailable 1802 * transport (or more specifically: an unsupported transport/CID 1803 * combination) results in EADDRNOTAVAIL, other errnos are fatal. 1804 */ 1805 fd = vsock_bind_try(cid, VMADDR_PORT_ANY, SOCK_STREAM); 1806 if (fd < 0) { 1807 if (errno != EADDRNOTAVAIL) { 1808 perror("Unexpected bind() errno"); 1809 exit(EXIT_FAILURE); 1810 } 1811 1812 return false; 1813 } 1814 1815 alen = sizeof(addr); 1816 if (getsockname(fd, (struct sockaddr *)&addr, &alen)) { 1817 perror("getsockname"); 1818 exit(EXIT_FAILURE); 1819 } 1820 1821 /* Drain the autobind pool; see __vsock_bind_connectible(). */ 1822 for (i = 0; i < MAX_PORT_RETRIES; ++i) 1823 sockets[i] = vsock_bind(cid, ++addr.svm_port, SOCK_STREAM); 1824 1825 close(fd); 1826 1827 /* Setting SOCK_NONBLOCK makes connect() return soon after 1828 * (re-)assigning the transport. We are not connecting to anything 1829 * anyway, so there is no point entering the main loop in 1830 * vsock_connect(); waiting for timeout, checking for signals, etc. 1831 */ 1832 fd = socket(AF_VSOCK, SOCK_STREAM | SOCK_NONBLOCK, 0); 1833 if (fd < 0) { 1834 perror("socket"); 1835 exit(EXIT_FAILURE); 1836 } 1837 1838 /* Assign transport, while failing to autobind. Autobind pool was 1839 * drained, so EADDRNOTAVAIL coming from __vsock_bind_connectible() is 1840 * expected. 1841 * 1842 * One exception is ENODEV which is thrown by vsock_assign_transport(), 1843 * i.e. before vsock_auto_bind(), when the only transport loaded is 1844 * vhost. 1845 */ 1846 if (!connect(fd, (struct sockaddr *)&addr, alen)) { 1847 fprintf(stderr, "Unexpected connect() success\n"); 1848 exit(EXIT_FAILURE); 1849 } 1850 if (errno == ENODEV && cid == VMADDR_CID_HOST) { 1851 ret = false; 1852 goto cleanup; 1853 } 1854 if (errno != EADDRNOTAVAIL) { 1855 perror("Unexpected connect() errno"); 1856 exit(EXIT_FAILURE); 1857 } 1858 1859 /* Reassign transport, triggering old transport release and 1860 * (potentially) unbinding of an unbound socket. 1861 * 1862 * Vulnerable system may crash now. 1863 */ 1864 for (c = VMADDR_CID_HYPERVISOR; c <= VMADDR_CID_HOST + 1; ++c) { 1865 if (c != cid) { 1866 addr.svm_cid = c; 1867 (void)connect(fd, (struct sockaddr *)&addr, alen); 1868 } 1869 } 1870 1871 ret = true; 1872 cleanup: 1873 close(fd); 1874 while (i--) 1875 close(sockets[i]); 1876 1877 return ret; 1878 } 1879 1880 /* Test attempts to trigger a transport release for an unbound socket. This can 1881 * lead to a reference count mishandling. 1882 */ 1883 static void test_stream_transport_uaf_client(const struct test_opts *opts) 1884 { 1885 bool tested = false; 1886 int cid, tr; 1887 1888 for (cid = VMADDR_CID_HYPERVISOR; cid <= VMADDR_CID_HOST + 1; ++cid) 1889 tested |= test_stream_transport_uaf(cid); 1890 1891 tr = get_transports(); 1892 if (!tr) 1893 fprintf(stderr, "No transports detected\n"); 1894 else if (tr == TRANSPORT_VIRTIO) 1895 fprintf(stderr, "Setup unsupported: sole virtio transport\n"); 1896 else if (!tested) 1897 fprintf(stderr, "No transports tested\n"); 1898 } 1899 1900 static void test_stream_connect_retry_client(const struct test_opts *opts) 1901 { 1902 int fd; 1903 1904 fd = socket(AF_VSOCK, SOCK_STREAM, 0); 1905 if (fd < 0) { 1906 perror("socket"); 1907 exit(EXIT_FAILURE); 1908 } 1909 1910 if (!vsock_connect_fd(fd, opts->peer_cid, opts->peer_port)) { 1911 fprintf(stderr, "Unexpected connect() #1 success\n"); 1912 exit(EXIT_FAILURE); 1913 } 1914 1915 control_writeln("LISTEN"); 1916 control_expectln("LISTENING"); 1917 1918 if (vsock_connect_fd(fd, opts->peer_cid, opts->peer_port)) { 1919 perror("connect() #2"); 1920 exit(EXIT_FAILURE); 1921 } 1922 1923 close(fd); 1924 } 1925 1926 static void test_stream_connect_retry_server(const struct test_opts *opts) 1927 { 1928 int fd; 1929 1930 control_expectln("LISTEN"); 1931 1932 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1933 if (fd < 0) { 1934 perror("accept"); 1935 exit(EXIT_FAILURE); 1936 } 1937 1938 vsock_wait_remote_close(fd); 1939 close(fd); 1940 } 1941 1942 #define TRANSPORT_CHANGE_TIMEOUT 2 /* seconds */ 1943 1944 static void *test_stream_transport_change_thread(void *vargp) 1945 { 1946 pid_t *pid = (pid_t *)vargp; 1947 int ret; 1948 1949 ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); 1950 if (ret) { 1951 fprintf(stderr, "pthread_setcanceltype: %d\n", ret); 1952 exit(EXIT_FAILURE); 1953 } 1954 1955 while (true) { 1956 if (kill(*pid, SIGUSR1) < 0) { 1957 perror("kill"); 1958 exit(EXIT_FAILURE); 1959 } 1960 } 1961 return NULL; 1962 } 1963 1964 static void test_transport_change_signal_handler(int signal) 1965 { 1966 /* We need a custom handler for SIGUSR1 as the default one terminates the process. */ 1967 } 1968 1969 static void test_stream_transport_change_client(const struct test_opts *opts) 1970 { 1971 __sighandler_t old_handler; 1972 pid_t pid = getpid(); 1973 pthread_t thread_id; 1974 time_t tout; 1975 int ret, tr; 1976 1977 tr = get_transports(); 1978 1979 /* Print a warning if there is a G2H transport loaded. 1980 * This is on a best effort basis because VMCI can be either G2H and H2G, and there is 1981 * no easy way to understand it. 1982 * The bug we are testing only appears when G2H transports are not loaded. 1983 * This is because `vsock_assign_transport`, when using CID 0, assigns a G2H transport 1984 * to vsk->transport. If none is available it is set to NULL, causing the null-ptr-deref. 1985 */ 1986 if (tr & TRANSPORTS_G2H) 1987 fprintf(stderr, "G2H Transport detected. This test will not fail.\n"); 1988 1989 old_handler = signal(SIGUSR1, test_transport_change_signal_handler); 1990 if (old_handler == SIG_ERR) { 1991 perror("signal"); 1992 exit(EXIT_FAILURE); 1993 } 1994 1995 ret = pthread_create(&thread_id, NULL, test_stream_transport_change_thread, &pid); 1996 if (ret) { 1997 fprintf(stderr, "pthread_create: %d\n", ret); 1998 exit(EXIT_FAILURE); 1999 } 2000 2001 control_expectln("LISTENING"); 2002 2003 tout = current_nsec() + TRANSPORT_CHANGE_TIMEOUT * NSEC_PER_SEC; 2004 do { 2005 struct sockaddr_vm sa = { 2006 .svm_family = AF_VSOCK, 2007 .svm_cid = opts->peer_cid, 2008 .svm_port = opts->peer_port, 2009 }; 2010 bool send_control = false; 2011 int s; 2012 2013 s = socket(AF_VSOCK, SOCK_STREAM, 0); 2014 if (s < 0) { 2015 perror("socket"); 2016 exit(EXIT_FAILURE); 2017 } 2018 2019 /* Although setting SO_LINGER does not affect the original test 2020 * for null-ptr-deref, it may trigger a lockdep warning. 2021 */ 2022 enable_so_linger(s, 1); 2023 2024 ret = connect(s, (struct sockaddr *)&sa, sizeof(sa)); 2025 /* The connect can fail due to signals coming from the thread, 2026 * or because the receiver connection queue is full. 2027 * Ignoring also the latter case because there is no way 2028 * of synchronizing client's connect and server's accept when 2029 * connect(s) are constantly being interrupted by signals. 2030 */ 2031 if (ret == -1 && (errno != EINTR && errno != ECONNRESET)) { 2032 perror("connect"); 2033 exit(EXIT_FAILURE); 2034 } 2035 2036 /* Notify the server if the connect() is successful or the 2037 * receiver connection queue is full, so it will do accept() 2038 * to drain it. 2039 */ 2040 if (!ret || errno == ECONNRESET) 2041 send_control = true; 2042 2043 /* Set CID to 0 cause a transport change. */ 2044 sa.svm_cid = 0; 2045 2046 /* There is a case where this will not fail: 2047 * if the previous connect() is interrupted while the 2048 * connection request is already sent, this second 2049 * connect() will wait for the response. 2050 */ 2051 ret = connect(s, (struct sockaddr *)&sa, sizeof(sa)); 2052 if (!ret || errno == ECONNRESET) 2053 send_control = true; 2054 2055 close(s); 2056 2057 if (send_control) 2058 control_writeulong(CONTROL_CONTINUE); 2059 2060 } while (current_nsec() < tout); 2061 2062 control_writeulong(CONTROL_DONE); 2063 2064 ret = pthread_cancel(thread_id); 2065 if (ret) { 2066 fprintf(stderr, "pthread_cancel: %d\n", ret); 2067 exit(EXIT_FAILURE); 2068 } 2069 2070 ret = pthread_join(thread_id, NULL); 2071 if (ret) { 2072 fprintf(stderr, "pthread_join: %d\n", ret); 2073 exit(EXIT_FAILURE); 2074 } 2075 2076 if (signal(SIGUSR1, old_handler) == SIG_ERR) { 2077 perror("signal"); 2078 exit(EXIT_FAILURE); 2079 } 2080 } 2081 2082 static void test_stream_transport_change_server(const struct test_opts *opts) 2083 { 2084 int s = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port); 2085 2086 /* Set the socket to be nonblocking because connects that have been interrupted 2087 * (EINTR) can fill the receiver's accept queue anyway, leading to connect failure. 2088 * As of today (6.15) in such situation there is no way to understand, from the 2089 * client side, if the connection has been queued in the server or not. 2090 */ 2091 if (fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK) < 0) { 2092 perror("fcntl"); 2093 exit(EXIT_FAILURE); 2094 } 2095 control_writeln("LISTENING"); 2096 2097 while (control_readulong() == CONTROL_CONTINUE) { 2098 /* Must accept the connection, otherwise the `listen` 2099 * queue will fill up and new connections will fail. 2100 * There can be more than one queued connection, 2101 * clear them all. 2102 */ 2103 while (true) { 2104 int client = accept(s, NULL, NULL); 2105 2106 if (client < 0) { 2107 if (errno == EAGAIN) 2108 break; 2109 2110 perror("accept"); 2111 exit(EXIT_FAILURE); 2112 } 2113 2114 close(client); 2115 } 2116 } 2117 2118 close(s); 2119 } 2120 2121 static void test_stream_linger_client(const struct test_opts *opts) 2122 { 2123 int fd; 2124 2125 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 2126 if (fd < 0) { 2127 perror("connect"); 2128 exit(EXIT_FAILURE); 2129 } 2130 2131 enable_so_linger(fd, 1); 2132 close(fd); 2133 } 2134 2135 static void test_stream_linger_server(const struct test_opts *opts) 2136 { 2137 int fd; 2138 2139 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 2140 if (fd < 0) { 2141 perror("accept"); 2142 exit(EXIT_FAILURE); 2143 } 2144 2145 vsock_wait_remote_close(fd); 2146 close(fd); 2147 } 2148 2149 /* Half of the default to not risk timing out the control channel */ 2150 #define LINGER_TIMEOUT (TIMEOUT / 2) 2151 2152 static void test_stream_nolinger_client(const struct test_opts *opts) 2153 { 2154 bool waited; 2155 time_t ns; 2156 int fd; 2157 2158 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 2159 if (fd < 0) { 2160 perror("connect"); 2161 exit(EXIT_FAILURE); 2162 } 2163 2164 enable_so_linger(fd, LINGER_TIMEOUT); 2165 send_byte(fd, 1, 0); /* Left unread to expose incorrect behaviour. */ 2166 waited = vsock_wait_sent(fd); 2167 2168 ns = current_nsec(); 2169 close(fd); 2170 ns = current_nsec() - ns; 2171 2172 if (!waited) { 2173 fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n"); 2174 } else if (DIV_ROUND_UP(ns, NSEC_PER_SEC) >= LINGER_TIMEOUT) { 2175 fprintf(stderr, "Unexpected lingering\n"); 2176 exit(EXIT_FAILURE); 2177 } 2178 2179 control_writeln("DONE"); 2180 } 2181 2182 static void test_stream_nolinger_server(const struct test_opts *opts) 2183 { 2184 int fd; 2185 2186 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 2187 if (fd < 0) { 2188 perror("accept"); 2189 exit(EXIT_FAILURE); 2190 } 2191 2192 control_expectln("DONE"); 2193 close(fd); 2194 } 2195 2196 static void test_stream_accepted_setsockopt_client(const struct test_opts *opts) 2197 { 2198 int fd; 2199 2200 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 2201 if (fd < 0) { 2202 perror("connect"); 2203 exit(EXIT_FAILURE); 2204 } 2205 2206 close(fd); 2207 } 2208 2209 static void test_stream_accepted_setsockopt_server(const struct test_opts *opts) 2210 { 2211 int fd; 2212 2213 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 2214 if (fd < 0) { 2215 perror("accept"); 2216 exit(EXIT_FAILURE); 2217 } 2218 2219 enable_so_zerocopy_check(fd); 2220 close(fd); 2221 } 2222 2223 static void test_stream_tx_credit_bounds_client(const struct test_opts *opts) 2224 { 2225 unsigned long long sock_buf_size; 2226 size_t total = 0; 2227 char buf[4096]; 2228 int fd; 2229 2230 memset(buf, 'A', sizeof(buf)); 2231 2232 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 2233 if (fd < 0) { 2234 perror("connect"); 2235 exit(EXIT_FAILURE); 2236 } 2237 2238 sock_buf_size = SOCK_BUF_SIZE_SMALL; 2239 2240 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, 2241 sock_buf_size, 2242 "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)"); 2243 2244 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 2245 sock_buf_size, 2246 "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)"); 2247 2248 if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK) < 0) { 2249 perror("fcntl(F_SETFL)"); 2250 exit(EXIT_FAILURE); 2251 } 2252 2253 control_expectln("SRVREADY"); 2254 2255 for (;;) { 2256 ssize_t sent = send(fd, buf, sizeof(buf), 0); 2257 2258 if (sent == 0) { 2259 fprintf(stderr, "unexpected EOF while sending bytes\n"); 2260 exit(EXIT_FAILURE); 2261 } 2262 2263 if (sent < 0) { 2264 if (errno == EINTR) 2265 continue; 2266 2267 if (errno == EAGAIN || errno == EWOULDBLOCK) 2268 break; 2269 2270 perror("send"); 2271 exit(EXIT_FAILURE); 2272 } 2273 2274 total += sent; 2275 } 2276 2277 control_writeln("CLIDONE"); 2278 close(fd); 2279 2280 /* We should not be able to send more bytes than the value set as 2281 * local buffer size. 2282 */ 2283 if (total > sock_buf_size) { 2284 fprintf(stderr, 2285 "TX credit too large: queued %zu bytes (expected <= %llu)\n", 2286 total, sock_buf_size); 2287 exit(EXIT_FAILURE); 2288 } 2289 } 2290 2291 static void test_stream_tx_credit_bounds_server(const struct test_opts *opts) 2292 { 2293 unsigned long long sock_buf_size; 2294 int fd; 2295 2296 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 2297 if (fd < 0) { 2298 perror("accept"); 2299 exit(EXIT_FAILURE); 2300 } 2301 2302 sock_buf_size = SOCK_BUF_SIZE; 2303 2304 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, 2305 sock_buf_size, 2306 "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)"); 2307 2308 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 2309 sock_buf_size, 2310 "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)"); 2311 2312 control_writeln("SRVREADY"); 2313 control_expectln("CLIDONE"); 2314 2315 close(fd); 2316 } 2317 2318 static struct test_case test_cases[] = { 2319 { 2320 .name = "SOCK_STREAM connection reset", 2321 .run_client = test_stream_connection_reset, 2322 }, 2323 { 2324 .name = "SOCK_STREAM bind only", 2325 .run_client = test_stream_bind_only_client, 2326 .run_server = test_stream_bind_only_server, 2327 }, 2328 { 2329 .name = "SOCK_STREAM client close", 2330 .run_client = test_stream_client_close_client, 2331 .run_server = test_stream_client_close_server, 2332 }, 2333 { 2334 .name = "SOCK_STREAM server close", 2335 .run_client = test_stream_server_close_client, 2336 .run_server = test_stream_server_close_server, 2337 }, 2338 { 2339 .name = "SOCK_STREAM multiple connections", 2340 .run_client = test_stream_multiconn_client, 2341 .run_server = test_stream_multiconn_server, 2342 }, 2343 { 2344 .name = "SOCK_STREAM MSG_PEEK", 2345 .run_client = test_stream_msg_peek_client, 2346 .run_server = test_stream_msg_peek_server, 2347 }, 2348 { 2349 .name = "SOCK_SEQPACKET msg bounds", 2350 .run_client = test_seqpacket_msg_bounds_client, 2351 .run_server = test_seqpacket_msg_bounds_server, 2352 }, 2353 { 2354 .name = "SOCK_SEQPACKET MSG_TRUNC flag", 2355 .run_client = test_seqpacket_msg_trunc_client, 2356 .run_server = test_seqpacket_msg_trunc_server, 2357 }, 2358 { 2359 .name = "SOCK_SEQPACKET timeout", 2360 .run_client = test_seqpacket_timeout_client, 2361 .run_server = test_seqpacket_timeout_server, 2362 }, 2363 { 2364 .name = "SOCK_SEQPACKET invalid receive buffer", 2365 .run_client = test_seqpacket_invalid_rec_buffer_client, 2366 .run_server = test_seqpacket_invalid_rec_buffer_server, 2367 }, 2368 { 2369 .name = "SOCK_STREAM poll() + SO_RCVLOWAT", 2370 .run_client = test_stream_poll_rcvlowat_client, 2371 .run_server = test_stream_poll_rcvlowat_server, 2372 }, 2373 { 2374 .name = "SOCK_SEQPACKET big message", 2375 .run_client = test_seqpacket_bigmsg_client, 2376 .run_server = test_seqpacket_bigmsg_server, 2377 }, 2378 { 2379 .name = "SOCK_STREAM test invalid buffer", 2380 .run_client = test_stream_inv_buf_client, 2381 .run_server = test_stream_inv_buf_server, 2382 }, 2383 { 2384 .name = "SOCK_SEQPACKET test invalid buffer", 2385 .run_client = test_seqpacket_inv_buf_client, 2386 .run_server = test_seqpacket_inv_buf_server, 2387 }, 2388 { 2389 .name = "SOCK_STREAM virtio skb merge", 2390 .run_client = test_stream_virtio_skb_merge_client, 2391 .run_server = test_stream_virtio_skb_merge_server, 2392 }, 2393 { 2394 .name = "SOCK_SEQPACKET MSG_PEEK", 2395 .run_client = test_seqpacket_msg_peek_client, 2396 .run_server = test_seqpacket_msg_peek_server, 2397 }, 2398 { 2399 .name = "SOCK_STREAM SHUT_WR", 2400 .run_client = test_stream_shutwr_client, 2401 .run_server = test_stream_shutwr_server, 2402 }, 2403 { 2404 .name = "SOCK_STREAM SHUT_RD", 2405 .run_client = test_stream_shutrd_client, 2406 .run_server = test_stream_shutrd_server, 2407 }, 2408 { 2409 .name = "SOCK_STREAM MSG_ZEROCOPY", 2410 .run_client = test_stream_msgzcopy_client, 2411 .run_server = test_stream_msgzcopy_server, 2412 }, 2413 { 2414 .name = "SOCK_SEQPACKET MSG_ZEROCOPY", 2415 .run_client = test_seqpacket_msgzcopy_client, 2416 .run_server = test_seqpacket_msgzcopy_server, 2417 }, 2418 { 2419 .name = "SOCK_STREAM MSG_ZEROCOPY empty MSG_ERRQUEUE", 2420 .run_client = test_stream_msgzcopy_empty_errq_client, 2421 .run_server = test_stream_msgzcopy_empty_errq_server, 2422 }, 2423 { 2424 .name = "SOCK_STREAM double bind connect", 2425 .run_client = test_double_bind_connect_client, 2426 .run_server = test_double_bind_connect_server, 2427 }, 2428 { 2429 .name = "SOCK_STREAM virtio credit update + SO_RCVLOWAT", 2430 .run_client = test_stream_rcvlowat_def_cred_upd_client, 2431 .run_server = test_stream_cred_upd_on_set_rcvlowat, 2432 }, 2433 { 2434 .name = "SOCK_STREAM virtio credit update + low rx_bytes", 2435 .run_client = test_stream_rcvlowat_def_cred_upd_client, 2436 .run_server = test_stream_cred_upd_on_low_rx_bytes, 2437 }, 2438 { 2439 .name = "SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes", 2440 .run_client = test_stream_unsent_bytes_client, 2441 .run_server = test_stream_unsent_bytes_server, 2442 }, 2443 { 2444 .name = "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes", 2445 .run_client = test_seqpacket_unsent_bytes_client, 2446 .run_server = test_seqpacket_unsent_bytes_server, 2447 }, 2448 { 2449 .name = "SOCK_STREAM leak accept queue", 2450 .run_client = test_stream_leak_acceptq_client, 2451 .run_server = test_stream_leak_acceptq_server, 2452 }, 2453 { 2454 .name = "SOCK_STREAM MSG_ZEROCOPY leak MSG_ERRQUEUE", 2455 .run_client = test_stream_msgzcopy_leak_errq_client, 2456 .run_server = test_stream_msgzcopy_leak_errq_server, 2457 }, 2458 { 2459 .name = "SOCK_STREAM MSG_ZEROCOPY leak completion skb", 2460 .run_client = test_stream_msgzcopy_leak_zcskb_client, 2461 .run_server = test_stream_msgzcopy_leak_zcskb_server, 2462 }, 2463 { 2464 .name = "SOCK_STREAM transport release use-after-free", 2465 .run_client = test_stream_transport_uaf_client, 2466 }, 2467 { 2468 .name = "SOCK_STREAM retry failed connect()", 2469 .run_client = test_stream_connect_retry_client, 2470 .run_server = test_stream_connect_retry_server, 2471 }, 2472 { 2473 .name = "SOCK_STREAM SO_LINGER null-ptr-deref", 2474 .run_client = test_stream_linger_client, 2475 .run_server = test_stream_linger_server, 2476 }, 2477 { 2478 .name = "SOCK_STREAM SO_LINGER close() on unread", 2479 .run_client = test_stream_nolinger_client, 2480 .run_server = test_stream_nolinger_server, 2481 }, 2482 { 2483 .name = "SOCK_STREAM transport change null-ptr-deref, lockdep warn", 2484 .run_client = test_stream_transport_change_client, 2485 .run_server = test_stream_transport_change_server, 2486 }, 2487 { 2488 .name = "SOCK_STREAM ioctl(SIOCINQ) functionality", 2489 .run_client = test_stream_unread_bytes_client, 2490 .run_server = test_stream_unread_bytes_server, 2491 }, 2492 { 2493 .name = "SOCK_SEQPACKET ioctl(SIOCINQ) functionality", 2494 .run_client = test_seqpacket_unread_bytes_client, 2495 .run_server = test_seqpacket_unread_bytes_server, 2496 }, 2497 { 2498 .name = "SOCK_STREAM accept()ed socket custom setsockopt()", 2499 .run_client = test_stream_accepted_setsockopt_client, 2500 .run_server = test_stream_accepted_setsockopt_server, 2501 }, 2502 { 2503 .name = "SOCK_STREAM virtio MSG_ZEROCOPY coalescence corruption", 2504 .run_client = test_stream_msgzcopy_mangle_client, 2505 .run_server = test_stream_msgzcopy_mangle_server, 2506 }, 2507 { 2508 .name = "SOCK_STREAM TX credit bounds", 2509 .run_client = test_stream_tx_credit_bounds_client, 2510 .run_server = test_stream_tx_credit_bounds_server, 2511 }, 2512 {}, 2513 }; 2514 2515 static const char optstring[] = ""; 2516 static const struct option longopts[] = { 2517 { 2518 .name = "control-host", 2519 .has_arg = required_argument, 2520 .val = 'H', 2521 }, 2522 { 2523 .name = "control-port", 2524 .has_arg = required_argument, 2525 .val = 'P', 2526 }, 2527 { 2528 .name = "mode", 2529 .has_arg = required_argument, 2530 .val = 'm', 2531 }, 2532 { 2533 .name = "peer-cid", 2534 .has_arg = required_argument, 2535 .val = 'p', 2536 }, 2537 { 2538 .name = "peer-port", 2539 .has_arg = required_argument, 2540 .val = 'q', 2541 }, 2542 { 2543 .name = "list", 2544 .has_arg = no_argument, 2545 .val = 'l', 2546 }, 2547 { 2548 .name = "skip", 2549 .has_arg = required_argument, 2550 .val = 's', 2551 }, 2552 { 2553 .name = "pick", 2554 .has_arg = required_argument, 2555 .val = 't', 2556 }, 2557 { 2558 .name = "help", 2559 .has_arg = no_argument, 2560 .val = '?', 2561 }, 2562 {}, 2563 }; 2564 2565 static void usage(void) 2566 { 2567 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" 2568 "\n" 2569 " Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n" 2570 " Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n" 2571 "\n" 2572 "Run vsock.ko tests. Must be launched in both guest\n" 2573 "and host. One side must use --mode=client and\n" 2574 "the other side must use --mode=server.\n" 2575 "\n" 2576 "A TCP control socket connection is used to coordinate tests\n" 2577 "between the client and the server. The server requires a\n" 2578 "listen address and the client requires an address to\n" 2579 "connect to.\n" 2580 "\n" 2581 "The CID of the other side must be given with --peer-cid=<cid>.\n" 2582 "During the test, two AF_VSOCK ports will be used: the port\n" 2583 "specified with --peer-port=<port> (or the default port)\n" 2584 "and the next one.\n" 2585 "\n" 2586 "Options:\n" 2587 " --help This help message\n" 2588 " --control-host <host> Server IP address to connect to\n" 2589 " --control-port <port> Server port to listen on/connect to\n" 2590 " --mode client|server Server or client mode\n" 2591 " --peer-cid <cid> CID of the other side\n" 2592 " --peer-port <port> AF_VSOCK port used for the test [default: %d]\n" 2593 " --list List of tests that will be executed\n" 2594 " --pick <test_id> Test ID to execute selectively;\n" 2595 " use multiple --pick options to select more tests\n" 2596 " --skip <test_id> Test ID to skip;\n" 2597 " use multiple --skip options to skip more tests\n", 2598 DEFAULT_PEER_PORT 2599 ); 2600 exit(EXIT_FAILURE); 2601 } 2602 2603 int main(int argc, char **argv) 2604 { 2605 const char *control_host = NULL; 2606 const char *control_port = NULL; 2607 struct test_opts opts = { 2608 .mode = TEST_MODE_UNSET, 2609 .peer_cid = VMADDR_CID_ANY, 2610 .peer_port = DEFAULT_PEER_PORT, 2611 }; 2612 2613 srand(time(NULL)); 2614 init_signals(); 2615 2616 for (;;) { 2617 int opt = getopt_long(argc, argv, optstring, longopts, NULL); 2618 2619 if (opt == -1) 2620 break; 2621 2622 switch (opt) { 2623 case 'H': 2624 control_host = optarg; 2625 break; 2626 case 'm': 2627 if (strcmp(optarg, "client") == 0) 2628 opts.mode = TEST_MODE_CLIENT; 2629 else if (strcmp(optarg, "server") == 0) 2630 opts.mode = TEST_MODE_SERVER; 2631 else { 2632 fprintf(stderr, "--mode must be \"client\" or \"server\"\n"); 2633 return EXIT_FAILURE; 2634 } 2635 break; 2636 case 'p': 2637 opts.peer_cid = parse_cid(optarg); 2638 break; 2639 case 'q': 2640 opts.peer_port = parse_port(optarg); 2641 break; 2642 case 'P': 2643 control_port = optarg; 2644 break; 2645 case 'l': 2646 list_tests(test_cases); 2647 break; 2648 case 's': 2649 skip_test(test_cases, ARRAY_SIZE(test_cases) - 1, 2650 optarg); 2651 break; 2652 case 't': 2653 pick_test(test_cases, ARRAY_SIZE(test_cases) - 1, 2654 optarg); 2655 break; 2656 case '?': 2657 default: 2658 usage(); 2659 } 2660 } 2661 2662 if (!control_port) 2663 usage(); 2664 if (opts.mode == TEST_MODE_UNSET) 2665 usage(); 2666 if (opts.peer_cid == VMADDR_CID_ANY) 2667 usage(); 2668 2669 if (!control_host) { 2670 if (opts.mode != TEST_MODE_SERVER) 2671 usage(); 2672 control_host = "0.0.0.0"; 2673 } 2674 2675 control_init(control_host, control_port, 2676 opts.mode == TEST_MODE_SERVER); 2677 2678 run_tests(test_cases, &opts); 2679 2680 control_cleanup(); 2681 return EXIT_SUCCESS; 2682 } 2683