1 /* 2 * iperf, Copyright (c) 2014, 2015, The Regents of the University of 3 * California, through Lawrence Berkeley National Laboratory (subject 4 * to receipt of any required approvals from the U.S. Dept. of 5 * Energy). All rights reserved. 6 * 7 * If you have questions about your rights to use or distribute this 8 * software, please contact Berkeley Lab's Technology Transfer 9 * Department at [email protected]. 10 * 11 * NOTICE. This software is owned by the U.S. Department of Energy. 12 * As such, the U.S. Government has been granted for itself and others 13 * acting on its behalf a paid-up, nonexclusive, irrevocable, 14 * worldwide license in the Software to reproduce, prepare derivative 15 * works, and perform publicly and display publicly. Beginning five 16 * (5) years after the date permission to assert copyright is obtained 17 * from the U.S. Department of Energy, and subject to any subsequent 18 * five (5) year renewals, the U.S. Government is granted for itself 19 * and others acting on its behalf a paid-up, nonexclusive, 20 * irrevocable, worldwide license in the Software to reproduce, 21 * prepare derivative works, distribute copies to the public, perform 22 * publicly and display publicly, and to permit others to do so. 23 * 24 * This code is distributed under a BSD style license, see the LICENSE file 25 * for complete information. 26 */ 27 #define _GNU_SOURCE 28 #define __USE_GNU 29 30 #include "iperf_config.h" 31 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <getopt.h> 36 #include <errno.h> 37 #include <signal.h> 38 #include <unistd.h> 39 #include <assert.h> 40 #include <fcntl.h> 41 #include <sys/socket.h> 42 #include <sys/types.h> 43 #include <netinet/in.h> 44 #include <arpa/inet.h> 45 #include <netdb.h> 46 #include <pthread.h> 47 #ifdef HAVE_STDINT_H 48 #include <stdint.h> 49 #endif 50 #include <netinet/tcp.h> 51 #include <sys/time.h> 52 #include <sys/resource.h> 53 #include <sys/mman.h> 54 #include <sys/stat.h> 55 #include <sched.h> 56 #include <setjmp.h> 57 #include <stdarg.h> 58 59 #if defined(HAVE_CPUSET_SETAFFINITY) 60 #include <sys/param.h> 61 #include <sys/cpuset.h> 62 #endif /* HAVE_CPUSET_SETAFFINITY */ 63 64 #include "net.h" 65 #include "iperf.h" 66 #include "iperf_api.h" 67 #include "iperf_udp.h" 68 #include "iperf_tcp.h" 69 #if defined(HAVE_SCTP) 70 #include "iperf_sctp.h" 71 #endif /* HAVE_SCTP */ 72 #include "timer.h" 73 74 #include "cjson.h" 75 #include "units.h" 76 #include "tcp_window_size.h" 77 #include "iperf_util.h" 78 #include "iperf_locale.h" 79 #include "version.h" 80 81 /* Forwards. */ 82 static int send_parameters(struct iperf_test *test); 83 static int get_parameters(struct iperf_test *test); 84 static int send_results(struct iperf_test *test); 85 static int get_results(struct iperf_test *test); 86 static int diskfile_send(struct iperf_stream *sp); 87 static int diskfile_recv(struct iperf_stream *sp); 88 static int JSON_write(int fd, cJSON *json); 89 static void print_interval_results(struct iperf_test *test, struct iperf_stream *sp, cJSON *json_interval_streams); 90 static cJSON *JSON_read(int fd); 91 92 93 /*************************** Print usage functions ****************************/ 94 95 void 96 usage() 97 { 98 fputs(usage_shortstr, stderr); 99 } 100 101 102 void 103 usage_long() 104 { 105 fprintf(stderr, usage_longstr, UDP_RATE / (1024*1024), DURATION, DEFAULT_TCP_BLKSIZE / 1024, DEFAULT_UDP_BLKSIZE / 1024); 106 } 107 108 109 void warning(char *str) 110 { 111 fprintf(stderr, "warning: %s\n", str); 112 } 113 114 115 /************** Getter routines for some fields inside iperf_test *************/ 116 117 int 118 iperf_get_verbose(struct iperf_test *ipt) 119 { 120 return ipt->verbose; 121 } 122 123 int 124 iperf_get_control_socket(struct iperf_test *ipt) 125 { 126 return ipt->ctrl_sck; 127 } 128 129 int 130 iperf_get_test_omit(struct iperf_test *ipt) 131 { 132 return ipt->omit; 133 } 134 135 int 136 iperf_get_test_duration(struct iperf_test *ipt) 137 { 138 return ipt->duration; 139 } 140 141 uint64_t 142 iperf_get_test_rate(struct iperf_test *ipt) 143 { 144 return ipt->settings->rate; 145 } 146 147 int 148 iperf_get_test_burst(struct iperf_test *ipt) 149 { 150 return ipt->settings->burst; 151 } 152 153 char 154 iperf_get_test_role(struct iperf_test *ipt) 155 { 156 return ipt->role; 157 } 158 159 int 160 iperf_get_test_reverse(struct iperf_test *ipt) 161 { 162 return ipt->reverse; 163 } 164 165 int 166 iperf_get_test_blksize(struct iperf_test *ipt) 167 { 168 return ipt->settings->blksize; 169 } 170 171 FILE * 172 iperf_get_test_outfile (struct iperf_test *ipt) 173 { 174 return ipt->outfile; 175 } 176 177 int 178 iperf_get_test_socket_bufsize(struct iperf_test *ipt) 179 { 180 return ipt->settings->socket_bufsize; 181 } 182 183 double 184 iperf_get_test_reporter_interval(struct iperf_test *ipt) 185 { 186 return ipt->reporter_interval; 187 } 188 189 double 190 iperf_get_test_stats_interval(struct iperf_test *ipt) 191 { 192 return ipt->stats_interval; 193 } 194 195 int 196 iperf_get_test_num_streams(struct iperf_test *ipt) 197 { 198 return ipt->num_streams; 199 } 200 201 int 202 iperf_get_test_server_port(struct iperf_test *ipt) 203 { 204 return ipt->server_port; 205 } 206 207 char* 208 iperf_get_test_server_hostname(struct iperf_test *ipt) 209 { 210 return ipt->server_hostname; 211 } 212 213 char* 214 iperf_get_test_template(struct iperf_test *ipt) 215 { 216 return ipt->tmp_template; 217 } 218 219 int 220 iperf_get_test_protocol_id(struct iperf_test *ipt) 221 { 222 return ipt->protocol->id; 223 } 224 225 int 226 iperf_get_test_json_output(struct iperf_test *ipt) 227 { 228 return ipt->json_output; 229 } 230 231 char * 232 iperf_get_test_json_output_string(struct iperf_test *ipt) 233 { 234 return ipt->json_output_string; 235 } 236 237 int 238 iperf_get_test_zerocopy(struct iperf_test *ipt) 239 { 240 return ipt->zerocopy; 241 } 242 243 int 244 iperf_get_test_get_server_output(struct iperf_test *ipt) 245 { 246 return ipt->get_server_output; 247 } 248 249 char 250 iperf_get_test_unit_format(struct iperf_test *ipt) 251 { 252 return ipt->settings->unit_format; 253 } 254 255 char * 256 iperf_get_test_bind_address(struct iperf_test *ipt) 257 { 258 return ipt->bind_address; 259 } 260 261 int 262 iperf_get_test_udp_counters_64bit(struct iperf_test *ipt) 263 { 264 return ipt->udp_counters_64bit; 265 } 266 267 int 268 iperf_get_test_one_off(struct iperf_test *ipt) 269 { 270 return ipt->one_off; 271 } 272 273 /************** Setter routines for some fields inside iperf_test *************/ 274 275 void 276 iperf_set_verbose(struct iperf_test *ipt, int verbose) 277 { 278 ipt->verbose = verbose; 279 } 280 281 void 282 iperf_set_control_socket(struct iperf_test *ipt, int ctrl_sck) 283 { 284 ipt->ctrl_sck = ctrl_sck; 285 } 286 287 void 288 iperf_set_test_omit(struct iperf_test *ipt, int omit) 289 { 290 ipt->omit = omit; 291 } 292 293 void 294 iperf_set_test_duration(struct iperf_test *ipt, int duration) 295 { 296 ipt->duration = duration; 297 } 298 299 void 300 iperf_set_test_reporter_interval(struct iperf_test *ipt, double reporter_interval) 301 { 302 ipt->reporter_interval = reporter_interval; 303 } 304 305 void 306 iperf_set_test_stats_interval(struct iperf_test *ipt, double stats_interval) 307 { 308 ipt->stats_interval = stats_interval; 309 } 310 311 void 312 iperf_set_test_state(struct iperf_test *ipt, signed char state) 313 { 314 ipt->state = state; 315 } 316 317 void 318 iperf_set_test_blksize(struct iperf_test *ipt, int blksize) 319 { 320 ipt->settings->blksize = blksize; 321 } 322 323 void 324 iperf_set_test_rate(struct iperf_test *ipt, uint64_t rate) 325 { 326 ipt->settings->rate = rate; 327 } 328 329 void 330 iperf_set_test_burst(struct iperf_test *ipt, int burst) 331 { 332 ipt->settings->burst = burst; 333 } 334 335 void 336 iperf_set_test_server_port(struct iperf_test *ipt, int server_port) 337 { 338 ipt->server_port = server_port; 339 } 340 341 void 342 iperf_set_test_socket_bufsize(struct iperf_test *ipt, int socket_bufsize) 343 { 344 ipt->settings->socket_bufsize = socket_bufsize; 345 } 346 347 void 348 iperf_set_test_num_streams(struct iperf_test *ipt, int num_streams) 349 { 350 ipt->num_streams = num_streams; 351 } 352 353 static void 354 check_sender_has_retransmits(struct iperf_test *ipt) 355 { 356 if (ipt->sender && ipt->protocol->id == Ptcp && has_tcpinfo_retransmits()) 357 ipt->sender_has_retransmits = 1; 358 else 359 ipt->sender_has_retransmits = 0; 360 } 361 362 void 363 iperf_set_test_role(struct iperf_test *ipt, char role) 364 { 365 ipt->role = role; 366 if (role == 'c') 367 ipt->sender = 1; 368 else if (role == 's') 369 ipt->sender = 0; 370 if (ipt->reverse) 371 ipt->sender = ! ipt->sender; 372 check_sender_has_retransmits(ipt); 373 } 374 375 void 376 iperf_set_test_server_hostname(struct iperf_test *ipt, char *server_hostname) 377 { 378 ipt->server_hostname = strdup(server_hostname); 379 } 380 381 void 382 iperf_set_test_template(struct iperf_test *ipt, char *tmp_template) 383 { 384 ipt->tmp_template = strdup(tmp_template); 385 } 386 387 void 388 iperf_set_test_reverse(struct iperf_test *ipt, int reverse) 389 { 390 ipt->reverse = reverse; 391 if (ipt->reverse) 392 ipt->sender = ! ipt->sender; 393 check_sender_has_retransmits(ipt); 394 } 395 396 void 397 iperf_set_test_json_output(struct iperf_test *ipt, int json_output) 398 { 399 ipt->json_output = json_output; 400 } 401 402 int 403 iperf_has_zerocopy( void ) 404 { 405 return has_sendfile(); 406 } 407 408 void 409 iperf_set_test_zerocopy(struct iperf_test *ipt, int zerocopy) 410 { 411 ipt->zerocopy = (zerocopy && has_sendfile()); 412 } 413 414 void 415 iperf_set_test_get_server_output(struct iperf_test *ipt, int get_server_output) 416 { 417 ipt->get_server_output = get_server_output; 418 } 419 420 void 421 iperf_set_test_unit_format(struct iperf_test *ipt, char unit_format) 422 { 423 ipt->settings->unit_format = unit_format; 424 } 425 426 void 427 iperf_set_test_bind_address(struct iperf_test *ipt, char *bind_address) 428 { 429 ipt->bind_address = strdup(bind_address); 430 } 431 432 void 433 iperf_set_test_udp_counters_64bit(struct iperf_test *ipt, int udp_counters_64bit) 434 { 435 ipt->udp_counters_64bit = udp_counters_64bit; 436 } 437 438 void 439 iperf_set_test_one_off(struct iperf_test *ipt, int one_off) 440 { 441 ipt->one_off = one_off; 442 } 443 444 /********************** Get/set test protocol structure ***********************/ 445 446 struct protocol * 447 get_protocol(struct iperf_test *test, int prot_id) 448 { 449 struct protocol *prot; 450 451 SLIST_FOREACH(prot, &test->protocols, protocols) { 452 if (prot->id == prot_id) 453 break; 454 } 455 456 if (prot == NULL) 457 i_errno = IEPROTOCOL; 458 459 return prot; 460 } 461 462 int 463 set_protocol(struct iperf_test *test, int prot_id) 464 { 465 struct protocol *prot = NULL; 466 467 SLIST_FOREACH(prot, &test->protocols, protocols) { 468 if (prot->id == prot_id) { 469 test->protocol = prot; 470 check_sender_has_retransmits(test); 471 return 0; 472 } 473 } 474 475 i_errno = IEPROTOCOL; 476 return -1; 477 } 478 479 480 /************************** Iperf callback functions **************************/ 481 482 void 483 iperf_on_new_stream(struct iperf_stream *sp) 484 { 485 connect_msg(sp); 486 } 487 488 void 489 iperf_on_test_start(struct iperf_test *test) 490 { 491 if (test->json_output) { 492 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", 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)); 493 } else { 494 if (test->verbose) { 495 if (test->settings->bytes) 496 iprintf(test, test_start_bytes, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->settings->bytes); 497 else if (test->settings->blocks) 498 iprintf(test, test_start_blocks, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->settings->blocks); 499 else 500 iprintf(test, test_start_time, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->duration); 501 } 502 } 503 } 504 505 /* This converts an IPv6 string address from IPv4-mapped format into regular 506 ** old IPv4 format, which is easier on the eyes of network veterans. 507 ** 508 ** If the v6 address is not v4-mapped it is left alone. 509 */ 510 static void 511 mapped_v4_to_regular_v4(char *str) 512 { 513 char *prefix = "::ffff:"; 514 int prefix_len; 515 516 prefix_len = strlen(prefix); 517 if (strncmp(str, prefix, prefix_len) == 0) { 518 int str_len = strlen(str); 519 memmove(str, str + prefix_len, str_len - prefix_len + 1); 520 } 521 } 522 523 void 524 iperf_on_connect(struct iperf_test *test) 525 { 526 time_t now_secs; 527 const char* rfc1123_fmt = "%a, %d %b %Y %H:%M:%S GMT"; 528 char now_str[100]; 529 char ipr[INET6_ADDRSTRLEN]; 530 int port; 531 struct sockaddr_storage sa; 532 struct sockaddr_in *sa_inP; 533 struct sockaddr_in6 *sa_in6P; 534 socklen_t len; 535 int opt; 536 537 now_secs = time((time_t*) 0); 538 (void) strftime(now_str, sizeof(now_str), rfc1123_fmt, gmtime(&now_secs)); 539 if (test->json_output) 540 cJSON_AddItemToObject(test->json_start, "timestamp", iperf_json_printf("time: %s timesecs: %d", now_str, (int64_t) now_secs)); 541 else if (test->verbose) 542 iprintf(test, report_time, now_str); 543 544 if (test->role == 'c') { 545 if (test->json_output) 546 cJSON_AddItemToObject(test->json_start, "connecting_to", iperf_json_printf("host: %s port: %d", test->server_hostname, (int64_t) test->server_port)); 547 else { 548 iprintf(test, report_connecting, test->server_hostname, test->server_port); 549 if (test->reverse) 550 iprintf(test, report_reverse, test->server_hostname); 551 } 552 } else { 553 len = sizeof(sa); 554 getpeername(test->ctrl_sck, (struct sockaddr *) &sa, &len); 555 if (getsockdomain(test->ctrl_sck) == AF_INET) { 556 sa_inP = (struct sockaddr_in *) &sa; 557 inet_ntop(AF_INET, &sa_inP->sin_addr, ipr, sizeof(ipr)); 558 port = ntohs(sa_inP->sin_port); 559 } else { 560 sa_in6P = (struct sockaddr_in6 *) &sa; 561 inet_ntop(AF_INET6, &sa_in6P->sin6_addr, ipr, sizeof(ipr)); 562 port = ntohs(sa_in6P->sin6_port); 563 } 564 mapped_v4_to_regular_v4(ipr); 565 if (test->json_output) 566 cJSON_AddItemToObject(test->json_start, "accepted_connection", iperf_json_printf("host: %s port: %d", ipr, (int64_t) port)); 567 else 568 iprintf(test, report_accepted, ipr, port); 569 } 570 if (test->json_output) { 571 cJSON_AddStringToObject(test->json_start, "cookie", test->cookie); 572 if (test->protocol->id == SOCK_STREAM) { 573 if (test->settings->mss) 574 cJSON_AddIntToObject(test->json_start, "tcp_mss", test->settings->mss); 575 else { 576 len = sizeof(opt); 577 getsockopt(test->ctrl_sck, IPPROTO_TCP, TCP_MAXSEG, &opt, &len); 578 cJSON_AddIntToObject(test->json_start, "tcp_mss_default", opt); 579 } 580 } 581 } else if (test->verbose) { 582 iprintf(test, report_cookie, test->cookie); 583 if (test->protocol->id == SOCK_STREAM) { 584 if (test->settings->mss) 585 iprintf(test, " TCP MSS: %d\n", test->settings->mss); 586 else { 587 len = sizeof(opt); 588 getsockopt(test->ctrl_sck, IPPROTO_TCP, TCP_MAXSEG, &opt, &len); 589 iprintf(test, " TCP MSS: %d (default)\n", opt); 590 } 591 } 592 593 } 594 } 595 596 void 597 iperf_on_test_finish(struct iperf_test *test) 598 { 599 } 600 601 602 /******************************************************************************/ 603 604 int 605 iperf_parse_arguments(struct iperf_test *test, int argc, char **argv) 606 { 607 static struct option longopts[] = 608 { 609 {"port", required_argument, NULL, 'p'}, 610 {"format", required_argument, NULL, 'f'}, 611 {"interval", required_argument, NULL, 'i'}, 612 {"daemon", no_argument, NULL, 'D'}, 613 {"one-off", no_argument, NULL, '1'}, 614 {"verbose", no_argument, NULL, 'V'}, 615 {"json", no_argument, NULL, 'J'}, 616 {"version", no_argument, NULL, 'v'}, 617 {"server", no_argument, NULL, 's'}, 618 {"client", required_argument, NULL, 'c'}, 619 {"udp", no_argument, NULL, 'u'}, 620 {"bandwidth", required_argument, NULL, 'b'}, 621 {"time", required_argument, NULL, 't'}, 622 {"bytes", required_argument, NULL, 'n'}, 623 {"blockcount", required_argument, NULL, 'k'}, 624 {"length", required_argument, NULL, 'l'}, 625 {"parallel", required_argument, NULL, 'P'}, 626 {"reverse", no_argument, NULL, 'R'}, 627 {"window", required_argument, NULL, 'w'}, 628 {"bind", required_argument, NULL, 'B'}, 629 {"cport", required_argument, NULL, OPT_CLIENT_PORT}, 630 {"set-mss", required_argument, NULL, 'M'}, 631 {"no-delay", no_argument, NULL, 'N'}, 632 {"version4", no_argument, NULL, '4'}, 633 {"version6", no_argument, NULL, '6'}, 634 {"tos", required_argument, NULL, 'S'}, 635 #if defined(HAVE_FLOWLABEL) 636 {"flowlabel", required_argument, NULL, 'L'}, 637 #endif /* HAVE_FLOWLABEL */ 638 {"zerocopy", no_argument, NULL, 'Z'}, 639 {"omit", required_argument, NULL, 'O'}, 640 {"file", required_argument, NULL, 'F'}, 641 #if defined(HAVE_CPU_AFFINITY) 642 {"affinity", required_argument, NULL, 'A'}, 643 #endif /* HAVE_CPU_AFFINITY */ 644 {"title", required_argument, NULL, 'T'}, 645 #if defined(HAVE_TCP_CONGESTION) 646 {"congestion", required_argument, NULL, 'C'}, 647 {"linux-congestion", required_argument, NULL, 'C'}, 648 #endif /* HAVE_TCP_CONGESTION */ 649 #if defined(HAVE_SCTP) 650 {"sctp", no_argument, NULL, OPT_SCTP}, 651 {"nstreams", required_argument, NULL, OPT_NUMSTREAMS}, 652 {"xbind", required_argument, NULL, 'X'}, 653 #endif 654 {"pidfile", required_argument, NULL, 'I'}, 655 {"logfile", required_argument, NULL, OPT_LOGFILE}, 656 {"get-server-output", no_argument, NULL, OPT_GET_SERVER_OUTPUT}, 657 {"udp-counters-64bit", no_argument, NULL, OPT_UDP_COUNTERS_64BIT}, 658 {"debug", no_argument, NULL, 'd'}, 659 {"help", no_argument, NULL, 'h'}, 660 {NULL, 0, NULL, 0} 661 }; 662 int flag; 663 int blksize; 664 int server_flag, client_flag, rate_flag, duration_flag; 665 char *endptr; 666 #if defined(HAVE_CPU_AFFINITY) 667 char* comma; 668 #endif /* HAVE_CPU_AFFINITY */ 669 char* slash; 670 struct xbind_entry *xbe; 671 672 blksize = 0; 673 server_flag = client_flag = rate_flag = duration_flag = 0; 674 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) { 675 switch (flag) { 676 case 'p': 677 test->server_port = atoi(optarg); 678 break; 679 case 'f': 680 test->settings->unit_format = *optarg; 681 break; 682 case 'i': 683 /* XXX: could potentially want separate stat collection and reporting intervals, 684 but just set them to be the same for now */ 685 test->stats_interval = test->reporter_interval = atof(optarg); 686 if ((test->stats_interval < MIN_INTERVAL || test->stats_interval > MAX_INTERVAL) && test->stats_interval != 0) { 687 i_errno = IEINTERVAL; 688 return -1; 689 } 690 break; 691 case 'D': 692 test->daemon = 1; 693 server_flag = 1; 694 break; 695 case '1': 696 test->one_off = 1; 697 server_flag = 1; 698 break; 699 case 'V': 700 test->verbose = 1; 701 break; 702 case 'J': 703 test->json_output = 1; 704 break; 705 case 'v': 706 printf("%s\n%s\n%s\n", version, get_system_info(), 707 get_optional_features()); 708 exit(0); 709 case 's': 710 if (test->role == 'c') { 711 i_errno = IESERVCLIENT; 712 return -1; 713 } 714 iperf_set_test_role(test, 's'); 715 break; 716 case 'c': 717 if (test->role == 's') { 718 i_errno = IESERVCLIENT; 719 return -1; 720 } 721 iperf_set_test_role(test, 'c'); 722 iperf_set_test_server_hostname(test, optarg); 723 break; 724 case 'u': 725 set_protocol(test, Pudp); 726 client_flag = 1; 727 break; 728 case OPT_SCTP: 729 #if defined(HAVE_SCTP) 730 set_protocol(test, Psctp); 731 client_flag = 1; 732 #else /* HAVE_SCTP */ 733 i_errno = IEUNIMP; 734 return -1; 735 #endif /* HAVE_SCTP */ 736 break; 737 738 case OPT_NUMSTREAMS: 739 #if defined(linux) || defined(__FreeBSD__) 740 test->settings->num_ostreams = unit_atoi(optarg); 741 client_flag = 1; 742 #else /* linux */ 743 i_errno = IEUNIMP; 744 return -1; 745 #endif /* linux */ 746 case 'b': 747 slash = strchr(optarg, '/'); 748 if (slash) { 749 *slash = '\0'; 750 ++slash; 751 test->settings->burst = atoi(slash); 752 if (test->settings->burst <= 0 || 753 test->settings->burst > MAX_BURST) { 754 i_errno = IEBURST; 755 return -1; 756 } 757 } 758 test->settings->rate = unit_atof_rate(optarg); 759 rate_flag = 1; 760 client_flag = 1; 761 break; 762 case 't': 763 test->duration = atoi(optarg); 764 if (test->duration > MAX_TIME) { 765 i_errno = IEDURATION; 766 return -1; 767 } 768 duration_flag = 1; 769 client_flag = 1; 770 break; 771 case 'n': 772 test->settings->bytes = unit_atoi(optarg); 773 client_flag = 1; 774 break; 775 case 'k': 776 test->settings->blocks = unit_atoi(optarg); 777 client_flag = 1; 778 break; 779 case 'l': 780 blksize = unit_atoi(optarg); 781 client_flag = 1; 782 break; 783 case 'P': 784 test->num_streams = atoi(optarg); 785 if (test->num_streams > MAX_STREAMS) { 786 i_errno = IENUMSTREAMS; 787 return -1; 788 } 789 client_flag = 1; 790 break; 791 case 'R': 792 iperf_set_test_reverse(test, 1); 793 client_flag = 1; 794 break; 795 case 'w': 796 // XXX: This is a socket buffer, not specific to TCP 797 test->settings->socket_bufsize = unit_atof(optarg); 798 if (test->settings->socket_bufsize > MAX_TCP_BUFFER) { 799 i_errno = IEBUFSIZE; 800 return -1; 801 } 802 client_flag = 1; 803 break; 804 case 'B': 805 test->bind_address = strdup(optarg); 806 break; 807 case OPT_CLIENT_PORT: 808 test->bind_port = atoi(optarg); 809 break; 810 case 'M': 811 test->settings->mss = atoi(optarg); 812 if (test->settings->mss > MAX_MSS) { 813 i_errno = IEMSS; 814 return -1; 815 } 816 client_flag = 1; 817 break; 818 case 'N': 819 test->no_delay = 1; 820 client_flag = 1; 821 break; 822 case '4': 823 test->settings->domain = AF_INET; 824 break; 825 case '6': 826 test->settings->domain = AF_INET6; 827 break; 828 case 'S': 829 test->settings->tos = strtol(optarg, &endptr, 0); 830 if (endptr == optarg || 831 test->settings->tos < 0 || 832 test->settings->tos > 255) { 833 i_errno = IEBADTOS; 834 return -1; 835 } 836 client_flag = 1; 837 break; 838 case 'L': 839 #if defined(HAVE_FLOWLABEL) 840 test->settings->flowlabel = strtol(optarg, &endptr, 0); 841 if (endptr == optarg || 842 test->settings->flowlabel < 1 || test->settings->flowlabel > 0xfffff) { 843 i_errno = IESETFLOW; 844 return -1; 845 } 846 client_flag = 1; 847 #else /* HAVE_FLOWLABEL */ 848 i_errno = IEUNIMP; 849 return -1; 850 #endif /* HAVE_FLOWLABEL */ 851 break; 852 case 'X': 853 xbe = (struct xbind_entry *)malloc(sizeof(struct xbind_entry)); 854 if (!xbe) { 855 i_errno = IESETSCTPBINDX; 856 return -1; 857 } 858 memset(xbe, 0, sizeof(*xbe)); 859 xbe->name = strdup(optarg); 860 if (!xbe->name) { 861 i_errno = IESETSCTPBINDX; 862 return -1; 863 } 864 TAILQ_INSERT_TAIL(&test->xbind_addrs, xbe, link); 865 break; 866 case 'Z': 867 if (!has_sendfile()) { 868 i_errno = IENOSENDFILE; 869 return -1; 870 } 871 test->zerocopy = 1; 872 client_flag = 1; 873 break; 874 case 'O': 875 test->omit = atoi(optarg); 876 if (test->omit < 0 || test->omit > 60) { 877 i_errno = IEOMIT; 878 return -1; 879 } 880 client_flag = 1; 881 break; 882 case 'F': 883 test->diskfile_name = optarg; 884 break; 885 case 'A': 886 #if defined(HAVE_CPU_AFFINITY) 887 test->affinity = strtol(optarg, &endptr, 0); 888 if (endptr == optarg || 889 test->affinity < 0 || test->affinity > 1024) { 890 i_errno = IEAFFINITY; 891 return -1; 892 } 893 comma = strchr(optarg, ','); 894 if (comma != NULL) { 895 test->server_affinity = atoi(comma+1); 896 if (test->server_affinity < 0 || test->server_affinity > 1024) { 897 i_errno = IEAFFINITY; 898 return -1; 899 } 900 client_flag = 1; 901 } 902 #else /* HAVE_CPU_AFFINITY */ 903 i_errno = IEUNIMP; 904 return -1; 905 #endif /* HAVE_CPU_AFFINITY */ 906 break; 907 case 'T': 908 test->title = strdup(optarg); 909 client_flag = 1; 910 break; 911 case 'C': 912 #if defined(HAVE_TCP_CONGESTION) 913 test->congestion = strdup(optarg); 914 client_flag = 1; 915 #else /* HAVE_TCP_CONGESTION */ 916 i_errno = IEUNIMP; 917 return -1; 918 #endif /* HAVE_TCP_CONGESTION */ 919 break; 920 case 'd': 921 test->debug = 1; 922 break; 923 case 'I': 924 test->pidfile = strdup(optarg); 925 server_flag = 1; 926 break; 927 case OPT_LOGFILE: 928 test->logfile = strdup(optarg); 929 break; 930 case OPT_GET_SERVER_OUTPUT: 931 test->get_server_output = 1; 932 client_flag = 1; 933 break; 934 case OPT_UDP_COUNTERS_64BIT: 935 test->udp_counters_64bit = 1; 936 break; 937 case 'h': 938 default: 939 usage_long(); 940 exit(1); 941 } 942 } 943 944 /* Set logging to a file if specified, otherwise use the default (stdout) */ 945 if (test->logfile) { 946 test->outfile = fopen(test->logfile, "a+"); 947 if (test->outfile == NULL) { 948 i_errno = IELOGFILE; 949 return -1; 950 } 951 } 952 953 /* Check flag / role compatibility. */ 954 if (test->role == 'c' && server_flag) { 955 i_errno = IESERVERONLY; 956 return -1; 957 } 958 if (test->role == 's' && client_flag) { 959 i_errno = IECLIENTONLY; 960 return -1; 961 } 962 963 if (!test->bind_address && test->bind_port) { 964 i_errno = IEBIND; 965 return -1; 966 } 967 if (blksize == 0) { 968 if (test->protocol->id == Pudp) 969 blksize = DEFAULT_UDP_BLKSIZE; 970 else if (test->protocol->id == Psctp) 971 blksize = DEFAULT_SCTP_BLKSIZE; 972 else 973 blksize = DEFAULT_TCP_BLKSIZE; 974 } 975 if (blksize <= 0 || blksize > MAX_BLOCKSIZE) { 976 i_errno = IEBLOCKSIZE; 977 return -1; 978 } 979 if (test->protocol->id == Pudp && 980 blksize > MAX_UDP_BLOCKSIZE) { 981 i_errno = IEUDPBLOCKSIZE; 982 return -1; 983 } 984 test->settings->blksize = blksize; 985 986 if (!rate_flag) 987 test->settings->rate = test->protocol->id == Pudp ? UDP_RATE : 0; 988 989 if ((test->settings->bytes != 0 || test->settings->blocks != 0) && ! duration_flag) 990 test->duration = 0; 991 992 /* Disallow specifying multiple test end conditions. The code actually 993 ** works just fine without this prohibition. As soon as any one of the 994 ** three possible end conditions is met, the test ends. So this check 995 ** could be removed if desired. 996 */ 997 if ((duration_flag && test->settings->bytes != 0) || 998 (duration_flag && test->settings->blocks != 0) || 999 (test->settings->bytes != 0 && test->settings->blocks != 0)) { 1000 i_errno = IEENDCONDITIONS; 1001 return -1; 1002 } 1003 1004 /* For subsequent calls to getopt */ 1005 #ifdef __APPLE__ 1006 optreset = 1; 1007 #endif 1008 optind = 0; 1009 1010 if ((test->role != 'c') && (test->role != 's')) { 1011 i_errno = IENOROLE; 1012 return -1; 1013 } 1014 1015 return 0; 1016 } 1017 1018 int 1019 iperf_set_send_state(struct iperf_test *test, signed char state) 1020 { 1021 test->state = state; 1022 if (Nwrite(test->ctrl_sck, (char*) &state, sizeof(state), Ptcp) < 0) { 1023 i_errno = IESENDMESSAGE; 1024 return -1; 1025 } 1026 return 0; 1027 } 1028 1029 void 1030 iperf_check_throttle(struct iperf_stream *sp, struct timeval *nowP) 1031 { 1032 double seconds; 1033 uint64_t bits_per_second; 1034 1035 if (sp->test->done) 1036 return; 1037 seconds = timeval_diff(&sp->result->start_time, nowP); 1038 bits_per_second = sp->result->bytes_sent * 8 / seconds; 1039 if (bits_per_second < sp->test->settings->rate) { 1040 sp->green_light = 1; 1041 FD_SET(sp->socket, &sp->test->write_set); 1042 } else { 1043 sp->green_light = 0; 1044 FD_CLR(sp->socket, &sp->test->write_set); 1045 } 1046 } 1047 1048 int 1049 iperf_send(struct iperf_test *test, fd_set *write_setP) 1050 { 1051 register int multisend, r, streams_active; 1052 register struct iperf_stream *sp; 1053 struct timeval now; 1054 1055 /* Can we do multisend mode? */ 1056 if (test->settings->burst != 0) 1057 multisend = test->settings->burst; 1058 else if (test->settings->rate == 0) 1059 multisend = test->multisend; 1060 else 1061 multisend = 1; /* nope */ 1062 1063 for (; multisend > 0; --multisend) { 1064 if (test->settings->rate != 0 && test->settings->burst == 0) 1065 gettimeofday(&now, NULL); 1066 streams_active = 0; 1067 SLIST_FOREACH(sp, &test->streams, streams) { 1068 if (sp->green_light && 1069 (write_setP == NULL || FD_ISSET(sp->socket, write_setP))) { 1070 if ((r = sp->snd(sp)) < 0) { 1071 if (r == NET_SOFTERROR) 1072 break; 1073 i_errno = IESTREAMWRITE; 1074 return r; 1075 } 1076 streams_active = 1; 1077 test->bytes_sent += r; 1078 ++test->blocks_sent; 1079 if (test->settings->rate != 0 && test->settings->burst == 0) 1080 iperf_check_throttle(sp, &now); 1081 if (multisend > 1 && test->settings->bytes != 0 && test->bytes_sent >= test->settings->bytes) 1082 break; 1083 if (multisend > 1 && test->settings->blocks != 0 && test->blocks_sent >= test->settings->blocks) 1084 break; 1085 } 1086 } 1087 if (!streams_active) 1088 break; 1089 } 1090 if (test->settings->burst != 0) { 1091 gettimeofday(&now, NULL); 1092 SLIST_FOREACH(sp, &test->streams, streams) 1093 iperf_check_throttle(sp, &now); 1094 } 1095 if (write_setP != NULL) 1096 SLIST_FOREACH(sp, &test->streams, streams) 1097 if (FD_ISSET(sp->socket, write_setP)) 1098 FD_CLR(sp->socket, write_setP); 1099 1100 return 0; 1101 } 1102 1103 int 1104 iperf_recv(struct iperf_test *test, fd_set *read_setP) 1105 { 1106 int r; 1107 struct iperf_stream *sp; 1108 1109 SLIST_FOREACH(sp, &test->streams, streams) { 1110 if (FD_ISSET(sp->socket, read_setP)) { 1111 if ((r = sp->rcv(sp)) < 0) { 1112 i_errno = IESTREAMREAD; 1113 return r; 1114 } 1115 test->bytes_sent += r; 1116 ++test->blocks_sent; 1117 FD_CLR(sp->socket, read_setP); 1118 } 1119 } 1120 1121 return 0; 1122 } 1123 1124 int 1125 iperf_init_test(struct iperf_test *test) 1126 { 1127 struct timeval now; 1128 struct iperf_stream *sp; 1129 1130 if (test->protocol->init) { 1131 if (test->protocol->init(test) < 0) 1132 return -1; 1133 } 1134 1135 /* Init each stream. */ 1136 if (gettimeofday(&now, NULL) < 0) { 1137 i_errno = IEINITTEST; 1138 return -1; 1139 } 1140 SLIST_FOREACH(sp, &test->streams, streams) { 1141 sp->result->start_time = now; 1142 } 1143 1144 if (test->on_test_start) 1145 test->on_test_start(test); 1146 1147 return 0; 1148 } 1149 1150 static void 1151 send_timer_proc(TimerClientData client_data, struct timeval *nowP) 1152 { 1153 struct iperf_stream *sp = client_data.p; 1154 1155 /* All we do here is set or clear the flag saying that this stream may 1156 ** be sent to. The actual sending gets done in the send proc, after 1157 ** checking the flag. 1158 */ 1159 iperf_check_throttle(sp, nowP); 1160 } 1161 1162 int 1163 iperf_create_send_timers(struct iperf_test * test) 1164 { 1165 struct timeval now; 1166 struct iperf_stream *sp; 1167 TimerClientData cd; 1168 1169 if (gettimeofday(&now, NULL) < 0) { 1170 i_errno = IEINITTEST; 1171 return -1; 1172 } 1173 SLIST_FOREACH(sp, &test->streams, streams) { 1174 sp->green_light = 1; 1175 if (test->settings->rate != 0) { 1176 cd.p = sp; 1177 sp->send_timer = tmr_create((struct timeval*) 0, send_timer_proc, cd, 100000L, 1); 1178 /* (Repeat every tenth second - arbitrary often value.) */ 1179 if (sp->send_timer == NULL) { 1180 i_errno = IEINITTEST; 1181 return -1; 1182 } 1183 } 1184 } 1185 return 0; 1186 } 1187 1188 /** 1189 * iperf_exchange_parameters - handles the param_Exchange part for client 1190 * 1191 */ 1192 1193 int 1194 iperf_exchange_parameters(struct iperf_test *test) 1195 { 1196 int s; 1197 int32_t err; 1198 1199 if (test->role == 'c') { 1200 1201 if (send_parameters(test) < 0) 1202 return -1; 1203 1204 } else { 1205 1206 if (get_parameters(test) < 0) 1207 return -1; 1208 1209 if ((s = test->protocol->listen(test)) < 0) { 1210 if (iperf_set_send_state(test, SERVER_ERROR) != 0) 1211 return -1; 1212 err = htonl(i_errno); 1213 if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) { 1214 i_errno = IECTRLWRITE; 1215 return -1; 1216 } 1217 err = htonl(errno); 1218 if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) { 1219 i_errno = IECTRLWRITE; 1220 return -1; 1221 } 1222 return -1; 1223 } 1224 FD_SET(s, &test->read_set); 1225 test->max_fd = (s > test->max_fd) ? s : test->max_fd; 1226 test->prot_listener = s; 1227 1228 // Send the control message to create streams and start the test 1229 if (iperf_set_send_state(test, CREATE_STREAMS) != 0) 1230 return -1; 1231 1232 } 1233 1234 return 0; 1235 } 1236 1237 /*************************************************************/ 1238 1239 int 1240 iperf_exchange_results(struct iperf_test *test) 1241 { 1242 if (test->role == 'c') { 1243 /* Send results to server. */ 1244 if (send_results(test) < 0) 1245 return -1; 1246 /* Get server results. */ 1247 if (get_results(test) < 0) 1248 return -1; 1249 } else { 1250 /* Get client results. */ 1251 if (get_results(test) < 0) 1252 return -1; 1253 /* Send results to client. */ 1254 if (send_results(test) < 0) 1255 return -1; 1256 } 1257 return 0; 1258 } 1259 1260 /*************************************************************/ 1261 1262 static int 1263 send_parameters(struct iperf_test *test) 1264 { 1265 int r = 0; 1266 cJSON *j; 1267 1268 j = cJSON_CreateObject(); 1269 if (j == NULL) { 1270 i_errno = IESENDPARAMS; 1271 r = -1; 1272 } else { 1273 if (test->protocol->id == Ptcp) 1274 cJSON_AddTrueToObject(j, "tcp"); 1275 else if (test->protocol->id == Pudp) 1276 cJSON_AddTrueToObject(j, "udp"); 1277 else if (test->protocol->id == Psctp) 1278 cJSON_AddTrueToObject(j, "sctp"); 1279 cJSON_AddIntToObject(j, "omit", test->omit); 1280 if (test->server_affinity != -1) 1281 cJSON_AddIntToObject(j, "server_affinity", test->server_affinity); 1282 if (test->duration) 1283 cJSON_AddIntToObject(j, "time", test->duration); 1284 if (test->settings->bytes) 1285 cJSON_AddIntToObject(j, "num", test->settings->bytes); 1286 if (test->settings->blocks) 1287 cJSON_AddIntToObject(j, "blockcount", test->settings->blocks); 1288 if (test->settings->mss) 1289 cJSON_AddIntToObject(j, "MSS", test->settings->mss); 1290 if (test->no_delay) 1291 cJSON_AddTrueToObject(j, "nodelay"); 1292 cJSON_AddIntToObject(j, "parallel", test->num_streams); 1293 if (test->reverse) 1294 cJSON_AddTrueToObject(j, "reverse"); 1295 if (test->settings->socket_bufsize) 1296 cJSON_AddIntToObject(j, "window", test->settings->socket_bufsize); 1297 if (test->settings->blksize) 1298 cJSON_AddIntToObject(j, "len", test->settings->blksize); 1299 if (test->settings->rate) 1300 cJSON_AddIntToObject(j, "bandwidth", test->settings->rate); 1301 if (test->settings->burst) 1302 cJSON_AddIntToObject(j, "burst", test->settings->burst); 1303 if (test->settings->tos) 1304 cJSON_AddIntToObject(j, "TOS", test->settings->tos); 1305 if (test->settings->flowlabel) 1306 cJSON_AddIntToObject(j, "flowlabel", test->settings->flowlabel); 1307 if (test->title) 1308 cJSON_AddStringToObject(j, "title", test->title); 1309 if (test->congestion) 1310 cJSON_AddStringToObject(j, "congestion", test->congestion); 1311 if (test->get_server_output) 1312 cJSON_AddIntToObject(j, "get_server_output", iperf_get_test_get_server_output(test)); 1313 if (test->udp_counters_64bit) 1314 cJSON_AddIntToObject(j, "udp_counters_64bit", iperf_get_test_udp_counters_64bit(test)); 1315 1316 cJSON_AddStringToObject(j, "client_version", IPERF_VERSION); 1317 1318 if (test->debug) { 1319 printf("send_parameters:\n%s\n", cJSON_Print(j)); 1320 } 1321 1322 if (JSON_write(test->ctrl_sck, j) < 0) { 1323 i_errno = IESENDPARAMS; 1324 r = -1; 1325 } 1326 cJSON_Delete(j); 1327 } 1328 return r; 1329 } 1330 1331 /*************************************************************/ 1332 1333 static int 1334 get_parameters(struct iperf_test *test) 1335 { 1336 int r = 0; 1337 cJSON *j; 1338 cJSON *j_p; 1339 1340 j = JSON_read(test->ctrl_sck); 1341 if (j == NULL) { 1342 i_errno = IERECVPARAMS; 1343 r = -1; 1344 } else { 1345 if (test->debug) { 1346 printf("get_parameters:\n%s\n", cJSON_Print(j)); 1347 } 1348 1349 if ((j_p = cJSON_GetObjectItem(j, "tcp")) != NULL) 1350 set_protocol(test, Ptcp); 1351 if ((j_p = cJSON_GetObjectItem(j, "udp")) != NULL) 1352 set_protocol(test, Pudp); 1353 if ((j_p = cJSON_GetObjectItem(j, "sctp")) != NULL) 1354 set_protocol(test, Psctp); 1355 if ((j_p = cJSON_GetObjectItem(j, "omit")) != NULL) 1356 test->omit = j_p->valueint; 1357 if ((j_p = cJSON_GetObjectItem(j, "server_affinity")) != NULL) 1358 test->server_affinity = j_p->valueint; 1359 if ((j_p = cJSON_GetObjectItem(j, "time")) != NULL) 1360 test->duration = j_p->valueint; 1361 if ((j_p = cJSON_GetObjectItem(j, "num")) != NULL) 1362 test->settings->bytes = j_p->valueint; 1363 if ((j_p = cJSON_GetObjectItem(j, "blockcount")) != NULL) 1364 test->settings->blocks = j_p->valueint; 1365 if ((j_p = cJSON_GetObjectItem(j, "MSS")) != NULL) 1366 test->settings->mss = j_p->valueint; 1367 if ((j_p = cJSON_GetObjectItem(j, "nodelay")) != NULL) 1368 test->no_delay = 1; 1369 if ((j_p = cJSON_GetObjectItem(j, "parallel")) != NULL) 1370 test->num_streams = j_p->valueint; 1371 if ((j_p = cJSON_GetObjectItem(j, "reverse")) != NULL) 1372 iperf_set_test_reverse(test, 1); 1373 if ((j_p = cJSON_GetObjectItem(j, "window")) != NULL) 1374 test->settings->socket_bufsize = j_p->valueint; 1375 if ((j_p = cJSON_GetObjectItem(j, "len")) != NULL) 1376 test->settings->blksize = j_p->valueint; 1377 if ((j_p = cJSON_GetObjectItem(j, "bandwidth")) != NULL) 1378 test->settings->rate = j_p->valueint; 1379 if ((j_p = cJSON_GetObjectItem(j, "burst")) != NULL) 1380 test->settings->burst = j_p->valueint; 1381 if ((j_p = cJSON_GetObjectItem(j, "TOS")) != NULL) 1382 test->settings->tos = j_p->valueint; 1383 if ((j_p = cJSON_GetObjectItem(j, "flowlabel")) != NULL) 1384 test->settings->flowlabel = j_p->valueint; 1385 if ((j_p = cJSON_GetObjectItem(j, "title")) != NULL) 1386 test->title = strdup(j_p->valuestring); 1387 if ((j_p = cJSON_GetObjectItem(j, "congestion")) != NULL) 1388 test->congestion = strdup(j_p->valuestring); 1389 if ((j_p = cJSON_GetObjectItem(j, "get_server_output")) != NULL) 1390 iperf_set_test_get_server_output(test, 1); 1391 if ((j_p = cJSON_GetObjectItem(j, "udp_counters_64bit")) != NULL) 1392 iperf_set_test_udp_counters_64bit(test, 1); 1393 if (test->sender && test->protocol->id == Ptcp && has_tcpinfo_retransmits()) 1394 test->sender_has_retransmits = 1; 1395 cJSON_Delete(j); 1396 } 1397 return r; 1398 } 1399 1400 /*************************************************************/ 1401 1402 static int 1403 send_results(struct iperf_test *test) 1404 { 1405 int r = 0; 1406 cJSON *j; 1407 cJSON *j_streams; 1408 struct iperf_stream *sp; 1409 cJSON *j_stream; 1410 int sender_has_retransmits; 1411 iperf_size_t bytes_transferred; 1412 int retransmits; 1413 1414 j = cJSON_CreateObject(); 1415 if (j == NULL) { 1416 i_errno = IEPACKAGERESULTS; 1417 r = -1; 1418 } else { 1419 cJSON_AddFloatToObject(j, "cpu_util_total", test->cpu_util[0]); 1420 cJSON_AddFloatToObject(j, "cpu_util_user", test->cpu_util[1]); 1421 cJSON_AddFloatToObject(j, "cpu_util_system", test->cpu_util[2]); 1422 if ( ! test->sender ) 1423 sender_has_retransmits = -1; 1424 else 1425 sender_has_retransmits = test->sender_has_retransmits; 1426 cJSON_AddIntToObject(j, "sender_has_retransmits", sender_has_retransmits); 1427 1428 /* If on the server and sending server output, then do this */ 1429 if (test->role == 's' && test->get_server_output) { 1430 if (test->json_output) { 1431 /* Add JSON output */ 1432 cJSON_AddItemReferenceToObject(j, "server_output_json", test->json_top); 1433 } 1434 else { 1435 /* Add textual output */ 1436 size_t buflen = 0; 1437 1438 /* Figure out how much room we need to hold the complete output string */ 1439 struct iperf_textline *t; 1440 TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) { 1441 buflen += strlen(t->line); 1442 } 1443 1444 /* Allocate and build it up from the component lines */ 1445 char *output = calloc(buflen + 1, 1); 1446 TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) { 1447 strncat(output, t->line, buflen); 1448 buflen -= strlen(t->line); 1449 } 1450 1451 cJSON_AddStringToObject(j, "server_output_text", output); 1452 } 1453 } 1454 1455 j_streams = cJSON_CreateArray(); 1456 if (j_streams == NULL) { 1457 i_errno = IEPACKAGERESULTS; 1458 r = -1; 1459 } else { 1460 cJSON_AddItemToObject(j, "streams", j_streams); 1461 SLIST_FOREACH(sp, &test->streams, streams) { 1462 j_stream = cJSON_CreateObject(); 1463 if (j_stream == NULL) { 1464 i_errno = IEPACKAGERESULTS; 1465 r = -1; 1466 } else { 1467 cJSON_AddItemToArray(j_streams, j_stream); 1468 bytes_transferred = test->sender ? sp->result->bytes_sent : sp->result->bytes_received; 1469 retransmits = (test->sender && test->sender_has_retransmits) ? sp->result->stream_retrans : -1; 1470 cJSON_AddIntToObject(j_stream, "id", sp->id); 1471 cJSON_AddIntToObject(j_stream, "bytes", bytes_transferred); 1472 cJSON_AddIntToObject(j_stream, "retransmits", retransmits); 1473 cJSON_AddFloatToObject(j_stream, "jitter", sp->jitter); 1474 cJSON_AddIntToObject(j_stream, "errors", sp->cnt_error); 1475 cJSON_AddIntToObject(j_stream, "packets", sp->packet_count); 1476 } 1477 } 1478 if (r == 0 && test->debug) { 1479 printf("send_results\n%s\n", cJSON_Print(j)); 1480 } 1481 if (r == 0 && JSON_write(test->ctrl_sck, j) < 0) { 1482 i_errno = IESENDRESULTS; 1483 r = -1; 1484 } 1485 } 1486 cJSON_Delete(j); 1487 } 1488 return r; 1489 } 1490 1491 /*************************************************************/ 1492 1493 static int 1494 get_results(struct iperf_test *test) 1495 { 1496 int r = 0; 1497 cJSON *j; 1498 cJSON *j_cpu_util_total; 1499 cJSON *j_cpu_util_user; 1500 cJSON *j_cpu_util_system; 1501 cJSON *j_sender_has_retransmits; 1502 int result_has_retransmits; 1503 cJSON *j_streams; 1504 int n, i; 1505 cJSON *j_stream; 1506 cJSON *j_id; 1507 cJSON *j_bytes; 1508 cJSON *j_retransmits; 1509 cJSON *j_jitter; 1510 cJSON *j_errors; 1511 cJSON *j_packets; 1512 cJSON *j_server_output; 1513 int sid, cerror, pcount; 1514 double jitter; 1515 iperf_size_t bytes_transferred; 1516 int retransmits; 1517 struct iperf_stream *sp; 1518 1519 j = JSON_read(test->ctrl_sck); 1520 if (j == NULL) { 1521 i_errno = IERECVRESULTS; 1522 r = -1; 1523 } else { 1524 j_cpu_util_total = cJSON_GetObjectItem(j, "cpu_util_total"); 1525 j_cpu_util_user = cJSON_GetObjectItem(j, "cpu_util_user"); 1526 j_cpu_util_system = cJSON_GetObjectItem(j, "cpu_util_system"); 1527 j_sender_has_retransmits = cJSON_GetObjectItem(j, "sender_has_retransmits"); 1528 if (j_cpu_util_total == NULL || j_cpu_util_user == NULL || j_cpu_util_system == NULL || j_sender_has_retransmits == NULL) { 1529 i_errno = IERECVRESULTS; 1530 r = -1; 1531 } else { 1532 if (test->debug) { 1533 printf("get_results\n%s\n", cJSON_Print(j)); 1534 } 1535 1536 test->remote_cpu_util[0] = j_cpu_util_total->valuefloat; 1537 test->remote_cpu_util[1] = j_cpu_util_user->valuefloat; 1538 test->remote_cpu_util[2] = j_cpu_util_system->valuefloat; 1539 result_has_retransmits = j_sender_has_retransmits->valueint; 1540 if (! test->sender) 1541 test->sender_has_retransmits = result_has_retransmits; 1542 j_streams = cJSON_GetObjectItem(j, "streams"); 1543 if (j_streams == NULL) { 1544 i_errno = IERECVRESULTS; 1545 r = -1; 1546 } else { 1547 n = cJSON_GetArraySize(j_streams); 1548 for (i=0; i<n; ++i) { 1549 j_stream = cJSON_GetArrayItem(j_streams, i); 1550 if (j_stream == NULL) { 1551 i_errno = IERECVRESULTS; 1552 r = -1; 1553 } else { 1554 j_id = cJSON_GetObjectItem(j_stream, "id"); 1555 j_bytes = cJSON_GetObjectItem(j_stream, "bytes"); 1556 j_retransmits = cJSON_GetObjectItem(j_stream, "retransmits"); 1557 j_jitter = cJSON_GetObjectItem(j_stream, "jitter"); 1558 j_errors = cJSON_GetObjectItem(j_stream, "errors"); 1559 j_packets = cJSON_GetObjectItem(j_stream, "packets"); 1560 if (j_id == NULL || j_bytes == NULL || j_retransmits == NULL || j_jitter == NULL || j_errors == NULL || j_packets == NULL) { 1561 i_errno = IERECVRESULTS; 1562 r = -1; 1563 } else { 1564 sid = j_id->valueint; 1565 bytes_transferred = j_bytes->valueint; 1566 retransmits = j_retransmits->valueint; 1567 jitter = j_jitter->valuefloat; 1568 cerror = j_errors->valueint; 1569 pcount = j_packets->valueint; 1570 SLIST_FOREACH(sp, &test->streams, streams) 1571 if (sp->id == sid) break; 1572 if (sp == NULL) { 1573 i_errno = IESTREAMID; 1574 r = -1; 1575 } else { 1576 if (test->sender) { 1577 sp->jitter = jitter; 1578 sp->cnt_error = cerror; 1579 sp->packet_count = pcount; 1580 sp->result->bytes_received = bytes_transferred; 1581 } else { 1582 sp->result->bytes_sent = bytes_transferred; 1583 sp->result->stream_retrans = retransmits; 1584 } 1585 } 1586 } 1587 } 1588 } 1589 /* 1590 * If we're the client and we're supposed to get remote results, 1591 * look them up and process accordingly. 1592 */ 1593 if (test->role == 'c' && iperf_get_test_get_server_output(test)) { 1594 /* Look for JSON. If we find it, grab the object so it doesn't get deleted. */ 1595 j_server_output = cJSON_DetachItemFromObject(j, "server_output_json"); 1596 if (j_server_output != NULL) { 1597 test->json_server_output = j_server_output; 1598 } 1599 else { 1600 /* No JSON, look for textual output. Make a copy of the text for later. */ 1601 j_server_output = cJSON_GetObjectItem(j, "server_output_text"); 1602 if (j_server_output != NULL) { 1603 test->server_output_text = strdup(j_server_output->valuestring); 1604 } 1605 } 1606 } 1607 } 1608 } 1609 cJSON_Delete(j); 1610 } 1611 return r; 1612 } 1613 1614 /*************************************************************/ 1615 1616 static int 1617 JSON_write(int fd, cJSON *json) 1618 { 1619 uint32_t hsize, nsize; 1620 char *str; 1621 int r = 0; 1622 1623 str = cJSON_PrintUnformatted(json); 1624 if (str == NULL) 1625 r = -1; 1626 else { 1627 hsize = strlen(str); 1628 nsize = htonl(hsize); 1629 if (Nwrite(fd, (char*) &nsize, sizeof(nsize), Ptcp) < 0) 1630 r = -1; 1631 else { 1632 if (Nwrite(fd, str, hsize, Ptcp) < 0) 1633 r = -1; 1634 } 1635 free(str); 1636 } 1637 return r; 1638 } 1639 1640 /*************************************************************/ 1641 1642 static cJSON * 1643 JSON_read(int fd) 1644 { 1645 uint32_t hsize, nsize; 1646 char *str; 1647 cJSON *json = NULL; 1648 1649 if (Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp) >= 0) { 1650 hsize = ntohl(nsize); 1651 str = (char *) malloc(hsize+1); /* +1 for EOS */ 1652 if (str != NULL) { 1653 if (Nread(fd, str, hsize, Ptcp) >= 0) { 1654 str[hsize] = '\0'; /* add the EOS */ 1655 json = cJSON_Parse(str); 1656 } 1657 } 1658 free(str); 1659 } 1660 return json; 1661 } 1662 1663 /*************************************************************/ 1664 /** 1665 * add_to_interval_list -- adds new interval to the interval_list 1666 */ 1667 1668 void 1669 add_to_interval_list(struct iperf_stream_result * rp, struct iperf_interval_results * new) 1670 { 1671 struct iperf_interval_results *irp; 1672 1673 irp = (struct iperf_interval_results *) malloc(sizeof(struct iperf_interval_results)); 1674 memcpy(irp, new, sizeof(struct iperf_interval_results)); 1675 TAILQ_INSERT_TAIL(&rp->interval_results, irp, irlistentries); 1676 } 1677 1678 1679 /************************************************************/ 1680 1681 /** 1682 * connect_msg -- displays connection message 1683 * denoting sender/receiver details 1684 * 1685 */ 1686 1687 void 1688 connect_msg(struct iperf_stream *sp) 1689 { 1690 char ipl[INET6_ADDRSTRLEN], ipr[INET6_ADDRSTRLEN]; 1691 int lport, rport; 1692 1693 if (getsockdomain(sp->socket) == AF_INET) { 1694 inet_ntop(AF_INET, (void *) &((struct sockaddr_in *) &sp->local_addr)->sin_addr, ipl, sizeof(ipl)); 1695 mapped_v4_to_regular_v4(ipl); 1696 inet_ntop(AF_INET, (void *) &((struct sockaddr_in *) &sp->remote_addr)->sin_addr, ipr, sizeof(ipr)); 1697 mapped_v4_to_regular_v4(ipr); 1698 lport = ntohs(((struct sockaddr_in *) &sp->local_addr)->sin_port); 1699 rport = ntohs(((struct sockaddr_in *) &sp->remote_addr)->sin_port); 1700 } else { 1701 inet_ntop(AF_INET6, (void *) &((struct sockaddr_in6 *) &sp->local_addr)->sin6_addr, ipl, sizeof(ipl)); 1702 mapped_v4_to_regular_v4(ipl); 1703 inet_ntop(AF_INET6, (void *) &((struct sockaddr_in6 *) &sp->remote_addr)->sin6_addr, ipr, sizeof(ipr)); 1704 mapped_v4_to_regular_v4(ipr); 1705 lport = ntohs(((struct sockaddr_in6 *) &sp->local_addr)->sin6_port); 1706 rport = ntohs(((struct sockaddr_in6 *) &sp->remote_addr)->sin6_port); 1707 } 1708 1709 if (sp->test->json_output) 1710 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)); 1711 else 1712 iprintf(sp->test, report_connected, sp->socket, ipl, lport, ipr, rport); 1713 } 1714 1715 1716 /**************************************************************************/ 1717 1718 struct iperf_test * 1719 iperf_new_test() 1720 { 1721 struct iperf_test *test; 1722 1723 test = (struct iperf_test *) malloc(sizeof(struct iperf_test)); 1724 if (!test) { 1725 i_errno = IENEWTEST; 1726 return NULL; 1727 } 1728 /* initialize everything to zero */ 1729 memset(test, 0, sizeof(struct iperf_test)); 1730 1731 test->settings = (struct iperf_settings *) malloc(sizeof(struct iperf_settings)); 1732 if (!test->settings) { 1733 free(test); 1734 i_errno = IENEWTEST; 1735 return NULL; 1736 } 1737 memset(test->settings, 0, sizeof(struct iperf_settings)); 1738 1739 /* By default all output goes to stdout */ 1740 test->outfile = stdout; 1741 1742 return test; 1743 } 1744 1745 /**************************************************************************/ 1746 1747 struct protocol * 1748 protocol_new(void) 1749 { 1750 struct protocol *proto; 1751 1752 proto = malloc(sizeof(struct protocol)); 1753 if(!proto) { 1754 return NULL; 1755 } 1756 memset(proto, 0, sizeof(struct protocol)); 1757 1758 return proto; 1759 } 1760 1761 void 1762 protocol_free(struct protocol *proto) 1763 { 1764 free(proto); 1765 } 1766 1767 /**************************************************************************/ 1768 int 1769 iperf_defaults(struct iperf_test *testp) 1770 { 1771 struct protocol *tcp, *udp; 1772 #if defined(HAVE_SCTP) 1773 struct protocol *sctp; 1774 #endif /* HAVE_SCTP */ 1775 1776 testp->omit = OMIT; 1777 testp->duration = DURATION; 1778 testp->diskfile_name = (char*) 0; 1779 testp->affinity = -1; 1780 testp->server_affinity = -1; 1781 TAILQ_INIT(&testp->xbind_addrs); 1782 #if defined(HAVE_CPUSET_SETAFFINITY) 1783 CPU_ZERO(&testp->cpumask); 1784 #endif /* HAVE_CPUSET_SETAFFINITY */ 1785 testp->title = NULL; 1786 testp->congestion = NULL; 1787 testp->server_port = PORT; 1788 testp->ctrl_sck = -1; 1789 testp->prot_listener = -1; 1790 1791 testp->stats_callback = iperf_stats_callback; 1792 testp->reporter_callback = iperf_reporter_callback; 1793 1794 testp->stats_interval = testp->reporter_interval = 1; 1795 testp->num_streams = 1; 1796 1797 testp->settings->domain = AF_UNSPEC; 1798 testp->settings->unit_format = 'a'; 1799 testp->settings->socket_bufsize = 0; /* use autotuning */ 1800 testp->settings->blksize = DEFAULT_TCP_BLKSIZE; 1801 testp->settings->rate = 0; 1802 testp->settings->burst = 0; 1803 testp->settings->mss = 0; 1804 testp->settings->bytes = 0; 1805 testp->settings->blocks = 0; 1806 memset(testp->cookie, 0, COOKIE_SIZE); 1807 1808 testp->multisend = 10; /* arbitrary */ 1809 1810 /* Set up protocol list */ 1811 SLIST_INIT(&testp->streams); 1812 SLIST_INIT(&testp->protocols); 1813 1814 tcp = protocol_new(); 1815 if (!tcp) 1816 return -1; 1817 1818 tcp->id = Ptcp; 1819 tcp->name = "TCP"; 1820 tcp->accept = iperf_tcp_accept; 1821 tcp->listen = iperf_tcp_listen; 1822 tcp->connect = iperf_tcp_connect; 1823 tcp->send = iperf_tcp_send; 1824 tcp->recv = iperf_tcp_recv; 1825 tcp->init = NULL; 1826 SLIST_INSERT_HEAD(&testp->protocols, tcp, protocols); 1827 1828 udp = protocol_new(); 1829 if (!udp) { 1830 protocol_free(tcp); 1831 return -1; 1832 } 1833 1834 udp->id = Pudp; 1835 udp->name = "UDP"; 1836 udp->accept = iperf_udp_accept; 1837 udp->listen = iperf_udp_listen; 1838 udp->connect = iperf_udp_connect; 1839 udp->send = iperf_udp_send; 1840 udp->recv = iperf_udp_recv; 1841 udp->init = iperf_udp_init; 1842 SLIST_INSERT_AFTER(tcp, udp, protocols); 1843 1844 set_protocol(testp, Ptcp); 1845 1846 #if defined(HAVE_SCTP) 1847 sctp = protocol_new(); 1848 if (!sctp) { 1849 protocol_free(tcp); 1850 protocol_free(udp); 1851 return -1; 1852 } 1853 1854 sctp->id = Psctp; 1855 sctp->name = "SCTP"; 1856 sctp->accept = iperf_sctp_accept; 1857 sctp->listen = iperf_sctp_listen; 1858 sctp->connect = iperf_sctp_connect; 1859 sctp->send = iperf_sctp_send; 1860 sctp->recv = iperf_sctp_recv; 1861 sctp->init = iperf_sctp_init; 1862 1863 SLIST_INSERT_AFTER(udp, sctp, protocols); 1864 #endif /* HAVE_SCTP */ 1865 1866 testp->on_new_stream = iperf_on_new_stream; 1867 testp->on_test_start = iperf_on_test_start; 1868 testp->on_connect = iperf_on_connect; 1869 testp->on_test_finish = iperf_on_test_finish; 1870 1871 TAILQ_INIT(&testp->server_output_list); 1872 1873 return 0; 1874 } 1875 1876 1877 /**************************************************************************/ 1878 void 1879 iperf_free_test(struct iperf_test *test) 1880 { 1881 struct protocol *prot; 1882 struct iperf_stream *sp; 1883 1884 /* Free streams */ 1885 while (!SLIST_EMPTY(&test->streams)) { 1886 sp = SLIST_FIRST(&test->streams); 1887 SLIST_REMOVE_HEAD(&test->streams, streams); 1888 iperf_free_stream(sp); 1889 } 1890 1891 if (test->server_hostname) 1892 free(test->server_hostname); 1893 if (test->tmp_template) 1894 free(test->tmp_template); 1895 if (test->bind_address) 1896 free(test->bind_address); 1897 if (!TAILQ_EMPTY(&test->xbind_addrs)) { 1898 struct xbind_entry *xbe; 1899 1900 while (!TAILQ_EMPTY(&test->xbind_addrs)) { 1901 xbe = TAILQ_FIRST(&test->xbind_addrs); 1902 TAILQ_REMOVE(&test->xbind_addrs, xbe, link); 1903 if (xbe->ai) 1904 freeaddrinfo(xbe->ai); 1905 free(xbe->name); 1906 free(xbe); 1907 } 1908 } 1909 if (test->settings) 1910 free(test->settings); 1911 if (test->title) 1912 free(test->title); 1913 if (test->congestion) 1914 free(test->congestion); 1915 if (test->omit_timer != NULL) 1916 tmr_cancel(test->omit_timer); 1917 if (test->timer != NULL) 1918 tmr_cancel(test->timer); 1919 if (test->stats_timer != NULL) 1920 tmr_cancel(test->stats_timer); 1921 if (test->reporter_timer != NULL) 1922 tmr_cancel(test->reporter_timer); 1923 1924 /* Free protocol list */ 1925 while (!SLIST_EMPTY(&test->protocols)) { 1926 prot = SLIST_FIRST(&test->protocols); 1927 SLIST_REMOVE_HEAD(&test->protocols, protocols); 1928 free(prot); 1929 } 1930 1931 if (test->server_output_text) { 1932 free(test->server_output_text); 1933 test->server_output_text = NULL; 1934 } 1935 1936 if (test->json_output_string) { 1937 free(test->json_output_string); 1938 test->json_output_string = NULL; 1939 } 1940 1941 /* Free output line buffers, if any (on the server only) */ 1942 struct iperf_textline *t; 1943 while (!TAILQ_EMPTY(&test->server_output_list)) { 1944 t = TAILQ_FIRST(&test->server_output_list); 1945 TAILQ_REMOVE(&test->server_output_list, t, textlineentries); 1946 free(t->line); 1947 free(t); 1948 } 1949 1950 /* sctp_bindx: do not free the arguments, only the resolver results */ 1951 if (!TAILQ_EMPTY(&test->xbind_addrs)) { 1952 struct xbind_entry *xbe; 1953 1954 TAILQ_FOREACH(xbe, &test->xbind_addrs, link) { 1955 if (xbe->ai) { 1956 freeaddrinfo(xbe->ai); 1957 xbe->ai = NULL; 1958 } 1959 } 1960 } 1961 1962 /* XXX: Why are we setting these values to NULL? */ 1963 // test->streams = NULL; 1964 test->stats_callback = NULL; 1965 test->reporter_callback = NULL; 1966 free(test); 1967 } 1968 1969 1970 void 1971 iperf_reset_test(struct iperf_test *test) 1972 { 1973 struct iperf_stream *sp; 1974 1975 /* Free streams */ 1976 while (!SLIST_EMPTY(&test->streams)) { 1977 sp = SLIST_FIRST(&test->streams); 1978 SLIST_REMOVE_HEAD(&test->streams, streams); 1979 iperf_free_stream(sp); 1980 } 1981 if (test->omit_timer != NULL) { 1982 tmr_cancel(test->omit_timer); 1983 test->omit_timer = NULL; 1984 } 1985 if (test->timer != NULL) { 1986 tmr_cancel(test->timer); 1987 test->timer = NULL; 1988 } 1989 if (test->stats_timer != NULL) { 1990 tmr_cancel(test->stats_timer); 1991 test->stats_timer = NULL; 1992 } 1993 if (test->reporter_timer != NULL) { 1994 tmr_cancel(test->reporter_timer); 1995 test->reporter_timer = NULL; 1996 } 1997 test->done = 0; 1998 1999 SLIST_INIT(&test->streams); 2000 2001 test->role = 's'; 2002 test->sender = 0; 2003 test->sender_has_retransmits = 0; 2004 set_protocol(test, Ptcp); 2005 test->omit = OMIT; 2006 test->duration = DURATION; 2007 test->server_affinity = -1; 2008 #if defined(HAVE_CPUSET_SETAFFINITY) 2009 CPU_ZERO(&test->cpumask); 2010 #endif /* HAVE_CPUSET_SETAFFINITY */ 2011 test->state = 0; 2012 2013 test->ctrl_sck = -1; 2014 test->prot_listener = -1; 2015 2016 test->bytes_sent = 0; 2017 test->blocks_sent = 0; 2018 2019 test->reverse = 0; 2020 test->no_delay = 0; 2021 2022 FD_ZERO(&test->read_set); 2023 FD_ZERO(&test->write_set); 2024 2025 test->num_streams = 1; 2026 test->settings->socket_bufsize = 0; 2027 test->settings->blksize = DEFAULT_TCP_BLKSIZE; 2028 test->settings->rate = 0; 2029 test->settings->burst = 0; 2030 test->settings->mss = 0; 2031 memset(test->cookie, 0, COOKIE_SIZE); 2032 test->multisend = 10; /* arbitrary */ 2033 test->udp_counters_64bit = 0; 2034 2035 /* Free output line buffers, if any (on the server only) */ 2036 struct iperf_textline *t; 2037 while (!TAILQ_EMPTY(&test->server_output_list)) { 2038 t = TAILQ_FIRST(&test->server_output_list); 2039 TAILQ_REMOVE(&test->server_output_list, t, textlineentries); 2040 free(t->line); 2041 free(t); 2042 } 2043 } 2044 2045 2046 /* Reset all of a test's stats back to zero. Called when the omitting 2047 ** period is over. 2048 */ 2049 void 2050 iperf_reset_stats(struct iperf_test *test) 2051 { 2052 struct timeval now; 2053 struct iperf_stream *sp; 2054 struct iperf_stream_result *rp; 2055 2056 test->bytes_sent = 0; 2057 test->blocks_sent = 0; 2058 gettimeofday(&now, NULL); 2059 SLIST_FOREACH(sp, &test->streams, streams) { 2060 sp->omitted_packet_count = sp->packet_count; 2061 sp->jitter = 0; 2062 sp->outoforder_packets = 0; 2063 sp->cnt_error = 0; 2064 rp = sp->result; 2065 rp->bytes_sent = rp->bytes_received = 0; 2066 rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0; 2067 if (test->sender && test->sender_has_retransmits) { 2068 struct iperf_interval_results ir; /* temporary results structure */ 2069 save_tcpinfo(sp, &ir); 2070 rp->stream_prev_total_retrans = get_total_retransmits(&ir); 2071 } 2072 rp->stream_retrans = 0; 2073 rp->start_time = now; 2074 } 2075 } 2076 2077 2078 /**************************************************************************/ 2079 2080 /** 2081 * Gather statistics during a test. 2082 * This function works for both the client and server side. 2083 */ 2084 void 2085 iperf_stats_callback(struct iperf_test *test) 2086 { 2087 struct iperf_stream *sp; 2088 struct iperf_stream_result *rp = NULL; 2089 struct iperf_interval_results *irp, temp; 2090 2091 temp.omitted = test->omitting; 2092 SLIST_FOREACH(sp, &test->streams, streams) { 2093 rp = sp->result; 2094 2095 temp.bytes_transferred = test->sender ? rp->bytes_sent_this_interval : rp->bytes_received_this_interval; 2096 2097 irp = TAILQ_LAST(&rp->interval_results, irlisthead); 2098 /* result->end_time contains timestamp of previous interval */ 2099 if ( irp != NULL ) /* not the 1st interval */ 2100 memcpy(&temp.interval_start_time, &rp->end_time, sizeof(struct timeval)); 2101 else /* or use timestamp from beginning */ 2102 memcpy(&temp.interval_start_time, &rp->start_time, sizeof(struct timeval)); 2103 /* now save time of end of this interval */ 2104 gettimeofday(&rp->end_time, NULL); 2105 memcpy(&temp.interval_end_time, &rp->end_time, sizeof(struct timeval)); 2106 temp.interval_duration = timeval_diff(&temp.interval_start_time, &temp.interval_end_time); 2107 //temp.interval_duration = timeval_diff(&temp.interval_start_time, &temp.interval_end_time); 2108 if (test->protocol->id == Ptcp) { 2109 if ( has_tcpinfo()) { 2110 save_tcpinfo(sp, &temp); 2111 if (test->sender && test->sender_has_retransmits) { 2112 long total_retrans = get_total_retransmits(&temp); 2113 temp.interval_retrans = total_retrans - rp->stream_prev_total_retrans; 2114 rp->stream_retrans += temp.interval_retrans; 2115 rp->stream_prev_total_retrans = total_retrans; 2116 2117 temp.snd_cwnd = get_snd_cwnd(&temp); 2118 if (temp.snd_cwnd > rp->stream_max_snd_cwnd) { 2119 rp->stream_max_snd_cwnd = temp.snd_cwnd; 2120 } 2121 2122 temp.rtt = get_rtt(&temp); 2123 if (temp.rtt > rp->stream_max_rtt) { 2124 rp->stream_max_rtt = temp.rtt; 2125 } 2126 if (rp->stream_min_rtt == 0 || 2127 temp.rtt < rp->stream_min_rtt) { 2128 rp->stream_min_rtt = temp.rtt; 2129 } 2130 rp->stream_sum_rtt += temp.rtt; 2131 rp->stream_count_rtt++; 2132 } 2133 } 2134 } else { 2135 if (irp == NULL) { 2136 temp.interval_packet_count = sp->packet_count; 2137 temp.interval_outoforder_packets = sp->outoforder_packets; 2138 temp.interval_cnt_error = sp->cnt_error; 2139 } else { 2140 temp.interval_packet_count = sp->packet_count - irp->packet_count; 2141 temp.interval_outoforder_packets = sp->outoforder_packets - irp->outoforder_packets; 2142 temp.interval_cnt_error = sp->cnt_error - irp->cnt_error; 2143 } 2144 temp.packet_count = sp->packet_count; 2145 temp.jitter = sp->jitter; 2146 temp.outoforder_packets = sp->outoforder_packets; 2147 temp.cnt_error = sp->cnt_error; 2148 } 2149 add_to_interval_list(rp, &temp); 2150 rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0; 2151 } 2152 } 2153 2154 /** 2155 * Print intermediate results during a test (interval report). 2156 * Uses print_interval_results to print the results for each stream, 2157 * then prints an interval summary for all streams in this 2158 * interval. 2159 */ 2160 static void 2161 iperf_print_intermediate(struct iperf_test *test) 2162 { 2163 char ubuf[UNIT_LEN]; 2164 char nbuf[UNIT_LEN]; 2165 struct iperf_stream *sp = NULL; 2166 struct iperf_interval_results *irp; 2167 iperf_size_t bytes = 0; 2168 double bandwidth; 2169 int retransmits = 0; 2170 double start_time, end_time; 2171 cJSON *json_interval; 2172 cJSON *json_interval_streams; 2173 int total_packets = 0, lost_packets = 0; 2174 double avg_jitter = 0.0, lost_percent; 2175 2176 if (test->json_output) { 2177 json_interval = cJSON_CreateObject(); 2178 if (json_interval == NULL) 2179 return; 2180 cJSON_AddItemToArray(test->json_intervals, json_interval); 2181 json_interval_streams = cJSON_CreateArray(); 2182 if (json_interval_streams == NULL) 2183 return; 2184 cJSON_AddItemToObject(json_interval, "streams", json_interval_streams); 2185 } else { 2186 json_interval = NULL; 2187 json_interval_streams = NULL; 2188 } 2189 2190 SLIST_FOREACH(sp, &test->streams, streams) { 2191 print_interval_results(test, sp, json_interval_streams); 2192 /* sum up all streams */ 2193 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); 2194 if (irp == NULL) { 2195 iperf_err(test, "iperf_print_intermediate error: interval_results is NULL"); 2196 return; 2197 } 2198 bytes += irp->bytes_transferred; 2199 if (test->protocol->id == Ptcp) { 2200 if (test->sender && test->sender_has_retransmits) { 2201 retransmits += irp->interval_retrans; 2202 } 2203 } else { 2204 total_packets += irp->interval_packet_count; 2205 lost_packets += irp->interval_cnt_error; 2206 avg_jitter += irp->jitter; 2207 } 2208 } 2209 2210 /* next build string with sum of all streams */ 2211 if (test->num_streams > 1 || test->json_output) { 2212 sp = SLIST_FIRST(&test->streams); /* reset back to 1st stream */ 2213 /* Only do this of course if there was a first stream */ 2214 if (sp) { 2215 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); /* use 1st stream for timing info */ 2216 2217 unit_snprintf(ubuf, UNIT_LEN, (double) bytes, 'A'); 2218 bandwidth = (double) bytes / (double) irp->interval_duration; 2219 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 2220 2221 start_time = timeval_diff(&sp->result->start_time,&irp->interval_start_time); 2222 end_time = timeval_diff(&sp->result->start_time,&irp->interval_end_time); 2223 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 2224 if (test->sender && test->sender_has_retransmits) { 2225 /* Interval sum, TCP with retransmits. */ 2226 if (test->json_output) 2227 cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d omitted: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (int64_t) retransmits, irp->omitted)); /* XXX irp->omitted or test->omitting? */ 2228 else 2229 iprintf(test, report_sum_bw_retrans_format, start_time, end_time, ubuf, nbuf, retransmits, irp->omitted?report_omitted:""); /* XXX irp->omitted or test->omitting? */ 2230 } else { 2231 /* Interval sum, TCP without retransmits. */ 2232 if (test->json_output) 2233 cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f omitted: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, test->omitting)); 2234 else 2235 iprintf(test, report_sum_bw_format, start_time, end_time, ubuf, nbuf, test->omitting?report_omitted:""); 2236 } 2237 } else { 2238 /* Interval sum, UDP. */ 2239 if (test->sender) { 2240 if (test->json_output) 2241 cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f packets: %d omitted: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (int64_t) total_packets, test->omitting)); 2242 else 2243 iprintf(test, report_sum_bw_udp_sender_format, start_time, end_time, ubuf, nbuf, total_packets, test->omitting?report_omitted:""); 2244 } else { 2245 avg_jitter /= test->num_streams; 2246 lost_percent = 100.0 * lost_packets / total_packets; 2247 if (test->json_output) 2248 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", (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)); 2249 else 2250 iprintf(test, report_sum_bw_udp_format, start_time, end_time, ubuf, nbuf, avg_jitter * 1000.0, lost_packets, total_packets, lost_percent, test->omitting?report_omitted:""); 2251 } 2252 } 2253 } 2254 } 2255 } 2256 2257 /** 2258 * Print overall summary statistics at the end of a test. 2259 */ 2260 static void 2261 iperf_print_results(struct iperf_test *test) 2262 { 2263 2264 cJSON *json_summary_streams = NULL; 2265 cJSON *json_summary_stream = NULL; 2266 int total_retransmits = 0; 2267 int total_packets = 0, lost_packets = 0; 2268 char ubuf[UNIT_LEN]; 2269 char nbuf[UNIT_LEN]; 2270 struct stat sb; 2271 char sbuf[UNIT_LEN]; 2272 struct iperf_stream *sp = NULL; 2273 iperf_size_t bytes_sent, total_sent = 0; 2274 iperf_size_t bytes_received, total_received = 0; 2275 double start_time, end_time, avg_jitter = 0.0, lost_percent; 2276 double bandwidth; 2277 2278 /* print final summary for all intervals */ 2279 2280 if (test->json_output) { 2281 json_summary_streams = cJSON_CreateArray(); 2282 if (json_summary_streams == NULL) 2283 return; 2284 cJSON_AddItemToObject(test->json_end, "streams", json_summary_streams); 2285 } else { 2286 iprintf(test, "%s", report_bw_separator); 2287 if (test->verbose) 2288 iprintf(test, "%s", report_summary); 2289 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 2290 if (test->sender_has_retransmits) 2291 iprintf(test, "%s", report_bw_retrans_header); 2292 else 2293 iprintf(test, "%s", report_bw_header); 2294 } else 2295 iprintf(test, "%s", report_bw_udp_header); 2296 } 2297 2298 start_time = 0.; 2299 sp = SLIST_FIRST(&test->streams); 2300 /* 2301 * If there is at least one stream, then figure out the length of time 2302 * we were running the tests and print out some statistics about 2303 * the streams. It's possible to not have any streams at all 2304 * if the client got interrupted before it got to do anything. 2305 */ 2306 if (sp) { 2307 end_time = timeval_diff(&sp->result->start_time, &sp->result->end_time); 2308 SLIST_FOREACH(sp, &test->streams, streams) { 2309 if (test->json_output) { 2310 json_summary_stream = cJSON_CreateObject(); 2311 if (json_summary_stream == NULL) 2312 return; 2313 cJSON_AddItemToArray(json_summary_streams, json_summary_stream); 2314 } 2315 2316 bytes_sent = sp->result->bytes_sent; 2317 bytes_received = sp->result->bytes_received; 2318 total_sent += bytes_sent; 2319 total_received += bytes_received; 2320 2321 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 2322 if (test->sender_has_retransmits) { 2323 total_retransmits += sp->result->stream_retrans; 2324 } 2325 } else { 2326 total_packets += (sp->packet_count - sp->omitted_packet_count); 2327 lost_packets += sp->cnt_error; 2328 avg_jitter += sp->jitter; 2329 } 2330 2331 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_sent, 'A'); 2332 bandwidth = (double) bytes_sent / (double) end_time; 2333 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 2334 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 2335 if (test->sender_has_retransmits) { 2336 /* Summary, TCP with retransmits. */ 2337 if (test->json_output) 2338 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", (int64_t) sp->socket, (double) start_time, (double) end_time, (double) end_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))); 2339 else 2340 iprintf(test, report_bw_retrans_format, sp->socket, start_time, end_time, ubuf, nbuf, sp->result->stream_retrans, report_sender); 2341 } else { 2342 /* Summary, TCP without retransmits. */ 2343 if (test->json_output) 2344 cJSON_AddItemToObject(json_summary_stream, "sender", iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f", (int64_t) sp->socket, (double) start_time, (double) end_time, (double) end_time, (int64_t) bytes_sent, bandwidth * 8)); 2345 else 2346 iprintf(test, report_bw_format, sp->socket, start_time, end_time, ubuf, nbuf, report_sender); 2347 } 2348 } else { 2349 /* Summary, UDP. */ 2350 lost_percent = 100.0 * sp->cnt_error / (sp->packet_count - sp->omitted_packet_count); 2351 if (test->json_output) 2352 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", (int64_t) sp->socket, (double) start_time, (double) end_time, (double) end_time, (int64_t) bytes_sent, bandwidth * 8, (double) sp->jitter * 1000.0, (int64_t) sp->cnt_error, (int64_t) (sp->packet_count - sp->omitted_packet_count), (double) lost_percent, (int64_t) sp->outoforder_packets)); 2353 else { 2354 iprintf(test, report_bw_udp_format, sp->socket, start_time, end_time, ubuf, nbuf, sp->jitter * 1000.0, sp->cnt_error, (sp->packet_count - sp->omitted_packet_count), lost_percent, ""); 2355 if (test->role == 'c') 2356 iprintf(test, report_datagrams, sp->socket, (sp->packet_count - sp->omitted_packet_count)); 2357 if (sp->outoforder_packets > 0) 2358 iprintf(test, report_sum_outoforder, start_time, end_time, sp->outoforder_packets); 2359 } 2360 } 2361 2362 if (sp->diskfile_fd >= 0) { 2363 if (fstat(sp->diskfile_fd, &sb) == 0) { 2364 int percent = (int) ( ( (double) bytes_sent / (double) sb.st_size ) * 100.0 ); 2365 unit_snprintf(sbuf, UNIT_LEN, (double) sb.st_size, 'A'); 2366 if (test->json_output) 2367 cJSON_AddItemToObject(json_summary_stream, "diskfile", iperf_json_printf("sent: %d size: %d percent: %d filename: %s", (int64_t) bytes_sent, (int64_t) sb.st_size, (int64_t) percent, test->diskfile_name)); 2368 else 2369 iprintf(test, report_diskfile, ubuf, sbuf, percent, test->diskfile_name); 2370 } 2371 } 2372 2373 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_received, 'A'); 2374 bandwidth = (double) bytes_received / (double) end_time; 2375 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 2376 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 2377 if (test->json_output) 2378 cJSON_AddItemToObject(json_summary_stream, "receiver", iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f", (int64_t) sp->socket, (double) start_time, (double) end_time, (double) end_time, (int64_t) bytes_received, bandwidth * 8)); 2379 else 2380 iprintf(test, report_bw_format, sp->socket, start_time, end_time, ubuf, nbuf, report_receiver); 2381 } 2382 } 2383 } 2384 2385 if (test->num_streams > 1 || test->json_output) { 2386 unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A'); 2387 /* If no tests were run, arbitrariliy set bandwidth to 0. */ 2388 if (end_time > 0.0) { 2389 bandwidth = (double) total_sent / (double) end_time; 2390 } 2391 else { 2392 bandwidth = 0.0; 2393 } 2394 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 2395 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 2396 if (test->sender_has_retransmits) { 2397 /* Summary sum, TCP with retransmits. */ 2398 if (test->json_output) 2399 cJSON_AddItemToObject(test->json_end, "sum_sent", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d", (double) start_time, (double) end_time, (double) end_time, (int64_t) total_sent, bandwidth * 8, (int64_t) total_retransmits)); 2400 else 2401 iprintf(test, report_sum_bw_retrans_format, start_time, end_time, ubuf, nbuf, total_retransmits, report_sender); 2402 } else { 2403 /* Summary sum, TCP without retransmits. */ 2404 if (test->json_output) 2405 cJSON_AddItemToObject(test->json_end, "sum_sent", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f", (double) start_time, (double) end_time, (double) end_time, (int64_t) total_sent, bandwidth * 8)); 2406 else 2407 iprintf(test, report_sum_bw_format, start_time, end_time, ubuf, nbuf, report_sender); 2408 } 2409 unit_snprintf(ubuf, UNIT_LEN, (double) total_received, 'A'); 2410 /* If no tests were run, set received bandwidth to 0 */ 2411 if (end_time > 0.0) { 2412 bandwidth = (double) total_received / (double) end_time; 2413 } 2414 else { 2415 bandwidth = 0.0; 2416 } 2417 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 2418 if (test->json_output) 2419 cJSON_AddItemToObject(test->json_end, "sum_received", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f", (double) start_time, (double) end_time, (double) end_time, (int64_t) total_received, bandwidth * 8)); 2420 else 2421 iprintf(test, report_sum_bw_format, start_time, end_time, ubuf, nbuf, report_receiver); 2422 } else { 2423 /* Summary sum, UDP. */ 2424 avg_jitter /= test->num_streams; 2425 /* If no packets were sent, arbitrarily set loss percentage to 100. */ 2426 if (total_packets > 0) { 2427 lost_percent = 100.0 * lost_packets / total_packets; 2428 } 2429 else { 2430 lost_percent = 100.0; 2431 } 2432 if (test->json_output) 2433 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", (double) start_time, (double) end_time, (double) end_time, (int64_t) total_sent, bandwidth * 8, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) total_packets, (double) lost_percent)); 2434 else 2435 iprintf(test, report_sum_bw_udp_format, start_time, end_time, ubuf, nbuf, avg_jitter * 1000.0, lost_packets, total_packets, lost_percent, ""); 2436 } 2437 } 2438 2439 if (test->json_output) 2440 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])); 2441 else { 2442 if (test->verbose) { 2443 iprintf(test, report_cpu, report_local, test->sender?report_sender:report_receiver, test->cpu_util[0], test->cpu_util[1], test->cpu_util[2], report_remote, test->sender?report_receiver:report_sender, test->remote_cpu_util[0], test->remote_cpu_util[1], test->remote_cpu_util[2]); 2444 } 2445 2446 /* Print server output if we're on the client and it was requested/provided */ 2447 if (test->role == 'c' && iperf_get_test_get_server_output(test)) { 2448 if (test->json_server_output) { 2449 iprintf(test, "\nServer JSON output:\n%s\n", cJSON_Print(test->json_server_output)); 2450 cJSON_Delete(test->json_server_output); 2451 test->json_server_output = NULL; 2452 } 2453 if (test->server_output_text) { 2454 iprintf(test, "\nServer output:\n%s\n", test->server_output_text); 2455 test->server_output_text = NULL; 2456 } 2457 } 2458 } 2459 } 2460 2461 /**************************************************************************/ 2462 2463 /** 2464 * Main report-printing callback. 2465 * Prints results either during a test (interval report only) or 2466 * after the entire test has been run (last interval report plus 2467 * overall summary). 2468 */ 2469 void 2470 iperf_reporter_callback(struct iperf_test *test) 2471 { 2472 switch (test->state) { 2473 case TEST_RUNNING: 2474 case STREAM_RUNNING: 2475 /* print interval results for each stream */ 2476 iperf_print_intermediate(test); 2477 break; 2478 case TEST_END: 2479 case DISPLAY_RESULTS: 2480 iperf_print_intermediate(test); 2481 iperf_print_results(test); 2482 break; 2483 } 2484 2485 } 2486 2487 /** 2488 * Print the interval results for one stream. 2489 * This function needs to know about the overall test so it can determine the 2490 * context for printing headers, separators, etc. 2491 */ 2492 static void 2493 print_interval_results(struct iperf_test *test, struct iperf_stream *sp, cJSON *json_interval_streams) 2494 { 2495 char ubuf[UNIT_LEN]; 2496 char nbuf[UNIT_LEN]; 2497 char cbuf[UNIT_LEN]; 2498 double st = 0., et = 0.; 2499 struct iperf_interval_results *irp = NULL; 2500 double bandwidth, lost_percent; 2501 2502 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); /* get last entry in linked list */ 2503 if (irp == NULL) { 2504 iperf_err(test, "print_interval_results error: interval_results is NULL"); 2505 return; 2506 } 2507 if (!test->json_output) { 2508 /* First stream? */ 2509 if (sp == SLIST_FIRST(&test->streams)) { 2510 /* It it's the first interval, print the header; 2511 ** else if there's more than one stream, print the separator; 2512 ** else nothing. 2513 */ 2514 if (timeval_equals(&sp->result->start_time, &irp->interval_start_time)) { 2515 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 2516 if (test->sender && test->sender_has_retransmits) 2517 iprintf(test, "%s", report_bw_retrans_cwnd_header); 2518 else 2519 iprintf(test, "%s", report_bw_header); 2520 } else { 2521 if (test->sender) 2522 iprintf(test, "%s", report_bw_udp_sender_header); 2523 else 2524 iprintf(test, "%s", report_bw_udp_header); 2525 } 2526 } else if (test->num_streams > 1) 2527 iprintf(test, "%s", report_bw_separator); 2528 } 2529 } 2530 2531 unit_snprintf(ubuf, UNIT_LEN, (double) (irp->bytes_transferred), 'A'); 2532 bandwidth = (double) irp->bytes_transferred / (double) irp->interval_duration; 2533 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); 2534 2535 st = timeval_diff(&sp->result->start_time, &irp->interval_start_time); 2536 et = timeval_diff(&sp->result->start_time, &irp->interval_end_time); 2537 2538 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { 2539 if (test->sender && test->sender_has_retransmits) { 2540 /* Interval, TCP with retransmits. */ 2541 if (test->json_output) 2542 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 omitted: %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, irp->omitted)); 2543 else { 2544 unit_snprintf(cbuf, UNIT_LEN, irp->snd_cwnd, 'A'); 2545 iprintf(test, report_bw_retrans_cwnd_format, sp->socket, st, et, ubuf, nbuf, irp->interval_retrans, cbuf, irp->omitted?report_omitted:""); 2546 } 2547 } else { 2548 /* Interval, TCP without retransmits. */ 2549 if (test->json_output) 2550 cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f omitted: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, irp->omitted)); 2551 else 2552 iprintf(test, report_bw_format, sp->socket, st, et, ubuf, nbuf, irp->omitted?report_omitted:""); 2553 } 2554 } else { 2555 /* Interval, UDP. */ 2556 if (test->sender) { 2557 if (test->json_output) 2558 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", (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)); 2559 else 2560 iprintf(test, report_bw_udp_sender_format, sp->socket, st, et, ubuf, nbuf, irp->interval_packet_count, irp->omitted?report_omitted:""); 2561 } else { 2562 lost_percent = 100.0 * irp->interval_cnt_error / irp->interval_packet_count; 2563 if (test->json_output) 2564 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", (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)); 2565 else 2566 iprintf(test, report_bw_udp_format, sp->socket, st, et, ubuf, nbuf, irp->jitter * 1000.0, irp->interval_cnt_error, irp->interval_packet_count, lost_percent, irp->omitted?report_omitted:""); 2567 } 2568 } 2569 2570 if (test->logfile) 2571 iflush(test); 2572 } 2573 2574 /**************************************************************************/ 2575 void 2576 iperf_free_stream(struct iperf_stream *sp) 2577 { 2578 struct iperf_interval_results *irp, *nirp; 2579 2580 /* XXX: need to free interval list too! */ 2581 munmap(sp->buffer, sp->test->settings->blksize); 2582 close(sp->buffer_fd); 2583 if (sp->diskfile_fd >= 0) 2584 close(sp->diskfile_fd); 2585 for (irp = TAILQ_FIRST(&sp->result->interval_results); irp != TAILQ_END(sp->result->interval_results); irp = nirp) { 2586 nirp = TAILQ_NEXT(irp, irlistentries); 2587 free(irp); 2588 } 2589 free(sp->result); 2590 if (sp->send_timer != NULL) 2591 tmr_cancel(sp->send_timer); 2592 free(sp); 2593 } 2594 2595 /**************************************************************************/ 2596 struct iperf_stream * 2597 iperf_new_stream(struct iperf_test *test, int s) 2598 { 2599 int i; 2600 struct iperf_stream *sp; 2601 2602 char template[1024]; 2603 if (test->tmp_template) { 2604 snprintf(template, sizeof(template) / sizeof(char), "%s", test->tmp_template); 2605 } else { 2606 char buf[] = "/tmp/iperf3.XXXXXX"; 2607 snprintf(template, sizeof(template) / sizeof(char), "%s", buf); 2608 } 2609 2610 h_errno = 0; 2611 2612 sp = (struct iperf_stream *) malloc(sizeof(struct iperf_stream)); 2613 if (!sp) { 2614 i_errno = IECREATESTREAM; 2615 return NULL; 2616 } 2617 2618 memset(sp, 0, sizeof(struct iperf_stream)); 2619 2620 sp->test = test; 2621 sp->settings = test->settings; 2622 sp->result = (struct iperf_stream_result *) malloc(sizeof(struct iperf_stream_result)); 2623 if (!sp->result) { 2624 free(sp); 2625 i_errno = IECREATESTREAM; 2626 return NULL; 2627 } 2628 2629 memset(sp->result, 0, sizeof(struct iperf_stream_result)); 2630 TAILQ_INIT(&sp->result->interval_results); 2631 2632 /* Create and randomize the buffer */ 2633 sp->buffer_fd = mkstemp(template); 2634 if (sp->buffer_fd == -1) { 2635 i_errno = IECREATESTREAM; 2636 free(sp->result); 2637 free(sp); 2638 return NULL; 2639 } 2640 if (unlink(template) < 0) { 2641 i_errno = IECREATESTREAM; 2642 free(sp->result); 2643 free(sp); 2644 return NULL; 2645 } 2646 if (ftruncate(sp->buffer_fd, test->settings->blksize) < 0) { 2647 i_errno = IECREATESTREAM; 2648 free(sp->result); 2649 free(sp); 2650 return NULL; 2651 } 2652 sp->buffer = (char *) mmap(NULL, test->settings->blksize, PROT_READ|PROT_WRITE, MAP_PRIVATE, sp->buffer_fd, 0); 2653 if (sp->buffer == MAP_FAILED) { 2654 i_errno = IECREATESTREAM; 2655 free(sp->result); 2656 free(sp); 2657 return NULL; 2658 } 2659 srandom(time(NULL)); 2660 for (i = 0; i < test->settings->blksize; ++i) 2661 sp->buffer[i] = random(); 2662 2663 /* Set socket */ 2664 sp->socket = s; 2665 2666 sp->snd = test->protocol->send; 2667 sp->rcv = test->protocol->recv; 2668 2669 if (test->diskfile_name != (char*) 0) { 2670 sp->diskfile_fd = open(test->diskfile_name, test->sender ? O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC), S_IRUSR|S_IWUSR); 2671 if (sp->diskfile_fd == -1) { 2672 i_errno = IEFILE; 2673 munmap(sp->buffer, sp->test->settings->blksize); 2674 free(sp->result); 2675 free(sp); 2676 return NULL; 2677 } 2678 sp->snd2 = sp->snd; 2679 sp->snd = diskfile_send; 2680 sp->rcv2 = sp->rcv; 2681 sp->rcv = diskfile_recv; 2682 } else 2683 sp->diskfile_fd = -1; 2684 2685 /* Initialize stream */ 2686 if (iperf_init_stream(sp, test) < 0) { 2687 close(sp->buffer_fd); 2688 munmap(sp->buffer, sp->test->settings->blksize); 2689 free(sp->result); 2690 free(sp); 2691 return NULL; 2692 } 2693 iperf_add_stream(test, sp); 2694 2695 return sp; 2696 } 2697 2698 /**************************************************************************/ 2699 int 2700 iperf_init_stream(struct iperf_stream *sp, struct iperf_test *test) 2701 { 2702 socklen_t len; 2703 int opt; 2704 2705 len = sizeof(struct sockaddr_storage); 2706 if (getsockname(sp->socket, (struct sockaddr *) &sp->local_addr, &len) < 0) { 2707 i_errno = IEINITSTREAM; 2708 return -1; 2709 } 2710 len = sizeof(struct sockaddr_storage); 2711 if (getpeername(sp->socket, (struct sockaddr *) &sp->remote_addr, &len) < 0) { 2712 i_errno = IEINITSTREAM; 2713 return -1; 2714 } 2715 2716 /* Set IP TOS */ 2717 if ((opt = test->settings->tos)) { 2718 if (getsockdomain(sp->socket) == AF_INET6) { 2719 #ifdef IPV6_TCLASS 2720 if (setsockopt(sp->socket, IPPROTO_IPV6, IPV6_TCLASS, &opt, sizeof(opt)) < 0) { 2721 i_errno = IESETCOS; 2722 return -1; 2723 } 2724 #else 2725 i_errno = IESETCOS; 2726 return -1; 2727 #endif 2728 } else { 2729 if (setsockopt(sp->socket, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)) < 0) { 2730 i_errno = IESETTOS; 2731 return -1; 2732 } 2733 } 2734 } 2735 2736 return 0; 2737 } 2738 2739 /**************************************************************************/ 2740 void 2741 iperf_add_stream(struct iperf_test *test, struct iperf_stream *sp) 2742 { 2743 int i; 2744 struct iperf_stream *n, *prev; 2745 2746 if (SLIST_EMPTY(&test->streams)) { 2747 SLIST_INSERT_HEAD(&test->streams, sp, streams); 2748 sp->id = 1; 2749 } else { 2750 // for (n = test->streams, i = 2; n->next; n = n->next, ++i); 2751 i = 2; 2752 SLIST_FOREACH(n, &test->streams, streams) { 2753 prev = n; 2754 ++i; 2755 } 2756 SLIST_INSERT_AFTER(prev, sp, streams); 2757 sp->id = i; 2758 } 2759 } 2760 2761 /* This pair of routines gets inserted into the snd/rcv function pointers 2762 ** when there's a -F flag. They handle the file stuff and call the real 2763 ** snd/rcv functions, which have been saved in snd2/rcv2. 2764 ** 2765 ** The advantage of doing it this way is that in the much more common 2766 ** case of no -F flag, there is zero extra overhead. 2767 */ 2768 2769 static int 2770 diskfile_send(struct iperf_stream *sp) 2771 { 2772 int r; 2773 2774 r = read(sp->diskfile_fd, sp->buffer, sp->test->settings->blksize); 2775 if (r == 0) 2776 sp->test->done = 1; 2777 else 2778 r = sp->snd2(sp); 2779 return r; 2780 } 2781 2782 static int 2783 diskfile_recv(struct iperf_stream *sp) 2784 { 2785 int r; 2786 2787 r = sp->rcv2(sp); 2788 if (r > 0) { 2789 (void) write(sp->diskfile_fd, sp->buffer, r); 2790 (void) fsync(sp->diskfile_fd); 2791 } 2792 return r; 2793 } 2794 2795 2796 void 2797 iperf_catch_sigend(void (*handler)(int)) 2798 { 2799 signal(SIGINT, handler); 2800 signal(SIGTERM, handler); 2801 signal(SIGHUP, handler); 2802 } 2803 2804 /** 2805 * Called as a result of getting a signal. 2806 * Depending on the current state of the test (and the role of this 2807 * process) compute and report one more set of ending statistics 2808 * before cleaning up and exiting. 2809 */ 2810 void 2811 iperf_got_sigend(struct iperf_test *test) 2812 { 2813 /* 2814 * If we're the client, or if we're a server and running a test, 2815 * then dump out the accumulated stats so far. 2816 */ 2817 if (test->role == 'c' || 2818 (test->role == 's' && test->state == TEST_RUNNING)) { 2819 2820 test->done = 1; 2821 cpu_util(test->cpu_util); 2822 test->stats_callback(test); 2823 test->state = DISPLAY_RESULTS; /* change local state only */ 2824 if (test->on_test_finish) 2825 test->on_test_finish(test); 2826 test->reporter_callback(test); 2827 } 2828 2829 if (test->ctrl_sck >= 0) { 2830 test->state = (test->role == 'c') ? CLIENT_TERMINATE : SERVER_TERMINATE; 2831 (void) Nwrite(test->ctrl_sck, (char*) &test->state, sizeof(signed char), Ptcp); 2832 } 2833 i_errno = (test->role == 'c') ? IECLIENTTERM : IESERVERTERM; 2834 iperf_errexit(test, "interrupt - %s", iperf_strerror(i_errno)); 2835 } 2836 2837 /* Try to write a PID file if requested, return -1 on an error. */ 2838 int 2839 iperf_create_pidfile(struct iperf_test *test) 2840 { 2841 if (test->pidfile) { 2842 int fd; 2843 char buf[8]; 2844 fd = open(test->pidfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR); 2845 if (fd < 0) { 2846 return -1; 2847 } 2848 snprintf(buf, sizeof(buf), "%d", getpid()); /* no trailing newline */ 2849 if (write(fd, buf, strlen(buf) + 1) < 0) { 2850 return -1; 2851 } 2852 if (close(fd) < 0) { 2853 return -1; 2854 }; 2855 } 2856 return 0; 2857 } 2858 2859 /* Get rid of a PID file, return -1 on error. */ 2860 int 2861 iperf_delete_pidfile(struct iperf_test *test) 2862 { 2863 if (test->pidfile) { 2864 if (unlink(test->pidfile) < 0) { 2865 return -1; 2866 } 2867 } 2868 return 0; 2869 } 2870 2871 int 2872 iperf_json_start(struct iperf_test *test) 2873 { 2874 test->json_top = cJSON_CreateObject(); 2875 if (test->json_top == NULL) 2876 return -1; 2877 test->json_start = cJSON_CreateObject(); 2878 if (test->json_start == NULL) 2879 return -1; 2880 cJSON_AddItemToObject(test->json_top, "start", test->json_start); 2881 test->json_connected = cJSON_CreateArray(); 2882 if (test->json_connected == NULL) 2883 return -1; 2884 cJSON_AddItemToObject(test->json_start, "connected", test->json_connected); 2885 test->json_intervals = cJSON_CreateArray(); 2886 if (test->json_intervals == NULL) 2887 return -1; 2888 cJSON_AddItemToObject(test->json_top, "intervals", test->json_intervals); 2889 test->json_end = cJSON_CreateObject(); 2890 if (test->json_end == NULL) 2891 return -1; 2892 cJSON_AddItemToObject(test->json_top, "end", test->json_end); 2893 return 0; 2894 } 2895 2896 int 2897 iperf_json_finish(struct iperf_test *test) 2898 { 2899 if (test->title) 2900 cJSON_AddStringToObject(test->json_top, "title", test->title); 2901 /* Include server output */ 2902 if (test->json_server_output) { 2903 cJSON_AddItemToObject(test->json_top, "server_output_json", test->json_server_output); 2904 } 2905 if (test->server_output_text) { 2906 cJSON_AddStringToObject(test->json_top, "server_output_text", test->server_output_text); 2907 } 2908 test->json_output_string = cJSON_Print(test->json_top); 2909 if (test->json_output_string == NULL) 2910 return -1; 2911 fprintf(test->outfile, "%s\n", test->json_output_string); 2912 iflush(test); 2913 cJSON_Delete(test->json_top); 2914 test->json_top = test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL; 2915 return 0; 2916 } 2917 2918 2919 /* CPU affinity stuff - Linux and FreeBSD only. */ 2920 2921 int 2922 iperf_setaffinity(struct iperf_test *test, int affinity) 2923 { 2924 #if defined(HAVE_SCHED_SETAFFINITY) 2925 cpu_set_t cpu_set; 2926 2927 CPU_ZERO(&cpu_set); 2928 CPU_SET(affinity, &cpu_set); 2929 if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0) { 2930 i_errno = IEAFFINITY; 2931 return -1; 2932 } 2933 return 0; 2934 #elif defined(HAVE_CPUSET_SETAFFINITY) 2935 cpuset_t cpumask; 2936 2937 if(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, 2938 sizeof(cpuset_t), &test->cpumask) != 0) { 2939 i_errno = IEAFFINITY; 2940 return -1; 2941 } 2942 2943 CPU_ZERO(&cpumask); 2944 CPU_SET(affinity, &cpumask); 2945 2946 if(cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_PID, -1, 2947 sizeof(cpuset_t), &cpumask) != 0) { 2948 i_errno = IEAFFINITY; 2949 return -1; 2950 } 2951 return 0; 2952 #else /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY */ 2953 i_errno = IEAFFINITY; 2954 return -1; 2955 #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY */ 2956 } 2957 2958 int 2959 iperf_clearaffinity(struct iperf_test *test) 2960 { 2961 #if defined(HAVE_SCHED_SETAFFINITY) 2962 cpu_set_t cpu_set; 2963 int i; 2964 2965 CPU_ZERO(&cpu_set); 2966 for (i = 0; i < CPU_SETSIZE; ++i) 2967 CPU_SET(i, &cpu_set); 2968 if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0) { 2969 i_errno = IEAFFINITY; 2970 return -1; 2971 } 2972 return 0; 2973 #elif defined(HAVE_CPUSET_SETAFFINITY) 2974 if(cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_PID, -1, 2975 sizeof(cpuset_t), &test->cpumask) != 0) { 2976 i_errno = IEAFFINITY; 2977 return -1; 2978 } 2979 return 0; 2980 #else /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY */ 2981 i_errno = IEAFFINITY; 2982 return -1; 2983 #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY */ 2984 } 2985 2986 int 2987 iprintf(struct iperf_test *test, const char* format, ...) 2988 { 2989 va_list argp; 2990 int r = -1; 2991 2992 /* 2993 * There are roughly two use cases here. If we're the client, 2994 * want to print stuff directly to the output stream. 2995 * If we're the sender we might need to buffer up output to send 2996 * to the client. 2997 * 2998 * This doesn't make a whole lot of difference except there are 2999 * some chunks of output on the client (on particular the whole 3000 * of the server output with --get-server-output) that could 3001 * easily exceed the size of the line buffer, but which don't need 3002 * to be buffered up anyway. 3003 */ 3004 if (test->role == 'c') { 3005 if (test->title) 3006 fprintf(test->outfile, "%s: ", test->title); 3007 va_start(argp, format); 3008 r = vfprintf(test->outfile, format, argp); 3009 va_end(argp); 3010 } 3011 else if (test->role == 's') { 3012 char linebuffer[1024]; 3013 va_start(argp, format); 3014 r = vsnprintf(linebuffer, sizeof(linebuffer), format, argp); 3015 va_end(argp); 3016 fprintf(test->outfile, "%s", linebuffer); 3017 3018 if (test->role == 's' && iperf_get_test_get_server_output(test)) { 3019 struct iperf_textline *l = (struct iperf_textline *) malloc(sizeof(struct iperf_textline)); 3020 l->line = strdup(linebuffer); 3021 TAILQ_INSERT_TAIL(&(test->server_output_list), l, textlineentries); 3022 } 3023 } 3024 return r; 3025 } 3026 3027 int 3028 iflush(struct iperf_test *test) 3029 { 3030 return fflush(test->outfile); 3031 } 3032