blob: 2c885e998cf5127d12d27bf2268e7554464a3b79 [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 Tarreau1bc4aab2012-10-08 20:11:03 +020021#include <common/accept4.h>
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +010022#include <common/cfgparse.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020023#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010024#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010025#include <common/initcall.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020026#include <common/mini-clist.h>
27#include <common/standard.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020028#include <common/time.h>
29
30#include <types/global.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020031#include <types/protocol.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020032
Willy Tarreau645513a2010-05-24 20:55:15 +020033#include <proto/acl.h>
Christopher Fauleta717b992018-04-10 14:43:00 +020034#include <proto/connection.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010035#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020036#include <proto/freq_ctr.h>
37#include <proto/log.h>
Willy Tarreau7a798e52016-04-14 11:13:20 +020038#include <proto/listener.h>
Willy Tarreau0de59fd2017-09-15 08:10:44 +020039#include <proto/protocol.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020040#include <proto/proto_sockpair.h>
Willy Tarreau0ccb7442013-01-07 22:54:17 +010041#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020042#include <proto/stream.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020043#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010044
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020045 /* listner_queue lock (same for global and per proxy queues) */
Willy Tarreau86abe442018-11-25 20:12:18 +010046__decl_spinlock(lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020047
Willy Tarreau26982662012-09-12 23:17:10 +020048/* List head of all known bind keywords */
49static struct bind_kw_list bind_keywords = {
50 .list = LIST_HEAD_INIT(bind_keywords.list)
51};
52
Olivier Houchardf73629d2017-04-05 22:33:04 +020053struct xfer_sock_list *xfer_sock_list = NULL;
54
Willy Tarreaudabf2e22007-10-28 21:59:24 +010055/* This function adds the specified listener's file descriptor to the polling
56 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Willy Tarreauae302532014-05-07 19:22:24 +020057 * LI_FULL state depending on its number of connections. In deamon mode, we
58 * also support binding only the relevant processes to their respective
59 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010060 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +020061static void enable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +010062{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010063 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010064 if (listener->state == LI_LISTEN) {
William Lallemand095ba4c2017-06-01 17:38:50 +020065 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreauae302532014-05-07 19:22:24 +020066 listener->bind_conf->bind_proc &&
Willy Tarreau387bd4f2017-11-10 19:08:14 +010067 !(listener->bind_conf->bind_proc & pid_bit)) {
Willy Tarreauae302532014-05-07 19:22:24 +020068 /* we don't want to enable this listener and don't
69 * want any fd event to reach it.
70 */
Olivier Houchard1fc05162017-04-06 01:05:05 +020071 if (!(global.tune.options & GTUNE_SOCKET_TRANSFER))
Christopher Faulet510c0d62018-03-16 10:04:47 +010072 do_unbind_listener(listener, 1);
Olivier Houchard1fc05162017-04-06 01:05:05 +020073 else {
Christopher Faulet510c0d62018-03-16 10:04:47 +010074 do_unbind_listener(listener, 0);
Olivier Houchard1fc05162017-04-06 01:05:05 +020075 listener->state = LI_LISTEN;
76 }
Willy Tarreauae302532014-05-07 19:22:24 +020077 }
78 else if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020079 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010080 listener->state = LI_READY;
Willy Tarreauae302532014-05-07 19:22:24 +020081 }
82 else {
Willy Tarreaudabf2e22007-10-28 21:59:24 +010083 listener->state = LI_FULL;
84 }
85 }
William Lallemande22f11f2018-09-11 10:06:27 +020086 /* if this listener is supposed to be only in the master, close it in the workers */
87 if ((global.mode & MODE_MWORKER) &&
88 (listener->options & LI_O_MWORKER) &&
89 master == 0) {
90 do_unbind_listener(listener, 1);
91 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +010092 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010093}
94
95/* This function removes the specified listener's file descriptor from the
96 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
97 * enters LI_LISTEN.
98 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +020099static void disable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100100{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100101 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100102 if (listener->state < LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200103 goto end;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100104 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200105 fd_stop_recv(listener->fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200106 if (listener->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100107 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200108 LIST_DEL(&listener->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100109 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200110 }
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100111 listener->state = LI_LISTEN;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200112 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100113 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100114}
115
Willy Tarreaube58c382011-07-24 18:28:10 +0200116/* This function tries to temporarily disable a listener, depending on the OS
117 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
118 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
119 * closes upon SHUT_WR and refuses to rebind. So a common validation path
120 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
121 * is disabled. It normally returns non-zero, unless an error is reported.
122 */
123int pause_listener(struct listener *l)
124{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200125 int ret = 1;
126
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100127 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200128
Olivier Houchard1fc05162017-04-06 01:05:05 +0200129 if (l->state <= LI_ZOMBIE)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200130 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200131
Willy Tarreau092d8652014-07-07 20:22:12 +0200132 if (l->proto->pause) {
133 /* Returns < 0 in case of failure, 0 if the listener
134 * was totally stopped, or > 0 if correctly paused.
135 */
136 int ret = l->proto->pause(l);
Willy Tarreaube58c382011-07-24 18:28:10 +0200137
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200138 if (ret < 0) {
139 ret = 0;
140 goto end;
141 }
Willy Tarreau092d8652014-07-07 20:22:12 +0200142 else if (ret == 0)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200143 goto end;
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200144 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200145
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200146 if (l->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100147 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200148 LIST_DEL(&l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100149 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200150 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200151
Willy Tarreau49b046d2012-08-09 12:11:58 +0200152 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200153 l->state = LI_PAUSED;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200154 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100155 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200156 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200157}
158
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200159/* This function tries to resume a temporarily disabled listener. Paused, full,
160 * limited and disabled listeners are handled, which means that this function
161 * may replace enable_listener(). The resulting state will either be LI_READY
162 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200163 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200164 * foreground mode, and are ignored. If the listener was only in the assigned
165 * state, it's totally rebound. This can happen if a pause() has completely
166 * stopped it. If the resume fails, 0 is returned and an error might be
167 * displayed.
Willy Tarreaube58c382011-07-24 18:28:10 +0200168 */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200169static int __resume_listener(struct listener *l)
Willy Tarreaube58c382011-07-24 18:28:10 +0200170{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200171 int ret = 1;
172
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100173 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200174
William Lallemand095ba4c2017-06-01 17:38:50 +0200175 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau3569df32017-03-15 12:47:46 +0100176 l->bind_conf->bind_proc &&
Willy Tarreau387bd4f2017-11-10 19:08:14 +0100177 !(l->bind_conf->bind_proc & pid_bit))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200178 goto end;
Willy Tarreau3569df32017-03-15 12:47:46 +0100179
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200180 if (l->state == LI_ASSIGNED) {
181 char msg[100];
182 int err;
183
184 err = l->proto->bind(l, msg, sizeof(msg));
185 if (err & ERR_ALERT)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100186 ha_alert("Resuming listener: %s\n", msg);
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200187 else if (err & ERR_WARN)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100188 ha_warning("Resuming listener: %s\n", msg);
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200189
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200190 if (err & (ERR_FATAL | ERR_ABORT)) {
191 ret = 0;
192 goto end;
193 }
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200194 }
195
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200196 if (l->state < LI_PAUSED || l->state == LI_ZOMBIE) {
197 ret = 0;
198 goto end;
199 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200200
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200201 if (l->proto->sock_prot == IPPROTO_TCP &&
202 l->state == LI_PAUSED &&
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200203 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) {
204 ret = 0;
205 goto end;
206 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200207
208 if (l->state == LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200209 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200210
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200211 if (l->state == LI_LIMITED)
212 LIST_DEL(&l->wait_queue);
213
Willy Tarreaube58c382011-07-24 18:28:10 +0200214 if (l->nbconn >= l->maxconn) {
215 l->state = LI_FULL;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200216 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200217 }
218
Willy Tarreau49b046d2012-08-09 12:11:58 +0200219 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200220 l->state = LI_READY;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200221 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100222 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200223 return ret;
224}
225
226int resume_listener(struct listener *l)
227{
228 int ret;
229
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100230 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200231 ret = __resume_listener(l);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100232 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200233 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200234}
235
Willy Tarreau87b09662015-04-03 00:22:06 +0200236/* Marks a ready listener as full so that the stream code tries to re-enable
Willy Tarreau62793712011-07-24 19:23:38 +0200237 * it upon next close() using resume_listener().
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200238 *
239 * Note: this function is only called from listener_accept so <l> is already
240 * locked.
Willy Tarreau62793712011-07-24 19:23:38 +0200241 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200242static void listener_full(struct listener *l)
Willy Tarreau62793712011-07-24 19:23:38 +0200243{
244 if (l->state >= LI_READY) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200245 if (l->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100246 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200247 LIST_DEL(&l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100248 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200249 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200250
Willy Tarreau49b046d2012-08-09 12:11:58 +0200251 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200252 l->state = LI_FULL;
253 }
254}
255
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200256/* Marks a ready listener as limited so that we only try to re-enable it when
257 * resources are free again. It will be queued into the specified queue.
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200258 *
259 * Note: this function is only called from listener_accept so <l> is already
260 * locked.
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200261 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200262static void limit_listener(struct listener *l, struct list *list)
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200263{
264 if (l->state == LI_READY) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100265 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200266 LIST_ADDQ(list, &l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100267 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200268 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200269 l->state = LI_LIMITED;
270 }
271}
272
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100273/* This function adds all of the protocol's listener's file descriptors to the
274 * polling lists when they are in the LI_LISTEN state. It is intended to be
275 * used as a protocol's generic enable_all() primitive, for use after the
276 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
277 * their number of connections. It always returns ERR_NONE.
278 */
279int enable_all_listeners(struct protocol *proto)
280{
281 struct listener *listener;
282
283 list_for_each_entry(listener, &proto->listeners, proto_list)
284 enable_listener(listener);
285 return ERR_NONE;
286}
287
288/* This function removes all of the protocol's listener's file descriptors from
289 * the polling lists when they are in the LI_READY or LI_FULL states. It is
290 * intended to be used as a protocol's generic disable_all() primitive. It puts
291 * the listeners into LI_LISTEN, and always returns ERR_NONE.
292 */
293int disable_all_listeners(struct protocol *proto)
294{
295 struct listener *listener;
296
297 list_for_each_entry(listener, &proto->listeners, proto_list)
298 disable_listener(listener);
299 return ERR_NONE;
300}
301
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200302/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
303void dequeue_all_listeners(struct list *list)
304{
305 struct listener *listener, *l_back;
306
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100307 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200308 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
309 /* This cannot fail because the listeners are by definition in
310 * the LI_LIMITED state. The function also removes the entry
311 * from the queue.
312 */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200313 __resume_listener(listener);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200314 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100315 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200316}
317
Christopher Faulet510c0d62018-03-16 10:04:47 +0100318/* Must be called with the lock held. Depending on <do_close> value, it does
319 * what unbind_listener or unbind_listener_no_close should do.
320 */
321void do_unbind_listener(struct listener *listener, int do_close)
Willy Tarreaub648d632007-10-28 22:13:50 +0100322{
323 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200324 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100325
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200326 if (listener->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100327 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200328 LIST_DEL(&listener->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100329 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200330 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200331
Willy Tarreaube58c382011-07-24 18:28:10 +0200332 if (listener->state >= LI_PAUSED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +0200333 if (do_close) {
334 fd_delete(listener->fd);
335 listener->fd = -1;
336 }
337 else
338 fd_remove(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100339 listener->state = LI_ASSIGNED;
340 }
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100341}
342
Olivier Houchard1fc05162017-04-06 01:05:05 +0200343/* This function closes the listening socket for the specified listener,
344 * provided that it's already in a listening state. The listener enters the
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100345 * LI_ASSIGNED state. This function is intended to be used as a generic
346 * function for standard protocols.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200347 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100348void unbind_listener(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200349{
Christopher Faulet510c0d62018-03-16 10:04:47 +0100350 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100351 do_unbind_listener(listener, 1);
Christopher Faulet510c0d62018-03-16 10:04:47 +0100352 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200353}
354
355/* This function pretends the listener is dead, but keeps the FD opened, so
356 * that we can provide it, for conf reloading.
357 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100358void unbind_listener_no_close(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200359{
Christopher Faulet510c0d62018-03-16 10:04:47 +0100360 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100361 do_unbind_listener(listener, 0);
Christopher Faulet510c0d62018-03-16 10:04:47 +0100362 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200363}
364
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100365/* This function closes all listening sockets bound to the protocol <proto>,
366 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
367 * detach them from the protocol. It always returns ERR_NONE.
368 */
369int unbind_all_listeners(struct protocol *proto)
370{
371 struct listener *listener;
372
373 list_for_each_entry(listener, &proto->listeners, proto_list)
374 unbind_listener(listener);
375 return ERR_NONE;
376}
377
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200378/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
379 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
380 * allocation). The address family is taken from ss->ss_family. The number of
381 * jobs and listeners is automatically increased by the number of listeners
William Lallemand75ea0a02017-11-15 19:02:58 +0100382 * created. If the <inherited> argument is set to 1, it specifies that the FD
383 * was obtained from a parent process.
384 * It returns non-zero on success, zero on error with the error message
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200385 * set in <err>.
386 */
387int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
William Lallemand75ea0a02017-11-15 19:02:58 +0100388 int portl, int porth, int fd, int inherited, char **err)
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200389{
390 struct protocol *proto = protocol_by_family(ss->ss_family);
391 struct listener *l;
392 int port;
393
394 if (!proto) {
395 memprintf(err, "unsupported protocol family %d", ss->ss_family);
396 return 0;
397 }
398
399 for (port = portl; port <= porth; port++) {
400 l = calloc(1, sizeof(*l));
401 if (!l) {
402 memprintf(err, "out of memory");
403 return 0;
404 }
405 l->obj_type = OBJ_TYPE_LISTENER;
406 LIST_ADDQ(&bc->frontend->conf.listeners, &l->by_fe);
407 LIST_ADDQ(&bc->listeners, &l->by_bind);
408 l->bind_conf = bc;
409
410 l->fd = fd;
411 memcpy(&l->addr, ss, sizeof(*ss));
412 l->state = LI_INIT;
413
414 proto->add(l, port);
415
William Lallemand75ea0a02017-11-15 19:02:58 +0100416 if (inherited)
417 l->options |= LI_O_INHERITED;
418
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100419 HA_SPIN_INIT(&l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200420 HA_ATOMIC_ADD(&jobs, 1);
421 HA_ATOMIC_ADD(&listeners, 1);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200422 }
423 return 1;
424}
425
Willy Tarreau1a64d162007-10-28 22:26:05 +0100426/* Delete a listener from its protocol's list of listeners. The listener's
427 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
Willy Tarreau2cc5bae2017-09-15 08:18:11 +0200428 * number of listeners is updated, as well as the global number of listeners
429 * and jobs. Note that the listener must have previously been unbound. This
430 * is the generic function to use to remove a listener.
Willy Tarreau1a64d162007-10-28 22:26:05 +0100431 */
432void delete_listener(struct listener *listener)
433{
434 if (listener->state != LI_ASSIGNED)
435 return;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200436
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100437 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100438 listener->state = LI_INIT;
439 LIST_DEL(&listener->proto_list);
440 listener->proto->nb_listeners--;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200441 HA_ATOMIC_SUB(&jobs, 1);
442 HA_ATOMIC_SUB(&listeners, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100443 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100444}
445
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200446/* This function is called on a read event from a listening socket, corresponding
447 * to an accept. It tries to accept as many connections as possible, and for each
448 * calls the listener's accept handler (generally the frontend's accept handler).
449 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200450void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200451{
452 struct listener *l = fdtab[fd].owner;
Willy Tarreauc95bad52016-12-22 00:13:31 +0100453 struct proxy *p = l->bind_conf->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100454 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubb660302014-05-07 19:47:02 +0200455 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200456 int cfd;
457 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100458#ifdef USE_ACCEPT4
459 static int accept4_broken;
460#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200461
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100462 if (HA_SPIN_TRYLOCK(LISTENER_LOCK, &l->lock))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200463 return;
464
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200465 if (unlikely(l->nbconn >= l->maxconn)) {
466 listener_full(l);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200467 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200468 }
469
Willy Tarreau93e7c002013-10-07 18:51:07 +0200470 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
471 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200472
473 if (unlikely(!max)) {
474 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200475 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200476 goto wait_expire;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200477 }
478
479 if (max_accept > max)
480 max_accept = max;
481 }
482
483 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200484 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
485
486 if (unlikely(!max)) {
487 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200488 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200489 goto wait_expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200490 }
491
492 if (max_accept > max)
493 max_accept = max;
494 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200495#ifdef USE_OPENSSL
496 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
497 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200498
Willy Tarreaue43d5322013-10-07 20:01:52 +0200499 if (unlikely(!max)) {
500 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200501 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200502 goto wait_expire;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200503 }
504
505 if (max_accept > max)
506 max_accept = max;
507 }
508#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200509 if (p && p->fe_sps_lim) {
510 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
511
512 if (unlikely(!max)) {
513 /* frontend accept rate limit was reached */
514 limit_listener(l, &p->listener_queue);
515 task_schedule(p->task, tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0)));
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200516 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200517 }
518
519 if (max_accept > max)
520 max_accept = max;
521 }
522
523 /* Note: if we fail to allocate a connection because of configured
524 * limits, we'll schedule a new attempt worst 1 second later in the
525 * worst case. If we fail due to system limits or temporary resource
526 * shortage, we try again 100ms later in the worst case.
527 */
528 while (max_accept--) {
529 struct sockaddr_storage addr;
530 socklen_t laddr = sizeof(addr);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200531 unsigned int count;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200532
533 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
534 limit_listener(l, &global_listener_queue);
535 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200536 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200537 }
538
539 if (unlikely(p && p->feconn >= p->maxconn)) {
540 limit_listener(l, &p->listener_queue);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200541 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200542 }
543
William Lallemand2fe7dd02018-09-11 16:51:29 +0200544 /* with sockpair@ we don't want to do an accept */
545 if (unlikely(l->addr.ss_family == AF_CUST_SOCKPAIR)) {
546 if ((cfd = recv_fd_uxst(fd)) != -1)
William Lallemandd9138002018-11-27 12:02:39 +0100547 fcntl(cfd, F_SETFL, O_NONBLOCK);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200548 } else
549
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200550#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100551 /* only call accept4() if it's known to be safe, otherwise
552 * fallback to the legacy accept() + fcntl().
553 */
554 if (unlikely(accept4_broken ||
William Lallemandd9138002018-11-27 12:02:39 +0100555 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
Willy Tarreau818dca52014-01-31 19:40:19 +0100556 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
557 (accept4_broken = 1))))
558#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200559 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
William Lallemandd9138002018-11-27 12:02:39 +0100560 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100561
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200562 if (unlikely(cfd == -1)) {
563 switch (errno) {
564 case EAGAIN:
Willy Tarreaubb660302014-05-07 19:47:02 +0200565 if (fdtab[fd].ev & FD_POLL_HUP) {
566 /* the listening socket might have been disabled in a shared
567 * process and we're a collateral victim. We'll just pause for
568 * a while in case it comes back. In the mean time, we need to
569 * clear this sticky flag.
570 */
571 fdtab[fd].ev &= ~FD_POLL_HUP;
572 goto transient_error;
573 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100574 fd_cant_recv(fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200575 goto end; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200576 case EINVAL:
577 /* might be trying to accept on a shut fd (eg: soft stop) */
578 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100579 case EINTR:
580 case ECONNABORTED:
581 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200582 case ENFILE:
583 if (p)
584 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100585 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
586 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200587 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200588 case EMFILE:
589 if (p)
590 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100591 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
592 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200593 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200594 case ENOBUFS:
595 case ENOMEM:
596 if (p)
597 send_log(p, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100598 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
599 p->id, global.maxsock);
Willy Tarreaubb660302014-05-07 19:47:02 +0200600 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200601 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100602 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100603 goto stop;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200604 }
605 }
606
William Lallemandd9138002018-11-27 12:02:39 +0100607 /* we don't want to leak the FD upon reload if it's in the master */
608 if (unlikely(master == 1))
609 fcntl(cfd, F_SETFD, FD_CLOEXEC);
610
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200611 if (unlikely(cfd >= global.maxsock)) {
612 send_log(p, LOG_EMERG,
613 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
614 p->id);
615 close(cfd);
616 limit_listener(l, &global_listener_queue);
617 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200618 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200619 }
620
621 /* increase the per-process number of cumulated connections */
622 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200623 count = update_freq_ctr(&global.conn_per_sec, 1);
624 HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
625 HA_ATOMIC_ADD(&actconn, 1);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200626 }
627
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200628 count = HA_ATOMIC_ADD(&l->nbconn, 1);
629 if (l->counters)
630 HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, count);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200631
632 ret = l->accept(l, cfd, &addr);
633 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200634 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200635 * we just have to ignore it (ret == 0) or it's a critical
636 * error due to a resource shortage, and we must stop the
637 * listener (ret < 0).
638 */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200639 if (ret == 0) /* successful termination */
640 continue;
641
Willy Tarreaubb660302014-05-07 19:47:02 +0200642 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200643 }
644
645 if (l->nbconn >= l->maxconn) {
646 listener_full(l);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200647 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200648 }
649
Willy Tarreau93e7c002013-10-07 18:51:07 +0200650 /* increase the per-process number of cumulated connections */
651 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200652 count = update_freq_ctr(&global.sess_per_sec, 1);
653 HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200654 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200655#ifdef USE_OPENSSL
656 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200657 count = update_freq_ctr(&global.ssl_per_sec, 1);
658 HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
Willy Tarreaue43d5322013-10-07 20:01:52 +0200659 }
660#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200661
Willy Tarreauaece46a2012-07-06 12:25:58 +0200662 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200663
Willy Tarreauaece46a2012-07-06 12:25:58 +0200664 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100665 stop:
666 fd_done_recv(fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200667 goto end;
Willy Tarreaubb660302014-05-07 19:47:02 +0200668
669 transient_error:
670 /* pause the listener and try again in 100 ms */
671 expire = tick_add(now_ms, 100);
672
673 wait_expire:
674 limit_listener(l, &global_listener_queue);
675 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200676 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100677 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200678}
679
Willy Tarreau05f50472017-09-15 09:19:58 +0200680/* Notify the listener that a connection initiated from it was released. This
681 * is used to keep the connection count consistent and to possibly re-open
682 * listening when it was limited.
683 */
684void listener_release(struct listener *l)
685{
686 struct proxy *fe = l->bind_conf->frontend;
687
688 if (!(l->options & LI_O_UNLIMITED))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200689 HA_ATOMIC_SUB(&actconn, 1);
690 HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreau05f50472017-09-15 09:19:58 +0200691 if (l->state == LI_FULL)
692 resume_listener(l);
693
694 /* Dequeues all of the listeners waiting for a resource */
695 if (!LIST_ISEMPTY(&global_listener_queue))
696 dequeue_all_listeners(&global_listener_queue);
697
698 if (!LIST_ISEMPTY(&fe->listener_queue) &&
699 (!fe->fe_sps_lim || freq_ctr_remain(&fe->fe_sess_per_sec, fe->fe_sps_lim, 0) > 0))
700 dequeue_all_listeners(&fe->listener_queue);
701}
702
Willy Tarreau26982662012-09-12 23:17:10 +0200703/*
704 * Registers the bind keyword list <kwl> as a list of valid keywords for next
705 * parsing sessions.
706 */
707void bind_register_keywords(struct bind_kw_list *kwl)
708{
709 LIST_ADDQ(&bind_keywords.list, &kwl->list);
710}
711
712/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
713 * keyword is found with a NULL ->parse() function, then an attempt is made to
714 * find one with a valid ->parse() function. This way it is possible to declare
715 * platform-dependant, known keywords as NULL, then only declare them as valid
716 * if some options are met. Note that if the requested keyword contains an
717 * opening parenthesis, everything from this point is ignored.
718 */
719struct bind_kw *bind_find_kw(const char *kw)
720{
721 int index;
722 const char *kwend;
723 struct bind_kw_list *kwl;
724 struct bind_kw *ret = NULL;
725
726 kwend = strchr(kw, '(');
727 if (!kwend)
728 kwend = kw + strlen(kw);
729
730 list_for_each_entry(kwl, &bind_keywords.list, list) {
731 for (index = 0; kwl->kw[index].kw != NULL; index++) {
732 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
733 kwl->kw[index].kw[kwend-kw] == 0) {
734 if (kwl->kw[index].parse)
735 return &kwl->kw[index]; /* found it !*/
736 else
737 ret = &kwl->kw[index]; /* may be OK */
738 }
739 }
740 }
741 return ret;
742}
743
Willy Tarreau8638f482012-09-18 18:01:17 +0200744/* Dumps all registered "bind" keywords to the <out> string pointer. The
745 * unsupported keywords are only dumped if their supported form was not
746 * found.
747 */
748void bind_dump_kws(char **out)
749{
750 struct bind_kw_list *kwl;
751 int index;
752
753 *out = NULL;
754 list_for_each_entry(kwl, &bind_keywords.list, list) {
755 for (index = 0; kwl->kw[index].kw != NULL; index++) {
756 if (kwl->kw[index].parse ||
757 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200758 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
759 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200760 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200761 kwl->kw[index].skip ? " <arg>" : "",
762 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200763 }
764 }
765 }
766}
767
Willy Tarreau645513a2010-05-24 20:55:15 +0200768/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100769/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200770/************************************************************************/
771
Willy Tarreaua5e37562011-12-16 17:06:15 +0100772/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200773static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200774smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +0200775{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200776 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200777 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200778 return 1;
779}
780
Willy Tarreaua5e37562011-12-16 17:06:15 +0100781/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200782static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200783smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +0200784{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200785 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200786 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200787 return 1;
788}
789
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200790/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200791static 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 +0200792{
793 struct listener *l;
794
Willy Tarreau4348fad2012-09-20 16:48:07 +0200795 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200796 l->options |= LI_O_ACC_PROXY;
797
798 return 0;
799}
800
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100801/* parse the "accept-netscaler-cip" bind keyword */
802static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
803{
804 struct listener *l;
805 uint32_t val;
806
807 if (!*args[cur_arg + 1]) {
808 memprintf(err, "'%s' : missing value", args[cur_arg]);
809 return ERR_ALERT | ERR_FATAL;
810 }
811
812 val = atol(args[cur_arg + 1]);
813 if (val <= 0) {
814 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
815 return ERR_ALERT | ERR_FATAL;
816 }
817
818 list_for_each_entry(l, &conf->listeners, by_bind) {
819 l->options |= LI_O_ACC_CIP;
820 conf->ns_cip_magic = val;
821 }
822
823 return 0;
824}
825
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200826/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200827static 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 +0200828{
829 struct listener *l;
830 int val;
831
832 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200833 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200834 return ERR_ALERT | ERR_FATAL;
835 }
836
837 val = atol(args[cur_arg + 1]);
838 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200839 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200840 return ERR_ALERT | ERR_FATAL;
841 }
842
Willy Tarreau4348fad2012-09-20 16:48:07 +0200843 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200844 l->backlog = val;
845
846 return 0;
847}
848
849/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200850static 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 +0200851{
852 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200853 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100854 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200855
Willy Tarreau4348fad2012-09-20 16:48:07 +0200856 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200857 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200858 return ERR_ALERT | ERR_FATAL;
859 }
860
861 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200862 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200863 return ERR_ALERT | ERR_FATAL;
864 }
865
Willy Tarreau4348fad2012-09-20 16:48:07 +0200866 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100867 new->luid = strtol(args[cur_arg + 1], &error, 10);
868 if (*error != '\0') {
869 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
870 return ERR_ALERT | ERR_FATAL;
871 }
Willy Tarreau4348fad2012-09-20 16:48:07 +0200872 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200873
Willy Tarreau4348fad2012-09-20 16:48:07 +0200874 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200875 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200876 return ERR_ALERT | ERR_FATAL;
877 }
878
Willy Tarreau4348fad2012-09-20 16:48:07 +0200879 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200880 if (node) {
881 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200882 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
883 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
884 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200885 return ERR_ALERT | ERR_FATAL;
886 }
887
Willy Tarreau4348fad2012-09-20 16:48:07 +0200888 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200889 return 0;
890}
891
892/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200893static 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 +0200894{
895 struct listener *l;
896 int val;
897
898 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200899 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200900 return ERR_ALERT | ERR_FATAL;
901 }
902
903 val = atol(args[cur_arg + 1]);
904 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200905 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200906 return ERR_ALERT | ERR_FATAL;
907 }
908
Willy Tarreau4348fad2012-09-20 16:48:07 +0200909 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200910 l->maxconn = val;
911
912 return 0;
913}
914
915/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200916static 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 +0200917{
918 struct listener *l;
919
920 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200921 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200922 return ERR_ALERT | ERR_FATAL;
923 }
924
Willy Tarreau4348fad2012-09-20 16:48:07 +0200925 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200926 l->name = strdup(args[cur_arg + 1]);
927
928 return 0;
929}
930
931/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200932static 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 +0200933{
934 struct listener *l;
935 int val;
936
937 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200938 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200939 return ERR_ALERT | ERR_FATAL;
940 }
941
942 val = atol(args[cur_arg + 1]);
943 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200944 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200945 return ERR_ALERT | ERR_FATAL;
946 }
947
Willy Tarreau4348fad2012-09-20 16:48:07 +0200948 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200949 l->nice = val;
950
951 return 0;
952}
953
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200954/* parse the "process" bind keyword */
955static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
956{
Christopher Fauletc644fa92017-11-23 22:44:11 +0100957 char *slash;
958 unsigned long proc = 0, thread = 0;
959 int i;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200960
Christopher Fauletc644fa92017-11-23 22:44:11 +0100961 if ((slash = strchr(args[cur_arg + 1], '/')) != NULL)
962 *slash = 0;
963
964 if (parse_process_number(args[cur_arg + 1], &proc, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +0100965 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200966 return ERR_ALERT | ERR_FATAL;
967 }
968
Christopher Fauletc644fa92017-11-23 22:44:11 +0100969 if (slash) {
970 if (parse_process_number(slash+1, &thread, NULL, err)) {
971 memprintf(err, "'%s' : %s", args[cur_arg], *err);
972 return ERR_ALERT | ERR_FATAL;
973 }
974 *slash = '/';
975 }
976
977 conf->bind_proc |= proc;
978 if (thread) {
Willy Tarreau421f02e2018-01-20 18:19:22 +0100979 for (i = 0; i < MAX_THREADS; i++)
Christopher Fauletc644fa92017-11-23 22:44:11 +0100980 if (!proc || (proc & (1UL << i)))
981 conf->bind_thread[i] |= thread;
982 }
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200983 return 0;
984}
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200985
Christopher Fauleta717b992018-04-10 14:43:00 +0200986/* parse the "proto" bind keyword */
987static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
988{
989 struct ist proto;
990
991 if (!*args[cur_arg + 1]) {
992 memprintf(err, "'%s' : missing value", args[cur_arg]);
993 return ERR_ALERT | ERR_FATAL;
994 }
995
996 proto = ist2(args[cur_arg + 1], strlen(args[cur_arg + 1]));
997 conf->mux_proto = get_mux_proto(proto);
998 if (!conf->mux_proto) {
999 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]);
1000 return ERR_ALERT | ERR_FATAL;
1001 }
Christopher Fauleta717b992018-04-10 14:43:00 +02001002 return 0;
1003}
1004
Willy Tarreau61612d42012-04-19 18:42:05 +02001005/* Note: must not be declared <const> as its list will be overwritten.
1006 * Please take care of keeping this list alphabetically sorted.
1007 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001008static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001009 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
1010 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001011 { /* END */ },
1012}};
1013
Willy Tarreau0108d902018-11-25 19:14:37 +01001014INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1015
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001016/* Note: must not be declared <const> as its list will be overwritten.
1017 * Please take care of keeping this list alphabetically sorted.
1018 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001019static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001020 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001021}};
1022
Willy Tarreau0108d902018-11-25 19:14:37 +01001023INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
1024
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001025/* Note: must not be declared <const> as its list will be overwritten.
1026 * Please take care of keeping this list alphabetically sorted, doing so helps
1027 * all code contributors.
1028 * Optional keywords are also declared with a NULL ->parse() function so that
1029 * the config parser can report an appropriate error when a known keyword was
1030 * not enabled.
1031 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001032static struct bind_kw_list bind_kws = { "ALL", { }, {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001033 { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001034 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
1035 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
1036 { "id", bind_parse_id, 1 }, /* set id of listening socket */
1037 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
1038 { "name", bind_parse_name, 1 }, /* set name of listening socket */
1039 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001040 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Christopher Fauleta717b992018-04-10 14:43:00 +02001041 { "proto", bind_parse_proto, 1 }, /* set the proto to use for all incoming connections */
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001042 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001043}};
1044
Willy Tarreau0108d902018-11-25 19:14:37 +01001045INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1046
Willy Tarreau645513a2010-05-24 20:55:15 +02001047/*
1048 * Local variables:
1049 * c-indent-level: 8
1050 * c-basic-offset: 8
1051 * End:
1052 */