1 /* 2 * iperf, Copyright (c) 2014-2020, 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 #ifndef _GNU_SOURCE 40 # define _GNU_SOURCE 41 #endif 42 #include <netinet/tcp.h> 43 #include <net/if.h> // for IFNAMSIZ 44 45 #if defined(HAVE_CPUSET_SETAFFINITY) 46 #include <sys/param.h> 47 #include <sys/cpuset.h> 48 #endif /* HAVE_CPUSET_SETAFFINITY */ 49 50 #if defined(HAVE_INTTYPES_H) 51 # include <inttypes.h> 52 #else 53 # ifndef PRIu64 54 # if sizeof(long) == 8 55 # define PRIu64 "lu" 56 # else 57 # define PRIu64 "llu" 58 # endif 59 # endif 60 #endif 61 62 #include "timer.h" 63 #include "queue.h" 64 #include "cjson.h" 65 #include "iperf_time.h" 66 67 #if defined(HAVE_SSL) 68 #include <openssl/bio.h> 69 #include <openssl/evp.h> 70 #endif // HAVE_SSL 71 72 #if !defined(__IPERF_API_H) 73 typedef uint64_t iperf_size_t; 74 #endif // __IPERF_API_H 75 76 struct iperf_interval_results 77 { 78 iperf_size_t bytes_transferred; /* bytes transfered in this interval */ 79 struct iperf_time interval_start_time; 80 struct iperf_time interval_end_time; 81 float interval_duration; 82 83 /* for UDP */ 84 int interval_packet_count; 85 int interval_outoforder_packets; 86 int interval_cnt_error; 87 int packet_count; 88 double jitter; 89 int outoforder_packets; 90 int cnt_error; 91 92 int omitted; 93 #if (defined(linux) || defined(__FreeBSD__) || defined(__NetBSD__)) && \ 94 defined(TCP_INFO) 95 struct tcp_info tcpInfo; /* getsockopt(TCP_INFO) for Linux, {Free,Net}BSD */ 96 #else 97 /* Just placeholders, never accessed. */ 98 char *tcpInfo; 99 #endif 100 int interval_retrans; 101 int interval_sacks; 102 int snd_cwnd; 103 TAILQ_ENTRY(iperf_interval_results) irlistentries; 104 void *custom_data; 105 int rtt; 106 int rttvar; 107 int pmtu; 108 }; 109 110 struct iperf_stream_result 111 { 112 iperf_size_t bytes_received; 113 iperf_size_t bytes_sent; 114 iperf_size_t bytes_received_this_interval; 115 iperf_size_t bytes_sent_this_interval; 116 iperf_size_t bytes_sent_omit; 117 int stream_prev_total_retrans; 118 int stream_retrans; 119 int stream_prev_total_sacks; 120 int stream_sacks; 121 int stream_max_rtt; 122 int stream_min_rtt; 123 int stream_sum_rtt; 124 int stream_count_rtt; 125 int stream_max_snd_cwnd; 126 struct iperf_time start_time; 127 struct iperf_time end_time; 128 struct iperf_time start_time_fixed; 129 double sender_time; 130 double receiver_time; 131 TAILQ_HEAD(irlisthead, iperf_interval_results) interval_results; 132 void *data; 133 }; 134 135 #define COOKIE_SIZE 37 /* size of an ascii uuid */ 136 struct iperf_settings 137 { 138 int domain; /* AF_INET or AF_INET6 */ 139 int socket_bufsize; /* window size for TCP */ 140 int blksize; /* size of read/writes (-l) */ 141 iperf_size_t rate; /* target data rate for application pacing*/ 142 iperf_size_t bitrate_limit; /* server's maximum allowed total data rate for all streams*/ 143 double bitrate_limit_interval; /* interval for avaraging total data rate */ 144 int bitrate_limit_stats_per_interval; /* calculated number of stats periods for averaging total data rate */ 145 uint64_t fqrate; /* target data rate for FQ pacing*/ 146 int pacing_timer; /* pacing timer in microseconds */ 147 int burst; /* packets per burst */ 148 int mss; /* for TCP MSS */ 149 int ttl; /* IP TTL option */ 150 int tos; /* type of service bit */ 151 int flowlabel; /* IPv6 flow label */ 152 iperf_size_t bytes; /* number of bytes to send */ 153 iperf_size_t blocks; /* number of blocks (packets) to send */ 154 char unit_format; /* -f */ 155 int num_ostreams; /* SCTP initmsg settings */ 156 int dont_fragment; /* Whether to set IP flag Do-Not_Fragment */ 157 #if defined(HAVE_SSL) 158 char *authtoken; /* Authentication token */ 159 char *client_username; 160 char *client_password; 161 EVP_PKEY *client_rsa_pubkey; 162 #endif // HAVE_SSL 163 int connect_timeout; /* socket connection timeout, in ms */ 164 int idle_timeout; /* server idle time timeout */ 165 struct iperf_time rcv_timeout; /* Timeout for receiving messages in active mode, in us */ 166 }; 167 168 struct iperf_test; 169 170 struct iperf_stream 171 { 172 struct iperf_test* test; 173 174 /* configurable members */ 175 int local_port; 176 int remote_port; 177 int socket; 178 int id; 179 int sender; 180 /* XXX: is settings just a pointer to the same struct in iperf_test? if not, 181 should it be? */ 182 struct iperf_settings *settings; /* pointer to structure settings */ 183 184 /* non configurable members */ 185 struct iperf_stream_result *result; /* structure pointer to result */ 186 Timer *send_timer; 187 int green_light; 188 int buffer_fd; /* data to send, file descriptor */ 189 char *buffer; /* data to send, mmapped */ 190 int pending_size; /* pending data to send */ 191 int diskfile_fd; /* file to send, file descriptor */ 192 int diskfile_left; /* remaining file data on disk */ 193 194 /* 195 * for udp measurements - This can be a structure outside stream, and 196 * stream can have a pointer to this 197 */ 198 int packet_count; 199 int peer_packet_count; 200 int omitted_packet_count; 201 double jitter; 202 double prev_transit; 203 int outoforder_packets; 204 int omitted_outoforder_packets; 205 int cnt_error; 206 int omitted_cnt_error; 207 uint64_t target; 208 209 struct sockaddr_storage local_addr; 210 struct sockaddr_storage remote_addr; 211 212 int (*rcv) (struct iperf_stream * stream); 213 int (*snd) (struct iperf_stream * stream); 214 215 /* chained send/receive routines for -F mode */ 216 int (*rcv2) (struct iperf_stream * stream); 217 int (*snd2) (struct iperf_stream * stream); 218 219 // struct iperf_stream *next; 220 SLIST_ENTRY(iperf_stream) streams; 221 222 void *data; 223 }; 224 225 struct protocol { 226 int id; 227 char *name; 228 int (*accept)(struct iperf_test *); 229 int (*listen)(struct iperf_test *); 230 int (*connect)(struct iperf_test *); 231 int (*send)(struct iperf_stream *); 232 int (*recv)(struct iperf_stream *); 233 int (*init)(struct iperf_test *); 234 SLIST_ENTRY(protocol) protocols; 235 }; 236 237 struct iperf_textline { 238 char *line; 239 TAILQ_ENTRY(iperf_textline) textlineentries; 240 }; 241 242 struct xbind_entry { 243 char *name; 244 struct addrinfo *ai; 245 TAILQ_ENTRY(xbind_entry) link; 246 }; 247 248 enum iperf_mode { 249 SENDER = 1, 250 RECEIVER = 0, 251 BIDIRECTIONAL = -1 252 }; 253 254 struct iperf_test 255 { 256 char role; /* 'c' lient or 's' erver */ 257 enum iperf_mode mode; 258 int sender_has_retransmits; 259 int other_side_has_retransmits; /* used if mode == BIDIRECTIONAL */ 260 struct protocol *protocol; 261 signed char state; 262 char *server_hostname; /* -c option */ 263 char *tmp_template; 264 char *bind_address; /* first -B option */ 265 char *bind_dev; /* bind to network device */ 266 TAILQ_HEAD(xbind_addrhead, xbind_entry) xbind_addrs; /* all -X opts */ 267 int bind_port; /* --cport option */ 268 int server_port; 269 int omit; /* duration of omit period (-O flag) */ 270 int duration; /* total duration of test (-t flag) */ 271 char *diskfile_name; /* -F option */ 272 int affinity, server_affinity; /* -A option */ 273 #if defined(HAVE_CPUSET_SETAFFINITY) 274 cpuset_t cpumask; 275 #endif /* HAVE_CPUSET_SETAFFINITY */ 276 char *title; /* -T option */ 277 char *extra_data; /* --extra-data */ 278 char *congestion; /* -C option */ 279 char *congestion_used; /* what was actually used */ 280 char *remote_congestion_used; /* what the other side used */ 281 char *pidfile; /* -P option */ 282 283 char *logfile; /* --logfile option */ 284 FILE *outfile; 285 286 int ctrl_sck; 287 int listener; 288 int prot_listener; 289 290 int ctrl_sck_mss; /* MSS for the control channel */ 291 292 #if defined(HAVE_SSL) 293 char *server_authorized_users; 294 EVP_PKEY *server_rsa_private_key; 295 int server_skew_threshold; 296 #endif // HAVE_SSL 297 298 /* boolean variables for Options */ 299 int daemon; /* -D option */ 300 int one_off; /* -1 option */ 301 int no_delay; /* -N option */ 302 int reverse; /* -R option */ 303 int bidirectional; /* --bidirectional */ 304 int verbose; /* -V option - verbose mode */ 305 int json_output; /* -J option - JSON output */ 306 int zerocopy; /* -Z option - use sendfile */ 307 int debug; /* -d option - enable debug */ 308 int get_server_output; /* --get-server-output */ 309 int udp_counters_64bit; /* --use-64-bit-udp-counters */ 310 int forceflush; /* --forceflush - flushing output at every interval */ 311 int multisend; 312 int repeating_payload; /* --repeating-payload */ 313 int timestamps; /* --timestamps */ 314 char *timestamp_format; 315 316 char *json_output_string; /* rendered JSON output if json_output is set */ 317 /* Select related parameters */ 318 int max_fd; 319 fd_set read_set; /* set of read sockets */ 320 fd_set write_set; /* set of write sockets */ 321 322 /* Interval related members */ 323 int omitting; 324 double stats_interval; 325 double reporter_interval; 326 void (*stats_callback) (struct iperf_test *); 327 void (*reporter_callback) (struct iperf_test *); 328 Timer *omit_timer; 329 Timer *timer; 330 int done; 331 Timer *stats_timer; 332 Timer *reporter_timer; 333 334 double cpu_util[3]; /* cpu utilization of the test - total, user, system */ 335 double remote_cpu_util[3]; /* cpu utilization for the remote host/client - total, user, system */ 336 337 int num_streams; /* total streams in the test (-P) */ 338 339 iperf_size_t bytes_sent; 340 iperf_size_t blocks_sent; 341 342 iperf_size_t bytes_received; 343 iperf_size_t blocks_received; 344 345 iperf_size_t bitrate_limit_stats_count; /* Number of stats periods accumulated for server's total bitrate average */ 346 iperf_size_t *bitrate_limit_intervals_traffic_bytes; /* Pointer to a cyclic array that includes the last interval's bytes transferred */ 347 iperf_size_t bitrate_limit_last_interval_index; /* Index of the last interval traffic insrted into the cyclic array */ 348 int bitrate_limit_exceeded; /* Set by callback routine when average data rate exceeded the server's bitrate limit */ 349 350 int server_last_run_rc; /* Save last server run rc for next test */ 351 uint server_forced_idle_restarts_count; /* count number of forced server restarts to make sure it is not stack */ 352 uint server_forced_no_msg_restarts_count; /* count number of forced server restarts to make sure it is not stack */ 353 uint server_test_number; /* count number of tests performed by a server */ 354 355 char cookie[COOKIE_SIZE]; 356 // struct iperf_stream *streams; /* pointer to list of struct stream */ 357 SLIST_HEAD(slisthead, iperf_stream) streams; 358 struct iperf_settings *settings; 359 360 SLIST_HEAD(plisthead, protocol) protocols; 361 362 /* callback functions */ 363 void (*on_new_stream)(struct iperf_stream *); 364 void (*on_test_start)(struct iperf_test *); 365 void (*on_connect)(struct iperf_test *); 366 void (*on_test_finish)(struct iperf_test *); 367 368 /* cJSON handles for use when in -J mode */\ 369 cJSON *json_top; 370 cJSON *json_start; 371 cJSON *json_connected; 372 cJSON *json_intervals; 373 cJSON *json_end; 374 375 /* Server output (use on client side only) */ 376 char *server_output_text; 377 cJSON *json_server_output; 378 379 /* Server output (use on server side only) */ 380 TAILQ_HEAD(iperf_textlisthead, iperf_textline) server_output_list; 381 382 }; 383 384 /* default settings */ 385 #define PORT 5201 /* default port to listen on (don't use the same port as iperf2) */ 386 #define uS_TO_NS 1000 387 #define mS_TO_US 1000 388 #define SEC_TO_mS 1000 389 #define SEC_TO_US 1000000LL 390 #define UDP_RATE (1024 * 1024) /* 1 Mbps */ 391 #define OMIT 0 /* seconds */ 392 #define DURATION 10 /* seconds */ 393 394 #define SEC_TO_NS 1000000000LL /* too big for enum/const on some platforms */ 395 #define MAX_RESULT_STRING 4096 396 397 #define UDP_BUFFER_EXTRA 1024 398 399 /* constants for command line arg sanity checks */ 400 #define MB (1024 * 1024) 401 #define MAX_TCP_BUFFER (512 * MB) 402 #define MAX_BLOCKSIZE MB 403 /* Minimum size UDP send is the size of two 32-bit ints followed by a 64-bit int */ 404 #define MIN_UDP_BLOCKSIZE (4 + 4 + 8) 405 /* Maximum size UDP send is (64K - 1) - IP and UDP header sizes */ 406 #define MAX_UDP_BLOCKSIZE (65535 - 8 - 20) 407 #define MIN_INTERVAL 0.1 408 #define MAX_INTERVAL 60.0 409 #define MAX_TIME 86400 410 #define MAX_BURST 1000 411 #define MAX_MSS (9 * 1024) 412 #define MAX_STREAMS 128 413 414 #define TIMESTAMP_FORMAT "%c " 415 416 extern int gerror; /* error value from getaddrinfo(3), for use in internal error handling */ 417 418 #endif /* !__IPERF_H */ 419