blob: 7019606f0ddcf2a346963849d0a8985e787cb351 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Proxy variables and functions.
3 *
Willy Tarreaud825eef2007-05-12 22:35:00 +02004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +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
13#include <fcntl.h>
14#include <unistd.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010015#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <sys/types.h>
17#include <sys/socket.h>
18#include <sys/stat.h>
19
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/defaults.h>
21#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020022#include <common/config.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010023#include <common/errors.h>
Willy Tarreau4d2d0982007-05-14 00:39:29 +020024#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
27#include <types/global.h>
28#include <types/polling.h>
29
30#include <proto/client.h>
Alexandre Cassen87ea5482007-10-11 20:48:58 +020031#include <proto/backend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <proto/fd.h>
33#include <proto/log.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <proto/protocols.h>
35#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/proxy.h>
37
38
Willy Tarreaue6b98942007-10-29 01:09:36 +010039int listeners; /* # of proxy listeners, set by cfgparse, unset by maintain_proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +020040struct proxy *proxy = NULL; /* list of all existing proxies */
Willy Tarreaudcd47712007-11-04 23:35:08 +010041int next_pxid = 1; /* UUID assigned to next new proxy, 0 reserved */
Willy Tarreaubaaee002006-06-26 02:48:02 +020042
Willy Tarreau977b8e42006-12-29 14:19:17 +010043/*
Willy Tarreau816eb542007-11-04 07:04:43 +010044 * This function returns a string containing a name describing capabilities to
45 * report comprehensible error messages. Specifically, it will return the words
46 * "frontend", "backend", "ruleset" when appropriate, or "proxy" for all other
47 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +010048 */
Willy Tarreau816eb542007-11-04 07:04:43 +010049const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +010050{
Willy Tarreau816eb542007-11-04 07:04:43 +010051 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
52 if (cap & PR_CAP_FE)
53 return "frontend";
54 else if (cap & PR_CAP_BE)
55 return "backend";
56 else if (cap & PR_CAP_RS)
57 return "ruleset";
58 }
59 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +010060}
61
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010062/*
63 * This function returns a string containing the mode of the proxy in a format
64 * suitable for error messages.
65 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010066const char *proxy_mode_str(int mode) {
67
68 if (mode == PR_MODE_TCP)
69 return "tcp";
70 else if (mode == PR_MODE_HTTP)
71 return "http";
72 else if (mode == PR_MODE_HEALTH)
73 return "health";
74 else
75 return "unknown";
76}
77
Willy Tarreaue219db72007-12-03 01:30:13 +010078/* This function parses a "timeout" statement in a proxy section. It returns
79 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
80 * return zero, it may write an error message into the <err> buffer, for at
81 * most <errlen> bytes, trailing zero included. The trailing '\n' must not
82 * be written. The function must be called with <args> pointing to the first
83 * word after "timeout", with <proxy> pointing to the proxy being parsed, and
84 * <defpx> to the default proxy or NULL. As a special case for compatibility
85 * with older configs, it also accepts "{cli|srv|con}timeout" in args[0].
86 */
87int proxy_parse_timeout(const char **args, struct proxy *proxy,
88 struct proxy *defpx, char *err, int errlen)
89{
90 unsigned timeout;
91 int retval, cap;
92 const char *res, *name;
93 struct timeval *tv = NULL;
94 struct timeval *td = NULL;
95
96 retval = 0;
97 name = args[0];
98 if (!strcmp(args[0], "client") || !strcmp(args[0], "clitimeout")) {
99 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100100 tv = &proxy->timeout.client;
101 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100102 cap = PR_CAP_FE;
103 } else if (!strcmp(args[0], "tarpit")) {
104 tv = &proxy->timeout.tarpit;
105 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100106 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau036fae02008-01-06 13:24:40 +0100107 } else if (!strcmp(args[0], "http-request")) {
108 tv = &proxy->timeout.httpreq;
109 td = &defpx->timeout.httpreq;
110 cap = PR_CAP_FE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100111 } else if (!strcmp(args[0], "server") || !strcmp(args[0], "srvtimeout")) {
112 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100113 tv = &proxy->timeout.server;
114 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100115 cap = PR_CAP_BE;
116 } else if (!strcmp(args[0], "connect") || !strcmp(args[0], "contimeout")) {
117 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100118 tv = &proxy->timeout.connect;
119 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100120 cap = PR_CAP_BE;
121 } else if (!strcmp(args[0], "appsession")) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100122 tv = &proxy->timeout.appsession;
123 td = &defpx->timeout.appsession;
Willy Tarreaue219db72007-12-03 01:30:13 +0100124 cap = PR_CAP_BE;
125 } else if (!strcmp(args[0], "queue")) {
126 tv = &proxy->timeout.queue;
127 td = &defpx->timeout.queue;
128 cap = PR_CAP_BE;
129 } else {
Willy Tarreau036fae02008-01-06 13:24:40 +0100130 snprintf(err, errlen,
131 "timeout '%s': must be 'client', 'server', 'connect', "
132 "'appsession', 'queue', 'http-request' or 'tarpit'",
Willy Tarreaue219db72007-12-03 01:30:13 +0100133 args[0]);
134 return -1;
135 }
136
137 if (*args[1] == 0) {
138 snprintf(err, errlen, "%s timeout expects an integer value (in milliseconds)", name);
139 return -1;
140 }
141
142 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
143 if (res) {
144 snprintf(err, errlen, "unexpected character '%c' in %s timeout", *err, name);
145 return -1;
146 }
147
148 if (!(proxy->cap & cap)) {
149 snprintf(err, errlen, "%s timeout will be ignored because %s '%s' has no %s capability",
150 name, proxy_type_str(proxy), proxy->id,
151 (cap & PR_CAP_BE) ? "backend" : "frontend");
152 retval = 1;
153 }
154 else if (defpx && !__tv_iseq(tv, td)) {
155 snprintf(err, errlen, "overwriting %s timeout which was already specified", name);
156 retval = 1;
157 }
158
159 if (timeout)
160 __tv_from_ms(tv, timeout);
161 else
162 tv_eternity(tv);
163
164 return retval;
165}
166
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100167/*
168 * This function finds a proxy with matching name, mode and with satisfying
169 * capabilities. It also checks if there are more matching proxies with
170 * requested name as this often leads into unexpected situations.
171 */
172
173struct proxy *findproxy(const char *name, int mode, int cap) {
174
175 struct proxy *curproxy, *target=NULL;
176
177 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
178 if ((curproxy->cap & cap)!=cap || strcmp(curproxy->id, name))
179 continue;
180
181 if (curproxy->mode != mode) {
182 Alert("Unable to use proxy '%s' with wrong mode, required: %s, has: %s.\n",
183 name, proxy_mode_str(mode), proxy_mode_str(curproxy->mode));
184 Alert("You may want to use 'mode %s'.\n", proxy_mode_str(mode));
185 return NULL;
186 }
187
188 if (!target) {
189 target = curproxy;
190 continue;
191 }
192
Willy Tarreau816eb542007-11-04 07:04:43 +0100193 Alert("Refusing to use duplicated proxy '%s' with overlapping capabilities: %s/%s!\n",
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100194 name, proxy_type_str(curproxy), proxy_type_str(target));
195
196 return NULL;
197 }
198
199 return target;
200}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200201
202/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200203 * This function creates all proxy sockets. It should be done very early,
204 * typically before privileges are dropped. The sockets will be registered
205 * but not added to any fd_set, in order not to loose them across the fork().
206 * The proxies also start in IDLE state, meaning that it will be
207 * maintain_proxies that will finally complete their loading.
208 *
209 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
210 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200211 */
212int start_proxies(int verbose)
213{
214 struct proxy *curproxy;
215 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100216 int lerr, err = ERR_NONE;
217 int pxerr;
218 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200219
220 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
221 if (curproxy->state != PR_STNEW)
222 continue; /* already initialized */
223
224 pxerr = 0;
225 for (listener = curproxy->listen; listener != NULL; listener = listener->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100226 if (listener->state != LI_ASSIGNED)
227 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200228
Willy Tarreaue6b98942007-10-29 01:09:36 +0100229 lerr = tcp_bind_listener(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200230
Willy Tarreaue6b98942007-10-29 01:09:36 +0100231 /* errors are reported if <verbose> is set or if they are fatal */
232 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
233 if (lerr & ERR_ALERT)
234 Alert("Starting %s %s: %s\n",
235 proxy_type_str(curproxy), curproxy->id, msg);
236 else if (lerr & ERR_WARN)
237 Warning("Starting %s %s: %s\n",
238 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239 }
240
Willy Tarreaue6b98942007-10-29 01:09:36 +0100241 err |= lerr;
242 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100244 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100246 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200247 pxerr |= 1;
248 continue;
249 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250 }
251
252 if (!pxerr) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200253 curproxy->state = PR_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200254 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
255 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100256
257 if (err & ERR_ABORT)
258 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259 }
260
261 return err;
262}
263
264
265/*
266 * this function enables proxies when there are enough free sessions,
267 * or stops them when the table is full. It is designed to be called from the
Willy Tarreaud825eef2007-05-12 22:35:00 +0200268 * select_loop(). It returns the date of next expiration event during stop
269 * time, ETERNITY otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200270 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200271void maintain_proxies(struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200272{
273 struct proxy *p;
274 struct listener *l;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200275
276 p = proxy;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200277 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200278
279 /* if there are enough free sessions, we'll activate proxies */
280 if (actconn < global.maxconn) {
281 while (p) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100282 if (p->feconn < p->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200283 if (p->state == PR_STIDLE) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100284 for (l = p->listen; l != NULL; l = l->next)
285 enable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286 p->state = PR_STRUN;
287 }
288 }
289 else {
290 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100291 for (l = p->listen; l != NULL; l = l->next)
292 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293 p->state = PR_STIDLE;
294 }
295 }
296 p = p->next;
297 }
298 }
299 else { /* block all proxies */
300 while (p) {
301 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100302 for (l = p->listen; l != NULL; l = l->next)
303 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304 p->state = PR_STIDLE;
305 }
306 p = p->next;
307 }
308 }
309
310 if (stopping) {
311 p = proxy;
312 while (p) {
313 if (p->state != PR_STSTOPPED) {
314 int t;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200315 t = tv_ms_remain2(&now, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316 if (t == 0) {
317 Warning("Proxy %s stopped.\n", p->id);
318 send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id);
319
320 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100321 unbind_listener(l);
322 if (l->state >= LI_ASSIGNED) {
323 delete_listener(l);
324 listeners--;
325 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326 }
327 p->state = PR_STSTOPPED;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200328 /* try to free more memory */
329 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330 }
331 else {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200332 tv_bound(next, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333 }
334 }
335 p = p->next;
336 }
337 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200338 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339}
340
341
342/*
343 * this function disables health-check servers so that the process will quickly be ignored
344 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
345 * time will not be used since it would already not listen anymore to the socket.
346 */
347void soft_stop(void)
348{
349 struct proxy *p;
350
351 stopping = 1;
352 p = proxy;
353 tv_now(&now); /* else, the old time before select will be used */
354 while (p) {
355 if (p->state != PR_STSTOPPED) {
356 Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace);
357 send_log(p, LOG_WARNING, "Stopping proxy %s in %d ms.\n", p->id, p->grace);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200358 tv_ms_add(&p->stop_time, &now, p->grace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359 }
360 p = p->next;
361 }
362}
363
364
365/*
366 * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR.
367 * Solaris refuses either shutdown().
368 * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind.
369 * So a common validation path involves SHUT_WR && listen && SHUT_RD.
370 * If disabling at least one listener returns an error, then the proxy
371 * state is set to PR_STERROR because we don't know how to resume from this.
372 */
373void pause_proxy(struct proxy *p)
374{
375 struct listener *l;
376 for (l = p->listen; l != NULL; l = l->next) {
377 if (shutdown(l->fd, SHUT_WR) == 0 &&
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100378 listen(l->fd, p->backlog ? p->backlog : p->maxconn) == 0 &&
Willy Tarreaubaaee002006-06-26 02:48:02 +0200379 shutdown(l->fd, SHUT_RD) == 0) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200380 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381 if (p->state != PR_STERROR)
382 p->state = PR_STPAUSED;
383 }
384 else
385 p->state = PR_STERROR;
386 }
387}
388
389/*
390 * This function temporarily disables listening so that another new instance
391 * can start listening. It is designed to be called upon reception of a
392 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
393 * the proxy, or a SIGTTIN can be sent to listen again.
394 */
395void pause_proxies(void)
396{
397 int err;
398 struct proxy *p;
399
400 err = 0;
401 p = proxy;
402 tv_now(&now); /* else, the old time before select will be used */
403 while (p) {
404 if (p->state != PR_STERROR &&
405 p->state != PR_STSTOPPED &&
406 p->state != PR_STPAUSED) {
407 Warning("Pausing proxy %s.\n", p->id);
408 send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id);
409 pause_proxy(p);
410 if (p->state != PR_STPAUSED) {
411 err |= 1;
412 Warning("Proxy %s failed to enter pause mode.\n", p->id);
413 send_log(p, LOG_WARNING, "Proxy %s failed to enter pause mode.\n", p->id);
414 }
415 }
416 p = p->next;
417 }
418 if (err) {
419 Warning("Some proxies refused to pause, performing soft stop now.\n");
420 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
421 soft_stop();
422 }
423}
424
425
426/*
427 * This function reactivates listening. This can be used after a call to
428 * sig_pause(), for example when a new instance has failed starting up.
429 * It is designed to be called upon reception of a SIGTTIN.
430 */
431void listen_proxies(void)
432{
433 struct proxy *p;
434 struct listener *l;
435
436 p = proxy;
437 tv_now(&now); /* else, the old time before select will be used */
438 while (p) {
439 if (p->state == PR_STPAUSED) {
440 Warning("Enabling proxy %s.\n", p->id);
441 send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id);
442
443 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100444 if (listen(l->fd, p->backlog ? p->backlog : p->maxconn) == 0) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100445 if (actconn < global.maxconn && p->feconn < p->maxconn) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200446 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200447 p->state = PR_STRUN;
448 }
449 else
450 p->state = PR_STIDLE;
451 } else {
452 int port;
453
454 if (l->addr.ss_family == AF_INET6)
455 port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port);
456 else
457 port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
458
459 Warning("Port %d busy while trying to enable proxy %s.\n",
460 port, p->id);
461 send_log(p, LOG_WARNING, "Port %d busy while trying to enable proxy %s.\n",
462 port, p->id);
463 /* Another port might have been enabled. Let's stop everything. */
464 pause_proxy(p);
465 break;
466 }
467 }
468 }
469 p = p->next;
470 }
471}
472
473
474/*
475 * Local variables:
476 * c-indent-level: 8
477 * c-basic-offset: 8
478 * End:
479 */