blob: adbe44d5c800d462bf6a7e3ed21343562867ea17 [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreau645513a2010-05-24 20:55:15 +02002 * Protocol registration and listener management functions.
Willy Tarreaudd815982007-10-16 12:25:14 +02003 *
Willy Tarreau645513a2010-05-24 20:55:15 +02004 * Copyright 2000-2010 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 Tarreaudd815982007-10-16 12:25:14 +020024
Willy Tarreau645513a2010-05-24 20:55:15 +020025#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010026#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020027#include <proto/freq_ctr.h>
28#include <proto/log.h>
29#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010030
Willy Tarreaudd815982007-10-16 12:25:14 +020031/* List head of all registered protocols */
32static struct list protocols = LIST_HEAD_INIT(protocols);
33
Willy Tarreaudabf2e22007-10-28 21:59:24 +010034/* This function adds the specified listener's file descriptor to the polling
35 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
36 * LI_FULL state depending on its number of connections.
37 */
38void enable_listener(struct listener *listener)
39{
40 if (listener->state == LI_LISTEN) {
41 if (listener->nbconn < listener->maxconn) {
42 EV_FD_SET(listener->fd, DIR_RD);
43 listener->state = LI_READY;
44 } else {
45 listener->state = LI_FULL;
46 }
47 }
48}
49
50/* This function removes the specified listener's file descriptor from the
51 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
52 * enters LI_LISTEN.
53 */
54void disable_listener(struct listener *listener)
55{
56 if (listener->state < LI_READY)
57 return;
58 if (listener->state == LI_READY)
59 EV_FD_CLR(listener->fd, DIR_RD);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020060 if (listener->state == LI_LIMITED)
61 LIST_DEL(&listener->wait_queue);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010062 listener->state = LI_LISTEN;
63}
64
Willy Tarreaube58c382011-07-24 18:28:10 +020065/* This function tries to temporarily disable a listener, depending on the OS
66 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
67 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
68 * closes upon SHUT_WR and refuses to rebind. So a common validation path
69 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
70 * is disabled. It normally returns non-zero, unless an error is reported.
71 */
72int pause_listener(struct listener *l)
73{
74 if (l->state <= LI_PAUSED)
75 return 1;
76
77 if (shutdown(l->fd, SHUT_WR) != 0)
78 return 0; /* Solaris dies here */
79
80 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
81 return 0; /* OpenBSD dies here */
82
83 if (shutdown(l->fd, SHUT_RD) != 0)
84 return 0; /* should always be OK */
85
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020086 if (l->state == LI_LIMITED)
87 LIST_DEL(&l->wait_queue);
88
Willy Tarreaube58c382011-07-24 18:28:10 +020089 EV_FD_CLR(l->fd, DIR_RD);
90 l->state = LI_PAUSED;
91 return 1;
92}
93
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020094/* This function tries to resume a temporarily disabled listener. Paused, full,
95 * limited and disabled listeners are handled, which means that this function
96 * may replace enable_listener(). The resulting state will either be LI_READY
97 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreaube58c382011-07-24 18:28:10 +020098 */
99int resume_listener(struct listener *l)
100{
101 if (l->state < LI_PAUSED)
102 return 0;
103
104 if (l->state == LI_PAUSED &&
105 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
106 return 0;
107
108 if (l->state == LI_READY)
109 return 1;
110
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200111 if (l->state == LI_LIMITED)
112 LIST_DEL(&l->wait_queue);
113
Willy Tarreaube58c382011-07-24 18:28:10 +0200114 if (l->nbconn >= l->maxconn) {
115 l->state = LI_FULL;
116 return 1;
117 }
118
119 EV_FD_SET(l->fd, DIR_RD);
120 l->state = LI_READY;
121 return 1;
122}
123
Willy Tarreau62793712011-07-24 19:23:38 +0200124/* Marks a ready listener as full so that the session code tries to re-enable
125 * it upon next close() using resume_listener().
126 */
127void listener_full(struct listener *l)
128{
129 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200130 if (l->state == LI_LIMITED)
131 LIST_DEL(&l->wait_queue);
132
Willy Tarreau62793712011-07-24 19:23:38 +0200133 EV_FD_CLR(l->fd, DIR_RD);
134 l->state = LI_FULL;
135 }
136}
137
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200138/* Marks a ready listener as limited so that we only try to re-enable it when
139 * resources are free again. It will be queued into the specified queue.
140 */
141void limit_listener(struct listener *l, struct list *list)
142{
143 if (l->state == LI_READY) {
144 LIST_ADDQ(list, &l->wait_queue);
145 EV_FD_CLR(l->fd, DIR_RD);
146 l->state = LI_LIMITED;
147 }
148}
149
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100150/* This function adds all of the protocol's listener's file descriptors to the
151 * polling lists when they are in the LI_LISTEN state. It is intended to be
152 * used as a protocol's generic enable_all() primitive, for use after the
153 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
154 * their number of connections. It always returns ERR_NONE.
155 */
156int enable_all_listeners(struct protocol *proto)
157{
158 struct listener *listener;
159
160 list_for_each_entry(listener, &proto->listeners, proto_list)
161 enable_listener(listener);
162 return ERR_NONE;
163}
164
165/* This function removes all of the protocol's listener's file descriptors from
166 * the polling lists when they are in the LI_READY or LI_FULL states. It is
167 * intended to be used as a protocol's generic disable_all() primitive. It puts
168 * the listeners into LI_LISTEN, and always returns ERR_NONE.
169 */
170int disable_all_listeners(struct protocol *proto)
171{
172 struct listener *listener;
173
174 list_for_each_entry(listener, &proto->listeners, proto_list)
175 disable_listener(listener);
176 return ERR_NONE;
177}
178
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200179/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
180void dequeue_all_listeners(struct list *list)
181{
182 struct listener *listener, *l_back;
183
184 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
185 /* This cannot fail because the listeners are by definition in
186 * the LI_LIMITED state. The function also removes the entry
187 * from the queue.
188 */
189 resume_listener(listener);
190 }
191}
192
Willy Tarreaub648d632007-10-28 22:13:50 +0100193/* This function closes the listening socket for the specified listener,
194 * provided that it's already in a listening state. The listener enters the
195 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
196 * to be used as a generic function for standard protocols.
197 */
198int unbind_listener(struct listener *listener)
199{
200 if (listener->state == LI_READY)
201 EV_FD_CLR(listener->fd, DIR_RD);
202
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200203 if (listener->state == LI_LIMITED)
204 LIST_DEL(&listener->wait_queue);
205
Willy Tarreaube58c382011-07-24 18:28:10 +0200206 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100207 fd_delete(listener->fd);
208 listener->state = LI_ASSIGNED;
209 }
210 return ERR_NONE;
211}
212
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100213/* This function closes all listening sockets bound to the protocol <proto>,
214 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
215 * detach them from the protocol. It always returns ERR_NONE.
216 */
217int unbind_all_listeners(struct protocol *proto)
218{
219 struct listener *listener;
220
221 list_for_each_entry(listener, &proto->listeners, proto_list)
222 unbind_listener(listener);
223 return ERR_NONE;
224}
225
Willy Tarreau1a64d162007-10-28 22:26:05 +0100226/* Delete a listener from its protocol's list of listeners. The listener's
227 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
228 * number of listeners is updated. Note that the listener must have previously
229 * been unbound. This is the generic function to use to remove a listener.
230 */
231void delete_listener(struct listener *listener)
232{
233 if (listener->state != LI_ASSIGNED)
234 return;
235 listener->state = LI_INIT;
236 LIST_DEL(&listener->proto_list);
237 listener->proto->nb_listeners--;
238}
239
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200240/* This function is called on a read event from a listening socket, corresponding
241 * to an accept. It tries to accept as many connections as possible, and for each
242 * calls the listener's accept handler (generally the frontend's accept handler).
243 */
244int listener_accept(int fd)
245{
246 struct listener *l = fdtab[fd].owner;
247 struct proxy *p = l->frontend;
248 int max_accept = global.tune.maxaccept;
249 int cfd;
250 int ret;
251
252 if (unlikely(l->nbconn >= l->maxconn)) {
253 listener_full(l);
254 return 0;
255 }
256
257 if (global.cps_lim && !(l->options & LI_O_UNLIMITED)) {
258 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
259
260 if (unlikely(!max)) {
261 /* frontend accept rate limit was reached */
262 limit_listener(l, &global_listener_queue);
263 task_schedule(global_listener_queue_task, tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0)));
264 return 0;
265 }
266
267 if (max_accept > max)
268 max_accept = max;
269 }
270
271 if (p && p->fe_sps_lim) {
272 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
273
274 if (unlikely(!max)) {
275 /* frontend accept rate limit was reached */
276 limit_listener(l, &p->listener_queue);
277 task_schedule(p->task, tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0)));
278 return 0;
279 }
280
281 if (max_accept > max)
282 max_accept = max;
283 }
284
285 /* Note: if we fail to allocate a connection because of configured
286 * limits, we'll schedule a new attempt worst 1 second later in the
287 * worst case. If we fail due to system limits or temporary resource
288 * shortage, we try again 100ms later in the worst case.
289 */
290 while (max_accept--) {
291 struct sockaddr_storage addr;
292 socklen_t laddr = sizeof(addr);
293
294 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
295 limit_listener(l, &global_listener_queue);
296 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
297 return 0;
298 }
299
300 if (unlikely(p && p->feconn >= p->maxconn)) {
301 limit_listener(l, &p->listener_queue);
302 return 0;
303 }
304
305 cfd = accept(fd, (struct sockaddr *)&addr, &laddr);
306 if (unlikely(cfd == -1)) {
307 switch (errno) {
308 case EAGAIN:
309 case EINTR:
310 case ECONNABORTED:
311 return 0; /* nothing more to accept */
312 case ENFILE:
313 if (p)
314 send_log(p, LOG_EMERG,
315 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
316 p->id, maxfd);
317 limit_listener(l, &global_listener_queue);
318 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
319 return 0;
320 case EMFILE:
321 if (p)
322 send_log(p, LOG_EMERG,
323 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
324 p->id, maxfd);
325 limit_listener(l, &global_listener_queue);
326 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
327 return 0;
328 case ENOBUFS:
329 case ENOMEM:
330 if (p)
331 send_log(p, LOG_EMERG,
332 "Proxy %s reached system memory limit at %d sockets. 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 */
336 return 0;
337 default:
338 return 0;
339 }
340 }
341
Willy Tarreaufe7f1ea2012-05-20 19:22:25 +0200342 /* if this connection comes from a known monitoring system, we want to ignore
343 * it as soon as possible, which means closing it immediately if it is only a
344 * TCP-based monitoring check.
345 */
346 if (unlikely((l->options & LI_O_CHK_MONNET) &&
347 (p->mode == PR_MODE_TCP) &&
348 addr.ss_family == AF_INET &&
349 (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
350 close(cfd);
351 continue;
352 }
353
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200354 if (unlikely(cfd >= global.maxsock)) {
355 send_log(p, LOG_EMERG,
356 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
357 p->id);
358 close(cfd);
359 limit_listener(l, &global_listener_queue);
360 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
361 return 0;
362 }
363
364 /* increase the per-process number of cumulated connections */
365 if (!(l->options & LI_O_UNLIMITED)) {
366 update_freq_ctr(&global.conn_per_sec, 1);
367 if (global.conn_per_sec.curr_ctr > global.cps_max)
368 global.cps_max = global.conn_per_sec.curr_ctr;
369 actconn++;
370 }
371
372 jobs++;
373 totalconn++;
374 l->nbconn++;
375
376 if (l->counters) {
377 if (l->nbconn > l->counters->conn_max)
378 l->counters->conn_max = l->nbconn;
379 }
380
381 ret = l->accept(l, cfd, &addr);
382 if (unlikely(ret <= 0)) {
383 /* The connection was closed by session_accept(). Either
384 * we just have to ignore it (ret == 0) or it's a critical
385 * error due to a resource shortage, and we must stop the
386 * listener (ret < 0).
387 */
388 if (!(l->options & LI_O_UNLIMITED))
389 actconn--;
390 jobs--;
391 l->nbconn--;
392 if (ret == 0) /* successful termination */
393 continue;
394
395 limit_listener(l, &global_listener_queue);
396 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
397 return 0;
398 }
399
400 if (l->nbconn >= l->maxconn) {
401 listener_full(l);
402 return 0;
403 }
404
405 } /* end of while (p->feconn < p->maxconn) */
406
407 return 0;
408}
409
Willy Tarreaudd815982007-10-16 12:25:14 +0200410/* Registers the protocol <proto> */
411void protocol_register(struct protocol *proto)
412{
413 LIST_ADDQ(&protocols, &proto->list);
414}
415
416/* Unregisters the protocol <proto>. Note that all listeners must have
417 * previously been unbound.
418 */
419void protocol_unregister(struct protocol *proto)
420{
421 LIST_DEL(&proto->list);
422 LIST_INIT(&proto->list);
423}
424
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100425/* binds all listeners of all registered protocols. Returns a composition
Willy Tarreaudd815982007-10-16 12:25:14 +0200426 * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
427 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200428int protocol_bind_all(char *errmsg, int errlen)
Willy Tarreaudd815982007-10-16 12:25:14 +0200429{
430 struct protocol *proto;
431 int err;
432
433 err = 0;
434 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200435 if (proto->bind_all) {
436 err |= proto->bind_all(proto, errmsg, errlen);
437 if ( err & ERR_ABORT )
438 break;
439 }
Willy Tarreaudd815982007-10-16 12:25:14 +0200440 }
441 return err;
442}
443
444/* unbinds all listeners of all registered protocols. They are also closed.
445 * This must be performed before calling exit() in order to get a chance to
446 * remove file-system based sockets and pipes.
Emeric Bruncf20bf12010-10-22 16:06:11 +0200447 * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT.
Willy Tarreaudd815982007-10-16 12:25:14 +0200448 */
449int protocol_unbind_all(void)
450{
451 struct protocol *proto;
452 int err;
453
454 err = 0;
455 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200456 if (proto->unbind_all) {
Willy Tarreaudd815982007-10-16 12:25:14 +0200457 err |= proto->unbind_all(proto);
Emeric Bruncf20bf12010-10-22 16:06:11 +0200458 }
Willy Tarreaudd815982007-10-16 12:25:14 +0200459 }
460 return err;
461}
462
463/* enables all listeners of all registered protocols. This is intended to be
464 * used after a fork() to enable reading on all file descriptors. Returns a
465 * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
466 */
467int protocol_enable_all(void)
468{
469 struct protocol *proto;
470 int err;
471
472 err = 0;
473 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200474 if (proto->enable_all) {
Willy Tarreaudd815982007-10-16 12:25:14 +0200475 err |= proto->enable_all(proto);
Emeric Bruncf20bf12010-10-22 16:06:11 +0200476 }
Willy Tarreaudd815982007-10-16 12:25:14 +0200477 }
478 return err;
479}
480
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100481/* disables all listeners of all registered protocols. This may be used before
482 * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE,
483 * ERR_RETRYABLE, ERR_FATAL.
484 */
485int protocol_disable_all(void)
486{
487 struct protocol *proto;
488 int err;
489
490 err = 0;
491 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200492 if (proto->disable_all) {
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100493 err |= proto->disable_all(proto);
Emeric Bruncf20bf12010-10-22 16:06:11 +0200494 }
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100495 }
496 return err;
497}
498
Willy Tarreau26d8c592012-05-07 18:12:14 +0200499/* Returns the protocol handler for socket family <family> or NULL if not found */
500struct protocol *protocol_by_family(int family)
501{
502 struct protocol *proto;
503
504 list_for_each_entry(proto, &protocols, list) {
505 if (proto->sock_domain == family)
506 return proto;
507 }
508 return NULL;
509}
510
Willy Tarreau645513a2010-05-24 20:55:15 +0200511/************************************************************************/
512/* All supported ACL keywords must be declared here. */
513/************************************************************************/
514
Willy Tarreaua5e37562011-12-16 17:06:15 +0100515/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200516static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200517acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200518 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +0200519{
Willy Tarreauf853c462012-04-23 18:53:56 +0200520 smp->type = SMP_T_UINT;
521 smp->data.uint = l4->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200522 return 1;
523}
524
Willy Tarreaua5e37562011-12-16 17:06:15 +0100525/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200526static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200527acl_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200528 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +0200529{
Willy Tarreauf853c462012-04-23 18:53:56 +0200530 smp->type = SMP_T_UINT;
531 smp->data.uint = l4->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200532 return 1;
533}
534
Willy Tarreau61612d42012-04-19 18:42:05 +0200535/* Note: must not be declared <const> as its list will be overwritten.
536 * Please take care of keeping this list alphabetically sorted.
537 */
Willy Tarreau645513a2010-05-24 20:55:15 +0200538static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +0200539 { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING, 0 },
540 { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +0200541 { NULL, NULL, NULL, NULL },
542}};
543
544__attribute__((constructor))
545static void __protocols_init(void)
546{
547 acl_register_keywords(&acl_kws);
548}
549
550/*
551 * Local variables:
552 * c-indent-level: 8
553 * c-basic-offset: 8
554 * End:
555 */