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