blob: 45595cc69e76b08c01d0d0d0c0cb5a29cd8d7916 [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreaud1d54542012-09-12 22:58:11 +02002 * Listener management functions.
Willy Tarreaudd815982007-10-16 12:25:14 +02003 *
Willy Tarreau0ccb7442013-01-07 22:54:17 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaudd815982007-10-16 12:25:14 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau6ae1ba62014-05-07 19:01:58 +020013#include <ctype.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020014#include <errno.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020015#include <stdio.h>
16#include <string.h>
Willy Tarreau95ccdde2014-02-01 09:28:36 +010017#include <unistd.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020018
Willy Tarreaudcc048a2020-06-04 19:11:43 +020019#include <haproxy/acl.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020020#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020021#include <haproxy/activity.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020022#include <haproxy/cfgparse.h>
Willy Tarreaudbf78022021-10-06 09:05:08 +020023#include <haproxy/cli-t.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020024#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020025#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/fd.h>
27#include <haproxy/freq_ctr.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020028#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020029#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020030#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/protocol.h>
Willy Tarreau5958c432021-05-08 20:30:37 +020033#include <haproxy/proxy.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020034#include <haproxy/quic_tp.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020037#include <haproxy/task.h>
Willy Tarreau9310f482021-10-06 16:18:40 +020038#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/tools.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020040
Willy Tarreaub648d632007-10-28 22:13:50 +010041
Willy Tarreau26982662012-09-12 23:17:10 +020042/* List head of all known bind keywords */
Willy Tarreauca1acd62022-03-29 15:02:44 +020043struct bind_kw_list bind_keywords = {
Willy Tarreau26982662012-09-12 23:17:10 +020044 .list = LIST_HEAD_INIT(bind_keywords.list)
45};
46
Willy Tarreaua1d97f82019-12-10 11:18:41 +010047/* list of the temporarily limited listeners because of lack of resource */
48static struct mt_list global_listener_queue = MT_LIST_HEAD_INIT(global_listener_queue);
49static struct task *global_listener_queue_task;
Willy Tarreau469fa472022-11-22 09:08:23 +010050__decl_thread(static HA_RWLOCK_T global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +010051
William Dauchy3679d0c2021-02-14 23:22:55 +010052/* listener status for stats */
53const char* li_status_st[LI_STATE_COUNT] = {
54 [LI_STATUS_WAITING] = "WAITING",
55 [LI_STATUS_OPEN] = "OPEN",
56 [LI_STATUS_FULL] = "FULL",
57};
Willy Tarreaua1d97f82019-12-10 11:18:41 +010058
Willy Tarreau1efafce2019-01-27 15:37:19 +010059#if defined(USE_THREAD)
60
61struct accept_queue_ring accept_queue_rings[MAX_THREADS] __attribute__((aligned(64))) = { };
62
63/* dequeue and process a pending connection from the local accept queue (single
Willy Tarreau83efc322020-10-14 17:37:17 +020064 * consumer). Returns the accepted connection or NULL if none was found.
Willy Tarreau1efafce2019-01-27 15:37:19 +010065 */
Willy Tarreau83efc322020-10-14 17:37:17 +020066struct connection *accept_queue_pop_sc(struct accept_queue_ring *ring)
Willy Tarreau1efafce2019-01-27 15:37:19 +010067{
Willy Tarreau1efafce2019-01-27 15:37:19 +010068 unsigned int pos, next;
Willy Tarreau83efc322020-10-14 17:37:17 +020069 struct connection *ptr;
70 struct connection **e;
Willy Tarreaue6f5ab52023-04-20 11:05:28 +020071 uint32_t idx = _HA_ATOMIC_LOAD(&ring->idx); /* (head << 16) + tail */
Willy Tarreau1efafce2019-01-27 15:37:19 +010072
Willy Tarreaue6f5ab52023-04-20 11:05:28 +020073 pos = idx >> 16;
74 if (pos == (uint16_t)idx)
Willy Tarreau83efc322020-10-14 17:37:17 +020075 return NULL;
Willy Tarreau1efafce2019-01-27 15:37:19 +010076
77 next = pos + 1;
78 if (next >= ACCEPT_QUEUE_SIZE)
79 next = 0;
80
81 e = &ring->entry[pos];
82
83 /* wait for the producer to update the listener's pointer */
84 while (1) {
Willy Tarreau83efc322020-10-14 17:37:17 +020085 ptr = *e;
Willy Tarreau1efafce2019-01-27 15:37:19 +010086 __ha_barrier_load();
87 if (ptr)
88 break;
89 pl_cpu_relax();
90 }
91
Willy Tarreau1efafce2019-01-27 15:37:19 +010092 /* release the entry */
Willy Tarreau83efc322020-10-14 17:37:17 +020093 *e = NULL;
Willy Tarreau1efafce2019-01-27 15:37:19 +010094
95 __ha_barrier_store();
Willy Tarreaue6f5ab52023-04-20 11:05:28 +020096 do {
97 pos = (next << 16) | (idx & 0xffff);
98 } while (unlikely(!HA_ATOMIC_CAS(&ring->idx, &idx, pos) && __ha_cpu_relax()));
99
Willy Tarreau83efc322020-10-14 17:37:17 +0200100 return ptr;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100101}
102
103
Willy Tarreau83efc322020-10-14 17:37:17 +0200104/* tries to push a new accepted connection <conn> into ring <ring>. Returns
105 * non-zero if it succeeds, or zero if the ring is full. Supports multiple
106 * producers.
Willy Tarreau1efafce2019-01-27 15:37:19 +0100107 */
Willy Tarreau83efc322020-10-14 17:37:17 +0200108int accept_queue_push_mp(struct accept_queue_ring *ring, struct connection *conn)
Willy Tarreau1efafce2019-01-27 15:37:19 +0100109{
Willy Tarreau1efafce2019-01-27 15:37:19 +0100110 unsigned int pos, next;
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200111 uint32_t idx = _HA_ATOMIC_LOAD(&ring->idx); /* (head << 16) + tail */
Willy Tarreau1efafce2019-01-27 15:37:19 +0100112
Willy Tarreau1efafce2019-01-27 15:37:19 +0100113 do {
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200114 pos = (uint16_t)idx;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100115 next = pos + 1;
116 if (next >= ACCEPT_QUEUE_SIZE)
117 next = 0;
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200118 if (next == (idx >> 16))
Willy Tarreau1efafce2019-01-27 15:37:19 +0100119 return 0; // ring full
Willy Tarreaue6f5ab52023-04-20 11:05:28 +0200120 next |= (idx & 0xffff0000U);
121 } while (unlikely(!_HA_ATOMIC_CAS(&ring->idx, &idx, next) && __ha_cpu_relax()));
Willy Tarreau1efafce2019-01-27 15:37:19 +0100122
Willy Tarreau83efc322020-10-14 17:37:17 +0200123 ring->entry[pos] = conn;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100124 __ha_barrier_store();
Willy Tarreau1efafce2019-01-27 15:37:19 +0100125 return 1;
126}
127
Willy Tarreaufb5401f2021-01-29 12:25:23 +0100128/* proceed with accepting new connections. Don't mark it static so that it appears
129 * in task dumps.
130 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100131struct task *accept_queue_process(struct task *t, void *context, unsigned int state)
Willy Tarreau1efafce2019-01-27 15:37:19 +0100132{
133 struct accept_queue_ring *ring = context;
Willy Tarreau83efc322020-10-14 17:37:17 +0200134 struct connection *conn;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100135 struct listener *li;
Christopher Faulet102854c2019-04-30 12:17:13 +0200136 unsigned int max_accept;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100137 int ret;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100138
Christopher Faulet102854c2019-04-30 12:17:13 +0200139 /* if global.tune.maxaccept is -1, then max_accept is UINT_MAX. It
140 * is not really illimited, but it is probably enough.
141 */
Willy Tarreau66161322021-02-19 15:50:27 +0100142 max_accept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Christopher Faulet102854c2019-04-30 12:17:13 +0200143 for (; max_accept; max_accept--) {
Willy Tarreau83efc322020-10-14 17:37:17 +0200144 conn = accept_queue_pop_sc(ring);
145 if (!conn)
Willy Tarreau1efafce2019-01-27 15:37:19 +0100146 break;
147
Willy Tarreau83efc322020-10-14 17:37:17 +0200148 li = __objt_listener(conn->target);
Willy Tarreaufea8c192023-02-28 10:25:57 +0100149 _HA_ATOMIC_INC(&li->thr_conn[ti->ltid]);
Willy Tarreau30836152023-01-12 19:10:17 +0100150 ret = li->bind_conf->accept(conn);
Willy Tarreau1efafce2019-01-27 15:37:19 +0100151 if (ret <= 0) {
152 /* connection was terminated by the application */
153 continue;
154 }
155
156 /* increase the per-process number of cumulated sessions, this
Willy Tarreau30836152023-01-12 19:10:17 +0100157 * may only be done once l->bind_conf->accept() has accepted the
158 * connection.
Willy Tarreau1efafce2019-01-27 15:37:19 +0100159 */
Willy Tarreau17146802023-01-12 19:58:42 +0100160 if (!(li->bind_conf->options & BC_O_UNLIMITED)) {
Willy Tarreau1efafce2019-01-27 15:37:19 +0100161 HA_ATOMIC_UPDATE_MAX(&global.sps_max,
162 update_freq_ctr(&global.sess_per_sec, 1));
Willy Tarreau11ba4042022-05-20 15:56:32 +0200163 if (li->bind_conf && li->bind_conf->options & BC_O_USE_SSL) {
Willy Tarreau1efafce2019-01-27 15:37:19 +0100164 HA_ATOMIC_UPDATE_MAX(&global.ssl_max,
165 update_freq_ctr(&global.ssl_per_sec, 1));
166 }
167 }
168 }
169
170 /* ran out of budget ? Let's come here ASAP */
Christopher Faulet102854c2019-04-30 12:17:13 +0200171 if (!max_accept)
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200172 tasklet_wakeup(ring->tasklet);
Willy Tarreau1efafce2019-01-27 15:37:19 +0100173
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200174 return NULL;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100175}
176
177/* Initializes the accept-queues. Returns 0 on success, otherwise ERR_* flags */
178static int accept_queue_init()
179{
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200180 struct tasklet *t;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100181 int i;
182
183 for (i = 0; i < global.nbthread; i++) {
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200184 t = tasklet_new();
Willy Tarreau1efafce2019-01-27 15:37:19 +0100185 if (!t) {
186 ha_alert("Out of memory while initializing accept queue for thread %d\n", i);
187 return ERR_FATAL|ERR_ABORT;
188 }
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200189 t->tid = i;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100190 t->process = accept_queue_process;
191 t->context = &accept_queue_rings[i];
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200192 accept_queue_rings[i].tasklet = t;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100193 }
194 return 0;
195}
196
197REGISTER_CONFIG_POSTPARSER("multi-threaded accept queue", accept_queue_init);
198
Willy Tarreaue01b08d2022-04-27 18:42:47 +0200199static void accept_queue_deinit()
200{
201 int i;
202
203 for (i = 0; i < global.nbthread; i++) {
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200204 tasklet_free(accept_queue_rings[i].tasklet);
Willy Tarreaue01b08d2022-04-27 18:42:47 +0200205 }
206}
207
208REGISTER_POST_DEINIT(accept_queue_deinit);
209
Willy Tarreau1efafce2019-01-27 15:37:19 +0100210#endif // USE_THREAD
211
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200212/* Memory allocation and initialization of the per_thr field (one entry per
213 * bound thread).
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100214 * Returns 0 if the field has been successfully initialized, -1 on failure.
215 */
216int li_init_per_thr(struct listener *li)
217{
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200218 int nbthr = MIN(global.nbthread, MAX_THREADS_PER_GROUP);
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100219 int i;
220
221 /* allocate per-thread elements for listener */
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200222 li->per_thr = calloc(nbthr, sizeof(*li->per_thr));
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100223 if (!li->per_thr)
224 return -1;
225
Willy Tarreau6a4d48b2023-04-21 10:46:45 +0200226 for (i = 0; i < nbthr; ++i) {
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100227 MT_LIST_INIT(&li->per_thr[i].quic_accept.list);
228 MT_LIST_INIT(&li->per_thr[i].quic_accept.conns);
229
230 li->per_thr[i].li = li;
231 }
232
233 return 0;
234}
235
William Dauchy3679d0c2021-02-14 23:22:55 +0100236/* helper to get listener status for stats */
237enum li_status get_li_status(struct listener *l)
238{
Willy Tarreau758c69d2023-01-12 18:59:37 +0100239 if (!l->bind_conf->maxconn || l->nbconn < l->bind_conf->maxconn) {
William Dauchy3679d0c2021-02-14 23:22:55 +0100240 if (l->state == LI_LIMITED)
241 return LI_STATUS_WAITING;
242 else
243 return LI_STATUS_OPEN;
244 }
245 return LI_STATUS_FULL;
246}
247
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200248/* adjust the listener's state and its proxy's listener counters if needed.
249 * It must be called under the listener's lock, but uses atomic ops to change
250 * the proxy's counters so that the proxy lock is not needed.
251 */
Willy Tarreaua37b2442020-09-24 07:23:45 +0200252void listener_set_state(struct listener *l, enum li_state st)
253{
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200254 struct proxy *px = l->bind_conf->frontend;
255
256 if (px) {
257 /* from state */
258 switch (l->state) {
259 case LI_NEW: /* first call */
Willy Tarreau4781b152021-04-06 13:53:36 +0200260 _HA_ATOMIC_INC(&px->li_all);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200261 break;
262 case LI_INIT:
263 case LI_ASSIGNED:
264 break;
265 case LI_PAUSED:
Willy Tarreau4781b152021-04-06 13:53:36 +0200266 _HA_ATOMIC_DEC(&px->li_paused);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200267 break;
268 case LI_LISTEN:
Willy Tarreau4781b152021-04-06 13:53:36 +0200269 _HA_ATOMIC_DEC(&px->li_bound);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200270 break;
271 case LI_READY:
272 case LI_FULL:
273 case LI_LIMITED:
Willy Tarreau4781b152021-04-06 13:53:36 +0200274 _HA_ATOMIC_DEC(&px->li_ready);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200275 break;
276 }
277
278 /* to state */
279 switch (st) {
280 case LI_NEW:
281 case LI_INIT:
282 case LI_ASSIGNED:
283 break;
284 case LI_PAUSED:
Willy Tarreau95a34602020-10-08 15:32:21 +0200285 BUG_ON(l->rx.fd == -1);
Willy Tarreau4781b152021-04-06 13:53:36 +0200286 _HA_ATOMIC_INC(&px->li_paused);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200287 break;
288 case LI_LISTEN:
Willy Tarreau95a34602020-10-08 15:32:21 +0200289 BUG_ON(l->rx.fd == -1);
Willy Tarreau4781b152021-04-06 13:53:36 +0200290 _HA_ATOMIC_INC(&px->li_bound);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200291 break;
292 case LI_READY:
293 case LI_FULL:
294 case LI_LIMITED:
Willy Tarreau95a34602020-10-08 15:32:21 +0200295 BUG_ON(l->rx.fd == -1);
Willy Tarreau4781b152021-04-06 13:53:36 +0200296 _HA_ATOMIC_INC(&px->li_ready);
Aurelien DARRAGON23705992023-02-14 08:51:14 +0100297 l->flags |= LI_F_FINALIZED;
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200298 break;
299 }
300 }
Willy Tarreaua37b2442020-09-24 07:23:45 +0200301 l->state = st;
302}
303
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100304/* This function adds the specified listener's file descriptor to the polling
305 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500306 * LI_FULL state depending on its number of connections. In daemon mode, we
Willy Tarreauae302532014-05-07 19:22:24 +0200307 * also support binding only the relevant processes to their respective
308 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100309 */
Willy Tarreau7834a3f2020-09-25 16:40:18 +0200310void enable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100311{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100312 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaud6afb532020-10-09 10:35:40 +0200313
314 /* If this listener is supposed to be only in the master, close it in
315 * the workers. Conversely, if it's supposed to be only in the workers
316 * close it in the master.
317 */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200318 if (!!master != !!(listener->rx.flags & RX_F_MWORKER))
Willy Tarreau75c98d12020-10-09 15:55:23 +0200319 do_unbind_listener(listener);
Willy Tarreaud6afb532020-10-09 10:35:40 +0200320
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100321 if (listener->state == LI_LISTEN) {
Willy Tarreau95a34602020-10-08 15:32:21 +0200322 BUG_ON(listener->rx.fd == -1);
William Lallemand095ba4c2017-06-01 17:38:50 +0200323 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau72faef32021-06-15 08:36:30 +0200324 (!!master != !!(listener->rx.flags & RX_F_MWORKER))) {
Willy Tarreauae302532014-05-07 19:22:24 +0200325 /* we don't want to enable this listener and don't
326 * want any fd event to reach it.
327 */
Willy Tarreau75c98d12020-10-09 15:55:23 +0200328 do_unbind_listener(listener);
Willy Tarreauae302532014-05-07 19:22:24 +0200329 }
Willy Tarreau758c69d2023-01-12 18:59:37 +0100330 else if (!listener->bind_conf->maxconn || listener->nbconn < listener->bind_conf->maxconn) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200331 listener->rx.proto->enable(listener);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200332 listener_set_state(listener, LI_READY);
Willy Tarreauae302532014-05-07 19:22:24 +0200333 }
334 else {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200335 listener_set_state(listener, LI_FULL);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100336 }
337 }
Willy Tarreaud6afb532020-10-09 10:35:40 +0200338
Willy Tarreau08b6f962022-02-01 16:23:00 +0100339 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100340}
341
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200342/*
Aurelien DARRAGON187396e2022-09-11 16:19:49 +0200343 * This function completely stops a listener.
344 * The proxy's listeners count is updated and the proxy is
345 * disabled and woken up after the last one is gone.
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100346 * It will need to operate under the proxy's lock, the protocol's lock and
347 * the listener's lock. The caller is responsible for indicating in lpx,
348 * lpr, lli whether the respective locks are already held (non-zero) or
349 * not (zero) so that the function picks the missing ones, in this order.
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200350 */
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100351void stop_listener(struct listener *l, int lpx, int lpr, int lli)
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200352{
353 struct proxy *px = l->bind_conf->frontend;
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200354
Willy Tarreau17146802023-01-12 19:58:42 +0100355 if (l->bind_conf->options & BC_O_NOSTOP) {
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200356 /* master-worker sockpairs are never closed but don't count as a
357 * job.
358 */
359 return;
360 }
361
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200362 if (!lpx && px)
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200363 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200364
365 if (!lpr)
366 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
367
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100368 if (!lli)
369 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200370
371 if (l->state > LI_INIT) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200372 do_unbind_listener(l);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200373
374 if (l->state >= LI_ASSIGNED)
375 __delete_listener(l);
376
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200377 if (px)
378 proxy_cond_disable(px);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200379 }
380
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100381 if (!lli)
382 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200383
384 if (!lpr)
385 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
386
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200387 if (!lpx && px)
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200388 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200389}
390
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100391/* This function adds the specified <listener> to the protocol <proto>. It
392 * does nothing if the protocol was already added. The listener's state is
393 * automatically updated from LI_INIT to LI_ASSIGNED. The number of listeners
394 * for the protocol is updated. This must be called with the proto lock held.
395 */
396void default_add_listener(struct protocol *proto, struct listener *listener)
397{
398 if (listener->state != LI_INIT)
399 return;
400 listener_set_state(listener, LI_ASSIGNED);
401 listener->rx.proto = proto;
Willy Tarreau2b718102021-04-21 07:32:39 +0200402 LIST_APPEND(&proto->receivers, &listener->rx.proto_list);
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100403 proto->nb_receivers++;
404}
405
Willy Tarreaue03204c2020-10-09 17:02:21 +0200406/* default function called to suspend a listener: it simply passes the call to
407 * the underlying receiver. This is find for most socket-based protocols. This
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100408 * must be called under the listener's lock. It will return < 0 in case of
409 * failure, 0 if the listener was totally stopped, or > 0 if correctly paused..
410 * If no receiver-level suspend is provided, the operation is assumed
411 * to succeed.
Willy Tarreaue03204c2020-10-09 17:02:21 +0200412 */
413int default_suspend_listener(struct listener *l)
414{
Willy Tarreaue03204c2020-10-09 17:02:21 +0200415 if (!l->rx.proto->rx_suspend)
416 return 1;
417
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100418 return l->rx.proto->rx_suspend(&l->rx);
Willy Tarreaue03204c2020-10-09 17:02:21 +0200419}
420
421
422/* Tries to resume a suspended listener, and returns non-zero on success or
423 * zero on failure. On certain errors, an alert or a warning might be displayed.
424 * It must be called with the listener's lock held. Depending on the listener's
425 * state and protocol, a listen() call might be used to resume operations, or a
426 * call to the receiver's resume() function might be used as well. This is
427 * suitable as a default function for TCP and UDP. This must be called with the
428 * listener's lock held.
429 */
430int default_resume_listener(struct listener *l)
431{
432 int ret = 1;
433
434 if (l->state == LI_ASSIGNED) {
435 char msg[100];
Aurelien DARRAGON046a75e2023-02-07 12:17:20 +0100436 char *errmsg;
Willy Tarreaue03204c2020-10-09 17:02:21 +0200437 int err;
438
Aurelien DARRAGON046a75e2023-02-07 12:17:20 +0100439 /* first, try to bind the receiver */
440 err = l->rx.proto->fam->bind(&l->rx, &errmsg);
441 if (err != ERR_NONE) {
442 if (err & ERR_WARN)
443 ha_warning("Resuming listener: %s\n", errmsg);
444 else if (err & ERR_ALERT)
445 ha_alert("Resuming listener: %s\n", errmsg);
446 ha_free(&errmsg);
447 if (err & (ERR_FATAL | ERR_ABORT)) {
448 ret = 0;
449 goto end;
450 }
451 }
452
453 /* then, try to listen:
454 * for now there's still always a listening function
455 * (same check performed in protocol_bind_all()
456 */
457 BUG_ON(!l->rx.proto->listen);
Willy Tarreaue03204c2020-10-09 17:02:21 +0200458 err = l->rx.proto->listen(l, msg, sizeof(msg));
459 if (err & ERR_ALERT)
460 ha_alert("Resuming listener: %s\n", msg);
461 else if (err & ERR_WARN)
462 ha_warning("Resuming listener: %s\n", msg);
463
464 if (err & (ERR_FATAL | ERR_ABORT)) {
465 ret = 0;
466 goto end;
467 }
468 }
469
470 if (l->state < LI_PAUSED) {
471 ret = 0;
472 goto end;
473 }
474
475 if (l->state == LI_PAUSED && l->rx.proto->rx_resume &&
476 l->rx.proto->rx_resume(&l->rx) <= 0)
477 ret = 0;
478 end:
479 return ret;
480}
481
482
Willy Tarreaube58c382011-07-24 18:28:10 +0200483/* This function tries to temporarily disable a listener, depending on the OS
484 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
485 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
486 * closes upon SHUT_WR and refuses to rebind. So a common validation path
487 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
488 * is disabled. It normally returns non-zero, unless an error is reported.
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100489 * suspend() may totally stop a listener if it doesn't support the PAUSED
490 * state, in which case state will be set to ASSIGNED.
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100491 * It will need to operate under the proxy's lock and the listener's lock.
492 * The caller is responsible for indicating in lpx, lli whether the respective
493 * locks are already held (non-zero) or not (zero) so that the function pick
494 * the missing ones, in this order.
Willy Tarreaube58c382011-07-24 18:28:10 +0200495 */
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100496int suspend_listener(struct listener *l, int lpx, int lli)
Willy Tarreaube58c382011-07-24 18:28:10 +0200497{
Willy Tarreau58651b42020-09-24 16:03:29 +0200498 struct proxy *px = l->bind_conf->frontend;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200499 int ret = 1;
500
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200501 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200502 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
503
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100504 if (!lli)
505 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200506
Aurelien DARRAGON23705992023-02-14 08:51:14 +0100507 if (!(l->flags & LI_F_FINALIZED) || l->state <= LI_PAUSED)
Willy Tarreau9b3a9322020-09-24 14:46:34 +0200508 goto end;
509
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100510 if (l->rx.proto->suspend) {
Willy Tarreaue03204c2020-10-09 17:02:21 +0200511 ret = l->rx.proto->suspend(l);
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100512 /* if the suspend() fails, we don't want to change the
513 * current listener state
514 */
515 if (ret < 0)
516 goto end;
517 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200518
Willy Tarreau2b718102021-04-21 07:32:39 +0200519 MT_LIST_DELETE(&l->wait_queue);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200520
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100521 /* ret == 0 means that the suspend() has been turned into
522 * an unbind(), meaning the listener is now stopped (ie: ABNS), we need
523 * to report this state change properly
524 */
525 listener_set_state(l, ((ret) ? LI_PAUSED : LI_ASSIGNED));
526
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100527 if (px && !(l->flags & LI_F_SUSPENDED))
528 px->li_suspended++;
529 l->flags |= LI_F_SUSPENDED;
530
Aurelien DARRAGON7a15fa52023-02-07 11:23:38 +0100531 /* at this point, everything is under control, no error should be
532 * returned to calling function
533 */
534 ret = 1;
Willy Tarreau58651b42020-09-24 16:03:29 +0200535
Aurelien DARRAGONca8a4b22023-02-07 12:36:27 +0100536 if (px && !(px->flags & PR_FL_PAUSED) && !px->li_ready) {
Aurelien DARRAGONd46f4372022-09-09 15:51:37 +0200537 /* PROXY_LOCK is required */
538 proxy_cond_pause(px);
Willy Tarreau58651b42020-09-24 16:03:29 +0200539 ha_warning("Paused %s %s.\n", proxy_cap_str(px->cap), px->id);
540 send_log(px, LOG_WARNING, "Paused %s %s.\n", proxy_cap_str(px->cap), px->id);
541 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200542 end:
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100543 if (!lli)
544 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200545
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200546 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200547 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
548
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200549 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200550}
551
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200552/* This function tries to resume a temporarily disabled listener. Paused, full,
553 * limited and disabled listeners are handled, which means that this function
554 * may replace enable_listener(). The resulting state will either be LI_READY
555 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200556 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200557 * foreground mode, and are ignored. If the listener was only in the assigned
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100558 * state, it's totally rebound. This can happen if a suspend() has completely
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200559 * stopped it. If the resume fails, 0 is returned and an error might be
560 * displayed.
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100561 * It will need to operate under the proxy's lock and the listener's lock.
562 * The caller is responsible for indicating in lpx, lli whether the respective
563 * locks are already held (non-zero) or not (zero) so that the function pick
564 * the missing ones, in this order.
Willy Tarreaube58c382011-07-24 18:28:10 +0200565 */
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100566int resume_listener(struct listener *l, int lpx, int lli)
Willy Tarreaube58c382011-07-24 18:28:10 +0200567{
Willy Tarreau58651b42020-09-24 16:03:29 +0200568 struct proxy *px = l->bind_conf->frontend;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200569 int ret = 1;
570
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200571 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200572 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
573
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100574 if (!lli)
575 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200576
Willy Tarreauf2cb1692019-07-11 10:08:31 +0200577 /* check that another thread didn't to the job in parallel (e.g. at the
578 * end of listen_accept() while we'd come from dequeue_all_listeners().
579 */
Willy Tarreau2b718102021-04-21 07:32:39 +0200580 if (MT_LIST_INLIST(&l->wait_queue))
Willy Tarreauf2cb1692019-07-11 10:08:31 +0200581 goto end;
582
Aurelien DARRAGON23705992023-02-14 08:51:14 +0100583 if (!(l->flags & LI_F_FINALIZED) || l->state == LI_READY)
Willy Tarreau5d7f9ce2020-09-24 18:54:11 +0200584 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200585
Aurelien DARRAGON3bb2a382023-02-07 13:26:14 +0100586 if (l->rx.proto->resume) {
Willy Tarreaue03204c2020-10-09 17:02:21 +0200587 ret = l->rx.proto->resume(l);
Aurelien DARRAGON3bb2a382023-02-07 13:26:14 +0100588 if (!ret)
589 goto end; /* failure to resume */
590 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200591
Willy Tarreau758c69d2023-01-12 18:59:37 +0100592 if (l->bind_conf->maxconn && l->nbconn >= l->bind_conf->maxconn) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200593 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200594 listener_set_state(l, LI_FULL);
Willy Tarreau58651b42020-09-24 16:03:29 +0200595 goto done;
Willy Tarreaube58c382011-07-24 18:28:10 +0200596 }
597
Willy Tarreau4b51f422020-09-25 20:32:28 +0200598 l->rx.proto->enable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200599 listener_set_state(l, LI_READY);
Willy Tarreau58651b42020-09-24 16:03:29 +0200600
601 done:
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100602 if (px && (l->flags & LI_F_SUSPENDED))
603 px->li_suspended--;
604 l->flags &= ~LI_F_SUSPENDED;
605
Aurelien DARRAGONca8a4b22023-02-07 12:36:27 +0100606 if (px && (px->flags & PR_FL_PAUSED) && !px->li_suspended) {
Aurelien DARRAGONd46f4372022-09-09 15:51:37 +0200607 /* PROXY_LOCK is required */
608 proxy_cond_resume(px);
Willy Tarreau58651b42020-09-24 16:03:29 +0200609 ha_warning("Resumed %s %s.\n", proxy_cap_str(px->cap), px->id);
610 send_log(px, LOG_WARNING, "Resumed %s %s.\n", proxy_cap_str(px->cap), px->id);
611 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200612 end:
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100613 if (!lli)
614 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200615
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +0200616 if (!lpx && px)
Aurelien DARRAGON00132882022-09-09 15:32:57 +0200617 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
618
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200619 return ret;
620}
621
Aurelien DARRAGONbcad7e62023-02-15 09:30:54 +0100622/* Same as resume_listener(), but will only work to resume from
623 * LI_FULL or LI_LIMITED states because we try to relax listeners that
624 * were temporarily restricted and not to resume inactive listeners that
625 * may have been paused or completely stopped in the meantime.
626 * Returns positive value for success and 0 for failure.
627 * It will need to operate under the proxy's lock and the listener's lock.
628 * The caller is responsible for indicating in lpx, lli whether the respective
629 * locks are already held (non-zero) or not (zero) so that the function pick
630 * the missing ones, in this order.
631 */
632int relax_listener(struct listener *l, int lpx, int lli)
633{
634 int ret = 1;
635
636 if (!lli)
637 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
638
639 if (l->state != LI_FULL && l->state != LI_LIMITED)
640 goto end; /* listener may be suspended or even stopped */
641 ret = resume_listener(l, lpx, 1);
642
643 end:
644 if (!lli)
645 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
646 return ret;
647}
648
Willy Tarreau87b09662015-04-03 00:22:06 +0200649/* Marks a ready listener as full so that the stream code tries to re-enable
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +0100650 * it upon next close() using relax_listener().
Willy Tarreau62793712011-07-24 19:23:38 +0200651 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200652static void listener_full(struct listener *l)
Willy Tarreau62793712011-07-24 19:23:38 +0200653{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100654 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200655 if (l->state >= LI_READY) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200656 MT_LIST_DELETE(&l->wait_queue);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100657 if (l->state != LI_FULL) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200658 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200659 listener_set_state(l, LI_FULL);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100660 }
Willy Tarreau62793712011-07-24 19:23:38 +0200661 }
Willy Tarreau08b6f962022-02-01 16:23:00 +0100662 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200663}
664
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200665/* Marks a ready listener as limited so that we only try to re-enable it when
666 * resources are free again. It will be queued into the specified queue.
667 */
Olivier Houchard859dc802019-08-08 15:47:21 +0200668static void limit_listener(struct listener *l, struct mt_list *list)
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200669{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100670 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200671 if (l->state == LI_READY) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200672 MT_LIST_TRY_APPEND(list, &l->wait_queue);
Willy Tarreau4b51f422020-09-25 20:32:28 +0200673 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200674 listener_set_state(l, LI_LIMITED);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200675 }
Willy Tarreau08b6f962022-02-01 16:23:00 +0100676 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200677}
678
Willy Tarreau241797a2019-12-10 14:10:52 +0100679/* Dequeues all listeners waiting for a resource the global wait queue */
680void dequeue_all_listeners()
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200681{
Willy Tarreau01abd022019-02-28 10:27:18 +0100682 struct listener *listener;
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200683
Willy Tarreau241797a2019-12-10 14:10:52 +0100684 while ((listener = MT_LIST_POP(&global_listener_queue, struct listener *, wait_queue))) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200685 /* This cannot fail because the listeners are by definition in
Willy Tarreau01abd022019-02-28 10:27:18 +0100686 * the LI_LIMITED state.
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200687 */
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +0100688 relax_listener(listener, 0, 0);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200689 }
690}
691
Willy Tarreau241797a2019-12-10 14:10:52 +0100692/* Dequeues all listeners waiting for a resource in proxy <px>'s queue */
693void dequeue_proxy_listeners(struct proxy *px)
694{
695 struct listener *listener;
696
697 while ((listener = MT_LIST_POP(&px->listener_queue, struct listener *, wait_queue))) {
698 /* This cannot fail because the listeners are by definition in
699 * the LI_LIMITED state.
700 */
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +0100701 relax_listener(listener, 0, 0);
Willy Tarreau241797a2019-12-10 14:10:52 +0100702 }
703}
704
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200705
706/* default function used to unbind a listener. This is for use by standard
707 * protocols working on top of accepted sockets. The receiver's rx_unbind()
708 * will automatically be used after the listener is disabled if the socket is
709 * still bound. This must be used under the listener's lock.
Christopher Faulet510c0d62018-03-16 10:04:47 +0100710 */
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200711void default_unbind_listener(struct listener *listener)
Willy Tarreaub648d632007-10-28 22:13:50 +0100712{
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200713 if (listener->state <= LI_ASSIGNED)
714 goto out_close;
715
716 if (listener->rx.fd == -1) {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200717 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200718 goto out_close;
719 }
720
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200721 if (listener->state >= LI_READY) {
722 listener->rx.proto->disable(listener);
723 if (listener->rx.flags & RX_F_BOUND)
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200724 listener_set_state(listener, LI_LISTEN);
Willy Tarreaub6607bf2020-09-23 16:24:23 +0200725 }
726
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200727 out_close:
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200728 if (listener->rx.flags & RX_F_BOUND)
729 listener->rx.proto->rx_unbind(&listener->rx);
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200730}
731
732/* This function closes the listening socket for the specified listener,
733 * provided that it's already in a listening state. The protocol's unbind()
734 * is called to put the listener into LI_ASSIGNED or LI_LISTEN and handle
735 * the unbinding tasks. The listener enters then the LI_ASSIGNED state if
736 * the receiver is unbound. Must be called with the lock held.
737 */
738void do_unbind_listener(struct listener *listener)
739{
Willy Tarreau2b718102021-04-21 07:32:39 +0200740 MT_LIST_DELETE(&listener->wait_queue);
Willy Tarreau7b2febd2020-10-09 17:18:29 +0200741
742 if (listener->rx.proto->unbind)
743 listener->rx.proto->unbind(listener);
Willy Tarreau374e9af2020-10-09 15:47:17 +0200744
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200745 /* we may have to downgrade the listener if the rx was closed */
746 if (!(listener->rx.flags & RX_F_BOUND) && listener->state > LI_ASSIGNED)
Willy Tarreau374e9af2020-10-09 15:47:17 +0200747 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100748}
749
Olivier Houchard1fc05162017-04-06 01:05:05 +0200750/* This function closes the listening socket for the specified listener,
751 * provided that it's already in a listening state. The listener enters the
Willy Tarreau75c98d12020-10-09 15:55:23 +0200752 * LI_ASSIGNED state, except if the FD is not closed, in which case it may
753 * remain in LI_LISTEN. This function is intended to be used as a generic
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100754 * function for standard protocols.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200755 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100756void unbind_listener(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200757{
Willy Tarreau08b6f962022-02-01 16:23:00 +0100758 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau75c98d12020-10-09 15:55:23 +0200759 do_unbind_listener(listener);
Willy Tarreau08b6f962022-02-01 16:23:00 +0100760 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200761}
762
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200763/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
764 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200765 * allocation). The address family is taken from ss->ss_family, and the protocol
Willy Tarreaud2fb99f2020-10-15 21:22:29 +0200766 * passed in <proto> must be usable on this family. The protocol's default iocb
767 * is automatically preset as the receivers' iocb. The number of jobs and
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200768 * listeners is automatically increased by the number of listeners created. It
769 * returns non-zero on success, zero on error with the error message set in <err>.
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200770 */
771int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200772 int portl, int porth, int fd, struct protocol *proto, char **err)
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200773{
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200774 struct listener *l;
775 int port;
776
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200777 for (port = portl; port <= porth; port++) {
778 l = calloc(1, sizeof(*l));
779 if (!l) {
780 memprintf(err, "out of memory");
781 return 0;
782 }
783 l->obj_type = OBJ_TYPE_LISTENER;
Willy Tarreau2b718102021-04-21 07:32:39 +0200784 LIST_APPEND(&bc->frontend->conf.listeners, &l->by_fe);
785 LIST_APPEND(&bc->listeners, &l->by_bind);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200786 l->bind_conf = bc;
Willy Tarreau0fce6bc2020-09-03 07:46:06 +0200787 l->rx.settings = &bc->settings;
Willy Tarreaueef45422020-09-03 10:05:03 +0200788 l->rx.owner = l;
Willy Tarreaud2fb99f2020-10-15 21:22:29 +0200789 l->rx.iocb = proto->default_iocb;
Willy Tarreau38ba6472020-08-27 08:16:52 +0200790 l->rx.fd = fd;
Willy Tarreau07400c52020-12-04 14:49:11 +0100791
Willy Tarreau37159062020-08-27 07:48:42 +0200792 memcpy(&l->rx.addr, ss, sizeof(*ss));
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100793 if (proto->fam->set_port)
794 proto->fam->set_port(&l->rx.addr, port);
Willy Tarreau07400c52020-12-04 14:49:11 +0100795
Olivier Houchard859dc802019-08-08 15:47:21 +0200796 MT_LIST_INIT(&l->wait_queue);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200797 listener_set_state(l, LI_INIT);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200798
Willy Tarreaud1f250f2020-12-04 15:03:36 +0100799 proto->add(proto, l);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200800
Willy Tarreau909c23b2020-09-15 13:50:58 +0200801 if (fd != -1)
Willy Tarreau43046fa2020-09-01 15:41:59 +0200802 l->rx.flags |= RX_F_INHERITED;
William Lallemand75ea0a02017-11-15 19:02:58 +0100803
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100804 l->extra_counters = NULL;
805
Willy Tarreau08b6f962022-02-01 16:23:00 +0100806 HA_RWLOCK_INIT(&l->lock);
Willy Tarreau4781b152021-04-06 13:53:36 +0200807 _HA_ATOMIC_INC(&jobs);
808 _HA_ATOMIC_INC(&listeners);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200809 }
810 return 1;
811}
812
Willy Tarreauaae18102023-03-01 18:25:58 +0100813/* Optionally allocates a new shard info (if si == NULL) for receiver rx and
814 * assigns it to it, or attaches to an existing one. If the rx already had a
815 * shard_info, it is simply returned. It is illegal to call this function with
816 * an rx that's part of a group that is already attached. Attaching means the
817 * shard_info's thread count and group count are updated so the rx's group is
818 * added to the shard_info's group mask. The rx are added to the members in the
819 * attachment order, though it must not matter. It is meant for boot time setup
820 * and is not thread safe. NULL is returned on allocation failure.
821 */
822struct shard_info *shard_info_attach(struct receiver *rx, struct shard_info *si)
823{
824 if (rx->shard_info)
825 return rx->shard_info;
826
827 if (!si) {
828 si = calloc(1, sizeof(*si));
829 if (!si)
830 return NULL;
831
832 si->ref = rx;
833 }
834
835 rx->shard_info = si;
836 BUG_ON (si->tgroup_mask & 1UL << (rx->bind_tgroup - 1));
837 si->tgroup_mask |= 1UL << (rx->bind_tgroup - 1);
838 si->nbgroups = my_popcountl(si->tgroup_mask);
839 si->nbthreads += my_popcountl(rx->bind_thread);
840 si->members[si->nbgroups - 1] = rx;
841 return si;
842}
843
844/* Detaches the rx from an optional shard_info it may be attached to. If so,
845 * the thread counts, group masks and refcounts are updated. The members list
846 * remains contiguous by replacing the current entry with the last one. The
847 * reference continues to point to the first receiver. If the group count
848 * reaches zero, the shard_info is automatically released.
849 */
850void shard_info_detach(struct receiver *rx)
851{
852 struct shard_info *si = rx->shard_info;
853 uint gr;
854
855 if (!si)
856 return;
857
858 rx->shard_info = NULL;
859
860 /* find the member slot this rx was attached to */
861 for (gr = 0; gr < MAX_TGROUPS && si->members[gr] != rx; gr++)
862 ;
863
864 BUG_ON(gr == MAX_TGROUPS);
865
866 si->nbthreads -= my_popcountl(rx->bind_thread);
867 si->tgroup_mask &= ~(1UL << (rx->bind_tgroup - 1));
868 si->nbgroups = my_popcountl(si->tgroup_mask);
869
870 /* replace the member by the last one. If we removed the reference, we
871 * have to switch to another one. It's always the first entry so we can
872 * simply enforce it upon every removal.
873 */
874 si->members[gr] = si->members[si->nbgroups];
875 si->members[si->nbgroups] = NULL;
876 si->ref = si->members[0];
877
878 if (!si->nbgroups)
879 free(si);
880}
881
Willy Tarreau59a877d2021-10-12 09:36:10 +0200882/* clones listener <src> and returns the new one. All dynamically allocated
883 * fields are reallocated (name for now). The new listener is inserted before
884 * the original one in the bind_conf and frontend lists. This allows it to be
885 * duplicated while iterating over the current list. The original listener must
886 * only be in the INIT or ASSIGNED states, and the new listener will only be
887 * placed into the INIT state. The counters are always set to NULL. Maxsock is
Willy Tarreauaae18102023-03-01 18:25:58 +0100888 * updated. Returns NULL on allocation error. The shard_info is never taken so
889 * that the caller can decide what to do with it depending on how it intends to
890 * clone the listener.
Willy Tarreau59a877d2021-10-12 09:36:10 +0200891 */
892struct listener *clone_listener(struct listener *src)
893{
894 struct listener *l;
895
896 l = calloc(1, sizeof(*l));
897 if (!l)
898 goto oom1;
899 memcpy(l, src, sizeof(*l));
900
901 if (l->name) {
902 l->name = strdup(l->name);
903 if (!l->name)
904 goto oom2;
905 }
906
907 l->rx.owner = l;
Willy Tarreauaae18102023-03-01 18:25:58 +0100908 l->rx.shard_info = NULL;
Willy Tarreau59a877d2021-10-12 09:36:10 +0200909 l->state = LI_INIT;
910 l->counters = NULL;
911 l->extra_counters = NULL;
912
913 LIST_APPEND(&src->by_fe, &l->by_fe);
914 LIST_APPEND(&src->by_bind, &l->by_bind);
915
916 MT_LIST_INIT(&l->wait_queue);
917
918 l->rx.proto->add(l->rx.proto, l);
919
Willy Tarreau08b6f962022-02-01 16:23:00 +0100920 HA_RWLOCK_INIT(&l->lock);
Willy Tarreau59a877d2021-10-12 09:36:10 +0200921 _HA_ATOMIC_INC(&jobs);
922 _HA_ATOMIC_INC(&listeners);
923 global.maxsock++;
924 return l;
925
Willy Tarreau59a877d2021-10-12 09:36:10 +0200926 oom2:
927 free(l);
928 oom1:
Willy Tarreaua1462892021-10-16 14:45:29 +0200929 return NULL;
Willy Tarreau59a877d2021-10-12 09:36:10 +0200930}
931
Willy Tarreau1a64d162007-10-28 22:26:05 +0100932/* Delete a listener from its protocol's list of listeners. The listener's
933 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
Willy Tarreau2cc5bae2017-09-15 08:18:11 +0200934 * number of listeners is updated, as well as the global number of listeners
935 * and jobs. Note that the listener must have previously been unbound. This
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200936 * is a low-level function expected to be called with the proto_lock and the
937 * listener's lock held.
Willy Tarreau1a64d162007-10-28 22:26:05 +0100938 */
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200939void __delete_listener(struct listener *listener)
Willy Tarreau1a64d162007-10-28 22:26:05 +0100940{
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100941 if (listener->state == LI_ASSIGNED) {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200942 listener_set_state(listener, LI_INIT);
Willy Tarreau2b718102021-04-21 07:32:39 +0200943 LIST_DELETE(&listener->rx.proto_list);
Willy Tarreauaae18102023-03-01 18:25:58 +0100944 shard_info_detach(&listener->rx);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200945 listener->rx.proto->nb_receivers--;
Willy Tarreau4781b152021-04-06 13:53:36 +0200946 _HA_ATOMIC_DEC(&jobs);
947 _HA_ATOMIC_DEC(&listeners);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100948 }
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200949}
950
951/* Delete a listener from its protocol's list of listeners (please check
952 * __delete_listener() above). The proto_lock and the listener's lock will
953 * be grabbed in this order.
954 */
955void delete_listener(struct listener *listener)
956{
957 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau08b6f962022-02-01 16:23:00 +0100958 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200959 __delete_listener(listener);
Willy Tarreau08b6f962022-02-01 16:23:00 +0100960 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau6ee9f8d2019-08-26 10:55:52 +0200961 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100962}
963
Willy Tarreaue2711c72019-02-27 15:39:41 +0100964/* Returns a suitable value for a listener's backlog. It uses the listener's,
965 * otherwise the frontend's backlog, otherwise the listener's maxconn,
966 * otherwise the frontend's maxconn, otherwise 1024.
967 */
968int listener_backlog(const struct listener *l)
969{
Willy Tarreau1920f892023-01-12 18:55:13 +0100970 if (l->bind_conf->backlog)
971 return l->bind_conf->backlog;
Willy Tarreaue2711c72019-02-27 15:39:41 +0100972
973 if (l->bind_conf->frontend->backlog)
974 return l->bind_conf->frontend->backlog;
975
Willy Tarreau758c69d2023-01-12 18:59:37 +0100976 if (l->bind_conf->maxconn)
977 return l->bind_conf->maxconn;
Willy Tarreaue2711c72019-02-27 15:39:41 +0100978
979 if (l->bind_conf->frontend->maxconn)
980 return l->bind_conf->frontend->maxconn;
981
982 return 1024;
983}
984
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200985/* This function is called on a read event from a listening socket, corresponding
986 * to an accept. It tries to accept as many connections as possible, and for each
987 * calls the listener's accept handler (generally the frontend's accept handler).
988 */
Willy Tarreaua74cb382020-10-15 21:29:49 +0200989void listener_accept(struct listener *l)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200990{
Willy Tarreau83efc322020-10-14 17:37:17 +0200991 struct connection *cli_conn;
Olivier Houchardd16a9df2019-02-25 16:18:16 +0100992 struct proxy *p;
Christopher Faulet102854c2019-04-30 12:17:13 +0200993 unsigned int max_accept;
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100994 int next_conn = 0;
Willy Tarreau82c97892019-02-27 19:32:32 +0100995 int next_feconn = 0;
996 int next_actconn = 0;
Willy Tarreaubb660302014-05-07 19:47:02 +0200997 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200998 int ret;
999
Olivier Houchardd16a9df2019-02-25 16:18:16 +01001000 p = l->bind_conf->frontend;
Christopher Faulet102854c2019-04-30 12:17:13 +02001001
Willy Tarreau882f2482023-01-12 18:52:23 +01001002 /* if l->bind_conf->maxaccept is -1, then max_accept is UINT_MAX. It is
1003 * not really illimited, but it is probably enough.
Christopher Faulet102854c2019-04-30 12:17:13 +02001004 */
Willy Tarreau882f2482023-01-12 18:52:23 +01001005 max_accept = l->bind_conf->maxaccept ? l->bind_conf->maxaccept : 1;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001006
Willy Tarreau17146802023-01-12 19:58:42 +01001007 if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.sps_lim) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001008 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +02001009
1010 if (unlikely(!max)) {
1011 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +02001012 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +01001013 goto limit_global;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001014 }
1015
1016 if (max_accept > max)
1017 max_accept = max;
1018 }
1019
Willy Tarreau17146802023-01-12 19:58:42 +01001020 if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001021 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
1022
1023 if (unlikely(!max)) {
1024 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +02001025 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +01001026 goto limit_global;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001027 }
1028
1029 if (max_accept > max)
1030 max_accept = max;
1031 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001032#ifdef USE_OPENSSL
Willy Tarreau17146802023-01-12 19:58:42 +01001033 if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.ssl_lim &&
Willy Tarreau11ba4042022-05-20 15:56:32 +02001034 l->bind_conf && l->bind_conf->options & BC_O_USE_SSL) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001035 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001036
Willy Tarreaue43d5322013-10-07 20:01:52 +02001037 if (unlikely(!max)) {
1038 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +02001039 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +01001040 goto limit_global;
Willy Tarreaue43d5322013-10-07 20:01:52 +02001041 }
1042
1043 if (max_accept > max)
1044 max_accept = max;
1045 }
1046#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001047 if (p && p->fe_sps_lim) {
1048 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
1049
1050 if (unlikely(!max)) {
1051 /* frontend accept rate limit was reached */
Willy Tarreau0591bf72019-12-10 12:01:21 +01001052 expire = tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0));
1053 goto limit_proxy;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001054 }
1055
1056 if (max_accept > max)
1057 max_accept = max;
1058 }
1059
1060 /* Note: if we fail to allocate a connection because of configured
1061 * limits, we'll schedule a new attempt worst 1 second later in the
1062 * worst case. If we fail due to system limits or temporary resource
1063 * shortage, we try again 100ms later in the worst case.
1064 */
Willy Tarreau02757d02021-01-28 18:07:24 +01001065 for (; max_accept; next_conn = next_feconn = next_actconn = 0, max_accept--) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001066 unsigned int count;
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001067 int status;
Willy Tarreau0aa5a5b2020-10-16 17:43:04 +02001068 __decl_thread(unsigned long mask);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001069
Willy Tarreau82c97892019-02-27 19:32:32 +01001070 /* pre-increase the number of connections without going too far.
1071 * We process the listener, then the proxy, then the process.
1072 * We know which ones to unroll based on the next_xxx value.
1073 */
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001074 do {
1075 count = l->nbconn;
Willy Tarreau758c69d2023-01-12 18:59:37 +01001076 if (unlikely(l->bind_conf->maxconn && count >= l->bind_conf->maxconn)) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001077 /* the listener was marked full or another
1078 * thread is going to do it.
1079 */
1080 next_conn = 0;
Willy Tarreau93604ed2019-11-15 10:20:07 +01001081 listener_full(l);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001082 goto end;
1083 }
1084 next_conn = count + 1;
David Carlier56716622019-03-27 16:08:42 +00001085 } while (!_HA_ATOMIC_CAS(&l->nbconn, (int *)(&count), next_conn));
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001086
Willy Tarreau82c97892019-02-27 19:32:32 +01001087 if (p) {
1088 do {
1089 count = p->feconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +01001090 if (unlikely(count >= p->maxconn)) {
Willy Tarreau82c97892019-02-27 19:32:32 +01001091 /* the frontend was marked full or another
1092 * thread is going to do it.
1093 */
1094 next_feconn = 0;
Willy Tarreau0591bf72019-12-10 12:01:21 +01001095 expire = TICK_ETERNITY;
1096 goto limit_proxy;
Willy Tarreau82c97892019-02-27 19:32:32 +01001097 }
1098 next_feconn = count + 1;
Olivier Houchard64213e92019-03-08 18:52:57 +01001099 } while (!_HA_ATOMIC_CAS(&p->feconn, &count, next_feconn));
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001100 }
1101
Willy Tarreau17146802023-01-12 19:58:42 +01001102 if (!(l->bind_conf->options & BC_O_UNLIMITED)) {
Willy Tarreau82c97892019-02-27 19:32:32 +01001103 do {
1104 count = actconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +01001105 if (unlikely(count >= global.maxconn)) {
Willy Tarreau82c97892019-02-27 19:32:32 +01001106 /* the process was marked full or another
1107 * thread is going to do it.
1108 */
1109 next_actconn = 0;
Willy Tarreau0591bf72019-12-10 12:01:21 +01001110 expire = tick_add(now_ms, 1000); /* try again in 1 second */
1111 goto limit_global;
Willy Tarreau82c97892019-02-27 19:32:32 +01001112 }
1113 next_actconn = count + 1;
David Carlier56716622019-03-27 16:08:42 +00001114 } while (!_HA_ATOMIC_CAS(&actconn, (int *)(&count), next_actconn));
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001115 }
1116
Willy Tarreaufed93d32022-02-01 16:37:00 +01001117 /* be careful below, the listener might be shutting down in
1118 * another thread on error and we must not dereference its
1119 * FD without a bit of protection.
1120 */
1121 cli_conn = NULL;
1122 status = CO_AC_PERMERR;
1123
1124 HA_RWLOCK_RDLOCK(LISTENER_LOCK, &l->lock);
1125 if (l->rx.flags & RX_F_BOUND)
1126 cli_conn = l->rx.proto->accept_conn(l, &status);
1127 HA_RWLOCK_RDUNLOCK(LISTENER_LOCK, &l->lock);
1128
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001129 if (!cli_conn) {
1130 switch (status) {
1131 case CO_AC_DONE:
1132 goto end;
Willy Tarreau818dca52014-01-31 19:40:19 +01001133
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001134 case CO_AC_RETRY: /* likely a signal */
Willy Tarreau4781b152021-04-06 13:53:36 +02001135 _HA_ATOMIC_DEC(&l->nbconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001136 if (p)
Willy Tarreau4781b152021-04-06 13:53:36 +02001137 _HA_ATOMIC_DEC(&p->feconn);
Willy Tarreau17146802023-01-12 19:58:42 +01001138 if (!(l->bind_conf->options & BC_O_UNLIMITED))
Willy Tarreau4781b152021-04-06 13:53:36 +02001139 _HA_ATOMIC_DEC(&actconn);
Willy Tarreaua593ec52014-01-20 21:21:30 +01001140 continue;
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001141
1142 case CO_AC_YIELD:
Willy Tarreau92079932019-12-10 09:30:05 +01001143 max_accept = 0;
1144 goto end;
William Lallemandd9138002018-11-27 12:02:39 +01001145
Willy Tarreau9378bbe2020-10-15 10:09:31 +02001146 default:
1147 goto transient_error;
Willy Tarreau83efc322020-10-14 17:37:17 +02001148 }
1149 }
1150
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001151 /* The connection was accepted, it must be counted as such */
1152 if (l->counters)
1153 HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, next_conn);
1154
Willy Tarreaud8679342022-05-09 20:41:54 +02001155 if (p) {
Willy Tarreau82c97892019-02-27 19:32:32 +01001156 HA_ATOMIC_UPDATE_MAX(&p->fe_counters.conn_max, next_feconn);
Willy Tarreaud8679342022-05-09 20:41:54 +02001157 proxy_inc_fe_conn_ctr(l, p);
1158 }
Willy Tarreau82c97892019-02-27 19:32:32 +01001159
Willy Tarreau17146802023-01-12 19:58:42 +01001160 if (!(l->bind_conf->options & BC_O_UNLIMITED)) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001161 count = update_freq_ctr(&global.conn_per_sec, 1);
1162 HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001163 }
1164
Willy Tarreau4781b152021-04-06 13:53:36 +02001165 _HA_ATOMIC_INC(&activity[tid].accepted);
Willy Tarreau64a9c052019-04-12 15:27:17 +02001166
Willy Tarreau30836152023-01-12 19:10:17 +01001167 /* past this point, l->bind_conf->accept() will automatically decrement
Willy Tarreau82c97892019-02-27 19:32:32 +01001168 * l->nbconn, feconn and actconn once done. Setting next_*conn=0
1169 * allows the error path not to rollback on nbconn. It's more
1170 * convenient than duplicating all exit labels.
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001171 */
1172 next_conn = 0;
Willy Tarreau82c97892019-02-27 19:32:32 +01001173 next_feconn = 0;
1174 next_actconn = 0;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001175
Willy Tarreau83efc322020-10-14 17:37:17 +02001176
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001177#if defined(USE_THREAD)
Willy Tarreau9d360602023-03-27 10:38:51 +02001178 if (!(global.tune.options & GTUNE_LISTENER_MQ_ANY) || stopping)
1179 goto local_accept;
1180
1181 /* we want to perform thread rebalancing if the listener is
1182 * bound to more than one thread or if it's part of a shard
1183 * with more than one listener.
1184 */
Willy Tarreaub2f38c12023-01-19 19:14:18 +01001185 mask = l->rx.bind_thread & _HA_ATOMIC_LOAD(&tg->threads_enabled);
Willy Tarreau9d360602023-03-27 10:38:51 +02001186 if (l->rx.shard_info || atleast2(mask)) {
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001187 struct accept_queue_ring *ring;
Willy Tarreau9d360602023-03-27 10:38:51 +02001188 struct listener *new_li;
Willy Tarreauff185042023-04-20 16:52:21 +02001189 uint r1, r2, t, t1, t2;
1190 ulong n0, n1;
Willy Tarreau9d360602023-03-27 10:38:51 +02001191 const struct tgroup_info *g1, *g2;
1192 ulong m1, m2;
Willy Tarreauff185042023-04-20 16:52:21 +02001193 ulong *thr_idx_ptr;
Willy Tarreaufc630bd2019-03-04 19:57:34 +01001194
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001195 /* The principle is that we have two running indexes,
1196 * each visiting in turn all threads bound to this
Willy Tarreau9d360602023-03-27 10:38:51 +02001197 * listener's shard. The connection will be assigned to
1198 * the one with the least connections, and the other
1199 * one will be updated. This provides a good fairness
1200 * on short connections (round robin) and on long ones
1201 * (conn count), without ever missing any idle thread.
1202 * Each thread number is encoded as a combination of
1203 * times the receiver number and its local thread
1204 * number from 0 to MAX_THREADS_PER_GROUP - 1. The two
Willy Tarreauff185042023-04-20 16:52:21 +02001205 * indexes are stored as 10/12 bit numbers in the thr_idx
1206 * array, since there are up to LONGBITS threads and
1207 * groups that can be represented. They are represented
1208 * like this:
1209 * 31:20 19:15 14:10 9:5 4:0
1210 * 32b: [ counter | r2num | t2num | r1num | t1num ]
1211 *
1212 * 63:24 23:18 17:12 11:6 5:0
1213 * 64b: [ counter | r2num | t2num | r1num | t1num ]
1214 *
1215 * The change counter is only used to avoid swapping too
1216 * old a value when the value loops back.
Willy Tarreau9d360602023-03-27 10:38:51 +02001217 *
1218 * In the loop below we have this for each index:
1219 * - n is the thread index
1220 * - r is the receiver number
1221 * - g is the receiver's thread group
1222 * - t is the thread number in this receiver
1223 * - m is the receiver's thread mask shifted by the thread number
Willy Tarreaufc630bd2019-03-04 19:57:34 +01001224 */
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001225
1226 /* keep a copy for the final update. thr_idx is composite
Willy Tarreau9d360602023-03-27 10:38:51 +02001227 * and made of (n2<<16) + n1.
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001228 */
Willy Tarreaub6574922023-03-29 17:02:17 +02001229 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 +02001230 while (1) {
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001231 int q0, q1, q2;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001232
Willy Tarreauff185042023-04-20 16:52:21 +02001233 /* calculate r1/g1/t1 first (ascending idx) */
1234 n0 = _HA_ATOMIC_LOAD(thr_idx_ptr);
Willy Tarreau9d360602023-03-27 10:38:51 +02001235 new_li = NULL;
1236
Willy Tarreauff185042023-04-20 16:52:21 +02001237 t1 = (uint)n0 & (LONGBITS - 1);
1238 r1 = ((uint)n0 / LONGBITS) & (LONGBITS - 1);
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001239
Willy Tarreau9d360602023-03-27 10:38:51 +02001240 while (1) {
1241 if (l->rx.shard_info) {
1242 /* multiple listeners, take the group into account */
1243 if (r1 >= l->rx.shard_info->nbgroups)
1244 r1 = 0;
1245
1246 g1 = &ha_tgroup_info[l->rx.shard_info->members[r1]->bind_tgroup - 1];
1247 m1 = l->rx.shard_info->members[r1]->bind_thread;
1248 } else {
1249 /* single listener */
1250 r1 = 0;
1251 g1 = tg;
1252 m1 = l->rx.bind_thread;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001253 }
Willy Tarreau9d360602023-03-27 10:38:51 +02001254 m1 &= _HA_ATOMIC_LOAD(&g1->threads_enabled);
1255 m1 >>= t1;
1256
1257 /* find first existing thread */
1258 if (unlikely(!(m1 & 1))) {
1259 m1 &= ~1UL;
1260 if (!m1) {
1261 /* no more threads here, switch to
1262 * first thread of next group.
1263 */
1264 t1 = 0;
1265 if (l->rx.shard_info)
1266 r1++;
1267 /* loop again */
1268 continue;
1269 }
1270 t1 += my_ffsl(m1) - 1;
1271 }
1272 /* done: r1 and t1 are OK */
1273 break;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001274 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001275
Willy Tarreauff185042023-04-20 16:52:21 +02001276 /* now r2/g2/t2 (descending idx) */
1277 t2 = ((uint)n0 / LONGBITS / LONGBITS) & (LONGBITS - 1);
1278 r2 = ((uint)n0 / LONGBITS / LONGBITS / LONGBITS) & (LONGBITS - 1);
Willy Tarreau9d360602023-03-27 10:38:51 +02001279
Willy Tarreau84fe1f42023-04-20 15:40:38 +02001280 /* if running in round-robin mode ("fair"), we don't need
1281 * to go further.
1282 */
1283 if ((global.tune.options & GTUNE_LISTENER_MQ_ANY) == GTUNE_LISTENER_MQ_FAIR) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001284 t = g1->base + t1;
1285 if (l->rx.shard_info && t != tid)
1286 new_li = l->rx.shard_info->members[r1]->owner;
Willy Tarreau84fe1f42023-04-20 15:40:38 +02001287 goto updt_t1;
1288 }
1289
Willy Tarreau9d360602023-03-27 10:38:51 +02001290 while (1) {
1291 if (l->rx.shard_info) {
1292 /* multiple listeners, take the group into account */
1293 if (r2 >= l->rx.shard_info->nbgroups)
1294 r2 = l->rx.shard_info->nbgroups - 1;
Willy Tarreau85d04242019-04-16 18:09:13 +02001295
Willy Tarreau9d360602023-03-27 10:38:51 +02001296 g2 = &ha_tgroup_info[l->rx.shard_info->members[r2]->bind_tgroup - 1];
1297 m2 = l->rx.shard_info->members[r2]->bind_thread;
1298 } else {
1299 /* single listener */
1300 r2 = 0;
1301 g2 = tg;
1302 m2 = l->rx.bind_thread;
1303 }
1304 m2 &= _HA_ATOMIC_LOAD(&g2->threads_enabled);
1305 m2 &= nbits(t2 + 1);
1306
1307 /* find previous existing thread */
1308 if (unlikely(!(m2 & (1UL << t2)) || (g1 == g2 && t1 == t2))) {
1309 /* highest bit not set or colliding threads, let's check
1310 * if we still have other threads available after this
1311 * one.
1312 */
1313 m2 &= ~(1UL << t2);
1314 if (!m2) {
1315 /* no more threads here, switch to
1316 * last thread of previous group.
1317 */
1318 t2 = MAX_THREADS_PER_GROUP - 1;
1319 if (l->rx.shard_info)
1320 r2--;
1321 /* loop again */
1322 continue;
1323 }
1324 t2 = my_flsl(m2) - 1;
1325 }
1326 /* done: r2 and t2 are OK */
1327 break;
Willy Tarreau85d04242019-04-16 18:09:13 +02001328 }
1329
Willy Tarreau77e33502023-04-19 17:19:28 +02001330 /* tests show that it's worth checking that other threads have not
1331 * already changed the index to save the rest of the calculation,
1332 * or we'd have to redo it anyway.
1333 */
Willy Tarreauff185042023-04-20 16:52:21 +02001334 if (n0 != _HA_ATOMIC_LOAD(thr_idx_ptr))
Willy Tarreau77e33502023-04-19 17:19:28 +02001335 continue;
Willy Tarreau77e33502023-04-19 17:19:28 +02001336
Willy Tarreau9d360602023-03-27 10:38:51 +02001337 /* here we have (r1,g1,t1) that designate the first receiver, its
1338 * thread group and local thread, and (r2,g2,t2) that designate
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001339 * the second receiver, its thread group and local thread. We'll
1340 * also consider the local thread with q0.
Willy Tarreau9d360602023-03-27 10:38:51 +02001341 */
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001342 q0 = accept_queue_ring_len(&accept_queue_rings[tid]);
Willy Tarreau9d360602023-03-27 10:38:51 +02001343 q1 = accept_queue_ring_len(&accept_queue_rings[g1->base + t1]);
1344 q2 = accept_queue_ring_len(&accept_queue_rings[g2->base + t2]);
1345
1346 /* add to this the currently active connections */
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001347 q0 += _HA_ATOMIC_LOAD(&l->thr_conn[ti->ltid]);
Willy Tarreau9d360602023-03-27 10:38:51 +02001348 if (l->rx.shard_info) {
1349 q1 += _HA_ATOMIC_LOAD(&((struct listener *)l->rx.shard_info->members[r1]->owner)->thr_conn[t1]);
1350 q2 += _HA_ATOMIC_LOAD(&((struct listener *)l->rx.shard_info->members[r2]->owner)->thr_conn[t2]);
1351 } else {
1352 q1 += _HA_ATOMIC_LOAD(&l->thr_conn[t1]);
1353 q2 += _HA_ATOMIC_LOAD(&l->thr_conn[t2]);
1354 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001355
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001356 /* we have 3 possibilities now :
1357 * q1 < q2 : t1 is less loaded than t2, so we pick it
1358 * and update t2 (since t1 might still be
1359 * lower than another thread)
1360 * q1 > q2 : t2 is less loaded than t1, so we pick it
1361 * and update t1 (since t2 might still be
1362 * lower than another thread)
1363 * q1 = q2 : both are equally loaded, thus we pick t1
1364 * and update t1 as it will become more loaded
1365 * than t2.
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001366 * On top of that, if in the end the current thread appears
1367 * to be as good of a deal, we'll prefer it over a foreign
1368 * one as it will improve locality and avoid a migration.
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001369 */
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001370
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001371 if (q1 - q2 < 0) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001372 t = g1->base + t1;
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001373 if (q0 <= q1)
1374 t = tid;
Willy Tarreau9d360602023-03-27 10:38:51 +02001375
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001376 if (l->rx.shard_info && t != tid)
Willy Tarreau9d360602023-03-27 10:38:51 +02001377 new_li = l->rx.shard_info->members[r1]->owner;
1378
1379 t2--;
1380 if (t2 >= MAX_THREADS_PER_GROUP) {
1381 if (l->rx.shard_info)
1382 r2--;
1383 t2 = MAX_THREADS_PER_GROUP - 1;
1384 }
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001385 }
1386 else if (q1 - q2 > 0) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001387 t = g2->base + t2;
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001388 if (q0 <= q2)
1389 t = tid;
Willy Tarreau9d360602023-03-27 10:38:51 +02001390
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001391 if (l->rx.shard_info && t != tid)
Willy Tarreau9d360602023-03-27 10:38:51 +02001392 new_li = l->rx.shard_info->members[r2]->owner;
1393 goto updt_t1;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001394 }
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001395 else { // q1 == q2
Willy Tarreau9d360602023-03-27 10:38:51 +02001396 t = g1->base + t1;
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001397 if (q0 < q1) // local must be strictly better than both
1398 t = tid;
Willy Tarreau9d360602023-03-27 10:38:51 +02001399
Willy Tarreau8adffaa2023-04-19 18:06:16 +02001400 if (l->rx.shard_info && t != tid)
Willy Tarreau9d360602023-03-27 10:38:51 +02001401 new_li = l->rx.shard_info->members[r1]->owner;
Willy Tarreau84fe1f42023-04-20 15:40:38 +02001402 updt_t1:
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001403 t1++;
Willy Tarreau9d360602023-03-27 10:38:51 +02001404 if (t1 >= MAX_THREADS_PER_GROUP) {
1405 if (l->rx.shard_info)
1406 r1++;
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001407 t1 = 0;
Willy Tarreau9d360602023-03-27 10:38:51 +02001408 }
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001409 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001410
Willy Tarreauff185042023-04-20 16:52:21 +02001411 /* The target thread number is in <t> now. Let's
1412 * compute the new index and try to update it.
1413 */
Willy Tarreau9d360602023-03-27 10:38:51 +02001414
Willy Tarreauff185042023-04-20 16:52:21 +02001415 /* take previous counter and increment it */
1416 n1 = n0 & -(ulong)(LONGBITS * LONGBITS * LONGBITS * LONGBITS);
1417 n1 += LONGBITS * LONGBITS * LONGBITS * LONGBITS;
1418 n1 += (((r2 * LONGBITS) + t2) * LONGBITS * LONGBITS);
1419 n1 += (r1 * LONGBITS) + t1;
Willy Tarreaub6574922023-03-29 17:02:17 +02001420 if (likely(_HA_ATOMIC_CAS(thr_idx_ptr, &n0, n1)))
Willy Tarreau9d360602023-03-27 10:38:51 +02001421 break;
Willy Tarreauff185042023-04-20 16:52:21 +02001422
1423 /* bah we lost the race, try again */
1424 __ha_cpu_relax();
Willy Tarreau9d360602023-03-27 10:38:51 +02001425 } /* end of main while() loop */
1426
1427 /* we may need to update the listener in the connection
1428 * if we switched to another group.
1429 */
1430 if (new_li)
1431 cli_conn->target = &new_li->obj_type;
1432
1433 /* here we have the target thread number in <t> and we hold a
1434 * reservation in the target ring.
1435 */
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001436
Amaury Denoyellea66e0432023-04-05 18:16:28 +02001437 if (l->rx.proto && l->rx.proto->set_affinity) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001438 if (l->rx.proto->set_affinity(cli_conn, t)) {
Amaury Denoyellea66e0432023-04-05 18:16:28 +02001439 /* Failed migration, stay on the same thread. */
1440 goto local_accept;
1441 }
1442 }
1443
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001444 /* We successfully selected the best thread "t" for this
1445 * connection. We use deferred accepts even if it's the
1446 * local thread because tests show that it's the best
1447 * performing model, likely due to better cache locality
1448 * when processing this loop.
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001449 */
Willy Tarreau9d360602023-03-27 10:38:51 +02001450 ring = &accept_queue_rings[t];
Willy Tarreau83efc322020-10-14 17:37:17 +02001451 if (accept_queue_push_mp(ring, cli_conn)) {
Willy Tarreau9d360602023-03-27 10:38:51 +02001452 _HA_ATOMIC_INC(&activity[t].accq_pushed);
Willy Tarreau2bd65a72019-09-24 06:55:18 +02001453 tasklet_wakeup(ring->tasklet);
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001454 continue;
1455 }
1456 /* If the ring is full we do a synchronous accept on
1457 * the local thread here.
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001458 */
Willy Tarreau9d360602023-03-27 10:38:51 +02001459 _HA_ATOMIC_INC(&activity[t].accq_full);
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001460 }
1461#endif // USE_THREAD
1462
Amaury Denoyelle7f7713d2022-01-19 11:37:50 +01001463 local_accept:
Willy Tarreau9d360602023-03-27 10:38:51 +02001464 /* restore the connection's listener in case we failed to migrate above */
1465 cli_conn->target = &l->obj_type;
Willy Tarreaufea8c192023-02-28 10:25:57 +01001466 _HA_ATOMIC_INC(&l->thr_conn[ti->ltid]);
Willy Tarreau30836152023-01-12 19:10:17 +01001467 ret = l->bind_conf->accept(cli_conn);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001468 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001469 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001470 * we just have to ignore it (ret == 0) or it's a critical
1471 * error due to a resource shortage, and we must stop the
1472 * listener (ret < 0).
1473 */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001474 if (ret == 0) /* successful termination */
1475 continue;
1476
Willy Tarreaubb660302014-05-07 19:47:02 +02001477 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001478 }
1479
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001480 /* increase the per-process number of cumulated sessions, this
Willy Tarreau30836152023-01-12 19:10:17 +01001481 * may only be done once l->bind_conf->accept() has accepted the
1482 * connection.
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001483 */
Willy Tarreau17146802023-01-12 19:58:42 +01001484 if (!(l->bind_conf->options & BC_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001485 count = update_freq_ctr(&global.sess_per_sec, 1);
1486 HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
Willy Tarreau93e7c002013-10-07 18:51:07 +02001487 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001488#ifdef USE_OPENSSL
Willy Tarreau17146802023-01-12 19:58:42 +01001489 if (!(l->bind_conf->options & BC_O_UNLIMITED) &&
Willy Tarreau11ba4042022-05-20 15:56:32 +02001490 l->bind_conf && l->bind_conf->options & BC_O_USE_SSL) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001491 count = update_freq_ctr(&global.ssl_per_sec, 1);
1492 HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
Willy Tarreaue43d5322013-10-07 20:01:52 +02001493 }
1494#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +02001495
Willy Tarreaubdcd3252022-06-22 09:19:46 +02001496 _HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_STUCK); // this thread is still running
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001497 } /* end of for (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001498
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001499 end:
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001500 if (next_conn)
Willy Tarreau4781b152021-04-06 13:53:36 +02001501 _HA_ATOMIC_DEC(&l->nbconn);
Willy Tarreau741b4d62019-02-25 15:02:04 +01001502
Willy Tarreau82c97892019-02-27 19:32:32 +01001503 if (p && next_feconn)
Willy Tarreau4781b152021-04-06 13:53:36 +02001504 _HA_ATOMIC_DEC(&p->feconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001505
1506 if (next_actconn)
Willy Tarreau4781b152021-04-06 13:53:36 +02001507 _HA_ATOMIC_DEC(&actconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001508
Willy Tarreau758c69d2023-01-12 18:59:37 +01001509 if ((l->state == LI_FULL && (!l->bind_conf->maxconn || l->nbconn < l->bind_conf->maxconn)) ||
Willy Tarreau02757d02021-01-28 18:07:24 +01001510 (l->state == LI_LIMITED &&
Willy Tarreaucdcba112019-12-11 15:06:30 +01001511 ((!p || p->feconn < p->maxconn) && (actconn < global.maxconn) &&
1512 (!tick_isset(global_listener_queue_task->expire) ||
1513 tick_is_expired(global_listener_queue_task->expire, now_ms))))) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001514 /* at least one thread has to this when quitting */
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +01001515 relax_listener(l, 0, 0);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001516
Willy Tarreau02757d02021-01-28 18:07:24 +01001517 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001518 dequeue_all_listeners();
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001519
Olivier Houchard859dc802019-08-08 15:47:21 +02001520 if (p && !MT_LIST_ISEMPTY(&p->listener_queue) &&
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001521 (!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 +01001522 dequeue_proxy_listeners(p);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001523 }
Willy Tarreau0591bf72019-12-10 12:01:21 +01001524 return;
1525
1526 transient_error:
1527 /* pause the listener for up to 100 ms */
1528 expire = tick_add(now_ms, 100);
1529
Willy Tarreau258b3512020-10-13 17:46:05 +02001530 /* This may be a shared socket that was paused by another process.
1531 * Let's put it to pause in this case.
1532 */
1533 if (l->rx.proto && l->rx.proto->rx_listening(&l->rx) == 0) {
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +01001534 suspend_listener(l, 0, 0);
Willy Tarreau258b3512020-10-13 17:46:05 +02001535 goto end;
1536 }
1537
Willy Tarreau0591bf72019-12-10 12:01:21 +01001538 limit_global:
1539 /* (re-)queue the listener to the global queue and set it to expire no
1540 * later than <expire> ahead. The listener turns to LI_LIMITED.
1541 */
1542 limit_listener(l, &global_listener_queue);
Christopher Faulet13e86d92022-11-17 14:40:20 +01001543 HA_RWLOCK_RDLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001544 task_schedule(global_listener_queue_task, expire);
Christopher Faulet13e86d92022-11-17 14:40:20 +01001545 HA_RWLOCK_RDUNLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001546 goto end;
1547
1548 limit_proxy:
1549 /* (re-)queue the listener to the proxy's queue and set it to expire no
1550 * later than <expire> ahead. The listener turns to LI_LIMITED.
1551 */
1552 limit_listener(l, &p->listener_queue);
Willy Tarreaueeea8082020-01-08 19:15:07 +01001553 if (p->task && tick_isset(expire))
1554 task_schedule(p->task, expire);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001555 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001556}
1557
Willy Tarreau05f50472017-09-15 09:19:58 +02001558/* Notify the listener that a connection initiated from it was released. This
1559 * is used to keep the connection count consistent and to possibly re-open
1560 * listening when it was limited.
1561 */
1562void listener_release(struct listener *l)
1563{
1564 struct proxy *fe = l->bind_conf->frontend;
1565
Willy Tarreau17146802023-01-12 19:58:42 +01001566 if (!(l->bind_conf->options & BC_O_UNLIMITED))
Willy Tarreau4781b152021-04-06 13:53:36 +02001567 _HA_ATOMIC_DEC(&actconn);
Willy Tarreau82c97892019-02-27 19:32:32 +01001568 if (fe)
Willy Tarreau4781b152021-04-06 13:53:36 +02001569 _HA_ATOMIC_DEC(&fe->feconn);
1570 _HA_ATOMIC_DEC(&l->nbconn);
Willy Tarreaufea8c192023-02-28 10:25:57 +01001571 _HA_ATOMIC_DEC(&l->thr_conn[ti->ltid]);
Willy Tarreau82c97892019-02-27 19:32:32 +01001572
1573 if (l->state == LI_FULL || l->state == LI_LIMITED)
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +01001574 relax_listener(l, 0, 0);
Willy Tarreau05f50472017-09-15 09:19:58 +02001575
Willy Tarreau02757d02021-01-28 18:07:24 +01001576 /* Dequeues all of the listeners waiting for a resource */
1577 dequeue_all_listeners();
1578
Aurelien DARRAGONa57786e2022-09-12 09:26:21 +02001579 if (fe && !MT_LIST_ISEMPTY(&fe->listener_queue) &&
Willy Tarreau05f50472017-09-15 09:19:58 +02001580 (!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 +01001581 dequeue_proxy_listeners(fe);
Willy Tarreau05f50472017-09-15 09:19:58 +02001582}
1583
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001584/* Initializes the listener queues. Returns 0 on success, otherwise ERR_* flags */
1585static int listener_queue_init()
1586{
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001587 global_listener_queue_task = task_new_anywhere();
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001588 if (!global_listener_queue_task) {
1589 ha_alert("Out of memory when initializing global listener queue\n");
1590 return ERR_FATAL|ERR_ABORT;
1591 }
1592 /* very simple initialization, users will queue the task if needed */
1593 global_listener_queue_task->context = NULL; /* not even a context! */
1594 global_listener_queue_task->process = manage_global_listener_queue;
Christopher Faulet13e86d92022-11-17 14:40:20 +01001595 HA_RWLOCK_INIT(&global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001596
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001597 return 0;
1598}
1599
1600static void listener_queue_deinit()
1601{
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001602 task_destroy(global_listener_queue_task);
1603 global_listener_queue_task = NULL;
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001604}
1605
1606REGISTER_CONFIG_POSTPARSER("multi-threaded listener queue", listener_queue_init);
1607REGISTER_POST_DEINIT(listener_queue_deinit);
1608
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001609
1610/* This is the global management task for listeners. It enables listeners waiting
1611 * for global resources when there are enough free resource, or at least once in
Willy Tarreaud597ec22021-01-29 14:29:06 +01001612 * a while. It is designed to be called as a task. It's exported so that it's easy
1613 * to spot in "show tasks" or "show profiling".
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001614 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001615struct task *manage_global_listener_queue(struct task *t, void *context, unsigned int state)
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001616{
1617 /* If there are still too many concurrent connections, let's wait for
1618 * some of them to go away. We don't need to re-arm the timer because
1619 * each of them will scan the queue anyway.
1620 */
1621 if (unlikely(actconn >= global.maxconn))
1622 goto out;
1623
1624 /* We should periodically try to enable listeners waiting for a global
1625 * resource here, because it is possible, though very unlikely, that
1626 * they have been blocked by a temporary lack of global resource such
1627 * as a file descriptor or memory and that the temporary condition has
1628 * disappeared.
1629 */
1630 dequeue_all_listeners();
1631
1632 out:
Christopher Faulet13e86d92022-11-17 14:40:20 +01001633 HA_RWLOCK_WRLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001634 t->expire = TICK_ETERNITY;
Christopher Faulet13e86d92022-11-17 14:40:20 +01001635 HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &global_listener_rwlock);
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001636 return t;
1637}
1638
Willy Tarreauf6a84442023-04-22 23:25:38 +02001639/* Applies the thread mask, shards etc to the bind_conf. It normally returns 0
1640 * otherwie the number of errors. Upon error it may set error codes (ERR_*) in
1641 * err_code. It is supposed to be called only once very late in the boot process
1642 * after the bind_conf's thread_set is fixed. The function may emit warnings and
1643 * alerts. Extra listeners may be created on the fly.
1644 */
1645int bind_complete_thread_setup(struct bind_conf *bind_conf, int *err_code)
1646{
1647 struct proxy *fe = bind_conf->frontend;
1648 struct listener *li, *new_li, *ref;
1649 struct thread_set new_ts;
1650 int shard, shards, todo, done, grp, dups;
1651 ulong mask, gmask, bit;
1652 int cfgerr = 0;
1653 char *err;
1654
1655 err = NULL;
1656 if (thread_resolve_group_mask(&bind_conf->thread_set, (fe == global.cli_fe) ? 1 : 0, &err) < 0) {
Willy Tarreaua22db652023-04-22 23:52:17 +02001657 ha_alert("%s '%s': %s in 'bind %s' at [%s:%d].\n",
1658 proxy_type_str(fe),
Willy Tarreauf6a84442023-04-22 23:25:38 +02001659 fe->id, err, bind_conf->arg, bind_conf->file, bind_conf->line);
1660 free(err);
1661 cfgerr++;
1662 return cfgerr;
1663 }
1664
1665 /* apply thread masks and groups to all receivers */
1666 list_for_each_entry(li, &bind_conf->listeners, by_bind) {
1667 shards = bind_conf->settings.shards;
1668 todo = thread_set_count(&bind_conf->thread_set);
1669
1670 /* special values: -1 = "by-thread", -2 = "by-group" */
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001671 if (shards == -1) {
1672 if (li->rx.proto->flags & PROTO_F_REUSEPORT_SUPPORTED)
1673 shards = todo;
1674 else {
1675 if (fe != global.cli_fe)
1676 ha_diag_warning("[%s:%d]: Disabling per-thread sharding for listener in"
1677 " %s '%s' because SO_REUSEPORT is disabled\n",
1678 bind_conf->file, bind_conf->line, proxy_type_str(fe), fe->id);
1679 shards = 1;
1680 }
1681 }
Willy Tarreauf6a84442023-04-22 23:25:38 +02001682 else if (shards == -2)
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001683 shards = (li->rx.proto->flags & PROTO_F_REUSEPORT_SUPPORTED) ? my_popcountl(bind_conf->thread_set.grps) : 1;
Willy Tarreauf6a84442023-04-22 23:25:38 +02001684
1685 /* no more shards than total threads */
1686 if (shards > todo)
1687 shards = todo;
1688
Willy Tarreauc1fbdd62023-04-22 11:38:55 +02001689 /* We also need to check if an explicit shards count was set and cannot be honored */
1690 if (shards > 1 && !(li->rx.proto->flags & PROTO_F_REUSEPORT_SUPPORTED)) {
1691 ha_warning("[%s:%d]: Disabling sharding for listener in %s '%s' because SO_REUSEPORT is disabled\n",
1692 bind_conf->file, bind_conf->line, proxy_type_str(fe), fe->id);
1693 shards = 1;
1694 }
1695
Willy Tarreauf6a84442023-04-22 23:25:38 +02001696 shard = done = grp = bit = mask = 0;
1697 new_li = li;
1698
1699 while (shard < shards) {
1700 memset(&new_ts, 0, sizeof(new_ts));
1701 while (grp < global.nbtgroups && done < todo) {
1702 /* enlarge mask to cover next bit of bind_thread till we
1703 * have enough bits for one shard. We restart from the
1704 * current grp+bit.
1705 */
1706
1707 /* first let's find the first non-empty group starting at <mask> */
1708 if (!(bind_conf->thread_set.rel[grp] & ha_tgroup_info[grp].threads_enabled & ~mask)) {
1709 grp++;
1710 mask = 0;
1711 continue;
1712 }
1713
1714 /* take next unassigned bit */
1715 bit = (bind_conf->thread_set.rel[grp] & ~mask) & -(bind_conf->thread_set.rel[grp] & ~mask);
1716 new_ts.rel[grp] |= bit;
1717 mask |= bit;
1718 new_ts.grps |= 1UL << grp;
1719
1720 done += shards;
1721 };
1722
1723 BUG_ON(!new_ts.grps); // no more bits left unassigned
1724
1725 /* Create all required listeners for all bound groups. If more than one group is
1726 * needed, the first receiver serves as a reference, and subsequent ones point to
1727 * it. We already have a listener available in new_li() so we only allocate a new
1728 * one if we're not on the last one. We count the remaining groups by copying their
1729 * mask into <gmask> and dropping the lowest bit at the end of the loop until there
1730 * is no more. Ah yes, it's not pretty :-/
1731 */
1732 ref = new_li;
1733 gmask = new_ts.grps;
1734 for (dups = 0; gmask; dups++) {
1735 /* assign the first (and only) thread and group */
1736 new_li->rx.bind_thread = thread_set_nth_tmask(&new_ts, dups);
1737 new_li->rx.bind_tgroup = thread_set_nth_group(&new_ts, dups);
1738
1739 if (dups) {
1740 /* it has been allocated already in the previous round */
1741 shard_info_attach(&new_li->rx, ref->rx.shard_info);
1742 new_li->rx.flags |= RX_F_MUST_DUP;
1743 }
1744
1745 gmask &= gmask - 1; // drop lowest bit
1746 if (gmask) {
1747 /* yet another listener expected in this shard, let's
1748 * chain it.
1749 */
1750 struct listener *tmp_li = clone_listener(new_li);
1751
1752 if (!tmp_li) {
1753 ha_alert("Out of memory while trying to allocate extra listener for group %u of shard %d in %s %s\n",
1754 new_li->rx.bind_tgroup, shard, proxy_type_str(fe), fe->id);
1755 cfgerr++;
1756 *err_code |= ERR_FATAL | ERR_ALERT;
1757 return cfgerr;
1758 }
1759
1760 /* if we're forced to create at least two listeners, we have to
1761 * allocate a shared shard_info that's linked to from the reference
1762 * and each other listener, so we'll create it here.
1763 */
1764 if (!shard_info_attach(&ref->rx, NULL)) {
1765 ha_alert("Out of memory while trying to allocate shard_info for listener for group %u of shard %d in %s %s\n",
1766 new_li->rx.bind_tgroup, shard, proxy_type_str(fe), fe->id);
1767 cfgerr++;
1768 *err_code |= ERR_FATAL | ERR_ALERT;
1769 return cfgerr;
1770 }
1771 new_li = tmp_li;
1772 }
1773 }
1774 done -= todo;
1775
1776 shard++;
1777 if (shard >= shards)
1778 break;
1779
1780 /* create another listener for new shards */
1781 new_li = clone_listener(li);
1782 if (!new_li) {
1783 ha_alert("Out of memory while trying to allocate extra listener for shard %d in %s %s\n",
1784 shard, proxy_type_str(fe), fe->id);
1785 cfgerr++;
1786 *err_code |= ERR_FATAL | ERR_ALERT;
1787 return cfgerr;
1788 }
1789 }
1790 }
1791
1792 /* success */
1793 return cfgerr;
1794}
1795
Willy Tarreau26982662012-09-12 23:17:10 +02001796/*
1797 * Registers the bind keyword list <kwl> as a list of valid keywords for next
1798 * parsing sessions.
1799 */
1800void bind_register_keywords(struct bind_kw_list *kwl)
1801{
Willy Tarreau2b718102021-04-21 07:32:39 +02001802 LIST_APPEND(&bind_keywords.list, &kwl->list);
Willy Tarreau26982662012-09-12 23:17:10 +02001803}
1804
1805/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
1806 * keyword is found with a NULL ->parse() function, then an attempt is made to
1807 * find one with a valid ->parse() function. This way it is possible to declare
1808 * platform-dependant, known keywords as NULL, then only declare them as valid
1809 * if some options are met. Note that if the requested keyword contains an
1810 * opening parenthesis, everything from this point is ignored.
1811 */
1812struct bind_kw *bind_find_kw(const char *kw)
1813{
1814 int index;
1815 const char *kwend;
1816 struct bind_kw_list *kwl;
1817 struct bind_kw *ret = NULL;
1818
1819 kwend = strchr(kw, '(');
1820 if (!kwend)
1821 kwend = kw + strlen(kw);
1822
1823 list_for_each_entry(kwl, &bind_keywords.list, list) {
1824 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1825 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1826 kwl->kw[index].kw[kwend-kw] == 0) {
1827 if (kwl->kw[index].parse)
1828 return &kwl->kw[index]; /* found it !*/
1829 else
1830 ret = &kwl->kw[index]; /* may be OK */
1831 }
1832 }
1833 }
1834 return ret;
1835}
1836
Willy Tarreau8638f482012-09-18 18:01:17 +02001837/* Dumps all registered "bind" keywords to the <out> string pointer. The
1838 * unsupported keywords are only dumped if their supported form was not
1839 * found.
1840 */
1841void bind_dump_kws(char **out)
1842{
1843 struct bind_kw_list *kwl;
1844 int index;
1845
Christopher Faulet784063e2020-05-18 12:14:18 +02001846 if (!out)
1847 return;
1848
Willy Tarreau8638f482012-09-18 18:01:17 +02001849 *out = NULL;
1850 list_for_each_entry(kwl, &bind_keywords.list, list) {
1851 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1852 if (kwl->kw[index].parse ||
1853 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +02001854 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
1855 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +02001856 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +02001857 kwl->kw[index].skip ? " <arg>" : "",
1858 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +02001859 }
1860 }
1861 }
1862}
1863
Willy Tarreau433b05f2021-03-12 10:14:07 +01001864/* Try to find in srv_keyword the word that looks closest to <word> by counting
1865 * transitions between letters, digits and other characters. Will return the
1866 * best matching word if found, otherwise NULL.
1867 */
1868const char *bind_find_best_kw(const char *word)
1869{
1870 uint8_t word_sig[1024];
1871 uint8_t list_sig[1024];
1872 const struct bind_kw_list *kwl;
1873 const char *best_ptr = NULL;
1874 int dist, best_dist = INT_MAX;
1875 int index;
1876
1877 make_word_fingerprint(word_sig, word);
1878 list_for_each_entry(kwl, &bind_keywords.list, list) {
1879 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1880 make_word_fingerprint(list_sig, kwl->kw[index].kw);
1881 dist = word_fingerprint_distance(word_sig, list_sig);
1882 if (dist < best_dist) {
1883 best_dist = dist;
1884 best_ptr = kwl->kw[index].kw;
1885 }
1886 }
1887 }
1888
1889 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
1890 best_ptr = NULL;
1891
1892 return best_ptr;
1893}
1894
Willy Tarreaudbf78022021-10-06 09:05:08 +02001895/* allocate an bind_conf struct for a bind line, and chain it to the frontend <fe>.
1896 * If <arg> is not NULL, it is duplicated into ->arg to store useful config
1897 * information for error reporting. NULL is returned on error.
1898 */
1899struct bind_conf *bind_conf_alloc(struct proxy *fe, const char *file,
1900 int line, const char *arg, struct xprt_ops *xprt)
1901{
1902 struct bind_conf *bind_conf = calloc(1, sizeof(*bind_conf));
1903
1904 if (!bind_conf)
1905 goto err;
1906
1907 bind_conf->file = strdup(file);
1908 if (!bind_conf->file)
1909 goto err;
1910 bind_conf->line = line;
1911 if (arg) {
1912 bind_conf->arg = strdup(arg);
1913 if (!bind_conf->arg)
1914 goto err;
1915 }
1916
1917 LIST_APPEND(&fe->conf.bind, &bind_conf->by_fe);
1918 bind_conf->settings.ux.uid = -1;
1919 bind_conf->settings.ux.gid = -1;
1920 bind_conf->settings.ux.mode = 0;
Willy Tarreau6dfbef42021-10-12 15:23:03 +02001921 bind_conf->settings.shards = 1;
Willy Tarreaudbf78022021-10-06 09:05:08 +02001922 bind_conf->xprt = xprt;
1923 bind_conf->frontend = fe;
Willy Tarreau7866e8e2023-01-12 18:39:42 +01001924 bind_conf->analysers = fe->fe_req_ana;
Willy Tarreaudbf78022021-10-06 09:05:08 +02001925 bind_conf->severity_output = CLI_SEVERITY_NONE;
1926#ifdef USE_OPENSSL
1927 HA_RWLOCK_INIT(&bind_conf->sni_lock);
1928 bind_conf->sni_ctx = EB_ROOT;
1929 bind_conf->sni_w_ctx = EB_ROOT;
1930#endif
1931 LIST_INIT(&bind_conf->listeners);
1932 return bind_conf;
1933
1934 err:
1935 if (bind_conf) {
1936 ha_free(&bind_conf->file);
1937 ha_free(&bind_conf->arg);
1938 }
1939 ha_free(&bind_conf);
1940 return NULL;
1941}
1942
1943const char *listener_state_str(const struct listener *l)
1944{
1945 static const char *states[8] = {
1946 "NEW", "INI", "ASS", "PAU", "LIS", "RDY", "FUL", "LIM",
1947 };
1948 unsigned int st = l->state;
1949
1950 if (st >= sizeof(states) / sizeof(*states))
1951 return "INVALID";
1952 return states[st];
1953}
1954
Willy Tarreau645513a2010-05-24 20:55:15 +02001955/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001956/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +02001957/************************************************************************/
1958
Willy Tarreaua5e37562011-12-16 17:06:15 +01001959/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +02001960static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001961smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001962{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001963 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001964 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +02001965 return 1;
1966}
1967
Willy Tarreaua5e37562011-12-16 17:06:15 +01001968/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +02001969static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001970smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001971{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001972 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001973 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +02001974 return 1;
1975}
Jerome Magnineb421b22020-03-27 22:08:40 +01001976static int
1977smp_fetch_so_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
1978{
1979 smp->data.u.str.area = smp->sess->listener->name;
1980 if (!smp->data.u.str.area)
1981 return 0;
1982
1983 smp->data.type = SMP_T_STR;
1984 smp->flags = SMP_F_CONST;
1985 smp->data.u.str.data = strlen(smp->data.u.str.area);
1986 return 1;
1987}
Willy Tarreau645513a2010-05-24 20:55:15 +02001988
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001989/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001990static 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 +02001991{
Willy Tarreauf1b47302023-01-12 19:48:50 +01001992 conf->options |= BC_O_ACC_PROXY;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001993 return 0;
1994}
1995
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001996/* parse the "accept-netscaler-cip" bind keyword */
1997static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1998{
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001999 uint32_t val;
2000
2001 if (!*args[cur_arg + 1]) {
2002 memprintf(err, "'%s' : missing value", args[cur_arg]);
2003 return ERR_ALERT | ERR_FATAL;
2004 }
2005
2006 val = atol(args[cur_arg + 1]);
2007 if (val <= 0) {
Willy Tarreaue2711c72019-02-27 15:39:41 +01002008 memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002009 return ERR_ALERT | ERR_FATAL;
2010 }
2011
Willy Tarreauf1b47302023-01-12 19:48:50 +01002012 conf->options |= BC_O_ACC_CIP;
2013 conf->ns_cip_magic = val;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002014 return 0;
2015}
2016
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002017/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002018static 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 +02002019{
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002020 int val;
2021
2022 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002023 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002024 return ERR_ALERT | ERR_FATAL;
2025 }
2026
2027 val = atol(args[cur_arg + 1]);
Willy Tarreaue2711c72019-02-27 15:39:41 +01002028 if (val < 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002029 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002030 return ERR_ALERT | ERR_FATAL;
2031 }
2032
Willy Tarreau1920f892023-01-12 18:55:13 +01002033 conf->backlog = val;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002034 return 0;
2035}
2036
2037/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002038static 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 +02002039{
2040 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002041 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +01002042 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002043
Willy Tarreau4348fad2012-09-20 16:48:07 +02002044 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002045 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002046 return ERR_ALERT | ERR_FATAL;
2047 }
2048
2049 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002050 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002051 return ERR_ALERT | ERR_FATAL;
2052 }
2053
Willy Tarreau4348fad2012-09-20 16:48:07 +02002054 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +01002055 new->luid = strtol(args[cur_arg + 1], &error, 10);
2056 if (*error != '\0') {
2057 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
2058 return ERR_ALERT | ERR_FATAL;
2059 }
Willy Tarreau4348fad2012-09-20 16:48:07 +02002060 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002061
Willy Tarreau4348fad2012-09-20 16:48:07 +02002062 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002063 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002064 return ERR_ALERT | ERR_FATAL;
2065 }
2066
Willy Tarreau4348fad2012-09-20 16:48:07 +02002067 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002068 if (node) {
2069 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002070 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
2071 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
2072 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002073 return ERR_ALERT | ERR_FATAL;
2074 }
2075
Willy Tarreau4348fad2012-09-20 16:48:07 +02002076 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002077 return 0;
2078}
2079
Willy Tarreau3882d2a2022-05-20 15:41:45 +02002080/* Complete a bind_conf by parsing the args after the address. <args> is the
2081 * arguments array, <cur_arg> is the first one to be considered. <section> is
2082 * the section name to report in error messages, and <file> and <linenum> are
2083 * the file name and line number respectively. Note that args[0..1] are used
2084 * in error messages to provide some context. The return value is an error
2085 * code, zero on success or an OR of ERR_{FATAL,ABORT,ALERT,WARN}.
2086 */
2087int bind_parse_args_list(struct bind_conf *bind_conf, char **args, int cur_arg, const char *section, const char *file, int linenum)
2088{
2089 int err_code = 0;
2090
2091 while (*(args[cur_arg])) {
2092 struct bind_kw *kw;
2093 const char *best;
2094
2095 kw = bind_find_kw(args[cur_arg]);
2096 if (kw) {
2097 char *err = NULL;
2098 int code;
2099
2100 if (!kw->parse) {
2101 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : '%s' option is not implemented in this version (check build options).\n",
2102 file, linenum, args[0], args[1], section, args[cur_arg]);
2103 cur_arg += 1 + kw->skip ;
2104 err_code |= ERR_ALERT | ERR_FATAL;
2105 goto out;
2106 }
2107
2108 code = kw->parse(args, cur_arg, bind_conf->frontend, bind_conf, &err);
2109 err_code |= code;
2110
2111 if (code) {
2112 if (err && *err) {
2113 indent_msg(&err, 2);
2114 if (((code & (ERR_WARN|ERR_ALERT)) == ERR_WARN))
2115 ha_warning("parsing [%s:%d] : '%s %s' in section '%s' : %s\n", file, linenum, args[0], args[1], section, err);
2116 else
2117 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : %s\n", file, linenum, args[0], args[1], section, err);
2118 }
2119 else
2120 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : error encountered while processing '%s'.\n",
2121 file, linenum, args[0], args[1], section, args[cur_arg]);
2122 if (code & ERR_FATAL) {
2123 free(err);
2124 cur_arg += 1 + kw->skip;
2125 goto out;
2126 }
2127 }
2128 free(err);
2129 cur_arg += 1 + kw->skip;
2130 continue;
2131 }
2132
2133 best = bind_find_best_kw(args[cur_arg]);
2134 if (best)
2135 ha_alert("parsing [%s:%d] : '%s %s' in section '%s': unknown keyword '%s'; did you mean '%s' maybe ?\n",
2136 file, linenum, args[0], args[1], section, args[cur_arg], best);
2137 else
2138 ha_alert("parsing [%s:%d] : '%s %s' in section '%s': unknown keyword '%s'.\n",
2139 file, linenum, args[0], args[1], section, args[cur_arg]);
2140
2141 err_code |= ERR_ALERT | ERR_FATAL;
2142 goto out;
2143 }
Willy Tarreau64306cc2022-05-20 16:20:52 +02002144
2145 if ((bind_conf->options & (BC_O_USE_SOCK_DGRAM|BC_O_USE_SOCK_STREAM)) == (BC_O_USE_SOCK_DGRAM|BC_O_USE_SOCK_STREAM) ||
2146 (bind_conf->options & (BC_O_USE_XPRT_DGRAM|BC_O_USE_XPRT_STREAM)) == (BC_O_USE_XPRT_DGRAM|BC_O_USE_XPRT_STREAM)) {
2147 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : cannot mix datagram and stream protocols.\n",
2148 file, linenum, args[0], args[1], section);
2149 err_code |= ERR_ALERT | ERR_FATAL;
2150 goto out;
2151 }
2152
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002153 /* The transport layer automatically switches to QUIC when QUIC is
2154 * selected, regardless of bind_conf settings. We then need to
2155 * initialize QUIC params.
2156 */
2157 if ((bind_conf->options & (BC_O_USE_SOCK_DGRAM|BC_O_USE_XPRT_STREAM)) == (BC_O_USE_SOCK_DGRAM|BC_O_USE_XPRT_STREAM)) {
2158#ifdef USE_QUIC
2159 bind_conf->xprt = xprt_get(XPRT_QUIC);
Willy Tarreau287f32f2022-05-20 18:16:52 +02002160 if (!(bind_conf->options & BC_O_USE_SSL)) {
2161 bind_conf->options |= BC_O_USE_SSL;
2162 ha_warning("parsing [%s:%d] : '%s %s' in section '%s' : QUIC protocol detected, enabling ssl. Use 'ssl' to shut this warning.\n",
2163 file, linenum, args[0], args[1], section);
2164 }
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002165 quic_transport_params_init(&bind_conf->quic_params, 1);
2166#else
2167 ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : QUIC protocol selected but support not compiled in (check build options).\n",
2168 file, linenum, args[0], args[1], section);
2169 err_code |= ERR_ALERT | ERR_FATAL;
2170 goto out;
2171#endif
2172 }
Willy Tarreau2071a992022-05-20 17:14:31 +02002173 else if (bind_conf->options & BC_O_USE_SSL) {
2174 bind_conf->xprt = xprt_get(XPRT_SSL);
2175 }
Willy Tarreau78d0dcd2022-05-20 17:10:00 +02002176
Willy Tarreau3882d2a2022-05-20 15:41:45 +02002177 out:
2178 return err_code;
2179}
2180
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002181/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002182static 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 +02002183{
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002184 int val;
2185
2186 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002187 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002188 return ERR_ALERT | ERR_FATAL;
2189 }
2190
2191 val = atol(args[cur_arg + 1]);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01002192 if (val < 0) {
2193 memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002194 return ERR_ALERT | ERR_FATAL;
2195 }
2196
Willy Tarreau758c69d2023-01-12 18:59:37 +01002197 conf->maxconn = val;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002198 return 0;
2199}
2200
2201/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002202static 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 +02002203{
2204 struct listener *l;
2205
2206 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002207 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002208 return ERR_ALERT | ERR_FATAL;
2209 }
2210
Willy Tarreau4348fad2012-09-20 16:48:07 +02002211 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002212 l->name = strdup(args[cur_arg + 1]);
2213
2214 return 0;
2215}
2216
2217/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002218static 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 +02002219{
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002220 int val;
2221
2222 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002223 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002224 return ERR_ALERT | ERR_FATAL;
2225 }
2226
2227 val = atol(args[cur_arg + 1]);
2228 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002229 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002230 return ERR_ALERT | ERR_FATAL;
2231 }
2232
Willy Tarreau7dbd4182023-01-12 19:32:45 +01002233 conf->nice = val;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002234 return 0;
2235}
2236
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02002237/* parse the "process" bind keyword */
2238static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2239{
Willy Tarreauacd64412022-07-15 17:16:01 +02002240 memprintf(err, "'process %s' on 'bind' lines is not supported anymore, please use 'thread' instead.", args[cur_arg+1]);
2241 return ERR_ALERT | ERR_FATAL;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02002242}
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002243
Christopher Fauleta717b992018-04-10 14:43:00 +02002244/* parse the "proto" bind keyword */
2245static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2246{
2247 struct ist proto;
2248
2249 if (!*args[cur_arg + 1]) {
2250 memprintf(err, "'%s' : missing value", args[cur_arg]);
2251 return ERR_ALERT | ERR_FATAL;
2252 }
2253
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002254 proto = ist(args[cur_arg + 1]);
Christopher Fauleta717b992018-04-10 14:43:00 +02002255 conf->mux_proto = get_mux_proto(proto);
2256 if (!conf->mux_proto) {
2257 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]);
2258 return ERR_ALERT | ERR_FATAL;
2259 }
Willy Tarreauc8cac042021-09-21 14:31:29 +02002260 return 0;
2261}
2262
Willy Tarreaua07635e2023-04-13 17:25:43 +02002263/* parse the "shards" bind keyword. Takes an integer, "by-thread", or "by-group" */
Willy Tarreau6dfbef42021-10-12 15:23:03 +02002264static int bind_parse_shards(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2265{
2266 int val;
2267
2268 if (!*args[cur_arg + 1]) {
2269 memprintf(err, "'%s' : missing value", args[cur_arg]);
2270 return ERR_ALERT | ERR_FATAL;
2271 }
2272
2273 if (strcmp(args[cur_arg + 1], "by-thread") == 0) {
Willy Tarreaud30e82b2023-04-13 17:11:23 +02002274 val = -1; /* -1 = "by-thread", will be fixed in check_config_validity() */
Willy Tarreaua07635e2023-04-13 17:25:43 +02002275 } else if (strcmp(args[cur_arg + 1], "by-group") == 0) {
2276 val = -2; /* -2 = "by-group", will be fixed in check_config_validity() */
Willy Tarreau6dfbef42021-10-12 15:23:03 +02002277 } else {
2278 val = atol(args[cur_arg + 1]);
2279 if (val < 1 || val > MAX_THREADS) {
2280 memprintf(err, "'%s' : invalid value %d, allowed range is %d..%d or 'by-thread'", args[cur_arg], val, 1, MAX_THREADS);
2281 return ERR_ALERT | ERR_FATAL;
2282 }
2283 }
2284
2285 conf->settings.shards = val;
2286 return 0;
2287}
2288
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01002289/* parse the "thread" bind keyword. This will replace any preset thread_set */
Willy Tarreauc8cac042021-09-21 14:31:29 +02002290static int bind_parse_thread(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2291{
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01002292 /* note that the thread set is zeroed before first call, and we don't
2293 * want to reset it so that it remains possible to chain multiple
2294 * "thread" directives.
2295 */
2296 if (parse_thread_set(args[cur_arg+1], &conf->thread_set, err) < 0)
Willy Tarreauc8cac042021-09-21 14:31:29 +02002297 return ERR_ALERT | ERR_FATAL;
Christopher Fauleta717b992018-04-10 14:43:00 +02002298 return 0;
2299}
2300
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002301/* config parser for global "tune.listener.multi-queue", accepts "on", "fair" or "off" */
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002302static int cfg_parse_tune_listener_mq(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01002303 const struct proxy *defpx, const char *file, int line,
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002304 char **err)
2305{
2306 if (too_many_args(1, args, err, NULL))
2307 return -1;
2308
2309 if (strcmp(args[1], "on") == 0)
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002310 global.tune.options = (global.tune.options & ~GTUNE_LISTENER_MQ_ANY) | GTUNE_LISTENER_MQ_OPT;
2311 else if (strcmp(args[1], "fair") == 0)
2312 global.tune.options = (global.tune.options & ~GTUNE_LISTENER_MQ_ANY) | GTUNE_LISTENER_MQ_FAIR;
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002313 else if (strcmp(args[1], "off") == 0)
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002314 global.tune.options &= ~GTUNE_LISTENER_MQ_ANY;
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002315 else {
Willy Tarreau84fe1f42023-04-20 15:40:38 +02002316 memprintf(err, "'%s' expects either 'on', 'fair', or 'off' but got '%s'.", args[0], args[1]);
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002317 return -1;
2318 }
2319 return 0;
2320}
2321
Willy Tarreau61612d42012-04-19 18:42:05 +02002322/* Note: must not be declared <const> as its list will be overwritten.
2323 * Please take care of keeping this list alphabetically sorted.
2324 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002325static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002326 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
2327 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Jerome Magnineb421b22020-03-27 22:08:40 +01002328 { "so_name", smp_fetch_so_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002329 { /* END */ },
2330}};
2331
Willy Tarreau0108d902018-11-25 19:14:37 +01002332INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
2333
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002334/* Note: must not be declared <const> as its list will be overwritten.
2335 * Please take care of keeping this list alphabetically sorted.
2336 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002337static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002338 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002339}};
2340
Willy Tarreau0108d902018-11-25 19:14:37 +01002341INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
2342
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002343/* Note: must not be declared <const> as its list will be overwritten.
2344 * Please take care of keeping this list alphabetically sorted, doing so helps
2345 * all code contributors.
2346 * Optional keywords are also declared with a NULL ->parse() function so that
2347 * the config parser can report an appropriate error when a known keyword was
2348 * not enabled.
2349 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002350static struct bind_kw_list bind_kws = { "ALL", { }, {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01002351 { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002352 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
2353 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
2354 { "id", bind_parse_id, 1 }, /* set id of listening socket */
2355 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
2356 { "name", bind_parse_name, 1 }, /* set name of listening socket */
2357 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02002358 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Christopher Fauleta717b992018-04-10 14:43:00 +02002359 { "proto", bind_parse_proto, 1 }, /* set the proto to use for all incoming connections */
Willy Tarreau6dfbef42021-10-12 15:23:03 +02002360 { "shards", bind_parse_shards, 1 }, /* set number of shards */
Willy Tarreauc8cac042021-09-21 14:31:29 +02002361 { "thread", bind_parse_thread, 1 }, /* set list of allowed threads for this socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +01002362 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +02002363}};
2364
Willy Tarreau0108d902018-11-25 19:14:37 +01002365INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
2366
Willy Tarreau7ac908b2019-02-27 12:02:18 +01002367/* config keyword parsers */
2368static struct cfg_kw_list cfg_kws = {ILH, {
2369 { CFG_GLOBAL, "tune.listener.multi-queue", cfg_parse_tune_listener_mq },
2370 { 0, NULL, NULL }
2371}};
2372
2373INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2374
Willy Tarreau645513a2010-05-24 20:55:15 +02002375/*
2376 * Local variables:
2377 * c-indent-level: 8
2378 * c-basic-offset: 8
2379 * End:
2380 */