]>
Commit | Line | Data |
---|---|---|
6039b729 | 1 | /* |
bf95c10a | 2 | * Copyright (C) 1996-2022 The Squid Software Foundation and contributors |
6039b729 | 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. | |
6039b729 | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 05 Socket Functions */ |
10 | ||
6039b729 | 11 | /* |
12 | * The idea for this came from these two websites: | |
13 | * http://www.xmailserver.org/linux-patches/nio-improve.html | |
14 | * http://www.kegel.com/c10k.html | |
15 | * | |
16 | * This is to support the epoll sysctl being added to the linux 2.5 | |
17 | * kernel tree. The new sys_epoll is an event based poller without | |
18 | * most of the fuss of rtsignals. | |
19 | * | |
20 | * -- David Nicklay <dnicklay@web.turner.com> | |
21 | */ | |
22 | ||
23 | /* | |
24 | * XXX Currently not implemented / supported by this module XXX | |
25 | * | |
26 | * - delay pools | |
27 | * - deferred reads | |
28 | * | |
29 | */ | |
30 | ||
f7f3304a | 31 | #include "squid.h" |
d841c88d AJ |
32 | |
33 | #if USE_EPOLL | |
34 | ||
ccfbe8f4 | 35 | #include "base/CodeContext.h" |
d841c88d | 36 | #include "comm/Loops.h" |
1287b9e1 | 37 | #include "fde.h" |
582c2af2 | 38 | #include "globals.h" |
d841c88d | 39 | #include "mgr/Registration.h" |
e1656dc4 | 40 | #include "StatCounters.h" |
00a7574e | 41 | #include "StatHist.h" |
d841c88d | 42 | #include "Store.h" |
8a02a7f8 | 43 | |
6039b729 | 44 | #define DEBUG_EPOLL 0 |
45 | ||
1a30fdf5 | 46 | #include <cerrno> |
ad32c661 | 47 | #if HAVE_SYS_EPOLL_H |
6039b729 | 48 | #include <sys/epoll.h> |
ad32c661 | 49 | #endif |
6039b729 | 50 | |
612fc474 | 51 | static int kdpfd = -1; |
6039b729 | 52 | static int max_poll_time = 1000; |
53 | ||
54 | static struct epoll_event *pevents; | |
55 | ||
5acc9f37 | 56 | static void commEPollRegisterWithCacheManager(void); |
6039b729 | 57 | |
6039b729 | 58 | /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */ |
59 | /* Public functions */ | |
60 | ||
6039b729 | 61 | /* |
6039b729 | 62 | * This is a needed exported function which will be called to initialise |
63 | * the network loop code. | |
64 | */ | |
65 | void | |
d841c88d | 66 | Comm::SelectLoopInit(void) |
6039b729 | 67 | { |
6039b729 | 68 | pevents = (struct epoll_event *) xmalloc(SQUID_MAXFD * sizeof(struct epoll_event)); |
69 | ||
70 | if (!pevents) { | |
b69e9ffa AJ |
71 | int xerrno = errno; |
72 | fatalf("comm_select_init: xmalloc() failed: %s\n", xstrerr(xerrno)); | |
6039b729 | 73 | } |
74 | ||
75 | kdpfd = epoll_create(SQUID_MAXFD); | |
76 | ||
77 | if (kdpfd < 0) { | |
b69e9ffa AJ |
78 | int xerrno = errno; |
79 | fatalf("comm_select_init: epoll_create(): %s\n", xstrerr(xerrno)); | |
6039b729 | 80 | } |
8cb183ec FC |
81 | |
82 | commEPollRegisterWithCacheManager(); | |
6039b729 | 83 | } |
84 | ||
751406fe | 85 | static const char* epolltype_atoi(int x) |
86 | { | |
26ac0430 | 87 | switch (x) { |
751406fe | 88 | |
89 | case EPOLL_CTL_ADD: | |
90 | return "EPOLL_CTL_ADD"; | |
91 | ||
92 | case EPOLL_CTL_DEL: | |
93 | return "EPOLL_CTL_DEL"; | |
94 | ||
95 | case EPOLL_CTL_MOD: | |
96 | return "EPOLL_CTL_MOD"; | |
97 | ||
98 | default: | |
99 | return "UNKNOWN_EPOLLCTL_OP"; | |
100 | } | |
101 | } | |
102 | ||
d841c88d | 103 | /** |
6039b729 | 104 | * This is a needed exported function which will be called to register |
105 | * and deregister interest in a pending IO state for a given FD. | |
6039b729 | 106 | */ |
107 | void | |
d841c88d | 108 | Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout) |
6039b729 | 109 | { |
110 | fde *F = &fd_table[fd]; | |
751406fe | 111 | int epoll_ctl_type = 0; |
6039b729 | 112 | |
6039b729 | 113 | assert(fd >= 0); |
bf95c10a | 114 | debugs(5, 5, "FD " << fd << ", type=" << type << |
d841c88d AJ |
115 | ", handler=" << handler << ", client_data=" << client_data << |
116 | ", timeout=" << timeout); | |
6039b729 | 117 | |
612fc474 AJ |
118 | struct epoll_event ev; |
119 | memset(&ev, 0, sizeof(ev)); | |
751406fe | 120 | ev.data.fd = fd; |
6039b729 | 121 | |
a1727eb7 | 122 | if (!F->flags.open) { |
123 | epoll_ctl(kdpfd, EPOLL_CTL_DEL, fd, &ev); | |
124 | return; | |
125 | } | |
126 | ||
751406fe | 127 | // If read is an interest |
6039b729 | 128 | |
129 | if (type & COMM_SELECT_READ) { | |
0a080ce3 | 130 | if (handler) { |
26ac0430 AJ |
131 | // Hack to keep the events flowing if there is data immediately ready |
132 | if (F->flags.read_pending) | |
133 | ev.events |= EPOLLOUT; | |
751406fe | 134 | ev.events |= EPOLLIN; |
26ac0430 | 135 | } |
6039b729 | 136 | |
137 | F->read_handler = handler; | |
138 | ||
139 | F->read_data = client_data; | |
751406fe | 140 | |
141 | // Otherwise, use previously stored value | |
142 | } else if (F->epoll_state & EPOLLIN) { | |
143 | ev.events |= EPOLLIN; | |
6039b729 | 144 | } |
145 | ||
751406fe | 146 | // If write is an interest |
6039b729 | 147 | if (type & COMM_SELECT_WRITE) { |
751406fe | 148 | if (handler) |
149 | ev.events |= EPOLLOUT; | |
6039b729 | 150 | |
151 | F->write_handler = handler; | |
152 | ||
153 | F->write_data = client_data; | |
6039b729 | 154 | |
751406fe | 155 | // Otherwise, use previously stored value |
156 | } else if (F->epoll_state & EPOLLOUT) { | |
157 | ev.events |= EPOLLOUT; | |
158 | } | |
6039b729 | 159 | |
751406fe | 160 | if (ev.events) |
161 | ev.events |= EPOLLHUP | EPOLLERR; | |
6039b729 | 162 | |
751406fe | 163 | if (ev.events != F->epoll_state) { |
164 | if (F->epoll_state) // already monitoring something. | |
165 | epoll_ctl_type = ev.events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; | |
166 | else | |
167 | epoll_ctl_type = EPOLL_CTL_ADD; | |
6039b729 | 168 | |
751406fe | 169 | F->epoll_state = ev.events; |
6039b729 | 170 | |
751406fe | 171 | if (epoll_ctl(kdpfd, epoll_ctl_type, fd, &ev) < 0) { |
b69e9ffa | 172 | int xerrno = errno; |
d816f28d | 173 | debugs(5, DEBUG_EPOLL ? 0 : 8, "ERROR: epoll_ctl(," << epolltype_atoi(epoll_ctl_type) << |
b69e9ffa | 174 | ",,): failed on FD " << fd << ": " << xstrerr(xerrno)); |
6039b729 | 175 | } |
176 | } | |
177 | ||
178 | if (timeout) | |
179 | F->timeout = squid_curtime + timeout; | |
ccfbe8f4 AR |
180 | |
181 | if (timeout || handler) // all non-cleanup requests | |
182 | F->codeContext = CodeContext::Current(); // TODO: Avoid clearing if set? | |
183 | else if (!ev.events) // full cleanup: no more FD-associated work expected | |
184 | F->codeContext = nullptr; | |
185 | // else: direction-specific/timeout cleanup requests preserve F->codeContext | |
6039b729 | 186 | } |
187 | ||
610ee341 | 188 | static void commIncomingStats(StoreEntry * sentry); |
189 | ||
5acc9f37 | 190 | static void |
dbeed9ea | 191 | commEPollRegisterWithCacheManager(void) |
610ee341 | 192 | { |
8822ebee | 193 | Mgr::RegisterAction("comm_epoll_incoming", |
d9fc6862 A |
194 | "comm_incoming() stats", |
195 | commIncomingStats, 0, 1); | |
610ee341 | 196 | } |
197 | ||
198 | static void | |
199 | commIncomingStats(StoreEntry * sentry) | |
200 | { | |
201 | StatCounters *f = &statCounter; | |
0a515876 | 202 | storeAppendPrintf(sentry, "Total number of epoll(2) loops: %ld\n", statCounter.select_loops); |
610ee341 | 203 | storeAppendPrintf(sentry, "Histogram of returned filedescriptors\n"); |
96886986 | 204 | f->select_fds_hist.dump(sentry, statHistIntDumper); |
610ee341 | 205 | } |
206 | ||
d841c88d | 207 | /** |
6039b729 | 208 | * Check all connections for new connections and input data that is to be |
209 | * processed. Also check for connections with data queued and whether we can | |
210 | * write it out. | |
6039b729 | 211 | * |
212 | * Called to do the new-style IO, courtesy of of squid (like most of this | |
213 | * new IO code). This routine handles the stuff we've hidden in | |
214 | * comm_setselect and fd_table[] and calls callbacks for IO ready | |
215 | * events. | |
216 | */ | |
c8407295 | 217 | Comm::Flag |
d841c88d | 218 | Comm::DoSelect(int msec) |
6039b729 | 219 | { |
220 | int num, i,fd; | |
221 | fde *F; | |
222 | PF *hdl; | |
223 | ||
224 | struct epoll_event *cevents; | |
6039b729 | 225 | |
226 | if (msec > max_poll_time) | |
227 | msec = max_poll_time; | |
228 | ||
229 | for (;;) { | |
230 | num = epoll_wait(kdpfd, pevents, SQUID_MAXFD, msec); | |
098346fd | 231 | ++ statCounter.select_loops; |
6039b729 | 232 | |
233 | if (num >= 0) | |
234 | break; | |
235 | ||
236 | if (ignoreErrno(errno)) | |
237 | break; | |
238 | ||
239 | getCurrentTime(); | |
240 | ||
4ee57cbe | 241 | return Comm::COMM_ERROR; |
6039b729 | 242 | } |
243 | ||
244 | getCurrentTime(); | |
245 | ||
f30f7998 | 246 | statCounter.select_fds_hist.count(num); |
751406fe | 247 | |
6039b729 | 248 | if (num == 0) |
f53969cc | 249 | return Comm::TIMEOUT; /* No error.. */ |
751406fe | 250 | |
cbebe602 | 251 | for (i = 0, cevents = pevents; i < num; ++i, ++cevents) { |
6039b729 | 252 | fd = cevents->data.fd; |
253 | F = &fd_table[fd]; | |
ccfbe8f4 | 254 | CodeContext::Reset(F->codeContext); |
bf95c10a | 255 | debugs(5, DEBUG_EPOLL ? 0 : 8, "got FD " << fd << " events=" << |
26ac0430 AJ |
256 | std::hex << cevents->events << " monitoring=" << F->epoll_state << |
257 | " F->read_handler=" << F->read_handler << " F->write_handler=" << F->write_handler); | |
751406fe | 258 | |
259 | // TODO: add EPOLLPRI?? | |
6039b729 | 260 | |
0a080ce3 | 261 | if (cevents->events & (EPOLLIN|EPOLLHUP|EPOLLERR) || F->flags.read_pending) { |
aee3523a | 262 | if ((hdl = F->read_handler) != nullptr) { |
bf95c10a | 263 | debugs(5, DEBUG_EPOLL ? 0 : 8, "Calling read handler on FD " << fd); |
aee3523a | 264 | F->read_handler = nullptr; |
6039b729 | 265 | hdl(fd, F->read_data); |
098346fd | 266 | ++ statCounter.select_fds; |
751406fe | 267 | } else { |
bf95c10a | 268 | debugs(5, DEBUG_EPOLL ? 0 : 8, "no read handler for FD " << fd); |
751406fe | 269 | // remove interest since no handler exist for this event. |
aee3523a | 270 | SetSelect(fd, COMM_SELECT_READ, nullptr, nullptr, 0); |
6039b729 | 271 | } |
272 | } | |
273 | ||
751406fe | 274 | if (cevents->events & (EPOLLOUT|EPOLLHUP|EPOLLERR)) { |
aee3523a | 275 | if ((hdl = F->write_handler) != nullptr) { |
bf95c10a | 276 | debugs(5, DEBUG_EPOLL ? 0 : 8, "Calling write handler on FD " << fd); |
aee3523a | 277 | F->write_handler = nullptr; |
6039b729 | 278 | hdl(fd, F->write_data); |
098346fd | 279 | ++ statCounter.select_fds; |
751406fe | 280 | } else { |
bf95c10a | 281 | debugs(5, DEBUG_EPOLL ? 0 : 8, "no write handler for FD " << fd); |
751406fe | 282 | // remove interest since no handler exist for this event. |
aee3523a | 283 | SetSelect(fd, COMM_SELECT_WRITE, nullptr, nullptr, 0); |
6039b729 | 284 | } |
285 | } | |
286 | } | |
287 | ||
ccfbe8f4 AR |
288 | CodeContext::Reset(); |
289 | ||
c8407295 | 290 | return Comm::OK; |
6039b729 | 291 | } |
292 | ||
293 | void | |
d841c88d | 294 | Comm::QuickPollRequired(void) |
6039b729 | 295 | { |
768d3d2f | 296 | max_poll_time = 10; |
6039b729 | 297 | } |
298 | ||
8a02a7f8 | 299 | #endif /* USE_EPOLL */ |
f53969cc | 300 |