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