blob: ba7d727fb274715a03c5f5c831a73f8eea9a3180 [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>
17
Willy Tarreau1bc4aab2012-10-08 20:11:03 +020018#include <common/accept4.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020019#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010020#include <common/errors.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020021#include <common/mini-clist.h>
22#include <common/standard.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020023#include <common/time.h>
24
25#include <types/global.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020026#include <types/protocol.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020027
Willy Tarreau645513a2010-05-24 20:55:15 +020028#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010029#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020030#include <proto/freq_ctr.h>
31#include <proto/log.h>
Willy Tarreau0ccb7442013-01-07 22:54:17 +010032#include <proto/sample.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020033#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010034
Willy Tarreau26982662012-09-12 23:17:10 +020035/* List head of all known bind keywords */
36static struct bind_kw_list bind_keywords = {
37 .list = LIST_HEAD_INIT(bind_keywords.list)
38};
39
Willy Tarreaudabf2e22007-10-28 21:59:24 +010040/* This function adds the specified listener's file descriptor to the polling
41 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
42 * LI_FULL state depending on its number of connections.
43 */
44void enable_listener(struct listener *listener)
45{
46 if (listener->state == LI_LISTEN) {
47 if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020048 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010049 listener->state = LI_READY;
50 } else {
51 listener->state = LI_FULL;
52 }
53 }
54}
55
56/* This function removes the specified listener's file descriptor from the
57 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
58 * enters LI_LISTEN.
59 */
60void disable_listener(struct listener *listener)
61{
62 if (listener->state < LI_READY)
63 return;
64 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +020065 fd_stop_recv(listener->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020066 if (listener->state == LI_LIMITED)
67 LIST_DEL(&listener->wait_queue);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010068 listener->state = LI_LISTEN;
69}
70
Willy Tarreaube58c382011-07-24 18:28:10 +020071/* This function tries to temporarily disable a listener, depending on the OS
72 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
73 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
74 * closes upon SHUT_WR and refuses to rebind. So a common validation path
75 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
76 * is disabled. It normally returns non-zero, unless an error is reported.
77 */
78int pause_listener(struct listener *l)
79{
80 if (l->state <= LI_PAUSED)
81 return 1;
82
Willy Tarreaub3fb60b2012-10-04 08:56:31 +020083 if (l->proto->sock_prot == IPPROTO_TCP) {
84 if (shutdown(l->fd, SHUT_WR) != 0)
85 return 0; /* Solaris dies here */
Willy Tarreaube58c382011-07-24 18:28:10 +020086
Willy Tarreaub3fb60b2012-10-04 08:56:31 +020087 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
88 return 0; /* OpenBSD dies here */
Willy Tarreaube58c382011-07-24 18:28:10 +020089
Willy Tarreaub3fb60b2012-10-04 08:56:31 +020090 if (shutdown(l->fd, SHUT_RD) != 0)
91 return 0; /* should always be OK */
92 }
Willy Tarreaube58c382011-07-24 18:28:10 +020093
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020094 if (l->state == LI_LIMITED)
95 LIST_DEL(&l->wait_queue);
96
Willy Tarreau49b046d2012-08-09 12:11:58 +020097 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +020098 l->state = LI_PAUSED;
99 return 1;
100}
101
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200102/* This function tries to resume a temporarily disabled listener. Paused, full,
103 * limited and disabled listeners are handled, which means that this function
104 * may replace enable_listener(). The resulting state will either be LI_READY
105 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreaube58c382011-07-24 18:28:10 +0200106 */
107int resume_listener(struct listener *l)
108{
109 if (l->state < LI_PAUSED)
110 return 0;
111
Willy Tarreaub3fb60b2012-10-04 08:56:31 +0200112 if (l->proto->sock_prot == IPPROTO_TCP &&
113 l->state == LI_PAUSED &&
Willy Tarreaube58c382011-07-24 18:28:10 +0200114 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
115 return 0;
116
117 if (l->state == LI_READY)
118 return 1;
119
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200120 if (l->state == LI_LIMITED)
121 LIST_DEL(&l->wait_queue);
122
Willy Tarreaube58c382011-07-24 18:28:10 +0200123 if (l->nbconn >= l->maxconn) {
124 l->state = LI_FULL;
125 return 1;
126 }
127
Willy Tarreau49b046d2012-08-09 12:11:58 +0200128 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200129 l->state = LI_READY;
130 return 1;
131}
132
Willy Tarreau62793712011-07-24 19:23:38 +0200133/* Marks a ready listener as full so that the session code tries to re-enable
134 * it upon next close() using resume_listener().
135 */
136void listener_full(struct listener *l)
137{
138 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200139 if (l->state == LI_LIMITED)
140 LIST_DEL(&l->wait_queue);
141
Willy Tarreau49b046d2012-08-09 12:11:58 +0200142 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200143 l->state = LI_FULL;
144 }
145}
146
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200147/* Marks a ready listener as limited so that we only try to re-enable it when
148 * resources are free again. It will be queued into the specified queue.
149 */
150void limit_listener(struct listener *l, struct list *list)
151{
152 if (l->state == LI_READY) {
153 LIST_ADDQ(list, &l->wait_queue);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200154 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200155 l->state = LI_LIMITED;
156 }
157}
158
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100159/* This function adds all of the protocol's listener's file descriptors to the
160 * polling lists when they are in the LI_LISTEN state. It is intended to be
161 * used as a protocol's generic enable_all() primitive, for use after the
162 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
163 * their number of connections. It always returns ERR_NONE.
164 */
165int enable_all_listeners(struct protocol *proto)
166{
167 struct listener *listener;
168
169 list_for_each_entry(listener, &proto->listeners, proto_list)
170 enable_listener(listener);
171 return ERR_NONE;
172}
173
174/* This function removes all of the protocol's listener's file descriptors from
175 * the polling lists when they are in the LI_READY or LI_FULL states. It is
176 * intended to be used as a protocol's generic disable_all() primitive. It puts
177 * the listeners into LI_LISTEN, and always returns ERR_NONE.
178 */
179int disable_all_listeners(struct protocol *proto)
180{
181 struct listener *listener;
182
183 list_for_each_entry(listener, &proto->listeners, proto_list)
184 disable_listener(listener);
185 return ERR_NONE;
186}
187
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200188/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
189void dequeue_all_listeners(struct list *list)
190{
191 struct listener *listener, *l_back;
192
193 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
194 /* This cannot fail because the listeners are by definition in
195 * the LI_LIMITED state. The function also removes the entry
196 * from the queue.
197 */
198 resume_listener(listener);
199 }
200}
201
Willy Tarreaub648d632007-10-28 22:13:50 +0100202/* This function closes the listening socket for the specified listener,
203 * provided that it's already in a listening state. The listener enters the
204 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
205 * to be used as a generic function for standard protocols.
206 */
207int unbind_listener(struct listener *listener)
208{
209 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200210 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100211
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200212 if (listener->state == LI_LIMITED)
213 LIST_DEL(&listener->wait_queue);
214
Willy Tarreaube58c382011-07-24 18:28:10 +0200215 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100216 fd_delete(listener->fd);
217 listener->state = LI_ASSIGNED;
218 }
219 return ERR_NONE;
220}
221
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100222/* This function closes all listening sockets bound to the protocol <proto>,
223 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
224 * detach them from the protocol. It always returns ERR_NONE.
225 */
226int unbind_all_listeners(struct protocol *proto)
227{
228 struct listener *listener;
229
230 list_for_each_entry(listener, &proto->listeners, proto_list)
231 unbind_listener(listener);
232 return ERR_NONE;
233}
234
Willy Tarreau1a64d162007-10-28 22:26:05 +0100235/* Delete a listener from its protocol's list of listeners. The listener's
236 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
237 * number of listeners is updated. Note that the listener must have previously
238 * been unbound. This is the generic function to use to remove a listener.
239 */
240void delete_listener(struct listener *listener)
241{
242 if (listener->state != LI_ASSIGNED)
243 return;
244 listener->state = LI_INIT;
245 LIST_DEL(&listener->proto_list);
246 listener->proto->nb_listeners--;
247}
248
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200249/* This function is called on a read event from a listening socket, corresponding
250 * to an accept. It tries to accept as many connections as possible, and for each
251 * calls the listener's accept handler (generally the frontend's accept handler).
252 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200253void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200254{
255 struct listener *l = fdtab[fd].owner;
256 struct proxy *p = l->frontend;
Willy Tarreau50de90a2012-11-23 20:11:45 +0100257 int max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200258 int cfd;
259 int ret;
260
261 if (unlikely(l->nbconn >= l->maxconn)) {
262 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200263 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200264 }
265
266 if (global.cps_lim && !(l->options & LI_O_UNLIMITED)) {
267 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
268
269 if (unlikely(!max)) {
270 /* frontend accept rate limit was reached */
271 limit_listener(l, &global_listener_queue);
272 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 +0200273 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200274 }
275
276 if (max_accept > max)
277 max_accept = max;
278 }
279
280 if (p && p->fe_sps_lim) {
281 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
282
283 if (unlikely(!max)) {
284 /* frontend accept rate limit was reached */
285 limit_listener(l, &p->listener_queue);
286 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 +0200287 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200288 }
289
290 if (max_accept > max)
291 max_accept = max;
292 }
293
294 /* Note: if we fail to allocate a connection because of configured
295 * limits, we'll schedule a new attempt worst 1 second later in the
296 * worst case. If we fail due to system limits or temporary resource
297 * shortage, we try again 100ms later in the worst case.
298 */
299 while (max_accept--) {
300 struct sockaddr_storage addr;
301 socklen_t laddr = sizeof(addr);
302
303 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
304 limit_listener(l, &global_listener_queue);
305 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200306 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200307 }
308
309 if (unlikely(p && p->feconn >= p->maxconn)) {
310 limit_listener(l, &p->listener_queue);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200311 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200312 }
313
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200314#ifdef USE_ACCEPT4
315 cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK);
Willy Tarreau6b3b0d42012-10-22 19:32:55 +0200316 if (unlikely(cfd == -1 && errno == EINVAL)) {
317 /* unsupported syscall, fallback to normal accept()+fcntl() */
318 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1)
319 fcntl(cfd, F_SETFL, O_NONBLOCK);
320 }
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200321#else
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200322 cfd = accept(fd, (struct sockaddr *)&addr, &laddr);
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200323#endif
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200324 if (unlikely(cfd == -1)) {
325 switch (errno) {
326 case EAGAIN:
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100327 fd_cant_recv(fd);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200328 return; /* nothing more to accept */
Willy Tarreaua593ec52014-01-20 21:21:30 +0100329 case EINTR:
330 case ECONNABORTED:
331 continue;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200332 case ENFILE:
333 if (p)
334 send_log(p, LOG_EMERG,
335 "Proxy %s reached system FD limit at %d. Please check system tunables.\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 EMFILE:
341 if (p)
342 send_log(p, LOG_EMERG,
343 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
344 p->id, maxfd);
345 limit_listener(l, &global_listener_queue);
346 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200347 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200348 case ENOBUFS:
349 case ENOMEM:
350 if (p)
351 send_log(p, LOG_EMERG,
352 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
353 p->id, maxfd);
354 limit_listener(l, &global_listener_queue);
355 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200356 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200357 default:
Willy Tarreaua593ec52014-01-20 21:21:30 +0100358 /* unexpected result, let's give up and let other tasks run */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200359 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200360 }
361 }
362
363 if (unlikely(cfd >= global.maxsock)) {
364 send_log(p, LOG_EMERG,
365 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
366 p->id);
367 close(cfd);
368 limit_listener(l, &global_listener_queue);
369 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200370 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200371 }
372
373 /* increase the per-process number of cumulated connections */
374 if (!(l->options & LI_O_UNLIMITED)) {
375 update_freq_ctr(&global.conn_per_sec, 1);
376 if (global.conn_per_sec.curr_ctr > global.cps_max)
377 global.cps_max = global.conn_per_sec.curr_ctr;
378 actconn++;
379 }
380
381 jobs++;
382 totalconn++;
383 l->nbconn++;
384
385 if (l->counters) {
386 if (l->nbconn > l->counters->conn_max)
387 l->counters->conn_max = l->nbconn;
388 }
389
390 ret = l->accept(l, cfd, &addr);
391 if (unlikely(ret <= 0)) {
392 /* The connection was closed by session_accept(). Either
393 * we just have to ignore it (ret == 0) or it's a critical
394 * error due to a resource shortage, and we must stop the
395 * listener (ret < 0).
396 */
397 if (!(l->options & LI_O_UNLIMITED))
398 actconn--;
399 jobs--;
400 l->nbconn--;
401 if (ret == 0) /* successful termination */
402 continue;
403
404 limit_listener(l, &global_listener_queue);
405 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200406 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200407 }
408
409 if (l->nbconn >= l->maxconn) {
410 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200411 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200412 }
413
Willy Tarreauaece46a2012-07-06 12:25:58 +0200414 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200415
Willy Tarreauaece46a2012-07-06 12:25:58 +0200416 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200417 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200418}
419
Willy Tarreau26982662012-09-12 23:17:10 +0200420/*
421 * Registers the bind keyword list <kwl> as a list of valid keywords for next
422 * parsing sessions.
423 */
424void bind_register_keywords(struct bind_kw_list *kwl)
425{
426 LIST_ADDQ(&bind_keywords.list, &kwl->list);
427}
428
429/* Return a pointer to the bind keyword <kw>, or NULL if not found. If the
430 * keyword is found with a NULL ->parse() function, then an attempt is made to
431 * find one with a valid ->parse() function. This way it is possible to declare
432 * platform-dependant, known keywords as NULL, then only declare them as valid
433 * if some options are met. Note that if the requested keyword contains an
434 * opening parenthesis, everything from this point is ignored.
435 */
436struct bind_kw *bind_find_kw(const char *kw)
437{
438 int index;
439 const char *kwend;
440 struct bind_kw_list *kwl;
441 struct bind_kw *ret = NULL;
442
443 kwend = strchr(kw, '(');
444 if (!kwend)
445 kwend = kw + strlen(kw);
446
447 list_for_each_entry(kwl, &bind_keywords.list, list) {
448 for (index = 0; kwl->kw[index].kw != NULL; index++) {
449 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
450 kwl->kw[index].kw[kwend-kw] == 0) {
451 if (kwl->kw[index].parse)
452 return &kwl->kw[index]; /* found it !*/
453 else
454 ret = &kwl->kw[index]; /* may be OK */
455 }
456 }
457 }
458 return ret;
459}
460
Willy Tarreau8638f482012-09-18 18:01:17 +0200461/* Dumps all registered "bind" keywords to the <out> string pointer. The
462 * unsupported keywords are only dumped if their supported form was not
463 * found.
464 */
465void bind_dump_kws(char **out)
466{
467 struct bind_kw_list *kwl;
468 int index;
469
470 *out = NULL;
471 list_for_each_entry(kwl, &bind_keywords.list, list) {
472 for (index = 0; kwl->kw[index].kw != NULL; index++) {
473 if (kwl->kw[index].parse ||
474 bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200475 memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "",
476 kwl->scope,
Willy Tarreau8638f482012-09-18 18:01:17 +0200477 kwl->kw[index].kw,
Willy Tarreau51fb7652012-09-18 18:24:39 +0200478 kwl->kw[index].skip ? " <arg>" : "",
479 kwl->kw[index].parse ? "" : " (not supported)");
Willy Tarreau8638f482012-09-18 18:01:17 +0200480 }
481 }
482 }
483}
484
Willy Tarreau645513a2010-05-24 20:55:15 +0200485/************************************************************************/
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100486/* All supported sample and ACL keywords must be declared here. */
Willy Tarreau645513a2010-05-24 20:55:15 +0200487/************************************************************************/
488
Willy Tarreaua5e37562011-12-16 17:06:15 +0100489/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200490static int
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100491smp_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200492 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +0200493{
Willy Tarreauf853c462012-04-23 18:53:56 +0200494 smp->type = SMP_T_UINT;
495 smp->data.uint = l4->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200496 return 1;
497}
498
Willy Tarreaua5e37562011-12-16 17:06:15 +0100499/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200500static int
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100501smp_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200502 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +0200503{
Willy Tarreauf853c462012-04-23 18:53:56 +0200504 smp->type = SMP_T_UINT;
505 smp->data.uint = l4->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200506 return 1;
507}
508
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200509/* parse the "accept-proxy" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200510static 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 +0200511{
512 struct listener *l;
513
Willy Tarreau4348fad2012-09-20 16:48:07 +0200514 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200515 l->options |= LI_O_ACC_PROXY;
516
517 return 0;
518}
519
520/* parse the "backlog" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200521static 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 +0200522{
523 struct listener *l;
524 int val;
525
526 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200527 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200528 return ERR_ALERT | ERR_FATAL;
529 }
530
531 val = atol(args[cur_arg + 1]);
532 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200533 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200534 return ERR_ALERT | ERR_FATAL;
535 }
536
Willy Tarreau4348fad2012-09-20 16:48:07 +0200537 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200538 l->backlog = val;
539
540 return 0;
541}
542
543/* parse the "id" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200544static 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 +0200545{
546 struct eb32_node *node;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200547 struct listener *l, *new;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200548
Willy Tarreau4348fad2012-09-20 16:48:07 +0200549 if (conf->listeners.n != conf->listeners.p) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200550 memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200551 return ERR_ALERT | ERR_FATAL;
552 }
553
554 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200555 memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200556 return ERR_ALERT | ERR_FATAL;
557 }
558
Willy Tarreau4348fad2012-09-20 16:48:07 +0200559 new = LIST_NEXT(&conf->listeners, struct listener *, by_bind);
560 new->luid = atol(args[cur_arg + 1]);
561 new->conf.id.key = new->luid;
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200562
Willy Tarreau4348fad2012-09-20 16:48:07 +0200563 if (new->luid <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200564 memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200565 return ERR_ALERT | ERR_FATAL;
566 }
567
Willy Tarreau4348fad2012-09-20 16:48:07 +0200568 node = eb32_lookup(&px->conf.used_listener_id, new->luid);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200569 if (node) {
570 l = container_of(node, struct listener, conf.id);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200571 memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
572 args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
573 l->bind_conf->arg);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200574 return ERR_ALERT | ERR_FATAL;
575 }
576
Willy Tarreau4348fad2012-09-20 16:48:07 +0200577 eb32_insert(&px->conf.used_listener_id, &new->conf.id);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200578 return 0;
579}
580
581/* parse the "maxconn" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200582static 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 +0200583{
584 struct listener *l;
585 int val;
586
587 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200588 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200589 return ERR_ALERT | ERR_FATAL;
590 }
591
592 val = atol(args[cur_arg + 1]);
593 if (val <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200594 memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200595 return ERR_ALERT | ERR_FATAL;
596 }
597
Willy Tarreau4348fad2012-09-20 16:48:07 +0200598 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200599 l->maxconn = val;
600
601 return 0;
602}
603
604/* parse the "name" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200605static 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 +0200606{
607 struct listener *l;
608
609 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200610 memprintf(err, "'%s' : missing name", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200611 return ERR_ALERT | ERR_FATAL;
612 }
613
Willy Tarreau4348fad2012-09-20 16:48:07 +0200614 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200615 l->name = strdup(args[cur_arg + 1]);
616
617 return 0;
618}
619
620/* parse the "nice" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200621static 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 +0200622{
623 struct listener *l;
624 int val;
625
626 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200627 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200628 return ERR_ALERT | ERR_FATAL;
629 }
630
631 val = atol(args[cur_arg + 1]);
632 if (val < -1024 || val > 1024) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200633 memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200634 return ERR_ALERT | ERR_FATAL;
635 }
636
Willy Tarreau4348fad2012-09-20 16:48:07 +0200637 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200638 l->nice = val;
639
640 return 0;
641}
642
643
Willy Tarreau61612d42012-04-19 18:42:05 +0200644/* Note: must not be declared <const> as its list will be overwritten.
645 * Please take care of keeping this list alphabetically sorted.
646 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200647static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100648 { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
649 { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
650 { /* END */ },
651}};
652
653/* Note: must not be declared <const> as its list will be overwritten.
654 * Please take care of keeping this list alphabetically sorted.
655 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200656static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100657 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +0200658}};
659
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200660/* Note: must not be declared <const> as its list will be overwritten.
661 * Please take care of keeping this list alphabetically sorted, doing so helps
662 * all code contributors.
663 * Optional keywords are also declared with a NULL ->parse() function so that
664 * the config parser can report an appropriate error when a known keyword was
665 * not enabled.
666 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200667static struct bind_kw_list bind_kws = { "ALL", { }, {
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200668 { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */
669 { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */
670 { "id", bind_parse_id, 1 }, /* set id of listening socket */
671 { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */
672 { "name", bind_parse_name, 1 }, /* set name of listening socket */
673 { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100674 { /* END */ },
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200675}};
676
Willy Tarreau645513a2010-05-24 20:55:15 +0200677__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +0200678static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +0200679{
Willy Tarreau0ccb7442013-01-07 22:54:17 +0100680 sample_register_fetches(&smp_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200681 acl_register_keywords(&acl_kws);
Willy Tarreau3dcc3412012-09-18 17:17:28 +0200682 bind_register_keywords(&bind_kws);
Willy Tarreau645513a2010-05-24 20:55:15 +0200683}
684
685/*
686 * Local variables:
687 * c-indent-level: 8
688 * c-basic-offset: 8
689 * End:
690 */