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