blob: 67f8ca7d4342064812b64369de4457b5e97546ea [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 Tarreaud903bb32014-07-07 20:22:12 +020098 if (l->proto->pause) {
99 /* Returns < 0 in case of failure, 0 if the listener
100 * was totally stopped, or > 0 if correctly paused.
101 */
102 int ret = l->proto->pause(l);
Willy Tarreaube58c382011-07-24 18:28:10 +0200103
Willy Tarreaud903bb32014-07-07 20:22:12 +0200104 if (ret < 0)
105 return 0;
106 else if (ret == 0)
107 return 1;
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200108 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200109
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200110 if (l->state == LI_LIMITED)
111 LIST_DEL(&l->wait_queue);
112
Willy Tarreau49b046d2012-08-09 12:11:58 +0200113 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200114 l->state = LI_PAUSED;
115 return 1;
116}
117
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200118/* This function tries to resume a temporarily disabled listener. Paused, full,
119 * limited and disabled listeners are handled, which means that this function
120 * may replace enable_listener(). The resulting state will either be LI_READY
121 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200122 * Listeners bound to a different process are not woken up unless we're in
123 * foreground mode.
Willy Tarreaube58c382011-07-24 18:28:10 +0200124 */
125int resume_listener(struct listener *l)
126{
127 if (l->state < LI_PAUSED)
128 return 0;
129
Willy Tarreauae302532014-05-07 19:22:24 +0200130 if ((global.mode & (MODE_DAEMON | MODE_SYSTEMD)) &&
131 l->bind_conf->bind_proc &&
132 !(l->bind_conf->bind_proc & (1UL << (relative_pid - 1))))
133 return 0;
134
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200135 if (l->proto->sock_prot == IPPROTO_TCP &&
136 l->state == LI_PAUSED &&
Willy Tarreaube58c382011-07-24 18:28:10 +0200137 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
138 return 0;
139
140 if (l->state == LI_READY)
141 return 1;
142
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200143 if (l->state == LI_LIMITED)
144 LIST_DEL(&l->wait_queue);
145
Willy Tarreaube58c382011-07-24 18:28:10 +0200146 if (l->nbconn >= l->maxconn) {
147 l->state = LI_FULL;
148 return 1;
149 }
150
Willy Tarreau49b046d2012-08-09 12:11:58 +0200151 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200152 l->state = LI_READY;
153 return 1;
154}
155
Willy Tarreau62793712011-07-24 19:23:38 +0200156/* Marks a ready listener as full so that the session code tries to re-enable
157 * it upon next close() using resume_listener().
158 */
159void listener_full(struct listener *l)
160{
161 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200162 if (l->state == LI_LIMITED)
163 LIST_DEL(&l->wait_queue);
164
Willy Tarreau49b046d2012-08-09 12:11:58 +0200165 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200166 l->state = LI_FULL;
167 }
168}
169
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200170/* Marks a ready listener as limited so that we only try to re-enable it when
171 * resources are free again. It will be queued into the specified queue.
172 */
173void limit_listener(struct listener *l, struct list *list)
174{
175 if (l->state == LI_READY) {
176 LIST_ADDQ(list, &l->wait_queue);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200177 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200178 l->state = LI_LIMITED;
179 }
180}
181
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100182/* This function adds all of the protocol's listener's file descriptors to the
183 * polling lists when they are in the LI_LISTEN state. It is intended to be
184 * used as a protocol's generic enable_all() primitive, for use after the
185 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
186 * their number of connections. It always returns ERR_NONE.
187 */
188int enable_all_listeners(struct protocol *proto)
189{
190 struct listener *listener;
191
192 list_for_each_entry(listener, &proto->listeners, proto_list)
193 enable_listener(listener);
194 return ERR_NONE;
195}
196
197/* This function removes all of the protocol's listener's file descriptors from
198 * the polling lists when they are in the LI_READY or LI_FULL states. It is
199 * intended to be used as a protocol's generic disable_all() primitive. It puts
200 * the listeners into LI_LISTEN, and always returns ERR_NONE.
201 */
202int disable_all_listeners(struct protocol *proto)
203{
204 struct listener *listener;
205
206 list_for_each_entry(listener, &proto->listeners, proto_list)
207 disable_listener(listener);
208 return ERR_NONE;
209}
210
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200211/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
212void dequeue_all_listeners(struct list *list)
213{
214 struct listener *listener, *l_back;
215
216 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
217 /* This cannot fail because the listeners are by definition in
218 * the LI_LIMITED state. The function also removes the entry
219 * from the queue.
220 */
221 resume_listener(listener);
222 }
223}
224
Willy Tarreaub648d632007-10-28 22:13:50 +0100225/* This function closes the listening socket for the specified listener,
226 * provided that it's already in a listening state. The listener enters the
227 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
228 * to be used as a generic function for standard protocols.
229 */
230int unbind_listener(struct listener *listener)
231{
232 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200233 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100234
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200235 if (listener->state == LI_LIMITED)
236 LIST_DEL(&listener->wait_queue);
237
Willy Tarreaube58c382011-07-24 18:28:10 +0200238 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100239 fd_delete(listener->fd);
Willy Tarreaufb8bf632014-07-07 18:24:48 +0200240 listener->fd = -1;
Willy Tarreaub648d632007-10-28 22:13:50 +0100241 listener->state = LI_ASSIGNED;
242 }
243 return ERR_NONE;
244}
245
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100246/* This function closes all listening sockets bound to the protocol <proto>,
247 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
248 * detach them from the protocol. It always returns ERR_NONE.
249 */
250int unbind_all_listeners(struct protocol *proto)
251{
252 struct listener *listener;
253
254 list_for_each_entry(listener, &proto->listeners, proto_list)
255 unbind_listener(listener);
256 return ERR_NONE;
257}
258
Willy Tarreau1a64d162007-10-28 22:26:05 +0100259/* Delete a listener from its protocol's list of listeners. The listener's
260 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
261 * number of listeners is updated. Note that the listener must have previously
262 * been unbound. This is the generic function to use to remove a listener.
263 */
264void delete_listener(struct listener *listener)
265{
266 if (listener->state != LI_ASSIGNED)
267 return;
268 listener->state = LI_INIT;
269 LIST_DEL(&listener->proto_list);
270 listener->proto->nb_listeners--;
271}
272
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200273/* This function is called on a read event from a listening socket, corresponding
274 * to an accept. It tries to accept as many connections as possible, and for each
275 * calls the listener's accept handler (generally the frontend's accept handler).
276 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200277void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200278{
279 struct listener *l = fdtab[fd].owner;
280 struct proxy *p = l->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100281 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubb660302014-05-07 19:47:02 +0200282 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200283 int cfd;
284 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100285#ifdef USE_ACCEPT4
286 static int accept4_broken;
287#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200288
289 if (unlikely(l->nbconn >= l->maxconn)) {
290 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200291 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200292 }
293
Willy Tarreau93e7c002013-10-07 18:51:07 +0200294 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
295 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200296
297 if (unlikely(!max)) {
298 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200299 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200300 goto wait_expire;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200301 }
302
303 if (max_accept > max)
304 max_accept = max;
305 }
306
307 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200308 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
309
310 if (unlikely(!max)) {
311 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200312 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200313 goto wait_expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200314 }
315
316 if (max_accept > max)
317 max_accept = max;
318 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200319#ifdef USE_OPENSSL
320 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
321 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200322
Willy Tarreaue43d5322013-10-07 20:01:52 +0200323 if (unlikely(!max)) {
324 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200325 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200326 goto wait_expire;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200327 }
328
329 if (max_accept > max)
330 max_accept = max;
331 }
332#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200333 if (p && p->fe_sps_lim) {
334 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
335
336 if (unlikely(!max)) {
337 /* frontend accept rate limit was reached */
338 limit_listener(l, &p->listener_queue);
339 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 +0200340 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200341 }
342
343 if (max_accept > max)
344 max_accept = max;
345 }
346
347 /* Note: if we fail to allocate a connection because of configured
348 * limits, we'll schedule a new attempt worst 1 second later in the
349 * worst case. If we fail due to system limits or temporary resource
350 * shortage, we try again 100ms later in the worst case.
351 */
352 while (max_accept--) {
353 struct sockaddr_storage addr;
354 socklen_t laddr = sizeof(addr);
355
356 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
357 limit_listener(l, &global_listener_queue);
358 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200359 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200360 }
361
362 if (unlikely(p && p->feconn >= p->maxconn)) {
363 limit_listener(l, &p->listener_queue);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200364 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200365 }
366
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200367#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100368 /* only call accept4() if it's known to be safe, otherwise
369 * fallback to the legacy accept() + fcntl().
370 */
371 if (unlikely(accept4_broken ||
372 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
373 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
374 (accept4_broken = 1))))
375#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200376 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
377 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100378
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200379 if (unlikely(cfd == -1)) {
380 switch (errno) {
381 case EAGAIN:
Willy Tarreaubb660302014-05-07 19:47:02 +0200382 if (fdtab[fd].ev & FD_POLL_HUP) {
383 /* the listening socket might have been disabled in a shared
384 * process and we're a collateral victim. We'll just pause for
385 * a while in case it comes back. In the mean time, we need to
386 * clear this sticky flag.
387 */
388 fdtab[fd].ev &= ~FD_POLL_HUP;
389 goto transient_error;
390 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100391 fd_cant_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200392 return; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200393 case EINVAL:
394 /* might be trying to accept on a shut fd (eg: soft stop) */
395 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100396 case EINTR:
397 case ECONNABORTED:
398 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200399 case ENFILE:
400 if (p)
401 send_log(p, LOG_EMERG,
402 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
403 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200404 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200405 case EMFILE:
406 if (p)
407 send_log(p, LOG_EMERG,
408 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
409 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200410 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200411 case ENOBUFS:
412 case ENOMEM:
413 if (p)
414 send_log(p, LOG_EMERG,
415 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
416 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200417 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200418 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100419 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100420 goto stop;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200421 }
422 }
423
424 if (unlikely(cfd >= global.maxsock)) {
425 send_log(p, LOG_EMERG,
426 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
427 p->id);
428 close(cfd);
429 limit_listener(l, &global_listener_queue);
430 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200431 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200432 }
433
434 /* increase the per-process number of cumulated connections */
435 if (!(l->options & LI_O_UNLIMITED)) {
436 update_freq_ctr(&global.conn_per_sec, 1);
437 if (global.conn_per_sec.curr_ctr > global.cps_max)
438 global.cps_max = global.conn_per_sec.curr_ctr;
439 actconn++;
440 }
441
442 jobs++;
443 totalconn++;
444 l->nbconn++;
445
446 if (l->counters) {
447 if (l->nbconn > l->counters->conn_max)
448 l->counters->conn_max = l->nbconn;
449 }
450
451 ret = l->accept(l, cfd, &addr);
452 if (unlikely(ret <= 0)) {
453 /* The connection was closed by session_accept(). Either
454 * we just have to ignore it (ret == 0) or it's a critical
455 * error due to a resource shortage, and we must stop the
456 * listener (ret < 0).
457 */
458 if (!(l->options & LI_O_UNLIMITED))
459 actconn--;
460 jobs--;
461 l->nbconn--;
462 if (ret == 0) /* successful termination */
463 continue;
464
Willy Tarreaubb660302014-05-07 19:47:02 +0200465 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200466 }
467
468 if (l->nbconn >= l->maxconn) {
469 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200470 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200471 }
472
Willy Tarreau93e7c002013-10-07 18:51:07 +0200473 /* increase the per-process number of cumulated connections */
474 if (!(l->options & LI_O_UNLIMITED)) {
475 update_freq_ctr(&global.sess_per_sec, 1);
476 if (global.sess_per_sec.curr_ctr > global.sps_max)
477 global.sps_max = global.sess_per_sec.curr_ctr;
478 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200479#ifdef USE_OPENSSL
480 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
481
482 update_freq_ctr(&global.ssl_per_sec, 1);
483 if (global.ssl_per_sec.curr_ctr > global.ssl_max)
484 global.ssl_max = global.ssl_per_sec.curr_ctr;
485 }
486#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200487
Willy Tarreauaece46a2012-07-06 12:25:58 +0200488 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200489
Willy Tarreauaece46a2012-07-06 12:25:58 +0200490 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100491 stop:
492 fd_done_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200493 return;
Willy Tarreaubb660302014-05-07 19:47:02 +0200494
495 transient_error:
496 /* pause the listener and try again in 100 ms */
497 expire = tick_add(now_ms, 100);
498
499 wait_expire:
500 limit_listener(l, &global_listener_queue);
501 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
502 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200503}
504
Willy Tarreau26982662012-09-12 23:17:10 +0200505/*
506 * Registers the bind keyword list <kwl> as a list of valid keywords for next
507 * parsing sessions.
508 */
509void bind_register_keywords(struct bind_kw_list *kwl)
510{
511 LIST_ADDQ(&bind_keywords.list, &kwl->list);
512}
513
514/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
515 * keyword is found with a NULL ->parse() function, then an attempt is made to
516 * find one with a valid ->parse() function. This way it is possible to declare
517 * platform-dependant, known keywords as NULL, then only declare them as valid
518 * if some options are met. Note that if the requested keyword contains an
519 * opening parenthesis, everything from this point is ignored.
520 */
521struct bind_kw *bind_find_kw(const char *kw)
522{
523 int index;
524 const char *kwend;
525 struct bind_kw_list *kwl;
526 struct bind_kw *ret = NULL;
527
528 kwend = strchr(kw, '(');
529 if (!kwend)
530 kwend = kw + strlen(kw);
531
532 list_for_each_entry(kwl, &bind_keywords.list, list) {
533 for (index = 0; kwl->kw[index].kw != NULL; index++) {
534 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
535 kwl->kw[index].kw[kwend-kw] == 0) {
536 if (kwl->kw[index].parse)
537 return &kwl->kw[index]; /* found it !*/
538 else
539 ret = &kwl->kw[index]; /* may be OK */
540 }
541 }
542 }
543 return ret;
544}
545
Willy Tarreau8638f482012-09-18 18:01:17 +0200546/* Dumps all registered "bind" keywords to the <out> string pointer. The
547 * unsupported keywords are only dumped if their supported form was not
548 * found.
549 */
550void bind_dump_kws(char **out)
551{
552 struct bind_kw_list *kwl;
553 int index;
554
555 *out = NULL;
556 list_for_each_entry(kwl, &bind_keywords.list, list) {
557 for (index = 0; kwl->kw[index].kw != NULL; index++) {
558 if (kwl->kw[index].parse ||
559 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200560 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
561 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200562 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200563 kwl->kw[index].skip ? " <arg>" : "",
564 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200565 }
566 }
567 }
568}
569
Willy Tarreau645513a2010-05-24 20:55:15 +0200570/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100571/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200572/************************************************************************/
573
Willy Tarreaua5e37562011-12-16 17:06:15 +0100574/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200575static int
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100576smp_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200577 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +0200578{
Willy Tarreauf853c462012-04-23 18:53:56 +0200579 smp->type = SMP_T_UINT;
580 smp->data.uint = l4->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200581 return 1;
582}
583
Willy Tarreaua5e37562011-12-16 17:06:15 +0100584/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200585static int
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100586smp_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200587 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +0200588{
Willy Tarreauf853c462012-04-23 18:53:56 +0200589 smp->type = SMP_T_UINT;
590 smp->data.uint = l4->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200591 return 1;
592}
593
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200594/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200595static 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 +0200596{
597 struct listener *l;
598
Willy Tarreau4348fad2012-09-20 16:48:07 +0200599 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200600 l->options |= LI_O_ACC_PROXY;
601
602 return 0;
603}
604
605/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200606static 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 +0200607{
608 struct listener *l;
609 int val;
610
611 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200612 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200613 return ERR_ALERT | ERR_FATAL;
614 }
615
616 val = atol(args[cur_arg + 1]);
617 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200618 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200619 return ERR_ALERT | ERR_FATAL;
620 }
621
Willy Tarreau4348fad2012-09-20 16:48:07 +0200622 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200623 l->backlog = val;
624
625 return 0;
626}
627
628/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200629static 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 +0200630{
631 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200632 struct listener *l, *new;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200633
Willy Tarreau4348fad2012-09-20 16:48:07 +0200634 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200635 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200636 return ERR_ALERT | ERR_FATAL;
637 }
638
639 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200640 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200641 return ERR_ALERT | ERR_FATAL;
642 }
643
Willy Tarreau4348fad2012-09-20 16:48:07 +0200644 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
645 new->luid = atol(args[cur_arg + 1]);
646 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200647
Willy Tarreau4348fad2012-09-20 16:48:07 +0200648 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200649 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200650 return ERR_ALERT | ERR_FATAL;
651 }
652
Willy Tarreau4348fad2012-09-20 16:48:07 +0200653 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200654 if (node) {
655 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200656 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
657 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
658 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200659 return ERR_ALERT | ERR_FATAL;
660 }
661
Willy Tarreau4348fad2012-09-20 16:48:07 +0200662 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200663 return 0;
664}
665
666/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200667static 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 +0200668{
669 struct listener *l;
670 int val;
671
672 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200673 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200674 return ERR_ALERT | ERR_FATAL;
675 }
676
677 val = atol(args[cur_arg + 1]);
678 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200679 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200680 return ERR_ALERT | ERR_FATAL;
681 }
682
Willy Tarreau4348fad2012-09-20 16:48:07 +0200683 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200684 l->maxconn = val;
685
686 return 0;
687}
688
689/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200690static 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 +0200691{
692 struct listener *l;
693
694 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200695 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200696 return ERR_ALERT | ERR_FATAL;
697 }
698
Willy Tarreau4348fad2012-09-20 16:48:07 +0200699 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200700 l->name = strdup(args[cur_arg + 1]);
701
702 return 0;
703}
704
705/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200706static 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 +0200707{
708 struct listener *l;
709 int val;
710
711 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200712 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200713 return ERR_ALERT | ERR_FATAL;
714 }
715
716 val = atol(args[cur_arg + 1]);
717 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200718 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200719 return ERR_ALERT | ERR_FATAL;
720 }
721
Willy Tarreau4348fad2012-09-20 16:48:07 +0200722 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200723 l->nice = val;
724
725 return 0;
726}
727
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200728/* parse the "process" bind keyword */
729static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
730{
731 unsigned long set = 0;
732 unsigned int low, high;
733
734 if (strcmp(args[cur_arg + 1], "all") == 0) {
735 set = 0;
736 }
737 else if (strcmp(args[cur_arg + 1], "odd") == 0) {
738 set |= ~0UL/3UL; /* 0x555....555 */
739 }
740 else if (strcmp(args[cur_arg + 1], "even") == 0) {
741 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
742 }
743 else if (isdigit((int)*args[cur_arg + 1])) {
744 char *dash = strchr(args[cur_arg + 1], '-');
745
746 low = high = str2uic(args[cur_arg + 1]);
747 if (dash)
748 high = str2uic(dash + 1);
749
750 if (high < low) {
751 unsigned int swap = low;
752 low = high;
753 high = swap;
754 }
755
756 if (low < 1 || high > LONGBITS) {
757 memprintf(err, "'%s' : invalid range %d-%d, allowed range is 1..%d", args[cur_arg], low, high, LONGBITS);
758 return ERR_ALERT | ERR_FATAL;
759 }
760 while (low <= high)
761 set |= 1UL << (low++ - 1);
762 }
763 else {
764 memprintf(err, "'%s' expects 'all', 'odd', 'even', or a process range with numbers from 1 to %d.", args[cur_arg], LONGBITS);
765 return ERR_ALERT | ERR_FATAL;
766 }
767
768 conf->bind_proc = set;
769 return 0;
770}
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200771
Willy Tarreau61612d42012-04-19 18:42:05 +0200772/* Note: must not be declared <const> as its list will be overwritten.
773 * Please take care of keeping this list alphabetically sorted.
774 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200775static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100776 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
777 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
778 { /* END */ },
779}};
780
781/* Note: must not be declared <const> as its list will be overwritten.
782 * Please take care of keeping this list alphabetically sorted.
783 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200784static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100785 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +0200786}};
787
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200788/* Note: must not be declared <const> as its list will be overwritten.
789 * Please take care of keeping this list alphabetically sorted, doing so helps
790 * all code contributors.
791 * Optional keywords are also declared with a NULL ->parse() function so that
792 * the config parser can report an appropriate error when a known keyword was
793 * not enabled.
794 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200795static struct bind_kw_list bind_kws = { "ALL", { }, {
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200796 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
797 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
798 { "id", bind_parse_id, 1 }, /* set id of listening socket */
799 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
800 { "name", bind_parse_name, 1 }, /* set name of listening socket */
801 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200802 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100803 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200804}};
805
Willy Tarreau645513a2010-05-24 20:55:15 +0200806__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +0200807static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +0200808{
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100809 sample_register_fetches(&smp_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200810 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200811 bind_register_keywords(&bind_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200812}
813
814/*
815 * Local variables:
816 * c-indent-level: 8
817 * c-basic-offset: 8
818 * End:
819 */