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