blob: dc6d3e1c6f955daf141084aa6c84b03cd1ab6e74 [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>
Christopher Fauletd7c91962015-04-30 11:48:27 +020038#include <proto/filters.h>
Willy Tarreau51aecc72009-07-12 09:47:04 +020039#include <proto/hdr_idx.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020040#include <proto/listener.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/log.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010042#include <proto/proto_tcp.h>
Willy Tarreau39e4f622010-05-31 17:01:36 +020043#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/proxy.h>
Willy Tarreaud0807c32010-08-27 18:26:11 +020045#include <proto/signal.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020046#include <proto/stream.h>
Emeric Brun5a8c0a92010-09-23 18:44:36 +020047#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
49
Willy Tarreau918ff602011-07-25 16:33:49 +020050int listeners; /* # of proxy listeners, set by cfgparse */
Willy Tarreaubaaee002006-06-26 02:48:02 +020051struct proxy *proxy = NULL; /* list of all existing proxies */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +020052struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */
Willy Tarreauf79d9502014-03-15 07:22:35 +010053struct eb_root proxy_by_name = EB_ROOT; /* tree of proxies sorted by name */
Willy Tarreau10479e42010-12-12 14:00:34 +010054unsigned int error_snapshot_id = 0; /* global ID assigned to each error then incremented */
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreau977b8e42006-12-29 14:19:17 +010056/*
Willy Tarreau816eb542007-11-04 07:04:43 +010057 * This function returns a string containing a name describing capabilities to
58 * report comprehensible error messages. Specifically, it will return the words
Christopher Faulet898566e2016-10-26 11:06:28 +020059 * "frontend", "backend" when appropriate, or "proxy" for all other
Willy Tarreau816eb542007-11-04 07:04:43 +010060 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +010061 */
Willy Tarreau816eb542007-11-04 07:04:43 +010062const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +010063{
Willy Tarreau816eb542007-11-04 07:04:43 +010064 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
65 if (cap & PR_CAP_FE)
66 return "frontend";
67 else if (cap & PR_CAP_BE)
68 return "backend";
Willy Tarreau816eb542007-11-04 07:04:43 +010069 }
70 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +010071}
72
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010073/*
74 * This function returns a string containing the mode of the proxy in a format
75 * suitable for error messages.
76 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010077const char *proxy_mode_str(int mode) {
78
79 if (mode == PR_MODE_TCP)
80 return "tcp";
81 else if (mode == PR_MODE_HTTP)
82 return "http";
83 else if (mode == PR_MODE_HEALTH)
84 return "health";
85 else
86 return "unknown";
87}
88
Willy Tarreauf3950172009-10-10 18:35:51 +020089/*
90 * This function scans the list of backends and servers to retrieve the first
91 * backend and the first server with the given names, and sets them in both
92 * parameters. It returns zero if either is not found, or non-zero and sets
93 * the ones it did not found to NULL. If a NULL pointer is passed for the
94 * backend, only the pointer to the server will be updated.
95 */
96int get_backend_server(const char *bk_name, const char *sv_name,
97 struct proxy **bk, struct server **sv)
98{
99 struct proxy *p;
100 struct server *s;
Willy Tarreau7ecc4202014-03-15 07:57:11 +0100101 int sid;
Willy Tarreauf3950172009-10-10 18:35:51 +0200102
103 *sv = NULL;
104
Willy Tarreau050536d2012-10-04 08:47:34 +0200105 sid = -1;
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200106 if (*sv_name == '#')
107 sid = atoi(sv_name + 1);
108
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200109 p = proxy_be_by_name(bk_name);
Willy Tarreauf3950172009-10-10 18:35:51 +0200110 if (bk)
111 *bk = p;
112 if (!p)
113 return 0;
114
115 for (s = p->srv; s; s = s->next)
Willy Tarreau4055a102012-11-15 00:15:18 +0100116 if ((sid >= 0 && s->puid == sid) ||
117 (sid < 0 && strcmp(s->id, sv_name) == 0))
Willy Tarreauf3950172009-10-10 18:35:51 +0200118 break;
119 *sv = s;
120 if (!s)
121 return 0;
122 return 1;
123}
124
Willy Tarreaue219db72007-12-03 01:30:13 +0100125/* This function parses a "timeout" statement in a proxy section. It returns
126 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200127 * return zero, it will write an error or warning message into a preallocated
128 * buffer returned at <err>. The trailing is not be written. The function must
129 * be called with <args> pointing to the first command line word, with <proxy>
130 * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL.
131 * As a special case for compatibility with older configs, it also accepts
132 * "{cli|srv|con}timeout" in args[0].
Willy Tarreaue219db72007-12-03 01:30:13 +0100133 */
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200134static int proxy_parse_timeout(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200135 struct proxy *defpx, const char *file, int line,
136 char **err)
Willy Tarreaue219db72007-12-03 01:30:13 +0100137{
138 unsigned timeout;
139 int retval, cap;
140 const char *res, *name;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200141 int *tv = NULL;
142 int *td = NULL;
Willy Tarreaued446492014-04-28 22:56:38 +0200143 int warn = 0;
Willy Tarreaue219db72007-12-03 01:30:13 +0100144
145 retval = 0;
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200146
147 /* simply skip "timeout" but remain compatible with old form */
148 if (strcmp(args[0], "timeout") == 0)
149 args++;
150
Willy Tarreaue219db72007-12-03 01:30:13 +0100151 name = args[0];
Willy Tarreaued446492014-04-28 22:56:38 +0200152 if (!strcmp(args[0], "client") || (!strcmp(args[0], "clitimeout") && (warn = WARN_CLITO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100153 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100154 tv = &proxy->timeout.client;
155 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100156 cap = PR_CAP_FE;
157 } else if (!strcmp(args[0], "tarpit")) {
158 tv = &proxy->timeout.tarpit;
159 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100160 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100161 } else if (!strcmp(args[0], "http-keep-alive")) {
162 tv = &proxy->timeout.httpka;
163 td = &defpx->timeout.httpka;
164 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau036fae02008-01-06 13:24:40 +0100165 } else if (!strcmp(args[0], "http-request")) {
166 tv = &proxy->timeout.httpreq;
167 td = &defpx->timeout.httpreq;
Willy Tarreaucd7afc02009-07-12 10:03:17 +0200168 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaued446492014-04-28 22:56:38 +0200169 } else if (!strcmp(args[0], "server") || (!strcmp(args[0], "srvtimeout") && (warn = WARN_SRVTO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100170 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100171 tv = &proxy->timeout.server;
172 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100173 cap = PR_CAP_BE;
Willy Tarreaued446492014-04-28 22:56:38 +0200174 } else if (!strcmp(args[0], "connect") || (!strcmp(args[0], "contimeout") && (warn = WARN_CONTO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100175 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100176 tv = &proxy->timeout.connect;
177 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100178 cap = PR_CAP_BE;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100179 } else if (!strcmp(args[0], "check")) {
180 tv = &proxy->timeout.check;
181 td = &defpx->timeout.check;
182 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100183 } else if (!strcmp(args[0], "queue")) {
184 tv = &proxy->timeout.queue;
185 td = &defpx->timeout.queue;
186 cap = PR_CAP_BE;
Willy Tarreauce887fd2012-05-12 12:50:00 +0200187 } else if (!strcmp(args[0], "tunnel")) {
188 tv = &proxy->timeout.tunnel;
189 td = &defpx->timeout.tunnel;
190 cap = PR_CAP_BE;
Willy Tarreau05cdd962014-05-10 14:30:07 +0200191 } else if (!strcmp(args[0], "client-fin")) {
192 tv = &proxy->timeout.clientfin;
193 td = &defpx->timeout.clientfin;
194 cap = PR_CAP_FE;
195 } else if (!strcmp(args[0], "server-fin")) {
196 tv = &proxy->timeout.serverfin;
197 td = &defpx->timeout.serverfin;
198 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100199 } else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200200 memprintf(err,
201 "'timeout' supports 'client', 'server', 'connect', 'check', "
Willy Tarreau05cdd962014-05-10 14:30:07 +0200202 "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', "
203 "'client-fin' and 'server-fin' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200204 args[0]);
Willy Tarreaue219db72007-12-03 01:30:13 +0100205 return -1;
206 }
207
208 if (*args[1] == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200209 memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100210 return -1;
211 }
212
213 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
214 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200215 memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100216 return -1;
217 }
218
219 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200220 memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability",
221 name, proxy_type_str(proxy), proxy->id,
222 (cap & PR_CAP_BE) ? "backend" : "frontend");
Willy Tarreaue219db72007-12-03 01:30:13 +0100223 retval = 1;
224 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200225 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200226 memprintf(err, "overwriting 'timeout %s' which was already specified", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100227 retval = 1;
228 }
Willy Tarreaued446492014-04-28 22:56:38 +0200229 else if (warn) {
230 if (!already_warned(warn)) {
231 memprintf(err, "the '%s' directive is now deprecated in favor of 'timeout %s', and will not be supported in future versions.",
232 args[0], name);
233 retval = 1;
234 }
235 }
Willy Tarreaue219db72007-12-03 01:30:13 +0100236
Willy Tarreaufac5b592014-05-22 08:24:46 +0200237 if (*args[2] != 0) {
238 memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]);
239 retval = -1;
240 }
241
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200242 *tv = MS_TO_TICKS(timeout);
Willy Tarreaue219db72007-12-03 01:30:13 +0100243 return retval;
244}
245
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100246/* This function parses a "rate-limit" statement in a proxy section. It returns
247 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200248 * return zero, it will write an error or warning message into a preallocated
249 * buffer returned at <err>. The function must be called with <args> pointing
250 * to the first command line word, with <proxy> pointing to the proxy being
251 * parsed, and <defpx> to the default proxy or NULL.
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100252 */
253static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200254 struct proxy *defpx, const char *file, int line,
255 char **err)
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100256{
257 int retval, cap;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200258 char *res;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100259 unsigned int *tv = NULL;
260 unsigned int *td = NULL;
261 unsigned int val;
262
263 retval = 0;
264
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200265 if (strcmp(args[1], "sessions") == 0) {
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200266 tv = &proxy->fe_sps_lim;
267 td = &defpx->fe_sps_lim;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100268 cap = PR_CAP_FE;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200269 }
270 else {
271 memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100272 return -1;
273 }
274
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200275 if (*args[2] == 0) {
276 memprintf(err, "'%s %s' expects expects an integer value (in sessions/second)", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100277 return -1;
278 }
279
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200280 val = strtoul(args[2], &res, 0);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100281 if (*res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200282 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 +0100283 return -1;
284 }
285
286 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 memprintf(err, "%s %s will be ignored because %s '%s' has no %s capability",
288 args[0], args[1], proxy_type_str(proxy), proxy->id,
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100289 (cap & PR_CAP_BE) ? "backend" : "frontend");
290 retval = 1;
291 }
292 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200293 memprintf(err, "overwriting %s %s which was already specified", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100294 retval = 1;
295 }
296
297 *tv = val;
298 return retval;
299}
300
Willy Tarreauc35362a2014-04-25 13:58:37 +0200301/* This function parses a "max-keep-alive-queue" statement in a proxy section.
302 * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it
303 * does not return zero, it will write an error or warning message into a
304 * preallocated buffer returned at <err>. The function must be called with
305 * <args> pointing to the first command line word, with <proxy> pointing to
306 * the proxy being parsed, and <defpx> to the default proxy or NULL.
307 */
308static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy,
309 struct proxy *defpx, const char *file, int line,
310 char **err)
311{
312 int retval;
313 char *res;
314 unsigned int val;
315
316 retval = 0;
317
318 if (*args[1] == 0) {
319 memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]);
320 return -1;
321 }
322
323 val = strtol(args[1], &res, 0);
324 if (*res) {
325 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
326 return -1;
327 }
328
329 if (!(proxy->cap & PR_CAP_BE)) {
330 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
331 args[0], proxy_type_str(proxy), proxy->id);
332 retval = 1;
333 }
334
335 /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */
336 proxy->max_ka_queue = val + 1;
337 return retval;
338}
339
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200340/* This function parses a "declare" statement in a proxy section. It returns -1
341 * if there is any error, 1 for warning, otherwise 0. If it does not return zero,
342 * it will write an error or warning message into a preallocated buffer returned
343 * at <err>. The function must be called with <args> pointing to the first command
344 * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the
345 * default proxy or NULL.
346 */
347static int proxy_parse_declare(char **args, int section, struct proxy *curpx,
348 struct proxy *defpx, const char *file, int line,
349 char **err)
350{
351 /* Capture keyword wannot be declared in a default proxy. */
352 if (curpx == defpx) {
353 memprintf(err, "'%s' not avalaible in default section", args[0]);
354 return -1;
355 }
356
357 /* Capture keywork is only avalaible in frontend. */
358 if (!(curpx->cap & PR_CAP_FE)) {
359 memprintf(err, "'%s' only avalaible in frontend or listen section", args[0]);
360 return -1;
361 }
362
363 /* Check mandatory second keyword. */
364 if (!args[1] || !*args[1]) {
365 memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]);
366 return -1;
367 }
368
369 /* Actually, declare is only avalaible for declaring capture
370 * slot, but in the future it can declare maps or variables.
371 * So, this section permits to check and switch acording with
372 * the second keyword.
373 */
374 if (strcmp(args[1], "capture") == 0) {
375 char *error = NULL;
376 long len;
377 struct cap_hdr *hdr;
378
379 /* Check the next keyword. */
380 if (!args[2] || !*args[2] ||
381 (strcmp(args[2], "response") != 0 &&
382 strcmp(args[2], "request") != 0)) {
383 memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]);
384 return -1;
385 }
386
387 /* Check the 'len' keyword. */
388 if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) {
389 memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]);
390 return -1;
391 }
392
393 /* Check the length value. */
394 if (!args[4] || !*args[4]) {
395 memprintf(err, "'%s %s': 'len' requires a numeric value that represents the "
396 "capture length",
397 args[0], args[1]);
398 return -1;
399 }
400
401 /* convert the length value. */
402 len = strtol(args[4], &error, 10);
403 if (*error != '\0') {
404 memprintf(err, "'%s %s': cannot parse the length '%s'.",
405 args[0], args[1], args[3]);
406 return -1;
407 }
408
409 /* check length. */
410 if (len <= 0) {
411 memprintf(err, "length must be > 0");
412 return -1;
413 }
414
415 /* register the capture. */
Vincent Bernat02779b62016-04-03 13:48:43 +0200416 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200417 hdr->name = NULL; /* not a header capture */
418 hdr->namelen = 0;
419 hdr->len = len;
420 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
421
422 if (strcmp(args[2], "request") == 0) {
423 hdr->next = curpx->req_cap;
424 hdr->index = curpx->nb_req_cap++;
425 curpx->req_cap = hdr;
426 }
427 if (strcmp(args[2], "response") == 0) {
428 hdr->next = curpx->rsp_cap;
429 hdr->index = curpx->nb_rsp_cap++;
430 curpx->rsp_cap = hdr;
431 }
432 return 0;
433 }
434 else {
435 memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]);
436 return -1;
437 }
438}
439
Willy Tarreauf79d9502014-03-15 07:22:35 +0100440/* This function inserts proxy <px> into the tree of known proxies. The proxy's
441 * name is used as the storing key so it must already have been initialized.
442 */
443void proxy_store_name(struct proxy *px)
444{
445 px->conf.by_name.key = px->id;
446 ebis_insert(&proxy_by_name, &px->conf.by_name);
447}
448
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200449/* Returns a pointer to the first proxy matching capabilities <cap> and id
450 * <id>. NULL is returned if no match is found. If <table> is non-zero, it
451 * only considers proxies having a table.
Willy Tarreaubc216c42011-08-02 11:25:54 +0200452 */
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200453struct proxy *proxy_find_by_id(int id, int cap, int table)
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200454{
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200455 struct eb32_node *n;
Willy Tarreaubc216c42011-08-02 11:25:54 +0200456
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200457 for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) {
458 struct proxy *px = container_of(n, struct proxy, conf.id);
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100459
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200460 if (px->uuid != id)
461 break;
Alex Williams96532db2009-11-01 21:27:13 -0500462
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200463 if ((px->cap & cap) != cap)
464 continue;
Alex Williams96532db2009-11-01 21:27:13 -0500465
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200466 if (table && !px->table.size)
467 continue;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100468
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200469 return px;
470 }
471 return NULL;
472}
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100473
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200474/* Returns a pointer to the first proxy matching either name <name>, or id
475 * <name> if <name> begins with a '#'. NULL is returned if no match is found.
476 * If <table> is non-zero, it only considers proxies having a table.
477 */
478struct proxy *proxy_find_by_name(const char *name, int cap, int table)
479{
480 struct proxy *curproxy;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200481
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200482 if (*name == '#') {
483 curproxy = proxy_find_by_id(atoi(name + 1), cap, table);
484 if (curproxy)
Willy Tarreauc739aa82015-05-26 11:35:41 +0200485 return curproxy;
Alex Williams96532db2009-11-01 21:27:13 -0500486 }
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100487 else {
488 struct ebpt_node *node;
489
490 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
491 curproxy = container_of(node, struct proxy, conf.by_name);
Alex Williams96532db2009-11-01 21:27:13 -0500492
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100493 if (strcmp(curproxy->id, name) != 0)
494 break;
495
496 if ((curproxy->cap & cap) != cap)
497 continue;
498
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200499 if (table && !curproxy->table.size)
500 continue;
501
Willy Tarreauc739aa82015-05-26 11:35:41 +0200502 return curproxy;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100503 }
504 }
Willy Tarreauc739aa82015-05-26 11:35:41 +0200505 return NULL;
Alex Williams96532db2009-11-01 21:27:13 -0500506}
507
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200508/* Finds the best match for a proxy with capabilities <cap>, name <name> and id
509 * <id>. At most one of <id> or <name> may be different provided that <cap> is
510 * valid. Either <id> or <name> may be left unspecified (0). The purpose is to
511 * find a proxy based on some information from a previous configuration, across
512 * reloads or during information exchange between peers.
513 *
514 * Names are looked up first if present, then IDs are compared if present. In
515 * case of an inexact match whatever is forced in the configuration has
516 * precedence in the following order :
517 * - 1) forced ID (proves a renaming / change of proxy type)
518 * - 2) proxy name+type (may indicate a move if ID differs)
519 * - 3) automatic ID+type (may indicate a renaming)
520 *
521 * Depending on what is found, we can end up in the following situations :
522 *
523 * name id cap | possible causes
524 * -------------+-----------------
525 * -- -- -- | nothing found
526 * -- -- ok | nothing found
527 * -- ok -- | proxy deleted, ID points to next one
528 * -- ok ok | proxy renamed, or deleted with ID pointing to next one
529 * ok -- -- | proxy deleted, but other half with same name still here (before)
530 * ok -- ok | proxy's ID changed (proxy moved in the config file)
531 * ok ok -- | proxy deleted, but other half with same name still here (after)
532 * ok ok ok | perfect match
533 *
534 * Upon return if <diff> is not NULL, it is zeroed then filled with up to 3 bits :
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200535 * - PR_FBM_MISMATCH_ID : proxy was found but ID differs
536 * (and ID was not zero)
537 * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs
538 * (and name was not NULL)
539 * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with
540 * the same name and/or id
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200541 *
542 * Only a valid proxy is returned. If capabilities do not match, NULL is
543 * returned. The caller can check <diff> to report detailed warnings / errors,
544 * and decide whether or not to use what was found.
545 */
546struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff)
547{
548 struct proxy *byname;
549 struct proxy *byid;
550
551 if (!name && !id)
552 return NULL;
553
554 if (diff)
555 *diff = 0;
556
557 byname = byid = NULL;
558
559 if (name) {
560 byname = proxy_find_by_name(name, cap, 0);
561 if (byname && (!id || byname->uuid == id))
562 return byname;
563 }
564
565 /* remaining possiblities :
566 * - name not set
567 * - name set but not found
568 * - name found, but ID doesn't match.
569 */
570 if (id) {
571 byid = proxy_find_by_id(id, cap, 0);
572 if (byid) {
573 if (byname) {
574 /* id+type found, name+type found, but not all 3.
575 * ID wins only if forced, otherwise name wins.
576 */
577 if (byid->options & PR_O_FORCED_ID) {
578 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200579 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200580 return byid;
581 }
582 else {
583 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200584 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200585 return byname;
586 }
587 }
588
589 /* remaining possiblities :
590 * - name not set
591 * - name set but not found
592 */
593 if (name && diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200594 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200595 return byid;
596 }
597
598 /* ID not found */
599 if (byname) {
600 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200601 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200602 return byname;
603 }
604 }
605
606 /* All remaining possiblities will lead to NULL. If we can report more
607 * detailed information to the caller about changed types and/or name,
608 * we'll do it. For example, we could detect that "listen foo" was
609 * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced.
610 * - name not set, ID not found
611 * - name not found, ID not set
612 * - name not found, ID not found
613 */
614 if (!diff)
615 return NULL;
616
617 if (name) {
618 byname = proxy_find_by_name(name, 0, 0);
619 if (byname && (!id || byname->uuid == id))
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200620 *diff |= PR_FBM_MISMATCH_PROXYTYPE;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200621 }
622
623 if (id) {
624 byid = proxy_find_by_id(id, 0, 0);
625 if (byid) {
626 if (!name)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200627 *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200628 else if (byid->options & PR_O_FORCED_ID)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200629 *diff |= PR_FBM_MISMATCH_NAME | PR_FBM_MISMATCH_PROXYTYPE; /* name and type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200630 /* otherwise it's a different proxy that was returned */
631 }
632 }
633 return NULL;
634}
635
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100637 * This function finds a server with matching name within selected proxy.
638 * It also checks if there are more matching servers with
639 * requested name as this often leads into unexpected situations.
640 */
641
642struct server *findserver(const struct proxy *px, const char *name) {
643
644 struct server *cursrv, *target = NULL;
645
646 if (!px)
647 return NULL;
648
649 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
650 if (strcmp(cursrv->id, name))
651 continue;
652
653 if (!target) {
654 target = cursrv;
655 continue;
656 }
657
Cyril Bonté0bb519e2012-04-04 12:57:19 +0200658 Alert("Refusing to use duplicated server '%s' found in proxy: %s!\n",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100659 name, px->id);
660
661 return NULL;
662 }
663
664 return target;
665}
666
Willy Tarreauff01a212009-03-15 13:46:16 +0100667/* This function checks that the designated proxy has no http directives
668 * enabled. It will output a warning if there are, and will fix some of them.
669 * It returns the number of fatal errors encountered. This should be called
670 * at the end of the configuration parsing if the proxy is not in http mode.
671 * The <file> argument is used to construct the error message.
672 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200673int proxy_cfg_ensure_no_http(struct proxy *curproxy)
Willy Tarreauff01a212009-03-15 13:46:16 +0100674{
675 if (curproxy->cookie_name != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200676 Warning("config : cookie will be ignored for %s '%s' (needs 'mode http').\n",
677 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100678 }
679 if (curproxy->rsp_exp != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200680 Warning("config : server regular expressions will be ignored for %s '%s' (needs 'mode http').\n",
681 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100682 }
683 if (curproxy->req_exp != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200684 Warning("config : client regular expressions will be ignored for %s '%s' (needs 'mode http').\n",
685 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100686 }
687 if (curproxy->monitor_uri != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200688 Warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n",
689 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100690 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200691 if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) {
Willy Tarreauff01a212009-03-15 13:46:16 +0100692 curproxy->lbprm.algo &= ~BE_LB_ALGO;
693 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200694 Warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n",
695 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100696 }
Willy Tarreau17804162009-11-09 21:27:51 +0100697 if (curproxy->to_log & (LW_REQ | LW_RESP)) {
698 curproxy->to_log &= ~(LW_REQ | LW_RESP);
Willy Tarreau59ad1a22014-01-29 14:39:58 +0100699 Warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200700 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
Willy Tarreau17804162009-11-09 21:27:51 +0100701 proxy_type_str(curproxy), curproxy->id);
702 }
Willy Tarreau62a61232013-04-12 18:13:46 +0200703 if (curproxy->conf.logformat_string == default_http_log_format ||
704 curproxy->conf.logformat_string == clf_http_log_format) {
705 /* Note: we don't change the directive's file:line number */
706 curproxy->conf.logformat_string = default_tcp_log_format;
707 Warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
708 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
Willy Tarreau196729e2012-05-31 19:30:26 +0200709 proxy_type_str(curproxy), curproxy->id);
710 }
711
Willy Tarreauff01a212009-03-15 13:46:16 +0100712 return 0;
713}
714
Willy Tarreau237250c2011-07-29 01:49:03 +0200715/* Perform the most basic initialization of a proxy :
716 * memset(), list_init(*), reset_timeouts(*).
Willy Tarreaub249e842011-09-07 18:41:08 +0200717 * Any new proxy or peer should be initialized via this function.
Willy Tarreau237250c2011-07-29 01:49:03 +0200718 */
719void init_new_proxy(struct proxy *p)
720{
721 memset(p, 0, sizeof(struct proxy));
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100722 p->obj_type = OBJ_TYPE_PROXY;
Willy Tarreau237250c2011-07-29 01:49:03 +0200723 LIST_INIT(&p->pendconns);
724 LIST_INIT(&p->acl);
725 LIST_INIT(&p->http_req_rules);
Willy Tarreaue365c0b2013-06-11 16:06:12 +0200726 LIST_INIT(&p->http_res_rules);
Willy Tarreau353bc9f2014-04-28 22:05:31 +0200727 LIST_INIT(&p->block_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200728 LIST_INIT(&p->redirect_rules);
729 LIST_INIT(&p->mon_fail_cond);
730 LIST_INIT(&p->switching_rules);
Willy Tarreau4a5cade2012-04-05 21:09:48 +0200731 LIST_INIT(&p->server_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200732 LIST_INIT(&p->persist_rules);
733 LIST_INIT(&p->sticking_rules);
734 LIST_INIT(&p->storersp_rules);
735 LIST_INIT(&p->tcp_req.inspect_rules);
736 LIST_INIT(&p->tcp_rep.inspect_rules);
737 LIST_INIT(&p->tcp_req.l4_rules);
Willy Tarreau620408f2016-10-21 16:37:51 +0200738 LIST_INIT(&p->tcp_req.l5_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200739 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);
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200744 LIST_INIT(&p->logformat_sd);
William Lallemanda73203e2012-03-12 12:48:57 +0100745 LIST_INIT(&p->format_unique_id);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200746 LIST_INIT(&p->conf.bind);
Willy Tarreau4348fad2012-09-20 16:48:07 +0200747 LIST_INIT(&p->conf.listeners);
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200748 LIST_INIT(&p->conf.args.list);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200749 LIST_INIT(&p->tcpcheck_rules);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100750 LIST_INIT(&p->filter_configs);
Willy Tarreau237250c2011-07-29 01:49:03 +0200751
752 /* Timeouts are defined as -1 */
753 proxy_reset_timeouts(p);
754 p->tcp_rep.inspect_delay = TICK_ETERNITY;
Willy Tarreau050536d2012-10-04 08:47:34 +0200755
756 /* initial uuid is unassigned (-1) */
757 p->uuid = -1;
Willy Tarreau237250c2011-07-29 01:49:03 +0200758}
759
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100760/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200761 * This function creates all proxy sockets. It should be done very early,
762 * typically before privileges are dropped. The sockets will be registered
763 * but not added to any fd_set, in order not to loose them across the fork().
Willy Tarreau562515c2011-07-25 08:11:52 +0200764 * The proxies also start in READY state because they all have their listeners
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200765 * bound.
Willy Tarreau2ff76222007-04-09 19:29:56 +0200766 *
767 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
768 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200769 */
770int start_proxies(int verbose)
771{
772 struct proxy *curproxy;
773 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100774 int lerr, err = ERR_NONE;
775 int pxerr;
776 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777
778 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
779 if (curproxy->state != PR_STNEW)
780 continue; /* already initialized */
781
782 pxerr = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200783 list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100784 if (listener->state != LI_ASSIGNED)
785 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200786
Emeric Bruncf20bf12010-10-22 16:06:11 +0200787 lerr = listener->proto->bind(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788
Willy Tarreaue6b98942007-10-29 01:09:36 +0100789 /* errors are reported if <verbose> is set or if they are fatal */
790 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
791 if (lerr & ERR_ALERT)
792 Alert("Starting %s %s: %s\n",
793 proxy_type_str(curproxy), curproxy->id, msg);
794 else if (lerr & ERR_WARN)
795 Warning("Starting %s %s: %s\n",
796 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797 }
798
Willy Tarreaue6b98942007-10-29 01:09:36 +0100799 err |= lerr;
800 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100802 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100804 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 pxerr |= 1;
806 continue;
807 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808 }
809
810 if (!pxerr) {
Willy Tarreau562515c2011-07-25 08:11:52 +0200811 curproxy->state = PR_STREADY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
813 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100814
815 if (err & ERR_ABORT)
816 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 }
818
819 return err;
820}
821
822
823/*
Willy Tarreau918ff602011-07-25 16:33:49 +0200824 * This is the proxy management task. It enables proxies when there are enough
Willy Tarreau87b09662015-04-03 00:22:06 +0200825 * free streams, or stops them when the table is full. It is designed to be
Willy Tarreau918ff602011-07-25 16:33:49 +0200826 * called as a task which is woken up upon stopping or when rate limiting must
827 * be enforced.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828 */
Willy Tarreau918ff602011-07-25 16:33:49 +0200829struct task *manage_proxy(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830{
Willy Tarreau918ff602011-07-25 16:33:49 +0200831 struct proxy *p = t->context;
832 int next = TICK_ETERNITY;
Willy Tarreau79584222009-03-06 09:18:27 +0100833 unsigned int wait;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834
Willy Tarreau918ff602011-07-25 16:33:49 +0200835 /* We should periodically try to enable listeners waiting for a
836 * global resource here.
837 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838
Willy Tarreau918ff602011-07-25 16:33:49 +0200839 /* first, let's check if we need to stop the proxy */
840 if (unlikely(stopping && p->state != PR_STSTOPPED)) {
841 int t;
842 t = tick_remain(now_ms, p->stop_time);
843 if (t == 0) {
844 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 send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
847 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
848 stop_proxy(p);
849 /* try to free more memory */
850 pool_gc2();
851 }
852 else {
853 next = tick_first(next, p->stop_time);
854 }
855 }
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200856
Willy Tarreau3a925c12013-09-04 17:54:01 +0200857 /* If the proxy holds a stick table, we need to purge all unused
858 * entries. These are all the ones in the table with ref_cnt == 0
859 * and all the ones in the pool used to allocate new entries. Any
Willy Tarreau87b09662015-04-03 00:22:06 +0200860 * entry attached to an existing stream waiting for a store will
Willy Tarreau3a925c12013-09-04 17:54:01 +0200861 * be in neither list. Any entry being dumped will have ref_cnt > 0.
862 * However we protect tables that are being synced to peers.
863 */
864 if (unlikely(stopping && p->state == PR_STSTOPPED && p->table.current)) {
865 if (!p->table.syncing) {
866 stktable_trash_oldest(&p->table, p->table.current);
867 pool_gc2();
868 }
869 if (p->table.current) {
870 /* some entries still remain, let's recheck in one second */
871 next = tick_first(next, tick_add(now_ms, 1000));
872 }
873 }
874
Willy Tarreau918ff602011-07-25 16:33:49 +0200875 /* the rest below is just for frontends */
876 if (!(p->cap & PR_CAP_FE))
877 goto out;
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200878
Willy Tarreau918ff602011-07-25 16:33:49 +0200879 /* check the various reasons we may find to block the frontend */
880 if (unlikely(p->feconn >= p->maxconn)) {
881 if (p->state == PR_STREADY)
882 p->state = PR_STFULL;
883 goto out;
884 }
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100885
Willy Tarreau918ff602011-07-25 16:33:49 +0200886 /* OK we have no reason to block, so let's unblock if we were blocking */
887 if (p->state == PR_STFULL)
888 p->state = PR_STREADY;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100889
Willy Tarreau918ff602011-07-25 16:33:49 +0200890 if (p->fe_sps_lim &&
891 (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) {
892 /* we're blocking because a limit was reached on the number of
893 * requests/s on the frontend. We want to re-check ASAP, which
894 * means in 1 ms before estimated expiration date, because the
895 * timer will have settled down.
896 */
897 next = tick_first(next, tick_add(now_ms, wait));
898 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200899 }
Willy Tarreau918ff602011-07-25 16:33:49 +0200900
901 /* The proxy is not limited so we can re-enable any waiting listener */
902 if (!LIST_ISEMPTY(&p->listener_queue))
903 dequeue_all_listeners(&p->listener_queue);
904 out:
905 t->expire = next;
906 task_queue(t);
907 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200908}
909
910
911/*
912 * this function disables health-check servers so that the process will quickly be ignored
913 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
914 * time will not be used since it would already not listen anymore to the socket.
915 */
916void soft_stop(void)
917{
918 struct proxy *p;
Willy Tarreaubbe11b12011-07-25 11:16:24 +0200919 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920
921 stopping = 1;
922 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200923 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 while (p) {
925 if (p->state != PR_STSTOPPED) {
Willy Tarreauf8fbcef2008-10-10 17:51:34 +0200926 Warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace);
927 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 +0200928 p->stop_time = tick_add(now_ms, p->grace);
Emeric Brun5a8c0a92010-09-23 18:44:36 +0200929
Willy Tarreau20b7afb2015-09-28 16:35:04 +0200930 /* Note: do not wake up stopped proxies' task nor their tables'
931 * tasks as these ones might point to already released entries.
932 */
933 if (p->table.size && p->table.sync_task)
934 task_wakeup(p->table.sync_task, TASK_WOKEN_MSG);
935
936 if (p->task)
937 task_wakeup(p->task, TASK_WOKEN_MSG);
938 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939 p = p->next;
940 }
Willy Tarreaubbe11b12011-07-25 11:16:24 +0200941
942 prs = peers;
943 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +0200944 if (prs->peers_fe)
945 stop_proxy(prs->peers_fe);
Willy Tarreaubbe11b12011-07-25 11:16:24 +0200946 prs = prs->next;
947 }
Willy Tarreaud0807c32010-08-27 18:26:11 +0200948 /* signal zero is used to broadcast the "stopping" event */
949 signal_handler(0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950}
951
952
Willy Tarreaube58c382011-07-24 18:28:10 +0200953/* Temporarily disables listening on all of the proxy's listeners. Upon
954 * success, the proxy enters the PR_PAUSED state. If disabling at least one
955 * listener returns an error, then the proxy state is set to PR_STERROR
Willy Tarreauce8fe252011-09-07 19:14:57 +0200956 * because we don't know how to resume from this. The function returns 0
957 * if it fails, or non-zero on success.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958 */
Willy Tarreauce8fe252011-09-07 19:14:57 +0200959int pause_proxy(struct proxy *p)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200960{
961 struct listener *l;
Willy Tarreauce8fe252011-09-07 19:14:57 +0200962
963 if (!(p->cap & PR_CAP_FE) || p->state == PR_STERROR ||
964 p->state == PR_STSTOPPED || p->state == PR_STPAUSED)
965 return 1;
966
967 Warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
968 send_log(p, LOG_WARNING, "Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
969
Willy Tarreau4348fad2012-09-20 16:48:07 +0200970 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreaube58c382011-07-24 18:28:10 +0200971 if (!pause_listener(l))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200972 p->state = PR_STERROR;
973 }
Willy Tarreauce8fe252011-09-07 19:14:57 +0200974
975 if (p->state == PR_STERROR) {
976 Warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
977 send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
978 return 0;
979 }
980
981 p->state = PR_STPAUSED;
982 return 1;
Willy Tarreauda250db2008-10-12 12:07:48 +0200983}
984
985
986/*
987 * This function completely stops a proxy and releases its listeners. It has
988 * to be called when going down in order to release the ports so that another
989 * process may bind to them. It must also be called on disabled proxies at the
990 * end of start-up. When all listeners are closed, the proxy is set to the
991 * PR_STSTOPPED state.
992 */
993void stop_proxy(struct proxy *p)
994{
995 struct listener *l;
996
Willy Tarreau4348fad2012-09-20 16:48:07 +0200997 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauda250db2008-10-12 12:07:48 +0200998 unbind_listener(l);
999 if (l->state >= LI_ASSIGNED) {
1000 delete_listener(l);
1001 listeners--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02001002 jobs--;
Willy Tarreauda250db2008-10-12 12:07:48 +02001003 }
1004 }
1005 p->state = PR_STSTOPPED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001006}
1007
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001008/* This function resumes listening on the specified proxy. It scans all of its
1009 * listeners and tries to enable them all. If any of them fails, the proxy is
1010 * put back to the paused state. It returns 1 upon success, or zero if an error
1011 * is encountered.
1012 */
1013int resume_proxy(struct proxy *p)
1014{
1015 struct listener *l;
1016 int fail;
1017
1018 if (p->state != PR_STPAUSED)
1019 return 1;
1020
1021 Warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
1022 send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
1023
1024 fail = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001025 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001026 if (!resume_listener(l)) {
1027 int port;
1028
1029 port = get_host_port(&l->addr);
1030 if (port) {
1031 Warning("Port %d busy while trying to enable %s %s.\n",
1032 port, proxy_cap_str(p->cap), p->id);
1033 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
1034 port, proxy_cap_str(p->cap), p->id);
1035 }
1036 else {
1037 Warning("Bind on socket %d busy while trying to enable %s %s.\n",
1038 l->luid, proxy_cap_str(p->cap), p->id);
1039 send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n",
1040 l->luid, proxy_cap_str(p->cap), p->id);
1041 }
1042
1043 /* Another port might have been enabled. Let's stop everything. */
1044 fail = 1;
1045 break;
1046 }
1047 }
1048
1049 p->state = PR_STREADY;
1050 if (fail) {
1051 pause_proxy(p);
1052 return 0;
1053 }
1054 return 1;
1055}
1056
Willy Tarreaubaaee002006-06-26 02:48:02 +02001057/*
1058 * This function temporarily disables listening so that another new instance
1059 * can start listening. It is designed to be called upon reception of a
1060 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
1061 * the proxy, or a SIGTTIN can be sent to listen again.
1062 */
1063void pause_proxies(void)
1064{
1065 int err;
1066 struct proxy *p;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001067 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001068
1069 err = 0;
1070 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001071 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001072 while (p) {
Willy Tarreauce8fe252011-09-07 19:14:57 +02001073 err |= !pause_proxy(p);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001074 p = p->next;
1075 }
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001076
1077 prs = peers;
1078 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001079 if (prs->peers_fe)
1080 err |= !pause_proxy(prs->peers_fe);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001081 prs = prs->next;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001082 }
1083
Willy Tarreaubaaee002006-06-26 02:48:02 +02001084 if (err) {
1085 Warning("Some proxies refused to pause, performing soft stop now.\n");
1086 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
1087 soft_stop();
1088 }
1089}
1090
1091
1092/*
1093 * This function reactivates listening. This can be used after a call to
1094 * sig_pause(), for example when a new instance has failed starting up.
1095 * It is designed to be called upon reception of a SIGTTIN.
1096 */
Willy Tarreaube58c382011-07-24 18:28:10 +02001097void resume_proxies(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098{
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001099 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100 struct proxy *p;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001101 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001102
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001103 err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001105 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001106 while (p) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001107 err |= !resume_proxy(p);
1108 p = p->next;
1109 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001110
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001111 prs = peers;
1112 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001113 if (prs->peers_fe)
1114 err |= !resume_proxy(prs->peers_fe);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001115 prs = prs->next;
1116 }
Willy Tarreaube58c382011-07-24 18:28:10 +02001117
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001118 if (err) {
1119 Warning("Some proxies refused to resume, a restart is probably needed to resume safe operations.\n");
1120 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 +02001121 }
1122}
1123
Willy Tarreau87b09662015-04-03 00:22:06 +02001124/* Set current stream's backend to <be>. Nothing is done if the
1125 * stream already had a backend assigned, which is indicated by
Willy Tarreaue7dff022015-04-03 01:14:29 +02001126 * s->flags & SF_BE_ASSIGNED.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001127 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001128 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001129 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001130int stream_set_backend(struct stream *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001131{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001132 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001133 return 1;
Christopher Faulet41179042016-06-21 11:54:52 +02001134
1135 if (flt_set_stream_backend(s, be) < 0)
1136 return 0;
1137
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001138 s->be = be;
1139 be->beconn++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001140 if (be->beconn > be->be_counters.conn_max)
1141 be->be_counters.conn_max = be->beconn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001142 proxy_inc_be_ctr(be);
1143
Willy Tarreau87b09662015-04-03 00:22:06 +02001144 /* assign new parameters to the stream from the new backend */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001145 s->si[1].flags &= ~SI_FL_INDEP_STR;
1146 if (be->options2 & PR_O2_INDEPSTR)
1147 s->si[1].flags |= SI_FL_INDEP_STR;
1148
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001149 /* We want to enable the backend-specific analysers except those which
1150 * were already run as part of the frontend/listener. Note that it would
1151 * be more reliable to store the list of analysers that have been run,
1152 * but what we do here is OK for now.
1153 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02001154 s->req.analysers |= be->be_req_ana & (strm_li(s) ? ~strm_li(s)->analysers : 0);
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001155
Willy Tarreau51aecc72009-07-12 09:47:04 +02001156 /* If the target backend requires HTTP processing, we have to allocate
Willy Tarreaueee5b512015-04-03 23:46:31 +02001157 * the HTTP transaction and hdr_idx if we did not have one.
Willy Tarreau51aecc72009-07-12 09:47:04 +02001158 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001159 if (unlikely(!s->txn && be->http_needed)) {
1160 if (unlikely(!http_alloc_txn(s)))
Willy Tarreau51aecc72009-07-12 09:47:04 +02001161 return 0; /* not enough memory */
Willy Tarreau39e4f622010-05-31 17:01:36 +02001162
1163 /* and now initialize the HTTP transaction state */
1164 http_init_txn(s);
Willy Tarreau51aecc72009-07-12 09:47:04 +02001165 }
1166
Christopher Faulet309c6412015-12-02 09:57:32 +01001167 /* Be sure to filter request headers if the backend is an HTTP proxy and
1168 * if there are filters attached to the stream. */
1169 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
1170 s->req.analysers |= AN_FLT_HTTP_HDRS;
1171
Willy Tarreaueee5b512015-04-03 23:46:31 +02001172 if (s->txn) {
1173 if (be->options2 & PR_O2_RSPBUG_OK)
1174 s->txn->rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001175
Willy Tarreaueee5b512015-04-03 23:46:31 +02001176 /* If we chain to an HTTP backend running a different HTTP mode, we
1177 * have to re-adjust the desired keep-alive/close mode to accommodate
1178 * both the frontend's and the backend's modes.
1179 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001180 if (strm_fe(s)->mode == PR_MODE_HTTP && be->mode == PR_MODE_HTTP &&
1181 ((strm_fe(s)->options & PR_O_HTTP_MODE) != (be->options & PR_O_HTTP_MODE)))
Willy Tarreaueee5b512015-04-03 23:46:31 +02001182 http_adjust_conn_mode(s, s->txn, &s->txn->req);
Willy Tarreau80a92c02014-03-12 10:41:13 +01001183
Willy Tarreaueee5b512015-04-03 23:46:31 +02001184 /* If an LB algorithm needs to access some pre-parsed body contents,
1185 * we must not start to forward anything until the connection is
1186 * confirmed otherwise we'll lose the pointer to these data and
1187 * prevent the hash from being doable again after a redispatch.
1188 */
1189 if (be->mode == PR_MODE_HTTP &&
1190 (be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_HI | BE_LB_HASH_PRM))
1191 s->txn->req.flags |= HTTP_MSGF_WAIT_CONN;
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001192
1193 /* we may request to parse a request body */
1194 if ((be->options & PR_O_WREQ_BODY) &&
1195 (s->txn->req.body_len || (s->txn->req.flags & HTTP_MSGF_TE_CHNK)))
1196 s->req.analysers |= AN_REQ_HTTP_BODY;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001197 }
1198
1199 s->flags |= SF_BE_ASSIGNED;
Willy Tarreau96e31212011-05-30 18:10:30 +02001200 if (be->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001201 s->req.flags |= CF_NEVER_WAIT;
1202 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02001203 }
1204
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001205 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001206}
1207
Willy Tarreaudc13c112013-06-21 23:16:39 +02001208static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001209 { CFG_LISTEN, "timeout", proxy_parse_timeout },
1210 { CFG_LISTEN, "clitimeout", proxy_parse_timeout },
1211 { CFG_LISTEN, "contimeout", proxy_parse_timeout },
1212 { CFG_LISTEN, "srvtimeout", proxy_parse_timeout },
Willy Tarreau3a7d2072009-03-05 23:48:25 +01001213 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreauc35362a2014-04-25 13:58:37 +02001214 { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue },
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +02001215 { CFG_LISTEN, "declare", proxy_parse_declare },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001216 { 0, NULL, NULL },
1217}};
1218
1219__attribute__((constructor))
1220static void __proxy_module_init(void)
1221{
1222 cfg_register_keywords(&cfg_kws);
1223}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001224
1225/*
1226 * Local variables:
1227 * c-indent-level: 8
1228 * c-basic-offset: 8
1229 * End:
1230 */