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