1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2005-2018 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file s_bsd_epoll.c |
23 |
* \brief Linux epoll() compatible network routines. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_EPOLL |
29 |
#include "fdlist.h" |
30 |
#include "ircd.h" |
31 |
#include "s_bsd.h" |
32 |
#include "log.h" |
33 |
#include "memory.h" |
34 |
#include <sys/epoll.h> |
35 |
|
36 |
enum |
37 |
{ |
38 |
INITIAL_NEVENT = 16, |
39 |
MAXIMUM_NEVENT = 4096 |
40 |
}; |
41 |
|
42 |
struct epollop |
43 |
{ |
44 |
struct epoll_event *events; |
45 |
int nevents; |
46 |
int fd; |
47 |
}; |
48 |
|
49 |
static struct epollop *epollop; |
50 |
|
51 |
|
52 |
/* |
53 |
* comm_select_init |
54 |
* |
55 |
* This is a needed exported function which will be called to initialise |
56 |
* the network loop code. |
57 |
*/ |
58 |
void |
59 |
comm_select_init(void) |
60 |
{ |
61 |
int fd = epoll_create1(EPOLL_CLOEXEC); |
62 |
if (fd < 0) |
63 |
{ |
64 |
ilog(LOG_TYPE_IRCD, "comm_select_init: couldn't open epoll fd: %s", |
65 |
strerror(errno)); |
66 |
exit(EXIT_FAILURE); /* Whee! */ |
67 |
} |
68 |
|
69 |
fd_open(fd, 0, "epoll file descriptor"); |
70 |
|
71 |
epollop = xcalloc(sizeof(*epollop)); |
72 |
epollop->fd = fd; |
73 |
epollop->nevents = INITIAL_NEVENT; |
74 |
epollop->events = xcalloc(epollop->nevents * sizeof(*epollop->events)); |
75 |
} |
76 |
|
77 |
/* |
78 |
* comm_setselect |
79 |
* |
80 |
* This is a needed exported function which will be called to register |
81 |
* and deregister interest in a pending IO state for a given FD. |
82 |
*/ |
83 |
void |
84 |
comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *), |
85 |
void *client_data, uintmax_t timeout) |
86 |
{ |
87 |
int new_events, op; |
88 |
struct epoll_event ep_event; |
89 |
|
90 |
assert(F->flags.open); |
91 |
|
92 |
if ((type & COMM_SELECT_READ)) |
93 |
{ |
94 |
F->read_handler = handler; |
95 |
F->read_data = client_data; |
96 |
} |
97 |
|
98 |
if ((type & COMM_SELECT_WRITE)) |
99 |
{ |
100 |
F->write_handler = handler; |
101 |
F->write_data = client_data; |
102 |
} |
103 |
|
104 |
new_events = (F->read_handler ? EPOLLIN : 0) | |
105 |
(F->write_handler ? EPOLLOUT : 0); |
106 |
|
107 |
if (timeout) |
108 |
{ |
109 |
F->timeout = CurrentTime + (timeout / 1000); |
110 |
F->timeout_handler = handler; |
111 |
F->timeout_data = client_data; |
112 |
} |
113 |
|
114 |
if (new_events != F->evcache) |
115 |
{ |
116 |
if (new_events == 0) |
117 |
op = EPOLL_CTL_DEL; |
118 |
else if (F->evcache == 0) |
119 |
op = EPOLL_CTL_ADD; |
120 |
else |
121 |
op = EPOLL_CTL_MOD; |
122 |
|
123 |
memset(&ep_event, 0, sizeof(ep_event)); |
124 |
ep_event.events = F->evcache = new_events; |
125 |
ep_event.data.ptr = F; |
126 |
|
127 |
if (epoll_ctl(epollop->fd, op, F->fd, &ep_event)) |
128 |
{ |
129 |
ilog(LOG_TYPE_IRCD, "comm_setselect: epoll_ctl() failed: %s", strerror(errno)); |
130 |
abort(); |
131 |
} |
132 |
} |
133 |
} |
134 |
|
135 |
/* |
136 |
* comm_select() |
137 |
* |
138 |
* Called to do the new-style IO, courtesy of of squid (like most of this |
139 |
* new IO code). This routine handles the stuff we've hidden in |
140 |
* comm_setselect and fd_table[] and calls callbacks for IO ready |
141 |
* events. |
142 |
*/ |
143 |
void |
144 |
comm_select(void) |
145 |
{ |
146 |
int num; |
147 |
void (*hdl)(fde_t *, void *); |
148 |
|
149 |
num = epoll_wait(epollop->fd, epollop->events, epollop->nevents, SELECT_DELAY); |
150 |
assert(num <= epollop->nevents); |
151 |
|
152 |
set_time(); |
153 |
|
154 |
if (num < 0) |
155 |
{ |
156 |
const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 }; |
157 |
nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */ |
158 |
return; |
159 |
} |
160 |
|
161 |
for (int i = 0; i < num; ++i) |
162 |
{ |
163 |
fde_t *F = epollop->events[i].data.ptr; |
164 |
|
165 |
if (F->flags.open == 0) |
166 |
continue; |
167 |
|
168 |
if ((epollop->events[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR))) |
169 |
{ |
170 |
if ((hdl = F->read_handler)) |
171 |
{ |
172 |
F->read_handler = NULL; |
173 |
hdl(F, F->read_data); |
174 |
|
175 |
if (F->flags.open == 0) |
176 |
continue; |
177 |
} |
178 |
} |
179 |
|
180 |
if ((epollop->events[i].events & (EPOLLOUT | EPOLLHUP | EPOLLERR))) |
181 |
{ |
182 |
if ((hdl = F->write_handler)) |
183 |
{ |
184 |
F->write_handler = NULL; |
185 |
hdl(F, F->write_data); |
186 |
|
187 |
if (F->flags.open == 0) |
188 |
continue; |
189 |
} |
190 |
} |
191 |
|
192 |
comm_setselect(F, 0, NULL, NULL, 0); |
193 |
} |
194 |
|
195 |
if (num == epollop->nevents && epollop->nevents < MAXIMUM_NEVENT) |
196 |
{ |
197 |
/* |
198 |
* We used all of the event space this time. We should be |
199 |
* ready for more events next time. |
200 |
*/ |
201 |
int new_nevents = epollop->nevents * 2; |
202 |
struct epoll_event *new_events = xrealloc(epollop->events, new_nevents * sizeof(*epollop->events)); |
203 |
|
204 |
epollop->events = new_events; |
205 |
epollop->nevents = new_nevents; |
206 |
} |
207 |
} |
208 |
#endif |