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 /* add macro if it is not defined in /usr/include/sys/queue.h */
135 #ifndef TAILQ_FOREACH_SAFE
136 #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
137 	for ((var) = TAILQ_FIRST((head));                               \
138 	     (var) && ((tvar) = TAILQ_NEXT((var), field), 1);		\
139 	     (var) = (tvar))
140 #endif
141 /*----------------------------------------------------------------------------*/
142 struct timer;
143 /*----------------------------------------------------------------------------*/
144 struct eth_table
145 {
146 	char dev_name[128];
147 	int ifindex;
148 	int stat_print;
149 	unsigned char haddr[ETH_ALEN];
150 	uint32_t netmask;
151 	uint32_t ip_addr;
152 	uint64_t cpu_mask;
153 };
154 /*----------------------------------------------------------------------------*/
155 struct route_table
156 {
157 	uint32_t daddr;
158 	uint32_t mask;
159 	uint32_t masked;
160 	int prefix;
161 	int nif;
162 };
163 /*----------------------------------------------------------------------------*/
164 struct arp_entry
165 {
166 	uint32_t ip;
167 	int8_t prefix;
168 	uint32_t ip_mask;
169 	uint32_t ip_masked;
170 	unsigned char haddr[ETH_ALEN];
171 };
172 /*----------------------------------------------------------------------------*/
173 struct arp_table
174 {
175 	struct arp_entry *entry;
176 	int entries;
177 };
178 /*----------------------------------------------------------------------------*/
179 struct mtcp_sender
180 {
181 	int ifidx;
182 
183 	TAILQ_HEAD (control_head, tcp_stream) control_list;
184 	TAILQ_HEAD (send_head, tcp_stream) send_list;
185 	TAILQ_HEAD (ack_head, tcp_stream) ack_list;
186 
187 	int control_list_cnt;
188 	int send_list_cnt;
189 	int ack_list_cnt;
190 };
191 /*----------------------------------------------------------------------------*/
192 struct mtcp_manager
193 {
194 	mem_pool_t bufseg_pool;
195 	mem_pool_t sockent_pool;    /* memory pool for msock list entry */
196 #ifdef USE_TIMER_POOL
197 	mem_pool_t timer_pool;		/* memory pool for struct timer */
198 #endif
199 	mem_pool_t evt_pool;		/* memory pool for struct ev_table */
200 	mem_pool_t flow_pool;		/* memory pool for tcp_stream */
201 	mem_pool_t rv_pool;			/* memory pool for recv variables */
202 	mem_pool_t sv_pool;			/* memory pool for send variables */
203 	mem_pool_t mv_pool;			/* memory pool for monitor variables */
204 
205 	kvs_t *ev_store;
206 
207 	//mem_pool_t socket_pool;
208 	sb_manager_t rbm_snd;
209 	rb_manager_t rbm_rcv;
210 	struct hashtable *tcp_flow_table;
211 
212 	uint32_t s_index;		/* stream index */
213 	/* ptr to smaps for end applications */
214 	socket_map_t smap;
215 	/* ptr to smaps for monitor applications */
216 	socket_map_t msmap;
217 	/* free socket map */
218 	TAILQ_HEAD (, socket_map) free_smap;
219 	/* free monitor socket map */
220 	TAILQ_HEAD (, socket_map) free_msmap;
221 
222 	addr_pool_t ap;			/* address pool */
223 
224 	uint32_t g_id;			/* id space in a thread */
225 	uint32_t flow_cnt;		/* number of concurrent flows */
226 
227 	struct mtcp_thread_context* ctx;
228 
229 	/* variables related to logger */
230 	int sp_fd;
231 	log_thread_context* logger;
232 	log_buff* w_buffer;
233 	FILE *log_fp;
234 
235 	/* variables related to event */
236 	struct mtcp_epoll *ep;
237 	uint32_t ts_last_event;
238 
239 	struct tcp_listener *listener;
240 	TAILQ_HEAD(, mon_listener) monitors;
241 	int num_msp;                    /* # of MOS_SOCK_MONITOR_STREAM */
242 	struct pkt_ctx *pctx;			/* current pkt context */
243 
244 	stream_queue_t connectq;				/* streams need to connect */
245 	stream_queue_t sendq;				/* streams need to send data */
246 	stream_queue_t ackq;					/* streams need to send ack */
247 
248 	stream_queue_t closeq;				/* streams need to close */
249 	stream_queue_int *closeq_int;		/* internally maintained closeq */
250 	stream_queue_t resetq;				/* streams need to reset */
251 	stream_queue_int *resetq_int;		/* internally maintained resetq */
252 
253 	stream_queue_t destroyq;				/* streams need to be destroyed */
254 
255 	struct mtcp_sender *g_sender;
256 	struct mtcp_sender *n_sender[ETH_NUM];
257 
258 	/* lists related to timeout */
259 	struct rto_hashstore* rto_store;
260 	TAILQ_HEAD (timewait_head, tcp_stream) timewait_list;
261 	TAILQ_HEAD (timeout_head, tcp_stream) timeout_list;
262 	TAILQ_HEAD (timer_head, timer) timer_list;
263 
264 	int rto_list_cnt;
265 	int timewait_list_cnt;
266 	int timeout_list_cnt;
267 
268 	uint32_t cur_ts;
269 
270 	int wakeup_flag;
271 	int is_sleeping;
272 
273 	/* statistics */
274 	struct bcast_stat bstat;
275 	struct timeout_stat tstat;
276 #ifdef NETSTAT
277 	struct net_stat nstat;
278 	struct net_stat p_nstat;
279 	uint32_t p_nstat_ts;
280 
281 	struct run_stat runstat;
282 	struct run_stat p_runstat;
283 
284 	struct time_stat rtstat;
285 #endif /* NETSTAT */
286 	struct io_module_func *iom;
287 
288 #ifdef ENABLE_DEBUG_EVENT
289 	char dbg_buf[DBG_BUF_LEN];
290 #endif
291 };
292 /*----------------------------------------------------------------------------*/
293 #ifndef __MTCP_MANAGER
294 #define __MTCP_MANAGER
295 typedef struct mtcp_manager* mtcp_manager_t;
296 #endif
297 /*----------------------------------------------------------------------------*/
298 mtcp_manager_t
299 GetMTCPManager(mctx_t mctx);
300 /*----------------------------------------------------------------------------*/
301 struct mtcp_thread_context
302 {
303 	int cpu;
304 	pthread_t thread;
305 	uint8_t done:1,
306 			exit:1,
307 			interrupt:1;
308 
309 	struct mtcp_manager* mtcp_manager;
310 
311 	void *io_private_context;
312 
313 	pthread_mutex_t flow_pool_lock;
314 	pthread_mutex_t socket_pool_lock;
315 
316 #if LOCK_STREAM_QUEUE
317 #if USE_SPIN_LOCK
318 	pthread_spinlock_t connect_lock;
319 	pthread_spinlock_t close_lock;
320 	pthread_spinlock_t reset_lock;
321 	pthread_spinlock_t sendq_lock;
322 	pthread_spinlock_t ackq_lock;
323 	pthread_spinlock_t destroyq_lock;
324 #else
325 	pthread_mutex_t connect_lock;
326 	pthread_mutex_t close_lock;
327 	pthread_mutex_t reset_lock;
328 	pthread_mutex_t sendq_lock;
329 	pthread_mutex_t ackq_lock;
330 	pthread_mutex_t destroyq_lock;
331 #endif /* USE_SPIN_LOCK */
332 #endif /* LOCK_STREAM_QUEUE */
333 };
334 /*----------------------------------------------------------------------------*/
335 typedef struct mtcp_thread_context* mtcp_thread_context_t;
336 /*----------------------------------------------------------------------------*/
337 struct mtcp_manager *g_mtcp[MAX_CPUS];
338 struct mtcp_context *g_ctx[MAX_CPUS];
339 extern addr_pool_t ap[ETH_NUM];
340 /*----------------------------------------------------------------------------*/
341 void
342 RunPassiveLoop(mtcp_manager_t mtcp);
343 /*----------------------------------------------------------------------------*/
344 #endif /* __MTCP_H_ */
345