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