blob: 7cb4d6aa25c8dc74619a2b8e940a118f80cd6587 [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>
Willy Tarreaudd815982007-10-16 12:25:14 +020022#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010023#include <common/errors.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020024#include <common/mini-clist.h>
25#include <common/standard.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020026#include <common/time.h>
27
28#include <types/global.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020029#include <types/protocol.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020030
Willy Tarreau645513a2010-05-24 20:55:15 +020031#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010032#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020033#include <proto/freq_ctr.h>
34#include <proto/log.h>
Willy Tarreau7a798e52016-04-14 11:13:20 +020035#include <proto/listener.h>
Willy Tarreau0de59fd2017-09-15 08:10:44 +020036#include <proto/protocol.h>
Willy Tarreau0ccb7442013-01-07 22:54:17 +010037#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020038#include <proto/stream.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020039#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010040
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020041 /* listner_queue lock (same for global and per proxy queues) */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010042__decl_hathreads(static HA_SPINLOCK_T lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020043
Willy Tarreau26982662012-09-12 23:17:10 +020044/* List head of all known bind keywords */
45static struct bind_kw_list bind_keywords = {
46 .list = LIST_HEAD_INIT(bind_keywords.list)
47};
48
Olivier Houchardf73629d2017-04-05 22:33:04 +020049struct xfer_sock_list *xfer_sock_list = NULL;
50
Willy Tarreaubbd09b92017-11-05 11:38:44 +010051static void __do_unbind_listener(struct listener *listener, int do_close);
52
Willy Tarreaudabf2e22007-10-28 21:59:24 +010053/* This function adds the specified listener's file descriptor to the polling
54 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Willy Tarreauae302532014-05-07 19:22:24 +020055 * LI_FULL state depending on its number of connections. In deamon mode, we
56 * also support binding only the relevant processes to their respective
57 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010058 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +020059static void enable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +010060{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010061 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010062 if (listener->state == LI_LISTEN) {
William Lallemand095ba4c2017-06-01 17:38:50 +020063 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreauae302532014-05-07 19:22:24 +020064 listener->bind_conf->bind_proc &&
Willy Tarreau387bd4f2017-11-10 19:08:14 +010065 !(listener->bind_conf->bind_proc & pid_bit)) {
Willy Tarreauae302532014-05-07 19:22:24 +020066 /* we don't want to enable this listener and don't
67 * want any fd event to reach it.
68 */
Olivier Houchard1fc05162017-04-06 01:05:05 +020069 if (!(global.tune.options & GTUNE_SOCKET_TRANSFER))
Willy Tarreaubbd09b92017-11-05 11:38:44 +010070 __do_unbind_listener(listener, 1);
Olivier Houchard1fc05162017-04-06 01:05:05 +020071 else {
Willy Tarreaubbd09b92017-11-05 11:38:44 +010072 __do_unbind_listener(listener, 0);
Olivier Houchard1fc05162017-04-06 01:05:05 +020073 listener->state = LI_LISTEN;
74 }
Willy Tarreauae302532014-05-07 19:22:24 +020075 }
76 else if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020077 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010078 listener->state = LI_READY;
Willy Tarreauae302532014-05-07 19:22:24 +020079 }
80 else {
Willy Tarreaudabf2e22007-10-28 21:59:24 +010081 listener->state = LI_FULL;
82 }
83 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +010084 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010085}
86
87/* This function removes the specified listener's file descriptor from the
88 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
89 * enters LI_LISTEN.
90 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +020091static void disable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +010092{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010093 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010094 if (listener->state < LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020095 goto end;
Willy Tarreaudabf2e22007-10-28 21:59:24 +010096 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +020097 fd_stop_recv(listener->fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020098 if (listener->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +010099 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200100 LIST_DEL(&listener->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100101 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200102 }
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100103 listener->state = LI_LISTEN;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200104 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100105 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100106}
107
Willy Tarreaube58c382011-07-24 18:28:10 +0200108/* This function tries to temporarily disable a listener, depending on the OS
109 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
110 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
111 * closes upon SHUT_WR and refuses to rebind. So a common validation path
112 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
113 * is disabled. It normally returns non-zero, unless an error is reported.
114 */
115int pause_listener(struct listener *l)
116{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200117 int ret = 1;
118
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100119 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200120
Olivier Houchard1fc05162017-04-06 01:05:05 +0200121 if (l->state <= LI_ZOMBIE)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200122 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200123
Willy Tarreau092d8652014-07-07 20:22:12 +0200124 if (l->proto->pause) {
125 /* Returns < 0 in case of failure, 0 if the listener
126 * was totally stopped, or > 0 if correctly paused.
127 */
128 int ret = l->proto->pause(l);
Willy Tarreaube58c382011-07-24 18:28:10 +0200129
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200130 if (ret < 0) {
131 ret = 0;
132 goto end;
133 }
Willy Tarreau092d8652014-07-07 20:22:12 +0200134 else if (ret == 0)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200135 goto end;
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200136 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200137
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200138 if (l->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100139 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200140 LIST_DEL(&l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100141 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200142 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200143
Willy Tarreau49b046d2012-08-09 12:11:58 +0200144 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200145 l->state = LI_PAUSED;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200146 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100147 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200148 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200149}
150
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200151/* This function tries to resume a temporarily disabled listener. Paused, full,
152 * limited and disabled listeners are handled, which means that this function
153 * may replace enable_listener(). The resulting state will either be LI_READY
154 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200155 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200156 * foreground mode, and are ignored. If the listener was only in the assigned
157 * state, it's totally rebound. This can happen if a pause() has completely
158 * stopped it. If the resume fails, 0 is returned and an error might be
159 * displayed.
Willy Tarreaube58c382011-07-24 18:28:10 +0200160 */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200161static int __resume_listener(struct listener *l)
Willy Tarreaube58c382011-07-24 18:28:10 +0200162{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200163 int ret = 1;
164
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100165 HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200166
William Lallemand095ba4c2017-06-01 17:38:50 +0200167 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau3569df32017-03-15 12:47:46 +0100168 l->bind_conf->bind_proc &&
Willy Tarreau387bd4f2017-11-10 19:08:14 +0100169 !(l->bind_conf->bind_proc & pid_bit))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200170 goto end;
Willy Tarreau3569df32017-03-15 12:47:46 +0100171
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200172 if (l->state == LI_ASSIGNED) {
173 char msg[100];
174 int err;
175
176 err = l->proto->bind(l, msg, sizeof(msg));
177 if (err & ERR_ALERT)
178 Alert("Resuming listener: %s\n", msg);
179 else if (err & ERR_WARN)
180 Warning("Resuming listener: %s\n", msg);
181
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200182 if (err & (ERR_FATAL | ERR_ABORT)) {
183 ret = 0;
184 goto end;
185 }
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200186 }
187
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200188 if (l->state < LI_PAUSED || l->state == LI_ZOMBIE) {
189 ret = 0;
190 goto end;
191 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200192
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200193 if (l->proto->sock_prot == IPPROTO_TCP &&
194 l->state == LI_PAUSED &&
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200195 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) {
196 ret = 0;
197 goto end;
198 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200199
200 if (l->state == LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200201 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200202
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200203 if (l->state == LI_LIMITED)
204 LIST_DEL(&l->wait_queue);
205
Willy Tarreaube58c382011-07-24 18:28:10 +0200206 if (l->nbconn >= l->maxconn) {
207 l->state = LI_FULL;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200208 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200209 }
210
Willy Tarreau49b046d2012-08-09 12:11:58 +0200211 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200212 l->state = LI_READY;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200213 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100214 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200215 return ret;
216}
217
218int resume_listener(struct listener *l)
219{
220 int ret;
221
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100222 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200223 ret = __resume_listener(l);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100224 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200225 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200226}
227
Willy Tarreau87b09662015-04-03 00:22:06 +0200228/* Marks a ready listener as full so that the stream code tries to re-enable
Willy Tarreau62793712011-07-24 19:23:38 +0200229 * it upon next close() using resume_listener().
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200230 *
231 * Note: this function is only called from listener_accept so <l> is already
232 * locked.
Willy Tarreau62793712011-07-24 19:23:38 +0200233 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200234static void listener_full(struct listener *l)
Willy Tarreau62793712011-07-24 19:23:38 +0200235{
236 if (l->state >= LI_READY) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200237 if (l->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100238 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200239 LIST_DEL(&l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100240 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200241 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200242
Willy Tarreau49b046d2012-08-09 12:11:58 +0200243 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200244 l->state = LI_FULL;
245 }
246}
247
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200248/* Marks a ready listener as limited so that we only try to re-enable it when
249 * resources are free again. It will be queued into the specified queue.
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200250 *
251 * Note: this function is only called from listener_accept so <l> is already
252 * locked.
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200253 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200254static void limit_listener(struct listener *l, struct list *list)
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200255{
256 if (l->state == LI_READY) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100257 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200258 LIST_ADDQ(list, &l->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100259 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200260 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200261 l->state = LI_LIMITED;
262 }
263}
264
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100265/* This function adds all of the protocol's listener's file descriptors to the
266 * polling lists when they are in the LI_LISTEN state. It is intended to be
267 * used as a protocol's generic enable_all() primitive, for use after the
268 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
269 * their number of connections. It always returns ERR_NONE.
270 */
271int enable_all_listeners(struct protocol *proto)
272{
273 struct listener *listener;
274
275 list_for_each_entry(listener, &proto->listeners, proto_list)
276 enable_listener(listener);
277 return ERR_NONE;
278}
279
280/* This function removes all of the protocol's listener's file descriptors from
281 * the polling lists when they are in the LI_READY or LI_FULL states. It is
282 * intended to be used as a protocol's generic disable_all() primitive. It puts
283 * the listeners into LI_LISTEN, and always returns ERR_NONE.
284 */
285int disable_all_listeners(struct protocol *proto)
286{
287 struct listener *listener;
288
289 list_for_each_entry(listener, &proto->listeners, proto_list)
290 disable_listener(listener);
291 return ERR_NONE;
292}
293
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200294/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
295void dequeue_all_listeners(struct list *list)
296{
297 struct listener *listener, *l_back;
298
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100299 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200300 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
301 /* This cannot fail because the listeners are by definition in
302 * the LI_LIMITED state. The function also removes the entry
303 * from the queue.
304 */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200305 __resume_listener(listener);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200306 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100307 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200308}
309
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100310/* must be called with the lock held */
311static void __do_unbind_listener(struct listener *listener, int do_close)
Willy Tarreaub648d632007-10-28 22:13:50 +0100312{
313 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200314 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100315
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200316 if (listener->state == LI_LIMITED) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100317 HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200318 LIST_DEL(&listener->wait_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200320 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200321
Willy Tarreaube58c382011-07-24 18:28:10 +0200322 if (listener->state >= LI_PAUSED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +0200323 if (do_close) {
324 fd_delete(listener->fd);
325 listener->fd = -1;
326 }
327 else
328 fd_remove(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100329 listener->state = LI_ASSIGNED;
330 }
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100331}
332
333static void do_unbind_listener(struct listener *listener, int do_close)
334{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100335 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100336 __do_unbind_listener(listener, do_close);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100337 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaub648d632007-10-28 22:13:50 +0100338}
339
Olivier Houchard1fc05162017-04-06 01:05:05 +0200340/* This function closes the listening socket for the specified listener,
341 * provided that it's already in a listening state. The listener enters the
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100342 * LI_ASSIGNED state. This function is intended to be used as a generic
343 * function for standard protocols.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200344 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100345void unbind_listener(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200346{
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100347 do_unbind_listener(listener, 1);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200348}
349
350/* This function pretends the listener is dead, but keeps the FD opened, so
351 * that we can provide it, for conf reloading.
352 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100353void unbind_listener_no_close(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200354{
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100355 do_unbind_listener(listener, 0);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200356}
357
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100358/* This function closes all listening sockets bound to the protocol <proto>,
359 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
360 * detach them from the protocol. It always returns ERR_NONE.
361 */
362int unbind_all_listeners(struct protocol *proto)
363{
364 struct listener *listener;
365
366 list_for_each_entry(listener, &proto->listeners, proto_list)
367 unbind_listener(listener);
368 return ERR_NONE;
369}
370
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200371/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
372 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
373 * allocation). The address family is taken from ss->ss_family. The number of
374 * jobs and listeners is automatically increased by the number of listeners
William Lallemand75ea0a02017-11-15 19:02:58 +0100375 * created. If the <inherited> argument is set to 1, it specifies that the FD
376 * was obtained from a parent process.
377 * It returns non-zero on success, zero on error with the error message
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200378 * set in <err>.
379 */
380int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
William Lallemand75ea0a02017-11-15 19:02:58 +0100381 int portl, int porth, int fd, int inherited, char **err)
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200382{
383 struct protocol *proto = protocol_by_family(ss->ss_family);
384 struct listener *l;
385 int port;
386
387 if (!proto) {
388 memprintf(err, "unsupported protocol family %d", ss->ss_family);
389 return 0;
390 }
391
392 for (port = portl; port <= porth; port++) {
393 l = calloc(1, sizeof(*l));
394 if (!l) {
395 memprintf(err, "out of memory");
396 return 0;
397 }
398 l->obj_type = OBJ_TYPE_LISTENER;
399 LIST_ADDQ(&bc->frontend->conf.listeners, &l->by_fe);
400 LIST_ADDQ(&bc->listeners, &l->by_bind);
401 l->bind_conf = bc;
402
403 l->fd = fd;
404 memcpy(&l->addr, ss, sizeof(*ss));
405 l->state = LI_INIT;
406
407 proto->add(l, port);
408
William Lallemand75ea0a02017-11-15 19:02:58 +0100409 if (inherited)
410 l->options |= LI_O_INHERITED;
411
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100412 HA_SPIN_INIT(&l->lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200413 HA_ATOMIC_ADD(&jobs, 1);
414 HA_ATOMIC_ADD(&listeners, 1);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200415 }
416 return 1;
417}
418
Willy Tarreau1a64d162007-10-28 22:26:05 +0100419/* Delete a listener from its protocol's list of listeners. The listener's
420 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
Willy Tarreau2cc5bae2017-09-15 08:18:11 +0200421 * number of listeners is updated, as well as the global number of listeners
422 * and jobs. Note that the listener must have previously been unbound. This
423 * is the generic function to use to remove a listener.
Willy Tarreau1a64d162007-10-28 22:26:05 +0100424 */
425void delete_listener(struct listener *listener)
426{
427 if (listener->state != LI_ASSIGNED)
428 return;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200429
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100430 HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100431 listener->state = LI_INIT;
432 LIST_DEL(&listener->proto_list);
433 listener->proto->nb_listeners--;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200434 HA_ATOMIC_SUB(&jobs, 1);
435 HA_ATOMIC_SUB(&listeners, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100436 HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100437}
438
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200439/* This function is called on a read event from a listening socket, corresponding
440 * to an accept. It tries to accept as many connections as possible, and for each
441 * calls the listener's accept handler (generally the frontend's accept handler).
442 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200443void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200444{
445 struct listener *l = fdtab[fd].owner;
Willy Tarreauc95bad52016-12-22 00:13:31 +0100446 struct proxy *p = l->bind_conf->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100447 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubb660302014-05-07 19:47:02 +0200448 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200449 int cfd;
450 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100451#ifdef USE_ACCEPT4
452 static int accept4_broken;
453#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200454
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100455 if (HA_SPIN_TRYLOCK(LISTENER_LOCK, &l->lock))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200456 return;
457
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200458 if (unlikely(l->nbconn >= l->maxconn)) {
459 listener_full(l);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200460 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200461 }
462
Willy Tarreau93e7c002013-10-07 18:51:07 +0200463 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
464 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200465
466 if (unlikely(!max)) {
467 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200468 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200469 goto wait_expire;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200470 }
471
472 if (max_accept > max)
473 max_accept = max;
474 }
475
476 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200477 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
478
479 if (unlikely(!max)) {
480 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200481 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200482 goto wait_expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200483 }
484
485 if (max_accept > max)
486 max_accept = max;
487 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200488#ifdef USE_OPENSSL
489 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
490 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200491
Willy Tarreaue43d5322013-10-07 20:01:52 +0200492 if (unlikely(!max)) {
493 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200494 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200495 goto wait_expire;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200496 }
497
498 if (max_accept > max)
499 max_accept = max;
500 }
501#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200502 if (p && p->fe_sps_lim) {
503 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
504
505 if (unlikely(!max)) {
506 /* frontend accept rate limit was reached */
507 limit_listener(l, &p->listener_queue);
508 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 +0200509 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200510 }
511
512 if (max_accept > max)
513 max_accept = max;
514 }
515
516 /* Note: if we fail to allocate a connection because of configured
517 * limits, we'll schedule a new attempt worst 1 second later in the
518 * worst case. If we fail due to system limits or temporary resource
519 * shortage, we try again 100ms later in the worst case.
520 */
521 while (max_accept--) {
522 struct sockaddr_storage addr;
523 socklen_t laddr = sizeof(addr);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200524 unsigned int count;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200525
526 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
527 limit_listener(l, &global_listener_queue);
528 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200529 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200530 }
531
532 if (unlikely(p && p->feconn >= p->maxconn)) {
533 limit_listener(l, &p->listener_queue);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200534 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200535 }
536
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200537#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100538 /* only call accept4() if it's known to be safe, otherwise
539 * fallback to the legacy accept() + fcntl().
540 */
541 if (unlikely(accept4_broken ||
542 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
543 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
544 (accept4_broken = 1))))
545#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200546 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
547 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100548
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200549 if (unlikely(cfd == -1)) {
550 switch (errno) {
551 case EAGAIN:
Willy Tarreaubb660302014-05-07 19:47:02 +0200552 if (fdtab[fd].ev & FD_POLL_HUP) {
553 /* the listening socket might have been disabled in a shared
554 * process and we're a collateral victim. We'll just pause for
555 * a while in case it comes back. In the mean time, we need to
556 * clear this sticky flag.
557 */
558 fdtab[fd].ev &= ~FD_POLL_HUP;
559 goto transient_error;
560 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100561 fd_cant_recv(fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200562 goto end; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200563 case EINVAL:
564 /* might be trying to accept on a shut fd (eg: soft stop) */
565 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100566 case EINTR:
567 case ECONNABORTED:
568 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200569 case ENFILE:
570 if (p)
571 send_log(p, LOG_EMERG,
572 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
573 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200574 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200575 case EMFILE:
576 if (p)
577 send_log(p, LOG_EMERG,
578 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
579 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200580 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200581 case ENOBUFS:
582 case ENOMEM:
583 if (p)
584 send_log(p, LOG_EMERG,
585 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
586 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200587 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200588 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100589 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100590 goto stop;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200591 }
592 }
593
594 if (unlikely(cfd >= global.maxsock)) {
595 send_log(p, LOG_EMERG,
596 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
597 p->id);
598 close(cfd);
599 limit_listener(l, &global_listener_queue);
600 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200601 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200602 }
603
604 /* increase the per-process number of cumulated connections */
605 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200606 count = update_freq_ctr(&global.conn_per_sec, 1);
607 HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
608 HA_ATOMIC_ADD(&actconn, 1);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200609 }
610
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200611 count = HA_ATOMIC_ADD(&l->nbconn, 1);
612 if (l->counters)
613 HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, count);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200614
615 ret = l->accept(l, cfd, &addr);
616 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200617 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200618 * we just have to ignore it (ret == 0) or it's a critical
619 * error due to a resource shortage, and we must stop the
620 * listener (ret < 0).
621 */
622 if (!(l->options & LI_O_UNLIMITED))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200623 HA_ATOMIC_SUB(&actconn, 1);
624 HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200625 if (ret == 0) /* successful termination */
626 continue;
627
Willy Tarreaubb660302014-05-07 19:47:02 +0200628 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200629 }
630
631 if (l->nbconn >= l->maxconn) {
632 listener_full(l);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200633 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200634 }
635
Willy Tarreau93e7c002013-10-07 18:51:07 +0200636 /* increase the per-process number of cumulated connections */
637 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200638 count = update_freq_ctr(&global.sess_per_sec, 1);
639 HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200640 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200641#ifdef USE_OPENSSL
642 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200643 count = update_freq_ctr(&global.ssl_per_sec, 1);
644 HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
Willy Tarreaue43d5322013-10-07 20:01:52 +0200645 }
646#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200647
Willy Tarreauaece46a2012-07-06 12:25:58 +0200648 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200649
Willy Tarreauaece46a2012-07-06 12:25:58 +0200650 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100651 stop:
652 fd_done_recv(fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200653 goto end;
Willy Tarreaubb660302014-05-07 19:47:02 +0200654
655 transient_error:
656 /* pause the listener and try again in 100 ms */
657 expire = tick_add(now_ms, 100);
658
659 wait_expire:
660 limit_listener(l, &global_listener_queue);
661 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200662 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100663 HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200664}
665
Willy Tarreau05f50472017-09-15 09:19:58 +0200666/* Notify the listener that a connection initiated from it was released. This
667 * is used to keep the connection count consistent and to possibly re-open
668 * listening when it was limited.
669 */
670void listener_release(struct listener *l)
671{
672 struct proxy *fe = l->bind_conf->frontend;
673
674 if (!(l->options & LI_O_UNLIMITED))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200675 HA_ATOMIC_SUB(&actconn, 1);
676 HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreau05f50472017-09-15 09:19:58 +0200677 if (l->state == LI_FULL)
678 resume_listener(l);
679
680 /* Dequeues all of the listeners waiting for a resource */
681 if (!LIST_ISEMPTY(&global_listener_queue))
682 dequeue_all_listeners(&global_listener_queue);
683
684 if (!LIST_ISEMPTY(&fe->listener_queue) &&
685 (!fe->fe_sps_lim || freq_ctr_remain(&fe->fe_sess_per_sec, fe->fe_sps_lim, 0) > 0))
686 dequeue_all_listeners(&fe->listener_queue);
687}
688
Willy Tarreau26982662012-09-12 23:17:10 +0200689/*
690 * Registers the bind keyword list <kwl> as a list of valid keywords for next
691 * parsing sessions.
692 */
693void bind_register_keywords(struct bind_kw_list *kwl)
694{
695 LIST_ADDQ(&bind_keywords.list, &kwl->list);
696}
697
698/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
699 * keyword is found with a NULL ->parse() function, then an attempt is made to
700 * find one with a valid ->parse() function. This way it is possible to declare
701 * platform-dependant, known keywords as NULL, then only declare them as valid
702 * if some options are met. Note that if the requested keyword contains an
703 * opening parenthesis, everything from this point is ignored.
704 */
705struct bind_kw *bind_find_kw(const char *kw)
706{
707 int index;
708 const char *kwend;
709 struct bind_kw_list *kwl;
710 struct bind_kw *ret = NULL;
711
712 kwend = strchr(kw, '(');
713 if (!kwend)
714 kwend = kw + strlen(kw);
715
716 list_for_each_entry(kwl, &bind_keywords.list, list) {
717 for (index = 0; kwl->kw[index].kw != NULL; index++) {
718 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
719 kwl->kw[index].kw[kwend-kw] == 0) {
720 if (kwl->kw[index].parse)
721 return &kwl->kw[index]; /* found it !*/
722 else
723 ret = &kwl->kw[index]; /* may be OK */
724 }
725 }
726 }
727 return ret;
728}
729
Willy Tarreau8638f482012-09-18 18:01:17 +0200730/* Dumps all registered "bind" keywords to the <out> string pointer. The
731 * unsupported keywords are only dumped if their supported form was not
732 * found.
733 */
734void bind_dump_kws(char **out)
735{
736 struct bind_kw_list *kwl;
737 int index;
738
739 *out = NULL;
740 list_for_each_entry(kwl, &bind_keywords.list, list) {
741 for (index = 0; kwl->kw[index].kw != NULL; index++) {
742 if (kwl->kw[index].parse ||
743 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200744 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
745 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200746 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200747 kwl->kw[index].skip ? " <arg>" : "",
748 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200749 }
750 }
751 }
752}
753
Willy Tarreau645513a2010-05-24 20:55:15 +0200754/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100755/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200756/************************************************************************/
757
Willy Tarreaua5e37562011-12-16 17:06:15 +0100758/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200759static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200760smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +0200761{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200762 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200763 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200764 return 1;
765}
766
Willy Tarreaua5e37562011-12-16 17:06:15 +0100767/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200768static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200769smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +0200770{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200771 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200772 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200773 return 1;
774}
775
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200776/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200777static 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 +0200778{
779 struct listener *l;
780
Willy Tarreau4348fad2012-09-20 16:48:07 +0200781 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200782 l->options |= LI_O_ACC_PROXY;
783
784 return 0;
785}
786
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100787/* parse the "accept-netscaler-cip" bind keyword */
788static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
789{
790 struct listener *l;
791 uint32_t val;
792
793 if (!*args[cur_arg + 1]) {
794 memprintf(err, "'%s' : missing value", args[cur_arg]);
795 return ERR_ALERT | ERR_FATAL;
796 }
797
798 val = atol(args[cur_arg + 1]);
799 if (val <= 0) {
800 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
801 return ERR_ALERT | ERR_FATAL;
802 }
803
804 list_for_each_entry(l, &conf->listeners, by_bind) {
805 l->options |= LI_O_ACC_CIP;
806 conf->ns_cip_magic = val;
807 }
808
809 return 0;
810}
811
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200812/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200813static 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 +0200814{
815 struct listener *l;
816 int val;
817
818 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200819 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200820 return ERR_ALERT | ERR_FATAL;
821 }
822
823 val = atol(args[cur_arg + 1]);
824 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200825 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200826 return ERR_ALERT | ERR_FATAL;
827 }
828
Willy Tarreau4348fad2012-09-20 16:48:07 +0200829 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200830 l->backlog = val;
831
832 return 0;
833}
834
835/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200836static 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 +0200837{
838 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200839 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100840 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200841
Willy Tarreau4348fad2012-09-20 16:48:07 +0200842 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200843 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200844 return ERR_ALERT | ERR_FATAL;
845 }
846
847 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200848 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200849 return ERR_ALERT | ERR_FATAL;
850 }
851
Willy Tarreau4348fad2012-09-20 16:48:07 +0200852 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100853 new->luid = strtol(args[cur_arg + 1], &error, 10);
854 if (*error != '\0') {
855 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
856 return ERR_ALERT | ERR_FATAL;
857 }
Willy Tarreau4348fad2012-09-20 16:48:07 +0200858 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200859
Willy Tarreau4348fad2012-09-20 16:48:07 +0200860 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200861 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200862 return ERR_ALERT | ERR_FATAL;
863 }
864
Willy Tarreau4348fad2012-09-20 16:48:07 +0200865 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200866 if (node) {
867 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200868 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
869 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
870 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200871 return ERR_ALERT | ERR_FATAL;
872 }
873
Willy Tarreau4348fad2012-09-20 16:48:07 +0200874 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200875 return 0;
876}
877
878/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200879static 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 +0200880{
881 struct listener *l;
882 int val;
883
884 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200885 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200886 return ERR_ALERT | ERR_FATAL;
887 }
888
889 val = atol(args[cur_arg + 1]);
890 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200891 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200892 return ERR_ALERT | ERR_FATAL;
893 }
894
Willy Tarreau4348fad2012-09-20 16:48:07 +0200895 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200896 l->maxconn = val;
897
898 return 0;
899}
900
901/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200902static 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 +0200903{
904 struct listener *l;
905
906 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200907 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200908 return ERR_ALERT | ERR_FATAL;
909 }
910
Willy Tarreau4348fad2012-09-20 16:48:07 +0200911 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200912 l->name = strdup(args[cur_arg + 1]);
913
914 return 0;
915}
916
917/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200918static 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 +0200919{
920 struct listener *l;
921 int val;
922
923 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200924 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200925 return ERR_ALERT | ERR_FATAL;
926 }
927
928 val = atol(args[cur_arg + 1]);
929 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200930 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200931 return ERR_ALERT | ERR_FATAL;
932 }
933
Willy Tarreau4348fad2012-09-20 16:48:07 +0200934 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200935 l->nice = val;
936
937 return 0;
938}
939
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200940/* parse the "process" bind keyword */
941static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
942{
943 unsigned long set = 0;
944 unsigned int low, high;
945
946 if (strcmp(args[cur_arg + 1], "all") == 0) {
947 set = 0;
948 }
949 else if (strcmp(args[cur_arg + 1], "odd") == 0) {
950 set |= ~0UL/3UL; /* 0x555....555 */
951 }
952 else if (strcmp(args[cur_arg + 1], "even") == 0) {
953 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
954 }
955 else if (isdigit((int)*args[cur_arg + 1])) {
956 char *dash = strchr(args[cur_arg + 1], '-');
957
958 low = high = str2uic(args[cur_arg + 1]);
959 if (dash)
960 high = str2uic(dash + 1);
961
962 if (high < low) {
963 unsigned int swap = low;
964 low = high;
965 high = swap;
966 }
967
968 if (low < 1 || high > LONGBITS) {
969 memprintf(err, "'%s' : invalid range %d-%d, allowed range is 1..%d", args[cur_arg], low, high, LONGBITS);
970 return ERR_ALERT | ERR_FATAL;
971 }
972 while (low <= high)
973 set |= 1UL << (low++ - 1);
974 }
975 else {
976 memprintf(err, "'%s' expects 'all', 'odd', 'even', or a process range with numbers from 1 to %d.", args[cur_arg], LONGBITS);
977 return ERR_ALERT | ERR_FATAL;
978 }
979
980 conf->bind_proc = set;
981 return 0;
982}
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200983
Willy Tarreau61612d42012-04-19 18:42:05 +0200984/* Note: must not be declared <const> as its list will be overwritten.
985 * Please take care of keeping this list alphabetically sorted.
986 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200987static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200988 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
989 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100990 { /* END */ },
991}};
992
993/* Note: must not be declared <const> as its list will be overwritten.
994 * Please take care of keeping this list alphabetically sorted.
995 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200996static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100997 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +0200998}};
999
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001000/* Note: must not be declared <const> as its list will be overwritten.
1001 * Please take care of keeping this list alphabetically sorted, doing so helps
1002 * all code contributors.
1003 * Optional keywords are also declared with a NULL ->parse() function so that
1004 * the config parser can report an appropriate error when a known keyword was
1005 * not enabled.
1006 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001007static struct bind_kw_list bind_kws = { "ALL", { }, {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001008 { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001009 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
1010 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
1011 { "id", bind_parse_id, 1 }, /* set id of listening socket */
1012 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
1013 { "name", bind_parse_name, 1 }, /* set name of listening socket */
1014 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001015 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001016 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001017}};
1018
Willy Tarreau645513a2010-05-24 20:55:15 +02001019__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +02001020static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +02001021{
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001022 sample_register_fetches(&smp_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +02001023 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001024 bind_register_keywords(&bind_kws);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001025 HA_SPIN_INIT(&lq_lock);
Willy Tarreau645513a2010-05-24 20:55:15 +02001026}
1027
1028/*
1029 * Local variables:
1030 * c-indent-level: 8
1031 * c-basic-offset: 8
1032 * End:
1033 */