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