blob: a82ce8109df001517dbf4a373c296fd8b72963e3 [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 Tarreau0ccb7442013-01-07 22:54:17 +010035#include <proto/sample.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020036#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010037
Willy Tarreau26982662012-09-12 23:17:10 +020038/* List head of all known bind keywords */
39static struct bind_kw_list bind_keywords = {
40 .list = LIST_HEAD_INIT(bind_keywords.list)
41};
42
Willy Tarreaudabf2e22007-10-28 21:59:24 +010043/* This function adds the specified listener's file descriptor to the polling
44 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Willy Tarreauae302532014-05-07 19:22:24 +020045 * LI_FULL state depending on its number of connections. In deamon mode, we
46 * also support binding only the relevant processes to their respective
47 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010048 */
49void enable_listener(struct listener *listener)
50{
51 if (listener->state == LI_LISTEN) {
Willy Tarreauae302532014-05-07 19:22:24 +020052 if ((global.mode & (MODE_DAEMON | MODE_SYSTEMD)) &&
53 listener->bind_conf->bind_proc &&
54 !(listener->bind_conf->bind_proc & (1UL << (relative_pid - 1)))) {
55 /* we don't want to enable this listener and don't
56 * want any fd event to reach it.
57 */
58 fd_stop_recv(listener->fd);
59 listener->state = LI_PAUSED;
60 }
61 else if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020062 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010063 listener->state = LI_READY;
Willy Tarreauae302532014-05-07 19:22:24 +020064 }
65 else {
Willy Tarreaudabf2e22007-10-28 21:59:24 +010066 listener->state = LI_FULL;
67 }
68 }
69}
70
71/* This function removes the specified listener's file descriptor from the
72 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
73 * enters LI_LISTEN.
74 */
75void disable_listener(struct listener *listener)
76{
77 if (listener->state < LI_READY)
78 return;
79 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +020080 fd_stop_recv(listener->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020081 if (listener->state == LI_LIMITED)
82 LIST_DEL(&listener->wait_queue);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010083 listener->state = LI_LISTEN;
84}
85
Willy Tarreaube58c382011-07-24 18:28:10 +020086/* This function tries to temporarily disable a listener, depending on the OS
87 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
88 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
89 * closes upon SHUT_WR and refuses to rebind. So a common validation path
90 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
91 * is disabled. It normally returns non-zero, unless an error is reported.
92 */
93int pause_listener(struct listener *l)
94{
95 if (l->state <= LI_PAUSED)
96 return 1;
97
Willy Tarreaub3fb60b2012-10-04 08:56:31 +020098 if (l->proto->sock_prot == IPPROTO_TCP) {
99 if (shutdown(l->fd, SHUT_WR) != 0)
100 return 0; /* Solaris dies here */
Willy Tarreaube58c382011-07-24 18:28:10 +0200101
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200102 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
103 return 0; /* OpenBSD dies here */
Willy Tarreaube58c382011-07-24 18:28:10 +0200104
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200105 if (shutdown(l->fd, SHUT_RD) != 0)
106 return 0; /* should always be OK */
107 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200108
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200109 if (l->state == LI_LIMITED)
110 LIST_DEL(&l->wait_queue);
111
Willy Tarreau49b046d2012-08-09 12:11:58 +0200112 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200113 l->state = LI_PAUSED;
114 return 1;
115}
116
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200117/* This function tries to resume a temporarily disabled listener. Paused, full,
118 * limited and disabled listeners are handled, which means that this function
119 * may replace enable_listener(). The resulting state will either be LI_READY
120 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200121 * Listeners bound to a different process are not woken up unless we're in
122 * foreground mode.
Willy Tarreaube58c382011-07-24 18:28:10 +0200123 */
124int resume_listener(struct listener *l)
125{
126 if (l->state < LI_PAUSED)
127 return 0;
128
Willy Tarreauae302532014-05-07 19:22:24 +0200129 if ((global.mode & (MODE_DAEMON | MODE_SYSTEMD)) &&
130 l->bind_conf->bind_proc &&
131 !(l->bind_conf->bind_proc & (1UL << (relative_pid - 1))))
132 return 0;
133
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200134 if (l->proto->sock_prot == IPPROTO_TCP &&
135 l->state == LI_PAUSED &&
Willy Tarreaube58c382011-07-24 18:28:10 +0200136 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
137 return 0;
138
139 if (l->state == LI_READY)
140 return 1;
141
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200142 if (l->state == LI_LIMITED)
143 LIST_DEL(&l->wait_queue);
144
Willy Tarreaube58c382011-07-24 18:28:10 +0200145 if (l->nbconn >= l->maxconn) {
146 l->state = LI_FULL;
147 return 1;
148 }
149
Willy Tarreau49b046d2012-08-09 12:11:58 +0200150 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200151 l->state = LI_READY;
152 return 1;
153}
154
Willy Tarreau62793712011-07-24 19:23:38 +0200155/* Marks a ready listener as full so that the session code tries to re-enable
156 * it upon next close() using resume_listener().
157 */
158void listener_full(struct listener *l)
159{
160 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200161 if (l->state == LI_LIMITED)
162 LIST_DEL(&l->wait_queue);
163
Willy Tarreau49b046d2012-08-09 12:11:58 +0200164 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200165 l->state = LI_FULL;
166 }
167}
168
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200169/* Marks a ready listener as limited so that we only try to re-enable it when
170 * resources are free again. It will be queued into the specified queue.
171 */
172void limit_listener(struct listener *l, struct list *list)
173{
174 if (l->state == LI_READY) {
175 LIST_ADDQ(list, &l->wait_queue);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200176 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200177 l->state = LI_LIMITED;
178 }
179}
180
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100181/* This function adds all of the protocol's listener's file descriptors to the
182 * polling lists when they are in the LI_LISTEN state. It is intended to be
183 * used as a protocol's generic enable_all() primitive, for use after the
184 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
185 * their number of connections. It always returns ERR_NONE.
186 */
187int enable_all_listeners(struct protocol *proto)
188{
189 struct listener *listener;
190
191 list_for_each_entry(listener, &proto->listeners, proto_list)
192 enable_listener(listener);
193 return ERR_NONE;
194}
195
196/* This function removes all of the protocol's listener's file descriptors from
197 * the polling lists when they are in the LI_READY or LI_FULL states. It is
198 * intended to be used as a protocol's generic disable_all() primitive. It puts
199 * the listeners into LI_LISTEN, and always returns ERR_NONE.
200 */
201int disable_all_listeners(struct protocol *proto)
202{
203 struct listener *listener;
204
205 list_for_each_entry(listener, &proto->listeners, proto_list)
206 disable_listener(listener);
207 return ERR_NONE;
208}
209
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200210/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
211void dequeue_all_listeners(struct list *list)
212{
213 struct listener *listener, *l_back;
214
215 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
216 /* This cannot fail because the listeners are by definition in
217 * the LI_LIMITED state. The function also removes the entry
218 * from the queue.
219 */
220 resume_listener(listener);
221 }
222}
223
Willy Tarreaub648d632007-10-28 22:13:50 +0100224/* This function closes the listening socket for the specified listener,
225 * provided that it's already in a listening state. The listener enters the
226 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
227 * to be used as a generic function for standard protocols.
228 */
229int unbind_listener(struct listener *listener)
230{
231 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200232 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100233
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200234 if (listener->state == LI_LIMITED)
235 LIST_DEL(&listener->wait_queue);
236
Willy Tarreaube58c382011-07-24 18:28:10 +0200237 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100238 fd_delete(listener->fd);
Willy Tarreaufb8bf632014-07-07 18:24:48 +0200239 listener->fd = -1;
Willy Tarreaub648d632007-10-28 22:13:50 +0100240 listener->state = LI_ASSIGNED;
241 }
242 return ERR_NONE;
243}
244
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100245/* This function closes all listening sockets bound to the protocol <proto>,
246 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
247 * detach them from the protocol. It always returns ERR_NONE.
248 */
249int unbind_all_listeners(struct protocol *proto)
250{
251 struct listener *listener;
252
253 list_for_each_entry(listener, &proto->listeners, proto_list)
254 unbind_listener(listener);
255 return ERR_NONE;
256}
257
Willy Tarreau1a64d162007-10-28 22:26:05 +0100258/* Delete a listener from its protocol's list of listeners. The listener's
259 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
260 * number of listeners is updated. Note that the listener must have previously
261 * been unbound. This is the generic function to use to remove a listener.
262 */
263void delete_listener(struct listener *listener)
264{
265 if (listener->state != LI_ASSIGNED)
266 return;
267 listener->state = LI_INIT;
268 LIST_DEL(&listener->proto_list);
269 listener->proto->nb_listeners--;
270}
271
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200272/* This function is called on a read event from a listening socket, corresponding
273 * to an accept. It tries to accept as many connections as possible, and for each
274 * calls the listener's accept handler (generally the frontend's accept handler).
275 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200276void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200277{
278 struct listener *l = fdtab[fd].owner;
279 struct proxy *p = l->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100280 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubb660302014-05-07 19:47:02 +0200281 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200282 int cfd;
283 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100284#ifdef USE_ACCEPT4
285 static int accept4_broken;
286#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200287
288 if (unlikely(l->nbconn >= l->maxconn)) {
289 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200290 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200291 }
292
Willy Tarreau93e7c002013-10-07 18:51:07 +0200293 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
294 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200295
296 if (unlikely(!max)) {
297 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200298 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200299 goto wait_expire;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200300 }
301
302 if (max_accept > max)
303 max_accept = max;
304 }
305
306 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200307 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
308
309 if (unlikely(!max)) {
310 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200311 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200312 goto wait_expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200313 }
314
315 if (max_accept > max)
316 max_accept = max;
317 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200318#ifdef USE_OPENSSL
319 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
320 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200321
Willy Tarreaue43d5322013-10-07 20:01:52 +0200322 if (unlikely(!max)) {
323 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200324 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200325 goto wait_expire;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200326 }
327
328 if (max_accept > max)
329 max_accept = max;
330 }
331#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200332 if (p && p->fe_sps_lim) {
333 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
334
335 if (unlikely(!max)) {
336 /* frontend accept rate limit was reached */
337 limit_listener(l, &p->listener_queue);
338 task_schedule(p->task, tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0)));
Willy Tarreauafad0e02012-08-09 14:45:22 +0200339 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200340 }
341
342 if (max_accept > max)
343 max_accept = max;
344 }
345
346 /* Note: if we fail to allocate a connection because of configured
347 * limits, we'll schedule a new attempt worst 1 second later in the
348 * worst case. If we fail due to system limits or temporary resource
349 * shortage, we try again 100ms later in the worst case.
350 */
351 while (max_accept--) {
352 struct sockaddr_storage addr;
353 socklen_t laddr = sizeof(addr);
354
355 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
356 limit_listener(l, &global_listener_queue);
357 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200358 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200359 }
360
361 if (unlikely(p && p->feconn >= p->maxconn)) {
362 limit_listener(l, &p->listener_queue);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200363 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200364 }
365
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200366#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100367 /* only call accept4() if it's known to be safe, otherwise
368 * fallback to the legacy accept() + fcntl().
369 */
370 if (unlikely(accept4_broken ||
371 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
372 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
373 (accept4_broken = 1))))
374#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200375 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
376 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100377
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200378 if (unlikely(cfd == -1)) {
379 switch (errno) {
380 case EAGAIN:
Willy Tarreaubb660302014-05-07 19:47:02 +0200381 if (fdtab[fd].ev & FD_POLL_HUP) {
382 /* the listening socket might have been disabled in a shared
383 * process and we're a collateral victim. We'll just pause for
384 * a while in case it comes back. In the mean time, we need to
385 * clear this sticky flag.
386 */
387 fdtab[fd].ev &= ~FD_POLL_HUP;
388 goto transient_error;
389 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100390 fd_cant_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200391 return; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200392 case EINVAL:
393 /* might be trying to accept on a shut fd (eg: soft stop) */
394 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100395 case EINTR:
396 case ECONNABORTED:
397 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200398 case ENFILE:
399 if (p)
400 send_log(p, LOG_EMERG,
401 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
402 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200403 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200404 case EMFILE:
405 if (p)
406 send_log(p, LOG_EMERG,
407 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
408 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200409 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200410 case ENOBUFS:
411 case ENOMEM:
412 if (p)
413 send_log(p, LOG_EMERG,
414 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
415 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200416 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200417 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100418 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100419 goto stop;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200420 }
421 }
422
423 if (unlikely(cfd >= global.maxsock)) {
424 send_log(p, LOG_EMERG,
425 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
426 p->id);
427 close(cfd);
428 limit_listener(l, &global_listener_queue);
429 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200430 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200431 }
432
433 /* increase the per-process number of cumulated connections */
434 if (!(l->options & LI_O_UNLIMITED)) {
435 update_freq_ctr(&global.conn_per_sec, 1);
436 if (global.conn_per_sec.curr_ctr > global.cps_max)
437 global.cps_max = global.conn_per_sec.curr_ctr;
438 actconn++;
439 }
440
441 jobs++;
442 totalconn++;
443 l->nbconn++;
444
445 if (l->counters) {
446 if (l->nbconn > l->counters->conn_max)
447 l->counters->conn_max = l->nbconn;
448 }
449
450 ret = l->accept(l, cfd, &addr);
451 if (unlikely(ret <= 0)) {
452 /* The connection was closed by session_accept(). Either
453 * we just have to ignore it (ret == 0) or it's a critical
454 * error due to a resource shortage, and we must stop the
455 * listener (ret < 0).
456 */
457 if (!(l->options & LI_O_UNLIMITED))
458 actconn--;
459 jobs--;
460 l->nbconn--;
461 if (ret == 0) /* successful termination */
462 continue;
463
Willy Tarreaubb660302014-05-07 19:47:02 +0200464 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200465 }
466
467 if (l->nbconn >= l->maxconn) {
468 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200469 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200470 }
471
Willy Tarreau93e7c002013-10-07 18:51:07 +0200472 /* increase the per-process number of cumulated connections */
473 if (!(l->options & LI_O_UNLIMITED)) {
474 update_freq_ctr(&global.sess_per_sec, 1);
475 if (global.sess_per_sec.curr_ctr > global.sps_max)
476 global.sps_max = global.sess_per_sec.curr_ctr;
477 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200478#ifdef USE_OPENSSL
479 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
480
481 update_freq_ctr(&global.ssl_per_sec, 1);
482 if (global.ssl_per_sec.curr_ctr > global.ssl_max)
483 global.ssl_max = global.ssl_per_sec.curr_ctr;
484 }
485#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200486
Willy Tarreauaece46a2012-07-06 12:25:58 +0200487 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200488
Willy Tarreauaece46a2012-07-06 12:25:58 +0200489 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100490 stop:
491 fd_done_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200492 return;
Willy Tarreaubb660302014-05-07 19:47:02 +0200493
494 transient_error:
495 /* pause the listener and try again in 100 ms */
496 expire = tick_add(now_ms, 100);
497
498 wait_expire:
499 limit_listener(l, &global_listener_queue);
500 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
501 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200502}
503
Willy Tarreau26982662012-09-12 23:17:10 +0200504/*
505 * Registers the bind keyword list <kwl> as a list of valid keywords for next
506 * parsing sessions.
507 */
508void bind_register_keywords(struct bind_kw_list *kwl)
509{
510 LIST_ADDQ(&bind_keywords.list, &kwl->list);
511}
512
513/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
514 * keyword is found with a NULL ->parse() function, then an attempt is made to
515 * find one with a valid ->parse() function. This way it is possible to declare
516 * platform-dependant, known keywords as NULL, then only declare them as valid
517 * if some options are met. Note that if the requested keyword contains an
518 * opening parenthesis, everything from this point is ignored.
519 */
520struct bind_kw *bind_find_kw(const char *kw)
521{
522 int index;
523 const char *kwend;
524 struct bind_kw_list *kwl;
525 struct bind_kw *ret = NULL;
526
527 kwend = strchr(kw, '(');
528 if (!kwend)
529 kwend = kw + strlen(kw);
530
531 list_for_each_entry(kwl, &bind_keywords.list, list) {
532 for (index = 0; kwl->kw[index].kw != NULL; index++) {
533 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
534 kwl->kw[index].kw[kwend-kw] == 0) {
535 if (kwl->kw[index].parse)
536 return &kwl->kw[index]; /* found it !*/
537 else
538 ret = &kwl->kw[index]; /* may be OK */
539 }
540 }
541 }
542 return ret;
543}
544
Willy Tarreau8638f482012-09-18 18:01:17 +0200545/* Dumps all registered "bind" keywords to the <out> string pointer. The
546 * unsupported keywords are only dumped if their supported form was not
547 * found.
548 */
549void bind_dump_kws(char **out)
550{
551 struct bind_kw_list *kwl;
552 int index;
553
554 *out = NULL;
555 list_for_each_entry(kwl, &bind_keywords.list, list) {
556 for (index = 0; kwl->kw[index].kw != NULL; index++) {
557 if (kwl->kw[index].parse ||
558 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200559 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
560 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200561 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200562 kwl->kw[index].skip ? " <arg>" : "",
563 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200564 }
565 }
566 }
567}
568
Willy Tarreau645513a2010-05-24 20:55:15 +0200569/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100570/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200571/************************************************************************/
572
Willy Tarreaua5e37562011-12-16 17:06:15 +0100573/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200574static int
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100575smp_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200576 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +0200577{
Willy Tarreauf853c462012-04-23 18:53:56 +0200578 smp->type = SMP_T_UINT;
579 smp->data.uint = l4->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200580 return 1;
581}
582
Willy Tarreaua5e37562011-12-16 17:06:15 +0100583/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200584static int
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100585smp_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200586 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +0200587{
Willy Tarreauf853c462012-04-23 18:53:56 +0200588 smp->type = SMP_T_UINT;
589 smp->data.uint = l4->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200590 return 1;
591}
592
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200593/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200594static 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 +0200595{
596 struct listener *l;
597
Willy Tarreau4348fad2012-09-20 16:48:07 +0200598 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200599 l->options |= LI_O_ACC_PROXY;
600
601 return 0;
602}
603
604/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200605static 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 +0200606{
607 struct listener *l;
608 int val;
609
610 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200611 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200612 return ERR_ALERT | ERR_FATAL;
613 }
614
615 val = atol(args[cur_arg + 1]);
616 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200617 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200618 return ERR_ALERT | ERR_FATAL;
619 }
620
Willy Tarreau4348fad2012-09-20 16:48:07 +0200621 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200622 l->backlog = val;
623
624 return 0;
625}
626
627/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200628static 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 +0200629{
630 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200631 struct listener *l, *new;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200632
Willy Tarreau4348fad2012-09-20 16:48:07 +0200633 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200634 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200635 return ERR_ALERT | ERR_FATAL;
636 }
637
638 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200639 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200640 return ERR_ALERT | ERR_FATAL;
641 }
642
Willy Tarreau4348fad2012-09-20 16:48:07 +0200643 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
644 new->luid = atol(args[cur_arg + 1]);
645 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200646
Willy Tarreau4348fad2012-09-20 16:48:07 +0200647 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200648 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200649 return ERR_ALERT | ERR_FATAL;
650 }
651
Willy Tarreau4348fad2012-09-20 16:48:07 +0200652 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200653 if (node) {
654 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200655 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
656 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
657 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200658 return ERR_ALERT | ERR_FATAL;
659 }
660
Willy Tarreau4348fad2012-09-20 16:48:07 +0200661 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200662 return 0;
663}
664
665/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200666static 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 +0200667{
668 struct listener *l;
669 int val;
670
671 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200672 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200673 return ERR_ALERT | ERR_FATAL;
674 }
675
676 val = atol(args[cur_arg + 1]);
677 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200678 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200679 return ERR_ALERT | ERR_FATAL;
680 }
681
Willy Tarreau4348fad2012-09-20 16:48:07 +0200682 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200683 l->maxconn = val;
684
685 return 0;
686}
687
688/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200689static 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 +0200690{
691 struct listener *l;
692
693 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200694 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200695 return ERR_ALERT | ERR_FATAL;
696 }
697
Willy Tarreau4348fad2012-09-20 16:48:07 +0200698 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200699 l->name = strdup(args[cur_arg + 1]);
700
701 return 0;
702}
703
704/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200705static 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 +0200706{
707 struct listener *l;
708 int val;
709
710 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200711 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200712 return ERR_ALERT | ERR_FATAL;
713 }
714
715 val = atol(args[cur_arg + 1]);
716 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200717 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200718 return ERR_ALERT | ERR_FATAL;
719 }
720
Willy Tarreau4348fad2012-09-20 16:48:07 +0200721 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200722 l->nice = val;
723
724 return 0;
725}
726
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200727/* parse the "process" bind keyword */
728static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
729{
730 unsigned long set = 0;
731 unsigned int low, high;
732
733 if (strcmp(args[cur_arg + 1], "all") == 0) {
734 set = 0;
735 }
736 else if (strcmp(args[cur_arg + 1], "odd") == 0) {
737 set |= ~0UL/3UL; /* 0x555....555 */
738 }
739 else if (strcmp(args[cur_arg + 1], "even") == 0) {
740 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
741 }
742 else if (isdigit((int)*args[cur_arg + 1])) {
743 char *dash = strchr(args[cur_arg + 1], '-');
744
745 low = high = str2uic(args[cur_arg + 1]);
746 if (dash)
747 high = str2uic(dash + 1);
748
749 if (high < low) {
750 unsigned int swap = low;
751 low = high;
752 high = swap;
753 }
754
755 if (low < 1 || high > LONGBITS) {
756 memprintf(err, "'%s' : invalid range %d-%d, allowed range is 1..%d", args[cur_arg], low, high, LONGBITS);
757 return ERR_ALERT | ERR_FATAL;
758 }
759 while (low <= high)
760 set |= 1UL << (low++ - 1);
761 }
762 else {
763 memprintf(err, "'%s' expects 'all', 'odd', 'even', or a process range with numbers from 1 to %d.", args[cur_arg], LONGBITS);
764 return ERR_ALERT | ERR_FATAL;
765 }
766
767 conf->bind_proc = set;
768 return 0;
769}
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200770
Willy Tarreau61612d42012-04-19 18:42:05 +0200771/* Note: must not be declared <const> as its list will be overwritten.
772 * Please take care of keeping this list alphabetically sorted.
773 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200774static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100775 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
776 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
777 { /* END */ },
778}};
779
780/* Note: must not be declared <const> as its list will be overwritten.
781 * Please take care of keeping this list alphabetically sorted.
782 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200783static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100784 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +0200785}};
786
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200787/* Note: must not be declared <const> as its list will be overwritten.
788 * Please take care of keeping this list alphabetically sorted, doing so helps
789 * all code contributors.
790 * Optional keywords are also declared with a NULL ->parse() function so that
791 * the config parser can report an appropriate error when a known keyword was
792 * not enabled.
793 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200794static struct bind_kw_list bind_kws = { "ALL", { }, {
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200795 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
796 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
797 { "id", bind_parse_id, 1 }, /* set id of listening socket */
798 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
799 { "name", bind_parse_name, 1 }, /* set name of listening socket */
800 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200801 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100802 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200803}};
804
Willy Tarreau645513a2010-05-24 20:55:15 +0200805__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +0200806static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +0200807{
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100808 sample_register_fetches(&smp_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200809 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200810 bind_register_keywords(&bind_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200811}
812
813/*
814 * Local variables:
815 * c-indent-level: 8
816 * c-basic-offset: 8
817 * End:
818 */