xref: /iperf/src/iperf.h (revision a57bcc2d)
1 /*
2  * Copyright (c) 2009-2014, The Regents of the University of California,
3  * through Lawrence Berkeley National Laboratory (subject to receipt of any
4  * required approvals from the U.S. Dept. of Energy).  All rights reserved.
5  *
6  * This code is distributed under a BSD style license, see the LICENSE file
7  * for complete information.
8  */
9 
10 #ifndef __IPERF_H
11 #define __IPERF_H
12 
13 #include "iperf_config.h"
14 
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <stdint.h>
18 #include <sys/socket.h>
19 #include <netinet/tcp.h>
20 
21 #if defined(HAVE_CPUSET_SETAFFINITY)
22 #include <sys/param.h>
23 #include <sys/cpuset.h>
24 #endif /* HAVE_CPUSET_SETAFFINITY */
25 
26 #include "timer.h"
27 #include "queue.h"
28 #include "cjson.h"
29 
30 typedef uint64_t iperf_size_t;
31 
32 struct iperf_interval_results
33 {
34     iperf_size_t bytes_transferred; /* bytes transfered in this interval */
35     struct timeval interval_start_time;
36     struct timeval interval_end_time;
37     float     interval_duration;
38 
39     /* for UDP */
40     int       interval_packet_count;
41     int       interval_outoforder_packets;
42     int       interval_cnt_error;
43     int       packet_count;
44     double    jitter;
45     int       outoforder_packets;
46     int       cnt_error;
47 
48     int omitted;
49 #if defined(linux) || defined(__FreeBSD__)
50     struct tcp_info tcpInfo;	/* getsockopt(TCP_INFO) for Linux and FreeBSD */
51 #else
52     /* Just placeholders, never accessed. */
53     char *tcpInfo;
54 #endif
55     int interval_retrans;
56     int interval_sacks;
57     int snd_cwnd;
58     TAILQ_ENTRY(iperf_interval_results) irlistentries;
59     void     *custom_data;
60 };
61 
62 struct iperf_stream_result
63 {
64     iperf_size_t bytes_received;
65     iperf_size_t bytes_sent;
66     iperf_size_t bytes_received_this_interval;
67     iperf_size_t bytes_sent_this_interval;
68     int stream_prev_total_retrans;
69     int stream_retrans;
70     int stream_prev_total_sacks;
71     int stream_sacks;
72     struct timeval start_time;
73     struct timeval end_time;
74     TAILQ_HEAD(irlisthead, iperf_interval_results) interval_results;
75     void     *data;
76 };
77 
78 #define COOKIE_SIZE 37		/* size of an ascii uuid */
79 struct iperf_settings
80 {
81     int       domain;               /* AF_INET or AF_INET6 */
82     int       socket_bufsize;       /* window size for TCP */
83     int       blksize;              /* size of read/writes (-l) */
84     uint64_t  rate;                 /* target data rate */
85     int       burst;                /* packets per burst */
86     int       mss;                  /* for TCP MSS */
87     int       ttl;                  /* IP TTL option */
88     int       tos;                  /* type of service bit */
89     int       flowlabel;            /* IPv6 flow label */
90     iperf_size_t bytes;             /* number of bytes to send */
91     int       blocks;               /* number of blocks (packets) to send */
92     char      unit_format;          /* -f */
93 };
94 
95 struct iperf_test;
96 
97 struct iperf_stream
98 {
99     struct iperf_test* test;
100 
101     /* configurable members */
102     int       local_port;
103     int       remote_port;
104     int       socket;
105     int       id;
106 	/* XXX: is settings just a pointer to the same struct in iperf_test? if not,
107 		should it be? */
108     struct iperf_settings *settings;	/* pointer to structure settings */
109 
110     /* non configurable members */
111     struct iperf_stream_result *result;	/* structure pointer to result */
112     Timer     *send_timer;
113     int       green_light;
114     int       buffer_fd;	/* data to send, file descriptor */
115     char      *buffer;		/* data to send, mmapped */
116     int       diskfile_fd;	/* file to send, file descriptor */
117 
118     /*
119      * for udp measurements - This can be a structure outside stream, and
120      * stream can have a pointer to this
121      */
122     int       packet_count;
123     int       omitted_packet_count;
124     double    jitter;
125     double    prev_transit;
126     int       outoforder_packets;
127     int       cnt_error;
128     uint64_t  target;
129 
130     struct sockaddr_storage local_addr;
131     struct sockaddr_storage remote_addr;
132 
133     int       (*rcv) (struct iperf_stream * stream);
134     int       (*snd) (struct iperf_stream * stream);
135 
136     /* chained send/receive routines for -F mode */
137     int       (*rcv2) (struct iperf_stream * stream);
138     int       (*snd2) (struct iperf_stream * stream);
139 
140 //    struct iperf_stream *next;
141     SLIST_ENTRY(iperf_stream) streams;
142 
143     void     *data;
144 };
145 
146 struct protocol {
147     int       id;
148     char      *name;
149     int       (*accept)(struct iperf_test *);
150     int       (*listen)(struct iperf_test *);
151     int       (*connect)(struct iperf_test *);
152     int       (*send)(struct iperf_stream *);
153     int       (*recv)(struct iperf_stream *);
154     int       (*init)(struct iperf_test *);
155     SLIST_ENTRY(protocol) protocols;
156 };
157 
158 struct iperf_test
159 {
160     char      role;                             /* 'c' lient or 's' erver */
161     int       sender;                           /* client & !reverse or server & reverse */
162     int       sender_has_retransmits;
163     struct protocol *protocol;
164     signed char state;
165     char     *server_hostname;                  /* -c option */
166     char     *bind_address;                     /* -B option */
167     int       server_port;
168     int       omit;                             /* duration of omit period (-O flag) */
169     int       duration;                         /* total duration of test (-t flag) */
170     char     *diskfile_name;			/* -F option */
171     int       affinity, server_affinity;	/* -A option */
172 #if defined(HAVE_CPUSET_SETAFFINITY)
173     cpuset_t cpumask;
174 #endif /* HAVE_CPUSET_SETAFFINITY */
175     char     *title;				/* -T option */
176     char     *congestion;			/* -C option */
177     char     *pidfile;				/* -P option */
178 
179     char     *logfile;				/* --logfile option */
180     FILE     *outfile;
181 
182     int       ctrl_sck;
183     int       listener;
184     int       prot_listener;
185 
186     /* boolean variables for Options */
187     int       daemon;                           /* -D option */
188     int       no_delay;                         /* -N option */
189     int       reverse;                          /* -R option */
190     int	      verbose;                          /* -V option - verbose mode */
191     int	      json_output;                      /* -J option - JSON output */
192     int	      zerocopy;                         /* -Z option - use sendfile */
193     int       debug;				/* -d option - enable debug */
194 
195     int	      multisend;
196     int	      may_use_sigalrm;
197 
198     char     *json_output_string; /* rendered JSON output if json_output is set */
199     /* Select related parameters */
200     int       max_fd;
201     fd_set    read_set;                         /* set of read sockets */
202     fd_set    write_set;                        /* set of write sockets */
203 
204     /* Interval related members */
205     int       omitting;
206     double    stats_interval;
207     double    reporter_interval;
208     void      (*stats_callback) (struct iperf_test *);
209     void      (*reporter_callback) (struct iperf_test *);
210     Timer     *omit_timer;
211     Timer     *timer;
212     int        done;
213     Timer     *stats_timer;
214     Timer     *reporter_timer;
215 
216     double cpu_util[3];                            /* cpu utilization of the test - total, user, system */
217     double remote_cpu_util[3];                     /* cpu utilization for the remote host/client - total, user, system */
218 
219     int       num_streams;                      /* total streams in the test (-P) */
220 
221     iperf_size_t bytes_sent;
222     int       blocks_sent;
223     char      cookie[COOKIE_SIZE];
224 //    struct iperf_stream *streams;               /* pointer to list of struct stream */
225     SLIST_HEAD(slisthead, iperf_stream) streams;
226     struct iperf_settings *settings;
227 
228     SLIST_HEAD(plisthead, protocol) protocols;
229 
230     /* callback functions */
231     void      (*on_new_stream)(struct iperf_stream *);
232     void      (*on_test_start)(struct iperf_test *);
233     void      (*on_connect)(struct iperf_test *);
234     void      (*on_test_finish)(struct iperf_test *);
235 
236     /* cJSON handles for use when in -J mode */\
237     cJSON *json_top;
238     cJSON *json_start;
239     cJSON *json_intervals;
240     cJSON *json_end;
241 };
242 
243 /* default settings */
244 #define PORT 5201  /* default port to listen on (don't use the same port as iperf2) */
245 #define uS_TO_NS 1000
246 #define SEC_TO_US 1000000LL
247 #define UDP_RATE (1024 * 1024) /* 1 Mbps */
248 #define OMIT 0 /* seconds */
249 #define DURATION 10 /* seconds */
250 
251 #define SEC_TO_NS 1000000000LL	/* too big for enum/const on some platforms */
252 #define MAX_RESULT_STRING 4096
253 
254 /* constants for command line arg sanity checks */
255 #define MB (1024 * 1024)
256 #define MAX_TCP_BUFFER (512 * MB)
257 #define MAX_BLOCKSIZE MB
258 #define MIN_INTERVAL 0.1
259 #define MAX_INTERVAL 60.0
260 #define MAX_TIME 3600
261 #define MAX_BURST 1000
262 #define MAX_MSS (9 * 1024)
263 #define MAX_STREAMS 128
264 
265 #endif /* !__IPERF_H */
266