1 /*
2  * iperf, Copyright (c) 2014-2020, 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
25  * file for complete information.
26  */
27 #ifndef __IPERF_H
28 #define __IPERF_H
29 
30 #include "iperf_config.h"
31 
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #ifdef HAVE_STDINT_H
35 #include <stdint.h>
36 #endif
37 #include <sys/select.h>
38 #include <sys/socket.h>
39 #ifndef _GNU_SOURCE
40 # define _GNU_SOURCE
41 #endif
42 #ifdef HAVE_LINUX_TCP_H
43 #include <linux/tcp.h>
44 #else
45 #include <netinet/tcp.h>
46 #endif
47 #include <net/if.h> // for IFNAMSIZ
48 
49 #if defined(HAVE_CPUSET_SETAFFINITY)
50 #include <sys/param.h>
51 #include <sys/cpuset.h>
52 #endif /* HAVE_CPUSET_SETAFFINITY */
53 
54 #if defined(HAVE_INTTYPES_H)
55 # include <inttypes.h>
56 #else
57 # ifndef PRIu64
58 #  if sizeof(long) == 8
59 #   define PRIu64		"lu"
60 #  else
61 #   define PRIu64		"llu"
62 #  endif
63 # endif
64 #endif
65 
66 #include "timer.h"
67 #include "queue.h"
68 #include "cjson.h"
69 #include "iperf_time.h"
70 
71 #if defined(HAVE_SSL)
72 #include <openssl/bio.h>
73 #include <openssl/evp.h>
74 #endif // HAVE_SSL
75 
76 #if !defined(__IPERF_API_H)
77 typedef uint64_t iperf_size_t;
78 #endif // __IPERF_API_H
79 
80 struct iperf_interval_results
81 {
82     iperf_size_t bytes_transferred; /* bytes transfered in this interval */
83     struct iperf_time interval_start_time;
84     struct iperf_time interval_end_time;
85     float     interval_duration;
86 
87     /* for UDP */
88     int       interval_packet_count;
89     int       interval_outoforder_packets;
90     int       interval_cnt_error;
91     int       packet_count;
92     double    jitter;
93     int       outoforder_packets;
94     int       cnt_error;
95 
96     int omitted;
97 #if (defined(linux) || defined(__FreeBSD__) || defined(__NetBSD__)) && \
98 	defined(TCP_INFO)
99     struct tcp_info tcpInfo; /* getsockopt(TCP_INFO) for Linux, {Free,Net}BSD */
100 #else
101     /* Just placeholders, never accessed. */
102     char *tcpInfo;
103 #endif
104     int interval_retrans;
105     int interval_sacks;
106     int snd_cwnd;
107     int snd_wnd;
108     TAILQ_ENTRY(iperf_interval_results) irlistentries;
109     void     *custom_data;
110     int rtt;
111     int rttvar;
112     int pmtu;
113 };
114 
115 struct iperf_stream_result
116 {
117     iperf_size_t bytes_received;
118     iperf_size_t bytes_sent;
119     iperf_size_t bytes_received_this_interval;
120     iperf_size_t bytes_sent_this_interval;
121     iperf_size_t bytes_sent_omit;
122     int stream_prev_total_retrans;
123     int stream_retrans;
124     int stream_prev_total_sacks;
125     int stream_sacks;
126     int stream_max_rtt;
127     int stream_min_rtt;
128     int stream_sum_rtt;
129     int stream_count_rtt;
130     int stream_max_snd_cwnd;
131     int stream_max_snd_wnd;
132     struct iperf_time start_time;
133     struct iperf_time end_time;
134     struct iperf_time start_time_fixed;
135     double sender_time;
136     double receiver_time;
137     TAILQ_HEAD(irlisthead, iperf_interval_results) interval_results;
138     void     *data;
139 };
140 
141 #define COOKIE_SIZE 37		/* size of an ascii uuid */
142 struct iperf_settings
143 {
144     int       domain;               /* AF_INET or AF_INET6 */
145     int       socket_bufsize;       /* window size for TCP */
146     int       blksize;              /* size of read/writes (-l) */
147     iperf_size_t  rate;                 /* target data rate for application pacing*/
148     iperf_size_t  bitrate_limit;   /* server's maximum allowed total data rate for all streams*/
149     double        bitrate_limit_interval;  /* interval for avaraging total data rate */
150     int           bitrate_limit_stats_per_interval;     /* calculated number of stats periods for averaging total data rate */
151     uint64_t  fqrate;               /* target data rate for FQ pacing*/
152     int	      pacing_timer;	    /* pacing timer in microseconds */
153     int       burst;                /* packets per burst */
154     int       mss;                  /* for TCP MSS */
155     int       ttl;                  /* IP TTL option */
156     int       tos;                  /* type of service bit */
157     int       flowlabel;            /* IPv6 flow label */
158     iperf_size_t bytes;             /* number of bytes to send */
159     iperf_size_t blocks;            /* number of blocks (packets) to send */
160     char      unit_format;          /* -f */
161     int       num_ostreams;         /* SCTP initmsg settings */
162     int       dont_fragment;        /* Whether to set IP flag Do-Not_Fragment */
163 #if defined(HAVE_SSL)
164     char      *authtoken;           /* Authentication token */
165     char      *client_username;
166     char      *client_password;
167     EVP_PKEY  *client_rsa_pubkey;
168 #endif // HAVE_SSL
169     int	      connect_timeout;	    /* socket connection timeout, in ms */
170     int       idle_timeout;         /* server idle time timeout */
171     struct iperf_time rcv_timeout;  /* Timeout for receiving messages in active mode, in us */
172 };
173 
174 struct iperf_test;
175 
176 struct iperf_stream
177 {
178     struct iperf_test* test;
179 
180     /* configurable members */
181     int       local_port;
182     int       remote_port;
183     int       socket;
184     int       id;
185     int       sender;
186 	/* XXX: is settings just a pointer to the same struct in iperf_test? if not,
187 		should it be? */
188     struct iperf_settings *settings;	/* pointer to structure settings */
189 
190     /* non configurable members */
191     struct iperf_stream_result *result;	/* structure pointer to result */
192     Timer     *send_timer;
193     int       green_light;
194     int       buffer_fd;	/* data to send, file descriptor */
195     char      *buffer;		/* data to send, mmapped */
196     int       pending_size;     /* pending data to send */
197     int       diskfile_fd;	/* file to send, file descriptor */
198     int	      diskfile_left;	/* remaining file data on disk */
199 
200     /*
201      * for udp measurements - This can be a structure outside stream, and
202      * stream can have a pointer to this
203      */
204     int       packet_count;
205     int	      peer_packet_count;
206     int       omitted_packet_count;
207     double    jitter;
208     double    prev_transit;
209     int       outoforder_packets;
210     int       omitted_outoforder_packets;
211     int       cnt_error;
212     int       omitted_cnt_error;
213     uint64_t  target;
214 
215     struct sockaddr_storage local_addr;
216     struct sockaddr_storage remote_addr;
217 
218     int       (*rcv) (struct iperf_stream * stream);
219     int       (*snd) (struct iperf_stream * stream);
220 
221     /* chained send/receive routines for -F mode */
222     int       (*rcv2) (struct iperf_stream * stream);
223     int       (*snd2) (struct iperf_stream * stream);
224 
225 //    struct iperf_stream *next;
226     SLIST_ENTRY(iperf_stream) streams;
227 
228     void     *data;
229 };
230 
231 struct protocol {
232     int       id;
233     char      *name;
234     int       (*accept)(struct iperf_test *);
235     int       (*listen)(struct iperf_test *);
236     int       (*connect)(struct iperf_test *);
237     int       (*send)(struct iperf_stream *);
238     int       (*recv)(struct iperf_stream *);
239     int       (*init)(struct iperf_test *);
240     SLIST_ENTRY(protocol) protocols;
241 };
242 
243 struct iperf_textline {
244     char *line;
245     TAILQ_ENTRY(iperf_textline) textlineentries;
246 };
247 
248 struct xbind_entry {
249     char *name;
250     struct addrinfo *ai;
251     TAILQ_ENTRY(xbind_entry) link;
252 };
253 
254 enum iperf_mode {
255 	SENDER = 1,
256 	RECEIVER = 0,
257 	BIDIRECTIONAL = -1
258 };
259 
260 struct iperf_test
261 {
262     char      role;                             /* 'c' lient or 's' erver */
263     enum iperf_mode mode;
264     int       sender_has_retransmits;
265     int       other_side_has_retransmits;       /* used if mode == BIDIRECTIONAL */
266     struct protocol *protocol;
267     signed char state;
268     char     *server_hostname;                  /* -c option */
269     char     *tmp_template;
270     char     *bind_address;                     /* first -B option */
271     char     *bind_dev;                         /* bind to network device */
272     TAILQ_HEAD(xbind_addrhead, xbind_entry) xbind_addrs; /* all -X opts */
273     int       bind_port;                        /* --cport option */
274     int       server_port;
275     int       omit;                             /* duration of omit period (-O flag) */
276     int       duration;                         /* total duration of test (-t flag) */
277     char     *diskfile_name;			/* -F option */
278     int       affinity, server_affinity;	/* -A option */
279 #if defined(HAVE_CPUSET_SETAFFINITY)
280     cpuset_t cpumask;
281 #endif /* HAVE_CPUSET_SETAFFINITY */
282     char     *title;				/* -T option */
283     char     *extra_data;			/* --extra-data */
284     char     *congestion;			/* -C option */
285     char     *congestion_used;			/* what was actually used */
286     char     *remote_congestion_used;		/* what the other side used */
287     char     *pidfile;				/* -P option */
288 
289     char     *logfile;				/* --logfile option */
290     FILE     *outfile;
291 
292     int       ctrl_sck;
293     int       listener;
294     int       prot_listener;
295 
296     int	      ctrl_sck_mss;			/* MSS for the control channel */
297 
298 #if defined(HAVE_SSL)
299     char      *server_authorized_users;
300     EVP_PKEY  *server_rsa_private_key;
301     int       server_skew_threshold;
302 #endif // HAVE_SSL
303 
304     /* boolean variables for Options */
305     int       daemon;                           /* -D option */
306     int       one_off;                          /* -1 option */
307     int       no_delay;                         /* -N option */
308     int       reverse;                          /* -R option */
309     int       bidirectional;                    /* --bidirectional */
310     int	      verbose;                          /* -V option - verbose mode */
311     int	      json_output;                      /* -J option - JSON output */
312     int	      zerocopy;                         /* -Z option - use sendfile */
313     int       debug;				/* -d option - enable debug */
314     int	      get_server_output;		/* --get-server-output */
315     int	      udp_counters_64bit;		/* --use-64-bit-udp-counters */
316     int       forceflush; /* --forceflush - flushing output at every interval */
317     int	      multisend;
318     int	      repeating_payload;                /* --repeating-payload */
319     int       timestamps;			/* --timestamps */
320     char     *timestamp_format;
321 
322     char     *json_output_string; /* rendered JSON output if json_output is set */
323     /* Select related parameters */
324     int       max_fd;
325     fd_set    read_set;                         /* set of read sockets */
326     fd_set    write_set;                        /* set of write sockets */
327 
328     /* Interval related members */
329     int       omitting;
330     double    stats_interval;
331     double    reporter_interval;
332     void      (*stats_callback) (struct iperf_test *);
333     void      (*reporter_callback) (struct iperf_test *);
334     Timer     *omit_timer;
335     Timer     *timer;
336     int        done;
337     Timer     *stats_timer;
338     Timer     *reporter_timer;
339 
340     double cpu_util[3];                            /* cpu utilization of the test - total, user, system */
341     double remote_cpu_util[3];                     /* cpu utilization for the remote host/client - total, user, system */
342 
343     int       num_streams;                      /* total streams in the test (-P) */
344 
345     iperf_size_t bytes_sent;
346     iperf_size_t blocks_sent;
347 
348     iperf_size_t bytes_received;
349     iperf_size_t blocks_received;
350 
351     iperf_size_t bitrate_limit_stats_count;               /* Number of stats periods accumulated for server's total bitrate average */
352     iperf_size_t *bitrate_limit_intervals_traffic_bytes;  /* Pointer to a cyclic array that includes the last interval's bytes transferred */
353     iperf_size_t bitrate_limit_last_interval_index;       /* Index of the last interval traffic insrted into the cyclic array */
354     int          bitrate_limit_exceeded;                  /* Set by callback routine when average data rate exceeded the server's bitrate limit */
355 
356     int server_last_run_rc;                      /* Save last server run rc for next test */
357     uint server_forced_idle_restarts_count;      /* count number of forced server restarts to make sure it is not stack */
358     uint server_forced_no_msg_restarts_count;    /* count number of forced server restarts to make sure it is not stack */
359     uint server_test_number;                     /* count number of tests performed by a server */
360 
361     char      cookie[COOKIE_SIZE];
362 //    struct iperf_stream *streams;               /* pointer to list of struct stream */
363     SLIST_HEAD(slisthead, iperf_stream) streams;
364     struct iperf_settings *settings;
365 
366     SLIST_HEAD(plisthead, protocol) protocols;
367 
368     /* callback functions */
369     void      (*on_new_stream)(struct iperf_stream *);
370     void      (*on_test_start)(struct iperf_test *);
371     void      (*on_connect)(struct iperf_test *);
372     void      (*on_test_finish)(struct iperf_test *);
373 
374     /* cJSON handles for use when in -J mode */\
375     cJSON *json_top;
376     cJSON *json_start;
377     cJSON *json_connected;
378     cJSON *json_intervals;
379     cJSON *json_end;
380 
381     /* Server output (use on client side only) */
382     char *server_output_text;
383     cJSON *json_server_output;
384 
385     /* Server output (use on server side only) */
386     TAILQ_HEAD(iperf_textlisthead, iperf_textline) server_output_list;
387 
388 };
389 
390 /* default settings */
391 #define PORT 5201  /* default port to listen on (don't use the same port as iperf2) */
392 #define uS_TO_NS 1000
393 #define mS_TO_US 1000
394 #define SEC_TO_mS 1000
395 #define SEC_TO_US 1000000LL
396 #define UDP_RATE (1024 * 1024) /* 1 Mbps */
397 #define OMIT 0 /* seconds */
398 #define DURATION 10 /* seconds */
399 
400 #define SEC_TO_NS 1000000000LL	/* too big for enum/const on some platforms */
401 #define MAX_RESULT_STRING 4096
402 
403 #define UDP_BUFFER_EXTRA 1024
404 
405 /* constants for command line arg sanity checks */
406 #define MB (1024 * 1024)
407 #define MAX_TCP_BUFFER (512 * MB)
408 #define MAX_BLOCKSIZE MB
409 /* Minimum size UDP send is the size of two 32-bit ints followed by a 64-bit int */
410 #define MIN_UDP_BLOCKSIZE (4 + 4 + 8)
411 /* Maximum size UDP send is (64K - 1) - IP and UDP header sizes */
412 #define MAX_UDP_BLOCKSIZE (65535 - 8 - 20)
413 #define MIN_INTERVAL 0.1
414 #define MAX_INTERVAL 60.0
415 #define MAX_TIME 86400
416 #define MAX_BURST 1000
417 #define MAX_MSS (9 * 1024)
418 #define MAX_STREAMS 128
419 
420 #define TIMESTAMP_FORMAT "%c "
421 
422 extern int gerror; /* error value from getaddrinfo(3), for use in internal error handling */
423 
424 #endif /* !__IPERF_H */
425