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 | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 32 | /* List head of all known bind keywords */ |
| 33 | static struct bind_kw_list bind_keywords = { |
| 34 | .list = LIST_HEAD_INIT(bind_keywords.list) |
| 35 | }; |
| 36 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 37 | /* This function adds the specified listener's file descriptor to the polling |
| 38 | * lists if it is in the LI_LISTEN state. The listener enters LI_READY or |
| 39 | * LI_FULL state depending on its number of connections. |
| 40 | */ |
| 41 | void enable_listener(struct listener *listener) |
| 42 | { |
| 43 | if (listener->state == LI_LISTEN) { |
| 44 | if (listener->nbconn < listener->maxconn) { |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 45 | fd_want_recv(listener->fd); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 46 | listener->state = LI_READY; |
| 47 | } else { |
| 48 | listener->state = LI_FULL; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /* This function removes the specified listener's file descriptor from the |
| 54 | * polling lists if it is in the LI_READY or in the LI_FULL state. The listener |
| 55 | * enters LI_LISTEN. |
| 56 | */ |
| 57 | void disable_listener(struct listener *listener) |
| 58 | { |
| 59 | if (listener->state < LI_READY) |
| 60 | return; |
| 61 | if (listener->state == LI_READY) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 62 | fd_stop_recv(listener->fd); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 63 | if (listener->state == LI_LIMITED) |
| 64 | LIST_DEL(&listener->wait_queue); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 65 | listener->state = LI_LISTEN; |
| 66 | } |
| 67 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 68 | /* This function tries to temporarily disable a listener, depending on the OS |
| 69 | * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores |
| 70 | * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but |
| 71 | * closes upon SHUT_WR and refuses to rebind. So a common validation path |
| 72 | * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling |
| 73 | * is disabled. It normally returns non-zero, unless an error is reported. |
| 74 | */ |
| 75 | int pause_listener(struct listener *l) |
| 76 | { |
| 77 | if (l->state <= LI_PAUSED) |
| 78 | return 1; |
| 79 | |
| 80 | if (shutdown(l->fd, SHUT_WR) != 0) |
| 81 | return 0; /* Solaris dies here */ |
| 82 | |
| 83 | if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) |
| 84 | return 0; /* OpenBSD dies here */ |
| 85 | |
| 86 | if (shutdown(l->fd, SHUT_RD) != 0) |
| 87 | return 0; /* should always be OK */ |
| 88 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 89 | if (l->state == LI_LIMITED) |
| 90 | LIST_DEL(&l->wait_queue); |
| 91 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 92 | fd_stop_recv(l->fd); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 93 | l->state = LI_PAUSED; |
| 94 | return 1; |
| 95 | } |
| 96 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 97 | /* This function tries to resume a temporarily disabled listener. Paused, full, |
| 98 | * limited and disabled listeners are handled, which means that this function |
| 99 | * may replace enable_listener(). The resulting state will either be LI_READY |
| 100 | * 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] | 101 | */ |
| 102 | int resume_listener(struct listener *l) |
| 103 | { |
| 104 | if (l->state < LI_PAUSED) |
| 105 | return 0; |
| 106 | |
| 107 | if (l->state == LI_PAUSED && |
| 108 | listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) |
| 109 | return 0; |
| 110 | |
| 111 | if (l->state == LI_READY) |
| 112 | return 1; |
| 113 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 114 | if (l->state == LI_LIMITED) |
| 115 | LIST_DEL(&l->wait_queue); |
| 116 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 117 | if (l->nbconn >= l->maxconn) { |
| 118 | l->state = LI_FULL; |
| 119 | return 1; |
| 120 | } |
| 121 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 122 | fd_want_recv(l->fd); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 123 | l->state = LI_READY; |
| 124 | return 1; |
| 125 | } |
| 126 | |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 127 | /* Marks a ready listener as full so that the session code tries to re-enable |
| 128 | * it upon next close() using resume_listener(). |
| 129 | */ |
| 130 | void listener_full(struct listener *l) |
| 131 | { |
| 132 | if (l->state >= LI_READY) { |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 133 | if (l->state == LI_LIMITED) |
| 134 | LIST_DEL(&l->wait_queue); |
| 135 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 136 | fd_stop_recv(l->fd); |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 137 | l->state = LI_FULL; |
| 138 | } |
| 139 | } |
| 140 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 141 | /* Marks a ready listener as limited so that we only try to re-enable it when |
| 142 | * resources are free again. It will be queued into the specified queue. |
| 143 | */ |
| 144 | void limit_listener(struct listener *l, struct list *list) |
| 145 | { |
| 146 | if (l->state == LI_READY) { |
| 147 | LIST_ADDQ(list, &l->wait_queue); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 148 | fd_stop_recv(l->fd); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 149 | l->state = LI_LIMITED; |
| 150 | } |
| 151 | } |
| 152 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 153 | /* This function adds all of the protocol's listener's file descriptors to the |
| 154 | * polling lists when they are in the LI_LISTEN state. It is intended to be |
| 155 | * used as a protocol's generic enable_all() primitive, for use after the |
| 156 | * fork(). It puts the listeners into LI_READY or LI_FULL states depending on |
| 157 | * their number of connections. It always returns ERR_NONE. |
| 158 | */ |
| 159 | int enable_all_listeners(struct protocol *proto) |
| 160 | { |
| 161 | struct listener *listener; |
| 162 | |
| 163 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 164 | enable_listener(listener); |
| 165 | return ERR_NONE; |
| 166 | } |
| 167 | |
| 168 | /* This function removes all of the protocol's listener's file descriptors from |
| 169 | * the polling lists when they are in the LI_READY or LI_FULL states. It is |
| 170 | * intended to be used as a protocol's generic disable_all() primitive. It puts |
| 171 | * the listeners into LI_LISTEN, and always returns ERR_NONE. |
| 172 | */ |
| 173 | int disable_all_listeners(struct protocol *proto) |
| 174 | { |
| 175 | struct listener *listener; |
| 176 | |
| 177 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 178 | disable_listener(listener); |
| 179 | return ERR_NONE; |
| 180 | } |
| 181 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 182 | /* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */ |
| 183 | void dequeue_all_listeners(struct list *list) |
| 184 | { |
| 185 | struct listener *listener, *l_back; |
| 186 | |
| 187 | list_for_each_entry_safe(listener, l_back, list, wait_queue) { |
| 188 | /* This cannot fail because the listeners are by definition in |
| 189 | * the LI_LIMITED state. The function also removes the entry |
| 190 | * from the queue. |
| 191 | */ |
| 192 | resume_listener(listener); |
| 193 | } |
| 194 | } |
| 195 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 196 | /* This function closes the listening socket for the specified listener, |
| 197 | * provided that it's already in a listening state. The listener enters the |
| 198 | * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended |
| 199 | * to be used as a generic function for standard protocols. |
| 200 | */ |
| 201 | int unbind_listener(struct listener *listener) |
| 202 | { |
| 203 | if (listener->state == LI_READY) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 204 | fd_stop_recv(listener->fd); |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 205 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 206 | if (listener->state == LI_LIMITED) |
| 207 | LIST_DEL(&listener->wait_queue); |
| 208 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 209 | if (listener->state >= LI_PAUSED) { |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 210 | fd_delete(listener->fd); |
| 211 | listener->state = LI_ASSIGNED; |
| 212 | } |
| 213 | return ERR_NONE; |
| 214 | } |
| 215 | |
Willy Tarreau | 3acf8c3 | 2007-10-28 22:35:41 +0100 | [diff] [blame] | 216 | /* This function closes all listening sockets bound to the protocol <proto>, |
| 217 | * and the listeners end in LI_ASSIGNED state if they were higher. It does not |
| 218 | * detach them from the protocol. It always returns ERR_NONE. |
| 219 | */ |
| 220 | int unbind_all_listeners(struct protocol *proto) |
| 221 | { |
| 222 | struct listener *listener; |
| 223 | |
| 224 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 225 | unbind_listener(listener); |
| 226 | return ERR_NONE; |
| 227 | } |
| 228 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 229 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 230 | * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's |
| 231 | * number of listeners is updated. Note that the listener must have previously |
| 232 | * been unbound. This is the generic function to use to remove a listener. |
| 233 | */ |
| 234 | void delete_listener(struct listener *listener) |
| 235 | { |
| 236 | if (listener->state != LI_ASSIGNED) |
| 237 | return; |
| 238 | listener->state = LI_INIT; |
| 239 | LIST_DEL(&listener->proto_list); |
| 240 | listener->proto->nb_listeners--; |
| 241 | } |
| 242 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 243 | /* This function is called on a read event from a listening socket, corresponding |
| 244 | * to an accept. It tries to accept as many connections as possible, and for each |
| 245 | * calls the listener's accept handler (generally the frontend's accept handler). |
| 246 | */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 247 | void listener_accept(int fd) |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 248 | { |
| 249 | struct listener *l = fdtab[fd].owner; |
| 250 | struct proxy *p = l->frontend; |
| 251 | int max_accept = global.tune.maxaccept; |
| 252 | int cfd; |
| 253 | int ret; |
| 254 | |
| 255 | if (unlikely(l->nbconn >= l->maxconn)) { |
| 256 | listener_full(l); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 257 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (global.cps_lim && !(l->options & LI_O_UNLIMITED)) { |
| 261 | int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0); |
| 262 | |
| 263 | if (unlikely(!max)) { |
| 264 | /* frontend accept rate limit was reached */ |
| 265 | limit_listener(l, &global_listener_queue); |
| 266 | 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] | 267 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | if (max_accept > max) |
| 271 | max_accept = max; |
| 272 | } |
| 273 | |
| 274 | if (p && p->fe_sps_lim) { |
| 275 | int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0); |
| 276 | |
| 277 | if (unlikely(!max)) { |
| 278 | /* frontend accept rate limit was reached */ |
| 279 | limit_listener(l, &p->listener_queue); |
| 280 | 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] | 281 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | if (max_accept > max) |
| 285 | max_accept = max; |
| 286 | } |
| 287 | |
| 288 | /* Note: if we fail to allocate a connection because of configured |
| 289 | * limits, we'll schedule a new attempt worst 1 second later in the |
| 290 | * worst case. If we fail due to system limits or temporary resource |
| 291 | * shortage, we try again 100ms later in the worst case. |
| 292 | */ |
| 293 | while (max_accept--) { |
| 294 | struct sockaddr_storage addr; |
| 295 | socklen_t laddr = sizeof(addr); |
| 296 | |
| 297 | if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) { |
| 298 | limit_listener(l, &global_listener_queue); |
| 299 | 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] | 300 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | if (unlikely(p && p->feconn >= p->maxconn)) { |
| 304 | limit_listener(l, &p->listener_queue); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 305 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | cfd = accept(fd, (struct sockaddr *)&addr, &laddr); |
| 309 | if (unlikely(cfd == -1)) { |
| 310 | switch (errno) { |
| 311 | case EAGAIN: |
| 312 | case EINTR: |
| 313 | case ECONNABORTED: |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 314 | fd_poll_recv(fd); |
| 315 | return; /* nothing more to accept */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 316 | case ENFILE: |
| 317 | if (p) |
| 318 | send_log(p, LOG_EMERG, |
| 319 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 320 | p->id, maxfd); |
| 321 | limit_listener(l, &global_listener_queue); |
| 322 | 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] | 323 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 324 | case EMFILE: |
| 325 | if (p) |
| 326 | send_log(p, LOG_EMERG, |
| 327 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 328 | p->id, maxfd); |
| 329 | limit_listener(l, &global_listener_queue); |
| 330 | 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] | 331 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 332 | case ENOBUFS: |
| 333 | case ENOMEM: |
| 334 | if (p) |
| 335 | send_log(p, LOG_EMERG, |
| 336 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 337 | p->id, maxfd); |
| 338 | limit_listener(l, &global_listener_queue); |
| 339 | 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] | 340 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 341 | default: |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 342 | /* unexpected result, let's go back to poll */ |
| 343 | fd_poll_recv(fd); |
| 344 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Willy Tarreau | fe7f1ea | 2012-05-20 19:22:25 +0200 | [diff] [blame] | 348 | /* if this connection comes from a known monitoring system, we want to ignore |
| 349 | * it as soon as possible, which means closing it immediately if it is only a |
| 350 | * TCP-based monitoring check. |
| 351 | */ |
| 352 | if (unlikely((l->options & LI_O_CHK_MONNET) && |
| 353 | (p->mode == PR_MODE_TCP) && |
| 354 | addr.ss_family == AF_INET && |
| 355 | (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) { |
| 356 | close(cfd); |
| 357 | continue; |
| 358 | } |
| 359 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 360 | if (unlikely(cfd >= global.maxsock)) { |
| 361 | send_log(p, LOG_EMERG, |
| 362 | "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n", |
| 363 | p->id); |
| 364 | close(cfd); |
| 365 | limit_listener(l, &global_listener_queue); |
| 366 | 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] | 367 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* increase the per-process number of cumulated connections */ |
| 371 | if (!(l->options & LI_O_UNLIMITED)) { |
| 372 | update_freq_ctr(&global.conn_per_sec, 1); |
| 373 | if (global.conn_per_sec.curr_ctr > global.cps_max) |
| 374 | global.cps_max = global.conn_per_sec.curr_ctr; |
| 375 | actconn++; |
| 376 | } |
| 377 | |
| 378 | jobs++; |
| 379 | totalconn++; |
| 380 | l->nbconn++; |
| 381 | |
| 382 | if (l->counters) { |
| 383 | if (l->nbconn > l->counters->conn_max) |
| 384 | l->counters->conn_max = l->nbconn; |
| 385 | } |
| 386 | |
| 387 | ret = l->accept(l, cfd, &addr); |
| 388 | if (unlikely(ret <= 0)) { |
| 389 | /* The connection was closed by session_accept(). Either |
| 390 | * we just have to ignore it (ret == 0) or it's a critical |
| 391 | * error due to a resource shortage, and we must stop the |
| 392 | * listener (ret < 0). |
| 393 | */ |
| 394 | if (!(l->options & LI_O_UNLIMITED)) |
| 395 | actconn--; |
| 396 | jobs--; |
| 397 | l->nbconn--; |
| 398 | if (ret == 0) /* successful termination */ |
| 399 | continue; |
| 400 | |
| 401 | limit_listener(l, &global_listener_queue); |
| 402 | 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] | 403 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | if (l->nbconn >= l->maxconn) { |
| 407 | listener_full(l); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 408 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 411 | } /* end of while (max_accept--) */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 412 | |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 413 | /* 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] | 414 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 417 | /* |
| 418 | * Registers the bind keyword list <kwl> as a list of valid keywords for next |
| 419 | * parsing sessions. |
| 420 | */ |
| 421 | void bind_register_keywords(struct bind_kw_list *kwl) |
| 422 | { |
| 423 | LIST_ADDQ(&bind_keywords.list, &kwl->list); |
| 424 | } |
| 425 | |
| 426 | /* Return a pointer to the bind keyword <kw>, or NULL if not found. If the |
| 427 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 428 | * find one with a valid ->parse() function. This way it is possible to declare |
| 429 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 430 | * if some options are met. Note that if the requested keyword contains an |
| 431 | * opening parenthesis, everything from this point is ignored. |
| 432 | */ |
| 433 | struct bind_kw *bind_find_kw(const char *kw) |
| 434 | { |
| 435 | int index; |
| 436 | const char *kwend; |
| 437 | struct bind_kw_list *kwl; |
| 438 | struct bind_kw *ret = NULL; |
| 439 | |
| 440 | kwend = strchr(kw, '('); |
| 441 | if (!kwend) |
| 442 | kwend = kw + strlen(kw); |
| 443 | |
| 444 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 445 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 446 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 447 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 448 | if (kwl->kw[index].parse) |
| 449 | return &kwl->kw[index]; /* found it !*/ |
| 450 | else |
| 451 | ret = &kwl->kw[index]; /* may be OK */ |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | return ret; |
| 456 | } |
| 457 | |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 458 | /* Dumps all registered "bind" keywords to the <out> string pointer. The |
| 459 | * unsupported keywords are only dumped if their supported form was not |
| 460 | * found. |
| 461 | */ |
| 462 | void bind_dump_kws(char **out) |
| 463 | { |
| 464 | struct bind_kw_list *kwl; |
| 465 | int index; |
| 466 | |
| 467 | *out = NULL; |
| 468 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 469 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 470 | if (kwl->kw[index].parse || |
| 471 | bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 472 | memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "", |
| 473 | kwl->scope, |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 474 | kwl->kw[index].kw, |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 475 | kwl->kw[index].skip ? " <arg>" : "", |
| 476 | kwl->kw[index].parse ? "" : " (not supported)"); |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 482 | /************************************************************************/ |
| 483 | /* All supported ACL keywords must be declared here. */ |
| 484 | /************************************************************************/ |
| 485 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 486 | /* 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] | 487 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 488 | 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] | 489 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 490 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 491 | smp->type = SMP_T_UINT; |
| 492 | smp->data.uint = l4->listener->nbconn; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 493 | return 1; |
| 494 | } |
| 495 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 496 | /* set temp integer to the id of the socket (listener) */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 497 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 498 | 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] | 499 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 500 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 501 | smp->type = SMP_T_UINT; |
| 502 | smp->data.uint = l4->listener->luid; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 503 | return 1; |
| 504 | } |
| 505 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 506 | /* parse the "accept-proxy" bind keyword */ |
| 507 | static int bind_parse_accept_proxy(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err) |
| 508 | { |
| 509 | struct listener *l; |
| 510 | |
| 511 | for (l = px->listen; l != last; l = l->next) |
| 512 | l->options |= LI_O_ACC_PROXY; |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | /* parse the "backlog" bind keyword */ |
| 518 | static int bind_parse_backlog(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err) |
| 519 | { |
| 520 | struct listener *l; |
| 521 | int val; |
| 522 | |
| 523 | if (!*args[cur_arg + 1]) { |
| 524 | if (err) |
| 525 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
| 526 | return ERR_ALERT | ERR_FATAL; |
| 527 | } |
| 528 | |
| 529 | val = atol(args[cur_arg + 1]); |
| 530 | if (val <= 0) { |
| 531 | if (err) |
| 532 | memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val); |
| 533 | return ERR_ALERT | ERR_FATAL; |
| 534 | } |
| 535 | |
| 536 | for (l = px->listen; l != last; l = l->next) |
| 537 | l->backlog = val; |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | /* parse the "id" bind keyword */ |
| 543 | static int bind_parse_id(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err) |
| 544 | { |
| 545 | struct eb32_node *node; |
| 546 | struct listener *l; |
| 547 | |
| 548 | if (px->listen->next != last) { |
| 549 | if (err) |
| 550 | memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]); |
| 551 | return ERR_ALERT | ERR_FATAL; |
| 552 | } |
| 553 | |
| 554 | if (!*args[cur_arg + 1]) { |
| 555 | if (err) |
| 556 | memprintf(err, "'%s' : expects an integer argument", args[cur_arg]); |
| 557 | return ERR_ALERT | ERR_FATAL; |
| 558 | } |
| 559 | |
| 560 | px->listen->luid = atol(args[cur_arg + 1]); |
| 561 | px->listen->conf.id.key = px->listen->luid; |
| 562 | |
| 563 | if (px->listen->luid <= 0) { |
| 564 | if (err) |
| 565 | memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]); |
| 566 | return ERR_ALERT | ERR_FATAL; |
| 567 | } |
| 568 | |
| 569 | node = eb32_lookup(&px->conf.used_listener_id, px->listen->luid); |
| 570 | if (node) { |
| 571 | l = container_of(node, struct listener, conf.id); |
| 572 | if (err) |
| 573 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')", |
| 574 | args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line, |
| 575 | l->bind_conf->arg); |
| 576 | return ERR_ALERT | ERR_FATAL; |
| 577 | } |
| 578 | |
| 579 | eb32_insert(&px->conf.used_listener_id, &px->listen->conf.id); |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | /* parse the "maxconn" bind keyword */ |
| 584 | static int bind_parse_maxconn(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err) |
| 585 | { |
| 586 | struct listener *l; |
| 587 | int val; |
| 588 | |
| 589 | if (!*args[cur_arg + 1]) { |
| 590 | if (err) |
| 591 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
| 592 | return ERR_ALERT | ERR_FATAL; |
| 593 | } |
| 594 | |
| 595 | val = atol(args[cur_arg + 1]); |
| 596 | if (val <= 0) { |
| 597 | if (err) |
| 598 | memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val); |
| 599 | return ERR_ALERT | ERR_FATAL; |
| 600 | } |
| 601 | |
| 602 | for (l = px->listen; l != last; l = l->next) |
| 603 | l->maxconn = val; |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | /* parse the "name" bind keyword */ |
| 609 | static int bind_parse_name(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err) |
| 610 | { |
| 611 | struct listener *l; |
| 612 | |
| 613 | if (!*args[cur_arg + 1]) { |
| 614 | if (err) |
| 615 | memprintf(err, "'%s' : missing name", args[cur_arg]); |
| 616 | return ERR_ALERT | ERR_FATAL; |
| 617 | } |
| 618 | |
| 619 | for (l = px->listen; l != last; l = l->next) |
| 620 | l->name = strdup(args[cur_arg + 1]); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | /* parse the "nice" bind keyword */ |
| 626 | static int bind_parse_nice(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err) |
| 627 | { |
| 628 | struct listener *l; |
| 629 | int val; |
| 630 | |
| 631 | if (!*args[cur_arg + 1]) { |
| 632 | if (err) |
| 633 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
| 634 | return ERR_ALERT | ERR_FATAL; |
| 635 | } |
| 636 | |
| 637 | val = atol(args[cur_arg + 1]); |
| 638 | if (val < -1024 || val > 1024) { |
| 639 | if (err) |
| 640 | memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val); |
| 641 | return ERR_ALERT | ERR_FATAL; |
| 642 | } |
| 643 | |
| 644 | for (l = px->listen; l != last; l = l->next) |
| 645 | l->nice = val; |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 651 | /* Note: must not be declared <const> as its list will be overwritten. |
| 652 | * Please take care of keeping this list alphabetically sorted. |
| 653 | */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 654 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 655 | { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING, 0 }, |
| 656 | { "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] | 657 | { NULL, NULL, NULL, NULL }, |
| 658 | }}; |
| 659 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 660 | /* Note: must not be declared <const> as its list will be overwritten. |
| 661 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 662 | * all code contributors. |
| 663 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 664 | * the config parser can report an appropriate error when a known keyword was |
| 665 | * not enabled. |
| 666 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 667 | static struct bind_kw_list bind_kws = { "ALL", { }, { |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 668 | { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */ |
| 669 | { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */ |
| 670 | { "id", bind_parse_id, 1 }, /* set id of listening socket */ |
| 671 | { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */ |
| 672 | { "name", bind_parse_name, 1 }, /* set name of listening socket */ |
| 673 | { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */ |
| 674 | { NULL, NULL, 0 }, |
| 675 | }}; |
| 676 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 677 | __attribute__((constructor)) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 678 | static void __listener_init(void) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 679 | { |
| 680 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 681 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Local variables: |
| 686 | * c-indent-level: 8 |
| 687 | * c-basic-offset: 8 |
| 688 | * End: |
| 689 | */ |