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