blob: 70d13ea84333423058b06f1408e238d3f6c72d9b [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreaud1d54542012-09-12 22:58:11 +02002 * Listener management functions.
Willy Tarreaudd815982007-10-16 12:25:14 +02003 *
Willy Tarreau0ccb7442013-01-07 22:54:17 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaudd815982007-10-16 12:25:14 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau6ae1ba62014-05-07 19:01:58 +020013#include <ctype.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020014#include <errno.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020015#include <stdio.h>
16#include <string.h>
Willy Tarreau95ccdde2014-02-01 09:28:36 +010017#include <unistd.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020018
Willy Tarreaudcc048a2020-06-04 19:11:43 +020019#include <haproxy/acl.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020020#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020021#include <haproxy/activity.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020022#include <haproxy/cfgparse.h>
Willy Tarreaudbf78022021-10-06 09:05:08 +020023#include <haproxy/cli-t.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020024#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020025#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/fd.h>
27#include <haproxy/freq_ctr.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020028#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020029#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020030#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/protocol.h>
Willy Tarreau5958c432021-05-08 20:30:37 +020033#include <haproxy/proxy.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020034#include <haproxy/quic_tp.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020037#include <haproxy/task.h>
Willy Tarreau9310f482021-10-06 16:18:40 +020038#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/tools.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020040
Willy Tarreaub648d632007-10-28 22:13:50 +010041
Willy Tarreau26982662012-09-12 23:17:10 +020042/* List head of all known bind keywords */
Willy Tarreauca1acd62022-03-29 15:02:44 +020043struct bind_kw_list bind_keywords = {
Willy Tarreau26982662012-09-12 23:17:10 +020044 .list = LIST_HEAD_INIT(bind_keywords.list)
45};
46
Willy Tarreaua1d97f82019-12-10 11:18:41 +010047/* list of the temporarily limited listeners because of lack of resource */
48static struct mt_list global_listener_queue = MT_LIST_HEAD_INIT(global_listener_queue);
49static struct task *global_listener_queue_task;
Willy Tarreau96151022023-05-11 13:51:31 +020050/* number of times an accepted connection resulted in maxconn being reached */
51ullong maxconn_reached = 0;
Willy Tarreau469fa472022-11-22 09:08:23 +010052__decl_thread(static HA_RWLOCK_T global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +010053
William Dauchy3679d0c2021-02-14 23:22:55 +010054/* listener status for stats */
55const char* li_status_st[LI_STATE_COUNT] = {
56 [LI_STATUS_WAITING] = "WAITING",
57 [LI_STATUS_OPEN] = "OPEN",
58 [LI_STATUS_FULL] = "FULL",
59};
Willy Tarreaua1d97f82019-12-10 11:18:41 +010060
Willy Tarreau1efafce2019-01-27 15:37:19 +010061#if defined(USE_THREAD)
62
63struct accept_queue_ring accept_queue_rings[MAX_THREADS] __attribute__((aligned(64))) = { };
64
65/* dequeue and process a pending connection from the local accept queue (single
Willy Tarreau83efc322020-10-14 17:37:17 +020066 * consumer). Returns the accepted connection or NULL if none was found.
Willy Tarreau1efafce2019-01-27 15:37:19 +010067 */
Willy Tarreau83efc322020-10-14 17:37:17 +020068struct connection *accept_queue_pop_sc(struct accept_queue_ring *ring)
Willy Tarreau1efafce2019-01-27 15:37:19 +010069{
Willy Tarreau1efafce2019-01-27 15:37:19 +010070 unsigned int pos, next;
Willy Tarreau83efc322020-10-14 17:37:17 +020071 struct connection *ptr;
72 struct connection **e;
Willy Tarreaue6f5ab52023-04-20 11:05:28 +020073 uint32_t idx = _HA_ATOMIC_LOAD(&ring->idx); /* (head << 16) + tail */
Willy Tarreau1efafce2019-01-27 15:37:19 +010074
Willy Tarreaue6f5ab52023-04-20 11:05:28 +020075 pos = idx >> 16;
76 if (pos == (uint16_t)idx)
Willy Tarreau83efc322020-10-14 17:37:17 +020077 return NULL;
Willy Tarreau1efafce2019-01-27 15:37:19 +010078
79 next = pos + 1;
80 if (next >= ACCEPT_QUEUE_SIZE)
81 next = 0;
82
83 e = &ring->entry[pos];
84
85 /* wait for the producer to update the listener's pointer */
86 while (1) {
Willy Tarreau83efc322020-10-14 17:37:17 +020087 ptr = *e;
Willy Tarreau1efafce2019-01-27 15:37:19 +010088 __ha_barrier_load();
89 if (ptr)
90 break;
91 pl_cpu_relax();
92 }
93
Willy Tarreau1efafce2019-01-27 15:37:19 +010094 /* release the entry */
Willy Tarreau83efc322020-10-14 17:37:17 +020095 *e = NULL;
Willy Tarreau1efafce2019-01-27 15:37:19 +010096
97 __ha_barrier_store();
Willy Tarreaue6f5ab52023-04-20 11:05:28 +020098 do {
99 pos = (next << 16) | (idx & 0xffff);
100 } while (unlikely(!HA_ATOMIC_CAS(&ring->idx, &idx, pos) && __ha_cpu_relax()));
101
Willy Tarreau83efc322020-10-14 17:37:17 +0200102 return ptr;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100103}
104
105
Willy Tarreau83efc322020-10-14 17:37:17 +0200106/* tries to push a new accepted connection <conn> into ring <ring>. Returns
107 * non-zero if it succeeds, or zero if the ring is full. Supports multiple
108 * producers.
Willy Tarreau1efafce2019-01-27 15:37:19 +0100109 */
Willy Tarreau83efc322020-10-14 17:37:17 +0200110int accept_queue_push_mp(struct accept_queue_ring *ring, struct connection *conn)
Willy Tarreau1efafce2019-01-27 15:37:19 +0100111{
Willy Tarreau1efafce2019-01-27 15:37:19 +0100112 unsigned int pos, next;
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200113 uint32_t idx = _HA_ATOMIC_LOAD(&ring->idx); /* (head << 16) + tail */
Willy Tarreau1efafce2019-01-27 15:37:19 +0100114
Willy Tarreau1efafce2019-01-27 15:37:19 +0100115 do {
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200116 pos = (uint16_t)idx;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100117 next = pos + 1;
118 if (next >= ACCEPT_QUEUE_SIZE)
119 next = 0;
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200120 if (next == (idx >> 16))
Willy Tarreau1efafce2019-01-27 15:37:19 +0100121 return 0; // ring full
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200122 next |= (idx & 0xffff0000U);
123 } while (unlikely(!_HA_ATOMIC_CAS(&ring->idx, &idx, next) && __ha_cpu_relax()));
Willy Tarreau1efafce2019-01-27 15:37:19 +0100124
Willy Tarreau83efc322020-10-14 17:37:17 +0200125 ring->entry[pos] = conn;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100126 __ha_barrier_store();
Willy Tarreau1efafce2019-01-27 15:37:19 +0100127 return 1;
128}
129
Willy Tarreaufb5401f2021-01-29 12:25:23 +0100130/* proceed with accepting new connections. Don't mark it static so that it appears
131 * in task dumps.
132 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100133struct task *accept_queue_process(struct task *t, void *context, unsigned int state)
Willy Tarreau1efafce2019-01-27 15:37:19 +0100134{
135 struct accept_queue_ring *ring = context;
Willy Tarreau83efc322020-10-14 17:37:17 +0200136 struct connection *conn;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100137 struct listener *li;
Christopher Faulet102854c2019-04-30 12:17:13 +0200138 unsigned int max_accept;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100139 int ret;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100140
Christopher Faulet102854c2019-04-30 12:17:13 +0200141 /* if global.tune.maxaccept is -1, then max_accept is UINT_MAX. It
142 * is not really illimited, but it is probably enough.
143 */
Willy Tarreau66161322021-02-19 15:50:27 +0100144 max_accept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Christopher Faulet102854c2019-04-30 12:17:13 +0200145 for (; max_accept; max_accept--) {
Willy Tarreau83efc322020-10-14 17:37:17 +0200146 conn = accept_queue_pop_sc(ring);
147 if (!conn)
Willy Tarreau1efafce2019-01-27 15:37:19 +0100148 break;
149
Willy Tarreau83efc322020-10-14 17:37:17 +0200150 li = __objt_listener(conn->target);
Willy Tarreaufea8c192023-02-28 10:25:57 +0100151 _HA_ATOMIC_INC(&li->thr_conn[ti->ltid]);
Willy Tarreau30836152023-01-12 19:10:17 +0100152 ret = li->bind_conf->accept(conn);
Willy Tarreau1efafce2019-01-27 15:37:19 +0100153 if (ret <= 0) {
154 /* connection was terminated by the application */
155 continue;
156 }
157
158 /* increase the per-process number of cumulated sessions, this
Willy Tarreau30836152023-01-12 19:10:17 +0100159 * may only be done once l->bind_conf->accept() has accepted the
160 * connection.
Willy Tarreau1efafce2019-01-27 15:37:19 +0100161 */
Willy Tarreau17146802023-01-12 19:58:42 +0100162 if (!(li->bind_conf->options & BC_O_UNLIMITED)) {
Willy Tarreau1efafce2019-01-27 15:37:19 +0100163 HA_ATOMIC_UPDATE_MAX(&global.sps_max,
164 update_freq_ctr(&global.sess_per_sec, 1));
Ilya Shipitsin83f54b92023-04-26 21:05:12 +0200165 if (li->bind_conf->options & BC_O_USE_SSL) {
Willy Tarreau1efafce2019-01-27 15:37:19 +0100166 HA_ATOMIC_UPDATE_MAX(&global.ssl_max,
167 update_freq_ctr(&global.ssl_per_sec, 1));
168 }
169 }
170 }
171
172 /* ran out of budget ? Let's come here ASAP */
Christopher Faulet102854c2019-04-30 12:17:13 +0200173 if (!max_accept)
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200174 tasklet_wakeup(ring->tasklet);
Willy Tarreau1efafce2019-01-27 15:37:19 +0100175
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200176 return NULL;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100177}
178
179/* Initializes the accept-queues. Returns 0 on success, otherwise ERR_* flags */
180static int accept_queue_init()
181{
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200182 struct tasklet *t;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100183 int i;
184
185 for (i = 0; i < global.nbthread; i++) {
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200186 t = tasklet_new();
Willy Tarreau1efafce2019-01-27 15:37:19 +0100187 if (!t) {
188 ha_alert("Out of memory while initializing accept queue for thread %d\n", i);
189 return ERR_FATAL|ERR_ABORT;
190 }
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200191 t->tid = i;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100192 t->process = accept_queue_process;
193 t->context = &accept_queue_rings[i];
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200194 accept_queue_rings[i].tasklet = t;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100195 }
196 return 0;
197}
198
199REGISTER_CONFIG_POSTPARSER("multi-threaded accept queue", accept_queue_init);
200
Willy Tarreaue01b08d2022-04-27 18:42:47 +0200201static void accept_queue_deinit()
202{
203 int i;
204
205 for (i = 0; i < global.nbthread; i++) {
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200206 tasklet_free(accept_queue_rings[i].tasklet);
Willy Tarreaue01b08d2022-04-27 18:42:47 +0200207 }
208}
209
210REGISTER_POST_DEINIT(accept_queue_deinit);
211
Willy Tarreau1efafce2019-01-27 15:37:19 +0100212#endif // USE_THREAD
213
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200214/* Memory allocation and initialization of the per_thr field (one entry per
215 * bound thread).
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100216 * Returns 0 if the field has been successfully initialized, -1 on failure.
217 */
218int li_init_per_thr(struct listener *li)
219{
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200220 int nbthr = MIN(global.nbthread, MAX_THREADS_PER_GROUP);
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100221 int i;
222
223 /* allocate per-thread elements for listener */
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200224 li->per_thr = calloc(nbthr, sizeof(*li->per_thr));
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100225 if (!li->per_thr)
226 return -1;
227
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200228 for (i = 0; i < nbthr; ++i) {
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100229 MT_LIST_INIT(&li->per_thr[i].quic_accept.list);
230 MT_LIST_INIT(&li->per_thr[i].quic_accept.conns);
231
232 li->per_thr[i].li = li;
233 }
234
235 return 0;
236}
237
William Dauchy3679d0c2021-02-14 23:22:55 +0100238/* helper to get listener status for stats */
239enum li_status get_li_status(struct listener *l)
240{
Willy Tarreau758c69d2023-01-12 18:59:37 +0100241 if (!l->bind_conf->maxconn || l->nbconn < l->bind_conf->maxconn) {
William Dauchy3679d0c2021-02-14 23:22:55 +0100242 if (l->state == LI_LIMITED)
243 return LI_STATUS_WAITING;
244 else
245 return LI_STATUS_OPEN;
246 }
247 return LI_STATUS_FULL;
248}
249
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200250/* adjust the listener's state and its proxy's listener counters if needed.
251 * It must be called under the listener's lock, but uses atomic ops to change
252 * the proxy's counters so that the proxy lock is not needed.
253 */
Willy Tarreaua37b2442020-09-24 07:23:45 +0200254void listener_set_state(struct listener *l, enum li_state st)
255{
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200256 struct proxy *px = l->bind_conf->frontend;
257
258 if (px) {
259 /* from state */
260 switch (l->state) {
261 case LI_NEW: /* first call */
Willy Tarreau4781b152021-04-06 13:53:36 +0200262 _HA_ATOMIC_INC(&px->li_all);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200263 break;
264 case LI_INIT:
265 case LI_ASSIGNED:
266 break;
267 case LI_PAUSED:
Willy Tarreau4781b152021-04-06 13:53:36 +0200268 _HA_ATOMIC_DEC(&px->li_paused);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200269 break;
270 case LI_LISTEN:
Willy Tarreau4781b152021-04-06 13:53:36 +0200271 _HA_ATOMIC_DEC(&px->li_bound);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200272 break;
273 case LI_READY:
274 case LI_FULL:
275 case LI_LIMITED:
Willy Tarreau4781b152021-04-06 13:53:36 +0200276 _HA_ATOMIC_DEC(&px->li_ready);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200277 break;
278 }
279
280 /* to state */
281 switch (st) {
282 case LI_NEW:
283 case LI_INIT:
284 case LI_ASSIGNED:
285 break;
286 case LI_PAUSED:
Willy Tarreau95a34602020-10-08 15:32:21 +0200287 BUG_ON(l->rx.fd == -1);
Willy Tarreau4781b152021-04-06 13:53:36 +0200288 _HA_ATOMIC_INC(&px->li_paused);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200289 break;
290 case LI_LISTEN:
Willy Tarreau95a34602020-10-08 15:32:21 +0200291 BUG_ON(l->rx.fd == -1);
Willy Tarreau4781b152021-04-06 13:53:36 +0200292 _HA_ATOMIC_INC(&px->li_bound);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200293 break;
294 case LI_READY:
295 case LI_FULL:
296 case LI_LIMITED:
Willy Tarreau95a34602020-10-08 15:32:21 +0200297 BUG_ON(l->rx.fd == -1);
Willy Tarreau4781b152021-04-06 13:53:36 +0200298 _HA_ATOMIC_INC(&px->li_ready);
Aurelien DARRAGON23705992023-02-14 08:51:14 +0100299 l->flags |= LI_F_FINALIZED;
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200300 break;
301 }
302 }
Willy Tarreaua37b2442020-09-24 07:23:45 +0200303 l->state = st;
304}
305
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100306/* This function adds the specified listener's file descriptor to the polling
307 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500308 * LI_FULL state depending on its number of connections. In daemon mode, we
Willy Tarreauae302532014-05-07 19:22:24 +0200309 * also support binding only the relevant processes to their respective
310 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100311 */
Willy Tarreau7834a3f2020-09-25 16:40:18 +0200312void enable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100313{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100314 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaud6afb532020-10-09 10:35:40 +0200315
316 /* If this listener is supposed to be only in the master, close it in
317 * the workers. Conversely, if it's supposed to be only in the workers
318 * close it in the master.
319 */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200320 if (!!master != !!(listener->rx.flags & RX_F_MWORKER))
Willy Tarreau75c98d12020-10-09 15:55:23 +0200321 do_unbind_listener(listener);
Willy Tarreaud6afb532020-10-09 10:35:40 +0200322
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100323 if (listener->state == LI_LISTEN) {
Willy Tarreau95a34602020-10-08 15:32:21 +0200324 BUG_ON(listener->rx.fd == -1);
William Lallemand095ba4c2017-06-01 17:38:50 +0200325 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau72faef32021-06-15 08:36:30 +0200326 (!!master != !!(listener->rx.flags & RX_F_MWORKER))) {
Willy Tarreauae302532014-05-07 19:22:24 +0200327 /* we don't want to enable this listener and don't
328 * want any fd event to reach it.
329 */
Willy Tarreau75c98d12020-10-09 15:55:23 +0200330 do_unbind_listener(listener);
Willy Tarreauae302532014-05-07 19:22:24 +0200331 }
Willy Tarreau758c69d2023-01-12 18:59:37 +0100332 else if (!listener->bind_conf->maxconn || listener->nbconn < listener->bind_conf->maxconn) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200333 listener->rx.proto->enable(listener);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200334 listener_set_state(listener, LI_READY);
Willy Tarreauae302532014-05-07 19:22:24 +0200335 }
336 else {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200337 listener_set_state(listener, LI_FULL);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100338 }
339 }
Willy Tarreaud6afb532020-10-09 10:35:40 +0200340
Willy Tarreau08b6f962022-02-01 16:23:00 +0100341 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100342}
343
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200344/*
Aurelien DARRAGON187396e2022-09-11 16:19:49 +0200345 * This function completely stops a listener.
346 * The proxy's listeners count is updated and the proxy is
347 * disabled and woken up after the last one is gone.
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100348 * It will need to operate under the proxy's lock, the protocol's lock and
349 * the listener's lock. The caller is responsible for indicating in lpx,
350 * lpr, lli whether the respective locks are already held (non-zero) or
351 * not (zero) so that the function picks the missing ones, in this order.
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200352 */
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100353void stop_listener(struct listener *l, int lpx, int lpr, int lli)
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200354{
355 struct proxy *px = l->bind_conf->frontend;
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200356
Willy Tarreau17146802023-01-12 19:58:42 +0100357 if (l->bind_conf->options & BC_O_NOSTOP) {
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200358 /* master-worker sockpairs are never closed but don't count as a
359 * job.
360 */
361 return;
362 }
363
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200364 if (!lpx && px)
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200365 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200366
367 if (!lpr)
368 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
369
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100370 if (!lli)
371 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200372
373 if (l->state > LI_INIT) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200374 do_unbind_listener(l);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200375
376 if (l->state >= LI_ASSIGNED)
377 __delete_listener(l);
378
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200379 if (px)
380 proxy_cond_disable(px);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200381 }
382
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100383 if (!lli)
384 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200385
386 if (!lpr)
387 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
388
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200389 if (!lpx && px)
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200390 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200391}
392
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100393/* This function adds the specified <listener> to the protocol <proto>. It
394 * does nothing if the protocol was already added. The listener's state is
395 * automatically updated from LI_INIT to LI_ASSIGNED. The number of listeners
396 * for the protocol is updated. This must be called with the proto lock held.
397 */
398void default_add_listener(struct protocol *proto, struct listener *listener)
399{
400 if (listener->state != LI_INIT)
401 return;
402 listener_set_state(listener, LI_ASSIGNED);
403 listener->rx.proto = proto;
Willy Tarreau2b718102021-04-21 07:32:39 +0200404 LIST_APPEND(&proto->receivers, &listener->rx.proto_list);
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100405 proto->nb_receivers++;
406}
407
Willy Tarreaue03204c2020-10-09 17:02:21 +0200408/* default function called to suspend a listener: it simply passes the call to
409 * the underlying receiver. This is find for most socket-based protocols. This
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100410 * must be called under the listener's lock. It will return < 0 in case of
411 * failure, 0 if the listener was totally stopped, or > 0 if correctly paused..
412 * If no receiver-level suspend is provided, the operation is assumed
413 * to succeed.
Willy Tarreaue03204c2020-10-09 17:02:21 +0200414 */
415int default_suspend_listener(struct listener *l)
416{
Willy Tarreaue03204c2020-10-09 17:02:21 +0200417 if (!l->rx.proto->rx_suspend)
418 return 1;
419
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100420 return l->rx.proto->rx_suspend(&l->rx);
Willy Tarreaue03204c2020-10-09 17:02:21 +0200421}
422
423
424/* Tries to resume a suspended listener, and returns non-zero on success or
425 * zero on failure. On certain errors, an alert or a warning might be displayed.
426 * It must be called with the listener's lock held. Depending on the listener's
427 * state and protocol, a listen() call might be used to resume operations, or a
428 * call to the receiver's resume() function might be used as well. This is
429 * suitable as a default function for TCP and UDP. This must be called with the
430 * listener's lock held.
431 */
432int default_resume_listener(struct listener *l)
433{
434 int ret = 1;
435
436 if (l->state == LI_ASSIGNED) {
437 char msg[100];
Aurelien DARRAGON046a75e2023-02-07 12:17:20 +0100438 char *errmsg;
Willy Tarreaue03204c2020-10-09 17:02:21 +0200439 int err;
440
Aurelien DARRAGON046a75e2023-02-07 12:17:20 +0100441 /* first, try to bind the receiver */
442 err = l->rx.proto->fam->bind(&l->rx, &errmsg);
443 if (err != ERR_NONE) {
444 if (err & ERR_WARN)
445 ha_warning("Resuming listener: %s\n", errmsg);
446 else if (err & ERR_ALERT)
447 ha_alert("Resuming listener: %s\n", errmsg);
448 ha_free(&errmsg);
449 if (err & (ERR_FATAL | ERR_ABORT)) {
450 ret = 0;
451 goto end;
452 }
453 }
454
455 /* then, try to listen:
456 * for now there's still always a listening function
457 * (same check performed in protocol_bind_all()
458 */
459 BUG_ON(!l->rx.proto->listen);
Willy Tarreaue03204c2020-10-09 17:02:21 +0200460 err = l->rx.proto->listen(l, msg, sizeof(msg));
461 if (err & ERR_ALERT)
462 ha_alert("Resuming listener: %s\n", msg);
463 else if (err & ERR_WARN)
464 ha_warning("Resuming listener: %s\n", msg);
465
466 if (err & (ERR_FATAL | ERR_ABORT)) {
467 ret = 0;
468 goto end;
469 }
470 }
471
472 if (l->state < LI_PAUSED) {
473 ret = 0;
474 goto end;
475 }
476
477 if (l->state == LI_PAUSED && l->rx.proto->rx_resume &&
478 l->rx.proto->rx_resume(&l->rx) <= 0)
479 ret = 0;
480 end:
481 return ret;
482}
483
484
Willy Tarreaube58c382011-07-24 18:28:10 +0200485/* This function tries to temporarily disable a listener, depending on the OS
486 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
487 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
488 * closes upon SHUT_WR and refuses to rebind. So a common validation path
489 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
490 * is disabled. It normally returns non-zero, unless an error is reported.
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100491 * suspend() may totally stop a listener if it doesn't support the PAUSED
492 * state, in which case state will be set to ASSIGNED.
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100493 * It will need to operate under the proxy's lock and the listener's lock.
494 * The caller is responsible for indicating in lpx, lli whether the respective
495 * locks are already held (non-zero) or not (zero) so that the function pick
496 * the missing ones, in this order.
Willy Tarreaube58c382011-07-24 18:28:10 +0200497 */
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100498int suspend_listener(struct listener *l, int lpx, int lli)
Willy Tarreaube58c382011-07-24 18:28:10 +0200499{
Willy Tarreau58651b42020-09-24 16:03:29 +0200500 struct proxy *px = l->bind_conf->frontend;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200501 int ret = 1;
502
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200503 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200504 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
505
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100506 if (!lli)
507 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200508
Aurelien DARRAGON23705992023-02-14 08:51:14 +0100509 if (!(l->flags & LI_F_FINALIZED) || l->state <= LI_PAUSED)
Willy Tarreau9b3a9322020-09-24 14:46:34 +0200510 goto end;
511
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100512 if (l->rx.proto->suspend) {
Willy Tarreaue03204c2020-10-09 17:02:21 +0200513 ret = l->rx.proto->suspend(l);
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100514 /* if the suspend() fails, we don't want to change the
515 * current listener state
516 */
517 if (ret < 0)
518 goto end;
519 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200520
Willy Tarreau2b718102021-04-21 07:32:39 +0200521 MT_LIST_DELETE(&l->wait_queue);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200522
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100523 /* ret == 0 means that the suspend() has been turned into
524 * an unbind(), meaning the listener is now stopped (ie: ABNS), we need
525 * to report this state change properly
526 */
527 listener_set_state(l, ((ret) ? LI_PAUSED : LI_ASSIGNED));
528
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100529 if (px && !(l->flags & LI_F_SUSPENDED))
530 px->li_suspended++;
531 l->flags |= LI_F_SUSPENDED;
532
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100533 /* at this point, everything is under control, no error should be
534 * returned to calling function
535 */
536 ret = 1;
Willy Tarreau58651b42020-09-24 16:03:29 +0200537
Aurelien DARRAGONca8a4b22023-02-07 12:36:27 +0100538 if (px && !(px->flags & PR_FL_PAUSED) && !px->li_ready) {
Aurelien DARRAGONd46f4372022-09-09 15:51:37 +0200539 /* PROXY_LOCK is required */
540 proxy_cond_pause(px);
Willy Tarreau58651b42020-09-24 16:03:29 +0200541 ha_warning("Paused %s %s.\n", proxy_cap_str(px->cap), px->id);
542 send_log(px, LOG_WARNING, "Paused %s %s.\n", proxy_cap_str(px->cap), px->id);
543 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200544 end:
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100545 if (!lli)
546 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200547
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200548 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200549 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
550
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200551 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200552}
553
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200554/* This function tries to resume a temporarily disabled listener. Paused, full,
555 * limited and disabled listeners are handled, which means that this function
556 * may replace enable_listener(). The resulting state will either be LI_READY
557 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200558 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200559 * foreground mode, and are ignored. If the listener was only in the assigned
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100560 * state, it's totally rebound. This can happen if a suspend() has completely
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200561 * stopped it. If the resume fails, 0 is returned and an error might be
562 * displayed.
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100563 * It will need to operate under the proxy's lock and the listener's lock.
564 * The caller is responsible for indicating in lpx, lli whether the respective
565 * locks are already held (non-zero) or not (zero) so that the function pick
566 * the missing ones, in this order.
Willy Tarreaube58c382011-07-24 18:28:10 +0200567 */
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100568int resume_listener(struct listener *l, int lpx, int lli)
Willy Tarreaube58c382011-07-24 18:28:10 +0200569{
Willy Tarreau58651b42020-09-24 16:03:29 +0200570 struct proxy *px = l->bind_conf->frontend;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200571 int ret = 1;
572
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200573 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200574 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
575
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100576 if (!lli)
577 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200578
Willy Tarreauf2cb1692019-07-11 10:08:31 +0200579 /* check that another thread didn't to the job in parallel (e.g. at the
580 * end of listen_accept() while we'd come from dequeue_all_listeners().
581 */
Willy Tarreau2b718102021-04-21 07:32:39 +0200582 if (MT_LIST_INLIST(&l->wait_queue))
Willy Tarreauf2cb1692019-07-11 10:08:31 +0200583 goto end;
584
Aurelien DARRAGON23705992023-02-14 08:51:14 +0100585 if (!(l->flags & LI_F_FINALIZED) || l->state == LI_READY)
Willy Tarreau5d7f9ce2020-09-24 18:54:11 +0200586 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200587
Aurelien DARRAGON3bb2a382023-02-07 13:26:14 +0100588 if (l->rx.proto->resume) {
Willy Tarreaue03204c2020-10-09 17:02:21 +0200589 ret = l->rx.proto->resume(l);
Aurelien DARRAGON3bb2a382023-02-07 13:26:14 +0100590 if (!ret)
591 goto end; /* failure to resume */
592 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200593
Willy Tarreau758c69d2023-01-12 18:59:37 +0100594 if (l->bind_conf->maxconn && l->nbconn >= l->bind_conf->maxconn) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200595 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200596 listener_set_state(l, LI_FULL);
Willy Tarreau58651b42020-09-24 16:03:29 +0200597 goto done;
Willy Tarreaube58c382011-07-24 18:28:10 +0200598 }
599
Willy Tarreau4b51f422020-09-25 20:32:28 +0200600 l->rx.proto->enable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200601 listener_set_state(l, LI_READY);
Willy Tarreau58651b42020-09-24 16:03:29 +0200602
603 done:
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100604 if (px && (l->flags & LI_F_SUSPENDED))
605 px->li_suspended--;
606 l->flags &= ~LI_F_SUSPENDED;
607
Aurelien DARRAGONca8a4b22023-02-07 12:36:27 +0100608 if (px && (px->flags & PR_FL_PAUSED) && !px->li_suspended) {
Aurelien DARRAGONd46f4372022-09-09 15:51:37 +0200609 /* PROXY_LOCK is required */
610 proxy_cond_resume(px);
Willy Tarreau58651b42020-09-24 16:03:29 +0200611 ha_warning("Resumed %s %s.\n", proxy_cap_str(px->cap), px->id);
612 send_log(px, LOG_WARNING, "Resumed %s %s.\n", proxy_cap_str(px->cap), px->id);
613 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200614 end:
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100615 if (!lli)
616 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200617
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200618 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200619 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
620
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200621 return ret;
622}
623
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100624/* Same as resume_listener(), but will only work to resume from
625 * LI_FULL or LI_LIMITED states because we try to relax listeners that
626 * were temporarily restricted and not to resume inactive listeners that
627 * may have been paused or completely stopped in the meantime.
628 * Returns positive value for success and 0 for failure.
629 * It will need to operate under the proxy's lock and the listener's lock.
630 * The caller is responsible for indicating in lpx, lli whether the respective
631 * locks are already held (non-zero) or not (zero) so that the function pick
632 * the missing ones, in this order.
633 */
634int relax_listener(struct listener *l, int lpx, int lli)
635{
Christopher Faulet6844af62023-07-20 14:53:50 +0200636 struct proxy *px = l->bind_conf->frontend;
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100637 int ret = 1;
638
Christopher Faulet6844af62023-07-20 14:53:50 +0200639 if (!lpx && px)
640 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
641
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100642 if (!lli)
643 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
644
645 if (l->state != LI_FULL && l->state != LI_LIMITED)
646 goto end; /* listener may be suspended or even stopped */
Christopher Faulet6844af62023-07-20 14:53:50 +0200647 ret = resume_listener(l, 1, 1);
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100648
649 end:
650 if (!lli)
651 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet6844af62023-07-20 14:53:50 +0200652
653 if (!lpx && px)
654 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
655
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100656 return ret;
657}
658
Willy Tarreau87b09662015-04-03 00:22:06 +0200659/* Marks a ready listener as full so that the stream code tries to re-enable
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +0100660 * it upon next close() using relax_listener().
Willy Tarreau62793712011-07-24 19:23:38 +0200661 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200662static void listener_full(struct listener *l)
Willy Tarreau62793712011-07-24 19:23:38 +0200663{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100664 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200665 if (l->state >= LI_READY) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200666 MT_LIST_DELETE(&l->wait_queue);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100667 if (l->state != LI_FULL) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200668 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200669 listener_set_state(l, LI_FULL);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100670 }
Willy Tarreau62793712011-07-24 19:23:38 +0200671 }
Willy Tarreau08b6f962022-02-01 16:23:00 +0100672 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200673}
674
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200675/* Marks a ready listener as limited so that we only try to re-enable it when
676 * resources are free again. It will be queued into the specified queue.
677 */
Olivier Houchard859dc802019-08-08 15:47:21 +0200678static void limit_listener(struct listener *l, struct mt_list *list)
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200679{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100680 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200681 if (l->state == LI_READY) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200682 MT_LIST_TRY_APPEND(list, &l->wait_queue);
Willy Tarreau4b51f422020-09-25 20:32:28 +0200683 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200684 listener_set_state(l, LI_LIMITED);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200685 }
Willy Tarreau08b6f962022-02-01 16:23:00 +0100686 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200687}
688
Willy Tarreau241797a2019-12-10 14:10:52 +0100689/* Dequeues all listeners waiting for a resource the global wait queue */
690void dequeue_all_listeners()
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200691{
Willy Tarreau01abd022019-02-28 10:27:18 +0100692 struct listener *listener;
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200693
Willy Tarreau241797a2019-12-10 14:10:52 +0100694 while ((listener = MT_LIST_POP(&global_listener_queue, struct listener *, wait_queue))) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200695 /* This cannot fail because the listeners are by definition in
Willy Tarreau01abd022019-02-28 10:27:18 +0100696 * the LI_LIMITED state.
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200697 */
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +0100698 relax_listener(listener, 0, 0);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200699 }
700}
701
Willy Tarreau241797a2019-12-10 14:10:52 +0100702/* Dequeues all listeners waiting for a resource in proxy <px>'s queue */
703void dequeue_proxy_listeners(struct proxy *px)
704{
705 struct listener *listener;
706
707 while ((listener = MT_LIST_POP(&px->listener_queue, struct listener *, wait_queue))) {
708 /* This cannot fail because the listeners are by definition in
709 * the LI_LIMITED state.
710 */
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +0100711 relax_listener(listener, 0, 0);
Willy Tarreau241797a2019-12-10 14:10:52 +0100712 }
713}
714
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200715
716/* default function used to unbind a listener. This is for use by standard
717 * protocols working on top of accepted sockets. The receiver's rx_unbind()
718 * will automatically be used after the listener is disabled if the socket is
719 * still bound. This must be used under the listener's lock.
Christopher Faulet510c0d62018-03-16 10:04:47 +0100720 */
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200721void default_unbind_listener(struct listener *listener)
Willy Tarreaub648d632007-10-28 22:13:50 +0100722{
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200723 if (listener->state <= LI_ASSIGNED)
724 goto out_close;
725
726 if (listener->rx.fd == -1) {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200727 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200728 goto out_close;
729 }
730
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200731 if (listener->state >= LI_READY) {
732 listener->rx.proto->disable(listener);
733 if (listener->rx.flags & RX_F_BOUND)
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200734 listener_set_state(listener, LI_LISTEN);
Willy Tarreaub6607bf2020-09-23 16:24:23 +0200735 }
736
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200737 out_close:
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200738 if (listener->rx.flags & RX_F_BOUND)
739 listener->rx.proto->rx_unbind(&listener->rx);
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200740}
741
742/* This function closes the listening socket for the specified listener,
743 * provided that it's already in a listening state. The protocol's unbind()
744 * is called to put the listener into LI_ASSIGNED or LI_LISTEN and handle
745 * the unbinding tasks. The listener enters then the LI_ASSIGNED state if
746 * the receiver is unbound. Must be called with the lock held.
747 */
748void do_unbind_listener(struct listener *listener)
749{
Willy Tarreau2b718102021-04-21 07:32:39 +0200750 MT_LIST_DELETE(&listener->wait_queue);
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200751
752 if (listener->rx.proto->unbind)
753 listener->rx.proto->unbind(listener);
Willy Tarreau374e9af2020-10-09 15:47:17 +0200754
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200755 /* we may have to downgrade the listener if the rx was closed */
756 if (!(listener->rx.flags & RX_F_BOUND) && listener->state > LI_ASSIGNED)
Willy Tarreau374e9af2020-10-09 15:47:17 +0200757 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100758}
759
Olivier Houchard1fc05162017-04-06 01:05:05 +0200760/* This function closes the listening socket for the specified listener,
761 * provided that it's already in a listening state. The listener enters the
Willy Tarreau75c98d12020-10-09 15:55:23 +0200762 * LI_ASSIGNED state, except if the FD is not closed, in which case it may
763 * remain in LI_LISTEN. This function is intended to be used as a generic
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100764 * function for standard protocols.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200765 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100766void unbind_listener(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200767{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100768 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau75c98d12020-10-09 15:55:23 +0200769 do_unbind_listener(listener);
Willy Tarreau08b6f962022-02-01 16:23:00 +0100770 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200771}
772
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200773/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
774 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200775 * allocation). The address family is taken from ss->ss_family, and the protocol
Willy Tarreaud2fb99f2020-10-15 21:22:29 +0200776 * passed in <proto> must be usable on this family. The protocol's default iocb
777 * is automatically preset as the receivers' iocb. The number of jobs and
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200778 * listeners is automatically increased by the number of listeners created. It
779 * returns non-zero on success, zero on error with the error message set in <err>.
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200780 */
781int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200782 int portl, int porth, int fd, struct protocol *proto, char **err)
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200783{
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200784 struct listener *l;
785 int port;
786
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200787 for (port = portl; port <= porth; port++) {
788 l = calloc(1, sizeof(*l));
789 if (!l) {
790 memprintf(err, "out of memory");
791 return 0;
792 }
793 l->obj_type = OBJ_TYPE_LISTENER;
Willy Tarreau2b718102021-04-21 07:32:39 +0200794 LIST_APPEND(&bc->frontend->conf.listeners, &l->by_fe);
795 LIST_APPEND(&bc->listeners, &l->by_bind);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200796 l->bind_conf = bc;
Willy Tarreau0fce6bc2020-09-03 07:46:06 +0200797 l->rx.settings = &bc->settings;
Willy Tarreaueef45422020-09-03 10:05:03 +0200798 l->rx.owner = l;
Willy Tarreaud2fb99f2020-10-15 21:22:29 +0200799 l->rx.iocb = proto->default_iocb;
Willy Tarreau38ba6472020-08-27 08:16:52 +0200800 l->rx.fd = fd;
Willy Tarreau07400c52020-12-04 14:49:11 +0100801
Willy Tarreau37159062020-08-27 07:48:42 +0200802 memcpy(&l->rx.addr, ss, sizeof(*ss));
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100803 if (proto->fam->set_port)
804 proto->fam->set_port(&l->rx.addr, port);
Willy Tarreau07400c52020-12-04 14:49:11 +0100805
Olivier Houchard859dc802019-08-08 15:47:21 +0200806 MT_LIST_INIT(&l->wait_queue);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200807 listener_set_state(l, LI_INIT);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200808
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100809 proto->add(proto, l);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200810
Willy Tarreau909c23b2020-09-15 13:50:58 +0200811 if (fd != -1)
Willy Tarreau43046fa2020-09-01 15:41:59 +0200812 l->rx.flags |= RX_F_INHERITED;
William Lallemand75ea0a02017-11-15 19:02:58 +0100813
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100814 l->extra_counters = NULL;
815
Willy Tarreau08b6f962022-02-01 16:23:00 +0100816 HA_RWLOCK_INIT(&l->lock);
Willy Tarreau4781b152021-04-06 13:53:36 +0200817 _HA_ATOMIC_INC(&jobs);
818 _HA_ATOMIC_INC(&listeners);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200819 }
820 return 1;
821}
822
Willy Tarreauaae18102023-03-01 18:25:58 +0100823/* Optionally allocates a new shard info (if si == NULL) for receiver rx and
824 * assigns it to it, or attaches to an existing one. If the rx already had a
825 * shard_info, it is simply returned. It is illegal to call this function with
826 * an rx that's part of a group that is already attached. Attaching means the
827 * shard_info's thread count and group count are updated so the rx's group is
828 * added to the shard_info's group mask. The rx are added to the members in the
829 * attachment order, though it must not matter. It is meant for boot time setup
830 * and is not thread safe. NULL is returned on allocation failure.
831 */
832struct shard_info *shard_info_attach(struct receiver *rx, struct shard_info *si)
833{
834 if (rx->shard_info)
835 return rx->shard_info;
836
837 if (!si) {
838 si = calloc(1, sizeof(*si));
839 if (!si)
840 return NULL;
841
842 si->ref = rx;
843 }
844
845 rx->shard_info = si;
846 BUG_ON (si->tgroup_mask & 1UL << (rx->bind_tgroup - 1));
847 si->tgroup_mask |= 1UL << (rx->bind_tgroup - 1);
848 si->nbgroups = my_popcountl(si->tgroup_mask);
849 si->nbthreads += my_popcountl(rx->bind_thread);
850 si->members[si->nbgroups - 1] = rx;
851 return si;
852}
853
854/* Detaches the rx from an optional shard_info it may be attached to. If so,
855 * the thread counts, group masks and refcounts are updated. The members list
856 * remains contiguous by replacing the current entry with the last one. The
857 * reference continues to point to the first receiver. If the group count
858 * reaches zero, the shard_info is automatically released.
859 */
860void shard_info_detach(struct receiver *rx)
861{
862 struct shard_info *si = rx->shard_info;
863 uint gr;
864
865 if (!si)
866 return;
867
868 rx->shard_info = NULL;
869
870 /* find the member slot this rx was attached to */
871 for (gr = 0; gr < MAX_TGROUPS && si->members[gr] != rx; gr++)
872 ;
873
874 BUG_ON(gr == MAX_TGROUPS);
875
876 si->nbthreads -= my_popcountl(rx->bind_thread);
877 si->tgroup_mask &= ~(1UL << (rx->bind_tgroup - 1));
878 si->nbgroups = my_popcountl(si->tgroup_mask);
879
880 /* replace the member by the last one. If we removed the reference, we
881 * have to switch to another one. It's always the first entry so we can
882 * simply enforce it upon every removal.
883 */
884 si->members[gr] = si->members[si->nbgroups];
885 si->members[si->nbgroups] = NULL;
886 si->ref = si->members[0];
887
888 if (!si->nbgroups)
889 free(si);
890}
891
Willy Tarreau59a877d2021-10-12 09:36:10 +0200892/* clones listener <src> and returns the new one. All dynamically allocated
893 * fields are reallocated (name for now). The new listener is inserted before
894 * the original one in the bind_conf and frontend lists. This allows it to be
895 * duplicated while iterating over the current list. The original listener must
896 * only be in the INIT or ASSIGNED states, and the new listener will only be
897 * placed into the INIT state. The counters are always set to NULL. Maxsock is
Willy Tarreauaae18102023-03-01 18:25:58 +0100898 * updated. Returns NULL on allocation error. The shard_info is never taken so
899 * that the caller can decide what to do with it depending on how it intends to
900 * clone the listener.
Willy Tarreau59a877d2021-10-12 09:36:10 +0200901 */
902struct listener *clone_listener(struct listener *src)
903{
904 struct listener *l;
905
906 l = calloc(1, sizeof(*l));
907 if (!l)
908 goto oom1;
909 memcpy(l, src, sizeof(*l));
910
911 if (l->name) {
912 l->name = strdup(l->name);
913 if (!l->name)
914 goto oom2;
915 }
916
917 l->rx.owner = l;
Willy Tarreauaae18102023-03-01 18:25:58 +0100918 l->rx.shard_info = NULL;
Willy Tarreau59a877d2021-10-12 09:36:10 +0200919 l->state = LI_INIT;
920 l->counters = NULL;
921 l->extra_counters = NULL;
922
923 LIST_APPEND(&src->by_fe, &l->by_fe);
924 LIST_APPEND(&src->by_bind, &l->by_bind);
925
926 MT_LIST_INIT(&l->wait_queue);
927
928 l->rx.proto->add(l->rx.proto, l);
929
Willy Tarreau08b6f962022-02-01 16:23:00 +0100930 HA_RWLOCK_INIT(&l->lock);
Willy Tarreau59a877d2021-10-12 09:36:10 +0200931 _HA_ATOMIC_INC(&jobs);
932 _HA_ATOMIC_INC(&listeners);
933 global.maxsock++;
934 return l;
935
Willy Tarreau59a877d2021-10-12 09:36:10 +0200936 oom2:
937 free(l);
938 oom1:
Willy Tarreaua1462892021-10-16 14:45:29 +0200939 return NULL;
Willy Tarreau59a877d2021-10-12 09:36:10 +0200940}
941
Willy Tarreau1a64d162007-10-28 22:26:05 +0100942/* Delete a listener from its protocol's list of listeners. The listener's
943 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
Willy Tarreau2cc5bae2017-09-15 08:18:11 +0200944 * number of listeners is updated, as well as the global number of listeners
945 * and jobs. Note that the listener must have previously been unbound. This
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200946 * is a low-level function expected to be called with the proto_lock and the
947 * listener's lock held.
Willy Tarreau1a64d162007-10-28 22:26:05 +0100948 */
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200949void __delete_listener(struct listener *listener)
Willy Tarreau1a64d162007-10-28 22:26:05 +0100950{
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100951 if (listener->state == LI_ASSIGNED) {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200952 listener_set_state(listener, LI_INIT);
Willy Tarreau2b718102021-04-21 07:32:39 +0200953 LIST_DELETE(&listener->rx.proto_list);
Willy Tarreauaae18102023-03-01 18:25:58 +0100954 shard_info_detach(&listener->rx);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200955 listener->rx.proto->nb_receivers--;
Willy Tarreau4781b152021-04-06 13:53:36 +0200956 _HA_ATOMIC_DEC(&jobs);
957 _HA_ATOMIC_DEC(&listeners);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100958 }
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200959}
960
961/* Delete a listener from its protocol's list of listeners (please check
962 * __delete_listener() above). The proto_lock and the listener's lock will
963 * be grabbed in this order.
964 */
965void delete_listener(struct listener *listener)
966{
967 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau08b6f962022-02-01 16:23:00 +0100968 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200969 __delete_listener(listener);
Willy Tarreau08b6f962022-02-01 16:23:00 +0100970 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau6ee9f8d2019-08-26 10:55:52 +0200971 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100972}
973
Willy Tarreaue2711c72019-02-27 15:39:41 +0100974/* Returns a suitable value for a listener's backlog. It uses the listener's,
975 * otherwise the frontend's backlog, otherwise the listener's maxconn,
976 * otherwise the frontend's maxconn, otherwise 1024.
977 */
978int listener_backlog(const struct listener *l)
979{
Willy Tarreau1920f892023-01-12 18:55:13 +0100980 if (l->bind_conf->backlog)
981 return l->bind_conf->backlog;
Willy Tarreaue2711c72019-02-27 15:39:41 +0100982
983 if (l->bind_conf->frontend->backlog)
984 return l->bind_conf->frontend->backlog;
985
Willy Tarreau758c69d2023-01-12 18:59:37 +0100986 if (l->bind_conf->maxconn)
987 return l->bind_conf->maxconn;
Willy Tarreaue2711c72019-02-27 15:39:41 +0100988
989 if (l->bind_conf->frontend->maxconn)
990 return l->bind_conf->frontend->maxconn;
991
992 return 1024;
993}
994
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200995/* This function is called on a read event from a listening socket, corresponding
996 * to an accept. It tries to accept as many connections as possible, and for each
997 * calls the listener's accept handler (generally the frontend's accept handler).
998 */
Willy Tarreaua74cb382020-10-15 21:29:49 +0200999void listener_accept(struct listener *l)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001000{
Willy Tarreau83efc322020-10-14 17:37:17 +02001001 struct connection *cli_conn;
Olivier Houchardd16a9df2019-02-25 16:18:16 +01001002 struct proxy *p;
Christopher Faulet102854c2019-04-30 12:17:13 +02001003 unsigned int max_accept;
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001004 int next_conn = 0;
Willy Tarreau82c97892019-02-27 19:32:32 +01001005 int next_feconn = 0;
1006 int next_actconn = 0;
Willy Tarreaubb660302014-05-07 19:47:02 +02001007 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001008 int ret;
1009
Olivier Houchardd16a9df2019-02-25 16:18:16 +01001010 p = l->bind_conf->frontend;
Christopher Faulet102854c2019-04-30 12:17:13 +02001011
Willy Tarreau882f2482023-01-12 18:52:23 +01001012 /* if l->bind_conf->maxaccept is -1, then max_accept is UINT_MAX. It is
1013 * not really illimited, but it is probably enough.
Christopher Faulet102854c2019-04-30 12:17:13 +02001014 */
Willy Tarreau882f2482023-01-12 18:52:23 +01001015 max_accept = l->bind_conf->maxaccept ? l->bind_conf->maxaccept : 1;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001016
Willy Tarreau17146802023-01-12 19:58:42 +01001017 if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.sps_lim) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001018 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +02001019
1020 if (unlikely(!max)) {
1021 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +02001022 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +01001023 goto limit_global;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001024 }
1025
1026 if (max_accept > max)
1027 max_accept = max;
1028 }
1029
Willy Tarreau17146802023-01-12 19:58:42 +01001030 if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001031 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
1032
1033 if (unlikely(!max)) {
1034 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +02001035 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +01001036 goto limit_global;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001037 }
1038
1039 if (max_accept > max)
1040 max_accept = max;
1041 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001042#ifdef USE_OPENSSL
Willy Tarreau17146802023-01-12 19:58:42 +01001043 if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.ssl_lim &&
Willy Tarreau11ba4042022-05-20 15:56:32 +02001044 l->bind_conf && l->bind_conf->options & BC_O_USE_SSL) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001045 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001046
Willy Tarreaue43d5322013-10-07 20:01:52 +02001047 if (unlikely(!max)) {
1048 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +02001049 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +01001050 goto limit_global;
Willy Tarreaue43d5322013-10-07 20:01:52 +02001051 }
1052
1053 if (max_accept > max)
1054 max_accept = max;
1055 }
1056#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001057 if (p && p->fe_sps_lim) {
1058 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
1059
1060 if (unlikely(!max)) {
1061 /* frontend accept rate limit was reached */
Willy Tarreau0591bf72019-12-10 12:01:21 +01001062 expire = tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0));
1063 goto limit_proxy;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001064 }
1065
1066 if (max_accept > max)
1067 max_accept = max;
1068 }
1069
1070 /* Note: if we fail to allocate a connection because of configured
1071 * limits, we'll schedule a new attempt worst 1 second later in the
1072 * worst case. If we fail due to system limits or temporary resource
1073 * shortage, we try again 100ms later in the worst case.
1074 */
Willy Tarreau02757d02021-01-28 18:07:24 +01001075 for (; max_accept; next_conn = next_feconn = next_actconn = 0, max_accept--) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001076 unsigned int count;
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001077 int status;
Willy Tarreau0aa5a5b2020-10-16 17:43:04 +02001078 __decl_thread(unsigned long mask);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001079
Willy Tarreau82c97892019-02-27 19:32:32 +01001080 /* pre-increase the number of connections without going too far.
1081 * We process the listener, then the proxy, then the process.
1082 * We know which ones to unroll based on the next_xxx value.
1083 */
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001084 do {
1085 count = l->nbconn;
Willy Tarreau758c69d2023-01-12 18:59:37 +01001086 if (unlikely(l->bind_conf->maxconn && count >= l->bind_conf->maxconn)) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001087 /* the listener was marked full or another
1088 * thread is going to do it.
1089 */
1090 next_conn = 0;
Willy Tarreau93604ed2019-11-15 10:20:07 +01001091 listener_full(l);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001092 goto end;
1093 }
1094 next_conn = count + 1;
David Carlier56716622019-03-27 16:08:42 +00001095 } while (!_HA_ATOMIC_CAS(&l->nbconn, (int *)(&count), next_conn));
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001096
Willy Tarreau82c97892019-02-27 19:32:32 +01001097 if (p) {
1098 do {
1099 count = p->feconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +01001100 if (unlikely(count >= p->maxconn)) {
Willy Tarreau82c97892019-02-27 19:32:32 +01001101 /* the frontend was marked full or another
1102 * thread is going to do it.
1103 */
1104 next_feconn = 0;
Willy Tarreau0591bf72019-12-10 12:01:21 +01001105 expire = TICK_ETERNITY;
1106 goto limit_proxy;
Willy Tarreau82c97892019-02-27 19:32:32 +01001107 }
1108 next_feconn = count + 1;
Olivier Houchard64213e92019-03-08 18:52:57 +01001109 } while (!_HA_ATOMIC_CAS(&p->feconn, &count, next_feconn));
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001110 }
1111
Willy Tarreau17146802023-01-12 19:58:42 +01001112 if (!(l->bind_conf->options & BC_O_UNLIMITED)) {
Willy Tarreau82c97892019-02-27 19:32:32 +01001113 do {
1114 count = actconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +01001115 if (unlikely(count >= global.maxconn)) {
Willy Tarreau82c97892019-02-27 19:32:32 +01001116 /* the process was marked full or another
1117 * thread is going to do it.
1118 */
1119 next_actconn = 0;
Willy Tarreau0591bf72019-12-10 12:01:21 +01001120 expire = tick_add(now_ms, 1000); /* try again in 1 second */
1121 goto limit_global;
Willy Tarreau82c97892019-02-27 19:32:32 +01001122 }
1123 next_actconn = count + 1;
David Carlier56716622019-03-27 16:08:42 +00001124 } while (!_HA_ATOMIC_CAS(&actconn, (int *)(&count), next_actconn));
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001125 }
1126
Willy Tarreaufed93d32022-02-01 16:37:00 +01001127 /* be careful below, the listener might be shutting down in
1128 * another thread on error and we must not dereference its
1129 * FD without a bit of protection.
1130 */
1131 cli_conn = NULL;
1132 status = CO_AC_PERMERR;
1133
1134 HA_RWLOCK_RDLOCK(LISTENER_LOCK, &l->lock);
1135 if (l->rx.flags & RX_F_BOUND)
1136 cli_conn = l->rx.proto->accept_conn(l, &status);
1137 HA_RWLOCK_RDUNLOCK(LISTENER_LOCK, &l->lock);
1138
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001139 if (!cli_conn) {
1140 switch (status) {
1141 case CO_AC_DONE:
1142 goto end;
Willy Tarreau818dca52014-01-31 19:40:19 +01001143
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001144 case CO_AC_RETRY: /* likely a signal */
Willy Tarreau4781b152021-04-06 13:53:36 +02001145 _HA_ATOMIC_DEC(&l->nbconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001146 if (p)
Willy Tarreau4781b152021-04-06 13:53:36 +02001147 _HA_ATOMIC_DEC(&p->feconn);
Willy Tarreau17146802023-01-12 19:58:42 +01001148 if (!(l->bind_conf->options & BC_O_UNLIMITED))
Willy Tarreau4781b152021-04-06 13:53:36 +02001149 _HA_ATOMIC_DEC(&actconn);
Willy Tarreaua593ec52014-01-20 21:21:30 +01001150 continue;
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001151
1152 case CO_AC_YIELD:
Willy Tarreau92079932019-12-10 09:30:05 +01001153 max_accept = 0;
1154 goto end;
William Lallemandd9138002018-11-27 12:02:39 +01001155
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001156 default:
1157 goto transient_error;
Willy Tarreau83efc322020-10-14 17:37:17 +02001158 }
1159 }
1160
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001161 /* The connection was accepted, it must be counted as such */
1162 if (l->counters)
1163 HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, next_conn);
1164
Willy Tarreaud8679342022-05-09 20:41:54 +02001165 if (p) {
Willy Tarreau82c97892019-02-27 19:32:32 +01001166 HA_ATOMIC_UPDATE_MAX(&p->fe_counters.conn_max, next_feconn);
Willy Tarreaud8679342022-05-09 20:41:54 +02001167 proxy_inc_fe_conn_ctr(l, p);
1168 }
Willy Tarreau82c97892019-02-27 19:32:32 +01001169
Willy Tarreau17146802023-01-12 19:58:42 +01001170 if (!(l->bind_conf->options & BC_O_UNLIMITED)) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001171 count = update_freq_ctr(&global.conn_per_sec, 1);
1172 HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001173 }
1174
Willy Tarreau4781b152021-04-06 13:53:36 +02001175 _HA_ATOMIC_INC(&activity[tid].accepted);
Willy Tarreau64a9c052019-04-12 15:27:17 +02001176
Willy Tarreau96151022023-05-11 13:51:31 +02001177 /* count the number of times an accepted connection resulted in
1178 * maxconn being reached.
1179 */
1180 if (unlikely(_HA_ATOMIC_LOAD(&actconn) + 1 >= global.maxconn))
1181 _HA_ATOMIC_INC(&maxconn_reached);
1182
Willy Tarreau30836152023-01-12 19:10:17 +01001183 /* past this point, l->bind_conf->accept() will automatically decrement
Willy Tarreau82c97892019-02-27 19:32:32 +01001184 * l->nbconn, feconn and actconn once done. Setting next_*conn=0
1185 * allows the error path not to rollback on nbconn. It's more
1186 * convenient than duplicating all exit labels.
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001187 */
1188 next_conn = 0;
Willy Tarreau82c97892019-02-27 19:32:32 +01001189 next_feconn = 0;
1190 next_actconn = 0;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001191
Willy Tarreau83efc322020-10-14 17:37:17 +02001192
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001193#if defined(USE_THREAD)
Willy Tarreau9d360602023-03-27 10:38:51 +02001194 if (!(global.tune.options & GTUNE_LISTENER_MQ_ANY) || stopping)
1195 goto local_accept;
1196
1197 /* we want to perform thread rebalancing if the listener is
1198 * bound to more than one thread or if it's part of a shard
1199 * with more than one listener.
1200 */
Willy Tarreaub2f38c12023-01-19 19:14:18 +01001201 mask = l->rx.bind_thread & _HA_ATOMIC_LOAD(&tg->threads_enabled);
Willy Tarreau9d360602023-03-27 10:38:51 +02001202 if (l->rx.shard_info || atleast2(mask)) {
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001203 struct accept_queue_ring *ring;
Willy Tarreau9d360602023-03-27 10:38:51 +02001204 struct listener *new_li;
Willy Tarreauff185042023-04-20 16:52:21 +02001205 uint r1, r2, t, t1, t2;
1206 ulong n0, n1;
Willy Tarreau9d360602023-03-27 10:38:51 +02001207 const struct tgroup_info *g1, *g2;
1208 ulong m1, m2;
Willy Tarreauff185042023-04-20 16:52:21 +02001209 ulong *thr_idx_ptr;
Willy Tarreaufc630bd2019-03-04 19:57:34 +01001210
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001211 /* The principle is that we have two running indexes,
1212 * each visiting in turn all threads bound to this
Willy Tarreau9d360602023-03-27 10:38:51 +02001213 * listener's shard. The connection will be assigned to
1214 * the one with the least connections, and the other
1215 * one will be updated. This provides a good fairness
1216 * on short connections (round robin) and on long ones
1217 * (conn count), without ever missing any idle thread.
1218 * Each thread number is encoded as a combination of
1219 * times the receiver number and its local thread
1220 * number from 0 to MAX_THREADS_PER_GROUP - 1. The two
Willy Tarreauff185042023-04-20 16:52:21 +02001221 * indexes are stored as 10/12 bit numbers in the thr_idx
1222 * array, since there are up to LONGBITS threads and
1223 * groups that can be represented. They are represented
1224 * like this:
1225 * 31:20 19:15 14:10 9:5 4:0
1226 * 32b: [ counter | r2num | t2num | r1num | t1num ]
1227 *
1228 * 63:24 23:18 17:12 11:6 5:0
1229 * 64b: [ counter | r2num | t2num | r1num | t1num ]
1230 *
1231 * The change counter is only used to avoid swapping too
1232 * old a value when the value loops back.
Willy Tarreau9d360602023-03-27 10:38:51 +02001233 *
1234 * In the loop below we have this for each index:
1235 * - n is the thread index
1236 * - r is the receiver number
1237 * - g is the receiver's thread group
1238 * - t is the thread number in this receiver
1239 * - m is the receiver's thread mask shifted by the thread number
Willy Tarreaufc630bd2019-03-04 19:57:34 +01001240 */
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001241
1242 /* keep a copy for the final update. thr_idx is composite
Willy Tarreau9d360602023-03-27 10:38:51 +02001243 * and made of (n2<<16) + n1.
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001244 */
Willy Tarreaub6574922023-03-29 17:02:17 +02001245 thr_idx_ptr = l->rx.shard_info ? &((struct listener *)(l->rx.shard_info->ref->owner))->thr_idx : &l->thr_idx;
Willy Tarreau9d360602023-03-27 10:38:51 +02001246 while (1) {
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001247 int q0, q1, q2;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001248
Willy Tarreauff185042023-04-20 16:52:21 +02001249 /* calculate r1/g1/t1 first (ascending idx) */
1250 n0 = _HA_ATOMIC_LOAD(thr_idx_ptr);
Willy Tarreau9d360602023-03-27 10:38:51 +02001251 new_li = NULL;
1252
Willy Tarreauff185042023-04-20 16:52:21 +02001253 t1 = (uint)n0 & (LONGBITS - 1);
1254 r1 = ((uint)n0 / LONGBITS) & (LONGBITS - 1);
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001255
Willy Tarreau9d360602023-03-27 10:38:51 +02001256 while (1) {
1257 if (l->rx.shard_info) {
1258 /* multiple listeners, take the group into account */
1259 if (r1 >= l->rx.shard_info->nbgroups)
1260 r1 = 0;
1261
1262 g1 = &ha_tgroup_info[l->rx.shard_info->members[r1]->bind_tgroup - 1];
1263 m1 = l->rx.shard_info->members[r1]->bind_thread;
1264 } else {
1265 /* single listener */
1266 r1 = 0;
1267 g1 = tg;
1268 m1 = l->rx.bind_thread;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001269 }
Willy Tarreau9d360602023-03-27 10:38:51 +02001270 m1 &= _HA_ATOMIC_LOAD(&g1->threads_enabled);
1271 m1 >>= t1;
1272
1273 /* find first existing thread */
1274 if (unlikely(!(m1 & 1))) {
1275 m1 &= ~1UL;
1276 if (!m1) {
1277 /* no more threads here, switch to
1278 * first thread of next group.
1279 */
1280 t1 = 0;
1281 if (l->rx.shard_info)
1282 r1++;
1283 /* loop again */
1284 continue;
1285 }
1286 t1 += my_ffsl(m1) - 1;
1287 }
1288 /* done: r1 and t1 are OK */
1289 break;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001290 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001291
Willy Tarreauff185042023-04-20 16:52:21 +02001292 /* now r2/g2/t2 (descending idx) */
1293 t2 = ((uint)n0 / LONGBITS / LONGBITS) & (LONGBITS - 1);
1294 r2 = ((uint)n0 / LONGBITS / LONGBITS / LONGBITS) & (LONGBITS - 1);
Willy Tarreau9d360602023-03-27 10:38:51 +02001295
Willy Tarreau84fe1f42023-04-20 15:40:38 +02001296 /* if running in round-robin mode ("fair"), we don't need
1297 * to go further.
1298 */
1299 if ((global.tune.options & GTUNE_LISTENER_MQ_ANY) == GTUNE_LISTENER_MQ_FAIR) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001300 t = g1->base + t1;
1301 if (l->rx.shard_info && t != tid)
1302 new_li = l->rx.shard_info->members[r1]->owner;
Willy Tarreau84fe1f42023-04-20 15:40:38 +02001303 goto updt_t1;
1304 }
1305
Willy Tarreau9d360602023-03-27 10:38:51 +02001306 while (1) {
1307 if (l->rx.shard_info) {
1308 /* multiple listeners, take the group into account */
1309 if (r2 >= l->rx.shard_info->nbgroups)
1310 r2 = l->rx.shard_info->nbgroups - 1;
Willy Tarreau85d04242019-04-16 18:09:13 +02001311
Willy Tarreau9d360602023-03-27 10:38:51 +02001312 g2 = &ha_tgroup_info[l->rx.shard_info->members[r2]->bind_tgroup - 1];
1313 m2 = l->rx.shard_info->members[r2]->bind_thread;
1314 } else {
1315 /* single listener */
1316 r2 = 0;
1317 g2 = tg;
1318 m2 = l->rx.bind_thread;
1319 }
1320 m2 &= _HA_ATOMIC_LOAD(&g2->threads_enabled);
1321 m2 &= nbits(t2 + 1);
1322
1323 /* find previous existing thread */
1324 if (unlikely(!(m2 & (1UL << t2)) || (g1 == g2 && t1 == t2))) {
1325 /* highest bit not set or colliding threads, let's check
1326 * if we still have other threads available after this
1327 * one.
1328 */
1329 m2 &= ~(1UL << t2);
1330 if (!m2) {
1331 /* no more threads here, switch to
1332 * last thread of previous group.
1333 */
1334 t2 = MAX_THREADS_PER_GROUP - 1;
1335 if (l->rx.shard_info)
1336 r2--;
1337 /* loop again */
1338 continue;
1339 }
1340 t2 = my_flsl(m2) - 1;
1341 }
1342 /* done: r2 and t2 are OK */
1343 break;
Willy Tarreau85d04242019-04-16 18:09:13 +02001344 }
1345
Willy Tarreau77e33502023-04-19 17:19:28 +02001346 /* tests show that it's worth checking that other threads have not
1347 * already changed the index to save the rest of the calculation,
1348 * or we'd have to redo it anyway.
1349 */
Willy Tarreauff185042023-04-20 16:52:21 +02001350 if (n0 != _HA_ATOMIC_LOAD(thr_idx_ptr))
Willy Tarreau77e33502023-04-19 17:19:28 +02001351 continue;
Willy Tarreau77e33502023-04-19 17:19:28 +02001352
Willy Tarreau9d360602023-03-27 10:38:51 +02001353 /* here we have (r1,g1,t1) that designate the first receiver, its
1354 * thread group and local thread, and (r2,g2,t2) that designate
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001355 * the second receiver, its thread group and local thread. We'll
1356 * also consider the local thread with q0.
Willy Tarreau9d360602023-03-27 10:38:51 +02001357 */
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001358 q0 = accept_queue_ring_len(&accept_queue_rings[tid]);
Willy Tarreau9d360602023-03-27 10:38:51 +02001359 q1 = accept_queue_ring_len(&accept_queue_rings[g1->base + t1]);
1360 q2 = accept_queue_ring_len(&accept_queue_rings[g2->base + t2]);
1361
1362 /* add to this the currently active connections */
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001363 q0 += _HA_ATOMIC_LOAD(&l->thr_conn[ti->ltid]);
Willy Tarreau9d360602023-03-27 10:38:51 +02001364 if (l->rx.shard_info) {
1365 q1 += _HA_ATOMIC_LOAD(&((struct listener *)l->rx.shard_info->members[r1]->owner)->thr_conn[t1]);
1366 q2 += _HA_ATOMIC_LOAD(&((struct listener *)l->rx.shard_info->members[r2]->owner)->thr_conn[t2]);
1367 } else {
1368 q1 += _HA_ATOMIC_LOAD(&l->thr_conn[t1]);
1369 q2 += _HA_ATOMIC_LOAD(&l->thr_conn[t2]);
1370 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001371
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001372 /* we have 3 possibilities now :
1373 * q1 < q2 : t1 is less loaded than t2, so we pick it
1374 * and update t2 (since t1 might still be
1375 * lower than another thread)
1376 * q1 > q2 : t2 is less loaded than t1, so we pick it
1377 * and update t1 (since t2 might still be
1378 * lower than another thread)
1379 * q1 = q2 : both are equally loaded, thus we pick t1
1380 * and update t1 as it will become more loaded
1381 * than t2.
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001382 * On top of that, if in the end the current thread appears
1383 * to be as good of a deal, we'll prefer it over a foreign
1384 * one as it will improve locality and avoid a migration.
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001385 */
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001386
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001387 if (q1 - q2 < 0) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001388 t = g1->base + t1;
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001389 if (q0 <= q1)
1390 t = tid;
Willy Tarreau9d360602023-03-27 10:38:51 +02001391
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001392 if (l->rx.shard_info && t != tid)
Willy Tarreau9d360602023-03-27 10:38:51 +02001393 new_li = l->rx.shard_info->members[r1]->owner;
1394
1395 t2--;
1396 if (t2 >= MAX_THREADS_PER_GROUP) {
1397 if (l->rx.shard_info)
1398 r2--;
1399 t2 = MAX_THREADS_PER_GROUP - 1;
1400 }
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001401 }
1402 else if (q1 - q2 > 0) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001403 t = g2->base + t2;
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001404 if (q0 <= q2)
1405 t = tid;
Willy Tarreau9d360602023-03-27 10:38:51 +02001406
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001407 if (l->rx.shard_info && t != tid)
Willy Tarreau9d360602023-03-27 10:38:51 +02001408 new_li = l->rx.shard_info->members[r2]->owner;
1409 goto updt_t1;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001410 }
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001411 else { // q1 == q2
Willy Tarreau9d360602023-03-27 10:38:51 +02001412 t = g1->base + t1;
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001413 if (q0 < q1) // local must be strictly better than both
1414 t = tid;
Willy Tarreau9d360602023-03-27 10:38:51 +02001415
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001416 if (l->rx.shard_info && t != tid)
Willy Tarreau9d360602023-03-27 10:38:51 +02001417 new_li = l->rx.shard_info->members[r1]->owner;
Willy Tarreau84fe1f42023-04-20 15:40:38 +02001418 updt_t1:
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001419 t1++;
Willy Tarreau9d360602023-03-27 10:38:51 +02001420 if (t1 >= MAX_THREADS_PER_GROUP) {
1421 if (l->rx.shard_info)
1422 r1++;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001423 t1 = 0;
Willy Tarreau9d360602023-03-27 10:38:51 +02001424 }
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001425 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001426
Willy Tarreauff185042023-04-20 16:52:21 +02001427 /* The target thread number is in <t> now. Let's
1428 * compute the new index and try to update it.
1429 */
Willy Tarreau9d360602023-03-27 10:38:51 +02001430
Willy Tarreauff185042023-04-20 16:52:21 +02001431 /* take previous counter and increment it */
1432 n1 = n0 & -(ulong)(LONGBITS * LONGBITS * LONGBITS * LONGBITS);
1433 n1 += LONGBITS * LONGBITS * LONGBITS * LONGBITS;
1434 n1 += (((r2 * LONGBITS) + t2) * LONGBITS * LONGBITS);
1435 n1 += (r1 * LONGBITS) + t1;
Willy Tarreaub6574922023-03-29 17:02:17 +02001436 if (likely(_HA_ATOMIC_CAS(thr_idx_ptr, &n0, n1)))
Willy Tarreau9d360602023-03-27 10:38:51 +02001437 break;
Willy Tarreauff185042023-04-20 16:52:21 +02001438
1439 /* bah we lost the race, try again */
1440 __ha_cpu_relax();
Willy Tarreau9d360602023-03-27 10:38:51 +02001441 } /* end of main while() loop */
1442
1443 /* we may need to update the listener in the connection
1444 * if we switched to another group.
1445 */
1446 if (new_li)
1447 cli_conn->target = &new_li->obj_type;
1448
1449 /* here we have the target thread number in <t> and we hold a
1450 * reservation in the target ring.
1451 */
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001452
Amaury Denoyellea66e0432023-04-05 18:16:28 +02001453 if (l->rx.proto && l->rx.proto->set_affinity) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001454 if (l->rx.proto->set_affinity(cli_conn, t)) {
Amaury Denoyellea66e0432023-04-05 18:16:28 +02001455 /* Failed migration, stay on the same thread. */
1456 goto local_accept;
1457 }
1458 }
1459
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001460 /* We successfully selected the best thread "t" for this
1461 * connection. We use deferred accepts even if it's the
1462 * local thread because tests show that it's the best
1463 * performing model, likely due to better cache locality
1464 * when processing this loop.
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001465 */
Willy Tarreau9d360602023-03-27 10:38:51 +02001466 ring = &accept_queue_rings[t];
Willy Tarreau83efc322020-10-14 17:37:17 +02001467 if (accept_queue_push_mp(ring, cli_conn)) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001468 _HA_ATOMIC_INC(&activity[t].accq_pushed);
Willy Tarreau2bd65a72019-09-24 06:55:18 +02001469 tasklet_wakeup(ring->tasklet);
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001470 continue;
1471 }
1472 /* If the ring is full we do a synchronous accept on
1473 * the local thread here.
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001474 */
Willy Tarreau9d360602023-03-27 10:38:51 +02001475 _HA_ATOMIC_INC(&activity[t].accq_full);
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001476 }
1477#endif // USE_THREAD
1478
Amaury Denoyelle7f7713d2022-01-19 11:37:50 +01001479 local_accept:
Willy Tarreau9d360602023-03-27 10:38:51 +02001480 /* restore the connection's listener in case we failed to migrate above */
1481 cli_conn->target = &l->obj_type;
Willy Tarreaufea8c192023-02-28 10:25:57 +01001482 _HA_ATOMIC_INC(&l->thr_conn[ti->ltid]);
Willy Tarreau30836152023-01-12 19:10:17 +01001483 ret = l->bind_conf->accept(cli_conn);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001484 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001485 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001486 * we just have to ignore it (ret == 0) or it's a critical
1487 * error due to a resource shortage, and we must stop the
1488 * listener (ret < 0).
1489 */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001490 if (ret == 0) /* successful termination */
1491 continue;
1492
Willy Tarreaubb660302014-05-07 19:47:02 +02001493 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001494 }
1495
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001496 /* increase the per-process number of cumulated sessions, this
Willy Tarreau30836152023-01-12 19:10:17 +01001497 * may only be done once l->bind_conf->accept() has accepted the
1498 * connection.
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001499 */
Willy Tarreau17146802023-01-12 19:58:42 +01001500 if (!(l->bind_conf->options & BC_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001501 count = update_freq_ctr(&global.sess_per_sec, 1);
1502 HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
Willy Tarreau93e7c002013-10-07 18:51:07 +02001503 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001504#ifdef USE_OPENSSL
Willy Tarreau17146802023-01-12 19:58:42 +01001505 if (!(l->bind_conf->options & BC_O_UNLIMITED) &&
Willy Tarreau11ba4042022-05-20 15:56:32 +02001506 l->bind_conf && l->bind_conf->options & BC_O_USE_SSL) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001507 count = update_freq_ctr(&global.ssl_per_sec, 1);
1508 HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
Willy Tarreaue43d5322013-10-07 20:01:52 +02001509 }
1510#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +02001511
Willy Tarreaubdcd3252022-06-22 09:19:46 +02001512 _HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_STUCK); // this thread is still running
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001513 } /* end of for (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001514
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001515 end:
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001516 if (next_conn)
Willy Tarreau4781b152021-04-06 13:53:36 +02001517 _HA_ATOMIC_DEC(&l->nbconn);
Willy Tarreau741b4d62019-02-25 15:02:04 +01001518
Willy Tarreau82c97892019-02-27 19:32:32 +01001519 if (p && next_feconn)
Willy Tarreau4781b152021-04-06 13:53:36 +02001520 _HA_ATOMIC_DEC(&p->feconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001521
1522 if (next_actconn)
Willy Tarreau4781b152021-04-06 13:53:36 +02001523 _HA_ATOMIC_DEC(&actconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001524
Willy Tarreau758c69d2023-01-12 18:59:37 +01001525 if ((l->state == LI_FULL && (!l->bind_conf->maxconn || l->nbconn < l->bind_conf->maxconn)) ||
Willy Tarreau02757d02021-01-28 18:07:24 +01001526 (l->state == LI_LIMITED &&
Willy Tarreaucdcba112019-12-11 15:06:30 +01001527 ((!p || p->feconn < p->maxconn) && (actconn < global.maxconn) &&
1528 (!tick_isset(global_listener_queue_task->expire) ||
1529 tick_is_expired(global_listener_queue_task->expire, now_ms))))) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001530 /* at least one thread has to this when quitting */
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +01001531 relax_listener(l, 0, 0);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001532
Willy Tarreau02757d02021-01-28 18:07:24 +01001533 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001534 dequeue_all_listeners();
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001535
Olivier Houchard859dc802019-08-08 15:47:21 +02001536 if (p && !MT_LIST_ISEMPTY(&p->listener_queue) &&
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001537 (!p->fe_sps_lim || freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0) > 0))
Willy Tarreau241797a2019-12-10 14:10:52 +01001538 dequeue_proxy_listeners(p);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001539 }
Willy Tarreau0591bf72019-12-10 12:01:21 +01001540 return;
1541
1542 transient_error:
1543 /* pause the listener for up to 100 ms */
1544 expire = tick_add(now_ms, 100);
1545
Willy Tarreau258b3512020-10-13 17:46:05 +02001546 /* This may be a shared socket that was paused by another process.
1547 * Let's put it to pause in this case.
1548 */
1549 if (l->rx.proto && l->rx.proto->rx_listening(&l->rx) == 0) {
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +01001550 suspend_listener(l, 0, 0);
Willy Tarreau258b3512020-10-13 17:46:05 +02001551 goto end;
1552 }
1553
Willy Tarreau0591bf72019-12-10 12:01:21 +01001554 limit_global:
1555 /* (re-)queue the listener to the global queue and set it to expire no
1556 * later than <expire> ahead. The listener turns to LI_LIMITED.
1557 */
1558 limit_listener(l, &global_listener_queue);
Christopher Faulet13e86d92022-11-17 14:40:20 +01001559 HA_RWLOCK_RDLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001560 task_schedule(global_listener_queue_task, expire);
Christopher Faulet13e86d92022-11-17 14:40:20 +01001561 HA_RWLOCK_RDUNLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001562 goto end;
1563
1564 limit_proxy:
1565 /* (re-)queue the listener to the proxy's queue and set it to expire no
1566 * later than <expire> ahead. The listener turns to LI_LIMITED.
1567 */
1568 limit_listener(l, &p->listener_queue);
Willy Tarreaueeea8082020-01-08 19:15:07 +01001569 if (p->task && tick_isset(expire))
1570 task_schedule(p->task, expire);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001571 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001572}
1573
Willy Tarreau05f50472017-09-15 09:19:58 +02001574/* Notify the listener that a connection initiated from it was released. This
1575 * is used to keep the connection count consistent and to possibly re-open
1576 * listening when it was limited.
1577 */
1578void listener_release(struct listener *l)
1579{
1580 struct proxy *fe = l->bind_conf->frontend;
1581
Willy Tarreau17146802023-01-12 19:58:42 +01001582 if (!(l->bind_conf->options & BC_O_UNLIMITED))
Willy Tarreau4781b152021-04-06 13:53:36 +02001583 _HA_ATOMIC_DEC(&actconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001584 if (fe)
Willy Tarreau4781b152021-04-06 13:53:36 +02001585 _HA_ATOMIC_DEC(&fe->feconn);
1586 _HA_ATOMIC_DEC(&l->nbconn);
Willy Tarreaufea8c192023-02-28 10:25:57 +01001587 _HA_ATOMIC_DEC(&l->thr_conn[ti->ltid]);
Willy Tarreau82c97892019-02-27 19:32:32 +01001588
1589 if (l->state == LI_FULL || l->state == LI_LIMITED)
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +01001590 relax_listener(l, 0, 0);
Willy Tarreau05f50472017-09-15 09:19:58 +02001591
Willy Tarreau02757d02021-01-28 18:07:24 +01001592 /* Dequeues all of the listeners waiting for a resource */
1593 dequeue_all_listeners();
1594
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +02001595 if (fe && !MT_LIST_ISEMPTY(&fe->listener_queue) &&
Willy Tarreau05f50472017-09-15 09:19:58 +02001596 (!fe->fe_sps_lim || freq_ctr_remain(&fe->fe_sess_per_sec, fe->fe_sps_lim, 0) > 0))
Willy Tarreau241797a2019-12-10 14:10:52 +01001597 dequeue_proxy_listeners(fe);
Willy Tarreau05f50472017-09-15 09:19:58 +02001598}
1599
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001600/* Initializes the listener queues. Returns 0 on success, otherwise ERR_* flags */
1601static int listener_queue_init()
1602{
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001603 global_listener_queue_task = task_new_anywhere();
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001604 if (!global_listener_queue_task) {
1605 ha_alert("Out of memory when initializing global listener queue\n");
1606 return ERR_FATAL|ERR_ABORT;
1607 }
1608 /* very simple initialization, users will queue the task if needed */
1609 global_listener_queue_task->context = NULL; /* not even a context! */
1610 global_listener_queue_task->process = manage_global_listener_queue;
Christopher Faulet13e86d92022-11-17 14:40:20 +01001611 HA_RWLOCK_INIT(&global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001612
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001613 return 0;
1614}
1615
1616static void listener_queue_deinit()
1617{
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001618 task_destroy(global_listener_queue_task);
1619 global_listener_queue_task = NULL;
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001620}
1621
1622REGISTER_CONFIG_POSTPARSER("multi-threaded listener queue", listener_queue_init);
1623REGISTER_POST_DEINIT(listener_queue_deinit);
1624
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001625
1626/* This is the global management task for listeners. It enables listeners waiting
1627 * for global resources when there are enough free resource, or at least once in
Willy Tarreaud597ec22021-01-29 14:29:06 +01001628 * a while. It is designed to be called as a task. It's exported so that it's easy
1629 * to spot in "show tasks" or "show profiling".
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001630 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001631struct task *manage_global_listener_queue(struct task *t, void *context, unsigned int state)
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001632{
1633 /* If there are still too many concurrent connections, let's wait for
1634 * some of them to go away. We don't need to re-arm the timer because
1635 * each of them will scan the queue anyway.
1636 */
1637 if (unlikely(actconn >= global.maxconn))
1638 goto out;
1639
1640 /* We should periodically try to enable listeners waiting for a global
1641 * resource here, because it is possible, though very unlikely, that
1642 * they have been blocked by a temporary lack of global resource such
1643 * as a file descriptor or memory and that the temporary condition has
1644 * disappeared.
1645 */
1646 dequeue_all_listeners();
1647
1648 out:
Christopher Faulet13e86d92022-11-17 14:40:20 +01001649 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001650 t->expire = TICK_ETERNITY;
Christopher Faulet13e86d92022-11-17 14:40:20 +01001651 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001652 return t;
1653}
1654
Willy Tarreauf6a84442023-04-22 23:25:38 +02001655/* Applies the thread mask, shards etc to the bind_conf. It normally returns 0
1656 * otherwie the number of errors. Upon error it may set error codes (ERR_*) in
1657 * err_code. It is supposed to be called only once very late in the boot process
1658 * after the bind_conf's thread_set is fixed. The function may emit warnings and
1659 * alerts. Extra listeners may be created on the fly.
1660 */
1661int bind_complete_thread_setup(struct bind_conf *bind_conf, int *err_code)
1662{
1663 struct proxy *fe = bind_conf->frontend;
1664 struct listener *li, *new_li, *ref;
1665 struct thread_set new_ts;
1666 int shard, shards, todo, done, grp, dups;
1667 ulong mask, gmask, bit;
1668 int cfgerr = 0;
1669 char *err;
1670
1671 err = NULL;
Willy Tarreauc38499c2023-04-22 22:27:31 +02001672 if (thread_resolve_group_mask(&bind_conf->thread_set, 0, &err) < 0) {
Willy Tarreaua22db652023-04-22 23:52:17 +02001673 ha_alert("%s '%s': %s in 'bind %s' at [%s:%d].\n",
1674 proxy_type_str(fe),
Willy Tarreauf6a84442023-04-22 23:25:38 +02001675 fe->id, err, bind_conf->arg, bind_conf->file, bind_conf->line);
1676 free(err);
1677 cfgerr++;
1678 return cfgerr;
1679 }
1680
1681 /* apply thread masks and groups to all receivers */
1682 list_for_each_entry(li, &bind_conf->listeners, by_bind) {
1683 shards = bind_conf->settings.shards;
1684 todo = thread_set_count(&bind_conf->thread_set);
1685
1686 /* special values: -1 = "by-thread", -2 = "by-group" */
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001687 if (shards == -1) {
Willy Tarreau8a5e6f42023-04-22 17:39:30 +02001688 if (protocol_supports_flag(li->rx.proto, PROTO_F_REUSEPORT_SUPPORTED))
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001689 shards = todo;
1690 else {
1691 if (fe != global.cli_fe)
1692 ha_diag_warning("[%s:%d]: Disabling per-thread sharding for listener in"
1693 " %s '%s' because SO_REUSEPORT is disabled\n",
1694 bind_conf->file, bind_conf->line, proxy_type_str(fe), fe->id);
1695 shards = 1;
1696 }
1697 }
Willy Tarreauf6a84442023-04-22 23:25:38 +02001698 else if (shards == -2)
Willy Tarreau8a5e6f42023-04-22 17:39:30 +02001699 shards = protocol_supports_flag(li->rx.proto, PROTO_F_REUSEPORT_SUPPORTED) ? my_popcountl(bind_conf->thread_set.grps) : 1;
Willy Tarreauf6a84442023-04-22 23:25:38 +02001700
1701 /* no more shards than total threads */
1702 if (shards > todo)
1703 shards = todo;
1704
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001705 /* We also need to check if an explicit shards count was set and cannot be honored */
Willy Tarreau8a5e6f42023-04-22 17:39:30 +02001706 if (shards > 1 && !protocol_supports_flag(li->rx.proto, PROTO_F_REUSEPORT_SUPPORTED)) {
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001707 ha_warning("[%s:%d]: Disabling sharding for listener in %s '%s' because SO_REUSEPORT is disabled\n",
1708 bind_conf->file, bind_conf->line, proxy_type_str(fe), fe->id);
1709 shards = 1;
1710 }
1711
Willy Tarreauf6a84442023-04-22 23:25:38 +02001712 shard = done = grp = bit = mask = 0;
1713 new_li = li;
1714
1715 while (shard < shards) {
1716 memset(&new_ts, 0, sizeof(new_ts));
1717 while (grp < global.nbtgroups && done < todo) {
1718 /* enlarge mask to cover next bit of bind_thread till we
1719 * have enough bits for one shard. We restart from the
1720 * current grp+bit.
1721 */
1722
1723 /* first let's find the first non-empty group starting at <mask> */
1724 if (!(bind_conf->thread_set.rel[grp] & ha_tgroup_info[grp].threads_enabled & ~mask)) {
1725 grp++;
1726 mask = 0;
1727 continue;
1728 }
1729
1730 /* take next unassigned bit */
1731 bit = (bind_conf->thread_set.rel[grp] & ~mask) & -(bind_conf->thread_set.rel[grp] & ~mask);
1732 new_ts.rel[grp] |= bit;
1733 mask |= bit;
1734 new_ts.grps |= 1UL << grp;
1735
1736 done += shards;
1737 };
1738
1739 BUG_ON(!new_ts.grps); // no more bits left unassigned
1740
1741 /* Create all required listeners for all bound groups. If more than one group is
1742 * needed, the first receiver serves as a reference, and subsequent ones point to
1743 * it. We already have a listener available in new_li() so we only allocate a new
1744 * one if we're not on the last one. We count the remaining groups by copying their
1745 * mask into <gmask> and dropping the lowest bit at the end of the loop until there
1746 * is no more. Ah yes, it's not pretty :-/
1747 */
1748 ref = new_li;
1749 gmask = new_ts.grps;
1750 for (dups = 0; gmask; dups++) {
1751 /* assign the first (and only) thread and group */
1752 new_li->rx.bind_thread = thread_set_nth_tmask(&new_ts, dups);
1753 new_li->rx.bind_tgroup = thread_set_nth_group(&new_ts, dups);
1754
1755 if (dups) {
1756 /* it has been allocated already in the previous round */
1757 shard_info_attach(&new_li->rx, ref->rx.shard_info);
1758 new_li->rx.flags |= RX_F_MUST_DUP;
1759 }
1760
1761 gmask &= gmask - 1; // drop lowest bit
1762 if (gmask) {
1763 /* yet another listener expected in this shard, let's
1764 * chain it.
1765 */
1766 struct listener *tmp_li = clone_listener(new_li);
1767
1768 if (!tmp_li) {
1769 ha_alert("Out of memory while trying to allocate extra listener for group %u of shard %d in %s %s\n",
1770 new_li->rx.bind_tgroup, shard, proxy_type_str(fe), fe->id);
1771 cfgerr++;
1772 *err_code |= ERR_FATAL | ERR_ALERT;
1773 return cfgerr;
1774 }
1775
1776 /* if we're forced to create at least two listeners, we have to
1777 * allocate a shared shard_info that's linked to from the reference
1778 * and each other listener, so we'll create it here.
1779 */
1780 if (!shard_info_attach(&ref->rx, NULL)) {
1781 ha_alert("Out of memory while trying to allocate shard_info for listener for group %u of shard %d in %s %s\n",
1782 new_li->rx.bind_tgroup, shard, proxy_type_str(fe), fe->id);
1783 cfgerr++;
1784 *err_code |= ERR_FATAL | ERR_ALERT;
1785 return cfgerr;
1786 }
1787 new_li = tmp_li;
1788 }
1789 }
1790 done -= todo;
1791
1792 shard++;
1793 if (shard >= shards)
1794 break;
1795
1796 /* create another listener for new shards */
1797 new_li = clone_listener(li);
1798 if (!new_li) {
1799 ha_alert("Out of memory while trying to allocate extra listener for shard %d in %s %s\n",
1800 shard, proxy_type_str(fe), fe->id);
1801 cfgerr++;
1802 *err_code |= ERR_FATAL | ERR_ALERT;
1803 return cfgerr;
1804 }
1805 }
1806 }
1807
1808 /* success */
1809 return cfgerr;
1810}
1811
Willy Tarreau26982662012-09-12 23:17:10 +02001812/*
1813 * Registers the bind keyword list <kwl> as a list of valid keywords for next
1814 * parsing sessions.
1815 */
1816void bind_register_keywords(struct bind_kw_list *kwl)
1817{
Willy Tarreau2b718102021-04-21 07:32:39 +02001818 LIST_APPEND(&bind_keywords.list, &kwl->list);
Willy Tarreau26982662012-09-12 23:17:10 +02001819}
1820
1821/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
1822 * keyword is found with a NULL ->parse() function, then an attempt is made to
1823 * find one with a valid ->parse() function. This way it is possible to declare
1824 * platform-dependant, known keywords as NULL, then only declare them as valid
1825 * if some options are met. Note that if the requested keyword contains an
1826 * opening parenthesis, everything from this point is ignored.
1827 */
1828struct bind_kw *bind_find_kw(const char *kw)
1829{
1830 int index;
1831 const char *kwend;
1832 struct bind_kw_list *kwl;
1833 struct bind_kw *ret = NULL;
1834
1835 kwend = strchr(kw, '(');
1836 if (!kwend)
1837 kwend = kw + strlen(kw);
1838
1839 list_for_each_entry(kwl, &bind_keywords.list, list) {
1840 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1841 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1842 kwl->kw[index].kw[kwend-kw] == 0) {
1843 if (kwl->kw[index].parse)
1844 return &kwl->kw[index]; /* found it !*/
1845 else
1846 ret = &kwl->kw[index]; /* may be OK */
1847 }
1848 }
1849 }
1850 return ret;
1851}
1852
Willy Tarreau8638f482012-09-18 18:01:17 +02001853/* Dumps all registered "bind" keywords to the <out> string pointer. The
1854 * unsupported keywords are only dumped if their supported form was not
1855 * found.
1856 */
1857void bind_dump_kws(char **out)
1858{
1859 struct bind_kw_list *kwl;
1860 int index;
1861
Christopher Faulet784063e2020-05-18 12:14:18 +02001862 if (!out)
1863 return;
1864
Willy Tarreau8638f482012-09-18 18:01:17 +02001865 *out = NULL;
1866 list_for_each_entry(kwl, &bind_keywords.list, list) {
1867 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1868 if (kwl->kw[index].parse ||
1869 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +02001870 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
1871 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +02001872 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +02001873 kwl->kw[index].skip ? " <arg>" : "",
1874 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +02001875 }
1876 }
1877 }
1878}
1879
Willy Tarreau433b05f2021-03-12 10:14:07 +01001880/* Try to find in srv_keyword the word that looks closest to <word> by counting
1881 * transitions between letters, digits and other characters. Will return the
1882 * best matching word if found, otherwise NULL.
1883 */
1884const char *bind_find_best_kw(const char *word)
1885{
1886 uint8_t word_sig[1024];
1887 uint8_t list_sig[1024];
1888 const struct bind_kw_list *kwl;
1889 const char *best_ptr = NULL;
1890 int dist, best_dist = INT_MAX;
1891 int index;
1892
1893 make_word_fingerprint(word_sig, word);
1894 list_for_each_entry(kwl, &bind_keywords.list, list) {
1895 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1896 make_word_fingerprint(list_sig, kwl->kw[index].kw);
1897 dist = word_fingerprint_distance(word_sig, list_sig);
1898 if (dist < best_dist) {
1899 best_dist = dist;
1900 best_ptr = kwl->kw[index].kw;
1901 }
1902 }
1903 }
1904
1905 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
1906 best_ptr = NULL;
1907
1908 return best_ptr;
1909}
1910
Willy Tarreaudbf78022021-10-06 09:05:08 +02001911/* allocate an bind_conf struct for a bind line, and chain it to the frontend <fe>.
1912 * If <arg> is not NULL, it is duplicated into ->arg to store useful config
1913 * information for error reporting. NULL is returned on error.
1914 */
1915struct bind_conf *bind_conf_alloc(struct proxy *fe, const char *file,
1916 int line, const char *arg, struct xprt_ops *xprt)
1917{
1918 struct bind_conf *bind_conf = calloc(1, sizeof(*bind_conf));
1919
1920 if (!bind_conf)
1921 goto err;
1922
1923 bind_conf->file = strdup(file);
1924 if (!bind_conf->file)
1925 goto err;
1926 bind_conf->line = line;
1927 if (arg) {
1928 bind_conf->arg = strdup(arg);
1929 if (!bind_conf->arg)
1930 goto err;
1931 }
1932
1933 LIST_APPEND(&fe->conf.bind, &bind_conf->by_fe);
1934 bind_conf->settings.ux.uid = -1;
1935 bind_conf->settings.ux.gid = -1;
1936 bind_conf->settings.ux.mode = 0;
Willy Tarreau73101642023-04-22 22:06:23 +02001937 bind_conf->settings.shards = global.tune.default_shards;
Willy Tarreaudbf78022021-10-06 09:05:08 +02001938 bind_conf->xprt = xprt;
1939 bind_conf->frontend = fe;
Willy Tarreau7866e8e2023-01-12 18:39:42 +01001940 bind_conf->analysers = fe->fe_req_ana;
Willy Tarreaudbf78022021-10-06 09:05:08 +02001941 bind_conf->severity_output = CLI_SEVERITY_NONE;
1942#ifdef USE_OPENSSL
1943 HA_RWLOCK_INIT(&bind_conf->sni_lock);
1944 bind_conf->sni_ctx = EB_ROOT;
1945 bind_conf->sni_w_ctx = EB_ROOT;
1946#endif
1947 LIST_INIT(&bind_conf->listeners);
1948 return bind_conf;
1949
1950 err:
1951 if (bind_conf) {
1952 ha_free(&bind_conf->file);
1953 ha_free(&bind_conf->arg);
1954 }
1955 ha_free(&bind_conf);
1956 return NULL;
1957}
1958
1959const char *listener_state_str(const struct listener *l)
1960{
1961 static const char *states[8] = {
1962 "NEW", "INI", "ASS", "PAU", "LIS", "RDY", "FUL", "LIM",
1963 };
1964 unsigned int st = l->state;
1965
1966 if (st >= sizeof(states) / sizeof(*states))
1967 return "INVALID";
1968 return states[st];
1969}
1970
Willy Tarreau645513a2010-05-24 20:55:15 +02001971/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001972/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +02001973/************************************************************************/
1974
Willy Tarreaua5e37562011-12-16 17:06:15 +01001975/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +02001976static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001977smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001978{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001979 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001980 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +02001981 return 1;
1982}
1983
Willy Tarreaua5e37562011-12-16 17:06:15 +01001984/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +02001985static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001986smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001987{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001988 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001989 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +02001990 return 1;
1991}
Jerome Magnineb421b22020-03-27 22:08:40 +01001992static int
1993smp_fetch_so_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
1994{
1995 smp->data.u.str.area = smp->sess->listener->name;
1996 if (!smp->data.u.str.area)
1997 return 0;
1998
1999 smp->data.type = SMP_T_STR;
2000 smp->flags = SMP_F_CONST;
2001 smp->data.u.str.data = strlen(smp->data.u.str.area);
2002 return 1;
2003}
Willy Tarreau645513a2010-05-24 20:55:15 +02002004
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002005/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002006static int bind_parse_accept_proxy(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002007{
Willy Tarreauf1b47302023-01-12 19:48:50 +01002008 conf->options |= BC_O_ACC_PROXY;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002009 return 0;
2010}
2011
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002012/* parse the "accept-netscaler-cip" bind keyword */
2013static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2014{
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002015 uint32_t val;
2016
2017 if (!*args[cur_arg + 1]) {
2018 memprintf(err, "'%s' : missing value", args[cur_arg]);
2019 return ERR_ALERT | ERR_FATAL;
2020 }
2021
2022 val = atol(args[cur_arg + 1]);
2023 if (val <= 0) {
Willy Tarreaue2711c72019-02-27 15:39:41 +01002024 memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002025 return ERR_ALERT | ERR_FATAL;
2026 }
2027
Willy Tarreauf1b47302023-01-12 19:48:50 +01002028 conf->options |= BC_O_ACC_CIP;
2029 conf->ns_cip_magic = val;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002030 return 0;
2031}
2032
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002033/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002034static int bind_parse_backlog(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002035{
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002036 int val;
2037
2038 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002039 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002040 return ERR_ALERT | ERR_FATAL;
2041 }
2042
2043 val = atol(args[cur_arg + 1]);
Willy Tarreaue2711c72019-02-27 15:39:41 +01002044 if (val < 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002045 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002046 return ERR_ALERT | ERR_FATAL;
2047 }
2048
Willy Tarreau1920f892023-01-12 18:55:13 +01002049 conf->backlog = val;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002050 return 0;
2051}
2052
2053/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002054static int bind_parse_id(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002055{
2056 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002057 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +01002058 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002059
Willy Tarreau4348fad2012-09-20 16:48:07 +02002060 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002061 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002062 return ERR_ALERT | ERR_FATAL;
2063 }
2064
2065 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002066 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002067 return ERR_ALERT | ERR_FATAL;
2068 }
2069
Willy Tarreau4348fad2012-09-20 16:48:07 +02002070 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +01002071 new->luid = strtol(args[cur_arg + 1], &error, 10);
2072 if (*error != '\0') {
2073 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
2074 return ERR_ALERT | ERR_FATAL;
2075 }
Willy Tarreau4348fad2012-09-20 16:48:07 +02002076 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002077
Willy Tarreau4348fad2012-09-20 16:48:07 +02002078 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002079 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002080 return ERR_ALERT | ERR_FATAL;
2081 }
2082
Willy Tarreau4348fad2012-09-20 16:48:07 +02002083 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002084 if (node) {
2085 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002086 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
2087 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
2088 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002089 return ERR_ALERT | ERR_FATAL;
2090 }
2091
Willy Tarreau4348fad2012-09-20 16:48:07 +02002092 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002093 return 0;
2094}
2095
Willy Tarreau3882d2a2022-05-20 15:41:45 +02002096/* Complete a bind_conf by parsing the args after the address. <args> is the
2097 * arguments array, <cur_arg> is the first one to be considered. <section> is
2098 * the section name to report in error messages, and <file> and <linenum> are
2099 * the file name and line number respectively. Note that args[0..1] are used
2100 * in error messages to provide some context. The return value is an error
2101 * code, zero on success or an OR of ERR_{FATAL,ABORT,ALERT,WARN}.
2102 */
2103int bind_parse_args_list(struct bind_conf *bind_conf, char **args, int cur_arg, const char *section, const char *file, int linenum)
2104{
2105 int err_code = 0;
2106
2107 while (*(args[cur_arg])) {
2108 struct bind_kw *kw;
2109 const char *best;
2110
2111 kw = bind_find_kw(args[cur_arg]);
2112 if (kw) {
2113 char *err = NULL;
2114 int code;
2115
2116 if (!kw->parse) {
2117 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : '%s' option is not implemented in this version (check build options).\n",
2118 file, linenum, args[0], args[1], section, args[cur_arg]);
2119 cur_arg += 1 + kw->skip ;
2120 err_code |= ERR_ALERT | ERR_FATAL;
2121 goto out;
2122 }
2123
2124 code = kw->parse(args, cur_arg, bind_conf->frontend, bind_conf, &err);
2125 err_code |= code;
2126
2127 if (code) {
2128 if (err && *err) {
2129 indent_msg(&err, 2);
2130 if (((code & (ERR_WARN|ERR_ALERT)) == ERR_WARN))
2131 ha_warning("parsing [%s:%d] : '%s %s' in section '%s' : %s\n", file, linenum, args[0], args[1], section, err);
2132 else
2133 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : %s\n", file, linenum, args[0], args[1], section, err);
2134 }
2135 else
2136 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : error encountered while processing '%s'.\n",
2137 file, linenum, args[0], args[1], section, args[cur_arg]);
2138 if (code & ERR_FATAL) {
2139 free(err);
2140 cur_arg += 1 + kw->skip;
2141 goto out;
2142 }
2143 }
2144 free(err);
2145 cur_arg += 1 + kw->skip;
2146 continue;
2147 }
2148
2149 best = bind_find_best_kw(args[cur_arg]);
2150 if (best)
2151 ha_alert("parsing [%s:%d] : '%s %s' in section '%s': unknown keyword '%s'; did you mean '%s' maybe ?\n",
2152 file, linenum, args[0], args[1], section, args[cur_arg], best);
2153 else
2154 ha_alert("parsing [%s:%d] : '%s %s' in section '%s': unknown keyword '%s'.\n",
2155 file, linenum, args[0], args[1], section, args[cur_arg]);
2156
2157 err_code |= ERR_ALERT | ERR_FATAL;
2158 goto out;
2159 }
Willy Tarreau64306cc2022-05-20 16:20:52 +02002160
2161 if ((bind_conf->options & (BC_O_USE_SOCK_DGRAM|BC_O_USE_SOCK_STREAM)) == (BC_O_USE_SOCK_DGRAM|BC_O_USE_SOCK_STREAM) ||
2162 (bind_conf->options & (BC_O_USE_XPRT_DGRAM|BC_O_USE_XPRT_STREAM)) == (BC_O_USE_XPRT_DGRAM|BC_O_USE_XPRT_STREAM)) {
2163 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : cannot mix datagram and stream protocols.\n",
2164 file, linenum, args[0], args[1], section);
2165 err_code |= ERR_ALERT | ERR_FATAL;
2166 goto out;
2167 }
2168
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002169 /* The transport layer automatically switches to QUIC when QUIC is
2170 * selected, regardless of bind_conf settings. We then need to
2171 * initialize QUIC params.
2172 */
2173 if ((bind_conf->options & (BC_O_USE_SOCK_DGRAM|BC_O_USE_XPRT_STREAM)) == (BC_O_USE_SOCK_DGRAM|BC_O_USE_XPRT_STREAM)) {
2174#ifdef USE_QUIC
2175 bind_conf->xprt = xprt_get(XPRT_QUIC);
Willy Tarreau287f32f2022-05-20 18:16:52 +02002176 if (!(bind_conf->options & BC_O_USE_SSL)) {
2177 bind_conf->options |= BC_O_USE_SSL;
2178 ha_warning("parsing [%s:%d] : '%s %s' in section '%s' : QUIC protocol detected, enabling ssl. Use 'ssl' to shut this warning.\n",
2179 file, linenum, args[0], args[1], section);
2180 }
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002181 quic_transport_params_init(&bind_conf->quic_params, 1);
2182#else
2183 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : QUIC protocol selected but support not compiled in (check build options).\n",
2184 file, linenum, args[0], args[1], section);
2185 err_code |= ERR_ALERT | ERR_FATAL;
2186 goto out;
2187#endif
2188 }
Willy Tarreau2071a992022-05-20 17:14:31 +02002189 else if (bind_conf->options & BC_O_USE_SSL) {
2190 bind_conf->xprt = xprt_get(XPRT_SSL);
2191 }
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002192
Willy Tarreau3882d2a2022-05-20 15:41:45 +02002193 out:
2194 return err_code;
2195}
2196
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002197/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002198static int bind_parse_maxconn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002199{
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002200 int val;
2201
2202 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002203 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002204 return ERR_ALERT | ERR_FATAL;
2205 }
2206
2207 val = atol(args[cur_arg + 1]);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01002208 if (val < 0) {
2209 memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002210 return ERR_ALERT | ERR_FATAL;
2211 }
2212
Willy Tarreau758c69d2023-01-12 18:59:37 +01002213 conf->maxconn = val;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002214 return 0;
2215}
2216
2217/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002218static int bind_parse_name(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002219{
2220 struct listener *l;
2221
2222 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002223 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002224 return ERR_ALERT | ERR_FATAL;
2225 }
2226
Willy Tarreau4348fad2012-09-20 16:48:07 +02002227 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002228 l->name = strdup(args[cur_arg + 1]);
2229
2230 return 0;
2231}
2232
2233/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002234static int bind_parse_nice(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002235{
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002236 int val;
2237
2238 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002239 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002240 return ERR_ALERT | ERR_FATAL;
2241 }
2242
2243 val = atol(args[cur_arg + 1]);
2244 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002245 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002246 return ERR_ALERT | ERR_FATAL;
2247 }
2248
Willy Tarreau7dbd4182023-01-12 19:32:45 +01002249 conf->nice = val;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002250 return 0;
2251}
2252
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02002253/* parse the "process" bind keyword */
2254static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2255{
Willy Tarreauacd64412022-07-15 17:16:01 +02002256 memprintf(err, "'process %s' on 'bind' lines is not supported anymore, please use 'thread' instead.", args[cur_arg+1]);
2257 return ERR_ALERT | ERR_FATAL;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02002258}
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002259
Christopher Fauleta717b992018-04-10 14:43:00 +02002260/* parse the "proto" bind keyword */
2261static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2262{
2263 struct ist proto;
2264
2265 if (!*args[cur_arg + 1]) {
2266 memprintf(err, "'%s' : missing value", args[cur_arg]);
2267 return ERR_ALERT | ERR_FATAL;
2268 }
2269
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002270 proto = ist(args[cur_arg + 1]);
Christopher Fauleta717b992018-04-10 14:43:00 +02002271 conf->mux_proto = get_mux_proto(proto);
2272 if (!conf->mux_proto) {
2273 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]);
2274 return ERR_ALERT | ERR_FATAL;
2275 }
Willy Tarreauc8cac042021-09-21 14:31:29 +02002276 return 0;
2277}
2278
Willy Tarreaua07635e2023-04-13 17:25:43 +02002279/* parse the "shards" bind keyword. Takes an integer, "by-thread", or "by-group" */
Willy Tarreau6dfbef42021-10-12 15:23:03 +02002280static int bind_parse_shards(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2281{
2282 int val;
2283
2284 if (!*args[cur_arg + 1]) {
2285 memprintf(err, "'%s' : missing value", args[cur_arg]);
2286 return ERR_ALERT | ERR_FATAL;
2287 }
2288
2289 if (strcmp(args[cur_arg + 1], "by-thread") == 0) {
Willy Tarreaud30e82b2023-04-13 17:11:23 +02002290 val = -1; /* -1 = "by-thread", will be fixed in check_config_validity() */
Willy Tarreaua07635e2023-04-13 17:25:43 +02002291 } else if (strcmp(args[cur_arg + 1], "by-group") == 0) {
2292 val = -2; /* -2 = "by-group", will be fixed in check_config_validity() */
Willy Tarreau6dfbef42021-10-12 15:23:03 +02002293 } else {
2294 val = atol(args[cur_arg + 1]);
2295 if (val < 1 || val > MAX_THREADS) {
2296 memprintf(err, "'%s' : invalid value %d, allowed range is %d..%d or 'by-thread'", args[cur_arg], val, 1, MAX_THREADS);
2297 return ERR_ALERT | ERR_FATAL;
2298 }
2299 }
2300
2301 conf->settings.shards = val;
2302 return 0;
2303}
2304
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01002305/* parse the "thread" bind keyword. This will replace any preset thread_set */
Willy Tarreauc8cac042021-09-21 14:31:29 +02002306static int bind_parse_thread(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2307{
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01002308 /* note that the thread set is zeroed before first call, and we don't
2309 * want to reset it so that it remains possible to chain multiple
2310 * "thread" directives.
2311 */
2312 if (parse_thread_set(args[cur_arg+1], &conf->thread_set, err) < 0)
Willy Tarreauc8cac042021-09-21 14:31:29 +02002313 return ERR_ALERT | ERR_FATAL;
Christopher Fauleta717b992018-04-10 14:43:00 +02002314 return 0;
2315}
2316
Willy Tarreau73101642023-04-22 22:06:23 +02002317/* config parser for global "tune.listener.default-shards" */
2318static int cfg_parse_tune_listener_shards(char **args, int section_type, struct proxy *curpx,
2319 const struct proxy *defpx, const char *file, int line,
2320 char **err)
2321{
2322 if (too_many_args(1, args, err, NULL))
2323 return -1;
2324
2325 if (strcmp(args[1], "by-thread") == 0)
2326 global.tune.default_shards = -1;
2327 else if (strcmp(args[1], "by-group") == 0)
2328 global.tune.default_shards = -2;
2329 else if (strcmp(args[1], "by-process") == 0)
2330 global.tune.default_shards = 1;
2331 else {
2332 memprintf(err, "'%s' expects either 'by-process', 'by-group', or 'by-thread' but got '%s'.", args[0], args[1]);
2333 return -1;
2334 }
2335 return 0;
2336}
2337
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002338/* config parser for global "tune.listener.multi-queue", accepts "on", "fair" or "off" */
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002339static int cfg_parse_tune_listener_mq(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01002340 const struct proxy *defpx, const char *file, int line,
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002341 char **err)
2342{
2343 if (too_many_args(1, args, err, NULL))
2344 return -1;
2345
2346 if (strcmp(args[1], "on") == 0)
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002347 global.tune.options = (global.tune.options & ~GTUNE_LISTENER_MQ_ANY) | GTUNE_LISTENER_MQ_OPT;
2348 else if (strcmp(args[1], "fair") == 0)
2349 global.tune.options = (global.tune.options & ~GTUNE_LISTENER_MQ_ANY) | GTUNE_LISTENER_MQ_FAIR;
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002350 else if (strcmp(args[1], "off") == 0)
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002351 global.tune.options &= ~GTUNE_LISTENER_MQ_ANY;
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002352 else {
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002353 memprintf(err, "'%s' expects either 'on', 'fair', or 'off' but got '%s'.", args[0], args[1]);
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002354 return -1;
2355 }
2356 return 0;
2357}
2358
Willy Tarreau61612d42012-04-19 18:42:05 +02002359/* Note: must not be declared <const> as its list will be overwritten.
2360 * Please take care of keeping this list alphabetically sorted.
2361 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002362static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002363 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
2364 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Jerome Magnineb421b22020-03-27 22:08:40 +01002365 { "so_name", smp_fetch_so_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002366 { /* END */ },
2367}};
2368
Willy Tarreau0108d902018-11-25 19:14:37 +01002369INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
2370
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002371/* Note: must not be declared <const> as its list will be overwritten.
2372 * Please take care of keeping this list alphabetically sorted.
2373 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002374static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002375 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002376}};
2377
Willy Tarreau0108d902018-11-25 19:14:37 +01002378INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
2379
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002380/* Note: must not be declared <const> as its list will be overwritten.
2381 * Please take care of keeping this list alphabetically sorted, doing so helps
2382 * all code contributors.
2383 * Optional keywords are also declared with a NULL ->parse() function so that
2384 * the config parser can report an appropriate error when a known keyword was
2385 * not enabled.
2386 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002387static struct bind_kw_list bind_kws = { "ALL", { }, {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002388 { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002389 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
2390 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
2391 { "id", bind_parse_id, 1 }, /* set id of listening socket */
2392 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
2393 { "name", bind_parse_name, 1 }, /* set name of listening socket */
2394 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02002395 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Christopher Fauleta717b992018-04-10 14:43:00 +02002396 { "proto", bind_parse_proto, 1 }, /* set the proto to use for all incoming connections */
Willy Tarreau6dfbef42021-10-12 15:23:03 +02002397 { "shards", bind_parse_shards, 1 }, /* set number of shards */
Willy Tarreauc8cac042021-09-21 14:31:29 +02002398 { "thread", bind_parse_thread, 1 }, /* set list of allowed threads for this socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002399 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002400}};
2401
Willy Tarreau0108d902018-11-25 19:14:37 +01002402INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
2403
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002404/* config keyword parsers */
2405static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau73101642023-04-22 22:06:23 +02002406 { CFG_GLOBAL, "tune.listener.default-shards", cfg_parse_tune_listener_shards },
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002407 { CFG_GLOBAL, "tune.listener.multi-queue", cfg_parse_tune_listener_mq },
2408 { 0, NULL, NULL }
2409}};
2410
2411INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2412
Willy Tarreau645513a2010-05-24 20:55:15 +02002413/*
2414 * Local variables:
2415 * c-indent-level: 8
2416 * c-basic-offset: 8
2417 * End:
2418 */