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