Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 2 | * Listener management functions. |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 13 | #include <errno.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include <common/config.h> |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 18 | #include <common/errors.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 19 | #include <common/mini-clist.h> |
| 20 | #include <common/standard.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 21 | #include <common/time.h> |
| 22 | |
| 23 | #include <types/global.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 24 | #include <types/protocol.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 26 | #include <proto/acl.h> |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 27 | #include <proto/fd.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 28 | #include <proto/freq_ctr.h> |
| 29 | #include <proto/log.h> |
| 30 | #include <proto/task.h> |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 31 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 32 | /* This function adds the specified listener's file descriptor to the polling |
| 33 | * lists if it is in the LI_LISTEN state. The listener enters LI_READY or |
| 34 | * LI_FULL state depending on its number of connections. |
| 35 | */ |
| 36 | void enable_listener(struct listener *listener) |
| 37 | { |
| 38 | if (listener->state == LI_LISTEN) { |
| 39 | if (listener->nbconn < listener->maxconn) { |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 40 | fd_want_recv(listener->fd); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 41 | listener->state = LI_READY; |
| 42 | } else { |
| 43 | listener->state = LI_FULL; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /* This function removes the specified listener's file descriptor from the |
| 49 | * polling lists if it is in the LI_READY or in the LI_FULL state. The listener |
| 50 | * enters LI_LISTEN. |
| 51 | */ |
| 52 | void disable_listener(struct listener *listener) |
| 53 | { |
| 54 | if (listener->state < LI_READY) |
| 55 | return; |
| 56 | if (listener->state == LI_READY) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 57 | fd_stop_recv(listener->fd); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 58 | if (listener->state == LI_LIMITED) |
| 59 | LIST_DEL(&listener->wait_queue); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 60 | listener->state = LI_LISTEN; |
| 61 | } |
| 62 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 63 | /* This function tries to temporarily disable a listener, depending on the OS |
| 64 | * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores |
| 65 | * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but |
| 66 | * closes upon SHUT_WR and refuses to rebind. So a common validation path |
| 67 | * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling |
| 68 | * is disabled. It normally returns non-zero, unless an error is reported. |
| 69 | */ |
| 70 | int pause_listener(struct listener *l) |
| 71 | { |
| 72 | if (l->state <= LI_PAUSED) |
| 73 | return 1; |
| 74 | |
| 75 | if (shutdown(l->fd, SHUT_WR) != 0) |
| 76 | return 0; /* Solaris dies here */ |
| 77 | |
| 78 | if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) |
| 79 | return 0; /* OpenBSD dies here */ |
| 80 | |
| 81 | if (shutdown(l->fd, SHUT_RD) != 0) |
| 82 | return 0; /* should always be OK */ |
| 83 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 84 | if (l->state == LI_LIMITED) |
| 85 | LIST_DEL(&l->wait_queue); |
| 86 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 87 | fd_stop_recv(l->fd); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 88 | l->state = LI_PAUSED; |
| 89 | return 1; |
| 90 | } |
| 91 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 92 | /* This function tries to resume a temporarily disabled listener. Paused, full, |
| 93 | * limited and disabled listeners are handled, which means that this function |
| 94 | * may replace enable_listener(). The resulting state will either be LI_READY |
| 95 | * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket). |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 96 | */ |
| 97 | int resume_listener(struct listener *l) |
| 98 | { |
| 99 | if (l->state < LI_PAUSED) |
| 100 | return 0; |
| 101 | |
| 102 | if (l->state == LI_PAUSED && |
| 103 | listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) |
| 104 | return 0; |
| 105 | |
| 106 | if (l->state == LI_READY) |
| 107 | return 1; |
| 108 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 109 | if (l->state == LI_LIMITED) |
| 110 | LIST_DEL(&l->wait_queue); |
| 111 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 112 | if (l->nbconn >= l->maxconn) { |
| 113 | l->state = LI_FULL; |
| 114 | return 1; |
| 115 | } |
| 116 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 117 | fd_want_recv(l->fd); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 118 | l->state = LI_READY; |
| 119 | return 1; |
| 120 | } |
| 121 | |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 122 | /* Marks a ready listener as full so that the session code tries to re-enable |
| 123 | * it upon next close() using resume_listener(). |
| 124 | */ |
| 125 | void listener_full(struct listener *l) |
| 126 | { |
| 127 | if (l->state >= LI_READY) { |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 128 | if (l->state == LI_LIMITED) |
| 129 | LIST_DEL(&l->wait_queue); |
| 130 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 131 | fd_stop_recv(l->fd); |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 132 | l->state = LI_FULL; |
| 133 | } |
| 134 | } |
| 135 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 136 | /* Marks a ready listener as limited so that we only try to re-enable it when |
| 137 | * resources are free again. It will be queued into the specified queue. |
| 138 | */ |
| 139 | void limit_listener(struct listener *l, struct list *list) |
| 140 | { |
| 141 | if (l->state == LI_READY) { |
| 142 | LIST_ADDQ(list, &l->wait_queue); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 143 | fd_stop_recv(l->fd); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 144 | l->state = LI_LIMITED; |
| 145 | } |
| 146 | } |
| 147 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 148 | /* This function adds all of the protocol's listener's file descriptors to the |
| 149 | * polling lists when they are in the LI_LISTEN state. It is intended to be |
| 150 | * used as a protocol's generic enable_all() primitive, for use after the |
| 151 | * fork(). It puts the listeners into LI_READY or LI_FULL states depending on |
| 152 | * their number of connections. It always returns ERR_NONE. |
| 153 | */ |
| 154 | int enable_all_listeners(struct protocol *proto) |
| 155 | { |
| 156 | struct listener *listener; |
| 157 | |
| 158 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 159 | enable_listener(listener); |
| 160 | return ERR_NONE; |
| 161 | } |
| 162 | |
| 163 | /* This function removes all of the protocol's listener's file descriptors from |
| 164 | * the polling lists when they are in the LI_READY or LI_FULL states. It is |
| 165 | * intended to be used as a protocol's generic disable_all() primitive. It puts |
| 166 | * the listeners into LI_LISTEN, and always returns ERR_NONE. |
| 167 | */ |
| 168 | int disable_all_listeners(struct protocol *proto) |
| 169 | { |
| 170 | struct listener *listener; |
| 171 | |
| 172 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 173 | disable_listener(listener); |
| 174 | return ERR_NONE; |
| 175 | } |
| 176 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 177 | /* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */ |
| 178 | void dequeue_all_listeners(struct list *list) |
| 179 | { |
| 180 | struct listener *listener, *l_back; |
| 181 | |
| 182 | list_for_each_entry_safe(listener, l_back, list, wait_queue) { |
| 183 | /* This cannot fail because the listeners are by definition in |
| 184 | * the LI_LIMITED state. The function also removes the entry |
| 185 | * from the queue. |
| 186 | */ |
| 187 | resume_listener(listener); |
| 188 | } |
| 189 | } |
| 190 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 191 | /* This function closes the listening socket for the specified listener, |
| 192 | * provided that it's already in a listening state. The listener enters the |
| 193 | * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended |
| 194 | * to be used as a generic function for standard protocols. |
| 195 | */ |
| 196 | int unbind_listener(struct listener *listener) |
| 197 | { |
| 198 | if (listener->state == LI_READY) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 199 | fd_stop_recv(listener->fd); |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 200 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 201 | if (listener->state == LI_LIMITED) |
| 202 | LIST_DEL(&listener->wait_queue); |
| 203 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 204 | if (listener->state >= LI_PAUSED) { |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 205 | fd_delete(listener->fd); |
| 206 | listener->state = LI_ASSIGNED; |
| 207 | } |
| 208 | return ERR_NONE; |
| 209 | } |
| 210 | |
Willy Tarreau | 3acf8c3 | 2007-10-28 22:35:41 +0100 | [diff] [blame] | 211 | /* This function closes all listening sockets bound to the protocol <proto>, |
| 212 | * and the listeners end in LI_ASSIGNED state if they were higher. It does not |
| 213 | * detach them from the protocol. It always returns ERR_NONE. |
| 214 | */ |
| 215 | int unbind_all_listeners(struct protocol *proto) |
| 216 | { |
| 217 | struct listener *listener; |
| 218 | |
| 219 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 220 | unbind_listener(listener); |
| 221 | return ERR_NONE; |
| 222 | } |
| 223 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 224 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 225 | * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's |
| 226 | * number of listeners is updated. Note that the listener must have previously |
| 227 | * been unbound. This is the generic function to use to remove a listener. |
| 228 | */ |
| 229 | void delete_listener(struct listener *listener) |
| 230 | { |
| 231 | if (listener->state != LI_ASSIGNED) |
| 232 | return; |
| 233 | listener->state = LI_INIT; |
| 234 | LIST_DEL(&listener->proto_list); |
| 235 | listener->proto->nb_listeners--; |
| 236 | } |
| 237 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 238 | /* This function is called on a read event from a listening socket, corresponding |
| 239 | * to an accept. It tries to accept as many connections as possible, and for each |
| 240 | * calls the listener's accept handler (generally the frontend's accept handler). |
| 241 | */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 242 | void listener_accept(int fd) |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 243 | { |
| 244 | struct listener *l = fdtab[fd].owner; |
| 245 | struct proxy *p = l->frontend; |
| 246 | int max_accept = global.tune.maxaccept; |
| 247 | int cfd; |
| 248 | int ret; |
| 249 | |
| 250 | if (unlikely(l->nbconn >= l->maxconn)) { |
| 251 | listener_full(l); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 252 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | if (global.cps_lim && !(l->options & LI_O_UNLIMITED)) { |
| 256 | int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0); |
| 257 | |
| 258 | if (unlikely(!max)) { |
| 259 | /* frontend accept rate limit was reached */ |
| 260 | limit_listener(l, &global_listener_queue); |
| 261 | task_schedule(global_listener_queue_task, tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0))); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 262 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | if (max_accept > max) |
| 266 | max_accept = max; |
| 267 | } |
| 268 | |
| 269 | if (p && p->fe_sps_lim) { |
| 270 | int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0); |
| 271 | |
| 272 | if (unlikely(!max)) { |
| 273 | /* frontend accept rate limit was reached */ |
| 274 | limit_listener(l, &p->listener_queue); |
| 275 | task_schedule(p->task, tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 276 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (max_accept > max) |
| 280 | max_accept = max; |
| 281 | } |
| 282 | |
| 283 | /* Note: if we fail to allocate a connection because of configured |
| 284 | * limits, we'll schedule a new attempt worst 1 second later in the |
| 285 | * worst case. If we fail due to system limits or temporary resource |
| 286 | * shortage, we try again 100ms later in the worst case. |
| 287 | */ |
| 288 | while (max_accept--) { |
| 289 | struct sockaddr_storage addr; |
| 290 | socklen_t laddr = sizeof(addr); |
| 291 | |
| 292 | if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) { |
| 293 | limit_listener(l, &global_listener_queue); |
| 294 | task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 295 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | if (unlikely(p && p->feconn >= p->maxconn)) { |
| 299 | limit_listener(l, &p->listener_queue); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 300 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | cfd = accept(fd, (struct sockaddr *)&addr, &laddr); |
| 304 | if (unlikely(cfd == -1)) { |
| 305 | switch (errno) { |
| 306 | case EAGAIN: |
| 307 | case EINTR: |
| 308 | case ECONNABORTED: |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 309 | fd_poll_recv(fd); |
| 310 | return; /* nothing more to accept */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 311 | case ENFILE: |
| 312 | if (p) |
| 313 | send_log(p, LOG_EMERG, |
| 314 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 315 | p->id, maxfd); |
| 316 | limit_listener(l, &global_listener_queue); |
| 317 | task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 318 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 319 | case EMFILE: |
| 320 | if (p) |
| 321 | send_log(p, LOG_EMERG, |
| 322 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 323 | p->id, maxfd); |
| 324 | limit_listener(l, &global_listener_queue); |
| 325 | task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 326 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 327 | case ENOBUFS: |
| 328 | case ENOMEM: |
| 329 | if (p) |
| 330 | send_log(p, LOG_EMERG, |
| 331 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 332 | p->id, maxfd); |
| 333 | limit_listener(l, &global_listener_queue); |
| 334 | task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 335 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 336 | default: |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 337 | /* unexpected result, let's go back to poll */ |
| 338 | fd_poll_recv(fd); |
| 339 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Willy Tarreau | fe7f1ea | 2012-05-20 19:22:25 +0200 | [diff] [blame] | 343 | /* if this connection comes from a known monitoring system, we want to ignore |
| 344 | * it as soon as possible, which means closing it immediately if it is only a |
| 345 | * TCP-based monitoring check. |
| 346 | */ |
| 347 | if (unlikely((l->options & LI_O_CHK_MONNET) && |
| 348 | (p->mode == PR_MODE_TCP) && |
| 349 | addr.ss_family == AF_INET && |
| 350 | (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) { |
| 351 | close(cfd); |
| 352 | continue; |
| 353 | } |
| 354 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 355 | if (unlikely(cfd >= global.maxsock)) { |
| 356 | send_log(p, LOG_EMERG, |
| 357 | "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n", |
| 358 | p->id); |
| 359 | close(cfd); |
| 360 | limit_listener(l, &global_listener_queue); |
| 361 | task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 362 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* increase the per-process number of cumulated connections */ |
| 366 | if (!(l->options & LI_O_UNLIMITED)) { |
| 367 | update_freq_ctr(&global.conn_per_sec, 1); |
| 368 | if (global.conn_per_sec.curr_ctr > global.cps_max) |
| 369 | global.cps_max = global.conn_per_sec.curr_ctr; |
| 370 | actconn++; |
| 371 | } |
| 372 | |
| 373 | jobs++; |
| 374 | totalconn++; |
| 375 | l->nbconn++; |
| 376 | |
| 377 | if (l->counters) { |
| 378 | if (l->nbconn > l->counters->conn_max) |
| 379 | l->counters->conn_max = l->nbconn; |
| 380 | } |
| 381 | |
| 382 | ret = l->accept(l, cfd, &addr); |
| 383 | if (unlikely(ret <= 0)) { |
| 384 | /* The connection was closed by session_accept(). Either |
| 385 | * we just have to ignore it (ret == 0) or it's a critical |
| 386 | * error due to a resource shortage, and we must stop the |
| 387 | * listener (ret < 0). |
| 388 | */ |
| 389 | if (!(l->options & LI_O_UNLIMITED)) |
| 390 | actconn--; |
| 391 | jobs--; |
| 392 | l->nbconn--; |
| 393 | if (ret == 0) /* successful termination */ |
| 394 | continue; |
| 395 | |
| 396 | limit_listener(l, &global_listener_queue); |
| 397 | task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 398 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | if (l->nbconn >= l->maxconn) { |
| 402 | listener_full(l); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 403 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 406 | } /* end of while (max_accept--) */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 407 | |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 408 | /* we've exhausted max_accept, so there is no need to poll again */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 409 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 412 | /************************************************************************/ |
| 413 | /* All supported ACL keywords must be declared here. */ |
| 414 | /************************************************************************/ |
| 415 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 416 | /* set temp integer to the number of connexions to the same listening socket */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 417 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 418 | acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 419 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 420 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 421 | smp->type = SMP_T_UINT; |
| 422 | smp->data.uint = l4->listener->nbconn; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 423 | return 1; |
| 424 | } |
| 425 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 426 | /* set temp integer to the id of the socket (listener) */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 427 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 428 | acl_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 429 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 430 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 431 | smp->type = SMP_T_UINT; |
| 432 | smp->data.uint = l4->listener->luid; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 433 | return 1; |
| 434 | } |
| 435 | |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 436 | /* Note: must not be declared <const> as its list will be overwritten. |
| 437 | * Please take care of keeping this list alphabetically sorted. |
| 438 | */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 439 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 440 | { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING, 0 }, |
| 441 | { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING, 0 }, |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 442 | { NULL, NULL, NULL, NULL }, |
| 443 | }}; |
| 444 | |
| 445 | __attribute__((constructor)) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 446 | static void __listener_init(void) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 447 | { |
| 448 | acl_register_keywords(&acl_kws); |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Local variables: |
| 453 | * c-indent-level: 8 |
| 454 | * c-basic-offset: 8 |
| 455 | * End: |
| 456 | */ |