blob: 9a6313a1c12ca2cf7893802592851db0283d1ff5 [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
1113 * end of start-up. When all listeners are closed, the proxy is set to the
1114 * PR_STSTOPPED state.
1115 */
1116void stop_proxy(struct proxy *p)
1117{
1118 struct listener *l;
1119
Willy Tarreau4348fad2012-09-20 16:48:07 +02001120 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauda250db2008-10-12 12:07:48 +02001121 unbind_listener(l);
1122 if (l->state >= LI_ASSIGNED) {
1123 delete_listener(l);
Willy Tarreauda250db2008-10-12 12:07:48 +02001124 }
1125 }
1126 p->state = PR_STSTOPPED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001127}
1128
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001129/* This function resumes listening on the specified proxy. It scans all of its
1130 * listeners and tries to enable them all. If any of them fails, the proxy is
1131 * put back to the paused state. It returns 1 upon success, or zero if an error
1132 * is encountered.
1133 */
1134int resume_proxy(struct proxy *p)
1135{
1136 struct listener *l;
1137 int fail;
1138
1139 if (p->state != PR_STPAUSED)
1140 return 1;
1141
Christopher Faulet767a84b2017-11-24 16:50:31 +01001142 ha_warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001143 send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
1144
1145 fail = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001146 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001147 if (!resume_listener(l)) {
1148 int port;
1149
1150 port = get_host_port(&l->addr);
1151 if (port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001152 ha_warning("Port %d busy while trying to enable %s %s.\n",
1153 port, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001154 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
1155 port, proxy_cap_str(p->cap), p->id);
1156 }
1157 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001158 ha_warning("Bind on socket %d busy while trying to enable %s %s.\n",
1159 l->luid, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001160 send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n",
1161 l->luid, proxy_cap_str(p->cap), p->id);
1162 }
1163
1164 /* Another port might have been enabled. Let's stop everything. */
1165 fail = 1;
1166 break;
1167 }
1168 }
1169
1170 p->state = PR_STREADY;
1171 if (fail) {
1172 pause_proxy(p);
1173 return 0;
1174 }
1175 return 1;
1176}
1177
Willy Tarreaubaaee002006-06-26 02:48:02 +02001178/*
1179 * This function temporarily disables listening so that another new instance
1180 * can start listening. It is designed to be called upon reception of a
1181 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
1182 * the proxy, or a SIGTTIN can be sent to listen again.
1183 */
1184void pause_proxies(void)
1185{
1186 int err;
1187 struct proxy *p;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001188 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001189
1190 err = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001191 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001192 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001193 while (p) {
Willy Tarreauce8fe252011-09-07 19:14:57 +02001194 err |= !pause_proxy(p);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001195 p = p->next;
1196 }
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001197
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001198 prs = cfg_peers;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001199 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001200 if (prs->peers_fe)
1201 err |= !pause_proxy(prs->peers_fe);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001202 prs = prs->next;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001203 }
1204
Willy Tarreaubaaee002006-06-26 02:48:02 +02001205 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001206 ha_warning("Some proxies refused to pause, performing soft stop now.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001207 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
1208 soft_stop();
1209 }
1210}
1211
1212
1213/*
1214 * This function reactivates listening. This can be used after a call to
1215 * sig_pause(), for example when a new instance has failed starting up.
1216 * It is designed to be called upon reception of a SIGTTIN.
1217 */
Willy Tarreaube58c382011-07-24 18:28:10 +02001218void resume_proxies(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001219{
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001220 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001221 struct proxy *p;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001222 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001223
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001224 err = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001225 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001226 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001227 while (p) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001228 err |= !resume_proxy(p);
1229 p = p->next;
1230 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001231
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001232 prs = cfg_peers;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001233 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001234 if (prs->peers_fe)
1235 err |= !resume_proxy(prs->peers_fe);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001236 prs = prs->next;
1237 }
Willy Tarreaube58c382011-07-24 18:28:10 +02001238
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001239 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001240 ha_warning("Some proxies refused to resume, a restart is probably needed to resume safe operations.\n");
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001241 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 +02001242 }
1243}
1244
Willy Tarreau87b09662015-04-03 00:22:06 +02001245/* Set current stream's backend to <be>. Nothing is done if the
1246 * stream already had a backend assigned, which is indicated by
Willy Tarreaue7dff022015-04-03 01:14:29 +02001247 * s->flags & SF_BE_ASSIGNED.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001248 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001249 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001250 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001251int stream_set_backend(struct stream *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001252{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001253 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001254 return 1;
Christopher Faulet41179042016-06-21 11:54:52 +02001255
1256 if (flt_set_stream_backend(s, be) < 0)
1257 return 0;
1258
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001259 s->be = be;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001260 HA_ATOMIC_UPDATE_MAX(&be->be_counters.conn_max,
1261 HA_ATOMIC_ADD(&be->beconn, 1));
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001262 proxy_inc_be_ctr(be);
1263
Willy Tarreau87b09662015-04-03 00:22:06 +02001264 /* assign new parameters to the stream from the new backend */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001265 s->si[1].flags &= ~SI_FL_INDEP_STR;
1266 if (be->options2 & PR_O2_INDEPSTR)
1267 s->si[1].flags |= SI_FL_INDEP_STR;
1268
Hongbo Longe39683c2017-03-10 18:41:51 +01001269 if (tick_isset(be->timeout.serverfin))
1270 s->si[1].hcto = be->timeout.serverfin;
1271
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001272 /* We want to enable the backend-specific analysers except those which
1273 * were already run as part of the frontend/listener. Note that it would
1274 * be more reliable to store the list of analysers that have been run,
1275 * but what we do here is OK for now.
1276 */
Christopher Faulet70e2f272017-01-09 16:33:19 +01001277 s->req.analysers |= be->be_req_ana & ~(strm_li(s) ? strm_li(s)->analysers : 0);
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001278
Willy Tarreau51aecc72009-07-12 09:47:04 +02001279 /* If the target backend requires HTTP processing, we have to allocate
Willy Tarreaueee5b512015-04-03 23:46:31 +02001280 * the HTTP transaction and hdr_idx if we did not have one.
Willy Tarreau51aecc72009-07-12 09:47:04 +02001281 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001282 if (unlikely(!s->txn && be->http_needed)) {
1283 if (unlikely(!http_alloc_txn(s)))
Willy Tarreau51aecc72009-07-12 09:47:04 +02001284 return 0; /* not enough memory */
Willy Tarreau39e4f622010-05-31 17:01:36 +02001285
1286 /* and now initialize the HTTP transaction state */
1287 http_init_txn(s);
Willy Tarreau51aecc72009-07-12 09:47:04 +02001288 }
1289
Christopher Faulet309c6412015-12-02 09:57:32 +01001290 /* Be sure to filter request headers if the backend is an HTTP proxy and
1291 * if there are filters attached to the stream. */
1292 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01001293 s->req.analysers |= AN_REQ_FLT_HTTP_HDRS;
Christopher Faulet309c6412015-12-02 09:57:32 +01001294
Willy Tarreaueee5b512015-04-03 23:46:31 +02001295 if (s->txn) {
1296 if (be->options2 & PR_O2_RSPBUG_OK)
1297 s->txn->rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001298
Willy Tarreaueee5b512015-04-03 23:46:31 +02001299 /* If we chain to an HTTP backend running a different HTTP mode, we
1300 * have to re-adjust the desired keep-alive/close mode to accommodate
1301 * both the frontend's and the backend's modes.
1302 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001303 if (strm_fe(s)->mode == PR_MODE_HTTP && be->mode == PR_MODE_HTTP &&
1304 ((strm_fe(s)->options & PR_O_HTTP_MODE) != (be->options & PR_O_HTTP_MODE)))
Willy Tarreaueee5b512015-04-03 23:46:31 +02001305 http_adjust_conn_mode(s, s->txn, &s->txn->req);
Willy Tarreau80a92c02014-03-12 10:41:13 +01001306
Willy Tarreaueee5b512015-04-03 23:46:31 +02001307 /* If an LB algorithm needs to access some pre-parsed body contents,
1308 * we must not start to forward anything until the connection is
1309 * confirmed otherwise we'll lose the pointer to these data and
1310 * prevent the hash from being doable again after a redispatch.
1311 */
1312 if (be->mode == PR_MODE_HTTP &&
1313 (be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_HI | BE_LB_HASH_PRM))
1314 s->txn->req.flags |= HTTP_MSGF_WAIT_CONN;
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001315
1316 /* we may request to parse a request body */
1317 if ((be->options & PR_O_WREQ_BODY) &&
1318 (s->txn->req.body_len || (s->txn->req.flags & HTTP_MSGF_TE_CHNK)))
1319 s->req.analysers |= AN_REQ_HTTP_BODY;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001320 }
1321
1322 s->flags |= SF_BE_ASSIGNED;
Willy Tarreau96e31212011-05-30 18:10:30 +02001323 if (be->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001324 s->req.flags |= CF_NEVER_WAIT;
1325 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02001326 }
1327
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001328 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001329}
1330
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001331/* Capture a bad request or response and archive it in the proxy's structure.
1332 * It is relatively protocol-agnostic so it requires that a number of elements
1333 * are passed :
1334 * - <proxy> is the proxy where the error was detected and where the snapshot
1335 * needs to be stored
1336 * - <is_back> indicates that the error happend when receiving the response
1337 * - <other_end> is a pointer to the proxy on the other side when known
1338 * - <target> is the target of the connection, usually a server or a proxy
1339 * - <sess> is the session which experienced the error
1340 * - <ctx> may be NULL or should contain any info relevant to the protocol
1341 * - <buf> is the buffer containing the offending data
1342 * - <buf_ofs> is the position of this buffer's input data in the input
1343 * stream, starting at zero. It may be passed as zero if unknown.
1344 * - <buf_out> is the portion of <buf->data> which was already forwarded and
1345 * which precedes the buffer's input. The buffer's input starts at
1346 * buf->head + buf_out.
1347 * - <err_pos> is the pointer to the faulty byte in the buffer's input.
1348 * - <show> is the callback to use to display <ctx>. It may be NULL.
1349 */
1350void proxy_capture_error(struct proxy *proxy, int is_back,
1351 struct proxy *other_end, enum obj_type *target,
1352 const struct session *sess,
1353 const struct buffer *buf, long buf_ofs,
1354 unsigned int buf_out, unsigned int err_pos,
1355 const union error_snapshot_ctx *ctx,
1356 void (*show)(struct buffer *, const struct error_snapshot *))
1357{
1358 struct error_snapshot *es;
1359 unsigned int buf_len;
1360 int len1, len2;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001361 unsigned int ev_id;
1362
1363 ev_id = HA_ATOMIC_XADD(&error_snapshot_id, 1);
1364
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001365 buf_len = b_data(buf) - buf_out;
1366
1367 es = malloc(sizeof(*es) + buf_len);
Willy Tarreauc55015e2018-09-07 19:02:32 +02001368 if (!es)
1369 return;
1370
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001371 es->buf_len = buf_len;
1372 es->ev_id = ev_id;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001373
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001374 len1 = b_size(buf) - buf_len;
1375 if (len1 > buf_len)
1376 len1 = buf_len;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001377
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001378 if (len1) {
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001379 memcpy(es->buf, b_peek(buf, buf_out), len1);
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001380 len2 = buf_len - len1;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001381 if (len2)
1382 memcpy(es->buf + len1, b_orig(buf), len2);
1383 }
1384
1385 es->buf_err = err_pos;
1386 es->when = date; // user-visible date
1387 es->srv = objt_server(target);
1388 es->oe = other_end;
1389 if (objt_conn(sess->origin))
1390 es->src = __objt_conn(sess->origin)->addr.from;
1391 else
1392 memset(&es->src, 0, sizeof(es->src));
1393
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001394 es->buf_wrap = b_wrap(buf) - b_peek(buf, buf_out);
1395 es->buf_out = buf_out;
1396 es->buf_ofs = buf_ofs;
1397
1398 /* be sure to indicate the offset of the first IN byte */
1399 if (es->buf_ofs >= es->buf_len)
1400 es->buf_ofs -= es->buf_len;
1401 else
1402 es->buf_ofs = 0;
1403
1404 /* protocol-specific part now */
1405 if (ctx)
1406 es->ctx = *ctx;
1407 else
1408 memset(&es->ctx, 0, sizeof(es->ctx));
1409 es->show = show;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001410
1411 /* note: we still lock since we have to be certain that nobody is
1412 * dumping the output while we free.
1413 */
1414 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
1415 if (is_back) {
1416 es = HA_ATOMIC_XCHG(&proxy->invalid_rep, es);
1417 } else {
1418 es = HA_ATOMIC_XCHG(&proxy->invalid_req, es);
1419 }
1420 free(es);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001421 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
1422}
1423
1424/* Config keywords below */
1425
Willy Tarreaudc13c112013-06-21 23:16:39 +02001426static struct cfg_kw_list cfg_kws = {ILH, {
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001427 { CFG_GLOBAL, "hard-stop-after", proxy_parse_hard_stop_after },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001428 { CFG_LISTEN, "timeout", proxy_parse_timeout },
1429 { CFG_LISTEN, "clitimeout", proxy_parse_timeout },
1430 { CFG_LISTEN, "contimeout", proxy_parse_timeout },
1431 { CFG_LISTEN, "srvtimeout", proxy_parse_timeout },
Willy Tarreau3a7d2072009-03-05 23:48:25 +01001432 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreauc35362a2014-04-25 13:58:37 +02001433 { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue },
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +02001434 { CFG_LISTEN, "declare", proxy_parse_declare },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001435 { 0, NULL, NULL },
1436}};
1437
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001438/* Expects to find a frontend named <arg> and returns it, otherwise displays various
1439 * adequate error messages and returns NULL. This function is designed to be used by
1440 * functions requiring a frontend on the CLI.
1441 */
1442struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg)
1443{
1444 struct proxy *px;
1445
1446 if (!*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001447 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001448 appctx->ctx.cli.msg = "A frontend name is expected.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001449 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001450 return NULL;
1451 }
1452
1453 px = proxy_fe_by_name(arg);
1454 if (!px) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001455 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001456 appctx->ctx.cli.msg = "No such frontend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001457 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001458 return NULL;
1459 }
1460 return px;
1461}
1462
Olivier Houchard614f8d72017-03-14 20:08:46 +01001463/* Expects to find a backend named <arg> and returns it, otherwise displays various
1464 * adequate error messages and returns NULL. This function is designed to be used by
1465 * functions requiring a frontend on the CLI.
1466 */
1467struct proxy *cli_find_backend(struct appctx *appctx, const char *arg)
1468{
1469 struct proxy *px;
1470
1471 if (!*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001472 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001473 appctx->ctx.cli.msg = "A backend name is expected.\n";
1474 appctx->st0 = CLI_ST_PRINT;
1475 return NULL;
1476 }
1477
1478 px = proxy_be_by_name(arg);
1479 if (!px) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001480 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001481 appctx->ctx.cli.msg = "No such backend.\n";
1482 appctx->st0 = CLI_ST_PRINT;
1483 return NULL;
1484 }
1485 return px;
1486}
1487
1488
William Lallemanda6c5f332016-11-19 02:25:36 +01001489/* parse a "show servers" CLI line, returns 0 if it wants to start the dump or
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001490 * 1 if it stops immediately. If an argument is specified, it will set the proxy
1491 * pointer into cli.p0 and its ID into cli.i0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001492 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001493static int cli_parse_show_servers(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemanda6c5f332016-11-19 02:25:36 +01001494{
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001495 struct proxy *px;
William Lallemanda6c5f332016-11-19 02:25:36 +01001496
1497 /* check if a backend name has been provided */
1498 if (*args[3]) {
1499 /* read server state from local file */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001500 px = proxy_be_by_name(args[3]);
William Lallemanda6c5f332016-11-19 02:25:36 +01001501
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001502 if (!px) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001503 appctx->ctx.cli.severity = LOG_ERR;
William Lallemanda6c5f332016-11-19 02:25:36 +01001504 appctx->ctx.cli.msg = "Can't find backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001505 appctx->st0 = CLI_ST_PRINT;
William Lallemanda6c5f332016-11-19 02:25:36 +01001506 return 1;
1507 }
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001508 appctx->ctx.cli.p0 = px;
1509 appctx->ctx.cli.i0 = px->uuid;
William Lallemanda6c5f332016-11-19 02:25:36 +01001510 }
1511 return 0;
1512}
1513
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001514/* dumps server state information into <buf> for all the servers found in backend cli.p0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001515 * These information are all the parameters which may change during HAProxy runtime.
1516 * By default, we only export to the last known server state file format.
1517 * These information can be used at next startup to recover same level of server state.
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001518 * It uses the proxy pointer from cli.p0, the proxy's id from cli.i0 and the server's
1519 * pointer from cli.p1.
William Lallemanda6c5f332016-11-19 02:25:36 +01001520 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001521static int dump_servers_state(struct stream_interface *si, struct buffer *buf)
William Lallemanda6c5f332016-11-19 02:25:36 +01001522{
1523 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001524 struct proxy *px = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001525 struct server *srv;
1526 char srv_addr[INET6_ADDRSTRLEN + 1];
1527 time_t srv_time_since_last_change;
1528 int bk_f_forced_id, srv_f_forced_id;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001529 char *srvrecord;
William Lallemanda6c5f332016-11-19 02:25:36 +01001530
William Lallemanda6c5f332016-11-19 02:25:36 +01001531 /* we don't want to report any state if the backend is not enabled on this process */
Willy Tarreau387bd4f2017-11-10 19:08:14 +01001532 if (px->bind_proc && !(px->bind_proc & pid_bit))
William Lallemanda6c5f332016-11-19 02:25:36 +01001533 return 1;
1534
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001535 if (!appctx->ctx.cli.p1)
1536 appctx->ctx.cli.p1 = px->srv;
William Lallemanda6c5f332016-11-19 02:25:36 +01001537
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001538 for (; appctx->ctx.cli.p1 != NULL; appctx->ctx.cli.p1 = srv->next) {
1539 srv = appctx->ctx.cli.p1;
William Lallemanda6c5f332016-11-19 02:25:36 +01001540 srv_addr[0] = '\0';
1541
1542 switch (srv->addr.ss_family) {
1543 case AF_INET:
1544 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
1545 srv_addr, INET_ADDRSTRLEN + 1);
1546 break;
1547 case AF_INET6:
1548 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
1549 srv_addr, INET6_ADDRSTRLEN + 1);
1550 break;
Daniel Corbett9215ffa2018-05-19 19:43:24 -04001551 default:
1552 memcpy(srv_addr, "-\0", 2);
1553 break;
William Lallemanda6c5f332016-11-19 02:25:36 +01001554 }
1555 srv_time_since_last_change = now.tv_sec - srv->last_change;
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001556 bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001557 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
1558
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001559 srvrecord = NULL;
1560 if (srv->srvrq && srv->srvrq->name)
1561 srvrecord = srv->srvrq->name;
1562
William Lallemanda6c5f332016-11-19 02:25:36 +01001563 chunk_appendf(buf,
1564 "%d %s "
1565 "%d %s %s "
1566 "%d %d %d %d %ld "
1567 "%d %d %d %d %d "
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001568 "%d %d %s %u %s"
William Lallemanda6c5f332016-11-19 02:25:36 +01001569 "\n",
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001570 px->uuid, px->id,
William Lallemanda6c5f332016-11-19 02:25:36 +01001571 srv->puid, srv->id, srv_addr,
Emeric Brun52a91d32017-08-31 14:41:55 +02001572 srv->cur_state, srv->cur_admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
William Lallemanda6c5f332016-11-19 02:25:36 +01001573 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001574 bk_f_forced_id, srv_f_forced_id, srv->hostname ? srv->hostname : "-", srv->svc_port,
1575 srvrecord ? srvrecord : "-");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001576 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemanda6c5f332016-11-19 02:25:36 +01001577 si_applet_cant_put(si);
1578 return 0;
1579 }
1580 }
1581 return 1;
1582}
1583
1584/* Parses backend list or simply use backend name provided by the user to return
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001585 * states of servers to stdout. It dumps proxy <cli.p0> and stops if <cli.i0> is
1586 * non-null.
William Lallemanda6c5f332016-11-19 02:25:36 +01001587 */
1588static int cli_io_handler_servers_state(struct appctx *appctx)
1589{
1590 struct stream_interface *si = appctx->owner;
William Lallemanda6c5f332016-11-19 02:25:36 +01001591 struct proxy *curproxy;
1592
1593 chunk_reset(&trash);
1594
1595 if (appctx->st2 == STAT_ST_INIT) {
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001596 if (!appctx->ctx.cli.p0)
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001597 appctx->ctx.cli.p0 = proxies_list;
William Lallemanda6c5f332016-11-19 02:25:36 +01001598 appctx->st2 = STAT_ST_HEAD;
1599 }
1600
1601 if (appctx->st2 == STAT_ST_HEAD) {
1602 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001603 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemanda6c5f332016-11-19 02:25:36 +01001604 si_applet_cant_put(si);
1605 return 0;
1606 }
1607 appctx->st2 = STAT_ST_INFO;
1608 }
1609
1610 /* STAT_ST_INFO */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001611 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1612 curproxy = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001613 /* servers are only in backends */
1614 if (curproxy->cap & PR_CAP_BE) {
1615 if (!dump_servers_state(si, &trash))
1616 return 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001617 }
1618 /* only the selected proxy is dumped */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001619 if (appctx->ctx.cli.i0)
William Lallemanda6c5f332016-11-19 02:25:36 +01001620 break;
1621 }
1622
1623 return 1;
1624}
1625
Willy Tarreau608ea592016-12-16 18:01:15 +01001626/* Parses backend list and simply report backend names. It keeps the proxy
1627 * pointer in cli.p0.
1628 */
William Lallemand933efcd2016-11-22 12:34:16 +01001629static int cli_io_handler_show_backend(struct appctx *appctx)
1630{
William Lallemand933efcd2016-11-22 12:34:16 +01001631 struct stream_interface *si = appctx->owner;
1632 struct proxy *curproxy;
1633
1634 chunk_reset(&trash);
1635
Willy Tarreau608ea592016-12-16 18:01:15 +01001636 if (!appctx->ctx.cli.p0) {
William Lallemand933efcd2016-11-22 12:34:16 +01001637 chunk_printf(&trash, "# name\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001638 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand933efcd2016-11-22 12:34:16 +01001639 si_applet_cant_put(si);
1640 return 0;
1641 }
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001642 appctx->ctx.cli.p0 = proxies_list;
William Lallemand933efcd2016-11-22 12:34:16 +01001643 }
1644
Willy Tarreau608ea592016-12-16 18:01:15 +01001645 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1646 curproxy = appctx->ctx.cli.p0;
William Lallemand933efcd2016-11-22 12:34:16 +01001647
1648 /* looking for backends only */
1649 if (!(curproxy->cap & PR_CAP_BE))
1650 continue;
1651
1652 /* we don't want to list a backend which is bound to this process */
Willy Tarreau387bd4f2017-11-10 19:08:14 +01001653 if (curproxy->bind_proc && !(curproxy->bind_proc & pid_bit))
William Lallemand933efcd2016-11-22 12:34:16 +01001654 continue;
1655
1656 chunk_appendf(&trash, "%s\n", curproxy->id);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001657 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand933efcd2016-11-22 12:34:16 +01001658 si_applet_cant_put(si);
1659 return 0;
1660 }
1661 }
1662
1663 return 1;
1664}
William Lallemanda6c5f332016-11-19 02:25:36 +01001665
Willy Tarreaua275a372018-08-21 14:50:44 +02001666/* Parses the "enable dynamic-cookies backend" directive, it always returns 1.
1667 *
1668 * Grabs the proxy lock and each server's lock.
1669 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001670static int cli_parse_enable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001671{
1672 struct proxy *px;
1673 struct server *s;
1674
1675 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1676 return 1;
1677
1678 px = cli_find_backend(appctx, args[3]);
1679 if (!px)
1680 return 1;
1681
Willy Tarreaua275a372018-08-21 14:50:44 +02001682 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1683
Olivier Houchard614f8d72017-03-14 20:08:46 +01001684 px->ck_opts |= PR_CK_DYNAMIC;
1685
Willy Tarreaua275a372018-08-21 14:50:44 +02001686 for (s = px->srv; s != NULL; s = s->next) {
1687 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001688 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02001689 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
1690 }
1691
1692 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001693
1694 return 1;
1695}
1696
Willy Tarreaua275a372018-08-21 14:50:44 +02001697/* Parses the "disable dynamic-cookies backend" directive, it always returns 1.
1698 *
1699 * Grabs the proxy lock and each server's lock.
1700 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001701static int cli_parse_disable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001702{
1703 struct proxy *px;
1704 struct server *s;
1705
1706 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1707 return 1;
1708
1709 px = cli_find_backend(appctx, args[3]);
1710 if (!px)
1711 return 1;
1712
Willy Tarreaua275a372018-08-21 14:50:44 +02001713 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1714
Olivier Houchard614f8d72017-03-14 20:08:46 +01001715 px->ck_opts &= ~PR_CK_DYNAMIC;
1716
1717 for (s = px->srv; s != NULL; s = s->next) {
Willy Tarreaua275a372018-08-21 14:50:44 +02001718 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001719 if (!(s->flags & SRV_F_COOKIESET)) {
1720 free(s->cookie);
1721 s->cookie = NULL;
1722 }
Willy Tarreaua275a372018-08-21 14:50:44 +02001723 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001724 }
1725
Willy Tarreaua275a372018-08-21 14:50:44 +02001726 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1727
Olivier Houchard614f8d72017-03-14 20:08:46 +01001728 return 1;
1729}
1730
Willy Tarreaua275a372018-08-21 14:50:44 +02001731/* Parses the "set dynamic-cookie-key backend" directive, it always returns 1.
1732 *
1733 * Grabs the proxy lock and each server's lock.
1734 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001735static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001736{
1737 struct proxy *px;
1738 struct server *s;
1739 char *newkey;
1740
1741 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1742 return 1;
1743
1744 px = cli_find_backend(appctx, args[3]);
1745 if (!px)
1746 return 1;
1747
1748 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001749 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001750 appctx->ctx.cli.msg = "String value expected.\n";
1751 appctx->st0 = CLI_ST_PRINT;
1752 return 1;
1753 }
1754
1755 newkey = strdup(args[4]);
1756 if (!newkey) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001757 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001758 appctx->ctx.cli.msg = "Failed to allocate memory.\n";
1759 appctx->st0 = CLI_ST_PRINT;
1760 return 1;
1761 }
Willy Tarreaua275a372018-08-21 14:50:44 +02001762
1763 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1764
Olivier Houchard614f8d72017-03-14 20:08:46 +01001765 free(px->dyncookie_key);
1766 px->dyncookie_key = newkey;
1767
Willy Tarreaua275a372018-08-21 14:50:44 +02001768 for (s = px->srv; s != NULL; s = s->next) {
1769 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001770 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02001771 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
1772 }
1773
1774 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001775
1776 return 1;
1777}
1778
Willy Tarreaua275a372018-08-21 14:50:44 +02001779/* Parses the "set maxconn frontend" directive, it always returns 1.
1780 *
1781 * Grabs the proxy lock.
1782 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001783static int cli_parse_set_maxconn_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001784{
1785 struct proxy *px;
1786 struct listener *l;
1787 int v;
1788
1789 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1790 return 1;
1791
1792 px = cli_find_frontend(appctx, args[3]);
1793 if (!px)
1794 return 1;
1795
1796 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001797 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001798 appctx->ctx.cli.msg = "Integer value expected.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001799 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001800 return 1;
1801 }
1802
1803 v = atoi(args[4]);
1804 if (v < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001805 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001806 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001807 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001808 return 1;
1809 }
1810
1811 /* OK, the value is fine, so we assign it to the proxy and to all of
1812 * its listeners. The blocked ones will be dequeued.
1813 */
Willy Tarreaua275a372018-08-21 14:50:44 +02001814 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1815
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001816 px->maxconn = v;
1817 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1818 l->maxconn = v;
1819 if (l->state == LI_FULL)
1820 resume_listener(l);
1821 }
1822
1823 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&px->listener_queue))
1824 dequeue_all_listeners(&px->listener_queue);
1825
Willy Tarreaua275a372018-08-21 14:50:44 +02001826 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1827
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001828 return 1;
1829}
1830
Willy Tarreaua275a372018-08-21 14:50:44 +02001831/* Parses the "shutdown frontend" directive, it always returns 1.
1832 *
1833 * Grabs the proxy lock.
1834 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001835static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001836{
1837 struct proxy *px;
1838
1839 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1840 return 1;
1841
1842 px = cli_find_frontend(appctx, args[2]);
1843 if (!px)
1844 return 1;
1845
1846 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001847 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001848 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001849 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001850 return 1;
1851 }
1852
Christopher Faulet767a84b2017-11-24 16:50:31 +01001853 ha_warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1854 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001855 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1856 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
Willy Tarreaua275a372018-08-21 14:50:44 +02001857
1858 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001859 stop_proxy(px);
Willy Tarreaua275a372018-08-21 14:50:44 +02001860 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1861
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001862 return 1;
1863}
1864
Willy Tarreaua275a372018-08-21 14:50:44 +02001865/* Parses the "disable frontend" directive, it always returns 1.
1866 *
1867 * Grabs the proxy lock.
1868 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001869static int cli_parse_disable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01001870{
1871 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02001872 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001873
1874 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1875 return 1;
1876
1877 px = cli_find_frontend(appctx, args[2]);
1878 if (!px)
1879 return 1;
1880
1881 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001882 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001883 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001884 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001885 return 1;
1886 }
1887
1888 if (px->state == PR_STPAUSED) {
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 is already disabled.\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
Willy Tarreaua275a372018-08-21 14:50:44 +02001895 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1896 ret = pause_proxy(px);
1897 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1898
1899 if (!ret) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001900 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001901 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001902 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001903 return 1;
1904 }
1905 return 1;
1906}
1907
Willy Tarreaua275a372018-08-21 14:50:44 +02001908/* Parses the "enable frontend" directive, it always returns 1.
1909 *
1910 * Grabs the proxy lock.
1911 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001912static int cli_parse_enable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01001913{
1914 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02001915 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001916
1917 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1918 return 1;
1919
1920 px = cli_find_frontend(appctx, args[2]);
1921 if (!px)
1922 return 1;
1923
1924 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001925 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001926 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001927 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001928 return 1;
1929 }
1930
1931 if (px->state != PR_STPAUSED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001932 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001933 appctx->ctx.cli.msg = "Frontend is already enabled.\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
Willy Tarreaua275a372018-08-21 14:50:44 +02001938 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1939 ret = resume_proxy(px);
1940 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1941
1942 if (!ret) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001943 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001944 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001945 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001946 return 1;
1947 }
1948 return 1;
1949}
1950
Willy Tarreauddb68ac2018-09-07 18:34:24 +02001951/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
1952 * now.
1953 */
1954static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
1955{
1956 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1957 return 1;
1958
1959 if (*args[2]) {
1960 struct proxy *px;
1961
1962 px = proxy_find_by_name(args[2], 0, 0);
1963 if (px)
1964 appctx->ctx.errors.iid = px->uuid;
1965 else
1966 appctx->ctx.errors.iid = atoi(args[2]);
1967
1968 if (!appctx->ctx.errors.iid) {
1969 appctx->ctx.cli.severity = LOG_ERR;
1970 appctx->ctx.cli.msg = "No such proxy.\n";
1971 appctx->st0 = CLI_ST_PRINT;
1972 return 1;
1973 }
1974 }
1975 else
1976 appctx->ctx.errors.iid = -1; // dump all proxies
1977
1978 appctx->ctx.errors.flag = 0;
1979 if (strcmp(args[3], "request") == 0)
1980 appctx->ctx.errors.flag |= 4; // ignore response
1981 else if (strcmp(args[3], "response") == 0)
1982 appctx->ctx.errors.flag |= 2; // ignore request
1983 appctx->ctx.errors.px = NULL;
1984 return 0;
1985}
1986
1987/* This function dumps all captured errors onto the stream interface's
1988 * read buffer. It returns 0 if the output buffer is full and it needs
1989 * to be called again, otherwise non-zero.
1990 */
1991static int cli_io_handler_show_errors(struct appctx *appctx)
1992{
1993 struct stream_interface *si = appctx->owner;
1994 extern const char *monthname[12];
1995
1996 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1997 return 1;
1998
1999 chunk_reset(&trash);
2000
2001 if (!appctx->ctx.errors.px) {
2002 /* the function had not been called yet, let's prepare the
2003 * buffer for a response.
2004 */
2005 struct tm tm;
2006
2007 get_localtime(date.tv_sec, &tm);
2008 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
2009 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2010 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
2011 error_snapshot_id);
2012
Willy Tarreau36b27362018-09-07 19:55:44 +02002013 if (ci_putchk(si_ic(si), &trash) == -1)
2014 goto cant_send;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002015
2016 appctx->ctx.errors.px = proxies_list;
2017 appctx->ctx.errors.bol = 0;
2018 appctx->ctx.errors.ptr = -1;
2019 }
2020
2021 /* we have two inner loops here, one for the proxy, the other one for
2022 * the buffer.
2023 */
2024 while (appctx->ctx.errors.px) {
2025 struct error_snapshot *es;
2026
Willy Tarreau36b27362018-09-07 19:55:44 +02002027 HA_SPIN_LOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
2028
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002029 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002030 es = appctx->ctx.errors.px->invalid_req;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002031 if (appctx->ctx.errors.flag & 2) // skip req
2032 goto next;
2033 }
2034 else {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002035 es = appctx->ctx.errors.px->invalid_rep;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002036 if (appctx->ctx.errors.flag & 4) // skip resp
2037 goto next;
2038 }
2039
Willy Tarreauc55015e2018-09-07 19:02:32 +02002040 if (!es)
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002041 goto next;
2042
2043 if (appctx->ctx.errors.iid >= 0 &&
2044 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
2045 es->oe->uuid != appctx->ctx.errors.iid)
2046 goto next;
2047
2048 if (appctx->ctx.errors.ptr < 0) {
2049 /* just print headers now */
2050
2051 char pn[INET6_ADDRSTRLEN];
2052 struct tm tm;
2053 int port;
2054
2055 get_localtime(es->when.tv_sec, &tm);
2056 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
2057 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2058 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
2059
2060 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
2061 case AF_INET:
2062 case AF_INET6:
2063 port = get_host_port(&es->src);
2064 break;
2065 default:
2066 port = 0;
2067 }
2068
2069 switch (appctx->ctx.errors.flag & 1) {
2070 case 0:
2071 chunk_appendf(&trash,
2072 " frontend %s (#%d): invalid request\n"
2073 " backend %s (#%d)",
2074 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
2075 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
2076 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
2077 break;
2078 case 1:
2079 chunk_appendf(&trash,
2080 " backend %s (#%d): invalid response\n"
2081 " frontend %s (#%d)",
2082 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
2083 es->oe->id, es->oe->uuid);
2084 break;
2085 }
2086
2087 chunk_appendf(&trash,
2088 ", server %s (#%d), event #%u, src %s:%d\n"
2089 " buffer starts at %llu (including %u out), %u free,\n"
2090 " len %u, wraps at %u, error at position %u\n",
2091 es->srv ? es->srv->id : "<NONE>",
2092 es->srv ? es->srv->puid : -1,
2093 es->ev_id, pn, port,
2094 es->buf_ofs, es->buf_out,
2095 global.tune.bufsize - es->buf_out - es->buf_len,
2096 es->buf_len, es->buf_wrap, es->buf_err);
2097
2098 if (es->show)
2099 es->show(&trash, es);
2100
2101 chunk_appendf(&trash, " \n");
2102
Willy Tarreau36b27362018-09-07 19:55:44 +02002103 if (ci_putchk(si_ic(si), &trash) == -1)
2104 goto cant_send_unlock;
2105
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002106 appctx->ctx.errors.ptr = 0;
2107 appctx->ctx.errors.ev_id = es->ev_id;
2108 }
2109
2110 if (appctx->ctx.errors.ev_id != es->ev_id) {
2111 /* the snapshot changed while we were dumping it */
2112 chunk_appendf(&trash,
2113 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau36b27362018-09-07 19:55:44 +02002114 if (ci_putchk(si_ic(si), &trash) == -1)
2115 goto cant_send_unlock;
2116
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002117 goto next;
2118 }
2119
2120 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau4bc7d902018-09-07 20:07:17 +02002121 while (appctx->ctx.errors.ptr < es->buf_len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002122 int newptr;
2123 int newline;
2124
2125 newline = appctx->ctx.errors.bol;
2126 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, appctx->ctx.errors.ptr);
2127 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau36b27362018-09-07 19:55:44 +02002128 goto cant_send_unlock;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002129
Willy Tarreau36b27362018-09-07 19:55:44 +02002130 if (ci_putchk(si_ic(si), &trash) == -1)
2131 goto cant_send_unlock;
2132
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002133 appctx->ctx.errors.ptr = newptr;
2134 appctx->ctx.errors.bol = newline;
2135 };
2136 next:
Willy Tarreau36b27362018-09-07 19:55:44 +02002137 HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002138 appctx->ctx.errors.bol = 0;
2139 appctx->ctx.errors.ptr = -1;
2140 appctx->ctx.errors.flag ^= 1;
2141 if (!(appctx->ctx.errors.flag & 1))
2142 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
2143 }
2144
2145 /* dump complete */
2146 return 1;
Willy Tarreau36b27362018-09-07 19:55:44 +02002147
2148 cant_send_unlock:
2149 HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
2150 cant_send:
2151 si_applet_cant_put(si);
2152 return 0;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002153}
2154
William Lallemanda6c5f332016-11-19 02:25:36 +01002155/* register cli keywords */
2156static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau15b9e682016-11-24 11:55:28 +01002157 { { "disable", "frontend", NULL }, "disable frontend : temporarily disable specific frontend", cli_parse_disable_frontend, NULL, NULL },
2158 { { "enable", "frontend", NULL }, "enable frontend : re-enable specific frontend", cli_parse_enable_frontend, NULL, NULL },
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002159 { { "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 +01002160 { { "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 +01002161 { { "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 +01002162 { { "shutdown", "frontend", NULL }, "shutdown frontend : stop a specific frontend", cli_parse_shutdown_frontend, NULL, NULL },
Olivier Houchard614f8d72017-03-14 20:08:46 +01002163 { { "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 },
2164 { { "enable", "dynamic-cookie", "backend", NULL }, "enable dynamic-cookie backend : enable dynamic cookies on a specific backend", cli_parse_enable_dyncookie_backend, NULL },
2165 { { "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 +02002166 { { "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 +01002167 {{},}
2168}};
2169
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02002170__attribute__((constructor))
2171static void __proxy_module_init(void)
2172{
2173 cfg_register_keywords(&cfg_kws);
William Lallemanda6c5f332016-11-19 02:25:36 +01002174 cli_register_kw(&cli_kws);
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02002175}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002176
2177/*
2178 * Local variables:
2179 * c-indent-level: 8
2180 * c-basic-offset: 8
2181 * End:
2182 */