blob: 5002879dceb198ee67c0b082c130771e143b7c24 [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreaud1d54542012-09-12 22:58:11 +02002 * Listener management functions.
Willy Tarreaudd815982007-10-16 12:25:14 +02003 *
Willy Tarreau0ccb7442013-01-07 22:54:17 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaudd815982007-10-16 12:25:14 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau44489252014-01-14 17:52:01 +010013#define _GNU_SOURCE
Willy Tarreau6ae1ba62014-05-07 19:01:58 +020014#include <ctype.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020015#include <errno.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020016#include <stdio.h>
17#include <string.h>
Willy Tarreau95ccdde2014-02-01 09:28:36 +010018#include <unistd.h>
19#include <fcntl.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020020
Willy Tarreaudcc048a2020-06-04 19:11:43 +020021#include <haproxy/acl.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020022#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020023#include <haproxy/cfgparse.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/proto_sockpair.h>
33#include <haproxy/protocol-t.h>
34#include <haproxy/protocol.h>
35#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 Tarreau92b4f132020-06-01 11:05:15 +020038#include <haproxy/time.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 */
43static struct bind_kw_list bind_keywords = {
44 .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;
50static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state);
51
52
Willy Tarreau1efafce2019-01-27 15:37:19 +010053#if defined(USE_THREAD)
54
55struct accept_queue_ring accept_queue_rings[MAX_THREADS] __attribute__((aligned(64))) = { };
56
57/* dequeue and process a pending connection from the local accept queue (single
58 * consumer). Returns the accepted fd or -1 if none was found. The listener is
59 * placed into *li. The address is copied into *addr for no more than *addr_len
60 * bytes, and the address length is returned into *addr_len.
61 */
62int accept_queue_pop_sc(struct accept_queue_ring *ring, struct listener **li, void *addr, int *addr_len)
63{
64 struct accept_queue_entry *e;
65 unsigned int pos, next;
66 struct listener *ptr;
67 int len;
68 int fd;
69
70 pos = ring->head;
71
72 if (pos == ring->tail)
73 return -1;
74
75 next = pos + 1;
76 if (next >= ACCEPT_QUEUE_SIZE)
77 next = 0;
78
79 e = &ring->entry[pos];
80
81 /* wait for the producer to update the listener's pointer */
82 while (1) {
83 ptr = e->listener;
84 __ha_barrier_load();
85 if (ptr)
86 break;
87 pl_cpu_relax();
88 }
89
90 fd = e->fd;
91 len = e->addr_len;
92 if (len > *addr_len)
93 len = *addr_len;
94
95 if (likely(len > 0))
96 memcpy(addr, &e->addr, len);
97
98 /* release the entry */
99 e->listener = NULL;
100
101 __ha_barrier_store();
102 ring->head = next;
103
104 *addr_len = len;
105 *li = ptr;
106
107 return fd;
108}
109
110
111/* tries to push a new accepted connection <fd> into ring <ring> for listener
112 * <li>, from address <addr> whose length is <addr_len>. Returns non-zero if it
113 * succeeds, or zero if the ring is full. Supports multiple producers.
114 */
115int accept_queue_push_mp(struct accept_queue_ring *ring, int fd,
116 struct listener *li, const void *addr, int addr_len)
117{
118 struct accept_queue_entry *e;
119 unsigned int pos, next;
120
121 pos = ring->tail;
122 do {
123 next = pos + 1;
124 if (next >= ACCEPT_QUEUE_SIZE)
125 next = 0;
126 if (next == ring->head)
127 return 0; // ring full
Olivier Houchard64213e92019-03-08 18:52:57 +0100128 } while (unlikely(!_HA_ATOMIC_CAS(&ring->tail, &pos, next)));
Willy Tarreau1efafce2019-01-27 15:37:19 +0100129
130
131 e = &ring->entry[pos];
132
133 if (addr_len > sizeof(e->addr))
134 addr_len = sizeof(e->addr);
135
136 if (addr_len)
137 memcpy(&e->addr, addr, addr_len);
138
139 e->addr_len = addr_len;
140 e->fd = fd;
141
142 __ha_barrier_store();
143 /* now commit the change */
144
145 e->listener = li;
146 return 1;
147}
148
149/* proceed with accepting new connections */
150static struct task *accept_queue_process(struct task *t, void *context, unsigned short state)
151{
152 struct accept_queue_ring *ring = context;
153 struct listener *li;
154 struct sockaddr_storage addr;
Christopher Faulet102854c2019-04-30 12:17:13 +0200155 unsigned int max_accept;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100156 int addr_len;
157 int ret;
158 int fd;
159
Christopher Faulet102854c2019-04-30 12:17:13 +0200160 /* if global.tune.maxaccept is -1, then max_accept is UINT_MAX. It
161 * is not really illimited, but it is probably enough.
162 */
163 max_accept = global.tune.maxaccept ? global.tune.maxaccept : 64;
164 for (; max_accept; max_accept--) {
Willy Tarreau1efafce2019-01-27 15:37:19 +0100165 addr_len = sizeof(addr);
166 fd = accept_queue_pop_sc(ring, &li, &addr, &addr_len);
167 if (fd < 0)
168 break;
169
Olivier Houchard64213e92019-03-08 18:52:57 +0100170 _HA_ATOMIC_ADD(&li->thr_conn[tid], 1);
Willy Tarreau1efafce2019-01-27 15:37:19 +0100171 ret = li->accept(li, fd, &addr);
172 if (ret <= 0) {
173 /* connection was terminated by the application */
174 continue;
175 }
176
177 /* increase the per-process number of cumulated sessions, this
178 * may only be done once l->accept() has accepted the connection.
179 */
180 if (!(li->options & LI_O_UNLIMITED)) {
181 HA_ATOMIC_UPDATE_MAX(&global.sps_max,
182 update_freq_ctr(&global.sess_per_sec, 1));
183 if (li->bind_conf && li->bind_conf->is_ssl) {
184 HA_ATOMIC_UPDATE_MAX(&global.ssl_max,
185 update_freq_ctr(&global.ssl_per_sec, 1));
186 }
187 }
188 }
189
190 /* ran out of budget ? Let's come here ASAP */
Christopher Faulet102854c2019-04-30 12:17:13 +0200191 if (!max_accept)
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200192 tasklet_wakeup(ring->tasklet);
Willy Tarreau1efafce2019-01-27 15:37:19 +0100193
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200194 return NULL;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100195}
196
197/* Initializes the accept-queues. Returns 0 on success, otherwise ERR_* flags */
198static int accept_queue_init()
199{
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200200 struct tasklet *t;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100201 int i;
202
203 for (i = 0; i < global.nbthread; i++) {
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200204 t = tasklet_new();
Willy Tarreau1efafce2019-01-27 15:37:19 +0100205 if (!t) {
206 ha_alert("Out of memory while initializing accept queue for thread %d\n", i);
207 return ERR_FATAL|ERR_ABORT;
208 }
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200209 t->tid = i;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100210 t->process = accept_queue_process;
211 t->context = &accept_queue_rings[i];
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200212 accept_queue_rings[i].tasklet = t;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100213 }
214 return 0;
215}
216
217REGISTER_CONFIG_POSTPARSER("multi-threaded accept queue", accept_queue_init);
218
219#endif // USE_THREAD
220
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200221/* adjust the listener's state and its proxy's listener counters if needed.
222 * It must be called under the listener's lock, but uses atomic ops to change
223 * the proxy's counters so that the proxy lock is not needed.
224 */
Willy Tarreaua37b2442020-09-24 07:23:45 +0200225void listener_set_state(struct listener *l, enum li_state st)
226{
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200227 struct proxy *px = l->bind_conf->frontend;
228
229 if (px) {
230 /* from state */
231 switch (l->state) {
232 case LI_NEW: /* first call */
233 _HA_ATOMIC_ADD(&px->li_all, 1);
234 break;
235 case LI_INIT:
236 case LI_ASSIGNED:
237 break;
238 case LI_PAUSED:
239 _HA_ATOMIC_SUB(&px->li_paused, 1);
240 break;
241 case LI_LISTEN:
242 _HA_ATOMIC_SUB(&px->li_bound, 1);
243 break;
244 case LI_READY:
245 case LI_FULL:
246 case LI_LIMITED:
247 _HA_ATOMIC_SUB(&px->li_ready, 1);
248 break;
249 }
250
251 /* to state */
252 switch (st) {
253 case LI_NEW:
254 case LI_INIT:
255 case LI_ASSIGNED:
256 break;
257 case LI_PAUSED:
Willy Tarreau95a34602020-10-08 15:32:21 +0200258 BUG_ON(l->rx.fd == -1);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200259 _HA_ATOMIC_ADD(&px->li_paused, 1);
260 break;
261 case LI_LISTEN:
Willy Tarreau95a34602020-10-08 15:32:21 +0200262 BUG_ON(l->rx.fd == -1);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200263 _HA_ATOMIC_ADD(&px->li_bound, 1);
264 break;
265 case LI_READY:
266 case LI_FULL:
267 case LI_LIMITED:
Willy Tarreau95a34602020-10-08 15:32:21 +0200268 BUG_ON(l->rx.fd == -1);
Willy Tarreauefc0eec2020-09-24 07:27:06 +0200269 _HA_ATOMIC_ADD(&px->li_ready, 1);
270 break;
271 }
272 }
Willy Tarreaua37b2442020-09-24 07:23:45 +0200273 l->state = st;
274}
275
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100276/* This function adds the specified listener's file descriptor to the polling
277 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500278 * LI_FULL state depending on its number of connections. In daemon mode, we
Willy Tarreauae302532014-05-07 19:22:24 +0200279 * also support binding only the relevant processes to their respective
280 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100281 */
Willy Tarreau7834a3f2020-09-25 16:40:18 +0200282void enable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100283{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100284 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaud6afb532020-10-09 10:35:40 +0200285
286 /* If this listener is supposed to be only in the master, close it in
287 * the workers. Conversely, if it's supposed to be only in the workers
288 * close it in the master.
289 */
290 if ((master && !(listener->options & LI_O_MWORKER)) ||
291 (!master && (listener->options & LI_O_MWORKER))) {
292 do_unbind_listener(listener, 1);
293 }
294
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100295 if (listener->state == LI_LISTEN) {
Willy Tarreau95a34602020-10-08 15:32:21 +0200296 BUG_ON(listener->rx.fd == -1);
William Lallemand095ba4c2017-06-01 17:38:50 +0200297 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau818a92e2020-09-03 07:50:19 +0200298 !(proc_mask(listener->rx.settings->bind_proc) & pid_bit)) {
Willy Tarreauae302532014-05-07 19:22:24 +0200299 /* we don't want to enable this listener and don't
300 * want any fd event to reach it.
301 */
Olivier Houchard1fc05162017-04-06 01:05:05 +0200302 if (!(global.tune.options & GTUNE_SOCKET_TRANSFER))
Christopher Faulet510c0d62018-03-16 10:04:47 +0100303 do_unbind_listener(listener, 1);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200304 else {
Christopher Faulet510c0d62018-03-16 10:04:47 +0100305 do_unbind_listener(listener, 0);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200306 listener_set_state(listener, LI_LISTEN);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200307 }
Willy Tarreauae302532014-05-07 19:22:24 +0200308 }
Willy Tarreaua8cf66b2019-02-27 16:49:00 +0100309 else if (!listener->maxconn || listener->nbconn < listener->maxconn) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200310 listener->rx.proto->enable(listener);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200311 listener_set_state(listener, LI_READY);
Willy Tarreauae302532014-05-07 19:22:24 +0200312 }
313 else {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200314 listener_set_state(listener, LI_FULL);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100315 }
316 }
Willy Tarreaud6afb532020-10-09 10:35:40 +0200317
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100318 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100319}
320
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200321/*
322 * This function completely stops a listener. It will need to operate under the
323 * proxy's lock, the protocol's lock, and the listener's lock. The caller is
324 * responsible for indicating in lpx, lpr, lli whether the respective locks are
325 * already held (non-zero) or not (zero) so that the function picks the missing
326 * ones, in this order. The proxy's listeners count is updated and the proxy is
327 * disabled and woken up after the last one is gone.
328 */
329void stop_listener(struct listener *l, int lpx, int lpr, int lli)
330{
331 struct proxy *px = l->bind_conf->frontend;
332 int must_close;
333
334 if (l->options & LI_O_NOSTOP) {
335 /* master-worker sockpairs are never closed but don't count as a
336 * job.
337 */
338 return;
339 }
340
341 /* There are several cases where we must not close an FD:
342 * - we're starting up and we have socket transfers enabled;
343 * - we're the master and this FD was inherited;
344 */
345 if ((global.tune.options & GTUNE_SOCKET_TRANSFER && global.mode & MODE_STARTING) ||
346 (master && (l->rx.flags & RX_F_INHERITED)))
347 must_close = 0;
348 else
349 must_close = 1;
350
351 if (!lpx)
352 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
353
354 if (!lpr)
355 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
356
357 if (!lli)
358 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
359
360 if (l->state > LI_INIT) {
361 do_unbind_listener(l, must_close);
362
363 if (l->state >= LI_ASSIGNED)
364 __delete_listener(l);
365
Willy Tarreauacde1522020-10-07 16:31:39 +0200366 proxy_cond_disable(px);
Willy Tarreaucaa7df12020-10-07 15:58:50 +0200367 }
368
369 if (!lli)
370 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
371
372 if (!lpr)
373 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
374
375 if (!lpx)
376 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
377}
378
Willy Tarreaube58c382011-07-24 18:28:10 +0200379/* This function tries to temporarily disable a listener, depending on the OS
380 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
381 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
382 * closes upon SHUT_WR and refuses to rebind. So a common validation path
383 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
384 * is disabled. It normally returns non-zero, unless an error is reported.
385 */
386int pause_listener(struct listener *l)
387{
Willy Tarreau58651b42020-09-24 16:03:29 +0200388 struct proxy *px = l->bind_conf->frontend;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200389 int ret = 1;
390
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100391 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200392
Willy Tarreau02e19752020-09-23 17:17:22 +0200393 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
394 !(proc_mask(l->rx.settings->bind_proc) & pid_bit))
395 goto end;
396
Willy Tarreau9b3a9322020-09-24 14:46:34 +0200397 if (l->state <= LI_PAUSED)
398 goto end;
399
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200400 if (l->rx.proto->rx_suspend) {
Willy Tarreau092d8652014-07-07 20:22:12 +0200401 /* Returns < 0 in case of failure, 0 if the listener
402 * was totally stopped, or > 0 if correctly paused.
403 */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200404 ret = l->rx.proto->rx_suspend(&l->rx);
Willy Tarreaube58c382011-07-24 18:28:10 +0200405
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200406 if (ret < 0) {
407 ret = 0;
408 goto end;
409 }
Willy Tarreaufb76bd52020-09-24 18:07:48 +0200410 ret = 1;
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200411 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200412
Olivier Houchard859dc802019-08-08 15:47:21 +0200413 MT_LIST_DEL(&l->wait_queue);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200414
Willy Tarreaua37b2442020-09-24 07:23:45 +0200415 listener_set_state(l, LI_PAUSED);
Willy Tarreau58651b42020-09-24 16:03:29 +0200416
417 if (px && !px->li_ready) {
418 ha_warning("Paused %s %s.\n", proxy_cap_str(px->cap), px->id);
419 send_log(px, LOG_WARNING, "Paused %s %s.\n", proxy_cap_str(px->cap), px->id);
420 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200421 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100422 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200423 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200424}
425
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200426/* This function tries to resume a temporarily disabled listener. Paused, full,
427 * limited and disabled listeners are handled, which means that this function
428 * may replace enable_listener(). The resulting state will either be LI_READY
429 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200430 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200431 * foreground mode, and are ignored. If the listener was only in the assigned
432 * state, it's totally rebound. This can happen if a pause() has completely
433 * stopped it. If the resume fails, 0 is returned and an error might be
434 * displayed.
Willy Tarreaube58c382011-07-24 18:28:10 +0200435 */
Willy Tarreau01abd022019-02-28 10:27:18 +0100436int resume_listener(struct listener *l)
Willy Tarreaube58c382011-07-24 18:28:10 +0200437{
Willy Tarreau58651b42020-09-24 16:03:29 +0200438 struct proxy *px = l->bind_conf->frontend;
439 int was_paused = px && px->li_paused;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200440 int ret = 1;
441
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100442 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200443
Willy Tarreauf2cb1692019-07-11 10:08:31 +0200444 /* check that another thread didn't to the job in parallel (e.g. at the
445 * end of listen_accept() while we'd come from dequeue_all_listeners().
446 */
Olivier Houchard859dc802019-08-08 15:47:21 +0200447 if (MT_LIST_ADDED(&l->wait_queue))
Willy Tarreauf2cb1692019-07-11 10:08:31 +0200448 goto end;
449
William Lallemand095ba4c2017-06-01 17:38:50 +0200450 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau818a92e2020-09-03 07:50:19 +0200451 !(proc_mask(l->rx.settings->bind_proc) & pid_bit))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200452 goto end;
Willy Tarreau3569df32017-03-15 12:47:46 +0100453
Willy Tarreau5d7f9ce2020-09-24 18:54:11 +0200454 if (l->state == LI_READY)
455 goto end;
456
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200457 if (l->state == LI_ASSIGNED) {
458 char msg[100];
459 int err;
460
Willy Tarreaub3580b12020-09-01 10:26:22 +0200461 err = l->rx.proto->listen(l, msg, sizeof(msg));
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200462 if (err & ERR_ALERT)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100463 ha_alert("Resuming listener: %s\n", msg);
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200464 else if (err & ERR_WARN)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100465 ha_warning("Resuming listener: %s\n", msg);
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200466
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200467 if (err & (ERR_FATAL | ERR_ABORT)) {
468 ret = 0;
469 goto end;
470 }
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200471 }
472
Willy Tarreauc6dac6c2020-09-23 17:34:22 +0200473 if (l->state < LI_PAUSED) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200474 ret = 0;
475 goto end;
476 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200477
Willy Tarreau010fe152020-09-25 17:31:05 +0200478 if (l->state == LI_PAUSED && l->rx.proto->rx_resume &&
479 l->rx.proto->rx_resume(&l->rx) <= 0) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200480 ret = 0;
481 goto end;
482 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200483
Willy Tarreaua8cf66b2019-02-27 16:49:00 +0100484 if (l->maxconn && l->nbconn >= l->maxconn) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200485 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200486 listener_set_state(l, LI_FULL);
Willy Tarreau58651b42020-09-24 16:03:29 +0200487 goto done;
Willy Tarreaube58c382011-07-24 18:28:10 +0200488 }
489
Willy Tarreau4b51f422020-09-25 20:32:28 +0200490 l->rx.proto->enable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200491 listener_set_state(l, LI_READY);
Willy Tarreau58651b42020-09-24 16:03:29 +0200492
493 done:
494 if (was_paused && !px->li_paused) {
495 ha_warning("Resumed %s %s.\n", proxy_cap_str(px->cap), px->id);
496 send_log(px, LOG_WARNING, "Resumed %s %s.\n", proxy_cap_str(px->cap), px->id);
497 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200498 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100499 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200500 return ret;
501}
502
Willy Tarreau87b09662015-04-03 00:22:06 +0200503/* Marks a ready listener as full so that the stream code tries to re-enable
Willy Tarreau62793712011-07-24 19:23:38 +0200504 * it upon next close() using resume_listener().
505 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200506static void listener_full(struct listener *l)
Willy Tarreau62793712011-07-24 19:23:38 +0200507{
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100508 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200509 if (l->state >= LI_READY) {
Olivier Houchard859dc802019-08-08 15:47:21 +0200510 MT_LIST_DEL(&l->wait_queue);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100511 if (l->state != LI_FULL) {
Willy Tarreau4b51f422020-09-25 20:32:28 +0200512 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200513 listener_set_state(l, LI_FULL);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100514 }
Willy Tarreau62793712011-07-24 19:23:38 +0200515 }
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100516 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200517}
518
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200519/* Marks a ready listener as limited so that we only try to re-enable it when
520 * resources are free again. It will be queued into the specified queue.
521 */
Olivier Houchard859dc802019-08-08 15:47:21 +0200522static void limit_listener(struct listener *l, struct mt_list *list)
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200523{
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100524 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200525 if (l->state == LI_READY) {
Willy Tarreaude4db172020-07-10 08:10:29 +0200526 MT_LIST_TRY_ADDQ(list, &l->wait_queue);
Willy Tarreau4b51f422020-09-25 20:32:28 +0200527 l->rx.proto->disable(l);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200528 listener_set_state(l, LI_LIMITED);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200529 }
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100530 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200531}
532
Willy Tarreau241797a2019-12-10 14:10:52 +0100533/* Dequeues all listeners waiting for a resource the global wait queue */
534void dequeue_all_listeners()
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200535{
Willy Tarreau01abd022019-02-28 10:27:18 +0100536 struct listener *listener;
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200537
Willy Tarreau241797a2019-12-10 14:10:52 +0100538 while ((listener = MT_LIST_POP(&global_listener_queue, struct listener *, wait_queue))) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200539 /* This cannot fail because the listeners are by definition in
Willy Tarreau01abd022019-02-28 10:27:18 +0100540 * the LI_LIMITED state.
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200541 */
Willy Tarreau01abd022019-02-28 10:27:18 +0100542 resume_listener(listener);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200543 }
544}
545
Willy Tarreau241797a2019-12-10 14:10:52 +0100546/* Dequeues all listeners waiting for a resource in proxy <px>'s queue */
547void dequeue_proxy_listeners(struct proxy *px)
548{
549 struct listener *listener;
550
551 while ((listener = MT_LIST_POP(&px->listener_queue, struct listener *, wait_queue))) {
552 /* This cannot fail because the listeners are by definition in
553 * the LI_LIMITED state.
554 */
555 resume_listener(listener);
556 }
557}
558
Christopher Faulet510c0d62018-03-16 10:04:47 +0100559/* Must be called with the lock held. Depending on <do_close> value, it does
Willy Tarreaub6607bf2020-09-23 16:24:23 +0200560 * what unbind_listener or unbind_listener_no_close should do. It can also
561 * close a zombie listener's FD when called in early states.
Christopher Faulet510c0d62018-03-16 10:04:47 +0100562 */
563void do_unbind_listener(struct listener *listener, int do_close)
Willy Tarreaub648d632007-10-28 22:13:50 +0100564{
Olivier Houchard859dc802019-08-08 15:47:21 +0200565 MT_LIST_DEL(&listener->wait_queue);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200566
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200567 if (listener->state <= LI_ASSIGNED)
568 goto out_close;
569
570 if (listener->rx.fd == -1) {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200571 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200572 goto out_close;
573 }
574
575 if (listener->state >= LI_PAUSED) {
576 if (listener->state >= LI_READY) {
577 listener->rx.proto->disable(listener);
578 listener_set_state(listener, LI_LISTEN);
579 }
Willy Tarreau4b51f422020-09-25 20:32:28 +0200580 listener->rx.proto->rx_disable(&listener->rx);
Willy Tarreaub6607bf2020-09-23 16:24:23 +0200581 }
582
Willy Tarreau87acd4e2020-10-08 15:36:46 +0200583 out_close:
Willy Tarreau374e9af2020-10-09 15:47:17 +0200584 /* There are a number of situations where we prefer to keep the FD and
585 * not to close it (unless we're stopping, of course):
586 * - worker process unbinding from a worker's FD with socket transfer enabled => keep
587 * - master process unbinding from a master's inherited FD => keep
588 * - master process unbinding from a master's FD => close
589 * - master process unbinding from a worker's FD => close
590 * - worker process unbinding from a master's FD => close
591 * - worker process unbinding from a worker's FD => close
592 */
593
594 if (!stopping && !master &&
595 !(listener->options & LI_O_MWORKER) &&
596 (global.tune.options & GTUNE_SOCKET_TRANSFER))
597 return;
598
599 if (!stopping && master &&
600 listener->options & LI_O_MWORKER &&
601 listener->rx.flags & RX_F_INHERITED)
602 return;
603
604 listener->rx.flags &= ~RX_F_BOUND;
605 if (listener->rx.fd != -1)
606 fd_delete(listener->rx.fd);
607 listener->rx.fd = -1;
608 if (listener->state > LI_ASSIGNED)
609 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100610}
611
Olivier Houchard1fc05162017-04-06 01:05:05 +0200612/* This function closes the listening socket for the specified listener,
613 * provided that it's already in a listening state. The listener enters the
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100614 * LI_ASSIGNED state. This function is intended to be used as a generic
615 * function for standard protocols.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200616 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100617void unbind_listener(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200618{
Christopher Faulet510c0d62018-03-16 10:04:47 +0100619 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100620 do_unbind_listener(listener, 1);
Christopher Faulet510c0d62018-03-16 10:04:47 +0100621 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200622}
623
624/* This function pretends the listener is dead, but keeps the FD opened, so
625 * that we can provide it, for conf reloading.
626 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100627void unbind_listener_no_close(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200628{
Christopher Faulet510c0d62018-03-16 10:04:47 +0100629 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100630 do_unbind_listener(listener, 0);
Christopher Faulet510c0d62018-03-16 10:04:47 +0100631 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200632}
633
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200634/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
635 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200636 * allocation). The address family is taken from ss->ss_family, and the protocol
637 * passed in <proto> must be usable on this family. The number of jobs and
638 * listeners is automatically increased by the number of listeners created. It
639 * returns non-zero on success, zero on error with the error message set in <err>.
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200640 */
641int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200642 int portl, int porth, int fd, struct protocol *proto, char **err)
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200643{
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200644 struct listener *l;
645 int port;
646
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200647 for (port = portl; port <= porth; port++) {
648 l = calloc(1, sizeof(*l));
649 if (!l) {
650 memprintf(err, "out of memory");
651 return 0;
652 }
653 l->obj_type = OBJ_TYPE_LISTENER;
654 LIST_ADDQ(&bc->frontend->conf.listeners, &l->by_fe);
655 LIST_ADDQ(&bc->listeners, &l->by_bind);
656 l->bind_conf = bc;
Willy Tarreau0fce6bc2020-09-03 07:46:06 +0200657 l->rx.settings = &bc->settings;
Willy Tarreaueef45422020-09-03 10:05:03 +0200658 l->rx.owner = l;
Willy Tarreau38ba6472020-08-27 08:16:52 +0200659 l->rx.fd = fd;
Willy Tarreau37159062020-08-27 07:48:42 +0200660 memcpy(&l->rx.addr, ss, sizeof(*ss));
Olivier Houchard859dc802019-08-08 15:47:21 +0200661 MT_LIST_INIT(&l->wait_queue);
Willy Tarreaua37b2442020-09-24 07:23:45 +0200662 listener_set_state(l, LI_INIT);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200663
664 proto->add(l, port);
665
Willy Tarreau909c23b2020-09-15 13:50:58 +0200666 if (fd != -1)
Willy Tarreau43046fa2020-09-01 15:41:59 +0200667 l->rx.flags |= RX_F_INHERITED;
William Lallemand75ea0a02017-11-15 19:02:58 +0100668
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100669 HA_SPIN_INIT(&l->lock);
Olivier Houchard64213e92019-03-08 18:52:57 +0100670 _HA_ATOMIC_ADD(&jobs, 1);
671 _HA_ATOMIC_ADD(&listeners, 1);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200672 }
673 return 1;
674}
675
Willy Tarreau1a64d162007-10-28 22:26:05 +0100676/* Delete a listener from its protocol's list of listeners. The listener's
677 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
Willy Tarreau2cc5bae2017-09-15 08:18:11 +0200678 * number of listeners is updated, as well as the global number of listeners
679 * and jobs. Note that the listener must have previously been unbound. This
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200680 * is a low-level function expected to be called with the proto_lock and the
681 * listener's lock held.
Willy Tarreau1a64d162007-10-28 22:26:05 +0100682 */
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200683void __delete_listener(struct listener *listener)
Willy Tarreau1a64d162007-10-28 22:26:05 +0100684{
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100685 if (listener->state == LI_ASSIGNED) {
Willy Tarreaua37b2442020-09-24 07:23:45 +0200686 listener_set_state(listener, LI_INIT);
Willy Tarreaub7436612020-08-28 19:51:44 +0200687 LIST_DEL(&listener->rx.proto_list);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200688 listener->rx.proto->nb_receivers--;
Olivier Houchard64213e92019-03-08 18:52:57 +0100689 _HA_ATOMIC_SUB(&jobs, 1);
690 _HA_ATOMIC_SUB(&listeners, 1);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100691 }
Willy Tarreaub4c083f2020-10-07 15:36:16 +0200692}
693
694/* Delete a listener from its protocol's list of listeners (please check
695 * __delete_listener() above). The proto_lock and the listener's lock will
696 * be grabbed in this order.
697 */
698void delete_listener(struct listener *listener)
699{
700 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
701 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
702 __delete_listener(listener);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100703 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau6ee9f8d2019-08-26 10:55:52 +0200704 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100705}
706
Willy Tarreaue2711c72019-02-27 15:39:41 +0100707/* Returns a suitable value for a listener's backlog. It uses the listener's,
708 * otherwise the frontend's backlog, otherwise the listener's maxconn,
709 * otherwise the frontend's maxconn, otherwise 1024.
710 */
711int listener_backlog(const struct listener *l)
712{
713 if (l->backlog)
714 return l->backlog;
715
716 if (l->bind_conf->frontend->backlog)
717 return l->bind_conf->frontend->backlog;
718
719 if (l->maxconn)
720 return l->maxconn;
721
722 if (l->bind_conf->frontend->maxconn)
723 return l->bind_conf->frontend->maxconn;
724
725 return 1024;
726}
727
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200728/* This function is called on a read event from a listening socket, corresponding
729 * to an accept. It tries to accept as many connections as possible, and for each
730 * calls the listener's accept handler (generally the frontend's accept handler).
731 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200732void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200733{
734 struct listener *l = fdtab[fd].owner;
Olivier Houchardd16a9df2019-02-25 16:18:16 +0100735 struct proxy *p;
Christopher Faulet102854c2019-04-30 12:17:13 +0200736 unsigned int max_accept;
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100737 int next_conn = 0;
Willy Tarreau82c97892019-02-27 19:32:32 +0100738 int next_feconn = 0;
739 int next_actconn = 0;
Willy Tarreaubb660302014-05-07 19:47:02 +0200740 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200741 int cfd;
742 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100743#ifdef USE_ACCEPT4
744 static int accept4_broken;
745#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200746
Olivier Houchardd16a9df2019-02-25 16:18:16 +0100747 if (!l)
748 return;
749 p = l->bind_conf->frontend;
Christopher Faulet102854c2019-04-30 12:17:13 +0200750
751 /* if l->maxaccept is -1, then max_accept is UINT_MAX. It is not really
752 * illimited, but it is probably enough.
753 */
Olivier Houchardd16a9df2019-02-25 16:18:16 +0100754 max_accept = l->maxaccept ? l->maxaccept : 1;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200755
Willy Tarreau93e7c002013-10-07 18:51:07 +0200756 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
757 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200758
759 if (unlikely(!max)) {
760 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200761 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +0100762 goto limit_global;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200763 }
764
765 if (max_accept > max)
766 max_accept = max;
767 }
768
769 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200770 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
771
772 if (unlikely(!max)) {
773 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200774 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +0100775 goto limit_global;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200776 }
777
778 if (max_accept > max)
779 max_accept = max;
780 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200781#ifdef USE_OPENSSL
782 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
783 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200784
Willy Tarreaue43d5322013-10-07 20:01:52 +0200785 if (unlikely(!max)) {
786 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200787 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +0100788 goto limit_global;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200789 }
790
791 if (max_accept > max)
792 max_accept = max;
793 }
794#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200795 if (p && p->fe_sps_lim) {
796 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
797
798 if (unlikely(!max)) {
799 /* frontend accept rate limit was reached */
Willy Tarreau0591bf72019-12-10 12:01:21 +0100800 expire = tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0));
801 goto limit_proxy;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200802 }
803
804 if (max_accept > max)
805 max_accept = max;
806 }
807
808 /* Note: if we fail to allocate a connection because of configured
809 * limits, we'll schedule a new attempt worst 1 second later in the
810 * worst case. If we fail due to system limits or temporary resource
811 * shortage, we try again 100ms later in the worst case.
812 */
Christopher Faulet102854c2019-04-30 12:17:13 +0200813 for (; max_accept; next_conn = next_feconn = next_actconn = 0, max_accept--) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200814 struct sockaddr_storage addr;
815 socklen_t laddr = sizeof(addr);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200816 unsigned int count;
Willy Tarreauaf613e82020-06-05 08:40:51 +0200817 __decl_thread(unsigned long mask);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200818
Willy Tarreau82c97892019-02-27 19:32:32 +0100819 /* pre-increase the number of connections without going too far.
820 * We process the listener, then the proxy, then the process.
821 * We know which ones to unroll based on the next_xxx value.
822 */
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100823 do {
824 count = l->nbconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +0100825 if (unlikely(l->maxconn && count >= l->maxconn)) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100826 /* the listener was marked full or another
827 * thread is going to do it.
828 */
829 next_conn = 0;
Willy Tarreau93604ed2019-11-15 10:20:07 +0100830 listener_full(l);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100831 goto end;
832 }
833 next_conn = count + 1;
David Carlier56716622019-03-27 16:08:42 +0000834 } while (!_HA_ATOMIC_CAS(&l->nbconn, (int *)(&count), next_conn));
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100835
Willy Tarreau82c97892019-02-27 19:32:32 +0100836 if (p) {
837 do {
838 count = p->feconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +0100839 if (unlikely(count >= p->maxconn)) {
Willy Tarreau82c97892019-02-27 19:32:32 +0100840 /* the frontend was marked full or another
841 * thread is going to do it.
842 */
843 next_feconn = 0;
Willy Tarreau0591bf72019-12-10 12:01:21 +0100844 expire = TICK_ETERNITY;
845 goto limit_proxy;
Willy Tarreau82c97892019-02-27 19:32:32 +0100846 }
847 next_feconn = count + 1;
Olivier Houchard64213e92019-03-08 18:52:57 +0100848 } while (!_HA_ATOMIC_CAS(&p->feconn, &count, next_feconn));
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200849 }
850
Willy Tarreau82c97892019-02-27 19:32:32 +0100851 if (!(l->options & LI_O_UNLIMITED)) {
852 do {
853 count = actconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +0100854 if (unlikely(count >= global.maxconn)) {
Willy Tarreau82c97892019-02-27 19:32:32 +0100855 /* the process was marked full or another
856 * thread is going to do it.
857 */
858 next_actconn = 0;
Willy Tarreau0591bf72019-12-10 12:01:21 +0100859 expire = tick_add(now_ms, 1000); /* try again in 1 second */
860 goto limit_global;
Willy Tarreau82c97892019-02-27 19:32:32 +0100861 }
862 next_actconn = count + 1;
David Carlier56716622019-03-27 16:08:42 +0000863 } while (!_HA_ATOMIC_CAS(&actconn, (int *)(&count), next_actconn));
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200864 }
865
William Lallemand2fe7dd02018-09-11 16:51:29 +0200866 /* with sockpair@ we don't want to do an accept */
Willy Tarreau37159062020-08-27 07:48:42 +0200867 if (unlikely(l->rx.addr.ss_family == AF_CUST_SOCKPAIR)) {
William Lallemand2fe7dd02018-09-11 16:51:29 +0200868 if ((cfd = recv_fd_uxst(fd)) != -1)
William Lallemandd9138002018-11-27 12:02:39 +0100869 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau888d5672019-01-27 18:34:12 +0100870 /* just like with UNIX sockets, only the family is filled */
871 addr.ss_family = AF_UNIX;
872 laddr = sizeof(addr.ss_family);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200873 } else
874
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200875#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100876 /* only call accept4() if it's known to be safe, otherwise
877 * fallback to the legacy accept() + fcntl().
878 */
879 if (unlikely(accept4_broken ||
William Lallemandd9138002018-11-27 12:02:39 +0100880 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
Willy Tarreau818dca52014-01-31 19:40:19 +0100881 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
882 (accept4_broken = 1))))
883#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200884 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
William Lallemandd9138002018-11-27 12:02:39 +0100885 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100886
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200887 if (unlikely(cfd == -1)) {
888 switch (errno) {
889 case EAGAIN:
Willy Tarreau20aeb1c2019-12-10 08:42:21 +0100890 if (fdtab[fd].ev & (FD_POLL_HUP|FD_POLL_ERR)) {
Willy Tarreaubb660302014-05-07 19:47:02 +0200891 /* the listening socket might have been disabled in a shared
892 * process and we're a collateral victim. We'll just pause for
893 * a while in case it comes back. In the mean time, we need to
894 * clear this sticky flag.
895 */
Willy Tarreau20aeb1c2019-12-10 08:42:21 +0100896 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Willy Tarreaubb660302014-05-07 19:47:02 +0200897 goto transient_error;
898 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200899 goto end; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200900 case EINVAL:
901 /* might be trying to accept on a shut fd (eg: soft stop) */
902 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100903 case EINTR:
904 case ECONNABORTED:
Olivier Houchard64213e92019-03-08 18:52:57 +0100905 _HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreau82c97892019-02-27 19:32:32 +0100906 if (p)
Olivier Houchard64213e92019-03-08 18:52:57 +0100907 _HA_ATOMIC_SUB(&p->feconn, 1);
Willy Tarreau82c97892019-02-27 19:32:32 +0100908 if (!(l->options & LI_O_UNLIMITED))
Olivier Houchard64213e92019-03-08 18:52:57 +0100909 _HA_ATOMIC_SUB(&actconn, 1);
Willy Tarreaua593ec52014-01-20 21:21:30 +0100910 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200911 case ENFILE:
912 if (p)
913 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100914 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
915 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200916 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200917 case EMFILE:
918 if (p)
919 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100920 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
921 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200922 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200923 case ENOBUFS:
924 case ENOMEM:
925 if (p)
926 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100927 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
928 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200929 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200930 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100931 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau92079932019-12-10 09:30:05 +0100932 max_accept = 0;
933 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200934 }
935 }
936
William Lallemandd9138002018-11-27 12:02:39 +0100937 /* we don't want to leak the FD upon reload if it's in the master */
938 if (unlikely(master == 1))
939 fcntl(cfd, F_SETFD, FD_CLOEXEC);
940
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100941 /* The connection was accepted, it must be counted as such */
942 if (l->counters)
943 HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, next_conn);
944
Willy Tarreau82c97892019-02-27 19:32:32 +0100945 if (p)
946 HA_ATOMIC_UPDATE_MAX(&p->fe_counters.conn_max, next_feconn);
947
948 proxy_inc_fe_conn_ctr(l, p);
949
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100950 if (!(l->options & LI_O_UNLIMITED)) {
951 count = update_freq_ctr(&global.conn_per_sec, 1);
952 HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100953 }
954
Willy Tarreau64a9c052019-04-12 15:27:17 +0200955 _HA_ATOMIC_ADD(&activity[tid].accepted, 1);
956
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200957 if (unlikely(cfd >= global.maxsock)) {
958 send_log(p, LOG_EMERG,
959 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
960 p->id);
961 close(cfd);
Willy Tarreau0591bf72019-12-10 12:01:21 +0100962 expire = tick_add(now_ms, 1000); /* try again in 1 second */
963 goto limit_global;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200964 }
965
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100966 /* past this point, l->accept() will automatically decrement
Willy Tarreau82c97892019-02-27 19:32:32 +0100967 * l->nbconn, feconn and actconn once done. Setting next_*conn=0
968 * allows the error path not to rollback on nbconn. It's more
969 * convenient than duplicating all exit labels.
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100970 */
971 next_conn = 0;
Willy Tarreau82c97892019-02-27 19:32:32 +0100972 next_feconn = 0;
973 next_actconn = 0;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200974
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100975#if defined(USE_THREAD)
Willy Tarreau818a92e2020-09-03 07:50:19 +0200976 mask = thread_mask(l->rx.settings->bind_thread) & all_threads_mask;
Willy Tarreaua7da5e82020-03-12 17:33:29 +0100977 if (atleast2(mask) && (global.tune.options & GTUNE_LISTENER_MQ) && !stopping) {
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100978 struct accept_queue_ring *ring;
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100979 unsigned int t, t0, t1, t2;
Willy Tarreaufc630bd2019-03-04 19:57:34 +0100980
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100981 /* The principle is that we have two running indexes,
982 * each visiting in turn all threads bound to this
983 * listener. The connection will be assigned to the one
984 * with the least connections, and the other one will
985 * be updated. This provides a good fairness on short
Willy Tarreaufc630bd2019-03-04 19:57:34 +0100986 * connections (round robin) and on long ones (conn
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100987 * count), without ever missing any idle thread.
Willy Tarreaufc630bd2019-03-04 19:57:34 +0100988 */
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100989
990 /* keep a copy for the final update. thr_idx is composite
991 * and made of (t2<<16) + t1.
992 */
Willy Tarreau0cf33172019-03-06 15:26:33 +0100993 t0 = l->thr_idx;
Willy Tarreaufc630bd2019-03-04 19:57:34 +0100994 do {
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100995 unsigned long m1, m2;
996 int q1, q2;
997
998 t2 = t1 = t0;
999 t2 >>= 16;
1000 t1 &= 0xFFFF;
1001
1002 /* t1 walks low to high bits ;
1003 * t2 walks high to low.
1004 */
1005 m1 = mask >> t1;
1006 m2 = mask & (t2 ? nbits(t2 + 1) : ~0UL);
1007
Willy Tarreau85d04242019-04-16 18:09:13 +02001008 if (unlikely(!(m1 & 1))) {
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001009 m1 &= ~1UL;
1010 if (!m1) {
1011 m1 = mask;
1012 t1 = 0;
1013 }
1014 t1 += my_ffsl(m1) - 1;
1015 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001016
Willy Tarreau85d04242019-04-16 18:09:13 +02001017 if (unlikely(!(m2 & (1UL << t2)) || t1 == t2)) {
1018 /* highest bit not set */
1019 if (!m2)
1020 m2 = mask;
1021
1022 t2 = my_flsl(m2) - 1;
1023 }
1024
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001025 /* now we have two distinct thread IDs belonging to the mask */
1026 q1 = accept_queue_rings[t1].tail - accept_queue_rings[t1].head + ACCEPT_QUEUE_SIZE;
1027 if (q1 >= ACCEPT_QUEUE_SIZE)
1028 q1 -= ACCEPT_QUEUE_SIZE;
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001029
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001030 q2 = accept_queue_rings[t2].tail - accept_queue_rings[t2].head + ACCEPT_QUEUE_SIZE;
1031 if (q2 >= ACCEPT_QUEUE_SIZE)
1032 q2 -= ACCEPT_QUEUE_SIZE;
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001033
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001034 /* we have 3 possibilities now :
1035 * q1 < q2 : t1 is less loaded than t2, so we pick it
1036 * and update t2 (since t1 might still be
1037 * lower than another thread)
1038 * q1 > q2 : t2 is less loaded than t1, so we pick it
1039 * and update t1 (since t2 might still be
1040 * lower than another thread)
1041 * q1 = q2 : both are equally loaded, thus we pick t1
1042 * and update t1 as it will become more loaded
1043 * than t2.
1044 */
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001045
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001046 q1 += l->thr_conn[t1];
1047 q2 += l->thr_conn[t2];
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001048
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001049 if (q1 - q2 < 0) {
1050 t = t1;
1051 t2 = t2 ? t2 - 1 : LONGBITS - 1;
1052 }
1053 else if (q1 - q2 > 0) {
1054 t = t2;
1055 t1++;
1056 if (t1 >= LONGBITS)
1057 t1 = 0;
1058 }
1059 else {
1060 t = t1;
1061 t1++;
1062 if (t1 >= LONGBITS)
1063 t1 = 0;
1064 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001065
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001066 /* new value for thr_idx */
1067 t1 += (t2 << 16);
Olivier Houchard64213e92019-03-08 18:52:57 +01001068 } while (unlikely(!_HA_ATOMIC_CAS(&l->thr_idx, &t0, t1)));
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001069
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001070 /* We successfully selected the best thread "t" for this
1071 * connection. We use deferred accepts even if it's the
1072 * local thread because tests show that it's the best
1073 * performing model, likely due to better cache locality
1074 * when processing this loop.
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001075 */
Willy Tarreau0fe703b2019-03-05 08:46:28 +01001076 ring = &accept_queue_rings[t];
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001077 if (accept_queue_push_mp(ring, cfd, l, &addr, laddr)) {
Olivier Houchard64213e92019-03-08 18:52:57 +01001078 _HA_ATOMIC_ADD(&activity[t].accq_pushed, 1);
Willy Tarreau2bd65a72019-09-24 06:55:18 +02001079 tasklet_wakeup(ring->tasklet);
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001080 continue;
1081 }
1082 /* If the ring is full we do a synchronous accept on
1083 * the local thread here.
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001084 */
Olivier Houchard64213e92019-03-08 18:52:57 +01001085 _HA_ATOMIC_ADD(&activity[t].accq_full, 1);
Willy Tarreaue0e9c482019-01-27 15:37:19 +01001086 }
1087#endif // USE_THREAD
1088
Olivier Houchard64213e92019-03-08 18:52:57 +01001089 _HA_ATOMIC_ADD(&l->thr_conn[tid], 1);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001090 ret = l->accept(l, cfd, &addr);
1091 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001092 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001093 * we just have to ignore it (ret == 0) or it's a critical
1094 * error due to a resource shortage, and we must stop the
1095 * listener (ret < 0).
1096 */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001097 if (ret == 0) /* successful termination */
1098 continue;
1099
Willy Tarreaubb660302014-05-07 19:47:02 +02001100 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001101 }
1102
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001103 /* increase the per-process number of cumulated sessions, this
1104 * may only be done once l->accept() has accepted the connection.
1105 */
Willy Tarreau93e7c002013-10-07 18:51:07 +02001106 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001107 count = update_freq_ctr(&global.sess_per_sec, 1);
1108 HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
Willy Tarreau93e7c002013-10-07 18:51:07 +02001109 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001110#ifdef USE_OPENSSL
1111 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001112 count = update_freq_ctr(&global.ssl_per_sec, 1);
1113 HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
Willy Tarreaue43d5322013-10-07 20:01:52 +02001114 }
1115#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +02001116
Willy Tarreau8d2c98b2020-05-01 09:51:11 +02001117 ti->flags &= ~TI_FL_STUCK; // this thread is still running
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001118 } /* end of for (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001119
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001120 end:
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001121 if (next_conn)
Olivier Houchard64213e92019-03-08 18:52:57 +01001122 _HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreau741b4d62019-02-25 15:02:04 +01001123
Willy Tarreau82c97892019-02-27 19:32:32 +01001124 if (p && next_feconn)
Olivier Houchard64213e92019-03-08 18:52:57 +01001125 _HA_ATOMIC_SUB(&p->feconn, 1);
Willy Tarreau82c97892019-02-27 19:32:32 +01001126
1127 if (next_actconn)
Olivier Houchard64213e92019-03-08 18:52:57 +01001128 _HA_ATOMIC_SUB(&actconn, 1);
Willy Tarreau82c97892019-02-27 19:32:32 +01001129
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001130 if ((l->state == LI_FULL && (!l->maxconn || l->nbconn < l->maxconn)) ||
Willy Tarreaucdcba112019-12-11 15:06:30 +01001131 (l->state == LI_LIMITED &&
1132 ((!p || p->feconn < p->maxconn) && (actconn < global.maxconn) &&
1133 (!tick_isset(global_listener_queue_task->expire) ||
1134 tick_is_expired(global_listener_queue_task->expire, now_ms))))) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001135 /* at least one thread has to this when quitting */
1136 resume_listener(l);
1137
1138 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001139 dequeue_all_listeners();
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001140
Olivier Houchard859dc802019-08-08 15:47:21 +02001141 if (p && !MT_LIST_ISEMPTY(&p->listener_queue) &&
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001142 (!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 +01001143 dequeue_proxy_listeners(p);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001144 }
Willy Tarreau4c044e22019-12-05 07:40:32 +01001145
Willy Tarreau92079932019-12-10 09:30:05 +01001146 /* Now it's getting tricky. The listener was supposed to be in LI_READY
1147 * state but in the mean time we might have changed it to LI_FULL or
1148 * LI_LIMITED, and another thread might also have turned it to
1149 * LI_PAUSED, LI_LISTEN or even LI_INI when stopping a proxy. We must
1150 * be certain to keep the FD enabled when in the READY state but we
1151 * must also stop it for other states that we might have switched to
1152 * while others re-enabled polling.
1153 */
1154 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
1155 if (l->state == LI_READY) {
1156 if (max_accept > 0)
1157 fd_cant_recv(fd);
1158 else
1159 fd_done_recv(fd);
1160 } else if (l->state > LI_ASSIGNED) {
Willy Tarreau38ba6472020-08-27 08:16:52 +02001161 fd_stop_recv(l->rx.fd);
Willy Tarreau92079932019-12-10 09:30:05 +01001162 }
1163 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001164 return;
1165
1166 transient_error:
1167 /* pause the listener for up to 100 ms */
1168 expire = tick_add(now_ms, 100);
1169
1170 limit_global:
1171 /* (re-)queue the listener to the global queue and set it to expire no
1172 * later than <expire> ahead. The listener turns to LI_LIMITED.
1173 */
1174 limit_listener(l, &global_listener_queue);
1175 task_schedule(global_listener_queue_task, expire);
1176 goto end;
1177
1178 limit_proxy:
1179 /* (re-)queue the listener to the proxy's queue and set it to expire no
1180 * later than <expire> ahead. The listener turns to LI_LIMITED.
1181 */
1182 limit_listener(l, &p->listener_queue);
Willy Tarreaueeea8082020-01-08 19:15:07 +01001183 if (p->task && tick_isset(expire))
1184 task_schedule(p->task, expire);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001185 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001186}
1187
Willy Tarreau05f50472017-09-15 09:19:58 +02001188/* Notify the listener that a connection initiated from it was released. This
1189 * is used to keep the connection count consistent and to possibly re-open
1190 * listening when it was limited.
1191 */
1192void listener_release(struct listener *l)
1193{
1194 struct proxy *fe = l->bind_conf->frontend;
1195
1196 if (!(l->options & LI_O_UNLIMITED))
Olivier Houchard64213e92019-03-08 18:52:57 +01001197 _HA_ATOMIC_SUB(&actconn, 1);
Willy Tarreau82c97892019-02-27 19:32:32 +01001198 if (fe)
Olivier Houchard64213e92019-03-08 18:52:57 +01001199 _HA_ATOMIC_SUB(&fe->feconn, 1);
1200 _HA_ATOMIC_SUB(&l->nbconn, 1);
1201 _HA_ATOMIC_SUB(&l->thr_conn[tid], 1);
Willy Tarreau82c97892019-02-27 19:32:32 +01001202
1203 if (l->state == LI_FULL || l->state == LI_LIMITED)
Willy Tarreau05f50472017-09-15 09:19:58 +02001204 resume_listener(l);
1205
1206 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001207 dequeue_all_listeners();
Willy Tarreau05f50472017-09-15 09:19:58 +02001208
Olivier Houchard859dc802019-08-08 15:47:21 +02001209 if (!MT_LIST_ISEMPTY(&fe->listener_queue) &&
Willy Tarreau05f50472017-09-15 09:19:58 +02001210 (!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 +01001211 dequeue_proxy_listeners(fe);
Willy Tarreau05f50472017-09-15 09:19:58 +02001212}
1213
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001214/* Initializes the listener queues. Returns 0 on success, otherwise ERR_* flags */
1215static int listener_queue_init()
1216{
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001217 global_listener_queue_task = task_new(MAX_THREADS_MASK);
1218 if (!global_listener_queue_task) {
1219 ha_alert("Out of memory when initializing global listener queue\n");
1220 return ERR_FATAL|ERR_ABORT;
1221 }
1222 /* very simple initialization, users will queue the task if needed */
1223 global_listener_queue_task->context = NULL; /* not even a context! */
1224 global_listener_queue_task->process = manage_global_listener_queue;
1225
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001226 return 0;
1227}
1228
1229static void listener_queue_deinit()
1230{
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001231 task_destroy(global_listener_queue_task);
1232 global_listener_queue_task = NULL;
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001233}
1234
1235REGISTER_CONFIG_POSTPARSER("multi-threaded listener queue", listener_queue_init);
1236REGISTER_POST_DEINIT(listener_queue_deinit);
1237
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001238
1239/* This is the global management task for listeners. It enables listeners waiting
1240 * for global resources when there are enough free resource, or at least once in
1241 * a while. It is designed to be called as a task.
1242 */
1243static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state)
1244{
1245 /* If there are still too many concurrent connections, let's wait for
1246 * some of them to go away. We don't need to re-arm the timer because
1247 * each of them will scan the queue anyway.
1248 */
1249 if (unlikely(actconn >= global.maxconn))
1250 goto out;
1251
1252 /* We should periodically try to enable listeners waiting for a global
1253 * resource here, because it is possible, though very unlikely, that
1254 * they have been blocked by a temporary lack of global resource such
1255 * as a file descriptor or memory and that the temporary condition has
1256 * disappeared.
1257 */
1258 dequeue_all_listeners();
1259
1260 out:
1261 t->expire = TICK_ETERNITY;
1262 task_queue(t);
1263 return t;
1264}
1265
Willy Tarreau26982662012-09-12 23:17:10 +02001266/*
1267 * Registers the bind keyword list <kwl> as a list of valid keywords for next
1268 * parsing sessions.
1269 */
1270void bind_register_keywords(struct bind_kw_list *kwl)
1271{
1272 LIST_ADDQ(&bind_keywords.list, &kwl->list);
1273}
1274
1275/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
1276 * keyword is found with a NULL ->parse() function, then an attempt is made to
1277 * find one with a valid ->parse() function. This way it is possible to declare
1278 * platform-dependant, known keywords as NULL, then only declare them as valid
1279 * if some options are met. Note that if the requested keyword contains an
1280 * opening parenthesis, everything from this point is ignored.
1281 */
1282struct bind_kw *bind_find_kw(const char *kw)
1283{
1284 int index;
1285 const char *kwend;
1286 struct bind_kw_list *kwl;
1287 struct bind_kw *ret = NULL;
1288
1289 kwend = strchr(kw, '(');
1290 if (!kwend)
1291 kwend = kw + strlen(kw);
1292
1293 list_for_each_entry(kwl, &bind_keywords.list, list) {
1294 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1295 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1296 kwl->kw[index].kw[kwend-kw] == 0) {
1297 if (kwl->kw[index].parse)
1298 return &kwl->kw[index]; /* found it !*/
1299 else
1300 ret = &kwl->kw[index]; /* may be OK */
1301 }
1302 }
1303 }
1304 return ret;
1305}
1306
Willy Tarreau8638f482012-09-18 18:01:17 +02001307/* Dumps all registered "bind" keywords to the <out> string pointer. The
1308 * unsupported keywords are only dumped if their supported form was not
1309 * found.
1310 */
1311void bind_dump_kws(char **out)
1312{
1313 struct bind_kw_list *kwl;
1314 int index;
1315
Christopher Faulet784063e2020-05-18 12:14:18 +02001316 if (!out)
1317 return;
1318
Willy Tarreau8638f482012-09-18 18:01:17 +02001319 *out = NULL;
1320 list_for_each_entry(kwl, &bind_keywords.list, list) {
1321 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1322 if (kwl->kw[index].parse ||
1323 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +02001324 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
1325 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +02001326 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +02001327 kwl->kw[index].skip ? " <arg>" : "",
1328 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +02001329 }
1330 }
1331 }
1332}
1333
Willy Tarreau645513a2010-05-24 20:55:15 +02001334/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001335/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +02001336/************************************************************************/
1337
Willy Tarreaua5e37562011-12-16 17:06:15 +01001338/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +02001339static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001340smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001341{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001342 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001343 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +02001344 return 1;
1345}
1346
Willy Tarreaua5e37562011-12-16 17:06:15 +01001347/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +02001348static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001349smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001350{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001351 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001352 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +02001353 return 1;
1354}
Jerome Magnineb421b22020-03-27 22:08:40 +01001355static int
1356smp_fetch_so_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
1357{
1358 smp->data.u.str.area = smp->sess->listener->name;
1359 if (!smp->data.u.str.area)
1360 return 0;
1361
1362 smp->data.type = SMP_T_STR;
1363 smp->flags = SMP_F_CONST;
1364 smp->data.u.str.data = strlen(smp->data.u.str.area);
1365 return 1;
1366}
Willy Tarreau645513a2010-05-24 20:55:15 +02001367
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001368/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001369static 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 +02001370{
1371 struct listener *l;
1372
Willy Tarreau4348fad2012-09-20 16:48:07 +02001373 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001374 l->options |= LI_O_ACC_PROXY;
1375
1376 return 0;
1377}
1378
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001379/* parse the "accept-netscaler-cip" bind keyword */
1380static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1381{
1382 struct listener *l;
1383 uint32_t val;
1384
1385 if (!*args[cur_arg + 1]) {
1386 memprintf(err, "'%s' : missing value", args[cur_arg]);
1387 return ERR_ALERT | ERR_FATAL;
1388 }
1389
1390 val = atol(args[cur_arg + 1]);
1391 if (val <= 0) {
Willy Tarreaue2711c72019-02-27 15:39:41 +01001392 memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001393 return ERR_ALERT | ERR_FATAL;
1394 }
1395
1396 list_for_each_entry(l, &conf->listeners, by_bind) {
1397 l->options |= LI_O_ACC_CIP;
1398 conf->ns_cip_magic = val;
1399 }
1400
1401 return 0;
1402}
1403
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001404/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001405static 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 +02001406{
1407 struct listener *l;
1408 int val;
1409
1410 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001411 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001412 return ERR_ALERT | ERR_FATAL;
1413 }
1414
1415 val = atol(args[cur_arg + 1]);
Willy Tarreaue2711c72019-02-27 15:39:41 +01001416 if (val < 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001417 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001418 return ERR_ALERT | ERR_FATAL;
1419 }
1420
Willy Tarreau4348fad2012-09-20 16:48:07 +02001421 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001422 l->backlog = val;
1423
1424 return 0;
1425}
1426
1427/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001428static 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 +02001429{
1430 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001431 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +01001432 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001433
Willy Tarreau4348fad2012-09-20 16:48:07 +02001434 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001435 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001436 return ERR_ALERT | ERR_FATAL;
1437 }
1438
1439 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001440 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001441 return ERR_ALERT | ERR_FATAL;
1442 }
1443
Willy Tarreau4348fad2012-09-20 16:48:07 +02001444 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +01001445 new->luid = strtol(args[cur_arg + 1], &error, 10);
1446 if (*error != '\0') {
1447 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
1448 return ERR_ALERT | ERR_FATAL;
1449 }
Willy Tarreau4348fad2012-09-20 16:48:07 +02001450 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001451
Willy Tarreau4348fad2012-09-20 16:48:07 +02001452 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001453 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001454 return ERR_ALERT | ERR_FATAL;
1455 }
1456
Willy Tarreau4348fad2012-09-20 16:48:07 +02001457 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001458 if (node) {
1459 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001460 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
1461 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
1462 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001463 return ERR_ALERT | ERR_FATAL;
1464 }
1465
Willy Tarreau4348fad2012-09-20 16:48:07 +02001466 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001467 return 0;
1468}
1469
1470/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001471static 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 +02001472{
1473 struct listener *l;
1474 int val;
1475
1476 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001477 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001478 return ERR_ALERT | ERR_FATAL;
1479 }
1480
1481 val = atol(args[cur_arg + 1]);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001482 if (val < 0) {
1483 memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001484 return ERR_ALERT | ERR_FATAL;
1485 }
1486
Willy Tarreau4348fad2012-09-20 16:48:07 +02001487 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001488 l->maxconn = val;
1489
1490 return 0;
1491}
1492
1493/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001494static 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 +02001495{
1496 struct listener *l;
1497
1498 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001499 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001500 return ERR_ALERT | ERR_FATAL;
1501 }
1502
Willy Tarreau4348fad2012-09-20 16:48:07 +02001503 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001504 l->name = strdup(args[cur_arg + 1]);
1505
1506 return 0;
1507}
1508
1509/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001510static 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 +02001511{
1512 struct listener *l;
1513 int val;
1514
1515 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001516 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001517 return ERR_ALERT | ERR_FATAL;
1518 }
1519
1520 val = atol(args[cur_arg + 1]);
1521 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001522 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001523 return ERR_ALERT | ERR_FATAL;
1524 }
1525
Willy Tarreau4348fad2012-09-20 16:48:07 +02001526 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001527 l->nice = val;
1528
1529 return 0;
1530}
1531
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001532/* parse the "process" bind keyword */
1533static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1534{
Christopher Fauletc644fa92017-11-23 22:44:11 +01001535 char *slash;
1536 unsigned long proc = 0, thread = 0;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001537
Christopher Fauletc644fa92017-11-23 22:44:11 +01001538 if ((slash = strchr(args[cur_arg + 1], '/')) != NULL)
1539 *slash = 0;
1540
Willy Tarreauff9c9142019-02-07 10:39:36 +01001541 if (parse_process_number(args[cur_arg + 1], &proc, MAX_PROCS, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +01001542 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001543 return ERR_ALERT | ERR_FATAL;
1544 }
1545
Christopher Fauletc644fa92017-11-23 22:44:11 +01001546 if (slash) {
Willy Tarreauc9a82e42019-01-26 13:25:14 +01001547 if (parse_process_number(slash+1, &thread, MAX_THREADS, NULL, err)) {
Christopher Fauletc644fa92017-11-23 22:44:11 +01001548 memprintf(err, "'%s' : %s", args[cur_arg], *err);
1549 return ERR_ALERT | ERR_FATAL;
1550 }
1551 *slash = '/';
1552 }
1553
Willy Tarreaue26993c2020-09-03 07:18:55 +02001554 conf->settings.bind_proc |= proc;
1555 conf->settings.bind_thread |= thread;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001556 return 0;
1557}
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001558
Christopher Fauleta717b992018-04-10 14:43:00 +02001559/* parse the "proto" bind keyword */
1560static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1561{
1562 struct ist proto;
1563
1564 if (!*args[cur_arg + 1]) {
1565 memprintf(err, "'%s' : missing value", args[cur_arg]);
1566 return ERR_ALERT | ERR_FATAL;
1567 }
1568
1569 proto = ist2(args[cur_arg + 1], strlen(args[cur_arg + 1]));
1570 conf->mux_proto = get_mux_proto(proto);
1571 if (!conf->mux_proto) {
1572 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]);
1573 return ERR_ALERT | ERR_FATAL;
1574 }
Christopher Fauleta717b992018-04-10 14:43:00 +02001575 return 0;
1576}
1577
Willy Tarreau7ac908b2019-02-27 12:02:18 +01001578/* config parser for global "tune.listener.multi-queue", accepts "on" or "off" */
1579static int cfg_parse_tune_listener_mq(char **args, int section_type, struct proxy *curpx,
1580 struct proxy *defpx, const char *file, int line,
1581 char **err)
1582{
1583 if (too_many_args(1, args, err, NULL))
1584 return -1;
1585
1586 if (strcmp(args[1], "on") == 0)
1587 global.tune.options |= GTUNE_LISTENER_MQ;
1588 else if (strcmp(args[1], "off") == 0)
1589 global.tune.options &= ~GTUNE_LISTENER_MQ;
1590 else {
1591 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
1592 return -1;
1593 }
1594 return 0;
1595}
1596
Willy Tarreau61612d42012-04-19 18:42:05 +02001597/* Note: must not be declared <const> as its list will be overwritten.
1598 * Please take care of keeping this list alphabetically sorted.
1599 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001600static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001601 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
1602 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Jerome Magnineb421b22020-03-27 22:08:40 +01001603 { "so_name", smp_fetch_so_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001604 { /* END */ },
1605}};
1606
Willy Tarreau0108d902018-11-25 19:14:37 +01001607INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1608
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001609/* Note: must not be declared <const> as its list will be overwritten.
1610 * Please take care of keeping this list alphabetically sorted.
1611 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001612static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001613 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001614}};
1615
Willy Tarreau0108d902018-11-25 19:14:37 +01001616INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
1617
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001618/* Note: must not be declared <const> as its list will be overwritten.
1619 * Please take care of keeping this list alphabetically sorted, doing so helps
1620 * all code contributors.
1621 * Optional keywords are also declared with a NULL ->parse() function so that
1622 * the config parser can report an appropriate error when a known keyword was
1623 * not enabled.
1624 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001625static struct bind_kw_list bind_kws = { "ALL", { }, {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001626 { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001627 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
1628 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
1629 { "id", bind_parse_id, 1 }, /* set id of listening socket */
1630 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
1631 { "name", bind_parse_name, 1 }, /* set name of listening socket */
1632 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001633 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Christopher Fauleta717b992018-04-10 14:43:00 +02001634 { "proto", bind_parse_proto, 1 }, /* set the proto to use for all incoming connections */
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001635 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001636}};
1637
Willy Tarreau0108d902018-11-25 19:14:37 +01001638INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1639
Willy Tarreau7ac908b2019-02-27 12:02:18 +01001640/* config keyword parsers */
1641static struct cfg_kw_list cfg_kws = {ILH, {
1642 { CFG_GLOBAL, "tune.listener.multi-queue", cfg_parse_tune_listener_mq },
1643 { 0, NULL, NULL }
1644}};
1645
1646INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1647
Willy Tarreau645513a2010-05-24 20:55:15 +02001648/*
1649 * Local variables:
1650 * c-indent-level: 8
1651 * c-basic-offset: 8
1652 * End:
1653 */