2007-10-16 10:25:14 +00:00
|
|
|
/*
|
2012-09-12 20:58:11 +00:00
|
|
|
* Listener management functions.
|
2007-10-16 10:25:14 +00:00
|
|
|
*
|
2013-01-07 21:54:17 +00:00
|
|
|
* Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
|
2007-10-16 10:25:14 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-01-14 16:52:01 +00:00
|
|
|
#define _GNU_SOURCE
|
2014-05-07 17:01:58 +00:00
|
|
|
#include <ctype.h>
|
2012-05-07 19:22:09 +00:00
|
|
|
#include <errno.h>
|
2007-10-16 10:25:14 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-02-01 08:28:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2007-10-16 10:25:14 +00:00
|
|
|
|
2020-05-27 10:58:42 +00:00
|
|
|
#include <haproxy/api.h>
|
2017-11-22 11:06:43 +00:00
|
|
|
#include <common/cfgparse.h>
|
2020-05-27 14:10:29 +00:00
|
|
|
#include <haproxy/errors.h>
|
2020-05-27 16:01:47 +00:00
|
|
|
#include <haproxy/list.h>
|
2020-06-04 12:58:24 +00:00
|
|
|
#include <haproxy/listener.h>
|
2020-06-03 16:09:46 +00:00
|
|
|
#include <haproxy/tools.h>
|
2020-06-01 09:05:15 +00:00
|
|
|
#include <haproxy/time.h>
|
2012-05-07 19:22:09 +00:00
|
|
|
|
|
|
|
#include <types/global.h>
|
2020-06-03 13:26:55 +00:00
|
|
|
#include <haproxy/protocol-t.h>
|
2007-10-16 10:25:14 +00:00
|
|
|
|
2010-05-24 18:55:15 +00:00
|
|
|
#include <proto/acl.h>
|
2018-04-10 12:43:00 +00:00
|
|
|
#include <proto/connection.h>
|
2020-06-03 17:33:00 +00:00
|
|
|
#include <haproxy/fd.h>
|
2020-06-01 10:18:08 +00:00
|
|
|
#include <haproxy/freq_ctr.h>
|
2012-05-07 19:22:09 +00:00
|
|
|
#include <proto/log.h>
|
2020-06-03 13:26:55 +00:00
|
|
|
#include <haproxy/protocol.h>
|
2020-06-04 06:41:30 +00:00
|
|
|
#include <haproxy/proto_sockpair.h>
|
2020-06-04 13:33:47 +00:00
|
|
|
#include <haproxy/sample.h>
|
2015-04-03 12:46:27 +00:00
|
|
|
#include <proto/stream.h>
|
2012-05-07 19:22:09 +00:00
|
|
|
#include <proto/task.h>
|
2007-10-28 21:13:50 +00:00
|
|
|
|
2012-09-12 21:17:10 +00:00
|
|
|
/* List head of all known bind keywords */
|
|
|
|
static struct bind_kw_list bind_keywords = {
|
|
|
|
.list = LIST_HEAD_INIT(bind_keywords.list)
|
|
|
|
};
|
|
|
|
|
2017-04-05 20:33:04 +00:00
|
|
|
struct xfer_sock_list *xfer_sock_list = NULL;
|
|
|
|
|
BUG/MAJOR: listener: fix thread safety in resume_listener()
resume_listener() can be called from a thread not part of the listener's
mask after a curr_conn has gone lower than a proxy's or the process' limit.
This results in fd_may_recv() being called unlocked if the listener is
bound to only one thread, and quickly locks up.
This patch solves this by creating a per-thread work_list dedicated to
listeners, and modifying resume_listener() so that it bounces the listener
to one of its owning thread's work_list and waking it up. This thread will
then call resume_listener() again and will perform the operation on the
file descriptor itself. It is important to do it this way so that the
listener's state cannot be modified while the listener is being moved,
otherwise multiple threads can take conflicting decisions and the listener
could be put back into the global queue if the listener was used at the
same time.
It seems like a slightly simpler approach would be possible if the locked
list API would provide the ability to return a locked element. In this
case the listener would be immediately requeued in dequeue_all_listeners()
without having to go through resume_listener() with its associated lock.
This fix must be backported to all versions having the lock-less accept
loop, which is as far as 1.8 since deadlock fixes involving this feature
had to be backported there. It is expected that the code should not differ
too much there. However, previous commit "MINOR: task: introduce work lists"
will be needed as well and should not present difficulties either. For 1.8,
the commits introducing thread_mask() and LIST_ADDED() will be needed as
well, either backporting my_flsl() or switching to my_ffsl() will be OK,
and some changes will have to be performed so that the init function is
properly called (and maybe the deinit one can be dropped).
In order to test for the fix, simply set up a multi-threaded frontend with
multiple bind lines each attached to a single thread (reproduced with 16
threads here), set up a very low maxconn value on the frontend, and inject
heavy traffic on all listeners in parallel with slightly more connections
than the configured limit ( typically +20%) so that it flips very
frequently. If the bug is still there, at some point (5-20 seconds) the
traffic will go much lower or even stop, either with spinning threads or
not.
2019-07-11 08:08:31 +00:00
|
|
|
/* there is one listener queue per thread so that a thread unblocking the
|
2020-04-02 10:25:26 +00:00
|
|
|
* global queue can wake up listeners bound only to foreign threads by
|
2019-09-24 04:55:18 +00:00
|
|
|
* moving them to the remote queues and waking up the associated tasklet.
|
BUG/MAJOR: listener: fix thread safety in resume_listener()
resume_listener() can be called from a thread not part of the listener's
mask after a curr_conn has gone lower than a proxy's or the process' limit.
This results in fd_may_recv() being called unlocked if the listener is
bound to only one thread, and quickly locks up.
This patch solves this by creating a per-thread work_list dedicated to
listeners, and modifying resume_listener() so that it bounces the listener
to one of its owning thread's work_list and waking it up. This thread will
then call resume_listener() again and will perform the operation on the
file descriptor itself. It is important to do it this way so that the
listener's state cannot be modified while the listener is being moved,
otherwise multiple threads can take conflicting decisions and the listener
could be put back into the global queue if the listener was used at the
same time.
It seems like a slightly simpler approach would be possible if the locked
list API would provide the ability to return a locked element. In this
case the listener would be immediately requeued in dequeue_all_listeners()
without having to go through resume_listener() with its associated lock.
This fix must be backported to all versions having the lock-less accept
loop, which is as far as 1.8 since deadlock fixes involving this feature
had to be backported there. It is expected that the code should not differ
too much there. However, previous commit "MINOR: task: introduce work lists"
will be needed as well and should not present difficulties either. For 1.8,
the commits introducing thread_mask() and LIST_ADDED() will be needed as
well, either backporting my_flsl() or switching to my_ffsl() will be OK,
and some changes will have to be performed so that the init function is
properly called (and maybe the deinit one can be dropped).
In order to test for the fix, simply set up a multi-threaded frontend with
multiple bind lines each attached to a single thread (reproduced with 16
threads here), set up a very low maxconn value on the frontend, and inject
heavy traffic on all listeners in parallel with slightly more connections
than the configured limit ( typically +20%) so that it flips very
frequently. If the bug is still there, at some point (5-20 seconds) the
traffic will go much lower or even stop, either with spinning threads or
not.
2019-07-11 08:08:31 +00:00
|
|
|
*/
|
|
|
|
static struct work_list *local_listener_queue;
|
|
|
|
|
2019-12-10 10:18:41 +00:00
|
|
|
/* list of the temporarily limited listeners because of lack of resource */
|
|
|
|
static struct mt_list global_listener_queue = MT_LIST_HEAD_INIT(global_listener_queue);
|
|
|
|
static struct task *global_listener_queue_task;
|
|
|
|
static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state);
|
|
|
|
|
|
|
|
|
2019-01-27 14:37:19 +00:00
|
|
|
#if defined(USE_THREAD)
|
|
|
|
|
|
|
|
struct accept_queue_ring accept_queue_rings[MAX_THREADS] __attribute__((aligned(64))) = { };
|
|
|
|
|
|
|
|
/* dequeue and process a pending connection from the local accept queue (single
|
|
|
|
* consumer). Returns the accepted fd or -1 if none was found. The listener is
|
|
|
|
* placed into *li. The address is copied into *addr for no more than *addr_len
|
|
|
|
* bytes, and the address length is returned into *addr_len.
|
|
|
|
*/
|
|
|
|
int accept_queue_pop_sc(struct accept_queue_ring *ring, struct listener **li, void *addr, int *addr_len)
|
|
|
|
{
|
|
|
|
struct accept_queue_entry *e;
|
|
|
|
unsigned int pos, next;
|
|
|
|
struct listener *ptr;
|
|
|
|
int len;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
pos = ring->head;
|
|
|
|
|
|
|
|
if (pos == ring->tail)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
next = pos + 1;
|
|
|
|
if (next >= ACCEPT_QUEUE_SIZE)
|
|
|
|
next = 0;
|
|
|
|
|
|
|
|
e = &ring->entry[pos];
|
|
|
|
|
|
|
|
/* wait for the producer to update the listener's pointer */
|
|
|
|
while (1) {
|
|
|
|
ptr = e->listener;
|
|
|
|
__ha_barrier_load();
|
|
|
|
if (ptr)
|
|
|
|
break;
|
|
|
|
pl_cpu_relax();
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = e->fd;
|
|
|
|
len = e->addr_len;
|
|
|
|
if (len > *addr_len)
|
|
|
|
len = *addr_len;
|
|
|
|
|
|
|
|
if (likely(len > 0))
|
|
|
|
memcpy(addr, &e->addr, len);
|
|
|
|
|
|
|
|
/* release the entry */
|
|
|
|
e->listener = NULL;
|
|
|
|
|
|
|
|
__ha_barrier_store();
|
|
|
|
ring->head = next;
|
|
|
|
|
|
|
|
*addr_len = len;
|
|
|
|
*li = ptr;
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* tries to push a new accepted connection <fd> into ring <ring> for listener
|
|
|
|
* <li>, from address <addr> whose length is <addr_len>. Returns non-zero if it
|
|
|
|
* succeeds, or zero if the ring is full. Supports multiple producers.
|
|
|
|
*/
|
|
|
|
int accept_queue_push_mp(struct accept_queue_ring *ring, int fd,
|
|
|
|
struct listener *li, const void *addr, int addr_len)
|
|
|
|
{
|
|
|
|
struct accept_queue_entry *e;
|
|
|
|
unsigned int pos, next;
|
|
|
|
|
|
|
|
pos = ring->tail;
|
|
|
|
do {
|
|
|
|
next = pos + 1;
|
|
|
|
if (next >= ACCEPT_QUEUE_SIZE)
|
|
|
|
next = 0;
|
|
|
|
if (next == ring->head)
|
|
|
|
return 0; // ring full
|
2019-03-08 17:52:57 +00:00
|
|
|
} while (unlikely(!_HA_ATOMIC_CAS(&ring->tail, &pos, next)));
|
2019-01-27 14:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
e = &ring->entry[pos];
|
|
|
|
|
|
|
|
if (addr_len > sizeof(e->addr))
|
|
|
|
addr_len = sizeof(e->addr);
|
|
|
|
|
|
|
|
if (addr_len)
|
|
|
|
memcpy(&e->addr, addr, addr_len);
|
|
|
|
|
|
|
|
e->addr_len = addr_len;
|
|
|
|
e->fd = fd;
|
|
|
|
|
|
|
|
__ha_barrier_store();
|
|
|
|
/* now commit the change */
|
|
|
|
|
|
|
|
e->listener = li;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* proceed with accepting new connections */
|
|
|
|
static struct task *accept_queue_process(struct task *t, void *context, unsigned short state)
|
|
|
|
{
|
|
|
|
struct accept_queue_ring *ring = context;
|
|
|
|
struct listener *li;
|
|
|
|
struct sockaddr_storage addr;
|
BUG/MEDIUM: listener: Fix how unlimited number of consecutive accepts is handled
There is a bug when global.tune.maxaccept is set to -1 (no limit). It is pretty
visible with one process (nbproc sets to 1). The functions listener_accept() and
accept_queue_process() don't expect to handle negative maxaccept values. So
instead of accepting incoming connections without any limit, none are never
accepted and HAProxy loop infinitly in the scheduler.
When there are 2 or more processes, the bug is a bit more subtile. The limit for
a listener is set to 1. So only one connection is accepted at a time by a given
listener. This happens because the listener's maxaccept value is an unsigned
integer. In check_config_validity(), it is first set to UINT_MAX (-1 casted in
an unsigned integer), and then some calculations on it leads to an integer
overflow.
To fix the bug, the listener's maxaccept value is now a signed integer. So, if a
negative value is set for global.tune.maxaccept, we keep it untouched for the
listener and no calculation is made on it. Then, in the listener code, this
signed value is casted to a unsigned one. It simplifies all tests instead of
dealing with negative values. So, it limits the number of connections accepted
at a time to UINT_MAX at most. But, honestly, it not an issue.
This patch must be backported to 1.9 and 1.8.
2019-04-30 10:17:13 +00:00
|
|
|
unsigned int max_accept;
|
2019-01-27 14:37:19 +00:00
|
|
|
int addr_len;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
|
BUG/MEDIUM: listener: Fix how unlimited number of consecutive accepts is handled
There is a bug when global.tune.maxaccept is set to -1 (no limit). It is pretty
visible with one process (nbproc sets to 1). The functions listener_accept() and
accept_queue_process() don't expect to handle negative maxaccept values. So
instead of accepting incoming connections without any limit, none are never
accepted and HAProxy loop infinitly in the scheduler.
When there are 2 or more processes, the bug is a bit more subtile. The limit for
a listener is set to 1. So only one connection is accepted at a time by a given
listener. This happens because the listener's maxaccept value is an unsigned
integer. In check_config_validity(), it is first set to UINT_MAX (-1 casted in
an unsigned integer), and then some calculations on it leads to an integer
overflow.
To fix the bug, the listener's maxaccept value is now a signed integer. So, if a
negative value is set for global.tune.maxaccept, we keep it untouched for the
listener and no calculation is made on it. Then, in the listener code, this
signed value is casted to a unsigned one. It simplifies all tests instead of
dealing with negative values. So, it limits the number of connections accepted
at a time to UINT_MAX at most. But, honestly, it not an issue.
This patch must be backported to 1.9 and 1.8.
2019-04-30 10:17:13 +00:00
|
|
|
/* if global.tune.maxaccept is -1, then max_accept is UINT_MAX. It
|
|
|
|
* is not really illimited, but it is probably enough.
|
|
|
|
*/
|
|
|
|
max_accept = global.tune.maxaccept ? global.tune.maxaccept : 64;
|
|
|
|
for (; max_accept; max_accept--) {
|
2019-01-27 14:37:19 +00:00
|
|
|
addr_len = sizeof(addr);
|
|
|
|
fd = accept_queue_pop_sc(ring, &li, &addr, &addr_len);
|
|
|
|
if (fd < 0)
|
|
|
|
break;
|
|
|
|
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_ADD(&li->thr_conn[tid], 1);
|
2019-01-27 14:37:19 +00:00
|
|
|
ret = li->accept(li, fd, &addr);
|
|
|
|
if (ret <= 0) {
|
|
|
|
/* connection was terminated by the application */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* increase the per-process number of cumulated sessions, this
|
|
|
|
* may only be done once l->accept() has accepted the connection.
|
|
|
|
*/
|
|
|
|
if (!(li->options & LI_O_UNLIMITED)) {
|
|
|
|
HA_ATOMIC_UPDATE_MAX(&global.sps_max,
|
|
|
|
update_freq_ctr(&global.sess_per_sec, 1));
|
|
|
|
if (li->bind_conf && li->bind_conf->is_ssl) {
|
|
|
|
HA_ATOMIC_UPDATE_MAX(&global.ssl_max,
|
|
|
|
update_freq_ctr(&global.ssl_per_sec, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ran out of budget ? Let's come here ASAP */
|
BUG/MEDIUM: listener: Fix how unlimited number of consecutive accepts is handled
There is a bug when global.tune.maxaccept is set to -1 (no limit). It is pretty
visible with one process (nbproc sets to 1). The functions listener_accept() and
accept_queue_process() don't expect to handle negative maxaccept values. So
instead of accepting incoming connections without any limit, none are never
accepted and HAProxy loop infinitly in the scheduler.
When there are 2 or more processes, the bug is a bit more subtile. The limit for
a listener is set to 1. So only one connection is accepted at a time by a given
listener. This happens because the listener's maxaccept value is an unsigned
integer. In check_config_validity(), it is first set to UINT_MAX (-1 casted in
an unsigned integer), and then some calculations on it leads to an integer
overflow.
To fix the bug, the listener's maxaccept value is now a signed integer. So, if a
negative value is set for global.tune.maxaccept, we keep it untouched for the
listener and no calculation is made on it. Then, in the listener code, this
signed value is casted to a unsigned one. It simplifies all tests instead of
dealing with negative values. So, it limits the number of connections accepted
at a time to UINT_MAX at most. But, honestly, it not an issue.
This patch must be backported to 1.9 and 1.8.
2019-04-30 10:17:13 +00:00
|
|
|
if (!max_accept)
|
2019-09-24 04:55:18 +00:00
|
|
|
tasklet_wakeup(ring->tasklet);
|
2019-01-27 14:37:19 +00:00
|
|
|
|
2019-09-24 04:55:18 +00:00
|
|
|
return NULL;
|
2019-01-27 14:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initializes the accept-queues. Returns 0 on success, otherwise ERR_* flags */
|
|
|
|
static int accept_queue_init()
|
|
|
|
{
|
2019-09-24 04:55:18 +00:00
|
|
|
struct tasklet *t;
|
2019-01-27 14:37:19 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < global.nbthread; i++) {
|
2019-09-24 04:55:18 +00:00
|
|
|
t = tasklet_new();
|
2019-01-27 14:37:19 +00:00
|
|
|
if (!t) {
|
|
|
|
ha_alert("Out of memory while initializing accept queue for thread %d\n", i);
|
|
|
|
return ERR_FATAL|ERR_ABORT;
|
|
|
|
}
|
2019-09-24 04:55:18 +00:00
|
|
|
t->tid = i;
|
2019-01-27 14:37:19 +00:00
|
|
|
t->process = accept_queue_process;
|
|
|
|
t->context = &accept_queue_rings[i];
|
2019-09-24 04:55:18 +00:00
|
|
|
accept_queue_rings[i].tasklet = t;
|
2019-01-27 14:37:19 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_CONFIG_POSTPARSER("multi-threaded accept queue", accept_queue_init);
|
|
|
|
|
|
|
|
#endif // USE_THREAD
|
|
|
|
|
2007-10-28 20:59:24 +00:00
|
|
|
/* This function adds the specified listener's file descriptor to the polling
|
|
|
|
* lists if it is in the LI_LISTEN state. The listener enters LI_READY or
|
2020-04-02 10:25:26 +00:00
|
|
|
* LI_FULL state depending on its number of connections. In daemon mode, we
|
2014-05-07 17:22:24 +00:00
|
|
|
* also support binding only the relevant processes to their respective
|
|
|
|
* listeners. We don't do that in debug mode however.
|
2007-10-28 20:59:24 +00:00
|
|
|
*/
|
2017-06-02 08:00:35 +00:00
|
|
|
static void enable_listener(struct listener *listener)
|
2007-10-28 20:59:24 +00:00
|
|
|
{
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
|
2007-10-28 20:59:24 +00:00
|
|
|
if (listener->state == LI_LISTEN) {
|
2017-06-01 15:38:50 +00:00
|
|
|
if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
|
2019-02-02 16:39:53 +00:00
|
|
|
!(proc_mask(listener->bind_conf->bind_proc) & pid_bit)) {
|
2014-05-07 17:22:24 +00:00
|
|
|
/* we don't want to enable this listener and don't
|
|
|
|
* want any fd event to reach it.
|
|
|
|
*/
|
2017-04-05 23:05:05 +00:00
|
|
|
if (!(global.tune.options & GTUNE_SOCKET_TRANSFER))
|
2018-03-16 09:04:47 +00:00
|
|
|
do_unbind_listener(listener, 1);
|
2017-04-05 23:05:05 +00:00
|
|
|
else {
|
2018-03-16 09:04:47 +00:00
|
|
|
do_unbind_listener(listener, 0);
|
2017-04-05 23:05:05 +00:00
|
|
|
listener->state = LI_LISTEN;
|
|
|
|
}
|
2014-05-07 17:22:24 +00:00
|
|
|
}
|
2019-02-27 15:49:00 +00:00
|
|
|
else if (!listener->maxconn || listener->nbconn < listener->maxconn) {
|
2012-08-09 10:11:58 +00:00
|
|
|
fd_want_recv(listener->fd);
|
2007-10-28 20:59:24 +00:00
|
|
|
listener->state = LI_READY;
|
2014-05-07 17:22:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-10-28 20:59:24 +00:00
|
|
|
listener->state = LI_FULL;
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 08:06:27 +00:00
|
|
|
/* if this listener is supposed to be only in the master, close it in the workers */
|
|
|
|
if ((global.mode & MODE_MWORKER) &&
|
|
|
|
(listener->options & LI_O_MWORKER) &&
|
|
|
|
master == 0) {
|
|
|
|
do_unbind_listener(listener, 1);
|
|
|
|
}
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
|
2007-10-28 20:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function removes the specified listener's file descriptor from the
|
|
|
|
* polling lists if it is in the LI_READY or in the LI_FULL state. The listener
|
|
|
|
* enters LI_LISTEN.
|
|
|
|
*/
|
2017-06-02 08:00:35 +00:00
|
|
|
static void disable_listener(struct listener *listener)
|
2007-10-28 20:59:24 +00:00
|
|
|
{
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
|
2007-10-28 20:59:24 +00:00
|
|
|
if (listener->state < LI_READY)
|
2017-05-30 13:36:50 +00:00
|
|
|
goto end;
|
2007-10-28 20:59:24 +00:00
|
|
|
if (listener->state == LI_READY)
|
2012-08-09 10:11:58 +00:00
|
|
|
fd_stop_recv(listener->fd);
|
2019-08-08 13:47:21 +00:00
|
|
|
MT_LIST_DEL(&listener->wait_queue);
|
2007-10-28 20:59:24 +00:00
|
|
|
listener->state = LI_LISTEN;
|
2017-05-30 13:36:50 +00:00
|
|
|
end:
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
|
2007-10-28 20:59:24 +00:00
|
|
|
}
|
|
|
|
|
2011-07-24 16:28:10 +00:00
|
|
|
/* This function tries to temporarily disable a listener, depending on the OS
|
|
|
|
* capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
|
|
|
|
* SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
|
|
|
|
* closes upon SHUT_WR and refuses to rebind. So a common validation path
|
|
|
|
* involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
|
|
|
|
* is disabled. It normally returns non-zero, unless an error is reported.
|
|
|
|
*/
|
|
|
|
int pause_listener(struct listener *l)
|
|
|
|
{
|
2017-05-30 13:36:50 +00:00
|
|
|
int ret = 1;
|
|
|
|
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
|
2017-05-30 13:36:50 +00:00
|
|
|
|
2017-04-05 23:05:05 +00:00
|
|
|
if (l->state <= LI_ZOMBIE)
|
2017-05-30 13:36:50 +00:00
|
|
|
goto end;
|
2011-07-24 16:28:10 +00:00
|
|
|
|
2014-07-07 18:22:12 +00:00
|
|
|
if (l->proto->pause) {
|
|
|
|
/* Returns < 0 in case of failure, 0 if the listener
|
|
|
|
* was totally stopped, or > 0 if correctly paused.
|
|
|
|
*/
|
|
|
|
int ret = l->proto->pause(l);
|
2011-07-24 16:28:10 +00:00
|
|
|
|
2017-05-30 13:36:50 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
2014-07-07 18:22:12 +00:00
|
|
|
else if (ret == 0)
|
2017-05-30 13:36:50 +00:00
|
|
|
goto end;
|
2012-10-04 06:56:31 +00:00
|
|
|
}
|
2011-07-24 16:28:10 +00:00
|
|
|
|
2019-08-08 13:47:21 +00:00
|
|
|
MT_LIST_DEL(&l->wait_queue);
|
2011-07-24 20:03:52 +00:00
|
|
|
|
2012-08-09 10:11:58 +00:00
|
|
|
fd_stop_recv(l->fd);
|
2011-07-24 16:28:10 +00:00
|
|
|
l->state = LI_PAUSED;
|
2017-05-30 13:36:50 +00:00
|
|
|
end:
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
|
2017-05-30 13:36:50 +00:00
|
|
|
return ret;
|
2011-07-24 16:28:10 +00:00
|
|
|
}
|
|
|
|
|
2011-07-24 20:03:52 +00:00
|
|
|
/* This function tries to resume a temporarily disabled listener. Paused, full,
|
|
|
|
* limited and disabled listeners are handled, which means that this function
|
|
|
|
* may replace enable_listener(). The resulting state will either be LI_READY
|
|
|
|
* or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
|
2014-05-07 17:22:24 +00:00
|
|
|
* Listeners bound to a different process are not woken up unless we're in
|
2015-04-14 10:07:16 +00:00
|
|
|
* foreground mode, and are ignored. If the listener was only in the assigned
|
|
|
|
* state, it's totally rebound. This can happen if a pause() has completely
|
|
|
|
* stopped it. If the resume fails, 0 is returned and an error might be
|
|
|
|
* displayed.
|
2011-07-24 16:28:10 +00:00
|
|
|
*/
|
BUG/MEDIUM: listener: use a self-locked list for the dequeue lists
There is a very difficult to reproduce race in the listener's accept
code, which is much easier to reproduce once connection limits are
properly enforced. It's an ABBA lock issue :
- the following functions take l->lock then lq_lock :
disable_listener, pause_listener, listener_full, limit_listener,
do_unbind_listener
- the following ones take lq_lock then l->lock :
resume_listener, dequeue_all_listener
This is because __resume_listener() only takes the listener's lock
and expects to be called with lq_lock held. The problem can easily
happen when listener_full() and limit_listener() are called a lot
while in parallel another thread releases sessions for the same
listener using listener_release() which in turn calls resume_listener().
This scenario is more prevalent in 2.0-dev since the removal of the
accept lock in listener_accept(). However in 1.9 and before, a different
but extremely unlikely scenario can happen :
thread1 thread2
............................ enter listener_accept()
limit_listener()
............................ long pause before taking the lock
session_free()
dequeue_all_listeners()
lock(lq_lock) [1]
............................ try_lock(l->lock) [2]
__resume_listener()
spin_lock(l->lock) =>WAIT[2]
............................ accept()
l->accept()
nbconn==maxconn =>
listener_full()
state==LI_LIMITED =>
lock(lq_lock) =>DEADLOCK[1]!
In practice it is almost impossible to trigger it because it requires
to limit both on the listener's maxconn and the frontend's rate limit,
at the same time, and to release the listener when the connection rate
goes below the limit between poll() returns the FD and the lock is
taken (a few nanoseconds). But maybe with threads competing on the
same core it has more chances to appear.
This patch removes the lq_lock and replaces it with a lockless queue
for the listener's wait queue (well, technically speaking a self-locked
queue) brought by commit a8434ec14 ("MINOR: lists: Implement locked
variations.") and its few subsequent fixes. This relieves us from the
need of the lq_lock and removes the deadlock. It also gets rid of the
distinction between __resume_listener() and resume_listener() since the
only difference was the lq_lock. All listener removals from the list
are now unconditional to avoid races on the state. It's worth noting
that the list used to never be initialized and that it used to work
only thanks to the state tests, so the initialization has now been
added.
This patch must carefully be backported to 1.9 and very likely 1.8.
It is mandatory to be careful about replacing all manipulations of
l->wait_queue, global.listener_queue and p->listener_queue.
2019-02-28 09:27:18 +00:00
|
|
|
int resume_listener(struct listener *l)
|
2011-07-24 16:28:10 +00:00
|
|
|
{
|
2017-05-30 13:36:50 +00:00
|
|
|
int ret = 1;
|
|
|
|
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
|
2017-05-30 13:36:50 +00:00
|
|
|
|
BUG/MAJOR: listener: fix thread safety in resume_listener()
resume_listener() can be called from a thread not part of the listener's
mask after a curr_conn has gone lower than a proxy's or the process' limit.
This results in fd_may_recv() being called unlocked if the listener is
bound to only one thread, and quickly locks up.
This patch solves this by creating a per-thread work_list dedicated to
listeners, and modifying resume_listener() so that it bounces the listener
to one of its owning thread's work_list and waking it up. This thread will
then call resume_listener() again and will perform the operation on the
file descriptor itself. It is important to do it this way so that the
listener's state cannot be modified while the listener is being moved,
otherwise multiple threads can take conflicting decisions and the listener
could be put back into the global queue if the listener was used at the
same time.
It seems like a slightly simpler approach would be possible if the locked
list API would provide the ability to return a locked element. In this
case the listener would be immediately requeued in dequeue_all_listeners()
without having to go through resume_listener() with its associated lock.
This fix must be backported to all versions having the lock-less accept
loop, which is as far as 1.8 since deadlock fixes involving this feature
had to be backported there. It is expected that the code should not differ
too much there. However, previous commit "MINOR: task: introduce work lists"
will be needed as well and should not present difficulties either. For 1.8,
the commits introducing thread_mask() and LIST_ADDED() will be needed as
well, either backporting my_flsl() or switching to my_ffsl() will be OK,
and some changes will have to be performed so that the init function is
properly called (and maybe the deinit one can be dropped).
In order to test for the fix, simply set up a multi-threaded frontend with
multiple bind lines each attached to a single thread (reproduced with 16
threads here), set up a very low maxconn value on the frontend, and inject
heavy traffic on all listeners in parallel with slightly more connections
than the configured limit ( typically +20%) so that it flips very
frequently. If the bug is still there, at some point (5-20 seconds) the
traffic will go much lower or even stop, either with spinning threads or
not.
2019-07-11 08:08:31 +00:00
|
|
|
/* check that another thread didn't to the job in parallel (e.g. at the
|
|
|
|
* end of listen_accept() while we'd come from dequeue_all_listeners().
|
|
|
|
*/
|
2019-08-08 13:47:21 +00:00
|
|
|
if (MT_LIST_ADDED(&l->wait_queue))
|
BUG/MAJOR: listener: fix thread safety in resume_listener()
resume_listener() can be called from a thread not part of the listener's
mask after a curr_conn has gone lower than a proxy's or the process' limit.
This results in fd_may_recv() being called unlocked if the listener is
bound to only one thread, and quickly locks up.
This patch solves this by creating a per-thread work_list dedicated to
listeners, and modifying resume_listener() so that it bounces the listener
to one of its owning thread's work_list and waking it up. This thread will
then call resume_listener() again and will perform the operation on the
file descriptor itself. It is important to do it this way so that the
listener's state cannot be modified while the listener is being moved,
otherwise multiple threads can take conflicting decisions and the listener
could be put back into the global queue if the listener was used at the
same time.
It seems like a slightly simpler approach would be possible if the locked
list API would provide the ability to return a locked element. In this
case the listener would be immediately requeued in dequeue_all_listeners()
without having to go through resume_listener() with its associated lock.
This fix must be backported to all versions having the lock-less accept
loop, which is as far as 1.8 since deadlock fixes involving this feature
had to be backported there. It is expected that the code should not differ
too much there. However, previous commit "MINOR: task: introduce work lists"
will be needed as well and should not present difficulties either. For 1.8,
the commits introducing thread_mask() and LIST_ADDED() will be needed as
well, either backporting my_flsl() or switching to my_ffsl() will be OK,
and some changes will have to be performed so that the init function is
properly called (and maybe the deinit one can be dropped).
In order to test for the fix, simply set up a multi-threaded frontend with
multiple bind lines each attached to a single thread (reproduced with 16
threads here), set up a very low maxconn value on the frontend, and inject
heavy traffic on all listeners in parallel with slightly more connections
than the configured limit ( typically +20%) so that it flips very
frequently. If the bug is still there, at some point (5-20 seconds) the
traffic will go much lower or even stop, either with spinning threads or
not.
2019-07-11 08:08:31 +00:00
|
|
|
goto end;
|
|
|
|
|
2017-06-01 15:38:50 +00:00
|
|
|
if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
|
2019-02-02 16:39:53 +00:00
|
|
|
!(proc_mask(l->bind_conf->bind_proc) & pid_bit))
|
2017-05-30 13:36:50 +00:00
|
|
|
goto end;
|
2017-03-15 11:47:46 +00:00
|
|
|
|
2014-07-07 19:06:24 +00:00
|
|
|
if (l->state == LI_ASSIGNED) {
|
|
|
|
char msg[100];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = l->proto->bind(l, msg, sizeof(msg));
|
|
|
|
if (err & ERR_ALERT)
|
2017-11-24 15:50:31 +00:00
|
|
|
ha_alert("Resuming listener: %s\n", msg);
|
2014-07-07 19:06:24 +00:00
|
|
|
else if (err & ERR_WARN)
|
2017-11-24 15:50:31 +00:00
|
|
|
ha_warning("Resuming listener: %s\n", msg);
|
2014-07-07 19:06:24 +00:00
|
|
|
|
2017-05-30 13:36:50 +00:00
|
|
|
if (err & (ERR_FATAL | ERR_ABORT)) {
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
2014-07-07 19:06:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 13:36:50 +00:00
|
|
|
if (l->state < LI_PAUSED || l->state == LI_ZOMBIE) {
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
2011-07-24 16:28:10 +00:00
|
|
|
|
2012-10-04 06:56:31 +00:00
|
|
|
if (l->proto->sock_prot == IPPROTO_TCP &&
|
|
|
|
l->state == LI_PAUSED &&
|
2019-02-27 14:39:41 +00:00
|
|
|
listen(l->fd, listener_backlog(l)) != 0) {
|
2017-05-30 13:36:50 +00:00
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
2011-07-24 16:28:10 +00:00
|
|
|
|
|
|
|
if (l->state == LI_READY)
|
2017-05-30 13:36:50 +00:00
|
|
|
goto end;
|
2011-07-24 16:28:10 +00:00
|
|
|
|
2019-08-08 13:47:21 +00:00
|
|
|
MT_LIST_DEL(&l->wait_queue);
|
2011-07-24 20:03:52 +00:00
|
|
|
|
2019-02-27 15:49:00 +00:00
|
|
|
if (l->maxconn && l->nbconn >= l->maxconn) {
|
2011-07-24 16:28:10 +00:00
|
|
|
l->state = LI_FULL;
|
2017-05-30 13:36:50 +00:00
|
|
|
goto end;
|
2011-07-24 16:28:10 +00:00
|
|
|
}
|
|
|
|
|
BUG/MAJOR: listener: fix thread safety in resume_listener()
resume_listener() can be called from a thread not part of the listener's
mask after a curr_conn has gone lower than a proxy's or the process' limit.
This results in fd_may_recv() being called unlocked if the listener is
bound to only one thread, and quickly locks up.
This patch solves this by creating a per-thread work_list dedicated to
listeners, and modifying resume_listener() so that it bounces the listener
to one of its owning thread's work_list and waking it up. This thread will
then call resume_listener() again and will perform the operation on the
file descriptor itself. It is important to do it this way so that the
listener's state cannot be modified while the listener is being moved,
otherwise multiple threads can take conflicting decisions and the listener
could be put back into the global queue if the listener was used at the
same time.
It seems like a slightly simpler approach would be possible if the locked
list API would provide the ability to return a locked element. In this
case the listener would be immediately requeued in dequeue_all_listeners()
without having to go through resume_listener() with its associated lock.
This fix must be backported to all versions having the lock-less accept
loop, which is as far as 1.8 since deadlock fixes involving this feature
had to be backported there. It is expected that the code should not differ
too much there. However, previous commit "MINOR: task: introduce work lists"
will be needed as well and should not present difficulties either. For 1.8,
the commits introducing thread_mask() and LIST_ADDED() will be needed as
well, either backporting my_flsl() or switching to my_ffsl() will be OK,
and some changes will have to be performed so that the init function is
properly called (and maybe the deinit one can be dropped).
In order to test for the fix, simply set up a multi-threaded frontend with
multiple bind lines each attached to a single thread (reproduced with 16
threads here), set up a very low maxconn value on the frontend, and inject
heavy traffic on all listeners in parallel with slightly more connections
than the configured limit ( typically +20%) so that it flips very
frequently. If the bug is still there, at some point (5-20 seconds) the
traffic will go much lower or even stop, either with spinning threads or
not.
2019-07-11 08:08:31 +00:00
|
|
|
if (!(thread_mask(l->bind_conf->bind_thread) & tid_bit)) {
|
|
|
|
/* we're not allowed to touch this listener's FD, let's requeue
|
|
|
|
* the listener into one of its owning thread's queue instead.
|
|
|
|
*/
|
2020-02-12 09:01:29 +00:00
|
|
|
int first_thread = my_flsl(thread_mask(l->bind_conf->bind_thread) & all_threads_mask) - 1;
|
BUG/MAJOR: listener: fix thread safety in resume_listener()
resume_listener() can be called from a thread not part of the listener's
mask after a curr_conn has gone lower than a proxy's or the process' limit.
This results in fd_may_recv() being called unlocked if the listener is
bound to only one thread, and quickly locks up.
This patch solves this by creating a per-thread work_list dedicated to
listeners, and modifying resume_listener() so that it bounces the listener
to one of its owning thread's work_list and waking it up. This thread will
then call resume_listener() again and will perform the operation on the
file descriptor itself. It is important to do it this way so that the
listener's state cannot be modified while the listener is being moved,
otherwise multiple threads can take conflicting decisions and the listener
could be put back into the global queue if the listener was used at the
same time.
It seems like a slightly simpler approach would be possible if the locked
list API would provide the ability to return a locked element. In this
case the listener would be immediately requeued in dequeue_all_listeners()
without having to go through resume_listener() with its associated lock.
This fix must be backported to all versions having the lock-less accept
loop, which is as far as 1.8 since deadlock fixes involving this feature
had to be backported there. It is expected that the code should not differ
too much there. However, previous commit "MINOR: task: introduce work lists"
will be needed as well and should not present difficulties either. For 1.8,
the commits introducing thread_mask() and LIST_ADDED() will be needed as
well, either backporting my_flsl() or switching to my_ffsl() will be OK,
and some changes will have to be performed so that the init function is
properly called (and maybe the deinit one can be dropped).
In order to test for the fix, simply set up a multi-threaded frontend with
multiple bind lines each attached to a single thread (reproduced with 16
threads here), set up a very low maxconn value on the frontend, and inject
heavy traffic on all listeners in parallel with slightly more connections
than the configured limit ( typically +20%) so that it flips very
frequently. If the bug is still there, at some point (5-20 seconds) the
traffic will go much lower or even stop, either with spinning threads or
not.
2019-07-11 08:08:31 +00:00
|
|
|
work_list_add(&local_listener_queue[first_thread], &l->wait_queue);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2012-08-09 10:11:58 +00:00
|
|
|
fd_want_recv(l->fd);
|
2011-07-24 16:28:10 +00:00
|
|
|
l->state = LI_READY;
|
2017-05-30 13:36:50 +00:00
|
|
|
end:
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
|
2017-05-30 13:36:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 22:22:06 +00:00
|
|
|
/* Marks a ready listener as full so that the stream code tries to re-enable
|
2011-07-24 17:23:38 +00:00
|
|
|
* it upon next close() using resume_listener().
|
|
|
|
*/
|
2017-08-28 13:29:20 +00:00
|
|
|
static void listener_full(struct listener *l)
|
2011-07-24 17:23:38 +00:00
|
|
|
{
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
|
2011-07-24 17:23:38 +00:00
|
|
|
if (l->state >= LI_READY) {
|
2019-08-08 13:47:21 +00:00
|
|
|
MT_LIST_DEL(&l->wait_queue);
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
if (l->state != LI_FULL) {
|
|
|
|
fd_stop_recv(l->fd);
|
|
|
|
l->state = LI_FULL;
|
|
|
|
}
|
2011-07-24 17:23:38 +00:00
|
|
|
}
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
|
2011-07-24 17:23:38 +00:00
|
|
|
}
|
|
|
|
|
2011-07-24 20:03:52 +00:00
|
|
|
/* Marks a ready listener as limited so that we only try to re-enable it when
|
|
|
|
* resources are free again. It will be queued into the specified queue.
|
|
|
|
*/
|
2019-08-08 13:47:21 +00:00
|
|
|
static void limit_listener(struct listener *l, struct mt_list *list)
|
2011-07-24 20:03:52 +00:00
|
|
|
{
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
|
2011-07-24 20:03:52 +00:00
|
|
|
if (l->state == LI_READY) {
|
2019-08-08 13:47:21 +00:00
|
|
|
MT_LIST_ADDQ(list, &l->wait_queue);
|
2012-08-09 10:11:58 +00:00
|
|
|
fd_stop_recv(l->fd);
|
2011-07-24 20:03:52 +00:00
|
|
|
l->state = LI_LIMITED;
|
|
|
|
}
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
|
2011-07-24 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-28 20:59:24 +00:00
|
|
|
/* This function adds all of the protocol's listener's file descriptors to the
|
|
|
|
* polling lists when they are in the LI_LISTEN state. It is intended to be
|
|
|
|
* used as a protocol's generic enable_all() primitive, for use after the
|
|
|
|
* fork(). It puts the listeners into LI_READY or LI_FULL states depending on
|
|
|
|
* their number of connections. It always returns ERR_NONE.
|
2019-07-24 14:45:02 +00:00
|
|
|
*
|
|
|
|
* Must be called with proto_lock held.
|
|
|
|
*
|
2007-10-28 20:59:24 +00:00
|
|
|
*/
|
|
|
|
int enable_all_listeners(struct protocol *proto)
|
|
|
|
{
|
|
|
|
struct listener *listener;
|
|
|
|
|
|
|
|
list_for_each_entry(listener, &proto->listeners, proto_list)
|
|
|
|
enable_listener(listener);
|
|
|
|
return ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function removes all of the protocol's listener's file descriptors from
|
|
|
|
* the polling lists when they are in the LI_READY or LI_FULL states. It is
|
|
|
|
* intended to be used as a protocol's generic disable_all() primitive. It puts
|
|
|
|
* the listeners into LI_LISTEN, and always returns ERR_NONE.
|
2019-07-24 14:45:02 +00:00
|
|
|
*
|
|
|
|
* Must be called with proto_lock held.
|
|
|
|
*
|
2007-10-28 20:59:24 +00:00
|
|
|
*/
|
|
|
|
int disable_all_listeners(struct protocol *proto)
|
|
|
|
{
|
|
|
|
struct listener *listener;
|
|
|
|
|
|
|
|
list_for_each_entry(listener, &proto->listeners, proto_list)
|
|
|
|
disable_listener(listener);
|
|
|
|
return ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:10:52 +00:00
|
|
|
/* Dequeues all listeners waiting for a resource the global wait queue */
|
|
|
|
void dequeue_all_listeners()
|
2011-07-24 20:03:52 +00:00
|
|
|
{
|
BUG/MEDIUM: listener: use a self-locked list for the dequeue lists
There is a very difficult to reproduce race in the listener's accept
code, which is much easier to reproduce once connection limits are
properly enforced. It's an ABBA lock issue :
- the following functions take l->lock then lq_lock :
disable_listener, pause_listener, listener_full, limit_listener,
do_unbind_listener
- the following ones take lq_lock then l->lock :
resume_listener, dequeue_all_listener
This is because __resume_listener() only takes the listener's lock
and expects to be called with lq_lock held. The problem can easily
happen when listener_full() and limit_listener() are called a lot
while in parallel another thread releases sessions for the same
listener using listener_release() which in turn calls resume_listener().
This scenario is more prevalent in 2.0-dev since the removal of the
accept lock in listener_accept(). However in 1.9 and before, a different
but extremely unlikely scenario can happen :
thread1 thread2
............................ enter listener_accept()
limit_listener()
............................ long pause before taking the lock
session_free()
dequeue_all_listeners()
lock(lq_lock) [1]
............................ try_lock(l->lock) [2]
__resume_listener()
spin_lock(l->lock) =>WAIT[2]
............................ accept()
l->accept()
nbconn==maxconn =>
listener_full()
state==LI_LIMITED =>
lock(lq_lock) =>DEADLOCK[1]!
In practice it is almost impossible to trigger it because it requires
to limit both on the listener's maxconn and the frontend's rate limit,
at the same time, and to release the listener when the connection rate
goes below the limit between poll() returns the FD and the lock is
taken (a few nanoseconds). But maybe with threads competing on the
same core it has more chances to appear.
This patch removes the lq_lock and replaces it with a lockless queue
for the listener's wait queue (well, technically speaking a self-locked
queue) brought by commit a8434ec14 ("MINOR: lists: Implement locked
variations.") and its few subsequent fixes. This relieves us from the
need of the lq_lock and removes the deadlock. It also gets rid of the
distinction between __resume_listener() and resume_listener() since the
only difference was the lq_lock. All listener removals from the list
are now unconditional to avoid races on the state. It's worth noting
that the list used to never be initialized and that it used to work
only thanks to the state tests, so the initialization has now been
added.
This patch must carefully be backported to 1.9 and very likely 1.8.
It is mandatory to be careful about replacing all manipulations of
l->wait_queue, global.listener_queue and p->listener_queue.
2019-02-28 09:27:18 +00:00
|
|
|
struct listener *listener;
|
2011-07-24 20:03:52 +00:00
|
|
|
|
2019-12-10 13:10:52 +00:00
|
|
|
while ((listener = MT_LIST_POP(&global_listener_queue, struct listener *, wait_queue))) {
|
|
|
|
/* This cannot fail because the listeners are by definition in
|
|
|
|
* the LI_LIMITED state.
|
|
|
|
*/
|
|
|
|
resume_listener(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dequeues all listeners waiting for a resource in proxy <px>'s queue */
|
|
|
|
void dequeue_proxy_listeners(struct proxy *px)
|
|
|
|
{
|
|
|
|
struct listener *listener;
|
|
|
|
|
|
|
|
while ((listener = MT_LIST_POP(&px->listener_queue, struct listener *, wait_queue))) {
|
2011-07-24 20:03:52 +00:00
|
|
|
/* This cannot fail because the listeners are by definition in
|
BUG/MEDIUM: listener: use a self-locked list for the dequeue lists
There is a very difficult to reproduce race in the listener's accept
code, which is much easier to reproduce once connection limits are
properly enforced. It's an ABBA lock issue :
- the following functions take l->lock then lq_lock :
disable_listener, pause_listener, listener_full, limit_listener,
do_unbind_listener
- the following ones take lq_lock then l->lock :
resume_listener, dequeue_all_listener
This is because __resume_listener() only takes the listener's lock
and expects to be called with lq_lock held. The problem can easily
happen when listener_full() and limit_listener() are called a lot
while in parallel another thread releases sessions for the same
listener using listener_release() which in turn calls resume_listener().
This scenario is more prevalent in 2.0-dev since the removal of the
accept lock in listener_accept(). However in 1.9 and before, a different
but extremely unlikely scenario can happen :
thread1 thread2
............................ enter listener_accept()
limit_listener()
............................ long pause before taking the lock
session_free()
dequeue_all_listeners()
lock(lq_lock) [1]
............................ try_lock(l->lock) [2]
__resume_listener()
spin_lock(l->lock) =>WAIT[2]
............................ accept()
l->accept()
nbconn==maxconn =>
listener_full()
state==LI_LIMITED =>
lock(lq_lock) =>DEADLOCK[1]!
In practice it is almost impossible to trigger it because it requires
to limit both on the listener's maxconn and the frontend's rate limit,
at the same time, and to release the listener when the connection rate
goes below the limit between poll() returns the FD and the lock is
taken (a few nanoseconds). But maybe with threads competing on the
same core it has more chances to appear.
This patch removes the lq_lock and replaces it with a lockless queue
for the listener's wait queue (well, technically speaking a self-locked
queue) brought by commit a8434ec14 ("MINOR: lists: Implement locked
variations.") and its few subsequent fixes. This relieves us from the
need of the lq_lock and removes the deadlock. It also gets rid of the
distinction between __resume_listener() and resume_listener() since the
only difference was the lq_lock. All listener removals from the list
are now unconditional to avoid races on the state. It's worth noting
that the list used to never be initialized and that it used to work
only thanks to the state tests, so the initialization has now been
added.
This patch must carefully be backported to 1.9 and very likely 1.8.
It is mandatory to be careful about replacing all manipulations of
l->wait_queue, global.listener_queue and p->listener_queue.
2019-02-28 09:27:18 +00:00
|
|
|
* the LI_LIMITED state.
|
2011-07-24 20:03:52 +00:00
|
|
|
*/
|
BUG/MEDIUM: listener: use a self-locked list for the dequeue lists
There is a very difficult to reproduce race in the listener's accept
code, which is much easier to reproduce once connection limits are
properly enforced. It's an ABBA lock issue :
- the following functions take l->lock then lq_lock :
disable_listener, pause_listener, listener_full, limit_listener,
do_unbind_listener
- the following ones take lq_lock then l->lock :
resume_listener, dequeue_all_listener
This is because __resume_listener() only takes the listener's lock
and expects to be called with lq_lock held. The problem can easily
happen when listener_full() and limit_listener() are called a lot
while in parallel another thread releases sessions for the same
listener using listener_release() which in turn calls resume_listener().
This scenario is more prevalent in 2.0-dev since the removal of the
accept lock in listener_accept(). However in 1.9 and before, a different
but extremely unlikely scenario can happen :
thread1 thread2
............................ enter listener_accept()
limit_listener()
............................ long pause before taking the lock
session_free()
dequeue_all_listeners()
lock(lq_lock) [1]
............................ try_lock(l->lock) [2]
__resume_listener()
spin_lock(l->lock) =>WAIT[2]
............................ accept()
l->accept()
nbconn==maxconn =>
listener_full()
state==LI_LIMITED =>
lock(lq_lock) =>DEADLOCK[1]!
In practice it is almost impossible to trigger it because it requires
to limit both on the listener's maxconn and the frontend's rate limit,
at the same time, and to release the listener when the connection rate
goes below the limit between poll() returns the FD and the lock is
taken (a few nanoseconds). But maybe with threads competing on the
same core it has more chances to appear.
This patch removes the lq_lock and replaces it with a lockless queue
for the listener's wait queue (well, technically speaking a self-locked
queue) brought by commit a8434ec14 ("MINOR: lists: Implement locked
variations.") and its few subsequent fixes. This relieves us from the
need of the lq_lock and removes the deadlock. It also gets rid of the
distinction between __resume_listener() and resume_listener() since the
only difference was the lq_lock. All listener removals from the list
are now unconditional to avoid races on the state. It's worth noting
that the list used to never be initialized and that it used to work
only thanks to the state tests, so the initialization has now been
added.
This patch must carefully be backported to 1.9 and very likely 1.8.
It is mandatory to be careful about replacing all manipulations of
l->wait_queue, global.listener_queue and p->listener_queue.
2019-02-28 09:27:18 +00:00
|
|
|
resume_listener(listener);
|
2011-07-24 20:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 09:04:47 +00:00
|
|
|
/* Must be called with the lock held. Depending on <do_close> value, it does
|
|
|
|
* what unbind_listener or unbind_listener_no_close should do.
|
|
|
|
*/
|
|
|
|
void do_unbind_listener(struct listener *listener, int do_close)
|
2007-10-28 21:13:50 +00:00
|
|
|
{
|
2019-03-08 14:35:42 +00:00
|
|
|
if (listener->state == LI_READY && fd_updt)
|
2012-08-09 10:11:58 +00:00
|
|
|
fd_stop_recv(listener->fd);
|
2007-10-28 21:13:50 +00:00
|
|
|
|
2019-08-08 13:47:21 +00:00
|
|
|
MT_LIST_DEL(&listener->wait_queue);
|
2011-07-24 20:03:52 +00:00
|
|
|
|
2011-07-24 16:28:10 +00:00
|
|
|
if (listener->state >= LI_PAUSED) {
|
2017-04-05 23:05:05 +00:00
|
|
|
if (do_close) {
|
|
|
|
fd_delete(listener->fd);
|
|
|
|
listener->fd = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fd_remove(listener->fd);
|
2007-10-28 21:13:50 +00:00
|
|
|
listener->state = LI_ASSIGNED;
|
|
|
|
}
|
2017-11-05 10:38:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 23:05:05 +00:00
|
|
|
/* This function closes the listening socket for the specified listener,
|
|
|
|
* provided that it's already in a listening state. The listener enters the
|
2017-11-05 10:38:44 +00:00
|
|
|
* LI_ASSIGNED state. This function is intended to be used as a generic
|
|
|
|
* function for standard protocols.
|
2017-04-05 23:05:05 +00:00
|
|
|
*/
|
2017-11-05 10:38:44 +00:00
|
|
|
void unbind_listener(struct listener *listener)
|
2017-04-05 23:05:05 +00:00
|
|
|
{
|
2018-03-16 09:04:47 +00:00
|
|
|
HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
|
2017-11-05 10:38:44 +00:00
|
|
|
do_unbind_listener(listener, 1);
|
2018-03-16 09:04:47 +00:00
|
|
|
HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
|
2017-04-05 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function pretends the listener is dead, but keeps the FD opened, so
|
|
|
|
* that we can provide it, for conf reloading.
|
|
|
|
*/
|
2017-11-05 10:38:44 +00:00
|
|
|
void unbind_listener_no_close(struct listener *listener)
|
2017-04-05 23:05:05 +00:00
|
|
|
{
|
2018-03-16 09:04:47 +00:00
|
|
|
HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
|
2017-11-05 10:38:44 +00:00
|
|
|
do_unbind_listener(listener, 0);
|
2018-03-16 09:04:47 +00:00
|
|
|
HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
|
2017-04-05 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
2007-10-28 21:35:41 +00:00
|
|
|
/* This function closes all listening sockets bound to the protocol <proto>,
|
|
|
|
* and the listeners end in LI_ASSIGNED state if they were higher. It does not
|
|
|
|
* detach them from the protocol. It always returns ERR_NONE.
|
2019-07-24 14:45:02 +00:00
|
|
|
*
|
|
|
|
* Must be called with proto_lock held.
|
|
|
|
*
|
2007-10-28 21:35:41 +00:00
|
|
|
*/
|
|
|
|
int unbind_all_listeners(struct protocol *proto)
|
|
|
|
{
|
|
|
|
struct listener *listener;
|
|
|
|
|
|
|
|
list_for_each_entry(listener, &proto->listeners, proto_list)
|
|
|
|
unbind_listener(listener);
|
|
|
|
return ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2017-09-15 06:10:44 +00:00
|
|
|
/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
|
|
|
|
* range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
|
|
|
|
* allocation). The address family is taken from ss->ss_family. The number of
|
|
|
|
* jobs and listeners is automatically increased by the number of listeners
|
2017-11-15 18:02:58 +00:00
|
|
|
* created. If the <inherited> argument is set to 1, it specifies that the FD
|
|
|
|
* was obtained from a parent process.
|
|
|
|
* It returns non-zero on success, zero on error with the error message
|
2017-09-15 06:10:44 +00:00
|
|
|
* set in <err>.
|
|
|
|
*/
|
|
|
|
int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
|
2017-11-15 18:02:58 +00:00
|
|
|
int portl, int porth, int fd, int inherited, char **err)
|
2017-09-15 06:10:44 +00:00
|
|
|
{
|
|
|
|
struct protocol *proto = protocol_by_family(ss->ss_family);
|
|
|
|
struct listener *l;
|
|
|
|
int port;
|
|
|
|
|
|
|
|
if (!proto) {
|
|
|
|
memprintf(err, "unsupported protocol family %d", ss->ss_family);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (port = portl; port <= porth; port++) {
|
|
|
|
l = calloc(1, sizeof(*l));
|
|
|
|
if (!l) {
|
|
|
|
memprintf(err, "out of memory");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
l->obj_type = OBJ_TYPE_LISTENER;
|
|
|
|
LIST_ADDQ(&bc->frontend->conf.listeners, &l->by_fe);
|
|
|
|
LIST_ADDQ(&bc->listeners, &l->by_bind);
|
|
|
|
l->bind_conf = bc;
|
|
|
|
|
|
|
|
l->fd = fd;
|
|
|
|
memcpy(&l->addr, ss, sizeof(*ss));
|
2019-08-08 13:47:21 +00:00
|
|
|
MT_LIST_INIT(&l->wait_queue);
|
2017-09-15 06:10:44 +00:00
|
|
|
l->state = LI_INIT;
|
|
|
|
|
|
|
|
proto->add(l, port);
|
|
|
|
|
2017-11-15 18:02:58 +00:00
|
|
|
if (inherited)
|
|
|
|
l->options |= LI_O_INHERITED;
|
|
|
|
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_INIT(&l->lock);
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_ADD(&jobs, 1);
|
|
|
|
_HA_ATOMIC_ADD(&listeners, 1);
|
2017-09-15 06:10:44 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-10-28 21:26:05 +00:00
|
|
|
/* Delete a listener from its protocol's list of listeners. The listener's
|
|
|
|
* state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
|
2017-09-15 06:18:11 +00:00
|
|
|
* number of listeners is updated, as well as the global number of listeners
|
|
|
|
* and jobs. Note that the listener must have previously been unbound. This
|
|
|
|
* is the generic function to use to remove a listener.
|
2019-07-24 14:45:02 +00:00
|
|
|
*
|
|
|
|
* Will grab the proto_lock.
|
|
|
|
*
|
2007-10-28 21:26:05 +00:00
|
|
|
*/
|
|
|
|
void delete_listener(struct listener *listener)
|
|
|
|
{
|
2019-08-26 08:55:52 +00:00
|
|
|
HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
if (listener->state == LI_ASSIGNED) {
|
|
|
|
listener->state = LI_INIT;
|
|
|
|
LIST_DEL(&listener->proto_list);
|
|
|
|
listener->proto->nb_listeners--;
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_SUB(&jobs, 1);
|
|
|
|
_HA_ATOMIC_SUB(&listeners, 1);
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
}
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
|
2019-08-26 08:55:52 +00:00
|
|
|
HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
|
2007-10-28 21:26:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 14:39:41 +00:00
|
|
|
/* Returns a suitable value for a listener's backlog. It uses the listener's,
|
|
|
|
* otherwise the frontend's backlog, otherwise the listener's maxconn,
|
|
|
|
* otherwise the frontend's maxconn, otherwise 1024.
|
|
|
|
*/
|
|
|
|
int listener_backlog(const struct listener *l)
|
|
|
|
{
|
|
|
|
if (l->backlog)
|
|
|
|
return l->backlog;
|
|
|
|
|
|
|
|
if (l->bind_conf->frontend->backlog)
|
|
|
|
return l->bind_conf->frontend->backlog;
|
|
|
|
|
|
|
|
if (l->maxconn)
|
|
|
|
return l->maxconn;
|
|
|
|
|
|
|
|
if (l->bind_conf->frontend->maxconn)
|
|
|
|
return l->bind_conf->frontend->maxconn;
|
|
|
|
|
|
|
|
return 1024;
|
|
|
|
}
|
|
|
|
|
2012-05-07 19:22:09 +00:00
|
|
|
/* This function is called on a read event from a listening socket, corresponding
|
|
|
|
* to an accept. It tries to accept as many connections as possible, and for each
|
|
|
|
* calls the listener's accept handler (generally the frontend's accept handler).
|
|
|
|
*/
|
2012-08-09 12:45:22 +00:00
|
|
|
void listener_accept(int fd)
|
2012-05-07 19:22:09 +00:00
|
|
|
{
|
|
|
|
struct listener *l = fdtab[fd].owner;
|
2019-02-25 15:18:16 +00:00
|
|
|
struct proxy *p;
|
BUG/MEDIUM: listener: Fix how unlimited number of consecutive accepts is handled
There is a bug when global.tune.maxaccept is set to -1 (no limit). It is pretty
visible with one process (nbproc sets to 1). The functions listener_accept() and
accept_queue_process() don't expect to handle negative maxaccept values. So
instead of accepting incoming connections without any limit, none are never
accepted and HAProxy loop infinitly in the scheduler.
When there are 2 or more processes, the bug is a bit more subtile. The limit for
a listener is set to 1. So only one connection is accepted at a time by a given
listener. This happens because the listener's maxaccept value is an unsigned
integer. In check_config_validity(), it is first set to UINT_MAX (-1 casted in
an unsigned integer), and then some calculations on it leads to an integer
overflow.
To fix the bug, the listener's maxaccept value is now a signed integer. So, if a
negative value is set for global.tune.maxaccept, we keep it untouched for the
listener and no calculation is made on it. Then, in the listener code, this
signed value is casted to a unsigned one. It simplifies all tests instead of
dealing with negative values. So, it limits the number of connections accepted
at a time to UINT_MAX at most. But, honestly, it not an issue.
This patch must be backported to 1.9 and 1.8.
2019-04-30 10:17:13 +00:00
|
|
|
unsigned int max_accept;
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
int next_conn = 0;
|
2019-02-27 18:32:32 +00:00
|
|
|
int next_feconn = 0;
|
|
|
|
int next_actconn = 0;
|
2014-05-07 17:47:02 +00:00
|
|
|
int expire;
|
2012-05-07 19:22:09 +00:00
|
|
|
int cfd;
|
|
|
|
int ret;
|
2014-01-31 18:40:19 +00:00
|
|
|
#ifdef USE_ACCEPT4
|
|
|
|
static int accept4_broken;
|
|
|
|
#endif
|
2012-05-07 19:22:09 +00:00
|
|
|
|
2019-02-25 15:18:16 +00:00
|
|
|
if (!l)
|
|
|
|
return;
|
|
|
|
p = l->bind_conf->frontend;
|
BUG/MEDIUM: listener: Fix how unlimited number of consecutive accepts is handled
There is a bug when global.tune.maxaccept is set to -1 (no limit). It is pretty
visible with one process (nbproc sets to 1). The functions listener_accept() and
accept_queue_process() don't expect to handle negative maxaccept values. So
instead of accepting incoming connections without any limit, none are never
accepted and HAProxy loop infinitly in the scheduler.
When there are 2 or more processes, the bug is a bit more subtile. The limit for
a listener is set to 1. So only one connection is accepted at a time by a given
listener. This happens because the listener's maxaccept value is an unsigned
integer. In check_config_validity(), it is first set to UINT_MAX (-1 casted in
an unsigned integer), and then some calculations on it leads to an integer
overflow.
To fix the bug, the listener's maxaccept value is now a signed integer. So, if a
negative value is set for global.tune.maxaccept, we keep it untouched for the
listener and no calculation is made on it. Then, in the listener code, this
signed value is casted to a unsigned one. It simplifies all tests instead of
dealing with negative values. So, it limits the number of connections accepted
at a time to UINT_MAX at most. But, honestly, it not an issue.
This patch must be backported to 1.9 and 1.8.
2019-04-30 10:17:13 +00:00
|
|
|
|
|
|
|
/* if l->maxaccept is -1, then max_accept is UINT_MAX. It is not really
|
|
|
|
* illimited, but it is probably enough.
|
|
|
|
*/
|
2019-02-25 15:18:16 +00:00
|
|
|
max_accept = l->maxaccept ? l->maxaccept : 1;
|
2017-05-30 13:36:50 +00:00
|
|
|
|
2013-10-07 16:51:07 +00:00
|
|
|
if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
|
|
|
|
int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
|
|
|
|
|
|
|
|
if (unlikely(!max)) {
|
|
|
|
/* frontend accept rate limit was reached */
|
|
|
|
expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
|
2019-12-10 11:01:21 +00:00
|
|
|
goto limit_global;
|
2013-10-07 16:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (max_accept > max)
|
|
|
|
max_accept = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
|
2012-05-07 19:22:09 +00:00
|
|
|
int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
|
|
|
|
|
|
|
|
if (unlikely(!max)) {
|
|
|
|
/* frontend accept rate limit was reached */
|
2013-10-07 16:51:07 +00:00
|
|
|
expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
|
2019-12-10 11:01:21 +00:00
|
|
|
goto limit_global;
|
2012-05-07 19:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (max_accept > max)
|
|
|
|
max_accept = max;
|
|
|
|
}
|
2013-10-07 18:01:52 +00:00
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
|
|
|
|
int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
|
|
|
|
|
|
|
|
if (unlikely(!max)) {
|
|
|
|
/* frontend accept rate limit was reached */
|
|
|
|
expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
|
2019-12-10 11:01:21 +00:00
|
|
|
goto limit_global;
|
2013-10-07 18:01:52 +00:00
|
|
|
}
|
2012-05-07 19:22:09 +00:00
|
|
|
|
2013-10-07 18:01:52 +00:00
|
|
|
if (max_accept > max)
|
|
|
|
max_accept = max;
|
|
|
|
}
|
|
|
|
#endif
|
2012-05-07 19:22:09 +00:00
|
|
|
if (p && p->fe_sps_lim) {
|
|
|
|
int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
|
|
|
|
|
|
|
|
if (unlikely(!max)) {
|
|
|
|
/* frontend accept rate limit was reached */
|
2019-12-10 11:01:21 +00:00
|
|
|
expire = tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0));
|
|
|
|
goto limit_proxy;
|
2012-05-07 19:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (max_accept > max)
|
|
|
|
max_accept = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: if we fail to allocate a connection because of configured
|
|
|
|
* limits, we'll schedule a new attempt worst 1 second later in the
|
|
|
|
* worst case. If we fail due to system limits or temporary resource
|
|
|
|
* shortage, we try again 100ms later in the worst case.
|
|
|
|
*/
|
BUG/MEDIUM: listener: Fix how unlimited number of consecutive accepts is handled
There is a bug when global.tune.maxaccept is set to -1 (no limit). It is pretty
visible with one process (nbproc sets to 1). The functions listener_accept() and
accept_queue_process() don't expect to handle negative maxaccept values. So
instead of accepting incoming connections without any limit, none are never
accepted and HAProxy loop infinitly in the scheduler.
When there are 2 or more processes, the bug is a bit more subtile. The limit for
a listener is set to 1. So only one connection is accepted at a time by a given
listener. This happens because the listener's maxaccept value is an unsigned
integer. In check_config_validity(), it is first set to UINT_MAX (-1 casted in
an unsigned integer), and then some calculations on it leads to an integer
overflow.
To fix the bug, the listener's maxaccept value is now a signed integer. So, if a
negative value is set for global.tune.maxaccept, we keep it untouched for the
listener and no calculation is made on it. Then, in the listener code, this
signed value is casted to a unsigned one. It simplifies all tests instead of
dealing with negative values. So, it limits the number of connections accepted
at a time to UINT_MAX at most. But, honestly, it not an issue.
This patch must be backported to 1.9 and 1.8.
2019-04-30 10:17:13 +00:00
|
|
|
for (; max_accept; next_conn = next_feconn = next_actconn = 0, max_accept--) {
|
2012-05-07 19:22:09 +00:00
|
|
|
struct sockaddr_storage addr;
|
|
|
|
socklen_t laddr = sizeof(addr);
|
2017-05-30 13:36:50 +00:00
|
|
|
unsigned int count;
|
2020-06-05 06:40:51 +00:00
|
|
|
__decl_thread(unsigned long mask);
|
2012-05-07 19:22:09 +00:00
|
|
|
|
2019-02-27 18:32:32 +00:00
|
|
|
/* pre-increase the number of connections without going too far.
|
|
|
|
* We process the listener, then the proxy, then the process.
|
|
|
|
* We know which ones to unroll based on the next_xxx value.
|
|
|
|
*/
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
do {
|
|
|
|
count = l->nbconn;
|
2019-11-15 09:20:07 +00:00
|
|
|
if (unlikely(l->maxconn && count >= l->maxconn)) {
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
/* the listener was marked full or another
|
|
|
|
* thread is going to do it.
|
|
|
|
*/
|
|
|
|
next_conn = 0;
|
2019-11-15 09:20:07 +00:00
|
|
|
listener_full(l);
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
next_conn = count + 1;
|
2019-03-27 16:08:42 +00:00
|
|
|
} while (!_HA_ATOMIC_CAS(&l->nbconn, (int *)(&count), next_conn));
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
|
2019-02-27 18:32:32 +00:00
|
|
|
if (p) {
|
|
|
|
do {
|
|
|
|
count = p->feconn;
|
2019-11-15 09:20:07 +00:00
|
|
|
if (unlikely(count >= p->maxconn)) {
|
2019-02-27 18:32:32 +00:00
|
|
|
/* the frontend was marked full or another
|
|
|
|
* thread is going to do it.
|
|
|
|
*/
|
|
|
|
next_feconn = 0;
|
2019-12-10 11:01:21 +00:00
|
|
|
expire = TICK_ETERNITY;
|
|
|
|
goto limit_proxy;
|
2019-02-27 18:32:32 +00:00
|
|
|
}
|
|
|
|
next_feconn = count + 1;
|
2019-03-08 17:52:57 +00:00
|
|
|
} while (!_HA_ATOMIC_CAS(&p->feconn, &count, next_feconn));
|
2012-05-07 19:22:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 18:32:32 +00:00
|
|
|
if (!(l->options & LI_O_UNLIMITED)) {
|
|
|
|
do {
|
|
|
|
count = actconn;
|
2019-11-15 09:20:07 +00:00
|
|
|
if (unlikely(count >= global.maxconn)) {
|
2019-02-27 18:32:32 +00:00
|
|
|
/* the process was marked full or another
|
|
|
|
* thread is going to do it.
|
|
|
|
*/
|
|
|
|
next_actconn = 0;
|
2019-12-10 11:01:21 +00:00
|
|
|
expire = tick_add(now_ms, 1000); /* try again in 1 second */
|
|
|
|
goto limit_global;
|
2019-02-27 18:32:32 +00:00
|
|
|
}
|
|
|
|
next_actconn = count + 1;
|
2019-03-27 16:08:42 +00:00
|
|
|
} while (!_HA_ATOMIC_CAS(&actconn, (int *)(&count), next_actconn));
|
2012-05-07 19:22:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 14:51:29 +00:00
|
|
|
/* with sockpair@ we don't want to do an accept */
|
|
|
|
if (unlikely(l->addr.ss_family == AF_CUST_SOCKPAIR)) {
|
|
|
|
if ((cfd = recv_fd_uxst(fd)) != -1)
|
2018-11-27 11:02:39 +00:00
|
|
|
fcntl(cfd, F_SETFL, O_NONBLOCK);
|
2019-01-27 17:34:12 +00:00
|
|
|
/* just like with UNIX sockets, only the family is filled */
|
|
|
|
addr.ss_family = AF_UNIX;
|
|
|
|
laddr = sizeof(addr.ss_family);
|
2018-09-11 14:51:29 +00:00
|
|
|
} else
|
|
|
|
|
2012-10-08 18:11:03 +00:00
|
|
|
#ifdef USE_ACCEPT4
|
2014-01-31 18:40:19 +00:00
|
|
|
/* only call accept4() if it's known to be safe, otherwise
|
|
|
|
* fallback to the legacy accept() + fcntl().
|
|
|
|
*/
|
|
|
|
if (unlikely(accept4_broken ||
|
2018-11-27 11:02:39 +00:00
|
|
|
((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
|
2014-01-31 18:40:19 +00:00
|
|
|
(errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
|
|
|
|
(accept4_broken = 1))))
|
|
|
|
#endif
|
2012-10-22 17:32:55 +00:00
|
|
|
if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
|
2018-11-27 11:02:39 +00:00
|
|
|
fcntl(cfd, F_SETFL, O_NONBLOCK);
|
2014-01-31 18:40:19 +00:00
|
|
|
|
2012-05-07 19:22:09 +00:00
|
|
|
if (unlikely(cfd == -1)) {
|
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
2019-12-10 07:42:21 +00:00
|
|
|
if (fdtab[fd].ev & (FD_POLL_HUP|FD_POLL_ERR)) {
|
2014-05-07 17:47:02 +00:00
|
|
|
/* the listening socket might have been disabled in a shared
|
|
|
|
* process and we're a collateral victim. We'll just pause for
|
|
|
|
* a while in case it comes back. In the mean time, we need to
|
|
|
|
* clear this sticky flag.
|
|
|
|
*/
|
2019-12-10 07:42:21 +00:00
|
|
|
_HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
|
2014-05-07 17:47:02 +00:00
|
|
|
goto transient_error;
|
|
|
|
}
|
2017-05-30 13:36:50 +00:00
|
|
|
goto end; /* nothing more to accept */
|
2014-05-07 17:47:02 +00:00
|
|
|
case EINVAL:
|
|
|
|
/* might be trying to accept on a shut fd (eg: soft stop) */
|
|
|
|
goto transient_error;
|
2014-01-20 20:21:30 +00:00
|
|
|
case EINTR:
|
|
|
|
case ECONNABORTED:
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_SUB(&l->nbconn, 1);
|
2019-02-27 18:32:32 +00:00
|
|
|
if (p)
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_SUB(&p->feconn, 1);
|
2019-02-27 18:32:32 +00:00
|
|
|
if (!(l->options & LI_O_UNLIMITED))
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_SUB(&actconn, 1);
|
2014-01-20 20:21:30 +00:00
|
|
|
continue;
|
2012-05-07 19:22:09 +00:00
|
|
|
case ENFILE:
|
|
|
|
if (p)
|
|
|
|
send_log(p, LOG_EMERG,
|
2018-01-29 14:06:04 +00:00
|
|
|
"Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
|
|
|
|
p->id, global.maxsock);
|
2014-05-07 17:47:02 +00:00
|
|
|
goto transient_error;
|
2012-05-07 19:22:09 +00:00
|
|
|
case EMFILE:
|
|
|
|
if (p)
|
|
|
|
send_log(p, LOG_EMERG,
|
2018-01-29 14:06:04 +00:00
|
|
|
"Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
|
|
|
|
p->id, global.maxsock);
|
2014-05-07 17:47:02 +00:00
|
|
|
goto transient_error;
|
2012-05-07 19:22:09 +00:00
|
|
|
case ENOBUFS:
|
|
|
|
case ENOMEM:
|
|
|
|
if (p)
|
|
|
|
send_log(p, LOG_EMERG,
|
2018-01-29 14:06:04 +00:00
|
|
|
"Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
|
|
|
|
p->id, global.maxsock);
|
2014-05-07 17:47:02 +00:00
|
|
|
goto transient_error;
|
2012-05-07 19:22:09 +00:00
|
|
|
default:
|
2014-01-20 20:21:30 +00:00
|
|
|
/* unexpected result, let's give up and let other tasks run */
|
2019-12-10 08:30:05 +00:00
|
|
|
max_accept = 0;
|
|
|
|
goto end;
|
2012-05-07 19:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:02:39 +00:00
|
|
|
/* we don't want to leak the FD upon reload if it's in the master */
|
|
|
|
if (unlikely(master == 1))
|
|
|
|
fcntl(cfd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
/* The connection was accepted, it must be counted as such */
|
|
|
|
if (l->counters)
|
|
|
|
HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, next_conn);
|
|
|
|
|
2019-02-27 18:32:32 +00:00
|
|
|
if (p)
|
|
|
|
HA_ATOMIC_UPDATE_MAX(&p->fe_counters.conn_max, next_feconn);
|
|
|
|
|
|
|
|
proxy_inc_fe_conn_ctr(l, p);
|
|
|
|
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
if (!(l->options & LI_O_UNLIMITED)) {
|
|
|
|
count = update_freq_ctr(&global.conn_per_sec, 1);
|
|
|
|
HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
|
|
|
|
}
|
|
|
|
|
2019-04-12 13:27:17 +00:00
|
|
|
_HA_ATOMIC_ADD(&activity[tid].accepted, 1);
|
|
|
|
|
2012-05-07 19:22:09 +00:00
|
|
|
if (unlikely(cfd >= global.maxsock)) {
|
|
|
|
send_log(p, LOG_EMERG,
|
|
|
|
"Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
|
|
|
|
p->id);
|
|
|
|
close(cfd);
|
2019-12-10 11:01:21 +00:00
|
|
|
expire = tick_add(now_ms, 1000); /* try again in 1 second */
|
|
|
|
goto limit_global;
|
2012-05-07 19:22:09 +00:00
|
|
|
}
|
|
|
|
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
/* past this point, l->accept() will automatically decrement
|
2019-02-27 18:32:32 +00:00
|
|
|
* l->nbconn, feconn and actconn once done. Setting next_*conn=0
|
|
|
|
* allows the error path not to rollback on nbconn. It's more
|
|
|
|
* convenient than duplicating all exit labels.
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
*/
|
|
|
|
next_conn = 0;
|
2019-02-27 18:32:32 +00:00
|
|
|
next_feconn = 0;
|
|
|
|
next_actconn = 0;
|
2012-05-07 19:22:09 +00:00
|
|
|
|
2019-01-27 14:37:19 +00:00
|
|
|
#if defined(USE_THREAD)
|
2019-03-13 14:03:53 +00:00
|
|
|
mask = thread_mask(l->bind_conf->bind_thread) & all_threads_mask;
|
2020-03-12 16:33:29 +00:00
|
|
|
if (atleast2(mask) && (global.tune.options & GTUNE_LISTENER_MQ) && !stopping) {
|
2019-01-27 14:37:19 +00:00
|
|
|
struct accept_queue_ring *ring;
|
2019-03-05 07:46:28 +00:00
|
|
|
unsigned int t, t0, t1, t2;
|
2019-03-04 18:57:34 +00:00
|
|
|
|
2019-03-05 07:46:28 +00:00
|
|
|
/* The principle is that we have two running indexes,
|
|
|
|
* each visiting in turn all threads bound to this
|
|
|
|
* listener. The connection will be assigned to the one
|
|
|
|
* with the least connections, and the other one will
|
|
|
|
* be updated. This provides a good fairness on short
|
2019-03-04 18:57:34 +00:00
|
|
|
* connections (round robin) and on long ones (conn
|
2019-03-05 07:46:28 +00:00
|
|
|
* count), without ever missing any idle thread.
|
2019-03-04 18:57:34 +00:00
|
|
|
*/
|
2019-03-05 07:46:28 +00:00
|
|
|
|
|
|
|
/* keep a copy for the final update. thr_idx is composite
|
|
|
|
* and made of (t2<<16) + t1.
|
|
|
|
*/
|
2019-03-06 14:26:33 +00:00
|
|
|
t0 = l->thr_idx;
|
2019-03-04 18:57:34 +00:00
|
|
|
do {
|
2019-03-05 07:46:28 +00:00
|
|
|
unsigned long m1, m2;
|
|
|
|
int q1, q2;
|
|
|
|
|
|
|
|
t2 = t1 = t0;
|
|
|
|
t2 >>= 16;
|
|
|
|
t1 &= 0xFFFF;
|
|
|
|
|
|
|
|
/* t1 walks low to high bits ;
|
|
|
|
* t2 walks high to low.
|
|
|
|
*/
|
|
|
|
m1 = mask >> t1;
|
|
|
|
m2 = mask & (t2 ? nbits(t2 + 1) : ~0UL);
|
|
|
|
|
2019-04-16 16:09:13 +00:00
|
|
|
if (unlikely(!(m1 & 1))) {
|
2019-03-05 07:46:28 +00:00
|
|
|
m1 &= ~1UL;
|
|
|
|
if (!m1) {
|
|
|
|
m1 = mask;
|
|
|
|
t1 = 0;
|
|
|
|
}
|
|
|
|
t1 += my_ffsl(m1) - 1;
|
|
|
|
}
|
2019-01-27 14:37:19 +00:00
|
|
|
|
2019-04-16 16:09:13 +00:00
|
|
|
if (unlikely(!(m2 & (1UL << t2)) || t1 == t2)) {
|
|
|
|
/* highest bit not set */
|
|
|
|
if (!m2)
|
|
|
|
m2 = mask;
|
|
|
|
|
|
|
|
t2 = my_flsl(m2) - 1;
|
|
|
|
}
|
|
|
|
|
2019-03-05 07:46:28 +00:00
|
|
|
/* now we have two distinct thread IDs belonging to the mask */
|
|
|
|
q1 = accept_queue_rings[t1].tail - accept_queue_rings[t1].head + ACCEPT_QUEUE_SIZE;
|
|
|
|
if (q1 >= ACCEPT_QUEUE_SIZE)
|
|
|
|
q1 -= ACCEPT_QUEUE_SIZE;
|
|
|
|
|
|
|
|
q2 = accept_queue_rings[t2].tail - accept_queue_rings[t2].head + ACCEPT_QUEUE_SIZE;
|
|
|
|
if (q2 >= ACCEPT_QUEUE_SIZE)
|
|
|
|
q2 -= ACCEPT_QUEUE_SIZE;
|
|
|
|
|
|
|
|
/* we have 3 possibilities now :
|
|
|
|
* q1 < q2 : t1 is less loaded than t2, so we pick it
|
|
|
|
* and update t2 (since t1 might still be
|
|
|
|
* lower than another thread)
|
|
|
|
* q1 > q2 : t2 is less loaded than t1, so we pick it
|
|
|
|
* and update t1 (since t2 might still be
|
|
|
|
* lower than another thread)
|
|
|
|
* q1 = q2 : both are equally loaded, thus we pick t1
|
|
|
|
* and update t1 as it will become more loaded
|
|
|
|
* than t2.
|
|
|
|
*/
|
2019-01-27 14:37:19 +00:00
|
|
|
|
2019-03-05 07:46:28 +00:00
|
|
|
q1 += l->thr_conn[t1];
|
|
|
|
q2 += l->thr_conn[t2];
|
2019-01-27 14:37:19 +00:00
|
|
|
|
2019-03-05 07:46:28 +00:00
|
|
|
if (q1 - q2 < 0) {
|
|
|
|
t = t1;
|
|
|
|
t2 = t2 ? t2 - 1 : LONGBITS - 1;
|
|
|
|
}
|
|
|
|
else if (q1 - q2 > 0) {
|
|
|
|
t = t2;
|
|
|
|
t1++;
|
|
|
|
if (t1 >= LONGBITS)
|
|
|
|
t1 = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t = t1;
|
|
|
|
t1++;
|
|
|
|
if (t1 >= LONGBITS)
|
|
|
|
t1 = 0;
|
|
|
|
}
|
2019-01-27 14:37:19 +00:00
|
|
|
|
2019-03-05 07:46:28 +00:00
|
|
|
/* new value for thr_idx */
|
|
|
|
t1 += (t2 << 16);
|
2019-03-08 17:52:57 +00:00
|
|
|
} while (unlikely(!_HA_ATOMIC_CAS(&l->thr_idx, &t0, t1)));
|
2019-01-27 14:37:19 +00:00
|
|
|
|
2019-03-05 07:46:28 +00:00
|
|
|
/* We successfully selected the best thread "t" for this
|
|
|
|
* connection. We use deferred accepts even if it's the
|
|
|
|
* local thread because tests show that it's the best
|
|
|
|
* performing model, likely due to better cache locality
|
|
|
|
* when processing this loop.
|
2019-01-27 14:37:19 +00:00
|
|
|
*/
|
2019-03-05 07:46:28 +00:00
|
|
|
ring = &accept_queue_rings[t];
|
2019-01-27 14:37:19 +00:00
|
|
|
if (accept_queue_push_mp(ring, cfd, l, &addr, laddr)) {
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_ADD(&activity[t].accq_pushed, 1);
|
2019-09-24 04:55:18 +00:00
|
|
|
tasklet_wakeup(ring->tasklet);
|
2019-01-27 14:37:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* If the ring is full we do a synchronous accept on
|
|
|
|
* the local thread here.
|
|
|
|
*/
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_ADD(&activity[t].accq_full, 1);
|
2019-01-27 14:37:19 +00:00
|
|
|
}
|
|
|
|
#endif // USE_THREAD
|
|
|
|
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_ADD(&l->thr_conn[tid], 1);
|
2012-05-07 19:22:09 +00:00
|
|
|
ret = l->accept(l, cfd, &addr);
|
|
|
|
if (unlikely(ret <= 0)) {
|
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 22:22:06 +00:00
|
|
|
/* The connection was closed by stream_accept(). Either
|
2012-05-07 19:22:09 +00:00
|
|
|
* we just have to ignore it (ret == 0) or it's a critical
|
|
|
|
* error due to a resource shortage, and we must stop the
|
|
|
|
* listener (ret < 0).
|
|
|
|
*/
|
|
|
|
if (ret == 0) /* successful termination */
|
|
|
|
continue;
|
|
|
|
|
2014-05-07 17:47:02 +00:00
|
|
|
goto transient_error;
|
2012-05-07 19:22:09 +00:00
|
|
|
}
|
|
|
|
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
/* increase the per-process number of cumulated sessions, this
|
|
|
|
* may only be done once l->accept() has accepted the connection.
|
|
|
|
*/
|
2013-10-07 16:51:07 +00:00
|
|
|
if (!(l->options & LI_O_UNLIMITED)) {
|
2017-05-30 13:36:50 +00:00
|
|
|
count = update_freq_ctr(&global.sess_per_sec, 1);
|
|
|
|
HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
|
2013-10-07 16:51:07 +00:00
|
|
|
}
|
2013-10-07 18:01:52 +00:00
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
|
2017-05-30 13:36:50 +00:00
|
|
|
count = update_freq_ctr(&global.ssl_per_sec, 1);
|
|
|
|
HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
|
2013-10-07 18:01:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
2013-10-07 16:51:07 +00:00
|
|
|
|
BUG/MEDIUM: listener: mark the thread as not stuck inside the loop
We tried hard to make sure we report threads as not stuck at various
crucial places, but one of them is special, it's the listener_accept()
function. The reason it is special is because it will loop a certain
number of times (default: 64) accepting incoming connections, allocating
resources, dispatching them to other threads or running L4 rules on them,
and while all of this is supposed to be extremely fast, when the machine
slows down or runs low on memory, the expectedly small delays in malloc()
caused by contention with other threads can quickly accumulate and suddenly
become critical to the point of triggering the watchdog. Furthermore, it
is technically possible to trigger this by pure configuration by setting
a huge tune.maxaccept value, which should not be possible.
Given that each operation isn't related to the same task but to a different
one each time, it is appropriate to mark the thread as not stuck each time
it accepts new work that possibly gets dispatched to other threads which
execute it.
This looks like this could be a good reason for the issue reported in
issue #388.
This fix must be backported to 2.0.
2020-05-01 07:51:11 +00:00
|
|
|
ti->flags &= ~TI_FL_STUCK; // this thread is still running
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
} /* end of for (max_accept--) */
|
2012-05-07 19:22:09 +00:00
|
|
|
|
2017-05-30 13:36:50 +00:00
|
|
|
end:
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
if (next_conn)
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_SUB(&l->nbconn, 1);
|
2019-02-25 14:02:04 +00:00
|
|
|
|
2019-02-27 18:32:32 +00:00
|
|
|
if (p && next_feconn)
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_SUB(&p->feconn, 1);
|
2019-02-27 18:32:32 +00:00
|
|
|
|
|
|
|
if (next_actconn)
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_SUB(&actconn, 1);
|
2019-02-27 18:32:32 +00:00
|
|
|
|
2019-02-27 15:49:00 +00:00
|
|
|
if ((l->state == LI_FULL && (!l->maxconn || l->nbconn < l->maxconn)) ||
|
2019-12-11 14:06:30 +00:00
|
|
|
(l->state == LI_LIMITED &&
|
|
|
|
((!p || p->feconn < p->maxconn) && (actconn < global.maxconn) &&
|
|
|
|
(!tick_isset(global_listener_queue_task->expire) ||
|
|
|
|
tick_is_expired(global_listener_queue_task->expire, now_ms))))) {
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
/* at least one thread has to this when quitting */
|
|
|
|
resume_listener(l);
|
|
|
|
|
|
|
|
/* Dequeues all of the listeners waiting for a resource */
|
2019-12-10 13:10:52 +00:00
|
|
|
dequeue_all_listeners();
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
|
2019-08-08 13:47:21 +00:00
|
|
|
if (p && !MT_LIST_ISEMPTY(&p->listener_queue) &&
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
(!p->fe_sps_lim || freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0) > 0))
|
2019-12-10 13:10:52 +00:00
|
|
|
dequeue_proxy_listeners(p);
|
MAJOR: listener: do not hold the listener lock in listener_accept()
This function used to hold the listener's lock as a way to stay safe
against concurrent manipulations, but it turns out this is wrong. First,
the lock is held during l->accept(), which itself might indirectly call
listener_release(), which, if the listener is marked full, could result
in __resume_listener() to be called and the lock being taken twice. In
practice it doesn't happen right now because the listener's FULL state
cannot change while we're doing this.
Second, all the code does is now protected against concurrent accesses.
It used not to be the case in the early days of threads : the frequency
counters are thread-safe. The rate limiting doesn't require extreme
precision. Only the nbconn check is not thread safe.
Third, the parts called here will have to be called from different
threads without holding this lock, and this becomes a bigger issue
if we need to keep this one.
This patch does 3 things which need to be addressed at once :
1) it moves the lock to the only 2 functions that were not protected
since called form listener_accept() :
- limit_listener()
- listener_full()
2) it makes sure delete_listener() properly checks its state within
the lock.
3) it updates the l->nbconn tracking to make sure that it is always
properly reported and accounted for. There is a point of particular
care around the situation where the listener's maxconn is reached
because the listener has to be marked full before accepting the
connection, then resumed if the connection finally gets dropped.
It is not possible to perform this change without removing the
lock due to the deadlock issue explained above.
This patch almost doubles the accept rate in multi-thread on a shared
port between 8 threads, and multiplies by 4 the connection rate on a
tcp-request connection reject rule.
2019-02-25 18:23:37 +00:00
|
|
|
}
|
BUG/MEDIUM: listener/thread: fix a race when pausing a listener
There exists a race in the listener code where a thread might disable
receipt on a listener's FD then turn it to LI_PAUSED while at the same
time another one faces EAGAIN on accept() and enables it again via
fd_cant_recv(). The result is that the FD is in LI_PAUSED state with
its polling still enabled. listener_accept() does not do anything then
and doesn't disable the FD either, resulting in a thread eating all the
CPU as reported in issue #358. A solution would be to take the listener's
lock to perform the fd_cant_recv() call and do it only if the FD is still
in LI_READY state, but this would be totally overkill while in practice
the issue only happens during shutdown.
Instead what is done here is that when leaving we recheck the state and
disable polling if the listener is not in LI_READY state, which never
happens except when being limited. In the worst case there could be one
extra check per thread for the time required to converge, which is
absolutely nothing.
This fix was successfully tested, and should be backported to all
versions using the lock-free listeners, which means all those containing
commit 3f0d02bb ("MAJOR: listener: do not hold the listener lock in
listener_accept()"), hence 2.1, 2.0, 1.9.7+, 1.8.20+.
2019-12-05 06:40:32 +00:00
|
|
|
|
2019-12-10 08:30:05 +00:00
|
|
|
/* Now it's getting tricky. The listener was supposed to be in LI_READY
|
|
|
|
* state but in the mean time we might have changed it to LI_FULL or
|
|
|
|
* LI_LIMITED, and another thread might also have turned it to
|
|
|
|
* LI_PAUSED, LI_LISTEN or even LI_INI when stopping a proxy. We must
|
|
|
|
* be certain to keep the FD enabled when in the READY state but we
|
|
|
|
* must also stop it for other states that we might have switched to
|
|
|
|
* while others re-enabled polling.
|
|
|
|
*/
|
|
|
|
HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
|
|
|
|
if (l->state == LI_READY) {
|
|
|
|
if (max_accept > 0)
|
|
|
|
fd_cant_recv(fd);
|
|
|
|
else
|
|
|
|
fd_done_recv(fd);
|
|
|
|
} else if (l->state > LI_ASSIGNED) {
|
BUG/MEDIUM: listener/thread: fix a race when pausing a listener
There exists a race in the listener code where a thread might disable
receipt on a listener's FD then turn it to LI_PAUSED while at the same
time another one faces EAGAIN on accept() and enables it again via
fd_cant_recv(). The result is that the FD is in LI_PAUSED state with
its polling still enabled. listener_accept() does not do anything then
and doesn't disable the FD either, resulting in a thread eating all the
CPU as reported in issue #358. A solution would be to take the listener's
lock to perform the fd_cant_recv() call and do it only if the FD is still
in LI_READY state, but this would be totally overkill while in practice
the issue only happens during shutdown.
Instead what is done here is that when leaving we recheck the state and
disable polling if the listener is not in LI_READY state, which never
happens except when being limited. In the worst case there could be one
extra check per thread for the time required to converge, which is
absolutely nothing.
This fix was successfully tested, and should be backported to all
versions using the lock-free listeners, which means all those containing
commit 3f0d02bb ("MAJOR: listener: do not hold the listener lock in
listener_accept()"), hence 2.1, 2.0, 1.9.7+, 1.8.20+.
2019-12-05 06:40:32 +00:00
|
|
|
fd_stop_recv(l->fd);
|
2019-12-10 08:30:05 +00:00
|
|
|
}
|
|
|
|
HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
|
2019-12-10 11:01:21 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
transient_error:
|
|
|
|
/* pause the listener for up to 100 ms */
|
|
|
|
expire = tick_add(now_ms, 100);
|
|
|
|
|
|
|
|
limit_global:
|
|
|
|
/* (re-)queue the listener to the global queue and set it to expire no
|
|
|
|
* later than <expire> ahead. The listener turns to LI_LIMITED.
|
|
|
|
*/
|
|
|
|
limit_listener(l, &global_listener_queue);
|
|
|
|
task_schedule(global_listener_queue_task, expire);
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
limit_proxy:
|
|
|
|
/* (re-)queue the listener to the proxy's queue and set it to expire no
|
|
|
|
* later than <expire> ahead. The listener turns to LI_LIMITED.
|
|
|
|
*/
|
|
|
|
limit_listener(l, &p->listener_queue);
|
BUG/MAJOR: listener: do not schedule a task-less proxy
Apparently seamingless commit 0591bf7deb ("MINOR: listener: make the
wait paths cleaner and more reliable") caused a nasty regression and
revealed a rare race that hits regtest stickiness/lb-services.vtc
about 4% of the times for 8 threads.
The problem is that when a multi-threaded listener wakes up on an
incoming connection, several threads can receive the event, especially
when idle. And all of them will race to accept the connections in
parallel, adjusting the listener's nbconn and proxy's feconn until
one reaches the proxy's limit and declines. At this step the changes
are cancelled, the listener is marked "limited", and when the threads
exit the function, one of them will unlimit the listener/proxy again
so that it can accept incoming connections again.
The problem happens when many threads connect to a small peers section
because its maxconn is very limited (typically 6 for 2 peers), and it's
sometimes possible for enough competing threads to hit the limit and
one of them will limit the listener and queue the proxy's task... except
that peers do not initialize their proxy task since they do not use rate
limiting. Thus the process crashes when doing task_schedule(p->task).
Prior to the cleanup patch above, this didn't happen because the error
path that was dedicated to only limiting the listener did not call
task_schedule(p->task).
Given that the proxy's task is optional, and that the expire value
passed there is always TICK_ETERNITY, it's sufficient and reasonable to
avoid calling this task_schedule() when expire is not set. And for long
term safety we can also avoid to do it when the task is not set. A first
fix consisted in allocating a task for the peers proxies but it's never
used and would eat resources for reason.
No backport is needed as this commit was only merged into 2.2.
2020-01-08 18:15:07 +00:00
|
|
|
if (p->task && tick_isset(expire))
|
|
|
|
task_schedule(p->task, expire);
|
2019-12-10 11:01:21 +00:00
|
|
|
goto end;
|
2012-05-07 19:22:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 07:19:58 +00:00
|
|
|
/* Notify the listener that a connection initiated from it was released. This
|
|
|
|
* is used to keep the connection count consistent and to possibly re-open
|
|
|
|
* listening when it was limited.
|
|
|
|
*/
|
|
|
|
void listener_release(struct listener *l)
|
|
|
|
{
|
|
|
|
struct proxy *fe = l->bind_conf->frontend;
|
|
|
|
|
|
|
|
if (!(l->options & LI_O_UNLIMITED))
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_SUB(&actconn, 1);
|
2019-02-27 18:32:32 +00:00
|
|
|
if (fe)
|
2019-03-08 17:52:57 +00:00
|
|
|
_HA_ATOMIC_SUB(&fe->feconn, 1);
|
|
|
|
_HA_ATOMIC_SUB(&l->nbconn, 1);
|
|
|
|
_HA_ATOMIC_SUB(&l->thr_conn[tid], 1);
|
2019-02-27 18:32:32 +00:00
|
|
|
|
|
|
|
if (l->state == LI_FULL || l->state == LI_LIMITED)
|
2017-09-15 07:19:58 +00:00
|
|
|
resume_listener(l);
|
|
|
|
|
|
|
|
/* Dequeues all of the listeners waiting for a resource */
|
2019-12-10 13:10:52 +00:00
|
|
|
dequeue_all_listeners();
|
2017-09-15 07:19:58 +00:00
|
|
|
|
2019-08-08 13:47:21 +00:00
|
|
|
if (!MT_LIST_ISEMPTY(&fe->listener_queue) &&
|
2017-09-15 07:19:58 +00:00
|
|
|
(!fe->fe_sps_lim || freq_ctr_remain(&fe->fe_sess_per_sec, fe->fe_sps_lim, 0) > 0))
|
2019-12-10 13:10:52 +00:00
|
|
|
dequeue_proxy_listeners(fe);
|
2017-09-15 07:19:58 +00:00
|
|
|
}
|
|
|
|
|
BUG/MAJOR: listener: fix thread safety in resume_listener()
resume_listener() can be called from a thread not part of the listener's
mask after a curr_conn has gone lower than a proxy's or the process' limit.
This results in fd_may_recv() being called unlocked if the listener is
bound to only one thread, and quickly locks up.
This patch solves this by creating a per-thread work_list dedicated to
listeners, and modifying resume_listener() so that it bounces the listener
to one of its owning thread's work_list and waking it up. This thread will
then call resume_listener() again and will perform the operation on the
file descriptor itself. It is important to do it this way so that the
listener's state cannot be modified while the listener is being moved,
otherwise multiple threads can take conflicting decisions and the listener
could be put back into the global queue if the listener was used at the
same time.
It seems like a slightly simpler approach would be possible if the locked
list API would provide the ability to return a locked element. In this
case the listener would be immediately requeued in dequeue_all_listeners()
without having to go through resume_listener() with its associated lock.
This fix must be backported to all versions having the lock-less accept
loop, which is as far as 1.8 since deadlock fixes involving this feature
had to be backported there. It is expected that the code should not differ
too much there. However, previous commit "MINOR: task: introduce work lists"
will be needed as well and should not present difficulties either. For 1.8,
the commits introducing thread_mask() and LIST_ADDED() will be needed as
well, either backporting my_flsl() or switching to my_ffsl() will be OK,
and some changes will have to be performed so that the init function is
properly called (and maybe the deinit one can be dropped).
In order to test for the fix, simply set up a multi-threaded frontend with
multiple bind lines each attached to a single thread (reproduced with 16
threads here), set up a very low maxconn value on the frontend, and inject
heavy traffic on all listeners in parallel with slightly more connections
than the configured limit ( typically +20%) so that it flips very
frequently. If the bug is still there, at some point (5-20 seconds) the
traffic will go much lower or even stop, either with spinning threads or
not.
2019-07-11 08:08:31 +00:00
|
|
|
/* resume listeners waiting in the local listener queue. They are still in LI_LIMITED state */
|
|
|
|
static struct task *listener_queue_process(struct task *t, void *context, unsigned short state)
|
|
|
|
{
|
|
|
|
struct work_list *wl = context;
|
|
|
|
struct listener *l;
|
|
|
|
|
2019-08-08 13:47:21 +00:00
|
|
|
while ((l = MT_LIST_POP(&wl->head, struct listener *, wait_queue))) {
|
BUG/MAJOR: listener: fix thread safety in resume_listener()
resume_listener() can be called from a thread not part of the listener's
mask after a curr_conn has gone lower than a proxy's or the process' limit.
This results in fd_may_recv() being called unlocked if the listener is
bound to only one thread, and quickly locks up.
This patch solves this by creating a per-thread work_list dedicated to
listeners, and modifying resume_listener() so that it bounces the listener
to one of its owning thread's work_list and waking it up. This thread will
then call resume_listener() again and will perform the operation on the
file descriptor itself. It is important to do it this way so that the
listener's state cannot be modified while the listener is being moved,
otherwise multiple threads can take conflicting decisions and the listener
could be put back into the global queue if the listener was used at the
same time.
It seems like a slightly simpler approach would be possible if the locked
list API would provide the ability to return a locked element. In this
case the listener would be immediately requeued in dequeue_all_listeners()
without having to go through resume_listener() with its associated lock.
This fix must be backported to all versions having the lock-less accept
loop, which is as far as 1.8 since deadlock fixes involving this feature
had to be backported there. It is expected that the code should not differ
too much there. However, previous commit "MINOR: task: introduce work lists"
will be needed as well and should not present difficulties either. For 1.8,
the commits introducing thread_mask() and LIST_ADDED() will be needed as
well, either backporting my_flsl() or switching to my_ffsl() will be OK,
and some changes will have to be performed so that the init function is
properly called (and maybe the deinit one can be dropped).
In order to test for the fix, simply set up a multi-threaded frontend with
multiple bind lines each attached to a single thread (reproduced with 16
threads here), set up a very low maxconn value on the frontend, and inject
heavy traffic on all listeners in parallel with slightly more connections
than the configured limit ( typically +20%) so that it flips very
frequently. If the bug is still there, at some point (5-20 seconds) the
traffic will go much lower or even stop, either with spinning threads or
not.
2019-07-11 08:08:31 +00:00
|
|
|
/* The listeners are still in the LI_LIMITED state */
|
|
|
|
resume_listener(l);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initializes the listener queues. Returns 0 on success, otherwise ERR_* flags */
|
|
|
|
static int listener_queue_init()
|
|
|
|
{
|
|
|
|
local_listener_queue = work_list_create(global.nbthread, listener_queue_process, NULL);
|
|
|
|
if (!local_listener_queue) {
|
|
|
|
ha_alert("Out of memory while initializing listener queues.\n");
|
|
|
|
return ERR_FATAL|ERR_ABORT;
|
|
|
|
}
|
2019-12-10 10:18:41 +00:00
|
|
|
|
|
|
|
global_listener_queue_task = task_new(MAX_THREADS_MASK);
|
|
|
|
if (!global_listener_queue_task) {
|
|
|
|
ha_alert("Out of memory when initializing global listener queue\n");
|
|
|
|
return ERR_FATAL|ERR_ABORT;
|
|
|
|
}
|
|
|
|
/* very simple initialization, users will queue the task if needed */
|
|
|
|
global_listener_queue_task->context = NULL; /* not even a context! */
|
|
|
|
global_listener_queue_task->process = manage_global_listener_queue;
|
|
|
|
|
BUG/MAJOR: listener: fix thread safety in resume_listener()
resume_listener() can be called from a thread not part of the listener's
mask after a curr_conn has gone lower than a proxy's or the process' limit.
This results in fd_may_recv() being called unlocked if the listener is
bound to only one thread, and quickly locks up.
This patch solves this by creating a per-thread work_list dedicated to
listeners, and modifying resume_listener() so that it bounces the listener
to one of its owning thread's work_list and waking it up. This thread will
then call resume_listener() again and will perform the operation on the
file descriptor itself. It is important to do it this way so that the
listener's state cannot be modified while the listener is being moved,
otherwise multiple threads can take conflicting decisions and the listener
could be put back into the global queue if the listener was used at the
same time.
It seems like a slightly simpler approach would be possible if the locked
list API would provide the ability to return a locked element. In this
case the listener would be immediately requeued in dequeue_all_listeners()
without having to go through resume_listener() with its associated lock.
This fix must be backported to all versions having the lock-less accept
loop, which is as far as 1.8 since deadlock fixes involving this feature
had to be backported there. It is expected that the code should not differ
too much there. However, previous commit "MINOR: task: introduce work lists"
will be needed as well and should not present difficulties either. For 1.8,
the commits introducing thread_mask() and LIST_ADDED() will be needed as
well, either backporting my_flsl() or switching to my_ffsl() will be OK,
and some changes will have to be performed so that the init function is
properly called (and maybe the deinit one can be dropped).
In order to test for the fix, simply set up a multi-threaded frontend with
multiple bind lines each attached to a single thread (reproduced with 16
threads here), set up a very low maxconn value on the frontend, and inject
heavy traffic on all listeners in parallel with slightly more connections
than the configured limit ( typically +20%) so that it flips very
frequently. If the bug is still there, at some point (5-20 seconds) the
traffic will go much lower or even stop, either with spinning threads or
not.
2019-07-11 08:08:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void listener_queue_deinit()
|
|
|
|
{
|
|
|
|
work_list_destroy(local_listener_queue, global.nbthread);
|
2019-12-10 10:18:41 +00:00
|
|
|
task_destroy(global_listener_queue_task);
|
|
|
|
global_listener_queue_task = NULL;
|
BUG/MAJOR: listener: fix thread safety in resume_listener()
resume_listener() can be called from a thread not part of the listener's
mask after a curr_conn has gone lower than a proxy's or the process' limit.
This results in fd_may_recv() being called unlocked if the listener is
bound to only one thread, and quickly locks up.
This patch solves this by creating a per-thread work_list dedicated to
listeners, and modifying resume_listener() so that it bounces the listener
to one of its owning thread's work_list and waking it up. This thread will
then call resume_listener() again and will perform the operation on the
file descriptor itself. It is important to do it this way so that the
listener's state cannot be modified while the listener is being moved,
otherwise multiple threads can take conflicting decisions and the listener
could be put back into the global queue if the listener was used at the
same time.
It seems like a slightly simpler approach would be possible if the locked
list API would provide the ability to return a locked element. In this
case the listener would be immediately requeued in dequeue_all_listeners()
without having to go through resume_listener() with its associated lock.
This fix must be backported to all versions having the lock-less accept
loop, which is as far as 1.8 since deadlock fixes involving this feature
had to be backported there. It is expected that the code should not differ
too much there. However, previous commit "MINOR: task: introduce work lists"
will be needed as well and should not present difficulties either. For 1.8,
the commits introducing thread_mask() and LIST_ADDED() will be needed as
well, either backporting my_flsl() or switching to my_ffsl() will be OK,
and some changes will have to be performed so that the init function is
properly called (and maybe the deinit one can be dropped).
In order to test for the fix, simply set up a multi-threaded frontend with
multiple bind lines each attached to a single thread (reproduced with 16
threads here), set up a very low maxconn value on the frontend, and inject
heavy traffic on all listeners in parallel with slightly more connections
than the configured limit ( typically +20%) so that it flips very
frequently. If the bug is still there, at some point (5-20 seconds) the
traffic will go much lower or even stop, either with spinning threads or
not.
2019-07-11 08:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_CONFIG_POSTPARSER("multi-threaded listener queue", listener_queue_init);
|
|
|
|
REGISTER_POST_DEINIT(listener_queue_deinit);
|
|
|
|
|
2019-12-10 10:18:41 +00:00
|
|
|
|
|
|
|
/* This is the global management task for listeners. It enables listeners waiting
|
|
|
|
* for global resources when there are enough free resource, or at least once in
|
|
|
|
* a while. It is designed to be called as a task.
|
|
|
|
*/
|
|
|
|
static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state)
|
|
|
|
{
|
|
|
|
/* If there are still too many concurrent connections, let's wait for
|
|
|
|
* some of them to go away. We don't need to re-arm the timer because
|
|
|
|
* each of them will scan the queue anyway.
|
|
|
|
*/
|
|
|
|
if (unlikely(actconn >= global.maxconn))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* We should periodically try to enable listeners waiting for a global
|
|
|
|
* resource here, because it is possible, though very unlikely, that
|
|
|
|
* they have been blocked by a temporary lack of global resource such
|
|
|
|
* as a file descriptor or memory and that the temporary condition has
|
|
|
|
* disappeared.
|
|
|
|
*/
|
|
|
|
dequeue_all_listeners();
|
|
|
|
|
|
|
|
out:
|
|
|
|
t->expire = TICK_ETERNITY;
|
|
|
|
task_queue(t);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2012-09-12 21:17:10 +00:00
|
|
|
/*
|
|
|
|
* Registers the bind keyword list <kwl> as a list of valid keywords for next
|
|
|
|
* parsing sessions.
|
|
|
|
*/
|
|
|
|
void bind_register_keywords(struct bind_kw_list *kwl)
|
|
|
|
{
|
|
|
|
LIST_ADDQ(&bind_keywords.list, &kwl->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
|
|
|
|
* keyword is found with a NULL ->parse() function, then an attempt is made to
|
|
|
|
* find one with a valid ->parse() function. This way it is possible to declare
|
|
|
|
* platform-dependant, known keywords as NULL, then only declare them as valid
|
|
|
|
* if some options are met. Note that if the requested keyword contains an
|
|
|
|
* opening parenthesis, everything from this point is ignored.
|
|
|
|
*/
|
|
|
|
struct bind_kw *bind_find_kw(const char *kw)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
const char *kwend;
|
|
|
|
struct bind_kw_list *kwl;
|
|
|
|
struct bind_kw *ret = NULL;
|
|
|
|
|
|
|
|
kwend = strchr(kw, '(');
|
|
|
|
if (!kwend)
|
|
|
|
kwend = kw + strlen(kw);
|
|
|
|
|
|
|
|
list_for_each_entry(kwl, &bind_keywords.list, list) {
|
|
|
|
for (index = 0; kwl->kw[index].kw != NULL; index++) {
|
|
|
|
if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
|
|
|
|
kwl->kw[index].kw[kwend-kw] == 0) {
|
|
|
|
if (kwl->kw[index].parse)
|
|
|
|
return &kwl->kw[index]; /* found it !*/
|
|
|
|
else
|
|
|
|
ret = &kwl->kw[index]; /* may be OK */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-18 16:01:17 +00:00
|
|
|
/* Dumps all registered "bind" keywords to the <out> string pointer. The
|
|
|
|
* unsupported keywords are only dumped if their supported form was not
|
|
|
|
* found.
|
|
|
|
*/
|
|
|
|
void bind_dump_kws(char **out)
|
|
|
|
{
|
|
|
|
struct bind_kw_list *kwl;
|
|
|
|
int index;
|
|
|
|
|
2020-05-18 10:14:18 +00:00
|
|
|
if (!out)
|
|
|
|
return;
|
|
|
|
|
2012-09-18 16:01:17 +00:00
|
|
|
*out = NULL;
|
|
|
|
list_for_each_entry(kwl, &bind_keywords.list, list) {
|
|
|
|
for (index = 0; kwl->kw[index].kw != NULL; index++) {
|
|
|
|
if (kwl->kw[index].parse ||
|
|
|
|
bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
|
2012-09-18 16:24:39 +00:00
|
|
|
memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
|
|
|
|
kwl->scope,
|
2012-09-18 16:01:17 +00:00
|
|
|
kwl->kw[index].kw,
|
2012-09-18 16:24:39 +00:00
|
|
|
kwl->kw[index].skip ? " <arg>" : "",
|
|
|
|
kwl->kw[index].parse ? "" : " (not supported)");
|
2012-09-18 16:01:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-24 18:55:15 +00:00
|
|
|
/************************************************************************/
|
2013-01-07 21:54:17 +00:00
|
|
|
/* All supported sample and ACL keywords must be declared here. */
|
2010-05-24 18:55:15 +00:00
|
|
|
/************************************************************************/
|
|
|
|
|
2011-12-16 16:06:15 +00:00
|
|
|
/* set temp integer to the number of connexions to the same listening socket */
|
2010-05-24 18:55:15 +00:00
|
|
|
static int
|
2015-05-11 13:42:45 +00:00
|
|
|
smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
|
2010-05-24 18:55:15 +00:00
|
|
|
{
|
2015-08-19 07:00:18 +00:00
|
|
|
smp->data.type = SMP_T_SINT;
|
2015-08-19 07:07:19 +00:00
|
|
|
smp->data.u.sint = smp->sess->listener->nbconn;
|
2010-05-24 18:55:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-12-16 16:06:15 +00:00
|
|
|
/* set temp integer to the id of the socket (listener) */
|
2010-05-24 18:55:15 +00:00
|
|
|
static int
|
2015-05-11 13:42:45 +00:00
|
|
|
smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
|
2012-04-23 14:16:37 +00:00
|
|
|
{
|
2015-08-19 07:00:18 +00:00
|
|
|
smp->data.type = SMP_T_SINT;
|
2015-08-19 07:07:19 +00:00
|
|
|
smp->data.u.sint = smp->sess->listener->luid;
|
2010-05-24 18:55:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-27 21:08:40 +00:00
|
|
|
static int
|
|
|
|
smp_fetch_so_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
|
|
|
|
{
|
|
|
|
smp->data.u.str.area = smp->sess->listener->name;
|
|
|
|
if (!smp->data.u.str.area)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
smp->data.type = SMP_T_STR;
|
|
|
|
smp->flags = SMP_F_CONST;
|
|
|
|
smp->data.u.str.data = strlen(smp->data.u.str.area);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-05-24 18:55:15 +00:00
|
|
|
|
2012-09-18 15:17:28 +00:00
|
|
|
/* parse the "accept-proxy" bind keyword */
|
2012-09-20 14:48:07 +00:00
|
|
|
static int bind_parse_accept_proxy(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
|
2012-09-18 15:17:28 +00:00
|
|
|
{
|
|
|
|
struct listener *l;
|
|
|
|
|
2012-09-20 14:48:07 +00:00
|
|
|
list_for_each_entry(l, &conf->listeners, by_bind)
|
2012-09-18 15:17:28 +00:00
|
|
|
l->options |= LI_O_ACC_PROXY;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-04 14:11:10 +00:00
|
|
|
/* parse the "accept-netscaler-cip" bind keyword */
|
|
|
|
static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
|
|
|
|
{
|
|
|
|
struct listener *l;
|
|
|
|
uint32_t val;
|
|
|
|
|
|
|
|
if (!*args[cur_arg + 1]) {
|
|
|
|
memprintf(err, "'%s' : missing value", args[cur_arg]);
|
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = atol(args[cur_arg + 1]);
|
|
|
|
if (val <= 0) {
|
2019-02-27 14:39:41 +00:00
|
|
|
memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
|
2016-06-04 14:11:10 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry(l, &conf->listeners, by_bind) {
|
|
|
|
l->options |= LI_O_ACC_CIP;
|
|
|
|
conf->ns_cip_magic = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-18 15:17:28 +00:00
|
|
|
/* parse the "backlog" bind keyword */
|
2012-09-20 14:48:07 +00:00
|
|
|
static int bind_parse_backlog(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
|
2012-09-18 15:17:28 +00:00
|
|
|
{
|
|
|
|
struct listener *l;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
if (!*args[cur_arg + 1]) {
|
2012-09-20 17:43:14 +00:00
|
|
|
memprintf(err, "'%s' : missing value", args[cur_arg]);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = atol(args[cur_arg + 1]);
|
2019-02-27 14:39:41 +00:00
|
|
|
if (val < 0) {
|
2012-09-20 17:43:14 +00:00
|
|
|
memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:48:07 +00:00
|
|
|
list_for_each_entry(l, &conf->listeners, by_bind)
|
2012-09-18 15:17:28 +00:00
|
|
|
l->backlog = val;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the "id" bind keyword */
|
2012-09-20 14:48:07 +00:00
|
|
|
static int bind_parse_id(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
|
2012-09-18 15:17:28 +00:00
|
|
|
{
|
|
|
|
struct eb32_node *node;
|
2012-09-20 14:48:07 +00:00
|
|
|
struct listener *l, *new;
|
2016-02-26 07:45:58 +00:00
|
|
|
char *error;
|
2012-09-18 15:17:28 +00:00
|
|
|
|
2012-09-20 14:48:07 +00:00
|
|
|
if (conf->listeners.n != conf->listeners.p) {
|
2012-09-20 17:43:14 +00:00
|
|
|
memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*args[cur_arg + 1]) {
|
2012-09-20 17:43:14 +00:00
|
|
|
memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:48:07 +00:00
|
|
|
new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
|
2016-02-26 07:45:58 +00:00
|
|
|
new->luid = strtol(args[cur_arg + 1], &error, 10);
|
|
|
|
if (*error != '\0') {
|
|
|
|
memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
|
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
2012-09-20 14:48:07 +00:00
|
|
|
new->conf.id.key = new->luid;
|
2012-09-18 15:17:28 +00:00
|
|
|
|
2012-09-20 14:48:07 +00:00
|
|
|
if (new->luid <= 0) {
|
2012-09-20 17:43:14 +00:00
|
|
|
memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:48:07 +00:00
|
|
|
node = eb32_lookup(&px->conf.used_listener_id, new->luid);
|
2012-09-18 15:17:28 +00:00
|
|
|
if (node) {
|
|
|
|
l = container_of(node, struct listener, conf.id);
|
2012-09-20 17:43:14 +00:00
|
|
|
memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
|
|
|
|
args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
|
|
|
|
l->bind_conf->arg);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:48:07 +00:00
|
|
|
eb32_insert(&px->conf.used_listener_id, &new->conf.id);
|
2012-09-18 15:17:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the "maxconn" bind keyword */
|
2012-09-20 14:48:07 +00:00
|
|
|
static int bind_parse_maxconn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
|
2012-09-18 15:17:28 +00:00
|
|
|
{
|
|
|
|
struct listener *l;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
if (!*args[cur_arg + 1]) {
|
2012-09-20 17:43:14 +00:00
|
|
|
memprintf(err, "'%s' : missing value", args[cur_arg]);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = atol(args[cur_arg + 1]);
|
2019-02-27 15:49:00 +00:00
|
|
|
if (val < 0) {
|
|
|
|
memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:48:07 +00:00
|
|
|
list_for_each_entry(l, &conf->listeners, by_bind)
|
2012-09-18 15:17:28 +00:00
|
|
|
l->maxconn = val;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the "name" bind keyword */
|
2012-09-20 14:48:07 +00:00
|
|
|
static int bind_parse_name(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
|
2012-09-18 15:17:28 +00:00
|
|
|
{
|
|
|
|
struct listener *l;
|
|
|
|
|
|
|
|
if (!*args[cur_arg + 1]) {
|
2012-09-20 17:43:14 +00:00
|
|
|
memprintf(err, "'%s' : missing name", args[cur_arg]);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:48:07 +00:00
|
|
|
list_for_each_entry(l, &conf->listeners, by_bind)
|
2012-09-18 15:17:28 +00:00
|
|
|
l->name = strdup(args[cur_arg + 1]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the "nice" bind keyword */
|
2012-09-20 14:48:07 +00:00
|
|
|
static int bind_parse_nice(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
|
2012-09-18 15:17:28 +00:00
|
|
|
{
|
|
|
|
struct listener *l;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
if (!*args[cur_arg + 1]) {
|
2012-09-20 17:43:14 +00:00
|
|
|
memprintf(err, "'%s' : missing value", args[cur_arg]);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = atol(args[cur_arg + 1]);
|
|
|
|
if (val < -1024 || val > 1024) {
|
2012-09-20 17:43:14 +00:00
|
|
|
memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
|
2012-09-18 15:17:28 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:48:07 +00:00
|
|
|
list_for_each_entry(l, &conf->listeners, by_bind)
|
2012-09-18 15:17:28 +00:00
|
|
|
l->nice = val;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-07 17:01:58 +00:00
|
|
|
/* parse the "process" bind keyword */
|
|
|
|
static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
|
|
|
|
{
|
2017-11-23 21:44:11 +00:00
|
|
|
char *slash;
|
|
|
|
unsigned long proc = 0, thread = 0;
|
2014-05-07 17:01:58 +00:00
|
|
|
|
2017-11-23 21:44:11 +00:00
|
|
|
if ((slash = strchr(args[cur_arg + 1], '/')) != NULL)
|
|
|
|
*slash = 0;
|
|
|
|
|
2019-02-07 09:39:36 +00:00
|
|
|
if (parse_process_number(args[cur_arg + 1], &proc, MAX_PROCS, NULL, err)) {
|
2017-11-22 11:06:43 +00:00
|
|
|
memprintf(err, "'%s' : %s", args[cur_arg], *err);
|
2014-05-07 17:01:58 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
2017-11-23 21:44:11 +00:00
|
|
|
if (slash) {
|
2019-01-26 12:25:14 +00:00
|
|
|
if (parse_process_number(slash+1, &thread, MAX_THREADS, NULL, err)) {
|
2017-11-23 21:44:11 +00:00
|
|
|
memprintf(err, "'%s' : %s", args[cur_arg], *err);
|
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
*slash = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
conf->bind_proc |= proc;
|
2019-02-02 12:14:34 +00:00
|
|
|
conf->bind_thread |= thread;
|
2014-05-07 17:01:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-09-18 15:17:28 +00:00
|
|
|
|
2018-04-10 12:43:00 +00:00
|
|
|
/* parse the "proto" bind keyword */
|
|
|
|
static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
|
|
|
|
{
|
|
|
|
struct ist proto;
|
|
|
|
|
|
|
|
if (!*args[cur_arg + 1]) {
|
|
|
|
memprintf(err, "'%s' : missing value", args[cur_arg]);
|
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
proto = ist2(args[cur_arg + 1], strlen(args[cur_arg + 1]));
|
|
|
|
conf->mux_proto = get_mux_proto(proto);
|
|
|
|
if (!conf->mux_proto) {
|
|
|
|
memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]);
|
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:02:18 +00:00
|
|
|
/* config parser for global "tune.listener.multi-queue", accepts "on" or "off" */
|
|
|
|
static int cfg_parse_tune_listener_mq(char **args, int section_type, struct proxy *curpx,
|
|
|
|
struct proxy *defpx, const char *file, int line,
|
|
|
|
char **err)
|
|
|
|
{
|
|
|
|
if (too_many_args(1, args, err, NULL))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (strcmp(args[1], "on") == 0)
|
|
|
|
global.tune.options |= GTUNE_LISTENER_MQ;
|
|
|
|
else if (strcmp(args[1], "off") == 0)
|
|
|
|
global.tune.options &= ~GTUNE_LISTENER_MQ;
|
|
|
|
else {
|
|
|
|
memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-07 21:54:17 +00:00
|
|
|
/* Note: must not be declared <const> as its list will be overwritten.
|
|
|
|
* Please take care of keeping this list alphabetically sorted.
|
|
|
|
*/
|
2013-06-21 21:16:39 +00:00
|
|
|
static struct sample_fetch_kw_list smp_kws = {ILH, {
|
2015-07-06 21:43:03 +00:00
|
|
|
{ "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
|
|
|
|
{ "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
|
2020-03-27 21:08:40 +00:00
|
|
|
{ "so_name", smp_fetch_so_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
|
2013-01-07 21:54:17 +00:00
|
|
|
{ /* END */ },
|
|
|
|
}};
|
|
|
|
|
2018-11-25 18:14:37 +00:00
|
|
|
INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
|
|
|
|
|
2012-04-19 16:42:05 +00:00
|
|
|
/* Note: must not be declared <const> as its list will be overwritten.
|
|
|
|
* Please take care of keeping this list alphabetically sorted.
|
|
|
|
*/
|
2013-06-21 21:16:39 +00:00
|
|
|
static struct acl_kw_list acl_kws = {ILH, {
|
2013-01-07 21:54:17 +00:00
|
|
|
{ /* END */ },
|
2010-05-24 18:55:15 +00:00
|
|
|
}};
|
|
|
|
|
2018-11-25 18:14:37 +00:00
|
|
|
INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
|
|
|
|
|
2012-09-18 15:17:28 +00:00
|
|
|
/* Note: must not be declared <const> as its list will be overwritten.
|
|
|
|
* Please take care of keeping this list alphabetically sorted, doing so helps
|
|
|
|
* all code contributors.
|
|
|
|
* Optional keywords are also declared with a NULL ->parse() function so that
|
|
|
|
* the config parser can report an appropriate error when a known keyword was
|
|
|
|
* not enabled.
|
|
|
|
*/
|
2012-09-18 16:24:39 +00:00
|
|
|
static struct bind_kw_list bind_kws = { "ALL", { }, {
|
2016-06-04 14:11:10 +00:00
|
|
|
{ "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
|
2012-09-18 15:17:28 +00:00
|
|
|
{ "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
|
|
|
|
{ "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
|
|
|
|
{ "id", bind_parse_id, 1 }, /* set id of listening socket */
|
|
|
|
{ "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
|
|
|
|
{ "name", bind_parse_name, 1 }, /* set name of listening socket */
|
|
|
|
{ "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
|
2014-05-07 17:01:58 +00:00
|
|
|
{ "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
|
2018-04-10 12:43:00 +00:00
|
|
|
{ "proto", bind_parse_proto, 1 }, /* set the proto to use for all incoming connections */
|
2013-01-07 21:54:17 +00:00
|
|
|
{ /* END */ },
|
2012-09-18 15:17:28 +00:00
|
|
|
}};
|
|
|
|
|
2018-11-25 18:14:37 +00:00
|
|
|
INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
|
|
|
|
|
2019-02-27 11:02:18 +00:00
|
|
|
/* config keyword parsers */
|
|
|
|
static struct cfg_kw_list cfg_kws = {ILH, {
|
|
|
|
{ CFG_GLOBAL, "tune.listener.multi-queue", cfg_parse_tune_listener_mq },
|
|
|
|
{ 0, NULL, NULL }
|
|
|
|
}};
|
|
|
|
|
|
|
|
INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
|
|
|
|
|
2010-05-24 18:55:15 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|