blob: 76d84a24fd5a836f95f0cbdd7956fe62bfbf4df8 [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 Tarreau3a1f5fd2018-11-11 15:40:36 +010062/* proxy->options */
63const struct cfg_opt cfg_opts[] =
64{
65 { "abortonclose", PR_O_ABRT_CLOSE, PR_CAP_BE, 0, 0 },
66 { "allbackups", PR_O_USE_ALL_BK, PR_CAP_BE, 0, 0 },
67 { "checkcache", PR_O_CHK_CACHE, PR_CAP_BE, 0, PR_MODE_HTTP },
68 { "clitcpka", PR_O_TCP_CLI_KA, PR_CAP_FE, 0, 0 },
69 { "contstats", PR_O_CONTSTATS, PR_CAP_FE, 0, 0 },
70 { "dontlognull", PR_O_NULLNOLOG, PR_CAP_FE, 0, 0 },
71 { "http_proxy", PR_O_HTTP_PROXY, PR_CAP_FE | PR_CAP_BE, 0, PR_MODE_HTTP },
72 { "http-buffer-request", PR_O_WREQ_BODY, PR_CAP_FE | PR_CAP_BE, 0, PR_MODE_HTTP },
73 { "http-ignore-probes", PR_O_IGNORE_PRB, PR_CAP_FE, 0, PR_MODE_HTTP },
74 { "prefer-last-server", PR_O_PREF_LAST, PR_CAP_BE, 0, PR_MODE_HTTP },
75 { "logasap", PR_O_LOGASAP, PR_CAP_FE, 0, 0 },
76 { "nolinger", PR_O_TCP_NOLING, PR_CAP_FE | PR_CAP_BE, 0, 0 },
77 { "persist", PR_O_PERSIST, PR_CAP_BE, 0, 0 },
78 { "srvtcpka", PR_O_TCP_SRV_KA, PR_CAP_BE, 0, 0 },
79#ifdef TPROXY
80 { "transparent", PR_O_TRANSP, PR_CAP_BE, 0, 0 },
81#else
82 { "transparent", 0, 0, 0, 0 },
83#endif
84
85 { NULL, 0, 0, 0, 0 }
86};
87
88/* proxy->options2 */
89const struct cfg_opt cfg_opts2[] =
90{
91#ifdef CONFIG_HAP_LINUX_SPLICE
92 { "splice-request", PR_O2_SPLIC_REQ, PR_CAP_FE|PR_CAP_BE, 0, 0 },
93 { "splice-response", PR_O2_SPLIC_RTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
94 { "splice-auto", PR_O2_SPLIC_AUT, PR_CAP_FE|PR_CAP_BE, 0, 0 },
95#else
96 { "splice-request", 0, 0, 0, 0 },
97 { "splice-response", 0, 0, 0, 0 },
98 { "splice-auto", 0, 0, 0, 0 },
99#endif
100 { "accept-invalid-http-request", PR_O2_REQBUG_OK, PR_CAP_FE, 0, PR_MODE_HTTP },
101 { "accept-invalid-http-response", PR_O2_RSPBUG_OK, PR_CAP_BE, 0, PR_MODE_HTTP },
102 { "dontlog-normal", PR_O2_NOLOGNORM, PR_CAP_FE, 0, 0 },
103 { "log-separate-errors", PR_O2_LOGERRORS, PR_CAP_FE, 0, 0 },
104 { "log-health-checks", PR_O2_LOGHCHKS, PR_CAP_BE, 0, 0 },
105 { "socket-stats", PR_O2_SOCKSTAT, PR_CAP_FE, 0, 0 },
106 { "tcp-smart-accept", PR_O2_SMARTACC, PR_CAP_FE, 0, 0 },
107 { "tcp-smart-connect", PR_O2_SMARTCON, PR_CAP_BE, 0, 0 },
108 { "independant-streams", PR_O2_INDEPSTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
109 { "independent-streams", PR_O2_INDEPSTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
110 { "http-use-proxy-header", PR_O2_USE_PXHDR, PR_CAP_FE, 0, PR_MODE_HTTP },
111 { "http-pretend-keepalive", PR_O2_FAKE_KA, PR_CAP_BE, 0, PR_MODE_HTTP },
112 { "http-no-delay", PR_O2_NODELAY, PR_CAP_FE|PR_CAP_BE, 0, PR_MODE_HTTP },
113 { "http-use-htx", PR_O2_USE_HTX, PR_CAP_FE|PR_CAP_BE, 0, PR_MODE_HTTP },
114 { NULL, 0, 0, 0 }
115};
116
Willy Tarreau977b8e42006-12-29 14:19:17 +0100117/*
Willy Tarreau816eb542007-11-04 07:04:43 +0100118 * This function returns a string containing a name describing capabilities to
119 * report comprehensible error messages. Specifically, it will return the words
Christopher Faulet898566e2016-10-26 11:06:28 +0200120 * "frontend", "backend" when appropriate, or "proxy" for all other
Willy Tarreau816eb542007-11-04 07:04:43 +0100121 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +0100122 */
Willy Tarreau816eb542007-11-04 07:04:43 +0100123const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +0100124{
Willy Tarreau816eb542007-11-04 07:04:43 +0100125 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
126 if (cap & PR_CAP_FE)
127 return "frontend";
128 else if (cap & PR_CAP_BE)
129 return "backend";
Willy Tarreau816eb542007-11-04 07:04:43 +0100130 }
131 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +0100132}
133
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100134/*
135 * This function returns a string containing the mode of the proxy in a format
136 * suitable for error messages.
137 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100138const char *proxy_mode_str(int mode) {
139
140 if (mode == PR_MODE_TCP)
141 return "tcp";
142 else if (mode == PR_MODE_HTTP)
143 return "http";
144 else if (mode == PR_MODE_HEALTH)
145 return "health";
William Lallemandcf62f7e2018-10-26 14:47:40 +0200146 else if (mode == PR_MODE_CLI)
147 return "cli";
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100148 else
149 return "unknown";
150}
151
Willy Tarreauf3950172009-10-10 18:35:51 +0200152/*
153 * This function scans the list of backends and servers to retrieve the first
154 * backend and the first server with the given names, and sets them in both
155 * parameters. It returns zero if either is not found, or non-zero and sets
156 * the ones it did not found to NULL. If a NULL pointer is passed for the
157 * backend, only the pointer to the server will be updated.
158 */
159int get_backend_server(const char *bk_name, const char *sv_name,
160 struct proxy **bk, struct server **sv)
161{
162 struct proxy *p;
163 struct server *s;
Willy Tarreau7ecc4202014-03-15 07:57:11 +0100164 int sid;
Willy Tarreauf3950172009-10-10 18:35:51 +0200165
166 *sv = NULL;
167
Willy Tarreau050536d2012-10-04 08:47:34 +0200168 sid = -1;
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200169 if (*sv_name == '#')
170 sid = atoi(sv_name + 1);
171
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200172 p = proxy_be_by_name(bk_name);
Willy Tarreauf3950172009-10-10 18:35:51 +0200173 if (bk)
174 *bk = p;
175 if (!p)
176 return 0;
177
178 for (s = p->srv; s; s = s->next)
Willy Tarreau4055a102012-11-15 00:15:18 +0100179 if ((sid >= 0 && s->puid == sid) ||
180 (sid < 0 && strcmp(s->id, sv_name) == 0))
Willy Tarreauf3950172009-10-10 18:35:51 +0200181 break;
182 *sv = s;
183 if (!s)
184 return 0;
185 return 1;
186}
187
Willy Tarreaue219db72007-12-03 01:30:13 +0100188/* This function parses a "timeout" statement in a proxy section. It returns
189 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200190 * return zero, it will write an error or warning message into a preallocated
191 * buffer returned at <err>. The trailing is not be written. The function must
192 * be called with <args> pointing to the first command line word, with <proxy>
193 * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL.
194 * As a special case for compatibility with older configs, it also accepts
195 * "{cli|srv|con}timeout" in args[0].
Willy Tarreaue219db72007-12-03 01:30:13 +0100196 */
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200197static int proxy_parse_timeout(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200198 struct proxy *defpx, const char *file, int line,
199 char **err)
Willy Tarreaue219db72007-12-03 01:30:13 +0100200{
201 unsigned timeout;
202 int retval, cap;
203 const char *res, *name;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200204 int *tv = NULL;
205 int *td = NULL;
Willy Tarreaued446492014-04-28 22:56:38 +0200206 int warn = 0;
Willy Tarreaue219db72007-12-03 01:30:13 +0100207
208 retval = 0;
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200209
210 /* simply skip "timeout" but remain compatible with old form */
211 if (strcmp(args[0], "timeout") == 0)
212 args++;
213
Willy Tarreaue219db72007-12-03 01:30:13 +0100214 name = args[0];
Willy Tarreaued446492014-04-28 22:56:38 +0200215 if (!strcmp(args[0], "client") || (!strcmp(args[0], "clitimeout") && (warn = WARN_CLITO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100216 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100217 tv = &proxy->timeout.client;
218 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100219 cap = PR_CAP_FE;
220 } else if (!strcmp(args[0], "tarpit")) {
221 tv = &proxy->timeout.tarpit;
222 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100223 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100224 } else if (!strcmp(args[0], "http-keep-alive")) {
225 tv = &proxy->timeout.httpka;
226 td = &defpx->timeout.httpka;
227 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau036fae02008-01-06 13:24:40 +0100228 } else if (!strcmp(args[0], "http-request")) {
229 tv = &proxy->timeout.httpreq;
230 td = &defpx->timeout.httpreq;
Willy Tarreaucd7afc02009-07-12 10:03:17 +0200231 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaued446492014-04-28 22:56:38 +0200232 } else if (!strcmp(args[0], "server") || (!strcmp(args[0], "srvtimeout") && (warn = WARN_SRVTO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100233 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100234 tv = &proxy->timeout.server;
235 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100236 cap = PR_CAP_BE;
Willy Tarreaued446492014-04-28 22:56:38 +0200237 } else if (!strcmp(args[0], "connect") || (!strcmp(args[0], "contimeout") && (warn = WARN_CONTO_DEPRECATED))) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100238 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100239 tv = &proxy->timeout.connect;
240 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100241 cap = PR_CAP_BE;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100242 } else if (!strcmp(args[0], "check")) {
243 tv = &proxy->timeout.check;
244 td = &defpx->timeout.check;
245 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100246 } else if (!strcmp(args[0], "queue")) {
247 tv = &proxy->timeout.queue;
248 td = &defpx->timeout.queue;
249 cap = PR_CAP_BE;
Willy Tarreauce887fd2012-05-12 12:50:00 +0200250 } else if (!strcmp(args[0], "tunnel")) {
251 tv = &proxy->timeout.tunnel;
252 td = &defpx->timeout.tunnel;
253 cap = PR_CAP_BE;
Willy Tarreau05cdd962014-05-10 14:30:07 +0200254 } else if (!strcmp(args[0], "client-fin")) {
255 tv = &proxy->timeout.clientfin;
256 td = &defpx->timeout.clientfin;
257 cap = PR_CAP_FE;
258 } else if (!strcmp(args[0], "server-fin")) {
259 tv = &proxy->timeout.serverfin;
260 td = &defpx->timeout.serverfin;
261 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100262 } else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200263 memprintf(err,
264 "'timeout' supports 'client', 'server', 'connect', 'check', "
Willy Tarreau05cdd962014-05-10 14:30:07 +0200265 "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', "
266 "'client-fin' and 'server-fin' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200267 args[0]);
Willy Tarreaue219db72007-12-03 01:30:13 +0100268 return -1;
269 }
270
271 if (*args[1] == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200272 memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100273 return -1;
274 }
275
276 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
277 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200278 memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100279 return -1;
280 }
281
282 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200283 memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability",
284 name, proxy_type_str(proxy), proxy->id,
285 (cap & PR_CAP_BE) ? "backend" : "frontend");
Willy Tarreaue219db72007-12-03 01:30:13 +0100286 retval = 1;
287 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200288 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200289 memprintf(err, "overwriting 'timeout %s' which was already specified", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100290 retval = 1;
291 }
Willy Tarreaued446492014-04-28 22:56:38 +0200292 else if (warn) {
293 if (!already_warned(warn)) {
294 memprintf(err, "the '%s' directive is now deprecated in favor of 'timeout %s', and will not be supported in future versions.",
295 args[0], name);
296 retval = 1;
297 }
298 }
Willy Tarreaue219db72007-12-03 01:30:13 +0100299
Willy Tarreaufac5b592014-05-22 08:24:46 +0200300 if (*args[2] != 0) {
301 memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]);
302 retval = -1;
303 }
304
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200305 *tv = MS_TO_TICKS(timeout);
Willy Tarreaue219db72007-12-03 01:30:13 +0100306 return retval;
307}
308
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100309/* This function parses a "rate-limit" statement in a proxy section. It returns
310 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200311 * return zero, it will write an error or warning message into a preallocated
312 * buffer returned at <err>. The function must be called with <args> pointing
313 * to the first command line word, with <proxy> pointing to the proxy being
314 * parsed, and <defpx> to the default proxy or NULL.
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100315 */
316static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200317 struct proxy *defpx, const char *file, int line,
318 char **err)
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100319{
320 int retval, cap;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200321 char *res;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100322 unsigned int *tv = NULL;
323 unsigned int *td = NULL;
324 unsigned int val;
325
326 retval = 0;
327
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200328 if (strcmp(args[1], "sessions") == 0) {
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200329 tv = &proxy->fe_sps_lim;
330 td = &defpx->fe_sps_lim;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100331 cap = PR_CAP_FE;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200332 }
333 else {
334 memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100335 return -1;
336 }
337
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200338 if (*args[2] == 0) {
339 memprintf(err, "'%s %s' expects expects an integer value (in sessions/second)", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100340 return -1;
341 }
342
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200343 val = strtoul(args[2], &res, 0);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100344 if (*res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200345 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 +0100346 return -1;
347 }
348
349 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200350 memprintf(err, "%s %s will be ignored because %s '%s' has no %s capability",
351 args[0], args[1], proxy_type_str(proxy), proxy->id,
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100352 (cap & PR_CAP_BE) ? "backend" : "frontend");
353 retval = 1;
354 }
355 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200356 memprintf(err, "overwriting %s %s which was already specified", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100357 retval = 1;
358 }
359
360 *tv = val;
361 return retval;
362}
363
Willy Tarreauc35362a2014-04-25 13:58:37 +0200364/* This function parses a "max-keep-alive-queue" statement in a proxy section.
365 * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it
366 * does not return zero, it will write an error or warning message into a
367 * preallocated buffer returned at <err>. The function must be called with
368 * <args> pointing to the first command line word, with <proxy> pointing to
369 * the proxy being parsed, and <defpx> to the default proxy or NULL.
370 */
371static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy,
372 struct proxy *defpx, const char *file, int line,
373 char **err)
374{
375 int retval;
376 char *res;
377 unsigned int val;
378
379 retval = 0;
380
381 if (*args[1] == 0) {
382 memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]);
383 return -1;
384 }
385
386 val = strtol(args[1], &res, 0);
387 if (*res) {
388 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
389 return -1;
390 }
391
392 if (!(proxy->cap & PR_CAP_BE)) {
393 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
394 args[0], proxy_type_str(proxy), proxy->id);
395 retval = 1;
396 }
397
398 /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */
399 proxy->max_ka_queue = val + 1;
400 return retval;
401}
402
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200403/* This function parses a "declare" statement in a proxy section. It returns -1
404 * if there is any error, 1 for warning, otherwise 0. If it does not return zero,
405 * it will write an error or warning message into a preallocated buffer returned
406 * at <err>. The function must be called with <args> pointing to the first command
407 * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the
408 * default proxy or NULL.
409 */
410static int proxy_parse_declare(char **args, int section, struct proxy *curpx,
411 struct proxy *defpx, const char *file, int line,
412 char **err)
413{
414 /* Capture keyword wannot be declared in a default proxy. */
415 if (curpx == defpx) {
Joseph Herlant9edebb82018-11-15 11:50:44 -0800416 memprintf(err, "'%s' not available in default section", args[0]);
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200417 return -1;
418 }
419
Joseph Herlant59dd2952018-11-15 11:46:55 -0800420 /* Capture keywork is only available in frontend. */
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200421 if (!(curpx->cap & PR_CAP_FE)) {
Joseph Herlant9edebb82018-11-15 11:50:44 -0800422 memprintf(err, "'%s' only available in frontend or listen section", args[0]);
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200423 return -1;
424 }
425
426 /* Check mandatory second keyword. */
427 if (!args[1] || !*args[1]) {
428 memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]);
429 return -1;
430 }
431
Joseph Herlant59dd2952018-11-15 11:46:55 -0800432 /* Actually, declare is only available for declaring capture
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200433 * slot, but in the future it can declare maps or variables.
Joseph Herlant59dd2952018-11-15 11:46:55 -0800434 * So, this section permits to check and switch according with
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200435 * the second keyword.
436 */
437 if (strcmp(args[1], "capture") == 0) {
438 char *error = NULL;
439 long len;
440 struct cap_hdr *hdr;
441
442 /* Check the next keyword. */
443 if (!args[2] || !*args[2] ||
444 (strcmp(args[2], "response") != 0 &&
445 strcmp(args[2], "request") != 0)) {
446 memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]);
447 return -1;
448 }
449
450 /* Check the 'len' keyword. */
451 if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) {
452 memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]);
453 return -1;
454 }
455
456 /* Check the length value. */
457 if (!args[4] || !*args[4]) {
458 memprintf(err, "'%s %s': 'len' requires a numeric value that represents the "
459 "capture length",
460 args[0], args[1]);
461 return -1;
462 }
463
464 /* convert the length value. */
465 len = strtol(args[4], &error, 10);
466 if (*error != '\0') {
467 memprintf(err, "'%s %s': cannot parse the length '%s'.",
468 args[0], args[1], args[3]);
469 return -1;
470 }
471
472 /* check length. */
473 if (len <= 0) {
474 memprintf(err, "length must be > 0");
475 return -1;
476 }
477
478 /* register the capture. */
Vincent Bernat02779b62016-04-03 13:48:43 +0200479 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200480 hdr->name = NULL; /* not a header capture */
481 hdr->namelen = 0;
482 hdr->len = len;
483 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
484
485 if (strcmp(args[2], "request") == 0) {
486 hdr->next = curpx->req_cap;
487 hdr->index = curpx->nb_req_cap++;
488 curpx->req_cap = hdr;
489 }
490 if (strcmp(args[2], "response") == 0) {
491 hdr->next = curpx->rsp_cap;
492 hdr->index = curpx->nb_rsp_cap++;
493 curpx->rsp_cap = hdr;
494 }
495 return 0;
496 }
497 else {
498 memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]);
499 return -1;
500 }
501}
502
Willy Tarreauf79d9502014-03-15 07:22:35 +0100503/* This function inserts proxy <px> into the tree of known proxies. The proxy's
504 * name is used as the storing key so it must already have been initialized.
505 */
506void proxy_store_name(struct proxy *px)
507{
508 px->conf.by_name.key = px->id;
509 ebis_insert(&proxy_by_name, &px->conf.by_name);
510}
511
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200512/* Returns a pointer to the first proxy matching capabilities <cap> and id
513 * <id>. NULL is returned if no match is found. If <table> is non-zero, it
514 * only considers proxies having a table.
Willy Tarreaubc216c42011-08-02 11:25:54 +0200515 */
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200516struct proxy *proxy_find_by_id(int id, int cap, int table)
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200517{
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200518 struct eb32_node *n;
Willy Tarreaubc216c42011-08-02 11:25:54 +0200519
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200520 for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) {
521 struct proxy *px = container_of(n, struct proxy, conf.id);
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100522
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200523 if (px->uuid != id)
524 break;
Alex Williams96532db2009-11-01 21:27:13 -0500525
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200526 if ((px->cap & cap) != cap)
527 continue;
Alex Williams96532db2009-11-01 21:27:13 -0500528
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200529 if (table && !px->table.size)
530 continue;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100531
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200532 return px;
533 }
534 return NULL;
535}
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100536
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200537/* Returns a pointer to the first proxy matching either name <name>, or id
538 * <name> if <name> begins with a '#'. NULL is returned if no match is found.
539 * If <table> is non-zero, it only considers proxies having a table.
540 */
541struct proxy *proxy_find_by_name(const char *name, int cap, int table)
542{
543 struct proxy *curproxy;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200544
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200545 if (*name == '#') {
546 curproxy = proxy_find_by_id(atoi(name + 1), cap, table);
547 if (curproxy)
Willy Tarreauc739aa82015-05-26 11:35:41 +0200548 return curproxy;
Alex Williams96532db2009-11-01 21:27:13 -0500549 }
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100550 else {
551 struct ebpt_node *node;
552
553 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
554 curproxy = container_of(node, struct proxy, conf.by_name);
Alex Williams96532db2009-11-01 21:27:13 -0500555
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100556 if (strcmp(curproxy->id, name) != 0)
557 break;
558
559 if ((curproxy->cap & cap) != cap)
560 continue;
561
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200562 if (table && !curproxy->table.size)
563 continue;
564
Willy Tarreauc739aa82015-05-26 11:35:41 +0200565 return curproxy;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100566 }
567 }
Willy Tarreauc739aa82015-05-26 11:35:41 +0200568 return NULL;
Alex Williams96532db2009-11-01 21:27:13 -0500569}
570
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200571/* Finds the best match for a proxy with capabilities <cap>, name <name> and id
572 * <id>. At most one of <id> or <name> may be different provided that <cap> is
573 * valid. Either <id> or <name> may be left unspecified (0). The purpose is to
574 * find a proxy based on some information from a previous configuration, across
575 * reloads or during information exchange between peers.
576 *
577 * Names are looked up first if present, then IDs are compared if present. In
578 * case of an inexact match whatever is forced in the configuration has
579 * precedence in the following order :
580 * - 1) forced ID (proves a renaming / change of proxy type)
581 * - 2) proxy name+type (may indicate a move if ID differs)
582 * - 3) automatic ID+type (may indicate a renaming)
583 *
584 * Depending on what is found, we can end up in the following situations :
585 *
586 * name id cap | possible causes
587 * -------------+-----------------
588 * -- -- -- | nothing found
589 * -- -- ok | nothing found
590 * -- ok -- | proxy deleted, ID points to next one
591 * -- ok ok | proxy renamed, or deleted with ID pointing to next one
592 * ok -- -- | proxy deleted, but other half with same name still here (before)
593 * ok -- ok | proxy's ID changed (proxy moved in the config file)
594 * ok ok -- | proxy deleted, but other half with same name still here (after)
595 * ok ok ok | perfect match
596 *
597 * Upon return if <diff> is not NULL, it is zeroed then filled with up to 3 bits :
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200598 * - PR_FBM_MISMATCH_ID : proxy was found but ID differs
599 * (and ID was not zero)
600 * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs
601 * (and name was not NULL)
602 * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with
603 * the same name and/or id
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200604 *
605 * Only a valid proxy is returned. If capabilities do not match, NULL is
606 * returned. The caller can check <diff> to report detailed warnings / errors,
607 * and decide whether or not to use what was found.
608 */
609struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff)
610{
611 struct proxy *byname;
612 struct proxy *byid;
613
614 if (!name && !id)
615 return NULL;
616
617 if (diff)
618 *diff = 0;
619
620 byname = byid = NULL;
621
622 if (name) {
623 byname = proxy_find_by_name(name, cap, 0);
624 if (byname && (!id || byname->uuid == id))
625 return byname;
626 }
627
Joseph Herlant59dd2952018-11-15 11:46:55 -0800628 /* remaining possibilities :
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200629 * - name not set
630 * - name set but not found
631 * - name found, but ID doesn't match.
632 */
633 if (id) {
634 byid = proxy_find_by_id(id, cap, 0);
635 if (byid) {
636 if (byname) {
637 /* id+type found, name+type found, but not all 3.
638 * ID wins only if forced, otherwise name wins.
639 */
640 if (byid->options & PR_O_FORCED_ID) {
641 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200642 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200643 return byid;
644 }
645 else {
646 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200647 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200648 return byname;
649 }
650 }
651
Joseph Herlant59dd2952018-11-15 11:46:55 -0800652 /* remaining possibilities :
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200653 * - name not set
654 * - name set but not found
655 */
656 if (name && diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200657 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200658 return byid;
659 }
660
661 /* ID not found */
662 if (byname) {
663 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200664 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200665 return byname;
666 }
667 }
668
Joseph Herlant59dd2952018-11-15 11:46:55 -0800669 /* All remaining possibilities will lead to NULL. If we can report more
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200670 * detailed information to the caller about changed types and/or name,
671 * we'll do it. For example, we could detect that "listen foo" was
672 * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced.
673 * - name not set, ID not found
674 * - name not found, ID not set
675 * - name not found, ID not found
676 */
677 if (!diff)
678 return NULL;
679
680 if (name) {
681 byname = proxy_find_by_name(name, 0, 0);
682 if (byname && (!id || byname->uuid == id))
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200683 *diff |= PR_FBM_MISMATCH_PROXYTYPE;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200684 }
685
686 if (id) {
687 byid = proxy_find_by_id(id, 0, 0);
688 if (byid) {
689 if (!name)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200690 *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200691 else if (byid->options & PR_O_FORCED_ID)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200692 *diff |= PR_FBM_MISMATCH_NAME | PR_FBM_MISMATCH_PROXYTYPE; /* name and type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200693 /* otherwise it's a different proxy that was returned */
694 }
695 }
696 return NULL;
697}
698
Willy Tarreaubaaee002006-06-26 02:48:02 +0200699/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100700 * This function finds a server with matching name within selected proxy.
701 * It also checks if there are more matching servers with
702 * requested name as this often leads into unexpected situations.
703 */
704
705struct server *findserver(const struct proxy *px, const char *name) {
706
707 struct server *cursrv, *target = NULL;
708
709 if (!px)
710 return NULL;
711
712 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
713 if (strcmp(cursrv->id, name))
714 continue;
715
716 if (!target) {
717 target = cursrv;
718 continue;
719 }
720
Christopher Faulet767a84b2017-11-24 16:50:31 +0100721 ha_alert("Refusing to use duplicated server '%s' found in proxy: %s!\n",
722 name, px->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100723
724 return NULL;
725 }
726
727 return target;
728}
729
Willy Tarreauff01a212009-03-15 13:46:16 +0100730/* This function checks that the designated proxy has no http directives
731 * enabled. It will output a warning if there are, and will fix some of them.
732 * It returns the number of fatal errors encountered. This should be called
733 * at the end of the configuration parsing if the proxy is not in http mode.
734 * The <file> argument is used to construct the error message.
735 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200736int proxy_cfg_ensure_no_http(struct proxy *curproxy)
Willy Tarreauff01a212009-03-15 13:46:16 +0100737{
738 if (curproxy->cookie_name != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100739 ha_warning("config : cookie will be ignored for %s '%s' (needs 'mode http').\n",
740 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100741 }
742 if (curproxy->rsp_exp != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100743 ha_warning("config : server regular expressions will be ignored for %s '%s' (needs 'mode http').\n",
744 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100745 }
746 if (curproxy->req_exp != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100747 ha_warning("config : client regular expressions will be ignored for %s '%s' (needs 'mode http').\n",
748 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100749 }
750 if (curproxy->monitor_uri != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100751 ha_warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n",
752 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100753 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200754 if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) {
Willy Tarreauff01a212009-03-15 13:46:16 +0100755 curproxy->lbprm.algo &= ~BE_LB_ALGO;
756 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Christopher Faulet767a84b2017-11-24 16:50:31 +0100757 ha_warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n",
758 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100759 }
Willy Tarreau17804162009-11-09 21:27:51 +0100760 if (curproxy->to_log & (LW_REQ | LW_RESP)) {
761 curproxy->to_log &= ~(LW_REQ | LW_RESP);
Christopher Faulet767a84b2017-11-24 16:50:31 +0100762 ha_warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n",
763 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
764 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau17804162009-11-09 21:27:51 +0100765 }
Willy Tarreau62a61232013-04-12 18:13:46 +0200766 if (curproxy->conf.logformat_string == default_http_log_format ||
767 curproxy->conf.logformat_string == clf_http_log_format) {
768 /* Note: we don't change the directive's file:line number */
769 curproxy->conf.logformat_string = default_tcp_log_format;
Christopher Faulet767a84b2017-11-24 16:50:31 +0100770 ha_warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
771 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
772 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau196729e2012-05-31 19:30:26 +0200773 }
774
Willy Tarreauff01a212009-03-15 13:46:16 +0100775 return 0;
776}
777
Willy Tarreau237250c2011-07-29 01:49:03 +0200778/* Perform the most basic initialization of a proxy :
779 * memset(), list_init(*), reset_timeouts(*).
Willy Tarreaub249e842011-09-07 18:41:08 +0200780 * Any new proxy or peer should be initialized via this function.
Willy Tarreau237250c2011-07-29 01:49:03 +0200781 */
782void init_new_proxy(struct proxy *p)
783{
784 memset(p, 0, sizeof(struct proxy));
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100785 p->obj_type = OBJ_TYPE_PROXY;
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400786 p->pendconns = EB_ROOT;
Willy Tarreau237250c2011-07-29 01:49:03 +0200787 LIST_INIT(&p->acl);
788 LIST_INIT(&p->http_req_rules);
Willy Tarreaue365c0b2013-06-11 16:06:12 +0200789 LIST_INIT(&p->http_res_rules);
Willy Tarreau353bc9f2014-04-28 22:05:31 +0200790 LIST_INIT(&p->block_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200791 LIST_INIT(&p->redirect_rules);
792 LIST_INIT(&p->mon_fail_cond);
793 LIST_INIT(&p->switching_rules);
Willy Tarreau4a5cade2012-04-05 21:09:48 +0200794 LIST_INIT(&p->server_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200795 LIST_INIT(&p->persist_rules);
796 LIST_INIT(&p->sticking_rules);
797 LIST_INIT(&p->storersp_rules);
798 LIST_INIT(&p->tcp_req.inspect_rules);
799 LIST_INIT(&p->tcp_rep.inspect_rules);
800 LIST_INIT(&p->tcp_req.l4_rules);
Willy Tarreau620408f2016-10-21 16:37:51 +0200801 LIST_INIT(&p->tcp_req.l5_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +0200802 LIST_INIT(&p->req_add);
803 LIST_INIT(&p->rsp_add);
804 LIST_INIT(&p->listener_queue);
William Lallemand0f99e342011-10-12 17:50:54 +0200805 LIST_INIT(&p->logsrvs);
William Lallemand723b73a2012-02-08 16:37:49 +0100806 LIST_INIT(&p->logformat);
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200807 LIST_INIT(&p->logformat_sd);
William Lallemanda73203e2012-03-12 12:48:57 +0100808 LIST_INIT(&p->format_unique_id);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200809 LIST_INIT(&p->conf.bind);
Willy Tarreau4348fad2012-09-20 16:48:07 +0200810 LIST_INIT(&p->conf.listeners);
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200811 LIST_INIT(&p->conf.args.list);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200812 LIST_INIT(&p->tcpcheck_rules);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100813 LIST_INIT(&p->filter_configs);
Willy Tarreau237250c2011-07-29 01:49:03 +0200814
815 /* Timeouts are defined as -1 */
816 proxy_reset_timeouts(p);
817 p->tcp_rep.inspect_delay = TICK_ETERNITY;
Willy Tarreau050536d2012-10-04 08:47:34 +0200818
819 /* initial uuid is unassigned (-1) */
820 p->uuid = -1;
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200821
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100822 HA_SPIN_INIT(&p->lock);
Willy Tarreau237250c2011-07-29 01:49:03 +0200823}
824
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100825/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200826 * This function creates all proxy sockets. It should be done very early,
827 * typically before privileges are dropped. The sockets will be registered
828 * but not added to any fd_set, in order not to loose them across the fork().
Willy Tarreau562515c2011-07-25 08:11:52 +0200829 * The proxies also start in READY state because they all have their listeners
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200830 * bound.
Willy Tarreau2ff76222007-04-09 19:29:56 +0200831 *
832 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
833 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834 */
835int start_proxies(int verbose)
836{
837 struct proxy *curproxy;
838 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100839 int lerr, err = ERR_NONE;
840 int pxerr;
841 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200842
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100843 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 if (curproxy->state != PR_STNEW)
845 continue; /* already initialized */
846
847 pxerr = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200848 list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100849 if (listener->state != LI_ASSIGNED)
850 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851
Emeric Bruncf20bf12010-10-22 16:06:11 +0200852 lerr = listener->proto->bind(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200853
Willy Tarreaue6b98942007-10-29 01:09:36 +0100854 /* errors are reported if <verbose> is set or if they are fatal */
855 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
856 if (lerr & ERR_ALERT)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100857 ha_alert("Starting %s %s: %s\n",
858 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100859 else if (lerr & ERR_WARN)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100860 ha_warning("Starting %s %s: %s\n",
861 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200862 }
863
Willy Tarreaue6b98942007-10-29 01:09:36 +0100864 err |= lerr;
865 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100867 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200868 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100869 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200870 pxerr |= 1;
871 continue;
872 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 }
874
875 if (!pxerr) {
Willy Tarreau562515c2011-07-25 08:11:52 +0200876 curproxy->state = PR_STREADY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
878 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100879
880 if (err & ERR_ABORT)
881 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200882 }
883
884 return err;
885}
886
887
888/*
Willy Tarreau918ff602011-07-25 16:33:49 +0200889 * This is the proxy management task. It enables proxies when there are enough
Willy Tarreau87b09662015-04-03 00:22:06 +0200890 * free streams, or stops them when the table is full. It is designed to be
Willy Tarreau918ff602011-07-25 16:33:49 +0200891 * called as a task which is woken up upon stopping or when rate limiting must
892 * be enforced.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893 */
Olivier Houchard9f6af332018-05-25 14:04:04 +0200894struct task *manage_proxy(struct task *t, void *context, unsigned short state)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895{
Olivier Houchard9f6af332018-05-25 14:04:04 +0200896 struct proxy *p = context;
Willy Tarreau918ff602011-07-25 16:33:49 +0200897 int next = TICK_ETERNITY;
Willy Tarreau79584222009-03-06 09:18:27 +0100898 unsigned int wait;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200899
Willy Tarreau918ff602011-07-25 16:33:49 +0200900 /* We should periodically try to enable listeners waiting for a
901 * global resource here.
902 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200903
Willy Tarreau918ff602011-07-25 16:33:49 +0200904 /* first, let's check if we need to stop the proxy */
905 if (unlikely(stopping && p->state != PR_STSTOPPED)) {
906 int t;
907 t = tick_remain(now_ms, p->stop_time);
908 if (t == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100909 ha_warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
910 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
Willy Tarreau918ff602011-07-25 16:33:49 +0200911 send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
912 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
913 stop_proxy(p);
914 /* try to free more memory */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100915 pool_gc(NULL);
Willy Tarreau918ff602011-07-25 16:33:49 +0200916 }
917 else {
918 next = tick_first(next, p->stop_time);
919 }
920 }
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200921
Willy Tarreau3a925c12013-09-04 17:54:01 +0200922 /* If the proxy holds a stick table, we need to purge all unused
923 * entries. These are all the ones in the table with ref_cnt == 0
924 * and all the ones in the pool used to allocate new entries. Any
Willy Tarreau87b09662015-04-03 00:22:06 +0200925 * entry attached to an existing stream waiting for a store will
Willy Tarreau3a925c12013-09-04 17:54:01 +0200926 * be in neither list. Any entry being dumped will have ref_cnt > 0.
927 * However we protect tables that are being synced to peers.
928 */
929 if (unlikely(stopping && p->state == PR_STSTOPPED && p->table.current)) {
930 if (!p->table.syncing) {
931 stktable_trash_oldest(&p->table, p->table.current);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100932 pool_gc(NULL);
Willy Tarreau3a925c12013-09-04 17:54:01 +0200933 }
934 if (p->table.current) {
935 /* some entries still remain, let's recheck in one second */
936 next = tick_first(next, tick_add(now_ms, 1000));
937 }
938 }
939
Willy Tarreau918ff602011-07-25 16:33:49 +0200940 /* the rest below is just for frontends */
941 if (!(p->cap & PR_CAP_FE))
942 goto out;
Willy Tarreauf3f8c702011-07-25 07:37:28 +0200943
Willy Tarreau918ff602011-07-25 16:33:49 +0200944 /* check the various reasons we may find to block the frontend */
945 if (unlikely(p->feconn >= p->maxconn)) {
946 if (p->state == PR_STREADY)
947 p->state = PR_STFULL;
948 goto out;
949 }
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100950
Willy Tarreau918ff602011-07-25 16:33:49 +0200951 /* OK we have no reason to block, so let's unblock if we were blocking */
952 if (p->state == PR_STFULL)
953 p->state = PR_STREADY;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100954
Willy Tarreau918ff602011-07-25 16:33:49 +0200955 if (p->fe_sps_lim &&
956 (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) {
957 /* we're blocking because a limit was reached on the number of
958 * requests/s on the frontend. We want to re-check ASAP, which
959 * means in 1 ms before estimated expiration date, because the
960 * timer will have settled down.
961 */
962 next = tick_first(next, tick_add(now_ms, wait));
963 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200964 }
Willy Tarreau918ff602011-07-25 16:33:49 +0200965
966 /* The proxy is not limited so we can re-enable any waiting listener */
967 if (!LIST_ISEMPTY(&p->listener_queue))
968 dequeue_all_listeners(&p->listener_queue);
969 out:
970 t->expire = next;
971 task_queue(t);
972 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200973}
974
975
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100976static int proxy_parse_hard_stop_after(char **args, int section_type, struct proxy *curpx,
977 struct proxy *defpx, const char *file, int line,
978 char **err)
979{
980 const char *res;
981
982 if (!*args[1]) {
983 memprintf(err, "'%s' expects <time> as argument.\n", args[0]);
984 return -1;
985 }
986 res = parse_time_err(args[1], &global.hard_stop_after, TIME_UNIT_MS);
987 if (res) {
988 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
989 return -1;
990 }
991 return 0;
992}
993
Olivier Houchard9f6af332018-05-25 14:04:04 +0200994struct task *hard_stop(struct task *t, void *context, unsigned short state)
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100995{
996 struct proxy *p;
997 struct stream *s;
998
999 if (killed) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001000 ha_warning("Some tasks resisted to hard-stop, exiting now.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001001 send_log(NULL, LOG_WARNING, "Some tasks resisted to hard-stop, exiting now.\n");
Joseph Herlant59dd2952018-11-15 11:46:55 -08001002 /* Do some cleanup and explicitly quit */
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001003 deinit();
1004 exit(0);
1005 }
1006
Christopher Faulet767a84b2017-11-24 16:50:31 +01001007 ha_warning("soft-stop running for too long, performing a hard-stop.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001008 send_log(NULL, LOG_WARNING, "soft-stop running for too long, performing a hard-stop.\n");
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001009 p = proxies_list;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001010 while (p) {
1011 if ((p->cap & PR_CAP_FE) && (p->feconn > 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001012 ha_warning("Proxy %s hard-stopped (%d remaining conns will be closed).\n",
1013 p->id, p->feconn);
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001014 send_log(p, LOG_WARNING, "Proxy %s hard-stopped (%d remaining conns will be closed).\n",
1015 p->id, p->feconn);
1016 }
1017 p = p->next;
1018 }
1019 list_for_each_entry(s, &streams, list) {
1020 stream_shutdown(s, SF_ERR_KILLED);
1021 }
1022
1023 killed = 1;
1024 t->expire = tick_add(now_ms, MS_TO_TICKS(1000));
1025 return t;
1026}
1027
Willy Tarreaubaaee002006-06-26 02:48:02 +02001028/*
1029 * this function disables health-check servers so that the process will quickly be ignored
1030 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
1031 * time will not be used since it would already not listen anymore to the socket.
1032 */
1033void soft_stop(void)
1034{
1035 struct proxy *p;
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001036 struct peers *prs;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001037 struct task *task;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001038
1039 stopping = 1;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001040 if (tick_isset(global.hard_stop_after)) {
Emeric Brunc60def82017-09-27 14:59:38 +02001041 task = task_new(MAX_THREADS_MASK);
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001042 if (task) {
1043 task->process = hard_stop;
1044 task_schedule(task, tick_add(now_ms, global.hard_stop_after));
1045 }
1046 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001047 ha_alert("out of memory trying to allocate the hard-stop task.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001048 }
1049 }
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001050 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001051 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001052 while (p) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001053 /* Zombie proxy, let's close the file descriptors */
1054 if (p->state == PR_STSTOPPED &&
1055 !LIST_ISEMPTY(&p->conf.listeners) &&
1056 LIST_ELEM(p->conf.listeners.n,
1057 struct listener *, by_fe)->state >= LI_ZOMBIE) {
1058 struct listener *l;
1059 list_for_each_entry(l, &p->conf.listeners, by_fe) {
1060 if (l->state >= LI_ZOMBIE)
1061 close(l->fd);
1062 l->state = LI_INIT;
1063 }
1064 }
1065
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066 if (p->state != PR_STSTOPPED) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001067 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 +02001068 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 +02001069 p->stop_time = tick_add(now_ms, p->grace);
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001070
Willy Tarreau20b7afb2015-09-28 16:35:04 +02001071 /* Note: do not wake up stopped proxies' task nor their tables'
1072 * tasks as these ones might point to already released entries.
1073 */
1074 if (p->table.size && p->table.sync_task)
1075 task_wakeup(p->table.sync_task, TASK_WOKEN_MSG);
1076
1077 if (p->task)
1078 task_wakeup(p->task, TASK_WOKEN_MSG);
1079 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001080 p = p->next;
1081 }
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001082
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001083 prs = cfg_peers;
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001084 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001085 if (prs->peers_fe)
1086 stop_proxy(prs->peers_fe);
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001087 prs = prs->next;
1088 }
Willy Tarreaud0807c32010-08-27 18:26:11 +02001089 /* signal zero is used to broadcast the "stopping" event */
1090 signal_handler(0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001091}
1092
1093
Willy Tarreaube58c382011-07-24 18:28:10 +02001094/* Temporarily disables listening on all of the proxy's listeners. Upon
1095 * success, the proxy enters the PR_PAUSED state. If disabling at least one
1096 * listener returns an error, then the proxy state is set to PR_STERROR
Willy Tarreauce8fe252011-09-07 19:14:57 +02001097 * because we don't know how to resume from this. The function returns 0
1098 * if it fails, or non-zero on success.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001099 */
Willy Tarreauce8fe252011-09-07 19:14:57 +02001100int pause_proxy(struct proxy *p)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001101{
1102 struct listener *l;
Willy Tarreauce8fe252011-09-07 19:14:57 +02001103
1104 if (!(p->cap & PR_CAP_FE) || p->state == PR_STERROR ||
1105 p->state == PR_STSTOPPED || p->state == PR_STPAUSED)
1106 return 1;
1107
Christopher Faulet767a84b2017-11-24 16:50:31 +01001108 ha_warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001109 send_log(p, LOG_WARNING, "Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
1110
Willy Tarreau4348fad2012-09-20 16:48:07 +02001111 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreaube58c382011-07-24 18:28:10 +02001112 if (!pause_listener(l))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113 p->state = PR_STERROR;
1114 }
Willy Tarreauce8fe252011-09-07 19:14:57 +02001115
1116 if (p->state == PR_STERROR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001117 ha_warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001118 send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
1119 return 0;
1120 }
1121
1122 p->state = PR_STPAUSED;
1123 return 1;
Willy Tarreauda250db2008-10-12 12:07:48 +02001124}
1125
Olivier Houchard1fc05162017-04-06 01:05:05 +02001126/* This function makes the proxy unusable, but keeps the listening sockets
1127 * opened, so that if any process requests them, we are able to serve them.
1128 * This should only be called early, before we started accepting requests.
1129 */
1130void zombify_proxy(struct proxy *p)
1131{
1132 struct listener *l;
1133 struct listener *first_to_listen = NULL;
1134
1135 list_for_each_entry(l, &p->conf.listeners, by_fe) {
1136 enum li_state oldstate = l->state;
1137
1138 unbind_listener_no_close(l);
1139 if (l->state >= LI_ASSIGNED) {
1140 delete_listener(l);
Olivier Houchard1fc05162017-04-06 01:05:05 +02001141 }
1142 /*
1143 * Pretend we're still up and running so that the fd
1144 * will be sent if asked.
1145 */
1146 l->state = LI_ZOMBIE;
1147 if (!first_to_listen && oldstate >= LI_LISTEN)
1148 first_to_listen = l;
1149 }
1150 /* Quick hack : at stop time, to know we have to close the sockets
1151 * despite the proxy being marked as stopped, make the first listener
1152 * of the listener list an active one, so that we don't have to
1153 * parse the whole list to be sure.
1154 */
1155 if (first_to_listen && LIST_ELEM(p->conf.listeners.n,
1156 struct listener *, by_fe) != first_to_listen) {
1157 LIST_DEL(&l->by_fe);
1158 LIST_ADD(&p->conf.listeners, &l->by_fe);
1159 }
1160
1161 p->state = PR_STSTOPPED;
1162}
Willy Tarreauda250db2008-10-12 12:07:48 +02001163
1164/*
1165 * This function completely stops a proxy and releases its listeners. It has
1166 * to be called when going down in order to release the ports so that another
1167 * process may bind to them. It must also be called on disabled proxies at the
William Lallemandc59f9882018-11-16 16:57:21 +01001168 * end of start-up. If all listeners are closed, the proxy is set to the
Willy Tarreauda250db2008-10-12 12:07:48 +02001169 * PR_STSTOPPED state.
1170 */
1171void stop_proxy(struct proxy *p)
1172{
1173 struct listener *l;
William Lallemandc59f9882018-11-16 16:57:21 +01001174 int nostop = 0;
Willy Tarreauda250db2008-10-12 12:07:48 +02001175
Willy Tarreau4348fad2012-09-20 16:48:07 +02001176 list_for_each_entry(l, &p->conf.listeners, by_fe) {
William Lallemandc59f9882018-11-16 16:57:21 +01001177 if (l->options & LI_O_NOSTOP) {
1178 HA_ATOMIC_ADD(&unstoppable_jobs, 1);
1179 nostop = 1;
1180 continue;
1181 }
Willy Tarreauda250db2008-10-12 12:07:48 +02001182 unbind_listener(l);
1183 if (l->state >= LI_ASSIGNED) {
1184 delete_listener(l);
Willy Tarreauda250db2008-10-12 12:07:48 +02001185 }
1186 }
William Lallemandc59f9882018-11-16 16:57:21 +01001187 if (!nostop)
1188 p->state = PR_STSTOPPED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001189}
1190
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001191/* This function resumes listening on the specified proxy. It scans all of its
1192 * listeners and tries to enable them all. If any of them fails, the proxy is
1193 * put back to the paused state. It returns 1 upon success, or zero if an error
1194 * is encountered.
1195 */
1196int resume_proxy(struct proxy *p)
1197{
1198 struct listener *l;
1199 int fail;
1200
1201 if (p->state != PR_STPAUSED)
1202 return 1;
1203
Christopher Faulet767a84b2017-11-24 16:50:31 +01001204 ha_warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001205 send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
1206
1207 fail = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001208 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001209 if (!resume_listener(l)) {
1210 int port;
1211
1212 port = get_host_port(&l->addr);
1213 if (port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001214 ha_warning("Port %d busy while trying to enable %s %s.\n",
1215 port, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001216 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
1217 port, proxy_cap_str(p->cap), p->id);
1218 }
1219 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001220 ha_warning("Bind on socket %d busy while trying to enable %s %s.\n",
1221 l->luid, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001222 send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n",
1223 l->luid, proxy_cap_str(p->cap), p->id);
1224 }
1225
1226 /* Another port might have been enabled. Let's stop everything. */
1227 fail = 1;
1228 break;
1229 }
1230 }
1231
1232 p->state = PR_STREADY;
1233 if (fail) {
1234 pause_proxy(p);
1235 return 0;
1236 }
1237 return 1;
1238}
1239
Willy Tarreaubaaee002006-06-26 02:48:02 +02001240/*
1241 * This function temporarily disables listening so that another new instance
1242 * can start listening. It is designed to be called upon reception of a
1243 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
1244 * the proxy, or a SIGTTIN can be sent to listen again.
1245 */
1246void pause_proxies(void)
1247{
1248 int err;
1249 struct proxy *p;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001250 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001251
1252 err = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001253 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001254 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001255 while (p) {
Willy Tarreauce8fe252011-09-07 19:14:57 +02001256 err |= !pause_proxy(p);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001257 p = p->next;
1258 }
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001259
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001260 prs = cfg_peers;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001261 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001262 if (prs->peers_fe)
1263 err |= !pause_proxy(prs->peers_fe);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001264 prs = prs->next;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001265 }
1266
Willy Tarreaubaaee002006-06-26 02:48:02 +02001267 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001268 ha_warning("Some proxies refused to pause, performing soft stop now.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001269 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
1270 soft_stop();
1271 }
1272}
1273
1274
1275/*
1276 * This function reactivates listening. This can be used after a call to
1277 * sig_pause(), for example when a new instance has failed starting up.
1278 * It is designed to be called upon reception of a SIGTTIN.
1279 */
Willy Tarreaube58c382011-07-24 18:28:10 +02001280void resume_proxies(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001281{
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001282 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001283 struct proxy *p;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001284 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001285
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001286 err = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001287 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001288 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001289 while (p) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001290 err |= !resume_proxy(p);
1291 p = p->next;
1292 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001294 prs = cfg_peers;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001295 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001296 if (prs->peers_fe)
1297 err |= !resume_proxy(prs->peers_fe);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001298 prs = prs->next;
1299 }
Willy Tarreaube58c382011-07-24 18:28:10 +02001300
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001301 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001302 ha_warning("Some proxies refused to resume, a restart is probably needed to resume safe operations.\n");
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001303 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 +02001304 }
1305}
1306
Willy Tarreau87b09662015-04-03 00:22:06 +02001307/* Set current stream's backend to <be>. Nothing is done if the
1308 * stream already had a backend assigned, which is indicated by
Willy Tarreaue7dff022015-04-03 01:14:29 +02001309 * s->flags & SF_BE_ASSIGNED.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001310 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001311 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001312 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001313int stream_set_backend(struct stream *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001314{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001315 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001316 return 1;
Christopher Faulet41179042016-06-21 11:54:52 +02001317
1318 if (flt_set_stream_backend(s, be) < 0)
1319 return 0;
1320
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001321 s->be = be;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001322 HA_ATOMIC_UPDATE_MAX(&be->be_counters.conn_max,
1323 HA_ATOMIC_ADD(&be->beconn, 1));
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001324 proxy_inc_be_ctr(be);
1325
Willy Tarreau87b09662015-04-03 00:22:06 +02001326 /* assign new parameters to the stream from the new backend */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001327 s->si[1].flags &= ~SI_FL_INDEP_STR;
1328 if (be->options2 & PR_O2_INDEPSTR)
1329 s->si[1].flags |= SI_FL_INDEP_STR;
1330
Hongbo Longe39683c2017-03-10 18:41:51 +01001331 if (tick_isset(be->timeout.serverfin))
1332 s->si[1].hcto = be->timeout.serverfin;
1333
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001334 /* We want to enable the backend-specific analysers except those which
1335 * were already run as part of the frontend/listener. Note that it would
1336 * be more reliable to store the list of analysers that have been run,
1337 * but what we do here is OK for now.
1338 */
Christopher Faulet70e2f272017-01-09 16:33:19 +01001339 s->req.analysers |= be->be_req_ana & ~(strm_li(s) ? strm_li(s)->analysers : 0);
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001340
Willy Tarreau51aecc72009-07-12 09:47:04 +02001341 /* If the target backend requires HTTP processing, we have to allocate
Willy Tarreaueee5b512015-04-03 23:46:31 +02001342 * the HTTP transaction and hdr_idx if we did not have one.
Willy Tarreau51aecc72009-07-12 09:47:04 +02001343 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001344 if (unlikely(!s->txn && be->http_needed)) {
1345 if (unlikely(!http_alloc_txn(s)))
Willy Tarreau51aecc72009-07-12 09:47:04 +02001346 return 0; /* not enough memory */
Willy Tarreau39e4f622010-05-31 17:01:36 +02001347
1348 /* and now initialize the HTTP transaction state */
1349 http_init_txn(s);
Willy Tarreau51aecc72009-07-12 09:47:04 +02001350 }
1351
Christopher Faulet309c6412015-12-02 09:57:32 +01001352 /* Be sure to filter request headers if the backend is an HTTP proxy and
1353 * if there are filters attached to the stream. */
1354 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01001355 s->req.analysers |= AN_REQ_FLT_HTTP_HDRS;
Christopher Faulet309c6412015-12-02 09:57:32 +01001356
Willy Tarreaueee5b512015-04-03 23:46:31 +02001357 if (s->txn) {
1358 if (be->options2 & PR_O2_RSPBUG_OK)
1359 s->txn->rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001360
Willy Tarreaueee5b512015-04-03 23:46:31 +02001361 /* If we chain to an HTTP backend running a different HTTP mode, we
1362 * have to re-adjust the desired keep-alive/close mode to accommodate
1363 * both the frontend's and the backend's modes.
1364 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001365 if (strm_fe(s)->mode == PR_MODE_HTTP && be->mode == PR_MODE_HTTP &&
1366 ((strm_fe(s)->options & PR_O_HTTP_MODE) != (be->options & PR_O_HTTP_MODE)))
Willy Tarreaueee5b512015-04-03 23:46:31 +02001367 http_adjust_conn_mode(s, s->txn, &s->txn->req);
Willy Tarreau80a92c02014-03-12 10:41:13 +01001368
Willy Tarreaueee5b512015-04-03 23:46:31 +02001369 /* If an LB algorithm needs to access some pre-parsed body contents,
1370 * we must not start to forward anything until the connection is
1371 * confirmed otherwise we'll lose the pointer to these data and
1372 * prevent the hash from being doable again after a redispatch.
1373 */
1374 if (be->mode == PR_MODE_HTTP &&
1375 (be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_HI | BE_LB_HASH_PRM))
1376 s->txn->req.flags |= HTTP_MSGF_WAIT_CONN;
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001377
1378 /* we may request to parse a request body */
1379 if ((be->options & PR_O_WREQ_BODY) &&
1380 (s->txn->req.body_len || (s->txn->req.flags & HTTP_MSGF_TE_CHNK)))
1381 s->req.analysers |= AN_REQ_HTTP_BODY;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001382 }
1383
1384 s->flags |= SF_BE_ASSIGNED;
Willy Tarreau96e31212011-05-30 18:10:30 +02001385 if (be->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001386 s->req.flags |= CF_NEVER_WAIT;
1387 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02001388 }
1389
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001390 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001391}
1392
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001393/* Capture a bad request or response and archive it in the proxy's structure.
1394 * It is relatively protocol-agnostic so it requires that a number of elements
1395 * are passed :
1396 * - <proxy> is the proxy where the error was detected and where the snapshot
1397 * needs to be stored
Joseph Herlant59dd2952018-11-15 11:46:55 -08001398 * - <is_back> indicates that the error happened when receiving the response
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001399 * - <other_end> is a pointer to the proxy on the other side when known
1400 * - <target> is the target of the connection, usually a server or a proxy
1401 * - <sess> is the session which experienced the error
1402 * - <ctx> may be NULL or should contain any info relevant to the protocol
1403 * - <buf> is the buffer containing the offending data
1404 * - <buf_ofs> is the position of this buffer's input data in the input
1405 * stream, starting at zero. It may be passed as zero if unknown.
1406 * - <buf_out> is the portion of <buf->data> which was already forwarded and
1407 * which precedes the buffer's input. The buffer's input starts at
1408 * buf->head + buf_out.
1409 * - <err_pos> is the pointer to the faulty byte in the buffer's input.
1410 * - <show> is the callback to use to display <ctx>. It may be NULL.
1411 */
1412void proxy_capture_error(struct proxy *proxy, int is_back,
1413 struct proxy *other_end, enum obj_type *target,
1414 const struct session *sess,
1415 const struct buffer *buf, long buf_ofs,
1416 unsigned int buf_out, unsigned int err_pos,
1417 const union error_snapshot_ctx *ctx,
1418 void (*show)(struct buffer *, const struct error_snapshot *))
1419{
1420 struct error_snapshot *es;
1421 unsigned int buf_len;
1422 int len1, len2;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001423 unsigned int ev_id;
1424
1425 ev_id = HA_ATOMIC_XADD(&error_snapshot_id, 1);
1426
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001427 buf_len = b_data(buf) - buf_out;
1428
1429 es = malloc(sizeof(*es) + buf_len);
Willy Tarreauc55015e2018-09-07 19:02:32 +02001430 if (!es)
1431 return;
1432
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001433 es->buf_len = buf_len;
1434 es->ev_id = ev_id;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001435
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001436 len1 = b_size(buf) - buf_len;
1437 if (len1 > buf_len)
1438 len1 = buf_len;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001439
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001440 if (len1) {
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001441 memcpy(es->buf, b_peek(buf, buf_out), len1);
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001442 len2 = buf_len - len1;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001443 if (len2)
1444 memcpy(es->buf + len1, b_orig(buf), len2);
1445 }
1446
1447 es->buf_err = err_pos;
1448 es->when = date; // user-visible date
1449 es->srv = objt_server(target);
1450 es->oe = other_end;
1451 if (objt_conn(sess->origin))
1452 es->src = __objt_conn(sess->origin)->addr.from;
1453 else
1454 memset(&es->src, 0, sizeof(es->src));
1455
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001456 es->buf_wrap = b_wrap(buf) - b_peek(buf, buf_out);
1457 es->buf_out = buf_out;
1458 es->buf_ofs = buf_ofs;
1459
1460 /* be sure to indicate the offset of the first IN byte */
1461 if (es->buf_ofs >= es->buf_len)
1462 es->buf_ofs -= es->buf_len;
1463 else
1464 es->buf_ofs = 0;
1465
1466 /* protocol-specific part now */
1467 if (ctx)
1468 es->ctx = *ctx;
1469 else
1470 memset(&es->ctx, 0, sizeof(es->ctx));
1471 es->show = show;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001472
1473 /* note: we still lock since we have to be certain that nobody is
1474 * dumping the output while we free.
1475 */
1476 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
1477 if (is_back) {
1478 es = HA_ATOMIC_XCHG(&proxy->invalid_rep, es);
1479 } else {
1480 es = HA_ATOMIC_XCHG(&proxy->invalid_req, es);
1481 }
1482 free(es);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001483 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
1484}
1485
1486/* Config keywords below */
1487
Willy Tarreaudc13c112013-06-21 23:16:39 +02001488static struct cfg_kw_list cfg_kws = {ILH, {
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001489 { CFG_GLOBAL, "hard-stop-after", proxy_parse_hard_stop_after },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001490 { CFG_LISTEN, "timeout", proxy_parse_timeout },
1491 { CFG_LISTEN, "clitimeout", proxy_parse_timeout },
1492 { CFG_LISTEN, "contimeout", proxy_parse_timeout },
1493 { CFG_LISTEN, "srvtimeout", proxy_parse_timeout },
Willy Tarreau3a7d2072009-03-05 23:48:25 +01001494 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreauc35362a2014-04-25 13:58:37 +02001495 { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue },
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +02001496 { CFG_LISTEN, "declare", proxy_parse_declare },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001497 { 0, NULL, NULL },
1498}};
1499
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001500/* Expects to find a frontend named <arg> and returns it, otherwise displays various
1501 * adequate error messages and returns NULL. This function is designed to be used by
1502 * functions requiring a frontend on the CLI.
1503 */
1504struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg)
1505{
1506 struct proxy *px;
1507
1508 if (!*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001509 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001510 appctx->ctx.cli.msg = "A frontend name is expected.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001511 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001512 return NULL;
1513 }
1514
1515 px = proxy_fe_by_name(arg);
1516 if (!px) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001517 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001518 appctx->ctx.cli.msg = "No such frontend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001519 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001520 return NULL;
1521 }
1522 return px;
1523}
1524
Olivier Houchard614f8d72017-03-14 20:08:46 +01001525/* Expects to find a backend named <arg> and returns it, otherwise displays various
1526 * adequate error messages and returns NULL. This function is designed to be used by
1527 * functions requiring a frontend on the CLI.
1528 */
1529struct proxy *cli_find_backend(struct appctx *appctx, const char *arg)
1530{
1531 struct proxy *px;
1532
1533 if (!*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001534 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001535 appctx->ctx.cli.msg = "A backend name is expected.\n";
1536 appctx->st0 = CLI_ST_PRINT;
1537 return NULL;
1538 }
1539
1540 px = proxy_be_by_name(arg);
1541 if (!px) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001542 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001543 appctx->ctx.cli.msg = "No such backend.\n";
1544 appctx->st0 = CLI_ST_PRINT;
1545 return NULL;
1546 }
1547 return px;
1548}
1549
1550
William Lallemanda6c5f332016-11-19 02:25:36 +01001551/* parse a "show servers" CLI line, returns 0 if it wants to start the dump or
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001552 * 1 if it stops immediately. If an argument is specified, it will set the proxy
1553 * pointer into cli.p0 and its ID into cli.i0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001554 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001555static int cli_parse_show_servers(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemanda6c5f332016-11-19 02:25:36 +01001556{
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001557 struct proxy *px;
William Lallemanda6c5f332016-11-19 02:25:36 +01001558
1559 /* check if a backend name has been provided */
1560 if (*args[3]) {
1561 /* read server state from local file */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001562 px = proxy_be_by_name(args[3]);
William Lallemanda6c5f332016-11-19 02:25:36 +01001563
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001564 if (!px) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001565 appctx->ctx.cli.severity = LOG_ERR;
William Lallemanda6c5f332016-11-19 02:25:36 +01001566 appctx->ctx.cli.msg = "Can't find backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001567 appctx->st0 = CLI_ST_PRINT;
William Lallemanda6c5f332016-11-19 02:25:36 +01001568 return 1;
1569 }
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001570 appctx->ctx.cli.p0 = px;
1571 appctx->ctx.cli.i0 = px->uuid;
William Lallemanda6c5f332016-11-19 02:25:36 +01001572 }
1573 return 0;
1574}
1575
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001576/* dumps server state information into <buf> for all the servers found in backend cli.p0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001577 * These information are all the parameters which may change during HAProxy runtime.
1578 * By default, we only export to the last known server state file format.
1579 * These information can be used at next startup to recover same level of server state.
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001580 * It uses the proxy pointer from cli.p0, the proxy's id from cli.i0 and the server's
1581 * pointer from cli.p1.
William Lallemanda6c5f332016-11-19 02:25:36 +01001582 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001583static int dump_servers_state(struct stream_interface *si, struct buffer *buf)
William Lallemanda6c5f332016-11-19 02:25:36 +01001584{
1585 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001586 struct proxy *px = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001587 struct server *srv;
1588 char srv_addr[INET6_ADDRSTRLEN + 1];
1589 time_t srv_time_since_last_change;
1590 int bk_f_forced_id, srv_f_forced_id;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001591 char *srvrecord;
William Lallemanda6c5f332016-11-19 02:25:36 +01001592
William Lallemanda6c5f332016-11-19 02:25:36 +01001593 /* we don't want to report any state if the backend is not enabled on this process */
Willy Tarreau387bd4f2017-11-10 19:08:14 +01001594 if (px->bind_proc && !(px->bind_proc & pid_bit))
William Lallemanda6c5f332016-11-19 02:25:36 +01001595 return 1;
1596
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001597 if (!appctx->ctx.cli.p1)
1598 appctx->ctx.cli.p1 = px->srv;
William Lallemanda6c5f332016-11-19 02:25:36 +01001599
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001600 for (; appctx->ctx.cli.p1 != NULL; appctx->ctx.cli.p1 = srv->next) {
1601 srv = appctx->ctx.cli.p1;
William Lallemanda6c5f332016-11-19 02:25:36 +01001602 srv_addr[0] = '\0';
1603
1604 switch (srv->addr.ss_family) {
1605 case AF_INET:
1606 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
1607 srv_addr, INET_ADDRSTRLEN + 1);
1608 break;
1609 case AF_INET6:
1610 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
1611 srv_addr, INET6_ADDRSTRLEN + 1);
1612 break;
Daniel Corbett9215ffa2018-05-19 19:43:24 -04001613 default:
1614 memcpy(srv_addr, "-\0", 2);
1615 break;
William Lallemanda6c5f332016-11-19 02:25:36 +01001616 }
1617 srv_time_since_last_change = now.tv_sec - srv->last_change;
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001618 bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001619 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
1620
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001621 srvrecord = NULL;
1622 if (srv->srvrq && srv->srvrq->name)
1623 srvrecord = srv->srvrq->name;
1624
William Lallemanda6c5f332016-11-19 02:25:36 +01001625 chunk_appendf(buf,
1626 "%d %s "
1627 "%d %s %s "
1628 "%d %d %d %d %ld "
1629 "%d %d %d %d %d "
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001630 "%d %d %s %u %s"
William Lallemanda6c5f332016-11-19 02:25:36 +01001631 "\n",
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001632 px->uuid, px->id,
William Lallemanda6c5f332016-11-19 02:25:36 +01001633 srv->puid, srv->id, srv_addr,
Emeric Brun52a91d32017-08-31 14:41:55 +02001634 srv->cur_state, srv->cur_admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
William Lallemanda6c5f332016-11-19 02:25:36 +01001635 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001636 bk_f_forced_id, srv_f_forced_id, srv->hostname ? srv->hostname : "-", srv->svc_port,
1637 srvrecord ? srvrecord : "-");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001638 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001639 si_rx_room_blk(si);
William Lallemanda6c5f332016-11-19 02:25:36 +01001640 return 0;
1641 }
1642 }
1643 return 1;
1644}
1645
1646/* Parses backend list or simply use backend name provided by the user to return
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001647 * states of servers to stdout. It dumps proxy <cli.p0> and stops if <cli.i0> is
1648 * non-null.
William Lallemanda6c5f332016-11-19 02:25:36 +01001649 */
1650static int cli_io_handler_servers_state(struct appctx *appctx)
1651{
1652 struct stream_interface *si = appctx->owner;
William Lallemanda6c5f332016-11-19 02:25:36 +01001653 struct proxy *curproxy;
1654
1655 chunk_reset(&trash);
1656
1657 if (appctx->st2 == STAT_ST_INIT) {
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001658 if (!appctx->ctx.cli.p0)
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001659 appctx->ctx.cli.p0 = proxies_list;
William Lallemanda6c5f332016-11-19 02:25:36 +01001660 appctx->st2 = STAT_ST_HEAD;
1661 }
1662
1663 if (appctx->st2 == STAT_ST_HEAD) {
1664 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001665 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001666 si_rx_room_blk(si);
William Lallemanda6c5f332016-11-19 02:25:36 +01001667 return 0;
1668 }
1669 appctx->st2 = STAT_ST_INFO;
1670 }
1671
1672 /* STAT_ST_INFO */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001673 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1674 curproxy = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001675 /* servers are only in backends */
1676 if (curproxy->cap & PR_CAP_BE) {
1677 if (!dump_servers_state(si, &trash))
1678 return 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001679 }
1680 /* only the selected proxy is dumped */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001681 if (appctx->ctx.cli.i0)
William Lallemanda6c5f332016-11-19 02:25:36 +01001682 break;
1683 }
1684
1685 return 1;
1686}
1687
Willy Tarreau608ea592016-12-16 18:01:15 +01001688/* Parses backend list and simply report backend names. It keeps the proxy
1689 * pointer in cli.p0.
1690 */
William Lallemand933efcd2016-11-22 12:34:16 +01001691static int cli_io_handler_show_backend(struct appctx *appctx)
1692{
William Lallemand933efcd2016-11-22 12:34:16 +01001693 struct stream_interface *si = appctx->owner;
1694 struct proxy *curproxy;
1695
1696 chunk_reset(&trash);
1697
Willy Tarreau608ea592016-12-16 18:01:15 +01001698 if (!appctx->ctx.cli.p0) {
William Lallemand933efcd2016-11-22 12:34:16 +01001699 chunk_printf(&trash, "# name\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001700 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001701 si_rx_room_blk(si);
William Lallemand933efcd2016-11-22 12:34:16 +01001702 return 0;
1703 }
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001704 appctx->ctx.cli.p0 = proxies_list;
William Lallemand933efcd2016-11-22 12:34:16 +01001705 }
1706
Willy Tarreau608ea592016-12-16 18:01:15 +01001707 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1708 curproxy = appctx->ctx.cli.p0;
William Lallemand933efcd2016-11-22 12:34:16 +01001709
1710 /* looking for backends only */
1711 if (!(curproxy->cap & PR_CAP_BE))
1712 continue;
1713
1714 /* we don't want to list a backend which is bound to this process */
Willy Tarreau387bd4f2017-11-10 19:08:14 +01001715 if (curproxy->bind_proc && !(curproxy->bind_proc & pid_bit))
William Lallemand933efcd2016-11-22 12:34:16 +01001716 continue;
1717
1718 chunk_appendf(&trash, "%s\n", curproxy->id);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001719 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001720 si_rx_room_blk(si);
William Lallemand933efcd2016-11-22 12:34:16 +01001721 return 0;
1722 }
1723 }
1724
1725 return 1;
1726}
William Lallemanda6c5f332016-11-19 02:25:36 +01001727
Willy Tarreaua275a372018-08-21 14:50:44 +02001728/* Parses the "enable dynamic-cookies backend" directive, it always returns 1.
1729 *
1730 * Grabs the proxy lock and each server's lock.
1731 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001732static int cli_parse_enable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001733{
1734 struct proxy *px;
1735 struct server *s;
1736
1737 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1738 return 1;
1739
1740 px = cli_find_backend(appctx, args[3]);
1741 if (!px)
1742 return 1;
1743
Willy Tarreaua275a372018-08-21 14:50:44 +02001744 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1745
Olivier Houchard614f8d72017-03-14 20:08:46 +01001746 px->ck_opts |= PR_CK_DYNAMIC;
1747
Willy Tarreaua275a372018-08-21 14:50:44 +02001748 for (s = px->srv; s != NULL; s = s->next) {
1749 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001750 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02001751 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
1752 }
1753
1754 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001755
1756 return 1;
1757}
1758
Willy Tarreaua275a372018-08-21 14:50:44 +02001759/* Parses the "disable dynamic-cookies backend" directive, it always returns 1.
1760 *
1761 * Grabs the proxy lock and each server's lock.
1762 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001763static int cli_parse_disable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001764{
1765 struct proxy *px;
1766 struct server *s;
1767
1768 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1769 return 1;
1770
1771 px = cli_find_backend(appctx, args[3]);
1772 if (!px)
1773 return 1;
1774
Willy Tarreaua275a372018-08-21 14:50:44 +02001775 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1776
Olivier Houchard614f8d72017-03-14 20:08:46 +01001777 px->ck_opts &= ~PR_CK_DYNAMIC;
1778
1779 for (s = px->srv; s != NULL; s = s->next) {
Willy Tarreaua275a372018-08-21 14:50:44 +02001780 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001781 if (!(s->flags & SRV_F_COOKIESET)) {
1782 free(s->cookie);
1783 s->cookie = NULL;
1784 }
Willy Tarreaua275a372018-08-21 14:50:44 +02001785 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001786 }
1787
Willy Tarreaua275a372018-08-21 14:50:44 +02001788 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1789
Olivier Houchard614f8d72017-03-14 20:08:46 +01001790 return 1;
1791}
1792
Willy Tarreaua275a372018-08-21 14:50:44 +02001793/* Parses the "set dynamic-cookie-key backend" directive, it always returns 1.
1794 *
1795 * Grabs the proxy lock and each server's lock.
1796 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001797static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001798{
1799 struct proxy *px;
1800 struct server *s;
1801 char *newkey;
1802
1803 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1804 return 1;
1805
1806 px = cli_find_backend(appctx, args[3]);
1807 if (!px)
1808 return 1;
1809
1810 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001811 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001812 appctx->ctx.cli.msg = "String value expected.\n";
1813 appctx->st0 = CLI_ST_PRINT;
1814 return 1;
1815 }
1816
1817 newkey = strdup(args[4]);
1818 if (!newkey) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001819 appctx->ctx.cli.severity = LOG_ERR;
Olivier Houchard614f8d72017-03-14 20:08:46 +01001820 appctx->ctx.cli.msg = "Failed to allocate memory.\n";
1821 appctx->st0 = CLI_ST_PRINT;
1822 return 1;
1823 }
Willy Tarreaua275a372018-08-21 14:50:44 +02001824
1825 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1826
Olivier Houchard614f8d72017-03-14 20:08:46 +01001827 free(px->dyncookie_key);
1828 px->dyncookie_key = newkey;
1829
Willy Tarreaua275a372018-08-21 14:50:44 +02001830 for (s = px->srv; s != NULL; s = s->next) {
1831 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001832 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02001833 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
1834 }
1835
1836 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001837
1838 return 1;
1839}
1840
Willy Tarreaua275a372018-08-21 14:50:44 +02001841/* Parses the "set maxconn frontend" directive, it always returns 1.
1842 *
1843 * Grabs the proxy lock.
1844 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001845static int cli_parse_set_maxconn_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001846{
1847 struct proxy *px;
1848 struct listener *l;
1849 int v;
1850
1851 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1852 return 1;
1853
1854 px = cli_find_frontend(appctx, args[3]);
1855 if (!px)
1856 return 1;
1857
1858 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001859 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001860 appctx->ctx.cli.msg = "Integer value expected.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001861 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001862 return 1;
1863 }
1864
1865 v = atoi(args[4]);
1866 if (v < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001867 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001868 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001869 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001870 return 1;
1871 }
1872
1873 /* OK, the value is fine, so we assign it to the proxy and to all of
1874 * its listeners. The blocked ones will be dequeued.
1875 */
Willy Tarreaua275a372018-08-21 14:50:44 +02001876 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1877
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001878 px->maxconn = v;
1879 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1880 l->maxconn = v;
1881 if (l->state == LI_FULL)
1882 resume_listener(l);
1883 }
1884
1885 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&px->listener_queue))
1886 dequeue_all_listeners(&px->listener_queue);
1887
Willy Tarreaua275a372018-08-21 14:50:44 +02001888 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1889
Willy Tarreauc429a1f2016-11-23 16:22:04 +01001890 return 1;
1891}
1892
Willy Tarreaua275a372018-08-21 14:50:44 +02001893/* Parses the "shutdown frontend" directive, it always returns 1.
1894 *
1895 * Grabs the proxy lock.
1896 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001897static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001898{
1899 struct proxy *px;
1900
1901 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1902 return 1;
1903
1904 px = cli_find_frontend(appctx, args[2]);
1905 if (!px)
1906 return 1;
1907
1908 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001909 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001910 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001911 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001912 return 1;
1913 }
1914
Christopher Faulet767a84b2017-11-24 16:50:31 +01001915 ha_warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1916 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001917 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1918 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
Willy Tarreaua275a372018-08-21 14:50:44 +02001919
1920 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001921 stop_proxy(px);
Willy Tarreaua275a372018-08-21 14:50:44 +02001922 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1923
Willy Tarreau5212d7f2016-11-24 11:13:06 +01001924 return 1;
1925}
1926
Willy Tarreaua275a372018-08-21 14:50:44 +02001927/* Parses the "disable frontend" directive, it always returns 1.
1928 *
1929 * Grabs the proxy lock.
1930 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001931static int cli_parse_disable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01001932{
1933 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02001934 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001935
1936 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1937 return 1;
1938
1939 px = cli_find_frontend(appctx, args[2]);
1940 if (!px)
1941 return 1;
1942
1943 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001944 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001945 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001946 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001947 return 1;
1948 }
1949
1950 if (px->state == PR_STPAUSED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001951 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001952 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001953 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001954 return 1;
1955 }
1956
Willy Tarreaua275a372018-08-21 14:50:44 +02001957 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
1958 ret = pause_proxy(px);
1959 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
1960
1961 if (!ret) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001962 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001963 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001964 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001965 return 1;
1966 }
1967 return 1;
1968}
1969
Willy Tarreaua275a372018-08-21 14:50:44 +02001970/* Parses the "enable frontend" directive, it always returns 1.
1971 *
1972 * Grabs the proxy lock.
1973 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001974static int cli_parse_enable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01001975{
1976 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02001977 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001978
1979 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1980 return 1;
1981
1982 px = cli_find_frontend(appctx, args[2]);
1983 if (!px)
1984 return 1;
1985
1986 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001987 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001988 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001989 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001990 return 1;
1991 }
1992
1993 if (px->state != PR_STPAUSED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001994 appctx->ctx.cli.severity = LOG_NOTICE;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001995 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001996 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01001997 return 1;
1998 }
1999
Willy Tarreaua275a372018-08-21 14:50:44 +02002000 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
2001 ret = resume_proxy(px);
2002 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
2003
2004 if (!ret) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002005 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau15b9e682016-11-24 11:55:28 +01002006 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002007 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau15b9e682016-11-24 11:55:28 +01002008 return 1;
2009 }
2010 return 1;
2011}
2012
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002013/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
2014 * now.
2015 */
2016static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
2017{
2018 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2019 return 1;
2020
2021 if (*args[2]) {
2022 struct proxy *px;
2023
2024 px = proxy_find_by_name(args[2], 0, 0);
2025 if (px)
2026 appctx->ctx.errors.iid = px->uuid;
2027 else
2028 appctx->ctx.errors.iid = atoi(args[2]);
2029
2030 if (!appctx->ctx.errors.iid) {
2031 appctx->ctx.cli.severity = LOG_ERR;
2032 appctx->ctx.cli.msg = "No such proxy.\n";
2033 appctx->st0 = CLI_ST_PRINT;
2034 return 1;
2035 }
2036 }
2037 else
2038 appctx->ctx.errors.iid = -1; // dump all proxies
2039
2040 appctx->ctx.errors.flag = 0;
2041 if (strcmp(args[3], "request") == 0)
2042 appctx->ctx.errors.flag |= 4; // ignore response
2043 else if (strcmp(args[3], "response") == 0)
2044 appctx->ctx.errors.flag |= 2; // ignore request
2045 appctx->ctx.errors.px = NULL;
2046 return 0;
2047}
2048
2049/* This function dumps all captured errors onto the stream interface's
2050 * read buffer. It returns 0 if the output buffer is full and it needs
2051 * to be called again, otherwise non-zero.
2052 */
2053static int cli_io_handler_show_errors(struct appctx *appctx)
2054{
2055 struct stream_interface *si = appctx->owner;
2056 extern const char *monthname[12];
2057
2058 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
2059 return 1;
2060
2061 chunk_reset(&trash);
2062
2063 if (!appctx->ctx.errors.px) {
2064 /* the function had not been called yet, let's prepare the
2065 * buffer for a response.
2066 */
2067 struct tm tm;
2068
2069 get_localtime(date.tv_sec, &tm);
2070 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
2071 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2072 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
2073 error_snapshot_id);
2074
Willy Tarreau36b27362018-09-07 19:55:44 +02002075 if (ci_putchk(si_ic(si), &trash) == -1)
2076 goto cant_send;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002077
2078 appctx->ctx.errors.px = proxies_list;
2079 appctx->ctx.errors.bol = 0;
2080 appctx->ctx.errors.ptr = -1;
2081 }
2082
2083 /* we have two inner loops here, one for the proxy, the other one for
2084 * the buffer.
2085 */
2086 while (appctx->ctx.errors.px) {
2087 struct error_snapshot *es;
2088
Willy Tarreau36b27362018-09-07 19:55:44 +02002089 HA_SPIN_LOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
2090
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002091 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002092 es = appctx->ctx.errors.px->invalid_req;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002093 if (appctx->ctx.errors.flag & 2) // skip req
2094 goto next;
2095 }
2096 else {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002097 es = appctx->ctx.errors.px->invalid_rep;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002098 if (appctx->ctx.errors.flag & 4) // skip resp
2099 goto next;
2100 }
2101
Willy Tarreauc55015e2018-09-07 19:02:32 +02002102 if (!es)
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002103 goto next;
2104
2105 if (appctx->ctx.errors.iid >= 0 &&
2106 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
2107 es->oe->uuid != appctx->ctx.errors.iid)
2108 goto next;
2109
2110 if (appctx->ctx.errors.ptr < 0) {
2111 /* just print headers now */
2112
2113 char pn[INET6_ADDRSTRLEN];
2114 struct tm tm;
2115 int port;
2116
2117 get_localtime(es->when.tv_sec, &tm);
2118 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
2119 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2120 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
2121
2122 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
2123 case AF_INET:
2124 case AF_INET6:
2125 port = get_host_port(&es->src);
2126 break;
2127 default:
2128 port = 0;
2129 }
2130
2131 switch (appctx->ctx.errors.flag & 1) {
2132 case 0:
2133 chunk_appendf(&trash,
2134 " frontend %s (#%d): invalid request\n"
2135 " backend %s (#%d)",
2136 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
2137 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
2138 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
2139 break;
2140 case 1:
2141 chunk_appendf(&trash,
2142 " backend %s (#%d): invalid response\n"
2143 " frontend %s (#%d)",
2144 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
2145 es->oe->id, es->oe->uuid);
2146 break;
2147 }
2148
2149 chunk_appendf(&trash,
2150 ", server %s (#%d), event #%u, src %s:%d\n"
2151 " buffer starts at %llu (including %u out), %u free,\n"
2152 " len %u, wraps at %u, error at position %u\n",
2153 es->srv ? es->srv->id : "<NONE>",
2154 es->srv ? es->srv->puid : -1,
2155 es->ev_id, pn, port,
2156 es->buf_ofs, es->buf_out,
2157 global.tune.bufsize - es->buf_out - es->buf_len,
2158 es->buf_len, es->buf_wrap, es->buf_err);
2159
2160 if (es->show)
2161 es->show(&trash, es);
2162
2163 chunk_appendf(&trash, " \n");
2164
Willy Tarreau36b27362018-09-07 19:55:44 +02002165 if (ci_putchk(si_ic(si), &trash) == -1)
2166 goto cant_send_unlock;
2167
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002168 appctx->ctx.errors.ptr = 0;
2169 appctx->ctx.errors.ev_id = es->ev_id;
2170 }
2171
2172 if (appctx->ctx.errors.ev_id != es->ev_id) {
2173 /* the snapshot changed while we were dumping it */
2174 chunk_appendf(&trash,
2175 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau36b27362018-09-07 19:55:44 +02002176 if (ci_putchk(si_ic(si), &trash) == -1)
2177 goto cant_send_unlock;
2178
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002179 goto next;
2180 }
2181
2182 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau4bc7d902018-09-07 20:07:17 +02002183 while (appctx->ctx.errors.ptr < es->buf_len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002184 int newptr;
2185 int newline;
2186
2187 newline = appctx->ctx.errors.bol;
2188 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, appctx->ctx.errors.ptr);
2189 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau36b27362018-09-07 19:55:44 +02002190 goto cant_send_unlock;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002191
Willy Tarreau36b27362018-09-07 19:55:44 +02002192 if (ci_putchk(si_ic(si), &trash) == -1)
2193 goto cant_send_unlock;
2194
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002195 appctx->ctx.errors.ptr = newptr;
2196 appctx->ctx.errors.bol = newline;
2197 };
2198 next:
Willy Tarreau36b27362018-09-07 19:55:44 +02002199 HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002200 appctx->ctx.errors.bol = 0;
2201 appctx->ctx.errors.ptr = -1;
2202 appctx->ctx.errors.flag ^= 1;
2203 if (!(appctx->ctx.errors.flag & 1))
2204 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
2205 }
2206
2207 /* dump complete */
2208 return 1;
Willy Tarreau36b27362018-09-07 19:55:44 +02002209
2210 cant_send_unlock:
2211 HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
2212 cant_send:
Willy Tarreaudb398432018-11-15 11:08:52 +01002213 si_rx_room_blk(si);
Willy Tarreau36b27362018-09-07 19:55:44 +02002214 return 0;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002215}
2216
William Lallemanda6c5f332016-11-19 02:25:36 +01002217/* register cli keywords */
2218static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau15b9e682016-11-24 11:55:28 +01002219 { { "disable", "frontend", NULL }, "disable frontend : temporarily disable specific frontend", cli_parse_disable_frontend, NULL, NULL },
2220 { { "enable", "frontend", NULL }, "enable frontend : re-enable specific frontend", cli_parse_enable_frontend, NULL, NULL },
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002221 { { "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 +01002222 { { "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 +01002223 { { "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 +01002224 { { "shutdown", "frontend", NULL }, "shutdown frontend : stop a specific frontend", cli_parse_shutdown_frontend, NULL, NULL },
Olivier Houchard614f8d72017-03-14 20:08:46 +01002225 { { "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 },
2226 { { "enable", "dynamic-cookie", "backend", NULL }, "enable dynamic-cookie backend : enable dynamic cookies on a specific backend", cli_parse_enable_dyncookie_backend, NULL },
2227 { { "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 +02002228 { { "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 +01002229 {{},}
2230}};
2231
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02002232__attribute__((constructor))
2233static void __proxy_module_init(void)
2234{
2235 cfg_register_keywords(&cfg_kws);
William Lallemanda6c5f332016-11-19 02:25:36 +01002236 cli_register_kw(&cli_kws);
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02002237}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002238
2239/*
2240 * Local variables:
2241 * c-indent-level: 8
2242 * c-basic-offset: 8
2243 * End:
2244 */