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