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