1 /* 2 * iperf, Copyright (c) 2014-2019, 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_API_H 28 #define __IPERF_API_H 29 30 #include <sys/socket.h> 31 #include <sys/time.h> 32 #include <setjmp.h> 33 #include <stdio.h> 34 #ifdef HAVE_STDINT_H 35 #include <stdint.h> 36 #endif 37 #ifdef __cplusplus 38 extern "C" { /* open extern "C" */ 39 #endif 40 41 42 struct iperf_test; 43 struct iperf_stream_result; 44 struct iperf_interval_results; 45 struct iperf_stream; 46 struct iperf_time; 47 48 /* default settings */ 49 #define Ptcp SOCK_STREAM 50 #define Pudp SOCK_DGRAM 51 #define Psctp 12 52 #define DEFAULT_UDP_BLKSIZE 1460 /* default is dynamically set, else this */ 53 #define DEFAULT_TCP_BLKSIZE (128 * 1024) /* default read/write block size */ 54 #define DEFAULT_SCTP_BLKSIZE (64 * 1024) 55 56 /* short option equivalents, used to support options that only have long form */ 57 #define OPT_SCTP 1 58 #define OPT_LOGFILE 2 59 #define OPT_GET_SERVER_OUTPUT 3 60 #define OPT_UDP_COUNTERS_64BIT 4 61 #define OPT_CLIENT_PORT 5 62 #define OPT_NUMSTREAMS 6 63 #define OPT_FORCEFLUSH 7 64 #define OPT_NO_FQ_SOCKET_PACING 9 /* UNUSED */ 65 #define OPT_FQ_RATE 10 66 #define OPT_DSCP 11 67 #define OPT_CLIENT_USERNAME 12 68 #define OPT_CLIENT_RSA_PUBLIC_KEY 13 69 #define OPT_SERVER_RSA_PRIVATE_KEY 14 70 #define OPT_SERVER_AUTHORIZED_USERS 15 71 #define OPT_PACING_TIMER 16 72 #define OPT_CONNECT_TIMEOUT 17 73 #define OPT_REPEATING_PAYLOAD 18 74 #define OPT_EXTRA_DATA 19 75 #define OPT_BIDIRECTIONAL 20 76 77 /* states */ 78 #define TEST_START 1 79 #define TEST_RUNNING 2 80 #define RESULT_REQUEST 3 /* not used */ 81 #define TEST_END 4 82 #define STREAM_BEGIN 5 /* not used */ 83 #define STREAM_RUNNING 6 /* not used */ 84 #define STREAM_END 7 /* not used */ 85 #define ALL_STREAMS_END 8 /* not used */ 86 #define PARAM_EXCHANGE 9 87 #define CREATE_STREAMS 10 88 #define SERVER_TERMINATE 11 89 #define CLIENT_TERMINATE 12 90 #define EXCHANGE_RESULTS 13 91 #define DISPLAY_RESULTS 14 92 #define IPERF_START 15 93 #define IPERF_DONE 16 94 #define ACCESS_DENIED (-1) 95 #define SERVER_ERROR (-2) 96 97 /* Getter routines for some fields inside iperf_test. */ 98 int iperf_get_verbose( struct iperf_test* ipt ); 99 int iperf_get_control_socket( struct iperf_test* ipt ); 100 int iperf_get_test_omit( struct iperf_test* ipt ); 101 int iperf_get_test_duration( struct iperf_test* ipt ); 102 char iperf_get_test_role( struct iperf_test* ipt ); 103 int iperf_get_test_reverse( struct iperf_test* ipt ); 104 int iperf_get_test_blksize( struct iperf_test* ipt ); 105 FILE* iperf_get_test_outfile( struct iperf_test* ipt ); 106 uint64_t iperf_get_test_rate( struct iperf_test* ipt ); 107 int iperf_get_test_pacing_timer( struct iperf_test* ipt ); 108 uint64_t iperf_get_test_bytes( struct iperf_test* ipt ); 109 uint64_t iperf_get_test_blocks( struct iperf_test* ipt ); 110 int iperf_get_test_burst( struct iperf_test* ipt ); 111 int iperf_get_test_socket_bufsize( struct iperf_test* ipt ); 112 double iperf_get_test_reporter_interval( struct iperf_test* ipt ); 113 double iperf_get_test_stats_interval( struct iperf_test* ipt ); 114 int iperf_get_test_num_streams( struct iperf_test* ipt ); 115 int iperf_get_test_repeating_payload( struct iperf_test* ipt ); 116 int iperf_get_test_server_port( struct iperf_test* ipt ); 117 char* iperf_get_test_server_hostname( struct iperf_test* ipt ); 118 char* iperf_get_test_template( struct iperf_test* ipt ); 119 int iperf_get_test_protocol_id( struct iperf_test* ipt ); 120 int iperf_get_test_json_output( struct iperf_test* ipt ); 121 char* iperf_get_test_json_output_string ( struct iperf_test* ipt ); 122 int iperf_get_test_zerocopy( struct iperf_test* ipt ); 123 int iperf_get_test_get_server_output( struct iperf_test* ipt ); 124 char* iperf_get_test_bind_address ( struct iperf_test* ipt ); 125 int iperf_get_test_udp_counters_64bit( struct iperf_test* ipt ); 126 int iperf_get_test_one_off( struct iperf_test* ipt ); 127 int iperf_get_test_tos( struct iperf_test* ipt ); 128 char* iperf_get_extra_data( struct iperf_test* ipt ); 129 char* iperf_get_iperf_version(void); 130 int iperf_get_test_no_delay( struct iperf_test* ipt ); 131 132 /* Setter routines for some fields inside iperf_test. */ 133 void iperf_set_verbose( struct iperf_test* ipt, int verbose ); 134 void iperf_set_control_socket( struct iperf_test* ipt, int ctrl_sck ); 135 void iperf_set_test_omit( struct iperf_test* ipt, int omit ); 136 void iperf_set_test_duration( struct iperf_test* ipt, int duration ); 137 void iperf_set_test_reporter_interval( struct iperf_test* ipt, double reporter_interval ); 138 void iperf_set_test_stats_interval( struct iperf_test* ipt, double stats_interval ); 139 void iperf_set_test_state( struct iperf_test* ipt, signed char state ); 140 void iperf_set_test_blksize( struct iperf_test* ipt, int blksize ); 141 void iperf_set_test_logfile( struct iperf_test* ipt, char *logfile ); 142 void iperf_set_test_rate( struct iperf_test* ipt, uint64_t rate ); 143 void iperf_set_test_pacing_timer( struct iperf_test* ipt, int pacing_timer ); 144 void iperf_set_test_bytes( struct iperf_test* ipt, uint64_t bytes ); 145 void iperf_set_test_blocks( struct iperf_test* ipt, uint64_t blocks ); 146 void iperf_set_test_burst( struct iperf_test* ipt, int burst ); 147 void iperf_set_test_server_port( struct iperf_test* ipt, int server_port ); 148 void iperf_set_test_socket_bufsize( struct iperf_test* ipt, int socket_bufsize ); 149 void iperf_set_test_num_streams( struct iperf_test* ipt, int num_streams ); 150 void iperf_set_test_repeating_payload( struct iperf_test* ipt, int repeating_payload ); 151 void iperf_set_test_role( struct iperf_test* ipt, char role ); 152 void iperf_set_test_server_hostname( struct iperf_test* ipt, char* server_hostname ); 153 void iperf_set_test_template( struct iperf_test *ipt, char *tmp_template ); 154 void iperf_set_test_reverse( struct iperf_test* ipt, int reverse ); 155 void iperf_set_test_json_output( struct iperf_test* ipt, int json_output ); 156 int iperf_has_zerocopy( void ); 157 void iperf_set_test_zerocopy( struct iperf_test* ipt, int zerocopy ); 158 void iperf_set_test_get_server_output( struct iperf_test* ipt, int get_server_output ); 159 void iperf_set_test_bind_address( struct iperf_test* ipt, char *bind_address ); 160 void iperf_set_test_udp_counters_64bit( struct iperf_test* ipt, int udp_counters_64bit ); 161 void iperf_set_test_one_off( struct iperf_test* ipt, int one_off ); 162 void iperf_set_test_tos( struct iperf_test* ipt, int tos ); 163 void iperf_set_extra_data( struct iperf_test* ipt, char *dat); 164 void iperf_set_test_bidirectional( struct iperf_test* ipt, int bidirectional); 165 void iperf_set_test_no_delay( struct iperf_test* ipt, int no_delay); 166 167 #if defined(HAVE_SSL) 168 void iperf_set_test_client_username(struct iperf_test *ipt, char *client_username); 169 void iperf_set_test_client_password(struct iperf_test *ipt, char *client_password); 170 void iperf_set_test_client_rsa_pubkey(struct iperf_test *ipt, char *client_rsa_pubkey_base64); 171 void iperf_set_test_server_authorized_users(struct iperf_test *ipt, char *server_authorized_users); 172 void iperf_set_test_server_rsa_privkey(struct iperf_test *ipt, char *server_rsa_privkey_base64); 173 #endif // HAVE_SSL 174 175 /** 176 * exchange_parameters - handles the param_Exchange part for client 177 * 178 */ 179 int iperf_exchange_parameters(struct iperf_test * test); 180 181 /** 182 * add_to_interval_list -- adds new interval to the interval_list 183 * 184 */ 185 void add_to_interval_list(struct iperf_stream_result * rp, struct iperf_interval_results *temp); 186 187 /** 188 * connect_msg -- displays connection message 189 * denoting senfer/receiver details 190 * 191 */ 192 void connect_msg(struct iperf_stream * sp); 193 194 /** 195 * iperf_stats_callback -- handles the statistic gathering 196 * 197 */ 198 void iperf_stats_callback(struct iperf_test * test); 199 200 /** 201 * iperf_reporter_callback -- handles the report printing 202 * 203 */ 204 void iperf_reporter_callback(struct iperf_test * test); 205 206 /** 207 * iperf_new_test -- return a new iperf_test with default values 208 * 209 * returns NULL on failure 210 * 211 */ 212 struct iperf_test *iperf_new_test(void); 213 214 int iperf_defaults(struct iperf_test * testp); 215 216 /** 217 * iperf_free_test -- free resources used by test, calls iperf_free_stream to 218 * free streams 219 * 220 */ 221 void iperf_free_test(struct iperf_test * testp); 222 223 /** 224 * iperf_new_stream -- return a net iperf_stream with default values 225 * 226 * returns NULL on failure 227 * 228 */ 229 struct iperf_stream *iperf_new_stream(struct iperf_test *, int, int); 230 231 /** 232 * iperf_add_stream -- add a stream to a test 233 * 234 */ 235 void iperf_add_stream(struct iperf_test * test, struct iperf_stream * stream); 236 237 /** 238 * iperf_init_stream -- init resources associated with test 239 * 240 */ 241 int iperf_init_stream(struct iperf_stream *, struct iperf_test *); 242 243 /** 244 * iperf_free_stream -- free resources associated with test 245 * 246 */ 247 void iperf_free_stream(struct iperf_stream * sp); 248 249 int has_tcpinfo(void); 250 int has_tcpinfo_retransmits(void); 251 void save_tcpinfo(struct iperf_stream *sp, struct iperf_interval_results *irp); 252 long get_total_retransmits(struct iperf_interval_results *irp); 253 long get_snd_cwnd(struct iperf_interval_results *irp); 254 long get_rtt(struct iperf_interval_results *irp); 255 long get_rttvar(struct iperf_interval_results *irp); 256 long get_pmtu(struct iperf_interval_results *irp); 257 void print_tcpinfo(struct iperf_test *test); 258 void build_tcpinfo_message(struct iperf_interval_results *r, char *message); 259 260 int iperf_set_send_state(struct iperf_test *test, signed char state); 261 void iperf_check_throttle(struct iperf_stream *sp, struct iperf_time *nowP); 262 int iperf_send(struct iperf_test *, fd_set *) /* __attribute__((hot)) */; 263 int iperf_recv(struct iperf_test *, fd_set *); 264 void iperf_catch_sigend(void (*handler)(int)); 265 void iperf_got_sigend(struct iperf_test *test) __attribute__ ((noreturn)); 266 void usage(void); 267 void usage_long(FILE * f); 268 void warning(char *); 269 int iperf_exchange_results(struct iperf_test *); 270 int iperf_init_test(struct iperf_test *); 271 int iperf_create_send_timers(struct iperf_test *); 272 int iperf_parse_arguments(struct iperf_test *, int, char **); 273 int iperf_open_logfile(struct iperf_test *); 274 void iperf_reset_test(struct iperf_test *); 275 void iperf_reset_stats(struct iperf_test * test); 276 277 struct protocol *get_protocol(struct iperf_test *, int); 278 int set_protocol(struct iperf_test *, int); 279 280 void iperf_on_new_stream(struct iperf_stream *); 281 void iperf_on_test_start(struct iperf_test *); 282 void iperf_on_connect(struct iperf_test *); 283 void iperf_on_test_finish(struct iperf_test *); 284 285 extern jmp_buf env; 286 287 /* Client routines. */ 288 int iperf_run_client(struct iperf_test *); 289 int iperf_connect(struct iperf_test *); 290 int iperf_create_streams(struct iperf_test *, int sender); 291 int iperf_handle_message_client(struct iperf_test *); 292 int iperf_client_end(struct iperf_test *); 293 294 /* Server routines. */ 295 int iperf_run_server(struct iperf_test *); 296 int iperf_server_listen(struct iperf_test *); 297 int iperf_accept(struct iperf_test *); 298 int iperf_handle_message_server(struct iperf_test *); 299 int iperf_create_pidfile(struct iperf_test *); 300 int iperf_delete_pidfile(struct iperf_test *); 301 302 /* JSON output routines. */ 303 int iperf_json_start(struct iperf_test *); 304 int iperf_json_finish(struct iperf_test *); 305 306 /* CPU affinity routines */ 307 int iperf_setaffinity(struct iperf_test *, int affinity); 308 int iperf_clearaffinity(struct iperf_test *); 309 310 /* Custom printf routine. */ 311 int iperf_printf(struct iperf_test *test, const char *format, ...) __attribute__ ((format(printf,2,3))); 312 int iflush(struct iperf_test *test); 313 314 /* Error routines. */ 315 void iperf_err(struct iperf_test *test, const char *format, ...) __attribute__ ((format(printf,2,3))); 316 void iperf_errexit(struct iperf_test *test, const char *format, ...) __attribute__ ((format(printf,2,3),noreturn)); 317 char *iperf_strerror(int); 318 extern int i_errno; 319 enum { 320 IENONE = 0, // No error 321 /* Parameter errors */ 322 IESERVCLIENT = 1, // Iperf cannot be both server and client 323 IENOROLE = 2, // Iperf must either be a client (-c) or server (-s) 324 IESERVERONLY = 3, // This option is server only 325 IECLIENTONLY = 4, // This option is client only 326 IEDURATION = 5, // test duration too long. Maximum value = %dMAX_TIME 327 IENUMSTREAMS = 6, // Number of parallel streams too large. Maximum value = %dMAX_STREAMS 328 IEBLOCKSIZE = 7, // Block size too large. Maximum value = %dMAX_BLOCKSIZE 329 IEBUFSIZE = 8, // Socket buffer size too large. Maximum value = %dMAX_TCP_BUFFER 330 IEINTERVAL = 9, // Invalid report interval (min = %gMIN_INTERVAL, max = %gMAX_INTERVAL seconds) 331 IEMSS = 10, // MSS too large. Maximum value = %dMAX_MSS 332 IENOSENDFILE = 11, // This OS does not support sendfile 333 IEOMIT = 12, // Bogus value for --omit 334 IEUNIMP = 13, // Not implemented yet 335 IEFILE = 14, // -F file couldn't be opened 336 IEBURST = 15, // Invalid burst count. Maximum value = %dMAX_BURST 337 IEENDCONDITIONS = 16, // Only one test end condition (-t, -n, -k) may be specified 338 IELOGFILE = 17, // Can't open log file 339 IENOSCTP = 18, // No SCTP support available 340 IEBIND = 19, // UNUSED: Local port specified with no local bind option 341 IEUDPBLOCKSIZE = 20, // Block size invalid 342 IEBADTOS = 21, // Bad TOS value 343 IESETCLIENTAUTH = 22, // Bad configuration of client authentication 344 IESETSERVERAUTH = 23, // Bad configuration of server authentication 345 IEBADFORMAT = 24, // Bad format argument to -f 346 IEREVERSEBIDIR = 25, // Iperf cannot be both reverse and bidirectional 347 IEBADPORT = 26, // Bad port number 348 /* Test errors */ 349 IENEWTEST = 100, // Unable to create a new test (check perror) 350 IEINITTEST = 101, // Test initialization failed (check perror) 351 IELISTEN = 102, // Unable to listen for connections (check perror) 352 IECONNECT = 103, // Unable to connect to server (check herror/perror) [from netdial] 353 IEACCEPT = 104, // Unable to accept connection from client (check herror/perror) 354 IESENDCOOKIE = 105, // Unable to send cookie to server (check perror) 355 IERECVCOOKIE = 106, // Unable to receive cookie from client (check perror) 356 IECTRLWRITE = 107, // Unable to write to the control socket (check perror) 357 IECTRLREAD = 108, // Unable to read from the control socket (check perror) 358 IECTRLCLOSE = 109, // Control socket has closed unexpectedly 359 IEMESSAGE = 110, // Received an unknown message 360 IESENDMESSAGE = 111, // Unable to send control message to client/server (check perror) 361 IERECVMESSAGE = 112, // Unable to receive control message from client/server (check perror) 362 IESENDPARAMS = 113, // Unable to send parameters to server (check perror) 363 IERECVPARAMS = 114, // Unable to receive parameters from client (check perror) 364 IEPACKAGERESULTS = 115, // Unable to package results (check perror) 365 IESENDRESULTS = 116, // Unable to send results to client/server (check perror) 366 IERECVRESULTS = 117, // Unable to receive results from client/server (check perror) 367 IESELECT = 118, // Select failed (check perror) 368 IECLIENTTERM = 119, // The client has terminated 369 IESERVERTERM = 120, // The server has terminated 370 IEACCESSDENIED = 121, // The server is busy running a test. Try again later. 371 IESETNODELAY = 122, // Unable to set TCP/SCTP NODELAY (check perror) 372 IESETMSS = 123, // Unable to set TCP/SCTP MSS (check perror) 373 IESETBUF = 124, // Unable to set socket buffer size (check perror) 374 IESETTOS = 125, // Unable to set IP TOS (check perror) 375 IESETCOS = 126, // Unable to set IPv6 traffic class (check perror) 376 IESETFLOW = 127, // Unable to set IPv6 flow label 377 IEREUSEADDR = 128, // Unable to set reuse address on socket (check perror) 378 IENONBLOCKING = 129, // Unable to set socket to non-blocking (check perror) 379 IESETWINDOWSIZE = 130, // Unable to set socket window size (check perror) 380 IEPROTOCOL = 131, // Protocol does not exist 381 IEAFFINITY = 132, // Unable to set CPU affinity (check perror) 382 IEDAEMON = 133, // Unable to become a daemon process 383 IESETCONGESTION = 134, // Unable to set TCP_CONGESTION 384 IEPIDFILE = 135, // Unable to write PID file 385 IEV6ONLY = 136, // Unable to set/unset IPV6_V6ONLY (check perror) 386 IESETSCTPDISABLEFRAG = 137, // Unable to set SCTP Fragmentation (check perror) 387 IESETSCTPNSTREAM= 138, // Unable to set SCTP number of streams (check perror) 388 IESETSCTPBINDX= 139, // Unable to process sctp_bindx() parameters 389 IESETPACING= 140, // Unable to set socket pacing rate 390 IESETBUF2= 141, // Socket buffer size incorrect (written value != read value) 391 IEAUTHTEST = 142, // Test authorization failed 392 /* Stream errors */ 393 IECREATESTREAM = 200, // Unable to create a new stream (check herror/perror) 394 IEINITSTREAM = 201, // Unable to initialize stream (check herror/perror) 395 IESTREAMLISTEN = 202, // Unable to start stream listener (check perror) 396 IESTREAMCONNECT = 203, // Unable to connect stream (check herror/perror) 397 IESTREAMACCEPT = 204, // Unable to accepte stream connection (check perror) 398 IESTREAMWRITE = 205, // Unable to write to stream socket (check perror) 399 IESTREAMREAD = 206, // Unable to read from stream (check perror) 400 IESTREAMCLOSE = 207, // Stream has closed unexpectedly 401 IESTREAMID = 208, // Stream has invalid ID 402 /* Timer errors */ 403 IENEWTIMER = 300, // Unable to create new timer (check perror) 404 IEUPDATETIMER = 301, // Unable to update timer (check perror) 405 }; 406 407 408 #ifdef __cplusplus 409 } /* close extern "C" */ 410 #endif 411 412 413 #endif /* !__IPERF_API_H */ 414