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