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