blob: c4259bcd5a1bf56f0de53093baf7c85a4bf5e9ea [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;
255 int max_accept = global.tune.maxaccept;
256 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);
314#else
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200315 cfd = accept(fd, (struct sockaddr *)&addr, &laddr);
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200316#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200317 if (unlikely(cfd == -1)) {
318 switch (errno) {
319 case EAGAIN:
320 case EINTR:
321 case ECONNABORTED:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200322 fd_poll_recv(fd);
323 return; /* nothing more to accept */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200324 case ENFILE:
325 if (p)
326 send_log(p, LOG_EMERG,
327 "Proxy %s reached system FD limit at %d. Please check system tunables.\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 EMFILE:
333 if (p)
334 send_log(p, LOG_EMERG,
335 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
336 p->id, maxfd);
337 limit_listener(l, &global_listener_queue);
338 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200339 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200340 case ENOBUFS:
341 case ENOMEM:
342 if (p)
343 send_log(p, LOG_EMERG,
344 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
345 p->id, maxfd);
346 limit_listener(l, &global_listener_queue);
347 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200348 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200349 default:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200350 /* unexpected result, let's go back to poll */
351 fd_poll_recv(fd);
352 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200353 }
354 }
355
356 if (unlikely(cfd >= global.maxsock)) {
357 send_log(p, LOG_EMERG,
358 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
359 p->id);
360 close(cfd);
361 limit_listener(l, &global_listener_queue);
362 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200363 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200364 }
365
366 /* increase the per-process number of cumulated connections */
367 if (!(l->options & LI_O_UNLIMITED)) {
368 update_freq_ctr(&global.conn_per_sec, 1);
369 if (global.conn_per_sec.curr_ctr > global.cps_max)
370 global.cps_max = global.conn_per_sec.curr_ctr;
371 actconn++;
372 }
373
374 jobs++;
375 totalconn++;
376 l->nbconn++;
377
378 if (l->counters) {
379 if (l->nbconn > l->counters->conn_max)
380 l->counters->conn_max = l->nbconn;
381 }
382
383 ret = l->accept(l, cfd, &addr);
384 if (unlikely(ret <= 0)) {
385 /* The connection was closed by session_accept(). Either
386 * we just have to ignore it (ret == 0) or it's a critical
387 * error due to a resource shortage, and we must stop the
388 * listener (ret < 0).
389 */
390 if (!(l->options & LI_O_UNLIMITED))
391 actconn--;
392 jobs--;
393 l->nbconn--;
394 if (ret == 0) /* successful termination */
395 continue;
396
397 limit_listener(l, &global_listener_queue);
398 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200399 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200400 }
401
402 if (l->nbconn >= l->maxconn) {
403 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200404 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200405 }
406
Willy Tarreauaece46a2012-07-06 12:25:58 +0200407 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200408
Willy Tarreauaece46a2012-07-06 12:25:58 +0200409 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200410 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200411}
412
Willy Tarreau26982662012-09-12 23:17:10 +0200413/*
414 * Registers the bind keyword list <kwl> as a list of valid keywords for next
415 * parsing sessions.
416 */
417void bind_register_keywords(struct bind_kw_list *kwl)
418{
419 LIST_ADDQ(&bind_keywords.list, &kwl->list);
420}
421
422/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
423 * keyword is found with a NULL ->parse() function, then an attempt is made to
424 * find one with a valid ->parse() function. This way it is possible to declare
425 * platform-dependant, known keywords as NULL, then only declare them as valid
426 * if some options are met. Note that if the requested keyword contains an
427 * opening parenthesis, everything from this point is ignored.
428 */
429struct bind_kw *bind_find_kw(const char *kw)
430{
431 int index;
432 const char *kwend;
433 struct bind_kw_list *kwl;
434 struct bind_kw *ret = NULL;
435
436 kwend = strchr(kw, '(');
437 if (!kwend)
438 kwend = kw + strlen(kw);
439
440 list_for_each_entry(kwl, &bind_keywords.list, list) {
441 for (index = 0; kwl->kw[index].kw != NULL; index++) {
442 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
443 kwl->kw[index].kw[kwend-kw] == 0) {
444 if (kwl->kw[index].parse)
445 return &kwl->kw[index]; /* found it !*/
446 else
447 ret = &kwl->kw[index]; /* may be OK */
448 }
449 }
450 }
451 return ret;
452}
453
Willy Tarreau8638f482012-09-18 18:01:17 +0200454/* Dumps all registered "bind" keywords to the <out> string pointer. The
455 * unsupported keywords are only dumped if their supported form was not
456 * found.
457 */
458void bind_dump_kws(char **out)
459{
460 struct bind_kw_list *kwl;
461 int index;
462
463 *out = NULL;
464 list_for_each_entry(kwl, &bind_keywords.list, list) {
465 for (index = 0; kwl->kw[index].kw != NULL; index++) {
466 if (kwl->kw[index].parse ||
467 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200468 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
469 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200470 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200471 kwl->kw[index].skip ? " <arg>" : "",
472 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200473 }
474 }
475 }
476}
477
Willy Tarreau645513a2010-05-24 20:55:15 +0200478/************************************************************************/
479/* All supported ACL keywords must be declared here. */
480/************************************************************************/
481
Willy Tarreaua5e37562011-12-16 17:06:15 +0100482/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200483static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200484acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200485 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +0200486{
Willy Tarreauf853c462012-04-23 18:53:56 +0200487 smp->type = SMP_T_UINT;
488 smp->data.uint = l4->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200489 return 1;
490}
491
Willy Tarreaua5e37562011-12-16 17:06:15 +0100492/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200493static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200494acl_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200495 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +0200496{
Willy Tarreauf853c462012-04-23 18:53:56 +0200497 smp->type = SMP_T_UINT;
498 smp->data.uint = l4->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200499 return 1;
500}
501
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200502/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200503static 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 +0200504{
505 struct listener *l;
506
Willy Tarreau4348fad2012-09-20 16:48:07 +0200507 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200508 l->options |= LI_O_ACC_PROXY;
509
510 return 0;
511}
512
513/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200514static 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 +0200515{
516 struct listener *l;
517 int val;
518
519 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200520 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200521 return ERR_ALERT | ERR_FATAL;
522 }
523
524 val = atol(args[cur_arg + 1]);
525 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200526 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200527 return ERR_ALERT | ERR_FATAL;
528 }
529
Willy Tarreau4348fad2012-09-20 16:48:07 +0200530 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200531 l->backlog = val;
532
533 return 0;
534}
535
536/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200537static 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 +0200538{
539 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200540 struct listener *l, *new;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200541
Willy Tarreau4348fad2012-09-20 16:48:07 +0200542 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200543 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200544 return ERR_ALERT | ERR_FATAL;
545 }
546
547 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200548 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200549 return ERR_ALERT | ERR_FATAL;
550 }
551
Willy Tarreau4348fad2012-09-20 16:48:07 +0200552 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
553 new->luid = atol(args[cur_arg + 1]);
554 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200555
Willy Tarreau4348fad2012-09-20 16:48:07 +0200556 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200557 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200558 return ERR_ALERT | ERR_FATAL;
559 }
560
Willy Tarreau4348fad2012-09-20 16:48:07 +0200561 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200562 if (node) {
563 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200564 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
565 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
566 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200567 return ERR_ALERT | ERR_FATAL;
568 }
569
Willy Tarreau4348fad2012-09-20 16:48:07 +0200570 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200571 return 0;
572}
573
574/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200575static 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 +0200576{
577 struct listener *l;
578 int val;
579
580 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200581 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200582 return ERR_ALERT | ERR_FATAL;
583 }
584
585 val = atol(args[cur_arg + 1]);
586 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200587 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200588 return ERR_ALERT | ERR_FATAL;
589 }
590
Willy Tarreau4348fad2012-09-20 16:48:07 +0200591 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200592 l->maxconn = val;
593
594 return 0;
595}
596
597/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200598static 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 +0200599{
600 struct listener *l;
601
602 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200603 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200604 return ERR_ALERT | ERR_FATAL;
605 }
606
Willy Tarreau4348fad2012-09-20 16:48:07 +0200607 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200608 l->name = strdup(args[cur_arg + 1]);
609
610 return 0;
611}
612
613/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200614static 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 +0200615{
616 struct listener *l;
617 int val;
618
619 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200620 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200621 return ERR_ALERT | ERR_FATAL;
622 }
623
624 val = atol(args[cur_arg + 1]);
625 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200626 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200627 return ERR_ALERT | ERR_FATAL;
628 }
629
Willy Tarreau4348fad2012-09-20 16:48:07 +0200630 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200631 l->nice = val;
632
633 return 0;
634}
635
636
Willy Tarreau61612d42012-04-19 18:42:05 +0200637/* Note: must not be declared <const> as its list will be overwritten.
638 * Please take care of keeping this list alphabetically sorted.
639 */
Willy Tarreau645513a2010-05-24 20:55:15 +0200640static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +0200641 { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING, 0 },
642 { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +0200643 { NULL, NULL, NULL, NULL },
644}};
645
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200646/* Note: must not be declared <const> as its list will be overwritten.
647 * Please take care of keeping this list alphabetically sorted, doing so helps
648 * all code contributors.
649 * Optional keywords are also declared with a NULL ->parse() function so that
650 * the config parser can report an appropriate error when a known keyword was
651 * not enabled.
652 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200653static struct bind_kw_list bind_kws = { "ALL", { }, {
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200654 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
655 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
656 { "id", bind_parse_id, 1 }, /* set id of listening socket */
657 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
658 { "name", bind_parse_name, 1 }, /* set name of listening socket */
659 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
660 { NULL, NULL, 0 },
661}};
662
Willy Tarreau645513a2010-05-24 20:55:15 +0200663__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +0200664static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +0200665{
666 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200667 bind_register_keywords(&bind_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200668}
669
670/*
671 * Local variables:
672 * c-indent-level: 8
673 * c-basic-offset: 8
674 * End:
675 */