1 #ifndef __MTCP_H_ 2 #define __MTCP_H_ 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <sys/time.h> 7 #include <sys/queue.h> 8 #include <pthread.h> 9 10 #include "memory_mgt.h" 11 #include "tcp_ring_buffer.h" 12 #include "tcp_send_buffer.h" 13 #include "tcp_stream_queue.h" 14 #include "socket.h" 15 #include "mtcp_api.h" 16 #include "eventpoll.h" 17 #include "addr_pool.h" 18 #include "logger.h" 19 #include "stat.h" 20 #include "io_module.h" 21 #include "key_value_store.h" 22 23 #ifndef TRUE 24 #define TRUE (1) 25 #endif 26 27 #ifndef FALSE 28 #define FALSE (0) 29 #endif 30 31 #ifndef ERROR 32 #define ERROR (-1) 33 #endif 34 35 #ifndef likely 36 #define likely(x) __builtin_expect((x),1) 37 #endif 38 #ifndef unlikely 39 #define unlikely(x) __builtin_expect((x),0) 40 #endif 41 42 #ifdef ENABLE_DEBUG_EVENT 43 #define DBG_BUF_LEN 2048 44 #endif 45 46 #define MAX_CPUS 16 47 48 /* 49 * Ethernet frame overhead 50 */ 51 #ifndef ETHER_CRC_LEN 52 #define ETHER_CRC_LEN 4 53 #endif 54 #define ETHER_IFG 12 55 #define ETHER_PREAMBLE 8 56 #define ETHER_OVR (ETHER_CRC_LEN + ETHER_PREAMBLE + ETHER_IFG) 57 58 #ifndef ETH_ALEN 59 #define ETH_ALEN 6 60 #endif 61 #define ETHERNET_HEADER_LEN 14 // sizeof(struct ethhdr) 62 #ifdef ENABLE_PCAP 63 #define ETHERNET_FRAME_LEN 4096 64 #else 65 #define ETHERNET_FRAME_LEN 1514 // max possible frame len 66 #endif 67 #define IP_HEADER_LEN 20 // sizeof(struct iphdr) 68 #define TCP_HEADER_LEN 20 // sizeof(struct tcphdr) 69 #define TOTAL_TCP_HEADER_LEN 54 // total header length 70 71 /* configrations */ 72 #define BACKLOG_SIZE (10*1024) 73 #define MAX_PKT_SIZE (2*1024) 74 #define ETH_NUM 16 75 #define LOGFLD_NAME_LEN 1024 76 #define APP_NAME_LEN 40 77 #define MOS_APP 20 78 79 #define TCP_OPT_TIMESTAMP_ENABLED TRUE 80 #define TCP_OPT_SACK_ENABLED FALSE 81 82 //#define USE_TIMER_POOL 83 84 #ifdef DARWIN 85 #define USE_SPIN_LOCK FALSE 86 #else 87 #define USE_SPIN_LOCK TRUE 88 #endif 89 #define LOCK_STREAM_QUEUE FALSE 90 #define INTR_SLEEPING_MTCP TRUE 91 #define PROMISCUOUS_MODE TRUE 92 93 #define MTCP_SET(a, x) (a |= x) 94 #define MTCP_ISSET(a, x) (a & x) 95 #define MTCP_CLR(a) (a = 0) 96 97 #define CB_SET(a, x) MTCP_SET(a, x) 98 #define CB_ISSET(a, x) MTCP_ISSET(a, x) 99 #define CB_CLR(a) MTCP_CLR(a) 100 101 #define ACTION_SET(a,x) MTCP_SET(a, x) 102 #define ACTION_ISSET(a, x) MTCP_ISSET(a, x) 103 #define ACTION_CLR(a) MTCP_CLR(a) 104 105 /*----------------------------------------------------------------------------*/ 106 /* Statistics */ 107 #ifdef NETSTAT 108 #define NETSTAT_PERTHREAD TRUE 109 #define NETSTAT_TOTAL TRUE 110 #endif /* NETSTAT */ 111 #define RTM_STAT FALSE 112 /*----------------------------------------------------------------------------*/ 113 /* Lock definitions for socket buffer */ 114 #if USE_SPIN_LOCK 115 #define SBUF_LOCK_INIT(lock, errmsg, action); \ 116 if (pthread_spin_init(lock, PTHREAD_PROCESS_PRIVATE)) { \ 117 perror("pthread_spin_init" errmsg); \ 118 action; \ 119 } 120 #define SBUF_LOCK_DESTROY(lock) pthread_spin_destroy(lock) 121 #define SBUF_LOCK(lock) pthread_spin_lock(lock) 122 #define SBUF_UNLOCK(lock) pthread_spin_unlock(lock) 123 #else 124 #define SBUF_LOCK_INIT(lock, errmsg, action); \ 125 if (pthread_mutex_init(lock, NULL)) { \ 126 perror("pthread_mutex_init" errmsg); \ 127 action; \ 128 } 129 #define SBUF_LOCK_DESTROY(lock) pthread_mutex_destroy(lock) 130 #define SBUF_LOCK(lock) pthread_mutex_lock(lock) 131 #define SBUF_UNLOCK(lock) pthread_mutex_unlock(lock) 132 #endif /* USE_SPIN_LOCK */ 133 /*----------------------------------------------------------------------------*/ 134 struct timer; 135 /*----------------------------------------------------------------------------*/ 136 struct eth_table 137 { 138 char dev_name[128]; 139 int ifindex; 140 int stat_print; 141 unsigned char haddr[ETH_ALEN]; 142 uint32_t netmask; 143 uint32_t ip_addr; 144 uint64_t cpu_mask; 145 }; 146 /*----------------------------------------------------------------------------*/ 147 struct route_table 148 { 149 uint32_t daddr; 150 uint32_t mask; 151 uint32_t masked; 152 int prefix; 153 int nif; 154 }; 155 /*----------------------------------------------------------------------------*/ 156 struct arp_entry 157 { 158 uint32_t ip; 159 int8_t prefix; 160 uint32_t ip_mask; 161 uint32_t ip_masked; 162 unsigned char haddr[ETH_ALEN]; 163 }; 164 /*----------------------------------------------------------------------------*/ 165 struct arp_table 166 { 167 struct arp_entry *entry; 168 int entries; 169 }; 170 /*----------------------------------------------------------------------------*/ 171 struct mtcp_sender 172 { 173 int ifidx; 174 175 TAILQ_HEAD (control_head, tcp_stream) control_list; 176 TAILQ_HEAD (send_head, tcp_stream) send_list; 177 TAILQ_HEAD (ack_head, tcp_stream) ack_list; 178 179 int control_list_cnt; 180 int send_list_cnt; 181 int ack_list_cnt; 182 }; 183 /*----------------------------------------------------------------------------*/ 184 struct mtcp_manager 185 { 186 mem_pool_t bufseg_pool; 187 mem_pool_t sockent_pool; /* memory pool for msock list entry */ 188 #ifdef USE_TIMER_POOL 189 mem_pool_t timer_pool; /* memory pool for struct timer */ 190 #endif 191 mem_pool_t evt_pool; /* memory pool for struct ev_table */ 192 mem_pool_t flow_pool; /* memory pool for tcp_stream */ 193 mem_pool_t rv_pool; /* memory pool for recv variables */ 194 mem_pool_t sv_pool; /* memory pool for send variables */ 195 mem_pool_t mv_pool; /* memory pool for monitor variables */ 196 197 kvs_t *ev_store; 198 199 //mem_pool_t socket_pool; 200 sb_manager_t rbm_snd; 201 rb_manager_t rbm_rcv; 202 struct hashtable *tcp_flow_table; 203 204 uint32_t s_index; /* stream index */ 205 /* ptr to smaps for end applications */ 206 socket_map_t smap; 207 /* ptr to smaps for monitor applications */ 208 socket_map_t msmap; 209 /* free socket map */ 210 TAILQ_HEAD (, socket_map) free_smap; 211 /* free monitor socket map */ 212 TAILQ_HEAD (, socket_map) free_msmap; 213 214 addr_pool_t ap; /* address pool */ 215 216 uint32_t g_id; /* id space in a thread */ 217 uint32_t flow_cnt; /* number of concurrent flows */ 218 219 struct mtcp_thread_context* ctx; 220 221 /* variables related to logger */ 222 int sp_fd; 223 log_thread_context* logger; 224 log_buff* w_buffer; 225 FILE *log_fp; 226 227 /* variables related to event */ 228 struct mtcp_epoll *ep; 229 uint32_t ts_last_event; 230 231 struct tcp_listener *listener; 232 TAILQ_HEAD(, mon_listener) monitors; 233 int num_msp; /* # of MOS_SOCK_MONITOR_STREAM */ 234 struct pkt_ctx *pctx; /* current pkt context */ 235 236 stream_queue_t connectq; /* streams need to connect */ 237 stream_queue_t sendq; /* streams need to send data */ 238 stream_queue_t ackq; /* streams need to send ack */ 239 240 stream_queue_t closeq; /* streams need to close */ 241 stream_queue_int *closeq_int; /* internally maintained closeq */ 242 stream_queue_t resetq; /* streams need to reset */ 243 stream_queue_int *resetq_int; /* internally maintained resetq */ 244 245 stream_queue_t destroyq; /* streams need to be destroyed */ 246 247 struct mtcp_sender *g_sender; 248 struct mtcp_sender *n_sender[ETH_NUM]; 249 250 /* lists related to timeout */ 251 struct rto_hashstore* rto_store; 252 TAILQ_HEAD (timewait_head, tcp_stream) timewait_list; 253 TAILQ_HEAD (timeout_head, tcp_stream) timeout_list; 254 TAILQ_HEAD (timer_head, timer) timer_list; 255 256 int rto_list_cnt; 257 int timewait_list_cnt; 258 int timeout_list_cnt; 259 260 uint32_t cur_ts; 261 262 int wakeup_flag; 263 int is_sleeping; 264 265 /* statistics */ 266 struct bcast_stat bstat; 267 struct timeout_stat tstat; 268 #ifdef NETSTAT 269 struct net_stat nstat; 270 struct net_stat p_nstat; 271 uint32_t p_nstat_ts; 272 273 struct run_stat runstat; 274 struct run_stat p_runstat; 275 276 struct time_stat rtstat; 277 #endif /* NETSTAT */ 278 struct io_module_func *iom; 279 280 #ifdef ENABLE_DEBUG_EVENT 281 char dbg_buf[DBG_BUF_LEN]; 282 #endif 283 }; 284 /*----------------------------------------------------------------------------*/ 285 #ifndef __MTCP_MANAGER 286 #define __MTCP_MANAGER 287 typedef struct mtcp_manager* mtcp_manager_t; 288 #endif 289 /*----------------------------------------------------------------------------*/ 290 mtcp_manager_t 291 GetMTCPManager(mctx_t mctx); 292 /*----------------------------------------------------------------------------*/ 293 struct mtcp_thread_context 294 { 295 int cpu; 296 pthread_t thread; 297 uint8_t done:1, 298 exit:1, 299 interrupt:1; 300 301 struct mtcp_manager* mtcp_manager; 302 303 void *io_private_context; 304 305 pthread_mutex_t flow_pool_lock; 306 pthread_mutex_t socket_pool_lock; 307 308 #if LOCK_STREAM_QUEUE 309 #if USE_SPIN_LOCK 310 pthread_spinlock_t connect_lock; 311 pthread_spinlock_t close_lock; 312 pthread_spinlock_t reset_lock; 313 pthread_spinlock_t sendq_lock; 314 pthread_spinlock_t ackq_lock; 315 pthread_spinlock_t destroyq_lock; 316 #else 317 pthread_mutex_t connect_lock; 318 pthread_mutex_t close_lock; 319 pthread_mutex_t reset_lock; 320 pthread_mutex_t sendq_lock; 321 pthread_mutex_t ackq_lock; 322 pthread_mutex_t destroyq_lock; 323 #endif /* USE_SPIN_LOCK */ 324 #endif /* LOCK_STREAM_QUEUE */ 325 }; 326 /*----------------------------------------------------------------------------*/ 327 typedef struct mtcp_thread_context* mtcp_thread_context_t; 328 /*----------------------------------------------------------------------------*/ 329 struct mtcp_manager *g_mtcp[MAX_CPUS]; 330 struct mtcp_context *g_ctx[MAX_CPUS]; 331 extern addr_pool_t ap[ETH_NUM]; 332 /*----------------------------------------------------------------------------*/ 333 void 334 RunPassiveLoop(mtcp_manager_t mtcp); 335 /*----------------------------------------------------------------------------*/ 336 #endif /* __MTCP_H_ */ 337