blob: d704b42448dd4ed999634333b7d5cd0630bd41f1 [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 Tarreaud1d54542012-09-12 22:58:11 +02004 * Copyright 2000-2012 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 Tarreaubbebbbf2012-05-07 21:22:09 +020013#include <errno.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010018#include <common/errors.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020019#include <common/mini-clist.h>
20#include <common/standard.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020021#include <common/time.h>
22
23#include <types/global.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020024#include <types/protocol.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020025
Willy Tarreau645513a2010-05-24 20:55:15 +020026#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010027#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020028#include <proto/freq_ctr.h>
29#include <proto/log.h>
30#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010031
Willy Tarreau26982662012-09-12 23:17:10 +020032/* List head of all known bind keywords */
33static struct bind_kw_list bind_keywords = {
34 .list = LIST_HEAD_INIT(bind_keywords.list)
35};
36
Willy Tarreaudabf2e22007-10-28 21:59:24 +010037/* This function adds the specified listener's file descriptor to the polling
38 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
39 * LI_FULL state depending on its number of connections.
40 */
41void enable_listener(struct listener *listener)
42{
43 if (listener->state == LI_LISTEN) {
44 if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020045 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010046 listener->state = LI_READY;
47 } else {
48 listener->state = LI_FULL;
49 }
50 }
51}
52
53/* This function removes the specified listener's file descriptor from the
54 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
55 * enters LI_LISTEN.
56 */
57void disable_listener(struct listener *listener)
58{
59 if (listener->state < LI_READY)
60 return;
61 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +020062 fd_stop_recv(listener->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020063 if (listener->state == LI_LIMITED)
64 LIST_DEL(&listener->wait_queue);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010065 listener->state = LI_LISTEN;
66}
67
Willy Tarreaube58c382011-07-24 18:28:10 +020068/* This function tries to temporarily disable a listener, depending on the OS
69 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
70 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
71 * closes upon SHUT_WR and refuses to rebind. So a common validation path
72 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
73 * is disabled. It normally returns non-zero, unless an error is reported.
74 */
75int pause_listener(struct listener *l)
76{
77 if (l->state <= LI_PAUSED)
78 return 1;
79
80 if (shutdown(l->fd, SHUT_WR) != 0)
81 return 0; /* Solaris dies here */
82
83 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
84 return 0; /* OpenBSD dies here */
85
86 if (shutdown(l->fd, SHUT_RD) != 0)
87 return 0; /* should always be OK */
88
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020089 if (l->state == LI_LIMITED)
90 LIST_DEL(&l->wait_queue);
91
Willy Tarreau49b046d2012-08-09 12:11:58 +020092 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +020093 l->state = LI_PAUSED;
94 return 1;
95}
96
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020097/* This function tries to resume a temporarily disabled listener. Paused, full,
98 * limited and disabled listeners are handled, which means that this function
99 * may replace enable_listener(). The resulting state will either be LI_READY
100 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreaube58c382011-07-24 18:28:10 +0200101 */
102int resume_listener(struct listener *l)
103{
104 if (l->state < LI_PAUSED)
105 return 0;
106
107 if (l->state == LI_PAUSED &&
108 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
109 return 0;
110
111 if (l->state == LI_READY)
112 return 1;
113
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200114 if (l->state == LI_LIMITED)
115 LIST_DEL(&l->wait_queue);
116
Willy Tarreaube58c382011-07-24 18:28:10 +0200117 if (l->nbconn >= l->maxconn) {
118 l->state = LI_FULL;
119 return 1;
120 }
121
Willy Tarreau49b046d2012-08-09 12:11:58 +0200122 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200123 l->state = LI_READY;
124 return 1;
125}
126
Willy Tarreau62793712011-07-24 19:23:38 +0200127/* Marks a ready listener as full so that the session code tries to re-enable
128 * it upon next close() using resume_listener().
129 */
130void listener_full(struct listener *l)
131{
132 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200133 if (l->state == LI_LIMITED)
134 LIST_DEL(&l->wait_queue);
135
Willy Tarreau49b046d2012-08-09 12:11:58 +0200136 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200137 l->state = LI_FULL;
138 }
139}
140
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200141/* Marks a ready listener as limited so that we only try to re-enable it when
142 * resources are free again. It will be queued into the specified queue.
143 */
144void limit_listener(struct listener *l, struct list *list)
145{
146 if (l->state == LI_READY) {
147 LIST_ADDQ(list, &l->wait_queue);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200148 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200149 l->state = LI_LIMITED;
150 }
151}
152
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100153/* This function adds all of the protocol's listener's file descriptors to the
154 * polling lists when they are in the LI_LISTEN state. It is intended to be
155 * used as a protocol's generic enable_all() primitive, for use after the
156 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
157 * their number of connections. It always returns ERR_NONE.
158 */
159int enable_all_listeners(struct protocol *proto)
160{
161 struct listener *listener;
162
163 list_for_each_entry(listener, &proto->listeners, proto_list)
164 enable_listener(listener);
165 return ERR_NONE;
166}
167
168/* This function removes all of the protocol's listener's file descriptors from
169 * the polling lists when they are in the LI_READY or LI_FULL states. It is
170 * intended to be used as a protocol's generic disable_all() primitive. It puts
171 * the listeners into LI_LISTEN, and always returns ERR_NONE.
172 */
173int disable_all_listeners(struct protocol *proto)
174{
175 struct listener *listener;
176
177 list_for_each_entry(listener, &proto->listeners, proto_list)
178 disable_listener(listener);
179 return ERR_NONE;
180}
181
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200182/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
183void dequeue_all_listeners(struct list *list)
184{
185 struct listener *listener, *l_back;
186
187 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
188 /* This cannot fail because the listeners are by definition in
189 * the LI_LIMITED state. The function also removes the entry
190 * from the queue.
191 */
192 resume_listener(listener);
193 }
194}
195
Willy Tarreaub648d632007-10-28 22:13:50 +0100196/* This function closes the listening socket for the specified listener,
197 * provided that it's already in a listening state. The listener enters the
198 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
199 * to be used as a generic function for standard protocols.
200 */
201int unbind_listener(struct listener *listener)
202{
203 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200204 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100205
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200206 if (listener->state == LI_LIMITED)
207 LIST_DEL(&listener->wait_queue);
208
Willy Tarreaube58c382011-07-24 18:28:10 +0200209 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100210 fd_delete(listener->fd);
211 listener->state = LI_ASSIGNED;
212 }
213 return ERR_NONE;
214}
215
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100216/* This function closes all listening sockets bound to the protocol <proto>,
217 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
218 * detach them from the protocol. It always returns ERR_NONE.
219 */
220int unbind_all_listeners(struct protocol *proto)
221{
222 struct listener *listener;
223
224 list_for_each_entry(listener, &proto->listeners, proto_list)
225 unbind_listener(listener);
226 return ERR_NONE;
227}
228
Willy Tarreau1a64d162007-10-28 22:26:05 +0100229/* Delete a listener from its protocol's list of listeners. The listener's
230 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
231 * number of listeners is updated. Note that the listener must have previously
232 * been unbound. This is the generic function to use to remove a listener.
233 */
234void delete_listener(struct listener *listener)
235{
236 if (listener->state != LI_ASSIGNED)
237 return;
238 listener->state = LI_INIT;
239 LIST_DEL(&listener->proto_list);
240 listener->proto->nb_listeners--;
241}
242
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200243/* This function is called on a read event from a listening socket, corresponding
244 * to an accept. It tries to accept as many connections as possible, and for each
245 * calls the listener's accept handler (generally the frontend's accept handler).
246 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200247void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200248{
249 struct listener *l = fdtab[fd].owner;
250 struct proxy *p = l->frontend;
251 int max_accept = global.tune.maxaccept;
252 int cfd;
253 int ret;
254
255 if (unlikely(l->nbconn >= l->maxconn)) {
256 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200257 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200258 }
259
260 if (global.cps_lim && !(l->options & LI_O_UNLIMITED)) {
261 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
262
263 if (unlikely(!max)) {
264 /* frontend accept rate limit was reached */
265 limit_listener(l, &global_listener_queue);
266 task_schedule(global_listener_queue_task, tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0)));
Willy Tarreauafad0e02012-08-09 14:45:22 +0200267 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200268 }
269
270 if (max_accept > max)
271 max_accept = max;
272 }
273
274 if (p && p->fe_sps_lim) {
275 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
276
277 if (unlikely(!max)) {
278 /* frontend accept rate limit was reached */
279 limit_listener(l, &p->listener_queue);
280 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 +0200281 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200282 }
283
284 if (max_accept > max)
285 max_accept = max;
286 }
287
288 /* Note: if we fail to allocate a connection because of configured
289 * limits, we'll schedule a new attempt worst 1 second later in the
290 * worst case. If we fail due to system limits or temporary resource
291 * shortage, we try again 100ms later in the worst case.
292 */
293 while (max_accept--) {
294 struct sockaddr_storage addr;
295 socklen_t laddr = sizeof(addr);
296
297 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
298 limit_listener(l, &global_listener_queue);
299 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200300 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200301 }
302
303 if (unlikely(p && p->feconn >= p->maxconn)) {
304 limit_listener(l, &p->listener_queue);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200305 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200306 }
307
308 cfd = accept(fd, (struct sockaddr *)&addr, &laddr);
309 if (unlikely(cfd == -1)) {
310 switch (errno) {
311 case EAGAIN:
312 case EINTR:
313 case ECONNABORTED:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200314 fd_poll_recv(fd);
315 return; /* nothing more to accept */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200316 case ENFILE:
317 if (p)
318 send_log(p, LOG_EMERG,
319 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
320 p->id, maxfd);
321 limit_listener(l, &global_listener_queue);
322 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200323 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200324 case EMFILE:
325 if (p)
326 send_log(p, LOG_EMERG,
327 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
328 p->id, maxfd);
329 limit_listener(l, &global_listener_queue);
330 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200331 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200332 case ENOBUFS:
333 case ENOMEM:
334 if (p)
335 send_log(p, LOG_EMERG,
336 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
337 p->id, maxfd);
338 limit_listener(l, &global_listener_queue);
339 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200340 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200341 default:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200342 /* unexpected result, let's go back to poll */
343 fd_poll_recv(fd);
344 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200345 }
346 }
347
Willy Tarreaufe7f1ea2012-05-20 19:22:25 +0200348 /* if this connection comes from a known monitoring system, we want to ignore
349 * it as soon as possible, which means closing it immediately if it is only a
350 * TCP-based monitoring check.
351 */
352 if (unlikely((l->options & LI_O_CHK_MONNET) &&
353 (p->mode == PR_MODE_TCP) &&
354 addr.ss_family == AF_INET &&
355 (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
356 close(cfd);
357 continue;
358 }
359
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200360 if (unlikely(cfd >= global.maxsock)) {
361 send_log(p, LOG_EMERG,
362 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
363 p->id);
364 close(cfd);
365 limit_listener(l, &global_listener_queue);
366 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200367 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200368 }
369
370 /* increase the per-process number of cumulated connections */
371 if (!(l->options & LI_O_UNLIMITED)) {
372 update_freq_ctr(&global.conn_per_sec, 1);
373 if (global.conn_per_sec.curr_ctr > global.cps_max)
374 global.cps_max = global.conn_per_sec.curr_ctr;
375 actconn++;
376 }
377
378 jobs++;
379 totalconn++;
380 l->nbconn++;
381
382 if (l->counters) {
383 if (l->nbconn > l->counters->conn_max)
384 l->counters->conn_max = l->nbconn;
385 }
386
387 ret = l->accept(l, cfd, &addr);
388 if (unlikely(ret <= 0)) {
389 /* The connection was closed by session_accept(). Either
390 * we just have to ignore it (ret == 0) or it's a critical
391 * error due to a resource shortage, and we must stop the
392 * listener (ret < 0).
393 */
394 if (!(l->options & LI_O_UNLIMITED))
395 actconn--;
396 jobs--;
397 l->nbconn--;
398 if (ret == 0) /* successful termination */
399 continue;
400
401 limit_listener(l, &global_listener_queue);
402 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200403 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200404 }
405
406 if (l->nbconn >= l->maxconn) {
407 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200408 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200409 }
410
Willy Tarreauaece46a2012-07-06 12:25:58 +0200411 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200412
Willy Tarreauaece46a2012-07-06 12:25:58 +0200413 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200414 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200415}
416
Willy Tarreau26982662012-09-12 23:17:10 +0200417/*
418 * Registers the bind keyword list <kwl> as a list of valid keywords for next
419 * parsing sessions.
420 */
421void bind_register_keywords(struct bind_kw_list *kwl)
422{
423 LIST_ADDQ(&bind_keywords.list, &kwl->list);
424}
425
426/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
427 * keyword is found with a NULL ->parse() function, then an attempt is made to
428 * find one with a valid ->parse() function. This way it is possible to declare
429 * platform-dependant, known keywords as NULL, then only declare them as valid
430 * if some options are met. Note that if the requested keyword contains an
431 * opening parenthesis, everything from this point is ignored.
432 */
433struct bind_kw *bind_find_kw(const char *kw)
434{
435 int index;
436 const char *kwend;
437 struct bind_kw_list *kwl;
438 struct bind_kw *ret = NULL;
439
440 kwend = strchr(kw, '(');
441 if (!kwend)
442 kwend = kw + strlen(kw);
443
444 list_for_each_entry(kwl, &bind_keywords.list, list) {
445 for (index = 0; kwl->kw[index].kw != NULL; index++) {
446 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
447 kwl->kw[index].kw[kwend-kw] == 0) {
448 if (kwl->kw[index].parse)
449 return &kwl->kw[index]; /* found it !*/
450 else
451 ret = &kwl->kw[index]; /* may be OK */
452 }
453 }
454 }
455 return ret;
456}
457
Willy Tarreau8638f482012-09-18 18:01:17 +0200458/* Dumps all registered "bind" keywords to the <out> string pointer. The
459 * unsupported keywords are only dumped if their supported form was not
460 * found.
461 */
462void bind_dump_kws(char **out)
463{
464 struct bind_kw_list *kwl;
465 int index;
466
467 *out = NULL;
468 list_for_each_entry(kwl, &bind_keywords.list, list) {
469 for (index = 0; kwl->kw[index].kw != NULL; index++) {
470 if (kwl->kw[index].parse ||
471 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200472 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
473 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200474 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200475 kwl->kw[index].skip ? " <arg>" : "",
476 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200477 }
478 }
479 }
480}
481
Willy Tarreau645513a2010-05-24 20:55:15 +0200482/************************************************************************/
483/* All supported ACL keywords must be declared here. */
484/************************************************************************/
485
Willy Tarreaua5e37562011-12-16 17:06:15 +0100486/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200487static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200488acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200489 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +0200490{
Willy Tarreauf853c462012-04-23 18:53:56 +0200491 smp->type = SMP_T_UINT;
492 smp->data.uint = l4->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200493 return 1;
494}
495
Willy Tarreaua5e37562011-12-16 17:06:15 +0100496/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200497static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200498acl_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200499 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +0200500{
Willy Tarreauf853c462012-04-23 18:53:56 +0200501 smp->type = SMP_T_UINT;
502 smp->data.uint = l4->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200503 return 1;
504}
505
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200506/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200507static 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 +0200508{
509 struct listener *l;
510
Willy Tarreau4348fad2012-09-20 16:48:07 +0200511 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200512 l->options |= LI_O_ACC_PROXY;
513
514 return 0;
515}
516
517/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200518static 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 +0200519{
520 struct listener *l;
521 int val;
522
523 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200524 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200525 return ERR_ALERT | ERR_FATAL;
526 }
527
528 val = atol(args[cur_arg + 1]);
529 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200530 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200531 return ERR_ALERT | ERR_FATAL;
532 }
533
Willy Tarreau4348fad2012-09-20 16:48:07 +0200534 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200535 l->backlog = val;
536
537 return 0;
538}
539
540/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200541static 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 +0200542{
543 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200544 struct listener *l, *new;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200545
Willy Tarreau4348fad2012-09-20 16:48:07 +0200546 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200547 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200548 return ERR_ALERT | ERR_FATAL;
549 }
550
551 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200552 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200553 return ERR_ALERT | ERR_FATAL;
554 }
555
Willy Tarreau4348fad2012-09-20 16:48:07 +0200556 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
557 new->luid = atol(args[cur_arg + 1]);
558 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200559
Willy Tarreau4348fad2012-09-20 16:48:07 +0200560 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200561 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200562 return ERR_ALERT | ERR_FATAL;
563 }
564
Willy Tarreau4348fad2012-09-20 16:48:07 +0200565 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200566 if (node) {
567 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200568 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
569 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
570 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200571 return ERR_ALERT | ERR_FATAL;
572 }
573
Willy Tarreau4348fad2012-09-20 16:48:07 +0200574 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200575 return 0;
576}
577
578/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200579static 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 +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->maxconn = val;
597
598 return 0;
599}
600
601/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200602static 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 +0200603{
604 struct listener *l;
605
606 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200607 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200608 return ERR_ALERT | ERR_FATAL;
609 }
610
Willy Tarreau4348fad2012-09-20 16:48:07 +0200611 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200612 l->name = strdup(args[cur_arg + 1]);
613
614 return 0;
615}
616
617/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200618static 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 +0200619{
620 struct listener *l;
621 int val;
622
623 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200624 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200625 return ERR_ALERT | ERR_FATAL;
626 }
627
628 val = atol(args[cur_arg + 1]);
629 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200630 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200631 return ERR_ALERT | ERR_FATAL;
632 }
633
Willy Tarreau4348fad2012-09-20 16:48:07 +0200634 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200635 l->nice = val;
636
637 return 0;
638}
639
640
Willy Tarreau61612d42012-04-19 18:42:05 +0200641/* Note: must not be declared <const> as its list will be overwritten.
642 * Please take care of keeping this list alphabetically sorted.
643 */
Willy Tarreau645513a2010-05-24 20:55:15 +0200644static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +0200645 { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING, 0 },
646 { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +0200647 { NULL, NULL, NULL, NULL },
648}};
649
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200650/* Note: must not be declared <const> as its list will be overwritten.
651 * Please take care of keeping this list alphabetically sorted, doing so helps
652 * all code contributors.
653 * Optional keywords are also declared with a NULL ->parse() function so that
654 * the config parser can report an appropriate error when a known keyword was
655 * not enabled.
656 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200657static struct bind_kw_list bind_kws = { "ALL", { }, {
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200658 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
659 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
660 { "id", bind_parse_id, 1 }, /* set id of listening socket */
661 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
662 { "name", bind_parse_name, 1 }, /* set name of listening socket */
663 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
664 { NULL, NULL, 0 },
665}};
666
Willy Tarreau645513a2010-05-24 20:55:15 +0200667__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +0200668static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +0200669{
670 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200671 bind_register_keywords(&bind_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200672}
673
674/*
675 * Local variables:
676 * c-indent-level: 8
677 * c-basic-offset: 8
678 * End:
679 */