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