]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ModKqueue.cc
Merged from trunk (r13515).
[thirdparty/squid.git] / src / comm / ModKqueue.cc
1 /*
2 * DEBUG: section 05 Socket Functions
3 *
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
6 *
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
29 *
30 */
31
32 /*
33 * This code was originally written by Benno Rice and hacked on quite
34 * a bit by Adrian. Adrian then took it to the hybrid-ircd project to use
35 * in their new IO subsystem. After a year of modifications and some
36 * rather interesting changes (event aggregation) its back in squid.
37 * Thanks to the ircd-hybrid guys.
38 */
39
40 /*
41 * XXX Currently not implemented / supported by this module XXX
42 *
43 * - delay pools
44 * - deferred reads
45 * - flags.read_pending
46 *
47 * So, its not entirely useful in a production setup since if a read
48 * is meant to be deferred it isn't (we're not even throwing the event
49 * away here). Eventually the rest of the code will be rewritten
50 * so deferred reads aren't required.
51 * -- adrian
52 */
53 #include "squid.h"
54
55 #if USE_KQUEUE
56 #include "comm/Loops.h"
57 #include "fde.h"
58 #include "globals.h"
59 #include "SquidTime.h"
60 #include "StatCounters.h"
61 #include "Store.h"
62
63 #include <cerrno>
64 #if HAVE_SYS_EVENT_H
65 #include <sys/event.h>
66 #endif
67
68 #define KE_LENGTH 128
69
70 /* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */
71
72 #ifndef EV_SET
73 #define EV_SET(kevp, a, b, c, d, e, f) do { \
74 (kevp)->ident = (a); \
75 (kevp)->filter = (b); \
76 (kevp)->flags = (c); \
77 (kevp)->fflags = (d); \
78 (kevp)->data = (e); \
79 (kevp)->udata = (f); \
80 } while(0)
81 #endif
82
83 static void kq_update_events(int, short, PF *);
84 static int kq;
85
86 static struct timespec zero_timespec;
87
88 static struct kevent *kqlst; /* kevent buffer */
89 static int kqmax; /* max structs to buffer */
90 static int kqoff; /* offset into the buffer */
91 static int max_poll_time = 1000;
92
93 static void commKQueueRegisterWithCacheManager(void);
94
95 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
96 /* Private functions */
97
98 void
99 kq_update_events(int fd, short filter, PF * handler)
100 {
101 PF *cur_handler;
102 int kep_flags;
103
104 switch (filter) {
105
106 case EVFILT_READ:
107 cur_handler = fd_table[fd].read_handler;
108 break;
109
110 case EVFILT_WRITE:
111 cur_handler = fd_table[fd].write_handler;
112 break;
113
114 default:
115 /* XXX bad! -- adrian */
116 return;
117 break;
118 }
119
120 if ((cur_handler == NULL && handler != NULL)
121 || (cur_handler != NULL && handler == NULL)) {
122
123 struct kevent *kep;
124
125 kep = kqlst + kqoff;
126
127 if (handler != NULL) {
128 kep_flags = (EV_ADD | EV_ONESHOT);
129 } else {
130 kep_flags = EV_DELETE;
131 }
132
133 EV_SET(kep, (uintptr_t) fd, filter, kep_flags, 0, 0, 0);
134
135 /* Check if we've used the last one. If we have then submit them all */
136 if (kqoff == kqmax - 1) {
137 int ret;
138
139 ret = kevent(kq, kqlst, kqmax, NULL, 0, &zero_timespec);
140 /* jdc -- someone needs to do error checking... */
141
142 if (ret == -1) {
143 perror("kq_update_events(): kevent()");
144 return;
145 }
146
147 kqoff = 0;
148 } else {
149 ++kqoff;
150 }
151 }
152 }
153
154 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
155 /* Public functions */
156
157 /*
158 * comm_select_init
159 *
160 * This is a needed exported function which will be called to initialise
161 * the network loop code.
162 */
163 void
164 Comm::SelectLoopInit(void)
165 {
166 kq = kqueue();
167
168 if (kq < 0) {
169 fatal("comm_select_init: Couldn't open kqueue fd!\n");
170 }
171
172 kqmax = getdtablesize();
173
174 kqlst = (struct kevent *)xmalloc(sizeof(*kqlst) * kqmax);
175 zero_timespec.tv_sec = 0;
176 zero_timespec.tv_nsec = 0;
177
178 commKQueueRegisterWithCacheManager();
179 }
180
181 /*
182 * comm_setselect
183 *
184 * This is a needed exported function which will be called to register
185 * and deregister interest in a pending IO state for a given FD.
186 */
187 void
188 Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout)
189 {
190 fde *F = &fd_table[fd];
191 assert(fd >= 0);
192 assert(F->flags.open);
193 debugs(5, 5, HERE << "FD " << fd << ", type=" << type <<
194 ", handler=" << handler << ", client_data=" << client_data <<
195 ", timeout=" << timeout);
196
197 if (type & COMM_SELECT_READ) {
198 if (F->flags.read_pending)
199 kq_update_events(fd, EVFILT_WRITE, handler);
200
201 kq_update_events(fd, EVFILT_READ, handler);
202
203 F->read_handler = handler;
204 F->read_data = client_data;
205 }
206
207 if (type & COMM_SELECT_WRITE) {
208 kq_update_events(fd, EVFILT_WRITE, handler);
209 F->write_handler = handler;
210 F->write_data = client_data;
211 }
212
213 if (timeout)
214 F->timeout = squid_curtime + timeout;
215
216 }
217
218 void
219 Comm::ResetSelect(int fd)
220 {
221 fde *F = &fd_table[fd];
222 if (F->read_handler) {
223 kq_update_events(fd, EVFILT_READ, (PF *)1);
224 }
225 if (F->write_handler) {
226 kq_update_events(fd, EVFILT_WRITE, (PF *)1);
227 }
228 }
229
230 /*
231 * Check all connections for new connections and input data that is to be
232 * processed. Also check for connections with data queued and whether we can
233 * write it out.
234 */
235
236 /*
237 * comm_select
238 *
239 * Called to do the new-style IO, courtesy of of squid (like most of this
240 * new IO code). This routine handles the stuff we've hidden in
241 * comm_setselect and fd_table[] and calls callbacks for IO ready
242 * events.
243 */
244
245 Comm::Flag
246 Comm::DoSelect(int msec)
247 {
248 int num, i;
249
250 static struct kevent ke[KE_LENGTH];
251
252 struct timespec poll_time;
253
254 if (msec > max_poll_time)
255 msec = max_poll_time;
256
257 poll_time.tv_sec = msec / 1000;
258
259 poll_time.tv_nsec = (msec % 1000) * 1000000;
260
261 for (;;) {
262 num = kevent(kq, kqlst, kqoff, ke, KE_LENGTH, &poll_time);
263 ++statCounter.select_loops;
264 kqoff = 0;
265
266 if (num >= 0)
267 break;
268
269 if (ignoreErrno(errno))
270 break;
271
272 getCurrentTime();
273
274 return Comm::COMM_ERROR;
275
276 /* NOTREACHED */
277 }
278
279 getCurrentTime();
280
281 if (num == 0)
282 return Comm::OK; /* No error.. */
283
284 for (i = 0; i < num; ++i) {
285 int fd = (int) ke[i].ident;
286 PF *hdl = NULL;
287 fde *F = &fd_table[fd];
288
289 if (ke[i].flags & EV_ERROR) {
290 errno = ke[i].data;
291 /* XXX error == bad! -- adrian */
292 continue; /* XXX! */
293 }
294
295 if (ke[i].filter == EVFILT_READ || F->flags.read_pending) {
296 if ((hdl = F->read_handler) != NULL) {
297 F->read_handler = NULL;
298 F->flags.read_pending = 0;
299 hdl(fd, F->read_data);
300 }
301 }
302
303 if (ke[i].filter == EVFILT_WRITE) {
304 if ((hdl = F->write_handler) != NULL) {
305 F->write_handler = NULL;
306 hdl(fd, F->write_data);
307 }
308 }
309
310 if (ke[i].filter != EVFILT_WRITE && ke[i].filter != EVFILT_READ) {
311 /* Bad! -- adrian */
312 debugs(5, DBG_IMPORTANT, "comm_select: kevent returned " << ke[i].filter << "!");
313 }
314 }
315
316 return Comm::OK;
317 }
318
319 void
320 Comm::QuickPollRequired(void)
321 {
322 max_poll_time = 10;
323 }
324
325 static void
326 commKQueueRegisterWithCacheManager(void)
327 {
328 }
329
330 #endif /* USE_KQUEUE */