blob: 566b34082ac85119d246771e86254330f4adcdf1 [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreaud1d54542012-09-12 22:58:11 +02002 * Listener management functions.
Willy Tarreaudd815982007-10-16 12:25:14 +02003 *
Willy Tarreau0ccb7442013-01-07 22:54:17 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaudd815982007-10-16 12:25:14 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau44489252014-01-14 17:52:01 +010013#define _GNU_SOURCE
Willy Tarreau6ae1ba62014-05-07 19:01:58 +020014#include <ctype.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020015#include <errno.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020016#include <stdio.h>
17#include <string.h>
Willy Tarreau95ccdde2014-02-01 09:28:36 +010018#include <unistd.h>
19#include <fcntl.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020020
Willy Tarreau1bc4aab2012-10-08 20:11:03 +020021#include <common/accept4.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020022#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010023#include <common/errors.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020024#include <common/mini-clist.h>
25#include <common/standard.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020026#include <common/time.h>
27
28#include <types/global.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020029#include <types/protocol.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020030
Willy Tarreau645513a2010-05-24 20:55:15 +020031#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010032#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020033#include <proto/freq_ctr.h>
34#include <proto/log.h>
Willy Tarreau7a798e52016-04-14 11:13:20 +020035#include <proto/listener.h>
Willy Tarreau0ccb7442013-01-07 22:54:17 +010036#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020037#include <proto/stream.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020038#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010039
Willy Tarreau26982662012-09-12 23:17:10 +020040/* List head of all known bind keywords */
41static struct bind_kw_list bind_keywords = {
42 .list = LIST_HEAD_INIT(bind_keywords.list)
43};
44
Willy Tarreaudabf2e22007-10-28 21:59:24 +010045/* This function adds the specified listener's file descriptor to the polling
46 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
Willy Tarreauae302532014-05-07 19:22:24 +020047 * LI_FULL state depending on its number of connections. In deamon mode, we
48 * also support binding only the relevant processes to their respective
49 * listeners. We don't do that in debug mode however.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010050 */
51void enable_listener(struct listener *listener)
52{
53 if (listener->state == LI_LISTEN) {
Willy Tarreauae302532014-05-07 19:22:24 +020054 if ((global.mode & (MODE_DAEMON | MODE_SYSTEMD)) &&
55 listener->bind_conf->bind_proc &&
56 !(listener->bind_conf->bind_proc & (1UL << (relative_pid - 1)))) {
57 /* we don't want to enable this listener and don't
58 * want any fd event to reach it.
59 */
60 fd_stop_recv(listener->fd);
61 listener->state = LI_PAUSED;
62 }
63 else if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020064 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010065 listener->state = LI_READY;
Willy Tarreauae302532014-05-07 19:22:24 +020066 }
67 else {
Willy Tarreaudabf2e22007-10-28 21:59:24 +010068 listener->state = LI_FULL;
69 }
70 }
71}
72
73/* This function removes the specified listener's file descriptor from the
74 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
75 * enters LI_LISTEN.
76 */
77void disable_listener(struct listener *listener)
78{
79 if (listener->state < LI_READY)
80 return;
81 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +020082 fd_stop_recv(listener->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020083 if (listener->state == LI_LIMITED)
84 LIST_DEL(&listener->wait_queue);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010085 listener->state = LI_LISTEN;
86}
87
Willy Tarreaube58c382011-07-24 18:28:10 +020088/* This function tries to temporarily disable a listener, depending on the OS
89 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
90 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
91 * closes upon SHUT_WR and refuses to rebind. So a common validation path
92 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
93 * is disabled. It normally returns non-zero, unless an error is reported.
94 */
95int pause_listener(struct listener *l)
96{
97 if (l->state <= LI_PAUSED)
98 return 1;
99
Willy Tarreau092d8652014-07-07 20:22:12 +0200100 if (l->proto->pause) {
101 /* Returns < 0 in case of failure, 0 if the listener
102 * was totally stopped, or > 0 if correctly paused.
103 */
104 int ret = l->proto->pause(l);
Willy Tarreaube58c382011-07-24 18:28:10 +0200105
Willy Tarreau092d8652014-07-07 20:22:12 +0200106 if (ret < 0)
107 return 0;
108 else if (ret == 0)
109 return 1;
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200110 }
Willy Tarreaube58c382011-07-24 18:28:10 +0200111
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200112 if (l->state == LI_LIMITED)
113 LIST_DEL(&l->wait_queue);
114
Willy Tarreau49b046d2012-08-09 12:11:58 +0200115 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200116 l->state = LI_PAUSED;
117 return 1;
118}
119
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200120/* This function tries to resume a temporarily disabled listener. Paused, full,
121 * limited and disabled listeners are handled, which means that this function
122 * may replace enable_listener(). The resulting state will either be LI_READY
123 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreauae302532014-05-07 19:22:24 +0200124 * Listeners bound to a different process are not woken up unless we're in
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200125 * foreground mode, and are ignored. If the listener was only in the assigned
126 * state, it's totally rebound. This can happen if a pause() has completely
127 * stopped it. If the resume fails, 0 is returned and an error might be
128 * displayed.
Willy Tarreaube58c382011-07-24 18:28:10 +0200129 */
130int resume_listener(struct listener *l)
131{
Willy Tarreau1c4b8142014-07-07 21:06:24 +0200132 if (l->state == LI_ASSIGNED) {
133 char msg[100];
134 int err;
135
136 err = l->proto->bind(l, msg, sizeof(msg));
137 if (err & ERR_ALERT)
138 Alert("Resuming listener: %s\n", msg);
139 else if (err & ERR_WARN)
140 Warning("Resuming listener: %s\n", msg);
141
142 if (err & (ERR_FATAL | ERR_ABORT))
143 return 0;
144 }
145
Willy Tarreaube58c382011-07-24 18:28:10 +0200146 if (l->state < LI_PAUSED)
147 return 0;
148
Willy Tarreauae302532014-05-07 19:22:24 +0200149 if ((global.mode & (MODE_DAEMON | MODE_SYSTEMD)) &&
150 l->bind_conf->bind_proc &&
151 !(l->bind_conf->bind_proc & (1UL << (relative_pid - 1))))
Willy Tarreauaf2fd582015-04-14 12:07:16 +0200152 return 1;
Willy Tarreauae302532014-05-07 19:22:24 +0200153
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200154 if (l->proto->sock_prot == IPPROTO_TCP &&
155 l->state == LI_PAUSED &&
Willy Tarreaube58c382011-07-24 18:28:10 +0200156 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
157 return 0;
158
159 if (l->state == LI_READY)
160 return 1;
161
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200162 if (l->state == LI_LIMITED)
163 LIST_DEL(&l->wait_queue);
164
Willy Tarreaube58c382011-07-24 18:28:10 +0200165 if (l->nbconn >= l->maxconn) {
166 l->state = LI_FULL;
167 return 1;
168 }
169
Willy Tarreau49b046d2012-08-09 12:11:58 +0200170 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200171 l->state = LI_READY;
172 return 1;
173}
174
Willy Tarreau87b09662015-04-03 00:22:06 +0200175/* Marks a ready listener as full so that the stream code tries to re-enable
Willy Tarreau62793712011-07-24 19:23:38 +0200176 * it upon next close() using resume_listener().
177 */
178void listener_full(struct listener *l)
179{
180 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200181 if (l->state == LI_LIMITED)
182 LIST_DEL(&l->wait_queue);
183
Willy Tarreau49b046d2012-08-09 12:11:58 +0200184 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200185 l->state = LI_FULL;
186 }
187}
188
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200189/* Marks a ready listener as limited so that we only try to re-enable it when
190 * resources are free again. It will be queued into the specified queue.
191 */
192void limit_listener(struct listener *l, struct list *list)
193{
194 if (l->state == LI_READY) {
195 LIST_ADDQ(list, &l->wait_queue);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200196 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200197 l->state = LI_LIMITED;
198 }
199}
200
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100201/* This function adds all of the protocol's listener's file descriptors to the
202 * polling lists when they are in the LI_LISTEN state. It is intended to be
203 * used as a protocol's generic enable_all() primitive, for use after the
204 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
205 * their number of connections. It always returns ERR_NONE.
206 */
207int enable_all_listeners(struct protocol *proto)
208{
209 struct listener *listener;
210
211 list_for_each_entry(listener, &proto->listeners, proto_list)
212 enable_listener(listener);
213 return ERR_NONE;
214}
215
216/* This function removes all of the protocol's listener's file descriptors from
217 * the polling lists when they are in the LI_READY or LI_FULL states. It is
218 * intended to be used as a protocol's generic disable_all() primitive. It puts
219 * the listeners into LI_LISTEN, and always returns ERR_NONE.
220 */
221int disable_all_listeners(struct protocol *proto)
222{
223 struct listener *listener;
224
225 list_for_each_entry(listener, &proto->listeners, proto_list)
226 disable_listener(listener);
227 return ERR_NONE;
228}
229
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200230/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
231void dequeue_all_listeners(struct list *list)
232{
233 struct listener *listener, *l_back;
234
235 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
236 /* This cannot fail because the listeners are by definition in
237 * the LI_LIMITED state. The function also removes the entry
238 * from the queue.
239 */
240 resume_listener(listener);
241 }
242}
243
Willy Tarreaub648d632007-10-28 22:13:50 +0100244/* This function closes the listening socket for the specified listener,
245 * provided that it's already in a listening state. The listener enters the
246 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
247 * to be used as a generic function for standard protocols.
248 */
249int unbind_listener(struct listener *listener)
250{
251 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200252 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100253
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200254 if (listener->state == LI_LIMITED)
255 LIST_DEL(&listener->wait_queue);
256
Willy Tarreaube58c382011-07-24 18:28:10 +0200257 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100258 fd_delete(listener->fd);
Willy Tarreau39447b62014-07-07 18:24:48 +0200259 listener->fd = -1;
Willy Tarreaub648d632007-10-28 22:13:50 +0100260 listener->state = LI_ASSIGNED;
261 }
262 return ERR_NONE;
263}
264
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100265/* This function closes all listening sockets bound to the protocol <proto>,
266 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
267 * detach them from the protocol. It always returns ERR_NONE.
268 */
269int unbind_all_listeners(struct protocol *proto)
270{
271 struct listener *listener;
272
273 list_for_each_entry(listener, &proto->listeners, proto_list)
274 unbind_listener(listener);
275 return ERR_NONE;
276}
277
Willy Tarreau1a64d162007-10-28 22:26:05 +0100278/* Delete a listener from its protocol's list of listeners. The listener's
279 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
280 * number of listeners is updated. Note that the listener must have previously
281 * been unbound. This is the generic function to use to remove a listener.
282 */
283void delete_listener(struct listener *listener)
284{
285 if (listener->state != LI_ASSIGNED)
286 return;
287 listener->state = LI_INIT;
288 LIST_DEL(&listener->proto_list);
289 listener->proto->nb_listeners--;
290}
291
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200292/* This function is called on a read event from a listening socket, corresponding
293 * to an accept. It tries to accept as many connections as possible, and for each
294 * calls the listener's accept handler (generally the frontend's accept handler).
295 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200296void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200297{
298 struct listener *l = fdtab[fd].owner;
299 struct proxy *p = l->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100300 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubb660302014-05-07 19:47:02 +0200301 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200302 int cfd;
303 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100304#ifdef USE_ACCEPT4
305 static int accept4_broken;
306#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200307
308 if (unlikely(l->nbconn >= l->maxconn)) {
309 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200310 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200311 }
312
Willy Tarreau93e7c002013-10-07 18:51:07 +0200313 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
314 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200315
316 if (unlikely(!max)) {
317 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200318 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200319 goto wait_expire;
Willy Tarreau93e7c002013-10-07 18:51:07 +0200320 }
321
322 if (max_accept > max)
323 max_accept = max;
324 }
325
326 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200327 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
328
329 if (unlikely(!max)) {
330 /* frontend accept rate limit was reached */
Willy Tarreau93e7c002013-10-07 18:51:07 +0200331 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200332 goto wait_expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200333 }
334
335 if (max_accept > max)
336 max_accept = max;
337 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200338#ifdef USE_OPENSSL
339 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
340 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200341
Willy Tarreaue43d5322013-10-07 20:01:52 +0200342 if (unlikely(!max)) {
343 /* frontend accept rate limit was reached */
Willy Tarreaue43d5322013-10-07 20:01:52 +0200344 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
Willy Tarreaubb660302014-05-07 19:47:02 +0200345 goto wait_expire;
Willy Tarreaue43d5322013-10-07 20:01:52 +0200346 }
347
348 if (max_accept > max)
349 max_accept = max;
350 }
351#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200352 if (p && p->fe_sps_lim) {
353 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
354
355 if (unlikely(!max)) {
356 /* frontend accept rate limit was reached */
357 limit_listener(l, &p->listener_queue);
358 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 +0200359 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200360 }
361
362 if (max_accept > max)
363 max_accept = max;
364 }
365
366 /* Note: if we fail to allocate a connection because of configured
367 * limits, we'll schedule a new attempt worst 1 second later in the
368 * worst case. If we fail due to system limits or temporary resource
369 * shortage, we try again 100ms later in the worst case.
370 */
371 while (max_accept--) {
372 struct sockaddr_storage addr;
373 socklen_t laddr = sizeof(addr);
374
375 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
376 limit_listener(l, &global_listener_queue);
377 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200378 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200379 }
380
381 if (unlikely(p && p->feconn >= p->maxconn)) {
382 limit_listener(l, &p->listener_queue);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200383 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200384 }
385
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200386#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100387 /* only call accept4() if it's known to be safe, otherwise
388 * fallback to the legacy accept() + fcntl().
389 */
390 if (unlikely(accept4_broken ||
391 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
392 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
393 (accept4_broken = 1))))
394#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200395 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
396 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100397
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200398 if (unlikely(cfd == -1)) {
399 switch (errno) {
400 case EAGAIN:
Willy Tarreaubb660302014-05-07 19:47:02 +0200401 if (fdtab[fd].ev & FD_POLL_HUP) {
402 /* the listening socket might have been disabled in a shared
403 * process and we're a collateral victim. We'll just pause for
404 * a while in case it comes back. In the mean time, we need to
405 * clear this sticky flag.
406 */
407 fdtab[fd].ev &= ~FD_POLL_HUP;
408 goto transient_error;
409 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100410 fd_cant_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200411 return; /* nothing more to accept */
Willy Tarreaubb660302014-05-07 19:47:02 +0200412 case EINVAL:
413 /* might be trying to accept on a shut fd (eg: soft stop) */
414 goto transient_error;
Willy Tarreaua593ec52014-01-20 21:21:30 +0100415 case EINTR:
416 case ECONNABORTED:
417 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200418 case ENFILE:
419 if (p)
420 send_log(p, LOG_EMERG,
421 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
422 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200423 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200424 case EMFILE:
425 if (p)
426 send_log(p, LOG_EMERG,
427 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
428 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200429 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200430 case ENOBUFS:
431 case ENOMEM:
432 if (p)
433 send_log(p, LOG_EMERG,
434 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
435 p->id, maxfd);
Willy Tarreaubb660302014-05-07 19:47:02 +0200436 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200437 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100438 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100439 goto stop;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200440 }
441 }
442
443 if (unlikely(cfd >= global.maxsock)) {
444 send_log(p, LOG_EMERG,
445 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
446 p->id);
447 close(cfd);
448 limit_listener(l, &global_listener_queue);
449 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200450 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200451 }
452
453 /* increase the per-process number of cumulated connections */
454 if (!(l->options & LI_O_UNLIMITED)) {
455 update_freq_ctr(&global.conn_per_sec, 1);
456 if (global.conn_per_sec.curr_ctr > global.cps_max)
457 global.cps_max = global.conn_per_sec.curr_ctr;
458 actconn++;
459 }
460
461 jobs++;
462 totalconn++;
463 l->nbconn++;
464
465 if (l->counters) {
466 if (l->nbconn > l->counters->conn_max)
467 l->counters->conn_max = l->nbconn;
468 }
469
470 ret = l->accept(l, cfd, &addr);
471 if (unlikely(ret <= 0)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200472 /* The connection was closed by stream_accept(). Either
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200473 * we just have to ignore it (ret == 0) or it's a critical
474 * error due to a resource shortage, and we must stop the
475 * listener (ret < 0).
476 */
477 if (!(l->options & LI_O_UNLIMITED))
478 actconn--;
479 jobs--;
480 l->nbconn--;
481 if (ret == 0) /* successful termination */
482 continue;
483
Willy Tarreaubb660302014-05-07 19:47:02 +0200484 goto transient_error;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200485 }
486
487 if (l->nbconn >= l->maxconn) {
488 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200489 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200490 }
491
Willy Tarreau93e7c002013-10-07 18:51:07 +0200492 /* increase the per-process number of cumulated connections */
493 if (!(l->options & LI_O_UNLIMITED)) {
494 update_freq_ctr(&global.sess_per_sec, 1);
495 if (global.sess_per_sec.curr_ctr > global.sps_max)
496 global.sps_max = global.sess_per_sec.curr_ctr;
497 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200498#ifdef USE_OPENSSL
499 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
500
501 update_freq_ctr(&global.ssl_per_sec, 1);
502 if (global.ssl_per_sec.curr_ctr > global.ssl_max)
503 global.ssl_max = global.ssl_per_sec.curr_ctr;
504 }
505#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200506
Willy Tarreauaece46a2012-07-06 12:25:58 +0200507 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200508
Willy Tarreauaece46a2012-07-06 12:25:58 +0200509 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100510 stop:
511 fd_done_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200512 return;
Willy Tarreaubb660302014-05-07 19:47:02 +0200513
514 transient_error:
515 /* pause the listener and try again in 100 ms */
516 expire = tick_add(now_ms, 100);
517
518 wait_expire:
519 limit_listener(l, &global_listener_queue);
520 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
521 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200522}
523
Willy Tarreau26982662012-09-12 23:17:10 +0200524/*
525 * Registers the bind keyword list <kwl> as a list of valid keywords for next
526 * parsing sessions.
527 */
528void bind_register_keywords(struct bind_kw_list *kwl)
529{
530 LIST_ADDQ(&bind_keywords.list, &kwl->list);
531}
532
533/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
534 * keyword is found with a NULL ->parse() function, then an attempt is made to
535 * find one with a valid ->parse() function. This way it is possible to declare
536 * platform-dependant, known keywords as NULL, then only declare them as valid
537 * if some options are met. Note that if the requested keyword contains an
538 * opening parenthesis, everything from this point is ignored.
539 */
540struct bind_kw *bind_find_kw(const char *kw)
541{
542 int index;
543 const char *kwend;
544 struct bind_kw_list *kwl;
545 struct bind_kw *ret = NULL;
546
547 kwend = strchr(kw, '(');
548 if (!kwend)
549 kwend = kw + strlen(kw);
550
551 list_for_each_entry(kwl, &bind_keywords.list, list) {
552 for (index = 0; kwl->kw[index].kw != NULL; index++) {
553 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
554 kwl->kw[index].kw[kwend-kw] == 0) {
555 if (kwl->kw[index].parse)
556 return &kwl->kw[index]; /* found it !*/
557 else
558 ret = &kwl->kw[index]; /* may be OK */
559 }
560 }
561 }
562 return ret;
563}
564
Willy Tarreau8638f482012-09-18 18:01:17 +0200565/* Dumps all registered "bind" keywords to the <out> string pointer. The
566 * unsupported keywords are only dumped if their supported form was not
567 * found.
568 */
569void bind_dump_kws(char **out)
570{
571 struct bind_kw_list *kwl;
572 int index;
573
574 *out = NULL;
575 list_for_each_entry(kwl, &bind_keywords.list, list) {
576 for (index = 0; kwl->kw[index].kw != NULL; index++) {
577 if (kwl->kw[index].parse ||
578 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200579 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
580 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200581 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200582 kwl->kw[index].skip ? " <arg>" : "",
583 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200584 }
585 }
586 }
587}
588
Willy Tarreau645513a2010-05-24 20:55:15 +0200589/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100590/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200591/************************************************************************/
592
Willy Tarreaua5e37562011-12-16 17:06:15 +0100593/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200594static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200595smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +0200596{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200597 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200598 smp->data.u.sint = smp->sess->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200599 return 1;
600}
601
Willy Tarreaua5e37562011-12-16 17:06:15 +0100602/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200603static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200604smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +0200605{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200606 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200607 smp->data.u.sint = smp->sess->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200608 return 1;
609}
610
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200611/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200612static 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 +0200613{
614 struct listener *l;
615
Willy Tarreau4348fad2012-09-20 16:48:07 +0200616 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200617 l->options |= LI_O_ACC_PROXY;
618
619 return 0;
620}
621
622/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200623static 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 +0200624{
625 struct listener *l;
626 int val;
627
628 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200629 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200630 return ERR_ALERT | ERR_FATAL;
631 }
632
633 val = atol(args[cur_arg + 1]);
634 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200635 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200636 return ERR_ALERT | ERR_FATAL;
637 }
638
Willy Tarreau4348fad2012-09-20 16:48:07 +0200639 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200640 l->backlog = val;
641
642 return 0;
643}
644
645/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200646static 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 +0200647{
648 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200649 struct listener *l, *new;
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100650 char *error;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200651
Willy Tarreau4348fad2012-09-20 16:48:07 +0200652 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200653 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200654 return ERR_ALERT | ERR_FATAL;
655 }
656
657 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200658 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200659 return ERR_ALERT | ERR_FATAL;
660 }
661
Willy Tarreau4348fad2012-09-20 16:48:07 +0200662 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
Thierry Fourniere7fe8eb2016-02-26 08:45:58 +0100663 new->luid = strtol(args[cur_arg + 1], &error, 10);
664 if (*error != '\0') {
665 memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]);
666 return ERR_ALERT | ERR_FATAL;
667 }
Willy Tarreau4348fad2012-09-20 16:48:07 +0200668 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200669
Willy Tarreau4348fad2012-09-20 16:48:07 +0200670 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200671 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200672 return ERR_ALERT | ERR_FATAL;
673 }
674
Willy Tarreau4348fad2012-09-20 16:48:07 +0200675 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200676 if (node) {
677 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200678 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
679 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
680 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200681 return ERR_ALERT | ERR_FATAL;
682 }
683
Willy Tarreau4348fad2012-09-20 16:48:07 +0200684 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200685 return 0;
686}
687
688/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200689static 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 +0200690{
691 struct listener *l;
692 int val;
693
694 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200695 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200696 return ERR_ALERT | ERR_FATAL;
697 }
698
699 val = atol(args[cur_arg + 1]);
700 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200701 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200702 return ERR_ALERT | ERR_FATAL;
703 }
704
Willy Tarreau4348fad2012-09-20 16:48:07 +0200705 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200706 l->maxconn = val;
707
708 return 0;
709}
710
711/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200712static 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 +0200713{
714 struct listener *l;
715
716 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200717 memprintf(err, "'%s' : missing name", args[cur_arg]);
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->name = strdup(args[cur_arg + 1]);
723
724 return 0;
725}
726
727/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200728static 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 +0200729{
730 struct listener *l;
731 int val;
732
733 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200734 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200735 return ERR_ALERT | ERR_FATAL;
736 }
737
738 val = atol(args[cur_arg + 1]);
739 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200740 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200741 return ERR_ALERT | ERR_FATAL;
742 }
743
Willy Tarreau4348fad2012-09-20 16:48:07 +0200744 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200745 l->nice = val;
746
747 return 0;
748}
749
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200750/* parse the "process" bind keyword */
751static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
752{
753 unsigned long set = 0;
754 unsigned int low, high;
755
756 if (strcmp(args[cur_arg + 1], "all") == 0) {
757 set = 0;
758 }
759 else if (strcmp(args[cur_arg + 1], "odd") == 0) {
760 set |= ~0UL/3UL; /* 0x555....555 */
761 }
762 else if (strcmp(args[cur_arg + 1], "even") == 0) {
763 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
764 }
765 else if (isdigit((int)*args[cur_arg + 1])) {
766 char *dash = strchr(args[cur_arg + 1], '-');
767
768 low = high = str2uic(args[cur_arg + 1]);
769 if (dash)
770 high = str2uic(dash + 1);
771
772 if (high < low) {
773 unsigned int swap = low;
774 low = high;
775 high = swap;
776 }
777
778 if (low < 1 || high > LONGBITS) {
779 memprintf(err, "'%s' : invalid range %d-%d, allowed range is 1..%d", args[cur_arg], low, high, LONGBITS);
780 return ERR_ALERT | ERR_FATAL;
781 }
782 while (low <= high)
783 set |= 1UL << (low++ - 1);
784 }
785 else {
786 memprintf(err, "'%s' expects 'all', 'odd', 'even', or a process range with numbers from 1 to %d.", args[cur_arg], LONGBITS);
787 return ERR_ALERT | ERR_FATAL;
788 }
789
790 conf->bind_proc = set;
791 return 0;
792}
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200793
Willy Tarreau61612d42012-04-19 18:42:05 +0200794/* Note: must not be declared <const> as its list will be overwritten.
795 * Please take care of keeping this list alphabetically sorted.
796 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200797static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200798 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
799 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100800 { /* END */ },
801}};
802
803/* Note: must not be declared <const> as its list will be overwritten.
804 * Please take care of keeping this list alphabetically sorted.
805 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200806static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100807 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +0200808}};
809
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200810/* Note: must not be declared <const> as its list will be overwritten.
811 * Please take care of keeping this list alphabetically sorted, doing so helps
812 * all code contributors.
813 * Optional keywords are also declared with a NULL ->parse() function so that
814 * the config parser can report an appropriate error when a known keyword was
815 * not enabled.
816 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200817static struct bind_kw_list bind_kws = { "ALL", { }, {
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200818 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
819 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
820 { "id", bind_parse_id, 1 }, /* set id of listening socket */
821 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
822 { "name", bind_parse_name, 1 }, /* set name of listening socket */
823 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200824 { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100825 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200826}};
827
Willy Tarreau645513a2010-05-24 20:55:15 +0200828__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +0200829static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +0200830{
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100831 sample_register_fetches(&smp_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200832 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200833 bind_register_keywords(&bind_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200834}
835
836/*
837 * Local variables:
838 * c-indent-level: 8
839 * c-basic-offset: 8
840 * End:
841 */