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