00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef VFER_H
00023 #define VFER_H
00024
00025
00026
00027
00028
00029 #include <stdlib.h>
00030 #include <stdio.h>
00031
00032 #include <unistd.h>
00033 #include <sys/types.h>
00034 #include <sys/socket.h>
00035 #include <sys/un.h>
00036 #include <string.h>
00037 #include <netinet/in.h>
00038 #include <arpa/inet.h>
00039 #include <pthread.h>
00040 #include <signal.h>
00041 #include <sys/select.h>
00042 #include <sys/mman.h>
00043 #include <errno.h>
00044 #include <sys/select.h>
00045 #include <math.h>
00046 #include <fcntl.h>
00047 #include <sys/time.h>
00048 #include <time.h>
00049
00050 #ifdef TSCI2
00051
00052 #include "/home/ivan/include/tsci2.h"
00053 #endif
00054
00055 #include "vfer_btree_delay.h"
00056 #include "vfer_accept_queue.h"
00057 #include "vfer_datagram.h"
00058 #include "vfer_packet.h"
00059
00060
00061
00062
00063
00064
00065
00066 #define VFER_VERSION 1
00067
00068
00069
00070
00071
00072 FILE* debug_out;
00073 FILE* error_out;
00074
00075
00076 #define DEBUG_ERR
00077
00078 #define DEBUG_OUT
00079
00080
00081
00082
00083
00084 char DEBUG_ACCEPTQ;
00085 char DEBUG_API;
00086 char DEBUG_CTL;
00087 char DEBUG_CCTL;
00088 char DEBUG_PACKET;
00089 char DEBUG_PMTUD;
00090
00091
00092 #ifdef DEBUG_ERR
00093 #define ERROR_PRINT(FILE, FUNC, ...) { \
00094 fprintf(error_out, "ERROR [%s :: %s] ", FILE, FUNC); \
00095 fprintf(error_out, __VA_ARGS__); \
00096 fprintf(error_out, "\n"); \
00097 fflush(error_out); }
00098 #else
00099 #define ERROR_PRINT(COND, FILE, FUNC, ...)
00100 #endif
00101
00102 #ifdef DEBUG_OUT
00103 #define DEBUG_PRINT(COND, FILE, FUNC, ...) \
00104 if (COND && debug_out != NULL) {fprintf(debug_out, "%-13s [ %-20s ] ", #COND, FUNC); \
00105 fprintf(debug_out, __VA_ARGS__); \
00106 fprintf(debug_out, "\n"); \
00107 fflush(debug_out); }
00108 #define DEBUG_PACK(COND, FILE, FUNC, PACKET, ...) \
00109 if (COND && debug_out != NULL) {fprintf(debug_out, "%-13s [ %-20s ] ", #COND, FUNC); \
00110 fprintf(debug_out, __VA_ARGS__); \
00111 fprintf(debug_out, "\n%-50s %s\n", " ", Packet_Str(PACKET)); \
00112 \
00113 fflush(debug_out); }
00114 #define DEBUG_CC(COND, FILE, FUNC, SOCKET, ...) \
00115 if (COND && debug_out != NULL) { \
00116 struct timeval tv; \
00117 GET_TIME_OF_DAY(&tv); \
00118 fprintf(debug_out, "%-13s [ %-20s ] ", #COND, FUNC); \
00119 fprintf(debug_out, "rtt[%u] sendw[%u,%u] cwnd[%d] inflight[%u] recvw[%u,%u] rlost[%u] delay[dlta %d]\n", \
00120 (RTT(SOCKET,tv) == 0 ? 1 : RTT(SOCKET,tv)), SOCKET->ccontrol.s_first_seq, SOCKET->ccontrol.s_last_seq, SOCKET->ccontrol.s_cwnd, \
00121 SOCKET->ccontrol.s_inflight, SOCKET->ccontrol.r_first_seq, SOCKET->ccontrol.r_last_seq, SOCKET->ccontrol.r_packets_lost, \
00122 SOCKET->ccontrol.delay_delta); \
00123 fflush(debug_out); \
00124 }
00125 #define DEBUG_PMTUD_PRINT(COND, FILE, FUNC, ...) \
00126 if (COND && debug_out != NULL) {fprintf(debug_out, "%-13s [ %-20s ] ", #COND, FUNC); \
00127 fprintf(debug_out, __VA_ARGS__); \
00128 fprintf(debug_out, "\n"); \
00129 fflush(debug_out);}
00130 #else
00131 #define DEBUG_PRINT(COND, FILE, FUNC, ...)
00132 #define DEBUG_PACK(COND, FILE, FUNC, PACKET, ...)
00133 #define DEBUG_CC(COND, FILE, FUNC, SOCKET, ...)
00134 #define DEBUG_PMTUD_PRINT(COND, FILE, FUNC, SOCKET, ...)
00135 #endif
00136
00137
00138 #define PRINT_MEM printf("MEMORY USED [%d bytes]\n", (int)ReqMem);
00139
00140
00141
00142
00143 #ifdef TSCI2
00144 #define GET_TIME_OF_DAY(TV_PTR) tsci2_gettimeofday(TV_PTR, NULL);
00145 #else
00146 #define GET_TIME_OF_DAY(TV_PTR) gettimeofday(TV_PTR, NULL);
00147 #endif
00148
00149
00150 #define TIMEVAL_MS(TV) ((unsigned int)(((TV).tv_sec * 1000) + ((TV).tv_usec / 1000)))
00151 #define TIMEVAL_US(TV) ((unsigned int)(((TV).tv_sec * 1000000) + ((TV).tv_usec)))
00152
00153
00154 #define TIMEVAL_MS_DIFF(TV1, TV2) (TIMEVAL_MS(TV2) - TIMEVAL_MS(TV1))
00155
00156
00157 #define TIMEVAL_US_DIFF(TV1, TV2) (TIMEVAL_US(TV2) - TIMEVAL_US(TV1))
00158
00159
00160 #define RTT(SOCK_PTR, TV) abs(SOCK_PTR->ccontrol.delay - Tree_Min(&(SOCK_PTR->ccontrol.rev_path_curr_delayhist), TV))
00161
00162 #define ALLOC(DST_ADDR,TYPE_POINTER,TYPE,NUM) ({ \
00163 DST_ADDR=NULL; \
00164 if ((DST_ADDR = (TYPE_POINTER) calloc(NUM, sizeof( TYPE )))) \
00165 ReqMem += (sizeof(TYPE) * NUM); \
00166 \
00167 DST_ADDR; })
00168
00169 #define RELEASE(POINTER_TO_BLOCK, SIZE) { \
00170 free(POINTER_TO_BLOCK); \
00171 ReqMem -= SIZE; \
00172 }\
00173
00174 #define RELEASE_IOV(IOV, LEN) { \
00175 int release_iov_itter; \
00176 for(release_iov_itter = 0; release_iov_itter < LEN; release_iov_itter++) { \
00177 \
00178 RELEASE(IOV[release_iov_itter].iov_base, IOV[release_iov_itter].iov_len); } \
00179 RELEASE(IOV, sizeof(struct iovec));}
00180
00181 #define RELEASE_PACK(PACK) { \
00182 \
00183 RELEASE_IOV(PACK->iov, PACK->iovlen); \
00184 RELEASE(PACK, sizeof(struct packet));}
00185
00186 #define UNLOCK_AND_RETURN_ERROR(mutex, err) { \
00187 pthread_mutex_unlock(&mutex); \
00188 return err; }
00189
00190 #define SET_ERROR_RETURN(sock, error_type, ret) { \
00191 if (sock != NULL) sock->err = error_type; \
00192 return ret; }
00193
00194 #define SET_ERROR_UNLOCK_RETURN(sock, error_type, mutex_ptr, ret) { \
00195 if (sock != NULL) sock->err = error_type; \
00196 pthread_mutex_lock(mutex_ptr); \
00197 return ret; }
00198
00199
00200 #define TREE_INIT(TREE_PTR, INIT_VAL, TV) { \
00201 if (INIT_VAL >= 1000000) { \
00202 TV.tv_sec = (int)(INIT_VAL / 1000000.0); \
00203 } else { \
00204 TV.tv_sec = 0; \
00205 } \
00206 TV.tv_usec = (INIT_VAL - TV.tv_sec * 1000000); \
00207 Tree_Init(TREE_PTR, TV); }
00208
00209 #define CLR_SOCKET_STATS(stats_ptr) { bzero(stats_ptr, sizeof(struct vfer_stats)); }
00210
00211
00212 #define SOCK_OPT_ISSET(socket, opt) (socket->opts & opt)
00213
00214
00215
00216
00217
00218 #define MAX_RESPONSE_TRIES 5
00219 #define HANDSHAKE_ACK_WAIT_INTERVAL 1000000
00220
00221
00222 #define MAX_REQUEST_TRIES 5
00223 #define REQUEST_RETRY_INTERVAL 1000000
00224
00225
00226 #define INIT_MTU 1024
00227 #define MAX_FRAME_SIZE 65535
00228 #define URGENCY_THRESHOLD 1
00229
00230
00231 #define COMMON_MTU_LIST { 1024, 1430, 1978, 4282, 8096, 17844, 31030, 65465 }
00232 #define COMMON_MTU_MAX_INDEX 7
00233 #define NO_PROBE_SENT 0
00234 #define PROBE_SENT 1
00235 #define MTU_FOUND 2
00236 #define NO_PMTUD 3
00237 #define PMTUD_TIME_INTERVAL 600
00238
00239
00240 #define DEST_UNREACHABLE 3
00241 #define FRAG_NEEDED 4
00242
00243
00244 #define DEFAULT_RECV_BUF_MAXPACKETS 3000
00245 #define DEFAULT_SEND_BUF_MAXPACKETS 3000
00246
00247
00248 #define CALC_UDP_RECV_BUF_SIZE(MAXPACKETS) ((DATA_LENGTH + INIT_MTU)*MAXPACKETS)
00249 #define CALC_UDP_SEND_BUF_SIZE(MAXPACKETS) ((DATA_LENGTH + INIT_MTU)*MAXPACKETS)
00250
00251 #define MMAP_CHUNK_SIZE 50000000
00252 #define SEQ_WINDOW 5
00253 #define FRAME_WINDOW 5
00254
00255
00256 #define CONTROLT_SLEEPTIME 1000
00257
00258
00259 #define MAX_SEND_WINDOW 1024*8000
00260
00261
00262 #define DEFAULT_CONNECT_TIMEOUT 60000000
00263
00264
00265 #define UNACKED_THRESH 4
00266 #define MAX_CWND_INCREASE_PACKETS_PER_RTT 8
00267 #define CCONTROL_TARGET 8000
00268
00269
00270
00271
00272
00273 typedef struct sockaddr SA;
00274
00275
00276
00277
00278 typedef struct ccontrol {
00279
00280 uint32_t s_first_seq;
00281 uint32_t s_last_seq;
00282 int32_t s_cwnd;
00283 uint32_t s_cwnd_sent;
00284 int32_t s_maxw;
00285 uint32_t s_inflight;
00286 struct timeval s_last_ack;
00287 struct timeval s_rtt_start;
00288 struct timeval s_last_unacked_data;
00289 frame_link* s_last_data_frame;
00290
00291 uint32_t s_last_data_offset;
00292
00293 struct timeval s_last_vec;
00294 uint16_t s_need_ccack;
00295
00296
00297 uint16_t r_packets_lost;
00298 uint32_t r_first_seq;
00299 uint32_t r_last_seq;
00300 uint32_t r_bytes_total;
00301 struct timeval r_last_ccack;
00302 struct timeval r_last_data;
00303 uint32_t r_unacked_bytes;
00304
00305 uint16_t last_packets_lost;
00306 uint32_t last_bytes_total;
00307
00308 uint32_t mtu;
00309 uint32_t rtt;
00310 int32_t delay_delta;
00311 int32_t delay;
00312 tree_t rev_path_base_delayhist;
00313 tree_t rev_path_curr_delayhist;
00314 int syncing;
00315 struct timeval r_last_loss;
00316 } ccontrol_t;
00317
00318
00319
00320
00321 typedef struct vfer_stats {
00322 uint32_t packets_recvd;
00323 uint32_t packets_sent;
00324 uint32_t bytes_recvd;
00325 uint32_t bytes_sent;
00326 uint32_t data_bytes_recvd;
00327 uint32_t data_bytes_sent;
00328 struct timeval tv_start;
00329 struct timeval tv_end;
00330 double avg_rtt;
00331 double max_rtt;
00332 double min_rtt;
00333 uint32_t rtt_measurements;
00334 uint32_t data_bytes_sender_sent;
00335 } vfer_stats;
00336
00337
00338
00339
00340 typedef struct context_t {
00341 frame f;
00342 vfer_stats* stats;
00343 frame_stats* fstats;
00344 } context_t;
00345
00346 typedef uint32_t bool_t;
00347 typedef bool_t(*relfun_t)(context_t*);
00348
00349
00350
00351
00352 enum vfer_error {
00353 VFER_BADTYPE=-40,
00354 VFER_IMPL,
00355 VFER_BADSOCK,
00356 VFER_TIMEOUT,
00357 VFER_REFUSED,
00358 VFER_NOCONN,
00359 VFER_NOBIND,
00360 VFER_BADADDR,
00361 VFER_NOLISTEN,
00362 VFER_NOACCEPT,
00363 VFER_NOTSTREAM,
00364 VFER_WOULDBLOCK,
00365 VFER_BADOPT,
00366 VFER_BADFD,
00367 VFER_INVAL,
00368 VFER_UNCONN,
00369 VFER_INPROGRESS
00370 } vfer_error;
00371
00372 typedef enum sock_mark {
00373 VFER_READABLE=1,
00374 VFER_WRITABLE=2,
00375 VFER_EXCEPTION=4
00376 } sock_mark_t;
00377
00378 typedef enum sock_state {
00379 CONN_ACQUIRED,
00380 CONN_BOUND,
00381 CONN_LISTENING,
00382 CONN_ACCEPTING,
00383 CONN_CONNECTING,
00384 CONN_CONNECTED,
00385 CONN_DISCONNECTING,
00386 CONN_DISCONNECTED,
00387
00388
00389 CONN_ERR
00390
00391
00392
00393 } sock_state;
00394
00395 typedef enum sock_opt {
00396 VFERSO_QTTL = 1,
00397 VFERSO_SNDSIZE = 3,
00398 VFERSO_RELFUN = 5,
00399
00400 VFERSO_CONNECTTIMEOUT = 6,
00401 VFERSO_CLOSETIMEOUT = 7,
00402 VFERSO_ACCEPTTIMEOUT = 9,
00403 VFERSO_SNDTIMEOUT = 10,
00404 VFERSO_RCVTIMEOUT = 11,
00405
00406
00407
00408 VFERSO_NOPMTUD = 2,
00409 VFERSO_NONBLOCK = 4,
00410
00411 VFERSO_CLIENT_SOCK = 8,
00412 } sock_opt;
00413
00414
00415
00416
00417 typedef struct path_mtu {
00418 char probe_state;
00419 int common_mtus[COMMON_MTU_MAX_INDEX + 1];
00420 int* search_high;
00421 int* search_low;
00422 int* probe_size;
00423 uint32_t frame_num;
00424 uint32_t data_offset;
00425 time_t next_probe_time;
00426 } path_mtu;
00427
00428
00429
00430
00431 typedef struct vfer_sock {
00432 int fd;
00433 int icmp_fd;
00434 int type;
00435 sock_state state;
00436 int opts;
00437 int err;
00438 struct sockaddr_in addr;
00439 socklen_t addr_len;
00440 vfer_stats stats;
00441 uint32_t l_seq;
00442 uint16_t l_frame_num;
00443 uint32_t r_seq;
00444 uint16_t r_frame_num;
00445 uint32_t r_opts;
00446 uint32_t r_recv_buf_size;
00447 uint32_t r_send_buf_size;
00448 uint32_t r_max_frame_size;
00449 relfun_t relfun;
00450
00451 int select_active;
00452 sock_mark_t select_mark;
00453 sock_mark_t select_result;
00454 pthread_mutex_t mutx_readable;
00455 pthread_cond_t cond_readable;
00456 pthread_mutex_t mutx_writeable;
00457 pthread_cond_t cond_writeable;
00458
00459 pthread_mutex_t mutex;
00460 pthread_cond_t cond;
00461 uint32_t udp_send_buf_size;
00462 uint32_t udp_recv_buf_size;
00463
00464 ccontrol_t ccontrol;
00465
00466 frame_list send_frames;
00467 frame_list recv_frames;
00468 frame_list recvd_frames;
00469
00470
00471 path_mtu pmtu;
00472 char is_df_set;
00473
00474
00475 int connect_timeout;
00476 int close_timeout;
00477 int accept_timeout;
00478 int snd_timeout;
00479 int rcv_timeout;
00480
00481 int backlog;
00482 struct accept_queue* accept_queue;
00483 int connect_tries;
00484 int response_tries;
00485 struct timeval last_request;
00486 struct timeval last_response;
00487 struct packet* request;
00488 struct packet* response;
00489 struct vfer_sock* listening_sock;
00490 struct queued_conn* qc;
00491 struct sockaddr_in sa;
00492 } vfer_sock;
00493
00494
00495 #define MAX_SOCKETS 64
00496
00497
00498
00499 typedef struct vfer_socks {
00500 pthread_mutex_t mutex;
00501 int num_active;
00502 fd_set fds;
00503 int max_fd;
00504 vfer_sock s[MAX_SOCKETS];
00505 pthread_mutex_t select_mutex;
00506 pthread_cond_t select_cond;
00507 } vfer_socks_t;
00508
00509 typedef int vfer_fd;
00510
00511
00512
00513
00514
00515 vfer_socks_t sockets;
00516 long int ReqMem;
00517
00518 #endif