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 | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 4 | * Copyright 2000-2013 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 | 4448925 | 2014-01-14 17:52:01 +0100 | [diff] [blame] | 13 | #define _GNU_SOURCE |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <string.h> |
Willy Tarreau | 95ccdde | 2014-02-01 09:28:36 +0100 | [diff] [blame] | 18 | #include <unistd.h> |
| 19 | #include <fcntl.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 20 | |
Willy Tarreau | 1bc4aab | 2012-10-08 20:11:03 +0200 | [diff] [blame] | 21 | #include <common/accept4.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 22 | #include <common/config.h> |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 23 | #include <common/errors.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 24 | #include <common/mini-clist.h> |
| 25 | #include <common/standard.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 26 | #include <common/time.h> |
| 27 | |
| 28 | #include <types/global.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 29 | #include <types/protocol.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 31 | #include <proto/acl.h> |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 32 | #include <proto/fd.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 33 | #include <proto/freq_ctr.h> |
| 34 | #include <proto/log.h> |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 35 | #include <proto/sample.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 36 | #include <proto/task.h> |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 37 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 38 | /* List head of all known bind keywords */ |
| 39 | static struct bind_kw_list bind_keywords = { |
| 40 | .list = LIST_HEAD_INIT(bind_keywords.list) |
| 41 | }; |
| 42 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 43 | /* This function adds the specified listener's file descriptor to the polling |
| 44 | * lists if it is in the LI_LISTEN state. The listener enters LI_READY or |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 45 | * LI_FULL state depending on its number of connections. In deamon mode, we |
| 46 | * also support binding only the relevant processes to their respective |
| 47 | * listeners. We don't do that in debug mode however. |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 48 | */ |
| 49 | void enable_listener(struct listener *listener) |
| 50 | { |
| 51 | if (listener->state == LI_LISTEN) { |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 52 | if ((global.mode & (MODE_DAEMON | MODE_SYSTEMD)) && |
| 53 | listener->bind_conf->bind_proc && |
| 54 | !(listener->bind_conf->bind_proc & (1UL << (relative_pid - 1)))) { |
| 55 | /* we don't want to enable this listener and don't |
| 56 | * want any fd event to reach it. |
| 57 | */ |
| 58 | fd_stop_recv(listener->fd); |
| 59 | listener->state = LI_PAUSED; |
| 60 | } |
| 61 | else if (listener->nbconn < listener->maxconn) { |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 62 | fd_want_recv(listener->fd); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 63 | listener->state = LI_READY; |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 64 | } |
| 65 | else { |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 66 | listener->state = LI_FULL; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* This function removes the specified listener's file descriptor from the |
| 72 | * polling lists if it is in the LI_READY or in the LI_FULL state. The listener |
| 73 | * enters LI_LISTEN. |
| 74 | */ |
| 75 | void disable_listener(struct listener *listener) |
| 76 | { |
| 77 | if (listener->state < LI_READY) |
| 78 | return; |
| 79 | if (listener->state == LI_READY) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 80 | fd_stop_recv(listener->fd); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 81 | if (listener->state == LI_LIMITED) |
| 82 | LIST_DEL(&listener->wait_queue); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 83 | listener->state = LI_LISTEN; |
| 84 | } |
| 85 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 86 | /* This function tries to temporarily disable a listener, depending on the OS |
| 87 | * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores |
| 88 | * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but |
| 89 | * closes upon SHUT_WR and refuses to rebind. So a common validation path |
| 90 | * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling |
| 91 | * is disabled. It normally returns non-zero, unless an error is reported. |
| 92 | */ |
| 93 | int pause_listener(struct listener *l) |
| 94 | { |
| 95 | if (l->state <= LI_PAUSED) |
| 96 | return 1; |
| 97 | |
Willy Tarreau | b3fb60b | 2012-10-04 08:56:31 +0200 | [diff] [blame] | 98 | if (l->proto->sock_prot == IPPROTO_TCP) { |
| 99 | if (shutdown(l->fd, SHUT_WR) != 0) |
| 100 | return 0; /* Solaris dies here */ |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 101 | |
Willy Tarreau | b3fb60b | 2012-10-04 08:56:31 +0200 | [diff] [blame] | 102 | if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) |
| 103 | return 0; /* OpenBSD dies here */ |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 104 | |
Willy Tarreau | b3fb60b | 2012-10-04 08:56:31 +0200 | [diff] [blame] | 105 | if (shutdown(l->fd, SHUT_RD) != 0) |
| 106 | return 0; /* should always be OK */ |
| 107 | } |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 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 | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 112 | fd_stop_recv(l->fd); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 113 | l->state = LI_PAUSED; |
| 114 | return 1; |
| 115 | } |
| 116 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 117 | /* This function tries to resume a temporarily disabled listener. Paused, full, |
| 118 | * limited and disabled listeners are handled, which means that this function |
| 119 | * may replace enable_listener(). The resulting state will either be LI_READY |
| 120 | * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket). |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 121 | * Listeners bound to a different process are not woken up unless we're in |
| 122 | * foreground mode. |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 123 | */ |
| 124 | int resume_listener(struct listener *l) |
| 125 | { |
| 126 | if (l->state < LI_PAUSED) |
| 127 | return 0; |
| 128 | |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 129 | if ((global.mode & (MODE_DAEMON | MODE_SYSTEMD)) && |
| 130 | l->bind_conf->bind_proc && |
| 131 | !(l->bind_conf->bind_proc & (1UL << (relative_pid - 1)))) |
| 132 | return 0; |
| 133 | |
Willy Tarreau | b3fb60b | 2012-10-04 08:56:31 +0200 | [diff] [blame] | 134 | if (l->proto->sock_prot == IPPROTO_TCP && |
| 135 | l->state == LI_PAUSED && |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 136 | listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) |
| 137 | return 0; |
| 138 | |
| 139 | if (l->state == LI_READY) |
| 140 | return 1; |
| 141 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 142 | if (l->state == LI_LIMITED) |
| 143 | LIST_DEL(&l->wait_queue); |
| 144 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 145 | if (l->nbconn >= l->maxconn) { |
| 146 | l->state = LI_FULL; |
| 147 | return 1; |
| 148 | } |
| 149 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 150 | fd_want_recv(l->fd); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 151 | l->state = LI_READY; |
| 152 | return 1; |
| 153 | } |
| 154 | |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 155 | /* Marks a ready listener as full so that the session code tries to re-enable |
| 156 | * it upon next close() using resume_listener(). |
| 157 | */ |
| 158 | void listener_full(struct listener *l) |
| 159 | { |
| 160 | if (l->state >= LI_READY) { |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 161 | if (l->state == LI_LIMITED) |
| 162 | LIST_DEL(&l->wait_queue); |
| 163 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 164 | fd_stop_recv(l->fd); |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 165 | l->state = LI_FULL; |
| 166 | } |
| 167 | } |
| 168 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 169 | /* Marks a ready listener as limited so that we only try to re-enable it when |
| 170 | * resources are free again. It will be queued into the specified queue. |
| 171 | */ |
| 172 | void limit_listener(struct listener *l, struct list *list) |
| 173 | { |
| 174 | if (l->state == LI_READY) { |
| 175 | LIST_ADDQ(list, &l->wait_queue); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 176 | fd_stop_recv(l->fd); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 177 | l->state = LI_LIMITED; |
| 178 | } |
| 179 | } |
| 180 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 181 | /* This function adds all of the protocol's listener's file descriptors to the |
| 182 | * polling lists when they are in the LI_LISTEN state. It is intended to be |
| 183 | * used as a protocol's generic enable_all() primitive, for use after the |
| 184 | * fork(). It puts the listeners into LI_READY or LI_FULL states depending on |
| 185 | * their number of connections. It always returns ERR_NONE. |
| 186 | */ |
| 187 | int enable_all_listeners(struct protocol *proto) |
| 188 | { |
| 189 | struct listener *listener; |
| 190 | |
| 191 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 192 | enable_listener(listener); |
| 193 | return ERR_NONE; |
| 194 | } |
| 195 | |
| 196 | /* This function removes all of the protocol's listener's file descriptors from |
| 197 | * the polling lists when they are in the LI_READY or LI_FULL states. It is |
| 198 | * intended to be used as a protocol's generic disable_all() primitive. It puts |
| 199 | * the listeners into LI_LISTEN, and always returns ERR_NONE. |
| 200 | */ |
| 201 | int disable_all_listeners(struct protocol *proto) |
| 202 | { |
| 203 | struct listener *listener; |
| 204 | |
| 205 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 206 | disable_listener(listener); |
| 207 | return ERR_NONE; |
| 208 | } |
| 209 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 210 | /* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */ |
| 211 | void dequeue_all_listeners(struct list *list) |
| 212 | { |
| 213 | struct listener *listener, *l_back; |
| 214 | |
| 215 | list_for_each_entry_safe(listener, l_back, list, wait_queue) { |
| 216 | /* This cannot fail because the listeners are by definition in |
| 217 | * the LI_LIMITED state. The function also removes the entry |
| 218 | * from the queue. |
| 219 | */ |
| 220 | resume_listener(listener); |
| 221 | } |
| 222 | } |
| 223 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 224 | /* This function closes the listening socket for the specified listener, |
| 225 | * provided that it's already in a listening state. The listener enters the |
| 226 | * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended |
| 227 | * to be used as a generic function for standard protocols. |
| 228 | */ |
| 229 | int unbind_listener(struct listener *listener) |
| 230 | { |
| 231 | if (listener->state == LI_READY) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 232 | fd_stop_recv(listener->fd); |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 233 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 234 | if (listener->state == LI_LIMITED) |
| 235 | LIST_DEL(&listener->wait_queue); |
| 236 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 237 | if (listener->state >= LI_PAUSED) { |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 238 | fd_delete(listener->fd); |
| 239 | listener->state = LI_ASSIGNED; |
| 240 | } |
| 241 | return ERR_NONE; |
| 242 | } |
| 243 | |
Willy Tarreau | 3acf8c3 | 2007-10-28 22:35:41 +0100 | [diff] [blame] | 244 | /* This function closes all listening sockets bound to the protocol <proto>, |
| 245 | * and the listeners end in LI_ASSIGNED state if they were higher. It does not |
| 246 | * detach them from the protocol. It always returns ERR_NONE. |
| 247 | */ |
| 248 | int unbind_all_listeners(struct protocol *proto) |
| 249 | { |
| 250 | struct listener *listener; |
| 251 | |
| 252 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 253 | unbind_listener(listener); |
| 254 | return ERR_NONE; |
| 255 | } |
| 256 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 257 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 258 | * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's |
| 259 | * number of listeners is updated. Note that the listener must have previously |
| 260 | * been unbound. This is the generic function to use to remove a listener. |
| 261 | */ |
| 262 | void delete_listener(struct listener *listener) |
| 263 | { |
| 264 | if (listener->state != LI_ASSIGNED) |
| 265 | return; |
| 266 | listener->state = LI_INIT; |
| 267 | LIST_DEL(&listener->proto_list); |
| 268 | listener->proto->nb_listeners--; |
| 269 | } |
| 270 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 271 | /* This function is called on a read event from a listening socket, corresponding |
| 272 | * to an accept. It tries to accept as many connections as possible, and for each |
| 273 | * calls the listener's accept handler (generally the frontend's accept handler). |
| 274 | */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 275 | void listener_accept(int fd) |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 276 | { |
| 277 | struct listener *l = fdtab[fd].owner; |
| 278 | struct proxy *p = l->frontend; |
Willy Tarreau | 50de90a | 2012-11-23 20:11:45 +0100 | [diff] [blame] | 279 | int max_accept = l->maxaccept ? l->maxaccept : 1; |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 280 | int expire; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 281 | int cfd; |
| 282 | int ret; |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 283 | #ifdef USE_ACCEPT4 |
| 284 | static int accept4_broken; |
| 285 | #endif |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 286 | |
| 287 | if (unlikely(l->nbconn >= l->maxconn)) { |
| 288 | listener_full(l); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 289 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 292 | if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) { |
| 293 | int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0); |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 294 | |
| 295 | if (unlikely(!max)) { |
| 296 | /* frontend accept rate limit was reached */ |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 297 | expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0)); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 298 | goto wait_expire; |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | if (max_accept > max) |
| 302 | max_accept = max; |
| 303 | } |
| 304 | |
| 305 | if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) { |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 306 | int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0); |
| 307 | |
| 308 | if (unlikely(!max)) { |
| 309 | /* frontend accept rate limit was reached */ |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 310 | expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0)); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 311 | goto wait_expire; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (max_accept > max) |
| 315 | max_accept = max; |
| 316 | } |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 317 | #ifdef USE_OPENSSL |
| 318 | if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) { |
| 319 | int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 320 | |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 321 | if (unlikely(!max)) { |
| 322 | /* frontend accept rate limit was reached */ |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 323 | expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0)); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 324 | goto wait_expire; |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (max_accept > max) |
| 328 | max_accept = max; |
| 329 | } |
| 330 | #endif |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 331 | if (p && p->fe_sps_lim) { |
| 332 | int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0); |
| 333 | |
| 334 | if (unlikely(!max)) { |
| 335 | /* frontend accept rate limit was reached */ |
| 336 | limit_listener(l, &p->listener_queue); |
| 337 | 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] | 338 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | if (max_accept > max) |
| 342 | max_accept = max; |
| 343 | } |
| 344 | |
| 345 | /* Note: if we fail to allocate a connection because of configured |
| 346 | * limits, we'll schedule a new attempt worst 1 second later in the |
| 347 | * worst case. If we fail due to system limits or temporary resource |
| 348 | * shortage, we try again 100ms later in the worst case. |
| 349 | */ |
| 350 | while (max_accept--) { |
| 351 | struct sockaddr_storage addr; |
| 352 | socklen_t laddr = sizeof(addr); |
| 353 | |
| 354 | if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) { |
| 355 | limit_listener(l, &global_listener_queue); |
| 356 | 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] | 357 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | if (unlikely(p && p->feconn >= p->maxconn)) { |
| 361 | limit_listener(l, &p->listener_queue); |
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 | |
Willy Tarreau | 1bc4aab | 2012-10-08 20:11:03 +0200 | [diff] [blame] | 365 | #ifdef USE_ACCEPT4 |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 366 | /* only call accept4() if it's known to be safe, otherwise |
| 367 | * fallback to the legacy accept() + fcntl(). |
| 368 | */ |
| 369 | if (unlikely(accept4_broken || |
| 370 | ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 && |
| 371 | (errno == ENOSYS || errno == EINVAL || errno == EBADF) && |
| 372 | (accept4_broken = 1)))) |
| 373 | #endif |
Willy Tarreau | 6b3b0d4 | 2012-10-22 19:32:55 +0200 | [diff] [blame] | 374 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1) |
| 375 | fcntl(cfd, F_SETFL, O_NONBLOCK); |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 376 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 377 | if (unlikely(cfd == -1)) { |
| 378 | switch (errno) { |
| 379 | case EAGAIN: |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 380 | if (fdtab[fd].ev & FD_POLL_HUP) { |
| 381 | /* the listening socket might have been disabled in a shared |
| 382 | * process and we're a collateral victim. We'll just pause for |
| 383 | * a while in case it comes back. In the mean time, we need to |
| 384 | * clear this sticky flag. |
| 385 | */ |
| 386 | fdtab[fd].ev &= ~FD_POLL_HUP; |
| 387 | goto transient_error; |
| 388 | } |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 389 | fd_cant_recv(fd); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 390 | return; /* nothing more to accept */ |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 391 | case EINVAL: |
| 392 | /* might be trying to accept on a shut fd (eg: soft stop) */ |
| 393 | goto transient_error; |
Willy Tarreau | a593ec5 | 2014-01-20 21:21:30 +0100 | [diff] [blame] | 394 | case EINTR: |
| 395 | case ECONNABORTED: |
| 396 | continue; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 397 | case ENFILE: |
| 398 | if (p) |
| 399 | send_log(p, LOG_EMERG, |
| 400 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 401 | p->id, maxfd); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 402 | goto transient_error; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 403 | case EMFILE: |
| 404 | if (p) |
| 405 | send_log(p, LOG_EMERG, |
| 406 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 407 | p->id, maxfd); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 408 | goto transient_error; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 409 | case ENOBUFS: |
| 410 | case ENOMEM: |
| 411 | if (p) |
| 412 | send_log(p, LOG_EMERG, |
| 413 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 414 | p->id, maxfd); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 415 | goto transient_error; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 416 | default: |
Willy Tarreau | a593ec5 | 2014-01-20 21:21:30 +0100 | [diff] [blame] | 417 | /* unexpected result, let's give up and let other tasks run */ |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 418 | goto stop; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | if (unlikely(cfd >= global.maxsock)) { |
| 423 | send_log(p, LOG_EMERG, |
| 424 | "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n", |
| 425 | p->id); |
| 426 | close(cfd); |
| 427 | limit_listener(l, &global_listener_queue); |
| 428 | 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] | 429 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* increase the per-process number of cumulated connections */ |
| 433 | if (!(l->options & LI_O_UNLIMITED)) { |
| 434 | update_freq_ctr(&global.conn_per_sec, 1); |
| 435 | if (global.conn_per_sec.curr_ctr > global.cps_max) |
| 436 | global.cps_max = global.conn_per_sec.curr_ctr; |
| 437 | actconn++; |
| 438 | } |
| 439 | |
| 440 | jobs++; |
| 441 | totalconn++; |
| 442 | l->nbconn++; |
| 443 | |
| 444 | if (l->counters) { |
| 445 | if (l->nbconn > l->counters->conn_max) |
| 446 | l->counters->conn_max = l->nbconn; |
| 447 | } |
| 448 | |
| 449 | ret = l->accept(l, cfd, &addr); |
| 450 | if (unlikely(ret <= 0)) { |
| 451 | /* The connection was closed by session_accept(). Either |
| 452 | * we just have to ignore it (ret == 0) or it's a critical |
| 453 | * error due to a resource shortage, and we must stop the |
| 454 | * listener (ret < 0). |
| 455 | */ |
| 456 | if (!(l->options & LI_O_UNLIMITED)) |
| 457 | actconn--; |
| 458 | jobs--; |
| 459 | l->nbconn--; |
| 460 | if (ret == 0) /* successful termination */ |
| 461 | continue; |
| 462 | |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 463 | goto transient_error; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | if (l->nbconn >= l->maxconn) { |
| 467 | listener_full(l); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 468 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 471 | /* increase the per-process number of cumulated connections */ |
| 472 | if (!(l->options & LI_O_UNLIMITED)) { |
| 473 | update_freq_ctr(&global.sess_per_sec, 1); |
| 474 | if (global.sess_per_sec.curr_ctr > global.sps_max) |
| 475 | global.sps_max = global.sess_per_sec.curr_ctr; |
| 476 | } |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 477 | #ifdef USE_OPENSSL |
| 478 | if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) { |
| 479 | |
| 480 | update_freq_ctr(&global.ssl_per_sec, 1); |
| 481 | if (global.ssl_per_sec.curr_ctr > global.ssl_max) |
| 482 | global.ssl_max = global.ssl_per_sec.curr_ctr; |
| 483 | } |
| 484 | #endif |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 485 | |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 486 | } /* end of while (max_accept--) */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 487 | |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 488 | /* we've exhausted max_accept, so there is no need to poll again */ |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 489 | stop: |
| 490 | fd_done_recv(fd); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 491 | return; |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 492 | |
| 493 | transient_error: |
| 494 | /* pause the listener and try again in 100 ms */ |
| 495 | expire = tick_add(now_ms, 100); |
| 496 | |
| 497 | wait_expire: |
| 498 | limit_listener(l, &global_listener_queue); |
| 499 | task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire)); |
| 500 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 501 | } |
| 502 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 503 | /* |
| 504 | * Registers the bind keyword list <kwl> as a list of valid keywords for next |
| 505 | * parsing sessions. |
| 506 | */ |
| 507 | void bind_register_keywords(struct bind_kw_list *kwl) |
| 508 | { |
| 509 | LIST_ADDQ(&bind_keywords.list, &kwl->list); |
| 510 | } |
| 511 | |
| 512 | /* Return a pointer to the bind keyword <kw>, or NULL if not found. If the |
| 513 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 514 | * find one with a valid ->parse() function. This way it is possible to declare |
| 515 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 516 | * if some options are met. Note that if the requested keyword contains an |
| 517 | * opening parenthesis, everything from this point is ignored. |
| 518 | */ |
| 519 | struct bind_kw *bind_find_kw(const char *kw) |
| 520 | { |
| 521 | int index; |
| 522 | const char *kwend; |
| 523 | struct bind_kw_list *kwl; |
| 524 | struct bind_kw *ret = NULL; |
| 525 | |
| 526 | kwend = strchr(kw, '('); |
| 527 | if (!kwend) |
| 528 | kwend = kw + strlen(kw); |
| 529 | |
| 530 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 531 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 532 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 533 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 534 | if (kwl->kw[index].parse) |
| 535 | return &kwl->kw[index]; /* found it !*/ |
| 536 | else |
| 537 | ret = &kwl->kw[index]; /* may be OK */ |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | return ret; |
| 542 | } |
| 543 | |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 544 | /* Dumps all registered "bind" keywords to the <out> string pointer. The |
| 545 | * unsupported keywords are only dumped if their supported form was not |
| 546 | * found. |
| 547 | */ |
| 548 | void bind_dump_kws(char **out) |
| 549 | { |
| 550 | struct bind_kw_list *kwl; |
| 551 | int index; |
| 552 | |
| 553 | *out = NULL; |
| 554 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 555 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 556 | if (kwl->kw[index].parse || |
| 557 | bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 558 | memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "", |
| 559 | kwl->scope, |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 560 | kwl->kw[index].kw, |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 561 | kwl->kw[index].skip ? " <arg>" : "", |
| 562 | kwl->kw[index].parse ? "" : " (not supported)"); |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 568 | /************************************************************************/ |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 569 | /* All supported sample and ACL keywords must be declared here. */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 570 | /************************************************************************/ |
| 571 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 572 | /* 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] | 573 | static int |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 574 | smp_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 575 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 576 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 577 | smp->type = SMP_T_UINT; |
| 578 | smp->data.uint = l4->listener->nbconn; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 579 | return 1; |
| 580 | } |
| 581 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 582 | /* set temp integer to the id of the socket (listener) */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 583 | static int |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 584 | smp_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 585 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 586 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 587 | smp->type = SMP_T_UINT; |
| 588 | smp->data.uint = l4->listener->luid; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 589 | return 1; |
| 590 | } |
| 591 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 592 | /* parse the "accept-proxy" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 593 | static int bind_parse_accept_proxy(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 594 | { |
| 595 | struct listener *l; |
| 596 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 597 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 598 | l->options |= LI_O_ACC_PROXY; |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /* parse the "backlog" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 604 | static int bind_parse_backlog(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 605 | { |
| 606 | struct listener *l; |
| 607 | int val; |
| 608 | |
| 609 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 610 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 611 | return ERR_ALERT | ERR_FATAL; |
| 612 | } |
| 613 | |
| 614 | val = atol(args[cur_arg + 1]); |
| 615 | if (val <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 616 | memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 617 | return ERR_ALERT | ERR_FATAL; |
| 618 | } |
| 619 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 620 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 621 | l->backlog = val; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* parse the "id" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 627 | static int bind_parse_id(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 628 | { |
| 629 | struct eb32_node *node; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 630 | struct listener *l, *new; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 631 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 632 | if (conf->listeners.n != conf->listeners.p) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 633 | memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 634 | return ERR_ALERT | ERR_FATAL; |
| 635 | } |
| 636 | |
| 637 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 638 | memprintf(err, "'%s' : expects an integer argument", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 639 | return ERR_ALERT | ERR_FATAL; |
| 640 | } |
| 641 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 642 | new = LIST_NEXT(&conf->listeners, struct listener *, by_bind); |
| 643 | new->luid = atol(args[cur_arg + 1]); |
| 644 | new->conf.id.key = new->luid; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 645 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 646 | if (new->luid <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 647 | memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 648 | return ERR_ALERT | ERR_FATAL; |
| 649 | } |
| 650 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 651 | node = eb32_lookup(&px->conf.used_listener_id, new->luid); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 652 | if (node) { |
| 653 | l = container_of(node, struct listener, conf.id); |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 654 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')", |
| 655 | args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line, |
| 656 | l->bind_conf->arg); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 657 | return ERR_ALERT | ERR_FATAL; |
| 658 | } |
| 659 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 660 | eb32_insert(&px->conf.used_listener_id, &new->conf.id); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /* parse the "maxconn" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 665 | static int bind_parse_maxconn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 666 | { |
| 667 | struct listener *l; |
| 668 | int val; |
| 669 | |
| 670 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 671 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 672 | return ERR_ALERT | ERR_FATAL; |
| 673 | } |
| 674 | |
| 675 | val = atol(args[cur_arg + 1]); |
| 676 | if (val <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 677 | memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 678 | return ERR_ALERT | ERR_FATAL; |
| 679 | } |
| 680 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 681 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 682 | l->maxconn = val; |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | /* parse the "name" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 688 | static int bind_parse_name(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 689 | { |
| 690 | struct listener *l; |
| 691 | |
| 692 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 693 | memprintf(err, "'%s' : missing name", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 694 | return ERR_ALERT | ERR_FATAL; |
| 695 | } |
| 696 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 697 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 698 | l->name = strdup(args[cur_arg + 1]); |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | /* parse the "nice" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 704 | static int bind_parse_nice(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 705 | { |
| 706 | struct listener *l; |
| 707 | int val; |
| 708 | |
| 709 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 710 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 711 | return ERR_ALERT | ERR_FATAL; |
| 712 | } |
| 713 | |
| 714 | val = atol(args[cur_arg + 1]); |
| 715 | if (val < -1024 || val > 1024) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 716 | memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 717 | return ERR_ALERT | ERR_FATAL; |
| 718 | } |
| 719 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 720 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 721 | l->nice = val; |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 726 | /* parse the "process" bind keyword */ |
| 727 | static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 728 | { |
| 729 | unsigned long set = 0; |
| 730 | unsigned int low, high; |
| 731 | |
| 732 | if (strcmp(args[cur_arg + 1], "all") == 0) { |
| 733 | set = 0; |
| 734 | } |
| 735 | else if (strcmp(args[cur_arg + 1], "odd") == 0) { |
| 736 | set |= ~0UL/3UL; /* 0x555....555 */ |
| 737 | } |
| 738 | else if (strcmp(args[cur_arg + 1], "even") == 0) { |
| 739 | set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */ |
| 740 | } |
| 741 | else if (isdigit((int)*args[cur_arg + 1])) { |
| 742 | char *dash = strchr(args[cur_arg + 1], '-'); |
| 743 | |
| 744 | low = high = str2uic(args[cur_arg + 1]); |
| 745 | if (dash) |
| 746 | high = str2uic(dash + 1); |
| 747 | |
| 748 | if (high < low) { |
| 749 | unsigned int swap = low; |
| 750 | low = high; |
| 751 | high = swap; |
| 752 | } |
| 753 | |
| 754 | if (low < 1 || high > LONGBITS) { |
| 755 | memprintf(err, "'%s' : invalid range %d-%d, allowed range is 1..%d", args[cur_arg], low, high, LONGBITS); |
| 756 | return ERR_ALERT | ERR_FATAL; |
| 757 | } |
| 758 | while (low <= high) |
| 759 | set |= 1UL << (low++ - 1); |
| 760 | } |
| 761 | else { |
| 762 | memprintf(err, "'%s' expects 'all', 'odd', 'even', or a process range with numbers from 1 to %d.", args[cur_arg], LONGBITS); |
| 763 | return ERR_ALERT | ERR_FATAL; |
| 764 | } |
| 765 | |
| 766 | conf->bind_proc = set; |
| 767 | return 0; |
| 768 | } |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 769 | |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 770 | /* Note: must not be declared <const> as its list will be overwritten. |
| 771 | * Please take care of keeping this list alphabetically sorted. |
| 772 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 773 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 774 | { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, }, |
| 775 | { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, }, |
| 776 | { /* END */ }, |
| 777 | }}; |
| 778 | |
| 779 | /* Note: must not be declared <const> as its list will be overwritten. |
| 780 | * Please take care of keeping this list alphabetically sorted. |
| 781 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 782 | static struct acl_kw_list acl_kws = {ILH, { |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 783 | { /* END */ }, |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 784 | }}; |
| 785 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 786 | /* Note: must not be declared <const> as its list will be overwritten. |
| 787 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 788 | * all code contributors. |
| 789 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 790 | * the config parser can report an appropriate error when a known keyword was |
| 791 | * not enabled. |
| 792 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 793 | static struct bind_kw_list bind_kws = { "ALL", { }, { |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 794 | { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */ |
| 795 | { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */ |
| 796 | { "id", bind_parse_id, 1 }, /* set id of listening socket */ |
| 797 | { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */ |
| 798 | { "name", bind_parse_name, 1 }, /* set name of listening socket */ |
| 799 | { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */ |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 800 | { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */ |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 801 | { /* END */ }, |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 802 | }}; |
| 803 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 804 | __attribute__((constructor)) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 805 | static void __listener_init(void) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 806 | { |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 807 | sample_register_fetches(&smp_kws); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 808 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 809 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Local variables: |
| 814 | * c-indent-level: 8 |
| 815 | * c-basic-offset: 8 |
| 816 | * End: |
| 817 | */ |