blob: d0b06d8eb1329a49a789ce7fa786e0ec51f6ca90 [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>
Olivier Houchard614f8d72017-03-14 20:08:46 +010049#include <proto/server.h>
Willy Tarreaud0807c32010-08-27 18:26:11 +020050#include <proto/signal.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020051#include <proto/stream.h>
William Lallemanda6c5f332016-11-19 02:25:36 +010052#include <proto/stream_interface.h>
Emeric Brun5a8c0a92010-09-23 18:44:36 +020053#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
55
Willy Tarreau918ff602011-07-25 16:33:49 +020056int listeners; /* # of proxy listeners, set by cfgparse */
Olivier Houchardfbc74e82017-11-24 16:54:05 +010057struct proxy *proxies_list = NULL; /* list of all existing proxies */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +020058struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */
Willy Tarreauf79d9502014-03-15 07:22:35 +010059struct eb_root proxy_by_name = EB_ROOT; /* tree of proxies sorted by name */
Willy Tarreau10479e42010-12-12 14:00:34 +010060unsigned int error_snapshot_id = 0; /* global ID assigned to each error then incremented */
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
Willy Tarreau977b8e42006-12-29 14:19:17 +010062/*
Willy Tarreau816eb542007-11-04 07:04:43 +010063 * This function returns a string containing a name describing capabilities to
64 * report comprehensible error messages. Specifically, it will return the words
Christopher Faulet898566e2016-10-26 11:06:28 +020065 * "frontend", "backend" when appropriate, or "proxy" for all other
Willy Tarreau816eb542007-11-04 07:04:43 +010066 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +010067 */
Willy Tarreau816eb542007-11-04 07:04:43 +010068const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +010069{
Willy Tarreau816eb542007-11-04 07:04:43 +010070 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
71 if (cap & PR_CAP_FE)
72 return "frontend";
73 else if (cap & PR_CAP_BE)
74 return "backend";
Willy Tarreau816eb542007-11-04 07:04:43 +010075 }
76 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +010077}
78
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010079/*
80 * This function returns a string containing the mode of the proxy in a format
81 * suitable for error messages.
82 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010083const char *proxy_mode_str(int mode) {
84
85 if (mode == PR_MODE_TCP)
86 return "tcp";
87 else if (mode == PR_MODE_HTTP)
88 return "http";
89 else if (mode == PR_MODE_HEALTH)
90 return "health";
William Lallemandcf62f7e2018-10-26 14:47:40 +020091 else if (mode == PR_MODE_CLI)
92 return "cli";
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010093 else
94 return "unknown";
95}
96
Willy Tarreauf3950172009-10-10 18:35:51 +020097/*
98 * This function scans the list of backends and servers to retrieve the first
99 * backend and the first server with the given names, and sets them in both
100 * parameters. It returns zero if either is not found, or non-zero and sets
101 * the ones it did not found to NULL. If a NULL pointer is passed for the
102 * backend, only the pointer to the server will be updated.
103 */
104int get_backend_server(const char *bk_name, const char *sv_name,
105 struct proxy **bk, struct server **sv)
106{
107 struct proxy *p;
108 struct server *s;
Willy Tarreau7ecc4202014-03-15 07:57:11 +0100109 int sid;
Willy Tarreauf3950172009-10-10 18:35:51 +0200110
111 *sv = NULL;
112
Willy Tarreau050536d2012-10-04 08:47:34 +0200113 sid = -1;
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200114 if (*sv_name == '#')
115 sid = atoi(sv_name + 1);
116
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200117 p = proxy_be_by_name(bk_name);
Willy Tarreauf3950172009-10-10 18:35:51 +0200118 if (bk)
119 *bk = p;
120 if (!p)
121 return 0;
122
123 for (s = p->srv; s; s = s->next)
Willy Tarreau4055a102012-11-15 00:15:18 +0100124 if ((sid >= 0 && s->puid == sid) ||
125 (sid < 0 && strcmp(s->id, sv_name) == 0))
Willy Tarreauf3950172009-10-10 18:35:51 +0200126 break;
127 *sv = s;
128 if (!s)
129 return 0;
130 return 1;
131}
132
Willy Tarreaue219db72007-12-03 01:30:13 +0100133/* This function parses a "timeout" statement in a proxy section. It returns
134 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200135 * return zero, it will write an error or warning message into a preallocated
136 * buffer returned at <err>. The trailing is not be written. The function must
137 * be called with <args> pointing to the first command line word, with <proxy>
138 * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL.
139 * As a special case for compatibility with older configs, it also accepts
140 * "{cli|srv|con}timeout" in args[0].
Willy Tarreaue219db72007-12-03 01:30:13 +0100141 */
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200142static int proxy_parse_timeout(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200143 struct proxy *defpx, const char *file, int line,
144 char **err)
Willy Tarreaue219db72007-12-03 01:30:13 +0100145{
146 unsigned timeout;
147 int retval, cap;
148 const char *res, *name;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200149 int *tv = NULL;
150 int *td = NULL;
Willy Tarreaued446492014-04-28 22:56:38 +0200151 int warn = 0;
Willy Tarreaue219db72007-12-03 01:30:13 +0100152
153 retval = 0;
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200154
155 /* simply skip "timeout" but remain compatible with old form */
156 if (strcmp(args[0], "timeout") == 0)
157 args++;
158
Willy Tarreaue219db72007-12-03 01:30:13 +0100159 name = args[0];
Willy Tarreaued446492014-04-28 22:56:38 +0200160 if (!strcmp(args[0], "client") || (!strcmp(args[0], "clitimeout") && (warn = WARN_CLITO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100161 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100162 tv = &proxy->timeout.client;
163 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100164 cap = PR_CAP_FE;
165 } else if (!strcmp(args[0], "tarpit")) {
166 tv = &proxy->timeout.tarpit;
167 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100168 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100169 } else if (!strcmp(args[0], "http-keep-alive")) {
170 tv = &proxy->timeout.httpka;
171 td = &defpx->timeout.httpka;
172 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau036fae02008-01-06 13:24:40 +0100173 } else if (!strcmp(args[0], "http-request")) {
174 tv = &proxy->timeout.httpreq;
175 td = &defpx->timeout.httpreq;
Willy Tarreaucd7afc02009-07-12 10:03:17 +0200176 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaued446492014-04-28 22:56:38 +0200177 } else if (!strcmp(args[0], "server") || (!strcmp(args[0], "srvtimeout") && (warn = WARN_SRVTO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100178 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100179 tv = &proxy->timeout.server;
180 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100181 cap = PR_CAP_BE;
Willy Tarreaued446492014-04-28 22:56:38 +0200182 } else if (!strcmp(args[0], "connect") || (!strcmp(args[0], "contimeout") && (warn = WARN_CONTO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100183 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100184 tv = &proxy->timeout.connect;
185 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100186 cap = PR_CAP_BE;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100187 } else if (!strcmp(args[0], "check")) {
188 tv = &proxy->timeout.check;
189 td = &defpx->timeout.check;
190 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100191 } else if (!strcmp(args[0], "queue")) {
192 tv = &proxy->timeout.queue;
193 td = &defpx->timeout.queue;
194 cap = PR_CAP_BE;
Willy Tarreauce887fd2012-05-12 12:50:00 +0200195 } else if (!strcmp(args[0], "tunnel")) {
196 tv = &proxy->timeout.tunnel;
197 td = &defpx->timeout.tunnel;
198 cap = PR_CAP_BE;
Willy Tarreau05cdd962014-05-10 14:30:07 +0200199 } else if (!strcmp(args[0], "client-fin")) {
200 tv = &proxy->timeout.clientfin;
201 td = &defpx->timeout.clientfin;
202 cap = PR_CAP_FE;
203 } else if (!strcmp(args[0], "server-fin")) {
204 tv = &proxy->timeout.serverfin;
205 td = &defpx->timeout.serverfin;
206 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100207 } else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200208 memprintf(err,
209 "'timeout' supports 'client', 'server', 'connect', 'check', "
Willy Tarreau05cdd962014-05-10 14:30:07 +0200210 "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', "
211 "'client-fin' and 'server-fin' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200212 args[0]);
Willy Tarreaue219db72007-12-03 01:30:13 +0100213 return -1;
214 }
215
216 if (*args[1] == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200217 memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100218 return -1;
219 }
220
221 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
222 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200223 memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100224 return -1;
225 }
226
227 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200228 memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability",
229 name, proxy_type_str(proxy), proxy->id,
230 (cap & PR_CAP_BE) ? "backend" : "frontend");
Willy Tarreaue219db72007-12-03 01:30:13 +0100231 retval = 1;
232 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200233 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200234 memprintf(err, "overwriting 'timeout %s' which was already specified", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100235 retval = 1;
236 }
Willy Tarreaued446492014-04-28 22:56:38 +0200237 else if (warn) {
238 if (!already_warned(warn)) {
239 memprintf(err, "the '%s' directive is now deprecated in favor of 'timeout %s', and will not be supported in future versions.",
240 args[0], name);
241 retval = 1;
242 }
243 }
Willy Tarreaue219db72007-12-03 01:30:13 +0100244
Willy Tarreaufac5b592014-05-22 08:24:46 +0200245 if (*args[2] != 0) {
246 memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]);
247 retval = -1;
248 }
249
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200250 *tv = MS_TO_TICKS(timeout);
Willy Tarreaue219db72007-12-03 01:30:13 +0100251 return retval;
252}
253
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100254/* This function parses a "rate-limit" statement in a proxy section. It returns
255 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200256 * return zero, it will write an error or warning message into a preallocated
257 * buffer returned at <err>. The function must be called with <args> pointing
258 * to the first command line word, with <proxy> pointing to the proxy being
259 * parsed, and <defpx> to the default proxy or NULL.
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100260 */
261static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200262 struct proxy *defpx, const char *file, int line,
263 char **err)
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100264{
265 int retval, cap;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 char *res;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100267 unsigned int *tv = NULL;
268 unsigned int *td = NULL;
269 unsigned int val;
270
271 retval = 0;
272
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200273 if (strcmp(args[1], "sessions") == 0) {
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200274 tv = &proxy->fe_sps_lim;
275 td = &defpx->fe_sps_lim;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100276 cap = PR_CAP_FE;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200277 }
278 else {
279 memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100280 return -1;
281 }
282
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200283 if (*args[2] == 0) {
284 memprintf(err, "'%s %s' expects expects an integer value (in sessions/second)", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100285 return -1;
286 }
287
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200288 val = strtoul(args[2], &res, 0);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100289 if (*res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200290 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 +0100291 return -1;
292 }
293
294 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200295 memprintf(err, "%s %s will be ignored because %s '%s' has no %s capability",
296 args[0], args[1], proxy_type_str(proxy), proxy->id,
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100297 (cap & PR_CAP_BE) ? "backend" : "frontend");
298 retval = 1;
299 }
300 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200301 memprintf(err, "overwriting %s %s which was already specified", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100302 retval = 1;
303 }
304
305 *tv = val;
306 return retval;
307}
308
Willy Tarreauc35362a2014-04-25 13:58:37 +0200309/* This function parses a "max-keep-alive-queue" statement in a proxy section.
310 * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it
311 * does not return zero, it will write an error or warning message into a
312 * preallocated buffer returned at <err>. The function must be called with
313 * <args> pointing to the first command line word, with <proxy> pointing to
314 * the proxy being parsed, and <defpx> to the default proxy or NULL.
315 */
316static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy,
317 struct proxy *defpx, const char *file, int line,
318 char **err)
319{
320 int retval;
321 char *res;
322 unsigned int val;
323
324 retval = 0;
325
326 if (*args[1] == 0) {
327 memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]);
328 return -1;
329 }
330
331 val = strtol(args[1], &res, 0);
332 if (*res) {
333 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
334 return -1;
335 }
336
337 if (!(proxy->cap & PR_CAP_BE)) {
338 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
339 args[0], proxy_type_str(proxy), proxy->id);
340 retval = 1;
341 }
342
343 /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */
344 proxy->max_ka_queue = val + 1;
345 return retval;
346}
347
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200348/* This function parses a "declare" statement in a proxy section. It returns -1
349 * if there is any error, 1 for warning, otherwise 0. If it does not return zero,
350 * it will write an error or warning message into a preallocated buffer returned
351 * at <err>. The function must be called with <args> pointing to the first command
352 * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the
353 * default proxy or NULL.
354 */
355static int proxy_parse_declare(char **args, int section, struct proxy *curpx,
356 struct proxy *defpx, const char *file, int line,
357 char **err)
358{
359 /* Capture keyword wannot be declared in a default proxy. */
360 if (curpx == defpx) {
361 memprintf(err, "'%s' not avalaible in default section", args[0]);
362 return -1;
363 }
364
365 /* Capture keywork is only avalaible in frontend. */
366 if (!(curpx->cap & PR_CAP_FE)) {
367 memprintf(err, "'%s' only avalaible in frontend or listen section", args[0]);
368 return -1;
369 }
370
371 /* Check mandatory second keyword. */
372 if (!args[1] || !*args[1]) {
373 memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]);
374 return -1;
375 }
376
377 /* Actually, declare is only avalaible for declaring capture
378 * slot, but in the future it can declare maps or variables.
379 * So, this section permits to check and switch acording with
380 * the second keyword.
381 */
382 if (strcmp(args[1], "capture") == 0) {
383 char *error = NULL;
384 long len;
385 struct cap_hdr *hdr;
386
387 /* Check the next keyword. */
388 if (!args[2] || !*args[2] ||
389 (strcmp(args[2], "response") != 0 &&
390 strcmp(args[2], "request") != 0)) {
391 memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]);
392 return -1;
393 }
394
395 /* Check the 'len' keyword. */
396 if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) {
397 memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]);
398 return -1;
399 }
400
401 /* Check the length value. */
402 if (!args[4] || !*args[4]) {
403 memprintf(err, "'%s %s': 'len' requires a numeric value that represents the "
404 "capture length",
405 args[0], args[1]);
406 return -1;
407 }
408
409 /* convert the length value. */
410 len = strtol(args[4], &error, 10);
411 if (*error != '\0') {
412 memprintf(err, "'%s %s': cannot parse the length '%s'.",
413 args[0], args[1], args[3]);
414 return -1;
415 }
416
417 /* check length. */
418 if (len <= 0) {
419 memprintf(err, "length must be > 0");
420 return -1;
421 }
422
423 /* register the capture. */
Vincent Bernat02779b62016-04-03 13:48:43 +0200424 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200425 hdr->name = NULL; /* not a header capture */
426 hdr->namelen = 0;
427 hdr->len = len;
428 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
429
430 if (strcmp(args[2], "request") == 0) {
431 hdr->next = curpx->req_cap;
432 hdr->index = curpx->nb_req_cap++;
433 curpx->req_cap = hdr;
434 }
435 if (strcmp(args[2], "response") == 0) {
436 hdr->next = curpx->rsp_cap;
437 hdr->index = curpx->nb_rsp_cap++;
438 curpx->rsp_cap = hdr;
439 }
440 return 0;
441 }
442 else {
443 memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]);
444 return -1;
445 }
446}
447
Willy Tarreauf79d9502014-03-15 07:22:35 +0100448/* This function inserts proxy <px> into the tree of known proxies. The proxy's
449 * name is used as the storing key so it must already have been initialized.
450 */
451void proxy_store_name(struct proxy *px)
452{
453 px->conf.by_name.key = px->id;
454 ebis_insert(&proxy_by_name, &px->conf.by_name);
455}
456
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200457/* Returns a pointer to the first proxy matching capabilities <cap> and id
458 * <id>. NULL is returned if no match is found. If <table> is non-zero, it
459 * only considers proxies having a table.
Willy Tarreaubc216c42011-08-02 11:25:54 +0200460 */
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200461struct proxy *proxy_find_by_id(int id, int cap, int table)
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200462{
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200463 struct eb32_node *n;
Willy Tarreaubc216c42011-08-02 11:25:54 +0200464
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200465 for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) {
466 struct proxy *px = container_of(n, struct proxy, conf.id);
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100467
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200468 if (px->uuid != id)
469 break;
Alex Williams96532db2009-11-01 21:27:13 -0500470
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200471 if ((px->cap & cap) != cap)
472 continue;
Alex Williams96532db2009-11-01 21:27:13 -0500473
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200474 if (table && !px->table.size)
475 continue;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100476
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200477 return px;
478 }
479 return NULL;
480}
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100481
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200482/* Returns a pointer to the first proxy matching either name <name>, or id
483 * <name> if <name> begins with a '#'. NULL is returned if no match is found.
484 * If <table> is non-zero, it only considers proxies having a table.
485 */
486struct proxy *proxy_find_by_name(const char *name, int cap, int table)
487{
488 struct proxy *curproxy;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200489
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200490 if (*name == '#') {
491 curproxy = proxy_find_by_id(atoi(name + 1), cap, table);
492 if (curproxy)
Willy Tarreauc739aa82015-05-26 11:35:41 +0200493 return curproxy;
Alex Williams96532db2009-11-01 21:27:13 -0500494 }
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100495 else {
496 struct ebpt_node *node;
497
498 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
499 curproxy = container_of(node, struct proxy, conf.by_name);
Alex Williams96532db2009-11-01 21:27:13 -0500500
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100501 if (strcmp(curproxy->id, name) != 0)
502 break;
503
504 if ((curproxy->cap & cap) != cap)
505 continue;
506
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200507 if (table && !curproxy->table.size)
508 continue;
509
Willy Tarreauc739aa82015-05-26 11:35:41 +0200510 return curproxy;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100511 }
512 }
Willy Tarreauc739aa82015-05-26 11:35:41 +0200513 return NULL;
Alex Williams96532db2009-11-01 21:27:13 -0500514}
515
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200516/* Finds the best match for a proxy with capabilities <cap>, name <name> and id
517 * <id>. At most one of <id> or <name> may be different provided that <cap> is
518 * valid. Either <id> or <name> may be left unspecified (0). The purpose is to
519 * find a proxy based on some information from a previous configuration, across
520 * reloads or during information exchange between peers.
521 *
522 * Names are looked up first if present, then IDs are compared if present. In
523 * case of an inexact match whatever is forced in the configuration has
524 * precedence in the following order :
525 * - 1) forced ID (proves a renaming / change of proxy type)
526 * - 2) proxy name+type (may indicate a move if ID differs)
527 * - 3) automatic ID+type (may indicate a renaming)
528 *
529 * Depending on what is found, we can end up in the following situations :
530 *
531 * name id cap | possible causes
532 * -------------+-----------------
533 * -- -- -- | nothing found
534 * -- -- ok | nothing found
535 * -- ok -- | proxy deleted, ID points to next one
536 * -- ok ok | proxy renamed, or deleted with ID pointing to next one
537 * ok -- -- | proxy deleted, but other half with same name still here (before)
538 * ok -- ok | proxy's ID changed (proxy moved in the config file)
539 * ok ok -- | proxy deleted, but other half with same name still here (after)
540 * ok ok ok | perfect match
541 *
542 * Upon return if <diff> is not NULL, it is zeroed then filled with up to 3 bits :
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200543 * - PR_FBM_MISMATCH_ID : proxy was found but ID differs
544 * (and ID was not zero)
545 * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs
546 * (and name was not NULL)
547 * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with
548 * the same name and/or id
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200549 *
550 * Only a valid proxy is returned. If capabilities do not match, NULL is
551 * returned. The caller can check <diff> to report detailed warnings / errors,
552 * and decide whether or not to use what was found.
553 */
554struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff)
555{
556 struct proxy *byname;
557 struct proxy *byid;
558
559 if (!name && !id)
560 return NULL;
561
562 if (diff)
563 *diff = 0;
564
565 byname = byid = NULL;
566
567 if (name) {
568 byname = proxy_find_by_name(name, cap, 0);
569 if (byname && (!id || byname->uuid == id))
570 return byname;
571 }
572
573 /* remaining possiblities :
574 * - name not set
575 * - name set but not found
576 * - name found, but ID doesn't match.
577 */
578 if (id) {
579 byid = proxy_find_by_id(id, cap, 0);
580 if (byid) {
581 if (byname) {
582 /* id+type found, name+type found, but not all 3.
583 * ID wins only if forced, otherwise name wins.
584 */
585 if (byid->options & PR_O_FORCED_ID) {
586 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200587 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200588 return byid;
589 }
590 else {
591 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200592 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200593 return byname;
594 }
595 }
596
597 /* remaining possiblities :
598 * - name not set
599 * - name set but not found
600 */
601 if (name && diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200602 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200603 return byid;
604 }
605
606 /* ID not found */
607 if (byname) {
608 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200609 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200610 return byname;
611 }
612 }
613
614 /* All remaining possiblities will lead to NULL. If we can report more
615 * detailed information to the caller about changed types and/or name,
616 * we'll do it. For example, we could detect that "listen foo" was
617 * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced.
618 * - name not set, ID not found
619 * - name not found, ID not set
620 * - name not found, ID not found
621 */
622 if (!diff)
623 return NULL;
624
625 if (name) {
626 byname = proxy_find_by_name(name, 0, 0);
627 if (byname && (!id || byname->uuid == id))
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200628 *diff |= PR_FBM_MISMATCH_PROXYTYPE;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200629 }
630
631 if (id) {
632 byid = proxy_find_by_id(id, 0, 0);
633 if (byid) {
634 if (!name)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200635 *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200636 else if (byid->options & PR_O_FORCED_ID)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200637 *diff |= PR_FBM_MISMATCH_NAME | PR_FBM_MISMATCH_PROXYTYPE; /* name and type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200638 /* otherwise it's a different proxy that was returned */
639 }
640 }
641 return NULL;
642}
643
Willy Tarreaubaaee002006-06-26 02:48:02 +0200644/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100645 * This function finds a server with matching name within selected proxy.
646 * It also checks if there are more matching servers with
647 * requested name as this often leads into unexpected situations.
648 */
649
650struct server *findserver(const struct proxy *px, const char *name) {
651
652 struct server *cursrv, *target = NULL;
653
654 if (!px)
655 return NULL;
656
657 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
658 if (strcmp(cursrv->id, name))
659 continue;
660
661 if (!target) {
662 target = cursrv;
663 continue;
664 }
665
Christopher Faulet767a84b2017-11-24 16:50:31 +0100666 ha_alert("Refusing to use duplicated server '%s' found in proxy: %s!\n",
667 name, px->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100668
669 return NULL;
670 }
671
672 return target;
673}
674
Willy Tarreauff01a212009-03-15 13:46:16 +0100675/* This function checks that the designated proxy has no http directives
676 * enabled. It will output a warning if there are, and will fix some of them.
677 * It returns the number of fatal errors encountered. This should be called
678 * at the end of the configuration parsing if the proxy is not in http mode.
679 * The <file> argument is used to construct the error message.
680 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200681int proxy_cfg_ensure_no_http(struct proxy *curproxy)
Willy Tarreauff01a212009-03-15 13:46:16 +0100682{
683 if (curproxy->cookie_name != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100684 ha_warning("config : cookie 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->rsp_exp != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100688 ha_warning("config : server regular expressions 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 }
691 if (curproxy->req_exp != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100692 ha_warning("config : client regular expressions will be ignored for %s '%s' (needs 'mode http').\n",
693 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100694 }
695 if (curproxy->monitor_uri != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100696 ha_warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n",
697 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100698 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200699 if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) {
Willy Tarreauff01a212009-03-15 13:46:16 +0100700 curproxy->lbprm.algo &= ~BE_LB_ALGO;
701 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Christopher Faulet767a84b2017-11-24 16:50:31 +0100702 ha_warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n",
703 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100704 }
Willy Tarreau17804162009-11-09 21:27:51 +0100705 if (curproxy->to_log & (LW_REQ | LW_RESP)) {
706 curproxy->to_log &= ~(LW_REQ | LW_RESP);
Christopher Faulet767a84b2017-11-24 16:50:31 +0100707 ha_warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n",
708 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
709 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau17804162009-11-09 21:27:51 +0100710 }
Willy Tarreau62a61232013-04-12 18:13:46 +0200711 if (curproxy->conf.logformat_string == default_http_log_format ||
712 curproxy->conf.logformat_string == clf_http_log_format) {
713 /* Note: we don't change the directive's file:line number */
714 curproxy->conf.logformat_string = default_tcp_log_format;
Christopher Faulet767a84b2017-11-24 16:50:31 +0100715 ha_warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
716 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
717 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau196729e2012-05-31 19:30:26 +0200718 }
719
Willy Tarreauff01a212009-03-15 13:46:16 +0100720 return 0;
721}
722
Willy Tarreau237250c2011-07-29 01:49:03 +0200723/* Perform the most basic initialization of a proxy :
724 * memset(), list_init(*), reset_timeouts(*).
Willy Tarreaub249e842011-09-07 18:41:08 +0200725 * Any new proxy or peer should be initialized via this function.
Willy Tarreau237250c2011-07-29 01:49:03 +0200726 */
727void init_new_proxy(struct proxy *p)
728{
729 memset(p, 0, sizeof(struct proxy));
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100730 p->obj_type = OBJ_TYPE_PROXY;
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400731 p->pendconns = EB_ROOT;
Willy Tarreau237250c2011-07-29 01:49:03 +0200732 LIST_INIT(&p->acl);
733 LIST_INIT(&p->http_req_rules);
Willy Tarreaue365c0b2013-06-11 16:06:12 +0200734 LIST_INIT(&p->http_res_rules);
Willy Tarreau353bc9f2014-04-28 22:05:31 +0200735 LIST_INIT(&p->block_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200736 LIST_INIT(&p->redirect_rules);
737 LIST_INIT(&p->mon_fail_cond);
738 LIST_INIT(&p->switching_rules);
Willy Tarreau4a5cade2012-04-05 21:09:48 +0200739 LIST_INIT(&p->server_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200740 LIST_INIT(&p->persist_rules);
741 LIST_INIT(&p->sticking_rules);
742 LIST_INIT(&p->storersp_rules);
743 LIST_INIT(&p->tcp_req.inspect_rules);
744 LIST_INIT(&p->tcp_rep.inspect_rules);
745 LIST_INIT(&p->tcp_req.l4_rules);
Willy Tarreau620408f2016-10-21 16:37:51 +0200746 LIST_INIT(&p->tcp_req.l5_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200747 LIST_INIT(&p->req_add);
748 LIST_INIT(&p->rsp_add);
749 LIST_INIT(&p->listener_queue);
William Lallemand0f99e342011-10-12 17:50:54 +0200750 LIST_INIT(&p->logsrvs);
William Lallemand723b73a2012-02-08 16:37:49 +0100751 LIST_INIT(&p->logformat);
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200752 LIST_INIT(&p->logformat_sd);
William Lallemanda73203e2012-03-12 12:48:57 +0100753 LIST_INIT(&p->format_unique_id);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200754 LIST_INIT(&p->conf.bind);
Willy Tarreau4348fad2012-09-20 16:48:07 +0200755 LIST_INIT(&p->conf.listeners);
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200756 LIST_INIT(&p->conf.args.list);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200757 LIST_INIT(&p->tcpcheck_rules);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100758 LIST_INIT(&p->filter_configs);
Willy Tarreau237250c2011-07-29 01:49:03 +0200759
760 /* Timeouts are defined as -1 */
761 proxy_reset_timeouts(p);
762 p->tcp_rep.inspect_delay = TICK_ETERNITY;
Willy Tarreau050536d2012-10-04 08:47:34 +0200763
764 /* initial uuid is unassigned (-1) */
765 p->uuid = -1;
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200766
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100767 HA_SPIN_INIT(&p->lock);
Willy Tarreau237250c2011-07-29 01:49:03 +0200768}
769
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100770/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200771 * This function creates all proxy sockets. It should be done very early,
772 * typically before privileges are dropped. The sockets will be registered
773 * but not added to any fd_set, in order not to loose them across the fork().
Willy Tarreau562515c2011-07-25 08:11:52 +0200774 * The proxies also start in READY state because they all have their listeners
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200775 * bound.
Willy Tarreau2ff76222007-04-09 19:29:56 +0200776 *
777 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
778 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779 */
780int start_proxies(int verbose)
781{
782 struct proxy *curproxy;
783 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100784 int lerr, err = ERR_NONE;
785 int pxerr;
786 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100788 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200789 if (curproxy->state != PR_STNEW)
790 continue; /* already initialized */
791
792 pxerr = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200793 list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100794 if (listener->state != LI_ASSIGNED)
795 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796
Emeric Bruncf20bf12010-10-22 16:06:11 +0200797 lerr = listener->proto->bind(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200798
Willy Tarreaue6b98942007-10-29 01:09:36 +0100799 /* errors are reported if <verbose> is set or if they are fatal */
800 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
801 if (lerr & ERR_ALERT)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100802 ha_alert("Starting %s %s: %s\n",
803 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100804 else if (lerr & ERR_WARN)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100805 ha_warning("Starting %s %s: %s\n",
806 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807 }
808
Willy Tarreaue6b98942007-10-29 01:09:36 +0100809 err |= lerr;
810 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100812 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100814 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815 pxerr |= 1;
816 continue;
817 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200818 }
819
820 if (!pxerr) {
Willy Tarreau562515c2011-07-25 08:11:52 +0200821 curproxy->state = PR_STREADY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
823 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100824
825 if (err & ERR_ABORT)
826 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200827 }
828
829 return err;
830}
831
832
833/*
Willy Tarreau918ff602011-07-25 16:33:49 +0200834 * This is the proxy management task. It enables proxies when there are enough
Willy Tarreau87b09662015-04-03 00:22:06 +0200835 * free streams, or stops them when the table is full. It is designed to be
Willy Tarreau918ff602011-07-25 16:33:49 +0200836 * called as a task which is woken up upon stopping or when rate limiting must
837 * be enforced.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 */
Olivier Houchard9f6af332018-05-25 14:04:04 +0200839struct task *manage_proxy(struct task *t, void *context, unsigned short state)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200840{
Olivier Houchard9f6af332018-05-25 14:04:04 +0200841 struct proxy *p = context;
Willy Tarreau918ff602011-07-25 16:33:49 +0200842 int next = TICK_ETERNITY;
Willy Tarreau79584222009-03-06 09:18:27 +0100843 unsigned int wait;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844
Willy Tarreau918ff602011-07-25 16:33:49 +0200845 /* We should periodically try to enable listeners waiting for a
846 * global resource here.
847 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848
Willy Tarreau918ff602011-07-25 16:33:49 +0200849 /* first, let's check if we need to stop the proxy */
850 if (unlikely(stopping && p->state != PR_STSTOPPED)) {
851 int t;
852 t = tick_remain(now_ms, p->stop_time);
853 if (t == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100854 ha_warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
855 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
Willy Tarreau918ff602011-07-25 16:33:49 +0200856 send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
857 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
858 stop_proxy(p);
859 /* try to free more memory */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100860 pool_gc(NULL);
Willy Tarreau918ff602011-07-25 16:33:49 +0200861 }
862 else {
863 next = tick_first(next, p->stop_time);
864 }
865 }
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200866
Willy Tarreau3a925c12013-09-04 17:54:01 +0200867 /* If the proxy holds a stick table, we need to purge all unused
868 * entries. These are all the ones in the table with ref_cnt == 0
869 * and all the ones in the pool used to allocate new entries. Any
Willy Tarreau87b09662015-04-03 00:22:06 +0200870 * entry attached to an existing stream waiting for a store will
Willy Tarreau3a925c12013-09-04 17:54:01 +0200871 * be in neither list. Any entry being dumped will have ref_cnt > 0.
872 * However we protect tables that are being synced to peers.
873 */
874 if (unlikely(stopping && p->state == PR_STSTOPPED && p->table.current)) {
875 if (!p->table.syncing) {
876 stktable_trash_oldest(&p->table, p->table.current);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100877 pool_gc(NULL);
Willy Tarreau3a925c12013-09-04 17:54:01 +0200878 }
879 if (p->table.current) {
880 /* some entries still remain, let's recheck in one second */
881 next = tick_first(next, tick_add(now_ms, 1000));
882 }
883 }
884
Willy Tarreau918ff602011-07-25 16:33:49 +0200885 /* the rest below is just for frontends */
886 if (!(p->cap & PR_CAP_FE))
887 goto out;
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200888
Willy Tarreau918ff602011-07-25 16:33:49 +0200889 /* check the various reasons we may find to block the frontend */
890 if (unlikely(p->feconn >= p->maxconn)) {
891 if (p->state == PR_STREADY)
892 p->state = PR_STFULL;
893 goto out;
894 }
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100895
Willy Tarreau918ff602011-07-25 16:33:49 +0200896 /* OK we have no reason to block, so let's unblock if we were blocking */
897 if (p->state == PR_STFULL)
898 p->state = PR_STREADY;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100899
Willy Tarreau918ff602011-07-25 16:33:49 +0200900 if (p->fe_sps_lim &&
901 (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) {
902 /* we're blocking because a limit was reached on the number of
903 * requests/s on the frontend. We want to re-check ASAP, which
904 * means in 1 ms before estimated expiration date, because the
905 * timer will have settled down.
906 */
907 next = tick_first(next, tick_add(now_ms, wait));
908 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200909 }
Willy Tarreau918ff602011-07-25 16:33:49 +0200910
911 /* The proxy is not limited so we can re-enable any waiting listener */
912 if (!LIST_ISEMPTY(&p->listener_queue))
913 dequeue_all_listeners(&p->listener_queue);
914 out:
915 t->expire = next;
916 task_queue(t);
917 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918}
919
920
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100921static int proxy_parse_hard_stop_after(char **args, int section_type, struct proxy *curpx,
922 struct proxy *defpx, const char *file, int line,
923 char **err)
924{
925 const char *res;
926
927 if (!*args[1]) {
928 memprintf(err, "'%s' expects <time> as argument.\n", args[0]);
929 return -1;
930 }
931 res = parse_time_err(args[1], &global.hard_stop_after, TIME_UNIT_MS);
932 if (res) {
933 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
934 return -1;
935 }
936 return 0;
937}
938
Olivier Houchard9f6af332018-05-25 14:04:04 +0200939struct task *hard_stop(struct task *t, void *context, unsigned short state)
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100940{
941 struct proxy *p;
942 struct stream *s;
943
944 if (killed) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100945 ha_warning("Some tasks resisted to hard-stop, exiting now.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100946 send_log(NULL, LOG_WARNING, "Some tasks resisted to hard-stop, exiting now.\n");
947 /* Do some cleanup and explicitely quit */
948 deinit();
949 exit(0);
950 }
951
Christopher Faulet767a84b2017-11-24 16:50:31 +0100952 ha_warning("soft-stop running for too long, performing a hard-stop.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100953 send_log(NULL, LOG_WARNING, "soft-stop running for too long, performing a hard-stop.\n");
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100954 p = proxies_list;
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100955 while (p) {
956 if ((p->cap & PR_CAP_FE) && (p->feconn > 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100957 ha_warning("Proxy %s hard-stopped (%d remaining conns will be closed).\n",
958 p->id, p->feconn);
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100959 send_log(p, LOG_WARNING, "Proxy %s hard-stopped (%d remaining conns will be closed).\n",
960 p->id, p->feconn);
961 }
962 p = p->next;
963 }
964 list_for_each_entry(s, &streams, list) {
965 stream_shutdown(s, SF_ERR_KILLED);
966 }
967
968 killed = 1;
969 t->expire = tick_add(now_ms, MS_TO_TICKS(1000));
970 return t;
971}
972
Willy Tarreaubaaee002006-06-26 02:48:02 +0200973/*
974 * this function disables health-check servers so that the process will quickly be ignored
975 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
976 * time will not be used since it would already not listen anymore to the socket.
977 */
978void soft_stop(void)
979{
980 struct proxy *p;
Willy Tarreaubbe11b12011-07-25 11:16:24 +0200981 struct peers *prs;
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100982 struct task *task;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200983
984 stopping = 1;
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100985 if (tick_isset(global.hard_stop_after)) {
Emeric Brunc60def82017-09-27 14:59:38 +0200986 task = task_new(MAX_THREADS_MASK);
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100987 if (task) {
988 task->process = hard_stop;
989 task_schedule(task, tick_add(now_ms, global.hard_stop_after));
990 }
991 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100992 ha_alert("out of memory trying to allocate the hard-stop task.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100993 }
994 }
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100995 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200996 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200997 while (p) {
Olivier Houchard1fc05162017-04-06 01:05:05 +0200998 /* Zombie proxy, let's close the file descriptors */
999 if (p->state == PR_STSTOPPED &&
1000 !LIST_ISEMPTY(&p->conf.listeners) &&
1001 LIST_ELEM(p->conf.listeners.n,
1002 struct listener *, by_fe)->state >= LI_ZOMBIE) {
1003 struct listener *l;
1004 list_for_each_entry(l, &p->conf.listeners, by_fe) {
1005 if (l->state >= LI_ZOMBIE)
1006 close(l->fd);
1007 l->state = LI_INIT;
1008 }
1009 }
1010
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011 if (p->state != PR_STSTOPPED) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001012 ha_warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace);
Willy Tarreauf8fbcef2008-10-10 17:51:34 +02001013 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 +02001014 p->stop_time = tick_add(now_ms, p->grace);
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001015
Willy Tarreau20b7afb2015-09-28 16:35:04 +02001016 /* Note: do not wake up stopped proxies' task nor their tables'
1017 * tasks as these ones might point to already released entries.
1018 */
1019 if (p->table.size && p->table.sync_task)
1020 task_wakeup(p->table.sync_task, TASK_WOKEN_MSG);
1021
1022 if (p->task)
1023 task_wakeup(p->task, TASK_WOKEN_MSG);
1024 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025 p = p->next;
1026 }
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001027
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001028 prs = cfg_peers;
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001029 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001030 if (prs->peers_fe)
1031 stop_proxy(prs->peers_fe);
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001032 prs = prs->next;
1033 }
Willy Tarreaud0807c32010-08-27 18:26:11 +02001034 /* signal zero is used to broadcast the "stopping" event */
1035 signal_handler(0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001036}
1037
1038
Willy Tarreaube58c382011-07-24 18:28:10 +02001039/* Temporarily disables listening on all of the proxy's listeners. Upon
1040 * success, the proxy enters the PR_PAUSED state. If disabling at least one
1041 * listener returns an error, then the proxy state is set to PR_STERROR
Willy Tarreauce8fe252011-09-07 19:14:57 +02001042 * because we don't know how to resume from this. The function returns 0
1043 * if it fails, or non-zero on success.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001044 */
Willy Tarreauce8fe252011-09-07 19:14:57 +02001045int pause_proxy(struct proxy *p)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046{
1047 struct listener *l;
Willy Tarreauce8fe252011-09-07 19:14:57 +02001048
1049 if (!(p->cap & PR_CAP_FE) || p->state == PR_STERROR ||
1050 p->state == PR_STSTOPPED || p->state == PR_STPAUSED)
1051 return 1;
1052
Christopher Faulet767a84b2017-11-24 16:50:31 +01001053 ha_warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001054 send_log(p, LOG_WARNING, "Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
1055
Willy Tarreau4348fad2012-09-20 16:48:07 +02001056 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreaube58c382011-07-24 18:28:10 +02001057 if (!pause_listener(l))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001058 p->state = PR_STERROR;
1059 }
Willy Tarreauce8fe252011-09-07 19:14:57 +02001060
1061 if (p->state == PR_STERROR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001062 ha_warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001063 send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
1064 return 0;
1065 }
1066
1067 p->state = PR_STPAUSED;
1068 return 1;
Willy Tarreauda250db2008-10-12 12:07:48 +02001069}
1070
Olivier Houchard1fc05162017-04-06 01:05:05 +02001071/* This function makes the proxy unusable, but keeps the listening sockets
1072 * opened, so that if any process requests them, we are able to serve them.
1073 * This should only be called early, before we started accepting requests.
1074 */
1075void zombify_proxy(struct proxy *p)
1076{
1077 struct listener *l;
1078 struct listener *first_to_listen = NULL;
1079
1080 list_for_each_entry(l, &p->conf.listeners, by_fe) {
1081 enum li_state oldstate = l->state;
1082
1083 unbind_listener_no_close(l);
1084 if (l->state >= LI_ASSIGNED) {
1085 delete_listener(l);
Olivier Houchard1fc05162017-04-06 01:05:05 +02001086 }
1087 /*
1088 * Pretend we're still up and running so that the fd
1089 * will be sent if asked.
1090 */
1091 l->state = LI_ZOMBIE;
1092 if (!first_to_listen && oldstate >= LI_LISTEN)
1093 first_to_listen = l;
1094 }
1095 /* Quick hack : at stop time, to know we have to close the sockets
1096 * despite the proxy being marked as stopped, make the first listener
1097 * of the listener list an active one, so that we don't have to
1098 * parse the whole list to be sure.
1099 */
1100 if (first_to_listen && LIST_ELEM(p->conf.listeners.n,
1101 struct listener *, by_fe) != first_to_listen) {
1102 LIST_DEL(&l->by_fe);
1103 LIST_ADD(&p->conf.listeners, &l->by_fe);
1104 }
1105
1106 p->state = PR_STSTOPPED;
1107}
Willy Tarreauda250db2008-10-12 12:07:48 +02001108
1109/*
1110 * This function completely stops a proxy and releases its listeners. It has
1111 * to be called when going down in order to release the ports so that another
1112 * process may bind to them. It must also be called on disabled proxies at the
William Lallemandc59f9882018-11-16 16:57:21 +01001113 * end of start-up. If all listeners are closed, the proxy is set to the
Willy Tarreauda250db2008-10-12 12:07:48 +02001114 * PR_STSTOPPED state.
1115 */
1116void stop_proxy(struct proxy *p)
1117{
1118 struct listener *l;
William Lallemandc59f9882018-11-16 16:57:21 +01001119 int nostop = 0;
Willy Tarreauda250db2008-10-12 12:07:48 +02001120
Willy Tarreau4348fad2012-09-20 16:48:07 +02001121 list_for_each_entry(l, &p->conf.listeners, by_fe) {
William Lallemandc59f9882018-11-16 16:57:21 +01001122 if (l->options & LI_O_NOSTOP) {
1123 HA_ATOMIC_ADD(&unstoppable_jobs, 1);
1124 nostop = 1;
1125 continue;
1126 }
Willy Tarreauda250db2008-10-12 12:07:48 +02001127 unbind_listener(l);
1128 if (l->state >= LI_ASSIGNED) {
1129 delete_listener(l);
Willy Tarreauda250db2008-10-12 12:07:48 +02001130 }
1131 }
William Lallemandc59f9882018-11-16 16:57:21 +01001132 if (!nostop)
1133 p->state = PR_STSTOPPED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001134}
1135
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001136/* This function resumes listening on the specified proxy. It scans all of its
1137 * listeners and tries to enable them all. If any of them fails, the proxy is
1138 * put back to the paused state. It returns 1 upon success, or zero if an error
1139 * is encountered.
1140 */
1141int resume_proxy(struct proxy *p)
1142{
1143 struct listener *l;
1144 int fail;
1145
1146 if (p->state != PR_STPAUSED)
1147 return 1;
1148
Christopher Faulet767a84b2017-11-24 16:50:31 +01001149 ha_warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001150 send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
1151
1152 fail = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001153 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001154 if (!resume_listener(l)) {
1155 int port;
1156
1157 port = get_host_port(&l->addr);
1158 if (port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001159 ha_warning("Port %d busy while trying to enable %s %s.\n",
1160 port, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001161 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
1162 port, proxy_cap_str(p->cap), p->id);
1163 }
1164 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001165 ha_warning("Bind on socket %d busy while trying to enable %s %s.\n",
1166 l->luid, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001167 send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n",
1168 l->luid, proxy_cap_str(p->cap), p->id);
1169 }
1170
1171 /* Another port might have been enabled. Let's stop everything. */
1172 fail = 1;
1173 break;
1174 }
1175 }
1176
1177 p->state = PR_STREADY;
1178 if (fail) {
1179 pause_proxy(p);
1180 return 0;
1181 }
1182 return 1;
1183}
1184
Willy Tarreaubaaee002006-06-26 02:48:02 +02001185/*
1186 * This function temporarily disables listening so that another new instance
1187 * can start listening. It is designed to be called upon reception of a
1188 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
1189 * the proxy, or a SIGTTIN can be sent to listen again.
1190 */
1191void pause_proxies(void)
1192{
1193 int err;
1194 struct proxy *p;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001195 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001196
1197 err = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001198 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001199 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001200 while (p) {
Willy Tarreauce8fe252011-09-07 19:14:57 +02001201 err |= !pause_proxy(p);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001202 p = p->next;
1203 }
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001204
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001205 prs = cfg_peers;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001206 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001207 if (prs->peers_fe)
1208 err |= !pause_proxy(prs->peers_fe);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001209 prs = prs->next;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001210 }
1211
Willy Tarreaubaaee002006-06-26 02:48:02 +02001212 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001213 ha_warning("Some proxies refused to pause, performing soft stop now.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001214 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
1215 soft_stop();
1216 }
1217}
1218
1219
1220/*
1221 * This function reactivates listening. This can be used after a call to
1222 * sig_pause(), for example when a new instance has failed starting up.
1223 * It is designed to be called upon reception of a SIGTTIN.
1224 */
Willy Tarreaube58c382011-07-24 18:28:10 +02001225void resume_proxies(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001226{
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001227 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001228 struct proxy *p;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001229 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001230
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001231 err = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001232 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001233 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001234 while (p) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001235 err |= !resume_proxy(p);
1236 p = p->next;
1237 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001238
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001239 prs = cfg_peers;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001240 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001241 if (prs->peers_fe)
1242 err |= !resume_proxy(prs->peers_fe);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001243 prs = prs->next;
1244 }
Willy Tarreaube58c382011-07-24 18:28:10 +02001245
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001246 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001247 ha_warning("Some proxies refused to resume, a restart is probably needed to resume safe operations.\n");
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001248 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 +02001249 }
1250}
1251
Willy Tarreau87b09662015-04-03 00:22:06 +02001252/* Set current stream's backend to <be>. Nothing is done if the
1253 * stream already had a backend assigned, which is indicated by
Willy Tarreaue7dff022015-04-03 01:14:29 +02001254 * s->flags & SF_BE_ASSIGNED.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001255 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001256 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001257 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001258int stream_set_backend(struct stream *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001259{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001260 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001261 return 1;
Christopher Faulet41179042016-06-21 11:54:52 +02001262
1263 if (flt_set_stream_backend(s, be) < 0)
1264 return 0;
1265
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001266 s->be = be;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001267 HA_ATOMIC_UPDATE_MAX(&be->be_counters.conn_max,
1268 HA_ATOMIC_ADD(&be->beconn, 1));
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001269 proxy_inc_be_ctr(be);
1270
Willy Tarreau87b09662015-04-03 00:22:06 +02001271 /* assign new parameters to the stream from the new backend */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001272 s->si[1].flags &= ~SI_FL_INDEP_STR;
1273 if (be->options2 & PR_O2_INDEPSTR)
1274 s->si[1].flags |= SI_FL_INDEP_STR;
1275
Hongbo Longe39683c2017-03-10 18:41:51 +01001276 if (tick_isset(be->timeout.serverfin))
1277 s->si[1].hcto = be->timeout.serverfin;
1278
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001279 /* We want to enable the backend-specific analysers except those which
1280 * were already run as part of the frontend/listener. Note that it would
1281 * be more reliable to store the list of analysers that have been run,
1282 * but what we do here is OK for now.
1283 */
Christopher Faulet70e2f272017-01-09 16:33:19 +01001284 s->req.analysers |= be->be_req_ana & ~(strm_li(s) ? strm_li(s)->analysers : 0);
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001285
Willy Tarreau51aecc72009-07-12 09:47:04 +02001286 /* If the target backend requires HTTP processing, we have to allocate
Willy Tarreaueee5b512015-04-03 23:46:31 +02001287 * the HTTP transaction and hdr_idx if we did not have one.
Willy Tarreau51aecc72009-07-12 09:47:04 +02001288 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001289 if (unlikely(!s->txn && be->http_needed)) {
1290 if (unlikely(!http_alloc_txn(s)))
Willy Tarreau51aecc72009-07-12 09:47:04 +02001291 return 0; /* not enough memory */
Willy Tarreau39e4f622010-05-31 17:01:36 +02001292
1293 /* and now initialize the HTTP transaction state */
1294 http_init_txn(s);
Willy Tarreau51aecc72009-07-12 09:47:04 +02001295 }
1296
Christopher Faulet309c6412015-12-02 09:57:32 +01001297 /* Be sure to filter request headers if the backend is an HTTP proxy and
1298 * if there are filters attached to the stream. */
1299 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01001300 s->req.analysers |= AN_REQ_FLT_HTTP_HDRS;
Christopher Faulet309c6412015-12-02 09:57:32 +01001301
Willy Tarreaueee5b512015-04-03 23:46:31 +02001302 if (s->txn) {
1303 if (be->options2 & PR_O2_RSPBUG_OK)
1304 s->txn->rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001305
Willy Tarreaueee5b512015-04-03 23:46:31 +02001306 /* If we chain to an HTTP backend running a different HTTP mode, we
1307 * have to re-adjust the desired keep-alive/close mode to accommodate
1308 * both the frontend's and the backend's modes.
1309 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001310 if (strm_fe(s)->mode == PR_MODE_HTTP && be->mode == PR_MODE_HTTP &&
1311 ((strm_fe(s)->options & PR_O_HTTP_MODE) != (be->options & PR_O_HTTP_MODE)))
Willy Tarreaueee5b512015-04-03 23:46:31 +02001312 http_adjust_conn_mode(s, s->txn, &s->txn->req);
Willy Tarreau80a92c02014-03-12 10:41:13 +01001313
Willy Tarreaueee5b512015-04-03 23:46:31 +02001314 /* If an LB algorithm needs to access some pre-parsed body contents,
1315 * we must not start to forward anything until the connection is
1316 * confirmed otherwise we'll lose the pointer to these data and
1317 * prevent the hash from being doable again after a redispatch.
1318 */
1319 if (be->mode == PR_MODE_HTTP &&
1320 (be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_HI | BE_LB_HASH_PRM))
1321 s->txn->req.flags |= HTTP_MSGF_WAIT_CONN;
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001322
1323 /* we may request to parse a request body */
1324 if ((be->options & PR_O_WREQ_BODY) &&
1325 (s->txn->req.body_len || (s->txn->req.flags & HTTP_MSGF_TE_CHNK)))
1326 s->req.analysers |= AN_REQ_HTTP_BODY;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001327 }
1328
1329 s->flags |= SF_BE_ASSIGNED;
Willy Tarreau96e31212011-05-30 18:10:30 +02001330 if (be->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001331 s->req.flags |= CF_NEVER_WAIT;
1332 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02001333 }
1334
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001335 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001336}
1337
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001338/* Capture a bad request or response and archive it in the proxy's structure.
1339 * It is relatively protocol-agnostic so it requires that a number of elements
1340 * are passed :
1341 * - <proxy> is the proxy where the error was detected and where the snapshot
1342 * needs to be stored
1343 * - <is_back> indicates that the error happend when receiving the response
1344 * - <other_end> is a pointer to the proxy on the other side when known
1345 * - <target> is the target of the connection, usually a server or a proxy
1346 * - <sess> is the session which experienced the error
1347 * - <ctx> may be NULL or should contain any info relevant to the protocol
1348 * - <buf> is the buffer containing the offending data
1349 * - <buf_ofs> is the position of this buffer's input data in the input
1350 * stream, starting at zero. It may be passed as zero if unknown.
1351 * - <buf_out> is the portion of <buf->data> which was already forwarded and
1352 * which precedes the buffer's input. The buffer's input starts at
1353 * buf->head + buf_out.
1354 * - <err_pos> is the pointer to the faulty byte in the buffer's input.
1355 * - <show> is the callback to use to display <ctx>. It may be NULL.
1356 */
1357void proxy_capture_error(struct proxy *proxy, int is_back,
1358 struct proxy *other_end, enum obj_type *target,
1359 const struct session *sess,
1360 const struct buffer *buf, long buf_ofs,
1361 unsigned int buf_out, unsigned int err_pos,
1362 const union error_snapshot_ctx *ctx,
1363 void (*show)(struct buffer *, const struct error_snapshot *))
1364{
1365 struct error_snapshot *es;
1366 unsigned int buf_len;
1367 int len1, len2;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001368 unsigned int ev_id;
1369
1370 ev_id = HA_ATOMIC_XADD(&error_snapshot_id, 1);
1371
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001372 buf_len = b_data(buf) - buf_out;
1373
1374 es = malloc(sizeof(*es) + buf_len);
Willy Tarreauc55015e2018-09-07 19:02:32 +02001375 if (!es)
1376 return;
1377
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001378 es->buf_len = buf_len;
1379 es->ev_id = ev_id;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001380
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001381 len1 = b_size(buf) - buf_len;
1382 if (len1 > buf_len)
1383 len1 = buf_len;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001384
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001385 if (len1) {
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001386 memcpy(es->buf, b_peek(buf, buf_out), len1);
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001387 len2 = buf_len - len1;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001388 if (len2)
1389 memcpy(es->buf + len1, b_orig(buf), len2);
1390 }
1391
1392 es->buf_err = err_pos;
1393 es->when = date; // user-visible date
1394 es->srv = objt_server(target);
1395 es->oe = other_end;
1396 if (objt_conn(sess->origin))
1397 es->src = __objt_conn(sess->origin)->addr.from;
1398 else
1399 memset(&es->src, 0, sizeof(es->src));
1400
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001401 es->buf_wrap = b_wrap(buf) - b_peek(buf, buf_out);
1402 es->buf_out = buf_out;
1403 es->buf_ofs = buf_ofs;
1404
1405 /* be sure to indicate the offset of the first IN byte */
1406 if (es->buf_ofs >= es->buf_len)
1407 es->buf_ofs -= es->buf_len;
1408 else
1409 es->buf_ofs = 0;
1410
1411 /* protocol-specific part now */
1412 if (ctx)
1413 es->ctx = *ctx;
1414 else
1415 memset(&es->ctx, 0, sizeof(es->ctx));
1416 es->show = show;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001417
1418 /* note: we still lock since we have to be certain that nobody is
1419 * dumping the output while we free.
1420 */
1421 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
1422 if (is_back) {
1423 es = HA_ATOMIC_XCHG(&proxy->invalid_rep, es);
1424 } else {
1425 es = HA_ATOMIC_XCHG(&proxy->invalid_req, es);
1426 }
1427 free(es);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001428 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
1429}
1430
1431/* Config keywords below */
1432
Willy Tarreaudc13c112013-06-21 23:16:39 +02001433static struct cfg_kw_list cfg_kws = {ILH, {
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001434 { CFG_GLOBAL, "hard-stop-after", proxy_parse_hard_stop_after },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001435 { CFG_LISTEN, "timeout", proxy_parse_timeout },
1436 { CFG_LISTEN, "clitimeout", proxy_parse_timeout },
1437 { CFG_LISTEN, "contimeout", proxy_parse_timeout },
1438 { CFG_LISTEN, "srvtimeout", proxy_parse_timeout },
Willy Tarreau3a7d2072009-03-05 23:48:25 +01001439 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreauc35362a2014-04-25 13:58:37 +02001440 { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue },
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +02001441 { CFG_LISTEN, "declare", proxy_parse_declare },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001442 { 0, NULL, NULL },
1443}};
1444
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001445/* Expects to find a frontend named <arg> and returns it, otherwise displays various
1446 * adequate error messages and returns NULL. This function is designed to be used by
1447 * functions requiring a frontend on the CLI.
1448 */
1449struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg)
1450{
1451 struct proxy *px;
1452
1453 if (!*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001454 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001455 appctx->ctx.cli.msg = "A frontend name is expected.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001456 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001457 return NULL;
1458 }
1459
1460 px = proxy_fe_by_name(arg);
1461 if (!px) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001462 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001463 appctx->ctx.cli.msg = "No such frontend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001464 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001465 return NULL;
1466 }
1467 return px;
1468}
1469
Olivier Houchard614f8d72017-03-14 20:08:46 +01001470/* Expects to find a backend named <arg> and returns it, otherwise displays various
1471 * adequate error messages and returns NULL. This function is designed to be used by
1472 * functions requiring a frontend on the CLI.
1473 */
1474struct proxy *cli_find_backend(struct appctx *appctx, const char *arg)
1475{
1476 struct proxy *px;
1477
1478 if (!*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001479 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001480 appctx->ctx.cli.msg = "A backend name is expected.\n";
1481 appctx->st0 = CLI_ST_PRINT;
1482 return NULL;
1483 }
1484
1485 px = proxy_be_by_name(arg);
1486 if (!px) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001487 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001488 appctx->ctx.cli.msg = "No such backend.\n";
1489 appctx->st0 = CLI_ST_PRINT;
1490 return NULL;
1491 }
1492 return px;
1493}
1494
1495
William Lallemanda6c5f332016-11-19 02:25:36 +01001496/* parse a "show servers" CLI line, returns 0 if it wants to start the dump or
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001497 * 1 if it stops immediately. If an argument is specified, it will set the proxy
1498 * pointer into cli.p0 and its ID into cli.i0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001499 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001500static int cli_parse_show_servers(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemanda6c5f332016-11-19 02:25:36 +01001501{
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001502 struct proxy *px;
William Lallemanda6c5f332016-11-19 02:25:36 +01001503
1504 /* check if a backend name has been provided */
1505 if (*args[3]) {
1506 /* read server state from local file */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001507 px = proxy_be_by_name(args[3]);
William Lallemanda6c5f332016-11-19 02:25:36 +01001508
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001509 if (!px) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001510 appctx->ctx.cli.severity = LOG_ERR;
William Lallemanda6c5f332016-11-19 02:25:36 +01001511 appctx->ctx.cli.msg = "Can't find backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001512 appctx->st0 = CLI_ST_PRINT;
William Lallemanda6c5f332016-11-19 02:25:36 +01001513 return 1;
1514 }
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001515 appctx->ctx.cli.p0 = px;
1516 appctx->ctx.cli.i0 = px->uuid;
William Lallemanda6c5f332016-11-19 02:25:36 +01001517 }
1518 return 0;
1519}
1520
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001521/* dumps server state information into <buf> for all the servers found in backend cli.p0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001522 * These information are all the parameters which may change during HAProxy runtime.
1523 * By default, we only export to the last known server state file format.
1524 * These information can be used at next startup to recover same level of server state.
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001525 * It uses the proxy pointer from cli.p0, the proxy's id from cli.i0 and the server's
1526 * pointer from cli.p1.
William Lallemanda6c5f332016-11-19 02:25:36 +01001527 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001528static int dump_servers_state(struct stream_interface *si, struct buffer *buf)
William Lallemanda6c5f332016-11-19 02:25:36 +01001529{
1530 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001531 struct proxy *px = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001532 struct server *srv;
1533 char srv_addr[INET6_ADDRSTRLEN + 1];
1534 time_t srv_time_since_last_change;
1535 int bk_f_forced_id, srv_f_forced_id;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001536 char *srvrecord;
William Lallemanda6c5f332016-11-19 02:25:36 +01001537
William Lallemanda6c5f332016-11-19 02:25:36 +01001538 /* we don't want to report any state if the backend is not enabled on this process */
Willy Tarreau387bd4f2017-11-10 19:08:14 +01001539 if (px->bind_proc && !(px->bind_proc & pid_bit))
William Lallemanda6c5f332016-11-19 02:25:36 +01001540 return 1;
1541
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001542 if (!appctx->ctx.cli.p1)
1543 appctx->ctx.cli.p1 = px->srv;
William Lallemanda6c5f332016-11-19 02:25:36 +01001544
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001545 for (; appctx->ctx.cli.p1 != NULL; appctx->ctx.cli.p1 = srv->next) {
1546 srv = appctx->ctx.cli.p1;
William Lallemanda6c5f332016-11-19 02:25:36 +01001547 srv_addr[0] = '\0';
1548
1549 switch (srv->addr.ss_family) {
1550 case AF_INET:
1551 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
1552 srv_addr, INET_ADDRSTRLEN + 1);
1553 break;
1554 case AF_INET6:
1555 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
1556 srv_addr, INET6_ADDRSTRLEN + 1);
1557 break;
Daniel Corbett9215ffa2018-05-19 19:43:24 -04001558 default:
1559 memcpy(srv_addr, "-\0", 2);
1560 break;
William Lallemanda6c5f332016-11-19 02:25:36 +01001561 }
1562 srv_time_since_last_change = now.tv_sec - srv->last_change;
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001563 bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001564 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
1565
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001566 srvrecord = NULL;
1567 if (srv->srvrq && srv->srvrq->name)
1568 srvrecord = srv->srvrq->name;
1569
William Lallemanda6c5f332016-11-19 02:25:36 +01001570 chunk_appendf(buf,
1571 "%d %s "
1572 "%d %s %s "
1573 "%d %d %d %d %ld "
1574 "%d %d %d %d %d "
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001575 "%d %d %s %u %s"
William Lallemanda6c5f332016-11-19 02:25:36 +01001576 "\n",
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001577 px->uuid, px->id,
William Lallemanda6c5f332016-11-19 02:25:36 +01001578 srv->puid, srv->id, srv_addr,
Emeric Brun52a91d32017-08-31 14:41:55 +02001579 srv->cur_state, srv->cur_admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
William Lallemanda6c5f332016-11-19 02:25:36 +01001580 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001581 bk_f_forced_id, srv_f_forced_id, srv->hostname ? srv->hostname : "-", srv->svc_port,
1582 srvrecord ? srvrecord : "-");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001583 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001584 si_rx_room_blk(si);
William Lallemanda6c5f332016-11-19 02:25:36 +01001585 return 0;
1586 }
1587 }
1588 return 1;
1589}
1590
1591/* Parses backend list or simply use backend name provided by the user to return
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001592 * states of servers to stdout. It dumps proxy <cli.p0> and stops if <cli.i0> is
1593 * non-null.
William Lallemanda6c5f332016-11-19 02:25:36 +01001594 */
1595static int cli_io_handler_servers_state(struct appctx *appctx)
1596{
1597 struct stream_interface *si = appctx->owner;
William Lallemanda6c5f332016-11-19 02:25:36 +01001598 struct proxy *curproxy;
1599
1600 chunk_reset(&trash);
1601
1602 if (appctx->st2 == STAT_ST_INIT) {
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001603 if (!appctx->ctx.cli.p0)
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001604 appctx->ctx.cli.p0 = proxies_list;
William Lallemanda6c5f332016-11-19 02:25:36 +01001605 appctx->st2 = STAT_ST_HEAD;
1606 }
1607
1608 if (appctx->st2 == STAT_ST_HEAD) {
1609 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001610 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001611 si_rx_room_blk(si);
William Lallemanda6c5f332016-11-19 02:25:36 +01001612 return 0;
1613 }
1614 appctx->st2 = STAT_ST_INFO;
1615 }
1616
1617 /* STAT_ST_INFO */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001618 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1619 curproxy = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001620 /* servers are only in backends */
1621 if (curproxy->cap & PR_CAP_BE) {
1622 if (!dump_servers_state(si, &trash))
1623 return 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001624 }
1625 /* only the selected proxy is dumped */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001626 if (appctx->ctx.cli.i0)
William Lallemanda6c5f332016-11-19 02:25:36 +01001627 break;
1628 }
1629
1630 return 1;
1631}
1632
Willy Tarreau608ea592016-12-16 18:01:15 +01001633/* Parses backend list and simply report backend names. It keeps the proxy
1634 * pointer in cli.p0.
1635 */
William Lallemand933efcd2016-11-22 12:34:16 +01001636static int cli_io_handler_show_backend(struct appctx *appctx)
1637{
William Lallemand933efcd2016-11-22 12:34:16 +01001638 struct stream_interface *si = appctx->owner;
1639 struct proxy *curproxy;
1640
1641 chunk_reset(&trash);
1642
Willy Tarreau608ea592016-12-16 18:01:15 +01001643 if (!appctx->ctx.cli.p0) {
William Lallemand933efcd2016-11-22 12:34:16 +01001644 chunk_printf(&trash, "# name\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001645 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001646 si_rx_room_blk(si);
William Lallemand933efcd2016-11-22 12:34:16 +01001647 return 0;
1648 }
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001649 appctx->ctx.cli.p0 = proxies_list;
William Lallemand933efcd2016-11-22 12:34:16 +01001650 }
1651
Willy Tarreau608ea592016-12-16 18:01:15 +01001652 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1653 curproxy = appctx->ctx.cli.p0;
William Lallemand933efcd2016-11-22 12:34:16 +01001654
1655 /* looking for backends only */
1656 if (!(curproxy->cap & PR_CAP_BE))
1657 continue;
1658
1659 /* we don't want to list a backend which is bound to this process */
Willy Tarreau387bd4f2017-11-10 19:08:14 +01001660 if (curproxy->bind_proc && !(curproxy->bind_proc & pid_bit))
William Lallemand933efcd2016-11-22 12:34:16 +01001661 continue;
1662
1663 chunk_appendf(&trash, "%s\n", curproxy->id);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001664 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001665 si_rx_room_blk(si);
William Lallemand933efcd2016-11-22 12:34:16 +01001666 return 0;
1667 }
1668 }
1669
1670 return 1;
1671}
William Lallemanda6c5f332016-11-19 02:25:36 +01001672
Willy Tarreaua275a372018-08-21 14:50:44 +02001673/* Parses the "enable dynamic-cookies backend" directive, it always returns 1.
1674 *
1675 * Grabs the proxy lock and each server's lock.
1676 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001677static int cli_parse_enable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001678{
1679 struct proxy *px;
1680 struct server *s;
1681
1682 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1683 return 1;
1684
1685 px = cli_find_backend(appctx, args[3]);
1686 if (!px)
1687 return 1;
1688
Willy Tarreaua275a372018-08-21 14:50:44 +02001689 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1690
Olivier Houchard614f8d72017-03-14 20:08:46 +01001691 px->ck_opts |= PR_CK_DYNAMIC;
1692
Willy Tarreaua275a372018-08-21 14:50:44 +02001693 for (s = px->srv; s != NULL; s = s->next) {
1694 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001695 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02001696 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
1697 }
1698
1699 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001700
1701 return 1;
1702}
1703
Willy Tarreaua275a372018-08-21 14:50:44 +02001704/* Parses the "disable dynamic-cookies backend" directive, it always returns 1.
1705 *
1706 * Grabs the proxy lock and each server's lock.
1707 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001708static int cli_parse_disable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001709{
1710 struct proxy *px;
1711 struct server *s;
1712
1713 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1714 return 1;
1715
1716 px = cli_find_backend(appctx, args[3]);
1717 if (!px)
1718 return 1;
1719
Willy Tarreaua275a372018-08-21 14:50:44 +02001720 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1721
Olivier Houchard614f8d72017-03-14 20:08:46 +01001722 px->ck_opts &= ~PR_CK_DYNAMIC;
1723
1724 for (s = px->srv; s != NULL; s = s->next) {
Willy Tarreaua275a372018-08-21 14:50:44 +02001725 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001726 if (!(s->flags & SRV_F_COOKIESET)) {
1727 free(s->cookie);
1728 s->cookie = NULL;
1729 }
Willy Tarreaua275a372018-08-21 14:50:44 +02001730 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001731 }
1732
Willy Tarreaua275a372018-08-21 14:50:44 +02001733 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1734
Olivier Houchard614f8d72017-03-14 20:08:46 +01001735 return 1;
1736}
1737
Willy Tarreaua275a372018-08-21 14:50:44 +02001738/* Parses the "set dynamic-cookie-key backend" directive, it always returns 1.
1739 *
1740 * Grabs the proxy lock and each server's lock.
1741 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001742static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001743{
1744 struct proxy *px;
1745 struct server *s;
1746 char *newkey;
1747
1748 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1749 return 1;
1750
1751 px = cli_find_backend(appctx, args[3]);
1752 if (!px)
1753 return 1;
1754
1755 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001756 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001757 appctx->ctx.cli.msg = "String value expected.\n";
1758 appctx->st0 = CLI_ST_PRINT;
1759 return 1;
1760 }
1761
1762 newkey = strdup(args[4]);
1763 if (!newkey) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001764 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001765 appctx->ctx.cli.msg = "Failed to allocate memory.\n";
1766 appctx->st0 = CLI_ST_PRINT;
1767 return 1;
1768 }
Willy Tarreaua275a372018-08-21 14:50:44 +02001769
1770 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1771
Olivier Houchard614f8d72017-03-14 20:08:46 +01001772 free(px->dyncookie_key);
1773 px->dyncookie_key = newkey;
1774
Willy Tarreaua275a372018-08-21 14:50:44 +02001775 for (s = px->srv; s != NULL; s = s->next) {
1776 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001777 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02001778 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
1779 }
1780
1781 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001782
1783 return 1;
1784}
1785
Willy Tarreaua275a372018-08-21 14:50:44 +02001786/* Parses the "set maxconn frontend" directive, it always returns 1.
1787 *
1788 * Grabs the proxy lock.
1789 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001790static int cli_parse_set_maxconn_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001791{
1792 struct proxy *px;
1793 struct listener *l;
1794 int v;
1795
1796 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1797 return 1;
1798
1799 px = cli_find_frontend(appctx, args[3]);
1800 if (!px)
1801 return 1;
1802
1803 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001804 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001805 appctx->ctx.cli.msg = "Integer value expected.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001806 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001807 return 1;
1808 }
1809
1810 v = atoi(args[4]);
1811 if (v < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001812 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001813 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001814 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001815 return 1;
1816 }
1817
1818 /* OK, the value is fine, so we assign it to the proxy and to all of
1819 * its listeners. The blocked ones will be dequeued.
1820 */
Willy Tarreaua275a372018-08-21 14:50:44 +02001821 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1822
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001823 px->maxconn = v;
1824 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1825 l->maxconn = v;
1826 if (l->state == LI_FULL)
1827 resume_listener(l);
1828 }
1829
1830 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&px->listener_queue))
1831 dequeue_all_listeners(&px->listener_queue);
1832
Willy Tarreaua275a372018-08-21 14:50:44 +02001833 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1834
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001835 return 1;
1836}
1837
Willy Tarreaua275a372018-08-21 14:50:44 +02001838/* Parses the "shutdown frontend" directive, it always returns 1.
1839 *
1840 * Grabs the proxy lock.
1841 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001842static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001843{
1844 struct proxy *px;
1845
1846 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1847 return 1;
1848
1849 px = cli_find_frontend(appctx, args[2]);
1850 if (!px)
1851 return 1;
1852
1853 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001854 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001855 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001856 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001857 return 1;
1858 }
1859
Christopher Faulet767a84b2017-11-24 16:50:31 +01001860 ha_warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1861 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001862 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1863 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
Willy Tarreaua275a372018-08-21 14:50:44 +02001864
1865 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001866 stop_proxy(px);
Willy Tarreaua275a372018-08-21 14:50:44 +02001867 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1868
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001869 return 1;
1870}
1871
Willy Tarreaua275a372018-08-21 14:50:44 +02001872/* Parses the "disable frontend" directive, it always returns 1.
1873 *
1874 * Grabs the proxy lock.
1875 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001876static int cli_parse_disable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01001877{
1878 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02001879 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001880
1881 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1882 return 1;
1883
1884 px = cli_find_frontend(appctx, args[2]);
1885 if (!px)
1886 return 1;
1887
1888 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001889 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001890 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001891 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001892 return 1;
1893 }
1894
1895 if (px->state == PR_STPAUSED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001896 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001897 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001898 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001899 return 1;
1900 }
1901
Willy Tarreaua275a372018-08-21 14:50:44 +02001902 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1903 ret = pause_proxy(px);
1904 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1905
1906 if (!ret) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001907 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001908 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001909 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001910 return 1;
1911 }
1912 return 1;
1913}
1914
Willy Tarreaua275a372018-08-21 14:50:44 +02001915/* Parses the "enable frontend" directive, it always returns 1.
1916 *
1917 * Grabs the proxy lock.
1918 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001919static int cli_parse_enable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01001920{
1921 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02001922 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001923
1924 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1925 return 1;
1926
1927 px = cli_find_frontend(appctx, args[2]);
1928 if (!px)
1929 return 1;
1930
1931 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001932 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001933 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001934 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001935 return 1;
1936 }
1937
1938 if (px->state != PR_STPAUSED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001939 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001940 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001941 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001942 return 1;
1943 }
1944
Willy Tarreaua275a372018-08-21 14:50:44 +02001945 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1946 ret = resume_proxy(px);
1947 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1948
1949 if (!ret) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001950 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001951 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001952 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001953 return 1;
1954 }
1955 return 1;
1956}
1957
Willy Tarreauddb68ac2018-09-07 18:34:24 +02001958/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
1959 * now.
1960 */
1961static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
1962{
1963 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1964 return 1;
1965
1966 if (*args[2]) {
1967 struct proxy *px;
1968
1969 px = proxy_find_by_name(args[2], 0, 0);
1970 if (px)
1971 appctx->ctx.errors.iid = px->uuid;
1972 else
1973 appctx->ctx.errors.iid = atoi(args[2]);
1974
1975 if (!appctx->ctx.errors.iid) {
1976 appctx->ctx.cli.severity = LOG_ERR;
1977 appctx->ctx.cli.msg = "No such proxy.\n";
1978 appctx->st0 = CLI_ST_PRINT;
1979 return 1;
1980 }
1981 }
1982 else
1983 appctx->ctx.errors.iid = -1; // dump all proxies
1984
1985 appctx->ctx.errors.flag = 0;
1986 if (strcmp(args[3], "request") == 0)
1987 appctx->ctx.errors.flag |= 4; // ignore response
1988 else if (strcmp(args[3], "response") == 0)
1989 appctx->ctx.errors.flag |= 2; // ignore request
1990 appctx->ctx.errors.px = NULL;
1991 return 0;
1992}
1993
1994/* This function dumps all captured errors onto the stream interface's
1995 * read buffer. It returns 0 if the output buffer is full and it needs
1996 * to be called again, otherwise non-zero.
1997 */
1998static int cli_io_handler_show_errors(struct appctx *appctx)
1999{
2000 struct stream_interface *si = appctx->owner;
2001 extern const char *monthname[12];
2002
2003 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
2004 return 1;
2005
2006 chunk_reset(&trash);
2007
2008 if (!appctx->ctx.errors.px) {
2009 /* the function had not been called yet, let's prepare the
2010 * buffer for a response.
2011 */
2012 struct tm tm;
2013
2014 get_localtime(date.tv_sec, &tm);
2015 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
2016 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2017 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
2018 error_snapshot_id);
2019
Willy Tarreau36b27362018-09-07 19:55:44 +02002020 if (ci_putchk(si_ic(si), &trash) == -1)
2021 goto cant_send;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002022
2023 appctx->ctx.errors.px = proxies_list;
2024 appctx->ctx.errors.bol = 0;
2025 appctx->ctx.errors.ptr = -1;
2026 }
2027
2028 /* we have two inner loops here, one for the proxy, the other one for
2029 * the buffer.
2030 */
2031 while (appctx->ctx.errors.px) {
2032 struct error_snapshot *es;
2033
Willy Tarreau36b27362018-09-07 19:55:44 +02002034 HA_SPIN_LOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
2035
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002036 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002037 es = appctx->ctx.errors.px->invalid_req;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002038 if (appctx->ctx.errors.flag & 2) // skip req
2039 goto next;
2040 }
2041 else {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002042 es = appctx->ctx.errors.px->invalid_rep;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002043 if (appctx->ctx.errors.flag & 4) // skip resp
2044 goto next;
2045 }
2046
Willy Tarreauc55015e2018-09-07 19:02:32 +02002047 if (!es)
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002048 goto next;
2049
2050 if (appctx->ctx.errors.iid >= 0 &&
2051 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
2052 es->oe->uuid != appctx->ctx.errors.iid)
2053 goto next;
2054
2055 if (appctx->ctx.errors.ptr < 0) {
2056 /* just print headers now */
2057
2058 char pn[INET6_ADDRSTRLEN];
2059 struct tm tm;
2060 int port;
2061
2062 get_localtime(es->when.tv_sec, &tm);
2063 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
2064 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2065 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
2066
2067 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
2068 case AF_INET:
2069 case AF_INET6:
2070 port = get_host_port(&es->src);
2071 break;
2072 default:
2073 port = 0;
2074 }
2075
2076 switch (appctx->ctx.errors.flag & 1) {
2077 case 0:
2078 chunk_appendf(&trash,
2079 " frontend %s (#%d): invalid request\n"
2080 " backend %s (#%d)",
2081 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
2082 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
2083 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
2084 break;
2085 case 1:
2086 chunk_appendf(&trash,
2087 " backend %s (#%d): invalid response\n"
2088 " frontend %s (#%d)",
2089 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
2090 es->oe->id, es->oe->uuid);
2091 break;
2092 }
2093
2094 chunk_appendf(&trash,
2095 ", server %s (#%d), event #%u, src %s:%d\n"
2096 " buffer starts at %llu (including %u out), %u free,\n"
2097 " len %u, wraps at %u, error at position %u\n",
2098 es->srv ? es->srv->id : "<NONE>",
2099 es->srv ? es->srv->puid : -1,
2100 es->ev_id, pn, port,
2101 es->buf_ofs, es->buf_out,
2102 global.tune.bufsize - es->buf_out - es->buf_len,
2103 es->buf_len, es->buf_wrap, es->buf_err);
2104
2105 if (es->show)
2106 es->show(&trash, es);
2107
2108 chunk_appendf(&trash, " \n");
2109
Willy Tarreau36b27362018-09-07 19:55:44 +02002110 if (ci_putchk(si_ic(si), &trash) == -1)
2111 goto cant_send_unlock;
2112
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002113 appctx->ctx.errors.ptr = 0;
2114 appctx->ctx.errors.ev_id = es->ev_id;
2115 }
2116
2117 if (appctx->ctx.errors.ev_id != es->ev_id) {
2118 /* the snapshot changed while we were dumping it */
2119 chunk_appendf(&trash,
2120 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau36b27362018-09-07 19:55:44 +02002121 if (ci_putchk(si_ic(si), &trash) == -1)
2122 goto cant_send_unlock;
2123
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002124 goto next;
2125 }
2126
2127 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau4bc7d902018-09-07 20:07:17 +02002128 while (appctx->ctx.errors.ptr < es->buf_len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002129 int newptr;
2130 int newline;
2131
2132 newline = appctx->ctx.errors.bol;
2133 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, appctx->ctx.errors.ptr);
2134 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau36b27362018-09-07 19:55:44 +02002135 goto cant_send_unlock;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002136
Willy Tarreau36b27362018-09-07 19:55:44 +02002137 if (ci_putchk(si_ic(si), &trash) == -1)
2138 goto cant_send_unlock;
2139
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002140 appctx->ctx.errors.ptr = newptr;
2141 appctx->ctx.errors.bol = newline;
2142 };
2143 next:
Willy Tarreau36b27362018-09-07 19:55:44 +02002144 HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002145 appctx->ctx.errors.bol = 0;
2146 appctx->ctx.errors.ptr = -1;
2147 appctx->ctx.errors.flag ^= 1;
2148 if (!(appctx->ctx.errors.flag & 1))
2149 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
2150 }
2151
2152 /* dump complete */
2153 return 1;
Willy Tarreau36b27362018-09-07 19:55:44 +02002154
2155 cant_send_unlock:
2156 HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
2157 cant_send:
Willy Tarreaudb398432018-11-15 11:08:52 +01002158 si_rx_room_blk(si);
Willy Tarreau36b27362018-09-07 19:55:44 +02002159 return 0;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002160}
2161
William Lallemanda6c5f332016-11-19 02:25:36 +01002162/* register cli keywords */
2163static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau15b9e682016-11-24 11:55:28 +01002164 { { "disable", "frontend", NULL }, "disable frontend : temporarily disable specific frontend", cli_parse_disable_frontend, NULL, NULL },
2165 { { "enable", "frontend", NULL }, "enable frontend : re-enable specific frontend", cli_parse_enable_frontend, NULL, NULL },
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002166 { { "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 +01002167 { { "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 +01002168 { { "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 +01002169 { { "shutdown", "frontend", NULL }, "shutdown frontend : stop a specific frontend", cli_parse_shutdown_frontend, NULL, NULL },
Olivier Houchard614f8d72017-03-14 20:08:46 +01002170 { { "set", "dynamic-cookie-key", "backend", NULL }, "set dynamic-cookie-key backend : change a backend secret key for dynamic cookies", cli_parse_set_dyncookie_key_backend, NULL },
2171 { { "enable", "dynamic-cookie", "backend", NULL }, "enable dynamic-cookie backend : enable dynamic cookies on a specific backend", cli_parse_enable_dyncookie_backend, NULL },
2172 { { "disable", "dynamic-cookie", "backend", NULL }, "disable dynamic-cookie backend : disable dynamic cookies on a specific backend", cli_parse_disable_dyncookie_backend, NULL },
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002173 { { "show", "errors", NULL }, "show errors : report last request and response errors for each proxy", cli_parse_show_errors, cli_io_handler_show_errors, NULL },
William Lallemanda6c5f332016-11-19 02:25:36 +01002174 {{},}
2175}};
2176
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02002177__attribute__((constructor))
2178static void __proxy_module_init(void)
2179{
2180 cfg_register_keywords(&cfg_kws);
William Lallemanda6c5f332016-11-19 02:25:36 +01002181 cli_register_kw(&cli_kws);
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02002182}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002183
2184/*
2185 * Local variables:
2186 * c-indent-level: 8
2187 * c-basic-offset: 8
2188 * End:
2189 */