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