]>
Commit | Line | Data |
---|---|---|
92b9f1fd | 1 | /* |
bbc27441 | 2 | * Copyright (C) 1996-2014 The Squid Software Foundation and contributors |
92b9f1fd | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
92b9f1fd | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 05 Socket Functions */ |
10 | ||
92b9f1fd | 11 | /* |
12 | * This code was originally written by Benno Rice and hacked on quite | |
13 | * a bit by Adrian. Adrian then took it to the hybrid-ircd project to use | |
14 | * in their new IO subsystem. After a year of modifications and some | |
15 | * rather interesting changes (event aggregation) its back in squid. | |
16 | * Thanks to the ircd-hybrid guys. | |
17 | */ | |
18 | ||
19 | /* | |
20 | * XXX Currently not implemented / supported by this module XXX | |
21 | * | |
22 | * - delay pools | |
23 | * - deferred reads | |
0a080ce3 | 24 | * - flags.read_pending |
92b9f1fd | 25 | * |
26 | * So, its not entirely useful in a production setup since if a read | |
27 | * is meant to be deferred it isn't (we're not even throwing the event | |
28 | * away here). Eventually the rest of the code will be rewritten | |
29 | * so deferred reads aren't required. | |
30 | * -- adrian | |
31 | */ | |
f7f3304a | 32 | #include "squid.h" |
11f11b5c AJ |
33 | |
34 | #if USE_KQUEUE | |
d841c88d | 35 | #include "comm/Loops.h" |
f0d40406 | 36 | #include "fde.h" |
88e446ed | 37 | #include "globals.h" |
a4452e04 | 38 | #include "SquidTime.h" |
5d203d9b | 39 | #include "StatCounters.h" |
582c2af2 | 40 | #include "Store.h" |
92b9f1fd | 41 | |
1a30fdf5 | 42 | #include <cerrno> |
11f11b5c | 43 | #if HAVE_SYS_EVENT_H |
92b9f1fd | 44 | #include <sys/event.h> |
11f11b5c | 45 | #endif |
92b9f1fd | 46 | |
71b7abe0 | 47 | #define KE_LENGTH 128 |
92b9f1fd | 48 | |
49 | /* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */ | |
50 | ||
51 | #ifndef EV_SET | |
52 | #define EV_SET(kevp, a, b, c, d, e, f) do { \ | |
53 | (kevp)->ident = (a); \ | |
54 | (kevp)->filter = (b); \ | |
55 | (kevp)->flags = (c); \ | |
56 | (kevp)->fflags = (d); \ | |
57 | (kevp)->data = (e); \ | |
58 | (kevp)->udata = (f); \ | |
59 | } while(0) | |
60 | #endif | |
61 | ||
62 | static void kq_update_events(int, short, PF *); | |
63 | static int kq; | |
62e76326 | 64 | |
92b9f1fd | 65 | static struct timespec zero_timespec; |
66 | ||
71b7abe0 | 67 | static struct kevent *kqlst; /* kevent buffer */ |
68 | static int kqmax; /* max structs to buffer */ | |
69 | static int kqoff; /* offset into the buffer */ | |
50198132 | 70 | static int max_poll_time = 1000; |
92b9f1fd | 71 | |
5acc9f37 | 72 | static void commKQueueRegisterWithCacheManager(void); |
92b9f1fd | 73 | |
74 | /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */ | |
75 | /* Private functions */ | |
76 | ||
77 | void | |
78 | kq_update_events(int fd, short filter, PF * handler) | |
79 | { | |
80 | PF *cur_handler; | |
81 | int kep_flags; | |
82 | ||
92b9f1fd | 83 | switch (filter) { |
62e76326 | 84 | |
92b9f1fd | 85 | case EVFILT_READ: |
71b7abe0 | 86 | cur_handler = fd_table[fd].read_handler; |
87 | break; | |
62e76326 | 88 | |
92b9f1fd | 89 | case EVFILT_WRITE: |
71b7abe0 | 90 | cur_handler = fd_table[fd].write_handler; |
91 | break; | |
62e76326 | 92 | |
92b9f1fd | 93 | default: |
71b7abe0 | 94 | /* XXX bad! -- adrian */ |
95 | return; | |
96 | break; | |
92b9f1fd | 97 | } |
98 | ||
99 | if ((cur_handler == NULL && handler != NULL) | |
62e76326 | 100 | || (cur_handler != NULL && handler == NULL)) { |
101 | ||
71b7abe0 | 102 | struct kevent *kep; |
103 | ||
104 | kep = kqlst + kqoff; | |
105 | ||
106 | if (handler != NULL) { | |
107 | kep_flags = (EV_ADD | EV_ONESHOT); | |
108 | } else { | |
109 | kep_flags = EV_DELETE; | |
110 | } | |
111 | ||
112 | EV_SET(kep, (uintptr_t) fd, filter, kep_flags, 0, 0, 0); | |
113 | ||
26ac0430 | 114 | /* Check if we've used the last one. If we have then submit them all */ |
09729b90 | 115 | if (kqoff == kqmax - 1) { |
71b7abe0 | 116 | int ret; |
117 | ||
09729b90 | 118 | ret = kevent(kq, kqlst, kqmax, NULL, 0, &zero_timespec); |
71b7abe0 | 119 | /* jdc -- someone needs to do error checking... */ |
62e76326 | 120 | |
71b7abe0 | 121 | if (ret == -1) { |
122 | perror("kq_update_events(): kevent()"); | |
123 | return; | |
124 | } | |
62e76326 | 125 | |
71b7abe0 | 126 | kqoff = 0; |
127 | } else { | |
cbebe602 | 128 | ++kqoff; |
71b7abe0 | 129 | } |
92b9f1fd | 130 | } |
131 | } | |
132 | ||
92b9f1fd | 133 | /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */ |
134 | /* Public functions */ | |
135 | ||
92b9f1fd | 136 | /* |
137 | * comm_select_init | |
138 | * | |
139 | * This is a needed exported function which will be called to initialise | |
140 | * the network loop code. | |
141 | */ | |
142 | void | |
d841c88d | 143 | Comm::SelectLoopInit(void) |
92b9f1fd | 144 | { |
145 | kq = kqueue(); | |
62e76326 | 146 | |
92b9f1fd | 147 | if (kq < 0) { |
71b7abe0 | 148 | fatal("comm_select_init: Couldn't open kqueue fd!\n"); |
92b9f1fd | 149 | } |
62e76326 | 150 | |
92b9f1fd | 151 | kqmax = getdtablesize(); |
62e76326 | 152 | |
50198132 | 153 | kqlst = (struct kevent *)xmalloc(sizeof(*kqlst) * kqmax); |
92b9f1fd | 154 | zero_timespec.tv_sec = 0; |
155 | zero_timespec.tv_nsec = 0; | |
da9b2c49 FC |
156 | |
157 | commKQueueRegisterWithCacheManager(); | |
92b9f1fd | 158 | } |
159 | ||
160 | /* | |
161 | * comm_setselect | |
162 | * | |
163 | * This is a needed exported function which will be called to register | |
164 | * and deregister interest in a pending IO state for a given FD. | |
165 | */ | |
166 | void | |
d841c88d | 167 | Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout) |
92b9f1fd | 168 | { |
169 | fde *F = &fd_table[fd]; | |
170 | assert(fd >= 0); | |
171 | assert(F->flags.open); | |
48e7baac AJ |
172 | debugs(5, 5, HERE << "FD " << fd << ", type=" << type << |
173 | ", handler=" << handler << ", client_data=" << client_data << | |
174 | ", timeout=" << timeout); | |
92b9f1fd | 175 | |
176 | if (type & COMM_SELECT_READ) { | |
5f84968c MF |
177 | if (F->flags.read_pending) |
178 | kq_update_events(fd, EVFILT_WRITE, handler); | |
326c5c41 | 179 | |
71b7abe0 | 180 | kq_update_events(fd, EVFILT_READ, handler); |
326c5c41 | 181 | |
71b7abe0 | 182 | F->read_handler = handler; |
183 | F->read_data = client_data; | |
92b9f1fd | 184 | } |
62e76326 | 185 | |
92b9f1fd | 186 | if (type & COMM_SELECT_WRITE) { |
71b7abe0 | 187 | kq_update_events(fd, EVFILT_WRITE, handler); |
188 | F->write_handler = handler; | |
189 | F->write_data = client_data; | |
92b9f1fd | 190 | } |
62e76326 | 191 | |
92b9f1fd | 192 | if (timeout) |
71b7abe0 | 193 | F->timeout = squid_curtime + timeout; |
92b9f1fd | 194 | |
195 | } | |
196 | ||
3a5a4930 | 197 | void |
d841c88d | 198 | Comm::ResetSelect(int fd) |
3a5a4930 | 199 | { |
200 | fde *F = &fd_table[fd]; | |
201 | if (F->read_handler) { | |
26ac0430 | 202 | kq_update_events(fd, EVFILT_READ, (PF *)1); |
3a5a4930 | 203 | } |
204 | if (F->write_handler) { | |
26ac0430 | 205 | kq_update_events(fd, EVFILT_WRITE, (PF *)1); |
3a5a4930 | 206 | } |
207 | } | |
208 | ||
92b9f1fd | 209 | /* |
210 | * Check all connections for new connections and input data that is to be | |
211 | * processed. Also check for connections with data queued and whether we can | |
212 | * write it out. | |
213 | */ | |
214 | ||
215 | /* | |
216 | * comm_select | |
217 | * | |
218 | * Called to do the new-style IO, courtesy of of squid (like most of this | |
219 | * new IO code). This routine handles the stuff we've hidden in | |
220 | * comm_setselect and fd_table[] and calls callbacks for IO ready | |
221 | * events. | |
222 | */ | |
223 | ||
c8407295 | 224 | Comm::Flag |
e70d24f3 | 225 | Comm::DoSelect(int msec) |
92b9f1fd | 226 | { |
227 | int num, i; | |
62e76326 | 228 | |
92b9f1fd | 229 | static struct kevent ke[KE_LENGTH]; |
62e76326 | 230 | |
92b9f1fd | 231 | struct timespec poll_time; |
232 | ||
50198132 | 233 | if (msec > max_poll_time) |
234 | msec = max_poll_time; | |
62e76326 | 235 | |
3de50fcd | 236 | poll_time.tv_sec = msec / 1000; |
62e76326 | 237 | |
3de50fcd | 238 | poll_time.tv_nsec = (msec % 1000) * 1000000; |
62e76326 | 239 | |
3de50fcd | 240 | for (;;) { |
241 | num = kevent(kq, kqlst, kqoff, ke, KE_LENGTH, &poll_time); | |
0a515876 | 242 | ++statCounter.select_loops; |
3de50fcd | 243 | kqoff = 0; |
62e76326 | 244 | |
3de50fcd | 245 | if (num >= 0) |
246 | break; | |
62e76326 | 247 | |
3de50fcd | 248 | if (ignoreErrno(errno)) |
249 | break; | |
62e76326 | 250 | |
71b7abe0 | 251 | getCurrentTime(); |
62e76326 | 252 | |
4ee57cbe | 253 | return Comm::COMM_ERROR; |
62e76326 | 254 | |
3de50fcd | 255 | /* NOTREACHED */ |
256 | } | |
257 | ||
258 | getCurrentTime(); | |
62e76326 | 259 | |
3de50fcd | 260 | if (num == 0) |
f53969cc | 261 | return Comm::OK; /* No error.. */ |
3de50fcd | 262 | |
cbebe602 | 263 | for (i = 0; i < num; ++i) { |
3de50fcd | 264 | int fd = (int) ke[i].ident; |
265 | PF *hdl = NULL; | |
266 | fde *F = &fd_table[fd]; | |
267 | ||
268 | if (ke[i].flags & EV_ERROR) { | |
269 | errno = ke[i].data; | |
270 | /* XXX error == bad! -- adrian */ | |
271 | continue; /* XXX! */ | |
272 | } | |
62e76326 | 273 | |
5f84968c | 274 | if (ke[i].filter == EVFILT_READ || F->flags.read_pending) { |
62e76326 | 275 | if ((hdl = F->read_handler) != NULL) { |
276 | F->read_handler = NULL; | |
26ac0430 | 277 | F->flags.read_pending = 0; |
62e76326 | 278 | hdl(fd, F->read_data); |
279 | } | |
5f84968c | 280 | } |
62e76326 | 281 | |
5f84968c | 282 | if (ke[i].filter == EVFILT_WRITE) { |
62e76326 | 283 | if ((hdl = F->write_handler) != NULL) { |
284 | F->write_handler = NULL; | |
285 | hdl(fd, F->write_data); | |
286 | } | |
5f84968c | 287 | } |
62e76326 | 288 | |
5f84968c | 289 | if (ke[i].filter != EVFILT_WRITE && ke[i].filter != EVFILT_READ) { |
62e76326 | 290 | /* Bad! -- adrian */ |
e0236918 | 291 | debugs(5, DBG_IMPORTANT, "comm_select: kevent returned " << ke[i].filter << "!"); |
71b7abe0 | 292 | } |
92b9f1fd | 293 | } |
62e76326 | 294 | |
c8407295 | 295 | return Comm::OK; |
92b9f1fd | 296 | } |
297 | ||
50198132 | 298 | void |
d841c88d | 299 | Comm::QuickPollRequired(void) |
50198132 | 300 | { |
768d3d2f | 301 | max_poll_time = 10; |
50198132 | 302 | } |
303 | ||
5acc9f37 | 304 | static void |
706744c3 | 305 | commKQueueRegisterWithCacheManager(void) |
d9088c69 | 306 | { |
307 | } | |
308 | ||
92b9f1fd | 309 | #endif /* USE_KQUEUE */ |
f53969cc | 310 |