blob: 0e864b2aa2cc9ef192553e17b81fbdf5e837958b [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreaud1d54542012-09-12 22:58:11 +02002 * Listener management functions.
Willy Tarreaudd815982007-10-16 12:25:14 +02003 *
Willy Tarreaud1d54542012-09-12 22:58:11 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Willy Tarreaudd815982007-10-16 12:25:14 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020013#include <errno.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010018#include <common/errors.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020019#include <common/mini-clist.h>
20#include <common/standard.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020021#include <common/time.h>
22
23#include <types/global.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020024#include <types/protocol.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020025
Willy Tarreau645513a2010-05-24 20:55:15 +020026#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010027#include <proto/fd.h>
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020028#include <proto/freq_ctr.h>
29#include <proto/log.h>
30#include <proto/task.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010031
Willy Tarreaudabf2e22007-10-28 21:59:24 +010032/* This function adds the specified listener's file descriptor to the polling
33 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
34 * LI_FULL state depending on its number of connections.
35 */
36void enable_listener(struct listener *listener)
37{
38 if (listener->state == LI_LISTEN) {
39 if (listener->nbconn < listener->maxconn) {
Willy Tarreau49b046d2012-08-09 12:11:58 +020040 fd_want_recv(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010041 listener->state = LI_READY;
42 } else {
43 listener->state = LI_FULL;
44 }
45 }
46}
47
48/* This function removes the specified listener's file descriptor from the
49 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
50 * enters LI_LISTEN.
51 */
52void disable_listener(struct listener *listener)
53{
54 if (listener->state < LI_READY)
55 return;
56 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +020057 fd_stop_recv(listener->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020058 if (listener->state == LI_LIMITED)
59 LIST_DEL(&listener->wait_queue);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010060 listener->state = LI_LISTEN;
61}
62
Willy Tarreaube58c382011-07-24 18:28:10 +020063/* This function tries to temporarily disable a listener, depending on the OS
64 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
65 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
66 * closes upon SHUT_WR and refuses to rebind. So a common validation path
67 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
68 * is disabled. It normally returns non-zero, unless an error is reported.
69 */
70int pause_listener(struct listener *l)
71{
72 if (l->state <= LI_PAUSED)
73 return 1;
74
75 if (shutdown(l->fd, SHUT_WR) != 0)
76 return 0; /* Solaris dies here */
77
78 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
79 return 0; /* OpenBSD dies here */
80
81 if (shutdown(l->fd, SHUT_RD) != 0)
82 return 0; /* should always be OK */
83
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020084 if (l->state == LI_LIMITED)
85 LIST_DEL(&l->wait_queue);
86
Willy Tarreau49b046d2012-08-09 12:11:58 +020087 fd_stop_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +020088 l->state = LI_PAUSED;
89 return 1;
90}
91
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020092/* This function tries to resume a temporarily disabled listener. Paused, full,
93 * limited and disabled listeners are handled, which means that this function
94 * may replace enable_listener(). The resulting state will either be LI_READY
95 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreaube58c382011-07-24 18:28:10 +020096 */
97int resume_listener(struct listener *l)
98{
99 if (l->state < LI_PAUSED)
100 return 0;
101
102 if (l->state == LI_PAUSED &&
103 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
104 return 0;
105
106 if (l->state == LI_READY)
107 return 1;
108
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200109 if (l->state == LI_LIMITED)
110 LIST_DEL(&l->wait_queue);
111
Willy Tarreaube58c382011-07-24 18:28:10 +0200112 if (l->nbconn >= l->maxconn) {
113 l->state = LI_FULL;
114 return 1;
115 }
116
Willy Tarreau49b046d2012-08-09 12:11:58 +0200117 fd_want_recv(l->fd);
Willy Tarreaube58c382011-07-24 18:28:10 +0200118 l->state = LI_READY;
119 return 1;
120}
121
Willy Tarreau62793712011-07-24 19:23:38 +0200122/* Marks a ready listener as full so that the session code tries to re-enable
123 * it upon next close() using resume_listener().
124 */
125void listener_full(struct listener *l)
126{
127 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200128 if (l->state == LI_LIMITED)
129 LIST_DEL(&l->wait_queue);
130
Willy Tarreau49b046d2012-08-09 12:11:58 +0200131 fd_stop_recv(l->fd);
Willy Tarreau62793712011-07-24 19:23:38 +0200132 l->state = LI_FULL;
133 }
134}
135
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200136/* Marks a ready listener as limited so that we only try to re-enable it when
137 * resources are free again. It will be queued into the specified queue.
138 */
139void limit_listener(struct listener *l, struct list *list)
140{
141 if (l->state == LI_READY) {
142 LIST_ADDQ(list, &l->wait_queue);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200143 fd_stop_recv(l->fd);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200144 l->state = LI_LIMITED;
145 }
146}
147
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100148/* This function adds all of the protocol's listener's file descriptors to the
149 * polling lists when they are in the LI_LISTEN state. It is intended to be
150 * used as a protocol's generic enable_all() primitive, for use after the
151 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
152 * their number of connections. It always returns ERR_NONE.
153 */
154int enable_all_listeners(struct protocol *proto)
155{
156 struct listener *listener;
157
158 list_for_each_entry(listener, &proto->listeners, proto_list)
159 enable_listener(listener);
160 return ERR_NONE;
161}
162
163/* This function removes all of the protocol's listener's file descriptors from
164 * the polling lists when they are in the LI_READY or LI_FULL states. It is
165 * intended to be used as a protocol's generic disable_all() primitive. It puts
166 * the listeners into LI_LISTEN, and always returns ERR_NONE.
167 */
168int disable_all_listeners(struct protocol *proto)
169{
170 struct listener *listener;
171
172 list_for_each_entry(listener, &proto->listeners, proto_list)
173 disable_listener(listener);
174 return ERR_NONE;
175}
176
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200177/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
178void dequeue_all_listeners(struct list *list)
179{
180 struct listener *listener, *l_back;
181
182 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
183 /* This cannot fail because the listeners are by definition in
184 * the LI_LIMITED state. The function also removes the entry
185 * from the queue.
186 */
187 resume_listener(listener);
188 }
189}
190
Willy Tarreaub648d632007-10-28 22:13:50 +0100191/* This function closes the listening socket for the specified listener,
192 * provided that it's already in a listening state. The listener enters the
193 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
194 * to be used as a generic function for standard protocols.
195 */
196int unbind_listener(struct listener *listener)
197{
198 if (listener->state == LI_READY)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200199 fd_stop_recv(listener->fd);
Willy Tarreaub648d632007-10-28 22:13:50 +0100200
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200201 if (listener->state == LI_LIMITED)
202 LIST_DEL(&listener->wait_queue);
203
Willy Tarreaube58c382011-07-24 18:28:10 +0200204 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100205 fd_delete(listener->fd);
206 listener->state = LI_ASSIGNED;
207 }
208 return ERR_NONE;
209}
210
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100211/* This function closes all listening sockets bound to the protocol <proto>,
212 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
213 * detach them from the protocol. It always returns ERR_NONE.
214 */
215int unbind_all_listeners(struct protocol *proto)
216{
217 struct listener *listener;
218
219 list_for_each_entry(listener, &proto->listeners, proto_list)
220 unbind_listener(listener);
221 return ERR_NONE;
222}
223
Willy Tarreau1a64d162007-10-28 22:26:05 +0100224/* Delete a listener from its protocol's list of listeners. The listener's
225 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
226 * number of listeners is updated. Note that the listener must have previously
227 * been unbound. This is the generic function to use to remove a listener.
228 */
229void delete_listener(struct listener *listener)
230{
231 if (listener->state != LI_ASSIGNED)
232 return;
233 listener->state = LI_INIT;
234 LIST_DEL(&listener->proto_list);
235 listener->proto->nb_listeners--;
236}
237
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200238/* This function is called on a read event from a listening socket, corresponding
239 * to an accept. It tries to accept as many connections as possible, and for each
240 * calls the listener's accept handler (generally the frontend's accept handler).
241 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200242void listener_accept(int fd)
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200243{
244 struct listener *l = fdtab[fd].owner;
245 struct proxy *p = l->frontend;
246 int max_accept = global.tune.maxaccept;
247 int cfd;
248 int ret;
249
250 if (unlikely(l->nbconn >= l->maxconn)) {
251 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200252 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200253 }
254
255 if (global.cps_lim && !(l->options & LI_O_UNLIMITED)) {
256 int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0);
257
258 if (unlikely(!max)) {
259 /* frontend accept rate limit was reached */
260 limit_listener(l, &global_listener_queue);
261 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 +0200262 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200263 }
264
265 if (max_accept > max)
266 max_accept = max;
267 }
268
269 if (p && p->fe_sps_lim) {
270 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
271
272 if (unlikely(!max)) {
273 /* frontend accept rate limit was reached */
274 limit_listener(l, &p->listener_queue);
275 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 +0200276 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200277 }
278
279 if (max_accept > max)
280 max_accept = max;
281 }
282
283 /* Note: if we fail to allocate a connection because of configured
284 * limits, we'll schedule a new attempt worst 1 second later in the
285 * worst case. If we fail due to system limits or temporary resource
286 * shortage, we try again 100ms later in the worst case.
287 */
288 while (max_accept--) {
289 struct sockaddr_storage addr;
290 socklen_t laddr = sizeof(addr);
291
292 if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) {
293 limit_listener(l, &global_listener_queue);
294 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200295 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200296 }
297
298 if (unlikely(p && p->feconn >= p->maxconn)) {
299 limit_listener(l, &p->listener_queue);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200300 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200301 }
302
303 cfd = accept(fd, (struct sockaddr *)&addr, &laddr);
304 if (unlikely(cfd == -1)) {
305 switch (errno) {
306 case EAGAIN:
307 case EINTR:
308 case ECONNABORTED:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200309 fd_poll_recv(fd);
310 return; /* nothing more to accept */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200311 case ENFILE:
312 if (p)
313 send_log(p, LOG_EMERG,
314 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
315 p->id, maxfd);
316 limit_listener(l, &global_listener_queue);
317 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200318 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200319 case EMFILE:
320 if (p)
321 send_log(p, LOG_EMERG,
322 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
323 p->id, maxfd);
324 limit_listener(l, &global_listener_queue);
325 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200326 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200327 case ENOBUFS:
328 case ENOMEM:
329 if (p)
330 send_log(p, LOG_EMERG,
331 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
332 p->id, maxfd);
333 limit_listener(l, &global_listener_queue);
334 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200335 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200336 default:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200337 /* unexpected result, let's go back to poll */
338 fd_poll_recv(fd);
339 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200340 }
341 }
342
Willy Tarreaufe7f1ea2012-05-20 19:22:25 +0200343 /* if this connection comes from a known monitoring system, we want to ignore
344 * it as soon as possible, which means closing it immediately if it is only a
345 * TCP-based monitoring check.
346 */
347 if (unlikely((l->options & LI_O_CHK_MONNET) &&
348 (p->mode == PR_MODE_TCP) &&
349 addr.ss_family == AF_INET &&
350 (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
351 close(cfd);
352 continue;
353 }
354
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200355 if (unlikely(cfd >= global.maxsock)) {
356 send_log(p, LOG_EMERG,
357 "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n",
358 p->id);
359 close(cfd);
360 limit_listener(l, &global_listener_queue);
361 task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200362 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200363 }
364
365 /* increase the per-process number of cumulated connections */
366 if (!(l->options & LI_O_UNLIMITED)) {
367 update_freq_ctr(&global.conn_per_sec, 1);
368 if (global.conn_per_sec.curr_ctr > global.cps_max)
369 global.cps_max = global.conn_per_sec.curr_ctr;
370 actconn++;
371 }
372
373 jobs++;
374 totalconn++;
375 l->nbconn++;
376
377 if (l->counters) {
378 if (l->nbconn > l->counters->conn_max)
379 l->counters->conn_max = l->nbconn;
380 }
381
382 ret = l->accept(l, cfd, &addr);
383 if (unlikely(ret <= 0)) {
384 /* The connection was closed by session_accept(). Either
385 * we just have to ignore it (ret == 0) or it's a critical
386 * error due to a resource shortage, and we must stop the
387 * listener (ret < 0).
388 */
389 if (!(l->options & LI_O_UNLIMITED))
390 actconn--;
391 jobs--;
392 l->nbconn--;
393 if (ret == 0) /* successful termination */
394 continue;
395
396 limit_listener(l, &global_listener_queue);
397 task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200398 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200399 }
400
401 if (l->nbconn >= l->maxconn) {
402 listener_full(l);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200403 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200404 }
405
Willy Tarreauaece46a2012-07-06 12:25:58 +0200406 } /* end of while (max_accept--) */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200407
Willy Tarreauaece46a2012-07-06 12:25:58 +0200408 /* we've exhausted max_accept, so there is no need to poll again */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200409 return;
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200410}
411
Willy Tarreau645513a2010-05-24 20:55:15 +0200412/************************************************************************/
413/* All supported ACL keywords must be declared here. */
414/************************************************************************/
415
Willy Tarreaua5e37562011-12-16 17:06:15 +0100416/* set temp integer to the number of connexions to the same listening socket */
Willy Tarreau645513a2010-05-24 20:55:15 +0200417static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200418acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200419 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +0200420{
Willy Tarreauf853c462012-04-23 18:53:56 +0200421 smp->type = SMP_T_UINT;
422 smp->data.uint = l4->listener->nbconn;
Willy Tarreau645513a2010-05-24 20:55:15 +0200423 return 1;
424}
425
Willy Tarreaua5e37562011-12-16 17:06:15 +0100426/* set temp integer to the id of the socket (listener) */
Willy Tarreau645513a2010-05-24 20:55:15 +0200427static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200428acl_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200429 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +0200430{
Willy Tarreauf853c462012-04-23 18:53:56 +0200431 smp->type = SMP_T_UINT;
432 smp->data.uint = l4->listener->luid;
Willy Tarreau645513a2010-05-24 20:55:15 +0200433 return 1;
434}
435
Willy Tarreau61612d42012-04-19 18:42:05 +0200436/* Note: must not be declared <const> as its list will be overwritten.
437 * Please take care of keeping this list alphabetically sorted.
438 */
Willy Tarreau645513a2010-05-24 20:55:15 +0200439static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +0200440 { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING, 0 },
441 { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +0200442 { NULL, NULL, NULL, NULL },
443}};
444
445__attribute__((constructor))
Willy Tarreaud1d54542012-09-12 22:58:11 +0200446static void __listener_init(void)
Willy Tarreau645513a2010-05-24 20:55:15 +0200447{
448 acl_register_keywords(&acl_kws);
449}
450
451/*
452 * Local variables:
453 * c-indent-level: 8
454 * c-basic-offset: 8
455 * End:
456 */