blob: 402fc88cf0757f637dddbc447c90074610961890 [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;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100121 } else if (!strcmp(args[0], "check")) {
122 tv = &proxy->timeout.check;
123 td = &defpx->timeout.check;
124 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100125 } else if (!strcmp(args[0], "appsession")) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100126 tv = &proxy->timeout.appsession;
127 td = &defpx->timeout.appsession;
Willy Tarreaue219db72007-12-03 01:30:13 +0100128 cap = PR_CAP_BE;
129 } else if (!strcmp(args[0], "queue")) {
130 tv = &proxy->timeout.queue;
131 td = &defpx->timeout.queue;
132 cap = PR_CAP_BE;
133 } else {
Willy Tarreau036fae02008-01-06 13:24:40 +0100134 snprintf(err, errlen,
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100135 "timeout '%s': must be 'client', 'server', 'connect', 'check', "
Willy Tarreau036fae02008-01-06 13:24:40 +0100136 "'appsession', 'queue', 'http-request' or 'tarpit'",
Willy Tarreaue219db72007-12-03 01:30:13 +0100137 args[0]);
138 return -1;
139 }
140
141 if (*args[1] == 0) {
142 snprintf(err, errlen, "%s timeout expects an integer value (in milliseconds)", name);
143 return -1;
144 }
145
146 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
147 if (res) {
148 snprintf(err, errlen, "unexpected character '%c' in %s timeout", *err, name);
149 return -1;
150 }
151
152 if (!(proxy->cap & cap)) {
153 snprintf(err, errlen, "%s timeout will be ignored because %s '%s' has no %s capability",
154 name, proxy_type_str(proxy), proxy->id,
155 (cap & PR_CAP_BE) ? "backend" : "frontend");
156 retval = 1;
157 }
158 else if (defpx && !__tv_iseq(tv, td)) {
159 snprintf(err, errlen, "overwriting %s timeout which was already specified", name);
160 retval = 1;
161 }
162
163 if (timeout)
164 __tv_from_ms(tv, timeout);
165 else
166 tv_eternity(tv);
167
168 return retval;
169}
170
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100171/*
172 * This function finds a proxy with matching name, mode and with satisfying
173 * capabilities. It also checks if there are more matching proxies with
174 * requested name as this often leads into unexpected situations.
175 */
176
177struct proxy *findproxy(const char *name, int mode, int cap) {
178
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100179 struct proxy *curproxy, *target = NULL;
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100180
181 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
182 if ((curproxy->cap & cap)!=cap || strcmp(curproxy->id, name))
183 continue;
184
185 if (curproxy->mode != mode) {
186 Alert("Unable to use proxy '%s' with wrong mode, required: %s, has: %s.\n",
187 name, proxy_mode_str(mode), proxy_mode_str(curproxy->mode));
188 Alert("You may want to use 'mode %s'.\n", proxy_mode_str(mode));
189 return NULL;
190 }
191
192 if (!target) {
193 target = curproxy;
194 continue;
195 }
196
Willy Tarreau816eb542007-11-04 07:04:43 +0100197 Alert("Refusing to use duplicated proxy '%s' with overlapping capabilities: %s/%s!\n",
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100198 name, proxy_type_str(curproxy), proxy_type_str(target));
199
200 return NULL;
201 }
202
203 return target;
204}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205
206/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100207 * This function finds a server with matching name within selected proxy.
208 * It also checks if there are more matching servers with
209 * requested name as this often leads into unexpected situations.
210 */
211
212struct server *findserver(const struct proxy *px, const char *name) {
213
214 struct server *cursrv, *target = NULL;
215
216 if (!px)
217 return NULL;
218
219 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
220 if (strcmp(cursrv->id, name))
221 continue;
222
223 if (!target) {
224 target = cursrv;
225 continue;
226 }
227
228 Alert("Refusing to use duplicated server '%s' fould in proxy: %s!\n",
229 name, px->id);
230
231 return NULL;
232 }
233
234 return target;
235}
236
237/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200238 * This function creates all proxy sockets. It should be done very early,
239 * typically before privileges are dropped. The sockets will be registered
240 * but not added to any fd_set, in order not to loose them across the fork().
241 * The proxies also start in IDLE state, meaning that it will be
242 * maintain_proxies that will finally complete their loading.
243 *
244 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
245 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200246 */
247int start_proxies(int verbose)
248{
249 struct proxy *curproxy;
250 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100251 int lerr, err = ERR_NONE;
252 int pxerr;
253 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200254
255 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
256 if (curproxy->state != PR_STNEW)
257 continue; /* already initialized */
258
259 pxerr = 0;
260 for (listener = curproxy->listen; listener != NULL; listener = listener->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100261 if (listener->state != LI_ASSIGNED)
262 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263
Willy Tarreaue6b98942007-10-29 01:09:36 +0100264 lerr = tcp_bind_listener(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200265
Willy Tarreaue6b98942007-10-29 01:09:36 +0100266 /* errors are reported if <verbose> is set or if they are fatal */
267 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
268 if (lerr & ERR_ALERT)
269 Alert("Starting %s %s: %s\n",
270 proxy_type_str(curproxy), curproxy->id, msg);
271 else if (lerr & ERR_WARN)
272 Warning("Starting %s %s: %s\n",
273 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274 }
275
Willy Tarreaue6b98942007-10-29 01:09:36 +0100276 err |= lerr;
277 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200278 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100279 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100281 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282 pxerr |= 1;
283 continue;
284 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285 }
286
287 if (!pxerr) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200288 curproxy->state = PR_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200289 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
290 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100291
292 if (err & ERR_ABORT)
293 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200294 }
295
296 return err;
297}
298
299
300/*
301 * this function enables proxies when there are enough free sessions,
302 * or stops them when the table is full. It is designed to be called from the
Willy Tarreaud825eef2007-05-12 22:35:00 +0200303 * select_loop(). It returns the date of next expiration event during stop
304 * time, ETERNITY otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200305 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200306void maintain_proxies(struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307{
308 struct proxy *p;
309 struct listener *l;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310
311 p = proxy;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200312 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313
314 /* if there are enough free sessions, we'll activate proxies */
315 if (actconn < global.maxconn) {
316 while (p) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100317 if (p->feconn < p->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318 if (p->state == PR_STIDLE) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100319 for (l = p->listen; l != NULL; l = l->next)
320 enable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321 p->state = PR_STRUN;
322 }
323 }
324 else {
325 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100326 for (l = p->listen; l != NULL; l = l->next)
327 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 p->state = PR_STIDLE;
329 }
330 }
331 p = p->next;
332 }
333 }
334 else { /* block all proxies */
335 while (p) {
336 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100337 for (l = p->listen; l != NULL; l = l->next)
338 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339 p->state = PR_STIDLE;
340 }
341 p = p->next;
342 }
343 }
344
345 if (stopping) {
346 p = proxy;
347 while (p) {
348 if (p->state != PR_STSTOPPED) {
349 int t;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200350 t = tv_ms_remain2(&now, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351 if (t == 0) {
352 Warning("Proxy %s stopped.\n", p->id);
353 send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id);
354
355 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100356 unbind_listener(l);
357 if (l->state >= LI_ASSIGNED) {
358 delete_listener(l);
359 listeners--;
360 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361 }
362 p->state = PR_STSTOPPED;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200363 /* try to free more memory */
364 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 }
366 else {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200367 tv_bound(next, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368 }
369 }
370 p = p->next;
371 }
372 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200373 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374}
375
376
377/*
378 * this function disables health-check servers so that the process will quickly be ignored
379 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
380 * time will not be used since it would already not listen anymore to the socket.
381 */
382void soft_stop(void)
383{
384 struct proxy *p;
385
386 stopping = 1;
387 p = proxy;
388 tv_now(&now); /* else, the old time before select will be used */
389 while (p) {
390 if (p->state != PR_STSTOPPED) {
391 Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace);
392 send_log(p, LOG_WARNING, "Stopping proxy %s in %d ms.\n", p->id, p->grace);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200393 tv_ms_add(&p->stop_time, &now, p->grace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394 }
395 p = p->next;
396 }
397}
398
399
400/*
401 * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR.
402 * Solaris refuses either shutdown().
403 * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind.
404 * So a common validation path involves SHUT_WR && listen && SHUT_RD.
405 * If disabling at least one listener returns an error, then the proxy
406 * state is set to PR_STERROR because we don't know how to resume from this.
407 */
408void pause_proxy(struct proxy *p)
409{
410 struct listener *l;
411 for (l = p->listen; l != NULL; l = l->next) {
412 if (shutdown(l->fd, SHUT_WR) == 0 &&
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100413 listen(l->fd, p->backlog ? p->backlog : p->maxconn) == 0 &&
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414 shutdown(l->fd, SHUT_RD) == 0) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200415 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416 if (p->state != PR_STERROR)
417 p->state = PR_STPAUSED;
418 }
419 else
420 p->state = PR_STERROR;
421 }
422}
423
424/*
425 * This function temporarily disables listening so that another new instance
426 * can start listening. It is designed to be called upon reception of a
427 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
428 * the proxy, or a SIGTTIN can be sent to listen again.
429 */
430void pause_proxies(void)
431{
432 int err;
433 struct proxy *p;
434
435 err = 0;
436 p = proxy;
437 tv_now(&now); /* else, the old time before select will be used */
438 while (p) {
439 if (p->state != PR_STERROR &&
440 p->state != PR_STSTOPPED &&
441 p->state != PR_STPAUSED) {
442 Warning("Pausing proxy %s.\n", p->id);
443 send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id);
444 pause_proxy(p);
445 if (p->state != PR_STPAUSED) {
446 err |= 1;
447 Warning("Proxy %s failed to enter pause mode.\n", p->id);
448 send_log(p, LOG_WARNING, "Proxy %s failed to enter pause mode.\n", p->id);
449 }
450 }
451 p = p->next;
452 }
453 if (err) {
454 Warning("Some proxies refused to pause, performing soft stop now.\n");
455 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
456 soft_stop();
457 }
458}
459
460
461/*
462 * This function reactivates listening. This can be used after a call to
463 * sig_pause(), for example when a new instance has failed starting up.
464 * It is designed to be called upon reception of a SIGTTIN.
465 */
466void listen_proxies(void)
467{
468 struct proxy *p;
469 struct listener *l;
470
471 p = proxy;
472 tv_now(&now); /* else, the old time before select will be used */
473 while (p) {
474 if (p->state == PR_STPAUSED) {
475 Warning("Enabling proxy %s.\n", p->id);
476 send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id);
477
478 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100479 if (listen(l->fd, p->backlog ? p->backlog : p->maxconn) == 0) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100480 if (actconn < global.maxconn && p->feconn < p->maxconn) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200481 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200482 p->state = PR_STRUN;
483 }
484 else
485 p->state = PR_STIDLE;
486 } else {
487 int port;
488
489 if (l->addr.ss_family == AF_INET6)
490 port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port);
491 else
492 port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
493
494 Warning("Port %d busy while trying to enable proxy %s.\n",
495 port, p->id);
496 send_log(p, LOG_WARNING, "Port %d busy while trying to enable proxy %s.\n",
497 port, p->id);
498 /* Another port might have been enabled. Let's stop everything. */
499 pause_proxy(p);
500 break;
501 }
502 }
503 }
504 p = p->next;
505 }
506}
507
508
509/*
510 * Local variables:
511 * c-indent-level: 8
512 * c-basic-offset: 8
513 * End:
514 */