blob: 1b12844ea25416d53d8c288af7432e3cbd261fcc [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;
106 cap = PR_CAP_FE;
107 } else if (!strcmp(args[0], "server") || !strcmp(args[0], "srvtimeout")) {
108 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100109 tv = &proxy->timeout.server;
110 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100111 cap = PR_CAP_BE;
112 } else if (!strcmp(args[0], "connect") || !strcmp(args[0], "contimeout")) {
113 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100114 tv = &proxy->timeout.connect;
115 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100116 cap = PR_CAP_BE;
117 } else if (!strcmp(args[0], "appsession")) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100118 tv = &proxy->timeout.appsession;
119 td = &defpx->timeout.appsession;
Willy Tarreaue219db72007-12-03 01:30:13 +0100120 cap = PR_CAP_BE;
121 } else if (!strcmp(args[0], "queue")) {
122 tv = &proxy->timeout.queue;
123 td = &defpx->timeout.queue;
124 cap = PR_CAP_BE;
125 } else {
126 snprintf(err, errlen, "timeout '%s': must be 'client', 'server', 'connect', 'appsession', 'queue', or 'tarpit'",
127 args[0]);
128 return -1;
129 }
130
131 if (*args[1] == 0) {
132 snprintf(err, errlen, "%s timeout expects an integer value (in milliseconds)", name);
133 return -1;
134 }
135
136 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
137 if (res) {
138 snprintf(err, errlen, "unexpected character '%c' in %s timeout", *err, name);
139 return -1;
140 }
141
142 if (!(proxy->cap & cap)) {
143 snprintf(err, errlen, "%s timeout will be ignored because %s '%s' has no %s capability",
144 name, proxy_type_str(proxy), proxy->id,
145 (cap & PR_CAP_BE) ? "backend" : "frontend");
146 retval = 1;
147 }
148 else if (defpx && !__tv_iseq(tv, td)) {
149 snprintf(err, errlen, "overwriting %s timeout which was already specified", name);
150 retval = 1;
151 }
152
153 if (timeout)
154 __tv_from_ms(tv, timeout);
155 else
156 tv_eternity(tv);
157
158 return retval;
159}
160
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100161/*
162 * This function finds a proxy with matching name, mode and with satisfying
163 * capabilities. It also checks if there are more matching proxies with
164 * requested name as this often leads into unexpected situations.
165 */
166
167struct proxy *findproxy(const char *name, int mode, int cap) {
168
169 struct proxy *curproxy, *target=NULL;
170
171 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
172 if ((curproxy->cap & cap)!=cap || strcmp(curproxy->id, name))
173 continue;
174
175 if (curproxy->mode != mode) {
176 Alert("Unable to use proxy '%s' with wrong mode, required: %s, has: %s.\n",
177 name, proxy_mode_str(mode), proxy_mode_str(curproxy->mode));
178 Alert("You may want to use 'mode %s'.\n", proxy_mode_str(mode));
179 return NULL;
180 }
181
182 if (!target) {
183 target = curproxy;
184 continue;
185 }
186
Willy Tarreau816eb542007-11-04 07:04:43 +0100187 Alert("Refusing to use duplicated proxy '%s' with overlapping capabilities: %s/%s!\n",
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100188 name, proxy_type_str(curproxy), proxy_type_str(target));
189
190 return NULL;
191 }
192
193 return target;
194}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195
196/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200197 * This function creates all proxy sockets. It should be done very early,
198 * typically before privileges are dropped. The sockets will be registered
199 * but not added to any fd_set, in order not to loose them across the fork().
200 * The proxies also start in IDLE state, meaning that it will be
201 * maintain_proxies that will finally complete their loading.
202 *
203 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
204 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205 */
206int start_proxies(int verbose)
207{
208 struct proxy *curproxy;
209 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100210 int lerr, err = ERR_NONE;
211 int pxerr;
212 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200213
214 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
215 if (curproxy->state != PR_STNEW)
216 continue; /* already initialized */
217
218 pxerr = 0;
219 for (listener = curproxy->listen; listener != NULL; listener = listener->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100220 if (listener->state != LI_ASSIGNED)
221 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200222
Willy Tarreaue6b98942007-10-29 01:09:36 +0100223 lerr = tcp_bind_listener(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224
Willy Tarreaue6b98942007-10-29 01:09:36 +0100225 /* errors are reported if <verbose> is set or if they are fatal */
226 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
227 if (lerr & ERR_ALERT)
228 Alert("Starting %s %s: %s\n",
229 proxy_type_str(curproxy), curproxy->id, msg);
230 else if (lerr & ERR_WARN)
231 Warning("Starting %s %s: %s\n",
232 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233 }
234
Willy Tarreaue6b98942007-10-29 01:09:36 +0100235 err |= lerr;
236 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100238 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100240 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241 pxerr |= 1;
242 continue;
243 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244 }
245
246 if (!pxerr) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200247 curproxy->state = PR_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200248 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
249 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100250
251 if (err & ERR_ABORT)
252 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253 }
254
255 return err;
256}
257
258
259/*
260 * this function enables proxies when there are enough free sessions,
261 * or stops them when the table is full. It is designed to be called from the
Willy Tarreaud825eef2007-05-12 22:35:00 +0200262 * select_loop(). It returns the date of next expiration event during stop
263 * time, ETERNITY otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200264 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200265void maintain_proxies(struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200266{
267 struct proxy *p;
268 struct listener *l;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269
270 p = proxy;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200271 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200272
273 /* if there are enough free sessions, we'll activate proxies */
274 if (actconn < global.maxconn) {
275 while (p) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100276 if (p->feconn < p->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277 if (p->state == PR_STIDLE) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100278 for (l = p->listen; l != NULL; l = l->next)
279 enable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280 p->state = PR_STRUN;
281 }
282 }
283 else {
284 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100285 for (l = p->listen; l != NULL; l = l->next)
286 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200287 p->state = PR_STIDLE;
288 }
289 }
290 p = p->next;
291 }
292 }
293 else { /* block all proxies */
294 while (p) {
295 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100296 for (l = p->listen; l != NULL; l = l->next)
297 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298 p->state = PR_STIDLE;
299 }
300 p = p->next;
301 }
302 }
303
304 if (stopping) {
305 p = proxy;
306 while (p) {
307 if (p->state != PR_STSTOPPED) {
308 int t;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200309 t = tv_ms_remain2(&now, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310 if (t == 0) {
311 Warning("Proxy %s stopped.\n", p->id);
312 send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id);
313
314 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100315 unbind_listener(l);
316 if (l->state >= LI_ASSIGNED) {
317 delete_listener(l);
318 listeners--;
319 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320 }
321 p->state = PR_STSTOPPED;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200322 /* try to free more memory */
323 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324 }
325 else {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200326 tv_bound(next, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 }
328 }
329 p = p->next;
330 }
331 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200332 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333}
334
335
336/*
337 * this function disables health-check servers so that the process will quickly be ignored
338 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
339 * time will not be used since it would already not listen anymore to the socket.
340 */
341void soft_stop(void)
342{
343 struct proxy *p;
344
345 stopping = 1;
346 p = proxy;
347 tv_now(&now); /* else, the old time before select will be used */
348 while (p) {
349 if (p->state != PR_STSTOPPED) {
350 Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace);
351 send_log(p, LOG_WARNING, "Stopping proxy %s in %d ms.\n", p->id, p->grace);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200352 tv_ms_add(&p->stop_time, &now, p->grace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353 }
354 p = p->next;
355 }
356}
357
358
359/*
360 * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR.
361 * Solaris refuses either shutdown().
362 * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind.
363 * So a common validation path involves SHUT_WR && listen && SHUT_RD.
364 * If disabling at least one listener returns an error, then the proxy
365 * state is set to PR_STERROR because we don't know how to resume from this.
366 */
367void pause_proxy(struct proxy *p)
368{
369 struct listener *l;
370 for (l = p->listen; l != NULL; l = l->next) {
371 if (shutdown(l->fd, SHUT_WR) == 0 &&
372 listen(l->fd, p->maxconn) == 0 &&
373 shutdown(l->fd, SHUT_RD) == 0) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200374 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375 if (p->state != PR_STERROR)
376 p->state = PR_STPAUSED;
377 }
378 else
379 p->state = PR_STERROR;
380 }
381}
382
383/*
384 * This function temporarily disables listening so that another new instance
385 * can start listening. It is designed to be called upon reception of a
386 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
387 * the proxy, or a SIGTTIN can be sent to listen again.
388 */
389void pause_proxies(void)
390{
391 int err;
392 struct proxy *p;
393
394 err = 0;
395 p = proxy;
396 tv_now(&now); /* else, the old time before select will be used */
397 while (p) {
398 if (p->state != PR_STERROR &&
399 p->state != PR_STSTOPPED &&
400 p->state != PR_STPAUSED) {
401 Warning("Pausing proxy %s.\n", p->id);
402 send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id);
403 pause_proxy(p);
404 if (p->state != PR_STPAUSED) {
405 err |= 1;
406 Warning("Proxy %s failed to enter pause mode.\n", p->id);
407 send_log(p, LOG_WARNING, "Proxy %s failed to enter pause mode.\n", p->id);
408 }
409 }
410 p = p->next;
411 }
412 if (err) {
413 Warning("Some proxies refused to pause, performing soft stop now.\n");
414 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
415 soft_stop();
416 }
417}
418
419
420/*
421 * This function reactivates listening. This can be used after a call to
422 * sig_pause(), for example when a new instance has failed starting up.
423 * It is designed to be called upon reception of a SIGTTIN.
424 */
425void listen_proxies(void)
426{
427 struct proxy *p;
428 struct listener *l;
429
430 p = proxy;
431 tv_now(&now); /* else, the old time before select will be used */
432 while (p) {
433 if (p->state == PR_STPAUSED) {
434 Warning("Enabling proxy %s.\n", p->id);
435 send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id);
436
437 for (l = p->listen; l != NULL; l = l->next) {
438 if (listen(l->fd, p->maxconn) == 0) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100439 if (actconn < global.maxconn && p->feconn < p->maxconn) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200440 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441 p->state = PR_STRUN;
442 }
443 else
444 p->state = PR_STIDLE;
445 } else {
446 int port;
447
448 if (l->addr.ss_family == AF_INET6)
449 port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port);
450 else
451 port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
452
453 Warning("Port %d busy while trying to enable proxy %s.\n",
454 port, p->id);
455 send_log(p, LOG_WARNING, "Port %d busy while trying to enable proxy %s.\n",
456 port, p->id);
457 /* Another port might have been enabled. Let's stop everything. */
458 pause_proxy(p);
459 break;
460 }
461 }
462 }
463 p = p->next;
464 }
465}
466
467
468/*
469 * Local variables:
470 * c-indent-level: 8
471 * c-basic-offset: 8
472 * End:
473 */