blob: b02fce5d3f62e450b9667ca3c1056e0fc5f8bbd0 [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>
Amaury Denoyelle8ee9fc72023-10-25 15:32:28 +020028#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020029#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020030#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020031#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020032#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/protocol.h>
Willy Tarreau5958c432021-05-08 20:30:37 +020034#include <haproxy/proxy.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020035#include <haproxy/quic_tp.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020038#include <haproxy/task.h>
Willy Tarreau9310f482021-10-06 16:18:40 +020039#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/tools.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020041
Willy Tarreaub648d632007-10-28 22:13:50 +010042
Willy Tarreau26982662012-09-12 23:17:10 +020043/* List head of all known bind keywords */
Willy Tarreauca1acd62022-03-29 15:02:44 +020044struct bind_kw_list bind_keywords = {
Willy Tarreau26982662012-09-12 23:17:10 +020045 .list = LIST_HEAD_INIT(bind_keywords.list)
46};
47
Willy Tarreaua1d97f82019-12-10 11:18:41 +010048/* list of the temporarily limited listeners because of lack of resource */
49static struct mt_list global_listener_queue = MT_LIST_HEAD_INIT(global_listener_queue);
50static struct task *global_listener_queue_task;
Willy Tarreau96151022023-05-11 13:51:31 +020051/* number of times an accepted connection resulted in maxconn being reached */
52ullong maxconn_reached = 0;
Willy Tarreau469fa472022-11-22 09:08:23 +010053__decl_thread(static HA_RWLOCK_T global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +010054
William Dauchy3679d0c2021-02-14 23:22:55 +010055/* listener status for stats */
56const char* li_status_st[LI_STATE_COUNT] = {
57 [LI_STATUS_WAITING] = "WAITING",
58 [LI_STATUS_OPEN] = "OPEN",
59 [LI_STATUS_FULL] = "FULL",
60};
Willy Tarreaua1d97f82019-12-10 11:18:41 +010061
Willy Tarreau1efafce2019-01-27 15:37:19 +010062#if defined(USE_THREAD)
63
64struct accept_queue_ring accept_queue_rings[MAX_THREADS] __attribute__((aligned(64))) = { };
65
66/* dequeue and process a pending connection from the local accept queue (single
Willy Tarreau83efc322020-10-14 17:37:17 +020067 * consumer). Returns the accepted connection or NULL if none was found.
Willy Tarreau1efafce2019-01-27 15:37:19 +010068 */
Willy Tarreau83efc322020-10-14 17:37:17 +020069struct connection *accept_queue_pop_sc(struct accept_queue_ring *ring)
Willy Tarreau1efafce2019-01-27 15:37:19 +010070{
Willy Tarreau1efafce2019-01-27 15:37:19 +010071 unsigned int pos, next;
Willy Tarreau83efc322020-10-14 17:37:17 +020072 struct connection *ptr;
73 struct connection **e;
Willy Tarreaue6f5ab52023-04-20 11:05:28 +020074 uint32_t idx = _HA_ATOMIC_LOAD(&ring->idx); /* (head << 16) + tail */
Willy Tarreau1efafce2019-01-27 15:37:19 +010075
Willy Tarreaue6f5ab52023-04-20 11:05:28 +020076 pos = idx >> 16;
77 if (pos == (uint16_t)idx)
Willy Tarreau83efc322020-10-14 17:37:17 +020078 return NULL;
Willy Tarreau1efafce2019-01-27 15:37:19 +010079
80 next = pos + 1;
81 if (next >= ACCEPT_QUEUE_SIZE)
82 next = 0;
83
84 e = &ring->entry[pos];
85
86 /* wait for the producer to update the listener's pointer */
87 while (1) {
Willy Tarreau83efc322020-10-14 17:37:17 +020088 ptr = *e;
Willy Tarreau1efafce2019-01-27 15:37:19 +010089 __ha_barrier_load();
90 if (ptr)
91 break;
92 pl_cpu_relax();
93 }
94
Willy Tarreau1efafce2019-01-27 15:37:19 +010095 /* release the entry */
Willy Tarreau83efc322020-10-14 17:37:17 +020096 *e = NULL;
Willy Tarreau1efafce2019-01-27 15:37:19 +010097
98 __ha_barrier_store();
Willy Tarreaue6f5ab52023-04-20 11:05:28 +020099 do {
100 pos = (next << 16) | (idx & 0xffff);
101 } while (unlikely(!HA_ATOMIC_CAS(&ring->idx, &idx, pos) && __ha_cpu_relax()));
102
Willy Tarreau83efc322020-10-14 17:37:17 +0200103 return ptr;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100104}
105
106
Willy Tarreau83efc322020-10-14 17:37:17 +0200107/* tries to push a new accepted connection <conn> into ring <ring>. Returns
108 * non-zero if it succeeds, or zero if the ring is full. Supports multiple
109 * producers.
Willy Tarreau1efafce2019-01-27 15:37:19 +0100110 */
Willy Tarreau83efc322020-10-14 17:37:17 +0200111int accept_queue_push_mp(struct accept_queue_ring *ring, struct connection *conn)
Willy Tarreau1efafce2019-01-27 15:37:19 +0100112{
Willy Tarreau1efafce2019-01-27 15:37:19 +0100113 unsigned int pos, next;
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200114 uint32_t idx = _HA_ATOMIC_LOAD(&ring->idx); /* (head << 16) + tail */
Willy Tarreau1efafce2019-01-27 15:37:19 +0100115
Willy Tarreau1efafce2019-01-27 15:37:19 +0100116 do {
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200117 pos = (uint16_t)idx;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100118 next = pos + 1;
119 if (next >= ACCEPT_QUEUE_SIZE)
120 next = 0;
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200121 if (next == (idx >> 16))
Willy Tarreau1efafce2019-01-27 15:37:19 +0100122 return 0; // ring full
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200123 next |= (idx & 0xffff0000U);
124 } while (unlikely(!_HA_ATOMIC_CAS(&ring->idx, &idx, next) && __ha_cpu_relax()));
Willy Tarreau1efafce2019-01-27 15:37:19 +0100125
Willy Tarreau83efc322020-10-14 17:37:17 +0200126 ring->entry[pos] = conn;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100127 __ha_barrier_store();
Willy Tarreau1efafce2019-01-27 15:37:19 +0100128 return 1;
129}
130
Willy Tarreaufb5401f2021-01-29 12:25:23 +0100131/* proceed with accepting new connections. Don't mark it static so that it appears
132 * in task dumps.
133 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100134struct task *accept_queue_process(struct task *t, void *context, unsigned int state)
Willy Tarreau1efafce2019-01-27 15:37:19 +0100135{
136 struct accept_queue_ring *ring = context;
Willy Tarreau83efc322020-10-14 17:37:17 +0200137 struct connection *conn;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100138 struct listener *li;
Christopher Faulet102854c2019-04-30 12:17:13 +0200139 unsigned int max_accept;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100140 int ret;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100141
Christopher Faulet102854c2019-04-30 12:17:13 +0200142 /* if global.tune.maxaccept is -1, then max_accept is UINT_MAX. It
143 * is not really illimited, but it is probably enough.
144 */
Willy Tarreau66161322021-02-19 15:50:27 +0100145 max_accept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Christopher Faulet102854c2019-04-30 12:17:13 +0200146 for (; max_accept; max_accept--) {
Willy Tarreau83efc322020-10-14 17:37:17 +0200147 conn = accept_queue_pop_sc(ring);
148 if (!conn)
Willy Tarreau1efafce2019-01-27 15:37:19 +0100149 break;
150
Willy Tarreau83efc322020-10-14 17:37:17 +0200151 li = __objt_listener(conn->target);
Willy Tarreaufea8c192023-02-28 10:25:57 +0100152 _HA_ATOMIC_INC(&li->thr_conn[ti->ltid]);
Willy Tarreau30836152023-01-12 19:10:17 +0100153 ret = li->bind_conf->accept(conn);
Willy Tarreau1efafce2019-01-27 15:37:19 +0100154 if (ret <= 0) {
155 /* connection was terminated by the application */
156 continue;
157 }
158
159 /* increase the per-process number of cumulated sessions, this
Willy Tarreau30836152023-01-12 19:10:17 +0100160 * may only be done once l->bind_conf->accept() has accepted the
161 * connection.
Willy Tarreau1efafce2019-01-27 15:37:19 +0100162 */
Willy Tarreau17146802023-01-12 19:58:42 +0100163 if (!(li->bind_conf->options & BC_O_UNLIMITED)) {
Willy Tarreau1efafce2019-01-27 15:37:19 +0100164 HA_ATOMIC_UPDATE_MAX(&global.sps_max,
165 update_freq_ctr(&global.sess_per_sec, 1));
Ilya Shipitsin83f54b92023-04-26 21:05:12 +0200166 if (li->bind_conf->options & BC_O_USE_SSL) {
Willy Tarreau1efafce2019-01-27 15:37:19 +0100167 HA_ATOMIC_UPDATE_MAX(&global.ssl_max,
168 update_freq_ctr(&global.ssl_per_sec, 1));
169 }
170 }
171 }
172
173 /* ran out of budget ? Let's come here ASAP */
Christopher Faulet102854c2019-04-30 12:17:13 +0200174 if (!max_accept)
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200175 tasklet_wakeup(ring->tasklet);
Willy Tarreau1efafce2019-01-27 15:37:19 +0100176
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200177 return NULL;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100178}
179
180/* Initializes the accept-queues. Returns 0 on success, otherwise ERR_* flags */
181static int accept_queue_init()
182{
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200183 struct tasklet *t;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100184 int i;
185
186 for (i = 0; i < global.nbthread; i++) {
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200187 t = tasklet_new();
Willy Tarreau1efafce2019-01-27 15:37:19 +0100188 if (!t) {
189 ha_alert("Out of memory while initializing accept queue for thread %d\n", i);
190 return ERR_FATAL|ERR_ABORT;
191 }
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200192 t->tid = i;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100193 t->process = accept_queue_process;
194 t->context = &accept_queue_rings[i];
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200195 accept_queue_rings[i].tasklet = t;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100196 }
197 return 0;
198}
199
200REGISTER_CONFIG_POSTPARSER("multi-threaded accept queue", accept_queue_init);
201
Willy Tarreaue01b08d2022-04-27 18:42:47 +0200202static void accept_queue_deinit()
203{
204 int i;
205
206 for (i = 0; i < global.nbthread; i++) {
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200207 tasklet_free(accept_queue_rings[i].tasklet);
Willy Tarreaue01b08d2022-04-27 18:42:47 +0200208 }
209}
210
211REGISTER_POST_DEINIT(accept_queue_deinit);
212
Willy Tarreau1efafce2019-01-27 15:37:19 +0100213#endif // USE_THREAD
214
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200215/* Memory allocation and initialization of the per_thr field (one entry per
216 * bound thread).
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100217 * Returns 0 if the field has been successfully initialized, -1 on failure.
218 */
219int li_init_per_thr(struct listener *li)
220{
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200221 int nbthr = MIN(global.nbthread, MAX_THREADS_PER_GROUP);
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100222 int i;
223
224 /* allocate per-thread elements for listener */
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200225 li->per_thr = calloc(nbthr, sizeof(*li->per_thr));
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100226 if (!li->per_thr)
227 return -1;
228
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200229 for (i = 0; i < nbthr; ++i) {
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100230 MT_LIST_INIT(&li->per_thr[i].quic_accept.list);
231 MT_LIST_INIT(&li->per_thr[i].quic_accept.conns);
232
233 li->per_thr[i].li = li;
234 }
235
236 return 0;
237}
238
William Dauchy3679d0c2021-02-14 23:22:55 +0100239/* helper to get listener status for stats */
240enum li_status get_li_status(struct listener *l)
241{
Willy Tarreau758c69d2023-01-12 18:59:37 +0100242 if (!l->bind_conf->maxconn || l->nbconn < l->bind_conf->maxconn) {
William Dauchy3679d0c2021-02-14 23:22:55 +0100243 if (l->state == LI_LIMITED)
244 return LI_STATUS_WAITING;
245 else
246 return LI_STATUS_OPEN;
247 }
248 return LI_STATUS_FULL;
249}
250
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200251/* adjust the listener's state and its proxy's listener counters if needed.
252 * It must be called under the listener's lock, but uses atomic ops to change
253 * the proxy's counters so that the proxy lock is not needed.
254 */
Willy Tarreaua37b2442020-09-24 07:23:45 +0200255void listener_set_state(struct listener *l, enum li_state st)
256{
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200257 struct proxy *px = l->bind_conf->frontend;
258
259 if (px) {
260 /* from state */
261 switch (l->state) {
262 case LI_NEW: /* first call */
Willy Tarreau4781b152021-04-06 13:53:36 +0200263 _HA_ATOMIC_INC(&px->li_all);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200264 break;
265 case LI_INIT:
266 case LI_ASSIGNED:
267 break;
268 case LI_PAUSED:
Willy Tarreau4781b152021-04-06 13:53:36 +0200269 _HA_ATOMIC_DEC(&px->li_paused);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200270 break;
271 case LI_LISTEN:
Willy Tarreau4781b152021-04-06 13:53:36 +0200272 _HA_ATOMIC_DEC(&px->li_bound);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200273 break;
274 case LI_READY:
275 case LI_FULL:
276 case LI_LIMITED:
Willy Tarreau4781b152021-04-06 13:53:36 +0200277 _HA_ATOMIC_DEC(&px->li_ready);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200278 break;
279 }
280
281 /* to state */
282 switch (st) {
283 case LI_NEW:
284 case LI_INIT:
285 case LI_ASSIGNED:
286 break;
287 case LI_PAUSED:
Willy Tarreau95a34602020-10-08 15:32:21 +0200288 BUG_ON(l->rx.fd == -1);
Willy Tarreau4781b152021-04-06 13:53:36 +0200289 _HA_ATOMIC_INC(&px->li_paused);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200290 break;
291 case LI_LISTEN:
Willy Tarreau95a34602020-10-08 15:32:21 +0200292 BUG_ON(l->rx.fd == -1);
Willy Tarreau4781b152021-04-06 13:53:36 +0200293 _HA_ATOMIC_INC(&px->li_bound);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200294 break;
295 case LI_READY:
296 case LI_FULL:
297 case LI_LIMITED:
Willy Tarreau95a34602020-10-08 15:32:21 +0200298 BUG_ON(l->rx.fd == -1);
Willy Tarreau4781b152021-04-06 13:53:36 +0200299 _HA_ATOMIC_INC(&px->li_ready);
Aurelien DARRAGON23705992023-02-14 08:51:14 +0100300 l->flags |= LI_F_FINALIZED;
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200301 break;
302 }
303 }
Willy Tarreaua37b2442020-09-24 07:23:45 +0200304 l->state = st;
305}
306
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100307/* This function adds the specified listener's file descriptor to the polling
308 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500309 * LI_FULL state depending on its number of connections. In daemon mode, we
Willy Tarreauae302532014-05-07 19:22:24 +0200310 * also support binding only the relevant processes to their respective
311 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100312 */
Willy Tarreau7834a3f2020-09-25 16:40:18 +0200313void enable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100314{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100315 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaud6afb532020-10-09 10:35:40 +0200316
317 /* If this listener is supposed to be only in the master, close it in
318 * the workers. Conversely, if it's supposed to be only in the workers
319 * close it in the master.
320 */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200321 if (!!master != !!(listener->rx.flags & RX_F_MWORKER))
Willy Tarreau75c98d12020-10-09 15:55:23 +0200322 do_unbind_listener(listener);
Willy Tarreaud6afb532020-10-09 10:35:40 +0200323
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100324 if (listener->state == LI_LISTEN) {
Willy Tarreau95a34602020-10-08 15:32:21 +0200325 BUG_ON(listener->rx.fd == -1);
William Lallemand095ba4c2017-06-01 17:38:50 +0200326 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau72faef32021-06-15 08:36:30 +0200327 (!!master != !!(listener->rx.flags & RX_F_MWORKER))) {
Willy Tarreauae302532014-05-07 19:22:24 +0200328 /* we don't want to enable this listener and don't
329 * want any fd event to reach it.
330 */
Willy Tarreau75c98d12020-10-09 15:55:23 +0200331 do_unbind_listener(listener);
Willy Tarreauae302532014-05-07 19:22:24 +0200332 }
Willy Tarreau758c69d2023-01-12 18:59:37 +0100333 else if (!listener->bind_conf->maxconn || listener->nbconn < listener->bind_conf->maxconn) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200334 listener->rx.proto->enable(listener);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200335 listener_set_state(listener, LI_READY);
Willy Tarreauae302532014-05-07 19:22:24 +0200336 }
337 else {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200338 listener_set_state(listener, LI_FULL);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100339 }
340 }
Willy Tarreaud6afb532020-10-09 10:35:40 +0200341
Willy Tarreau08b6f962022-02-01 16:23:00 +0100342 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100343}
344
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200345/*
Aurelien DARRAGON187396e2022-09-11 16:19:49 +0200346 * This function completely stops a listener.
347 * The proxy's listeners count is updated and the proxy is
348 * disabled and woken up after the last one is gone.
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100349 * It will need to operate under the proxy's lock, the protocol's lock and
350 * the listener's lock. The caller is responsible for indicating in lpx,
351 * lpr, lli whether the respective locks are already held (non-zero) or
352 * not (zero) so that the function picks the missing ones, in this order.
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200353 */
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100354void stop_listener(struct listener *l, int lpx, int lpr, int lli)
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200355{
356 struct proxy *px = l->bind_conf->frontend;
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200357
Willy Tarreau17146802023-01-12 19:58:42 +0100358 if (l->bind_conf->options & BC_O_NOSTOP) {
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200359 /* master-worker sockpairs are never closed but don't count as a
360 * job.
361 */
362 return;
363 }
364
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200365 if (!lpx && px)
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200366 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200367
368 if (!lpr)
369 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
370
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100371 if (!lli)
372 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200373
374 if (l->state > LI_INIT) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200375 do_unbind_listener(l);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200376
377 if (l->state >= LI_ASSIGNED)
378 __delete_listener(l);
379
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200380 if (px)
381 proxy_cond_disable(px);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200382 }
383
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100384 if (!lli)
385 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200386
387 if (!lpr)
388 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
389
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200390 if (!lpx && px)
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200391 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200392}
393
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100394/* This function adds the specified <listener> to the protocol <proto>. It
395 * does nothing if the protocol was already added. The listener's state is
396 * automatically updated from LI_INIT to LI_ASSIGNED. The number of listeners
397 * for the protocol is updated. This must be called with the proto lock held.
398 */
399void default_add_listener(struct protocol *proto, struct listener *listener)
400{
401 if (listener->state != LI_INIT)
402 return;
403 listener_set_state(listener, LI_ASSIGNED);
404 listener->rx.proto = proto;
Willy Tarreau2b718102021-04-21 07:32:39 +0200405 LIST_APPEND(&proto->receivers, &listener->rx.proto_list);
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100406 proto->nb_receivers++;
407}
408
Willy Tarreaue03204c2020-10-09 17:02:21 +0200409/* default function called to suspend a listener: it simply passes the call to
410 * the underlying receiver. This is find for most socket-based protocols. This
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100411 * must be called under the listener's lock. It will return < 0 in case of
412 * failure, 0 if the listener was totally stopped, or > 0 if correctly paused..
413 * If no receiver-level suspend is provided, the operation is assumed
414 * to succeed.
Willy Tarreaue03204c2020-10-09 17:02:21 +0200415 */
416int default_suspend_listener(struct listener *l)
417{
Willy Tarreaue03204c2020-10-09 17:02:21 +0200418 if (!l->rx.proto->rx_suspend)
419 return 1;
420
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100421 return l->rx.proto->rx_suspend(&l->rx);
Willy Tarreaue03204c2020-10-09 17:02:21 +0200422}
423
424
425/* Tries to resume a suspended listener, and returns non-zero on success or
426 * zero on failure. On certain errors, an alert or a warning might be displayed.
427 * It must be called with the listener's lock held. Depending on the listener's
428 * state and protocol, a listen() call might be used to resume operations, or a
429 * call to the receiver's resume() function might be used as well. This is
430 * suitable as a default function for TCP and UDP. This must be called with the
431 * listener's lock held.
432 */
433int default_resume_listener(struct listener *l)
434{
435 int ret = 1;
436
437 if (l->state == LI_ASSIGNED) {
438 char msg[100];
Aurelien DARRAGON046a75e2023-02-07 12:17:20 +0100439 char *errmsg;
Willy Tarreaue03204c2020-10-09 17:02:21 +0200440 int err;
441
Aurelien DARRAGON046a75e2023-02-07 12:17:20 +0100442 /* first, try to bind the receiver */
443 err = l->rx.proto->fam->bind(&l->rx, &errmsg);
444 if (err != ERR_NONE) {
445 if (err & ERR_WARN)
446 ha_warning("Resuming listener: %s\n", errmsg);
447 else if (err & ERR_ALERT)
448 ha_alert("Resuming listener: %s\n", errmsg);
449 ha_free(&errmsg);
450 if (err & (ERR_FATAL | ERR_ABORT)) {
451 ret = 0;
452 goto end;
453 }
454 }
455
456 /* then, try to listen:
457 * for now there's still always a listening function
458 * (same check performed in protocol_bind_all()
459 */
460 BUG_ON(!l->rx.proto->listen);
Willy Tarreaue03204c2020-10-09 17:02:21 +0200461 err = l->rx.proto->listen(l, msg, sizeof(msg));
462 if (err & ERR_ALERT)
463 ha_alert("Resuming listener: %s\n", msg);
464 else if (err & ERR_WARN)
465 ha_warning("Resuming listener: %s\n", msg);
466
467 if (err & (ERR_FATAL | ERR_ABORT)) {
468 ret = 0;
469 goto end;
470 }
471 }
472
473 if (l->state < LI_PAUSED) {
474 ret = 0;
475 goto end;
476 }
477
478 if (l->state == LI_PAUSED && l->rx.proto->rx_resume &&
479 l->rx.proto->rx_resume(&l->rx) <= 0)
480 ret = 0;
481 end:
482 return ret;
483}
484
485
Willy Tarreaube58c382011-07-24 18:28:10 +0200486/* This function tries to temporarily disable a listener, depending on the OS
487 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
488 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
489 * closes upon SHUT_WR and refuses to rebind. So a common validation path
490 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
491 * is disabled. It normally returns non-zero, unless an error is reported.
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100492 * suspend() may totally stop a listener if it doesn't support the PAUSED
493 * state, in which case state will be set to ASSIGNED.
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100494 * It will need to operate under the proxy's lock and the listener's lock.
495 * The caller is responsible for indicating in lpx, lli whether the respective
496 * locks are already held (non-zero) or not (zero) so that the function pick
497 * the missing ones, in this order.
Willy Tarreaube58c382011-07-24 18:28:10 +0200498 */
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100499int suspend_listener(struct listener *l, int lpx, int lli)
Willy Tarreaube58c382011-07-24 18:28:10 +0200500{
Willy Tarreau58651b42020-09-24 16:03:29 +0200501 struct proxy *px = l->bind_conf->frontend;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200502 int ret = 1;
503
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200504 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200505 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
506
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100507 if (!lli)
508 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200509
Aurelien DARRAGON23705992023-02-14 08:51:14 +0100510 if (!(l->flags & LI_F_FINALIZED) || l->state <= LI_PAUSED)
Willy Tarreau9b3a9322020-09-24 14:46:34 +0200511 goto end;
512
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100513 if (l->rx.proto->suspend) {
Willy Tarreaue03204c2020-10-09 17:02:21 +0200514 ret = l->rx.proto->suspend(l);
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100515 /* if the suspend() fails, we don't want to change the
516 * current listener state
517 */
518 if (ret < 0)
519 goto end;
520 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200521
Willy Tarreau2b718102021-04-21 07:32:39 +0200522 MT_LIST_DELETE(&l->wait_queue);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200523
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100524 /* ret == 0 means that the suspend() has been turned into
525 * an unbind(), meaning the listener is now stopped (ie: ABNS), we need
526 * to report this state change properly
527 */
528 listener_set_state(l, ((ret) ? LI_PAUSED : LI_ASSIGNED));
529
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100530 if (px && !(l->flags & LI_F_SUSPENDED))
531 px->li_suspended++;
532 l->flags |= LI_F_SUSPENDED;
533
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100534 /* at this point, everything is under control, no error should be
535 * returned to calling function
536 */
537 ret = 1;
Willy Tarreau58651b42020-09-24 16:03:29 +0200538
Aurelien DARRAGONca8a4b22023-02-07 12:36:27 +0100539 if (px && !(px->flags & PR_FL_PAUSED) && !px->li_ready) {
Aurelien DARRAGONd46f4372022-09-09 15:51:37 +0200540 /* PROXY_LOCK is required */
541 proxy_cond_pause(px);
Willy Tarreau58651b42020-09-24 16:03:29 +0200542 ha_warning("Paused %s %s.\n", proxy_cap_str(px->cap), px->id);
543 send_log(px, LOG_WARNING, "Paused %s %s.\n", proxy_cap_str(px->cap), px->id);
544 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200545 end:
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100546 if (!lli)
547 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200548
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200549 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200550 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
551
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200552 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200553}
554
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200555/* This function tries to resume a temporarily disabled listener. Paused, full,
556 * limited and disabled listeners are handled, which means that this function
557 * may replace enable_listener(). The resulting state will either be LI_READY
558 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200559 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200560 * foreground mode, and are ignored. If the listener was only in the assigned
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100561 * state, it's totally rebound. This can happen if a suspend() has completely
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200562 * stopped it. If the resume fails, 0 is returned and an error might be
563 * displayed.
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100564 * It will need to operate under the proxy's lock and the listener's lock.
565 * The caller is responsible for indicating in lpx, lli whether the respective
566 * locks are already held (non-zero) or not (zero) so that the function pick
567 * the missing ones, in this order.
Willy Tarreaube58c382011-07-24 18:28:10 +0200568 */
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100569int resume_listener(struct listener *l, int lpx, int lli)
Willy Tarreaube58c382011-07-24 18:28:10 +0200570{
Willy Tarreau58651b42020-09-24 16:03:29 +0200571 struct proxy *px = l->bind_conf->frontend;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200572 int ret = 1;
573
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200574 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200575 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
576
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100577 if (!lli)
578 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200579
Willy Tarreauf2cb1692019-07-11 10:08:31 +0200580 /* check that another thread didn't to the job in parallel (e.g. at the
581 * end of listen_accept() while we'd come from dequeue_all_listeners().
582 */
Willy Tarreau2b718102021-04-21 07:32:39 +0200583 if (MT_LIST_INLIST(&l->wait_queue))
Willy Tarreauf2cb1692019-07-11 10:08:31 +0200584 goto end;
585
Aurelien DARRAGON23705992023-02-14 08:51:14 +0100586 if (!(l->flags & LI_F_FINALIZED) || l->state == LI_READY)
Willy Tarreau5d7f9ce2020-09-24 18:54:11 +0200587 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200588
Aurelien DARRAGON3bb2a382023-02-07 13:26:14 +0100589 if (l->rx.proto->resume) {
Willy Tarreaue03204c2020-10-09 17:02:21 +0200590 ret = l->rx.proto->resume(l);
Aurelien DARRAGON3bb2a382023-02-07 13:26:14 +0100591 if (!ret)
592 goto end; /* failure to resume */
593 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200594
Willy Tarreau758c69d2023-01-12 18:59:37 +0100595 if (l->bind_conf->maxconn && l->nbconn >= l->bind_conf->maxconn) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200596 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200597 listener_set_state(l, LI_FULL);
Willy Tarreau58651b42020-09-24 16:03:29 +0200598 goto done;
Willy Tarreaube58c382011-07-24 18:28:10 +0200599 }
600
Willy Tarreau4b51f422020-09-25 20:32:28 +0200601 l->rx.proto->enable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200602 listener_set_state(l, LI_READY);
Willy Tarreau58651b42020-09-24 16:03:29 +0200603
604 done:
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100605 if (px && (l->flags & LI_F_SUSPENDED))
606 px->li_suspended--;
607 l->flags &= ~LI_F_SUSPENDED;
608
Aurelien DARRAGONca8a4b22023-02-07 12:36:27 +0100609 if (px && (px->flags & PR_FL_PAUSED) && !px->li_suspended) {
Aurelien DARRAGONd46f4372022-09-09 15:51:37 +0200610 /* PROXY_LOCK is required */
611 proxy_cond_resume(px);
Willy Tarreau58651b42020-09-24 16:03:29 +0200612 ha_warning("Resumed %s %s.\n", proxy_cap_str(px->cap), px->id);
613 send_log(px, LOG_WARNING, "Resumed %s %s.\n", proxy_cap_str(px->cap), px->id);
614 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200615 end:
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100616 if (!lli)
617 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200618
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200619 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200620 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
621
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200622 return ret;
623}
624
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100625/* Same as resume_listener(), but will only work to resume from
626 * LI_FULL or LI_LIMITED states because we try to relax listeners that
627 * were temporarily restricted and not to resume inactive listeners that
628 * may have been paused or completely stopped in the meantime.
629 * Returns positive value for success and 0 for failure.
630 * It will need to operate under the proxy's lock and the listener's lock.
631 * The caller is responsible for indicating in lpx, lli whether the respective
632 * locks are already held (non-zero) or not (zero) so that the function pick
633 * the missing ones, in this order.
634 */
635int relax_listener(struct listener *l, int lpx, int lli)
636{
Christopher Faulet6844af62023-07-20 14:53:50 +0200637 struct proxy *px = l->bind_conf->frontend;
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100638 int ret = 1;
639
Christopher Faulet6844af62023-07-20 14:53:50 +0200640 if (!lpx && px)
641 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
642
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100643 if (!lli)
644 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
645
646 if (l->state != LI_FULL && l->state != LI_LIMITED)
647 goto end; /* listener may be suspended or even stopped */
Christopher Faulet6844af62023-07-20 14:53:50 +0200648 ret = resume_listener(l, 1, 1);
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100649
650 end:
651 if (!lli)
652 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet6844af62023-07-20 14:53:50 +0200653
654 if (!lpx && px)
655 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
656
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100657 return ret;
658}
659
Willy Tarreau87b09662015-04-03 00:22:06 +0200660/* Marks a ready listener as full so that the stream code tries to re-enable
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +0100661 * it upon next close() using relax_listener().
Willy Tarreau62793712011-07-24 19:23:38 +0200662 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200663static void listener_full(struct listener *l)
Willy Tarreau62793712011-07-24 19:23:38 +0200664{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100665 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200666 if (l->state >= LI_READY) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200667 MT_LIST_DELETE(&l->wait_queue);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100668 if (l->state != LI_FULL) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200669 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200670 listener_set_state(l, LI_FULL);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100671 }
Willy Tarreau62793712011-07-24 19:23:38 +0200672 }
Willy Tarreau08b6f962022-02-01 16:23:00 +0100673 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200674}
675
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200676/* Marks a ready listener as limited so that we only try to re-enable it when
677 * resources are free again. It will be queued into the specified queue.
678 */
Olivier Houchard859dc802019-08-08 15:47:21 +0200679static void limit_listener(struct listener *l, struct mt_list *list)
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200680{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100681 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200682 if (l->state == LI_READY) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200683 MT_LIST_TRY_APPEND(list, &l->wait_queue);
Willy Tarreau4b51f422020-09-25 20:32:28 +0200684 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200685 listener_set_state(l, LI_LIMITED);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200686 }
Willy Tarreau08b6f962022-02-01 16:23:00 +0100687 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200688}
689
Willy Tarreau241797a2019-12-10 14:10:52 +0100690/* Dequeues all listeners waiting for a resource the global wait queue */
691void dequeue_all_listeners()
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200692{
Willy Tarreau01abd022019-02-28 10:27:18 +0100693 struct listener *listener;
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200694
Willy Tarreau241797a2019-12-10 14:10:52 +0100695 while ((listener = MT_LIST_POP(&global_listener_queue, struct listener *, wait_queue))) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200696 /* This cannot fail because the listeners are by definition in
Willy Tarreau01abd022019-02-28 10:27:18 +0100697 * the LI_LIMITED state.
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200698 */
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +0100699 relax_listener(listener, 0, 0);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200700 }
701}
702
Willy Tarreau241797a2019-12-10 14:10:52 +0100703/* Dequeues all listeners waiting for a resource in proxy <px>'s queue */
704void dequeue_proxy_listeners(struct proxy *px)
705{
706 struct listener *listener;
707
708 while ((listener = MT_LIST_POP(&px->listener_queue, struct listener *, wait_queue))) {
709 /* This cannot fail because the listeners are by definition in
710 * the LI_LIMITED state.
711 */
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +0100712 relax_listener(listener, 0, 0);
Willy Tarreau241797a2019-12-10 14:10:52 +0100713 }
714}
715
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200716
717/* default function used to unbind a listener. This is for use by standard
718 * protocols working on top of accepted sockets. The receiver's rx_unbind()
719 * will automatically be used after the listener is disabled if the socket is
720 * still bound. This must be used under the listener's lock.
Christopher Faulet510c0d62018-03-16 10:04:47 +0100721 */
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200722void default_unbind_listener(struct listener *listener)
Willy Tarreaub648d632007-10-28 22:13:50 +0100723{
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200724 if (listener->state <= LI_ASSIGNED)
725 goto out_close;
726
727 if (listener->rx.fd == -1) {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200728 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200729 goto out_close;
730 }
731
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200732 if (listener->state >= LI_READY) {
733 listener->rx.proto->disable(listener);
734 if (listener->rx.flags & RX_F_BOUND)
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200735 listener_set_state(listener, LI_LISTEN);
Willy Tarreaub6607bf2020-09-23 16:24:23 +0200736 }
737
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200738 out_close:
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200739 if (listener->rx.flags & RX_F_BOUND)
740 listener->rx.proto->rx_unbind(&listener->rx);
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200741}
742
743/* This function closes the listening socket for the specified listener,
744 * provided that it's already in a listening state. The protocol's unbind()
745 * is called to put the listener into LI_ASSIGNED or LI_LISTEN and handle
746 * the unbinding tasks. The listener enters then the LI_ASSIGNED state if
747 * the receiver is unbound. Must be called with the lock held.
748 */
749void do_unbind_listener(struct listener *listener)
750{
Willy Tarreau2b718102021-04-21 07:32:39 +0200751 MT_LIST_DELETE(&listener->wait_queue);
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200752
753 if (listener->rx.proto->unbind)
754 listener->rx.proto->unbind(listener);
Willy Tarreau374e9af2020-10-09 15:47:17 +0200755
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200756 /* we may have to downgrade the listener if the rx was closed */
757 if (!(listener->rx.flags & RX_F_BOUND) && listener->state > LI_ASSIGNED)
Willy Tarreau374e9af2020-10-09 15:47:17 +0200758 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100759}
760
Olivier Houchard1fc05162017-04-06 01:05:05 +0200761/* This function closes the listening socket for the specified listener,
762 * provided that it's already in a listening state. The listener enters the
Willy Tarreau75c98d12020-10-09 15:55:23 +0200763 * LI_ASSIGNED state, except if the FD is not closed, in which case it may
764 * remain in LI_LISTEN. This function is intended to be used as a generic
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100765 * function for standard protocols.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200766 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100767void unbind_listener(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200768{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100769 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau75c98d12020-10-09 15:55:23 +0200770 do_unbind_listener(listener);
Willy Tarreau08b6f962022-02-01 16:23:00 +0100771 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200772}
773
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200774/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
775 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200776 * allocation). The address family is taken from ss->ss_family, and the protocol
Willy Tarreaud2fb99f2020-10-15 21:22:29 +0200777 * passed in <proto> must be usable on this family. The protocol's default iocb
778 * is automatically preset as the receivers' iocb. The number of jobs and
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200779 * listeners is automatically increased by the number of listeners created. It
780 * returns non-zero on success, zero on error with the error message set in <err>.
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200781 */
782int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200783 int portl, int porth, int fd, struct protocol *proto, char **err)
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200784{
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200785 struct listener *l;
786 int port;
787
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200788 for (port = portl; port <= porth; port++) {
789 l = calloc(1, sizeof(*l));
790 if (!l) {
791 memprintf(err, "out of memory");
792 return 0;
793 }
794 l->obj_type = OBJ_TYPE_LISTENER;
Willy Tarreau2b718102021-04-21 07:32:39 +0200795 LIST_APPEND(&bc->frontend->conf.listeners, &l->by_fe);
796 LIST_APPEND(&bc->listeners, &l->by_bind);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200797 l->bind_conf = bc;
Willy Tarreau0fce6bc2020-09-03 07:46:06 +0200798 l->rx.settings = &bc->settings;
Willy Tarreaueef45422020-09-03 10:05:03 +0200799 l->rx.owner = l;
Willy Tarreaud2fb99f2020-10-15 21:22:29 +0200800 l->rx.iocb = proto->default_iocb;
Willy Tarreau38ba6472020-08-27 08:16:52 +0200801 l->rx.fd = fd;
Willy Tarreau07400c52020-12-04 14:49:11 +0100802
Willy Tarreau37159062020-08-27 07:48:42 +0200803 memcpy(&l->rx.addr, ss, sizeof(*ss));
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100804 if (proto->fam->set_port)
805 proto->fam->set_port(&l->rx.addr, port);
Willy Tarreau07400c52020-12-04 14:49:11 +0100806
Olivier Houchard859dc802019-08-08 15:47:21 +0200807 MT_LIST_INIT(&l->wait_queue);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200808 listener_set_state(l, LI_INIT);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200809
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100810 proto->add(proto, l);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200811
Willy Tarreau909c23b2020-09-15 13:50:58 +0200812 if (fd != -1)
Willy Tarreau43046fa2020-09-01 15:41:59 +0200813 l->rx.flags |= RX_F_INHERITED;
William Lallemand75ea0a02017-11-15 19:02:58 +0100814
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100815 l->extra_counters = NULL;
816
Willy Tarreau08b6f962022-02-01 16:23:00 +0100817 HA_RWLOCK_INIT(&l->lock);
Willy Tarreau4781b152021-04-06 13:53:36 +0200818 _HA_ATOMIC_INC(&jobs);
819 _HA_ATOMIC_INC(&listeners);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200820 }
821 return 1;
822}
823
Willy Tarreauaae18102023-03-01 18:25:58 +0100824/* Optionally allocates a new shard info (if si == NULL) for receiver rx and
825 * assigns it to it, or attaches to an existing one. If the rx already had a
826 * shard_info, it is simply returned. It is illegal to call this function with
827 * an rx that's part of a group that is already attached. Attaching means the
828 * shard_info's thread count and group count are updated so the rx's group is
829 * added to the shard_info's group mask. The rx are added to the members in the
830 * attachment order, though it must not matter. It is meant for boot time setup
831 * and is not thread safe. NULL is returned on allocation failure.
832 */
833struct shard_info *shard_info_attach(struct receiver *rx, struct shard_info *si)
834{
835 if (rx->shard_info)
836 return rx->shard_info;
837
838 if (!si) {
839 si = calloc(1, sizeof(*si));
840 if (!si)
841 return NULL;
842
843 si->ref = rx;
844 }
845
846 rx->shard_info = si;
847 BUG_ON (si->tgroup_mask & 1UL << (rx->bind_tgroup - 1));
848 si->tgroup_mask |= 1UL << (rx->bind_tgroup - 1);
849 si->nbgroups = my_popcountl(si->tgroup_mask);
850 si->nbthreads += my_popcountl(rx->bind_thread);
851 si->members[si->nbgroups - 1] = rx;
852 return si;
853}
854
855/* Detaches the rx from an optional shard_info it may be attached to. If so,
856 * the thread counts, group masks and refcounts are updated. The members list
857 * remains contiguous by replacing the current entry with the last one. The
858 * reference continues to point to the first receiver. If the group count
859 * reaches zero, the shard_info is automatically released.
860 */
861void shard_info_detach(struct receiver *rx)
862{
863 struct shard_info *si = rx->shard_info;
864 uint gr;
865
866 if (!si)
867 return;
868
869 rx->shard_info = NULL;
870
871 /* find the member slot this rx was attached to */
872 for (gr = 0; gr < MAX_TGROUPS && si->members[gr] != rx; gr++)
873 ;
874
875 BUG_ON(gr == MAX_TGROUPS);
876
877 si->nbthreads -= my_popcountl(rx->bind_thread);
878 si->tgroup_mask &= ~(1UL << (rx->bind_tgroup - 1));
879 si->nbgroups = my_popcountl(si->tgroup_mask);
880
881 /* replace the member by the last one. If we removed the reference, we
882 * have to switch to another one. It's always the first entry so we can
883 * simply enforce it upon every removal.
884 */
885 si->members[gr] = si->members[si->nbgroups];
886 si->members[si->nbgroups] = NULL;
887 si->ref = si->members[0];
888
889 if (!si->nbgroups)
890 free(si);
891}
892
Willy Tarreau59a877d2021-10-12 09:36:10 +0200893/* clones listener <src> and returns the new one. All dynamically allocated
894 * fields are reallocated (name for now). The new listener is inserted before
895 * the original one in the bind_conf and frontend lists. This allows it to be
896 * duplicated while iterating over the current list. The original listener must
897 * only be in the INIT or ASSIGNED states, and the new listener will only be
898 * placed into the INIT state. The counters are always set to NULL. Maxsock is
Willy Tarreauaae18102023-03-01 18:25:58 +0100899 * updated. Returns NULL on allocation error. The shard_info is never taken so
900 * that the caller can decide what to do with it depending on how it intends to
901 * clone the listener.
Willy Tarreau59a877d2021-10-12 09:36:10 +0200902 */
903struct listener *clone_listener(struct listener *src)
904{
905 struct listener *l;
906
907 l = calloc(1, sizeof(*l));
908 if (!l)
909 goto oom1;
910 memcpy(l, src, sizeof(*l));
911
Willy Tarreaue2348bd2024-04-09 08:41:06 +0200912 l->luid = 0; // don't dup the listener's ID!
Willy Tarreau59a877d2021-10-12 09:36:10 +0200913 if (l->name) {
914 l->name = strdup(l->name);
915 if (!l->name)
916 goto oom2;
917 }
918
919 l->rx.owner = l;
Willy Tarreauaae18102023-03-01 18:25:58 +0100920 l->rx.shard_info = NULL;
Willy Tarreau59a877d2021-10-12 09:36:10 +0200921 l->state = LI_INIT;
922 l->counters = NULL;
923 l->extra_counters = NULL;
924
925 LIST_APPEND(&src->by_fe, &l->by_fe);
926 LIST_APPEND(&src->by_bind, &l->by_bind);
927
928 MT_LIST_INIT(&l->wait_queue);
929
930 l->rx.proto->add(l->rx.proto, l);
931
Willy Tarreau08b6f962022-02-01 16:23:00 +0100932 HA_RWLOCK_INIT(&l->lock);
Willy Tarreau59a877d2021-10-12 09:36:10 +0200933 _HA_ATOMIC_INC(&jobs);
934 _HA_ATOMIC_INC(&listeners);
935 global.maxsock++;
936 return l;
937
Willy Tarreau59a877d2021-10-12 09:36:10 +0200938 oom2:
939 free(l);
940 oom1:
Willy Tarreaua1462892021-10-16 14:45:29 +0200941 return NULL;
Willy Tarreau59a877d2021-10-12 09:36:10 +0200942}
943
Willy Tarreau1a64d162007-10-28 22:26:05 +0100944/* Delete a listener from its protocol's list of listeners. The listener's
945 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
Willy Tarreau2cc5bae2017-09-15 08:18:11 +0200946 * number of listeners is updated, as well as the global number of listeners
947 * and jobs. Note that the listener must have previously been unbound. This
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200948 * is a low-level function expected to be called with the proto_lock and the
949 * listener's lock held.
Willy Tarreau1a64d162007-10-28 22:26:05 +0100950 */
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200951void __delete_listener(struct listener *listener)
Willy Tarreau1a64d162007-10-28 22:26:05 +0100952{
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100953 if (listener->state == LI_ASSIGNED) {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200954 listener_set_state(listener, LI_INIT);
Willy Tarreau2b718102021-04-21 07:32:39 +0200955 LIST_DELETE(&listener->rx.proto_list);
Willy Tarreauaae18102023-03-01 18:25:58 +0100956 shard_info_detach(&listener->rx);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200957 listener->rx.proto->nb_receivers--;
Willy Tarreau4781b152021-04-06 13:53:36 +0200958 _HA_ATOMIC_DEC(&jobs);
959 _HA_ATOMIC_DEC(&listeners);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100960 }
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200961}
962
963/* Delete a listener from its protocol's list of listeners (please check
964 * __delete_listener() above). The proto_lock and the listener's lock will
965 * be grabbed in this order.
966 */
967void delete_listener(struct listener *listener)
968{
969 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau08b6f962022-02-01 16:23:00 +0100970 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200971 __delete_listener(listener);
Willy Tarreau08b6f962022-02-01 16:23:00 +0100972 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau6ee9f8d2019-08-26 10:55:52 +0200973 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100974}
975
Willy Tarreaue2711c72019-02-27 15:39:41 +0100976/* Returns a suitable value for a listener's backlog. It uses the listener's,
977 * otherwise the frontend's backlog, otherwise the listener's maxconn,
978 * otherwise the frontend's maxconn, otherwise 1024.
979 */
980int listener_backlog(const struct listener *l)
981{
Willy Tarreau1920f892023-01-12 18:55:13 +0100982 if (l->bind_conf->backlog)
983 return l->bind_conf->backlog;
Willy Tarreaue2711c72019-02-27 15:39:41 +0100984
985 if (l->bind_conf->frontend->backlog)
986 return l->bind_conf->frontend->backlog;
987
Willy Tarreau758c69d2023-01-12 18:59:37 +0100988 if (l->bind_conf->maxconn)
989 return l->bind_conf->maxconn;
Willy Tarreaue2711c72019-02-27 15:39:41 +0100990
991 if (l->bind_conf->frontend->maxconn)
992 return l->bind_conf->frontend->maxconn;
993
994 return 1024;
995}
996
Amaury Denoyelle331b8b12023-10-25 10:52:23 +0200997/* Returns true if listener <l> must check maxconn limit prior to accept. */
998static inline int listener_uses_maxconn(const struct listener *l)
999{
1000 return !(l->bind_conf->options & (BC_O_UNLIMITED|BC_O_XPRT_MAXCONN));
1001}
1002
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001003/* This function is called on a read event from a listening socket, corresponding
1004 * to an accept. It tries to accept as many connections as possible, and for each
1005 * calls the listener's accept handler (generally the frontend's accept handler).
1006 */
Willy Tarreaua74cb382020-10-15 21:29:49 +02001007void listener_accept(struct listener *l)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001008{
Willy Tarreau83efc322020-10-14 17:37:17 +02001009 struct connection *cli_conn;
Olivier Houchardd16a9df2019-02-25 16:18:16 +01001010 struct proxy *p;
Christopher Faulet102854c2019-04-30 12:17:13 +02001011 unsigned int max_accept;
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001012 int next_conn = 0;
Willy Tarreau82c97892019-02-27 19:32:32 +01001013 int next_feconn = 0;
1014 int next_actconn = 0;
Willy Tarreaubb660302014-05-07 19:47:02 +02001015 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001016 int ret;
1017
Olivier Houchardd16a9df2019-02-25 16:18:16 +01001018 p = l->bind_conf->frontend;
Christopher Faulet102854c2019-04-30 12:17:13 +02001019
Willy Tarreau882f2482023-01-12 18:52:23 +01001020 /* if l->bind_conf->maxaccept is -1, then max_accept is UINT_MAX. It is
1021 * not really illimited, but it is probably enough.
Christopher Faulet102854c2019-04-30 12:17:13 +02001022 */
Willy Tarreau882f2482023-01-12 18:52:23 +01001023 max_accept = l->bind_conf->maxaccept ? l->bind_conf->maxaccept : 1;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001024
Willy Tarreau17146802023-01-12 19:58:42 +01001025 if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.sps_lim) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001026 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +02001027
1028 if (unlikely(!max)) {
1029 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +02001030 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +01001031 goto limit_global;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001032 }
1033
1034 if (max_accept > max)
1035 max_accept = max;
1036 }
1037
Willy Tarreau17146802023-01-12 19:58:42 +01001038 if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001039 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
1040
1041 if (unlikely(!max)) {
1042 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +02001043 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +01001044 goto limit_global;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001045 }
1046
1047 if (max_accept > max)
1048 max_accept = max;
1049 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001050#ifdef USE_OPENSSL
Willy Tarreau17146802023-01-12 19:58:42 +01001051 if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.ssl_lim &&
Willy Tarreau11ba4042022-05-20 15:56:32 +02001052 l->bind_conf && l->bind_conf->options & BC_O_USE_SSL) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001053 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001054
Willy Tarreaue43d5322013-10-07 20:01:52 +02001055 if (unlikely(!max)) {
1056 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +02001057 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +01001058 goto limit_global;
Willy Tarreaue43d5322013-10-07 20:01:52 +02001059 }
1060
1061 if (max_accept > max)
1062 max_accept = max;
1063 }
1064#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001065 if (p && p->fe_sps_lim) {
1066 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
1067
1068 if (unlikely(!max)) {
1069 /* frontend accept rate limit was reached */
Willy Tarreau0591bf72019-12-10 12:01:21 +01001070 expire = tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0));
1071 goto limit_proxy;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001072 }
1073
1074 if (max_accept > max)
1075 max_accept = max;
1076 }
1077
1078 /* Note: if we fail to allocate a connection because of configured
1079 * limits, we'll schedule a new attempt worst 1 second later in the
1080 * worst case. If we fail due to system limits or temporary resource
1081 * shortage, we try again 100ms later in the worst case.
1082 */
Willy Tarreau02757d02021-01-28 18:07:24 +01001083 for (; max_accept; next_conn = next_feconn = next_actconn = 0, max_accept--) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001084 unsigned int count;
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001085 int status;
Willy Tarreau0aa5a5b2020-10-16 17:43:04 +02001086 __decl_thread(unsigned long mask);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001087
Willy Tarreau82c97892019-02-27 19:32:32 +01001088 /* pre-increase the number of connections without going too far.
1089 * We process the listener, then the proxy, then the process.
1090 * We know which ones to unroll based on the next_xxx value.
1091 */
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001092 do {
1093 count = l->nbconn;
Willy Tarreau758c69d2023-01-12 18:59:37 +01001094 if (unlikely(l->bind_conf->maxconn && count >= l->bind_conf->maxconn)) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001095 /* the listener was marked full or another
1096 * thread is going to do it.
1097 */
1098 next_conn = 0;
Willy Tarreau93604ed2019-11-15 10:20:07 +01001099 listener_full(l);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001100 goto end;
1101 }
1102 next_conn = count + 1;
David Carlier56716622019-03-27 16:08:42 +00001103 } while (!_HA_ATOMIC_CAS(&l->nbconn, (int *)(&count), next_conn));
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001104
Willy Tarreau82c97892019-02-27 19:32:32 +01001105 if (p) {
1106 do {
1107 count = p->feconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +01001108 if (unlikely(count >= p->maxconn)) {
Willy Tarreau82c97892019-02-27 19:32:32 +01001109 /* the frontend was marked full or another
1110 * thread is going to do it.
1111 */
1112 next_feconn = 0;
Willy Tarreau0591bf72019-12-10 12:01:21 +01001113 expire = TICK_ETERNITY;
1114 goto limit_proxy;
Willy Tarreau82c97892019-02-27 19:32:32 +01001115 }
1116 next_feconn = count + 1;
Olivier Houchard64213e92019-03-08 18:52:57 +01001117 } while (!_HA_ATOMIC_CAS(&p->feconn, &count, next_feconn));
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001118 }
1119
Amaury Denoyelle331b8b12023-10-25 10:52:23 +02001120 if (listener_uses_maxconn(l)) {
Amaury Denoyelle8ee9fc72023-10-25 15:32:28 +02001121 next_actconn = increment_actconn();
1122 if (!next_actconn) {
1123 /* the process was marked full or another
1124 * thread is going to do it.
1125 */
1126 expire = tick_add(now_ms, 1000); /* try again in 1 second */
1127 goto limit_global;
1128 }
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001129 }
1130
Willy Tarreaufed93d32022-02-01 16:37:00 +01001131 /* be careful below, the listener might be shutting down in
1132 * another thread on error and we must not dereference its
1133 * FD without a bit of protection.
1134 */
1135 cli_conn = NULL;
1136 status = CO_AC_PERMERR;
1137
1138 HA_RWLOCK_RDLOCK(LISTENER_LOCK, &l->lock);
1139 if (l->rx.flags & RX_F_BOUND)
1140 cli_conn = l->rx.proto->accept_conn(l, &status);
1141 HA_RWLOCK_RDUNLOCK(LISTENER_LOCK, &l->lock);
1142
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001143 if (!cli_conn) {
1144 switch (status) {
1145 case CO_AC_DONE:
1146 goto end;
Willy Tarreau818dca52014-01-31 19:40:19 +01001147
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001148 case CO_AC_RETRY: /* likely a signal */
Willy Tarreau4781b152021-04-06 13:53:36 +02001149 _HA_ATOMIC_DEC(&l->nbconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001150 if (p)
Willy Tarreau4781b152021-04-06 13:53:36 +02001151 _HA_ATOMIC_DEC(&p->feconn);
Amaury Denoyelle331b8b12023-10-25 10:52:23 +02001152 if (listener_uses_maxconn(l))
Willy Tarreau4781b152021-04-06 13:53:36 +02001153 _HA_ATOMIC_DEC(&actconn);
Willy Tarreaua593ec52014-01-20 21:21:30 +01001154 continue;
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001155
1156 case CO_AC_YIELD:
Willy Tarreau92079932019-12-10 09:30:05 +01001157 max_accept = 0;
1158 goto end;
William Lallemandd9138002018-11-27 12:02:39 +01001159
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001160 default:
1161 goto transient_error;
Willy Tarreau83efc322020-10-14 17:37:17 +02001162 }
1163 }
1164
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001165 /* The connection was accepted, it must be counted as such */
1166 if (l->counters)
1167 HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, next_conn);
1168
Willy Tarreaud8679342022-05-09 20:41:54 +02001169 if (p) {
Willy Tarreau82c97892019-02-27 19:32:32 +01001170 HA_ATOMIC_UPDATE_MAX(&p->fe_counters.conn_max, next_feconn);
Willy Tarreaud8679342022-05-09 20:41:54 +02001171 proxy_inc_fe_conn_ctr(l, p);
1172 }
Willy Tarreau82c97892019-02-27 19:32:32 +01001173
Willy Tarreau17146802023-01-12 19:58:42 +01001174 if (!(l->bind_conf->options & BC_O_UNLIMITED)) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001175 count = update_freq_ctr(&global.conn_per_sec, 1);
1176 HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001177 }
1178
Willy Tarreau4781b152021-04-06 13:53:36 +02001179 _HA_ATOMIC_INC(&activity[tid].accepted);
Willy Tarreau64a9c052019-04-12 15:27:17 +02001180
Willy Tarreau96151022023-05-11 13:51:31 +02001181 /* count the number of times an accepted connection resulted in
1182 * maxconn being reached.
1183 */
1184 if (unlikely(_HA_ATOMIC_LOAD(&actconn) + 1 >= global.maxconn))
1185 _HA_ATOMIC_INC(&maxconn_reached);
1186
Willy Tarreau30836152023-01-12 19:10:17 +01001187 /* past this point, l->bind_conf->accept() will automatically decrement
Willy Tarreau82c97892019-02-27 19:32:32 +01001188 * l->nbconn, feconn and actconn once done. Setting next_*conn=0
1189 * allows the error path not to rollback on nbconn. It's more
1190 * convenient than duplicating all exit labels.
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001191 */
1192 next_conn = 0;
Willy Tarreau82c97892019-02-27 19:32:32 +01001193 next_feconn = 0;
1194 next_actconn = 0;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001195
Willy Tarreau83efc322020-10-14 17:37:17 +02001196
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001197#if defined(USE_THREAD)
Willy Tarreau9d360602023-03-27 10:38:51 +02001198 if (!(global.tune.options & GTUNE_LISTENER_MQ_ANY) || stopping)
1199 goto local_accept;
1200
1201 /* we want to perform thread rebalancing if the listener is
1202 * bound to more than one thread or if it's part of a shard
1203 * with more than one listener.
1204 */
Willy Tarreaub2f38c12023-01-19 19:14:18 +01001205 mask = l->rx.bind_thread & _HA_ATOMIC_LOAD(&tg->threads_enabled);
Willy Tarreau9d360602023-03-27 10:38:51 +02001206 if (l->rx.shard_info || atleast2(mask)) {
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001207 struct accept_queue_ring *ring;
Willy Tarreau9d360602023-03-27 10:38:51 +02001208 struct listener *new_li;
Willy Tarreauff185042023-04-20 16:52:21 +02001209 uint r1, r2, t, t1, t2;
1210 ulong n0, n1;
Willy Tarreau9d360602023-03-27 10:38:51 +02001211 const struct tgroup_info *g1, *g2;
1212 ulong m1, m2;
Willy Tarreauff185042023-04-20 16:52:21 +02001213 ulong *thr_idx_ptr;
Willy Tarreaufc630bd2019-03-04 19:57:34 +01001214
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001215 /* The principle is that we have two running indexes,
1216 * each visiting in turn all threads bound to this
Willy Tarreau9d360602023-03-27 10:38:51 +02001217 * listener's shard. The connection will be assigned to
1218 * the one with the least connections, and the other
1219 * one will be updated. This provides a good fairness
1220 * on short connections (round robin) and on long ones
1221 * (conn count), without ever missing any idle thread.
1222 * Each thread number is encoded as a combination of
1223 * times the receiver number and its local thread
1224 * number from 0 to MAX_THREADS_PER_GROUP - 1. The two
Willy Tarreauff185042023-04-20 16:52:21 +02001225 * indexes are stored as 10/12 bit numbers in the thr_idx
1226 * array, since there are up to LONGBITS threads and
1227 * groups that can be represented. They are represented
1228 * like this:
1229 * 31:20 19:15 14:10 9:5 4:0
1230 * 32b: [ counter | r2num | t2num | r1num | t1num ]
1231 *
1232 * 63:24 23:18 17:12 11:6 5:0
1233 * 64b: [ counter | r2num | t2num | r1num | t1num ]
1234 *
1235 * The change counter is only used to avoid swapping too
1236 * old a value when the value loops back.
Willy Tarreau9d360602023-03-27 10:38:51 +02001237 *
1238 * In the loop below we have this for each index:
1239 * - n is the thread index
1240 * - r is the receiver number
1241 * - g is the receiver's thread group
1242 * - t is the thread number in this receiver
1243 * - m is the receiver's thread mask shifted by the thread number
Willy Tarreaufc630bd2019-03-04 19:57:34 +01001244 */
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001245
1246 /* keep a copy for the final update. thr_idx is composite
Willy Tarreau9d360602023-03-27 10:38:51 +02001247 * and made of (n2<<16) + n1.
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001248 */
Willy Tarreaub6574922023-03-29 17:02:17 +02001249 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 +02001250 while (1) {
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001251 int q0, q1, q2;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001252
Willy Tarreauff185042023-04-20 16:52:21 +02001253 /* calculate r1/g1/t1 first (ascending idx) */
1254 n0 = _HA_ATOMIC_LOAD(thr_idx_ptr);
Willy Tarreau9d360602023-03-27 10:38:51 +02001255 new_li = NULL;
1256
Willy Tarreauff185042023-04-20 16:52:21 +02001257 t1 = (uint)n0 & (LONGBITS - 1);
1258 r1 = ((uint)n0 / LONGBITS) & (LONGBITS - 1);
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001259
Willy Tarreau9d360602023-03-27 10:38:51 +02001260 while (1) {
1261 if (l->rx.shard_info) {
1262 /* multiple listeners, take the group into account */
1263 if (r1 >= l->rx.shard_info->nbgroups)
1264 r1 = 0;
1265
1266 g1 = &ha_tgroup_info[l->rx.shard_info->members[r1]->bind_tgroup - 1];
1267 m1 = l->rx.shard_info->members[r1]->bind_thread;
1268 } else {
1269 /* single listener */
1270 r1 = 0;
1271 g1 = tg;
1272 m1 = l->rx.bind_thread;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001273 }
Willy Tarreau9d360602023-03-27 10:38:51 +02001274 m1 &= _HA_ATOMIC_LOAD(&g1->threads_enabled);
1275 m1 >>= t1;
1276
1277 /* find first existing thread */
1278 if (unlikely(!(m1 & 1))) {
1279 m1 &= ~1UL;
1280 if (!m1) {
1281 /* no more threads here, switch to
1282 * first thread of next group.
1283 */
1284 t1 = 0;
1285 if (l->rx.shard_info)
1286 r1++;
1287 /* loop again */
1288 continue;
1289 }
1290 t1 += my_ffsl(m1) - 1;
1291 }
1292 /* done: r1 and t1 are OK */
1293 break;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001294 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001295
Willy Tarreauff185042023-04-20 16:52:21 +02001296 /* now r2/g2/t2 (descending idx) */
1297 t2 = ((uint)n0 / LONGBITS / LONGBITS) & (LONGBITS - 1);
1298 r2 = ((uint)n0 / LONGBITS / LONGBITS / LONGBITS) & (LONGBITS - 1);
Willy Tarreau9d360602023-03-27 10:38:51 +02001299
Willy Tarreau84fe1f42023-04-20 15:40:38 +02001300 /* if running in round-robin mode ("fair"), we don't need
1301 * to go further.
1302 */
1303 if ((global.tune.options & GTUNE_LISTENER_MQ_ANY) == GTUNE_LISTENER_MQ_FAIR) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001304 t = g1->base + t1;
1305 if (l->rx.shard_info && t != tid)
1306 new_li = l->rx.shard_info->members[r1]->owner;
Willy Tarreau84fe1f42023-04-20 15:40:38 +02001307 goto updt_t1;
1308 }
1309
Willy Tarreau9d360602023-03-27 10:38:51 +02001310 while (1) {
1311 if (l->rx.shard_info) {
1312 /* multiple listeners, take the group into account */
1313 if (r2 >= l->rx.shard_info->nbgroups)
1314 r2 = l->rx.shard_info->nbgroups - 1;
Willy Tarreau85d04242019-04-16 18:09:13 +02001315
Willy Tarreau9d360602023-03-27 10:38:51 +02001316 g2 = &ha_tgroup_info[l->rx.shard_info->members[r2]->bind_tgroup - 1];
1317 m2 = l->rx.shard_info->members[r2]->bind_thread;
1318 } else {
1319 /* single listener */
1320 r2 = 0;
1321 g2 = tg;
1322 m2 = l->rx.bind_thread;
1323 }
1324 m2 &= _HA_ATOMIC_LOAD(&g2->threads_enabled);
1325 m2 &= nbits(t2 + 1);
1326
1327 /* find previous existing thread */
1328 if (unlikely(!(m2 & (1UL << t2)) || (g1 == g2 && t1 == t2))) {
1329 /* highest bit not set or colliding threads, let's check
1330 * if we still have other threads available after this
1331 * one.
1332 */
1333 m2 &= ~(1UL << t2);
1334 if (!m2) {
1335 /* no more threads here, switch to
1336 * last thread of previous group.
1337 */
1338 t2 = MAX_THREADS_PER_GROUP - 1;
1339 if (l->rx.shard_info)
1340 r2--;
1341 /* loop again */
1342 continue;
1343 }
1344 t2 = my_flsl(m2) - 1;
1345 }
1346 /* done: r2 and t2 are OK */
1347 break;
Willy Tarreau85d04242019-04-16 18:09:13 +02001348 }
1349
Willy Tarreau77e33502023-04-19 17:19:28 +02001350 /* tests show that it's worth checking that other threads have not
1351 * already changed the index to save the rest of the calculation,
1352 * or we'd have to redo it anyway.
1353 */
Willy Tarreauff185042023-04-20 16:52:21 +02001354 if (n0 != _HA_ATOMIC_LOAD(thr_idx_ptr))
Willy Tarreau77e33502023-04-19 17:19:28 +02001355 continue;
Willy Tarreau77e33502023-04-19 17:19:28 +02001356
Willy Tarreau9d360602023-03-27 10:38:51 +02001357 /* here we have (r1,g1,t1) that designate the first receiver, its
1358 * thread group and local thread, and (r2,g2,t2) that designate
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001359 * the second receiver, its thread group and local thread. We'll
1360 * also consider the local thread with q0.
Willy Tarreau9d360602023-03-27 10:38:51 +02001361 */
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001362 q0 = accept_queue_ring_len(&accept_queue_rings[tid]);
Willy Tarreau9d360602023-03-27 10:38:51 +02001363 q1 = accept_queue_ring_len(&accept_queue_rings[g1->base + t1]);
1364 q2 = accept_queue_ring_len(&accept_queue_rings[g2->base + t2]);
1365
1366 /* add to this the currently active connections */
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001367 q0 += _HA_ATOMIC_LOAD(&l->thr_conn[ti->ltid]);
Willy Tarreau9d360602023-03-27 10:38:51 +02001368 if (l->rx.shard_info) {
1369 q1 += _HA_ATOMIC_LOAD(&((struct listener *)l->rx.shard_info->members[r1]->owner)->thr_conn[t1]);
1370 q2 += _HA_ATOMIC_LOAD(&((struct listener *)l->rx.shard_info->members[r2]->owner)->thr_conn[t2]);
1371 } else {
1372 q1 += _HA_ATOMIC_LOAD(&l->thr_conn[t1]);
1373 q2 += _HA_ATOMIC_LOAD(&l->thr_conn[t2]);
1374 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001375
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001376 /* we have 3 possibilities now :
1377 * q1 < q2 : t1 is less loaded than t2, so we pick it
1378 * and update t2 (since t1 might still be
1379 * lower than another thread)
1380 * q1 > q2 : t2 is less loaded than t1, so we pick it
1381 * and update t1 (since t2 might still be
1382 * lower than another thread)
1383 * q1 = q2 : both are equally loaded, thus we pick t1
1384 * and update t1 as it will become more loaded
1385 * than t2.
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001386 * On top of that, if in the end the current thread appears
1387 * to be as good of a deal, we'll prefer it over a foreign
1388 * one as it will improve locality and avoid a migration.
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001389 */
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001390
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001391 if (q1 - q2 < 0) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001392 t = g1->base + t1;
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001393 if (q0 <= q1)
1394 t = tid;
Willy Tarreau9d360602023-03-27 10:38:51 +02001395
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001396 if (l->rx.shard_info && t != tid)
Willy Tarreau9d360602023-03-27 10:38:51 +02001397 new_li = l->rx.shard_info->members[r1]->owner;
1398
1399 t2--;
1400 if (t2 >= MAX_THREADS_PER_GROUP) {
1401 if (l->rx.shard_info)
1402 r2--;
1403 t2 = MAX_THREADS_PER_GROUP - 1;
1404 }
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001405 }
1406 else if (q1 - q2 > 0) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001407 t = g2->base + t2;
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001408 if (q0 <= q2)
1409 t = tid;
Willy Tarreau9d360602023-03-27 10:38:51 +02001410
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001411 if (l->rx.shard_info && t != tid)
Willy Tarreau9d360602023-03-27 10:38:51 +02001412 new_li = l->rx.shard_info->members[r2]->owner;
1413 goto updt_t1;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001414 }
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001415 else { // q1 == q2
Willy Tarreau9d360602023-03-27 10:38:51 +02001416 t = g1->base + t1;
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001417 if (q0 < q1) // local must be strictly better than both
1418 t = tid;
Willy Tarreau9d360602023-03-27 10:38:51 +02001419
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001420 if (l->rx.shard_info && t != tid)
Willy Tarreau9d360602023-03-27 10:38:51 +02001421 new_li = l->rx.shard_info->members[r1]->owner;
Willy Tarreau84fe1f42023-04-20 15:40:38 +02001422 updt_t1:
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001423 t1++;
Willy Tarreau9d360602023-03-27 10:38:51 +02001424 if (t1 >= MAX_THREADS_PER_GROUP) {
1425 if (l->rx.shard_info)
1426 r1++;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001427 t1 = 0;
Willy Tarreau9d360602023-03-27 10:38:51 +02001428 }
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001429 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001430
Willy Tarreauff185042023-04-20 16:52:21 +02001431 /* The target thread number is in <t> now. Let's
1432 * compute the new index and try to update it.
1433 */
Willy Tarreau9d360602023-03-27 10:38:51 +02001434
Willy Tarreauff185042023-04-20 16:52:21 +02001435 /* take previous counter and increment it */
1436 n1 = n0 & -(ulong)(LONGBITS * LONGBITS * LONGBITS * LONGBITS);
1437 n1 += LONGBITS * LONGBITS * LONGBITS * LONGBITS;
1438 n1 += (((r2 * LONGBITS) + t2) * LONGBITS * LONGBITS);
1439 n1 += (r1 * LONGBITS) + t1;
Willy Tarreaub6574922023-03-29 17:02:17 +02001440 if (likely(_HA_ATOMIC_CAS(thr_idx_ptr, &n0, n1)))
Willy Tarreau9d360602023-03-27 10:38:51 +02001441 break;
Willy Tarreauff185042023-04-20 16:52:21 +02001442
1443 /* bah we lost the race, try again */
1444 __ha_cpu_relax();
Willy Tarreau9d360602023-03-27 10:38:51 +02001445 } /* end of main while() loop */
1446
1447 /* we may need to update the listener in the connection
1448 * if we switched to another group.
1449 */
1450 if (new_li)
1451 cli_conn->target = &new_li->obj_type;
1452
1453 /* here we have the target thread number in <t> and we hold a
1454 * reservation in the target ring.
1455 */
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001456
Amaury Denoyellea66e0432023-04-05 18:16:28 +02001457 if (l->rx.proto && l->rx.proto->set_affinity) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001458 if (l->rx.proto->set_affinity(cli_conn, t)) {
Amaury Denoyellea66e0432023-04-05 18:16:28 +02001459 /* Failed migration, stay on the same thread. */
1460 goto local_accept;
1461 }
1462 }
1463
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001464 /* We successfully selected the best thread "t" for this
1465 * connection. We use deferred accepts even if it's the
1466 * local thread because tests show that it's the best
1467 * performing model, likely due to better cache locality
1468 * when processing this loop.
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001469 */
Willy Tarreau9d360602023-03-27 10:38:51 +02001470 ring = &accept_queue_rings[t];
Willy Tarreau83efc322020-10-14 17:37:17 +02001471 if (accept_queue_push_mp(ring, cli_conn)) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001472 _HA_ATOMIC_INC(&activity[t].accq_pushed);
Willy Tarreau2bd65a72019-09-24 06:55:18 +02001473 tasklet_wakeup(ring->tasklet);
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001474 continue;
1475 }
1476 /* If the ring is full we do a synchronous accept on
1477 * the local thread here.
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001478 */
Willy Tarreau9d360602023-03-27 10:38:51 +02001479 _HA_ATOMIC_INC(&activity[t].accq_full);
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001480 }
1481#endif // USE_THREAD
1482
Amaury Denoyelle7f7713d2022-01-19 11:37:50 +01001483 local_accept:
Willy Tarreau9d360602023-03-27 10:38:51 +02001484 /* restore the connection's listener in case we failed to migrate above */
1485 cli_conn->target = &l->obj_type;
Willy Tarreaufea8c192023-02-28 10:25:57 +01001486 _HA_ATOMIC_INC(&l->thr_conn[ti->ltid]);
Willy Tarreau30836152023-01-12 19:10:17 +01001487 ret = l->bind_conf->accept(cli_conn);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001488 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001489 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001490 * we just have to ignore it (ret == 0) or it's a critical
1491 * error due to a resource shortage, and we must stop the
1492 * listener (ret < 0).
1493 */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001494 if (ret == 0) /* successful termination */
1495 continue;
1496
Willy Tarreaubb660302014-05-07 19:47:02 +02001497 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001498 }
1499
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001500 /* increase the per-process number of cumulated sessions, this
Willy Tarreau30836152023-01-12 19:10:17 +01001501 * may only be done once l->bind_conf->accept() has accepted the
1502 * connection.
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001503 */
Willy Tarreau17146802023-01-12 19:58:42 +01001504 if (!(l->bind_conf->options & BC_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001505 count = update_freq_ctr(&global.sess_per_sec, 1);
1506 HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
Willy Tarreau93e7c002013-10-07 18:51:07 +02001507 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001508#ifdef USE_OPENSSL
Willy Tarreau17146802023-01-12 19:58:42 +01001509 if (!(l->bind_conf->options & BC_O_UNLIMITED) &&
Willy Tarreau11ba4042022-05-20 15:56:32 +02001510 l->bind_conf && l->bind_conf->options & BC_O_USE_SSL) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001511 count = update_freq_ctr(&global.ssl_per_sec, 1);
1512 HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
Willy Tarreaue43d5322013-10-07 20:01:52 +02001513 }
1514#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +02001515
Willy Tarreaubdcd3252022-06-22 09:19:46 +02001516 _HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_STUCK); // this thread is still running
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001517 } /* end of for (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001518
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001519 end:
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001520 if (next_conn)
Willy Tarreau4781b152021-04-06 13:53:36 +02001521 _HA_ATOMIC_DEC(&l->nbconn);
Willy Tarreau741b4d62019-02-25 15:02:04 +01001522
Willy Tarreau82c97892019-02-27 19:32:32 +01001523 if (p && next_feconn)
Willy Tarreau4781b152021-04-06 13:53:36 +02001524 _HA_ATOMIC_DEC(&p->feconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001525
1526 if (next_actconn)
Willy Tarreau4781b152021-04-06 13:53:36 +02001527 _HA_ATOMIC_DEC(&actconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001528
Willy Tarreau758c69d2023-01-12 18:59:37 +01001529 if ((l->state == LI_FULL && (!l->bind_conf->maxconn || l->nbconn < l->bind_conf->maxconn)) ||
Willy Tarreau02757d02021-01-28 18:07:24 +01001530 (l->state == LI_LIMITED &&
Willy Tarreaucdcba112019-12-11 15:06:30 +01001531 ((!p || p->feconn < p->maxconn) && (actconn < global.maxconn) &&
1532 (!tick_isset(global_listener_queue_task->expire) ||
1533 tick_is_expired(global_listener_queue_task->expire, now_ms))))) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001534 /* at least one thread has to this when quitting */
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +01001535 relax_listener(l, 0, 0);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001536
Willy Tarreau02757d02021-01-28 18:07:24 +01001537 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001538 dequeue_all_listeners();
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001539
Olivier Houchard859dc802019-08-08 15:47:21 +02001540 if (p && !MT_LIST_ISEMPTY(&p->listener_queue) &&
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001541 (!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 +01001542 dequeue_proxy_listeners(p);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001543 }
Willy Tarreau0591bf72019-12-10 12:01:21 +01001544 return;
1545
1546 transient_error:
1547 /* pause the listener for up to 100 ms */
1548 expire = tick_add(now_ms, 100);
1549
Willy Tarreau258b3512020-10-13 17:46:05 +02001550 /* This may be a shared socket that was paused by another process.
1551 * Let's put it to pause in this case.
1552 */
1553 if (l->rx.proto && l->rx.proto->rx_listening(&l->rx) == 0) {
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +01001554 suspend_listener(l, 0, 0);
Willy Tarreau258b3512020-10-13 17:46:05 +02001555 goto end;
1556 }
1557
Willy Tarreau0591bf72019-12-10 12:01:21 +01001558 limit_global:
1559 /* (re-)queue the listener to the global queue and set it to expire no
1560 * later than <expire> ahead. The listener turns to LI_LIMITED.
1561 */
1562 limit_listener(l, &global_listener_queue);
Christopher Faulet13e86d92022-11-17 14:40:20 +01001563 HA_RWLOCK_RDLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001564 task_schedule(global_listener_queue_task, expire);
Christopher Faulet13e86d92022-11-17 14:40:20 +01001565 HA_RWLOCK_RDUNLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001566 goto end;
1567
1568 limit_proxy:
1569 /* (re-)queue the listener to the proxy's queue and set it to expire no
1570 * later than <expire> ahead. The listener turns to LI_LIMITED.
1571 */
1572 limit_listener(l, &p->listener_queue);
Willy Tarreaueeea8082020-01-08 19:15:07 +01001573 if (p->task && tick_isset(expire))
1574 task_schedule(p->task, expire);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001575 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001576}
1577
Willy Tarreau05f50472017-09-15 09:19:58 +02001578/* Notify the listener that a connection initiated from it was released. This
1579 * is used to keep the connection count consistent and to possibly re-open
1580 * listening when it was limited.
1581 */
1582void listener_release(struct listener *l)
1583{
1584 struct proxy *fe = l->bind_conf->frontend;
1585
Amaury Denoyelle331b8b12023-10-25 10:52:23 +02001586 if (listener_uses_maxconn(l))
Willy Tarreau4781b152021-04-06 13:53:36 +02001587 _HA_ATOMIC_DEC(&actconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001588 if (fe)
Willy Tarreau4781b152021-04-06 13:53:36 +02001589 _HA_ATOMIC_DEC(&fe->feconn);
1590 _HA_ATOMIC_DEC(&l->nbconn);
Willy Tarreaufea8c192023-02-28 10:25:57 +01001591 _HA_ATOMIC_DEC(&l->thr_conn[ti->ltid]);
Willy Tarreau82c97892019-02-27 19:32:32 +01001592
1593 if (l->state == LI_FULL || l->state == LI_LIMITED)
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +01001594 relax_listener(l, 0, 0);
Willy Tarreau05f50472017-09-15 09:19:58 +02001595
Willy Tarreau02757d02021-01-28 18:07:24 +01001596 /* Dequeues all of the listeners waiting for a resource */
1597 dequeue_all_listeners();
1598
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +02001599 if (fe && !MT_LIST_ISEMPTY(&fe->listener_queue) &&
Willy Tarreau05f50472017-09-15 09:19:58 +02001600 (!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 +01001601 dequeue_proxy_listeners(fe);
Christopher Faulet42b71d92024-03-12 07:31:56 +01001602 else {
1603 unsigned int wait;
1604 int expire = TICK_ETERNITY;
1605
Christopher Faulet07838f72024-03-14 09:29:09 +01001606 if (fe->task && fe->fe_sps_lim &&
Christopher Faulet42b71d92024-03-12 07:31:56 +01001607 (wait = next_event_delay(&fe->fe_sess_per_sec,fe->fe_sps_lim, 0))) {
1608 /* we're blocking because a limit was reached on the number of
1609 * requests/s on the frontend. We want to re-check ASAP, which
1610 * means in 1 ms before estimated expiration date, because the
1611 * timer will have settled down.
1612 */
1613 expire = tick_first(fe->task->expire, tick_add(now_ms, wait));
Christopher Faulet07838f72024-03-14 09:29:09 +01001614 if (tick_isset(expire))
Christopher Faulet42b71d92024-03-12 07:31:56 +01001615 task_schedule(fe->task, expire);
1616 }
1617 }
Willy Tarreau05f50472017-09-15 09:19:58 +02001618}
1619
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001620/* Initializes the listener queues. Returns 0 on success, otherwise ERR_* flags */
1621static int listener_queue_init()
1622{
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001623 global_listener_queue_task = task_new_anywhere();
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001624 if (!global_listener_queue_task) {
1625 ha_alert("Out of memory when initializing global listener queue\n");
1626 return ERR_FATAL|ERR_ABORT;
1627 }
1628 /* very simple initialization, users will queue the task if needed */
1629 global_listener_queue_task->context = NULL; /* not even a context! */
1630 global_listener_queue_task->process = manage_global_listener_queue;
Christopher Faulet13e86d92022-11-17 14:40:20 +01001631 HA_RWLOCK_INIT(&global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001632
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001633 return 0;
1634}
1635
1636static void listener_queue_deinit()
1637{
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001638 task_destroy(global_listener_queue_task);
1639 global_listener_queue_task = NULL;
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001640}
1641
1642REGISTER_CONFIG_POSTPARSER("multi-threaded listener queue", listener_queue_init);
1643REGISTER_POST_DEINIT(listener_queue_deinit);
1644
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001645
1646/* This is the global management task for listeners. It enables listeners waiting
1647 * for global resources when there are enough free resource, or at least once in
Willy Tarreaud597ec22021-01-29 14:29:06 +01001648 * a while. It is designed to be called as a task. It's exported so that it's easy
1649 * to spot in "show tasks" or "show profiling".
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001650 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001651struct task *manage_global_listener_queue(struct task *t, void *context, unsigned int state)
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001652{
1653 /* If there are still too many concurrent connections, let's wait for
1654 * some of them to go away. We don't need to re-arm the timer because
1655 * each of them will scan the queue anyway.
1656 */
1657 if (unlikely(actconn >= global.maxconn))
1658 goto out;
1659
1660 /* We should periodically try to enable listeners waiting for a global
1661 * resource here, because it is possible, though very unlikely, that
1662 * they have been blocked by a temporary lack of global resource such
1663 * as a file descriptor or memory and that the temporary condition has
1664 * disappeared.
1665 */
1666 dequeue_all_listeners();
1667
1668 out:
Christopher Faulet13e86d92022-11-17 14:40:20 +01001669 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001670 t->expire = TICK_ETERNITY;
Christopher Faulet13e86d92022-11-17 14:40:20 +01001671 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001672 return t;
1673}
1674
Willy Tarreauf6a84442023-04-22 23:25:38 +02001675/* Applies the thread mask, shards etc to the bind_conf. It normally returns 0
1676 * otherwie the number of errors. Upon error it may set error codes (ERR_*) in
1677 * err_code. It is supposed to be called only once very late in the boot process
1678 * after the bind_conf's thread_set is fixed. The function may emit warnings and
1679 * alerts. Extra listeners may be created on the fly.
1680 */
1681int bind_complete_thread_setup(struct bind_conf *bind_conf, int *err_code)
1682{
1683 struct proxy *fe = bind_conf->frontend;
1684 struct listener *li, *new_li, *ref;
1685 struct thread_set new_ts;
1686 int shard, shards, todo, done, grp, dups;
1687 ulong mask, gmask, bit;
1688 int cfgerr = 0;
1689 char *err;
1690
1691 err = NULL;
Willy Tarreauc38499c2023-04-22 22:27:31 +02001692 if (thread_resolve_group_mask(&bind_conf->thread_set, 0, &err) < 0) {
Willy Tarreaua22db652023-04-22 23:52:17 +02001693 ha_alert("%s '%s': %s in 'bind %s' at [%s:%d].\n",
1694 proxy_type_str(fe),
Willy Tarreauf6a84442023-04-22 23:25:38 +02001695 fe->id, err, bind_conf->arg, bind_conf->file, bind_conf->line);
1696 free(err);
1697 cfgerr++;
1698 return cfgerr;
1699 }
1700
1701 /* apply thread masks and groups to all receivers */
1702 list_for_each_entry(li, &bind_conf->listeners, by_bind) {
1703 shards = bind_conf->settings.shards;
1704 todo = thread_set_count(&bind_conf->thread_set);
1705
1706 /* special values: -1 = "by-thread", -2 = "by-group" */
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001707 if (shards == -1) {
Willy Tarreau8a5e6f42023-04-22 17:39:30 +02001708 if (protocol_supports_flag(li->rx.proto, PROTO_F_REUSEPORT_SUPPORTED))
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001709 shards = todo;
1710 else {
1711 if (fe != global.cli_fe)
1712 ha_diag_warning("[%s:%d]: Disabling per-thread sharding for listener in"
1713 " %s '%s' because SO_REUSEPORT is disabled\n",
1714 bind_conf->file, bind_conf->line, proxy_type_str(fe), fe->id);
1715 shards = 1;
1716 }
1717 }
Willy Tarreauf6a84442023-04-22 23:25:38 +02001718 else if (shards == -2)
Willy Tarreau8a5e6f42023-04-22 17:39:30 +02001719 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 +02001720
1721 /* no more shards than total threads */
1722 if (shards > todo)
1723 shards = todo;
1724
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001725 /* We also need to check if an explicit shards count was set and cannot be honored */
Willy Tarreau8a5e6f42023-04-22 17:39:30 +02001726 if (shards > 1 && !protocol_supports_flag(li->rx.proto, PROTO_F_REUSEPORT_SUPPORTED)) {
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001727 ha_warning("[%s:%d]: Disabling sharding for listener in %s '%s' because SO_REUSEPORT is disabled\n",
1728 bind_conf->file, bind_conf->line, proxy_type_str(fe), fe->id);
1729 shards = 1;
1730 }
1731
Willy Tarreauf6a84442023-04-22 23:25:38 +02001732 shard = done = grp = bit = mask = 0;
1733 new_li = li;
1734
1735 while (shard < shards) {
1736 memset(&new_ts, 0, sizeof(new_ts));
1737 while (grp < global.nbtgroups && done < todo) {
1738 /* enlarge mask to cover next bit of bind_thread till we
1739 * have enough bits for one shard. We restart from the
1740 * current grp+bit.
1741 */
1742
1743 /* first let's find the first non-empty group starting at <mask> */
1744 if (!(bind_conf->thread_set.rel[grp] & ha_tgroup_info[grp].threads_enabled & ~mask)) {
1745 grp++;
1746 mask = 0;
1747 continue;
1748 }
1749
1750 /* take next unassigned bit */
1751 bit = (bind_conf->thread_set.rel[grp] & ~mask) & -(bind_conf->thread_set.rel[grp] & ~mask);
1752 new_ts.rel[grp] |= bit;
1753 mask |= bit;
1754 new_ts.grps |= 1UL << grp;
1755
1756 done += shards;
1757 };
1758
1759 BUG_ON(!new_ts.grps); // no more bits left unassigned
1760
1761 /* Create all required listeners for all bound groups. If more than one group is
1762 * needed, the first receiver serves as a reference, and subsequent ones point to
1763 * it. We already have a listener available in new_li() so we only allocate a new
1764 * one if we're not on the last one. We count the remaining groups by copying their
1765 * mask into <gmask> and dropping the lowest bit at the end of the loop until there
1766 * is no more. Ah yes, it's not pretty :-/
1767 */
1768 ref = new_li;
1769 gmask = new_ts.grps;
1770 for (dups = 0; gmask; dups++) {
1771 /* assign the first (and only) thread and group */
1772 new_li->rx.bind_thread = thread_set_nth_tmask(&new_ts, dups);
1773 new_li->rx.bind_tgroup = thread_set_nth_group(&new_ts, dups);
1774
1775 if (dups) {
1776 /* it has been allocated already in the previous round */
1777 shard_info_attach(&new_li->rx, ref->rx.shard_info);
1778 new_li->rx.flags |= RX_F_MUST_DUP;
1779 }
1780
1781 gmask &= gmask - 1; // drop lowest bit
1782 if (gmask) {
1783 /* yet another listener expected in this shard, let's
1784 * chain it.
1785 */
1786 struct listener *tmp_li = clone_listener(new_li);
1787
1788 if (!tmp_li) {
1789 ha_alert("Out of memory while trying to allocate extra listener for group %u of shard %d in %s %s\n",
1790 new_li->rx.bind_tgroup, shard, proxy_type_str(fe), fe->id);
1791 cfgerr++;
1792 *err_code |= ERR_FATAL | ERR_ALERT;
1793 return cfgerr;
1794 }
1795
1796 /* if we're forced to create at least two listeners, we have to
1797 * allocate a shared shard_info that's linked to from the reference
1798 * and each other listener, so we'll create it here.
1799 */
1800 if (!shard_info_attach(&ref->rx, NULL)) {
1801 ha_alert("Out of memory while trying to allocate shard_info for listener for group %u of shard %d in %s %s\n",
1802 new_li->rx.bind_tgroup, shard, proxy_type_str(fe), fe->id);
1803 cfgerr++;
1804 *err_code |= ERR_FATAL | ERR_ALERT;
1805 return cfgerr;
1806 }
Willy Tarreaue2348bd2024-04-09 08:41:06 +02001807 /* assign the ID to the first one only */
1808 new_li->luid = new_li->conf.id.key = tmp_li->luid;
1809 tmp_li->luid = 0;
1810 eb32_delete(&tmp_li->conf.id);
1811 if (tmp_li->luid)
1812 eb32_insert(&fe->conf.used_listener_id, &new_li->conf.id);
Willy Tarreauf6a84442023-04-22 23:25:38 +02001813 new_li = tmp_li;
1814 }
1815 }
1816 done -= todo;
1817
1818 shard++;
1819 if (shard >= shards)
1820 break;
1821
1822 /* create another listener for new shards */
1823 new_li = clone_listener(li);
1824 if (!new_li) {
1825 ha_alert("Out of memory while trying to allocate extra listener for shard %d in %s %s\n",
1826 shard, proxy_type_str(fe), fe->id);
1827 cfgerr++;
1828 *err_code |= ERR_FATAL | ERR_ALERT;
1829 return cfgerr;
1830 }
Willy Tarreaue2348bd2024-04-09 08:41:06 +02001831 /* assign the ID to the first one only */
1832 new_li->luid = new_li->conf.id.key = li->luid;
1833 li->luid = 0;
1834 eb32_delete(&li->conf.id);
1835 if (li->luid)
1836 eb32_insert(&fe->conf.used_listener_id, &new_li->conf.id);
Willy Tarreauf6a84442023-04-22 23:25:38 +02001837 }
1838 }
1839
1840 /* success */
1841 return cfgerr;
1842}
1843
Willy Tarreau26982662012-09-12 23:17:10 +02001844/*
1845 * Registers the bind keyword list <kwl> as a list of valid keywords for next
1846 * parsing sessions.
1847 */
1848void bind_register_keywords(struct bind_kw_list *kwl)
1849{
Willy Tarreau2b718102021-04-21 07:32:39 +02001850 LIST_APPEND(&bind_keywords.list, &kwl->list);
Willy Tarreau26982662012-09-12 23:17:10 +02001851}
1852
1853/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
1854 * keyword is found with a NULL ->parse() function, then an attempt is made to
1855 * find one with a valid ->parse() function. This way it is possible to declare
1856 * platform-dependant, known keywords as NULL, then only declare them as valid
1857 * if some options are met. Note that if the requested keyword contains an
1858 * opening parenthesis, everything from this point is ignored.
1859 */
1860struct bind_kw *bind_find_kw(const char *kw)
1861{
1862 int index;
1863 const char *kwend;
1864 struct bind_kw_list *kwl;
1865 struct bind_kw *ret = NULL;
1866
1867 kwend = strchr(kw, '(');
1868 if (!kwend)
1869 kwend = kw + strlen(kw);
1870
1871 list_for_each_entry(kwl, &bind_keywords.list, list) {
1872 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1873 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1874 kwl->kw[index].kw[kwend-kw] == 0) {
1875 if (kwl->kw[index].parse)
1876 return &kwl->kw[index]; /* found it !*/
1877 else
1878 ret = &kwl->kw[index]; /* may be OK */
1879 }
1880 }
1881 }
1882 return ret;
1883}
1884
Willy Tarreau8638f482012-09-18 18:01:17 +02001885/* Dumps all registered "bind" keywords to the <out> string pointer. The
1886 * unsupported keywords are only dumped if their supported form was not
1887 * found.
1888 */
1889void bind_dump_kws(char **out)
1890{
1891 struct bind_kw_list *kwl;
1892 int index;
1893
Christopher Faulet784063e2020-05-18 12:14:18 +02001894 if (!out)
1895 return;
1896
Willy Tarreau8638f482012-09-18 18:01:17 +02001897 *out = NULL;
1898 list_for_each_entry(kwl, &bind_keywords.list, list) {
1899 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1900 if (kwl->kw[index].parse ||
1901 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +02001902 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
1903 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +02001904 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +02001905 kwl->kw[index].skip ? " <arg>" : "",
1906 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +02001907 }
1908 }
1909 }
1910}
1911
Willy Tarreau433b05f2021-03-12 10:14:07 +01001912/* Try to find in srv_keyword the word that looks closest to <word> by counting
1913 * transitions between letters, digits and other characters. Will return the
1914 * best matching word if found, otherwise NULL.
1915 */
1916const char *bind_find_best_kw(const char *word)
1917{
1918 uint8_t word_sig[1024];
1919 uint8_t list_sig[1024];
1920 const struct bind_kw_list *kwl;
1921 const char *best_ptr = NULL;
1922 int dist, best_dist = INT_MAX;
1923 int index;
1924
1925 make_word_fingerprint(word_sig, word);
1926 list_for_each_entry(kwl, &bind_keywords.list, list) {
1927 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1928 make_word_fingerprint(list_sig, kwl->kw[index].kw);
1929 dist = word_fingerprint_distance(word_sig, list_sig);
1930 if (dist < best_dist) {
1931 best_dist = dist;
1932 best_ptr = kwl->kw[index].kw;
1933 }
1934 }
1935 }
1936
1937 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
1938 best_ptr = NULL;
1939
1940 return best_ptr;
1941}
1942
Willy Tarreaudbf78022021-10-06 09:05:08 +02001943/* allocate an bind_conf struct for a bind line, and chain it to the frontend <fe>.
1944 * If <arg> is not NULL, it is duplicated into ->arg to store useful config
1945 * information for error reporting. NULL is returned on error.
1946 */
1947struct bind_conf *bind_conf_alloc(struct proxy *fe, const char *file,
1948 int line, const char *arg, struct xprt_ops *xprt)
1949{
1950 struct bind_conf *bind_conf = calloc(1, sizeof(*bind_conf));
1951
1952 if (!bind_conf)
1953 goto err;
1954
1955 bind_conf->file = strdup(file);
1956 if (!bind_conf->file)
1957 goto err;
1958 bind_conf->line = line;
1959 if (arg) {
1960 bind_conf->arg = strdup(arg);
1961 if (!bind_conf->arg)
1962 goto err;
1963 }
1964
1965 LIST_APPEND(&fe->conf.bind, &bind_conf->by_fe);
1966 bind_conf->settings.ux.uid = -1;
1967 bind_conf->settings.ux.gid = -1;
1968 bind_conf->settings.ux.mode = 0;
Willy Tarreau73101642023-04-22 22:06:23 +02001969 bind_conf->settings.shards = global.tune.default_shards;
Willy Tarreaudbf78022021-10-06 09:05:08 +02001970 bind_conf->xprt = xprt;
1971 bind_conf->frontend = fe;
Willy Tarreau7866e8e2023-01-12 18:39:42 +01001972 bind_conf->analysers = fe->fe_req_ana;
Willy Tarreaudbf78022021-10-06 09:05:08 +02001973 bind_conf->severity_output = CLI_SEVERITY_NONE;
1974#ifdef USE_OPENSSL
1975 HA_RWLOCK_INIT(&bind_conf->sni_lock);
1976 bind_conf->sni_ctx = EB_ROOT;
1977 bind_conf->sni_w_ctx = EB_ROOT;
1978#endif
1979 LIST_INIT(&bind_conf->listeners);
1980 return bind_conf;
1981
1982 err:
1983 if (bind_conf) {
1984 ha_free(&bind_conf->file);
1985 ha_free(&bind_conf->arg);
1986 }
1987 ha_free(&bind_conf);
1988 return NULL;
1989}
1990
1991const char *listener_state_str(const struct listener *l)
1992{
1993 static const char *states[8] = {
1994 "NEW", "INI", "ASS", "PAU", "LIS", "RDY", "FUL", "LIM",
1995 };
1996 unsigned int st = l->state;
1997
1998 if (st >= sizeof(states) / sizeof(*states))
1999 return "INVALID";
2000 return states[st];
2001}
2002
Willy Tarreau645513a2010-05-24 20:55:15 +02002003/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002004/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +02002005/************************************************************************/
2006
Willy Tarreaua5e37562011-12-16 17:06:15 +01002007/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +02002008static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002009smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002010{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002011 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002012 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +02002013 return 1;
2014}
2015
Willy Tarreaua5e37562011-12-16 17:06:15 +01002016/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +02002017static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002018smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02002019{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002020 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002021 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +02002022 return 1;
2023}
Jerome Magnineb421b22020-03-27 22:08:40 +01002024static int
2025smp_fetch_so_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
2026{
2027 smp->data.u.str.area = smp->sess->listener->name;
2028 if (!smp->data.u.str.area)
2029 return 0;
2030
2031 smp->data.type = SMP_T_STR;
2032 smp->flags = SMP_F_CONST;
2033 smp->data.u.str.data = strlen(smp->data.u.str.area);
2034 return 1;
2035}
Willy Tarreau645513a2010-05-24 20:55:15 +02002036
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002037/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002038static 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 +02002039{
Willy Tarreauf1b47302023-01-12 19:48:50 +01002040 conf->options |= BC_O_ACC_PROXY;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002041 return 0;
2042}
2043
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002044/* parse the "accept-netscaler-cip" bind keyword */
2045static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2046{
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002047 uint32_t val;
2048
2049 if (!*args[cur_arg + 1]) {
2050 memprintf(err, "'%s' : missing value", args[cur_arg]);
2051 return ERR_ALERT | ERR_FATAL;
2052 }
2053
2054 val = atol(args[cur_arg + 1]);
2055 if (val <= 0) {
Willy Tarreaue2711c72019-02-27 15:39:41 +01002056 memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002057 return ERR_ALERT | ERR_FATAL;
2058 }
2059
Willy Tarreauf1b47302023-01-12 19:48:50 +01002060 conf->options |= BC_O_ACC_CIP;
2061 conf->ns_cip_magic = val;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002062 return 0;
2063}
2064
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002065/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002066static 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 +02002067{
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002068 int val;
2069
2070 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002071 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002072 return ERR_ALERT | ERR_FATAL;
2073 }
2074
2075 val = atol(args[cur_arg + 1]);
Willy Tarreaue2711c72019-02-27 15:39:41 +01002076 if (val < 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002077 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002078 return ERR_ALERT | ERR_FATAL;
2079 }
2080
Willy Tarreau1920f892023-01-12 18:55:13 +01002081 conf->backlog = val;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002082 return 0;
2083}
2084
2085/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002086static 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 +02002087{
2088 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002089 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +01002090 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002091
Willy Tarreau4348fad2012-09-20 16:48:07 +02002092 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002093 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002094 return ERR_ALERT | ERR_FATAL;
2095 }
2096
2097 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002098 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002099 return ERR_ALERT | ERR_FATAL;
2100 }
2101
Willy Tarreau4348fad2012-09-20 16:48:07 +02002102 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +01002103 new->luid = strtol(args[cur_arg + 1], &error, 10);
2104 if (*error != '\0') {
2105 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
2106 return ERR_ALERT | ERR_FATAL;
2107 }
Willy Tarreau4348fad2012-09-20 16:48:07 +02002108 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002109
Willy Tarreau4348fad2012-09-20 16:48:07 +02002110 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002111 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002112 return ERR_ALERT | ERR_FATAL;
2113 }
2114
Willy Tarreau4348fad2012-09-20 16:48:07 +02002115 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002116 if (node) {
2117 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002118 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
2119 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
2120 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002121 return ERR_ALERT | ERR_FATAL;
2122 }
2123
Willy Tarreau4348fad2012-09-20 16:48:07 +02002124 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002125 return 0;
2126}
2127
Willy Tarreau3882d2a2022-05-20 15:41:45 +02002128/* Complete a bind_conf by parsing the args after the address. <args> is the
2129 * arguments array, <cur_arg> is the first one to be considered. <section> is
2130 * the section name to report in error messages, and <file> and <linenum> are
2131 * the file name and line number respectively. Note that args[0..1] are used
2132 * in error messages to provide some context. The return value is an error
2133 * code, zero on success or an OR of ERR_{FATAL,ABORT,ALERT,WARN}.
2134 */
2135int bind_parse_args_list(struct bind_conf *bind_conf, char **args, int cur_arg, const char *section, const char *file, int linenum)
2136{
2137 int err_code = 0;
2138
2139 while (*(args[cur_arg])) {
2140 struct bind_kw *kw;
2141 const char *best;
2142
2143 kw = bind_find_kw(args[cur_arg]);
2144 if (kw) {
2145 char *err = NULL;
2146 int code;
2147
2148 if (!kw->parse) {
2149 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : '%s' option is not implemented in this version (check build options).\n",
2150 file, linenum, args[0], args[1], section, args[cur_arg]);
2151 cur_arg += 1 + kw->skip ;
2152 err_code |= ERR_ALERT | ERR_FATAL;
2153 goto out;
2154 }
2155
2156 code = kw->parse(args, cur_arg, bind_conf->frontend, bind_conf, &err);
2157 err_code |= code;
2158
2159 if (code) {
2160 if (err && *err) {
2161 indent_msg(&err, 2);
2162 if (((code & (ERR_WARN|ERR_ALERT)) == ERR_WARN))
2163 ha_warning("parsing [%s:%d] : '%s %s' in section '%s' : %s\n", file, linenum, args[0], args[1], section, err);
2164 else
2165 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : %s\n", file, linenum, args[0], args[1], section, err);
2166 }
2167 else
2168 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : error encountered while processing '%s'.\n",
2169 file, linenum, args[0], args[1], section, args[cur_arg]);
2170 if (code & ERR_FATAL) {
2171 free(err);
2172 cur_arg += 1 + kw->skip;
2173 goto out;
2174 }
2175 }
2176 free(err);
2177 cur_arg += 1 + kw->skip;
2178 continue;
2179 }
2180
2181 best = bind_find_best_kw(args[cur_arg]);
2182 if (best)
2183 ha_alert("parsing [%s:%d] : '%s %s' in section '%s': unknown keyword '%s'; did you mean '%s' maybe ?\n",
2184 file, linenum, args[0], args[1], section, args[cur_arg], best);
2185 else
2186 ha_alert("parsing [%s:%d] : '%s %s' in section '%s': unknown keyword '%s'.\n",
2187 file, linenum, args[0], args[1], section, args[cur_arg]);
2188
2189 err_code |= ERR_ALERT | ERR_FATAL;
2190 goto out;
2191 }
Willy Tarreau64306cc2022-05-20 16:20:52 +02002192
2193 if ((bind_conf->options & (BC_O_USE_SOCK_DGRAM|BC_O_USE_SOCK_STREAM)) == (BC_O_USE_SOCK_DGRAM|BC_O_USE_SOCK_STREAM) ||
2194 (bind_conf->options & (BC_O_USE_XPRT_DGRAM|BC_O_USE_XPRT_STREAM)) == (BC_O_USE_XPRT_DGRAM|BC_O_USE_XPRT_STREAM)) {
2195 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : cannot mix datagram and stream protocols.\n",
2196 file, linenum, args[0], args[1], section);
2197 err_code |= ERR_ALERT | ERR_FATAL;
2198 goto out;
2199 }
2200
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002201 /* The transport layer automatically switches to QUIC when QUIC is
2202 * selected, regardless of bind_conf settings. We then need to
2203 * initialize QUIC params.
2204 */
2205 if ((bind_conf->options & (BC_O_USE_SOCK_DGRAM|BC_O_USE_XPRT_STREAM)) == (BC_O_USE_SOCK_DGRAM|BC_O_USE_XPRT_STREAM)) {
2206#ifdef USE_QUIC
Amaury Denoyellec13d9e12024-02-16 15:40:06 +01002207 struct listener *l __maybe_unused;
2208 int listener_count __maybe_unused = 0;
2209
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002210 bind_conf->xprt = xprt_get(XPRT_QUIC);
Willy Tarreau287f32f2022-05-20 18:16:52 +02002211 if (!(bind_conf->options & BC_O_USE_SSL)) {
2212 bind_conf->options |= BC_O_USE_SSL;
2213 ha_warning("parsing [%s:%d] : '%s %s' in section '%s' : QUIC protocol detected, enabling ssl. Use 'ssl' to shut this warning.\n",
2214 file, linenum, args[0], args[1], section);
2215 }
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002216 quic_transport_params_init(&bind_conf->quic_params, 1);
Amaury Denoyellec13d9e12024-02-16 15:40:06 +01002217
2218#if (!defined(IP_PKTINFO) && !defined(IP_RECVDSTADDR)) || !defined(IPV6_RECVPKTINFO)
2219 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
2220 if (++listener_count > 1 || !is_inet_addr(&l->rx.addr)) {
2221 ha_diag_warning("parsing [%s:%d] : '%s %s' in section '%s' : UDP binding on multiple addresses without IP_PKTINFO or equivalent support may be unreliable.\n",
2222 file, linenum, args[0], args[1], section);
2223 break;
2224 }
2225 }
2226#endif /* (!IP_PKTINFO && !IP_RECVDSTADDR) || !IPV6_RECVPKTINFO */
2227
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002228#else
2229 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : QUIC protocol selected but support not compiled in (check build options).\n",
2230 file, linenum, args[0], args[1], section);
2231 err_code |= ERR_ALERT | ERR_FATAL;
2232 goto out;
2233#endif
2234 }
Willy Tarreau2071a992022-05-20 17:14:31 +02002235 else if (bind_conf->options & BC_O_USE_SSL) {
2236 bind_conf->xprt = xprt_get(XPRT_SSL);
2237 }
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002238
Willy Tarreau3882d2a2022-05-20 15:41:45 +02002239 out:
2240 return err_code;
2241}
2242
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002243/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002244static 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 +02002245{
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002246 int val;
2247
2248 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002249 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002250 return ERR_ALERT | ERR_FATAL;
2251 }
2252
2253 val = atol(args[cur_arg + 1]);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01002254 if (val < 0) {
2255 memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002256 return ERR_ALERT | ERR_FATAL;
2257 }
2258
Willy Tarreau758c69d2023-01-12 18:59:37 +01002259 conf->maxconn = val;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002260 return 0;
2261}
2262
2263/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002264static 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 +02002265{
2266 struct listener *l;
2267
2268 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002269 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002270 return ERR_ALERT | ERR_FATAL;
2271 }
2272
Willy Tarreau4348fad2012-09-20 16:48:07 +02002273 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002274 l->name = strdup(args[cur_arg + 1]);
2275
2276 return 0;
2277}
2278
2279/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002280static 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 +02002281{
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002282 int val;
2283
2284 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002285 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002286 return ERR_ALERT | ERR_FATAL;
2287 }
2288
2289 val = atol(args[cur_arg + 1]);
2290 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002291 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002292 return ERR_ALERT | ERR_FATAL;
2293 }
2294
Willy Tarreau7dbd4182023-01-12 19:32:45 +01002295 conf->nice = val;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002296 return 0;
2297}
2298
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02002299/* parse the "process" bind keyword */
2300static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2301{
Willy Tarreauacd64412022-07-15 17:16:01 +02002302 memprintf(err, "'process %s' on 'bind' lines is not supported anymore, please use 'thread' instead.", args[cur_arg+1]);
2303 return ERR_ALERT | ERR_FATAL;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02002304}
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002305
Christopher Fauleta717b992018-04-10 14:43:00 +02002306/* parse the "proto" bind keyword */
2307static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2308{
2309 struct ist proto;
2310
2311 if (!*args[cur_arg + 1]) {
2312 memprintf(err, "'%s' : missing value", args[cur_arg]);
2313 return ERR_ALERT | ERR_FATAL;
2314 }
2315
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002316 proto = ist(args[cur_arg + 1]);
Christopher Fauleta717b992018-04-10 14:43:00 +02002317 conf->mux_proto = get_mux_proto(proto);
2318 if (!conf->mux_proto) {
2319 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]);
2320 return ERR_ALERT | ERR_FATAL;
2321 }
Willy Tarreauc8cac042021-09-21 14:31:29 +02002322 return 0;
2323}
2324
Willy Tarreaua07635e2023-04-13 17:25:43 +02002325/* parse the "shards" bind keyword. Takes an integer, "by-thread", or "by-group" */
Willy Tarreau6dfbef42021-10-12 15:23:03 +02002326static int bind_parse_shards(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2327{
2328 int val;
2329
2330 if (!*args[cur_arg + 1]) {
2331 memprintf(err, "'%s' : missing value", args[cur_arg]);
2332 return ERR_ALERT | ERR_FATAL;
2333 }
2334
2335 if (strcmp(args[cur_arg + 1], "by-thread") == 0) {
Willy Tarreaud30e82b2023-04-13 17:11:23 +02002336 val = -1; /* -1 = "by-thread", will be fixed in check_config_validity() */
Willy Tarreaua07635e2023-04-13 17:25:43 +02002337 } else if (strcmp(args[cur_arg + 1], "by-group") == 0) {
2338 val = -2; /* -2 = "by-group", will be fixed in check_config_validity() */
Willy Tarreau6dfbef42021-10-12 15:23:03 +02002339 } else {
2340 val = atol(args[cur_arg + 1]);
2341 if (val < 1 || val > MAX_THREADS) {
2342 memprintf(err, "'%s' : invalid value %d, allowed range is %d..%d or 'by-thread'", args[cur_arg], val, 1, MAX_THREADS);
2343 return ERR_ALERT | ERR_FATAL;
2344 }
2345 }
2346
2347 conf->settings.shards = val;
2348 return 0;
2349}
2350
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01002351/* parse the "thread" bind keyword. This will replace any preset thread_set */
Willy Tarreauc8cac042021-09-21 14:31:29 +02002352static int bind_parse_thread(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2353{
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01002354 /* note that the thread set is zeroed before first call, and we don't
2355 * want to reset it so that it remains possible to chain multiple
2356 * "thread" directives.
2357 */
2358 if (parse_thread_set(args[cur_arg+1], &conf->thread_set, err) < 0)
Willy Tarreauc8cac042021-09-21 14:31:29 +02002359 return ERR_ALERT | ERR_FATAL;
Christopher Fauleta717b992018-04-10 14:43:00 +02002360 return 0;
2361}
2362
Willy Tarreau73101642023-04-22 22:06:23 +02002363/* config parser for global "tune.listener.default-shards" */
2364static int cfg_parse_tune_listener_shards(char **args, int section_type, struct proxy *curpx,
2365 const struct proxy *defpx, const char *file, int line,
2366 char **err)
2367{
2368 if (too_many_args(1, args, err, NULL))
2369 return -1;
2370
2371 if (strcmp(args[1], "by-thread") == 0)
2372 global.tune.default_shards = -1;
2373 else if (strcmp(args[1], "by-group") == 0)
2374 global.tune.default_shards = -2;
2375 else if (strcmp(args[1], "by-process") == 0)
2376 global.tune.default_shards = 1;
2377 else {
2378 memprintf(err, "'%s' expects either 'by-process', 'by-group', or 'by-thread' but got '%s'.", args[0], args[1]);
2379 return -1;
2380 }
2381 return 0;
2382}
2383
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002384/* config parser for global "tune.listener.multi-queue", accepts "on", "fair" or "off" */
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002385static int cfg_parse_tune_listener_mq(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01002386 const struct proxy *defpx, const char *file, int line,
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002387 char **err)
2388{
2389 if (too_many_args(1, args, err, NULL))
2390 return -1;
2391
2392 if (strcmp(args[1], "on") == 0)
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002393 global.tune.options = (global.tune.options & ~GTUNE_LISTENER_MQ_ANY) | GTUNE_LISTENER_MQ_OPT;
2394 else if (strcmp(args[1], "fair") == 0)
2395 global.tune.options = (global.tune.options & ~GTUNE_LISTENER_MQ_ANY) | GTUNE_LISTENER_MQ_FAIR;
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002396 else if (strcmp(args[1], "off") == 0)
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002397 global.tune.options &= ~GTUNE_LISTENER_MQ_ANY;
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002398 else {
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002399 memprintf(err, "'%s' expects either 'on', 'fair', or 'off' but got '%s'.", args[0], args[1]);
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002400 return -1;
2401 }
2402 return 0;
2403}
2404
Willy Tarreau61612d42012-04-19 18:42:05 +02002405/* Note: must not be declared <const> as its list will be overwritten.
2406 * Please take care of keeping this list alphabetically sorted.
2407 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002408static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002409 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
2410 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Jerome Magnineb421b22020-03-27 22:08:40 +01002411 { "so_name", smp_fetch_so_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002412 { /* END */ },
2413}};
2414
Willy Tarreau0108d902018-11-25 19:14:37 +01002415INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
2416
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002417/* Note: must not be declared <const> as its list will be overwritten.
2418 * Please take care of keeping this list alphabetically sorted.
2419 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002420static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002421 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002422}};
2423
Willy Tarreau0108d902018-11-25 19:14:37 +01002424INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
2425
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002426/* Note: must not be declared <const> as its list will be overwritten.
2427 * Please take care of keeping this list alphabetically sorted, doing so helps
2428 * all code contributors.
2429 * Optional keywords are also declared with a NULL ->parse() function so that
2430 * the config parser can report an appropriate error when a known keyword was
2431 * not enabled.
2432 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002433static struct bind_kw_list bind_kws = { "ALL", { }, {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002434 { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002435 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
2436 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
2437 { "id", bind_parse_id, 1 }, /* set id of listening socket */
2438 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
2439 { "name", bind_parse_name, 1 }, /* set name of listening socket */
2440 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02002441 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Christopher Fauleta717b992018-04-10 14:43:00 +02002442 { "proto", bind_parse_proto, 1 }, /* set the proto to use for all incoming connections */
Willy Tarreau6dfbef42021-10-12 15:23:03 +02002443 { "shards", bind_parse_shards, 1 }, /* set number of shards */
Willy Tarreauc8cac042021-09-21 14:31:29 +02002444 { "thread", bind_parse_thread, 1 }, /* set list of allowed threads for this socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002445 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002446}};
2447
Willy Tarreau0108d902018-11-25 19:14:37 +01002448INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
2449
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002450/* config keyword parsers */
2451static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau73101642023-04-22 22:06:23 +02002452 { CFG_GLOBAL, "tune.listener.default-shards", cfg_parse_tune_listener_shards },
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002453 { CFG_GLOBAL, "tune.listener.multi-queue", cfg_parse_tune_listener_mq },
2454 { 0, NULL, NULL }
2455}};
2456
2457INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2458
Willy Tarreau645513a2010-05-24 20:55:15 +02002459/*
2460 * Local variables:
2461 * c-indent-level: 8
2462 * c-basic-offset: 8
2463 * End:
2464 */