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