blob: 7ab1a87d14812c0bbd34c7e60d173152be648506 [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 Tarreaubbebbbf2012-05-07 21:22:09 +020014#include <errno.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020015#include <stdio.h>
16#include <string.h>
Willy Tarreau95ccdde2014-02-01 09:28:36 +010017#include <unistd.h>
18#include <fcntl.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020019
Willy Tarreau1bc4aab2012-10-08 20:11:03 +020020#include <common/accept4.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020021#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010022#include <common/errors.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020023#include <common/mini-clist.h>
24#include <common/standard.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020025#include <common/time.h>
26
27#include <types/global.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020028#include <types/protocol.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020029
Willy Tarreau645513a2010-05-24 20:55:15 +020030#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010031#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020032#include <proto/freq_ctr.h>
33#include <proto/log.h>
Willy Tarreau0ccb7442013-01-07 22:54:17 +010034#include <proto/sample.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020035#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010036
Willy Tarreau26982662012-09-12 23:17:10 +020037/* List head of all known bind keywords */
38static struct bind_kw_list bind_keywords = {
39 .list = LIST_HEAD_INIT(bind_keywords.list)
40};
41
Willy Tarreaudabf2e22007-10-28 21:59:24 +010042/* This function adds the specified listener's file descriptor to the polling
43 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
44 * LI_FULL state depending on its number of connections.
45 */
46void enable_listener(struct listener *listener)
47{
48 if (listener->state == LI_LISTEN) {
49 if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020050 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010051 listener->state = LI_READY;
52 } else {
53 listener->state = LI_FULL;
54 }
55 }
56}
57
58/* This function removes the specified listener's file descriptor from the
59 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
60 * enters LI_LISTEN.
61 */
62void disable_listener(struct listener *listener)
63{
64 if (listener->state < LI_READY)
65 return;
66 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +020067 fd_stop_recv(listener->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020068 if (listener->state == LI_LIMITED)
69 LIST_DEL(&listener->wait_queue);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010070 listener->state = LI_LISTEN;
71}
72
Willy Tarreaube58c382011-07-24 18:28:10 +020073/* This function tries to temporarily disable a listener, depending on the OS
74 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
75 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
76 * closes upon SHUT_WR and refuses to rebind. So a common validation path
77 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
78 * is disabled. It normally returns non-zero, unless an error is reported.
79 */
80int pause_listener(struct listener *l)
81{
82 if (l->state <= LI_PAUSED)
83 return 1;
84
Willy Tarreaub3fb60b2012-10-04 08:56:31 +020085 if (l->proto->sock_prot == IPPROTO_TCP) {
86 if (shutdown(l->fd, SHUT_WR) != 0)
87 return 0; /* Solaris dies here */
Willy Tarreaube58c382011-07-24 18:28:10 +020088
Willy Tarreaub3fb60b2012-10-04 08:56:31 +020089 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
90 return 0; /* OpenBSD dies here */
Willy Tarreaube58c382011-07-24 18:28:10 +020091
Willy Tarreaub3fb60b2012-10-04 08:56:31 +020092 if (shutdown(l->fd, SHUT_RD) != 0)
93 return 0; /* should always be OK */
94 }
Willy Tarreaube58c382011-07-24 18:28:10 +020095
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020096 if (l->state == LI_LIMITED)
97 LIST_DEL(&l->wait_queue);
98
Willy Tarreau49b046d2012-08-09 12:11:58 +020099 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200100 l->state = LI_PAUSED;
101 return 1;
102}
103
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200104/* This function tries to resume a temporarily disabled listener. Paused, full,
105 * limited and disabled listeners are handled, which means that this function
106 * may replace enable_listener(). The resulting state will either be LI_READY
107 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreaube58c382011-07-24 18:28:10 +0200108 */
109int resume_listener(struct listener *l)
110{
111 if (l->state < LI_PAUSED)
112 return 0;
113
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200114 if (l->proto->sock_prot == IPPROTO_TCP &&
115 l->state == LI_PAUSED &&
Willy Tarreaube58c382011-07-24 18:28:10 +0200116 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
117 return 0;
118
119 if (l->state == LI_READY)
120 return 1;
121
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200122 if (l->state == LI_LIMITED)
123 LIST_DEL(&l->wait_queue);
124
Willy Tarreaube58c382011-07-24 18:28:10 +0200125 if (l->nbconn >= l->maxconn) {
126 l->state = LI_FULL;
127 return 1;
128 }
129
Willy Tarreau49b046d2012-08-09 12:11:58 +0200130 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200131 l->state = LI_READY;
132 return 1;
133}
134
Willy Tarreau62793712011-07-24 19:23:38 +0200135/* Marks a ready listener as full so that the session code tries to re-enable
136 * it upon next close() using resume_listener().
137 */
138void listener_full(struct listener *l)
139{
140 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200141 if (l->state == LI_LIMITED)
142 LIST_DEL(&l->wait_queue);
143
Willy Tarreau49b046d2012-08-09 12:11:58 +0200144 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200145 l->state = LI_FULL;
146 }
147}
148
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200149/* Marks a ready listener as limited so that we only try to re-enable it when
150 * resources are free again. It will be queued into the specified queue.
151 */
152void limit_listener(struct listener *l, struct list *list)
153{
154 if (l->state == LI_READY) {
155 LIST_ADDQ(list, &l->wait_queue);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200156 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200157 l->state = LI_LIMITED;
158 }
159}
160
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100161/* This function adds all of the protocol's listener's file descriptors to the
162 * polling lists when they are in the LI_LISTEN state. It is intended to be
163 * used as a protocol's generic enable_all() primitive, for use after the
164 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
165 * their number of connections. It always returns ERR_NONE.
166 */
167int enable_all_listeners(struct protocol *proto)
168{
169 struct listener *listener;
170
171 list_for_each_entry(listener, &proto->listeners, proto_list)
172 enable_listener(listener);
173 return ERR_NONE;
174}
175
176/* This function removes all of the protocol's listener's file descriptors from
177 * the polling lists when they are in the LI_READY or LI_FULL states. It is
178 * intended to be used as a protocol's generic disable_all() primitive. It puts
179 * the listeners into LI_LISTEN, and always returns ERR_NONE.
180 */
181int disable_all_listeners(struct protocol *proto)
182{
183 struct listener *listener;
184
185 list_for_each_entry(listener, &proto->listeners, proto_list)
186 disable_listener(listener);
187 return ERR_NONE;
188}
189
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200190/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
191void dequeue_all_listeners(struct list *list)
192{
193 struct listener *listener, *l_back;
194
195 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
196 /* This cannot fail because the listeners are by definition in
197 * the LI_LIMITED state. The function also removes the entry
198 * from the queue.
199 */
200 resume_listener(listener);
201 }
202}
203
Willy Tarreaub648d632007-10-28 22:13:50 +0100204/* This function closes the listening socket for the specified listener,
205 * provided that it's already in a listening state. The listener enters the
206 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
207 * to be used as a generic function for standard protocols.
208 */
209int unbind_listener(struct listener *listener)
210{
211 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200212 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100213
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200214 if (listener->state == LI_LIMITED)
215 LIST_DEL(&listener->wait_queue);
216
Willy Tarreaube58c382011-07-24 18:28:10 +0200217 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100218 fd_delete(listener->fd);
219 listener->state = LI_ASSIGNED;
220 }
221 return ERR_NONE;
222}
223
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100224/* This function closes all listening sockets bound to the protocol <proto>,
225 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
226 * detach them from the protocol. It always returns ERR_NONE.
227 */
228int unbind_all_listeners(struct protocol *proto)
229{
230 struct listener *listener;
231
232 list_for_each_entry(listener, &proto->listeners, proto_list)
233 unbind_listener(listener);
234 return ERR_NONE;
235}
236
Willy Tarreau1a64d162007-10-28 22:26:05 +0100237/* Delete a listener from its protocol's list of listeners. The listener's
238 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
239 * number of listeners is updated. Note that the listener must have previously
240 * been unbound. This is the generic function to use to remove a listener.
241 */
242void delete_listener(struct listener *listener)
243{
244 if (listener->state != LI_ASSIGNED)
245 return;
246 listener->state = LI_INIT;
247 LIST_DEL(&listener->proto_list);
248 listener->proto->nb_listeners--;
249}
250
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200251/* This function is called on a read event from a listening socket, corresponding
252 * to an accept. It tries to accept as many connections as possible, and for each
253 * calls the listener's accept handler (generally the frontend's accept handler).
254 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200255void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200256{
257 struct listener *l = fdtab[fd].owner;
258 struct proxy *p = l->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100259 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200260 int cfd;
261 int ret;
Willy Tarreau818dca52014-01-31 19:40:19 +0100262#ifdef USE_ACCEPT4
263 static int accept4_broken;
264#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200265
266 if (unlikely(l->nbconn >= l->maxconn)) {
267 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200268 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200269 }
270
Willy Tarreau93e7c002013-10-07 18:51:07 +0200271 if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) {
272 int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0);
273 int expire;
274
275 if (unlikely(!max)) {
276 /* frontend accept rate limit was reached */
277 limit_listener(l, &global_listener_queue);
278 expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0));
279 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
280 return;
281 }
282
283 if (max_accept > max)
284 max_accept = max;
285 }
286
287 if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) {
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200288 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200289 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200290
291 if (unlikely(!max)) {
292 /* frontend accept rate limit was reached */
293 limit_listener(l, &global_listener_queue);
Willy Tarreau93e7c002013-10-07 18:51:07 +0200294 expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0));
295 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
Willy Tarreauafad0e02012-08-09 14:45:22 +0200296 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200297 }
298
299 if (max_accept > max)
300 max_accept = max;
301 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200302#ifdef USE_OPENSSL
303 if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) {
304 int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0);
305 int expire;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200306
Willy Tarreaue43d5322013-10-07 20:01:52 +0200307 if (unlikely(!max)) {
308 /* frontend accept rate limit was reached */
309 limit_listener(l, &global_listener_queue);
310 expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0));
311 task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire));
312 return;
313 }
314
315 if (max_accept > max)
316 max_accept = max;
317 }
318#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200319 if (p && p->fe_sps_lim) {
320 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
321
322 if (unlikely(!max)) {
323 /* frontend accept rate limit was reached */
324 limit_listener(l, &p->listener_queue);
325 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 +0200326 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200327 }
328
329 if (max_accept > max)
330 max_accept = max;
331 }
332
333 /* Note: if we fail to allocate a connection because of configured
334 * limits, we'll schedule a new attempt worst 1 second later in the
335 * worst case. If we fail due to system limits or temporary resource
336 * shortage, we try again 100ms later in the worst case.
337 */
338 while (max_accept--) {
339 struct sockaddr_storage addr;
340 socklen_t laddr = sizeof(addr);
341
342 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
343 limit_listener(l, &global_listener_queue);
344 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200345 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200346 }
347
348 if (unlikely(p && p->feconn >= p->maxconn)) {
349 limit_listener(l, &p->listener_queue);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200350 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200351 }
352
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200353#ifdef USE_ACCEPT4
Willy Tarreau818dca52014-01-31 19:40:19 +0100354 /* only call accept4() if it's known to be safe, otherwise
355 * fallback to the legacy accept() + fcntl().
356 */
357 if (unlikely(accept4_broken ||
358 ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 &&
359 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
360 (accept4_broken = 1))))
361#endif
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200362 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
363 fcntl(cfd, F_SETFL, O_NONBLOCK);
Willy Tarreau818dca52014-01-31 19:40:19 +0100364
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200365 if (unlikely(cfd == -1)) {
366 switch (errno) {
367 case EAGAIN:
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100368 fd_cant_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200369 return; /* nothing more to accept */
Willy Tarreaua593ec52014-01-20 21:21:30 +0100370 case EINTR:
371 case ECONNABORTED:
372 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200373 case ENFILE:
374 if (p)
375 send_log(p, LOG_EMERG,
376 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
377 p->id, maxfd);
378 limit_listener(l, &global_listener_queue);
379 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200380 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200381 case EMFILE:
382 if (p)
383 send_log(p, LOG_EMERG,
384 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
385 p->id, maxfd);
386 limit_listener(l, &global_listener_queue);
387 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200388 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200389 case ENOBUFS:
390 case ENOMEM:
391 if (p)
392 send_log(p, LOG_EMERG,
393 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
394 p->id, maxfd);
395 limit_listener(l, &global_listener_queue);
396 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200397 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200398 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100399 /* unexpected result, let's give up and let other tasks run */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100400 goto stop;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200401 }
402 }
403
404 if (unlikely(cfd >= global.maxsock)) {
405 send_log(p, LOG_EMERG,
406 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
407 p->id);
408 close(cfd);
409 limit_listener(l, &global_listener_queue);
410 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200411 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200412 }
413
414 /* increase the per-process number of cumulated connections */
415 if (!(l->options & LI_O_UNLIMITED)) {
416 update_freq_ctr(&global.conn_per_sec, 1);
417 if (global.conn_per_sec.curr_ctr > global.cps_max)
418 global.cps_max = global.conn_per_sec.curr_ctr;
419 actconn++;
420 }
421
422 jobs++;
423 totalconn++;
424 l->nbconn++;
425
426 if (l->counters) {
427 if (l->nbconn > l->counters->conn_max)
428 l->counters->conn_max = l->nbconn;
429 }
430
431 ret = l->accept(l, cfd, &addr);
432 if (unlikely(ret <= 0)) {
433 /* The connection was closed by session_accept(). Either
434 * we just have to ignore it (ret == 0) or it's a critical
435 * error due to a resource shortage, and we must stop the
436 * listener (ret < 0).
437 */
438 if (!(l->options & LI_O_UNLIMITED))
439 actconn--;
440 jobs--;
441 l->nbconn--;
442 if (ret == 0) /* successful termination */
443 continue;
444
445 limit_listener(l, &global_listener_queue);
446 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200447 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200448 }
449
450 if (l->nbconn >= l->maxconn) {
451 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200452 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200453 }
454
Willy Tarreau93e7c002013-10-07 18:51:07 +0200455 /* increase the per-process number of cumulated connections */
456 if (!(l->options & LI_O_UNLIMITED)) {
457 update_freq_ctr(&global.sess_per_sec, 1);
458 if (global.sess_per_sec.curr_ctr > global.sps_max)
459 global.sps_max = global.sess_per_sec.curr_ctr;
460 }
Willy Tarreaue43d5322013-10-07 20:01:52 +0200461#ifdef USE_OPENSSL
462 if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) {
463
464 update_freq_ctr(&global.ssl_per_sec, 1);
465 if (global.ssl_per_sec.curr_ctr > global.ssl_max)
466 global.ssl_max = global.ssl_per_sec.curr_ctr;
467 }
468#endif
Willy Tarreau93e7c002013-10-07 18:51:07 +0200469
Willy Tarreauaece46a2012-07-06 12:25:58 +0200470 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200471
Willy Tarreauaece46a2012-07-06 12:25:58 +0200472 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100473 stop:
474 fd_done_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200475 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200476}
477
Willy Tarreau26982662012-09-12 23:17:10 +0200478/*
479 * Registers the bind keyword list <kwl> as a list of valid keywords for next
480 * parsing sessions.
481 */
482void bind_register_keywords(struct bind_kw_list *kwl)
483{
484 LIST_ADDQ(&bind_keywords.list, &kwl->list);
485}
486
487/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
488 * keyword is found with a NULL ->parse() function, then an attempt is made to
489 * find one with a valid ->parse() function. This way it is possible to declare
490 * platform-dependant, known keywords as NULL, then only declare them as valid
491 * if some options are met. Note that if the requested keyword contains an
492 * opening parenthesis, everything from this point is ignored.
493 */
494struct bind_kw *bind_find_kw(const char *kw)
495{
496 int index;
497 const char *kwend;
498 struct bind_kw_list *kwl;
499 struct bind_kw *ret = NULL;
500
501 kwend = strchr(kw, '(');
502 if (!kwend)
503 kwend = kw + strlen(kw);
504
505 list_for_each_entry(kwl, &bind_keywords.list, list) {
506 for (index = 0; kwl->kw[index].kw != NULL; index++) {
507 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
508 kwl->kw[index].kw[kwend-kw] == 0) {
509 if (kwl->kw[index].parse)
510 return &kwl->kw[index]; /* found it !*/
511 else
512 ret = &kwl->kw[index]; /* may be OK */
513 }
514 }
515 }
516 return ret;
517}
518
Willy Tarreau8638f482012-09-18 18:01:17 +0200519/* Dumps all registered "bind" keywords to the <out> string pointer. The
520 * unsupported keywords are only dumped if their supported form was not
521 * found.
522 */
523void bind_dump_kws(char **out)
524{
525 struct bind_kw_list *kwl;
526 int index;
527
528 *out = NULL;
529 list_for_each_entry(kwl, &bind_keywords.list, list) {
530 for (index = 0; kwl->kw[index].kw != NULL; index++) {
531 if (kwl->kw[index].parse ||
532 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200533 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
534 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200535 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200536 kwl->kw[index].skip ? " <arg>" : "",
537 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200538 }
539 }
540 }
541}
542
Willy Tarreau645513a2010-05-24 20:55:15 +0200543/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100544/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200545/************************************************************************/
546
Willy Tarreaua5e37562011-12-16 17:06:15 +0100547/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200548static int
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100549smp_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200550 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +0200551{
Willy Tarreauf853c462012-04-23 18:53:56 +0200552 smp->type = SMP_T_UINT;
553 smp->data.uint = l4->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200554 return 1;
555}
556
Willy Tarreaua5e37562011-12-16 17:06:15 +0100557/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200558static int
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100559smp_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200560 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +0200561{
Willy Tarreauf853c462012-04-23 18:53:56 +0200562 smp->type = SMP_T_UINT;
563 smp->data.uint = l4->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200564 return 1;
565}
566
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200567/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200568static 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 +0200569{
570 struct listener *l;
571
Willy Tarreau4348fad2012-09-20 16:48:07 +0200572 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200573 l->options |= LI_O_ACC_PROXY;
574
575 return 0;
576}
577
578/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200579static 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 +0200580{
581 struct listener *l;
582 int val;
583
584 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200585 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200586 return ERR_ALERT | ERR_FATAL;
587 }
588
589 val = atol(args[cur_arg + 1]);
590 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200591 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200592 return ERR_ALERT | ERR_FATAL;
593 }
594
Willy Tarreau4348fad2012-09-20 16:48:07 +0200595 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200596 l->backlog = val;
597
598 return 0;
599}
600
601/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200602static 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 +0200603{
604 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200605 struct listener *l, *new;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200606
Willy Tarreau4348fad2012-09-20 16:48:07 +0200607 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200608 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200609 return ERR_ALERT | ERR_FATAL;
610 }
611
612 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200613 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200614 return ERR_ALERT | ERR_FATAL;
615 }
616
Willy Tarreau4348fad2012-09-20 16:48:07 +0200617 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
618 new->luid = atol(args[cur_arg + 1]);
619 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200620
Willy Tarreau4348fad2012-09-20 16:48:07 +0200621 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200622 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200623 return ERR_ALERT | ERR_FATAL;
624 }
625
Willy Tarreau4348fad2012-09-20 16:48:07 +0200626 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200627 if (node) {
628 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200629 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
630 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
631 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200632 return ERR_ALERT | ERR_FATAL;
633 }
634
Willy Tarreau4348fad2012-09-20 16:48:07 +0200635 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200636 return 0;
637}
638
639/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200640static 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 +0200641{
642 struct listener *l;
643 int val;
644
645 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200646 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200647 return ERR_ALERT | ERR_FATAL;
648 }
649
650 val = atol(args[cur_arg + 1]);
651 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200652 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200653 return ERR_ALERT | ERR_FATAL;
654 }
655
Willy Tarreau4348fad2012-09-20 16:48:07 +0200656 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200657 l->maxconn = val;
658
659 return 0;
660}
661
662/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200663static 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 +0200664{
665 struct listener *l;
666
667 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200668 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200669 return ERR_ALERT | ERR_FATAL;
670 }
671
Willy Tarreau4348fad2012-09-20 16:48:07 +0200672 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200673 l->name = strdup(args[cur_arg + 1]);
674
675 return 0;
676}
677
678/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200679static 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 +0200680{
681 struct listener *l;
682 int val;
683
684 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200685 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200686 return ERR_ALERT | ERR_FATAL;
687 }
688
689 val = atol(args[cur_arg + 1]);
690 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200691 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200692 return ERR_ALERT | ERR_FATAL;
693 }
694
Willy Tarreau4348fad2012-09-20 16:48:07 +0200695 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200696 l->nice = val;
697
698 return 0;
699}
700
701
Willy Tarreau61612d42012-04-19 18:42:05 +0200702/* Note: must not be declared <const> as its list will be overwritten.
703 * Please take care of keeping this list alphabetically sorted.
704 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200705static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100706 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
707 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
708 { /* END */ },
709}};
710
711/* Note: must not be declared <const> as its list will be overwritten.
712 * Please take care of keeping this list alphabetically sorted.
713 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200714static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100715 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +0200716}};
717
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200718/* Note: must not be declared <const> as its list will be overwritten.
719 * Please take care of keeping this list alphabetically sorted, doing so helps
720 * all code contributors.
721 * Optional keywords are also declared with a NULL ->parse() function so that
722 * the config parser can report an appropriate error when a known keyword was
723 * not enabled.
724 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200725static struct bind_kw_list bind_kws = { "ALL", { }, {
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200726 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
727 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
728 { "id", bind_parse_id, 1 }, /* set id of listening socket */
729 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
730 { "name", bind_parse_name, 1 }, /* set name of listening socket */
731 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100732 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200733}};
734
Willy Tarreau645513a2010-05-24 20:55:15 +0200735__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +0200736static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +0200737{
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100738 sample_register_fetches(&smp_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200739 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200740 bind_register_keywords(&bind_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200741}
742
743/*
744 * Local variables:
745 * c-indent-level: 8
746 * c-basic-offset: 8
747 * End:
748 */