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 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 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 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 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 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 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 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 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 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 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 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 347 static void test_stream_msg_peek_client(const struct test_opts *opts) 348 { 349 return test_msg_peek_client(opts, false); 350 } 351 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 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 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 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, 448 sock_buf_size, 449 "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)"); 450 451 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 452 sock_buf_size, 453 "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)"); 454 455 /* Ready to receive data. */ 456 control_writeln("SRVREADY"); 457 /* Wait, until peer sends whole data. */ 458 control_expectln("SENDDONE"); 459 iov.iov_len = MAX_MSG_PAGES * getpagesize(); 460 iov.iov_base = malloc(iov.iov_len); 461 if (!iov.iov_base) { 462 perror("malloc"); 463 exit(EXIT_FAILURE); 464 } 465 466 msg.msg_iov = &iov; 467 msg.msg_iovlen = 1; 468 469 curr_hash = 0; 470 471 while (1) { 472 ssize_t recv_size; 473 474 recv_size = recvmsg(fd, &msg, 0); 475 476 if (!recv_size) 477 break; 478 479 if (recv_size < 0) { 480 perror("recvmsg"); 481 exit(EXIT_FAILURE); 482 } 483 484 if (msg.msg_flags & MSG_EOR) 485 curr_hash++; 486 487 curr_hash += hash_djb2(msg.msg_iov[0].iov_base, recv_size); 488 } 489 490 free(iov.iov_base); 491 close(fd); 492 remote_hash = control_readulong(); 493 494 if (curr_hash != remote_hash) { 495 fprintf(stderr, "Message bounds broken\n"); 496 exit(EXIT_FAILURE); 497 } 498 } 499 500 #define MESSAGE_TRUNC_SZ 32 501 static void test_seqpacket_msg_trunc_client(const struct test_opts *opts) 502 { 503 int fd; 504 char buf[MESSAGE_TRUNC_SZ]; 505 506 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port); 507 if (fd < 0) { 508 perror("connect"); 509 exit(EXIT_FAILURE); 510 } 511 512 send_buf(fd, buf, sizeof(buf), 0, sizeof(buf)); 513 514 control_writeln("SENDDONE"); 515 close(fd); 516 } 517 518 static void test_seqpacket_msg_trunc_server(const struct test_opts *opts) 519 { 520 int fd; 521 char buf[MESSAGE_TRUNC_SZ / 2]; 522 struct msghdr msg = {0}; 523 struct iovec iov = {0}; 524 525 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 526 if (fd < 0) { 527 perror("accept"); 528 exit(EXIT_FAILURE); 529 } 530 531 control_expectln("SENDDONE"); 532 iov.iov_base = buf; 533 iov.iov_len = sizeof(buf); 534 msg.msg_iov = &iov; 535 msg.msg_iovlen = 1; 536 537 ssize_t ret = recvmsg(fd, &msg, MSG_TRUNC); 538 539 if (ret != MESSAGE_TRUNC_SZ) { 540 printf("%zi\n", ret); 541 perror("MSG_TRUNC doesn't work"); 542 exit(EXIT_FAILURE); 543 } 544 545 if (!(msg.msg_flags & MSG_TRUNC)) { 546 fprintf(stderr, "MSG_TRUNC expected\n"); 547 exit(EXIT_FAILURE); 548 } 549 550 close(fd); 551 } 552 553 static time_t current_nsec(void) 554 { 555 struct timespec ts; 556 557 if (clock_gettime(CLOCK_REALTIME, &ts)) { 558 perror("clock_gettime(3) failed"); 559 exit(EXIT_FAILURE); 560 } 561 562 return (ts.tv_sec * 1000000000ULL) + ts.tv_nsec; 563 } 564 565 #define RCVTIMEO_TIMEOUT_SEC 1 566 #define READ_OVERHEAD_NSEC 250000000 /* 0.25 sec */ 567 568 static void test_seqpacket_timeout_client(const struct test_opts *opts) 569 { 570 int fd; 571 struct timeval tv; 572 char dummy; 573 time_t read_enter_ns; 574 time_t read_overhead_ns; 575 576 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port); 577 if (fd < 0) { 578 perror("connect"); 579 exit(EXIT_FAILURE); 580 } 581 582 tv.tv_sec = RCVTIMEO_TIMEOUT_SEC; 583 tv.tv_usec = 0; 584 585 setsockopt_timeval_check(fd, SOL_SOCKET, SO_RCVTIMEO, tv, 586 "setsockopt(SO_RCVTIMEO)"); 587 588 read_enter_ns = current_nsec(); 589 590 if (read(fd, &dummy, sizeof(dummy)) != -1) { 591 fprintf(stderr, 592 "expected 'dummy' read(2) failure\n"); 593 exit(EXIT_FAILURE); 594 } 595 596 if (errno != EAGAIN) { 597 perror("EAGAIN expected"); 598 exit(EXIT_FAILURE); 599 } 600 601 read_overhead_ns = current_nsec() - read_enter_ns - 602 1000000000ULL * RCVTIMEO_TIMEOUT_SEC; 603 604 if (read_overhead_ns > READ_OVERHEAD_NSEC) { 605 fprintf(stderr, 606 "too much time in read(2), %lu > %i ns\n", 607 read_overhead_ns, READ_OVERHEAD_NSEC); 608 exit(EXIT_FAILURE); 609 } 610 611 control_writeln("WAITDONE"); 612 close(fd); 613 } 614 615 static void test_seqpacket_timeout_server(const struct test_opts *opts) 616 { 617 int fd; 618 619 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 620 if (fd < 0) { 621 perror("accept"); 622 exit(EXIT_FAILURE); 623 } 624 625 control_expectln("WAITDONE"); 626 close(fd); 627 } 628 629 static void test_seqpacket_bigmsg_client(const struct test_opts *opts) 630 { 631 unsigned long long sock_buf_size; 632 size_t buf_size; 633 socklen_t len; 634 void *data; 635 int fd; 636 637 len = sizeof(sock_buf_size); 638 639 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port); 640 if (fd < 0) { 641 perror("connect"); 642 exit(EXIT_FAILURE); 643 } 644 645 if (getsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 646 &sock_buf_size, &len)) { 647 perror("getsockopt"); 648 exit(EXIT_FAILURE); 649 } 650 651 sock_buf_size++; 652 653 /* size_t can be < unsigned long long */ 654 buf_size = (size_t)sock_buf_size; 655 if (buf_size != sock_buf_size) { 656 fprintf(stderr, "Returned BUFFER_SIZE too large\n"); 657 exit(EXIT_FAILURE); 658 } 659 660 data = malloc(buf_size); 661 if (!data) { 662 perror("malloc"); 663 exit(EXIT_FAILURE); 664 } 665 666 send_buf(fd, data, buf_size, 0, -EMSGSIZE); 667 668 control_writeln("CLISENT"); 669 670 free(data); 671 close(fd); 672 } 673 674 static void test_seqpacket_bigmsg_server(const struct test_opts *opts) 675 { 676 int fd; 677 678 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 679 if (fd < 0) { 680 perror("accept"); 681 exit(EXIT_FAILURE); 682 } 683 684 control_expectln("CLISENT"); 685 686 close(fd); 687 } 688 689 #define BUF_PATTERN_1 'a' 690 #define BUF_PATTERN_2 'b' 691 692 static void test_seqpacket_invalid_rec_buffer_client(const struct test_opts *opts) 693 { 694 int fd; 695 unsigned char *buf1; 696 unsigned char *buf2; 697 int buf_size = getpagesize() * 3; 698 699 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port); 700 if (fd < 0) { 701 perror("connect"); 702 exit(EXIT_FAILURE); 703 } 704 705 buf1 = malloc(buf_size); 706 if (!buf1) { 707 perror("'malloc()' for 'buf1'"); 708 exit(EXIT_FAILURE); 709 } 710 711 buf2 = malloc(buf_size); 712 if (!buf2) { 713 perror("'malloc()' for 'buf2'"); 714 exit(EXIT_FAILURE); 715 } 716 717 memset(buf1, BUF_PATTERN_1, buf_size); 718 memset(buf2, BUF_PATTERN_2, buf_size); 719 720 send_buf(fd, buf1, buf_size, 0, buf_size); 721 722 send_buf(fd, buf2, buf_size, 0, buf_size); 723 724 close(fd); 725 } 726 727 static void test_seqpacket_invalid_rec_buffer_server(const struct test_opts *opts) 728 { 729 int fd; 730 unsigned char *broken_buf; 731 unsigned char *valid_buf; 732 int page_size = getpagesize(); 733 int buf_size = page_size * 3; 734 ssize_t res; 735 int prot = PROT_READ | PROT_WRITE; 736 int flags = MAP_PRIVATE | MAP_ANONYMOUS; 737 int i; 738 739 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 740 if (fd < 0) { 741 perror("accept"); 742 exit(EXIT_FAILURE); 743 } 744 745 /* Setup first buffer. */ 746 broken_buf = mmap(NULL, buf_size, prot, flags, -1, 0); 747 if (broken_buf == MAP_FAILED) { 748 perror("mmap for 'broken_buf'"); 749 exit(EXIT_FAILURE); 750 } 751 752 /* Unmap "hole" in buffer. */ 753 if (munmap(broken_buf + page_size, page_size)) { 754 perror("'broken_buf' setup"); 755 exit(EXIT_FAILURE); 756 } 757 758 valid_buf = mmap(NULL, buf_size, prot, flags, -1, 0); 759 if (valid_buf == MAP_FAILED) { 760 perror("mmap for 'valid_buf'"); 761 exit(EXIT_FAILURE); 762 } 763 764 /* Try to fill buffer with unmapped middle. */ 765 res = read(fd, broken_buf, buf_size); 766 if (res != -1) { 767 fprintf(stderr, 768 "expected 'broken_buf' read(2) failure, got %zi\n", 769 res); 770 exit(EXIT_FAILURE); 771 } 772 773 if (errno != EFAULT) { 774 perror("unexpected errno of 'broken_buf'"); 775 exit(EXIT_FAILURE); 776 } 777 778 /* Try to fill valid buffer. */ 779 res = read(fd, valid_buf, buf_size); 780 if (res < 0) { 781 perror("unexpected 'valid_buf' read(2) failure"); 782 exit(EXIT_FAILURE); 783 } 784 785 if (res != buf_size) { 786 fprintf(stderr, 787 "invalid 'valid_buf' read(2), expected %i, got %zi\n", 788 buf_size, res); 789 exit(EXIT_FAILURE); 790 } 791 792 for (i = 0; i < buf_size; i++) { 793 if (valid_buf[i] != BUF_PATTERN_2) { 794 fprintf(stderr, 795 "invalid pattern for 'valid_buf' at %i, expected %hhX, got %hhX\n", 796 i, BUF_PATTERN_2, valid_buf[i]); 797 exit(EXIT_FAILURE); 798 } 799 } 800 801 /* Unmap buffers. */ 802 munmap(broken_buf, page_size); 803 munmap(broken_buf + page_size * 2, page_size); 804 munmap(valid_buf, buf_size); 805 close(fd); 806 } 807 808 #define RCVLOWAT_BUF_SIZE 128 809 810 static void test_stream_poll_rcvlowat_server(const struct test_opts *opts) 811 { 812 int fd; 813 int i; 814 815 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 816 if (fd < 0) { 817 perror("accept"); 818 exit(EXIT_FAILURE); 819 } 820 821 /* Send 1 byte. */ 822 send_byte(fd, 1, 0); 823 824 control_writeln("SRVSENT"); 825 826 /* Wait until client is ready to receive rest of data. */ 827 control_expectln("CLNSENT"); 828 829 for (i = 0; i < RCVLOWAT_BUF_SIZE - 1; i++) 830 send_byte(fd, 1, 0); 831 832 /* Keep socket in active state. */ 833 control_expectln("POLLDONE"); 834 835 close(fd); 836 } 837 838 static void test_stream_poll_rcvlowat_client(const struct test_opts *opts) 839 { 840 int lowat_val = RCVLOWAT_BUF_SIZE; 841 char buf[RCVLOWAT_BUF_SIZE]; 842 struct pollfd fds; 843 short poll_flags; 844 int fd; 845 846 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 847 if (fd < 0) { 848 perror("connect"); 849 exit(EXIT_FAILURE); 850 } 851 852 setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT, 853 lowat_val, "setsockopt(SO_RCVLOWAT)"); 854 855 control_expectln("SRVSENT"); 856 857 /* At this point, server sent 1 byte. */ 858 fds.fd = fd; 859 poll_flags = POLLIN | POLLRDNORM; 860 fds.events = poll_flags; 861 862 /* Try to wait for 1 sec. */ 863 if (poll(&fds, 1, 1000) < 0) { 864 perror("poll"); 865 exit(EXIT_FAILURE); 866 } 867 868 /* poll() must return nothing. */ 869 if (fds.revents) { 870 fprintf(stderr, "Unexpected poll result %hx\n", 871 fds.revents); 872 exit(EXIT_FAILURE); 873 } 874 875 /* Tell server to send rest of data. */ 876 control_writeln("CLNSENT"); 877 878 /* Poll for data. */ 879 if (poll(&fds, 1, 10000) < 0) { 880 perror("poll"); 881 exit(EXIT_FAILURE); 882 } 883 884 /* Only these two bits are expected. */ 885 if (fds.revents != poll_flags) { 886 fprintf(stderr, "Unexpected poll result %hx\n", 887 fds.revents); 888 exit(EXIT_FAILURE); 889 } 890 891 /* Use MSG_DONTWAIT, if call is going to wait, EAGAIN 892 * will be returned. 893 */ 894 recv_buf(fd, buf, sizeof(buf), MSG_DONTWAIT, RCVLOWAT_BUF_SIZE); 895 896 control_writeln("POLLDONE"); 897 898 close(fd); 899 } 900 901 #define INV_BUF_TEST_DATA_LEN 512 902 903 static void test_inv_buf_client(const struct test_opts *opts, bool stream) 904 { 905 unsigned char data[INV_BUF_TEST_DATA_LEN] = {0}; 906 ssize_t expected_ret; 907 int fd; 908 909 if (stream) 910 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 911 else 912 fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port); 913 914 if (fd < 0) { 915 perror("connect"); 916 exit(EXIT_FAILURE); 917 } 918 919 control_expectln("SENDDONE"); 920 921 /* Use invalid buffer here. */ 922 recv_buf(fd, NULL, sizeof(data), 0, -EFAULT); 923 924 if (stream) { 925 /* For SOCK_STREAM we must continue reading. */ 926 expected_ret = sizeof(data); 927 } else { 928 /* For SOCK_SEQPACKET socket's queue must be empty. */ 929 expected_ret = -EAGAIN; 930 } 931 932 recv_buf(fd, data, sizeof(data), MSG_DONTWAIT, expected_ret); 933 934 control_writeln("DONE"); 935 936 close(fd); 937 } 938 939 static void test_inv_buf_server(const struct test_opts *opts, bool stream) 940 { 941 unsigned char data[INV_BUF_TEST_DATA_LEN] = {0}; 942 int fd; 943 944 if (stream) 945 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 946 else 947 fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 948 949 if (fd < 0) { 950 perror("accept"); 951 exit(EXIT_FAILURE); 952 } 953 954 send_buf(fd, data, sizeof(data), 0, sizeof(data)); 955 956 control_writeln("SENDDONE"); 957 958 control_expectln("DONE"); 959 960 close(fd); 961 } 962 963 static void test_stream_inv_buf_client(const struct test_opts *opts) 964 { 965 test_inv_buf_client(opts, true); 966 } 967 968 static void test_stream_inv_buf_server(const struct test_opts *opts) 969 { 970 test_inv_buf_server(opts, true); 971 } 972 973 static void test_seqpacket_inv_buf_client(const struct test_opts *opts) 974 { 975 test_inv_buf_client(opts, false); 976 } 977 978 static void test_seqpacket_inv_buf_server(const struct test_opts *opts) 979 { 980 test_inv_buf_server(opts, false); 981 } 982 983 #define HELLO_STR "HELLO" 984 #define WORLD_STR "WORLD" 985 986 static void test_stream_virtio_skb_merge_client(const struct test_opts *opts) 987 { 988 int fd; 989 990 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 991 if (fd < 0) { 992 perror("connect"); 993 exit(EXIT_FAILURE); 994 } 995 996 /* Send first skbuff. */ 997 send_buf(fd, HELLO_STR, strlen(HELLO_STR), 0, strlen(HELLO_STR)); 998 999 control_writeln("SEND0"); 1000 /* Peer reads part of first skbuff. */ 1001 control_expectln("REPLY0"); 1002 1003 /* Send second skbuff, it will be appended to the first. */ 1004 send_buf(fd, WORLD_STR, strlen(WORLD_STR), 0, strlen(WORLD_STR)); 1005 1006 control_writeln("SEND1"); 1007 /* Peer reads merged skbuff packet. */ 1008 control_expectln("REPLY1"); 1009 1010 close(fd); 1011 } 1012 1013 static void test_stream_virtio_skb_merge_server(const struct test_opts *opts) 1014 { 1015 size_t read = 0, to_read; 1016 unsigned char buf[64]; 1017 int fd; 1018 1019 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1020 if (fd < 0) { 1021 perror("accept"); 1022 exit(EXIT_FAILURE); 1023 } 1024 1025 control_expectln("SEND0"); 1026 1027 /* Read skbuff partially. */ 1028 to_read = 2; 1029 recv_buf(fd, buf + read, to_read, 0, to_read); 1030 read += to_read; 1031 1032 control_writeln("REPLY0"); 1033 control_expectln("SEND1"); 1034 1035 /* Read the rest of both buffers */ 1036 to_read = strlen(HELLO_STR WORLD_STR) - read; 1037 recv_buf(fd, buf + read, to_read, 0, to_read); 1038 read += to_read; 1039 1040 /* No more bytes should be there */ 1041 to_read = sizeof(buf) - read; 1042 recv_buf(fd, buf + read, to_read, MSG_DONTWAIT, -EAGAIN); 1043 1044 if (memcmp(buf, HELLO_STR WORLD_STR, strlen(HELLO_STR WORLD_STR))) { 1045 fprintf(stderr, "pattern mismatch\n"); 1046 exit(EXIT_FAILURE); 1047 } 1048 1049 control_writeln("REPLY1"); 1050 1051 close(fd); 1052 } 1053 1054 static void test_seqpacket_msg_peek_client(const struct test_opts *opts) 1055 { 1056 return test_msg_peek_client(opts, true); 1057 } 1058 1059 static void test_seqpacket_msg_peek_server(const struct test_opts *opts) 1060 { 1061 return test_msg_peek_server(opts, true); 1062 } 1063 1064 static sig_atomic_t have_sigpipe; 1065 1066 static void sigpipe(int signo) 1067 { 1068 have_sigpipe = 1; 1069 } 1070 1071 static void test_stream_check_sigpipe(int fd) 1072 { 1073 ssize_t res; 1074 1075 have_sigpipe = 0; 1076 1077 res = send(fd, "A", 1, 0); 1078 if (res != -1) { 1079 fprintf(stderr, "expected send(2) failure, got %zi\n", res); 1080 exit(EXIT_FAILURE); 1081 } 1082 1083 if (!have_sigpipe) { 1084 fprintf(stderr, "SIGPIPE expected\n"); 1085 exit(EXIT_FAILURE); 1086 } 1087 1088 have_sigpipe = 0; 1089 1090 res = send(fd, "A", 1, MSG_NOSIGNAL); 1091 if (res != -1) { 1092 fprintf(stderr, "expected send(2) failure, got %zi\n", res); 1093 exit(EXIT_FAILURE); 1094 } 1095 1096 if (have_sigpipe) { 1097 fprintf(stderr, "SIGPIPE not expected\n"); 1098 exit(EXIT_FAILURE); 1099 } 1100 } 1101 1102 static void test_stream_shutwr_client(const struct test_opts *opts) 1103 { 1104 int fd; 1105 1106 struct sigaction act = { 1107 .sa_handler = sigpipe, 1108 }; 1109 1110 sigaction(SIGPIPE, &act, NULL); 1111 1112 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 1113 if (fd < 0) { 1114 perror("connect"); 1115 exit(EXIT_FAILURE); 1116 } 1117 1118 if (shutdown(fd, SHUT_WR)) { 1119 perror("shutdown"); 1120 exit(EXIT_FAILURE); 1121 } 1122 1123 test_stream_check_sigpipe(fd); 1124 1125 control_writeln("CLIENTDONE"); 1126 1127 close(fd); 1128 } 1129 1130 static void test_stream_shutwr_server(const struct test_opts *opts) 1131 { 1132 int fd; 1133 1134 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1135 if (fd < 0) { 1136 perror("accept"); 1137 exit(EXIT_FAILURE); 1138 } 1139 1140 control_expectln("CLIENTDONE"); 1141 1142 close(fd); 1143 } 1144 1145 static void test_stream_shutrd_client(const struct test_opts *opts) 1146 { 1147 int fd; 1148 1149 struct sigaction act = { 1150 .sa_handler = sigpipe, 1151 }; 1152 1153 sigaction(SIGPIPE, &act, NULL); 1154 1155 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 1156 if (fd < 0) { 1157 perror("connect"); 1158 exit(EXIT_FAILURE); 1159 } 1160 1161 control_expectln("SHUTRDDONE"); 1162 1163 test_stream_check_sigpipe(fd); 1164 1165 control_writeln("CLIENTDONE"); 1166 1167 close(fd); 1168 } 1169 1170 static void test_stream_shutrd_server(const struct test_opts *opts) 1171 { 1172 int fd; 1173 1174 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1175 if (fd < 0) { 1176 perror("accept"); 1177 exit(EXIT_FAILURE); 1178 } 1179 1180 if (shutdown(fd, SHUT_RD)) { 1181 perror("shutdown"); 1182 exit(EXIT_FAILURE); 1183 } 1184 1185 control_writeln("SHUTRDDONE"); 1186 control_expectln("CLIENTDONE"); 1187 1188 close(fd); 1189 } 1190 1191 static void test_double_bind_connect_server(const struct test_opts *opts) 1192 { 1193 int listen_fd, client_fd, i; 1194 struct sockaddr_vm sa_client; 1195 socklen_t socklen_client = sizeof(sa_client); 1196 1197 listen_fd = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port); 1198 1199 for (i = 0; i < 2; i++) { 1200 control_writeln("LISTENING"); 1201 1202 timeout_begin(TIMEOUT); 1203 do { 1204 client_fd = accept(listen_fd, (struct sockaddr *)&sa_client, 1205 &socklen_client); 1206 timeout_check("accept"); 1207 } while (client_fd < 0 && errno == EINTR); 1208 timeout_end(); 1209 1210 if (client_fd < 0) { 1211 perror("accept"); 1212 exit(EXIT_FAILURE); 1213 } 1214 1215 /* Waiting for remote peer to close connection */ 1216 vsock_wait_remote_close(client_fd); 1217 } 1218 1219 close(listen_fd); 1220 } 1221 1222 static void test_double_bind_connect_client(const struct test_opts *opts) 1223 { 1224 int i, client_fd; 1225 1226 for (i = 0; i < 2; i++) { 1227 /* Wait until server is ready to accept a new connection */ 1228 control_expectln("LISTENING"); 1229 1230 /* We use 'peer_port + 1' as "some" port for the 'bind()' 1231 * call. It is safe for overflow, but must be considered, 1232 * when running multiple test applications simultaneously 1233 * where 'peer-port' argument differs by 1. 1234 */ 1235 client_fd = vsock_bind_connect(opts->peer_cid, opts->peer_port, 1236 opts->peer_port + 1, SOCK_STREAM); 1237 1238 close(client_fd); 1239 } 1240 } 1241 1242 #define MSG_BUF_IOCTL_LEN 64 1243 static void test_unsent_bytes_server(const struct test_opts *opts, int type) 1244 { 1245 unsigned char buf[MSG_BUF_IOCTL_LEN]; 1246 int client_fd; 1247 1248 client_fd = vsock_accept(VMADDR_CID_ANY, opts->peer_port, NULL, type); 1249 if (client_fd < 0) { 1250 perror("accept"); 1251 exit(EXIT_FAILURE); 1252 } 1253 1254 recv_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf)); 1255 control_writeln("RECEIVED"); 1256 1257 close(client_fd); 1258 } 1259 1260 static void test_unsent_bytes_client(const struct test_opts *opts, int type) 1261 { 1262 unsigned char buf[MSG_BUF_IOCTL_LEN]; 1263 int ret, fd, sock_bytes_unsent; 1264 1265 fd = vsock_connect(opts->peer_cid, opts->peer_port, type); 1266 if (fd < 0) { 1267 perror("connect"); 1268 exit(EXIT_FAILURE); 1269 } 1270 1271 for (int i = 0; i < sizeof(buf); i++) 1272 buf[i] = rand() & 0xFF; 1273 1274 send_buf(fd, buf, sizeof(buf), 0, sizeof(buf)); 1275 control_expectln("RECEIVED"); 1276 1277 ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent); 1278 if (ret < 0) { 1279 if (errno == EOPNOTSUPP) { 1280 fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n"); 1281 } else { 1282 perror("ioctl"); 1283 exit(EXIT_FAILURE); 1284 } 1285 } else if (ret == 0 && sock_bytes_unsent != 0) { 1286 fprintf(stderr, 1287 "Unexpected 'SIOCOUTQ' value, expected 0, got %i\n", 1288 sock_bytes_unsent); 1289 exit(EXIT_FAILURE); 1290 } 1291 1292 close(fd); 1293 } 1294 1295 static void test_stream_unsent_bytes_client(const struct test_opts *opts) 1296 { 1297 test_unsent_bytes_client(opts, SOCK_STREAM); 1298 } 1299 1300 static void test_stream_unsent_bytes_server(const struct test_opts *opts) 1301 { 1302 test_unsent_bytes_server(opts, SOCK_STREAM); 1303 } 1304 1305 static void test_seqpacket_unsent_bytes_client(const struct test_opts *opts) 1306 { 1307 test_unsent_bytes_client(opts, SOCK_SEQPACKET); 1308 } 1309 1310 static void test_seqpacket_unsent_bytes_server(const struct test_opts *opts) 1311 { 1312 test_unsent_bytes_server(opts, SOCK_SEQPACKET); 1313 } 1314 1315 #define RCVLOWAT_CREDIT_UPD_BUF_SIZE (1024 * 128) 1316 /* This define is the same as in 'include/linux/virtio_vsock.h': 1317 * it is used to decide when to send credit update message during 1318 * reading from rx queue of a socket. Value and its usage in 1319 * kernel is important for this test. 1320 */ 1321 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (1024 * 64) 1322 1323 static void test_stream_rcvlowat_def_cred_upd_client(const struct test_opts *opts) 1324 { 1325 size_t buf_size; 1326 void *buf; 1327 int fd; 1328 1329 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); 1330 if (fd < 0) { 1331 perror("connect"); 1332 exit(EXIT_FAILURE); 1333 } 1334 1335 /* Send 1 byte more than peer's buffer size. */ 1336 buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE + 1; 1337 1338 buf = malloc(buf_size); 1339 if (!buf) { 1340 perror("malloc"); 1341 exit(EXIT_FAILURE); 1342 } 1343 1344 /* Wait until peer sets needed buffer size. */ 1345 recv_byte(fd, 1, 0); 1346 1347 if (send(fd, buf, buf_size, 0) != buf_size) { 1348 perror("send failed"); 1349 exit(EXIT_FAILURE); 1350 } 1351 1352 free(buf); 1353 close(fd); 1354 } 1355 1356 static void test_stream_credit_update_test(const struct test_opts *opts, 1357 bool low_rx_bytes_test) 1358 { 1359 int recv_buf_size; 1360 struct pollfd fds; 1361 size_t buf_size; 1362 unsigned long long sock_buf_size; 1363 void *buf; 1364 int fd; 1365 1366 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); 1367 if (fd < 0) { 1368 perror("accept"); 1369 exit(EXIT_FAILURE); 1370 } 1371 1372 buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE; 1373 1374 /* size_t can be < unsigned long long */ 1375 sock_buf_size = buf_size; 1376 1377 setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 1378 sock_buf_size, 1379 "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)"); 1380 1381 if (low_rx_bytes_test) { 1382 /* Set new SO_RCVLOWAT here. This enables sending credit 1383 * update when number of bytes if our rx queue become < 1384 * SO_RCVLOWAT value. 1385 */ 1386 recv_buf_size = 1 + VIRTIO_VSOCK_MAX_PKT_BUF_SIZE; 1387 1388 setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT, 1389 recv_buf_size, "setsockopt(SO_RCVLOWAT)"); 1390 } 1391 1392 /* Send one dummy byte here, because 'setsockopt()' above also 1393 * sends special packet which tells sender to update our buffer 1394 * size. This 'send_byte()' will serialize such packet with data 1395 * reads in a loop below. Sender starts transmission only when 1396 * it receives this single byte. 1397 */ 1398 send_byte(fd, 1, 0); 1399 1400 buf = malloc(buf_size); 1401 if (!buf) { 1402 perror("malloc"); 1403 exit(EXIT_FAILURE); 1404 } 1405 1406 /* Wait until there will be 128KB of data in rx queue. */ 1407 while (1) { 1408 ssize_t res; 1409 1410 res = recv(fd, buf, buf_size, MSG_PEEK); 1411 if (res == buf_size) 1412 break; 1413 1414 if (res <= 0) { 1415 fprintf(stderr, "unexpected 'recv()' return: %zi\n", res); 1416 exit(EXIT_FAILURE); 1417 } 1418 } 1419 1420 /* There is 128KB of data in the socket's rx queue, dequeue first 1421 * 64KB, credit update is sent if 'low_rx_bytes_test' == true. 1422 * Otherwise, credit update is sent in 'if (!low_rx_bytes_test)'. 1423 */ 1424 recv_buf_size = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE; 1425 recv_buf(fd, buf, recv_buf_size, 0, recv_buf_size); 1426 1427 if (!low_rx_bytes_test) { 1428 recv_buf_size++; 1429 1430 /* Updating SO_RCVLOWAT will send credit update. */ 1431 setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT, 1432 recv_buf_size, "setsockopt(SO_RCVLOWAT)"); 1433 } 1434 1435 fds.fd = fd; 1436 fds.events = POLLIN | POLLRDNORM | POLLERR | 1437 POLLRDHUP | POLLHUP; 1438 1439 /* This 'poll()' will return once we receive last byte 1440 * sent by client. 1441 */ 1442 if (poll(&fds, 1, -1) < 0) { 1443 perror("poll"); 1444 exit(EXIT_FAILURE); 1445 } 1446 1447 if (fds.revents & POLLERR) { 1448 fprintf(stderr, "'poll()' error\n"); 1449 exit(EXIT_FAILURE); 1450 } 1451 1452 if (fds.revents & (POLLIN | POLLRDNORM)) { 1453 recv_buf(fd, buf, recv_buf_size, MSG_DONTWAIT, recv_buf_size); 1454 } else { 1455 /* These flags must be set, as there is at 1456 * least 64KB of data ready to read. 1457 */ 1458 fprintf(stderr, "POLLIN | POLLRDNORM expected\n"); 1459 exit(EXIT_FAILURE); 1460 } 1461 1462 free(buf); 1463 close(fd); 1464 } 1465 1466 static void test_stream_cred_upd_on_low_rx_bytes(const struct test_opts *opts) 1467 { 1468 test_stream_credit_update_test(opts, true); 1469 } 1470 1471 static void test_stream_cred_upd_on_set_rcvlowat(const struct test_opts *opts) 1472 { 1473 test_stream_credit_update_test(opts, false); 1474 } 1475 1476 static struct test_case test_cases[] = { 1477 { 1478 .name = "SOCK_STREAM connection reset", 1479 .run_client = test_stream_connection_reset, 1480 }, 1481 { 1482 .name = "SOCK_STREAM bind only", 1483 .run_client = test_stream_bind_only_client, 1484 .run_server = test_stream_bind_only_server, 1485 }, 1486 { 1487 .name = "SOCK_STREAM client close", 1488 .run_client = test_stream_client_close_client, 1489 .run_server = test_stream_client_close_server, 1490 }, 1491 { 1492 .name = "SOCK_STREAM server close", 1493 .run_client = test_stream_server_close_client, 1494 .run_server = test_stream_server_close_server, 1495 }, 1496 { 1497 .name = "SOCK_STREAM multiple connections", 1498 .run_client = test_stream_multiconn_client, 1499 .run_server = test_stream_multiconn_server, 1500 }, 1501 { 1502 .name = "SOCK_STREAM MSG_PEEK", 1503 .run_client = test_stream_msg_peek_client, 1504 .run_server = test_stream_msg_peek_server, 1505 }, 1506 { 1507 .name = "SOCK_SEQPACKET msg bounds", 1508 .run_client = test_seqpacket_msg_bounds_client, 1509 .run_server = test_seqpacket_msg_bounds_server, 1510 }, 1511 { 1512 .name = "SOCK_SEQPACKET MSG_TRUNC flag", 1513 .run_client = test_seqpacket_msg_trunc_client, 1514 .run_server = test_seqpacket_msg_trunc_server, 1515 }, 1516 { 1517 .name = "SOCK_SEQPACKET timeout", 1518 .run_client = test_seqpacket_timeout_client, 1519 .run_server = test_seqpacket_timeout_server, 1520 }, 1521 { 1522 .name = "SOCK_SEQPACKET invalid receive buffer", 1523 .run_client = test_seqpacket_invalid_rec_buffer_client, 1524 .run_server = test_seqpacket_invalid_rec_buffer_server, 1525 }, 1526 { 1527 .name = "SOCK_STREAM poll() + SO_RCVLOWAT", 1528 .run_client = test_stream_poll_rcvlowat_client, 1529 .run_server = test_stream_poll_rcvlowat_server, 1530 }, 1531 { 1532 .name = "SOCK_SEQPACKET big message", 1533 .run_client = test_seqpacket_bigmsg_client, 1534 .run_server = test_seqpacket_bigmsg_server, 1535 }, 1536 { 1537 .name = "SOCK_STREAM test invalid buffer", 1538 .run_client = test_stream_inv_buf_client, 1539 .run_server = test_stream_inv_buf_server, 1540 }, 1541 { 1542 .name = "SOCK_SEQPACKET test invalid buffer", 1543 .run_client = test_seqpacket_inv_buf_client, 1544 .run_server = test_seqpacket_inv_buf_server, 1545 }, 1546 { 1547 .name = "SOCK_STREAM virtio skb merge", 1548 .run_client = test_stream_virtio_skb_merge_client, 1549 .run_server = test_stream_virtio_skb_merge_server, 1550 }, 1551 { 1552 .name = "SOCK_SEQPACKET MSG_PEEK", 1553 .run_client = test_seqpacket_msg_peek_client, 1554 .run_server = test_seqpacket_msg_peek_server, 1555 }, 1556 { 1557 .name = "SOCK_STREAM SHUT_WR", 1558 .run_client = test_stream_shutwr_client, 1559 .run_server = test_stream_shutwr_server, 1560 }, 1561 { 1562 .name = "SOCK_STREAM SHUT_RD", 1563 .run_client = test_stream_shutrd_client, 1564 .run_server = test_stream_shutrd_server, 1565 }, 1566 { 1567 .name = "SOCK_STREAM MSG_ZEROCOPY", 1568 .run_client = test_stream_msgzcopy_client, 1569 .run_server = test_stream_msgzcopy_server, 1570 }, 1571 { 1572 .name = "SOCK_SEQPACKET MSG_ZEROCOPY", 1573 .run_client = test_seqpacket_msgzcopy_client, 1574 .run_server = test_seqpacket_msgzcopy_server, 1575 }, 1576 { 1577 .name = "SOCK_STREAM MSG_ZEROCOPY empty MSG_ERRQUEUE", 1578 .run_client = test_stream_msgzcopy_empty_errq_client, 1579 .run_server = test_stream_msgzcopy_empty_errq_server, 1580 }, 1581 { 1582 .name = "SOCK_STREAM double bind connect", 1583 .run_client = test_double_bind_connect_client, 1584 .run_server = test_double_bind_connect_server, 1585 }, 1586 { 1587 .name = "SOCK_STREAM virtio credit update + SO_RCVLOWAT", 1588 .run_client = test_stream_rcvlowat_def_cred_upd_client, 1589 .run_server = test_stream_cred_upd_on_set_rcvlowat, 1590 }, 1591 { 1592 .name = "SOCK_STREAM virtio credit update + low rx_bytes", 1593 .run_client = test_stream_rcvlowat_def_cred_upd_client, 1594 .run_server = test_stream_cred_upd_on_low_rx_bytes, 1595 }, 1596 { 1597 .name = "SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes", 1598 .run_client = test_stream_unsent_bytes_client, 1599 .run_server = test_stream_unsent_bytes_server, 1600 }, 1601 { 1602 .name = "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes", 1603 .run_client = test_seqpacket_unsent_bytes_client, 1604 .run_server = test_seqpacket_unsent_bytes_server, 1605 }, 1606 {}, 1607 }; 1608 1609 static const char optstring[] = ""; 1610 static const struct option longopts[] = { 1611 { 1612 .name = "control-host", 1613 .has_arg = required_argument, 1614 .val = 'H', 1615 }, 1616 { 1617 .name = "control-port", 1618 .has_arg = required_argument, 1619 .val = 'P', 1620 }, 1621 { 1622 .name = "mode", 1623 .has_arg = required_argument, 1624 .val = 'm', 1625 }, 1626 { 1627 .name = "peer-cid", 1628 .has_arg = required_argument, 1629 .val = 'p', 1630 }, 1631 { 1632 .name = "peer-port", 1633 .has_arg = required_argument, 1634 .val = 'q', 1635 }, 1636 { 1637 .name = "list", 1638 .has_arg = no_argument, 1639 .val = 'l', 1640 }, 1641 { 1642 .name = "skip", 1643 .has_arg = required_argument, 1644 .val = 's', 1645 }, 1646 { 1647 .name = "help", 1648 .has_arg = no_argument, 1649 .val = '?', 1650 }, 1651 {}, 1652 }; 1653 1654 static void usage(void) 1655 { 1656 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" 1657 "\n" 1658 " Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n" 1659 " Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n" 1660 "\n" 1661 "Run vsock.ko tests. Must be launched in both guest\n" 1662 "and host. One side must use --mode=client and\n" 1663 "the other side must use --mode=server.\n" 1664 "\n" 1665 "A TCP control socket connection is used to coordinate tests\n" 1666 "between the client and the server. The server requires a\n" 1667 "listen address and the client requires an address to\n" 1668 "connect to.\n" 1669 "\n" 1670 "The CID of the other side must be given with --peer-cid=<cid>.\n" 1671 "During the test, two AF_VSOCK ports will be used: the port\n" 1672 "specified with --peer-port=<port> (or the default port)\n" 1673 "and the next one.\n" 1674 "\n" 1675 "Options:\n" 1676 " --help This help message\n" 1677 " --control-host <host> Server IP address to connect to\n" 1678 " --control-port <port> Server port to listen on/connect to\n" 1679 " --mode client|server Server or client mode\n" 1680 " --peer-cid <cid> CID of the other side\n" 1681 " --peer-port <port> AF_VSOCK port used for the test [default: %d]\n" 1682 " --list List of tests that will be executed\n" 1683 " --skip <test_id> Test ID to skip;\n" 1684 " use multiple --skip options to skip more tests\n", 1685 DEFAULT_PEER_PORT 1686 ); 1687 exit(EXIT_FAILURE); 1688 } 1689 1690 int main(int argc, char **argv) 1691 { 1692 const char *control_host = NULL; 1693 const char *control_port = NULL; 1694 struct test_opts opts = { 1695 .mode = TEST_MODE_UNSET, 1696 .peer_cid = VMADDR_CID_ANY, 1697 .peer_port = DEFAULT_PEER_PORT, 1698 }; 1699 1700 srand(time(NULL)); 1701 init_signals(); 1702 1703 for (;;) { 1704 int opt = getopt_long(argc, argv, optstring, longopts, NULL); 1705 1706 if (opt == -1) 1707 break; 1708 1709 switch (opt) { 1710 case 'H': 1711 control_host = optarg; 1712 break; 1713 case 'm': 1714 if (strcmp(optarg, "client") == 0) 1715 opts.mode = TEST_MODE_CLIENT; 1716 else if (strcmp(optarg, "server") == 0) 1717 opts.mode = TEST_MODE_SERVER; 1718 else { 1719 fprintf(stderr, "--mode must be \"client\" or \"server\"\n"); 1720 return EXIT_FAILURE; 1721 } 1722 break; 1723 case 'p': 1724 opts.peer_cid = parse_cid(optarg); 1725 break; 1726 case 'q': 1727 opts.peer_port = parse_port(optarg); 1728 break; 1729 case 'P': 1730 control_port = optarg; 1731 break; 1732 case 'l': 1733 list_tests(test_cases); 1734 break; 1735 case 's': 1736 skip_test(test_cases, ARRAY_SIZE(test_cases) - 1, 1737 optarg); 1738 break; 1739 case '?': 1740 default: 1741 usage(); 1742 } 1743 } 1744 1745 if (!control_port) 1746 usage(); 1747 if (opts.mode == TEST_MODE_UNSET) 1748 usage(); 1749 if (opts.peer_cid == VMADDR_CID_ANY) 1750 usage(); 1751 1752 if (!control_host) { 1753 if (opts.mode != TEST_MODE_SERVER) 1754 usage(); 1755 control_host = "0.0.0.0"; 1756 } 1757 1758 control_init(control_host, control_port, 1759 opts.mode == TEST_MODE_SERVER); 1760 1761 run_tests(test_cases, &opts); 1762 1763 control_cleanup(); 1764 return EXIT_SUCCESS; 1765 } 1766