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