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