1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * vsock test utilities 4 * 5 * Copyright (C) 2017 Red Hat, Inc. 6 * 7 * Author: Stefan Hajnoczi <stefanha@redhat.com> 8 */ 9 10 #include <errno.h> 11 #include <stdio.h> 12 #include <stdint.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include <signal.h> 16 #include <unistd.h> 17 #include <assert.h> 18 #include <sys/epoll.h> 19 #include <sys/mman.h> 20 #include <linux/sockios.h> 21 22 #include "timeout.h" 23 #include "control.h" 24 #include "util.h" 25 26 /* Install signal handlers */ 27 void init_signals(void) 28 { 29 struct sigaction act = { 30 .sa_handler = sigalrm, 31 }; 32 33 sigaction(SIGALRM, &act, NULL); 34 signal(SIGPIPE, SIG_IGN); 35 } 36 37 static unsigned int parse_uint(const char *str, const char *err_str) 38 { 39 char *endptr = NULL; 40 unsigned long n; 41 42 errno = 0; 43 n = strtoul(str, &endptr, 10); 44 if (errno || *endptr != '\0') { 45 fprintf(stderr, "malformed %s \"%s\"\n", err_str, str); 46 exit(EXIT_FAILURE); 47 } 48 return n; 49 } 50 51 /* Parse a CID in string representation */ 52 unsigned int parse_cid(const char *str) 53 { 54 return parse_uint(str, "CID"); 55 } 56 57 /* Parse a port in string representation */ 58 unsigned int parse_port(const char *str) 59 { 60 return parse_uint(str, "port"); 61 } 62 63 /* Wait for the remote to close the connection */ 64 void vsock_wait_remote_close(int fd) 65 { 66 struct epoll_event ev; 67 int epollfd, nfds; 68 69 epollfd = epoll_create1(0); 70 if (epollfd == -1) { 71 perror("epoll_create1"); 72 exit(EXIT_FAILURE); 73 } 74 75 ev.events = EPOLLRDHUP | EPOLLHUP; 76 ev.data.fd = fd; 77 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) { 78 perror("epoll_ctl"); 79 exit(EXIT_FAILURE); 80 } 81 82 nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000); 83 if (nfds == -1) { 84 perror("epoll_wait"); 85 exit(EXIT_FAILURE); 86 } 87 88 if (nfds == 0) { 89 fprintf(stderr, "epoll_wait timed out\n"); 90 exit(EXIT_FAILURE); 91 } 92 93 assert(nfds == 1); 94 assert(ev.events & (EPOLLRDHUP | EPOLLHUP)); 95 assert(ev.data.fd == fd); 96 97 close(epollfd); 98 } 99 100 /* Wait until transport reports no data left to be sent. 101 * Return false if transport does not implement the unsent_bytes() callback. 102 */ 103 bool vsock_wait_sent(int fd) 104 { 105 int ret, sock_bytes_unsent; 106 107 timeout_begin(TIMEOUT); 108 do { 109 ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent); 110 if (ret < 0) { 111 if (errno == EOPNOTSUPP) 112 break; 113 114 perror("ioctl(SIOCOUTQ)"); 115 exit(EXIT_FAILURE); 116 } 117 timeout_check("SIOCOUTQ"); 118 } while (sock_bytes_unsent != 0); 119 timeout_end(); 120 121 return !ret; 122 } 123 124 /* Create socket <type>, bind to <cid, port>. 125 * Return the file descriptor, or -1 on error. 126 */ 127 int vsock_bind_try(unsigned int cid, unsigned int port, int type) 128 { 129 struct sockaddr_vm sa = { 130 .svm_family = AF_VSOCK, 131 .svm_cid = cid, 132 .svm_port = port, 133 }; 134 int fd, saved_errno; 135 136 fd = socket(AF_VSOCK, type, 0); 137 if (fd < 0) { 138 perror("socket"); 139 exit(EXIT_FAILURE); 140 } 141 142 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa))) { 143 saved_errno = errno; 144 close(fd); 145 errno = saved_errno; 146 fd = -1; 147 } 148 149 return fd; 150 } 151 152 /* Create socket <type>, bind to <cid, port> and return the file descriptor. */ 153 int vsock_bind(unsigned int cid, unsigned int port, int type) 154 { 155 int fd; 156 157 fd = vsock_bind_try(cid, port, type); 158 if (fd < 0) { 159 perror("bind"); 160 exit(EXIT_FAILURE); 161 } 162 163 return fd; 164 } 165 166 int vsock_connect_fd(int fd, unsigned int cid, unsigned int port) 167 { 168 struct sockaddr_vm sa = { 169 .svm_family = AF_VSOCK, 170 .svm_cid = cid, 171 .svm_port = port, 172 }; 173 int ret; 174 175 timeout_begin(TIMEOUT); 176 do { 177 ret = connect(fd, (struct sockaddr *)&sa, sizeof(sa)); 178 timeout_check("connect"); 179 } while (ret < 0 && errno == EINTR); 180 timeout_end(); 181 182 return ret; 183 } 184 185 /* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */ 186 int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type) 187 { 188 int client_fd; 189 190 client_fd = vsock_bind(VMADDR_CID_ANY, bind_port, type); 191 192 if (vsock_connect_fd(client_fd, cid, port)) { 193 perror("connect"); 194 exit(EXIT_FAILURE); 195 } 196 197 return client_fd; 198 } 199 200 /* Connect to <cid, port> and return the file descriptor. */ 201 int vsock_connect(unsigned int cid, unsigned int port, int type) 202 { 203 int fd; 204 205 control_expectln("LISTENING"); 206 207 fd = socket(AF_VSOCK, type, 0); 208 if (fd < 0) { 209 perror("socket"); 210 exit(EXIT_FAILURE); 211 } 212 213 if (vsock_connect_fd(fd, cid, port)) { 214 int old_errno = errno; 215 216 close(fd); 217 fd = -1; 218 errno = old_errno; 219 } 220 221 return fd; 222 } 223 224 int vsock_stream_connect(unsigned int cid, unsigned int port) 225 { 226 return vsock_connect(cid, port, SOCK_STREAM); 227 } 228 229 int vsock_seqpacket_connect(unsigned int cid, unsigned int port) 230 { 231 return vsock_connect(cid, port, SOCK_SEQPACKET); 232 } 233 234 /* Listen on <cid, port> and return the file descriptor. */ 235 static int vsock_listen(unsigned int cid, unsigned int port, int type) 236 { 237 int fd; 238 239 fd = vsock_bind(cid, port, type); 240 241 if (listen(fd, 1) < 0) { 242 perror("listen"); 243 exit(EXIT_FAILURE); 244 } 245 246 return fd; 247 } 248 249 /* Listen on <cid, port> and return the first incoming connection. The remote 250 * address is stored to clientaddrp. clientaddrp may be NULL. 251 */ 252 int vsock_accept(unsigned int cid, unsigned int port, 253 struct sockaddr_vm *clientaddrp, int type) 254 { 255 union { 256 struct sockaddr sa; 257 struct sockaddr_vm svm; 258 } clientaddr; 259 socklen_t clientaddr_len = sizeof(clientaddr.svm); 260 int fd, client_fd, old_errno; 261 262 fd = vsock_listen(cid, port, type); 263 264 control_writeln("LISTENING"); 265 266 timeout_begin(TIMEOUT); 267 do { 268 client_fd = accept(fd, &clientaddr.sa, &clientaddr_len); 269 timeout_check("accept"); 270 } while (client_fd < 0 && errno == EINTR); 271 timeout_end(); 272 273 old_errno = errno; 274 close(fd); 275 errno = old_errno; 276 277 if (client_fd < 0) 278 return client_fd; 279 280 if (clientaddr_len != sizeof(clientaddr.svm)) { 281 fprintf(stderr, "unexpected addrlen from accept(2), %zu\n", 282 (size_t)clientaddr_len); 283 exit(EXIT_FAILURE); 284 } 285 if (clientaddr.sa.sa_family != AF_VSOCK) { 286 fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n", 287 clientaddr.sa.sa_family); 288 exit(EXIT_FAILURE); 289 } 290 291 if (clientaddrp) 292 *clientaddrp = clientaddr.svm; 293 return client_fd; 294 } 295 296 int vsock_stream_accept(unsigned int cid, unsigned int port, 297 struct sockaddr_vm *clientaddrp) 298 { 299 return vsock_accept(cid, port, clientaddrp, SOCK_STREAM); 300 } 301 302 int vsock_stream_listen(unsigned int cid, unsigned int port) 303 { 304 return vsock_listen(cid, port, SOCK_STREAM); 305 } 306 307 int vsock_seqpacket_accept(unsigned int cid, unsigned int port, 308 struct sockaddr_vm *clientaddrp) 309 { 310 return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET); 311 } 312 313 /* Transmit bytes from a buffer and check the return value. 314 * 315 * expected_ret: 316 * <0 Negative errno (for testing errors) 317 * 0 End-of-file 318 * >0 Success (bytes successfully written) 319 */ 320 void send_buf(int fd, const void *buf, size_t len, int flags, 321 ssize_t expected_ret) 322 { 323 ssize_t nwritten = 0; 324 ssize_t ret; 325 326 timeout_begin(TIMEOUT); 327 do { 328 ret = send(fd, buf + nwritten, len - nwritten, flags); 329 timeout_check("send"); 330 331 if (ret == 0 || (ret < 0 && errno != EINTR)) 332 break; 333 334 nwritten += ret; 335 } while (nwritten < len); 336 timeout_end(); 337 338 if (expected_ret < 0) { 339 if (ret != -1) { 340 fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n", 341 ret, expected_ret); 342 exit(EXIT_FAILURE); 343 } 344 if (errno != -expected_ret) { 345 perror("send"); 346 exit(EXIT_FAILURE); 347 } 348 return; 349 } 350 351 if (ret < 0) { 352 perror("send"); 353 exit(EXIT_FAILURE); 354 } 355 356 if (nwritten != expected_ret) { 357 if (ret == 0) 358 fprintf(stderr, "unexpected EOF while sending bytes\n"); 359 360 fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n", 361 nwritten, expected_ret); 362 exit(EXIT_FAILURE); 363 } 364 } 365 366 /* Receive bytes in a buffer and check the return value. 367 * 368 * expected_ret: 369 * <0 Negative errno (for testing errors) 370 * 0 End-of-file 371 * >0 Success (bytes successfully read) 372 */ 373 void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret) 374 { 375 ssize_t nread = 0; 376 ssize_t ret; 377 378 timeout_begin(TIMEOUT); 379 do { 380 ret = recv(fd, buf + nread, len - nread, flags); 381 timeout_check("recv"); 382 383 if (ret == 0 || (ret < 0 && errno != EINTR)) 384 break; 385 386 nread += ret; 387 } while (nread < len); 388 timeout_end(); 389 390 if (expected_ret < 0) { 391 if (ret != -1) { 392 fprintf(stderr, "bogus recv(2) return value %zd (expected %zd)\n", 393 ret, expected_ret); 394 exit(EXIT_FAILURE); 395 } 396 if (errno != -expected_ret) { 397 perror("recv"); 398 exit(EXIT_FAILURE); 399 } 400 return; 401 } 402 403 if (ret < 0) { 404 perror("recv"); 405 exit(EXIT_FAILURE); 406 } 407 408 if (nread != expected_ret) { 409 if (ret == 0) 410 fprintf(stderr, "unexpected EOF while receiving bytes\n"); 411 412 fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n", 413 nread, expected_ret); 414 exit(EXIT_FAILURE); 415 } 416 } 417 418 /* Transmit one byte and check the return value. 419 * 420 * expected_ret: 421 * <0 Negative errno (for testing errors) 422 * 0 End-of-file 423 * 1 Success 424 */ 425 void send_byte(int fd, int expected_ret, int flags) 426 { 427 static const uint8_t byte = 'A'; 428 429 send_buf(fd, &byte, sizeof(byte), flags, expected_ret); 430 } 431 432 /* Receive one byte and check the return value. 433 * 434 * expected_ret: 435 * <0 Negative errno (for testing errors) 436 * 0 End-of-file 437 * 1 Success 438 */ 439 void recv_byte(int fd, int expected_ret, int flags) 440 { 441 uint8_t byte; 442 443 recv_buf(fd, &byte, sizeof(byte), flags, expected_ret); 444 445 if (byte != 'A') { 446 fprintf(stderr, "unexpected byte read 0x%02x\n", byte); 447 exit(EXIT_FAILURE); 448 } 449 } 450 451 /* Run test cases. The program terminates if a failure occurs. */ 452 void run_tests(const struct test_case *test_cases, 453 const struct test_opts *opts) 454 { 455 int i; 456 457 for (i = 0; test_cases[i].name; i++) { 458 void (*run)(const struct test_opts *opts); 459 char *line; 460 461 printf("%d - %s...", i, test_cases[i].name); 462 fflush(stdout); 463 464 /* Full barrier before executing the next test. This 465 * ensures that client and server are executing the 466 * same test case. In particular, it means whoever is 467 * faster will not see the peer still executing the 468 * last test. This is important because port numbers 469 * can be used by multiple test cases. 470 */ 471 if (test_cases[i].skip) 472 control_writeln("SKIP"); 473 else 474 control_writeln("NEXT"); 475 476 line = control_readln(); 477 if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) { 478 479 printf("skipped\n"); 480 481 free(line); 482 continue; 483 } 484 485 control_cmpln(line, "NEXT", true); 486 free(line); 487 488 if (opts->mode == TEST_MODE_CLIENT) 489 run = test_cases[i].run_client; 490 else 491 run = test_cases[i].run_server; 492 493 if (run) 494 run(opts); 495 496 printf("ok\n"); 497 } 498 } 499 500 void list_tests(const struct test_case *test_cases) 501 { 502 int i; 503 504 printf("ID\tTest name\n"); 505 506 for (i = 0; test_cases[i].name; i++) 507 printf("%d\t%s\n", i, test_cases[i].name); 508 509 exit(EXIT_FAILURE); 510 } 511 512 static unsigned long parse_test_id(const char *test_id_str, size_t test_cases_len) 513 { 514 unsigned long test_id; 515 char *endptr = NULL; 516 517 errno = 0; 518 test_id = strtoul(test_id_str, &endptr, 10); 519 if (errno || *endptr != '\0') { 520 fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str); 521 exit(EXIT_FAILURE); 522 } 523 524 if (test_id >= test_cases_len) { 525 fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n", 526 test_id, test_cases_len - 1); 527 exit(EXIT_FAILURE); 528 } 529 530 return test_id; 531 } 532 533 void skip_test(struct test_case *test_cases, size_t test_cases_len, 534 const char *test_id_str) 535 { 536 unsigned long test_id = parse_test_id(test_id_str, test_cases_len); 537 test_cases[test_id].skip = true; 538 } 539 540 void pick_test(struct test_case *test_cases, size_t test_cases_len, 541 const char *test_id_str) 542 { 543 static bool skip_all = true; 544 unsigned long test_id; 545 546 if (skip_all) { 547 unsigned long i; 548 549 for (i = 0; i < test_cases_len; ++i) 550 test_cases[i].skip = true; 551 552 skip_all = false; 553 } 554 555 test_id = parse_test_id(test_id_str, test_cases_len); 556 test_cases[test_id].skip = false; 557 } 558 559 unsigned long hash_djb2(const void *data, size_t len) 560 { 561 unsigned long hash = 5381; 562 int i = 0; 563 564 while (i < len) { 565 hash = ((hash << 5) + hash) + ((unsigned char *)data)[i]; 566 i++; 567 } 568 569 return hash; 570 } 571 572 size_t iovec_bytes(const struct iovec *iov, size_t iovnum) 573 { 574 size_t bytes; 575 int i; 576 577 for (bytes = 0, i = 0; i < iovnum; i++) 578 bytes += iov[i].iov_len; 579 580 return bytes; 581 } 582 583 unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum) 584 { 585 unsigned long hash; 586 size_t iov_bytes; 587 size_t offs; 588 void *tmp; 589 int i; 590 591 iov_bytes = iovec_bytes(iov, iovnum); 592 593 tmp = malloc(iov_bytes); 594 if (!tmp) { 595 perror("malloc"); 596 exit(EXIT_FAILURE); 597 } 598 599 for (offs = 0, i = 0; i < iovnum; i++) { 600 memcpy(tmp + offs, iov[i].iov_base, iov[i].iov_len); 601 offs += iov[i].iov_len; 602 } 603 604 hash = hash_djb2(tmp, iov_bytes); 605 free(tmp); 606 607 return hash; 608 } 609 610 /* Allocates and returns new 'struct iovec *' according pattern 611 * in the 'test_iovec'. For each element in the 'test_iovec' it 612 * allocates new element in the resulting 'iovec'. 'iov_len' 613 * of the new element is copied from 'test_iovec'. 'iov_base' is 614 * allocated depending on the 'iov_base' of 'test_iovec': 615 * 616 * 'iov_base' == NULL -> valid buf: mmap('iov_len'). 617 * 618 * 'iov_base' == MAP_FAILED -> invalid buf: 619 * mmap('iov_len'), then munmap('iov_len'). 620 * 'iov_base' still contains result of 621 * mmap(). 622 * 623 * 'iov_base' == number -> unaligned valid buf: 624 * mmap('iov_len') + number. 625 * 626 * 'iovnum' is number of elements in 'test_iovec'. 627 * 628 * Returns new 'iovec' or calls 'exit()' on error. 629 */ 630 struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum) 631 { 632 struct iovec *iovec; 633 int i; 634 635 iovec = malloc(sizeof(*iovec) * iovnum); 636 if (!iovec) { 637 perror("malloc"); 638 exit(EXIT_FAILURE); 639 } 640 641 for (i = 0; i < iovnum; i++) { 642 iovec[i].iov_len = test_iovec[i].iov_len; 643 644 iovec[i].iov_base = mmap(NULL, iovec[i].iov_len, 645 PROT_READ | PROT_WRITE, 646 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, 647 -1, 0); 648 if (iovec[i].iov_base == MAP_FAILED) { 649 perror("mmap"); 650 exit(EXIT_FAILURE); 651 } 652 653 if (test_iovec[i].iov_base != MAP_FAILED) 654 iovec[i].iov_base += (uintptr_t)test_iovec[i].iov_base; 655 } 656 657 /* Unmap "invalid" elements. */ 658 for (i = 0; i < iovnum; i++) { 659 if (test_iovec[i].iov_base == MAP_FAILED) { 660 if (munmap(iovec[i].iov_base, iovec[i].iov_len)) { 661 perror("munmap"); 662 exit(EXIT_FAILURE); 663 } 664 } 665 } 666 667 for (i = 0; i < iovnum; i++) { 668 int j; 669 670 if (test_iovec[i].iov_base == MAP_FAILED) 671 continue; 672 673 for (j = 0; j < iovec[i].iov_len; j++) 674 ((uint8_t *)iovec[i].iov_base)[j] = rand() & 0xff; 675 } 676 677 return iovec; 678 } 679 680 /* Frees 'iovec *', previously allocated by 'alloc_test_iovec()'. 681 * On error calls 'exit()'. 682 */ 683 void free_test_iovec(const struct iovec *test_iovec, 684 struct iovec *iovec, int iovnum) 685 { 686 int i; 687 688 for (i = 0; i < iovnum; i++) { 689 if (test_iovec[i].iov_base != MAP_FAILED) { 690 if (test_iovec[i].iov_base) 691 iovec[i].iov_base -= (uintptr_t)test_iovec[i].iov_base; 692 693 if (munmap(iovec[i].iov_base, iovec[i].iov_len)) { 694 perror("munmap"); 695 exit(EXIT_FAILURE); 696 } 697 } 698 } 699 700 free(iovec); 701 } 702 703 /* Set "unsigned long long" socket option and check that it's indeed set */ 704 void setsockopt_ull_check(int fd, int level, int optname, 705 unsigned long long val, char const *errmsg) 706 { 707 unsigned long long chkval; 708 socklen_t chklen; 709 int err; 710 711 err = setsockopt(fd, level, optname, &val, sizeof(val)); 712 if (err) { 713 fprintf(stderr, "setsockopt err: %s (%d)\n", 714 strerror(errno), errno); 715 goto fail; 716 } 717 718 chkval = ~val; /* just make storage != val */ 719 chklen = sizeof(chkval); 720 721 err = getsockopt(fd, level, optname, &chkval, &chklen); 722 if (err) { 723 fprintf(stderr, "getsockopt err: %s (%d)\n", 724 strerror(errno), errno); 725 goto fail; 726 } 727 728 if (chklen != sizeof(chkval)) { 729 fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val), 730 chklen); 731 goto fail; 732 } 733 734 if (chkval != val) { 735 fprintf(stderr, "value mismatch: set %llu got %llu\n", val, 736 chkval); 737 goto fail; 738 } 739 return; 740 fail: 741 fprintf(stderr, "%s val %llu\n", errmsg, val); 742 exit(EXIT_FAILURE); 743 ; 744 } 745 746 /* Set "int" socket option and check that it's indeed set */ 747 void setsockopt_int_check(int fd, int level, int optname, int val, 748 char const *errmsg) 749 { 750 int chkval; 751 socklen_t chklen; 752 int err; 753 754 err = setsockopt(fd, level, optname, &val, sizeof(val)); 755 if (err) { 756 fprintf(stderr, "setsockopt err: %s (%d)\n", 757 strerror(errno), errno); 758 goto fail; 759 } 760 761 chkval = ~val; /* just make storage != val */ 762 chklen = sizeof(chkval); 763 764 err = getsockopt(fd, level, optname, &chkval, &chklen); 765 if (err) { 766 fprintf(stderr, "getsockopt err: %s (%d)\n", 767 strerror(errno), errno); 768 goto fail; 769 } 770 771 if (chklen != sizeof(chkval)) { 772 fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val), 773 chklen); 774 goto fail; 775 } 776 777 if (chkval != val) { 778 fprintf(stderr, "value mismatch: set %d got %d\n", val, chkval); 779 goto fail; 780 } 781 return; 782 fail: 783 fprintf(stderr, "%s val %d\n", errmsg, val); 784 exit(EXIT_FAILURE); 785 } 786 787 static void mem_invert(unsigned char *mem, size_t size) 788 { 789 size_t i; 790 791 for (i = 0; i < size; i++) 792 mem[i] = ~mem[i]; 793 } 794 795 /* Set "timeval" socket option and check that it's indeed set */ 796 void setsockopt_timeval_check(int fd, int level, int optname, 797 struct timeval val, char const *errmsg) 798 { 799 struct timeval chkval; 800 socklen_t chklen; 801 int err; 802 803 err = setsockopt(fd, level, optname, &val, sizeof(val)); 804 if (err) { 805 fprintf(stderr, "setsockopt err: %s (%d)\n", 806 strerror(errno), errno); 807 goto fail; 808 } 809 810 /* just make storage != val */ 811 chkval = val; 812 mem_invert((unsigned char *)&chkval, sizeof(chkval)); 813 chklen = sizeof(chkval); 814 815 err = getsockopt(fd, level, optname, &chkval, &chklen); 816 if (err) { 817 fprintf(stderr, "getsockopt err: %s (%d)\n", 818 strerror(errno), errno); 819 goto fail; 820 } 821 822 if (chklen != sizeof(chkval)) { 823 fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val), 824 chklen); 825 goto fail; 826 } 827 828 if (memcmp(&chkval, &val, sizeof(val)) != 0) { 829 fprintf(stderr, "value mismatch: set %ld:%ld got %ld:%ld\n", 830 val.tv_sec, val.tv_usec, chkval.tv_sec, chkval.tv_usec); 831 goto fail; 832 } 833 return; 834 fail: 835 fprintf(stderr, "%s val %ld:%ld\n", errmsg, val.tv_sec, val.tv_usec); 836 exit(EXIT_FAILURE); 837 } 838 839 void enable_so_zerocopy_check(int fd) 840 { 841 setsockopt_int_check(fd, SOL_SOCKET, SO_ZEROCOPY, 1, 842 "setsockopt SO_ZEROCOPY"); 843 } 844 845 void enable_so_linger(int fd, int timeout) 846 { 847 struct linger optval = { 848 .l_onoff = 1, 849 .l_linger = timeout 850 }; 851 852 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) { 853 perror("setsockopt(SO_LINGER)"); 854 exit(EXIT_FAILURE); 855 } 856 } 857