1 /*
2 * iperf, Copyright (c) 2014-2021, The Regents of the University of
3 * California, through Lawrence Berkeley National Laboratory (subject
4 * to receipt of any required approvals from the U.S. Dept. of
5 * Energy). All rights reserved.
6 *
7 * If you have questions about your rights to use or distribute this
8 * software, please contact Berkeley Lab's Technology Transfer
9 * Department at TTD@lbl.gov.
10 *
11 * NOTICE. This software is owned by the U.S. Department of Energy.
12 * As such, the U.S. Government has been granted for itself and others
13 * acting on its behalf a paid-up, nonexclusive, irrevocable,
14 * worldwide license in the Software to reproduce, prepare derivative
15 * works, and perform publicly and display publicly. Beginning five
16 * (5) years after the date permission to assert copyright is obtained
17 * from the U.S. Department of Energy, and subject to any subsequent
18 * five (5) year renewals, the U.S. Government is granted for itself
19 * and others acting on its behalf a paid-up, nonexclusive,
20 * irrevocable, worldwide license in the Software to reproduce,
21 * prepare derivative works, distribute copies to the public, perform
22 * publicly and display publicly, and to permit others to do so.
23 *
24 * This code is distributed under a BSD style license, see the LICENSE file
25 * for complete information.
26 */
27 #ifndef _GNU_SOURCE
28 # define _GNU_SOURCE
29 #endif
30 #define __USE_GNU
31
32 #include "iperf_config.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <getopt.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <unistd.h>
42 #include <assert.h>
43 #include <fcntl.h>
44 #include <sys/socket.h>
45 #include <sys/types.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <netdb.h>
49 #ifdef HAVE_STDINT_H
50 #include <stdint.h>
51 #endif
52 #include <sys/time.h>
53 #include <sys/resource.h>
54 #include <sys/mman.h>
55 #include <sys/stat.h>
56 #include <sched.h>
57 #include <setjmp.h>
58 #include <stdarg.h>
59 #include <math.h>
60
61 #if defined(HAVE_CPUSET_SETAFFINITY)
62 #include <sys/param.h>
63 #include <sys/cpuset.h>
64 #endif /* HAVE_CPUSET_SETAFFINITY */
65
66 #if defined(__CYGWIN__) || defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
67 #define CPU_SETSIZE __CPU_SETSIZE
68 #endif /* __CYGWIN__, _WIN32, _WIN64, __WINDOWS__ */
69
70 #if defined(HAVE_SETPROCESSAFFINITYMASK)
71 #include <Windows.h>
72 #endif /* HAVE_SETPROCESSAFFINITYMASK */
73
74 #include "net.h"
75 #include "iperf.h"
76 #include "iperf_api.h"
77 #include "iperf_udp.h"
78 #include "iperf_tcp.h"
79 #if defined(HAVE_SCTP_H)
80 #include "iperf_sctp.h"
81 #endif /* HAVE_SCTP_H */
82 #include "timer.h"
83
84 #include "cjson.h"
85 #include "units.h"
86 #include "iperf_util.h"
87 #include "iperf_locale.h"
88 #include "version.h"
89 #if defined(HAVE_SSL)
90 #include <openssl/bio.h>
91 #include <openssl/err.h>
92 #include "iperf_auth.h"
93 #endif /* HAVE_SSL */
94
95 /* Forwards. */
96 static int send_parameters(struct iperf_test *test);
97 static int get_parameters(struct iperf_test *test);
98 static int send_results(struct iperf_test *test);
99 static int get_results(struct iperf_test *test);
100 static int diskfile_send(struct iperf_stream *sp);
101 static int diskfile_recv(struct iperf_stream *sp);
102 static int JSON_write(int fd, cJSON *json);
103 static void print_interval_results(struct iperf_test *test, struct iperf_stream *sp, cJSON *json_interval_streams);
104 static cJSON *JSON_read(int fd);
105
106
107 /*************************** Print usage functions ****************************/
108
109 void
usage()110 usage()
111 {
112 fputs(usage_shortstr, stderr);
113 }
114
115
116 void
usage_long(FILE * f)117 usage_long(FILE *f)
118 {
119 fprintf(f, usage_longstr, DEFAULT_NO_MSG_RCVD_TIMEOUT, UDP_RATE / (1024*1024), DEFAULT_PACING_TIMER, DURATION, DEFAULT_TCP_BLKSIZE / 1024, DEFAULT_UDP_BLKSIZE);
120 }
121
122
warning(const char * str)123 void warning(const char *str)
124 {
125 fprintf(stderr, "warning: %s\n", str);
126 }
127
128
129 /************** Getter routines for some fields inside iperf_test *************/
130
131 int
iperf_get_verbose(struct iperf_test * ipt)132 iperf_get_verbose(struct iperf_test *ipt)
133 {
134 return ipt->verbose;
135 }
136
137 int
iperf_get_control_socket(struct iperf_test * ipt)138 iperf_get_control_socket(struct iperf_test *ipt)
139 {
140 return ipt->ctrl_sck;
141 }
142
143 int
iperf_get_control_socket_mss(struct iperf_test * ipt)144 iperf_get_control_socket_mss(struct iperf_test *ipt)
145 {
146 return ipt->ctrl_sck_mss;
147 }
148
149 int
iperf_get_test_omit(struct iperf_test * ipt)150 iperf_get_test_omit(struct iperf_test *ipt)
151 {
152 return ipt->omit;
153 }
154
155 int
iperf_get_test_duration(struct iperf_test * ipt)156 iperf_get_test_duration(struct iperf_test *ipt)
157 {
158 return ipt->duration;
159 }
160
161 uint64_t
iperf_get_test_rate(struct iperf_test * ipt)162 iperf_get_test_rate(struct iperf_test *ipt)
163 {
164 return ipt->settings->rate;
165 }
166
167 uint64_t
iperf_get_test_bitrate_limit(struct iperf_test * ipt)168 iperf_get_test_bitrate_limit(struct iperf_test *ipt)
169 {
170 return ipt->settings->bitrate_limit;
171 }
172
173 double
iperf_get_test_bitrate_limit_interval(struct iperf_test * ipt)174 iperf_get_test_bitrate_limit_interval(struct iperf_test *ipt)
175 {
176 return ipt->settings->bitrate_limit_interval;
177 }
178
179 int
iperf_get_test_bitrate_limit_stats_per_interval(struct iperf_test * ipt)180 iperf_get_test_bitrate_limit_stats_per_interval(struct iperf_test *ipt)
181 {
182 return ipt->settings->bitrate_limit_stats_per_interval;
183 }
184
185 uint64_t
iperf_get_test_fqrate(struct iperf_test * ipt)186 iperf_get_test_fqrate(struct iperf_test *ipt)
187 {
188 return ipt->settings->fqrate;
189 }
190
191 int
iperf_get_test_pacing_timer(struct iperf_test * ipt)192 iperf_get_test_pacing_timer(struct iperf_test *ipt)
193 {
194 return ipt->settings->pacing_timer;
195 }
196
197 uint64_t
iperf_get_test_bytes(struct iperf_test * ipt)198 iperf_get_test_bytes(struct iperf_test *ipt)
199 {
200 return (uint64_t) ipt->settings->bytes;
201 }
202
203 uint64_t
iperf_get_test_blocks(struct iperf_test * ipt)204 iperf_get_test_blocks(struct iperf_test *ipt)
205 {
206 return (uint64_t) ipt->settings->blocks;
207 }
208
209 int
iperf_get_test_burst(struct iperf_test * ipt)210 iperf_get_test_burst(struct iperf_test *ipt)
211 {
212 return ipt->settings->burst;
213 }
214
215 char
iperf_get_test_role(struct iperf_test * ipt)216 iperf_get_test_role(struct iperf_test *ipt)
217 {
218 return ipt->role;
219 }
220
221 int
iperf_get_test_reverse(struct iperf_test * ipt)222 iperf_get_test_reverse(struct iperf_test *ipt)
223 {
224 return ipt->reverse;
225 }
226
227 int
iperf_get_test_blksize(struct iperf_test * ipt)228 iperf_get_test_blksize(struct iperf_test *ipt)
229 {
230 return ipt->settings->blksize;
231 }
232
233 FILE *
iperf_get_test_outfile(struct iperf_test * ipt)234 iperf_get_test_outfile (struct iperf_test *ipt)
235 {
236 return ipt->outfile;
237 }
238
239 int
iperf_get_test_socket_bufsize(struct iperf_test * ipt)240 iperf_get_test_socket_bufsize(struct iperf_test *ipt)
241 {
242 return ipt->settings->socket_bufsize;
243 }
244
245 double
iperf_get_test_reporter_interval(struct iperf_test * ipt)246 iperf_get_test_reporter_interval(struct iperf_test *ipt)
247 {
248 return ipt->reporter_interval;
249 }
250
251 double
iperf_get_test_stats_interval(struct iperf_test * ipt)252 iperf_get_test_stats_interval(struct iperf_test *ipt)
253 {
254 return ipt->stats_interval;
255 }
256
257 int
iperf_get_test_num_streams(struct iperf_test * ipt)258 iperf_get_test_num_streams(struct iperf_test *ipt)
259 {
260 return ipt->num_streams;
261 }
262
263 int
iperf_get_test_timestamps(struct iperf_test * ipt)264 iperf_get_test_timestamps(struct iperf_test *ipt)
265 {
266 return ipt->timestamps;
267 }
268
269 const char *
iperf_get_test_timestamp_format(struct iperf_test * ipt)270 iperf_get_test_timestamp_format(struct iperf_test *ipt)
271 {
272 return ipt->timestamp_format;
273 }
274
275 int
iperf_get_test_repeating_payload(struct iperf_test * ipt)276 iperf_get_test_repeating_payload(struct iperf_test *ipt)
277 {
278 return ipt->repeating_payload;
279 }
280
281 int
iperf_get_test_server_port(struct iperf_test * ipt)282 iperf_get_test_server_port(struct iperf_test *ipt)
283 {
284 return ipt->server_port;
285 }
286
287 char*
iperf_get_test_server_hostname(struct iperf_test * ipt)288 iperf_get_test_server_hostname(struct iperf_test *ipt)
289 {
290 return ipt->server_hostname;
291 }
292
293 char*
iperf_get_test_template(struct iperf_test * ipt)294 iperf_get_test_template(struct iperf_test *ipt)
295 {
296 return ipt->tmp_template;
297 }
298
299 int
iperf_get_test_protocol_id(struct iperf_test * ipt)300 iperf_get_test_protocol_id(struct iperf_test *ipt)
301 {
302 return ipt->protocol->id;
303 }
304
305 int
iperf_get_test_json_output(struct iperf_test * ipt)306 iperf_get_test_json_output(struct iperf_test *ipt)
307 {
308 return ipt->json_output;
309 }
310
311 char *
iperf_get_test_json_output_string(struct iperf_test * ipt)312 iperf_get_test_json_output_string(struct iperf_test *ipt)
313 {
314 return ipt->json_output_string;
315 }
316
317 int
iperf_get_test_zerocopy(struct iperf_test * ipt)318 iperf_get_test_zerocopy(struct iperf_test *ipt)
319 {
320 return ipt->zerocopy;
321 }
322
323 int
iperf_get_test_get_server_output(struct iperf_test * ipt)324 iperf_get_test_get_server_output(struct iperf_test *ipt)
325 {
326 return ipt->get_server_output;
327 }
328
329 char
iperf_get_test_unit_format(struct iperf_test * ipt)330 iperf_get_test_unit_format(struct iperf_test *ipt)
331 {
332 return ipt->settings->unit_format;
333 }
334
335 char *
iperf_get_test_bind_address(struct iperf_test * ipt)336 iperf_get_test_bind_address(struct iperf_test *ipt)
337 {
338 return ipt->bind_address;
339 }
340
341 char *
iperf_get_test_bind_dev(struct iperf_test * ipt)342 iperf_get_test_bind_dev(struct iperf_test *ipt)
343 {
344 return ipt->bind_dev;
345 }
346
347 int
iperf_get_test_udp_counters_64bit(struct iperf_test * ipt)348 iperf_get_test_udp_counters_64bit(struct iperf_test *ipt)
349 {
350 return ipt->udp_counters_64bit;
351 }
352
353 int
iperf_get_test_one_off(struct iperf_test * ipt)354 iperf_get_test_one_off(struct iperf_test *ipt)
355 {
356 return ipt->one_off;
357 }
358
359 int
iperf_get_test_tos(struct iperf_test * ipt)360 iperf_get_test_tos(struct iperf_test *ipt)
361 {
362 return ipt->settings->tos;
363 }
364
365 char *
iperf_get_test_extra_data(struct iperf_test * ipt)366 iperf_get_test_extra_data(struct iperf_test *ipt)
367 {
368 return ipt->extra_data;
369 }
370
371 static const char iperf_version[] = IPERF_VERSION;
372 char *
iperf_get_iperf_version(void)373 iperf_get_iperf_version(void)
374 {
375 return (char*)iperf_version;
376 }
377
378 int
iperf_get_test_no_delay(struct iperf_test * ipt)379 iperf_get_test_no_delay(struct iperf_test *ipt)
380 {
381 return ipt->no_delay;
382 }
383
384 int
iperf_get_test_connect_timeout(struct iperf_test * ipt)385 iperf_get_test_connect_timeout(struct iperf_test *ipt)
386 {
387 return ipt->settings->connect_timeout;
388 }
389
390 int
iperf_get_test_idle_timeout(struct iperf_test * ipt)391 iperf_get_test_idle_timeout(struct iperf_test *ipt)
392 {
393 return ipt->settings->idle_timeout;
394 }
395
396 int
iperf_get_dont_fragment(struct iperf_test * ipt)397 iperf_get_dont_fragment(struct iperf_test *ipt)
398 {
399 return ipt->settings->dont_fragment;
400 }
401
402 struct iperf_time*
iperf_get_test_rcv_timeout(struct iperf_test * ipt)403 iperf_get_test_rcv_timeout(struct iperf_test *ipt)
404 {
405 return &ipt->settings->rcv_timeout;
406 }
407
408 char*
iperf_get_test_congestion_control(struct iperf_test * ipt)409 iperf_get_test_congestion_control(struct iperf_test* ipt)
410 {
411 return ipt->congestion;
412 }
413
414 /************** Setter routines for some fields inside iperf_test *************/
415
416 void
iperf_set_verbose(struct iperf_test * ipt,int verbose)417 iperf_set_verbose(struct iperf_test *ipt, int verbose)
418 {
419 ipt->verbose = verbose;
420 }
421
422 void
iperf_set_control_socket(struct iperf_test * ipt,int ctrl_sck)423 iperf_set_control_socket(struct iperf_test *ipt, int ctrl_sck)
424 {
425 ipt->ctrl_sck = ctrl_sck;
426 }
427
428 void
iperf_set_test_omit(struct iperf_test * ipt,int omit)429 iperf_set_test_omit(struct iperf_test *ipt, int omit)
430 {
431 ipt->omit = omit;
432 }
433
434 void
iperf_set_test_duration(struct iperf_test * ipt,int duration)435 iperf_set_test_duration(struct iperf_test *ipt, int duration)
436 {
437 ipt->duration = duration;
438 }
439
440 void
iperf_set_test_reporter_interval(struct iperf_test * ipt,double reporter_interval)441 iperf_set_test_reporter_interval(struct iperf_test *ipt, double reporter_interval)
442 {
443 ipt->reporter_interval = reporter_interval;
444 }
445
446 void
iperf_set_test_stats_interval(struct iperf_test * ipt,double stats_interval)447 iperf_set_test_stats_interval(struct iperf_test *ipt, double stats_interval)
448 {
449 ipt->stats_interval = stats_interval;
450 }
451
452 void
iperf_set_test_state(struct iperf_test * ipt,signed char state)453 iperf_set_test_state(struct iperf_test *ipt, signed char state)
454 {
455 ipt->state = state;
456 }
457
458 void
iperf_set_test_blksize(struct iperf_test * ipt,int blksize)459 iperf_set_test_blksize(struct iperf_test *ipt, int blksize)
460 {
461 ipt->settings->blksize = blksize;
462 }
463
464 void
iperf_set_test_logfile(struct iperf_test * ipt,const char * logfile)465 iperf_set_test_logfile(struct iperf_test *ipt, const char *logfile)
466 {
467 ipt->logfile = strdup(logfile);
468 }
469
470 void
iperf_set_test_rate(struct iperf_test * ipt,uint64_t rate)471 iperf_set_test_rate(struct iperf_test *ipt, uint64_t rate)
472 {
473 ipt->settings->rate = rate;
474 }
475
476 void
iperf_set_test_bitrate_limit_maximum(struct iperf_test * ipt,uint64_t total_rate)477 iperf_set_test_bitrate_limit_maximum(struct iperf_test *ipt, uint64_t total_rate)
478 {
479 ipt->settings->bitrate_limit = total_rate;
480 }
481
482 void
iperf_set_test_bitrate_limit_interval(struct iperf_test * ipt,uint64_t bitrate_limit_interval)483 iperf_set_test_bitrate_limit_interval(struct iperf_test *ipt, uint64_t bitrate_limit_interval)
484 {
485 ipt->settings->bitrate_limit_interval = bitrate_limit_interval;
486 }
487
488 void
iperf_set_test_bitrate_limit_stats_per_interval(struct iperf_test * ipt,uint64_t bitrate_limit_stats_per_interval)489 iperf_set_test_bitrate_limit_stats_per_interval(struct iperf_test *ipt, uint64_t bitrate_limit_stats_per_interval)
490 {
491 ipt->settings->bitrate_limit_stats_per_interval = bitrate_limit_stats_per_interval;
492 }
493
494 void
iperf_set_test_fqrate(struct iperf_test * ipt,uint64_t fqrate)495 iperf_set_test_fqrate(struct iperf_test *ipt, uint64_t fqrate)
496 {
497 ipt->settings->fqrate = fqrate;
498 }
499
500 void
iperf_set_test_pacing_timer(struct iperf_test * ipt,int pacing_timer)501 iperf_set_test_pacing_timer(struct iperf_test *ipt, int pacing_timer)
502 {
503 ipt->settings->pacing_timer = pacing_timer;
504 }
505
506 void
iperf_set_test_bytes(struct iperf_test * ipt,uint64_t bytes)507 iperf_set_test_bytes(struct iperf_test *ipt, uint64_t bytes)
508 {
509 ipt->settings->bytes = (iperf_size_t) bytes;
510 }
511
512 void
iperf_set_test_blocks(struct iperf_test * ipt,uint64_t blocks)513 iperf_set_test_blocks(struct iperf_test *ipt, uint64_t blocks)
514 {
515 ipt->settings->blocks = (iperf_size_t) blocks;
516 }
517
518 void
iperf_set_test_burst(struct iperf_test * ipt,int burst)519 iperf_set_test_burst(struct iperf_test *ipt, int burst)
520 {
521 ipt->settings->burst = burst;
522 }
523
524 void
iperf_set_test_server_port(struct iperf_test * ipt,int srv_port)525 iperf_set_test_server_port(struct iperf_test *ipt, int srv_port)
526 {
527 ipt->server_port = srv_port;
528 }
529
530 void
iperf_set_test_socket_bufsize(struct iperf_test * ipt,int socket_bufsize)531 iperf_set_test_socket_bufsize(struct iperf_test *ipt, int socket_bufsize)
532 {
533 ipt->settings->socket_bufsize = socket_bufsize;
534 }
535
536 void
iperf_set_test_num_streams(struct iperf_test * ipt,int num_streams)537 iperf_set_test_num_streams(struct iperf_test *ipt, int num_streams)
538 {
539 ipt->num_streams = num_streams;
540 }
541
542 void
iperf_set_test_repeating_payload(struct iperf_test * ipt,int repeating_payload)543 iperf_set_test_repeating_payload(struct iperf_test *ipt, int repeating_payload)
544 {
545 ipt->repeating_payload = repeating_payload;
546 }
547
548 void
iperf_set_test_timestamps(struct iperf_test * ipt,int timestamps)549 iperf_set_test_timestamps(struct iperf_test *ipt, int timestamps)
550 {
551 ipt->timestamps = timestamps;
552 }
553
554 void
iperf_set_test_timestamp_format(struct iperf_test * ipt,const char * tf)555 iperf_set_test_timestamp_format(struct iperf_test *ipt, const char *tf)
556 {
557 ipt->timestamp_format = strdup(tf);
558 }
559
560 static void
check_sender_has_retransmits(struct iperf_test * ipt)561 check_sender_has_retransmits(struct iperf_test *ipt)
562 {
563 if (ipt->mode != RECEIVER && ipt->protocol->id == Ptcp && has_tcpinfo_retransmits())
564 ipt->sender_has_retransmits = 1;
565 else
566 ipt->sender_has_retransmits = 0;
567 }
568
569 void
iperf_set_test_role(struct iperf_test * ipt,char role)570 iperf_set_test_role(struct iperf_test *ipt, char role)
571 {
572 ipt->role = role;
573 if (!ipt->reverse) {
574 if (ipt->bidirectional)
575 ipt->mode = BIDIRECTIONAL;
576 else if (role == 'c')
577 ipt->mode = SENDER;
578 else if (role == 's')
579 ipt->mode = RECEIVER;
580 } else {
581 if (role == 'c')
582 ipt->mode = RECEIVER;
583 else if (role == 's')
584 ipt->mode = SENDER;
585 }
586 check_sender_has_retransmits(ipt);
587 }
588
589 void
iperf_set_test_server_hostname(struct iperf_test * ipt,const char * server_hostname)590 iperf_set_test_server_hostname(struct iperf_test *ipt, const char *server_hostname)
591 {
592 ipt->server_hostname = strdup(server_hostname);
593 }
594
595 void
iperf_set_test_template(struct iperf_test * ipt,const char * tmp_template)596 iperf_set_test_template(struct iperf_test *ipt, const char *tmp_template)
597 {
598 ipt->tmp_template = strdup(tmp_template);
599 }
600
601 void
iperf_set_test_reverse(struct iperf_test * ipt,int reverse)602 iperf_set_test_reverse(struct iperf_test *ipt, int reverse)
603 {
604 ipt->reverse = reverse;
605 if (!ipt->reverse) {
606 if (ipt->role == 'c')
607 ipt->mode = SENDER;
608 else if (ipt->role == 's')
609 ipt->mode = RECEIVER;
610 } else {
611 if (ipt->role == 'c')
612 ipt->mode = RECEIVER;
613 else if (ipt->role == 's')
614 ipt->mode = SENDER;
615 }
616 check_sender_has_retransmits(ipt);
617 }
618
619 void
iperf_set_test_json_output(struct iperf_test * ipt,int json_output)620 iperf_set_test_json_output(struct iperf_test *ipt, int json_output)
621 {
622 ipt->json_output = json_output;
623 }
624
625 int
iperf_has_zerocopy(void)626 iperf_has_zerocopy( void )
627 {
628 return has_sendfile();
629 }
630
631 void
iperf_set_test_zerocopy(struct iperf_test * ipt,int zerocopy)632 iperf_set_test_zerocopy(struct iperf_test *ipt, int zerocopy)
633 {
634 ipt->zerocopy = (zerocopy && has_sendfile());
635 }
636
637 void
iperf_set_test_get_server_output(struct iperf_test * ipt,int get_server_output)638 iperf_set_test_get_server_output(struct iperf_test *ipt, int get_server_output)
639 {
640 ipt->get_server_output = get_server_output;
641 }
642
643 void
iperf_set_test_unit_format(struct iperf_test * ipt,char unit_format)644 iperf_set_test_unit_format(struct iperf_test *ipt, char unit_format)
645 {
646 ipt->settings->unit_format = unit_format;
647 }
648
649 #if defined(HAVE_SSL)
650 void
iperf_set_test_client_username(struct iperf_test * ipt,const char * client_username)651 iperf_set_test_client_username(struct iperf_test *ipt, const char *client_username)
652 {
653 ipt->settings->client_username = strdup(client_username);
654 }
655
656 void
iperf_set_test_client_password(struct iperf_test * ipt,const char * client_password)657 iperf_set_test_client_password(struct iperf_test *ipt, const char *client_password)
658 {
659 ipt->settings->client_password = strdup(client_password);
660 }
661
662 void
iperf_set_test_client_rsa_pubkey(struct iperf_test * ipt,const char * client_rsa_pubkey_base64)663 iperf_set_test_client_rsa_pubkey(struct iperf_test *ipt, const char *client_rsa_pubkey_base64)
664 {
665 ipt->settings->client_rsa_pubkey = load_pubkey_from_base64(client_rsa_pubkey_base64);
666 }
667
668 void
iperf_set_test_server_authorized_users(struct iperf_test * ipt,const char * server_authorized_users)669 iperf_set_test_server_authorized_users(struct iperf_test *ipt, const char *server_authorized_users)
670 {
671 ipt->server_authorized_users = strdup(server_authorized_users);
672 }
673
674 void
iperf_set_test_server_skew_threshold(struct iperf_test * ipt,int server_skew_threshold)675 iperf_set_test_server_skew_threshold(struct iperf_test *ipt, int server_skew_threshold)
676 {
677 ipt->server_skew_threshold = server_skew_threshold;
678 }
679
680 void
iperf_set_test_server_rsa_privkey(struct iperf_test * ipt,const char * server_rsa_privkey_base64)681 iperf_set_test_server_rsa_privkey(struct iperf_test *ipt, const char *server_rsa_privkey_base64)
682 {
683 ipt->server_rsa_private_key = load_privkey_from_base64(server_rsa_privkey_base64);
684 }
685 #endif // HAVE_SSL
686
687 void
iperf_set_test_bind_address(struct iperf_test * ipt,const char * bnd_address)688 iperf_set_test_bind_address(struct iperf_test *ipt, const char *bnd_address)
689 {
690 ipt->bind_address = strdup(bnd_address);
691 }
692
693 void
iperf_set_test_bind_dev(struct iperf_test * ipt,char * bnd_dev)694 iperf_set_test_bind_dev(struct iperf_test *ipt, char *bnd_dev)
695 {
696 ipt->bind_dev = strdup(bnd_dev);
697 }
698
699 void
iperf_set_test_udp_counters_64bit(struct iperf_test * ipt,int udp_counters_64bit)700 iperf_set_test_udp_counters_64bit(struct iperf_test *ipt, int udp_counters_64bit)
701 {
702 ipt->udp_counters_64bit = udp_counters_64bit;
703 }
704
705 void
iperf_set_test_one_off(struct iperf_test * ipt,int one_off)706 iperf_set_test_one_off(struct iperf_test *ipt, int one_off)
707 {
708 ipt->one_off = one_off;
709 }
710
711 void
iperf_set_test_tos(struct iperf_test * ipt,int tos)712 iperf_set_test_tos(struct iperf_test *ipt, int tos)
713 {
714 ipt->settings->tos = tos;
715 }
716
717 void
iperf_set_test_extra_data(struct iperf_test * ipt,const char * dat)718 iperf_set_test_extra_data(struct iperf_test *ipt, const char *dat)
719 {
720 ipt->extra_data = strdup(dat);
721 }
722
723 void
iperf_set_test_bidirectional(struct iperf_test * ipt,int bidirectional)724 iperf_set_test_bidirectional(struct iperf_test* ipt, int bidirectional)
725 {
726 ipt->bidirectional = bidirectional;
727 if (bidirectional)
728 ipt->mode = BIDIRECTIONAL;
729 else
730 iperf_set_test_reverse(ipt, ipt->reverse);
731 }
732
733 void
iperf_set_test_no_delay(struct iperf_test * ipt,int no_delay)734 iperf_set_test_no_delay(struct iperf_test* ipt, int no_delay)
735 {
736 ipt->no_delay = no_delay;
737 }
738
739 void
iperf_set_test_connect_timeout(struct iperf_test * ipt,int ct)740 iperf_set_test_connect_timeout(struct iperf_test* ipt, int ct)
741 {
742 ipt->settings->connect_timeout = ct;
743 }
744
745 void
iperf_set_test_idle_timeout(struct iperf_test * ipt,int to)746 iperf_set_test_idle_timeout(struct iperf_test* ipt, int to)
747 {
748 ipt->settings->idle_timeout = to;
749 }
750
751 void
iperf_set_dont_fragment(struct iperf_test * ipt,int dnf)752 iperf_set_dont_fragment(struct iperf_test* ipt, int dnf)
753 {
754 ipt->settings->dont_fragment = dnf;
755 }
756
757 void
iperf_set_test_rcv_timeout(struct iperf_test * ipt,struct iperf_time * to)758 iperf_set_test_rcv_timeout(struct iperf_test* ipt, struct iperf_time* to)
759 {
760 ipt->settings->rcv_timeout.secs = to->secs;
761 ipt->settings->rcv_timeout.usecs = to->usecs;
762 }
763
764 void
iperf_set_test_congestion_control(struct iperf_test * ipt,char * cc)765 iperf_set_test_congestion_control(struct iperf_test* ipt, char* cc)
766 {
767 ipt->congestion = strdup(cc);
768 }
769
770
771 /********************** Get/set test protocol structure ***********************/
772
773 struct protocol *
get_protocol(struct iperf_test * test,int prot_id)774 get_protocol(struct iperf_test *test, int prot_id)
775 {
776 struct protocol *prot;
777
778 SLIST_FOREACH(prot, &test->protocols, protocols) {
779 if (prot->id == prot_id)
780 break;
781 }
782
783 if (prot == NULL)
784 i_errno = IEPROTOCOL;
785
786 return prot;
787 }
788
789 int
set_protocol(struct iperf_test * test,int prot_id)790 set_protocol(struct iperf_test *test, int prot_id)
791 {
792 struct protocol *prot = NULL;
793
794 SLIST_FOREACH(prot, &test->protocols, protocols) {
795 if (prot->id == prot_id) {
796 test->protocol = prot;
797 check_sender_has_retransmits(test);
798 return 0;
799 }
800 }
801
802 i_errno = IEPROTOCOL;
803 return -1;
804 }
805
806
807 /************************** Iperf callback functions **************************/
808
809 void
iperf_on_new_stream(struct iperf_stream * sp)810 iperf_on_new_stream(struct iperf_stream *sp)
811 {
812 connect_msg(sp);
813 }
814
815 void
iperf_on_test_start(struct iperf_test * test)816 iperf_on_test_start(struct iperf_test *test)
817 {
818 if (test->json_output) {
819 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));
820 } else {
821 if (test->verbose) {
822 if (test->settings->bytes)
823 iperf_printf(test, test_start_bytes, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->settings->bytes, test->settings->tos);
824 else if (test->settings->blocks)
825 iperf_printf(test, test_start_blocks, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->settings->blocks, test->settings->tos);
826 else
827 iperf_printf(test, test_start_time, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->duration, test->settings->tos);
828 }
829 }
830 }
831
832 /* This converts an IPv6 string address from IPv4-mapped format into regular
833 ** old IPv4 format, which is easier on the eyes of network veterans.
834 **
835 ** If the v6 address is not v4-mapped it is left alone.
836 */
837 static void
mapped_v4_to_regular_v4(char * str)838 mapped_v4_to_regular_v4(char *str)
839 {
840 char *prefix = "::ffff:";
841 int prefix_len;
842
843 prefix_len = strlen(prefix);
844 if (strncmp(str, prefix, prefix_len) == 0) {
845 int str_len = strlen(str);
846 memmove(str, str + prefix_len, str_len - prefix_len + 1);
847 }
848 }
849
850 void
iperf_on_connect(struct iperf_test * test)851 iperf_on_connect(struct iperf_test *test)
852 {
853 time_t now_secs;
854 const char* rfc1123_fmt = "%a, %d %b %Y %H:%M:%S %Z";
855 char now_str[100];
856 char ipr[INET6_ADDRSTRLEN];
857 int port;
858 struct sockaddr_storage sa;
859 struct sockaddr_in *sa_inP;
860 struct sockaddr_in6 *sa_in6P;
861 socklen_t len;
862
863 now_secs = time((time_t*) 0);
864 (void) strftime(now_str, sizeof(now_str), rfc1123_fmt, gmtime(&now_secs));
865 if (test->json_output)
866 cJSON_AddItemToObject(test->json_start, "timestamp", iperf_json_printf("time: %s timesecs: %d", now_str, (int64_t) now_secs));
867 else if (test->verbose)
868 iperf_printf(test, report_time, now_str);
869
870 if (test->role == 'c') {
871 if (test->json_output)
872 cJSON_AddItemToObject(test->json_start, "connecting_to", iperf_json_printf("host: %s port: %d", test->server_hostname, (int64_t) test->server_port));
873 else {
874 iperf_printf(test, report_connecting, test->server_hostname, test->server_port);
875 if (test->reverse)
876 iperf_printf(test, report_reverse, test->server_hostname);
877 }
878 } else {
879 len = sizeof(sa);
880 getpeername(test->ctrl_sck, (struct sockaddr *) &sa, &len);
881 if (getsockdomain(test->ctrl_sck) == AF_INET) {
882 sa_inP = (struct sockaddr_in *) &sa;
883 inet_ntop(AF_INET, &sa_inP->sin_addr, ipr, sizeof(ipr));
884 port = ntohs(sa_inP->sin_port);
885 } else {
886 sa_in6P = (struct sockaddr_in6 *) &sa;
887 inet_ntop(AF_INET6, &sa_in6P->sin6_addr, ipr, sizeof(ipr));
888 port = ntohs(sa_in6P->sin6_port);
889 }
890 mapped_v4_to_regular_v4(ipr);
891 if (test->json_output)
892 cJSON_AddItemToObject(test->json_start, "accepted_connection", iperf_json_printf("host: %s port: %d", ipr, (int64_t) port));
893 else
894 iperf_printf(test, report_accepted, ipr, port);
895 }
896 if (test->json_output) {
897 cJSON_AddStringToObject(test->json_start, "cookie", test->cookie);
898 if (test->protocol->id == SOCK_STREAM) {
899 if (test->settings->mss)
900 cJSON_AddNumberToObject(test->json_start, "tcp_mss", test->settings->mss);
901 else {
902 cJSON_AddNumberToObject(test->json_start, "tcp_mss_default", test->ctrl_sck_mss);
903 }
904 if (test->settings->rate)
905 cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate);
906 }
907 } else if (test->verbose) {
908 iperf_printf(test, report_cookie, test->cookie);
909 if (test->protocol->id == SOCK_STREAM) {
910 if (test->settings->mss)
911 iperf_printf(test, " TCP MSS: %d\n", test->settings->mss);
912 else {
913 iperf_printf(test, " TCP MSS: %d (default)\n", test->ctrl_sck_mss);
914 }
915 }
916 if (test->settings->rate)
917 iperf_printf(test, " Target Bitrate: %"PRIu64"\n", test->settings->rate);
918 }
919 }
920
921 void
iperf_on_test_finish(struct iperf_test * test)922 iperf_on_test_finish(struct iperf_test *test)
923 {
924 }
925
926
927 /******************************************************************************/
928
929 int
iperf_parse_arguments(struct iperf_test * test,int argc,char ** argv)930 iperf_parse_arguments(struct iperf_test *test, int argc, char **argv)
931 {
932 static struct option longopts[] =
933 {
934 {"port", required_argument, NULL, 'p'},
935 {"format", required_argument, NULL, 'f'},
936 {"interval", required_argument, NULL, 'i'},
937 {"daemon", no_argument, NULL, 'D'},
938 {"one-off", no_argument, NULL, '1'},
939 {"verbose", no_argument, NULL, 'V'},
940 {"json", no_argument, NULL, 'J'},
941 {"version", no_argument, NULL, 'v'},
942 {"server", no_argument, NULL, 's'},
943 {"client", required_argument, NULL, 'c'},
944 {"udp", no_argument, NULL, 'u'},
945 {"bitrate", required_argument, NULL, 'b'},
946 {"bandwidth", required_argument, NULL, 'b'},
947 {"server-bitrate-limit", required_argument, NULL, OPT_SERVER_BITRATE_LIMIT},
948 {"time", required_argument, NULL, 't'},
949 {"bytes", required_argument, NULL, 'n'},
950 {"blockcount", required_argument, NULL, 'k'},
951 {"length", required_argument, NULL, 'l'},
952 {"parallel", required_argument, NULL, 'P'},
953 {"reverse", no_argument, NULL, 'R'},
954 {"bidir", no_argument, NULL, OPT_BIDIRECTIONAL},
955 {"window", required_argument, NULL, 'w'},
956 {"bind", required_argument, NULL, 'B'},
957 #if defined(HAVE_SO_BINDTODEVICE)
958 {"bind-dev", required_argument, NULL, OPT_BIND_DEV},
959 #endif /* HAVE_SO_BINDTODEVICE */
960 {"cport", required_argument, NULL, OPT_CLIENT_PORT},
961 {"set-mss", required_argument, NULL, 'M'},
962 {"no-delay", no_argument, NULL, 'N'},
963 {"version4", no_argument, NULL, '4'},
964 {"version6", no_argument, NULL, '6'},
965 {"tos", required_argument, NULL, 'S'},
966 {"dscp", required_argument, NULL, OPT_DSCP},
967 {"extra-data", required_argument, NULL, OPT_EXTRA_DATA},
968 #if defined(HAVE_FLOWLABEL)
969 {"flowlabel", required_argument, NULL, 'L'},
970 #endif /* HAVE_FLOWLABEL */
971 {"zerocopy", no_argument, NULL, 'Z'},
972 {"omit", required_argument, NULL, 'O'},
973 {"file", required_argument, NULL, 'F'},
974 {"repeating-payload", no_argument, NULL, OPT_REPEATING_PAYLOAD},
975 {"timestamps", optional_argument, NULL, OPT_TIMESTAMPS},
976 #if defined(HAVE_CPU_AFFINITY)
977 {"affinity", required_argument, NULL, 'A'},
978 #endif /* HAVE_CPU_AFFINITY */
979 {"title", required_argument, NULL, 'T'},
980 #if defined(HAVE_TCP_CONGESTION)
981 {"congestion", required_argument, NULL, 'C'},
982 {"linux-congestion", required_argument, NULL, 'C'},
983 #endif /* HAVE_TCP_CONGESTION */
984 #if defined(HAVE_SCTP_H)
985 {"sctp", no_argument, NULL, OPT_SCTP},
986 {"nstreams", required_argument, NULL, OPT_NUMSTREAMS},
987 {"xbind", required_argument, NULL, 'X'},
988 #endif
989 {"pidfile", required_argument, NULL, 'I'},
990 {"logfile", required_argument, NULL, OPT_LOGFILE},
991 {"forceflush", no_argument, NULL, OPT_FORCEFLUSH},
992 {"get-server-output", no_argument, NULL, OPT_GET_SERVER_OUTPUT},
993 {"udp-counters-64bit", no_argument, NULL, OPT_UDP_COUNTERS_64BIT},
994 {"no-fq-socket-pacing", no_argument, NULL, OPT_NO_FQ_SOCKET_PACING},
995 #if defined(HAVE_DONT_FRAGMENT)
996 {"dont-fragment", no_argument, NULL, OPT_DONT_FRAGMENT},
997 #endif /* HAVE_DONT_FRAGMENT */
998 #if defined(HAVE_SSL)
999 {"username", required_argument, NULL, OPT_CLIENT_USERNAME},
1000 {"rsa-public-key-path", required_argument, NULL, OPT_CLIENT_RSA_PUBLIC_KEY},
1001 {"rsa-private-key-path", required_argument, NULL, OPT_SERVER_RSA_PRIVATE_KEY},
1002 {"authorized-users-path", required_argument, NULL, OPT_SERVER_AUTHORIZED_USERS},
1003 {"time-skew-threshold", required_argument, NULL, OPT_SERVER_SKEW_THRESHOLD},
1004 #endif /* HAVE_SSL */
1005 {"fq-rate", required_argument, NULL, OPT_FQ_RATE},
1006 {"pacing-timer", required_argument, NULL, OPT_PACING_TIMER},
1007 {"connect-timeout", required_argument, NULL, OPT_CONNECT_TIMEOUT},
1008 {"idle-timeout", required_argument, NULL, OPT_IDLE_TIMEOUT},
1009 {"rcv-timeout", required_argument, NULL, OPT_RCV_TIMEOUT},
1010 {"debug", no_argument, NULL, 'd'},
1011 {"help", no_argument, NULL, 'h'},
1012 {NULL, 0, NULL, 0}
1013 };
1014 int flag;
1015 int portno;
1016 int blksize;
1017 int server_flag, client_flag, rate_flag, duration_flag, rcv_timeout_flag;
1018 char *endptr;
1019 #if defined(HAVE_CPU_AFFINITY)
1020 char* comma;
1021 #endif /* HAVE_CPU_AFFINITY */
1022 char* slash;
1023 struct xbind_entry *xbe;
1024 double farg;
1025 int rcv_timeout_in = 0;
1026
1027 blksize = 0;
1028 server_flag = client_flag = rate_flag = duration_flag = rcv_timeout_flag = 0;
1029 #if defined(HAVE_SSL)
1030 char *client_username = NULL, *client_rsa_public_key = NULL, *server_rsa_private_key = NULL;
1031 #endif /* HAVE_SSL */
1032
1033 while ((flag = getopt_long(argc, argv, "p:f:i:D1VJvsc:ub:t:n:k:l:P:Rw:B:M:N46S:L:ZO:F:A:T:C:dI:hX:", longopts, NULL)) != -1) {
1034 switch (flag) {
1035 case 'p':
1036 portno = atoi(optarg);
1037 if (portno < 1 || portno > 65535) {
1038 i_errno = IEBADPORT;
1039 return -1;
1040 }
1041 test->server_port = portno;
1042 break;
1043 case 'f':
1044 if (!optarg) {
1045 i_errno = IEBADFORMAT;
1046 return -1;
1047 }
1048 test->settings->unit_format = *optarg;
1049 if (test->settings->unit_format == 'k' ||
1050 test->settings->unit_format == 'K' ||
1051 test->settings->unit_format == 'm' ||
1052 test->settings->unit_format == 'M' ||
1053 test->settings->unit_format == 'g' ||
1054 test->settings->unit_format == 'G' ||
1055 test->settings->unit_format == 't' ||
1056 test->settings->unit_format == 'T') {
1057 break;
1058 }
1059 else {
1060 i_errno = IEBADFORMAT;
1061 return -1;
1062 }
1063 break;
1064 case 'i':
1065 /* XXX: could potentially want separate stat collection and reporting intervals,
1066 but just set them to be the same for now */
1067 test->stats_interval = test->reporter_interval = atof(optarg);
1068 if ((test->stats_interval < MIN_INTERVAL || test->stats_interval > MAX_INTERVAL) && test->stats_interval != 0) {
1069 i_errno = IEINTERVAL;
1070 return -1;
1071 }
1072 break;
1073 case 'D':
1074 test->daemon = 1;
1075 server_flag = 1;
1076 break;
1077 case '1':
1078 test->one_off = 1;
1079 server_flag = 1;
1080 break;
1081 case 'V':
1082 test->verbose = 1;
1083 break;
1084 case 'J':
1085 test->json_output = 1;
1086 break;
1087 case 'v':
1088 printf("%s (cJSON %s)\n%s\n%s\n", version, cJSON_Version(), get_system_info(),
1089 get_optional_features());
1090 exit(0);
1091 case 's':
1092 if (test->role == 'c') {
1093 i_errno = IESERVCLIENT;
1094 return -1;
1095 }
1096 iperf_set_test_role(test, 's');
1097 break;
1098 case 'c':
1099 if (test->role == 's') {
1100 i_errno = IESERVCLIENT;
1101 return -1;
1102 }
1103 iperf_set_test_role(test, 'c');
1104 iperf_set_test_server_hostname(test, optarg);
1105 break;
1106 case 'u':
1107 set_protocol(test, Pudp);
1108 client_flag = 1;
1109 break;
1110 case OPT_SCTP:
1111 #if defined(HAVE_SCTP_H)
1112 set_protocol(test, Psctp);
1113 client_flag = 1;
1114 break;
1115 #else /* HAVE_SCTP_H */
1116 i_errno = IEUNIMP;
1117 return -1;
1118 #endif /* HAVE_SCTP_H */
1119
1120 case OPT_NUMSTREAMS:
1121 #if defined(linux) || defined(__FreeBSD__)
1122 test->settings->num_ostreams = unit_atoi(optarg);
1123 client_flag = 1;
1124 #else /* linux */
1125 i_errno = IEUNIMP;
1126 return -1;
1127 #endif /* linux */
1128 case 'b':
1129 slash = strchr(optarg, '/');
1130 if (slash) {
1131 *slash = '\0';
1132 ++slash;
1133 test->settings->burst = atoi(slash);
1134 if (test->settings->burst <= 0 ||
1135 test->settings->burst > MAX_BURST) {
1136 i_errno = IEBURST;
1137 return -1;
1138 }
1139 }
1140 test->settings->rate = unit_atof_rate(optarg);
1141 rate_flag = 1;
1142 client_flag = 1;
1143 break;
1144 case OPT_SERVER_BITRATE_LIMIT:
1145 slash = strchr(optarg, '/');
1146 if (slash) {
1147 *slash = '\0';
1148 ++slash;
1149 test->settings->bitrate_limit_interval = atof(slash);
1150 if (test->settings->bitrate_limit_interval != 0 && /* Using same Max/Min limits as for Stats Interval */
1151 (test->settings->bitrate_limit_interval < MIN_INTERVAL || test->settings->bitrate_limit_interval > MAX_INTERVAL) ) {
1152 i_errno = IETOTALINTERVAL;
1153 return -1;
1154 }
1155 }
1156 test->settings->bitrate_limit = unit_atof_rate(optarg);
1157 server_flag = 1;
1158 break;
1159 case 't':
1160 test->duration = atoi(optarg);
1161 if (test->duration > MAX_TIME) {
1162 i_errno = IEDURATION;
1163 return -1;
1164 }
1165 duration_flag = 1;
1166 client_flag = 1;
1167 break;
1168 case 'n':
1169 test->settings->bytes = unit_atoi(optarg);
1170 client_flag = 1;
1171 break;
1172 case 'k':
1173 test->settings->blocks = unit_atoi(optarg);
1174 client_flag = 1;
1175 break;
1176 case 'l':
1177 blksize = unit_atoi(optarg);
1178 client_flag = 1;
1179 break;
1180 case 'P':
1181 test->num_streams = atoi(optarg);
1182 if (test->num_streams > MAX_STREAMS) {
1183 i_errno = IENUMSTREAMS;
1184 return -1;
1185 }
1186 client_flag = 1;
1187 break;
1188 case 'R':
1189 if (test->bidirectional) {
1190 i_errno = IEREVERSEBIDIR;
1191 return -1;
1192 }
1193 iperf_set_test_reverse(test, 1);
1194 client_flag = 1;
1195 break;
1196 case OPT_BIDIRECTIONAL:
1197 if (test->reverse) {
1198 i_errno = IEREVERSEBIDIR;
1199 return -1;
1200 }
1201 iperf_set_test_bidirectional(test, 1);
1202 client_flag = 1;
1203 break;
1204 case 'w':
1205 // XXX: This is a socket buffer, not specific to TCP
1206 // Do sanity checks as double-precision floating point
1207 // to avoid possible integer overflows.
1208 farg = unit_atof(optarg);
1209 if (farg > (double) MAX_TCP_BUFFER) {
1210 i_errno = IEBUFSIZE;
1211 return -1;
1212 }
1213 test->settings->socket_bufsize = (int) farg;
1214 client_flag = 1;
1215 break;
1216 case 'B':
1217 test->bind_address = strdup(optarg);
1218 break;
1219 #if defined (HAVE_SO_BINDTODEVICE)
1220 case OPT_BIND_DEV:
1221 test->bind_dev = strdup(optarg);
1222 break;
1223 #endif /* HAVE_SO_BINDTODEVICE */
1224 case OPT_CLIENT_PORT:
1225 portno = atoi(optarg);
1226 if (portno < 1 || portno > 65535) {
1227 i_errno = IEBADPORT;
1228 return -1;
1229 }
1230 test->bind_port = portno;
1231 break;
1232 case 'M':
1233 test->settings->mss = atoi(optarg);
1234 if (test->settings->mss > MAX_MSS) {
1235 i_errno = IEMSS;
1236 return -1;
1237 }
1238 client_flag = 1;
1239 break;
1240 case 'N':
1241 test->no_delay = 1;
1242 client_flag = 1;
1243 break;
1244 case '4':
1245 test->settings->domain = AF_INET;
1246 break;
1247 case '6':
1248 test->settings->domain = AF_INET6;
1249 break;
1250 case 'S':
1251 test->settings->tos = strtol(optarg, &endptr, 0);
1252 if (endptr == optarg ||
1253 test->settings->tos < 0 ||
1254 test->settings->tos > 255) {
1255 i_errno = IEBADTOS;
1256 return -1;
1257 }
1258 client_flag = 1;
1259 break;
1260 case OPT_DSCP:
1261 test->settings->tos = parse_qos(optarg);
1262 if(test->settings->tos < 0) {
1263 i_errno = IEBADTOS;
1264 return -1;
1265 }
1266 client_flag = 1;
1267 break;
1268 case OPT_EXTRA_DATA:
1269 test->extra_data = strdup(optarg);
1270 client_flag = 1;
1271 break;
1272 case 'L':
1273 #if defined(HAVE_FLOWLABEL)
1274 test->settings->flowlabel = strtol(optarg, &endptr, 0);
1275 if (endptr == optarg ||
1276 test->settings->flowlabel < 1 || test->settings->flowlabel > 0xfffff) {
1277 i_errno = IESETFLOW;
1278 return -1;
1279 }
1280 client_flag = 1;
1281 #else /* HAVE_FLOWLABEL */
1282 i_errno = IEUNIMP;
1283 return -1;
1284 #endif /* HAVE_FLOWLABEL */
1285 break;
1286 case 'X':
1287 xbe = (struct xbind_entry *)malloc(sizeof(struct xbind_entry));
1288 if (!xbe) {
1289 i_errno = IESETSCTPBINDX;
1290 return -1;
1291 }
1292 memset(xbe, 0, sizeof(*xbe));
1293 xbe->name = strdup(optarg);
1294 if (!xbe->name) {
1295 i_errno = IESETSCTPBINDX;
1296 return -1;
1297 }
1298 TAILQ_INSERT_TAIL(&test->xbind_addrs, xbe, link);
1299 break;
1300 case 'Z':
1301 if (!has_sendfile()) {
1302 i_errno = IENOSENDFILE;
1303 return -1;
1304 }
1305 test->zerocopy = 1;
1306 client_flag = 1;
1307 break;
1308 case OPT_REPEATING_PAYLOAD:
1309 test->repeating_payload = 1;
1310 client_flag = 1;
1311 break;
1312 case OPT_TIMESTAMPS:
1313 iperf_set_test_timestamps(test, 1);
1314 if (optarg) {
1315 iperf_set_test_timestamp_format(test, optarg);
1316 }
1317 else {
1318 iperf_set_test_timestamp_format(test, TIMESTAMP_FORMAT);
1319 }
1320 break;
1321 case 'O':
1322 test->omit = atoi(optarg);
1323 if (test->omit < 0 || test->omit > 60) {
1324 i_errno = IEOMIT;
1325 return -1;
1326 }
1327 client_flag = 1;
1328 break;
1329 case 'F':
1330 test->diskfile_name = optarg;
1331 break;
1332 case OPT_IDLE_TIMEOUT:
1333 test->settings->idle_timeout = atoi(optarg);
1334 if (test->settings->idle_timeout < 1 || test->settings->idle_timeout > MAX_TIME) {
1335 i_errno = IEIDLETIMEOUT;
1336 return -1;
1337 }
1338 server_flag = 1;
1339 break;
1340 case OPT_RCV_TIMEOUT:
1341 rcv_timeout_in = atoi(optarg);
1342 if (rcv_timeout_in < MIN_NO_MSG_RCVD_TIMEOUT || rcv_timeout_in > MAX_TIME * SEC_TO_mS) {
1343 i_errno = IERCVTIMEOUT;
1344 return -1;
1345 }
1346 test->settings->rcv_timeout.secs = rcv_timeout_in / SEC_TO_mS;
1347 test->settings->rcv_timeout.usecs = (rcv_timeout_in % SEC_TO_mS) * mS_TO_US;
1348 rcv_timeout_flag = 1;
1349 break;
1350 case 'A':
1351 #if defined(HAVE_CPU_AFFINITY)
1352 test->affinity = strtol(optarg, &endptr, 0);
1353 if (endptr == optarg ||
1354 test->affinity < 0 || test->affinity > 1024) {
1355 i_errno = IEAFFINITY;
1356 return -1;
1357 }
1358 comma = strchr(optarg, ',');
1359 if (comma != NULL) {
1360 test->server_affinity = atoi(comma+1);
1361 if (test->server_affinity < 0 || test->server_affinity > 1024) {
1362 i_errno = IEAFFINITY;
1363 return -1;
1364 }
1365 client_flag = 1;
1366 }
1367 #else /* HAVE_CPU_AFFINITY */
1368 i_errno = IEUNIMP;
1369 return -1;
1370 #endif /* HAVE_CPU_AFFINITY */
1371 break;
1372 case 'T':
1373 test->title = strdup(optarg);
1374 client_flag = 1;
1375 break;
1376 case 'C':
1377 #if defined(HAVE_TCP_CONGESTION)
1378 test->congestion = strdup(optarg);
1379 client_flag = 1;
1380 #else /* HAVE_TCP_CONGESTION */
1381 i_errno = IEUNIMP;
1382 return -1;
1383 #endif /* HAVE_TCP_CONGESTION */
1384 break;
1385 case 'd':
1386 test->debug = 1;
1387 break;
1388 case 'I':
1389 test->pidfile = strdup(optarg);
1390 break;
1391 case OPT_LOGFILE:
1392 test->logfile = strdup(optarg);
1393 break;
1394 case OPT_FORCEFLUSH:
1395 test->forceflush = 1;
1396 break;
1397 case OPT_GET_SERVER_OUTPUT:
1398 test->get_server_output = 1;
1399 client_flag = 1;
1400 break;
1401 case OPT_UDP_COUNTERS_64BIT:
1402 test->udp_counters_64bit = 1;
1403 break;
1404 case OPT_NO_FQ_SOCKET_PACING:
1405 #if defined(HAVE_SO_MAX_PACING_RATE)
1406 printf("Warning: --no-fq-socket-pacing is deprecated\n");
1407 test->settings->fqrate = 0;
1408 client_flag = 1;
1409 #else /* HAVE_SO_MAX_PACING_RATE */
1410 i_errno = IEUNIMP;
1411 return -1;
1412 #endif
1413 break;
1414 case OPT_FQ_RATE:
1415 #if defined(HAVE_SO_MAX_PACING_RATE)
1416 test->settings->fqrate = unit_atof_rate(optarg);
1417 client_flag = 1;
1418 #else /* HAVE_SO_MAX_PACING_RATE */
1419 i_errno = IEUNIMP;
1420 return -1;
1421 #endif
1422 break;
1423 #if defined(HAVE_DONT_FRAGMENT)
1424 case OPT_DONT_FRAGMENT:
1425 test->settings->dont_fragment = 1;
1426 client_flag = 1;
1427 break;
1428 #endif /* HAVE_DONT_FRAGMENT */
1429 #if defined(HAVE_SSL)
1430 case OPT_CLIENT_USERNAME:
1431 client_username = strdup(optarg);
1432 break;
1433 case OPT_CLIENT_RSA_PUBLIC_KEY:
1434 client_rsa_public_key = strdup(optarg);
1435 break;
1436 case OPT_SERVER_RSA_PRIVATE_KEY:
1437 server_rsa_private_key = strdup(optarg);
1438 break;
1439 case OPT_SERVER_AUTHORIZED_USERS:
1440 test->server_authorized_users = strdup(optarg);
1441 break;
1442 case OPT_SERVER_SKEW_THRESHOLD:
1443 test->server_skew_threshold = atoi(optarg);
1444 if(test->server_skew_threshold <= 0){
1445 i_errno = IESKEWTHRESHOLD;
1446 return -1;
1447 }
1448 break;
1449 #endif /* HAVE_SSL */
1450 case OPT_PACING_TIMER:
1451 test->settings->pacing_timer = unit_atoi(optarg);
1452 client_flag = 1;
1453 break;
1454 case OPT_CONNECT_TIMEOUT:
1455 test->settings->connect_timeout = unit_atoi(optarg);
1456 client_flag = 1;
1457 break;
1458 case 'h':
1459 usage_long(stdout);
1460 exit(0);
1461 default:
1462 usage_long(stderr);
1463 exit(1);
1464 }
1465 }
1466
1467 /* Check flag / role compatibility. */
1468 if (test->role == 'c' && server_flag) {
1469 i_errno = IESERVERONLY;
1470 return -1;
1471 }
1472 if (test->role == 's' && client_flag) {
1473 i_errno = IECLIENTONLY;
1474 return -1;
1475 }
1476
1477 #if defined(HAVE_SSL)
1478
1479 if (test->role == 's' && (client_username || client_rsa_public_key)){
1480 i_errno = IECLIENTONLY;
1481 return -1;
1482 } else if (test->role == 'c' && (client_username || client_rsa_public_key) &&
1483 !(client_username && client_rsa_public_key)) {
1484 i_errno = IESETCLIENTAUTH;
1485 return -1;
1486 } else if (test->role == 'c' && (client_username && client_rsa_public_key)){
1487
1488 char *client_password = NULL;
1489 size_t s;
1490 /* Need to copy env var, so we can do a common free */
1491 if ((client_password = getenv("IPERF3_PASSWORD")) != NULL)
1492 client_password = strdup(client_password);
1493 else if (iperf_getpass(&client_password, &s, stdin) < 0){
1494 i_errno = IESETCLIENTAUTH;
1495 return -1;
1496 }
1497 if (test_load_pubkey_from_file(client_rsa_public_key) < 0){
1498 iperf_err(test, "%s\n", ERR_error_string(ERR_get_error(), NULL));
1499 i_errno = IESETCLIENTAUTH;
1500 return -1;
1501 }
1502
1503 test->settings->client_username = client_username;
1504 test->settings->client_password = client_password;
1505 test->settings->client_rsa_pubkey = load_pubkey_from_file(client_rsa_public_key);
1506 free(client_rsa_public_key);
1507 client_rsa_public_key = NULL;
1508 }
1509
1510 if (test->role == 'c' && (server_rsa_private_key || test->server_authorized_users)){
1511 i_errno = IESERVERONLY;
1512 return -1;
1513 } else if (test->role == 'c' && (test->server_skew_threshold != 0)){
1514 i_errno = IESERVERONLY;
1515 return -1;
1516 } else if (test->role == 'c' && rcv_timeout_flag && test->mode == SENDER){
1517 i_errno = IERVRSONLYRCVTIMEOUT;
1518 return -1;
1519 } else if (test->role == 's' && (server_rsa_private_key || test->server_authorized_users) &&
1520 !(server_rsa_private_key && test->server_authorized_users)) {
1521 i_errno = IESETSERVERAUTH;
1522 return -1;
1523 } else if (test->role == 's' && server_rsa_private_key) {
1524 test->server_rsa_private_key = load_privkey_from_file(server_rsa_private_key);
1525 if (test->server_rsa_private_key == NULL){
1526 iperf_err(test, "%s\n", ERR_error_string(ERR_get_error(), NULL));
1527 i_errno = IESETSERVERAUTH;
1528 return -1;
1529 }
1530 free(server_rsa_private_key);
1531 server_rsa_private_key = NULL;
1532
1533 if(test->server_skew_threshold == 0){
1534 // Set default value for time skew threshold
1535 test->server_skew_threshold=10;
1536 }
1537 }
1538
1539 #endif //HAVE_SSL
1540 if (blksize == 0) {
1541 if (test->protocol->id == Pudp)
1542 blksize = 0; /* try to dynamically determine from MSS */
1543 else if (test->protocol->id == Psctp)
1544 blksize = DEFAULT_SCTP_BLKSIZE;
1545 else
1546 blksize = DEFAULT_TCP_BLKSIZE;
1547 }
1548 if ((test->protocol->id != Pudp && blksize <= 0)
1549 || blksize > MAX_BLOCKSIZE) {
1550 i_errno = IEBLOCKSIZE;
1551 return -1;
1552 }
1553 if (test->protocol->id == Pudp &&
1554 (blksize > 0 &&
1555 (blksize < MIN_UDP_BLOCKSIZE || blksize > MAX_UDP_BLOCKSIZE))) {
1556 i_errno = IEUDPBLOCKSIZE;
1557 return -1;
1558 }
1559 test->settings->blksize = blksize;
1560
1561 if (!rate_flag)
1562 test->settings->rate = test->protocol->id == Pudp ? UDP_RATE : 0;
1563
1564 if ((test->settings->bytes != 0 || test->settings->blocks != 0) && ! duration_flag)
1565 test->duration = 0;
1566
1567 /* Disallow specifying multiple test end conditions. The code actually
1568 ** works just fine without this prohibition. As soon as any one of the
1569 ** three possible end conditions is met, the test ends. So this check
1570 ** could be removed if desired.
1571 */
1572 if ((duration_flag && test->settings->bytes != 0) ||
1573 (duration_flag && test->settings->blocks != 0) ||
1574 (test->settings->bytes != 0 && test->settings->blocks != 0)) {
1575 i_errno = IEENDCONDITIONS;
1576 return -1;
1577 }
1578
1579 /* For subsequent calls to getopt */
1580 #ifdef __APPLE__
1581 optreset = 1;
1582 #endif
1583 optind = 0;
1584
1585 if ((test->role != 'c') && (test->role != 's')) {
1586 i_errno = IENOROLE;
1587 return -1;
1588 }
1589
1590 /* Set Total-rate average interval to multiplicity of State interval */
1591 if (test->settings->bitrate_limit_interval != 0) {
1592 test->settings->bitrate_limit_stats_per_interval =
1593 (test->settings->bitrate_limit_interval <= test->stats_interval ?
1594 1 : round(test->settings->bitrate_limit_interval/test->stats_interval) );
1595 }
1596
1597 /* Show warning if JSON output is used with explicit report format */
1598 if ((test->json_output) && (test->settings->unit_format != 'a')) {
1599 warning("Report format (-f) flag ignored with JSON output (-J)");
1600 }
1601
1602 /* Show warning if JSON output is used with verbose or debug flags */
1603 if (test->json_output && test->verbose) {
1604 warning("Verbose output (-v) may interfere with JSON output (-J)");
1605 }
1606 if (test->json_output && test->debug) {
1607 warning("Debug output (-d) may interfere with JSON output (-J)");
1608 }
1609
1610 return 0;
1611 }
1612
1613 /*
1614 * Open the file specified by test->logfile and set test->outfile to its' FD.
1615 */
iperf_open_logfile(struct iperf_test * test)1616 int iperf_open_logfile(struct iperf_test *test)
1617 {
1618 test->outfile = fopen(test->logfile, "a+");
1619 if (test->outfile == NULL) {
1620 i_errno = IELOGFILE;
1621 return -1;
1622 }
1623
1624 return 0;
1625 }
1626
1627 int
iperf_set_send_state(struct iperf_test * test,signed char state)1628 iperf_set_send_state(struct iperf_test *test, signed char state)
1629 {
1630 if (test->ctrl_sck >= 0) {
1631 test->state = state;
1632 if (Nwrite(test->ctrl_sck, (char*) &state, sizeof(state), Ptcp) < 0) {
1633 i_errno = IESENDMESSAGE;
1634 return -1;
1635 }
1636 }
1637 return 0;
1638 }
1639
1640 void
iperf_check_throttle(struct iperf_stream * sp,struct iperf_time * nowP)1641 iperf_check_throttle(struct iperf_stream *sp, struct iperf_time *nowP)
1642 {
1643 struct iperf_time temp_time;
1644 double seconds;
1645 uint64_t bits_per_second;
1646
1647 if (sp->test->done || sp->test->settings->rate == 0)
1648 return;
1649 iperf_time_diff(&sp->result->start_time_fixed, nowP, &temp_time);
1650 seconds = iperf_time_in_secs(&temp_time);
1651 bits_per_second = sp->result->bytes_sent * 8 / seconds;
1652 if (bits_per_second < sp->test->settings->rate) {
1653 sp->green_light = 1;
1654 FD_SET(sp->socket, &sp->test->write_set);
1655 } else {
1656 sp->green_light = 0;
1657 FD_CLR(sp->socket, &sp->test->write_set);
1658 }
1659 }
1660
1661 /* Verify that average traffic is not greater than the specifid limit */
1662 void
iperf_check_total_rate(struct iperf_test * test,iperf_size_t last_interval_bytes_transferred)1663 iperf_check_total_rate(struct iperf_test *test, iperf_size_t last_interval_bytes_transferred)
1664 {
1665 double seconds;
1666 uint64_t bits_per_second;
1667 iperf_size_t total_bytes;
1668 int i;
1669
1670 if (test->done || test->settings->bitrate_limit == 0) // Continue only if check should be done
1671 return;
1672
1673 /* Add last inetrval's transffered bytes to the array */
1674 if (++test->bitrate_limit_last_interval_index >= test->settings->bitrate_limit_stats_per_interval)
1675 test->bitrate_limit_last_interval_index = 0;
1676 test->bitrate_limit_intervals_traffic_bytes[test->bitrate_limit_last_interval_index] = last_interval_bytes_transferred;
1677
1678 /* Ensure that enough stats periods passed to allow averaging throughput */
1679 test->bitrate_limit_stats_count += 1;
1680 if (test->bitrate_limit_stats_count < test->settings->bitrate_limit_stats_per_interval)
1681 return;
1682
1683 /* Calculating total bytes traffic to be averaged */
1684 for (total_bytes = 0, i = 0; i < test->settings->bitrate_limit_stats_per_interval; i++) {
1685 total_bytes += test->bitrate_limit_intervals_traffic_bytes[i];
1686 }
1687
1688 seconds = test->stats_interval * test->settings->bitrate_limit_stats_per_interval;
1689 bits_per_second = total_bytes * 8 / seconds;
1690 if (test->debug) {
1691 iperf_printf(test,"Interval %" PRIu64 " - throughput %" PRIu64 " bps (limit %" PRIu64 ")\n", test->bitrate_limit_stats_count, bits_per_second, test->settings->bitrate_limit);
1692 }
1693
1694 if (bits_per_second > test->settings->bitrate_limit) {
1695 if (iperf_get_verbose(test))
1696 iperf_err(test, "Total throughput of %" PRIu64 " bps exceeded %" PRIu64 " bps limit", bits_per_second, test->settings->bitrate_limit);
1697 test->bitrate_limit_exceeded = 1;
1698 }
1699 }
1700
1701 int
iperf_send(struct iperf_test * test,fd_set * write_setP)1702 iperf_send(struct iperf_test *test, fd_set *write_setP)
1703 {
1704 register int multisend, r, streams_active;
1705 register struct iperf_stream *sp;
1706 struct iperf_time now;
1707 int no_throttle_check;
1708
1709 /* Can we do multisend mode? */
1710 if (test->settings->burst != 0)
1711 multisend = test->settings->burst;
1712 else if (test->settings->rate == 0)
1713 multisend = test->multisend;
1714 else
1715 multisend = 1; /* nope */
1716
1717 /* Should bitrate throttle be checked for every send */
1718 no_throttle_check = test->settings->rate != 0 && test->settings->burst == 0;
1719
1720 for (; multisend > 0; --multisend) {
1721 if (no_throttle_check)
1722 iperf_time_now(&now);
1723 streams_active = 0;
1724 SLIST_FOREACH(sp, &test->streams, streams) {
1725 if ((sp->green_light && sp->sender &&
1726 (write_setP == NULL || FD_ISSET(sp->socket, write_setP)))) {
1727 if (multisend > 1 && test->settings->bytes != 0 && test->bytes_sent >= test->settings->bytes)
1728 break;
1729 if (multisend > 1 && test->settings->blocks != 0 && test->blocks_sent >= test->settings->blocks)
1730 break;
1731 if ((r = sp->snd(sp)) < 0) {
1732 if (r == NET_SOFTERROR)
1733 break;
1734 i_errno = IESTREAMWRITE;
1735 return r;
1736 }
1737 streams_active = 1;
1738 test->bytes_sent += r;
1739 if (!sp->pending_size)
1740 ++test->blocks_sent;
1741 if (no_throttle_check)
1742 iperf_check_throttle(sp, &now);
1743 }
1744 }
1745 if (!streams_active)
1746 break;
1747 }
1748 if (!no_throttle_check) { /* Throttle check if was not checked for each send */
1749 iperf_time_now(&now);
1750 SLIST_FOREACH(sp, &test->streams, streams)
1751 if (sp->sender)
1752 iperf_check_throttle(sp, &now);
1753 }
1754 if (write_setP != NULL)
1755 SLIST_FOREACH(sp, &test->streams, streams)
1756 if (FD_ISSET(sp->socket, write_setP))
1757 FD_CLR(sp->socket, write_setP);
1758
1759 return 0;
1760 }
1761
1762 int
iperf_recv(struct iperf_test * test,fd_set * read_setP)1763 iperf_recv(struct iperf_test *test, fd_set *read_setP)
1764 {
1765 int r;
1766 struct iperf_stream *sp;
1767
1768 SLIST_FOREACH(sp, &test->streams, streams) {
1769 if (FD_ISSET(sp->socket, read_setP) && !sp->sender) {
1770 if ((r = sp->rcv(sp)) < 0) {
1771 i_errno = IESTREAMREAD;
1772 return r;
1773 }
1774 test->bytes_received += r;
1775 ++test->blocks_received;
1776 FD_CLR(sp->socket, read_setP);
1777 }
1778 }
1779
1780 return 0;
1781 }
1782
1783 int
iperf_init_test(struct iperf_test * test)1784 iperf_init_test(struct iperf_test *test)
1785 {
1786 struct iperf_time now;
1787 struct iperf_stream *sp;
1788
1789 if (test->protocol->init) {
1790 if (test->protocol->init(test) < 0)
1791 return -1;
1792 }
1793
1794 /* Init each stream. */
1795 if (iperf_time_now(&now) < 0) {
1796 i_errno = IEINITTEST;
1797 return -1;
1798 }
1799 SLIST_FOREACH(sp, &test->streams, streams) {
1800 sp->result->start_time = sp->result->start_time_fixed = now;
1801 }
1802
1803 if (test->on_test_start)
1804 test->on_test_start(test);
1805
1806 return 0;
1807 }
1808
1809 static void
send_timer_proc(TimerClientData client_data,struct iperf_time * nowP)1810 send_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
1811 {
1812 struct iperf_stream *sp = client_data.p;
1813
1814 /* All we do here is set or clear the flag saying that this stream may
1815 ** be sent to. The actual sending gets done in the send proc, after
1816 ** checking the flag.
1817 */
1818 iperf_check_throttle(sp, nowP);
1819 }
1820
1821 int
iperf_create_send_timers(struct iperf_test * test)1822 iperf_create_send_timers(struct iperf_test * test)
1823 {
1824 struct iperf_time now;
1825 struct iperf_stream *sp;
1826 TimerClientData cd;
1827
1828 if (iperf_time_now(&now) < 0) {
1829 i_errno = IEINITTEST;
1830 return -1;
1831 }
1832 SLIST_FOREACH(sp, &test->streams, streams) {
1833 sp->green_light = 1;
1834 if (test->settings->rate != 0 && sp->sender) {
1835 cd.p = sp;
1836 sp->send_timer = tmr_create(NULL, send_timer_proc, cd, test->settings->pacing_timer, 1);
1837 if (sp->send_timer == NULL) {
1838 i_errno = IEINITTEST;
1839 return -1;
1840 }
1841 }
1842 }
1843 return 0;
1844 }
1845
1846 #if defined(HAVE_SSL)
test_is_authorized(struct iperf_test * test)1847 int test_is_authorized(struct iperf_test *test){
1848 if ( !(test->server_rsa_private_key && test->server_authorized_users)) {
1849 return 0;
1850 }
1851
1852 if (test->settings->authtoken){
1853 char *username = NULL, *password = NULL;
1854 time_t ts;
1855 int rc = decode_auth_setting(test->debug, test->settings->authtoken, test->server_rsa_private_key, &username, &password, &ts);
1856 if (rc) {
1857 return -1;
1858 }
1859 int ret = check_authentication(username, password, ts, test->server_authorized_users, test->server_skew_threshold);
1860 if (ret == 0){
1861 if (test->debug) {
1862 iperf_printf(test, report_authentication_succeeded, username, ts);
1863 }
1864 free(username);
1865 free(password);
1866 return 0;
1867 } else {
1868 if (test->debug) {
1869 iperf_printf(test, report_authentication_failed, username, ts);
1870 }
1871 free(username);
1872 free(password);
1873 return -1;
1874 }
1875 }
1876 return -1;
1877 }
1878 #endif //HAVE_SSL
1879
1880 /**
1881 * iperf_exchange_parameters - handles the param_Exchange part for client
1882 *
1883 */
1884
1885 int
iperf_exchange_parameters(struct iperf_test * test)1886 iperf_exchange_parameters(struct iperf_test *test)
1887 {
1888 int s;
1889 int32_t err;
1890
1891 if (test->role == 'c') {
1892
1893 if (send_parameters(test) < 0)
1894 return -1;
1895
1896 } else {
1897
1898 if (get_parameters(test) < 0)
1899 return -1;
1900
1901 #if defined(HAVE_SSL)
1902 if (test_is_authorized(test) < 0){
1903 if (iperf_set_send_state(test, SERVER_ERROR) != 0)
1904 return -1;
1905 i_errno = IEAUTHTEST;
1906 err = htonl(i_errno);
1907 if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) {
1908 i_errno = IECTRLWRITE;
1909 return -1;
1910 }
1911 return -1;
1912 }
1913 #endif //HAVE_SSL
1914
1915 if ((s = test->protocol->listen(test)) < 0) {
1916 if (iperf_set_send_state(test, SERVER_ERROR) != 0)
1917 return -1;
1918 err = htonl(i_errno);
1919 if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) {
1920 i_errno = IECTRLWRITE;
1921 return -1;
1922 }
1923 err = htonl(errno);
1924 if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) {
1925 i_errno = IECTRLWRITE;
1926 return -1;
1927 }
1928 return -1;
1929 }
1930
1931 FD_SET(s, &test->read_set);
1932 test->max_fd = (s > test->max_fd) ? s : test->max_fd;
1933 test->prot_listener = s;
1934
1935 // Send the control message to create streams and start the test
1936 if (iperf_set_send_state(test, CREATE_STREAMS) != 0)
1937 return -1;
1938
1939 }
1940
1941 return 0;
1942 }
1943
1944 /*************************************************************/
1945
1946 int
iperf_exchange_results(struct iperf_test * test)1947 iperf_exchange_results(struct iperf_test *test)
1948 {
1949 if (test->role == 'c') {
1950 /* Send results to server. */
1951 if (send_results(test) < 0)
1952 return -1;
1953 /* Get server results. */
1954 if (get_results(test) < 0)
1955 return -1;
1956 } else {
1957 /* Get client results. */
1958 if (get_results(test) < 0)
1959 return -1;
1960 /* Send results to client. */
1961 if (send_results(test) < 0)
1962 return -1;
1963 }
1964 return 0;
1965 }
1966
1967 /*************************************************************/
1968
1969 static int
send_parameters(struct iperf_test * test)1970 send_parameters(struct iperf_test *test)
1971 {
1972 int r = 0;
1973 cJSON *j;
1974
1975 j = cJSON_CreateObject();
1976 if (j == NULL) {
1977 i_errno = IESENDPARAMS;
1978 r = -1;
1979 } else {
1980 if (test->protocol->id == Ptcp)
1981 cJSON_AddTrueToObject(j, "tcp");
1982 else if (test->protocol->id == Pudp)
1983 cJSON_AddTrueToObject(j, "udp");
1984 else if (test->protocol->id == Psctp)
1985 cJSON_AddTrueToObject(j, "sctp");
1986 cJSON_AddNumberToObject(j, "omit", test->omit);
1987 if (test->server_affinity != -1)
1988 cJSON_AddNumberToObject(j, "server_affinity", test->server_affinity);
1989 cJSON_AddNumberToObject(j, "time", test->duration);
1990 if (test->settings->bytes)
1991 cJSON_AddNumberToObject(j, "num", test->settings->bytes);
1992 if (test->settings->blocks)
1993 cJSON_AddNumberToObject(j, "blockcount", test->settings->blocks);
1994 if (test->settings->mss)
1995 cJSON_AddNumberToObject(j, "MSS", test->settings->mss);
1996 if (test->no_delay)
1997 cJSON_AddTrueToObject(j, "nodelay");
1998 cJSON_AddNumberToObject(j, "parallel", test->num_streams);
1999 if (test->reverse)
2000 cJSON_AddTrueToObject(j, "reverse");
2001 if (test->bidirectional)
2002 cJSON_AddTrueToObject(j, "bidirectional");
2003 if (test->settings->socket_bufsize)
2004 cJSON_AddNumberToObject(j, "window", test->settings->socket_bufsize);
2005 if (test->settings->blksize)
2006 cJSON_AddNumberToObject(j, "len", test->settings->blksize);
2007 if (test->settings->rate)
2008 cJSON_AddNumberToObject(j, "bandwidth", test->settings->rate);
2009 if (test->settings->fqrate)
2010 cJSON_AddNumberToObject(j, "fqrate", test->settings->fqrate);
2011 if (test->settings->pacing_timer)
2012 cJSON_AddNumberToObject(j, "pacing_timer", test->settings->pacing_timer);
2013 if (test->settings->burst)
2014 cJSON_AddNumberToObject(j, "burst", test->settings->burst);
2015 if (test->settings->tos)
2016 cJSON_AddNumberToObject(j, "TOS", test->settings->tos);
2017 if (test->settings->flowlabel)
2018 cJSON_AddNumberToObject(j, "flowlabel", test->settings->flowlabel);
2019 if (test->title)
2020 cJSON_AddStringToObject(j, "title", test->title);
2021 if (test->extra_data)
2022 cJSON_AddStringToObject(j, "extra_data", test->extra_data);
2023 if (test->congestion)
2024 cJSON_AddStringToObject(j, "congestion", test->congestion);
2025 if (test->congestion_used)
2026 cJSON_AddStringToObject(j, "congestion_used", test->congestion_used);
2027 if (test->get_server_output)
2028 cJSON_AddNumberToObject(j, "get_server_output", iperf_get_test_get_server_output(test));
2029 if (test->udp_counters_64bit)
2030 cJSON_AddNumberToObject(j, "udp_counters_64bit", iperf_get_test_udp_counters_64bit(test));
2031 if (test->repeating_payload)
2032 cJSON_AddNumberToObject(j, "repeating_payload", test->repeating_payload);
2033 #if defined(HAVE_DONT_FRAGMENT)
2034 if (test->settings->dont_fragment)
2035 cJSON_AddNumberToObject(j, "dont_fragment", test->settings->dont_fragment);
2036 #endif /* HAVE_DONT_FRAGMENT */
2037 #if defined(HAVE_SSL)
2038 /* Send authentication parameters */
2039 if (test->settings->client_username && test->settings->client_password && test->settings->client_rsa_pubkey){
2040 int rc = encode_auth_setting(test->settings->client_username, test->settings->client_password, test->settings->client_rsa_pubkey, &test->settings->authtoken);
2041
2042 if (rc) {
2043 cJSON_Delete(j);
2044 i_errno = IESENDPARAMS;
2045 return -1;
2046 }
2047
2048 cJSON_AddStringToObject(j, "authtoken", test->settings->authtoken);
2049 }
2050 #endif // HAVE_SSL
2051 cJSON_AddStringToObject(j, "client_version", IPERF_VERSION);
2052
2053 if (test->debug) {
2054 char *str = cJSON_Print(j);
2055 printf("send_parameters:\n%s\n", str);
2056 cJSON_free(str);
2057 }
2058
2059 if (JSON_write(test->ctrl_sck, j) < 0) {
2060 i_errno = IESENDPARAMS;
2061 r = -1;
2062 }
2063 cJSON_Delete(j);
2064 }
2065 return r;
2066 }
2067
2068 /*************************************************************/
2069
2070 static int
get_parameters(struct iperf_test * test)2071 get_parameters(struct iperf_test *test)
2072 {
2073 int r = 0;
2074 cJSON *j;
2075 cJSON *j_p;
2076
2077 j = JSON_read(test->ctrl_sck);
2078 if (j == NULL) {
2079 i_errno = IERECVPARAMS;
2080 r = -1;
2081 } else {
2082 if (test->debug) {
2083 char *str;
2084 str = cJSON_Print(j);
2085 printf("get_parameters:\n%s\n", str );
2086 cJSON_free(str);
2087 }
2088
2089 if ((j_p = cJSON_GetObjectItem(j, "tcp")) != NULL)
2090 set_protocol(test, Ptcp);
2091 if ((j_p = cJSON_GetObjectItem(j, "udp")) != NULL)
2092 set_protocol(test, Pudp);
2093 if ((j_p = cJSON_GetObjectItem(j, "sctp")) != NULL)
2094 set_protocol(test, Psctp);
2095 if ((j_p = cJSON_GetObjectItem(j, "omit")) != NULL)
2096 test->omit = j_p->valueint;
2097 if ((j_p = cJSON_GetObjectItem(j, "server_affinity")) != NULL)
2098 test->server_affinity = j_p->valueint;
2099 if ((j_p = cJSON_GetObjectItem(j, "time")) != NULL)
2100 test->duration = j_p->valueint;
2101 if ((j_p = cJSON_GetObjectItem(j, "num")) != NULL)
2102 test->settings->bytes = j_p->valueint;
2103 if ((j_p = cJSON_GetObjectItem(j, "blockcount")) != NULL)
2104 test->settings->blocks = j_p->valueint;
2105 if ((j_p = cJSON_GetObjectItem(j, "MSS")) != NULL)
2106 test->settings->mss = j_p->valueint;
2107 if ((j_p = cJSON_GetObjectItem(j, "nodelay")) != NULL)
2108 test->no_delay = 1;
2109 if ((j_p = cJSON_GetObjectItem(j, "parallel")) != NULL)
2110 test->num_streams = j_p->valueint;
2111 if ((j_p = cJSON_GetObjectItem(j, "reverse")) != NULL)
2112 iperf_set_test_reverse(test, 1);
2113 if ((j_p = cJSON_GetObjectItem(j, "bidirectional")) != NULL)
2114 iperf_set_test_bidirectional(test, 1);
2115 if ((j_p = cJSON_GetObjectItem(j, "window")) != NULL)
2116 test->settings->socket_bufsize = j_p->valueint;
2117 if ((j_p = cJSON_GetObjectItem(j, "len")) != NULL)
2118 test->settings->blksize = j_p->valueint;
2119 if ((j_p = cJSON_GetObjectItem(j, "bandwidth")) != NULL)
2120 test->settings->rate = j_p->valueint;
2121 if ((j_p = cJSON_GetObjectItem(j, "fqrate")) != NULL)
2122 test->settings->fqrate = j_p->valueint;
2123 if ((j_p = cJSON_GetObjectItem(j, "pacing_timer")) != NULL)
2124 test->settings->pacing_timer = j_p->valueint;
2125 if ((j_p = cJSON_GetObjectItem(j, "burst")) != NULL)
2126 test->settings->burst = j_p->valueint;
2127 if ((j_p = cJSON_GetObjectItem(j, "TOS")) != NULL)
2128 test->settings->tos = j_p->valueint;
2129 if ((j_p = cJSON_GetObjectItem(j, "flowlabel")) != NULL)
2130 test->settings->flowlabel = j_p->valueint;
2131 if ((j_p = cJSON_GetObjectItem(j, "title")) != NULL)
2132 test->title = strdup(j_p->valuestring);
2133 if ((j_p = cJSON_GetObjectItem(j, "extra_data")) != NULL)
2134 test->extra_data = strdup(j_p->valuestring);
2135 if ((j_p = cJSON_GetObjectItem(j, "congestion")) != NULL)
2136 test->congestion = strdup(j_p->valuestring);
2137 if ((j_p = cJSON_GetObjectItem(j, "congestion_used")) != NULL)
2138 test->congestion_used = strdup(j_p->valuestring);
2139 if ((j_p = cJSON_GetObjectItem(j, "get_server_output")) != NULL)
2140 iperf_set_test_get_server_output(test, 1);
2141 if ((j_p = cJSON_GetObjectItem(j, "udp_counters_64bit")) != NULL)
2142 iperf_set_test_udp_counters_64bit(test, 1);
2143 if ((j_p = cJSON_GetObjectItem(j, "repeating_payload")) != NULL)
2144 test->repeating_payload = 1;
2145 #if defined(HAVE_DONT_FRAGMENT)
2146 if ((j_p = cJSON_GetObjectItem(j, "dont_fragment")) != NULL)
2147 test->settings->dont_fragment = j_p->valueint;
2148 #endif /* HAVE_DONT_FRAGMENT */
2149 #if defined(HAVE_SSL)
2150 if ((j_p = cJSON_GetObjectItem(j, "authtoken")) != NULL)
2151 test->settings->authtoken = strdup(j_p->valuestring);
2152 #endif //HAVE_SSL
2153 if (test->mode && test->protocol->id == Ptcp && has_tcpinfo_retransmits())
2154 test->sender_has_retransmits = 1;
2155 if (test->settings->rate)
2156 cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate);
2157 cJSON_Delete(j);
2158 }
2159 return r;
2160 }
2161
2162 /*************************************************************/
2163
2164 static int
send_results(struct iperf_test * test)2165 send_results(struct iperf_test *test)
2166 {
2167 int r = 0;
2168 cJSON *j;
2169 cJSON *j_streams;
2170 struct iperf_stream *sp;
2171 cJSON *j_stream;
2172 int sender_has_retransmits;
2173 iperf_size_t bytes_transferred;
2174 int retransmits;
2175 struct iperf_time temp_time;
2176 double start_time, end_time;
2177
2178 j = cJSON_CreateObject();
2179 if (j == NULL) {
2180 i_errno = IEPACKAGERESULTS;
2181 r = -1;
2182 } else {
2183 cJSON_AddNumberToObject(j, "cpu_util_total", test->cpu_util[0]);
2184 cJSON_AddNumberToObject(j, "cpu_util_user", test->cpu_util[1]);
2185 cJSON_AddNumberToObject(j, "cpu_util_system", test->cpu_util[2]);
2186 if ( test->mode == RECEIVER )
2187 sender_has_retransmits = -1;
2188 else
2189 sender_has_retransmits = test->sender_has_retransmits;
2190 cJSON_AddNumberToObject(j, "sender_has_retransmits", sender_has_retransmits);
2191 if ( test->congestion_used ) {
2192 cJSON_AddStringToObject(j, "congestion_used", test->congestion_used);
2193 }
2194
2195 /* If on the server and sending server output, then do this */
2196 if (test->role == 's' && test->get_server_output) {
2197 if (test->json_output) {
2198 /* Add JSON output */
2199 cJSON_AddItemReferenceToObject(j, "server_output_json", test->json_top);
2200 }
2201 else {
2202 /* Add textual output */
2203 size_t buflen = 0;
2204
2205 /* Figure out how much room we need to hold the complete output string */
2206 struct iperf_textline *t;
2207 TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) {
2208 buflen += strlen(t->line);
2209 }
2210
2211 /* Allocate and build it up from the component lines */
2212 char *output = calloc(buflen + 1, 1);
2213 TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) {
2214 strncat(output, t->line, buflen);
2215 buflen -= strlen(t->line);
2216 }
2217
2218 cJSON_AddStringToObject(j, "server_output_text", output);
2219 free(output);
2220 }
2221 }
2222
2223 j_streams = cJSON_CreateArray();
2224 if (j_streams == NULL) {
2225 i_errno = IEPACKAGERESULTS;
2226 r = -1;
2227 } else {
2228 cJSON_AddItemToObject(j, "streams", j_streams);
2229 SLIST_FOREACH(sp, &test->streams, streams) {
2230 j_stream = cJSON_CreateObject();
2231 if (j_stream == NULL) {
2232 i_errno = IEPACKAGERESULTS;
2233 r = -1;
2234 } else {
2235 cJSON_AddItemToArray(j_streams, j_stream);
2236 bytes_transferred = sp->sender ? (sp->result->bytes_sent - sp->result->bytes_sent_omit) : sp->result->bytes_received;
2237 retransmits = (sp->sender && test->sender_has_retransmits) ? sp->result->stream_retrans : -1;
2238 cJSON_AddNumberToObject(j_stream, "id", sp->id);
2239 cJSON_AddNumberToObject(j_stream, "bytes", bytes_transferred);
2240 cJSON_AddNumberToObject(j_stream, "retransmits", retransmits);
2241 cJSON_AddNumberToObject(j_stream, "jitter", sp->jitter);
2242 cJSON_AddNumberToObject(j_stream, "errors", sp->cnt_error);
2243 cJSON_AddNumberToObject(j_stream, "packets", sp->packet_count);
2244
2245 iperf_time_diff(&sp->result->start_time, &sp->result->start_time, &temp_time);
2246 start_time = iperf_time_in_secs(&temp_time);
2247 iperf_time_diff(&sp->result->start_time, &sp->result->end_time, &temp_time);
2248 end_time = iperf_time_in_secs(&temp_time);
2249 cJSON_AddNumberToObject(j_stream, "start_time", start_time);
2250 cJSON_AddNumberToObject(j_stream, "end_time", end_time);
2251
2252 }
2253 }
2254 if (r == 0 && test->debug) {
2255 char *str = cJSON_Print(j);
2256 printf("send_results\n%s\n", str);
2257 cJSON_free(str);
2258 }
2259 if (r == 0 && JSON_write(test->ctrl_sck, j) < 0) {
2260 i_errno = IESENDRESULTS;
2261 r = -1;
2262 }
2263 }
2264 cJSON_Delete(j);
2265 }
2266 return r;
2267 }
2268
2269 /*************************************************************/
2270
2271 static int
get_results(struct iperf_test * test)2272 get_results(struct iperf_test *test)
2273 {
2274 int r = 0;
2275 cJSON *j;
2276 cJSON *j_cpu_util_total;
2277 cJSON *j_cpu_util_user;
2278 cJSON *j_cpu_util_system;
2279 cJSON *j_remote_congestion_used;
2280 cJSON *j_sender_has_retransmits;
2281 int result_has_retransmits;
2282 cJSON *j_streams;
2283 int n, i;
2284 cJSON *j_stream;
2285 cJSON *j_id;
2286 cJSON *j_bytes;
2287 cJSON *j_retransmits;
2288 cJSON *j_jitter;
2289 cJSON *j_errors;
2290 cJSON *j_packets;
2291 cJSON *j_server_output;
2292 cJSON *j_start_time, *j_end_time;
2293 int sid, cerror, pcount;
2294 double jitter;
2295 iperf_size_t bytes_transferred;
2296 int retransmits;
2297 struct iperf_stream *sp;
2298
2299 j = JSON_read(test->ctrl_sck);
2300 if (j == NULL) {
2301 i_errno = IERECVRESULTS;
2302 r = -1;
2303 } else {
2304 j_cpu_util_total = cJSON_GetObjectItem(j, "cpu_util_total");
2305 j_cpu_util_user = cJSON_GetObjectItem(j, "cpu_util_user");
2306 j_cpu_util_system = cJSON_GetObjectItem(j, "cpu_util_system");
2307 j_sender_has_retransmits = cJSON_GetObjectItem(j, "sender_has_retransmits");
2308 if (j_cpu_util_total == NULL || j_cpu_util_user == NULL || j_cpu_util_system == NULL || j_sender_has_retransmits == NULL) {
2309 i_errno = IERECVRESULTS;
2310 r = -1;
2311 } else {
2312 if (test->debug) {
2313 char *str = cJSON_Print(j);
2314 printf("get_results\n%s\n", str);
2315 cJSON_free(str);
2316 }
2317
2318 test->remote_cpu_util[0] = j_cpu_util_total->valuedouble;
2319 test->remote_cpu_util[1] = j_cpu_util_user->valuedouble;
2320 test->remote_cpu_util[2] = j_cpu_util_system->valuedouble;
2321 result_has_retransmits = j_sender_has_retransmits->valueint;
2322 if ( test->mode == RECEIVER ) {
2323 test->sender_has_retransmits = result_has_retransmits;
2324 test->other_side_has_retransmits = 0;
2325 }
2326 else if ( test->mode == BIDIRECTIONAL )
2327 test->other_side_has_retransmits = result_has_retransmits;
2328
2329 j_streams = cJSON_GetObjectItem(j, "streams");
2330 if (j_streams == NULL) {
2331 i_errno = IERECVRESULTS;
2332 r = -1;
2333 } else {
2334 n = cJSON_GetArraySize(j_streams);
2335 for (i=0; i<n; ++i) {
2336 j_stream = cJSON_GetArrayItem(j_streams, i);
2337 if (j_stream == NULL) {
2338 i_errno = IERECVRESULTS;
2339 r = -1;
2340 } else {
2341 j_id = cJSON_GetObjectItem(j_stream, "id");
2342 j_bytes = cJSON_GetObjectItem(j_stream, "bytes");
2343 j_retransmits = cJSON_GetObjectItem(j_stream, "retransmits");
2344 j_jitter = cJSON_GetObjectItem(j_stream, "jitter");
2345 j_errors = cJSON_GetObjectItem(j_stream, "errors");
2346 j_packets = cJSON_GetObjectItem(j_stream, "packets");
2347 j_start_time = cJSON_GetObjectItem(j_stream, "start_time");
2348 j_end_time = cJSON_GetObjectItem(j_stream, "end_time");
2349 if (j_id == NULL || j_bytes == NULL || j_retransmits == NULL || j_jitter == NULL || j_errors == NULL || j_packets == NULL) {
2350 i_errno = IERECVRESULTS;
2351 r = -1;
2352 } else {
2353 sid = j_id->valueint;
2354 bytes_transferred = j_bytes->valueint;
2355 retransmits = j_retransmits->valueint;
2356 jitter = j_jitter->valuedouble;
2357 cerror = j_errors->valueint;
2358 pcount = j_packets->valueint;
2359 SLIST_FOREACH(sp, &test->streams, streams)
2360 if (sp->id == sid) break;
2361 if (sp == NULL) {
2362 i_errno = IESTREAMID;
2363 r = -1;
2364 } else {
2365 if (sp->sender) {
2366 sp->jitter = jitter;
2367 sp->cnt_error = cerror;
2368 sp->peer_packet_count = pcount;
2369 sp->result->bytes_received = bytes_transferred;
2370 /*
2371 * We have to handle the possibilty that
2372 * start_time and end_time might not be
2373 * available; this is the case for older (pre-3.2)
2374 * servers.
2375 *
2376 * We need to have result structure members to hold
2377 * the both sides' start_time and end_time.
2378 */
2379 if (j_start_time && j_end_time) {
2380 sp->result->receiver_time = j_end_time->valuedouble - j_start_time->valuedouble;
2381 }
2382 else {
2383 sp->result->receiver_time = 0.0;
2384 }
2385 } else {
2386 sp->peer_packet_count = pcount;
2387 sp->result->bytes_sent = bytes_transferred;
2388 sp->result->stream_retrans = retransmits;
2389 if (j_start_time && j_end_time) {
2390 sp->result->sender_time = j_end_time->valuedouble - j_start_time->valuedouble;
2391 }
2392 else {
2393 sp->result->sender_time = 0.0;
2394 }
2395 }
2396 }
2397 }
2398 }
2399 }
2400 /*
2401 * If we're the client and we're supposed to get remote results,
2402 * look them up and process accordingly.
2403 */
2404 if (test->role == 'c' && iperf_get_test_get_server_output(test)) {
2405 /* Look for JSON. If we find it, grab the object so it doesn't get deleted. */
2406 j_server_output = cJSON_DetachItemFromObject(j, "server_output_json");
2407 if (j_server_output != NULL) {
2408 test->json_server_output = j_server_output;
2409 }
2410 else {
2411 /* No JSON, look for textual output. Make a copy of the text for later. */
2412 j_server_output = cJSON_GetObjectItem(j, "server_output_text");
2413 if (j_server_output != NULL) {
2414 test->server_output_text = strdup(j_server_output->valuestring);
2415 }
2416 }
2417 }
2418 }
2419 }
2420
2421 j_remote_congestion_used = cJSON_GetObjectItem(j, "congestion_used");
2422 if (j_remote_congestion_used != NULL) {
2423 test->remote_congestion_used = strdup(j_remote_congestion_used->valuestring);
2424 }
2425
2426 cJSON_Delete(j);
2427 }
2428 return r;
2429 }
2430
2431 /*************************************************************/
2432
2433 static int
JSON_write(int fd,cJSON * json)2434 JSON_write(int fd, cJSON *json)
2435 {
2436 uint32_t hsize, nsize;
2437 char *str;
2438 int r = 0;
2439
2440 str = cJSON_PrintUnformatted(json);
2441 if (str == NULL)
2442 r = -1;
2443 else {
2444 hsize = strlen(str);
2445 nsize = htonl(hsize);
2446 if (Nwrite(fd, (char*) &nsize, sizeof(nsize), Ptcp) < 0)
2447 r = -1;
2448 else {
2449 if (Nwrite(fd, str, hsize, Ptcp) < 0)
2450 r = -1;
2451 }
2452 cJSON_free(str);
2453 }
2454 return r;
2455 }
2456
2457 /*************************************************************/
2458
2459 static cJSON *
JSON_read(int fd)2460 JSON_read(int fd)
2461 {
2462 uint32_t hsize, nsize;
2463 char *str;
2464 cJSON *json = NULL;
2465 int rc;
2466
2467 /*
2468 * Read a four-byte integer, which is the length of the JSON to follow.
2469 * Then read the JSON into a buffer and parse it. Return a parsed JSON
2470 * structure, NULL if there was an error.
2471 */
2472 if (Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp) >= 0) {
2473 hsize = ntohl(nsize);
2474 /* Allocate a buffer to hold the JSON */
2475 str = (char *) calloc(sizeof(char), hsize+1); /* +1 for trailing null */
2476 if (str != NULL) {
2477 rc = Nread(fd, str, hsize, Ptcp);
2478 if (rc >= 0) {
2479 /*
2480 * We should be reading in the number of bytes corresponding to the
2481 * length in that 4-byte integer. If we don't the socket might have
2482 * prematurely closed. Only do the JSON parsing if we got the
2483 * correct number of bytes.
2484 */
2485 if (rc == hsize) {
2486 json = cJSON_Parse(str);
2487 }
2488 else {
2489 printf("WARNING: Size of data read does not correspond to offered length\n");
2490 }
2491 }
2492 }
2493 free(str);
2494 }
2495 return json;
2496 }
2497
2498 /*************************************************************/
2499 /**
2500 * add_to_interval_list -- adds new interval to the interval_list
2501 */
2502
2503 void
add_to_interval_list(struct iperf_stream_result * rp,struct iperf_interval_results * new)2504 add_to_interval_list(struct iperf_stream_result * rp, struct iperf_interval_results * new)
2505 {
2506 struct iperf_interval_results *irp;
2507
2508 irp = (struct iperf_interval_results *) malloc(sizeof(struct iperf_interval_results));
2509 memcpy(irp, new, sizeof(struct iperf_interval_results));
2510 TAILQ_INSERT_TAIL(&rp->interval_results, irp, irlistentries);
2511 }
2512
2513
2514 /************************************************************/
2515
2516 /**
2517 * connect_msg -- displays connection message
2518 * denoting sender/receiver details
2519 *
2520 */
2521
2522 void
connect_msg(struct iperf_stream * sp)2523 connect_msg(struct iperf_stream *sp)
2524 {
2525 char ipl[INET6_ADDRSTRLEN], ipr[INET6_ADDRSTRLEN];
2526 int lport, rport;
2527
2528 if (getsockdomain(sp->socket) == AF_INET) {
2529 inet_ntop(AF_INET, (void *) &((struct sockaddr_in *) &sp->local_addr)->sin_addr, ipl, sizeof(ipl));
2530 mapped_v4_to_regular_v4(ipl);
2531 inet_ntop(AF_INET, (void *) &((struct sockaddr_in *) &sp->remote_addr)->sin_addr, ipr, sizeof(ipr));
2532 mapped_v4_to_regular_v4(ipr);
2533 lport = ntohs(((struct sockaddr_in *) &sp->local_addr)->sin_port);
2534 rport = ntohs(((struct sockaddr_in *) &sp->remote_addr)->sin_port);
2535 } else {
2536 inet_ntop(AF_INET6, (void *) &((struct sockaddr_in6 *) &sp->local_addr)->sin6_addr, ipl, sizeof(ipl));
2537 mapped_v4_to_regular_v4(ipl);
2538 inet_ntop(AF_INET6, (void *) &((struct sockaddr_in6 *) &sp->remote_addr)->sin6_addr, ipr, sizeof(ipr));
2539 mapped_v4_to_regular_v4(ipr);
2540 lport = ntohs(((struct sockaddr_in6 *) &sp->local_addr)->sin6_port);
2541 rport = ntohs(((struct sockaddr_in6 *) &sp->remote_addr)->sin6_port);
2542 }
2543
2544 if (sp->test->json_output)
2545 cJSON_AddItemToArray(sp->test->json_connected, iperf_json_printf("socket: %d local_host: %s local_port: %d remote_host: %s remote_port: %d", (int64_t) sp->socket, ipl, (int64_t) lport, ipr, (int64_t) rport));
2546 else
2547 iperf_printf(sp->test, report_connected, sp->socket, ipl, lport, ipr, rport);
2548 }
2549
2550
2551 /**************************************************************************/
2552
2553 struct iperf_test *
iperf_new_test()2554 iperf_new_test()
2555 {
2556 struct iperf_test *test;
2557
2558 test = (struct iperf_test *) malloc(sizeof(struct iperf_test));
2559 if (!test) {
2560 i_errno = IENEWTEST;
2561 return NULL;
2562 }
2563 /* initialize everything to zero */
2564 memset(test, 0, sizeof(struct iperf_test));
2565
2566 test->settings = (struct iperf_settings *) malloc(sizeof(struct iperf_settings));
2567 if (!test->settings) {
2568 free(test);
2569 i_errno = IENEWTEST;
2570 return NULL;
2571 }
2572 memset(test->settings, 0, sizeof(struct iperf_settings));
2573
2574 test->bitrate_limit_intervals_traffic_bytes = (iperf_size_t *) malloc(sizeof(iperf_size_t) * MAX_INTERVAL);
2575 if (!test->bitrate_limit_intervals_traffic_bytes) {
2576 free(test);
2577 i_errno = IENEWTEST;
2578 return NULL;
2579 }
2580 memset(test->bitrate_limit_intervals_traffic_bytes, 0, sizeof(sizeof(iperf_size_t) * MAX_INTERVAL));
2581
2582 /* By default all output goes to stdout */
2583 test->outfile = stdout;
2584
2585 return test;
2586 }
2587
2588 /**************************************************************************/
2589
2590 struct protocol *
protocol_new(void)2591 protocol_new(void)
2592 {
2593 struct protocol *proto;
2594
2595 proto = malloc(sizeof(struct protocol));
2596 if(!proto) {
2597 return NULL;
2598 }
2599 memset(proto, 0, sizeof(struct protocol));
2600
2601 return proto;
2602 }
2603
2604 void
protocol_free(struct protocol * proto)2605 protocol_free(struct protocol *proto)
2606 {
2607 free(proto);
2608 }
2609
2610 /**************************************************************************/
2611 int
iperf_defaults(struct iperf_test * testp)2612 iperf_defaults(struct iperf_test *testp)
2613 {
2614 struct protocol *tcp, *udp;
2615 #if defined(HAVE_SCTP_H)
2616 struct protocol *sctp;
2617 #endif /* HAVE_SCTP_H */
2618
2619 testp->omit = OMIT;
2620 testp->duration = DURATION;
2621 testp->diskfile_name = (char*) 0;
2622 testp->affinity = -1;
2623 testp->server_affinity = -1;
2624 TAILQ_INIT(&testp->xbind_addrs);
2625 #if defined(HAVE_CPUSET_SETAFFINITY)
2626 CPU_ZERO(&testp->cpumask);
2627 #endif /* HAVE_CPUSET_SETAFFINITY */
2628 testp->title = NULL;
2629 testp->extra_data = NULL;
2630 testp->congestion = NULL;
2631 testp->congestion_used = NULL;
2632 testp->remote_congestion_used = NULL;
2633 testp->server_port = PORT;
2634 testp->ctrl_sck = -1;
2635 testp->prot_listener = -1;
2636 testp->other_side_has_retransmits = 0;
2637
2638 testp->stats_callback = iperf_stats_callback;
2639 testp->reporter_callback = iperf_reporter_callback;
2640
2641 testp->stats_interval = testp->reporter_interval = 1;
2642 testp->num_streams = 1;
2643
2644 testp->settings->domain = AF_UNSPEC;
2645 testp->settings->unit_format = 'a';
2646 testp->settings->socket_bufsize = 0; /* use autotuning */
2647 testp->settings->blksize = DEFAULT_TCP_BLKSIZE;
2648 testp->settings->rate = 0;
2649 testp->settings->bitrate_limit = 0;
2650 testp->settings->bitrate_limit_interval = 5;
2651 testp->settings->bitrate_limit_stats_per_interval = 0;
2652 testp->settings->fqrate = 0;
2653 testp->settings->pacing_timer = DEFAULT_PACING_TIMER;
2654 testp->settings->burst = 0;
2655 testp->settings->mss = 0;
2656 testp->settings->bytes = 0;
2657 testp->settings->blocks = 0;
2658 testp->settings->connect_timeout = -1;
2659 testp->settings->rcv_timeout.secs = DEFAULT_NO_MSG_RCVD_TIMEOUT / SEC_TO_mS;
2660 testp->settings->rcv_timeout.usecs = (DEFAULT_NO_MSG_RCVD_TIMEOUT % SEC_TO_mS) * mS_TO_US;
2661
2662 memset(testp->cookie, 0, COOKIE_SIZE);
2663
2664 testp->multisend = 10; /* arbitrary */
2665
2666 /* Set up protocol list */
2667 SLIST_INIT(&testp->streams);
2668 SLIST_INIT(&testp->protocols);
2669
2670 tcp = protocol_new();
2671 if (!tcp)
2672 return -1;
2673
2674 tcp->id = Ptcp;
2675 tcp->name = "TCP";
2676 tcp->accept = iperf_tcp_accept;
2677 tcp->listen = iperf_tcp_listen;
2678 tcp->connect = iperf_tcp_connect;
2679 tcp->send = iperf_tcp_send;
2680 tcp->recv = iperf_tcp_recv;
2681 tcp->init = NULL;
2682 SLIST_INSERT_HEAD(&testp->protocols, tcp, protocols);
2683
2684 udp = protocol_new();
2685 if (!udp) {
2686 protocol_free(tcp);
2687 return -1;
2688 }
2689
2690 udp->id = Pudp;
2691 udp->name = "UDP";
2692 udp->accept = iperf_udp_accept;
2693 udp->listen = iperf_udp_listen;
2694 udp->connect = iperf_udp_connect;
2695 udp->send = iperf_udp_send;
2696 udp->recv = iperf_udp_recv;
2697 udp->init = iperf_udp_init;
2698 SLIST_INSERT_AFTER(tcp, udp, protocols);
2699
2700 set_protocol(testp, Ptcp);
2701
2702 #if defined(HAVE_SCTP_H)
2703 sctp = protocol_new();
2704 if (!sctp) {
2705 protocol_free(tcp);
2706 protocol_free(udp);
2707 return -1;
2708 }
2709
2710 sctp->id = Psctp;
2711 sctp->name = "SCTP";
2712 sctp->accept = iperf_sctp_accept;
2713 sctp->listen = iperf_sctp_listen;
2714 sctp->connect = iperf_sctp_connect;
2715 sctp->send = iperf_sctp_send;
2716 sctp->recv = iperf_sctp_recv;
2717 sctp->init = iperf_sctp_init;
2718
2719 SLIST_INSERT_AFTER(udp, sctp, protocols);
2720 #endif /* HAVE_SCTP_H */
2721
2722 testp->on_new_stream = iperf_on_new_stream;
2723 testp->on_test_start = iperf_on_test_start;
2724 testp->on_connect = iperf_on_connect;
2725 testp->on_test_finish = iperf_on_test_finish;
2726
2727 TAILQ_INIT(&testp->server_output_list);
2728
2729 return 0;
2730 }
2731
2732
2733 /**************************************************************************/
2734 void
iperf_free_test(struct iperf_test * test)2735 iperf_free_test(struct iperf_test *test)
2736 {
2737 struct protocol *prot;
2738 struct iperf_stream *sp;
2739
2740 /* Free streams */
2741 while (!SLIST_EMPTY(&test->streams)) {
2742 sp = SLIST_FIRST(&test->streams);
2743 SLIST_REMOVE_HEAD(&test->streams, streams);
2744 iperf_free_stream(sp);
2745 }
2746 if (test->server_hostname)
2747 free(test->server_hostname);
2748 if (test->tmp_template)
2749 free(test->tmp_template);
2750 if (test->bind_address)
2751 free(test->bind_address);
2752 if (test->bind_dev)
2753 free(test->bind_dev);
2754 if (!TAILQ_EMPTY(&test->xbind_addrs)) {
2755 struct xbind_entry *xbe;
2756
2757 while (!TAILQ_EMPTY(&test->xbind_addrs)) {
2758 xbe = TAILQ_FIRST(&test->xbind_addrs);
2759 TAILQ_REMOVE(&test->xbind_addrs, xbe, link);
2760 if (xbe->ai)
2761 freeaddrinfo(xbe->ai);
2762 free(xbe->name);
2763 free(xbe);
2764 }
2765 }
2766 #if defined(HAVE_SSL)
2767
2768 if (test->server_rsa_private_key)
2769 EVP_PKEY_free(test->server_rsa_private_key);
2770 test->server_rsa_private_key = NULL;
2771
2772 free(test->settings->authtoken);
2773 test->settings->authtoken = NULL;
2774
2775 free(test->settings->client_username);
2776 test->settings->client_username = NULL;
2777
2778 free(test->settings->client_password);
2779 test->settings->client_password = NULL;
2780
2781 if (test->settings->client_rsa_pubkey)
2782 EVP_PKEY_free(test->settings->client_rsa_pubkey);
2783 test->settings->client_rsa_pubkey = NULL;
2784 #endif /* HAVE_SSL */
2785
2786 if (test->settings)
2787 free(test->settings);
2788 if (test->title)
2789 free(test->title);
2790 if (test->extra_data)
2791 free(test->extra_data);
2792 if (test->congestion)
2793 free(test->congestion);
2794 if (test->congestion_used)
2795 free(test->congestion_used);
2796 if (test->remote_congestion_used)
2797 free(test->remote_congestion_used);
2798 if (test->timestamp_format)
2799 free(test->timestamp_format);
2800 if (test->omit_timer != NULL)
2801 tmr_cancel(test->omit_timer);
2802 if (test->timer != NULL)
2803 tmr_cancel(test->timer);
2804 if (test->stats_timer != NULL)
2805 tmr_cancel(test->stats_timer);
2806 if (test->reporter_timer != NULL)
2807 tmr_cancel(test->reporter_timer);
2808
2809 /* Free protocol list */
2810 while (!SLIST_EMPTY(&test->protocols)) {
2811 prot = SLIST_FIRST(&test->protocols);
2812 SLIST_REMOVE_HEAD(&test->protocols, protocols);
2813 free(prot);
2814 }
2815
2816 if (test->logfile) {
2817 free(test->logfile);
2818 test->logfile = NULL;
2819 if (test->outfile) {
2820 fclose(test->outfile);
2821 test->outfile = NULL;
2822 }
2823 }
2824
2825 if (test->server_output_text) {
2826 free(test->server_output_text);
2827 test->server_output_text = NULL;
2828 }
2829
2830 if (test->json_output_string) {
2831 free(test->json_output_string);
2832 test->json_output_string = NULL;
2833 }
2834
2835 /* Free output line buffers, if any (on the server only) */
2836 struct iperf_textline *t;
2837 while (!TAILQ_EMPTY(&test->server_output_list)) {
2838 t = TAILQ_FIRST(&test->server_output_list);
2839 TAILQ_REMOVE(&test->server_output_list, t, textlineentries);
2840 free(t->line);
2841 free(t);
2842 }
2843
2844 /* sctp_bindx: do not free the arguments, only the resolver results */
2845 if (!TAILQ_EMPTY(&test->xbind_addrs)) {
2846 struct xbind_entry *xbe;
2847
2848 TAILQ_FOREACH(xbe, &test->xbind_addrs, link) {
2849 if (xbe->ai) {
2850 freeaddrinfo(xbe->ai);
2851 xbe->ai = NULL;
2852 }
2853 }
2854 }
2855
2856 /* Free interval's traffic array for avrage rate calculations */
2857 if (test->bitrate_limit_intervals_traffic_bytes != NULL)
2858 free(test->bitrate_limit_intervals_traffic_bytes);
2859
2860 /* XXX: Why are we setting these values to NULL? */
2861 // test->streams = NULL;
2862 test->stats_callback = NULL;
2863 test->reporter_callback = NULL;
2864 free(test);
2865 }
2866
2867
2868 void
iperf_reset_test(struct iperf_test * test)2869 iperf_reset_test(struct iperf_test *test)
2870 {
2871 struct iperf_stream *sp;
2872 int i;
2873
2874 /* Free streams */
2875 while (!SLIST_EMPTY(&test->streams)) {
2876 sp = SLIST_FIRST(&test->streams);
2877 SLIST_REMOVE_HEAD(&test->streams, streams);
2878 iperf_free_stream(sp);
2879 }
2880 if (test->omit_timer != NULL) {
2881 tmr_cancel(test->omit_timer);
2882 test->omit_timer = NULL;
2883 }
2884 if (test->timer != NULL) {
2885 tmr_cancel(test->timer);
2886 test->timer = NULL;
2887 }
2888 if (test->stats_timer != NULL) {
2889 tmr_cancel(test->stats_timer);
2890 test->stats_timer = NULL;
2891 }
2892 if (test->reporter_timer != NULL) {
2893 tmr_cancel(test->reporter_timer);
2894 test->reporter_timer = NULL;
2895 }
2896 test->done = 0;
2897
2898 SLIST_INIT(&test->streams);
2899
2900 if (test->remote_congestion_used)
2901 free(test->remote_congestion_used);
2902 test->remote_congestion_used = NULL;
2903 test->role = 's';
2904 test->mode = RECEIVER;
2905 test->sender_has_retransmits = 0;
2906 set_protocol(test, Ptcp);
2907 test->omit = OMIT;
2908 test->duration = DURATION;
2909 test->server_affinity = -1;
2910 #if defined(HAVE_CPUSET_SETAFFINITY)
2911 CPU_ZERO(&test->cpumask);
2912 #endif /* HAVE_CPUSET_SETAFFINITY */
2913 test->state = 0;
2914
2915 test->ctrl_sck = -1;
2916 test->prot_listener = -1;
2917
2918 test->bytes_sent = 0;
2919 test->blocks_sent = 0;
2920
2921 test->bytes_received = 0;
2922 test->blocks_received = 0;
2923
2924 test->other_side_has_retransmits = 0;
2925
2926 test->bitrate_limit_stats_count = 0;
2927 test->bitrate_limit_last_interval_index = 0;
2928 test->bitrate_limit_exceeded = 0;
2929
2930 for (i = 0; i < MAX_INTERVAL; i++)
2931 test->bitrate_limit_intervals_traffic_bytes[i] = 0;
2932
2933 test->reverse = 0;
2934 test->bidirectional = 0;
2935 test->no_delay = 0;
2936
2937 FD_ZERO(&test->read_set);
2938 FD_ZERO(&test->write_set);
2939
2940 test->num_streams = 1;
2941 test->settings->socket_bufsize = 0;
2942 test->settings->blksize = DEFAULT_TCP_BLKSIZE;
2943 test->settings->rate = 0;
2944 test->settings->burst = 0;
2945 test->settings->mss = 0;
2946 test->settings->tos = 0;
2947 test->settings->dont_fragment = 0;
2948
2949 #if defined(HAVE_SSL)
2950 if (test->settings->authtoken) {
2951 free(test->settings->authtoken);
2952 test->settings->authtoken = NULL;
2953 }
2954 if (test->settings->client_username) {
2955 free(test->settings->client_username);
2956 test->settings->client_username = NULL;
2957 }
2958 if (test->settings->client_password) {
2959 free(test->settings->client_password);
2960 test->settings->client_password = NULL;
2961 }
2962 if (test->settings->client_rsa_pubkey) {
2963 EVP_PKEY_free(test->settings->client_rsa_pubkey);
2964 test->settings->client_rsa_pubkey = NULL;
2965 }
2966 #endif /* HAVE_SSL */
2967
2968 memset(test->cookie, 0, COOKIE_SIZE);
2969 test->multisend = 10; /* arbitrary */
2970 test->udp_counters_64bit = 0;
2971 if (test->title) {
2972 free(test->title);
2973 test->title = NULL;
2974 }
2975 if (test->extra_data) {
2976 free(test->extra_data);
2977 test->extra_data = NULL;
2978 }
2979
2980 /* Free output line buffers, if any (on the server only) */
2981 struct iperf_textline *t;
2982 while (!TAILQ_EMPTY(&test->server_output_list)) {
2983 t = TAILQ_FIRST(&test->server_output_list);
2984 TAILQ_REMOVE(&test->server_output_list, t, textlineentries);
2985 free(t->line);
2986 free(t);
2987 }
2988 }
2989
2990
2991 /* Reset all of a test's stats back to zero. Called when the omitting
2992 ** period is over.
2993 */
2994 void
iperf_reset_stats(struct iperf_test * test)2995 iperf_reset_stats(struct iperf_test *test)
2996 {
2997 struct iperf_time now;
2998 struct iperf_stream *sp;
2999 struct iperf_stream_result *rp;
3000
3001 test->bytes_sent = 0;
3002 test->blocks_sent = 0;
3003 iperf_time_now(&now);
3004 SLIST_FOREACH(sp, &test->streams, streams) {
3005 sp->omitted_packet_count = sp->packet_count;
3006 sp->omitted_cnt_error = sp->cnt_error;
3007 sp->omitted_outoforder_packets = sp->outoforder_packets;
3008 sp->jitter = 0;
3009 rp = sp->result;
3010 rp->bytes_sent_omit = rp->bytes_sent;
3011 rp->bytes_received = 0;
3012 rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0;
3013 if (test->sender_has_retransmits == 1) {
3014 struct iperf_interval_results ir; /* temporary results structure */
3015 save_tcpinfo(sp, &ir);
3016 rp->stream_prev_total_retrans = get_total_retransmits(&ir);
3017 }
3018 rp->stream_retrans = 0;
3019 rp->start_time = now;
3020 }
3021 }
3022
3023
3024 /**************************************************************************/
3025
3026 /**
3027 * Gather statistics during a test.
3028 * This function works for both the client and server side.
3029 */
3030 void
iperf_stats_callback(struct iperf_test * test)3031 iperf_stats_callback(struct iperf_test *test)
3032 {
3033 struct iperf_stream *sp;
3034 struct iperf_stream_result *rp = NULL;
3035 struct iperf_interval_results *irp, temp;
3036 struct iperf_time temp_time;
3037 iperf_size_t total_interval_bytes_transferred = 0;
3038
3039 temp.omitted = test->omitting;
3040 SLIST_FOREACH(sp, &test->streams, streams) {
3041 rp = sp->result;
3042 temp.bytes_transferred = sp->sender ? rp->bytes_sent_this_interval : rp->bytes_received_this_interval;
3043
3044 // Total bytes transferred this interval
3045 total_interval_bytes_transferred += rp->bytes_sent_this_interval + rp->bytes_received_this_interval;
3046
3047 irp = TAILQ_LAST(&rp->interval_results, irlisthead);
3048 /* result->end_time contains timestamp of previous interval */
3049 if ( irp != NULL ) /* not the 1st interval */
3050 memcpy(&temp.interval_start_time, &rp->end_time, sizeof(struct iperf_time));
3051 else /* or use timestamp from beginning */
3052 memcpy(&temp.interval_start_time, &rp->start_time, sizeof(struct iperf_time));
3053 /* now save time of end of this interval */
3054 iperf_time_now(&rp->end_time);
3055 memcpy(&temp.interval_end_time, &rp->end_time, sizeof(struct iperf_time));
3056 iperf_time_diff(&temp.interval_start_time, &temp.interval_end_time, &temp_time);
3057 temp.interval_duration = iperf_time_in_secs(&temp_time);
3058 if (test->protocol->id == Ptcp) {
3059 if ( has_tcpinfo()) {
3060 save_tcpinfo(sp, &temp);
3061 if (test->sender_has_retransmits == 1) {
3062 long total_retrans = get_total_retransmits(&temp);
3063 temp.interval_retrans = total_retrans - rp->stream_prev_total_retrans;
3064 rp->stream_retrans += temp.interval_retrans;
3065 rp->stream_prev_total_retrans = total_retrans;
3066
3067 temp.snd_cwnd = get_snd_cwnd(&temp);
3068 if (temp.snd_cwnd > rp->stream_max_snd_cwnd) {
3069 rp->stream_max_snd_cwnd = temp.snd_cwnd;
3070 }
3071
3072 temp.snd_wnd = get_snd_wnd(&temp);
3073 if (temp.snd_wnd > rp->stream_max_snd_wnd) {
3074 rp->stream_max_snd_wnd = temp.snd_wnd;
3075 }
3076
3077 temp.rtt = get_rtt(&temp);
3078 if (temp.rtt > rp->stream_max_rtt) {
3079 rp->stream_max_rtt = temp.rtt;
3080 }
3081 if (rp->stream_min_rtt == 0 ||
3082 temp.rtt < rp->stream_min_rtt) {
3083 rp->stream_min_rtt = temp.rtt;
3084 }
3085 rp->stream_sum_rtt += temp.rtt;
3086 rp->stream_count_rtt++;
3087
3088 temp.rttvar = get_rttvar(&temp);
3089 temp.pmtu = get_pmtu(&temp);
3090 }
3091 }
3092 } else {
3093 if (irp == NULL) {
3094 temp.interval_packet_count = sp->packet_count;
3095 temp.interval_outoforder_packets = sp->outoforder_packets;
3096 temp.interval_cnt_error = sp->cnt_error;
3097 } else {
3098 temp.interval_packet_count = sp->packet_count - irp->packet_count;
3099 temp.interval_outoforder_packets = sp->outoforder_packets - irp->outoforder_packets;
3100 temp.interval_cnt_error = sp->cnt_error - irp->cnt_error;
3101 }
3102 temp.packet_count = sp->packet_count;
3103 temp.jitter = sp->jitter;
3104 temp.outoforder_packets = sp->outoforder_packets;
3105 temp.cnt_error = sp->cnt_error;
3106 }
3107 add_to_interval_list(rp, &temp);
3108 rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0;
3109 }
3110
3111 /* Verify that total server's throughput is not above specified limit */
3112 if (test->role == 's') {
3113 iperf_check_total_rate(test, total_interval_bytes_transferred);
3114 }
3115 }
3116
3117 /**
3118 * Print intermediate results during a test (interval report).
3119 * Uses print_interval_results to print the results for each stream,
3120 * then prints an interval summary for all streams in this
3121 * interval.
3122 */
3123 static void
iperf_print_intermediate(struct iperf_test * test)3124 iperf_print_intermediate(struct iperf_test *test)
3125 {
3126 struct iperf_stream *sp = NULL;
3127 struct iperf_interval_results *irp;
3128 struct iperf_time temp_time;
3129 cJSON *json_interval;
3130 cJSON *json_interval_streams;
3131
3132 int lower_mode, upper_mode;
3133 int current_mode;
3134
3135 /*
3136 * Due to timing oddities, there can be cases, especially on the
3137 * server side, where at the end of a test there is a fairly short
3138 * interval with no data transferred. This could caused by
3139 * the control and data flows sharing the same path in the network,
3140 * and having the control messages for stopping the test being
3141 * queued behind the data packets.
3142 *
3143 * We'd like to try to omit that last interval when it happens, to
3144 * avoid cluttering data and output with useless stuff.
3145 * So we're going to try to ignore very short intervals (less than
3146 * 10% of the interval time) that have no data.
3147 */
3148 int interval_ok = 0;
3149 SLIST_FOREACH(sp, &test->streams, streams) {
3150 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead);
3151 if (irp) {
3152 iperf_time_diff(&irp->interval_start_time, &irp->interval_end_time, &temp_time);
3153 double interval_len = iperf_time_in_secs(&temp_time);
3154 if (test->debug) {
3155 printf("interval_len %f bytes_transferred %" PRIu64 "\n", interval_len, irp->bytes_transferred);
3156 }
3157
3158 /*
3159 * If the interval is at least 10% the normal interval
3160 * length, or if there were actual bytes transferrred,
3161 * then we want to keep this interval.
3162 */
3163 if (interval_len >= test->stats_interval * 0.10 ||
3164 irp->bytes_transferred > 0) {
3165 interval_ok = 1;
3166 if (test->debug) {
3167 printf("interval forces keep\n");
3168 }
3169 }
3170 }
3171 }
3172 if (!interval_ok) {
3173 if (test->debug) {
3174 printf("ignoring short interval with no data\n");
3175 }
3176 return;
3177 }
3178
3179 if (test->json_output) {
3180 json_interval = cJSON_CreateObject();
3181 if (json_interval == NULL)
3182 return;
3183 cJSON_AddItemToArray(test->json_intervals, json_interval);
3184 json_interval_streams = cJSON_CreateArray();
3185 if (json_interval_streams == NULL)
3186 return;
3187 cJSON_AddItemToObject(json_interval, "streams", json_interval_streams);
3188 } else {
3189 json_interval = NULL;
3190 json_interval_streams = NULL;
3191 }
3192
3193 /*
3194 * We must to sum streams separately.
3195 * For bidirectional mode we must to display
3196 * information about sender and receiver streams.
3197 * For client side we must handle sender streams
3198 * firstly and receiver streams for server side.
3199 * The following design allows us to do this.
3200 */
3201
3202 if (test->mode == BIDIRECTIONAL) {
3203 if (test->role == 'c') {
3204 lower_mode = -1;
3205 upper_mode = 0;
3206 } else {
3207 lower_mode = 0;
3208 upper_mode = 1;
3209 }
3210 } else {
3211 lower_mode = test->mode;
3212 upper_mode = lower_mode;
3213 }
3214
3215
3216 for (current_mode = lower_mode; current_mode <= upper_mode; ++current_mode) {
3217 char ubuf[UNIT_LEN];
3218 char nbuf[UNIT_LEN];
3219 char mbuf[UNIT_LEN];
3220 char zbuf[] = " ";
3221
3222 iperf_size_t bytes = 0;
3223 double bandwidth;
3224 int retransmits = 0;
3225 double start_time, end_time;
3226
3227 int total_packets = 0, lost_packets = 0;
3228 double avg_jitter = 0.0, lost_percent;
3229 int stream_must_be_sender = current_mode * current_mode;
3230
3231 /* Print stream role just for bidirectional mode. */
3232
3233 if (test->mode == BIDIRECTIONAL) {
3234 sprintf(mbuf, "[%s-%s]", stream_must_be_sender?"TX":"RX", test->role == 'c'?"C":"S");
3235 } else {
3236 mbuf[0] = '\0';
3237 zbuf[0] = '\0';
3238 }
3239
3240 SLIST_FOREACH(sp, &test->streams, streams) {
3241 if (sp->sender == stream_must_be_sender) {
3242 print_interval_results(test, sp, json_interval_streams);
3243 /* sum up all streams */
3244 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead);
3245 if (irp == NULL) {
3246 iperf_err(test,
3247 "iperf_print_intermediate error: interval_results is NULL");
3248 return;
3249 }
3250 bytes += irp->bytes_transferred;
3251 if (test->protocol->id == Ptcp) {
3252 if (test->sender_has_retransmits == 1) {
3253 retransmits += irp->interval_retrans;
3254 }
3255 } else {
3256 total_packets += irp->interval_packet_count;
3257 lost_packets += irp->interval_cnt_error;
3258 avg_jitter += irp->jitter;
3259 }
3260 }
3261 }
3262
3263 /* next build string with sum of all streams */
3264 if (test->num_streams > 1 || test->json_output) {
3265 sp = SLIST_FIRST(&test->streams); /* reset back to 1st stream */
3266 /* Only do this of course if there was a first stream */
3267 if (sp) {
3268 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); /* use 1st stream for timing info */
3269
3270 unit_snprintf(ubuf, UNIT_LEN, (double) bytes, 'A');
3271 bandwidth = (double) bytes / (double) irp->interval_duration;
3272 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3273
3274 iperf_time_diff(&sp->result->start_time,&irp->interval_start_time, &temp_time);
3275 start_time = iperf_time_in_secs(&temp_time);
3276 iperf_time_diff(&sp->result->start_time,&irp->interval_end_time, &temp_time);
3277 end_time = iperf_time_in_secs(&temp_time);
3278 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3279 if (test->sender_has_retransmits == 1 && stream_must_be_sender) {
3280 /* Interval sum, TCP with retransmits. */
3281 if (test->json_output)
3282 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? */
3283 else
3284 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? */
3285 } else {
3286 /* Interval sum, TCP without retransmits. */
3287 if (test->json_output)
3288 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));
3289 else
3290 iperf_printf(test, report_sum_bw_format, mbuf, start_time, end_time, ubuf, nbuf, test->omitting?report_omitted:"");
3291 }
3292 } else {
3293 /* Interval sum, UDP. */
3294 if (stream_must_be_sender) {
3295 if (test->json_output)
3296 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));
3297 else
3298 iperf_printf(test, report_sum_bw_udp_sender_format, mbuf, start_time, end_time, ubuf, nbuf, zbuf, total_packets, test->omitting?report_omitted:"");
3299 } else {
3300 avg_jitter /= test->num_streams;
3301 if (total_packets > 0) {
3302 lost_percent = 100.0 * lost_packets / total_packets;
3303 }
3304 else {
3305 lost_percent = 0.0;
3306 }
3307 if (test->json_output)
3308 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));
3309 else
3310 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:"");
3311 }
3312 }
3313 }
3314 }
3315 }
3316 }
3317
3318 /**
3319 * Print overall summary statistics at the end of a test.
3320 */
3321 static void
iperf_print_results(struct iperf_test * test)3322 iperf_print_results(struct iperf_test *test)
3323 {
3324
3325 cJSON *json_summary_streams = NULL;
3326
3327 int lower_mode, upper_mode;
3328 int current_mode;
3329
3330 int tmp_sender_has_retransmits = test->sender_has_retransmits;
3331
3332 /* print final summary for all intervals */
3333
3334 if (test->json_output) {
3335 json_summary_streams = cJSON_CreateArray();
3336 if (json_summary_streams == NULL)
3337 return;
3338 cJSON_AddItemToObject(test->json_end, "streams", json_summary_streams);
3339 } else {
3340 iperf_printf(test, "%s", report_bw_separator);
3341 if (test->verbose)
3342 iperf_printf(test, "%s", report_summary);
3343 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3344 if (test->sender_has_retransmits || test->other_side_has_retransmits) {
3345 if (test->bidirectional)
3346 iperf_printf(test, "%s", report_bw_retrans_header_bidir);
3347 else
3348 iperf_printf(test, "%s", report_bw_retrans_header);
3349 }
3350 else {
3351 if (test->bidirectional)
3352 iperf_printf(test, "%s", report_bw_header_bidir);
3353 else
3354 iperf_printf(test, "%s", report_bw_header);
3355 }
3356 } else {
3357 if (test->bidirectional)
3358 iperf_printf(test, "%s", report_bw_udp_header_bidir);
3359 else
3360 iperf_printf(test, "%s", report_bw_udp_header);
3361 }
3362 }
3363
3364 /*
3365 * We must to sum streams separately.
3366 * For bidirectional mode we must to display
3367 * information about sender and receiver streams.
3368 * For client side we must handle sender streams
3369 * firstly and receiver streams for server side.
3370 * The following design allows us to do this.
3371 */
3372
3373 if (test->mode == BIDIRECTIONAL) {
3374 if (test->role == 'c') {
3375 lower_mode = -1;
3376 upper_mode = 0;
3377 } else {
3378 lower_mode = 0;
3379 upper_mode = 1;
3380 }
3381 } else {
3382 lower_mode = test->mode;
3383 upper_mode = lower_mode;
3384 }
3385
3386
3387 for (current_mode = lower_mode; current_mode <= upper_mode; ++current_mode) {
3388 cJSON *json_summary_stream = NULL;
3389 int total_retransmits = 0;
3390 int total_packets = 0, lost_packets = 0;
3391 int sender_packet_count = 0, receiver_packet_count = 0; /* for this stream, this interval */
3392 int sender_total_packets = 0, receiver_total_packets = 0; /* running total */
3393 char ubuf[UNIT_LEN];
3394 char nbuf[UNIT_LEN];
3395 struct stat sb;
3396 char sbuf[UNIT_LEN];
3397 struct iperf_stream *sp = NULL;
3398 iperf_size_t bytes_sent, total_sent = 0;
3399 iperf_size_t bytes_received, total_received = 0;
3400 double start_time, end_time = 0.0, avg_jitter = 0.0, lost_percent = 0.0;
3401 double sender_time = 0.0, receiver_time = 0.0;
3402 struct iperf_time temp_time;
3403 double bandwidth;
3404
3405 char mbuf[UNIT_LEN];
3406 int stream_must_be_sender = current_mode * current_mode;
3407
3408
3409 /* Print stream role just for bidirectional mode. */
3410
3411 if (test->mode == BIDIRECTIONAL) {
3412 sprintf(mbuf, "[%s-%s]", stream_must_be_sender?"TX":"RX", test->role == 'c'?"C":"S");
3413 } else {
3414 mbuf[0] = '\0';
3415 }
3416
3417 /* Get sender_has_retransmits for each sender side (client and server) */
3418 if (test->mode == BIDIRECTIONAL && stream_must_be_sender)
3419 test->sender_has_retransmits = tmp_sender_has_retransmits;
3420 else if (test->mode == BIDIRECTIONAL && !stream_must_be_sender)
3421 test->sender_has_retransmits = test->other_side_has_retransmits;
3422
3423 start_time = 0.;
3424 sp = SLIST_FIRST(&test->streams);
3425
3426 /*
3427 * If there is at least one stream, then figure out the length of time
3428 * we were running the tests and print out some statistics about
3429 * the streams. It's possible to not have any streams at all
3430 * if the client got interrupted before it got to do anything.
3431 *
3432 * Also note that we try to keep seperate values for the sender
3433 * and receiver ending times. Earlier iperf (3.1 and earlier)
3434 * servers didn't send that to the clients, so in this case we fall
3435 * back to using the client's ending timestamp. The fallback is
3436 * basically emulating what iperf 3.1 did.
3437 */
3438
3439 if (sp) {
3440 iperf_time_diff(&sp->result->start_time, &sp->result->end_time, &temp_time);
3441 end_time = iperf_time_in_secs(&temp_time);
3442 if (sp->sender) {
3443 sp->result->sender_time = end_time;
3444 if (sp->result->receiver_time == 0.0) {
3445 sp->result->receiver_time = sp->result->sender_time;
3446 }
3447 }
3448 else {
3449 sp->result->receiver_time = end_time;
3450 if (sp->result->sender_time == 0.0) {
3451 sp->result->sender_time = sp->result->receiver_time;
3452 }
3453 }
3454 sender_time = sp->result->sender_time;
3455 receiver_time = sp->result->receiver_time;
3456 SLIST_FOREACH(sp, &test->streams, streams) {
3457 if (sp->sender == stream_must_be_sender) {
3458 if (test->json_output) {
3459 json_summary_stream = cJSON_CreateObject();
3460 if (json_summary_stream == NULL)
3461 return;
3462 cJSON_AddItemToArray(json_summary_streams, json_summary_stream);
3463 }
3464
3465 bytes_sent = sp->result->bytes_sent - sp->result->bytes_sent_omit;
3466 bytes_received = sp->result->bytes_received;
3467 total_sent += bytes_sent;
3468 total_received += bytes_received;
3469
3470 if (sp->sender) {
3471 sender_packet_count = sp->packet_count;
3472 receiver_packet_count = sp->peer_packet_count;
3473 }
3474 else {
3475 sender_packet_count = sp->peer_packet_count;
3476 receiver_packet_count = sp->packet_count;
3477 }
3478
3479 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3480 if (test->sender_has_retransmits) {
3481 total_retransmits += sp->result->stream_retrans;
3482 }
3483 } else {
3484 /*
3485 * Running total of the total number of packets. Use the sender packet count if we
3486 * have it, otherwise use the receiver packet count.
3487 */
3488 int packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count;
3489 total_packets += (packet_count - sp->omitted_packet_count);
3490 sender_total_packets += (sender_packet_count - sp->omitted_packet_count);
3491 receiver_total_packets += (receiver_packet_count - sp->omitted_packet_count);
3492 lost_packets += (sp->cnt_error - sp->omitted_cnt_error);
3493 avg_jitter += sp->jitter;
3494 }
3495
3496 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_sent, 'A');
3497 if (sender_time > 0.0) {
3498 bandwidth = (double) bytes_sent / (double) sender_time;
3499 }
3500 else {
3501 bandwidth = 0.0;
3502 }
3503 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3504 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3505 if (test->sender_has_retransmits) {
3506 /* Sender summary, TCP and SCTP with retransmits. */
3507 if (test->json_output)
3508 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_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));
3509 else
3510 if (test->role == 's' && !sp->sender) {
3511 if (test->verbose)
3512 iperf_printf(test, report_sender_not_available_format, sp->socket);
3513 }
3514 else {
3515 iperf_printf(test, report_bw_retrans_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, sp->result->stream_retrans, report_sender);
3516 }
3517 } else {
3518 /* Sender summary, TCP and SCTP without retransmits. */
3519 if (test->json_output)
3520 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));
3521 else
3522 if (test->role == 's' && !sp->sender) {
3523 if (test->verbose)
3524 iperf_printf(test, report_sender_not_available_format, sp->socket);
3525 }
3526 else {
3527 iperf_printf(test, report_bw_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, report_sender);
3528 }
3529 }
3530 } else {
3531 /* Sender summary, UDP. */
3532 if (sender_packet_count - sp->omitted_packet_count > 0) {
3533 lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (sender_packet_count - sp->omitted_packet_count);
3534 }
3535 else {
3536 lost_percent = 0.0;
3537 }
3538 if (test->json_output) {
3539 /*
3540 * For hysterical raisins, we only emit one JSON
3541 * object for the UDP summary, and it contains
3542 * information for both the sender and receiver
3543 * side.
3544 *
3545 * The JSON format as currently defined only includes one
3546 * value for the number of packets. We usually want that
3547 * to be the sender's value (how many packets were sent
3548 * by the sender). However this value might not be
3549 * available on the receiver in certain circumstances
3550 * specifically on the server side for a normal test or
3551 * the client side for a reverse-mode test. If this
3552 * is the case, then use the receiver's count of packets
3553 * instead.
3554 */
3555 int packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count;
3556 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));
3557 }
3558 else {
3559 /*
3560 * Due to ordering of messages on the control channel,
3561 * the server cannot report on client-side summary
3562 * statistics. If we're the server, omit one set of
3563 * summary statistics to avoid giving meaningless
3564 * results.
3565 */
3566 if (test->role == 's' && !sp->sender) {
3567 if (test->verbose)
3568 iperf_printf(test, report_sender_not_available_format, sp->socket);
3569 }
3570 else {
3571 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);
3572 }
3573 if ((sp->outoforder_packets - sp->omitted_outoforder_packets) > 0)
3574 iperf_printf(test, report_sum_outoforder, mbuf, start_time, sender_time, (sp->outoforder_packets - sp->omitted_outoforder_packets));
3575 }
3576 }
3577
3578 if (sp->diskfile_fd >= 0) {
3579 if (fstat(sp->diskfile_fd, &sb) == 0) {
3580 /* In the odd case that it's a zero-sized file, say it was all transferred. */
3581 int percent_sent = 100, percent_received = 100;
3582 if (sb.st_size > 0) {
3583 percent_sent = (int) ( ( (double) bytes_sent / (double) sb.st_size ) * 100.0 );
3584 percent_received = (int) ( ( (double) bytes_received / (double) sb.st_size ) * 100.0 );
3585 }
3586 unit_snprintf(sbuf, UNIT_LEN, (double) sb.st_size, 'A');
3587 if (test->json_output)
3588 cJSON_AddItemToObject(json_summary_stream, "diskfile", iperf_json_printf("sent: %d received: %d size: %d percent_sent: %d percent_received: %d filename: %s", (int64_t) bytes_sent, (int64_t) bytes_received, (int64_t) sb.st_size, (int64_t) percent_sent, (int64_t) percent_received, test->diskfile_name));
3589 else
3590 if (stream_must_be_sender) {
3591 iperf_printf(test, report_diskfile, ubuf, sbuf, percent_sent, test->diskfile_name);
3592 }
3593 else {
3594 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_received, 'A');
3595 iperf_printf(test, report_diskfile, ubuf, sbuf, percent_received, test->diskfile_name);
3596 }
3597 }
3598 }
3599
3600 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_received, 'A');
3601 if (receiver_time > 0) {
3602 bandwidth = (double) bytes_received / (double) receiver_time;
3603 }
3604 else {
3605 bandwidth = 0.0;
3606 }
3607 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3608 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3609 /* Receiver summary, TCP and SCTP */
3610 if (test->json_output)
3611 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));
3612 else
3613 if (test->role == 's' && sp->sender) {
3614 if (test->verbose)
3615 iperf_printf(test, report_receiver_not_available_format, sp->socket);
3616 }
3617 else {
3618 iperf_printf(test, report_bw_format, sp->socket, mbuf, start_time, receiver_time, ubuf, nbuf, report_receiver);
3619 }
3620 }
3621 else {
3622 /*
3623 * Receiver summary, UDP. Note that JSON was emitted with
3624 * the sender summary, so we only deal with human-readable
3625 * data here.
3626 */
3627 if (! test->json_output) {
3628 if (receiver_packet_count - sp->omitted_packet_count > 0) {
3629 lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (receiver_packet_count - sp->omitted_packet_count);
3630 }
3631 else {
3632 lost_percent = 0.0;
3633 }
3634
3635 if (test->role == 's' && sp->sender) {
3636 if (test->verbose)
3637 iperf_printf(test, report_receiver_not_available_format, sp->socket);
3638 }
3639 else {
3640 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);
3641 }
3642 }
3643 }
3644 }
3645 }
3646 }
3647
3648 if (test->num_streams > 1 || test->json_output) {
3649 unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A');
3650 /* If no tests were run, arbitrarily set bandwidth to 0. */
3651 if (sender_time > 0.0) {
3652 bandwidth = (double) total_sent / (double) sender_time;
3653 }
3654 else {
3655 bandwidth = 0.0;
3656 }
3657 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3658 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3659 if (test->sender_has_retransmits) {
3660 /* Summary sum, TCP with retransmits. */
3661 if (test->json_output)
3662 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));
3663 else
3664 if (test->role == 's' && !stream_must_be_sender) {
3665 if (test->verbose)
3666 iperf_printf(test, report_sender_not_available_summary_format, "SUM");
3667 }
3668 else {
3669 iperf_printf(test, report_sum_bw_retrans_format, mbuf, start_time, sender_time, ubuf, nbuf, total_retransmits, report_sender);
3670 }
3671 } else {
3672 /* Summary sum, TCP without retransmits. */
3673 if (test->json_output)
3674 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));
3675 else
3676 if (test->role == 's' && !stream_must_be_sender) {
3677 if (test->verbose)
3678 iperf_printf(test, report_sender_not_available_summary_format, "SUM");
3679 }
3680 else {
3681 iperf_printf(test, report_sum_bw_format, mbuf, start_time, sender_time, ubuf, nbuf, report_sender);
3682 }
3683 }
3684 unit_snprintf(ubuf, UNIT_LEN, (double) total_received, 'A');
3685 /* If no tests were run, set received bandwidth to 0 */
3686 if (receiver_time > 0.0) {
3687 bandwidth = (double) total_received / (double) receiver_time;
3688 }
3689 else {
3690 bandwidth = 0.0;
3691 }
3692 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3693 if (test->json_output)
3694 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));
3695 else
3696 if (test->role == 's' && stream_must_be_sender) {
3697 if (test->verbose)
3698 iperf_printf(test, report_receiver_not_available_summary_format, "SUM");
3699 }
3700 else {
3701 iperf_printf(test, report_sum_bw_format, mbuf, start_time, receiver_time, ubuf, nbuf, report_receiver);
3702 }
3703 } else {
3704 /* Summary sum, UDP. */
3705 avg_jitter /= test->num_streams;
3706 /* If no packets were sent, arbitrarily set loss percentage to 0. */
3707 if (total_packets > 0) {
3708 lost_percent = 100.0 * lost_packets / total_packets;
3709 }
3710 else {
3711 lost_percent = 0.0;
3712 }
3713 if (test->json_output)
3714 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));
3715 else {
3716 /*
3717 * On the client we have both sender and receiver overall summary
3718 * stats. On the server we have only the side that was on the
3719 * server. Output whatever we have.
3720 */
3721 if (! (test->role == 's' && !stream_must_be_sender) ) {
3722 unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A');
3723 iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, sender_time, ubuf, nbuf, 0.0, 0, sender_total_packets, 0.0, "sender");
3724 }
3725 if (! (test->role == 's' && stream_must_be_sender) ) {
3726
3727 unit_snprintf(ubuf, UNIT_LEN, (double) total_received, 'A');
3728 /* Compute received bandwidth. */
3729 if (end_time > 0.0) {
3730 bandwidth = (double) total_received / (double) receiver_time;
3731 }
3732 else {
3733 bandwidth = 0.0;
3734 }
3735 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3736 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");
3737 }
3738 }
3739 }
3740 }
3741
3742 if (test->json_output && current_mode == upper_mode) {
3743 cJSON_AddItemToObject(test->json_end, "cpu_utilization_percent", iperf_json_printf("host_total: %f host_user: %f host_system: %f remote_total: %f remote_user: %f remote_system: %f", (double) test->cpu_util[0], (double) test->cpu_util[1], (double) test->cpu_util[2], (double) test->remote_cpu_util[0], (double) test->remote_cpu_util[1], (double) test->remote_cpu_util[2]));
3744 if (test->protocol->id == Ptcp) {
3745 char *snd_congestion = NULL, *rcv_congestion = NULL;
3746 if (stream_must_be_sender) {
3747 snd_congestion = test->congestion_used;
3748 rcv_congestion = test->remote_congestion_used;
3749 }
3750 else {
3751 snd_congestion = test->remote_congestion_used;
3752 rcv_congestion = test->congestion_used;
3753 }
3754 if (snd_congestion) {
3755 cJSON_AddStringToObject(test->json_end, "sender_tcp_congestion", snd_congestion);
3756 }
3757 if (rcv_congestion) {
3758 cJSON_AddStringToObject(test->json_end, "receiver_tcp_congestion", rcv_congestion);
3759 }
3760 }
3761 }
3762 else {
3763 if (test->verbose) {
3764 if (stream_must_be_sender) {
3765 if (test->bidirectional) {
3766 iperf_printf(test, report_cpu, report_local, stream_must_be_sender?report_sender:report_receiver, test->cpu_util[0], test->cpu_util[1], test->cpu_util[2], report_remote, stream_must_be_sender?report_receiver:report_sender, test->remote_cpu_util[0], test->remote_cpu_util[1], test->remote_cpu_util[2]);
3767 iperf_printf(test, report_cpu, report_local, !stream_must_be_sender?report_sender:report_receiver, test->cpu_util[0], test->cpu_util[1], test->cpu_util[2], report_remote, !stream_must_be_sender?report_receiver:report_sender, test->remote_cpu_util[0], test->remote_cpu_util[1], test->remote_cpu_util[2]);
3768 } else
3769 iperf_printf(test, report_cpu, report_local, stream_must_be_sender?report_sender:report_receiver, test->cpu_util[0], test->cpu_util[1], test->cpu_util[2], report_remote, stream_must_be_sender?report_receiver:report_sender, test->remote_cpu_util[0], test->remote_cpu_util[1], test->remote_cpu_util[2]);
3770 }
3771 if (test->protocol->id == Ptcp) {
3772 char *snd_congestion = NULL, *rcv_congestion = NULL;
3773 if (stream_must_be_sender) {
3774 snd_congestion = test->congestion_used;
3775 rcv_congestion = test->remote_congestion_used;
3776 }
3777 else {
3778 snd_congestion = test->remote_congestion_used;
3779 rcv_congestion = test->congestion_used;
3780 }
3781 if (snd_congestion) {
3782 iperf_printf(test, "snd_tcp_congestion %s\n", snd_congestion);
3783 }
3784 if (rcv_congestion) {
3785 iperf_printf(test, "rcv_tcp_congestion %s\n", rcv_congestion);
3786 }
3787 }
3788 }
3789
3790 /* Print server output if we're on the client and it was requested/provided */
3791 if (test->role == 'c' && iperf_get_test_get_server_output(test) && !test->json_output) {
3792 if (test->json_server_output) {
3793 char *str = cJSON_Print(test->json_server_output);
3794 iperf_printf(test, "\nServer JSON output:\n%s\n", str);
3795 cJSON_free(str);
3796 cJSON_Delete(test->json_server_output);
3797 test->json_server_output = NULL;
3798 }
3799 if (test->server_output_text) {
3800 iperf_printf(test, "\nServer output:\n%s\n", test->server_output_text);
3801 test->server_output_text = NULL;
3802 }
3803 }
3804 }
3805 }
3806
3807 /* Set real sender_has_retransmits for current side */
3808 if (test->mode == BIDIRECTIONAL)
3809 test->sender_has_retransmits = tmp_sender_has_retransmits;
3810 }
3811
3812 /**************************************************************************/
3813
3814 /**
3815 * Main report-printing callback.
3816 * Prints results either during a test (interval report only) or
3817 * after the entire test has been run (last interval report plus
3818 * overall summary).
3819 */
3820 void
iperf_reporter_callback(struct iperf_test * test)3821 iperf_reporter_callback(struct iperf_test *test)
3822 {
3823 switch (test->state) {
3824 case TEST_RUNNING:
3825 case STREAM_RUNNING:
3826 /* print interval results for each stream */
3827 iperf_print_intermediate(test);
3828 break;
3829 case TEST_END:
3830 case DISPLAY_RESULTS:
3831 iperf_print_intermediate(test);
3832 iperf_print_results(test);
3833 break;
3834 }
3835
3836 }
3837
3838 /**
3839 * Print the interval results for one stream.
3840 * This function needs to know about the overall test so it can determine the
3841 * context for printing headers, separators, etc.
3842 */
3843 static void
print_interval_results(struct iperf_test * test,struct iperf_stream * sp,cJSON * json_interval_streams)3844 print_interval_results(struct iperf_test *test, struct iperf_stream *sp, cJSON *json_interval_streams)
3845 {
3846 char ubuf[UNIT_LEN];
3847 char nbuf[UNIT_LEN];
3848 char cbuf[UNIT_LEN];
3849 char mbuf[UNIT_LEN];
3850 char zbuf[] = " ";
3851 double st = 0., et = 0.;
3852 struct iperf_time temp_time;
3853 struct iperf_interval_results *irp = NULL;
3854 double bandwidth, lost_percent;
3855
3856 if (test->mode == BIDIRECTIONAL) {
3857 sprintf(mbuf, "[%s-%s]", sp->sender?"TX":"RX", test->role == 'c'?"C":"S");
3858 } else {
3859 mbuf[0] = '\0';
3860 zbuf[0] = '\0';
3861 }
3862
3863 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); /* get last entry in linked list */
3864 if (irp == NULL) {
3865 iperf_err(test, "print_interval_results error: interval_results is NULL");
3866 return;
3867 }
3868 if (!test->json_output) {
3869 /* First stream? */
3870 if (sp == SLIST_FIRST(&test->streams)) {
3871 /* It it's the first interval, print the header;
3872 ** else if there's more than one stream, print the separator;
3873 ** else nothing.
3874 */
3875 if (iperf_time_compare(&sp->result->start_time, &irp->interval_start_time) == 0) {
3876 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3877 if (test->sender_has_retransmits == 1) {
3878 if (test->bidirectional)
3879 iperf_printf(test, "%s", report_bw_retrans_cwnd_header_bidir);
3880 else
3881 iperf_printf(test, "%s", report_bw_retrans_cwnd_header);
3882 }
3883 else {
3884 if (test->bidirectional)
3885 iperf_printf(test, "%s", report_bw_header_bidir);
3886 else
3887 iperf_printf(test, "%s", report_bw_header);
3888 }
3889 } else {
3890 if (test->mode == SENDER) {
3891 iperf_printf(test, "%s", report_bw_udp_sender_header);
3892 } else if (test->mode == RECEIVER){
3893 iperf_printf(test, "%s", report_bw_udp_header);
3894 } else {
3895 /* BIDIRECTIONAL */
3896 iperf_printf(test, "%s", report_bw_udp_header_bidir);
3897 }
3898 }
3899 } else if (test->num_streams > 1)
3900 iperf_printf(test, "%s", report_bw_separator);
3901 }
3902 }
3903
3904 unit_snprintf(ubuf, UNIT_LEN, (double) (irp->bytes_transferred), 'A');
3905 if (irp->interval_duration > 0.0) {
3906 bandwidth = (double) irp->bytes_transferred / (double) irp->interval_duration;
3907 }
3908 else {
3909 bandwidth = 0.0;
3910 }
3911 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3912
3913 iperf_time_diff(&sp->result->start_time, &irp->interval_start_time, &temp_time);
3914 st = iperf_time_in_secs(&temp_time);
3915 iperf_time_diff(&sp->result->start_time, &irp->interval_end_time, &temp_time);
3916 et = iperf_time_in_secs(&temp_time);
3917
3918 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3919 if (test->sender_has_retransmits == 1 && sp->sender) {
3920 /* Interval, TCP with retransmits. */
3921 if (test->json_output)
3922 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));
3923 else {
3924 unit_snprintf(cbuf, UNIT_LEN, irp->snd_cwnd, 'A');
3925 iperf_printf(test, report_bw_retrans_cwnd_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->interval_retrans, cbuf, irp->omitted?report_omitted:"");
3926 }
3927 } else {
3928 /* Interval, TCP without retransmits. */
3929 if (test->json_output)
3930 cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, irp->omitted, sp->sender));
3931 else
3932 iperf_printf(test, report_bw_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->omitted?report_omitted:"");
3933 }
3934 } else {
3935 /* Interval, UDP. */
3936 if (sp->sender) {
3937 if (test->json_output)
3938 cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f packets: %d omitted: %b 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_packet_count, irp->omitted, sp->sender));
3939 else
3940 iperf_printf(test, report_bw_udp_sender_format, sp->socket, mbuf, st, et, ubuf, nbuf, zbuf, irp->interval_packet_count, irp->omitted?report_omitted:"");
3941 } else {
3942 if (irp->interval_packet_count > 0) {
3943 lost_percent = 100.0 * irp->interval_cnt_error / irp->interval_packet_count;
3944 }
3945 else {
3946 lost_percent = 0.0;
3947 }
3948 if (test->json_output)
3949 cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d start: %f end: %f seconds: %f bytes: %d bits_per_second: %f jitter_ms: %f lost_packets: %d packets: %d lost_percent: %f omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, (double) irp->jitter * 1000.0, (int64_t) irp->interval_cnt_error, (int64_t) irp->interval_packet_count, (double) lost_percent, irp->omitted, sp->sender));
3950 else
3951 iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->jitter * 1000.0, irp->interval_cnt_error, irp->interval_packet_count, lost_percent, irp->omitted?report_omitted:"");
3952 }
3953 }
3954
3955 if (test->logfile || test->forceflush)
3956 iflush(test);
3957 }
3958
3959 /**************************************************************************/
3960 void
iperf_free_stream(struct iperf_stream * sp)3961 iperf_free_stream(struct iperf_stream *sp)
3962 {
3963 struct iperf_interval_results *irp, *nirp;
3964
3965 /* XXX: need to free interval list too! */
3966 munmap(sp->buffer, sp->test->settings->blksize);
3967 close(sp->buffer_fd);
3968 if (sp->diskfile_fd >= 0)
3969 close(sp->diskfile_fd);
3970 for (irp = TAILQ_FIRST(&sp->result->interval_results); irp != NULL; irp = nirp) {
3971 nirp = TAILQ_NEXT(irp, irlistentries);
3972 free(irp);
3973 }
3974 free(sp->result);
3975 if (sp->send_timer != NULL)
3976 tmr_cancel(sp->send_timer);
3977 free(sp);
3978 }
3979
3980 /**************************************************************************/
3981 struct iperf_stream *
iperf_new_stream(struct iperf_test * test,int s,int sender)3982 iperf_new_stream(struct iperf_test *test, int s, int sender)
3983 {
3984 struct iperf_stream *sp;
3985 int ret = 0;
3986
3987 char template[1024];
3988 if (test->tmp_template) {
3989 snprintf(template, sizeof(template) / sizeof(char), "%s", test->tmp_template);
3990 } else {
3991 //find the system temporary dir *unix, windows, cygwin support
3992 char* tempdir = getenv("TMPDIR");
3993 if (tempdir == 0){
3994 tempdir = getenv("TEMP");
3995 }
3996 if (tempdir == 0){
3997 tempdir = getenv("TMP");
3998 }
3999 if (tempdir == 0){
4000 tempdir = "/tmp";
4001 }
4002 snprintf(template, sizeof(template) / sizeof(char), "%s/iperf3.XXXXXX", tempdir);
4003 }
4004
4005 sp = (struct iperf_stream *) malloc(sizeof(struct iperf_stream));
4006 if (!sp) {
4007 i_errno = IECREATESTREAM;
4008 return NULL;
4009 }
4010
4011 memset(sp, 0, sizeof(struct iperf_stream));
4012
4013 sp->sender = sender;
4014 sp->test = test;
4015 sp->settings = test->settings;
4016 sp->result = (struct iperf_stream_result *) malloc(sizeof(struct iperf_stream_result));
4017 if (!sp->result) {
4018 free(sp);
4019 i_errno = IECREATESTREAM;
4020 return NULL;
4021 }
4022
4023 memset(sp->result, 0, sizeof(struct iperf_stream_result));
4024 TAILQ_INIT(&sp->result->interval_results);
4025
4026 /* Create and randomize the buffer */
4027 sp->buffer_fd = mkstemp(template);
4028 if (sp->buffer_fd == -1) {
4029 i_errno = IECREATESTREAM;
4030 free(sp->result);
4031 free(sp);
4032 return NULL;
4033 }
4034 if (unlink(template) < 0) {
4035 i_errno = IECREATESTREAM;
4036 free(sp->result);
4037 free(sp);
4038 return NULL;
4039 }
4040 if (ftruncate(sp->buffer_fd, test->settings->blksize) < 0) {
4041 i_errno = IECREATESTREAM;
4042 free(sp->result);
4043 free(sp);
4044 return NULL;
4045 }
4046 sp->buffer = (char *) mmap(NULL, test->settings->blksize, PROT_READ|PROT_WRITE, MAP_PRIVATE, sp->buffer_fd, 0);
4047 if (sp->buffer == MAP_FAILED) {
4048 i_errno = IECREATESTREAM;
4049 free(sp->result);
4050 free(sp);
4051 return NULL;
4052 }
4053 sp->pending_size = 0;
4054
4055 /* Set socket */
4056 sp->socket = s;
4057
4058 sp->snd = test->protocol->send;
4059 sp->rcv = test->protocol->recv;
4060
4061 if (test->diskfile_name != (char*) 0) {
4062 sp->diskfile_fd = open(test->diskfile_name, sender ? O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC), S_IRUSR|S_IWUSR);
4063 if (sp->diskfile_fd == -1) {
4064 i_errno = IEFILE;
4065 munmap(sp->buffer, sp->test->settings->blksize);
4066 free(sp->result);
4067 free(sp);
4068 return NULL;
4069 }
4070 sp->snd2 = sp->snd;
4071 sp->snd = diskfile_send;
4072 sp->rcv2 = sp->rcv;
4073 sp->rcv = diskfile_recv;
4074 } else
4075 sp->diskfile_fd = -1;
4076
4077 /* Initialize stream */
4078 if (test->repeating_payload)
4079 fill_with_repeating_pattern(sp->buffer, test->settings->blksize);
4080 else
4081 ret = readentropy(sp->buffer, test->settings->blksize);
4082
4083 if ((ret < 0) || (iperf_init_stream(sp, test) < 0)) {
4084 close(sp->buffer_fd);
4085 munmap(sp->buffer, sp->test->settings->blksize);
4086 free(sp->result);
4087 free(sp);
4088 return NULL;
4089 }
4090 iperf_add_stream(test, sp);
4091
4092 return sp;
4093 }
4094
4095 /**************************************************************************/
4096 int
iperf_init_stream(struct iperf_stream * sp,struct iperf_test * test)4097 iperf_init_stream(struct iperf_stream *sp, struct iperf_test *test)
4098 {
4099 socklen_t len;
4100 int opt;
4101
4102 len = sizeof(struct sockaddr_storage);
4103 if (getsockname(sp->socket, (struct sockaddr *) &sp->local_addr, &len) < 0) {
4104 i_errno = IEINITSTREAM;
4105 return -1;
4106 }
4107 len = sizeof(struct sockaddr_storage);
4108 if (getpeername(sp->socket, (struct sockaddr *) &sp->remote_addr, &len) < 0) {
4109 i_errno = IEINITSTREAM;
4110 return -1;
4111 }
4112
4113 /* Set IP TOS */
4114 if ((opt = test->settings->tos)) {
4115 if (getsockdomain(sp->socket) == AF_INET6) {
4116 #ifdef IPV6_TCLASS
4117 if (setsockopt(sp->socket, IPPROTO_IPV6, IPV6_TCLASS, &opt, sizeof(opt)) < 0) {
4118 i_errno = IESETCOS;
4119 return -1;
4120 }
4121 #else
4122 i_errno = IESETCOS;
4123 return -1;
4124 #endif
4125 } else {
4126 if (setsockopt(sp->socket, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)) < 0) {
4127 i_errno = IESETTOS;
4128 return -1;
4129 }
4130 }
4131 }
4132
4133 #if defined(HAVE_DONT_FRAGMENT)
4134 /* Set Don't Fragment (DF). Only applicable to IPv4/UDP tests. */
4135 if (iperf_get_test_protocol_id(test) == Pudp &&
4136 getsockdomain(sp->socket) == AF_INET &&
4137 iperf_get_dont_fragment(test)) {
4138
4139 /*
4140 * There are multiple implementations of this feature depending on the OS.
4141 * We need to handle separately Linux, UNIX, and Windows, as well as
4142 * the case that DF isn't supported at all (such as on macOS).
4143 */
4144 #if defined(IP_MTU_DISCOVER) /* Linux version of IP_DONTFRAG */
4145 opt = IP_PMTUDISC_DO;
4146 if (setsockopt(sp->socket, IPPROTO_IP, IP_MTU_DISCOVER, &opt, sizeof(opt)) < 0) {
4147 i_errno = IESETDONTFRAGMENT;
4148 return -1;
4149 }
4150 #else
4151 #if defined(IP_DONTFRAG) /* UNIX does IP_DONTFRAG */
4152 opt = 1;
4153 if (setsockopt(sp->socket, IPPROTO_IP, IP_DONTFRAG, &opt, sizeof(opt)) < 0) {
4154 i_errno = IESETDONTFRAGMENT;
4155 return -1;
4156 }
4157 #else
4158 #if defined(IP_DONTFRAGMENT) /* Windows does IP_DONTFRAGMENT */
4159 opt = 1;
4160 if (setsockopt(sp->socket, IPPROTO_IP, IP_DONTFRAGMENT, &opt, sizeof(opt)) < 0) {
4161 i_errno = IESETDONTFRAGMENT;
4162 return -1;
4163 }
4164 #else
4165 i_errno = IESETDONTFRAGMENT;
4166 return -1;
4167 #endif /* IP_DONTFRAGMENT */
4168 #endif /* IP_DONTFRAG */
4169 #endif /* IP_MTU_DISCOVER */
4170 }
4171 #endif /* HAVE_DONT_FRAGMENT */
4172 return 0;
4173 }
4174
4175 /**************************************************************************/
4176 void
iperf_add_stream(struct iperf_test * test,struct iperf_stream * sp)4177 iperf_add_stream(struct iperf_test *test, struct iperf_stream *sp)
4178 {
4179 int i;
4180 struct iperf_stream *n, *prev;
4181
4182 if (SLIST_EMPTY(&test->streams)) {
4183 SLIST_INSERT_HEAD(&test->streams, sp, streams);
4184 sp->id = 1;
4185 } else {
4186 // for (n = test->streams, i = 2; n->next; n = n->next, ++i);
4187 i = 2;
4188 SLIST_FOREACH(n, &test->streams, streams) {
4189 prev = n;
4190 ++i;
4191 }
4192 SLIST_INSERT_AFTER(prev, sp, streams);
4193 sp->id = i;
4194 }
4195 }
4196
4197 /* This pair of routines gets inserted into the snd/rcv function pointers
4198 ** when there's a -F flag. They handle the file stuff and call the real
4199 ** snd/rcv functions, which have been saved in snd2/rcv2.
4200 **
4201 ** The advantage of doing it this way is that in the much more common
4202 ** case of no -F flag, there is zero extra overhead.
4203 */
4204
4205 static int
diskfile_send(struct iperf_stream * sp)4206 diskfile_send(struct iperf_stream *sp)
4207 {
4208 int r;
4209 int buffer_left = sp->diskfile_left; // represents total data in buffer to be sent out
4210 static int rtot;
4211
4212 /* if needed, read enough data from the disk to fill up the buffer */
4213 if (sp->diskfile_left < sp->test->settings->blksize && !sp->test->done) {
4214 r = read(sp->diskfile_fd, sp->buffer, sp->test->settings->blksize -
4215 sp->diskfile_left);
4216 buffer_left += r;
4217 rtot += r;
4218 if (sp->test->debug) {
4219 printf("read %d bytes from file, %d total\n", r, rtot);
4220 }
4221
4222 // If the buffer doesn't contain a full buffer at this point,
4223 // adjust the size of the data to send.
4224 if (buffer_left != sp->test->settings->blksize) {
4225 if (sp->test->debug)
4226 printf("possible eof\n");
4227 // setting data size to be sent,
4228 // which is less than full block/buffer size
4229 // (to be used by iperf_tcp_send, etc.)
4230 sp->pending_size = buffer_left;
4231 }
4232
4233 // If there's no work left, we're done.
4234 if (buffer_left == 0) {
4235 sp->test->done = 1;
4236 if (sp->test->debug)
4237 printf("done\n");
4238 }
4239 }
4240
4241 // If there's no data left in the file or in the buffer, we're done.
4242 // No more data available to be sent.
4243 // Return without sending data to the network
4244 if( sp->test->done || buffer_left == 0 ){
4245 if (sp->test->debug)
4246 printf("already done\n");
4247 sp->test->done = 1;
4248 return 0;
4249 }
4250
4251 r = sp->snd2(sp);
4252 if (r < 0) {
4253 return r;
4254 }
4255 /*
4256 * Compute how much data is in the buffer but didn't get sent.
4257 * If there are bytes that got left behind, slide them to the
4258 * front of the buffer so they can hopefully go out on the next
4259 * pass.
4260 */
4261 sp->diskfile_left = buffer_left - r;
4262 if (sp->diskfile_left && sp->diskfile_left < sp->test->settings->blksize) {
4263 memcpy(sp->buffer,
4264 sp->buffer + (sp->test->settings->blksize - sp->diskfile_left),
4265 sp->diskfile_left);
4266 if (sp->test->debug)
4267 printf("Shifting %d bytes by %d\n", sp->diskfile_left, (sp->test->settings->blksize - sp->diskfile_left));
4268 }
4269 return r;
4270 }
4271
4272 static int
diskfile_recv(struct iperf_stream * sp)4273 diskfile_recv(struct iperf_stream *sp)
4274 {
4275 int r;
4276
4277 r = sp->rcv2(sp);
4278 if (r > 0) {
4279 (void) write(sp->diskfile_fd, sp->buffer, r);
4280 (void) fsync(sp->diskfile_fd);
4281 }
4282 return r;
4283 }
4284
4285
4286 void
iperf_catch_sigend(void (* handler)(int))4287 iperf_catch_sigend(void (*handler)(int))
4288 {
4289 #ifdef SIGINT
4290 signal(SIGINT, handler);
4291 #endif
4292 #ifdef SIGTERM
4293 signal(SIGTERM, handler);
4294 #endif
4295 #ifdef SIGHUP
4296 signal(SIGHUP, handler);
4297 #endif
4298 }
4299
4300 /**
4301 * Called as a result of getting a signal.
4302 * Depending on the current state of the test (and the role of this
4303 * process) compute and report one more set of ending statistics
4304 * before cleaning up and exiting.
4305 */
4306 void
iperf_got_sigend(struct iperf_test * test)4307 iperf_got_sigend(struct iperf_test *test)
4308 {
4309 /*
4310 * If we're the client, or if we're a server and running a test,
4311 * then dump out the accumulated stats so far.
4312 */
4313 if (test->role == 'c' ||
4314 (test->role == 's' && test->state == TEST_RUNNING)) {
4315
4316 test->done = 1;
4317 cpu_util(test->cpu_util);
4318 test->stats_callback(test);
4319 test->state = DISPLAY_RESULTS; /* change local state only */
4320 if (test->on_test_finish)
4321 test->on_test_finish(test);
4322 test->reporter_callback(test);
4323 }
4324
4325 if (test->ctrl_sck >= 0) {
4326 test->state = (test->role == 'c') ? CLIENT_TERMINATE : SERVER_TERMINATE;
4327 (void) Nwrite(test->ctrl_sck, (char*) &test->state, sizeof(signed char), Ptcp);
4328 }
4329 i_errno = (test->role == 'c') ? IECLIENTTERM : IESERVERTERM;
4330 iperf_errexit(test, "interrupt - %s", iperf_strerror(i_errno));
4331 }
4332
4333 /* Try to write a PID file if requested, return -1 on an error. */
4334 int
iperf_create_pidfile(struct iperf_test * test)4335 iperf_create_pidfile(struct iperf_test *test)
4336 {
4337 if (test->pidfile) {
4338 int fd;
4339 char buf[8];
4340
4341 /* See if the file already exists and we can read it. */
4342 fd = open(test->pidfile, O_RDONLY, 0);
4343 if (fd >= 0) {
4344 if (read(fd, buf, sizeof(buf) - 1) >= 0) {
4345
4346 /* We read some bytes, see if they correspond to a valid PID */
4347 pid_t pid;
4348 pid = atoi(buf);
4349 if (pid > 0) {
4350
4351 /* See if the process exists. */
4352 if (kill(pid, 0) == 0) {
4353 /*
4354 * Make sure not to try to delete existing PID file by
4355 * scribbling over the pathname we'd use to refer to it.
4356 * Then exit with an error.
4357 */
4358 free(test->pidfile);
4359 test->pidfile = NULL;
4360 iperf_errexit(test, "Another instance of iperf3 appears to be running");
4361 }
4362 }
4363 }
4364 }
4365
4366 /*
4367 * File didn't exist, we couldn't read it, or it didn't correspond to
4368 * a running process. Try to create it.
4369 */
4370 fd = open(test->pidfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR);
4371 if (fd < 0) {
4372 return -1;
4373 }
4374 snprintf(buf, sizeof(buf), "%d", getpid()); /* no trailing newline */
4375 if (write(fd, buf, strlen(buf)) < 0) {
4376 return -1;
4377 }
4378 if (close(fd) < 0) {
4379 return -1;
4380 };
4381 }
4382 return 0;
4383 }
4384
4385 /* Get rid of a PID file, return -1 on error. */
4386 int
iperf_delete_pidfile(struct iperf_test * test)4387 iperf_delete_pidfile(struct iperf_test *test)
4388 {
4389 if (test->pidfile) {
4390 if (unlink(test->pidfile) < 0) {
4391 return -1;
4392 }
4393 }
4394 return 0;
4395 }
4396
4397 int
iperf_json_start(struct iperf_test * test)4398 iperf_json_start(struct iperf_test *test)
4399 {
4400 test->json_top = cJSON_CreateObject();
4401 if (test->json_top == NULL)
4402 return -1;
4403 test->json_start = cJSON_CreateObject();
4404 if (test->json_start == NULL)
4405 return -1;
4406 cJSON_AddItemToObject(test->json_top, "start", test->json_start);
4407 test->json_connected = cJSON_CreateArray();
4408 if (test->json_connected == NULL)
4409 return -1;
4410 cJSON_AddItemToObject(test->json_start, "connected", test->json_connected);
4411 test->json_intervals = cJSON_CreateArray();
4412 if (test->json_intervals == NULL)
4413 return -1;
4414 cJSON_AddItemToObject(test->json_top, "intervals", test->json_intervals);
4415 test->json_end = cJSON_CreateObject();
4416 if (test->json_end == NULL)
4417 return -1;
4418 cJSON_AddItemToObject(test->json_top, "end", test->json_end);
4419 return 0;
4420 }
4421
4422 int
iperf_json_finish(struct iperf_test * test)4423 iperf_json_finish(struct iperf_test *test)
4424 {
4425 if (test->title)
4426 cJSON_AddStringToObject(test->json_top, "title", test->title);
4427 if (test->extra_data)
4428 cJSON_AddStringToObject(test->json_top, "extra_data", test->extra_data);
4429 /* Include server output */
4430 if (test->json_server_output) {
4431 cJSON_AddItemToObject(test->json_top, "server_output_json", test->json_server_output);
4432 }
4433 if (test->server_output_text) {
4434 cJSON_AddStringToObject(test->json_top, "server_output_text", test->server_output_text);
4435 }
4436 // Get ASCII rendering of JSON structure. Then make our
4437 // own copy of it and return the storage that cJSON allocated
4438 // on our behalf. We keep our own copy around.
4439 char *str = cJSON_Print(test->json_top);
4440 if (str == NULL)
4441 return -1;
4442 test->json_output_string = strdup(str);
4443 cJSON_free(str);
4444 if (test->json_output_string == NULL)
4445 return -1;
4446 fprintf(test->outfile, "%s\n", test->json_output_string);
4447 iflush(test);
4448 cJSON_Delete(test->json_top);
4449 test->json_top = test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL;
4450 return 0;
4451 }
4452
4453
4454 /* CPU affinity stuff - Linux, FreeBSD, and Windows only. */
4455
4456 int
iperf_setaffinity(struct iperf_test * test,int affinity)4457 iperf_setaffinity(struct iperf_test *test, int affinity)
4458 {
4459 #if defined(HAVE_SCHED_SETAFFINITY)
4460 cpu_set_t cpu_set;
4461
4462 CPU_ZERO(&cpu_set);
4463 CPU_SET(affinity, &cpu_set);
4464 if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0) {
4465 i_errno = IEAFFINITY;
4466 return -1;
4467 }
4468 return 0;
4469 #elif defined(HAVE_CPUSET_SETAFFINITY)
4470 cpuset_t cpumask;
4471
4472 if(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
4473 sizeof(cpuset_t), &test->cpumask) != 0) {
4474 i_errno = IEAFFINITY;
4475 return -1;
4476 }
4477
4478 CPU_ZERO(&cpumask);
4479 CPU_SET(affinity, &cpumask);
4480
4481 if(cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_PID, -1,
4482 sizeof(cpuset_t), &cpumask) != 0) {
4483 i_errno = IEAFFINITY;
4484 return -1;
4485 }
4486 return 0;
4487 #elif defined(HAVE_SETPROCESSAFFINITYMASK)
4488 HANDLE process = GetCurrentProcess();
4489 DWORD_PTR processAffinityMask = 1 << affinity;
4490
4491 if (SetProcessAffinityMask(process, processAffinityMask) == 0) {
4492 i_errno = IEAFFINITY;
4493 return -1;
4494 }
4495 return 0;
4496 #else /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */
4497 i_errno = IEAFFINITY;
4498 return -1;
4499 #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */
4500 }
4501
4502 int
iperf_clearaffinity(struct iperf_test * test)4503 iperf_clearaffinity(struct iperf_test *test)
4504 {
4505 #if defined(HAVE_SCHED_SETAFFINITY)
4506 cpu_set_t cpu_set;
4507 int i;
4508
4509 CPU_ZERO(&cpu_set);
4510 for (i = 0; i < CPU_SETSIZE; ++i)
4511 CPU_SET(i, &cpu_set);
4512 if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0) {
4513 i_errno = IEAFFINITY;
4514 return -1;
4515 }
4516 return 0;
4517 #elif defined(HAVE_CPUSET_SETAFFINITY)
4518 if(cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_PID, -1,
4519 sizeof(cpuset_t), &test->cpumask) != 0) {
4520 i_errno = IEAFFINITY;
4521 return -1;
4522 }
4523 return 0;
4524 #elif defined(HAVE_SETPROCESSAFFINITYMASK)
4525 HANDLE process = GetCurrentProcess();
4526 DWORD_PTR processAffinityMask;
4527 DWORD_PTR lpSystemAffinityMask;
4528
4529 if (GetProcessAffinityMask(process, &processAffinityMask, &lpSystemAffinityMask) == 0
4530 || SetProcessAffinityMask(process, lpSystemAffinityMask) == 0) {
4531 i_errno = IEAFFINITY;
4532 return -1;
4533 }
4534 return 0;
4535 #else /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */
4536 i_errno = IEAFFINITY;
4537 return -1;
4538 #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */
4539 }
4540
4541 static char iperf_timestr[100];
4542 static char linebuffer[1024];
4543
4544 int
iperf_printf(struct iperf_test * test,const char * format,...)4545 iperf_printf(struct iperf_test *test, const char* format, ...)
4546 {
4547 va_list argp;
4548 int r = 0, r0;
4549 time_t now;
4550 struct tm *ltm = NULL;
4551 char *ct = NULL;
4552
4553 /* Timestamp if requested */
4554 if (iperf_get_test_timestamps(test)) {
4555 time(&now);
4556 ltm = localtime(&now);
4557 strftime(iperf_timestr, sizeof(iperf_timestr), iperf_get_test_timestamp_format(test), ltm);
4558 ct = iperf_timestr;
4559 }
4560
4561 /*
4562 * There are roughly two use cases here. If we're the client,
4563 * want to print stuff directly to the output stream.
4564 * If we're the sender we might need to buffer up output to send
4565 * to the client.
4566 *
4567 * This doesn't make a whole lot of difference except there are
4568 * some chunks of output on the client (on particular the whole
4569 * of the server output with --get-server-output) that could
4570 * easily exceed the size of the line buffer, but which don't need
4571 * to be buffered up anyway.
4572 */
4573 if (test->role == 'c') {
4574 if (ct) {
4575 r0 = fprintf(test->outfile, "%s", ct);
4576 if (r0 < 0)
4577 return r0;
4578 r += r0;
4579 }
4580 if (test->title) {
4581 r0 = fprintf(test->outfile, "%s: ", test->title);
4582 if (r0 < 0)
4583 return r0;
4584 r += r0;
4585 }
4586 va_start(argp, format);
4587 r0 = vfprintf(test->outfile, format, argp);
4588 va_end(argp);
4589 if (r0 < 0)
4590 return r0;
4591 r += r0;
4592 }
4593 else if (test->role == 's') {
4594 if (ct) {
4595 r0 = snprintf(linebuffer, sizeof(linebuffer), "%s", ct);
4596 if (r0 < 0)
4597 return r0;
4598 r += r0;
4599 }
4600 /* Should always be true as long as sizeof(ct) < sizeof(linebuffer) */
4601 if (r < sizeof(linebuffer)) {
4602 va_start(argp, format);
4603 r0 = vsnprintf(linebuffer + r, sizeof(linebuffer) - r, format, argp);
4604 va_end(argp);
4605 if (r0 < 0)
4606 return r0;
4607 r += r0;
4608 }
4609 fprintf(test->outfile, "%s", linebuffer);
4610
4611 if (test->role == 's' && iperf_get_test_get_server_output(test)) {
4612 struct iperf_textline *l = (struct iperf_textline *) malloc(sizeof(struct iperf_textline));
4613 l->line = strdup(linebuffer);
4614 TAILQ_INSERT_TAIL(&(test->server_output_list), l, textlineentries);
4615 }
4616 }
4617 return r;
4618 }
4619
4620 int
iflush(struct iperf_test * test)4621 iflush(struct iperf_test *test)
4622 {
4623 return fflush(test->outfile);
4624 }
4625