blob: f25216c0eab9233f6ed994daeff608cda585e6d9 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Proxy variables and functions.
3 *
Willy Tarreau3a7d2072009-03-05 23:48:25 +01004 * Copyright 2000-2009 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>
Willy Tarreau9de1bbd2008-07-09 20:34:27 +020021#include <common/cfgparse.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020023#include <common/config.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010024#include <common/errors.h>
Willy Tarreau4d2d0982007-05-14 00:39:29 +020025#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
28#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Alexandre Cassen87ea5482007-10-11 20:48:58 +020030#include <proto/backend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/fd.h>
Willy Tarreau51aecc72009-07-12 09:47:04 +020032#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/log.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <proto/protocols.h>
35#include <proto/proto_tcp.h>
Willy Tarreau39e4f622010-05-31 17:01:36 +020036#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/proxy.h>
Willy Tarreaud0807c32010-08-27 18:26:11 +020038#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
40
Willy Tarreaue6b98942007-10-29 01:09:36 +010041int listeners; /* # of proxy listeners, set by cfgparse, unset by maintain_proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +020042struct proxy *proxy = NULL; /* list of all existing proxies */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +020043struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau977b8e42006-12-29 14:19:17 +010045/*
Willy Tarreau816eb542007-11-04 07:04:43 +010046 * This function returns a string containing a name describing capabilities to
47 * report comprehensible error messages. Specifically, it will return the words
48 * "frontend", "backend", "ruleset" when appropriate, or "proxy" for all other
49 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +010050 */
Willy Tarreau816eb542007-11-04 07:04:43 +010051const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +010052{
Willy Tarreau816eb542007-11-04 07:04:43 +010053 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
54 if (cap & PR_CAP_FE)
55 return "frontend";
56 else if (cap & PR_CAP_BE)
57 return "backend";
58 else if (cap & PR_CAP_RS)
59 return "ruleset";
60 }
61 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +010062}
63
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010064/*
65 * This function returns a string containing the mode of the proxy in a format
66 * suitable for error messages.
67 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010068const char *proxy_mode_str(int mode) {
69
70 if (mode == PR_MODE_TCP)
71 return "tcp";
72 else if (mode == PR_MODE_HTTP)
73 return "http";
74 else if (mode == PR_MODE_HEALTH)
75 return "health";
76 else
77 return "unknown";
78}
79
Willy Tarreauf3950172009-10-10 18:35:51 +020080/*
81 * This function scans the list of backends and servers to retrieve the first
82 * backend and the first server with the given names, and sets them in both
83 * parameters. It returns zero if either is not found, or non-zero and sets
84 * the ones it did not found to NULL. If a NULL pointer is passed for the
85 * backend, only the pointer to the server will be updated.
86 */
87int get_backend_server(const char *bk_name, const char *sv_name,
88 struct proxy **bk, struct server **sv)
89{
90 struct proxy *p;
91 struct server *s;
Willy Tarreaucfeaa472009-10-10 22:33:08 +020092 int pid, sid;
Willy Tarreauf3950172009-10-10 18:35:51 +020093
94 *sv = NULL;
95
Willy Tarreaucfeaa472009-10-10 22:33:08 +020096 pid = 0;
97 if (*bk_name == '#')
98 pid = atoi(bk_name + 1);
99 sid = 0;
100 if (*sv_name == '#')
101 sid = atoi(sv_name + 1);
102
Willy Tarreauf3950172009-10-10 18:35:51 +0200103 for (p = proxy; p; p = p->next)
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200104 if ((p->cap & PR_CAP_BE) &&
105 ((pid && p->uuid == pid) ||
106 (!pid && strcmp(p->id, bk_name) == 0)))
Willy Tarreauf3950172009-10-10 18:35:51 +0200107 break;
108 if (bk)
109 *bk = p;
110 if (!p)
111 return 0;
112
113 for (s = p->srv; s; s = s->next)
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200114 if ((sid && s->puid == sid) ||
115 (!sid && strcmp(s->id, sv_name) == 0))
Willy Tarreauf3950172009-10-10 18:35:51 +0200116 break;
117 *sv = s;
118 if (!s)
119 return 0;
120 return 1;
121}
122
Willy Tarreaue219db72007-12-03 01:30:13 +0100123/* This function parses a "timeout" statement in a proxy section. It returns
124 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
125 * return zero, it may write an error message into the <err> buffer, for at
126 * most <errlen> bytes, trailing zero included. The trailing '\n' must not
127 * be written. The function must be called with <args> pointing to the first
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200128 * command line word, with <proxy> pointing to the proxy being parsed, and
Willy Tarreaue219db72007-12-03 01:30:13 +0100129 * <defpx> to the default proxy or NULL. As a special case for compatibility
130 * with older configs, it also accepts "{cli|srv|con}timeout" in args[0].
131 */
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200132static int proxy_parse_timeout(char **args, int section, struct proxy *proxy,
133 struct proxy *defpx, char *err, int errlen)
Willy Tarreaue219db72007-12-03 01:30:13 +0100134{
135 unsigned timeout;
136 int retval, cap;
137 const char *res, *name;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200138 int *tv = NULL;
139 int *td = NULL;
Willy Tarreaue219db72007-12-03 01:30:13 +0100140
141 retval = 0;
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200142
143 /* simply skip "timeout" but remain compatible with old form */
144 if (strcmp(args[0], "timeout") == 0)
145 args++;
146
Willy Tarreaue219db72007-12-03 01:30:13 +0100147 name = args[0];
148 if (!strcmp(args[0], "client") || !strcmp(args[0], "clitimeout")) {
149 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100150 tv = &proxy->timeout.client;
151 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100152 cap = PR_CAP_FE;
153 } else if (!strcmp(args[0], "tarpit")) {
154 tv = &proxy->timeout.tarpit;
155 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100156 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100157 } else if (!strcmp(args[0], "http-keep-alive")) {
158 tv = &proxy->timeout.httpka;
159 td = &defpx->timeout.httpka;
160 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau036fae02008-01-06 13:24:40 +0100161 } else if (!strcmp(args[0], "http-request")) {
162 tv = &proxy->timeout.httpreq;
163 td = &defpx->timeout.httpreq;
Willy Tarreaucd7afc02009-07-12 10:03:17 +0200164 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100165 } else if (!strcmp(args[0], "server") || !strcmp(args[0], "srvtimeout")) {
166 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100167 tv = &proxy->timeout.server;
168 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100169 cap = PR_CAP_BE;
170 } else if (!strcmp(args[0], "connect") || !strcmp(args[0], "contimeout")) {
171 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100172 tv = &proxy->timeout.connect;
173 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100174 cap = PR_CAP_BE;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100175 } else if (!strcmp(args[0], "check")) {
176 tv = &proxy->timeout.check;
177 td = &defpx->timeout.check;
178 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100179 } else if (!strcmp(args[0], "queue")) {
180 tv = &proxy->timeout.queue;
181 td = &defpx->timeout.queue;
182 cap = PR_CAP_BE;
183 } else {
Willy Tarreau036fae02008-01-06 13:24:40 +0100184 snprintf(err, errlen,
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100185 "timeout '%s': must be 'client', 'server', 'connect', 'check', "
Willy Tarreau76bfc952010-01-10 17:48:11 +0100186 "'queue', 'http-keep-alive', 'http-request' or 'tarpit'",
Willy Tarreaue219db72007-12-03 01:30:13 +0100187 args[0]);
188 return -1;
189 }
190
191 if (*args[1] == 0) {
192 snprintf(err, errlen, "%s timeout expects an integer value (in milliseconds)", name);
193 return -1;
194 }
195
196 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
197 if (res) {
Willy Tarreaubb9251e2009-03-06 08:05:40 +0100198 snprintf(err, errlen, "unexpected character '%c' in %s timeout", *res, name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100199 return -1;
200 }
201
202 if (!(proxy->cap & cap)) {
203 snprintf(err, errlen, "%s timeout will be ignored because %s '%s' has no %s capability",
204 name, proxy_type_str(proxy), proxy->id,
205 (cap & PR_CAP_BE) ? "backend" : "frontend");
206 retval = 1;
207 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200208 else if (defpx && *tv != *td) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100209 snprintf(err, errlen, "overwriting %s timeout which was already specified", name);
210 retval = 1;
211 }
212
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200213 *tv = MS_TO_TICKS(timeout);
Willy Tarreaue219db72007-12-03 01:30:13 +0100214 return retval;
215}
216
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100217/* This function parses a "rate-limit" statement in a proxy section. It returns
218 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
219 * return zero, it may write an error message into the <err> buffer, for at
220 * most <errlen> bytes, trailing zero included. The trailing '\n' must not
221 * be written. The function must be called with <args> pointing to the first
222 * command line word, with <proxy> pointing to the proxy being parsed, and
223 * <defpx> to the default proxy or NULL.
224 */
225static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy,
226 struct proxy *defpx, char *err, int errlen)
227{
228 int retval, cap;
229 char *res, *name;
230 unsigned int *tv = NULL;
231 unsigned int *td = NULL;
232 unsigned int val;
233
234 retval = 0;
235
236 /* simply skip "rate-limit" */
237 if (strcmp(args[0], "rate-limit") == 0)
238 args++;
239
240 name = args[0];
241 if (!strcmp(args[0], "sessions")) {
242 name = "sessions";
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200243 tv = &proxy->fe_sps_lim;
244 td = &defpx->fe_sps_lim;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100245 cap = PR_CAP_FE;
246 } else {
247 snprintf(err, errlen,
248 "%s '%s': must be 'sessions'",
249 "rate-limit", args[0]);
250 return -1;
251 }
252
253 if (*args[1] == 0) {
254 snprintf(err, errlen, "%s %s expects expects an integer value (in sessions/second)", "rate-limit", name);
255 return -1;
256 }
257
258 val = strtoul(args[1], &res, 0);
259 if (*res) {
260 snprintf(err, errlen, "%s %s: unexpected character '%c' in integer value '%s'", "rate-limit", name, *res, args[1]);
261 return -1;
262 }
263
264 if (!(proxy->cap & cap)) {
265 snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability",
266 "rate-limit", name, proxy_type_str(proxy), proxy->id,
267 (cap & PR_CAP_BE) ? "backend" : "frontend");
268 retval = 1;
269 }
270 else if (defpx && *tv != *td) {
271 snprintf(err, errlen, "overwriting %s %s which was already specified", "rate-limit", name);
272 retval = 1;
273 }
274
275 *tv = val;
276 return retval;
277}
278
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100279/*
280 * This function finds a proxy with matching name, mode and with satisfying
281 * capabilities. It also checks if there are more matching proxies with
282 * requested name as this often leads into unexpected situations.
283 */
284
Alex Williams96532db2009-11-01 21:27:13 -0500285struct proxy *findproxy_mode(const char *name, int mode, int cap) {
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100286
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100287 struct proxy *curproxy, *target = NULL;
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100288
289 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
290 if ((curproxy->cap & cap)!=cap || strcmp(curproxy->id, name))
291 continue;
292
Willy Tarreau51aecc72009-07-12 09:47:04 +0200293 if (curproxy->mode != mode &&
294 !(curproxy->mode == PR_MODE_HTTP && mode == PR_MODE_TCP)) {
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100295 Alert("Unable to use proxy '%s' with wrong mode, required: %s, has: %s.\n",
296 name, proxy_mode_str(mode), proxy_mode_str(curproxy->mode));
297 Alert("You may want to use 'mode %s'.\n", proxy_mode_str(mode));
298 return NULL;
299 }
300
301 if (!target) {
302 target = curproxy;
303 continue;
304 }
305
Willy Tarreau816eb542007-11-04 07:04:43 +0100306 Alert("Refusing to use duplicated proxy '%s' with overlapping capabilities: %s/%s!\n",
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100307 name, proxy_type_str(curproxy), proxy_type_str(target));
308
309 return NULL;
310 }
311
312 return target;
313}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200314
Alex Williams96532db2009-11-01 21:27:13 -0500315struct proxy *findproxy(const char *name, int cap) {
316
317 struct proxy *curproxy, *target = NULL;
318
319 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
320 if ((curproxy->cap & cap)!=cap || strcmp(curproxy->id, name))
321 continue;
322
323 if (!target) {
324 target = curproxy;
325 continue;
326 }
327
328 return NULL;
329 }
330
331 return target;
332}
333
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100335 * This function finds a server with matching name within selected proxy.
336 * It also checks if there are more matching servers with
337 * requested name as this often leads into unexpected situations.
338 */
339
340struct server *findserver(const struct proxy *px, const char *name) {
341
342 struct server *cursrv, *target = NULL;
343
344 if (!px)
345 return NULL;
346
347 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
348 if (strcmp(cursrv->id, name))
349 continue;
350
351 if (!target) {
352 target = cursrv;
353 continue;
354 }
355
356 Alert("Refusing to use duplicated server '%s' fould in proxy: %s!\n",
357 name, px->id);
358
359 return NULL;
360 }
361
362 return target;
363}
364
Willy Tarreauff01a212009-03-15 13:46:16 +0100365/* This function checks that the designated proxy has no http directives
366 * enabled. It will output a warning if there are, and will fix some of them.
367 * It returns the number of fatal errors encountered. This should be called
368 * at the end of the configuration parsing if the proxy is not in http mode.
369 * The <file> argument is used to construct the error message.
370 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200371int proxy_cfg_ensure_no_http(struct proxy *curproxy)
Willy Tarreauff01a212009-03-15 13:46:16 +0100372{
373 if (curproxy->cookie_name != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200374 Warning("config : cookie will be ignored for %s '%s' (needs 'mode http').\n",
375 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100376 }
377 if (curproxy->rsp_exp != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200378 Warning("config : server regular expressions will be ignored for %s '%s' (needs 'mode http').\n",
379 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100380 }
381 if (curproxy->req_exp != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200382 Warning("config : client regular expressions will be ignored for %s '%s' (needs 'mode http').\n",
383 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100384 }
385 if (curproxy->monitor_uri != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200386 Warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n",
387 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100388 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200389 if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) {
Willy Tarreauff01a212009-03-15 13:46:16 +0100390 curproxy->lbprm.algo &= ~BE_LB_ALGO;
391 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200392 Warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n",
393 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100394 }
Willy Tarreau17804162009-11-09 21:27:51 +0100395 if (curproxy->to_log & (LW_REQ | LW_RESP)) {
396 curproxy->to_log &= ~(LW_REQ | LW_RESP);
397 Warning("config : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
398 proxy_type_str(curproxy), curproxy->id);
399 }
Willy Tarreauff01a212009-03-15 13:46:16 +0100400 return 0;
401}
402
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100403/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200404 * This function creates all proxy sockets. It should be done very early,
405 * typically before privileges are dropped. The sockets will be registered
406 * but not added to any fd_set, in order not to loose them across the fork().
407 * The proxies also start in IDLE state, meaning that it will be
408 * maintain_proxies that will finally complete their loading.
409 *
410 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
411 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412 */
413int start_proxies(int verbose)
414{
415 struct proxy *curproxy;
416 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100417 int lerr, err = ERR_NONE;
418 int pxerr;
419 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420
421 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
422 if (curproxy->state != PR_STNEW)
423 continue; /* already initialized */
424
425 pxerr = 0;
426 for (listener = curproxy->listen; listener != NULL; listener = listener->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100427 if (listener->state != LI_ASSIGNED)
428 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200429
Willy Tarreaue6b98942007-10-29 01:09:36 +0100430 lerr = tcp_bind_listener(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200431
Willy Tarreaue6b98942007-10-29 01:09:36 +0100432 /* errors are reported if <verbose> is set or if they are fatal */
433 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
434 if (lerr & ERR_ALERT)
435 Alert("Starting %s %s: %s\n",
436 proxy_type_str(curproxy), curproxy->id, msg);
437 else if (lerr & ERR_WARN)
438 Warning("Starting %s %s: %s\n",
439 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440 }
441
Willy Tarreaue6b98942007-10-29 01:09:36 +0100442 err |= lerr;
443 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200444 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100445 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100447 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448 pxerr |= 1;
449 continue;
450 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200451 }
452
453 if (!pxerr) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200454 curproxy->state = PR_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
456 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100457
458 if (err & ERR_ABORT)
459 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 }
461
462 return err;
463}
464
465
466/*
467 * this function enables proxies when there are enough free sessions,
468 * or stops them when the table is full. It is designed to be called from the
Willy Tarreau58b458d2008-06-29 22:40:23 +0200469 * select_loop(). It adjusts the date of next expiration event during stop
470 * time if appropriate.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200471 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200472void maintain_proxies(int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473{
474 struct proxy *p;
475 struct listener *l;
Willy Tarreau79584222009-03-06 09:18:27 +0100476 unsigned int wait;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477
478 p = proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479
480 /* if there are enough free sessions, we'll activate proxies */
481 if (actconn < global.maxconn) {
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100482 for (; p; p = p->next) {
483 /* check the various reasons we may find to block the frontend */
484 if (p->feconn >= p->maxconn)
485 goto do_block;
486
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200487 if (p->fe_sps_lim &&
Willy Tarreaud9bbe172010-06-07 10:40:48 +0200488 (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 1))) {
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100489 /* we're blocking because a limit was reached on the number of
490 * requests/s on the frontend. We want to re-check ASAP, which
Willy Tarreauefcbc6e2009-03-06 08:27:10 +0100491 * means in 1 ms before estimated expiration date, because the
492 * timer will have settled down. Note that we may already be in
493 * IDLE state here.
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100494 */
Willy Tarreauefcbc6e2009-03-06 08:27:10 +0100495 *next = tick_first(*next, tick_add(now_ms, wait));
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100496 goto do_block;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200497 }
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100498
499 /* OK we have no reason to block, so let's unblock if we were blocking */
500 if (p->state == PR_STIDLE) {
501 for (l = p->listen; l != NULL; l = l->next)
502 enable_listener(l);
503 p->state = PR_STRUN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200504 }
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100505 continue;
506
507 do_block:
508 if (p->state == PR_STRUN) {
509 for (l = p->listen; l != NULL; l = l->next)
510 disable_listener(l);
511 p->state = PR_STIDLE;
512 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513 }
514 }
515 else { /* block all proxies */
516 while (p) {
517 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100518 for (l = p->listen; l != NULL; l = l->next)
519 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520 p->state = PR_STIDLE;
521 }
522 p = p->next;
523 }
524 }
525
526 if (stopping) {
527 p = proxy;
528 while (p) {
529 if (p->state != PR_STSTOPPED) {
530 int t;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200531 t = tick_remain(now_ms, p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532 if (t == 0) {
Willy Tarreau11046142010-03-04 23:07:28 +0100533 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
534 p->id, p->counters.cum_feconn, p->counters.cum_beconn);
535 send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
536 p->id, p->counters.cum_feconn, p->counters.cum_beconn);
Willy Tarreauda250db2008-10-12 12:07:48 +0200537 stop_proxy(p);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200538 /* try to free more memory */
539 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540 }
541 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200542 *next = tick_first(*next, p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 }
544 }
545 p = p->next;
546 }
547 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200548 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200549}
550
551
552/*
553 * this function disables health-check servers so that the process will quickly be ignored
554 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
555 * time will not be used since it would already not listen anymore to the socket.
556 */
557void soft_stop(void)
558{
559 struct proxy *p;
560
561 stopping = 1;
562 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200563 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200564 while (p) {
565 if (p->state != PR_STSTOPPED) {
Willy Tarreauf8fbcef2008-10-10 17:51:34 +0200566 Warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace);
567 send_log(p, LOG_WARNING, "Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200568 p->stop_time = tick_add(now_ms, p->grace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569 }
570 p = p->next;
571 }
Willy Tarreaud0807c32010-08-27 18:26:11 +0200572 /* signal zero is used to broadcast the "stopping" event */
573 signal_handler(0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574}
575
576
577/*
578 * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR.
579 * Solaris refuses either shutdown().
580 * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind.
581 * So a common validation path involves SHUT_WR && listen && SHUT_RD.
582 * If disabling at least one listener returns an error, then the proxy
583 * state is set to PR_STERROR because we don't know how to resume from this.
584 */
585void pause_proxy(struct proxy *p)
586{
587 struct listener *l;
588 for (l = p->listen; l != NULL; l = l->next) {
589 if (shutdown(l->fd, SHUT_WR) == 0 &&
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100590 listen(l->fd, p->backlog ? p->backlog : p->maxconn) == 0 &&
Willy Tarreaubaaee002006-06-26 02:48:02 +0200591 shutdown(l->fd, SHUT_RD) == 0) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200592 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200593 if (p->state != PR_STERROR)
594 p->state = PR_STPAUSED;
595 }
596 else
597 p->state = PR_STERROR;
598 }
Willy Tarreauda250db2008-10-12 12:07:48 +0200599}
600
601
602/*
603 * This function completely stops a proxy and releases its listeners. It has
604 * to be called when going down in order to release the ports so that another
605 * process may bind to them. It must also be called on disabled proxies at the
606 * end of start-up. When all listeners are closed, the proxy is set to the
607 * PR_STSTOPPED state.
608 */
609void stop_proxy(struct proxy *p)
610{
611 struct listener *l;
612
613 for (l = p->listen; l != NULL; l = l->next) {
614 unbind_listener(l);
615 if (l->state >= LI_ASSIGNED) {
616 delete_listener(l);
617 listeners--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200618 jobs--;
Willy Tarreauda250db2008-10-12 12:07:48 +0200619 }
620 }
621 p->state = PR_STSTOPPED;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200622}
623
624/*
625 * This function temporarily disables listening so that another new instance
626 * can start listening. It is designed to be called upon reception of a
627 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
628 * the proxy, or a SIGTTIN can be sent to listen again.
629 */
630void pause_proxies(void)
631{
632 int err;
633 struct proxy *p;
634
635 err = 0;
636 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200637 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200638 while (p) {
Willy Tarreauf8fbcef2008-10-10 17:51:34 +0200639 if (p->cap & PR_CAP_FE &&
640 p->state != PR_STERROR &&
Willy Tarreaubaaee002006-06-26 02:48:02 +0200641 p->state != PR_STSTOPPED &&
642 p->state != PR_STPAUSED) {
Willy Tarreauf8fbcef2008-10-10 17:51:34 +0200643 Warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
644 send_log(p, LOG_WARNING, "Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 pause_proxy(p);
646 if (p->state != PR_STPAUSED) {
647 err |= 1;
Willy Tarreauf8fbcef2008-10-10 17:51:34 +0200648 Warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
649 send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650 }
651 }
652 p = p->next;
653 }
654 if (err) {
655 Warning("Some proxies refused to pause, performing soft stop now.\n");
656 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
657 soft_stop();
658 }
659}
660
661
662/*
663 * This function reactivates listening. This can be used after a call to
664 * sig_pause(), for example when a new instance has failed starting up.
665 * It is designed to be called upon reception of a SIGTTIN.
666 */
667void listen_proxies(void)
668{
669 struct proxy *p;
670 struct listener *l;
671
672 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200673 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 while (p) {
675 if (p->state == PR_STPAUSED) {
Willy Tarreauf8fbcef2008-10-10 17:51:34 +0200676 Warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
677 send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678
679 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100680 if (listen(l->fd, p->backlog ? p->backlog : p->maxconn) == 0) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100681 if (actconn < global.maxconn && p->feconn < p->maxconn) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200682 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200683 p->state = PR_STRUN;
684 }
685 else
686 p->state = PR_STIDLE;
687 } else {
688 int port;
689
690 if (l->addr.ss_family == AF_INET6)
691 port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port);
692 else
693 port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
694
Willy Tarreauf8fbcef2008-10-10 17:51:34 +0200695 Warning("Port %d busy while trying to enable %s %s.\n",
696 port, proxy_cap_str(p->cap), p->id);
697 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
698 port, proxy_cap_str(p->cap), p->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200699 /* Another port might have been enabled. Let's stop everything. */
700 pause_proxy(p);
701 break;
702 }
703 }
704 }
705 p = p->next;
706 }
707}
708
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200709/* Set current session's backend to <be>. Nothing is done if the
710 * session already had a backend assigned, which is indicated by
711 * s->flags & SN_BE_ASSIGNED.
712 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200713 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200714 */
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200715int session_set_backend(struct session *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200716{
717 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200718 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200719 s->be = be;
720 be->beconn++;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200721 if (be->beconn > be->counters.beconn_max)
722 be->counters.beconn_max = be->beconn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200723 proxy_inc_be_ctr(be);
724
725 /* assign new parameters to the session from the new backend */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200726 s->si[1].flags &= ~SI_FL_INDEP_STR;
727 if (be->options2 & PR_O2_INDEPSTR)
728 s->si[1].flags |= SI_FL_INDEP_STR;
729
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200730 if (be->options2 & PR_O2_RSPBUG_OK)
731 s->txn.rsp.err_pos = -1; /* let buggy responses pass */
732 s->flags |= SN_BE_ASSIGNED;
Willy Tarreau51aecc72009-07-12 09:47:04 +0200733
734 /* If the target backend requires HTTP processing, we have to allocate
735 * a struct hdr_idx for it if we did not have one.
736 */
737 if (unlikely(!s->txn.hdr_idx.v && (be->acl_requires & ACL_USE_L7_ANY))) {
738 if ((s->txn.hdr_idx.v = pool_alloc2(s->fe->hdr_idx_pool)) == NULL)
739 return 0; /* not enough memory */
Willy Tarreau39e4f622010-05-31 17:01:36 +0200740
741 /* and now initialize the HTTP transaction state */
742 http_init_txn(s);
743
Willy Tarreau51aecc72009-07-12 09:47:04 +0200744 s->txn.hdr_idx.size = MAX_HTTP_HDR;
745 hdr_idx_init(&s->txn.hdr_idx);
746 }
747
Willy Tarreauc1a21672009-08-16 22:37:44 +0200748 /* We want to enable the backend-specific analysers except those which
749 * were already run as part of the frontend/listener. Note that it would
750 * be more reliable to store the list of analysers that have been run,
751 * but what we do here is OK for now.
Emeric Brun647caf12009-06-30 17:57:00 +0200752 */
Willy Tarreauc1a21672009-08-16 22:37:44 +0200753 s->req->analysers |= be->be_req_ana & ~(s->listener->analysers);
Emeric Brun647caf12009-06-30 17:57:00 +0200754
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200755 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200756}
757
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200758static struct cfg_kw_list cfg_kws = {{ },{
759 { CFG_LISTEN, "timeout", proxy_parse_timeout },
760 { CFG_LISTEN, "clitimeout", proxy_parse_timeout },
761 { CFG_LISTEN, "contimeout", proxy_parse_timeout },
762 { CFG_LISTEN, "srvtimeout", proxy_parse_timeout },
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100763 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200764 { 0, NULL, NULL },
765}};
766
767__attribute__((constructor))
768static void __proxy_module_init(void)
769{
770 cfg_register_keywords(&cfg_kws);
771}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772
773/*
774 * Local variables:
775 * c-indent-level: 8
776 * c-basic-offset: 8
777 * End:
778 */