xref: /iperf/src/iperf_api.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_API_H
11 #define        __IPERF_API_H
12 
13 #include <setjmp.h>
14 
15 struct iperf_test;
16 struct iperf_stream_result;
17 struct iperf_interval_results;
18 struct iperf_stream;
19 
20 /* default settings */
21 #define Ptcp SOCK_STREAM
22 #define Pudp SOCK_DGRAM
23 #define Psctp 12
24 #define DEFAULT_UDP_BLKSIZE 8192
25 #define DEFAULT_TCP_BLKSIZE (128 * 1024)  /* default read/write block size */
26 #define DEFAULT_SCTP_BLKSIZE (64 * 1024)
27 
28 /* short option equivalents, used to support options that only have long form */
29 #define OPT_SCTP 1
30 #define OPT_LOGFILE 2
31 
32 /* states */
33 #define TEST_START 1
34 #define TEST_RUNNING 2
35 #define RESULT_REQUEST 3
36 #define TEST_END 4
37 #define STREAM_BEGIN 5
38 #define STREAM_RUNNING 6
39 #define STREAM_END 7
40 #define ALL_STREAMS_END 8
41 #define PARAM_EXCHANGE 9
42 #define CREATE_STREAMS 10
43 #define SERVER_TERMINATE 11
44 #define CLIENT_TERMINATE 12
45 #define EXCHANGE_RESULTS 13
46 #define DISPLAY_RESULTS 14
47 #define IPERF_START 15
48 #define IPERF_DONE 16
49 #define ACCESS_DENIED (-1)
50 #define SERVER_ERROR (-2)
51 
52 /* Getter routines for some fields inside iperf_test. */
53 int	iperf_get_verbose( struct iperf_test* ipt );
54 int	iperf_get_control_socket( struct iperf_test* ipt );
55 int	iperf_get_test_omit( struct iperf_test* ipt );
56 int	iperf_get_test_duration( struct iperf_test* ipt );
57 char	iperf_get_test_role( struct iperf_test* ipt );
58 int	iperf_get_test_reverse( struct iperf_test* ipt );
59 int	iperf_get_test_blksize( struct iperf_test* ipt );
60 FILE*	iperf_get_test_outfile( struct iperf_test* ipt );
61 uint64_t iperf_get_test_rate( struct iperf_test* ipt );
62 int     iperf_get_test_burst( struct iperf_test* ipt );
63 int	iperf_get_test_socket_bufsize( struct iperf_test* ipt );
64 double	iperf_get_test_reporter_interval( struct iperf_test* ipt );
65 double	iperf_get_test_stats_interval( struct iperf_test* ipt );
66 int	iperf_get_test_num_streams( struct iperf_test* ipt );
67 int	iperf_get_test_server_port( struct iperf_test* ipt );
68 char*	iperf_get_test_server_hostname( struct iperf_test* ipt );
69 int	iperf_get_test_protocol_id( struct iperf_test* ipt );
70 int	iperf_get_test_json_output( struct iperf_test* ipt );
71 char*	iperf_get_test_json_output_string ( struct iperf_test* ipt );
72 int	iperf_get_test_zerocopy( struct iperf_test* ipt );
73 int	iperf_get_test_may_use_sigalrm( struct iperf_test* ipt );
74 
75 /* Setter routines for some fields inside iperf_test. */
76 void	iperf_set_verbose( struct iperf_test* ipt, int verbose );
77 void	iperf_set_control_socket( struct iperf_test* ipt, int ctrl_sck );
78 void	iperf_set_test_omit( struct iperf_test* ipt, int omit );
79 void	iperf_set_test_duration( struct iperf_test* ipt, int duration );
80 void	iperf_set_test_reporter_interval( struct iperf_test* ipt, double reporter_interval );
81 void	iperf_set_test_stats_interval( struct iperf_test* ipt, double stats_interval );
82 void	iperf_set_test_state( struct iperf_test* ipt, signed char state );
83 void	iperf_set_test_blksize( struct iperf_test* ipt, int blksize );
84 void	iperf_set_test_rate( struct iperf_test* ipt, uint64_t rate );
85 void	iperf_set_test_burst( struct iperf_test* ipt, int burst );
86 void	iperf_set_test_server_port( struct iperf_test* ipt, int server_port );
87 void	iperf_set_test_socket_bufsize( struct iperf_test* ipt, int socket_bufsize );
88 void	iperf_set_test_num_streams( struct iperf_test* ipt, int num_streams );
89 void	iperf_set_test_role( struct iperf_test* ipt, char role );
90 void	iperf_set_test_server_hostname( struct iperf_test* ipt, char* server_hostname );
91 void	iperf_set_test_reverse( struct iperf_test* ipt, int reverse );
92 void	iperf_set_test_json_output( struct iperf_test* ipt, int json_output );
93 int	iperf_has_zerocopy( void );
94 void	iperf_set_test_zerocopy( struct iperf_test* ipt, int zerocopy );
95 void	iperf_set_test_may_use_sigalrm( struct iperf_test* ipt, int may_use_sigalrm );
96 
97 /**
98  * exchange_parameters - handles the param_Exchange part for client
99  *
100  */
101 int      iperf_exchange_parameters(struct iperf_test * test);
102 
103 /**
104  * add_to_interval_list -- adds new interval to the interval_list
105  *
106  */
107 void      add_to_interval_list(struct iperf_stream_result * rp, struct iperf_interval_results *temp);
108 
109 /**
110  * connect_msg -- displays connection message
111  * denoting senfer/receiver details
112  *
113  */
114 void      connect_msg(struct iperf_stream * sp);
115 
116 /**
117  * iperf_stats_callback -- handles the statistic gathering
118  *
119  */
120 void     iperf_stats_callback(struct iperf_test * test);
121 
122 /**
123  * iperf_reporter_callback -- handles the report printing
124  *
125  */
126 void     iperf_reporter_callback(struct iperf_test * test);
127 
128 /**
129  * iperf_new_test -- return a new iperf_test with default values
130  *
131  * returns NULL on failure
132  *
133  */
134 struct iperf_test *iperf_new_test();
135 
136 int      iperf_defaults(struct iperf_test * testp);
137 
138 /**
139  * iperf_free_test -- free resources used by test, calls iperf_free_stream to
140  * free streams
141  *
142  */
143 void      iperf_free_test(struct iperf_test * testp);
144 
145 /**
146  * iperf_new_stream -- return a net iperf_stream with default values
147  *
148  * returns NULL on failure
149  *
150  */
151 struct iperf_stream *iperf_new_stream(struct iperf_test *, int);
152 
153 /**
154  * iperf_add_stream -- add a stream to a test
155  *
156  */
157 void      iperf_add_stream(struct iperf_test * test, struct iperf_stream * stream);
158 
159 /**
160  * iperf_init_stream -- init resources associated with test
161  *
162  */
163 int       iperf_init_stream(struct iperf_stream *, struct iperf_test *);
164 
165 /**
166  * iperf_free_stream -- free resources associated with test
167  *
168  */
169 void      iperf_free_stream(struct iperf_stream * sp);
170 
171 int has_tcpinfo(void);
172 int has_tcpinfo_retransmits(void);
173 void save_tcpinfo(struct iperf_stream *sp, struct iperf_interval_results *irp);
174 long get_total_retransmits(struct iperf_interval_results *irp);
175 long get_snd_cwnd(struct iperf_interval_results *irp);
176 void print_tcpinfo(struct iperf_test *test);
177 void build_tcpinfo_message(struct iperf_interval_results *r, char *message);
178 
179 int iperf_set_send_state(struct iperf_test *test, signed char state);
180 void iperf_check_throttle(struct iperf_stream *sp, struct timeval *nowP);
181 int iperf_send(struct iperf_test *, fd_set *) /* __attribute__((hot)) */;
182 int iperf_recv(struct iperf_test *, fd_set *);
183 void iperf_catch_sigend(void (*handler)(int));
184 void iperf_got_sigend(struct iperf_test *test) __attribute__ ((noreturn));
185 void usage();
186 void usage_long();
187 void warning(char *);
188 int iperf_exchange_results(struct iperf_test *);
189 int iperf_init_test(struct iperf_test *);
190 int iperf_create_send_timers(struct iperf_test *);
191 int iperf_parse_arguments(struct iperf_test *, int, char **);
192 void iperf_reset_test(struct iperf_test *);
193 void iperf_reset_stats(struct iperf_test * test);
194 
195 struct protocol *get_protocol(struct iperf_test *, int);
196 int set_protocol(struct iperf_test *, int);
197 
198 void iperf_on_new_stream(struct iperf_stream *);
199 void iperf_on_test_start(struct iperf_test *);
200 void iperf_on_connect(struct iperf_test *);
201 void iperf_on_test_finish(struct iperf_test *);
202 
203 extern jmp_buf env;
204 
205 /* Client routines. */
206 int iperf_run_client(struct iperf_test *);
207 int iperf_connect(struct iperf_test *);
208 int iperf_create_streams(struct iperf_test *);
209 int iperf_handle_message_client(struct iperf_test *);
210 int iperf_client_end(struct iperf_test *);
211 
212 /* Server routines. */
213 int iperf_run_server(struct iperf_test *);
214 int iperf_server_listen(struct iperf_test *);
215 int iperf_accept(struct iperf_test *);
216 int iperf_handle_message_server(struct iperf_test *);
217 void iperf_test_reset(struct iperf_test *);
218 int iperf_create_pidfile(struct iperf_test *);
219 int iperf_delete_pidfile(struct iperf_test *);
220 
221 /* JSON output routines. */
222 int iperf_json_start(struct iperf_test *);
223 int iperf_json_finish(struct iperf_test *);
224 
225 /* CPU affinity routines */
226 int iperf_setaffinity(struct iperf_test *, int affinity);
227 int iperf_clearaffinity(struct iperf_test *);
228 
229 /* Custom printf routine. */
230 int iprintf(struct iperf_test *test, const char *format, ...) __attribute__ ((format(printf,2,3)));
231 int iflush(struct iperf_test *test);
232 
233 /* Error routines. */
234 void iperf_err(struct iperf_test *test, const char *format, ...) __attribute__ ((format(printf,2,3)));
235 void iperf_errexit(struct iperf_test *test, const char *format, ...) __attribute__ ((format(printf,2,3),noreturn));
236 char *iperf_strerror(int);
237 extern int i_errno;
238 enum {
239     IENONE = 0,             // No error
240     /* Parameter errors */
241     IESERVCLIENT = 1,       // Iperf cannot be both server and client
242     IENOROLE = 2,           // Iperf must either be a client (-c) or server (-s)
243     IESERVERONLY = 3,       // This option is server only
244     IECLIENTONLY = 4,       // This option is client only
245     IEDURATION = 5,         // test duration too long. Maximum value = %dMAX_TIME
246     IENUMSTREAMS = 6,       // Number of parallel streams too large. Maximum value = %dMAX_STREAMS
247     IEBLOCKSIZE = 7,        // Block size too large. Maximum value = %dMAX_BLOCKSIZE
248     IEBUFSIZE = 8,          // Socket buffer size too large. Maximum value = %dMAX_TCP_BUFFER
249     IEINTERVAL = 9,         // Invalid report interval (min = %gMIN_INTERVAL, max = %gMAX_INTERVAL seconds)
250     IEMSS = 10,             // MSS too large. Maximum value = %dMAX_MSS
251     IENOSENDFILE = 11,      // This OS does not support sendfile
252     IEOMIT = 12,            // Bogus value for --omit
253     IEUNIMP = 13,           // Not implemented yet
254     IEFILE = 14,            // -F file couldn't be opened
255     IEBURST = 15,           // Invalid burst count. Maximum value = %dMAX_BURST
256     IEENDCONDITIONS = 16,   // Only one test end condition (-t, -n, -k) may be specified
257     IELOGFILE = 17,	    // Can't open log file
258     IENOSCTP = 18,	    // No SCTP support available
259     /* Test errors */
260     IENEWTEST = 100,        // Unable to create a new test (check perror)
261     IEINITTEST = 101,       // Test initialization failed (check perror)
262     IELISTEN = 102,         // Unable to listen for connections (check perror)
263     IECONNECT = 103,        // Unable to connect to server (check herror/perror) [from netdial]
264     IEACCEPT = 104,         // Unable to accept connection from client (check herror/perror)
265     IESENDCOOKIE = 105,     // Unable to send cookie to server (check perror)
266     IERECVCOOKIE = 106,     // Unable to receive cookie from client (check perror)
267     IECTRLWRITE = 107,      // Unable to write to the control socket (check perror)
268     IECTRLREAD = 108,       // Unable to read from the control socket (check perror)
269     IECTRLCLOSE = 109,      // Control socket has closed unexpectedly
270     IEMESSAGE = 110,        // Received an unknown message
271     IESENDMESSAGE = 111,    // Unable to send control message to client/server (check perror)
272     IERECVMESSAGE = 112,    // Unable to receive control message from client/server (check perror)
273     IESENDPARAMS = 113,     // Unable to send parameters to server (check perror)
274     IERECVPARAMS = 114,     // Unable to receive parameters from client (check perror)
275     IEPACKAGERESULTS = 115, // Unable to package results (check perror)
276     IESENDRESULTS = 116,    // Unable to send results to client/server (check perror)
277     IERECVRESULTS = 117,    // Unable to receive results from client/server (check perror)
278     IESELECT = 118,         // Select failed (check perror)
279     IECLIENTTERM = 119,     // The client has terminated
280     IESERVERTERM = 120,     // The server has terminated
281     IEACCESSDENIED = 121,   // The server is busy running a test. Try again later.
282     IESETNODELAY = 122,     // Unable to set TCP NODELAY (check perror)
283     IESETMSS = 123,         // Unable to set TCP MSS (check perror)
284     IESETBUF = 124,         // Unable to set socket buffer size (check perror)
285     IESETTOS = 125,         // Unable to set IP TOS (check perror)
286     IESETCOS = 126,         // Unable to set IPv6 traffic class (check perror)
287     IESETFLOW = 127,        // Unable to set IPv6 flow label
288     IEREUSEADDR = 128,      // Unable to set reuse address on socket (check perror)
289     IENONBLOCKING = 129,    // Unable to set socket to non-blocking (check perror)
290     IESETWINDOWSIZE = 130,  // Unable to set socket window size (check perror)
291     IEPROTOCOL = 131,       // Protocol does not exist
292     IEAFFINITY = 132,       // Unable to set CPU affinity (check perror)
293     IEDAEMON = 133,	    // Unable to become a daemon process
294     IESETCONGESTION = 134,  // Unable to set TCP_CONGESTION
295     IEPIDFILE = 135,	    // Unable to write PID file
296     IEV6ONLY = 136,  	    // Unable to set/unset IPV6_V6ONLY (check perror)
297     IESETSCTPDISABLEFRAG = 137, // Unable to set SCTP Fragmentation (check perror)
298     /* Stream errors */
299     IECREATESTREAM = 200,   // Unable to create a new stream (check herror/perror)
300     IEINITSTREAM = 201,     // Unable to initialize stream (check herror/perror)
301     IESTREAMLISTEN = 202,   // Unable to start stream listener (check perror)
302     IESTREAMCONNECT = 203,  // Unable to connect stream (check herror/perror)
303     IESTREAMACCEPT = 204,   // Unable to accepte stream connection (check perror)
304     IESTREAMWRITE = 205,    // Unable to write to stream socket (check perror)
305     IESTREAMREAD = 206,     // Unable to read from stream (check perror)
306     IESTREAMCLOSE = 207,    // Stream has closed unexpectedly
307     IESTREAMID = 208,       // Stream has invalid ID
308     /* Timer errors */
309     IENEWTIMER = 300,       // Unable to create new timer (check perror)
310     IEUPDATETIMER = 301,    // Unable to update timer (check perror)
311 };
312 
313 #endif /* !__IPERF_API_H */
314