blob: 41e40e666dedd65cac53cc391d1def33a493146c [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 Tarreau960f2cb2016-11-24 12:02:29 +010032#include <types/cli.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010034#include <types/obj_type.h>
Emeric Brun5a8c0a92010-09-23 18:44:36 +020035#include <types/peers.h>
William Lallemanda6c5f332016-11-19 02:25:36 +010036#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
William Lallemanda6c5f332016-11-19 02:25:36 +010038#include <proto/applet.h>
39#include <proto/cli.h>
Alexandre Cassen87ea5482007-10-11 20:48:58 +020040#include <proto/backend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020042#include <proto/filters.h>
Willy Tarreau51aecc72009-07-12 09:47:04 +020043#include <proto/hdr_idx.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020044#include <proto/listener.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/log.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010046#include <proto/proto_tcp.h>
Willy Tarreau39e4f622010-05-31 17:01:36 +020047#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/proxy.h>
Willy Tarreaud0807c32010-08-27 18:26:11 +020049#include <proto/signal.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020050#include <proto/stream.h>
William Lallemanda6c5f332016-11-19 02:25:36 +010051#include <proto/stream_interface.h>
Emeric Brun5a8c0a92010-09-23 18:44:36 +020052#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
54
Willy Tarreau918ff602011-07-25 16:33:49 +020055int listeners; /* # of proxy listeners, set by cfgparse */
Willy Tarreaubaaee002006-06-26 02:48:02 +020056struct proxy *proxy = NULL; /* list of all existing proxies */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +020057struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */
Willy Tarreauf79d9502014-03-15 07:22:35 +010058struct eb_root proxy_by_name = EB_ROOT; /* tree of proxies sorted by name */
Willy Tarreau10479e42010-12-12 14:00:34 +010059unsigned int error_snapshot_id = 0; /* global ID assigned to each error then incremented */
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
Willy Tarreau977b8e42006-12-29 14:19:17 +010061/*
Willy Tarreau816eb542007-11-04 07:04:43 +010062 * This function returns a string containing a name describing capabilities to
63 * report comprehensible error messages. Specifically, it will return the words
Christopher Faulet898566e2016-10-26 11:06:28 +020064 * "frontend", "backend" when appropriate, or "proxy" for all other
Willy Tarreau816eb542007-11-04 07:04:43 +010065 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +010066 */
Willy Tarreau816eb542007-11-04 07:04:43 +010067const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +010068{
Willy Tarreau816eb542007-11-04 07:04:43 +010069 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
70 if (cap & PR_CAP_FE)
71 return "frontend";
72 else if (cap & PR_CAP_BE)
73 return "backend";
Willy Tarreau816eb542007-11-04 07:04:43 +010074 }
75 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +010076}
77
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010078/*
79 * This function returns a string containing the mode of the proxy in a format
80 * suitable for error messages.
81 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010082const char *proxy_mode_str(int mode) {
83
84 if (mode == PR_MODE_TCP)
85 return "tcp";
86 else if (mode == PR_MODE_HTTP)
87 return "http";
88 else if (mode == PR_MODE_HEALTH)
89 return "health";
90 else
91 return "unknown";
92}
93
Willy Tarreauf3950172009-10-10 18:35:51 +020094/*
95 * This function scans the list of backends and servers to retrieve the first
96 * backend and the first server with the given names, and sets them in both
97 * parameters. It returns zero if either is not found, or non-zero and sets
98 * the ones it did not found to NULL. If a NULL pointer is passed for the
99 * backend, only the pointer to the server will be updated.
100 */
101int get_backend_server(const char *bk_name, const char *sv_name,
102 struct proxy **bk, struct server **sv)
103{
104 struct proxy *p;
105 struct server *s;
Willy Tarreau7ecc4202014-03-15 07:57:11 +0100106 int sid;
Willy Tarreauf3950172009-10-10 18:35:51 +0200107
108 *sv = NULL;
109
Willy Tarreau050536d2012-10-04 08:47:34 +0200110 sid = -1;
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200111 if (*sv_name == '#')
112 sid = atoi(sv_name + 1);
113
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200114 p = proxy_be_by_name(bk_name);
Willy Tarreauf3950172009-10-10 18:35:51 +0200115 if (bk)
116 *bk = p;
117 if (!p)
118 return 0;
119
120 for (s = p->srv; s; s = s->next)
Willy Tarreau4055a102012-11-15 00:15:18 +0100121 if ((sid >= 0 && s->puid == sid) ||
122 (sid < 0 && strcmp(s->id, sv_name) == 0))
Willy Tarreauf3950172009-10-10 18:35:51 +0200123 break;
124 *sv = s;
125 if (!s)
126 return 0;
127 return 1;
128}
129
Willy Tarreaue219db72007-12-03 01:30:13 +0100130/* This function parses a "timeout" statement in a proxy section. It returns
131 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200132 * return zero, it will write an error or warning message into a preallocated
133 * buffer returned at <err>. The trailing is not be written. The function must
134 * be called with <args> pointing to the first command line word, with <proxy>
135 * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL.
136 * As a special case for compatibility with older configs, it also accepts
137 * "{cli|srv|con}timeout" in args[0].
Willy Tarreaue219db72007-12-03 01:30:13 +0100138 */
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200139static int proxy_parse_timeout(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200140 struct proxy *defpx, const char *file, int line,
141 char **err)
Willy Tarreaue219db72007-12-03 01:30:13 +0100142{
143 unsigned timeout;
144 int retval, cap;
145 const char *res, *name;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200146 int *tv = NULL;
147 int *td = NULL;
Willy Tarreaued446492014-04-28 22:56:38 +0200148 int warn = 0;
Willy Tarreaue219db72007-12-03 01:30:13 +0100149
150 retval = 0;
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200151
152 /* simply skip "timeout" but remain compatible with old form */
153 if (strcmp(args[0], "timeout") == 0)
154 args++;
155
Willy Tarreaue219db72007-12-03 01:30:13 +0100156 name = args[0];
Willy Tarreaued446492014-04-28 22:56:38 +0200157 if (!strcmp(args[0], "client") || (!strcmp(args[0], "clitimeout") && (warn = WARN_CLITO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100158 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100159 tv = &proxy->timeout.client;
160 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100161 cap = PR_CAP_FE;
162 } else if (!strcmp(args[0], "tarpit")) {
163 tv = &proxy->timeout.tarpit;
164 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100165 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100166 } else if (!strcmp(args[0], "http-keep-alive")) {
167 tv = &proxy->timeout.httpka;
168 td = &defpx->timeout.httpka;
169 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau036fae02008-01-06 13:24:40 +0100170 } else if (!strcmp(args[0], "http-request")) {
171 tv = &proxy->timeout.httpreq;
172 td = &defpx->timeout.httpreq;
Willy Tarreaucd7afc02009-07-12 10:03:17 +0200173 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaued446492014-04-28 22:56:38 +0200174 } else if (!strcmp(args[0], "server") || (!strcmp(args[0], "srvtimeout") && (warn = WARN_SRVTO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100175 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100176 tv = &proxy->timeout.server;
177 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100178 cap = PR_CAP_BE;
Willy Tarreaued446492014-04-28 22:56:38 +0200179 } else if (!strcmp(args[0], "connect") || (!strcmp(args[0], "contimeout") && (warn = WARN_CONTO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100180 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100181 tv = &proxy->timeout.connect;
182 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100183 cap = PR_CAP_BE;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100184 } else if (!strcmp(args[0], "check")) {
185 tv = &proxy->timeout.check;
186 td = &defpx->timeout.check;
187 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100188 } else if (!strcmp(args[0], "queue")) {
189 tv = &proxy->timeout.queue;
190 td = &defpx->timeout.queue;
191 cap = PR_CAP_BE;
Willy Tarreauce887fd2012-05-12 12:50:00 +0200192 } else if (!strcmp(args[0], "tunnel")) {
193 tv = &proxy->timeout.tunnel;
194 td = &defpx->timeout.tunnel;
195 cap = PR_CAP_BE;
Willy Tarreau05cdd962014-05-10 14:30:07 +0200196 } else if (!strcmp(args[0], "client-fin")) {
197 tv = &proxy->timeout.clientfin;
198 td = &defpx->timeout.clientfin;
199 cap = PR_CAP_FE;
200 } else if (!strcmp(args[0], "server-fin")) {
201 tv = &proxy->timeout.serverfin;
202 td = &defpx->timeout.serverfin;
203 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100204 } else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200205 memprintf(err,
206 "'timeout' supports 'client', 'server', 'connect', 'check', "
Willy Tarreau05cdd962014-05-10 14:30:07 +0200207 "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', "
208 "'client-fin' and 'server-fin' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200209 args[0]);
Willy Tarreaue219db72007-12-03 01:30:13 +0100210 return -1;
211 }
212
213 if (*args[1] == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200214 memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100215 return -1;
216 }
217
218 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
219 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200220 memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100221 return -1;
222 }
223
224 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200225 memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability",
226 name, proxy_type_str(proxy), proxy->id,
227 (cap & PR_CAP_BE) ? "backend" : "frontend");
Willy Tarreaue219db72007-12-03 01:30:13 +0100228 retval = 1;
229 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200230 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200231 memprintf(err, "overwriting 'timeout %s' which was already specified", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100232 retval = 1;
233 }
Willy Tarreaued446492014-04-28 22:56:38 +0200234 else if (warn) {
235 if (!already_warned(warn)) {
236 memprintf(err, "the '%s' directive is now deprecated in favor of 'timeout %s', and will not be supported in future versions.",
237 args[0], name);
238 retval = 1;
239 }
240 }
Willy Tarreaue219db72007-12-03 01:30:13 +0100241
Willy Tarreaufac5b592014-05-22 08:24:46 +0200242 if (*args[2] != 0) {
243 memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]);
244 retval = -1;
245 }
246
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200247 *tv = MS_TO_TICKS(timeout);
Willy Tarreaue219db72007-12-03 01:30:13 +0100248 return retval;
249}
250
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100251/* This function parses a "rate-limit" statement in a proxy section. It returns
252 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200253 * return zero, it will write an error or warning message into a preallocated
254 * buffer returned at <err>. The function must be called with <args> pointing
255 * to the first command line word, with <proxy> pointing to the proxy being
256 * parsed, and <defpx> to the default proxy or NULL.
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100257 */
258static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200259 struct proxy *defpx, const char *file, int line,
260 char **err)
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100261{
262 int retval, cap;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200263 char *res;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100264 unsigned int *tv = NULL;
265 unsigned int *td = NULL;
266 unsigned int val;
267
268 retval = 0;
269
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200270 if (strcmp(args[1], "sessions") == 0) {
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200271 tv = &proxy->fe_sps_lim;
272 td = &defpx->fe_sps_lim;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100273 cap = PR_CAP_FE;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200274 }
275 else {
276 memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100277 return -1;
278 }
279
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200280 if (*args[2] == 0) {
281 memprintf(err, "'%s %s' expects expects an integer value (in sessions/second)", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100282 return -1;
283 }
284
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200285 val = strtoul(args[2], &res, 0);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100286 if (*res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 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 +0100288 return -1;
289 }
290
291 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200292 memprintf(err, "%s %s will be ignored because %s '%s' has no %s capability",
293 args[0], args[1], proxy_type_str(proxy), proxy->id,
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100294 (cap & PR_CAP_BE) ? "backend" : "frontend");
295 retval = 1;
296 }
297 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200298 memprintf(err, "overwriting %s %s which was already specified", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100299 retval = 1;
300 }
301
302 *tv = val;
303 return retval;
304}
305
Willy Tarreauc35362a2014-04-25 13:58:37 +0200306/* This function parses a "max-keep-alive-queue" statement in a proxy section.
307 * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it
308 * does not return zero, it will write an error or warning message into a
309 * preallocated buffer returned at <err>. The function must be called with
310 * <args> pointing to the first command line word, with <proxy> pointing to
311 * the proxy being parsed, and <defpx> to the default proxy or NULL.
312 */
313static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy,
314 struct proxy *defpx, const char *file, int line,
315 char **err)
316{
317 int retval;
318 char *res;
319 unsigned int val;
320
321 retval = 0;
322
323 if (*args[1] == 0) {
324 memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]);
325 return -1;
326 }
327
328 val = strtol(args[1], &res, 0);
329 if (*res) {
330 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
331 return -1;
332 }
333
334 if (!(proxy->cap & PR_CAP_BE)) {
335 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
336 args[0], proxy_type_str(proxy), proxy->id);
337 retval = 1;
338 }
339
340 /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */
341 proxy->max_ka_queue = val + 1;
342 return retval;
343}
344
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200345/* This function parses a "declare" statement in a proxy section. It returns -1
346 * if there is any error, 1 for warning, otherwise 0. If it does not return zero,
347 * it will write an error or warning message into a preallocated buffer returned
348 * at <err>. The function must be called with <args> pointing to the first command
349 * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the
350 * default proxy or NULL.
351 */
352static int proxy_parse_declare(char **args, int section, struct proxy *curpx,
353 struct proxy *defpx, const char *file, int line,
354 char **err)
355{
356 /* Capture keyword wannot be declared in a default proxy. */
357 if (curpx == defpx) {
358 memprintf(err, "'%s' not avalaible in default section", args[0]);
359 return -1;
360 }
361
362 /* Capture keywork is only avalaible in frontend. */
363 if (!(curpx->cap & PR_CAP_FE)) {
364 memprintf(err, "'%s' only avalaible in frontend or listen section", args[0]);
365 return -1;
366 }
367
368 /* Check mandatory second keyword. */
369 if (!args[1] || !*args[1]) {
370 memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]);
371 return -1;
372 }
373
374 /* Actually, declare is only avalaible for declaring capture
375 * slot, but in the future it can declare maps or variables.
376 * So, this section permits to check and switch acording with
377 * the second keyword.
378 */
379 if (strcmp(args[1], "capture") == 0) {
380 char *error = NULL;
381 long len;
382 struct cap_hdr *hdr;
383
384 /* Check the next keyword. */
385 if (!args[2] || !*args[2] ||
386 (strcmp(args[2], "response") != 0 &&
387 strcmp(args[2], "request") != 0)) {
388 memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]);
389 return -1;
390 }
391
392 /* Check the 'len' keyword. */
393 if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) {
394 memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]);
395 return -1;
396 }
397
398 /* Check the length value. */
399 if (!args[4] || !*args[4]) {
400 memprintf(err, "'%s %s': 'len' requires a numeric value that represents the "
401 "capture length",
402 args[0], args[1]);
403 return -1;
404 }
405
406 /* convert the length value. */
407 len = strtol(args[4], &error, 10);
408 if (*error != '\0') {
409 memprintf(err, "'%s %s': cannot parse the length '%s'.",
410 args[0], args[1], args[3]);
411 return -1;
412 }
413
414 /* check length. */
415 if (len <= 0) {
416 memprintf(err, "length must be > 0");
417 return -1;
418 }
419
420 /* register the capture. */
Vincent Bernat02779b62016-04-03 13:48:43 +0200421 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200422 hdr->name = NULL; /* not a header capture */
423 hdr->namelen = 0;
424 hdr->len = len;
425 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
426
427 if (strcmp(args[2], "request") == 0) {
428 hdr->next = curpx->req_cap;
429 hdr->index = curpx->nb_req_cap++;
430 curpx->req_cap = hdr;
431 }
432 if (strcmp(args[2], "response") == 0) {
433 hdr->next = curpx->rsp_cap;
434 hdr->index = curpx->nb_rsp_cap++;
435 curpx->rsp_cap = hdr;
436 }
437 return 0;
438 }
439 else {
440 memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]);
441 return -1;
442 }
443}
444
Willy Tarreauf79d9502014-03-15 07:22:35 +0100445/* This function inserts proxy <px> into the tree of known proxies. The proxy's
446 * name is used as the storing key so it must already have been initialized.
447 */
448void proxy_store_name(struct proxy *px)
449{
450 px->conf.by_name.key = px->id;
451 ebis_insert(&proxy_by_name, &px->conf.by_name);
452}
453
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200454/* Returns a pointer to the first proxy matching capabilities <cap> and id
455 * <id>. NULL is returned if no match is found. If <table> is non-zero, it
456 * only considers proxies having a table.
Willy Tarreaubc216c42011-08-02 11:25:54 +0200457 */
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200458struct proxy *proxy_find_by_id(int id, int cap, int table)
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200459{
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200460 struct eb32_node *n;
Willy Tarreaubc216c42011-08-02 11:25:54 +0200461
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200462 for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) {
463 struct proxy *px = container_of(n, struct proxy, conf.id);
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100464
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200465 if (px->uuid != id)
466 break;
Alex Williams96532db2009-11-01 21:27:13 -0500467
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200468 if ((px->cap & cap) != cap)
469 continue;
Alex Williams96532db2009-11-01 21:27:13 -0500470
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200471 if (table && !px->table.size)
472 continue;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100473
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200474 return px;
475 }
476 return NULL;
477}
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100478
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200479/* Returns a pointer to the first proxy matching either name <name>, or id
480 * <name> if <name> begins with a '#'. NULL is returned if no match is found.
481 * If <table> is non-zero, it only considers proxies having a table.
482 */
483struct proxy *proxy_find_by_name(const char *name, int cap, int table)
484{
485 struct proxy *curproxy;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200486
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200487 if (*name == '#') {
488 curproxy = proxy_find_by_id(atoi(name + 1), cap, table);
489 if (curproxy)
Willy Tarreauc739aa82015-05-26 11:35:41 +0200490 return curproxy;
Alex Williams96532db2009-11-01 21:27:13 -0500491 }
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100492 else {
493 struct ebpt_node *node;
494
495 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
496 curproxy = container_of(node, struct proxy, conf.by_name);
Alex Williams96532db2009-11-01 21:27:13 -0500497
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100498 if (strcmp(curproxy->id, name) != 0)
499 break;
500
501 if ((curproxy->cap & cap) != cap)
502 continue;
503
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200504 if (table && !curproxy->table.size)
505 continue;
506
Willy Tarreauc739aa82015-05-26 11:35:41 +0200507 return curproxy;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100508 }
509 }
Willy Tarreauc739aa82015-05-26 11:35:41 +0200510 return NULL;
Alex Williams96532db2009-11-01 21:27:13 -0500511}
512
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200513/* Finds the best match for a proxy with capabilities <cap>, name <name> and id
514 * <id>. At most one of <id> or <name> may be different provided that <cap> is
515 * valid. Either <id> or <name> may be left unspecified (0). The purpose is to
516 * find a proxy based on some information from a previous configuration, across
517 * reloads or during information exchange between peers.
518 *
519 * Names are looked up first if present, then IDs are compared if present. In
520 * case of an inexact match whatever is forced in the configuration has
521 * precedence in the following order :
522 * - 1) forced ID (proves a renaming / change of proxy type)
523 * - 2) proxy name+type (may indicate a move if ID differs)
524 * - 3) automatic ID+type (may indicate a renaming)
525 *
526 * Depending on what is found, we can end up in the following situations :
527 *
528 * name id cap | possible causes
529 * -------------+-----------------
530 * -- -- -- | nothing found
531 * -- -- ok | nothing found
532 * -- ok -- | proxy deleted, ID points to next one
533 * -- ok ok | proxy renamed, or deleted with ID pointing to next one
534 * ok -- -- | proxy deleted, but other half with same name still here (before)
535 * ok -- ok | proxy's ID changed (proxy moved in the config file)
536 * ok ok -- | proxy deleted, but other half with same name still here (after)
537 * ok ok ok | perfect match
538 *
539 * Upon return if <diff> is not NULL, it is zeroed then filled with up to 3 bits :
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200540 * - PR_FBM_MISMATCH_ID : proxy was found but ID differs
541 * (and ID was not zero)
542 * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs
543 * (and name was not NULL)
544 * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with
545 * the same name and/or id
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200546 *
547 * Only a valid proxy is returned. If capabilities do not match, NULL is
548 * returned. The caller can check <diff> to report detailed warnings / errors,
549 * and decide whether or not to use what was found.
550 */
551struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff)
552{
553 struct proxy *byname;
554 struct proxy *byid;
555
556 if (!name && !id)
557 return NULL;
558
559 if (diff)
560 *diff = 0;
561
562 byname = byid = NULL;
563
564 if (name) {
565 byname = proxy_find_by_name(name, cap, 0);
566 if (byname && (!id || byname->uuid == id))
567 return byname;
568 }
569
570 /* remaining possiblities :
571 * - name not set
572 * - name set but not found
573 * - name found, but ID doesn't match.
574 */
575 if (id) {
576 byid = proxy_find_by_id(id, cap, 0);
577 if (byid) {
578 if (byname) {
579 /* id+type found, name+type found, but not all 3.
580 * ID wins only if forced, otherwise name wins.
581 */
582 if (byid->options & PR_O_FORCED_ID) {
583 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200584 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200585 return byid;
586 }
587 else {
588 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200589 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200590 return byname;
591 }
592 }
593
594 /* remaining possiblities :
595 * - name not set
596 * - name set but not found
597 */
598 if (name && diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200599 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200600 return byid;
601 }
602
603 /* ID not found */
604 if (byname) {
605 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200606 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200607 return byname;
608 }
609 }
610
611 /* All remaining possiblities will lead to NULL. If we can report more
612 * detailed information to the caller about changed types and/or name,
613 * we'll do it. For example, we could detect that "listen foo" was
614 * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced.
615 * - name not set, ID not found
616 * - name not found, ID not set
617 * - name not found, ID not found
618 */
619 if (!diff)
620 return NULL;
621
622 if (name) {
623 byname = proxy_find_by_name(name, 0, 0);
624 if (byname && (!id || byname->uuid == id))
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200625 *diff |= PR_FBM_MISMATCH_PROXYTYPE;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200626 }
627
628 if (id) {
629 byid = proxy_find_by_id(id, 0, 0);
630 if (byid) {
631 if (!name)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200632 *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200633 else if (byid->options & PR_O_FORCED_ID)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200634 *diff |= PR_FBM_MISMATCH_NAME | PR_FBM_MISMATCH_PROXYTYPE; /* name and type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200635 /* otherwise it's a different proxy that was returned */
636 }
637 }
638 return NULL;
639}
640
Willy Tarreaubaaee002006-06-26 02:48:02 +0200641/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100642 * This function finds a server with matching name within selected proxy.
643 * It also checks if there are more matching servers with
644 * requested name as this often leads into unexpected situations.
645 */
646
647struct server *findserver(const struct proxy *px, const char *name) {
648
649 struct server *cursrv, *target = NULL;
650
651 if (!px)
652 return NULL;
653
654 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
655 if (strcmp(cursrv->id, name))
656 continue;
657
658 if (!target) {
659 target = cursrv;
660 continue;
661 }
662
Cyril Bonté0bb519e2012-04-04 12:57:19 +0200663 Alert("Refusing to use duplicated server '%s' found in proxy: %s!\n",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100664 name, px->id);
665
666 return NULL;
667 }
668
669 return target;
670}
671
Willy Tarreauff01a212009-03-15 13:46:16 +0100672/* This function checks that the designated proxy has no http directives
673 * enabled. It will output a warning if there are, and will fix some of them.
674 * It returns the number of fatal errors encountered. This should be called
675 * at the end of the configuration parsing if the proxy is not in http mode.
676 * The <file> argument is used to construct the error message.
677 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200678int proxy_cfg_ensure_no_http(struct proxy *curproxy)
Willy Tarreauff01a212009-03-15 13:46:16 +0100679{
680 if (curproxy->cookie_name != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200681 Warning("config : cookie 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->rsp_exp != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200685 Warning("config : server 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->req_exp != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200689 Warning("config : client regular expressions 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 }
692 if (curproxy->monitor_uri != NULL) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200693 Warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n",
694 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100695 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200696 if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) {
Willy Tarreauff01a212009-03-15 13:46:16 +0100697 curproxy->lbprm.algo &= ~BE_LB_ALGO;
698 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200699 Warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n",
700 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100701 }
Willy Tarreau17804162009-11-09 21:27:51 +0100702 if (curproxy->to_log & (LW_REQ | LW_RESP)) {
703 curproxy->to_log &= ~(LW_REQ | LW_RESP);
Willy Tarreau59ad1a22014-01-29 14:39:58 +0100704 Warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200705 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
Willy Tarreau17804162009-11-09 21:27:51 +0100706 proxy_type_str(curproxy), curproxy->id);
707 }
Willy Tarreau62a61232013-04-12 18:13:46 +0200708 if (curproxy->conf.logformat_string == default_http_log_format ||
709 curproxy->conf.logformat_string == clf_http_log_format) {
710 /* Note: we don't change the directive's file:line number */
711 curproxy->conf.logformat_string = default_tcp_log_format;
712 Warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
713 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
Willy Tarreau196729e2012-05-31 19:30:26 +0200714 proxy_type_str(curproxy), curproxy->id);
715 }
716
Willy Tarreauff01a212009-03-15 13:46:16 +0100717 return 0;
718}
719
Willy Tarreau237250c2011-07-29 01:49:03 +0200720/* Perform the most basic initialization of a proxy :
721 * memset(), list_init(*), reset_timeouts(*).
Willy Tarreaub249e842011-09-07 18:41:08 +0200722 * Any new proxy or peer should be initialized via this function.
Willy Tarreau237250c2011-07-29 01:49:03 +0200723 */
724void init_new_proxy(struct proxy *p)
725{
726 memset(p, 0, sizeof(struct proxy));
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100727 p->obj_type = OBJ_TYPE_PROXY;
Willy Tarreau237250c2011-07-29 01:49:03 +0200728 LIST_INIT(&p->pendconns);
729 LIST_INIT(&p->acl);
730 LIST_INIT(&p->http_req_rules);
Willy Tarreaue365c0b2013-06-11 16:06:12 +0200731 LIST_INIT(&p->http_res_rules);
Willy Tarreau353bc9f2014-04-28 22:05:31 +0200732 LIST_INIT(&p->block_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200733 LIST_INIT(&p->redirect_rules);
734 LIST_INIT(&p->mon_fail_cond);
735 LIST_INIT(&p->switching_rules);
Willy Tarreau4a5cade2012-04-05 21:09:48 +0200736 LIST_INIT(&p->server_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200737 LIST_INIT(&p->persist_rules);
738 LIST_INIT(&p->sticking_rules);
739 LIST_INIT(&p->storersp_rules);
740 LIST_INIT(&p->tcp_req.inspect_rules);
741 LIST_INIT(&p->tcp_rep.inspect_rules);
742 LIST_INIT(&p->tcp_req.l4_rules);
Willy Tarreau620408f2016-10-21 16:37:51 +0200743 LIST_INIT(&p->tcp_req.l5_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200744 LIST_INIT(&p->req_add);
745 LIST_INIT(&p->rsp_add);
746 LIST_INIT(&p->listener_queue);
William Lallemand0f99e342011-10-12 17:50:54 +0200747 LIST_INIT(&p->logsrvs);
William Lallemand723b73a2012-02-08 16:37:49 +0100748 LIST_INIT(&p->logformat);
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200749 LIST_INIT(&p->logformat_sd);
William Lallemanda73203e2012-03-12 12:48:57 +0100750 LIST_INIT(&p->format_unique_id);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200751 LIST_INIT(&p->conf.bind);
Willy Tarreau4348fad2012-09-20 16:48:07 +0200752 LIST_INIT(&p->conf.listeners);
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200753 LIST_INIT(&p->conf.args.list);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200754 LIST_INIT(&p->tcpcheck_rules);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100755 LIST_INIT(&p->filter_configs);
Willy Tarreau237250c2011-07-29 01:49:03 +0200756
757 /* Timeouts are defined as -1 */
758 proxy_reset_timeouts(p);
759 p->tcp_rep.inspect_delay = TICK_ETERNITY;
Willy Tarreau050536d2012-10-04 08:47:34 +0200760
761 /* initial uuid is unassigned (-1) */
762 p->uuid = -1;
Willy Tarreau237250c2011-07-29 01:49:03 +0200763}
764
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100765/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200766 * This function creates all proxy sockets. It should be done very early,
767 * typically before privileges are dropped. The sockets will be registered
768 * but not added to any fd_set, in order not to loose them across the fork().
Willy Tarreau562515c2011-07-25 08:11:52 +0200769 * The proxies also start in READY state because they all have their listeners
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200770 * bound.
Willy Tarreau2ff76222007-04-09 19:29:56 +0200771 *
772 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
773 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774 */
775int start_proxies(int verbose)
776{
777 struct proxy *curproxy;
778 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100779 int lerr, err = ERR_NONE;
780 int pxerr;
781 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782
783 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
784 if (curproxy->state != PR_STNEW)
785 continue; /* already initialized */
786
787 pxerr = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200788 list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100789 if (listener->state != LI_ASSIGNED)
790 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791
Emeric Bruncf20bf12010-10-22 16:06:11 +0200792 lerr = listener->proto->bind(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200793
Willy Tarreaue6b98942007-10-29 01:09:36 +0100794 /* errors are reported if <verbose> is set or if they are fatal */
795 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
796 if (lerr & ERR_ALERT)
797 Alert("Starting %s %s: %s\n",
798 proxy_type_str(curproxy), curproxy->id, msg);
799 else if (lerr & ERR_WARN)
800 Warning("Starting %s %s: %s\n",
801 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802 }
803
Willy Tarreaue6b98942007-10-29 01:09:36 +0100804 err |= lerr;
805 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200806 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100807 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100809 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200810 pxerr |= 1;
811 continue;
812 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813 }
814
815 if (!pxerr) {
Willy Tarreau562515c2011-07-25 08:11:52 +0200816 curproxy->state = PR_STREADY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
818 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100819
820 if (err & ERR_ABORT)
821 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822 }
823
824 return err;
825}
826
827
828/*
Willy Tarreau918ff602011-07-25 16:33:49 +0200829 * This is the proxy management task. It enables proxies when there are enough
Willy Tarreau87b09662015-04-03 00:22:06 +0200830 * free streams, or stops them when the table is full. It is designed to be
Willy Tarreau918ff602011-07-25 16:33:49 +0200831 * called as a task which is woken up upon stopping or when rate limiting must
832 * be enforced.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833 */
Willy Tarreau918ff602011-07-25 16:33:49 +0200834struct task *manage_proxy(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835{
Willy Tarreau918ff602011-07-25 16:33:49 +0200836 struct proxy *p = t->context;
837 int next = TICK_ETERNITY;
Willy Tarreau79584222009-03-06 09:18:27 +0100838 unsigned int wait;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839
Willy Tarreau918ff602011-07-25 16:33:49 +0200840 /* We should periodically try to enable listeners waiting for a
841 * global resource here.
842 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843
Willy Tarreau918ff602011-07-25 16:33:49 +0200844 /* first, let's check if we need to stop the proxy */
845 if (unlikely(stopping && p->state != PR_STSTOPPED)) {
846 int t;
847 t = tick_remain(now_ms, p->stop_time);
848 if (t == 0) {
849 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
850 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
851 send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
852 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
853 stop_proxy(p);
854 /* try to free more memory */
855 pool_gc2();
856 }
857 else {
858 next = tick_first(next, p->stop_time);
859 }
860 }
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200861
Willy Tarreau3a925c12013-09-04 17:54:01 +0200862 /* If the proxy holds a stick table, we need to purge all unused
863 * entries. These are all the ones in the table with ref_cnt == 0
864 * and all the ones in the pool used to allocate new entries. Any
Willy Tarreau87b09662015-04-03 00:22:06 +0200865 * entry attached to an existing stream waiting for a store will
Willy Tarreau3a925c12013-09-04 17:54:01 +0200866 * be in neither list. Any entry being dumped will have ref_cnt > 0.
867 * However we protect tables that are being synced to peers.
868 */
869 if (unlikely(stopping && p->state == PR_STSTOPPED && p->table.current)) {
870 if (!p->table.syncing) {
871 stktable_trash_oldest(&p->table, p->table.current);
872 pool_gc2();
873 }
874 if (p->table.current) {
875 /* some entries still remain, let's recheck in one second */
876 next = tick_first(next, tick_add(now_ms, 1000));
877 }
878 }
879
Willy Tarreau918ff602011-07-25 16:33:49 +0200880 /* the rest below is just for frontends */
881 if (!(p->cap & PR_CAP_FE))
882 goto out;
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200883
Willy Tarreau918ff602011-07-25 16:33:49 +0200884 /* check the various reasons we may find to block the frontend */
885 if (unlikely(p->feconn >= p->maxconn)) {
886 if (p->state == PR_STREADY)
887 p->state = PR_STFULL;
888 goto out;
889 }
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100890
Willy Tarreau918ff602011-07-25 16:33:49 +0200891 /* OK we have no reason to block, so let's unblock if we were blocking */
892 if (p->state == PR_STFULL)
893 p->state = PR_STREADY;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100894
Willy Tarreau918ff602011-07-25 16:33:49 +0200895 if (p->fe_sps_lim &&
896 (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) {
897 /* we're blocking because a limit was reached on the number of
898 * requests/s on the frontend. We want to re-check ASAP, which
899 * means in 1 ms before estimated expiration date, because the
900 * timer will have settled down.
901 */
902 next = tick_first(next, tick_add(now_ms, wait));
903 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200904 }
Willy Tarreau918ff602011-07-25 16:33:49 +0200905
906 /* The proxy is not limited so we can re-enable any waiting listener */
907 if (!LIST_ISEMPTY(&p->listener_queue))
908 dequeue_all_listeners(&p->listener_queue);
909 out:
910 t->expire = next;
911 task_queue(t);
912 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200913}
914
915
916/*
917 * this function disables health-check servers so that the process will quickly be ignored
918 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
919 * time will not be used since it would already not listen anymore to the socket.
920 */
921void soft_stop(void)
922{
923 struct proxy *p;
Willy Tarreaubbe11b12011-07-25 11:16:24 +0200924 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925
926 stopping = 1;
927 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200928 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929 while (p) {
930 if (p->state != PR_STSTOPPED) {
Willy Tarreauf8fbcef2008-10-10 17:51:34 +0200931 Warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace);
932 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 +0200933 p->stop_time = tick_add(now_ms, p->grace);
Emeric Brun5a8c0a92010-09-23 18:44:36 +0200934
Willy Tarreau20b7afb2015-09-28 16:35:04 +0200935 /* Note: do not wake up stopped proxies' task nor their tables'
936 * tasks as these ones might point to already released entries.
937 */
938 if (p->table.size && p->table.sync_task)
939 task_wakeup(p->table.sync_task, TASK_WOKEN_MSG);
940
941 if (p->task)
942 task_wakeup(p->task, TASK_WOKEN_MSG);
943 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200944 p = p->next;
945 }
Willy Tarreaubbe11b12011-07-25 11:16:24 +0200946
947 prs = peers;
948 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +0200949 if (prs->peers_fe)
950 stop_proxy(prs->peers_fe);
Willy Tarreaubbe11b12011-07-25 11:16:24 +0200951 prs = prs->next;
952 }
Willy Tarreaud0807c32010-08-27 18:26:11 +0200953 /* signal zero is used to broadcast the "stopping" event */
954 signal_handler(0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955}
956
957
Willy Tarreaube58c382011-07-24 18:28:10 +0200958/* Temporarily disables listening on all of the proxy's listeners. Upon
959 * success, the proxy enters the PR_PAUSED state. If disabling at least one
960 * listener returns an error, then the proxy state is set to PR_STERROR
Willy Tarreauce8fe252011-09-07 19:14:57 +0200961 * because we don't know how to resume from this. The function returns 0
962 * if it fails, or non-zero on success.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200963 */
Willy Tarreauce8fe252011-09-07 19:14:57 +0200964int pause_proxy(struct proxy *p)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200965{
966 struct listener *l;
Willy Tarreauce8fe252011-09-07 19:14:57 +0200967
968 if (!(p->cap & PR_CAP_FE) || p->state == PR_STERROR ||
969 p->state == PR_STSTOPPED || p->state == PR_STPAUSED)
970 return 1;
971
972 Warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
973 send_log(p, LOG_WARNING, "Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
974
Willy Tarreau4348fad2012-09-20 16:48:07 +0200975 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreaube58c382011-07-24 18:28:10 +0200976 if (!pause_listener(l))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200977 p->state = PR_STERROR;
978 }
Willy Tarreauce8fe252011-09-07 19:14:57 +0200979
980 if (p->state == PR_STERROR) {
981 Warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
982 send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
983 return 0;
984 }
985
986 p->state = PR_STPAUSED;
987 return 1;
Willy Tarreauda250db2008-10-12 12:07:48 +0200988}
989
990
991/*
992 * This function completely stops a proxy and releases its listeners. It has
993 * to be called when going down in order to release the ports so that another
994 * process may bind to them. It must also be called on disabled proxies at the
995 * end of start-up. When all listeners are closed, the proxy is set to the
996 * PR_STSTOPPED state.
997 */
998void stop_proxy(struct proxy *p)
999{
1000 struct listener *l;
1001
Willy Tarreau4348fad2012-09-20 16:48:07 +02001002 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauda250db2008-10-12 12:07:48 +02001003 unbind_listener(l);
1004 if (l->state >= LI_ASSIGNED) {
1005 delete_listener(l);
1006 listeners--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02001007 jobs--;
Willy Tarreauda250db2008-10-12 12:07:48 +02001008 }
1009 }
1010 p->state = PR_STSTOPPED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011}
1012
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001013/* This function resumes listening on the specified proxy. It scans all of its
1014 * listeners and tries to enable them all. If any of them fails, the proxy is
1015 * put back to the paused state. It returns 1 upon success, or zero if an error
1016 * is encountered.
1017 */
1018int resume_proxy(struct proxy *p)
1019{
1020 struct listener *l;
1021 int fail;
1022
1023 if (p->state != PR_STPAUSED)
1024 return 1;
1025
1026 Warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
1027 send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
1028
1029 fail = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001030 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001031 if (!resume_listener(l)) {
1032 int port;
1033
1034 port = get_host_port(&l->addr);
1035 if (port) {
1036 Warning("Port %d busy while trying to enable %s %s.\n",
1037 port, proxy_cap_str(p->cap), p->id);
1038 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
1039 port, proxy_cap_str(p->cap), p->id);
1040 }
1041 else {
1042 Warning("Bind on socket %d busy while trying to enable %s %s.\n",
1043 l->luid, proxy_cap_str(p->cap), p->id);
1044 send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n",
1045 l->luid, proxy_cap_str(p->cap), p->id);
1046 }
1047
1048 /* Another port might have been enabled. Let's stop everything. */
1049 fail = 1;
1050 break;
1051 }
1052 }
1053
1054 p->state = PR_STREADY;
1055 if (fail) {
1056 pause_proxy(p);
1057 return 0;
1058 }
1059 return 1;
1060}
1061
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062/*
1063 * This function temporarily disables listening so that another new instance
1064 * can start listening. It is designed to be called upon reception of a
1065 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
1066 * the proxy, or a SIGTTIN can be sent to listen again.
1067 */
1068void pause_proxies(void)
1069{
1070 int err;
1071 struct proxy *p;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001072 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001073
1074 err = 0;
1075 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001076 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001077 while (p) {
Willy Tarreauce8fe252011-09-07 19:14:57 +02001078 err |= !pause_proxy(p);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001079 p = p->next;
1080 }
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001081
1082 prs = peers;
1083 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001084 if (prs->peers_fe)
1085 err |= !pause_proxy(prs->peers_fe);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001086 prs = prs->next;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001087 }
1088
Willy Tarreaubaaee002006-06-26 02:48:02 +02001089 if (err) {
1090 Warning("Some proxies refused to pause, performing soft stop now.\n");
1091 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
1092 soft_stop();
1093 }
1094}
1095
1096
1097/*
1098 * This function reactivates listening. This can be used after a call to
1099 * sig_pause(), for example when a new instance has failed starting up.
1100 * It is designed to be called upon reception of a SIGTTIN.
1101 */
Willy Tarreaube58c382011-07-24 18:28:10 +02001102void resume_proxies(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001103{
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001104 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001105 struct proxy *p;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001106 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001107
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001108 err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001109 p = proxy;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001110 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001111 while (p) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001112 err |= !resume_proxy(p);
1113 p = p->next;
1114 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001115
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001116 prs = peers;
1117 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001118 if (prs->peers_fe)
1119 err |= !resume_proxy(prs->peers_fe);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001120 prs = prs->next;
1121 }
Willy Tarreaube58c382011-07-24 18:28:10 +02001122
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001123 if (err) {
1124 Warning("Some proxies refused to resume, a restart is probably needed to resume safe operations.\n");
1125 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 +02001126 }
1127}
1128
Willy Tarreau87b09662015-04-03 00:22:06 +02001129/* Set current stream's backend to <be>. Nothing is done if the
1130 * stream already had a backend assigned, which is indicated by
Willy Tarreaue7dff022015-04-03 01:14:29 +02001131 * s->flags & SF_BE_ASSIGNED.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001132 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001133 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001134 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001135int stream_set_backend(struct stream *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001136{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001137 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001138 return 1;
Christopher Faulet41179042016-06-21 11:54:52 +02001139
1140 if (flt_set_stream_backend(s, be) < 0)
1141 return 0;
1142
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001143 s->be = be;
1144 be->beconn++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001145 if (be->beconn > be->be_counters.conn_max)
1146 be->be_counters.conn_max = be->beconn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001147 proxy_inc_be_ctr(be);
1148
Willy Tarreau87b09662015-04-03 00:22:06 +02001149 /* assign new parameters to the stream from the new backend */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001150 s->si[1].flags &= ~SI_FL_INDEP_STR;
1151 if (be->options2 & PR_O2_INDEPSTR)
1152 s->si[1].flags |= SI_FL_INDEP_STR;
1153
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001154 /* We want to enable the backend-specific analysers except those which
1155 * were already run as part of the frontend/listener. Note that it would
1156 * be more reliable to store the list of analysers that have been run,
1157 * but what we do here is OK for now.
1158 */
Christopher Faulet70e2f272017-01-09 16:33:19 +01001159 s->req.analysers |= be->be_req_ana & ~(strm_li(s) ? strm_li(s)->analysers : 0);
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001160
Willy Tarreau51aecc72009-07-12 09:47:04 +02001161 /* If the target backend requires HTTP processing, we have to allocate
Willy Tarreaueee5b512015-04-03 23:46:31 +02001162 * the HTTP transaction and hdr_idx if we did not have one.
Willy Tarreau51aecc72009-07-12 09:47:04 +02001163 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001164 if (unlikely(!s->txn && be->http_needed)) {
1165 if (unlikely(!http_alloc_txn(s)))
Willy Tarreau51aecc72009-07-12 09:47:04 +02001166 return 0; /* not enough memory */
Willy Tarreau39e4f622010-05-31 17:01:36 +02001167
1168 /* and now initialize the HTTP transaction state */
1169 http_init_txn(s);
Willy Tarreau51aecc72009-07-12 09:47:04 +02001170 }
1171
Christopher Faulet309c6412015-12-02 09:57:32 +01001172 /* Be sure to filter request headers if the backend is an HTTP proxy and
1173 * if there are filters attached to the stream. */
1174 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01001175 s->req.analysers |= AN_REQ_FLT_HTTP_HDRS;
Christopher Faulet309c6412015-12-02 09:57:32 +01001176
Willy Tarreaueee5b512015-04-03 23:46:31 +02001177 if (s->txn) {
1178 if (be->options2 & PR_O2_RSPBUG_OK)
1179 s->txn->rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001180
Willy Tarreaueee5b512015-04-03 23:46:31 +02001181 /* If we chain to an HTTP backend running a different HTTP mode, we
1182 * have to re-adjust the desired keep-alive/close mode to accommodate
1183 * both the frontend's and the backend's modes.
1184 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001185 if (strm_fe(s)->mode == PR_MODE_HTTP && be->mode == PR_MODE_HTTP &&
1186 ((strm_fe(s)->options & PR_O_HTTP_MODE) != (be->options & PR_O_HTTP_MODE)))
Willy Tarreaueee5b512015-04-03 23:46:31 +02001187 http_adjust_conn_mode(s, s->txn, &s->txn->req);
Willy Tarreau80a92c02014-03-12 10:41:13 +01001188
Willy Tarreaueee5b512015-04-03 23:46:31 +02001189 /* If an LB algorithm needs to access some pre-parsed body contents,
1190 * we must not start to forward anything until the connection is
1191 * confirmed otherwise we'll lose the pointer to these data and
1192 * prevent the hash from being doable again after a redispatch.
1193 */
1194 if (be->mode == PR_MODE_HTTP &&
1195 (be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_HI | BE_LB_HASH_PRM))
1196 s->txn->req.flags |= HTTP_MSGF_WAIT_CONN;
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001197
1198 /* we may request to parse a request body */
1199 if ((be->options & PR_O_WREQ_BODY) &&
1200 (s->txn->req.body_len || (s->txn->req.flags & HTTP_MSGF_TE_CHNK)))
1201 s->req.analysers |= AN_REQ_HTTP_BODY;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001202 }
1203
1204 s->flags |= SF_BE_ASSIGNED;
Willy Tarreau96e31212011-05-30 18:10:30 +02001205 if (be->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001206 s->req.flags |= CF_NEVER_WAIT;
1207 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02001208 }
1209
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001210 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001211}
1212
Willy Tarreaudc13c112013-06-21 23:16:39 +02001213static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001214 { CFG_LISTEN, "timeout", proxy_parse_timeout },
1215 { CFG_LISTEN, "clitimeout", proxy_parse_timeout },
1216 { CFG_LISTEN, "contimeout", proxy_parse_timeout },
1217 { CFG_LISTEN, "srvtimeout", proxy_parse_timeout },
Willy Tarreau3a7d2072009-03-05 23:48:25 +01001218 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreauc35362a2014-04-25 13:58:37 +02001219 { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue },
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +02001220 { CFG_LISTEN, "declare", proxy_parse_declare },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001221 { 0, NULL, NULL },
1222}};
1223
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001224/* Expects to find a frontend named <arg> and returns it, otherwise displays various
1225 * adequate error messages and returns NULL. This function is designed to be used by
1226 * functions requiring a frontend on the CLI.
1227 */
1228struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg)
1229{
1230 struct proxy *px;
1231
1232 if (!*arg) {
1233 appctx->ctx.cli.msg = "A frontend name is expected.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001234 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001235 return NULL;
1236 }
1237
1238 px = proxy_fe_by_name(arg);
1239 if (!px) {
1240 appctx->ctx.cli.msg = "No such frontend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001241 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001242 return NULL;
1243 }
1244 return px;
1245}
1246
William Lallemanda6c5f332016-11-19 02:25:36 +01001247/* parse a "show servers" CLI line, returns 0 if it wants to start the dump or
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001248 * 1 if it stops immediately. If an argument is specified, it will set the proxy
1249 * pointer into cli.p0 and its ID into cli.i0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001250 */
1251static int cli_parse_show_servers(char **args, struct appctx *appctx, void *private)
1252{
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001253 struct proxy *px;
William Lallemanda6c5f332016-11-19 02:25:36 +01001254
1255 /* check if a backend name has been provided */
1256 if (*args[3]) {
1257 /* read server state from local file */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001258 px = proxy_be_by_name(args[3]);
William Lallemanda6c5f332016-11-19 02:25:36 +01001259
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001260 if (!px) {
William Lallemanda6c5f332016-11-19 02:25:36 +01001261 appctx->ctx.cli.msg = "Can't find backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001262 appctx->st0 = CLI_ST_PRINT;
William Lallemanda6c5f332016-11-19 02:25:36 +01001263 return 1;
1264 }
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001265 appctx->ctx.cli.p0 = px;
1266 appctx->ctx.cli.i0 = px->uuid;
William Lallemanda6c5f332016-11-19 02:25:36 +01001267 }
1268 return 0;
1269}
1270
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001271/* dumps server state information into <buf> for all the servers found in backend cli.p0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001272 * These information are all the parameters which may change during HAProxy runtime.
1273 * By default, we only export to the last known server state file format.
1274 * These information can be used at next startup to recover same level of server state.
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001275 * It uses the proxy pointer from cli.p0, the proxy's id from cli.i0 and the server's
1276 * pointer from cli.p1.
William Lallemanda6c5f332016-11-19 02:25:36 +01001277 */
1278static int dump_servers_state(struct stream_interface *si, struct chunk *buf)
1279{
1280 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001281 struct proxy *px = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001282 struct server *srv;
1283 char srv_addr[INET6_ADDRSTRLEN + 1];
1284 time_t srv_time_since_last_change;
1285 int bk_f_forced_id, srv_f_forced_id;
1286
William Lallemanda6c5f332016-11-19 02:25:36 +01001287 /* we don't want to report any state if the backend is not enabled on this process */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001288 if (px->bind_proc && !(px->bind_proc & (1UL << (relative_pid - 1))))
William Lallemanda6c5f332016-11-19 02:25:36 +01001289 return 1;
1290
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001291 if (!appctx->ctx.cli.p1)
1292 appctx->ctx.cli.p1 = px->srv;
William Lallemanda6c5f332016-11-19 02:25:36 +01001293
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001294 for (; appctx->ctx.cli.p1 != NULL; appctx->ctx.cli.p1 = srv->next) {
1295 srv = appctx->ctx.cli.p1;
William Lallemanda6c5f332016-11-19 02:25:36 +01001296 srv_addr[0] = '\0';
1297
1298 switch (srv->addr.ss_family) {
1299 case AF_INET:
1300 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
1301 srv_addr, INET_ADDRSTRLEN + 1);
1302 break;
1303 case AF_INET6:
1304 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
1305 srv_addr, INET6_ADDRSTRLEN + 1);
1306 break;
1307 }
1308 srv_time_since_last_change = now.tv_sec - srv->last_change;
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001309 bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001310 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
1311
1312 chunk_appendf(buf,
1313 "%d %s "
1314 "%d %s %s "
1315 "%d %d %d %d %ld "
1316 "%d %d %d %d %d "
1317 "%d %d"
1318 "\n",
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001319 px->uuid, px->id,
William Lallemanda6c5f332016-11-19 02:25:36 +01001320 srv->puid, srv->id, srv_addr,
1321 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
1322 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
1323 bk_f_forced_id, srv_f_forced_id);
1324 if (bi_putchk(si_ic(si), &trash) == -1) {
1325 si_applet_cant_put(si);
1326 return 0;
1327 }
1328 }
1329 return 1;
1330}
1331
1332/* Parses backend list or simply use backend name provided by the user to return
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001333 * states of servers to stdout. It dumps proxy <cli.p0> and stops if <cli.i0> is
1334 * non-null.
William Lallemanda6c5f332016-11-19 02:25:36 +01001335 */
1336static int cli_io_handler_servers_state(struct appctx *appctx)
1337{
1338 struct stream_interface *si = appctx->owner;
1339 extern struct proxy *proxy;
1340 struct proxy *curproxy;
1341
1342 chunk_reset(&trash);
1343
1344 if (appctx->st2 == STAT_ST_INIT) {
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001345 if (!appctx->ctx.cli.p0)
1346 appctx->ctx.cli.p0 = proxy;
William Lallemanda6c5f332016-11-19 02:25:36 +01001347 appctx->st2 = STAT_ST_HEAD;
1348 }
1349
1350 if (appctx->st2 == STAT_ST_HEAD) {
1351 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
1352 if (bi_putchk(si_ic(si), &trash) == -1) {
1353 si_applet_cant_put(si);
1354 return 0;
1355 }
1356 appctx->st2 = STAT_ST_INFO;
1357 }
1358
1359 /* STAT_ST_INFO */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001360 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1361 curproxy = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001362 /* servers are only in backends */
1363 if (curproxy->cap & PR_CAP_BE) {
1364 if (!dump_servers_state(si, &trash))
1365 return 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001366 }
1367 /* only the selected proxy is dumped */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001368 if (appctx->ctx.cli.i0)
William Lallemanda6c5f332016-11-19 02:25:36 +01001369 break;
1370 }
1371
1372 return 1;
1373}
1374
Willy Tarreau608ea592016-12-16 18:01:15 +01001375/* Parses backend list and simply report backend names. It keeps the proxy
1376 * pointer in cli.p0.
1377 */
William Lallemand933efcd2016-11-22 12:34:16 +01001378static int cli_io_handler_show_backend(struct appctx *appctx)
1379{
1380 extern struct proxy *proxy;
1381 struct stream_interface *si = appctx->owner;
1382 struct proxy *curproxy;
1383
1384 chunk_reset(&trash);
1385
Willy Tarreau608ea592016-12-16 18:01:15 +01001386 if (!appctx->ctx.cli.p0) {
William Lallemand933efcd2016-11-22 12:34:16 +01001387 chunk_printf(&trash, "# name\n");
1388 if (bi_putchk(si_ic(si), &trash) == -1) {
1389 si_applet_cant_put(si);
1390 return 0;
1391 }
Willy Tarreau608ea592016-12-16 18:01:15 +01001392 appctx->ctx.cli.p0 = proxy;
William Lallemand933efcd2016-11-22 12:34:16 +01001393 }
1394
Willy Tarreau608ea592016-12-16 18:01:15 +01001395 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1396 curproxy = appctx->ctx.cli.p0;
William Lallemand933efcd2016-11-22 12:34:16 +01001397
1398 /* looking for backends only */
1399 if (!(curproxy->cap & PR_CAP_BE))
1400 continue;
1401
1402 /* we don't want to list a backend which is bound to this process */
1403 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
1404 continue;
1405
1406 chunk_appendf(&trash, "%s\n", curproxy->id);
1407 if (bi_putchk(si_ic(si), &trash) == -1) {
1408 si_applet_cant_put(si);
1409 return 0;
1410 }
1411 }
1412
1413 return 1;
1414}
William Lallemanda6c5f332016-11-19 02:25:36 +01001415
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001416/* Parses the "set maxconn frontend" directive, it always returns 1 */
1417static int cli_parse_set_maxconn_frontend(char **args, struct appctx *appctx, void *private)
1418{
1419 struct proxy *px;
1420 struct listener *l;
1421 int v;
1422
1423 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1424 return 1;
1425
1426 px = cli_find_frontend(appctx, args[3]);
1427 if (!px)
1428 return 1;
1429
1430 if (!*args[4]) {
1431 appctx->ctx.cli.msg = "Integer value expected.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001432 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001433 return 1;
1434 }
1435
1436 v = atoi(args[4]);
1437 if (v < 0) {
1438 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001439 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001440 return 1;
1441 }
1442
1443 /* OK, the value is fine, so we assign it to the proxy and to all of
1444 * its listeners. The blocked ones will be dequeued.
1445 */
1446 px->maxconn = v;
1447 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1448 l->maxconn = v;
1449 if (l->state == LI_FULL)
1450 resume_listener(l);
1451 }
1452
1453 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&px->listener_queue))
1454 dequeue_all_listeners(&px->listener_queue);
1455
1456 return 1;
1457}
1458
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001459/* Parses the "shutdown frontend" directive, it always returns 1 */
1460static int cli_parse_shutdown_frontend(char **args, struct appctx *appctx, void *private)
1461{
1462 struct proxy *px;
1463
1464 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1465 return 1;
1466
1467 px = cli_find_frontend(appctx, args[2]);
1468 if (!px)
1469 return 1;
1470
1471 if (px->state == PR_STSTOPPED) {
1472 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001473 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001474 return 1;
1475 }
1476
1477 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1478 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1479 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1480 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1481 stop_proxy(px);
1482 return 1;
1483}
1484
Willy Tarreau15b9e682016-11-24 11:55:28 +01001485/* Parses the "disable frontend" directive, it always returns 1 */
1486static int cli_parse_disable_frontend(char **args, struct appctx *appctx, void *private)
1487{
1488 struct proxy *px;
1489
1490 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1491 return 1;
1492
1493 px = cli_find_frontend(appctx, args[2]);
1494 if (!px)
1495 return 1;
1496
1497 if (px->state == PR_STSTOPPED) {
1498 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001499 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001500 return 1;
1501 }
1502
1503 if (px->state == PR_STPAUSED) {
1504 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001505 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001506 return 1;
1507 }
1508
1509 if (!pause_proxy(px)) {
1510 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001511 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001512 return 1;
1513 }
1514 return 1;
1515}
1516
1517/* Parses the "enable frontend" directive, it always returns 1 */
1518static int cli_parse_enable_frontend(char **args, struct appctx *appctx, void *private)
1519{
1520 struct proxy *px;
1521
1522 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1523 return 1;
1524
1525 px = cli_find_frontend(appctx, args[2]);
1526 if (!px)
1527 return 1;
1528
1529 if (px->state == PR_STSTOPPED) {
1530 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001531 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001532 return 1;
1533 }
1534
1535 if (px->state != PR_STPAUSED) {
1536 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001537 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001538 return 1;
1539 }
1540
1541 if (!resume_proxy(px)) {
1542 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001543 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001544 return 1;
1545 }
1546 return 1;
1547}
1548
William Lallemanda6c5f332016-11-19 02:25:36 +01001549/* register cli keywords */
1550static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau15b9e682016-11-24 11:55:28 +01001551 { { "disable", "frontend", NULL }, "disable frontend : temporarily disable specific frontend", cli_parse_disable_frontend, NULL, NULL },
1552 { { "enable", "frontend", NULL }, "enable frontend : re-enable specific frontend", cli_parse_enable_frontend, NULL, NULL },
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001553 { { "set", "maxconn", "frontend", NULL }, "set maxconn frontend : change a frontend's maxconn setting", cli_parse_set_maxconn_frontend, NULL },
William Lallemanda6c5f332016-11-19 02:25:36 +01001554 { { "show","servers", "state", NULL }, "show servers state [id]: dump volatile server information (for backend <id>)", cli_parse_show_servers, cli_io_handler_servers_state },
Willy Tarreau608ea592016-12-16 18:01:15 +01001555 { { "show", "backend", NULL }, "show backend : list backends in the current running config", NULL, cli_io_handler_show_backend },
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001556 { { "shutdown", "frontend", NULL }, "shutdown frontend : stop a specific frontend", cli_parse_shutdown_frontend, NULL, NULL },
William Lallemanda6c5f332016-11-19 02:25:36 +01001557 {{},}
1558}};
1559
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001560__attribute__((constructor))
1561static void __proxy_module_init(void)
1562{
1563 cfg_register_keywords(&cfg_kws);
William Lallemanda6c5f332016-11-19 02:25:36 +01001564 cli_register_kw(&cli_kws);
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001565}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001566
1567/*
1568 * Local variables:
1569 * c-indent-level: 8
1570 * c-basic-offset: 8
1571 * End:
1572 */