blob: fb62b7c044052c68b40bd1f6477a6bbbfa01b772 [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
Willy Tarreaucfd837f2014-03-15 07:43:51 +010028#include <eb32tree.h>
29#include <ebistree.h>
30
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +020031#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010033#include <types/obj_type.h>
Emeric Brun5a8c0a92010-09-23 18:44:36 +020034#include <types/peers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Alexandre Cassen87ea5482007-10-11 20:48:58 +020036#include <proto/backend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/fd.h>
Willy Tarreau51aecc72009-07-12 09:47:04 +020038#include <proto/hdr_idx.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020039#include <proto/listener.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/log.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010041#include <proto/proto_tcp.h>
Willy Tarreau39e4f622010-05-31 17:01:36 +020042#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <proto/proxy.h>
Willy Tarreaud0807c32010-08-27 18:26:11 +020044#include <proto/signal.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020045#include <proto/stream.h>
Emeric Brun5a8c0a92010-09-23 18:44:36 +020046#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
48
Willy Tarreau918ff602011-07-25 16:33:49 +020049int listeners; /* # of proxy listeners, set by cfgparse */
Willy Tarreaubaaee002006-06-26 02:48:02 +020050struct proxy *proxy = NULL; /* list of all existing proxies */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +020051struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */
Willy Tarreauf79d9502014-03-15 07:22:35 +010052struct eb_root proxy_by_name = EB_ROOT; /* tree of proxies sorted by name */
Willy Tarreau10479e42010-12-12 14:00:34 +010053unsigned int error_snapshot_id = 0; /* global ID assigned to each error then incremented */
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau977b8e42006-12-29 14:19:17 +010055/*
Willy Tarreau816eb542007-11-04 07:04:43 +010056 * This function returns a string containing a name describing capabilities to
57 * report comprehensible error messages. Specifically, it will return the words
58 * "frontend", "backend", "ruleset" when appropriate, or "proxy" for all other
59 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +010060 */
Willy Tarreau816eb542007-11-04 07:04:43 +010061const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +010062{
Willy Tarreau816eb542007-11-04 07:04:43 +010063 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
64 if (cap & PR_CAP_FE)
65 return "frontend";
66 else if (cap & PR_CAP_BE)
67 return "backend";
68 else if (cap & PR_CAP_RS)
69 return "ruleset";
70 }
71 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +010072}
73
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010074/*
75 * This function returns a string containing the mode of the proxy in a format
76 * suitable for error messages.
77 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010078const char *proxy_mode_str(int mode) {
79
80 if (mode == PR_MODE_TCP)
81 return "tcp";
82 else if (mode == PR_MODE_HTTP)
83 return "http";
84 else if (mode == PR_MODE_HEALTH)
85 return "health";
86 else
87 return "unknown";
88}
89
Willy Tarreauf3950172009-10-10 18:35:51 +020090/*
91 * This function scans the list of backends and servers to retrieve the first
92 * backend and the first server with the given names, and sets them in both
93 * parameters. It returns zero if either is not found, or non-zero and sets
94 * the ones it did not found to NULL. If a NULL pointer is passed for the
95 * backend, only the pointer to the server will be updated.
96 */
97int get_backend_server(const char *bk_name, const char *sv_name,
98 struct proxy **bk, struct server **sv)
99{
100 struct proxy *p;
101 struct server *s;
Willy Tarreau7ecc4202014-03-15 07:57:11 +0100102 int sid;
Willy Tarreauf3950172009-10-10 18:35:51 +0200103
104 *sv = NULL;
105
Willy Tarreau050536d2012-10-04 08:47:34 +0200106 sid = -1;
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200107 if (*sv_name == '#')
108 sid = atoi(sv_name + 1);
109
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200110 p = proxy_be_by_name(bk_name);
Willy Tarreauf3950172009-10-10 18:35:51 +0200111 if (bk)
112 *bk = p;
113 if (!p)
114 return 0;
115
116 for (s = p->srv; s; s = s->next)
Willy Tarreau4055a102012-11-15 00:15:18 +0100117 if ((sid >= 0 && s->puid == sid) ||
118 (sid < 0 && strcmp(s->id, sv_name) == 0))
Willy Tarreauf3950172009-10-10 18:35:51 +0200119 break;
120 *sv = s;
121 if (!s)
122 return 0;
123 return 1;
124}
125
Willy Tarreaue219db72007-12-03 01:30:13 +0100126/* This function parses a "timeout" statement in a proxy section. It returns
127 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200128 * return zero, it will write an error or warning message into a preallocated
129 * buffer returned at <err>. The trailing is not be written. The function must
130 * be called with <args> pointing to the first command line word, with <proxy>
131 * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL.
132 * As a special case for compatibility with older configs, it also accepts
133 * "{cli|srv|con}timeout" in args[0].
Willy Tarreaue219db72007-12-03 01:30:13 +0100134 */
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200135static int proxy_parse_timeout(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200136 struct proxy *defpx, const char *file, int line,
137 char **err)
Willy Tarreaue219db72007-12-03 01:30:13 +0100138{
139 unsigned timeout;
140 int retval, cap;
141 const char *res, *name;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200142 int *tv = NULL;
143 int *td = NULL;
Willy Tarreaued446492014-04-28 22:56:38 +0200144 int warn = 0;
Willy Tarreaue219db72007-12-03 01:30:13 +0100145
146 retval = 0;
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200147
148 /* simply skip "timeout" but remain compatible with old form */
149 if (strcmp(args[0], "timeout") == 0)
150 args++;
151
Willy Tarreaue219db72007-12-03 01:30:13 +0100152 name = args[0];
Willy Tarreaued446492014-04-28 22:56:38 +0200153 if (!strcmp(args[0], "client") || (!strcmp(args[0], "clitimeout") && (warn = WARN_CLITO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100154 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100155 tv = &proxy->timeout.client;
156 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100157 cap = PR_CAP_FE;
158 } else if (!strcmp(args[0], "tarpit")) {
159 tv = &proxy->timeout.tarpit;
160 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100161 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100162 } else if (!strcmp(args[0], "http-keep-alive")) {
163 tv = &proxy->timeout.httpka;
164 td = &defpx->timeout.httpka;
165 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau036fae02008-01-06 13:24:40 +0100166 } else if (!strcmp(args[0], "http-request")) {
167 tv = &proxy->timeout.httpreq;
168 td = &defpx->timeout.httpreq;
Willy Tarreaucd7afc02009-07-12 10:03:17 +0200169 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaued446492014-04-28 22:56:38 +0200170 } else if (!strcmp(args[0], "server") || (!strcmp(args[0], "srvtimeout") && (warn = WARN_SRVTO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100171 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100172 tv = &proxy->timeout.server;
173 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100174 cap = PR_CAP_BE;
Willy Tarreaued446492014-04-28 22:56:38 +0200175 } else if (!strcmp(args[0], "connect") || (!strcmp(args[0], "contimeout") && (warn = WARN_CONTO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100176 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100177 tv = &proxy->timeout.connect;
178 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100179 cap = PR_CAP_BE;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100180 } else if (!strcmp(args[0], "check")) {
181 tv = &proxy->timeout.check;
182 td = &defpx->timeout.check;
183 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100184 } else if (!strcmp(args[0], "queue")) {
185 tv = &proxy->timeout.queue;
186 td = &defpx->timeout.queue;
187 cap = PR_CAP_BE;
Willy Tarreauce887fd2012-05-12 12:50:00 +0200188 } else if (!strcmp(args[0], "tunnel")) {
189 tv = &proxy->timeout.tunnel;
190 td = &defpx->timeout.tunnel;
191 cap = PR_CAP_BE;
Willy Tarreau05cdd962014-05-10 14:30:07 +0200192 } else if (!strcmp(args[0], "client-fin")) {
193 tv = &proxy->timeout.clientfin;
194 td = &defpx->timeout.clientfin;
195 cap = PR_CAP_FE;
196 } else if (!strcmp(args[0], "server-fin")) {
197 tv = &proxy->timeout.serverfin;
198 td = &defpx->timeout.serverfin;
199 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100200 } else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200201 memprintf(err,
202 "'timeout' supports 'client', 'server', 'connect', 'check', "
Willy Tarreau05cdd962014-05-10 14:30:07 +0200203 "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', "
204 "'client-fin' and 'server-fin' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200205 args[0]);
Willy Tarreaue219db72007-12-03 01:30:13 +0100206 return -1;
207 }
208
209 if (*args[1] == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200210 memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100211 return -1;
212 }
213
214 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
215 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200216 memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100217 return -1;
218 }
219
220 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200221 memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability",
222 name, proxy_type_str(proxy), proxy->id,
223 (cap & PR_CAP_BE) ? "backend" : "frontend");
Willy Tarreaue219db72007-12-03 01:30:13 +0100224 retval = 1;
225 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200226 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200227 memprintf(err, "overwriting 'timeout %s' which was already specified", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100228 retval = 1;
229 }
Willy Tarreaued446492014-04-28 22:56:38 +0200230 else if (warn) {
231 if (!already_warned(warn)) {
232 memprintf(err, "the '%s' directive is now deprecated in favor of 'timeout %s', and will not be supported in future versions.",
233 args[0], name);
234 retval = 1;
235 }
236 }
Willy Tarreaue219db72007-12-03 01:30:13 +0100237
Willy Tarreaufac5b592014-05-22 08:24:46 +0200238 if (*args[2] != 0) {
239 memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]);
240 retval = -1;
241 }
242
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200243 *tv = MS_TO_TICKS(timeout);
Willy Tarreaue219db72007-12-03 01:30:13 +0100244 return retval;
245}
246
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100247/* This function parses a "rate-limit" statement in a proxy section. It returns
248 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200249 * return zero, it will write an error or warning message into a preallocated
250 * buffer returned at <err>. The function must be called with <args> pointing
251 * to the first command line word, with <proxy> pointing to the proxy being
252 * parsed, and <defpx> to the default proxy or NULL.
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100253 */
254static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200255 struct proxy *defpx, const char *file, int line,
256 char **err)
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100257{
258 int retval, cap;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200259 char *res;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100260 unsigned int *tv = NULL;
261 unsigned int *td = NULL;
262 unsigned int val;
263
264 retval = 0;
265
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 if (strcmp(args[1], "sessions") == 0) {
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200267 tv = &proxy->fe_sps_lim;
268 td = &defpx->fe_sps_lim;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100269 cap = PR_CAP_FE;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200270 }
271 else {
272 memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100273 return -1;
274 }
275
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200276 if (*args[2] == 0) {
277 memprintf(err, "'%s %s' expects expects an integer value (in sessions/second)", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100278 return -1;
279 }
280
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200281 val = strtoul(args[2], &res, 0);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100282 if (*res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200283 memprintf(err, "'%s %s' : unexpected character '%c' in integer value '%s'", args[0], args[1], *res, args[2]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100284 return -1;
285 }
286
287 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200288 memprintf(err, "%s %s will be ignored because %s '%s' has no %s capability",
289 args[0], args[1], proxy_type_str(proxy), proxy->id,
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100290 (cap & PR_CAP_BE) ? "backend" : "frontend");
291 retval = 1;
292 }
293 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200294 memprintf(err, "overwriting %s %s which was already specified", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100295 retval = 1;
296 }
297
298 *tv = val;
299 return retval;
300}
301
Willy Tarreauc35362a2014-04-25 13:58:37 +0200302/* This function parses a "max-keep-alive-queue" statement in a proxy section.
303 * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it
304 * does not return zero, it will write an error or warning message into a
305 * preallocated buffer returned at <err>. The function must be called with
306 * <args> pointing to the first command line word, with <proxy> pointing to
307 * the proxy being parsed, and <defpx> to the default proxy or NULL.
308 */
309static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy,
310 struct proxy *defpx, const char *file, int line,
311 char **err)
312{
313 int retval;
314 char *res;
315 unsigned int val;
316
317 retval = 0;
318
319 if (*args[1] == 0) {
320 memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]);
321 return -1;
322 }
323
324 val = strtol(args[1], &res, 0);
325 if (*res) {
326 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
327 return -1;
328 }
329
330 if (!(proxy->cap & PR_CAP_BE)) {
331 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
332 args[0], proxy_type_str(proxy), proxy->id);
333 retval = 1;
334 }
335
336 /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */
337 proxy->max_ka_queue = val + 1;
338 return retval;
339}
340
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200341/* This function parses a "declare" statement in a proxy section. It returns -1
342 * if there is any error, 1 for warning, otherwise 0. If it does not return zero,
343 * it will write an error or warning message into a preallocated buffer returned
344 * at <err>. The function must be called with <args> pointing to the first command
345 * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the
346 * default proxy or NULL.
347 */
348static int proxy_parse_declare(char **args, int section, struct proxy *curpx,
349 struct proxy *defpx, const char *file, int line,
350 char **err)
351{
352 /* Capture keyword wannot be declared in a default proxy. */
353 if (curpx == defpx) {
354 memprintf(err, "'%s' not avalaible in default section", args[0]);
355 return -1;
356 }
357
358 /* Capture keywork is only avalaible in frontend. */
359 if (!(curpx->cap & PR_CAP_FE)) {
360 memprintf(err, "'%s' only avalaible in frontend or listen section", args[0]);
361 return -1;
362 }
363
364 /* Check mandatory second keyword. */
365 if (!args[1] || !*args[1]) {
366 memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]);
367 return -1;
368 }
369
370 /* Actually, declare is only avalaible for declaring capture
371 * slot, but in the future it can declare maps or variables.
372 * So, this section permits to check and switch acording with
373 * the second keyword.
374 */
375 if (strcmp(args[1], "capture") == 0) {
376 char *error = NULL;
377 long len;
378 struct cap_hdr *hdr;
379
380 /* Check the next keyword. */
381 if (!args[2] || !*args[2] ||
382 (strcmp(args[2], "response") != 0 &&
383 strcmp(args[2], "request") != 0)) {
384 memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]);
385 return -1;
386 }
387
388 /* Check the 'len' keyword. */
389 if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) {
390 memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]);
391 return -1;
392 }
393
394 /* Check the length value. */
395 if (!args[4] || !*args[4]) {
396 memprintf(err, "'%s %s': 'len' requires a numeric value that represents the "
397 "capture length",
398 args[0], args[1]);
399 return -1;
400 }
401
402 /* convert the length value. */
403 len = strtol(args[4], &error, 10);
404 if (*error != '\0') {
405 memprintf(err, "'%s %s': cannot parse the length '%s'.",
406 args[0], args[1], args[3]);
407 return -1;
408 }
409
410 /* check length. */
411 if (len <= 0) {
412 memprintf(err, "length must be > 0");
413 return -1;
414 }
415
416 /* register the capture. */
417 hdr = calloc(sizeof(struct cap_hdr), 1);
418 hdr->name = NULL; /* not a header capture */
419 hdr->namelen = 0;
420 hdr->len = len;
421 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
422
423 if (strcmp(args[2], "request") == 0) {
424 hdr->next = curpx->req_cap;
425 hdr->index = curpx->nb_req_cap++;
426 curpx->req_cap = hdr;
427 }
428 if (strcmp(args[2], "response") == 0) {
429 hdr->next = curpx->rsp_cap;
430 hdr->index = curpx->nb_rsp_cap++;
431 curpx->rsp_cap = hdr;
432 }
433 return 0;
434 }
435 else {
436 memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]);
437 return -1;
438 }
439}
440
Willy Tarreauf79d9502014-03-15 07:22:35 +0100441/* This function inserts proxy <px> into the tree of known proxies. The proxy's
442 * name is used as the storing key so it must already have been initialized.
443 */
444void proxy_store_name(struct proxy *px)
445{
446 px->conf.by_name.key = px->id;
447 ebis_insert(&proxy_by_name, &px->conf.by_name);
448}
449
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200450/* Returns a pointer to the first proxy matching capabilities <cap> and id
451 * <id>. NULL is returned if no match is found. If <table> is non-zero, it
452 * only considers proxies having a table.
Willy Tarreaubc216c42011-08-02 11:25:54 +0200453 */
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200454struct proxy *proxy_find_by_id(int id, int cap, int table)
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200455{
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200456 struct eb32_node *n;
Willy Tarreaubc216c42011-08-02 11:25:54 +0200457
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200458 for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) {
459 struct proxy *px = container_of(n, struct proxy, conf.id);
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100460
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200461 if (px->uuid != id)
462 break;
Alex Williams96532db2009-11-01 21:27:13 -0500463
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200464 if ((px->cap & cap) != cap)
465 continue;
Alex Williams96532db2009-11-01 21:27:13 -0500466
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200467 if (table && !px->table.size)
468 continue;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100469
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200470 return px;
471 }
472 return NULL;
473}
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100474
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200475/* Returns a pointer to the first proxy matching either name <name>, or id
476 * <name> if <name> begins with a '#'. NULL is returned if no match is found.
477 * If <table> is non-zero, it only considers proxies having a table.
478 */
479struct proxy *proxy_find_by_name(const char *name, int cap, int table)
480{
481 struct proxy *curproxy;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200482
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200483 if (*name == '#') {
484 curproxy = proxy_find_by_id(atoi(name + 1), cap, table);
485 if (curproxy)
Willy Tarreauc739aa82015-05-26 11:35:41 +0200486 return curproxy;
Alex Williams96532db2009-11-01 21:27:13 -0500487 }
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100488 else {
489 struct ebpt_node *node;
490
491 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
492 curproxy = container_of(node, struct proxy, conf.by_name);
Alex Williams96532db2009-11-01 21:27:13 -0500493
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100494 if (strcmp(curproxy->id, name) != 0)
495 break;
496
497 if ((curproxy->cap & cap) != cap)
498 continue;
499
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200500 if (table && !curproxy->table.size)
501 continue;
502
Willy Tarreauc739aa82015-05-26 11:35:41 +0200503 return curproxy;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100504 }
505 }
Willy Tarreauc739aa82015-05-26 11:35:41 +0200506 return NULL;
Alex Williams96532db2009-11-01 21:27:13 -0500507}
508
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200509/* Finds the best match for a proxy with capabilities <cap>, name <name> and id
510 * <id>. At most one of <id> or <name> may be different provided that <cap> is
511 * valid. Either <id> or <name> may be left unspecified (0). The purpose is to
512 * find a proxy based on some information from a previous configuration, across
513 * reloads or during information exchange between peers.
514 *
515 * Names are looked up first if present, then IDs are compared if present. In
516 * case of an inexact match whatever is forced in the configuration has
517 * precedence in the following order :
518 * - 1) forced ID (proves a renaming / change of proxy type)
519 * - 2) proxy name+type (may indicate a move if ID differs)
520 * - 3) automatic ID+type (may indicate a renaming)
521 *
522 * Depending on what is found, we can end up in the following situations :
523 *
524 * name id cap | possible causes
525 * -------------+-----------------
526 * -- -- -- | nothing found
527 * -- -- ok | nothing found
528 * -- ok -- | proxy deleted, ID points to next one
529 * -- ok ok | proxy renamed, or deleted with ID pointing to next one
530 * ok -- -- | proxy deleted, but other half with same name still here (before)
531 * ok -- ok | proxy's ID changed (proxy moved in the config file)
532 * ok ok -- | proxy deleted, but other half with same name still here (after)
533 * ok ok ok | perfect match
534 *
535 * Upon return if <diff> is not NULL, it is zeroed then filled with up to 3 bits :
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200536 * - PR_FBM_MISMATCH_ID : proxy was found but ID differs
537 * (and ID was not zero)
538 * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs
539 * (and name was not NULL)
540 * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with
541 * the same name and/or id
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200542 *
543 * Only a valid proxy is returned. If capabilities do not match, NULL is
544 * returned. The caller can check <diff> to report detailed warnings / errors,
545 * and decide whether or not to use what was found.
546 */
547struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff)
548{
549 struct proxy *byname;
550 struct proxy *byid;
551
552 if (!name && !id)
553 return NULL;
554
555 if (diff)
556 *diff = 0;
557
558 byname = byid = NULL;
559
560 if (name) {
561 byname = proxy_find_by_name(name, cap, 0);
562 if (byname && (!id || byname->uuid == id))
563 return byname;
564 }
565
566 /* remaining possiblities :
567 * - name not set
568 * - name set but not found
569 * - name found, but ID doesn't match.
570 */
571 if (id) {
572 byid = proxy_find_by_id(id, cap, 0);
573 if (byid) {
574 if (byname) {
575 /* id+type found, name+type found, but not all 3.
576 * ID wins only if forced, otherwise name wins.
577 */
578 if (byid->options & PR_O_FORCED_ID) {
579 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200580 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200581 return byid;
582 }
583 else {
584 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200585 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200586 return byname;
587 }
588 }
589
590 /* remaining possiblities :
591 * - name not set
592 * - name set but not found
593 */
594 if (name && diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200595 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200596 return byid;
597 }
598
599 /* ID not found */
600 if (byname) {
601 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200602 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200603 return byname;
604 }
605 }
606
607 /* All remaining possiblities will lead to NULL. If we can report more
608 * detailed information to the caller about changed types and/or name,
609 * we'll do it. For example, we could detect that "listen foo" was
610 * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced.
611 * - name not set, ID not found
612 * - name not found, ID not set
613 * - name not found, ID not found
614 */
615 if (!diff)
616 return NULL;
617
618 if (name) {
619 byname = proxy_find_by_name(name, 0, 0);
620 if (byname && (!id || byname->uuid == id))
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200621 *diff |= PR_FBM_MISMATCH_PROXYTYPE;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200622 }
623
624 if (id) {
625 byid = proxy_find_by_id(id, 0, 0);
626 if (byid) {
627 if (!name)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200628 *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200629 else if (byid->options & PR_O_FORCED_ID)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200630 *diff |= PR_FBM_MISMATCH_NAME | PR_FBM_MISMATCH_PROXYTYPE; /* name and type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200631 /* otherwise it's a different proxy that was returned */
632 }
633 }
634 return NULL;
635}
636
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100638 * This function finds a server with matching name within selected proxy.
639 * It also checks if there are more matching servers with
640 * requested name as this often leads into unexpected situations.
641 */
642
643struct server *findserver(const struct proxy *px, const char *name) {
644
645 struct server *cursrv, *target = NULL;
646
647 if (!px)
648 return NULL;
649
650 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
651 if (strcmp(cursrv->id, name))
652 continue;
653
654 if (!target) {
655 target = cursrv;
656 continue;
657 }
658
Cyril Bonté0bb519e2012-04-04 12:57:19 +0200659 Alert("Refusing to use duplicated server '%s' found in proxy: %s!\n",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100660 name, px->id);
661
662 return NULL;
663 }
664
665 return target;
666}
667
Willy Tarreauff01a212009-03-15 13:46:16 +0100668/* This function checks that the designated proxy has no http directives
669 * enabled. It will output a warning if there are, and will fix some of them.
670 * It returns the number of fatal errors encountered. This should be called
671 * at the end of the configuration parsing if the proxy is not in http mode.
672 * The <file> argument is used to construct the error message.
673 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200674int proxy_cfg_ensure_no_http(struct proxy *curproxy)
Willy Tarreauff01a212009-03-15 13:46:16 +0100675{
676 if (curproxy->cookie_name != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200677 Warning("config : cookie will be ignored for %s '%s' (needs 'mode http').\n",
678 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100679 }
680 if (curproxy->rsp_exp != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200681 Warning("config : server regular expressions will be ignored for %s '%s' (needs 'mode http').\n",
682 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100683 }
684 if (curproxy->req_exp != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200685 Warning("config : client regular expressions will be ignored for %s '%s' (needs 'mode http').\n",
686 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100687 }
688 if (curproxy->monitor_uri != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200689 Warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n",
690 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100691 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200692 if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) {
Willy Tarreauff01a212009-03-15 13:46:16 +0100693 curproxy->lbprm.algo &= ~BE_LB_ALGO;
694 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200695 Warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n",
696 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100697 }
Willy Tarreau17804162009-11-09 21:27:51 +0100698 if (curproxy->to_log & (LW_REQ | LW_RESP)) {
699 curproxy->to_log &= ~(LW_REQ | LW_RESP);
Willy Tarreau59ad1a22014-01-29 14:39:58 +0100700 Warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200701 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
Willy Tarreau17804162009-11-09 21:27:51 +0100702 proxy_type_str(curproxy), curproxy->id);
703 }
Willy Tarreau62a61232013-04-12 18:13:46 +0200704 if (curproxy->conf.logformat_string == default_http_log_format ||
705 curproxy->conf.logformat_string == clf_http_log_format) {
706 /* Note: we don't change the directive's file:line number */
707 curproxy->conf.logformat_string = default_tcp_log_format;
708 Warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
709 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
Willy Tarreau196729e2012-05-31 19:30:26 +0200710 proxy_type_str(curproxy), curproxy->id);
711 }
712
Willy Tarreauff01a212009-03-15 13:46:16 +0100713 return 0;
714}
715
Willy Tarreau237250c2011-07-29 01:49:03 +0200716/* Perform the most basic initialization of a proxy :
717 * memset(), list_init(*), reset_timeouts(*).
Willy Tarreaub249e842011-09-07 18:41:08 +0200718 * Any new proxy or peer should be initialized via this function.
Willy Tarreau237250c2011-07-29 01:49:03 +0200719 */
720void init_new_proxy(struct proxy *p)
721{
722 memset(p, 0, sizeof(struct proxy));
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100723 p->obj_type = OBJ_TYPE_PROXY;
Willy Tarreau237250c2011-07-29 01:49:03 +0200724 LIST_INIT(&p->pendconns);
725 LIST_INIT(&p->acl);
726 LIST_INIT(&p->http_req_rules);
Willy Tarreaue365c0b2013-06-11 16:06:12 +0200727 LIST_INIT(&p->http_res_rules);
Willy Tarreau353bc9f2014-04-28 22:05:31 +0200728 LIST_INIT(&p->block_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200729 LIST_INIT(&p->redirect_rules);
730 LIST_INIT(&p->mon_fail_cond);
731 LIST_INIT(&p->switching_rules);
Willy Tarreau4a5cade2012-04-05 21:09:48 +0200732 LIST_INIT(&p->server_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200733 LIST_INIT(&p->persist_rules);
734 LIST_INIT(&p->sticking_rules);
735 LIST_INIT(&p->storersp_rules);
736 LIST_INIT(&p->tcp_req.inspect_rules);
737 LIST_INIT(&p->tcp_rep.inspect_rules);
738 LIST_INIT(&p->tcp_req.l4_rules);
739 LIST_INIT(&p->req_add);
740 LIST_INIT(&p->rsp_add);
741 LIST_INIT(&p->listener_queue);
William Lallemand0f99e342011-10-12 17:50:54 +0200742 LIST_INIT(&p->logsrvs);
William Lallemand723b73a2012-02-08 16:37:49 +0100743 LIST_INIT(&p->logformat);
William Lallemanda73203e2012-03-12 12:48:57 +0100744 LIST_INIT(&p->format_unique_id);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200745 LIST_INIT(&p->conf.bind);
Willy Tarreau4348fad2012-09-20 16:48:07 +0200746 LIST_INIT(&p->conf.listeners);
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200747 LIST_INIT(&p->conf.args.list);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200748 LIST_INIT(&p->tcpcheck_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200749
750 /* Timeouts are defined as -1 */
751 proxy_reset_timeouts(p);
752 p->tcp_rep.inspect_delay = TICK_ETERNITY;
Willy Tarreau050536d2012-10-04 08:47:34 +0200753
754 /* initial uuid is unassigned (-1) */
755 p->uuid = -1;
Willy Tarreau237250c2011-07-29 01:49:03 +0200756}
757
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100758/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200759 * This function creates all proxy sockets. It should be done very early,
760 * typically before privileges are dropped. The sockets will be registered
761 * but not added to any fd_set, in order not to loose them across the fork().
Willy Tarreau562515c2011-07-25 08:11:52 +0200762 * The proxies also start in READY state because they all have their listeners
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200763 * bound.
Willy Tarreau2ff76222007-04-09 19:29:56 +0200764 *
765 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
766 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200767 */
768int start_proxies(int verbose)
769{
770 struct proxy *curproxy;
771 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100772 int lerr, err = ERR_NONE;
773 int pxerr;
774 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775
776 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
777 if (curproxy->state != PR_STNEW)
778 continue; /* already initialized */
779
780 pxerr = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200781 list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100782 if (listener->state != LI_ASSIGNED)
783 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200784
Emeric Bruncf20bf12010-10-22 16:06:11 +0200785 lerr = listener->proto->bind(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200786
Willy Tarreaue6b98942007-10-29 01:09:36 +0100787 /* errors are reported if <verbose> is set or if they are fatal */
788 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
789 if (lerr & ERR_ALERT)
790 Alert("Starting %s %s: %s\n",
791 proxy_type_str(curproxy), curproxy->id, msg);
792 else if (lerr & ERR_WARN)
793 Warning("Starting %s %s: %s\n",
794 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795 }
796
Willy Tarreaue6b98942007-10-29 01:09:36 +0100797 err |= lerr;
798 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100800 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100802 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 pxerr |= 1;
804 continue;
805 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200806 }
807
808 if (!pxerr) {
Willy Tarreau562515c2011-07-25 08:11:52 +0200809 curproxy->state = PR_STREADY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200810 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
811 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100812
813 if (err & ERR_ABORT)
814 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815 }
816
817 return err;
818}
819
820
821/*
Willy Tarreau918ff602011-07-25 16:33:49 +0200822 * This is the proxy management task. It enables proxies when there are enough
Willy Tarreau87b09662015-04-03 00:22:06 +0200823 * free streams, or stops them when the table is full. It is designed to be
Willy Tarreau918ff602011-07-25 16:33:49 +0200824 * called as a task which is woken up upon stopping or when rate limiting must
825 * be enforced.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826 */
Willy Tarreau918ff602011-07-25 16:33:49 +0200827struct task *manage_proxy(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828{
Willy Tarreau918ff602011-07-25 16:33:49 +0200829 struct proxy *p = t->context;
830 int next = TICK_ETERNITY;
Willy Tarreau79584222009-03-06 09:18:27 +0100831 unsigned int wait;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200832
Willy Tarreau918ff602011-07-25 16:33:49 +0200833 /* We should periodically try to enable listeners waiting for a
834 * global resource here.
835 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200836
Willy Tarreau918ff602011-07-25 16:33:49 +0200837 /* first, let's check if we need to stop the proxy */
838 if (unlikely(stopping && p->state != PR_STSTOPPED)) {
839 int t;
840 t = tick_remain(now_ms, p->stop_time);
841 if (t == 0) {
842 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
843 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
844 send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
845 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
846 stop_proxy(p);
847 /* try to free more memory */
848 pool_gc2();
849 }
850 else {
851 next = tick_first(next, p->stop_time);
852 }
853 }
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200854
Willy Tarreau3a925c12013-09-04 17:54:01 +0200855 /* If the proxy holds a stick table, we need to purge all unused
856 * entries. These are all the ones in the table with ref_cnt == 0
857 * and all the ones in the pool used to allocate new entries. Any
Willy Tarreau87b09662015-04-03 00:22:06 +0200858 * entry attached to an existing stream waiting for a store will
Willy Tarreau3a925c12013-09-04 17:54:01 +0200859 * be in neither list. Any entry being dumped will have ref_cnt > 0.
860 * However we protect tables that are being synced to peers.
861 */
862 if (unlikely(stopping && p->state == PR_STSTOPPED && p->table.current)) {
863 if (!p->table.syncing) {
864 stktable_trash_oldest(&p->table, p->table.current);
865 pool_gc2();
866 }
867 if (p->table.current) {
868 /* some entries still remain, let's recheck in one second */
869 next = tick_first(next, tick_add(now_ms, 1000));
870 }
871 }
872
Willy Tarreau918ff602011-07-25 16:33:49 +0200873 /* the rest below is just for frontends */
874 if (!(p->cap & PR_CAP_FE))
875 goto out;
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200876
Willy Tarreau918ff602011-07-25 16:33:49 +0200877 /* check the various reasons we may find to block the frontend */
878 if (unlikely(p->feconn >= p->maxconn)) {
879 if (p->state == PR_STREADY)
880 p->state = PR_STFULL;
881 goto out;
882 }
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100883
Willy Tarreau918ff602011-07-25 16:33:49 +0200884 /* OK we have no reason to block, so let's unblock if we were blocking */
885 if (p->state == PR_STFULL)
886 p->state = PR_STREADY;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100887
Willy Tarreau918ff602011-07-25 16:33:49 +0200888 if (p->fe_sps_lim &&
889 (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) {
890 /* we're blocking because a limit was reached on the number of
891 * requests/s on the frontend. We want to re-check ASAP, which
892 * means in 1 ms before estimated expiration date, because the
893 * timer will have settled down.
894 */
895 next = tick_first(next, tick_add(now_ms, wait));
896 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200897 }
Willy Tarreau918ff602011-07-25 16:33:49 +0200898
899 /* The proxy is not limited so we can re-enable any waiting listener */
900 if (!LIST_ISEMPTY(&p->listener_queue))
901 dequeue_all_listeners(&p->listener_queue);
902 out:
903 t->expire = next;
904 task_queue(t);
905 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200906}
907
908
909/*
910 * this function disables health-check servers so that the process will quickly be ignored
911 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
912 * time will not be used since it would already not listen anymore to the socket.
913 */
914void soft_stop(void)
915{
916 struct proxy *p;
Willy Tarreaubbe11b12011-07-25 11:16:24 +0200917 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918
919 stopping = 1;
920 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200921 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922 while (p) {
923 if (p->state != PR_STSTOPPED) {
Willy Tarreauf8fbcef2008-10-10 17:51:34 +0200924 Warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace);
925 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 +0200926 p->stop_time = tick_add(now_ms, p->grace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927 }
Emeric Brun5a8c0a92010-09-23 18:44:36 +0200928 if (p->table.size && p->table.sync_task)
929 task_wakeup(p->table.sync_task, TASK_WOKEN_MSG);
930
Willy Tarreau918ff602011-07-25 16:33:49 +0200931 /* wake every proxy task up so that they can handle the stopping */
Willy Tarreaud1a33e32012-10-04 00:14:33 +0200932 if (p->task)
933 task_wakeup(p->task, TASK_WOKEN_MSG);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934 p = p->next;
935 }
Willy Tarreaubbe11b12011-07-25 11:16:24 +0200936
937 prs = peers;
938 while (prs) {
939 stop_proxy((struct proxy *)prs->peers_fe);
940 prs = prs->next;
941 }
Willy Tarreaud0807c32010-08-27 18:26:11 +0200942 /* signal zero is used to broadcast the "stopping" event */
943 signal_handler(0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200944}
945
946
Willy Tarreaube58c382011-07-24 18:28:10 +0200947/* Temporarily disables listening on all of the proxy's listeners. Upon
948 * success, the proxy enters the PR_PAUSED state. If disabling at least one
949 * listener returns an error, then the proxy state is set to PR_STERROR
Willy Tarreauce8fe252011-09-07 19:14:57 +0200950 * because we don't know how to resume from this. The function returns 0
951 * if it fails, or non-zero on success.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952 */
Willy Tarreauce8fe252011-09-07 19:14:57 +0200953int pause_proxy(struct proxy *p)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200954{
955 struct listener *l;
Willy Tarreauce8fe252011-09-07 19:14:57 +0200956
957 if (!(p->cap & PR_CAP_FE) || p->state == PR_STERROR ||
958 p->state == PR_STSTOPPED || p->state == PR_STPAUSED)
959 return 1;
960
961 Warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
962 send_log(p, LOG_WARNING, "Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
963
Willy Tarreau4348fad2012-09-20 16:48:07 +0200964 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreaube58c382011-07-24 18:28:10 +0200965 if (!pause_listener(l))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200966 p->state = PR_STERROR;
967 }
Willy Tarreauce8fe252011-09-07 19:14:57 +0200968
969 if (p->state == PR_STERROR) {
970 Warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
971 send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
972 return 0;
973 }
974
975 p->state = PR_STPAUSED;
976 return 1;
Willy Tarreauda250db2008-10-12 12:07:48 +0200977}
978
979
980/*
981 * This function completely stops a proxy and releases its listeners. It has
982 * to be called when going down in order to release the ports so that another
983 * process may bind to them. It must also be called on disabled proxies at the
984 * end of start-up. When all listeners are closed, the proxy is set to the
985 * PR_STSTOPPED state.
986 */
987void stop_proxy(struct proxy *p)
988{
989 struct listener *l;
990
Willy Tarreau4348fad2012-09-20 16:48:07 +0200991 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauda250db2008-10-12 12:07:48 +0200992 unbind_listener(l);
993 if (l->state >= LI_ASSIGNED) {
994 delete_listener(l);
995 listeners--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200996 jobs--;
Willy Tarreauda250db2008-10-12 12:07:48 +0200997 }
998 }
999 p->state = PR_STSTOPPED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001000}
1001
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001002/* This function resumes listening on the specified proxy. It scans all of its
1003 * listeners and tries to enable them all. If any of them fails, the proxy is
1004 * put back to the paused state. It returns 1 upon success, or zero if an error
1005 * is encountered.
1006 */
1007int resume_proxy(struct proxy *p)
1008{
1009 struct listener *l;
1010 int fail;
1011
1012 if (p->state != PR_STPAUSED)
1013 return 1;
1014
1015 Warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
1016 send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
1017
1018 fail = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001019 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001020 if (!resume_listener(l)) {
1021 int port;
1022
1023 port = get_host_port(&l->addr);
1024 if (port) {
1025 Warning("Port %d busy while trying to enable %s %s.\n",
1026 port, proxy_cap_str(p->cap), p->id);
1027 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
1028 port, proxy_cap_str(p->cap), p->id);
1029 }
1030 else {
1031 Warning("Bind on socket %d busy while trying to enable %s %s.\n",
1032 l->luid, proxy_cap_str(p->cap), p->id);
1033 send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n",
1034 l->luid, proxy_cap_str(p->cap), p->id);
1035 }
1036
1037 /* Another port might have been enabled. Let's stop everything. */
1038 fail = 1;
1039 break;
1040 }
1041 }
1042
1043 p->state = PR_STREADY;
1044 if (fail) {
1045 pause_proxy(p);
1046 return 0;
1047 }
1048 return 1;
1049}
1050
Willy Tarreaubaaee002006-06-26 02:48:02 +02001051/*
1052 * This function temporarily disables listening so that another new instance
1053 * can start listening. It is designed to be called upon reception of a
1054 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
1055 * the proxy, or a SIGTTIN can be sent to listen again.
1056 */
1057void pause_proxies(void)
1058{
1059 int err;
1060 struct proxy *p;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001061 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062
1063 err = 0;
1064 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001065 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066 while (p) {
Willy Tarreauce8fe252011-09-07 19:14:57 +02001067 err |= !pause_proxy(p);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001068 p = p->next;
1069 }
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001070
1071 prs = peers;
1072 while (prs) {
1073 p = prs->peers_fe;
Willy Tarreauce8fe252011-09-07 19:14:57 +02001074 err |= !pause_proxy(p);
1075 prs = prs->next;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001076 }
1077
Willy Tarreaubaaee002006-06-26 02:48:02 +02001078 if (err) {
1079 Warning("Some proxies refused to pause, performing soft stop now.\n");
1080 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
1081 soft_stop();
1082 }
1083}
1084
1085
1086/*
1087 * This function reactivates listening. This can be used after a call to
1088 * sig_pause(), for example when a new instance has failed starting up.
1089 * It is designed to be called upon reception of a SIGTTIN.
1090 */
Willy Tarreaube58c382011-07-24 18:28:10 +02001091void resume_proxies(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001092{
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001093 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001094 struct proxy *p;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001095 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001096
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001097 err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001099 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100 while (p) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001101 err |= !resume_proxy(p);
1102 p = p->next;
1103 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001105 prs = peers;
1106 while (prs) {
1107 p = prs->peers_fe;
1108 err |= !resume_proxy(p);
1109 prs = prs->next;
1110 }
Willy Tarreaube58c382011-07-24 18:28:10 +02001111
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001112 if (err) {
1113 Warning("Some proxies refused to resume, a restart is probably needed to resume safe operations.\n");
1114 send_log(p, LOG_WARNING, "Some proxies refused to resume, a restart is probably needed to resume safe operations.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001115 }
1116}
1117
Willy Tarreau87b09662015-04-03 00:22:06 +02001118/* Set current stream's backend to <be>. Nothing is done if the
1119 * stream already had a backend assigned, which is indicated by
Willy Tarreaue7dff022015-04-03 01:14:29 +02001120 * s->flags & SF_BE_ASSIGNED.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001121 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001122 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001123 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001124int stream_set_backend(struct stream *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001125{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001126 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001127 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001128 s->be = be;
1129 be->beconn++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001130 if (be->beconn > be->be_counters.conn_max)
1131 be->be_counters.conn_max = be->beconn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001132 proxy_inc_be_ctr(be);
1133
Willy Tarreau87b09662015-04-03 00:22:06 +02001134 /* assign new parameters to the stream from the new backend */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001135 s->si[1].flags &= ~SI_FL_INDEP_STR;
1136 if (be->options2 & PR_O2_INDEPSTR)
1137 s->si[1].flags |= SI_FL_INDEP_STR;
1138
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001139 /* We want to enable the backend-specific analysers except those which
1140 * were already run as part of the frontend/listener. Note that it would
1141 * be more reliable to store the list of analysers that have been run,
1142 * but what we do here is OK for now.
1143 */
Willy Tarreaub7463292015-09-23 12:20:10 +02001144 s->req.analysers |= be->be_req_ana;
1145 if (strm_li(s))
1146 s->req.analysers &= ~strm_li(s)->analysers;
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001147
Willy Tarreau51aecc72009-07-12 09:47:04 +02001148 /* If the target backend requires HTTP processing, we have to allocate
Willy Tarreaueee5b512015-04-03 23:46:31 +02001149 * the HTTP transaction and hdr_idx if we did not have one.
Willy Tarreau51aecc72009-07-12 09:47:04 +02001150 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001151 if (unlikely(!s->txn && be->http_needed)) {
1152 if (unlikely(!http_alloc_txn(s)))
Willy Tarreau51aecc72009-07-12 09:47:04 +02001153 return 0; /* not enough memory */
Willy Tarreau39e4f622010-05-31 17:01:36 +02001154
1155 /* and now initialize the HTTP transaction state */
1156 http_init_txn(s);
Willy Tarreau51aecc72009-07-12 09:47:04 +02001157 }
1158
Willy Tarreaueee5b512015-04-03 23:46:31 +02001159 if (s->txn) {
1160 if (be->options2 & PR_O2_RSPBUG_OK)
1161 s->txn->rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001162
Willy Tarreaueee5b512015-04-03 23:46:31 +02001163 /* If we chain to an HTTP backend running a different HTTP mode, we
1164 * have to re-adjust the desired keep-alive/close mode to accommodate
1165 * both the frontend's and the backend's modes.
1166 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001167 if (strm_fe(s)->mode == PR_MODE_HTTP && be->mode == PR_MODE_HTTP &&
1168 ((strm_fe(s)->options & PR_O_HTTP_MODE) != (be->options & PR_O_HTTP_MODE)))
Willy Tarreaueee5b512015-04-03 23:46:31 +02001169 http_adjust_conn_mode(s, s->txn, &s->txn->req);
Willy Tarreau80a92c02014-03-12 10:41:13 +01001170
Willy Tarreaueee5b512015-04-03 23:46:31 +02001171 /* If an LB algorithm needs to access some pre-parsed body contents,
1172 * we must not start to forward anything until the connection is
1173 * confirmed otherwise we'll lose the pointer to these data and
1174 * prevent the hash from being doable again after a redispatch.
1175 */
1176 if (be->mode == PR_MODE_HTTP &&
1177 (be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_HI | BE_LB_HASH_PRM))
1178 s->txn->req.flags |= HTTP_MSGF_WAIT_CONN;
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001179
1180 /* we may request to parse a request body */
1181 if ((be->options & PR_O_WREQ_BODY) &&
1182 (s->txn->req.body_len || (s->txn->req.flags & HTTP_MSGF_TE_CHNK)))
1183 s->req.analysers |= AN_REQ_HTTP_BODY;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001184 }
1185
1186 s->flags |= SF_BE_ASSIGNED;
Willy Tarreau96e31212011-05-30 18:10:30 +02001187 if (be->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001188 s->req.flags |= CF_NEVER_WAIT;
1189 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02001190 }
1191
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001192 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001193}
1194
Willy Tarreaudc13c112013-06-21 23:16:39 +02001195static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001196 { CFG_LISTEN, "timeout", proxy_parse_timeout },
1197 { CFG_LISTEN, "clitimeout", proxy_parse_timeout },
1198 { CFG_LISTEN, "contimeout", proxy_parse_timeout },
1199 { CFG_LISTEN, "srvtimeout", proxy_parse_timeout },
Willy Tarreau3a7d2072009-03-05 23:48:25 +01001200 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreauc35362a2014-04-25 13:58:37 +02001201 { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue },
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +02001202 { CFG_LISTEN, "declare", proxy_parse_declare },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001203 { 0, NULL, NULL },
1204}};
1205
1206__attribute__((constructor))
1207static void __proxy_module_init(void)
1208{
1209 cfg_register_keywords(&cfg_kws);
1210}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001211
1212/*
1213 * Local variables:
1214 * c-indent-level: 8
1215 * c-basic-offset: 8
1216 * End:
1217 */