blob: 22e386e3ae7de844c97642a627ac2531c6bef393 [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
Willy Tarreau1bc4aab2012-10-08 20:11:03 +020017#include <common/accept4.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020018#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010019#include <common/errors.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020020#include <common/mini-clist.h>
21#include <common/standard.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020022#include <common/time.h>
23
24#include <types/global.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020025#include <types/protocol.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020026
Willy Tarreau645513a2010-05-24 20:55:15 +020027#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010028#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020029#include <proto/freq_ctr.h>
30#include <proto/log.h>
31#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010032
Willy Tarreau26982662012-09-12 23:17:10 +020033/* List head of all known bind keywords */
34static struct bind_kw_list bind_keywords = {
35 .list = LIST_HEAD_INIT(bind_keywords.list)
36};
37
Willy Tarreaudabf2e22007-10-28 21:59:24 +010038/* This function adds the specified listener's file descriptor to the polling
39 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
40 * LI_FULL state depending on its number of connections.
41 */
42void enable_listener(struct listener *listener)
43{
44 if (listener->state == LI_LISTEN) {
45 if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020046 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010047 listener->state = LI_READY;
48 } else {
49 listener->state = LI_FULL;
50 }
51 }
52}
53
54/* This function removes the specified listener's file descriptor from the
55 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
56 * enters LI_LISTEN.
57 */
58void disable_listener(struct listener *listener)
59{
60 if (listener->state < LI_READY)
61 return;
62 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +020063 fd_stop_recv(listener->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020064 if (listener->state == LI_LIMITED)
65 LIST_DEL(&listener->wait_queue);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010066 listener->state = LI_LISTEN;
67}
68
Willy Tarreaube58c382011-07-24 18:28:10 +020069/* This function tries to temporarily disable a listener, depending on the OS
70 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
71 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
72 * closes upon SHUT_WR and refuses to rebind. So a common validation path
73 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
74 * is disabled. It normally returns non-zero, unless an error is reported.
75 */
76int pause_listener(struct listener *l)
77{
78 if (l->state <= LI_PAUSED)
79 return 1;
80
Willy Tarreaub3fb60b2012-10-04 08:56:31 +020081 if (l->proto->sock_prot == IPPROTO_TCP) {
82 if (shutdown(l->fd, SHUT_WR) != 0)
83 return 0; /* Solaris dies here */
Willy Tarreaube58c382011-07-24 18:28:10 +020084
Willy Tarreaub3fb60b2012-10-04 08:56:31 +020085 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
86 return 0; /* OpenBSD dies here */
Willy Tarreaube58c382011-07-24 18:28:10 +020087
Willy Tarreaub3fb60b2012-10-04 08:56:31 +020088 if (shutdown(l->fd, SHUT_RD) != 0)
89 return 0; /* should always be OK */
90 }
Willy Tarreaube58c382011-07-24 18:28:10 +020091
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020092 if (l->state == LI_LIMITED)
93 LIST_DEL(&l->wait_queue);
94
Willy Tarreau49b046d2012-08-09 12:11:58 +020095 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +020096 l->state = LI_PAUSED;
97 return 1;
98}
99
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200100/* This function tries to resume a temporarily disabled listener. Paused, full,
101 * limited and disabled listeners are handled, which means that this function
102 * may replace enable_listener(). The resulting state will either be LI_READY
103 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreaube58c382011-07-24 18:28:10 +0200104 */
105int resume_listener(struct listener *l)
106{
107 if (l->state < LI_PAUSED)
108 return 0;
109
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200110 if (l->proto->sock_prot == IPPROTO_TCP &&
111 l->state == LI_PAUSED &&
Willy Tarreaube58c382011-07-24 18:28:10 +0200112 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
113 return 0;
114
115 if (l->state == LI_READY)
116 return 1;
117
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200118 if (l->state == LI_LIMITED)
119 LIST_DEL(&l->wait_queue);
120
Willy Tarreaube58c382011-07-24 18:28:10 +0200121 if (l->nbconn >= l->maxconn) {
122 l->state = LI_FULL;
123 return 1;
124 }
125
Willy Tarreau49b046d2012-08-09 12:11:58 +0200126 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200127 l->state = LI_READY;
128 return 1;
129}
130
Willy Tarreau62793712011-07-24 19:23:38 +0200131/* Marks a ready listener as full so that the session code tries to re-enable
132 * it upon next close() using resume_listener().
133 */
134void listener_full(struct listener *l)
135{
136 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200137 if (l->state == LI_LIMITED)
138 LIST_DEL(&l->wait_queue);
139
Willy Tarreau49b046d2012-08-09 12:11:58 +0200140 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200141 l->state = LI_FULL;
142 }
143}
144
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200145/* Marks a ready listener as limited so that we only try to re-enable it when
146 * resources are free again. It will be queued into the specified queue.
147 */
148void limit_listener(struct listener *l, struct list *list)
149{
150 if (l->state == LI_READY) {
151 LIST_ADDQ(list, &l->wait_queue);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200152 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200153 l->state = LI_LIMITED;
154 }
155}
156
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100157/* This function adds all of the protocol's listener's file descriptors to the
158 * polling lists when they are in the LI_LISTEN state. It is intended to be
159 * used as a protocol's generic enable_all() primitive, for use after the
160 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
161 * their number of connections. It always returns ERR_NONE.
162 */
163int enable_all_listeners(struct protocol *proto)
164{
165 struct listener *listener;
166
167 list_for_each_entry(listener, &proto->listeners, proto_list)
168 enable_listener(listener);
169 return ERR_NONE;
170}
171
172/* This function removes all of the protocol's listener's file descriptors from
173 * the polling lists when they are in the LI_READY or LI_FULL states. It is
174 * intended to be used as a protocol's generic disable_all() primitive. It puts
175 * the listeners into LI_LISTEN, and always returns ERR_NONE.
176 */
177int disable_all_listeners(struct protocol *proto)
178{
179 struct listener *listener;
180
181 list_for_each_entry(listener, &proto->listeners, proto_list)
182 disable_listener(listener);
183 return ERR_NONE;
184}
185
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200186/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
187void dequeue_all_listeners(struct list *list)
188{
189 struct listener *listener, *l_back;
190
191 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
192 /* This cannot fail because the listeners are by definition in
193 * the LI_LIMITED state. The function also removes the entry
194 * from the queue.
195 */
196 resume_listener(listener);
197 }
198}
199
Willy Tarreaub648d632007-10-28 22:13:50 +0100200/* This function closes the listening socket for the specified listener,
201 * provided that it's already in a listening state. The listener enters the
202 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
203 * to be used as a generic function for standard protocols.
204 */
205int unbind_listener(struct listener *listener)
206{
207 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200208 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100209
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200210 if (listener->state == LI_LIMITED)
211 LIST_DEL(&listener->wait_queue);
212
Willy Tarreaube58c382011-07-24 18:28:10 +0200213 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100214 fd_delete(listener->fd);
215 listener->state = LI_ASSIGNED;
216 }
217 return ERR_NONE;
218}
219
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100220/* This function closes all listening sockets bound to the protocol <proto>,
221 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
222 * detach them from the protocol. It always returns ERR_NONE.
223 */
224int unbind_all_listeners(struct protocol *proto)
225{
226 struct listener *listener;
227
228 list_for_each_entry(listener, &proto->listeners, proto_list)
229 unbind_listener(listener);
230 return ERR_NONE;
231}
232
Willy Tarreau1a64d162007-10-28 22:26:05 +0100233/* Delete a listener from its protocol's list of listeners. The listener's
234 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
235 * number of listeners is updated. Note that the listener must have previously
236 * been unbound. This is the generic function to use to remove a listener.
237 */
238void delete_listener(struct listener *listener)
239{
240 if (listener->state != LI_ASSIGNED)
241 return;
242 listener->state = LI_INIT;
243 LIST_DEL(&listener->proto_list);
244 listener->proto->nb_listeners--;
245}
246
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200247/* This function is called on a read event from a listening socket, corresponding
248 * to an accept. It tries to accept as many connections as possible, and for each
249 * calls the listener's accept handler (generally the frontend's accept handler).
250 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200251void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200252{
253 struct listener *l = fdtab[fd].owner;
254 struct proxy *p = l->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100255 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200256 int cfd;
257 int ret;
258
259 if (unlikely(l->nbconn >= l->maxconn)) {
260 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200261 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200262 }
263
264 if (global.cps_lim && !(l->options & LI_O_UNLIMITED)) {
265 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
266
267 if (unlikely(!max)) {
268 /* frontend accept rate limit was reached */
269 limit_listener(l, &global_listener_queue);
270 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 +0200271 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200272 }
273
274 if (max_accept > max)
275 max_accept = max;
276 }
277
278 if (p && p->fe_sps_lim) {
279 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
280
281 if (unlikely(!max)) {
282 /* frontend accept rate limit was reached */
283 limit_listener(l, &p->listener_queue);
284 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 +0200285 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200286 }
287
288 if (max_accept > max)
289 max_accept = max;
290 }
291
292 /* Note: if we fail to allocate a connection because of configured
293 * limits, we'll schedule a new attempt worst 1 second later in the
294 * worst case. If we fail due to system limits or temporary resource
295 * shortage, we try again 100ms later in the worst case.
296 */
297 while (max_accept--) {
298 struct sockaddr_storage addr;
299 socklen_t laddr = sizeof(addr);
300
301 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
302 limit_listener(l, &global_listener_queue);
303 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200304 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200305 }
306
307 if (unlikely(p && p->feconn >= p->maxconn)) {
308 limit_listener(l, &p->listener_queue);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200309 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200310 }
311
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200312#ifdef USE_ACCEPT4
313 cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK);
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200314 if (unlikely(cfd == -1 && errno == EINVAL)) {
315 /* unsupported syscall, fallback to normal accept()+fcntl() */
316 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
317 fcntl(cfd, F_SETFL, O_NONBLOCK);
318 }
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200319#else
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200320 cfd = accept(fd, (struct sockaddr *)&addr, &laddr);
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200321#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200322 if (unlikely(cfd == -1)) {
323 switch (errno) {
324 case EAGAIN:
325 case EINTR:
326 case ECONNABORTED:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200327 fd_poll_recv(fd);
328 return; /* nothing more to accept */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200329 case ENFILE:
330 if (p)
331 send_log(p, LOG_EMERG,
332 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
333 p->id, maxfd);
334 limit_listener(l, &global_listener_queue);
335 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200336 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200337 case EMFILE:
338 if (p)
339 send_log(p, LOG_EMERG,
340 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
341 p->id, maxfd);
342 limit_listener(l, &global_listener_queue);
343 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200344 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200345 case ENOBUFS:
346 case ENOMEM:
347 if (p)
348 send_log(p, LOG_EMERG,
349 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
350 p->id, maxfd);
351 limit_listener(l, &global_listener_queue);
352 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200353 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200354 default:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200355 /* unexpected result, let's go back to poll */
356 fd_poll_recv(fd);
357 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200358 }
359 }
360
361 if (unlikely(cfd >= global.maxsock)) {
362 send_log(p, LOG_EMERG,
363 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
364 p->id);
365 close(cfd);
366 limit_listener(l, &global_listener_queue);
367 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200368 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200369 }
370
371 /* increase the per-process number of cumulated connections */
372 if (!(l->options & LI_O_UNLIMITED)) {
373 update_freq_ctr(&global.conn_per_sec, 1);
374 if (global.conn_per_sec.curr_ctr > global.cps_max)
375 global.cps_max = global.conn_per_sec.curr_ctr;
376 actconn++;
377 }
378
379 jobs++;
380 totalconn++;
381 l->nbconn++;
382
383 if (l->counters) {
384 if (l->nbconn > l->counters->conn_max)
385 l->counters->conn_max = l->nbconn;
386 }
387
388 ret = l->accept(l, cfd, &addr);
389 if (unlikely(ret <= 0)) {
390 /* The connection was closed by session_accept(). Either
391 * we just have to ignore it (ret == 0) or it's a critical
392 * error due to a resource shortage, and we must stop the
393 * listener (ret < 0).
394 */
395 if (!(l->options & LI_O_UNLIMITED))
396 actconn--;
397 jobs--;
398 l->nbconn--;
399 if (ret == 0) /* successful termination */
400 continue;
401
402 limit_listener(l, &global_listener_queue);
403 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200404 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200405 }
406
407 if (l->nbconn >= l->maxconn) {
408 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200409 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200410 }
411
Willy Tarreauaece46a2012-07-06 12:25:58 +0200412 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200413
Willy Tarreauaece46a2012-07-06 12:25:58 +0200414 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200415 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200416}
417
Willy Tarreau26982662012-09-12 23:17:10 +0200418/*
419 * Registers the bind keyword list <kwl> as a list of valid keywords for next
420 * parsing sessions.
421 */
422void bind_register_keywords(struct bind_kw_list *kwl)
423{
424 LIST_ADDQ(&bind_keywords.list, &kwl->list);
425}
426
427/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
428 * keyword is found with a NULL ->parse() function, then an attempt is made to
429 * find one with a valid ->parse() function. This way it is possible to declare
430 * platform-dependant, known keywords as NULL, then only declare them as valid
431 * if some options are met. Note that if the requested keyword contains an
432 * opening parenthesis, everything from this point is ignored.
433 */
434struct bind_kw *bind_find_kw(const char *kw)
435{
436 int index;
437 const char *kwend;
438 struct bind_kw_list *kwl;
439 struct bind_kw *ret = NULL;
440
441 kwend = strchr(kw, '(');
442 if (!kwend)
443 kwend = kw + strlen(kw);
444
445 list_for_each_entry(kwl, &bind_keywords.list, list) {
446 for (index = 0; kwl->kw[index].kw != NULL; index++) {
447 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
448 kwl->kw[index].kw[kwend-kw] == 0) {
449 if (kwl->kw[index].parse)
450 return &kwl->kw[index]; /* found it !*/
451 else
452 ret = &kwl->kw[index]; /* may be OK */
453 }
454 }
455 }
456 return ret;
457}
458
Willy Tarreau8638f482012-09-18 18:01:17 +0200459/* Dumps all registered "bind" keywords to the <out> string pointer. The
460 * unsupported keywords are only dumped if their supported form was not
461 * found.
462 */
463void bind_dump_kws(char **out)
464{
465 struct bind_kw_list *kwl;
466 int index;
467
468 *out = NULL;
469 list_for_each_entry(kwl, &bind_keywords.list, list) {
470 for (index = 0; kwl->kw[index].kw != NULL; index++) {
471 if (kwl->kw[index].parse ||
472 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200473 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
474 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200475 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200476 kwl->kw[index].skip ? " <arg>" : "",
477 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200478 }
479 }
480 }
481}
482
Willy Tarreau645513a2010-05-24 20:55:15 +0200483/************************************************************************/
484/* All supported ACL keywords must be declared here. */
485/************************************************************************/
486
Willy Tarreaua5e37562011-12-16 17:06:15 +0100487/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200488static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200489acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200490 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +0200491{
Willy Tarreauf853c462012-04-23 18:53:56 +0200492 smp->type = SMP_T_UINT;
493 smp->data.uint = l4->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200494 return 1;
495}
496
Willy Tarreaua5e37562011-12-16 17:06:15 +0100497/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200498static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200499acl_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200500 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +0200501{
Willy Tarreauf853c462012-04-23 18:53:56 +0200502 smp->type = SMP_T_UINT;
503 smp->data.uint = l4->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200504 return 1;
505}
506
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200507/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200508static 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 +0200509{
510 struct listener *l;
511
Willy Tarreau4348fad2012-09-20 16:48:07 +0200512 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200513 l->options |= LI_O_ACC_PROXY;
514
515 return 0;
516}
517
518/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200519static 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 +0200520{
521 struct listener *l;
522 int val;
523
524 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200525 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200526 return ERR_ALERT | ERR_FATAL;
527 }
528
529 val = atol(args[cur_arg + 1]);
530 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200531 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200532 return ERR_ALERT | ERR_FATAL;
533 }
534
Willy Tarreau4348fad2012-09-20 16:48:07 +0200535 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200536 l->backlog = val;
537
538 return 0;
539}
540
541/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200542static 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 +0200543{
544 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200545 struct listener *l, *new;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200546
Willy Tarreau4348fad2012-09-20 16:48:07 +0200547 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200548 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200549 return ERR_ALERT | ERR_FATAL;
550 }
551
552 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200553 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200554 return ERR_ALERT | ERR_FATAL;
555 }
556
Willy Tarreau4348fad2012-09-20 16:48:07 +0200557 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
558 new->luid = atol(args[cur_arg + 1]);
559 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200560
Willy Tarreau4348fad2012-09-20 16:48:07 +0200561 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200562 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200563 return ERR_ALERT | ERR_FATAL;
564 }
565
Willy Tarreau4348fad2012-09-20 16:48:07 +0200566 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200567 if (node) {
568 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200569 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
570 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
571 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200572 return ERR_ALERT | ERR_FATAL;
573 }
574
Willy Tarreau4348fad2012-09-20 16:48:07 +0200575 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200576 return 0;
577}
578
579/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200580static 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 +0200581{
582 struct listener *l;
583 int val;
584
585 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200586 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200587 return ERR_ALERT | ERR_FATAL;
588 }
589
590 val = atol(args[cur_arg + 1]);
591 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200592 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200593 return ERR_ALERT | ERR_FATAL;
594 }
595
Willy Tarreau4348fad2012-09-20 16:48:07 +0200596 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200597 l->maxconn = val;
598
599 return 0;
600}
601
602/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200603static 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 +0200604{
605 struct listener *l;
606
607 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200608 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200609 return ERR_ALERT | ERR_FATAL;
610 }
611
Willy Tarreau4348fad2012-09-20 16:48:07 +0200612 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200613 l->name = strdup(args[cur_arg + 1]);
614
615 return 0;
616}
617
618/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200619static 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 +0200620{
621 struct listener *l;
622 int val;
623
624 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200625 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200626 return ERR_ALERT | ERR_FATAL;
627 }
628
629 val = atol(args[cur_arg + 1]);
630 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200631 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200632 return ERR_ALERT | ERR_FATAL;
633 }
634
Willy Tarreau4348fad2012-09-20 16:48:07 +0200635 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200636 l->nice = val;
637
638 return 0;
639}
640
641
Willy Tarreau61612d42012-04-19 18:42:05 +0200642/* Note: must not be declared <const> as its list will be overwritten.
643 * Please take care of keeping this list alphabetically sorted.
644 */
Willy Tarreau645513a2010-05-24 20:55:15 +0200645static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +0200646 { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING, 0 },
647 { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +0200648 { NULL, NULL, NULL, NULL },
649}};
650
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200651/* Note: must not be declared <const> as its list will be overwritten.
652 * Please take care of keeping this list alphabetically sorted, doing so helps
653 * all code contributors.
654 * Optional keywords are also declared with a NULL ->parse() function so that
655 * the config parser can report an appropriate error when a known keyword was
656 * not enabled.
657 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200658static struct bind_kw_list bind_kws = { "ALL", { }, {
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200659 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
660 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
661 { "id", bind_parse_id, 1 }, /* set id of listening socket */
662 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
663 { "name", bind_parse_name, 1 }, /* set name of listening socket */
664 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
665 { NULL, NULL, 0 },
666}};
667
Willy Tarreau645513a2010-05-24 20:55:15 +0200668__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +0200669static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +0200670{
671 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200672 bind_register_keywords(&bind_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200673}
674
675/*
676 * Local variables:
677 * c-indent-level: 8
678 * c-basic-offset: 8
679 * End:
680 */