blob: b4e0bc20b24cf532d8754eb79f1502671acaaffd [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreaud1d54542012-09-12 22:58:11 +02002 * Listener management functions.
Willy Tarreaudd815982007-10-16 12:25:14 +02003 *
Willy Tarreau0ccb7442013-01-07 22:54:17 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaudd815982007-10-16 12:25:14 +02005 *
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 Tarreau44489252014-01-14 17:52:01 +010013#define _GNU_SOURCE
Willy Tarreau6ae1ba62014-05-07 19:01:58 +020014#include <ctype.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020015#include <errno.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020016#include <stdio.h>
17#include <string.h>
Willy Tarreau95ccdde2014-02-01 09:28:36 +010018#include <unistd.h>
19#include <fcntl.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020020
Willy Tarreau1bc4aab2012-10-08 20:11:03 +020021#include <common/accept4.h>
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +010022#include <common/cfgparse.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020023#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010024#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010025#include <common/initcall.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020026#include <common/mini-clist.h>
27#include <common/standard.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020028#include <common/time.h>
29
30#include <types/global.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020031#include <types/protocol.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020032
Willy Tarreau645513a2010-05-24 20:55:15 +020033#include <proto/acl.h>
Christopher Fauleta717b992018-04-10 14:43:00 +020034#include <proto/connection.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010035#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020036#include <proto/freq_ctr.h>
37#include <proto/log.h>
Willy Tarreau7a798e52016-04-14 11:13:20 +020038#include <proto/listener.h>
Willy Tarreau0de59fd2017-09-15 08:10:44 +020039#include <proto/protocol.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020040#include <proto/proto_sockpair.h>
Willy Tarreau0ccb7442013-01-07 22:54:17 +010041#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020042#include <proto/stream.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020043#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010044
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020045 /* listner_queue lock (same for global and per proxy queues) */
Willy Tarreau86abe442018-11-25 20:12:18 +010046__decl_spinlock(lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020047
Willy Tarreau26982662012-09-12 23:17:10 +020048/* List head of all known bind keywords */
49static struct bind_kw_list bind_keywords = {
50 .list = LIST_HEAD_INIT(bind_keywords.list)
51};
52
Olivier Houchardf73629d2017-04-05 22:33:04 +020053struct xfer_sock_list *xfer_sock_list = NULL;
54
Willy Tarreaudabf2e22007-10-28 21:59:24 +010055/* This function adds the specified listener's file descriptor to the polling
56 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Willy Tarreauae302532014-05-07 19:22:24 +020057 * LI_FULL state depending on its number of connections. In deamon mode, we
58 * also support binding only the relevant processes to their respective
59 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010060 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +020061static void enable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +010062{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010063 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010064 if (listener->state == LI_LISTEN) {
William Lallemand095ba4c2017-06-01 17:38:50 +020065 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau6daac192019-02-02 17:39:53 +010066 !(proc_mask(listener->bind_conf->bind_proc) & pid_bit)) {
Willy Tarreauae302532014-05-07 19:22:24 +020067 /* we don't want to enable this listener and don't
68 * want any fd event to reach it.
69 */
Olivier Houchard1fc05162017-04-06 01:05:05 +020070 if (!(global.tune.options & GTUNE_SOCKET_TRANSFER))
Christopher Faulet510c0d62018-03-16 10:04:47 +010071 do_unbind_listener(listener, 1);
Olivier Houchard1fc05162017-04-06 01:05:05 +020072 else {
Christopher Faulet510c0d62018-03-16 10:04:47 +010073 do_unbind_listener(listener, 0);
Olivier Houchard1fc05162017-04-06 01:05:05 +020074 listener->state = LI_LISTEN;
75 }
Willy Tarreauae302532014-05-07 19:22:24 +020076 }
77 else if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020078 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010079 listener->state = LI_READY;
Willy Tarreauae302532014-05-07 19:22:24 +020080 }
81 else {
Willy Tarreaudabf2e22007-10-28 21:59:24 +010082 listener->state = LI_FULL;
83 }
84 }
William Lallemande22f11f2018-09-11 10:06:27 +020085 /* if this listener is supposed to be only in the master, close it in the workers */
86 if ((global.mode & MODE_MWORKER) &&
87 (listener->options & LI_O_MWORKER) &&
88 master == 0) {
89 do_unbind_listener(listener, 1);
90 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +010091 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010092}
93
94/* This function removes the specified listener's file descriptor from the
95 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
96 * enters LI_LISTEN.
97 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +020098static void disable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +010099{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100100 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100101 if (listener->state < LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200102 goto end;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100103 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200104 fd_stop_recv(listener->fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200105 if (listener->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100106 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200107 LIST_DEL(&listener->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100108 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200109 }
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100110 listener->state = LI_LISTEN;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200111 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100112 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100113}
114
Willy Tarreaube58c382011-07-24 18:28:10 +0200115/* This function tries to temporarily disable a listener, depending on the OS
116 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
117 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
118 * closes upon SHUT_WR and refuses to rebind. So a common validation path
119 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
120 * is disabled. It normally returns non-zero, unless an error is reported.
121 */
122int pause_listener(struct listener *l)
123{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200124 int ret = 1;
125
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100126 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200127
Olivier Houchard1fc05162017-04-06 01:05:05 +0200128 if (l->state <= LI_ZOMBIE)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200129 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200130
Willy Tarreau092d8652014-07-07 20:22:12 +0200131 if (l->proto->pause) {
132 /* Returns < 0 in case of failure, 0 if the listener
133 * was totally stopped, or > 0 if correctly paused.
134 */
135 int ret = l->proto->pause(l);
Willy Tarreaube58c382011-07-24 18:28:10 +0200136
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200137 if (ret < 0) {
138 ret = 0;
139 goto end;
140 }
Willy Tarreau092d8652014-07-07 20:22:12 +0200141 else if (ret == 0)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200142 goto end;
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200143 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200144
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200145 if (l->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100146 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200147 LIST_DEL(&l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100148 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200149 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200150
Willy Tarreau49b046d2012-08-09 12:11:58 +0200151 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200152 l->state = LI_PAUSED;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200153 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100154 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200155 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200156}
157
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200158/* This function tries to resume a temporarily disabled listener. Paused, full,
159 * limited and disabled listeners are handled, which means that this function
160 * may replace enable_listener(). The resulting state will either be LI_READY
161 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200162 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200163 * foreground mode, and are ignored. If the listener was only in the assigned
164 * state, it's totally rebound. This can happen if a pause() has completely
165 * stopped it. If the resume fails, 0 is returned and an error might be
166 * displayed.
Willy Tarreaube58c382011-07-24 18:28:10 +0200167 */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200168static int __resume_listener(struct listener *l)
Willy Tarreaube58c382011-07-24 18:28:10 +0200169{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200170 int ret = 1;
171
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100172 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200173
William Lallemand095ba4c2017-06-01 17:38:50 +0200174 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau6daac192019-02-02 17:39:53 +0100175 !(proc_mask(l->bind_conf->bind_proc) & pid_bit))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200176 goto end;
Willy Tarreau3569df32017-03-15 12:47:46 +0100177
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200178 if (l->state == LI_ASSIGNED) {
179 char msg[100];
180 int err;
181
182 err = l->proto->bind(l, msg, sizeof(msg));
183 if (err & ERR_ALERT)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100184 ha_alert("Resuming listener: %s\n", msg);
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200185 else if (err & ERR_WARN)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100186 ha_warning("Resuming listener: %s\n", msg);
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200187
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200188 if (err & (ERR_FATAL | ERR_ABORT)) {
189 ret = 0;
190 goto end;
191 }
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200192 }
193
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200194 if (l->state < LI_PAUSED || l->state == LI_ZOMBIE) {
195 ret = 0;
196 goto end;
197 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200198
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200199 if (l->proto->sock_prot == IPPROTO_TCP &&
200 l->state == LI_PAUSED &&
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200201 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) {
202 ret = 0;
203 goto end;
204 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200205
206 if (l->state == LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200207 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200208
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200209 if (l->state == LI_LIMITED)
210 LIST_DEL(&l->wait_queue);
211
Willy Tarreaube58c382011-07-24 18:28:10 +0200212 if (l->nbconn >= l->maxconn) {
213 l->state = LI_FULL;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200214 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200215 }
216
Willy Tarreau49b046d2012-08-09 12:11:58 +0200217 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200218 l->state = LI_READY;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200219 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100220 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200221 return ret;
222}
223
224int resume_listener(struct listener *l)
225{
226 int ret;
227
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100228 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200229 ret = __resume_listener(l);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100230 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200231 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200232}
233
Willy Tarreau87b09662015-04-03 00:22:06 +0200234/* Marks a ready listener as full so that the stream code tries to re-enable
Willy Tarreau62793712011-07-24 19:23:38 +0200235 * it upon next close() using resume_listener().
236 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200237static void listener_full(struct listener *l)
Willy Tarreau62793712011-07-24 19:23:38 +0200238{
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100239 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200240 if (l->state >= LI_READY) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200241 if (l->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100242 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200243 LIST_DEL(&l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100244 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200245 }
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100246 if (l->state != LI_FULL) {
247 fd_stop_recv(l->fd);
248 l->state = LI_FULL;
249 }
Willy Tarreau62793712011-07-24 19:23:38 +0200250 }
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100251 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200252}
253
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200254/* Marks a ready listener as limited so that we only try to re-enable it when
255 * resources are free again. It will be queued into the specified queue.
256 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200257static void limit_listener(struct listener *l, struct list *list)
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200258{
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100259 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200260 if (l->state == LI_READY) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100261 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200262 LIST_ADDQ(list, &l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100263 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200264 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200265 l->state = LI_LIMITED;
266 }
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100267 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200268}
269
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100270/* This function adds all of the protocol's listener's file descriptors to the
271 * polling lists when they are in the LI_LISTEN state. It is intended to be
272 * used as a protocol's generic enable_all() primitive, for use after the
273 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
274 * their number of connections. It always returns ERR_NONE.
275 */
276int enable_all_listeners(struct protocol *proto)
277{
278 struct listener *listener;
279
280 list_for_each_entry(listener, &proto->listeners, proto_list)
281 enable_listener(listener);
282 return ERR_NONE;
283}
284
285/* This function removes all of the protocol's listener's file descriptors from
286 * the polling lists when they are in the LI_READY or LI_FULL states. It is
287 * intended to be used as a protocol's generic disable_all() primitive. It puts
288 * the listeners into LI_LISTEN, and always returns ERR_NONE.
289 */
290int disable_all_listeners(struct protocol *proto)
291{
292 struct listener *listener;
293
294 list_for_each_entry(listener, &proto->listeners, proto_list)
295 disable_listener(listener);
296 return ERR_NONE;
297}
298
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200299/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
300void dequeue_all_listeners(struct list *list)
301{
302 struct listener *listener, *l_back;
303
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100304 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200305 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
306 /* This cannot fail because the listeners are by definition in
307 * the LI_LIMITED state. The function also removes the entry
308 * from the queue.
309 */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200310 __resume_listener(listener);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200311 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100312 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200313}
314
Christopher Faulet510c0d62018-03-16 10:04:47 +0100315/* Must be called with the lock held. Depending on <do_close> value, it does
316 * what unbind_listener or unbind_listener_no_close should do.
317 */
318void do_unbind_listener(struct listener *listener, int do_close)
Willy Tarreaub648d632007-10-28 22:13:50 +0100319{
320 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200321 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100322
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200323 if (listener->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100324 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200325 LIST_DEL(&listener->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100326 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200327 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200328
Willy Tarreaube58c382011-07-24 18:28:10 +0200329 if (listener->state >= LI_PAUSED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +0200330 if (do_close) {
331 fd_delete(listener->fd);
332 listener->fd = -1;
333 }
334 else
335 fd_remove(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100336 listener->state = LI_ASSIGNED;
337 }
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100338}
339
Olivier Houchard1fc05162017-04-06 01:05:05 +0200340/* This function closes the listening socket for the specified listener,
341 * provided that it's already in a listening state. The listener enters the
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100342 * LI_ASSIGNED state. This function is intended to be used as a generic
343 * function for standard protocols.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200344 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100345void unbind_listener(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200346{
Christopher Faulet510c0d62018-03-16 10:04:47 +0100347 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100348 do_unbind_listener(listener, 1);
Christopher Faulet510c0d62018-03-16 10:04:47 +0100349 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200350}
351
352/* This function pretends the listener is dead, but keeps the FD opened, so
353 * that we can provide it, for conf reloading.
354 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100355void unbind_listener_no_close(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200356{
Christopher Faulet510c0d62018-03-16 10:04:47 +0100357 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100358 do_unbind_listener(listener, 0);
Christopher Faulet510c0d62018-03-16 10:04:47 +0100359 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200360}
361
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100362/* This function closes all listening sockets bound to the protocol <proto>,
363 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
364 * detach them from the protocol. It always returns ERR_NONE.
365 */
366int unbind_all_listeners(struct protocol *proto)
367{
368 struct listener *listener;
369
370 list_for_each_entry(listener, &proto->listeners, proto_list)
371 unbind_listener(listener);
372 return ERR_NONE;
373}
374
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200375/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
376 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
377 * allocation). The address family is taken from ss->ss_family. The number of
378 * jobs and listeners is automatically increased by the number of listeners
William Lallemand75ea0a02017-11-15 19:02:58 +0100379 * created. If the <inherited> argument is set to 1, it specifies that the FD
380 * was obtained from a parent process.
381 * It returns non-zero on success, zero on error with the error message
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200382 * set in <err>.
383 */
384int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
William Lallemand75ea0a02017-11-15 19:02:58 +0100385 int portl, int porth, int fd, int inherited, char **err)
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200386{
387 struct protocol *proto = protocol_by_family(ss->ss_family);
388 struct listener *l;
389 int port;
390
391 if (!proto) {
392 memprintf(err, "unsupported protocol family %d", ss->ss_family);
393 return 0;
394 }
395
396 for (port = portl; port <= porth; port++) {
397 l = calloc(1, sizeof(*l));
398 if (!l) {
399 memprintf(err, "out of memory");
400 return 0;
401 }
402 l->obj_type = OBJ_TYPE_LISTENER;
403 LIST_ADDQ(&bc->frontend->conf.listeners, &l->by_fe);
404 LIST_ADDQ(&bc->listeners, &l->by_bind);
405 l->bind_conf = bc;
406
407 l->fd = fd;
408 memcpy(&l->addr, ss, sizeof(*ss));
409 l->state = LI_INIT;
410
411 proto->add(l, port);
412
William Lallemand75ea0a02017-11-15 19:02:58 +0100413 if (inherited)
414 l->options |= LI_O_INHERITED;
415
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100416 HA_SPIN_INIT(&l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200417 HA_ATOMIC_ADD(&jobs, 1);
418 HA_ATOMIC_ADD(&listeners, 1);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200419 }
420 return 1;
421}
422
Willy Tarreau1a64d162007-10-28 22:26:05 +0100423/* Delete a listener from its protocol's list of listeners. The listener's
424 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
Willy Tarreau2cc5bae2017-09-15 08:18:11 +0200425 * number of listeners is updated, as well as the global number of listeners
426 * and jobs. Note that the listener must have previously been unbound. This
427 * is the generic function to use to remove a listener.
Willy Tarreau1a64d162007-10-28 22:26:05 +0100428 */
429void delete_listener(struct listener *listener)
430{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100431 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100432 if (listener->state == LI_ASSIGNED) {
433 listener->state = LI_INIT;
434 LIST_DEL(&listener->proto_list);
435 listener->proto->nb_listeners--;
436 HA_ATOMIC_SUB(&jobs, 1);
437 HA_ATOMIC_SUB(&listeners, 1);
438 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100439 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100440}
441
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200442/* This function is called on a read event from a listening socket, corresponding
443 * to an accept. It tries to accept as many connections as possible, and for each
444 * calls the listener's accept handler (generally the frontend's accept handler).
445 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200446void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200447{
448 struct listener *l = fdtab[fd].owner;
Olivier Houchardd16a9df2019-02-25 16:18:16 +0100449 struct proxy *p;
450 int max_accept;
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100451 int next_conn = 0;
Willy Tarreaubb660302014-05-07 19:47:02 +0200452 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200453 int cfd;
454 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100455#ifdef USE_ACCEPT4
456 static int accept4_broken;
457#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200458
Olivier Houchardd16a9df2019-02-25 16:18:16 +0100459 if (!l)
460 return;
461 p = l->bind_conf->frontend;
462 max_accept = l->maxaccept ? l->maxaccept : 1;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200463
Willy Tarreau93e7c002013-10-07 18:51:07 +0200464 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
465 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200466
467 if (unlikely(!max)) {
468 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200469 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200470 goto wait_expire;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200471 }
472
473 if (max_accept > max)
474 max_accept = max;
475 }
476
477 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200478 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
479
480 if (unlikely(!max)) {
481 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200482 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200483 goto wait_expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200484 }
485
486 if (max_accept > max)
487 max_accept = max;
488 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200489#ifdef USE_OPENSSL
490 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
491 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200492
Willy Tarreaue43d5322013-10-07 20:01:52 +0200493 if (unlikely(!max)) {
494 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200495 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200496 goto wait_expire;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200497 }
498
499 if (max_accept > max)
500 max_accept = max;
501 }
502#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200503 if (p && p->fe_sps_lim) {
504 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
505
506 if (unlikely(!max)) {
507 /* frontend accept rate limit was reached */
508 limit_listener(l, &p->listener_queue);
509 task_schedule(p->task, tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0)));
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200510 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200511 }
512
513 if (max_accept > max)
514 max_accept = max;
515 }
516
517 /* Note: if we fail to allocate a connection because of configured
518 * limits, we'll schedule a new attempt worst 1 second later in the
519 * worst case. If we fail due to system limits or temporary resource
520 * shortage, we try again 100ms later in the worst case.
521 */
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100522 for (; max_accept-- > 0; next_conn = 0) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200523 struct sockaddr_storage addr;
524 socklen_t laddr = sizeof(addr);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200525 unsigned int count;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200526
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100527 /* pre-increase the number of connections without going too far */
528 do {
529 count = l->nbconn;
530 if (count >= l->maxconn) {
531 /* the listener was marked full or another
532 * thread is going to do it.
533 */
534 next_conn = 0;
535 goto end;
536 }
537 next_conn = count + 1;
538 } while (!HA_ATOMIC_CAS(&l->nbconn, &count, next_conn));
539
540 if (next_conn == l->maxconn) {
541 /* we filled it, mark it full */
542 listener_full(l);
543 }
544
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200545 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
546 limit_listener(l, &global_listener_queue);
547 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200548 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200549 }
550
551 if (unlikely(p && p->feconn >= p->maxconn)) {
552 limit_listener(l, &p->listener_queue);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200553 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200554 }
555
William Lallemand2fe7dd02018-09-11 16:51:29 +0200556 /* with sockpair@ we don't want to do an accept */
557 if (unlikely(l->addr.ss_family == AF_CUST_SOCKPAIR)) {
558 if ((cfd = recv_fd_uxst(fd)) != -1)
William Lallemandd9138002018-11-27 12:02:39 +0100559 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau888d5672019-01-27 18:34:12 +0100560 /* just like with UNIX sockets, only the family is filled */
561 addr.ss_family = AF_UNIX;
562 laddr = sizeof(addr.ss_family);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200563 } else
564
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200565#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100566 /* only call accept4() if it's known to be safe, otherwise
567 * fallback to the legacy accept() + fcntl().
568 */
569 if (unlikely(accept4_broken ||
William Lallemandd9138002018-11-27 12:02:39 +0100570 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
Willy Tarreau818dca52014-01-31 19:40:19 +0100571 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
572 (accept4_broken = 1))))
573#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200574 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
William Lallemandd9138002018-11-27 12:02:39 +0100575 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100576
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200577 if (unlikely(cfd == -1)) {
578 switch (errno) {
579 case EAGAIN:
Willy Tarreaubb660302014-05-07 19:47:02 +0200580 if (fdtab[fd].ev & FD_POLL_HUP) {
581 /* the listening socket might have been disabled in a shared
582 * process and we're a collateral victim. We'll just pause for
583 * a while in case it comes back. In the mean time, we need to
584 * clear this sticky flag.
585 */
586 fdtab[fd].ev &= ~FD_POLL_HUP;
587 goto transient_error;
588 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100589 fd_cant_recv(fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200590 goto end; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200591 case EINVAL:
592 /* might be trying to accept on a shut fd (eg: soft stop) */
593 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100594 case EINTR:
595 case ECONNABORTED:
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100596 HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreaua593ec52014-01-20 21:21:30 +0100597 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200598 case ENFILE:
599 if (p)
600 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100601 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
602 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200603 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200604 case EMFILE:
605 if (p)
606 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100607 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
608 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200609 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200610 case ENOBUFS:
611 case ENOMEM:
612 if (p)
613 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100614 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
615 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200616 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200617 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100618 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100619 goto stop;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200620 }
621 }
622
William Lallemandd9138002018-11-27 12:02:39 +0100623 /* we don't want to leak the FD upon reload if it's in the master */
624 if (unlikely(master == 1))
625 fcntl(cfd, F_SETFD, FD_CLOEXEC);
626
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100627 /* The connection was accepted, it must be counted as such */
628 if (l->counters)
629 HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, next_conn);
630
631 if (!(l->options & LI_O_UNLIMITED)) {
632 count = update_freq_ctr(&global.conn_per_sec, 1);
633 HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
634 HA_ATOMIC_ADD(&actconn, 1);
635 }
636
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200637 if (unlikely(cfd >= global.maxsock)) {
638 send_log(p, LOG_EMERG,
639 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
640 p->id);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100641 if (!(l->options & LI_O_UNLIMITED))
642 HA_ATOMIC_SUB(&actconn, 1);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200643 close(cfd);
644 limit_listener(l, &global_listener_queue);
645 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200646 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200647 }
648
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100649 /* past this point, l->accept() will automatically decrement
650 * l->nbconn and actconn once done. Setting next_conn=0 allows
651 * the error path not to rollback on nbconn. It's more convenient
652 * than duplicating all exit labels.
653 */
654 next_conn = 0;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200655
656 ret = l->accept(l, cfd, &addr);
657 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200658 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200659 * we just have to ignore it (ret == 0) or it's a critical
660 * error due to a resource shortage, and we must stop the
661 * listener (ret < 0).
662 */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200663 if (ret == 0) /* successful termination */
664 continue;
665
Willy Tarreaubb660302014-05-07 19:47:02 +0200666 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200667 }
668
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100669 /* increase the per-process number of cumulated sessions, this
670 * may only be done once l->accept() has accepted the connection.
671 */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200672 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200673 count = update_freq_ctr(&global.sess_per_sec, 1);
674 HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200675 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200676#ifdef USE_OPENSSL
677 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200678 count = update_freq_ctr(&global.ssl_per_sec, 1);
679 HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
Willy Tarreaue43d5322013-10-07 20:01:52 +0200680 }
681#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200682
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100683 } /* end of for (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200684
Willy Tarreauaece46a2012-07-06 12:25:58 +0200685 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100686 stop:
687 fd_done_recv(fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200688 goto end;
Willy Tarreaubb660302014-05-07 19:47:02 +0200689
690 transient_error:
691 /* pause the listener and try again in 100 ms */
692 expire = tick_add(now_ms, 100);
693
694 wait_expire:
695 limit_listener(l, &global_listener_queue);
696 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200697 end:
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100698 if (next_conn)
699 HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreau741b4d62019-02-25 15:02:04 +0100700
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100701 if (l->nbconn < l->maxconn && l->state == LI_FULL) {
702 /* at least one thread has to this when quitting */
703 resume_listener(l);
704
705 /* Dequeues all of the listeners waiting for a resource */
706 if (!LIST_ISEMPTY(&global_listener_queue))
707 dequeue_all_listeners(&global_listener_queue);
708
709 if (!LIST_ISEMPTY(&p->listener_queue) &&
710 (!p->fe_sps_lim || freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0) > 0))
711 dequeue_all_listeners(&p->listener_queue);
712 }
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200713}
714
Willy Tarreau05f50472017-09-15 09:19:58 +0200715/* Notify the listener that a connection initiated from it was released. This
716 * is used to keep the connection count consistent and to possibly re-open
717 * listening when it was limited.
718 */
719void listener_release(struct listener *l)
720{
721 struct proxy *fe = l->bind_conf->frontend;
722
723 if (!(l->options & LI_O_UNLIMITED))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200724 HA_ATOMIC_SUB(&actconn, 1);
725 HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreau05f50472017-09-15 09:19:58 +0200726 if (l->state == LI_FULL)
727 resume_listener(l);
728
729 /* Dequeues all of the listeners waiting for a resource */
730 if (!LIST_ISEMPTY(&global_listener_queue))
731 dequeue_all_listeners(&global_listener_queue);
732
733 if (!LIST_ISEMPTY(&fe->listener_queue) &&
734 (!fe->fe_sps_lim || freq_ctr_remain(&fe->fe_sess_per_sec, fe->fe_sps_lim, 0) > 0))
735 dequeue_all_listeners(&fe->listener_queue);
736}
737
Willy Tarreau26982662012-09-12 23:17:10 +0200738/*
739 * Registers the bind keyword list <kwl> as a list of valid keywords for next
740 * parsing sessions.
741 */
742void bind_register_keywords(struct bind_kw_list *kwl)
743{
744 LIST_ADDQ(&bind_keywords.list, &kwl->list);
745}
746
747/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
748 * keyword is found with a NULL ->parse() function, then an attempt is made to
749 * find one with a valid ->parse() function. This way it is possible to declare
750 * platform-dependant, known keywords as NULL, then only declare them as valid
751 * if some options are met. Note that if the requested keyword contains an
752 * opening parenthesis, everything from this point is ignored.
753 */
754struct bind_kw *bind_find_kw(const char *kw)
755{
756 int index;
757 const char *kwend;
758 struct bind_kw_list *kwl;
759 struct bind_kw *ret = NULL;
760
761 kwend = strchr(kw, '(');
762 if (!kwend)
763 kwend = kw + strlen(kw);
764
765 list_for_each_entry(kwl, &bind_keywords.list, list) {
766 for (index = 0; kwl->kw[index].kw != NULL; index++) {
767 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
768 kwl->kw[index].kw[kwend-kw] == 0) {
769 if (kwl->kw[index].parse)
770 return &kwl->kw[index]; /* found it !*/
771 else
772 ret = &kwl->kw[index]; /* may be OK */
773 }
774 }
775 }
776 return ret;
777}
778
Willy Tarreau8638f482012-09-18 18:01:17 +0200779/* Dumps all registered "bind" keywords to the <out> string pointer. The
780 * unsupported keywords are only dumped if their supported form was not
781 * found.
782 */
783void bind_dump_kws(char **out)
784{
785 struct bind_kw_list *kwl;
786 int index;
787
788 *out = NULL;
789 list_for_each_entry(kwl, &bind_keywords.list, list) {
790 for (index = 0; kwl->kw[index].kw != NULL; index++) {
791 if (kwl->kw[index].parse ||
792 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200793 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
794 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200795 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200796 kwl->kw[index].skip ? " <arg>" : "",
797 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200798 }
799 }
800 }
801}
802
Willy Tarreau645513a2010-05-24 20:55:15 +0200803/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100804/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200805/************************************************************************/
806
Willy Tarreaua5e37562011-12-16 17:06:15 +0100807/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200808static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200809smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +0200810{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200811 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200812 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200813 return 1;
814}
815
Willy Tarreaua5e37562011-12-16 17:06:15 +0100816/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200817static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200818smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +0200819{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200820 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200821 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200822 return 1;
823}
824
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200825/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200826static int bind_parse_accept_proxy(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200827{
828 struct listener *l;
829
Willy Tarreau4348fad2012-09-20 16:48:07 +0200830 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200831 l->options |= LI_O_ACC_PROXY;
832
833 return 0;
834}
835
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100836/* parse the "accept-netscaler-cip" bind keyword */
837static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
838{
839 struct listener *l;
840 uint32_t val;
841
842 if (!*args[cur_arg + 1]) {
843 memprintf(err, "'%s' : missing value", args[cur_arg]);
844 return ERR_ALERT | ERR_FATAL;
845 }
846
847 val = atol(args[cur_arg + 1]);
848 if (val <= 0) {
849 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
850 return ERR_ALERT | ERR_FATAL;
851 }
852
853 list_for_each_entry(l, &conf->listeners, by_bind) {
854 l->options |= LI_O_ACC_CIP;
855 conf->ns_cip_magic = val;
856 }
857
858 return 0;
859}
860
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200861/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200862static int bind_parse_backlog(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200863{
864 struct listener *l;
865 int val;
866
867 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200868 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200869 return ERR_ALERT | ERR_FATAL;
870 }
871
872 val = atol(args[cur_arg + 1]);
873 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200874 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200875 return ERR_ALERT | ERR_FATAL;
876 }
877
Willy Tarreau4348fad2012-09-20 16:48:07 +0200878 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200879 l->backlog = val;
880
881 return 0;
882}
883
884/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200885static int bind_parse_id(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200886{
887 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200888 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100889 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200890
Willy Tarreau4348fad2012-09-20 16:48:07 +0200891 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200892 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200893 return ERR_ALERT | ERR_FATAL;
894 }
895
896 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200897 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200898 return ERR_ALERT | ERR_FATAL;
899 }
900
Willy Tarreau4348fad2012-09-20 16:48:07 +0200901 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100902 new->luid = strtol(args[cur_arg + 1], &error, 10);
903 if (*error != '\0') {
904 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
905 return ERR_ALERT | ERR_FATAL;
906 }
Willy Tarreau4348fad2012-09-20 16:48:07 +0200907 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200908
Willy Tarreau4348fad2012-09-20 16:48:07 +0200909 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200910 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200911 return ERR_ALERT | ERR_FATAL;
912 }
913
Willy Tarreau4348fad2012-09-20 16:48:07 +0200914 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200915 if (node) {
916 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200917 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
918 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
919 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200920 return ERR_ALERT | ERR_FATAL;
921 }
922
Willy Tarreau4348fad2012-09-20 16:48:07 +0200923 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200924 return 0;
925}
926
927/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200928static int bind_parse_maxconn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200929{
930 struct listener *l;
931 int val;
932
933 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200934 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200935 return ERR_ALERT | ERR_FATAL;
936 }
937
938 val = atol(args[cur_arg + 1]);
939 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200940 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200941 return ERR_ALERT | ERR_FATAL;
942 }
943
Willy Tarreau4348fad2012-09-20 16:48:07 +0200944 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200945 l->maxconn = val;
946
947 return 0;
948}
949
950/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200951static int bind_parse_name(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200952{
953 struct listener *l;
954
955 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200956 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200957 return ERR_ALERT | ERR_FATAL;
958 }
959
Willy Tarreau4348fad2012-09-20 16:48:07 +0200960 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200961 l->name = strdup(args[cur_arg + 1]);
962
963 return 0;
964}
965
966/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200967static int bind_parse_nice(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200968{
969 struct listener *l;
970 int val;
971
972 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200973 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200974 return ERR_ALERT | ERR_FATAL;
975 }
976
977 val = atol(args[cur_arg + 1]);
978 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200979 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200980 return ERR_ALERT | ERR_FATAL;
981 }
982
Willy Tarreau4348fad2012-09-20 16:48:07 +0200983 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200984 l->nice = val;
985
986 return 0;
987}
988
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200989/* parse the "process" bind keyword */
990static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
991{
Christopher Fauletc644fa92017-11-23 22:44:11 +0100992 char *slash;
993 unsigned long proc = 0, thread = 0;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200994
Christopher Fauletc644fa92017-11-23 22:44:11 +0100995 if ((slash = strchr(args[cur_arg + 1], '/')) != NULL)
996 *slash = 0;
997
Willy Tarreauff9c9142019-02-07 10:39:36 +0100998 if (parse_process_number(args[cur_arg + 1], &proc, MAX_PROCS, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +0100999 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001000 return ERR_ALERT | ERR_FATAL;
1001 }
1002
Christopher Fauletc644fa92017-11-23 22:44:11 +01001003 if (slash) {
Willy Tarreauc9a82e42019-01-26 13:25:14 +01001004 if (parse_process_number(slash+1, &thread, MAX_THREADS, NULL, err)) {
Christopher Fauletc644fa92017-11-23 22:44:11 +01001005 memprintf(err, "'%s' : %s", args[cur_arg], *err);
1006 return ERR_ALERT | ERR_FATAL;
1007 }
1008 *slash = '/';
1009 }
1010
1011 conf->bind_proc |= proc;
Willy Tarreaua36b3242019-02-02 13:14:34 +01001012 conf->bind_thread |= thread;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001013 return 0;
1014}
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001015
Christopher Fauleta717b992018-04-10 14:43:00 +02001016/* parse the "proto" bind keyword */
1017static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1018{
1019 struct ist proto;
1020
1021 if (!*args[cur_arg + 1]) {
1022 memprintf(err, "'%s' : missing value", args[cur_arg]);
1023 return ERR_ALERT | ERR_FATAL;
1024 }
1025
1026 proto = ist2(args[cur_arg + 1], strlen(args[cur_arg + 1]));
1027 conf->mux_proto = get_mux_proto(proto);
1028 if (!conf->mux_proto) {
1029 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]);
1030 return ERR_ALERT | ERR_FATAL;
1031 }
Christopher Fauleta717b992018-04-10 14:43:00 +02001032 return 0;
1033}
1034
Willy Tarreau61612d42012-04-19 18:42:05 +02001035/* Note: must not be declared <const> as its list will be overwritten.
1036 * Please take care of keeping this list alphabetically sorted.
1037 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001038static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001039 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
1040 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001041 { /* END */ },
1042}};
1043
Willy Tarreau0108d902018-11-25 19:14:37 +01001044INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1045
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001046/* Note: must not be declared <const> as its list will be overwritten.
1047 * Please take care of keeping this list alphabetically sorted.
1048 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001049static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001050 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001051}};
1052
Willy Tarreau0108d902018-11-25 19:14:37 +01001053INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
1054
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001055/* Note: must not be declared <const> as its list will be overwritten.
1056 * Please take care of keeping this list alphabetically sorted, doing so helps
1057 * all code contributors.
1058 * Optional keywords are also declared with a NULL ->parse() function so that
1059 * the config parser can report an appropriate error when a known keyword was
1060 * not enabled.
1061 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001062static struct bind_kw_list bind_kws = { "ALL", { }, {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001063 { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001064 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
1065 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
1066 { "id", bind_parse_id, 1 }, /* set id of listening socket */
1067 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
1068 { "name", bind_parse_name, 1 }, /* set name of listening socket */
1069 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001070 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Christopher Fauleta717b992018-04-10 14:43:00 +02001071 { "proto", bind_parse_proto, 1 }, /* set the proto to use for all incoming connections */
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001072 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001073}};
1074
Willy Tarreau0108d902018-11-25 19:14:37 +01001075INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1076
Willy Tarreau645513a2010-05-24 20:55:15 +02001077/*
1078 * Local variables:
1079 * c-indent-level: 8
1080 * c-basic-offset: 8
1081 * End:
1082 */