blob: f6ab73c50d34ff124783f1524d72a97e1d0252ea [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 Tarreaudabf2e22007-10-28 21:59:24 +0100221/* This function adds the specified listener's file descriptor to the polling
222 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500223 * LI_FULL state depending on its number of connections. In daemon mode, we
Willy Tarreauae302532014-05-07 19:22:24 +0200224 * also support binding only the relevant processes to their respective
225 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100226 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +0200227static void enable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100228{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100229 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100230 if (listener->state == LI_LISTEN) {
William Lallemand095ba4c2017-06-01 17:38:50 +0200231 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau818a92e2020-09-03 07:50:19 +0200232 !(proc_mask(listener->rx.settings->bind_proc) & pid_bit)) {
Willy Tarreauae302532014-05-07 19:22:24 +0200233 /* we don't want to enable this listener and don't
234 * want any fd event to reach it.
235 */
Olivier Houchard1fc05162017-04-06 01:05:05 +0200236 if (!(global.tune.options & GTUNE_SOCKET_TRANSFER))
Christopher Faulet510c0d62018-03-16 10:04:47 +0100237 do_unbind_listener(listener, 1);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200238 else {
Christopher Faulet510c0d62018-03-16 10:04:47 +0100239 do_unbind_listener(listener, 0);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200240 listener->state = LI_LISTEN;
241 }
Willy Tarreauae302532014-05-07 19:22:24 +0200242 }
Willy Tarreaua8cf66b2019-02-27 16:49:00 +0100243 else if (!listener->maxconn || listener->nbconn < listener->maxconn) {
Willy Tarreau38ba6472020-08-27 08:16:52 +0200244 fd_want_recv(listener->rx.fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100245 listener->state = LI_READY;
Willy Tarreauae302532014-05-07 19:22:24 +0200246 }
247 else {
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100248 listener->state = LI_FULL;
249 }
250 }
William Lallemande22f11f2018-09-11 10:06:27 +0200251 /* if this listener is supposed to be only in the master, close it in the workers */
252 if ((global.mode & MODE_MWORKER) &&
253 (listener->options & LI_O_MWORKER) &&
254 master == 0) {
255 do_unbind_listener(listener, 1);
256 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100257 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100258}
259
260/* This function removes the specified listener's file descriptor from the
261 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
262 * enters LI_LISTEN.
263 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +0200264static void disable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100265{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100266 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100267 if (listener->state < LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200268 goto end;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100269 if (listener->state == LI_READY)
Willy Tarreau38ba6472020-08-27 08:16:52 +0200270 fd_stop_recv(listener->rx.fd);
Olivier Houchard859dc802019-08-08 15:47:21 +0200271 MT_LIST_DEL(&listener->wait_queue);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100272 listener->state = LI_LISTEN;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200273 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100274 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100275}
276
Willy Tarreaube58c382011-07-24 18:28:10 +0200277/* This function tries to temporarily disable a listener, depending on the OS
278 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
279 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
280 * closes upon SHUT_WR and refuses to rebind. So a common validation path
281 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
282 * is disabled. It normally returns non-zero, unless an error is reported.
283 */
284int pause_listener(struct listener *l)
285{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200286 int ret = 1;
287
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100288 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200289
Willy Tarreau02e19752020-09-23 17:17:22 +0200290 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
291 !(proc_mask(l->rx.settings->bind_proc) & pid_bit))
292 goto end;
293
Willy Tarreaub7436612020-08-28 19:51:44 +0200294 if (l->rx.proto->pause) {
Willy Tarreau092d8652014-07-07 20:22:12 +0200295 /* Returns < 0 in case of failure, 0 if the listener
296 * was totally stopped, or > 0 if correctly paused.
297 */
Willy Tarreaub7436612020-08-28 19:51:44 +0200298 int ret = l->rx.proto->pause(l);
Willy Tarreaube58c382011-07-24 18:28:10 +0200299
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200300 if (ret < 0) {
301 ret = 0;
302 goto end;
303 }
Willy Tarreau092d8652014-07-07 20:22:12 +0200304 else if (ret == 0)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200305 goto end;
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200306 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200307
Olivier Houchard859dc802019-08-08 15:47:21 +0200308 MT_LIST_DEL(&l->wait_queue);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200309
Willy Tarreau38ba6472020-08-27 08:16:52 +0200310 fd_stop_recv(l->rx.fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200311 l->state = LI_PAUSED;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200312 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100313 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200314 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200315}
316
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200317/* This function tries to resume a temporarily disabled listener. Paused, full,
318 * limited and disabled listeners are handled, which means that this function
319 * may replace enable_listener(). The resulting state will either be LI_READY
320 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200321 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200322 * foreground mode, and are ignored. If the listener was only in the assigned
323 * state, it's totally rebound. This can happen if a pause() has completely
324 * stopped it. If the resume fails, 0 is returned and an error might be
325 * displayed.
Willy Tarreaube58c382011-07-24 18:28:10 +0200326 */
Willy Tarreau01abd022019-02-28 10:27:18 +0100327int resume_listener(struct listener *l)
Willy Tarreaube58c382011-07-24 18:28:10 +0200328{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200329 int ret = 1;
330
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200332
Willy Tarreauf2cb1692019-07-11 10:08:31 +0200333 /* check that another thread didn't to the job in parallel (e.g. at the
334 * end of listen_accept() while we'd come from dequeue_all_listeners().
335 */
Olivier Houchard859dc802019-08-08 15:47:21 +0200336 if (MT_LIST_ADDED(&l->wait_queue))
Willy Tarreauf2cb1692019-07-11 10:08:31 +0200337 goto end;
338
William Lallemand095ba4c2017-06-01 17:38:50 +0200339 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau818a92e2020-09-03 07:50:19 +0200340 !(proc_mask(l->rx.settings->bind_proc) & pid_bit))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200341 goto end;
Willy Tarreau3569df32017-03-15 12:47:46 +0100342
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200343 if (l->state == LI_ASSIGNED) {
344 char msg[100];
345 int err;
346
Willy Tarreaub3580b12020-09-01 10:26:22 +0200347 err = l->rx.proto->listen(l, msg, sizeof(msg));
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200348 if (err & ERR_ALERT)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100349 ha_alert("Resuming listener: %s\n", msg);
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200350 else if (err & ERR_WARN)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100351 ha_warning("Resuming listener: %s\n", msg);
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200352
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200353 if (err & (ERR_FATAL | ERR_ABORT)) {
354 ret = 0;
355 goto end;
356 }
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200357 }
358
Willy Tarreauc6dac6c2020-09-23 17:34:22 +0200359 if (l->state < LI_PAUSED) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200360 ret = 0;
361 goto end;
362 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200363
Willy Tarreaub7436612020-08-28 19:51:44 +0200364 if (l->rx.proto->sock_prot == IPPROTO_TCP &&
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200365 l->state == LI_PAUSED &&
Willy Tarreau38ba6472020-08-27 08:16:52 +0200366 listen(l->rx.fd, listener_backlog(l)) != 0) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200367 ret = 0;
368 goto end;
369 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200370
371 if (l->state == LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200372 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200373
Willy Tarreaua8cf66b2019-02-27 16:49:00 +0100374 if (l->maxconn && l->nbconn >= l->maxconn) {
Willy Tarreaube58c382011-07-24 18:28:10 +0200375 l->state = LI_FULL;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200376 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200377 }
378
Willy Tarreau38ba6472020-08-27 08:16:52 +0200379 fd_want_recv(l->rx.fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200380 l->state = LI_READY;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200381 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100382 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200383 return ret;
384}
385
Willy Tarreau87b09662015-04-03 00:22:06 +0200386/* Marks a ready listener as full so that the stream code tries to re-enable
Willy Tarreau62793712011-07-24 19:23:38 +0200387 * it upon next close() using resume_listener().
388 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200389static void listener_full(struct listener *l)
Willy Tarreau62793712011-07-24 19:23:38 +0200390{
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100391 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200392 if (l->state >= LI_READY) {
Olivier Houchard859dc802019-08-08 15:47:21 +0200393 MT_LIST_DEL(&l->wait_queue);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100394 if (l->state != LI_FULL) {
Willy Tarreau38ba6472020-08-27 08:16:52 +0200395 fd_stop_recv(l->rx.fd);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100396 l->state = LI_FULL;
397 }
Willy Tarreau62793712011-07-24 19:23:38 +0200398 }
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100399 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau62793712011-07-24 19:23:38 +0200400}
401
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200402/* Marks a ready listener as limited so that we only try to re-enable it when
403 * resources are free again. It will be queued into the specified queue.
404 */
Olivier Houchard859dc802019-08-08 15:47:21 +0200405static void limit_listener(struct listener *l, struct mt_list *list)
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200406{
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100407 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200408 if (l->state == LI_READY) {
Willy Tarreaude4db172020-07-10 08:10:29 +0200409 MT_LIST_TRY_ADDQ(list, &l->wait_queue);
Willy Tarreau38ba6472020-08-27 08:16:52 +0200410 fd_stop_recv(l->rx.fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200411 l->state = LI_LIMITED;
412 }
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100413 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200414}
415
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100416/* This function adds all of the protocol's listener's file descriptors to the
417 * polling lists when they are in the LI_LISTEN state. It is intended to be
418 * used as a protocol's generic enable_all() primitive, for use after the
419 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
420 * their number of connections. It always returns ERR_NONE.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200421 *
422 * Must be called with proto_lock held.
423 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100424 */
425int enable_all_listeners(struct protocol *proto)
426{
427 struct listener *listener;
428
Willy Tarreaub7436612020-08-28 19:51:44 +0200429 list_for_each_entry(listener, &proto->listeners, rx.proto_list)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100430 enable_listener(listener);
431 return ERR_NONE;
432}
433
434/* This function removes all of the protocol's listener's file descriptors from
435 * the polling lists when they are in the LI_READY or LI_FULL states. It is
436 * intended to be used as a protocol's generic disable_all() primitive. It puts
437 * the listeners into LI_LISTEN, and always returns ERR_NONE.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200438 *
439 * Must be called with proto_lock held.
440 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100441 */
442int disable_all_listeners(struct protocol *proto)
443{
444 struct listener *listener;
445
Willy Tarreaub7436612020-08-28 19:51:44 +0200446 list_for_each_entry(listener, &proto->listeners, rx.proto_list)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100447 disable_listener(listener);
448 return ERR_NONE;
449}
450
Willy Tarreau241797a2019-12-10 14:10:52 +0100451/* Dequeues all listeners waiting for a resource the global wait queue */
452void dequeue_all_listeners()
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200453{
Willy Tarreau01abd022019-02-28 10:27:18 +0100454 struct listener *listener;
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200455
Willy Tarreau241797a2019-12-10 14:10:52 +0100456 while ((listener = MT_LIST_POP(&global_listener_queue, struct listener *, wait_queue))) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200457 /* This cannot fail because the listeners are by definition in
Willy Tarreau01abd022019-02-28 10:27:18 +0100458 * the LI_LIMITED state.
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200459 */
Willy Tarreau01abd022019-02-28 10:27:18 +0100460 resume_listener(listener);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200461 }
462}
463
Willy Tarreau241797a2019-12-10 14:10:52 +0100464/* Dequeues all listeners waiting for a resource in proxy <px>'s queue */
465void dequeue_proxy_listeners(struct proxy *px)
466{
467 struct listener *listener;
468
469 while ((listener = MT_LIST_POP(&px->listener_queue, struct listener *, wait_queue))) {
470 /* This cannot fail because the listeners are by definition in
471 * the LI_LIMITED state.
472 */
473 resume_listener(listener);
474 }
475}
476
Christopher Faulet510c0d62018-03-16 10:04:47 +0100477/* Must be called with the lock held. Depending on <do_close> value, it does
Willy Tarreaub6607bf2020-09-23 16:24:23 +0200478 * what unbind_listener or unbind_listener_no_close should do. It can also
479 * close a zombie listener's FD when called in early states.
Christopher Faulet510c0d62018-03-16 10:04:47 +0100480 */
481void do_unbind_listener(struct listener *listener, int do_close)
Willy Tarreaub648d632007-10-28 22:13:50 +0100482{
Olivier Houcharda5188562019-03-08 15:35:42 +0100483 if (listener->state == LI_READY && fd_updt)
Willy Tarreau38ba6472020-08-27 08:16:52 +0200484 fd_stop_recv(listener->rx.fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100485
Olivier Houchard859dc802019-08-08 15:47:21 +0200486 MT_LIST_DEL(&listener->wait_queue);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200487
Willy Tarreaube58c382011-07-24 18:28:10 +0200488 if (listener->state >= LI_PAUSED) {
Willy Tarreau67672452020-08-26 11:44:17 +0200489 listener->state = LI_ASSIGNED;
Willy Tarreau38ba6472020-08-27 08:16:52 +0200490 fd_stop_both(listener->rx.fd);
Willy Tarreaub6607bf2020-09-23 16:24:23 +0200491 }
492
493 if (do_close && listener->rx.fd != -1) {
494 fd_delete(listener->rx.fd);
495 listener->rx.flags &= ~RX_F_BOUND;
496 listener->rx.fd = -1;
Willy Tarreaub648d632007-10-28 22:13:50 +0100497 }
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100498}
499
Olivier Houchard1fc05162017-04-06 01:05:05 +0200500/* This function closes the listening socket for the specified listener,
501 * provided that it's already in a listening state. The listener enters the
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100502 * LI_ASSIGNED state. This function is intended to be used as a generic
503 * function for standard protocols.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200504 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100505void unbind_listener(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200506{
Christopher Faulet510c0d62018-03-16 10:04:47 +0100507 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100508 do_unbind_listener(listener, 1);
Christopher Faulet510c0d62018-03-16 10:04:47 +0100509 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200510}
511
512/* This function pretends the listener is dead, but keeps the FD opened, so
513 * that we can provide it, for conf reloading.
514 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100515void unbind_listener_no_close(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200516{
Christopher Faulet510c0d62018-03-16 10:04:47 +0100517 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100518 do_unbind_listener(listener, 0);
Christopher Faulet510c0d62018-03-16 10:04:47 +0100519 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200520}
521
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200522/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
523 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200524 * allocation). The address family is taken from ss->ss_family, and the protocol
525 * passed in <proto> must be usable on this family. The number of jobs and
526 * listeners is automatically increased by the number of listeners created. It
527 * returns non-zero on success, zero on error with the error message set in <err>.
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200528 */
529int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200530 int portl, int porth, int fd, struct protocol *proto, char **err)
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200531{
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200532 struct listener *l;
533 int port;
534
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200535 for (port = portl; port <= porth; port++) {
536 l = calloc(1, sizeof(*l));
537 if (!l) {
538 memprintf(err, "out of memory");
539 return 0;
540 }
541 l->obj_type = OBJ_TYPE_LISTENER;
542 LIST_ADDQ(&bc->frontend->conf.listeners, &l->by_fe);
543 LIST_ADDQ(&bc->listeners, &l->by_bind);
544 l->bind_conf = bc;
Willy Tarreau0fce6bc2020-09-03 07:46:06 +0200545 l->rx.settings = &bc->settings;
Willy Tarreaueef45422020-09-03 10:05:03 +0200546 l->rx.owner = l;
Willy Tarreau38ba6472020-08-27 08:16:52 +0200547 l->rx.fd = fd;
Willy Tarreau37159062020-08-27 07:48:42 +0200548 memcpy(&l->rx.addr, ss, sizeof(*ss));
Olivier Houchard859dc802019-08-08 15:47:21 +0200549 MT_LIST_INIT(&l->wait_queue);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200550 l->state = LI_INIT;
551
552 proto->add(l, port);
553
Willy Tarreau909c23b2020-09-15 13:50:58 +0200554 if (fd != -1)
Willy Tarreau43046fa2020-09-01 15:41:59 +0200555 l->rx.flags |= RX_F_INHERITED;
William Lallemand75ea0a02017-11-15 19:02:58 +0100556
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100557 HA_SPIN_INIT(&l->lock);
Olivier Houchard64213e92019-03-08 18:52:57 +0100558 _HA_ATOMIC_ADD(&jobs, 1);
559 _HA_ATOMIC_ADD(&listeners, 1);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200560 }
561 return 1;
562}
563
Willy Tarreau1a64d162007-10-28 22:26:05 +0100564/* Delete a listener from its protocol's list of listeners. The listener's
565 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
Willy Tarreau2cc5bae2017-09-15 08:18:11 +0200566 * number of listeners is updated, as well as the global number of listeners
567 * and jobs. Note that the listener must have previously been unbound. This
568 * is the generic function to use to remove a listener.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200569 *
570 * Will grab the proto_lock.
571 *
Willy Tarreau1a64d162007-10-28 22:26:05 +0100572 */
573void delete_listener(struct listener *listener)
574{
Willy Tarreau6ee9f8d2019-08-26 10:55:52 +0200575 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100576 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100577 if (listener->state == LI_ASSIGNED) {
578 listener->state = LI_INIT;
Willy Tarreaub7436612020-08-28 19:51:44 +0200579 LIST_DEL(&listener->rx.proto_list);
580 listener->rx.proto->nb_listeners--;
Olivier Houchard64213e92019-03-08 18:52:57 +0100581 _HA_ATOMIC_SUB(&jobs, 1);
582 _HA_ATOMIC_SUB(&listeners, 1);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100583 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100584 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau6ee9f8d2019-08-26 10:55:52 +0200585 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100586}
587
Willy Tarreaue2711c72019-02-27 15:39:41 +0100588/* Returns a suitable value for a listener's backlog. It uses the listener's,
589 * otherwise the frontend's backlog, otherwise the listener's maxconn,
590 * otherwise the frontend's maxconn, otherwise 1024.
591 */
592int listener_backlog(const struct listener *l)
593{
594 if (l->backlog)
595 return l->backlog;
596
597 if (l->bind_conf->frontend->backlog)
598 return l->bind_conf->frontend->backlog;
599
600 if (l->maxconn)
601 return l->maxconn;
602
603 if (l->bind_conf->frontend->maxconn)
604 return l->bind_conf->frontend->maxconn;
605
606 return 1024;
607}
608
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200609/* This function is called on a read event from a listening socket, corresponding
610 * to an accept. It tries to accept as many connections as possible, and for each
611 * calls the listener's accept handler (generally the frontend's accept handler).
612 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200613void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200614{
615 struct listener *l = fdtab[fd].owner;
Olivier Houchardd16a9df2019-02-25 16:18:16 +0100616 struct proxy *p;
Christopher Faulet102854c2019-04-30 12:17:13 +0200617 unsigned int max_accept;
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100618 int next_conn = 0;
Willy Tarreau82c97892019-02-27 19:32:32 +0100619 int next_feconn = 0;
620 int next_actconn = 0;
Willy Tarreaubb660302014-05-07 19:47:02 +0200621 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200622 int cfd;
623 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100624#ifdef USE_ACCEPT4
625 static int accept4_broken;
626#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200627
Olivier Houchardd16a9df2019-02-25 16:18:16 +0100628 if (!l)
629 return;
630 p = l->bind_conf->frontend;
Christopher Faulet102854c2019-04-30 12:17:13 +0200631
632 /* if l->maxaccept is -1, then max_accept is UINT_MAX. It is not really
633 * illimited, but it is probably enough.
634 */
Olivier Houchardd16a9df2019-02-25 16:18:16 +0100635 max_accept = l->maxaccept ? l->maxaccept : 1;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200636
Willy Tarreau93e7c002013-10-07 18:51:07 +0200637 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
638 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200639
640 if (unlikely(!max)) {
641 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200642 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +0100643 goto limit_global;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200644 }
645
646 if (max_accept > max)
647 max_accept = max;
648 }
649
650 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200651 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
652
653 if (unlikely(!max)) {
654 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200655 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +0100656 goto limit_global;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200657 }
658
659 if (max_accept > max)
660 max_accept = max;
661 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200662#ifdef USE_OPENSSL
663 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
664 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200665
Willy Tarreaue43d5322013-10-07 20:01:52 +0200666 if (unlikely(!max)) {
667 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200668 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreau0591bf72019-12-10 12:01:21 +0100669 goto limit_global;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200670 }
671
672 if (max_accept > max)
673 max_accept = max;
674 }
675#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200676 if (p && p->fe_sps_lim) {
677 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
678
679 if (unlikely(!max)) {
680 /* frontend accept rate limit was reached */
Willy Tarreau0591bf72019-12-10 12:01:21 +0100681 expire = tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0));
682 goto limit_proxy;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200683 }
684
685 if (max_accept > max)
686 max_accept = max;
687 }
688
689 /* Note: if we fail to allocate a connection because of configured
690 * limits, we'll schedule a new attempt worst 1 second later in the
691 * worst case. If we fail due to system limits or temporary resource
692 * shortage, we try again 100ms later in the worst case.
693 */
Christopher Faulet102854c2019-04-30 12:17:13 +0200694 for (; max_accept; next_conn = next_feconn = next_actconn = 0, max_accept--) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200695 struct sockaddr_storage addr;
696 socklen_t laddr = sizeof(addr);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200697 unsigned int count;
Willy Tarreauaf613e82020-06-05 08:40:51 +0200698 __decl_thread(unsigned long mask);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200699
Willy Tarreau82c97892019-02-27 19:32:32 +0100700 /* pre-increase the number of connections without going too far.
701 * We process the listener, then the proxy, then the process.
702 * We know which ones to unroll based on the next_xxx value.
703 */
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100704 do {
705 count = l->nbconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +0100706 if (unlikely(l->maxconn && count >= l->maxconn)) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100707 /* the listener was marked full or another
708 * thread is going to do it.
709 */
710 next_conn = 0;
Willy Tarreau93604ed2019-11-15 10:20:07 +0100711 listener_full(l);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100712 goto end;
713 }
714 next_conn = count + 1;
David Carlier56716622019-03-27 16:08:42 +0000715 } while (!_HA_ATOMIC_CAS(&l->nbconn, (int *)(&count), next_conn));
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100716
Willy Tarreau82c97892019-02-27 19:32:32 +0100717 if (p) {
718 do {
719 count = p->feconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +0100720 if (unlikely(count >= p->maxconn)) {
Willy Tarreau82c97892019-02-27 19:32:32 +0100721 /* the frontend was marked full or another
722 * thread is going to do it.
723 */
724 next_feconn = 0;
Willy Tarreau0591bf72019-12-10 12:01:21 +0100725 expire = TICK_ETERNITY;
726 goto limit_proxy;
Willy Tarreau82c97892019-02-27 19:32:32 +0100727 }
728 next_feconn = count + 1;
Olivier Houchard64213e92019-03-08 18:52:57 +0100729 } while (!_HA_ATOMIC_CAS(&p->feconn, &count, next_feconn));
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200730 }
731
Willy Tarreau82c97892019-02-27 19:32:32 +0100732 if (!(l->options & LI_O_UNLIMITED)) {
733 do {
734 count = actconn;
Willy Tarreau93604ed2019-11-15 10:20:07 +0100735 if (unlikely(count >= global.maxconn)) {
Willy Tarreau82c97892019-02-27 19:32:32 +0100736 /* the process was marked full or another
737 * thread is going to do it.
738 */
739 next_actconn = 0;
Willy Tarreau0591bf72019-12-10 12:01:21 +0100740 expire = tick_add(now_ms, 1000); /* try again in 1 second */
741 goto limit_global;
Willy Tarreau82c97892019-02-27 19:32:32 +0100742 }
743 next_actconn = count + 1;
David Carlier56716622019-03-27 16:08:42 +0000744 } while (!_HA_ATOMIC_CAS(&actconn, (int *)(&count), next_actconn));
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200745 }
746
William Lallemand2fe7dd02018-09-11 16:51:29 +0200747 /* with sockpair@ we don't want to do an accept */
Willy Tarreau37159062020-08-27 07:48:42 +0200748 if (unlikely(l->rx.addr.ss_family == AF_CUST_SOCKPAIR)) {
William Lallemand2fe7dd02018-09-11 16:51:29 +0200749 if ((cfd = recv_fd_uxst(fd)) != -1)
William Lallemandd9138002018-11-27 12:02:39 +0100750 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau888d5672019-01-27 18:34:12 +0100751 /* just like with UNIX sockets, only the family is filled */
752 addr.ss_family = AF_UNIX;
753 laddr = sizeof(addr.ss_family);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200754 } else
755
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200756#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100757 /* only call accept4() if it's known to be safe, otherwise
758 * fallback to the legacy accept() + fcntl().
759 */
760 if (unlikely(accept4_broken ||
William Lallemandd9138002018-11-27 12:02:39 +0100761 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
Willy Tarreau818dca52014-01-31 19:40:19 +0100762 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
763 (accept4_broken = 1))))
764#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200765 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
William Lallemandd9138002018-11-27 12:02:39 +0100766 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100767
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200768 if (unlikely(cfd == -1)) {
769 switch (errno) {
770 case EAGAIN:
Willy Tarreau20aeb1c2019-12-10 08:42:21 +0100771 if (fdtab[fd].ev & (FD_POLL_HUP|FD_POLL_ERR)) {
Willy Tarreaubb660302014-05-07 19:47:02 +0200772 /* the listening socket might have been disabled in a shared
773 * process and we're a collateral victim. We'll just pause for
774 * a while in case it comes back. In the mean time, we need to
775 * clear this sticky flag.
776 */
Willy Tarreau20aeb1c2019-12-10 08:42:21 +0100777 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Willy Tarreaubb660302014-05-07 19:47:02 +0200778 goto transient_error;
779 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200780 goto end; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200781 case EINVAL:
782 /* might be trying to accept on a shut fd (eg: soft stop) */
783 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100784 case EINTR:
785 case ECONNABORTED:
Olivier Houchard64213e92019-03-08 18:52:57 +0100786 _HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreau82c97892019-02-27 19:32:32 +0100787 if (p)
Olivier Houchard64213e92019-03-08 18:52:57 +0100788 _HA_ATOMIC_SUB(&p->feconn, 1);
Willy Tarreau82c97892019-02-27 19:32:32 +0100789 if (!(l->options & LI_O_UNLIMITED))
Olivier Houchard64213e92019-03-08 18:52:57 +0100790 _HA_ATOMIC_SUB(&actconn, 1);
Willy Tarreaua593ec52014-01-20 21:21:30 +0100791 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200792 case ENFILE:
793 if (p)
794 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100795 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
796 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200797 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200798 case EMFILE:
799 if (p)
800 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100801 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
802 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200803 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200804 case ENOBUFS:
805 case ENOMEM:
806 if (p)
807 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100808 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
809 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200810 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200811 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100812 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau92079932019-12-10 09:30:05 +0100813 max_accept = 0;
814 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200815 }
816 }
817
William Lallemandd9138002018-11-27 12:02:39 +0100818 /* we don't want to leak the FD upon reload if it's in the master */
819 if (unlikely(master == 1))
820 fcntl(cfd, F_SETFD, FD_CLOEXEC);
821
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100822 /* The connection was accepted, it must be counted as such */
823 if (l->counters)
824 HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, next_conn);
825
Willy Tarreau82c97892019-02-27 19:32:32 +0100826 if (p)
827 HA_ATOMIC_UPDATE_MAX(&p->fe_counters.conn_max, next_feconn);
828
829 proxy_inc_fe_conn_ctr(l, p);
830
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100831 if (!(l->options & LI_O_UNLIMITED)) {
832 count = update_freq_ctr(&global.conn_per_sec, 1);
833 HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100834 }
835
Willy Tarreau64a9c052019-04-12 15:27:17 +0200836 _HA_ATOMIC_ADD(&activity[tid].accepted, 1);
837
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200838 if (unlikely(cfd >= global.maxsock)) {
839 send_log(p, LOG_EMERG,
840 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
841 p->id);
842 close(cfd);
Willy Tarreau0591bf72019-12-10 12:01:21 +0100843 expire = tick_add(now_ms, 1000); /* try again in 1 second */
844 goto limit_global;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200845 }
846
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100847 /* past this point, l->accept() will automatically decrement
Willy Tarreau82c97892019-02-27 19:32:32 +0100848 * l->nbconn, feconn and actconn once done. Setting next_*conn=0
849 * allows the error path not to rollback on nbconn. It's more
850 * convenient than duplicating all exit labels.
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100851 */
852 next_conn = 0;
Willy Tarreau82c97892019-02-27 19:32:32 +0100853 next_feconn = 0;
854 next_actconn = 0;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200855
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100856#if defined(USE_THREAD)
Willy Tarreau818a92e2020-09-03 07:50:19 +0200857 mask = thread_mask(l->rx.settings->bind_thread) & all_threads_mask;
Willy Tarreaua7da5e82020-03-12 17:33:29 +0100858 if (atleast2(mask) && (global.tune.options & GTUNE_LISTENER_MQ) && !stopping) {
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100859 struct accept_queue_ring *ring;
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100860 unsigned int t, t0, t1, t2;
Willy Tarreaufc630bd2019-03-04 19:57:34 +0100861
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100862 /* The principle is that we have two running indexes,
863 * each visiting in turn all threads bound to this
864 * listener. The connection will be assigned to the one
865 * with the least connections, and the other one will
866 * be updated. This provides a good fairness on short
Willy Tarreaufc630bd2019-03-04 19:57:34 +0100867 * connections (round robin) and on long ones (conn
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100868 * count), without ever missing any idle thread.
Willy Tarreaufc630bd2019-03-04 19:57:34 +0100869 */
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100870
871 /* keep a copy for the final update. thr_idx is composite
872 * and made of (t2<<16) + t1.
873 */
Willy Tarreau0cf33172019-03-06 15:26:33 +0100874 t0 = l->thr_idx;
Willy Tarreaufc630bd2019-03-04 19:57:34 +0100875 do {
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100876 unsigned long m1, m2;
877 int q1, q2;
878
879 t2 = t1 = t0;
880 t2 >>= 16;
881 t1 &= 0xFFFF;
882
883 /* t1 walks low to high bits ;
884 * t2 walks high to low.
885 */
886 m1 = mask >> t1;
887 m2 = mask & (t2 ? nbits(t2 + 1) : ~0UL);
888
Willy Tarreau85d04242019-04-16 18:09:13 +0200889 if (unlikely(!(m1 & 1))) {
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100890 m1 &= ~1UL;
891 if (!m1) {
892 m1 = mask;
893 t1 = 0;
894 }
895 t1 += my_ffsl(m1) - 1;
896 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100897
Willy Tarreau85d04242019-04-16 18:09:13 +0200898 if (unlikely(!(m2 & (1UL << t2)) || t1 == t2)) {
899 /* highest bit not set */
900 if (!m2)
901 m2 = mask;
902
903 t2 = my_flsl(m2) - 1;
904 }
905
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100906 /* now we have two distinct thread IDs belonging to the mask */
907 q1 = accept_queue_rings[t1].tail - accept_queue_rings[t1].head + ACCEPT_QUEUE_SIZE;
908 if (q1 >= ACCEPT_QUEUE_SIZE)
909 q1 -= ACCEPT_QUEUE_SIZE;
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100910
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100911 q2 = accept_queue_rings[t2].tail - accept_queue_rings[t2].head + ACCEPT_QUEUE_SIZE;
912 if (q2 >= ACCEPT_QUEUE_SIZE)
913 q2 -= ACCEPT_QUEUE_SIZE;
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100914
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100915 /* we have 3 possibilities now :
916 * q1 < q2 : t1 is less loaded than t2, so we pick it
917 * and update t2 (since t1 might still be
918 * lower than another thread)
919 * q1 > q2 : t2 is less loaded than t1, so we pick it
920 * and update t1 (since t2 might still be
921 * lower than another thread)
922 * q1 = q2 : both are equally loaded, thus we pick t1
923 * and update t1 as it will become more loaded
924 * than t2.
925 */
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100926
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100927 q1 += l->thr_conn[t1];
928 q2 += l->thr_conn[t2];
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100929
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100930 if (q1 - q2 < 0) {
931 t = t1;
932 t2 = t2 ? t2 - 1 : LONGBITS - 1;
933 }
934 else if (q1 - q2 > 0) {
935 t = t2;
936 t1++;
937 if (t1 >= LONGBITS)
938 t1 = 0;
939 }
940 else {
941 t = t1;
942 t1++;
943 if (t1 >= LONGBITS)
944 t1 = 0;
945 }
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100946
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100947 /* new value for thr_idx */
948 t1 += (t2 << 16);
Olivier Houchard64213e92019-03-08 18:52:57 +0100949 } while (unlikely(!_HA_ATOMIC_CAS(&l->thr_idx, &t0, t1)));
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100950
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100951 /* We successfully selected the best thread "t" for this
952 * connection. We use deferred accepts even if it's the
953 * local thread because tests show that it's the best
954 * performing model, likely due to better cache locality
955 * when processing this loop.
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100956 */
Willy Tarreau0fe703b2019-03-05 08:46:28 +0100957 ring = &accept_queue_rings[t];
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100958 if (accept_queue_push_mp(ring, cfd, l, &addr, laddr)) {
Olivier Houchard64213e92019-03-08 18:52:57 +0100959 _HA_ATOMIC_ADD(&activity[t].accq_pushed, 1);
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200960 tasklet_wakeup(ring->tasklet);
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100961 continue;
962 }
963 /* If the ring is full we do a synchronous accept on
964 * the local thread here.
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100965 */
Olivier Houchard64213e92019-03-08 18:52:57 +0100966 _HA_ATOMIC_ADD(&activity[t].accq_full, 1);
Willy Tarreaue0e9c482019-01-27 15:37:19 +0100967 }
968#endif // USE_THREAD
969
Olivier Houchard64213e92019-03-08 18:52:57 +0100970 _HA_ATOMIC_ADD(&l->thr_conn[tid], 1);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200971 ret = l->accept(l, cfd, &addr);
972 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200973 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200974 * we just have to ignore it (ret == 0) or it's a critical
975 * error due to a resource shortage, and we must stop the
976 * listener (ret < 0).
977 */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200978 if (ret == 0) /* successful termination */
979 continue;
980
Willy Tarreaubb660302014-05-07 19:47:02 +0200981 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200982 }
983
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100984 /* increase the per-process number of cumulated sessions, this
985 * may only be done once l->accept() has accepted the connection.
986 */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200987 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200988 count = update_freq_ctr(&global.sess_per_sec, 1);
989 HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200990 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200991#ifdef USE_OPENSSL
992 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200993 count = update_freq_ctr(&global.ssl_per_sec, 1);
994 HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
Willy Tarreaue43d5322013-10-07 20:01:52 +0200995 }
996#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200997
Willy Tarreau8d2c98b2020-05-01 09:51:11 +0200998 ti->flags &= ~TI_FL_STUCK; // this thread is still running
Willy Tarreau3f0d02b2019-02-25 19:23:37 +0100999 } /* end of for (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001000
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001001 end:
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001002 if (next_conn)
Olivier Houchard64213e92019-03-08 18:52:57 +01001003 _HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreau741b4d62019-02-25 15:02:04 +01001004
Willy Tarreau82c97892019-02-27 19:32:32 +01001005 if (p && next_feconn)
Olivier Houchard64213e92019-03-08 18:52:57 +01001006 _HA_ATOMIC_SUB(&p->feconn, 1);
Willy Tarreau82c97892019-02-27 19:32:32 +01001007
1008 if (next_actconn)
Olivier Houchard64213e92019-03-08 18:52:57 +01001009 _HA_ATOMIC_SUB(&actconn, 1);
Willy Tarreau82c97892019-02-27 19:32:32 +01001010
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001011 if ((l->state == LI_FULL && (!l->maxconn || l->nbconn < l->maxconn)) ||
Willy Tarreaucdcba112019-12-11 15:06:30 +01001012 (l->state == LI_LIMITED &&
1013 ((!p || p->feconn < p->maxconn) && (actconn < global.maxconn) &&
1014 (!tick_isset(global_listener_queue_task->expire) ||
1015 tick_is_expired(global_listener_queue_task->expire, now_ms))))) {
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001016 /* at least one thread has to this when quitting */
1017 resume_listener(l);
1018
1019 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001020 dequeue_all_listeners();
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001021
Olivier Houchard859dc802019-08-08 15:47:21 +02001022 if (p && !MT_LIST_ISEMPTY(&p->listener_queue) &&
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001023 (!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 +01001024 dequeue_proxy_listeners(p);
Willy Tarreau3f0d02b2019-02-25 19:23:37 +01001025 }
Willy Tarreau4c044e22019-12-05 07:40:32 +01001026
Willy Tarreau92079932019-12-10 09:30:05 +01001027 /* Now it's getting tricky. The listener was supposed to be in LI_READY
1028 * state but in the mean time we might have changed it to LI_FULL or
1029 * LI_LIMITED, and another thread might also have turned it to
1030 * LI_PAUSED, LI_LISTEN or even LI_INI when stopping a proxy. We must
1031 * be certain to keep the FD enabled when in the READY state but we
1032 * must also stop it for other states that we might have switched to
1033 * while others re-enabled polling.
1034 */
1035 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
1036 if (l->state == LI_READY) {
1037 if (max_accept > 0)
1038 fd_cant_recv(fd);
1039 else
1040 fd_done_recv(fd);
1041 } else if (l->state > LI_ASSIGNED) {
Willy Tarreau38ba6472020-08-27 08:16:52 +02001042 fd_stop_recv(l->rx.fd);
Willy Tarreau92079932019-12-10 09:30:05 +01001043 }
1044 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001045 return;
1046
1047 transient_error:
1048 /* pause the listener for up to 100 ms */
1049 expire = tick_add(now_ms, 100);
1050
1051 limit_global:
1052 /* (re-)queue the listener to the global queue and set it to expire no
1053 * later than <expire> ahead. The listener turns to LI_LIMITED.
1054 */
1055 limit_listener(l, &global_listener_queue);
1056 task_schedule(global_listener_queue_task, expire);
1057 goto end;
1058
1059 limit_proxy:
1060 /* (re-)queue the listener to the proxy's queue and set it to expire no
1061 * later than <expire> ahead. The listener turns to LI_LIMITED.
1062 */
1063 limit_listener(l, &p->listener_queue);
Willy Tarreaueeea8082020-01-08 19:15:07 +01001064 if (p->task && tick_isset(expire))
1065 task_schedule(p->task, expire);
Willy Tarreau0591bf72019-12-10 12:01:21 +01001066 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +02001067}
1068
Willy Tarreau05f50472017-09-15 09:19:58 +02001069/* Notify the listener that a connection initiated from it was released. This
1070 * is used to keep the connection count consistent and to possibly re-open
1071 * listening when it was limited.
1072 */
1073void listener_release(struct listener *l)
1074{
1075 struct proxy *fe = l->bind_conf->frontend;
1076
1077 if (!(l->options & LI_O_UNLIMITED))
Olivier Houchard64213e92019-03-08 18:52:57 +01001078 _HA_ATOMIC_SUB(&actconn, 1);
Willy Tarreau82c97892019-02-27 19:32:32 +01001079 if (fe)
Olivier Houchard64213e92019-03-08 18:52:57 +01001080 _HA_ATOMIC_SUB(&fe->feconn, 1);
1081 _HA_ATOMIC_SUB(&l->nbconn, 1);
1082 _HA_ATOMIC_SUB(&l->thr_conn[tid], 1);
Willy Tarreau82c97892019-02-27 19:32:32 +01001083
1084 if (l->state == LI_FULL || l->state == LI_LIMITED)
Willy Tarreau05f50472017-09-15 09:19:58 +02001085 resume_listener(l);
1086
1087 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001088 dequeue_all_listeners();
Willy Tarreau05f50472017-09-15 09:19:58 +02001089
Olivier Houchard859dc802019-08-08 15:47:21 +02001090 if (!MT_LIST_ISEMPTY(&fe->listener_queue) &&
Willy Tarreau05f50472017-09-15 09:19:58 +02001091 (!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 +01001092 dequeue_proxy_listeners(fe);
Willy Tarreau05f50472017-09-15 09:19:58 +02001093}
1094
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001095/* Initializes the listener queues. Returns 0 on success, otherwise ERR_* flags */
1096static int listener_queue_init()
1097{
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001098 global_listener_queue_task = task_new(MAX_THREADS_MASK);
1099 if (!global_listener_queue_task) {
1100 ha_alert("Out of memory when initializing global listener queue\n");
1101 return ERR_FATAL|ERR_ABORT;
1102 }
1103 /* very simple initialization, users will queue the task if needed */
1104 global_listener_queue_task->context = NULL; /* not even a context! */
1105 global_listener_queue_task->process = manage_global_listener_queue;
1106
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001107 return 0;
1108}
1109
1110static void listener_queue_deinit()
1111{
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001112 task_destroy(global_listener_queue_task);
1113 global_listener_queue_task = NULL;
Willy Tarreauf2cb1692019-07-11 10:08:31 +02001114}
1115
1116REGISTER_CONFIG_POSTPARSER("multi-threaded listener queue", listener_queue_init);
1117REGISTER_POST_DEINIT(listener_queue_deinit);
1118
Willy Tarreaua1d97f82019-12-10 11:18:41 +01001119
1120/* This is the global management task for listeners. It enables listeners waiting
1121 * for global resources when there are enough free resource, or at least once in
1122 * a while. It is designed to be called as a task.
1123 */
1124static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state)
1125{
1126 /* If there are still too many concurrent connections, let's wait for
1127 * some of them to go away. We don't need to re-arm the timer because
1128 * each of them will scan the queue anyway.
1129 */
1130 if (unlikely(actconn >= global.maxconn))
1131 goto out;
1132
1133 /* We should periodically try to enable listeners waiting for a global
1134 * resource here, because it is possible, though very unlikely, that
1135 * they have been blocked by a temporary lack of global resource such
1136 * as a file descriptor or memory and that the temporary condition has
1137 * disappeared.
1138 */
1139 dequeue_all_listeners();
1140
1141 out:
1142 t->expire = TICK_ETERNITY;
1143 task_queue(t);
1144 return t;
1145}
1146
Willy Tarreau26982662012-09-12 23:17:10 +02001147/*
1148 * Registers the bind keyword list <kwl> as a list of valid keywords for next
1149 * parsing sessions.
1150 */
1151void bind_register_keywords(struct bind_kw_list *kwl)
1152{
1153 LIST_ADDQ(&bind_keywords.list, &kwl->list);
1154}
1155
1156/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
1157 * keyword is found with a NULL ->parse() function, then an attempt is made to
1158 * find one with a valid ->parse() function. This way it is possible to declare
1159 * platform-dependant, known keywords as NULL, then only declare them as valid
1160 * if some options are met. Note that if the requested keyword contains an
1161 * opening parenthesis, everything from this point is ignored.
1162 */
1163struct bind_kw *bind_find_kw(const char *kw)
1164{
1165 int index;
1166 const char *kwend;
1167 struct bind_kw_list *kwl;
1168 struct bind_kw *ret = NULL;
1169
1170 kwend = strchr(kw, '(');
1171 if (!kwend)
1172 kwend = kw + strlen(kw);
1173
1174 list_for_each_entry(kwl, &bind_keywords.list, list) {
1175 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1176 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1177 kwl->kw[index].kw[kwend-kw] == 0) {
1178 if (kwl->kw[index].parse)
1179 return &kwl->kw[index]; /* found it !*/
1180 else
1181 ret = &kwl->kw[index]; /* may be OK */
1182 }
1183 }
1184 }
1185 return ret;
1186}
1187
Willy Tarreau8638f482012-09-18 18:01:17 +02001188/* Dumps all registered "bind" keywords to the <out> string pointer. The
1189 * unsupported keywords are only dumped if their supported form was not
1190 * found.
1191 */
1192void bind_dump_kws(char **out)
1193{
1194 struct bind_kw_list *kwl;
1195 int index;
1196
Christopher Faulet784063e2020-05-18 12:14:18 +02001197 if (!out)
1198 return;
1199
Willy Tarreau8638f482012-09-18 18:01:17 +02001200 *out = NULL;
1201 list_for_each_entry(kwl, &bind_keywords.list, list) {
1202 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1203 if (kwl->kw[index].parse ||
1204 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +02001205 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
1206 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +02001207 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +02001208 kwl->kw[index].skip ? " <arg>" : "",
1209 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +02001210 }
1211 }
1212 }
1213}
1214
Willy Tarreau645513a2010-05-24 20:55:15 +02001215/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001216/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +02001217/************************************************************************/
1218
Willy Tarreaua5e37562011-12-16 17:06:15 +01001219/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +02001220static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001221smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001222{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001223 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001224 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +02001225 return 1;
1226}
1227
Willy Tarreaua5e37562011-12-16 17:06:15 +01001228/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +02001229static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001230smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001231{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001232 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001233 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +02001234 return 1;
1235}
Jerome Magnineb421b22020-03-27 22:08:40 +01001236static int
1237smp_fetch_so_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
1238{
1239 smp->data.u.str.area = smp->sess->listener->name;
1240 if (!smp->data.u.str.area)
1241 return 0;
1242
1243 smp->data.type = SMP_T_STR;
1244 smp->flags = SMP_F_CONST;
1245 smp->data.u.str.data = strlen(smp->data.u.str.area);
1246 return 1;
1247}
Willy Tarreau645513a2010-05-24 20:55:15 +02001248
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001249/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001250static 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 +02001251{
1252 struct listener *l;
1253
Willy Tarreau4348fad2012-09-20 16:48:07 +02001254 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001255 l->options |= LI_O_ACC_PROXY;
1256
1257 return 0;
1258}
1259
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001260/* parse the "accept-netscaler-cip" bind keyword */
1261static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1262{
1263 struct listener *l;
1264 uint32_t val;
1265
1266 if (!*args[cur_arg + 1]) {
1267 memprintf(err, "'%s' : missing value", args[cur_arg]);
1268 return ERR_ALERT | ERR_FATAL;
1269 }
1270
1271 val = atol(args[cur_arg + 1]);
1272 if (val <= 0) {
Willy Tarreaue2711c72019-02-27 15:39:41 +01001273 memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001274 return ERR_ALERT | ERR_FATAL;
1275 }
1276
1277 list_for_each_entry(l, &conf->listeners, by_bind) {
1278 l->options |= LI_O_ACC_CIP;
1279 conf->ns_cip_magic = val;
1280 }
1281
1282 return 0;
1283}
1284
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001285/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001286static 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 +02001287{
1288 struct listener *l;
1289 int val;
1290
1291 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001292 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001293 return ERR_ALERT | ERR_FATAL;
1294 }
1295
1296 val = atol(args[cur_arg + 1]);
Willy Tarreaue2711c72019-02-27 15:39:41 +01001297 if (val < 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001298 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001299 return ERR_ALERT | ERR_FATAL;
1300 }
1301
Willy Tarreau4348fad2012-09-20 16:48:07 +02001302 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001303 l->backlog = val;
1304
1305 return 0;
1306}
1307
1308/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001309static 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 +02001310{
1311 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001312 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +01001313 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001314
Willy Tarreau4348fad2012-09-20 16:48:07 +02001315 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001316 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001317 return ERR_ALERT | ERR_FATAL;
1318 }
1319
1320 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001321 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001322 return ERR_ALERT | ERR_FATAL;
1323 }
1324
Willy Tarreau4348fad2012-09-20 16:48:07 +02001325 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +01001326 new->luid = strtol(args[cur_arg + 1], &error, 10);
1327 if (*error != '\0') {
1328 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
1329 return ERR_ALERT | ERR_FATAL;
1330 }
Willy Tarreau4348fad2012-09-20 16:48:07 +02001331 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001332
Willy Tarreau4348fad2012-09-20 16:48:07 +02001333 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001334 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001335 return ERR_ALERT | ERR_FATAL;
1336 }
1337
Willy Tarreau4348fad2012-09-20 16:48:07 +02001338 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001339 if (node) {
1340 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001341 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
1342 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
1343 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001344 return ERR_ALERT | ERR_FATAL;
1345 }
1346
Willy Tarreau4348fad2012-09-20 16:48:07 +02001347 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001348 return 0;
1349}
1350
1351/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001352static 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 +02001353{
1354 struct listener *l;
1355 int val;
1356
1357 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001358 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001359 return ERR_ALERT | ERR_FATAL;
1360 }
1361
1362 val = atol(args[cur_arg + 1]);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001363 if (val < 0) {
1364 memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001365 return ERR_ALERT | ERR_FATAL;
1366 }
1367
Willy Tarreau4348fad2012-09-20 16:48:07 +02001368 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001369 l->maxconn = val;
1370
1371 return 0;
1372}
1373
1374/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001375static 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 +02001376{
1377 struct listener *l;
1378
1379 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001380 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001381 return ERR_ALERT | ERR_FATAL;
1382 }
1383
Willy Tarreau4348fad2012-09-20 16:48:07 +02001384 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001385 l->name = strdup(args[cur_arg + 1]);
1386
1387 return 0;
1388}
1389
1390/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001391static 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 +02001392{
1393 struct listener *l;
1394 int val;
1395
1396 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001397 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001398 return ERR_ALERT | ERR_FATAL;
1399 }
1400
1401 val = atol(args[cur_arg + 1]);
1402 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001403 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001404 return ERR_ALERT | ERR_FATAL;
1405 }
1406
Willy Tarreau4348fad2012-09-20 16:48:07 +02001407 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001408 l->nice = val;
1409
1410 return 0;
1411}
1412
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001413/* parse the "process" bind keyword */
1414static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1415{
Christopher Fauletc644fa92017-11-23 22:44:11 +01001416 char *slash;
1417 unsigned long proc = 0, thread = 0;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001418
Christopher Fauletc644fa92017-11-23 22:44:11 +01001419 if ((slash = strchr(args[cur_arg + 1], '/')) != NULL)
1420 *slash = 0;
1421
Willy Tarreauff9c9142019-02-07 10:39:36 +01001422 if (parse_process_number(args[cur_arg + 1], &proc, MAX_PROCS, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +01001423 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001424 return ERR_ALERT | ERR_FATAL;
1425 }
1426
Christopher Fauletc644fa92017-11-23 22:44:11 +01001427 if (slash) {
Willy Tarreauc9a82e42019-01-26 13:25:14 +01001428 if (parse_process_number(slash+1, &thread, MAX_THREADS, NULL, err)) {
Christopher Fauletc644fa92017-11-23 22:44:11 +01001429 memprintf(err, "'%s' : %s", args[cur_arg], *err);
1430 return ERR_ALERT | ERR_FATAL;
1431 }
1432 *slash = '/';
1433 }
1434
Willy Tarreaue26993c2020-09-03 07:18:55 +02001435 conf->settings.bind_proc |= proc;
1436 conf->settings.bind_thread |= thread;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001437 return 0;
1438}
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001439
Christopher Fauleta717b992018-04-10 14:43:00 +02001440/* parse the "proto" bind keyword */
1441static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1442{
1443 struct ist proto;
1444
1445 if (!*args[cur_arg + 1]) {
1446 memprintf(err, "'%s' : missing value", args[cur_arg]);
1447 return ERR_ALERT | ERR_FATAL;
1448 }
1449
1450 proto = ist2(args[cur_arg + 1], strlen(args[cur_arg + 1]));
1451 conf->mux_proto = get_mux_proto(proto);
1452 if (!conf->mux_proto) {
1453 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]);
1454 return ERR_ALERT | ERR_FATAL;
1455 }
Christopher Fauleta717b992018-04-10 14:43:00 +02001456 return 0;
1457}
1458
Willy Tarreau7ac908b2019-02-27 12:02:18 +01001459/* config parser for global "tune.listener.multi-queue", accepts "on" or "off" */
1460static int cfg_parse_tune_listener_mq(char **args, int section_type, struct proxy *curpx,
1461 struct proxy *defpx, const char *file, int line,
1462 char **err)
1463{
1464 if (too_many_args(1, args, err, NULL))
1465 return -1;
1466
1467 if (strcmp(args[1], "on") == 0)
1468 global.tune.options |= GTUNE_LISTENER_MQ;
1469 else if (strcmp(args[1], "off") == 0)
1470 global.tune.options &= ~GTUNE_LISTENER_MQ;
1471 else {
1472 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
1473 return -1;
1474 }
1475 return 0;
1476}
1477
Willy Tarreau61612d42012-04-19 18:42:05 +02001478/* Note: must not be declared <const> as its list will be overwritten.
1479 * Please take care of keeping this list alphabetically sorted.
1480 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001481static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001482 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
1483 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Jerome Magnineb421b22020-03-27 22:08:40 +01001484 { "so_name", smp_fetch_so_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001485 { /* END */ },
1486}};
1487
Willy Tarreau0108d902018-11-25 19:14:37 +01001488INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1489
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001490/* Note: must not be declared <const> as its list will be overwritten.
1491 * Please take care of keeping this list alphabetically sorted.
1492 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001493static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001494 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001495}};
1496
Willy Tarreau0108d902018-11-25 19:14:37 +01001497INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
1498
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001499/* Note: must not be declared <const> as its list will be overwritten.
1500 * Please take care of keeping this list alphabetically sorted, doing so helps
1501 * all code contributors.
1502 * Optional keywords are also declared with a NULL ->parse() function so that
1503 * the config parser can report an appropriate error when a known keyword was
1504 * not enabled.
1505 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001506static struct bind_kw_list bind_kws = { "ALL", { }, {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001507 { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001508 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
1509 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
1510 { "id", bind_parse_id, 1 }, /* set id of listening socket */
1511 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
1512 { "name", bind_parse_name, 1 }, /* set name of listening socket */
1513 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001514 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Christopher Fauleta717b992018-04-10 14:43:00 +02001515 { "proto", bind_parse_proto, 1 }, /* set the proto to use for all incoming connections */
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001516 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001517}};
1518
Willy Tarreau0108d902018-11-25 19:14:37 +01001519INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1520
Willy Tarreau7ac908b2019-02-27 12:02:18 +01001521/* config keyword parsers */
1522static struct cfg_kw_list cfg_kws = {ILH, {
1523 { CFG_GLOBAL, "tune.listener.multi-queue", cfg_parse_tune_listener_mq },
1524 { 0, NULL, NULL }
1525}};
1526
1527INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1528
Willy Tarreau645513a2010-05-24 20:55:15 +02001529/*
1530 * Local variables:
1531 * c-indent-level: 8
1532 * c-basic-offset: 8
1533 * End:
1534 */