]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ModKqueue.cc
2 * DEBUG: section 05 Socket Functions
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
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.
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.
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.
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.
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.
41 * XXX Currently not implemented / supported by this module XXX
45 * - flags.read_pending
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.
56 #include "comm/Loops.h"
58 #include "SquidTime.h"
59 #include "StatCounters.h"
63 #include <sys/event.h>
71 /* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */
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); \
80 (kevp)->udata = (f); \
84 static void kq_update_events(int, short, PF
*);
87 static struct timespec zero_timespec
;
89 static struct kevent
*kqlst
; /* kevent buffer */
90 static int kqmax
; /* max structs to buffer */
91 static int kqoff
; /* offset into the buffer */
92 static int max_poll_time
= 1000;
94 static void commKQueueRegisterWithCacheManager(void);
96 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
97 /* Private functions */
100 kq_update_events(int fd
, short filter
, PF
* handler
)
108 cur_handler
= fd_table
[fd
].read_handler
;
112 cur_handler
= fd_table
[fd
].write_handler
;
116 /* XXX bad! -- adrian */
121 if ((cur_handler
== NULL
&& handler
!= NULL
)
122 || (cur_handler
!= NULL
&& handler
== NULL
)) {
128 if (handler
!= NULL
) {
129 kep_flags
= (EV_ADD
| EV_ONESHOT
);
131 kep_flags
= EV_DELETE
;
134 EV_SET(kep
, (uintptr_t) fd
, filter
, kep_flags
, 0, 0, 0);
136 /* Check if we've used the last one. If we have then submit them all */
137 if (kqoff
== kqmax
- 1) {
140 ret
= kevent(kq
, kqlst
, kqmax
, NULL
, 0, &zero_timespec
);
141 /* jdc -- someone needs to do error checking... */
144 perror("kq_update_events(): kevent()");
155 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
156 /* Public functions */
161 * This is a needed exported function which will be called to initialise
162 * the network loop code.
165 Comm::SelectLoopInit(void)
170 fatal("comm_select_init: Couldn't open kqueue fd!\n");
173 kqmax
= getdtablesize();
175 kqlst
= (struct kevent
*)xmalloc(sizeof(*kqlst
) * kqmax
);
176 zero_timespec
.tv_sec
= 0;
177 zero_timespec
.tv_nsec
= 0;
179 commKQueueRegisterWithCacheManager();
185 * This is a needed exported function which will be called to register
186 * and deregister interest in a pending IO state for a given FD.
189 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
191 fde
*F
= &fd_table
[fd
];
193 assert(F
->flags
.open
);
194 debugs(5, 5, HERE
<< "FD " << fd
<< ", type=" << type
<<
195 ", handler=" << handler
<< ", client_data=" << client_data
<<
196 ", timeout=" << timeout
);
198 if (type
& COMM_SELECT_READ
) {
199 kq_update_events(fd
, EVFILT_READ
, handler
);
200 F
->read_handler
= handler
;
201 F
->read_data
= client_data
;
204 if (type
& COMM_SELECT_WRITE
) {
205 kq_update_events(fd
, EVFILT_WRITE
, handler
);
206 F
->write_handler
= handler
;
207 F
->write_data
= client_data
;
211 F
->timeout
= squid_curtime
+ timeout
;
216 Comm::ResetSelect(int fd
)
218 fde
*F
= &fd_table
[fd
];
219 if (F
->read_handler
) {
220 kq_update_events(fd
, EVFILT_READ
, (PF
*)1);
222 if (F
->write_handler
) {
223 kq_update_events(fd
, EVFILT_WRITE
, (PF
*)1);
228 * Check all connections for new connections and input data that is to be
229 * processed. Also check for connections with data queued and whether we can
236 * Called to do the new-style IO, courtesy of of squid (like most of this
237 * new IO code). This routine handles the stuff we've hidden in
238 * comm_setselect and fd_table[] and calls callbacks for IO ready
243 Comm::DoSelect(int msec
)
247 static struct kevent ke
[KE_LENGTH
];
249 struct timespec poll_time
;
251 if (msec
> max_poll_time
)
252 msec
= max_poll_time
;
254 poll_time
.tv_sec
= msec
/ 1000;
256 poll_time
.tv_nsec
= (msec
% 1000) * 1000000;
259 num
= kevent(kq
, kqlst
, kqoff
, ke
, KE_LENGTH
, &poll_time
);
260 ++statCounter
.select_loops
;
266 if (ignoreErrno(errno
))
279 return COMM_OK
; /* No error.. */
281 for (i
= 0; i
< num
; ++i
) {
282 int fd
= (int) ke
[i
].ident
;
284 fde
*F
= &fd_table
[fd
];
286 if (ke
[i
].flags
& EV_ERROR
) {
288 /* XXX error == bad! -- adrian */
292 switch (ke
[i
].filter
) {
296 if ((hdl
= F
->read_handler
) != NULL
) {
297 F
->read_handler
= NULL
;
298 F
->flags
.read_pending
= 0;
299 hdl(fd
, F
->read_data
);
306 if ((hdl
= F
->write_handler
) != NULL
) {
307 F
->write_handler
= NULL
;
308 hdl(fd
, F
->write_data
);
315 debugs(5, DBG_IMPORTANT
, "comm_select: kevent returned " << ke
[i
].filter
<< "!");
324 Comm::QuickPollRequired(void)
330 commKQueueRegisterWithCacheManager(void)
334 #endif /* USE_KQUEUE */