1 #ifndef __MTCP_EPOLL_H_
2 #define __MTCP_EPOLL_H_
3 
4 #include "mtcp_api.h"
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 /** `mtcp_epoll_ctl()` operations */
11 enum mtcp_epoll_op
12 {
13 	MOS_EPOLL_CTL_ADD = 1,
14 	MOS_EPOLL_CTL_DEL = 2,
15 	MOS_EPOLL_CTL_MOD = 3,
16 };
17 
18 /** Event types for mtcp epoll */
19 enum epoll_event_type
20 {
21 	/* mtcp-defined epoll events */
22 	MOS_EPOLLNONE		= (0x1<<0),
23 	MOS_EPOLLIN			= (0x1<<1),
24 	MOS_EPOLLPRI		= (0x1<<2),
25 	MOS_EPOLLOUT		= (0x1<<3),
26 	MOS_EPOLLRDNORM		= (0x1<<4),
27 	MOS_EPOLLRDBAND		= (0x1<<5),
28 	MOS_EPOLLWRNORM		= (0x1<<6),
29 	MOS_EPOLLWRBAND		= (0x1<<7),
30 	MOS_EPOLLMSG		= (0x1<<8),
31 	MOS_EPOLLERR		= (0x1<<9),
32 	MOS_EPOLLHUP		= (0x1<<10),
33 	MOS_EPOLLRDHUP 		= (0x1<<11),
34 
35 	/* mtcp-defined epoll events */
36 	MOS_EPOLLONESHOT	= (0x1 << 30),
37 	MOS_EPOLLET			= (0x1 << 31)
38 };
39 
40 /** Control messages from state update module to react module
41  * XXX: Is this only for internal use? */
42 enum mtcp_action
43 {
44 	/* mtcp action */
45 	MOS_ACT_SEND_DATA 	=	(0x01<<1),
46 	MOS_ACT_SEND_ACK_NOW 	=	(0x01<<2),
47 	MOS_ACT_SEND_ACK_AGG 	=	(0x01<<3),
48 	MOS_ACT_SEND_CONTROL 	=	(0x01<<4),
49 	MOS_ACT_SEND_RST	=	(0x01<<5),
50 	MOS_ACT_DESTROY	 	=	(0x01<<6),
51 	/* only used by monitoring socket */
52 	MOS_ACT_READ_DATA	=	(0x01<<7),
53 	MOS_ACT_CNT
54 };
55 
56 /** epoll data structure */
57 typedef union mtcp_epoll_data
58 {
59 	void *ptr;
60 	int sock;
61 	uint32_t u32;
62 	uint64_t u64;
63 } mtcp_epoll_data_t;
64 
65 /** epoll data structure */
66 struct mtcp_epoll_event
67 {
68 	uint64_t events;
69 	mtcp_epoll_data_t data;
70 };
71 
72 /** Create new epoll descriptor.
73  * @param [in] mctx: mtcp context
74  * @param [in] size: backlog size
75  * @return new epoll descriptor on success, -1 on error
76  *
77  * Same with `epoll_create()`
78  */
79 int
80 mtcp_epoll_create(mctx_t mctx, int size);
81 
82 /** Control epoll.
83  * @param [in] mctx: mtcp context
84  * @param [in] epid: epoll descriptor
85  * @param [in] op: operation
86  *                 (MOS_EPOLL_CTL_ADD, MOS_EPOLL_CTL_DEL, MOS_EPOLL_CTL_MOD)
87  * @param [in] sock: socket ID
88  * @param [in] event: event to be controlled
89  * @return zero on success, -1 on error
90  *
91  * Same with `epoll_ctl()`
92  */
93 int
94 mtcp_epoll_ctl(mctx_t mctx, int epid,
95 		int op, int sock, struct mtcp_epoll_event *event);
96 
97 /** Wait for events.
98  * @param [in] mctx: mtcp context
99  * @param [in] epid: epoll descriptor
100  * @param [in] events: occured events
101  * @param [in] maxevents: maximum number of events to read
102  * @param [in] timeout: timeout
103  * @return number of events occured, -1 on error
104  *
105  * Same with `epoll_wait()`
106  */
107 int
108 mtcp_epoll_wait(mctx_t mctx, int epid,
109 		struct mtcp_epoll_event *events, int maxevents, int timeout);
110 
111 /** Convert built-in event ID to string
112  * @param [in] event: built-in event ID
113  * @return string of the event name
114  */
115 char *
116 EventToString(uint32_t event);
117 
118 #ifdef __cplusplus
119 };
120 #endif
121 
122 #endif /* __MTCP_EPOLL_H_ */
123