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 (test->zerocopy) 2052 cJSON_AddNumberToObject(j, "zerocopy", test->zerocopy); 2053 #if defined(HAVE_DONT_FRAGMENT) 2054 if (test->settings->dont_fragment) 2055 cJSON_AddNumberToObject(j, "dont_fragment", test->settings->dont_fragment); 2056 #endif /* HAVE_DONT_FRAGMENT */ 2057 #if defined(HAVE_SSL) 2058 /* Send authentication parameters */ 2059 if (test->settings->client_username && test->settings->client_password && test->settings->client_rsa_pubkey){ 2060 int rc = encode_auth_setting(test->settings->client_username, test->settings->client_password, test->settings->client_rsa_pubkey, &test->settings->authtoken); 2061 2062 if (rc) { 2063 cJSON_Delete(j); 2064 i_errno = IESENDPARAMS; 2065 return -1; 2066 } 2067 2068 cJSON_AddStringToObject(j, "authtoken", test->settings->authtoken); 2069 } 2070 #endif // HAVE_SSL 2071 cJSON_AddStringToObject(j, "client_version", IPERF_VERSION); 2072 2073 if (test->debug) { 2074 char *str = cJSON_Print(j); 2075 printf("send_parameters:\n%s\n", str); 2076 cJSON_free(str); 2077 } 2078 2079 if (JSON_write(test->ctrl_sck, j) < 0) { 2080 i_errno = IESENDPARAMS; 2081 r = -1; 2082 } 2083 cJSON_Delete(j); 2084 } 2085 return r; 2086 } 2087 2088 /*************************************************************/ 2089 2090 static int 2091 get_parameters(struct iperf_test *test) 2092 { 2093 int r = 0; 2094 cJSON *j; 2095 cJSON *j_p; 2096 2097 j = JSON_read(test->ctrl_sck); 2098 if (j == NULL) { 2099 i_errno = IERECVPARAMS; 2100 r = -1; 2101 } else { 2102 if (test->debug) { 2103 char *str; 2104 str = cJSON_Print(j); 2105 printf("get_parameters:\n%s\n", str ); 2106 cJSON_free(str); 2107 } 2108 2109 if ((j_p = cJSON_GetObjectItem(j, "tcp")) != NULL) 2110 set_protocol(test, Ptcp); 2111 if ((j_p = cJSON_GetObjectItem(j, "udp")) != NULL) 2112 set_protocol(test, Pudp); 2113 if ((j_p = cJSON_GetObjectItem(j, "sctp")) != NULL) 2114 set_protocol(test, Psctp); 2115 if ((j_p = cJSON_GetObjectItem(j, "omit")) != NULL) 2116 test->omit = j_p->valueint; 2117 if ((j_p = cJSON_GetObjectItem(j, "server_affinity")) != NULL) 2118 test->server_affinity = j_p->valueint; 2119 if ((j_p = cJSON_GetObjectItem(j, "time")) != NULL) 2120 test->duration = j_p->valueint; 2121 if ((j_p = cJSON_GetObjectItem(j, "num")) != NULL) 2122 test->settings->bytes = j_p->valueint; 2123 if ((j_p = cJSON_GetObjectItem(j, "blockcount")) != NULL) 2124 test->settings->blocks = j_p->valueint; 2125 if ((j_p = cJSON_GetObjectItem(j, "MSS")) != NULL) 2126 test->settings->mss = j_p->valueint; 2127 if ((j_p = cJSON_GetObjectItem(j, "nodelay")) != NULL) 2128 test->no_delay = 1; 2129 if ((j_p = cJSON_GetObjectItem(j, "parallel")) != NULL) 2130 test->num_streams = j_p->valueint; 2131 if ((j_p = cJSON_GetObjectItem(j, "reverse")) != NULL) 2132 iperf_set_test_reverse(test, 1); 2133 if ((j_p = cJSON_GetObjectItem(j, "bidirectional")) != NULL) 2134 iperf_set_test_bidirectional(test, 1); 2135 if ((j_p = cJSON_GetObjectItem(j, "window")) != NULL) 2136 test->settings->socket_bufsize = j_p->valueint; 2137 if ((j_p = cJSON_GetObjectItem(j, "len")) != NULL) 2138 test->settings->blksize = j_p->valueint; 2139 if ((j_p = cJSON_GetObjectItem(j, "bandwidth")) != NULL) 2140 test->settings->rate = j_p->valueint; 2141 if ((j_p = cJSON_GetObjectItem(j, "fqrate")) != NULL) 2142 test->settings->fqrate = j_p->valueint; 2143 if ((j_p = cJSON_GetObjectItem(j, "pacing_timer")) != NULL) 2144 test->settings->pacing_timer = j_p->valueint; 2145 if ((j_p = cJSON_GetObjectItem(j, "burst")) != NULL) 2146 test->settings->burst = j_p->valueint; 2147 if ((j_p = cJSON_GetObjectItem(j, "TOS")) != NULL) 2148 test->settings->tos = j_p->valueint; 2149 if ((j_p = cJSON_GetObjectItem(j, "flowlabel")) != NULL) 2150 test->settings->flowlabel = j_p->valueint; 2151 if ((j_p = cJSON_GetObjectItem(j, "title")) != NULL) 2152 test->title = strdup(j_p->valuestring); 2153 if ((j_p = cJSON_GetObjectItem(j, "extra_data")) != NULL) 2154 test->extra_data = strdup(j_p->valuestring); 2155 if ((j_p = cJSON_GetObjectItem(j, "congestion")) != NULL) 2156 test->congestion = strdup(j_p->valuestring); 2157 if ((j_p = cJSON_GetObjectItem(j, "congestion_used")) != NULL) 2158 test->congestion_used = strdup(j_p->valuestring); 2159 if ((j_p = cJSON_GetObjectItem(j, "get_server_output")) != NULL) 2160 iperf_set_test_get_server_output(test, 1); 2161 if ((j_p = cJSON_GetObjectItem(j, "udp_counters_64bit")) != NULL) 2162 iperf_set_test_udp_counters_64bit(test, 1); 2163 if ((j_p = cJSON_GetObjectItem(j, "repeating_payload")) != NULL) 2164 test->repeating_payload = 1; 2165 if ((j_p = cJSON_GetObjectItem(j, "zerocopy")) != NULL) 2166 test->zerocopy = j_p->valueint; 2167 #if defined(HAVE_DONT_FRAGMENT) 2168 if ((j_p = cJSON_GetObjectItem(j, "dont_fragment")) != NULL) 2169 test->settings->dont_fragment = j_p->valueint; 2170 #endif /* HAVE_DONT_FRAGMENT */ 2171 #if defined(HAVE_SSL) 2172 if ((j_p = cJSON_GetObjectItem(j, "authtoken")) != NULL) 2173 test->settings->authtoken = strdup(j_p->valuestring); 2174 #endif //HAVE_SSL 2175 if (test->mode && test->protocol->id == Ptcp && has_tcpinfo_retransmits()) 2176 test->sender_has_retransmits = 1; 2177 if (test->settings->rate) 2178 cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate); 2179 cJSON_Delete(j); 2180 } 2181 return r; 2182 } 2183 2184 /*************************************************************/ 2185 2186 static int 2187 send_results(struct iperf_test *test) 2188 { 2189 int r = 0; 2190 cJSON *j; 2191 cJSON *j_streams; 2192 struct iperf_stream *sp; 2193 cJSON *j_stream; 2194 int sender_has_retransmits; 2195 iperf_size_t bytes_transferred; 2196 int retransmits; 2197 struct iperf_time temp_time; 2198 double start_time, end_time; 2199 2200 j = cJSON_CreateObject(); 2201 if (j == NULL) { 2202 i_errno = IEPACKAGERESULTS; 2203 r = -1; 2204 } else { 2205 cJSON_AddNumberToObject(j, "cpu_util_total", test->cpu_util[0]); 2206 cJSON_AddNumberToObject(j, "cpu_util_user", test->cpu_util[1]); 2207 cJSON_AddNumberToObject(j, "cpu_util_system", test->cpu_util[2]); 2208 if ( test->mode == RECEIVER ) 2209 sender_has_retransmits = -1; 2210 else 2211 sender_has_retransmits = test->sender_has_retransmits; 2212 cJSON_AddNumberToObject(j, "sender_has_retransmits", sender_has_retransmits); 2213 if ( test->congestion_used ) { 2214 cJSON_AddStringToObject(j, "congestion_used", test->congestion_used); 2215 } 2216 2217 /* If on the server and sending server output, then do this */ 2218 if (test->role == 's' && test->get_server_output) { 2219 if (test->json_output) { 2220 /* Add JSON output */ 2221 cJSON_AddItemReferenceToObject(j, "server_output_json", test->json_top); 2222 } 2223 else { 2224 /* Add textual output */ 2225 size_t buflen = 0; 2226 2227 /* Figure out how much room we need to hold the complete output string */ 2228 struct iperf_textline *t; 2229 TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) { 2230 buflen += strlen(t->line); 2231 } 2232 2233 /* Allocate and build it up from the component lines */ 2234 char *output = calloc(buflen + 1, 1); 2235 TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) { 2236 strncat(output, t->line, buflen); 2237 buflen -= strlen(t->line); 2238 } 2239 2240 cJSON_AddStringToObject(j, "server_output_text", output); 2241 free(output); 2242 } 2243 } 2244 2245 j_streams = cJSON_CreateArray(); 2246 if (j_streams == NULL) { 2247 i_errno = IEPACKAGERESULTS; 2248 r = -1; 2249 } else { 2250 cJSON_AddItemToObject(j, "streams", j_streams); 2251 SLIST_FOREACH(sp, &test->streams, streams) { 2252 j_stream = cJSON_CreateObject(); 2253 if (j_stream == NULL) { 2254 i_errno = IEPACKAGERESULTS; 2255 r = -1; 2256 } else { 2257 cJSON_AddItemToArray(j_streams, j_stream); 2258 bytes_transferred = sp->sender ? (sp->result->bytes_sent - sp->result->bytes_sent_omit) : sp->result->bytes_received; 2259 retransmits = (sp->sender && test->sender_has_retransmits) ? sp->result->stream_retrans : -1; 2260 cJSON_AddNumberToObject(j_stream, "id", sp->id); 2261 cJSON_AddNumberToObject(j_stream, "bytes", bytes_transferred); 2262 cJSON_AddNumberToObject(j_stream, "retransmits", retransmits); 2263 cJSON_AddNumberToObject(j_stream, "jitter", sp->jitter); 2264 cJSON_AddNumberToObject(j_stream, "errors", sp->cnt_error); 2265 cJSON_AddNumberToObject(j_stream, "packets", sp->packet_count); 2266 2267 iperf_time_diff(&sp->result->start_time, &sp->result->start_time, &temp_time); 2268 start_time = iperf_time_in_secs(&temp_time); 2269 iperf_time_diff(&sp->result->start_time, &sp->result->end_time, &temp_time); 2270 end_time = iperf_time_in_secs(&temp_time); 2271 cJSON_AddNumberToObject(j_stream, "start_time", start_time); 2272 cJSON_AddNumberToObject(j_stream, "end_time", end_time); 2273 2274 } 2275 } 2276 if (r == 0 && test->debug) { 2277 char *str = cJSON_Print(j); 2278 printf("send_results\n%s\n", str); 2279 cJSON_free(str); 2280 } 2281 if (r == 0 && JSON_write(test->ctrl_sck, j) < 0) { 2282 i_errno = IESENDRESULTS; 2283 r = -1; 2284 } 2285 } 2286 cJSON_Delete(j); 2287 } 2288 return r; 2289 } 2290 2291 /*************************************************************/ 2292 2293 static int 2294 get_results(struct iperf_test *test) 2295 { 2296 int r = 0; 2297 cJSON *j; 2298 cJSON *j_cpu_util_total; 2299 cJSON *j_cpu_util_user; 2300 cJSON *j_cpu_util_system; 2301 cJSON *j_remote_congestion_used; 2302 cJSON *j_sender_has_retransmits; 2303 int result_has_retransmits; 2304 cJSON *j_streams; 2305 int n, i; 2306 cJSON *j_stream; 2307 cJSON *j_id; 2308 cJSON *j_bytes; 2309 cJSON *j_retransmits; 2310 cJSON *j_jitter; 2311 cJSON *j_errors; 2312 cJSON *j_packets; 2313 cJSON *j_server_output; 2314 cJSON *j_start_time, *j_end_time; 2315 int sid, cerror, pcount; 2316 double jitter; 2317 iperf_size_t bytes_transferred; 2318 int retransmits; 2319 struct iperf_stream *sp; 2320 2321 j = JSON_read(test->ctrl_sck); 2322 if (j == NULL) { 2323 i_errno = IERECVRESULTS; 2324 r = -1; 2325 } else { 2326 j_cpu_util_total = cJSON_GetObjectItem(j, "cpu_util_total"); 2327 j_cpu_util_user = cJSON_GetObjectItem(j, "cpu_util_user"); 2328 j_cpu_util_system = cJSON_GetObjectItem(j, "cpu_util_system"); 2329 j_sender_has_retransmits = cJSON_GetObjectItem(j, "sender_has_retransmits"); 2330 if (j_cpu_util_total == NULL || j_cpu_util_user == NULL || j_cpu_util_system == NULL || j_sender_has_retransmits == NULL) { 2331 i_errno = IERECVRESULTS; 2332 r = -1; 2333 } else { 2334 if (test->debug) { 2335 char *str = cJSON_Print(j); 2336 printf("get_results\n%s\n", str); 2337 cJSON_free(str); 2338 } 2339 2340 test->remote_cpu_util[0] = j_cpu_util_total->valuedouble; 2341 test->remote_cpu_util[1] = j_cpu_util_user->valuedouble; 2342 test->remote_cpu_util[2] = j_cpu_util_system->valuedouble; 2343 result_has_retransmits = j_sender_has_retransmits->valueint; 2344 if ( test->mode == RECEIVER ) { 2345 test->sender_has_retransmits = result_has_retransmits; 2346 test->other_side_has_retransmits = 0; 2347 } 2348 else if ( test->mode == BIDIRECTIONAL ) 2349 test->other_side_has_retransmits = result_has_retransmits; 2350 2351 j_streams = cJSON_GetObjectItem(j, "streams"); 2352 if (j_streams == NULL) { 2353 i_errno = IERECVRESULTS; 2354 r = -1; 2355 } else { 2356 n = cJSON_GetArraySize(j_streams); 2357 for (i=0; i<n; ++i) { 2358 j_stream = cJSON_GetArrayItem(j_streams, i); 2359 if (j_stream == NULL) { 2360 i_errno = IERECVRESULTS; 2361 r = -1; 2362 } else { 2363 j_id = cJSON_GetObjectItem(j_stream, "id"); 2364 j_bytes = cJSON_GetObjectItem(j_stream, "bytes"); 2365 j_retransmits = cJSON_GetObjectItem(j_stream, "retransmits"); 2366 j_jitter = cJSON_GetObjectItem(j_stream, "jitter"); 2367 j_errors = cJSON_GetObjectItem(j_stream, "errors"); 2368 j_packets = cJSON_GetObjectItem(j_stream, "packets"); 2369 j_start_time = cJSON_GetObjectItem(j_stream, "start_time"); 2370 j_end_time = cJSON_GetObjectItem(j_stream, "end_time"); 2371 if (j_id == NULL || j_bytes == NULL || j_retransmits == NULL || j_jitter == NULL || j_errors == NULL || j_packets == NULL) { 2372 i_errno = IERECVRESULTS; 2373 r = -1; 2374 } else { 2375 sid = j_id->valueint; 2376 bytes_transferred = j_bytes->valueint; 2377 retransmits = j_retransmits->valueint; 2378 jitter = j_jitter->valuedouble; 2379 cerror = j_errors->valueint; 2380 pcount = j_packets->valueint; 2381 SLIST_FOREACH(sp, &test->streams, streams) 2382 if (sp->id == sid) break; 2383 if (sp == NULL) { 2384 i_errno = IESTREAMID; 2385 r = -1; 2386 } else { 2387 if (sp->sender) { 2388 sp->jitter = jitter; 2389 sp->cnt_error = cerror; 2390 sp->peer_packet_count = pcount; 2391 sp->result->bytes_received = bytes_transferred; 2392 /* 2393 * We have to handle the possibility that 2394 * start_time and end_time might not be 2395 * available; this is the case for older (pre-3.2) 2396 * servers. 2397 * 2398 * We need to have result structure members to hold 2399 * the both sides' start_time and end_time. 2400 */ 2401 if (j_start_time && j_end_time) { 2402 sp->result->receiver_time = j_end_time->valuedouble - j_start_time->valuedouble; 2403 } 2404 else { 2405 sp->result->receiver_time = 0.0; 2406 } 2407 } else { 2408 sp->peer_packet_count = pcount; 2409 sp->result->bytes_sent = bytes_transferred; 2410 sp->result->stream_retrans = retransmits; 2411 if (j_start_time && j_end_time) { 2412 sp->result->sender_time = j_end_time->valuedouble - j_start_time->valuedouble; 2413 } 2414 else { 2415 sp->result->sender_time = 0.0; 2416 } 2417 } 2418 } 2419 } 2420 } 2421 } 2422 /* 2423 * If we're the client and we're supposed to get remote results, 2424 * look them up and process accordingly. 2425 */ 2426 if (test->role == 'c' && iperf_get_test_get_server_output(test)) { 2427 /* Look for JSON. If we find it, grab the object so it doesn't get deleted. */ 2428 j_server_output = cJSON_DetachItemFromObject(j, "server_output_json"); 2429 if (j_server_output != NULL) { 2430 test->json_server_output = j_server_output; 2431 } 2432 else { 2433 /* No JSON, look for textual output. Make a copy of the text for later. */ 2434 j_server_output = cJSON_GetObjectItem(j, "server_output_text"); 2435 if (j_server_output != NULL) { 2436 test->server_output_text = strdup(j_server_output->valuestring); 2437 } 2438 } 2439 } 2440 } 2441 } 2442 2443 j_remote_congestion_used = cJSON_GetObjectItem(j, "congestion_used"); 2444 if (j_remote_congestion_used != NULL) { 2445 test->remote_congestion_used = strdup(j_remote_congestion_used->valuestring); 2446 } 2447 2448 cJSON_Delete(j); 2449 } 2450 return r; 2451 } 2452 2453 /*************************************************************/ 2454 2455 static int 2456 JSON_write(int fd, cJSON *json) 2457 { 2458 uint32_t hsize, nsize; 2459 char *str; 2460 int r = 0; 2461 2462 str = cJSON_PrintUnformatted(json); 2463 if (str == NULL) 2464 r = -1; 2465 else { 2466 hsize = strlen(str); 2467 nsize = htonl(hsize); 2468 if (Nwrite(fd, (char*) &nsize, sizeof(nsize), Ptcp) < 0) 2469 r = -1; 2470 else { 2471 if (Nwrite(fd, str, hsize, Ptcp) < 0) 2472 r = -1; 2473 } 2474 cJSON_free(str); 2475 } 2476 return r; 2477 } 2478 2479 /*************************************************************/ 2480 2481 static cJSON * 2482 JSON_read(int fd) 2483 { 2484 uint32_t hsize, nsize; 2485 char *str; 2486 cJSON *json = NULL; 2487 int rc; 2488 2489 /* 2490 * Read a four-byte integer, which is the length of the JSON to follow. 2491 * Then read the JSON into a buffer and parse it. Return a parsed JSON 2492 * structure, NULL if there was an error. 2493 */ 2494 if (Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp) >= 0) { 2495 hsize = ntohl(nsize); 2496 /* Allocate a buffer to hold the JSON */ 2497 str = (char *) calloc(sizeof(char), hsize+1); /* +1 for trailing null */ 2498 if (str != NULL) { 2499 rc = Nread(fd, str, hsize, Ptcp); 2500 if (rc >= 0) { 2501 /* 2502 * We should be reading in the number of bytes corresponding to the 2503 * length in that 4-byte integer. If we don't the socket might have 2504 * prematurely closed. Only do the JSON parsing if we got the 2505 * correct number of bytes. 2506 */ 2507 if (rc == hsize) { 2508 json = cJSON_Parse(str); 2509 } 2510 else { 2511 printf("WARNING: Size of data read does not correspond to offered length\n"); 2512 } 2513 } 2514 } 2515 free(str); 2516 } 2517 return json; 2518 } 2519 2520 /*************************************************************/ 2521 /** 2522 * add_to_interval_list -- adds new interval to the interval_list 2523 */ 2524 2525 void 2526 add_to_interval_list(struct iperf_stream_result * rp, struct iperf_interval_results * new) 2527 { 2528 struct iperf_interval_results *irp; 2529 2530 irp = (struct iperf_interval_results *) malloc(sizeof(struct iperf_interval_results)); 2531 memcpy(irp, new, sizeof(struct iperf_interval_results)); 2532 TAILQ_INSERT_TAIL(&rp->interval_results, irp, irlistentries); 2533 } 2534 2535 2536 /************************************************************/ 2537 2538 /** 2539 * connect_msg -- displays connection message 2540 * denoting sender/receiver details 2541 * 2542 */ 2543 2544 void 2545 connect_msg(struct iperf_stream *sp) 2546 { 2547 char ipl[INET6_ADDRSTRLEN], ipr[INET6_ADDRSTRLEN]; 2548 int lport, rport; 2549 2550 if (getsockdomain(sp->socket) == AF_INET) { 2551 inet_ntop(AF_INET, (void *) &((struct sockaddr_in *) &sp->local_addr)->sin_addr, ipl, sizeof(ipl)); 2552 mapped_v4_to_regular_v4(ipl); 2553 inet_ntop(AF_INET, (void *) &((struct sockaddr_in *) &sp->remote_addr)->sin_addr, ipr, sizeof(ipr)); 2554 mapped_v4_to_regular_v4(ipr); 2555 lport = ntohs(((struct sockaddr_in *) &sp->local_addr)->sin_port); 2556 rport = ntohs(((struct sockaddr_in *) &sp->remote_addr)->sin_port); 2557 } else { 2558 inet_ntop(AF_INET6, (void *) &((struct sockaddr_in6 *) &sp->local_addr)->sin6_addr, ipl, sizeof(ipl)); 2559 mapped_v4_to_regular_v4(ipl); 2560 inet_ntop(AF_INET6, (void *) &((struct sockaddr_in6 *) &sp->remote_addr)->sin6_addr, ipr, sizeof(ipr)); 2561 mapped_v4_to_regular_v4(ipr); 2562 lport = ntohs(((struct sockaddr_in6 *) &sp->local_addr)->sin6_port); 2563 rport = ntohs(((struct sockaddr_in6 *) &sp->remote_addr)->sin6_port); 2564 } 2565 2566 if (sp->test->json_output) 2567 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)); 2568 else 2569 iperf_printf(sp->test, report_connected, sp->socket, ipl, lport, ipr, rport); 2570 } 2571 2572 2573 /**************************************************************************/ 2574 2575 struct iperf_test * 2576 iperf_new_test() 2577 { 2578 struct iperf_test *test; 2579 2580 test = (struct iperf_test *) malloc(sizeof(struct iperf_test)); 2581 if (!test) { 2582 i_errno = IENEWTEST; 2583 return NULL; 2584 } 2585 /* initialize everything to zero */ 2586 memset(test, 0, sizeof(struct iperf_test)); 2587 2588 test->settings = (struct iperf_settings *) malloc(sizeof(struct iperf_settings)); 2589 if (!test->settings) { 2590 free(test); 2591 i_errno = IENEWTEST; 2592 return NULL; 2593 } 2594 memset(test->settings, 0, sizeof(struct iperf_settings)); 2595 2596 test->bitrate_limit_intervals_traffic_bytes = (iperf_size_t *) malloc(sizeof(iperf_size_t) * MAX_INTERVAL); 2597 if (!test->bitrate_limit_intervals_traffic_bytes) { 2598 free(test); 2599 i_errno = IENEWTEST; 2600 return NULL; 2601 } 2602 memset(test->bitrate_limit_intervals_traffic_bytes, 0, sizeof(sizeof(iperf_size_t) * MAX_INTERVAL)); 2603 2604 /* By default all output goes to stdout */ 2605 test->outfile = stdout; 2606 2607 return test; 2608 } 2609 2610 /**************************************************************************/ 2611 2612 struct protocol * 2613 protocol_new(void) 2614 { 2615 struct protocol *proto; 2616 2617 proto = malloc(sizeof(struct protocol)); 2618 if(!proto) { 2619 return NULL; 2620 } 2621 memset(proto, 0, sizeof(struct protocol)); 2622 2623 return proto; 2624 } 2625 2626 void 2627 protocol_free(struct protocol *proto) 2628 { 2629 free(proto); 2630 } 2631 2632 /**************************************************************************/ 2633 int 2634 iperf_defaults(struct iperf_test *testp) 2635 { 2636 struct protocol *tcp, *udp; 2637 #if defined(HAVE_SCTP_H) 2638 struct protocol *sctp; 2639 #endif /* HAVE_SCTP_H */ 2640 2641 testp->omit = OMIT; 2642 testp->duration = DURATION; 2643 testp->diskfile_name = (char*) 0; 2644 testp->affinity = -1; 2645 testp->server_affinity = -1; 2646 TAILQ_INIT(&testp->xbind_addrs); 2647 #if defined(HAVE_CPUSET_SETAFFINITY) 2648 CPU_ZERO(&testp->cpumask); 2649 #endif /* HAVE_CPUSET_SETAFFINITY */ 2650 testp->title = NULL; 2651 testp->extra_data = NULL; 2652 testp->congestion = NULL; 2653 testp->congestion_used = NULL; 2654 testp->remote_congestion_used = NULL; 2655 testp->server_port = PORT; 2656 testp->ctrl_sck = -1; 2657 testp->prot_listener = -1; 2658 testp->other_side_has_retransmits = 0; 2659 2660 testp->stats_callback = iperf_stats_callback; 2661 testp->reporter_callback = iperf_reporter_callback; 2662 2663 testp->stats_interval = testp->reporter_interval = 1; 2664 testp->num_streams = 1; 2665 2666 testp->settings->domain = AF_UNSPEC; 2667 testp->settings->unit_format = 'a'; 2668 testp->settings->socket_bufsize = 0; /* use autotuning */ 2669 testp->settings->blksize = DEFAULT_TCP_BLKSIZE; 2670 testp->settings->rate = 0; 2671 testp->settings->bitrate_limit = 0; 2672 testp->settings->bitrate_limit_interval = 5; 2673 testp->settings->bitrate_limit_stats_per_interval = 0; 2674 testp->settings->fqrate = 0; 2675 testp->settings->pacing_timer = DEFAULT_PACING_TIMER; 2676 testp->settings->burst = 0; 2677 testp->settings->mss = 0; 2678 testp->settings->bytes = 0; 2679 testp->settings->blocks = 0; 2680 testp->settings->connect_timeout = -1; 2681 testp->settings->rcv_timeout.secs = DEFAULT_NO_MSG_RCVD_TIMEOUT / SEC_TO_mS; 2682 testp->settings->rcv_timeout.usecs = (DEFAULT_NO_MSG_RCVD_TIMEOUT % SEC_TO_mS) * mS_TO_US; 2683 testp->zerocopy = 0; 2684 2685 memset(testp->cookie, 0, COOKIE_SIZE); 2686 2687 testp->multisend = 10; /* arbitrary */ 2688 2689 /* Set up protocol list */ 2690 SLIST_INIT(&testp->streams); 2691 SLIST_INIT(&testp->protocols); 2692 2693 tcp = protocol_new(); 2694 if (!tcp) 2695 return -1; 2696 2697 tcp->id = Ptcp; 2698 tcp->name = "TCP"; 2699 tcp->accept = iperf_tcp_accept; 2700 tcp->listen = iperf_tcp_listen; 2701 tcp->connect = iperf_tcp_connect; 2702 tcp->send = iperf_tcp_send; 2703 tcp->recv = iperf_tcp_recv; 2704 tcp->init = NULL; 2705 SLIST_INSERT_HEAD(&testp->protocols, tcp, protocols); 2706 2707 udp = protocol_new(); 2708 if (!udp) { 2709 protocol_free(tcp); 2710 return -1; 2711 } 2712 2713 udp->id = Pudp; 2714 udp->name = "UDP"; 2715 udp->accept = iperf_udp_accept; 2716 udp->listen = iperf_udp_listen; 2717 udp->connect = iperf_udp_connect; 2718 udp->send = iperf_udp_send; 2719 udp->recv = iperf_udp_recv; 2720 udp->init = iperf_udp_init; 2721 SLIST_INSERT_AFTER(tcp, udp, protocols); 2722 2723 set_protocol(testp, Ptcp); 2724 2725 #if defined(HAVE_SCTP_H) 2726 sctp = protocol_new(); 2727 if (!sctp) { 2728 protocol_free(tcp); 2729 protocol_free(udp); 2730 return -1; 2731 } 2732 2733 sctp->id = Psctp; 2734 sctp->name = "SCTP"; 2735 sctp->accept = iperf_sctp_accept; 2736 sctp->listen = iperf_sctp_listen; 2737 sctp->connect = iperf_sctp_connect; 2738 sctp->send = iperf_sctp_send; 2739 sctp->recv = iperf_sctp_recv; 2740 sctp->init = iperf_sctp_init; 2741 2742 SLIST_INSERT_AFTER(udp, sctp, protocols); 2743 #endif /* HAVE_SCTP_H */ 2744 2745 testp->on_new_stream = iperf_on_new_stream; 2746 testp->on_test_start = iperf_on_test_start; 2747 testp->on_connect = iperf_on_connect; 2748 testp->on_test_finish = iperf_on_test_finish; 2749 2750 TAILQ_INIT(&testp->server_output_list); 2751 2752 return 0; 2753 } 2754 2755 2756 /**************************************************************************/ 2757 void 2758 iperf_free_test(struct iperf_test *test) 2759 { 2760 struct protocol *prot; 2761 struct iperf_stream *sp; 2762 2763 /* Free streams */ 2764 while (!SLIST_EMPTY(&test->streams)) { 2765 sp = SLIST_FIRST(&test->streams); 2766 SLIST_REMOVE_HEAD(&test->streams, streams); 2767 iperf_free_stream(sp); 2768 } 2769 if (test->server_hostname) 2770 free(test->server_hostname); 2771 if (test->tmp_template) 2772 free(test->tmp_template); 2773 if (test->bind_address) 2774 free(test->bind_address); 2775 if (test->bind_dev) 2776 free(test->bind_dev); 2777 if (!TAILQ_EMPTY(&test->xbind_addrs)) { 2778 struct xbind_entry *xbe; 2779 2780 while (!TAILQ_EMPTY(&test->xbind_addrs)) { 2781 xbe = TAILQ_FIRST(&test->xbind_addrs); 2782 TAILQ_REMOVE(&test->xbind_addrs, xbe, link); 2783 if (xbe->ai) 2784 freeaddrinfo(xbe->ai); 2785 free(xbe->name); 2786 free(xbe); 2787 } 2788 } 2789 #if defined(HAVE_SSL) 2790 2791 if (test->server_rsa_private_key) 2792 EVP_PKEY_free(test->server_rsa_private_key); 2793 test->server_rsa_private_key = NULL; 2794 2795 free(test->settings->authtoken); 2796 test->settings->authtoken = NULL; 2797 2798 free(test->settings->client_username); 2799 test->settings->client_username = NULL; 2800 2801 free(test->settings->client_password); 2802 test->settings->client_password = NULL; 2803 2804 if (test->settings->client_rsa_pubkey) 2805 EVP_PKEY_free(test->settings->client_rsa_pubkey); 2806 test->settings->client_rsa_pubkey = NULL; 2807 #endif /* HAVE_SSL */ 2808 2809 if (test->settings) 2810 free(test->settings); 2811 if (test->title) 2812 free(test->title); 2813 if (test->extra_data) 2814 free(test->extra_data); 2815 if (test->congestion) 2816 free(test->congestion); 2817 if (test->congestion_used) 2818 free(test->congestion_used); 2819 if (test->remote_congestion_used) 2820 free(test->remote_congestion_used); 2821 if (test->timestamp_format) 2822 free(test->timestamp_format); 2823 if (test->omit_timer != NULL) 2824 tmr_cancel(test->omit_timer); 2825 if (test->timer != NULL) 2826 tmr_cancel(test->timer); 2827 if (test->stats_timer != NULL) 2828 tmr_cancel(test->stats_timer); 2829 if (test->reporter_timer != NULL) 2830 tmr_cancel(test->reporter_timer); 2831 2832 /* Free protocol list */ 2833 while (!SLIST_EMPTY(&test->protocols)) { 2834 prot = SLIST_FIRST(&test->protocols); 2835 SLIST_REMOVE_HEAD(&test->protocols, protocols); 2836 free(prot); 2837 } 2838 2839 if (test->logfile) { 2840 free(test->logfile); 2841 test->logfile = NULL; 2842 if (test->outfile) { 2843 fclose(test->outfile); 2844 test->outfile = NULL; 2845 } 2846 } 2847 2848 if (test->server_output_text) { 2849 free(test->server_output_text); 2850 test->server_output_text = NULL; 2851 } 2852 2853 if (test->json_output_string) { 2854 free(test->json_output_string); 2855 test->json_output_string = NULL; 2856 } 2857 2858 /* Free output line buffers, if any (on the server only) */ 2859 struct iperf_textline *t; 2860 while (!TAILQ_EMPTY(&test->server_output_list)) { 2861 t = TAILQ_FIRST(&test->server_output_list); 2862 TAILQ_REMOVE(&test->server_output_list, t, textlineentries); 2863 free(t->line); 2864 free(t); 2865 } 2866 2867 /* sctp_bindx: do not free the arguments, only the resolver results */ 2868 if (!TAILQ_EMPTY(&test->xbind_addrs)) { 2869 struct xbind_entry *xbe; 2870 2871 TAILQ_FOREACH(xbe, &test->xbind_addrs, link) { 2872 if (xbe->ai) { 2873 freeaddrinfo(xbe->ai); 2874 xbe->ai = NULL; 2875 } 2876 } 2877 } 2878 2879 /* Free interval's traffic array for average rate calculations */ 2880 if (test->bitrate_limit_intervals_traffic_bytes != NULL) 2881 free(test->bitrate_limit_intervals_traffic_bytes); 2882 2883 /* XXX: Why are we setting these values to NULL? */ 2884 // test->streams = NULL; 2885 test->stats_callback = NULL; 2886 test->reporter_callback = NULL; 2887 free(test); 2888 } 2889 2890 2891 void 2892 iperf_reset_test(struct iperf_test *test) 2893 { 2894 struct iperf_stream *sp; 2895 int i; 2896 2897 /* Free streams */ 2898 while (!SLIST_EMPTY(&test->streams)) { 2899 sp = SLIST_FIRST(&test->streams); 2900 SLIST_REMOVE_HEAD(&test->streams, streams); 2901 iperf_free_stream(sp); 2902 } 2903 if (test->omit_timer != NULL) { 2904 tmr_cancel(test->omit_timer); 2905 test->omit_timer = NULL; 2906 } 2907 if (test->timer != NULL) { 2908 tmr_cancel(test->timer); 2909 test->timer = NULL; 2910 } 2911 if (test->stats_timer != NULL) { 2912 tmr_cancel(test->stats_timer); 2913 test->stats_timer = NULL; 2914 } 2915 if (test->reporter_timer != NULL) { 2916 tmr_cancel(test->reporter_timer); 2917 test->reporter_timer = NULL; 2918 } 2919 test->done = 0; 2920 2921 SLIST_INIT(&test->streams); 2922 2923 if (test->remote_congestion_used) 2924 free(test->remote_congestion_used); 2925 test->remote_congestion_used = NULL; 2926 test->role = 's'; 2927 test->mode = RECEIVER; 2928 test->sender_has_retransmits = 0; 2929 set_protocol(test, Ptcp); 2930 test->omit = OMIT; 2931 test->duration = DURATION; 2932 test->server_affinity = -1; 2933 #if defined(HAVE_CPUSET_SETAFFINITY) 2934 CPU_ZERO(&test->cpumask); 2935 #endif /* HAVE_CPUSET_SETAFFINITY */ 2936 test->state = 0; 2937 2938 test->ctrl_sck = -1; 2939 test->prot_listener = -1; 2940 2941 test->bytes_sent = 0; 2942 test->blocks_sent = 0; 2943 2944 test->bytes_received = 0; 2945 test->blocks_received = 0; 2946 2947 test->other_side_has_retransmits = 0; 2948 2949 test->bitrate_limit_stats_count = 0; 2950 test->bitrate_limit_last_interval_index = 0; 2951 test->bitrate_limit_exceeded = 0; 2952 2953 for (i = 0; i < MAX_INTERVAL; i++) 2954 test->bitrate_limit_intervals_traffic_bytes[i] = 0; 2955 2956 test->reverse = 0; 2957 test->bidirectional = 0; 2958 test->no_delay = 0; 2959 2960 FD_ZERO(&test->read_set); 2961 FD_ZERO(&test->write_set); 2962 2963 test->num_streams = 1; 2964 test->settings->socket_bufsize = 0; 2965 test->settings->blksize = DEFAULT_TCP_BLKSIZE; 2966 test->settings->rate = 0; 2967 test->settings->burst = 0; 2968 test->settings->mss = 0; 2969 test->settings->tos = 0; 2970 test->settings->dont_fragment = 0; 2971 test->zerocopy = 0; 2972 2973 #if defined(HAVE_SSL) 2974 if (test->settings->authtoken) { 2975 free(test->settings->authtoken); 2976 test->settings->authtoken = NULL; 2977 } 2978 if (test->settings->client_username) { 2979 free(test->settings->client_username); 2980 test->settings->client_username = NULL; 2981 } 2982 if (test->settings->client_password) { 2983 free(test->settings->client_password); 2984 test->settings->client_password = NULL; 2985 } 2986 if (test->settings->client_rsa_pubkey) { 2987 EVP_PKEY_free(test->settings->client_rsa_pubkey); 2988 test->settings->client_rsa_pubkey = NULL; 2989 } 2990 #endif /* HAVE_SSL */ 2991 2992 memset(test->cookie, 0, COOKIE_SIZE); 2993 test->multisend = 10; /* arbitrary */ 2994 test->udp_counters_64bit = 0; 2995 if (test->title) { 2996 free(test->title); 2997 test->title = NULL; 2998 } 2999 if (test->extra_data) { 3000 free(test->extra_data); 3001 test->extra_data = NULL; 3002 } 3003 3004 /* Free output line buffers, if any (on the server only) */ 3005 struct iperf_textline *t; 3006 while (!TAILQ_EMPTY(&test->server_output_list)) { 3007 t = TAILQ_FIRST(&test->server_output_list); 3008 TAILQ_REMOVE(&test->server_output_list, t, textlineentries); 3009 free(t->line); 3010 free(t); 3011 } 3012 } 3013 3014 3015 /* Reset all of a test's stats back to zero. Called when the omitting 3016 ** period is over. 3017 */ 3018 void 3019 iperf_reset_stats(struct iperf_test *test) 3020 { 3021 struct iperf_time now; 3022 struct iperf_stream *sp; 3023 struct iperf_stream_result *rp; 3024 3025 test->bytes_sent = 0; 3026 test->blocks_sent = 0; 3027 iperf_time_now(&now); 3028 SLIST_FOREACH(sp, &test->streams, streams) { 3029 sp->omitted_packet_count = sp->packet_count; 3030 sp->omitted_cnt_error = sp->cnt_error; 3031 sp->omitted_outoforder_packets = sp->outoforder_packets; 3032 sp->jitter = 0; 3033 rp = sp->result; 3034 rp->bytes_sent_omit = rp->bytes_sent; 3035 rp->bytes_received = 0; 3036 rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0; 3037 if (test->sender_has_retransmits == 1) { 3038 struct iperf_interval_results ir; /* temporary results structure */ 3039 save_tcpinfo(sp, &ir); 3040 rp->stream_prev_total_retrans = get_total_retransmits(&ir); 3041 } 3042 rp->stream_retrans = 0; 3043 rp->start_time = now; 3044 } 3045 } 3046 3047 3048 /**************************************************************************/ 3049 3050 /** 3051 * Gather statistics during a test. 3052 * This function works for both the client and server side. 3053 */ 3054 void 3055 iperf_stats_callback(struct iperf_test *test) 3056 { 3057 struct iperf_stream *sp; 3058 struct iperf_stream_result *rp = NULL; 3059 struct iperf_interval_results *irp, temp; 3060 struct iperf_time temp_time; 3061 iperf_size_t total_interval_bytes_transferred = 0; 3062 3063 temp.omitted = test->omitting; 3064 SLIST_FOREACH(sp, &test->streams, streams) { 3065 rp = sp->result; 3066 temp.bytes_transferred = sp->sender ? rp->bytes_sent_this_interval : rp->bytes_received_this_interval; 3067 3068 // Total bytes transferred this interval 3069 total_interval_bytes_transferred += rp->bytes_sent_this_interval + rp->bytes_received_this_interval; 3070 3071 irp = TAILQ_LAST(&rp->interval_results, irlisthead); 3072 /* result->end_time contains timestamp of previous interval */ 3073 if ( irp != NULL ) /* not the 1st interval */ 3074 memcpy(&temp.interval_start_time, &rp->end_time, sizeof(struct iperf_time)); 3075 else /* or use timestamp from beginning */ 3076 memcpy(&temp.interval_start_time, &rp->start_time, sizeof(struct iperf_time)); 3077 /* now save time of end of this interval */ 3078 iperf_time_now(&rp->end_time); 3079 memcpy(&temp.interval_end_time, &rp->end_time, sizeof(struct iperf_time)); 3080 iperf_time_diff(&temp.interval_start_time, &temp.interval_end_time, &temp_time); 3081 temp.interval_duration = iperf_time_in_secs(&temp_time); 3082 if (test->protocol->id == Ptcp) { 3083 if ( has_tcpinfo()) { 3084 save_tcpinfo(sp, &temp); 3085 if (test->sender_has_retransmits == 1) { 3086 long total_retrans = get_total_retransmits(&temp); 3087 temp.interval_retrans = total_retrans - rp->stream_prev_total_retrans; 3088 rp->stream_retrans += temp.interval_retrans; 3089 rp->stream_prev_total_retrans = total_retrans; 3090 3091 temp.snd_cwnd = get_snd_cwnd(&temp); 3092 if (temp.snd_cwnd > rp->stream_max_snd_cwnd) { 3093 rp->stream_max_snd_cwnd = temp.snd_cwnd; 3094 } 3095 3096 temp.snd_wnd = get_snd_wnd(&temp); 3097 if (temp.snd_wnd > rp->stream_max_snd_wnd) { 3098 rp->stream_max_snd_wnd = temp.snd_wnd; 3099 } 3100 3101 temp.rtt = get_rtt(&temp); 3102 if (temp.rtt > rp->stream_max_rtt) { 3103 rp->stream_max_rtt = temp.rtt; 3104 } 3105 if (rp->stream_min_rtt == 0 || 3106 temp.rtt < rp->stream_min_rtt) { 3107 rp->stream_min_rtt = temp.rtt; 3108 } 3109 rp->stream_sum_rtt += temp.rtt; 3110 rp->stream_count_rtt++; 3111 3112 temp.rttvar = get_rttvar(&temp); 3113 temp.pmtu = get_pmtu(&temp); 3114 } 3115 } 3116 } else { 3117 if (irp == NULL) { 3118 temp.interval_packet_count = sp->packet_count; 3119 temp.interval_outoforder_packets = sp->outoforder_packets; 3120 temp.interval_cnt_error = sp->cnt_error; 3121 } else { 3122 temp.interval_packet_count = sp->packet_count - irp->packet_count; 3123 temp.interval_outoforder_packets = sp->outoforder_packets - irp->outoforder_packets; 3124 temp.interval_cnt_error = sp->cnt_error - irp->cnt_error; 3125 } 3126 temp.packet_count = sp->packet_count; 3127 temp.jitter = sp->jitter; 3128 temp.outoforder_packets = sp->outoforder_packets; 3129 temp.cnt_error = sp->cnt_error; 3130 } 3131 add_to_interval_list(rp, &temp); 3132 rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0; 3133 } 3134 3135 /* Verify that total server's throughput is not above specified limit */ 3136 if (test->role == 's') { 3137 iperf_check_total_rate(test, total_interval_bytes_transferred); 3138 } 3139 } 3140 3141 /** 3142 * Print intermediate results during a test (interval report). 3143 * Uses print_interval_results to print the results for each stream, 3144 * then prints an interval summary for all streams in this 3145 * interval. 3146 */ 3147 static void 3148 iperf_print_intermediate(struct iperf_test *test) 3149 { 3150 struct iperf_stream *sp = NULL; 3151 struct iperf_interval_results *irp; 3152 struct iperf_time temp_time; 3153 cJSON *json_interval; 3154 cJSON *json_interval_streams; 3155 3156 int lower_mode, upper_mode; 3157 int current_mode; 3158 3159 /* 3160 * Due to timing oddities, there can be cases, especially on the 3161 * server side, where at the end of a test there is a fairly short 3162 * interval with no data transferred. This could caused by 3163 * the control and data flows sharing the same path in the network, 3164 * and having the control messages for stopping the test being 3165 * queued behind the data packets. 3166 * 3167 * We'd like to try to omit that last interval when it happens, to 3168 * avoid cluttering data and output with useless stuff. 3169 * So we're going to try to ignore very short intervals (less than 3170 * 10% of the interval time) that have no data. 3171 */ 3172 int interval_ok = 0; 3173 SLIST_FOREACH(sp, &test->streams, streams) { 3174 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); 3175 if (irp) { 3176 iperf_time_diff(&irp->interval_start_time, &irp->interval_end_time, &temp_time); 3177 double interval_len = iperf_time_in_secs(&temp_time); 3178 if (test->debug) { 3179 printf("interval_len %f bytes_transferred %" PRIu64 "\n", interval_len, irp->bytes_transferred); 3180 } 3181 3182 /* 3183 * If the interval is at least 10% the normal interval 3184 * length, or if there were actual bytes transferred, 3185 * then we want to keep this interval. 3186 */ 3187 if (interval_len >= test->stats_interval * 0.10 || 3188 irp->bytes_transferred > 0) { 3189 interval_ok = 1; 3190 if (test->debug) { 3191 printf("interval forces keep\n"); 3192 } 3193 } 3194 } 3195 } 3196 if (!interval_ok) { 3197 if (test->debug) { 3198 printf("ignoring short interval with no data\n"); 3199 } 3200 return; 3201 } 3202 3203 if (test->json_output) { 3204 json_interval = cJSON_CreateObject(); 3205 if (json_interval == NULL) 3206 return; 3207 cJSON_AddItemToArray(test->json_intervals, json_interval); 3208 json_interval_streams = cJSON_CreateArray(); 3209 if (json_interval_streams == NULL) 3210 return; 3211 cJSON_AddItemToObject(json_interval, "streams", json_interval_streams); 3212 } else { 3213 json_interval = NULL; 3214 json_interval_streams = NULL; 3215 } 3216 3217 /* 3218 * We must to sum streams separately. 3219 * For bidirectional mode we must to display 3220 * information about sender and receiver streams. 3221 * For client side we must handle sender streams 3222 * firstly and receiver streams for server side. 3223 * The following design allows us to do this. 3224 */ 3225 3226 if (test->mode == BIDIRECTIONAL) { 3227 if (test->role == 'c') { 3228 lower_mode = -1; 3229 upper_mode = 0; 3230 } else { 3231 lower_mode = 0; 3232 upper_mode = 1; 3233 } 3234 } else { 3235 lower_mode = test->mode; 3236 upper_mode = lower_mode; 3237 } 3238 3239 3240 for (current_mode = lower_mode; current_mode <= upper_mode; ++current_mode) { 3241 char ubuf[UNIT_LEN]; 3242 char nbuf[UNIT_LEN]; 3243 char mbuf[UNIT_LEN]; 3244 char zbuf[] = " "; 3245 3246 iperf_size_t bytes = 0; 3247 double bandwidth; 3248 int retransmits = 0; 3249 double start_time, end_time; 3250 3251 int total_packets = 0, lost_packets = 0; 3252 double avg_jitter = 0.0, lost_percent; 3253 int stream_must_be_sender = current_mode * current_mode; 3254 3255 /* Print stream role just for bidirectional mode. */ 3256 3257 if (test->mode == BIDIRECTIONAL) { 3258 sprintf(mbuf, "[%s-%s]", stream_must_be_sender?"TX":"RX", test->role == 'c'?"C":"S"); 3259 } else { 3260 mbuf[0] = '\0'; 3261 zbuf[0] = '\0'; 3262 } 3263 3264 SLIST_FOREACH(sp, &test->streams, streams) { 3265 if (sp->sender == stream_must_be_sender) { 3266 print_interval_results(test, sp, json_interval_streams); 3267 /* sum up all streams */ 3268 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); 3269 if (irp == NULL) { 3270 iperf_err(test, 3271 "iperf_print_intermediate error: interval_results is NULL"); 3272 return; 3273 } 3274 bytes += irp->bytes_transferred; 3275 if (test->protocol->id == Ptcp) { 3276 if (test->sender_has_retransmits == 1) { 3277 retransmits += irp->interval_retrans; 3278 } 3279 } else { 3280 total_packets += irp->interval_packet_count; 3281 lost_packets += irp->interval_cnt_error; 3282 avg_jitter += irp->jitter; 3283 } 3284 } 3285 } 3286 3287 /* next build string with sum of all streams */ 3288 if (test->num_streams > 1 || test->json_output) { 3289 sp = SLIST_FIRST(&test->streams); /* reset back to 1st stream */ 3290 /* Only do this of course if there was a first stream */ 3291 if (sp) { 3292 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); /* use 1st stream for timing info */ 3293 3294 unit_snprintf(ubuf, UNIT_LEN, (double) bytes, 'A'); 3295 bandwidth = (double) bytes / (double) irp->interval_duration; 3296 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3297 3298 iperf_time_diff(&sp->result->start_time,&irp->interval_start_time, &temp_time); 3299 start_time = iperf_time_in_secs(&temp_time); 3300 iperf_time_diff(&sp->result->start_time,&irp->interval_end_time, &temp_time); 3301 end_time = iperf_time_in_secs(&temp_time); 3302 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3303 if (test->sender_has_retransmits == 1 && stream_must_be_sender) { 3304 /* Interval sum, TCP with 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 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? */ 3307 else 3308 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? */ 3309 } else { 3310 /* Interval sum, TCP without retransmits. */ 3311 if (test->json_output) 3312 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)); 3313 else 3314 iperf_printf(test, report_sum_bw_format, mbuf, start_time, end_time, ubuf, nbuf, test->omitting?report_omitted:""); 3315 } 3316 } else { 3317 /* Interval sum, UDP. */ 3318 if (stream_must_be_sender) { 3319 if (test->json_output) 3320 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)); 3321 else 3322 iperf_printf(test, report_sum_bw_udp_sender_format, mbuf, start_time, end_time, ubuf, nbuf, zbuf, total_packets, test->omitting?report_omitted:""); 3323 } else { 3324 avg_jitter /= test->num_streams; 3325 if (total_packets > 0) { 3326 lost_percent = 100.0 * lost_packets / total_packets; 3327 } 3328 else { 3329 lost_percent = 0.0; 3330 } 3331 if (test->json_output) 3332 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)); 3333 else 3334 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:""); 3335 } 3336 } 3337 } 3338 } 3339 } 3340 } 3341 3342 /** 3343 * Print overall summary statistics at the end of a test. 3344 */ 3345 static void 3346 iperf_print_results(struct iperf_test *test) 3347 { 3348 3349 cJSON *json_summary_streams = NULL; 3350 3351 int lower_mode, upper_mode; 3352 int current_mode; 3353 3354 int tmp_sender_has_retransmits = test->sender_has_retransmits; 3355 3356 /* print final summary for all intervals */ 3357 3358 if (test->json_output) { 3359 json_summary_streams = cJSON_CreateArray(); 3360 if (json_summary_streams == NULL) 3361 return; 3362 cJSON_AddItemToObject(test->json_end, "streams", json_summary_streams); 3363 } else { 3364 iperf_printf(test, "%s", report_bw_separator); 3365 if (test->verbose) 3366 iperf_printf(test, "%s", report_summary); 3367 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3368 if (test->sender_has_retransmits || test->other_side_has_retransmits) { 3369 if (test->bidirectional) 3370 iperf_printf(test, "%s", report_bw_retrans_header_bidir); 3371 else 3372 iperf_printf(test, "%s", report_bw_retrans_header); 3373 } 3374 else { 3375 if (test->bidirectional) 3376 iperf_printf(test, "%s", report_bw_header_bidir); 3377 else 3378 iperf_printf(test, "%s", report_bw_header); 3379 } 3380 } else { 3381 if (test->bidirectional) 3382 iperf_printf(test, "%s", report_bw_udp_header_bidir); 3383 else 3384 iperf_printf(test, "%s", report_bw_udp_header); 3385 } 3386 } 3387 3388 /* 3389 * We must to sum streams separately. 3390 * For bidirectional mode we must to display 3391 * information about sender and receiver streams. 3392 * For client side we must handle sender streams 3393 * firstly and receiver streams for server side. 3394 * The following design allows us to do this. 3395 */ 3396 3397 if (test->mode == BIDIRECTIONAL) { 3398 if (test->role == 'c') { 3399 lower_mode = -1; 3400 upper_mode = 0; 3401 } else { 3402 lower_mode = 0; 3403 upper_mode = 1; 3404 } 3405 } else { 3406 lower_mode = test->mode; 3407 upper_mode = lower_mode; 3408 } 3409 3410 3411 for (current_mode = lower_mode; current_mode <= upper_mode; ++current_mode) { 3412 cJSON *json_summary_stream = NULL; 3413 int total_retransmits = 0; 3414 int total_packets = 0, lost_packets = 0; 3415 int sender_packet_count = 0, receiver_packet_count = 0; /* for this stream, this interval */ 3416 int sender_total_packets = 0, receiver_total_packets = 0; /* running total */ 3417 char ubuf[UNIT_LEN]; 3418 char nbuf[UNIT_LEN]; 3419 struct stat sb; 3420 char sbuf[UNIT_LEN]; 3421 struct iperf_stream *sp = NULL; 3422 iperf_size_t bytes_sent, total_sent = 0; 3423 iperf_size_t bytes_received, total_received = 0; 3424 double start_time, end_time = 0.0, avg_jitter = 0.0, lost_percent = 0.0; 3425 double sender_time = 0.0, receiver_time = 0.0; 3426 struct iperf_time temp_time; 3427 double bandwidth; 3428 3429 char mbuf[UNIT_LEN]; 3430 int stream_must_be_sender = current_mode * current_mode; 3431 3432 3433 /* Print stream role just for bidirectional mode. */ 3434 3435 if (test->mode == BIDIRECTIONAL) { 3436 sprintf(mbuf, "[%s-%s]", stream_must_be_sender?"TX":"RX", test->role == 'c'?"C":"S"); 3437 } else { 3438 mbuf[0] = '\0'; 3439 } 3440 3441 /* Get sender_has_retransmits for each sender side (client and server) */ 3442 if (test->mode == BIDIRECTIONAL && stream_must_be_sender) 3443 test->sender_has_retransmits = tmp_sender_has_retransmits; 3444 else if (test->mode == BIDIRECTIONAL && !stream_must_be_sender) 3445 test->sender_has_retransmits = test->other_side_has_retransmits; 3446 3447 start_time = 0.; 3448 sp = SLIST_FIRST(&test->streams); 3449 3450 /* 3451 * If there is at least one stream, then figure out the length of time 3452 * we were running the tests and print out some statistics about 3453 * the streams. It's possible to not have any streams at all 3454 * if the client got interrupted before it got to do anything. 3455 * 3456 * Also note that we try to keep separate values for the sender 3457 * and receiver ending times. Earlier iperf (3.1 and earlier) 3458 * servers didn't send that to the clients, so in this case we fall 3459 * back to using the client's ending timestamp. The fallback is 3460 * basically emulating what iperf 3.1 did. 3461 */ 3462 3463 if (sp) { 3464 iperf_time_diff(&sp->result->start_time, &sp->result->end_time, &temp_time); 3465 end_time = iperf_time_in_secs(&temp_time); 3466 if (sp->sender) { 3467 sp->result->sender_time = end_time; 3468 if (sp->result->receiver_time == 0.0) { 3469 sp->result->receiver_time = sp->result->sender_time; 3470 } 3471 } 3472 else { 3473 sp->result->receiver_time = end_time; 3474 if (sp->result->sender_time == 0.0) { 3475 sp->result->sender_time = sp->result->receiver_time; 3476 } 3477 } 3478 sender_time = sp->result->sender_time; 3479 receiver_time = sp->result->receiver_time; 3480 SLIST_FOREACH(sp, &test->streams, streams) { 3481 if (sp->sender == stream_must_be_sender) { 3482 if (test->json_output) { 3483 json_summary_stream = cJSON_CreateObject(); 3484 if (json_summary_stream == NULL) 3485 return; 3486 cJSON_AddItemToArray(json_summary_streams, json_summary_stream); 3487 } 3488 3489 bytes_sent = sp->result->bytes_sent - sp->result->bytes_sent_omit; 3490 bytes_received = sp->result->bytes_received; 3491 total_sent += bytes_sent; 3492 total_received += bytes_received; 3493 3494 if (sp->sender) { 3495 sender_packet_count = sp->packet_count; 3496 receiver_packet_count = sp->peer_packet_count; 3497 } 3498 else { 3499 sender_packet_count = sp->peer_packet_count; 3500 receiver_packet_count = sp->packet_count; 3501 } 3502 3503 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3504 if (test->sender_has_retransmits) { 3505 total_retransmits += sp->result->stream_retrans; 3506 } 3507 } else { 3508 /* 3509 * Running total of the total number of packets. Use the sender packet count if we 3510 * have it, otherwise use the receiver packet count. 3511 */ 3512 int packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count; 3513 total_packets += (packet_count - sp->omitted_packet_count); 3514 sender_total_packets += (sender_packet_count - sp->omitted_packet_count); 3515 receiver_total_packets += (receiver_packet_count - sp->omitted_packet_count); 3516 lost_packets += (sp->cnt_error - sp->omitted_cnt_error); 3517 avg_jitter += sp->jitter; 3518 } 3519 3520 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_sent, 'A'); 3521 if (sender_time > 0.0) { 3522 bandwidth = (double) bytes_sent / (double) sender_time; 3523 } 3524 else { 3525 bandwidth = 0.0; 3526 } 3527 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3528 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3529 if (test->sender_has_retransmits) { 3530 /* Sender summary, TCP and SCTP with retransmits. */ 3531 if (test->json_output) 3532 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)); 3533 else 3534 if (test->role == 's' && !sp->sender) { 3535 if (test->verbose) 3536 iperf_printf(test, report_sender_not_available_format, sp->socket); 3537 } 3538 else { 3539 iperf_printf(test, report_bw_retrans_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, sp->result->stream_retrans, report_sender); 3540 } 3541 } else { 3542 /* Sender summary, TCP and SCTP without retransmits. */ 3543 if (test->json_output) 3544 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)); 3545 else 3546 if (test->role == 's' && !sp->sender) { 3547 if (test->verbose) 3548 iperf_printf(test, report_sender_not_available_format, sp->socket); 3549 } 3550 else { 3551 iperf_printf(test, report_bw_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, report_sender); 3552 } 3553 } 3554 } else { 3555 /* Sender summary, UDP. */ 3556 if (sender_packet_count - sp->omitted_packet_count > 0) { 3557 lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (sender_packet_count - sp->omitted_packet_count); 3558 } 3559 else { 3560 lost_percent = 0.0; 3561 } 3562 if (test->json_output) { 3563 /* 3564 * For hysterical raisins, we only emit one JSON 3565 * object for the UDP summary, and it contains 3566 * information for both the sender and receiver 3567 * side. 3568 * 3569 * The JSON format as currently defined only includes one 3570 * value for the number of packets. We usually want that 3571 * to be the sender's value (how many packets were sent 3572 * by the sender). However this value might not be 3573 * available on the receiver in certain circumstances 3574 * specifically on the server side for a normal test or 3575 * the client side for a reverse-mode test. If this 3576 * is the case, then use the receiver's count of packets 3577 * instead. 3578 */ 3579 int packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count; 3580 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)); 3581 } 3582 else { 3583 /* 3584 * Due to ordering of messages on the control channel, 3585 * the server cannot report on client-side summary 3586 * statistics. If we're the server, omit one set of 3587 * summary statistics to avoid giving meaningless 3588 * results. 3589 */ 3590 if (test->role == 's' && !sp->sender) { 3591 if (test->verbose) 3592 iperf_printf(test, report_sender_not_available_format, sp->socket); 3593 } 3594 else { 3595 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); 3596 } 3597 if ((sp->outoforder_packets - sp->omitted_outoforder_packets) > 0) 3598 iperf_printf(test, report_sum_outoforder, mbuf, start_time, sender_time, (sp->outoforder_packets - sp->omitted_outoforder_packets)); 3599 } 3600 } 3601 3602 if (sp->diskfile_fd >= 0) { 3603 if (fstat(sp->diskfile_fd, &sb) == 0) { 3604 /* In the odd case that it's a zero-sized file, say it was all transferred. */ 3605 int percent_sent = 100, percent_received = 100; 3606 if (sb.st_size > 0) { 3607 percent_sent = (int) ( ( (double) bytes_sent / (double) sb.st_size ) * 100.0 ); 3608 percent_received = (int) ( ( (double) bytes_received / (double) sb.st_size ) * 100.0 ); 3609 } 3610 unit_snprintf(sbuf, UNIT_LEN, (double) sb.st_size, 'A'); 3611 if (test->json_output) 3612 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)); 3613 else 3614 if (stream_must_be_sender) { 3615 iperf_printf(test, report_diskfile, ubuf, sbuf, percent_sent, test->diskfile_name); 3616 } 3617 else { 3618 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_received, 'A'); 3619 iperf_printf(test, report_diskfile, ubuf, sbuf, percent_received, test->diskfile_name); 3620 } 3621 } 3622 } 3623 3624 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_received, 'A'); 3625 if (receiver_time > 0) { 3626 bandwidth = (double) bytes_received / (double) receiver_time; 3627 } 3628 else { 3629 bandwidth = 0.0; 3630 } 3631 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3632 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3633 /* Receiver summary, TCP and SCTP */ 3634 if (test->json_output) 3635 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)); 3636 else 3637 if (test->role == 's' && sp->sender) { 3638 if (test->verbose) 3639 iperf_printf(test, report_receiver_not_available_format, sp->socket); 3640 } 3641 else { 3642 iperf_printf(test, report_bw_format, sp->socket, mbuf, start_time, receiver_time, ubuf, nbuf, report_receiver); 3643 } 3644 } 3645 else { 3646 /* 3647 * Receiver summary, UDP. Note that JSON was emitted with 3648 * the sender summary, so we only deal with human-readable 3649 * data here. 3650 */ 3651 if (! test->json_output) { 3652 if (receiver_packet_count - sp->omitted_packet_count > 0) { 3653 lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (receiver_packet_count - sp->omitted_packet_count); 3654 } 3655 else { 3656 lost_percent = 0.0; 3657 } 3658 3659 if (test->role == 's' && sp->sender) { 3660 if (test->verbose) 3661 iperf_printf(test, report_receiver_not_available_format, sp->socket); 3662 } 3663 else { 3664 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); 3665 } 3666 } 3667 } 3668 } 3669 } 3670 } 3671 3672 if (test->num_streams > 1 || test->json_output) { 3673 unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A'); 3674 /* If no tests were run, arbitrarily set bandwidth to 0. */ 3675 if (sender_time > 0.0) { 3676 bandwidth = (double) total_sent / (double) sender_time; 3677 } 3678 else { 3679 bandwidth = 0.0; 3680 } 3681 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3682 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3683 if (test->sender_has_retransmits) { 3684 /* Summary sum, TCP with retransmits. */ 3685 if (test->json_output) 3686 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)); 3687 else 3688 if (test->role == 's' && !stream_must_be_sender) { 3689 if (test->verbose) 3690 iperf_printf(test, report_sender_not_available_summary_format, "SUM"); 3691 } 3692 else { 3693 iperf_printf(test, report_sum_bw_retrans_format, mbuf, start_time, sender_time, ubuf, nbuf, total_retransmits, report_sender); 3694 } 3695 } else { 3696 /* Summary sum, TCP without retransmits. */ 3697 if (test->json_output) 3698 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)); 3699 else 3700 if (test->role == 's' && !stream_must_be_sender) { 3701 if (test->verbose) 3702 iperf_printf(test, report_sender_not_available_summary_format, "SUM"); 3703 } 3704 else { 3705 iperf_printf(test, report_sum_bw_format, mbuf, start_time, sender_time, ubuf, nbuf, report_sender); 3706 } 3707 } 3708 unit_snprintf(ubuf, UNIT_LEN, (double) total_received, 'A'); 3709 /* If no tests were run, set received bandwidth to 0 */ 3710 if (receiver_time > 0.0) { 3711 bandwidth = (double) total_received / (double) receiver_time; 3712 } 3713 else { 3714 bandwidth = 0.0; 3715 } 3716 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3717 if (test->json_output) 3718 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)); 3719 else 3720 if (test->role == 's' && stream_must_be_sender) { 3721 if (test->verbose) 3722 iperf_printf(test, report_receiver_not_available_summary_format, "SUM"); 3723 } 3724 else { 3725 iperf_printf(test, report_sum_bw_format, mbuf, start_time, receiver_time, ubuf, nbuf, report_receiver); 3726 } 3727 } else { 3728 /* Summary sum, UDP. */ 3729 avg_jitter /= test->num_streams; 3730 /* If no packets were sent, arbitrarily set loss percentage to 0. */ 3731 if (total_packets > 0) { 3732 lost_percent = 100.0 * lost_packets / total_packets; 3733 } 3734 else { 3735 lost_percent = 0.0; 3736 } 3737 if (test->json_output) { 3738 /* 3739 * Original, summary structure. Using this 3740 * structure is not recommended due to 3741 * ambiguities between the sender and receiver. 3742 */ 3743 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)); 3744 /* 3745 * Separate sum_sent and sum_received structures. 3746 * Using these structures to get the most complete 3747 * information about UDP transfer. 3748 */ 3749 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)); 3750 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)); 3751 } else { 3752 /* 3753 * On the client we have both sender and receiver overall summary 3754 * stats. On the server we have only the side that was on the 3755 * server. Output whatever we have. 3756 */ 3757 if (! (test->role == 's' && !stream_must_be_sender) ) { 3758 unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A'); 3759 iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, sender_time, ubuf, nbuf, 0.0, 0, sender_total_packets, 0.0, "sender"); 3760 } 3761 if (! (test->role == 's' && stream_must_be_sender) ) { 3762 3763 unit_snprintf(ubuf, UNIT_LEN, (double) total_received, 'A'); 3764 /* Compute received bandwidth. */ 3765 if (end_time > 0.0) { 3766 bandwidth = (double) total_received / (double) receiver_time; 3767 } 3768 else { 3769 bandwidth = 0.0; 3770 } 3771 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3772 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"); 3773 } 3774 } 3775 } 3776 } 3777 3778 if (test->json_output && current_mode == upper_mode) { 3779 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])); 3780 if (test->protocol->id == Ptcp) { 3781 char *snd_congestion = NULL, *rcv_congestion = NULL; 3782 if (stream_must_be_sender) { 3783 snd_congestion = test->congestion_used; 3784 rcv_congestion = test->remote_congestion_used; 3785 } 3786 else { 3787 snd_congestion = test->remote_congestion_used; 3788 rcv_congestion = test->congestion_used; 3789 } 3790 if (snd_congestion) { 3791 cJSON_AddStringToObject(test->json_end, "sender_tcp_congestion", snd_congestion); 3792 } 3793 if (rcv_congestion) { 3794 cJSON_AddStringToObject(test->json_end, "receiver_tcp_congestion", rcv_congestion); 3795 } 3796 } 3797 } 3798 else { 3799 if (test->verbose) { 3800 if (stream_must_be_sender) { 3801 if (test->bidirectional) { 3802 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]); 3803 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]); 3804 } else 3805 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]); 3806 } 3807 if (test->protocol->id == Ptcp) { 3808 char *snd_congestion = NULL, *rcv_congestion = NULL; 3809 if (stream_must_be_sender) { 3810 snd_congestion = test->congestion_used; 3811 rcv_congestion = test->remote_congestion_used; 3812 } 3813 else { 3814 snd_congestion = test->remote_congestion_used; 3815 rcv_congestion = test->congestion_used; 3816 } 3817 if (snd_congestion) { 3818 iperf_printf(test, "snd_tcp_congestion %s\n", snd_congestion); 3819 } 3820 if (rcv_congestion) { 3821 iperf_printf(test, "rcv_tcp_congestion %s\n", rcv_congestion); 3822 } 3823 } 3824 } 3825 3826 /* Print server output if we're on the client and it was requested/provided */ 3827 if (test->role == 'c' && iperf_get_test_get_server_output(test) && !test->json_output) { 3828 if (test->json_server_output) { 3829 char *str = cJSON_Print(test->json_server_output); 3830 iperf_printf(test, "\nServer JSON output:\n%s\n", str); 3831 cJSON_free(str); 3832 cJSON_Delete(test->json_server_output); 3833 test->json_server_output = NULL; 3834 } 3835 if (test->server_output_text) { 3836 iperf_printf(test, "\nServer output:\n%s\n", test->server_output_text); 3837 test->server_output_text = NULL; 3838 } 3839 } 3840 } 3841 } 3842 3843 /* Set real sender_has_retransmits for current side */ 3844 if (test->mode == BIDIRECTIONAL) 3845 test->sender_has_retransmits = tmp_sender_has_retransmits; 3846 } 3847 3848 /**************************************************************************/ 3849 3850 /** 3851 * Main report-printing callback. 3852 * Prints results either during a test (interval report only) or 3853 * after the entire test has been run (last interval report plus 3854 * overall summary). 3855 */ 3856 void 3857 iperf_reporter_callback(struct iperf_test *test) 3858 { 3859 switch (test->state) { 3860 case TEST_RUNNING: 3861 case STREAM_RUNNING: 3862 /* print interval results for each stream */ 3863 iperf_print_intermediate(test); 3864 break; 3865 case TEST_END: 3866 case DISPLAY_RESULTS: 3867 iperf_print_intermediate(test); 3868 iperf_print_results(test); 3869 break; 3870 } 3871 3872 } 3873 3874 /** 3875 * Print the interval results for one stream. 3876 * This function needs to know about the overall test so it can determine the 3877 * context for printing headers, separators, etc. 3878 */ 3879 static void 3880 print_interval_results(struct iperf_test *test, struct iperf_stream *sp, cJSON *json_interval_streams) 3881 { 3882 char ubuf[UNIT_LEN]; 3883 char nbuf[UNIT_LEN]; 3884 char cbuf[UNIT_LEN]; 3885 char mbuf[UNIT_LEN]; 3886 char zbuf[] = " "; 3887 double st = 0., et = 0.; 3888 struct iperf_time temp_time; 3889 struct iperf_interval_results *irp = NULL; 3890 double bandwidth, lost_percent; 3891 3892 if (test->mode == BIDIRECTIONAL) { 3893 sprintf(mbuf, "[%s-%s]", sp->sender?"TX":"RX", test->role == 'c'?"C":"S"); 3894 } else { 3895 mbuf[0] = '\0'; 3896 zbuf[0] = '\0'; 3897 } 3898 3899 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); /* get last entry in linked list */ 3900 if (irp == NULL) { 3901 iperf_err(test, "print_interval_results error: interval_results is NULL"); 3902 return; 3903 } 3904 if (!test->json_output) { 3905 /* First stream? */ 3906 if (sp == SLIST_FIRST(&test->streams)) { 3907 /* It it's the first interval, print the header; 3908 ** else if there's more than one stream, print the separator; 3909 ** else nothing. 3910 */ 3911 if (iperf_time_compare(&sp->result->start_time, &irp->interval_start_time) == 0) { 3912 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3913 if (test->sender_has_retransmits == 1) { 3914 if (test->bidirectional) 3915 iperf_printf(test, "%s", report_bw_retrans_cwnd_header_bidir); 3916 else 3917 iperf_printf(test, "%s", report_bw_retrans_cwnd_header); 3918 } 3919 else { 3920 if (test->bidirectional) 3921 iperf_printf(test, "%s", report_bw_header_bidir); 3922 else 3923 iperf_printf(test, "%s", report_bw_header); 3924 } 3925 } else { 3926 if (test->mode == SENDER) { 3927 iperf_printf(test, "%s", report_bw_udp_sender_header); 3928 } else if (test->mode == RECEIVER){ 3929 iperf_printf(test, "%s", report_bw_udp_header); 3930 } else { 3931 /* BIDIRECTIONAL */ 3932 iperf_printf(test, "%s", report_bw_udp_header_bidir); 3933 } 3934 } 3935 } else if (test->num_streams > 1) 3936 iperf_printf(test, "%s", report_bw_separator); 3937 } 3938 } 3939 3940 unit_snprintf(ubuf, UNIT_LEN, (double) (irp->bytes_transferred), 'A'); 3941 if (irp->interval_duration > 0.0) { 3942 bandwidth = (double) irp->bytes_transferred / (double) irp->interval_duration; 3943 } 3944 else { 3945 bandwidth = 0.0; 3946 } 3947 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 3948 3949 iperf_time_diff(&sp->result->start_time, &irp->interval_start_time, &temp_time); 3950 st = iperf_time_in_secs(&temp_time); 3951 iperf_time_diff(&sp->result->start_time, &irp->interval_end_time, &temp_time); 3952 et = iperf_time_in_secs(&temp_time); 3953 3954 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 3955 if (test->sender_has_retransmits == 1 && sp->sender) { 3956 /* Interval, TCP with retransmits. */ 3957 if (test->json_output) 3958 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)); 3959 else { 3960 unit_snprintf(cbuf, UNIT_LEN, irp->snd_cwnd, 'A'); 3961 iperf_printf(test, report_bw_retrans_cwnd_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->interval_retrans, cbuf, irp->omitted?report_omitted:""); 3962 } 3963 } else { 3964 /* Interval, TCP without retransmits. */ 3965 if (test->json_output) 3966 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)); 3967 else 3968 iperf_printf(test, report_bw_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->omitted?report_omitted:""); 3969 } 3970 } else { 3971 /* Interval, UDP. */ 3972 if (sp->sender) { 3973 if (test->json_output) 3974 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)); 3975 else 3976 iperf_printf(test, report_bw_udp_sender_format, sp->socket, mbuf, st, et, ubuf, nbuf, zbuf, irp->interval_packet_count, irp->omitted?report_omitted:""); 3977 } else { 3978 if (irp->interval_packet_count > 0) { 3979 lost_percent = 100.0 * irp->interval_cnt_error / irp->interval_packet_count; 3980 } 3981 else { 3982 lost_percent = 0.0; 3983 } 3984 if (test->json_output) 3985 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)); 3986 else 3987 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:""); 3988 } 3989 } 3990 3991 if (test->logfile || test->forceflush) 3992 iflush(test); 3993 } 3994 3995 /**************************************************************************/ 3996 void 3997 iperf_free_stream(struct iperf_stream *sp) 3998 { 3999 struct iperf_interval_results *irp, *nirp; 4000 4001 /* XXX: need to free interval list too! */ 4002 munmap(sp->buffer, sp->test->settings->blksize); 4003 close(sp->buffer_fd); 4004 if (sp->diskfile_fd >= 0) 4005 close(sp->diskfile_fd); 4006 for (irp = TAILQ_FIRST(&sp->result->interval_results); irp != NULL; irp = nirp) { 4007 nirp = TAILQ_NEXT(irp, irlistentries); 4008 free(irp); 4009 } 4010 free(sp->result); 4011 if (sp->send_timer != NULL) 4012 tmr_cancel(sp->send_timer); 4013 free(sp); 4014 } 4015 4016 /**************************************************************************/ 4017 struct iperf_stream * 4018 iperf_new_stream(struct iperf_test *test, int s, int sender) 4019 { 4020 struct iperf_stream *sp; 4021 int ret = 0; 4022 4023 char template[1024]; 4024 if (test->tmp_template) { 4025 snprintf(template, sizeof(template) / sizeof(char), "%s", test->tmp_template); 4026 } else { 4027 //find the system temporary dir *unix, windows, cygwin support 4028 char* tempdir = getenv("TMPDIR"); 4029 if (tempdir == 0){ 4030 tempdir = getenv("TEMP"); 4031 } 4032 if (tempdir == 0){ 4033 tempdir = getenv("TMP"); 4034 } 4035 if (tempdir == 0){ 4036 tempdir = "/tmp"; 4037 } 4038 snprintf(template, sizeof(template) / sizeof(char), "%s/iperf3.XXXXXX", tempdir); 4039 } 4040 4041 sp = (struct iperf_stream *) malloc(sizeof(struct iperf_stream)); 4042 if (!sp) { 4043 i_errno = IECREATESTREAM; 4044 return NULL; 4045 } 4046 4047 memset(sp, 0, sizeof(struct iperf_stream)); 4048 4049 sp->sender = sender; 4050 sp->test = test; 4051 sp->settings = test->settings; 4052 sp->result = (struct iperf_stream_result *) malloc(sizeof(struct iperf_stream_result)); 4053 if (!sp->result) { 4054 free(sp); 4055 i_errno = IECREATESTREAM; 4056 return NULL; 4057 } 4058 4059 memset(sp->result, 0, sizeof(struct iperf_stream_result)); 4060 TAILQ_INIT(&sp->result->interval_results); 4061 4062 /* Create and randomize the buffer */ 4063 sp->buffer_fd = mkstemp(template); 4064 if (sp->buffer_fd == -1) { 4065 i_errno = IECREATESTREAM; 4066 free(sp->result); 4067 free(sp); 4068 return NULL; 4069 } 4070 if (unlink(template) < 0) { 4071 i_errno = IECREATESTREAM; 4072 free(sp->result); 4073 free(sp); 4074 return NULL; 4075 } 4076 if (ftruncate(sp->buffer_fd, test->settings->blksize) < 0) { 4077 i_errno = IECREATESTREAM; 4078 free(sp->result); 4079 free(sp); 4080 return NULL; 4081 } 4082 sp->buffer = (char *) mmap(NULL, test->settings->blksize, PROT_READ|PROT_WRITE, MAP_PRIVATE, sp->buffer_fd, 0); 4083 if (sp->buffer == MAP_FAILED) { 4084 i_errno = IECREATESTREAM; 4085 free(sp->result); 4086 free(sp); 4087 return NULL; 4088 } 4089 sp->pending_size = 0; 4090 4091 /* Set socket */ 4092 sp->socket = s; 4093 4094 sp->snd = test->protocol->send; 4095 sp->rcv = test->protocol->recv; 4096 4097 if (test->diskfile_name != (char*) 0) { 4098 sp->diskfile_fd = open(test->diskfile_name, sender ? O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC), S_IRUSR|S_IWUSR); 4099 if (sp->diskfile_fd == -1) { 4100 i_errno = IEFILE; 4101 munmap(sp->buffer, sp->test->settings->blksize); 4102 free(sp->result); 4103 free(sp); 4104 return NULL; 4105 } 4106 sp->snd2 = sp->snd; 4107 sp->snd = diskfile_send; 4108 sp->rcv2 = sp->rcv; 4109 sp->rcv = diskfile_recv; 4110 } else 4111 sp->diskfile_fd = -1; 4112 4113 /* Initialize stream */ 4114 if (test->repeating_payload) 4115 fill_with_repeating_pattern(sp->buffer, test->settings->blksize); 4116 else 4117 ret = readentropy(sp->buffer, test->settings->blksize); 4118 4119 if ((ret < 0) || (iperf_init_stream(sp, test) < 0)) { 4120 close(sp->buffer_fd); 4121 munmap(sp->buffer, sp->test->settings->blksize); 4122 free(sp->result); 4123 free(sp); 4124 return NULL; 4125 } 4126 iperf_add_stream(test, sp); 4127 4128 return sp; 4129 } 4130 4131 /**************************************************************************/ 4132 int 4133 iperf_init_stream(struct iperf_stream *sp, struct iperf_test *test) 4134 { 4135 socklen_t len; 4136 int opt; 4137 4138 len = sizeof(struct sockaddr_storage); 4139 if (getsockname(sp->socket, (struct sockaddr *) &sp->local_addr, &len) < 0) { 4140 i_errno = IEINITSTREAM; 4141 return -1; 4142 } 4143 len = sizeof(struct sockaddr_storage); 4144 if (getpeername(sp->socket, (struct sockaddr *) &sp->remote_addr, &len) < 0) { 4145 i_errno = IEINITSTREAM; 4146 return -1; 4147 } 4148 4149 /* Set IP TOS */ 4150 if ((opt = test->settings->tos)) { 4151 if (getsockdomain(sp->socket) == AF_INET6) { 4152 #ifdef IPV6_TCLASS 4153 if (setsockopt(sp->socket, IPPROTO_IPV6, IPV6_TCLASS, &opt, sizeof(opt)) < 0) { 4154 i_errno = IESETCOS; 4155 return -1; 4156 } 4157 #else 4158 i_errno = IESETCOS; 4159 return -1; 4160 #endif 4161 } else { 4162 if (setsockopt(sp->socket, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)) < 0) { 4163 i_errno = IESETTOS; 4164 return -1; 4165 } 4166 } 4167 } 4168 4169 #if defined(HAVE_DONT_FRAGMENT) 4170 /* Set Don't Fragment (DF). Only applicable to IPv4/UDP tests. */ 4171 if (iperf_get_test_protocol_id(test) == Pudp && 4172 getsockdomain(sp->socket) == AF_INET && 4173 iperf_get_dont_fragment(test)) { 4174 4175 /* 4176 * There are multiple implementations of this feature depending on the OS. 4177 * We need to handle separately Linux, UNIX, and Windows, as well as 4178 * the case that DF isn't supported at all (such as on macOS). 4179 */ 4180 #if defined(IP_MTU_DISCOVER) /* Linux version of IP_DONTFRAG */ 4181 opt = IP_PMTUDISC_DO; 4182 if (setsockopt(sp->socket, IPPROTO_IP, IP_MTU_DISCOVER, &opt, sizeof(opt)) < 0) { 4183 i_errno = IESETDONTFRAGMENT; 4184 return -1; 4185 } 4186 #else 4187 #if defined(IP_DONTFRAG) /* UNIX does IP_DONTFRAG */ 4188 opt = 1; 4189 if (setsockopt(sp->socket, IPPROTO_IP, IP_DONTFRAG, &opt, sizeof(opt)) < 0) { 4190 i_errno = IESETDONTFRAGMENT; 4191 return -1; 4192 } 4193 #else 4194 #if defined(IP_DONTFRAGMENT) /* Windows does IP_DONTFRAGMENT */ 4195 opt = 1; 4196 if (setsockopt(sp->socket, IPPROTO_IP, IP_DONTFRAGMENT, &opt, sizeof(opt)) < 0) { 4197 i_errno = IESETDONTFRAGMENT; 4198 return -1; 4199 } 4200 #else 4201 i_errno = IESETDONTFRAGMENT; 4202 return -1; 4203 #endif /* IP_DONTFRAGMENT */ 4204 #endif /* IP_DONTFRAG */ 4205 #endif /* IP_MTU_DISCOVER */ 4206 } 4207 #endif /* HAVE_DONT_FRAGMENT */ 4208 return 0; 4209 } 4210 4211 /**************************************************************************/ 4212 void 4213 iperf_add_stream(struct iperf_test *test, struct iperf_stream *sp) 4214 { 4215 int i; 4216 struct iperf_stream *n, *prev; 4217 4218 if (SLIST_EMPTY(&test->streams)) { 4219 SLIST_INSERT_HEAD(&test->streams, sp, streams); 4220 sp->id = 1; 4221 } else { 4222 // for (n = test->streams, i = 2; n->next; n = n->next, ++i); 4223 i = 2; 4224 SLIST_FOREACH(n, &test->streams, streams) { 4225 prev = n; 4226 ++i; 4227 } 4228 SLIST_INSERT_AFTER(prev, sp, streams); 4229 sp->id = i; 4230 } 4231 } 4232 4233 /* This pair of routines gets inserted into the snd/rcv function pointers 4234 ** when there's a -F flag. They handle the file stuff and call the real 4235 ** snd/rcv functions, which have been saved in snd2/rcv2. 4236 ** 4237 ** The advantage of doing it this way is that in the much more common 4238 ** case of no -F flag, there is zero extra overhead. 4239 */ 4240 4241 static int 4242 diskfile_send(struct iperf_stream *sp) 4243 { 4244 int r; 4245 int buffer_left = sp->diskfile_left; // represents total data in buffer to be sent out 4246 static int rtot; 4247 4248 /* if needed, read enough data from the disk to fill up the buffer */ 4249 if (sp->diskfile_left < sp->test->settings->blksize && !sp->test->done) { 4250 r = read(sp->diskfile_fd, sp->buffer, sp->test->settings->blksize - 4251 sp->diskfile_left); 4252 buffer_left += r; 4253 rtot += r; 4254 if (sp->test->debug) { 4255 printf("read %d bytes from file, %d total\n", r, rtot); 4256 } 4257 4258 // If the buffer doesn't contain a full buffer at this point, 4259 // adjust the size of the data to send. 4260 if (buffer_left != sp->test->settings->blksize) { 4261 if (sp->test->debug) 4262 printf("possible eof\n"); 4263 // setting data size to be sent, 4264 // which is less than full block/buffer size 4265 // (to be used by iperf_tcp_send, etc.) 4266 sp->pending_size = buffer_left; 4267 } 4268 4269 // If there's no work left, we're done. 4270 if (buffer_left == 0) { 4271 sp->test->done = 1; 4272 if (sp->test->debug) 4273 printf("done\n"); 4274 } 4275 } 4276 4277 // If there's no data left in the file or in the buffer, we're done. 4278 // No more data available to be sent. 4279 // Return without sending data to the network 4280 if( sp->test->done || buffer_left == 0 ){ 4281 if (sp->test->debug) 4282 printf("already done\n"); 4283 sp->test->done = 1; 4284 return 0; 4285 } 4286 4287 r = sp->snd2(sp); 4288 if (r < 0) { 4289 return r; 4290 } 4291 /* 4292 * Compute how much data is in the buffer but didn't get sent. 4293 * If there are bytes that got left behind, slide them to the 4294 * front of the buffer so they can hopefully go out on the next 4295 * pass. 4296 */ 4297 sp->diskfile_left = buffer_left - r; 4298 if (sp->diskfile_left && sp->diskfile_left < sp->test->settings->blksize) { 4299 memcpy(sp->buffer, 4300 sp->buffer + (sp->test->settings->blksize - sp->diskfile_left), 4301 sp->diskfile_left); 4302 if (sp->test->debug) 4303 printf("Shifting %d bytes by %d\n", sp->diskfile_left, (sp->test->settings->blksize - sp->diskfile_left)); 4304 } 4305 return r; 4306 } 4307 4308 static int 4309 diskfile_recv(struct iperf_stream *sp) 4310 { 4311 int r; 4312 4313 r = sp->rcv2(sp); 4314 if (r > 0) { 4315 (void) write(sp->diskfile_fd, sp->buffer, r); 4316 } 4317 return r; 4318 } 4319 4320 4321 void 4322 iperf_catch_sigend(void (*handler)(int)) 4323 { 4324 #ifdef SIGINT 4325 signal(SIGINT, handler); 4326 #endif 4327 #ifdef SIGTERM 4328 signal(SIGTERM, handler); 4329 #endif 4330 #ifdef SIGHUP 4331 signal(SIGHUP, handler); 4332 #endif 4333 } 4334 4335 /** 4336 * Called as a result of getting a signal. 4337 * Depending on the current state of the test (and the role of this 4338 * process) compute and report one more set of ending statistics 4339 * before cleaning up and exiting. 4340 */ 4341 void 4342 iperf_got_sigend(struct iperf_test *test) 4343 { 4344 /* 4345 * If we're the client, or if we're a server and running a test, 4346 * then dump out the accumulated stats so far. 4347 */ 4348 if (test->role == 'c' || 4349 (test->role == 's' && test->state == TEST_RUNNING)) { 4350 4351 test->done = 1; 4352 cpu_util(test->cpu_util); 4353 test->stats_callback(test); 4354 test->state = DISPLAY_RESULTS; /* change local state only */ 4355 if (test->on_test_finish) 4356 test->on_test_finish(test); 4357 test->reporter_callback(test); 4358 } 4359 4360 if (test->ctrl_sck >= 0) { 4361 test->state = (test->role == 'c') ? CLIENT_TERMINATE : SERVER_TERMINATE; 4362 (void) Nwrite(test->ctrl_sck, (char*) &test->state, sizeof(signed char), Ptcp); 4363 } 4364 i_errno = (test->role == 'c') ? IECLIENTTERM : IESERVERTERM; 4365 iperf_errexit(test, "interrupt - %s", iperf_strerror(i_errno)); 4366 } 4367 4368 /* Try to write a PID file if requested, return -1 on an error. */ 4369 int 4370 iperf_create_pidfile(struct iperf_test *test) 4371 { 4372 if (test->pidfile) { 4373 int fd; 4374 char buf[8]; 4375 4376 /* See if the file already exists and we can read it. */ 4377 fd = open(test->pidfile, O_RDONLY, 0); 4378 if (fd >= 0) { 4379 if (read(fd, buf, sizeof(buf) - 1) >= 0) { 4380 4381 /* We read some bytes, see if they correspond to a valid PID */ 4382 pid_t pid; 4383 pid = atoi(buf); 4384 if (pid > 0) { 4385 4386 /* See if the process exists. */ 4387 if (kill(pid, 0) == 0) { 4388 /* 4389 * Make sure not to try to delete existing PID file by 4390 * scribbling over the pathname we'd use to refer to it. 4391 * Then exit with an error. 4392 */ 4393 free(test->pidfile); 4394 test->pidfile = NULL; 4395 iperf_errexit(test, "Another instance of iperf3 appears to be running"); 4396 } 4397 } 4398 } 4399 } 4400 4401 /* 4402 * File didn't exist, we couldn't read it, or it didn't correspond to 4403 * a running process. Try to create it. 4404 */ 4405 fd = open(test->pidfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR); 4406 if (fd < 0) { 4407 return -1; 4408 } 4409 snprintf(buf, sizeof(buf), "%d", getpid()); /* no trailing newline */ 4410 if (write(fd, buf, strlen(buf)) < 0) { 4411 return -1; 4412 } 4413 if (close(fd) < 0) { 4414 return -1; 4415 }; 4416 } 4417 return 0; 4418 } 4419 4420 /* Get rid of a PID file, return -1 on error. */ 4421 int 4422 iperf_delete_pidfile(struct iperf_test *test) 4423 { 4424 if (test->pidfile) { 4425 if (unlink(test->pidfile) < 0) { 4426 return -1; 4427 } 4428 } 4429 return 0; 4430 } 4431 4432 int 4433 iperf_json_start(struct iperf_test *test) 4434 { 4435 test->json_top = cJSON_CreateObject(); 4436 if (test->json_top == NULL) 4437 return -1; 4438 test->json_start = cJSON_CreateObject(); 4439 if (test->json_start == NULL) 4440 return -1; 4441 cJSON_AddItemToObject(test->json_top, "start", test->json_start); 4442 test->json_connected = cJSON_CreateArray(); 4443 if (test->json_connected == NULL) 4444 return -1; 4445 cJSON_AddItemToObject(test->json_start, "connected", test->json_connected); 4446 test->json_intervals = cJSON_CreateArray(); 4447 if (test->json_intervals == NULL) 4448 return -1; 4449 cJSON_AddItemToObject(test->json_top, "intervals", test->json_intervals); 4450 test->json_end = cJSON_CreateObject(); 4451 if (test->json_end == NULL) 4452 return -1; 4453 cJSON_AddItemToObject(test->json_top, "end", test->json_end); 4454 return 0; 4455 } 4456 4457 int 4458 iperf_json_finish(struct iperf_test *test) 4459 { 4460 if (test->title) 4461 cJSON_AddStringToObject(test->json_top, "title", test->title); 4462 if (test->extra_data) 4463 cJSON_AddStringToObject(test->json_top, "extra_data", test->extra_data); 4464 /* Include server output */ 4465 if (test->json_server_output) { 4466 cJSON_AddItemToObject(test->json_top, "server_output_json", test->json_server_output); 4467 } 4468 if (test->server_output_text) { 4469 cJSON_AddStringToObject(test->json_top, "server_output_text", test->server_output_text); 4470 } 4471 // Get ASCII rendering of JSON structure. Then make our 4472 // own copy of it and return the storage that cJSON allocated 4473 // on our behalf. We keep our own copy around. 4474 char *str = cJSON_Print(test->json_top); 4475 if (str == NULL) 4476 return -1; 4477 test->json_output_string = strdup(str); 4478 cJSON_free(str); 4479 if (test->json_output_string == NULL) 4480 return -1; 4481 fprintf(test->outfile, "%s\n", test->json_output_string); 4482 iflush(test); 4483 cJSON_Delete(test->json_top); 4484 test->json_top = test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL; 4485 return 0; 4486 } 4487 4488 4489 /* CPU affinity stuff - Linux, FreeBSD, and Windows only. */ 4490 4491 int 4492 iperf_setaffinity(struct iperf_test *test, int affinity) 4493 { 4494 #if defined(HAVE_SCHED_SETAFFINITY) 4495 cpu_set_t cpu_set; 4496 4497 CPU_ZERO(&cpu_set); 4498 CPU_SET(affinity, &cpu_set); 4499 if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0) { 4500 i_errno = IEAFFINITY; 4501 return -1; 4502 } 4503 return 0; 4504 #elif defined(HAVE_CPUSET_SETAFFINITY) 4505 cpuset_t cpumask; 4506 4507 if(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, 4508 sizeof(cpuset_t), &test->cpumask) != 0) { 4509 i_errno = IEAFFINITY; 4510 return -1; 4511 } 4512 4513 CPU_ZERO(&cpumask); 4514 CPU_SET(affinity, &cpumask); 4515 4516 if(cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_PID, -1, 4517 sizeof(cpuset_t), &cpumask) != 0) { 4518 i_errno = IEAFFINITY; 4519 return -1; 4520 } 4521 return 0; 4522 #elif defined(HAVE_SETPROCESSAFFINITYMASK) 4523 HANDLE process = GetCurrentProcess(); 4524 DWORD_PTR processAffinityMask = 1 << affinity; 4525 4526 if (SetProcessAffinityMask(process, processAffinityMask) == 0) { 4527 i_errno = IEAFFINITY; 4528 return -1; 4529 } 4530 return 0; 4531 #else /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */ 4532 i_errno = IEAFFINITY; 4533 return -1; 4534 #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */ 4535 } 4536 4537 int 4538 iperf_clearaffinity(struct iperf_test *test) 4539 { 4540 #if defined(HAVE_SCHED_SETAFFINITY) 4541 cpu_set_t cpu_set; 4542 int i; 4543 4544 CPU_ZERO(&cpu_set); 4545 for (i = 0; i < CPU_SETSIZE; ++i) 4546 CPU_SET(i, &cpu_set); 4547 if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0) { 4548 i_errno = IEAFFINITY; 4549 return -1; 4550 } 4551 return 0; 4552 #elif defined(HAVE_CPUSET_SETAFFINITY) 4553 if(cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_PID, -1, 4554 sizeof(cpuset_t), &test->cpumask) != 0) { 4555 i_errno = IEAFFINITY; 4556 return -1; 4557 } 4558 return 0; 4559 #elif defined(HAVE_SETPROCESSAFFINITYMASK) 4560 HANDLE process = GetCurrentProcess(); 4561 DWORD_PTR processAffinityMask; 4562 DWORD_PTR lpSystemAffinityMask; 4563 4564 if (GetProcessAffinityMask(process, &processAffinityMask, &lpSystemAffinityMask) == 0 4565 || SetProcessAffinityMask(process, lpSystemAffinityMask) == 0) { 4566 i_errno = IEAFFINITY; 4567 return -1; 4568 } 4569 return 0; 4570 #else /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */ 4571 i_errno = IEAFFINITY; 4572 return -1; 4573 #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */ 4574 } 4575 4576 static char iperf_timestr[100]; 4577 static char linebuffer[1024]; 4578 4579 int 4580 iperf_printf(struct iperf_test *test, const char* format, ...) 4581 { 4582 va_list argp; 4583 int r = 0, r0; 4584 time_t now; 4585 struct tm *ltm = NULL; 4586 char *ct = NULL; 4587 4588 /* Timestamp if requested */ 4589 if (iperf_get_test_timestamps(test)) { 4590 time(&now); 4591 ltm = localtime(&now); 4592 strftime(iperf_timestr, sizeof(iperf_timestr), iperf_get_test_timestamp_format(test), ltm); 4593 ct = iperf_timestr; 4594 } 4595 4596 /* 4597 * There are roughly two use cases here. If we're the client, 4598 * want to print stuff directly to the output stream. 4599 * If we're the sender we might need to buffer up output to send 4600 * to the client. 4601 * 4602 * This doesn't make a whole lot of difference except there are 4603 * some chunks of output on the client (on particular the whole 4604 * of the server output with --get-server-output) that could 4605 * easily exceed the size of the line buffer, but which don't need 4606 * to be buffered up anyway. 4607 */ 4608 if (test->role == 'c') { 4609 if (ct) { 4610 r0 = fprintf(test->outfile, "%s", ct); 4611 if (r0 < 0) 4612 return r0; 4613 r += r0; 4614 } 4615 if (test->title) { 4616 r0 = fprintf(test->outfile, "%s: ", test->title); 4617 if (r0 < 0) 4618 return r0; 4619 r += r0; 4620 } 4621 va_start(argp, format); 4622 r0 = vfprintf(test->outfile, format, argp); 4623 va_end(argp); 4624 if (r0 < 0) 4625 return r0; 4626 r += r0; 4627 } 4628 else if (test->role == 's') { 4629 if (ct) { 4630 r0 = snprintf(linebuffer, sizeof(linebuffer), "%s", ct); 4631 if (r0 < 0) 4632 return r0; 4633 r += r0; 4634 } 4635 /* Should always be true as long as sizeof(ct) < sizeof(linebuffer) */ 4636 if (r < sizeof(linebuffer)) { 4637 va_start(argp, format); 4638 r0 = vsnprintf(linebuffer + r, sizeof(linebuffer) - r, format, argp); 4639 va_end(argp); 4640 if (r0 < 0) 4641 return r0; 4642 r += r0; 4643 } 4644 fprintf(test->outfile, "%s", linebuffer); 4645 4646 if (test->role == 's' && iperf_get_test_get_server_output(test)) { 4647 struct iperf_textline *l = (struct iperf_textline *) malloc(sizeof(struct iperf_textline)); 4648 l->line = strdup(linebuffer); 4649 TAILQ_INSERT_TAIL(&(test->server_output_list), l, textlineentries); 4650 } 4651 } 4652 return r; 4653 } 4654 4655 int 4656 iflush(struct iperf_test *test) 4657 { 4658 return fflush(test->outfile); 4659 } 4660