blob: dfd36cca34d1f14ed620a519a1b9b9f64544bba4 [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 Tarreaudd815982007-10-16 12:25:14 +020025#include <common/mini-clist.h>
26#include <common/standard.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020027#include <common/time.h>
28
29#include <types/global.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020030#include <types/protocol.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020031
Willy Tarreau645513a2010-05-24 20:55:15 +020032#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010033#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020034#include <proto/freq_ctr.h>
35#include <proto/log.h>
Willy Tarreau7a798e52016-04-14 11:13:20 +020036#include <proto/listener.h>
Willy Tarreau0de59fd2017-09-15 08:10:44 +020037#include <proto/protocol.h>
Willy Tarreau0ccb7442013-01-07 22:54:17 +010038#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020039#include <proto/stream.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020040#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010041
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020042 /* listner_queue lock (same for global and per proxy queues) */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010043__decl_hathreads(static HA_SPINLOCK_T lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020044
Willy Tarreau26982662012-09-12 23:17:10 +020045/* List head of all known bind keywords */
46static struct bind_kw_list bind_keywords = {
47 .list = LIST_HEAD_INIT(bind_keywords.list)
48};
49
Olivier Houchardf73629d2017-04-05 22:33:04 +020050struct xfer_sock_list *xfer_sock_list = NULL;
51
Willy Tarreaubbd09b92017-11-05 11:38:44 +010052static void __do_unbind_listener(struct listener *listener, int do_close);
53
Willy Tarreaudabf2e22007-10-28 21:59:24 +010054/* This function adds the specified listener's file descriptor to the polling
55 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Willy Tarreauae302532014-05-07 19:22:24 +020056 * LI_FULL state depending on its number of connections. In deamon mode, we
57 * also support binding only the relevant processes to their respective
58 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010059 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +020060static void enable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +010061{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010062 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010063 if (listener->state == LI_LISTEN) {
William Lallemand095ba4c2017-06-01 17:38:50 +020064 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreauae302532014-05-07 19:22:24 +020065 listener->bind_conf->bind_proc &&
Willy Tarreau387bd4f2017-11-10 19:08:14 +010066 !(listener->bind_conf->bind_proc & pid_bit)) {
Willy Tarreauae302532014-05-07 19:22:24 +020067 /* we don't want to enable this listener and don't
68 * want any fd event to reach it.
69 */
Olivier Houchard1fc05162017-04-06 01:05:05 +020070 if (!(global.tune.options & GTUNE_SOCKET_TRANSFER))
Willy Tarreaubbd09b92017-11-05 11:38:44 +010071 __do_unbind_listener(listener, 1);
Olivier Houchard1fc05162017-04-06 01:05:05 +020072 else {
Willy Tarreaubbd09b92017-11-05 11:38:44 +010073 __do_unbind_listener(listener, 0);
Olivier Houchard1fc05162017-04-06 01:05:05 +020074 listener->state = LI_LISTEN;
75 }
Willy Tarreauae302532014-05-07 19:22:24 +020076 }
77 else if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020078 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010079 listener->state = LI_READY;
Willy Tarreauae302532014-05-07 19:22:24 +020080 }
81 else {
Willy Tarreaudabf2e22007-10-28 21:59:24 +010082 listener->state = LI_FULL;
83 }
84 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +010085 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010086}
87
88/* This function removes the specified listener's file descriptor from the
89 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
90 * enters LI_LISTEN.
91 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +020092static void disable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +010093{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010094 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010095 if (listener->state < LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020096 goto end;
Willy Tarreaudabf2e22007-10-28 21:59:24 +010097 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +020098 fd_stop_recv(listener->fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020099 if (listener->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100100 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200101 LIST_DEL(&listener->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100102 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200103 }
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100104 listener->state = LI_LISTEN;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200105 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100106 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100107}
108
Willy Tarreaube58c382011-07-24 18:28:10 +0200109/* This function tries to temporarily disable a listener, depending on the OS
110 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
111 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
112 * closes upon SHUT_WR and refuses to rebind. So a common validation path
113 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
114 * is disabled. It normally returns non-zero, unless an error is reported.
115 */
116int pause_listener(struct listener *l)
117{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200118 int ret = 1;
119
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100120 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200121
Olivier Houchard1fc05162017-04-06 01:05:05 +0200122 if (l->state <= LI_ZOMBIE)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200123 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200124
Willy Tarreau092d8652014-07-07 20:22:12 +0200125 if (l->proto->pause) {
126 /* Returns < 0 in case of failure, 0 if the listener
127 * was totally stopped, or > 0 if correctly paused.
128 */
129 int ret = l->proto->pause(l);
Willy Tarreaube58c382011-07-24 18:28:10 +0200130
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200131 if (ret < 0) {
132 ret = 0;
133 goto end;
134 }
Willy Tarreau092d8652014-07-07 20:22:12 +0200135 else if (ret == 0)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200136 goto end;
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200137 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200138
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200139 if (l->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100140 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200141 LIST_DEL(&l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100142 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200143 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200144
Willy Tarreau49b046d2012-08-09 12:11:58 +0200145 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200146 l->state = LI_PAUSED;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200147 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100148 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200149 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200150}
151
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200152/* This function tries to resume a temporarily disabled listener. Paused, full,
153 * limited and disabled listeners are handled, which means that this function
154 * may replace enable_listener(). The resulting state will either be LI_READY
155 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200156 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200157 * foreground mode, and are ignored. If the listener was only in the assigned
158 * state, it's totally rebound. This can happen if a pause() has completely
159 * stopped it. If the resume fails, 0 is returned and an error might be
160 * displayed.
Willy Tarreaube58c382011-07-24 18:28:10 +0200161 */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200162static int __resume_listener(struct listener *l)
Willy Tarreaube58c382011-07-24 18:28:10 +0200163{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200164 int ret = 1;
165
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100166 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200167
William Lallemand095ba4c2017-06-01 17:38:50 +0200168 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau3569df32017-03-15 12:47:46 +0100169 l->bind_conf->bind_proc &&
Willy Tarreau387bd4f2017-11-10 19:08:14 +0100170 !(l->bind_conf->bind_proc & pid_bit))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200171 goto end;
Willy Tarreau3569df32017-03-15 12:47:46 +0100172
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200173 if (l->state == LI_ASSIGNED) {
174 char msg[100];
175 int err;
176
177 err = l->proto->bind(l, msg, sizeof(msg));
178 if (err & ERR_ALERT)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100179 ha_alert("Resuming listener: %s\n", msg);
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200180 else if (err & ERR_WARN)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100181 ha_warning("Resuming listener: %s\n", msg);
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200182
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200183 if (err & (ERR_FATAL | ERR_ABORT)) {
184 ret = 0;
185 goto end;
186 }
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200187 }
188
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200189 if (l->state < LI_PAUSED || l->state == LI_ZOMBIE) {
190 ret = 0;
191 goto end;
192 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200193
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200194 if (l->proto->sock_prot == IPPROTO_TCP &&
195 l->state == LI_PAUSED &&
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200196 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) {
197 ret = 0;
198 goto end;
199 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200200
201 if (l->state == LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200202 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200203
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200204 if (l->state == LI_LIMITED)
205 LIST_DEL(&l->wait_queue);
206
Willy Tarreaube58c382011-07-24 18:28:10 +0200207 if (l->nbconn >= l->maxconn) {
208 l->state = LI_FULL;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200209 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200210 }
211
Willy Tarreau49b046d2012-08-09 12:11:58 +0200212 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200213 l->state = LI_READY;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200214 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100215 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200216 return ret;
217}
218
219int resume_listener(struct listener *l)
220{
221 int ret;
222
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100223 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200224 ret = __resume_listener(l);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100225 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200226 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200227}
228
Willy Tarreau87b09662015-04-03 00:22:06 +0200229/* Marks a ready listener as full so that the stream code tries to re-enable
Willy Tarreau62793712011-07-24 19:23:38 +0200230 * it upon next close() using resume_listener().
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200231 *
232 * Note: this function is only called from listener_accept so <l> is already
233 * locked.
Willy Tarreau62793712011-07-24 19:23:38 +0200234 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200235static void listener_full(struct listener *l)
Willy Tarreau62793712011-07-24 19:23:38 +0200236{
237 if (l->state >= LI_READY) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200238 if (l->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100239 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200240 LIST_DEL(&l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100241 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200242 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200243
Willy Tarreau49b046d2012-08-09 12:11:58 +0200244 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200245 l->state = LI_FULL;
246 }
247}
248
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200249/* Marks a ready listener as limited so that we only try to re-enable it when
250 * resources are free again. It will be queued into the specified queue.
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200251 *
252 * Note: this function is only called from listener_accept so <l> is already
253 * locked.
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200254 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200255static void limit_listener(struct listener *l, struct list *list)
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200256{
257 if (l->state == LI_READY) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100258 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200259 LIST_ADDQ(list, &l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100260 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200261 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200262 l->state = LI_LIMITED;
263 }
264}
265
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100266/* This function adds all of the protocol's listener's file descriptors to the
267 * polling lists when they are in the LI_LISTEN state. It is intended to be
268 * used as a protocol's generic enable_all() primitive, for use after the
269 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
270 * their number of connections. It always returns ERR_NONE.
271 */
272int enable_all_listeners(struct protocol *proto)
273{
274 struct listener *listener;
275
276 list_for_each_entry(listener, &proto->listeners, proto_list)
277 enable_listener(listener);
278 return ERR_NONE;
279}
280
281/* This function removes all of the protocol's listener's file descriptors from
282 * the polling lists when they are in the LI_READY or LI_FULL states. It is
283 * intended to be used as a protocol's generic disable_all() primitive. It puts
284 * the listeners into LI_LISTEN, and always returns ERR_NONE.
285 */
286int disable_all_listeners(struct protocol *proto)
287{
288 struct listener *listener;
289
290 list_for_each_entry(listener, &proto->listeners, proto_list)
291 disable_listener(listener);
292 return ERR_NONE;
293}
294
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200295/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
296void dequeue_all_listeners(struct list *list)
297{
298 struct listener *listener, *l_back;
299
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100300 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200301 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
302 /* This cannot fail because the listeners are by definition in
303 * the LI_LIMITED state. The function also removes the entry
304 * from the queue.
305 */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200306 __resume_listener(listener);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200307 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100308 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200309}
310
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100311/* must be called with the lock held */
312static void __do_unbind_listener(struct listener *listener, int do_close)
Willy Tarreaub648d632007-10-28 22:13:50 +0100313{
314 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200315 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100316
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200317 if (listener->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100318 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200319 LIST_DEL(&listener->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100320 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200321 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200322
Willy Tarreaube58c382011-07-24 18:28:10 +0200323 if (listener->state >= LI_PAUSED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +0200324 if (do_close) {
325 fd_delete(listener->fd);
326 listener->fd = -1;
327 }
328 else
329 fd_remove(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100330 listener->state = LI_ASSIGNED;
331 }
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100332}
333
334static void do_unbind_listener(struct listener *listener, int do_close)
335{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100336 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100337 __do_unbind_listener(listener, do_close);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100338 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaub648d632007-10-28 22:13:50 +0100339}
340
Olivier Houchard1fc05162017-04-06 01:05:05 +0200341/* This function closes the listening socket for the specified listener,
342 * provided that it's already in a listening state. The listener enters the
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100343 * LI_ASSIGNED state. This function is intended to be used as a generic
344 * function for standard protocols.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200345 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100346void unbind_listener(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200347{
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100348 do_unbind_listener(listener, 1);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200349}
350
351/* This function pretends the listener is dead, but keeps the FD opened, so
352 * that we can provide it, for conf reloading.
353 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100354void unbind_listener_no_close(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200355{
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100356 do_unbind_listener(listener, 0);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200357}
358
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100359/* This function closes all listening sockets bound to the protocol <proto>,
360 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
361 * detach them from the protocol. It always returns ERR_NONE.
362 */
363int unbind_all_listeners(struct protocol *proto)
364{
365 struct listener *listener;
366
367 list_for_each_entry(listener, &proto->listeners, proto_list)
368 unbind_listener(listener);
369 return ERR_NONE;
370}
371
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200372/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
373 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
374 * allocation). The address family is taken from ss->ss_family. The number of
375 * jobs and listeners is automatically increased by the number of listeners
William Lallemand75ea0a02017-11-15 19:02:58 +0100376 * created. If the <inherited> argument is set to 1, it specifies that the FD
377 * was obtained from a parent process.
378 * It returns non-zero on success, zero on error with the error message
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200379 * set in <err>.
380 */
381int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
William Lallemand75ea0a02017-11-15 19:02:58 +0100382 int portl, int porth, int fd, int inherited, char **err)
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200383{
384 struct protocol *proto = protocol_by_family(ss->ss_family);
385 struct listener *l;
386 int port;
387
388 if (!proto) {
389 memprintf(err, "unsupported protocol family %d", ss->ss_family);
390 return 0;
391 }
392
393 for (port = portl; port <= porth; port++) {
394 l = calloc(1, sizeof(*l));
395 if (!l) {
396 memprintf(err, "out of memory");
397 return 0;
398 }
399 l->obj_type = OBJ_TYPE_LISTENER;
400 LIST_ADDQ(&bc->frontend->conf.listeners, &l->by_fe);
401 LIST_ADDQ(&bc->listeners, &l->by_bind);
402 l->bind_conf = bc;
403
404 l->fd = fd;
405 memcpy(&l->addr, ss, sizeof(*ss));
406 l->state = LI_INIT;
407
408 proto->add(l, port);
409
William Lallemand75ea0a02017-11-15 19:02:58 +0100410 if (inherited)
411 l->options |= LI_O_INHERITED;
412
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100413 HA_SPIN_INIT(&l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200414 HA_ATOMIC_ADD(&jobs, 1);
415 HA_ATOMIC_ADD(&listeners, 1);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200416 }
417 return 1;
418}
419
Willy Tarreau1a64d162007-10-28 22:26:05 +0100420/* Delete a listener from its protocol's list of listeners. The listener's
421 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
Willy Tarreau2cc5bae2017-09-15 08:18:11 +0200422 * number of listeners is updated, as well as the global number of listeners
423 * and jobs. Note that the listener must have previously been unbound. This
424 * is the generic function to use to remove a listener.
Willy Tarreau1a64d162007-10-28 22:26:05 +0100425 */
426void delete_listener(struct listener *listener)
427{
428 if (listener->state != LI_ASSIGNED)
429 return;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200430
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100431 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100432 listener->state = LI_INIT;
433 LIST_DEL(&listener->proto_list);
434 listener->proto->nb_listeners--;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200435 HA_ATOMIC_SUB(&jobs, 1);
436 HA_ATOMIC_SUB(&listeners, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100437 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100438}
439
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200440/* This function is called on a read event from a listening socket, corresponding
441 * to an accept. It tries to accept as many connections as possible, and for each
442 * calls the listener's accept handler (generally the frontend's accept handler).
443 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200444void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200445{
446 struct listener *l = fdtab[fd].owner;
Willy Tarreauc95bad52016-12-22 00:13:31 +0100447 struct proxy *p = l->bind_conf->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100448 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubb660302014-05-07 19:47:02 +0200449 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200450 int cfd;
451 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100452#ifdef USE_ACCEPT4
453 static int accept4_broken;
454#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200455
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100456 if (HA_SPIN_TRYLOCK(LISTENER_LOCK, &l->lock))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200457 return;
458
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200459 if (unlikely(l->nbconn >= l->maxconn)) {
460 listener_full(l);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200461 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200462 }
463
Willy Tarreau93e7c002013-10-07 18:51:07 +0200464 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
465 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200466
467 if (unlikely(!max)) {
468 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200469 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200470 goto wait_expire;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200471 }
472
473 if (max_accept > max)
474 max_accept = max;
475 }
476
477 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200478 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
479
480 if (unlikely(!max)) {
481 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200482 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200483 goto wait_expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200484 }
485
486 if (max_accept > max)
487 max_accept = max;
488 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200489#ifdef USE_OPENSSL
490 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
491 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200492
Willy Tarreaue43d5322013-10-07 20:01:52 +0200493 if (unlikely(!max)) {
494 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200495 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200496 goto wait_expire;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200497 }
498
499 if (max_accept > max)
500 max_accept = max;
501 }
502#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200503 if (p && p->fe_sps_lim) {
504 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
505
506 if (unlikely(!max)) {
507 /* frontend accept rate limit was reached */
508 limit_listener(l, &p->listener_queue);
509 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 +0200510 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200511 }
512
513 if (max_accept > max)
514 max_accept = max;
515 }
516
517 /* Note: if we fail to allocate a connection because of configured
518 * limits, we'll schedule a new attempt worst 1 second later in the
519 * worst case. If we fail due to system limits or temporary resource
520 * shortage, we try again 100ms later in the worst case.
521 */
522 while (max_accept--) {
523 struct sockaddr_storage addr;
524 socklen_t laddr = sizeof(addr);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200525 unsigned int count;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200526
527 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
528 limit_listener(l, &global_listener_queue);
529 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200530 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200531 }
532
533 if (unlikely(p && p->feconn >= p->maxconn)) {
534 limit_listener(l, &p->listener_queue);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200535 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200536 }
537
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200538#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100539 /* only call accept4() if it's known to be safe, otherwise
540 * fallback to the legacy accept() + fcntl().
541 */
542 if (unlikely(accept4_broken ||
543 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
544 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
545 (accept4_broken = 1))))
546#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200547 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
548 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100549
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200550 if (unlikely(cfd == -1)) {
551 switch (errno) {
552 case EAGAIN:
Willy Tarreaubb660302014-05-07 19:47:02 +0200553 if (fdtab[fd].ev & FD_POLL_HUP) {
554 /* the listening socket might have been disabled in a shared
555 * process and we're a collateral victim. We'll just pause for
556 * a while in case it comes back. In the mean time, we need to
557 * clear this sticky flag.
558 */
559 fdtab[fd].ev &= ~FD_POLL_HUP;
560 goto transient_error;
561 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100562 fd_cant_recv(fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200563 goto end; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200564 case EINVAL:
565 /* might be trying to accept on a shut fd (eg: soft stop) */
566 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100567 case EINTR:
568 case ECONNABORTED:
569 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200570 case ENFILE:
571 if (p)
572 send_log(p, LOG_EMERG,
573 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
574 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200575 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200576 case EMFILE:
577 if (p)
578 send_log(p, LOG_EMERG,
579 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
580 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200581 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200582 case ENOBUFS:
583 case ENOMEM:
584 if (p)
585 send_log(p, LOG_EMERG,
586 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
587 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200588 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200589 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100590 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100591 goto stop;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200592 }
593 }
594
595 if (unlikely(cfd >= global.maxsock)) {
596 send_log(p, LOG_EMERG,
597 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
598 p->id);
599 close(cfd);
600 limit_listener(l, &global_listener_queue);
601 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200602 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200603 }
604
605 /* increase the per-process number of cumulated connections */
606 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200607 count = update_freq_ctr(&global.conn_per_sec, 1);
608 HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
609 HA_ATOMIC_ADD(&actconn, 1);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200610 }
611
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200612 count = HA_ATOMIC_ADD(&l->nbconn, 1);
613 if (l->counters)
614 HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, count);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200615
616 ret = l->accept(l, cfd, &addr);
617 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200618 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200619 * we just have to ignore it (ret == 0) or it's a critical
620 * error due to a resource shortage, and we must stop the
621 * listener (ret < 0).
622 */
623 if (!(l->options & LI_O_UNLIMITED))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200624 HA_ATOMIC_SUB(&actconn, 1);
625 HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200626 if (ret == 0) /* successful termination */
627 continue;
628
Willy Tarreaubb660302014-05-07 19:47:02 +0200629 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200630 }
631
632 if (l->nbconn >= l->maxconn) {
633 listener_full(l);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200634 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200635 }
636
Willy Tarreau93e7c002013-10-07 18:51:07 +0200637 /* increase the per-process number of cumulated connections */
638 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200639 count = update_freq_ctr(&global.sess_per_sec, 1);
640 HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200641 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200642#ifdef USE_OPENSSL
643 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200644 count = update_freq_ctr(&global.ssl_per_sec, 1);
645 HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
Willy Tarreaue43d5322013-10-07 20:01:52 +0200646 }
647#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200648
Willy Tarreauaece46a2012-07-06 12:25:58 +0200649 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200650
Willy Tarreauaece46a2012-07-06 12:25:58 +0200651 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100652 stop:
653 fd_done_recv(fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200654 goto end;
Willy Tarreaubb660302014-05-07 19:47:02 +0200655
656 transient_error:
657 /* pause the listener and try again in 100 ms */
658 expire = tick_add(now_ms, 100);
659
660 wait_expire:
661 limit_listener(l, &global_listener_queue);
662 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200663 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100664 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200665}
666
Willy Tarreau05f50472017-09-15 09:19:58 +0200667/* Notify the listener that a connection initiated from it was released. This
668 * is used to keep the connection count consistent and to possibly re-open
669 * listening when it was limited.
670 */
671void listener_release(struct listener *l)
672{
673 struct proxy *fe = l->bind_conf->frontend;
674
675 if (!(l->options & LI_O_UNLIMITED))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200676 HA_ATOMIC_SUB(&actconn, 1);
677 HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreau05f50472017-09-15 09:19:58 +0200678 if (l->state == LI_FULL)
679 resume_listener(l);
680
681 /* Dequeues all of the listeners waiting for a resource */
682 if (!LIST_ISEMPTY(&global_listener_queue))
683 dequeue_all_listeners(&global_listener_queue);
684
685 if (!LIST_ISEMPTY(&fe->listener_queue) &&
686 (!fe->fe_sps_lim || freq_ctr_remain(&fe->fe_sess_per_sec, fe->fe_sps_lim, 0) > 0))
687 dequeue_all_listeners(&fe->listener_queue);
688}
689
Willy Tarreau26982662012-09-12 23:17:10 +0200690/*
691 * Registers the bind keyword list <kwl> as a list of valid keywords for next
692 * parsing sessions.
693 */
694void bind_register_keywords(struct bind_kw_list *kwl)
695{
696 LIST_ADDQ(&bind_keywords.list, &kwl->list);
697}
698
699/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
700 * keyword is found with a NULL ->parse() function, then an attempt is made to
701 * find one with a valid ->parse() function. This way it is possible to declare
702 * platform-dependant, known keywords as NULL, then only declare them as valid
703 * if some options are met. Note that if the requested keyword contains an
704 * opening parenthesis, everything from this point is ignored.
705 */
706struct bind_kw *bind_find_kw(const char *kw)
707{
708 int index;
709 const char *kwend;
710 struct bind_kw_list *kwl;
711 struct bind_kw *ret = NULL;
712
713 kwend = strchr(kw, '(');
714 if (!kwend)
715 kwend = kw + strlen(kw);
716
717 list_for_each_entry(kwl, &bind_keywords.list, list) {
718 for (index = 0; kwl->kw[index].kw != NULL; index++) {
719 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
720 kwl->kw[index].kw[kwend-kw] == 0) {
721 if (kwl->kw[index].parse)
722 return &kwl->kw[index]; /* found it !*/
723 else
724 ret = &kwl->kw[index]; /* may be OK */
725 }
726 }
727 }
728 return ret;
729}
730
Willy Tarreau8638f482012-09-18 18:01:17 +0200731/* Dumps all registered "bind" keywords to the <out> string pointer. The
732 * unsupported keywords are only dumped if their supported form was not
733 * found.
734 */
735void bind_dump_kws(char **out)
736{
737 struct bind_kw_list *kwl;
738 int index;
739
740 *out = NULL;
741 list_for_each_entry(kwl, &bind_keywords.list, list) {
742 for (index = 0; kwl->kw[index].kw != NULL; index++) {
743 if (kwl->kw[index].parse ||
744 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200745 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
746 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200747 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200748 kwl->kw[index].skip ? " <arg>" : "",
749 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200750 }
751 }
752 }
753}
754
Willy Tarreau645513a2010-05-24 20:55:15 +0200755/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100756/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200757/************************************************************************/
758
Willy Tarreaua5e37562011-12-16 17:06:15 +0100759/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200760static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200761smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +0200762{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200763 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200764 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200765 return 1;
766}
767
Willy Tarreaua5e37562011-12-16 17:06:15 +0100768/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200769static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200770smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +0200771{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200772 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200773 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200774 return 1;
775}
776
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200777/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200778static 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 +0200779{
780 struct listener *l;
781
Willy Tarreau4348fad2012-09-20 16:48:07 +0200782 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200783 l->options |= LI_O_ACC_PROXY;
784
785 return 0;
786}
787
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100788/* parse the "accept-netscaler-cip" bind keyword */
789static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
790{
791 struct listener *l;
792 uint32_t val;
793
794 if (!*args[cur_arg + 1]) {
795 memprintf(err, "'%s' : missing value", args[cur_arg]);
796 return ERR_ALERT | ERR_FATAL;
797 }
798
799 val = atol(args[cur_arg + 1]);
800 if (val <= 0) {
801 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
802 return ERR_ALERT | ERR_FATAL;
803 }
804
805 list_for_each_entry(l, &conf->listeners, by_bind) {
806 l->options |= LI_O_ACC_CIP;
807 conf->ns_cip_magic = val;
808 }
809
810 return 0;
811}
812
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200813/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200814static 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 +0200815{
816 struct listener *l;
817 int val;
818
819 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200820 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200821 return ERR_ALERT | ERR_FATAL;
822 }
823
824 val = atol(args[cur_arg + 1]);
825 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200826 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200827 return ERR_ALERT | ERR_FATAL;
828 }
829
Willy Tarreau4348fad2012-09-20 16:48:07 +0200830 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200831 l->backlog = val;
832
833 return 0;
834}
835
836/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200837static 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 +0200838{
839 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200840 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100841 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200842
Willy Tarreau4348fad2012-09-20 16:48:07 +0200843 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200844 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200845 return ERR_ALERT | ERR_FATAL;
846 }
847
848 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200849 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200850 return ERR_ALERT | ERR_FATAL;
851 }
852
Willy Tarreau4348fad2012-09-20 16:48:07 +0200853 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100854 new->luid = strtol(args[cur_arg + 1], &error, 10);
855 if (*error != '\0') {
856 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
857 return ERR_ALERT | ERR_FATAL;
858 }
Willy Tarreau4348fad2012-09-20 16:48:07 +0200859 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200860
Willy Tarreau4348fad2012-09-20 16:48:07 +0200861 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200862 memprintf(err, "'%s' : custom id has to be > 0", 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 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200867 if (node) {
868 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200869 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
870 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
871 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200872 return ERR_ALERT | ERR_FATAL;
873 }
874
Willy Tarreau4348fad2012-09-20 16:48:07 +0200875 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200876 return 0;
877}
878
879/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200880static 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 +0200881{
882 struct listener *l;
883 int val;
884
885 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200886 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200887 return ERR_ALERT | ERR_FATAL;
888 }
889
890 val = atol(args[cur_arg + 1]);
891 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200892 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200893 return ERR_ALERT | ERR_FATAL;
894 }
895
Willy Tarreau4348fad2012-09-20 16:48:07 +0200896 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200897 l->maxconn = val;
898
899 return 0;
900}
901
902/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200903static 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 +0200904{
905 struct listener *l;
906
907 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200908 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200909 return ERR_ALERT | ERR_FATAL;
910 }
911
Willy Tarreau4348fad2012-09-20 16:48:07 +0200912 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200913 l->name = strdup(args[cur_arg + 1]);
914
915 return 0;
916}
917
918/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200919static 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 +0200920{
921 struct listener *l;
922 int val;
923
924 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200925 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200926 return ERR_ALERT | ERR_FATAL;
927 }
928
929 val = atol(args[cur_arg + 1]);
930 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200931 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200932 return ERR_ALERT | ERR_FATAL;
933 }
934
Willy Tarreau4348fad2012-09-20 16:48:07 +0200935 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200936 l->nice = val;
937
938 return 0;
939}
940
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200941/* parse the "process" bind keyword */
942static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
943{
Christopher Fauletc644fa92017-11-23 22:44:11 +0100944 char *slash;
945 unsigned long proc = 0, thread = 0;
946 int i;
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200947
Christopher Fauletc644fa92017-11-23 22:44:11 +0100948 if ((slash = strchr(args[cur_arg + 1], '/')) != NULL)
949 *slash = 0;
950
951 if (parse_process_number(args[cur_arg + 1], &proc, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +0100952 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200953 return ERR_ALERT | ERR_FATAL;
954 }
955
Christopher Fauletc644fa92017-11-23 22:44:11 +0100956 if (slash) {
957 if (parse_process_number(slash+1, &thread, NULL, err)) {
958 memprintf(err, "'%s' : %s", args[cur_arg], *err);
959 return ERR_ALERT | ERR_FATAL;
960 }
961 *slash = '/';
962 }
963
964 conf->bind_proc |= proc;
965 if (thread) {
966 for (i = 0; i < LONGBITS; i++)
967 if (!proc || (proc & (1UL << i)))
968 conf->bind_thread[i] |= thread;
969 }
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200970 return 0;
971}
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200972
Willy Tarreau61612d42012-04-19 18:42:05 +0200973/* Note: must not be declared <const> as its list will be overwritten.
974 * Please take care of keeping this list alphabetically sorted.
975 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200976static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200977 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
978 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100979 { /* END */ },
980}};
981
982/* Note: must not be declared <const> as its list will be overwritten.
983 * Please take care of keeping this list alphabetically sorted.
984 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200985static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100986 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +0200987}};
988
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200989/* Note: must not be declared <const> as its list will be overwritten.
990 * Please take care of keeping this list alphabetically sorted, doing so helps
991 * all code contributors.
992 * Optional keywords are also declared with a NULL ->parse() function so that
993 * the config parser can report an appropriate error when a known keyword was
994 * not enabled.
995 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200996static struct bind_kw_list bind_kws = { "ALL", { }, {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100997 { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200998 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
999 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
1000 { "id", bind_parse_id, 1 }, /* set id of listening socket */
1001 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
1002 { "name", bind_parse_name, 1 }, /* set name of listening socket */
1003 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001004 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001005 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001006}};
1007
Willy Tarreau645513a2010-05-24 20:55:15 +02001008__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +02001009static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +02001010{
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001011 sample_register_fetches(&smp_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +02001012 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001013 bind_register_keywords(&bind_kws);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001014 HA_SPIN_INIT(&lq_lock);
Willy Tarreau645513a2010-05-24 20:55:15 +02001015}
1016
1017/*
1018 * Local variables:
1019 * c-indent-level: 8
1020 * c-basic-offset: 8
1021 * End:
1022 */