blob: c88d76d2736f44b3cb4156b76925cb508c68a746 [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#ifdef USE_THREAD
42 /* listner_queue lock (same for global and per proxy queues) */
43static HA_SPINLOCK_T lq_lock;
44#endif
45
Willy Tarreau26982662012-09-12 23:17:10 +020046/* List head of all known bind keywords */
47static struct bind_kw_list bind_keywords = {
48 .list = LIST_HEAD_INIT(bind_keywords.list)
49};
50
Olivier Houchardf73629d2017-04-05 22:33:04 +020051struct xfer_sock_list *xfer_sock_list = NULL;
52
Willy Tarreaubbd09b92017-11-05 11:38:44 +010053static void __do_unbind_listener(struct listener *listener, int do_close);
54
Willy Tarreaudabf2e22007-10-28 21:59:24 +010055/* This function adds the specified listener's file descriptor to the polling
56 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Willy Tarreauae302532014-05-07 19:22:24 +020057 * LI_FULL state depending on its number of connections. In deamon mode, we
58 * also support binding only the relevant processes to their respective
59 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010060 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +020061static void enable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +010062{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020063 SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010064 if (listener->state == LI_LISTEN) {
William Lallemand095ba4c2017-06-01 17:38:50 +020065 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreauae302532014-05-07 19:22:24 +020066 listener->bind_conf->bind_proc &&
67 !(listener->bind_conf->bind_proc & (1UL << (relative_pid - 1)))) {
68 /* we don't want to enable this listener and don't
69 * want any fd event to reach it.
70 */
Olivier Houchard1fc05162017-04-06 01:05:05 +020071 if (!(global.tune.options & GTUNE_SOCKET_TRANSFER))
Willy Tarreaubbd09b92017-11-05 11:38:44 +010072 __do_unbind_listener(listener, 1);
Olivier Houchard1fc05162017-04-06 01:05:05 +020073 else {
Willy Tarreaubbd09b92017-11-05 11:38:44 +010074 __do_unbind_listener(listener, 0);
Olivier Houchard1fc05162017-04-06 01:05:05 +020075 listener->state = LI_LISTEN;
76 }
Willy Tarreauae302532014-05-07 19:22:24 +020077 }
78 else if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020079 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010080 listener->state = LI_READY;
Willy Tarreauae302532014-05-07 19:22:24 +020081 }
82 else {
Willy Tarreaudabf2e22007-10-28 21:59:24 +010083 listener->state = LI_FULL;
84 }
85 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020086 SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010087}
88
89/* This function removes the specified listener's file descriptor from the
90 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
91 * enters LI_LISTEN.
92 */
Christopher Fauletf5b8adc2017-06-02 10:00:35 +020093static void disable_listener(struct listener *listener)
Willy Tarreaudabf2e22007-10-28 21:59:24 +010094{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020095 SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010096 if (listener->state < LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020097 goto end;
Willy Tarreaudabf2e22007-10-28 21:59:24 +010098 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +020099 fd_stop_recv(listener->fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200100 if (listener->state == LI_LIMITED) {
101 SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200102 LIST_DEL(&listener->wait_queue);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200103 SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
104 }
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100105 listener->state = LI_LISTEN;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200106 end:
107 SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100108}
109
Willy Tarreaube58c382011-07-24 18:28:10 +0200110/* This function tries to temporarily disable a listener, depending on the OS
111 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
112 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
113 * closes upon SHUT_WR and refuses to rebind. So a common validation path
114 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
115 * is disabled. It normally returns non-zero, unless an error is reported.
116 */
117int pause_listener(struct listener *l)
118{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200119 int ret = 1;
120
121 SPIN_LOCK(LISTENER_LOCK, &l->lock);
122
Olivier Houchard1fc05162017-04-06 01:05:05 +0200123 if (l->state <= LI_ZOMBIE)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200124 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200125
Willy Tarreau092d8652014-07-07 20:22:12 +0200126 if (l->proto->pause) {
127 /* Returns < 0 in case of failure, 0 if the listener
128 * was totally stopped, or > 0 if correctly paused.
129 */
130 int ret = l->proto->pause(l);
Willy Tarreaube58c382011-07-24 18:28:10 +0200131
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200132 if (ret < 0) {
133 ret = 0;
134 goto end;
135 }
Willy Tarreau092d8652014-07-07 20:22:12 +0200136 else if (ret == 0)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200137 goto end;
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200138 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200139
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200140 if (l->state == LI_LIMITED) {
141 SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200142 LIST_DEL(&l->wait_queue);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200143 SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
144 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200145
Willy Tarreau49b046d2012-08-09 12:11:58 +0200146 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200147 l->state = LI_PAUSED;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200148 end:
149 SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
150 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200151}
152
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200153/* This function tries to resume a temporarily disabled listener. Paused, full,
154 * limited and disabled listeners are handled, which means that this function
155 * may replace enable_listener(). The resulting state will either be LI_READY
156 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200157 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200158 * foreground mode, and are ignored. If the listener was only in the assigned
159 * state, it's totally rebound. This can happen if a pause() has completely
160 * stopped it. If the resume fails, 0 is returned and an error might be
161 * displayed.
Willy Tarreaube58c382011-07-24 18:28:10 +0200162 */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200163static int __resume_listener(struct listener *l)
Willy Tarreaube58c382011-07-24 18:28:10 +0200164{
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200165 int ret = 1;
166
167 SPIN_LOCK(LISTENER_LOCK, &l->lock);
168
William Lallemand095ba4c2017-06-01 17:38:50 +0200169 if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
Willy Tarreau3569df32017-03-15 12:47:46 +0100170 l->bind_conf->bind_proc &&
171 !(l->bind_conf->bind_proc & (1UL << (relative_pid - 1))))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200172 goto end;
Willy Tarreau3569df32017-03-15 12:47:46 +0100173
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200174 if (l->state == LI_ASSIGNED) {
175 char msg[100];
176 int err;
177
178 err = l->proto->bind(l, msg, sizeof(msg));
179 if (err & ERR_ALERT)
180 Alert("Resuming listener: %s\n", msg);
181 else if (err & ERR_WARN)
182 Warning("Resuming listener: %s\n", msg);
183
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200184 if (err & (ERR_FATAL | ERR_ABORT)) {
185 ret = 0;
186 goto end;
187 }
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200188 }
189
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200190 if (l->state < LI_PAUSED || l->state == LI_ZOMBIE) {
191 ret = 0;
192 goto end;
193 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200194
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200195 if (l->proto->sock_prot == IPPROTO_TCP &&
196 l->state == LI_PAUSED &&
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200197 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) {
198 ret = 0;
199 goto end;
200 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200201
202 if (l->state == LI_READY)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200203 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200204
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200205 if (l->state == LI_LIMITED)
206 LIST_DEL(&l->wait_queue);
207
Willy Tarreaube58c382011-07-24 18:28:10 +0200208 if (l->nbconn >= l->maxconn) {
209 l->state = LI_FULL;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200210 goto end;
Willy Tarreaube58c382011-07-24 18:28:10 +0200211 }
212
Willy Tarreau49b046d2012-08-09 12:11:58 +0200213 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200214 l->state = LI_READY;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200215 end:
216 SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
217 return ret;
218}
219
220int resume_listener(struct listener *l)
221{
222 int ret;
223
224 SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
225 ret = __resume_listener(l);
226 SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
227 return ret;
Willy Tarreaube58c382011-07-24 18:28:10 +0200228}
229
Willy Tarreau87b09662015-04-03 00:22:06 +0200230/* Marks a ready listener as full so that the stream code tries to re-enable
Willy Tarreau62793712011-07-24 19:23:38 +0200231 * it upon next close() using resume_listener().
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200232 *
233 * Note: this function is only called from listener_accept so <l> is already
234 * locked.
Willy Tarreau62793712011-07-24 19:23:38 +0200235 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200236static void listener_full(struct listener *l)
Willy Tarreau62793712011-07-24 19:23:38 +0200237{
238 if (l->state >= LI_READY) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200239 if (l->state == LI_LIMITED) {
240 SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200241 LIST_DEL(&l->wait_queue);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200242 SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
243 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200244
Willy Tarreau49b046d2012-08-09 12:11:58 +0200245 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200246 l->state = LI_FULL;
247 }
248}
249
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200250/* Marks a ready listener as limited so that we only try to re-enable it when
251 * resources are free again. It will be queued into the specified queue.
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200252 *
253 * Note: this function is only called from listener_accept so <l> is already
254 * locked.
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200255 */
Christopher Faulet5580ba22017-08-28 15:29:20 +0200256static void limit_listener(struct listener *l, struct list *list)
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200257{
258 if (l->state == LI_READY) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200259 SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200260 LIST_ADDQ(list, &l->wait_queue);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200261 SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200262 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200263 l->state = LI_LIMITED;
264 }
265}
266
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100267/* This function adds all of the protocol's listener's file descriptors to the
268 * polling lists when they are in the LI_LISTEN state. It is intended to be
269 * used as a protocol's generic enable_all() primitive, for use after the
270 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
271 * their number of connections. It always returns ERR_NONE.
272 */
273int enable_all_listeners(struct protocol *proto)
274{
275 struct listener *listener;
276
277 list_for_each_entry(listener, &proto->listeners, proto_list)
278 enable_listener(listener);
279 return ERR_NONE;
280}
281
282/* This function removes all of the protocol's listener's file descriptors from
283 * the polling lists when they are in the LI_READY or LI_FULL states. It is
284 * intended to be used as a protocol's generic disable_all() primitive. It puts
285 * the listeners into LI_LISTEN, and always returns ERR_NONE.
286 */
287int disable_all_listeners(struct protocol *proto)
288{
289 struct listener *listener;
290
291 list_for_each_entry(listener, &proto->listeners, proto_list)
292 disable_listener(listener);
293 return ERR_NONE;
294}
295
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200296/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
297void dequeue_all_listeners(struct list *list)
298{
299 struct listener *listener, *l_back;
300
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200301 SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200302 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
303 /* This cannot fail because the listeners are by definition in
304 * the LI_LIMITED state. The function also removes the entry
305 * from the queue.
306 */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200307 __resume_listener(listener);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200308 }
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200309 SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200310}
311
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100312/* must be called with the lock held */
313static void __do_unbind_listener(struct listener *listener, int do_close)
Willy Tarreaub648d632007-10-28 22:13:50 +0100314{
315 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200316 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100317
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200318 if (listener->state == LI_LIMITED) {
319 SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200320 LIST_DEL(&listener->wait_queue);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200321 SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock);
322 }
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200323
Willy Tarreaube58c382011-07-24 18:28:10 +0200324 if (listener->state >= LI_PAUSED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +0200325 if (do_close) {
326 fd_delete(listener->fd);
327 listener->fd = -1;
328 }
329 else
330 fd_remove(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100331 listener->state = LI_ASSIGNED;
332 }
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100333}
334
335static void do_unbind_listener(struct listener *listener, int do_close)
336{
337 SPIN_LOCK(LISTENER_LOCK, &listener->lock);
338 __do_unbind_listener(listener, do_close);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200339 SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreaub648d632007-10-28 22:13:50 +0100340}
341
Olivier Houchard1fc05162017-04-06 01:05:05 +0200342/* This function closes the listening socket for the specified listener,
343 * provided that it's already in a listening state. The listener enters the
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100344 * LI_ASSIGNED state. This function is intended to be used as a generic
345 * function for standard protocols.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200346 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100347void unbind_listener(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200348{
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100349 do_unbind_listener(listener, 1);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200350}
351
352/* This function pretends the listener is dead, but keeps the FD opened, so
353 * that we can provide it, for conf reloading.
354 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100355void unbind_listener_no_close(struct listener *listener)
Olivier Houchard1fc05162017-04-06 01:05:05 +0200356{
Willy Tarreaubbd09b92017-11-05 11:38:44 +0100357 do_unbind_listener(listener, 0);
Olivier Houchard1fc05162017-04-06 01:05:05 +0200358}
359
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100360/* This function closes all listening sockets bound to the protocol <proto>,
361 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
362 * detach them from the protocol. It always returns ERR_NONE.
363 */
364int unbind_all_listeners(struct protocol *proto)
365{
366 struct listener *listener;
367
368 list_for_each_entry(listener, &proto->listeners, proto_list)
369 unbind_listener(listener);
370 return ERR_NONE;
371}
372
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200373/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
374 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
375 * allocation). The address family is taken from ss->ss_family. The number of
376 * jobs and listeners is automatically increased by the number of listeners
377 * created. It returns non-zero on success, zero on error with the error message
378 * set in <err>.
379 */
380int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
381 int portl, int porth, int fd, char **err)
382{
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
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200409 SPIN_INIT(&l->lock);
410 HA_ATOMIC_ADD(&jobs, 1);
411 HA_ATOMIC_ADD(&listeners, 1);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200412 }
413 return 1;
414}
415
Willy Tarreau1a64d162007-10-28 22:26:05 +0100416/* Delete a listener from its protocol's list of listeners. The listener's
417 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
Willy Tarreau2cc5bae2017-09-15 08:18:11 +0200418 * number of listeners is updated, as well as the global number of listeners
419 * and jobs. Note that the listener must have previously been unbound. This
420 * is the generic function to use to remove a listener.
Willy Tarreau1a64d162007-10-28 22:26:05 +0100421 */
422void delete_listener(struct listener *listener)
423{
424 if (listener->state != LI_ASSIGNED)
425 return;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200426
427 SPIN_LOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100428 listener->state = LI_INIT;
429 LIST_DEL(&listener->proto_list);
430 listener->proto->nb_listeners--;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200431 HA_ATOMIC_SUB(&jobs, 1);
432 HA_ATOMIC_SUB(&listeners, 1);
433 SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
Willy Tarreau1a64d162007-10-28 22:26:05 +0100434}
435
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200436/* This function is called on a read event from a listening socket, corresponding
437 * to an accept. It tries to accept as many connections as possible, and for each
438 * calls the listener's accept handler (generally the frontend's accept handler).
439 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200440void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200441{
442 struct listener *l = fdtab[fd].owner;
Willy Tarreauc95bad52016-12-22 00:13:31 +0100443 struct proxy *p = l->bind_conf->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100444 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubb660302014-05-07 19:47:02 +0200445 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200446 int cfd;
447 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100448#ifdef USE_ACCEPT4
449 static int accept4_broken;
450#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200451
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200452 if (SPIN_TRYLOCK(LISTENER_LOCK, &l->lock))
453 return;
454
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200455 if (unlikely(l->nbconn >= l->maxconn)) {
456 listener_full(l);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200457 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200458 }
459
Willy Tarreau93e7c002013-10-07 18:51:07 +0200460 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
461 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200462
463 if (unlikely(!max)) {
464 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200465 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200466 goto wait_expire;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200467 }
468
469 if (max_accept > max)
470 max_accept = max;
471 }
472
473 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200474 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
475
476 if (unlikely(!max)) {
477 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200478 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200479 goto wait_expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200480 }
481
482 if (max_accept > max)
483 max_accept = max;
484 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200485#ifdef USE_OPENSSL
486 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
487 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200488
Willy Tarreaue43d5322013-10-07 20:01:52 +0200489 if (unlikely(!max)) {
490 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200491 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200492 goto wait_expire;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200493 }
494
495 if (max_accept > max)
496 max_accept = max;
497 }
498#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200499 if (p && p->fe_sps_lim) {
500 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
501
502 if (unlikely(!max)) {
503 /* frontend accept rate limit was reached */
504 limit_listener(l, &p->listener_queue);
505 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 +0200506 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200507 }
508
509 if (max_accept > max)
510 max_accept = max;
511 }
512
513 /* Note: if we fail to allocate a connection because of configured
514 * limits, we'll schedule a new attempt worst 1 second later in the
515 * worst case. If we fail due to system limits or temporary resource
516 * shortage, we try again 100ms later in the worst case.
517 */
518 while (max_accept--) {
519 struct sockaddr_storage addr;
520 socklen_t laddr = sizeof(addr);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200521 unsigned int count;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200522
523 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
524 limit_listener(l, &global_listener_queue);
525 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200526 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200527 }
528
529 if (unlikely(p && p->feconn >= p->maxconn)) {
530 limit_listener(l, &p->listener_queue);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200531 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200532 }
533
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200534#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100535 /* only call accept4() if it's known to be safe, otherwise
536 * fallback to the legacy accept() + fcntl().
537 */
538 if (unlikely(accept4_broken ||
539 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
540 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
541 (accept4_broken = 1))))
542#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200543 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
544 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100545
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200546 if (unlikely(cfd == -1)) {
547 switch (errno) {
548 case EAGAIN:
Willy Tarreaubb660302014-05-07 19:47:02 +0200549 if (fdtab[fd].ev & FD_POLL_HUP) {
550 /* the listening socket might have been disabled in a shared
551 * process and we're a collateral victim. We'll just pause for
552 * a while in case it comes back. In the mean time, we need to
553 * clear this sticky flag.
554 */
555 fdtab[fd].ev &= ~FD_POLL_HUP;
556 goto transient_error;
557 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100558 fd_cant_recv(fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200559 goto end; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200560 case EINVAL:
561 /* might be trying to accept on a shut fd (eg: soft stop) */
562 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100563 case EINTR:
564 case ECONNABORTED:
565 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200566 case ENFILE:
567 if (p)
568 send_log(p, LOG_EMERG,
569 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
570 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200571 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200572 case EMFILE:
573 if (p)
574 send_log(p, LOG_EMERG,
575 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
576 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200577 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200578 case ENOBUFS:
579 case ENOMEM:
580 if (p)
581 send_log(p, LOG_EMERG,
582 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
583 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200584 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200585 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100586 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100587 goto stop;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200588 }
589 }
590
591 if (unlikely(cfd >= global.maxsock)) {
592 send_log(p, LOG_EMERG,
593 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
594 p->id);
595 close(cfd);
596 limit_listener(l, &global_listener_queue);
597 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200598 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200599 }
600
601 /* increase the per-process number of cumulated connections */
602 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200603 count = update_freq_ctr(&global.conn_per_sec, 1);
604 HA_ATOMIC_UPDATE_MAX(&global.cps_max, count);
605 HA_ATOMIC_ADD(&actconn, 1);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200606 }
607
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200608 count = HA_ATOMIC_ADD(&l->nbconn, 1);
609 if (l->counters)
610 HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, count);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200611
612 ret = l->accept(l, cfd, &addr);
613 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200614 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200615 * we just have to ignore it (ret == 0) or it's a critical
616 * error due to a resource shortage, and we must stop the
617 * listener (ret < 0).
618 */
619 if (!(l->options & LI_O_UNLIMITED))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200620 HA_ATOMIC_SUB(&actconn, 1);
621 HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200622 if (ret == 0) /* successful termination */
623 continue;
624
Willy Tarreaubb660302014-05-07 19:47:02 +0200625 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200626 }
627
628 if (l->nbconn >= l->maxconn) {
629 listener_full(l);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200630 goto end;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200631 }
632
Willy Tarreau93e7c002013-10-07 18:51:07 +0200633 /* increase the per-process number of cumulated connections */
634 if (!(l->options & LI_O_UNLIMITED)) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200635 count = update_freq_ctr(&global.sess_per_sec, 1);
636 HA_ATOMIC_UPDATE_MAX(&global.sps_max, count);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200637 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200638#ifdef USE_OPENSSL
639 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200640 count = update_freq_ctr(&global.ssl_per_sec, 1);
641 HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count);
Willy Tarreaue43d5322013-10-07 20:01:52 +0200642 }
643#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200644
Willy Tarreauaece46a2012-07-06 12:25:58 +0200645 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200646
Willy Tarreauaece46a2012-07-06 12:25:58 +0200647 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100648 stop:
649 fd_done_recv(fd);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200650 goto end;
Willy Tarreaubb660302014-05-07 19:47:02 +0200651
652 transient_error:
653 /* pause the listener and try again in 100 ms */
654 expire = tick_add(now_ms, 100);
655
656 wait_expire:
657 limit_listener(l, &global_listener_queue);
658 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200659 end:
660 SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200661}
662
Willy Tarreau05f50472017-09-15 09:19:58 +0200663/* Notify the listener that a connection initiated from it was released. This
664 * is used to keep the connection count consistent and to possibly re-open
665 * listening when it was limited.
666 */
667void listener_release(struct listener *l)
668{
669 struct proxy *fe = l->bind_conf->frontend;
670
671 if (!(l->options & LI_O_UNLIMITED))
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200672 HA_ATOMIC_SUB(&actconn, 1);
673 HA_ATOMIC_SUB(&l->nbconn, 1);
Willy Tarreau05f50472017-09-15 09:19:58 +0200674 if (l->state == LI_FULL)
675 resume_listener(l);
676
677 /* Dequeues all of the listeners waiting for a resource */
678 if (!LIST_ISEMPTY(&global_listener_queue))
679 dequeue_all_listeners(&global_listener_queue);
680
681 if (!LIST_ISEMPTY(&fe->listener_queue) &&
682 (!fe->fe_sps_lim || freq_ctr_remain(&fe->fe_sess_per_sec, fe->fe_sps_lim, 0) > 0))
683 dequeue_all_listeners(&fe->listener_queue);
684}
685
Willy Tarreau26982662012-09-12 23:17:10 +0200686/*
687 * Registers the bind keyword list <kwl> as a list of valid keywords for next
688 * parsing sessions.
689 */
690void bind_register_keywords(struct bind_kw_list *kwl)
691{
692 LIST_ADDQ(&bind_keywords.list, &kwl->list);
693}
694
695/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
696 * keyword is found with a NULL ->parse() function, then an attempt is made to
697 * find one with a valid ->parse() function. This way it is possible to declare
698 * platform-dependant, known keywords as NULL, then only declare them as valid
699 * if some options are met. Note that if the requested keyword contains an
700 * opening parenthesis, everything from this point is ignored.
701 */
702struct bind_kw *bind_find_kw(const char *kw)
703{
704 int index;
705 const char *kwend;
706 struct bind_kw_list *kwl;
707 struct bind_kw *ret = NULL;
708
709 kwend = strchr(kw, '(');
710 if (!kwend)
711 kwend = kw + strlen(kw);
712
713 list_for_each_entry(kwl, &bind_keywords.list, list) {
714 for (index = 0; kwl->kw[index].kw != NULL; index++) {
715 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
716 kwl->kw[index].kw[kwend-kw] == 0) {
717 if (kwl->kw[index].parse)
718 return &kwl->kw[index]; /* found it !*/
719 else
720 ret = &kwl->kw[index]; /* may be OK */
721 }
722 }
723 }
724 return ret;
725}
726
Willy Tarreau8638f482012-09-18 18:01:17 +0200727/* Dumps all registered "bind" keywords to the <out> string pointer. The
728 * unsupported keywords are only dumped if their supported form was not
729 * found.
730 */
731void bind_dump_kws(char **out)
732{
733 struct bind_kw_list *kwl;
734 int index;
735
736 *out = NULL;
737 list_for_each_entry(kwl, &bind_keywords.list, list) {
738 for (index = 0; kwl->kw[index].kw != NULL; index++) {
739 if (kwl->kw[index].parse ||
740 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200741 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
742 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200743 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200744 kwl->kw[index].skip ? " <arg>" : "",
745 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200746 }
747 }
748 }
749}
750
Willy Tarreau645513a2010-05-24 20:55:15 +0200751/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100752/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200753/************************************************************************/
754
Willy Tarreaua5e37562011-12-16 17:06:15 +0100755/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200756static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200757smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +0200758{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200759 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200760 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200761 return 1;
762}
763
Willy Tarreaua5e37562011-12-16 17:06:15 +0100764/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200765static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200766smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +0200767{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200768 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200769 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200770 return 1;
771}
772
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200773/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200774static 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 +0200775{
776 struct listener *l;
777
Willy Tarreau4348fad2012-09-20 16:48:07 +0200778 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200779 l->options |= LI_O_ACC_PROXY;
780
781 return 0;
782}
783
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100784/* parse the "accept-netscaler-cip" bind keyword */
785static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
786{
787 struct listener *l;
788 uint32_t val;
789
790 if (!*args[cur_arg + 1]) {
791 memprintf(err, "'%s' : missing value", args[cur_arg]);
792 return ERR_ALERT | ERR_FATAL;
793 }
794
795 val = atol(args[cur_arg + 1]);
796 if (val <= 0) {
797 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
798 return ERR_ALERT | ERR_FATAL;
799 }
800
801 list_for_each_entry(l, &conf->listeners, by_bind) {
802 l->options |= LI_O_ACC_CIP;
803 conf->ns_cip_magic = val;
804 }
805
806 return 0;
807}
808
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200809/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200810static 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 +0200811{
812 struct listener *l;
813 int val;
814
815 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200816 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200817 return ERR_ALERT | ERR_FATAL;
818 }
819
820 val = atol(args[cur_arg + 1]);
821 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200822 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200823 return ERR_ALERT | ERR_FATAL;
824 }
825
Willy Tarreau4348fad2012-09-20 16:48:07 +0200826 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200827 l->backlog = val;
828
829 return 0;
830}
831
832/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200833static 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 +0200834{
835 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200836 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100837 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200838
Willy Tarreau4348fad2012-09-20 16:48:07 +0200839 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200840 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200841 return ERR_ALERT | ERR_FATAL;
842 }
843
844 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200845 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200846 return ERR_ALERT | ERR_FATAL;
847 }
848
Willy Tarreau4348fad2012-09-20 16:48:07 +0200849 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100850 new->luid = strtol(args[cur_arg + 1], &error, 10);
851 if (*error != '\0') {
852 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
853 return ERR_ALERT | ERR_FATAL;
854 }
Willy Tarreau4348fad2012-09-20 16:48:07 +0200855 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200856
Willy Tarreau4348fad2012-09-20 16:48:07 +0200857 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200858 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200859 return ERR_ALERT | ERR_FATAL;
860 }
861
Willy Tarreau4348fad2012-09-20 16:48:07 +0200862 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200863 if (node) {
864 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200865 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
866 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
867 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200868 return ERR_ALERT | ERR_FATAL;
869 }
870
Willy Tarreau4348fad2012-09-20 16:48:07 +0200871 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200872 return 0;
873}
874
875/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200876static 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 +0200877{
878 struct listener *l;
879 int val;
880
881 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200882 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200883 return ERR_ALERT | ERR_FATAL;
884 }
885
886 val = atol(args[cur_arg + 1]);
887 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200888 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200889 return ERR_ALERT | ERR_FATAL;
890 }
891
Willy Tarreau4348fad2012-09-20 16:48:07 +0200892 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200893 l->maxconn = val;
894
895 return 0;
896}
897
898/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200899static 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 +0200900{
901 struct listener *l;
902
903 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200904 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200905 return ERR_ALERT | ERR_FATAL;
906 }
907
Willy Tarreau4348fad2012-09-20 16:48:07 +0200908 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200909 l->name = strdup(args[cur_arg + 1]);
910
911 return 0;
912}
913
914/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200915static 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 +0200916{
917 struct listener *l;
918 int val;
919
920 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200921 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200922 return ERR_ALERT | ERR_FATAL;
923 }
924
925 val = atol(args[cur_arg + 1]);
926 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200927 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200928 return ERR_ALERT | ERR_FATAL;
929 }
930
Willy Tarreau4348fad2012-09-20 16:48:07 +0200931 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200932 l->nice = val;
933
934 return 0;
935}
936
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200937/* parse the "process" bind keyword */
938static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
939{
940 unsigned long set = 0;
941 unsigned int low, high;
942
943 if (strcmp(args[cur_arg + 1], "all") == 0) {
944 set = 0;
945 }
946 else if (strcmp(args[cur_arg + 1], "odd") == 0) {
947 set |= ~0UL/3UL; /* 0x555....555 */
948 }
949 else if (strcmp(args[cur_arg + 1], "even") == 0) {
950 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
951 }
952 else if (isdigit((int)*args[cur_arg + 1])) {
953 char *dash = strchr(args[cur_arg + 1], '-');
954
955 low = high = str2uic(args[cur_arg + 1]);
956 if (dash)
957 high = str2uic(dash + 1);
958
959 if (high < low) {
960 unsigned int swap = low;
961 low = high;
962 high = swap;
963 }
964
965 if (low < 1 || high > LONGBITS) {
966 memprintf(err, "'%s' : invalid range %d-%d, allowed range is 1..%d", args[cur_arg], low, high, LONGBITS);
967 return ERR_ALERT | ERR_FATAL;
968 }
969 while (low <= high)
970 set |= 1UL << (low++ - 1);
971 }
972 else {
973 memprintf(err, "'%s' expects 'all', 'odd', 'even', or a process range with numbers from 1 to %d.", args[cur_arg], LONGBITS);
974 return ERR_ALERT | ERR_FATAL;
975 }
976
977 conf->bind_proc = set;
978 return 0;
979}
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200980
Willy Tarreau61612d42012-04-19 18:42:05 +0200981/* Note: must not be declared <const> as its list will be overwritten.
982 * Please take care of keeping this list alphabetically sorted.
983 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200984static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200985 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
986 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100987 { /* END */ },
988}};
989
990/* Note: must not be declared <const> as its list will be overwritten.
991 * Please take care of keeping this list alphabetically sorted.
992 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200993static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100994 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +0200995}};
996
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200997/* Note: must not be declared <const> as its list will be overwritten.
998 * Please take care of keeping this list alphabetically sorted, doing so helps
999 * all code contributors.
1000 * Optional keywords are also declared with a NULL ->parse() function so that
1001 * the config parser can report an appropriate error when a known keyword was
1002 * not enabled.
1003 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001004static struct bind_kw_list bind_kws = { "ALL", { }, {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001005 { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001006 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
1007 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
1008 { "id", bind_parse_id, 1 }, /* set id of listening socket */
1009 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
1010 { "name", bind_parse_name, 1 }, /* set name of listening socket */
1011 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +02001012 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001013 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001014}};
1015
Willy Tarreau645513a2010-05-24 20:55:15 +02001016__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +02001017static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +02001018{
Willy Tarreau0ccb7442013-01-07 22:54:17 +01001019 sample_register_fetches(&smp_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +02001020 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +02001021 bind_register_keywords(&bind_kws);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001022 SPIN_INIT(&lq_lock);
Willy Tarreau645513a2010-05-24 20:55:15 +02001023}
1024
1025/*
1026 * Local variables:
1027 * c-indent-level: 8
1028 * c-basic-offset: 8
1029 * End:
1030 */