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