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