blob: 79ba5a679b172f44065490f715711e47b14eff18 [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 Tarreaub2551052020-06-09 09:07:15 +020020#include <import/eb32tree.h>
21#include <import/ebistree.h>
22
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/applet-t.h>
25#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020026#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020027#include <haproxy/cli.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020028#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/fd.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020030#include <haproxy/filters.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020031#include <haproxy/global.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020032#include <haproxy/http_ana.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020033#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020034#include <haproxy/log.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020035#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020036#include <haproxy/peers.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020037#include <haproxy/pool.h>
Willy Tarreau626f3a72020-10-07 16:52:43 +020038#include <haproxy/protocol.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/proto_tcp.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020040#include <haproxy/proxy.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020041#include <haproxy/server-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020042#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020043#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020044#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020045#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020046#include <haproxy/task.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020047#include <haproxy/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
Willy Tarreau918ff602011-07-25 16:33:49 +020050int listeners; /* # of proxy listeners, set by cfgparse */
Olivier Houchardfbc74e82017-11-24 16:54:05 +010051struct proxy *proxies_list = NULL; /* list of all existing proxies */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +020052struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */
Willy Tarreauf79d9502014-03-15 07:22:35 +010053struct eb_root proxy_by_name = EB_ROOT; /* tree of proxies sorted by name */
Willy Tarreau10479e42010-12-12 14:00:34 +010054unsigned int error_snapshot_id = 0; /* global ID assigned to each error then incremented */
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010056/* proxy->options */
57const struct cfg_opt cfg_opts[] =
58{
59 { "abortonclose", PR_O_ABRT_CLOSE, PR_CAP_BE, 0, 0 },
60 { "allbackups", PR_O_USE_ALL_BK, PR_CAP_BE, 0, 0 },
61 { "checkcache", PR_O_CHK_CACHE, PR_CAP_BE, 0, PR_MODE_HTTP },
62 { "clitcpka", PR_O_TCP_CLI_KA, PR_CAP_FE, 0, 0 },
63 { "contstats", PR_O_CONTSTATS, PR_CAP_FE, 0, 0 },
64 { "dontlognull", PR_O_NULLNOLOG, PR_CAP_FE, 0, 0 },
65 { "http_proxy", PR_O_HTTP_PROXY, PR_CAP_FE | PR_CAP_BE, 0, PR_MODE_HTTP },
66 { "http-buffer-request", PR_O_WREQ_BODY, PR_CAP_FE | PR_CAP_BE, 0, PR_MODE_HTTP },
67 { "http-ignore-probes", PR_O_IGNORE_PRB, PR_CAP_FE, 0, PR_MODE_HTTP },
68 { "prefer-last-server", PR_O_PREF_LAST, PR_CAP_BE, 0, PR_MODE_HTTP },
69 { "logasap", PR_O_LOGASAP, PR_CAP_FE, 0, 0 },
70 { "nolinger", PR_O_TCP_NOLING, PR_CAP_FE | PR_CAP_BE, 0, 0 },
71 { "persist", PR_O_PERSIST, PR_CAP_BE, 0, 0 },
72 { "srvtcpka", PR_O_TCP_SRV_KA, PR_CAP_BE, 0, 0 },
Willy Tarreaue5733232019-05-22 19:24:06 +020073#ifdef USE_TPROXY
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010074 { "transparent", PR_O_TRANSP, PR_CAP_BE, 0, 0 },
75#else
76 { "transparent", 0, 0, 0, 0 },
77#endif
78
79 { NULL, 0, 0, 0, 0 }
80};
81
82/* proxy->options2 */
83const struct cfg_opt cfg_opts2[] =
84{
Willy Tarreaue5733232019-05-22 19:24:06 +020085#ifdef USE_LINUX_SPLICE
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010086 { "splice-request", PR_O2_SPLIC_REQ, PR_CAP_FE|PR_CAP_BE, 0, 0 },
87 { "splice-response", PR_O2_SPLIC_RTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
88 { "splice-auto", PR_O2_SPLIC_AUT, PR_CAP_FE|PR_CAP_BE, 0, 0 },
89#else
90 { "splice-request", 0, 0, 0, 0 },
91 { "splice-response", 0, 0, 0, 0 },
92 { "splice-auto", 0, 0, 0, 0 },
93#endif
94 { "accept-invalid-http-request", PR_O2_REQBUG_OK, PR_CAP_FE, 0, PR_MODE_HTTP },
95 { "accept-invalid-http-response", PR_O2_RSPBUG_OK, PR_CAP_BE, 0, PR_MODE_HTTP },
96 { "dontlog-normal", PR_O2_NOLOGNORM, PR_CAP_FE, 0, 0 },
97 { "log-separate-errors", PR_O2_LOGERRORS, PR_CAP_FE, 0, 0 },
98 { "log-health-checks", PR_O2_LOGHCHKS, PR_CAP_BE, 0, 0 },
99 { "socket-stats", PR_O2_SOCKSTAT, PR_CAP_FE, 0, 0 },
100 { "tcp-smart-accept", PR_O2_SMARTACC, PR_CAP_FE, 0, 0 },
101 { "tcp-smart-connect", PR_O2_SMARTCON, PR_CAP_BE, 0, 0 },
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100102 { "independent-streams", PR_O2_INDEPSTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
103 { "http-use-proxy-header", PR_O2_USE_PXHDR, PR_CAP_FE, 0, PR_MODE_HTTP },
104 { "http-pretend-keepalive", PR_O2_FAKE_KA, PR_CAP_BE, 0, PR_MODE_HTTP },
105 { "http-no-delay", PR_O2_NODELAY, PR_CAP_FE|PR_CAP_BE, 0, PR_MODE_HTTP },
Christopher Faulet31930372019-07-15 10:16:58 +0200106 { "http-use-htx", 0, PR_CAP_FE|PR_CAP_BE, 0, 0 }, // deprecated
Christopher Faulet98fbe952019-07-22 16:18:24 +0200107
108 {"h1-case-adjust-bogus-client", PR_O2_H1_ADJ_BUGCLI, PR_CAP_FE, 0, PR_MODE_HTTP },
109 {"h1-case-adjust-bogus-server", PR_O2_H1_ADJ_BUGSRV, PR_CAP_BE, 0, PR_MODE_HTTP },
Christopher Faulet89aed322020-06-02 17:33:56 +0200110 {"disable-h2-upgrade", PR_O2_NO_H2_UPGRADE, PR_CAP_FE, 0, PR_MODE_HTTP },
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100111 { NULL, 0, 0, 0 }
112};
113
Willy Tarreau977b8e42006-12-29 14:19:17 +0100114/*
Willy Tarreau816eb542007-11-04 07:04:43 +0100115 * This function returns a string containing a name describing capabilities to
116 * report comprehensible error messages. Specifically, it will return the words
Christopher Faulet898566e2016-10-26 11:06:28 +0200117 * "frontend", "backend" when appropriate, or "proxy" for all other
Willy Tarreau816eb542007-11-04 07:04:43 +0100118 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +0100119 */
Willy Tarreau816eb542007-11-04 07:04:43 +0100120const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +0100121{
Willy Tarreau816eb542007-11-04 07:04:43 +0100122 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
123 if (cap & PR_CAP_FE)
124 return "frontend";
125 else if (cap & PR_CAP_BE)
126 return "backend";
Willy Tarreau816eb542007-11-04 07:04:43 +0100127 }
128 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +0100129}
130
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100131/*
132 * This function returns a string containing the mode of the proxy in a format
133 * suitable for error messages.
134 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100135const char *proxy_mode_str(int mode) {
136
137 if (mode == PR_MODE_TCP)
138 return "tcp";
139 else if (mode == PR_MODE_HTTP)
140 return "http";
141 else if (mode == PR_MODE_HEALTH)
142 return "health";
William Lallemandcf62f7e2018-10-26 14:47:40 +0200143 else if (mode == PR_MODE_CLI)
144 return "cli";
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100145 else
146 return "unknown";
147}
148
Willy Tarreauf3950172009-10-10 18:35:51 +0200149/*
150 * This function scans the list of backends and servers to retrieve the first
151 * backend and the first server with the given names, and sets them in both
152 * parameters. It returns zero if either is not found, or non-zero and sets
153 * the ones it did not found to NULL. If a NULL pointer is passed for the
154 * backend, only the pointer to the server will be updated.
155 */
156int get_backend_server(const char *bk_name, const char *sv_name,
157 struct proxy **bk, struct server **sv)
158{
159 struct proxy *p;
160 struct server *s;
Willy Tarreau7ecc4202014-03-15 07:57:11 +0100161 int sid;
Willy Tarreauf3950172009-10-10 18:35:51 +0200162
163 *sv = NULL;
164
Willy Tarreau050536d2012-10-04 08:47:34 +0200165 sid = -1;
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200166 if (*sv_name == '#')
167 sid = atoi(sv_name + 1);
168
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200169 p = proxy_be_by_name(bk_name);
Willy Tarreauf3950172009-10-10 18:35:51 +0200170 if (bk)
171 *bk = p;
172 if (!p)
173 return 0;
174
175 for (s = p->srv; s; s = s->next)
Willy Tarreau4055a102012-11-15 00:15:18 +0100176 if ((sid >= 0 && s->puid == sid) ||
177 (sid < 0 && strcmp(s->id, sv_name) == 0))
Willy Tarreauf3950172009-10-10 18:35:51 +0200178 break;
179 *sv = s;
180 if (!s)
181 return 0;
182 return 1;
183}
184
Willy Tarreaue219db72007-12-03 01:30:13 +0100185/* This function parses a "timeout" statement in a proxy section. It returns
186 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200187 * return zero, it will write an error or warning message into a preallocated
188 * buffer returned at <err>. The trailing is not be written. The function must
189 * be called with <args> pointing to the first command line word, with <proxy>
190 * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL.
191 * As a special case for compatibility with older configs, it also accepts
192 * "{cli|srv|con}timeout" in args[0].
Willy Tarreaue219db72007-12-03 01:30:13 +0100193 */
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200194static int proxy_parse_timeout(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200195 struct proxy *defpx, const char *file, int line,
196 char **err)
Willy Tarreaue219db72007-12-03 01:30:13 +0100197{
198 unsigned timeout;
199 int retval, cap;
200 const char *res, *name;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200201 int *tv = NULL;
202 int *td = NULL;
Willy Tarreaue219db72007-12-03 01:30:13 +0100203
204 retval = 0;
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200205
206 /* simply skip "timeout" but remain compatible with old form */
207 if (strcmp(args[0], "timeout") == 0)
208 args++;
209
Willy Tarreaue219db72007-12-03 01:30:13 +0100210 name = args[0];
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200211 if (!strcmp(args[0], "client")) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100212 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100213 tv = &proxy->timeout.client;
214 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100215 cap = PR_CAP_FE;
216 } else if (!strcmp(args[0], "tarpit")) {
217 tv = &proxy->timeout.tarpit;
218 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100219 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100220 } else if (!strcmp(args[0], "http-keep-alive")) {
221 tv = &proxy->timeout.httpka;
222 td = &defpx->timeout.httpka;
223 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau036fae02008-01-06 13:24:40 +0100224 } else if (!strcmp(args[0], "http-request")) {
225 tv = &proxy->timeout.httpreq;
226 td = &defpx->timeout.httpreq;
Willy Tarreaucd7afc02009-07-12 10:03:17 +0200227 cap = PR_CAP_FE | PR_CAP_BE;
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200228 } else if (!strcmp(args[0], "server")) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100229 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100230 tv = &proxy->timeout.server;
231 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100232 cap = PR_CAP_BE;
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200233 } else if (!strcmp(args[0], "connect")) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100234 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100235 tv = &proxy->timeout.connect;
236 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100237 cap = PR_CAP_BE;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100238 } else if (!strcmp(args[0], "check")) {
239 tv = &proxy->timeout.check;
240 td = &defpx->timeout.check;
241 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100242 } else if (!strcmp(args[0], "queue")) {
243 tv = &proxy->timeout.queue;
244 td = &defpx->timeout.queue;
245 cap = PR_CAP_BE;
Willy Tarreauce887fd2012-05-12 12:50:00 +0200246 } else if (!strcmp(args[0], "tunnel")) {
247 tv = &proxy->timeout.tunnel;
248 td = &defpx->timeout.tunnel;
249 cap = PR_CAP_BE;
Willy Tarreau05cdd962014-05-10 14:30:07 +0200250 } else if (!strcmp(args[0], "client-fin")) {
251 tv = &proxy->timeout.clientfin;
252 td = &defpx->timeout.clientfin;
253 cap = PR_CAP_FE;
254 } else if (!strcmp(args[0], "server-fin")) {
255 tv = &proxy->timeout.serverfin;
256 td = &defpx->timeout.serverfin;
257 cap = PR_CAP_BE;
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200258 } else if (!strcmp(args[0], "clitimeout")) {
259 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout client'.", args[0]);
260 return -1;
261 } else if (!strcmp(args[0], "srvtimeout")) {
262 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout server'.", args[0]);
263 return -1;
264 } else if (!strcmp(args[0], "contimeout")) {
265 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout connect'.", args[0]);
266 return -1;
Willy Tarreaue219db72007-12-03 01:30:13 +0100267 } else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200268 memprintf(err,
269 "'timeout' supports 'client', 'server', 'connect', 'check', "
Willy Tarreau05cdd962014-05-10 14:30:07 +0200270 "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', "
271 "'client-fin' and 'server-fin' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200272 args[0]);
Willy Tarreaue219db72007-12-03 01:30:13 +0100273 return -1;
274 }
275
276 if (*args[1] == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200277 memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100278 return -1;
279 }
280
281 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200282 if (res == PARSE_TIME_OVER) {
283 memprintf(err, "timer overflow in argument '%s' to 'timeout %s' (maximum value is 2147483647 ms or ~24.8 days)",
284 args[1], name);
285 return -1;
286 }
287 else if (res == PARSE_TIME_UNDER) {
288 memprintf(err, "timer underflow in argument '%s' to 'timeout %s' (minimum non-null value is 1 ms)",
289 args[1], name);
290 return -1;
291 }
292 else if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200293 memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100294 return -1;
295 }
296
297 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200298 memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability",
299 name, proxy_type_str(proxy), proxy->id,
300 (cap & PR_CAP_BE) ? "backend" : "frontend");
Willy Tarreaue219db72007-12-03 01:30:13 +0100301 retval = 1;
302 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200303 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200304 memprintf(err, "overwriting 'timeout %s' which was already specified", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100305 retval = 1;
306 }
307
Willy Tarreaufac5b592014-05-22 08:24:46 +0200308 if (*args[2] != 0) {
309 memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]);
310 retval = -1;
311 }
312
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200313 *tv = MS_TO_TICKS(timeout);
Willy Tarreaue219db72007-12-03 01:30:13 +0100314 return retval;
315}
316
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100317/* This function parses a "rate-limit" statement in a proxy section. It returns
318 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200319 * return zero, it will write an error or warning message into a preallocated
320 * buffer returned at <err>. The function must be called with <args> pointing
321 * to the first command line word, with <proxy> pointing to the proxy being
322 * parsed, and <defpx> to the default proxy or NULL.
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100323 */
324static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200325 struct proxy *defpx, const char *file, int line,
326 char **err)
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100327{
William Dauchybb9da0b2020-01-16 01:34:27 +0100328 int retval;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200329 char *res;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100330 unsigned int *tv = NULL;
331 unsigned int *td = NULL;
332 unsigned int val;
333
334 retval = 0;
335
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200336 if (strcmp(args[1], "sessions") == 0) {
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200337 tv = &proxy->fe_sps_lim;
338 td = &defpx->fe_sps_lim;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200339 }
340 else {
341 memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100342 return -1;
343 }
344
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200345 if (*args[2] == 0) {
346 memprintf(err, "'%s %s' expects expects an integer value (in sessions/second)", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100347 return -1;
348 }
349
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200350 val = strtoul(args[2], &res, 0);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100351 if (*res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200352 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 +0100353 return -1;
354 }
355
William Dauchybb9da0b2020-01-16 01:34:27 +0100356 if (!(proxy->cap & PR_CAP_FE)) {
357 memprintf(err, "%s %s will be ignored because %s '%s' has no frontend capability",
358 args[0], args[1], proxy_type_str(proxy), proxy->id);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100359 retval = 1;
360 }
361 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200362 memprintf(err, "overwriting %s %s which was already specified", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100363 retval = 1;
364 }
365
366 *tv = val;
367 return retval;
368}
369
Willy Tarreauc35362a2014-04-25 13:58:37 +0200370/* This function parses a "max-keep-alive-queue" statement in a proxy section.
371 * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it
372 * does not return zero, it will write an error or warning message into a
373 * preallocated buffer returned at <err>. The function must be called with
374 * <args> pointing to the first command line word, with <proxy> pointing to
375 * the proxy being parsed, and <defpx> to the default proxy or NULL.
376 */
377static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy,
378 struct proxy *defpx, const char *file, int line,
379 char **err)
380{
381 int retval;
382 char *res;
383 unsigned int val;
384
385 retval = 0;
386
387 if (*args[1] == 0) {
388 memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]);
389 return -1;
390 }
391
392 val = strtol(args[1], &res, 0);
393 if (*res) {
394 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
395 return -1;
396 }
397
398 if (!(proxy->cap & PR_CAP_BE)) {
399 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
400 args[0], proxy_type_str(proxy), proxy->id);
401 retval = 1;
402 }
403
404 /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */
405 proxy->max_ka_queue = val + 1;
406 return retval;
407}
408
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200409/* This function parses a "declare" statement in a proxy section. It returns -1
410 * if there is any error, 1 for warning, otherwise 0. If it does not return zero,
411 * it will write an error or warning message into a preallocated buffer returned
412 * at <err>. The function must be called with <args> pointing to the first command
413 * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the
414 * default proxy or NULL.
415 */
416static int proxy_parse_declare(char **args, int section, struct proxy *curpx,
417 struct proxy *defpx, const char *file, int line,
418 char **err)
419{
420 /* Capture keyword wannot be declared in a default proxy. */
421 if (curpx == defpx) {
Joseph Herlant9edebb82018-11-15 11:50:44 -0800422 memprintf(err, "'%s' not available in default section", args[0]);
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200423 return -1;
424 }
425
Joseph Herlant59dd2952018-11-15 11:46:55 -0800426 /* Capture keywork is only available in frontend. */
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200427 if (!(curpx->cap & PR_CAP_FE)) {
Joseph Herlant9edebb82018-11-15 11:50:44 -0800428 memprintf(err, "'%s' only available in frontend or listen section", args[0]);
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200429 return -1;
430 }
431
432 /* Check mandatory second keyword. */
433 if (!args[1] || !*args[1]) {
434 memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]);
435 return -1;
436 }
437
Joseph Herlant59dd2952018-11-15 11:46:55 -0800438 /* Actually, declare is only available for declaring capture
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200439 * slot, but in the future it can declare maps or variables.
Joseph Herlant59dd2952018-11-15 11:46:55 -0800440 * So, this section permits to check and switch according with
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200441 * the second keyword.
442 */
443 if (strcmp(args[1], "capture") == 0) {
444 char *error = NULL;
445 long len;
446 struct cap_hdr *hdr;
447
448 /* Check the next keyword. */
449 if (!args[2] || !*args[2] ||
450 (strcmp(args[2], "response") != 0 &&
451 strcmp(args[2], "request") != 0)) {
452 memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]);
453 return -1;
454 }
455
456 /* Check the 'len' keyword. */
457 if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) {
458 memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]);
459 return -1;
460 }
461
462 /* Check the length value. */
463 if (!args[4] || !*args[4]) {
464 memprintf(err, "'%s %s': 'len' requires a numeric value that represents the "
465 "capture length",
466 args[0], args[1]);
467 return -1;
468 }
469
470 /* convert the length value. */
471 len = strtol(args[4], &error, 10);
472 if (*error != '\0') {
473 memprintf(err, "'%s %s': cannot parse the length '%s'.",
474 args[0], args[1], args[3]);
475 return -1;
476 }
477
478 /* check length. */
479 if (len <= 0) {
480 memprintf(err, "length must be > 0");
481 return -1;
482 }
483
484 /* register the capture. */
Vincent Bernat02779b62016-04-03 13:48:43 +0200485 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200486 hdr->name = NULL; /* not a header capture */
487 hdr->namelen = 0;
488 hdr->len = len;
489 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
490
491 if (strcmp(args[2], "request") == 0) {
492 hdr->next = curpx->req_cap;
493 hdr->index = curpx->nb_req_cap++;
494 curpx->req_cap = hdr;
495 }
496 if (strcmp(args[2], "response") == 0) {
497 hdr->next = curpx->rsp_cap;
498 hdr->index = curpx->nb_rsp_cap++;
499 curpx->rsp_cap = hdr;
500 }
501 return 0;
502 }
503 else {
504 memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]);
505 return -1;
506 }
507}
508
Olivier Houcharda254a372019-04-05 15:30:12 +0200509/* This function parses a "retry-on" statement */
510static int
511proxy_parse_retry_on(char **args, int section, struct proxy *curpx,
512 struct proxy *defpx, const char *file, int line,
513 char **err)
514{
515 int i;
516
517 if (!(*args[1])) {
518 memprintf(err, "'%s' needs at least one keyword to specify when to retry", args[0]);
519 return -1;
520 }
521 if (!(curpx->cap & PR_CAP_BE)) {
522 memprintf(err, "'%s' only available in backend or listen section", args[0]);
523 return -1;
524 }
525 curpx->retry_type = 0;
526 for (i = 1; *(args[i]); i++) {
527 if (!strcmp(args[i], "conn-failure"))
528 curpx->retry_type |= PR_RE_CONN_FAILED;
529 else if (!strcmp(args[i], "empty-response"))
530 curpx->retry_type |= PR_RE_DISCONNECTED;
531 else if (!strcmp(args[i], "response-timeout"))
532 curpx->retry_type |= PR_RE_TIMEOUT;
533 else if (!strcmp(args[i], "404"))
534 curpx->retry_type |= PR_RE_404;
535 else if (!strcmp(args[i], "408"))
536 curpx->retry_type |= PR_RE_408;
537 else if (!strcmp(args[i], "425"))
538 curpx->retry_type |= PR_RE_425;
539 else if (!strcmp(args[i], "500"))
540 curpx->retry_type |= PR_RE_500;
541 else if (!strcmp(args[i], "501"))
542 curpx->retry_type |= PR_RE_501;
543 else if (!strcmp(args[i], "502"))
544 curpx->retry_type |= PR_RE_502;
545 else if (!strcmp(args[i], "503"))
546 curpx->retry_type |= PR_RE_503;
547 else if (!strcmp(args[i], "504"))
548 curpx->retry_type |= PR_RE_504;
Olivier Houchard865d8392019-05-03 22:46:27 +0200549 else if (!strcmp(args[i], "0rtt-rejected"))
550 curpx->retry_type |= PR_RE_EARLY_ERROR;
Olivier Houcharde3249a92019-05-03 23:01:47 +0200551 else if (!strcmp(args[i], "junk-response"))
552 curpx->retry_type |= PR_RE_JUNK_REQUEST;
Olivier Houchardddf0e032019-05-10 18:05:40 +0200553 else if (!(strcmp(args[i], "all-retryable-errors")))
554 curpx->retry_type |= PR_RE_CONN_FAILED | PR_RE_DISCONNECTED |
555 PR_RE_TIMEOUT | PR_RE_500 | PR_RE_502 |
556 PR_RE_503 | PR_RE_504 | PR_RE_EARLY_ERROR |
557 PR_RE_JUNK_REQUEST;
Olivier Houcharda254a372019-04-05 15:30:12 +0200558 else if (!strcmp(args[i], "none")) {
559 if (i != 1 || *args[i + 1]) {
560 memprintf(err, "'%s' 'none' keyworld only usable alone", args[0]);
561 return -1;
562 }
563 } else {
564 memprintf(err, "'%s': unknown keyword '%s'", args[0], args[i]);
565 return -1;
566 }
567
568 }
569
570
571 return 0;
572}
573
Willy Tarreau52543212020-07-09 05:58:51 +0200574#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900575/* This function parses "{cli|srv}tcpka-cnt" statements */
576static int proxy_parse_tcpka_cnt(char **args, int section, struct proxy *proxy,
577 struct proxy *defpx, const char *file, int line,
578 char **err)
579{
580 int retval;
581 char *res;
582 unsigned int tcpka_cnt;
583
584 retval = 0;
585
586 if (*args[1] == 0) {
587 memprintf(err, "'%s' expects an integer value", args[0]);
588 return -1;
589 }
590
591 tcpka_cnt = strtol(args[1], &res, 0);
592 if (*res) {
593 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
594 return -1;
595 }
596
597 if (!strcmp(args[0], "clitcpka-cnt")) {
598 if (!(proxy->cap & PR_CAP_FE)) {
599 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
600 args[0], proxy_type_str(proxy), proxy->id);
601 retval = 1;
602 }
603 proxy->clitcpka_cnt = tcpka_cnt;
604 } else if (!strcmp(args[0], "srvtcpka-cnt")) {
605 if (!(proxy->cap & PR_CAP_BE)) {
606 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
607 args[0], proxy_type_str(proxy), proxy->id);
608 retval = 1;
609 }
610 proxy->srvtcpka_cnt = tcpka_cnt;
611 } else {
612 /* unreachable */
613 memprintf(err, "'%s': unknown keyword", args[0]);
614 return -1;
615 }
616
617 return retval;
618}
Willy Tarreau52543212020-07-09 05:58:51 +0200619#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900620
Willy Tarreau52543212020-07-09 05:58:51 +0200621#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900622/* This function parses "{cli|srv}tcpka-idle" statements */
623static int proxy_parse_tcpka_idle(char **args, int section, struct proxy *proxy,
624 struct proxy *defpx, const char *file, int line,
625 char **err)
626{
627 int retval;
628 const char *res;
629 unsigned int tcpka_idle;
630
631 retval = 0;
632
633 if (*args[1] == 0) {
634 memprintf(err, "'%s' expects an integer value", args[0]);
635 return -1;
636 }
637 res = parse_time_err(args[1], &tcpka_idle, TIME_UNIT_S);
638 if (res == PARSE_TIME_OVER) {
639 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
640 args[1], args[0]);
641 return -1;
642 }
643 else if (res == PARSE_TIME_UNDER) {
644 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
645 args[1], args[0]);
646 return -1;
647 }
648 else if (res) {
649 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
650 return -1;
651 }
652
653 if (!strcmp(args[0], "clitcpka-idle")) {
654 if (!(proxy->cap & PR_CAP_FE)) {
655 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
656 args[0], proxy_type_str(proxy), proxy->id);
657 retval = 1;
658 }
659 proxy->clitcpka_idle = tcpka_idle;
660 } else if (!strcmp(args[0], "srvtcpka-idle")) {
661 if (!(proxy->cap & PR_CAP_BE)) {
662 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
663 args[0], proxy_type_str(proxy), proxy->id);
664 retval = 1;
665 }
666 proxy->srvtcpka_idle = tcpka_idle;
667 } else {
668 /* unreachable */
669 memprintf(err, "'%s': unknown keyword", args[0]);
670 return -1;
671 }
672
673 return retval;
674}
Willy Tarreau52543212020-07-09 05:58:51 +0200675#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900676
Willy Tarreau52543212020-07-09 05:58:51 +0200677#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900678/* This function parses "{cli|srv}tcpka-intvl" statements */
679static int proxy_parse_tcpka_intvl(char **args, int section, struct proxy *proxy,
680 struct proxy *defpx, const char *file, int line,
681 char **err)
682{
683 int retval;
684 const char *res;
685 unsigned int tcpka_intvl;
686
687 retval = 0;
688
689 if (*args[1] == 0) {
690 memprintf(err, "'%s' expects an integer value", args[0]);
691 return -1;
692 }
693 res = parse_time_err(args[1], &tcpka_intvl, TIME_UNIT_S);
694 if (res == PARSE_TIME_OVER) {
695 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
696 args[1], args[0]);
697 return -1;
698 }
699 else if (res == PARSE_TIME_UNDER) {
700 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
701 args[1], args[0]);
702 return -1;
703 }
704 else if (res) {
705 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
706 return -1;
707 }
708
709 if (!strcmp(args[0], "clitcpka-intvl")) {
710 if (!(proxy->cap & PR_CAP_FE)) {
711 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
712 args[0], proxy_type_str(proxy), proxy->id);
713 retval = 1;
714 }
715 proxy->clitcpka_intvl = tcpka_intvl;
716 } else if (!strcmp(args[0], "srvtcpka-intvl")) {
717 if (!(proxy->cap & PR_CAP_BE)) {
718 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
719 args[0], proxy_type_str(proxy), proxy->id);
720 retval = 1;
721 }
722 proxy->srvtcpka_intvl = tcpka_intvl;
723 } else {
724 /* unreachable */
725 memprintf(err, "'%s': unknown keyword", args[0]);
726 return -1;
727 }
728
729 return retval;
730}
Willy Tarreau52543212020-07-09 05:58:51 +0200731#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900732
Willy Tarreauf79d9502014-03-15 07:22:35 +0100733/* This function inserts proxy <px> into the tree of known proxies. The proxy's
734 * name is used as the storing key so it must already have been initialized.
735 */
736void proxy_store_name(struct proxy *px)
737{
738 px->conf.by_name.key = px->id;
739 ebis_insert(&proxy_by_name, &px->conf.by_name);
740}
741
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200742/* Returns a pointer to the first proxy matching capabilities <cap> and id
743 * <id>. NULL is returned if no match is found. If <table> is non-zero, it
744 * only considers proxies having a table.
Willy Tarreaubc216c42011-08-02 11:25:54 +0200745 */
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200746struct proxy *proxy_find_by_id(int id, int cap, int table)
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200747{
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200748 struct eb32_node *n;
Willy Tarreaubc216c42011-08-02 11:25:54 +0200749
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200750 for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) {
751 struct proxy *px = container_of(n, struct proxy, conf.id);
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100752
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200753 if (px->uuid != id)
754 break;
Alex Williams96532db2009-11-01 21:27:13 -0500755
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200756 if ((px->cap & cap) != cap)
757 continue;
Alex Williams96532db2009-11-01 21:27:13 -0500758
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100759 if (table && (!px->table || !px->table->size))
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200760 continue;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100761
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200762 return px;
763 }
764 return NULL;
765}
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100766
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200767/* Returns a pointer to the first proxy matching either name <name>, or id
768 * <name> if <name> begins with a '#'. NULL is returned if no match is found.
769 * If <table> is non-zero, it only considers proxies having a table.
770 */
771struct proxy *proxy_find_by_name(const char *name, int cap, int table)
772{
773 struct proxy *curproxy;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200774
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200775 if (*name == '#') {
776 curproxy = proxy_find_by_id(atoi(name + 1), cap, table);
777 if (curproxy)
Willy Tarreauc739aa82015-05-26 11:35:41 +0200778 return curproxy;
Alex Williams96532db2009-11-01 21:27:13 -0500779 }
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100780 else {
781 struct ebpt_node *node;
782
783 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
784 curproxy = container_of(node, struct proxy, conf.by_name);
Alex Williams96532db2009-11-01 21:27:13 -0500785
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100786 if (strcmp(curproxy->id, name) != 0)
787 break;
788
789 if ((curproxy->cap & cap) != cap)
790 continue;
791
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100792 if (table && (!curproxy->table || !curproxy->table->size))
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200793 continue;
794
Willy Tarreauc739aa82015-05-26 11:35:41 +0200795 return curproxy;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100796 }
797 }
Willy Tarreauc739aa82015-05-26 11:35:41 +0200798 return NULL;
Alex Williams96532db2009-11-01 21:27:13 -0500799}
800
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200801/* Finds the best match for a proxy with capabilities <cap>, name <name> and id
802 * <id>. At most one of <id> or <name> may be different provided that <cap> is
803 * valid. Either <id> or <name> may be left unspecified (0). The purpose is to
804 * find a proxy based on some information from a previous configuration, across
805 * reloads or during information exchange between peers.
806 *
807 * Names are looked up first if present, then IDs are compared if present. In
808 * case of an inexact match whatever is forced in the configuration has
809 * precedence in the following order :
810 * - 1) forced ID (proves a renaming / change of proxy type)
811 * - 2) proxy name+type (may indicate a move if ID differs)
812 * - 3) automatic ID+type (may indicate a renaming)
813 *
814 * Depending on what is found, we can end up in the following situations :
815 *
816 * name id cap | possible causes
817 * -------------+-----------------
818 * -- -- -- | nothing found
819 * -- -- ok | nothing found
820 * -- ok -- | proxy deleted, ID points to next one
821 * -- ok ok | proxy renamed, or deleted with ID pointing to next one
822 * ok -- -- | proxy deleted, but other half with same name still here (before)
823 * ok -- ok | proxy's ID changed (proxy moved in the config file)
824 * ok ok -- | proxy deleted, but other half with same name still here (after)
825 * ok ok ok | perfect match
826 *
827 * Upon return if <diff> is not NULL, it is zeroed then filled with up to 3 bits :
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200828 * - PR_FBM_MISMATCH_ID : proxy was found but ID differs
829 * (and ID was not zero)
830 * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs
831 * (and name was not NULL)
832 * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with
833 * the same name and/or id
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200834 *
835 * Only a valid proxy is returned. If capabilities do not match, NULL is
836 * returned. The caller can check <diff> to report detailed warnings / errors,
837 * and decide whether or not to use what was found.
838 */
839struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff)
840{
841 struct proxy *byname;
842 struct proxy *byid;
843
844 if (!name && !id)
845 return NULL;
846
847 if (diff)
848 *diff = 0;
849
850 byname = byid = NULL;
851
852 if (name) {
853 byname = proxy_find_by_name(name, cap, 0);
854 if (byname && (!id || byname->uuid == id))
855 return byname;
856 }
857
Joseph Herlant59dd2952018-11-15 11:46:55 -0800858 /* remaining possibilities :
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200859 * - name not set
860 * - name set but not found
861 * - name found, but ID doesn't match.
862 */
863 if (id) {
864 byid = proxy_find_by_id(id, cap, 0);
865 if (byid) {
866 if (byname) {
867 /* id+type found, name+type found, but not all 3.
868 * ID wins only if forced, otherwise name wins.
869 */
870 if (byid->options & PR_O_FORCED_ID) {
871 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200872 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200873 return byid;
874 }
875 else {
876 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200877 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200878 return byname;
879 }
880 }
881
Joseph Herlant59dd2952018-11-15 11:46:55 -0800882 /* remaining possibilities :
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200883 * - name not set
884 * - name set but not found
885 */
886 if (name && diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200887 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200888 return byid;
889 }
890
891 /* ID not found */
892 if (byname) {
893 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200894 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200895 return byname;
896 }
897 }
898
Joseph Herlant59dd2952018-11-15 11:46:55 -0800899 /* All remaining possibilities will lead to NULL. If we can report more
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200900 * detailed information to the caller about changed types and/or name,
901 * we'll do it. For example, we could detect that "listen foo" was
902 * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced.
903 * - name not set, ID not found
904 * - name not found, ID not set
905 * - name not found, ID not found
906 */
907 if (!diff)
908 return NULL;
909
910 if (name) {
911 byname = proxy_find_by_name(name, 0, 0);
912 if (byname && (!id || byname->uuid == id))
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200913 *diff |= PR_FBM_MISMATCH_PROXYTYPE;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200914 }
915
916 if (id) {
917 byid = proxy_find_by_id(id, 0, 0);
918 if (byid) {
919 if (!name)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200920 *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200921 else if (byid->options & PR_O_FORCED_ID)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200922 *diff |= PR_FBM_MISMATCH_NAME | PR_FBM_MISMATCH_PROXYTYPE; /* name and type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200923 /* otherwise it's a different proxy that was returned */
924 }
925 }
926 return NULL;
927}
928
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100930 * This function finds a server with matching name within selected proxy.
931 * It also checks if there are more matching servers with
932 * requested name as this often leads into unexpected situations.
933 */
934
935struct server *findserver(const struct proxy *px, const char *name) {
936
937 struct server *cursrv, *target = NULL;
938
939 if (!px)
940 return NULL;
941
942 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
943 if (strcmp(cursrv->id, name))
944 continue;
945
946 if (!target) {
947 target = cursrv;
948 continue;
949 }
950
Christopher Faulet767a84b2017-11-24 16:50:31 +0100951 ha_alert("Refusing to use duplicated server '%s' found in proxy: %s!\n",
952 name, px->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100953
954 return NULL;
955 }
956
957 return target;
958}
959
Willy Tarreauff01a212009-03-15 13:46:16 +0100960/* This function checks that the designated proxy has no http directives
961 * enabled. It will output a warning if there are, and will fix some of them.
962 * It returns the number of fatal errors encountered. This should be called
963 * at the end of the configuration parsing if the proxy is not in http mode.
964 * The <file> argument is used to construct the error message.
965 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200966int proxy_cfg_ensure_no_http(struct proxy *curproxy)
Willy Tarreauff01a212009-03-15 13:46:16 +0100967{
968 if (curproxy->cookie_name != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100969 ha_warning("config : cookie will be ignored for %s '%s' (needs 'mode http').\n",
970 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100971 }
Willy Tarreauff01a212009-03-15 13:46:16 +0100972 if (curproxy->monitor_uri != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100973 ha_warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n",
974 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100975 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200976 if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) {
Willy Tarreauff01a212009-03-15 13:46:16 +0100977 curproxy->lbprm.algo &= ~BE_LB_ALGO;
978 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Christopher Faulet767a84b2017-11-24 16:50:31 +0100979 ha_warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n",
980 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100981 }
Willy Tarreau17804162009-11-09 21:27:51 +0100982 if (curproxy->to_log & (LW_REQ | LW_RESP)) {
983 curproxy->to_log &= ~(LW_REQ | LW_RESP);
Christopher Faulet767a84b2017-11-24 16:50:31 +0100984 ha_warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n",
985 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
986 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau17804162009-11-09 21:27:51 +0100987 }
Willy Tarreau62a61232013-04-12 18:13:46 +0200988 if (curproxy->conf.logformat_string == default_http_log_format ||
989 curproxy->conf.logformat_string == clf_http_log_format) {
990 /* Note: we don't change the directive's file:line number */
991 curproxy->conf.logformat_string = default_tcp_log_format;
Christopher Faulet767a84b2017-11-24 16:50:31 +0100992 ha_warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
993 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
994 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau196729e2012-05-31 19:30:26 +0200995 }
996
Willy Tarreauff01a212009-03-15 13:46:16 +0100997 return 0;
998}
999
Willy Tarreau237250c2011-07-29 01:49:03 +02001000/* Perform the most basic initialization of a proxy :
1001 * memset(), list_init(*), reset_timeouts(*).
Willy Tarreaub249e842011-09-07 18:41:08 +02001002 * Any new proxy or peer should be initialized via this function.
Willy Tarreau237250c2011-07-29 01:49:03 +02001003 */
1004void init_new_proxy(struct proxy *p)
1005{
1006 memset(p, 0, sizeof(struct proxy));
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001007 p->obj_type = OBJ_TYPE_PROXY;
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001008 p->pendconns = EB_ROOT;
Willy Tarreau237250c2011-07-29 01:49:03 +02001009 LIST_INIT(&p->acl);
1010 LIST_INIT(&p->http_req_rules);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001011 LIST_INIT(&p->http_res_rules);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001012 LIST_INIT(&p->http_after_res_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +02001013 LIST_INIT(&p->redirect_rules);
1014 LIST_INIT(&p->mon_fail_cond);
1015 LIST_INIT(&p->switching_rules);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001016 LIST_INIT(&p->server_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +02001017 LIST_INIT(&p->persist_rules);
1018 LIST_INIT(&p->sticking_rules);
1019 LIST_INIT(&p->storersp_rules);
1020 LIST_INIT(&p->tcp_req.inspect_rules);
1021 LIST_INIT(&p->tcp_rep.inspect_rules);
1022 LIST_INIT(&p->tcp_req.l4_rules);
Willy Tarreau620408f2016-10-21 16:37:51 +02001023 LIST_INIT(&p->tcp_req.l5_rules);
Olivier Houchard859dc802019-08-08 15:47:21 +02001024 MT_LIST_INIT(&p->listener_queue);
William Lallemand0f99e342011-10-12 17:50:54 +02001025 LIST_INIT(&p->logsrvs);
William Lallemand723b73a2012-02-08 16:37:49 +01001026 LIST_INIT(&p->logformat);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001027 LIST_INIT(&p->logformat_sd);
William Lallemanda73203e2012-03-12 12:48:57 +01001028 LIST_INIT(&p->format_unique_id);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001029 LIST_INIT(&p->conf.bind);
Willy Tarreau4348fad2012-09-20 16:48:07 +02001030 LIST_INIT(&p->conf.listeners);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001031 LIST_INIT(&p->conf.errors);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001032 LIST_INIT(&p->conf.args.list);
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001033 LIST_INIT(&p->filter_configs);
Christopher Faulet7a1e2e12020-04-02 18:05:11 +02001034 LIST_INIT(&p->tcpcheck_rules.preset_vars);
Willy Tarreau237250c2011-07-29 01:49:03 +02001035
1036 /* Timeouts are defined as -1 */
1037 proxy_reset_timeouts(p);
1038 p->tcp_rep.inspect_delay = TICK_ETERNITY;
Willy Tarreau050536d2012-10-04 08:47:34 +02001039
1040 /* initial uuid is unassigned (-1) */
1041 p->uuid = -1;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001042
Olivier Houcharda254a372019-04-05 15:30:12 +02001043 /* Default to only allow L4 retries */
1044 p->retry_type = PR_RE_CONN_FAILED;
1045
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001046 HA_SPIN_INIT(&p->lock);
Willy Tarreau237250c2011-07-29 01:49:03 +02001047}
1048
Willy Tarreauacde1522020-10-07 16:31:39 +02001049/* to be called under the proxy lock after stopping some listeners. This will
1050 * automatically update the p->disabled flag after stopping the last one, and
1051 * will emit a log indicating the proxy's condition. The function is idempotent
1052 * so that it will not emit multiple logs; a proxy will be disabled only once.
1053 */
1054void proxy_cond_disable(struct proxy *p)
1055{
1056 if (p->disabled)
1057 return;
1058
1059 if (p->li_ready + p->li_paused > 0)
1060 return;
1061
1062 p->disabled = 1;
1063
1064 if (!(proc_mask(p->bind_proc) & pid_bit))
1065 goto silent;
1066
1067 /* Note: syslog proxies use their own loggers so while it's somewhat OK
1068 * to report them being stopped as a warning, we must not spam their log
1069 * servers which are in fact production servers. For other types (CLI,
1070 * peers, etc) we must not report them at all as they're not really on
1071 * the data plane but on the control plane.
1072 */
1073 if (p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP || p->mode == PR_MODE_SYSLOG)
1074 ha_warning("Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
1075 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
1076
1077 if (p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
1078 send_log(p, LOG_WARNING, "Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
1079 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
1080
1081 silent:
1082 if (p->table && p->table->size && p->table->sync_task)
1083 task_wakeup(p->table->sync_task, TASK_WOKEN_MSG);
1084
1085 if (p->task)
1086 task_wakeup(p->task, TASK_WOKEN_MSG);
1087}
1088
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001089/*
Willy Tarreau918ff602011-07-25 16:33:49 +02001090 * This is the proxy management task. It enables proxies when there are enough
Willy Tarreau87b09662015-04-03 00:22:06 +02001091 * free streams, or stops them when the table is full. It is designed to be
Willy Tarreau918ff602011-07-25 16:33:49 +02001092 * called as a task which is woken up upon stopping or when rate limiting must
1093 * be enforced.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001094 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001095struct task *manage_proxy(struct task *t, void *context, unsigned short state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001096{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001097 struct proxy *p = context;
Willy Tarreau918ff602011-07-25 16:33:49 +02001098 int next = TICK_ETERNITY;
Willy Tarreau79584222009-03-06 09:18:27 +01001099 unsigned int wait;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100
Willy Tarreau918ff602011-07-25 16:33:49 +02001101 /* We should periodically try to enable listeners waiting for a
1102 * global resource here.
1103 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104
Willy Tarreau918ff602011-07-25 16:33:49 +02001105 /* first, let's check if we need to stop the proxy */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001106 if (unlikely(stopping && !p->disabled)) {
Willy Tarreau918ff602011-07-25 16:33:49 +02001107 int t;
1108 t = tick_remain(now_ms, p->stop_time);
1109 if (t == 0) {
Willy Tarreau918ff602011-07-25 16:33:49 +02001110 stop_proxy(p);
1111 /* try to free more memory */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001112 pool_gc(NULL);
Willy Tarreau918ff602011-07-25 16:33:49 +02001113 }
1114 else {
1115 next = tick_first(next, p->stop_time);
1116 }
1117 }
Willy Tarreauf3f8c702011-07-25 07:37:28 +02001118
Willy Tarreau3a925c12013-09-04 17:54:01 +02001119 /* If the proxy holds a stick table, we need to purge all unused
1120 * entries. These are all the ones in the table with ref_cnt == 0
1121 * and all the ones in the pool used to allocate new entries. Any
Willy Tarreau87b09662015-04-03 00:22:06 +02001122 * entry attached to an existing stream waiting for a store will
Willy Tarreau3a925c12013-09-04 17:54:01 +02001123 * be in neither list. Any entry being dumped will have ref_cnt > 0.
1124 * However we protect tables that are being synced to peers.
1125 */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001126 if (unlikely(stopping && p->disabled && p->table && p->table->current)) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001127 if (!p->table->syncing) {
1128 stktable_trash_oldest(p->table, p->table->current);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001129 pool_gc(NULL);
Willy Tarreau3a925c12013-09-04 17:54:01 +02001130 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001131 if (p->table->current) {
Willy Tarreau3a925c12013-09-04 17:54:01 +02001132 /* some entries still remain, let's recheck in one second */
1133 next = tick_first(next, tick_add(now_ms, 1000));
1134 }
1135 }
1136
Willy Tarreau918ff602011-07-25 16:33:49 +02001137 /* the rest below is just for frontends */
1138 if (!(p->cap & PR_CAP_FE))
1139 goto out;
Willy Tarreauf3f8c702011-07-25 07:37:28 +02001140
Willy Tarreau918ff602011-07-25 16:33:49 +02001141 /* check the various reasons we may find to block the frontend */
Willy Tarreau6b3bf732020-09-24 07:35:46 +02001142 if (unlikely(p->feconn >= p->maxconn))
Willy Tarreau918ff602011-07-25 16:33:49 +02001143 goto out;
Willy Tarreau3a7d2072009-03-05 23:48:25 +01001144
Willy Tarreau918ff602011-07-25 16:33:49 +02001145 if (p->fe_sps_lim &&
1146 (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) {
1147 /* we're blocking because a limit was reached on the number of
1148 * requests/s on the frontend. We want to re-check ASAP, which
1149 * means in 1 ms before estimated expiration date, because the
1150 * timer will have settled down.
1151 */
1152 next = tick_first(next, tick_add(now_ms, wait));
1153 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001154 }
Willy Tarreau918ff602011-07-25 16:33:49 +02001155
1156 /* The proxy is not limited so we can re-enable any waiting listener */
Willy Tarreau241797a2019-12-10 14:10:52 +01001157 dequeue_proxy_listeners(p);
Willy Tarreau918ff602011-07-25 16:33:49 +02001158 out:
1159 t->expire = next;
1160 task_queue(t);
1161 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001162}
1163
1164
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001165static int proxy_parse_hard_stop_after(char **args, int section_type, struct proxy *curpx,
1166 struct proxy *defpx, const char *file, int line,
1167 char **err)
1168{
1169 const char *res;
1170
1171 if (!*args[1]) {
1172 memprintf(err, "'%s' expects <time> as argument.\n", args[0]);
1173 return -1;
1174 }
1175 res = parse_time_err(args[1], &global.hard_stop_after, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001176 if (res == PARSE_TIME_OVER) {
1177 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1178 args[1], args[0]);
1179 return -1;
1180 }
1181 else if (res == PARSE_TIME_UNDER) {
1182 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1183 args[1], args[0]);
1184 return -1;
1185 }
1186 else if (res) {
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001187 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
1188 return -1;
1189 }
1190 return 0;
1191}
1192
Olivier Houchard9f6af332018-05-25 14:04:04 +02001193struct task *hard_stop(struct task *t, void *context, unsigned short state)
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001194{
1195 struct proxy *p;
1196 struct stream *s;
1197
1198 if (killed) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001199 ha_warning("Some tasks resisted to hard-stop, exiting now.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001200 send_log(NULL, LOG_WARNING, "Some tasks resisted to hard-stop, exiting now.\n");
Willy Tarreau7067b3a2019-06-02 11:11:29 +02001201 killed = 2;
1202 t->expire = TICK_ETERNITY;
1203 return t;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001204 }
1205
Christopher Faulet767a84b2017-11-24 16:50:31 +01001206 ha_warning("soft-stop running for too long, performing a hard-stop.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001207 send_log(NULL, LOG_WARNING, "soft-stop running for too long, performing a hard-stop.\n");
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001208 p = proxies_list;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001209 while (p) {
1210 if ((p->cap & PR_CAP_FE) && (p->feconn > 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001211 ha_warning("Proxy %s hard-stopped (%d remaining conns will be closed).\n",
1212 p->id, p->feconn);
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001213 send_log(p, LOG_WARNING, "Proxy %s hard-stopped (%d remaining conns will be closed).\n",
1214 p->id, p->feconn);
1215 }
1216 p = p->next;
1217 }
1218 list_for_each_entry(s, &streams, list) {
1219 stream_shutdown(s, SF_ERR_KILLED);
1220 }
1221
1222 killed = 1;
1223 t->expire = tick_add(now_ms, MS_TO_TICKS(1000));
1224 return t;
1225}
1226
Willy Tarreaubaaee002006-06-26 02:48:02 +02001227/*
1228 * this function disables health-check servers so that the process will quickly be ignored
1229 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
1230 * time will not be used since it would already not listen anymore to the socket.
1231 */
1232void soft_stop(void)
1233{
1234 struct proxy *p;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001235 struct task *task;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001236
1237 stopping = 1;
William Dauchy3894d972019-12-28 15:36:02 +01001238 /* disable busy polling to avoid cpu eating for the new process */
1239 global.tune.options &= ~GTUNE_BUSY_POLLING;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001240 if (tick_isset(global.hard_stop_after)) {
Emeric Brunc60def82017-09-27 14:59:38 +02001241 task = task_new(MAX_THREADS_MASK);
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001242 if (task) {
1243 task->process = hard_stop;
1244 task_schedule(task, tick_add(now_ms, global.hard_stop_after));
1245 }
1246 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001247 ha_alert("out of memory trying to allocate the hard-stop task.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001248 }
1249 }
Willy Tarreau626f3a72020-10-07 16:52:43 +02001250
1251 /* stop all stoppable listeners, resulting in disabling all proxies
1252 * that don't use a grace period.
1253 */
1254 protocol_stop_now();
1255
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001256 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001257 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001258 while (p) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02001259 if (!p->disabled) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001260 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 +02001261 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 +02001262 p->stop_time = tick_add(now_ms, p->grace);
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001263
Willy Tarreau20b7afb2015-09-28 16:35:04 +02001264 /* Note: do not wake up stopped proxies' task nor their tables'
1265 * tasks as these ones might point to already released entries.
1266 */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001267 if (p->table && p->table->size && p->table->sync_task)
1268 task_wakeup(p->table->sync_task, TASK_WOKEN_MSG);
Willy Tarreau20b7afb2015-09-28 16:35:04 +02001269
1270 if (p->task)
1271 task_wakeup(p->task, TASK_WOKEN_MSG);
1272 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273 p = p->next;
1274 }
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001275
Willy Tarreaud0807c32010-08-27 18:26:11 +02001276 /* signal zero is used to broadcast the "stopping" event */
1277 signal_handler(0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001278}
1279
1280
Willy Tarreaube58c382011-07-24 18:28:10 +02001281/* Temporarily disables listening on all of the proxy's listeners. Upon
Willy Tarreaua17c91b2020-09-24 07:44:34 +02001282 * success, the proxy enters the PR_PAUSED state. The function returns 0
Willy Tarreauce8fe252011-09-07 19:14:57 +02001283 * if it fails, or non-zero on success.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001284 */
Willy Tarreauce8fe252011-09-07 19:14:57 +02001285int pause_proxy(struct proxy *p)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001286{
1287 struct listener *l;
Willy Tarreauce8fe252011-09-07 19:14:57 +02001288
Willy Tarreauc3914d42020-09-24 08:39:22 +02001289 if (!(p->cap & PR_CAP_FE) || p->disabled || !p->li_ready)
Willy Tarreauce8fe252011-09-07 19:14:57 +02001290 return 1;
1291
Willy Tarreaua17c91b2020-09-24 07:44:34 +02001292 list_for_each_entry(l, &p->conf.listeners, by_fe)
1293 pause_listener(l);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001294
Willy Tarreaua17c91b2020-09-24 07:44:34 +02001295 if (p->li_ready) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001296 ha_warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001297 send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
1298 return 0;
1299 }
Willy Tarreauce8fe252011-09-07 19:14:57 +02001300 return 1;
Willy Tarreauda250db2008-10-12 12:07:48 +02001301}
1302
Willy Tarreauda250db2008-10-12 12:07:48 +02001303/*
1304 * This function completely stops a proxy and releases its listeners. It has
1305 * to be called when going down in order to release the ports so that another
1306 * process may bind to them. It must also be called on disabled proxies at the
William Lallemandc59f9882018-11-16 16:57:21 +01001307 * end of start-up. If all listeners are closed, the proxy is set to the
Willy Tarreau3de3cd42019-07-24 17:42:44 +02001308 * PR_STSTOPPED state. The function takes the proxy's lock so it's safe to
1309 * call from multiple places.
Willy Tarreauda250db2008-10-12 12:07:48 +02001310 */
1311void stop_proxy(struct proxy *p)
1312{
1313 struct listener *l;
1314
Willy Tarreau3de3cd42019-07-24 17:42:44 +02001315 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
1316
Willy Tarreau322b9b92020-10-07 16:20:34 +02001317 list_for_each_entry(l, &p->conf.listeners, by_fe)
1318 stop_listener(l, 1, 0, 0);
Willy Tarreau3de3cd42019-07-24 17:42:44 +02001319
1320 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001321}
1322
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001323/* This function resumes listening on the specified proxy. It scans all of its
1324 * listeners and tries to enable them all. If any of them fails, the proxy is
1325 * put back to the paused state. It returns 1 upon success, or zero if an error
1326 * is encountered.
1327 */
1328int resume_proxy(struct proxy *p)
1329{
1330 struct listener *l;
1331 int fail;
1332
Willy Tarreauc3914d42020-09-24 08:39:22 +02001333 if (p->disabled || !p->li_paused)
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001334 return 1;
1335
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001336 fail = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001337 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001338 if (!resume_listener(l)) {
1339 int port;
1340
Willy Tarreau37159062020-08-27 07:48:42 +02001341 port = get_host_port(&l->rx.addr);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001342 if (port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001343 ha_warning("Port %d busy while trying to enable %s %s.\n",
1344 port, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001345 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
1346 port, proxy_cap_str(p->cap), p->id);
1347 }
1348 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001349 ha_warning("Bind on socket %d busy while trying to enable %s %s.\n",
1350 l->luid, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001351 send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n",
1352 l->luid, proxy_cap_str(p->cap), p->id);
1353 }
1354
1355 /* Another port might have been enabled. Let's stop everything. */
1356 fail = 1;
1357 break;
1358 }
1359 }
1360
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001361 if (fail) {
1362 pause_proxy(p);
1363 return 0;
1364 }
1365 return 1;
1366}
1367
Willy Tarreau87b09662015-04-03 00:22:06 +02001368/* Set current stream's backend to <be>. Nothing is done if the
1369 * stream already had a backend assigned, which is indicated by
Willy Tarreaue7dff022015-04-03 01:14:29 +02001370 * s->flags & SF_BE_ASSIGNED.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001371 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001372 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001373 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001374int stream_set_backend(struct stream *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001375{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001376 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001377 return 1;
Christopher Faulet41179042016-06-21 11:54:52 +02001378
1379 if (flt_set_stream_backend(s, be) < 0)
1380 return 0;
1381
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001382 s->be = be;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001383 HA_ATOMIC_UPDATE_MAX(&be->be_counters.conn_max,
1384 HA_ATOMIC_ADD(&be->beconn, 1));
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001385 proxy_inc_be_ctr(be);
1386
Willy Tarreau87b09662015-04-03 00:22:06 +02001387 /* assign new parameters to the stream from the new backend */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001388 s->si[1].flags &= ~SI_FL_INDEP_STR;
1389 if (be->options2 & PR_O2_INDEPSTR)
1390 s->si[1].flags |= SI_FL_INDEP_STR;
1391
Hongbo Longe39683c2017-03-10 18:41:51 +01001392 if (tick_isset(be->timeout.serverfin))
1393 s->si[1].hcto = be->timeout.serverfin;
1394
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001395 /* We want to enable the backend-specific analysers except those which
1396 * were already run as part of the frontend/listener. Note that it would
1397 * be more reliable to store the list of analysers that have been run,
1398 * but what we do here is OK for now.
1399 */
Christopher Faulet70e2f272017-01-09 16:33:19 +01001400 s->req.analysers |= be->be_req_ana & ~(strm_li(s) ? strm_li(s)->analysers : 0);
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001401
Willy Tarreau51aecc72009-07-12 09:47:04 +02001402 /* If the target backend requires HTTP processing, we have to allocate
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001403 * the HTTP transaction if we did not have one.
Willy Tarreau51aecc72009-07-12 09:47:04 +02001404 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001405 if (unlikely(!s->txn && be->http_needed)) {
1406 if (unlikely(!http_alloc_txn(s)))
Willy Tarreau51aecc72009-07-12 09:47:04 +02001407 return 0; /* not enough memory */
Willy Tarreau39e4f622010-05-31 17:01:36 +02001408
1409 /* and now initialize the HTTP transaction state */
1410 http_init_txn(s);
Willy Tarreau51aecc72009-07-12 09:47:04 +02001411 }
1412
Christopher Faulet309c6412015-12-02 09:57:32 +01001413 /* Be sure to filter request headers if the backend is an HTTP proxy and
1414 * if there are filters attached to the stream. */
1415 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01001416 s->req.analysers |= AN_REQ_FLT_HTTP_HDRS;
Christopher Faulet309c6412015-12-02 09:57:32 +01001417
Willy Tarreaueee5b512015-04-03 23:46:31 +02001418 if (s->txn) {
Christopher Fauletbbe68542019-04-08 10:53:51 +02001419 /* If we chain a TCP frontend to an HTX backend, we must upgrade
1420 * the client mux */
Christopher Faulet60d29b32019-07-15 15:00:25 +02001421 if (!IS_HTX_STRM(s) && be->mode == PR_MODE_HTTP) {
Christopher Fauletbbe68542019-04-08 10:53:51 +02001422 struct connection *conn = objt_conn(strm_sess(s)->origin);
1423 struct conn_stream *cs = objt_cs(s->si[0].end);
1424
1425 if (conn && cs) {
1426 si_rx_endp_more(&s->si[0]);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +02001427 /* Make sure we're unsubscribed, the the new
1428 * mux will probably want to subscribe to
1429 * the underlying XPRT
1430 */
1431 if (s->si[0].wait_event.events)
1432 conn->mux->unsubscribe(cs, s->si[0].wait_event.events,
1433 &s->si[0].wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +02001434 if (conn_upgrade_mux_fe(conn, cs, &s->req.buf, ist(""), PROTO_MODE_HTTP) == -1)
Christopher Fauletbbe68542019-04-08 10:53:51 +02001435 return 0;
Olivier Houchard4c18f942019-07-31 18:05:26 +02001436 if (!strcmp(conn->mux->name, "H2")) {
1437 /* For HTTP/2, destroy the conn_stream,
1438 * disable logging, and pretend that we
1439 * failed, to that the stream is
1440 * silently destroyed. The new mux
1441 * will create new streams.
1442 */
Olivier Houchard59dd06d2019-08-09 18:01:15 +02001443 cs_free(cs);
Olivier Houchard4c18f942019-07-31 18:05:26 +02001444 si_detach_endpoint(&s->si[0]);
1445 s->logs.logwait = 0;
1446 s->logs.level = 0;
1447 s->flags |= SF_IGNORE;
1448 return 0;
1449 }
Olivier Houchard71b20c22019-08-09 17:50:05 +02001450 s->flags |= SF_HTX;
Christopher Fauletbbe68542019-04-08 10:53:51 +02001451 }
1452 }
Christopher Fauleteec7f8a2019-12-20 15:59:20 +01001453 else if (IS_HTX_STRM(s) && be->mode != PR_MODE_HTTP) {
1454 /* If a TCP backend is assgiend to an HTX stream, return
1455 * an error. It may happens for a new stream on a
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001456 * previously upgraded connections. */
Christopher Fauleteec7f8a2019-12-20 15:59:20 +01001457 if (!(s->flags & SF_ERR_MASK))
1458 s->flags |= SF_ERR_INTERNAL;
1459 return 0;
1460 }
Christopher Fauletbbe68542019-04-08 10:53:51 +02001461
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001462 /* we may request to parse a request body */
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001463 if (be->options & PR_O_WREQ_BODY)
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001464 s->req.analysers |= AN_REQ_HTTP_BODY;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001465 }
1466
1467 s->flags |= SF_BE_ASSIGNED;
Willy Tarreau96e31212011-05-30 18:10:30 +02001468 if (be->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001469 s->req.flags |= CF_NEVER_WAIT;
1470 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02001471 }
1472
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001473 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001474}
1475
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001476/* Capture a bad request or response and archive it in the proxy's structure.
1477 * It is relatively protocol-agnostic so it requires that a number of elements
1478 * are passed :
1479 * - <proxy> is the proxy where the error was detected and where the snapshot
1480 * needs to be stored
Joseph Herlant59dd2952018-11-15 11:46:55 -08001481 * - <is_back> indicates that the error happened when receiving the response
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001482 * - <other_end> is a pointer to the proxy on the other side when known
1483 * - <target> is the target of the connection, usually a server or a proxy
1484 * - <sess> is the session which experienced the error
1485 * - <ctx> may be NULL or should contain any info relevant to the protocol
1486 * - <buf> is the buffer containing the offending data
1487 * - <buf_ofs> is the position of this buffer's input data in the input
1488 * stream, starting at zero. It may be passed as zero if unknown.
1489 * - <buf_out> is the portion of <buf->data> which was already forwarded and
1490 * which precedes the buffer's input. The buffer's input starts at
1491 * buf->head + buf_out.
1492 * - <err_pos> is the pointer to the faulty byte in the buffer's input.
1493 * - <show> is the callback to use to display <ctx>. It may be NULL.
1494 */
1495void proxy_capture_error(struct proxy *proxy, int is_back,
1496 struct proxy *other_end, enum obj_type *target,
1497 const struct session *sess,
1498 const struct buffer *buf, long buf_ofs,
1499 unsigned int buf_out, unsigned int err_pos,
1500 const union error_snapshot_ctx *ctx,
1501 void (*show)(struct buffer *, const struct error_snapshot *))
1502{
1503 struct error_snapshot *es;
1504 unsigned int buf_len;
1505 int len1, len2;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001506 unsigned int ev_id;
1507
1508 ev_id = HA_ATOMIC_XADD(&error_snapshot_id, 1);
1509
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001510 buf_len = b_data(buf) - buf_out;
1511
1512 es = malloc(sizeof(*es) + buf_len);
Willy Tarreauc55015e2018-09-07 19:02:32 +02001513 if (!es)
1514 return;
1515
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001516 es->buf_len = buf_len;
1517 es->ev_id = ev_id;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001518
Christopher Faulet47a72102020-01-06 11:37:00 +01001519 len1 = b_size(buf) - b_peek_ofs(buf, buf_out);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001520 if (len1 > buf_len)
1521 len1 = buf_len;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001522
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001523 if (len1) {
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001524 memcpy(es->buf, b_peek(buf, buf_out), len1);
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001525 len2 = buf_len - len1;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001526 if (len2)
1527 memcpy(es->buf + len1, b_orig(buf), len2);
1528 }
1529
1530 es->buf_err = err_pos;
1531 es->when = date; // user-visible date
1532 es->srv = objt_server(target);
1533 es->oe = other_end;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001534 if (sess && objt_conn(sess->origin) && conn_get_src(__objt_conn(sess->origin)))
Willy Tarreau026efc72019-07-17 15:20:02 +02001535 es->src = *__objt_conn(sess->origin)->src;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001536 else
1537 memset(&es->src, 0, sizeof(es->src));
1538
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001539 es->buf_wrap = b_wrap(buf) - b_peek(buf, buf_out);
1540 es->buf_out = buf_out;
1541 es->buf_ofs = buf_ofs;
1542
1543 /* be sure to indicate the offset of the first IN byte */
1544 if (es->buf_ofs >= es->buf_len)
1545 es->buf_ofs -= es->buf_len;
1546 else
1547 es->buf_ofs = 0;
1548
1549 /* protocol-specific part now */
1550 if (ctx)
1551 es->ctx = *ctx;
1552 else
1553 memset(&es->ctx, 0, sizeof(es->ctx));
1554 es->show = show;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001555
1556 /* note: we still lock since we have to be certain that nobody is
1557 * dumping the output while we free.
1558 */
1559 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
1560 if (is_back) {
1561 es = HA_ATOMIC_XCHG(&proxy->invalid_rep, es);
1562 } else {
1563 es = HA_ATOMIC_XCHG(&proxy->invalid_req, es);
1564 }
1565 free(es);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001566 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
1567}
1568
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001569/* Configure all proxies which lack a maxconn setting to use the global one by
1570 * default. This avoids the common mistake consisting in setting maxconn only
1571 * in the global section and discovering the hard way that it doesn't propagate
1572 * through the frontends. These values are also propagated through the various
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001573 * targeted backends, whose fullconn is finally calculated if not yet set.
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001574 */
1575void proxy_adjust_all_maxconn()
1576{
1577 struct proxy *curproxy;
1578 struct switching_rule *swrule1, *swrule2;
1579
1580 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02001581 if (curproxy->disabled)
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001582 continue;
1583
1584 if (!(curproxy->cap & PR_CAP_FE))
1585 continue;
1586
1587 if (!curproxy->maxconn)
1588 curproxy->maxconn = global.maxconn;
1589
1590 /* update the target backend's fullconn count : default_backend */
1591 if (curproxy->defbe.be)
1592 curproxy->defbe.be->tot_fe_maxconn += curproxy->maxconn;
1593 else if ((curproxy->cap & PR_CAP_LISTEN) == PR_CAP_LISTEN)
1594 curproxy->tot_fe_maxconn += curproxy->maxconn;
1595
1596 list_for_each_entry(swrule1, &curproxy->switching_rules, list) {
1597 /* For each target of switching rules, we update their
1598 * tot_fe_maxconn, except if a previous rule points to
1599 * the same backend or to the default backend.
1600 */
1601 if (swrule1->be.backend != curproxy->defbe.be) {
Frédéric Lécaille2365fb02019-03-07 15:02:52 +01001602 /* note: swrule1->be.backend isn't a backend if the rule
1603 * is dynamic, it's an expression instead, so it must not
1604 * be dereferenced as a backend before being certain it is.
1605 */
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001606 list_for_each_entry(swrule2, &curproxy->switching_rules, list) {
1607 if (swrule2 == swrule1) {
Frédéric Lécaille2365fb02019-03-07 15:02:52 +01001608 if (!swrule1->dynamic)
1609 swrule1->be.backend->tot_fe_maxconn += curproxy->maxconn;
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001610 break;
1611 }
1612 else if (!swrule2->dynamic && swrule2->be.backend == swrule1->be.backend) {
1613 /* there are multiple refs of this backend */
1614 break;
1615 }
1616 }
1617 }
1618 }
1619 }
1620
1621 /* automatically compute fullconn if not set. We must not do it in the
1622 * loop above because cross-references are not yet fully resolved.
1623 */
1624 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02001625 if (curproxy->disabled)
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001626 continue;
1627
1628 /* If <fullconn> is not set, let's set it to 10% of the sum of
1629 * the possible incoming frontend's maxconns.
1630 */
1631 if (!curproxy->fullconn && (curproxy->cap & PR_CAP_BE)) {
1632 /* we have the sum of the maxconns in <total>. We only
1633 * keep 10% of that sum to set the default fullconn, with
1634 * a hard minimum of 1 (to avoid a divide by zero).
1635 */
1636 curproxy->fullconn = (curproxy->tot_fe_maxconn + 9) / 10;
1637 if (!curproxy->fullconn)
1638 curproxy->fullconn = 1;
1639 }
1640 }
1641}
1642
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001643/* Config keywords below */
1644
Willy Tarreaudc13c112013-06-21 23:16:39 +02001645static struct cfg_kw_list cfg_kws = {ILH, {
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001646 { CFG_GLOBAL, "hard-stop-after", proxy_parse_hard_stop_after },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001647 { CFG_LISTEN, "timeout", proxy_parse_timeout },
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +02001648 { CFG_LISTEN, "clitimeout", proxy_parse_timeout }, /* This keyword actually fails to parse, this line remains for better error messages. */
1649 { CFG_LISTEN, "contimeout", proxy_parse_timeout }, /* This keyword actually fails to parse, this line remains for better error messages. */
1650 { CFG_LISTEN, "srvtimeout", proxy_parse_timeout }, /* This keyword actually fails to parse, this line remains for better error messages. */
Willy Tarreau3a7d2072009-03-05 23:48:25 +01001651 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreauc35362a2014-04-25 13:58:37 +02001652 { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue },
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +02001653 { CFG_LISTEN, "declare", proxy_parse_declare },
Olivier Houcharda254a372019-04-05 15:30:12 +02001654 { CFG_LISTEN, "retry-on", proxy_parse_retry_on },
Willy Tarreau52543212020-07-09 05:58:51 +02001655#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001656 { CFG_LISTEN, "clitcpka-cnt", proxy_parse_tcpka_cnt },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001657 { CFG_LISTEN, "srvtcpka-cnt", proxy_parse_tcpka_cnt },
Willy Tarreau52543212020-07-09 05:58:51 +02001658#endif
1659#ifdef TCP_KEEPIDLE
1660 { CFG_LISTEN, "clitcpka-idle", proxy_parse_tcpka_idle },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001661 { CFG_LISTEN, "srvtcpka-idle", proxy_parse_tcpka_idle },
Willy Tarreau52543212020-07-09 05:58:51 +02001662#endif
1663#ifdef TCP_KEEPINTVL
1664 { CFG_LISTEN, "clitcpka-intvl", proxy_parse_tcpka_intvl },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001665 { CFG_LISTEN, "srvtcpka-intvl", proxy_parse_tcpka_intvl },
Willy Tarreau52543212020-07-09 05:58:51 +02001666#endif
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001667 { 0, NULL, NULL },
1668}};
1669
Willy Tarreau0108d902018-11-25 19:14:37 +01001670INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1671
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001672/* Expects to find a frontend named <arg> and returns it, otherwise displays various
1673 * adequate error messages and returns NULL. This function is designed to be used by
1674 * functions requiring a frontend on the CLI.
1675 */
1676struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg)
1677{
1678 struct proxy *px;
1679
1680 if (!*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001681 cli_err(appctx, "A frontend name is expected.\n");
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001682 return NULL;
1683 }
1684
1685 px = proxy_fe_by_name(arg);
1686 if (!px) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001687 cli_err(appctx, "No such frontend.\n");
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001688 return NULL;
1689 }
1690 return px;
1691}
1692
Olivier Houchard614f8d72017-03-14 20:08:46 +01001693/* Expects to find a backend named <arg> and returns it, otherwise displays various
1694 * adequate error messages and returns NULL. This function is designed to be used by
1695 * functions requiring a frontend on the CLI.
1696 */
1697struct proxy *cli_find_backend(struct appctx *appctx, const char *arg)
1698{
1699 struct proxy *px;
1700
1701 if (!*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001702 cli_err(appctx, "A backend name is expected.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01001703 return NULL;
1704 }
1705
1706 px = proxy_be_by_name(arg);
1707 if (!px) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001708 cli_err(appctx, "No such backend.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01001709 return NULL;
1710 }
1711 return px;
1712}
1713
1714
Willy Tarreau69f591e2020-07-01 07:00:59 +02001715/* parse a "show servers [state|conn]" CLI line, returns 0 if it wants to start
1716 * the dump or 1 if it stops immediately. If an argument is specified, it will
1717 * set the proxy pointer into cli.p0 and its ID into cli.i0. It sets cli.o0 to
1718 * 0 for "state", or 1 for "conn".
William Lallemanda6c5f332016-11-19 02:25:36 +01001719 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001720static int cli_parse_show_servers(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemanda6c5f332016-11-19 02:25:36 +01001721{
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001722 struct proxy *px;
William Lallemanda6c5f332016-11-19 02:25:36 +01001723
Willy Tarreau69f591e2020-07-01 07:00:59 +02001724 appctx->ctx.cli.o0 = *args[2] == 'c'; // "conn" vs "state"
1725
William Lallemanda6c5f332016-11-19 02:25:36 +01001726 /* check if a backend name has been provided */
1727 if (*args[3]) {
1728 /* read server state from local file */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001729 px = proxy_be_by_name(args[3]);
William Lallemanda6c5f332016-11-19 02:25:36 +01001730
Willy Tarreau9d008692019-08-09 11:21:01 +02001731 if (!px)
1732 return cli_err(appctx, "Can't find backend.\n");
1733
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001734 appctx->ctx.cli.p0 = px;
1735 appctx->ctx.cli.i0 = px->uuid;
William Lallemanda6c5f332016-11-19 02:25:36 +01001736 }
1737 return 0;
1738}
1739
Willy Tarreau6ff81432020-07-01 07:02:42 +02001740/* dumps server state information for all the servers found in backend cli.p0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001741 * These information are all the parameters which may change during HAProxy runtime.
1742 * By default, we only export to the last known server state file format.
1743 * These information can be used at next startup to recover same level of server state.
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001744 * It uses the proxy pointer from cli.p0, the proxy's id from cli.i0 and the server's
1745 * pointer from cli.p1.
William Lallemanda6c5f332016-11-19 02:25:36 +01001746 */
Willy Tarreau6ff81432020-07-01 07:02:42 +02001747static int dump_servers_state(struct stream_interface *si)
William Lallemanda6c5f332016-11-19 02:25:36 +01001748{
1749 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001750 struct proxy *px = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001751 struct server *srv;
1752 char srv_addr[INET6_ADDRSTRLEN + 1];
1753 time_t srv_time_since_last_change;
1754 int bk_f_forced_id, srv_f_forced_id;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001755 char *srvrecord;
William Lallemanda6c5f332016-11-19 02:25:36 +01001756
William Lallemanda6c5f332016-11-19 02:25:36 +01001757 /* we don't want to report any state if the backend is not enabled on this process */
Willy Tarreau6daac192019-02-02 17:39:53 +01001758 if (!(proc_mask(px->bind_proc) & pid_bit))
William Lallemanda6c5f332016-11-19 02:25:36 +01001759 return 1;
1760
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001761 if (!appctx->ctx.cli.p1)
1762 appctx->ctx.cli.p1 = px->srv;
William Lallemanda6c5f332016-11-19 02:25:36 +01001763
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001764 for (; appctx->ctx.cli.p1 != NULL; appctx->ctx.cli.p1 = srv->next) {
1765 srv = appctx->ctx.cli.p1;
William Lallemanda6c5f332016-11-19 02:25:36 +01001766 srv_addr[0] = '\0';
1767
1768 switch (srv->addr.ss_family) {
1769 case AF_INET:
1770 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
1771 srv_addr, INET_ADDRSTRLEN + 1);
1772 break;
1773 case AF_INET6:
1774 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
1775 srv_addr, INET6_ADDRSTRLEN + 1);
1776 break;
Daniel Corbett9215ffa2018-05-19 19:43:24 -04001777 default:
1778 memcpy(srv_addr, "-\0", 2);
1779 break;
William Lallemanda6c5f332016-11-19 02:25:36 +01001780 }
1781 srv_time_since_last_change = now.tv_sec - srv->last_change;
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001782 bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001783 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
1784
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001785 srvrecord = NULL;
1786 if (srv->srvrq && srv->srvrq->name)
1787 srvrecord = srv->srvrq->name;
1788
Willy Tarreau69f591e2020-07-01 07:00:59 +02001789 if (appctx->ctx.cli.o0 == 0) {
1790 /* show servers state */
1791 chunk_printf(&trash,
1792 "%d %s "
1793 "%d %s %s "
1794 "%d %d %d %d %ld "
1795 "%d %d %d %d %d "
1796 "%d %d %s %u %s"
1797 "\n",
1798 px->uuid, px->id,
1799 srv->puid, srv->id, srv_addr,
1800 srv->cur_state, srv->cur_admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
1801 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
1802 bk_f_forced_id, srv_f_forced_id, srv->hostname ? srv->hostname : "-", srv->svc_port,
1803 srvrecord ? srvrecord : "-");
1804 } else {
1805 /* show servers conn */
1806 int thr;
1807
1808 chunk_printf(&trash,
1809 "%s/%s %d/%d %s %u - %u %u %u %u %u %u %d %u",
1810 px->id, srv->id, px->uuid, srv->puid, srv_addr,srv->svc_port,
1811 srv->pool_purge_delay,
1812 srv->curr_used_conns, srv->max_used_conns, srv->est_need_conns,
1813 srv->curr_idle_nb, srv->curr_safe_nb, (int)srv->max_idle_conns, srv->curr_idle_conns);
1814
Willy Tarreau42abe682020-07-02 15:19:57 +02001815 for (thr = 0; thr < global.nbthread && srv->curr_idle_thr; thr++)
Willy Tarreau69f591e2020-07-01 07:00:59 +02001816 chunk_appendf(&trash, " %u", srv->curr_idle_thr[thr]);
1817
1818 chunk_appendf(&trash, "\n");
1819 }
1820
Willy Tarreau06d80a92017-10-19 14:32:15 +02001821 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001822 si_rx_room_blk(si);
William Lallemanda6c5f332016-11-19 02:25:36 +01001823 return 0;
1824 }
1825 }
1826 return 1;
1827}
1828
1829/* Parses backend list or simply use backend name provided by the user to return
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001830 * states of servers to stdout. It dumps proxy <cli.p0> and stops if <cli.i0> is
1831 * non-null.
William Lallemanda6c5f332016-11-19 02:25:36 +01001832 */
1833static int cli_io_handler_servers_state(struct appctx *appctx)
1834{
1835 struct stream_interface *si = appctx->owner;
William Lallemanda6c5f332016-11-19 02:25:36 +01001836 struct proxy *curproxy;
1837
1838 chunk_reset(&trash);
1839
1840 if (appctx->st2 == STAT_ST_INIT) {
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001841 if (!appctx->ctx.cli.p0)
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001842 appctx->ctx.cli.p0 = proxies_list;
William Lallemanda6c5f332016-11-19 02:25:36 +01001843 appctx->st2 = STAT_ST_HEAD;
1844 }
1845
1846 if (appctx->st2 == STAT_ST_HEAD) {
Willy Tarreau69f591e2020-07-01 07:00:59 +02001847 if (appctx->ctx.cli.o0 == 0)
1848 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
1849 else
1850 chunk_printf(&trash,
1851 "# bkname/svname bkid/svid addr port - purge_delay used_cur used_max need_est unsafe_nb safe_nb idle_lim idle_cur idle_per_thr[%d]\n",
1852 global.nbthread);
1853
Willy Tarreau06d80a92017-10-19 14:32:15 +02001854 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001855 si_rx_room_blk(si);
William Lallemanda6c5f332016-11-19 02:25:36 +01001856 return 0;
1857 }
1858 appctx->st2 = STAT_ST_INFO;
1859 }
1860
1861 /* STAT_ST_INFO */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001862 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1863 curproxy = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001864 /* servers are only in backends */
1865 if (curproxy->cap & PR_CAP_BE) {
Willy Tarreau6ff81432020-07-01 07:02:42 +02001866 if (!dump_servers_state(si))
William Lallemanda6c5f332016-11-19 02:25:36 +01001867 return 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001868 }
1869 /* only the selected proxy is dumped */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001870 if (appctx->ctx.cli.i0)
William Lallemanda6c5f332016-11-19 02:25:36 +01001871 break;
1872 }
1873
1874 return 1;
1875}
1876
Willy Tarreau608ea592016-12-16 18:01:15 +01001877/* Parses backend list and simply report backend names. It keeps the proxy
1878 * pointer in cli.p0.
1879 */
William Lallemand933efcd2016-11-22 12:34:16 +01001880static int cli_io_handler_show_backend(struct appctx *appctx)
1881{
William Lallemand933efcd2016-11-22 12:34:16 +01001882 struct stream_interface *si = appctx->owner;
1883 struct proxy *curproxy;
1884
1885 chunk_reset(&trash);
1886
Willy Tarreau608ea592016-12-16 18:01:15 +01001887 if (!appctx->ctx.cli.p0) {
William Lallemand933efcd2016-11-22 12:34:16 +01001888 chunk_printf(&trash, "# name\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001889 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001890 si_rx_room_blk(si);
William Lallemand933efcd2016-11-22 12:34:16 +01001891 return 0;
1892 }
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001893 appctx->ctx.cli.p0 = proxies_list;
William Lallemand933efcd2016-11-22 12:34:16 +01001894 }
1895
Willy Tarreau608ea592016-12-16 18:01:15 +01001896 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1897 curproxy = appctx->ctx.cli.p0;
William Lallemand933efcd2016-11-22 12:34:16 +01001898
1899 /* looking for backends only */
1900 if (!(curproxy->cap & PR_CAP_BE))
1901 continue;
1902
1903 /* we don't want to list a backend which is bound to this process */
Willy Tarreau6daac192019-02-02 17:39:53 +01001904 if (!(proc_mask(curproxy->bind_proc) & pid_bit))
William Lallemand933efcd2016-11-22 12:34:16 +01001905 continue;
1906
1907 chunk_appendf(&trash, "%s\n", curproxy->id);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001908 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001909 si_rx_room_blk(si);
William Lallemand933efcd2016-11-22 12:34:16 +01001910 return 0;
1911 }
1912 }
1913
1914 return 1;
1915}
William Lallemanda6c5f332016-11-19 02:25:36 +01001916
Willy Tarreaua275a372018-08-21 14:50:44 +02001917/* Parses the "enable dynamic-cookies backend" directive, it always returns 1.
1918 *
1919 * Grabs the proxy lock and each server's lock.
1920 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001921static int cli_parse_enable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001922{
1923 struct proxy *px;
1924 struct server *s;
1925
1926 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1927 return 1;
1928
1929 px = cli_find_backend(appctx, args[3]);
1930 if (!px)
1931 return 1;
1932
Willy Tarreau5e83d992019-07-30 11:59:34 +02001933 /* Note: this lock is to make sure this doesn't change while another
1934 * thread is in srv_set_dyncookie().
1935 */
Willy Tarreaua275a372018-08-21 14:50:44 +02001936 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001937 px->ck_opts |= PR_CK_DYNAMIC;
Willy Tarreau5e83d992019-07-30 11:59:34 +02001938 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001939
Willy Tarreaua275a372018-08-21 14:50:44 +02001940 for (s = px->srv; s != NULL; s = s->next) {
1941 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001942 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02001943 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
1944 }
1945
Olivier Houchard614f8d72017-03-14 20:08:46 +01001946 return 1;
1947}
1948
Willy Tarreaua275a372018-08-21 14:50:44 +02001949/* Parses the "disable dynamic-cookies backend" directive, it always returns 1.
1950 *
1951 * Grabs the proxy lock and each server's lock.
1952 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001953static int cli_parse_disable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001954{
1955 struct proxy *px;
1956 struct server *s;
1957
1958 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1959 return 1;
1960
1961 px = cli_find_backend(appctx, args[3]);
1962 if (!px)
1963 return 1;
1964
Willy Tarreau5e83d992019-07-30 11:59:34 +02001965 /* Note: this lock is to make sure this doesn't change while another
1966 * thread is in srv_set_dyncookie().
1967 */
Willy Tarreaua275a372018-08-21 14:50:44 +02001968 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001969 px->ck_opts &= ~PR_CK_DYNAMIC;
Willy Tarreau5e83d992019-07-30 11:59:34 +02001970 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001971
1972 for (s = px->srv; s != NULL; s = s->next) {
Willy Tarreaua275a372018-08-21 14:50:44 +02001973 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001974 if (!(s->flags & SRV_F_COOKIESET)) {
1975 free(s->cookie);
1976 s->cookie = NULL;
1977 }
Willy Tarreaua275a372018-08-21 14:50:44 +02001978 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01001979 }
1980
1981 return 1;
1982}
1983
Willy Tarreaua275a372018-08-21 14:50:44 +02001984/* Parses the "set dynamic-cookie-key backend" directive, it always returns 1.
1985 *
1986 * Grabs the proxy lock and each server's lock.
1987 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001988static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001989{
1990 struct proxy *px;
1991 struct server *s;
1992 char *newkey;
1993
1994 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1995 return 1;
1996
1997 px = cli_find_backend(appctx, args[3]);
1998 if (!px)
1999 return 1;
2000
Willy Tarreau9d008692019-08-09 11:21:01 +02002001 if (!*args[4])
2002 return cli_err(appctx, "String value expected.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01002003
2004 newkey = strdup(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02002005 if (!newkey)
2006 return cli_err(appctx, "Failed to allocate memory.\n");
Willy Tarreaua275a372018-08-21 14:50:44 +02002007
Willy Tarreau5e83d992019-07-30 11:59:34 +02002008 /* Note: this lock is to make sure this doesn't change while another
2009 * thread is in srv_set_dyncookie().
2010 */
Willy Tarreaua275a372018-08-21 14:50:44 +02002011 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002012 free(px->dyncookie_key);
2013 px->dyncookie_key = newkey;
Willy Tarreau5e83d992019-07-30 11:59:34 +02002014 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002015
Willy Tarreaua275a372018-08-21 14:50:44 +02002016 for (s = px->srv; s != NULL; s = s->next) {
2017 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002018 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02002019 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
2020 }
2021
Olivier Houchard614f8d72017-03-14 20:08:46 +01002022 return 1;
2023}
2024
Willy Tarreaua275a372018-08-21 14:50:44 +02002025/* Parses the "set maxconn frontend" directive, it always returns 1.
2026 *
2027 * Grabs the proxy lock.
2028 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002029static int cli_parse_set_maxconn_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002030{
2031 struct proxy *px;
2032 struct listener *l;
2033 int v;
2034
2035 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2036 return 1;
2037
2038 px = cli_find_frontend(appctx, args[3]);
2039 if (!px)
2040 return 1;
2041
Willy Tarreau9d008692019-08-09 11:21:01 +02002042 if (!*args[4])
2043 return cli_err(appctx, "Integer value expected.\n");
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002044
2045 v = atoi(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02002046 if (v < 0)
2047 return cli_err(appctx, "Value out of range.\n");
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002048
2049 /* OK, the value is fine, so we assign it to the proxy and to all of
2050 * its listeners. The blocked ones will be dequeued.
2051 */
Willy Tarreaua275a372018-08-21 14:50:44 +02002052 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
2053
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002054 px->maxconn = v;
2055 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002056 if (l->state == LI_FULL)
2057 resume_listener(l);
2058 }
2059
Willy Tarreau241797a2019-12-10 14:10:52 +01002060 if (px->maxconn > px->feconn)
2061 dequeue_proxy_listeners(px);
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002062
Willy Tarreaua275a372018-08-21 14:50:44 +02002063 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
2064
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002065 return 1;
2066}
2067
Willy Tarreaua275a372018-08-21 14:50:44 +02002068/* Parses the "shutdown frontend" directive, it always returns 1.
2069 *
2070 * Grabs the proxy lock.
2071 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002072static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau5212d7f2016-11-24 11:13:06 +01002073{
2074 struct proxy *px;
2075
2076 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2077 return 1;
2078
2079 px = cli_find_frontend(appctx, args[2]);
2080 if (!px)
2081 return 1;
2082
Willy Tarreauc3914d42020-09-24 08:39:22 +02002083 if (px->disabled)
Willy Tarreau9d008692019-08-09 11:21:01 +02002084 return cli_msg(appctx, LOG_NOTICE, "Frontend was already shut down.\n");
Willy Tarreau5212d7f2016-11-24 11:13:06 +01002085
Willy Tarreau5212d7f2016-11-24 11:13:06 +01002086 stop_proxy(px);
2087 return 1;
2088}
2089
Willy Tarreaua275a372018-08-21 14:50:44 +02002090/* Parses the "disable frontend" directive, it always returns 1.
2091 *
2092 * Grabs the proxy lock.
2093 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002094static int cli_parse_disable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01002095{
2096 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02002097 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01002098
2099 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2100 return 1;
2101
2102 px = cli_find_frontend(appctx, args[2]);
2103 if (!px)
2104 return 1;
2105
Willy Tarreauc3914d42020-09-24 08:39:22 +02002106 if (px->disabled)
Willy Tarreau9d008692019-08-09 11:21:01 +02002107 return cli_msg(appctx, LOG_NOTICE, "Frontend was previously shut down, cannot disable.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002108
Willy Tarreauf18d9682020-09-24 08:04:27 +02002109 if (!px->li_ready)
2110 return cli_msg(appctx, LOG_NOTICE, "All sockets are already disabled.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002111
Willy Tarreaua275a372018-08-21 14:50:44 +02002112 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
2113 ret = pause_proxy(px);
2114 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
2115
Willy Tarreau9d008692019-08-09 11:21:01 +02002116 if (!ret)
2117 return cli_err(appctx, "Failed to pause frontend, check logs for precise cause.\n");
2118
Willy Tarreau15b9e682016-11-24 11:55:28 +01002119 return 1;
2120}
2121
Willy Tarreaua275a372018-08-21 14:50:44 +02002122/* Parses the "enable frontend" directive, it always returns 1.
2123 *
2124 * Grabs the proxy lock.
2125 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002126static int cli_parse_enable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01002127{
2128 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02002129 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01002130
2131 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2132 return 1;
2133
2134 px = cli_find_frontend(appctx, args[2]);
2135 if (!px)
2136 return 1;
2137
Willy Tarreauc3914d42020-09-24 08:39:22 +02002138 if (px->disabled)
Willy Tarreau9d008692019-08-09 11:21:01 +02002139 return cli_err(appctx, "Frontend was previously shut down, cannot enable.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002140
Willy Tarreauf18d9682020-09-24 08:04:27 +02002141 if (px->li_ready == px->li_all)
2142 return cli_msg(appctx, LOG_NOTICE, "All sockets are already enabled.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002143
Willy Tarreaua275a372018-08-21 14:50:44 +02002144 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
2145 ret = resume_proxy(px);
2146 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
2147
Willy Tarreau9d008692019-08-09 11:21:01 +02002148 if (!ret)
2149 return cli_err(appctx, "Failed to resume frontend, check logs for precise cause (port conflict?).\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002150 return 1;
2151}
2152
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002153/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
2154 * now.
2155 */
2156static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
2157{
2158 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2159 return 1;
2160
2161 if (*args[2]) {
2162 struct proxy *px;
2163
2164 px = proxy_find_by_name(args[2], 0, 0);
2165 if (px)
2166 appctx->ctx.errors.iid = px->uuid;
2167 else
2168 appctx->ctx.errors.iid = atoi(args[2]);
2169
Willy Tarreau9d008692019-08-09 11:21:01 +02002170 if (!appctx->ctx.errors.iid)
2171 return cli_err(appctx, "No such proxy.\n");
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002172 }
2173 else
2174 appctx->ctx.errors.iid = -1; // dump all proxies
2175
2176 appctx->ctx.errors.flag = 0;
2177 if (strcmp(args[3], "request") == 0)
2178 appctx->ctx.errors.flag |= 4; // ignore response
2179 else if (strcmp(args[3], "response") == 0)
2180 appctx->ctx.errors.flag |= 2; // ignore request
2181 appctx->ctx.errors.px = NULL;
2182 return 0;
2183}
2184
2185/* This function dumps all captured errors onto the stream interface's
2186 * read buffer. It returns 0 if the output buffer is full and it needs
2187 * to be called again, otherwise non-zero.
2188 */
2189static int cli_io_handler_show_errors(struct appctx *appctx)
2190{
2191 struct stream_interface *si = appctx->owner;
2192 extern const char *monthname[12];
2193
2194 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
2195 return 1;
2196
2197 chunk_reset(&trash);
2198
2199 if (!appctx->ctx.errors.px) {
2200 /* the function had not been called yet, let's prepare the
2201 * buffer for a response.
2202 */
2203 struct tm tm;
2204
2205 get_localtime(date.tv_sec, &tm);
2206 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
2207 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2208 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
2209 error_snapshot_id);
2210
Willy Tarreau36b27362018-09-07 19:55:44 +02002211 if (ci_putchk(si_ic(si), &trash) == -1)
2212 goto cant_send;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002213
2214 appctx->ctx.errors.px = proxies_list;
2215 appctx->ctx.errors.bol = 0;
2216 appctx->ctx.errors.ptr = -1;
2217 }
2218
2219 /* we have two inner loops here, one for the proxy, the other one for
2220 * the buffer.
2221 */
2222 while (appctx->ctx.errors.px) {
2223 struct error_snapshot *es;
2224
Willy Tarreau36b27362018-09-07 19:55:44 +02002225 HA_SPIN_LOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
2226
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002227 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002228 es = appctx->ctx.errors.px->invalid_req;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002229 if (appctx->ctx.errors.flag & 2) // skip req
2230 goto next;
2231 }
2232 else {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002233 es = appctx->ctx.errors.px->invalid_rep;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002234 if (appctx->ctx.errors.flag & 4) // skip resp
2235 goto next;
2236 }
2237
Willy Tarreauc55015e2018-09-07 19:02:32 +02002238 if (!es)
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002239 goto next;
2240
2241 if (appctx->ctx.errors.iid >= 0 &&
2242 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
Olivier Houchardbdb00c52020-03-12 15:30:17 +01002243 (!es->oe || es->oe->uuid != appctx->ctx.errors.iid))
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002244 goto next;
2245
2246 if (appctx->ctx.errors.ptr < 0) {
2247 /* just print headers now */
2248
2249 char pn[INET6_ADDRSTRLEN];
2250 struct tm tm;
2251 int port;
2252
2253 get_localtime(es->when.tv_sec, &tm);
2254 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
2255 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2256 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
2257
2258 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
2259 case AF_INET:
2260 case AF_INET6:
2261 port = get_host_port(&es->src);
2262 break;
2263 default:
2264 port = 0;
2265 }
2266
2267 switch (appctx->ctx.errors.flag & 1) {
2268 case 0:
2269 chunk_appendf(&trash,
2270 " frontend %s (#%d): invalid request\n"
2271 " backend %s (#%d)",
2272 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Olivier Houchardbdb00c52020-03-12 15:30:17 +01002273 (es->oe && es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
2274 (es->oe && es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002275 break;
2276 case 1:
2277 chunk_appendf(&trash,
2278 " backend %s (#%d): invalid response\n"
2279 " frontend %s (#%d)",
2280 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Olivier Houchardbdb00c52020-03-12 15:30:17 +01002281 es->oe ? es->oe->id : "<NONE>" , es->oe ? es->oe->uuid : -1);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002282 break;
2283 }
2284
2285 chunk_appendf(&trash,
2286 ", server %s (#%d), event #%u, src %s:%d\n"
2287 " buffer starts at %llu (including %u out), %u free,\n"
2288 " len %u, wraps at %u, error at position %u\n",
2289 es->srv ? es->srv->id : "<NONE>",
2290 es->srv ? es->srv->puid : -1,
2291 es->ev_id, pn, port,
2292 es->buf_ofs, es->buf_out,
2293 global.tune.bufsize - es->buf_out - es->buf_len,
2294 es->buf_len, es->buf_wrap, es->buf_err);
2295
2296 if (es->show)
2297 es->show(&trash, es);
2298
2299 chunk_appendf(&trash, " \n");
2300
Willy Tarreau36b27362018-09-07 19:55:44 +02002301 if (ci_putchk(si_ic(si), &trash) == -1)
2302 goto cant_send_unlock;
2303
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002304 appctx->ctx.errors.ptr = 0;
2305 appctx->ctx.errors.ev_id = es->ev_id;
2306 }
2307
2308 if (appctx->ctx.errors.ev_id != es->ev_id) {
2309 /* the snapshot changed while we were dumping it */
2310 chunk_appendf(&trash,
2311 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau36b27362018-09-07 19:55:44 +02002312 if (ci_putchk(si_ic(si), &trash) == -1)
2313 goto cant_send_unlock;
2314
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002315 goto next;
2316 }
2317
2318 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau4bc7d902018-09-07 20:07:17 +02002319 while (appctx->ctx.errors.ptr < es->buf_len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002320 int newptr;
2321 int newline;
2322
2323 newline = appctx->ctx.errors.bol;
2324 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, appctx->ctx.errors.ptr);
2325 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau36b27362018-09-07 19:55:44 +02002326 goto cant_send_unlock;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002327
Willy Tarreau36b27362018-09-07 19:55:44 +02002328 if (ci_putchk(si_ic(si), &trash) == -1)
2329 goto cant_send_unlock;
2330
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002331 appctx->ctx.errors.ptr = newptr;
2332 appctx->ctx.errors.bol = newline;
2333 };
2334 next:
Willy Tarreau36b27362018-09-07 19:55:44 +02002335 HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002336 appctx->ctx.errors.bol = 0;
2337 appctx->ctx.errors.ptr = -1;
2338 appctx->ctx.errors.flag ^= 1;
2339 if (!(appctx->ctx.errors.flag & 1))
2340 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
2341 }
2342
2343 /* dump complete */
2344 return 1;
Willy Tarreau36b27362018-09-07 19:55:44 +02002345
2346 cant_send_unlock:
2347 HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
2348 cant_send:
Willy Tarreaudb398432018-11-15 11:08:52 +01002349 si_rx_room_blk(si);
Willy Tarreau36b27362018-09-07 19:55:44 +02002350 return 0;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002351}
2352
William Lallemanda6c5f332016-11-19 02:25:36 +01002353/* register cli keywords */
2354static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau15b9e682016-11-24 11:55:28 +01002355 { { "disable", "frontend", NULL }, "disable frontend : temporarily disable specific frontend", cli_parse_disable_frontend, NULL, NULL },
2356 { { "enable", "frontend", NULL }, "enable frontend : re-enable specific frontend", cli_parse_enable_frontend, NULL, NULL },
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002357 { { "set", "maxconn", "frontend", NULL }, "set maxconn frontend : change a frontend's maxconn setting", cli_parse_set_maxconn_frontend, NULL },
Willy Tarreau69f591e2020-07-01 07:00:59 +02002358 { { "show","servers", "conn", NULL }, "show servers conn [id]: dump server connections status (for backend <id>)", cli_parse_show_servers, cli_io_handler_servers_state },
William Lallemanda6c5f332016-11-19 02:25:36 +01002359 { { "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 +01002360 { { "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 +01002361 { { "shutdown", "frontend", NULL }, "shutdown frontend : stop a specific frontend", cli_parse_shutdown_frontend, NULL, NULL },
Olivier Houchard614f8d72017-03-14 20:08:46 +01002362 { { "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 },
2363 { { "enable", "dynamic-cookie", "backend", NULL }, "enable dynamic-cookie backend : enable dynamic cookies on a specific backend", cli_parse_enable_dyncookie_backend, NULL },
2364 { { "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 +02002365 { { "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 +01002366 {{},}
2367}};
2368
Willy Tarreau0108d902018-11-25 19:14:37 +01002369INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002370
2371/*
2372 * Local variables:
2373 * c-indent-level: 8
2374 * c-basic-offset: 8
2375 * End:
2376 */