--- embedaddon/iperf/src/iperf_api.c 2021/03/17 00:36:46 1.1.1.2 +++ embedaddon/iperf/src/iperf_api.c 2023/09/27 11:14:54 1.1.1.3 @@ -1,5 +1,5 @@ /* - * iperf, Copyright (c) 2014-2020, The Regents of the University of + * iperf, Copyright (c) 2014-2022, The Regents of the University of * California, through Lawrence Berkeley National Laboratory (subject * to receipt of any required approvals from the U.S. Dept. of * Energy). All rights reserved. @@ -49,7 +49,6 @@ #ifdef HAVE_STDINT_H #include #endif -#include #include #include #include @@ -89,6 +88,7 @@ #include "version.h" #if defined(HAVE_SSL) #include +#include #include "iperf_auth.h" #endif /* HAVE_SSL */ @@ -116,7 +116,7 @@ usage() void usage_long(FILE *f) { - fprintf(f, usage_longstr, UDP_RATE / (1024*1024), DURATION, DEFAULT_TCP_BLKSIZE / 1024, DEFAULT_UDP_BLKSIZE); + fprintf(f, usage_longstr, DEFAULT_NO_MSG_RCVD_TIMEOUT, UDP_RATE / (1024*1024), DEFAULT_PACING_TIMER, DURATION, DEFAULT_TCP_BLKSIZE / 1024, DEFAULT_UDP_BLKSIZE); } @@ -225,6 +225,12 @@ iperf_get_test_reverse(struct iperf_test *ipt) } int +iperf_get_test_bidirectional(struct iperf_test *ipt) +{ + return ipt->bidirectional; +} + +int iperf_get_test_blksize(struct iperf_test *ipt) { return ipt->settings->blksize; @@ -279,6 +285,12 @@ iperf_get_test_repeating_payload(struct iperf_test *ip } int +iperf_get_test_bind_port(struct iperf_test *ipt) +{ + return ipt->bind_port; +} + +int iperf_get_test_server_port(struct iperf_test *ipt) { return ipt->server_port; @@ -338,6 +350,12 @@ iperf_get_test_bind_address(struct iperf_test *ipt) return ipt->bind_address; } +char * +iperf_get_test_bind_dev(struct iperf_test *ipt) +{ + return ipt->bind_dev; +} + int iperf_get_test_udp_counters_64bit(struct iperf_test *ipt) { @@ -381,6 +399,42 @@ iperf_get_test_connect_timeout(struct iperf_test *ipt) return ipt->settings->connect_timeout; } +int +iperf_get_test_idle_timeout(struct iperf_test *ipt) +{ + return ipt->settings->idle_timeout; +} + +int +iperf_get_dont_fragment(struct iperf_test *ipt) +{ + return ipt->settings->dont_fragment; +} + +struct iperf_time* +iperf_get_test_rcv_timeout(struct iperf_test *ipt) +{ + return &ipt->settings->rcv_timeout; +} + +char* +iperf_get_test_congestion_control(struct iperf_test* ipt) +{ + return ipt->congestion; +} + +int +iperf_get_test_mss(struct iperf_test *ipt) +{ + return ipt->settings->mss; +} + +int +iperf_get_mapped_v4(struct iperf_test* ipt) +{ + return ipt->mapped_v4; +} + /************** Setter routines for some fields inside iperf_test *************/ void @@ -492,6 +546,12 @@ iperf_set_test_burst(struct iperf_test *ipt, int burst } void +iperf_set_test_bind_port(struct iperf_test *ipt, int bind_port) +{ + ipt->bind_port = bind_port; +} + +void iperf_set_test_server_port(struct iperf_test *ipt, int srv_port) { ipt->server_port = srv_port; @@ -527,6 +587,36 @@ iperf_set_test_timestamp_format(struct iperf_test *ipt ipt->timestamp_format = strdup(tf); } +void +iperf_set_mapped_v4(struct iperf_test *ipt, const int val) +{ + ipt->mapped_v4 = val; +} + +void +iperf_set_on_new_stream_callback(struct iperf_test* ipt, void (*callback)()) +{ + ipt->on_new_stream = callback; +} + +void +iperf_set_on_test_start_callback(struct iperf_test* ipt, void (*callback)()) +{ + ipt->on_test_start = callback; +} + +void +iperf_set_on_test_connect_callback(struct iperf_test* ipt, void (*callback)()) +{ + ipt->on_connect = callback; +} + +void +iperf_set_on_test_finish_callback(struct iperf_test* ipt, void (*callback)()) +{ + ipt->on_test_finish = callback; +} + static void check_sender_has_retransmits(struct iperf_test *ipt) { @@ -642,6 +732,12 @@ iperf_set_test_server_authorized_users(struct iperf_te } void +iperf_set_test_server_skew_threshold(struct iperf_test *ipt, int server_skew_threshold) +{ + ipt->server_skew_threshold = server_skew_threshold; +} + +void iperf_set_test_server_rsa_privkey(struct iperf_test *ipt, const char *server_rsa_privkey_base64) { ipt->server_rsa_private_key = load_privkey_from_base64(server_rsa_privkey_base64); @@ -655,6 +751,12 @@ iperf_set_test_bind_address(struct iperf_test *ipt, co } void +iperf_set_test_bind_dev(struct iperf_test *ipt, const char *bnd_dev) +{ + ipt->bind_dev = strdup(bnd_dev); +} + +void iperf_set_test_udp_counters_64bit(struct iperf_test *ipt, int udp_counters_64bit) { ipt->udp_counters_64bit = udp_counters_64bit; @@ -700,7 +802,37 @@ iperf_set_test_connect_timeout(struct iperf_test* ipt, ipt->settings->connect_timeout = ct; } +void +iperf_set_test_idle_timeout(struct iperf_test* ipt, int to) +{ + ipt->settings->idle_timeout = to; +} +void +iperf_set_dont_fragment(struct iperf_test* ipt, int dnf) +{ + ipt->settings->dont_fragment = dnf; +} + +void +iperf_set_test_rcv_timeout(struct iperf_test* ipt, struct iperf_time* to) +{ + ipt->settings->rcv_timeout.secs = to->secs; + ipt->settings->rcv_timeout.usecs = to->usecs; +} + +void +iperf_set_test_congestion_control(struct iperf_test* ipt, char* cc) +{ + ipt->congestion = strdup(cc); +} + +void +iperf_set_test_mss(struct iperf_test *ipt, int mss) +{ + ipt->settings->mss = mss; +} + /********************** Get/set test protocol structure ***********************/ struct protocol * @@ -749,7 +881,7 @@ void iperf_on_test_start(struct iperf_test *test) { if (test->json_output) { - cJSON_AddItemToObject(test->json_start, "test_start", iperf_json_printf("protocol: %s num_streams: %d blksize: %d omit: %d duration: %d bytes: %d blocks: %d reverse: %d tos: %d", test->protocol->name, (int64_t) test->num_streams, (int64_t) test->settings->blksize, (int64_t) test->omit, (int64_t) test->duration, (int64_t) test->settings->bytes, (int64_t) test->settings->blocks, test->reverse?(int64_t)1:(int64_t)0, (int64_t) test->settings->tos)); + cJSON_AddItemToObject(test->json_start, "test_start", iperf_json_printf("protocol: %s num_streams: %d blksize: %d omit: %d duration: %d bytes: %d blocks: %d reverse: %d tos: %d target_bitrate: %d bidir: %d fqrate: %d", test->protocol->name, (int64_t) test->num_streams, (int64_t) test->settings->blksize, (int64_t) test->omit, (int64_t) test->duration, (int64_t) test->settings->bytes, (int64_t) test->settings->blocks, test->reverse?(int64_t)1:(int64_t)0, (int64_t) test->settings->tos, (int64_t) test->settings->rate, (int64_t) test->bidirectional, (uint64_t) test->settings->fqrate)); } else { if (test->verbose) { if (test->settings->bytes) @@ -766,8 +898,10 @@ iperf_on_test_start(struct iperf_test *test) ** old IPv4 format, which is easier on the eyes of network veterans. ** ** If the v6 address is not v4-mapped it is left alone. +** +** Returns 1 if the v6 address is v4-mapped, 0 otherwise. */ -static void +static int mapped_v4_to_regular_v4(char *str) { char *prefix = "::ffff:"; @@ -777,7 +911,9 @@ mapped_v4_to_regular_v4(char *str) if (strncmp(str, prefix, prefix_len) == 0) { int str_len = strlen(str); memmove(str, str + prefix_len, str_len - prefix_len + 1); + return 1; } + return 0; } void @@ -820,7 +956,9 @@ iperf_on_connect(struct iperf_test *test) inet_ntop(AF_INET6, &sa_in6P->sin6_addr, ipr, sizeof(ipr)); port = ntohs(sa_in6P->sin6_port); } - mapped_v4_to_regular_v4(ipr); + if (mapped_v4_to_regular_v4(ipr)) { + iperf_set_mapped_v4(test, 1); + } if (test->json_output) cJSON_AddItemToObject(test->json_start, "accepted_connection", iperf_json_printf("host: %s port: %d", ipr, (int64_t) port)); else @@ -834,9 +972,10 @@ iperf_on_connect(struct iperf_test *test) else { cJSON_AddNumberToObject(test->json_start, "tcp_mss_default", test->ctrl_sck_mss); } - if (test->settings->rate) - cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate); } + // Duplicate to make sure it appears on all output + cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate); + cJSON_AddNumberToObject(test->json_start, "fq_rate", test->settings->fqrate); } else if (test->verbose) { iperf_printf(test, report_cookie, test->cookie); if (test->protocol->id == SOCK_STREAM) { @@ -859,7 +998,55 @@ iperf_on_test_finish(struct iperf_test *test) /******************************************************************************/ +/* + * iperf_parse_hostname tries to split apart a string into hostname % + * interface parts, which are returned in **p and **p1, if they + * exist. If the %interface part is detected, and it's not an IPv6 + * link local address, then returns 1, else returns 0. + * + * Modifies the string pointed to by spec in-place due to the use of + * strtok(3). The caller should strdup(3) or otherwise copy the string + * if an unmodified copy is needed. + */ int +iperf_parse_hostname(struct iperf_test *test, char *spec, char **p, char **p1) { + struct in6_addr ipv6_addr; + + // Format is [%] + if ((*p = strtok(spec, "%")) != NULL && + (*p1 = strtok(NULL, "%")) != NULL) { + + /* + * If an IPv6 literal for a link-local address, then + * tell the caller to leave the "%" in the hostname. + */ + if (inet_pton(AF_INET6, *p, &ipv6_addr) == 1 && + IN6_IS_ADDR_LINKLOCAL(&ipv6_addr)) { + if (test->debug) { + iperf_printf(test, "IPv6 link-local address literal detected\n"); + } + return 0; + } + /* + * Other kind of address or FQDN. The interface name after + * "%" is a shorthand for --bind-dev. + */ + else { + if (test->debug) { + iperf_printf(test, "p %s p1 %s\n", *p, *p1); + } + return 1; + } + } + else { + if (test->debug) { + iperf_printf(test, "noparse\n"); + } + return 0; + } +} + +int iperf_parse_arguments(struct iperf_test *test, int argc, char **argv) { static struct option longopts[] = @@ -887,6 +1074,9 @@ iperf_parse_arguments(struct iperf_test *test, int arg {"bidir", no_argument, NULL, OPT_BIDIRECTIONAL}, {"window", required_argument, NULL, 'w'}, {"bind", required_argument, NULL, 'B'}, +#if defined(HAVE_SO_BINDTODEVICE) + {"bind-dev", required_argument, NULL, OPT_BIND_DEV}, +#endif /* HAVE_SO_BINDTODEVICE */ {"cport", required_argument, NULL, OPT_CLIENT_PORT}, {"set-mss", required_argument, NULL, 'M'}, {"no-delay", no_argument, NULL, 'N'}, @@ -922,33 +1112,42 @@ iperf_parse_arguments(struct iperf_test *test, int arg {"get-server-output", no_argument, NULL, OPT_GET_SERVER_OUTPUT}, {"udp-counters-64bit", no_argument, NULL, OPT_UDP_COUNTERS_64BIT}, {"no-fq-socket-pacing", no_argument, NULL, OPT_NO_FQ_SOCKET_PACING}, +#if defined(HAVE_DONT_FRAGMENT) + {"dont-fragment", no_argument, NULL, OPT_DONT_FRAGMENT}, +#endif /* HAVE_DONT_FRAGMENT */ #if defined(HAVE_SSL) {"username", required_argument, NULL, OPT_CLIENT_USERNAME}, {"rsa-public-key-path", required_argument, NULL, OPT_CLIENT_RSA_PUBLIC_KEY}, {"rsa-private-key-path", required_argument, NULL, OPT_SERVER_RSA_PRIVATE_KEY}, {"authorized-users-path", required_argument, NULL, OPT_SERVER_AUTHORIZED_USERS}, + {"time-skew-threshold", required_argument, NULL, OPT_SERVER_SKEW_THRESHOLD}, #endif /* HAVE_SSL */ {"fq-rate", required_argument, NULL, OPT_FQ_RATE}, {"pacing-timer", required_argument, NULL, OPT_PACING_TIMER}, {"connect-timeout", required_argument, NULL, OPT_CONNECT_TIMEOUT}, - {"debug", no_argument, NULL, 'd'}, + {"idle-timeout", required_argument, NULL, OPT_IDLE_TIMEOUT}, + {"rcv-timeout", required_argument, NULL, OPT_RCV_TIMEOUT}, + {"snd-timeout", required_argument, NULL, OPT_SND_TIMEOUT}, + {"debug", optional_argument, NULL, 'd'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; int flag; int portno; int blksize; - int server_flag, client_flag, rate_flag, duration_flag; + int server_flag, client_flag, rate_flag, duration_flag, rcv_timeout_flag, snd_timeout_flag; char *endptr; #if defined(HAVE_CPU_AFFINITY) char* comma; #endif /* HAVE_CPU_AFFINITY */ char* slash; + char *p, *p1; struct xbind_entry *xbe; double farg; + int rcv_timeout_in = 0; blksize = 0; - server_flag = client_flag = rate_flag = duration_flag = 0; + server_flag = client_flag = rate_flag = duration_flag = rcv_timeout_flag = snd_timeout_flag =0; #if defined(HAVE_SSL) char *client_username = NULL, *client_rsa_public_key = NULL, *server_rsa_private_key = NULL; #endif /* HAVE_SSL */ @@ -1025,6 +1224,18 @@ iperf_parse_arguments(struct iperf_test *test, int arg } iperf_set_test_role(test, 'c'); iperf_set_test_server_hostname(test, optarg); + + if (iperf_parse_hostname(test, optarg, &p, &p1)) { +#if defined(HAVE_SO_BINDTODEVICE) + /* Get rid of the hostname we saved earlier. */ + free(iperf_get_test_server_hostname(test)); + iperf_set_test_server_hostname(test, p); + iperf_set_test_bind_dev(test, p1); +#else /* HAVE_SO_BINDTODEVICE */ + i_errno = IEBINDDEVNOSUPPORT; + return -1; +#endif /* HAVE_SO_BINDTODEVICE */ + } break; case 'u': set_protocol(test, Pudp); @@ -1126,7 +1337,7 @@ iperf_parse_arguments(struct iperf_test *test, int arg break; case 'w': // XXX: This is a socket buffer, not specific to TCP - // Do sanity checks as double-precision floating point + // Do sanity checks as double-precision floating point // to avoid possible integer overflows. farg = unit_atof(optarg); if (farg > (double) MAX_TCP_BUFFER) { @@ -1136,9 +1347,27 @@ iperf_parse_arguments(struct iperf_test *test, int arg test->settings->socket_bufsize = (int) farg; client_flag = 1; break; + case 'B': - test->bind_address = strdup(optarg); + iperf_set_test_bind_address(test, optarg); + + if (iperf_parse_hostname(test, optarg, &p, &p1)) { +#if defined(HAVE_SO_BINDTODEVICE) + /* Get rid of the hostname we saved earlier. */ + free(iperf_get_test_bind_address(test)); + iperf_set_test_bind_address(test, p); + iperf_set_test_bind_dev(test, p1); +#else /* HAVE_SO_BINDTODEVICE */ + i_errno = IEBINDDEVNOSUPPORT; + return -1; +#endif /* HAVE_SO_BINDTODEVICE */ + } break; +#if defined (HAVE_SO_BINDTODEVICE) + case OPT_BIND_DEV: + iperf_set_test_bind_dev(test, optarg); + break; +#endif /* HAVE_SO_BINDTODEVICE */ case OPT_CLIENT_PORT: portno = atoi(optarg); if (portno < 1 || portno > 65535) { @@ -1247,10 +1476,38 @@ iperf_parse_arguments(struct iperf_test *test, int arg case 'F': test->diskfile_name = optarg; break; + case OPT_IDLE_TIMEOUT: + test->settings->idle_timeout = atoi(optarg); + if (test->settings->idle_timeout < 1 || test->settings->idle_timeout > MAX_TIME) { + i_errno = IEIDLETIMEOUT; + return -1; + } + server_flag = 1; + break; + case OPT_RCV_TIMEOUT: + rcv_timeout_in = atoi(optarg); + if (rcv_timeout_in < MIN_NO_MSG_RCVD_TIMEOUT || rcv_timeout_in > MAX_TIME * SEC_TO_mS) { + i_errno = IERCVTIMEOUT; + return -1; + } + test->settings->rcv_timeout.secs = rcv_timeout_in / SEC_TO_mS; + test->settings->rcv_timeout.usecs = (rcv_timeout_in % SEC_TO_mS) * mS_TO_US; + rcv_timeout_flag = 1; + break; +#if defined(HAVE_TCP_USER_TIMEOUT) + case OPT_SND_TIMEOUT: + test->settings->snd_timeout = atoi(optarg); + if (test->settings->snd_timeout < 0 || test->settings->snd_timeout > MAX_TIME * SEC_TO_mS) { + i_errno = IESNDTIMEOUT; + return -1; + } + snd_timeout_flag = 1; + break; +#endif /* HAVE_TCP_USER_TIMEOUT */ case 'A': #if defined(HAVE_CPU_AFFINITY) test->affinity = strtol(optarg, &endptr, 0); - if (endptr == optarg || + if (endptr == optarg || test->affinity < 0 || test->affinity > 1024) { i_errno = IEAFFINITY; return -1; @@ -1284,10 +1541,15 @@ iperf_parse_arguments(struct iperf_test *test, int arg break; case 'd': test->debug = 1; + test->debug_level = DEBUG_LEVEL_MAX; + if (optarg) { + test->debug_level = atoi(optarg); + if (test->debug_level < 0) + test->debug_level = DEBUG_LEVEL_MAX; + } break; case 'I': test->pidfile = strdup(optarg); - server_flag = 1; break; case OPT_LOGFILE: test->logfile = strdup(optarg); @@ -1321,6 +1583,12 @@ iperf_parse_arguments(struct iperf_test *test, int arg return -1; #endif break; +#if defined(HAVE_DONT_FRAGMENT) + case OPT_DONT_FRAGMENT: + test->settings->dont_fragment = 1; + client_flag = 1; + break; +#endif /* HAVE_DONT_FRAGMENT */ #if defined(HAVE_SSL) case OPT_CLIENT_USERNAME: client_username = strdup(optarg); @@ -1334,6 +1602,13 @@ iperf_parse_arguments(struct iperf_test *test, int arg case OPT_SERVER_AUTHORIZED_USERS: test->server_authorized_users = strdup(optarg); break; + case OPT_SERVER_SKEW_THRESHOLD: + test->server_skew_threshold = atoi(optarg); + if(test->server_skew_threshold <= 0){ + i_errno = IESKEWTHRESHOLD; + return -1; + } + break; #endif /* HAVE_SSL */ case OPT_PACING_TIMER: test->settings->pacing_timer = unit_atoi(optarg); @@ -1347,7 +1622,8 @@ iperf_parse_arguments(struct iperf_test *test, int arg usage_long(stdout); exit(0); default: - usage_long(stderr); + fprintf(stderr, "\n"); + usage(); exit(1); } } @@ -1367,7 +1643,7 @@ iperf_parse_arguments(struct iperf_test *test, int arg if (test->role == 's' && (client_username || client_rsa_public_key)){ i_errno = IECLIENTONLY; return -1; - } else if (test->role == 'c' && (client_username || client_rsa_public_key) && + } else if (test->role == 'c' && (client_username || client_rsa_public_key) && !(client_username && client_rsa_public_key)) { i_errno = IESETCLIENTAUTH; return -1; @@ -1375,16 +1651,17 @@ iperf_parse_arguments(struct iperf_test *test, int arg char *client_password = NULL; size_t s; + if (test_load_pubkey_from_file(client_rsa_public_key) < 0){ + iperf_err(test, "%s\n", ERR_error_string(ERR_get_error(), NULL)); + i_errno = IESETCLIENTAUTH; + return -1; + } /* Need to copy env var, so we can do a common free */ if ((client_password = getenv("IPERF3_PASSWORD")) != NULL) client_password = strdup(client_password); else if (iperf_getpass(&client_password, &s, stdin) < 0){ i_errno = IESETCLIENTAUTH; return -1; - } - if (test_load_pubkey_from_file(client_rsa_public_key) < 0){ - i_errno = IESETCLIENTAUTH; - return -1; } test->settings->client_username = client_username; @@ -1397,21 +1674,40 @@ iperf_parse_arguments(struct iperf_test *test, int arg if (test->role == 'c' && (server_rsa_private_key || test->server_authorized_users)){ i_errno = IESERVERONLY; return -1; - } else if (test->role == 's' && (server_rsa_private_key || test->server_authorized_users) && + } else if (test->role == 'c' && (test->server_skew_threshold != 0)){ + i_errno = IESERVERONLY; + return -1; + } else if (test->role == 'c' && rcv_timeout_flag && test->mode == SENDER){ + i_errno = IERVRSONLYRCVTIMEOUT; + return -1; + } else if (test->role == 's' && (server_rsa_private_key || test->server_authorized_users) && !(server_rsa_private_key && test->server_authorized_users)) { i_errno = IESETSERVERAUTH; return -1; } else if (test->role == 's' && server_rsa_private_key) { test->server_rsa_private_key = load_privkey_from_file(server_rsa_private_key); if (test->server_rsa_private_key == NULL){ + iperf_err(test, "%s\n", ERR_error_string(ERR_get_error(), NULL)); i_errno = IESETSERVERAUTH; return -1; } - free(server_rsa_private_key); - server_rsa_private_key = NULL; + free(server_rsa_private_key); + server_rsa_private_key = NULL; + + if(test->server_skew_threshold == 0){ + // Set default value for time skew threshold + test->server_skew_threshold=10; + } } #endif //HAVE_SSL + + // File cannot be transferred using UDP because of the UDP packets header (packet number, etc.) + if(test->role == 'c' && test->diskfile_name != (char*) 0 && test->protocol->id == Pudp) { + i_errno = IEUDPFILETRANSFER; + return -1; + } + if (blksize == 0) { if (test->protocol->id == Pudp) blksize = 0; /* try to dynamically determine from MSS */ @@ -1420,7 +1716,7 @@ iperf_parse_arguments(struct iperf_test *test, int arg else blksize = DEFAULT_TCP_BLKSIZE; } - if ((test->protocol->id != Pudp && blksize <= 0) + if ((test->protocol->id != Pudp && blksize <= 0) || blksize > MAX_BLOCKSIZE) { i_errno = IEBLOCKSIZE; return -1; @@ -1436,6 +1732,25 @@ iperf_parse_arguments(struct iperf_test *test, int arg if (!rate_flag) test->settings->rate = test->protocol->id == Pudp ? UDP_RATE : 0; + /* if no bytes or blocks specified, nor a duration_flag, and we have -F, + ** get the file-size as the bytes count to be transferred + */ + if (test->settings->bytes == 0 && + test->settings->blocks == 0 && + ! duration_flag && + test->diskfile_name != (char*) 0 && + test->role == 'c' + ){ + struct stat st; + if( stat(test->diskfile_name, &st) == 0 ){ + iperf_size_t file_bytes = st.st_size; + test->settings->bytes = file_bytes; + if (test->debug) + printf("End condition set to file-size: %"PRIu64" bytes\n", test->settings->bytes); + } + // if failing to read file stat, it should fallback to default duration mode + } + if ((test->settings->bytes != 0 || test->settings->blocks != 0) && ! duration_flag) test->duration = 0; @@ -1499,13 +1814,23 @@ int iperf_open_logfile(struct iperf_test *test) return 0; } +void iperf_close_logfile(struct iperf_test *test) +{ + if (test->outfile && test->outfile != stdout) { + fclose(test->outfile); + test->outfile = NULL; + } +} + int iperf_set_send_state(struct iperf_test *test, signed char state) { - test->state = state; - if (Nwrite(test->ctrl_sck, (char*) &state, sizeof(state), Ptcp) < 0) { - i_errno = IESENDMESSAGE; - return -1; + if (test->ctrl_sck >= 0) { + test->state = state; + if (Nwrite(test->ctrl_sck, (char*) &state, sizeof(state), Ptcp) < 0) { + i_errno = IESENDMESSAGE; + return -1; + } } return 0; } @@ -1517,7 +1842,7 @@ iperf_check_throttle(struct iperf_stream *sp, struct i double seconds; uint64_t bits_per_second; - if (sp->test->done || sp->test->settings->rate == 0 || sp->test->settings->burst != 0) + if (sp->test->done || sp->test->settings->rate == 0) return; iperf_time_diff(&sp->result->start_time_fixed, nowP, &temp_time); seconds = iperf_time_in_secs(&temp_time); @@ -1531,7 +1856,7 @@ iperf_check_throttle(struct iperf_stream *sp, struct i } } -/* Verify that average traffic is not greater than the specifid limit */ +/* Verify that average traffic is not greater than the specified limit */ void iperf_check_total_rate(struct iperf_test *test, iperf_size_t last_interval_bytes_transferred) { @@ -1542,8 +1867,8 @@ iperf_check_total_rate(struct iperf_test *test, iperf_ if (test->done || test->settings->bitrate_limit == 0) // Continue only if check should be done return; - - /* Add last inetrval's transffered bytes to the array */ + + /* Add last inetrval's transferred bytes to the array */ if (++test->bitrate_limit_last_interval_index >= test->settings->bitrate_limit_stats_per_interval) test->bitrate_limit_last_interval_index = 0; test->bitrate_limit_intervals_traffic_bytes[test->bitrate_limit_last_interval_index] = last_interval_bytes_transferred; @@ -1552,7 +1877,7 @@ iperf_check_total_rate(struct iperf_test *test, iperf_ test->bitrate_limit_stats_count += 1; if (test->bitrate_limit_stats_count < test->settings->bitrate_limit_stats_per_interval) return; - + /* Calculating total bytes traffic to be averaged */ for (total_bytes = 0, i = 0; i < test->settings->bitrate_limit_stats_per_interval; i++) { total_bytes += test->bitrate_limit_intervals_traffic_bytes[i]; @@ -1565,7 +1890,8 @@ iperf_check_total_rate(struct iperf_test *test, iperf_ } if (bits_per_second > test->settings->bitrate_limit) { - iperf_err(test, "Total throughput of %" PRIu64 " bps exceeded %" PRIu64 " bps limit", bits_per_second, test->settings->bitrate_limit); + if (iperf_get_verbose(test)) + iperf_err(test, "Total throughput of %" PRIu64 " bps exceeded %" PRIu64 " bps limit", bits_per_second, test->settings->bitrate_limit); test->bitrate_limit_exceeded = 1; } } @@ -1576,6 +1902,7 @@ iperf_send(struct iperf_test *test, fd_set *write_setP register int multisend, r, streams_active; register struct iperf_stream *sp; struct iperf_time now; + int no_throttle_check; /* Can we do multisend mode? */ if (test->settings->burst != 0) @@ -1585,13 +1912,20 @@ iperf_send(struct iperf_test *test, fd_set *write_setP else multisend = 1; /* nope */ + /* Should bitrate throttle be checked for every send */ + no_throttle_check = test->settings->rate != 0 && test->settings->burst == 0; + for (; multisend > 0; --multisend) { - if (test->settings->rate != 0 && test->settings->burst == 0) + if (no_throttle_check) iperf_time_now(&now); streams_active = 0; SLIST_FOREACH(sp, &test->streams, streams) { if ((sp->green_light && sp->sender && (write_setP == NULL || FD_ISSET(sp->socket, write_setP)))) { + if (multisend > 1 && test->settings->bytes != 0 && test->bytes_sent >= test->settings->bytes) + break; + if (multisend > 1 && test->settings->blocks != 0 && test->blocks_sent >= test->settings->blocks) + break; if ((r = sp->snd(sp)) < 0) { if (r == NET_SOFTERROR) break; @@ -1600,18 +1934,16 @@ iperf_send(struct iperf_test *test, fd_set *write_setP } streams_active = 1; test->bytes_sent += r; - ++test->blocks_sent; - iperf_check_throttle(sp, &now); - if (multisend > 1 && test->settings->bytes != 0 && test->bytes_sent >= test->settings->bytes) - break; - if (multisend > 1 && test->settings->blocks != 0 && test->blocks_sent >= test->settings->blocks) - break; + if (!sp->pending_size) + ++test->blocks_sent; + if (no_throttle_check) + iperf_check_throttle(sp, &now); } } if (!streams_active) break; } - if (test->settings->burst != 0) { + if (!no_throttle_check) { /* Throttle check if was not checked for each send */ iperf_time_now(&now); SLIST_FOREACH(sp, &test->streams, streams) if (sp->sender) @@ -1722,14 +2054,18 @@ int test_is_authorized(struct iperf_test *test){ if (rc) { return -1; } - int ret = check_authentication(username, password, ts, test->server_authorized_users); + int ret = check_authentication(username, password, ts, test->server_authorized_users, test->server_skew_threshold); if (ret == 0){ - iperf_printf(test, report_authentication_succeeded, username, ts); + if (test->debug) { + iperf_printf(test, report_authentication_succeeded, username, ts); + } free(username); free(password); return 0; } else { - iperf_printf(test, report_authentication_failed, username, ts); + if (test->debug) { + iperf_printf(test, report_authentication_failed, ret, username, ts); + } free(username); free(password); return -1; @@ -1849,10 +2185,8 @@ send_parameters(struct iperf_test *test) if (test->server_affinity != -1) cJSON_AddNumberToObject(j, "server_affinity", test->server_affinity); cJSON_AddNumberToObject(j, "time", test->duration); - if (test->settings->bytes) - cJSON_AddNumberToObject(j, "num", test->settings->bytes); - if (test->settings->blocks) - cJSON_AddNumberToObject(j, "blockcount", test->settings->blocks); + cJSON_AddNumberToObject(j, "num", test->settings->bytes); + cJSON_AddNumberToObject(j, "blockcount", test->settings->blocks); if (test->settings->mss) cJSON_AddNumberToObject(j, "MSS", test->settings->mss); if (test->no_delay) @@ -1892,6 +2226,12 @@ send_parameters(struct iperf_test *test) cJSON_AddNumberToObject(j, "udp_counters_64bit", iperf_get_test_udp_counters_64bit(test)); if (test->repeating_payload) cJSON_AddNumberToObject(j, "repeating_payload", test->repeating_payload); + if (test->zerocopy) + cJSON_AddNumberToObject(j, "zerocopy", test->zerocopy); +#if defined(HAVE_DONT_FRAGMENT) + if (test->settings->dont_fragment) + cJSON_AddNumberToObject(j, "dont_fragment", test->settings->dont_fragment); +#endif /* HAVE_DONT_FRAGMENT */ #if defined(HAVE_SSL) /* Send authentication parameters */ if (test->settings->client_username && test->settings->client_password && test->settings->client_rsa_pubkey){ @@ -1902,7 +2242,7 @@ send_parameters(struct iperf_test *test) i_errno = IESENDPARAMS; return -1; } - + cJSON_AddStringToObject(j, "authtoken", test->settings->authtoken); } #endif // HAVE_SSL @@ -1956,8 +2296,10 @@ get_parameters(struct iperf_test *test) test->server_affinity = j_p->valueint; if ((j_p = cJSON_GetObjectItem(j, "time")) != NULL) test->duration = j_p->valueint; + test->settings->bytes = 0; if ((j_p = cJSON_GetObjectItem(j, "num")) != NULL) test->settings->bytes = j_p->valueint; + test->settings->blocks = 0; if ((j_p = cJSON_GetObjectItem(j, "blockcount")) != NULL) test->settings->blocks = j_p->valueint; if ((j_p = cJSON_GetObjectItem(j, "MSS")) != NULL) @@ -2000,6 +2342,12 @@ get_parameters(struct iperf_test *test) iperf_set_test_udp_counters_64bit(test, 1); if ((j_p = cJSON_GetObjectItem(j, "repeating_payload")) != NULL) test->repeating_payload = 1; + if ((j_p = cJSON_GetObjectItem(j, "zerocopy")) != NULL) + test->zerocopy = j_p->valueint; +#if defined(HAVE_DONT_FRAGMENT) + if ((j_p = cJSON_GetObjectItem(j, "dont_fragment")) != NULL) + test->settings->dont_fragment = j_p->valueint; +#endif /* HAVE_DONT_FRAGMENT */ #if defined(HAVE_SSL) if ((j_p = cJSON_GetObjectItem(j, "authtoken")) != NULL) test->settings->authtoken = strdup(j_p->valuestring); @@ -2094,7 +2442,9 @@ send_results(struct iperf_test *test) cJSON_AddNumberToObject(j_stream, "retransmits", retransmits); cJSON_AddNumberToObject(j_stream, "jitter", sp->jitter); cJSON_AddNumberToObject(j_stream, "errors", sp->cnt_error); + cJSON_AddNumberToObject(j_stream, "omitted_errors", sp->omitted_cnt_error); cJSON_AddNumberToObject(j_stream, "packets", sp->packet_count); + cJSON_AddNumberToObject(j_stream, "omitted_packets", sp->omitted_packet_count); iperf_time_diff(&sp->result->start_time, &sp->result->start_time, &temp_time); start_time = iperf_time_in_secs(&temp_time); @@ -2141,10 +2491,13 @@ get_results(struct iperf_test *test) cJSON *j_retransmits; cJSON *j_jitter; cJSON *j_errors; + cJSON *j_omitted_errors; cJSON *j_packets; + cJSON *j_omitted_packets; cJSON *j_server_output; cJSON *j_start_time, *j_end_time; - int sid, cerror, pcount; + int sid; + int64_t cerror, pcount, omitted_cerror, omitted_pcount; double jitter; iperf_size_t bytes_transferred; int retransmits; @@ -2197,12 +2550,18 @@ get_results(struct iperf_test *test) j_retransmits = cJSON_GetObjectItem(j_stream, "retransmits"); j_jitter = cJSON_GetObjectItem(j_stream, "jitter"); j_errors = cJSON_GetObjectItem(j_stream, "errors"); + j_omitted_errors = cJSON_GetObjectItem(j_stream, "omitted_errors"); j_packets = cJSON_GetObjectItem(j_stream, "packets"); + j_omitted_packets = cJSON_GetObjectItem(j_stream, "omitted_packets"); j_start_time = cJSON_GetObjectItem(j_stream, "start_time"); j_end_time = cJSON_GetObjectItem(j_stream, "end_time"); if (j_id == NULL || j_bytes == NULL || j_retransmits == NULL || j_jitter == NULL || j_errors == NULL || j_packets == NULL) { i_errno = IERECVRESULTS; r = -1; + } else if ( (j_omitted_errors == NULL && j_omitted_packets != NULL) || (j_omitted_errors != NULL && j_omitted_packets == NULL) ) { + /* For backward compatibility allow to not receive "omitted" statistcs */ + i_errno = IERECVRESULTS; + r = -1; } else { sid = j_id->valueint; bytes_transferred = j_bytes->valueint; @@ -2210,6 +2569,10 @@ get_results(struct iperf_test *test) jitter = j_jitter->valuedouble; cerror = j_errors->valueint; pcount = j_packets->valueint; + if (j_omitted_packets != NULL) { + omitted_cerror = j_omitted_errors->valueint; + omitted_pcount = j_omitted_packets->valueint; + } SLIST_FOREACH(sp, &test->streams, streams) if (sp->id == sid) break; if (sp == NULL) { @@ -2221,8 +2584,20 @@ get_results(struct iperf_test *test) sp->cnt_error = cerror; sp->peer_packet_count = pcount; sp->result->bytes_received = bytes_transferred; + if (j_omitted_packets != NULL) { + sp->omitted_cnt_error = omitted_cerror; + sp->peer_omitted_packet_count = omitted_pcount; + } else { + sp->peer_omitted_packet_count = sp->omitted_packet_count; + if (sp->peer_omitted_packet_count > 0) { + /* -1 indicates unknown error count since it includes the omitted count */ + sp->omitted_cnt_error = (sp->cnt_error > 0) ? -1 : 0; + } else { + sp->omitted_cnt_error = sp->cnt_error; + } + } /* - * We have to handle the possibilty that + * We have to handle the possibility that * start_time and end_time might not be * available; this is the case for older (pre-3.2) * servers. @@ -2240,6 +2615,11 @@ get_results(struct iperf_test *test) sp->peer_packet_count = pcount; sp->result->bytes_sent = bytes_transferred; sp->result->stream_retrans = retransmits; + if (j_omitted_packets != NULL) { + sp->peer_omitted_packet_count = omitted_pcount; + } else { + sp->peer_omitted_packet_count = sp->peer_packet_count; + } if (j_start_time && j_end_time) { sp->result->sender_time = j_end_time->valuedouble - j_start_time->valuedouble; } @@ -2314,6 +2694,7 @@ static cJSON * JSON_read(int fd) { uint32_t hsize, nsize; + size_t strsize; char *str; cJSON *json = NULL; int rc; @@ -2326,7 +2707,9 @@ JSON_read(int fd) if (Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp) >= 0) { hsize = ntohl(nsize); /* Allocate a buffer to hold the JSON */ - str = (char *) calloc(sizeof(char), hsize+1); /* +1 for trailing null */ + strsize = hsize + 1; /* +1 for trailing NULL */ + if (strsize) { + str = (char *) calloc(sizeof(char), strsize); if (str != NULL) { rc = Nread(fd, str, hsize, Ptcp); if (rc >= 0) { @@ -2345,6 +2728,10 @@ JSON_read(int fd) } } free(str); + } + else { + printf("WARNING: Data length overflow\n"); + } } return json; } @@ -2427,11 +2814,12 @@ iperf_new_test() test->bitrate_limit_intervals_traffic_bytes = (iperf_size_t *) malloc(sizeof(iperf_size_t) * MAX_INTERVAL); if (!test->bitrate_limit_intervals_traffic_bytes) { + free(test->settings); free(test); i_errno = IENEWTEST; return NULL; } - memset(test->bitrate_limit_intervals_traffic_bytes, 0, sizeof(sizeof(iperf_size_t) * MAX_INTERVAL)); + memset(test->bitrate_limit_intervals_traffic_bytes, 0, sizeof(sizeof(iperf_size_t) * MAX_INTERVAL)); /* By default all output goes to stdout */ test->outfile = stdout; @@ -2458,7 +2846,7 @@ protocol_new(void) void protocol_free(struct protocol *proto) { - free(proto); + free(proto); } /**************************************************************************/ @@ -2486,6 +2874,7 @@ iperf_defaults(struct iperf_test *testp) testp->remote_congestion_used = NULL; testp->server_port = PORT; testp->ctrl_sck = -1; + testp->listener = -1; testp->prot_listener = -1; testp->other_side_has_retransmits = 0; @@ -2504,12 +2893,16 @@ iperf_defaults(struct iperf_test *testp) testp->settings->bitrate_limit_interval = 5; testp->settings->bitrate_limit_stats_per_interval = 0; testp->settings->fqrate = 0; - testp->settings->pacing_timer = 1000; + testp->settings->pacing_timer = DEFAULT_PACING_TIMER; testp->settings->burst = 0; testp->settings->mss = 0; testp->settings->bytes = 0; testp->settings->blocks = 0; testp->settings->connect_timeout = -1; + testp->settings->rcv_timeout.secs = DEFAULT_NO_MSG_RCVD_TIMEOUT / SEC_TO_mS; + testp->settings->rcv_timeout.usecs = (DEFAULT_NO_MSG_RCVD_TIMEOUT % SEC_TO_mS) * mS_TO_US; + testp->zerocopy = 0; + memset(testp->cookie, 0, COOKIE_SIZE); testp->multisend = 10; /* arbitrary */ @@ -2600,6 +2993,8 @@ iperf_free_test(struct iperf_test *test) free(test->tmp_template); if (test->bind_address) free(test->bind_address); + if (test->bind_dev) + free(test->bind_dev); if (!TAILQ_EMPTY(&test->xbind_addrs)) { struct xbind_entry *xbe; @@ -2658,17 +3053,14 @@ iperf_free_test(struct iperf_test *test) /* Free protocol list */ while (!SLIST_EMPTY(&test->protocols)) { prot = SLIST_FIRST(&test->protocols); - SLIST_REMOVE_HEAD(&test->protocols, protocols); + SLIST_REMOVE_HEAD(&test->protocols, protocols); free(prot); } if (test->logfile) { free(test->logfile); test->logfile = NULL; - if (test->outfile) { - fclose(test->outfile); - test->outfile = NULL; - } + iperf_close_logfile(test); } if (test->server_output_text) { @@ -2702,7 +3094,7 @@ iperf_free_test(struct iperf_test *test) } } - /* Free interval's traffic array for avrage rate calculations */ + /* Free interval's traffic array for average rate calculations */ if (test->bitrate_limit_intervals_traffic_bytes != NULL) free(test->bitrate_limit_intervals_traffic_bytes); @@ -2720,6 +3112,8 @@ iperf_reset_test(struct iperf_test *test) struct iperf_stream *sp; int i; + iperf_close_logfile(test); + /* Free streams */ while (!SLIST_EMPTY(&test->streams)) { sp = SLIST_FIRST(&test->streams); @@ -2760,8 +3154,9 @@ iperf_reset_test(struct iperf_test *test) CPU_ZERO(&test->cpumask); #endif /* HAVE_CPUSET_SETAFFINITY */ test->state = 0; - + test->ctrl_sck = -1; + test->listener = -1; test->prot_listener = -1; test->bytes_sent = 0; @@ -2785,7 +3180,7 @@ iperf_reset_test(struct iperf_test *test) FD_ZERO(&test->read_set); FD_ZERO(&test->write_set); - + test->num_streams = 1; test->settings->socket_bufsize = 0; test->settings->blksize = DEFAULT_TCP_BLKSIZE; @@ -2793,6 +3188,8 @@ iperf_reset_test(struct iperf_test *test) test->settings->burst = 0; test->settings->mss = 0; test->settings->tos = 0; + test->settings->dont_fragment = 0; + test->zerocopy = 0; #if defined(HAVE_SSL) if (test->settings->authtoken) { @@ -2891,7 +3288,7 @@ iperf_stats_callback(struct iperf_test *test) // Total bytes transferred this interval total_interval_bytes_transferred += rp->bytes_sent_this_interval + rp->bytes_received_this_interval; - + irp = TAILQ_LAST(&rp->interval_results, irlisthead); /* result->end_time contains timestamp of previous interval */ if ( irp != NULL ) /* not the 1st interval */ @@ -2916,7 +3313,12 @@ iperf_stats_callback(struct iperf_test *test) if (temp.snd_cwnd > rp->stream_max_snd_cwnd) { rp->stream_max_snd_cwnd = temp.snd_cwnd; } - + + temp.snd_wnd = get_snd_wnd(&temp); + if (temp.snd_wnd > rp->stream_max_snd_wnd) { + rp->stream_max_snd_wnd = temp.snd_wnd; + } + temp.rtt = get_rtt(&temp); if (temp.rtt > rp->stream_max_rtt) { rp->stream_max_rtt = temp.rtt; @@ -3000,7 +3402,7 @@ iperf_print_intermediate(struct iperf_test *test) /* * If the interval is at least 10% the normal interval - * length, or if there were actual bytes transferrred, + * length, or if there were actual bytes transferred, * then we want to keep this interval. */ if (interval_len >= test->stats_interval * 0.10 || @@ -3067,10 +3469,12 @@ iperf_print_intermediate(struct iperf_test *test) int retransmits = 0; double start_time, end_time; - int total_packets = 0, lost_packets = 0; + int64_t total_packets = 0, lost_packets = 0; double avg_jitter = 0.0, lost_percent; int stream_must_be_sender = current_mode * current_mode; + char *sum_name; + /* Print stream role just for bidirectional mode. */ if (test->mode == BIDIRECTIONAL) { @@ -3105,6 +3509,22 @@ iperf_print_intermediate(struct iperf_test *test) /* next build string with sum of all streams */ if (test->num_streams > 1 || test->json_output) { + /* + * With BIDIR give a different JSON object name to the one sent/receive sums. + * The different name is given to the data sent from the server, which is + * the "reverse" channel. This makes sure that the name reported on the server + * and client are compatible, and the names are the same as with non-bidir, + * except for when reverse is used. + */ + sum_name = "sum"; + if (test->mode == BIDIRECTIONAL) { + if ((test->role == 'c' && !stream_must_be_sender) || + (test->role != 'c' && stream_must_be_sender)) + { + sum_name = "sum_bidir_reverse"; + } + } + sp = SLIST_FIRST(&test->streams); /* reset back to 1st stream */ /* Only do this of course if there was a first stream */ if (sp) { @@ -3122,13 +3542,13 @@ iperf_print_intermediate(struct iperf_test *test) if (test->sender_has_retransmits == 1 && stream_must_be_sender) { /* Interval sum, TCP with retransmits. */ if (test->json_output) - cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (int64_t) retransmits, irp->omitted, stream_must_be_sender)); /* XXX irp->omitted or test->omitting? */ + cJSON_AddItemToObject(json_interval, sum_name, iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (int64_t) retransmits, irp->omitted, stream_must_be_sender)); /* XXX irp->omitted or test->omitting? */ else iperf_printf(test, report_sum_bw_retrans_format, mbuf, start_time, end_time, ubuf, nbuf, retransmits, irp->omitted?report_omitted:""); /* XXX irp->omitted or test->omitting? */ } else { /* Interval sum, TCP without retransmits. */ if (test->json_output) - cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, test->omitting, stream_must_be_sender)); + cJSON_AddItemToObject(json_interval, sum_name, iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, test->omitting, stream_must_be_sender)); else iperf_printf(test, report_sum_bw_format, mbuf, start_time, end_time, ubuf, nbuf, test->omitting?report_omitted:""); } @@ -3136,7 +3556,7 @@ iperf_print_intermediate(struct iperf_test *test) /* Interval sum, UDP. */ if (stream_must_be_sender) { if (test->json_output) - cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f packets: %d omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (int64_t) total_packets, test->omitting, stream_must_be_sender)); + cJSON_AddItemToObject(json_interval, sum_name, iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f packets: %d omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (int64_t) total_packets, test->omitting, stream_must_be_sender)); else iperf_printf(test, report_sum_bw_udp_sender_format, mbuf, start_time, end_time, ubuf, nbuf, zbuf, total_packets, test->omitting?report_omitted:""); } else { @@ -3148,7 +3568,7 @@ iperf_print_intermediate(struct iperf_test *test) lost_percent = 0.0; } if (test->json_output) - cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) total_packets, (double) lost_percent, test->omitting, stream_must_be_sender)); + cJSON_AddItemToObject(json_interval, sum_name, iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) total_packets, (double) lost_percent, test->omitting, stream_must_be_sender)); else iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, end_time, ubuf, nbuf, avg_jitter * 1000.0, lost_packets, total_packets, lost_percent, test->omitting?report_omitted:""); } @@ -3170,6 +3590,8 @@ iperf_print_results(struct iperf_test *test) int lower_mode, upper_mode; int current_mode; + char *sum_sent_name, *sum_received_name, *sum_name; + int tmp_sender_has_retransmits = test->sender_has_retransmits; /* print final summary for all intervals */ @@ -3229,10 +3651,11 @@ iperf_print_results(struct iperf_test *test) for (current_mode = lower_mode; current_mode <= upper_mode; ++current_mode) { cJSON *json_summary_stream = NULL; - int total_retransmits = 0; - int total_packets = 0, lost_packets = 0; - int sender_packet_count = 0, receiver_packet_count = 0; /* for this stream, this interval */ - int sender_total_packets = 0, receiver_total_packets = 0; /* running total */ + int64_t total_retransmits = 0; + int64_t total_packets = 0, lost_packets = 0; + int64_t sender_packet_count = 0, receiver_packet_count = 0; /* for this stream, this interval */ + int64_t sender_omitted_packet_count = 0, receiver_omitted_packet_count = 0; /* for this stream, this interval */ + int64_t sender_total_packets = 0, receiver_total_packets = 0; /* running total */ char ubuf[UNIT_LEN]; char nbuf[UNIT_LEN]; struct stat sb; @@ -3242,7 +3665,7 @@ iperf_print_results(struct iperf_test *test) iperf_size_t bytes_received, total_received = 0; double start_time, end_time = 0.0, avg_jitter = 0.0, lost_percent = 0.0; double sender_time = 0.0, receiver_time = 0.0; - struct iperf_time temp_time; + struct iperf_time temp_time; double bandwidth; char mbuf[UNIT_LEN]; @@ -3272,7 +3695,7 @@ iperf_print_results(struct iperf_test *test) * the streams. It's possible to not have any streams at all * if the client got interrupted before it got to do anything. * - * Also note that we try to keep seperate values for the sender + * Also note that we try to keep separate values for the sender * and receiver ending times. Earlier iperf (3.1 and earlier) * servers didn't send that to the clients, so in this case we fall * back to using the client's ending timestamp. The fallback is @@ -3280,8 +3703,8 @@ iperf_print_results(struct iperf_test *test) */ if (sp) { - iperf_time_diff(&sp->result->start_time, &sp->result->end_time, &temp_time); - end_time = iperf_time_in_secs(&temp_time); + iperf_time_diff(&sp->result->start_time, &sp->result->end_time, &temp_time); + end_time = iperf_time_in_secs(&temp_time); if (sp->sender) { sp->result->sender_time = end_time; if (sp->result->receiver_time == 0.0) { @@ -3312,11 +3735,15 @@ iperf_print_results(struct iperf_test *test) if (sp->sender) { sender_packet_count = sp->packet_count; + sender_omitted_packet_count = sp->omitted_packet_count; receiver_packet_count = sp->peer_packet_count; + receiver_omitted_packet_count = sp->peer_omitted_packet_count; } else { sender_packet_count = sp->peer_packet_count; + sender_omitted_packet_count = sp->peer_omitted_packet_count; receiver_packet_count = sp->packet_count; + receiver_omitted_packet_count = sp->omitted_packet_count; } if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { @@ -3328,11 +3755,13 @@ iperf_print_results(struct iperf_test *test) * Running total of the total number of packets. Use the sender packet count if we * have it, otherwise use the receiver packet count. */ - int packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count; + int64_t packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count; total_packets += (packet_count - sp->omitted_packet_count); - sender_total_packets += (sender_packet_count - sp->omitted_packet_count); - receiver_total_packets += (receiver_packet_count - sp->omitted_packet_count); - lost_packets += (sp->cnt_error - sp->omitted_cnt_error); + sender_total_packets += (sender_packet_count - sender_omitted_packet_count); + receiver_total_packets += (receiver_packet_count - receiver_omitted_packet_count); + lost_packets += sp->cnt_error; + if (sp->omitted_cnt_error > -1) + lost_packets -= sp->omitted_cnt_error; avg_jitter += sp->jitter; } @@ -3348,7 +3777,7 @@ iperf_print_results(struct iperf_test *test) if (test->sender_has_retransmits) { /* Sender summary, TCP and SCTP with retransmits. */ if (test->json_output) - cJSON_AddItemToObject(json_summary_stream, "sender", iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d max_snd_cwnd: %d max_rtt: %d min_rtt: %d mean_rtt: %d sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8, (int64_t) sp->result->stream_retrans, (int64_t) sp->result->stream_max_snd_cwnd, (int64_t) sp->result->stream_max_rtt, (int64_t) sp->result->stream_min_rtt, (int64_t) ((sp->result->stream_count_rtt == 0) ? 0 : sp->result->stream_sum_rtt / sp->result->stream_count_rtt), stream_must_be_sender)); + cJSON_AddItemToObject(json_summary_stream, report_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_snd_wnd: %d max_rtt: %d min_rtt: %d mean_rtt: %d sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8, (int64_t) sp->result->stream_retrans, (int64_t) sp->result->stream_max_snd_cwnd, (int64_t) sp->result->stream_max_snd_wnd, (int64_t) sp->result->stream_max_rtt, (int64_t) sp->result->stream_min_rtt, (int64_t) ((sp->result->stream_count_rtt == 0) ? 0 : sp->result->stream_sum_rtt / sp->result->stream_count_rtt), stream_must_be_sender)); else if (test->role == 's' && !sp->sender) { if (test->verbose) @@ -3360,7 +3789,7 @@ iperf_print_results(struct iperf_test *test) } else { /* Sender summary, TCP and SCTP without retransmits. */ if (test->json_output) - cJSON_AddItemToObject(json_summary_stream, "sender", iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8, stream_must_be_sender)); + cJSON_AddItemToObject(json_summary_stream, report_sender, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8, stream_must_be_sender)); else if (test->role == 's' && !sp->sender) { if (test->verbose) @@ -3372,15 +3801,15 @@ iperf_print_results(struct iperf_test *test) } } else { /* Sender summary, UDP. */ - if (sender_packet_count - sp->omitted_packet_count > 0) { - lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (sender_packet_count - sp->omitted_packet_count); + if (sender_packet_count - sender_omitted_packet_count > 0) { + lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (sender_packet_count - sender_omitted_packet_count); } else { lost_percent = 0.0; } if (test->json_output) { /* - * For hysterical raisins, we only emit one JSON + * For historical reasons, we only emit one JSON * object for the UDP summary, and it contains * information for both the sender and receiver * side. @@ -3395,7 +3824,7 @@ iperf_print_results(struct iperf_test *test) * is the case, then use the receiver's count of packets * instead. */ - int packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count; + int64_t packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count; cJSON_AddItemToObject(json_summary_stream, "udp", iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f out_of_order: %d sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8, (double) sp->jitter * 1000.0, (int64_t) (sp->cnt_error - sp->omitted_cnt_error), (int64_t) (packet_count - sp->omitted_packet_count), (double) lost_percent, (int64_t) (sp->outoforder_packets - sp->omitted_outoforder_packets), stream_must_be_sender)); } else { @@ -3411,7 +3840,7 @@ iperf_print_results(struct iperf_test *test) iperf_printf(test, report_sender_not_available_format, sp->socket); } else { - iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, 0.0, 0, (sender_packet_count - sp->omitted_packet_count), (double) 0, report_sender); + iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, 0.0, (int64_t) 0, (sender_packet_count - sender_omitted_packet_count), (double) 0, report_sender); } if ((sp->outoforder_packets - sp->omitted_outoforder_packets) > 0) iperf_printf(test, report_sum_outoforder, mbuf, start_time, sender_time, (sp->outoforder_packets - sp->omitted_outoforder_packets)); @@ -3451,7 +3880,7 @@ iperf_print_results(struct iperf_test *test) if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { /* Receiver summary, TCP and SCTP */ if (test->json_output) - cJSON_AddItemToObject(json_summary_stream, "receiver", iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (int64_t) sp->socket, (double) start_time, (double) receiver_time, (double) end_time, (int64_t) bytes_received, bandwidth * 8, stream_must_be_sender)); + cJSON_AddItemToObject(json_summary_stream, report_receiver, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (int64_t) sp->socket, (double) start_time, (double) receiver_time, (double) end_time, (int64_t) bytes_received, bandwidth * 8, stream_must_be_sender)); else if (test->role == 's' && sp->sender) { if (test->verbose) @@ -3468,8 +3897,8 @@ iperf_print_results(struct iperf_test *test) * data here. */ if (! test->json_output) { - if (receiver_packet_count - sp->omitted_packet_count > 0) { - lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (receiver_packet_count - sp->omitted_packet_count); + if (receiver_packet_count - receiver_omitted_packet_count > 0 && sp->omitted_cnt_error > -1) { + lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (receiver_packet_count - receiver_omitted_packet_count); } else { lost_percent = 0.0; @@ -3480,7 +3909,11 @@ iperf_print_results(struct iperf_test *test) iperf_printf(test, report_receiver_not_available_format, sp->socket); } else { - iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, start_time, receiver_time, ubuf, nbuf, sp->jitter * 1000.0, (sp->cnt_error - sp->omitted_cnt_error), (receiver_packet_count - sp->omitted_packet_count), lost_percent, report_receiver); + if (sp->omitted_cnt_error > -1) { + iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, start_time, receiver_time, ubuf, nbuf, sp->jitter * 1000.0, (sp->cnt_error - sp->omitted_cnt_error), (receiver_packet_count - receiver_omitted_packet_count), lost_percent, report_receiver); + } else { + iperf_printf(test, report_bw_udp_format_no_omitted_error, sp->socket, mbuf, start_time, receiver_time, ubuf, nbuf, sp->jitter * 1000.0, (receiver_packet_count - receiver_omitted_packet_count), report_receiver); + } } } } @@ -3489,6 +3922,27 @@ iperf_print_results(struct iperf_test *test) } if (test->num_streams > 1 || test->json_output) { + /* + * With BIDIR give a different JSON object name to the one sent/receive sums. + * The different name is given to the data sent from the server, which is + * the "reverse" channel. This makes sure that the name reported on the server + * and client are compatible, and the names are the same as with non-bidir, + * except for when reverse is used. + */ + sum_name = "sum"; + sum_sent_name = "sum_sent"; + sum_received_name = "sum_received"; + if (test->mode == BIDIRECTIONAL) { + if ((test->role == 'c' && !stream_must_be_sender) || + (test->role != 'c' && stream_must_be_sender)) + { + sum_name = "sum_bidir_reverse"; + sum_sent_name = "sum_sent_bidir_reverse"; + sum_received_name = "sum_received_bidir_reverse"; + } + + } + unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A'); /* If no tests were run, arbitrarily set bandwidth to 0. */ if (sender_time > 0.0) { @@ -3502,7 +3956,7 @@ iperf_print_results(struct iperf_test *test) if (test->sender_has_retransmits) { /* Summary sum, TCP with retransmits. */ if (test->json_output) - cJSON_AddItemToObject(test->json_end, "sum_sent", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d sender: %b", (double) start_time, (double) sender_time, (double) sender_time, (int64_t) total_sent, bandwidth * 8, (int64_t) total_retransmits, stream_must_be_sender)); + cJSON_AddItemToObject(test->json_end, sum_sent_name, iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d sender: %b", (double) start_time, (double) sender_time, (double) sender_time, (int64_t) total_sent, bandwidth * 8, (int64_t) total_retransmits, stream_must_be_sender)); else if (test->role == 's' && !stream_must_be_sender) { if (test->verbose) @@ -3514,7 +3968,7 @@ iperf_print_results(struct iperf_test *test) } else { /* Summary sum, TCP without retransmits. */ if (test->json_output) - cJSON_AddItemToObject(test->json_end, "sum_sent", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (double) start_time, (double) sender_time, (double) sender_time, (int64_t) total_sent, bandwidth * 8, stream_must_be_sender)); + cJSON_AddItemToObject(test->json_end, sum_sent_name, iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (double) start_time, (double) sender_time, (double) sender_time, (int64_t) total_sent, bandwidth * 8, stream_must_be_sender)); else if (test->role == 's' && !stream_must_be_sender) { if (test->verbose) @@ -3534,7 +3988,7 @@ iperf_print_results(struct iperf_test *test) } unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); if (test->json_output) - cJSON_AddItemToObject(test->json_end, "sum_received", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (double) start_time, (double) receiver_time, (double) receiver_time, (int64_t) total_received, bandwidth * 8, stream_must_be_sender)); + cJSON_AddItemToObject(test->json_end, sum_received_name, iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f sender: %b", (double) start_time, (double) receiver_time, (double) receiver_time, (int64_t) total_received, bandwidth * 8, stream_must_be_sender)); else if (test->role == 's' && stream_must_be_sender) { if (test->verbose) @@ -3553,17 +4007,29 @@ iperf_print_results(struct iperf_test *test) else { lost_percent = 0.0; } - if (test->json_output) - cJSON_AddItemToObject(test->json_end, "sum", iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f sender: %b", (double) start_time, (double) receiver_time, (double) receiver_time, (int64_t) total_sent, bandwidth * 8, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) total_packets, (double) lost_percent, stream_must_be_sender)); - else { + if (test->json_output) { /* + * Original, summary structure. Using this + * structure is not recommended due to + * ambiguities between the sender and receiver. + */ + cJSON_AddItemToObject(test->json_end, sum_name, iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f sender: %b", (double) start_time, (double) receiver_time, (double) receiver_time, (int64_t) total_sent, bandwidth * 8, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) total_packets, (double) lost_percent, stream_must_be_sender)); + /* + * Separate sum_sent and sum_received structures. + * Using these structures to get the most complete + * information about UDP transfer. + */ + cJSON_AddItemToObject(test->json_end, sum_sent_name, iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f sender: %b", (double) start_time, (double) sender_time, (double) sender_time, (int64_t) total_sent, (double) total_sent * 8 / sender_time, (double) 0.0, (int64_t) 0, (int64_t) sender_total_packets, (double) 0.0, 1)); + cJSON_AddItemToObject(test->json_end, sum_received_name, iperf_json_printf("start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f sender: %b", (double) start_time, (double) receiver_time, (double) receiver_time, (int64_t) total_received, (double) total_received * 8 / receiver_time, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) receiver_total_packets, (double) lost_percent, 0)); + } else { + /* * On the client we have both sender and receiver overall summary * stats. On the server we have only the side that was on the * server. Output whatever we have. */ if (! (test->role == 's' && !stream_must_be_sender) ) { unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A'); - iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, sender_time, ubuf, nbuf, 0.0, 0, sender_total_packets, 0.0, "sender"); + iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, sender_time, ubuf, nbuf, 0.0, (int64_t) 0, sender_total_packets, 0.0, report_sender); } if (! (test->role == 's' && stream_must_be_sender) ) { @@ -3576,7 +4042,7 @@ iperf_print_results(struct iperf_test *test) bandwidth = 0.0; } unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); - iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, receiver_time, ubuf, nbuf, avg_jitter * 1000.0, lost_packets, receiver_total_packets, lost_percent, "receiver"); + iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, receiver_time, ubuf, nbuf, avg_jitter * 1000.0, lost_packets, receiver_total_packets, lost_percent, report_receiver); } } } @@ -3656,8 +4122,8 @@ iperf_print_results(struct iperf_test *test) /** * Main report-printing callback. - * Prints results either during a test (interval report only) or - * after the entire test has been run (last interval report plus + * Prints results either during a test (interval report only) or + * after the entire test has been run (last interval report plus * overall summary). */ void @@ -3674,7 +4140,7 @@ iperf_reporter_callback(struct iperf_test *test) iperf_print_intermediate(test); iperf_print_results(test); break; - } + } } @@ -3752,17 +4218,17 @@ print_interval_results(struct iperf_test *test, struct bandwidth = 0.0; } unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format); - + iperf_time_diff(&sp->result->start_time, &irp->interval_start_time, &temp_time); st = iperf_time_in_secs(&temp_time); iperf_time_diff(&sp->result->start_time, &irp->interval_end_time, &temp_time); et = iperf_time_in_secs(&temp_time); - + if (test->protocol->id == Ptcp || test->protocol->id == Psctp) { if (test->sender_has_retransmits == 1 && sp->sender) { /* Interval, TCP with retransmits. */ if (test->json_output) - cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f retransmits: %d snd_cwnd: %d rtt: %d rttvar: %d pmtu: %d omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, (int64_t) irp->interval_retrans, (int64_t) irp->snd_cwnd, (int64_t) irp->rtt, (int64_t) irp->rttvar, (int64_t) irp->pmtu, irp->omitted, sp->sender)); + 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 snd_wnd: %d rtt: %d rttvar: %d pmtu: %d omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, (int64_t) irp->interval_retrans, (int64_t) irp->snd_cwnd, (int64_t) irp->snd_wnd, (int64_t) irp->rtt, (int64_t) irp->rttvar, (int64_t) irp->pmtu, irp->omitted, sp->sender)); else { unit_snprintf(cbuf, UNIT_LEN, irp->snd_cwnd, 'A'); iperf_printf(test, report_bw_retrans_cwnd_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->interval_retrans, cbuf, irp->omitted?report_omitted:""); @@ -3840,7 +4306,11 @@ iperf_new_stream(struct iperf_test *test, int s, int s tempdir = getenv("TMP"); } if (tempdir == 0){ +#if defined(__ANDROID__) + tempdir = "/data/local/tmp"; +#else tempdir = "/tmp"; +#endif } snprintf(template, sizeof(template) / sizeof(char), "%s/iperf3.XXXXXX", tempdir); } @@ -3865,7 +4335,7 @@ iperf_new_stream(struct iperf_test *test, int s, int s memset(sp->result, 0, sizeof(struct iperf_stream_result)); TAILQ_INIT(&sp->result->interval_results); - + /* Create and randomize the buffer */ sp->buffer_fd = mkstemp(template); if (sp->buffer_fd == -1) { @@ -3893,6 +4363,7 @@ iperf_new_stream(struct iperf_test *test, int s, int s free(sp); return NULL; } + sp->pending_size = 0; /* Set socket */ sp->socket = s; @@ -3936,42 +4407,98 @@ iperf_new_stream(struct iperf_test *test, int s, int s /**************************************************************************/ int -iperf_init_stream(struct iperf_stream *sp, struct iperf_test *test) +iperf_common_sockopts(struct iperf_test *test, int s) { - socklen_t len; int opt; - len = sizeof(struct sockaddr_storage); - if (getsockname(sp->socket, (struct sockaddr *) &sp->local_addr, &len) < 0) { - i_errno = IEINITSTREAM; - return -1; - } - len = sizeof(struct sockaddr_storage); - if (getpeername(sp->socket, (struct sockaddr *) &sp->remote_addr, &len) < 0) { - i_errno = IEINITSTREAM; - return -1; - } - /* Set IP TOS */ if ((opt = test->settings->tos)) { - if (getsockdomain(sp->socket) == AF_INET6) { + if (getsockdomain(s) == AF_INET6) { #ifdef IPV6_TCLASS - if (setsockopt(sp->socket, IPPROTO_IPV6, IPV6_TCLASS, &opt, sizeof(opt)) < 0) { + if (setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, &opt, sizeof(opt)) < 0) { i_errno = IESETCOS; return -1; } + + /* if the control connection was established with a mapped v4 address + then set IP_TOS on v6 stream socket as well */ + if (iperf_get_mapped_v4(test)) { + if (setsockopt(s, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)) < 0) { + /* ignore any failure of v4 TOS in IPv6 case */ + } + } #else i_errno = IESETCOS; return -1; #endif } else { - if (setsockopt(sp->socket, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)) < 0) { + if (setsockopt(s, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)) < 0) { i_errno = IESETTOS; return -1; } } } + return 0; +} +/**************************************************************************/ +int +iperf_init_stream(struct iperf_stream *sp, struct iperf_test *test) +{ + int opt; + socklen_t len; + + len = sizeof(struct sockaddr_storage); + if (getsockname(sp->socket, (struct sockaddr *) &sp->local_addr, &len) < 0) { + i_errno = IEINITSTREAM; + return -1; + } + len = sizeof(struct sockaddr_storage); + if (getpeername(sp->socket, (struct sockaddr *) &sp->remote_addr, &len) < 0) { + i_errno = IEINITSTREAM; + return -1; + } + +#if defined(HAVE_DONT_FRAGMENT) + /* Set Don't Fragment (DF). Only applicable to IPv4/UDP tests. */ + if (iperf_get_test_protocol_id(test) == Pudp && + getsockdomain(sp->socket) == AF_INET && + iperf_get_dont_fragment(test)) { + + /* + * There are multiple implementations of this feature depending on the OS. + * We need to handle separately Linux, UNIX, and Windows, as well as + * the case that DF isn't supported at all (such as on macOS). + */ +#if defined(IP_MTU_DISCOVER) /* Linux version of IP_DONTFRAG */ + opt = IP_PMTUDISC_DO; + if (setsockopt(sp->socket, IPPROTO_IP, IP_MTU_DISCOVER, &opt, sizeof(opt)) < 0) { + i_errno = IESETDONTFRAGMENT; + return -1; + } +#else +#if defined(IP_DONTFRAG) /* UNIX does IP_DONTFRAG */ + opt = 1; + if (setsockopt(sp->socket, IPPROTO_IP, IP_DONTFRAG, &opt, sizeof(opt)) < 0) { + i_errno = IESETDONTFRAGMENT; + return -1; + } +#else +#if defined(IP_DONTFRAGMENT) /* Windows does IP_DONTFRAGMENT */ + opt = 1; + if (setsockopt(sp->socket, IPPROTO_IP, IP_DONTFRAGMENT, &opt, sizeof(opt)) < 0) { + i_errno = IESETDONTFRAGMENT; + return -1; + } +#else + i_errno = IESETDONTFRAGMENT; + return -1; +#endif /* IP_DONTFRAGMENT */ +#endif /* IP_DONTFRAG */ +#endif /* IP_MTU_DISCOVER */ + } +#endif /* HAVE_DONT_FRAGMENT */ + return 0; } @@ -3987,13 +4514,20 @@ iperf_add_stream(struct iperf_test *test, struct iperf sp->id = 1; } else { // for (n = test->streams, i = 2; n->next; n = n->next, ++i); + // NOTE: this would ideally be set to 1, however this will not + // be changed since it is not causing a significant problem + // and changing it would break multi-stream tests between old + // and new iperf3 versions. i = 2; + prev = NULL; SLIST_FOREACH(n, &test->streams, streams) { prev = n; ++i; } - SLIST_INSERT_AFTER(prev, sp, streams); - sp->id = i; + if (prev) { + SLIST_INSERT_AFTER(prev, sp, streams); + sp->id = i; + } } } @@ -4009,26 +4543,48 @@ static int diskfile_send(struct iperf_stream *sp) { int r; + int buffer_left = sp->diskfile_left; // represents total data in buffer to be sent out static int rtot; /* if needed, read enough data from the disk to fill up the buffer */ if (sp->diskfile_left < sp->test->settings->blksize && !sp->test->done) { - r = read(sp->diskfile_fd, sp->buffer, sp->test->settings->blksize - - sp->diskfile_left); - rtot += r; - if (sp->test->debug) { - printf("read %d bytes from file, %d total\n", r, rtot); - if (r != sp->test->settings->blksize - sp->diskfile_left) - printf("possible eof\n"); - } - /* If there's no data left in the file or in the buffer, we're done */ - if (r == 0 && sp->diskfile_left == 0) { - sp->test->done = 1; - if (sp->test->debug) - printf("done\n"); - } + r = read(sp->diskfile_fd, sp->buffer, sp->test->settings->blksize - + sp->diskfile_left); + buffer_left += r; + rtot += r; + if (sp->test->debug) { + printf("read %d bytes from file, %d total\n", r, rtot); + } + + // If the buffer doesn't contain a full buffer at this point, + // adjust the size of the data to send. + if (buffer_left != sp->test->settings->blksize) { + if (sp->test->debug) + printf("possible eof\n"); + // setting data size to be sent, + // which is less than full block/buffer size + // (to be used by iperf_tcp_send, etc.) + sp->pending_size = buffer_left; + } + + // If there's no work left, we're done. + if (buffer_left == 0) { + sp->test->done = 1; + if (sp->test->debug) + printf("done\n"); + } } + // If there's no data left in the file or in the buffer, we're done. + // No more data available to be sent. + // Return without sending data to the network + if( sp->test->done || buffer_left == 0 ){ + if (sp->test->debug) + printf("already done\n"); + sp->test->done = 1; + return 0; + } + r = sp->snd2(sp); if (r < 0) { return r; @@ -4039,7 +4595,7 @@ diskfile_send(struct iperf_stream *sp) * front of the buffer so they can hopefully go out on the next * pass. */ - sp->diskfile_left = sp->test->settings->blksize - r; + sp->diskfile_left = buffer_left - r; if (sp->diskfile_left && sp->diskfile_left < sp->test->settings->blksize) { memcpy(sp->buffer, sp->buffer + (sp->test->settings->blksize - sp->diskfile_left), @@ -4057,8 +4613,8 @@ diskfile_recv(struct iperf_stream *sp) r = sp->rcv2(sp); if (r > 0) { - (void) write(sp->diskfile_fd, sp->buffer, r); - (void) fsync(sp->diskfile_fd); + // NOTE: Currently ignoring the return value of writing to disk + (void) (write(sp->diskfile_fd, sp->buffer, r) + 1); } return r; } @@ -4143,17 +4699,18 @@ iperf_create_pidfile(struct iperf_test *test) } } } - + /* - * File didn't exist, we couldn't read it, or it didn't correspond to - * a running process. Try to create it. + * File didn't exist, we couldn't read it, or it didn't correspond to + * a running process. Try to create it. */ fd = open(test->pidfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR); if (fd < 0) { return -1; } snprintf(buf, sizeof(buf), "%d", getpid()); /* no trailing newline */ - if (write(fd, buf, strlen(buf) + 1) < 0) { + if (write(fd, buf, strlen(buf)) < 0) { + (void)close(fd); return -1; } if (close(fd) < 0) { @@ -4203,26 +4760,38 @@ iperf_json_start(struct iperf_test *test) int iperf_json_finish(struct iperf_test *test) { - if (test->title) - cJSON_AddStringToObject(test->json_top, "title", test->title); - if (test->extra_data) - cJSON_AddStringToObject(test->json_top, "extra_data", test->extra_data); - /* Include server output */ - if (test->json_server_output) { - cJSON_AddItemToObject(test->json_top, "server_output_json", test->json_server_output); + if (test->json_top) { + if (test->title) { + cJSON_AddStringToObject(test->json_top, "title", test->title); + } + if (test->extra_data) { + cJSON_AddStringToObject(test->json_top, "extra_data", test->extra_data); + } + /* Include server output */ + if (test->json_server_output) { + cJSON_AddItemToObject(test->json_top, "server_output_json", test->json_server_output); + } + if (test->server_output_text) { + cJSON_AddStringToObject(test->json_top, "server_output_text", test->server_output_text); + } + // Get ASCII rendering of JSON structure. Then make our + // own copy of it and return the storage that cJSON allocated + // on our behalf. We keep our own copy around. + char *str = cJSON_Print(test->json_top); + if (str == NULL) { + return -1; + } + test->json_output_string = strdup(str); + cJSON_free(str); + if (test->json_output_string == NULL) { + return -1; + } + fprintf(test->outfile, "%s\n", test->json_output_string); + iflush(test); + cJSON_Delete(test->json_top); + test->json_top = NULL; } - if (test->server_output_text) { - cJSON_AddStringToObject(test->json_top, "server_output_text", test->server_output_text); - } - test->json_output_string = cJSON_Print(test->json_top); - if (test->json_output_string == NULL) - return -1; - fprintf(test->outfile, "%s\n", test->json_output_string); - iflush(test); - cJSON_free(test->json_output_string); - test->json_output_string = NULL; - cJSON_Delete(test->json_top); - test->json_top = test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL; + test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL; return 0; } @@ -4314,13 +4883,14 @@ iperf_clearaffinity(struct iperf_test *test) #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */ } -char iperf_timestr[100]; +static char iperf_timestr[100]; +static char linebuffer[1024]; int iperf_printf(struct iperf_test *test, const char* format, ...) { va_list argp; - int r = -1; + int r = 0, r0; time_t now; struct tm *ltm = NULL; char *ct = NULL; @@ -4347,23 +4917,40 @@ iperf_printf(struct iperf_test *test, const char* form */ if (test->role == 'c') { if (ct) { - fprintf(test->outfile, "%s", ct); + r0 = fprintf(test->outfile, "%s", ct); + if (r0 < 0) + return r0; + r += r0; } - if (test->title) - fprintf(test->outfile, "%s: ", test->title); + if (test->title) { + r0 = fprintf(test->outfile, "%s: ", test->title); + if (r0 < 0) + return r0; + r += r0; + } va_start(argp, format); - r = vfprintf(test->outfile, format, argp); + r0 = vfprintf(test->outfile, format, argp); va_end(argp); + if (r0 < 0) + return r0; + r += r0; } else if (test->role == 's') { - char linebuffer[1024]; - int i = 0; if (ct) { - i = sprintf(linebuffer, "%s", ct); + r0 = snprintf(linebuffer, sizeof(linebuffer), "%s", ct); + if (r0 < 0) + return r0; + r += r0; } - va_start(argp, format); - r = vsnprintf(linebuffer + i, sizeof(linebuffer), format, argp); - va_end(argp); + /* Should always be true as long as sizeof(ct) < sizeof(linebuffer) */ + if (r < sizeof(linebuffer)) { + va_start(argp, format); + r0 = vsnprintf(linebuffer + r, sizeof(linebuffer) - r, format, argp); + va_end(argp); + if (r0 < 0) + return r0; + r += r0; + } fprintf(test->outfile, "%s", linebuffer); if (test->role == 's' && iperf_get_test_get_server_output(test)) {