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