blob: 11df69fd17e3fcc7915999bb47f1ffa098af953d [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 Tarreau092d8652014-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 Tarreau092d8652014-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
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200123 * foreground mode. If the listener was only in the assigned state, it's totally
124 * rebound. This can happen if a pause() has completely stopped it. If the
125 * resume fails, 0 is returned and an error might be displayed.
Willy Tarreaube58c382011-07-24 18:28:10 +0200126 */
127int resume_listener(struct listener *l)
128{
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200129 if (l->state == LI_ASSIGNED) {
130 char msg[100];
131 int err;
132
133 err = l->proto->bind(l, msg, sizeof(msg));
134 if (err & ERR_ALERT)
135 Alert("Resuming listener: %s\n", msg);
136 else if (err & ERR_WARN)
137 Warning("Resuming listener: %s\n", msg);
138
139 if (err & (ERR_FATAL | ERR_ABORT))
140 return 0;
141 }
142
Willy Tarreaube58c382011-07-24 18:28:10 +0200143 if (l->state < LI_PAUSED)
144 return 0;
145
Willy Tarreauae302532014-05-07 19:22:24 +0200146 if ((global.mode & (MODE_DAEMON | MODE_SYSTEMD)) &&
147 l->bind_conf->bind_proc &&
148 !(l->bind_conf->bind_proc & (1UL << (relative_pid - 1))))
149 return 0;
150
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200151 if (l->proto->sock_prot == IPPROTO_TCP &&
152 l->state == LI_PAUSED &&
Willy Tarreaube58c382011-07-24 18:28:10 +0200153 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
154 return 0;
155
156 if (l->state == LI_READY)
157 return 1;
158
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200159 if (l->state == LI_LIMITED)
160 LIST_DEL(&l->wait_queue);
161
Willy Tarreaube58c382011-07-24 18:28:10 +0200162 if (l->nbconn >= l->maxconn) {
163 l->state = LI_FULL;
164 return 1;
165 }
166
Willy Tarreau49b046d2012-08-09 12:11:58 +0200167 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200168 l->state = LI_READY;
169 return 1;
170}
171
Willy Tarreau62793712011-07-24 19:23:38 +0200172/* Marks a ready listener as full so that the session code tries to re-enable
173 * it upon next close() using resume_listener().
174 */
175void listener_full(struct listener *l)
176{
177 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200178 if (l->state == LI_LIMITED)
179 LIST_DEL(&l->wait_queue);
180
Willy Tarreau49b046d2012-08-09 12:11:58 +0200181 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200182 l->state = LI_FULL;
183 }
184}
185
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200186/* Marks a ready listener as limited so that we only try to re-enable it when
187 * resources are free again. It will be queued into the specified queue.
188 */
189void limit_listener(struct listener *l, struct list *list)
190{
191 if (l->state == LI_READY) {
192 LIST_ADDQ(list, &l->wait_queue);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200193 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200194 l->state = LI_LIMITED;
195 }
196}
197
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100198/* This function adds all of the protocol's listener's file descriptors to the
199 * polling lists when they are in the LI_LISTEN state. It is intended to be
200 * used as a protocol's generic enable_all() primitive, for use after the
201 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
202 * their number of connections. It always returns ERR_NONE.
203 */
204int enable_all_listeners(struct protocol *proto)
205{
206 struct listener *listener;
207
208 list_for_each_entry(listener, &proto->listeners, proto_list)
209 enable_listener(listener);
210 return ERR_NONE;
211}
212
213/* This function removes all of the protocol's listener's file descriptors from
214 * the polling lists when they are in the LI_READY or LI_FULL states. It is
215 * intended to be used as a protocol's generic disable_all() primitive. It puts
216 * the listeners into LI_LISTEN, and always returns ERR_NONE.
217 */
218int disable_all_listeners(struct protocol *proto)
219{
220 struct listener *listener;
221
222 list_for_each_entry(listener, &proto->listeners, proto_list)
223 disable_listener(listener);
224 return ERR_NONE;
225}
226
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200227/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
228void dequeue_all_listeners(struct list *list)
229{
230 struct listener *listener, *l_back;
231
232 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
233 /* This cannot fail because the listeners are by definition in
234 * the LI_LIMITED state. The function also removes the entry
235 * from the queue.
236 */
237 resume_listener(listener);
238 }
239}
240
Willy Tarreaub648d632007-10-28 22:13:50 +0100241/* This function closes the listening socket for the specified listener,
242 * provided that it's already in a listening state. The listener enters the
243 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
244 * to be used as a generic function for standard protocols.
245 */
246int unbind_listener(struct listener *listener)
247{
248 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200249 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100250
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200251 if (listener->state == LI_LIMITED)
252 LIST_DEL(&listener->wait_queue);
253
Willy Tarreaube58c382011-07-24 18:28:10 +0200254 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100255 fd_delete(listener->fd);
Willy Tarreau39447b62014-07-07 18:24:48 +0200256 listener->fd = -1;
Willy Tarreaub648d632007-10-28 22:13:50 +0100257 listener->state = LI_ASSIGNED;
258 }
259 return ERR_NONE;
260}
261
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100262/* This function closes all listening sockets bound to the protocol <proto>,
263 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
264 * detach them from the protocol. It always returns ERR_NONE.
265 */
266int unbind_all_listeners(struct protocol *proto)
267{
268 struct listener *listener;
269
270 list_for_each_entry(listener, &proto->listeners, proto_list)
271 unbind_listener(listener);
272 return ERR_NONE;
273}
274
Willy Tarreau1a64d162007-10-28 22:26:05 +0100275/* Delete a listener from its protocol's list of listeners. The listener's
276 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
277 * number of listeners is updated. Note that the listener must have previously
278 * been unbound. This is the generic function to use to remove a listener.
279 */
280void delete_listener(struct listener *listener)
281{
282 if (listener->state != LI_ASSIGNED)
283 return;
284 listener->state = LI_INIT;
285 LIST_DEL(&listener->proto_list);
286 listener->proto->nb_listeners--;
287}
288
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200289/* This function is called on a read event from a listening socket, corresponding
290 * to an accept. It tries to accept as many connections as possible, and for each
291 * calls the listener's accept handler (generally the frontend's accept handler).
292 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200293void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200294{
295 struct listener *l = fdtab[fd].owner;
296 struct proxy *p = l->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100297 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubb660302014-05-07 19:47:02 +0200298 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200299 int cfd;
300 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100301#ifdef USE_ACCEPT4
302 static int accept4_broken;
303#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200304
305 if (unlikely(l->nbconn >= l->maxconn)) {
306 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200307 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200308 }
309
Willy Tarreau93e7c002013-10-07 18:51:07 +0200310 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
311 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200312
313 if (unlikely(!max)) {
314 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200315 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200316 goto wait_expire;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200317 }
318
319 if (max_accept > max)
320 max_accept = max;
321 }
322
323 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200324 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
325
326 if (unlikely(!max)) {
327 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200328 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200329 goto wait_expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200330 }
331
332 if (max_accept > max)
333 max_accept = max;
334 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200335#ifdef USE_OPENSSL
336 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
337 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200338
Willy Tarreaue43d5322013-10-07 20:01:52 +0200339 if (unlikely(!max)) {
340 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200341 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200342 goto wait_expire;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200343 }
344
345 if (max_accept > max)
346 max_accept = max;
347 }
348#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200349 if (p && p->fe_sps_lim) {
350 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
351
352 if (unlikely(!max)) {
353 /* frontend accept rate limit was reached */
354 limit_listener(l, &p->listener_queue);
355 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 +0200356 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200357 }
358
359 if (max_accept > max)
360 max_accept = max;
361 }
362
363 /* Note: if we fail to allocate a connection because of configured
364 * limits, we'll schedule a new attempt worst 1 second later in the
365 * worst case. If we fail due to system limits or temporary resource
366 * shortage, we try again 100ms later in the worst case.
367 */
368 while (max_accept--) {
369 struct sockaddr_storage addr;
370 socklen_t laddr = sizeof(addr);
371
372 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
373 limit_listener(l, &global_listener_queue);
374 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200375 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200376 }
377
378 if (unlikely(p && p->feconn >= p->maxconn)) {
379 limit_listener(l, &p->listener_queue);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200380 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200381 }
382
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200383#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100384 /* only call accept4() if it's known to be safe, otherwise
385 * fallback to the legacy accept() + fcntl().
386 */
387 if (unlikely(accept4_broken ||
388 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
389 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
390 (accept4_broken = 1))))
391#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200392 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
393 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100394
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200395 if (unlikely(cfd == -1)) {
396 switch (errno) {
397 case EAGAIN:
Willy Tarreaubb660302014-05-07 19:47:02 +0200398 if (fdtab[fd].ev & FD_POLL_HUP) {
399 /* the listening socket might have been disabled in a shared
400 * process and we're a collateral victim. We'll just pause for
401 * a while in case it comes back. In the mean time, we need to
402 * clear this sticky flag.
403 */
404 fdtab[fd].ev &= ~FD_POLL_HUP;
405 goto transient_error;
406 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100407 fd_cant_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200408 return; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200409 case EINVAL:
410 /* might be trying to accept on a shut fd (eg: soft stop) */
411 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100412 case EINTR:
413 case ECONNABORTED:
414 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200415 case ENFILE:
416 if (p)
417 send_log(p, LOG_EMERG,
418 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
419 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200420 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200421 case EMFILE:
422 if (p)
423 send_log(p, LOG_EMERG,
424 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
425 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200426 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200427 case ENOBUFS:
428 case ENOMEM:
429 if (p)
430 send_log(p, LOG_EMERG,
431 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
432 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200433 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200434 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100435 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100436 goto stop;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200437 }
438 }
439
440 if (unlikely(cfd >= global.maxsock)) {
441 send_log(p, LOG_EMERG,
442 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
443 p->id);
444 close(cfd);
445 limit_listener(l, &global_listener_queue);
446 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200447 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200448 }
449
450 /* increase the per-process number of cumulated connections */
451 if (!(l->options & LI_O_UNLIMITED)) {
452 update_freq_ctr(&global.conn_per_sec, 1);
453 if (global.conn_per_sec.curr_ctr > global.cps_max)
454 global.cps_max = global.conn_per_sec.curr_ctr;
455 actconn++;
456 }
457
458 jobs++;
459 totalconn++;
460 l->nbconn++;
461
462 if (l->counters) {
463 if (l->nbconn > l->counters->conn_max)
464 l->counters->conn_max = l->nbconn;
465 }
466
467 ret = l->accept(l, cfd, &addr);
468 if (unlikely(ret <= 0)) {
469 /* The connection was closed by session_accept(). Either
470 * we just have to ignore it (ret == 0) or it's a critical
471 * error due to a resource shortage, and we must stop the
472 * listener (ret < 0).
473 */
474 if (!(l->options & LI_O_UNLIMITED))
475 actconn--;
476 jobs--;
477 l->nbconn--;
478 if (ret == 0) /* successful termination */
479 continue;
480
Willy Tarreaubb660302014-05-07 19:47:02 +0200481 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200482 }
483
484 if (l->nbconn >= l->maxconn) {
485 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200486 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200487 }
488
Willy Tarreau93e7c002013-10-07 18:51:07 +0200489 /* increase the per-process number of cumulated connections */
490 if (!(l->options & LI_O_UNLIMITED)) {
491 update_freq_ctr(&global.sess_per_sec, 1);
492 if (global.sess_per_sec.curr_ctr > global.sps_max)
493 global.sps_max = global.sess_per_sec.curr_ctr;
494 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200495#ifdef USE_OPENSSL
496 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
497
498 update_freq_ctr(&global.ssl_per_sec, 1);
499 if (global.ssl_per_sec.curr_ctr > global.ssl_max)
500 global.ssl_max = global.ssl_per_sec.curr_ctr;
501 }
502#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200503
Willy Tarreauaece46a2012-07-06 12:25:58 +0200504 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200505
Willy Tarreauaece46a2012-07-06 12:25:58 +0200506 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100507 stop:
508 fd_done_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200509 return;
Willy Tarreaubb660302014-05-07 19:47:02 +0200510
511 transient_error:
512 /* pause the listener and try again in 100 ms */
513 expire = tick_add(now_ms, 100);
514
515 wait_expire:
516 limit_listener(l, &global_listener_queue);
517 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
518 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200519}
520
Willy Tarreau26982662012-09-12 23:17:10 +0200521/*
522 * Registers the bind keyword list <kwl> as a list of valid keywords for next
523 * parsing sessions.
524 */
525void bind_register_keywords(struct bind_kw_list *kwl)
526{
527 LIST_ADDQ(&bind_keywords.list, &kwl->list);
528}
529
530/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
531 * keyword is found with a NULL ->parse() function, then an attempt is made to
532 * find one with a valid ->parse() function. This way it is possible to declare
533 * platform-dependant, known keywords as NULL, then only declare them as valid
534 * if some options are met. Note that if the requested keyword contains an
535 * opening parenthesis, everything from this point is ignored.
536 */
537struct bind_kw *bind_find_kw(const char *kw)
538{
539 int index;
540 const char *kwend;
541 struct bind_kw_list *kwl;
542 struct bind_kw *ret = NULL;
543
544 kwend = strchr(kw, '(');
545 if (!kwend)
546 kwend = kw + strlen(kw);
547
548 list_for_each_entry(kwl, &bind_keywords.list, list) {
549 for (index = 0; kwl->kw[index].kw != NULL; index++) {
550 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
551 kwl->kw[index].kw[kwend-kw] == 0) {
552 if (kwl->kw[index].parse)
553 return &kwl->kw[index]; /* found it !*/
554 else
555 ret = &kwl->kw[index]; /* may be OK */
556 }
557 }
558 }
559 return ret;
560}
561
Willy Tarreau8638f482012-09-18 18:01:17 +0200562/* Dumps all registered "bind" keywords to the <out> string pointer. The
563 * unsupported keywords are only dumped if their supported form was not
564 * found.
565 */
566void bind_dump_kws(char **out)
567{
568 struct bind_kw_list *kwl;
569 int index;
570
571 *out = NULL;
572 list_for_each_entry(kwl, &bind_keywords.list, list) {
573 for (index = 0; kwl->kw[index].kw != NULL; index++) {
574 if (kwl->kw[index].parse ||
575 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200576 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
577 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200578 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200579 kwl->kw[index].skip ? " <arg>" : "",
580 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200581 }
582 }
583 }
584}
585
Willy Tarreau645513a2010-05-24 20:55:15 +0200586/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100587/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200588/************************************************************************/
589
Willy Tarreaua5e37562011-12-16 17:06:15 +0100590/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200591static int
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100592smp_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200593 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +0200594{
Willy Tarreauf853c462012-04-23 18:53:56 +0200595 smp->type = SMP_T_UINT;
596 smp->data.uint = l4->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200597 return 1;
598}
599
Willy Tarreaua5e37562011-12-16 17:06:15 +0100600/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200601static int
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100602smp_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200603 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +0200604{
Willy Tarreauf853c462012-04-23 18:53:56 +0200605 smp->type = SMP_T_UINT;
606 smp->data.uint = l4->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200607 return 1;
608}
609
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200610/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200611static 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 +0200612{
613 struct listener *l;
614
Willy Tarreau4348fad2012-09-20 16:48:07 +0200615 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200616 l->options |= LI_O_ACC_PROXY;
617
618 return 0;
619}
620
621/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200622static 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 +0200623{
624 struct listener *l;
625 int val;
626
627 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200628 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200629 return ERR_ALERT | ERR_FATAL;
630 }
631
632 val = atol(args[cur_arg + 1]);
633 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200634 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200635 return ERR_ALERT | ERR_FATAL;
636 }
637
Willy Tarreau4348fad2012-09-20 16:48:07 +0200638 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200639 l->backlog = val;
640
641 return 0;
642}
643
644/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200645static 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 +0200646{
647 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200648 struct listener *l, *new;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200649
Willy Tarreau4348fad2012-09-20 16:48:07 +0200650 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200651 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200652 return ERR_ALERT | ERR_FATAL;
653 }
654
655 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200656 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200657 return ERR_ALERT | ERR_FATAL;
658 }
659
Willy Tarreau4348fad2012-09-20 16:48:07 +0200660 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
661 new->luid = atol(args[cur_arg + 1]);
662 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200663
Willy Tarreau4348fad2012-09-20 16:48:07 +0200664 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200665 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200666 return ERR_ALERT | ERR_FATAL;
667 }
668
Willy Tarreau4348fad2012-09-20 16:48:07 +0200669 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200670 if (node) {
671 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200672 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
673 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
674 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200675 return ERR_ALERT | ERR_FATAL;
676 }
677
Willy Tarreau4348fad2012-09-20 16:48:07 +0200678 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200679 return 0;
680}
681
682/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200683static 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 +0200684{
685 struct listener *l;
686 int val;
687
688 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200689 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200690 return ERR_ALERT | ERR_FATAL;
691 }
692
693 val = atol(args[cur_arg + 1]);
694 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200695 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
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->maxconn = val;
701
702 return 0;
703}
704
705/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200706static 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 +0200707{
708 struct listener *l;
709
710 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200711 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200712 return ERR_ALERT | ERR_FATAL;
713 }
714
Willy Tarreau4348fad2012-09-20 16:48:07 +0200715 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200716 l->name = strdup(args[cur_arg + 1]);
717
718 return 0;
719}
720
721/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200722static 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 +0200723{
724 struct listener *l;
725 int val;
726
727 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200728 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200729 return ERR_ALERT | ERR_FATAL;
730 }
731
732 val = atol(args[cur_arg + 1]);
733 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200734 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200735 return ERR_ALERT | ERR_FATAL;
736 }
737
Willy Tarreau4348fad2012-09-20 16:48:07 +0200738 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200739 l->nice = val;
740
741 return 0;
742}
743
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200744/* parse the "process" bind keyword */
745static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
746{
747 unsigned long set = 0;
748 unsigned int low, high;
749
750 if (strcmp(args[cur_arg + 1], "all") == 0) {
751 set = 0;
752 }
753 else if (strcmp(args[cur_arg + 1], "odd") == 0) {
754 set |= ~0UL/3UL; /* 0x555....555 */
755 }
756 else if (strcmp(args[cur_arg + 1], "even") == 0) {
757 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
758 }
759 else if (isdigit((int)*args[cur_arg + 1])) {
760 char *dash = strchr(args[cur_arg + 1], '-');
761
762 low = high = str2uic(args[cur_arg + 1]);
763 if (dash)
764 high = str2uic(dash + 1);
765
766 if (high < low) {
767 unsigned int swap = low;
768 low = high;
769 high = swap;
770 }
771
772 if (low < 1 || high > LONGBITS) {
773 memprintf(err, "'%s' : invalid range %d-%d, allowed range is 1..%d", args[cur_arg], low, high, LONGBITS);
774 return ERR_ALERT | ERR_FATAL;
775 }
776 while (low <= high)
777 set |= 1UL << (low++ - 1);
778 }
779 else {
780 memprintf(err, "'%s' expects 'all', 'odd', 'even', or a process range with numbers from 1 to %d.", args[cur_arg], LONGBITS);
781 return ERR_ALERT | ERR_FATAL;
782 }
783
784 conf->bind_proc = set;
785 return 0;
786}
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200787
Willy Tarreau61612d42012-04-19 18:42:05 +0200788/* Note: must not be declared <const> as its list will be overwritten.
789 * Please take care of keeping this list alphabetically sorted.
790 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200791static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100792 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
793 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
794 { /* END */ },
795}};
796
797/* Note: must not be declared <const> as its list will be overwritten.
798 * Please take care of keeping this list alphabetically sorted.
799 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200800static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100801 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +0200802}};
803
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200804/* Note: must not be declared <const> as its list will be overwritten.
805 * Please take care of keeping this list alphabetically sorted, doing so helps
806 * all code contributors.
807 * Optional keywords are also declared with a NULL ->parse() function so that
808 * the config parser can report an appropriate error when a known keyword was
809 * not enabled.
810 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200811static struct bind_kw_list bind_kws = { "ALL", { }, {
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200812 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
813 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
814 { "id", bind_parse_id, 1 }, /* set id of listening socket */
815 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
816 { "name", bind_parse_name, 1 }, /* set name of listening socket */
817 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200818 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100819 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200820}};
821
Willy Tarreau645513a2010-05-24 20:55:15 +0200822__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +0200823static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +0200824{
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100825 sample_register_fetches(&smp_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200826 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200827 bind_register_keywords(&bind_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200828}
829
830/*
831 * Local variables:
832 * c-indent-level: 8
833 * c-basic-offset: 8
834 * End:
835 */