1 /* 2 * iperf, Copyright (c) 2014-2021, The Regents of the University of 3 * California, through Lawrence Berkeley National Laboratory (subject 4 * to receipt of any required approvals from the U.S. Dept. of 5 * Energy). All rights reserved. 6 * 7 * If you have questions about your rights to use or distribute this 8 * software, please contact Berkeley Lab's Technology Transfer 9 * Department at [email protected]. 10 * 11 * NOTICE. This software is owned by the U.S. Department of Energy. 12 * As such, the U.S. Government has been granted for itself and others 13 * acting on its behalf a paid-up, nonexclusive, irrevocable, 14 * worldwide license in the Software to reproduce, prepare derivative 15 * works, and perform publicly and display publicly. Beginning five 16 * (5) years after the date permission to assert copyright is obtained 17 * from the U.S. Department of Energy, and subject to any subsequent 18 * five (5) year renewals, the U.S. Government is granted for itself 19 * and others acting on its behalf a paid-up, nonexclusive, 20 * irrevocable, worldwide license in the Software to reproduce, 21 * prepare derivative works, distribute copies to the public, perform 22 * publicly and display publicly, and to permit others to do so. 23 * 24 * This code is distributed under a BSD style license, see the LICENSE file 25 * for complete information. 26 */ 27 #ifndef _GNU_SOURCE 28 # define _GNU_SOURCE 29 #endif 30 #define __USE_GNU 31 32 #include "iperf_config.h" 33 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <time.h> 38 #include <getopt.h> 39 #include <errno.h> 40 #include <signal.h> 41 #include <unistd.h> 42 #include <assert.h> 43 #include <fcntl.h> 44 #include <sys/socket.h> 45 #include <sys/types.h> 46 #include <netinet/in.h> 47 #include <arpa/inet.h> 48 #include <netdb.h> 49 #ifdef HAVE_STDINT_H 50 #include <stdint.h> 51 #endif 52 #include <sys/time.h> 53 #include <sys/resource.h> 54 #include <sys/mman.h> 55 #include <sys/stat.h> 56 #include <sched.h> 57 #include <setjmp.h> 58 #include <stdarg.h> 59 #include <math.h> 60 61 #if defined(HAVE_CPUSET_SETAFFINITY) 62 #include <sys/param.h> 63 #include <sys/cpuset.h> 64 #endif /* HAVE_CPUSET_SETAFFINITY */ 65 66 #if defined(__CYGWIN__) || defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__) 67 #define CPU_SETSIZE __CPU_SETSIZE 68 #endif /* __CYGWIN__, _WIN32, _WIN64, __WINDOWS__ */ 69 70 #if defined(HAVE_SETPROCESSAFFINITYMASK) 71 #include <Windows.h> 72 #endif /* HAVE_SETPROCESSAFFINITYMASK */ 73 74 #include "net.h" 75 #include "iperf.h" 76 #include "iperf_api.h" 77 #include "iperf_udp.h" 78 #include "iperf_tcp.h" 79 #if defined(HAVE_SCTP_H) 80 #include "iperf_sctp.h" 81 #endif /* HAVE_SCTP_H */ 82 #include "timer.h" 83 84 #include "cjson.h" 85 #include "units.h" 86 #include "iperf_util.h" 87 #include "iperf_locale.h" 88 #include "version.h" 89 #if defined(HAVE_SSL) 90 #include <openssl/bio.h> 91 #include <openssl/err.h> 92 #include "iperf_auth.h" 93 #endif /* HAVE_SSL */ 94 95 /* Forwards. */ 96 static int send_parameters(struct iperf_test *test); 97 static int get_parameters(struct iperf_test *test); 98 static int send_results(struct iperf_test *test); 99 static int get_results(struct iperf_test *test); 100 static int diskfile_send(struct iperf_stream *sp); 101 static int diskfile_recv(struct iperf_stream *sp); 102 static int JSON_write(int fd, cJSON *json); 103 static void print_interval_results(struct iperf_test *test, struct iperf_stream *sp, cJSON *json_interval_streams); 104 static cJSON *JSON_read(int fd); 105 106 107 /*************************** Print usage functions ****************************/ 108 109 void 110 usage() 111 { 112 fputs(usage_shortstr, stderr); 113 } 114 115 116 void 117 usage_long(FILE *f) 118 { 119 fprintf(f, usage_longstr, DEFAULT_NO_MSG_RCVD_TIMEOUT, UDP_RATE / (1024*1024), DEFAULT_PACING_TIMER, DURATION, DEFAULT_TCP_BLKSIZE / 1024, DEFAULT_UDP_BLKSIZE); 120 } 121 122 123 void warning(const char *str) 124 { 125 fprintf(stderr, "warning: %s\n", str); 126 } 127 128 129 /************** Getter routines for some fields inside iperf_test *************/ 130 131 int 132 iperf_get_verbose(struct iperf_test *ipt) 133 { 134 return ipt->verbose; 135 } 136 137 int 138 iperf_get_control_socket(struct iperf_test *ipt) 139 { 140 return ipt->ctrl_sck; 141 } 142 143 int 144 iperf_get_control_socket_mss(struct iperf_test *ipt) 145 { 146 return ipt->ctrl_sck_mss; 147 } 148 149 int 150 iperf_get_test_omit(struct iperf_test *ipt) 151 { 152 return ipt->omit; 153 } 154 155 int 156 iperf_get_test_duration(struct iperf_test *ipt) 157 { 158 return ipt->duration; 159 } 160 161 uint64_t 162 iperf_get_test_rate(struct iperf_test *ipt) 163 { 164 return ipt->settings->rate; 165 } 166 167 uint64_t 168 iperf_get_test_bitrate_limit(struct iperf_test *ipt) 169 { 170 return ipt->settings->bitrate_limit; 171 } 172 173 double 174 iperf_get_test_bitrate_limit_interval(struct iperf_test *ipt) 175 { 176 return ipt->settings->bitrate_limit_interval; 177 } 178 179 int 180 iperf_get_test_bitrate_limit_stats_per_interval(struct iperf_test *ipt) 181 { 182 return ipt->settings->bitrate_limit_stats_per_interval; 183 } 184 185 uint64_t 186 iperf_get_test_fqrate(struct iperf_test *ipt) 187 { 188 return ipt->settings->fqrate; 189 } 190 191 int 192 iperf_get_test_pacing_timer(struct iperf_test *ipt) 193 { 194 return ipt->settings->pacing_timer; 195 } 196 197 uint64_t 198 iperf_get_test_bytes(struct iperf_test *ipt) 199 { 200 return (uint64_t) ipt->settings->bytes; 201 } 202 203 uint64_t 204 iperf_get_test_blocks(struct iperf_test *ipt) 205 { 206 return (uint64_t) ipt->settings->blocks; 207 } 208 209 int 210 iperf_get_test_burst(struct iperf_test *ipt) 211 { 212 return ipt->settings->burst; 213 } 214 215 char 216 iperf_get_test_role(struct iperf_test *ipt) 217 { 218 return ipt->role; 219 } 220 221 int 222 iperf_get_test_reverse(struct iperf_test *ipt) 223 { 224 return ipt->reverse; 225 } 226 227 int 228 iperf_get_test_blksize(struct iperf_test *ipt) 229 { 230 return ipt->settings->blksize; 231 } 232 233 FILE * 234 iperf_get_test_outfile (struct iperf_test *ipt) 235 { 236 return ipt->outfile; 237 } 238 239 int 240 iperf_get_test_socket_bufsize(struct iperf_test *ipt) 241 { 242 return ipt->settings->socket_bufsize; 243 } 244 245 double 246 iperf_get_test_reporter_interval(struct iperf_test *ipt) 247 { 248 return ipt->reporter_interval; 249 } 250 251 double 252 iperf_get_test_stats_interval(struct iperf_test *ipt) 253 { 254 return ipt->stats_interval; 255 } 256 257 int 258 iperf_get_test_num_streams(struct iperf_test *ipt) 259 { 260 return ipt->num_streams; 261 } 262 263 int 264 iperf_get_test_timestamps(struct iperf_test *ipt) 265 { 266 return ipt->timestamps; 267 } 268 269 const char * 270 iperf_get_test_timestamp_format(struct iperf_test *ipt) 271 { 272 return ipt->timestamp_format; 273 } 274 275 int 276 iperf_get_test_repeating_payload(struct iperf_test *ipt) 277 { 278 return ipt->repeating_payload; 279 } 280 281 int 282 iperf_get_test_server_port(struct iperf_test *ipt) 283 { 284 return ipt->server_port; 285 } 286 287 char* 288 iperf_get_test_server_hostname(struct iperf_test *ipt) 289 { 290 return ipt->server_hostname; 291 } 292 293 char* 294 iperf_get_test_template(struct iperf_test *ipt) 295 { 296 return ipt->tmp_template; 297 } 298 299 int 300 iperf_get_test_protocol_id(struct iperf_test *ipt) 301 { 302 return ipt->protocol->id; 303 } 304 305 int 306 iperf_get_test_json_output(struct iperf_test *ipt) 307 { 308 return ipt->json_output; 309 } 310 311 char * 312 iperf_get_test_json_output_string(struct iperf_test *ipt) 313 { 314 return ipt->json_output_string; 315 } 316 317 int 318 iperf_get_test_zerocopy(struct iperf_test *ipt) 319 { 320 return ipt->zerocopy; 321 } 322 323 int 324 iperf_get_test_get_server_output(struct iperf_test *ipt) 325 { 326 return ipt->get_server_output; 327 } 328 329 char 330 iperf_get_test_unit_format(struct iperf_test *ipt) 331 { 332 return ipt->settings->unit_format; 333 } 334 335 char * 336 iperf_get_test_bind_address(struct iperf_test *ipt) 337 { 338 return ipt->bind_address; 339 } 340 341 char * 342 iperf_get_test_bind_dev(struct iperf_test *ipt) 343 { 344 return ipt->bind_dev; 345 } 346 347 int 348 iperf_get_test_udp_counters_64bit(struct iperf_test *ipt) 349 { 350 return ipt->udp_counters_64bit; 351 } 352 353 int 354 iperf_get_test_one_off(struct iperf_test *ipt) 355 { 356 return ipt->one_off; 357 } 358 359 int 360 iperf_get_test_tos(struct iperf_test *ipt) 361 { 362 return ipt->settings->tos; 363 } 364 365 char * 366 iperf_get_test_extra_data(struct iperf_test *ipt) 367 { 368 return ipt->extra_data; 369 } 370 371 static const char iperf_version[] = IPERF_VERSION; 372 char * 373 iperf_get_iperf_version(void) 374 { 375 return (char*)iperf_version; 376 } 377 378 int 379 iperf_get_test_no_delay(struct iperf_test *ipt) 380 { 381 return ipt->no_delay; 382 } 383 384 int 385 iperf_get_test_connect_timeout(struct iperf_test *ipt) 386 { 387 return ipt->settings->connect_timeout; 388 } 389 390 int 391 iperf_get_test_idle_timeout(struct iperf_test *ipt) 392 { 393 return ipt->settings->idle_timeout; 394 } 395 396 int 397 iperf_get_dont_fragment(struct iperf_test *ipt) 398 { 399 return ipt->settings->dont_fragment; 400 } 401 402 struct iperf_time* 403 iperf_get_test_rcv_timeout(struct iperf_test *ipt) 404 { 405 return &ipt->settings->rcv_timeout; 406 } 407 408 char* 409 iperf_get_test_congestion_control(struct iperf_test* ipt) 410 { 411 return ipt->congestion; 412 } 413 414 /************** Setter routines for some fields inside iperf_test *************/ 415 416 void 417 iperf_set_verbose(struct iperf_test *ipt, int verbose) 418 { 419 ipt->verbose = verbose; 420 } 421 422 void 423 iperf_set_control_socket(struct iperf_test *ipt, int ctrl_sck) 424 { 425 ipt->ctrl_sck = ctrl_sck; 426 } 427 428 void 429 iperf_set_test_omit(struct iperf_test *ipt, int omit) 430 { 431 ipt->omit = omit; 432 } 433 434 void 435 iperf_set_test_duration(struct iperf_test *ipt, int duration) 436 { 437 ipt->duration = duration; 438 } 439 440 void 441 iperf_set_test_reporter_interval(struct iperf_test *ipt, double reporter_interval) 442 { 443 ipt->reporter_interval = reporter_interval; 444 } 445 446 void 447 iperf_set_test_stats_interval(struct iperf_test *ipt, double stats_interval) 448 { 449 ipt->stats_interval = stats_interval; 450 } 451 452 void 453 iperf_set_test_state(struct iperf_test *ipt, signed char state) 454 { 455 ipt->state = state; 456 } 457 458 void 459 iperf_set_test_blksize(struct iperf_test *ipt, int blksize) 460 { 461 ipt->settings->blksize = blksize; 462 } 463 464 void 465 iperf_set_test_logfile(struct iperf_test *ipt, const char *logfile) 466 { 467 ipt->logfile = strdup(logfile); 468 } 469 470 void 471 iperf_set_test_rate(struct iperf_test *ipt, uint64_t rate) 472 { 473 ipt->settings->rate = rate; 474 } 475 476 void 477 iperf_set_test_bitrate_limit_maximum(struct iperf_test *ipt, uint64_t total_rate) 478 { 479 ipt->settings->bitrate_limit = total_rate; 480 } 481 482 void 483 iperf_set_test_bitrate_limit_interval(struct iperf_test *ipt, uint64_t bitrate_limit_interval) 484 { 485 ipt->settings->bitrate_limit_interval = bitrate_limit_interval; 486 } 487 488 void 489 iperf_set_test_bitrate_limit_stats_per_interval(struct iperf_test *ipt, uint64_t bitrate_limit_stats_per_interval) 490 { 491 ipt->settings->bitrate_limit_stats_per_interval = bitrate_limit_stats_per_interval; 492 } 493 494 void 495 iperf_set_test_fqrate(struct iperf_test *ipt, uint64_t fqrate) 496 { 497 ipt->settings->fqrate = fqrate; 498 } 499 500 void 501 iperf_set_test_pacing_timer(struct iperf_test *ipt, int pacing_timer) 502 { 503 ipt->settings->pacing_timer = pacing_timer; 504 } 505 506 void 507 iperf_set_test_bytes(struct iperf_test *ipt, uint64_t bytes) 508 { 509 ipt->settings->bytes = (iperf_size_t) bytes; 510 } 511 512 void 513 iperf_set_test_blocks(struct iperf_test *ipt, uint64_t blocks) 514 { 515 ipt->settings->blocks = (iperf_size_t) blocks; 516 } 517 518 void 519 iperf_set_test_burst(struct iperf_test *ipt, int burst) 520 { 521 ipt->settings->burst = burst; 522 } 523 524 void 525 iperf_set_test_server_port(struct iperf_test *ipt, int srv_port) 526 { 527 ipt->server_port = srv_port; 528 } 529 530 void 531 iperf_set_test_socket_bufsize(struct iperf_test *ipt, int socket_bufsize) 532 { 533 ipt->settings->socket_bufsize = socket_bufsize; 534 } 535 536 void 537 iperf_set_test_num_streams(struct iperf_test *ipt, int num_streams) 538 { 539 ipt->num_streams = num_streams; 540 } 541 542 void 543 iperf_set_test_repeating_payload(struct iperf_test *ipt, int repeating_payload) 544 { 545 ipt->repeating_payload = repeating_payload; 546 } 547 548 void 549 iperf_set_test_timestamps(struct iperf_test *ipt, int timestamps) 550 { 551 ipt->timestamps = timestamps; 552 } 553 554 void 555 iperf_set_test_timestamp_format(struct iperf_test *ipt, const char *tf) 556 { 557 ipt->timestamp_format = strdup(tf); 558 } 559 560 static void 561 check_sender_has_retransmits(struct iperf_test *ipt) 562 { 563 if (ipt->mode != RECEIVER && ipt->protocol->id == Ptcp && has_tcpinfo_retransmits()) 564 ipt->sender_has_retransmits = 1; 565 else 566 ipt->sender_has_retransmits = 0; 567 } 568 569 void 570 iperf_set_test_role(struct iperf_test *ipt, char role) 571 { 572 ipt->role = role; 573 if (!ipt->reverse) { 574 if (ipt->bidirectional) 575 ipt->mode = BIDIRECTIONAL; 576 else if (role == 'c') 577 ipt->mode = SENDER; 578 else if (role == 's') 579 ipt->mode = RECEIVER; 580 } else { 581 if (role == 'c') 582 ipt->mode = RECEIVER; 583 else if (role == 's') 584 ipt->mode = SENDER; 585 } 586 check_sender_has_retransmits(ipt); 587 } 588 589 void 590 iperf_set_test_server_hostname(struct iperf_test *ipt, const char *server_hostname) 591 { 592 ipt->server_hostname = strdup(server_hostname); 593 } 594 595 void 596 iperf_set_test_template(struct iperf_test *ipt, const char *tmp_template) 597 { 598 ipt->tmp_template = strdup(tmp_template); 599 } 600 601 void 602 iperf_set_test_reverse(struct iperf_test *ipt, int reverse) 603 { 604 ipt->reverse = reverse; 605 if (!ipt->reverse) { 606 if (ipt->role == 'c') 607 ipt->mode = SENDER; 608 else if (ipt->role == 's') 609 ipt->mode = RECEIVER; 610 } else { 611 if (ipt->role == 'c') 612 ipt->mode = RECEIVER; 613 else if (ipt->role == 's') 614 ipt->mode = SENDER; 615 } 616 check_sender_has_retransmits(ipt); 617 } 618 619 void 620 iperf_set_test_json_output(struct iperf_test *ipt, int json_output) 621 { 622 ipt->json_output = json_output; 623 } 624 625 int 626 iperf_has_zerocopy( void ) 627 { 628 return has_sendfile(); 629 } 630 631 void 632 iperf_set_test_zerocopy(struct iperf_test *ipt, int zerocopy) 633 { 634 ipt->zerocopy = (zerocopy && has_sendfile()); 635 } 636 637 void 638 iperf_set_test_get_server_output(struct iperf_test *ipt, int get_server_output) 639 { 640 ipt->get_server_output = get_server_output; 641 } 642 643 void 644 iperf_set_test_unit_format(struct iperf_test *ipt, char unit_format) 645 { 646 ipt->settings->unit_format = unit_format; 647 } 648 649 #if defined(HAVE_SSL) 650 void 651 iperf_set_test_client_username(struct iperf_test *ipt, const char *client_username) 652 { 653 ipt->settings->client_username = strdup(client_username); 654 } 655 656 void 657 iperf_set_test_client_password(struct iperf_test *ipt, const char *client_password) 658 { 659 ipt->settings->client_password = strdup(client_password); 660 } 661 662 void 663 iperf_set_test_client_rsa_pubkey(struct iperf_test *ipt, const char *client_rsa_pubkey_base64) 664 { 665 ipt->settings->client_rsa_pubkey = load_pubkey_from_base64(client_rsa_pubkey_base64); 666 } 667 668 void 669 iperf_set_test_server_authorized_users(struct iperf_test *ipt, const char *server_authorized_users) 670 { 671 ipt->server_authorized_users = strdup(server_authorized_users); 672 } 673 674 void 675 iperf_set_test_server_skew_threshold(struct iperf_test *ipt, int server_skew_threshold) 676 { 677 ipt->server_skew_threshold = server_skew_threshold; 678 } 679 680 void 681 iperf_set_test_server_rsa_privkey(struct iperf_test *ipt, const char *server_rsa_privkey_base64) 682 { 683 ipt->server_rsa_private_key = load_privkey_from_base64(server_rsa_privkey_base64); 684 } 685 #endif // HAVE_SSL 686 687 void 688 iperf_set_test_bind_address(struct iperf_test *ipt, const char *bnd_address) 689 { 690 ipt->bind_address = strdup(bnd_address); 691 } 692 693 void 694 iperf_set_test_bind_dev(struct iperf_test *ipt, char *bnd_dev) 695 { 696 ipt->bind_dev = strdup(bnd_dev); 697 } 698 699 void 700 iperf_set_test_udp_counters_64bit(struct iperf_test *ipt, int udp_counters_64bit) 701 { 702 ipt->udp_counters_64bit = udp_counters_64bit; 703 } 704 705 void 706 iperf_set_test_one_off(struct iperf_test *ipt, int one_off) 707 { 708 ipt->one_off = one_off; 709 } 710 711 void 712 iperf_set_test_tos(struct iperf_test *ipt, int tos) 713 { 714 ipt->settings->tos = tos; 715 } 716 717 void 718 iperf_set_test_extra_data(struct iperf_test *ipt, const char *dat) 719 { 720 ipt->extra_data = strdup(dat); 721 } 722 723 void 724 iperf_set_test_bidirectional(struct iperf_test* ipt, int bidirectional) 725 { 726 ipt->bidirectional = bidirectional; 727 if (bidirectional) 728 ipt->mode = BIDIRECTIONAL; 729 else 730 iperf_set_test_reverse(ipt, ipt->reverse); 731 } 732 733 void 734 iperf_set_test_no_delay(struct iperf_test* ipt, int no_delay) 735 { 736 ipt->no_delay = no_delay; 737 } 738 739 void 740 iperf_set_test_connect_timeout(struct iperf_test* ipt, int ct) 741 { 742 ipt->settings->connect_timeout = ct; 743 } 744 745 void 746 iperf_set_test_idle_timeout(struct iperf_test* ipt, int to) 747 { 748 ipt->settings->idle_timeout = to; 749 } 750 751 void 752 iperf_set_dont_fragment(struct iperf_test* ipt, int dnf) 753 { 754 ipt->settings->dont_fragment = dnf; 755 } 756 757 void 758 iperf_set_test_rcv_timeout(struct iperf_test* ipt, struct iperf_time* to) 759 { 760 ipt->settings->rcv_timeout.secs = to->secs; 761 ipt->settings->rcv_timeout.usecs = to->usecs; 762 } 763 764 void 765 iperf_set_test_congestion_control(struct iperf_test* ipt, char* cc) 766 { 767 ipt->congestion = strdup(cc); 768 } 769 770 771 /********************** Get/set test protocol structure ***********************/ 772 773 struct protocol * 774 get_protocol(struct iperf_test *test, int prot_id) 775 { 776 struct protocol *prot; 777 778 SLIST_FOREACH(prot, &test->protocols, protocols) { 779 if (prot->id == prot_id) 780 break; 781 } 782 783 if (prot == NULL) 784 i_errno = IEPROTOCOL; 785 786 return prot; 787 } 788 789 int 790 set_protocol(struct iperf_test *test, int prot_id) 791 { 792 struct protocol *prot = NULL; 793 794 SLIST_FOREACH(prot, &test->protocols, protocols) { 795 if (prot->id == prot_id) { 796 test->protocol = prot; 797 check_sender_has_retransmits(test); 798 return 0; 799 } 800 } 801 802 i_errno = IEPROTOCOL; 803 return -1; 804 } 805 806 807 /************************** Iperf callback functions **************************/ 808 809 void 810 iperf_on_new_stream(struct iperf_stream *sp) 811 { 812 connect_msg(sp); 813 } 814 815 void 816 iperf_on_test_start(struct iperf_test *test) 817 { 818 if (test->json_output) { 819 cJSON_AddItemToObject(test->json_start, "test_start", iperf_json_printf("protocol: %s num_streams: %d blksize: %d omit: %d duration: %d bytes: %d blocks: %d reverse: %d tos: %d target_bitrate: %d", test->protocol->name, (int64_t) test->num_streams, (int64_t) test->settings->blksize, (int64_t) test->omit, (int64_t) test->duration, (int64_t) test->settings->bytes, (int64_t) test->settings->blocks, test->reverse?(int64_t)1:(int64_t)0, (int64_t) test->settings->tos, (int64_t) test->settings->rate)); 820 } else { 821 if (test->verbose) { 822 if (test->settings->bytes) 823 iperf_printf(test, test_start_bytes, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->settings->bytes, test->settings->tos); 824 else if (test->settings->blocks) 825 iperf_printf(test, test_start_blocks, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->settings->blocks, test->settings->tos); 826 else 827 iperf_printf(test, test_start_time, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->duration, test->settings->tos); 828 } 829 } 830 } 831 832 /* This converts an IPv6 string address from IPv4-mapped format into regular 833 ** old IPv4 format, which is easier on the eyes of network veterans. 834 ** 835 ** If the v6 address is not v4-mapped it is left alone. 836 */ 837 static void 838 mapped_v4_to_regular_v4(char *str) 839 { 840 char *prefix = "::ffff:"; 841 int prefix_len; 842 843 prefix_len = strlen(prefix); 844 if (strncmp(str, prefix, prefix_len) == 0) { 845 int str_len = strlen(str); 846 memmove(str, str + prefix_len, str_len - prefix_len + 1); 847 } 848 } 849 850 void 851 iperf_on_connect(struct iperf_test *test) 852 { 853 time_t now_secs; 854 const char* rfc1123_fmt = "%a, %d %b %Y %H:%M:%S %Z"; 855 char now_str[100]; 856 char ipr[INET6_ADDRSTRLEN]; 857 int port; 858 struct sockaddr_storage sa; 859 struct sockaddr_in *sa_inP; 860 struct sockaddr_in6 *sa_in6P; 861 socklen_t len; 862 863 now_secs = time((time_t*) 0); 864 (void) strftime(now_str, sizeof(now_str), rfc1123_fmt, gmtime(&now_secs)); 865 if (test->json_output) 866 cJSON_AddItemToObject(test->json_start, "timestamp", iperf_json_printf("time: %s timesecs: %d", now_str, (int64_t) now_secs)); 867 else if (test->verbose) 868 iperf_printf(test, report_time, now_str); 869 870 if (test->role == 'c') { 871 if (test->json_output) 872 cJSON_AddItemToObject(test->json_start, "connecting_to", iperf_json_printf("host: %s port: %d", test->server_hostname, (int64_t) test->server_port)); 873 else { 874 iperf_printf(test, report_connecting, test->server_hostname, test->server_port); 875 if (test->reverse) 876 iperf_printf(test, report_reverse, test->server_hostname); 877 } 878 } else { 879 len = sizeof(sa); 880 getpeername(test->ctrl_sck, (struct sockaddr *) &sa, &len); 881 if (getsockdomain(test->ctrl_sck) == AF_INET) { 882 sa_inP = (struct sockaddr_in *) &sa; 883 inet_ntop(AF_INET, &sa_inP->sin_addr, ipr, sizeof(ipr)); 884 port = ntohs(sa_inP->sin_port); 885 } else { 886 sa_in6P = (struct sockaddr_in6 *) &sa; 887 inet_ntop(AF_INET6, &sa_in6P->sin6_addr, ipr, sizeof(ipr)); 888 port = ntohs(sa_in6P->sin6_port); 889 } 890 mapped_v4_to_regular_v4(ipr); 891 if (test->json_output) 892 cJSON_AddItemToObject(test->json_start, "accepted_connection", iperf_json_printf("host: %s port: %d", ipr, (int64_t) port)); 893 else 894 iperf_printf(test, report_accepted, ipr, port); 895 } 896 if (test->json_output) { 897 cJSON_AddStringToObject(test->json_start, "cookie", test->cookie); 898 if (test->protocol->id == SOCK_STREAM) { 899 if (test->settings->mss) 900 cJSON_AddNumberToObject(test->json_start, "tcp_mss", test->settings->mss); 901 else { 902 cJSON_AddNumberToObject(test->json_start, "tcp_mss_default", test->ctrl_sck_mss); 903 } 904 } 905 cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate); 906 } else if (test->verbose) { 907 iperf_printf(test, report_cookie, test->cookie); 908 if (test->protocol->id == SOCK_STREAM) { 909 if (test->settings->mss) 910 iperf_printf(test, " TCP MSS: %d\n", test->settings->mss); 911 else { 912 iperf_printf(test, " TCP MSS: %d (default)\n", test->ctrl_sck_mss); 913 } 914 } 915 if (test->settings->rate) 916 iperf_printf(test, " Target Bitrate: %"PRIu64"\n", test->settings->rate); 917 } 918 } 919 920 void 921 iperf_on_test_finish(struct iperf_test *test) 922 { 923 } 924 925 926 /******************************************************************************/ 927 928 int 929 iperf_parse_arguments(struct iperf_test *test, int argc, char **argv) 930 { 931 static struct option longopts[] = 932 { 933 {"port", required_argument, NULL, 'p'}, 934 {"format", required_argument, NULL, 'f'}, 935 {"interval", required_argument, NULL, 'i'}, 936 {"daemon", no_argument, NULL, 'D'}, 937 {"one-off", no_argument, NULL, '1'}, 938 {"verbose", no_argument, NULL, 'V'}, 939 {"json", no_argument, NULL, 'J'}, 940 {"version", no_argument, NULL, 'v'}, 941 {"server", no_argument, NULL, 's'}, 942 {"client", required_argument, NULL, 'c'}, 943 {"udp", no_argument, NULL, 'u'}, 944 {"bitrate", required_argument, NULL, 'b'}, 945 {"bandwidth", required_argument, NULL, 'b'}, 946 {"server-bitrate-limit", required_argument, NULL, OPT_SERVER_BITRATE_LIMIT}, 947 {"time", required_argument, NULL, 't'}, 948 {"bytes", required_argument, NULL, 'n'}, 949 {"blockcount", required_argument, NULL, 'k'}, 950 {"length", required_argument, NULL, 'l'}, 951 {"parallel", required_argument, NULL, 'P'}, 952 {"reverse", no_argument, NULL, 'R'}, 953 {"bidir", no_argument, NULL, OPT_BIDIRECTIONAL}, 954 {"window", required_argument, NULL, 'w'}, 955 {"bind", required_argument, NULL, 'B'}, 956 #if defined(HAVE_SO_BINDTODEVICE) 957 {"bind-dev", required_argument, NULL, OPT_BIND_DEV}, 958 #endif /* HAVE_SO_BINDTODEVICE */ 959 {"cport", required_argument, NULL, OPT_CLIENT_PORT}, 960 {"set-mss", required_argument, NULL, 'M'}, 961 {"no-delay", no_argument, NULL, 'N'}, 962 {"version4", no_argument, NULL, '4'}, 963 {"version6", no_argument, NULL, '6'}, 964 {"tos", required_argument, NULL, 'S'}, 965 {"dscp", required_argument, NULL, OPT_DSCP}, 966 {"extra-data", required_argument, NULL, OPT_EXTRA_DATA}, 967 #if defined(HAVE_FLOWLABEL) 968 {"flowlabel", required_argument, NULL, 'L'}, 969 #endif /* HAVE_FLOWLABEL */ 970 {"zerocopy", no_argument, NULL, 'Z'}, 971 {"omit", required_argument, NULL, 'O'}, 972 {"file", required_argument, NULL, 'F'}, 973 {"repeating-payload", no_argument, NULL, OPT_REPEATING_PAYLOAD}, 974 {"timestamps", optional_argument, NULL, OPT_TIMESTAMPS}, 975 #if defined(HAVE_CPU_AFFINITY) 976 {"affinity", required_argument, NULL, 'A'}, 977 #endif /* HAVE_CPU_AFFINITY */ 978 {"title", required_argument, NULL, 'T'}, 979 #if defined(HAVE_TCP_CONGESTION) 980 {"congestion", required_argument, NULL, 'C'}, 981 {"linux-congestion", required_argument, NULL, 'C'}, 982 #endif /* HAVE_TCP_CONGESTION */ 983 #if defined(HAVE_SCTP_H) 984 {"sctp", no_argument, NULL, OPT_SCTP}, 985 {"nstreams", required_argument, NULL, OPT_NUMSTREAMS}, 986 {"xbind", required_argument, NULL, 'X'}, 987 #endif 988 {"pidfile", required_argument, NULL, 'I'}, 989 {"logfile", required_argument, NULL, OPT_LOGFILE}, 990 {"forceflush", no_argument, NULL, OPT_FORCEFLUSH}, 991 {"get-server-output", no_argument, NULL, OPT_GET_SERVER_OUTPUT}, 992 {"udp-counters-64bit", no_argument, NULL, OPT_UDP_COUNTERS_64BIT}, 993 {"no-fq-socket-pacing", no_argument, NULL, OPT_NO_FQ_SOCKET_PACING}, 994 #if defined(HAVE_DONT_FRAGMENT) 995 {"dont-fragment", no_argument, NULL, OPT_DONT_FRAGMENT}, 996 #endif /* HAVE_DONT_FRAGMENT */ 997 #if defined(HAVE_SSL) 998 {"username", required_argument, NULL, OPT_CLIENT_USERNAME}, 999 {"rsa-public-key-path", required_argument, NULL, OPT_CLIENT_RSA_PUBLIC_KEY}, 1000 {"rsa-private-key-path", required_argument, NULL, OPT_SERVER_RSA_PRIVATE_KEY}, 1001 {"authorized-users-path", required_argument, NULL, OPT_SERVER_AUTHORIZED_USERS}, 1002 {"time-skew-threshold", required_argument, NULL, OPT_SERVER_SKEW_THRESHOLD}, 1003 #endif /* HAVE_SSL */ 1004 {"fq-rate", required_argument, NULL, OPT_FQ_RATE}, 1005 {"pacing-timer", required_argument, NULL, OPT_PACING_TIMER}, 1006 {"connect-timeout", required_argument, NULL, OPT_CONNECT_TIMEOUT}, 1007 {"idle-timeout", required_argument, NULL, OPT_IDLE_TIMEOUT}, 1008 {"rcv-timeout", required_argument, NULL, OPT_RCV_TIMEOUT}, 1009 {"debug", no_argument, NULL, 'd'}, 1010 {"help", no_argument, NULL, 'h'}, 1011 {NULL, 0, NULL, 0} 1012 }; 1013 int flag; 1014 int portno; 1015 int blksize; 1016 int server_flag, client_flag, rate_flag, duration_flag, rcv_timeout_flag; 1017 char *endptr; 1018 #if defined(HAVE_CPU_AFFINITY) 1019 char* comma; 1020 #endif /* HAVE_CPU_AFFINITY */ 1021 char* slash; 1022 struct xbind_entry *xbe; 1023 double farg; 1024 int rcv_timeout_in = 0; 1025 1026 blksize = 0; 1027 server_flag = client_flag = rate_flag = duration_flag = rcv_timeout_flag = 0; 1028 #if defined(HAVE_SSL) 1029 char *client_username = NULL, *client_rsa_public_key = NULL, *server_rsa_private_key = NULL; 1030 #endif /* HAVE_SSL */ 1031 1032 while ((flag = getopt_long(argc, argv, "p:f:i:D1VJvsc:ub:t:n:k:l:P:Rw:B:M:N46S:L:ZO:F:A:T:C:dI:hX:", longopts, NULL)) != -1) { 1033 switch (flag) { 1034 case 'p': 1035 portno = atoi(optarg); 1036 if (portno < 1 || portno > 65535) { 1037 i_errno = IEBADPORT; 1038 return -1; 1039 } 1040 test->server_port = portno; 1041 break; 1042 case 'f': 1043 if (!optarg) { 1044 i_errno = IEBADFORMAT; 1045 return -1; 1046 } 1047 test->settings->unit_format = *optarg; 1048 if (test->settings->unit_format == 'k' || 1049 test->settings->unit_format == 'K' || 1050 test->settings->unit_format == 'm' || 1051 test->settings->unit_format == 'M' || 1052 test->settings->unit_format == 'g' || 1053 test->settings->unit_format == 'G' || 1054 test->settings->unit_format == 't' || 1055 test->settings->unit_format == 'T') { 1056 break; 1057 } 1058 else { 1059 i_errno = IEBADFORMAT; 1060 return -1; 1061 } 1062 break; 1063 case 'i': 1064 /* XXX: could potentially want separate stat collection and reporting intervals, 1065 but just set them to be the same for now */ 1066 test->stats_interval = test->reporter_interval = atof(optarg); 1067 if ((test->stats_interval < MIN_INTERVAL || test->stats_interval > MAX_INTERVAL) && test->stats_interval != 0) { 1068 i_errno = IEINTERVAL; 1069 return -1; 1070 } 1071 break; 1072 case 'D': 1073 test->daemon = 1; 1074 server_flag = 1; 1075 break; 1076 case '1': 1077 test->one_off = 1; 1078 server_flag = 1; 1079 break; 1080 case 'V': 1081 test->verbose = 1; 1082 break; 1083 case 'J': 1084 test->json_output = 1; 1085 break; 1086 case 'v': 1087 printf("%s (cJSON %s)\n%s\n%s\n", version, cJSON_Version(), get_system_info(), 1088 get_optional_features()); 1089 exit(0); 1090 case 's': 1091 if (test->role == 'c') { 1092 i_errno = IESERVCLIENT; 1093 return -1; 1094 } 1095 iperf_set_test_role(test, 's'); 1096 break; 1097 case 'c': 1098 if (test->role == 's') { 1099 i_errno = IESERVCLIENT; 1100 return -1; 1101 } 1102 iperf_set_test_role(test, 'c'); 1103 iperf_set_test_server_hostname(test, optarg); 1104 break; 1105 case 'u': 1106 set_protocol(test, Pudp); 1107 client_flag = 1; 1108 break; 1109 case OPT_SCTP: 1110 #if defined(HAVE_SCTP_H) 1111 set_protocol(test, Psctp); 1112 client_flag = 1; 1113 break; 1114 #else /* HAVE_SCTP_H */ 1115 i_errno = IEUNIMP; 1116 return -1; 1117 #endif /* HAVE_SCTP_H */ 1118 1119 case OPT_NUMSTREAMS: 1120 #if defined(linux) || defined(__FreeBSD__) 1121 test->settings->num_ostreams = unit_atoi(optarg); 1122 client_flag = 1; 1123 #else /* linux */ 1124 i_errno = IEUNIMP; 1125 return -1; 1126 #endif /* linux */ 1127 case 'b': 1128 slash = strchr(optarg, '/'); 1129 if (slash) { 1130 *slash = '\0'; 1131 ++slash; 1132 test->settings->burst = atoi(slash); 1133 if (test->settings->burst <= 0 || 1134 test->settings->burst > MAX_BURST) { 1135 i_errno = IEBURST; 1136 return -1; 1137 } 1138 } 1139 test->settings->rate = unit_atof_rate(optarg); 1140 rate_flag = 1; 1141 client_flag = 1; 1142 break; 1143 case OPT_SERVER_BITRATE_LIMIT: 1144 slash = strchr(optarg, '/'); 1145 if (slash) { 1146 *slash = '\0'; 1147 ++slash; 1148 test->settings->bitrate_limit_interval = atof(slash); 1149 if (test->settings->bitrate_limit_interval != 0 && /* Using same Max/Min limits as for Stats Interval */ 1150 (test->settings->bitrate_limit_interval < MIN_INTERVAL || test->settings->bitrate_limit_interval > MAX_INTERVAL) ) { 1151 i_errno = IETOTALINTERVAL; 1152 return -1; 1153 } 1154 } 1155 test->settings->bitrate_limit = unit_atof_rate(optarg); 1156 server_flag = 1; 1157 break; 1158 case 't': 1159 test->duration = atoi(optarg); 1160 if (test->duration > MAX_TIME) { 1161 i_errno = IEDURATION; 1162 return -1; 1163 } 1164 duration_flag = 1; 1165 client_flag = 1; 1166 break; 1167 case 'n': 1168 test->settings->bytes = unit_atoi(optarg); 1169 client_flag = 1; 1170 break; 1171 case 'k': 1172 test->settings->blocks = unit_atoi(optarg); 1173 client_flag = 1; 1174 break; 1175 case 'l': 1176 blksize = unit_atoi(optarg); 1177 client_flag = 1; 1178 break; 1179 case 'P': 1180 test->num_streams = atoi(optarg); 1181 if (test->num_streams > MAX_STREAMS) { 1182 i_errno = IENUMSTREAMS; 1183 return -1; 1184 } 1185 client_flag = 1; 1186 break; 1187 case 'R': 1188 if (test->bidirectional) { 1189 i_errno = IEREVERSEBIDIR; 1190 return -1; 1191 } 1192 iperf_set_test_reverse(test, 1); 1193 client_flag = 1; 1194 break; 1195 case OPT_BIDIRECTIONAL: 1196 if (test->reverse) { 1197 i_errno = IEREVERSEBIDIR; 1198 return -1; 1199 } 1200 iperf_set_test_bidirectional(test, 1); 1201 client_flag = 1; 1202 break; 1203 case 'w': 1204 // XXX: This is a socket buffer, not specific to TCP 1205 // Do sanity checks as double-precision floating point 1206 // to avoid possible integer overflows. 1207 farg = unit_atof(optarg); 1208 if (farg > (double) MAX_TCP_BUFFER) { 1209 i_errno = IEBUFSIZE; 1210 return -1; 1211 } 1212 test->settings->socket_bufsize = (int) farg; 1213 client_flag = 1; 1214 break; 1215 case 'B': 1216 test->bind_address = strdup(optarg); 1217 break; 1218 #if defined (HAVE_SO_BINDTODEVICE) 1219 case OPT_BIND_DEV: 1220 test->bind_dev = strdup(optarg); 1221 break; 1222 #endif /* HAVE_SO_BINDTODEVICE */ 1223 case OPT_CLIENT_PORT: 1224 portno = atoi(optarg); 1225 if (portno < 1 || portno > 65535) { 1226 i_errno = IEBADPORT; 1227 return -1; 1228 } 1229 test->bind_port = portno; 1230 break; 1231 case 'M': 1232 test->settings->mss = atoi(optarg); 1233 if (test->settings->mss > MAX_MSS) { 1234 i_errno = IEMSS; 1235 return -1; 1236 } 1237 client_flag = 1; 1238 break; 1239 case 'N': 1240 test->no_delay = 1; 1241 client_flag = 1; 1242 break; 1243 case '4': 1244 test->settings->domain = AF_INET; 1245 break; 1246 case '6': 1247 test->settings->domain = AF_INET6; 1248 break; 1249 case 'S': 1250 test->settings->tos = strtol(optarg, &endptr, 0); 1251 if (endptr == optarg || 1252 test->settings->tos < 0 || 1253 test->settings->tos > 255) { 1254 i_errno = IEBADTOS; 1255 return -1; 1256 } 1257 client_flag = 1; 1258 break; 1259 case OPT_DSCP: 1260 test->settings->tos = parse_qos(optarg); 1261 if(test->settings->tos < 0) { 1262 i_errno = IEBADTOS; 1263 return -1; 1264 } 1265 client_flag = 1; 1266 break; 1267 case OPT_EXTRA_DATA: 1268 test->extra_data = strdup(optarg); 1269 client_flag = 1; 1270 break; 1271 case 'L': 1272 #if defined(HAVE_FLOWLABEL) 1273 test->settings->flowlabel = strtol(optarg, &endptr, 0); 1274 if (endptr == optarg || 1275 test->settings->flowlabel < 1 || test->settings->flowlabel > 0xfffff) { 1276 i_errno = IESETFLOW; 1277 return -1; 1278 } 1279 client_flag = 1; 1280 #else /* HAVE_FLOWLABEL */ 1281 i_errno = IEUNIMP; 1282 return -1; 1283 #endif /* HAVE_FLOWLABEL */ 1284 break; 1285 case 'X': 1286 xbe = (struct xbind_entry *)malloc(sizeof(struct xbind_entry)); 1287 if (!xbe) { 1288 i_errno = IESETSCTPBINDX; 1289 return -1; 1290 } 1291 memset(xbe, 0, sizeof(*xbe)); 1292 xbe->name = strdup(optarg); 1293 if (!xbe->name) { 1294 i_errno = IESETSCTPBINDX; 1295 return -1; 1296 } 1297 TAILQ_INSERT_TAIL(&test->xbind_addrs, xbe, link); 1298 break; 1299 case 'Z': 1300 if (!has_sendfile()) { 1301 i_errno = IENOSENDFILE; 1302 return -1; 1303 } 1304 test->zerocopy = 1; 1305 client_flag = 1; 1306 break; 1307 case OPT_REPEATING_PAYLOAD: 1308 test->repeating_payload = 1; 1309 client_flag = 1; 1310 break; 1311 case OPT_TIMESTAMPS: 1312 iperf_set_test_timestamps(test, 1); 1313 if (optarg) { 1314 iperf_set_test_timestamp_format(test, optarg); 1315 } 1316 else { 1317 iperf_set_test_timestamp_format(test, TIMESTAMP_FORMAT); 1318 } 1319 break; 1320 case 'O': 1321 test->omit = atoi(optarg); 1322 if (test->omit < 0 || test->omit > 60) { 1323 i_errno = IEOMIT; 1324 return -1; 1325 } 1326 client_flag = 1; 1327 break; 1328 case 'F': 1329 test->diskfile_name = optarg; 1330 break; 1331 case OPT_IDLE_TIMEOUT: 1332 test->settings->idle_timeout = atoi(optarg); 1333 if (test->settings->idle_timeout < 1 || test->settings->idle_timeout > MAX_TIME) { 1334 i_errno = IEIDLETIMEOUT; 1335 return -1; 1336 } 1337 server_flag = 1; 1338 break; 1339 case OPT_RCV_TIMEOUT: 1340 rcv_timeout_in = atoi(optarg); 1341 if (rcv_timeout_in < MIN_NO_MSG_RCVD_TIMEOUT || rcv_timeout_in > MAX_TIME * SEC_TO_mS) { 1342 i_errno = IERCVTIMEOUT; 1343 return -1; 1344 } 1345 test->settings->rcv_timeout.secs = rcv_timeout_in / SEC_TO_mS; 1346 test->settings->rcv_timeout.usecs = (rcv_timeout_in % SEC_TO_mS) * mS_TO_US; 1347 rcv_timeout_flag = 1; 1348 break; 1349 case 'A': 1350 #if defined(HAVE_CPU_AFFINITY) 1351 test->affinity = strtol(optarg, &endptr, 0); 1352 if (endptr == optarg || 1353 test->affinity < 0 || test->affinity > 1024) { 1354 i_errno = IEAFFINITY; 1355 return -1; 1356 } 1357 comma = strchr(optarg, ','); 1358 if (comma != NULL) { 1359 test->server_affinity = atoi(comma+1); 1360 if (test->server_affinity < 0 || test->server_affinity > 1024) { 1361 i_errno = IEAFFINITY; 1362 return -1; 1363 } 1364 client_flag = 1; 1365 } 1366 #else /* HAVE_CPU_AFFINITY */ 1367 i_errno = IEUNIMP; 1368 return -1; 1369 #endif /* HAVE_CPU_AFFINITY */ 1370 break; 1371 case 'T': 1372 test->title = strdup(optarg); 1373 client_flag = 1; 1374 break; 1375 case 'C': 1376 #if defined(HAVE_TCP_CONGESTION) 1377 test->congestion = strdup(optarg); 1378 client_flag = 1; 1379 #else /* HAVE_TCP_CONGESTION */ 1380 i_errno = IEUNIMP; 1381 return -1; 1382 #endif /* HAVE_TCP_CONGESTION */ 1383 break; 1384 case 'd': 1385 test->debug = 1; 1386 break; 1387 case 'I': 1388 test->pidfile = strdup(optarg); 1389 break; 1390 case OPT_LOGFILE: 1391 test->logfile = strdup(optarg); 1392 break; 1393 case OPT_FORCEFLUSH: 1394 test->forceflush = 1; 1395 break; 1396 case OPT_GET_SERVER_OUTPUT: 1397 test->get_server_output = 1; 1398 client_flag = 1; 1399 break; 1400 case OPT_UDP_COUNTERS_64BIT: 1401 test->udp_counters_64bit = 1; 1402 break; 1403 case OPT_NO_FQ_SOCKET_PACING: 1404 #if defined(HAVE_SO_MAX_PACING_RATE) 1405 printf("Warning: --no-fq-socket-pacing is deprecated\n"); 1406 test->settings->fqrate = 0; 1407 client_flag = 1; 1408 #else /* HAVE_SO_MAX_PACING_RATE */ 1409 i_errno = IEUNIMP; 1410 return -1; 1411 #endif 1412 break; 1413 case OPT_FQ_RATE: 1414 #if defined(HAVE_SO_MAX_PACING_RATE) 1415 test->settings->fqrate = unit_atof_rate(optarg); 1416 client_flag = 1; 1417 #else /* HAVE_SO_MAX_PACING_RATE */ 1418 i_errno = IEUNIMP; 1419 return -1; 1420 #endif 1421 break; 1422 #if defined(HAVE_DONT_FRAGMENT) 1423 case OPT_DONT_FRAGMENT: 1424 test->settings->dont_fragment = 1; 1425 client_flag = 1; 1426 break; 1427 #endif /* HAVE_DONT_FRAGMENT */ 1428 #if defined(HAVE_SSL) 1429 case OPT_CLIENT_USERNAME: 1430 client_username = strdup(optarg); 1431 break; 1432 case OPT_CLIENT_RSA_PUBLIC_KEY: 1433 client_rsa_public_key = strdup(optarg); 1434 break; 1435 case OPT_SERVER_RSA_PRIVATE_KEY: 1436 server_rsa_private_key = strdup(optarg); 1437 break; 1438 case OPT_SERVER_AUTHORIZED_USERS: 1439 test->server_authorized_users = strdup(optarg); 1440 break; 1441 case OPT_SERVER_SKEW_THRESHOLD: 1442 test->server_skew_threshold = atoi(optarg); 1443 if(test->server_skew_threshold <= 0){ 1444 i_errno = IESKEWTHRESHOLD; 1445 return -1; 1446 } 1447 break; 1448 #endif /* HAVE_SSL */ 1449 case OPT_PACING_TIMER: 1450 test->settings->pacing_timer = unit_atoi(optarg); 1451 client_flag = 1; 1452 break; 1453 case OPT_CONNECT_TIMEOUT: 1454 test->settings->connect_timeout = unit_atoi(optarg); 1455 client_flag = 1; 1456 break; 1457 case 'h': 1458 usage_long(stdout); 1459 exit(0); 1460 default: 1461 usage_long(stderr); 1462 exit(1); 1463 } 1464 } 1465 1466 /* Check flag / role compatibility. */ 1467 if (test->role == 'c' && server_flag) { 1468 i_errno = IESERVERONLY; 1469 return -1; 1470 } 1471 if (test->role == 's' && client_flag) { 1472 i_errno = IECLIENTONLY; 1473 return -1; 1474 } 1475 1476 #if defined(HAVE_SSL) 1477 1478 if (test->role == 's' && (client_username || client_rsa_public_key)){ 1479 i_errno = IECLIENTONLY; 1480 return -1; 1481 } else if (test->role == 'c' && (client_username || client_rsa_public_key) && 1482 !(client_username && client_rsa_public_key)) { 1483 i_errno = IESETCLIENTAUTH; 1484 return -1; 1485 } else if (test->role == 'c' && (client_username && client_rsa_public_key)){ 1486 1487 char *client_password = NULL; 1488 size_t s; 1489 /* Need to copy env var, so we can do a common free */ 1490 if ((client_password = getenv("IPERF3_PASSWORD")) != NULL) 1491 client_password = strdup(client_password); 1492 else if (iperf_getpass(&client_password, &s, stdin) < 0){ 1493 i_errno = IESETCLIENTAUTH; 1494 return -1; 1495 } 1496 if (test_load_pubkey_from_file(client_rsa_public_key) < 0){ 1497 iperf_err(test, "%s\n", ERR_error_string(ERR_get_error(), NULL)); 1498 i_errno = IESETCLIENTAUTH; 1499 return -1; 1500 } 1501 1502 test->settings->client_username = client_username; 1503 test->settings->client_password = client_password; 1504 test->settings->client_rsa_pubkey = load_pubkey_from_file(client_rsa_public_key); 1505 free(client_rsa_public_key); 1506 client_rsa_public_key = NULL; 1507 } 1508 1509 if (test->role == 'c' && (server_rsa_private_key || test->server_authorized_users)){ 1510 i_errno = IESERVERONLY; 1511 return -1; 1512 } else if (test->role == 'c' && (test->server_skew_threshold != 0)){ 1513 i_errno = IESERVERONLY; 1514 return -1; 1515 } else if (test->role == 'c' && rcv_timeout_flag && test->mode == SENDER){ 1516 i_errno = IERVRSONLYRCVTIMEOUT; 1517 return -1; 1518 } else if (test->role == 's' && (server_rsa_private_key || test->server_authorized_users) && 1519 !(server_rsa_private_key && test->server_authorized_users)) { 1520 i_errno = IESETSERVERAUTH; 1521 return -1; 1522 } else if (test->role == 's' && server_rsa_private_key) { 1523 test->server_rsa_private_key = load_privkey_from_file(server_rsa_private_key); 1524 if (test->server_rsa_private_key == NULL){ 1525 iperf_err(test, "%s\n", ERR_error_string(ERR_get_error(), NULL)); 1526 i_errno = IESETSERVERAUTH; 1527 return -1; 1528 } 1529 free(server_rsa_private_key); 1530 server_rsa_private_key = NULL; 1531 1532 if(test->server_skew_threshold == 0){ 1533 // Set default value for time skew threshold 1534 test->server_skew_threshold=10; 1535 } 1536 } 1537 1538 #endif //HAVE_SSL 1539 if (blksize == 0) { 1540 if (test->protocol->id == Pudp) 1541 blksize = 0; /* try to dynamically determine from MSS */ 1542 else if (test->protocol->id == Psctp) 1543 blksize = DEFAULT_SCTP_BLKSIZE; 1544 else 1545 blksize = DEFAULT_TCP_BLKSIZE; 1546 } 1547 if ((test->protocol->id != Pudp && blksize <= 0) 1548 || blksize > MAX_BLOCKSIZE) { 1549 i_errno = IEBLOCKSIZE; 1550 return -1; 1551 } 1552 if (test->protocol->id == Pudp && 1553 (blksize > 0 && 1554 (blksize < MIN_UDP_BLOCKSIZE || blksize > MAX_UDP_BLOCKSIZE))) { 1555 i_errno = IEUDPBLOCKSIZE; 1556 return -1; 1557 } 1558 test->settings->blksize = blksize; 1559 1560 if (!rate_flag) 1561 test->settings->rate = test->protocol->id == Pudp ? UDP_RATE : 0; 1562 1563 /* if no bytes or blocks specified, nor a duration_flag, and we have -F, 1564 ** get the file-size as the bytes count to be transferred 1565 */ 1566 if (test->settings->bytes == 0 && 1567 test->settings->blocks == 0 && 1568 ! duration_flag && 1569 test->diskfile_name != (char*) 0 && 1570 test->role == 'c' 1571 ){ 1572 struct stat st; 1573 if( stat(test->diskfile_name, &st) == 0 ){ 1574 iperf_size_t file_bytes = st.st_size; 1575 test->settings->bytes = file_bytes; 1576 if (test->debug) 1577 printf("End condition set to file-size: %d bytes\n", test->settings->bytes); 1578 } 1579 // if failing to read file stat, it should fallback to default duration mode 1580 } 1581 1582 if ((test->settings->bytes != 0 || test->settings->blocks != 0) && ! duration_flag) 1583 test->duration = 0; 1584 1585 /* Disallow specifying multiple test end conditions. The code actually 1586 ** works just fine without this prohibition. As soon as any one of the 1587 ** three possible end conditions is met, the test ends. So this check 1588 ** could be removed if desired. 1589 */ 1590 if ((duration_flag && test->settings->bytes != 0) || 1591 (duration_flag && test->settings->blocks != 0) || 1592 (test->settings->bytes != 0 && test->settings->blocks != 0)) { 1593 i_errno = IEENDCONDITIONS; 1594 return -1; 1595 } 1596 1597 /* For subsequent calls to getopt */ 1598 #ifdef __APPLE__ 1599 optreset = 1; 1600 #endif 1601 optind = 0; 1602 1603 if ((test->role != 'c') && (test->role != 's')) { 1604 i_errno = IENOROLE; 1605 return -1; 1606 } 1607 1608 /* Set Total-rate average interval to multiplicity of State interval */ 1609 if (test->settings->bitrate_limit_interval != 0) { 1610 test->settings->bitrate_limit_stats_per_interval = 1611 (test->settings->bitrate_limit_interval <= test->stats_interval ? 1612 1 : round(test->settings->bitrate_limit_interval/test->stats_interval) ); 1613 } 1614 1615 /* Show warning if JSON output is used with explicit report format */ 1616 if ((test->json_output) && (test->settings->unit_format != 'a')) { 1617 warning("Report format (-f) flag ignored with JSON output (-J)"); 1618 } 1619 1620 /* Show warning if JSON output is used with verbose or debug flags */ 1621 if (test->json_output && test->verbose) { 1622 warning("Verbose output (-v) may interfere with JSON output (-J)"); 1623 } 1624 if (test->json_output && test->debug) { 1625 warning("Debug output (-d) may interfere with JSON output (-J)"); 1626 } 1627 1628 return 0; 1629 } 1630 1631 /* 1632 * Open the file specified by test->logfile and set test->outfile to its' FD. 1633 */ 1634 int iperf_open_logfile(struct iperf_test *test) 1635 { 1636 test->outfile = fopen(test->logfile, "a+"); 1637 if (test->outfile == NULL) { 1638 i_errno = IELOGFILE; 1639 return -1; 1640 } 1641 1642 return 0; 1643 } 1644 1645 int 1646 iperf_set_send_state(struct iperf_test *test, signed char state) 1647 { 1648 if (test->ctrl_sck >= 0) { 1649 test->state = state; 1650 if (Nwrite(test->ctrl_sck, (char*) &state, sizeof(state), Ptcp) < 0) { 1651 i_errno = IESENDMESSAGE; 1652 return -1; 1653 } 1654 } 1655 return 0; 1656 } 1657 1658 void 1659 iperf_check_throttle(struct iperf_stream *sp, struct iperf_time *nowP) 1660 { 1661 struct iperf_time temp_time; 1662 double seconds; 1663 uint64_t bits_per_second; 1664 1665 if (sp->test->done || sp->test->settings->rate == 0) 1666 return; 1667 iperf_time_diff(&sp->result->start_time_fixed, nowP, &temp_time); 1668 seconds = iperf_time_in_secs(&temp_time); 1669 bits_per_second = sp->result->bytes_sent * 8 / seconds; 1670 if (bits_per_second < sp->test->settings->rate) { 1671 sp->green_light = 1; 1672 FD_SET(sp->socket, &sp->test->write_set); 1673 } else { 1674 sp->green_light = 0; 1675 FD_CLR(sp->socket, &sp->test->write_set); 1676 } 1677 } 1678 1679 /* Verify that average traffic is not greater than the specified limit */ 1680 void 1681 iperf_check_total_rate(struct iperf_test *test, iperf_size_t last_interval_bytes_transferred) 1682 { 1683 double seconds; 1684 uint64_t bits_per_second; 1685 iperf_size_t total_bytes; 1686 int i; 1687 1688 if (test->done || test->settings->bitrate_limit == 0) // Continue only if check should be done 1689 return; 1690 1691 /* Add last inetrval's transferred bytes to the array */ 1692 if (++test->bitrate_limit_last_interval_index >= test->settings->bitrate_limit_stats_per_interval) 1693 test->bitrate_limit_last_interval_index = 0; 1694 test->bitrate_limit_intervals_traffic_bytes[test->bitrate_limit_last_interval_index] = last_interval_bytes_transferred; 1695 1696 /* Ensure that enough stats periods passed to allow averaging throughput */ 1697 test->bitrate_limit_stats_count += 1; 1698 if (test->bitrate_limit_stats_count < test->settings->bitrate_limit_stats_per_interval) 1699 return; 1700 1701 /* Calculating total bytes traffic to be averaged */ 1702 for (total_bytes = 0, i = 0; i < test->settings->bitrate_limit_stats_per_interval; i++) { 1703 total_bytes += test->bitrate_limit_intervals_traffic_bytes[i]; 1704 } 1705 1706 seconds = test->stats_interval * test->settings->bitrate_limit_stats_per_interval; 1707 bits_per_second = total_bytes * 8 / seconds; 1708 if (test->debug) { 1709 iperf_printf(test,"Interval %" PRIu64 " - throughput %" PRIu64 " bps (limit %" PRIu64 ")\n", test->bitrate_limit_stats_count, bits_per_second, test->settings->bitrate_limit); 1710 } 1711 1712 if (bits_per_second > test->settings->bitrate_limit) { 1713 if (iperf_get_verbose(test)) 1714 iperf_err(test, "Total throughput of %" PRIu64 " bps exceeded %" PRIu64 " bps limit", bits_per_second, test->settings->bitrate_limit); 1715 test->bitrate_limit_exceeded = 1; 1716 } 1717 } 1718 1719 int 1720 iperf_send(struct iperf_test *test, fd_set *write_setP) 1721 { 1722 register int multisend, r, streams_active; 1723 register struct iperf_stream *sp; 1724 struct iperf_time now; 1725 int no_throttle_check; 1726 1727 /* Can we do multisend mode? */ 1728 if (test->settings->burst != 0) 1729 multisend = test->settings->burst; 1730 else if (test->settings->rate == 0) 1731 multisend = test->multisend; 1732 else 1733 multisend = 1; /* nope */ 1734 1735 /* Should bitrate throttle be checked for every send */ 1736 no_throttle_check = test->settings->rate != 0 && test->settings->burst == 0; 1737 1738 for (; multisend > 0; --multisend) { 1739 if (no_throttle_check) 1740 iperf_time_now(&now); 1741 streams_active = 0; 1742 SLIST_FOREACH(sp, &test->streams, streams) { 1743 if ((sp->green_light && sp->sender && 1744 (write_setP == NULL || FD_ISSET(sp->socket, write_setP)))) { 1745 if (multisend > 1 && test->settings->bytes != 0 && test->bytes_sent >= test->settings->bytes) 1746 break; 1747 if (multisend > 1 && test->settings->blocks != 0 && test->blocks_sent >= test->settings->blocks) 1748 break; 1749 if ((r = sp->snd(sp)) < 0) { 1750 if (r == NET_SOFTERROR) 1751 break; 1752 i_errno = IESTREAMWRITE; 1753 return r; 1754 } 1755 streams_active = 1; 1756 test->bytes_sent += r; 1757 if (!sp->pending_size) 1758 ++test->blocks_sent; 1759 if (no_throttle_check) 1760 iperf_check_throttle(sp, &now); 1761 } 1762 } 1763 if (!streams_active) 1764 break; 1765 } 1766 if (!no_throttle_check) { /* Throttle check if was not checked for each send */ 1767 iperf_time_now(&now); 1768 SLIST_FOREACH(sp, &test->streams, streams) 1769 if (sp->sender) 1770 iperf_check_throttle(sp, &now); 1771 } 1772 if (write_setP != NULL) 1773 SLIST_FOREACH(sp, &test->streams, streams) 1774 if (FD_ISSET(sp->socket, write_setP)) 1775 FD_CLR(sp->socket, write_setP); 1776 1777 return 0; 1778 } 1779 1780 int 1781 iperf_recv(struct iperf_test *test, fd_set *read_setP) 1782 { 1783 int r; 1784 struct iperf_stream *sp; 1785 1786 SLIST_FOREACH(sp, &test->streams, streams) { 1787 if (FD_ISSET(sp->socket, read_setP) && !sp->sender) { 1788 if ((r = sp->rcv(sp)) < 0) { 1789 i_errno = IESTREAMREAD; 1790 return r; 1791 } 1792 test->bytes_received += r; 1793 ++test->blocks_received; 1794 FD_CLR(sp->socket, read_setP); 1795 } 1796 } 1797 1798 return 0; 1799 } 1800 1801 int 1802 iperf_init_test(struct iperf_test *test) 1803 { 1804 struct iperf_time now; 1805 struct iperf_stream *sp; 1806 1807 if (test->protocol->init) { 1808 if (test->protocol->init(test) < 0) 1809 return -1; 1810 } 1811 1812 /* Init each stream. */ 1813 if (iperf_time_now(&now) < 0) { 1814 i_errno = IEINITTEST; 1815 return -1; 1816 } 1817 SLIST_FOREACH(sp, &test->streams, streams) { 1818 sp->result->start_time = sp->result->start_time_fixed = now; 1819 } 1820 1821 if (test->on_test_start) 1822 test->on_test_start(test); 1823 1824 return 0; 1825 } 1826 1827 static void 1828 send_timer_proc(TimerClientData client_data, struct iperf_time *nowP) 1829 { 1830 struct iperf_stream *sp = client_data.p; 1831 1832 /* All we do here is set or clear the flag saying that this stream may 1833 ** be sent to. The actual sending gets done in the send proc, after 1834 ** checking the flag. 1835 */ 1836 iperf_check_throttle(sp, nowP); 1837 } 1838 1839 int 1840 iperf_create_send_timers(struct iperf_test * test) 1841 { 1842 struct iperf_time now; 1843 struct iperf_stream *sp; 1844 TimerClientData cd; 1845 1846 if (iperf_time_now(&now) < 0) { 1847 i_errno = IEINITTEST; 1848 return -1; 1849 } 1850 SLIST_FOREACH(sp, &test->streams, streams) { 1851 sp->green_light = 1; 1852 if (test->settings->rate != 0 && sp->sender) { 1853 cd.p = sp; 1854 sp->send_timer = tmr_create(NULL, send_timer_proc, cd, test->settings->pacing_timer, 1); 1855 if (sp->send_timer == NULL) { 1856 i_errno = IEINITTEST; 1857 return -1; 1858 } 1859 } 1860 } 1861 return 0; 1862 } 1863 1864 #if defined(HAVE_SSL) 1865 int test_is_authorized(struct iperf_test *test){ 1866 if ( !(test->server_rsa_private_key && test->server_authorized_users)) { 1867 return 0; 1868 } 1869 1870 if (test->settings->authtoken){ 1871 char *username = NULL, *password = NULL; 1872 time_t ts; 1873 int rc = decode_auth_setting(test->debug, test->settings->authtoken, test->server_rsa_private_key, &username, &password, &ts); 1874 if (rc) { 1875 return -1; 1876 } 1877 int ret = check_authentication(username, password, ts, test->server_authorized_users, test->server_skew_threshold); 1878 if (ret == 0){ 1879 if (test->debug) { 1880 iperf_printf(test, report_authentication_succeeded, username, ts); 1881 } 1882 free(username); 1883 free(password); 1884 return 0; 1885 } else { 1886 if (test->debug) { 1887 iperf_printf(test, report_authentication_failed, username, ts); 1888 } 1889 free(username); 1890 free(password); 1891 return -1; 1892 } 1893 } 1894 return -1; 1895 } 1896 #endif //HAVE_SSL 1897 1898 /** 1899 * iperf_exchange_parameters - handles the param_Exchange part for client 1900 * 1901 */ 1902 1903 int 1904 iperf_exchange_parameters(struct iperf_test *test) 1905 { 1906 int s; 1907 int32_t err; 1908 1909 if (test->role == 'c') { 1910 1911 if (send_parameters(test) < 0) 1912 return -1; 1913 1914 } else { 1915 1916 if (get_parameters(test) < 0) 1917 return -1; 1918 1919 #if defined(HAVE_SSL) 1920 if (test_is_authorized(test) < 0){ 1921 if (iperf_set_send_state(test, SERVER_ERROR) != 0) 1922 return -1; 1923 i_errno = IEAUTHTEST; 1924 err = htonl(i_errno); 1925 if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) { 1926 i_errno = IECTRLWRITE; 1927 return -1; 1928 } 1929 return -1; 1930 } 1931 #endif //HAVE_SSL 1932 1933 if ((s = test->protocol->listen(test)) < 0) { 1934 if (iperf_set_send_state(test, SERVER_ERROR) != 0) 1935 return -1; 1936 err = htonl(i_errno); 1937 if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) { 1938 i_errno = IECTRLWRITE; 1939 return -1; 1940 } 1941 err = htonl(errno); 1942 if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) { 1943 i_errno = IECTRLWRITE; 1944 return -1; 1945 } 1946 return -1; 1947 } 1948 1949 FD_SET(s, &test->read_set); 1950 test->max_fd = (s > test->max_fd) ? s : test->max_fd; 1951 test->prot_listener = s; 1952 1953 // Send the control message to create streams and start the test 1954 if (iperf_set_send_state(test, CREATE_STREAMS) != 0) 1955 return -1; 1956 1957 } 1958 1959 return 0; 1960 } 1961 1962 /*************************************************************/ 1963 1964 int 1965 iperf_exchange_results(struct iperf_test *test) 1966 { 1967 if (test->role == 'c') { 1968 /* Send results to server. */ 1969 if (send_results(test) < 0) 1970 return -1; 1971 /* Get server results. */ 1972 if (get_results(test) < 0) 1973 return -1; 1974 } else { 1975 /* Get client results. */ 1976 if (get_results(test) < 0) 1977 return -1; 1978 /* Send results to client. */ 1979 if (send_results(test) < 0) 1980 return -1; 1981 } 1982 return 0; 1983 } 1984 1985 /*************************************************************/ 1986 1987 static int 1988 send_parameters(struct iperf_test *test) 1989 { 1990 int r = 0; 1991 cJSON *j; 1992 1993 j = cJSON_CreateObject(); 1994 if (j == NULL) { 1995 i_errno = IESENDPARAMS; 1996 r = -1; 1997 } else { 1998 if (test->protocol->id == Ptcp) 1999 cJSON_AddTrueToObject(j, "tcp"); 2000 else if (test->protocol->id == Pudp) 2001 cJSON_AddTrueToObject(j, "udp"); 2002 else if (test->protocol->id == Psctp) 2003 cJSON_AddTrueToObject(j, "sctp"); 2004 cJSON_AddNumberToObject(j, "omit", test->omit); 2005 if (test->server_affinity != -1) 2006 cJSON_AddNumberToObject(j, "server_affinity", test->server_affinity); 2007 cJSON_AddNumberToObject(j, "time", test->duration); 2008 if (test->settings->bytes) 2009 cJSON_AddNumberToObject(j, "num", test->settings->bytes); 2010 if (test->settings->blocks) 2011 cJSON_AddNumberToObject(j, "blockcount", test->settings->blocks); 2012 if (test->settings->mss) 2013 cJSON_AddNumberToObject(j, "MSS", test->settings->mss); 2014 if (test->no_delay) 2015 cJSON_AddTrueToObject(j, "nodelay"); 2016 cJSON_AddNumberToObject(j, "parallel", test->num_streams); 2017 if (test->reverse) 2018 cJSON_AddTrueToObject(j, "reverse"); 2019 if (test->bidirectional) 2020 cJSON_AddTrueToObject(j, "bidirectional"); 2021 if (test->settings->socket_bufsize) 2022 cJSON_AddNumberToObject(j, "window", test->settings->socket_bufsize); 2023 if (test->settings->blksize) 2024 cJSON_AddNumberToObject(j, "len", test->settings->blksize); 2025 if (test->settings->rate) 2026 cJSON_AddNumberToObject(j, "bandwidth", test->settings->rate); 2027 if (test->settings->fqrate) 2028 cJSON_AddNumberToObject(j, "fqrate", test->settings->fqrate); 2029 if (test->settings->pacing_timer) 2030 cJSON_AddNumberToObject(j, "pacing_timer", test->settings->pacing_timer); 2031 if (test->settings->burst) 2032 cJSON_AddNumberToObject(j, "burst", test->settings->burst); 2033 if (test->settings->tos) 2034 cJSON_AddNumberToObject(j, "TOS", test->settings->tos); 2035 if (test->settings->flowlabel) 2036 cJSON_AddNumberToObject(j, "flowlabel", test->settings->flowlabel); 2037 if (test->title) 2038 cJSON_AddStringToObject(j, "title", test->title); 2039 if (test->extra_data) 2040 cJSON_AddStringToObject(j, "extra_data", test->extra_data); 2041 if (test->congestion) 2042 cJSON_AddStringToObject(j, "congestion", test->congestion); 2043 if (test->congestion_used) 2044 cJSON_AddStringToObject(j, "congestion_used", test->congestion_used); 2045 if (test->get_server_output) 2046 cJSON_AddNumberToObject(j, "get_server_output", iperf_get_test_get_server_output(test)); 2047 if (test->udp_counters_64bit) 2048 cJSON_AddNumberToObject(j, "udp_counters_64bit", iperf_get_test_udp_counters_64bit(test)); 2049 if (test->repeating_payload) 2050 cJSON_AddNumberToObject(j, "repeating_payload", test->repeating_payload); 2051 #if defined(HAVE_DONT_FRAGMENT) 2052 if (test->settings->dont_fragment) 2053 cJSON_AddNumberToObject(j, "dont_fragment", test->settings->dont_fragment); 2054 #endif /* HAVE_DONT_FRAGMENT */ 2055 #if defined(HAVE_SSL) 2056 /* Send authentication parameters */ 2057 if (test->settings->client_username && test->settings->client_password && test->settings->client_rsa_pubkey){ 2058 int rc = encode_auth_setting(test->settings->client_username, test->settings->client_password, test->settings->client_rsa_pubkey, &test->settings->authtoken); 2059 2060 if (rc) { 2061 cJSON_Delete(j); 2062 i_errno = IESENDPARAMS; 2063 return -1; 2064 } 2065 2066 cJSON_AddStringToObject(j, "authtoken", test->settings->authtoken); 2067 } 2068 #endif // HAVE_SSL 2069 cJSON_AddStringToObject(j, "client_version", IPERF_VERSION); 2070 2071 if (test->debug) { 2072 char *str = cJSON_Print(j); 2073 printf("send_parameters:\n%s\n", str); 2074 cJSON_free(str); 2075 } 2076 2077 if (JSON_write(test->ctrl_sck, j) < 0) { 2078 i_errno = IESENDPARAMS; 2079 r = -1; 2080 } 2081 cJSON_Delete(j); 2082 } 2083 return r; 2084 } 2085 2086 /*************************************************************/ 2087 2088 static int 2089 get_parameters(struct iperf_test *test) 2090 { 2091 int r = 0; 2092 cJSON *j; 2093 cJSON *j_p; 2094 2095 j = JSON_read(test->ctrl_sck); 2096 if (j == NULL) { 2097 i_errno = IERECVPARAMS; 2098 r = -1; 2099 } else { 2100 if (test->debug) { 2101 char *str; 2102 str = cJSON_Print(j); 2103 printf("get_parameters:\n%s\n", str ); 2104 cJSON_free(str); 2105 } 2106 2107 if ((j_p = cJSON_GetObjectItem(j, "tcp")) != NULL) 2108 set_protocol(test, Ptcp); 2109 if ((j_p = cJSON_GetObjectItem(j, "udp")) != NULL) 2110 set_protocol(test, Pudp); 2111 if ((j_p = cJSON_GetObjectItem(j, "sctp")) != NULL) 2112 set_protocol(test, Psctp); 2113 if ((j_p = cJSON_GetObjectItem(j, "omit")) != NULL) 2114 test->omit = j_p->valueint; 2115 if ((j_p = cJSON_GetObjectItem(j, "server_affinity")) != NULL) 2116 test->server_affinity = j_p->valueint; 2117 if ((j_p = cJSON_GetObjectItem(j, "time")) != NULL) 2118 test->duration = j_p->valueint; 2119 if ((j_p = cJSON_GetObjectItem(j, "num")) != NULL) 2120 test->settings->bytes = j_p->valueint; 2121 if ((j_p = cJSON_GetObjectItem(j, "blockcount")) != NULL) 2122 test->settings->blocks = j_p->valueint; 2123 if ((j_p = cJSON_GetObjectItem(j, "MSS")) != NULL) 2124 test->settings->mss = j_p->valueint; 2125 if ((j_p = cJSON_GetObjectItem(j, "nodelay")) != NULL) 2126 test->no_delay = 1; 2127 if ((j_p = cJSON_GetObjectItem(j, "parallel")) != NULL) 2128 test->num_streams = j_p->valueint; 2129 if ((j_p = cJSON_GetObjectItem(j, "reverse")) != NULL) 2130 iperf_set_test_reverse(test, 1); 2131 if ((j_p = cJSON_GetObjectItem(j, "bidirectional")) != NULL) 2132 iperf_set_test_bidirectional(test, 1); 2133 if ((j_p = cJSON_GetObjectItem(j, "window")) != NULL) 2134 test->settings->socket_bufsize = j_p->valueint; 2135 if ((j_p = cJSON_GetObjectItem(j, "len")) != NULL) 2136 test->settings->blksize = j_p->valueint; 2137 if ((j_p = cJSON_GetObjectItem(j, "bandwidth")) != NULL) 2138 test->settings->rate = j_p->valueint; 2139 if ((j_p = cJSON_GetObjectItem(j, "fqrate")) != NULL) 2140 test->settings->fqrate = j_p->valueint; 2141 if ((j_p = cJSON_GetObjectItem(j, "pacing_timer")) != NULL) 2142 test->settings->pacing_timer = j_p->valueint; 2143 if ((j_p = cJSON_GetObjectItem(j, "burst")) != NULL) 2144 test->settings->burst = j_p->valueint; 2145 if ((j_p = cJSON_GetObjectItem(j, "TOS")) != NULL) 2146 test->settings->tos = j_p->valueint; 2147 if ((j_p = cJSON_GetObjectItem(j, "flowlabel")) != NULL) 2148 test->settings->flowlabel = j_p->valueint; 2149 if ((j_p = cJSON_GetObjectItem(j, "title")) != NULL) 2150 test->title = strdup(j_p->valuestring); 2151 if ((j_p = cJSON_GetObjectItem(j, "extra_data")) != NULL) 2152 test->extra_data = strdup(j_p->valuestring); 2153 if ((j_p = cJSON_GetObjectItem(j, "congestion")) != NULL) 2154 test->congestion = strdup(j_p->valuestring); 2155 if ((j_p = cJSON_GetObjectItem(j, "congestion_used")) != NULL) 2156 test->congestion_used = strdup(j_p->valuestring); 2157 if ((j_p = cJSON_GetObjectItem(j, "get_server_output")) != NULL) 2158 iperf_set_test_get_server_output(test, 1); 2159 if ((j_p = cJSON_GetObjectItem(j, "udp_counters_64bit")) != NULL) 2160 iperf_set_test_udp_counters_64bit(test, 1); 2161 if ((j_p = cJSON_GetObjectItem(j, "repeating_payload")) != NULL) 2162 test->repeating_payload = 1; 2163 #if defined(HAVE_DONT_FRAGMENT) 2164 if ((j_p = cJSON_GetObjectItem(j, "dont_fragment")) != NULL) 2165 test->settings->dont_fragment = j_p->valueint; 2166 #endif /* HAVE_DONT_FRAGMENT */ 2167 #if defined(HAVE_SSL) 2168 if ((j_p = cJSON_GetObjectItem(j, "authtoken")) != NULL) 2169 test->settings->authtoken = strdup(j_p->valuestring); 2170 #endif //HAVE_SSL 2171 if (test->mode && test->protocol->id == Ptcp && has_tcpinfo_retransmits()) 2172 test->sender_has_retransmits = 1; 2173 if (test->settings->rate) 2174 cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate); 2175 cJSON_Delete(j); 2176 } 2177 return r; 2178 } 2179 2180 /*************************************************************/ 2181 2182 static int 2183 send_results(struct iperf_test *test) 2184 { 2185 int r = 0; 2186 cJSON *j; 2187 cJSON *j_streams; 2188 struct iperf_stream *sp; 2189 cJSON *j_stream; 2190 int sender_has_retransmits; 2191 iperf_size_t bytes_transferred; 2192 int retransmits; 2193 struct iperf_time temp_time; 2194 double start_time, end_time; 2195 2196 j = cJSON_CreateObject(); 2197 if (j == NULL) { 2198 i_errno = IEPACKAGERESULTS; 2199 r = -1; 2200 } else { 2201 cJSON_AddNumberToObject(j, "cpu_util_total", test->cpu_util[0]); 2202 cJSON_AddNumberToObject(j, "cpu_util_user", test->cpu_util[1]); 2203 cJSON_AddNumberToObject(j, "cpu_util_system", test->cpu_util[2]); 2204 if ( test->mode == RECEIVER ) 2205 sender_has_retransmits = -1; 2206 else 2207 sender_has_retransmits = test->sender_has_retransmits; 2208 cJSON_AddNumberToObject(j, "sender_has_retransmits", sender_has_retransmits); 2209 if ( test->congestion_used ) { 2210 cJSON_AddStringToObject(j, "congestion_used", test->congestion_used); 2211 } 2212 2213 /* If on the server and sending server output, then do this */ 2214 if (test->role == 's' && test->get_server_output) { 2215 if (test->json_output) { 2216 /* Add JSON output */ 2217 cJSON_AddItemReferenceToObject(j, "server_output_json", test->json_top); 2218 } 2219 else { 2220 /* Add textual output */ 2221 size_t buflen = 0; 2222 2223 /* Figure out how much room we need to hold the complete output string */ 2224 struct iperf_textline *t; 2225 TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) { 2226 buflen += strlen(t->line); 2227 } 2228 2229 /* Allocate and build it up from the component lines */ 2230 char *output = calloc(buflen + 1, 1); 2231 TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) { 2232 strncat(output, t->line, buflen); 2233 buflen -= strlen(t->line); 2234 } 2235 2236 cJSON_AddStringToObject(j, "server_output_text", output); 2237 free(output); 2238 } 2239 } 2240 2241 j_streams = cJSON_CreateArray(); 2242 if (j_streams == NULL) { 2243 i_errno = IEPACKAGERESULTS; 2244 r = -1; 2245 } else { 2246 cJSON_AddItemToObject(j, "streams", j_streams); 2247 SLIST_FOREACH(sp, &test->streams, streams) { 2248 j_stream = cJSON_CreateObject(); 2249 if (j_stream == NULL) { 2250 i_errno = IEPACKAGERESULTS; 2251 r = -1; 2252 } else { 2253 cJSON_AddItemToArray(j_streams, j_stream); 2254 bytes_transferred = sp->sender ? (sp->result->bytes_sent - sp->result->bytes_sent_omit) : sp->result->bytes_received; 2255 retransmits = (sp->sender && test->sender_has_retransmits) ? sp->result->stream_retrans : -1; 2256 cJSON_AddNumberToObject(j_stream, "id", sp->id); 2257 cJSON_AddNumberToObject(j_stream, "bytes", bytes_transferred); 2258 cJSON_AddNumberToObject(j_stream, "retransmits", retransmits); 2259 cJSON_AddNumberToObject(j_stream, "jitter", sp->jitter); 2260 cJSON_AddNumberToObject(j_stream, "errors", sp->cnt_error); 2261 cJSON_AddNumberToObject(j_stream, "packets", sp->packet_count); 2262 2263 iperf_time_diff(&sp->result->start_time, &sp->result->start_time, &temp_time); 2264 start_time = iperf_time_in_secs(&temp_time); 2265 iperf_time_diff(&sp->result->start_time, &sp->result->end_time, &temp_time); 2266 end_time = iperf_time_in_secs(&temp_time); 2267 cJSON_AddNumberToObject(j_stream, "start_time", start_time); 2268 cJSON_AddNumberToObject(j_stream, "end_time", end_time); 2269 2270 } 2271 } 2272 if (r == 0 && test->debug) { 2273 char *str = cJSON_Print(j); 2274 printf("send_results\n%s\n", str); 2275 cJSON_free(str); 2276 } 2277 if (r == 0 && JSON_write(test->ctrl_sck, j) < 0) { 2278 i_errno = IESENDRESULTS; 2279 r = -1; 2280 } 2281 } 2282 cJSON_Delete(j); 2283 } 2284 return r; 2285 } 2286 2287 /*************************************************************/ 2288 2289 static int 2290 get_results(struct iperf_test *test) 2291 { 2292 int r = 0; 2293 cJSON *j; 2294 cJSON *j_cpu_util_total; 2295 cJSON *j_cpu_util_user; 2296 cJSON *j_cpu_util_system; 2297 cJSON *j_remote_congestion_used; 2298 cJSON *j_sender_has_retransmits; 2299 int result_has_retransmits; 2300 cJSON *j_streams; 2301 int n, i; 2302 cJSON *j_stream; 2303 cJSON *j_id; 2304 cJSON *j_bytes; 2305 cJSON *j_retransmits; 2306 cJSON *j_jitter; 2307 cJSON *j_errors; 2308 cJSON *j_packets; 2309 cJSON *j_server_output; 2310 cJSON *j_start_time, *j_end_time; 2311 int sid, cerror, pcount; 2312 double jitter; 2313 iperf_size_t bytes_transferred; 2314 int retransmits; 2315 struct iperf_stream *sp; 2316 2317 j = JSON_read(test->ctrl_sck); 2318 if (j == NULL) { 2319 i_errno = IERECVRESULTS; 2320 r = -1; 2321 } else { 2322 j_cpu_util_total = cJSON_GetObjectItem(j, "cpu_util_total"); 2323 j_cpu_util_user = cJSON_GetObjectItem(j, "cpu_util_user"); 2324 j_cpu_util_system = cJSON_GetObjectItem(j, "cpu_util_system"); 2325 j_sender_has_retransmits = cJSON_GetObjectItem(j, "sender_has_retransmits"); 2326 if (j_cpu_util_total == NULL || j_cpu_util_user == NULL || j_cpu_util_system == NULL || j_sender_has_retransmits == NULL) { 2327 i_errno = IERECVRESULTS; 2328 r = -1; 2329 } else { 2330 if (test->debug) { 2331 char *str = cJSON_Print(j); 2332 printf("get_results\n%s\n", str); 2333 cJSON_free(str); 2334 } 2335 2336 test->remote_cpu_util[0] = j_cpu_util_total->valuedouble; 2337 test->remote_cpu_util[1] = j_cpu_util_user->valuedouble; 2338 test->remote_cpu_util[2] = j_cpu_util_system->valuedouble; 2339 result_has_retransmits = j_sender_has_retransmits->valueint; 2340 if ( test->mode == RECEIVER ) { 2341 test->sender_has_retransmits = result_has_retransmits; 2342 test->other_side_has_retransmits = 0; 2343 } 2344 else if ( test->mode == BIDIRECTIONAL ) 2345 test->other_side_has_retransmits = result_has_retransmits; 2346 2347 j_streams = cJSON_GetObjectItem(j, "streams"); 2348 if (j_streams == NULL) { 2349 i_errno = IERECVRESULTS; 2350 r = -1; 2351 } else { 2352 n = cJSON_GetArraySize(j_streams); 2353 for (i=0; i<n; ++i) { 2354 j_stream = cJSON_GetArrayItem(j_streams, i); 2355 if (j_stream == NULL) { 2356 i_errno = IERECVRESULTS; 2357 r = -1; 2358 } else { 2359 j_id = cJSON_GetObjectItem(j_stream, "id"); 2360 j_bytes = cJSON_GetObjectItem(j_stream, "bytes"); 2361 j_retransmits = cJSON_GetObjectItem(j_stream, "retransmits"); 2362 j_jitter = cJSON_GetObjectItem(j_stream, "jitter"); 2363 j_errors = cJSON_GetObjectItem(j_stream, "errors"); 2364 j_packets = cJSON_GetObjectItem(j_stream, "packets"); 2365 j_start_time = cJSON_GetObjectItem(j_stream, "start_time"); 2366 j_end_time = cJSON_GetObjectItem(j_stream, "end_time"); 2367 if (j_id == NULL || j_bytes == NULL || j_retransmits == NULL || j_jitter == NULL || j_errors == NULL || j_packets == NULL) { 2368 i_errno = IERECVRESULTS; 2369 r = -1; 2370 } else { 2371 sid = j_id->valueint; 2372 bytes_transferred = j_bytes->valueint; 2373 retransmits = j_retransmits->valueint; 2374 jitter = j_jitter->valuedouble; 2375 cerror = j_errors->valueint; 2376 pcount = j_packets->valueint; 2377 SLIST_FOREACH(sp, &test->streams, streams) 2378 if (sp->id == sid) break; 2379 if (sp == NULL) { 2380 i_errno = IESTREAMID; 2381 r = -1; 2382 } else { 2383 if (sp->sender) { 2384 sp->jitter = jitter; 2385 sp->cnt_error = cerror; 2386 sp->peer_packet_count = pcount; 2387 sp->result->bytes_received = bytes_transferred; 2388 /* 2389 * We have to handle the possibility that 2390 * start_time and end_time might not be 2391 * available; this is the case for older (pre-3.2) 2392 * servers. 2393 * 2394 * We need to have result structure members to hold 2395 * the both sides' start_time and end_time. 2396 */ 2397 if (j_start_time && j_end_time) { 2398 sp->result->receiver_time = j_end_time->valuedouble - j_start_time->valuedouble; 2399 } 2400 else { 2401 sp->result->receiver_time = 0.0; 2402 } 2403 } else { 2404 sp->peer_packet_count = pcount; 2405 sp->result->bytes_sent = bytes_transferred; 2406 sp->result->stream_retrans = retransmits; 2407 if (j_start_time && j_end_time) { 2408 sp->result->sender_time = j_end_time->valuedouble - j_start_time->valuedouble; 2409 } 2410 else { 2411 sp->result->sender_time = 0.0; 2412 } 2413 } 2414 } 2415 } 2416 } 2417 } 2418 /* 2419 * If we're the client and we're supposed to get remote results, 2420 * look them up and process accordingly. 2421 */ 2422 if (test->role == 'c' && iperf_get_test_get_server_output(test)) { 2423 /* Look for JSON. If we find it, grab the object so it doesn't get deleted. */ 2424 j_server_output = cJSON_DetachItemFromObject(j, "server_output_json"); 2425 if (j_server_output != NULL) { 2426 test->json_server_output = j_server_output; 2427 } 2428 else { 2429 /* No JSON, look for textual output. Make a copy of the text for later. */ 2430 j_server_output = cJSON_GetObjectItem(j, "server_output_text"); 2431 if (j_server_output != NULL) { 2432 test->server_output_text = strdup(j_server_output->valuestring); 2433 } 2434 } 2435 } 2436 } 2437 } 2438 2439 j_remote_congestion_used = cJSON_GetObjectItem(j, "congestion_used"); 2440 if (j_remote_congestion_used != NULL) { 2441 test->remote_congestion_used = strdup(j_remote_congestion_used->valuestring); 2442 } 2443 2444 cJSON_Delete(j); 2445 } 2446 return r; 2447 } 2448 2449 /*************************************************************/ 2450 2451 static int 2452 JSON_write(int fd, cJSON *json) 2453 { 2454 uint32_t hsize, nsize; 2455 char *str; 2456 int r = 0; 2457 2458 str = cJSON_PrintUnformatted(json); 2459 if (str == NULL) 2460 r = -1; 2461 else { 2462 hsize = strlen(str); 2463 nsize = htonl(hsize); 2464 if (Nwrite(fd, (char*) &nsize, sizeof(nsize), Ptcp) < 0) 2465 r = -1; 2466 else { 2467 if (Nwrite(fd, str, hsize, Ptcp) < 0) 2468 r = -1; 2469 } 2470 cJSON_free(str); 2471 } 2472 return r; 2473 } 2474 2475 /*************************************************************/ 2476 2477 static cJSON * 2478 JSON_read(int fd) 2479 { 2480 uint32_t hsize, nsize; 2481 char *str; 2482 cJSON *json = NULL; 2483 int rc; 2484 2485 /* 2486 * Read a four-byte integer, which is the length of the JSON to follow. 2487 * Then read the JSON into a buffer and parse it. Return a parsed JSON 2488 * structure, NULL if there was an error. 2489 */ 2490 if (Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp) >= 0) { 2491 hsize = ntohl(nsize); 2492 /* Allocate a buffer to hold the JSON */ 2493 str = (char *) calloc(sizeof(char), hsize+1); /* +1 for trailing null */ 2494 if (str != NULL) { 2495 rc = Nread(fd, str, hsize, Ptcp); 2496 if (rc >= 0) { 2497 /* 2498 * We should be reading in the number of bytes corresponding to the 2499 * length in that 4-byte integer. If we don't the socket might have 2500 * prematurely closed. Only do the JSON parsing if we got the 2501 * correct number of bytes. 2502 */ 2503 if (rc == hsize) { 2504 json = cJSON_Parse(str); 2505 } 2506 else { 2507 printf("WARNING: Size of data read does not correspond to offered length\n"); 2508 } 2509 } 2510 } 2511 free(str); 2512 } 2513 return json; 2514 } 2515 2516 /*************************************************************/ 2517 /** 2518 * add_to_interval_list -- adds new interval to the interval_list 2519 */ 2520 2521 void 2522 add_to_interval_list(struct iperf_stream_result * rp, struct iperf_interval_results * new) 2523 { 2524 struct iperf_interval_results *irp; 2525 2526 irp = (struct iperf_interval_results *) malloc(sizeof(struct iperf_interval_results)); 2527 memcpy(irp, new, sizeof(struct iperf_interval_results)); 2528 TAILQ_INSERT_TAIL(&rp->interval_results, irp, irlistentries); 2529 } 2530 2531 2532 /************************************************************/ 2533 2534 /** 2535 * connect_msg -- displays connection message 2536 * denoting sender/receiver details 2537 * 2538 */ 2539 2540 void 2541 connect_msg(struct iperf_stream *sp) 2542 { 2543 char ipl[INET6_ADDRSTRLEN], ipr[INET6_ADDRSTRLEN]; 2544 int lport, rport; 2545 2546 if (getsockdomain(sp->socket) == AF_INET) { 2547 inet_ntop(AF_INET, (void *) &((struct sockaddr_in *) &sp->local_addr)->sin_addr, ipl, sizeof(ipl)); 2548 mapped_v4_to_regular_v4(ipl); 2549 inet_ntop(AF_INET, (void *) &((struct sockaddr_in *) &sp->remote_addr)->sin_addr, ipr, sizeof(ipr)); 2550 mapped_v4_to_regular_v4(ipr); 2551 lport = ntohs(((struct sockaddr_in *) &sp->local_addr)->sin_port); 2552 rport = ntohs(((struct sockaddr_in *) &sp->remote_addr)->sin_port); 2553 } else { 2554 inet_ntop(AF_INET6, (void *) &((struct sockaddr_in6 *) &sp->local_addr)->sin6_addr, ipl, sizeof(ipl)); 2555 mapped_v4_to_regular_v4(ipl); 2556 inet_ntop(AF_INET6, (void *) &((struct sockaddr_in6 *) &sp->remote_addr)->sin6_addr, ipr, sizeof(ipr)); 2557 mapped_v4_to_regular_v4(ipr); 2558 lport = ntohs(((struct sockaddr_in6 *) &sp->local_addr)->sin6_port); 2559 rport = ntohs(((struct sockaddr_in6 *) &sp->remote_addr)->sin6_port); 2560 } 2561 2562 if (sp->test->json_output) 2563 cJSON_AddItemToArray(sp->test->json_connected, iperf_json_printf("socket: %d local_host: %s local_port: %d remote_host: %s remote_port: %d", (int64_t) sp->socket, ipl, (int64_t) lport, ipr, (int64_t) rport)); 2564 else 2565 iperf_printf(sp->test, report_connected, sp->socket, ipl, lport, ipr, rport); 2566 } 2567 2568 2569 /**************************************************************************/ 2570 2571 struct iperf_test * 2572 iperf_new_test() 2573 { 2574 struct iperf_test *test; 2575 2576 test = (struct iperf_test *) malloc(sizeof(struct iperf_test)); 2577 if (!test) { 2578 i_errno = IENEWTEST; 2579 return NULL; 2580 } 2581 /* initialize everything to zero */ 2582 memset(test, 0, sizeof(struct iperf_test)); 2583 2584 test->settings = (struct iperf_settings *) malloc(sizeof(struct iperf_settings)); 2585 if (!test->settings) { 2586 free(test); 2587 i_errno = IENEWTEST; 2588 return NULL; 2589 } 2590 memset(test->settings, 0, sizeof(struct iperf_settings)); 2591 2592 test->bitrate_limit_intervals_traffic_bytes = (iperf_size_t *) malloc(sizeof(iperf_size_t) * MAX_INTERVAL); 2593 if (!test->bitrate_limit_intervals_traffic_bytes) { 2594 free(test); 2595 i_errno = IENEWTEST; 2596 return NULL; 2597 } 2598 memset(test->bitrate_limit_intervals_traffic_bytes, 0, sizeof(sizeof(iperf_size_t) * MAX_INTERVAL)); 2599 2600 /* By default all output goes to stdout */ 2601 test->outfile = stdout; 2602 2603 return test; 2604 } 2605 2606 /**************************************************************************/ 2607 2608 struct protocol * 2609 protocol_new(void) 2610 { 2611 struct protocol *proto; 2612 2613 proto = malloc(sizeof(struct protocol)); 2614 if(!proto) { 2615 return NULL; 2616 } 2617 memset(proto, 0, sizeof(struct protocol)); 2618 2619 return proto; 2620 } 2621 2622 void 2623 protocol_free(struct protocol *proto) 2624 { 2625 free(proto); 2626 } 2627 2628 /**************************************************************************/ 2629 int 2630 iperf_defaults(struct iperf_test *testp) 2631 { 2632 struct protocol *tcp, *udp; 2633 #if defined(HAVE_SCTP_H) 2634 struct protocol *sctp; 2635 #endif /* HAVE_SCTP_H */ 2636 2637 testp->omit = OMIT; 2638 testp->duration = DURATION; 2639 testp->diskfile_name = (char*) 0; 2640 testp->affinity = -1; 2641 testp->server_affinity = -1; 2642 TAILQ_INIT(&testp->xbind_addrs); 2643 #if defined(HAVE_CPUSET_SETAFFINITY) 2644 CPU_ZERO(&testp->cpumask); 2645 #endif /* HAVE_CPUSET_SETAFFINITY */ 2646 testp->title = NULL; 2647 testp->extra_data = NULL; 2648 testp->congestion = NULL; 2649 testp->congestion_used = NULL; 2650 testp->remote_congestion_used = NULL; 2651 testp->server_port = PORT; 2652 testp->ctrl_sck = -1; 2653 testp->prot_listener = -1; 2654 testp->other_side_has_retransmits = 0; 2655 2656 testp->stats_callback = iperf_stats_callback; 2657 testp->reporter_callback = iperf_reporter_callback; 2658 2659 testp->stats_interval = testp->reporter_interval = 1; 2660 testp->num_streams = 1; 2661 2662 testp->settings->domain = AF_UNSPEC; 2663 testp->settings->unit_format = 'a'; 2664 testp->settings->socket_bufsize = 0; /* use autotuning */ 2665 testp->settings->blksize = DEFAULT_TCP_BLKSIZE; 2666 testp->settings->rate = 0; 2667 testp->settings->bitrate_limit = 0; 2668 testp->settings->bitrate_limit_interval = 5; 2669 testp->settings->bitrate_limit_stats_per_interval = 0; 2670 testp->settings->fqrate = 0; 2671 testp->settings->pacing_timer = DEFAULT_PACING_TIMER; 2672 testp->settings->burst = 0; 2673 testp->settings->mss = 0; 2674 testp->settings->bytes = 0; 2675 testp->settings->blocks = 0; 2676 testp->settings->connect_timeout = -1; 2677 testp->settings->rcv_timeout.secs = DEFAULT_NO_MSG_RCVD_TIMEOUT / SEC_TO_mS; 2678 testp->settings->rcv_timeout.usecs = (DEFAULT_NO_MSG_RCVD_TIMEOUT % SEC_TO_mS) * mS_TO_US; 2679 2680 memset(testp->cookie, 0, COOKIE_SIZE); 2681 2682 testp->multisend = 10; /* arbitrary */ 2683 2684 /* Set up protocol list */ 2685 SLIST_INIT(&testp->streams); 2686 SLIST_INIT(&testp->protocols); 2687 2688 tcp = protocol_new(); 2689 if (!tcp) 2690 return -1; 2691 2692 tcp->id = Ptcp; 2693 tcp->name = "TCP"; 2694 tcp->accept = iperf_tcp_accept; 2695 tcp->listen = iperf_tcp_listen; 2696 tcp->connect = iperf_tcp_connect; 2697 tcp->send = iperf_tcp_send; 2698 tcp->recv = iperf_tcp_recv; 2699 tcp->init = NULL; 2700 SLIST_INSERT_HEAD(&testp->protocols, tcp, protocols); 2701 2702 udp = protocol_new(); 2703 if (!udp) { 2704 protocol_free(tcp); 2705 return -1; 2706 } 2707 2708 udp->id = Pudp; 2709 udp->name = "UDP"; 2710 udp->accept = iperf_udp_accept; 2711 udp->listen = iperf_udp_listen; 2712 udp->connect = iperf_udp_connect; 2713 udp->send = iperf_udp_send; 2714 udp->recv = iperf_udp_recv; 2715 udp->init = iperf_udp_init; 2716 SLIST_INSERT_AFTER(tcp, udp, protocols); 2717 2718 set_protocol(testp, Ptcp); 2719 2720 #if defined(HAVE_SCTP_H) 2721 sctp = protocol_new(); 2722 if (!sctp) { 2723 protocol_free(tcp); 2724 protocol_free(udp); 2725 return -1; 2726 } 2727 2728 sctp->id = Psctp; 2729 sctp->name = "SCTP"; 2730 sctp->accept = iperf_sctp_accept; 2731 sctp->listen = iperf_sctp_listen; 2732 sctp->connect = iperf_sctp_connect; 2733 sctp->send = iperf_sctp_send; 2734 sctp->recv = iperf_sctp_recv; 2735 sctp->init = iperf_sctp_init; 2736 2737 SLIST_INSERT_AFTER(udp, sctp, protocols); 2738 #endif /* HAVE_SCTP_H */ 2739 2740 testp->on_new_stream = iperf_on_new_stream; 2741 testp->on_test_start = iperf_on_test_start; 2742 testp->on_connect = iperf_on_connect; 2743 testp->on_test_finish = iperf_on_test_finish; 2744 2745 TAILQ_INIT(&testp->server_output_list); 2746 2747 return 0; 2748 } 2749 2750 2751 /**************************************************************************/ 2752 void 2753 iperf_free_test(struct iperf_test *test) 2754 { 2755 struct protocol *prot; 2756 struct iperf_stream *sp; 2757 2758 /* Free streams */ 2759 while (!SLIST_EMPTY(&test->streams)) { 2760 sp = SLIST_FIRST(&test->streams); 2761 SLIST_REMOVE_HEAD(&test->streams, streams); 2762 iperf_free_stream(sp); 2763 } 2764 if (test->server_hostname) 2765 free(test->server_hostname); 2766 if (test->tmp_template) 2767 free(test->tmp_template); 2768 if (test->bind_address) 2769 free(test->bind_address); 2770 if (test->bind_dev) 2771 free(test->bind_dev); 2772 if (!TAILQ_EMPTY(&test->xbind_addrs)) { 2773 struct xbind_entry *xbe; 2774 2775 while (!TAILQ_EMPTY(&test->xbind_addrs)) { 2776 xbe = TAILQ_FIRST(&test->xbind_addrs); 2777 TAILQ_REMOVE(&test->xbind_addrs, xbe, link); 2778 if (xbe->ai) 2779 freeaddrinfo(xbe->ai); 2780 free(xbe->name); 2781 free(xbe); 2782 } 2783 } 2784 #if defined(HAVE_SSL) 2785 2786 if (test->server_rsa_private_key) 2787 EVP_PKEY_free(test->server_rsa_private_key); 2788 test->server_rsa_private_key = NULL; 2789 2790 free(test->settings->authtoken); 2791 test->settings->authtoken = NULL; 2792 2793 free(test->settings->client_username); 2794 test->settings->client_username = NULL; 2795 2796 free(test->settings->client_password); 2797 test->settings->client_password = NULL; 2798 2799 if (test->settings->client_rsa_pubkey) 2800 EVP_PKEY_free(test->settings->client_rsa_pubkey); 2801 test->settings->client_rsa_pubkey = NULL; 2802 #endif /* HAVE_SSL */ 2803 2804 if (test->settings) 2805 free(test->settings); 2806 if (test->title) 2807 free(test->title); 2808 if (test->extra_data) 2809 free(test->extra_data); 2810 if (test->congestion) 2811 free(test->congestion); 2812 if (test->congestion_used) 2813 free(test->congestion_used); 2814 if (test->remote_congestion_used) 2815 free(test->remote_congestion_used); 2816 if (test->timestamp_format) 2817 free(test->timestamp_format); 2818 if (test->omit_timer != NULL) 2819 tmr_cancel(test->omit_timer); 2820 if (test->timer != NULL) 2821 tmr_cancel(test->timer); 2822 if (test->stats_timer != NULL) 2823 tmr_cancel(test->stats_timer); 2824 if (test->reporter_timer != NULL) 2825 tmr_cancel(test->reporter_timer); 2826 2827 /* Free protocol list */ 2828 while (!SLIST_EMPTY(&test->protocols)) { 2829 prot = SLIST_FIRST(&test->protocols); 2830 SLIST_REMOVE_HEAD(&test->protocols, protocols); 2831 free(prot); 2832 } 2833 2834 if (test->logfile) { 2835 free(test->logfile); 2836 test->logfile = NULL; 2837 if (test->outfile) { 2838 fclose(test->outfile); 2839 test->outfile = NULL; 2840 } 2841 } 2842 2843 if (test->server_output_text) { 2844 free(test->server_output_text); 2845 test->server_output_text = NULL; 2846 } 2847 2848 if (test->json_output_string) { 2849 free(test->json_output_string); 2850 test->json_output_string = NULL; 2851 } 2852 2853 /* Free output line buffers, if any (on the server only) */ 2854 struct iperf_textline *t; 2855 while (!TAILQ_EMPTY(&test->server_output_list)) { 2856 t = TAILQ_FIRST(&test->server_output_list); 2857 TAILQ_REMOVE(&test->server_output_list, t, textlineentries); 2858 free(t->line); 2859 free(t); 2860 } 2861 2862 /* sctp_bindx: do not free the arguments, only the resolver results */ 2863 if (!TAILQ_EMPTY(&test->xbind_addrs)) { 2864 struct xbind_entry *xbe; 2865 2866 TAILQ_FOREACH(xbe, &test->xbind_addrs, link) { 2867 if (xbe->ai) { 2868 freeaddrinfo(xbe->ai); 2869 xbe->ai = NULL; 2870 } 2871 } 2872 } 2873 2874 /* Free interval's traffic array for average rate calculations */ 2875 if (test->bitrate_limit_intervals_traffic_bytes != NULL) 2876 free(test->bitrate_limit_intervals_traffic_bytes); 2877 2878 /* XXX: Why are we setting these values to NULL? */ 2879 // test->streams = NULL; 2880 test->stats_callback = NULL; 2881 test->reporter_callback = NULL; 2882 free(test); 2883 } 2884 2885 2886 void 2887 iperf_reset_test(struct iperf_test *test) 2888 { 2889 struct iperf_stream *sp; 2890 int i; 2891 2892 /* Free streams */ 2893 while (!SLIST_EMPTY(&test->streams)) { 2894 sp = SLIST_FIRST(&test->streams); 2895 SLIST_REMOVE_HEAD(&test->streams, streams); 2896 iperf_free_stream(sp); 2897 } 2898 if (test->omit_timer != NULL) { 2899 tmr_cancel(test->omit_timer); 2900 test->omit_timer = NULL; 2901 } 2902 if (test->timer != NULL) { 2903 tmr_cancel(test->timer); 2904 test->timer = NULL; 2905 } 2906 if (test->stats_timer != NULL) { 2907 tmr_cancel(test->stats_timer); 2908 test->stats_timer = NULL; 2909 } 2910 if (test->reporter_timer != NULL) { 2911 tmr_cancel(test->reporter_timer); 2912 test->reporter_timer = NULL; 2913 } 2914 test->done = 0; 2915 2916 SLIST_INIT(&test->streams); 2917 2918 if (test->remote_congestion_used) 2919 free(test->remote_congestion_used); 2920 test->remote_congestion_used = NULL; 2921 test->role = 's'; 2922 test->mode = RECEIVER; 2923 test->sender_has_retransmits = 0; 2924 set_protocol(test, Ptcp); 2925 test->omit = OMIT; 2926 test->duration = DURATION; 2927 test->server_affinity = -1; 2928 #if defined(HAVE_CPUSET_SETAFFINITY) 2929 CPU_ZERO(&test->cpumask); 2930 #endif /* HAVE_CPUSET_SETAFFINITY */ 2931 test->state = 0; 2932 2933 test->ctrl_sck = -1; 2934 test->prot_listener = -1; 2935 2936 test->bytes_sent = 0; 2937 test->blocks_sent = 0; 2938 2939 test->bytes_received = 0; 2940 test->blocks_received = 0; 2941 2942 test->other_side_has_retransmits = 0; 2943 2944 test->bitrate_limit_stats_count = 0; 2945 test->bitrate_limit_last_interval_index = 0; 2946 test->bitrate_limit_exceeded = 0; 2947 2948 for (i = 0; i < MAX_INTERVAL; i++) 2949 test->bitrate_limit_intervals_traffic_bytes[i] = 0; 2950 2951 test->reverse = 0; 2952 test->bidirectional = 0; 2953 test->no_delay = 0; 2954 2955 FD_ZERO(&test->read_set); 2956 FD_ZERO(&test->write_set); 2957 2958 test->num_streams = 1; 2959 test->settings->socket_bufsize = 0; 2960 test->settings->blksize = DEFAULT_TCP_BLKSIZE; 2961 test->settings->rate = 0; 2962 test->settings->burst = 0; 2963 test->settings->mss = 0; 2964 test->settings->tos = 0; 2965 test->settings->dont_fragment = 0; 2966 2967 #if defined(HAVE_SSL) 2968 if (test->settings->authtoken) { 2969 free(test->settings->authtoken); 2970 test->settings->authtoken = NULL; 2971 } 2972 if (test->settings->client_username) { 2973 free(test->settings->client_username); 2974 test->settings->client_username = NULL; 2975 } 2976 if (test->settings->client_password) { 2977 free(test->settings->client_password); 2978 test->settings->client_password = NULL; 2979 } 2980 if (test->settings->client_rsa_pubkey) { 2981 EVP_PKEY_free(test->settings->client_rsa_pubkey); 2982 test->settings->client_rsa_pubkey = NULL; 2983 } 2984 #endif /* HAVE_SSL */ 2985 2986 memset(test->cookie, 0, COOKIE_SIZE); 2987 test->multisend = 10; /* arbitrary */ 2988 test->udp_counters_64bit = 0; 2989 if (test->title) { 2990 free(test->title); 2991 test->title = NULL; 2992 } 2993 if (test->extra_data) { 2994 free(test->extra_data); 2995 test->extra_data = NULL; 2996 } 2997 2998 /* Free output line buffers, if any (on the server only) */ 2999 struct iperf_textline *t; 3000 while (!TAILQ_EMPTY(&test->server_output_list)) { 3001 t = TAILQ_FIRST(&test->server_output_list); 3002 TAILQ_REMOVE(&test->server_output_list, t, textlineentries); 3003 free(t->line); 3004 free(t); 3005 } 3006 } 3007 3008 3009 /* Reset all of a test's stats back to zero. Called when the omitting 3010 ** period is over. 3011 */ 3012 void 3013 iperf_reset_stats(struct iperf_test *test) 3014 { 3015 struct iperf_time now; 3016 struct iperf_stream *sp; 3017 struct iperf_stream_result *rp; 3018 3019 test->bytes_sent = 0; 3020 test->blocks_sent = 0; 3021 iperf_time_now(&now); 3022 SLIST_FOREACH(sp, &test->streams, streams) { 3023 sp->omitted_packet_count = sp->packet_count; 3024 sp->omitted_cnt_error = sp->cnt_error; 3025 sp->omitted_outoforder_packets = sp->outoforder_packets; 3026 sp->jitter = 0; 3027 rp = sp->result; 3028 rp->bytes_sent_omit = rp->bytes_sent; 3029 rp->bytes_received = 0; 3030 rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0; 3031 if (test->sender_has_retransmits == 1) { 3032 struct iperf_interval_results ir; /* temporary results structure */ 3033 save_tcpinfo(sp, &ir); 3034 rp->stream_prev_total_retrans = get_total_retransmits(&ir); 3035 } 3036 rp->stream_retrans = 0; 3037 rp->start_time = now; 3038 } 3039 } 3040 3041 3042 /**************************************************************************/ 3043 3044 /** 3045 * Gather statistics during a test. 3046 * This function works for both the client and server side. 3047 */ 3048 void 3049 iperf_stats_callback(struct iperf_test *test) 3050 { 3051 struct iperf_stream *sp; 3052 struct iperf_stream_result *rp = NULL; 3053 struct iperf_interval_results *irp, temp; 3054 struct iperf_time temp_time; 3055 iperf_size_t total_interval_bytes_transferred = 0; 3056 3057 temp.omitted = test->omitting; 3058 SLIST_FOREACH(sp, &test->streams, streams) { 3059 rp = sp->result; 3060 temp.bytes_transferred = sp->sender ? rp->bytes_sent_this_interval : rp->bytes_received_this_interval; 3061 3062 // Total bytes transferred this interval 3063 total_interval_bytes_transferred += rp->bytes_sent_this_interval + rp->bytes_received_this_interval; 3064 3065 irp = TAILQ_LAST(&rp->interval_results, irlisthead); 3066 /* result->end_time contains timestamp of previous interval */ 3067 if ( irp != NULL ) /* not the 1st interval */ 3068 memcpy(&temp.interval_start_time, &rp->end_time, sizeof(struct iperf_time)); 3069 else /* or use timestamp from beginning */ 3070 memcpy(&temp.interval_start_time, &rp->start_time, sizeof(struct iperf_time)); 3071 /* now save time of end of this interval */ 3072 iperf_time_now(&rp->end_time); 3073 memcpy(&temp.interval_end_time, &rp->end_time, sizeof(struct iperf_time)); 3074 iperf_time_diff(&temp.interval_start_time, &temp.interval_end_time, &temp_time); 3075 temp.interval_duration = iperf_time_in_secs(&temp_time); 3076 if (test->protocol->id == Ptcp) { 3077 if ( has_tcpinfo()) { 3078 save_tcpinfo(sp, &temp); 3079 if (test->sender_has_retransmits == 1) { 3080 long total_retrans = get_total_retransmits(&temp); 3081 temp.interval_retrans = total_retrans - rp->stream_prev_total_retrans; 3082 rp->stream_retrans += temp.interval_retrans; 3083 rp->stream_prev_total_retrans = total_retrans; 3084 3085 temp.snd_cwnd = get_snd_cwnd(&temp); 3086 if (temp.snd_cwnd > rp->stream_max_snd_cwnd) { 3087 rp->stream_max_snd_cwnd = temp.snd_cwnd; 3088 } 3089 3090 temp.snd_wnd = get_snd_wnd(&temp); 3091 if (temp.snd_wnd > rp->stream_max_snd_wnd) { 3092 rp->stream_max_snd_wnd = temp.snd_wnd; 3093 } 3094 3095 temp.rtt = get_rtt(&temp); 3096 if (temp.rtt > rp->stream_max_rtt) { 3097 rp->stream_max_rtt = temp.rtt; 3098 } 3099 if (rp->stream_min_rtt == 0 || 3100 temp.rtt < rp->stream_min_rtt) { 3101 rp->stream_min_rtt = temp.rtt; 3102 } 3103 rp->stream_sum_rtt += temp.rtt; 3104 rp->stream_count_rtt++; 3105 3106 temp.rttvar = get_rttvar(&temp); 3107 temp.pmtu = get_pmtu(&temp); 3108 } 3109 } 3110 } else { 3111 if (irp == NULL) { 3112 temp.interval_packet_count = sp->packet_count; 3113 temp.interval_outoforder_packets = sp->outoforder_packets; 3114 temp.interval_cnt_error = sp->cnt_error; 3115 } else { 3116 temp.interval_packet_count = sp->packet_count - irp->packet_count; 3117 temp.interval_outoforder_packets = sp->outoforder_packets - irp->outoforder_packets; 3118 temp.interval_cnt_error = sp->cnt_error - irp->cnt_error; 3119 } 3120 temp.packet_count = sp->packet_count; 3121 temp.jitter = sp->jitter; 3122 temp.outoforder_packets = sp->outoforder_packets; 3123 temp.cnt_error = sp->cnt_error; 3124 } 3125 add_to_interval_list(rp, &temp); 3126 rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0; 3127 } 3128 3129 /* Verify that total server's throughput is not above specified limit */ 3130 if (test->role == 's') { 3131 iperf_check_total_rate(test, total_interval_bytes_transferred); 3132 } 3133 } 3134 3135 /** 3136 * Print intermediate results during a test (interval report). 3137 * Uses print_interval_results to print the results for each stream, 3138 * then prints an interval summary for all streams in this 3139 * interval. 3140 */ 3141 static void 3142 iperf_print_intermediate(struct iperf_test *test) 3143 { 3144 struct iperf_stream *sp = NULL; 3145 struct iperf_interval_results *irp; 3146 struct iperf_time temp_time; 3147 cJSON *json_interval; 3148 cJSON *json_interval_streams; 3149 3150 int lower_mode, upper_mode; 3151 int current_mode; 3152 3153 /* 3154 * Due to timing oddities, there can be cases, especially on the 3155 * server side, where at the end of a test there is a fairly short 3156 * interval with no data transferred. This could caused by 3157 * the control and data flows sharing the same path in the network, 3158 * and having the control messages for stopping the test being 3159 * queued behind the data packets. 3160 * 3161 * We'd like to try to omit that last interval when it happens, to 3162 * avoid cluttering data and output with useless stuff. 3163 * So we're going to try to ignore very short intervals (less than 3164 * 10% of the interval time) that have no data. 3165 */ 3166 int interval_ok = 0; 3167 SLIST_FOREACH(sp, &test->streams, streams) { 3168 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); 3169 if (irp) { 3170 iperf_time_diff(&irp->interval_start_time, &irp->interval_end_time, &temp_time); 3171 double interval_len = iperf_time_in_secs(&temp_time); 3172 if (test->debug) { 3173 printf("interval_len %f bytes_transferred %" PRIu64 "\n", interval_len, irp->bytes_transferred); 3174 } 3175 3176 /* 3177 * If the interval is at least 10% the normal interval 3178 * length, or if there were actual bytes transferred, 3179 * then we want to keep this interval. 3180 */ 3181 if (interval_len >= test->stats_interval * 0.10 || 3182 irp->bytes_transferred > 0) { 3183 interval_ok = 1; 3184 if (test->debug) { 3185 printf("interval forces keep\n"); 3186 } 3187 } 3188 } 3189 } 3190 if (!interval_ok) { 3191 if (test->debug) { 3192 printf("ignoring short interval with no data\n"); 3193 } 3194 return; 3195 } 3196 3197 if (test->json_output) { 3198 json_interval = cJSON_CreateObject(); 3199 if (json_interval == NULL) 3200 return; 3201 cJSON_AddItemToArray(test->json_intervals, json_interval); 3202 json_interval_streams = cJSON_CreateArray(); 3203 if (json_interval_streams == NULL) 3204 return; 3205 cJSON_AddItemToObject(json_interval, "streams", json_interval_streams); 3206 } else { 3207 json_interval = NULL; 3208 json_interval_streams = NULL; 3209 } 3210 3211 /* 3212 * We must to sum streams separately. 3213 * For bidirectional mode we must to display 3214 * information about sender and receiver streams. 3215 * For client side we must handle sender streams 3216 * firstly and receiver streams for server side. 3217 * The following design allows us to do this. 3218 */ 3219 3220 if (test->mode == BIDIRECTIONAL) { 3221 if (test->role == 'c') { 3222 lower_mode = -1; 3223 upper_mode = 0; 3224 } else { 3225 lower_mode = 0; 3226 upper_mode = 1; 3227 } 3228 } else { 3229 lower_mode = test->mode; 3230 upper_mode = lower_mode; 3231 } 3232 3233 3234 for (current_mode = lower_mode; current_mode <= upper_mode; ++current_mode) { 3235 char ubuf[UNIT_LEN]; 3236 char nbuf[UNIT_LEN]; 3237 char mbuf[UNIT_LEN]; 3238 char zbuf[] = " "; 3239 3240 iperf_size_t bytes = 0; 3241 double bandwidth; 3242 int retransmits = 0; 3243 double start_time, end_time; 3244 3245 int total_packets = 0, lost_packets = 0; 3246 double avg_jitter = 0.0, lost_percent; 3247 int stream_must_be_sender = current_mode * current_mode; 3248 3249 /* Print stream role just for bidirectional mode. */ 3250 3251 if (test->mode == BIDIRECTIONAL) { 3252 sprintf(mbuf, "[%s-%s]", stream_must_be_sender?"TX":"RX", test->role == 'c'?"C":"S"); 3253 } else { 3254 mbuf[0] = '\0'; 3255 zbuf[0] = '\0'; 3256 } 3257 3258 SLIST_FOREACH(sp, &test->streams, streams) { 3259 if (sp->sender == stream_must_be_sender) { 3260 print_interval_results(test, sp, json_interval_streams); 3261 /* sum up all streams */ 3262 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); 3263 if (irp == NULL) { 3264 iperf_err(test, 3265 "iperf_print_intermediate error: interval_results is NULL"); 3266 return; 3267 } 3268 bytes += irp->bytes_transferred; 3269 if (test->protocol->id == Ptcp) { 3270 if (test->sender_has_retransmits == 1) { 3271 retransmits += irp->interval_retrans; 3272 } 3273 } else { 3274 total_packets += irp->interval_packet_count; 3275 lost_packets += irp->interval_cnt_error; 3276 avg_jitter += irp->jitter; 3277 } 3278 } 3279 } 3280 3281 /* next build string with sum of all streams */ 3282 if (test->num_streams > 1 || test->json_output) { 3283 sp = SLIST_FIRST(&test->streams); /* reset back to 1st stream */ 3284 /* Only do this of course if there was a first stream */ 3285 if (sp) { 3286 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); /* use 1st stream for timing info */ 3287 3288 unit_snprintf(ubuf, UNIT_LEN, (double) bytes, 'A'); 3289 bandwidth = (double) bytes / (double) irp->interval_duration; 3290 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3291 3292 iperf_time_diff(&sp->result->start_time,&irp->interval_start_time, &temp_time); 3293 start_time = iperf_time_in_secs(&temp_time); 3294 iperf_time_diff(&sp->result->start_time,&irp->interval_end_time, &temp_time); 3295 end_time = iperf_time_in_secs(&temp_time); 3296 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3297 if (test->sender_has_retransmits == 1 && stream_must_be_sender) { 3298 /* Interval sum, TCP with retransmits. */ 3299 if (test->json_output) 3300 cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (int64_t) retransmits, irp->omitted, stream_must_be_sender)); /* XXX irp->omitted or test->omitting? */ 3301 else 3302 iperf_printf(test, report_sum_bw_retrans_format, mbuf, start_time, end_time, ubuf, nbuf, retransmits, irp->omitted?report_omitted:""); /* XXX irp->omitted or test->omitting? */ 3303 } else { 3304 /* Interval sum, TCP without retransmits. */ 3305 if (test->json_output) 3306 cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, test->omitting, stream_must_be_sender)); 3307 else 3308 iperf_printf(test, report_sum_bw_format, mbuf, start_time, end_time, ubuf, nbuf, test->omitting?report_omitted:""); 3309 } 3310 } else { 3311 /* Interval sum, UDP. */ 3312 if (stream_must_be_sender) { 3313 if (test->json_output) 3314 cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f packets: %d omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (int64_t) total_packets, test->omitting, stream_must_be_sender)); 3315 else 3316 iperf_printf(test, report_sum_bw_udp_sender_format, mbuf, start_time, end_time, ubuf, nbuf, zbuf, total_packets, test->omitting?report_omitted:""); 3317 } else { 3318 avg_jitter /= test->num_streams; 3319 if (total_packets > 0) { 3320 lost_percent = 100.0 * lost_packets / total_packets; 3321 } 3322 else { 3323 lost_percent = 0.0; 3324 } 3325 if (test->json_output) 3326 cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) total_packets, (double) lost_percent, test->omitting, stream_must_be_sender)); 3327 else 3328 iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, end_time, ubuf, nbuf, avg_jitter * 1000.0, lost_packets, total_packets, lost_percent, test->omitting?report_omitted:""); 3329 } 3330 } 3331 } 3332 } 3333 } 3334 } 3335 3336 /** 3337 * Print overall summary statistics at the end of a test. 3338 */ 3339 static void 3340 iperf_print_results(struct iperf_test *test) 3341 { 3342 3343 cJSON *json_summary_streams = NULL; 3344 3345 int lower_mode, upper_mode; 3346 int current_mode; 3347 3348 int tmp_sender_has_retransmits = test->sender_has_retransmits; 3349 3350 /* print final summary for all intervals */ 3351 3352 if (test->json_output) { 3353 json_summary_streams = cJSON_CreateArray(); 3354 if (json_summary_streams == NULL) 3355 return; 3356 cJSON_AddItemToObject(test->json_end, "streams", json_summary_streams); 3357 } else { 3358 iperf_printf(test, "%s", report_bw_separator); 3359 if (test->verbose) 3360 iperf_printf(test, "%s", report_summary); 3361 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3362 if (test->sender_has_retransmits || test->other_side_has_retransmits) { 3363 if (test->bidirectional) 3364 iperf_printf(test, "%s", report_bw_retrans_header_bidir); 3365 else 3366 iperf_printf(test, "%s", report_bw_retrans_header); 3367 } 3368 else { 3369 if (test->bidirectional) 3370 iperf_printf(test, "%s", report_bw_header_bidir); 3371 else 3372 iperf_printf(test, "%s", report_bw_header); 3373 } 3374 } else { 3375 if (test->bidirectional) 3376 iperf_printf(test, "%s", report_bw_udp_header_bidir); 3377 else 3378 iperf_printf(test, "%s", report_bw_udp_header); 3379 } 3380 } 3381 3382 /* 3383 * We must to sum streams separately. 3384 * For bidirectional mode we must to display 3385 * information about sender and receiver streams. 3386 * For client side we must handle sender streams 3387 * firstly and receiver streams for server side. 3388 * The following design allows us to do this. 3389 */ 3390 3391 if (test->mode == BIDIRECTIONAL) { 3392 if (test->role == 'c') { 3393 lower_mode = -1; 3394 upper_mode = 0; 3395 } else { 3396 lower_mode = 0; 3397 upper_mode = 1; 3398 } 3399 } else { 3400 lower_mode = test->mode; 3401 upper_mode = lower_mode; 3402 } 3403 3404 3405 for (current_mode = lower_mode; current_mode <= upper_mode; ++current_mode) { 3406 cJSON *json_summary_stream = NULL; 3407 int total_retransmits = 0; 3408 int total_packets = 0, lost_packets = 0; 3409 int sender_packet_count = 0, receiver_packet_count = 0; /* for this stream, this interval */ 3410 int sender_total_packets = 0, receiver_total_packets = 0; /* running total */ 3411 char ubuf[UNIT_LEN]; 3412 char nbuf[UNIT_LEN]; 3413 struct stat sb; 3414 char sbuf[UNIT_LEN]; 3415 struct iperf_stream *sp = NULL; 3416 iperf_size_t bytes_sent, total_sent = 0; 3417 iperf_size_t bytes_received, total_received = 0; 3418 double start_time, end_time = 0.0, avg_jitter = 0.0, lost_percent = 0.0; 3419 double sender_time = 0.0, receiver_time = 0.0; 3420 struct iperf_time temp_time; 3421 double bandwidth; 3422 3423 char mbuf[UNIT_LEN]; 3424 int stream_must_be_sender = current_mode * current_mode; 3425 3426 3427 /* Print stream role just for bidirectional mode. */ 3428 3429 if (test->mode == BIDIRECTIONAL) { 3430 sprintf(mbuf, "[%s-%s]", stream_must_be_sender?"TX":"RX", test->role == 'c'?"C":"S"); 3431 } else { 3432 mbuf[0] = '\0'; 3433 } 3434 3435 /* Get sender_has_retransmits for each sender side (client and server) */ 3436 if (test->mode == BIDIRECTIONAL && stream_must_be_sender) 3437 test->sender_has_retransmits = tmp_sender_has_retransmits; 3438 else if (test->mode == BIDIRECTIONAL && !stream_must_be_sender) 3439 test->sender_has_retransmits = test->other_side_has_retransmits; 3440 3441 start_time = 0.; 3442 sp = SLIST_FIRST(&test->streams); 3443 3444 /* 3445 * If there is at least one stream, then figure out the length of time 3446 * we were running the tests and print out some statistics about 3447 * the streams. It's possible to not have any streams at all 3448 * if the client got interrupted before it got to do anything. 3449 * 3450 * Also note that we try to keep separate values for the sender 3451 * and receiver ending times. Earlier iperf (3.1 and earlier) 3452 * servers didn't send that to the clients, so in this case we fall 3453 * back to using the client's ending timestamp. The fallback is 3454 * basically emulating what iperf 3.1 did. 3455 */ 3456 3457 if (sp) { 3458 iperf_time_diff(&sp->result->start_time, &sp->result->end_time, &temp_time); 3459 end_time = iperf_time_in_secs(&temp_time); 3460 if (sp->sender) { 3461 sp->result->sender_time = end_time; 3462 if (sp->result->receiver_time == 0.0) { 3463 sp->result->receiver_time = sp->result->sender_time; 3464 } 3465 } 3466 else { 3467 sp->result->receiver_time = end_time; 3468 if (sp->result->sender_time == 0.0) { 3469 sp->result->sender_time = sp->result->receiver_time; 3470 } 3471 } 3472 sender_time = sp->result->sender_time; 3473 receiver_time = sp->result->receiver_time; 3474 SLIST_FOREACH(sp, &test->streams, streams) { 3475 if (sp->sender == stream_must_be_sender) { 3476 if (test->json_output) { 3477 json_summary_stream = cJSON_CreateObject(); 3478 if (json_summary_stream == NULL) 3479 return; 3480 cJSON_AddItemToArray(json_summary_streams, json_summary_stream); 3481 } 3482 3483 bytes_sent = sp->result->bytes_sent - sp->result->bytes_sent_omit; 3484 bytes_received = sp->result->bytes_received; 3485 total_sent += bytes_sent; 3486 total_received += bytes_received; 3487 3488 if (sp->sender) { 3489 sender_packet_count = sp->packet_count; 3490 receiver_packet_count = sp->peer_packet_count; 3491 } 3492 else { 3493 sender_packet_count = sp->peer_packet_count; 3494 receiver_packet_count = sp->packet_count; 3495 } 3496 3497 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3498 if (test->sender_has_retransmits) { 3499 total_retransmits += sp->result->stream_retrans; 3500 } 3501 } else { 3502 /* 3503 * Running total of the total number of packets. Use the sender packet count if we 3504 * have it, otherwise use the receiver packet count. 3505 */ 3506 int packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count; 3507 total_packets += (packet_count - sp->omitted_packet_count); 3508 sender_total_packets += (sender_packet_count - sp->omitted_packet_count); 3509 receiver_total_packets += (receiver_packet_count - sp->omitted_packet_count); 3510 lost_packets += (sp->cnt_error - sp->omitted_cnt_error); 3511 avg_jitter += sp->jitter; 3512 } 3513 3514 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_sent, 'A'); 3515 if (sender_time > 0.0) { 3516 bandwidth = (double) bytes_sent / (double) sender_time; 3517 } 3518 else { 3519 bandwidth = 0.0; 3520 } 3521 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3522 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3523 if (test->sender_has_retransmits) { 3524 /* Sender summary, TCP and SCTP with retransmits. */ 3525 if (test->json_output) 3526 cJSON_AddItemToObject(json_summary_stream, "sender", iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d max_snd_cwnd: %d max_snd_wnd: %d max_rtt: %d min_rtt: %d mean_rtt: %d sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8, (int64_t) sp->result->stream_retrans, (int64_t) sp->result->stream_max_snd_cwnd, (int64_t) sp->result->stream_max_snd_wnd, (int64_t) sp->result->stream_max_rtt, (int64_t) sp->result->stream_min_rtt, (int64_t) ((sp->result->stream_count_rtt == 0) ? 0 : sp->result->stream_sum_rtt / sp->result->stream_count_rtt), stream_must_be_sender)); 3527 else 3528 if (test->role == 's' && !sp->sender) { 3529 if (test->verbose) 3530 iperf_printf(test, report_sender_not_available_format, sp->socket); 3531 } 3532 else { 3533 iperf_printf(test, report_bw_retrans_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, sp->result->stream_retrans, report_sender); 3534 } 3535 } else { 3536 /* Sender summary, TCP and SCTP without retransmits. */ 3537 if (test->json_output) 3538 cJSON_AddItemToObject(json_summary_stream, "sender", iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8, stream_must_be_sender)); 3539 else 3540 if (test->role == 's' && !sp->sender) { 3541 if (test->verbose) 3542 iperf_printf(test, report_sender_not_available_format, sp->socket); 3543 } 3544 else { 3545 iperf_printf(test, report_bw_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, report_sender); 3546 } 3547 } 3548 } else { 3549 /* Sender summary, UDP. */ 3550 if (sender_packet_count - sp->omitted_packet_count > 0) { 3551 lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (sender_packet_count - sp->omitted_packet_count); 3552 } 3553 else { 3554 lost_percent = 0.0; 3555 } 3556 if (test->json_output) { 3557 /* 3558 * For hysterical raisins, we only emit one JSON 3559 * object for the UDP summary, and it contains 3560 * information for both the sender and receiver 3561 * side. 3562 * 3563 * The JSON format as currently defined only includes one 3564 * value for the number of packets. We usually want that 3565 * to be the sender's value (how many packets were sent 3566 * by the sender). However this value might not be 3567 * available on the receiver in certain circumstances 3568 * specifically on the server side for a normal test or 3569 * the client side for a reverse-mode test. If this 3570 * is the case, then use the receiver's count of packets 3571 * instead. 3572 */ 3573 int packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count; 3574 cJSON_AddItemToObject(json_summary_stream, "udp", iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f out_of_order: %d sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8, (double) sp->jitter * 1000.0, (int64_t) (sp->cnt_error - sp->omitted_cnt_error), (int64_t) (packet_count - sp->omitted_packet_count), (double) lost_percent, (int64_t) (sp->outoforder_packets - sp->omitted_outoforder_packets), stream_must_be_sender)); 3575 } 3576 else { 3577 /* 3578 * Due to ordering of messages on the control channel, 3579 * the server cannot report on client-side summary 3580 * statistics. If we're the server, omit one set of 3581 * summary statistics to avoid giving meaningless 3582 * results. 3583 */ 3584 if (test->role == 's' && !sp->sender) { 3585 if (test->verbose) 3586 iperf_printf(test, report_sender_not_available_format, sp->socket); 3587 } 3588 else { 3589 iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, 0.0, 0, (sender_packet_count - sp->omitted_packet_count), (double) 0, report_sender); 3590 } 3591 if ((sp->outoforder_packets - sp->omitted_outoforder_packets) > 0) 3592 iperf_printf(test, report_sum_outoforder, mbuf, start_time, sender_time, (sp->outoforder_packets - sp->omitted_outoforder_packets)); 3593 } 3594 } 3595 3596 if (sp->diskfile_fd >= 0) { 3597 if (fstat(sp->diskfile_fd, &sb) == 0) { 3598 /* In the odd case that it's a zero-sized file, say it was all transferred. */ 3599 int percent_sent = 100, percent_received = 100; 3600 if (sb.st_size > 0) { 3601 percent_sent = (int) ( ( (double) bytes_sent / (double) sb.st_size ) * 100.0 ); 3602 percent_received = (int) ( ( (double) bytes_received / (double) sb.st_size ) * 100.0 ); 3603 } 3604 unit_snprintf(sbuf, UNIT_LEN, (double) sb.st_size, 'A'); 3605 if (test->json_output) 3606 cJSON_AddItemToObject(json_summary_stream, "diskfile", iperf_json_printf("sent: %d received: %d size: %d percent_sent: %d percent_received: %d filename: %s", (int64_t) bytes_sent, (int64_t) bytes_received, (int64_t) sb.st_size, (int64_t) percent_sent, (int64_t) percent_received, test->diskfile_name)); 3607 else 3608 if (stream_must_be_sender) { 3609 iperf_printf(test, report_diskfile, ubuf, sbuf, percent_sent, test->diskfile_name); 3610 } 3611 else { 3612 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_received, 'A'); 3613 iperf_printf(test, report_diskfile, ubuf, sbuf, percent_received, test->diskfile_name); 3614 } 3615 } 3616 } 3617 3618 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_received, 'A'); 3619 if (receiver_time > 0) { 3620 bandwidth = (double) bytes_received / (double) receiver_time; 3621 } 3622 else { 3623 bandwidth = 0.0; 3624 } 3625 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3626 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3627 /* Receiver summary, TCP and SCTP */ 3628 if (test->json_output) 3629 cJSON_AddItemToObject(json_summary_stream, "receiver", iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (int64_t) sp->socket, (double) start_time, (double) receiver_time, (double) end_time, (int64_t) bytes_received, bandwidth * 8, stream_must_be_sender)); 3630 else 3631 if (test->role == 's' && sp->sender) { 3632 if (test->verbose) 3633 iperf_printf(test, report_receiver_not_available_format, sp->socket); 3634 } 3635 else { 3636 iperf_printf(test, report_bw_format, sp->socket, mbuf, start_time, receiver_time, ubuf, nbuf, report_receiver); 3637 } 3638 } 3639 else { 3640 /* 3641 * Receiver summary, UDP. Note that JSON was emitted with 3642 * the sender summary, so we only deal with human-readable 3643 * data here. 3644 */ 3645 if (! test->json_output) { 3646 if (receiver_packet_count - sp->omitted_packet_count > 0) { 3647 lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (receiver_packet_count - sp->omitted_packet_count); 3648 } 3649 else { 3650 lost_percent = 0.0; 3651 } 3652 3653 if (test->role == 's' && sp->sender) { 3654 if (test->verbose) 3655 iperf_printf(test, report_receiver_not_available_format, sp->socket); 3656 } 3657 else { 3658 iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, start_time, receiver_time, ubuf, nbuf, sp->jitter * 1000.0, (sp->cnt_error - sp->omitted_cnt_error), (receiver_packet_count - sp->omitted_packet_count), lost_percent, report_receiver); 3659 } 3660 } 3661 } 3662 } 3663 } 3664 } 3665 3666 if (test->num_streams > 1 || test->json_output) { 3667 unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A'); 3668 /* If no tests were run, arbitrarily set bandwidth to 0. */ 3669 if (sender_time > 0.0) { 3670 bandwidth = (double) total_sent / (double) sender_time; 3671 } 3672 else { 3673 bandwidth = 0.0; 3674 } 3675 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3676 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3677 if (test->sender_has_retransmits) { 3678 /* Summary sum, TCP with retransmits. */ 3679 if (test->json_output) 3680 cJSON_AddItemToObject(test->json_end, "sum_sent", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d sender: %b", (double) start_time, (double) sender_time, (double) sender_time, (int64_t) total_sent, bandwidth * 8, (int64_t) total_retransmits, stream_must_be_sender)); 3681 else 3682 if (test->role == 's' && !stream_must_be_sender) { 3683 if (test->verbose) 3684 iperf_printf(test, report_sender_not_available_summary_format, "SUM"); 3685 } 3686 else { 3687 iperf_printf(test, report_sum_bw_retrans_format, mbuf, start_time, sender_time, ubuf, nbuf, total_retransmits, report_sender); 3688 } 3689 } else { 3690 /* Summary sum, TCP without retransmits. */ 3691 if (test->json_output) 3692 cJSON_AddItemToObject(test->json_end, "sum_sent", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (double) start_time, (double) sender_time, (double) sender_time, (int64_t) total_sent, bandwidth * 8, stream_must_be_sender)); 3693 else 3694 if (test->role == 's' && !stream_must_be_sender) { 3695 if (test->verbose) 3696 iperf_printf(test, report_sender_not_available_summary_format, "SUM"); 3697 } 3698 else { 3699 iperf_printf(test, report_sum_bw_format, mbuf, start_time, sender_time, ubuf, nbuf, report_sender); 3700 } 3701 } 3702 unit_snprintf(ubuf, UNIT_LEN, (double) total_received, 'A'); 3703 /* If no tests were run, set received bandwidth to 0 */ 3704 if (receiver_time > 0.0) { 3705 bandwidth = (double) total_received / (double) receiver_time; 3706 } 3707 else { 3708 bandwidth = 0.0; 3709 } 3710 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3711 if (test->json_output) 3712 cJSON_AddItemToObject(test->json_end, "sum_received", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (double) start_time, (double) receiver_time, (double) receiver_time, (int64_t) total_received, bandwidth * 8, stream_must_be_sender)); 3713 else 3714 if (test->role == 's' && stream_must_be_sender) { 3715 if (test->verbose) 3716 iperf_printf(test, report_receiver_not_available_summary_format, "SUM"); 3717 } 3718 else { 3719 iperf_printf(test, report_sum_bw_format, mbuf, start_time, receiver_time, ubuf, nbuf, report_receiver); 3720 } 3721 } else { 3722 /* Summary sum, UDP. */ 3723 avg_jitter /= test->num_streams; 3724 /* If no packets were sent, arbitrarily set loss percentage to 0. */ 3725 if (total_packets > 0) { 3726 lost_percent = 100.0 * lost_packets / total_packets; 3727 } 3728 else { 3729 lost_percent = 0.0; 3730 } 3731 if (test->json_output) { 3732 /* 3733 * Original, summary structure. Using this 3734 * structure is not recommended due to 3735 * ambiguities between the sender and receiver. 3736 */ 3737 cJSON_AddItemToObject(test->json_end, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f sender: %b", (double) start_time, (double) receiver_time, (double) receiver_time, (int64_t) total_sent, bandwidth * 8, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) total_packets, (double) lost_percent, stream_must_be_sender)); 3738 /* 3739 * Separate sum_sent and sum_received structures. 3740 * Using these structures to get the most complete 3741 * information about UDP transfer. 3742 */ 3743 cJSON_AddItemToObject(test->json_end, "sum_sent", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f sender: %b", (double) start_time, (double) sender_time, (double) sender_time, (int64_t) total_sent, (double) total_sent * 8 / sender_time, (double) 0.0, (int64_t) 0, (int64_t) sender_total_packets, (double) 0.0, 1)); 3744 cJSON_AddItemToObject(test->json_end, "sum_received", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f sender: %b", (double) start_time, (double) receiver_time, (double) receiver_time, (int64_t) total_received, (double) total_received * 8 / receiver_time, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) receiver_total_packets, (double) lost_percent, 0)); 3745 } else { 3746 /* 3747 * On the client we have both sender and receiver overall summary 3748 * stats. On the server we have only the side that was on the 3749 * server. Output whatever we have. 3750 */ 3751 if (! (test->role == 's' && !stream_must_be_sender) ) { 3752 unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A'); 3753 iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, sender_time, ubuf, nbuf, 0.0, 0, sender_total_packets, 0.0, "sender"); 3754 } 3755 if (! (test->role == 's' && stream_must_be_sender) ) { 3756 3757 unit_snprintf(ubuf, UNIT_LEN, (double) total_received, 'A'); 3758 /* Compute received bandwidth. */ 3759 if (end_time > 0.0) { 3760 bandwidth = (double) total_received / (double) receiver_time; 3761 } 3762 else { 3763 bandwidth = 0.0; 3764 } 3765 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3766 iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, receiver_time, ubuf, nbuf, avg_jitter * 1000.0, lost_packets, receiver_total_packets, lost_percent, "receiver"); 3767 } 3768 } 3769 } 3770 } 3771 3772 if (test->json_output && current_mode == upper_mode) { 3773 cJSON_AddItemToObject(test->json_end, "cpu_utilization_percent", iperf_json_printf("host_total: %f host_user: %f host_system: %f remote_total: %f remote_user: %f remote_system: %f", (double) test->cpu_util[0], (double) test->cpu_util[1], (double) test->cpu_util[2], (double) test->remote_cpu_util[0], (double) test->remote_cpu_util[1], (double) test->remote_cpu_util[2])); 3774 if (test->protocol->id == Ptcp) { 3775 char *snd_congestion = NULL, *rcv_congestion = NULL; 3776 if (stream_must_be_sender) { 3777 snd_congestion = test->congestion_used; 3778 rcv_congestion = test->remote_congestion_used; 3779 } 3780 else { 3781 snd_congestion = test->remote_congestion_used; 3782 rcv_congestion = test->congestion_used; 3783 } 3784 if (snd_congestion) { 3785 cJSON_AddStringToObject(test->json_end, "sender_tcp_congestion", snd_congestion); 3786 } 3787 if (rcv_congestion) { 3788 cJSON_AddStringToObject(test->json_end, "receiver_tcp_congestion", rcv_congestion); 3789 } 3790 } 3791 } 3792 else { 3793 if (test->verbose) { 3794 if (stream_must_be_sender) { 3795 if (test->bidirectional) { 3796 iperf_printf(test, report_cpu, report_local, stream_must_be_sender?report_sender:report_receiver, test->cpu_util[0], test->cpu_util[1], test->cpu_util[2], report_remote, stream_must_be_sender?report_receiver:report_sender, test->remote_cpu_util[0], test->remote_cpu_util[1], test->remote_cpu_util[2]); 3797 iperf_printf(test, report_cpu, report_local, !stream_must_be_sender?report_sender:report_receiver, test->cpu_util[0], test->cpu_util[1], test->cpu_util[2], report_remote, !stream_must_be_sender?report_receiver:report_sender, test->remote_cpu_util[0], test->remote_cpu_util[1], test->remote_cpu_util[2]); 3798 } else 3799 iperf_printf(test, report_cpu, report_local, stream_must_be_sender?report_sender:report_receiver, test->cpu_util[0], test->cpu_util[1], test->cpu_util[2], report_remote, stream_must_be_sender?report_receiver:report_sender, test->remote_cpu_util[0], test->remote_cpu_util[1], test->remote_cpu_util[2]); 3800 } 3801 if (test->protocol->id == Ptcp) { 3802 char *snd_congestion = NULL, *rcv_congestion = NULL; 3803 if (stream_must_be_sender) { 3804 snd_congestion = test->congestion_used; 3805 rcv_congestion = test->remote_congestion_used; 3806 } 3807 else { 3808 snd_congestion = test->remote_congestion_used; 3809 rcv_congestion = test->congestion_used; 3810 } 3811 if (snd_congestion) { 3812 iperf_printf(test, "snd_tcp_congestion %s\n", snd_congestion); 3813 } 3814 if (rcv_congestion) { 3815 iperf_printf(test, "rcv_tcp_congestion %s\n", rcv_congestion); 3816 } 3817 } 3818 } 3819 3820 /* Print server output if we're on the client and it was requested/provided */ 3821 if (test->role == 'c' && iperf_get_test_get_server_output(test) && !test->json_output) { 3822 if (test->json_server_output) { 3823 char *str = cJSON_Print(test->json_server_output); 3824 iperf_printf(test, "\nServer JSON output:\n%s\n", str); 3825 cJSON_free(str); 3826 cJSON_Delete(test->json_server_output); 3827 test->json_server_output = NULL; 3828 } 3829 if (test->server_output_text) { 3830 iperf_printf(test, "\nServer output:\n%s\n", test->server_output_text); 3831 test->server_output_text = NULL; 3832 } 3833 } 3834 } 3835 } 3836 3837 /* Set real sender_has_retransmits for current side */ 3838 if (test->mode == BIDIRECTIONAL) 3839 test->sender_has_retransmits = tmp_sender_has_retransmits; 3840 } 3841 3842 /**************************************************************************/ 3843 3844 /** 3845 * Main report-printing callback. 3846 * Prints results either during a test (interval report only) or 3847 * after the entire test has been run (last interval report plus 3848 * overall summary). 3849 */ 3850 void 3851 iperf_reporter_callback(struct iperf_test *test) 3852 { 3853 switch (test->state) { 3854 case TEST_RUNNING: 3855 case STREAM_RUNNING: 3856 /* print interval results for each stream */ 3857 iperf_print_intermediate(test); 3858 break; 3859 case TEST_END: 3860 case DISPLAY_RESULTS: 3861 iperf_print_intermediate(test); 3862 iperf_print_results(test); 3863 break; 3864 } 3865 3866 } 3867 3868 /** 3869 * Print the interval results for one stream. 3870 * This function needs to know about the overall test so it can determine the 3871 * context for printing headers, separators, etc. 3872 */ 3873 static void 3874 print_interval_results(struct iperf_test *test, struct iperf_stream *sp, cJSON *json_interval_streams) 3875 { 3876 char ubuf[UNIT_LEN]; 3877 char nbuf[UNIT_LEN]; 3878 char cbuf[UNIT_LEN]; 3879 char mbuf[UNIT_LEN]; 3880 char zbuf[] = " "; 3881 double st = 0., et = 0.; 3882 struct iperf_time temp_time; 3883 struct iperf_interval_results *irp = NULL; 3884 double bandwidth, lost_percent; 3885 3886 if (test->mode == BIDIRECTIONAL) { 3887 sprintf(mbuf, "[%s-%s]", sp->sender?"TX":"RX", test->role == 'c'?"C":"S"); 3888 } else { 3889 mbuf[0] = '\0'; 3890 zbuf[0] = '\0'; 3891 } 3892 3893 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); /* get last entry in linked list */ 3894 if (irp == NULL) { 3895 iperf_err(test, "print_interval_results error: interval_results is NULL"); 3896 return; 3897 } 3898 if (!test->json_output) { 3899 /* First stream? */ 3900 if (sp == SLIST_FIRST(&test->streams)) { 3901 /* It it's the first interval, print the header; 3902 ** else if there's more than one stream, print the separator; 3903 ** else nothing. 3904 */ 3905 if (iperf_time_compare(&sp->result->start_time, &irp->interval_start_time) == 0) { 3906 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3907 if (test->sender_has_retransmits == 1) { 3908 if (test->bidirectional) 3909 iperf_printf(test, "%s", report_bw_retrans_cwnd_header_bidir); 3910 else 3911 iperf_printf(test, "%s", report_bw_retrans_cwnd_header); 3912 } 3913 else { 3914 if (test->bidirectional) 3915 iperf_printf(test, "%s", report_bw_header_bidir); 3916 else 3917 iperf_printf(test, "%s", report_bw_header); 3918 } 3919 } else { 3920 if (test->mode == SENDER) { 3921 iperf_printf(test, "%s", report_bw_udp_sender_header); 3922 } else if (test->mode == RECEIVER){ 3923 iperf_printf(test, "%s", report_bw_udp_header); 3924 } else { 3925 /* BIDIRECTIONAL */ 3926 iperf_printf(test, "%s", report_bw_udp_header_bidir); 3927 } 3928 } 3929 } else if (test->num_streams > 1) 3930 iperf_printf(test, "%s", report_bw_separator); 3931 } 3932 } 3933 3934 unit_snprintf(ubuf, UNIT_LEN, (double) (irp->bytes_transferred), 'A'); 3935 if (irp->interval_duration > 0.0) { 3936 bandwidth = (double) irp->bytes_transferred / (double) irp->interval_duration; 3937 } 3938 else { 3939 bandwidth = 0.0; 3940 } 3941 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3942 3943 iperf_time_diff(&sp->result->start_time, &irp->interval_start_time, &temp_time); 3944 st = iperf_time_in_secs(&temp_time); 3945 iperf_time_diff(&sp->result->start_time, &irp->interval_end_time, &temp_time); 3946 et = iperf_time_in_secs(&temp_time); 3947 3948 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3949 if (test->sender_has_retransmits == 1 && sp->sender) { 3950 /* Interval, TCP with retransmits. */ 3951 if (test->json_output) 3952 cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d snd_cwnd: %d snd_wnd: %d rtt: %d rttvar: %d pmtu: %d omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, (int64_t) irp->interval_retrans, (int64_t) irp->snd_cwnd, (int64_t) irp->snd_wnd, (int64_t) irp->rtt, (int64_t) irp->rttvar, (int64_t) irp->pmtu, irp->omitted, sp->sender)); 3953 else { 3954 unit_snprintf(cbuf, UNIT_LEN, irp->snd_cwnd, 'A'); 3955 iperf_printf(test, report_bw_retrans_cwnd_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->interval_retrans, cbuf, irp->omitted?report_omitted:""); 3956 } 3957 } else { 3958 /* Interval, TCP without retransmits. */ 3959 if (test->json_output) 3960 cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, irp->omitted, sp->sender)); 3961 else 3962 iperf_printf(test, report_bw_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->omitted?report_omitted:""); 3963 } 3964 } else { 3965 /* Interval, UDP. */ 3966 if (sp->sender) { 3967 if (test->json_output) 3968 cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f packets: %d omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, (int64_t) irp->interval_packet_count, irp->omitted, sp->sender)); 3969 else 3970 iperf_printf(test, report_bw_udp_sender_format, sp->socket, mbuf, st, et, ubuf, nbuf, zbuf, irp->interval_packet_count, irp->omitted?report_omitted:""); 3971 } else { 3972 if (irp->interval_packet_count > 0) { 3973 lost_percent = 100.0 * irp->interval_cnt_error / irp->interval_packet_count; 3974 } 3975 else { 3976 lost_percent = 0.0; 3977 } 3978 if (test->json_output) 3979 cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, (double) irp->jitter * 1000.0, (int64_t) irp->interval_cnt_error, (int64_t) irp->interval_packet_count, (double) lost_percent, irp->omitted, sp->sender)); 3980 else 3981 iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->jitter * 1000.0, irp->interval_cnt_error, irp->interval_packet_count, lost_percent, irp->omitted?report_omitted:""); 3982 } 3983 } 3984 3985 if (test->logfile || test->forceflush) 3986 iflush(test); 3987 } 3988 3989 /**************************************************************************/ 3990 void 3991 iperf_free_stream(struct iperf_stream *sp) 3992 { 3993 struct iperf_interval_results *irp, *nirp; 3994 3995 /* XXX: need to free interval list too! */ 3996 munmap(sp->buffer, sp->test->settings->blksize); 3997 close(sp->buffer_fd); 3998 if (sp->diskfile_fd >= 0) 3999 close(sp->diskfile_fd); 4000 for (irp = TAILQ_FIRST(&sp->result->interval_results); irp != NULL; irp = nirp) { 4001 nirp = TAILQ_NEXT(irp, irlistentries); 4002 free(irp); 4003 } 4004 free(sp->result); 4005 if (sp->send_timer != NULL) 4006 tmr_cancel(sp->send_timer); 4007 free(sp); 4008 } 4009 4010 /**************************************************************************/ 4011 struct iperf_stream * 4012 iperf_new_stream(struct iperf_test *test, int s, int sender) 4013 { 4014 struct iperf_stream *sp; 4015 int ret = 0; 4016 4017 char template[1024]; 4018 if (test->tmp_template) { 4019 snprintf(template, sizeof(template) / sizeof(char), "%s", test->tmp_template); 4020 } else { 4021 //find the system temporary dir *unix, windows, cygwin support 4022 char* tempdir = getenv("TMPDIR"); 4023 if (tempdir == 0){ 4024 tempdir = getenv("TEMP"); 4025 } 4026 if (tempdir == 0){ 4027 tempdir = getenv("TMP"); 4028 } 4029 if (tempdir == 0){ 4030 tempdir = "/tmp"; 4031 } 4032 snprintf(template, sizeof(template) / sizeof(char), "%s/iperf3.XXXXXX", tempdir); 4033 } 4034 4035 sp = (struct iperf_stream *) malloc(sizeof(struct iperf_stream)); 4036 if (!sp) { 4037 i_errno = IECREATESTREAM; 4038 return NULL; 4039 } 4040 4041 memset(sp, 0, sizeof(struct iperf_stream)); 4042 4043 sp->sender = sender; 4044 sp->test = test; 4045 sp->settings = test->settings; 4046 sp->result = (struct iperf_stream_result *) malloc(sizeof(struct iperf_stream_result)); 4047 if (!sp->result) { 4048 free(sp); 4049 i_errno = IECREATESTREAM; 4050 return NULL; 4051 } 4052 4053 memset(sp->result, 0, sizeof(struct iperf_stream_result)); 4054 TAILQ_INIT(&sp->result->interval_results); 4055 4056 /* Create and randomize the buffer */ 4057 sp->buffer_fd = mkstemp(template); 4058 if (sp->buffer_fd == -1) { 4059 i_errno = IECREATESTREAM; 4060 free(sp->result); 4061 free(sp); 4062 return NULL; 4063 } 4064 if (unlink(template) < 0) { 4065 i_errno = IECREATESTREAM; 4066 free(sp->result); 4067 free(sp); 4068 return NULL; 4069 } 4070 if (ftruncate(sp->buffer_fd, test->settings->blksize) < 0) { 4071 i_errno = IECREATESTREAM; 4072 free(sp->result); 4073 free(sp); 4074 return NULL; 4075 } 4076 sp->buffer = (char *) mmap(NULL, test->settings->blksize, PROT_READ|PROT_WRITE, MAP_PRIVATE, sp->buffer_fd, 0); 4077 if (sp->buffer == MAP_FAILED) { 4078 i_errno = IECREATESTREAM; 4079 free(sp->result); 4080 free(sp); 4081 return NULL; 4082 } 4083 sp->pending_size = 0; 4084 4085 /* Set socket */ 4086 sp->socket = s; 4087 4088 sp->snd = test->protocol->send; 4089 sp->rcv = test->protocol->recv; 4090 4091 if (test->diskfile_name != (char*) 0) { 4092 sp->diskfile_fd = open(test->diskfile_name, sender ? O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC), S_IRUSR|S_IWUSR); 4093 if (sp->diskfile_fd == -1) { 4094 i_errno = IEFILE; 4095 munmap(sp->buffer, sp->test->settings->blksize); 4096 free(sp->result); 4097 free(sp); 4098 return NULL; 4099 } 4100 sp->snd2 = sp->snd; 4101 sp->snd = diskfile_send; 4102 sp->rcv2 = sp->rcv; 4103 sp->rcv = diskfile_recv; 4104 } else 4105 sp->diskfile_fd = -1; 4106 4107 /* Initialize stream */ 4108 if (test->repeating_payload) 4109 fill_with_repeating_pattern(sp->buffer, test->settings->blksize); 4110 else 4111 ret = readentropy(sp->buffer, test->settings->blksize); 4112 4113 if ((ret < 0) || (iperf_init_stream(sp, test) < 0)) { 4114 close(sp->buffer_fd); 4115 munmap(sp->buffer, sp->test->settings->blksize); 4116 free(sp->result); 4117 free(sp); 4118 return NULL; 4119 } 4120 iperf_add_stream(test, sp); 4121 4122 return sp; 4123 } 4124 4125 /**************************************************************************/ 4126 int 4127 iperf_init_stream(struct iperf_stream *sp, struct iperf_test *test) 4128 { 4129 socklen_t len; 4130 int opt; 4131 4132 len = sizeof(struct sockaddr_storage); 4133 if (getsockname(sp->socket, (struct sockaddr *) &sp->local_addr, &len) < 0) { 4134 i_errno = IEINITSTREAM; 4135 return -1; 4136 } 4137 len = sizeof(struct sockaddr_storage); 4138 if (getpeername(sp->socket, (struct sockaddr *) &sp->remote_addr, &len) < 0) { 4139 i_errno = IEINITSTREAM; 4140 return -1; 4141 } 4142 4143 /* Set IP TOS */ 4144 if ((opt = test->settings->tos)) { 4145 if (getsockdomain(sp->socket) == AF_INET6) { 4146 #ifdef IPV6_TCLASS 4147 if (setsockopt(sp->socket, IPPROTO_IPV6, IPV6_TCLASS, &opt, sizeof(opt)) < 0) { 4148 i_errno = IESETCOS; 4149 return -1; 4150 } 4151 #else 4152 i_errno = IESETCOS; 4153 return -1; 4154 #endif 4155 } else { 4156 if (setsockopt(sp->socket, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)) < 0) { 4157 i_errno = IESETTOS; 4158 return -1; 4159 } 4160 } 4161 } 4162 4163 #if defined(HAVE_DONT_FRAGMENT) 4164 /* Set Don't Fragment (DF). Only applicable to IPv4/UDP tests. */ 4165 if (iperf_get_test_protocol_id(test) == Pudp && 4166 getsockdomain(sp->socket) == AF_INET && 4167 iperf_get_dont_fragment(test)) { 4168 4169 /* 4170 * There are multiple implementations of this feature depending on the OS. 4171 * We need to handle separately Linux, UNIX, and Windows, as well as 4172 * the case that DF isn't supported at all (such as on macOS). 4173 */ 4174 #if defined(IP_MTU_DISCOVER) /* Linux version of IP_DONTFRAG */ 4175 opt = IP_PMTUDISC_DO; 4176 if (setsockopt(sp->socket, IPPROTO_IP, IP_MTU_DISCOVER, &opt, sizeof(opt)) < 0) { 4177 i_errno = IESETDONTFRAGMENT; 4178 return -1; 4179 } 4180 #else 4181 #if defined(IP_DONTFRAG) /* UNIX does IP_DONTFRAG */ 4182 opt = 1; 4183 if (setsockopt(sp->socket, IPPROTO_IP, IP_DONTFRAG, &opt, sizeof(opt)) < 0) { 4184 i_errno = IESETDONTFRAGMENT; 4185 return -1; 4186 } 4187 #else 4188 #if defined(IP_DONTFRAGMENT) /* Windows does IP_DONTFRAGMENT */ 4189 opt = 1; 4190 if (setsockopt(sp->socket, IPPROTO_IP, IP_DONTFRAGMENT, &opt, sizeof(opt)) < 0) { 4191 i_errno = IESETDONTFRAGMENT; 4192 return -1; 4193 } 4194 #else 4195 i_errno = IESETDONTFRAGMENT; 4196 return -1; 4197 #endif /* IP_DONTFRAGMENT */ 4198 #endif /* IP_DONTFRAG */ 4199 #endif /* IP_MTU_DISCOVER */ 4200 } 4201 #endif /* HAVE_DONT_FRAGMENT */ 4202 return 0; 4203 } 4204 4205 /**************************************************************************/ 4206 void 4207 iperf_add_stream(struct iperf_test *test, struct iperf_stream *sp) 4208 { 4209 int i; 4210 struct iperf_stream *n, *prev; 4211 4212 if (SLIST_EMPTY(&test->streams)) { 4213 SLIST_INSERT_HEAD(&test->streams, sp, streams); 4214 sp->id = 1; 4215 } else { 4216 // for (n = test->streams, i = 2; n->next; n = n->next, ++i); 4217 i = 2; 4218 SLIST_FOREACH(n, &test->streams, streams) { 4219 prev = n; 4220 ++i; 4221 } 4222 SLIST_INSERT_AFTER(prev, sp, streams); 4223 sp->id = i; 4224 } 4225 } 4226 4227 /* This pair of routines gets inserted into the snd/rcv function pointers 4228 ** when there's a -F flag. They handle the file stuff and call the real 4229 ** snd/rcv functions, which have been saved in snd2/rcv2. 4230 ** 4231 ** The advantage of doing it this way is that in the much more common 4232 ** case of no -F flag, there is zero extra overhead. 4233 */ 4234 4235 static int 4236 diskfile_send(struct iperf_stream *sp) 4237 { 4238 int r; 4239 int buffer_left = sp->diskfile_left; // represents total data in buffer to be sent out 4240 static int rtot; 4241 4242 /* if needed, read enough data from the disk to fill up the buffer */ 4243 if (sp->diskfile_left < sp->test->settings->blksize && !sp->test->done) { 4244 r = read(sp->diskfile_fd, sp->buffer, sp->test->settings->blksize - 4245 sp->diskfile_left); 4246 buffer_left += r; 4247 rtot += r; 4248 if (sp->test->debug) { 4249 printf("read %d bytes from file, %d total\n", r, rtot); 4250 } 4251 4252 // If the buffer doesn't contain a full buffer at this point, 4253 // adjust the size of the data to send. 4254 if (buffer_left != sp->test->settings->blksize) { 4255 if (sp->test->debug) 4256 printf("possible eof\n"); 4257 // setting data size to be sent, 4258 // which is less than full block/buffer size 4259 // (to be used by iperf_tcp_send, etc.) 4260 sp->pending_size = buffer_left; 4261 } 4262 4263 // If there's no work left, we're done. 4264 if (buffer_left == 0) { 4265 sp->test->done = 1; 4266 if (sp->test->debug) 4267 printf("done\n"); 4268 } 4269 } 4270 4271 // If there's no data left in the file or in the buffer, we're done. 4272 // No more data available to be sent. 4273 // Return without sending data to the network 4274 if( sp->test->done || buffer_left == 0 ){ 4275 if (sp->test->debug) 4276 printf("already done\n"); 4277 sp->test->done = 1; 4278 return 0; 4279 } 4280 4281 r = sp->snd2(sp); 4282 if (r < 0) { 4283 return r; 4284 } 4285 /* 4286 * Compute how much data is in the buffer but didn't get sent. 4287 * If there are bytes that got left behind, slide them to the 4288 * front of the buffer so they can hopefully go out on the next 4289 * pass. 4290 */ 4291 sp->diskfile_left = buffer_left - r; 4292 if (sp->diskfile_left && sp->diskfile_left < sp->test->settings->blksize) { 4293 memcpy(sp->buffer, 4294 sp->buffer + (sp->test->settings->blksize - sp->diskfile_left), 4295 sp->diskfile_left); 4296 if (sp->test->debug) 4297 printf("Shifting %d bytes by %d\n", sp->diskfile_left, (sp->test->settings->blksize - sp->diskfile_left)); 4298 } 4299 return r; 4300 } 4301 4302 static int 4303 diskfile_recv(struct iperf_stream *sp) 4304 { 4305 int r; 4306 4307 r = sp->rcv2(sp); 4308 if (r > 0) { 4309 (void) write(sp->diskfile_fd, sp->buffer, r); 4310 } 4311 return r; 4312 } 4313 4314 4315 void 4316 iperf_catch_sigend(void (*handler)(int)) 4317 { 4318 #ifdef SIGINT 4319 signal(SIGINT, handler); 4320 #endif 4321 #ifdef SIGTERM 4322 signal(SIGTERM, handler); 4323 #endif 4324 #ifdef SIGHUP 4325 signal(SIGHUP, handler); 4326 #endif 4327 } 4328 4329 /** 4330 * Called as a result of getting a signal. 4331 * Depending on the current state of the test (and the role of this 4332 * process) compute and report one more set of ending statistics 4333 * before cleaning up and exiting. 4334 */ 4335 void 4336 iperf_got_sigend(struct iperf_test *test) 4337 { 4338 /* 4339 * If we're the client, or if we're a server and running a test, 4340 * then dump out the accumulated stats so far. 4341 */ 4342 if (test->role == 'c' || 4343 (test->role == 's' && test->state == TEST_RUNNING)) { 4344 4345 test->done = 1; 4346 cpu_util(test->cpu_util); 4347 test->stats_callback(test); 4348 test->state = DISPLAY_RESULTS; /* change local state only */ 4349 if (test->on_test_finish) 4350 test->on_test_finish(test); 4351 test->reporter_callback(test); 4352 } 4353 4354 if (test->ctrl_sck >= 0) { 4355 test->state = (test->role == 'c') ? CLIENT_TERMINATE : SERVER_TERMINATE; 4356 (void) Nwrite(test->ctrl_sck, (char*) &test->state, sizeof(signed char), Ptcp); 4357 } 4358 i_errno = (test->role == 'c') ? IECLIENTTERM : IESERVERTERM; 4359 iperf_errexit(test, "interrupt - %s", iperf_strerror(i_errno)); 4360 } 4361 4362 /* Try to write a PID file if requested, return -1 on an error. */ 4363 int 4364 iperf_create_pidfile(struct iperf_test *test) 4365 { 4366 if (test->pidfile) { 4367 int fd; 4368 char buf[8]; 4369 4370 /* See if the file already exists and we can read it. */ 4371 fd = open(test->pidfile, O_RDONLY, 0); 4372 if (fd >= 0) { 4373 if (read(fd, buf, sizeof(buf) - 1) >= 0) { 4374 4375 /* We read some bytes, see if they correspond to a valid PID */ 4376 pid_t pid; 4377 pid = atoi(buf); 4378 if (pid > 0) { 4379 4380 /* See if the process exists. */ 4381 if (kill(pid, 0) == 0) { 4382 /* 4383 * Make sure not to try to delete existing PID file by 4384 * scribbling over the pathname we'd use to refer to it. 4385 * Then exit with an error. 4386 */ 4387 free(test->pidfile); 4388 test->pidfile = NULL; 4389 iperf_errexit(test, "Another instance of iperf3 appears to be running"); 4390 } 4391 } 4392 } 4393 } 4394 4395 /* 4396 * File didn't exist, we couldn't read it, or it didn't correspond to 4397 * a running process. Try to create it. 4398 */ 4399 fd = open(test->pidfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR); 4400 if (fd < 0) { 4401 return -1; 4402 } 4403 snprintf(buf, sizeof(buf), "%d", getpid()); /* no trailing newline */ 4404 if (write(fd, buf, strlen(buf)) < 0) { 4405 return -1; 4406 } 4407 if (close(fd) < 0) { 4408 return -1; 4409 }; 4410 } 4411 return 0; 4412 } 4413 4414 /* Get rid of a PID file, return -1 on error. */ 4415 int 4416 iperf_delete_pidfile(struct iperf_test *test) 4417 { 4418 if (test->pidfile) { 4419 if (unlink(test->pidfile) < 0) { 4420 return -1; 4421 } 4422 } 4423 return 0; 4424 } 4425 4426 int 4427 iperf_json_start(struct iperf_test *test) 4428 { 4429 test->json_top = cJSON_CreateObject(); 4430 if (test->json_top == NULL) 4431 return -1; 4432 test->json_start = cJSON_CreateObject(); 4433 if (test->json_start == NULL) 4434 return -1; 4435 cJSON_AddItemToObject(test->json_top, "start", test->json_start); 4436 test->json_connected = cJSON_CreateArray(); 4437 if (test->json_connected == NULL) 4438 return -1; 4439 cJSON_AddItemToObject(test->json_start, "connected", test->json_connected); 4440 test->json_intervals = cJSON_CreateArray(); 4441 if (test->json_intervals == NULL) 4442 return -1; 4443 cJSON_AddItemToObject(test->json_top, "intervals", test->json_intervals); 4444 test->json_end = cJSON_CreateObject(); 4445 if (test->json_end == NULL) 4446 return -1; 4447 cJSON_AddItemToObject(test->json_top, "end", test->json_end); 4448 return 0; 4449 } 4450 4451 int 4452 iperf_json_finish(struct iperf_test *test) 4453 { 4454 if (test->title) 4455 cJSON_AddStringToObject(test->json_top, "title", test->title); 4456 if (test->extra_data) 4457 cJSON_AddStringToObject(test->json_top, "extra_data", test->extra_data); 4458 /* Include server output */ 4459 if (test->json_server_output) { 4460 cJSON_AddItemToObject(test->json_top, "server_output_json", test->json_server_output); 4461 } 4462 if (test->server_output_text) { 4463 cJSON_AddStringToObject(test->json_top, "server_output_text", test->server_output_text); 4464 } 4465 // Get ASCII rendering of JSON structure. Then make our 4466 // own copy of it and return the storage that cJSON allocated 4467 // on our behalf. We keep our own copy around. 4468 char *str = cJSON_Print(test->json_top); 4469 if (str == NULL) 4470 return -1; 4471 test->json_output_string = strdup(str); 4472 cJSON_free(str); 4473 if (test->json_output_string == NULL) 4474 return -1; 4475 fprintf(test->outfile, "%s\n", test->json_output_string); 4476 iflush(test); 4477 cJSON_Delete(test->json_top); 4478 test->json_top = test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL; 4479 return 0; 4480 } 4481 4482 4483 /* CPU affinity stuff - Linux, FreeBSD, and Windows only. */ 4484 4485 int 4486 iperf_setaffinity(struct iperf_test *test, int affinity) 4487 { 4488 #if defined(HAVE_SCHED_SETAFFINITY) 4489 cpu_set_t cpu_set; 4490 4491 CPU_ZERO(&cpu_set); 4492 CPU_SET(affinity, &cpu_set); 4493 if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0) { 4494 i_errno = IEAFFINITY; 4495 return -1; 4496 } 4497 return 0; 4498 #elif defined(HAVE_CPUSET_SETAFFINITY) 4499 cpuset_t cpumask; 4500 4501 if(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, 4502 sizeof(cpuset_t), &test->cpumask) != 0) { 4503 i_errno = IEAFFINITY; 4504 return -1; 4505 } 4506 4507 CPU_ZERO(&cpumask); 4508 CPU_SET(affinity, &cpumask); 4509 4510 if(cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_PID, -1, 4511 sizeof(cpuset_t), &cpumask) != 0) { 4512 i_errno = IEAFFINITY; 4513 return -1; 4514 } 4515 return 0; 4516 #elif defined(HAVE_SETPROCESSAFFINITYMASK) 4517 HANDLE process = GetCurrentProcess(); 4518 DWORD_PTR processAffinityMask = 1 << affinity; 4519 4520 if (SetProcessAffinityMask(process, processAffinityMask) == 0) { 4521 i_errno = IEAFFINITY; 4522 return -1; 4523 } 4524 return 0; 4525 #else /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */ 4526 i_errno = IEAFFINITY; 4527 return -1; 4528 #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */ 4529 } 4530 4531 int 4532 iperf_clearaffinity(struct iperf_test *test) 4533 { 4534 #if defined(HAVE_SCHED_SETAFFINITY) 4535 cpu_set_t cpu_set; 4536 int i; 4537 4538 CPU_ZERO(&cpu_set); 4539 for (i = 0; i < CPU_SETSIZE; ++i) 4540 CPU_SET(i, &cpu_set); 4541 if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0) { 4542 i_errno = IEAFFINITY; 4543 return -1; 4544 } 4545 return 0; 4546 #elif defined(HAVE_CPUSET_SETAFFINITY) 4547 if(cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_PID, -1, 4548 sizeof(cpuset_t), &test->cpumask) != 0) { 4549 i_errno = IEAFFINITY; 4550 return -1; 4551 } 4552 return 0; 4553 #elif defined(HAVE_SETPROCESSAFFINITYMASK) 4554 HANDLE process = GetCurrentProcess(); 4555 DWORD_PTR processAffinityMask; 4556 DWORD_PTR lpSystemAffinityMask; 4557 4558 if (GetProcessAffinityMask(process, &processAffinityMask, &lpSystemAffinityMask) == 0 4559 || SetProcessAffinityMask(process, lpSystemAffinityMask) == 0) { 4560 i_errno = IEAFFINITY; 4561 return -1; 4562 } 4563 return 0; 4564 #else /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */ 4565 i_errno = IEAFFINITY; 4566 return -1; 4567 #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */ 4568 } 4569 4570 static char iperf_timestr[100]; 4571 static char linebuffer[1024]; 4572 4573 int 4574 iperf_printf(struct iperf_test *test, const char* format, ...) 4575 { 4576 va_list argp; 4577 int r = 0, r0; 4578 time_t now; 4579 struct tm *ltm = NULL; 4580 char *ct = NULL; 4581 4582 /* Timestamp if requested */ 4583 if (iperf_get_test_timestamps(test)) { 4584 time(&now); 4585 ltm = localtime(&now); 4586 strftime(iperf_timestr, sizeof(iperf_timestr), iperf_get_test_timestamp_format(test), ltm); 4587 ct = iperf_timestr; 4588 } 4589 4590 /* 4591 * There are roughly two use cases here. If we're the client, 4592 * want to print stuff directly to the output stream. 4593 * If we're the sender we might need to buffer up output to send 4594 * to the client. 4595 * 4596 * This doesn't make a whole lot of difference except there are 4597 * some chunks of output on the client (on particular the whole 4598 * of the server output with --get-server-output) that could 4599 * easily exceed the size of the line buffer, but which don't need 4600 * to be buffered up anyway. 4601 */ 4602 if (test->role == 'c') { 4603 if (ct) { 4604 r0 = fprintf(test->outfile, "%s", ct); 4605 if (r0 < 0) 4606 return r0; 4607 r += r0; 4608 } 4609 if (test->title) { 4610 r0 = fprintf(test->outfile, "%s: ", test->title); 4611 if (r0 < 0) 4612 return r0; 4613 r += r0; 4614 } 4615 va_start(argp, format); 4616 r0 = vfprintf(test->outfile, format, argp); 4617 va_end(argp); 4618 if (r0 < 0) 4619 return r0; 4620 r += r0; 4621 } 4622 else if (test->role == 's') { 4623 if (ct) { 4624 r0 = snprintf(linebuffer, sizeof(linebuffer), "%s", ct); 4625 if (r0 < 0) 4626 return r0; 4627 r += r0; 4628 } 4629 /* Should always be true as long as sizeof(ct) < sizeof(linebuffer) */ 4630 if (r < sizeof(linebuffer)) { 4631 va_start(argp, format); 4632 r0 = vsnprintf(linebuffer + r, sizeof(linebuffer) - r, format, argp); 4633 va_end(argp); 4634 if (r0 < 0) 4635 return r0; 4636 r += r0; 4637 } 4638 fprintf(test->outfile, "%s", linebuffer); 4639 4640 if (test->role == 's' && iperf_get_test_get_server_output(test)) { 4641 struct iperf_textline *l = (struct iperf_textline *) malloc(sizeof(struct iperf_textline)); 4642 l->line = strdup(linebuffer); 4643 TAILQ_INSERT_TAIL(&(test->server_output_list), l, textlineentries); 4644 } 4645 } 4646 return r; 4647 } 4648 4649 int 4650 iflush(struct iperf_test *test) 4651 { 4652 return fflush(test->outfile); 4653 } 4654