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