xref: /aosp_15_r20/external/iperf3/src/iperf_client_api.c (revision 7ab6e6ace082586527a400463bc693a412a40341)
1*7ab6e6acSAndroid Build Coastguard Worker /*
2*7ab6e6acSAndroid Build Coastguard Worker  * iperf, Copyright (c) 2014-2020, The Regents of the University of
3*7ab6e6acSAndroid Build Coastguard Worker  * California, through Lawrence Berkeley National Laboratory (subject
4*7ab6e6acSAndroid Build Coastguard Worker  * to receipt of any required approvals from the U.S. Dept. of
5*7ab6e6acSAndroid Build Coastguard Worker  * Energy).  All rights reserved.
6*7ab6e6acSAndroid Build Coastguard Worker  *
7*7ab6e6acSAndroid Build Coastguard Worker  * If you have questions about your rights to use or distribute this
8*7ab6e6acSAndroid Build Coastguard Worker  * software, please contact Berkeley Lab's Technology Transfer
9*7ab6e6acSAndroid Build Coastguard Worker  * Department at [email protected].
10*7ab6e6acSAndroid Build Coastguard Worker  *
11*7ab6e6acSAndroid Build Coastguard Worker  * NOTICE.  This software is owned by the U.S. Department of Energy.
12*7ab6e6acSAndroid Build Coastguard Worker  * As such, the U.S. Government has been granted for itself and others
13*7ab6e6acSAndroid Build Coastguard Worker  * acting on its behalf a paid-up, nonexclusive, irrevocable,
14*7ab6e6acSAndroid Build Coastguard Worker  * worldwide license in the Software to reproduce, prepare derivative
15*7ab6e6acSAndroid Build Coastguard Worker  * works, and perform publicly and display publicly.  Beginning five
16*7ab6e6acSAndroid Build Coastguard Worker  * (5) years after the date permission to assert copyright is obtained
17*7ab6e6acSAndroid Build Coastguard Worker  * from the U.S. Department of Energy, and subject to any subsequent
18*7ab6e6acSAndroid Build Coastguard Worker  * five (5) year renewals, the U.S. Government is granted for itself
19*7ab6e6acSAndroid Build Coastguard Worker  * and others acting on its behalf a paid-up, nonexclusive,
20*7ab6e6acSAndroid Build Coastguard Worker  * irrevocable, worldwide license in the Software to reproduce,
21*7ab6e6acSAndroid Build Coastguard Worker  * prepare derivative works, distribute copies to the public, perform
22*7ab6e6acSAndroid Build Coastguard Worker  * publicly and display publicly, and to permit others to do so.
23*7ab6e6acSAndroid Build Coastguard Worker  *
24*7ab6e6acSAndroid Build Coastguard Worker  * This code is distributed under a BSD style license, see the LICENSE
25*7ab6e6acSAndroid Build Coastguard Worker  * file for complete information.
26*7ab6e6acSAndroid Build Coastguard Worker  */
27*7ab6e6acSAndroid Build Coastguard Worker #include <errno.h>
28*7ab6e6acSAndroid Build Coastguard Worker #include <setjmp.h>
29*7ab6e6acSAndroid Build Coastguard Worker #include <stdio.h>
30*7ab6e6acSAndroid Build Coastguard Worker #include <stdlib.h>
31*7ab6e6acSAndroid Build Coastguard Worker #include <string.h>
32*7ab6e6acSAndroid Build Coastguard Worker #include <unistd.h>
33*7ab6e6acSAndroid Build Coastguard Worker #include <signal.h>
34*7ab6e6acSAndroid Build Coastguard Worker #include <sys/types.h>
35*7ab6e6acSAndroid Build Coastguard Worker #include <netinet/in.h>
36*7ab6e6acSAndroid Build Coastguard Worker #include <sys/select.h>
37*7ab6e6acSAndroid Build Coastguard Worker #include <sys/uio.h>
38*7ab6e6acSAndroid Build Coastguard Worker #include <arpa/inet.h>
39*7ab6e6acSAndroid Build Coastguard Worker 
40*7ab6e6acSAndroid Build Coastguard Worker #include "iperf.h"
41*7ab6e6acSAndroid Build Coastguard Worker #include "iperf_api.h"
42*7ab6e6acSAndroid Build Coastguard Worker #include "iperf_util.h"
43*7ab6e6acSAndroid Build Coastguard Worker #include "iperf_locale.h"
44*7ab6e6acSAndroid Build Coastguard Worker #include "iperf_time.h"
45*7ab6e6acSAndroid Build Coastguard Worker #include "net.h"
46*7ab6e6acSAndroid Build Coastguard Worker #include "timer.h"
47*7ab6e6acSAndroid Build Coastguard Worker 
48*7ab6e6acSAndroid Build Coastguard Worker #if defined(HAVE_TCP_CONGESTION)
49*7ab6e6acSAndroid Build Coastguard Worker #if !defined(TCP_CA_NAME_MAX)
50*7ab6e6acSAndroid Build Coastguard Worker #define TCP_CA_NAME_MAX 16
51*7ab6e6acSAndroid Build Coastguard Worker #endif /* TCP_CA_NAME_MAX */
52*7ab6e6acSAndroid Build Coastguard Worker #endif /* HAVE_TCP_CONGESTION */
53*7ab6e6acSAndroid Build Coastguard Worker 
54*7ab6e6acSAndroid Build Coastguard Worker int
iperf_create_streams(struct iperf_test * test,int sender)55*7ab6e6acSAndroid Build Coastguard Worker iperf_create_streams(struct iperf_test *test, int sender)
56*7ab6e6acSAndroid Build Coastguard Worker {
57*7ab6e6acSAndroid Build Coastguard Worker     int i, s;
58*7ab6e6acSAndroid Build Coastguard Worker #if defined(HAVE_TCP_CONGESTION)
59*7ab6e6acSAndroid Build Coastguard Worker     int saved_errno;
60*7ab6e6acSAndroid Build Coastguard Worker #endif /* HAVE_TCP_CONGESTION */
61*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_stream *sp;
62*7ab6e6acSAndroid Build Coastguard Worker 
63*7ab6e6acSAndroid Build Coastguard Worker     int orig_bind_port = test->bind_port;
64*7ab6e6acSAndroid Build Coastguard Worker     for (i = 0; i < test->num_streams; ++i) {
65*7ab6e6acSAndroid Build Coastguard Worker 
66*7ab6e6acSAndroid Build Coastguard Worker         test->bind_port = orig_bind_port;
67*7ab6e6acSAndroid Build Coastguard Worker 	if (orig_bind_port)
68*7ab6e6acSAndroid Build Coastguard Worker 	    test->bind_port += i;
69*7ab6e6acSAndroid Build Coastguard Worker         if ((s = test->protocol->connect(test)) < 0)
70*7ab6e6acSAndroid Build Coastguard Worker             return -1;
71*7ab6e6acSAndroid Build Coastguard Worker 
72*7ab6e6acSAndroid Build Coastguard Worker #if defined(HAVE_TCP_CONGESTION)
73*7ab6e6acSAndroid Build Coastguard Worker 	if (test->protocol->id == Ptcp) {
74*7ab6e6acSAndroid Build Coastguard Worker 	    if (test->congestion) {
75*7ab6e6acSAndroid Build Coastguard Worker 		if (setsockopt(s, IPPROTO_TCP, TCP_CONGESTION, test->congestion, strlen(test->congestion)) < 0) {
76*7ab6e6acSAndroid Build Coastguard Worker 		    saved_errno = errno;
77*7ab6e6acSAndroid Build Coastguard Worker 		    close(s);
78*7ab6e6acSAndroid Build Coastguard Worker 		    errno = saved_errno;
79*7ab6e6acSAndroid Build Coastguard Worker 		    i_errno = IESETCONGESTION;
80*7ab6e6acSAndroid Build Coastguard Worker 		    return -1;
81*7ab6e6acSAndroid Build Coastguard Worker 		}
82*7ab6e6acSAndroid Build Coastguard Worker 	    }
83*7ab6e6acSAndroid Build Coastguard Worker 	    {
84*7ab6e6acSAndroid Build Coastguard Worker 		socklen_t len = TCP_CA_NAME_MAX;
85*7ab6e6acSAndroid Build Coastguard Worker 		char ca[TCP_CA_NAME_MAX + 1];
86*7ab6e6acSAndroid Build Coastguard Worker 		if (getsockopt(s, IPPROTO_TCP, TCP_CONGESTION, ca, &len) < 0) {
87*7ab6e6acSAndroid Build Coastguard Worker 		    saved_errno = errno;
88*7ab6e6acSAndroid Build Coastguard Worker 		    close(s);
89*7ab6e6acSAndroid Build Coastguard Worker 		    errno = saved_errno;
90*7ab6e6acSAndroid Build Coastguard Worker 		    i_errno = IESETCONGESTION;
91*7ab6e6acSAndroid Build Coastguard Worker 		    return -1;
92*7ab6e6acSAndroid Build Coastguard Worker 		}
93*7ab6e6acSAndroid Build Coastguard Worker 		test->congestion_used = strdup(ca);
94*7ab6e6acSAndroid Build Coastguard Worker 		if (test->debug) {
95*7ab6e6acSAndroid Build Coastguard Worker 		    printf("Congestion algorithm is %s\n", test->congestion_used);
96*7ab6e6acSAndroid Build Coastguard Worker 		}
97*7ab6e6acSAndroid Build Coastguard Worker 	    }
98*7ab6e6acSAndroid Build Coastguard Worker 	}
99*7ab6e6acSAndroid Build Coastguard Worker #endif /* HAVE_TCP_CONGESTION */
100*7ab6e6acSAndroid Build Coastguard Worker 
101*7ab6e6acSAndroid Build Coastguard Worker 	if (sender)
102*7ab6e6acSAndroid Build Coastguard Worker 	    FD_SET(s, &test->write_set);
103*7ab6e6acSAndroid Build Coastguard Worker 	else
104*7ab6e6acSAndroid Build Coastguard Worker 	    FD_SET(s, &test->read_set);
105*7ab6e6acSAndroid Build Coastguard Worker 	if (s > test->max_fd) test->max_fd = s;
106*7ab6e6acSAndroid Build Coastguard Worker 
107*7ab6e6acSAndroid Build Coastguard Worker         sp = iperf_new_stream(test, s, sender);
108*7ab6e6acSAndroid Build Coastguard Worker         if (!sp)
109*7ab6e6acSAndroid Build Coastguard Worker             return -1;
110*7ab6e6acSAndroid Build Coastguard Worker 
111*7ab6e6acSAndroid Build Coastguard Worker         /* Perform the new stream callback */
112*7ab6e6acSAndroid Build Coastguard Worker         if (test->on_new_stream)
113*7ab6e6acSAndroid Build Coastguard Worker             test->on_new_stream(sp);
114*7ab6e6acSAndroid Build Coastguard Worker     }
115*7ab6e6acSAndroid Build Coastguard Worker 
116*7ab6e6acSAndroid Build Coastguard Worker     return 0;
117*7ab6e6acSAndroid Build Coastguard Worker }
118*7ab6e6acSAndroid Build Coastguard Worker 
119*7ab6e6acSAndroid Build Coastguard Worker static void
test_timer_proc(TimerClientData client_data,struct iperf_time * nowP)120*7ab6e6acSAndroid Build Coastguard Worker test_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
121*7ab6e6acSAndroid Build Coastguard Worker {
122*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_test *test = client_data.p;
123*7ab6e6acSAndroid Build Coastguard Worker 
124*7ab6e6acSAndroid Build Coastguard Worker     test->timer = NULL;
125*7ab6e6acSAndroid Build Coastguard Worker     test->done = 1;
126*7ab6e6acSAndroid Build Coastguard Worker }
127*7ab6e6acSAndroid Build Coastguard Worker 
128*7ab6e6acSAndroid Build Coastguard Worker static void
client_stats_timer_proc(TimerClientData client_data,struct iperf_time * nowP)129*7ab6e6acSAndroid Build Coastguard Worker client_stats_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
130*7ab6e6acSAndroid Build Coastguard Worker {
131*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_test *test = client_data.p;
132*7ab6e6acSAndroid Build Coastguard Worker 
133*7ab6e6acSAndroid Build Coastguard Worker     if (test->done)
134*7ab6e6acSAndroid Build Coastguard Worker         return;
135*7ab6e6acSAndroid Build Coastguard Worker     if (test->stats_callback)
136*7ab6e6acSAndroid Build Coastguard Worker 	test->stats_callback(test);
137*7ab6e6acSAndroid Build Coastguard Worker }
138*7ab6e6acSAndroid Build Coastguard Worker 
139*7ab6e6acSAndroid Build Coastguard Worker static void
client_reporter_timer_proc(TimerClientData client_data,struct iperf_time * nowP)140*7ab6e6acSAndroid Build Coastguard Worker client_reporter_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
141*7ab6e6acSAndroid Build Coastguard Worker {
142*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_test *test = client_data.p;
143*7ab6e6acSAndroid Build Coastguard Worker 
144*7ab6e6acSAndroid Build Coastguard Worker     if (test->done)
145*7ab6e6acSAndroid Build Coastguard Worker         return;
146*7ab6e6acSAndroid Build Coastguard Worker     if (test->reporter_callback)
147*7ab6e6acSAndroid Build Coastguard Worker 	test->reporter_callback(test);
148*7ab6e6acSAndroid Build Coastguard Worker }
149*7ab6e6acSAndroid Build Coastguard Worker 
150*7ab6e6acSAndroid Build Coastguard Worker static int
create_client_timers(struct iperf_test * test)151*7ab6e6acSAndroid Build Coastguard Worker create_client_timers(struct iperf_test * test)
152*7ab6e6acSAndroid Build Coastguard Worker {
153*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_time now;
154*7ab6e6acSAndroid Build Coastguard Worker     TimerClientData cd;
155*7ab6e6acSAndroid Build Coastguard Worker 
156*7ab6e6acSAndroid Build Coastguard Worker     if (iperf_time_now(&now) < 0) {
157*7ab6e6acSAndroid Build Coastguard Worker 	i_errno = IEINITTEST;
158*7ab6e6acSAndroid Build Coastguard Worker 	return -1;
159*7ab6e6acSAndroid Build Coastguard Worker     }
160*7ab6e6acSAndroid Build Coastguard Worker     cd.p = test;
161*7ab6e6acSAndroid Build Coastguard Worker     test->timer = test->stats_timer = test->reporter_timer = NULL;
162*7ab6e6acSAndroid Build Coastguard Worker     if (test->duration != 0) {
163*7ab6e6acSAndroid Build Coastguard Worker 	test->done = 0;
164*7ab6e6acSAndroid Build Coastguard Worker         test->timer = tmr_create(&now, test_timer_proc, cd, ( test->duration + test->omit ) * SEC_TO_US, 0);
165*7ab6e6acSAndroid Build Coastguard Worker         if (test->timer == NULL) {
166*7ab6e6acSAndroid Build Coastguard Worker             i_errno = IEINITTEST;
167*7ab6e6acSAndroid Build Coastguard Worker             return -1;
168*7ab6e6acSAndroid Build Coastguard Worker 	}
169*7ab6e6acSAndroid Build Coastguard Worker     }
170*7ab6e6acSAndroid Build Coastguard Worker     if (test->stats_interval != 0) {
171*7ab6e6acSAndroid Build Coastguard Worker         test->stats_timer = tmr_create(&now, client_stats_timer_proc, cd, test->stats_interval * SEC_TO_US, 1);
172*7ab6e6acSAndroid Build Coastguard Worker         if (test->stats_timer == NULL) {
173*7ab6e6acSAndroid Build Coastguard Worker             i_errno = IEINITTEST;
174*7ab6e6acSAndroid Build Coastguard Worker             return -1;
175*7ab6e6acSAndroid Build Coastguard Worker 	}
176*7ab6e6acSAndroid Build Coastguard Worker     }
177*7ab6e6acSAndroid Build Coastguard Worker     if (test->reporter_interval != 0) {
178*7ab6e6acSAndroid Build Coastguard Worker         test->reporter_timer = tmr_create(&now, client_reporter_timer_proc, cd, test->reporter_interval * SEC_TO_US, 1);
179*7ab6e6acSAndroid Build Coastguard Worker         if (test->reporter_timer == NULL) {
180*7ab6e6acSAndroid Build Coastguard Worker             i_errno = IEINITTEST;
181*7ab6e6acSAndroid Build Coastguard Worker             return -1;
182*7ab6e6acSAndroid Build Coastguard Worker 	}
183*7ab6e6acSAndroid Build Coastguard Worker     }
184*7ab6e6acSAndroid Build Coastguard Worker     return 0;
185*7ab6e6acSAndroid Build Coastguard Worker }
186*7ab6e6acSAndroid Build Coastguard Worker 
187*7ab6e6acSAndroid Build Coastguard Worker static void
client_omit_timer_proc(TimerClientData client_data,struct iperf_time * nowP)188*7ab6e6acSAndroid Build Coastguard Worker client_omit_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
189*7ab6e6acSAndroid Build Coastguard Worker {
190*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_test *test = client_data.p;
191*7ab6e6acSAndroid Build Coastguard Worker 
192*7ab6e6acSAndroid Build Coastguard Worker     test->omit_timer = NULL;
193*7ab6e6acSAndroid Build Coastguard Worker     test->omitting = 0;
194*7ab6e6acSAndroid Build Coastguard Worker     iperf_reset_stats(test);
195*7ab6e6acSAndroid Build Coastguard Worker     if (test->verbose && !test->json_output && test->reporter_interval == 0)
196*7ab6e6acSAndroid Build Coastguard Worker         iperf_printf(test, "%s", report_omit_done);
197*7ab6e6acSAndroid Build Coastguard Worker 
198*7ab6e6acSAndroid Build Coastguard Worker     /* Reset the timers. */
199*7ab6e6acSAndroid Build Coastguard Worker     if (test->stats_timer != NULL)
200*7ab6e6acSAndroid Build Coastguard Worker         tmr_reset(nowP, test->stats_timer);
201*7ab6e6acSAndroid Build Coastguard Worker     if (test->reporter_timer != NULL)
202*7ab6e6acSAndroid Build Coastguard Worker         tmr_reset(nowP, test->reporter_timer);
203*7ab6e6acSAndroid Build Coastguard Worker }
204*7ab6e6acSAndroid Build Coastguard Worker 
205*7ab6e6acSAndroid Build Coastguard Worker static int
create_client_omit_timer(struct iperf_test * test)206*7ab6e6acSAndroid Build Coastguard Worker create_client_omit_timer(struct iperf_test * test)
207*7ab6e6acSAndroid Build Coastguard Worker {
208*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_time now;
209*7ab6e6acSAndroid Build Coastguard Worker     TimerClientData cd;
210*7ab6e6acSAndroid Build Coastguard Worker 
211*7ab6e6acSAndroid Build Coastguard Worker     if (test->omit == 0) {
212*7ab6e6acSAndroid Build Coastguard Worker 	test->omit_timer = NULL;
213*7ab6e6acSAndroid Build Coastguard Worker         test->omitting = 0;
214*7ab6e6acSAndroid Build Coastguard Worker     } else {
215*7ab6e6acSAndroid Build Coastguard Worker 	if (iperf_time_now(&now) < 0) {
216*7ab6e6acSAndroid Build Coastguard Worker 	    i_errno = IEINITTEST;
217*7ab6e6acSAndroid Build Coastguard Worker 	    return -1;
218*7ab6e6acSAndroid Build Coastguard Worker 	}
219*7ab6e6acSAndroid Build Coastguard Worker 	test->omitting = 1;
220*7ab6e6acSAndroid Build Coastguard Worker 	cd.p = test;
221*7ab6e6acSAndroid Build Coastguard Worker 	test->omit_timer = tmr_create(&now, client_omit_timer_proc, cd, test->omit * SEC_TO_US, 0);
222*7ab6e6acSAndroid Build Coastguard Worker 	if (test->omit_timer == NULL) {
223*7ab6e6acSAndroid Build Coastguard Worker 	    i_errno = IEINITTEST;
224*7ab6e6acSAndroid Build Coastguard Worker 	    return -1;
225*7ab6e6acSAndroid Build Coastguard Worker 	}
226*7ab6e6acSAndroid Build Coastguard Worker     }
227*7ab6e6acSAndroid Build Coastguard Worker     return 0;
228*7ab6e6acSAndroid Build Coastguard Worker }
229*7ab6e6acSAndroid Build Coastguard Worker 
230*7ab6e6acSAndroid Build Coastguard Worker int
iperf_handle_message_client(struct iperf_test * test)231*7ab6e6acSAndroid Build Coastguard Worker iperf_handle_message_client(struct iperf_test *test)
232*7ab6e6acSAndroid Build Coastguard Worker {
233*7ab6e6acSAndroid Build Coastguard Worker     int rval;
234*7ab6e6acSAndroid Build Coastguard Worker     int32_t err;
235*7ab6e6acSAndroid Build Coastguard Worker 
236*7ab6e6acSAndroid Build Coastguard Worker     /*!!! Why is this read() and not Nread()? */
237*7ab6e6acSAndroid Build Coastguard Worker     if ((rval = read(test->ctrl_sck, (char*) &test->state, sizeof(signed char))) <= 0) {
238*7ab6e6acSAndroid Build Coastguard Worker         if (rval == 0) {
239*7ab6e6acSAndroid Build Coastguard Worker             i_errno = IECTRLCLOSE;
240*7ab6e6acSAndroid Build Coastguard Worker             return -1;
241*7ab6e6acSAndroid Build Coastguard Worker         } else {
242*7ab6e6acSAndroid Build Coastguard Worker             i_errno = IERECVMESSAGE;
243*7ab6e6acSAndroid Build Coastguard Worker             return -1;
244*7ab6e6acSAndroid Build Coastguard Worker         }
245*7ab6e6acSAndroid Build Coastguard Worker     }
246*7ab6e6acSAndroid Build Coastguard Worker 
247*7ab6e6acSAndroid Build Coastguard Worker     switch (test->state) {
248*7ab6e6acSAndroid Build Coastguard Worker         case PARAM_EXCHANGE:
249*7ab6e6acSAndroid Build Coastguard Worker             if (iperf_exchange_parameters(test) < 0)
250*7ab6e6acSAndroid Build Coastguard Worker                 return -1;
251*7ab6e6acSAndroid Build Coastguard Worker             if (test->on_connect)
252*7ab6e6acSAndroid Build Coastguard Worker                 test->on_connect(test);
253*7ab6e6acSAndroid Build Coastguard Worker             break;
254*7ab6e6acSAndroid Build Coastguard Worker         case CREATE_STREAMS:
255*7ab6e6acSAndroid Build Coastguard Worker             if (test->mode == BIDIRECTIONAL)
256*7ab6e6acSAndroid Build Coastguard Worker             {
257*7ab6e6acSAndroid Build Coastguard Worker                 if (iperf_create_streams(test, 1) < 0)
258*7ab6e6acSAndroid Build Coastguard Worker                     return -1;
259*7ab6e6acSAndroid Build Coastguard Worker                 if (iperf_create_streams(test, 0) < 0)
260*7ab6e6acSAndroid Build Coastguard Worker                     return -1;
261*7ab6e6acSAndroid Build Coastguard Worker             }
262*7ab6e6acSAndroid Build Coastguard Worker             else if (iperf_create_streams(test, test->mode) < 0)
263*7ab6e6acSAndroid Build Coastguard Worker                 return -1;
264*7ab6e6acSAndroid Build Coastguard Worker             break;
265*7ab6e6acSAndroid Build Coastguard Worker         case TEST_START:
266*7ab6e6acSAndroid Build Coastguard Worker             if (iperf_init_test(test) < 0)
267*7ab6e6acSAndroid Build Coastguard Worker                 return -1;
268*7ab6e6acSAndroid Build Coastguard Worker             if (create_client_timers(test) < 0)
269*7ab6e6acSAndroid Build Coastguard Worker                 return -1;
270*7ab6e6acSAndroid Build Coastguard Worker             if (create_client_omit_timer(test) < 0)
271*7ab6e6acSAndroid Build Coastguard Worker                 return -1;
272*7ab6e6acSAndroid Build Coastguard Worker 	    if (test->mode)
273*7ab6e6acSAndroid Build Coastguard Worker 		if (iperf_create_send_timers(test) < 0)
274*7ab6e6acSAndroid Build Coastguard Worker 		    return -1;
275*7ab6e6acSAndroid Build Coastguard Worker             break;
276*7ab6e6acSAndroid Build Coastguard Worker         case TEST_RUNNING:
277*7ab6e6acSAndroid Build Coastguard Worker             break;
278*7ab6e6acSAndroid Build Coastguard Worker         case EXCHANGE_RESULTS:
279*7ab6e6acSAndroid Build Coastguard Worker             if (iperf_exchange_results(test) < 0)
280*7ab6e6acSAndroid Build Coastguard Worker                 return -1;
281*7ab6e6acSAndroid Build Coastguard Worker             break;
282*7ab6e6acSAndroid Build Coastguard Worker         case DISPLAY_RESULTS:
283*7ab6e6acSAndroid Build Coastguard Worker             if (test->on_test_finish)
284*7ab6e6acSAndroid Build Coastguard Worker                 test->on_test_finish(test);
285*7ab6e6acSAndroid Build Coastguard Worker             iperf_client_end(test);
286*7ab6e6acSAndroid Build Coastguard Worker             break;
287*7ab6e6acSAndroid Build Coastguard Worker         case IPERF_DONE:
288*7ab6e6acSAndroid Build Coastguard Worker             break;
289*7ab6e6acSAndroid Build Coastguard Worker         case SERVER_TERMINATE:
290*7ab6e6acSAndroid Build Coastguard Worker             i_errno = IESERVERTERM;
291*7ab6e6acSAndroid Build Coastguard Worker 
292*7ab6e6acSAndroid Build Coastguard Worker 	    /*
293*7ab6e6acSAndroid Build Coastguard Worker 	     * Temporarily be in DISPLAY_RESULTS phase so we can get
294*7ab6e6acSAndroid Build Coastguard Worker 	     * ending summary statistics.
295*7ab6e6acSAndroid Build Coastguard Worker 	     */
296*7ab6e6acSAndroid Build Coastguard Worker 	    signed char oldstate = test->state;
297*7ab6e6acSAndroid Build Coastguard Worker 	    cpu_util(test->cpu_util);
298*7ab6e6acSAndroid Build Coastguard Worker 	    test->state = DISPLAY_RESULTS;
299*7ab6e6acSAndroid Build Coastguard Worker 	    test->reporter_callback(test);
300*7ab6e6acSAndroid Build Coastguard Worker 	    test->state = oldstate;
301*7ab6e6acSAndroid Build Coastguard Worker             return -1;
302*7ab6e6acSAndroid Build Coastguard Worker         case ACCESS_DENIED:
303*7ab6e6acSAndroid Build Coastguard Worker             i_errno = IEACCESSDENIED;
304*7ab6e6acSAndroid Build Coastguard Worker             return -1;
305*7ab6e6acSAndroid Build Coastguard Worker         case SERVER_ERROR:
306*7ab6e6acSAndroid Build Coastguard Worker             if (Nread(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) {
307*7ab6e6acSAndroid Build Coastguard Worker                 i_errno = IECTRLREAD;
308*7ab6e6acSAndroid Build Coastguard Worker                 return -1;
309*7ab6e6acSAndroid Build Coastguard Worker             }
310*7ab6e6acSAndroid Build Coastguard Worker 	    i_errno = ntohl(err);
311*7ab6e6acSAndroid Build Coastguard Worker             if (Nread(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) {
312*7ab6e6acSAndroid Build Coastguard Worker                 i_errno = IECTRLREAD;
313*7ab6e6acSAndroid Build Coastguard Worker                 return -1;
314*7ab6e6acSAndroid Build Coastguard Worker             }
315*7ab6e6acSAndroid Build Coastguard Worker             errno = ntohl(err);
316*7ab6e6acSAndroid Build Coastguard Worker             return -1;
317*7ab6e6acSAndroid Build Coastguard Worker         default:
318*7ab6e6acSAndroid Build Coastguard Worker             i_errno = IEMESSAGE;
319*7ab6e6acSAndroid Build Coastguard Worker             return -1;
320*7ab6e6acSAndroid Build Coastguard Worker     }
321*7ab6e6acSAndroid Build Coastguard Worker 
322*7ab6e6acSAndroid Build Coastguard Worker     return 0;
323*7ab6e6acSAndroid Build Coastguard Worker }
324*7ab6e6acSAndroid Build Coastguard Worker 
325*7ab6e6acSAndroid Build Coastguard Worker 
326*7ab6e6acSAndroid Build Coastguard Worker 
327*7ab6e6acSAndroid Build Coastguard Worker /* iperf_connect -- client to server connection function */
328*7ab6e6acSAndroid Build Coastguard Worker int
iperf_connect(struct iperf_test * test)329*7ab6e6acSAndroid Build Coastguard Worker iperf_connect(struct iperf_test *test)
330*7ab6e6acSAndroid Build Coastguard Worker {
331*7ab6e6acSAndroid Build Coastguard Worker     FD_ZERO(&test->read_set);
332*7ab6e6acSAndroid Build Coastguard Worker     FD_ZERO(&test->write_set);
333*7ab6e6acSAndroid Build Coastguard Worker 
334*7ab6e6acSAndroid Build Coastguard Worker     make_cookie(test->cookie);
335*7ab6e6acSAndroid Build Coastguard Worker 
336*7ab6e6acSAndroid Build Coastguard Worker     /* Create and connect the control channel */
337*7ab6e6acSAndroid Build Coastguard Worker     if (test->ctrl_sck < 0)
338*7ab6e6acSAndroid Build Coastguard Worker 	// Create the control channel using an ephemeral port
339*7ab6e6acSAndroid Build Coastguard Worker 	test->ctrl_sck = netdial(test->settings->domain, Ptcp, test->bind_address, 0, test->server_hostname, test->server_port, test->settings->connect_timeout);
340*7ab6e6acSAndroid Build Coastguard Worker     if (test->ctrl_sck < 0) {
341*7ab6e6acSAndroid Build Coastguard Worker         i_errno = IECONNECT;
342*7ab6e6acSAndroid Build Coastguard Worker         return -1;
343*7ab6e6acSAndroid Build Coastguard Worker     }
344*7ab6e6acSAndroid Build Coastguard Worker 
345*7ab6e6acSAndroid Build Coastguard Worker     if (Nwrite(test->ctrl_sck, test->cookie, COOKIE_SIZE, Ptcp) < 0) {
346*7ab6e6acSAndroid Build Coastguard Worker         i_errno = IESENDCOOKIE;
347*7ab6e6acSAndroid Build Coastguard Worker         return -1;
348*7ab6e6acSAndroid Build Coastguard Worker     }
349*7ab6e6acSAndroid Build Coastguard Worker 
350*7ab6e6acSAndroid Build Coastguard Worker     FD_SET(test->ctrl_sck, &test->read_set);
351*7ab6e6acSAndroid Build Coastguard Worker     if (test->ctrl_sck > test->max_fd) test->max_fd = test->ctrl_sck;
352*7ab6e6acSAndroid Build Coastguard Worker 
353*7ab6e6acSAndroid Build Coastguard Worker     int opt;
354*7ab6e6acSAndroid Build Coastguard Worker     socklen_t len;
355*7ab6e6acSAndroid Build Coastguard Worker 
356*7ab6e6acSAndroid Build Coastguard Worker     len = sizeof(opt);
357*7ab6e6acSAndroid Build Coastguard Worker     if (getsockopt(test->ctrl_sck, IPPROTO_TCP, TCP_MAXSEG, &opt, &len) < 0) {
358*7ab6e6acSAndroid Build Coastguard Worker         test->ctrl_sck_mss = 0;
359*7ab6e6acSAndroid Build Coastguard Worker     }
360*7ab6e6acSAndroid Build Coastguard Worker     else {
361*7ab6e6acSAndroid Build Coastguard Worker         if (opt > 0 && opt <= MAX_UDP_BLOCKSIZE) {
362*7ab6e6acSAndroid Build Coastguard Worker             test->ctrl_sck_mss = opt;
363*7ab6e6acSAndroid Build Coastguard Worker         }
364*7ab6e6acSAndroid Build Coastguard Worker         else {
365*7ab6e6acSAndroid Build Coastguard Worker             char str[128];
366*7ab6e6acSAndroid Build Coastguard Worker             snprintf(str, sizeof(str),
367*7ab6e6acSAndroid Build Coastguard Worker                      "Ignoring nonsense TCP MSS %d", opt);
368*7ab6e6acSAndroid Build Coastguard Worker             warning(str);
369*7ab6e6acSAndroid Build Coastguard Worker 
370*7ab6e6acSAndroid Build Coastguard Worker             test->ctrl_sck_mss = 0;
371*7ab6e6acSAndroid Build Coastguard Worker         }
372*7ab6e6acSAndroid Build Coastguard Worker     }
373*7ab6e6acSAndroid Build Coastguard Worker 
374*7ab6e6acSAndroid Build Coastguard Worker     if (test->verbose) {
375*7ab6e6acSAndroid Build Coastguard Worker 	printf("Control connection MSS %d\n", test->ctrl_sck_mss);
376*7ab6e6acSAndroid Build Coastguard Worker     }
377*7ab6e6acSAndroid Build Coastguard Worker 
378*7ab6e6acSAndroid Build Coastguard Worker     /*
379*7ab6e6acSAndroid Build Coastguard Worker      * If we're doing a UDP test and the block size wasn't explicitly
380*7ab6e6acSAndroid Build Coastguard Worker      * set, then use the known MSS of the control connection to pick
381*7ab6e6acSAndroid Build Coastguard Worker      * an appropriate default.  If we weren't able to get the
382*7ab6e6acSAndroid Build Coastguard Worker      * MSS for some reason, then default to something that should
383*7ab6e6acSAndroid Build Coastguard Worker      * work on non-jumbo-frame Ethernet networks.  The goal is to
384*7ab6e6acSAndroid Build Coastguard Worker      * pick a reasonable default that is large but should get from
385*7ab6e6acSAndroid Build Coastguard Worker      * sender to receiver without any IP fragmentation.
386*7ab6e6acSAndroid Build Coastguard Worker      *
387*7ab6e6acSAndroid Build Coastguard Worker      * We assume that the control connection is routed the same as the
388*7ab6e6acSAndroid Build Coastguard Worker      * data packets (thus has the same PMTU).  Also in the case of
389*7ab6e6acSAndroid Build Coastguard Worker      * --reverse tests, we assume that the MTU is the same in both
390*7ab6e6acSAndroid Build Coastguard Worker      * directions.  Note that even if the algorithm guesses wrong,
391*7ab6e6acSAndroid Build Coastguard Worker      * the user always has the option to override.
392*7ab6e6acSAndroid Build Coastguard Worker      */
393*7ab6e6acSAndroid Build Coastguard Worker     if (test->protocol->id == Pudp) {
394*7ab6e6acSAndroid Build Coastguard Worker 	if (test->settings->blksize == 0) {
395*7ab6e6acSAndroid Build Coastguard Worker 	    if (test->ctrl_sck_mss) {
396*7ab6e6acSAndroid Build Coastguard Worker 		test->settings->blksize = test->ctrl_sck_mss;
397*7ab6e6acSAndroid Build Coastguard Worker 	    }
398*7ab6e6acSAndroid Build Coastguard Worker 	    else {
399*7ab6e6acSAndroid Build Coastguard Worker 		test->settings->blksize = DEFAULT_UDP_BLKSIZE;
400*7ab6e6acSAndroid Build Coastguard Worker 	    }
401*7ab6e6acSAndroid Build Coastguard Worker 	    if (test->verbose) {
402*7ab6e6acSAndroid Build Coastguard Worker 		printf("Setting UDP block size to %d\n", test->settings->blksize);
403*7ab6e6acSAndroid Build Coastguard Worker 	    }
404*7ab6e6acSAndroid Build Coastguard Worker 	}
405*7ab6e6acSAndroid Build Coastguard Worker 
406*7ab6e6acSAndroid Build Coastguard Worker 	/*
407*7ab6e6acSAndroid Build Coastguard Worker 	 * Regardless of whether explicitly or implicitly set, if the
408*7ab6e6acSAndroid Build Coastguard Worker 	 * block size is larger than the MSS, print a warning.
409*7ab6e6acSAndroid Build Coastguard Worker 	 */
410*7ab6e6acSAndroid Build Coastguard Worker 	if (test->ctrl_sck_mss > 0 &&
411*7ab6e6acSAndroid Build Coastguard Worker 	    test->settings->blksize > test->ctrl_sck_mss) {
412*7ab6e6acSAndroid Build Coastguard Worker 	    char str[128];
413*7ab6e6acSAndroid Build Coastguard Worker 	    snprintf(str, sizeof(str),
414*7ab6e6acSAndroid Build Coastguard Worker 		     "UDP block size %d exceeds TCP MSS %d, may result in fragmentation / drops", test->settings->blksize, test->ctrl_sck_mss);
415*7ab6e6acSAndroid Build Coastguard Worker 	    warning(str);
416*7ab6e6acSAndroid Build Coastguard Worker 	}
417*7ab6e6acSAndroid Build Coastguard Worker     }
418*7ab6e6acSAndroid Build Coastguard Worker 
419*7ab6e6acSAndroid Build Coastguard Worker     return 0;
420*7ab6e6acSAndroid Build Coastguard Worker }
421*7ab6e6acSAndroid Build Coastguard Worker 
422*7ab6e6acSAndroid Build Coastguard Worker 
423*7ab6e6acSAndroid Build Coastguard Worker int
iperf_client_end(struct iperf_test * test)424*7ab6e6acSAndroid Build Coastguard Worker iperf_client_end(struct iperf_test *test)
425*7ab6e6acSAndroid Build Coastguard Worker {
426*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_stream *sp;
427*7ab6e6acSAndroid Build Coastguard Worker 
428*7ab6e6acSAndroid Build Coastguard Worker     /* Close all stream sockets */
429*7ab6e6acSAndroid Build Coastguard Worker     SLIST_FOREACH(sp, &test->streams, streams) {
430*7ab6e6acSAndroid Build Coastguard Worker         close(sp->socket);
431*7ab6e6acSAndroid Build Coastguard Worker     }
432*7ab6e6acSAndroid Build Coastguard Worker 
433*7ab6e6acSAndroid Build Coastguard Worker     /* show final summary */
434*7ab6e6acSAndroid Build Coastguard Worker     test->reporter_callback(test);
435*7ab6e6acSAndroid Build Coastguard Worker 
436*7ab6e6acSAndroid Build Coastguard Worker     if (iperf_set_send_state(test, IPERF_DONE) != 0)
437*7ab6e6acSAndroid Build Coastguard Worker         return -1;
438*7ab6e6acSAndroid Build Coastguard Worker 
439*7ab6e6acSAndroid Build Coastguard Worker     /* Close control socket */
440*7ab6e6acSAndroid Build Coastguard Worker     if (test->ctrl_sck)
441*7ab6e6acSAndroid Build Coastguard Worker         close(test->ctrl_sck);
442*7ab6e6acSAndroid Build Coastguard Worker 
443*7ab6e6acSAndroid Build Coastguard Worker     return 0;
444*7ab6e6acSAndroid Build Coastguard Worker }
445*7ab6e6acSAndroid Build Coastguard Worker 
446*7ab6e6acSAndroid Build Coastguard Worker 
447*7ab6e6acSAndroid Build Coastguard Worker int
iperf_run_client(struct iperf_test * test)448*7ab6e6acSAndroid Build Coastguard Worker iperf_run_client(struct iperf_test * test)
449*7ab6e6acSAndroid Build Coastguard Worker {
450*7ab6e6acSAndroid Build Coastguard Worker     int startup;
451*7ab6e6acSAndroid Build Coastguard Worker     int result = 0;
452*7ab6e6acSAndroid Build Coastguard Worker     fd_set read_set, write_set;
453*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_time now;
454*7ab6e6acSAndroid Build Coastguard Worker     struct timeval* timeout = NULL;
455*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_stream *sp;
456*7ab6e6acSAndroid Build Coastguard Worker 
457*7ab6e6acSAndroid Build Coastguard Worker     if (test->logfile)
458*7ab6e6acSAndroid Build Coastguard Worker         if (iperf_open_logfile(test) < 0)
459*7ab6e6acSAndroid Build Coastguard Worker             return -1;
460*7ab6e6acSAndroid Build Coastguard Worker 
461*7ab6e6acSAndroid Build Coastguard Worker     if (test->affinity != -1)
462*7ab6e6acSAndroid Build Coastguard Worker 	if (iperf_setaffinity(test, test->affinity) != 0)
463*7ab6e6acSAndroid Build Coastguard Worker 	    return -1;
464*7ab6e6acSAndroid Build Coastguard Worker 
465*7ab6e6acSAndroid Build Coastguard Worker     if (test->json_output)
466*7ab6e6acSAndroid Build Coastguard Worker 	if (iperf_json_start(test) < 0)
467*7ab6e6acSAndroid Build Coastguard Worker 	    return -1;
468*7ab6e6acSAndroid Build Coastguard Worker 
469*7ab6e6acSAndroid Build Coastguard Worker     if (test->json_output) {
470*7ab6e6acSAndroid Build Coastguard Worker 	cJSON_AddItemToObject(test->json_start, "version", cJSON_CreateString(version));
471*7ab6e6acSAndroid Build Coastguard Worker 	cJSON_AddItemToObject(test->json_start, "system_info", cJSON_CreateString(get_system_info()));
472*7ab6e6acSAndroid Build Coastguard Worker     } else if (test->verbose) {
473*7ab6e6acSAndroid Build Coastguard Worker 	iperf_printf(test, "%s\n", version);
474*7ab6e6acSAndroid Build Coastguard Worker 	iperf_printf(test, "%s", "");
475*7ab6e6acSAndroid Build Coastguard Worker 	iperf_printf(test, "%s\n", get_system_info());
476*7ab6e6acSAndroid Build Coastguard Worker 	iflush(test);
477*7ab6e6acSAndroid Build Coastguard Worker     }
478*7ab6e6acSAndroid Build Coastguard Worker 
479*7ab6e6acSAndroid Build Coastguard Worker     /* Start the client and connect to the server */
480*7ab6e6acSAndroid Build Coastguard Worker     if (iperf_connect(test) < 0)
481*7ab6e6acSAndroid Build Coastguard Worker         goto cleanup_and_fail;
482*7ab6e6acSAndroid Build Coastguard Worker 
483*7ab6e6acSAndroid Build Coastguard Worker     /* Begin calculating CPU utilization */
484*7ab6e6acSAndroid Build Coastguard Worker     cpu_util(NULL);
485*7ab6e6acSAndroid Build Coastguard Worker 
486*7ab6e6acSAndroid Build Coastguard Worker     startup = 1;
487*7ab6e6acSAndroid Build Coastguard Worker     while (test->state != IPERF_DONE) {
488*7ab6e6acSAndroid Build Coastguard Worker 	memcpy(&read_set, &test->read_set, sizeof(fd_set));
489*7ab6e6acSAndroid Build Coastguard Worker 	memcpy(&write_set, &test->write_set, sizeof(fd_set));
490*7ab6e6acSAndroid Build Coastguard Worker 	iperf_time_now(&now);
491*7ab6e6acSAndroid Build Coastguard Worker 	timeout = tmr_timeout(&now);
492*7ab6e6acSAndroid Build Coastguard Worker 	result = select(test->max_fd + 1, &read_set, &write_set, NULL, timeout);
493*7ab6e6acSAndroid Build Coastguard Worker 	if (result < 0 && errno != EINTR) {
494*7ab6e6acSAndroid Build Coastguard Worker   	    i_errno = IESELECT;
495*7ab6e6acSAndroid Build Coastguard Worker 	    goto cleanup_and_fail;
496*7ab6e6acSAndroid Build Coastguard Worker 	}
497*7ab6e6acSAndroid Build Coastguard Worker 	if (result > 0) {
498*7ab6e6acSAndroid Build Coastguard Worker 	    if (FD_ISSET(test->ctrl_sck, &read_set)) {
499*7ab6e6acSAndroid Build Coastguard Worker  	        if (iperf_handle_message_client(test) < 0) {
500*7ab6e6acSAndroid Build Coastguard Worker 		    goto cleanup_and_fail;
501*7ab6e6acSAndroid Build Coastguard Worker 		}
502*7ab6e6acSAndroid Build Coastguard Worker 		FD_CLR(test->ctrl_sck, &read_set);
503*7ab6e6acSAndroid Build Coastguard Worker 	    }
504*7ab6e6acSAndroid Build Coastguard Worker 	}
505*7ab6e6acSAndroid Build Coastguard Worker 
506*7ab6e6acSAndroid Build Coastguard Worker 	if (test->state == TEST_RUNNING) {
507*7ab6e6acSAndroid Build Coastguard Worker 
508*7ab6e6acSAndroid Build Coastguard Worker 	    /* Is this our first time really running? */
509*7ab6e6acSAndroid Build Coastguard Worker 	    if (startup) {
510*7ab6e6acSAndroid Build Coastguard Worker 	        startup = 0;
511*7ab6e6acSAndroid Build Coastguard Worker 
512*7ab6e6acSAndroid Build Coastguard Worker 		// Set non-blocking for non-UDP tests
513*7ab6e6acSAndroid Build Coastguard Worker 		if (test->protocol->id != Pudp) {
514*7ab6e6acSAndroid Build Coastguard Worker 		    SLIST_FOREACH(sp, &test->streams, streams) {
515*7ab6e6acSAndroid Build Coastguard Worker 			setnonblocking(sp->socket, 1);
516*7ab6e6acSAndroid Build Coastguard Worker 		    }
517*7ab6e6acSAndroid Build Coastguard Worker 		}
518*7ab6e6acSAndroid Build Coastguard Worker 	    }
519*7ab6e6acSAndroid Build Coastguard Worker 
520*7ab6e6acSAndroid Build Coastguard Worker 
521*7ab6e6acSAndroid Build Coastguard Worker 	    if (test->mode == BIDIRECTIONAL)
522*7ab6e6acSAndroid Build Coastguard Worker 	    {
523*7ab6e6acSAndroid Build Coastguard Worker                 if (iperf_send(test, &write_set) < 0)
524*7ab6e6acSAndroid Build Coastguard Worker                     goto cleanup_and_fail;
525*7ab6e6acSAndroid Build Coastguard Worker                 if (iperf_recv(test, &read_set) < 0)
526*7ab6e6acSAndroid Build Coastguard Worker                     goto cleanup_and_fail;
527*7ab6e6acSAndroid Build Coastguard Worker 	    } else if (test->mode == SENDER) {
528*7ab6e6acSAndroid Build Coastguard Worker                 // Regular mode. Client sends.
529*7ab6e6acSAndroid Build Coastguard Worker                 if (iperf_send(test, &write_set) < 0)
530*7ab6e6acSAndroid Build Coastguard Worker                     goto cleanup_and_fail;
531*7ab6e6acSAndroid Build Coastguard Worker 	    } else {
532*7ab6e6acSAndroid Build Coastguard Worker                 // Reverse mode. Client receives.
533*7ab6e6acSAndroid Build Coastguard Worker                 if (iperf_recv(test, &read_set) < 0)
534*7ab6e6acSAndroid Build Coastguard Worker                     goto cleanup_and_fail;
535*7ab6e6acSAndroid Build Coastguard Worker 	    }
536*7ab6e6acSAndroid Build Coastguard Worker 
537*7ab6e6acSAndroid Build Coastguard Worker 
538*7ab6e6acSAndroid Build Coastguard Worker             /* Run the timers. */
539*7ab6e6acSAndroid Build Coastguard Worker             iperf_time_now(&now);
540*7ab6e6acSAndroid Build Coastguard Worker             tmr_run(&now);
541*7ab6e6acSAndroid Build Coastguard Worker 
542*7ab6e6acSAndroid Build Coastguard Worker 	    /* Is the test done yet? */
543*7ab6e6acSAndroid Build Coastguard Worker 	    if ((!test->omitting) &&
544*7ab6e6acSAndroid Build Coastguard Worker 	        ((test->duration != 0 && test->done) ||
545*7ab6e6acSAndroid Build Coastguard Worker 	         (test->settings->bytes != 0 && test->bytes_sent >= test->settings->bytes) ||
546*7ab6e6acSAndroid Build Coastguard Worker 	         (test->settings->blocks != 0 && test->blocks_sent >= test->settings->blocks))) {
547*7ab6e6acSAndroid Build Coastguard Worker 
548*7ab6e6acSAndroid Build Coastguard Worker 		// Unset non-blocking for non-UDP tests
549*7ab6e6acSAndroid Build Coastguard Worker 		if (test->protocol->id != Pudp) {
550*7ab6e6acSAndroid Build Coastguard Worker 		    SLIST_FOREACH(sp, &test->streams, streams) {
551*7ab6e6acSAndroid Build Coastguard Worker 			setnonblocking(sp->socket, 0);
552*7ab6e6acSAndroid Build Coastguard Worker 		    }
553*7ab6e6acSAndroid Build Coastguard Worker 		}
554*7ab6e6acSAndroid Build Coastguard Worker 
555*7ab6e6acSAndroid Build Coastguard Worker 		/* Yes, done!  Send TEST_END. */
556*7ab6e6acSAndroid Build Coastguard Worker 		test->done = 1;
557*7ab6e6acSAndroid Build Coastguard Worker 		cpu_util(test->cpu_util);
558*7ab6e6acSAndroid Build Coastguard Worker 		test->stats_callback(test);
559*7ab6e6acSAndroid Build Coastguard Worker 		if (iperf_set_send_state(test, TEST_END) != 0)
560*7ab6e6acSAndroid Build Coastguard Worker                     goto cleanup_and_fail;
561*7ab6e6acSAndroid Build Coastguard Worker 	    }
562*7ab6e6acSAndroid Build Coastguard Worker 	}
563*7ab6e6acSAndroid Build Coastguard Worker 	// If we're in reverse mode, continue draining the data
564*7ab6e6acSAndroid Build Coastguard Worker 	// connection(s) even if test is over.  This prevents a
565*7ab6e6acSAndroid Build Coastguard Worker 	// deadlock where the server side fills up its pipe(s)
566*7ab6e6acSAndroid Build Coastguard Worker 	// and gets blocked, so it can't receive state changes
567*7ab6e6acSAndroid Build Coastguard Worker 	// from the client side.
568*7ab6e6acSAndroid Build Coastguard Worker 	else if (test->mode == RECEIVER && test->state == TEST_END) {
569*7ab6e6acSAndroid Build Coastguard Worker 	    if (iperf_recv(test, &read_set) < 0)
570*7ab6e6acSAndroid Build Coastguard Worker 		goto cleanup_and_fail;
571*7ab6e6acSAndroid Build Coastguard Worker 	}
572*7ab6e6acSAndroid Build Coastguard Worker     }
573*7ab6e6acSAndroid Build Coastguard Worker 
574*7ab6e6acSAndroid Build Coastguard Worker     if (test->json_output) {
575*7ab6e6acSAndroid Build Coastguard Worker 	if (iperf_json_finish(test) < 0)
576*7ab6e6acSAndroid Build Coastguard Worker 	    return -1;
577*7ab6e6acSAndroid Build Coastguard Worker     } else {
578*7ab6e6acSAndroid Build Coastguard Worker 	iperf_printf(test, "\n");
579*7ab6e6acSAndroid Build Coastguard Worker 	iperf_printf(test, "%s", report_done);
580*7ab6e6acSAndroid Build Coastguard Worker     }
581*7ab6e6acSAndroid Build Coastguard Worker 
582*7ab6e6acSAndroid Build Coastguard Worker     iflush(test);
583*7ab6e6acSAndroid Build Coastguard Worker 
584*7ab6e6acSAndroid Build Coastguard Worker     return 0;
585*7ab6e6acSAndroid Build Coastguard Worker 
586*7ab6e6acSAndroid Build Coastguard Worker   cleanup_and_fail:
587*7ab6e6acSAndroid Build Coastguard Worker     iperf_client_end(test);
588*7ab6e6acSAndroid Build Coastguard Worker     if (test->json_output)
589*7ab6e6acSAndroid Build Coastguard Worker 	iperf_json_finish(test);
590*7ab6e6acSAndroid Build Coastguard Worker     iflush(test);
591*7ab6e6acSAndroid Build Coastguard Worker     return -1;
592*7ab6e6acSAndroid Build Coastguard Worker }
593